mlx icon indicating copy to clipboard operation
mlx copied to clipboard

[Bug] MoE LoRA training crashes on M5 Max — missing float32 NAX gather_mm kernel

Open Snail3D opened this issue 7 months ago • 0 comments

Bug

LoRA training of MoE models (Qwen3.5-35B-A3B) on Apple M5 Max crashes with:

RuntimeError: [metal::Device] Unable to load function steel_gather_mm_rhs_nax_nt_float32_float32_bm16_bn128_bk128_wm1_wn4
Function steel_gather_mm_rhs_nax_nt_float32_float32_bm16_bn128_bk128_wm1_wn4 was not found in the library

Root Cause

mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_gather_nax.metal only instantiates gather_mm_rhs_nax for float16 and bfloat16:

instantiate_gather_mm_shapes_helper(float16, half, float16, half);
instantiate_gather_mm_shapes_helper(bfloat16, bfloat, bfloat16, bfloat);

No float32 instantiation exists. When TF32 is enabled (default on M5), GatherMM::eval_gpu routes float32 ops to the NAX path because enable_tf32() returns true. The NAX gather_mm kernel doesn't have a float32 variant, causing the crash.

Workaround

export MLX_ENABLE_TF32=0

This routes float32 operations to the standard (non-NAX) gather_mm path which works.

Proper Fix

Add float32 instantiation to steel_gemm_gather_nax.metal:

instantiate_gather_mm_shapes_helper(float32, float, float32, float);

Or, the dispatch logic in matmul.cpp should exclude float32 from the NAX path when the kernel isn't available.

Reproduction

pip install mlx==0.31.1 mlx-lm==0.31.1
python -m mlx_lm lora \
  --model mlx-community/Qwen3.5-35B-A3B-4bit \
  --data ./data --train --batch-size 1 --iters 10
# Crashes immediately

With workaround:

MLX_ENABLE_TF32=0 python -m mlx_lm lora \
  --model mlx-community/Qwen3.5-35B-A3B-4bit \
  --data ./data --train --batch-size 1 --iters 10
# Works: 235 tok/s, val loss 0.522

Environment

  • MLX 0.31.1
  • macOS 26, Apple M5 Max 128GB, Metal 4
  • Model: Qwen3.5-35B-A3B-4bit (MoE, 256 experts)

Snail3D avatar Apr 03 '26 20:04 Snail3D