Skip to content

Nemotron: two members a pass at 8 rows a simdgroup for wide M1 windows - #1466

Merged
ashhart merged 5 commits into
ashhart:mainfrom
mrv777:perf/nemotron-m1-wide-expert-rows
Oct 30, 2026
Merged

ashhart merged 5 commits into
ashhart:mainfrom
mrv777:perf/nemotron-m1-wide-expert-rows

Conversation

@mrv777

@mrv777 mrv777 commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

On M1-family GPUs (applegpu_g13), Nemotron's windowed forward runs routed experts differently for windows of 16 or more rows. They now take two members a pass at 8 rows a simdgroup, through two new instantiations of the existing tf_experts_rows template: tf_xup_rows2r8 and tf_xdown_rows2r8 (RPS 8, SG 2, MB 2). Until now they took tf_experts_w's three members a pass. No kernel body changes.

The following keep their kernels: every other GPU generation, one-row decode, the MTP head (members 0) and windows under 16 rows. tf-nemotron-experts' rows2r8 variant now checks the shipped instantiation instead of compiling its own copy.

The outputs cannot change. tf_rowdot_rows keeps tf_rowdot's per-lane chunk order: the tf_load16 sums, the 4×4 accum, scale * accum + sum * bias, then simd_sum. RPS and SG only choose which outputs a simdgroup owns. An odd member left over takes the same single-member tf_rowdot tail as w3. Both grids divide exactly (1856 / 16 and 2688 / 16).

The windowed forward meets 16 or more rows in three places:

  • shared rounds of concurrent streams;
  • a 16-row prompt chunk (longer chunks take the prefill kernels);
  • a single greedy stream's GPU-round copy windows (copy lanes are on by default, copy_max 30).

GPU rounds choose by verify_rows, the most rows the round could take. A round that priced a copy but verified fewer rows can therefore take rows2r8 at a narrow width, where it is 2–8% slower at 2–5 rows and level at 8.

This is not the M5 Ultra result in "Don't bother" ("Six or eight members a pass, or four at two simdgroups, lose 5-40%"). Here it is still two members a pass, with 8 output rows a simdgroup. On this M1 Max, w4r8 (four members) loses too. The gate is M1-only, so the M5 Ultra path is untouched.

On M1 Max I measured the engine's own loaded pipelines on synthetic 4-bit weights: 4 layers × 128 experts, each layer's up then down as the engine encodes them. Each round ran w3, rows2r8 and w3 again in rotated order, for 30–40 rounds of about 4 ms samples. Routing was three Zipf routings a width. I report the median of paired per-round ratios.

Rows rows2r8 / w3, Zipf s = 1 s = 2 s = 3 w3 / w3 control, s = 1
16 0.953–0.959 (3 runs) 0.884 0.848 1.000–1.001
24 0.928–0.937 (3 runs) 0.839 0.799 0.995–1.002
32 0.898–0.913 (2 runs) 0.823 0.787 1.000–1.001
64 0.855–0.856 (2 runs) 0.777 0.749 0.999–1.004
128 0.795–0.804 (2 runs) 0.746 0.720 0.998–1.004
  • At exactly 16 rows on Zipf s = 1, the median gain (4–5%) sits inside the per-round spread. It is clear from 24 rows, and at every width under the more concentrated routings.
  • Below the gate, rows2r8 is slower at 2–4 rows (1.03–1.08) and level at 8–12. Concentrated routing favours it even at 8 rows (0.81–0.94). I kept 16 as the conservative floor.
  • tf-nemotron-experts, run alone, shows the same trend: at 32 rows 3,271.8 against w3's 3,508.2 µs a layer, and at 128 rows 13,103.2 against 16,392.7.
  • Runs were at load 3–5, about 70% memory free, with no thermal or performance warnings.

These are single-kernel GPU timings. Model throughput was not measured because no checkpoint was available on this machine. The shared expert's down projection runs alongside the routed up in the real encoder, and these timings don't include that overlap.

Validated on M1 Max, macOS 26.7, Zig 0.17.0, -Dcpu=apple_m1, against b30d5b9d:

  • Bits: rows2r8 equals the engine's w3 for every output, up and down. That covers three routings a width, widths 2–128 including 3, 5, 7, 9, 15, 17 and 31 (odd member tails), and Zipf s = 1, 2 and 3. About 121 million values were compared, all equal.
  • tf-nemotron-experts: every kernel: bits == the engine's, rows exact.
  • zig build test test-golden: both trees report 7215/7261 passed, 45 skipped, 1 failed. The failure is the same on both: families.glm.eh_check "eh_proj on the tensor units: fp64 error no worse than the row kernel, decode and prompt apart". zig build native succeeds on both.
  • Formatting and lean: zig fmt --check passes. The lean checker reports 428 problems on both trees, with identical output (zero added).
  • Kernel contract: the registry has no Nemotron routed-expert entries, so tf-kernel-contract does not cover these launches. The new launches use 64 threads a group, against w3's 128.

Untested: M1, M1 Pro and M1 Ultra (the same generation, assumed alike); M2–M5 (unchanged by design); a real checkpoint, decode and serving throughput; CUDA (not affected).

@mrv777
mrv777 marked this pull request as ready for review October 27, 2026 14:12
@ashhart

ashhart commented Oct 27, 2026

Copy link
Copy Markdown
Owner

Thanks for this, and for the bit checks on your M1 Max. We read the arithmetic as bit-exact too: tf_rowdot_rows keeps tf_rowdot's per-lane chunk order, RPS and SG only choose which outputs a simdgroup owns, and every other chip dispatches exactly as before.

Two things before it can land:

  1. A served receipt on M1. It's the only chip this changes, and we no longer have an M1-family Mac to run it on. On a real checkpoint (for example TensorFold/NVIDIA-Nemotron-3.5-Lightning-30B-A3B-MLX-4bit), with main and this branch alternated: bench_openai.py --tokens 256 --reps 3 --temperatures 0 on a single stream with the default copy lanes, bench_concurrent.py --levels 1,4 --alone --serial, and drafted == "draft": false token hashes. You mention that GPU rounds pick the kernel by verify_rows, so a round that priced a copy but verifies 2-5 rows can take rows2r8 where it is 2-8% slower. That is M1's default single-stream path, so the single-stream receipt matters most. If it is slower there, choosing by the rows the round actually verifies would avoid it.
  2. Kernel contract. tf_xup_rows2r8 and tf_xdown_rows2r8 are new pipelines and need registering. tf-kernel-contract --offline can't measure them as they stand: on M1 and M2 its search probes 512 and then 256, and these builds differ from the plain build at both, so it records 0. The simplest route is [[max_total_threads_per_threadgroup(64)]] on the two instantiations with .reserved = 64 in their records, as glm_rows_records.zig does. The attribute changes the compiled code, so the bit check needs to run again afterwards.

On our side I'll run tf-nemotron-experts and a Nemotron token-hash A/B on an M5 Ultra to confirm the other chips are unchanged. I also have two small doc-comment fixes (the rows_names note for r8 and the tool's builtIn list) that I'll fold in when it lands.

@mrv777

mrv777 commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Item 2 is in 4921cb9.

Metal rejects [[max_total_threads_per_threadgroup(64)]] on an explicit instantiation ("not allowed on explicit instantiation declarations"), and a kernel can't call another kernel. So the row body moves to an always-inline tf_rows_pass. tf_experts_rows and a new tf_experts_rows64 (which carries the attribute) both call it. tf_xup_rows2r8 and tf_xdown_rows2r8 now instantiate tf_experts_rows64. Their records are in nemotron_rows_records.zig with .reserved = 64, as glm_rows_records.zig does. metal_limits.zig maps nemotron experts … records to nemotron_experts.metal.

  • IR: I compared each kernel's IR at -O2 against 35a207c, with metadata ids normalised. tf_xup/xdown_rows2, rows4, w3 and both rows2r8 bodies are identical; only the r8 kernels' thread-limit metadata changes. The existing pipelines on every chip compile as before.
  • Bits, rerun after the change (M1 Max): rows2r8 equals the engine's w3 for every output, up and down. That covers widths 2–128 (including 3, 5, 7, 9, 15, 17 and 31) and Zipf s = 1, 2 and 3, about 92.5 million values. tf-nemotron-experts reports every kernel: bits == the engine's, rows exact, with timings unchanged.
  • Contract: tf-kernel-contract --only "nemotron experts" --offline DIR gives PASS limits here for both pipelines (64 threads launched, the pipeline takes 64). Offline they show N/A (reserved), the same as the glm rows entries. registry.checkMetal reports 0 problems across all 278 launches.
  • Checks: zig fmt --check passes, and the lean checker is unchanged at 428 (zero added).
  • Tests: zig build test test-golden fails the same two tests on this branch and on a clean b30d5b9 checkout run at the same time. One is the known glm.eh_check; the other is core.paced_read "aligned spans", which also fails on the clean checkout on this machine right now.

Item 1, the served receipt, is next. I'll post the single-stream and concurrent numbers and the token hashes from the Nemotron checkpoint when I have them. If single-stream regresses, I'll switch to picking the kernel by the rows the round actually verifies.

@ashhart

ashhart commented Oct 27, 2026

Copy link
Copy Markdown
Owner

A follow-up on the kernel contract point: the trouble registering tf_xup_rows2r8 and tf_xdown_rows2r8 is partly our tool's fault. --offline's limit search assumes builds match plain at every thread count up to the limit, and for these pipelines on M1 and M2 they match at 224 and 320-448 but not at 256 or 288. We're fixing the search in #1493. Once that lands, the offline run should record their real limit and you can register them without the attribute workaround. The served M1 receipt is still the main thing we need from you.

@ashhart

ashhart commented Oct 27, 2026

Copy link
Copy Markdown
Owner

Thanks @mrv777. The offline limit search fix (#1493) and Nemotron's routed-expert pipelines in the contract (#1494) have both landed, so this branch now needs a rebase. After it, the offline run should record the real limits of tf_xup_rows2r8 and tf_xdown_rows2r8, so the 64-thread attribute workaround can go. The served M1 receipt against main is still the main thing we need.

@mrv777
mrv777 force-pushed the perf/nemotron-m1-wide-expert-rows branch from 4921cb9 to 947cef7 Compare October 28, 2026 01:20
@ashhart

ashhart commented Oct 28, 2026

Copy link
Copy Markdown
Owner

Thanks @mrv777 for bringing this up to date with main, and for going back to plain instantiations now that #1493 is in: the records carry the offline limits (448 on M1 and M2), and the 64-thread workaround is gone.

CI's zig job fails in test-kernel-contract, in zig/nemotron_experts_contract_host.zig. The census still expects 20 entries and 18 plain pipelines (:23, :29), and the fp32-sum test reads cases.entries[19] (:126), which is now a pipeline rather than that control. With the two r8 pipelines it's 22 and 20, and the control sits at index 21. Finding the control by its fault would stop the index moving again, and the test's name still says eighteen. A rebase onto main, rather than a merge commit, will also make it simpler to land.

The served receipt on M1 is still the main thing we need: bench_openai.py --tokens 256 --reps 3 --temperatures 0 on one stream with the default copy lanes, bench_concurrent.py --levels 1,4 --alone --serial, and drafted equal to "draft": false, with main and this branch alternated.

@ashhart

ashhart commented Oct 28, 2026

Copy link
Copy Markdown
Owner

Our M5 Ultra A/B is done, with Nemotron 3.5 Lightning, main against this branch, alternating main, branch, branch, main:

  • Tokens: they match main in all 20 prompt rows, and every stream in the sweep matches its run alone.
  • Speed: prefill and decode after prompts from 2k to 64k are within 0.1% of main, and the 1 to 8 stream sweep is within 0.1%.

So nothing changes on an M5, as expected for a change aimed at M1's wide windows. What's left is on your side, from our last comment: the CI fix in nemotron_experts_contract_host.zig, and the served M1 receipt alternated with main. Thanks, @mrv777.

@mrv777

mrv777 commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

Item 1, the served receipt from an M1 Max (32 GB). The branch is rebased, and rows2r8 is registered in nemotron_experts_records.zig and the contract cases at 448/448, the offline-measured limits.

Setup: checkpoint TensorFold/NVIDIA-Nemotron-3.5-Lightning-30B-A3B-MLX-4bit at d9d758fb. Each tree is built with zig build native -Dcpu=apple_m1. "Main" is this PR's merge parent, 15a0b2a, so the PR is the only difference. One server ran at a time on default flags, alternating main, branch, branch, main. Swap-outs stayed flat for the whole run.

bench_openai --tokens 256 --reps 3 --temperatures 0 (decode tok/s, median)

prompt main branch branch main
fibonacci-raw 10.34 11.04 10.98 10.70
gpu-chat-no-think 10.34 11.17 11.01 10.77

Token hashes are identical in every session.

bench_concurrent --levels 1,4 --alone --serial: no failures. Every cell's alone and serial equal/unequal counts are the same on both trees. End-to-end rates for the branch fall within main's own spread between the first and last sessions.

Thinking-on greedy chat, 512 tokens. I added this because it's the path that drafts. On prompts 0 and 1, drafted hashes equal "draft": false in every session. The branch ran at 65.5 and 75.4 tok/s; main ran at 61.6–63.7 and 70.2–71.1.

The branch's small lead also shows on "draft": false runs, which this PR doesn't touch, so I read it as drift between sessions, not the kernel. These workloads also rarely reach the 16-row windows where rows2r8 takes over on M1: drafting peaks at about 2.2 drafts per round. So the receipt shows no regression, but it isn't evidence of a served gain.

Two things showed up that are the same on main as on this branch, so they're separate from this PR:

  1. Greedy rounds that propose no draft are slow. With drafts on, they take about 95 ms. The same request with "draft": false decodes at 76–84 tok/s, about 12 ms a token. Both bench_openai prompts draft nothing (accepted=0/0), which is why they sit at about 10.5 tok/s. Sampled rounds on the same raw prompt take about 33 ms.
  2. Greedy drafted output doesn't always match serial.
    • In the bench_concurrent greedy chat cell (thinking off), serial is 0/1 equal.
    • At 4 streams, 9 of 12 replies differ from the same request alone.
    • On the thinking prompt "Solve for x: 3x + 7 = 31…", drafted runs disagree with each other (f36f62ef… and 9493605a…, on both trees) and with serial (faee5c55…).
    • Every sampled cell is equal.

I can open an issue for these two, or dig into either if that helps.

@ashhart

ashhart commented Oct 28, 2026

Copy link
Copy Markdown
Owner

Thanks @mrv777, that's a thorough receipt. Our side, alternating main and this branch with Nemotron 3.5 Lightning 4-bit:

  • M3 Ultra: the same tokens as main in all 20 prompt rows, and every sweep stream equals its run alone and its serial run. Prefill and decode after 2k to 64k prompts are within 0.6%, and one stream in the sweep is 1.3% faster. So no regression on an M3 either.
  • M5 Ultra: unchanged, as before.

One thing left before it lands: CI's zig job still fails nemotron_experts_contract_host, "all eighteen pipelines and two controls reach the actual collector". With the two rows2r8 pipelines, that count needs updating.

On the two things you found on main, yes please: open an issue for each, starting with greedy drafted output that doesn't match serial. That's an exactness failure, so it's our top priority, and we have no M1 to reproduce it on. For that one, please include:

  • the exact commands, with every request's prompt and settings;
  • the token arrays where the drafted, alone and serial replies part, and the index where they first differ;
  • whether it happens at one stream with drafts on (drafted against "draft": false), or only with several streams.

Our M3 Ultra and M5 Ultra runs hold drafted == serial and concurrent == alone on our prompts, so we'll replay your exact requests on both to see whether it's specific to M1. The slow no-draft greedy rounds (about 95 ms against 12 ms a token) can go in a second issue.

mrv777 and others added 5 commits October 30, 2026 02:15
…windows

On applegpu_g13, windows of 16 or more rows take the routed experts through tf_experts_rows at RPS 8, SG 2, MB 2
(tf_xup_rows2r8 / tf_xdown_rows2r8) instead of tf_experts_w's three members a pass. Each member row's dot keeps
tf_qdot16's order and its single-member tail, so the bits are unchanged. Other chips, one-row decode, MTP and windows
under 16 rows keep their kernels. tf-nemotron-experts' rows2r8 variant now checks the shipped instantiation.

(cherry picked from commit 33bc47c9401a51523bb8224eac99cc1b0aba9a95)
… kernel contract

Both join the routed-expert cases as replacements that must equal tf_x*_w3's bytes, and their records carry the M1/M2
limits the offline search compiled (448 threads on both). The cases' completeness check needs a larger eval quota.

(cherry picked from commit d4ff9b7618ce762c844e0d3e66b355cdb4affd68)
…census

The census now expects 22 entries and 20 plain pipelines, and the fp32-sum
control is found by its fault rather than by index. Doc comments name the
rows2r8 pipelines.

(cherry picked from commit 87016e0bf7e6f45d99b968f4cefddb569da2225b)
(cherry picked from commit 52733daf28ff9bf5b7b979eb4aa73daf103d7583)
@ashhart
ashhart force-pushed the perf/nemotron-m1-wide-expert-rows branch from 947cef7 to bd662ec Compare October 30, 2026 02:15
@ashhart
ashhart merged commit 581cbe6 into ashhart:main Oct 30, 2026
9 checks passed
ashhart added a commit that referenced this pull request Oct 30, 2026
(cherry picked from commit 52733daf28ff9bf5b7b979eb4aa73daf103d7583)
@ashhart

ashhart commented Oct 31, 2026

Copy link
Copy Markdown
Owner

Thank you @mrv777. This is in TensorFold 1.2.0, released today: https://github.com/ashhart/TensorFold/releases/tag/v1.2.0

It landed with your commits under your own name, so you're listed as a TensorFold contributor, and it's credited to you in the release notes and the changelog.

@mrv777
mrv777 deleted the perf/nemotron-m1-wide-expert-rows branch November 26, 2026 22:24
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