Add Spark rotated-int8 inference and calibration

Serve the Spark X2.6 trunk on IQ8R's rotated int8 grid. Six projection types a layer carry
fwht128-rotated int8 weights with a bf16 scale per 128 columns; activations are quantised into
the same 128-column groups and feed the `i8a8h` rows, exact GELU runs before the down
projection's own Hadamard quantiser, and the embedding, the two norms a layer, the head's norm
and the 16-row attention gate stay as they are. The descriptor is `w8a8_fwht128`, and the
container says `quantization_config.quant_method=w8a8`, `fmt=fwht128`.

`data/recipes/spark-x26-4b-iq8r.recipe` fits those weights by GPTQ against the model's own
calibration Hessians, which the BF16 plugin writes as RADGRAM2 files under `RADIANCE_CALIB_DIR`;
each layer's f32 [K,K] accumulators are 533 MB, so `RADIANCE_SPARK_CALIB_LAYERS=lo:hi` calibrates
a slice at a time into one directory. The output head is RTN. Column-sharded inputs and their
offsets must land on whole scale groups, so a rank with an unaligned span is refused rather than
served with misaligned scales: TP 1, 2, 4 and 8 align for these shapes, TP 3 does not for the
FFN-down shard. Calibration asks for TP=1, because the off-diagonal cross-rank terms only exist
on one card.

`tests/spark25_iq8r.py` compares a deterministic quantised fixture against the Transformers
reference, top-10 probabilities included, across cached decoding; `--tp 2` checks two fake host
ranks and `--wide-tp --tp N` the group-aligned attention and FFN shards for N from 1 to 8. Run it
under a GPU-denying device cgroup, as with the BF16 fixture.

Against this checkpoint's own BF16 container, 32 held-out chat, math, code and tool-call samples
and 31,391 positions, greedy with prefix caching off: 98.60% of positions keep their top token,
mean KL 0.00408, median 6.5e-5, p99 0.0496, p99.9 0.348, worst 11.88. The tail is the honest
part -- 13 positions above one nat -- and leaving the first twelve layers in BF16 takes the worst
to 0.247 with nothing above one nat, for about 12% of prefill and 17% of decode. One R9700, zero
cached prompt tokens: 11.8k, 11.8k and 11.3k tok/s of prefill at 1.1k, 4.4k and 9.9k-token
prompts, 93.7 tok/s of decode from a short prompt and 88.9 with a 4.4k-token context. 8.29 GiB of
BF16 weights become 4.52 GiB.

That comparison is distribution agreement against BF16, not a task benchmark, and the corpus the
Grams came from is public text held disjoint from the holdout by hash; no MMLU or GSM8K-class
number is claimed here. The numbers need the prefill softmax scaling per row rather than per
wave: while a row's rounding depended on its wave-mates, the quantised container was worse off
against BF16 than its own error explains.
This commit is contained in:
Constantin committed 2026-11-03 13:53:26 -05:00
1 parent 5d6de60318
commit cb97b1c886
9 files changed
+974 -44

No files matched your search

+76
View File
@@ -3,6 +3,7 @@
#include "arch_fakes.h"
#include "rad_plugin.h"
#include "../arch/spark25_bf16/spark25_bf16.cpp"
#include "../arch/spark25_w8a8/spark25_w8a8.cpp"
using namespace rad_fake;
@@ -152,6 +153,81 @@ TEST(tensor_parallel_covers_heads_and_ffn_without_changing_gqa) {
}
}
static void spark_iq8r_encodings(RadBuilder& b, bool rotated = true) {
RadEncoding e = rad_enc_affine(RAD_I8, RAD_BF16, 1, 128);
if (rotated) rad_enc_copy_str(e.transform, "fwht128");
for (const char* name : {"attn_q.weight", "attn_k.weight", "attn_v.weight",
"attn_output.weight", "ffn_gate_up.weight", "ffn_down.weight"})
b.encs.emplace_back(name, e);
b.encs.emplace_back("output.weight", rad_enc_affine(RAD_I8, RAD_BF16, 1, 128));
}
TEST(iq8r_graph_uses_rotated_codes_and_is_liveness_safe) {
for (int world = 1; world <= 8; ++world) {
for (int rank = 0; rank < world; ++rank) {
MetaKV k = spark_meta();
k.m.quant = "w8a8_fwht128";
k.set("quantization_config.quant_method", "w8a8")
.set("quantization_config.fmt", "fwht128");
RadBuilder b;
spark_iq8r_encodings(b);
RadBuildCtx ctx = build_ctx();
ctx.world_size = world;
ctx.rank = rank;
REQUIRE_EQ(spark25_w8a8::declare(&b, k.meta(), &ctx), RAD_OK);
int quantized = 0;
for (const auto& op : b.ops) if (op.op == "gemm_nt_q") {
CHECK_EQ(pstr(&op, "dtype"), std::string("i8a8h"));
++quantized;
}
CHECK_EQ(quantized, 24); /* six linears x four layers */
CHECK_EQ(count_op(b, "all_reduce"), world == 1 ? 0 : 9);
for (int n : {1, 8}) {
RadBatch batch = decode_batch();
batch.n_tok = batch.max_q_len = n;
RadCtx c{.b = &b, .batch = &batch, .rank = rank, .world = world};
spark25_w8a8::step(&c, &batch);
CHECK_LINT(b, &c);
CHECK(c.step_fail.empty());
}
}
}
}
TEST(iq8r_rejects_unrotated_weights_and_missing_collectives) {
MetaKV k = spark_meta();
k.m.quant = "w8a8_fwht128";
k.set("quantization_config.quant_method", "w8a8")
.set("quantization_config.fmt", "fwht128");
RadBuildCtx ctx = build_ctx();
RadBuilder unrotated;
spark_iq8r_encodings(unrotated, false);
CHECK_EQ(stderr_of_rc([&] { return spark25_w8a8::declare(&unrotated, k.meta(), &ctx); }),
RAD_E_DTYPE);
RadBuilder missing;
spark_iq8r_encodings(missing);
missing.refuse.insert("all_reduce");
ctx.world_size = 2;
CHECK_EQ(stderr_of_rc([&] { return spark25_w8a8::declare(&missing, k.meta(), &ctx); }),
RAD_E_UNSUPPORTED);
}
TEST(tp_rejects_missing_all_reduce_instead_of_skipping_it) {
MetaKV k = spark_meta();
RadBuilder b;
b.refuse.insert("all_reduce");
RadBuildCtx ctx = build_ctx();
ctx.world_size = 2;
CHECK_EQ(stderr_of_rc([&] { return spark25_bf16::declare(&b, k.meta(), &ctx); }),
RAD_E_UNSUPPORTED);
/* No collective is required for single-device inference. */
RadBuilder single;
single.refuse.insert("all_reduce");
ctx.world_size = 1;
CHECK_EQ(spark25_bf16::declare(&single, k.meta(), &ctx), RAD_OK);
}
TEST(tp_projection_rounds_to_bf16_before_the_fp32_residual_add) {
MetaKV k = spark_meta();
RadBuilder b;