Skip to content

perf(moe): wire mixtral to the fused decode-MoE kernel #301

Closed

Description

@inureyes

Parent epic: #307. Sibling: #286 (qwen2_moe, same pattern).

Background

The fused single-token decode-MoE kernel (#268 series) and the default-on flip (#282, PR #285) are wired into only four model paths: qwen3_moe, qwen3_next, dots1, gemma4. #286 wires qwen2_moe. Several more MoE families reuse a SwitchGLU for the expert matmul but never call forward_fused_kernel, so they always fall back to gather_qmm regardless of MLXCEL_FUSED_MOE. This issue wires mixtral.

The canonical one-branch pattern lives in src/models/qwen3_moe.rs, src/models/dots1.rs, and src/models/qwen3_next.rs: gate on single-token decode plus fused_moe_enabled() (src/models/switch_layers.rs:42), call experts.forward_fused_kernel(&x_flat, &topk_indices, &scores) (src/models/switch_layers.rs:276), and fall back to experts.forward(...) plus moe_weighted_sum(...) (src/models/switch_layers.rs:419).

Scope

File: src/models/mixtral.rs. SparseMoeBlock::forward is at line 281 (expert call at 313, moe_weighted_sum at 315). Routing: softmax over top-k, no shared expert. mixtral experts are SwiGLU (SiLU), so the kernel runs with the default swiglu activation (act=0); no GeGLU/relu2 template is needed.

Implementation class: Group B (needs SwitchGLU consolidation first). mixtral defines its OWN local SwitchGLU (line 156) and a local SwitchLinear that do NOT expose forward_fused_kernel. Before the one-branch dispatch can work, migrate the experts to the shared crate::models::switch_layers::SwitchGLU (preferred: removes duplication) OR add a forward_fused_kernel method to the local struct. Prefer migration. Verify there is no mixtral-specific behavior in the local struct before removing it.

Validation checkpoint: mlx-community/Mixtral-8x7B-Instruct-v0.1-4bit (in the project regression set; about 24 GB at 4-bit, fits 128 GB).

Acceptance Criteria

  • mixtral's MoE block dispatches the fused kernel for single-token decode when MLXCEL_FUSED_MOE is enabled (default) and the config qualifies; the non-fused fallback path is unchanged.
  • The local SwitchGLU/SwitchLinear is migrated to the shared switch_layers::SwitchGLU (or a forward_fused_kernel method is added), the duplicate is removed where migrated, and the existing model forward stays correct.
  • INTEGRATION: the change is wired into the actual mixtral forward decode path (not an orphaned helper). Confirm the kernel actually dispatches at runtime via a dispatch trace, not inferred from byte-identical output.
  • Validate on mlx-community/Mixtral-8x7B-Instruct-v0.1-4bit: greedy temp-0 output is byte-identical or within the documented f16 jitter class vs the MLXCEL_FUSED_MOE=0 (gather_qmm) baseline, with no NaN and no garbage.
  • Measure decode tok/s fused-on vs off; record the gain; confirm no regression.
  • Update the "Models covered" list in docs/benchmark_results/fused-moe-decode-kernel-design.md to move mixtral to the wired list.
  • Add or extend tests where practical.

Technical Considerations / References

Activity

  1. added
    type:performance
    type:refactor
    priority:medium
    area:models
    area:inference
    platform:macos
    status:ready
    on Jun 16, 2026
  2. added
    status:in-progress
    status:review
    status:done
    and removed
    status:ready
    status:in-progress
    status:review
    on Jun 16, 2026
  3. added this to the 0.3 milestone on Jun 21, 2026
  4. self-assigned this
    on Jun 21, 2026
  5. removed
    type:refactor
    on Sep 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:inferencearea:modelsplatform:macospriority:mediumstatus:donetype:performance

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions