Skip to content

Read f16 scales natively on the CPU; mixed path on CPU and Metal only - #11

Merged
jjang-ai merged 3 commits into
osaurus-ai:mainfrom
rcfa:fix/cpu-mixed-qmv
Nov 22, 2026
Merged

jjang-ai merged 3 commits into
osaurus-ai:mainfrom
rcfa:fix/cpu-mixed-qmv

Conversation

@rcfa

@rcfa rcfa commented Sep 28, 2026 •

Copy link
Copy Markdown

What failed

For bf16 activations with f16 scales and biases (affine, group size 64, 4 or 8 bits, one row of x), quantized_matmul takes a mixed path that hands the scales and biases to the kernels unconverted and returns bf16. Its gate held on any device. The Metal kernels read that layout; the CPU kernels read scales and biases in the activations' dtype, so on a CPU stream the f16 bit patterns were read as bf16 and the result was wrong, with no error. gather_qmm avoided this only by keeping CPU streams off its mixed path, so it returned float32 on the CPU and bf16 on Metal.

The design

This replaces the PR's earlier design, which kept CPU streams off the mixed path and returned float32 there. That made the output dtype depend on the device, and the promoted path converts every scale and bias to float32 on every call, a separate pass over the metadata in exactly the one-row decode case the gate covers.

Now the CPU kernels read the f16 scales and biases natively: _qmm_t widens one scale and one bias per group, sums in float32 and rounds once at the output. So on the CPU and on Metal the output dtype is the same, and there is no conversion pass. Every other combination of types computes exactly as before.

Width CPU Metal GPU CUDA
4, 8 bf16 bf16 float32
6 float32 float32 float32

The sorted-matrix gather_qmm is float32 everywhere: Metal's matrix kernel reads the metadata in the activations' dtype, and the CPU follows it so the dtype does not depend on the device.

Numerics. float32 accumulation and one rounding, as Metal's qmv_fast_impl. The CPU sums x·(s·q + b), while Metal factors s·Σx·q + b·Σx, so on general inputs the two agree to float32 rounding, not bit for bit. The tests use inputs whose sums are exact in any order, so they compare exactly.

CUDA

Both functions keep CUDA on the promoted path at 4 and 8 bits. For quantized_matmul that is a change in behaviour: CUDA returned bf16 before, probably wrong, and returns float32 now. The reason is that cuda/quantized/qmm/qmv.cu reads scales and biases as the output type (:439, :401-412, :460-461). On sm90, a one-row call with N or K over 16384 goes to qmm_sm90 instead (cuda/quantized/quantized.cpp:106-110), and that kernel was not read. All of this is read from the code, not run; nothing here builds CUDA. CUDA can join once its kernels read the mixed types.

The commits

  1. Read f16 scales and biases natively in the bf16 CPU quantized matmul. _qmm_t and its dispatch chain take the scales' type separately from x's. quantized_matmul's mixed path runs only where a kernel reads that layout: the CPU and a Metal GPU (reads_mixed_bf16_f16).
  2. Take the mixed bf16/f16 gather_qmm path on the CPU too. The gather dispatch uses the same kernels, and its gate the same helper. The selected-consumer test now expects bf16 on the CPU and on Metal for the vector case.
  3. Require f16 scales on the mixed bf16/f16 quantized path. Both gates chose the mixed path from the type the scales and biases promote to, not from the scales' own type, so integer scales such as int16 (which promote with f16 to f16) took it uncast and were read as bf16. This predates the branch: on main it gave wrong results in quantized_matmul on the CPU and Metal, and in gather_qmm on Metal. Both gates now require f16 scales, and the CPU dispatches check the kernels' precondition (one type for scales and biases, x's or f16 for bf16 x, and an output of x's type) and throw on anything else.

Verification

Every test compares against a float32 reference built from the quantized codes and a BLAS matmul, so it never runs through the code under test. The new cases run on the CPU and, where present, on Metal.

Reds, each before its own fix:

  • Commit 1: the CPU failed at 4 and 8 bits on x86-64 and arm64 Linux and on macOS; Metal passed, since it reads f16 natively.
  • Commit 2: 24 checks failed, every non-matrix CPU combination (float32 instead of bf16); none on Metal.
  • Commit 3: every check failed, at 4 and 8 bits, for both ops, on the CPU and on Metal.

Tips (Clang; 16 CPU-JIT cases skipped on Linux):

Commit x86-64 Linux arm64 Linux
1 265 passed, 3999 assertions 265 passed, 3999 assertions
2 266 passed, 4006 assertions 266 passed, 4006 assertions
3 267 passed, 4014 assertions 267 passed, 4014 assertions

macOS at commit 3, with Metal: 311 cases passed with DEVICE=cpu (5064 assertions) and on Metal (5043). One Metal run failed test concurrent allocator statistics (tests/allocator_tests.cpp:190), a test this branch does not touch. It is intermittent and also fails at main: interleaved with this branch, it failed 9 of 20 runs on each binary.

Mutations, each made alone, each failing only its own checks:

  • the bf16 dispatch always taking the single-type kernel; summing in bf16; the scale rounded to bf16; the bias rounded to bf16: commit 1's value checks;
  • each gate without its CPU part: that op's dtype checks;
  • the gather dispatch always taking the single-type kernel: commit 2's value checks;
  • each gate without the f16-scales check: that op's int16 checks, with the CPU guard firing.

The gates' CPU and Metal parts ran here; their CUDA part did not.

For #10 and #18

Their Highway quantized backends still read the metadata as x's type. Once rebased on this, a Highway build takes the mixed path on the CPU, misreads it, and fails these new cases until a follow-up gives those backends the same native reads.

🤖 Generated with Claude Code

@rcfa
rcfa changed the base branch from qwen4-exp to main October 13, 2026 15:39
@rcfa
rcfa force-pushed the fix/cpu-mixed-qmv branch from f4f37e0 to 59f5ddf Compare October 13, 2026 15:40
rcfa added 3 commits October 22, 2026 01:55
quantized_matmul hands bf16 activations with f16 scales and biases to
the kernels unconverted when the mixed QMV conditions hold, and at 4
and 8 bits they hold on any device. The Metal kernels read that layout.
The CPU kernels read scales and biases in the activations' dtype, so on
a CPU stream the f16 bit patterns were read as bf16 and the result was
wrong, with no error. This commit's test compares quantized_matmul on
the CPU and, where present, on Metal with the float32 product of the
same values, rounded once to bf16. Without the fix it failed on the CPU
at 4 and 8 bits; at 4 bits:

    tests/quantized_stream_tests.cpp:282: ERROR: CHECK( array_equal(y, astype(ref, bfloat16, cpu), false, cpu).item<bool>() ) is NOT correct!
      values: CHECK( false )

On Metal it passed before the fix, since Metal reads f16 natively.

_qmm_t and its dispatch chain now take the type of the scales and
biases separately from x's. For bf16 x with f16 scales and biases,
_qmm_t widens one scale and one bias per group, sums in float32 and
rounds once at the output, as the Metal kernels do, with no conversion
pass over the metadata. Every other combination of types computes as
before. Mixed types with untransposed weights throw; the ops never pass
them.

The mixed path now runs only where a kernel reads that layout: on the
CPU, and on a Metal GPU. CUDA keeps the promoted float32 path, because
its qmv reads scales and biases as the output type (read from the code,
not run). This changes CUDA's output at 4 and 8 bits from bf16,
probably wrong, to float32, so CUDA's dtype still differs from the
CPU's and Metal's.
gather_qmm took the mixed bf16/f16 path only on a Metal GPU (0f9b6f3),
because the CPU kernels read scales and biases in the activations'
dtype. The previous commit gave the shared CPU kernels native f16
reads. This one routes the gather dispatch to them, and its gate calls
the previous commit's reads_mixed_bf16_f16, so gather_qmm returns bf16
on the CPU and on Metal, as quantized_matmul does. CUDA keeps the
promoted float32 path in both functions. Sorted matrix rows still
promote on every device: Metal's matrix consumer reads the metadata in
the activations' dtype, and the CPU follows it so the dtype does not
depend on the device.

This commit's test compares gather_qmm on the CPU and, where present,
on Metal with the float32 product of the same values, rounded once to
bf16. Before this change the CPU returned float32; the first failure:

    tests/quantized_stream_tests.cpp:333: ERROR: CHECK( g.dtype() == bfloat16 ) is NOT correct!
      values: CHECK( float32 == bfloat16 )

On Metal it passed before this change.

The selected-consumer test expected float32 on the CPU for the vector
case. It now expects bf16 on the CPU and on Metal.
quantized_matmul and gather_qmm chose the mixed bf16/f16 path from the
type that the scales and biases promote to, and from the biases' type,
but not from the scales' own type. Integer scales, such as int16,
promote with f16 biases to f16, so int16 scales took the mixed path
uncast. The CPU and Metal dispatches choose their bf16/f16 kernels only
for f16 scales, so the bf16 kernels ran and read the int16 scales and
the f16 biases as bf16. The result was wrong, with no error.

The bug predates this branch: on main, int16 scales gave wrong results
this way in quantized_matmul on the CPU and on Metal, and in gather_qmm
on Metal. This commit's test gives both ops bf16 x, int16 scales and
f16 biases, on the CPU and, where present, on Metal, and expects the
promoted path: float32 output equal to the float32 product of the same
values. Before this change every check failed, at 4 and 8 bits, on the
CPU and on Metal. The first failure on the CPU:

    tests/quantized_stream_tests.cpp:393: ERROR: CHECK( y.dtype() == float32 ) is NOT correct!
      values: CHECK( bfloat16 == float32 )

On Metal, the first failure:

    tests/quantized_stream_tests.cpp:393: ERROR: CHECK( y.dtype() == float32 ) is NOT correct!
      values: CHECK( bfloat16 == float32 )
      logged: device := Device(gpu, 0)

Both gates now require f16 scales, in place of f16 as the type that the
scales and biases promote to, so other scale types promote. The CPU
dispatches check the kernels' precondition: scales and biases of one
type, which is x's or, for bf16 x, f16, and an output of x's type. They
throw on anything else, such as scales and biases of different types.
The ops never pass such a pair, so the check has no test. It guards
primitives built without the ops, such as one that import_function
rebuilds from a function exported before this change.
@rcfa
rcfa force-pushed the fix/cpu-mixed-qmv branch from 59f5ddf to 368e01b Compare October 22, 2026 01:08
@rcfa rcfa changed the title Keep CPU streams off the mixed bf16/f16 quantized matmul path Read f16 scales natively on the CPU; mixed path on CPU and Metal only Oct 22, 2026
rcfa added a commit to cubiculum-ai/mlx that referenced this pull request Nov 15, 2026
…urus-ai#11's f16 scales

# Conflicts:
#	mlx/backend/cpu/quantized.cpp
@jjang-ai
jjang-ai merged commit bae5201 into osaurus-ai:main Nov 22, 2026
rcfa pushed a commit to cubiculum-ai/mlx that referenced this pull request Nov 22, 2026
osaurus-ai#11 adds a ScaleT template parameter to _qmm_dispatch_group; main's affine
1-bit case (osaurus-ai#32) still instantiated it without one, which does not compile.
rcfa added a commit to cubiculum-ai/mlx that referenced this pull request Nov 22, 2026
The CPU's mixed bf16/f16 quantized path (osaurus-ai#11) is in quantized.cpp, which
Highway builds do not compile. Their kernels read the f16 scales as bf16,
so the mixed path gave wrong values. Highway builds now promote on the
CPU as before, until the Highway kernels read f16 scales.
rcfa added a commit to cubiculum-ai/mlx that referenced this pull request Nov 22, 2026
cpu/quantized.cpp conflicted where ml-explore#4803's float32 accumulation meets
osaurus-ai#11's mixed bf16/f16 path, now on main. The resolution is the one tested
in shared since 2026-11-16 (via t332/qmm-f32-with-11): ml-explore#4803's `acc`
argument at osaurus-ai#11's new call sites, and float32 accumulation in osaurus-ai#11's
mixed path.
@rcfa
rcfa deleted the fix/cpu-mixed-qmv branch November 23, 2026 00:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants