Repository navigation
fix(core): accumulate CPU quantized matmul in f32 for bf16 activations - #2261
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
MLXCEL_DEVICE=cpureturned unusable logits at 3x scalar speed.What changed
patches/mlx/backend/cpu/quantized.cppsynced to pin 81ba1c6a:_qmm,_qmm_t,fp_qmmandfp_qmm_taccumulate 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_overlayscopies the new tree on every build, since every backend compiles MLX's CPU backend;patches-rocm/README.md,docs/architecture.mdand theci.ymlpath filters record the overlay kind and its refresh rule.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
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 GPUdefault_w1.tsvchunk 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 failedmake verify-rocmunder the host GPU lock on gfx1151: 12,202 passed, 0 failed, 403 ignored, smoke OKCloses #2248