Skip to content

fix(core): accumulate CPU quantized matmul in f32 for bf16 activations - #2261

Merged
inureyes merged 2 commits into
mainfrom
fix/issue-2248-cpu-qmm-f32-accum
Oct 10, 2026
Merged

inureyes merged 2 commits into
mainfrom
fix/issue-2248-cpu-qmm-f32-accum

Conversation

@inureyes

Copy link
Copy Markdown
Member

Summary

  • MLX's scalar CPU quantized matmul loops kept their running sum in the activation type, so a bf16 projection summed its 2048 products in an 8-bit mantissa and MLXCEL_DEVICE=cpu returned unusable logits at 3x scalar speed.

What changed

  • New overlay patches/mlx/backend/cpu/quantized.cpp synced to pin 81ba1c6a: _qmm, _qmm_t, fp_qmm and fp_qmm_t accumulate in float, widen the activation row and scale/bias once, and round to T on store; the SIMD variants are upstream verbatim.
  • mlx_apply_source_overlays copies the new tree on every build, since every backend compiles MLX's CPU backend; patches-rocm/README.md, docs/architecture.md and the ci.yml path filters record the overlay kind and its refresh rule.
  • New cpu_qmm_accum_tests.rs: 4 tests, 12 cases (affine transposed and plain, 3/6/8-bit, MXFP4; bf16/f16/f32) against a host f64 reference.

Reproduction

  • Without the overlay the step-1 test fails: bf16 rel L2 5.164e-2, 31x the store-rounding floor (f16 6.703e-3, 31x; f32 unchanged). With it every 16-bit case sits at ratio 1.00 of the floor and f32 stays below 1e-6.
  • cpubf16 rerun on gfx1151 (logit_trace 1 1 8 0, granite-4.0-h-tiny-4bit): top-1 logit 18.875000 in 125 s, was 14.50 after a 600 s timeout; byte-identical top-1 to the GPU default_w1.tsv chunk 0. The remaining 0.24 gap to the f32 CPU reference is the bf16-vs-f32 head error the GPU shows too, so the issue's within-0.1 bound sits below the intrinsic bf16-vs-f32 gap at this position.

Test plan

  • cargo test -p mlxcel-core --lib cpu_qmm_accum_tests: 4 passed, 0 failed (all 16-bit cases at ratio 1.00)
  • cargo test -p mlxcel-core quantized -- --test-threads=1: 39 passed, 0 failed
  • make verify-rocm under the host GPU lock on gfx1151: 12,202 passed, 0 failed, 403 ignored, smoke OK

Closes #2248

The scalar _qmm, _qmm_t, fp_qmm and fp_qmm_t loops in MLX's CPU backend kept their running sum, and computed scale * w + bias, in the activation type T. Without Accelerate's SIMD header (every Linux build, x86 macOS) simd::max_size is 1, so every transposed affine quantized_matmul runs the scalar loops and a bf16 projection summed 2048 products in an 8-bit mantissa: MLXCEL_DEVICE=cpu returned granite w1 top-1 logit 14.50 against 18.63 f32 and 18.875 GPU, at 3x scalar speed.

- New overlay patches/mlx/backend/cpu/quantized.cpp, synced to pin 81ba1c6a: the four marked bodies accumulate in float, widen the activation row and scale/bias once, and round to T on store; the SIMD variants and everything else are upstream verbatim.
- mlx_apply_source_overlays copies the new tree on every build, since every backend compiles MLX's CPU backend and can run a quantized matmul on the CPU stream; patches-rocm/README.md and docs/architecture.md record the overlay and its refresh rule.
- New cpu_qmm_accum_tests.rs: 4 tests, 12 cases (affine transposed and plain, 3/6/8-bit, MXFP4; bf16/f16/f32) against a host f64 reference; ci.yml path filters name the new tree for the CUDA, HIP and cpu_link jobs.

Validated on gfx1151: the reproduction test fails without the overlay (bf16 rel L2 5.164e-2, 31x the store-rounding floor; f16 6.703e-3, 31x) and passes with it (every 16-bit case at ratio 1.00, f32 below 1e-6); cargo test -p mlxcel-core quantized 39 passed; make verify-rocm under the host GPU lock: 12,202 passed, 0 failed, 403 ignored, smoke OK.

Refs #2248
Re-ran the cpubf16 arm of benchmarks/logit_traces/rocm_gfx1151_a18d3d76 after the #2248 fix, same command shape on the same checkpoint and corpus: top-1 logit 18.875000 at chunk 0 in 125 s elapsed with no timeout, where the recorded pre-#2248 trace hit a 600 s timeout at 14.50. The fixed CPU bf16 output matches the GPU default_w1.tsv chunk 0 top-1 logit exactly; the remaining 0.24 gap to the f32 CPU reference is the bf16-vs-f32 head error the GPU shows too, which is why the issue's within-0.1 bound sits below the intrinsic bf16-vs-f32 gap at this position.

The rerun's tsv is kept in the ledger directory with its SHA256SUMS entry, and RUNS.txt records the run at the #2248 fix branch rather than a18d3d7.

Refs #2248
@inureyes
inureyes merged commit d49b664 into main Oct 10, 2026
25 of 27 checks passed
@inureyes
inureyes deleted the fix/issue-2248-cpu-qmm-f32-accum branch October 10, 2026 14:42
@inureyes inureyes added this to the 0.8 milestone Nov 30, 2026
@inureyes inureyes added area:core mlxcel-core: MLX FFI, primitives, KV cache, layers priority:medium Medium priority status:done Completed type:bug Bug fixes, error corrections, or issue resolutions labels Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core mlxcel-core: MLX FFI, primitives, KV cache, layers priority:medium Medium priority status:done Completed type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(core): accumulate CPU quantized matmul in f32 for bf16 activations

1 participant