Skip to content

[HiCache] Compact DSA indexer host storage to producer layers - #38426

Closed
anish-shanbhag wants to merge 22 commits into
sgl-project:mainfrom
anish-shanbhag:perf/hicache-dsa-producer-sidecar
Closed

anish-shanbhag wants to merge 22 commits into
sgl-project:mainfrom
anish-shanbhag:perf/hicache-dsa-producer-sidecar

Conversation

@anish-shanbhag

@anish-shanbhag anish-shanbhag commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

Problem this solves

HiCache currently allocates and transfers a host-side DSA index-K slot for every target transformer layer, including layers that never produce index-K state. Some DSA models compute top-k indices only on selected producer layers and carry those indices through intervening shared-top-k layers. Keeping a host index-K copy for the shared layers wastes memory without preserving additional state.

This is significant for long-context GLM-5.2 NVFP4 serving. With index_topk_freq=4 and index_skip_topk_offset=3, only 21 of 78 target layers produce top-k indices; the other 57 reuse the preceding producer's indices. The current host sidecar nevertheless reserves all 78 index-K layers.

For the measured configuration, the per-token host allocation was:

Component Before After
Target MLA KV (all 78 layers) 44,928 bytes 44,928 bytes
DSA index-K sidecar 10,296 bytes (78 layers) 2,772 bytes (21 layers)
Packed draft state 576 bytes 576 bytes
Total 55,800 bytes 48,276 bytes

The change saves 7,524 bytes per cached token, or 13.48% of total host-cache bytes per token. In full disaggregated workload runs, this made a 170 GiB HiCache setting feasible: prefill-node RSS was approximately 719.5–726.7 GiB, versus about 827 GiB estimated for the previous all-layer allocation. A 180 GiB setting still reached approximately 759.3 GiB and OOMed, so this removes a specific source of over-allocation rather than providing unlimited headroom.

What changes

  • Derive the stage-local producer-layer list with the existing dsa_layer_skips_topk predicate used by model execution.
  • Give the indexer sidecar its own filtered layer mapping while leaving the anchor MLA KV mapping unchanged.
  • Pack target device pointers in producer-layer order and map original device layer IDs to compact host slots.
  • Skip index-K backup/restore for shared-top-k layers.
  • Preserve packed MTP draft indexer buffers after the compacted target layers and keep their transfer mapping intact.
  • Retain the existing all-layer behavior for callers that do not provide a restricted layer list.

This only changes the DSA index-K host sidecar. It does not compact ordinary MLA KV, target device KV/index-K allocation, model weights, attention math, indexer math, logits, or packed draft state.

Why the mapping is expected to preserve model behavior

dsa_layer_skips_topk(config, layer_id) is the canonical predicate for layers that reuse an earlier layer's top-k result. Such layers do not run the indexer and therefore do not create independent index-K state to offload. Producer layers keep a one-to-one device-to-host association through an explicit compact mapping, and every producer buffer remains present in bulk and per-layer transfer paths.

Current SGLang already uses the same skip predicate to create zero-row device index-K placeholders when hierarchical cache, disaggregation, and external linking are disabled. Those modes currently prevent that device-side elision because the HiCache transfer layout assumes one host slot per layer. This change addresses that transfer/layout assumption while retaining full device allocation in the affected modes.

MTP drafts are handled separately: target producer buffers are packed first, followed by every draft buffer. The controller continues to expose the original target-layer span, maps draft transfer layers after that span, and the host pool translates them to slots immediately after the compact target section.

Workload evidence

Two full GLM-5.2 NVFP4 workload validations using the compact sidecar scored GSM8K exact-match values of 0.9697 and 0.9613, both above a 0.9 acceptance gate. The same runs exercised realistic prefix-cache traffic and completed with the expected 21-of-78 producer mapping.

These scores rule out a gross end-to-end accuracy regression in those runs, but they do not prove bitwise or numerical equivalence. They were not patched-versus-unpatched twins on identical requests, and GSM8K does not guarantee that every evaluated request crossed a host eviction/restore boundary.

Tests

Added or strengthened coverage for:

  • the exact 21-of-78 GLM producer-layer pattern, including a pipeline-stage-local slice;
  • the no-sharing fallback where all 78 layers remain present;
  • compact target pointer packing with packed MTP draft buffers retained in order;
  • distinct anchor and indexer layer maps, verifying ordinary MLA KV keeps all target and draft layers;
  • exact device-to-host copies for producer index-K buffers while ordinary MLA KV is still copied for every layer;
  • the existing page-registration test with explicit all-layer selection.

Checks run in the development environment:

ruff format --check <changed Python files>        PASS
ruff check <changed Python files>                 PASS
git diff --check                                  PASS
python3 -m compileall -q <changed Python files>   PASS

The targeted pytest module could not be collected in the available login environment. The local Python has no PyTorch; the available CUDA PyTorch environment first had an incompatible Transformers version, and with the required Transformers version overlaid, importing torchaudio failed because libcudart.so.12 was unavailable on the login host. No GPU-capable worker was available for a real eviction/restore test, so the added GPU transfer coverage is left to CI.

Limitations and follow-up

  • The full workload evidence covers GLM-5.2 with index_topk_freq=4, index_skip_topk_offset=3, PP1, and the standard DSA execution path. Other producer patterns use the same predicate but were not exercised end to end here.
  • The unit tests establish the selected-layer mapping and transfer invariants; they do not establish numerical parity of model outputs.
  • The decisive follow-up is a deterministic patched/unpatched test that fills device KV, forces selected prefixes into HiCache, restores them, and compares next-token logits or greedy tokens request by request. Instrumenting skipped-layer index-K access to fail during that test would provide an additional invariant check.
  • Direct speedup is not claimed. This is a capacity-enabling optimization: it allows more useful MLA KV to fit in a fixed host-memory envelope.

CI States

Latest PR Test (Base): ⏳ Run #36780642759
Latest PR Test (Extra): ❌ Run #36780642642
Latest PR Test (AMD ROCm 10): ⏳ Run #36780642928

@github-actions github-actions Bot added the hicache Hierarchical Caching for SGLang label Sep 8, 2026
@anish-shanbhag
anish-shanbhag force-pushed the perf/hicache-dsa-producer-sidecar branch from 45c3a5c to 35fc49c Compare September 9, 2026 00:10
@anish-shanbhag
anish-shanbhag marked this pull request as ready for review September 9, 2026 00:10
@anish-shanbhag
anish-shanbhag force-pushed the perf/hicache-dsa-producer-sidecar branch from 35fc49c to 98bb500 Compare September 9, 2026 00:12
@nvpohanh

nvpohanh commented Sep 9, 2026

Copy link
Copy Markdown
Collaborator

/rerun-tests test/registered/radix_cache/unified_radix_tree/test_unified_radix_cache_kl_glm52.py

@github-actions

github-actions Bot commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

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

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

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

@nvpohanh

nvpohanh commented Sep 9, 2026

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 Sep 9, 2026
@nvpohanh

nvpohanh commented Sep 9, 2026

Copy link
Copy Markdown
Collaborator

@anish-shanbhag Could you fix this real pipeline failure?

Traceback (most recent call last):
  File "/home/runner/work/sglang/sglang/python/sglang/srt/utils/common.py", line 3514, in retry
    return fn()
  File "/home/runner/work/sglang/sglang/python/sglang/test/test_utils.py", line 2196, in <lambda>
    lambda: super(CustomTestCase, self)._callTestMethod(method),
  File "/opt/hostedtoolcache/Python/3.10.21/x64/lib/python3.10/unittest/case.py", line 549, in _callTestMethod
    method()
  File "/home/runner/work/sglang/sglang/test/registered/unit/mem_cache/test_hicache_staged_write_back_dispatch.py", line 889, in test_dsa_indexer_backup_then_load_roundtrip_uses_staged
    host.load_to_device_per_layer(
  File "/home/runner/work/sglang/sglang/python/sglang/srt/mem_cache/pool_host/dsa.py", line 285, in load_to_device_per_layer
    else self._host_layer_index(layer_id)
  File "/home/runner/work/sglang/sglang/python/sglang/srt/mem_cache/pool_host/dsa.py", line 246, in _host_layer_index
    return self.host_layer_by_device.get(layer_id)
AttributeError: 'DSAIndexerPoolHost' object has no attribute 'host_layer_by_device'
E

https://github.com/sgl-project/sglang/actions/runs/34294022525/job/102316020645?pr=38426

@anish-shanbhag

Copy link
Copy Markdown
Contributor Author

@nvpohanh Fixed in 8b0a4b72b8. This test fixture constructs DSAIndexerPoolHost with __new__, bypassing __init__, so it now initializes the identity host_layer_by_device mapping that the pre-compaction host layout represents. The new base-a CPU CI ran test_hicache_staged_write_back_dispatch.py (all 17 tests passed, including test_dsa_indexer_backup_then_load_roundtrip_uses_staged): https://github.com/sgl-project/sglang/actions/runs/34321708659/job/102369838907?pr=38426

@nvpohanh

nvpohanh commented Sep 9, 2026

Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@nvpohanh

Copy link
Copy Markdown
Collaborator

[by Codex] @anish-shanbhag, the relevant CI failure appears to be caused by this PR: https://github.com/sgl-project/sglang/actions/runs/34558027928/job/103137031714. test_hiradix_extra_pools_include_minimax_indexer fails because the new _get_extra_pools() access to self.cache_controller.mem_pool_host.entry_map raises AttributeError for the test fixture that creates HybridCacheController via object.__new__. Could you please fix it? Thanks!

@nvpohanh

Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@nvpohanh

Copy link
Copy Markdown
Collaborator

/rerun-tests test/registered/e2e/models/test_glm53_flash_b200.py

@github-actions

github-actions Bot commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-tests test/registered/e2e/models/test_glm53_flash_b200.py:

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

cd test/ && python3 registered/e2e/models/test_glm53_flash_b200.py

@nvpohanh

Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

1 similar comment
@nvpohanh

Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@nvpohanh

Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@nvpohanh

Copy link
Copy Markdown
Collaborator

@anish-shanbhag could you fix the conflicts?

anish-shanbhag and others added 2 commits September 21, 2026 10:18
…dex/pr-38426-merge

# Conflicts:
#	python/sglang/srt/mem_cache/hybrid_cache/hybrid_pool_assembler.py
@nvpohanh

Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@nvpohanh

nvpohanh commented Oct 1, 2026

Copy link
Copy Markdown
Collaborator

Let's use #41769 instead

@nvpohanh nvpohanh closed this Oct 1, 2026
wenscarl pushed a commit to wenscarl/sglang that referenced this pull request Oct 2, 2026
Resolves the conflicts in the DSA indexer host pool. main has since grown its
own producer-layer compaction: the host pool declaration now carries
`owned_device_layers`, built in pool_host/dsa.py from `skip_topk_layers`, and
DSAIndexerPoolHost derives `_live_target_layers` / `_device_to_host_layer` /
`target_layer_num` from it.

That supersedes the host-pool half this branch carried from sgl-project#38426 (closed in
favour of this PR), so the conflicting files take main's version:

- mem_cache/pool_host/dsa.py
- mem_cache/hybrid_cache/hybrid_pool_assembler.py
- test/registered/unit/mem_cache/test_hicache_staged_write_back_dispatch.py
- test/registered/unit/mem_cache/test_dsa_pool_host_unit.py
- test/registered/unit/mem_cache/test_hicache_host_register.py

`get_dsa_hicache_indexer_layers` and the `hicache_indexer_layers` assignment it
fed are removed as dead code -- main derives the same producer set directly from
`skip_topk_layers`, without re-deriving it from the model config. The
`_should_elide_dsa_index_k` docstring now points at that declaration instead.

What this branch still contributes, unchanged:

- `_should_elide_dsa_index_k` drops the `enable_hierarchical_cache` and
  `disaggregation_mode == "null"` conditions, so device index-K elision also
  applies under HiCache and PD disaggregation.
- The Mooncake PD transfer tolerates the resulting zero-length entries: it
  aligns the peers' layer spans before comparing, drops entries elided on both
  peers, raises when an entry is elided on exactly one, and refuses to register
  or queue a zero-length region.

Net against main: 2 files, +80/-2.

Co-Authored-By: Claude Opus 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hicache Hierarchical Caching for SGLang run-ci CI: run the baseline test suite on this PR unified-radix-cache

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants