Repository navigation
[CPU] Fix accumulation precision in affine quantized matmul - #4803
Merged
Merged
Conversation
zcbenz
requested changes
Nov 1, 2026
| constexpr int bytes_per_pack = get_bytes_per_pack(bits); | ||
| constexpr int packs_in_group = group_size / pack_factor; | ||
|
|
||
| std::vector<float> acc(N); |
Member
There was a problem hiding this comment.
Instead of allocating a temporary memory with std::vector directly, can you allocate memory with MLX's allocator and register it with encoder's add_temporary and set_input_array like what GPU backends would do with intermediate buffers? It will make use of our memory cache, and will be required when we parallelize CPU scheduler.
zcbenz
force-pushed
the
fix-cpu-qmm-bf16-accum
branch
from
November 3, 2026 03:11
0823c38 to
b434bdb
Compare
zcbenz
approved these changes
Nov 3, 2026
kadyrbekovhamit-cyber
added a commit
to kadyrbekovhamit-cyber/mlx
that referenced
this pull request
Nov 12, 2026
…r affine; add test
rcfa
added a commit
to cubiculum-ai/mlx
that referenced
this pull request
Nov 15, 2026
The C++ form of ml-explore#4803's test, for the CPU path.
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
added a commit
to cubiculum-ai/mlx
that referenced
this pull request
Nov 15, 2026
The scalar paths (_qmm, _qmm_t_cols and _qmm_t's sequential loop) summed in the input type, as quantized.cpp did before ml-explore#4803. The SIMD and BLAS paths already sum in float32.
zcbenz
pushed a commit
to kadyrbekovhamit-cyber/mlx
that referenced
this pull request
Nov 22, 2026
…r affine; add test
rcfa
added a commit
to cubiculum-ai/mlx
that referenced
this pull request
Nov 22, 2026
The scalar paths (_qmm, _qmm_t_cols and _qmm_t's sequential loop) summed in the input type, as quantized.cpp did before ml-explore#4803. The SIMD and BLAS paths already sum in float32.
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
pushed a commit
to cubiculum-ai/mlx
that referenced
this pull request
Nov 26, 2026
…re#4803) Ported: the fork's _qmm_t takes scales and biases of a second type for the mixed bf16/f16 metadata path, with AccT = T when the types are the same. AccT is now float for all types, in place of Apple's float sum, scale and bias. The fork's extra dispatch calls pass acc too. (cherry picked from commit a159fb8)
rcfa
pushed a commit
to cubiculum-ai/mlx
that referenced
this pull request
Nov 26, 2026
) Ported onto the fork's nvfp4 global scales (osaurus-ai/mlx PR 31): scale_dec stays the last argument and acc follows transposed_w. The row accumulator acc is used for GatherQMM and QQMatmul too. Of the test changes, only test_fp_qmm_accumulation_precision is taken; its context, test_qmv_affine_bias_sum_precision, is not in the fork. The fork's Highway backend (quantized_highway_backend.cpp, used when MLX_USE_HIGHWAY_KERNELS is set) has its own non-transposed fp_qmm. It now also accumulates each row in float32, in a local buffer, as its affine _qmm does since ml-explore#4803. Before, the Linux build failed test_fp_qmm_accumulation_precision with transpose=False. (cherry picked from commit 07fff01)
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.
Fix precision loss in CPU quantized matmul by accumulating in
float32instead ofbfloat16orfloat16.The scalar
_qmm_tpath accumulates inT sum, while_qmmaccumulates directly into the low-precision output buffer. This repeatedly rounds intermediate sums, causing substantial errors for large reduction dimensions.The fix:
_qmm_t._qmm, converting to the output dtype once.Dequantization arithmetic and existing SIMD paths are unchanged.
Validation
Tested on M5 Max (18-core), macOS 27.0.1, CPU-only MLX builds.
For a 4096×4096 quantized matmul (4-bit, bf16):
The fix reduces maximum error by approximately 29× and improves transposed bf16 performance by 2.7×.
Remaining bf16 discrepancies come from low-precision dequantization and multiplication, which are intentionally outside this change.