Skip to content

[Fix] hicache: Evict pass-through Mamba host checkpoints before path tails - #47339

Open
koladefaj wants to merge 3 commits into
sgl-project:mainfrom
koladefaj:fix/hicache-mamba-host-eviction-order
Open

koladefaj wants to merge 3 commits into
sgl-project:mainfrom
koladefaj:fix/hicache-mamba-host-eviction-order

Conversation

@koladefaj

@koladefaj koladefaj commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

Fixes #45400.

On a hybrid GDN model with --enable-hierarchical-cache, host-tier hits drop to zero once the evicted working set is a few long prefixes larger than the device pool. The KV pages are still on host, but identical requests get cached_tokens: 0 and a full recompute.

A host prefix hit ends at the deepest node on the matched path that still has a Mamba state (device or host). Host Mamba eviction (drive_host_eviction) is plain LRU, and a long prefill backs up many chunk checkpoints along one path. With few host Mamba slots, newer intermediate checkpoints evict the tail states of earlier documents, so their host KV is left with no usable Mamba state. --mamba-max-states-per-path doesn't help, because it only trims device states after they've been backed up.

Modifications

  • unified_cache/components/mamba.py and rust/sglang-radix-tree/src/components/mamba.rs: host Mamba eviction now runs in two LRU passes. The first pass evicts only pass-through checkpoints (a node with one child that holds its own Mamba state on host). A device-only child doesn't count, because it can leave the device without a host copy (write_through_selective before its threshold, or a backup that finds no host slot). The second pass, which runs only if the first one didn't free enough, is the existing LRU order. Path tails, forks, and states whose child has no host state are kept until no pass-through checkpoint is left, so each path keeps the state its host hit ends at. This matches what _evict_excess_path_states already preserves on device.

  • The Python and Rust cores get the same change.

  • components/README.md: describes the new order under drive_host_eviction().

  • Tests, in both cores:

    • a pass-through checkpoint is evicted before an older tail
    • a fork is kept until the fallback pass
    • a state whose child has no state is kept
    • a state whose child is device-only is kept
    • eviction falls back to LRU order once no pass-through is left

    Python: test/registered/unit/mem_cache/test_mamba_host_eviction_order.py (CPU). Rust: rust/sglang-radix-tree/src/tests/components/mamba.rs.

  • Real-tree test: test_hicache_mamba_host_eviction_keeps_path_tails in test_unified_radix_cache_hicache.py (FULL_MAMBA, both tree cores). It builds an older single-state path and a multi-checkpoint path, makes every state host-only, evicts one host Mamba state, and asserts that both paths keep their full host hit. It fails on main on both cores and passes with this PR.

Accuracy Tests

Answers are unchanged: all 24 responses across the three runs below return the correct passphrase. This change only affects which host Mamba states are evicted, not model outputs.

Speed Tests and Profiling

1x NVIDIA RTX PRO 6000 Blackwell Server Edition, driver 595.91.07, image lmsysorg/sglang:nightly-dev-cu13-20261029-c10e0e17. The sglang tree was checked out at this PR's base (97483164) for "before" and at this commit for "after", with the Rust core rebuilt for each. Model nvidia/Qwen3.8-27B-NVFP4, launched with the flags from the issue minus PP and EAGLE:

sglang serve --model-path nvidia/Qwen3.8-27B-NVFP4 --trust-remote-code --port 30000 \
  --json-model-override-args '{"language_model_only": true}' \
  --context-length 262144 --kv-cache-dtype fp8_e4m3 --attention-backend flashinfer \
  --chunked-prefill-size 1024 --mem-fraction-static 0.95 --max-total-tokens 262144 \
  --max-running-requests 4 --cuda-graph-bs-decode 1 2 3 4 --cuda-graph-backend-prefill disabled \
  --mamba-radix-cache-strategy extra_buffer --mamba-ssm-dtype bfloat16 --max-mamba-cache-size 16 \
  --enable-hierarchical-cache --hicache-size 15 --mamba-max-states-per-path 5 \
  --enable-cache-report --enable-metrics

Host pools: 396,247 KV tokens and 26 Mamba state slots. Client: repro.py 4 from the issue (4 × ~89k-token documents, then an identical re-ask of each), with a fresh server per run.

Rebased re-run (2026-11-10)

Same GPU and flags. Image lmsysorg/sglang:nightly-dev-cu13-20261110-4a5c6b8f, trees set to exactly main e0187268f6 and this PR. I deleted the image's prebuilt tree-core .so and set SGLANG_RUST_BUILD_MODE=auto, so each tree ran its own Rust core.

main e0187268f6 this PR
cached_tokens per re-ask 0 89,088–89,280
re-ask latency 16.72–17.06 s 0.22–0.45 s
prefill_effective_tokens_total{mode="host_hit"} 0 356,672
hicache_backup_tokens_total{pool="mamba"} 702 351
hicache_host_used_tokens 355,447 356,663
cargo test (sglang-radix-tree) 965 passed 970 passed
test_unified_radix_cache_hicache.py + _load_back.py (Python core) 361 passed 362 passed
test_unified_radix_cache_rust.py (Rust core) 1319 OK 1320 OK

All 16 answers are correct.

Original run (base 97483164)

before (main, Rust core) after (Rust core) after (Python core)
cached_tokens per re-ask 0 89,088–89,280 89,088–89,280
re-ask latency 16.68–16.70 s 0.21–0.41 s 0.21–0.26 s
prefill_effective_tokens_total{mode="host_hit"} 0 356,672 356,672
hicache_backup_tokens_total{pool="mamba"} 702 351 351
hicache_host_used_tokens 355,447 356,663 356,663

Before (main 97483164, Rust core)

doc0 first 89294 21.28 0 'pass-0'
doc1 first 89200 16.92 0 'pass-1'
doc2 first 89117 17.13 0 'pass-2'
doc3 first 89195 16.97 0 'pass-3'
doc0 again (identical) 89294 16.7 0 'pass-0'
doc1 again (identical) 89200 16.69 0 'pass-1'
doc2 again (identical) 89117 16.7 0 'pass-2'
doc3 again (identical) 89195 16.68 0 'pass-3'

After (this PR, Rust core)

doc0 first 89294 17.17 0 'pass-0'
doc1 first 89200 16.89 0 'pass-1'
doc2 first 89117 16.9 0 'pass-2'
doc3 first 89195 16.94 0 'pass-3'
doc0 again (identical) 89294 0.41 89280 'pass-0'
doc1 again (identical) 89200 0.28 89152 'pass-1'
doc2 again (identical) 89117 0.21 89088 'pass-2'
doc3 again (identical) 89195 0.21 89152 'pass-3'

After (this PR, Python core, SGLANG_UNIFIED_RADIX_TREE_CORE_BACKEND=python)

doc0 again (identical) 89294 0.26 89280 'pass-0'
doc1 again (identical) 89200 0.26 89152 'pass-1'
doc2 again (identical) 89117 0.21 89088 'pass-2'
doc3 again (identical) 89195 0.21 89152 'pass-3'

Columns: label, prompt tokens, request time (s), cached_tokens, answer. Cold requests take about 17 s in every run.

Follow-up

Once no pass-through checkpoint is left, eviction falls back to plain LRU, so tails can still be evicted under heavy pressure, for example branch-heavy sub-agent workloads. A minimum-token-distance rule for keeping pass-through checkpoints is being discussed in the issue. This PR keeps to the reproduced case.

Checklist


CI States

Latest PR Test (Base): ✅ Run #42798150683
Latest PR Test (Extra): ✅ Run #42798150491
Latest PR Test (AMD ROCm 10): ❌ Run #42798150512

Tip

Base CI only covers the core cases, and its workload may not exercise your change. Use /rerun-test to run the tests your change touches, or copy this prompt for an agent:

Find the registered tests this PR affects and run them with /rerun-test, following https://github.com/sgl-project/sglang/blob/main/.agents/skills/rerun-test/SKILL.md

@koladefaj
koladefaj requested a review from Jialin as a code owner October 29, 2026 22:40

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@xiezhq-hermann xiezhq-hermann 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.

Thanks @koladefaj. This is a focused fix with a clear root cause and convincing before/after numbers. I checked it against current main (34f49c9):

  • It merges cleanly. On the Rust side, the four new host_eviction_* tests pass, and cargo test for sglang-radix-tree passes (969 tests).
  • The new Python test doesn't collect on current main. It imports UnifiedLRUList and UnifiedTreeNode from sglang.srt.mem_cache.unified_radix_cache, and #47990 (11-02) removed that re-export. With the import pointed at sglang.srt.mem_cache.unified_cache.unified_tree_core, all 4 tests pass with your change and all 4 fail on main, so they do pin the new order.
  • The existing Mamba and HiCache unit files I could run give identical results with and without the patch. The real-tree HiCache suites (test_unified_radix_cache_hicache.py, test_unified_radix_cache_load_back.py) did not run cleanly in my environment on either tree, so CI is the check there.

Requests:

  1. Rebase and fix the import. Please rebase onto current main and fix that import. mamba.py also changed after your base (among others #44718 host-only checkpoints and #47680), though without textual conflicts. A re-run of your repro.py 4 comparison on the rebased branch would be good to have in the description.
  2. Add one real-tree test. The Python test drives MambaComponent against a _FakeTreeCore that stubs _evict_component_and_detach_lru and _cascade_evict. So it checks the order, but not the symptom. Could you add one case on a real UnifiedRadixCache with Full + Mamba and a host pool? The FULL_MAMBA classes in test/registered/unit/mem_cache/unified_radix_cache/test_unified_radix_cache_hicache.py run on both tree cores. For example: build a path with several host-only checkpoints plus an older tail, call evict_host, and assert that match_prefix still returns the host hit up to the tail.
  3. A question on _is_pass_through_checkpoint. It also counts a child whose Mamba state is on device only. That is what lets the sweep make room while the next checkpoint is being backed up. But the child might later be evicted from the device without a host copy, for example under write_through_selective before its second hit, or when its backup can't get a host slot. Then the path loses both states, and the host hit falls back to an earlier ancestor. Is that reachable in practice? If you keep the device case on purpose, a short comment explaining why would help.

On #45400: #48431 (opt-in, request-end-only Mamba backups) attacks the same symptom at write time, and the two PRs merge cleanly together. We'd like this PR to be the primary fix for #45400, and to evaluate #48431 on top of it. @MarlzRana, if your 2048-chunk / 400 s / 16-agent workload can run against this branch, that would be a useful second data point.

After the rebase we can run CI.

@koladefaj
koladefaj force-pushed the fix/hicache-mamba-host-eviction-order branch from f3796ab to 864dda0 Compare November 10, 2026 05:57
@github-actions github-actions Bot added the hicache Hierarchical Caching for SGLang label Nov 10, 2026
@koladefaj

Copy link
Copy Markdown
Contributor Author

Thanks @xiezhq-hermann, all three are addressed. The branch is rebased onto current main (e0187268f6).

  1. Rebase and import. The test now imports UnifiedLRUList / UnifiedTreeNode from sglang.srt.mem_cache.unified_cache.unified_tree_core and ComponentType from unified_cache.component_type. I re-ran repro.py 4 on the rebased branch and on current main. The results are below and in the description.

  2. Real-tree test. I added test_hicache_mamba_host_eviction_keeps_path_tails to test_unified_radix_cache_hicache.py (FULL_MAMBA ps1, so it runs on both tree cores). It builds an older single-state path and a 3-checkpoint path, backs both up, and drops them from the device so every state is host-only with the older tail least recent. It then calls evict_host(1, MAMBA) and asserts that match_prefix still returns the full host hit on both paths and that one middle checkpoint went. On main's eviction code it fails on both cores (host_hit_length 0 != 2: the older path loses its hit). With this PR it passes on both.

  3. Device-only child. Good catch. A device-only child doesn't guarantee anything on the host tier: it can leave the device with no host copy (write_through_selective before its threshold, or a backup that finds no host slot), and then the path has lost both states. _is_pass_through_checkpoint / is_pass_through_checkpoint_ now require the child to hold a host state, with a comment on why. This doesn't weaken the fix for [Bug] HiCache on hybrid GDN model: host-tier hits drop to zero once evicted working set exceeds ~3 long prefixes (host mamba pool exhausted while KV stays on host) #45400. During a long prefill, the checkpoint two back already has a host-backed child, so the sweep can still free middle checkpoints. It only gives up the newest pending one. I added a test for this in each core (test_state_whose_child_is_device_only_is_kept / host_eviction_keeps_a_state_whose_child_is_device_only).

Verification, on 1x RTX PRO 6000 Blackwell Server Edition, image lmsysorg/sglang:nightly-dev-cu13-20261110-4a5c6b8f, with the trees set to exactly current main and this branch. I deleted the image's prebuilt tree-core .so and set SGLANG_RUST_BUILD_MODE=auto, so each tree ran its own Rust core:

main e0187268f6 this PR
cargo test (sglang-radix-tree) 965 passed 970 passed
test_unified_radix_cache_hicache.py + _load_back.py (Python core) 361 passed, 65 skipped 362 passed, 65 skipped
test_unified_radix_cache_rust.py (all suites, Rust core) 1319 OK, 187 skipped 1320 OK, 187 skipped
test_mamba_host_eviction_order.py n/a 5 passed

The real-tree suites run cleanly there. On my laptop they failed on both trees (host-memory preflight, then a CUDA illegal address on an 8 GB 4060), including Full-only configs this PR doesn't touch, so I think that was the environment.

repro.py 4 with the description's launch flags (fresh server per run; host pools 396,247 KV tokens and 26 Mamba slots; Rust core):

main e0187268f6 this PR
cached_tokens per re-ask 0 89,088–89,280
re-ask latency 16.72–17.06 s 0.22–0.45 s
prefill_effective_tokens_total{mode="host_hit"} 0 356,672
hicache_backup_tokens_total{pool="mamba"} 702 351
hicache_host_used_tokens 355,447 356,663

All 16 answers are correct. Cold requests take 17.0–18.2 s on both trees.

# this PR
doc0 again (identical) 89294 0.45 89280 'pass-0'
doc1 again (identical) 89200 0.3 89152 'pass-1'
doc2 again (identical) 89117 0.22 89088 'pass-2'
doc3 again (identical) 89195 0.22 89152 'pass-3'
# main
doc0 again (identical) 89294 17.06 0 'pass-0'
doc1 again (identical) 89200 16.72 0 'pass-1'
doc2 again (identical) 89117 16.82 0 'pass-2'
doc3 again (identical) 89195 16.94 0 'pass-3'

@MarlzRana, a run of your 2048-chunk / 16-agent workload against this branch would still be welcome.

@alphabetc1 alphabetc1 added run-ci CI: run the baseline test suite on this PR run-ci-nv-extra CI: NVIDIA/shared Extra suite (requires run-ci) labels Nov 10, 2026
@xiezhq-hermann

Copy link
Copy Markdown
Collaborator

Thanks @koladefaj, all three points are addressed: the real-tree FULL_MAMBA test now fails on main on both tree cores (0 != 2) and passes here, the pass-through check only counts host-backed children, and NVIDIA Base/Extra are green on this head. @alphabetc1, could you take it from here? You're also on #48431, which we planned to evaluate on top of this.

@MarlzRana

MarlzRana commented Nov 17, 2026 •

Copy link
Copy Markdown

Here they are @xiezhq-hermann and @koladefaj:

Tested this on a different setup from the PR's, with a before/after/control comparison. It fixes the zero-hit symptom from #45400 there tool.

Setup

  • 1x NVIDIA A100 80GB (SXM4), Qwen/Qwen3.8-27B-FP8, SGLang 0.5.21 release image (not main)
  • Flags from the issue, minus PP and EAGLE, and without language_model_only: --chunked-prefill-size 1024 --max-total-tokens 262144 --context-length 262144 --max-running-requests 4 --mamba-radix-cache-strategy extra_buffer --mamba-ssm-dtype bfloat16 --max-mamba-cache-size 16 --enable-hierarchical-cache --hicache-size 15 --kv-cache-dtype fp8_e4m3 --attention-backend flashinfer --mem-fraction-static 0.88 --enable-cache-report --enable-metrics
  • --mamba-max-states-per-path left at its default (-1). Host pools: 396,247 KV tokens and 2.04 GB for the host Mamba cache.
  • Client: a Bash port of repro.py 4 (4 documents of about 89K tokens of random words with a passphrase in the middle, then the same 4 again). Fresh server for each run.

Results (single runs; cells are the four re-asks, in order)

stock, Rust core stock, Python core this PR, Python core
re-ask cached_tokens 0, 0, 0, 0 0, 0, 0, 0 89344, 89152, 89216, 89152
re-ask latency 44.4-44.5 s 45.2-45.3 s 0.42-0.90 s
prefill_effective_tokens_total{mode="host_hit"} 0 0 356,864
hicache_backup_tokens_total{pool="mamba"} 397 397 45
hicache_host_used_tokens 356,407 356,407 356,918
correct passphrase answers 4/4 4/4 4/4

The stock Rust and stock Python runs behave the same, so the Python core is not what changes the result. The unpatched backup count (397) is close to the issue's 394 for -1.

With --mamba-max-states-per-path 5 (the value in the issue's command), this build already got host hits on the stock code: 25 Mamba backup tokens in total, 89K cached on every re-ask. The failure on our build only appeared with the default -1.

Not covered

  • Only the Python tree core was run with the patch. The PR's Rust hunk did not apply to the release source, so the Rust core fix is untested here.
  • Single runs, one configuration (chunk 1024, page size 1, 4 documents). I did not run the PR's unit tests.
  • Correctness was checked only through the passphrase answers above (4/4 on the host-tier hits).

This branch has not been deployed

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

Labels

documentation Improvements or additions to documentation hicache Hierarchical Caching for SGLang run-ci CI: run the baseline test suite on this PR run-ci-nv-extra CI: NVIDIA/shared Extra suite (requires run-ci) unified-radix-cache

Projects

None yet

4 participants