Skip to content

expert_cache+prefill: SSD expert streaming protections (prefill cache protection, next-layer NVMe prefetch, cold-expert release) - #108

Open
ybenyedder wants to merge 1 commit into
RustingStudio:mainfrom
ybenyedder:feat/ssd-expert-streaming
Open

ybenyedder wants to merge 1 commit into
RustingStudio:mainfrom
ybenyedder:feat/ssd-expert-streaming

Conversation

@ybenyedder

Copy link
Copy Markdown
Contributor

Summary

Improves partial-residency / SSD-streamed MoE execution in src/expert_cache.rs and src/gpu_model/prefill.rs:

  1. Decode cache protection across long prompts (STRATA_PREFILL_PROTECT, default on): ExpertCache::settle() now caps prompt routing contribution to 0.25 * steady and caps per-pool turnover to 10% of slots (instead of usize::MAX), preventing long prompts from flushing hot decode experts (STRATA_PREFILL_PROTECT=strict for 0 swaps, =0 for legacy behavior).
  2. Inter-layer NVMe lookahead prefetch (ExpertCache::cold_ranges_for_next): while layer il computes on GPU during prefill, queues the top-16 hottest non-resident experts of layer il + 1 onto the background Prefetch thread.
  3. Cold prefill expert page-cache release: calls 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 new prefill_protect_blends_gently_and_preserves_hot_decode_counts)

…es (STRATA_PREFILL_PROTECT), overlap next-layer cold NVMe prefetch, and release cold prefill experts
@aletheore

aletheore Bot commented Nov 25, 2026

Copy link
Copy Markdown

🔍 Aletheore evidence diff

graph 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
Loading

What changed
Deterministic, computed from this commit's real scan and import graph - posted on every run, whether or not Flash Review found anything.

  • src/expert_cache.rs · modified · +54/-5 · +2 functions · 3 other dependents
  • src/gpu_model/prefill.rs · modified · +11/-3 · 1 other dependent

Aggregate deltas

  • Modules: -5
  • Dependency graph edges: -27
  • Commits: -27

Comment thread src/expert_cache.rs
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") {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 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.

Comment thread src/expert_cache.rs
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 };

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 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.

Comment thread src/expert_cache.rs
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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 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.

Comment thread src/expert_cache.rs
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];

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Comment thread src/gpu_model/prefill.rs
// 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));
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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.

Comment thread src/gpu_model/prefill.rs
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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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.

Comment thread src/gpu_model/prefill.rs
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();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Comment thread src/expert_cache.rs
#[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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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

aletheore Bot commented Nov 25, 2026

Copy link
Copy Markdown

Aletheore Flash Lite review

What 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 STRATA_PREFILL_PROTECT=0 restoring the old full flush, and strict forbidding swaps). It also adds NVMe read-ahead for the next MoE layer's hottest cold experts during prefill and releases streamed expert pages from the OS page cache after host-to-device copy, and it adds a unit test for the blending behavior.

Merge confidence Review effort Merge risk
2 / 5 2 / 5 Medium
Area Files Lines
src 1 +54 −5
src/gpu_model 1 +11 −3

8 finding(s) posted as inline review comment(s).

# Severity Category Where
1 🟠 High Correctness expert_cache.rs:236 Strict mode skips the whole blend, so prompt routing never reaches counts, though the doc says strict only…
2 🟠 High Correctness expert_cache.rs:238 prompt_cap is 0 when steady is 0 or unset, which silently zeroes all prompt contributions. If steady has not…
3 🟠 High Correctness expert_cache.rs:249 max_swaps is a single global budget, not the per-pool 10% the doc promises. The budget is min_pool/10 applied…
4 🟡 Medium Correctness expert_cache.rs:268 cold_ranges_for_next may not truncate cands to top_k if smallest only reorders. If smallest partitions in…
5 🔵 Low Performance prefill.rs:587 Next-layer NVMe prefetch of 16 experts runs unconditionally, even when the prefill will not need them. Every…
6 🔵 Low Performance prefill.rs:667 cold() is called unconditionally, though the comment says it applies only when the model exceeds RAM. The…
7 🟡 Medium Correctness prefill.rs:668 cold(slice) may be applied to non-page-aligned or heap-backed memory, risking zeroed weights or EINVAL. The…
8 🔵 Low Maintainability expert_cache.rs:440 New test re-implements the blend inline and never calls settle(), so it does not test production code. The…
Review details

Reviewed 2 file(s); skipped 0. Dismissed 0; outside PR diff 0. Grounding: 8 of 8 proposed finding(s).

Fix all with your 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.

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.

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.

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.

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.

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.

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.

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.
Blast 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.

  • src/expert_cache.rs is imported by src/gpu_model.rs, src/lib.rs, src/main.rs
  • src/gpu_model/prefill.rs is imported by src/gpu_model.rs

Indirectly affected: examples/gguf_dump.rs, src/backend.rs, src/chat.rs, src/config.rs, src/cpu_moe.rs, src/cpu_worker.rs (+44 more)

The graph is large; this list is capped and not exhaustive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant