diff --git a/crates/cuda-driver/src/mmq_original_host_stage.rs b/crates/cuda-driver/src/mmq_original_host_stage.rs index dcca1f66..5c3e3c00 100644 --- a/crates/cuda-driver/src/mmq_original_host_stage.rs +++ b/crates/cuda-driver/src/mmq_original_host_stage.rs @@ -31,6 +31,63 @@ fn verify_enabled() -> bool { #[derive(Clone, Copy)] pub(super) struct Job { pub(super) pieces: [(usize, usize, u32); 3], + /// Memory priority the helper reads these pages at (Windows; 0: leave the thread's default): see `layer_priority`. + pub(super) priority: u8, +} + +/// The default memory priority of a thread on Windows. +const NORMAL_PRIORITY: u8 = 5; +/// The priority of the layers whose experts are not meant to stay cached. +const LOW_PRIORITY: u8 = 1; +const LAYERS: usize = 48; + +/// Memory priority for the page-cache pages of the experts a layer's helpers read (Windows only, `LELEKA_STAGE_PRIORITY=on|off|`). +/// +/// Every chunk of a long prompt reads every expert the GPU does not hold, layer 0 to 47, then starts again at layer 0. The file cache keeps +/// the most recently used pages, so a scan longer than the cache finds none of its pages again: with 10 GiB of cache and 40 GiB to read, +/// every chunk reads all 40 GiB from the drive. Reading the first layers at a low memory priority makes the memory manager repurpose their +/// pages first, so the pages of the last layers stay cached from one chunk to the next (measured with `wsrun-lvv/iobench.ps1`: a cyclic +/// scan of 12 GiB through ~7 GiB of cache took 3.0-3.7 s per pass without it and 0.8 s with it). The first `layers - n` layers read at +/// low priority, where `n` layers fit the budget: `LELEKA_STAGE_PRIORITY=`, or with `on` 90 % of the available RAM less 2 GiB. Only +/// which pages the cache keeps changes, never a byte read or its order. Off unless asked for (until measured on the laptop). +pub(super) fn layer_priority(layer: usize, per_layer_bytes: usize) -> u8 { + #[cfg(windows)] + { + use std::sync::OnceLock; + static MODE: OnceLock>> = OnceLock::new(); + let mode = *MODE.get_or_init(|| match std::env::var("LELEKA_STAGE_PRIORITY").ok().as_deref() { + Some("on") | Some("auto") | Some("1") => Some(None), + Some(other) => other.parse::().ok().filter(|g| *g > 0.0).map(|g| Some((g * (1u64 << 30) as f64) as u64)), + None => None, + }); + let Some(fixed) = mode else { return 0 }; + let budget = match fixed { + Some(bytes) => bytes, + None => match libc::physical_memory() { + Some((_, available)) => (available.saturating_sub(2 << 30) as f64 * 0.9) as u64, + None => return 0, + }, + }; + let high = (budget / per_layer_bytes.max(1) as u64) as usize; + if layer + high < LAYERS { LOW_PRIORITY } else { NORMAL_PRIORITY } + } + #[cfg(not(windows))] + { + let _ = (layer, per_layer_bytes); + 0 + } +} + +/// Set the calling thread's memory priority (a no-op off Windows). +fn apply_priority(priority: u8) { + #[cfg(windows)] + { + let _ = libc::set_thread_memory_priority(u32::from(priority)); + } + #[cfg(not(windows))] + { + let _ = priority; + } } struct Wake { @@ -100,6 +157,7 @@ impl Shared { } fn work(&self) { let mut seen = 0u32; + let mut applied = NORMAL_PRIORITY; loop { let jobs = { let mut wake = self.wake.lock().unwrap_or_else(|p| p.into_inner()); @@ -134,6 +192,11 @@ impl Shared { cancelled |= self.finished(seen); if !cancelled { let job = jobs[j]; + let wanted = if job.priority == 0 { NORMAL_PRIORITY } else { job.priority }; + if wanted != applied { + apply_priority(wanted); + applied = wanted; + } let base = self.buffers[j % ring]; let mut offset = 0usize; let mut finite = true; @@ -480,7 +543,7 @@ mod tests { } fn job(bytes: &[u8]) -> Job { let piece = |i: usize| (bytes[i * 18..].as_ptr() as usize, 18, 20); - Job { pieces: [piece(0), piece(1), piece(2)] } + Job { pieces: [piece(0), piece(1), piece(2)], priority: 0 } } /// `wait` hands out `length` bytes of a page-locked buffer that production allocates uninitialised: a length diff --git a/crates/cuda-driver/src/mmq_original_ring.rs b/crates/cuda-driver/src/mmq_original_ring.rs index d6e0d942..e46ae071 100644 --- a/crates/cuda-driver/src/mmq_original_ring.rs +++ b/crates/cuda-driver/src/mmq_original_ring.rs @@ -728,7 +728,13 @@ impl<'read, 'ctx> Ring<'read, 'ctx> { pieces[role] = (bytes.as_ptr() as usize, bytes.len(), span.raw_kind()); } source.job = Some(jobs.len()); - jobs.push(Job { pieces }); + jobs.push(Job { pieces, priority: 0 }); + } + } + if !jobs.is_empty() { + let priority = host_stage::layer_priority(layer, jobs.len().saturating_mul(blob)); + for job in &mut jobs { + job.priority = priority; } } let charge = groups * 16; diff --git a/docs/laptop-vs-vanilla.md b/docs/laptop-vs-vanilla.md new file mode 100644 index 00000000..685530b1 --- /dev/null +++ b/docs/laptop-vs-vanilla.md @@ -0,0 +1,42 @@ +# Laptop (RTX 3080 16 GB, native Windows): Leleka against vanilla Strata on the 256k agent session + +Branch `claude/laptop-vs-vanilla`, started 2026-10-27 from `main` (3bedec6). Machine: RTX 3080 Laptop 16 GB (driver 617.42), i9-11900H (8 cores / 16 threads, AVX-512, no AVX-VNNI), +47.7 GB RAM, Samsung PM9A1 NVMe, Windows 11, WSL shut down for every run. Model Qwen3.8-Flash-Next IQ3_S (the pinned pack, digest `sha256:80d5e333...66a3`). + +Owner's rule: on large-context work our end-to-end speed has to be better than vanilla Strata on the same machine, a 1 % gain counts. The measure is the 5-turn +long session (`scripts/e2e/long_session.py --turns 5 --target-tokens 245000 --max-output 300 --context 262144`, source snapshot +`C:\Users\pavel\work\wsrun\src-snap\crates`, turn 1 = 45,766 input tokens), nothing shorter. + +## 1. The reference: what vanilla does on this laptop (engine log `wsrun\vanilla\engine-vanilla.log`, `engine/strata` sources, release 0.2.6 / engine 0.1.50) + +| | vanilla Strata | ours before this work (`workstation`, dist build `c40531f`) | +| --- | --- | --- | +| TTFT per turn (s) | 230 / 260 / 279 / 236 / 243 | 432 / 490 / 707 / 514 / 360 (run 1), 449 / 528 / 743 / 611 / - (run 4) | +| prefill tok/s on the tokens actually read | 199.5 / 192.6 / 191.3 / 199.3 / 187.8 | 105-115 | +| decode tok/s | 15.8 / 17.5 / 16.1 / 15.0 / 17.1 | 5.9-7.5 | +| total wall of the session | 1,341 s | about 2,500 s | +| prompt chunk | 8192 (ring 96 slots; the prompt path borrows 1,781 of 2,661 cache slots, 3.41 GiB) | 2048 (borrows 1,154 of 2,234 slots) | +| VRAM left for the desktop | 459 MiB free (it warns, and runs) | 2.8 GiB (`workstation`: budget 13 GiB of 16) | +| expert cache | 2,661 slots (5.10 GiB), profile fill, adaptive | 2,234 slots (4.29 GiB), same profile (identical file, sha256 `8f59b4aa...`) | +| CPU pool | 7 workers pinned to logical processors 2,4,...,14 (one per physical core), host thread on 0, 24 tasks per phase | 12 workers (the preset counts logical cores), 7 pinned, 5 spinning unpinned | +| PCIe share | `pcie_frac` auto-balanced 0.20 -> 0.40 -> 0.30 -> 0.35 from the measured CPU-pool speed | 0 (pageable bank) | +| route tail skip | **ON by default (`STRATA_ROUTE_TAIL_SKIP=7`, changes the answer)**: 25-30 % of the missed experts are never computed | off (experimental, quality gate failed on the 5090) | +| exact sm_86 speed switches | 9 on by default (ADAPT_NOWAIT, ADAPT_LAG=2, CPU_IQ_ALL_EXACT1, SIDE_MIXER=2, ATTN_DAILY, GR_UP75, GU_SWIGLU, QSA_ROWS, B6_MMVQ_ROWS) | none (rows 2.1 / 2.2 of the port plan, other agent) | +| experts on disk | one packed `experts.bin` (46.8 GB, one blob per expert, layer-major) mapped, file tier 41.7 GiB | the two GGUF shards mapped, gate/up/down of an expert in three different tensors | +| PLE rows | direct (unbuffered, overlapped) reads, 146k rows/s probe, on a worker thread | KAR level-2 PLE: three positional reads of random 4 KiB pages per row, on the prompt's critical path | + +Vanilla's decode is 107 ms per speculation round (300 tokens = 177 rounds, 123 of 227 drafts accepted); ours is 240 ms for the same 1.7 tokens per round. + +## 2. Where the time goes (ours, measured with the engine's own `PREFILL-PROFILE` lines) + +One 2048-token chunk at the old setting (job ab2, 18.9 s): `moe` 14.5 s of which `moe.ring` (the expert stream) 11.8 s, `input` 2.5-5.0 s (all of it `input.ple`), +`gdn` 1.1 s, `qsa` 0.5-0.6 s. At 6144 tokens (33 s, steady state): `input.ple` 10.3 s, `moe` 15.6 s (`ring` 14.3), `gdn` 3.8 s, `qsa` 2-2.9 s. + +* PLE: 16 rows per token, derived from the token and the two before it (n-gram hash), almost never in the 64 MiB device cache; each row is three random 4 KiB reads in the KAR archive. The reads are + latency-bound, 20-45 k per second, and happen before the chunk starts. That is 13-26 % of every 2048-token chunk and 31 % of a 6144-token one. +* MoE ring: every expert the GPU does not hold streams once per chunk, from the page cache or the NVMe. The cost per chunk is nearly constant (11.8 s at 2048 tokens, 14.3 s at 6144), so + a bigger chunk divides it. + +## 3. Gaps, ranked by expected gain on the 256k session (about 85 % of a turn is prefill) + +(filled in with measurements below)