[TopK] Large-path hardening

- Inline-PTX acquire/release helpers declare their memory effects.
- The fault verdict is aggregated across the CTA group before emission, so a
  faulted row is failed closed by every CTA.
- 64-bit device counters and an int64 reader (counter protocol v2).
- Tests: adversarial distributions, slot-residue coverage, an sm_90 gate for
  tests that need it.

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 63e2d8be20
commit 512ca2fc0b
3 changed files with 314 additions and 79 deletions
@@ -2,12 +2,20 @@
# 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
The counters themselves are device-global uint64s 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
snapshots them into an **int64** 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.
DTYPE IS PART OF THE PROTOCOL (large-path determinism change). Version 1 was u32
device counters read into a signed int32 tensor, which goes negative after
~33k all-equal rows at k=512 and wraps after ~66k, so a delta taken across the
boundary is a wrong number that looks like a measurement. Version 2 widens
both sides, and the op REJECTS an int32 buffer rather than filling it from half
of each counter. An older build whose op still demands int32 will therefore
raise here; that is the intended signal, not a regression to paper over.
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
@@ -84,7 +92,7 @@ def _get_op(name: str):
def slot_count() -> int:
"""How many int32 slots this build's counter array carries."""
"""How many int64 slots this build's counter array carries."""
query = _get_op(_OP_NAME + "_slots")
if query is not None:
return int(query())
@@ -118,7 +126,9 @@ def read(reset: bool = False, sync: bool = True) -> dict[str, int]:
return {}
count = slot_count()
device = torch.device("cuda", torch.cuda.current_device())
out = torch.zeros(count, dtype=torch.int32, device=device)
# int64: the device counters are 64-bit and the op refuses any other
# width. See the module docstring -- this is protocol version 2.
out = torch.zeros(count, dtype=torch.int64, device=device)
op(out, 1 if reset else 0, 1 if sync else 0)
return dict(zip(slot_names(count), out.tolist(), strict=True))