libquant: the codebook loader caps as it reads and refuses an infinite unit

vq= is the one codebook path that reads a file; table= caps its values
at 4096 but vq= appended every line to a growing vector and checked the
count only after reading the whole file, so a 10 GB codebook grew 10 GB
of vector before anything said no. And its "# unit X" line parsed X with
sscanf %lf -- which accepts "inf" -- while the fallback guard was
!(unit > 0.0), an infinity passes. A "# unit inf" file set every decoded
value to infinity.

The count is now checked as rows append, at 4096: the widest code the
grid emits is u12, so past 4096 codewords could not be indexed even with
the memory read, and the old code silently mis-indexed the over-4095 case
itself. And only a finite positive names a unit -- "inf" and "nan" fall
back to the entries' own rms, as a malformed value already did. A
quant_test case pins the 4096/4097 boundary and that "# unit inf" now
decodes finite.
This commit is contained in:
fl0l0u committed 2026-11-15 20:38:10 +00:00
1 parent 3ac3954fea
commit be09d90c71
2 files changed
+91 -4

No files matched your search

+29 -4
View File
@@ -337,10 +337,15 @@ int grid_parse(const RadParam* o, int n_o, Grid* g, std::string* why) {
}
char line[4096];
double unit = 0.0;
int64_t rows = 0;
while (std::fgets(line, sizeof line, f)) {
if (line[0] == '#') {
double u = 0.0;
if (std::sscanf(line, "# unit %lf", &u) == 1) unit = u;
/* %lf parses "inf", and the fallback guard below is `!(unit > 0.0)`, which an
* infinite unit passes: an infinite scale unit would flow into quantisation
* where a NaN already falls back. Only a finite positive names the unit. */
if (std::sscanf(line, "# unit %lf", &u) == 1 && std::isfinite(u) && u > 0.0)
unit = u;
continue;
}
std::vector<float> row;
@@ -353,6 +358,16 @@ int grid_parse(const RadParam* o, int n_o, Grid* g, std::string* why) {
p = end;
}
if (row.empty()) continue;
/* Capped while appending, as table= is: the widest code is u12, so more than 4096
* codewords could not be indexed even with the memory read, and the file must be
* refused without first growing a vector over every line of it. */
if (++rows > 4096) {
std::fclose(f);
if (why)
*why = fmt("vq=%s holds more than 4096 codewords; a codebook holds at most 4096",
vf);
return RAD_E_INVAL;
}
if (g->vdim == 0) g->vdim = (int64_t)row.size();
bool ok = (int64_t)row.size() == g->vdim;
for (float v : row) ok = ok && v >= -127.0f && v <= 127.0f;
@@ -453,7 +468,9 @@ int grid_parse(const RadParam* o, int n_o, Grid* g, std::string* why) {
char rest[256] = {};
if (!std::fgets(rest, sizeof rest, f)) break;
double u = 0.0;
if (std::sscanf(rest, " unit %lf", &u) == 1) lut_unit = u;
if (std::sscanf(rest, " unit %lf", &u) == 1)
lut_unit = (std::isfinite(u) && u > 0.0) ? u : 0.0; // like vq=: only a
// finite positive names a unit; "inf"/"nan" fall back to the entries' rms
continue;
}
v = std::strtol(tok, nullptr, 10);
@@ -470,8 +487,16 @@ int grid_parse(const RadParam* o, int n_o, Grid* g, std::string* why) {
}
double ss = 0.0;
for (float v2 : g->tcq_lut) ss += (double)v2 * v2;
g->vq_unit = (lut_unit > 0.0 ? lut_unit : std::sqrt(ss / (double)S)) /
rad_param_getf(o, n_o, "tcq_gain", 1.0);
const double gain = rad_param_getf(o, n_o, "tcq_gain", 1.0);
if (!(gain > 0.0) || !std::isfinite(gain)) {
if (why) *why = "tcq_gain must be a positive finite number";
return RAD_E_INVAL;
}
g->vq_unit = (lut_unit > 0.0 ? lut_unit : std::sqrt(ss / (double)S)) / gain;
if (!std::isfinite(g->vq_unit)) {
if (why) *why = "the tcq unit is not finite; check tcq_lut's values";
return RAD_E_INVAL;
}
if (rad_param_geti(o, n_o, "tcq_peak", 0))
for (float v2 : g->tcq_lut) g->tcq_peak = std::max(g->tcq_peak, std::fabs(v2));
g->tcq_rb = rad_param_geti(o, n_o, "tcq_rb", 0);
+62
View File
@@ -877,6 +877,68 @@ TEST(a_codebook_indexes_its_table_and_the_scale_multiplies_it) {
}
}
/* A vq= codebook file is attacker data, and its loader appended every line to memory before
* checking any count, while its "# unit" line parsed `inf` through %lf -- and the fallback
* guard `!(unit > 0.0)` let infinity through, where a NaN falls back to the entries' rms. The
* count is now checked while appending (the widest code is u12: past 4096 codewords could not
* be indexed even with the memory), and only a finite positive names the unit. */
TEST(the_codebook_loader_caps_while_reading_and_refuses_an_infinite_unit) {
NEED_LIBQUANT();
TempDir dir("vqcap");
auto write_cb = [&](const char* name, const char* head, int lines) -> std::string {
const std::string p = dir.path + "/" + name;
/* No REQUIRE in here: its failure spelling returns void, and this lambda returns a
* path. An unopenable path yields an empty/short file, which the loaders refuse --
* the surrounding CHECKs catch it either way. */
std::FILE* f = std::fopen(p.c_str(), "wb");
if (f != nullptr) {
if (head) std::fprintf(f, "%s\n", head);
for (int i = 0; i < lines; ++i) std::fprintf(f, "%d %d %d %d\n", i % 5 - 2, 3, -4, 5);
std::fclose(f);
}
return p;
};
const int64_t R = 8, C = 128;
const std::vector<float> w = draw(R * C, 5, 0.0f);
/* vq= is gptq's path -- rtn refuses it ("vq= is quantised by gptq") -- so every case here
* goes through gptq and every refusal below is the LOADER's, not a quantizer mismatch. */
/* One past the cap: refused by name. 4097 entries used to build an encoding whose u12
* codes could not index its own last codeword. */
{
Opts o;
o.s("vq", write_cb("big.txt", "# unit 1.0", 4097).c_str()).i("group", 128).s("scale", "bf16");
CHECK(quantise(quantizer("gptq"), o, "x", R, C, w).status != RAD_OK);
}
/* The cap itself is the largest honest codebook: it reads. */
{
Opts o;
o.s("vq", write_cb("cap.txt", "# unit 1.0", 4096).c_str()).i("group", 128).s("scale", "bf16");
CHECK_EQ(quantise(quantizer("gptq"), o, "x", R, C, w).status, RAD_OK);
}
/* "# unit inf" used to multiply every decoded value by infinity; ignored now, the entries'
* own rms takes over and the decode stays finite. */
{
Opts o;
o.s("vq", write_cb("inf.txt", "# unit inf", 256).c_str()).i("group", 128).s("scale", "bf16");
Quantised q = quantise(quantizer("gptq"), o, "x", R, C, w);
REQUIRE_EQ(q.status, RAD_OK);
const std::vector<float> d = decode(q, R, C);
REQUIRE(!d.empty());
int bad = 0;
for (float x : d) bad += !std::isfinite(x);
CHECK_EQ(bad, 0);
}
/* tcq's sibling flow: a zero tcq_gain used to divide the unit by zero and every code
* became inf; the loader refuses it by name now. */
{
Opts o;
o.s("codes", "u2").i("tcq", 10).i("tcq_rows", 256).i("group", 128).s("scale", "bf16")
.f("tcq_gain", 0.0);
CHECK(quantise(quantizer("gptq"), o, "x", R, C, w).status != RAD_OK);
}
}
/* A trellis grid (tcq=L): each column's codes are the bits a 2^L-state bitshift trellis shifts in,
* row by row, and a value is its state's table entry times the block's scale. Its own scheme,
* decoded by libquant's hook: the decode is the values the Viterbi search chose, and at the same