Skip to content

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

Closed
SharpAI/mlx-swift-lm
#23

Description

@notatestuser

Summary

After the Gemma 4 MoE loading fix in PR #21 (commit 7fb05f7, tag b430 of SharpAI/mlx-swift-lm), mlx-community/gemma-4-26b-a4b-it-*bit and every other Gemma 4 MoE export tested loads successfully via LLMModelFactory.shared but crashes on the first forward pass with:

MLX/ErrorHandler.swift:345: Fatal error: [broadcast_shapes]
Shapes (1,512,2,512) and (1,2,512,512) cannot be broadcast
  at mlx-c/mlx/c/ops.cpp:3150

The crash is deterministic across prompts, fires before the first output token, and is independent of: quantization bits, model publisher, kvBits (TurboKV) setting, ssdStreamExperts setting, speculative decoding on/off, and draft-model choice.

The shapes suggest an attention-mask broadcast mismatch — (B=1, Q=512, ?, K=512) against (B=1, ?, H=512, K=512) — likely around createAttentionMask / per-layer mask application in Gemma4TextModelInner where head_dim = 512.

Environment

SharpAI/mlx-swift-lm tag b430 (commit 7fb05f7)
SharpAI/mlx-swift branch: main @ commit 26f1e1c (tag b460)
Platform macOS 26.3, Apple M2 Pro (32 GB)
Path MLXLLM/Models/Gemma4.swift + Gemma4Text.swift, forward of Gemma4ModelInternal

Reproduction

Load and generate with any Gemma4 MoE model via LLMModelFactory.shared:

let config = ModelConfiguration(id: "mlx-community/gemma-4-26b-a4b-it-4bit")
let container = try await LLMModelFactory.shared.loadContainer(
    from: hubDownloader, using: tokenizerLoader, configuration: config
) { _ in }
try await container.perform { context in
    let input = try await context.processor.prepare(input: UserInput(chat: [
        .init(role: .user, content: "Hello")
    ]))
    let params = GenerateParameters(temperature: 0.7)   // kvBits on/off both crash
    _ = try MLXLMCommon.generate(input: input, parameters: params, context: context)
}
// → Fatal error: [broadcast_shapes] (1,512,2,512) vs (1,2,512,512)

Models tested (all hit the same crash)

All load successfully, all crash at the first forward call:

Model Quant Publisher
mlx-community/gemma-4-26b-a4b-it-4bit 4-bit mlx-community
mlx-community/gemma-4-26b-a4b-it-8bit 8-bit mlx-community
unsloth/gemma-4-26b-a4b-it-MLX-8bit 8-bit unsloth
zecanard/gemma-4-26B-A4B-it-Claude-Opus-Distilled-v2-MLX-6bit-int6-affine int6 affine zecanard

A fifth variant, inferencerlabs/gemma-4-26B-A4B-MLX-9bit, fails earlier at weight load with a shape mismatch (likely unrelated — SwitchGLU.SwitchLinear expected [128, 2816, 704] got [128, 2816, 176], a 4× packing ratio — quantization layout incompatibility rather than the broadcast bug). Reporting here for completeness.

Setting combinations tried (all produce the same broadcast_shapes crash)

kvBits (TurboKV) ssdStreamExperts Draft model Result
nil (off) off none crash
4 off none crash
4 on none crash
4 on mlx-community/gemma-4-e2b-it-4bit crash
0 (off) on none crash

What works (for comparison)

Loading the exact same mlx-community/gemma-4-26b-a4b-it-*bit weights via VLMModelFactory.shared (the MLXVLM/Models/Gemma4.swift path) loads and generates fine. However, that class does not conform to StreamableMoE, so ModelContainer.setStreamExperts(true) returns false — SSD expert streaming does not engage and peak RAM equals the full model size. So the VLM path is not a workaround for users who need the streaming behavior that the StreamableMoE-conforming Gemma4ModelInternal provides.

Qwen3.5 / Qwen3.6 MoE models (mlx-community/Qwen3.6-35B-A3B-{4,5,8}bit, jedisct1/Qwen3.6-35B-A3B-q4-mlx) under the same harness and fork tag work end-to-end with ssdStream=active and peak RAM ~4–6 GB on a 32 GB M2 Pro — so the SSD streaming infrastructure and setStreamExperts(...) wiring are not themselves broken.

Impact

Gemma 4 MoE currently cannot be used with SSD expert streaming at all on this fork:

  • LLM path: crashes at first forward (this issue)
  • VLM path: loads and runs, but Gemma4Model (in MLXVLM/Models/Gemma4.swift) does not conform to StreamableMoE — streaming cannot engage

A fix on the LLM side would unblock Gemma 4 MoE + SSD streaming end-to-end (text-only). Adding StreamableMoE conformance on the VLM side — mirroring the Qwen35 VLM class (MLXVLM/Models/Qwen35.swift:838) which derives from the same pattern — would additionally unblock the VLM streaming path.

Suggested next steps (for whoever picks this up)

  1. Locate the per-layer mask broadcast in Gemma4TextModelInner forward. The offending op is almost certainly where a [1, Q, 1, K] causal/sliding mask meets a [1, 1, H, K] head-broadcast tensor (or vice versa) for head_dim = 512.
  2. Repro locally with any of the 4 Gemma4 MoE variants listed above via LLMModelFactory + MLXLMCommon.generate(...).
  3. As a stretch, also add : LayerPartitionable, StreamableMoE conformance + partitionedLayerCall(...) wrapping in the VLM Gemma4Model (mirroring MLXVLM/Models/Qwen35.swift which already does this).

Activity

  1. added 2 commits that reference this issue on Apr 16, 2026
    0613b7c
    6d08be9
  2. added a commit that references this issue on Apr 16, 2026
    90e294b
  3. solderzzc commented on Apr 16, 2026

    @solderzzc
    Member

    Waiting for #60

  4. added a commit that references this issue on Apr 16, 2026
    ef2fca9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions