libquant: a column-split linear's Hessian from per-rank parts

Under tensor parallelism an attention output or down projection sees only its rank's K / world
columns, so no rank can write the K x K Gram. A tap may write each rank's block as
<stem>.part; with no whole file calib_sum places the parts on the diagonal in rank order (the
cross-rank products, never formed, zero) and converts the result like a whole file. Parts that
do not cover K, or not in domain 0, are reported and the weight rounds to nearest. Tested
byte for byte against the whole block-diagonal file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mici81andClaude Opus 5.5 authored and StillDeadcode committed 2026-10-19 10:41:22 +02:00
1 parent c94a0e6cc2
commit 400bbefa13
4 files changed
+106 -2

No files matched your search

+5 -1
View File
@@ -278,7 +278,11 @@ rad-convert $SRC -o new.rad --recipe new.recipe --reuse old.rad
it in. A container that stores a weight rotated records the rotated input, and a recipe that
rotates the same weight reads it as it is. A weight with no file there is quantised uncalibrated,
and the converter says so; `awq`, `autoround` and `paroquant` use the imatrix's diagonal instead
when `--imatrix` gives one. The three-step calibrated conversion is in [MOE-W4.md](MOE-W4.md).
when `--imatrix` gives one. Under tensor parallelism a column-split linear (an attention output or
down projection) sees only its rank's columns, so a tap may write each rank's Gram as
`gram.r<rank>.<stem>.part.bin`: with no whole file, libquant places the parts on the diagonal in
rank order (the cross-rank products, never formed, left zero). They must cover K and be in the
model's own domain. The three-step calibrated conversion is in [MOE-W4.md](MOE-W4.md).
- `autoround` and `paroquant` are slow. AutoRound's 200 default steps each cost N·K² multiply-adds;
on a 16-core Zen 5 at about 2.5 TFLOP/s that is minutes for a 17408-wide down projection and
most of a working day for a 27B dense model. `iters=50` takes a quarter of the time.
+55
View File
@@ -3,6 +3,7 @@
#include "lq_common.h"
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
@@ -233,6 +234,58 @@ std::string calib_stem(const char* name) {
return s;
}
/* The rank parts of `stem` (see calib_sum), block diagonal into `acc`; the number read. */
int calib_sum_parts(const char* dir, const std::string& stem, int64_t K, int64_t domain,
std::vector<double>* acc) {
std::vector<std::vector<float>> parts;
int64_t total = 0;
for (int r = 0; r < kMaxRanks; ++r) {
const std::string path = gram_path(dir, r, stem + ".part");
FILE* f = std::fopen(path.c_str(), "rb");
if (!f) break;
char hdr[kHdr];
uint32_t kk = 0, dom = 0;
bool ok = std::fread(hdr, 1, kHdr, f) == (size_t)kHdr && std::memcmp(hdr, "RADGRAM2", 8) == 0;
if (ok) { std::memcpy(&kk, hdr + 8, 4); std::memcpy(&dom, hdr + 24, 4); }
ok = ok && dom == 0 && kk > 0 && total + kk <= K;
std::vector<float> h;
if (ok) {
h.resize((size_t)kk * kk);
ok = std::fread(h.data(), 4, h.size(), f) == h.size();
}
std::fclose(f);
if (!ok) {
std::fprintf(stderr, "libquant: %s is not a usable part of the %lld-wide '%s' (a domain-0 "
"RADGRAM2 within K)\n", path.c_str(), (long long)K, stem.c_str());
return 0;
}
total += kk;
parts.push_back(std::move(h));
}
if (parts.empty()) return 0;
if (total != K) {
std::fprintf(stderr, "libquant: the %zu part(s) of '%s' cover %lld of K=%lld columns\n",
parts.size(), stem.c_str(), (long long)total, (long long)K);
return 0;
}
std::vector<double> h((size_t)(K * K), 0.0);
int64_t off = 0;
for (const auto& p : parts) {
const int64_t k = (int64_t)std::llround(std::sqrt((double)p.size()));
for (int64_t i = 0; i < k; ++i)
for (int64_t j = 0; j < k; ++j)
h[(size_t)((off + i) * K + off + j)] = p[(size_t)(i * k + j)];
off += k;
}
if (domain != 0) {
rotate_gram(h, K, domain);
for (double& x : h) x = (double)(float)x;
}
if (acc->empty()) acc->assign(h.size(), 0.0);
for (size_t i = 0; i < h.size(); ++i) (*acc)[i] += h[i];
return (int)parts.size();
}
int calib_sum(const char* dir, const std::string& stem, int64_t K, int64_t domain,
std::vector<double>* acc) {
/* Sum every rank's file. Under expert parallelism a rank's `down` Hessian covers only tokens
@@ -242,6 +295,8 @@ int calib_sum(const char* dir, const std::string& stem, int64_t K, int64_t domai
int found = 0;
for (int r = 0; r < kMaxRanks; ++r)
if (read_gram(gram_path(dir, r, stem), K, domain, acc)) ++found;
if (!found && (domain == 0 || (domain >= 2 && (domain & (domain - 1)) == 0 && K % domain == 0)))
found = calib_sum_parts(dir, stem, K, domain, acc);
return found;
}
+8 -1
View File
@@ -51,7 +51,14 @@ std::string calib_resolve(const char* dir, const char* name, int64_t K);
/* Every rank's file for `stem` in `dir`, each converted into `domain` (0, or the Hadamard width a
* rotated grid quantises in), summed into `acc` (K x K, double, row-major). Returns the number of
* files read, 0 when there are none. A file that exists but is unusable (wrong magic, wrong K,
* short, or a domain that cannot be converted) is reported on stderr and skipped. */
* short, or a domain that cannot be converted) is reported on stderr and skipped.
*
* With no whole file, the parts: under tensor parallelism a column-split linear (an attention
* output or down projection) sees only its rank's K / world input columns, so each rank's tap
* writes the Gram of those as `<stem>.part` (gram.r<rank>.<stem>.part.bin). They are placed on
* the diagonal in rank order, the cross-rank products, never formed on one card, left zero. The
* parts must cover K exactly and be in domain 0 (a rotation of K does not keep the blocks); the
* result is then converted into `domain` like a whole file. */
int calib_sum(const char* dir, const std::string& stem, int64_t K, int64_t domain,
std::vector<double>* acc);
+38
View File
@@ -272,6 +272,44 @@ TEST(an_expert_reads_its_own_hessian_before_its_layers) {
CHECK(same_planes(run(both, e11), run(only_pool, e11)));
}
TEST(a_column_split_hessian_is_assembled_from_its_rank_parts) {
/* Under tensor parallelism a down projection's tap sees one rank's K / 2 input columns, and each
* rank writes the Gram of its own as `<stem>.part`. GPTQ against the two parts must quantise
* exactly as against one whole file holding them block diagonally (the cross-rank products
* zero, never formed). Parts that do not cover K are reported and the weight is rounded to
* nearest, as with no calibration at all. */
NEED_LIBQUANT();
const std::vector<float>& h = H();
const int64_t k2 = kC / 2;
std::vector<float> p0((size_t)(k2 * k2)), p1((size_t)(k2 * k2)), whole((size_t)(kC * kC), 0.0f);
for (int64_t i = 0; i < k2; ++i)
for (int64_t j = 0; j < k2; ++j) {
p0[(size_t)(i * k2 + j)] = h[(size_t)(i * kC + j)];
p1[(size_t)(i * k2 + j)] = h[(size_t)((k2 + i) * kC + k2 + j)];
whole[(size_t)(i * kC + j)] = p0[(size_t)(i * k2 + j)];
whole[(size_t)((k2 + i) * kC + k2 + j)] = p1[(size_t)(i * k2 + j)];
}
const TempDir tp("rad_quant_parts"), tw("rad_quant_whole"), ts("rad_quant_short"),
tn("rad_quant_none");
REQUIRE(!tp.path.empty() && !tw.path.empty() && !ts.path.empty() && !tn.path.empty());
REQUIRE(write_gram(tp.path + "/gram.r0.blk.2.ffn_down.part.bin", k2, kT, p0));
REQUIRE(write_gram(tp.path + "/gram.r1.blk.2.ffn_down.part.bin", k2, kT, p1));
REQUIRE(write_gram(tw.path + "/gram.r0.blk.2.ffn_down.bin", kC, kT, whole));
REQUIRE(write_gram(ts.path + "/gram.r0.blk.2.ffn_down.part.bin", k2, kT, p0));
auto run = [&](const std::string& dir) -> Quantised {
Opts o;
o.s("codes", "i4").i("group", 32).s("scale", "f16").s("calib", dir.c_str());
Quantised q = quantise(quantizer("gptq"), o, "blk.2.ffn_down.weight", kR, kC, weight());
CHECK_EQ(q.status, RAD_OK);
return q;
};
const Quantised parts = run(tp.path), one = run(tw.path), short_ = run(ts.path), none = run(tn.path);
CHECK(same_planes(parts, one));
CHECK(!same_planes(parts, none));
CHECK(same_planes(short_, none));
}
TEST(awq_scales_the_columns_the_input_says_matter) {
NEED_LIBQUANT();
Opts g, x;