Skip to content

[mem_cache][5/N] refactor: extract host KV cache base layer into pool_host package - #27273

Merged
ispobock merged 14 commits into
sgl-project:mainfrom
alphabetc1:refactor/mem-cache-poolhost
Jun 20, 2026
Merged

[mem_cache][5/N] refactor: extract host KV cache base layer into pool_host package#27273
ispobock merged 14 commits into
sgl-project:mainfrom
alphabetc1:refactor/mem-cache-poolhost

Conversation

@alphabetc1

@alphabetc1 alphabetc1 commented Jun 4, 2026

Copy link
Copy Markdown
Collaborator

Motivation

First PR of the pool_host phase of the mem_cache refactor (issue #25371). Sibling of the completed allocator series (#26675, #26676).

memory_pool_host.py is a ~2960-line module holding the host-side KV cache base class, shared allocation helpers, and every concrete host pool (MHA / MLA / Mamba / DeepSeekV4 / DSA). This PR extracts only the shared base layer into a new pool_host/ subpackage. All concrete pool classes stay in memory_pool_host.py and will be relocated one family at a time in follow-up PRs (mha, mla, mamba, deepseek_v4, nsa, group), all tracked under #25371.

Modifications

Mechanical relocation. The moved implementation bodies are unchanged; only import preambles and caller import routes are rewritten.

Symbol From To
HostKVCache (abc), synchronized, HICACHE_HOST_MEMORY_RESERVE_BYTES memory_pool_host.py pool_host/base.py
HostTensorAllocator, get_allocator_from_storage, alloc_with_host_register, alloc_with_pin_memory, ALLOC_MEMORY_FUNCS memory_pool_host.py pool_host/common.py
HiSparseHostPoolMixin memory_pool_host.py pool_host/hisparse.py

HiSparseHostPoolMixin gets its own pool_host/hisparse.py (rather than base.py or mla.py) because it is a concrete mixin shared by multiple host pools (MLATokenToKVPoolHost and DeepSeekV4PagedHostPool, both still in memory_pool_host.py). Keeping it separate from mla.py avoids making the DeepSeekV4/paged host path depend on an MLA-family module in follow-up moves.

pool_host/__init__.py intentionally re-exports only the package-level public API used by callers today: HostKVCache and HostTensorAllocator. Lower-level helpers stay importable from their owning submodules (pool_host.base, pool_host.common, pool_host.hisparse). The Mooncake allocator import inside get_allocator_from_storage stays lazy (function-local), matching the original.

memory_pool_host.py keeps every concrete pool class. It imports the package-level HostKVCache from pool_host; imports base-only helpers (HICACHE_HOST_MEMORY_RESERVE_BYTES, synchronized) from pool_host.base; imports host allocation helpers (ALLOC_MEMORY_FUNCS, get_allocator_from_storage) from pool_host.common; and imports HiSparseHostPoolMixin from pool_host.hisparse for its still-local implementations. ruff removed the now-orphaned abc, defaultdict, wraps, alloc_mmap, and KVCache imports from it.

Caller-site impact

12 import sites across 11 files are redirected away from memory_pool_host for moved symbols:

  • 7 sites: bare HostKVCache imports now use sglang.srt.mem_cache.pool_host; 2 of these are inside TYPE_CHECKING blocks.
  • 1 site: mooncake_store.py splits the not-yet-moved MLATokenToKVPoolHost import from memory_pool_host while moving HostKVCache / HostTensorAllocator to pool_host.
  • 4 sites: ALLOC_MEMORY_FUNCS / alloc_with_pin_memory now import from pool_host.common (jit_kernel/tests/test_hicache.py, test_dsa_pool_host_unit.py, plus 2 method-local lazy imports in test_hisparse_unit.py).

A repo-wide symbol scan confirms no external caller imports the moved symbols from memory_pool_host after this PR; memory_pool_host.py itself still imports the helpers because its concrete host pools remain there for now.

Git blame preservation

git blame -C -C -C on the three relocated-code files recovers their pre-refactor ancestry from memory_pool_host.py and earlier history:

File Traced / Total
base.py 171 / 176
common.py 86 / 93
hisparse.py 69 / 71

= 326 / 340 (95.9%) of relocated lines traced; the rest are rewritten import preambles. pool_host/__init__.py is a new 7-line re-export module. memory_pool_host.py is modified (not deleted), so the concrete pools' blame is preserved natively for the follow-up PRs.

Mechanical Move

Transform script (self-contained, reproduces this PR's current diff byte-for-byte from the current PR base): https://gist.github.com/alphabetc1/34850017427438e4b26fad6fb3874d2a

Current verified target: 7e7e3820c26ba0609f017b99358172a9776197de from base e1955bf57ab39a4de7f7daf9a619e4f68f952ed9.

One-click verification — run from the repo root with Python >= 3.10:

python3.10 <(curl -sL https://gist.githubusercontent.com/alphabetc1/34850017427438e4b26fad6fb3874d2a/raw/5cd0e3a845800883de7ca210eec603835a031f59/transform_pool_host_base.py)

Expected output: PASS: transform reproduces the commit exactly.

Accuracy Tests

The unit tests that exercise the moved symbols (host pools + allocation funcs) all pass on an NVIDIA H20:

python -m pytest -q \
    test/registered/unit/mem_cache/test_dsa_pool_host_unit.py \
    test/registered/unit/managers/test_hisparse_unit.py \
    python/sglang/jit_kernel/tests/test_hicache.py
21 passed, 23 warnings in 50.18s

Speed Tests and Profiling

N/A — mechanical relocation only, no hot-path change.

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 #27803114008
Latest PR Test (Extra): ❌ Run #27803113902

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the host-side memory pool implementation by splitting memory_pool_host.py into a modular package under pool_host/ and updating all dependent imports. Feedback on these changes includes correcting the page alignment calculation in HostKVCache to prevent allocating an unnecessary extra page, and relaxing the type hint for the allocator parameter in alloc_with_pin_memory to avoid static type-checking issues.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +57 to +58
self.page_num = self.size // self.page_size + 1
self.size = self.page_num * self.page_size

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Using self.size // self.page_size + 1 to align the host memory pool size always allocates an extra page even when self.size is already a multiple of self.page_size. It is more efficient and standard to use the round-up formula (self.size + self.page_size - 1) // self.page_size.

        self.page_num = (self.size + self.page_size - 1) // self.page_size\n        self.size = self.page_num * self.page_size

dtype: torch.dtype,
device: str,
pin_memory: bool,
allocator: None,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The type hint allocator: None is overly restrictive and will cause static type checkers to complain when a HostTensorAllocator instance is passed (which happens in memory_pool_host.py). Changing it to allocator: HostTensorAllocator | None = None makes it compatible with the signature of alloc_with_host_register and type-safe.

Suggested change
allocator: None,
allocator: HostTensorAllocator | None = None,

@alphabetc1
alphabetc1 force-pushed the refactor/mem-cache-poolhost branch from a0a9628 to fb7fcd7 Compare June 4, 2026 11:51
@alphabetc1
alphabetc1 force-pushed the refactor/mem-cache-poolhost branch from fb7fcd7 to d3b6125 Compare June 5, 2026 11:18
@alphabetc1

Copy link
Copy Markdown
Collaborator Author

/tag-and-rerun-ci

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.

is this file necessary? should we combine it with the base.py?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I think base.py is for abstract base classes, and utilities shared across modules should go in a separate file.

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

looks good

@hzh0425

hzh0425 commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

please resolve the conflicts and rerun ci

@alphabetc1
alphabetc1 force-pushed the refactor/mem-cache-poolhost branch from 1ded307 to 9f863fa Compare June 16, 2026 03:09
@alphabetc1

Copy link
Copy Markdown
Collaborator Author

/rerun-group radix_cache/unified_radix_tree/hicache

@github-actions

Copy link
Copy Markdown
Contributor

radix_cache/unified_radix_tree/hicache: Unknown test group radix_cache/unified_radix_tree/hicache.

Known groups: 4-gpu-models, 8-gpu-models, amd, ascend, attention, backends, bench_fn, chunked_prefill, constrained_decoding, core, cp, cpu, cuda_graph, debug_utils, disaggregation, dllm, dp_attn, dp_engine, ep, eplb, eval, function_call, gb300, hicache, input_embedding, jit, kernels, kv_canary, layers, lora, mla, mock_model, model_loading, models, models_e2e, moe, musa, observability, openai_server, ops, perf, pp, prefill_only, profiling, quant, radix_cache, reasoning, rl, rotary, sampling, scheduler, scripted_runtime, sessions, spec, stress, tokenizer, unit, utils, vlm, xpu

@alphabetc1

Copy link
Copy Markdown
Collaborator Author

/rerun-group radix_cache hicache

@github-actions

github-actions Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Results for /rerun-group radix_cache hicache:

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

cd test/ && python3 registered/radix_cache/test_cpp_radix_cache.py
cd test/ && python3 registered/radix_cache/test_swa_radix_cache_kl.py
cd test/ && python3 registered/hicache/test_hicache_spec_file_storage.py
cd test/ && python3 registered/hicache/test_hicache_variants.py

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

cd test/ && python3 registered/radix_cache/test_int8_mamba_checkpoint_e2e.py
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
cd test/ && python3 registered/hicache/test_hicache_storage_3fs_backend.py
cd test/ && python3 registered/hicache/test_qwen35_hicache.py

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

cd test/ && python3 registered/radix_cache/test_radix_attention.py
cd test/ && python3 registered/radix_cache/test_radix_cache_hit.py
cd test/ && python3 registered/hicache/test_hicache_storage.py

🚀 2-gpu-h100 (6 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
cd test/ && python3 registered/hicache/test_hicache_spec_mooncake_storage.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

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

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

@alphabetc1

Copy link
Copy Markdown
Collaborator Author

View workflow run

image This failed CI is irrelevant

@ispobock

Copy link
Copy Markdown
Collaborator

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

@github-actions

github-actions Bot commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

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

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

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

@ispobock
ispobock merged commit ff1fc1f into sgl-project:main Jun 20, 2026
355 of 379 checks passed
@alphabetc1
alphabetc1 deleted the refactor/mem-cache-poolhost branch June 20, 2026 14:55
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants