[mem_cache][6/N] refactor: move MHA host-pool into pool_host/mha.py - #30249
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors the codebase by moving MHA-related host memory pool classes (including MHATokenToKVPoolHost, MHATokenToKOnlyPoolHost, AsymmetricMHATokenToKVPoolHost, and get_mha_host_pool_cls) from memory_pool_host.py to a new dedicated module under pool_host/mha.py, updating all corresponding imports across benchmarks, tests, and core components. The review feedback correctly points out that the import of _WRITE_BACK_STAGING_PAGE_CHUNK in memory_pool_host.py is now redundant and should be removed since the class using it was relocated.
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.
|
|
||
| from sglang.srt.mem_cache.pool_host import HostKVCache | ||
| from sglang.srt.mem_cache.pool_host.base import ( | ||
| _WRITE_BACK_STAGING_PAGE_CHUNK, |
|
/rerun-test test/registered/unit/mem_cache/test_mem_pool_host.py test/registered/unit/mem_cache/test_asymmetric_mha_pool_host_unit.py test/registered/unit/mem_cache/test_hicache_staged_write_back_dispatch.py test/registered/jit/test_hicache.py test/registered/jit/test_kvcacheio_asymmetric.py test/registered/unit/mem_cache/test_minimax_sparse_pool_host_unit.py |
|
Results for 🚀 🚀 🚀 |
|
The failing |
|
All other tests have passed |

Motivation
Part of the
pool_hostphase of the mem_cache refactor (issue #25371), continuing directly from #27273 ([5/N]), which extracted the shared host base layer into thepool_host/subpackage.That PR left every concrete host pool (MHA / MLA / Mamba / DeepSeekV4 / DSA) in
memory_pool_host.py, to be relocated one family at a time. This PR relocates the MHA family — the first concrete pool family to move out of the ~3600-linememory_pool_host.pyinto its ownpool_host/mha.pymodule.Modifications
Mechanical relocation. The moved implementation bodies are byte-for-byte unchanged; only the new module's import preamble and caller import routes are rewritten.
MHATokenToKVPoolHost,MHATokenToKOnlyPoolHost,AsymmetricMHATokenToKVPoolHost,get_mha_host_pool_clsmemory_pool_host.pypool_host/mha.py_WRITE_BACK_STAGING_PAGE_CHUNKmemory_pool_host.pypool_host/base.pyThe whole MHA family moves together because it is tightly coupled:
AsymmetricMHATokenToKVPoolHostsubclassesMHATokenToKVPoolHost, andget_mha_host_pool_clsdispatches between the two based on the device pool's K/V dims. Splitting them would create cross-module inheritance for no benefit._WRITE_BACK_STAGING_PAGE_CHUNKis not MHA-specific — it is also read by the MLA / Mamba / DeepSeekV4 / DSA host pools that stay inmemory_pool_host.py. Leaving it there would forcepool_host/mha.pyto import back up from the aggregator module (an inverted layering that becomes a hard import cycle the momentmemory_pool_host.pyever importsmha). Relocating it intopool_host/base.py— which already owns the sibling shared constantHICACHE_HOST_MEMORY_RESERVE_BYTES— keeps the dependency direction pointing down into the package, and both modules import it from there (no duplication).Consistent with #27273,
pool_host/__init__.pyis not touched: the MHA symbols are imported directly frompool_host.mhaat every call site, with no package-level re-export.ruffremoved the now-orphaned MHA-only imports frommemory_pool_host.py(MHATokenToKOnlyPool,MHATokenToKVPool, and severalsgl_kernel/jit_kerneltransfer helpers used only by the moved code).Caller-site impact
12 import sites are redirected from
memory_pool_hosttopool_host.mhafor the moved symbols; a repo-wide symbol scan confirms no other caller imports them frommemory_pool_hostafter this PR.Runtime (5):
kv_cache_builder.py—get_mha_host_pool_cls(method-local lazy import)hiradix_cache.py—get_mha_host_pool_clshybrid_cache/hybrid_pool_assembler.py—MHATokenToKOnlyPoolHost,get_mha_host_pool_clsdisaggregation/decode_kvcache_offload_manager.py—get_mha_host_pool_clsstorage/aibrix_kvcache/unit_test.py—MHATokenToKVPoolHostTests + benchmark (7):
test_mem_pool_host.py,jit/test_hicache.py,jit/test_kvcacheio_asymmetric.py,test_minimax_sparse_pool_host_unit.py,benchmark/hf3fs/bench_zerocopy.py— import redirects only.test_asymmetric_mha_pool_host_unit.py— import redirect plus 4mock.patchtargets retargeted topool_host.mha(the Asymmetric pool's kernels are now resolved in themhamodule namespace).test_hicache_staged_write_back_dispatch.py— import redirect plus a newMHA_POOL_HOST_MODULEconstant; the MHA test's 4mock.patchtargets move to it, while the MLA test's patches stay onMEMORY_POOL_HOST_MODULE(that pool is still inmemory_pool_host.py).Git blame preservation
git blame -C -C -Conpool_host/mha.pyrecovers the moved code's pre-refactor ancestry:pool_host/mha.pyThe 6 untraced lines are the rewritten import preamble.
base.pygains 2 lines (the relocated constant + a blank).memory_pool_host.pyis modified (not deleted), so the remaining pools' blame is preserved natively for the follow-up PRs.Mechanical Move
Transform script (self-contained, reproduces this PR's diff byte-for-byte from the PR base): https://gist.github.com/alphabetc1/bc0d1350cf7d5af01f50a27a1b59b313
Current verified target:
5c6b17c285e5aebb70e0b4770af4f4e3475f9581from base24c42c90be82c24433ab10e65db24e52a48d8084.One-click verification — run from the repo root with Python >= 3.10:
python3 <(curl -sL https://gist.githubusercontent.com/alphabetc1/bc0d1350cf7d5af01f50a27a1b59b313/raw/f7274ac087a4aefe8f56530308b41a154fb6e138/transform_move_mha_host_pool.py)Expected output:
PASS: transform reproduces the commit exactly.Accuracy Tests
Mechanical relocation with byte-for-byte identical implementation bodies, so no model-output change is possible. Correctness of the move is established by the reproducible transform above.
The unit tests that exercise the moved symbols are dispatched via CI (I don't have an on-box CUDA runtime to attach local results):
Speed Tests and Profiling
N/A — mechanical relocation only, no hot-path change.
Checklist
Review and Merge Process
/tag-and-rerun-ci,/tag-run-ci-label,/rerun-failed-ciCI States
Latest PR Test (Base): ❌ Run #28785703297
Latest PR Test (Extra): ✅ Run #28785703187