Files
leleka/docs/prefill-throughput.md
T
Pavlo RatushnyiandClaude Opus 5.5 499faec1ae perf(prefill): the chunk expert stream is EXPERIMENTAL and off by default
Job 0708 (same binary, LELEKA_PREFILL_STREAM A/B): with next-MoE copies
queued under the GDN batches the ring fell 1,108 -> 776 ms per 8192-token
chunk but the GDN windows grew by about as much (2,676 vs 2,730 ms per
chunk), and 2048-token chunks got slower (1,755 vs 1,410 ms). The
per-layer copy ring is the default again; LELEKA_PREFILL_STREAM=1 enables
the stream for experiments.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-17 00:03:18 +02:00

10 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.)