Skip to content

Flash Next on CUDA: one pooled n-gram gather for every row a shared pass needs - #4009

Merged
ashhart merged 6 commits into
ashhart:mainfrom
BadAd84:flashnext-gather-early-up
Nov 21, 2026
Merged

ashhart merged 6 commits into
ashhart:mainfrom
BadAd84:flashnext-gather-early-up

Conversation

@BadAd84

@BadAd84 BadAd84 commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Terms used below: "main" is upstream TensorFold at 3661a4281425a71eb5f2a56922f0f2a33e5f4f17, the base the branch's commits sit on (the tip has since moved to 6fc30cf0bf3bf0f6488d62f93550e56d7ded1944), 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.

How the pooled gather reaches the code:

  • cuda_ple_gather_lanes.zig (new) holds the whole change. early slices the pass's lanes into Parts over the pass's own buffers (each lane's history, its tokens, and its consecutive slices of emb and xs); rowsMany copies 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 paths Ngram.rows takes (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's xs slice and writes each lane's history back past its own rows, in the order rows does it.
  • cuda_batch_state.zig: Gather.main calls gather_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.Split and Split.part become public, so the pooled call reaches the same parked split rows reaches; nothing about the split changes.
  • The switch is TF_FLASHNEXT_GATHER_EARLY, default 0, read once a pass through sys.environment.read in cuda_ple_gather_lanes.zig and validated at startup by env_validate.startup(), because its row is in the .flashnext scope of platform/environment.zig, the scope that check covers. Its row in docs/environment.md says what it does.
  • The lane bound is 64: the parts and their histories are the gather thread's own stack arrays, so a pass wider than 64 lanes keeps the per-stream call rather than growing them. earlyLanes is the one place that decides, and its test pins 1, 64, 65 and 0.
  • Untouched: the reader and the page-cache policy (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 below Gather.main's first statement changes.

Checks on this head

  • The host gates in this section ran on 1ba863a5d, our own merge of 3661a4281425 into the branch before the maintainer's rebase, on an archive-verified tree of it; this head differs from that tree in one line of docs/environment.md and nothing else (git diff --name-only 1ba863a5d 834522789 names 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 (tree b72c9fcb3944ae64a676867a869bef9e2092819b), the maintainer's commit on top of our two, 80550ba6b (the change) and a50eba654 (the receipt). Every commit on the branch is unsigned (%G? = N on 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..834522789 is clean. The two files the pooled call lives in, cuda_ple_gather_lanes.zig and cuda_ple.zig, are byte-identical to the receipted head 4c276efdf (equal object names); cuda_batch_state.zig is 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 prefix a08d2a424e01b16a): base 3661a4281 6,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.py gives lean: 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 is WARN: 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 main 3661a4281, 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 fresh git archive tree of the gate tree checked blob by blob against its commit's git ls-tree (12,572 entries, 0 mismatches, 0 extra files): zig build -Dfixed-cpu test-environment-host rc 0 in 73 s, Build Summary: 3/3 steps succeeded; 27/27 tests passed; zig build -Dfixed-cpu test-gradient-serving test-qwen-serving-host rc 0 in 4 s, Build Summary: 7/7 steps succeeded; 3/3 tests passed; zig build compile-config-server -Dtarget=aarch64-linux-gnu rc 0 in 243 s, Build Summary: 8/8 steps succeeded; zig build compile-cuda-tests -Dtarget=aarch64-linux-gnu rc 0 in 212 s, Build Summary: 10/10 steps succeeded; zig build -Dfixed-cpu test-cuda-host rc 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, and failblocks.py named 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, and compare-fails.py against the control reads in both: 0, only A: 0, only B: 1, the one being the shared pass takes the pooled gather before its own rows call a stream. The other five edit cuda_ple_gather_lanes.zig, which is byte-identical (da641c2450a7) to the head they were killed on and reads no default_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.

Mutant What fails
the shared pass never takes the pooled call, so main's per-stream path runs the shared pass takes the pooled gather before its own rows call a stream
every lane count takes the pooled call, so a pass above 64 lanes overruns the parts and their histories a pooled pass takes 1 to 64 lanes, and the switch decides the rest
every lane hashes its tokens from the first lane's history the one pooled call equals each stream's own rows call, byte for byte, on both table formats
each lane's history is written back from the next lane's the one pooled call equals each stream's own rows call, byte for byte, on both table formats
the parts' outputs need not be one buffer's consecutive slices a pooled gather refuses parts whose outputs are not one buffer's consecutive slices
each lane's group sums land in its own slice reversed the one pooled call equals each stream's own rows call, byte for byte, on both table formats

Measured 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.py named 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 whose cuda_ple_gather_lanes.zig is 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.

  • CI on 83452278966bf6be577ce6f5962598f60b729bda (this head), run on this pull request: fmt, zig, zig-b, zig-windows, pytest, benchmark-clients (ubuntu and windows), release-paths and zig-macos-dev-tools all completed successfully; host-nightly-linux and host-nightly-macos are skipped by their own conditions.
  • Not run, and why: two ranks (no second GPU here); and the MLX checkpoint on a device (the MLX path is exercised through its table format in the host test, not through a device launch).

Receipt

  • Main against branch: docs/receipts/flashnext-gather-early/README.md (with cells.csv, noise-rule.md and exactness.md), measured on one GB10 in one GPU window on 2026-11-16/17, with nothing else on the GPU. Arms M (main 077998f7d), B0 (the change commit 05e319fca, TF_FLASHNEXT_GATHER_EARLY=0) and B1 (the change commit 05e319fca, =1), two fresh boots an arm in the order M1 B0a B1a B1b B0b M2, tools/bench_openai.py --tokens 256 --reps 3 --temperatures 0 at one stream and tools/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.
  • Boxes it reaches: Flash Next on CUDA, one GPU and two ranks, at every admitted stream count; the MLX table format through the same call.
  • The branch carries the change's own code unchanged, file by file against the receipted head 4c276efdf (the change commit 05e319fca plus the receipt's four files): cuda_ple_gather_lanes.zig and cuda_ple.zig are byte-identical (equal object names), and so are the receipt's four files under docs/receipts/flashnext-gather-early/; cuda_batch_state.zig is not: main's prompt-chunk default went from 1,536 to 3,072 rows in it while this change's one gather_lanes.early(g) line carried across, so its pin row was retaken (396d3672 at the receipted head, 493ee0dc on this head). The receipt was measured on the change commit against main 077998f7d, 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.zig and docs/environment.md differ from the receipted head by main's rows beside this change's own row, gradient-serving.tsv by main's pins beside this change's two re-pins, and environment_flashnext_test.zig by 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.
  • New switch? TF_FLASHNEXT_GATHER_EARLY, default 0; accepted values exactly 0 and 1, anything else stops the start naming the setting, the text and the accepted forms; the profile row is the row in docs/environment.md (the recipe exports 1). With it off nothing changes, so no profile needs it.
  • Environment, with the TensorFold commit, Zig and backend toolchain, machine and GPU, checkpoint and revision: the receipt's runs are the change commit 05e319fca with Zig 0.17.0 on an aarch64 Ubuntu 24.04.5 machine (NVIDIA GB10, 20 CPUs); the receipt images are main 077998f7d and that commit, each built from a verified git archive tree 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 is nvidia/Qwen3.8-Flash-Next-FP4-Local at revision b9ce18a6887e18c089fdea8509a33e63429a3300 on 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 tree 1ba863a5d and main 3661a4281425a71eb5f2a56922f0f2a33e5f4f17, each on a fresh git archive tree (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.
  • Paths and platforms the change reaches, which I ran and which I could not: every path is host code compiled into the CUDA server, and the host suite covers it on Linux aarch64; I did not run it on macOS, Windows, HIP or a device.
  • Exactness, from tools/bench_concurrent.py --alone --serial or token_sha against "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": false 20/20 per arm; the six golden prompts are 8/0 drafted against serial and 5/0 concurrent against solo per arm; served_exactness.py drafts is 8/8 on the cached boot and 8/8 on the cache-off boot; turns against fresh is 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 by token_sha, 528/528 for B0 and 528/528 for B1. The host test pins the same thing on the CPU: the pooled call's out, xs and moved histories equal successive per-lane rows calls, 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.
  • Main against this branch, same machine and checkpoint, posted before asking for a run on our machines (or why you can't): measured here, not inferred. Aggregate decode tok/s at 8 streams, chat 257.9 and 255.9 on main against 262.0 and 262.2 on =1; code 373.9 and 369.0 against 379.3 and 379.8. The mean gap against main by stream count on =1 is +0.34% at 1, +0.68% at 2, +1.32% at 4, +1.77% at 6 and +2.10% at 8, and =0 is 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.
  • Decode speed before and after, from tools/bench_openai.py: 73.9/72.1 tok/s on main against 72.1/73.0 on =1 for 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.
  • Prompt speed before and after: the cold first request's ttft_s from tools/bench_cache.py --require-cold on 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-token bench_cache prompt 1.230/1.222 against 1.218/1.219 s, every first request reporting 0 cached tokens.

Checklist

  • The title and description match the head commit, including the stack and the checks.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency (they need no device and no checkpoint).
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.
  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off. (Nothing in this change touches a lane round; the window measured 2.510 to 5.224 tokens a round with identical drafted and accepted counters on all six arms, and with DRAFTS=0 the two arms' replies are identical, 90 of 90 hashes.)
  • Drafted output equals "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.)
  • No precision traded for speed. No sum's order or type changes: the same one-row dequantization and the same group sums, on the same rows.
  • Decode and prompt processing are no slower than main at one stream and the widest admitted; with --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.)

@ashhart

ashhart commented Nov 19, 2026

Copy link
Copy Markdown
Owner

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.

  • Synthena

@ashhart
ashhart force-pushed the flashnext-gather-early-up branch from 9d60e51 to 8345227 Compare November 20, 2026 07:41
@AIFlowML

Copy link
Copy Markdown
Contributor

@BadAd84 @ashhart I independently checked 834522789 on M5 Max, Zig 0.17.0. zig build test-environment-host test-gradient-serving test-qwen-serving-host -Dcpu=apple_m1 --summary all passed all 28 tests. A temporary local build alias for the existing pooled-gather tests also passed 29/29, covering the NVFP4 and MLX byte comparisons and the dispatch boundaries.

Since the review above, main 1ece6e6db has moved. My test merge conflicted only in the generated pins for cuda_batch.zig and cuda_batch_state.zig. Regenerating both from the merged sources made zig build test-gradient-serving test-qwen-serving-host -Dcpu=apple_m1 --summary all pass 3/3 on that disposable merge. The GB10 shared-pass A/B remains separate.

Igor Lessio - AIFLOW LABS

BadAd84 and others added 6 commits November 21, 2026 21:00
…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.
@ashhart
ashhart force-pushed the flashnext-gather-early-up branch from 576fac4 to ee71cd9 Compare November 21, 2026 21:00
@ashhart
ashhart merged commit 69e4d0f into ashhart:main Nov 21, 2026
1 check failed
@ashhart

ashhart commented Nov 21, 2026

Copy link
Copy Markdown
Owner

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.

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.

3 participants