Repository navigation
fix(Gemma4): correct double-transpose bug in K-eq-V attention path (closes #59) - #23
Merged
Merged
Conversation
solderzzc
added a commit
to SharpAI/SwiftLM
that referenced
this pull request
Apr 16, 2026
…mlx-swift-lm#23) Points the submodule at the fix/gemma4-keqv-transpose-issue-59 branch which corrects the double-transpose in Gemma4Attention (K-eq-V path) causing [broadcast_shapes] crashes on all Gemma 4 MoE models. Also adds LayerPartitionable + StreamableMoE conformance to Gemma4TextBackbone so the VLM path can engage SSD expert streaming. Closes #59
When attention_k_eq_v=true (full attention layers in the 26b-a4b MoE
model), the Swift port had a subtle bug:
k = kProj(x).reshaped(...) → [B, L, nKvH, D]
k = kNorm(k) → [B, L, nKvH, D]
k = k.transposed(0,2,1,3) → [B, nKvH, L, D] ← TRANSPOSED
k = rope(k, …)
// useKeqV branch — v aliased to the already-transposed k:
v = k → [B, nKvH, L, D]
v = vNorm(v) → [B, nKvH, L, D]
v = v.transposed(0,2,1,3) → [B, L, nKvH, D] ← WRONG LAYOUT
This produced the crash:
[broadcast_shapes] Shapes (1,512,2,512) and (1,2,512,512) cannot
be broadcast together
Fix: when vProj is nil (K-eq-V path), call vNorm directly on the
already-transposed k and skip the redundant transpose. The vNorm
applies on the last axis regardless of layout, so the result is
correct [B, nKvH, L, D] — exactly what SDPA expects.
Tested: swift build --target MLXVLM, swift build --target MLXLLM,
swift test --filter Gemma4 all pass.
Closes #59 (LLM path fix)
Stretch: Also adds LayerPartitionable + StreamableMoE conformance to
Gemma4TextBackbone (VLM path) and wires streamExperts through the
public Gemma4 VLM model, mirroring Qwen35.swift. This allows
ModelContainer.setStreamExperts(true) to engage SSD expert streaming
for multimodal Gemma 4 MoE inference, closing the remaining VLM
streaming gap noted in the issue.
solderzzc
force-pushed
the
fix/gemma4-keqv-transpose-issue-59
branch
from
April 16, 2026 22:08
bd03e7c to
effc211
Compare
solderzzc
added a commit
to SharpAI/SwiftLM
that referenced
this pull request
Apr 16, 2026
…mlx-swift-lm#23) Points the submodule at the fix/gemma4-keqv-transpose-issue-59 branch which corrects the double-transpose in Gemma4Attention (K-eq-V path) causing [broadcast_shapes] crashes on all Gemma 4 MoE models. Also adds LayerPartitionable + StreamableMoE conformance to Gemma4TextBackbone so the VLM path can engage SSD expert streaming. Closes #59
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Fixes the
[broadcast_shapes] (1,512,2,512) vs (1,2,512,512)crash that affects all Gemma 4 MoE models (gemma-4-26b-a4b-it-*) loaded viaLLMModelFactory.Root Cause
In
Gemma4Text.swift, theGemma4Attention.callAsFunctionbuildskthenv:The extra transpose flips
vinto[B, L, nKvH, D]instead of the correct[B, nKvH, L, D]layout SDPA expects, causing the broadcast error at the first forward call.Fix
When
vProjis nil (K-eq-V), applyvNormdirectly to the already-transposedkand skip the transpose:Stretch: StreamableMoE for the VLM path
Also adds
LayerPartitionable+StreamableMoEconformance toGemma4TextBackbone(VLM Gemma4.swift), mirroring the existing pattern inQwen35.swift. WiresstreamExpertspass-through on the publicGemma4VLM model soModelContainer.setStreamExperts(true)can engage SSD expert streaming for multimodal Gemma 4 MoE.Testing
swift build --target MLXLLM✅swift build --target MLXVLM✅swift test --filter Gemma4Tests✅Closes SharpAI/SwiftLM#59