diff --git a/CHANGELOG.md b/CHANGELOG.md index f4037e287b2..84648c7224b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel ## Unreleased +- Flash Next on CUDA: `TF_FLASHNEXT_GATHER_EARLY=1` (default `0`) gathers a shared decode pass's n-gram rows for every lane in one pooled call instead of one call a stream, for passes of at most 64 lanes (wider passes keep the per-stream call); which rows are read and how a row is dequantized do not change, so replies stay main's, and the measured mean decode gain on one GB10 rises to +2.10% at eight streams (#4009). Thanks to @BadAd84. + ## 1.3.3 - Kernel contract: the HIP relu² stream tile is checked at 5, 6 and 8 bits in groups of 64 as well as 4, with gates 1 to 4, a planted control for each wider width, and gate-5 records on the four RDNA chips (#3163, part of #3061). Thanks to @gilby. diff --git a/docs/environment.md b/docs/environment.md index e2511ca136e..60f962676ae 100644 --- a/docs/environment.md +++ b/docs/environment.md @@ -133,7 +133,7 @@ Direct family readers also refuse malformed values when called without the serve ## Flash Next -Flash Next checks these 35 settings when its engine loads, before any device or checkpoint work: a malformed value +Flash Next checks these 36 settings when its engine loads, before any device or checkpoint work: a malformed value stops the start with the variable, the supplied text and the accepted forms. | Variable | Accepted explicit value | @@ -159,6 +159,7 @@ stops the start with the variable, the supplied text and the accepted forms. | `TF_FLASHNEXT_ATTN_LANES` | Exactly `0` or `1`; default off | | `TF_FLASHNEXT_WARM_AHEAD` | Exactly `0` or `1`; default off | | `TF_FLASHNEXT_TABLE_WARM` | Exactly `0` or `1`; default off | +| `TF_FLASHNEXT_GATHER_EARLY` | Exactly `0` or `1`; default off. `1` gathers a shared pass's n-gram rows for every stream in one pool call, so the pass shares one distinct-row read and one set of parked readers; the rows, their order and every reply are the per-stream call's | | `TF_FLASHNEXT_PROFILE` | Exactly `0` or `1`; default off | | `TF_FLASHNEXT_VERIFY_HOST_LOGITS` | Exactly `0` or `1`; default off | | `TF_FLASHNEXT_DRAFTS` | Whole draft count; default 6, then clamped to 1 through 15 | diff --git a/docs/receipts/flashnext-gather-early/README.md b/docs/receipts/flashnext-gather-early/README.md new file mode 100644 index 00000000000..14124bf6ce1 --- /dev/null +++ b/docs/receipts/flashnext-gather-early/README.md @@ -0,0 +1,178 @@ +# Flash Next pooled n-gram gather: main against the branch + +`TF_FLASHNEXT_GATHER_EARLY`, default `0`, has a decode round's shared pass gather every stream's n-gram rows in one pool call instead of one call a stream. Unset or `0` is main's per-stream call. + +## Source and environment + +- Main: `077998f7d9b14adebec3a2acc2caa47ff7ae6ec7`. Branch: this pull request at `05e319fca3dcef27d12cdd76373b276765e1c9d7`, one commit on that main. +- Each image built from a verified `git archive` tree of its own commit (checked blob by blob against its commit's `git ls-tree`), the recipe's Zig base image with `tensorfold-native`, its tools and the fatbins. +- One NVIDIA GB10 (sm_121), Zig 0.17.0, CUDA 13.3, one model process. +- Checkpoint `nvidia/Qwen3.8-Flash-Next-FP4-Local` at revision `b9ce18a6887e18c089fdea8509a33e63429a3300`, one checkpoint for every arm. +- A fresh server per arm through the MiaAI-Lab recipe's `start.sh`: `CHECKPOINT=nvfp4`, `VISION=0`, `TENSORFOLD_CUDA_KERNELS=native`, `KV_DTYPE=bf16`, `--context 32768 --parallel 8` on the decode arms, bf16 KV, drafts on. +- The recipe's `scripts/config.sh` exports `TF_FLASHNEXT_GATHER_EARLY=1`, so the branch's off arm sets it to `0` explicitly and main's arm carries the variable and ignores it (main has no such row). The container's own environment is recorded per boot in each arm's `env.txt`. +- Arms, in order (two boots an arm, alternating): M1, B0a, B1a, B1b, B0b, M2. M is main, B0 the branch with the switch off, B1 the branch with it on. + +## Cells + +- Decode, cache on: `tools/bench_openai.py --tokens 256 --reps 3 --temperatures 0` at one stream and `tools/bench_concurrent.py --tokens 256 --reps 3 --levels 1,2,4,6,8 --alone --serial --temperatures 0` (code and chat prompts), from main's own `tools/` for every arm. +- Cold prompts, cache off (`--prompt-cache-gib 0`, 131,072-token window, 4 slots): 2k, 8k, 32k and 64k tokens at one stream, three salted prompts a size, thinking off, and `tools/bench_cache.py --require-cold`. +- Exactness: the six golden prompts at 1 and 4 streams `--alone --serial` greedy, `served_exactness.py drafts` and `turns` with the cache on, `served_exactness.py fresh` and `drafts` with it off, and a cancel probe on the server's own cancel route. +- Drafts off (`DRAFTS=0`): `bench_concurrent --levels 1,2,4,8 --alone` greedy. + +## Decode speed and tokens a round + +Aggregate tok/s; tokens a round is the tools' tokens a streamed chunk (a round sends its accepted drafts and one verified token). Change and verdict compare the branch's two runs against main's two runs by the noise rule below; `noise-rule.md` has every cell of every arm and the rerun pairs, `cells.csv` has them as data. + +| streams | cells | B0 vs M mean | verdicts | B1 vs M mean | verdicts | +|---|---|---|---|---|---| +| 1 | 2 | -0.52% | 0 faster, 2 level, 0 slower | +0.34% | 0 faster, 2 level, 0 slower | +| 2 | 2 | -0.42% | 0 faster, 2 level, 0 slower | +0.68% | 0 faster, 2 level, 0 slower | +| 4 | 2 | -0.40% | 0 faster, 2 level, 0 slower | +1.32% | 2 faster, 0 level, 0 slower | +| 6 | 2 | -0.12% | 0 faster, 2 level, 0 slower | +1.77% | 2 faster, 0 level, 0 slower | +| 8 | 2 | -0.46% | 0 faster, 2 level, 0 slower | +2.10% | 2 faster, 0 level, 0 slower | + +Verdicts (first pair): B0 vs M: 0 faster, 12 level, 0 slower; B1 vs M: 6 faster, 6 level, 0 slower; B1 vs B0: 7 faster, 5 level, 0 slower +Rerun (one per flagged cell; a repeat of slower is a loss): +Losses: 0 + +## Cold prompts + +Seconds to the first token; a fresh salt opens every prompt, so every first request reports 0 cached tokens. + +``` +| prompt | MC1 | MC2 | B0C1 | B0C2 | B1C1 | B1C2 | B0 vs M | B1 vs M | B1 vs B0 | +|---|---|---|---|---|---|---|---|---|---| +| cold2k-off | 1.093 | 1.072 | 1.078 | 1.076 | 1.091 | 1.067 | +0.5% level | +0.3% level | -0.2% level | +| cold8k-off | 4.051 | 3.967 | 3.989 | 3.980 | 3.981 | 4.001 | +0.6% level | +0.4% level | -0.2% level | +| cold32k-off | 16.248 | 15.924 | 16.018 | 16.052 | 15.992 | 16.003 | +0.3% level | +0.6% level | +0.2% level | +| cold64k-off | 33.098 | 32.902 | 32.926 | 33.028 | 32.928 | 32.890 | +0.1% level | +0.3% level | +0.2% level | +| first_ttft_s | 1.230 | 1.222 | 1.225 | 1.227 | 1.218 | 1.219 | -0.0% level | +0.6% level | +0.7% level | +- B0C1: first ttft 1.2254346199333668 s, 2571 prompt tokens, cached 0 (cold), qualified True +- B0C2: first ttft 1.2271071821451187 s, 2571 prompt tokens, cached 0 (cold), qualified True +- B1C1: first ttft 1.2175115970894694 s, 2571 prompt tokens, cached 0 (cold), qualified True +- B1C2: first ttft 1.2187956660054624 s, 2571 prompt tokens, cached 0 (cold), qualified True +- MC1: first ttft 1.2304023278411478 s, 2571 prompt tokens, cached 0 (cold), qualified True +- MC2: first ttft 1.22166123916395 s, 2571 prompt tokens, cached 0 (cold), qualified True + +``` + +## Exactness + +``` +| contract | M | B0 | B1 | +|---|---|---|---| +| concurrent equals solo (pass A, 1 to 8 streams, greedy) | 252/252 equal | 252/252 equal | 252/252 equal | +| drafted equals `"draft": false` (pass A, 1 to 8 streams, greedy) | 20/20 equal | 20/20 equal | 20/20 equal | +| resumed equals fresh (pass C, 7 chats, cache on against cache off) | 7/7 equal | 7/7 equal | 7/7 equal | +| cancel and reset leave the next request valid (pass C, the cancel route) | 3/3 contracts | 3/3 contracts | 3/3 contracts | +| every greedy reply equals main's (token_sha over pass A's cells), B0 | 528/528 equal | | | +| every greedy reply equals main's (token_sha over pass A's cells), B1 | 528/528 equal | | | + +- B0 cache on golden (6 prompts, 1 and 4 streams): concurrent equals solo 5/0; drafted equals `"draft": false` 8/0 +- B0 cache on drafts: 8 of 8 equal to `"draft": false` (basis token_ids) +- B0 cache on turns: 7 chats, resumed from cached prompt tokens [13, 16, 16, 13, 24, 18, 3406] +- B0 cache on cancel: 3 of 3 contracts, reply after the cancel True equal to the reference (token_sha256) +- B0 cache off drafts: 8 of 8 equal to `"draft": false` (basis token_ids) +- B0 cache off fresh: 7 of 7 resumed replies equal the fresh ones +- B1 cache on golden (6 prompts, 1 and 4 streams): concurrent equals solo 5/0; drafted equals `"draft": false` 8/0 +- B1 cache on drafts: 8 of 8 equal to `"draft": false` (basis token_ids) +- B1 cache on turns: 7 chats, resumed from cached prompt tokens [13, 16, 16, 13, 24, 18, 3406] +- B1 cache on cancel: 3 of 3 contracts, reply after the cancel True equal to the reference (token_sha256) +- B1 cache off drafts: 8 of 8 equal to `"draft": false` (basis token_ids) +- B1 cache off fresh: 7 of 7 resumed replies equal the fresh ones +- M cache on golden (6 prompts, 1 and 4 streams): concurrent equals solo 5/0; drafted equals `"draft": false` 8/0 +- M cache on drafts: 8 of 8 equal to `"draft": false` (basis token_ids) +- M cache on turns: 7 chats, resumed from cached prompt tokens [13, 16, 16, 13, 24, 18, 3406] +- M cache on cancel: 3 of 3 contracts, reply after the cancel True equal to the reference (token_sha256) +- M cache off drafts: 8 of 8 equal to `"draft": false` (basis token_ids) +- M cache off fresh: 7 of 7 resumed replies equal the fresh ones +- B0: replies equal over pass A's greedy cells (token_sha): 528 equal, 0 unequal +- B1: replies equal over pass A's greedy cells (token_sha): 528 equal, 0 unequal +- B1 against B0: replies equal over pass A's greedy cells (token_sha): 528 equal, 0 unequal + +``` + +## Pool line, boot time and server acceptance + +``` +| arm | image | ctx/par | wall s | answered s | gpu at boot (C, MHz, W) | mem GiB | pool line | +|---|---|---|---|---|---|---|---| +| 1/M1 | tensorfold-qwen38:p1-m-077998f7 | 32768/8 | 34.0 | 33 | 42,2463MHz,13.78W | 35 | 8 streams at once, 1.29 GiB each at a 32768-token window, of 27.8 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, a 16.0 GiB prompt cache; own glue kernels | +| 1/B0a | tensorfold-qwen38:p1-b-05e319fc | 32768/8 | 34.0 | 33 | 53,2450MHz,15.13W | 34 | 8 streams at once, 1.29 GiB each at a 32768-token window, of 27.9 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, a 16.0 GiB prompt cache; own glue kernels | +| 1/B1a | tensorfold-qwen38:p1-b-05e319fc | 32768/8 | 34.0 | 33 | 57,2450MHz,15.69W | 33 | 8 streams at once, 1.29 GiB each at a 32768-token window, of 27.8 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, a 16.0 GiB prompt cache; own glue kernels | +| 1/B1b | tensorfold-qwen38:p1-b-05e319fc | 32768/8 | 34.1 | 34 | 57,2411MHz,15.71W | 33 | 8 streams at once, 1.29 GiB each at a 32768-token window, of 27.9 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, a 16.0 GiB prompt cache; own glue kernels | +| 1/B0b | tensorfold-qwen38:p1-b-05e319fc | 32768/8 | 34.0 | 34 | 59,2411MHz,15.62W | 33 | 8 streams at once, 1.29 GiB each at a 32768-token window, of 27.9 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, a 16.0 GiB prompt cache; own glue kernels | +| 1/M2 | tensorfold-qwen38:p1-m-077998f7 | 32768/8 | 34.0 | 33 | 58,2411MHz,15.76W | 33 | 8 streams at once, 1.29 GiB each at a 32768-token window, of 27.8 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, a 16.0 GiB prompt cache; own glue kernels | +| 2/MC1 | tensorfold-qwen38:p1-m-077998f7 | 131072/4 | 34.0 | 34 | 61,2411MHz,15.95W | 34 | 4 streams at once, 4.11 GiB each at a 131072-token window, of 27.8 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, no prompt cache; own glue kernels | +| 2/B0C1 | tensorfold-qwen38:p1-b-05e319fc | 131072/4 | 34.0 | 33 | 62,2411MHz,16.47W | 33 | 4 streams at once, 4.11 GiB each at a 131072-token window, of 27.8 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, no prompt cache; own glue kernels | +| 2/B1C1 | tensorfold-qwen38:p1-b-05e319fc | 131072/4 | 34.0 | 33 | 64,2411MHz,16.43W | 34 | 4 streams at once, 4.11 GiB each at a 131072-token window, of 27.9 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, no prompt cache; own glue kernels | +| 2/B1C2 | tensorfold-qwen38:p1-b-05e319fc | 131072/4 | 34.0 | 33 | 63,2450MHz,16.55W | 34 | 4 streams at once, 4.11 GiB each at a 131072-token window, of 26.9 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, no prompt cache; own glue kernels | +| 2/B0C2 | tensorfold-qwen38:p1-b-05e319fc | 131072/4 | 34.0 | 34 | 64,2431MHz,16.46W | 33 | 4 streams at once, 4.11 GiB each at a 131072-token window, of 27.9 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, no prompt cache; own glue kernels | +| 2/MC2 | tensorfold-qwen38:p1-m-077998f7 | 131072/4 | 34.0 | 34 | 64,2411MHz,16.37W | 36 | 4 streams at once, 4.11 GiB each at a 131072-token window, of 28.0 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, no prompt cache; own glue kernels | +| 3/M/on | tensorfold-qwen38:p1-m-077998f7 | 32768/8 | 34.0 | 33 | 64,2411MHz,16.45W | 33 | 8 streams at once, 1.29 GiB each at a 32768-token window, of 27.9 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, a 16.0 GiB prompt cache; own glue kernels | +| 3/M/off | tensorfold-qwen38:p1-m-077998f7 | 32768/8 | 34.0 | 33 | 56,2431MHz,15.54W | 34 | 8 streams at once, 1.29 GiB each at a 32768-token window, of 27.9 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, no prompt cache; own glue kernels | +| 3/B0/on | tensorfold-qwen38:p1-b-05e319fc | 32768/8 | 34.0 | 34 | 55,2424MHz,15.38W | 32 | 8 streams at once, 1.29 GiB each at a 32768-token window, of 27.9 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, a 16.0 GiB prompt cache; own glue kernels | +| 3/B0/off | tensorfold-qwen38:p1-b-05e319fc | 32768/8 | 34.0 | 34 | 54,2437MHz,15.03W | 33 | 8 streams at once, 1.29 GiB each at a 32768-token window, of 27.9 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, no prompt cache; own glue kernels | +| 3/B1/on | tensorfold-qwen38:p1-b-05e319fc | 32768/8 | 34.0 | 34 | 54,2405MHz,15.17W | 34 | 8 streams at once, 1.29 GiB each at a 32768-token window, of 27.9 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, a 16.0 GiB prompt cache; own glue kernels | +| 3/B1/off | tensorfold-qwen38:p1-b-05e319fc | 32768/8 | 34.0 | 33 | 54,2405MHz,15.07W | 33 | 8 streams at once, 1.29 GiB each at a 32768-token window, of 27.9 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, no prompt cache; own glue kernels | +| 4/MD | tensorfold-qwen38:p1-m-077998f7 | 32768/8 | 31.0 | 31 | 53,2405MHz,14.65W | 34 | 8 streams at once, 0.98 GiB each at a 32768-token window, of 30.1 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, a 16.0 GiB prompt cache; own glue kernels | +| 4/B1D | tensorfold-qwen38:p1-b-05e319fc | 32768/8 | 30.9 | 30 | 53,2437MHz,14.65W | 35 | 8 streams at once, 0.98 GiB each at a 32768-token window, of 30.1 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, a 16.0 GiB prompt cache; own glue kernels | + +``` + +``` +| arm | invocation | accepted | drafted | acceptance | alone equal/unequal | serial equal/unequal | failed | unmeasured | +|---|---|---|---|---|---|---|---|---| +| M1 | openai_t0 | 1488 | 2384 | 0.6242 | - | - | 0 | 0 | +| M1 | conc_t0 | 22760 | 33060 | 0.6884 | 24/0 | 1/0 | 0 | 0 | +| B0a | openai_t0 | 1488 | 2384 | 0.6242 | - | - | 0 | 0 | +| B0a | conc_t0 | 22760 | 33060 | 0.6884 | 24/0 | 1/0 | 0 | 0 | +| B1a | openai_t0 | 1488 | 2384 | 0.6242 | - | - | 0 | 0 | +| B1a | conc_t0 | 22760 | 33060 | 0.6884 | 24/0 | 1/0 | 0 | 0 | +| B1b | openai_t0 | 1488 | 2384 | 0.6242 | - | - | 0 | 0 | +| B1b | conc_t0 | 22760 | 33060 | 0.6884 | 24/0 | 1/0 | 0 | 0 | +| B0b | openai_t0 | 1488 | 2384 | 0.6242 | - | - | 0 | 0 | +| B0b | conc_t0 | 22760 | 33060 | 0.6884 | 24/0 | 1/0 | 0 | 0 | +| M2 | openai_t0 | 1488 | 2384 | 0.6242 | - | - | 0 | 0 | +| M2 | conc_t0 | 22760 | 33060 | 0.6884 | 24/0 | 1/0 | 0 | 0 | + +``` + +## Method and the noise rule + +- A cell pairs each arm's two runs (mean): it is slower when both branch runs are under both main runs and the mean gap is over 1%, faster when both are over both and the gap is over 1%, level otherwise; for a latency the same rule with higher meaning slower. A cell slower in the first pair gets one rerun of its invocation on fresh boots an arm, and a repeat of slower is a loss. +- Every probe records `/metrics` (`mtp_drafted_total`, `mtp_accepted_total`) before and after itself and the server's own per-path request counts; an arm that saw a request it did not send, or whose boot or a probe failed, is rerun once on a fresh boot with its first attempt kept as `.rejected1`. +- The GPU lock was held for the whole window, the production serve was stopped inside it and restored from the runner's EXIT trap with `/health` checked, and the fleet dashboard's poller was stopped before the window and started after it. + +## Evidence + +- IMAGE p1-m-077998f7 sha256:d00a7156c897a07e09c7f5d29e81e094792e6130ff598e13dd45d5ad925cab65 created=2026-11-16T20:37:06.583481326Z kernels=embedded +- IMAGE p1-b-05e319fc sha256:c28e46c9ebf6379f53c543803bf19273b0cd602d7366b970d4c3ca555dd75aad created=2026-11-16T23:06:14.411234398Z kernels=embedded +- POOL 1/M1 8 streams at once, 1.29 GiB each at a 32768-token window, of 27.8 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, a 16.0 GiB prompt cache; own glue kernels +- POOL 1/B0a 8 streams at once, 1.29 GiB each at a 32768-token window, of 27.9 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, a 16.0 GiB prompt cache; own glue kernels +- POOL 1/B1a 8 streams at once, 1.29 GiB each at a 32768-token window, of 27.8 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, a 16.0 GiB prompt cache; own glue kernels +- POOL 1/B1b 8 streams at once, 1.29 GiB each at a 32768-token window, of 27.9 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, a 16.0 GiB prompt cache; own glue kernels +- POOL 1/B0b 8 streams at once, 1.29 GiB each at a 32768-token window, of 27.9 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, a 16.0 GiB prompt cache; own glue kernels +- POOL 1/M2 8 streams at once, 1.29 GiB each at a 32768-token window, of 27.8 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, a 16.0 GiB prompt cache; own glue kernels +- POOL 2/MC1 4 streams at once, 4.11 GiB each at a 131072-token window, of 27.8 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, no prompt cache; own glue kernels +- POOL 2/B0C1 4 streams at once, 4.11 GiB each at a 131072-token window, of 27.8 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, no prompt cache; own glue kernels +- POOL 2/B1C1 4 streams at once, 4.11 GiB each at a 131072-token window, of 27.9 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, no prompt cache; own glue kernels +- POOL 2/B1C2 4 streams at once, 4.11 GiB each at a 131072-token window, of 26.9 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, no prompt cache; own glue kernels +- POOL 2/B0C2 4 streams at once, 4.11 GiB each at a 131072-token window, of 27.9 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, no prompt cache; own glue kernels +- POOL 2/MC2 4 streams at once, 4.11 GiB each at a 131072-token window, of 28.0 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, no prompt cache; own glue kernels +- POOL 3/M/on 8 streams at once, 1.29 GiB each at a 32768-token window, of 27.9 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, a 16.0 GiB prompt cache; own glue kernels +- POOL 3/M/off 8 streams at once, 1.29 GiB each at a 32768-token window, of 27.9 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, no prompt cache; own glue kernels +- POOL 3/B0/on 8 streams at once, 1.29 GiB each at a 32768-token window, of 27.9 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, a 16.0 GiB prompt cache; own glue kernels +- POOL 3/B0/off 8 streams at once, 1.29 GiB each at a 32768-token window, of 27.9 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, no prompt cache; own glue kernels +- POOL 3/B1/on 8 streams at once, 1.29 GiB each at a 32768-token window, of 27.9 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, a 16.0 GiB prompt cache; own glue kernels +- POOL 3/B1/off 8 streams at once, 1.29 GiB each at a 32768-token window, of 27.9 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, no prompt cache; own glue kernels +- POOL 4/MD 8 streams at once, 0.98 GiB each at a 32768-token window, of 30.1 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, a 16.0 GiB prompt cache; own glue kernels +- POOL 4/B1D 8 streams at once, 0.98 GiB each at a 32768-token window, of 30.1 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, a 16.0 GiB prompt cache; own glue kernels +- MEMLINE 1/M1 model 72.71 GiB; 8 streams at once, 1.29 GiB each at a 32768-token window, of 27.8 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, a 16.0 GiB prompt cache; own glue kernels +- MEMLINE 1/B0a model 72.71 GiB; 8 streams at once, 1.29 GiB each at a 32768-token window, of 27.9 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, a 16.0 GiB prompt cache; own glue kernels +- MEMLINE 1/B1a model 72.71 GiB; 8 streams at once, 1.29 GiB each at a 32768-token window, of 27.8 GiB left after a 9.0 GiB reserve; prompts in passes of up to 1536 rows, a 16.0 GiB prompt cache; own glue kernels +- RESTORE prod_rc 0 /health 200 2026-11-17T00:29:58+00:00 +- FOREIGN-SUMMARY 1/B0a=0 1/B0b=0 1/B1a=0 1/B1b=0 1/M1=0 1/M2=0 2/B0C1=0 2/B0C2=0 2/B1C1=0 2/B1C2=0 2/MC1=0 2/MC2=0 3/B0/off=0 3/B0/on=0 3/B1/off=0 3/B1/on=0 3/M/off=0 3/M/on=0 4/B1D=0 4/MD=0 +- DONE 2026-11-17T00:28:27+00:00 + +Every arm's JSON, boot log, `server.log`, `gets.txt`, `metrics.txt`, `gpu.txt` and `env.txt` are kept with the run, and `cells.csv` has every cell of every arm. diff --git a/docs/receipts/flashnext-gather-early/cells.csv b/docs/receipts/flashnext-gather-early/cells.csv new file mode 100644 index 00000000000..013b4f8077f --- /dev/null +++ b/docs/receipts/flashnext-gather-early/cells.csv @@ -0,0 +1,73 @@ +arm,invocation,prompt,temperature,streams,aggregate_tps,per_stream_tps,tokens_per_round,accepted_per_round,server_accepted,server_drafted,server_acceptance,alone_equal,alone_unequal,serial_equal,serial_unequal,failed,unmeasured +M1,conc_t0,chat,0.0,1,46.5,46.5,2.813,1.813,22760,33060,0.6884,3,0,1,0,0,0 +M1,conc_t0,chat,0.0,2,86.6,43.5,2.51,1.51,22760,33060,0.6884,6,0,1,0,0,0 +M1,conc_t0,chat,0.0,4,156.6,39.7,2.51,1.51,22760,33060,0.6884,12,0,1,0,0,0 +M1,conc_t0,chat,0.0,6,210.7,35.9,2.51,1.51,22760,33060,0.6884,18,0,1,0,0,0 +M1,conc_t0,chat,0.0,8,257.9,33.1,2.51,1.51,22760,33060,0.6884,24,0,1,0,0,0 +M1,conc_t0,code,0.0,1,73.5,73.5,5.224,4.224,22760,33060,0.6884,3,0,1,0,0,0 +M1,conc_t0,code,0.0,2,139.2,70.2,4.83,3.83,22760,33060,0.6884,6,0,1,0,0,0 +M1,conc_t0,code,0.0,4,243.7,62.5,4.83,3.83,22760,33060,0.6884,12,0,1,0,0,0 +M1,conc_t0,code,0.0,6,307.5,52.9,4.491,3.491,22760,33060,0.6884,18,0,1,0,0,0 +M1,conc_t0,code,0.0,8,373.9,49.1,4.491,3.491,22760,33060,0.6884,24,0,1,0,0,0 +M1,openai_t0,fibonacci-raw,0.0,1,73.9407779779337,73.9407779779337,5.224,4.224,1488,2384,0.6242,,,,,0,0 +M1,openai_t0,gpu-chat-no-think,0.0,1,47.29243959209471,47.29243959209471,2.813,1.813,1488,2384,0.6242,,,,,0,0 +B0a,conc_t0,chat,0.0,1,45.9,45.9,2.813,1.813,22760,33060,0.6884,3,0,1,0,0,0 +B0a,conc_t0,chat,0.0,2,85.7,43.1,2.51,1.51,22760,33060,0.6884,6,0,1,0,0,0 +B0a,conc_t0,chat,0.0,4,154.7,39.3,2.51,1.51,22760,33060,0.6884,12,0,1,0,0,0 +B0a,conc_t0,chat,0.0,6,208.6,35.6,2.51,1.51,22760,33060,0.6884,18,0,1,0,0,0 +B0a,conc_t0,chat,0.0,8,255.3,32.9,2.51,1.51,22760,33060,0.6884,24,0,1,0,0,0 +B0a,conc_t0,code,0.0,1,71.9,71.9,5.224,4.224,22760,33060,0.6884,3,0,1,0,0,0 +B0a,conc_t0,code,0.0,2,137.2,69.1,4.83,3.83,22760,33060,0.6884,6,0,1,0,0,0 +B0a,conc_t0,code,0.0,4,239.8,61.8,4.83,3.83,22760,33060,0.6884,12,0,1,0,0,0 +B0a,conc_t0,code,0.0,6,304.4,52.7,4.491,3.491,22760,33060,0.6884,18,0,1,0,0,0 +B0a,conc_t0,code,0.0,8,369.1,48.5,4.491,3.491,22760,33060,0.6884,24,0,1,0,0,0 +B0a,openai_t0,fibonacci-raw,0.0,1,71.81520376837803,71.81520376837803,5.224,4.224,1488,2384,0.6242,,,,,0,0 +B0a,openai_t0,gpu-chat-no-think,0.0,1,46.038164951481015,46.038164951481015,2.813,1.813,1488,2384,0.6242,,,,,0,0 +B1a,conc_t0,chat,0.0,1,46.7,46.7,2.813,1.813,22760,33060,0.6884,3,0,1,0,0,0 +B1a,conc_t0,chat,0.0,2,87.0,43.8,2.51,1.51,22760,33060,0.6884,6,0,1,0,0,0 +B1a,conc_t0,chat,0.0,4,158.0,40.0,2.51,1.51,22760,33060,0.6884,12,0,1,0,0,0 +B1a,conc_t0,chat,0.0,6,213.7,36.5,2.51,1.51,22760,33060,0.6884,18,0,1,0,0,0 +B1a,conc_t0,chat,0.0,8,262.0,33.8,2.51,1.51,22760,33060,0.6884,24,0,1,0,0,0 +B1a,conc_t0,code,0.0,1,72.9,72.9,5.224,4.224,22760,33060,0.6884,3,0,1,0,0,0 +B1a,conc_t0,code,0.0,2,139.0,70.0,4.83,3.83,22760,33060,0.6884,6,0,1,0,0,0 +B1a,conc_t0,code,0.0,4,245.5,63.0,4.83,3.83,22760,33060,0.6884,12,0,1,0,0,0 +B1a,conc_t0,code,0.0,6,312.1,54.1,4.491,3.491,22760,33060,0.6884,18,0,1,0,0,0 +B1a,conc_t0,code,0.0,8,379.3,49.9,4.491,3.491,22760,33060,0.6884,24,0,1,0,0,0 +B1a,openai_t0,fibonacci-raw,0.0,1,72.0827562664294,72.0827562664294,5.224,4.224,1488,2384,0.6242,,,,,0,0 +B1a,openai_t0,gpu-chat-no-think,0.0,1,46.10521318194038,46.10521318194038,2.813,1.813,1488,2384,0.6242,,,,,0,0 +B1b,conc_t0,chat,0.0,1,46.7,46.7,2.813,1.813,22760,33060,0.6884,3,0,1,0,0,0 +B1b,conc_t0,chat,0.0,2,87.1,43.8,2.51,1.51,22760,33060,0.6884,6,0,1,0,0,0 +B1b,conc_t0,chat,0.0,4,158.2,40.1,2.51,1.51,22760,33060,0.6884,12,0,1,0,0,0 +B1b,conc_t0,chat,0.0,6,213.6,36.5,2.51,1.51,22760,33060,0.6884,18,0,1,0,0,0 +B1b,conc_t0,chat,0.0,8,262.2,33.9,2.51,1.51,22760,33060,0.6884,24,0,1,0,0,0 +B1b,conc_t0,code,0.0,1,72.7,72.7,5.224,4.224,22760,33060,0.6884,3,0,1,0,0,0 +B1b,conc_t0,code,0.0,2,138.8,69.9,4.83,3.83,22760,33060,0.6884,6,0,1,0,0,0 +B1b,conc_t0,code,0.0,4,245.2,63.0,4.83,3.83,22760,33060,0.6884,12,0,1,0,0,0 +B1b,conc_t0,code,0.0,6,312.0,54.1,4.491,3.491,22760,33060,0.6884,18,0,1,0,0,0 +B1b,conc_t0,code,0.0,8,379.8,50.0,4.491,3.491,22760,33060,0.6884,24,0,1,0,0,0 +B1b,openai_t0,fibonacci-raw,0.0,1,72.96582501540956,72.96582501540956,5.224,4.224,1488,2384,0.6242,,,,,0,0 +B1b,openai_t0,gpu-chat-no-think,0.0,1,46.71976289621671,46.71976289621671,2.813,1.813,1488,2384,0.6242,,,,,0,0 +B0b,conc_t0,chat,0.0,1,46.6,46.6,2.813,1.813,22760,33060,0.6884,3,0,1,0,0,0 +B0b,conc_t0,chat,0.0,2,86.3,43.4,2.51,1.51,22760,33060,0.6884,6,0,1,0,0,0 +B0b,conc_t0,chat,0.0,4,156.4,39.6,2.51,1.51,22760,33060,0.6884,12,0,1,0,0,0 +B0b,conc_t0,chat,0.0,6,210.9,35.8,2.51,1.51,22760,33060,0.6884,18,0,1,0,0,0 +B0b,conc_t0,chat,0.0,8,255.1,32.9,2.51,1.51,22760,33060,0.6884,24,0,1,0,0,0 +B0b,conc_t0,code,0.0,1,72.6,72.6,5.224,4.224,22760,33060,0.6884,3,0,1,0,0,0 +B0b,conc_t0,code,0.0,2,137.9,69.7,4.83,3.83,22760,33060,0.6884,6,0,1,0,0,0 +B0b,conc_t0,code,0.0,4,242.1,62.2,4.83,3.83,22760,33060,0.6884,12,0,1,0,0,0 +B0b,conc_t0,code,0.0,6,307.8,53.3,4.491,3.491,22760,33060,0.6884,18,0,1,0,0,0 +B0b,conc_t0,code,0.0,8,371.9,49.0,4.491,3.491,22760,33060,0.6884,24,0,1,0,0,0 +B0b,openai_t0,fibonacci-raw,0.0,1,72.73186303148962,72.73186303148962,5.224,4.224,1488,2384,0.6242,,,,,0,0 +B0b,openai_t0,gpu-chat-no-think,0.0,1,46.631364348835525,46.631364348835525,2.813,1.813,1488,2384,0.6242,,,,,0,0 +M2,conc_t0,chat,0.0,1,46.2,46.2,2.813,1.813,22760,33060,0.6884,3,0,1,0,0,0 +M2,conc_t0,chat,0.0,2,86.1,43.2,2.51,1.51,22760,33060,0.6884,6,0,1,0,0,0 +M2,conc_t0,chat,0.0,4,155.4,39.5,2.51,1.51,22760,33060,0.6884,12,0,1,0,0,0 +M2,conc_t0,chat,0.0,6,209.3,35.8,2.51,1.51,22760,33060,0.6884,18,0,1,0,0,0 +M2,conc_t0,chat,0.0,8,255.9,33.0,2.51,1.51,22760,33060,0.6884,24,0,1,0,0,0 +M2,conc_t0,code,0.0,1,72.2,72.2,5.224,4.224,22760,33060,0.6884,3,0,1,0,0,0 +M2,conc_t0,code,0.0,2,137.1,69.2,4.83,3.83,22760,33060,0.6884,6,0,1,0,0,0 +M2,conc_t0,code,0.0,4,240.7,61.9,4.83,3.83,22760,33060,0.6884,12,0,1,0,0,0 +M2,conc_t0,code,0.0,6,305.5,52.8,4.491,3.491,22760,33060,0.6884,18,0,1,0,0,0 +M2,conc_t0,code,0.0,8,369.0,48.6,4.491,3.491,22760,33060,0.6884,24,0,1,0,0,0 +M2,openai_t0,fibonacci-raw,0.0,1,72.1171739425607,72.1171739425607,5.224,4.224,1488,2384,0.6242,,,,,0,0 +M2,openai_t0,gpu-chat-no-think,0.0,1,46.069790889775746,46.069790889775746,2.813,1.813,1488,2384,0.6242,,,,,0,0 diff --git a/docs/receipts/flashnext-gather-early/exactness.md b/docs/receipts/flashnext-gather-early/exactness.md new file mode 100644 index 00000000000..338ec92bd73 --- /dev/null +++ b/docs/receipts/flashnext-gather-early/exactness.md @@ -0,0 +1,30 @@ +| contract | M | B0 | B1 | +|---|---|---|---| +| concurrent equals solo (pass A, 1 to 8 streams, greedy) | 252/252 equal | 252/252 equal | 252/252 equal | +| drafted equals `"draft": false` (pass A, 1 to 8 streams, greedy) | 20/20 equal | 20/20 equal | 20/20 equal | +| resumed equals fresh (pass C, 7 chats, cache on against cache off) | 7/7 equal | 7/7 equal | 7/7 equal | +| cancel and reset leave the next request valid (pass C, the cancel route) | 3/3 contracts | 3/3 contracts | 3/3 contracts | +| every greedy reply equals main's (token_sha over pass A's cells), B0 | 528/528 equal | | | +| every greedy reply equals main's (token_sha over pass A's cells), B1 | 528/528 equal | | | + +- B0 cache on golden (6 prompts, 1 and 4 streams): concurrent equals solo 5/0; drafted equals `"draft": false` 8/0 +- B0 cache on drafts: 8 of 8 equal to `"draft": false` (basis token_ids) +- B0 cache on turns: 7 chats, resumed from cached prompt tokens [13, 16, 16, 13, 24, 18, 3406] +- B0 cache on cancel: 3 of 3 contracts, reply after the cancel True equal to the reference (token_sha256) +- B0 cache off drafts: 8 of 8 equal to `"draft": false` (basis token_ids) +- B0 cache off fresh: 7 of 7 resumed replies equal the fresh ones +- B1 cache on golden (6 prompts, 1 and 4 streams): concurrent equals solo 5/0; drafted equals `"draft": false` 8/0 +- B1 cache on drafts: 8 of 8 equal to `"draft": false` (basis token_ids) +- B1 cache on turns: 7 chats, resumed from cached prompt tokens [13, 16, 16, 13, 24, 18, 3406] +- B1 cache on cancel: 3 of 3 contracts, reply after the cancel True equal to the reference (token_sha256) +- B1 cache off drafts: 8 of 8 equal to `"draft": false` (basis token_ids) +- B1 cache off fresh: 7 of 7 resumed replies equal the fresh ones +- M cache on golden (6 prompts, 1 and 4 streams): concurrent equals solo 5/0; drafted equals `"draft": false` 8/0 +- M cache on drafts: 8 of 8 equal to `"draft": false` (basis token_ids) +- M cache on turns: 7 chats, resumed from cached prompt tokens [13, 16, 16, 13, 24, 18, 3406] +- M cache on cancel: 3 of 3 contracts, reply after the cancel True equal to the reference (token_sha256) +- M cache off drafts: 8 of 8 equal to `"draft": false` (basis token_ids) +- M cache off fresh: 7 of 7 resumed replies equal the fresh ones +- B0: replies equal over pass A's greedy cells (token_sha): 528 equal, 0 unequal +- B1: replies equal over pass A's greedy cells (token_sha): 528 equal, 0 unequal +- B1 against B0: replies equal over pass A's greedy cells (token_sha): 528 equal, 0 unequal diff --git a/docs/receipts/flashnext-gather-early/noise-rule.md b/docs/receipts/flashnext-gather-early/noise-rule.md new file mode 100644 index 00000000000..485406d5a63 --- /dev/null +++ b/docs/receipts/flashnext-gather-early/noise-rule.md @@ -0,0 +1,28 @@ +| invocation | prompt | streams | M1 | B0a | B1a | B1b | B0b | M2 | B0 vs M | B1 vs M | B1 vs B0 | tokens a round M / B0 / B1 | +|---|---|---|---|---|---|---|---|---|---|---|---|---| +| openai_t0 | fibonacci-raw | 1 | 73.9 | 71.8 | 72.1 | 73.0 | 72.7 | 72.1 | -1.0% level | -0.7% level | +0.3% level | 5.224 / 5.224 / 5.224 | +| openai_t0 | gpu-chat-no-think | 1 | 47.3 | 46.0 | 46.1 | 46.7 | 46.6 | 46.1 | -0.7% level | -0.6% level | +0.2% level | 2.813 / 2.813 / 2.813 | +| conc_t0 | chat | 1 | 46.5 | 45.9 | 46.7 | 46.7 | 46.6 | 46.2 | -0.2% level | +0.8% level | +1.0% level | 2.813 / 2.813 / 2.813 | +| conc_t0 | chat | 2 | 86.6 | 85.7 | 87.0 | 87.1 | 86.3 | 86.1 | -0.4% level | +0.8% level | +1.2% faster | 2.510 / 2.510 / 2.510 | +| conc_t0 | chat | 4 | 156.6 | 154.7 | 158.0 | 158.2 | 156.4 | 155.4 | -0.3% level | +1.3% faster | +1.6% faster | 2.510 / 2.510 / 2.510 | +| conc_t0 | chat | 6 | 210.7 | 208.6 | 213.7 | 213.6 | 210.9 | 209.3 | -0.1% level | +1.7% faster | +1.9% faster | 2.510 / 2.510 / 2.510 | +| conc_t0 | chat | 8 | 257.9 | 255.3 | 262.0 | 262.2 | 255.1 | 255.9 | -0.7% level | +2.0% faster | +2.7% faster | 2.510 / 2.510 / 2.510 | +| conc_t0 | code | 1 | 73.5 | 71.9 | 72.9 | 72.7 | 72.6 | 72.2 | -0.8% level | -0.1% level | +0.8% level | 5.224 / 5.224 / 5.224 | +| conc_t0 | code | 2 | 139.2 | 137.2 | 139.0 | 138.8 | 137.9 | 137.1 | -0.4% level | +0.5% level | +1.0% level | 4.830 / 4.830 / 4.830 | +| conc_t0 | code | 4 | 243.7 | 239.8 | 245.5 | 245.2 | 242.1 | 240.7 | -0.5% level | +1.3% faster | +1.8% faster | 4.830 / 4.830 / 4.830 | +| conc_t0 | code | 6 | 307.5 | 304.4 | 312.1 | 312.0 | 307.8 | 305.5 | -0.1% level | +1.8% faster | +1.9% faster | 4.491 / 4.491 / 4.491 | +| conc_t0 | code | 8 | 373.9 | 369.1 | 379.3 | 379.8 | 371.9 | 369.0 | -0.3% level | +2.2% faster | +2.4% faster | 4.491 / 4.491 / 4.491 | + +Verdicts (first pair): B0 vs M: 0 faster, 12 level, 0 slower; B1 vs M: 6 faster, 6 level, 0 slower; B1 vs B0: 7 faster, 5 level, 0 slower + +| streams | cells | B0 vs M mean | verdicts | B1 vs M mean | verdicts | +|---|---|---|---|---|---| +| 1 | 2 | -0.52% | 0 faster, 2 level, 0 slower | +0.34% | 0 faster, 2 level, 0 slower | +| 2 | 2 | -0.42% | 0 faster, 2 level, 0 slower | +0.68% | 0 faster, 2 level, 0 slower | +| 4 | 2 | -0.40% | 0 faster, 2 level, 0 slower | +1.32% | 2 faster, 0 level, 0 slower | +| 6 | 2 | -0.12% | 0 faster, 2 level, 0 slower | +1.77% | 2 faster, 0 level, 0 slower | +| 8 | 2 | -0.46% | 0 faster, 2 level, 0 slower | +2.10% | 2 faster, 0 level, 0 slower | + +Rerun (one per flagged cell; a repeat of slower is a loss): +No cell was slower in the first pair, so no rerun was taken. +Losses: 0 diff --git a/zig/src/families/flashnext/cuda_batch_state.zig b/zig/src/families/flashnext/cuda_batch_state.zig index 80d3d073b76..30f047ff104 100644 --- a/zig/src/families/flashnext/cuda_batch_state.zig +++ b/zig/src/families/flashnext/cuda_batch_state.zig @@ -5,6 +5,7 @@ const cuda = @import("cuda"); const gdn = @import("cuda_gdn.zig"); const ple = @import("cuda_ple.zig"); const wt = @import("cuda_prompt_wt.zig"); +const gather_lanes = @import("cuda_ple_gather_lanes.zig"); const tk = @import("cuda_tile_kernels.zig"); const load = @import("cuda_prompt_load.zig"); const shared = @import("cuda_batch_side.zig"); @@ -575,6 +576,7 @@ pub const Gather = struct { }; return; } + if (gather_lanes.early(g)) return; var row0: usize = 0; for (g.lanes) |l| { g.ngram.rows(l.seq.history[0..], g.ids[row0..][0..l.rows], g.emb[row0 * dims ..][0 .. l.rows * dims], g.xs[row0 * (dims / 32) ..][0 .. l.rows * (dims / 32)]) catch |err| { diff --git a/zig/src/families/flashnext/cuda_ple.zig b/zig/src/families/flashnext/cuda_ple.zig index 5252a4ca955..ba795b7b97e 100644 --- a/zig/src/families/flashnext/cuda_ple.zig +++ b/zig/src/families/flashnext/cuda_ple.zig @@ -494,13 +494,13 @@ pub const Ngram = struct { } /// One call's rows split into `parts` equal ranges, part i filling [i * per, (i + 1) * per). - const Split = struct { + pub const Split = struct { g: *const Ngram, ids: []const i64, out: []u16, per: usize, - fn part(ctx: *const anyopaque, i: usize) anyerror!void { + pub fn part(ctx: *const anyopaque, i: usize) anyerror!void { const s: *const Split = @ptrCast(@alignCast(ctx)); const lo = @min(s.ids.len, i * s.per); try s.g.fill(s.ids, s.out, lo, @min(s.ids.len, lo + s.per)); @@ -572,6 +572,7 @@ test "a fresh window hashes token 7 into sixteen rows" { } test { + _ = @import("cuda_ple_gather_lanes.zig"); _ = @import("cuda_ple_nvfp4_test.zig"); _ = @import("cuda_ple_pool.zig"); _ = @import("cuda_ple_pool_gather_test.zig"); diff --git a/zig/src/families/flashnext/cuda_ple_gather_lanes.zig b/zig/src/families/flashnext/cuda_ple_gather_lanes.zig new file mode 100644 index 00000000000..da641c2450a --- /dev/null +++ b/zig/src/families/flashnext/cuda_ple_gather_lanes.zig @@ -0,0 +1,301 @@ +//! The shared pass's n-gram gather: every lane's rows in one pool call, so the dedup set and the readers are shared. + +const std = @import("std"); +const ple = @import("cuda_ple.zig"); +const ple_pool = @import("cuda_ple_pool.zig"); +const wt = @import("cuda_prompt_wt.zig"); +const batch_state = @import("cuda_batch_state.zig"); + +const dims = wt.dims; + +/// The most lanes one pooled call takes: its parts and their histories sit on the gather's own stack. +pub const max_lanes: usize = 64; + +/// TF_FLASHNEXT_GATHER_EARLY=1: a shared pass gathers every lane's rows in one pool call. +pub fn gatherEarly() bool { + // the startup check refused every malformed value already, so a plain default stands here + return @import("sys").environment.read(bool, "TF_FLASHNEXT_GATHER_EARLY", false) catch false; +} + +/// The switch and the lane bound: one lane, or 65 and more, keeps the pass's own `rows` call a stream. +pub fn earlyLanes(on: bool, lanes: usize) bool { + return on and lanes >= 1 and lanes <= max_lanes; +} + +/// One stream's rows in a shared gather: its history (moved past the rows), its tokens, and its slices of out and xs. +pub const Part = struct { history: []i64, tokens: []const u32, out: []u16, xs: []f32 }; + +/// A shared pass's lanes gathered in one pool call when the switch says so; sets failed. +pub fn early(g: *batch_state.Gather) bool { + return earlyOn(g, gatherEarly()); +} + +/// `early` with the switch given, so a test drives the lanes without the process's environment. +pub fn earlyOn(g: *batch_state.Gather, on: bool) bool { + if (!earlyLanes(on, g.lanes.len)) return false; + var parts: [max_lanes]Part = undefined; + var at: usize = 0; + // each stream's ids come from its own history, so every row holds the bits its own `rows` call writes + for (g.lanes, 0..) |l, k| { + parts[k] = .{ .history = l.seq.history[0..], .tokens = g.ids[at..][0..l.rows], .out = g.emb[at * dims ..][0 .. l.rows * dims], .xs = g.xs[at * (dims / 32) ..][0 .. l.rows * (dims / 32)] }; + at += l.rows; + } + rowsMany(g.ngram, parts[0..g.lanes.len]) catch |err| { + g.failed = err; + }; + return true; +} + +/// `Ngram.rows` over several streams' rows in one pool call; each stream's rows follow the one before it. +pub fn rowsMany(g: *const ple.Ngram, parts: []const Part) !void { + const heads = g.hash.heads; + const w = g.width(); + if (parts.len == 0 or parts.len > max_lanes) return error.UnexpectedTensor; + var total: usize = 0; + for (parts) |q| { + if (q.history.len != g.hash.n - 1 or q.out.len != q.tokens.len * w or q.xs.len != q.out.len / 32) return error.UnexpectedTensor; + total += q.tokens.len; + } + // the parts' outputs are one buffer's consecutive slices, so one fill covers all of them + for (parts[1..], 0..) |q, k| if (q.out.ptr != parts[k].out.ptr + parts[k].out.len) return error.UnexpectedTensor; + var hists: [max_lanes][ple.max_n]i64 = undefined; + const items = total * heads; + const ids = try g.gpa.alloc(i64, items); + defer g.gpa.free(ids); + var at: usize = 0; + for (parts, 0..) |q, k| { + const hist = &hists[k]; + @memcpy(hist[0..q.history.len], q.history); + for (q.tokens) |token| { + try ple.lastIds(g.hash, hist[0..q.history.len], @intCast(token), ids[at * heads ..][0..heads]); + for (0..q.history.len - 1) |i| hist[i] = hist[i + 1]; + hist[q.history.len - 1] = token; + at += 1; + } + } + var out = parts[0].out; + out.len = total * w; + const n_threads = @min(ple_pool.max_parts, items / 4); + if (g.format != .mlx and items > 1) { + try @import("cuda_ple_gather.zig").run(g, ids, out); + } else if (n_threads <= 1 or g.pool == null) { + try g.fill(ids, out, 0, items); + } else { + const split: ple.Ngram.Split = .{ .g = g, .ids = ids, .out = out, .per = (items + n_threads - 1) / n_threads }; + try g.pool.?.run(n_threads, .{ .ctx = &split, .run = ple.Ngram.Split.part }); + } + for (parts) |q| { + for (0..q.tokens.len) |t| { + const row = q.out[t * w ..][0..w]; + if (g.format == .mlx and g.scale != 1) { + for (row) |*v| { + const f: f32 = @bitCast(@as(u32, v.*) << 16); + v.* = ple.toBf16(f * g.scale); + } + } + ple.sumGroups(row, q.xs[t * (w / 32) ..][0 .. w / 32]); + } + } + for (parts, 0..) |q, k| @memcpy(q.history, hists[k][0..q.history.len]); +} + +const a = std.testing.allocator; +const io = std.testing.io; +const base = "layers.1.ple.ple_embedding.ngram_embedding"; +const config = + \\{"text_config":{"num_hidden_layers":2,"hidden_size":32,"vocab_size":100,"ple_layer_ids":[2], + \\ "ngram_size":3,"heads_per_ngram":2,"ngram_vocab_size_base":13,"make_ngram_vocab_size_divisible_by":2, + \\ "split_ngram_parts":2,"ple_embed_dim":2560,"eos_token_id":99}} +; + +const Kind = enum { nvfp4, mlx }; + +const Fill = union(enum) { nibbles: usize, byte: u8, words: usize, bf16: u16 }; + +/// A two-shard table of the served shape (2,560 wide, 3-gram, 2 heads a gram), NVFP4 or the same rows as MLX 4-bit. +fn fixture(dir: std.Io.Dir, kind: Kind) !void { + try dir.writeFile(io, .{ .sub_path = "config.json", .data = config }); + const h = try ple.Hash.init(.{ .vocab = 100, .ngram = 3, .per = 2, .base = 13, .divisor = 2, .shards = 2, .seed = 1234, .eos = 99, .ple_dim = dims, .ple_index = 0 }); + var header: std.Io.Writer.Allocating = .init(a); + defer header.deinit(); + var index: std.Io.Writer.Allocating = .init(a); + defer index.deinit(); + var data: std.ArrayList(u8) = .empty; + defer data.deinit(a); + try header.writer.writeByte('{'); + try index.writer.writeAll("{\"weight_map\":{"); + var first = true; + for (0..2) |shard| { + const rows: usize = @intCast(h.starts[shard + 1] - h.starts[shard]); + const start: usize = @intCast(h.starts[shard]); + switch (kind) { + .nvfp4 => { + try tensor(&header, &index, &data, &first, "model.language_model", shard, "weight", "U8", rows, dims / 8, .{ .nibbles = start }); + try tensor(&header, &index, &data, &first, "model.language_model", shard, "weight_scale", "F8_E4M3", rows, dims / 64, .{ .byte = 0x38 }); + }, + .mlx => { + try tensor(&header, &index, &data, &first, "language_model.model", shard, "weight", "U32", rows, dims / 32, .{ .words = start }); + try tensor(&header, &index, &data, &first, "language_model.model", shard, "scales", "BF16", rows, dims / 128, .{ .bf16 = 0x3f00 }); + try tensor(&header, &index, &data, &first, "language_model.model", shard, "biases", "BF16", rows, dims / 128, .{ .bf16 = 0x3d00 }); + }, + } + } + // the table's own scale, which the MLX rows multiply and the NVFP4 rows require + const scale_name = if (kind == .mlx) "language_model.model." ++ base ++ ".weight_scale" else "model.language_model." ++ base ++ ".weight_scale_2"; + const begin = data.items.len; + try put(&data, u32, @bitCast(@as(f32, if (kind == .mlx) 0.75 else 0.375))); + if (!first) { + try header.writer.writeByte(','); + try index.writer.writeByte(','); + } + try header.writer.print("\"{s}\":{{\"dtype\":\"F32\",\"shape\":[],\"data_offsets\":[{d},{d}]}}", .{ scale_name, begin, data.items.len }); + try index.writer.print("\"{s}\":\"table.safetensors\"", .{scale_name}); + try header.writer.writeByte('}'); + try index.writer.writeAll("}}"); + var image: std.Io.Writer.Allocating = .init(a); + defer image.deinit(); + const size = std.mem.toBytes(std.mem.nativeToLittle(u64, header.written().len)); + try image.writer.writeAll(&size); + try image.writer.writeAll(header.written()); + try image.writer.writeAll(data.items); + try dir.writeFile(io, .{ .sub_path = "table.safetensors", .data = image.written() }); + try dir.writeFile(io, .{ .sub_path = "model.safetensors.index.json", .data = index.written() }); +} + +fn tensor(header: *std.Io.Writer.Allocating, index: *std.Io.Writer.Allocating, data: *std.ArrayList(u8), first: *bool, pre: []const u8, shard: usize, part: []const u8, dtype: []const u8, rows: usize, cols: usize, fill: Fill) !void { + const begin = data.items.len; + for (0..rows) |row| { + for (0..cols) |col| switch (fill) { + .nibbles => |start| try data.append(a, @intCast((start + row + col) % 16 | (((start + row) * 3 + col) % 16) << 4)), + .byte => |v| try data.append(a, v), + .words => |start| try put(data, u32, @truncate((start + row) *% 2654435761 +% col *% 0x11111111)), + .bf16 => |v| try put(data, u16, v), + }; + } + if (first.*) first.* = false else { + try header.writer.writeByte(','); + try index.writer.writeByte(','); + } + var name: [220]u8 = undefined; + const full = try std.fmt.bufPrint(&name, "{s}.{s}.shard_{d}.{s}", .{ pre, base, shard, part }); + try header.writer.print("\"{s}\":{{\"dtype\":\"{s}\",\"shape\":[{d},{d}],\"data_offsets\":[{d},{d}]}}", .{ full, dtype, rows, cols, begin, data.items.len }); + try index.writer.print("\"{s}\":\"table.safetensors\"", .{full}); +} + +fn put(data: *std.ArrayList(u8), comptime T: type, v: T) !void { + var b: [@sizeOf(T)]u8 = undefined; + std.mem.writeInt(T, &b, v, .little); + try data.appendSlice(a, &b); +} + +fn load(tmp: *std.testing.TmpDir, kind: Kind) !ple.Ngram { + try fixture(tmp.dir, kind); + const path = try std.fmt.allocPrint(a, ".zig-cache/tmp/{s}", .{tmp.sub_path}); + defer a.free(path); + return ple.Ngram.init(a, io, path); +} + +test "the one pooled call equals each stream's own rows call, byte for byte, on both table formats" { + const Case = struct { + history: [3][2]i64, + tokens: [3][8]u32, + rows: [3]usize, + lanes: usize, + }; + const cases = [_]Case{ + // two lanes of the same rows: every id is asked for twice in the one call + .{ .history = .{ .{ 99, 99 }, .{ 99, 99 }, .{ 0, 0 } }, .tokens = .{ .{ 7, 12, 7, 0, 0, 0, 0, 0 }, .{ 7, 12, 7, 0, 0, 0, 0, 0 }, .{ 0, 0, 0, 0, 0, 0, 0, 0 } }, .rows = .{ 3, 3, 0 }, .lanes = 2 }, + // three lanes over different histories: the same token hashes elsewhere, and some ids still collide + .{ .history = .{ .{ 99, 99 }, .{ 12, 99 }, .{ 40, 5 } }, .tokens = .{ .{ 7, 12, 3, 5, 40, 1, 9, 2 }, .{ 12, 7, 3, 9, 1, 40, 5, 2 }, .{ 5, 40, 5, 0, 0, 0, 0, 0 } }, .rows = .{ 8, 8, 3 }, .lanes = 3 }, + // one lane: the same one call, over one stream + .{ .history = .{ .{ 99, 99 }, .{ 0, 0 }, .{ 0, 0 } }, .tokens = .{ .{ 7, 12, 7, 12, 7, 12, 7, 12 }, .{ 0, 0, 0, 0, 0, 0, 0, 0 }, .{ 0, 0, 0, 0, 0, 0, 0, 0 } }, .rows = .{ 8, 0, 0 }, .lanes = 1 }, + }; + for ([_]Kind{ .nvfp4, .mlx }) |kind| { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var g = try load(&tmp, kind); + defer g.deinit(); + const w = g.width(); + try std.testing.expectEqual(dims, w); + for (cases) |c| { + var total: usize = 0; + for (c.rows[0..c.lanes]) |n| total += n; + const want = try a.alloc(u16, total * w); + defer a.free(want); + const got = try a.alloc(u16, total * w); + defer a.free(got); + const want_xs = try a.alloc(f32, total * (w / 32)); + defer a.free(want_xs); + const got_xs = try a.alloc(f32, total * (w / 32)); + defer a.free(got_xs); + var want_hist = c.history; + var got_hist = c.history; + var at: usize = 0; + for (0..c.lanes) |k| { + const n = c.rows[k]; + try g.rows(want_hist[k][0 .. g.hash.n - 1], c.tokens[k][0..n], want[at * w ..][0 .. n * w], want_xs[at * (w / 32) ..][0 .. n * (w / 32)]); + at += n; + } + const ids = try a.alloc(u32, total); + defer a.free(ids); + at = 0; + for (0..c.lanes) |k| { + @memcpy(ids[at..][0..c.rows[k]], c.tokens[k][0..c.rows[k]]); + at += c.rows[k]; + } + var lanes: [3]batch_state.Lane = undefined; + for (0..c.lanes) |k| lanes[k] = .{ .seq = .{ .conv = &.{}, .state = &.{}, .kc = &.{}, .vc = &.{}, .ikc = &.{}, .pooled = &.{}, .tail = undefined, .history = &got_hist[k], .cap = 0, .scores = 0 }, .pos = 0, .rows = c.rows[k] }; + @memset(got, 0xdead); + @memset(got_xs, @bitCast(@as(u32, 0xdeadbeef))); + var gather: batch_state.Gather = .{ .ngram = &g, .history = &.{}, .ids = ids, .emb = got, .xs = got_xs, .io = io, .lanes = lanes[0..c.lanes] }; + try std.testing.expect(!earlyOn(&gather, false)); + try std.testing.expect(gather.failed == null); + try std.testing.expectEqual(@as(u16, 0xdead), got[0]); + try std.testing.expect(earlyOn(&gather, true)); + try std.testing.expect(gather.failed == null); + try std.testing.expectEqualSlices(u16, want, got); + try std.testing.expectEqualSlices(u8, std.mem.sliceAsBytes(want_xs), std.mem.sliceAsBytes(got_xs)); + for (0..c.lanes) |k| try std.testing.expectEqualSlices(i64, want_hist[k][0 .. g.hash.n - 1], got_hist[k][0 .. g.hash.n - 1]); + } + } +} + +test "a pooled gather refuses parts whose outputs are not one buffer's consecutive slices" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var g = try load(&tmp, .nvfp4); + defer g.deinit(); + const w = g.width(); + const tokens = [_]u32{ 7, 12 }; + const out = try a.alloc(u16, 8 * w); + defer a.free(out); + const xs = try a.alloc(f32, 8 * (w / 32)); + defer a.free(xs); + var hist = [_]i64{ 99, 99 }; + const good: Part = .{ .history = &hist, .tokens = &tokens, .out = out[0 .. 2 * w], .xs = xs[0 .. 2 * (w / 32)] }; + try rowsMany(&g, &.{good}); + var gap_hist = [_]i64{ 99, 99 }; + const gap: Part = .{ .history = &gap_hist, .tokens = &tokens, .out = out[4 * w ..][0 .. 2 * w], .xs = xs[2 * (w / 32) ..][0 .. 2 * (w / 32)] }; + try std.testing.expectError(error.UnexpectedTensor, rowsMany(&g, &.{ good, gap })); + var short_hist = [_]i64{99}; + const bad_history: Part = .{ .history = &short_hist, .tokens = &tokens, .out = out[0 .. 2 * w], .xs = xs[0 .. 2 * (w / 32)] }; + try std.testing.expectError(error.UnexpectedTensor, rowsMany(&g, &.{bad_history})); + try std.testing.expectError(error.UnexpectedTensor, rowsMany(&g, &.{})); +} + +test "a pooled pass takes 1 to 64 lanes, and the switch decides the rest" { + try std.testing.expect(earlyLanes(true, 1)); + try std.testing.expect(earlyLanes(true, 64)); + try std.testing.expect(!earlyLanes(true, 65)); + try std.testing.expect(!earlyLanes(true, 0)); + try std.testing.expect(!earlyLanes(false, 4)); + try std.testing.expectEqual(@as(usize, 64), max_lanes); +} + +test "the shared pass takes the pooled gather before its own rows call a stream" { + const body = @embedFile("cuda_batch_state.zig"); + const main = std.mem.indexOf(u8, body, "pub fn main(g: *Gather) void {") orelse return error.TestUnexpectedResult; + const call = std.mem.indexOfPos(u8, body, main, "if (gather_lanes.early(g)) return;") orelse return error.TestUnexpectedResult; + const per_lane = std.mem.indexOfPos(u8, body, main, "for (g.lanes) |l| {") orelse return error.TestUnexpectedResult; + try std.testing.expect(call < per_lane); +} diff --git a/zig/src/platform/environment.zig b/zig/src/platform/environment.zig index 907cd26d86a..c6ef423b578 100644 --- a/zig/src/platform/environment.zig +++ b/zig/src/platform/environment.zig @@ -256,6 +256,7 @@ pub const rules = [_]Rule{ .{ .name = "TF_FLASHNEXT_ATTN_LANES", .scope = .flashnext, .kind = .flag, .forms = "0 or 1 (default 0)" }, .{ .name = "TF_FLASHNEXT_WARM_AHEAD", .scope = .flashnext, .kind = .flag, .forms = "0 or 1 (default 0)" }, .{ .name = "TF_FLASHNEXT_TABLE_WARM", .scope = .flashnext, .kind = .flag, .forms = "0 or 1 (default 0)" }, + .{ .name = "TF_FLASHNEXT_GATHER_EARLY", .scope = .flashnext, .kind = .flag, .forms = "0 or 1 (1: one pooled n-gram gather a shared pass; default 0)" }, .{ .name = "TF_FLASHNEXT_PROFILE", .scope = .flashnext, .kind = .flag, .forms = "0 or 1 (default 0)" }, .{ .name = "TF_FLASHNEXT_VERIFY_HOST_LOGITS", .scope = .flashnext, .kind = .flag, .forms = "0 or 1 (default 0)" }, .{ .name = "TF_FLASHNEXT_DRAFTS", .scope = .flashnext, .kind = .size, .forms = "a whole draft count from 0 to the native size maximum (default 6; clamped to 1 through 15)" }, diff --git a/zig/src/platform/environment_flashnext_test.zig b/zig/src/platform/environment_flashnext_test.zig index bf2f77352f7..559f6e03e3a 100644 --- a/zig/src/platform/environment_flashnext_test.zig +++ b/zig/src/platform/environment_flashnext_test.zig @@ -33,13 +33,13 @@ fn check(name: []const u8, raw: []const u8, valid: bool) !void { try t.expect(std.mem.endsWith(u8, why.?, e.rule(name).forms)); } -test "Flash Next strict env: the table holds all 35 settings: 23 switches, 9 integers, 1 real, 2 structured" { +test "Flash Next strict env: the table holds all 36 settings: 24 switches, 9 integers, 1 real, 2 structured" { var counts: [4]usize = @splat(0); for (e.rules) |r| if (r.scope == .flashnext) { try t.expect(std.mem.startsWith(u8, r.name, "TF_FLASHNEXT_")); counts[@backingInt(group(r))] += 1; }; - try t.expectEqual([4]usize{ 23, 9, 1, 2 }, counts); + try t.expectEqual([4]usize{ 24, 9, 1, 2 }, counts); } test "Flash Next strict env: unset and blank settings both read as the caller's default" { diff --git a/zig/tests/fixtures/gradient-serving.tsv b/zig/tests/fixtures/gradient-serving.tsv index 841a566b9fd..5af388e902c 100644 --- a/zig/tests/fixtures/gradient-serving.tsv +++ b/zig/tests/fixtures/gradient-serving.tsv @@ -116,7 +116,7 @@ zig/src/cuda/triton.zig 256323865c6a4676b8b75aae9e32d2d9732cf2623efedb9462cf6f95 zig/src/cuda/vit.zig 72c4301251f50d0f25907f0d8ad03a943281b234c506a455e413627a3ffe09c0 raw zig/src/families/flashnext/cuda_attn.zig 99dfe79f919a8599490f1cdba0bec20d6e73ccbeafe26d5848419a465fdfa0d0 raw zig/src/families/flashnext/cuda_batch.zig f1c5ab1478497ab2c0abdf77776dfed36fa9092579d4306e4320a32cfadf6910 raw main 42ff6c510f (disk states keyed by the resolved prompt passes; serving unchanged); #XXXX: the scratch test's ceiling restated at the 3,072-row default, #3413 (TF_FLASHNEXT_READOUT_FUSED's prompt read-out; unchanged when unset) -zig/src/families/flashnext/cuda_batch_state.zig 7e940f58ec4ecba11a657983375e3cfec7492989c6246f037759b8051ce70129 raw y and moe_f32 take one spare row for a --tp 2 prompt pass's padded rank block; replies unchanged; #XXXX: the default prompt chunk is 3,072 rows +zig/src/families/flashnext/cuda_batch_state.zig 493ee0dc5bc832b9344d16de92ab33f5de15574b4d9a81f283558c4d729c6db6 raw y and moe_f32 take one spare row for a --tp 2 prompt pass's padded rank block; replies unchanged; #XXXX: the default prompt chunk is 3,072 rows; the shared pass takes the pooled n-gram gather first (TF_FLASHNEXT_GATHER_EARLY); replies unchanged zig/src/families/flashnext/cuda_collective.zig bf475ffb46b3440fcad280f46e34de25cd22b79ed96a0b587d0869e7451d3519 raw adds sumBlock, the in-place FP32 reduce-scatter with sum's peer check, deadline and abort; no served change on its own zig/src/families/flashnext/cuda_compare_main.zig ade014dfec88f0b386076cdc093ea20710d7bf36c33ff566ca2caf472064695b raw main dfd9ac5ef4 (#2585: the TF_FLASHNEXT_* startup check), c274b45cc1 (the check through the flashnext module) zig/src/families/flashnext/cuda_config.zig 19366ee591270c719fa6f710af0efd4d5a55cfc3e30c923f5ed08771b5c14001 raw @@ -147,7 +147,7 @@ zig/src/families/flashnext/cuda_mtp_draft.zig 41628480a1071c4a58a229f5e8b42e26fc zig/src/families/flashnext/cuda_mtp_load.zig d205877fe6d9d6b7b24a0ae061e8fc59a947fdcb1911d38e9f788af2468816c2 raw main 3a56d4eb92 (the draft head's mode read through platform/environment.zig: unset or blank keeps affine4, bf16 keeps the stored rows; kernels and launch counts unchanged) zig/src/families/flashnext/cuda_mtp_ops.zig c6e07580378538c0131fc687003d7559bca8b2318373c1f889ef1a07423ffa3c raw main 4b2679bf9 (grouped draft head carries its page table), 4f6017ced, ec76abd45 (reserveRows), 80e0fd72c, c6a337753 (#2753: verify rows checked after one completion wait, non-finite rows refused; finite rows unchanged), #2927 zig/src/families/flashnext/cuda_norms.zig c6ca7f9c1a9049c0f9d512fe2a38c2672a2e96b5c060d6cec2bca0d39e34bad0 raw -zig/src/families/flashnext/cuda_ple.zig 7c4b009de2e44a7c28db56725ed010bbc7e67dff4688640ecf8e22a1cbefeac7 raw main dfd9ac5ef4 (#2585: TF_FLASHNEXT_* read through platform/environment.zig, malformed text refused at startup; valid values keep their meaning) +zig/src/families/flashnext/cuda_ple.zig 417abb05f6cf212c09042bbbd695f313666b13639a81229d9fc6b26e5026b6a2 raw main dfd9ac5ef4 (#2585: TF_FLASHNEXT_* read through platform/environment.zig, malformed text refused at startup; valid values keep their meaning); the shared pass gathers its lanes in one pool call (Split and part reachable from cuda_ple_gather_lanes.zig); every row is the per-stream call's zig/src/families/flashnext/cuda_ple_check.zig e8b7cac1e0d3718f106482f6cff7375c0063e1757c46814bd40da435c83ba8ed raw zig/src/families/flashnext/cuda_ple_nvfp4_test.zig 742bef48f8ddbf7257c18ad78911d519d8a49a2d9fce5885df7377875bf8008c raw zig/src/families/flashnext/cuda_projection.zig 42e553d337ee5b2acd2146018a4db96bc471a5cc7bc2a87f567ace934b1b40d5 raw