Repository navigation
fix(loading): load text-only Gemma 4 and Gemma 3n exports - #2997
Conversation
Two text-only Gemma exports stopped at "Weight not found: language_model.model.embed_tokens.weight". Their tensors are all there, under another name layout than the multimodal conversions. A text-only Gemma 4 export names its tensors `model.*` and `lm_head.*`. The Gemma 4 model, the KV-shared-layer strip and the tied-embedding step read `language_model.model.*`, so both text loaders now move such an export under that prefix. A checkpoint with any `language_model.` tensor is left as it is. A language-model-only Gemma 3n export keeps the Hugging Face names, `model.language_model.*`. The directory loader and the weight-map loader hard-coded `language_model.model`; both now pick the prefix the embedding table is found under, among the two the multimodal loader knows and that one. An unknown layout fails with the same error as before.
A text-only mlx-lm Gemma 4 export keys its per-module quantization overrides by the names it was saved under (`model.layers.N.mlp.down_proj`). After `adopt_gemma4_language_model_prefix` moves the weights under `language_model.`, the MLP asks for `language_model.model.layers.N...`, so a mixed-bit text-only export would silently fall back to the root group size and bits for every overridden projection. `QuantizationArgs::quant_params_for` now falls back to the key without the `language_model.` prefix when the exact key is absent. The exact multimodal key still wins, and multimodal checkpoints are unaffected. Validation: new unit test `gemma4_text_only_overrides_resolve_under_the_adopted_prefix`; `models::gemma4_tests::gemma4_`, `models::sanitize`, `models::gemma3n`, `loading::` selections pass; clippy `--lib --tests -D warnings` clean; release-build smoke on text-only exports derived from gemma-3n-e2b-it-4bit and gemma-4-12b-it-4bit (`Paris.`/`4`, `Paris`/`4`) and on the original multimodal checkpoints. Refs lablup#2996, refs lablup#2997
|
Thanks for the careful fix and the thorough write-up, @Jaeyeong-CHOI. The prefix resolution for both families matches the issue, and the multimodal layouts are untouched. One gap I found and fixed in fc192f4: Gemma 4 resolves per-module quantization overrides by the exact module name ( Security: no new input surface; only tensor and config key names are resolved. Performance: a one-time rename pass at load, no runtime cost. Validation on an M3 Ultra (Metal):
|
Summary
Two text-only Gemma exports on
mlx-communitystopped atWeight not found: language_model.model.embed_tokens.weight. Their tensors are all there, under another name layout than the multimodal conversions, and the text loaders hard-coded the multimodal prefix. Both layouts now load.Related issues
Closes #2996.
Type of change
fix— bug fixCause
model.*andlm_head.*. The weight filter already lets those through (is_gemma4_text_weight), but the model,strip_gemma4_kv_shared_weightsand the tied-embedding step all readlanguage_model.model.*. The tensors were read from disk and then not found.model.language_model.*.Gemma3nModel::load, the directory loadermlxcel generatetakes, and the weight-map route inloading::specialboth passed the fixed prefix"language_model.model". The multimodal loader resolves the prefix from the weights after stripping a leadingmodel.; the text path did neither.Change
adopt_gemma4_language_model_prefix(src/models/sanitize.rs): when a checkpoint hasmodel.embed_tokens.weightand nolanguage_model.tensor, itsmodel.*andlm_head.*tensors are renamed underlanguage_model.. Called by both Gemma 4 text loaders (load_gemma4_text_weights,load_gemma4_text_weights_with_backing), before the steps that read the multimodal names. Any other layout is left as it is.text_only_language_model_prefix(src/models/gemma3n.rs): the prefix under which the embedding table is found, amonglanguage_model.model,language_modelandmodel.language_model. Used byGemma3nModel::loadand by the weight-map loader. An unknown layout falls back to the first, so the error still names the tensor that was looked for.Before / after
GB10 (sm_121, CUDA 13.0),
generate -n 200 --temp 0.mainis the release build ofce6855bc8; the fix column is a test-fast build of this branch whose three runtime files are identical to this commit. The last column ismainon a copy of the checkpoint with only the tensors renamed to the layoutmainlooks for.mainmain, tensors renamedmlx-community/Gemma4-E2B-IT-Text-int4@61d85e83c959Weight not found: language_model.model.embed_tokens.weightParisParis, byte-identical output44, byte-identicalmlx-community/gemma-3n-E2B-it-lm-4bit@62bd60f57959Weight not found: language_model.model.embed_tokens.weightParis.Paris., byte-identical44, byte-identicalThe layouts that already load answer the second prompt as on
main, byte for byte:gemma-4-e2b-it-4bitandgemma-3n-E2B-it-4bit.Test plan
Run on this PR's source (one commit on
ce6855bc8, tree4797b61e95a7) on a DGX Spark (GB10).cargo fmt --all -- --checkcargo clippy --workspace --all-targets --features cuda -- -D warnings(the Linux spelling of the template's line)cargo build --release --features cudacargo test --profile test-fast --features cuda --lib -- --test-threads=1formodels::gemma3n(8 passed, 5 ignored),models::sanitize(88 passed) andloading::(398 passed, 4 ignored). New tests: the prefix choice; the tiny Gemma 3n model written to a directory under each of the three layouts and loaded throughGemma3nModel::load, with logits equal to the in-memory model; the Gemma 4 rename on a weight map; and a text-only Gemma 4 directory through each of the two loaders, one of them withoutlm_headso the tied head has to be found under the new prefixmake verify-test-cuda(cargo test --workspace --profile test-fast --features cuda --no-fail-fast -- --test-threads=1): 186 of 187 test binaries pass (15492 tests passed, 1 failed, 496 ignored), the three directory tests among them. The one failure isnative_env_tests::the_cuda_cache_defaults_reach_mlx, which fails the same way on a cleanmain(test(cuda): make verify-test-cuda fails the cache-default test on every CUDA host since #2876 #2987)ci.ymljobs a pull request from a fork does not get, run by hand:clippy(cargo clippy -p mlxcel --lib --tests -- -D warnings, default features) andxla-compile(cargo check --features cuda,xla-iree --all-targets,cargo check --no-default-features --features xla-diagnostics --all-targets): all passmake verify-python-tooling(219 tests OK), license headers, env-var docs, kernel dtype keys, kernel port dispatch, cross-repo refscargo deny check: not run and not applicable (no manifest, lockfile ordeny.tomlchange)Notes for reviewers
language_model.*Gemma 3n checkpoint now also loads through the text path, as it does through the multimodal one. Before, the text path took onlylanguage_model.model.Checklist
docs/: not needed (no option, flag or documented behaviour changes)// Used by: ...comments: on the two new functions.envfiles committed