Repository navigation
Description
Problem / Background
MLXCEL_DEVICE=cpu gives inaccurate, very slow output for bf16 quantized checkpoints on x86 Linux (the gfx1151 host), and by the analysis below on every non-Accelerate build. While explaining granite-4.0-h-tiny's w1 perplexity gap (PR #2240), the CPU bf16 arm was unusable as a reference. Its top-1 logit at chunk 0 was 14.50, against 18.63 for the f32 reference and 18.875 on the GPU, and each chunk took about 400 s (benchmarks/logit_traces/rocm_gfx1151_a18d3d76/README.md:14, :49). Anyone who uses the CPU device as a reference or fallback there gets wrong logits with no warning.
Current Behavior
The likely cause comes from reading upstream MLX at the pinned commit 81ba1c6a (src/lib/mlx-cpp/CMakeLists.txt:178). The path has not been confirmed at runtime. In mlx/backend/cpu/quantized.cpp:
_qmm_dispatch_transpose(:287-305) uses_qmm_t_simd, which accumulates infloat, only whensimd::max_size<T> % (32 / bits) == 0(:299).simd/type.hincludes the Accelerate SIMD header only whenMLX_USE_ACCELERATEis set and the target is not__x86_64__. Every other build (all of Linux, including aarch64 hosts such as GB10, plus x86 macOS) gets onlybase_simd.h, wheremax_size = 1(base_simd.h:24). So every transposed affinequantized_matmulthere runs the scalar_qmm_t(:155-210), which accumulates inT sum = 0(:177) and computesscale * w + biasinT.- For bf16 that means a bf16 running sum over all of K (8 mantissa bits), which fits the size of the error. The scalar loop with no SIMD also explains the speed.
mlxcel has no CPU-backend overlay today. mlx_apply_source_overlays (CMakeLists.txt:18) copies a Metal file list, patches/mlx/backend/cuda/, patches-cuda/ and the patches-rocm/ tree.
Proposed Solution
- Confirm the path. Add a test in
src/lib/mlxcel-core/src/(next to the existing quantized-matmul tests) that runsquantized_matmulon the CPU stream for a bf16[1, 2048] x [2048, 2048]affine 4-bit, group 64 weight, against an f32 dequantize-then-matmul reference. On the x86 host, record the relative error for bf16 and f32 activations in the PR. - If bf16 error is far above f32 (relative L2 above 1e-2, f32 below 1e-4), add
patches/mlx/backend/cpu/quantized.cppwith_qmm_tand_qmmaccumulating infloat(float sum, scale and bias widened to float, one cast toTon store), and add a copy rule for it inmlx_apply_source_overlaysthat applies on every build. Each backend's overlay ledger must record it: the ROCm build checks overlay files (make verify-rocm-overlay,patches-rocm/README.mdcounts), so update whatever it requires. The overlay stays in mlxcel and is not proposed upstream. - Only if the overlay cannot pass parity, fall back to a one-time warning when a quantized checkpoint with bf16 activations is loaded with
MLXCEL_DEVICE=cpu. It would be emitted next to the device selection insrc/commands/generate.rs:93and the server equivalent, and name the f32 alternative.
Out of scope: CPU speed (a SIMD x86 _qmm_t), and fp_qmm (mxfp4/nvfp4) CPU paths unless step 1 shows the same defect.
Acceptance Criteria
- The step 1 test exists and passes on x86 with bf16 relative L2 error at or below 2x the f32 error.
- Re-running the
cpubf16arm for one chunk gives a top-1 logit within 0.1 off32refcpu_w1x3.tsvchunk 0. Update the README row. - Metal and CUDA outputs unchanged (the overlay touches only the CPU backend).
Verification
cargo test -p mlxcel-core quantized -- --test-threads=1
make verify
make verify-rocm-overlay
Activity
fix(core): accumulate CPU quantized matmul in f32 for bf16 activations (
update: advance MLX pin to 5a48f9cc (#3517)