Skip to content

Commit 6cd76fe

Browse files
pcullitoncopybara-github
authored andcommitted
Serialization: EnumValid uses switch, not sentinel; add framing
Calling visitor() on a sub-struct adds size field, whereas the direct call to VisitFields did not. This should be a no-op for existing models. PiperOrigin-RevId: 955295371
1 parent c48a9ae commit 6cd76fe

2 files changed

Lines changed: 34 additions & 11 deletions

File tree

gemma/configs.h

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,24 +153,34 @@ AttentionImpl GetAttentionImpl(const std::string& impl);
153153
enum class PostNormType {
154154
None,
155155
Scale,
156-
kSentinel // must be last
157156
};
158157

159158
static inline bool EnumValid(PostNormType type) {
160-
return static_cast<size_t>(type) <
161-
static_cast<size_t>(PostNormType::kSentinel);
159+
switch (type) {
160+
case PostNormType::None:
161+
case PostNormType::Scale:
162+
return true;
163+
default:
164+
return false;
165+
}
162166
}
163167

164168
// Post qk projection operation type.
165169
enum class PostQKType {
166170
Rope,
167171
HalfRope,
168172
NormLocalRope = 8, // Norm without scale, and rope for local attention layers
169-
kSentinel // must be last
170173
};
171174

172175
static inline bool EnumValid(PostQKType type) {
173-
return static_cast<size_t>(type) < static_cast<size_t>(PostQKType::kSentinel);
176+
switch (type) {
177+
case PostQKType::Rope:
178+
case PostQKType::HalfRope:
179+
case PostQKType::NormLocalRope:
180+
return true;
181+
default:
182+
return false;
183+
}
174184
}
175185

176186
// FFW activation function.
@@ -215,12 +225,15 @@ static inline bool EnumValid(QueryScaleType type) {
215225
// Residual connection type.
216226
enum class ResidualType {
217227
Add,
218-
kSentinel // must be last
219228
};
220229

221230
static inline bool EnumValid(ResidualType type) {
222-
return static_cast<size_t>(type) <
223-
static_cast<size_t>(ResidualType::kSentinel);
231+
switch (type) {
232+
case ResidualType::Add:
233+
return true;
234+
default:
235+
return false;
236+
}
224237
}
225238

226239
template <size_t kNum>
@@ -314,6 +327,8 @@ void ForEachModel(const Func& func) {
314327
}
315328
}
316329

330+
static inline bool IsInternal(Model model) { return false; }
331+
317332
static inline bool EnumValid(Model model) {
318333
// Valid for purposes of serialization, even if unknown.
319334
if (model == Model::UNKNOWN) return true;
@@ -360,7 +375,7 @@ struct LayerConfig : public IFields {
360375
visitor(activation);
361376
visitor(post_qk);
362377
visitor(use_qk_norm);
363-
internal.VisitFields(visitor);
378+
// Visiting includes size prefix, whereas calling VisitFields would inline.
364379
visitor(norm_v);
365380
visitor(num_experts);
366381
visitor(num_experts_per_datapoint);
@@ -611,7 +626,7 @@ struct ModelConfig : public IFields {
611626

612627
visitor(scale_base_names);
613628

614-
internal.VisitFields(visitor);
629+
// Visiting includes size prefix, whereas calling VisitFields would inline.
615630

616631
visitor(use_global_timescale);
617632
visitor(partial_rotary_factor);

python/convert_from_safetensors.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1181,7 +1181,15 @@ def add_cross_projection(prefix, hf_name, sbs_name, i):
11811181
raise ValueError(
11821182
f"{sbs_model_specifier!r} is not an encoder-decoder config."
11831183
)
1184-
writer.write(sbs_config, tokenizer_file)
1184+
if tokenizer_file.endswith(".json"):
1185+
sbs_config.tokenizer_kind = configs.TokenizerKind.kHfBpe
1186+
tokenizer_blob = pack_bpe_tokenizer(tokenizer_file)
1187+
else:
1188+
sbs_config.tokenizer_kind = configs.TokenizerKind.kSentencePiece
1189+
with open(tokenizer_file, "rb") as f:
1190+
tokenizer_blob = f.read()
1191+
writer.write(sbs_config, tokenizer_blob)
1192+
11851193

11861194
with open(csv_file, "w") as csv_handle:
11871195
csv.writer(csv_handle).writerows(metadata)

0 commit comments

Comments
 (0)