Skip to content

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

Closed

Description

@inureyes

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 in float, only when simd::max_size<T> % (32 / bits) == 0 (:299).
  • simd/type.h includes the Accelerate SIMD header only when MLX_USE_ACCELERATE is set and the target is not __x86_64__. Every other build (all of Linux, including aarch64 hosts such as GB10, plus x86 macOS) gets only base_simd.h, where max_size = 1 (base_simd.h:24). So every transposed affine quantized_matmul there runs the scalar _qmm_t (:155-210), which accumulates in T sum = 0 (:177) and computes scale * w + bias in T.
  • 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

  1. Confirm the path. Add a test in src/lib/mlxcel-core/src/ (next to the existing quantized-matmul tests) that runs quantized_matmul on 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.
  2. If bf16 error is far above f32 (relative L2 above 1e-2, f32 below 1e-4), add patches/mlx/backend/cpu/quantized.cpp with _qmm_t and _qmm accumulating in float (float sum, scale and bias widened to float, one cast to T on store), and add a copy rule for it in mlx_apply_source_overlays that 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.md counts), so update whatever it requires. The overlay stays in mlxcel and is not proposed upstream.
  3. 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 in src/commands/generate.rs:93 and 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 cpubf16 arm for one chunk gives a top-1 logit within 0.1 of f32refcpu_w1x3.tsv chunk 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

Related: PR #2240, #2154.

Activity

  1. added
    status:ready
    type:bug
    priority:medium
    area:core
    on Oct 8, 2026
  2. added a commit that references this issue on Oct 12, 2026
    d49b664
  3. added this to the 0.8 milestone on Nov 30, 2026
  4. added
    status:done
    and removed
    status:ready
    on Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:corepriority:mediumstatus:donetype:bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions