Skip to content

fix(Gemma4): correct double-transpose bug in K-eq-V attention path (closes #59) - #23

Merged
solderzzc merged 1 commit into
mainfrom
fix/gemma4-keqv-transpose-issue-59
Apr 16, 2026
Merged

solderzzc merged 1 commit into
mainfrom
fix/gemma4-keqv-transpose-issue-59

Conversation

@solderzzc

Copy link
Copy Markdown
Member

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 via LLMModelFactory.

Root Cause

In Gemma4Text.swift, the Gemma4Attention.callAsFunction builds k then v:

k = kProj(x)...
k = kNorm(k)
k = k.transposed(0, 2, 1, 3)  // ← k is now [B, nKvH, L, D]
k = rope(k, …)

// K-eq-V path (useKeqV = true):
v = k                          // ← v aliases already-transposed k
v = vNorm(v)
v = v.transposed(0, 2, 1, 3)  // ← BUG: produces [B, L, nKvH, D]

The extra transpose flips v into [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 vProj is nil (K-eq-V), apply vNorm directly to the already-transposed k and skip the transpose:

if let vProj {
    v = vProj(x).reshaped(...)
    v = vNorm(v)
    v = v.transposed(0, 2, 1, 3)
} else {
    // k is already [B, nKvH, L, D] — just norm, no re-transpose
    v = vNorm(k)
}

Stretch: StreamableMoE for the VLM path

Also adds LayerPartitionable + StreamableMoE conformance to Gemma4TextBackbone (VLM Gemma4.swift), mirroring the existing pattern in Qwen35.swift. Wires streamExperts pass-through on the public Gemma4 VLM model so ModelContainer.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

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
solderzzc force-pushed the fix/gemma4-keqv-transpose-issue-59 branch from bd03e7c to effc211 Compare April 16, 2026 22:08
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
@solderzzc
solderzzc merged commit 3e10527 into main Apr 16, 2026
6 checks passed
@solderzzc
solderzzc deleted the fix/gemma4-keqv-transpose-issue-59 branch April 16, 2026 22:44
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.

[Bug]: MLXLLM Gemma 4 MoE forward crashes with broadcast_shapes (1,512,2,512) vs (1,2,512,512)

1 participant