feat(worker): add KVCacheManager stats tier (Phase 5.1 of decouple, refs #175) - #193
Merged
Merged
Conversation
…efs #175) Phase 2 / Slice 5 of the worker decouple initiative — port step. Adds the fifth sibling under `batchgen/worker/`: read-only KV cache stats helpers. Worker unchanged in this PR. Surface: - `batchgen/worker/kv_manager.py` KVStats + HostKVUtilization + KVUtilizationRequest + KVStatsBackend Protocol + KVCacheManager - `tests/worker/test_kv_manager.py` 9 real-fixture unit tests Scope refinement: the KV Cache Helper section has 27 methods totaling ~1330 LOC spanning four distinct concerns (read-only stats, allocation, planning, migration). Porting all in one slice is too risky. This slice establishes the `KVStatsBackend` Protocol + `KVCacheManager` shell with just the three read-only stat methods (~76 LOC of legacy). Allocators, planners, and migration executors land in later sub-slices (5.2+). Methods ported (from worker:3002-3089): - get_host_free_pages was 5 LOC - get_gpu_free_pages was 6 LOC - get_host_utilization was 65 LOC Design follows the per-slice Backend Protocol pattern introduced by `SyncCoordinator` (Slice 3): the handler takes a `KVStatsBackend` that the worker wires to its real KV managers; tests wire `FakeKVBackend` with explicit pre-set stats. Pure read-only — no NCCL, no state mutation, no compare-mode complexity. Refs #175. Part of milestone Worker decouple.
This was referenced May 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Phase 2 / Slice 5 — port step (1 of 3+). Issue #175. Worker unchanged in this PR.
Surface (+426 LOC, 2 files)
batchgen/worker/kv_manager.pyKVStats+HostKVUtilization+KVUtilizationRequest+KVStatsBackendProtocol +KVCacheManagertests/worker/test_kv_manager.pyScope refinement vs original plan
Plan §D claimed Slice 5 would retire the whole KV Cache Helper section (~1330 LOC, 27 methods). Audit shows the 27 methods span four distinct concerns:
Porting all of Slice 5 in one PR would be too risky. This PR establishes the
KVStatsBackendProtocol +KVCacheManagershell with just the three read-only stat methods; the harder allocators / planners / migrations land in later sub-slices.Methods ported
KVCacheManager_get_host_kv_free_pagesget_host_free_pages()_get_gpu_kv_free_pagesget_gpu_free_pages()_get_host_kv_utilizationget_host_utilization(req)Design
KVStatsdataclass for C++-backed page-counter snapshots.HostKVUtilizationfor per-node aggregated views (host KV is shared across all GPUs on a node).KVStatsBackendProtocol: justget_host_stats() -> KVStatsandget_gpu_stats() -> Optional[KVStats]. Production wires the worker'shost_paged_kv_worker_view/gpu_paged_kv_cache_manager; tests wire aFakeKVBackendwith explicit pre-set stats.Pure read-only — no NCCL, no state mutation, no compare-mode complexity. The handler reads stats, aggregates by node, returns a frozen dataclass.
Tests
9 cases: free-page reads, GPU-unbound fallback to 0, node aggregation across an 8-GPU node with mixed sequence statuses (IN_DECODE, PREFILLED, ON_HOLD all counted as valid; QUEUEING and COMPLETED excluded), free_percent handles 0-total edge case, node_rank_end clamps to world_size when nodes are partial, and backend swap works structurally.
Motivation
Refs #175. Part of milestone Worker decouple.
Type of Change
(New module + tests. Worker behavior unchanged.)
Checklist
Pre-merge verification
python -m py_compile batchgen/worker/kv_manager.py tests/worker/test_kv_manager.pysucceeds.pytest tests/worker/ -x→ 67/67 passing.pr-gpu-smoke.yml— runs on PR push.