Repository navigation
Flash Next on CUDA: one pooled n-gram gather for every row a shared pass needs - #4009
Conversation
|
Thanks @BadAd84. Reviewed as a first review and it is in good shape: the switch is off by default with exact accepted values and startup validation, the off arm is level with main in every cell, every reply matches main by token hash on both arms, and the mutant table shows each new test catches its own planted break. CI is green at the head and it applies cleanly to today's main. One merge-order note, no action needed from you: #2281 shares only the gradient-serving pin row, and whichever lands second retakes it. We will run the device check on our side: one GB10 A/B of the shared-pass cells against main. We were unable to run any GPU cell here.
|
9d60e51 to
8345227
Compare
|
@BadAd84 @ashhart I independently checked Since the review above, main Igor Lessio - AIFLOW LABS |
8345227 to
3f3b422
Compare
3f3b422 to
576fac4
Compare
…ne pool call A round's shared pass called the n-gram gather once a stream, so the distinct-row read, the row dequantization and the parked readers all ran a stream at a time. cuda_ple_gather_lanes.zig hashes every lane's tokens from that lane's own history into one id array and fills the lanes' consecutive slices of the pass's buffers in one call, with the distinct-row gather and the pool shared by the pass. One lane, 65 lanes and more, and the switch off (TF_FLASHNEXT_GATHER_EARLY, default 0) all keep the per-stream call. Every row is the per-stream call's: the same ids from the same history, the same one-row dequantization, the same group sums, and each stream's history moved past its own rows. The tests compare both table formats against successive per-lane rows calls, byte for byte in out, xs and the history, with lanes whose ids collide across the call, and pin the lane bound.
Keep main e172bcc and its prompt/scratch changes while carrying the pooled gather. Retake the combined state source identity; leave the unchanged batch identity and checker intact.
576fac4 to
ee71cd9
Compare
|
Thank you @BadAd84. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes. |
Terms used below: "main" is upstream TensorFold at
3661a4281425a71eb5f2a56922f0f2a33e5f4f17, the base the branch's commits sit on (the tip has since moved to6fc30cf0bf3bf0f6488d62f93550e56d7ded1944), and "the branch" is the maintainer's rebase of this pull request's two commits onto it plus his own one commit on top, whose head is the commit named below.What this changes
A round's shared pass called the n-gram gather once a stream: each lane's tokens were hashed into that lane's own id array and its rows were read, dequantized and summed on a call of its own, so the distinct-row read and the parked readers served one stream at a time. With this change the pass hashes every lane's tokens from that lane's own history into one id array, reads the distinct rows once for the whole pass through the same one-row gather, and sums each stream's groups from that stream's own rows; the per-stream call stays for one lane, 65 lanes and more, and for a pass with the switch off. Which rows are asked for and how a row is dequantized do not change, so every reply is main's by construction, and the new test compares the two paths byte for byte on both table formats.
3661a4281425a71eb5f2a56922f0f2a33e5f4f17. The branch is the maintainer's rebase of our two commits onto it,80550ba6b(the change: 7 files, 312 insertions and 6 deletions, one new file of 301 lines) anda50eba654(the receipt: 4 files, 309 insertions), both authored byBadAd84and committed under the maintainer's identity (ashhart) by the rebase, followed by his own834522789(1 file, 1 insertion and 1 deletion: thecuda_batch_state.zigpin row retaken for the merged bytes). Against3661a4281the head is 11 files, 621 insertions and 6 deletions, one insertion and one deletion fewer than the sum of the three commits because our change and his commit rewrite the same pin line, andCHANGELOG.mdis main's file entire: the changelog line is the maintainer's to add with the number, so none is claimed here.atc overlap --json: re-run on this head83452278966bf6be577ce6f5962598f60b729bdaon 2026-11-20: incomplete (complete: false, with its four limits,otherPullRequestsLimit,pullFilesUnavailable,pullRequestsLimitandtimeLimit), 11 changed files and 19 overlap rows, all of them working-tree rows (5clean, 3conflicts, 11unknown); no row this run comes from our own trunk. The threeconflictsentries are CUDA: #1764's BF16 prompt tile for Qwen ModelOpt, Nemotron and Dory, plus two exact Qwen prompt kernels (#1762) #2008, moss image: serve pictures on CUDA (encoder, decoder and host for #1573) #2281 and Ling-3.1-flash on CUDA, on one or two GB10s #2340, and each conflicts in the same files it conflicts with3661a4281alone: CUDA: #1764's BF16 prompt tile for Qwen ModelOpt, Nemotron and Dory, plus two exact Qwen prompt kernels (#1762) #2008 inzig/tests/cuda/contract_limits.zigandzig/tests/fixtures/gradient-serving.tsv; moss image: serve pictures on CUDA (encoder, decoder and host for #1573) #2281 inCHANGELOG.md,zig/tests/cuda/contract_limits.zig,zig/tests/cuda/main.zigandzig/tests/fixtures/gradient-serving.tsv; Ling-3.1-flash on CUDA, on one or two GB10s #2340 indocs/environment.md,zig/src/platform/environment.zigandzig/src/platform/environment_test.zig. The files they share with this change are the pin fixture (CUDA: #1764's BF16 prompt tile for Qwen ModelOpt, Nemotron and Dory, plus two exact Qwen prompt kernels (#1762) #2008, moss image: serve pictures on CUDA (encoder, decoder and host for #1573) #2281 and Ling-3.1-flash on CUDA, on one or two GB10s #2340),docs/environment.md(Ling-3.1-flash on CUDA, on one or two GB10s #2340) andzig/src/platform/environment.zig(CUDA: #1764's BF16 prompt tile for Qwen ModelOpt, Nemotron and Dory, plus two exact Qwen prompt kernels (#1762) #2008 and Ling-3.1-flash on CUDA, on one or two GB10s #2340): a pin row or a table row is retaken by whichever lands second. This head also merges into the current main6fc30cf0bwith one conflicted file,zig/tests/fixtures/gradient-serving.tsv(git merge-tree, rc 1), and nothing else.How the pooled gather reaches the code:
cuda_ple_gather_lanes.zig(new) holds the whole change.earlyslices the pass's lanes intoParts over the pass's own buffers (each lane's history, its tokens, and its consecutive slices ofembandxs);rowsManycopies each lane's history into its own[max_n]i64, hashes that lane's tokens from it, fills the lanes' slices in one call through the same pathsNgram.rowstakes (the distinct-row gather for NVFP4 and FP8 tables, the parked split for MLX tables, the caller's fill for a single row), then multiplies an MLX table's scale, sums each row's groups into its stream'sxsslice and writes each lane's history back past its own rows, in the orderrowsdoes it.cuda_batch_state.zig:Gather.maincallsgather_lanes.early(g)after the single-stream branch and returns when it ran, so the per-stream loop below it is main's path untouched.cuda_ple.zig:Ngram.SplitandSplit.partbecome public, so the pooled call reaches the same parked splitrowsreaches; nothing about the split changes.TF_FLASHNEXT_GATHER_EARLY, default0, read once a pass throughsys.environment.readincuda_ple_gather_lanes.zigand validated at startup byenv_validate.startup(), because its row is in the.flashnextscope ofplatform/environment.zig, the scope that check covers. Its row indocs/environment.mdsays what it does.earlyLanesis the one place that decides, and its test pins 1, 64, 65 and 0.TF_FLASHNEXT_PLE_IO), the dedup gather,TF_FLASHNEXT_WARM_AHEAD,TF_FLASHNEXT_TABLE_WARM, the draft path, and every default. With the switch off, no code belowGather.main's first statement changes.Checks on this head
The host gates in this section ran on
1ba863a5d, our own merge of3661a4281425into the branch before the maintainer's rebase, on an archive-verified tree of it; this head differs from that tree in one line ofdocs/environment.mdand nothing else (git diff --name-only 1ba863a5d 834522789names that one path): the settings-count sentence, which reads 34 there and 33 on this head while the table holds 34 rows. CI ran on this head itself.Head commit:
83452278966bf6be577ce6f5962598f60b729bda(treeb72c9fcb3944ae64a676867a869bef9e2092819b), the maintainer's commit on top of our two,80550ba6b(the change) anda50eba654(the receipt). Every commit on the branch is unsigned (%G?=Non all three): the rebase carried the authors over and left the signatures our two commits had before it behind, and the maintainer's commit is his own.git diff --check 3661a4281..834522789is clean. The two files the pooled call lives in,cuda_ple_gather_lanes.zigandcuda_ple.zig, are byte-identical to the receipted head4c276efdf(equal object names);cuda_batch_state.zigis not, because main's prompt-chunk default is in it beside this change's one call line, and the receipt paragraph below says exactly what that leaves standing.Lean checker,
python3 tools/zig/lean_check.py, one tree at a time with that tree's own copy (the base's copy and the gate tree's are byte-identical, sha256 prefixa08d2a424e01b16a): base3661a42816,967 files and 168 problems; the gate tree 6,968 files and 168 problems; both rc 1, the tool's "problems found".python3 tools/zig/lean_new.pygiveslean: 168 problems, 168 on the base, 0 new. The one extra file is the change's new source file and it adds no finding; the 168 are inherited from main.python tools/pr_check.py --base 3661a4281425 --pr 4009:merge base 3661a4281425a71eb5f2a56922f0f2a33e5f4f17; 11 changed files; lean 168 vs 168, 0 new;0 error(s), 1 warning(s); warnings are advisory. The warning isWARN: Zig changed: no Unreleased bullet naming this PR, advisory because the Unreleased bullet is the maintainer's to add with the number.zig fmt --check build.zig zig: rc 0 with no output, on the gate tree and on main3661a4281, both extracted from verified archives.Tests run as the no-GPU workflow runs them, on x86_64 Linux (16 CPUs, 27 GiB available, Zig 0.17.0,
--maxrss 6000000000, umask 0022, no GPU visible, taken under a host-wide lock), on a freshgit archivetree of the gate tree checked blob by blob against its commit'sgit ls-tree(12,572 entries, 0 mismatches, 0 extra files):zig build -Dfixed-cpu test-environment-hostrc 0 in 73 s,Build Summary: 3/3 steps succeeded; 27/27 tests passed;zig build -Dfixed-cpu test-gradient-serving test-qwen-serving-hostrc 0 in 4 s,Build Summary: 7/7 steps succeeded; 3/3 tests passed;zig build compile-config-server -Dtarget=aarch64-linux-gnurc 0 in 243 s,Build Summary: 8/8 steps succeeded;zig build compile-cuda-tests -Dtarget=aarch64-linux-gnurc 0 in 212 s,Build Summary: 10/10 steps succeeded;zig build -Dfixed-cpu test-cuda-hostrc 0 in 1,005 s,Build Summary: 436/436 steps succeeded; 7606/7649 tests passed (43 skipped), with no failure.New tests, each shown failing on a mutant of this change: the set was re-run on a fresh extraction of the gate tree, one mutant at a time with its anchor asserted once, and the unmutated control first. Control rc 0,
Build Summary: 436/436 steps succeeded; 7606/7649 tests passed (43 skipped)in 1,005 s, andfailblocks.pynamed nothing.ANCHORS 6 mutants, 0 not exactly once. The one mutant whose file the merge changed,no-call, was re-run on the same tree: rc 1,Build Summary: 434/436 steps succeeded (1 failed); 7632/7676 tests passed (43 skipped, 1 failed)in 1,339 s, andcompare-fails.pyagainst the control readsin both: 0, only A: 0, only B: 1, the one beingthe shared pass takes the pooled gather before its own rows call a stream. The other five editcuda_ple_gather_lanes.zig, which is byte-identical (da641c2450a7) to the head they were killed on and reads nodefault_chunk, so their earlier KILLED results stand and they were not re-run. Every one of the six fails exactly one test, and it is the one its anchor plants, which the table names.the shared pass takes the pooled gather before its own rows call a streama pooled pass takes 1 to 64 lanes, and the switch decides the restthe one pooled call equals each stream's own rows call, byte for byte, on both table formatsthe one pooled call equals each stream's own rows call, byte for byte, on both table formatsa pooled gather refuses parts whose outputs are not one buffer's consecutive slicesthe one pooled call equals each stream's own rows call, byte for byte, on both table formatsMeasured on the gate tree on one x86_64 box: the control and the re-run mutant read the Build Summary lines quoted, and
compare-fails.pynamed the re-run's failing test from its own log rather than any grep of mine; the five not re-run were killed on an earlier head whosecuda_ple_gather_lanes.zigis this head's byte for byte. Each mutant fails exactly one test and that test is the one its anchor plants, which is the discrimination the set exists for.83452278966bf6be577ce6f5962598f60b729bda(this head), run on this pull request:fmt,zig,zig-b,zig-windows,pytest,benchmark-clients(ubuntu and windows),release-pathsandzig-macos-dev-toolsall completed successfully;host-nightly-linuxandhost-nightly-macosare skipped by their own conditions.Receipt
docs/receipts/flashnext-gather-early/README.md(withcells.csv,noise-rule.mdandexactness.md), measured on one GB10 in one GPU window on 2026-11-16/17, with nothing else on the GPU. Arms M (main077998f7d), B0 (the change commit05e319fca,TF_FLASHNEXT_GATHER_EARLY=0) and B1 (the change commit05e319fca,=1), two fresh boots an arm in the order M1 B0a B1a B1b B0b M2,tools/bench_openai.py --tokens 256 --reps 3 --temperatures 0at one stream andtools/bench_concurrent.py --tokens 256 --reps 3 --temperatures 0 --levels 1,2,4,6,8 --alone --serial, code and chat prompts, cache on, 32,768-token window and 8 slots, bf16 KV, drafts on. Verdicts by the 1% both-under-both rule: B1 against main 6 cells faster, 6 level, 0 slower; B0 against main 0 faster, 12 level, 0 slower; B1 against B0 7 faster, 5 level, 0 slower. No cell was slower, so no rerun was taken.4c276efdf(the change commit05e319fcaplus the receipt's four files):cuda_ple_gather_lanes.zigandcuda_ple.zigare byte-identical (equal object names), and so are the receipt's four files underdocs/receipts/flashnext-gather-early/;cuda_batch_state.zigis not: main's prompt-chunk default went from 1,536 to 3,072 rows in it while this change's onegather_lanes.early(g)line carried across, so its pin row was retaken (396d3672at the receipted head,493ee0dcon this head). The receipt was measured on the change commit against main077998f7d, both with the 1,536-row default; the gather code it measured is this head's byte for byte, and main's default is main's own change.environment.ziganddocs/environment.mddiffer from the receipted head by main's rows beside this change's own row,gradient-serving.tsvby main's pins beside this change's two re-pins, andenvironment_flashnext_test.zigby the setting count main set (34: 23 switches, 9 integers, 1 real, 1 structured), which this change's one switch row is part of and its test asserts.TF_FLASHNEXT_GATHER_EARLY, default0; accepted values exactly0and1, anything else stops the start naming the setting, the text and the accepted forms; the profile row is the row indocs/environment.md(the recipe exports1). With it off nothing changes, so no profile needs it.05e319fcawith Zig 0.17.0 on an aarch64 Ubuntu 24.04.5 machine (NVIDIA GB10, 20 CPUs); the receipt images are main077998f7dand that commit, each built from a verifiedgit archivetree of its own commit (11,832 and 11,833 entries, 0 mismatches) in the recipe's Zig base image with CUDA 13.3, and the checkpoint isnvidia/Qwen3.8-Flash-Next-FP4-Localat revisionb9ce18a6887e18c089fdea8509a33e63429a3300on every arm. Boot cost is unmoved: 34.0 to 34.1 s of launch-to-answered on every arm, and the engine's pool line is the same on all three (8 streams at once, 1.29 GiB each at a 32,768-token window, 27.8 to 27.9 GiB left after a 9.0 GiB reserve, 1,536-row passes, a 16.0 GiB prompt cache), with 0 differing startup line shapes between main and the change commit. The host checks above ran the gate tree1ba863a5dand main3661a4281425a71eb5f2a56922f0f2a33e5f4f17, each on a freshgit archivetree (12,572 and 12,567 entries, 0 mismatches, 0 extra files), with Zig 0.17.0 on x86_64 Linux, 16 CPUs, no GPU and no checkpoint.tools/bench_concurrent.py --alone --serialortoken_shaagainst"draft": false: all four contracts hold on all three arms, cache on and cache off. Concurrent equals solo 252/252 per arm (levels 1 to 8, greedy); drafted equals"draft": false20/20 per arm; the six golden prompts are 8/0 drafted against serial and 5/0 concurrent against solo per arm;served_exactness.py draftsis 8/8 on the cached boot and 8/8 on the cache-off boot;turnsagainstfreshis 7/7 (resumed from [13, 16, 16, 13, 24, 18, 3406] cached prompt tokens); a cancelled stream leaves the next request valid and equal 3/3; and every greedy reply of pass A is main's bytoken_sha, 528/528 for B0 and 528/528 for B1. The host test pins the same thing on the CPU: the pooled call'sout,xsand moved histories equal successive per-lanerowscalls, byte for byte, on an NVFP4 table and on an MLX table, with lanes whose ids collide across the call, and the distinct-row gather is the one that runs on the NVFP4 table.=1; code 373.9 and 369.0 against 379.3 and 379.8. The mean gap against main by stream count on=1is +0.34% at 1, +0.68% at 2, +1.32% at 4, +1.77% at 6 and +2.10% at 8, and=0is level with main at every count (-0.52% to -0.12%, 12 cells level). Tokens a round are identical on every arm and cell (5.224 and 2.813 at one stream, 2.510 for chat at 4 to 8, 4.830 for code at 2 to 4, 4.491 for code at 6 to 8), and the server's own counters are identical too: 1,488 of 2,384 accepted (0.6242) after the one-stream probe and 22,760 of 33,060 (0.6884) after the concurrent probe, on all six arms.tools/bench_openai.py: 73.9/72.1 tok/s on main against 72.1/73.0 on=1for the raw completion prompt (level, -0.7%), and 47.3/46.1 against 46.1/46.7 for the chat prompt (level, -0.6%). The gain is at the stream counts that share the call, not at one stream.ttft_sfromtools/bench_cache.py --require-coldon a long prompt: level at every size against an image started with--prompt-cache-gib 0, two boots an arm: 2k 1.093/1.072 s on main against 1.091/1.067 s on=1, 8k 4.051/3.967 against 3.981/4.001, 32k 16.248/15.924 against 15.992/16.003, 64k 33.098/32.902 against 32.928/32.890, and the 2,571-tokenbench_cacheprompt 1.230/1.222 against 1.218/1.219 s, every first request reporting 0 cached tokens.Checklist
DRAFTS=0the two arms' replies are identical, 90 of 90 hashes.)"draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. (Measured: 20/20, 7/7 and 252/252 per arm, cache on and off; a cancelled stream leaves the next request valid and equal 3/3.)--drafter, no slower than without it, greedy and sampled. (Measured greedy: 0 cells slower of 36, one stream +0.34% and 8 streams +2.10% on the switch, cold prompts level at 2k to 64k, drafts-off 8 streams 153.9 against 152.1 tok/s. Sampled cells were not run in the window: the change moves which call reads the rows, not which rows are read or how a token is drawn from the logits.)