Files
leleka/scripts/q8/rebuild_quantizer_ptx.sh
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

40 lines
3.3 KiB
Bash

#!/bin/bash
# Build the spliced PTX of every q8_1-quantizer asset from the modified .cu (docs/upstream-port-plan.md row 1.1).
# Linux, CUDA 13.2.2 at ~/cuda-toolkits/13.2.2, run from the repository root: `bash scripts/q8/rebuild_quantizer_ptx.sh`.
# Output in $W/out (default /tmp/q8new/out); nothing is installed.
set -e
cd "$(git rev-parse --show-toplevel 2>/dev/null || pwd)"
export PATH=$HOME/cuda-toolkits/13.2.2/bin:$PATH
R=$PWD; SRC=crates/cuda-driver/src; W=${W:-/tmp/q8new}; mkdir -p $W
INC="-I$R/engine/strata/include -I$R/engine/strata/third_party/ggml -I$R/$SRC/third_party/llamacpp/ggml-cuda -I$R/$SRC/third_party/llamacpp/ggml-include -I$R/$SRC/third_party/llamacpp/ggml-src"
FAST="-std=c++17 -O3 -arch=sm_120 --use_fast_math"
PREC="--ptx -std=c++17 -O3 -arch=sm_120 --fmad=true --ftz=false"
nvcc --ptx --fmad=true $FAST $INC $SRC/mtp_original_full_head_native.cu -o $W/mtp_original_full_head_native.ptx
nvcc -ptx $FAST $INC $SRC/native_head_native.cu -o $W/native_head_native.ptx
nvcc -ptx $FAST $INC $SRC/native_head_iq.cu -o $W/native_head_iq.ptx
nvcc $PREC $INC $SRC/mtp_original_full_head_iq.cu -o $W/mtp_original_full_head_iq.ptx
nvcc $PREC $INC $SRC/mtp_original_tail.cu -o $W/mtp_original_tail.ptx
nvcc $PREC $INC $SRC/mtp_original_full_checks.cu -o $W/mtp_original_full_checks.ptx
# chain_window.cu no longer compiles against the merged engine headers (FusedGrArgs ABI of 0.1.50): compile the kernel region alone
python3 - <<'PY'
import re
s=open("crates/cuda-driver/src/chain_window.cu").read()
a=s.index("namespace {\nstruct Q81Block")
b=s.index("// `native_scalar_sigmoid(_multi)_kernel`")
pre="#include <cuda_runtime.h>\n#include <stdint.h>\n#include <cuda_fp16.h>\n#include \"strata/kernels/q8_1_finite.hpp\"\nusing strata::kernels::q8_1_ds;\nusing strata::kernels::q8_1_finite;\nusing strata::kernels::q8_1_quant;\n"
open("/tmp/q8new/chain_swiglu_only.cu","w").write(pre+s[a:b])
PY
nvcc -ptx -O3 -arch=sm_120 -std=c++17 --fmad=true --ftz=false $INC $W/chain_swiglu_only.cu -o $W/chain_window_swiglu.ptx
D=$W/out; mkdir -p $D
S="python3 scripts/q8/splice_ptx.py"
$S $SRC/mtp_original_full_head_native.ptx $W/mtp_original_full_head_native.ptx $D/mtp_original_full_head_native.ptx head_quant suffix:native_quantize_q8_1_kernelEPKfPNS1_8Q81BlockEi
$S $SRC/native_head_native.ptx $W/native_head_native.ptx $D/native_head_native.ptx head_quant suffix:native_quantize_q8_1_kernelEPKfPNS1_8Q81BlockEi
$S $SRC/native_head_iq.ptx $W/native_head_iq.ptx $D/native_head_iq.ptx suffix:quantize_q8_1_kernelEPKfP10block_q8_1x
$S $SRC/mtp_original_full_head_iq.ptx $W/mtp_original_full_head_iq.ptx $D/mtp_original_full_head_iq.ptx suffix:quantize_q8_1_kernelEPKfP10block_q8_1x
$S $SRC/mtp_original_tail.ptx $W/mtp_original_tail.ptx $D/mtp_original_tail.ptx mtp_original_quantize
$S $SRC/mtp_original_full_checks.ptx $W/mtp_original_full_checks.ptx $D/mtp_original_full_checks.ptx mtp_original_quantize
$S $SRC/chain_window.ptx $W/chain_window_swiglu.ptx $D/chain_window.ptx chain_swiglu_quant_kernel
# the sealed files end with one NUL (the loaders terminate the text with it); ptxas wants the text without it
for f in $D/*.ptx; do python3 -c "import sys; sys.stdout.buffer.write(open(sys.argv[1], 'rb').read().rstrip(bytes([0])))" $f > $W/check.ptx; ptxas -arch=sm_120 -o /dev/null $W/check.ptx && echo "ptxas ok $(basename $f)"; done
sha256sum $D/*.ptx