Skip to content

Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦

C

qpack: refuse a manifest whose quantization disagrees with the packed shapes at open

Léon Avic Simmonsfb3a87f3ab97
diff --git a/c/qpack.c b/c/qpack.c
index 0ac07baa..6d72185b 100644
--- a/c/qpack.c
+++ b/c/qpack.c
@@ -388,6 +388,76 @@ static int qpack_verify_manifest_size(jval *files, const char *relative,
return 0;
}
+/* Every packed projection is sized by the one quantization the manifest
+ * declares. A quantized weight stores input_dim / (32 / bits) U32 words per
+ * row and its scales input_dim / group_size groups per row; both describe the
+ * same input_dim, so when the manifest's bits and group size make them
+ * disagree the recorded quantization is not the one the bytes were packed
+ * with -- a module quantized at another width than the manifest says, or a
+ * manifest carried over from another export -- and dequantizing by it would
+ * decode garbage. Refused here, at open, naming the section and both widths,
+ * before a caller has sized a single expert slot from the stride; the affine
+ * view used to be the first place this surfaced, after the slots existed.
+ * (Swiftlet #31 refuses the same container the same way at load.)
+ *
+ * The container records one quantization for every layer and expert, and one
+ * section table shared by all of them, so "mixed expert quant" can only show
+ * up as one module's widths disagreeing with the manifest: that is refused by
+ * the module's name. A manifest with no quantization metadata is not judged
+ * here; the affine view refuses it as unsupported when a projection is asked
+ * for. */
+static int qpack_check_packed_widths(const ColiQpackLayout *layout,
+ int quant_bits, int quant_group_size,
+ char *error, size_t error_capacity) {
+ if (quant_bits == 0 && quant_group_size == 0) return 0;
+ if ((quant_bits != 4 && quant_bits != 8) || quant_group_size <= 0)
+ return qpack_fail(error, error_capacity,
+ "manifest %d-bit/g%d is not a supported affine "
+ "quantization", quant_bits, quant_group_size);
+ const size_t per_word = 32u / (size_t)quant_bits;
+ const size_t group_size = (size_t)quant_group_size;
+ static const char suffix[] = ".weight";
+ const size_t suffix_size = sizeof(suffix) - 1;
+ for (size_t i = 0; i < layout->section_count; i++) {
+ const ColiQpackSection *weight = &layout->sections[i];
+ size_t name_size = strlen(weight->name);
+ if (name_size <= suffix_size ||
+ strcmp(weight->name + name_size - suffix_size, suffix) != 0 ||
+ strcmp(weight->dtype, "U32") != 0)
+ continue;
+ int module_size = (int)(name_size - suffix_size);
+ char scales_name[256];
+ int count = snprintf(scales_name, sizeof(scales_name), "%.*s.scales",
+ module_size, weight->name);
+ if (count < 0 || (size_t)count >= sizeof(scales_name)) continue;
+ const ColiQpackSection *scales = NULL;
+ for (size_t j = 0; j < layout->section_count && !scales; j++)
+ if (strcmp(layout->sections[j].name, scales_name) == 0)
+ scales = &layout->sections[j];
+ /* No scales beside the weight: the affine view's refusal, not ours. */
+ if (!scales) continue;
+ size_t weight_inner = weight->shape[weight->rank - 1];
+ size_t scales_inner = scales->shape[scales->rank - 1];
+ if (weight_inner > SIZE_MAX / per_word ||
+ scales_inner > SIZE_MAX / group_size)
+ return qpack_fail(error, error_capacity,
+ "section %.*s: packed width overflows",
+ module_size, weight->name);
+ size_t from_weight = weight_inner * per_word;
+ size_t from_scales = scales_inner * group_size;
+ if (from_weight != from_scales)
+ return qpack_fail(
+ error, error_capacity,
+ "section %.*s: manifest %d-bit/g%d disagrees with the packed "
+ "shapes (weight inner %zu = %zu inputs, scales inner %zu = "
+ "%zu inputs); the container's recorded quantization does not "
+ "match its expert bytes",
+ module_size, weight->name, quant_bits, quant_group_size,
+ weight_inner, from_weight, scales_inner, from_scales);
+ }
+ return 0;
+}
+
int coli_qpack_open(ColiQpackReader *reader, const char *container_dir,
char *error, size_t error_capacity) {
if (!reader)
@@ -447,6 +517,14 @@ int coli_qpack_open(ColiQpackReader *reader, const char *container_dir,
}
free(layout_path);
+ if (qpack_check_packed_widths(&reader->layout, reader->quant_bits,
+ reader->quant_group_size,
+ error, error_capacity)) {
+ json_free(manifest_root);
+ coli_qpack_close(reader);
+ return -1;
+ }
+
if (reader->layout.expert_count >
(uint64_t)INT64_MAX / reader->layout.expert_stride) {
json_free(manifest_root);
diff --git a/c/tests/test_qpack.c b/c/tests/test_qpack.c
index c9f5f756..3ff2d810 100644
--- a/c/tests/test_qpack.c
+++ b/c/tests/test_qpack.c
@@ -84,16 +84,68 @@ static const char *overflow_layout =
"{\"name\":\"w\",\"dtype\":\"U32\","
"\"shape\":[4294967296,4294967296],\"offset\":0,\"size\":4}]}";
+/* Scales with the right packed width but the wrong leading dimension: open
+ * accepts it (the widths agree), the affine view refuses it. */
static const char *bad_affine_shape_layout =
"{\"expertCount\":2,\"layerCount\":2,\"expertStride\":16384,"
"\"linearLayers\":[true,false],\"sections\":["
"{\"name\":\"gate_proj.weight\",\"dtype\":\"U32\","
"\"shape\":[2,1],\"offset\":0,\"size\":8},"
"{\"name\":\"gate_proj.scales\",\"dtype\":\"BF16\","
- "\"shape\":[1,2],\"offset\":8,\"size\":4},"
+ "\"shape\":[1,1],\"offset\":8,\"size\":2},"
"{\"name\":\"gate_proj.biases\",\"dtype\":\"BF16\","
"\"shape\":[2,1],\"offset\":12,\"size\":4}]}";
+/* Manifest 4-bit/g8, but the scales carry two groups per row (16 inputs)
+ * against a one-word weight row (8 inputs): the recorded quantization does
+ * not match the packed bytes. */
+static const char *mismatched_width_layout =
+ "{\"expertCount\":2,\"layerCount\":2,\"expertStride\":16384,"
+ "\"linearLayers\":[true,false],\"sections\":["
+ "{\"name\":\"gate_proj.weight\",\"dtype\":\"U32\","
+ "\"shape\":[2,1],\"offset\":0,\"size\":8},"
+ "{\"name\":\"gate_proj.scales\",\"dtype\":\"BF16\","
+ "\"shape\":[2,2],\"offset\":8,\"size\":8},"
+ "{\"name\":\"gate_proj.biases\",\"dtype\":\"BF16\","
+ "\"shape\":[2,2],\"offset\":16,\"size\":8}]}";
+
+/* gate_proj packed at the manifest's 4-bit/g8 (8 inputs), up_proj packed at
+ * 8-bit/g8 for the same 8 inputs (two words per row, one group): one module
+ * quantized differently from the rest, which a single-valued manifest cannot
+ * describe. Refused by the module's name. */
+static const char *mixed_module_layout =
+ "{\"expertCount\":2,\"layerCount\":2,\"expertStride\":16384,"
+ "\"linearLayers\":[true,false],\"sections\":["
+ "{\"name\":\"gate_proj.weight\",\"dtype\":\"U32\","
+ "\"shape\":[2,1],\"offset\":0,\"size\":8},"
+ "{\"name\":\"gate_proj.scales\",\"dtype\":\"BF16\","
+ "\"shape\":[2,1],\"offset\":8,\"size\":4},"
+ "{\"name\":\"gate_proj.biases\",\"dtype\":\"BF16\","
+ "\"shape\":[2,1],\"offset\":12,\"size\":4},"
+ "{\"name\":\"up_proj.weight\",\"dtype\":\"U32\","
+ "\"shape\":[2,2],\"offset\":16,\"size\":16},"
+ "{\"name\":\"up_proj.scales\",\"dtype\":\"BF16\","
+ "\"shape\":[2,1],\"offset\":32,\"size\":4},"
+ "{\"name\":\"up_proj.biases\",\"dtype\":\"BF16\","
+ "\"shape\":[2,1],\"offset\":36,\"size\":4}]}";
+
+/* The same two modules, both packed at the manifest's 4-bit/g8: opens. */
+static const char *two_module_layout =
+ "{\"expertCount\":2,\"layerCount\":2,\"expertStride\":16384,"
+ "\"linearLayers\":[true,false],\"sections\":["
+ "{\"name\":\"gate_proj.weight\",\"dtype\":\"U32\","
+ "\"shape\":[2,1],\"offset\":0,\"size\":8},"
+ "{\"name\":\"gate_proj.scales\",\"dtype\":\"BF16\","
+ "\"shape\":[2,1],\"offset\":8,\"size\":4},"
+ "{\"name\":\"gate_proj.biases\",\"dtype\":\"BF16\","
+ "\"shape\":[2,1],\"offset\":12,\"size\":4},"
+ "{\"name\":\"up_proj.weight\",\"dtype\":\"U32\","
+ "\"shape\":[2,1],\"offset\":16,\"size\":8},"
+ "{\"name\":\"up_proj.scales\",\"dtype\":\"BF16\","
+ "\"shape\":[2,1],\"offset\":24,\"size\":4},"
+ "{\"name\":\"up_proj.biases\",\"dtype\":\"BF16\","
+ "\"shape\":[2,1],\"offset\":28,\"size\":4}]}";
+
static void make_path(char *output, size_t capacity,
const char *root, const char *name) {
int count = snprintf(output, capacity, "%s/%s", root, name);
@@ -301,6 +353,63 @@ static void test_valid_container(const char *layout,
cleanup(root);
}
+/* A manifest whose bits/group size disagree with the packed shapes is refused
+ * at open, by section and both widths, before a single layer file is opened
+ * or an expert slot sized. */
+static void expect_width_refusal(const char *layout, int quant_bits,
+ int group_size, const char *section,
+ const char *widths) {
+ char root[] = "test_qpack_width_XXXXXX";
+ CHECK(mkdtemp(root) != NULL);
+ write_fixture(root, layout, "QPACK", 32768, quant_bits, group_size,
+ COLI_AFFINE_SCALAR_BF16);
+ ColiQpackReader reader;
+ char error[512] = {0};
+ CHECK(coli_qpack_open(&reader, root, error, sizeof(error)) != 0);
+ CHECK(strstr(error, section) != NULL);
+ CHECK(strstr(error, widths) != NULL);
+ CHECK(strstr(error, "recorded quantization does not match") != NULL);
+ CHECK(reader.layer_fds == NULL && reader.layout.sections == NULL);
+ cleanup(root);
+}
+
+static void test_packed_width_refusals(void) {
+ /* 4-bit/g8: weight inner 1 word = 8 inputs, scales inner 2 groups = 16. */
+ expect_width_refusal(mismatched_width_layout, 4, 8, "section gate_proj:",
+ "weight inner 1 = 8 inputs, scales inner 2 = 16 inputs");
+ /* The same bytes read as 8-bit/g4: 4 inputs against 8. Still refused. */
+ expect_width_refusal(mismatched_width_layout, 8, 4, "section gate_proj:",
+ "weight inner 1 = 4 inputs, scales inner 2 = 8 inputs");
+ /* gate_proj agrees with the manifest; up_proj was packed at 8 bits. */
+ expect_width_refusal(mixed_module_layout, 4, 8, "section up_proj:",
+ "weight inner 2 = 16 inputs, scales inner 1 = 8 inputs");
+
+ /* Declared quantization the affine contract cannot size anything by. */
+ char root[] = "test_qpack_bits_XXXXXX";
+ CHECK(mkdtemp(root) != NULL);
+ write_fixture(root, valid_layout_bf16, "QPACK", 32768, 2, 8,
+ COLI_AFFINE_SCALAR_BF16);
+ ColiQpackReader reader;
+ char error[512] = {0};
+ CHECK(coli_qpack_open(&reader, root, error, sizeof(error)) != 0);
+ CHECK(strstr(error, "2-bit/g8 is not a supported") != NULL);
+ CHECK(reader.layer_fds == NULL && reader.layout.sections == NULL);
+ cleanup(root);
+}
+
+static void test_two_modules_open(void) {
+ char root[] = "test_qpack_two_XXXXXX";
+ CHECK(mkdtemp(root) != NULL);
+ write_fixture(root, two_module_layout, "QPACK", 32768, 4, 8,
+ COLI_AFFINE_SCALAR_BF16);
+ ColiQpackReader reader;
+ char error[512] = {0};
+ CHECK(coli_qpack_open(&reader, root, error, sizeof(error)) == 0);
+ CHECK(coli_qpack_find_section(&reader, "up_proj.scales") != NULL);
+ coli_qpack_close(&reader);
+ cleanup(root);
+}
+
static void test_affine_shape_refusal(void) {
char root[] = "test_qpack_shape_XXXXXX";
CHECK(mkdtemp(root) != NULL);
@@ -403,10 +512,13 @@ int main(void) {
expect_open_failure(overflow_layout, "QPACK", 32768, "invalid shape");
expect_open_failure(valid_layout_bf16, "QPACK", 32767,
"manifest size mismatch");
+ test_packed_width_refusals();
+ test_two_modules_open();
test_affine_shape_refusal();
test_eager_layer_validation_cleanup();
test_embedded_nul_metadata();
test_post_open_truncation();
- puts("test_qpack: strict v1 metadata, fixed-stride read, affine views: ok");
+ puts("test_qpack: strict v1 metadata, packed widths, fixed-stride read, "
+ "affine views: ok");
return 0;
}