Files
leleka/scripts/q8/repin.py
T
Pavlo RatushnyiandClaude Sonnet 5.5 4654f01b15 q8_1 scale and sum stay finite in the sealed quantizers (plan 1.1; upstream 2b726ba6, f6601bf8, 7a0e3023, e372d4ff)
A q8_1 block stores d = amax / 127 and the sum of its 32 activations as fp16: one massive activation (amax > 8.3 M, or a sum past 65504)
rounded to inf and the dot products then read inf * 0 = NaN (NonFiniteMmqOutput here, "!!!!" upstream). The quantizers now clamp both
to the largest finite half and keep the codes inside int8 (strata/kernels/q8_1_finite.hpp); a NaN input still gives a NaN sum.
Finite blocks are stored bit for bit as before.

Sites: 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: the routed experts' input and SwiGLU output) and the opt-in chain
swiglu+quantize (chain_window). Not taken: the MMQ prompt quantizers (ggml quantize_mmq_q8_1, mtp_prefill_q8), which upstream did not clamp.

The sealed PTX are patched entry by entry (scripts/q8): three were built with CUDA 13.0.88, which is not installed, and a recompile
renames the hashed anonymous-namespace entries; only the quantizer entries change, every other byte is the sealed one. Pins, the seal
plan digests and the sm_86 companions (sm86_assets.py build) follow. tools/q8-probe compares old and new PTX on the GPU.

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

67 lines
2.8 KiB
Python

#!/usr/bin/env python3
"""Move every Rust pin and doc digest of a re-sealed PTX from its old body to its new one (docs/upstream-port-plan.md row 1.1).
repin.py OLD_DIR NEW_DIR NAME [NAME ...] OLD_DIR/NAME.ptx = the sealed file before, NEW_DIR/NAME.ptx = after
The pin of a sealed file is the SHA-256 of its body (the file without its terminal NUL, or the whole file when it has none), written as
a decimal byte array in Rust (`sha: [..]`, `Some((len, [..]))` with len = body + 1 for the NUL the loader appends) and as lowercase hex in
the seal-plan JSON. Run from the repository root; prints what it changed and fails if a pin of the old body is not found anywhere
(the assets without a Rust pin say so explicitly: the MTP tail/checks files are guarded by the UNSEALED marker only).
"""
import glob
import hashlib
import re
import sys
NO_RUST_PIN = {'mtp_original_tail', 'mtp_original_full_checks'}
def body(path):
b = open(path, 'rb').read()
return b.rstrip(b'\0') if b.endswith(b'\0') else b
def arr(digest, sep=', '):
return sep.join(str(x) for x in digest)
def main(old_dir, new_dir, names):
rs = {f: open(f, encoding='utf-8', newline='').read() for f in glob.glob('crates/**/*.rs', recursive=True)}
docs = {f: open(f, encoding='utf-8', newline='').read() for f in ['docs/cuda-seal-plan-additions-window.json']}
changed = set()
for name in names:
ob, nb = body(f'{old_dir}/{name}.ptx'), body(f'{new_dir}/{name}.ptx')
od, nd = hashlib.sha256(ob).digest(), hashlib.sha256(nb).digest()
found = 0
for f, s in rs.items():
if f.endswith('sm86_table.rs'):
continue # regenerated by sm86_assets.py build
for sep in (', ', ','):
if arr(od, sep) in s:
# `(len, [..])` pins carry body + 1
pat = re.compile(r'\((\d+), \[' + re.escape(arr(od, sep)) + r'\]\)')
s = pat.sub(lambda m: f'({len(nb) + 1}, [{arr(nd, sep)}])', s)
s = s.replace(arr(od, sep), arr(nd, sep))
rs[f] = s
changed.add(f)
found += 1
for f, s in docs.items():
if od.hex() in s:
docs[f] = s.replace(od.hex(), nd.hex())
changed.add(f)
found += 1
print(f'{name}: {od.hex()[:12]} -> {nd.hex()[:12]}, {found} place(s)')
if found == 0 and name not in NO_RUST_PIN:
raise SystemExit(f'{name}: no pin of the old body found')
for f in changed:
text = rs.get(f, docs.get(f))
open(f, 'w', encoding='utf-8', newline='').write(text)
print('rewrote', ', '.join(sorted(changed)))
if __name__ == '__main__':
if len(sys.argv) < 4:
print(__doc__)
sys.exit(2)
main(sys.argv[1], sys.argv[2], sys.argv[3:])