Repository navigation
nemotron cuda: skip empty pocket layers and spread the pocket over many blocks - #787
Conversation
… its two halves over many blocks tf_pocket_rows ran every MoE layer's pocket as one block of 256 threads a row over all 64 neurons, in use or not, so an empty pocket cost as much as a full one, and one SM read the layer's 1.4 MB of fp32 weights alone. Both changes are philip-pentatonic's proposals in ashhart#678. - Each pocket keeps its count of neurons in use, the first ones. The loader counts through the last up row that is not all zero, as pocketRead does, and the learner's setter keeps the count it is given instead of dropping it. The forward skips a layer whose count is 0. The kernels read the count from the device, so captured graphs follow the learner's changes; they are captured again only when a layer they skipped gets its first neuron. The buffers stay 64 neurons wide, so pocketRead still reads a loaded pocket. - tf_pocket_in computes relu^2(up y) into a new [rows, 64] fp32 scratch with a warp a neuron and a row: each lane adds its strided products and the xor tree adds the lanes, as in tf_pocket_rows. tf_pocket_out then adds act down into the shared expert's slot with a thread an output, neurons in order 0..n-1, and the same bf16 or fp32 add. From 256 rows (the first half) and 64 rows (the second) tiled forms take eight neurons by eight rows a warp and 16 rows a thread; every sum keeps its order. tf_pocket_rows stays in nemotron_ops as the test's reference; the engine no longer loads it. Every output keeps its bits. tf-cuda-test pocket compares the two kernels with tf_pocket_rows over all 64 neurons byte for byte: widths 2688 and 2728, 1 to 2048 rows, 0 to 64 neurons in use, fp32 and bf16 out, 194 cases. Its controls, the same neurons summed in reverse and the inputs rotated by one, change the bytes; the test fails with a batch of neurons added in reverse, with the relu removed, without the fused multiply-add, and with the last output dropped. A skipped layer leaves its output as it was where tf_pocket_rows added +0.0, which can only turn an exact -0.0 to +0.0. On the GB10, timed by the test with the L2 flushed before each launch and 44 neurons in use (two runs), a decode row takes 18 us instead of 94, 16 rows 24 to 27 us instead of 92 to 96, and a 2048-row prompt chunk 0.49 ms instead of 1.26 to 1.28. In the dev CLI (256 greedy tokens, drafts off, two runs each), a folder with 64 facts in 16 layers keeps its tokens at 9.66 to 9.67 ms a token instead of 11.19 to 11.28, the untaught model's 9.66 to 9.75, and the empty pocket keeps the untaught tokens at 9.63 to 9.75 ms instead of 11.26 to 11.29. A 4,096-token prompt takes 0.556 s instead of 0.585 taught and 0.530 s instead of 0.599 empty, against 0.528 untaught (one run each).
…es into its launcher, and a test pins its -0.0 The check moves from Forward.pocket into Ops.pocketRows, so tf-cuda-test pocket reaches the code the forward runs; the forward makes the same launches as before. tf-cuda-test pocket gains the one case where the bytes can differ from 1.0.8: a layer with no neuron in use whose shared expert output holds an exact -0.0. tf_pocket_rows over its 64 zero neurons adds +0.0, which turns each such -0.0 into +0.0 and leaves every other value as it was; the launcher launches nothing and keeps every value, -0.0 included. A graph captured while the layer held neurons still launches both kernels after its count falls to 0, and they add +0.0 as tf_pocket_rows did. 1, 16 and 300 rows, fp32 and bf16 out. Without the launcher's skip the case fails.
…nt of 0, and the count stays in its width - tf_pocket_out returns when the count it reads is 0. A graph captured while a layer held neurons and replayed after the learner emptied it added +0.0 there, turning an exact -0.0 into +0.0, while the eager forward skips the layer and keeps it. Now both keep every bit, so a replayed graph gives the eager forward's bytes. - The learner's setter refuses a count past the pocket's width: the kernels take their loop bounds and shared memory reads from the count on the device. - tf-cuda-test pocket's -0.0 case expects a launch reading a count of 0 to keep every byte, and fails on a tf_pocket_out without the return. Its comments name tf_pocket_rows instead of a release. On the GB10, tf-cuda-test pocket passes its 194 cases and the -0.0 case (1, 16 and 300 rows, fp32 and bf16 out), and the dev CLI gives the same 256 greedy tokens as before on the untaught, taught and empty-pocket folders, drafts on and off.
…another sequence is bound recapture binds the own sequence, records its graphs with a placeholder rule in e.sampling (null for the greedy set, seed 0 and temperature 1 for the sampled one), then binds the other sequence back. Engine.bind stores the current e.sampling into the sequence it leaves, so the own sequence kept the placeholder: a greedy request running on it would next launch the sampled set. The rule the own sequence had after its bind is now put back before it is left. The pocket now captures again when a layer gets its first neuron in an existing pocket, so this path runs more often than when only a new pocket did.
…pt chunk A serial prompt pass now runs up to pass_rows (4,096) rows, so the byte comparison with tf_pocket_rows covers that many rows too, with 44 and 64 neurons in use.
adec42d to
76fe1de
Compare
…e graphs are captured again recapture becomes pub so the test can call it; the engine is otherwise unchanged. The test loads the model with both draw modes' windows, as cuda_native.open leaves them with drafts off, and makes a second sequence. Four cases give the two sequences distinct rules: own greedy and other sampled, both sampled with other seeds and temperatures, own sampled and other greedy, and the last again with the own sequence bound. Each case captures the graphs again, then checks e.sampling, the rule each sequence keeps, and each rule once its sequence is bound. The cases run again with the draw kernel's handle cleared, so the sampled set's first capture fails after its placeholder rule is set; the count of graphs captured (the serial one and the greedy set) shows where it failed. On the GB10 all 9 runs pass. With a3df10e's two lines taken out, the 7 runs with the other sequence bound fail, the failing captures too: the own sequence keeps seed 0 and temperature 1.
The pocket kernels' signatures, pocketRows and its launches, the scratch table and tf-cuda-test pocket. Line breaks, trailing commas and two format strings split with ++ only: with comments and whitespace set aside the code is the same.
|
Thank you, @ThinkOffApp, and @philip-pentatonic, whose #678 traced the cost and proposed both changes. On CUDA, Nemotron with a Living Weights pocket skips a layer with no neuron in use and runs a taught layer's pocket as Your commits are in 1.0.10 under your name ( Your current head On a GB10, |
The pocket kernels' signatures, pocketRows and its launches, the scratch table and tf-cuda-test pocket. Line breaks, trailing commas and two format strings split with ++ only: with comments and whitespace set aside the code is the same.
What this changes
On CUDA, a Nemotron model with a Living Weights pocket decodes slower than without one, even with an empty pocket. @philip-pentatonic found and traced this in #678: on his RTX PRO 6000 with 1.0.7, undrafted decode fell from 290.4 to 199.3 tok/s, all in
tf_pocket_rows, which runs each MoE layer's pocket as one 256-thread block per row over all 64 neurons. This PR implements the two fixes he proposed there:tf_pocket_in(a warp per neuron and row) writes relu² into a small fp32 scratch, thentf_pocket_out(a thread per output) adds neurons 0..n-1 into the shared expert's output. The sums, their order and the final add are those oftf_pocket_rows, in the tiled prompt forms too.recaptureleft the captures' placeholder rule (seed 0, temperature 1) on the own sequence, so a greedy request on it would next launch the sampled set. 1.0.9 has this too; the new trigger above makes it run more often.recapturenow gives the own sequence back its rule before it binds the other one, andtf-cuda-test recapture MODELchecks it.On our GB10, 1.0.9 still pays about 1.6 ms a token for the pocket, empty or full. With this PR, taught and empty decode at the untaught speed undrafted (empty drafted too), with the same tokens. The GB10 numbers are ours; we have not measured philip's RTX PRO 6000. Metal already skipped empty pockets, and 1.0.9 has the Metal side of this work (#775).
Receipt
c36b536d) against this branch at76fe1de4(the last commit,5dc34d9d, only addstf-cuda-test recaptureand makesrecapturepub), built alike (Zig 0.17.0, CUDA 13.0,-Dsm=121). NVIDIA GB10, Ubuntu 24.04.5. Nemotron 3.5 Lightning 30B-A3B MLX 4-bitd9d758fb: untaught; taught (64 teaching attempts: 40 learned, 20 missed, 4 replaced; 440 neurons in use in 16 of 23 MoE layers); empty (64 neurons, none in use). Lines marked 1.0.8 come from the receipt before the rebase: v1.0.8 (0efbd447) against07189f62, the same fix.token_shaon all 120 requests, includingtools/bench_concurrent.py --alone --serial(concurrent equals solo, solo equals"draft": false). Taught differs from untaught on 98 of 120, so the check sees the pocket.tf-cuda-test pocketmatchestf_pocket_rowsbyte for byte in 196 cases, up to a whole 4,096-row prompt pass.tools/bench_openai.pyis 3-9% faster taught and 1-11% empty; untaught moves -2.4% to +2.2%.tools/prefill_cold.pyin 1.0.9. A 4,096-token prompt in the dev CLI (one 4,096-row pass in 1.0.9): 0.577 → 0.527 s taught, 0.576 → 0.517 s empty, 0.516 → 0.513 s untaught.zig build test700 of 705 passed, 5 skipped, as on 1.0.9.tf-cuda-test recapture MODEL(4 cases with distinct rules on the two sequences, each again with the sampled capture failing partway, then a full recapture) passes all 9 runs; witha3df10ea's two lines taken out, the 7 runs with the other sequence bound fail with the own sequence holding seed 0 and temperature 1.lean_check.py538, unchanged.zig fmt --checkclean.--slideserver learning on this branch writes the same files, slide graph and probe replies (logprobs included) as 1.0.9 at every checkpoint and after a restart. On 1.0.8 the same held on07189f62,e987544eandadec42df.Checklist
"draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. Drafted and concurrent: on every server (on 1.0.8 also in the dev CLI's lanes). Resumed equals fresh: one request pair per server (29 of 35 prompt tokens from the cache), equal on all 6.prefill_cold.py, which 1.0.9 does not have. On the taught folder a 4,096-token prompt is still 2.0% slower than untaught (12.6% on 1.0.9).tf-cuda-test recapturefails withouta3df10ea's two lines (7 of 9 runs). Fortf-cuda-test pocket: on 1.0.8 it failed on four broken kernels, and its -0.0 case failed on07189f62and on atf_pocket_outwithout its count-0 return. It cannot run on 1.0.9, where the command does not exist. It prints SKIP when built without the CUDA kernels (I read that path, I did not run such a build).Full receipt
Builds, folders and conditions
c36b536d(v1.0.9). After:76fe1de4, the tip of this branch (e7b65ff4,9803c8fc,0ffaefc2,a3df10ea,76fe1de4on v1.0.9;5dc34d9dafter it adds only the recapture test). Each tree matchedgit archiveof its commit file for file. Both were built withzig build installthenzig build native,-Dsm=121 -Dversion=1.0.9, Zig 0.17.0, nvcc 13.0.88. Onlynemotron_ops.fatbindiffers between the two builds.c36b536d, which are the same files as in 1.0.8; this PR touches no tool. One model process at a time.*.safetensorsandconfig.jsonhashed the same before and after every server and run.TENSORFOLD_NO_LIVE=1 tensorfold-native serve FOLDER --host 127.0.0.1 --port PORT --no-update-check, no--slide. Each server started with a fresh empty HOME, so it timed its own draft-window costs. One series of six servers, each folder's two builds next to each other in mirrored order (before first on taught and empty, after first on untaught). 120 s idle before each server. On 1.0.8 there were two such series.Between the second and third, my own client sent
"draft": falserequests: 8 prompts, 2 warm-up and 16 measured requests of 256 greedy tokens, thinking off.tensorfold run FOLDER --tokens <36 ids> --max-tokens 256, drafts off and on, and the 4,096-token prompt with--max-tokens 2 --no-drafts: 4 reps each, the folders rotated, the build order flipped every rep, 45 s idle before each rep.Same tokens
Nemotron 3.5 Lightning 30B-A3B MLX 4-bit, NVIDIA GB10, v1.0.9 against
76fe1de4:token_shabefore and after, every request, both serversbench_concurrent.py: concurrent equals its solo run, per serverbench_concurrent.py --serial: solo equals"draft": false, per serverNot re-run on 1.0.9. Nemotron 3.5 Lightning 30B-A3B MLX 4-bit, NVIDIA GB10, v1.0.8 against
07189f62:"draft": false: same before and aftertensorfold lanes, 134 fact questions thinking off and 40 on, solo and 40 at once, drafted and not: same token ids before and after--no-drafts, each buildtoken_sha. The next one is the same prompt and seed, with 29 of its 35 prompt tokens from the cache.Kernel test.
tf-cuda-test pocketrunstf_pocket_inthentf_pocket_outagainsttf_pocket_rowsover all 64 neurons and compares every output byte.tf_pocket_rowsturns 568,064 of them into +0.0. A skipped launch and a captured launch reading a count of 0 both keep every byte. On 1.0.8 the case failed on07189f62(the skip lived in the forward, not the launcher) and on atf_pocket_outwithout its count-0 return.Live learning. A
--slideserver learned 3 facts into a fresh copy of the untaught model on76fe1de4, with a restart after the first. 1.0.9 did the same on its own copy."draft": false, logprobs included). They equal them again after a restart, and the two folders end byte-identical.07189f62,e987544e,adec42df) the same checks held over 7 facts, with chat requests alongside: 587 of 600 chat pairs were identical, and the 13 that differ were sent while the trainer was fitting, with the two runs at different trainer steps; we read that as timing, not a forward difference.adec42dfwith log lines only showed the recapture firing after a restart, and a failed lesson taking a layer back from 1 to 0. The replies alone cannot prove the recapture: a build that recaptures only for new pockets also matched, and with no recapture at all 2 of 16 replies changed. So the logs are the evidence for the recapture, and the kernel test for the -0.0 bytes.Undrafted decode speed
Nemotron 3.5 Lightning 30B-A3B MLX 4-bit, NVIDIA GB10, v1.0.9 against
76fe1de4, medians:run --no-drafts, 256 tokens, tok/s (4 reps)"draft": false, 16 × 256 tokens, tok/s (one server each)tensorfold rounds, 4 reps × 2) took 11.487 → 9.893 ms a round taught and 11.474 → 9.812 empty, against 9.864 → 9.857 untaught. Of 1.0.8's 1.6 ms a token, 0.045 ms was left on the taught folder and none with the empty pocket. Not re-run on 1.0.9.Drafted decode speed
tools/bench_openai.py: decode tok/s, the median of the reps on the one server of each build. Nemotron 3.5 Lightning 30B-A3B MLX 4-bit, NVIDIA GB10, v1.0.9 against76fe1de4:bench_concurrent.py, one stream, greedy, 256 tokens: taught 1.055 and 1.075 after/before, empty 1.058 and 1.068, untaught 1.009 and 0.991. Its 4-stream greedy cells jump between two levels from rep to rep on every folder and build, untaught included, so we read no speed from them.run, 256 greedy tokens, 4 reps): taught 135.34 → 146.13 tok/s, empty 146.55 → 160.68, untaught 160.05 → 160.75.Prompt speed
tensorfold run MODEL --tokens-file <4,096 ids> --max-tokens 2 --no-drafts(one 4,096-row pass in 1.0.9), 4 reps, builds alternated. Nemotron 3.5 Lightning 30B-A3B MLX 4-bit, NVIDIA GB10, v1.0.9 against76fe1de4, medians:Where the time went
nsys profile -t cuda --cuda-graph-trace=nodearound one dev CLI run of 256 tokens, drafts off. Nemotron 3.5 Lightning 30B-A3B MLX 4-bit, NVIDIA GB10, 1.0.8 (not re-run on 1.0.9):tf_pocket_rows(23 layers × 255)07189f62tf_pocket_in+ 4,080tf_pocket_out(16 layers × 255)07189f6207189f62Kernel time in
tf-cuda-test pocket 30, with the L2 flushed before each launch and 44 neurons in use. NVIDIA GB10,76fe1de4, one run:tf_pocket_rows, µstf_pocket_in+tf_pocket_out, µsThe tiled forms take over from 256 rows for the first half and from 64 rows for the second. I picked those cut-overs on the GB10; other GPUs may do better with others.