Files
leleka/docs/upstream-port-numerics.md
T
Pavlo RatushnyiandClaude Sonnet 5.5 a75d3902ed NaN guard heals on the live stack: take the refused window back before the retry (plan 1.2)
The retry of a refused verify window failed on the RTX 5090 with Failed(InvalidDimension): a window that completed and was never
committed keeps the driver verifier's window_ready flag (run refuses the next window with InvalidQwenShape) and its armed QSA journal.
A rerun that only cleared that completed but differed from the refused window, deterministically: the window also advances the PLE
history row by row (only the commit graph sets it) and overwrites the index tails and the spare pooled row.

OriginalSpecEngine::discard_window (port, OriginalVerifier::discard_window, OriginalVerifyQsaJournal::discard_window) runs after the
two checkpoint restores; the journal writes back the index state and the PLE history it read when the window began
(set_window_snapshots, only when the guard has retries). The fake engine refuses a window while a completed one is outstanding, so
the retry tests fail without the take-back. Live, guard on, deterministic arm: G1/G2 equal G0 in all 546 window blocks and 711 forced
rows, every rerun bit-equal to the refused window; default tier: all runs rc=0. Default stays off.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-27 01:40:17 +01:00

73 lines
6.7 KiB
Markdown

# Numerical robustness ported from Strata 41cf05a0 (plan rows 1.1 and 1.2)
Evidence tables are in `docs/upstream-port-plan.md`; this page is the how-to.
## 1.1 q8_1 scale and sum stay finite
A q8_1 block stores `d = amax / 127` and the sum of its 32 activations as fp16. One massive activation (amax above 8.3 million, or a block
sum past 65504) rounds to inf, the dot products then read `inf * 0 = NaN`, and the window ends in `NonFiniteMmqOutput` (upstream: one token
repeated, issue #606). The quantizers now use `strata/kernels/q8_1_finite.hpp` (read from `engine/strata/include`, not modified): `d` and the
sum are clamped to 65504, the codes to +-127, a NaN input keeps a NaN sum. A block that was finite before is stored bit for bit as before.
Sites that carry it (production decode path): `head_quant` (`mtp_original_full_head_native`, `native_head_native`), `mtp_original_quantize`
(`mtp_original_tail`, `mtp_original_full_checks`), `quantize_q8_1_kernel` (`mtp_original_full_head_iq`, `native_head_iq`: input and SwiGLU
output of the routed experts), `chain_swiglu_quant_kernel` (`chain_window`, `LELEKA_CHAIN`). Not changed: the MMQ prompt quantizers
(ggml `quantize_mmq_q8_1`, `mtp_prefill_q8`: upstream did not clamp them) and the Q8_0 quantizers of the CPU-consistent expert path.
### Why the PTX is patched entry by entry
`mtp_original_{tail,full_checks,full_head_iq}.ptx` were built with CUDA 13.0.88, which is not installed anywhere we run; the others with
13.2.86. A recompile renames every hashed anonymous-namespace entry (`_GLOBAL__N__<hash of the compile command>`, which the Rust window code
looks up) and may reorder unrelated kernels. So only the quantizer entries are replaced:
```
bash scripts/q8/rebuild_quantizer_ptx.sh # nvcc 13.2.86: compile the modified .cu, splice the quantizer entries into the sealed files (scripts/q8/splice_ptx.py)
python3 scripts/q8/repin.py OLD_DIR NEW_DIR NAMES # move the Rust pins and the seal-plan digests to the new bodies
python3 scripts/gpu-bringup/sm86_assets.py build --toolkit ~/cuda-toolkits/13.2.2 --work /tmp/sm86w # companions + MANIFEST + sm86_table.rs
bash scripts/q8/probe_all.sh OLD_DIR NEW_DIR # the identity probe on the GPU (tools/q8-probe/q8_probe.cpp)
```
The recipe was checked on the entries that were built with 13.2.86: recompiling the unchanged source gives a `head_quant` entry text identical
to the sealed one. `mtp_original_tail.ptx` and `mtp_original_full_checks.ptx` have no Rust sha pin (only the `UNSEALED` marker is refused).
## 1.2 NaN guard with self-healing
`OriginalSpecController::verify_commit` runs a window from checkpoints it takes first. A verify window whose logits or stage outputs are not
finite (`Error::NonFinite`: `NonFiniteMmqOutput` from the window's finite guards, STRA-235) used to abort: both checkpoints restored, the
request fails. With the guard on, the same restore is followed by running the round again, up to `retries` times (default 2); only
`NonFinite` is retried, a restore that fails ends the healing with the original error, and a window that stays non-finite fails the request
as before. Nothing of a window is committed or emitted before it passed, so a healed round is indistinguishable from a clean one.
A window that completed on the device and was refused afterwards (the injection, or an admission refusal) leaves the driver's verifier
with a ready, uncommitted window: its `run` refuses the next window (`InvalidQwenShape`, which the serve error map calls `InvalidDimension`,
the failure of the first live attempt, `Failed(InvalidDimension)` at the second verify), and its QSA journal stays armed with the index state
the window changed. After both checkpoints are restored the controller therefore calls `OriginalSpecEngine::discard_window` (port
`discard_window`, driver `OriginalVerifier::discard_window`): `window_ready` is cleared and the QSA journal writes back what the window began
with and the accepted-prefix graph would not put back, the four pieces of an index checkpoint (tail, `dead`, the pooled row of the block in
progress, the block position) and the PLE history (every window row advances it, `ple_history_advance`; only the commit graph sets it from
the kept row's snapshot), un-arms itself and marks the indices published again. That state is read from the device when a window begins
(`OriginalVerifyQsaJournal::set_window_snapshots`, switched on by the stack only when the guard has retries: twelve small reads and one
360 KB read per window, nothing when the guard is off). Restoring the index state alone was not enough: the rerun of window 122 completed
but differed from the refused window (`-0.000392` against `-0.000368` on the first row, a different value for every further rerun, the same
values in four processes), which pointed to state the window leaves behind; the PLE history was it, and with it restored the rerun is bit-equal. The GDN canonical state and the PLE history are not written before a commit, and the K/V cells and pooled
keys the window wrote above the block in progress lie beyond every coverage, as after a rejected suffix, so the rerun at the same position
rewrites them before it reads them. A discard that fails leaves the verifier failed and the controller failed with the original `NonFinite`.
Not healed: a non-finite value that the driver detects inside `run` (`NonFiniteMmqOutput` from the window's stage checks or the logits scan)
aborts the window with `abort_gpu_waits`, which releases the verifier for good; the retry's discard then fails and the request ends with the
original `NonFinite` as before (without a second attempt on a released verifier). Healing those needs a soft path in `run` after the drain,
which was not built and cannot be provoked on the live stack.
Upstream (`STRATA_NAN_GUARD`, #879) ends the request and has its gateway repeat it from token 0; here the window is repeated, so a request
keeps its place. Not covered: non-finite output in a prefill chunk (the deferred prompt guards poison the CUDA context by design, so there is
nothing to retry on) and a failure after the commit was enqueued.
| switch | meaning |
| --- | --- |
| `LELEKA_ORIGINAL_SERVE_NAN_GUARD=on\|off` | the guard (a profile `engine_env` key); unset = `NanGuard::DEFAULT_ON` |
| `LELEKA_ORIGINAL_SERVE_NAN_GUARD_RETRIES=N` | 0..=8, default 2 |
| `LELEKA_ORIGINAL_SERVE_NAN_INJECT=n[xK]` | debug (upstream `STRATA_NAN_INJECT`): the n-th checked window of the process reads as non-finite K times (default 1) after it ran well, so the path is exercised on a real window |
Every healed or unhealed window is written to stderr (`leleka-serve nan-guard: ...`), counted in `SpecStats` (`healed=` in `DBG spec-stats`) and
published in the telemetry document: `nan_guard.healed_windows`, `nan_guard.unhealed_windows`, `nan_guard.last`.