Repository navigation
[CPU] Fix accumulation precision in affine quantized matmul - #4803
Open
erwinzhang7 wants to merge 2 commits into
Open
erwinzhang7 wants to merge 2 commits into
erwinzhang7 wants to merge 2 commits into
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
This branch has not been deployed
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.