Skip to content

Nemotron routed experts past one row: a 16-group's weight words as the outer loop, three members a pass, same bits - #502

Closed
BobClawblaw wants to merge 1 commit into
ashhart:mainfrom
BobClawblaw:nemotron-expert-words
Closed

BobClawblaw wants to merge 1 commit into
ashhart:mainfrom
BobClawblaw:nemotron-expert-words

Conversation

@BobClawblaw

Copy link
Copy Markdown
Contributor

ZIG-PREVIEW's "cheaper extra lanes": the routed-expert lane kernel past one row, faster at every width from 4 rows up and bit-identical to the one-row kernel at every width. Independent of #497 and #499 (based on main).

What changed

  • zig/kernels/metal/nemotron_experts.metal: tf_experts_w, instantiated as tf_xup_w3 / tf_xdown_w3. The existing lane kernel takes an expert's member rows two at a time with all 16 activations of a 512-chunk live per member. The new one takes the four weight words of a 16-group as the outer loop, so four activations a member are live, and three members a pass at four simdgroups fit. Each (member, output) accumulator still receives its terms in tf_qdot16's order, the group's scale * accum + sum * bias step is unchanged, and the pass ends in the same simd_sum, so every width gives the one-row kernel's bits. Single leftover members go through the unchanged tf_rowdot.
  • zig/src/families/nemotron/layers.zig, kernels.zig, backend.zig: members = 3 selects it with the {4, 4} geometry; rows2/rows4 stay. One-row forwards, the MTP head, the prompt path and CUDA are untouched.
  • zig/tests/nemotron_experts.zig: the word-outer shapes in the bench's variant table (the engine's own w3s4 as compiled, plus 2/4/6 members, two simdgroups and eight outputs for comparison).
  • Docs: the step in adding-a-zig-family.md with its numbers, three exact-but-not-faster shapes in its "Don't bother" list, a section in the Nemotron recipe, and ZIG-PREVIEW's item 4 restated with what remains.

Exactness

  • tf-nemotron-experts (synthetic 4-bit weights, Zipf routing): every shape bits == the engine's kernel at 2, 3, 4, 8, 16, 32 and 64 rows, and each row alone through the one-row kernel equals its row inside the 8-row window. Repeated over four runs while the shapes were explored.
  • Served, this binary against the previous one: 30 cache-off reference replies byte-equal (four 4-turn chats with 9k-14k-token system prompts, a divergence case, two seeded sampled chats, drafted against draft: false); then 467 replies across solo, draft: false, seeded sampling solo and 3x concurrent, dropped connections, 8 threads x 6 reps, 10 threads on 2 lanes, and 89k- and 130k-token prompts, all equal to the reference. The speed runs' own checks: 656 concurrent replies equal to their solo runs and 24 equal to their plain runs, 0 unequal.

Speed

Expert up + down, 23 layers, µs a layer in the bench (M5 Ultra):

Rows 2 4 8 16 32 64
rows2 (before) 57.5 102.4 188.1 291.0 520.2 982.9
tf_experts_w 57.5 98.7 185.1 281.8 488.7 894.6

Served, cache off, old and new binaries alternated twice (bench_openai.py, bench_concurrent.py --alone --serial, 256 tokens, greedy):

Before After
1 stream, code / prose 503-506 / 346-348 tok/s 513-514 / 350-352
8 streams, code / prose, aggregate 947-948 / 713-722 984-986 / 729-731
32 streams, code / prose, aggregate 790-794 / 751-754 801-803 / 760-762

A small step, not the flattened curve: the kernel streams unique expert weights at about 1 TB/s up to 8 rows, 730 GB/s at 32 and 540 GB/s at 64, and most of that gap remains. Three shapes that kept every bit and gained nothing are recorded in the guide so no one repeats them: member blocks spread over grid z, activations prepared once a forward (50 fewer ALU ops a chunk, same time), and 8-byte vector loads. Six or eight members a pass, or four at two simdgroups, lose 5-40%.

Receipts

  • Apple M5 Ultra, 256 GB, macOS 27.0.1; Zig 0.17.0, Metal Toolchain 27A266a; base main f8fe17d; checkpoint TensorFold/NVIDIA-Nemotron-3.5-Lightning-30B-A3B-MLX-4bit revision d9d758fb; serve --temperature 0 --no-thinking --parallel 32 --context 32768 --prompt-cache-gib 0 for the speed runs.
  • zig build test -Dcpu=apple_m1: 91/91 steps. zig fmt --check clean. tools/zig/lean_check.py: 512 on main, 512 after.
  • Not run: Metal on M1 to M4 (the kernel compiles there too; tf-nemotron-experts would show its bits and time), CUDA (untouched).

🤖 Generated with Claude Code

…e outer loop, three members a pass, same bits

The lane kernel took an expert's member rows two at a time with all 16 activations of a chunk live. tf_experts_w takes the four weight words of a 16-group as its outer loop, so four activations a member are live and three members a pass at four simdgroups fit. Each lane sums in tf_qdot16's order and ends in the same simd_sum, so every width gives the one-row kernel's bits; tf-nemotron-experts checks that at 2 to 64 rows and shows the 23 layers' up and down projections going from 11.93 to 11.24 ms at 32 rows and 22.57 to 20.58 at 64, equal at 2.

Served on an M5 Ultra with the cache off, alternated twice against the previous kernel: one stream 503 to 513 tok/s on code and 348 to 351 on prose, 8 streams 947 to 985 and 713 to 731 in all, 32 streams 792 to 802 and 752 to 761, every reply equal to its solo and plain run. Three other shapes that kept the bits and gained nothing go to the guide's "Don't bother" list: member blocks over grid z, activations prepared once a forward, and vector loads.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@ashhart

ashhart commented Oct 10, 2026

Copy link
Copy Markdown
Owner

Thank you, @BobClawblaw. Turning the weight words into the outer loop while keeping each lane's summation order is
careful kernel work, and the shapes you list as exact but no faster will save the next person a day.

It's on main as 2985ed7e2, under your name. You left M1 to M4 open, so we ran it on an M3 Ultra.
tf-nemotron-experts shows every shape bit-equal at every width, now including 128 rows (our follow-up 0bf8ec369),
and tf_experts_w 7-9% faster than the old kernel at 32 rows, 10-13% at 64 and 15% at 128. Greedy prose and code came
out token for token as before. One stream is level (200 against 199 tok/s on prose), and with #518, 64 sessions went
from 396 to 409 tok/s. It stays on for every chip.

We re-made the commits without their AI-tool trailer lines, which main's history doesn't carry, so GitHub shows this PR as closed rather than merged. The commit is yours.

@ashhart ashhart closed this Oct 10, 2026
eleqtrizit pushed a commit to eleqtrizit/TensorFold that referenced this pull request Oct 10, 2026
…hhart#518

Lands ashhart#502 on main beside ashhart#518: a shared round can now hold 128 rows, so the routed-expert checker
bit-checks and times every pass shape at 128 rows too. Layer 0's routing copy is sized by the
widest width.
eleqtrizit pushed a commit to eleqtrizit/TensorFold that referenced this pull request Oct 10, 2026
@BobClawblaw
BobClawblaw deleted the nemotron-expert-words branch October 15, 2026 15:21
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…hhart#518

Lands ashhart#502 on main beside ashhart#518: a shared round can now hold 128 rows, so the routed-expert checker
bit-checks and times every pass shape at 128 rows too. Layer 0's routing copy is sized by the
widest width.
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 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.

2 participants