Archived
radiance 1.7.0
- every model serves without a GPU: libavx covers every op, so `--kernels libavx` alone serves every shipped container - Qwen3-ASR (zivius): speech to text on /v1/audio/transcriptions and live over /v1/realtime - /v1/messages works with Claude Code as it ships (gearwave00001) - a long agent conversation keeps its cached history when the client rewrites its last turn (manueloverride) - a stream's first event goes out with the first token, so llama-benchy measures prefill right - opt-in --prefill-interleave (Smellyyyy, baKon) and --prefill-span (keptn42) - four-rank TP releases the link early; four-rank selftests and docs/TP4.md (goodman-b) - NVFP4 and FP8 compressed-tensors imports (devmilan); tools/rad-export for finetunes and LoRA adapters (baKon) - many fixes, among them lost tokens on stop strings under concurrency, keep-alive, MiniCPM5 tool arguments and the CPU's vision and audio encoders - plugin ABI 17 and changed op schemas: out-of-tree plugins must be rebuilt
This commit is contained in:
1 parent
b068190326
commit
1e968f6e1d
739 files changed
+128447
-84632
No files matched your search
+121
-56
@@ -1,13 +1,12 @@
|
||||
/* quant_algo_test.cpp -- libquant's calibrated algorithms: GPTQ's act order, AWQ, AutoRound and
|
||||
* ParoQuant, each against round-to-nearest on the same grid.
|
||||
/* quant_algo_test.cpp -- libquant's calibrated algorithms (GPTQ act order, AWQ, AutoRound,
|
||||
* ParoQuant), each against round-to-nearest on the same grid.
|
||||
*
|
||||
* WHAT THEY ARE HELD TO is the number every one of them minimises: the layer's output error, the
|
||||
* Hessian's quadratic form sum_r (w_hat_r - w_r) H (w_hat_r - w_r)^T, with H the Gram of a
|
||||
* synthetic input whose channels are as uneven as a real layer's -- a few carry most of the
|
||||
* energy, and neighbours are correlated. The decoded weight is read back through the core's
|
||||
* decoder, so a transform or a column scale the quantiser wrote and the reader did not undo is an
|
||||
* error of order one, not a near miss. Each is also run twice at two thread counts and must give
|
||||
* the same bytes: its output is a file.
|
||||
* The metric is what they all minimise: the layer's output error, the quadratic form
|
||||
* sum_r (w_hat_r - w_r) H (w_hat_r - w_r)^T, where H is the Gram of a synthetic input whose
|
||||
* channels are as uneven as a real layer's (a few carry most of the energy; neighbours
|
||||
* correlate). The weight is decoded through the core's decoder, so a transform or column scale
|
||||
* the reader fails to undo shows up as an error of order one. Each algorithm also runs at one and
|
||||
* four threads and must produce the same bytes, since its output is a file.
|
||||
*/
|
||||
#include "quant_fixture.h"
|
||||
|
||||
@@ -53,17 +52,27 @@ const std::vector<float>& H() {
|
||||
return h;
|
||||
}
|
||||
|
||||
/* A calibration directory holding the Gram for `stem`, made once. */
|
||||
/* A calibration directory holding kName's Gram, made once and removed at exit. */
|
||||
const std::string& calib_dir() {
|
||||
static std::string d;
|
||||
if (d.empty()) {
|
||||
char tmpl[] = "/tmp/rad_quant_algo_XXXXXX";
|
||||
if (mkdtemp(tmpl)) {
|
||||
d = tmpl;
|
||||
write_gram(d + "/gram.r0.blk.2.attn_q.bin", kC, kT, H());
|
||||
static const struct Dir {
|
||||
TempDir t{ "rad_quant_algo" };
|
||||
std::string d, gram;
|
||||
Dir() {
|
||||
if (t.path.empty()) return;
|
||||
d = t.path;
|
||||
gram = d + "/gram.r0.blk.2.attn_q.bin";
|
||||
write_gram(gram, kC, kT, H());
|
||||
}
|
||||
}
|
||||
return d;
|
||||
} dir;
|
||||
return dir.d;
|
||||
}
|
||||
|
||||
/* Byte-identical results: as many planes, each equal. */
|
||||
bool same_planes(const Quantised& a, const Quantised& b) {
|
||||
if (a.enc.n_planes != b.enc.n_planes) return false;
|
||||
for (int i = 0; i < a.enc.n_planes; ++i)
|
||||
if (a.planes[(size_t)i] != b.planes[(size_t)i]) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
double out_err(const std::vector<float>& w, const std::vector<float>& d) {
|
||||
@@ -115,9 +124,7 @@ void beats_rtn(const char* algo, Opts grid, Opts extra, double margin, const cha
|
||||
REQUIRE_EQ(a.status, RAD_OK);
|
||||
REQUIRE_EQ(b.status, RAD_OK);
|
||||
if (want_enc) CHECK_EQ(enc_name(a.enc), std::string(want_enc));
|
||||
int same = 1;
|
||||
for (int i = 0; i < a.enc.n_planes; ++i) same &= a.planes[(size_t)i] == b.planes[(size_t)i];
|
||||
CHECK(same);
|
||||
CHECK(same_planes(a, b));
|
||||
const std::vector<float> dr = decode(r, kR, kC), da = decode(a, kR, kC);
|
||||
REQUIRE(!da.empty());
|
||||
const double er = out_err(w, dr), ea = out_err(w, da);
|
||||
@@ -179,11 +186,10 @@ void fwht_rows(std::vector<double>& h, int64_t k, int64_t n) {
|
||||
}
|
||||
|
||||
TEST(a_hessian_is_brought_into_the_domain_its_grid_quantises_in) {
|
||||
/* The same Gram twice over: as the model's input (domain 0, what a bf16 model's tap writes)
|
||||
* and rotated by the width-128 Hadamard (domain 128, what a rotated container's tap writes).
|
||||
* A rotated grid quantised against either must come out byte for byte the same -- the reader
|
||||
* rotates the first exactly as the second was rotated -- and an unrotated grid against the
|
||||
* rotated file must too, after the reader rotates it back. */
|
||||
/* The same Gram as the model's input (domain 0, a bf16 model's tap) and rotated by the
|
||||
* width-128 Hadamard (domain 128, a rotated container's tap). A rotated grid must quantise
|
||||
* byte-identically against either file, since the reader rotates the first exactly as the
|
||||
* second was rotated; an unrotated grid against the rotated file is checked below. */
|
||||
NEED_LIBQUANT();
|
||||
const std::vector<float>& hf = H();
|
||||
std::vector<double> h(hf.begin(), hf.end());
|
||||
@@ -196,9 +202,9 @@ TEST(a_hessian_is_brought_into_the_domain_its_grid_quantises_in) {
|
||||
for (int64_t r = 0; r < kC; ++r)
|
||||
for (int64_t c = 0; c < kC; ++c) rot[(size_t)(r * kC + c)] = (float)t[(size_t)(c * kC + r)];
|
||||
|
||||
char tmpl[] = "/tmp/rad_quant_dom_XXXXXX";
|
||||
REQUIRE(mkdtemp(tmpl) != nullptr);
|
||||
const std::string dm = tmpl;
|
||||
const TempDir td("rad_quant_dom");
|
||||
REQUIRE(!td.path.empty());
|
||||
const std::string dm = td.path;
|
||||
REQUIRE(write_gram(dm + "/gram.r0.blk.2.attn_q.bin", kC, kT, rot, 128));
|
||||
|
||||
auto run = [&](const std::string& dir, bool rotated) {
|
||||
@@ -210,28 +216,24 @@ TEST(a_hessian_is_brought_into_the_domain_its_grid_quantises_in) {
|
||||
const Quantised a = run(calib_dir(), true), b = run(dm, true);
|
||||
REQUIRE_EQ(a.status, RAD_OK);
|
||||
REQUIRE_EQ(b.status, RAD_OK);
|
||||
int same = a.enc.n_planes == b.enc.n_planes;
|
||||
for (int i = 0; same && i < a.enc.n_planes; ++i) same &= a.planes[(size_t)i] == b.planes[(size_t)i];
|
||||
CHECK(same);
|
||||
CHECK(same_planes(a, b));
|
||||
|
||||
/* And the way back: a rotated file read for an unrotated grid is the model's Gram up to the
|
||||
* rounding of a rotation there and back, so the two quantise within a hair of each other. */
|
||||
/* The way back: a rotated file read for an unrotated grid is the model's Gram up to the
|
||||
* rounding of a rotation there and back, so the two results are nearly equal. */
|
||||
const Quantised c = run(calib_dir(), false), d = run(dm, false);
|
||||
REQUIRE_EQ(c.status, RAD_OK);
|
||||
REQUIRE_EQ(d.status, RAD_OK);
|
||||
const double ec = out_err(weight(), decode(c, kR, kC)), ed = out_err(weight(), decode(d, kR, kC));
|
||||
std::fprintf(stderr, " unrotated grid: model-domain %.6g, back from rotated %.6g\n", ec, ed);
|
||||
CHECK(std::fabs(ec - ed) <= 1e-3 * ec);
|
||||
std::remove((dm + "/gram.r0.blk.2.attn_q.bin").c_str());
|
||||
rmdir(dm.c_str());
|
||||
}
|
||||
|
||||
TEST(an_expert_reads_its_own_hessian_before_its_layers) {
|
||||
/* A layer's pooled Gram and one expert's own, which differ (the own one is the pooled one with
|
||||
* its channels reversed). The expert with a file of its own must quantise exactly as against
|
||||
* that Gram alone, wherever its rank put it; an expert with no file of its own, and one whose
|
||||
* file says fewer rows than libquant trusts an own Gram with, exactly as against the pooled
|
||||
* one. And the two answers must differ, or the test could not tell them apart. */
|
||||
/* A layer's pooled Gram and one expert's own (the pooled one with its channels reversed). An
|
||||
* expert with its own file quantises exactly as against that Gram alone, whatever rank wrote
|
||||
* it. An expert with no own file, or one whose file has fewer rows than libquant trusts,
|
||||
* quantises exactly as against the pooled Gram. The two results must differ, or the test
|
||||
* would prove nothing. */
|
||||
NEED_LIBQUANT();
|
||||
const std::vector<float>& pool = H();
|
||||
std::vector<float> own((size_t)(kC * kC));
|
||||
@@ -239,10 +241,9 @@ TEST(an_expert_reads_its_own_hessian_before_its_layers) {
|
||||
for (int64_t j = 0; j < kC; ++j)
|
||||
own[(size_t)(i * kC + j)] = pool[(size_t)((kC - 1 - i) * kC + (kC - 1 - j))];
|
||||
|
||||
char t1[] = "/tmp/rad_quant_exp_XXXXXX", t2[] = "/tmp/rad_quant_own_XXXXXX",
|
||||
t3[] = "/tmp/rad_quant_pool_XXXXXX";
|
||||
REQUIRE(mkdtemp(t1) && mkdtemp(t2) && mkdtemp(t3));
|
||||
const std::string both = t1, only_own = t2, only_pool = t3;
|
||||
const TempDir t1("rad_quant_exp"), t2("rad_quant_own"), t3("rad_quant_pool");
|
||||
REQUIRE(!t1.path.empty() && !t2.path.empty() && !t3.path.empty());
|
||||
const std::string both = t1.path, only_own = t2.path, only_pool = t3.path;
|
||||
const std::vector<std::string> files = {
|
||||
both + "/gram.r0.blk.2.ffn_down_exps.bin", both + "/gram.r1.blk.2.ffn_down_exps.7.bin",
|
||||
both + "/gram.r0.blk.2.ffn_down_exps.9.bin", only_own + "/gram.r0.blk.2.ffn_down_exps.bin",
|
||||
@@ -262,20 +263,13 @@ TEST(an_expert_reads_its_own_hessian_before_its_layers) {
|
||||
CHECK_EQ(q.status, RAD_OK);
|
||||
return q;
|
||||
};
|
||||
auto same = [](const Quantised& a, const Quantised& b) {
|
||||
int s = a.enc.n_planes == b.enc.n_planes;
|
||||
for (int i = 0; s && i < a.enc.n_planes; ++i) s &= a.planes[(size_t)i] == b.planes[(size_t)i];
|
||||
return s != 0;
|
||||
};
|
||||
const char* e7 = "blk.2.ffn_down_exps.7.weight";
|
||||
const char* e9 = "blk.2.ffn_down_exps.9.weight";
|
||||
const char* e11 = "blk.2.ffn_down_exps.11.weight";
|
||||
CHECK(same(run(both, e7), run(only_own, e7)));
|
||||
CHECK(!same(run(both, e7), run(only_pool, e7)));
|
||||
CHECK(same(run(both, e9), run(only_pool, e9)));
|
||||
CHECK(same(run(both, e11), run(only_pool, e11)));
|
||||
for (const std::string& f : files) std::remove(f.c_str());
|
||||
for (const std::string& d : { both, only_own, only_pool }) rmdir(d.c_str());
|
||||
CHECK(same_planes(run(both, e7), run(only_own, e7)));
|
||||
CHECK(!same_planes(run(both, e7), run(only_pool, e7)));
|
||||
CHECK(same_planes(run(both, e9), run(only_pool, e9)));
|
||||
CHECK(same_planes(run(both, e11), run(only_pool, e11)));
|
||||
}
|
||||
|
||||
TEST(awq_scales_the_columns_the_input_says_matter) {
|
||||
@@ -331,6 +325,77 @@ TEST(paroquant_learns_a_rotation_the_reader_undoes) {
|
||||
CHECK_EQ(bad, 0);
|
||||
}
|
||||
|
||||
/* A calibration file GPTQ cannot use is reported on stderr, by path and reason, and skipped; with
|
||||
* nothing usable the weight is rounded to nearest, byte for byte, and that is said too. The report
|
||||
* is the only sign of it in an hours-long conversion, so each reason is pinned: a file that is not
|
||||
* a Gram, one of another width, one cut short, domains that cannot both tile K, and a Hessian that
|
||||
* is not positive definite even damped. */
|
||||
TEST(an_unusable_calibration_file_is_reported_and_gptq_rounds_to_nearest) {
|
||||
NEED_LIBQUANT();
|
||||
const int64_t R = 8, C = 128;
|
||||
const char* name = "blk.6.attn_k.weight";
|
||||
const std::vector<float> w = draw(R * C, 61, 4.0f);
|
||||
std::vector<float> spd((size_t)(C * C), 0.0f), neg((size_t)(C * C), 0.0f);
|
||||
for (int64_t i = 0; i < C; ++i) {
|
||||
spd[(size_t)(i * C + i)] = 2.0f;
|
||||
if (i + 1 < C) spd[(size_t)(i * C + i + 1)] = spd[(size_t)((i + 1) * C + i)] = 0.5f;
|
||||
neg[(size_t)(i * C + i)] = -1.0f;
|
||||
}
|
||||
Opts ro;
|
||||
ro.s("codes", "i4").i("group", 32).s("scale", "bf16");
|
||||
const Quantised rtn = quantise(quantizer("rtn"), ro, name, R, C, w);
|
||||
REQUIRE_EQ(rtn.status, RAD_OK);
|
||||
|
||||
struct Bad {
|
||||
const char* what;
|
||||
const char* says;
|
||||
std::function<bool(const std::string&)> write;
|
||||
};
|
||||
const std::string file = "/gram.r0.blk.6.attn_k.bin";
|
||||
const Bad cases[] = {
|
||||
{ "not a Gram", "is not a RADGRAM2 file", [&](const std::string& d) {
|
||||
FILE* f = std::fopen((d + file).c_str(), "wb");
|
||||
if (!f) return false;
|
||||
const char junk[64] = "RADGRAM1";
|
||||
std::fwrite(junk, 1, sizeof junk, f);
|
||||
return std::fclose(f) == 0; } },
|
||||
{ "another width", "holds a 64-wide Hessian and this weight's K is 128",
|
||||
[&](const std::string& d) {
|
||||
return write_gram(d + file, 64, 100, std::vector<float>(64 * 64, 1.0f)); } },
|
||||
{ "cut short", "is short", [&](const std::string& d) {
|
||||
if (!write_gram(d + file, C, 100, spd)) return false;
|
||||
return ::truncate((d + file).c_str(), 32 + 4 * C * C / 2) == 0; } },
|
||||
{ "a domain that cannot tile K", "do not both tile K=128", [&](const std::string& d) {
|
||||
return write_gram(d + file, C, 100, spd, 96); } },
|
||||
{ "not positive definite", "not positive definite even damped", [&](const std::string& d) {
|
||||
return write_gram(d + file, C, 100, neg); } },
|
||||
};
|
||||
for (const Bad& b : cases) {
|
||||
const TempDir dir("rad_quant_badcalib");
|
||||
REQUIRE(!dir.path.empty());
|
||||
REQUIRE(b.write(dir.path));
|
||||
Opts go;
|
||||
go.s("codes", "i4").i("group", 32).s("scale", "bf16").s("calib", dir.path.c_str());
|
||||
Quantised g;
|
||||
const std::string log = stderr_of([&] { g = quantise(quantizer("gptq"), go, name, R, C, w); });
|
||||
REQUIRE_EQ(g.status, RAD_OK);
|
||||
const bool said = has(log, b.says) && has(log, "rounding it to nearest");
|
||||
if (!said) std::fprintf(stderr, " %s: %s", b.what, log.c_str());
|
||||
CHECK(said);
|
||||
CHECK(same_planes(g, rtn));
|
||||
}
|
||||
|
||||
/* A usable file in the same place does move codes, so the equality above is the fallback's. */
|
||||
const TempDir dir("rad_quant_goodcalib");
|
||||
REQUIRE(write_gram(dir.path + file, C, 100, spd));
|
||||
Opts go;
|
||||
go.s("codes", "i4").i("group", 32).s("scale", "bf16").s("calib", dir.path.c_str());
|
||||
Quantised g;
|
||||
(void)stderr_of([&] { g = quantise(quantizer("gptq"), go, name, R, C, w); });
|
||||
REQUIRE_EQ(g.status, RAD_OK);
|
||||
CHECK(!same_planes(g, rtn));
|
||||
}
|
||||
|
||||
TEST(a_calibrated_quantiser_refuses_a_grid_it_cannot_serve) {
|
||||
NEED_LIBQUANT();
|
||||
RadQuantWeight qw{};
|
||||
|
||||
Reference in new issue
Block a user