Skip to content

[HiCache] Detect for double-free in HostKVCache - #29310

Merged
hzh0425 merged 3 commits into
sgl-project:mainfrom
stepinto:host_mem_pool_double_free_0625
Jun 29, 2026
Merged

hzh0425 merged 3 commits into
sgl-project:mainfrom
stepinto:host_mem_pool_double_free_0625

Conversation

@stepinto

@stepinto stepinto commented Jun 25, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

The uses of host_indices in HiRadixCache and HiCacheController are tricky, because of the flexible life-time. This patch adds detection for double-free in HostKVCache. When double-free happens, an assertion failure will occur.

Modifications

Modified HostKVCache, using a booelan tensor to track allocated indices. Added assertion in alloc and free.

Also added a unit test case.

Accuracy Tests

N/A.

Speed Tests and Profiling

I tested GLM5.2 PP=4 TP=4. The checking code seems not affect performance.

ACE5635E-0D4A-4ADC-B7C5-12A4353103A2

Server startup command:
(num-total-tokens and hicache-ratio are set so that L1 + L2 are large enough to hold all tokens).

python3 -m sglang.launch_server \
--model-path zai-org/GLM-5.2-FP8 \
--host 0.0.0.0 --port 30000 \
--tp 4 --pp-size 4 --dist-init-addr <node0>:20102 --nnodes 2 --node-rank 0 \
--context-length 202752 --mem-fraction-static 0.8 --kv-cache-dtype fp8_e4m3 \
--page-size 64 --chunked-prefill-size 8192 \
--enable-hierarchical-cache --hicache-ratio 2 \
--enable-metrics \
--max-total-tokens=1000000 \
--hicache-ratio=4 \
--trust-remote-code

Testing script:

set -e
for i in $(seq 1 5); do
    python benchmark/hicache/bench_multiturn.py  \
        --num-clients=512 \
        --num-rounds=10 \
        --disable-auto-run  \
        --model=deepseek-ai/DeepSeek-V4-Flash  \
        --request-rate 16
done

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 #28291858821
Latest PR Test (Extra): ✅ Run #28291858745

@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 force-pushed the host_mem_pool_double_free_0625 branch from d06954c to 6fc7330 Compare June 25, 2026 14:42
@hzh0425 hzh0425 self-assigned this Jun 26, 2026

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

Please take the time to add some benchmark comparison data and long-term stability tests to verify that introducing this won't cause any unusual crashes.

@hzh0425

hzh0425 commented Jun 26, 2026

Copy link
Copy Markdown
Collaborator

/rerun-group mem_cache/unified_radix_cache

@hzh0425

hzh0425 commented Jun 26, 2026

Copy link
Copy Markdown
Collaborator

/rerun-group /radix_cache/unified_radix_tree

@sgl-project sgl-project deleted a comment from github-actions Bot Jun 26, 2026
@github-actions

github-actions Bot commented Jun 26, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-group /radix_cache/unified_radix_tree:

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

cd test/ && python3 registered/radix_cache/unified_radix_tree/test_unified_radix_cache_hicache_pp_kl.py
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

@hzh0425 hzh0425 added run-ci CI: run the baseline test suite on this PR run-ci-extra CI: also run the extra suite (requires run-ci) labels Jun 27, 2026
@hzh0425
hzh0425 merged commit bd2a5db into sgl-project:main Jun 29, 2026
177 of 185 checks passed
hzwzwzw added a commit to hzwzwzw/sglang that referenced this pull request Jul 13, 2026
…gg abort + double-free + DSv4 PP SWA + HiSparse tail

Seven upstream-merged bug/stability fixes for HiCache and DSv4 PP.
All adapted where our fork's structure differs (HostKVCache lives
inline in memory_pool_host.py rather than pool_host/base.py; some
fill_ids semantics differ).

- sgl-project#29823 (1e80d93) fix draft host pool allocator type
  kv_cache_builder.py: pass allocator_type=server_args.hicache_storage_backend
  into the draft host pool kwargs so the draft path uses the same
  allocator kind as target (was defaulting to None -> wrong allocator
  under storage-backed setups).

- sgl-project#26009 (f4029d0) clear storage reset state
  cache_controller.py reset(): also clear host_mem_release_queue and
  reset prefetch_tokens_occupied so no stale credit / stale slots survive
  a scheduler reset.

- sgl-project#27291 (b7ae714) fix SWA L3 cache miss due to prefetch/hit len mismatch
  scheduler.py: prefetch computes new_input_tokens up to
  _compute_max_prefix_len(len(fill_ids)) rather than the raw tail --
  the storage prefetch key was overshooting SWA-window match_prefix by
  up to page_size, causing systematic L3 misses on full-prefix reuse.
  Upstream uses full_untruncated_fill_ids; our fork uses fill_ids
  (equivalent surface, no _untruncated variant).

- sgl-project#30053 (48ba79c) release HiCache prefetch resources on disagg-prefill
  bootstrap-queue abort
  scheduler.py: when aborting a bootstrap-queued disagg-prefill req,
  also call tree_cache.release_aborted_request(req.rid) so its
  in-flight L2/L3 prefetch resources are released. Previously they
  leaked until process shutdown.

- sgl-project#29310 (bd2a5db) detect double-free / double-alloc in HostKVCache
  memory_pool_host.py HostKVCache: track a per-slot slot_used bool
  tensor; alloc asserts none of the returned slots is already used,
  free asserts every freed slot is currently used. Turns silent
  double-free (which corrupts the free_slots list and eventually
  causes phantom slot reuse) into a loud assertion at the call site.

- sgl-project#29106 (c1b5c7e) fix DSv4 PP HiCache SWA allocation and layer mapping
  deepseek_v4_memory_pool.py: size the SWA sub-pool by stage_layer_num
  (= len(stage_ratios)) instead of the model-global layer_num, matching
  the c4/c128 sub-pools which are already stage-local; add
  get_swa_raw_buffer(layer_id) helper that goes through _swa_local_layer_id
  for consistency.
  hybrid_pool_assembler.py: assert that swa_kv_pool.kv_buffer length
  matches transfer_layer_num (catches the mismatch that sgl-project#29106 fixes at
  the source); rename the misnamed c4_state_global_layers list --
  previously it held LOCAL layer ids but was later indexed with
  compress_state_pools[layer_id + start_layer]. Introduce a proper
  c4_state_local_layers (for c4_state_mapping keys) and a true
  c4_state_global_layers list, and index compress_state_pools /
  indexer_compress_state_pools by the real global id (no offset). Drop
  unused c128_state_mapping / c128_state_global_layers now that C128 is
  not registered with HiCache (upstream sgl-project#27293, already in base).

- sgl-project#30408 (1d8e3c2) fix DSV4 HiSparse SWA tail allocation forwarding
  disaggregation/decode.py DecodePreallocQueue:
  - Admission-control available_size for hisparse now uses
    logical_attn_allocator.full_available_size() when the SWA-tail
    prealloc path is active and that method exists, matching the
    allocation actually issued below.
  - _pre_alloc direct-to-host branch now routes through
    alloc_extend_swa_tail (with swa_tail_len and swa_evicted_seqlen
    bookkeeping) when the SWA-tail path is active; falls back to the
    original alloc_logical_only otherwise. Keeps our fork's
    mem_pool_host.alloc(fill_len) call (upstream uses
    alloc_paged_token_slots but our host pool exposes .alloc()).

Verified: AST parse OK, compileall OK across 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

2 participants