Skip to content

fix(loading): load text-only Gemma 4 and Gemma 3n exports - #2997

Merged
inureyes merged 3 commits into
lablup:mainfrom
Jaeyeong-CHOI:fix/gemma-text-only-layouts
Nov 14, 2026
Merged

inureyes merged 3 commits into
lablup:mainfrom
Jaeyeong-CHOI:fix/gemma-text-only-layouts

Conversation

@Jaeyeong-CHOI

Copy link
Copy Markdown
Contributor

Summary

Two text-only Gemma exports on mlx-community stopped at Weight 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 fix

Cause

  • Gemma 4. A text-only export in the mlx-lm layout names its tensors model.* and lm_head.*. The weight filter already lets those through (is_gemma4_text_weight), but the model, strip_gemma4_kv_shared_weights and the tied-embedding step all read language_model.model.*. The tensors were read from disk and then not found.
  • Gemma 3n. A language-model-only export keeps the Hugging Face names, model.language_model.*. Gemma3nModel::load, the directory loader mlxcel generate takes, and the weight-map route in loading::special both passed the fixed prefix "language_model.model". The multimodal loader resolves the prefix from the weights after stripping a leading model.; the text path did neither.

Change

  • adopt_gemma4_language_model_prefix (src/models/sanitize.rs): when a checkpoint has model.embed_tokens.weight and no language_model. tensor, its model.* and lm_head.* tensors are renamed under language_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, among language_model.model, language_model and model.language_model. Used by Gemma3nModel::load and by the weight-map loader. An unknown layout falls back to the first, so the error still names the tensor that was looked for.
  • No tensor value changes; only names are resolved.

Before / after

GB10 (sm_121, CUDA 13.0), generate -n 200 --temp 0. main is the release build of ce6855bc8; the fix column is a test-fast build of this branch whose three runtime files are identical to this commit. The last column is main on a copy of the checkpoint with only the tensors renamed to the layout main looks for.

Checkpoint Prompt main This PR main, tensors renamed
mlx-community/Gemma4-E2B-IT-Text-int4 @ 61d85e83c959 What is the capital of France? Answer in one word. Weight not found: language_model.model.embed_tokens.weight Paris Paris, byte-identical output
same What is 2 + 2? Answer with just the number. (same error) 4 4, byte-identical
mlx-community/gemma-3n-E2B-it-lm-4bit @ 62bd60f57959 capital Weight not found: language_model.model.embed_tokens.weight Paris. Paris., byte-identical
same 2 + 2 (same error) 4 4, byte-identical

The layouts that already load answer the second prompt as on main, byte for byte: gemma-4-e2b-it-4bit and gemma-3n-E2B-it-4bit.

Test plan

Run on this PR's source (one commit on ce6855bc8, tree 4797b61e95a7) on a DGX Spark (GB10).

  • cargo fmt --all -- --check
  • cargo clippy --workspace --all-targets --features cuda -- -D warnings (the Linux spelling of the template's line)
  • cargo build --release --features cuda
  • cargo test --profile test-fast --features cuda --lib -- --test-threads=1 for models::gemma3n (8 passed, 5 ignored), models::sanitize (88 passed) and loading:: (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 through Gemma3nModel::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 without lm_head so the tied head has to be found under the new prefix
  • Negative control, the fix taken out of the loaders with the tests kept: the Gemma 3n directory test fails on the missing embedding name and both Gemma 4 directory tests fail on the unrenamed names (6 passed, 3 failed in that selection). Restored, they pass
  • make 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 is native_env_tests::the_cuda_cache_defaults_reach_mlx, which fails the same way on a clean main (test(cuda): make verify-test-cuda fails the cache-default test on every CUDA host since #2876 #2987)
  • The self-hosted ci.yml jobs a pull request from a fork does not get, run by hand: clippy (cargo clippy -p mlxcel --lib --tests -- -D warnings, default features) and xla-compile (cargo check --features cuda,xla-iree --all-targets, cargo check --no-default-features --features xla-diagnostics --all-targets): all pass
  • make verify-python-tooling (219 tests OK), license headers, env-var docs, kernel dtype keys, kernel port dispatch, cross-repo refs
  • Validated with real checkpoints: the table above
  • cargo deny check: not run and not applicable (no manifest, lockfile or deny.toml change)
  • Metal and ROCm: not built or run here. The change resolves tensor names at load

Notes for reviewers

  • The table's fix column comes from a test-fast build, not from the release build of the test plan; the three runtime files are the same blobs. The release build was built and gated, and the four prompts were not repeated on it.
  • A dense language_model.* Gemma 3n checkpoint now also loads through the text path, as it does through the multimodal one. Before, the text path took only language_model.model.
  • A first run of the final tests was thrown away: it shared a Cargo target directory with another checkout and executed a stale test binary that did not contain the new tests. The numbers above are from a rerun with its own target directory, checked by test name.
  • Implemented and validated with AI assistance (Claude) driving the GB10 host. Every number above comes from the logs of those runs.

Checklist

  • PR title uses a Conventional Commits prefix
  • One logical change per PR
  • Updated docs/: not needed (no option, flag or documented behaviour changes)
  • Updated // Used by: ... comments: on the two new functions
  • No secrets, credentials, or .env files committed

Jaeyeong-CHOI and others added 3 commits November 12, 2026 14:14
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
@inureyes

Copy link
Copy Markdown
Member

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 (QuantizationArgs::quant_params_for). A mixed-bit text-only mlx-lm export keys those overrides as model.layers.N.mlp.*, but after adopt_gemma4_language_model_prefix the MLP asks for language_model.model.layers.N.mlp.*, so overridden projections would fall back to the root group size and bits. The lookup now falls back to the key without language_model. when the exact key is absent (the exact key still wins), with a unit test. I also merged main into the branch.

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):

  • models::gemma4_tests::gemma4_ (5), models::sanitize (88), models::gemma3n (8 passed, 5 ignored), loading:: (398 passed, 4 ignored); cargo clippy --lib --tests -- -D warnings clean; cargo fmt --check clean.
  • Release build, generate --temp 0: text-only exports derived from local gemma-3n-e2b-it-4bit (HF model.language_model.* names, no vision/audio) and gemma-4-12b-it-4bit (mlx-lm model.*/lm_head.*, gemma4_text) fail on the pre-PR binary with Weight not found: language_model.model.embed_tokens.weight and answer Paris./4 and Paris/4 with this branch. The original multimodal checkpoints still answer the same.

@inureyes
inureyes merged commit 56b318d into lablup:main Nov 14, 2026
33 checks passed
@inureyes inureyes added the status:done Completed label Nov 14, 2026
@inureyes inureyes added type:bug Bug fixes, error corrections, or issue resolutions priority:high High priority area:models Model architectures, weights, loading, metadata modeltype:text Text-only language model labels Nov 30, 2026
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:models Model architectures, weights, loading, metadata modeltype:text Text-only language model priority:high High priority status:done Completed type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(loading): load text-only Gemma 4 and Gemma 3n exports by resolving the weight prefix

2 participants