Skip to content

fix(mlx): cover NF12 short-row output tails in dot-product dispatch (#4478) - #4523

Merged
Qubitium merged 1 commit into
mainfrom
fix/issue-4478-mlx-nf12-tail
Nov 17, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/issue-4478-mlx-nf12-tail

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4478

Root cause

MlxNF12Linear._rows() launches the decode-plus-dot kernel with

grid=(threads, self.out_features // (16 * blocks), 1)

Each threadgroup owns blocks adjacent 16-column blocks, so the launch covers
exactly out_features // (16 * blocks) * 16 * blocks columns. _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. For
K=64, N=1040, M=1 it selected blocks=2, grid_y=32 (1024 columns, 16
unwritten); for N=2096 it selected blocks=4, grid_y=32 (2048 columns, 48
unwritten).

Fix

_row_blocks() now steps the preferred block count down to the largest divisor
of the 16-column block count (4 -> 2 -> 1). The launch stays exact:
grid_y * 16 * blocks == out_features, so every accepted output column is
written 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:

1 1040 2 32 16
1 2096 4 32 48
13 1040 1 65 0

After:

1 1040 1 65 0
1 2096 1 131 0
13 1040 1 65 0

Regression tests (tests/test_mlx_nf12_row_dispatch_4478.py) cover
rows=(1,4,8,12,13,16) x N=(512,528,1024,1040,2048,2064,2080,2096) and
assert every column has exactly one owner:

  • before fix: 2 failed
  • after fix: 2 passed

Tests run

CUDA venv (Python 3.14t, torch 2.16.0.dev20261102+cu130), thread caps = 4:

PYTHONPATH=$PWD /opt/venvs/cu130t/bin/python -m pytest \
  tests/test_mlx_nf12_row_dispatch_4478.py tests/test_mlx_nf12.py tests/test_nf12_kernel_manifest.py -q
-> 24 passed, 6 skipped

ROCm venv (torch 2.16.0.dev20261025+rocm10.1), HIP_VISIBLE_DEVICES=2, thread caps = 4:

PYTHONPATH=$PWD /opt/venvs/rocm101t/bin/python -m pytest \
  tests/test_mlx_nf12_row_dispatch_4478.py tests/test_mlx_nf12.py tests/test_nf12_kernel_manifest.py -q
-> 24 passed, 6 skipped

Lint:

ruff check gptqmodel docs/eora tests setup.py
-> All checks passed!

Branch merged origin/main (461b42023, already up to date) before the final
test/lint run.

Hardware

CPU-only host-launch verification; AMD Eng Sample 100-000001458-01. No GPU
kernel involved. The 6 skips are the existing tests/test_mlx_nf12.py Metal
tests, which require MLX.

Limitations

  • No Apple Metal/MLX runtime is available on this Linux host (import mlx
    raises ModuleNotFoundError), so the Metal kernel itself is not numerically
    executed 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.py
    remains the qualification for Metal numerics and is skipped on this host.
  • For widths whose 16-column count is not divisible by the preferred block
    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.

@Qubitium
Qubitium merged commit 30ee048 into main Nov 17, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-4478-mlx-nf12-tail branch November 17, 2026 00:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] MLX NF12 short-row dispatch leaves accepted output tail columns uncovered

1 participant