diff --git a/CHANGELOG.md b/CHANGELOG.md index 375ae6915f8..8bea80affa7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,14 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel ## Unreleased +- Server: `TENSORFOLD_PARALLEL` sets the default lanes when `serve` has no `--parallel`: a whole number 1 to 4096 or `auto`; the flag wins when both are given, a blank value keeps the default, and any other value is refused by name at startup (#3685, for #3684). Thanks to @bikepresentball. + +- Server: a reply that ends on its own end-of-sequence inside a think block it never closed now returns that text as its answer only when the request asked for reasoning and no tool call was required, no grammar applied and no call was streamed, and the server logs a warning when it does (#3721, refs #3687). Thanks to @joshuaswarren. + +- CUDA: a Flash Next prompt pass holds 3,072 rows while other streams decode (it held 1,536), so a long prompt beside running streams takes half as many passes (#3726). Thanks to @BadAd84. + +- GLM-5.3-Flash on CUDA: the sampled draw takes its rule from the shared `cuda.sample.rule`, as MiniMax M3 does, instead of building its own copy with a fixed `near` value (#3705, part of #2680). Thanks to @jschmied. + - Core prompt cache: the radix store can keep a checkpoint inside a page as a writable tail, a store-held copy of that page's rows that is never written again; no family turns tails on yet, so every current store behaves as before (#3329, part of #2444). Thanks to @laudney. - CUDA: on a GB10, the one-launch path for two to four affine-4 prompt projections (`tf_qmm_prefill_many`) works out each thread's copy addresses once, before the K loop, instead of at every stage; the copies, their order and the math are unchanged (#2825). Thanks to @GumbiiDigital. @@ -425,6 +433,7 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel - GLM-5.3-Flash on CUDA with `TF_KV_SHARE` and no `--prompt-cache-gib`: the kept states' slots, `TF_KV_SHARE` at most, now come where an asked cache goes, before the windows past the engine's own, so a room that holds one more whole window no longer keeps fewer states; Jammed XSFP4 at TP=3 on three GB10s keeps all 32 at `TF_KV_SHARE=32` instead of 8, with the same tokens (#3125). Thanks to @Bizuayeu. - Metal: Baidu's Unlimited-OCR-2 is served natively from its BF16 release on M5-class Macs: page images through `serve --vision`, the family's chat template built in, its SAM and CLIP image encoders and decoder under the kernel contract; on five reference pages every departure from the release's own reply is a one-step tie (#2074). Thanks to @BobClawblaw. + ## 1.2.9 - Flash Next on CUDA: a lone stream draws on its own path again; the batched draws #2927 added now run only when two or more streams share a step, so one sampled stream no longer pays the group's whole-buffer download each step. Tokens are unchanged (#3391, after #2927). diff --git a/docs/environment.md b/docs/environment.md index bf11b83c5f3..339e207e5f9 100644 --- a/docs/environment.md +++ b/docs/environment.md @@ -10,6 +10,7 @@ Diagnostics quote and escape the supplied text so it cannot create another termi | `TF_CACHE_EVICT` | `main` (default) or `decayed_resume`. The opt-in gives recently resumed RAM snapshots bounded recency credit, within the same cache budget. It changes victim choice, not stored state or token arithmetic. Paged stores keep their own policy. Blank keeps `main`; other values are refused at server startup. | | `TENSORFOLD_VIDEO_TOKENS` | Nonnegative whole video-token budget; default 16384; stored as an unsigned 64-bit count | | `TENSORFOLD_MAX_PDF_PAGES` | Whole count 1 through 4294967295, capped by `--vision-max-images`; default min(16, image limit) | +| `TENSORFOLD_PARALLEL` | Whole count 1 through 4096, or `auto` (`auto` = 8); default: what `--parallel` says. Read only when `--parallel` is not on the command line, so the flag wins; blank keeps the default; other values are refused at server startup | | `TENSORFOLD_PRESSURE_CACHE_PERCENT` | Whole percentage 0 through 100; default 50 | | `TENSORFOLD_PREFIX_WAIT_MS` | Whole milliseconds 0 through 4294967295; zero disables waiting; default 60000 | | `TENSORFOLD_RESTORE_PREFETCH` | Whole unsigned 64-bit count, capped at 65536; zero disables reading ahead; default 2 | @@ -139,7 +140,7 @@ stops the start with the variable, the supplied text and the accepted forms. | `TF_FLASHNEXT_GREEDY_WIDE` | Whole round width; default 4 | | `TF_FLASHNEXT_WINDOW_ROWS` | Whole row count; default the shared round's rows, capped by them | | `TF_FLASHNEXT_READOUT_ROWS` | Whole row count; default 64; `0` never takes the tiles | -| `TF_FLASHNEXT_CHUNK` | Whole prompt rows a pass, 1 through 16384; default 1536 | +| `TF_FLASHNEXT_CHUNK` | Whole prompt rows a pass, 1 through 16384; default 3072 | | `TF_FLASHNEXT_CHUNK_SOLO` | Whole prompt rows a pass with no stream decoding, from the chunk `TF_FLASHNEXT_CHUNK` resolves to through 16384; unset or blank is the larger of that chunk and 8192 on a server of one stream, and that chunk elsewhere | | `TF_FLASHNEXT_FACT_ROWS` | Whole row count; default 64; `0` never takes the tiles | | `TF_FLASHNEXT_KV_PAGE_TEST` | Whole page size the kernels take: whole blocks of the indexer's ratio up to the window; unset or blank leaves the plain caches | diff --git a/docs/receipts/flashnext-prompt-chunk3072/README.md b/docs/receipts/flashnext-prompt-chunk3072/README.md new file mode 100644 index 00000000000..10a617e82c6 --- /dev/null +++ b/docs/receipts/flashnext-prompt-chunk3072/README.md @@ -0,0 +1,151 @@ +# Flash Next CUDA: a prompt pass holds 3,072 rows by default + +A Flash Next prompt pass held 1,536 rows at most while another stream decoded (`cuda_batch_state.zig`'s `default_chunk`), so a long prompt ran in passes of 1,536 and each lane admitted 1,536 rows a pass. It holds 3,072 now: about half the passes for a long prompt, each holding twice the rows, and the same 3,072 rows a lane's prompt admission. A server started at one stream is untouched: it takes 8,192-row passes on both sides (`cuda_chunk.zig`'s solo rule). `TF_FLASHNEXT_CHUNK` still overrides the default, 1 to 16,384. + +Result: cold prompts 6.4% to 9.8% faster to the first token idle and 12.2% to 17.2% beside three decoders (2k to 64k), `bench_cache`'s first cold request 7.2% faster; decode level at 1 to 8 streams; the one-stream control level; every exactness contract equal on both arms and every greedy reply equal across them. + +## Qualified scope + +TensorFold main `077998f7d9b14adebec3a2acc2caa47ff7ae6ec7` against main plus this change, commit `052e72a9c3eb6025b0f6a6095f06194fdc9a3313`, both built from source with Zig 0.17.0 and nvcc 13.3 in the `nvcr.io/nvidia/pytorch:26.07-py3` image (`zig build -Dnvcc=/usr/local/cuda/bin/nvcc -Doptimize=fast fatbins install native`). One DGX Spark (GB10, sm_121, memory shared with the host), kernel driver 580.178.04. Checkpoint `nvidia/Qwen3.8-Flash-Next-FP4-Local` at `b9ce18a6887e18c089fdea8509a33e63429a3300`. + +Server per arm, through the serving recipe (`CHECKPOINT=nvfp4 KV_DTYPE=bf16 TENSORFOLD_CUDA_KERNELS=native VISION=0`); the container command line on the decode boots was: + +``` +tensorfold-native serve CHECKPOINT --name Qwen3.8-Flash-Next --host 127.0.0.1 --port 8080 --context 32768 --parallel 8 --max-tokens 32768 --temperature 1.0 --top-p 0.95 --top-k 20 --thinking --kv-cache bf16 +``` + +with `--context 131072 --parallel 4` and `--prompt-cache-gib 0` for the cold prompts, `--parallel 1` for the control and `--parallel 8` for the pool check at that window. The recipe exports these variables into the container, the same set on every boot of both arms (each boot's own environment was recorded and compared): + +``` +TENSORFOLD_CUDA_KERNELS=native +TENSORFOLD_IMAGE_TOKENS=16384 +TENSORFOLD_MAX_IMAGES=50 +TENSORFOLD_MEMORY_RESERVE_GIB=9 +TENSORFOLD_NO_UPDATE_CHECK=1 +TENSORFOLD_VIDEO_TOKENS=16384 +TENSORFOLD_VISION_PYTHONPATH=/opt/tensorfold/python +TENSORFOLD_VISION_WORKSPACE_MIB=2048 +TF_FLASHNEXT_CONFIDENCE=-0.4 +TF_FLASHNEXT_DRAFTS=6 +TF_FLASHNEXT_DRAFT_AHEAD=1 +TF_FLASHNEXT_GATHER_EARLY=1 +TF_FLASHNEXT_GATHER_READ=pread +TF_FLASHNEXT_GDN_LANES=1 +TF_FLASHNEXT_HEAD_PIECE=1536 +TF_FLASHNEXT_HOST_DRAW_THREADS=7 +TF_FLASHNEXT_KEEP_LATE=1 +TF_FLASHNEXT_PREFILL_TAIL=512 +TF_FLASHNEXT_PRODUCT_STREAMS=5 +TF_FLASHNEXT_PROMPT_EXPERT_TILE_ROWS=256 +TF_FLASHNEXT_PROMPT_GEMM2=1 +TF_FLASHNEXT_PROMPT_HC2=1 +TF_FLASHNEXT_PROMPT_SHARED_DIRECT=1 +TF_FLASHNEXT_SAMPLED_HEADS=1 +TF_FLASHNEXT_SAMPLE_CAND=1 +TF_FLASHNEXT_SHARED_SIDE=1 +TF_FLASHNEXT_WARM_AHEAD=1 +``` + +12 of them are read by neither tree (`TF_FLASHNEXT_DRAFT_AHEAD`, `TF_FLASHNEXT_GATHER_EARLY`, `TF_FLASHNEXT_GATHER_READ`, `TF_FLASHNEXT_HEAD_PIECE`, `TF_FLASHNEXT_HOST_DRAW_THREADS`, `TF_FLASHNEXT_PREFILL_TAIL`, `TF_FLASHNEXT_PRODUCT_STREAMS`, `TF_FLASHNEXT_PROMPT_EXPERT_TILE_ROWS`, `TF_FLASHNEXT_PROMPT_GEMM2`, `TF_FLASHNEXT_PROMPT_HC2`, `TF_FLASHNEXT_PROMPT_SHARED_DIRECT`, `TF_FLASHNEXT_SAMPLE_CAND`), so they change nothing on either arm; the rest are read by both. The two arms differ in nothing but the image, and `TF_FLASHNEXT_CHUNK` is set on neither, so each serves its own default. + +Order: `M1 B1 B2 M2`, a fresh server each, in every timed pass. A cell is slower when both branch runs are under both main runs (for a latency: over) and the mean gap is over 1%, faster when both are over both and the gap is over 1%, level otherwise; a flagged cell would have been re-measured once on fresh boots. No cell was flagged. + +## Correctness + +- B cache on concurrent: concurrent equals solo 156/0 (unverified 0); drafted equals `"draft": false` 56/0 +- B cache on golden: concurrent equals solo 13/0 (unverified 0); drafted equals `"draft": false` 24/0 +- B cache on drafts: 8 of 8 equal to `"draft": false` (basis token_ids) +- B cache on turns: resumed from cached prompt tokens [13, 16, 16, 13, 24, 18, 3406] +- B cache off drafts: 8 of 8 equal to `"draft": false` (basis token_ids) +- B cache off fresh: 7 of 7 resumed replies equal the fresh ones +- M cache on concurrent: concurrent equals solo 156/0 (unverified 0); drafted equals `"draft": false` 56/0 +- M cache on golden: concurrent equals solo 13/0 (unverified 0); drafted equals `"draft": false` 24/0 +- M cache on drafts: 8 of 8 equal to `"draft": false` (basis token_ids) +- M cache on turns: resumed from cached prompt tokens [13, 16, 16, 13, 24, 18, 3406] +- 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 +- B vs M: replies equal over pass A's greedy cells (token_sha): 528 equal, 0 unequal + +- the cold cells' token ids and reply text, the same prompt bytes sent to every arm: 8 of 8 cells at four slots and 4 of 4 at one slot equal on all four boots +- drafts off: 42 replies equal between the arms, 0 unequal + +## Prompt speed + +Cold prompts, `--prompt-cache-gib 0`, a 131,072-token window at four slots, so a 64k prompt is admitted beside three decoders (`load3`). Time to the first token, seconds, the median of three salted prompts, thinking off (`ttft.py --sizes 2k,8k,32k` and `--sizes 64k`, `--loads idle,load3 --reps 3 --max-tokens 16`), and `bench_cache.py --tokens 64 --repeats 2 --streams 2 --require-cold`'s first request. + +| prompt | load | M1 | M2 | B1 | B2 | B vs M | +|---|---|---|---|---|---|---| +| cold2k-off | idle | 1.070 | 1.071 | 0.972 | 0.960 | -9.8% faster | +| cold2k-off | load3 | 1.273 | 1.262 | 1.053 | 1.046 | -17.2% faster | +| cold8k-off | idle | 4.035 | 3.986 | 3.664 | 3.685 | -8.4% faster | +| cold8k-off | load3 | 4.653 | 4.632 | 3.989 | 3.987 | -14.1% faster | +| cold32k-off | idle | 16.015 | 16.044 | 14.980 | 15.039 | -6.4% faster | +| cold32k-off | load3 | 18.249 | 18.420 | 16.114 | 16.094 | -12.2% faster | +| cold64k-off | idle | 33.058 | 33.175 | 30.665 | 30.682 | -7.4% faster | +| cold64k-off | load3 | 37.582 | 37.718 | 32.780 | 32.786 | -12.9% faster | +| first_ttft_s | idle | 1.241 | 1.221 | 1.142 | 1.143 | -7.2% faster | + +Cold: 9 faster, 0 level, 0 slower, 0 unmeasured. + +## The one-stream control + +The same cells at one slot (idle only: a second stream cannot decode beside the measured prompt), where both arms take 8,192-row passes: + +| prompt | load | M1 | M2 | B1 | B2 | B vs M | +|---|---|---|---|---|---|---| +| cold2k-off | idle | 0.979 | 0.968 | 0.969 | 0.966 | -0.6% level | +| cold8k-off | idle | 3.458 | 3.434 | 3.453 | 3.414 | -0.4% level | +| cold32k-off | idle | 14.144 | 14.150 | 14.124 | 14.100 | -0.2% level | +| cold64k-off | idle | 29.025 | 28.967 | 28.896 | 28.984 | -0.2% level | +| first_ttft_s | idle | 1.148 | 1.150 | 1.143 | 1.141 | -0.6% level | + +Control: 0 faster, 5 level, 0 slower, 0 unmeasured. + +## Decode speed + +`bench_openai.py --tokens 256 --reps 3 --temperatures 0` (one stream) and `bench_concurrent.py --tokens 256 --reps 3 --levels 1,2,4,6,8 --alone --serial --temperatures 0`, prompt cache on, 32,768-token window, eight slots; aggregate tokens a second, the median of three reps. + +| prompt | streams | M1 | M2 | B1 | B2 | B vs M | tokens a round, M / B | +|---|---|---|---|---|---|---|---| +| chat | 1 | 46.9 | 46.2 | 46.5 | 46.3 | -0.3% level | 2.81 / 2.81 | +| chat | 2 | 86.8 | 86.2 | 86.3 | 86.3 | -0.2% level | 2.51 / 2.51 | +| chat | 4 | 156.6 | 155.1 | 155.7 | 155.7 | -0.1% level | 2.51 / 2.51 | +| chat | 6 | 211.0 | 209.2 | 209.8 | 209.5 | -0.2% level | 2.51 / 2.51 | +| chat | 8 | 258.1 | 256.1 | 256.9 | 256.5 | -0.2% level | 2.51 / 2.51 | +| code | 1 | 73.1 | 71.9 | 72.3 | 72.5 | -0.1% level | 5.22 / 5.22 | +| code | 2 | 138.8 | 136.9 | 138.0 | 137.5 | -0.1% level | 4.83 / 4.83 | +| code | 4 | 244.9 | 240.8 | 242.7 | 241.6 | -0.3% level | 4.83 / 4.83 | +| code | 6 | 310.5 | 306.5 | 307.7 | 306.2 | -0.5% level | 4.49 / 4.49 | +| code | 8 | 374.5 | 370.5 | 370.8 | 371.7 | -0.3% level | 4.49 / 4.49 | +| fibonacci-raw | 1 | 73.4 | 72.2 | 72.7 | 72.6 | -0.2% level | 5.22 / 5.22 | +| gpu-chat-no-think | 1 | 47.0 | 46.3 | 46.5 | 46.5 | -0.3% level | 2.81 / 2.81 | + +Decode: 0 faster, 12 level, 0 slower, 0 unmeasured. The server's acceptance ratio is identical on every boot (0.6242, 0.6884). + +## Memory + +The pass's device scratch at the default grows with the rows (`Scratch.bytes(default_chunk, .{ .head_rows = default_chunk })`: 2,753,048,104 bytes at 3,072 rows against 1,397,558,248 at 1,536). The engine reserves its scratch at load, before it reads its room, so each arm's own startup line carries the cost: + +| window / slots | arm | model GiB | a stream | streams at once | GiB left after the 9.0 GiB reserve | pass rows | +|---|---|---|---|---|---|---| +| 32768 / 8 | main | 72.71 | 1.29 GiB each | 8 | 27.9 | 1536 | +| 32768 / 8 | branch | 73.50 | 1.32 GiB each | 8 | 27.1 | 3072 | +| 131072 / 4 | main | 72.73 | 4.11 GiB each | 4 | 27.9 | 1536 | +| 131072 / 4 | branch | 73.52 | 4.14 GiB each | 4 | 27.0 | 3072 | +| 131072 / 1 | main | 76.16 | 4.23 GiB each | 1 | 24.3 | 8192 | +| 131072 / 1 | branch | 76.16 | 4.23 GiB each | 1 | 24.4 | 8192 | +| 131072 / 8 | main | - | 8 need 32.86 GiB | refused, names 6 | 27.85 | - | +| 131072 / 8 | branch | - | 8 need 33.10 GiB | refused, names 6 | 27.01 | - | + +At 131,072 tokens and eight slots both arms refuse `--parallel 8` by name and name six streams; at every other setting both admit the same streams. + +The lines verbatim: + +- 32768/8 main: CUDA sm_121 device 0 (NVIDIA GB10, memory shared with the host): 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 +- 32768/8 branch: CUDA sm_121 device 0 (NVIDIA GB10, memory shared with the host): model 73.50 GiB; 8 streams at once, 1.32 GiB each at a 32768-token window, of 27.1 GiB left after a 9.0 GiB reserve; prompts in passes of up to 3072 rows, a 16.0 GiB prompt cache; own glue kernels +- 131072/4 main: CUDA sm_121 device 0 (NVIDIA GB10, memory shared with the host): model 72.73 GiB; 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 +- 131072/4 branch: CUDA sm_121 device 0 (NVIDIA GB10, memory shared with the host): model 73.52 GiB; 4 streams at once, 4.14 GiB each at a 131072-token window, of 27.0 GiB left after a 9.0 GiB reserve; prompts in passes of up to 3072 rows, no prompt cache; own glue kernels +- 131072/1 main: CUDA sm_121 device 0 (NVIDIA GB10, memory shared with the host): model 76.16 GiB; 1 stream at once, 4.23 GiB each at a 131072-token window, of 24.3 GiB left after a 9.0 GiB reserve; prompts in passes of up to 8192 rows, no prompt cache; own glue kernels +- 131072/1 branch: CUDA sm_121 device 0 (NVIDIA GB10, memory shared with the host): model 76.16 GiB; 1 stream at once, 4.23 GiB each at a 131072-token window, of 24.4 GiB left after a 9.0 GiB reserve; prompts in passes of up to 8192 rows, no prompt cache; own glue kernels +- 131072/8 main: refused at start: --parallel 8 needs 32.86 GiB for its streams at a 131072-token window, and the CUDA memory budget leaves 27.85 GiB: serve --parallel 6, or lower --context +- 131072/8 branch: refused at start: --parallel 8 needs 33.10 GiB for its streams at a 131072-token window, and the CUDA memory budget leaves 27.01 GiB: serve --parallel 6, or lower --context diff --git a/docs/receipts/flashnext-prompt-chunk3072/cells.csv b/docs/receipts/flashnext-prompt-chunk3072/cells.csv new file mode 100644 index 00000000000..dc5fafc7508 --- /dev/null +++ b/docs/receipts/flashnext-prompt-chunk3072/cells.csv @@ -0,0 +1,125 @@ +arm,pass,invocation,prompt,case,temperature,streams,value,per_stream,tokens_per_round,accepted_per_round,server_accepted,server_drafted,server_acceptance,alone_equal,alone_unequal,serial_equal,serial_unequal,failed,unmeasured,prompt_tokens +M1,A,conc_t0,chat,,0.0,1,46.9,46.9,2.813,1.813,22760,33060,0.6884,3,0,1,0,0,0, +M1,A,conc_t0,chat,,0.0,2,86.8,43.5,2.51,1.51,22760,33060,0.6884,6,0,1,0,0,0, +M1,A,conc_t0,chat,,0.0,4,156.6,39.8,2.51,1.51,22760,33060,0.6884,12,0,1,0,0,0, +M1,A,conc_t0,chat,,0.0,6,211.0,36.0,2.51,1.51,22760,33060,0.6884,18,0,1,0,0,0, +M1,A,conc_t0,chat,,0.0,8,258.1,33.3,2.51,1.51,22760,33060,0.6884,24,0,1,0,0,0, +M1,A,conc_t0,code,,0.0,1,73.1,73.1,5.224,4.224,22760,33060,0.6884,3,0,1,0,0,0, +M1,A,conc_t0,code,,0.0,2,138.8,70.0,4.83,3.83,22760,33060,0.6884,6,0,1,0,0,0, +M1,A,conc_t0,code,,0.0,4,244.9,62.8,4.83,3.83,22760,33060,0.6884,12,0,1,0,0,0, +M1,A,conc_t0,code,,0.0,6,310.5,53.8,4.491,3.491,22760,33060,0.6884,18,0,1,0,0,0, +M1,A,conc_t0,code,,0.0,8,374.5,49.2,4.491,3.491,22760,33060,0.6884,24,0,1,0,0,0, +M1,A,openai_t0,fibonacci-raw,,0.0,1,73.38379231509755,73.38379231509755,5.224,4.224,1488,2384,0.6242,,,,,0,0, +M1,A,openai_t0,gpu-chat-no-think,,0.0,1,46.965075034072775,46.965075034072775,2.813,1.813,1488,2384,0.6242,,,,,0,0, +M2,A,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,A,conc_t0,chat,,0.0,2,86.2,43.4,2.51,1.51,22760,33060,0.6884,6,0,1,0,0,0, +M2,A,conc_t0,chat,,0.0,4,155.1,39.5,2.51,1.51,22760,33060,0.6884,12,0,1,0,0,0, +M2,A,conc_t0,chat,,0.0,6,209.2,35.6,2.51,1.51,22760,33060,0.6884,18,0,1,0,0,0, +M2,A,conc_t0,chat,,0.0,8,256.1,33.0,2.51,1.51,22760,33060,0.6884,24,0,1,0,0,0, +M2,A,conc_t0,code,,0.0,1,71.9,71.9,5.224,4.224,22760,33060,0.6884,3,0,1,0,0,0, +M2,A,conc_t0,code,,0.0,2,136.9,69.2,4.83,3.83,22760,33060,0.6884,6,0,1,0,0,0, +M2,A,conc_t0,code,,0.0,4,240.8,62.0,4.83,3.83,22760,33060,0.6884,12,0,1,0,0,0, +M2,A,conc_t0,code,,0.0,6,306.5,53.1,4.491,3.491,22760,33060,0.6884,18,0,1,0,0,0, +M2,A,conc_t0,code,,0.0,8,370.5,48.6,4.491,3.491,22760,33060,0.6884,24,0,1,0,0,0, +M2,A,openai_t0,fibonacci-raw,,0.0,1,72.1865781794484,72.1865781794484,5.224,4.224,1488,2384,0.6242,,,,,0,0, +M2,A,openai_t0,gpu-chat-no-think,,0.0,1,46.342541484219055,46.342541484219055,2.813,1.813,1488,2384,0.6242,,,,,0,0, +B1,A,conc_t0,chat,,0.0,1,46.5,46.5,2.813,1.813,22760,33060,0.6884,3,0,1,0,0,0, +B1,A,conc_t0,chat,,0.0,2,86.3,43.4,2.51,1.51,22760,33060,0.6884,6,0,1,0,0,0, +B1,A,conc_t0,chat,,0.0,4,155.7,39.5,2.51,1.51,22760,33060,0.6884,12,0,1,0,0,0, +B1,A,conc_t0,chat,,0.0,6,209.8,35.8,2.51,1.51,22760,33060,0.6884,18,0,1,0,0,0, +B1,A,conc_t0,chat,,0.0,8,256.9,33.0,2.51,1.51,22760,33060,0.6884,24,0,1,0,0,0, +B1,A,conc_t0,code,,0.0,1,72.3,72.3,5.224,4.224,22760,33060,0.6884,3,0,1,0,0,0, +B1,A,conc_t0,code,,0.0,2,138.0,69.8,4.83,3.83,22760,33060,0.6884,6,0,1,0,0,0, +B1,A,conc_t0,code,,0.0,4,242.7,62.4,4.83,3.83,22760,33060,0.6884,12,0,1,0,0,0, +B1,A,conc_t0,code,,0.0,6,307.7,53.1,4.491,3.491,22760,33060,0.6884,18,0,1,0,0,0, +B1,A,conc_t0,code,,0.0,8,370.8,48.7,4.491,3.491,22760,33060,0.6884,24,0,1,0,0,0, +B1,A,openai_t0,fibonacci-raw,,0.0,1,72.67244569061366,72.67244569061366,5.224,4.224,1488,2384,0.6242,,,,,0,0, +B1,A,openai_t0,gpu-chat-no-think,,0.0,1,46.49557287715273,46.49557287715273,2.813,1.813,1488,2384,0.6242,,,,,0,0, +B2,A,conc_t0,chat,,0.0,1,46.3,46.3,2.813,1.813,22760,33060,0.6884,3,0,1,0,0,0, +B2,A,conc_t0,chat,,0.0,2,86.3,43.4,2.51,1.51,22760,33060,0.6884,6,0,1,0,0,0, +B2,A,conc_t0,chat,,0.0,4,155.7,39.5,2.51,1.51,22760,33060,0.6884,12,0,1,0,0,0, +B2,A,conc_t0,chat,,0.0,6,209.5,35.8,2.51,1.51,22760,33060,0.6884,18,0,1,0,0,0, +B2,A,conc_t0,chat,,0.0,8,256.5,33.0,2.51,1.51,22760,33060,0.6884,24,0,1,0,0,0, +B2,A,conc_t0,code,,0.0,1,72.5,72.5,5.224,4.224,22760,33060,0.6884,3,0,1,0,0,0, +B2,A,conc_t0,code,,0.0,2,137.5,69.6,4.83,3.83,22760,33060,0.6884,6,0,1,0,0,0, +B2,A,conc_t0,code,,0.0,4,241.6,62.1,4.83,3.83,22760,33060,0.6884,12,0,1,0,0,0, +B2,A,conc_t0,code,,0.0,6,306.2,53.0,4.491,3.491,22760,33060,0.6884,18,0,1,0,0,0, +B2,A,conc_t0,code,,0.0,8,371.7,48.9,4.491,3.491,22760,33060,0.6884,24,0,1,0,0,0, +B2,A,openai_t0,fibonacci-raw,,0.0,1,72.59725635790427,72.59725635790427,5.224,4.224,1488,2384,0.6242,,,,,0,0, +B2,A,openai_t0,gpu-chat-no-think,,0.0,1,46.50159814093216,46.50159814093216,2.813,1.813,1488,2384,0.6242,,,,,0,0, +M1,2,ttft,cold2k-off,cold2k-off,,,1.07,,,,,,,,,,,,, +M1,2,ttft,cold8k-off,cold8k-off,,,4.035,,,,,,,,,,,,, +M1,2,ttft,cold32k-off,cold32k-off,,,16.015,,,,,,,,,,,,, +M1,2,ttft,cold2k-off,cold2k-off,,,1.273,,,,,,,,,,,,, +M1,2,ttft,cold8k-off,cold8k-off,,,4.653,,,,,,,,,,,,, +M1,2,ttft,cold32k-off,cold32k-off,,,18.249,,,,,,,,,,,,, +M1,2,ttft64,cold64k-off,cold64k-off,,,33.058,,,,,,,,,,,,, +M1,2,ttft64,cold64k-off,cold64k-off,,,37.582,,,,,,,,,,,,, +M1,8,ttft1,cold2k-off,cold2k-off,,,0.979,,,,,,,,,,,,, +M1,8,ttft1,cold8k-off,cold8k-off,,,3.458,,,,,,,,,,,,, +M1,8,ttft1,cold32k-off,cold32k-off,,,14.144,,,,,,,,,,,,, +M1,8,ttft1,cold64k-off,cold64k-off,,,29.025,,,,,,,,,,,,, +M1,2,cache,bench_cache,first_ttft_s,,,1.2410360348876566,,,,,,,,,,,,,2571 +M1,8,cache,bench_cache,first_ttft_s,,,1.148247711127624,,,,,,,,,,,,,2571 +M2,2,ttft,cold2k-off,cold2k-off,,,1.071,,,,,,,,,,,,, +M2,2,ttft,cold8k-off,cold8k-off,,,3.986,,,,,,,,,,,,, +M2,2,ttft,cold32k-off,cold32k-off,,,16.044,,,,,,,,,,,,, +M2,2,ttft,cold2k-off,cold2k-off,,,1.262,,,,,,,,,,,,, +M2,2,ttft,cold8k-off,cold8k-off,,,4.632,,,,,,,,,,,,, +M2,2,ttft,cold32k-off,cold32k-off,,,18.42,,,,,,,,,,,,, +M2,2,ttft64,cold64k-off,cold64k-off,,,33.175,,,,,,,,,,,,, +M2,2,ttft64,cold64k-off,cold64k-off,,,37.718,,,,,,,,,,,,, +M2,8,ttft1,cold2k-off,cold2k-off,,,0.968,,,,,,,,,,,,, +M2,8,ttft1,cold8k-off,cold8k-off,,,3.434,,,,,,,,,,,,, +M2,8,ttft1,cold32k-off,cold32k-off,,,14.15,,,,,,,,,,,,, +M2,8,ttft1,cold64k-off,cold64k-off,,,28.967,,,,,,,,,,,,, +M2,2,cache,bench_cache,first_ttft_s,,,1.2209141689818352,,,,,,,,,,,,,2571 +M2,8,cache,bench_cache,first_ttft_s,,,1.1495070881210268,,,,,,,,,,,,,2571 +B1,2,ttft,cold2k-off,cold2k-off,,,0.972,,,,,,,,,,,,, +B1,2,ttft,cold8k-off,cold8k-off,,,3.664,,,,,,,,,,,,, +B1,2,ttft,cold32k-off,cold32k-off,,,14.98,,,,,,,,,,,,, +B1,2,ttft,cold2k-off,cold2k-off,,,1.053,,,,,,,,,,,,, +B1,2,ttft,cold8k-off,cold8k-off,,,3.989,,,,,,,,,,,,, +B1,2,ttft,cold32k-off,cold32k-off,,,16.114,,,,,,,,,,,,, +B1,2,ttft64,cold64k-off,cold64k-off,,,30.665,,,,,,,,,,,,, +B1,2,ttft64,cold64k-off,cold64k-off,,,32.78,,,,,,,,,,,,, +B1,8,ttft1,cold2k-off,cold2k-off,,,0.969,,,,,,,,,,,,, +B1,8,ttft1,cold8k-off,cold8k-off,,,3.453,,,,,,,,,,,,, +B1,8,ttft1,cold32k-off,cold32k-off,,,14.124,,,,,,,,,,,,, +B1,8,ttft1,cold64k-off,cold64k-off,,,28.896,,,,,,,,,,,,, +B1,2,cache,bench_cache,first_ttft_s,,,1.1424408501479775,,,,,,,,,,,,,2571 +B1,8,cache,bench_cache,first_ttft_s,,,1.1430892788339406,,,,,,,,,,,,,2571 +B2,2,ttft,cold2k-off,cold2k-off,,,0.96,,,,,,,,,,,,, +B2,2,ttft,cold8k-off,cold8k-off,,,3.685,,,,,,,,,,,,, +B2,2,ttft,cold32k-off,cold32k-off,,,15.039,,,,,,,,,,,,, +B2,2,ttft,cold2k-off,cold2k-off,,,1.046,,,,,,,,,,,,, +B2,2,ttft,cold8k-off,cold8k-off,,,3.987,,,,,,,,,,,,, +B2,2,ttft,cold32k-off,cold32k-off,,,16.094,,,,,,,,,,,,, +B2,2,ttft64,cold64k-off,cold64k-off,,,30.682,,,,,,,,,,,,, +B2,2,ttft64,cold64k-off,cold64k-off,,,32.786,,,,,,,,,,,,, +B2,8,ttft1,cold2k-off,cold2k-off,,,0.966,,,,,,,,,,,,, +B2,8,ttft1,cold8k-off,cold8k-off,,,3.414,,,,,,,,,,,,, +B2,8,ttft1,cold32k-off,cold32k-off,,,14.1,,,,,,,,,,,,, +B2,8,ttft1,cold64k-off,cold64k-off,,,28.984,,,,,,,,,,,,, +B2,2,cache,bench_cache,first_ttft_s,,,1.1431879128795117,,,,,,,,,,,,,2571 +B2,8,cache,bench_cache,first_ttft_s,,,1.1406444171443582,,,,,,,,,,,,,2571 +1/B1,pool,startup,32768/8,,,,,,3072,,,,,,,,,,, +1/B2,pool,startup,32768/8,,,,,,3072,,,,,,,,,,, +1/M1,pool,startup,32768/8,,,,,,1536,,,,,,,,,,, +1/M2,pool,startup,32768/8,,,,,,1536,,,,,,,,,,, +2/B1,pool,startup,131072/4,,,,,,3072,,,,,,,,,,, +2/B2,pool,startup,131072/4,,,,,,3072,,,,,,,,,,, +2/M1,pool,startup,131072/4,,,,,,1536,,,,,,,,,,, +2/M2,pool,startup,131072/4,,,,,,1536,,,,,,,,,,, +3/B/off,pool,startup,32768/8,,,,,,3072,,,,,,,,,,, +3/B/on,pool,startup,32768/8,,,,,,3072,,,,,,,,,,, +3/M/off,pool,startup,32768/8,,,,,,1536,,,,,,,,,,, +3/M/on,pool,startup,32768/8,,,,,,1536,,,,,,,,,,, +4/BD,pool,startup,32768/8,,,,,,3072,,,,,,,,,,, +4/MD,pool,startup,32768/8,,,,,,1536,,,,,,,,,,, +8/B1,pool,startup,131072/1,,,,,,8192,,,,,,,,,,, +8/B2,pool,startup,131072/1,,,,,,8192,,,,,,,,,,, +8/M1,pool,startup,131072/1,,,,,,8192,,,,,,,,,,, +8/M2,pool,startup,131072/1,,,,,,8192,,,,,,,,,,, +9/BP,pool,startup,131072/8,,,,,,,,,,,,,,,,, +9/MP,pool,startup,131072/8,,,,,,,,,,,,,,,,, diff --git a/docs/receipts/flashnext-prompt-chunk3072/exactness.md b/docs/receipts/flashnext-prompt-chunk3072/exactness.md new file mode 100644 index 00000000000..e7957f7f2d7 --- /dev/null +++ b/docs/receipts/flashnext-prompt-chunk3072/exactness.md @@ -0,0 +1,20 @@ +# Exactness on the served server + +Each contract on both arms with the prompt cache on and off (`bench_concurrent.py --alone --serial` at temperatures 1.0 and 0 and on the six golden prompts; `served_exactness.py drafts`, `turns`, and `fresh` against the cache-on turns), then the greedy replies compared across the arms. + +- B cache on concurrent: concurrent equals solo 156/0 (unverified 0); drafted equals `"draft": false` 56/0 +- B cache on golden: concurrent equals solo 13/0 (unverified 0); drafted equals `"draft": false` 24/0 +- B cache on drafts: 8 of 8 equal to `"draft": false` (basis token_ids) +- B cache on turns: resumed from cached prompt tokens [13, 16, 16, 13, 24, 18, 3406] +- B cache off drafts: 8 of 8 equal to `"draft": false` (basis token_ids) +- B cache off fresh: 7 of 7 resumed replies equal the fresh ones +- M cache on concurrent: concurrent equals solo 156/0 (unverified 0); drafted equals `"draft": false` 56/0 +- M cache on golden: concurrent equals solo 13/0 (unverified 0); drafted equals `"draft": false` 24/0 +- M cache on drafts: 8 of 8 equal to `"draft": false` (basis token_ids) +- M cache on turns: resumed from cached prompt tokens [13, 16, 16, 13, 24, 18, 3406] +- 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 +- B vs M: replies equal over pass A's greedy cells (token_sha): 528 equal, 0 unequal + +- the cold cells: 8 of 8 at four slots and 4 of 4 at one slot carry the same token ids and reply text on all four boots (`coldhash.py`, the same prompt bytes on every arm) +- drafts off: 42 replies equal, 0 unequal diff --git a/docs/receipts/flashnext-prompt-chunk3072/noise-rule.md b/docs/receipts/flashnext-prompt-chunk3072/noise-rule.md new file mode 100644 index 00000000000..5130473f8b8 --- /dev/null +++ b/docs/receipts/flashnext-prompt-chunk3072/noise-rule.md @@ -0,0 +1,49 @@ +# The noise rule, and the cells it judged + +A cell is SLOWER when both after runs are under both before runs (for a latency: over) and the mean gap is over 1%; FASTER when both are over both and the gap is over 1%; LEVEL otherwise. Each arm booted twice per timed pass (`M1 B1 B2 M2`). A flagged cell gets one rerun of its invocation on fresh boots inside the same window, and a repeat of slower is a loss; this window flagged none. + +- cold prompts: 9 faster, 0 level, 0 slower, 0 unmeasured +- decode: 0 faster, 12 level, 0 slower, 0 unmeasured +- one-stream control: 0 faster, 5 level, 0 slower, 0 unmeasured +- losses: none + +## Cold prompts, four slots + +| prompt | load | M1 | M2 | B1 | B2 | B vs M | +|---|---|---|---|---|---|---| +| cold2k-off | idle | 1.070 | 1.071 | 0.972 | 0.960 | -9.8% faster | +| cold2k-off | load3 | 1.273 | 1.262 | 1.053 | 1.046 | -17.2% faster | +| cold8k-off | idle | 4.035 | 3.986 | 3.664 | 3.685 | -8.4% faster | +| cold8k-off | load3 | 4.653 | 4.632 | 3.989 | 3.987 | -14.1% faster | +| cold32k-off | idle | 16.015 | 16.044 | 14.980 | 15.039 | -6.4% faster | +| cold32k-off | load3 | 18.249 | 18.420 | 16.114 | 16.094 | -12.2% faster | +| cold64k-off | idle | 33.058 | 33.175 | 30.665 | 30.682 | -7.4% faster | +| cold64k-off | load3 | 37.582 | 37.718 | 32.780 | 32.786 | -12.9% faster | +| first_ttft_s | idle | 1.241 | 1.221 | 1.142 | 1.143 | -7.2% faster | + +## The one-stream control + +| prompt | load | M1 | M2 | B1 | B2 | B vs M | +|---|---|---|---|---|---|---| +| cold2k-off | idle | 0.979 | 0.968 | 0.969 | 0.966 | -0.6% level | +| cold8k-off | idle | 3.458 | 3.434 | 3.453 | 3.414 | -0.4% level | +| cold32k-off | idle | 14.144 | 14.150 | 14.124 | 14.100 | -0.2% level | +| cold64k-off | idle | 29.025 | 28.967 | 28.896 | 28.984 | -0.2% level | +| first_ttft_s | idle | 1.148 | 1.150 | 1.143 | 1.141 | -0.6% level | + +## Decode + +| prompt | streams | M1 | M2 | B1 | B2 | B vs M | tokens a round, M / B | +|---|---|---|---|---|---|---|---| +| chat | 1 | 46.9 | 46.2 | 46.5 | 46.3 | -0.3% level | 2.81 / 2.81 | +| chat | 2 | 86.8 | 86.2 | 86.3 | 86.3 | -0.2% level | 2.51 / 2.51 | +| chat | 4 | 156.6 | 155.1 | 155.7 | 155.7 | -0.1% level | 2.51 / 2.51 | +| chat | 6 | 211.0 | 209.2 | 209.8 | 209.5 | -0.2% level | 2.51 / 2.51 | +| chat | 8 | 258.1 | 256.1 | 256.9 | 256.5 | -0.2% level | 2.51 / 2.51 | +| code | 1 | 73.1 | 71.9 | 72.3 | 72.5 | -0.1% level | 5.22 / 5.22 | +| code | 2 | 138.8 | 136.9 | 138.0 | 137.5 | -0.1% level | 4.83 / 4.83 | +| code | 4 | 244.9 | 240.8 | 242.7 | 241.6 | -0.3% level | 4.83 / 4.83 | +| code | 6 | 310.5 | 306.5 | 307.7 | 306.2 | -0.5% level | 4.49 / 4.49 | +| code | 8 | 374.5 | 370.5 | 370.8 | 371.7 | -0.3% level | 4.49 / 4.49 | +| fibonacci-raw | 1 | 73.4 | 72.2 | 72.7 | 72.6 | -0.2% level | 5.22 / 5.22 | +| gpu-chat-no-think | 1 | 47.0 | 46.3 | 46.5 | 46.5 | -0.3% level | 2.81 / 2.81 | diff --git a/docs/recipes/flash-next-cuda.md b/docs/recipes/flash-next-cuda.md index 8815ec610af..d31ccdf5247 100644 --- a/docs/recipes/flash-next-cuda.md +++ b/docs/recipes/flash-next-cuda.md @@ -91,7 +91,7 @@ tensorfold-native serve /path/to/checkpoint --temperature 0 - `--no-drafts` serves without the MTP head; `"draft": false` on a request does the same for that request. - `TF_FLASHNEXT_READOUT_ROWS` (default 64, `0` never) is the rows from which the hyper-connection read-out takes the prompt tiles. Same bits either way; `128` keeps 16-stream decode rounds on the row kernels, up to 7% quicker on a GB10. -- `TF_FLASHNEXT_CHUNK` (1 to 16,384, default 1,536) caps the rows of one prompt pass. Every pass reads the weights +- `TF_FLASHNEXT_CHUNK` (1 to 16,384, default 3,072) caps the rows of one prompt pass. Every pass reads the weights of every expert its rows route to, so fewer, larger passes cost less a token; but a pass runs to its end, so a stream decoding during a pass waits up to the whole pass for its next token. See [Prompt chunk size](#prompt-chunk-size). @@ -244,7 +244,7 @@ in one start of four on this machine. After the 131,072-token prompt the process ### Prompt chunk size `TF_FLASHNEXT_CHUNK` sets how many prompt rows one pass takes. A prompt of N rows runs in ceil(N / chunk) passes: a -1,922-row prompt is two passes at the default 1,536 and one at 8,192. +1,922-row prompt is two passes at 1,536 and one at the default 3,072 or at 8,192. - **For:** every pass reads the gate, up and down weights of each routed expert its rows route to (1.84 MB of gate and up weights an expert, 512 experts a layer). Fewer passes read each expert fewer times, so a prompt longer than one @@ -265,7 +265,7 @@ change the sums. Two costs remain: On one GB10 with `nvidia/Qwen3.8-Flash-Next-FP4-Local` and `--parallel 3 --context 40960 --no-drafts --prompt-cache-gib 0` (two fresh servers for each setting, medians): -| | unset (1,536) | `TF_FLASHNEXT_CHUNK_SOLO=4096` | `=8192` | +| | `TF_FLASHNEXT_CHUNK_SOLO` unset, chunk 1,536 | `TF_FLASHNEXT_CHUNK_SOLO=4096` | `=8192` | | --- | --- | --- | --- | | First token, one stream, at 1,927 / 8,320 / 31,736 tokens | 0.99 / 4.07 / 16.05 s | 0.91 / 3.88 / 14.94 s | 0.89 / 3.86 / 14.78 s | | Longest wait between a decoding stream's tokens while an 8k prompt runs | 0.81 s | 0.81 s | 0.85 s | diff --git a/zig/src/families/flashnext/cuda_batch.zig b/zig/src/families/flashnext/cuda_batch.zig index bd1b87e63fb..e1ce349aa16 100644 --- a/zig/src/families/flashnext/cuda_batch.zig +++ b/zig/src/families/flashnext/cuda_batch.zig @@ -583,9 +583,9 @@ pub fn Pass(comptime Tri: type) type { }; } -test "a pass's scratch at the dense cap stays near a gigabyte and a half" { +test "a pass's scratch at the default chunk stays under three gigabytes" { _ = @import("cuda_paging_batch_test.zig"); - try std.testing.expect(Scratch.bytes(default_chunk, .{ .head_rows = default_chunk }) < 2 << 30); + try std.testing.expect(Scratch.bytes(default_chunk, .{ .head_rows = default_chunk }) < 3 << 30); try std.testing.expect(Scratch.bytes(1, .{}) < Scratch.bytes(2, .{})); try std.testing.expect(Scratch.bytes(4096, .{ .lane_reduce = false }) < Scratch.bytes(4096, .{})); } diff --git a/zig/src/families/flashnext/cuda_batch_state.zig b/zig/src/families/flashnext/cuda_batch_state.zig index 959ced3b65b..80d3d073b76 100644 --- a/zig/src/families/flashnext/cuda_batch_state.zig +++ b/zig/src/families/flashnext/cuda_batch_state.zig @@ -53,7 +53,7 @@ pub const attn_lanes: usize = 32; pub const lanes_ptrs: usize = (wt.layers_n / 4) * attn_lanes * 4 * 8; pub const lanes_bytes: usize = lanes_ptrs + (4 * attn_lanes + att_rows) * 4; /// Rows a pass holds at most unless asked for more. -pub const default_chunk: usize = 1536; +pub const default_chunk: usize = 3072; /// The most rows TF_FLASHNEXT_CHUNK may ask for (the largest measured pass; its scratch is about 13 GiB more). pub const max_chunk: usize = 16384; const ratio = wt.index_ratio; diff --git a/zig/src/families/flashnext/cuda_chunk.zig b/zig/src/families/flashnext/cuda_chunk.zig index 65577d1f157..c6af34e07f5 100644 --- a/zig/src/families/flashnext/cuda_chunk.zig +++ b/zig/src/families/flashnext/cuda_chunk.zig @@ -44,7 +44,8 @@ pub fn readoutTileRows() usize { test "TF_FLASHNEXT_CHUNK takes 1 to the largest measured pass and refuses the rest" { try std.testing.expectEqual(batch.default_chunk, try parseChunk(null)); - try std.testing.expectEqual(@as(usize, 1536), try parseChunk("")); + try std.testing.expectEqual(batch.default_chunk, try parseChunk("")); + try std.testing.expectEqual(@as(usize, 1536), try parseChunk("1536")); try std.testing.expectEqual(@as(usize, 4096), try parseChunk("4096")); try std.testing.expectEqual(batch.max_chunk, try parseChunk("16384")); for ([_][]const u8{ "0", "16385", "-1", "4k" }) |bad| try std.testing.expectError(error.BadChunk, parseChunk(bad)); diff --git a/zig/src/families/glm/cuda_sampler.zig b/zig/src/families/glm/cuda_sampler.zig index c3bfacdbaac..3e34a2e0bd5 100644 --- a/zig/src/families/glm/cuda_sampler.zig +++ b/zig/src/families/glm/cuda_sampler.zig @@ -14,12 +14,11 @@ pub fn check(s: ?Sampling) ?Sampling { return if (x.temperature > 0) x else null; } -/// sample.cu's Rule: the seed, 1 / T, top_p, the candidates' near window, ln(min_p) and top_k (0: off). -pub const Rule = extern struct { seed: u64, inv_t: f32, top_p: f32, near: f32, min_log: f32, top_k: u32, pad: u32 = 0 }; +pub const Rule = cuda.sample.Rule; -/// The rule as nemotron/cuda_sampler.zig fills it. +/// The rule sample.cu reads for `s` (cuda.sample.rule over lanes.gpu_rule). pub fn rule(s: Sampling) Rule { - return .{ .seed = s.seed, .inv_t = @floatCast(1.0 / @max(s.temperature, 1e-6)), .top_p = @floatCast(s.top_p), .near = 20.0, .min_log = @floatCast(s.minLog()), .top_k = s.top_k }; + return cuda.sample.rule(s); } /// A rank's logits rows: bf16 [rows][its vocabulary span]. @@ -160,6 +159,31 @@ test "rules as nemotron/cuda_sampler fills them; temperature 0 is greedy" { try std.testing.expectEqual(@as(?Sampling, null), check(.{ .seed = 0, .temperature = 0 })); } +test "GLM sampler rule: the hand-built rule it replaced, byte for byte; check is ruleFor's gate" { + const Hand = extern struct { seed: u64, inv_t: f32, top_p: f32, near: f32, min_log: f32, top_k: u32, pad: u32 = 0 }; + const inf = std.math.inf(f64); + const seeds = [_]u64{ 0, 1234, 1 << 63 | 5, std.math.maxInt(u64) }; + const temps = [_]f64{ -1, 0, 1e-9, 1e-6, 0.5, 0.7, 1, 2, inf }; + const ks = [_]u32{ 0, 1, 20, 1024, 1025 }; + const ps = [_]f64{ 0, 0.5, 0.95, 1, 1.5 }; + const mins = [_]f64{ -1, 0, 1e-12, 0.05, 1 }; + var sampled: usize = 0; + for (seeds) |seed| for (temps) |t| for (ks) |k| for (ps) |p| for (mins) |m| { + const s: Sampling = .{ .seed = seed, .temperature = t, .top_k = k, .top_p = p, .min_p = m }; + const h: Hand = .{ .seed = s.seed, .inv_t = @floatCast(1.0 / @max(s.temperature, 1e-6)), .top_p = @floatCast(s.top_p), .near = 20.0, .min_log = @floatCast(s.minLog()), .top_k = s.top_k }; + const r = rule(s); + try std.testing.expectEqualSlices(u8, std.mem.asBytes(&h), std.mem.asBytes(&r)); + const via = cuda.sample.ruleFor(@as(?Sampling, s)); + try std.testing.expectEqual(check(s) == null, via == null); + if (via) |v| { + try std.testing.expectEqualSlices(u8, std.mem.asBytes(&h), std.mem.asBytes(&v)); + sampled += 1; + } + }; + try std.testing.expectEqual(@as(usize, 4 * 7 * 5 * 5 * 5), sampled); + try std.testing.expect(cuda.sample.ruleFor(@as(?Sampling, null)) == null); +} + /// Each rank's columns as one block from that rank (spans may differ in width: TP=3), in one group: world broadcasts. const Spread = struct { lib: *const cuda.nccl.Library, diff --git a/zig/src/native/cuda_startup_test.zig b/zig/src/native/cuda_startup_test.zig index 1a7d265a810..a9630daaef9 100644 --- a/zig/src/native/cuda_startup_test.zig +++ b/zig/src/native/cuda_startup_test.zig @@ -50,7 +50,7 @@ fn check(value: ?[:0]const u8, comptime rows: u32) !void { } test "CUDA startup: unset Flash Next chunk reports the default stored pass" { - try check(null, 1536); + try check(null, 3072); } test "CUDA startup: Flash Next chunk 4096 reports the stored pass" { diff --git a/zig/src/platform/environment.zig b/zig/src/platform/environment.zig index 438892aa48c..f77f1225c06 100644 --- a/zig/src/platform/environment.zig +++ b/zig/src/platform/environment.zig @@ -4,7 +4,7 @@ const sys = @import("sys.zig"); pub const Error = error{InvalidEnvironment}; pub const Scope = enum { common, cuda, metal, xpu, slide, hooks, dsv41, gemma4, drama3, glm, flashnext, nemotron, minimax_m3 }; -pub const Kind = enum { u32, u64, size, percent, positive, device, segments, mib, bit_width, word, prompt, flag, toggle, real, positive_real, signed_real, range, path }; +pub const Kind = enum { u32, u64, size, percent, positive, device, segments, mib, bit_width, word, prompt, flag, toggle, real, positive_real, signed_real, range, path, parallel }; pub const Rule = struct { name: [:0]const u8, scope: Scope, @@ -38,6 +38,7 @@ pub const rules = [_]Rule{ .{ .name = "TENSORFOLD_DEVICE_CHECK", .scope = .common, .kind = .flag, .forms = "0 or 1 (0 skips serve's device-profile check; default 1)" }, .{ .name = "TENSORFOLD_VIDEO_TOKENS", .scope = .common, .kind = .u64, .forms = "a nonnegative whole video-token budget (default 16384; unsigned 64-bit count)" }, .{ .name = "TENSORFOLD_MAX_PDF_PAGES", .scope = .common, .kind = .positive, .forms = "a whole number from 1 to 4294967295 (capped by --vision-max-images)" }, + .{ .name = "TENSORFOLD_PARALLEL", .scope = .common, .kind = .parallel, .trim = " \t", .min = 1, .max = 4096, .forms = "a whole number from 1 to 4096, or auto (up to 8 lanes; the --parallel flag wins when present)" }, .{ .name = "TENSORFOLD_PRESSURE_CACHE_PERCENT", .scope = .common, .kind = .percent, .trim = " \t", .forms = "a whole percentage from 0 to 100" }, .{ .name = "TENSORFOLD_PREFIX_WAIT_MS", .scope = .common, .kind = .u32, .trim = " \t", .first_digit = true, .forms = "whole milliseconds from 0 to 4294967295 (0 disables waiting)" }, .{ .name = "TENSORFOLD_RESTORE_PREFETCH", .scope = .common, .kind = .u64, .trim = " \t", .first_digit = true, .forms = "a whole unsigned 64-bit read count (0 disables reading ahead; capped at 65536)" }, @@ -205,7 +206,7 @@ pub const rules = [_]Rule{ .{ .name = "TF_FLASHNEXT_GREEDY_WIDE", .scope = .flashnext, .kind = .size, .forms = "a whole round width from 0 to the native size maximum (default 4)" }, .{ .name = "TF_FLASHNEXT_WINDOW_ROWS", .scope = .flashnext, .kind = .size, .forms = "a whole row count from 0 to the native size maximum (default the shared round's rows; capped by them)" }, .{ .name = "TF_FLASHNEXT_READOUT_ROWS", .scope = .flashnext, .kind = .size, .forms = "a whole row count from 0 to the native size maximum (default 64; 0 never takes the tiles)" }, - .{ .name = "TF_FLASHNEXT_CHUNK", .scope = .flashnext, .kind = .size, .min = 1, .max = 16384, .forms = "whole prompt rows a pass, 1 to 16384 (default 1536)" }, + .{ .name = "TF_FLASHNEXT_CHUNK", .scope = .flashnext, .kind = .size, .min = 1, .max = 16384, .forms = "whole prompt rows a pass, 1 to 16384 (default 3072)" }, .{ .name = "TF_FLASHNEXT_CHUNK_SOLO", .scope = .flashnext, .kind = .size, .max = 16384, .forms = "whole prompt rows a pass with no stream decoding, TF_FLASHNEXT_CHUNK to 16384 (unset: 8192 alone at --parallel 1, else off)" }, .{ .name = "TF_FLASHNEXT_FACT_ROWS", .scope = .flashnext, .kind = .size, .forms = "a whole row count from 0 to the native size maximum (default 64; 0 never takes the tiles)" }, .{ .name = "TF_FLASHNEXT_KV_PAGE_TEST", .scope = .flashnext, .kind = .size, .forms = "a page size the kernels take, whole blocks of the indexer's ratio up to the window (unset: off)" }, @@ -244,6 +245,13 @@ pub fn parse(name: []const u8, raw: ?[]const u8) Error!?u64 { for (r.words, 0..) |v, i| if (std.mem.eql(u8, text, v)) return i; return error.InvalidEnvironment; } + // "auto" travels as 0: the caller reads it back through `parallel`, never the raw number + if (r.kind == .parallel) { + if (std.ascii.eqlIgnoreCase(text, "auto")) return 0; + const n = std.fmt.parseInt(u64, text, 10) catch return error.InvalidEnvironment; + if (n < r.min or n > r.max) return error.InvalidEnvironment; + return n; + } if (r.kind == .toggle) return toggle(r, text) orelse error.InvalidEnvironment; // a path travels as 0, its text read by `path`; the file itself is its reader's to check if (r.kind == .path) return if (for (text) |ch| { @@ -372,6 +380,14 @@ pub fn device(flag: ?u32, raw: ?[]const u8) Error!u32 { return @intCast((try checked("TF_CUDA_DEVICE", raw)) orelse 0); } +/// TENSORFOLD_PARALLEL's text: the flag wins, else the trimmed setting, else null; bad values are refused by name. +pub fn parallel(flag: ?[]const u8, raw: ?[]const u8) Error!?[]const u8 { + if (flag) |f| return f; + _ = try checked("TENSORFOLD_PARALLEL", raw); + const text = std.mem.trim(u8, raw orelse return null, rule("TENSORFOLD_PARALLEL").trim); + return if (text.len == 0) null else text; +} + pub const Flags = struct { cuda: bool = false, metal: bool = false, @@ -388,6 +404,8 @@ pub const Flags = struct { device: ?u32 = null, segments: ?u32 = null, carveout: bool = false, + /// --parallel named: TENSORFOLD_PARALLEL then stays out of the way, as a flag-named device does. + parallel_named: bool = false, fn includes(f: Flags, scope: Scope) bool { return switch (scope) { @@ -414,6 +432,7 @@ pub fn refusal(a: std.mem.Allocator, environment: anytype, flags: Flags) !?[]con if (!flags.includes(r.scope) or masked(r, environment)) continue; if (std.mem.eql(u8, r.name, "TF_CUDA_DEVICE") and flags.device != null) continue; if (std.mem.eql(u8, r.name, "TF_CUDA_SEGMENTS") and flags.segments != null) continue; + if (std.mem.eql(u8, r.name, "TENSORFOLD_PARALLEL") and flags.parallel_named) continue; if (std.mem.eql(u8, r.name, "TF_CUDA_CARVEOUT") and flags.carveout) continue; if (std.mem.eql(u8, r.name, "TF_CUDA_CARVEOUT_MIB") and !flags.carveout and !std.mem.eql(u8, environment.get("TF_CUDA_CARVEOUT") orelse "0", "1")) continue; diff --git a/zig/src/platform/environment_test.zig b/zig/src/platform/environment_test.zig index de938481524..b7003ee9947 100644 --- a/zig/src/platform/environment_test.zig +++ b/zig/src/platform/environment_test.zig @@ -135,6 +135,11 @@ const valid = [_]Case{ .{ .name = "TF_NEMOTRON_WORLD", .raw = "2", .n = 2 }, .{ .name = "TF_MINIMAX_RANK", .raw = "1", .n = 1 }, .{ .name = "TF_MINIMAX_WORLD", .raw = "2", .n = 2 }, + .{ .name = "TENSORFOLD_PARALLEL", .raw = "4", .n = 4 }, + .{ .name = "TENSORFOLD_PARALLEL", .raw = " 8\t", .n = 8 }, + .{ .name = "TENSORFOLD_PARALLEL", .raw = "4096", .n = 4096 }, + .{ .name = "TENSORFOLD_PARALLEL", .raw = "auto", .n = 0 }, + .{ .name = "TENSORFOLD_PARALLEL", .raw = "AUTO", .n = 0 }, }; test "environment: valid forms remain raw numbers before existing consumer clamps and defaults" { @@ -353,3 +358,19 @@ test "environment: the Metal Qwen and slide-learning switches take their forms, try t.expect(try e.value(bool, "TF_QWEN27_JOIN", null, true) and !(try e.value(bool, "TF_QWEN27_JOIN", "0", true))); try t.expect(try e.value(bool, "TF_QWEN35_METAL_KV_PAGES", "1", false)); } + +test "environment: TENSORFOLD_PARALLEL takes the flag over the setting, blank keeps the default, garbage is refused" { + try t.expectEqualStrings("4", (try e.parallel("4", "8")).?); + try t.expectEqualStrings("auto", (try e.parallel("auto", "8")).?); + try t.expectEqualStrings("8", (try e.parallel(null, " 8 ")).?); + try t.expectEqualStrings("Auto", (try e.parallel(null, "Auto")).?); // the text passes through; cli.parallel matches it without case + try t.expect((try e.parallel(null, null)) == null); + try t.expect((try e.parallel(null, " \t")) == null); + for ([_][]const u8{ "0", "4097", "typo", "-1", "8\nwrong" }) |raw| { + try t.expectError(error.InvalidEnvironment, e.parallel(null, raw)); + const why = (try e.refusal(a, Env{ .name = "TENSORFOLD_PARALLEL", .raw = raw }, all)).?; + defer a.free(why); + try t.expect(std.mem.indexOf(u8, why, "TENSORFOLD_PARALLEL") != null); + try t.expect(std.mem.indexOf(u8, why, "or auto") != null); + } +} diff --git a/zig/src/server/chat.zig b/zig/src/server/chat.zig index 8a591e5ddf6..69b962a0516 100644 --- a/zig/src/server/chat.zig +++ b/zig/src/server/chat.zig @@ -275,7 +275,7 @@ pub fn generate(srv: *Server, cx: *Cx, prepared: Prepared, sink: ?Sink, gone: an }; srv.release(preparing, null); // a background request waits only while a foreground one prepares preparing = false; - var gen: Generation = .{ .srv = srv, .a = a, .box = &box, .id = id, .asked = input.cancel, .reply_id = input.id, .seed = if (request.sampling) |v| v.seed else null, .sink = sink, .thinking = prepared.reasoning or (srv.text.vtable.format != .kimi and srv.info.capability(.reasoning) and reply_text.isChannel(srv.markers)), .chat = input.prompt == null, .stops = .{ .strings = stops_opt.strings }, .ignore_eos = stops_opt.ignore_eos, .max_tokens = request.max_tokens, .tools = input.tools, .token_ids = prepared.token_ids, .lp = if (request.logprobs != null) .{ .positions = if (sink) |s| s.tokens != null else false } else null, .loop_stop = request.loop_stop, .single = flag(input.fields, "tool_call_single") orelse false, .recover = input.prompt == null and request.structure == null }; + var gen: Generation = .{ .srv = srv, .a = a, .box = &box, .id = id, .asked = input.cancel, .reply_id = input.id, .seed = if (request.sampling) |v| v.seed else null, .sink = sink, .thinking = prepared.reasoning or (srv.text.vtable.format != .kimi and srv.info.capability(.reasoning) and reply_text.isChannel(srv.markers)), .chat = input.prompt == null, .stops = .{ .strings = stops_opt.strings }, .ignore_eos = stops_opt.ignore_eos, .max_tokens = request.max_tokens, .tools = input.tools, .token_ids = prepared.token_ids, .lp = if (request.logprobs != null) .{ .positions = if (sink) |s| s.tokens != null else false } else null, .loop_stop = request.loop_stop, .single = flag(input.fields, "tool_call_single") orelse false, .required = flag(input.fields, "tool_call_required") orelse false, .recover = input.prompt == null and request.structure == null }; defer srv.noteRequest(prepared.prompt_len, cached_tokens.within(box.cached, prepared.prompt_len), gen.collected.items.len, box.stats.drafted, box.stats.accepted, box.stats.rounds, received, gen.first_ns, gen.last_ns, box.stats.prefill_seconds, if (box.stats.prefill_seconds != null) box.stats.queued_seconds else null); errdefer if (!gen.engine_done) gen.cancel(); // the engine writes to the mailbox until it says finished const result: Failure!Reply = blk: { diff --git a/zig/src/server/chat_generation.zig b/zig/src/server/chat_generation.zig index 3868f69293c..a1d16ffa84b 100644 --- a/zig/src/server/chat_generation.zig +++ b/zig/src/server/chat_generation.zig @@ -52,6 +52,7 @@ pub const Generation = struct { ignore_eos: bool, max_tokens: u32, single: bool, // parallel_tool_calls: false: a cut call is no call, so its prose ends as the whole reply's does + required: bool = false, // tool_choice "required" or a named function: the reply must call a tool token_ids: bool, // "return_token_ids": the response's tensorfold block carries the reply's token ids tools: []const Value, calls: ?tool_stream.Streamer = null, @@ -317,10 +318,18 @@ pub const Generation = struct { reasoning = h.reasoning; } if (native == null) content = try tool_lfm2.dsml(a, content, g.tools, true); - // the model ended its turn inside a think block that never closed: its reasoning is also its answer (#3687) - const recovered = g.recover and reasoning != null and reply_text.pyStrip(content).len == 0 and g.endedOnEos(reason, raw, stopped) and - (if (native) |n| n.open_think and n.calls == null else neverClosed(text, g.srv.markers, close_at)) and !try g.callIn(reasoning.?); - if (recovered) content = reasoning.?; + // Recovery only when reasoning was asked: no tools, no grammar; the unclosed-think EOS text becomes content. + const recovered = g.recover and g.thinking and reasons and reasoning != null and + reply_text.pyStrip(content).len == 0 and + g.endedOnEos(reason, raw, stopped) and + !g.required and m.stats.grammar == null and + (g.calls == null or !g.calls.?.streamed) and + (if (native) |n| n.open_think and n.calls == null else neverClosed(text, g.srv.markers, close_at)) and + !try g.callIn(reasoning.?); + if (recovered) { + log.line("warning: a reply ended inside an unclosed think block on its own end-of-sequence, so its text is returned as the answer too", .{}); + content = reasoning.?; + } if (g.sink) |sink| { const thought = g.streamed_reasoning.text.items; if (reasoning) |r| if (std.mem.startsWith(u8, r, thought) and r.len > thought.len) diff --git a/zig/src/server/cli.zig b/zig/src/server/cli.zig index cd80de16cad..8e2fa7a8f18 100644 --- a/zig/src/server/cli.zig +++ b/zig/src/server/cli.zig @@ -244,6 +244,7 @@ pub const Args = struct { slide_drift_gate: bool = false, slide_graph: ?[]const u8 = null, // the Sliding Weights fact graph's file (null: memory only) parallel: []const u8 = "auto", + parallel_named: bool = false, // --parallel was on the command line: TENSORFOLD_PARALLEL then stays out of the way backend: []const u8 = "auto", device: ?u32 = null, segments: ?u32 = null, @@ -488,7 +489,9 @@ fn apply(a: Allocator, out: *Args, name: []const u8, value: ?[]const u8, u: *Usa const n = try int(u, a, name, v); if (n < 0) return fail(u, a, "argument --compact-keep: expected a token count from 0: '{s}'", .{v}); out.compact_keep = @intCast(n); - } else if (is(name, "--compact-memory")) out.compact_memory = v else if (is(name, "--slide")) out.slide = true else if (is(name, "--slide-drift-gate")) out.slide_drift_gate = true else if (is(name, "--slide-graph")) out.slide_graph = v else if (is(name, "--parallel")) out.parallel = v else if (is(name, "--backend")) out.backend = v else if (is(name, "--cortheon")) out.cortheon = true else if (is(name, "--vision")) out.vision = true else if (is(name, "--vision-urls")) out.vision_urls = true else if (is(name, "--vision-pdf")) out.vision_pdf = true else if (is(name, "--vision-max-images")) { + } else if (is(name, "--compact-memory")) out.compact_memory = v else if (is(name, "--slide")) out.slide = true else if (is(name, "--slide-drift-gate")) out.slide_drift_gate = true else if (is(name, "--slide-graph")) out.slide_graph = v else if (is(name, "--parallel")) { + out.parallel, out.parallel_named = .{ v, true }; + } else if (is(name, "--backend")) out.backend = v else if (is(name, "--cortheon")) out.cortheon = true else if (is(name, "--vision")) out.vision = true else if (is(name, "--vision-urls")) out.vision_urls = true else if (is(name, "--vision-pdf")) out.vision_pdf = true else if (is(name, "--vision-max-images")) { const n = try int(u, a, name, v); if (n < 1 or n > 64) return fail(u, a, "argument --vision-max-images: expected 1 to 64 images: '{s}'", .{v}); out.vision_max_images = @intCast(n); @@ -584,9 +587,7 @@ pub const capabilities = @import("cli_capabilities.zig").capabilities; pub const cudaFlag = @import("cli_gpu_flags.zig").cudaFlag; pub const gpuFlag = @import("cli_gpu_flags.zig").gpuFlag; pub const ranks = @import("cli_gpu_flags.zig").ranks; - const cli = @This(); - /// The private flag helpers cli_test.zig checks. pub const Test = if (builtin.is_test) struct { pub const cuda_build = cli.cuda_build; diff --git a/zig/src/server/deepseek_reply_test.zig b/zig/src/server/deepseek_reply_test.zig index d59da4f1587..9b2e91cbb1a 100644 --- a/zig/src/server/deepseek_reply_test.zig +++ b/zig/src/server/deepseek_reply_test.zig @@ -92,7 +92,7 @@ const Got = struct { }; /// What a client is left with: reasoning_content and content, joined across a stream's deltas. -const Seen = struct { reasoning: []const u8, content: []const u8, prompt_end: []const u8 }; +const Seen = struct { reasoning: []const u8, content: []const u8, prompt_end: []const u8, calls: bool = false }; fn ask(a: std.mem.Allocator, reply: []const u32, server_thinking: bool, extra: []const u8, stream: bool, stride: usize) !Seen { var fx = try Fixture.init(a); @@ -112,12 +112,14 @@ fn ask(a: std.mem.Allocator, reply: []const u32, server_thinking: bool, extra: [ const prompt_end = if (script.prompt.items.len > 0) try hf.text().tokenString(a, script.prompt.items[script.prompt.items.len - 1]) else ""; var reasoning: std.ArrayList(u8) = .empty; var content: std.ArrayList(u8) = .empty; + var calls = false; if (!stream) { try t.expectEqual(@as(u16, 200), got.status); const v = (try json.parseText(a, got.body)).ok; const m = v.get("choices").?.array[0].get("message").?; if (m.get("reasoning_content")) |r| if (r == .string) try reasoning.appendSlice(a, r.string); if (m.get("content")) |c| if (c == .string) try content.appendSlice(a, c.string); + if (m.get("tool_calls")) |tc| calls = tc == .array and tc.array.len > 0; } else for (got.events.items) |e| { const v = (try json.parseText(a, e)).ok; const choices = v.get("choices") orelse continue; @@ -125,8 +127,9 @@ fn ask(a: std.mem.Allocator, reply: []const u32, server_thinking: bool, extra: [ const d = choices.array[0].get("delta") orelse continue; if (d.get("reasoning_content")) |r| if (r == .string) try reasoning.appendSlice(a, r.string); if (d.get("content")) |c| if (c == .string) try content.appendSlice(a, c.string); + if (d.get("tool_calls")) |tc| calls = calls or (tc == .array and tc.array.len > 0); } - return .{ .reasoning = reasoning.items, .content = content.items, .prompt_end = prompt_end }; + return .{ .reasoning = reasoning.items, .content = content.items, .prompt_end = prompt_end, .calls = calls }; } fn ids(a: std.mem.Allocator, parts: []const Part) ![]const u32 { @@ -171,10 +174,14 @@ test "DeepSeek chat: reply splits, whole and streamed, thinking on and off" { // cut by the length while still reasoning: no content s = try probe(a, "on/cut", &.{ .{ .text = "Plan" }, .{ .text = "Hi" } }, true, ""); try t.expectEqualStrings("", s.content); - // ended (EOS) before any close: the reasoning is also the answer (#3687) + // ended (EOS) before any close: its reasoning text is the answer too s = try probe(a, "on/no-close", &.{ .{ .text = "Plan" }, .{ .id = eos } }, true, ""); try t.expectEqualStrings("Plan", s.reasoning); try t.expectEqualStrings("Plan", s.content); + // tools attached: a call envelope inside an unclosed block never becomes a call; the reply keeps empty content + s = try probe(a, "on/no-close-markup", &.{ .{ .text = "Plan[nope" ++ "<" ++ "|tool_call_" ++ "start" ++ "|>[lookup()]" ++ "<" ++ "|tool_call_" ++ "end" ++ "|>" }, .{ .id = eos } }, true, ",\"tools\":[{\"type\":\"function\",\"function\":{\"name\":\"lookup\",\"parameters\":{\"type\":\"object\"}}}],\"max_tokens\":256"); + try t.expectEqualStrings("", s.content); + try t.expect(!s.calls); // a reply that writes its own opener after the prompt's s = try probe(a, "on/own-opener", &.{ .{ .id = think_open }, .{ .text = "Plan" }, .{ .id = think_close }, .{ .text = "Hi" }, .{ .id = eos } }, true, ""); try t.expectEqualStrings("Hi", s.content); diff --git a/zig/src/server/engines.zig b/zig/src/server/engines.zig index dca07818f41..0c39d5801b9 100644 --- a/zig/src/server/engines.zig +++ b/zig/src/server/engines.zig @@ -13,7 +13,7 @@ pub const Opened = api.Opened; /// Refuse malformed settings before resolving checkpoints and again before an idle reload. pub fn environment(a: Allocator, args: cli.Args) !?[]const u8 { const settings = @import("sys").environment; - var flags: settings.Flags = .{ .slide = args.slide, .device = args.device, .segments = args.segments, .hooks = @import("build_options").slide_test_hooks }; + var flags: settings.Flags = .{ .slide = args.slide, .device = args.device, .segments = args.segments, .hooks = @import("build_options").slide_test_hooks, .parallel_named = args.parallel_named }; for (native.backends) |backend| { if (std.mem.eql(u8, backend, "cuda")) flags.cuda = true; if (std.mem.eql(u8, backend, "metal")) flags.metal = true; diff --git a/zig/src/server/main.zig b/zig/src/server/main.zig index 4cc71962897..049f8d86294 100644 --- a/zig/src/server/main.zig +++ b/zig/src/server/main.zig @@ -132,6 +132,8 @@ pub fn main(init: std.process.Init) !u8 { }; if (expanded.path) |path| log.line("config: {s} ({d} options)", .{ path, expanded.options }); if (try engines.environment(a, args)) |why| return fail(why); + // TENSORFOLD_PARALLEL names the default lanes once the environment check passed; the flag wins. + if (try @import("sys").environment.parallel(if (args.parallel_named) args.parallel else null, @import("sys").environment.Process.get(.{}, "TENSORFOLD_PARALLEL"))) |text| args.parallel = text; const receipt = @import("core").startup_receipt; receipt.enable(io, entry, try @import("sys").environment.read(bool, "TF_STARTUP_RECEIPT", false)); defer receipt.disable(); diff --git a/zig/src/server/thinking_request_test.zig b/zig/src/server/thinking_request_test.zig index 62e8ac93aed..45d16e08f7b 100644 --- a/zig/src/server/thinking_request_test.zig +++ b/zig/src/server/thinking_request_test.zig @@ -14,6 +14,7 @@ const Script = struct { seen: bool = false, prompt: [4096]u32 = undefined, count: usize = 0, + emit: []const u32 = &.{2}, fn engine(s: *Script) api.Engine { return .{ .ctx = s, .vtable = &.{ .info = info, .submit = submit, .cancel = cancel, .status = status, .memory = memory } }; @@ -28,7 +29,7 @@ const Script = struct { s.count = request.prompt.len; @memcpy(s.prompt[0..s.count], request.prompt); sink.event(sink.ctx, id, &.{ .prefilled = 0 }); - sink.event(sink.ctx, id, &.{ .tokens = &.{2} }); + sink.event(sink.ctx, id, &.{ .tokens = s.emit }); sink.event(sink.ctx, id, &.{ .finished = .{ .reason = .stop } }); } fn cancel(_: *anyopaque, _: api.Id) void {} @@ -402,3 +403,36 @@ test "thinking request: a named effort is the request's own, with no fallback, s try std.testing.expectEqualStrings("high", fields.namedEffort("high", &levels).?); try std.testing.expectEqualStrings("high", fields.namedEffort("medium", &levels).?); // on GLM-5.3's levels } + +test "thinking off on a channel model: the model's own unclosed block keeps its scratch text out of the answer" { + const t = std.testing; + var arena = std.heap.ArenaAllocator.init(t.allocator); + defer arena.deinit(); + const a = arena.allocator(); + var f = try fixture.Fixture.init(t.allocator, t.io, null); + defer f.deinit(); + try f.tmp.dir.writeFile(t.io, .{ .sub_path = "tokenizer.json", .data = + \\{"model":{"type":"BPE","vocab":{"H":0,"O":1,"K":2,"<|channel>thought":302,"":303,"<|im_end|>":127906},"merges":[]},"decoder":{"type":"Fuse"}, + \\ "added_tokens":[{"id":302,"content":"<|channel>thought","special":true},{"id":303,"content":"","special":true},{"id":127906,"content":"<|im_end|>","special":true}]} + }); + try f.tmp.dir.writeFile(t.io, .{ .sub_path = "chat_template.jinja", .data = unclosed_switch }); + var problem: []const u8 = ""; + const text = try HfText.load(t.allocator, t.io, f.path, null, a, &problem); + defer text.deinit(); + var script: Script = .{ .emit = &.{ 302, 2, 127906 } }; // its own open, then K, then its end-of-sequence: never closed + const srv = try Server.init(t.allocator, t.io, script.engine(), text.text(), .{ + .served_name = "m", + .model_ids = &.{"m"}, + .enable_thinking = true, + .use_drafts = false, + }, null); + defer srv.deinit(); + const body = (try json.parse(a, "{\"model\":\"m\",\"messages\":[{\"role\":\"user\",\"content\":\"H\"}],\"max_tokens\":4" ++ off ++ "}")).ok; + var conn: Conn = .{ .fd = -1, .peer = "", .buf = &.{}, .gpa = a }; + var response: Reply = .{ .a = a }; + openai.run(srv, a, response.out(), .{ .conn = &conn }, true, body); + try t.expectEqual(@as(?u16, 200), response.status); + try t.expect(script.seen); + const message = response.whole.?.get("choices").?.array[0].get("message").?; + try t.expectEqualStrings("", json.strOr(message.get("content"))); +} diff --git a/zig/tests/fixtures/gradient-serving.tsv b/zig/tests/fixtures/gradient-serving.tsv index 3d06ab5050a..5cf99cceb7c 100644 --- a/zig/tests/fixtures/gradient-serving.tsv +++ b/zig/tests/fixtures/gradient-serving.tsv @@ -115,8 +115,8 @@ zig/src/cuda/tp_watch.zig 19ed1d12bc56f804b5d207d04226e9f6131a517aaf7aec4c4b2d0b zig/src/cuda/triton.zig 256323865c6a4676b8b75aae9e32d2d9732cf2623efedb9462cf6f95302c3e83 raw zig/src/cuda/vit.zig 72c4301251f50d0f25907f0d8ad03a943281b234c506a455e413627a3ffe09c0 raw zig/src/families/flashnext/cuda_attn.zig 99dfe79f919a8599490f1cdba0bec20d6e73ccbeafe26d5848419a465fdfa0d0 raw -zig/src/families/flashnext/cuda_batch.zig 7fa858c564356e9cc80ee50a86602a5d1c1c3d1ef9ef6d0d69b2ac7a8a4b4e5e raw main 42ff6c510f (disk states keyed by the resolved prompt passes; serving unchanged) -zig/src/families/flashnext/cuda_batch_state.zig ae9d16a76aa56cd3448b3414140cbabd2f918bfb69a094b02be82544f371603c raw y and moe_f32 take one spare row for a --tp 2 prompt pass's padded rank block; replies unchanged +zig/src/families/flashnext/cuda_batch.zig eaf08f697f93041f93d2388b345e90a788398a5cf7bfa7641c63a816c6ac5946 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 +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_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