Skip to content

Limit pair-enumeration candidates in snapshot selection (fixes #319 livelock) - #326

Closed
ouyyy18 wants to merge 3 commits into
NVIDIA:mainfrom
ouyyy18:fix/snapshot-pair-candidate-cap
Closed

ouyyy18 wants to merge 3 commits into
NVIDIA:mainfrom
ouyyy18:fix/snapshot-pair-candidate-cap

Conversation

@ouyyy18

@ouyyy18 ouyyy18 commented Nov 18, 2026

Copy link
Copy Markdown

Fixes the scheduler livelock analyzed in #319.

Root cause (confirmed with symbolized gdb stacks + an offline reproduction): select_snapshots enumerates candidate pairs through an inverted page index. Under heavily shared prefixes every posting list carries almost every candidate, so the pair loop approaches O(n² · pages): on a package with ~2000 free snapshot slots a single quote measured 9.4 s in release mode (offline unit test on the exact function, select_snapshots_shared_prefix_blowup in this PR). simulate_snapshot_quote re-runs the selection after every eviction, so planning never returns — the worker thread spins inside quote_snapshots_preserving, ranks idle, GPUs idle, /health keeps answering 200 and nothing is logged.

Fix: truncate the candidate list entering the pair phase to a constant 128 ids. Candidates arrive in id order and the tie-break already prefers lower ids, so the documented preference is kept; quoting stays a heuristic and apply_snapshot_quote re-verifies the quote deterministically. The reproduced input now quotes in ~40 ms (222×).

Validation:

select_snapshots builds an inverted index and enumerates candidate
pairs to find the cheapest two-row quote. With heavily shared
prefixes (agent-style traffic that reuses one long context across
many requests) every posting list carries almost every candidate,
so the pair loop approaches O(n^2 * pages): a single quote on a
package with ~2000 free snapshots measured 9.4 s in release mode,
and the quoting loop re-runs it after every eviction, wedging the
scheduler (health stays 200, GPUs idle, no log output). See NVIDIA#319
for full evidence including symbolized stacks of the livelock.

Truncate the candidate list entering the pair phase to a constant
128 ids. Candidates arrive in id order and ties already prefer
lower ids, so the cap keeps the documented preference while
bounding the worst case; quoting stays a heuristic and
apply_snapshot_quote re-verifies deterministically. On the
reproduced input the quote now takes ~40 ms, and 15 minutes of
the wedge-triggering load (12 workers, /v1/responses, max
reasoning effort, ~350 KB shared-prefix contexts, mid-stream
cancels) run with no stall while decode throughput is unchanged
(C16 -1.2%, C32 -0.7% vs the same load before the window).

@original-el8 original-el8 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The scheduler stall in #319 needs a bounded-cost fix, but this truncation changes snapshot selection and can reject a feasible allocation. I reproduced the regression using the exact posting_starts/select_snapshots functions from this PR and from main 5fd4070, compiled with rustc. With 130 candidates, ids 0..127 costing two pages and ids 128/129 costing zero, two available pages and keep_pages=2, main selects [128] for count=1 and [128,129] for count=2; this head returns None for both. This is a functional regression, separate from the reported performance improvement. The complete runtime workload was not rerun during this review.

Comment thread helios/src/scheduler/cache_memory.rs Outdated
const MAX_PAIR_CANDIDATES: usize = 128;
let truncated = candidates.len() > MAX_PAIR_CANDIDATES;
let candidates = if truncated {
candidates.into_iter().take(MAX_PAIR_CANDIDATES).collect::<Vec<_>>()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[P1] Preserve feasible snapshots beyond the first 128 IDs

Taking the first 128 IDs drops candidates before evaluating their costs and even before the count==1 fast path. IDs are only a tie-break after cost; lower IDs are not generally better choices. Reproduction: backing=[true,true], protected_refs=[0,0], ranges[0..128]=0..2, ranges[128]=ranges[129]=0..0, candidates=0..130 and keep_pages=2. Main returns Some([128]) for one row and Some([128,129]) for two rows; this PR returns None for both although those zero-cost aliases fit. Callers can consequently evict useful checkpoints or reject/delay admissible work. Bound the expensive pair search without imposing this ID-prefix allocation restriction, preserve the unaffected single-row path, and add coverage where the only feasible or cheapest candidates lie past index 127. Deterministically rechecking a returned quote cannot recover candidates that were discarded.

@original-el8 original-el8 added area:scheduling Request scheduling, fairness, batching, and speculative-width selection. bug Something isn't working priority:P1 High attention: unresolved correctness, serving failure, or model-data deletion risk. triage:review Proposed change needs code review; this does not imply merge readiness. labels Nov 18, 2026
Review feedback: taking the first 128 ids drops candidates before
considering cost, so the only feasible (zero-cost) snapshots can be
discarded and the selector rejects an allocation main would make.

- Leave the single-row path on the full candidate list (it is already
  linear; the blowup is only the pair phase).
- For pairs, keep the cheapest MAX_PAIR_CANDIDATES by (cost, id)
  instead of the first ids; zero-cost candidates therefore always
  survive the bound. Inputs with at most that many candidates run the
  exact original enumeration unchanged.
- Compare ties explicitly on the (union, id pair) key: the candidate
  list may now be cost-sorted, so scan order no longer encodes the
  lexicographic id preference.
- Add the review's minimal case (feasible zero-cost candidates at ids
  128/129 under 130 candidates) as a regression test, alongside the
  shared-prefix blowup test with a bounded-time assertion.

Verified: scheduler suite 310/310; shared-prefix quote still ~40 ms
(was 9.4 s unbounded); review case selects [128] and [128,129].
@ouyyy18

ouyyy18 commented Nov 18, 2026

Copy link
Copy Markdown
Author

Thanks for the precise review and the minimal reproduction — agreed, the id-prefix truncation was wrong. Pushed a revision to this branch:

  • The single-row path now runs on the full candidate list (it was never the expensive part).
  • The pair phase keeps the cheapest MAX_PAIR_CANDIDATES by (cost, id) instead of the first ids, so zero-cost candidates always survive the bound; inputs with at most that many candidates run the exact original enumeration unchanged.
  • Ties are now compared explicitly on (union, id pair) since the list may be cost-sorted.
  • Your reproduction (zero-cost ids 128/129 under 130 candidates) is added as a regression test together with the shared-prefix blowup test (bounded-time assertion); both pass, scheduler suite 310/310, shared-prefix quote still ~40 ms.

We will also run the #296/#329 branches through the shared-prefix/cancellation workload from #319 and report back.

Live re-testing of the previous revision exposed a second unbounded
path: with the bound placed in the two-row branch only, requests whose
quote rows exceed two (speculative-decode workloads quote more rows)
still reached the count>2 greedy witness with the full candidate list,
and the engine wedged again under the same shared-prefix load
(symbolized stack: select_snapshots -> simulate_snapshot_quote, hot in
BTreeSet probes per page; GH NVIDIA#319 evidence recorded).

Move the bound before every multi-row path (both the conservative
witness and the two-row enumeration); the single-row path keeps the
full candidate list as requested in review. Added a count=7
shared-prefix regression test alongside the pair-path one: both now
quote in ~40 ms where the unbounded originals take seconds, and the
review's zero-cost-at-id-128/129 case still selects correctly.

Validation on 4x RTX PRO 6000 (TP4, draft 7, CSF package): scheduler
suite 311/311; 15 min of the NVIDIA#319 wedge load with zero stalls on this
revision (the previous revision wedged at ~165 s); decode bench C1
285.5 / C8 1035.8 / C16 1468.7 / C32 1881.2 tok/s vs 290/1076/1465/
1896 on the working 972e28e baseline (within run-to-run noise).
@ouyyy18

ouyyy18 commented Nov 18, 2026

Copy link
Copy Markdown
Author

Follow-up with live results, including a correction to the previous revision:

The cost-sorted bound placed only in the two-row branch was insufficient. Live re-testing wedged the engine again at ~165 s under the #319 load — requests that quote more than two rows still reached the count > 2 conservative witness with the full candidate list (symbolized stack again in select_snapshots → simulate_snapshot_quote, hot in per-page BTreeSet probes; evidence captured). The new head moves the bound before every multi-row path and keeps the single-row path on the full list per review.

Validation matrix (4× RTX PRO 6000, TP4, draft 7, CSF package, 15 min of the #319 workload each):

build result
main 6b68b9b4 wedge in 8 s–8 min (5 runs)
prev revision (bound in pair path only) wedge at ~165 s
new head (bound before all multi-row paths) no stall in 15 min, 232 req / 53 completed / 0 errors
972e28e4 (working baseline) no stall (18 h production)

New head decode bench: C1 285.5 / C8 1035.8 / C16 1468.7 / C32 1881.2 tok/s vs 290/1076/1465/1896 on the working baseline (run-to-run noise). Offline: pair-path and count=7 shared-prefix quotes both ~40 ms (was 9.4 s / multi-second); scheduler suite 311/311; your zero-cost-at-128/129 case passes.

On #296/#329: per the earlier suggestion we ran #296 (2fa59c5, no bound of ours) through the same 15-minute load — it survived without stalls (229 req / 65 completed / 0 errors), consistent with the repetition being a major amplifier. A single unbounded quote can still cost ~9 s on that branch, so we see the two approaches as complementary: reduce repetition (#296/#329) and bound per-quote enumeration (this PR). We have not yet run #329 through the workload; happy to report back when we do.

@yatesdr

yatesdr commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

Your profiling found that the cost is in pair enumeration inside each selection, and #346 removes exactly that. We merged #346 instead because it keeps the selection exact. A 128-candidate cap misses some feasible cases: with 128 disjoint 4-page aliases plus one 8-page alias spanning two of them, budget 8 and count 3, the cap returns no selection, and the quote then evicts checkpoints it didn't need to. #346 brings the worst nested-alias selection from ~10 s to under 1 ms, with results identical to main across 18M randomized inputs. Closing in favour of #346. Thanks again for the diagnosis.

@yatesdr yatesdr closed this Nov 18, 2026
@ouyyy18

ouyyy18 commented Nov 18, 2026

Copy link
Copy Markdown
Author

Follow-up as promised: #329 (c6060f3, Reuse checkpoint selections through scheduler commit, no bound of ours) has now been run through the same 15-minute #319 workload.

Result: no stalls (905 s, canary green throughout), decode bench C1 281.4 / C8 1049.6 / C16 1465.0 / C32 1915.7 tok/s, prefill 32k 13,200 tok/s — at or slightly above the working baseline.

Complete validation matrix on this host (4× RTX PRO 6000, TP4, draft 7, CSF package, 15 min of the #319 load each):

build 15-min #319 workload
main 6b68b9b4 wedge in 8 s–8 min (5 runs)
this PR, pair-path-only bound (superseded) wedge at ~165 s
this PR head (all multi-row paths bounded) no stall
#296 2fa59c5 no stall
#329 c6060f3 no stall
972e28e4 baseline no stall (18 h production)

So both #296 and #329 independently avoid the livelock by eliminating the repetition amplifier, while a single unbounded quote can still cost ~9 s on those branches; the bound in this PR caps the per-quote cost itself. The approaches look complementary — happy to rebase or rework the bound however maintainers prefer if #296/#329 land first.

(For the record: our production instance has been serving on this PR's build since validation — 9+ hours overnight, zero incidents.)

@ouyyy18

ouyyy18 commented Nov 19, 2026

Copy link
Copy Markdown
Author

Thank you — closing in favour of #346 makes complete sense. Prefix sums over contiguous alias ranges keeping the selection exact (and at 0.84 ms on our reported shape) is strictly better than our heuristic cap, and the additional feasibility counter-example is a fair kill for the 128-candidate bound.

Glad the diagnosis was useful. For the record, we are migrating our production instance to a main build containing #346 (currently serving on our interim bounded build, which has been stable through the same workload), and will report if anything resurfaces under the shared-prefix agent traffic.

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

Labels

area:scheduling Request scheduling, fairness, batching, and speculative-width selection. bug Something isn't working priority:P1 High attention: unresolved correctness, serving failure, or model-data deletion risk. triage:review Proposed change needs code review; this does not imply merge readiness.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants