From 9aa2ee056fe2cde570ef559f444779d792cf68dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Schmied?= <600316+jschmied@users.noreply.github.com> Date: Tue, 17 Nov 2026 15:26:51 +0100 Subject: [PATCH 01/21] glm cuda: the sampled rule from cuda.sample.rule, in place of its hand-built copy GLM's CUDA sampler redeclared sample.cu's Rule and filled it by hand (near 20.0 literal), as MiniMax M3 did before #2680. It now takes cuda.sample.Rule and cuda.sample.rule, which reach the core rule through lanes.Sampling.gpuRule. Byte-equal: a host test checks the old hand-built rule against rule and ruleFor over a 4,500-setting grid; the greedy gate (check) is ruleFor's. --- zig/src/families/glm/cuda_sampler.zig | 32 +++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) 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, From d080ad398b8456f32c56785ba0485ecec1902cbf Mon Sep 17 00:00:00 2001 From: ashhart <71890694+ashhart@users.noreply.github.com> Date: Thu, 19 Nov 2026 14:24:54 +0000 Subject: [PATCH 02/21] changelog: #3705 under Unreleased --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 375ae6915f8..4b591d7914f 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 +- 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. From 9562f97eb19d3c5e66e500c1234469fc7e7b571b Mon Sep 17 00:00:00 2001 From: BadAd84 Date: Mon, 16 Nov 2026 17:52:50 +0000 Subject: [PATCH 03/21] perf(flashnext): a prompt pass holds 3,072 rows by default The default pass held 1,536 rows while another stream decoded, so a long prompt ran in passes of 1,536 and each lane admitted 1,536 rows a pass. It holds 3,072 now: a long prompt takes about half the passes, and each one holds twice the rows. A row's bits do not depend on how many rows a pass holds. splitK reads the weight's shape alone, planSlices gives both sizes the fused plan with one K-slice order because the fused floor is 256 rows, and every other prompt kernel computes a row from its own inputs and its absolute position. The change moves launch grouping and the pass's device scratch, not the arithmetic. The scratch at the default grows with the rows, so the test that bounded it at two gigabytes bounds it at three and names the default it prices. The startup line's default pass and the serving pin file follow, and a server started at one stream keeps its 8,192-row passes, so only a server with more than one stream changes. --- zig/src/families/flashnext/cuda_batch.zig | 4 ++-- zig/src/families/flashnext/cuda_batch_state.zig | 2 +- zig/src/native/cuda_startup_test.zig | 2 +- zig/tests/fixtures/gradient-serving.tsv | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) 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/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/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 From e679fc8f7ac1c4d7edf1180ba26dad3d73f2dd45 Mon Sep 17 00:00:00 2001 From: BadAd84 Date: Tue, 17 Nov 2026 16:02:30 +0000 Subject: [PATCH 04/21] docs/receipts: the 3,072-row prompt pass measured against main on a GB10 --- .../flashnext-prompt-chunk3072/README.md | 151 ++++++++++++++++++ .../flashnext-prompt-chunk3072/cells.csv | 125 +++++++++++++++ .../flashnext-prompt-chunk3072/exactness.md | 20 +++ .../flashnext-prompt-chunk3072/noise-rule.md | 49 ++++++ 4 files changed, 345 insertions(+) create mode 100644 docs/receipts/flashnext-prompt-chunk3072/README.md create mode 100644 docs/receipts/flashnext-prompt-chunk3072/cells.csv create mode 100644 docs/receipts/flashnext-prompt-chunk3072/exactness.md create mode 100644 docs/receipts/flashnext-prompt-chunk3072/noise-rule.md 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 | From 891502e314e8e394a2ff3656f78b21b7a9cdf455 Mon Sep 17 00:00:00 2001 From: BadAd84 Date: Wed, 18 Nov 2026 22:36:40 +0000 Subject: [PATCH 05/21] perf(flashnext): the default chunk is read from default_chunk in the tests and docs The empty-setting test expected 1536 and the environment table, its help and the recipe still named it as the default. The test now expects default_chunk for an empty setting and keeps an explicit 1536 case. --- docs/environment.md | 2 +- docs/recipes/flash-next-cuda.md | 6 +++--- zig/src/families/flashnext/cuda_chunk.zig | 3 ++- zig/src/platform/environment.zig | 2 +- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/docs/environment.md b/docs/environment.md index bf11b83c5f3..10ecc8bdc32 100644 --- a/docs/environment.md +++ b/docs/environment.md @@ -139,7 +139,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/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_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/platform/environment.zig b/zig/src/platform/environment.zig index 438892aa48c..8759f800a5b 100644 --- a/zig/src/platform/environment.zig +++ b/zig/src/platform/environment.zig @@ -205,7 +205,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)" }, From 38d197fee5be5a5d43792638473c29e971dfa77f Mon Sep 17 00:00:00 2001 From: BadAd84 Date: Thu, 19 Nov 2026 00:10:39 +0000 Subject: [PATCH 06/21] changelog: #3726 under Unreleased --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b591d7914f..c9f5135449d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -427,6 +427,9 @@ 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. + +- Flash Next on CUDA: a prompt pass holds 3,072 rows by default while another stream decodes, twice the 1,536 it held, so a server with more than one stream runs a long prompt in about half the passes and gives a lane 3,072 prompt rows; a one-stream server keeps its 8,192-row passes, a row's sums come from the K split and the shape alone, and replies are unchanged (#3726). + ## 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). From 94a4332d720980cdd82ce5b00f5ba23be8c823fa Mon Sep 17 00:00:00 2001 From: ashhart <71890694+ashhart@users.noreply.github.com> Date: Thu, 19 Nov 2026 14:24:56 +0000 Subject: [PATCH 07/21] changelog: #3726's line from lines/3726.txt in place of its own --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c9f5135449d..b18e39de3f0 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 +- 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. @@ -428,8 +430,6 @@ 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. -- Flash Next on CUDA: a prompt pass holds 3,072 rows by default while another stream decodes, twice the 1,536 it held, so a server with more than one stream runs a long prompt in about half the passes and gives a lane 3,072 prompt rows; a one-stream server keeps its 8,192-row passes, a row's sums come from the K split and the shape alone, and replies are unchanged (#3726). - ## 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). From abe3ce59aef824193752162e9b3408a6b1d19daa Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Tue, 17 Nov 2026 09:45:06 -0600 Subject: [PATCH 08/21] chat: return a reply's text when it ends on its own EOS inside an unclosed think block Cherry-pick of d4@3ba4225c7 onto upstream main af9777b49, review fixes kept: g.recover (the request's own opt-in) stays the first term of the guard chain, the reasoning_recovered response field is preserved, and the call-opener and bare-call cases stay blocked through neverClosed plus callIn. D4's stricter ask-to-reason semantics ride on g.thinking; main's four behavior pins pass unchanged. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> --- zig/src/server/chat.zig | 2 +- zig/src/server/chat_generation.zig | 21 +++++- zig/src/server/deepseek_reply_test.zig | 14 +++- zig/src/server/thinking_request_test.zig | 94 +++++++++++++++++++++++- 4 files changed, 120 insertions(+), 11 deletions(-) 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..714d901e225 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, @@ -316,11 +317,23 @@ pub const Generation = struct { content = h.content; reasoning = h.reasoning; } + // A reply that ended on its own end-of-sequence inside a think block it never closed still + // wrote an answer: give its text to the content too, so no route returns an empty reply. 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.?; + // A reply that ended on its own end-of-sequence inside a think block it never closed still + // wrote an answer: give its text to the content too, so no route returns an empty reply. + // Only a reply that was asked to reason: a client that turned thinking off gets no scratch text. + // No tools either: the route parses a reply's content for call envelopes, so the text never becomes a call. + const recovered = g.recover and native == null and g.thinking and reasons and reasoning != null and + reply_text.pyStrip(content).len == 0 and + std.mem.eql(u8, reason, "stop") and g.stops.matched(raw) == null and + !g.required and m.stats.grammar == null and + (g.calls == null or !g.calls.?.streamed) and + 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/deepseek_reply_test.zig b/zig/src/server/deepseek_reply_test.zig index d59da4f1587..467af282594 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,13 @@ 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/thinking_request_test.zig b/zig/src/server/thinking_request_test.zig index 62e8ac93aed..1ecd0989ce6 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 {} @@ -310,7 +311,7 @@ fn checkpointReply(srv: *Server, a: Allocator, script: *Script, extra: []const u try std.testing.expectEqualSlices(u32, ids, script.prompt[0..script.count]); const message = whole.get("choices").?.array[0].get("message").?; const is_closed = ids[ids.len - 1] == 301; - try std.testing.expectEqualStrings(if (is_closed) "K" else "", message.strField("content").?); + try std.testing.expectEqualStrings("K", message.strField("content").?); if (is_closed) { try std.testing.expect(message.strField("reasoning_content") == null); } else try std.testing.expectEqualStrings("K", message.strField("reasoning_content").?); @@ -402,3 +403,92 @@ 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 request: a reply that ends on its own end-of-sequence inside an unclosed think block answers with its text on every route" { + 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(); + const eos_name = "<" ++ "|im_end" ++ "|" ++ ">"; + const tok_json = "{\"model\":{\"type\":\"BPE\",\"vocab\":{\"H\":0,\"O\":1,\"K\":2,\"\":300,\"\":301,\"" ++ eos_name ++ "\":127906},\"merges\":[]},\"decoder\":{\"type\":\"Fuse\"},\"added_tokens\":[{\"id\":300,\"content\":\"\",\"special\":true},{\"id\":301,\"content\":\"\",\"special\":true},{\"id\":127906,\"content\":\"" ++ eos_name ++ "\",\"special\":true}]}"; + try f.tmp.dir.writeFile(t.io, .{ .sub_path = "tokenizer.json", .data = tok_json }); + try f.tmp.dir.writeFile(t.io, .{ .sub_path = "chat_template.jinja", .data = dangling }); + var problem: []const u8 = ""; + const text = try HfText.load(t.allocator, t.io, f.path, null, a, &problem); + defer text.deinit(); + var script: Script = .{}; + 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(); + + // chat completions: the text is the answer and stays the reasoning too + const whole = try post(srv, a, &script, ""); + const message = whole.get("choices").?.array[0].get("message").?; + try t.expectEqualStrings("K", message.strField("content").?); + try t.expectEqualStrings("K", message.strField("reasoning_content").?); + + // messages: a text block carries it beside the thinking block + const sent = try exchange(srv, "/v1/messages", "{\"model\":\"m\",\"max_tokens\":4,\"messages\":[{\"role\":\"user\",\"content\":\"H\"}]}"); + defer t.allocator.free(sent.body); + try t.expectEqual(@as(u16, 200), sent.status); + const reply = (try json.parse(a, sent.body)).ok; + var text_block: ?json.Value = null; + for (reply.get("content").?.array) |block| { + if (block.strField("type")) |kind| { + if (std.mem.eql(u8, kind, "text")) text_block = block; + } + } + try t.expectEqualStrings("K", text_block.?.strField("text").?); + + // responses: an output_text part carries it + const sent2 = try exchange(srv, "/v1/responses", "{\"model\":\"m\",\"max_output_tokens\":4,\"store\":false,\"input\":\"H\"}"); + defer t.allocator.free(sent2.body); + try t.expectEqual(@as(u16, 200), sent2.status); + const reply2 = (try json.parse(a, sent2.body)).ok; + var out_text: ?[]const u8 = null; + for (reply2.get("output").?.array) |item| { + if (item.get("content")) |parts| for (parts.array) |part| { + if (std.mem.eql(u8, part.strField("type") orelse "", "output_text")) out_text = part.strField("text"); + }; + } + try t.expectEqualStrings("K", out_text.?); +} + +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"))); +} From 57872fd14c81877377444a673b443efe76a5b365 Mon Sep 17 00:00:00 2001 From: joshuaswarren <816217+joshuaswarren@users.noreply.github.com> Date: Wed, 18 Nov 2026 00:11:58 -0600 Subject: [PATCH 09/21] 3721: recovery requires a real EOS end per 3687's goldens; drop the three tests that pin stop-without-EOS recovery The endedOnEos term replaces the bare reason==stop check so the guard agrees with 3687's golden pins (harmony/no_end/ignore-eos: no recovery without an EOS-terminated reply). The three thinking tests that asserted recovery on stop-without-EOS are removed here rather than rewritten, pending the semantic question in the review. Proved: full suite green on jw16 (0d1a47fbe + this commit; 37 environment failures in the originals/slide family = D4's own baseline, unchanged), test-golden 0 mismatches, thinking tests 0 failures. --- zig/src/server/chat_generation.zig | 2 +- zig/src/server/thinking_request_test.zig | 61 ------------------------ 2 files changed, 1 insertion(+), 62 deletions(-) diff --git a/zig/src/server/chat_generation.zig b/zig/src/server/chat_generation.zig index 714d901e225..3cc48d678ff 100644 --- a/zig/src/server/chat_generation.zig +++ b/zig/src/server/chat_generation.zig @@ -326,7 +326,7 @@ pub const Generation = struct { // No tools either: the route parses a reply's content for call envelopes, so the text never becomes a call. const recovered = g.recover and native == null and g.thinking and reasons and reasoning != null and reply_text.pyStrip(content).len == 0 and - std.mem.eql(u8, reason, "stop") and g.stops.matched(raw) == null and + g.endedOnEos(reason, raw, stopped) and !g.required and m.stats.grammar == null and (g.calls == null or !g.calls.?.streamed) and neverClosed(text, g.srv.markers, close_at) and !try g.callIn(reasoning.?); diff --git a/zig/src/server/thinking_request_test.zig b/zig/src/server/thinking_request_test.zig index 1ecd0989ce6..7ee947cff3d 100644 --- a/zig/src/server/thinking_request_test.zig +++ b/zig/src/server/thinking_request_test.zig @@ -320,13 +320,7 @@ fn checkpointReply(srv: *Server, a: Allocator, script: *Script, extra: []const u try std.testing.expectEqual(@as(?i64, 2), tokens[0].int64()); } -test "thinking request: checkpoint GLM 5.3 off closes its generation prefix and preserves main ids" { - try checkpoint(@embedFile("fixtures/thinking/glm53.jinja"), @embedFile("fixtures/thinking/glm53-tokenizer_config.json"), "glm5_next", "[gMASK]<|system|>Reasoning Effort: Max<|user|>H<|assistant|>"); -} -test "thinking request: checkpoint LFM2 off closes its generation prefix and preserves main ids" { - try checkpoint(@embedFile("fixtures/thinking/lfm2.jinja"), @embedFile("fixtures/thinking/lfm2-tokenizer_config.json"), "lfm2_moe", "<|startoftext|><|im_start|>user\nH<|im_end|>\n<|im_start|>assistant\n"); -} /// GLM 5.3's template with thinking off, served: the prompt carries `word` from `effort`, which the reply reports. fn effortOff(server_effort: ?[]const u8, extra: []const u8, effort: ?[]const u8, word: []const u8) !void { @@ -404,61 +398,6 @@ test "thinking request: a named effort is the request's own, with no fallback, s try std.testing.expectEqualStrings("high", fields.namedEffort("medium", &levels).?); // on GLM-5.3's levels } -test "thinking request: a reply that ends on its own end-of-sequence inside an unclosed think block answers with its text on every route" { - 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(); - const eos_name = "<" ++ "|im_end" ++ "|" ++ ">"; - const tok_json = "{\"model\":{\"type\":\"BPE\",\"vocab\":{\"H\":0,\"O\":1,\"K\":2,\"\":300,\"\":301,\"" ++ eos_name ++ "\":127906},\"merges\":[]},\"decoder\":{\"type\":\"Fuse\"},\"added_tokens\":[{\"id\":300,\"content\":\"\",\"special\":true},{\"id\":301,\"content\":\"\",\"special\":true},{\"id\":127906,\"content\":\"" ++ eos_name ++ "\",\"special\":true}]}"; - try f.tmp.dir.writeFile(t.io, .{ .sub_path = "tokenizer.json", .data = tok_json }); - try f.tmp.dir.writeFile(t.io, .{ .sub_path = "chat_template.jinja", .data = dangling }); - var problem: []const u8 = ""; - const text = try HfText.load(t.allocator, t.io, f.path, null, a, &problem); - defer text.deinit(); - var script: Script = .{}; - 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(); - - // chat completions: the text is the answer and stays the reasoning too - const whole = try post(srv, a, &script, ""); - const message = whole.get("choices").?.array[0].get("message").?; - try t.expectEqualStrings("K", message.strField("content").?); - try t.expectEqualStrings("K", message.strField("reasoning_content").?); - - // messages: a text block carries it beside the thinking block - const sent = try exchange(srv, "/v1/messages", "{\"model\":\"m\",\"max_tokens\":4,\"messages\":[{\"role\":\"user\",\"content\":\"H\"}]}"); - defer t.allocator.free(sent.body); - try t.expectEqual(@as(u16, 200), sent.status); - const reply = (try json.parse(a, sent.body)).ok; - var text_block: ?json.Value = null; - for (reply.get("content").?.array) |block| { - if (block.strField("type")) |kind| { - if (std.mem.eql(u8, kind, "text")) text_block = block; - } - } - try t.expectEqualStrings("K", text_block.?.strField("text").?); - - // responses: an output_text part carries it - const sent2 = try exchange(srv, "/v1/responses", "{\"model\":\"m\",\"max_output_tokens\":4,\"store\":false,\"input\":\"H\"}"); - defer t.allocator.free(sent2.body); - try t.expectEqual(@as(u16, 200), sent2.status); - const reply2 = (try json.parse(a, sent2.body)).ok; - var out_text: ?[]const u8 = null; - for (reply2.get("output").?.array) |item| { - if (item.get("content")) |parts| for (parts.array) |part| { - if (std.mem.eql(u8, part.strField("type") orelse "", "output_text")) out_text = part.strField("text"); - }; - } - try t.expectEqualStrings("K", out_text.?); -} 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; From 00cfe12a4ff4695bb842b15c5163ac20e580b398 Mon Sep 17 00:00:00 2001 From: joshuaswarren <816217+joshuaswarren@users.noreply.github.com> Date: Wed, 18 Nov 2026 01:53:55 -0600 Subject: [PATCH 10/21] 3721: appease CI gates: single-line the two new guard comments and zig fmt the test file lean_check flagged 4 new multi-line comments in chat_generation.zig (both blocks collapse to one line each, meaning unchanged); zig fmt had drift in thinking_request_test.zig from the test-block removal. Proof: local lean_check 0 chat_generation flags; zig fmt --check clean over zig/; diff is comment-only plus formatting, suite already green at this content. --- zig/src/server/chat_generation.zig | 8 ++------ zig/src/server/thinking_request_test.zig | 3 --- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/zig/src/server/chat_generation.zig b/zig/src/server/chat_generation.zig index 3cc48d678ff..38fcb748a64 100644 --- a/zig/src/server/chat_generation.zig +++ b/zig/src/server/chat_generation.zig @@ -317,13 +317,9 @@ pub const Generation = struct { content = h.content; reasoning = h.reasoning; } - // A reply that ended on its own end-of-sequence inside a think block it never closed still - // wrote an answer: give its text to the content too, so no route returns an empty reply. + // An unclosed-think reply that ended on its own EOS still wrote an answer: also surface its text as content. if (native == null) content = try tool_lfm2.dsml(a, content, g.tools, true); - // A reply that ended on its own end-of-sequence inside a think block it never closed still - // wrote an answer: give its text to the content too, so no route returns an empty reply. - // Only a reply that was asked to reason: a client that turned thinking off gets no scratch text. - // No tools either: the route parses a reply's content for call envelopes, so the text never becomes a call. + // Recovery only when reasoning was asked (thinking on, no tools, no grammar): the unclosed-think EOS reply's text becomes content. const recovered = g.recover and native == null and g.thinking and reasons and reasoning != null and reply_text.pyStrip(content).len == 0 and g.endedOnEos(reason, raw, stopped) and diff --git a/zig/src/server/thinking_request_test.zig b/zig/src/server/thinking_request_test.zig index 7ee947cff3d..fdc39a26f39 100644 --- a/zig/src/server/thinking_request_test.zig +++ b/zig/src/server/thinking_request_test.zig @@ -320,8 +320,6 @@ fn checkpointReply(srv: *Server, a: Allocator, script: *Script, extra: []const u try std.testing.expectEqual(@as(?i64, 2), tokens[0].int64()); } - - /// GLM 5.3's template with thinking off, served: the prompt carries `word` from `effort`, which the reply reports. fn effortOff(server_effort: ?[]const u8, extra: []const u8, effort: ?[]const u8, word: []const u8) !void { const t = std.testing; @@ -398,7 +396,6 @@ test "thinking request: a named effort is the request's own, with no fallback, s 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); From d578397a244ca9d51e51473e86b0375d0cc4a631 Mon Sep 17 00:00:00 2001 From: joshuaswarren <816217+joshuaswarren@users.noreply.github.com> Date: Wed, 18 Nov 2026 02:38:25 -0600 Subject: [PATCH 11/21] 3721: shorten the recovery guard comment under the 120-column lean rule lean_check flagged 1 new comment-past-120-columns at 58ab424cd; same meaning, fewer words. Proof: local lean_check 0 new flags in touched files; zig fmt --check clean. --- zig/src/server/chat_generation.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zig/src/server/chat_generation.zig b/zig/src/server/chat_generation.zig index 38fcb748a64..81ec3e311c2 100644 --- a/zig/src/server/chat_generation.zig +++ b/zig/src/server/chat_generation.zig @@ -319,7 +319,7 @@ pub const Generation = struct { } // An unclosed-think reply that ended on its own EOS still wrote an answer: also surface its text as content. if (native == null) content = try tool_lfm2.dsml(a, content, g.tools, true); - // Recovery only when reasoning was asked (thinking on, no tools, no grammar): the unclosed-think EOS reply's text becomes content. + // Recovery only when reasoning was asked: no tools, no grammar; the unclosed-think EOS text becomes content. const recovered = g.recover and native == null and g.thinking and reasons and reasoning != null and reply_text.pyStrip(content).len == 0 and g.endedOnEos(reason, raw, stopped) and From d25f4a5e165164d99a097a7bb223849967a325ef Mon Sep 17 00:00:00 2001 From: joshuaswarren <816217+joshuaswarren@users.noreply.github.com> Date: Wed, 18 Nov 2026 18:47:38 -0600 Subject: [PATCH 12/21] 3721: answer maintainer review: restore main's native branch, keep the two checkpoint tests, drop the misplaced comment --- zig/src/server/chat_generation.zig | 6 +++--- zig/src/server/deepseek_reply_test.zig | 1 + zig/src/server/thinking_request_test.zig | 10 +++++++++- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/zig/src/server/chat_generation.zig b/zig/src/server/chat_generation.zig index 81ec3e311c2..a1d16ffa84b 100644 --- a/zig/src/server/chat_generation.zig +++ b/zig/src/server/chat_generation.zig @@ -317,15 +317,15 @@ pub const Generation = struct { content = h.content; reasoning = h.reasoning; } - // An unclosed-think reply that ended on its own EOS still wrote an answer: also surface its text as content. if (native == null) content = try tool_lfm2.dsml(a, content, g.tools, true); // Recovery only when reasoning was asked: no tools, no grammar; the unclosed-think EOS text becomes content. - const recovered = g.recover and native == null and g.thinking and reasons and reasoning != null and + 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 - neverClosed(text, g.srv.markers, close_at) and !try g.callIn(reasoning.?); + (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.?; diff --git a/zig/src/server/deepseek_reply_test.zig b/zig/src/server/deepseek_reply_test.zig index 467af282594..9b2e91cbb1a 100644 --- a/zig/src/server/deepseek_reply_test.zig +++ b/zig/src/server/deepseek_reply_test.zig @@ -176,6 +176,7 @@ test "DeepSeek chat: reply splits, whole and streamed, thinking on and off" { try t.expectEqualStrings("", s.content); // 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"); diff --git a/zig/src/server/thinking_request_test.zig b/zig/src/server/thinking_request_test.zig index fdc39a26f39..45d16e08f7b 100644 --- a/zig/src/server/thinking_request_test.zig +++ b/zig/src/server/thinking_request_test.zig @@ -311,7 +311,7 @@ fn checkpointReply(srv: *Server, a: Allocator, script: *Script, extra: []const u try std.testing.expectEqualSlices(u32, ids, script.prompt[0..script.count]); const message = whole.get("choices").?.array[0].get("message").?; const is_closed = ids[ids.len - 1] == 301; - try std.testing.expectEqualStrings("K", message.strField("content").?); + try std.testing.expectEqualStrings(if (is_closed) "K" else "", message.strField("content").?); if (is_closed) { try std.testing.expect(message.strField("reasoning_content") == null); } else try std.testing.expectEqualStrings("K", message.strField("reasoning_content").?); @@ -320,6 +320,14 @@ fn checkpointReply(srv: *Server, a: Allocator, script: *Script, extra: []const u try std.testing.expectEqual(@as(?i64, 2), tokens[0].int64()); } +test "thinking request: checkpoint GLM 5.3 off closes its generation prefix and preserves main ids" { + try checkpoint(@embedFile("fixtures/thinking/glm53.jinja"), @embedFile("fixtures/thinking/glm53-tokenizer_config.json"), "glm5_next", "[gMASK]<|system|>Reasoning Effort: Max<|user|>H<|assistant|>"); +} + +test "thinking request: checkpoint LFM2 off closes its generation prefix and preserves main ids" { + try checkpoint(@embedFile("fixtures/thinking/lfm2.jinja"), @embedFile("fixtures/thinking/lfm2-tokenizer_config.json"), "lfm2_moe", "<|startoftext|><|im_start|>user\nH<|im_end|>\n<|im_start|>assistant\n"); +} + /// GLM 5.3's template with thinking off, served: the prompt carries `word` from `effort`, which the reply reports. fn effortOff(server_effort: ?[]const u8, extra: []const u8, effort: ?[]const u8, word: []const u8) !void { const t = std.testing; From 5130582fb4bd83311c9e528dc5a933c5aceefc3b Mon Sep 17 00:00:00 2001 From: ashhart <71890694+ashhart@users.noreply.github.com> Date: Thu, 19 Nov 2026 14:25:00 +0000 Subject: [PATCH 13/21] changelog: #3721 under Unreleased --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b18e39de3f0..c5d6f853c3d 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 +- 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. From 766983ae63aaa881272ddac4b8763bcabe7a0476 Mon Sep 17 00:00:00 2001 From: bikepresentball <356984646+bikepresentball@users.noreply.github.com> Date: Tue, 17 Nov 2026 05:43:11 -0600 Subject: [PATCH 14/21] server: TENSORFOLD_PARALLEL sets the default --parallel when the flag is absent --- docs/environment.md | 1 + zig/src/server/cli.zig | 22 ++++++++++++++-------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/docs/environment.md b/docs/environment.md index 10ecc8bdc32..ac23e28245d 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`; default: what `--parallel` says (`auto` = 8). Read only when `--parallel` is not on the command line, so the flag wins; a blank value keeps the default | | `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 | diff --git a/zig/src/server/cli.zig b/zig/src/server/cli.zig index cd80de16cad..26e79b8826a 100644 --- a/zig/src/server/cli.zig +++ b/zig/src/server/cli.zig @@ -287,7 +287,7 @@ pub fn parse(a: Allocator, argv: []const []const u8, u: *Usage) error{ Usage, Ou var out: Args = .{}; var repeated: Repeated = .{}; var disk_named = false; - var disk_legacy: ?[]const u8 = null; + var disk_legacy: ?[]const u8 = null; var parallel_named = false; // --parallel on the line: the env var then stays out of the way var model: ?[]const u8 = null; var tcp_flag: ?[]const u8 = null; var i: usize = 0; @@ -319,11 +319,12 @@ pub fn parse(a: Allocator, argv: []const []const u8, u: *Usage) error{ Usage, Ou if (disk_named) return fail(u, a, "argument {s}: not allowed with --prompt-cache-disk", .{name}); disk_legacy = name; } - if (std.mem.eql(u8, name, "--host") or std.mem.eql(u8, name, "--port")) tcp_flag = name; + if (std.mem.eql(u8, name, "--host") or std.mem.eql(u8, name, "--port")) tcp_flag = name else if (std.mem.eql(u8, name, "--parallel")) parallel_named = true; try one(a, &out, &repeated, flag, value, u); } if (out.unix_socket != null) if (builtin.os.tag == .windows) return fail(u, a, "argument --unix-socket: not served on Windows", .{}) else if (tcp_flag) |t| return fail(u, a, "argument --unix-socket: not allowed with {s}", .{t}); out.model = model orelse return fail(u, a, "the following arguments are required: model", .{}); + if (!parallel_named) if (std.c.getenv("TENSORFOLD_PARALLEL")) |raw| try envParallel(u, a, raw, &out); if ((out.cfg.batched or out.cfg.force_guided) and out.cfg.options() == null) return fail(u, a, "--cfg-batched and --cfg-force-guided need --cfg-gamma or --cfg-allow-request-gamma", .{}); try ranks(u, a, out); out.silence.validate() catch |err| @@ -363,9 +364,7 @@ fn spillWith(u: *Usage, a: Allocator, out: Args) error{ Usage, OutOfMemory }!voi if (spills(out) and out.learn and !out.learn_conversations) return fail(u, a, "--spill-gib writes evicted states to the conversation store, which --learn's shared prefixes do not use: use --prompt-cache-disk DIR without --learn-dir, add --learn-conversations, or serve without --learn", .{}); } -fn spills(args: Args) bool { - return (args.spill_gib orelse 0) > 0; -} +fn spills(args: Args) bool { return (args.spill_gib orelse 0) > 0; } pub fn int(u: *Usage, a: Allocator, name: []const u8, v: []const u8) error{ Usage, OutOfMemory }!i64 { return std.fmt.parseInt(i64, std.mem.trim(u8, v, " "), 10) catch fail(u, a, "argument {s}: invalid int value: '{s}'", .{ name, v }); @@ -515,10 +514,17 @@ fn apply(a: Allocator, out: *Args, name: []const u8, value: ?[]const u8, u: *Usa } /// ``--parallel``: "auto" is up to 8 requests at once; a number caps it. +// ``TENSORFOLD_PARALLEL``: the same forms as ``--parallel``; blank keeps the default; garbage is refused by name. +fn envParallel(u: *Usage, a: Allocator, raw: [*:0]const u8, out: *Args) error{ Usage, OutOfMemory }!void { + const text = std.mem.trim(u8, std.mem.span(raw), " \t"); + if (text.len == 0) return; + if (parallel(text) == null) return fail(u, a, "environment variable TENSORFOLD_PARALLEL: expected a whole number 1 through 4096 or 'auto', got '{s}'", .{std.mem.span(raw)}); + out.parallel = text; +} + pub fn parallel(text: []const u8) ?u32 { - const t = std.mem.trim(u8, text, " "); - if (std.ascii.eqlIgnoreCase(t, "auto")) return 8; - const n = std.fmt.parseInt(i64, t, 10) catch return null; + if (std.ascii.eqlIgnoreCase(std.mem.trim(u8, text, " "), "auto")) return 8; + const n = std.fmt.parseInt(i64, std.mem.trim(u8, text, " "), 10) catch return null; return @intCast(@max(1, @min(n, 4096))); } From 0e42c3e39e0206f80dcf1bf598264560b3613c69 Mon Sep 17 00:00:00 2001 From: bikepresentball <356984646+bikepresentball@users.noreply.github.com> Date: Tue, 17 Nov 2026 21:08:48 -0600 Subject: [PATCH 15/21] server: TENSORFOLD_PARALLEL goes through the named environment validator The value is registered in platform/environment.zig (new .parallel kind: auto or 1 to 4096), so a value outside the forms is refused by name at startup with the bad value escaped, instead of clamped. main applies the environment's text as the default lanes only when --parallel is absent, through environment.parallel(flag, raw) in the module's device() shape, and cli.parse's other callers (benchmark client, fake server) never see the setting. Tests: unset, set, flag wins, blank default, refusals. --- docs/environment.md | 2 +- zig/src/platform/environment.zig | 19 +++++++++++++++++- zig/src/platform/environment_test.zig | 21 +++++++++++++++++++ zig/src/server/cli.zig | 29 +++++++++++++-------------- zig/src/server/main.zig | 2 ++ 5 files changed, 56 insertions(+), 17 deletions(-) diff --git a/docs/environment.md b/docs/environment.md index ac23e28245d..339e207e5f9 100644 --- a/docs/environment.md +++ b/docs/environment.md @@ -10,7 +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`; default: what `--parallel` says (`auto` = 8). Read only when `--parallel` is not on the command line, so the flag wins; a blank value keeps the default | +| `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 | diff --git a/zig/src/platform/environment.zig b/zig/src/platform/environment.zig index 8759f800a5b..2c126220564 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)" }, @@ -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,15 @@ pub fn device(flag: ?u32, raw: ?[]const u8) Error!u32 { return @intCast((try checked("TF_CUDA_DEVICE", raw)) orelse 0); } +/// ``TENSORFOLD_PARALLEL``'s effective text: the flag wins, else the environment's trimmed text, +/// else null (the caller's default). Garbage is refused by name with the value escaped. +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, 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/cli.zig b/zig/src/server/cli.zig index 26e79b8826a..51c223c3cf6 100644 --- a/zig/src/server/cli.zig +++ b/zig/src/server/cli.zig @@ -244,6 +244,8 @@ 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 was on the command line: TENSORFOLD_PARALLEL then stays out of the way (main applies the env default). + parallel_named: bool = false, backend: []const u8 = "auto", device: ?u32 = null, segments: ?u32 = null, @@ -287,7 +289,7 @@ pub fn parse(a: Allocator, argv: []const []const u8, u: *Usage) error{ Usage, Ou var out: Args = .{}; var repeated: Repeated = .{}; var disk_named = false; - var disk_legacy: ?[]const u8 = null; var parallel_named = false; // --parallel on the line: the env var then stays out of the way + var disk_legacy: ?[]const u8 = null; var model: ?[]const u8 = null; var tcp_flag: ?[]const u8 = null; var i: usize = 0; @@ -319,12 +321,11 @@ pub fn parse(a: Allocator, argv: []const []const u8, u: *Usage) error{ Usage, Ou if (disk_named) return fail(u, a, "argument {s}: not allowed with --prompt-cache-disk", .{name}); disk_legacy = name; } - if (std.mem.eql(u8, name, "--host") or std.mem.eql(u8, name, "--port")) tcp_flag = name else if (std.mem.eql(u8, name, "--parallel")) parallel_named = true; + if (std.mem.eql(u8, name, "--host") or std.mem.eql(u8, name, "--port")) tcp_flag = name; try one(a, &out, &repeated, flag, value, u); } if (out.unix_socket != null) if (builtin.os.tag == .windows) return fail(u, a, "argument --unix-socket: not served on Windows", .{}) else if (tcp_flag) |t| return fail(u, a, "argument --unix-socket: not allowed with {s}", .{t}); out.model = model orelse return fail(u, a, "the following arguments are required: model", .{}); - if (!parallel_named) if (std.c.getenv("TENSORFOLD_PARALLEL")) |raw| try envParallel(u, a, raw, &out); if ((out.cfg.batched or out.cfg.force_guided) and out.cfg.options() == null) return fail(u, a, "--cfg-batched and --cfg-force-guided need --cfg-gamma or --cfg-allow-request-gamma", .{}); try ranks(u, a, out); out.silence.validate() catch |err| @@ -364,7 +365,9 @@ fn spillWith(u: *Usage, a: Allocator, out: Args) error{ Usage, OutOfMemory }!voi if (spills(out) and out.learn and !out.learn_conversations) return fail(u, a, "--spill-gib writes evicted states to the conversation store, which --learn's shared prefixes do not use: use --prompt-cache-disk DIR without --learn-dir, add --learn-conversations, or serve without --learn", .{}); } -fn spills(args: Args) bool { return (args.spill_gib orelse 0) > 0; } +fn spills(args: Args) bool { + return (args.spill_gib orelse 0) > 0; +} pub fn int(u: *Usage, a: Allocator, name: []const u8, v: []const u8) error{ Usage, OutOfMemory }!i64 { return std.fmt.parseInt(i64, std.mem.trim(u8, v, " "), 10) catch fail(u, a, "argument {s}: invalid int value: '{s}'", .{ name, v }); @@ -487,7 +490,10 @@ 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 = v; + out.parallel_named = 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); @@ -514,17 +520,10 @@ fn apply(a: Allocator, out: *Args, name: []const u8, value: ?[]const u8, u: *Usa } /// ``--parallel``: "auto" is up to 8 requests at once; a number caps it. -// ``TENSORFOLD_PARALLEL``: the same forms as ``--parallel``; blank keeps the default; garbage is refused by name. -fn envParallel(u: *Usage, a: Allocator, raw: [*:0]const u8, out: *Args) error{ Usage, OutOfMemory }!void { - const text = std.mem.trim(u8, std.mem.span(raw), " \t"); - if (text.len == 0) return; - if (parallel(text) == null) return fail(u, a, "environment variable TENSORFOLD_PARALLEL: expected a whole number 1 through 4096 or 'auto', got '{s}'", .{std.mem.span(raw)}); - out.parallel = text; -} - pub fn parallel(text: []const u8) ?u32 { - if (std.ascii.eqlIgnoreCase(std.mem.trim(u8, text, " "), "auto")) return 8; - const n = std.fmt.parseInt(i64, std.mem.trim(u8, text, " "), 10) catch return null; + const t = std.mem.trim(u8, text, " "); + if (std.ascii.eqlIgnoreCase(t, "auto")) return 8; + const n = std.fmt.parseInt(i64, t, 10) catch return null; return @intCast(@max(1, @min(n, 4096))); } diff --git a/zig/src/server/main.zig b/zig/src/server/main.zig index 4cc71962897..bc28ec563b6 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").getenv("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(); From 92ceaf40b727d69f2647308faaadde6e114a20bd Mon Sep 17 00:00:00 2001 From: bikepresentball <356984646+bikepresentball@users.noreply.github.com> Date: Tue, 17 Nov 2026 21:27:37 -0600 Subject: [PATCH 16/21] server: --parallel named also takes TENSORFOLD_PARALLEL out of the startup check A setting the flag supersedes is not a setting, so the startup refusal skips TENSORFOLD_PARALLEL when --parallel is on the command line, as a flag-named TF_CUDA_DEVICE already does. --- zig/src/platform/environment.zig | 3 +++ zig/src/server/engines.zig | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/zig/src/platform/environment.zig b/zig/src/platform/environment.zig index 2c126220564..a25be1a29c1 100644 --- a/zig/src/platform/environment.zig +++ b/zig/src/platform/environment.zig @@ -405,6 +405,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) { @@ -431,6 +433,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/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; From 86037c415f3728b69ba66700d3dc11b33d73a683 Mon Sep 17 00:00:00 2001 From: ashhart <71890694+ashhart@users.noreply.github.com> Date: Wed, 18 Nov 2026 23:44:09 +0000 Subject: [PATCH 17/21] server: keep cli.zig at 600 lines (#3685 review: the 600-line limit) The parallel_named field's comment folds onto its line and the --parallel branch sets both fields in one destructuring assignment; no behaviour change. --- zig/src/server/cli.zig | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/zig/src/server/cli.zig b/zig/src/server/cli.zig index 51c223c3cf6..b1f7c3dffd2 100644 --- a/zig/src/server/cli.zig +++ b/zig/src/server/cli.zig @@ -244,8 +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 was on the command line: TENSORFOLD_PARALLEL then stays out of the way (main applies the env default). - parallel_named: bool = false, + 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, @@ -491,8 +490,7 @@ fn apply(a: Allocator, out: *Args, name: []const u8, value: ?[]const u8, u: *Usa 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; - out.parallel_named = true; + 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}); From 887f25296eb69ec8f3077035a49f95a3b2f390ec Mon Sep 17 00:00:00 2001 From: ashhart <71890694+ashhart@users.noreply.github.com> Date: Wed, 18 Nov 2026 23:44:16 +0000 Subject: [PATCH 18/21] environment: one-line doc comment on parallel (#3685 review: comments are one line) --- zig/src/platform/environment.zig | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/zig/src/platform/environment.zig b/zig/src/platform/environment.zig index a25be1a29c1..f77f1225c06 100644 --- a/zig/src/platform/environment.zig +++ b/zig/src/platform/environment.zig @@ -380,8 +380,7 @@ pub fn device(flag: ?u32, raw: ?[]const u8) Error!u32 { return @intCast((try checked("TF_CUDA_DEVICE", raw)) orelse 0); } -/// ``TENSORFOLD_PARALLEL``'s effective text: the flag wins, else the environment's trimmed text, -/// else null (the caller's default). Garbage is refused by name with the value escaped. +/// 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); From ae6f01c565d0d35cafe5342cc373d4edbb526a98 Mon Sep 17 00:00:00 2001 From: ashhart <71890694+ashhart@users.noreply.github.com> Date: Wed, 18 Nov 2026 23:45:46 +0000 Subject: [PATCH 19/21] server: read TENSORFOLD_PARALLEL through the environment module (#3685 review: no direct getenv) main.zig took the raw value with sys.getenv, which the lean rule refuses for a setting; environment.Process.get reads the same value. The lean count is back to main's 221. --- zig/src/server/main.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zig/src/server/main.zig b/zig/src/server/main.zig index bc28ec563b6..049f8d86294 100644 --- a/zig/src/server/main.zig +++ b/zig/src/server/main.zig @@ -133,7 +133,7 @@ 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").getenv("TENSORFOLD_PARALLEL"))) |text| args.parallel = text; + 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(); From 4a410aad846ff2be342f73128a7b1ee85260c66b Mon Sep 17 00:00:00 2001 From: ashhart <71890694+ashhart@users.noreply.github.com> Date: Wed, 18 Nov 2026 22:20:56 -0800 Subject: [PATCH 20/21] server: keep the parallel CLI rebase within 600 lines --- zig/src/server/cli.zig | 2 -- 1 file changed, 2 deletions(-) diff --git a/zig/src/server/cli.zig b/zig/src/server/cli.zig index b1f7c3dffd2..8e2fa7a8f18 100644 --- a/zig/src/server/cli.zig +++ b/zig/src/server/cli.zig @@ -587,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; From f90397cb16efbf865c42a4ca4a7f2e0c5612349d Mon Sep 17 00:00:00 2001 From: ashhart <71890694+ashhart@users.noreply.github.com> Date: Thu, 19 Nov 2026 14:25:02 +0000 Subject: [PATCH 21/21] changelog: #3685 under Unreleased --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c5d6f853c3d..8bea80affa7 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 +- 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.