Repository navigation
Read f16 scales natively on the CPU; mixed path on CPU and Metal only - #11
Merged
Merged
Conversation
rcfa
force-pushed
the
fix/cpu-mixed-qmv
branch
from
October 13, 2026 15:40
f4f37e0 to
59f5ddf
Compare
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
force-pushed
the
fix/cpu-mixed-qmv
branch
from
October 22, 2026 01:08
59f5ddf to
368e01b
Compare
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
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.
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.
What failed
For bf16 activations with f16 scales and biases (affine, group size 64, 4 or 8 bits, one row of x),
quantized_matmultakes 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_qmmavoided 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_twidens 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.The sorted-matrix
gather_qmmis 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_matmulthat is a change in behaviour: CUDA returned bf16 before, probably wrong, and returns float32 now. The reason is thatcuda/quantized/qmm/qmv.cureads 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 toqmm_sm90instead (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
_qmm_tand 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).mainit gave wrong results inquantized_matmulon the CPU and Metal, and ingather_qmmon 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:
Tips (Clang; 16 CPU-JIT cases skipped on Linux):
macOS at commit 3, with Metal: 311 cases passed with
DEVICE=cpu(5064 assertions) and on Metal (5043). One Metal run failedtest concurrent allocator statistics(tests/allocator_tests.cpp:190), a test this branch does not touch. It is intermittent and also fails atmain: interleaved with this branch, it failed 9 of 20 runs on each binary.Mutations, each made alone, each failing only its own checks:
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