Repository navigation
Description
mlx-community/gemma-4-e4b-it-4bit cannot be loaded. This is why integration_matrix (audio), (omni) and (opencode) fail on every PR — they are the three modalities that use this model, and they have been red regardless of the change under test.
Reproduced locally (not just CI):
.build/release/SwiftLM --model <gemma-4-e4b-it-4bit snapshot> --port 5470 --audio
Error: Key language_model.model.layers.24.self_attn.k_proj.weight not found in
Gemma4.Gemma4TextLanguageModel.Gemma4TextBackbone.Gemma4TextDecoderLayer.Gemma4TextAttention.LinearRoot cause
The checkpoint uses KV-shared layers. From its config.json:
num_hidden_layers: 42
num_kv_shared_layers: 18 → layers 24-41 reuse KV from earlier layers
and it therefore omits those projections:
layer 23 self_attn: k_norm, k_proj, o_proj, q_norm, q_proj, v_proj
layer 24 self_attn: o_proj, q_norm, q_proj ← no k_proj / v_proj / k_norm
Layer 24 is exactly 42 − 18, matching the first failing layer in the error.
MLXVLM/Models/Gemma4.swift parses the setting and computes the flag:
let firstKVSharedLayer = config.hiddenLayers - config.numKVSharedLayers
self.isKVSharedLayer = layerIdx >= firstKVSharedLayer && firstKVSharedLayer > 0 // line 632but isKVSharedLayer is never read again in that file outside the MLP's double-wide check. The attention module builds k_proj, v_proj and k_norm for every layer:
@ModuleInfo(key: "k_proj") var kProj: Linear // not optional
self._kProj.wrappedValue = Linear(...) // unconditionalso model.update(verify: .all) demands weights the checkpoint does not contain. vProj is already optional, but gated on attentionKEqV — a different feature — not on KV sharing.
MLXLLM/Models/Gemma4Text.swift has no KV-sharing support at all (zero references to numKVSharedLayers), which is why the opencode job fails in a different namespace (Gemma4Model.Gemma4TextModel.Gemma4TextModelInner).
Second symptom, same model
Error: Mismatched parameter language_model.model.per_layer_model_projection.weight
Actual [10752, 320], expected [10752, 2560]
2560 / 320 = 8, i.e. 4-bit packing density, so the checkpoint's tensor is quantized while the module receiving it is a plain Linear — the quantization predicate did not fire for that path. The config has no per-layer quantization entries (global 4-bit), so this looks like a module-path mismatch rather than a config issue. Not root-caused — noting it so it is not mistaken for a consequence of the KV-sharing gap.
Which error appears varies between retries. That is Swift Dictionary iteration order, not instability: whichever mismatch is reached first gets reported.
Scope
Implementing this means skipping the K/V projections and k_norm for shared layers and having those layers read K/V from the appropriate earlier layer's cache. The flag and config plumbing already exist; the forward path does not. It needs checking against the reference implementation, so I have not attempted it blind.
Until then the three jobs cannot pass. Options:
- Implement KV sharing in both Gemma4 implementations (correct fix).
- Point
audio/omni/opencodeat a Gemma 4 build without KV-shared layers, if one exists that still exercises those modalities. - Mark the three jobs
continue-on-errorwith a link here, so PR authors are not blocked by a known model-support gap.opencodeis already marked.
Related: SharpAI/mlx-swift-lm#43 fixes a different weight-loading defect (#118). Same area, different mechanism — it does not address this.
Environment: SwiftLM main after #119, mlx-swift-lm b9bf50b, macOS 26.5.2, M5 Pro.
Activity
fix: load Gemma 4 checkpoints with KV-shared layers (#44)
chore: advance mlx-swift-lm to include the Gemma 4 KV-shared fix
chore: bump mlx-swift-lm for the non-indexed weights fix (#118) (#122)
solderzzc commentedon Aug 7, 2026
Shipped in b674 — gemma-4-e4b-it-4bit (KV-shared layers) loads and generates correctly on the release binary.
test: cover load-path defect shapes with synthetic checkpoints (#143)