Skip to content

fix: Gemma 4 review follow-ups — assistant-case parity and KV-shared tests - #45

Merged
solderzzc merged 2 commits into
mainfrom
fix/gemma4-followups
Aug 7, 2026
Merged

solderzzc merged 2 commits into
mainfrom
fix/gemma4-followups

Conversation

@solderzzc

Copy link
Copy Markdown
Member

The deferred LOW findings from the #44 review.

Changes

1. The two implementations disagreed on the assistant case. MLXVLM/Models/Gemma4.swift kept a firstKVSharedLayer > 0 clause that MLXLLM/Models/Gemma4Text.swift does not have. For an assistant model (hiddenLayers == numKVSharedLayers, boundary 0) the VLM built K/V for every layer while the LLM built none — and the checkpoint ships none. The clause was never needed for numKVSharedLayers == 0 either: the boundary equals hiddenLayers there, so no layer is shared regardless. Removed from both the attention and double-wide-MLP sites.

2. Dropped the redundant deprecated toQuantized(groupSize:bits:) overloads. The Quantizable protocol extension already supplies them, and the requirement is marked deprecated — matching how SwitchLinear implements only the mode variant.

3. Added the regression tests the changed branch had none of. Every config in Gemma4Tests.swift used num_kv_shared_layers: 0, so the entire KV-sharing branch was uncovered:

  • a 4-layer / 2-shared config (gemma-4-e4b's 42/18 shape at tiny scale) asserting shared layers build no k_proj/v_proj/k_norm while owning layers do
  • a forward pass with cache: nil — the path that fatalErrored once shared layers stopped building their own projections, and the specific crash fix: load Gemma 4 checkpoints with KV-shared layers #44's review caught

Verification

  • Verified red: forcing hasKv = true fails the projection test on every shared layer.
  • All 15 Gemma4Tests pass.
  • Re-checked against real checkpoints on this branch: gemma-4-e4b-it-4bit answers correctly with --audio and plain, Qwen3.6-27B-OptiQ-4bit unaffected.

Note for anyone running the suite locally: the tests need mlx.metallib colocated with the xctest bundle, otherwise MLX aborts before any test executes.

🤖 Generated with Claude Code

Aegis AI Assistant and others added 2 commits August 7, 2026 11:21
…tests

Deferred LOW findings from the #44 review.

- MLXVLM and MLXLLM disagreed on the assistant case. The VLM kept a
  `firstKVSharedLayer > 0` clause, so for an assistant model
  (hiddenLayers == numKVSharedLayers, boundary 0) it built K/V for every
  layer while MLXLLM built none — and the checkpoint ships none. The clause
  was never needed for numKVSharedLayers == 0 either: the boundary equals
  hiddenLayers there, so no layer is shared regardless. Removed from both the
  attention and the double-wide-MLP sites.
- Dropped the redundant deprecated `toQuantized(groupSize:bits:)` overloads;
  the Quantizable protocol extension already supplies them, matching how
  SwitchLinear implements only the mode variant.
- Added the regression tests the changed branch had none of: a 4-layer /
  2-shared config asserting that shared layers build no k_proj, v_proj or
  k_norm while owning layers do, and a forward pass with cache: nil, which is
  the path that used to fatalError once shared layers stopped building their
  own projections.

Verified red: forcing hasKv = true fails the projection test on every shared
layer. All 15 Gemma4 tests pass. Re-checked against the real checkpoints —
gemma-4-e4b-it-4bit generates correctly with --audio and plain, and
Qwen3.6-27B-OptiQ-4bit is unaffected.

Co-Authored-By: Claude Opus 5 <[email protected]>
Review of this PR surfaced a regression already merged and shipped in SwiftLM
b674. #44 assumed that a checkpoint with num_kv_shared_layers omits
k_proj/v_proj/k_norm for its shared layers, which holds for
gemma-4-e4b-it-4bit (42 layers / 18 shared, weights for the first 24 only) but
not for gemma-4-e2b-it-4bit (35 / 20, weights for all 35). Since #44 the
latter fails to load:

    Error: Unable to set language_model.model.layers.15.self_attn.k_proj

Confirmed by bisect: e2b answers correctly on the pre-#44 submodule and fails
on it. The config key alone cannot decide whether to build the modules, so the
modules are still built per the config and sanitize now discards the vestigial
weights — those layers reuse an earlier layer's K/V and never project their
own, so the extra tensors are unused at inference.

Also in this commit, from the same review:

- The MLP site in Gemma4Text kept `firstKvSharedLayerIdx > 0` while this PR
  removed it from both MLXVLM sites, so the two implementations disagreed on
  MLP width for an assistant model — the exact divergence the PR claimed to
  remove. Aligned.
- Removed the dead `hasExplicitCache` binding left by #44.

Verified against every Gemma 4 checkpoint on hand: e2b, e4b and 26b-a4b all
load and answer correctly. 17 Gemma4 tests pass, including a new one pinning
the sanitize behaviour.

Co-Authored-By: Claude Opus 5 <[email protected]>
@solderzzc
solderzzc merged commit b320bc4 into main Aug 7, 2026
6 checks passed
@solderzzc
solderzzc deleted the fix/gemma4-followups branch August 7, 2026 21:30
solderzzc added a commit that referenced this pull request Aug 15, 2026
Coverage measurement (SharpAI/SwiftLM#146) put the entire VLM half of the model
layer at 0.3% over 13,304 lines, with zero test files under Tests/MLXLMTests
touching MLXVLM at all. MLXVLM/Models/Gemma4.swift — where #45's changes
landed, and the file issue ml-explore#128 named specifically — is 1,771 lines and none
of them ran.

Vision models are exercised end to end by the vision and omni CI jobs, so the
gap was never "this code doesn't run" — it was that a shape or dtype fault
inside one surfaces as a wrong answer about an image rather than a failing
assertion, discovered downstream and expensively.

Four tests, tiny random-init configs in the style of the existing Gemma4Tests:
config decoding for both the text and vision halves, instantiation, a
text-only forward pass (the path every request without an image attachment
takes), and determinism. Establishes shape and finiteness, not numerical
correctness — real checkpoints remain the only way to judge output quality.

Full suite: 108 tests, 18 suites, passing.

Co-authored-by: Aegis AI Assistant <[email protected]>
Co-authored-by: Claude Opus 5 <[email protected]>
solderzzc added a commit to SharpAI/SwiftLM that referenced this pull request Aug 15, 2026
Tier 4 of #128, which asked for `--enable-code-coverage` to find blind spots
objectively rather than by eye. This runs it and aggregates the result into
something readable: coverage per area, plus the largest files no test executes.

Both suites are measured, because reading either alone misleads. From the
umbrella package the model architectures look like 0%, which is not because they
are untested but because their tests live in the submodule. Measured separately:

  SwiftLM package        server 24.4%, inference core 57.7%, SwiftBuddy 5.3%
  mlx-swift-lm submodule LLM models 24.1%, VLM models 0.3%, MLXLMCommon 41.3%

Two findings worth acting on.

#128 named `MLXVLM/Models/Gemma4.swift` as having no tests despite being where
SharpAI/mlx-swift-lm#45's changes landed. Measurement confirms it and adds that
it is the single largest untested file in the model layer, at 1771 lines.

More striking is the pattern around it: the VLM half of the model layer is at
**0.3% over 13,304 lines** — Qwen3VL, Qwen35, LFM2VL, GlmOcr, Qwen25VL, Gemma3,
Pixtral, FastVLM are all at zero. CI does run vision and omni end-to-end tests,
so these paths are exercised; nothing touches them at unit level, which is why a
shape or dtype mistake inside one surfaces only as a wrong image answer.

Deliberately not wired into CI. A coverage gate rejects work for touching an
already-thin file and invites tests written to raise a number, and the run costs
a full debug rebuild of both packages. This is a tool to answer "where are we
blind" when someone asks, not a check to pass.

The script says so itself: coverage measures execution, not correctness — a
covered line may be asserted on by nothing at all.

Co-authored-by: Claude Opus 5 <[email protected]>
solderzzc added a commit to SharpAI/SwiftLM that referenced this pull request Aug 15, 2026
Points at acb6020 (SharpAI/mlx-swift-lm#49), which adds VLMGemma4Tests — the
first unit tests for anything under MLXVLM. Measured coverage had that half of
the model layer at 0.3% over 13,304 lines; this is the first shape/config/
forward-pass test against MLXVLM/Models/Gemma4.swift, the largest untested file
in the model layer and where SharpAI/mlx-swift-lm#45's changes landed.

No SwiftLM-side code changes; verified the umbrella builds clean against the
bumped pointer.

Co-authored-by: Claude Opus 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant