Skip to content

fix(olmoe): q_norm/k_norm applied after head reshape crashes OLMoE-1B-7B (rms_norm dim mismatch) #316

Closed

Description

@inureyes

Problem

OLMoE aborts at the first decode step on mlx-community/OLMoE-1B-7B-0125-Instruct-4bit:

libc++abi: terminating due to uncaught exception of type std::invalid_argument: [rms_norm] (*weight) must have the same size as the last dimension of x but has 2048 elements.

The model loads, then crashes during generation. OLMoE is effectively non-functional for this checkpoint. This is pre-existing and unrelated to the fused decode-MoE wiring (epic #307); the crash is in the attention QK-norm path, not the MoE path.

Root cause

In src/models/olmoe.rs, Attention::forward reshapes Q/K to [batch, seq_len, n_heads, head_dim] (head_dim = 128 for this model) and then applies q_norm/k_norm to the reshaped tensors (see src/models/olmoe.rs:218-225). But OLMoE's QK-norm weights are full-projection sized (num_heads * head_dim = hidden = 2048), matching the HF OLMoE convention where the RMSNorm is applied to the flattened q/k projection before the head reshape. Applying a 2048-element norm weight to a tensor whose last dim is head_dim (128) is the dimension mismatch that aborts.

Repro

mlxcel download mlx-community/OLMoE-1B-7B-0125-Instruct-4bit
mlxcel generate -m <path> -p "Hello, my name is" -n 20 -t 0 --no-chat-template

Fix

Apply q_norm/k_norm to the flattened projection output ([batch, seq_len, num_heads*head_dim]) BEFORE the head reshape, then reshape. This matches the 2048-element weight and the upstream OLMoE order (norm over the full projection, then split into heads). RoPE and the transpose stay after the reshape as today. Reference: ml-explore/mlx-lm OLMoE attention (q_norm/k_norm precede the head reshape).

Acceptance Criteria

  • q_norm/k_norm are applied before the [b, l, heads, head_dim] reshape, on the full projection, so the 2048-element norm weights match.
  • OLMoE-1B-7B (mlx-community/OLMoE-1B-7B-0125-Instruct-4bit) loads and generates coherent greedy temp-0 output with no crash and no NaN/garbage.
  • No regression to other models (the change is confined to olmoe.rs attention).
  • Add or extend a test where practical.

Notes

Found while validating epic #307 (#302 wires olmoe to the fused decode-MoE kernel). #302's MoE wiring cannot be runtime-validated until this attention crash is fixed.

Activity

  1. added
    type:bug
    priority:high
    area:models
    platform:macos
    status:ready
    status:in-progress
    status:done
    and removed
    status:ready
    status:in-progress
    on Jun 16, 2026
  2. added this to the 0.3 milestone on Jun 21, 2026
  3. self-assigned this
    on Jun 21, 2026
  4. added 2 commits that reference this issue on Oct 27, 2026
    c3a1558
    79cb580
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:modelsplatform:macospriority:highstatus:donetype:bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions