Fix int16 overflow in NAX qmm edge-tile bounds#3631
Open
scyyh11 wants to merge 1 commit into
Open
Conversation
The per-simdgroup edge sizes in qmm_t_nax_tgp_impl and the fp_ variants were computed as min(SN, short(N - (y_col + tn))). The short() cast wraps for distances over 32767, so on M5 a transposed quantized matmul with unaligned N > 2^15 left the output columns in [N - 2^16, N - 2^15) unwritten whenever the M-tile was partial (M % 32 != 0), and M > 2^15 similarly corrupted leading rows. Real-world trigger: MiniCPM3-4B's vocab of 73448 (% 64 = 40) produced wrong logits for prompt batches of 10-31 tokens. Compute the min in int like the dense NAX GEMM kernels (steel_gemm_fused_nax.h) and the adjacent tgp_bn line already do. Signed-off-by: Bvicii <yizhanhuang2002@gmail.com>
Author
zcbenz
approved these changes
Jun 9, 2026
zcbenz
left a comment
Collaborator
There was a problem hiding this comment.
I verified that the the bug exists on M5 and your fix works, thanks!
Collaborator
Author
Author
|
The failed test on macOS (14.0) is |
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.
Proposed changes
qmm_t_nax_tgp_impland thefp_variants compute per-simdgroup edge sizes with:The short() cast wraps for distances greater than 32767. On M-series devices using the NAX path, this can leave output regions unwritten or corrupted for large unaligned dimensions. For example, transposed quantized matmul with unaligned N > 2^15 leaves a column band unwritten when the M-tile is partial (M % 32 != 0). Symmetrically, large M can corrupt leading rows even when N is aligned.
Fix: compute the min in int, matching the dense NAX GEMM kernels in steel_gemm_fused_nax.h and the adjacent tgp_bn calculation.
Real-world impact
MiniCPM3-4B has vocab size 73448 (% 64 = 40). On an M5, 10-31 token prompt processing hits the affected qmm path for lm_head, so token ids in the corrupted output band can receive wrong logits and lead to wrong generations. This was hit in vllm-project/vllm-metal; standalone mlx_lm prompt processing is equally affected. Many models avoid this because common vocab sizes are multiples of 64.
Repro on main, Apple M5 Max:
The corrupted column band matches the int16 wrap points: first bad column 7912 = N - 65536, and the bad range ends before the first tile where N - base <= 32767. Rows <= 9 use qmv, and M multiples of 32 use a different store path, matching the observed unaffected cases.
Tests
Added test_qmm_large_dims covering:
All three fail on main on an M5 and pass with this fix. The rest of test_quantized.py has no new pass/fail changes on my machine.
Checklist
pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes