Repository navigation
expert_cache+prefill: SSD expert streaming protections (prefill cache protection, next-layer NVMe prefetch, cold-expert release) - #108
Conversation
…es (STRATA_PREFILL_PROTECT), overlap next-layer cold NVMe prefetch, and release cold prefill experts
🔍 Aletheore evidence diffgraph LR
n0["expert_cache.rs"]
n1["prefill.rs"]
n2["gpu_model.rs"]
n3["lib.rs"]
n4["main.rs"]
n0 --> n2
n0 --> n3
n0 --> n4
n1 --> n2
classDef changed fill:#d9772c,color:#fff,stroke:#b85f1c;
classDef changedFinding fill:#c0392b,color:#fff,stroke:#922b21;
classDef dependent fill:#f2f0ec,color:#18140f,stroke:#d6d0c5;
class n0 changed
class n1 changed
class n2 dependent
class n3 dependent
class n4 dependent
What changed
Aggregate deltas
|
| let n = self.n_expert; | ||
| if self.profile.is_some() { | ||
| let mode = std::env::var("STRATA_PREFILL_PROTECT").unwrap_or_default(); | ||
| if self.profile.is_some() && !mode.eq_ignore_ascii_case("strict") { |
There was a problem hiding this comment.
🟠 High · Correctness · 1 of 8
Strict mode skips the whole blend, so prompt routing never reaches counts, though the doc says strict only means zero swaps. With STRATA_PREFILL_PROTECT=strict and a profile present, the if short-circuits and counts never receives prompt routing. The placement after prefill ignores the prompt entirely, which contradicts the doc comment saying strict means 0 swaps.
Prompt for your AI agent
File: src/expert_cache.rs
Line: 236
Symbol: settle
Issue: Strict mode skips the whole blend, so prompt routing never reaches counts, though the doc says strict only means zero swaps. With STRATA_PREFILL_PROTECT=strict and a profile present, the `if` short-circuits and `counts` never receives prompt routing. The placement after prefill ignores the prompt entirely, which contradicts the doc comment saying strict means 0 swaps.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
| let mode = std::env::var("STRATA_PREFILL_PROTECT").unwrap_or_default(); | ||
| if self.profile.is_some() && !mode.eq_ignore_ascii_case("strict") { | ||
| let protect = mode != "0"; | ||
| let prompt_cap = if protect { 0.25 * self.steady } else { f32::INFINITY }; |
There was a problem hiding this comment.
🟠 High · Correctness · 2 of 8
prompt_cap is 0 when steady is 0 or unset, which silently zeroes all prompt contributions. If steady has not been set (0.0), prompt_cap is 0. Any non-empty prompt then gets pscale = 0/psum = 0, so no prompt routing is counted and placement ignores the prompt with no warning.
Prompt for your AI agent
File: src/expert_cache.rs
Line: 238
Symbol: settle
Issue: prompt_cap is 0 when steady is 0 or unset, which silently zeroes all prompt contributions. If steady has not been set (0.0), prompt_cap is 0. Any non-empty prompt then gets pscale = 0/psum = 0, so no prompt routing is counted and placement ignores the prompt with no warning.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
| self.swap(m, gpu, usize::MAX)?; | ||
| let max_swaps = if protect { | ||
| let min_pool = self.pools.iter().map(|p| p.owner.len()).filter(|&l| l > 0).min().unwrap_or(0); | ||
| (min_pool / 10).max(1) |
There was a problem hiding this comment.
🟠 High · Correctness · 3 of 8
max_swaps is a single global budget, not the per-pool 10% the doc promises. The budget is min_pool/10 applied to the whole swap call. If swaps concentrate in one pool, that pool can lose far more than 10% of its slots, contrary to the doc comment's per-pool guarantee against flushing the decode cache.
Prompt for your AI agent
File: src/expert_cache.rs
Line: 249
Symbol: settle
Issue: max_swaps is a single global budget, not the per-pool 10% the doc promises. The budget is min_pool/10 applied to the whole swap call. If swaps concentrate in one pool, that pool can lose far more than 10% of its slots, contrary to the doc comment's per-pool guarantee against flushing the decode cache.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
| let base = next_il * n; | ||
| let mut cands: Vec<usize> = (0..n).filter(|&e| lx.table[e] < 0).collect(); | ||
| if cands.is_empty() { return Vec::new(); } | ||
| let score = |e: usize| self.counts[base + e] + 0.01 * self.long[base + e]; |
There was a problem hiding this comment.
🟡 Medium · Correctness · 4 of 8
cold_ranges_for_next may not truncate cands to top_k if smallest only reorders. If smallest partitions in place without truncating, cands keeps every non-resident expert. The loop then emits ranges for all of them, up to 3 * n reads per layer, instead of the 16 that the call site asks for.
Prompt for your AI agent
File: src/expert_cache.rs
Line: 268
Symbol: cold_ranges_for_next
Issue: cold_ranges_for_next may not truncate cands to top_k if smallest only reorders. If `smallest` partitions in place without truncating, cands keeps every non-resident expert. The loop then emits ranges for all of them, up to 3 * n reads per layer, instead of the 16 that the call site asks for.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
| // Overlap NVMe read-ahead for the next MoE layer's hottest cold experts behind this layer's GEMMs. | ||
| if let Some(next_il) = (il + 1..c.n_layer).find(|&j| cache.layer(j).is_some()) { | ||
| rs.extend(cache.cold_ranges_for_next(self.m, next_il, 16)); | ||
| } |
There was a problem hiding this comment.
🔵 Low · Performance · 5 of 8
Next-layer NVMe prefetch of 16 experts runs unconditionally, even when the prefill will not need them. Every prefill layer reads the top 16 cold experts of the next layer. Many of those may never be selected for the prompt, so bandwidth is spent on reads that go unused and compete with the real streamed reads.
Prompt for your AI agent
File: src/gpu_model/prefill.rs
Line: 587
Symbol: prefill_chunk
Issue: Next-layer NVMe prefetch of 16 experts runs unconditionally, even when the prefill will not need them. Every prefill layer reads the top 16 cold experts of the next layer. Many of those may never be selected for the prompt, so bandwidth is spent on reads that go unused and compete with the real streamed reads.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
| let slice = &w.data[e * mat.bytes..(e + run.len()) * mat.bytes]; | ||
| g.htod(slice, &pf.staging, half(j) + STAGE * mat.off + i * mat.bytes)?; | ||
| // Release cold streamed prefill experts from OS page cache when the model exceeds RAM. | ||
| crate::weights::cold(slice); |
There was a problem hiding this comment.
🔵 Low · Performance · 6 of 8
cold() is called unconditionally, though the comment says it applies only when the model exceeds RAM. The comment says release happens when the model exceeds RAM, but the code calls cold(slice) on every streamed run. On machines where the model fits in RAM, the released pages must be re-read from SSD on each later chunk and layer, slowing prefill.
Prompt for your AI agent
File: src/gpu_model/prefill.rs
Line: 667
Symbol: prefill_chunk
Issue: cold() is called unconditionally, though the comment says it applies only when the model exceeds RAM. The comment says release happens when the model exceeds RAM, but the code calls cold(slice) on every streamed run. On machines where the model fits in RAM, the released pages must be re-read from SSD on each later chunk and layer, slowing prefill.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
| g.htod(slice, &pf.staging, half(j) + STAGE * mat.off + i * mat.bytes)?; | ||
| // Release cold streamed prefill experts from OS page cache when the model exceeds RAM. | ||
| crate::weights::cold(slice); | ||
| i += run.len(); |
There was a problem hiding this comment.
🟡 Medium · Correctness · 7 of 8
cold(slice) may be applied to non-page-aligned or heap-backed memory, risking zeroed weights or EINVAL. The slice starts at e * mat.bytes, which is not page-aligned. If cold() forwards this to madvise(MADV_DONTNEED) and the weights are heap-backed, the call can return EINVAL or, for anonymous memory, discard the data so later reads see zeros.
Prompt for your AI agent
File: src/gpu_model/prefill.rs
Line: 668
Symbol: prefill_chunk
Issue: cold(slice) may be applied to non-page-aligned or heap-backed memory, risking zeroed weights or EINVAL. The slice starts at e * mat.bytes, which is not page-aligned. If cold() forwards this to madvise(MADV_DONTNEED) and the weights are heap-backed, the call can return EINVAL or, for anonymous memory, discard the data so later reads see zeros.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
| #[test] | ||
| fn prefill_protect_blends_gently_and_preserves_hot_decode_counts() { | ||
| let mut c = fake(1, 4, 2, None); | ||
| // Simulate hot decode experts 0 and 1, then a huge prompt hitting cold experts 2 and 3. |
There was a problem hiding this comment.
🔵 Low · Maintainability · 8 of 8
New test re-implements the blend inline and never calls settle(), so it does not test production code. The test duplicates the pscale arithmetic. If settle() changes, for example by a wrong cap or a broken strict path, the test still passes, so it gives false confidence about the protection.
Prompt for your AI agent
File: src/expert_cache.rs
Line: 440
Symbol: prefill_protect_blends_gently_and_preserves_hot_decode_counts
Issue: New test re-implements the blend inline and never calls settle(), so it does not test production code. The test duplicates the pscale arithmetic. If settle() changes, for example by a wrong cap or a broken strict path, the test still passes, so it gives false confidence about the protection.
Make the smallest change that fixes this, add a test, and change nothing unrelated.
Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.
Aletheore Flash Lite reviewWhat this PR does. This PR changes how the expert cache settles after prefill: by default, a long prompt's routing counts are capped at 25% of steady-state weight and at most 10% of each pool's slots are swapped, so the hot decode cache is not flushed (with
8 finding(s) posted as inline review comment(s).
Review detailsReviewed 2 file(s); skipped 0. Dismissed 0; outside PR diff 0. Grounding: 8 of 8 proposed finding(s). Fix all with your agentBlast radius: 53 other file(s) depend on what this PR changes (3 directly, 50 indirectly)Computed from the import graph of this exact commit, not guessed by a model. It sees imports only, so dynamic imports, reflection and non-code references are not counted.
Indirectly affected: The graph is large; this list is capped and not exhaustive. |
|
Big ty @ybenyedder for this! The next layer prefetch and the page cache release are merged as is. I made STRATA_PREFILL_PROTECT opt-in (STRATA_PREFILL_PROTECT=1), bc on Qwen3.6-35B-A3B with an 8k prompt it dropped the reply's expert hit rate from 83% to 76%, the prompt was a better guess for the reply there. If you have a case where it helps, send numbers and we can look again |
Summary
Improves partial-residency / SSD-streamed MoE execution in
src/expert_cache.rsandsrc/gpu_model/prefill.rs:STRATA_PREFILL_PROTECT, default on):ExpertCache::settle()now caps prompt routing contribution to0.25 * steadyand caps per-pool turnover to 10% of slots (instead ofusize::MAX), preventing long prompts from flushing hot decode experts (STRATA_PREFILL_PROTECT=strictfor 0 swaps,=0for legacy behavior).ExpertCache::cold_ranges_for_next): while layerilcomputes on GPU during prefill, queues the top-16 hottest non-resident experts of layeril + 1onto the backgroundPrefetchthread.weights::cold()right after staging each cold prefill expert into GPU staging memory so models exceeding RAM stream without bloating page cache or triggering swap.Verification
cargo build --release --bin rustingstrata --locked(0 warnings in changed code)cargo test --release --lib --locked -- expert_cache::tests(5/5 passed, including newprefill_protect_blends_gently_and_preserves_hot_decode_counts)