Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
18 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
19fac1brow validation once per set (linear), same checks and order.5416f4ea routed expert's bank bytes are admitted finite once per bank (bit per layer/expert in the MoE cache).ae283e6pageable (file-mapped, laptop) banks: prefill.cpp'sStagerin 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, driverrun_stream_all_forwardexists, 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) andgdn_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=1prints 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.