[TopK] Python reader for the exact-restart counters

Reads persistent_topk_exact_counters into {slot: value}, logs one line on a
cadence, and degrades to a one-time warning when the op is absent.

Co-Authored-By: Hinata <hinata@idihore.id>
Signed-off-by: MOVZX <movzx@yahoo.com>
This commit is contained in:
2026-10-09 19:55:59 +07:00
co-authored by Hinata
parent 7f4385a422
commit 66c3d11016
3 changed files with 198 additions and 3 deletions
@@ -0,0 +1,167 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""Python reader for the medium-path exact-restart top-k counters.
The counters themselves are device-global uint32s owned by
`csrc/libtorch_stable/topk.cu`; `torch.ops._C.persistent_topk_exact_counters`
snapshots them into an int32 CUDA tensor. Without this module nothing in a
running engine ever calls that op, so the instrument records into a buffer no
one reads -- which measures nothing.
SLOT LAYOUT is build-dependent, so nothing here hard-codes five. `slot_count()`
asks the build first (a `..._slots` op, should a later build expose one) and
otherwise reads `MERLIN_TOPK_COUNTER_SLOTS` (default 5); names come from a
table keyed by that length:
5 rows overflow fallbacks faults switch
10 rows overflow fallbacks faults rows_large ties_cut ties_dropped
faults_large switch switch_large
An unknown length degrades to `slot<i>` names rather than mislabelling.
HOW THE SMOKE READS IT. Set `MERLIN_TOPK_EXACT_COUNTERS_LOG=<seconds>` on the
engine; each GPU worker then emits, at most that often and once more at
shutdown, one line into the ordinary engine log:
topk-exact-counters rank=PP0_TP1 rows=... overflow=... fallbacks=...
faults=... switch=...
Counts are cumulative per worker process, so the smoke takes the LATEST line
per rank -- `grep topk-exact-counters <engine log> | ...` keyed on the `rank=`
field -- and reads `faults` as the pass/fail number (it must stay 0) with
`rows` as the denominator. Earlier lines are only useful as a rate.
COST. `read()` passes `sync=1`, i.e. a full device synchronize, because a
stream-ordered snapshot can miss increments from work in flight on another
stream. That is acceptable ONLY because the cadence is coarse (>= 30 s in
practice): the time check happens before the op call, so a step that is not
due for a log touches nothing but `time.monotonic()`. The read is also refused
inside a cudagraph capture -- the op allocates and synchronizes, which would
abort the capture.
"""
import os
import time
import torch
import vllm.envs as envs
from vllm.logger import init_logger
logger = init_logger(__name__)
_OP_NAME = "persistent_topk_exact_counters"
_MEDIUM_NAMES = ("rows", "overflow", "fallbacks", "faults", "switch")
_LARGEDET_NAMES = (
"rows",
"overflow",
"fallbacks",
"faults",
"rows_large",
"ties_cut",
"ties_dropped",
"faults_large",
"switch",
"switch_large",
)
_SLOT_NAMES: dict[int, tuple[str, ...]] = {5: _MEDIUM_NAMES, 10: _LARGEDET_NAMES}
# The medium-path line contract. Any further name is appended after these, in slot
# order, so a 10-slot build still starts with exactly the medium-path line.
_LINE_ORDER = _MEDIUM_NAMES
_warned_absent = False
_warned_failed = False
_last_log_monotonic = 0.0
def _get_op(name: str):
try:
return getattr(torch.ops._C, name, None)
except (AttributeError, RuntimeError):
return None
def slot_count() -> int:
"""How many int32 slots this build's counter array carries."""
query = _get_op(_OP_NAME + "_slots")
if query is not None:
return int(query())
return int(os.getenv("MERLIN_TOPK_COUNTER_SLOTS", "5"))
def slot_names(count: int) -> tuple[str, ...]:
names = _SLOT_NAMES.get(count)
return names if names is not None else tuple(f"slot{i}" for i in range(count))
def read(reset: bool = False, sync: bool = True) -> dict[str, int]:
"""Snapshot the counters as {slot name: value}; {} when unreadable."""
global _warned_absent
op = _get_op(_OP_NAME)
if op is None:
if not _warned_absent:
_warned_absent = True
logger.warning(
"torch.ops._C.%s is not in this build; the top-k exact-restart "
"counters cannot be read.",
_OP_NAME,
)
return {}
if reset and not sync:
raise ValueError("reset requires sync: zeroing races in-flight atomics")
if not (torch.cuda.is_available() and torch.cuda.is_initialized()):
return {}
if torch.cuda.is_current_stream_capturing():
# The op allocates and synchronizes; calling it here aborts the capture.
return {}
count = slot_count()
device = torch.device("cuda", torch.cuda.current_device())
out = torch.zeros(count, dtype=torch.int32, device=device)
op(out, 1 if reset else 0, 1 if sync else 0)
return dict(zip(slot_names(count), out.tolist(), strict=True))
def rank_tag() -> str:
from vllm.distributed.parallel_state import get_pp_group, get_tp_group
try:
return f"PP{get_pp_group().rank_in_group}_TP{get_tp_group().rank_in_group}"
except Exception:
# Pre-init or single-process: say so rather than claim rank 0.
return "PP?_TP?"
def log_line(tag: str) -> str:
"""The one-line form the smoke greps; "" when the counters are unreadable."""
counters = read()
if not counters:
return ""
order = [name for name in _LINE_ORDER if name in counters]
order += [name for name in counters if name not in _LINE_ORDER]
fields = " ".join(f"{name}={counters[name]}" for name in order)
return f"topk-exact-counters rank={tag} {fields}"
def maybe_log(force: bool = False) -> str:
"""Emit the line if due (or forced). Time check first: when not due this
costs one clock read and never touches the device."""
global _last_log_monotonic, _warned_failed
period = envs.MERLIN_TOPK_EXACT_COUNTERS_LOG
if period <= 0:
return ""
now = time.monotonic()
if not force and now - _last_log_monotonic < period:
return ""
_last_log_monotonic = now
try:
line = log_line(rank_tag())
except Exception as exc: # never let an instrument break a step or shutdown
if not _warned_failed:
_warned_failed = True
logger.warning("top-k exact-restart counter read failed: %s", exc)
return ""
if line:
logger.info("%s", line)
return line