Skip to content

feat(worker): KVCacheManager stats dual-path gate (Phase 5.1b, refs #175) - #194

Merged
Andrewxu313 merged 1 commit into
mainfrom
tairan/worker-decouple-phase-5-kv-stats-gate
May 28, 2026
Merged

feat(worker): KVCacheManager stats dual-path gate (Phase 5.1b, refs #175)#194
Andrewxu313 merged 1 commit into
mainfrom
tairan/worker-decouple-phase-5-kv-stats-gate

Conversation

@Andrewxu313

Copy link
Copy Markdown
Contributor

Description

Phase 2 / Slice 5 / sub-slice 1b — dual-path gate step. Issue #175. Stacked on the Slice 5.1 port (PR #193, merged).

Wires the read-only stats helpers into the worker behind BATCHGEN_WORKER_KV_STATS_NATIVE / ..._COMPARE env vars.

Surface added (+87 LOC)

Change Lines
Imports (KVCacheManager, KVStats, KVUtilizationRequest, dataclasses) 6
__init__: env reads + lazy _kv_cache_manager field + comment 9
_make_kv_cache_manager() (wires TorchKVStatsBackend reading from host_paged_kv_worker_view + gpu_paged_kv_cache_manager) 23
_make_kv_utilization_request() 8
3 gate wrappers (_get_host_kv_free_pages, _get_gpu_kv_free_pages, _get_host_kv_utilization) ~30
3 _legacy_<name> methods (preserve bodies verbatim) ~75
Net +87

Design note: dict ↔ dataclass adapter

Legacy _get_host_kv_utilization returns a Dict[str, int]; the new KVCacheManager.get_host_utilization returns a HostKVUtilization dataclass. The gate wrapper converts via dataclasses.asdict() so the 4 caller sites see the same dict shape (field names already match exactly). Phase 5.1c will keep the dict return type to avoid touching callers — only the legacy body gets deleted.

Motivation

Refs #175. Part of milestone Worker decouple.

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update

(Migration-window infrastructure. Production behavior unchanged when both env vars are unset.)

Checklist

  • I have read the CONTRIBUTION guide.
  • I have updated the tests (if applicable). tests/worker/test_kv_manager.py covers KVCacheManager directly. Gate validated via H20 dual-mode.
  • I have updated the documentation (if applicable). N/A.

Pre-merge verification

  • python -m py_compile batchgen/batchgen_worker.py succeeds.
  • pytest tests/worker/ -x → 67/67 passing.
  • pr-gpu-smoke.yml — runs on PR push.
  • H20 L2-2048 with COMPARE=1 NATIVE=1 — parity green; accuracy in sampling band.

…se 5.1b, refs #175)

Phase 2 / Slice 5 / sub-slice 1b — dual-path gate.

Wires the read-only stats helpers landed in PR #193 into the worker
behind BATCHGEN_WORKER_KV_STATS_NATIVE / ..._COMPARE env vars.

Surface added:
  - Import KVCacheManager, KVStats, KVUtilizationRequest + dataclasses
  - `_kv_stats_native` + `_kv_stats_compare` env reads at __init__
  - `_kv_cache_manager` lazy field (KVCacheManager constructed on first use)
  - `_make_kv_cache_manager()` — wires a TorchKVStatsBackend that reads
    from `host_paged_kv_worker_view` and `gpu_paged_kv_cache_manager`
  - `_make_kv_utilization_request()` — frozen snapshot per call
  - Gate wrappers around 3 methods (preserve original return types)
  - 3 `_legacy_<name>` methods (preserve bodies verbatim)

Net: +87 LOC on batchgen_worker.py.

Design note: the legacy `_get_host_kv_utilization` returns a dict; the
new `KVCacheManager.get_host_utilization` returns a `HostKVUtilization`
dataclass. The gate wrapper converts the dataclass via
`dataclasses.asdict()` so callers see the same dict shape; the field
names already match exactly. Phase 5.1c will keep the dict return type
to avoid touching the 4 caller sites — only the legacy body gets deleted.

Refs #175. Part of milestone Worker decouple.
@Andrewxu313 Andrewxu313 added this to the Worker decouple milestone May 28, 2026
@Andrewxu313 Andrewxu313 added enhancement New feature or request worker-decouple Worker monolith decouple initiative (May 2026) labels May 28, 2026
@Andrewxu313 Andrewxu313 self-assigned this May 28, 2026
@Andrewxu313
Andrewxu313 merged commit 7102ab8 into main May 28, 2026
5 checks passed
@drunkcoding
drunkcoding deleted the tairan/worker-decouple-phase-5-kv-stats-gate branch July 14, 2026 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request worker-decouple Worker monolith decouple initiative (May 2026)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant