Repository navigation
Conversation
There was a problem hiding this comment.
Code Review
This pull request adds support for the vision tower (multimodal E2B / E4B) in the GEMMA4_SMALL_HF_WEIGHTS_TO_SHAPE function within hf_shape.py. It maps the shapes of the vision tower components, including the patch embedder, position embedding table, standardization parameters, projection layers, and encoder layers, when a vision_config is present. Additionally, a comprehensive suite of unit tests has been introduced in hf_shape_test.py to validate this mapping behavior. There are no review comments, and I have no additional feedback to provide.
GEMMA4_SMALL_HF_WEIGHTS_TO_SHAPE returned only language-model / PLE shapes and
emitted zero model.vision_tower.* / model.embed_vision.* keys, while
GEMMA4_SMALL_MAXTEXT_TO_HF_PARAM_MAPPING already maps the full vision encoder.
Since utils._process requires every mapped HF target to be present in the shape
map, 'to_huggingface use_multimodal=true' on a converted gemma4-e2b/e4b
checkpoint died on the first vision tensor:
ValueError: HF path 'model.vision_tower.patch_embedder.input_proj.weight'
not found in hf_shape_map
Add the vision block to GEMMA4_SMALL_HF_WEIGHTS_TO_SHAPE, gated on vision_config
presence (matching the mapping's 'use_multimodal and vcfg' gate) and mirroring
the GEMMA4_HF_WEIGHTS_TO_SHAPE (26B/31B) vision convention: each clipped-linear
vision projection emits a '.linear.weight'; attention q/k/v/o + q_norm/k_norm,
the four layernorms, gate/up/down MLP, patch-embedder input_proj +
position_embedding_table, and the embed_vision projection. std_scale / std_bias
are emitted only under standardize=true (E2B/E4B ship standardize=false).
Also emit the four scalar activation clip bounds per clipped-linear projection
(input_min/input_max/output_min/output_max, shape [] not [1]) that
GEMMA4_SMALL_MAXTEXT_TO_HF_PARAM_MAPPING maps under use_clipped_linears_for_vit.
The shape fn sees only the HF config (not the MaxText config), so it emits the
bound shapes whenever the vision block is present: _process is driven by the
param map, so the extra keys are a harmless superset when clipped-linears is off
and required coverage when it is on. A rank-1 [1] shape would make the HF loader
reinitialize the bound to a non-finite sentinel.
Add tests/unit/hf_shape_test.py: fail-closed coverage (every mapped vision HF
target, including the 448 clip bounds when the running param map supports them,
exists in the shape map), clip-bound scalar-shape, std-conditional, and
text-only-config checks.
Text-only and 26B/31B export paths are unaffected.
Signed-off-by: Loki Chen <dengcchi@meta.com>
d4eabe0 to
7eb0f94
Compare
csgoogle
left a comment
There was a problem hiding this comment.
Review of #4860 — the emitted vision-tower keys and shapes in hf_shape.py match HF Gemma-4 and MaxText's param mapping accurately. Three substantive items before merge:
-
Unit tests never assert shapes of non-clip vision tensors (
tests/unit/hf_shape_test.py:97-107):test_vision_tower_keys_presentonly checksassertInon 5 keys; none of the non-clip vision weights (input_proj,position_embedding_table,embedding_projection,q/k/v/o_proj,mlp, norms,std_scale/std_bias) have their shapes asserted.- In addition,
_gemma4_e2b_configuses square 768×768 dimensions forinput_projandq/k/v/o_proj, so transpose bugs would pass silently even if shapes were checked on that fixture alone. Please assert exact shapes for all emitted keys (including an asymmetric-dim case) and add coverage against the realhf_model_configsE2B/E4B dicts andvision_config=None.
-
to_huggingfacehazard forstd_scale/std_biasplaceholder (src/maxtext/checkpoint_conversion/utils/param_mapping.py:3419-3423):- When
standardize=False,GEMMA4_SMALL_MAXTEXT_TO_HF_PARAM_MAPPINGmaps bothstd_scaleandstd_biasto"model.vision_tower.encoder.layers.0.input_layernorm.weight", which only avoids overwritinglayers.0.input_layernorm.weightinto_huggingface.pydue to dict insertion order. Oncestd_scale/std_biasare kept unconditionally asUntrainableParam(perlokic233/maxtext#4/#6262), every E2B/E4Bto_huggingfaceexport hits this path — worth guarding or adding a realprocess_maxtext_paramround-trip test.
- When
-
Stale base & missing CI: This branch is ~3 months behind
main(predatingb656c919f5inhf_shape.py) and has never run pytest/lint CI; please rebase ontomainand trigger CI.
— Review assisted by Jetski agent
|
This change is included in #6621, with the two review points fixed: the shape tests compare exact shapes on a config whose sizes all differ, and |
Description
GEMMA4_SMALL_HF_WEIGHTS_TO_SHAPE(used bygemma4-e2b/gemma4-e4b) returned only language-model / PLEshapes and emitted zero
model.vision_tower.*/model.embed_vision.*keys, whileGEMMA4_SMALL_MAXTEXT_TO_HF_PARAM_MAPPINGmaps the full vision encoder. Becausecheckpoint_conversion/utils/utils.py::_processrequires every mapped HF target to be present in the shapemap,
to_huggingface use_multimodal=trueon a converted Gemma-4 E2B/E4B checkpoint died on the first visiontensor:
This adds the vision block to
GEMMA4_SMALL_HF_WEIGHTS_TO_SHAPE, gated onvision_configpresence:GEMMA4_HF_WEIGHTS_TO_SHAPE(26B/31B) convention: each clipped-linear vision projection emitsa
.linear.weight; attention q/k/v/o + q_norm/k_norm, four layernorms, gate/up/down MLP, patch-embedderinput_proj+position_embedding_table,embed_visionprojection.(
input_min/input_max/output_min/output_max) thatGEMMA4_SMALL_MAXTEXT_TO_HF_PARAM_MAPPINGmaps underuse_clipped_linears_for_vit(the E2B/E4B image-parity path). Bounds are scalar[](a rank-1[1]shapemakes the HF loader reinitialize the bound to a non-finite sentinel). The shape fn receives only the HF
config (not the MaxText config), and
_processiterates the param map, so emitting the bound shapeswhenever the vision block is present is a harmless superset when clipped-linears is off and required
coverage when it is on.
std_scale/std_biasare emitted only understandardize=true(E2B/E4B shipstandardize=false).Scope: this PR completes the vision HF export shape map for gemma4-small. Shared-layer text K/V /
k_norm are intentionally omitted on the HF side (HF transformers
Gemma4TextAttentionitself omits them forKV-shared layers), so this is HF-faithful; the serving-side shared-layer checkpoint-completeness invariant is
a separate concern and is not part of this PR.
FIXES: to_huggingface multimodal export for gemma4-e2b / gemma4-e4b.
Tests
tests/unit/hf_shape_test.py(CPU, no gated checkpoint):test_shape_map_covers_every_mapped_vision_target— fail-closed: every mapped vision HF target has a shape.test_shape_map_covers_clipped_mapping_targets— fail-closed incl. the 448 clip bounds when the param map maps them.test_clip_bound_keys_present_and_scalar— clip bounds present and scalar[].test_vision_tower_keys_present,test_std_keys_only_when_standardize,test_text_only_config_emits_no_vision_keys.Validated composed with #4790 (which adds the clip-bound mapping): mapping<->shape coverage = 0 missing
across clipped ON/OFF x standardize ON/OFF.
pyink/pylint(10.00/10)/codespellclean.Related work
GEMMA4_SMALL_MAXTEXT_TO_HF_PARAM_MAPPING(incl. clip-bound mapping this PR covers).Checklist
Authored with Navi on behalf of Loki Chen (@lokic233).