Archived
gemma4: a GPTQ calibration tap and recipe for the 31B's w4a8 container, whose tool calls then close their strings
Rounded to nearest, the 31B's w4a8 trunk leaves streamed tool calls inside their string arguments: after `sku:<|"|>MS-110` it prefers `**,` (58%) to the string's close (41%; bf16 ~1.0), and BetterBench's order request stalled in 10 of 20 tries. This adds what it takes to make the same container (same grid, kernels and size) with the trunk's int4 codes chosen by GPTQ against the bf16 model's own activations. - arch/gemma4: with RADIANCE_CALIB_DIR set, the plugin serving the 31B's bf16 checkpoint sums the Gram of every dense linear's bf16 input over the corpus a server is fed (gram_accum into a PERSIST [K, K] f32 buffer a tap) and writes the files at RADIANCE_CALIB_TOKENS, as arch/gemma3's tap does: q|k|v's and gate|up's input whole from rank 0, o_proj's and down's this rank's columns as `<stem>.part`, which libquant places on the diagonal. The files carry the names the w4a8 container gives the weights (gemma4_bf16's attn_qkv, attn_o, ffn_gate_up, ffn_down), so a GPTQ recipe converting that container reads them with calib=DIR. RADIANCE_G4_CALIB_LAYERS=lo:hi records a slice of layers (the 31B's taps are ~364 MiB a layer a rank at --tp 4, and 60 layers of them do not fit beside the bf16 weights). Under the tap a step runs whole and on one lane; without RADIANCE_CALIB_DIR nothing changes. - data/recipes/gemma4-31b-dense-bf16-w4a8g-df2.recipe: the -df2 recipe with `gptq codes=i4 group=128 scale=f16 rule=search calib=$CALIB` on the trunk's four linears. - docs/GEMMA4-31B-GPTQ.md: the route, the corpus, how to run it, and the measurements. Measured (1.11.0, the same tap and recipe): by GPTQ the close is 99.8% and no try stalls; KL against bf16 on the model's own turns falls from 0.341 to 0.251 (the w8a8 container 0.226), at the same speed. The convert takes 75 minutes with OpenBLAS in the image (#220; an estimated days without). A container made this way is on Hugging Face as Ashlyn4202025/gemma4-31b-w4a8-gptq-dflash2, and with the block-16 drafter as Ashlyn4202025/gemma4-31b-w4a8-gptq-dflash2-b16. Tests: engine_e2e takes the tiny Gemma 4 (shaped like the 31B) through the whole route at --tp 2: the tap's files, by name and width on each rank; rad-convert with the two shipped recipes, every trunk linear fitted and none rounded to nearest; and the GPTQ container's KL against the bf16 model on held-out documents more than three times below the round-to-nearest one's (0.0018 against 0.0105). With rank 0's and rank 1's .part blocks swapped the margin is 2.2 times, which fails. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
66c86b9f89
commit
d8b9ce3a32
7 files changed
+404
-3
No files matched your search
@@ -1170,6 +1170,89 @@ TEST(gemma4_chunking_and_batching_change_no_logit) {
|
||||
{ "--max-num-batched-tokens", "512", "--max-num-seqs", "4" });
|
||||
}
|
||||
|
||||
/* GPTQ for the Gemma 4 31B's w4a8 container, end to end on the tiny Gemma 4 (shaped like the 31B):
|
||||
* arch/gemma4's calibration tap records the trunk's Hessians from the bf16 checkpoint at --tp 2,
|
||||
* where o_proj's and down's inputs are split and each rank writes its block; rad-convert makes the
|
||||
* container with the two shipped recipes, round to nearest and GPTQ; and on documents the Hessians
|
||||
* never saw, the GPTQ one is more than three times closer to the bf16 model (5.8 times here). A
|
||||
* Hessian under another name or of another width is refused, and the weight rounds to nearest,
|
||||
* said on stderr. With the ranks' blocks swapped, the codes fit the wrong inputs and GPTQ is only
|
||||
* 2.2 times closer. */
|
||||
TEST(gemma4_s_calibration_tap_gives_gptq_a_container_closer_to_bf16_than_rounding) {
|
||||
REQUIRE(world().ok);
|
||||
World& w = world();
|
||||
const tiny::Gemma4 s;
|
||||
tiny::TempDir d("e2e_g4calib");
|
||||
const std::string grams = d / "grams";
|
||||
REQUIRE(::mkdir(grams.c_str(), 0755) == 0);
|
||||
const Ran tap = radiance(w.gemma4, { "--tp", "2", "--kld-record", d / "tap", "--kld-corpus",
|
||||
w.corpus, "--max-num-batched-tokens", "512",
|
||||
"--max-num-seqs", "1" },
|
||||
{ "RADIANCE_HOST_DEVICES=2", "RADIANCE_CALIB_DIR=" + grams,
|
||||
"RADIANCE_CALIB_TOKENS=1000" });
|
||||
if (tap.status != 0) fprintf(stderr, " the tap exited %d:\n%s\n", tap.status, tail(tap.log).c_str());
|
||||
REQUIRE_EQ(tap.status, 0);
|
||||
/* Rank 0 writes q|k|v's and gate|up's inputs whole; each rank its half of o_proj's (the heads)
|
||||
* and of down's (the FFN). A global layer's heads are wider. */
|
||||
auto expect = [&](int rank, const std::string& what, int64_t k) {
|
||||
const std::string f = grams + "/gram.r" + std::to_string(rank) + "." + what + ".bin";
|
||||
std::error_code ec;
|
||||
const auto n = std::filesystem::file_size(f, ec);
|
||||
if (ec || (int64_t)n != 32 + k * k * 4)
|
||||
fprintf(stderr, " %s: %s, expected %lld bytes\n", f.c_str(),
|
||||
ec ? "missing" : std::to_string(n).c_str(), (long long)(32 + k * k * 4));
|
||||
CHECK(!ec && (int64_t)n == 32 + k * k * 4);
|
||||
};
|
||||
for (int64_t l = 0; l < s.layers; ++l) {
|
||||
const std::string b = "blk." + std::to_string(l) + ".";
|
||||
const int64_t hd = (l + 1) % s.interval == 0 ? s.global_head_dim : s.head_dim;
|
||||
expect(0, b + "attn_qkv", s.hidden);
|
||||
expect(0, b + "ffn_gate_up", s.hidden);
|
||||
for (int rank : { 0, 1 }) {
|
||||
expect(rank, b + "attn_o.part", s.heads * hd / 2);
|
||||
expect(rank, b + "ffn_down.part", s.ffn / 2);
|
||||
}
|
||||
}
|
||||
|
||||
auto convert = [&](const char* recipe, const std::string& out, std::string* log) {
|
||||
const int st = tiny::run({ RAD_E2E_CONVERT, w.gemma4, "-o", out, "--recipe",
|
||||
std::string(RAD_E2E_RECIPES "/") + recipe, "--set",
|
||||
"quantization_config.quant_method=compressed-tensors",
|
||||
"--kernels", "libref" },
|
||||
engine_env({ "CALIB=" + grams }), log);
|
||||
if (st != 0) fprintf(stderr, " rad-convert exited %d:\n%s\n", st, tail(*log).c_str());
|
||||
return st;
|
||||
};
|
||||
std::string rlog, glog;
|
||||
REQUIRE_EQ(convert("gemma4-31b-dense-bf16-w4a8-df2.recipe", d / "rtn.rad", &rlog), 0);
|
||||
REQUIRE_EQ(convert("gemma4-31b-dense-bf16-w4a8g-df2.recipe", d / "gptq.rad", &glog), 0);
|
||||
/* Every trunk linear of every layer fitted to its Hessian, none rounded to nearest. */
|
||||
size_t fitted = 0;
|
||||
for (size_t at = glog.find("GPTQ quantises"); at != std::string::npos;
|
||||
at = glog.find("GPTQ quantises", at + 1))
|
||||
++fitted;
|
||||
CHECK_EQ(fitted, (size_t)(4 * s.layers));
|
||||
CHECK(glog.find("rounding it to nearest") == std::string::npos);
|
||||
|
||||
std::string ref_log;
|
||||
const std::string ref = w.reference(w.gemma4, w.corpus_short, &ref_log);
|
||||
REQUIRE(!ref.empty());
|
||||
auto kl = [&](const std::string& rad) {
|
||||
tiny::TempDir o("e2e_g4calib_kld");
|
||||
const Ran r = radiance(rad, { "--kld-ref", ref, "--kld-out", o / "kld.json",
|
||||
"--max-num-batched-tokens", "512", "--max-num-seqs", "1" });
|
||||
if (r.status != 0) fprintf(stderr, " child exited %d:\n%s\n", r.status, tail(r.log).c_str());
|
||||
const json rep = json::parse(tiny::read_text(o / "kld.json"), nullptr, false);
|
||||
return rep.is_object() && rep.contains("all") ? rep["all"]["kld"]["mean"].get<double>()
|
||||
: -1.0;
|
||||
};
|
||||
const double rtn = kl(d / "rtn.rad"), gptq = kl(d / "gptq.rad");
|
||||
fprintf(stderr, " mean KL against the bf16 model: round to nearest %.4g, GPTQ %.4g\n", rtn,
|
||||
gptq);
|
||||
REQUIRE(rtn > 0.0 && gptq > 0.0);
|
||||
CHECK(gptq * 3.0 < rtn);
|
||||
}
|
||||
|
||||
/* A container rad-convert made block fp8 (llama_dense_fp8): the fp8 activation quantisers and
|
||||
* GEMMs are per row too, so the invariants hold through them. */
|
||||
TEST(llama_fp8_container_chunking_and_batching_change_no_logit) {
|
||||
|
||||
Reference in new issue
Block a user