Repository navigation
fix(mlx): cover NF12 short-row output tails in dot-product dispatch (#4478) - #4523
Merged
Merged
Conversation
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.
Fixes #4478
Root cause
MlxNF12Linear._rows()launches the decode-plus-dot kernel withEach threadgroup owns
blocksadjacent 16-column blocks, so the launch coversexactly
out_features // (16 * blocks) * 16 * blockscolumns._row_blocks()could return a block count that does not divide the 16-column block count
(
out_features // 16), and the floor in the grid then dropped the tail. ForK=64, N=1040, M=1it selectedblocks=2, grid_y=32(1024 columns, 16unwritten); for
N=2096it selectedblocks=4, grid_y=32(2048 columns, 48unwritten).
Fix
_row_blocks()now steps the preferred block count down to the largest divisorof the 16-column block count (
4 -> 2 -> 1). The launch stays exact:grid_y * 16 * blocks == out_features, so every accepted output column iswritten exactly once. Divisible widths keep their previous fast choices; only
non-divisor widths step down. No kernel arithmetic changed.
Before / after
Issue repro (shipped host dispatch with stand-in MLX primitives and a launch
recorder), format
rows out_features blocks grid_y missing_columns:Before:
After:
Regression tests (
tests/test_mlx_nf12_row_dispatch_4478.py) coverrows=(1,4,8,12,13,16)xN=(512,528,1024,1040,2048,2064,2080,2096)andassert every column has exactly one owner:
2 failed2 passedTests run
CUDA venv (Python 3.14t, torch 2.16.0.dev20261102+cu130), thread caps = 4:
ROCm venv (torch 2.16.0.dev20261025+rocm10.1),
HIP_VISIBLE_DEVICES=2, thread caps = 4:Lint:
Branch merged
origin/main(461b42023, already up to date) before the finaltest/lint run.
Hardware
CPU-only host-launch verification; AMD Eng Sample
100-000001458-01. No GPUkernel involved. The 6 skips are the existing
tests/test_mlx_nf12.pyMetaltests, which require MLX.
Limitations
import mlxraises
ModuleNotFoundError), so the Metal kernel itself is not numericallyexecuted here. The regression pins the shipped host launch/coverage contract,
which is the defect class; kernel arithmetic is unchanged and no numerical
claim is made. The independent fp64 oracle path in
tests/test_mlx_nf12.pyremains the qualification for Metal numerics and is skipped on this host.
count (e.g. 1040 and 2096), the row kernel now uses one block per threadgroup
instead of two/four. That is a correctness-first choice; no performance
measurement was made for those widths.