Skip to content

[Refactor] mem_cache: [11/N] extract KVCache and BaseSWAKVPool into pool/base.py - #35647

Open
alphabetc1 wants to merge 4 commits into
sgl-project:mainfrom
alphabetc1:refactor/mem-cache-pool-base
Open

alphabetc1 wants to merge 4 commits into
sgl-project:mainfrom
alphabetc1:refactor/mem-cache-pool-base

Conversation

@alphabetc1

@alphabetc1 alphabetc1 commented Aug 20, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

Part of #25371. Mechanical Move. First PR of the mem_cache/pool/ layer: create the package and move the base layer every device pool derives from into it, so later family moves have a target.

Modifications

Rebuilt on current main. Four commits, each labeled by kind:

Commit Kind What
add the mem_cache pool package non-mechanical empty mem_cache/pool/__init__.py
extract KVCache and the write-loc helpers mechanical KVWriteLoc, _check_dense_loc, window_write_locs, layer_window_locs, unwrap_write_loc, write_loc_is_physical, KvBufferDesc, KVCache, GB from memory_pool.py into pool/base.py; every importer repointed
move BaseSWAKVPool mechanical BaseSWAKVPool from base_swa_memory_pool.py (deleted) into pool/base.py; every importer repointed
stub unwrap_write_loc on pool.base non-mechanical test/manual/minimax_m3/test_npu_memory_pool.py fakes sglang.srt.mem_cache.pool.base for the NPU pool's new import

No re-export shim: all importers (83 files) import from pool.base directly, as the check-indirect-imports hook requires. memory_pool.py imports back the six names it still uses; ruff drops its now-unused imports (abc, KW_ONLY, IdSpace, PlainPoolRouter).

Accuracy Test

Mechanical move, so the bar is byte-level equality:

  • Both mechanical commits are reproduced byte-for-byte from main by the mechanical-refactor-verify primitives (cut each def/class verbatim, author only the new module's import header, repoint imports), then pre-commit and a byte diff against the commit. The chain verifier reports PASS for both proofs and HUMAN_REVIEW for the two small non-mechanical commits above.
  • Every changed module imports in a fresh interpreter on both trees; the only failures are the same hardware-only modules (mlx, torch_npu, MUSA flash-attn, XPU kernels) on main and on this branch. pool.base imports first without a cycle.
  • test/registered/unit/mem_cache/, test/registered/unit/disaggregation/ and every changed test file, same command on main and on this branch: 5115 passed / 19 failed / 1 collection error on both, with identical failing test IDs (pre-existing on main).
  • The NPU memory pool manual test passes on both.

Benchmark and Profiling Results

Not applicable -- no runtime behavior changes.

Checklist

  • Format your code according to the Code Formatting with Pre-Commit.
  • Add unit tests as outlined in the Running Unit Tests.
  • Update documentation / docstrings / example tutorials as needed, according to Writing Documentation.
  • Provide throughput / latency benchmark results and accuracy evaluation results as needed, according to Benchmark and Profiling.
  • For reviewers: If you haven't made any contributions to this PR and are only assisting with merging the main branch, please remove yourself as a co-author when merging the PR.
  • Please feel free to join our Slack channel at https://slack.sglang.ai to discuss your PR.

🤖 Generated with Claude Code


CI States

Latest PR Test (Base): ✅ Run #43675877392
Latest PR Test (Extra): ❌ Run #43675877334
Latest PR Test (AMD ROCm 10): ❌ Run #43675877496

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

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e6bbc4efc0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines 14 to 16
from sglang.srt.mem_cache.pool.base import (
unwrap_write_loc,
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Update the mocked module tree for the new pool import

When test/manual/minimax_m3/test_npu_memory_pool.py runs _load_npu_memory_pool_module(), it stubs sglang.srt.mem_cache as a plain ModuleType and provides only the old memory_pool.unwrap_write_loc. Executing this newly added import therefore raises ModuleNotFoundError: 'sglang.srt.mem_cache' is not a package before any of the standalone NPU pool tests run. The loader needs to stub sglang.srt.mem_cache.pool.base and its unwrap_write_loc symbol, or otherwise load the real package.

Useful? React with 👍 / 👎.

@alphabetc1 alphabetc1 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 Aug 20, 2026
@alphabetc1
alphabetc1 force-pushed the refactor/mem-cache-pool-base branch from e6bbc4e to 2e551e7 Compare September 2, 2026 10:51
@github-actions github-actions Bot added hicache Hierarchical Caching for SGLang memory-pool labels Sep 2, 2026
@alphabetc1 alphabetc1 changed the title [mem_cache][11/N] refactor: extract KVCache and BaseSWAKVPool into pool/base.py [Refactor] mem_cache: [11/N] extract KVCache and BaseSWAKVPool into pool/base.py Oct 14, 2026
@alphabetc1
alphabetc1 requested a review from fzyzcjy as a code owner October 20, 2026 01:11
@github-actions github-actions Bot added the amd label Oct 20, 2026

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

amd apple-silicon blackwell SM100/SM120 bypass-fastfail deepseek hicache Hierarchical Caching for SGLang memory-pool mthreads npu run-ci CI: run the baseline test suite on this PR run-ci-extra CI: also run the extra suite (requires run-ci) unified-radix-cache

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant