[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:
2026-10-09 19:56:00 +07:00
co-authored by Hinata
parent 512ca2fc0b
commit 2918e3fda1
10 changed files with 753 additions and 119 deletions
+125 -26
View File
@@ -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),
}