Skip to content

[HiCache] Fix crash when using PP + HiCache L2 - #27285

Merged
hzh0425 merged 6 commits into
sgl-project:mainfrom
stepinto:sglang_pp_l2_only
Jun 6, 2026
Merged

hzh0425 merged 6 commits into
sgl-project:mainfrom
stepinto:sglang_pp_l2_only

Conversation

@stepinto

@stepinto stepinto commented Jun 4, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

HiCache does not work well with PP. It leads to server crash.

I have tried to solve the PP + HiCache L2+ L3 in a single patch #27010. But that patch became larger and larger when I try to deal with UnifiedRadixCache and HybridCacheController. Let me divide that patch into smaller ones.

This patch fixes PP + L2 only, leaving L3 unsolved.

Modifications

The reason to server crash is due to lack of synchronization beteeen scheduler threads of PP0 and PP1. The details are discussed in #22607.

This patch introduced a new synchrnoization mechanism called pp_sync for scheduler threads.

PP0's scheduler thread determine the number of completion events in its local queues (ack_load_queue, ack_prefetch_queue, etc). Then, PP1 must consume the exact same number of events from its queues. This prevents divergence.

Tests

Reproduce

A minimal reproduce step for "PP + L2 => crash" issue:

sglang serve \
  --model-path=Qwen/Qwen3-32B \
  --pp-size=2 \
  --enable-hierarchical-cache \
  --max-total-tokens=$((256*1024))

python -m sglang.bench_serving --num-prompts 1000

Got this error:
RuntimeError: shape '[3013, -1, 128]' is invalid for input of size 8192000                                                                                                                                                                                                                                                                                                                                          

This patch fixes that crash.

Effectiveness of L2 cache

I tested Qwen3-32B the following configuration:

  • pp-size=4
  • tp-size=2
  • #tokens in L1 = 256K
  • #tokens in L2 = 512K

Each client produces ~6K input tokens in 10 rounds. So 128 clients use 768K tokens in total, exceeding L2 a little bit.

python benchmark/hicache/bench_multiturn.py  \
    --num-clients=128 \
    --num-rounds=10 \
    --disable-auto-run  \
    --model=Qwen/Qwen3-32B  \
    --enable-round-barrier \
    --request-rate 16

Group A: No HiCache
sglang serve \
  --model=Qwen/Qwen3-32B \
  --tp-size=2 \
  --pp-size=4 \
  --max-total-tokens=$((256*1024)) \
  --reasoning-parser qwen3 \
  --tool-call-parser qwen25 \
  --page-size=64 \
  --enable-metrics

Group B: HiCache (L2 only)
sglang serve \
  --model=Qwen/Qwen3-32B \
  --tp-size=2 \
  --pp-size=4 \
  --max-total-tokens=$((256*1024)) \
  --reasoning-parser qwen3 \
  --tool-call-parser qwen25 \
  --page-size=64 \
  --enable-metrics \
  --enable-hierarchical-cache \
  --hicache-mem-layout=page_first

Result for group A:
  Round 0: Average TTFT = 1.69s, Cache Hit Rate = 0.000000 (128 requests, 128 clients)
  Round 1: Average TTFT = 0.82s, Cache Hit Rate = 0.470588 (128 requests, 128 clients)
  Round 2: Average TTFT = 2.14s, Cache Hit Rate = 0.653846 (128 requests, 128 clients)
  Round 3: Average TTFT = 2.42s, Cache Hit Rate = 0.615179 (128 requests, 128 clients)
  Round 4: Average TTFT = 5.60s, Cache Hit Rate = 0.343217 (128 requests, 128 clients)
  Round 5: Average TTFT = 9.10s, Cache Hit Rate = 0.237323 (128 requests, 128 clients)
  Round 6: Average TTFT = 12.86s, Cache Hit Rate = 0.146925 (128 requests, 128 clients)
  Round 7: Average TTFT = 17.84s, Cache Hit Rate = 0.081866 (128 requests, 128 clients)
  Round 8: Average TTFT = 19.32s, Cache Hit Rate = 0.117871 (128 requests, 128 clients)
  Round 9: Average TTFT = 23.94s, Cache Hit Rate = 0.091292 (128 requests, 128 clients)


Result for group B:
  Round 0: Average TTFT = 1.67s, Cache Hit Rate = 0.000000 (128 requests, 128 clients)
  Round 1: Average TTFT = 1.16s, Cache Hit Rate = 0.470588 (128 requests, 128 clients)
  Round 2: Average TTFT = 2.04s, Cache Hit Rate = 0.653846 (128 requests, 128 clients)
  Round 3: Average TTFT = 1.69s, Cache Hit Rate = 0.742857 (128 requests, 128 clients)
  Round 4: Average TTFT = 2.93s, Cache Hit Rate = 0.795455 (128 requests, 128 clients)
  Round 5: Average TTFT = 3.58s, Cache Hit Rate = 0.830189 (128 requests, 128 clients)
  Round 6: Average TTFT = 2.84s, Cache Hit Rate = 0.854839 (128 requests, 128 clients)
  Round 7: Average TTFT = 5.31s, Cache Hit Rate = 0.713358 (128 requests, 128 clients)
  Round 8: Average TTFT = 9.42s, Cache Hit Rate = 0.507812 (128 requests, 128 clients)
  Round 9: Average TTFT = 12.95s, Cache Hit Rate = 0.400281 (128 requests, 128 clients)

It shows in group B (HiCache L2), there are good cache hit ratio in round 4~7. However, the cache hit ratio in group A drops dramatically after round 4.

Checklist

Review and Merge Process

  1. Ping Merge Oncalls to start the process. See the PR Merge Process.
  2. Get approvals from CODEOWNERS and other reviewers.
  3. Trigger CI tests with comments or contact authorized users to do so.
    • Common commands include /tag-and-rerun-ci, /tag-run-ci-label, /rerun-failed-ci
  4. After green CI and required approvals, ask Merge Oncalls or people with Write permission to merge the PR.

CI States

Latest PR Test (Base): ✅ Run #27050101296
Latest PR Test (Extra): ✅ Run #27050101252

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@stepinto stepinto changed the title [HiCache] Fix crash when using PP + HiCache L1 [HiCache] Fix crash when using PP + HiCache L2 Jun 4, 2026
@whybeyoung

Copy link
Copy Markdown
Collaborator

/tag-and-rerun-ci

@github-actions github-actions Bot added the run-ci CI: run the baseline test suite on this PR label Jun 5, 2026
@hzh0425

hzh0425 commented Jun 5, 2026

Copy link
Copy Markdown
Collaborator

/rerun-group radix_cache/unified_radix_tree

@hzh0425

hzh0425 commented Jun 5, 2026

Copy link
Copy Markdown
Collaborator

/rerun-group hicache

@github-actions

github-actions Bot commented Jun 5, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-group radix_cache/unified_radix_tree:

🚀 4-gpu-h100 (3 tests): ✅ View workflow run

cd test/ && python3 registered/radix_cache/unified_radix_tree/test_unified_radix_cache_kl_cp.py
cd test/ && python3 registered/radix_cache/unified_radix_tree/test_unified_radix_cache_kl_dsv4.py
cd test/ && python3 registered/radix_cache/unified_radix_tree/test_unified_radix_cache_kl_mamba.py

🚀 2-gpu-h100 (2 tests): ✅ View workflow run

cd test/ && python3 registered/radix_cache/unified_radix_tree/test_unified_radix_cache_kl_full.py
cd test/ && python3 registered/radix_cache/unified_radix_tree/test_unified_radix_cache_kl_swa.py

🚀 8-gpu-h200 (1 test): ✅ View workflow run

cd test/ && python3 registered/radix_cache/unified_radix_tree/test_unified_radix_cache_kl_nightly.py

@github-actions

github-actions Bot commented Jun 5, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-group hicache:

🚀 1-gpu-h100 (2 tests): ✅ View workflow run

cd test/ && python3 registered/hicache/test_hicache_spec_file_storage.py
cd test/ && python3 registered/hicache/test_hicache_variants.py

🚀 2-gpu-h100 (5 tests): ✅ View workflow run

cd test/ && python3 registered/hicache/test_hicache_spec_mooncake_storage.py
cd test/ && python3 registered/hicache/test_hicache_storage_3fs_backend.py
cd test/ && python3 registered/hicache/test_hicache_storage_file_backend.py
cd test/ && python3 registered/hicache/test_hicache_storage_mooncake_backend.py
cd test/ && python3 registered/hicache/test_hicache_storage_runtime_attach_detach.py

🚀 1-gpu-5090 (1 test): ✅ View workflow run

cd test/ && python3 registered/hicache/test_hicache_storage.py

🚀 4-gpu-h100 (1 test): ❌ View workflow run

cd test/ && python3 registered/hicache/test_qwen35_hicache.py

@@ -382,7 +376,7 @@ def build_deepseek_v4_hicache_stack(
c4_state_host_pool = DeepSeekV4StateHostPool(
pool_name=str(PoolName.DEEPSEEK_V4_C4_STATE),
state_pools=[
kvcache.compress_state_pools[layer_id]

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.

dsv4 + pp should be fixed in a separate PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

OK. Reverted dsv4-related change.

@stepinto
stepinto force-pushed the sglang_pp_l2_only branch from ccf2741 to 4b83db5 Compare June 5, 2026 05:21

@ShangmingCai ShangmingCai 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.

LGTM

@ShangmingCai

Copy link
Copy Markdown
Collaborator

/rerun-test registered/disaggregation/test_disaggregation_pp.py test/registered/pp/test_pp_single_node.py

@github-actions

github-actions Bot commented Jun 5, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test registered/disaggregation/test_disaggregation_pp.py test/registered/pp/test_pp_single_node.py:

🚀 8-gpu-h20 (1 test): ✅ View workflow run

cd test/ && python3 registered/disaggregation/test_disaggregation_pp.py

🚀 4-gpu-h100 (1 test): ✅ View workflow run

cd test/ && python3 registered/pp/test_pp_single_node.py

@hzh0425 hzh0425 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.

Amazing

Thanks!

@github-actions github-actions Bot added the hicache Hierarchical Caching for SGLang label Jun 5, 2026
@hzh0425

hzh0425 commented Jun 5, 2026

Copy link
Copy Markdown
Collaborator

/rerun-test test/registered/radix_cache/unified_radix_tree/test_unified_radix_cache_hicache_pp_kl.py test/registered/unit/mem_cache/test_unified_radix_cache_unittest.py

@github-actions

github-actions Bot commented Jun 5, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/radix_cache/unified_radix_tree/test_unified_radix_cache_hicache_pp_kl.py test/registered/unit/mem_cache/test_unified_radix_cache_unittest.py:

🚀 4-gpu-h100 (1 test): ❌ View workflow run

cd test/ && python3 registered/radix_cache/unified_radix_tree/test_unified_radix_cache_hicache_pp_kl.py

🚀 1-gpu-5090 (1 test): ✅ View workflow run

cd test/ && python3 registered/unit/mem_cache/test_unified_radix_cache_unittest.py

@hzh0425 hzh0425 added run-ci-extra CI: also run the extra suite (requires run-ci) bypass-fastfail labels Jun 5, 2026
@hzh0425

hzh0425 commented Jun 6, 2026

Copy link
Copy Markdown
Collaborator

/rerun-test test/registered/radix_cache/unified_radix_tree/test_unified_radix_cache_hicache_pp_kl.py

@github-actions

github-actions Bot commented Jun 6, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/radix_cache/unified_radix_tree/test_unified_radix_cache_hicache_pp_kl.py:

🚀 4-gpu-h100 (1 test): ✅ View workflow run

cd test/ && python3 registered/radix_cache/unified_radix_tree/test_unified_radix_cache_hicache_pp_kl.py

@hzh0425

hzh0425 commented Jun 6, 2026

Copy link
Copy Markdown
Collaborator
image

@hzh0425
hzh0425 merged commit 42fe025 into sgl-project:main Jun 6, 2026
220 of 234 checks passed
@stepinto
stepinto deleted the sglang_pp_l2_only branch June 6, 2026 12:04
@hnyls2002

Copy link
Copy Markdown
Collaborator

@stepinto @whybeyoung @hzh0425 @ShangmingCai
I encountered a PP error in the CI, but succeeded after retrying:
https://github.com/sgl-project/sglang/actions/runs/27075032092/job/79915300478

hzwzwzw pushed a commit to hzwzwzw/sglang that referenced this pull request Jun 15, 2026
…ject#27285)

Co-authored-by: ybyang <[email protected]>
Co-authored-by: hzh0425 <[email protected]>
Co-authored-by: shangmingc <[email protected]>
Co-authored-by: 晟海 <[email protected]>
jeremyzhang866 added a commit to jeremyzhang866/sglang that referenced this pull request Jun 17, 2026
…he writing_check

Complement to sgl-project#27489 (which fixed UnifiedRadixCache only).
Related: sgl-project#26923, sgl-project#26921.

writing_check conditionally skipped the all_reduce when ongoing_write_through
was empty. ongoing_write_through can diverge across ranks (e.g. write_backup
returning 0 on a subset under host memory pressure), so the skip desyncs the
NCCL op sequence -> deadlock under TP > 1.

HiMambaRadixCache:
- make all_reduce unconditional in writing_check
- add all_reduce(MIN) sync to loading_check (previously had none)

HiRadixCache:
- make all_reduce unconditional in writing_check
  (loading_check already had all_reduce via sgl-project#27285)

Both now match UnifiedRadixCache (sgl-project#27489).

Co-Authored-By: Claude <[email protected]>
hzwzwzw added a commit to hzwzwzw/sglang that referenced this pull request Jul 2, 2026
…-project#601 and sgl-project#628

Merge target: d958a63 "Return bad request for malformed DSV4 tool
arguments (sgl-project#602)". Excludes b4de164 (sgl-project#601 "Fix DSV4 C128 state
mapping lifecycle") and f59f166 (sgl-project#628 "Sync clean SM90 FP8+FP4
MegaMoE from main") per user instruction.

Brings in 13 upstream commits including:
- 3e31718 [HiCache] DeepSeek-V4 host-mem L2 + NVMe/disk L3 (sgl-project#564)
- 6dddb6d Fix DSv4 draft extend replay forward batch (sgl-project#569)
- 9d212a9 add observability (sgl-project#570)
- 104a38d Bytedance/deepseek v4 fix draft extend padding (sgl-project#573)
- d958a63 Return bad request for malformed DSV4 tool arguments (sgl-project#602)
- 8 docker/codex build hardening commits

Conflict resolutions (all 8 files stability-critical, memory-related):

1. tree_component.py / full_component.py / swa_component.py / mamba_component.py
   -- commit_hicache_transfer signature: keep HEAD's typed
   (insert_result, pool_storage_result) kwargs. Callers in
   unified_radix_cache.py explicitly pass these; their **kw variant
   is strictly a superset but less safe.
   Also drop their side's manual `kw.get(...)` extraction in mamba
   PREFETCH branch since the params are now directly bound.

2. hybrid_cache_controller.py -- extra_host_mem_release_queues type
   annotation: keep HEAD's Queue[torch.Tensor] (more precise).

3. swa_memory_pool.py -- their side added a full duplicate
   SWATokenToKVPoolAllocator class inline in this file, but our fork
   moved the class to allocator/swa.py in an earlier refactor. Drop
   their duplicate; instead apply sgl-project#573's free_swa fix (guard against
   negative sentinels and out-of-range indices, expand page-aligned
   free) directly to allocator/swa.py where the class actually lives.

4. hybrid_pool_assembler.py:
   - kvcache.compress_state_pools indexing: keep either form (addition
     is commutative); use HEAD's ordering for local consistency.
   - C128 state pool registration: keep HEAD's explicit non-registration
     with the "C128 State pool intentionally not registered" comment.
     Our HEAD's upstream sgl-project#27293 deliberately opted out; their side (from
     before sgl-project#601) still registers it. Silently reintroducing the pool
     would regress the fix and pressure host memory.
   - attach_hybrid_pool_to_unified_cache body: keep HEAD's
     _select_strategy(...).build() dispatch (from upstream sgl-project#26295)
     over their 245-line inline if-elif chain. Our _DeepSeekV4Strategy
     already handles the deepseek_v4 stack; the strategy signature
     already accepts storage_backend / storage_extra_config /
     storage_prefetch_threshold.

5. unified_radix_cache.py (24 conflicts) -- keep HEAD wholesale:
   - HEAD has 10 methods theirs lacks (all from upstream sgl-project#26881,
     sgl-project#27285, sgl-project#27391, sgl-project#27489, sgl-project#27554, sgl-project#27655 L3 support + our PR sgl-project#27010
     port: prefetch_sync_thread, ack-driven sync, handle_prefetch_result,
     _drain_async_work, _finish_write_through_ack, etc.)
   - Their only extra method (terminate_prefetch(rid)) was deliberately
     removed by our PR sgl-project#27010 port.
   - HEAD's load_back_threshold=10 matches hi_mamba_radix_cache and
     hiradix_cache (theirs=256 was an inconsistent local tuning).
   - HEAD passes total_len to InsertResult (dataclass field exists) and
     priority to UnifiedTreeNode (LRU correctness).
   - HEAD's write_backup early-returns on written==0 (upstream sgl-project#27108
     safety fix); theirs drops the check.
   - HEAD's _drain_queue is blocking with qsize MIN-reduce (our PR
     sgl-project#27010's lockstep drain); theirs is old get_nowait+Empty pattern
     that would desync PP/TP.
   - sgl-project#564's L2/L3 additions in this file are functionally covered by
     upstream sgl-project#26881 (already in HEAD via ep_main). No net feature
     loss from taking HEAD.

Verified: AST parse OK for all 9 mem_cache files, compileall exit=0
on managers + mem_cache trees.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Chronostasys pushed a commit to MindLab-Research/sglang that referenced this pull request Aug 24, 2026
…ject#27285)

Co-authored-by: ybyang <[email protected]>
Co-authored-by: hzh0425 <[email protected]>
Co-authored-by: shangmingc <[email protected]>
Co-authored-by: 晟海 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bypass-fastfail hicache Hierarchical Caching for SGLang high priority run-ci CI: run the baseline test suite on this PR run-ci-extra CI: also run the extra suite (requires run-ci)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants