[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:
@@ -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