Skip to content

Padded 8-bit verify matmuls onto MLX's NAX tile (MTPLX_Q8_NAX_PAD, off by default) - #641

Closed
jvmenen wants to merge 1 commit into
youssofal:mainfrom
jvmenen:perf/q8-verify-nax-padding
Closed

jvmenen wants to merge 1 commit into
youssofal:mainfrom
jvmenen:perf/q8-verify-nax-padding

Conversation

@jvmenen

@jvmenen jvmenen commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

Why

Mixed-precision checkpoints keep some layers at 8 bits. On Qwen3.8-27B Optimized-Speed these are the 48 GDN out-projections, the MLP of the last 8 layers and lm_head. When a verify window is wide (a context-copy block: 13, 17 or 25 rows), stock MLX runs those 8-bit matmuls on kernels that do not use the M5's tensor units:

  • 7 to 12 rows go to qmv_wide, which reads the weights again for every few rows (lm_head at 9 rows: 11.3 ms against 7.1 ms at 13 rows);
  • from 13 rows, qmm_splitk splits K whenever the 32x32 tile grid has 256 or fewer threadgroups. For outputs of 5120 (the GDN out-projection and the MLP down-projection) that is every window up to 32 rows, so they run on qmm_t_splitk at 2.6 to 2.7 times their weight-read time (GDN out-projection 0.31 ms against a 0.12 ms floor).

The 4-bit layers already have NAX lanes for these windows; the 8-bit layers are what is left.

How it works, in plain terms

The weights are read once per matmul whatever the number of rows, so extra input rows cost almost nothing. The lane appends zero rows to the input until MLX's own dispatch picks its NAX tile (qmm_t_nax), runs the stock mx.quantized_matmul, and keeps only the real rows. Wide outputs need 13 rows (the qmv limit; one 32-row tile), narrow outputs 33 (two tiles, so split-K is no longer chosen). A small function mirrors MLX's dispatch rule (mlx_qmm_takes_nax, MLX 0.32.2 to 0.32.4, generation-17 GPUs) to decide the row count; where stock already reaches the NAX tile (the wide shapes from 13 rows) nothing changes. No new kernel: the numbers come from MLX's own NAX matmul, which differs from qmv_wide and split-K only in accumulation order.

What changes

  • mtplx/nax_verify.py: q8_nax_pad_enabled, mlx_qmm_takes_nax, q8_nax_pad_rows, nax_qmm_q8_padded, and a branch in the 8-bit path of the QuantizedLinear patch for 12 to 32 verify rows (never in prefill, bf16/fp16 affine only, NAX hardware only, honours the self-check kill switch).
  • MTPLX_Q8_NAX_PAD=1 turns it on (default off). MTPLX_Q8_NAX_PAD_MIN_ROWS moves the floor (default 12; below 9 rows a narrow shape is never padded, since two tiles cost more than qmv_wide there).
  • mtplx/kernel_selfcheck.py: lane qmm_q8_nax_pad, probed whenever the switch is on, whatever the model's main bit width (mixed models report 4 bits); a mismatch turns the lane off for the process.
  • Tests in tests/test_nax_verify.py (dispatch mirror on the Qwen3.8 shapes, off by default bit-identical to stock, routing only for wide verify rows, not in prefill, the row floor) and tests/test_kernel_selfcheck.py (probe present only with the switch, corruption falls back).
  • CHANGELOG line.

Results

M5 Pro (20-core GPU), Qwen3.8-27B Optimized-Speed, MLX 0.32.4, production verify core (linear-gdn-from-conv-tape).

Per matmul (ms, stock / padded):

Shape 9 rows 13 rows 25 rows
GDN out q8 6144->5120 (x48) 0.220 / 0.201 0.315 / 0.202 0.318 / 0.201
MLP down q8 17408->5120 (x8) 0.592 / 0.604 0.854 / 0.606 0.880 / 0.608
MLP gate/up q8 5120->17408 (x16) 0.729 / 0.511 stock already NAX stock already NAX
lm_head q8 5120->248320 11.34 / 7.06 stock already NAX stock already NAX

Whole verify forward in the model (median of 12 alternating runs, real prompt):

Rows 32K context 100K context
4, 7, 8, 9, 10 untouched (floor 12) untouched
12 131.9 -> 125.5 ms (-4.9%) 164.6 -> 158.4 ms (-3.8%)
13 133.9 -> 126.7 ms (-5.4%) 168.2 -> 162.0 ms (-3.7%)
17 153.6 -> 146.3 ms (-4.8%) 215.4 -> 210.3 ms (-2.3%)
25 164.6 -> 157.1 ms (-4.6%) 237.8 -> 230.7 ms (-3.0%)

The 9 and 10-row windows were padded in a first version: the matmuls alone are faster there, but the whole forward was 0.4 to 2.9% slower, so the default floor is 12.

Parity:

  • Per layer, on the inputs of a real verify window: the padded result differs from stock by at most 7e-3 relative (accumulation order); padding to 33 or 64 rows gives bit-identical real rows, and padding a shape that already reaches the NAX tile is bit-identical to stock.
  • 26 context-copy windows recorded from a server replay of real agent turns (26K and 62K context, 9 to 25 rows), each verified on the same cache with the current route, the lane and stock MLX: argmax equal on 358 of 362 rows (current route against stock: 359 of 362). Every differing row is a near tie: the two tokens are 0.25 to 0.63 logits apart. Expected accepted tokens per window under the default sampler (temperature 1.0, top-k 20, top-p 0.95): 4.8253 current, 4.8251 lane, 4.8180 stock.
  • On synthetic windows (blocks copied from random earlier positions), a few rows react strongly to any change of accumulation order: the current route against stock shows the same flips and logit distances there as the lane against the current route.

Tests

  • tests/test_nax_verify.py and tests/test_kernel_selfcheck.py: pass (switch off and on).
  • Full suite on the production branch with this commit: no failures.

@jvmenen
jvmenen requested a review from youssofal as a code owner October 19, 2026 08:33
@jvmenen jvmenen closed this Oct 19, 2026
@jvmenen jvmenen reopened this Oct 19, 2026
jvmenen pushed a commit to jvmenen/MTPLX that referenced this pull request Oct 19, 2026
… 8-bit verify results, attention research
jvmenen pushed a commit to jvmenen/MTPLX that referenced this pull request Oct 21, 2026
@jvmenen

jvmenen commented Oct 21, 2026

Copy link
Copy Markdown
Contributor Author

Closing this. MLX main now has ml-explore/mlx#4682 (bff10220c, after v0.32.4): from 8 rows an affine NAX qmm with split-K takes over from qmv_wide and qmm_t_splitk when the tile grid is small. On the Qwen3.8-27B Optimized-Speed shapes (M5 Pro, per call) the unpadded 8-bit GDN out-projection runs at 0.16 ms and the MLP down-projection at 0.48 ms for 13 to 25 rows, against 0.24 ms and 0.68 ms with this padding on 0.32.4, and the new route also covers 9 to 11 rows. With MLX newer than 0.32.4 the padding is no longer needed; the only shape it still reaches there is lm_head at 9 to 12 rows, a few milliseconds per such round.

@jvmenen jvmenen closed this Oct 21, 2026
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.

1 participant