[Guards] Repair: carry patch 0027's slot encode/decode helpers

Landed 0027 with mamba_utils.py and guard_telemetry.py under the
ours-newer rule; that rule saw the slot-ownership check and the
diagnostic gate, not the bias-encoding helpers. decode_slot(),
encode_slot(), GUARD_MAX_ENCODABLE and GUARD_MIN_ENCODABLE are new
names, present in v2.5.1 and absent here, and patch 0049's ring-row
report calls decode_slot(). Replays 0027's guard_telemetry.py section
(3 hunks, patch -F 8) onto the current tree.

Co-Authored-By: Hinata <hinata@idihore.id>
Signed-off-by: MOVZX <movzx@yahoo.com>
This commit is contained in:
2026-10-09 19:58:32 +07:00
co-authored by Hinata
parent dec8be19de
commit ad80cac06d
+74 -2
View File
@@ -78,6 +78,59 @@ GUARD_BIAS = 1 << 31
# One (n_masked, row, value) triple per guarded kernel.
SLOTS_PER_SITE = 3
# Largest value the bias encoding can carry without overflowing the int64 slot.
GUARD_MAX_ENCODABLE = (1 << 63) - 1 - GUARD_BIAS
# Smallest one. At exactly -GUARD_BIAS the encoded word is 0, which the decode
# cannot tell from "nothing recorded"; below it the encoded word is negative
# and tl.atomic_max against a zeroed buffer never takes it. Both cases lose the
# VALUE and neither loses the COUNT -- see decode_slot.
GUARD_MIN_ENCODABLE = -GUARD_BIAS + 1
def encode_slot(value: int) -> int:
"""Producer side of the bias encoding, in Python (for the limits tests).
The guarded kernels inline `(value + _GUARD_BIAS).to(tl.int64)` and store
it with `tl.atomic_max`; this is the same arithmetic, exposed so the
encoding's limits can be tested without Triton and without a GPU. It is
NOT called by the kernels -- a Python function is not readable from a
@triton.jit body, which is why _GUARD_BIAS is a tl.constexpr there.
"""
return int(value) + GUARD_BIAS
def decode_slot(biased: int) -> int | None:
"""Reader side. ``None`` means "this slot recorded nothing".
THE ENCODING'S LIMITS (closed here by test
rather than by change). A slot is an int64 zeroed at allocation and only
ever written with `tl.atomic_max`, so the recorded value must be biased to
survive negatives, and 0 has to mean "unwritten". Three classes of extreme
value are therefore not recoverable from the slot:
* ``value == -GUARD_BIAS`` (INT32_MIN) encodes to exactly 0 and is
FALSIFIED: it reads back as "nothing recorded".
* ``value < -GUARD_BIAS`` encodes negative, `atomic_max` against 0 keeps
0, and the value is LOST the same way.
* ``value > GUARD_MAX_ENCODABLE`` overflows the int64 slot.
In every one of those cases the COUNT slot, which carries no bias and is
written with `atomic_add`, still advances.
BUT `None` IS NOT A DIAGNOSIS OF "UNENCODABLE". The reader has
NO ordering dependency on the producer -- it copies through a pinned mirror
on a side stream with no dependency on the compute stream -- so a count
that has become visible with its maxima not yet visible produces exactly
the same reading. `_poll_once`'s one-tick confirm delay makes that less
likely; it does not make it impossible, and a delay is not a happens-before
relationship. So `None` means UNOBSERVED OR UNENCODABLE.
What it never means is "this field recorded nothing while the count rose".
Anything reading these slots must say so, and must not build a fatal
decision on the maxima -- those decisions are count-based.
"""
return None if biased == 0 else int(biased) - GUARD_BIAS
SITE_MAMBA_COPY = 0
SITE_GDN_DECODE_STATE = 1
SITE_QSA_PRE_INDEXER = 2
@@ -424,8 +477,8 @@ def _report_rows(device: torch.device, rows: list[list[int]]) -> None:
if n_masked <= _seen.get((device, site), 0):
continue
_seen[(device, site)] = n_masked
row = biased_row - GUARD_BIAS if biased_row else None
val = biased_val - GUARD_BIAS if biased_val else None
row = decode_slot(biased_row)
val = decode_slot(biased_val)
msg = (
f"bounds guard masked {n_masked} out-of-range access(es) "
f"in {SITE_NAMES.get(site, site)} on {device} "
@@ -434,6 +487,25 @@ def _report_rows(device: torch.device, rows: list[list[int]]) -> None:
f"access was suppressed, so this is a MASKED FAULT and not a "
f"correct result -- the metadata is wrong upstream."
)
if row is None or val is None:
# Corrected wording. A None field has TWO
# possible causes and this reader cannot distinguish them, because
# it has no happens-before relationship with the producer:
# * UNOBSERVED -- the kernel incremented the count and its
# atomic_max on this slot is not yet visible to this copy;
# * UNENCODABLE -- the value was == INT32_MIN, below it, or past
# GUARD_MAX_ENCODABLE, so the bias encoding could not carry it.
# Either way it does NOT mean the field recorded nothing. The
# earlier wording claimed "unencodable" outright, which the
# one-tick confirm delay does not establish.
msg += (
" A None field is UNOBSERVED OR UNENCODABLE, not absent: "
"either this copy predates the slot's atomic_max becoming "
"visible, or the value was == INT32_MIN, below it, or past "
f"{GUARD_MAX_ENCODABLE}. The reader has no ordering "
"dependency on the producer, so it cannot tell which; see "
"decode_slot."
)
logger.warning("%s", msg)
if _MODE == "raise" or site in _ALWAYS_FATAL_SITES:
_arm(msg)