[TopK] Determinism toggles for greedy repeatability
- MERLIN_FULL_K: deterministic Marlin MoE split-K (after unmerged upstream #46639). - MERLIN_QSA_SORT: canonical order for the QSA selected blocks. - MERLIN_TIE_DET: deterministic tie-break at the persistent_topk cutoff. - MERLIN_TIE_RECENT: cutoff ties keep the highest indices. - Decode candidate truncation is detected; one tie direction across the medium path; the kernel switches are registered. Co-Authored-By: Hinata <hinata@idihore.id> Signed-off-by: MOVZX <movzx@yahoo.com>
This commit is contained in:
+125
-26
@@ -461,32 +461,12 @@ if TYPE_CHECKING:
|
||||
# an identity, it does not choose one. Ignored factor.
|
||||
VLLM_E2_EXPECTED_CUDAGRAPH_MODE: str = ""
|
||||
#
|
||||
# --8<-- [start:fork-unaudited-knobs]
|
||||
# --- TODO: knobs that are NOT in this tree at all ----------
|
||||
# The tie-determinism toggles and the BSM pin live in a separate
|
||||
# patch stack, not in v2.0.1 and not in this tree:
|
||||
#
|
||||
# MERLIN_FULL_K MERLIN_QSA_SORT MERLIN_TIE_DET MERLIN_TIE_RECENT
|
||||
# MERLIN_PIN_BSM
|
||||
#
|
||||
# They are therefore UNAUDITED: nothing in this repository shows what they
|
||||
# change, and this file will not classify code it cannot read. The rule they
|
||||
# fall under is the standing one -- an os.environ (or std::getenv) knob that
|
||||
# changes a compiled or captured artifact is not in the cache key unless it
|
||||
# is registered here. From what is known of them (Marlin split-K, a
|
||||
# persistent_topk ordering change and a cutoff tie-break) they are very
|
||||
# likely capture factors and possibly AOT factors, which is exactly why a
|
||||
# guess is not good enough.
|
||||
#
|
||||
# WHEN THE PATCHES LAND, for each name: register it below, label both
|
||||
# identities from its actual read site, hash it unless exclusion is proven,
|
||||
# and delete it from this block. tests/test_envs.py holds a test that fails
|
||||
# if a name is registered while still listed here, so the classification
|
||||
# cannot be skipped silently. That test reads ONLY the delimited block
|
||||
# below/above -- scanning the whole file could never pass, because a
|
||||
# registry entry necessarily contains its own name.
|
||||
# ----------------------------------------------------------------------
|
||||
# --8<-- [end:fork-unaudited-knobs]
|
||||
# The unaudited-knob fence that stood here listed the
|
||||
# tie-determinism toggles as not-yet-in-tree. They landed with the
|
||||
# v2.2 kernel port: the four are registered and
|
||||
# classified in the "determinism toggles" block below, and the
|
||||
# one knob still unclassified sits in that block's own fence, which
|
||||
# tests/test_envs.py reads as a set of identifiers.
|
||||
VLLM_LOG_MODEL_INSPECTION: bool = False
|
||||
VLLM_DEBUG_MFU_METRICS: bool = False
|
||||
VLLM_WEIGHT_OFFLOADING_DISABLE_PIN_MEMORY: bool = False
|
||||
@@ -512,6 +492,112 @@ if TYPE_CHECKING:
|
||||
VLLM_NIC_SELECTION_VARS: str = ""
|
||||
VLLM_ENABLE_HPC_OPS: bool = False
|
||||
MERLIN_TOPK_EXACT_COUNTERS_LOG: int = 0
|
||||
#
|
||||
# --- determinism toggles -----------------------------------------
|
||||
# The three nondeterminism mechanisms (FULL_K, QSA_SORT, TIE_DET/TIE_RECENT).
|
||||
# Every one of them is
|
||||
# OFF by default: promotion is the maintainer's decision.
|
||||
#
|
||||
# Why these are registered at all: validate_environ() only inspects names
|
||||
# beginning with VLLM_, so a MERLIN_ name never trips the unknown-variable
|
||||
# warning. Registration here exists for ONE reason -- compile_factors() can
|
||||
# only hash names it knows, and compile_factors() is a deny-list, so
|
||||
# registering a name hashes it unless ignored_factors says otherwise. All
|
||||
# four are deliberately absent from ignored_factors.
|
||||
#
|
||||
# CAPTURE: yes, all four. Each resolves once per process and the resolved
|
||||
# value then reaches the GPU as a kernel launch argument, a launch
|
||||
# configuration, or an extra op in the traced sequence -- so a cudagraph
|
||||
# bakes in whatever was live at capture time. Flipping one between boots
|
||||
# changes the captured graph, which is exactly what a cache key is for.
|
||||
# AOT: not established for the three CUDA toggles (the Marlin and topk
|
||||
# kernels are compiled into the extension at build time, not by the AOT
|
||||
# compiler); established for MERLIN_QSA_SORT, which adds torch ops to the
|
||||
# traced graph. Hashed conservatively throughout, the same rule
|
||||
# already applied to MERLIN_TOPK_EXACT_MEDIUM.
|
||||
#
|
||||
# NOT A PYTHON READ SITE (FULL_K / TIE_DET / TIE_RECENT): each is resolved
|
||||
# once by a std::getenv in C++ -- merlin_full_k() in
|
||||
# csrc/libtorch_stable/moe/marlin_moe_wna16/ops.cu and merlin_det_ties() /
|
||||
# merlin_tie_recent() in csrc/libtorch_stable/topk.cu -- and that is the
|
||||
# single source of truth. The entries below are raw passthroughs so nothing
|
||||
# can mistake them for a second parser; do not use them to decide
|
||||
# behaviour. A raw passthrough hashes "0" and "00" differently even though
|
||||
# the C++ treats them alike: over-invalidation, which is the safe direction.
|
||||
#
|
||||
# Marlin MoE split-K reduction order; the CUDA half of unmerged upstream
|
||||
# PR #46639 (`use_full_k`).
|
||||
MERLIN_FULL_K: str | None = None
|
||||
# persistent_topk tie-break at the k-th cutoff, and its direction.
|
||||
MERLIN_TIE_DET: str | None = None
|
||||
MERLIN_TIE_RECENT: str | None = None
|
||||
#
|
||||
# A REAL Python read site, unlike the three above: parsed here and read
|
||||
# once at import by models/qwen4_exp/nvidia/ops/qsa_indexer.py, which adds
|
||||
# a torch.sort + two torch.where ops to the QSA selection path.
|
||||
MERLIN_QSA_SORT: bool = False
|
||||
#
|
||||
# --- persistent_topk kernel switches ------------------------------
|
||||
# Same classification as the three raw passthroughs above and registered
|
||||
# for the same single reason: compile_factors() is a deny-list, so a name
|
||||
# it does not know cannot be hashed, and a Ray deployment that propagates
|
||||
# registered variables can otherwise drop an explicitly-disabled switch
|
||||
# and leave a worker on the default. Each is resolved ONCE per process by
|
||||
# a std::getenv in csrc/libtorch_stable/topk.cu -- that is the single
|
||||
# source of truth -- and the resolved value reaches the GPU as a kernel
|
||||
# launch argument, so a cudagraph bakes in whatever was live at capture.
|
||||
# CAPTURE: yes, all three. AOT: not established (these kernels are built
|
||||
# into the extension, not by the AOT compiler); hashed conservatively
|
||||
# anyway, same rule as above. None is in ignored_factors.
|
||||
#
|
||||
# Raw passthroughs, so "unset" reads as None here. That is NOT the same as
|
||||
# "off" for the C++: MERLIN_TOPK_EXACT_MEDIUM defaults ON. Read the getter
|
||||
# in topk.cu, not this default, for the shipped behaviour.
|
||||
#
|
||||
# Take the exact restart on an overflowing MEDIUM row:
|
||||
# MERLIN_TOPK_EXACT_MEDIUM. Declared ONCE, in the cross-change knob
|
||||
# block above (same raw passthrough, same hashing); the 0.30 tree already
|
||||
# carried that entry, so it is not repeated here.
|
||||
# The same restart on an overflowing DECODE row.
|
||||
MERLIN_TOPK_EXACT_DECODE: str | None = None
|
||||
# Deterministic placement and tie membership on the LARGE path.
|
||||
# Registered here ahead of its code, exactly as the four toggles above
|
||||
# were, so the full tree is complete; its read site is
|
||||
# det_large_enabled() in topk.cu on the large path. Setting it without that
|
||||
# code does nothing, which is why it is a passthrough and not a parsed
|
||||
# bool that would imply this tree can act on it.
|
||||
MERLIN_TOPK_DET_LARGE: str | None = None
|
||||
#
|
||||
# NOT REGISTERED, deliberately: MERLIN_TOPK_EXACT_TRAP. It is a
|
||||
# preprocessor define consumed by `#ifndef` in persistent_topk.cuh at
|
||||
# BUILD time (-D), not a process environment variable -- nothing in the
|
||||
# tree calls getenv on it. Registering it would advertise a runtime knob
|
||||
# that does not exist. Its default (trap ON) is an open maintainer decision,
|
||||
# not something to settle by
|
||||
# quietly making it settable.
|
||||
#
|
||||
# --- UNAUDITED: reference-host knobs with no read site in this tree ----------
|
||||
# A knob named inside the fence below is one this repository cannot
|
||||
# classify, because the code that reads it is not in it. Registering one
|
||||
# without first deleting it from the fence -- which means writing down its
|
||||
# AOT and capture identity against its real read site -- is the exact
|
||||
# failure the fork's env rule exists to prevent, and
|
||||
# tests/test_envs.py::test_unaudited_host_side_knob_stays_listed_until_it_is_classified
|
||||
# fails on it.
|
||||
#
|
||||
# The fence holds NOTHING BUT the names, one per line. Prose belongs out
|
||||
# here: the gate reads the fence as a set of identifiers, and a knob name
|
||||
# mentioned in passing inside it would otherwise read as a listing.
|
||||
#
|
||||
# The one entry, MERLIN_PIN_BSM, pins Marlin's block_size_m to 64 (Python,
|
||||
# no rebuild). It is an ALTERNATIVE to the split-K toggle above, not a
|
||||
# complement: on its own it fixed only the 74/102-token cells and left
|
||||
# 1050 upward broken, while use_full_k fixed all of them with block_size_m
|
||||
# still varying. The tie-determinism change promotes use_full_k and keeps this one off, so its
|
||||
# read site was never brought across and it stays unclassified.
|
||||
# --8<-- [start:unaudited-host-side-knobs]
|
||||
# MERLIN_PIN_BSM
|
||||
# --8<-- [end:unaudited-host-side-knobs]
|
||||
|
||||
|
||||
def get_default_cache_root():
|
||||
@@ -2546,6 +2632,19 @@ environment_variables: dict[str, Callable[[], Any]] = {
|
||||
"MERLIN_TOPK_EXACT_COUNTERS_LOG": lambda: int(
|
||||
os.getenv("MERLIN_TOPK_EXACT_COUNTERS_LOG", "0")
|
||||
),
|
||||
# Determinism toggles. See the declaration block above for the AOT /
|
||||
# capture classification and for why three of these are raw passthroughs.
|
||||
# All four are absent from ignored_factors on purpose, so all four are
|
||||
# hashed into the compile cache key.
|
||||
"MERLIN_FULL_K": lambda: os.getenv("MERLIN_FULL_K", None),
|
||||
"MERLIN_TIE_DET": lambda: os.getenv("MERLIN_TIE_DET", None),
|
||||
"MERLIN_TIE_RECENT": lambda: os.getenv("MERLIN_TIE_RECENT", None),
|
||||
"MERLIN_QSA_SORT": lambda: bool(int(os.getenv("MERLIN_QSA_SORT", "0"))),
|
||||
# persistent_topk kernel switches. Raw passthroughs
|
||||
# of values C++ parses; see the declaration block above.
|
||||
# MERLIN_TOPK_EXACT_MEDIUM is registered once, earlier in this dict.
|
||||
"MERLIN_TOPK_EXACT_DECODE": lambda: os.getenv("MERLIN_TOPK_EXACT_DECODE", None),
|
||||
"MERLIN_TOPK_DET_LARGE": lambda: os.getenv("MERLIN_TOPK_DET_LARGE", None),
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -23,6 +23,46 @@ _TOPK_WORKSPACE_BYTES = 1024 * 1024
|
||||
_DECODE_BLOCK_N = 64
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# MERLIN_QSA_SORT: canonicalise the ORDER of the selected block indices.
|
||||
#
|
||||
# persistent_topk assigns output slots by atomicAdd, so two identical requests
|
||||
# get the same selected SET in a different ORDER (measured on the reference host: matching
|
||||
# sorted hashes, differing raw hashes). Sparse attention then accumulates over
|
||||
# those blocks in whatever order it was handed, and fp32 addition is not
|
||||
# associative, so the arithmetic moves. Sorting is semantically a no-op -- the
|
||||
# same blocks are attended either way -- and makes the accumulation order
|
||||
# canonical.
|
||||
#
|
||||
# Padding (negative sentinels) is pushed to the END so the valid prefix is
|
||||
# preserved: `_expand_qsa_indices_kernel` truncates by POSITION
|
||||
# (`complete_blocks = min(visible_blocks, BLOCK_TOPK)`), and position is never
|
||||
# compared against a score anywhere in the chain, so there is no relevance-rank
|
||||
# contract for the sort to break.
|
||||
#
|
||||
# Branch-free and sync-free on purpose. `.any()` / `.item()` raise
|
||||
# cudaErrorStreamCaptureUnsupported inside graph capture; that cost one boot
|
||||
# on the reference host and is why this version has no Python-visible branch on data.
|
||||
#
|
||||
# Resolved once at import so it is a launch constant: a cudagraph captures the
|
||||
# op sequence that was live at capture time. Default OFF -- promotion is a release
|
||||
# decision, which is the maintainer's.
|
||||
# ---------------------------------------------------------------------------
|
||||
_QSA_SORT = envs.MERLIN_QSA_SORT
|
||||
|
||||
|
||||
def _qsa_canonicalise(block_indices: torch.Tensor) -> None:
|
||||
"""Sort selected block indices ascending, padding last. In place, shape preserved."""
|
||||
if not _QSA_SORT:
|
||||
return
|
||||
big = torch.iinfo(block_indices.dtype).max
|
||||
tmp = torch.where(
|
||||
block_indices < 0, torch.full_like(block_indices, big), block_indices
|
||||
)
|
||||
tmp = torch.sort(tmp, dim=-1).values
|
||||
block_indices.copy_(torch.where(tmp == big, torch.full_like(tmp, -1), tmp))
|
||||
|
||||
|
||||
@triton.jit
|
||||
def _qsa_mqa_paged_uniform_kernel(
|
||||
q_ptr,
|
||||
@@ -568,6 +608,7 @@ def _topk(
|
||||
block_topk,
|
||||
logits.shape[1],
|
||||
)
|
||||
_qsa_canonicalise(block_indices)
|
||||
|
||||
|
||||
def qsa_select_paged_decode(
|
||||
|
||||
@@ -17,22 +17,40 @@ 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
|
||||
table keyed by that length:
|
||||
asks the build first -- the tie-determinism change added the `..._slots` op this was written to look
|
||||
for, so on a build that has it the env variable is never consulted -- and
|
||||
otherwise reads `MERLIN_TOPK_COUNTER_SLOTS` (default 5). The op demands
|
||||
EXACTLY that many elements, so a guessed length is an error, not a short read;
|
||||
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
|
||||
11 the ten above, plus decode_overflow
|
||||
|
||||
An unknown length degrades to `slot<i>` names rather than mislabelling.
|
||||
|
||||
WHY 11 AND NOT 10. The tie-determinism change numbered its one new counter around the large-path block
|
||||
instead of after the medium-path one, so the integration renumbers nothing: slots 4-7 are
|
||||
the large-path counters, 8-9 the two switch flags, 10 the decode overflow.
|
||||
On the integrated build every one of the eleven is live; on a single-branch
|
||||
build the other branch's slots are structurally zero and the line still
|
||||
reports them by name -- they are the slots they will hold, not a
|
||||
mislabelling. `decode_overflow` is the number that has to be read before any
|
||||
claim that the DECODE path is deterministic: it counts rows whose buffered
|
||||
candidates exceeded the 3,708-slot capacity before the tie ranking ran, which
|
||||
ranking cannot repair.
|
||||
|
||||
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=...
|
||||
faults=... switch=... [rows_large=... ... decode_overflow=...]
|
||||
|
||||
The first five fields are the medium-path line and keep their order on every build;
|
||||
any further slot is appended after them, so an 11-slot build reads as the medium-path
|
||||
line plus the four large-path fields, `switch_large` and `decode_overflow`.
|
||||
|
||||
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=`
|
||||
@@ -73,7 +91,14 @@ _LARGEDET_NAMES = (
|
||||
"switch",
|
||||
"switch_large",
|
||||
)
|
||||
_SLOT_NAMES: dict[int, tuple[str, ...]] = {5: _MEDIUM_NAMES, 10: _LARGEDET_NAMES}
|
||||
# The decode-overflow counter comes after the large-path block; see the module
|
||||
# docstring for why it is slot 10 and not slot 4.
|
||||
_TIEDET_NAMES = _LARGEDET_NAMES + ("decode_overflow",)
|
||||
_SLOT_NAMES: dict[int, tuple[str, ...]] = {
|
||||
5: _MEDIUM_NAMES,
|
||||
10: _LARGEDET_NAMES,
|
||||
11: _TIEDET_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.
|
||||
@@ -92,7 +117,13 @@ def _get_op(name: str):
|
||||
|
||||
|
||||
def slot_count() -> int:
|
||||
"""How many int64 slots this build's counter array carries."""
|
||||
"""How many int64 slots this build's counter array carries.
|
||||
|
||||
The build is asked first and the environment is only a fallback for builds
|
||||
older than the slot-count op: a guessed length that does not match the extension is
|
||||
rejected by the op's own length check, so the instrument would go dark
|
||||
exactly when the layout changed -- which is when it matters most.
|
||||
"""
|
||||
query = _get_op(_OP_NAME + "_slots")
|
||||
if query is not None:
|
||||
return int(query())
|
||||
|
||||
Reference in New Issue
Block a user