Skip to content

perf(nemotron-super): fused MTP verify window, window submission, NAX prefill kernels on M5 (onto main) - #323

Merged
Gajesh2007 merged 7 commits into
mainfrom
perf/nemotron-super-m5-main
Oct 21, 2026
Merged

Gajesh2007 merged 7 commits into
mainfrom
perf/nemotron-super-m5-main

Conversation

@jonathan308

Copy link
Copy Markdown

Re-opened onto main. #319 was merged into its stacked base fix/nemotron-super-prefill-memory (f219559b) after #318 had been squash-merged, so these commits never reached main. This branch replays #319's seven commits onto main (66f556b7, whose tree equals #318's head). Its tree is byte-identical to #319's final, CI-green head 9cd8fc43 (8e6c619f), so every measurement and test in #319 applies unchanged. The last commit gates the runtime-compiled NAX kernels to macOS 27 (the CI fix).

Summary

Decode and prefill speedups for Nemotron 3.5 Super on M5 (NAX) GPUs. The two decode changes are bit-identical (same tokens, same logits). The prefill change alters summation order and passes the accuracy-vs-truth gate (below).

  • Window submission (NemotronSuperMTPWindowSubmission.everyLayers, default 12; DARKBLOOM_NEMOTRON_SUPER_MTP_WINDOW_SUBMIT=0 restores whole-window evaluation): the row-exact trunk (MTP verify windows, batched decode) is submitted to the GPU every 12 blocks, so the GPU runs while the rest of the window's graph is built. Only the evaluation order changes.

  • Verify-window and draft-head fusion (NemotronSuperMTPFusion.enabled, default on; DARKBLOOM_NEMOTRON_SUPER_MTP_FUSION=0 restores the op chains): the verify window, batched decode and the MTP head use Super's existing fused kernels exactly where the target's one-token decode uses them: residual-add + next-norm boundary, the Mamba gated norm, the one-token SSM step, the MoE combine, squared ReLU fused into the shared-expert and routed fc1 products, and the head's fused expert selection with in-range router indices. Every fused kernel works row by row, behind its supports* guard, so each window row reproduces one-token decode by construction. About 20% fewer kernels per round.

  • Prefill on NAX GPUs (ModelOptNVFP4GroupedGEMM.isEnabled now defaults to off when NAX is available): routed NVFP4 expert products use MLX's sorted NAX gather instead of the simdgroup grouped GEMM tuned on the M3 Ultra. Non-NAX GPUs are unchanged.

Follows #318 (prefill memory fix, merged as 66f556b7). Supersedes the misdirected merge of #319.

Before

flowchart LR
  B[build whole verify window graph] --> S[submit] --> G[GPU runs ~2,600 kernels/round]
  W[window rows] -->|op chains: add, norm, gated norm, combine, relu2| G
Loading

After

flowchart LR
  B1[build blocks 1-12] --> S1[submit] --> B2[build blocks 13-24] --> S2[submit] --> Bn[...]
  S1 --> G[GPU runs while host builds]
  W[window rows] -->|fused row-wise kernels, same as one-token decode| G
Loading

Measured (M5 Ultra, full checkpoint, shipped grouped GEMM and MTP depth)

Same probe binary with the switches flipped; shipped MTP depth; one client.

before after
Realistic decode, median of 14 prompts (code, chat, math, reasoning) 70.5 tok/s 79.4 tok/s (+13%)
Counting benchmark decode (1.5K prompt) 87.7 tok/s 102.0 tok/s (+16%)
Whole 26-prompt A/B set 92.3 s 82.8 s (1.12x)
Greedy outputs, 26 prompts identical 26/26
Window submission alone / fusion alone +10% / +1-2%
Prefill, 12K prompt (NAX expert products) 1,556 tok/s 2,100 tok/s (+35%)

Prefill accuracy gate (not bit-identical). Teacher-forced over 46,070 positions of a mixed corpus, scored against the true next token:

top-1 accuracy perplexity agreement with grouped GEMM
grouped GEMM (before) 78.120% 2.7294 100%
MLX NAX gather (after) 78.157% 2.7238 96.12%
grouped GEMM, TF32 off (control) 78.170% 2.7255 96.16%

The scorer is deterministic (repeat runs 100.000% identical). Any summation-order change, TF32 included, agrees with the other path at ~96% of positions; that is Nemotron's numeric noise floor, not an accuracy loss. Where the two expert paths disagree (1,789 positions), the NAX path is right 388 times and the grouped GEMM 371 times.

Testing

  • Super SDK harness (scripts/nemotron-qualification SDK template, debug): 307 tests in 76 suites pass (--no-parallel; in parallel, the process-memory residency suite can be disturbed by a concurrently allocating suite that is not part of this PR). New: NemotronSuperMTPBatchedWindowTests.fusedTrunkMatchesUnfusedTrunk (logits, hidden and committed state, bit for bit, up to 18 rows), NemotronSuperMTPGraphTests.fusedGraphHeadMatchesUnfusedHead (fp32/bf16), NVFP4/FP8 squared-ReLU row-exactness, the multi-row gated norm on strided gate slices, and head selection.
  • Full-model A/B on the M5 Ultra: 26 prompts (code, chat, math, reasoning, long), greedy, token-identical vs the same binary with both switches off.
  • swift-format format makes no changes.

Refactor pass

A dedicated review found no blocking issues. Applied: window submission is now covered by a test (submission after every layer vs the unfused whole window), the env switches are static let, the zero-index cache is bounded by the row-exact maximum, the squared-ReLU mirror is folded into superRowExactProjection(squaredReLU:), residualParts is shared, the backbone's residual norm uses the same boundary helper, one guarded combine helper, doc fixes and test nits.

Update: NAX expert kernels, fused route, NAX dense projections (770555d1)

Bit-identical to the MLX NAX products they replace: greedy outputs identical on 26 A/B prompts, and the teacher-forced score identical at all 46,070 positions.

M5 Ultra, 12K prompt prefill tok/s
shipped (grouped GEMM) 1,567
MLX NAX expert gather (3798463a) 2,094
+ dedicated NAX NVFP4 expert kernel 2,255
+ fused expert route (no sorted/unsorted copies, fc2 rounding in fc1's epilogue) 2,368
+ NAX dense FP8/NVFP4 projections (no per-call BF16 weight copy) 2,471

Per MoE layer at a 2,048-token chunk: expert block 8.85 → 6.47 ms. Super SDK harness: 315 tests pass at this commit (serial).

Update: chunked NAX Mamba prefill (c6aa5665) — accuracy-gated, not bit-identical

M5 Ultra, 12K prompt prefill tok/s top-1 vs truth perplexity
shipped grouped GEMM (gate baseline) 1,567 78.120% 2.7294
NAX experts + fused route + NAX dense (770555d1, bit-identical to MLX NAX) 2,471 78.157% 2.7238
+ chunked NAX SSD / fused conv 2,881 78.094% 2.7275

Passes the owner's gate (top-1 within 0.05 pts of shipped, perplexity within +0.1% of shipped). Against the bit-identical NAX build it is −0.06 pts / +0.13% perplexity, spread evenly across window positions (no growth with context). One Mamba layer at 2,048 tokens: 11.3 → 8.0 ms (SSD 6.1 → 1.9 ms). NemotronSuperMambaPrefill.enabled = false restores the previous path. Super SDK harness: 318 tests pass.

…it the window in parts

Speculative decode rounds, with tokens and logits unchanged:

- The row-exact trunk (MTP verify windows, batched decode) is submitted to
  the GPU every 12 blocks (NemotronSuperMTPWindowSubmission.everyLayers;
  DARKBLOOM_NEMOTRON_SUPER_MTP_WINDOW_SUBMIT, 0 builds it whole), so the GPU
  runs while the rest of the graph is built. Evaluation order only.
- The trunk and the MTP head use Super's fused kernels exactly where the
  target's one-token decode uses them (residual-add + next-norm boundary,
  Mamba gated norm, one-token SSM step, expert combine, squared ReLU fused
  into the shared-expert products, the head's fused expert selection with
  in-range router indices). Each works row by row behind its supports guard,
  so every row reproduces one-token decode by construction
  (NemotronSuperMTPFusion.enabled; DARKBLOOM_NEMOTRON_SUPER_MTP_FUSION=0
  restores the op chains).

M5 Ultra, full checkpoint, 26 A/B prompts greedy: token-identical; realistic
decode (code, chat, math, reasoning) median 70.5 -> 79.4 tok/s, the counting
benchmark 87.7 -> 102.0 tok/s, the whole set 92.3 -> 82.8 s. Window
submission alone gives +10%, fusion +1-2%.

Tests compare fused and unfused trunks and heads bit for bit (logits, hidden,
committed state), including submission after every layer.
…X GPUs

The simdgroup grouped GEMM was tuned on an M3 Ultra; on NAX GPUs (M5 and
later) MLX's sorted NAX gather product is faster. Nemotron 3.5 Super prefill
on an M5 Ultra: 1,556 -> 2,100 tokens/s on a 12K prompt.

The two paths sum in different orders, so this is not bit-identical. Scored
teacher-forced against the true next token over 46,070 positions, the NAX
path is at least as accurate: top-1 78.16% vs 78.12%, perplexity 2.7238 vs
2.7294. Any change in summation order (TF32 on or off as well) agrees with
the other path on ~96% of positions; at the 1,789 positions where these two
disagree, the NAX path is right 388 times and the grouped GEMM 371.
Non-NAX GPUs keep the grouped GEMM.
…dense projections

Prefill on NAX GPUs (M5 and later), bit-identical to the MLX products they
replace:

- ModelOptNVFP4NAXExperts: a dedicated NVFP4 expert GEMM adapted from the
  GLM prefill kernel (double-buffered weight tiles decoded eight codes per
  word, one tile-scan launch, empty fragments skipped, expert scale and relu2
  in the epilogue), with the fastest measured plan per projection pinned.
  It issues MLX's sorted NAX gather's tensor ops in the same order.
- fusedRoute: one route launch replaces the argsorts, gathers and scans; fc1
  reads the latent rows through the sort order; fc1's epilogue does fc2's
  NVFP4 activation rounding; the combine reads through the inverse order.
- ModelOptNAXDenseLinear: FP8 and NVFP4 dense prefill projections decode
  their weights inside the NAX GEMM instead of writing a BF16 weight copy
  per call (FP8) or float32 sums and a separate scale pass (NVFP4); taken
  only where MLX runs its regular NAX kernel, never for decode rows.

M5 Ultra, full checkpoint: 12K-token prefill 2,094 -> 2,255 tokens/s with
the expert kernel, 2,471 with the fused route and dense projections; greedy
outputs identical on 26 A/B prompts and the teacher-forced score identical
at all 46,070 positions. Per MoE layer at a 2,048-token chunk the expert
block takes 6.47 instead of 8.85 ms.
…l on M5

Mamba-2 prefill chunks on NAX GPUs run a chunked SSD kernel: one
threadgroup per head walks the prompt in 32-token chunks with the head's
float32 state resident in registers (as the GLM 5.3 recurrent prefill
kernels keep each core's state), and the intra-chunk C.B^T with the decay
mask, the intra-chunk and carried-state outputs and the state update run as
NAX tensor ops with float32 operands and accumulation. One kernel does the
conv-state concatenation, depthwise conv, bias, silu and retained state
(bit-identical to the op chain); dt is computed inside the SSD.
NemotronSuperMambaPrefill.enabled (on by default on NAX GPUs) restores the
previous path when cleared.

Not bit-identical. M5 Ultra, full checkpoint: one layer at 2,048 tokens
11.3 -> 8.0 ms (SSD 6.1 -> 1.9 ms); 12K-token prefill 2,471 -> 2,881
tokens/s. Teacher-forced over 46,070 positions: top-1 78.09%, perplexity
2.7275, against 78.12% / 2.7294 for the grouped GEMM build (the accuracy
gate's baseline) and 78.16% / 2.7238 for the bit-identical NAX expert
build; the perplexity difference is even across positions.
… only

The NAX expert kernels and fused route, the NAX dense projections and the
chunked SSD compile at first use with the OS's Metal compiler. macOS 26's
compiler rejects some of these tensor-op kernels ("unsupported
deferred-static-alloca-size" in the chunked SSD on the CI runner), which MLX
reports as a fatal error. They were validated on macOS 27 (M5 Ultra 27.0.1,
M5 Max 27.0, M3 Ultra 27.2): ModelOptNVFP4NAXExperts.runtimeCompilerSupported
gates their defaults and tests, so earlier systems keep MLX's prebuilt NAX
kernels and every decode change.
jonathan308 added a commit to Layr-Labs/d-inference that referenced this pull request Oct 21, 2026
Pins mlx-swift-lm 8cf6698b (Layr-Labs/mlx-swift-lm#323: #319's commits replayed onto main; same tree as 9cd8fc43).
@Gajesh2007
Gajesh2007 merged commit 0351444 into main Oct 21, 2026
6 checks passed
jonathan308 added a commit to Layr-Labs/d-inference that referenced this pull request Oct 21, 2026
Pins mlx-swift-lm main 0351444e (Layr-Labs/mlx-swift-lm#323 squash-merged; contains the parent's 19634dd8 and this branch's speedups) with master's mlx-swift 67dc7528 and mlx ad4745a2.
jonathan308 added a commit that referenced this pull request Oct 22, 2026
Bring in main through #323, including the mlx-swift main pin (67dc7528),
GLM-5.3-Flash native support and Nemotron Super kernels. Resolve the
tool-call format conflicts by keeping both the Flint and GLM5 parsers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
javiech1 pushed a commit to javiech1/mlx-swift-lm that referenced this pull request Oct 24, 2026
Store GatedDelta convolution state contiguously and advance array-cache metadata after each recurrent step, matching upstream mlx-lm behavior for Qwen3.5/Qwen3-Next style models.

Keep left-padding masks active after recurrent cache state initialization, add coverage for ArraysCache metadata advancement, and align Qwen3 RoPE setup with the shared rope initializer.
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.

2 participants