[TopK] Follow-up fixes to the persistent top-k kernels

- Counter enum declared above the decode template (two-phase lookup).
- Emission-fault flag set atomically.
- Close the legacy large tie loop with a block barrier (inherited).
- Large-path fault injectors registered as hashed passthroughs.
- The tensor-free counter-slots op registered for CompositeExplicitAutograd.
- Tie measurements recorded only on rows neither fault verdict failed.
- Tests scoped to the properties the build promises.

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 2918e3fda1
commit cff46af086
4 changed files with 215 additions and 70 deletions
+37
View File
@@ -568,6 +568,35 @@ if TYPE_CHECKING:
# bool that would imply this tree can act on it.
MERLIN_TOPK_DET_LARGE: str | None = None
#
# The large-path FAULT INJECTORS. TEST
# ONLY, and registered for exactly the same reason as the switches above,
# not because anything should set them in production:
#
# * both are resolved once per process by a std::getenv in topk.cu
# (large_fault_inject_cta(), large_fault_inject_emit()) and the
# resolved values reach the GPU as kernel launch arguments, so a
# cudagraph BAKES THEM IN. An injected build and a clean build differ
# in the captured graph while sharing a cache key -- which is the
# failure mode: a graph captured under injection returns an all -1 row
# for every deterministic large row, and on replay nothing re-reads the
# environment to notice the variable has since been unset. FAULT_INJECT
# was once "unregistered and capture-baked"; hashing
# them is what stops a cached artifact crossing between the arms.
# * compile_factors() is a deny-list, so a name it does not know cannot
# be hashed at all. Neither is in ignored_factors.
#
# Raw passthroughs, like the switches: the C++ getters are the single
# source of truth (INJECT parses with atoi and treats <0 and unset alike;
# KIND compares against the literal "emit"). Do not parse them here.
#
# Which CTA of the group has its fault predicate forced; unset / negative
# is off, and setting it also suppresses __trap().
MERLIN_TOPK_LARGE_FAULT_INJECT: str | None = None
# Which fault: unset / "pre" forces the pre-emission predicate caught at
# X3, "emit" forces the emission-bound slot check caught at X4. Ignored
# unless MERLIN_TOPK_LARGE_FAULT_INJECT names a CTA.
MERLIN_TOPK_LARGE_FAULT_KIND: 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
@@ -2645,6 +2674,14 @@ environment_variables: dict[str, Callable[[], Any]] = {
# 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),
# Large-path fault injectors. TEST ONLY, hashed for
# the reason in the declaration block: both are capture-baked.
"MERLIN_TOPK_LARGE_FAULT_INJECT": lambda: os.getenv(
"MERLIN_TOPK_LARGE_FAULT_INJECT", None
),
"MERLIN_TOPK_LARGE_FAULT_KIND": lambda: os.getenv(
"MERLIN_TOPK_LARGE_FAULT_KIND", None
),
}