Files
leleka/docs/prefill-throughput.md
T

41 KiB

Prefill throughput of the Original serve stack (port)

Branch claude/prefill-throughput. Measurements on the RTX 5090 server (job queue holder2, jobs 700-702) with the long-context smoke original_serve_long_context_streamed_kv_smoke (LELEKA_ORIGINAL_SERVE_LONG_TOKENS=8000,16000, greedy, 16 new tokens, INT8 KV, resident window 20480, ceiling 65536, budget 30 GiB, model IQ3_S + KAR L2 + MTP pack as in job 312). Expert tier "on" = EXPERT_SLOTS=2048 EXPERT_PER_LAYER=yes EXPERT_PROFILE=baseline/profile.bin PCIE_FRAC=0.55. One process per configuration, one pass per prompt (n = 1: indicative, not a charter A/B).

Profile

LELEKA_BRINGUP_TRACE=1 prints one PREFILL-PROFILE line per published chunk (qwen_forward/prefill_profile.rs): host wall time per stage, nested moe.* and inject spans included in their parents, other = outside every top-level span. LELEKA_PREFILL_PROFILE_SYNC=1 drains the device at every span end (GPU attribution); on this workload it changed no stage by more than a few percent, i.e. the stages below are host-bound or synchronous.

Before (job 700, cffbf88, 2048-token chunk, steady state, ms per chunk):

stage ms what
validate 4,450 per-row session validation: quadratic in the chunk, and every row re-ran the dense-source checks
moe.ring 850 expert DMA ring + grouped MMQ (registered bank, PCIe-bound: ~34 GB of experts per chunk)
moe.spans 650 host finite rescan of every routed expert's bytes (~1.4 MB each, one thread) every chunk
gdn / qsa 220 / 85 mixers incl. GR injection
input / init 50 / 50 per-token KAR/PLE input staging / row upload
reads, inject, prepare, finish 65, 32, 18, 10
total ~6,500 315 tok/s

Fixes

  1. 19fac1b row validation once per set (linear), same checks and order.
  2. 5416f4e a routed expert's bank bytes are admitted finite once per bank (bit per layer/expert in the MoE cache).
  3. ae283e6 pageable (file-mapped, laptop) banks: prefill.cpp's Stager in the copy ring (helper threads copy into page-locked buffers, check the block scales there, the launching thread DMAs); registered banks unchanged.

Results (RTX 5090)

build chunk tier 8000 tok/s 16000 tok/s
before cffbf88 2048 off 307 311
before cffbf88 2048 on 306 310
after 5416f4e 2048 off 1,145 1,435
after 5416f4e 2048 on 1,174 1,463
after 5416f4e 8192 off 1,922 2,887
after 5416f4e 8192 on (lends 1,891 slots) 1,883 2,828

Greedy output ids at chunk 2048 are identical before and after (both prompts, tier off and on). The first chunk of a process still pays the one-time finite admission (~0.7-0.85 s).

After (8192-token chunk, ms): moe.ring 1,110, gdn 585, qsa 254, input 184, init 210, attn+ffn reads 229, inject 125, moe.spans 36, prepare 47, finish 36.

Chunk size and greedy output (jobs 702/703, tier off, 16 greedy tokens; every configuration is deterministic over two passes on one stack): 8000, 10000 and 12000 tokens answer identically at chunks 2048 and 8192; 16000 tokens answers identically at 2048, 4096 and 6144 (1,430 / 1,569 / 1,982 tok/s) but differently at 8192 (an immediate </think> answer instead of a thinking turn). Whether that is chunk-order rounding at a decision boundary or a large-chunk defect is open: compare final-row logits at 6144 vs 8192 (and the Original at 8192) before making 8192 the default. The default stays 2048; LELEKA_ORIGINAL_SERVE_CAPACITY / prefill_capacity select larger chunks. The Original's --prefill auto reaches 8192 only when it can lend expert-cache slots (profile + borrow), and otherwise keeps 2048, so the port's default matches it without the tier.

Laptop (RTX 3080 laptop, WSL2, mapped experts, VRAM tier auto + profile, KAR file, 3 prompts of 78-88 tokens)

Same harness as docs/laptop-profile.md (scripts/laptop/port_bench.sh, 16 new tokens, 2 passes), one process per arm, W first (cold page cache after a WSL restart), then Y. Greedy outputs identical.

arm prefill tok/s pass 0 / pass 1 TTFT s pass 0 / pass 1
W 3e05437 (integration head) 0.7, 1.4, 3.1 / 1.6, 1.9, 2.3 120, 59, 26 / 56, 44, 35
Y ae283e6 (all fixes + stager) 5.6, 4.5, 6.4 / 9.0, 9.4, 9.4 17, 19, 13 / 10.5, 9.5, 8.9

Y's profile (pass 1, ~80 tokens): moe.ring 5.5-7.0 s (expert bytes over PCIe from the page cache), gdn 1.33 s, qsa 0.33 s, everything else < 0.2 s. The Original's paired row in docs/laptop-profile.md: 12.4 tok/s, TTFT 7.2 s.

Remaining gap (RTX 5090, 8192-token chunks: 2,830-2,930 tok/s vs the Original's 5,400-5,900)

  • moe.ring 1.1 s: the expert DMA (~34 GB per chunk) runs layer by layer and drains before the next mixer; the Original's stream_all (prefill.cpp 1159-1286, driver run_stream_all_forward exists, unused by the runtime) keeps the copy stream busy across layers.
  • gdn 0.58 s, qsa 0.25 s, GR reads/injects 0.35 s per 8192 tokens. GDN costs 1.3 s even on an 80-token laptop prompt, so most of it does not scale with the chunk; the chunk owner checks and stages each layer's recurrent state from host values (GdnChunkDevice::new), the likely cause (not yet measured separately).
  • input 0.18 s (per-token KAR gather with device round trips) and init 0.21 s (host row staging) per 8192 tokens.

Follow-up (jobs 704-708, laptop runs E/F/G)

Chunk 8192 on the 16,000-token prompt: rounding, not a defect

LELEKA_TRACE_LOGPROBS=5 / LELEKA_TRACE_RESIDUAL (jobs 704, 706). The answers differ at the third generated token (position 16001): chunk 6144 gives 198 (logprob -0.568) over 271 (-0.836); chunk 8192 gives 271 (-0.578) over 198 (-0.824): a 0.25-nat near tie. The final residual of the last prompt row differs between ANY two chunk sizes by about the same amount: relative L2 2048/4096 0.168, 2048/6144 0.194, 4096/6144 0.179, 2048/8192 0.203, 6144/8192 0.185 (cos 0.982-0.988); 8000 tokens at 2048 vs 8192: 0.137. 8192 is not an outlier, so this is chunk-order rounding accumulated over 16,000 tokens and 48 layers. Proposal: take the Original's --prefill auto ladder as the default when the expert tier lends slots (8192 on the 5090), 2048 otherwise (the Original's own fallback without a profile/borrow). Not changed in this branch (it changes answers at decision boundaries like this one; a deliberate default change).

Where an 8192-token chunk's time goes (job 705, LELEKA_PREFILL_PROFILE_SYNC per-command timing)

  • Expert DMA: LELEKA_PREFILL_DIAG_SKIP_EXPERT_DMA=1 (EXPERIMENTAL, wrong outputs) cuts moe.ring from 1,105 to 330 ms: ~0.77 s of the chunk is the per-layer expert stream (34 GB at ~45 GB/s; the Original's probe: 49.9 GB/s), not MMQ. Without it the chunk would run at ~3,970 tok/s.
  • GDN (540-600 ms): recurrence kernel 148-155 ms, QKV/Z/Out GEMMs 145-155 ms, weight dequantization 29 ms (fixed per chunk), state staging (state_in/state_out) 80-115 ms; the finite-guard reuse (f6b4822) measured no gain.
  • input 184 ms (KAR PLE gather, ~19 us/token with device round trips), init 198 ms (row staging).

Expert stream overlap

80627bc ran the Original's stream_all with its own issuer thread: slower (job 707: 1,083 vs 1,413 tok/s at 2048, 2,419 vs 2,624 at 8192) because every stage of the port's compute path drains with cuCtxSynchronize, which also waits for whatever the issuer has queued. e9b8a5e drives the same stream from the calling thread and queues the next MoE's copies only under the GDN prompt producer's sync-free kernel batch (prefetch_point). Job 0708 (same binary, A/B with the switch): at 8192-token chunks moe.ring fell 1,108 -> 776 ms but the GDN windows grew 537-578 -> 825-850 ms (chunk 2,676 vs 2,730 ms, 2,992 vs 2,941 tok/s on 16,000 tokens: within noise); at 2048 the chunk got slower (1,755 vs 1,410 ms). The copies moved out of the ring into the GDN windows instead of overlapping them. The stream is therefore EXPERIMENTAL and off (LELEKA_PREFILL_STREAM=1 enables it). Real overlap needs the compute path's drains scoped to its own streams (cuStreamSynchronize of the producers' streams instead of cuCtxSynchronize) and the per-stage finite-status readbacks deferred; both are driver-wide changes.

Final RTX 5090 numbers (job 0708, stream off, tier off, greedy ids equal to every earlier build of the same chunking): 16,000 tokens at chunk 8192 2,941 tok/s (5.44 s), 8,000 tokens 1,878 tok/s (first prompt of the process, includes the one-time expert finite admission ~0.8 s); chunk 2048 ~1,430-1,450 tok/s steady. One-pass row staging (84983ab): init.upload 179-189 -> 131-141 ms per 8192 chunk.

Laptop (RTX 3080, same harness, cache off for pairing: LELEKA_LAPTOP_PROMPT_CACHE=0)

The page cache dominates there: the same Y binary went from TTFT 8.6-9.4 s (after the WSL restart) to 4.1-4.7 s (warm, later runs). Paired runs Y/E and Y/G show no difference beyond that noise; outputs identical. Per-command profile of an 80-token prompt (Esync): GDN 1.29 s per prompt, of which the per-layer weight dequantization is 0.80 s (QKV 10 ms, Z 4.7 ms, Out 7.4 ms per layer: the Original's one-thread-per-32-values kernel, ~6 GB/s on this GPU) and state staging 0.26 s. With the conversation cache on by default (--prompt-cache 6), a chat prompt is read as two batched chunks (to the last turn token, then the short tail): the tail chunk costs ~2.8 s of fixed per-chunk work on the laptop, where the Original reads tails of <= 64 tokens with verify windows.

Laptop, paired with the Original (same session, ~/LAPTOP_GPU.lock, Original first)

docs/laptop-profile.md row-2 arguments for the Original (--expert-cache auto --prefill auto --spec 4 --spec-min-p 0.5 --mtp --kv int8 --prompt-cache 0 --resident-experts (falls back to mmap) + KAR PLE L2, 16 new tokens, 2 passes); the port e9b8a5e with the VRAM tier auto + profile and the conversation cache off. Greedy outputs identical between the two engines on all three prompts (16 tokens).

engine prefill tok/s pass 0 pass 1 TTFT s pass 0 pass 1
Original 2.9, 4.3, 7.7 7.0, 12.6, 16.4 32.2, 19.6, 10.2 13.8, 6.7, 4.9
port e9b8a5e 4.5, 8.6, 11.9 13.4, 15.2, 15.3 20.4, 10.4, 7.0 7.4, 5.9, 5.6

(The page cache dominates both: the earlier docs/laptop-profile.md run had the Original at 12.4 and the port at 3.1 tok/s; n = 6, indicative only.)

Compute-stage kernels (branch claude/prefill-kernels)

Goal: the non-DMA compute of the batched prefill (input, init, GDN, QSA, GR reads and injections). Everything below is exact: every change keeps the arithmetic expression of the kernel it replaces, and each is behind a switch so the same binary measures old against new.

change switch (default on) what it replaces
chunk-wide KAR PLE + embedding staging LELEKA_PREFILL_STAGED_INPUT=0 per token: 16-row scatter, 16-row decode, synchronous readback, host assembly and upload of the 10240-float residual row
guards folded into the producers LELEKA_PREFILL_FUSED_SCANS=0 GR read/inject and GDN chain: a second pass over every 335 MB tensor just to scan it
element-parallel dequantization (GDN, QSA) LELEKA_PREFILL_FAST_DEQUANT=0 one thread per 32 values, ~6 GB/s on the laptop
GDN chunk starts from the mirror's device state LELEKA_PREFILL_GDN_MIRROR=0 canonical host state converted and uploaded for every layer of every chunk
two-barrier GDN recurrence (EXPERIMENTAL, off) LELEKA_PREFILL_FAST_REC=1 gdn_rec_cols_pipe_kernel

Input staging: the rows of a chunk (16 per token) are resolved against the cache map on the host, the packets of the misses are read in parallel (pread, up to 32 threads), and one kernel decodes all of them straight into the arena's PleEmbedding span; each distinct token's embedding row is decoded once, uploaded once, and broadcast to the four residual streams by a kernel. KarPleCache::stage_batch evicts least recently used rows the chunk does not touch (pure lookup_rows/assign_slots, unit tested with a tiny capacity). A non-finite value, bad slot or partition is refused by the device flags with the same error class as the host path.

New sealed asset crates/cuda-driver/src/prefill_stage.cu (precise recipe, nvcc 13.2.86; extra-seal-plan.json group prefill-stage, sm_86 companion in ptx-sm86/). Kernels: leleka_prefill_ple_scatter, _ple_decode, _embed_broadcast, _gr_norm_rs, _gr_mix_r, _gr_write_norm_rs, _gr_write, _dequant_{q8_0,q4_k,q5_k,q6_k,iq4_nl,iq4_xs}, _gdn_conv_tiled, _gdn_l2, _gdn_out_norm, _gdn_rec.

Quality gates

  • Model-free GPU probes (LELEKA_PREFILL_STAGE_GPU=yes, RTX 3080 laptop; the same tests run on the 5090 in job 960): staged_input_equals_the_per_token_kernels_bit_for_bit (512,000 PLE values and the broadcast rows), gr_fused_guards_equal_the_separate_guard_kernels_bit_for_bit (outputs and flag words, clean and with a non-finite R word, gate word and an overflowing product), gdn_stage_kernels_equal_the_originals_bit_for_bit (all six dequantized formats, convolution, L2 norm, output norm and their status words) and gdn_recurrence_equals_the_original_bit_for_bit (t = 1, 7, 200, 1500 and an overflowing state): every comparison is identical bit for bit, maximum difference 0.
  • LELEKA_TRACE_PREFILL_R=1 prints an FNV-1a hash of the bits of every row of the chunk's final residual R (all four streams, after 48 layers). On the laptop (prompts of 300, 1,200 and 2,600 tokens, chunks 299 / 1,199 / 2,048 / 551) the hash of every chunk is identical between the all-switches-off arm and the default arm, and the 16 greedy output ids of the three prompts are identical.

RTX 5090 (job 961, one binary, tier off, greedy, 16 new tokens; B = the four switches off, N = defaults)

Greedy output ids of all 16 runs of N and B equal job 0708's (2048-token chunks: 8000 271,248068,271,248069,..., 16000 271,248068,198,760,...; 8192-token chunks: both prompts 271,248068,271,248069,...). LELEKA_TRACE_PREFILL_R=1 (cap 8192): the FNV-1a hash of every row of the final residual R of each chunk is identical between N and B (7999 rows 11c898a92de08a9b; 8192 rows 7ea88178bf76c5a1; 7807 rows 8a27594e2b566315).

Prefill tok/s, two interleaved repetitions (B reproduces job 0708: 2,970 vs 2,941 at 16,000 tokens / 8192):

chunk tokens B rep1 / rep2 N rep1 / rep2 gain
2048 8,000 1,096 / 1,168 1,298 / 1,300 +15%
2048 16,000 1,468 / 1,456 1,651 / 1,651 +13%
8192 8,000 (first prompt of the process) 1,935 / 1,879 1,994 / 2,186 +6%..+16%
8192 16,000 2,971 / 2,969 3,735 / 3,715 +25%

Per 8192-token chunk (job 961 rep1, first chunk of the 8,000-token prompt; ms, host spans, expert DMA still not overlapped): total 2,713 -> 2,158; input 178 -> 3; init 169 -> 1.5; attn_read 115 -> 68; ffn_read 114 -> 67; inject 125 -> 75; gdn 548 -> 471 (compute 428 -> 365; state_in 38 -> 40, state_out 32 -> 34); qsa 253 -> 241; moe 1,303 -> 1,279 (ring 1,114 unchanged). The 16,000-token prompt's 7,807-token chunk: 2,671 -> 2,125.

Where the rest goes (job 961 PROFILE_SYNC arm, one chunk, ms): GDN cuBLAS GEMMs 152 (QKV 68, Z 43, Out 41; ~220 TFLOP/s, at the hardware limit), recurrence 155, convolution 17, output norm 16, L2 6, dequantization 9 (was 29), GDN host row/state bookkeeping ~75; QSA attention 113 (already tensor-core mma), index/norm/rope 38, Q projection 33, Out projection 17, KV append 14; GR reads: mix 47, GEMMs 70; injection (write + next norm) 55. What is left outside the expert ring (~1.04 s) is bandwidth-, tensor-core- or latency-bound; the per-stage host drains remain (claude/prefill-overlap).

Laptop (RTX 3080, WSL2, mapped experts, VRAM tier auto + profile, KAR file; ~/LAPTOP_GPU.lock)

Prompts of 300 / 1,200 / 2,600 tokens (the first 2,600 ids of job 0708's 8,000-token prompt), chunk 2,048, 16 greedy tokens, LELEKA_LAPTOP_PROMPT_CACHE=0; ABAB of the all-switches-off arm and the defaults, two passes per process. Greedy outputs of all runs are identical, and with LELEKA_TRACE_PREFILL_R=1 the hash of the final residual of every chunk (299, 1,199, 2,048, 551 rows) is identical between the arms. Sum over the four chunks of the warm pass, mean of the two processes (ms):

stage switches off defaults
input + init 588 9
attn_read + ffn_read 553 416
inject 288 211
gdn (compute 1,192 -> 865; state_in 353 -> 288; state_out 331 -> 324) 2,016 1,586
qsa 654 569
moe.ring (expert bytes over PCIe/page cache: unchanged) 19,204 19,650

The GDN weight dequantization, 0.80 s of a short prompt's GDN on this card, is now 36 ms per 2,048-token chunk (gdn#3/#5/#20); end-to-end prefill tok/s moves by 0-5% because the expert DMA from the page cache is 85% of the time. The remaining laptop-only costs are input.ple (random 20-byte reads of the 55 GB KAR file that is not in the page cache: ~0.5-1 s per prompt, 32 reader threads) and the 3 MB state download (gdn.state_out, WSL pageable copy).

Expert tier ON (job 962: EXPERT_SLOTS=2048 per layer, profile, PCIE_FRAC=0.55; same binary, B = switches off). All greedy ids equal job 0708's reference of the same chunking:

chunk tokens B N gain
2048 8,000 / 16,000 1,178 / 1,477 1,316 / 1,685 +12% / +14%
8192 8,000 (rep1 / rep2) 1,639 / 1,886 1,959 / 2,123 +20% / +13%
8192 16,000 (rep1 / rep2) 2,897 / 2,902 3,606 / 3,627 +24% / +25%

LELEKA_PREFILL_FAST_REC=1 (two-barrier recurrence), bit-identical on the 5090 probe (0.81 -> 0.71 ms at 1,500 tokens, one layer), measured 3,756 / 3,662 tok/s at 16,000 tokens (tier off / on) against 3,735 / 3,606 for the default: within noise at chunk level, so it stays EXPERIMENTAL and off. Defaults of the other four switches are on. What remains: the expert DMA (claude/prefill-overlap), the GDN recurrence (155 ms, a 128-step FMA chain per token that cannot be reordered without changing bits), cuBLAS GEMMs at the tensor-core limit, per-layer host drains.

Expert stream overlap (branch claude/prefill-overlap, jobs 950-968, RTX 5090)

Goal: hide the expert DMA of an 8192-token chunk (24,576 experts = 50.3 GB, i.e. every expert of all 48 layers at 2.05 MB; a 16,000-token prompt moves it twice) under the mixers. The per-layer ring paid it layer by layer (moe.ring ~1,110 ms of a 2,140 ms chunk after the prefill-kernels merge, 3,780 tok/s on 16,000 tokens).

What blocked the overlap (and what was removed)

  1. Every compute producer drained with cuCtxSynchronize, which also waits for the copy stream. All of them go through ffi::Api::synchronize; while a chunk's expert stream runs (compute_drain, background stream registered by begin_prompt_stream) it now waits for the legacy default stream and every registered compute stream except the copy stream (all four stream creators register: Blas, Original schedule, owned copy, swap). Outside a chunk stream it is the unchanged context-wide drain. The ring's own layer drain no longer syncs the copy stream either.
  2. cuMemFree synchronizes the whole device (per-layer GDN/QSA/PLE scratch, 6 MB per GDN layer): frees are queued on the Context while the stream runs and executed when it ends (or past LELEKA_PREFILL_DEFER_FREE_MIB, 512); the ring's per-layer status buffer is pooled.
  3. The host-to-device copy engine is a FIFO. Any other transfer of the compute path (synchronous H2D uploads, D2H status readbacks) waits for all expert copies queued before it: model-free probe, job 955 (scripts/h2d_copy_engine_probe.cu): a 327 KB cudaMemcpy waits 0.13 ms behind 2 queued experts, 0.42 ms behind 8, 1.6 ms behind 32, 5.7 ms behind 128+ (= outstanding bytes / 36 GB/s); memsets, kernels and D2D copies do not queue. With a deep queue the waits only moved from the ring into moe.finish (372 ms) and inject (650 ms) (job 953: PREFILL-SITE/PREFILL-STALL call-site histograms). Fixes: an issuer thread (the Original's D-5) keeps only LELEKA_PREFILL_DEPTH (default 2) entries outstanding on the copy stream, event-query paced, the compute thread only waits until an entry is enqueued before ordering its stream after it; all-zero uploads become device memsets while the stream runs (LELEKA_PREFILL_ZERO_MEMSET=0 disables); the routed slot table (327 KB, non-zero) is uploaded before the ring instead of after it.
  4. Not done (open, see below): ~1,100 small blocking H2D/D2H calls per chunk remain (status readbacks, shared-layer weight uploads, GDN state staging, QSA tables); each costs up to the queue depth in latency and keeps the depth at 2.

The stream copies every expert of every layer (prefill.cpp stream_all), while the per-layer ring copies only the routed ones: at 2,048 rows only ~60% of the experts are routed, so the stream is 20% slower there (jobs 961/962: 1,330 vs 1,670 tok/s) and nearly free at 8,192 (~100% routed). It therefore starts at 4,096 rows (STRATA_PREFILL_STREAM_MIN); LELEKA_PREFILL_STREAM=0 restores the per-layer ring everywhere.

Results (paired passes alternating inside one process, same binary and prompts, greedy 16 tokens)

config prompt per-layer ring expert stream notes
chunk 8192, tier off, depth 16 (job 960) 8,000 3,764-3,784 4,367-4,406 +16%
chunk 8192, tier off, depth 16 (job 960) 16,000 3,780 4,275-4,358 +13%
chunk 8192, tier off, depth sweep (job 958) 16,000 3,795-3,799 2: 4,805-4,811; 4: 4,163-4,181; 8: 4,227-4,293; 16: 4,300-4,350; 32: 4,199 depth 2 best
chunk 8192, tier off, depth 2, 12 passes (job 963) 16,000 - 4,789-4,851 +27%
chunk 8192, tier on (job 962, depth 16) 8,000 / 16,000 3,573 / 3,625-3,686 4,499-4,528 / 4,631-4,654 +26%
chunk 2048 (jobs 961/962, stream forced) 8,000 / 16,000 1,670-1,718 / 1,659-1,711 1,325-1,380 / 1,324-1,384 -20%: off below 4096 rows

Greedy ids: identical between the two modes on every paired pass (8000 and 16000, chunk 8192 and 2048, tier off and on) and equal to job 0708's ids for the same chunking. With the tier on the 16000-token answer at chunk 8192 is a near-tie that the unmodified head already flips between passes (one OFF pass of job 962 answered 271,248069 instead of 198); the stream's passes all gave 198. Race hunt (job 963): 36 consecutive full 16,000-token prefills (12 plain, 12 with pseudo-random host delays between the copy enqueues LELEKA_PREFILL_STREAM_JITTER, 12 with the same jitter at depth 1): identical ids, no CudaExecution. compute-sanitizer racecheck/synccheck only see shared-memory hazards inside a kernel, which this change does not touch; the cross-stream ordering is by events (copied/used per ring slot), and the repeated runs under jitter are the evidence.

Copy rate, bytes per chunk, tier

Every 8192-row chunk streams 24,576 entries x 3 copies of 682 KB = 50.3 GB (job 950+ PREFILL-COPY: per-entry event timing 35.5-36.5 GB/s, independent of queue depth and of concurrent kernels; cudaHostAlloc memory in the model-free probe 40.3-40.9 GB/s for 3x682 KB, 1x2 MB, depth 4-64, 1-2 streams; the registered bank is ~12% slower). With the stream the DMA is busy ~77% of the chunk (stream window 1.6-1.8 s for a 1.42 s copy floor at 35.5 GB/s). The VRAM expert tier (2048 slots per layer, 1891 lent) removes only ~0.6% of the entries at chunk 8192 (24,419 vs 24,576) and 4.7% at 2048 (23,415): prefill with the tier streams essentially the same bytes. Chunk sizes above 8192 are not possible: the prompt arena, GEMM and MMQ shape checks are capped at 8192 rows (30+ sites, 81920-row MMQ layouts); bytes per token fall 4x from 2048 to 8192 as the per-chunk traffic is constant.

Late chunks (180,000-token prompt, job 915/957)

Per-chunk time grows from 2,120 ms (position 0) to 2,480 ms (position 172k) on the unmodified head, of which QSA 250 -> 537 ms and moe.ring 1,117 -> 1,200 ms; GDN is flat. Per-command profile (job 957, 8 -> 90k -> 164k): qsa index+norm+rope run 49 -> 167 -> 269 ms (the indexer scores every earlier block: ~1.4 ms per 1k positions per chunk), kv append 15 -> 36 -> 54 ms, attend flat 124 ms, proj Q/out flat. The indexer is the stage that grows with position; its kernels were not touched here.

Final defaults and the 180k prompt (jobs 965-968)

  • Queue depth (job 965, 16,000 tokens @8192, same process): depth 1 3,343; 2 4,805-4,821; 3 4,238-4,310 tok/s (per-layer ring 3,790); depth 2 is the default. The stream runs one expert (2 MB) ahead of its consumer, so the copy engine is never idle for long but any other transfer waits at most ~2 MB / 36 GB/s.
  • Shipped defaults vs LELEKA_PREFILL_STREAM=0, separate processes (job 966, second and third pass): chunk 8192 8,000 tokens 4,851 / 4,844 vs 3,760 / 3,766 tok/s, 16,000 tokens 4,820 / 4,816 vs 3,796 / 3,778; chunk 2048 identical (1,670 vs 1,671 / 1,656 vs 1,655: the stream is off below 4096 rows). All greedy ids equal.
  • Chunk 4096 (job 967): stream = ring (2,598-2,601 vs 2,583-2,584 for 8,000 tokens; 2,593-2,598 vs 2,598 for 16,000), ids equal: copying every expert costs what the overlap saves.
  • 180,000 tokens, tier on, chunk 8192 (job 968, passes alternating in one process): stream 40.9 s / 39.9 s (4,397 / 4,508 tok/s) vs ring 51.0 s (3,532 tok/s) = +25..28%; identical ids. Per chunk (position 8k / 82k / 164k): stream 1,680 / 1,773 / 1,905 ms (qsa 272 / 398 / 530, moe.ring 539 / 582 / 581, gdn 464, inject 85) vs ring 2,080 / 2,320 / 2,473 ms (moe.ring 1,057 / 1,169 / 1,186). The unmodified Original needs 39.4 s for the same prompt. What still grows with position is the QSA indexer (above) and, slightly, the ring.

Merged copy path

claude/ledger's page-aligned expert supersets (+12% on the ring: DMA 36 -> 47 GB/s) are merged into the stream: stream entries carry the 4 KiB-aligned supersets, their slot offsets and the offsets of the exact pieces, and the gather reads the exact pieces there (LELEKA_PREFILL_ALIGNED_COPY=0 turns both paths off). The schedule agent's deferred guard status (LELEKA_PREFILL_DEFER_GUARDS=1, experimental, default off; +1.6% in its A/B) is merged. Open: runs of consecutive experts per role in one copy / cuMemcpyBatchAsync (50-51 GB/s in the roofline probes), the ~1,100 remaining blocking small copies per chunk, QSA indexer growth with position.

Aligned copies in the stream, deferred guards (jobs 970-973, 975)

  • Bit gate (job 970, LELEKA_TRACE_PREFILL_R=1, which slows every chunk ~0.3 s: the tok/s of that job are not comparable): the final residual R hash of every chunk is identical between ring and stream and between aligned and literal copies (8000 tokens 11c898a92de08a9b; 16000 tokens 7ea88178bf76c5a1 / 8a27594e2b566315, 12 runs each), greedy ids equal. Aligned copies in the stream: DMA 34.9 -> 44.6 GB/s per entry, stream window 1,603 -> 1,457 ms, +8% tok/s over the literal-layout stream.
  • Deferred guard status (job 971, ABBA in separate processes, 16,000 tokens, with the hash trace): 4,349-4,363 -> 4,553-4,571 tok/s (+4.8%), hashes and ids identical; on by default, LELEKA_PREFILL_DEFER_GUARDS=0 restores the immediate guards.
  • Clean timings without hashing, stream + aligned copies, deferred guards still off (job 972, tier on, chunk 8192): 8,000 tokens 4,953-4,987 tok/s vs 4,088 for the (aligned) ring; 16,000 tokens 5,123-5,138 vs 4,215-4,216.
  • 180,000 tokens, tier on (job 973): stream 37.8 / 36.8 s (4,756 / 4,895 tok/s) vs ring 45.3 s (3,977 tok/s); the recorded Original needs 39.4 s. Per chunk at position 8k / 82k / 164k: stream 1,535 / 1,615 / 1,752 ms (gdn 460, qsa 248 / 374 / 509, moe.ring 434 / 452 / 453, inject 84) vs ring 1,843 / 2,057 / 2,208.
  • Where the host blocks at depth 2 (PREFILL-COPY ... blocking:): alloc 83 calls / 9 ms, h2d 239 / 12 ms, d2h 761 / 57 ms, memset 523 / 2 ms, drains 1,536 / 12 ms, stream/event syncs 668 / 860 ms (the real waits for the GPU's work). Removing all remaining small copies is therefore worth at most ~90 ms of a ~1.55 s chunk; what is left of the chunk is stage compute (gdn 460, qsa 250 growing with position, ring 434 for ~330 of kernel work).

Late-chunk QSA selection (branch claude/perf3, jobs p30/p31, RTX 5090)

Goal: the per-chunk time of a long cold prompt grows from 2.07 to 2.23 s over 100k tokens at chunk 8192 (job 97z), of which the QSA stage goes 253 -> 296 ms (100k) and 267 -> 507 ms (180k). Per-command profile of the 180k prompt (job 97z, position 147k, ms per chunk): tc scores 17 -> 131, topk 12 -> 94, kv copy_prefix (host -> arena stage, the whole prior prefix every chunk) 2 -> 37, score scan 13.6 flat, kv append 13 flat, attend 104 flat.

crates/cuda-driver/src/qsa_late.cu (sealed, extra-seal-plan.json group qsa-late, the work of the claude/qsa-late worktree, imported here) replaces two launches of the resident prompt indexer with identical results: leleka_qsa_topk_fast for the Original's qsa_topk_ref (coalesced score reads, same weighted radix threshold, ids in the same order) and leleka_qsa_score_scan_fast for qsa_score_scan over the reach blocks a tile can hold. Wired into QsaPromptIndexWorkspace::run_resident_arena (prompt only; the decode window keeps the Original's launches); LELEKA_QSA_LATE=0 keeps the original launches, LELEKA_QSA_LATE_SEQ=1,0,0,1 switches per pass inside the long smoke. A build with sm86 and no companion image (the sealed asset is sm_120) keeps the original launches (qsa_late::usable). The sm_86 companions of qsa_late.ptx and chain_window.ptx were built with sm86_assets.py build (the full set: it also refreshed the two stale original_verify_window_pcie* companions; cargo test -p leleka-cuda-driver --features cuda-driver,sm86 is green again, 388 tests), but the fast path stays opt-in on a pre-Blackwell card (LELEKA_QSA_LATE_SM86=1) because its identity probe has not run on sm_86 hardware (the probe needs the pinned cuBLAS, absent from the WSL image).

Model-free probe (LELEKA_QSA_LATE_GPU=yes, qsa_late::gpu_tests, ids equal to the Original's in every case; rerun here on the 5090, bin/drv-a6, 9 cases): at position 180,000 with 256 rows the Original's qsa_topk_ref takes 0.328 ms (continuous scores) / 0.323 ms (16 tied levels) and the fast kernel 0.258 / 0.257 ms (-21 %); at 100,003: 0.143 -> 0.138; at 261,000: 0.348 -> 0.333; below 65k the fast kernel is 1.5-3.5x faster in absolute terms but those launches are 0.02-0.12 ms. The 4.245 ms of the first run of this probe (97z-qsa-aprobe, case 0) was the first launch of the process (module and clock warm-up), not a property of the kernel: the branch's headline "4.2 -> 0.26 ms" does not reproduce; the honest synthetic gain is -0.07 ms per launch at 180k, which over 384 launches per chunk is -27 ms of a 2.2 s chunk.

Agreement (job p31, LELEKA_TRACE_PREFILL_R=1, passes late on / off in one process, 100,000 and 180,000 tokens, chunk 8192, tier on): the FNV-1a hash of every row of the final residual R of all 13 and 22 chunks is identical, and the 16 greedy ids are identical (271,248068,198,760,...). Bit-identical, so no tolerance applies.

Paired A/B (job p30, rt-a2, one process, passes 1,0,0,1 per prompt, default stream, tier 2048 slots per layer + profile, PREFILL-PROFILE per chunk; pass 0 of the process pays the one-time expert finite admission):

prompt arm prefill s (passes) QSA stage, sum over the chunks (ms) QSA per chunk, last quarter (ms)
100,000 late off 22.71, 22.55 4,004, 4,012 303, 303
100,000 late on 24.91 (warm-up), 22.86 3,890, 3,898 296, 296
180,000 late off 37.96, 38.87 8,588, 8,622 507, 507
180,000 late on 38.27, 37.36 8,372, 8,335 492, 493

The QSA stage is 2.8-3.1 % shorter in every pair (-110 ms at 100k, -250 ms at 180k, -14 ms per late chunk); the prefill wall time differs by -1.6 % (180k) and +1.0 % (100k, passes 1-3) with the pass-to-pass noise of the expert stream (the moe and gdn sums move by +-250 ms between passes of one arm), i.e. not measurable end to end. Default on (exact, never slower at stage level); expected gain 0.1-0.25 s of a 22-38 s cold prefill. The measured stage gain (-14 ms per late chunk) is half the synthetic one (-21 % per launch = -27 ms per late chunk: 0.07 ms x 384 launches).

What grows with the position is therefore tc scores (the Original's tensor-core score kernel, 0.34 ms per launch at 147k: ~7 TFLOP/s on 2.4 GFLOP, a kernel of the sealed Original asset) and the per-chunk copy of the whole prior KV prefix into the arena stage (kv copy_prefix, 37 ms at 147k, DMA-bound at ~50 GB/s). Both are open (a bit-identical rewrite of the score kernel is the remaining ~100 ms per late chunk).

Prefill chunk 8192 in live sessions: where the VRAM goes, and the loan ladder (branch claude/perf3, jobs p32 done, p34/p35 queued)

Question: a live Codex run with --original-prefill-capacity 8192 --prefix-cache-block 8192 (e2e job 9e4) lost 2 % decode against the 2048 run (127.9 vs 130.7 tok/s, job 9e3), "presumably VRAM taken from the expert tier". It is not.

  1. VRAM. The two engine logs open with the same numbers: DBG vram after prompt executor and draft: free 23103 MiB (2048) / 23101 MiB (8192), and expert cache 8704 slots (21.58 GiB, per layer) in both. The prompt path's chunk buffers are not an allocation of its own once the tier is on: the stack lends the cache's last slots to every chunk (lend_prefill_tail) and refills them before the decode (refill_lent). What the chunk size changes is how many slots are lent: 891 at 2048, 1,895 at 8192 (3.9 GB; the arena is ~1.15 GB fixed plus ~0.33 MB per row). The tier the decode sees is the same 8,704 slots.
  2. The price of a bigger loan is paid per request, in the time to first token: refill.queue 94 ms (1,895 slots) against 33 ms (891) in the engine's own profile lines of 9e4 / 9e3, because every request that reads at least one chunk refills the whole loan (DMA-bound, ~48 us per slot). Warm Codex turns re-read 60-500 tokens, so they pay it for nothing.
  3. Decode. The two live sessions are not a pair: 9e4 reached 128k tokens of context (33 of 85 requests above 96k) and 9e3 113k (15 of 64); decode falls with context (130 -> 107 tok/s medians in both). A paired replay on the same recorded prompts and replies is the A/B (job p32, rt-a3, one process, fresh stack per arm, teacher forced, first 40 requests of 9e3, auto-sized per-layer tier, profile, adaptation and seed at their defaults, prefix cache 20 GiB with the reply tail; arms A B B A = chunk/grid 2048, 8192, 8192, 2048; scripts/perf3/parse_replay.py, parse_spans.py):
arm chunk / grid decode tok/s rounds window ms per round (round.run_window) TTFT sum (s) re-prefilled tokens prefill.refill per request (ms) warm-turn TTFT (ms, n)
A1 2048 / 2048 140.6 17,668 22.22 60.2 67,906 188 763 (38)
B1 8192 / 8192 136.2 18,060 22.29 54.8 87,841 249 866 (35)
B2 8192 / 8192 137.0 18,049 22.05 45.7 67,916 248 858 (38)
A2 2048 / 2048 135.6 18,020 22.46 60.4 68,855 190 781 (37)

The decode is the same machine: 22.05-22.46 ms per verify round in all four arms. The tok/s differences are the number of rounds for the same 64,288 forced tokens (17,668 ... 18,060), and the two 2048 arms alone differ by 3.6 % (140.6 vs 135.6), i.e. the arm-to-arm noise of this replay (tier adaptation and prefix retention are timing dependent) is larger than the 2048 / 8192 difference (138.1 vs 136.6 pooled, -1.1 %). The live "-2 %" is context depth plus that noise, not a cost of the 8192 chunk. What the chunk does cost is TTFT of warm turns: +90 ms per warm request (+11 %: 60 ms more refill plus larger tail checkpoints, prefix.export_kv + park_tail 0.59 s per 40 requests against 0.29 s) and what it earns is the cold reads: the two requests that re-prefilled 57.9k tokens took 31.2 s at 2048 (1,857 tok/s) and 13.1 s at 8192 (4,422 tok/s), TTFT sum -24 % at equal re-prefill volume (B2 vs A2: 45.7 vs 60.4 s).

The loan ladder (original_serve_stack.rs, LoanTier, tier_for): a chunk of at most 512 / 2048 rows borrows (and so refills) the slots of an arena laid out for that many rows instead of the full 8192-row loan; the lent slots are the last ones, so the rungs nest, and the refill is driven by the per-slot NeedsRefill state, so a small loan refills less with no other change. The stack opens with the rungs it can afford (prompt loan ladder: ... in the operator log). The arithmetic of a chunk does not depend on the arena capacity (buffers only); the gate is the hash of the final residual R of every chunk with and without the ladder (job p34: 24 requests of 9e3, arms N = !notiers, T = !tiers, static tier so the arms do identical work) and the paired A/B of TTFT (job p35, arms T N N T). Off by default (LELEKA_PREFILL_LOAN_TIERS=1, replay arm flag !tiers) until p34 passes; both jobs are queued behind the owner's disk-maintenance job (see the status in the report).

Recommended once p34/p35 pass: prefill_capacity: 8192 in the rtx5090-32g profile with the ladder on (cold re-reads at 4.4k tok/s, warm turns unchanged); the grid follows the capacity (--prefix-cache-block 0 = the chunk), the trade being the reuse granularity after a fork (up to 8192 tokens re-read instead of 2048: 67.9k vs 67.9k re-prefilled tokens in B2 vs A2, equal on this session, 87.8k in B1 where the prefix store evicted differently).

Not done: the Original reads tails of at most 64 tokens through verify windows (short_read: 64 in PromptOptions; the port forces the batched read), while the port pays ~0.45 s of expert streaming for any chunk, even 10 tokens (TTFT median 0.57-0.65 s for 0-64 re-prefilled tokens in 9e3/9e4; 38 of 85 requests of 9e4 are in that class). That is the largest remaining TTFT item of live agent sessions.

Conditions of the perf3 measurements

RTX 5090 (driver 595.99.02, CUDA 13.2.2 toolchain, cuBLAS 12.9.1.4), Ryzen 9 9950X3D, kernel 7.0.0-38, 121 GB RAM; model Qwen3.8-Flash-Next IQ3_S + KAR level 2 (qwen-iq3-s-l2-v4.kar, archive sha256 536d612c...afa5, provenance file sha256 prefix 3edceb51), MTP pack data/mtp-rt; binaries rt-a2 (p30/p31, sha256 prefix 247af7ea), rt-a3 (p32, 2d3b0a81), rt-a7 (p34-p36 queued, 7024fc54), all cargo test --release --no-run -p leleka-runtime --lib builds of claude/perf3 (commits in git log). p30/p31: long-context smoke, chunk 8192, INT8 KV resident 20,480, ceiling 262,144, budget 30 GiB, tier 2048 slots per layer + the 98x profile (baseline/profile.bin, PCIe fraction 0.55), default expert stream, prompt = the smoke's code filler (token counts exact), 16 greedy tokens, one process, passes alternating. p32: replay of the first 40 requests of the capture 9e3-crm-tail-reply (64 real Codex requests of 17-113k tokens, recorded prompts and replies, teacher forced), auto-sized per-layer tier (8,704 slots, 21.58 GiB), profile baseline/profile.bin, adaptation / seed / spec window at their defaults, prefix cache 20,000 MiB with the reply tail, cold page cache not controlled. Warm-up: pass 0 of a process pays the one-time expert finite admission (~0.8 s). The arena carve differs structurally below 1,024 rows (72 spans for capacity <= 512, 448 above; 0.72 GB at 512 rows against 2.2 GB at 2048 and 4.6 GB at 8192 with synthetic blob sizes), which is why the 512-row rung of the ladder is part of the p34 identity gate.

Pending gates (queued in holder2 behind the owner's 000-disk-maintenance; run in this order)

Copy of the job files: scripts/perf3/ (common.sh, p30-p36); binaries ~/work/leleka-perf3/bin/rt-a7. After a job: scp is not needed, the logs are on the server.

job question read with decision
p34-loan-tiers-identity does the loan ladder change any chunk's arithmetic? (arms N full loan, T ladder; static tier; R hash per chunk) python3 scripts/perf3/cmp_rhash.py ~/work/leleka-ttft-runs/loan-tiers-id.full N T IDENTICAL on all chunks -> the ladder may be defaulted (loan_tiers_enabled); any difference or a failed request (a missing span of the 512-row layout) -> keep off
p36-chain-down-ab window time per round and bit identity of the tiled down projection in the real window (arms D P P D, P = !w8) parse_spans.py ~/work/leleka-ttft-runs/chain-down-ab.full round.run_window window.verifier and parse_replay.py same rounds / tokens per round / agreement in D and P (bit-identical work) and round.run_window per round lower in D -> keep the tiled default; otherwise LELEKA_CHAIN_DOWN=w8
p35-loan-tiers-ab warm-turn TTFT and cold-read speed of the ladder at chunk 8192 (arms T N N T) parse_replay.py ~/work/leleka-ttft-runs/loan-tiers-ab.full warm-turn TTFT of T at or below the 2048 arms of p32 (~770 ms) with the cold reads of 8192 (~4.4k tok/s) -> set prefill_capacity: 8192 in the rtx5090-32g profile with the ladder on

The jobs were written to be short (<= 25 min of work plus the model load); p35 is dated two hours ahead so that p34 and p36 run first.