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

feat(qwen36): load MLX-affine dense weights from a qpack container

Léon Avic Simmonsab18e375a2cd
diff --git a/c/Makefile b/c/Makefile
index 62568728..8280bb28 100644
--- a/c/Makefile
+++ b/c/Makefile
@@ -1291,6 +1291,14 @@ tests/test_qwen36_dense_batch$(EXE): tests/test_qwen36_dense_batch.c gsgemv.h qg
tests/test_qwen36_json_escape$(EXE): tests/test_qwen36_json_escape.c gsgemv.h qgemv.h sse41_kernels.h qwen36.c qwen38_vision.h expert_ffn.h qwen36_tier.h st.h json.h compat.h $(QWEN36_TIER_SRC) $(CUDA_OBJ)
$(CC) $(QWEN36_CFLAGS) $< $(QWEN36_TIER_SRC) $(CUDA_OBJ) -o $@ $(QWEN36_LDFLAGS)
+# Dense-bridge gate: qwen36's loader against an MLX affine
+# model.safetensors (the dense half of a Swiftlet qpack container) --
+# `language_model.` name resolution, U32 triple indexing, checked Q4/Q8
+# expansion, refusals. With argv it doubles as the round-trip harness for
+# tests/test_make_qwen36_qpack_snap.py.
+tests/test_qwen36_dense_affine$(EXE): tests/test_qwen36_dense_affine.c qwen36.c affine_quant.h qwen36_tier.h st.h json.h compat.h $(QWEN36_TIER_SRC) $(CUDA_OBJ)
+ $(CC) $(QWEN36_CFLAGS) $< $(QWEN36_TIER_SRC) $(CUDA_OBJ) -o $@ $(QWEN36_LDFLAGS)
+
# Both tokenizer.json merge spellings ("a b" strings and ["a","b"] pairs)
# must index the same merge table; the pair form is what Qwen3.6 ships.
tests/test_qwen36_tok_merges$(EXE): tests/test_qwen36_tok_merges.c gsgemv.h qgemv.h sse41_kernels.h qwen36.c qwen38_vision.h expert_ffn.h qwen36_tier.h st.h json.h compat.h $(QWEN36_TIER_SRC) $(CUDA_OBJ)
diff --git a/c/qwen36.c b/c/qwen36.c
index a9d4df89..50d53207 100644
--- a/c/qwen36.c
+++ b/c/qwen36.c
@@ -69,6 +69,7 @@ static int qwen36_max_ctx(void) {
#include "expert_ffn.h" /* routed experts: planar int4 kernel + layer runner */
#include "idot.h" /* integer dot kernels for the dense trunk (COLI_DENSE_IDOT, COLI_DENSE_BITS) */
#include "qwen38_vision.h" /* the ViT: the same tower in Qwen3.5/3.6/3.8 (#1757) */
+#include "affine_quant.h" /* MLX affine dense triples in model.safetensors */
#ifdef COLI_SEGMENT_ADAPTER
#include "segment_runtime.h"
#include "segment_adapters.h"
@@ -1587,20 +1588,128 @@ static void load_meta(Cfg *c, const char *snap) {
c->dn_vheads, c->dn_kheads, c->dn_kdim, c->dn_vdim, c->dn_convk, c->dn_conv_dim);
}
+/* Multimodal Qwen3.5/3.6 checkpoints (a Swiftlet qpack container's
+ * model.safetensors included) store the text stack under a `language_model.`
+ * prefix; the engine and the converter both speak unprefixed names. Resolve
+ * the plain name first so converted snapshots are untouched, then the
+ * prefixed one; when neither exists return the CANONICAL name so the caller's
+ * refusal names the tensor the engine actually wanted. */
+#define QW_DENSE_NAME_MAX 288 /* 256-byte call-site buffers + the prefix */
+static const char *dense_resolve(Model *m, const char *name,
+ char *buf, size_t cap) {
+ if (st_has(&m->S, name)) return name;
+ snprintf(buf, cap, "language_model.%s", name);
+ if (st_has(&m->S, buf)) return buf;
+ return name;
+}
+static int dense_has(Model *m, const char *name) {
+ char rn[QW_DENSE_NAME_MAX];
+ return st_has(&m->S, dense_resolve(m, name, rn, sizeof rn));
+}
+
+/* Dense tensor stored as an MLX affine triple (packed U32 `.weight` + sibling
+ * `.scales`/`.biases`, the layout Swiftlet writes into a qpack container's
+ * model.safetensors): expand to f32 rows through the checked affine contract.
+ * Every dimension is anchored to `want`, the CONFIG-implied element count the
+ * forward pass will index with -- the same discipline as load_t_n below, so a
+ * container whose packed geometry disagrees with config.json is a refusal,
+ * never a plausible heap OOB. bits (Q4/Q8) and the group size are DERIVED
+ * from the shapes (logical input over packed words, logical input over scale
+ * groups) rather than parsed from config quantization overrides: the file's
+ * own byte layout is what the expansion must agree with, and the affine
+ * validator re-checks the derived geometry against every buffer length. */
+static float *load_t_affine(Model *m, const char *wname, int64_t want) {
+ st_tensor *w = st_find(&m->S, wname);
+ if (!w) { fprintf(stderr, "missing %s\n", wname); exit(1); }
+ size_t wlen = strlen(wname);
+ char sname[QW_DENSE_NAME_MAX], bname[QW_DENSE_NAME_MAX];
+ if (wlen < 7 || strcmp(wname + wlen - 7, ".weight") != 0 ||
+ wlen - 7 + sizeof(".scales") > sizeof(sname)) {
+ fprintf(stderr, "%s: U32 tensor is not a `.weight` with room for "
+ "`.scales`/`.biases` siblings -- refusing\n", wname); exit(1);
+ }
+ snprintf(sname, sizeof(sname), "%.*s.scales", (int)(wlen - 7), wname);
+ snprintf(bname, sizeof(bname), "%.*s.biases", (int)(wlen - 7), wname);
+ st_tensor *s = st_find(&m->S, sname), *b = st_find(&m->S, bname);
+ if (!s || !b) {
+ fprintf(stderr, "%s: affine `.scales`/`.biases` siblings are missing "
+ "-- refusing\n", wname); exit(1);
+ }
+ ColiAffineScalarFormat sf;
+ switch (s->dtype) {
+ case 0: sf = COLI_AFFINE_SCALAR_BF16; break;
+ case 1: sf = COLI_AFFINE_SCALAR_F16; break;
+ case 2: sf = COLI_AFFINE_SCALAR_F32; break;
+ default:
+ fprintf(stderr, "%s: scales dtype %s is not BF16/F16/F32 -- "
+ "refusing\n", sname, st_dtype_name(s->dtype)); exit(1);
+ }
+ if (b->dtype != s->dtype || b->numel != s->numel) {
+ fprintf(stderr, "%s: biases dtype/numel disagree with scales -- "
+ "refusing\n", bname); exit(1);
+ }
+ int64_t packed = w->rank >= 2 ? w->shape[w->rank - 1] : 0;
+ int64_t rows = packed > 0 ? w->numel / packed : 0;
+ if (want <= 0 || rows <= 0 || w->numel != rows * packed ||
+ want % rows != 0) {
+ fprintf(stderr, "%s: packed shape does not divide the config-implied "
+ "%lld elements -- refusing\n", wname, (long long)want); exit(1);
+ }
+ int64_t in = want / rows;
+ int64_t per_word = packed > 0 && in % packed == 0 ? in / packed : 0;
+ if (per_word != 8 && per_word != 4) {
+ fprintf(stderr, "%s: %lld packed words for %lld logical columns is "
+ "neither Q4 nor Q8 -- refusing\n",
+ wname, (long long)packed, (long long)in); exit(1);
+ }
+ int64_t groups = s->numel % rows == 0 ? s->numel / rows : 0;
+ int64_t gs = groups > 0 && in % groups == 0 ? in / groups : 0;
+ if (gs <= 0) {
+ fprintf(stderr, "%s: %lld scale groups do not tile %lld logical "
+ "columns -- refusing\n",
+ sname, (long long)groups, (long long)in); exit(1);
+ }
+ void *wraw = malloc((size_t)w->nbytes);
+ void *sraw = malloc((size_t)s->nbytes);
+ void *braw = malloc((size_t)b->nbytes);
+ if (!wraw || !sraw || !braw) { fprintf(stderr, "OOM reading %s\n", wname); exit(1); }
+ st_read_raw(&m->S, wname, wraw, 1);
+ st_read_raw(&m->S, sname, sraw, 1);
+ st_read_raw(&m->S, bname, braw, 1);
+ ColiAffineQuantizedView view = {
+ wraw, sraw, braw,
+ (size_t)w->nbytes, (size_t)s->nbytes, (size_t)b->nbytes,
+ (size_t)rows, (size_t)in, (size_t)gs,
+ per_word == 8 ? COLI_AFFINE_MLX_Q4 : COLI_AFFINE_MLX_Q8, sf
+ };
+ float *p = falloc(want);
+ ColiAffineStatus status = coli_affine_dequant_ref(&view, p);
+ if (status != COLI_AFFINE_OK) {
+ fprintf(stderr, "%s: affine expansion refused (%s)\n",
+ wname, coli_affine_status_string(status)); exit(1);
+ }
+ free(wraw); free(sraw); free(braw);
+ return p;
+}
+
/* `want` is the element count the forward pass will index with. The container
* is a file, not an invariant: this used to allocate whatever st_numel reported
* while every read afterwards used CONFIG dims, so a short tensor was a plain
* heap OOB read (embed is indexed as m->embed + ids[s]*D). The expert path
* already refuses a wrong size; this is the same discipline for the dense set. */
static float *load_t_n(Model *m, const char *name, int64_t want) {
- int64_t n = st_numel(&m->S, name);
- if (n < 0) { fprintf(stderr, "missing %s\n", name); exit(1); }
+ char rn[QW_DENSE_NAME_MAX];
+ const char *nm = dense_resolve(m, name, rn, sizeof rn);
+ st_tensor *t = st_find(&m->S, nm);
+ if (!t) { fprintf(stderr, "missing %s\n", name); exit(1); }
+ if (t->dtype == 7) return load_t_affine(m, nm, want);
+ int64_t n = t->numel;
if (want > 0 && n != want) {
fprintf(stderr, "%s: %lld elements, config implies %lld -- refusing\n",
- name, (long long)n, (long long)want); exit(1);
+ nm, (long long)n, (long long)want); exit(1);
}
float *p = falloc(n);
- st_read_f32(&m->S, name, p, 0);
+ st_read_f32(&m->S, nm, p, 0);
return p;
}
@@ -1801,13 +1910,13 @@ static void model_init_range(Model *m, const char *snap, int cap, int bits,
/* q/k norms are per-head [head_dim]; only on attention layers, load if present */
if (c->has_qk_norm) {
snprintf(nm,sizeof(nm),"model.layers.%d.self_attn.q_norm.weight", ai);
- l->qn = st_has(&m->S, nm) ? load_t_n(m, nm, c->head_dim) : NULL;
+ l->qn = dense_has(m, nm) ? load_t_n(m, nm, c->head_dim) : NULL;
snprintf(nm,sizeof(nm),"model.layers.%d.self_attn.k_norm.weight", ai);
- l->kn = st_has(&m->S, nm) ? load_t_n(m, nm, c->head_dim) : NULL;
+ l->kn = dense_has(m, nm) ? load_t_n(m, nm, c->head_dim) : NULL;
} else { l->qn = NULL; l->kn = NULL; }
/* router correction bias (optional) */
snprintf(nm,sizeof(nm),"model.layers.%d.mlp.gate.e_score_correction_bias", ai);
- if (c->n_experts && st_has(&m->S, nm)) { l->gate_bias = falloc(c->n_experts); st_read_f32(&m->S, nm, l->gate_bias, 0); }
+ if (c->n_experts && dense_has(m, nm)) { l->gate_bias = load_t_n(m, nm, c->n_experts); }
else l->gate_bias = NULL;
/* shared expert (dense, int8-during-load). A dense model's MLP is the same
* SwiGLU under mlp.{gate,up,down}_proj, with no gate in front of it. */
@@ -1820,7 +1929,7 @@ static void model_init_range(Model *m, const char *snap, int cap, int bits,
load_tq(m, nm, c->shared_inter, c->hidden, quantize_dense, "shexp", &l->sh_d); QCOUNT(l->sh_d);
/* shared_expert_gate: Linear(hidden -> 1), sigmoid-gated shared expert */
snprintf(nm,sizeof(nm),"model.layers.%d.mlp.shared_expert_gate.weight", ai);
- l->sh_gate = c->n_experts && st_has(&m->S, nm) ? load_t_n(m, nm, c->hidden) : NULL;
+ l->sh_gate = c->n_experts && dense_has(m, nm) ? load_t_n(m, nm, c->hidden) : NULL;
if (c->is_attn[i]) {
/* Gated Attention (full_attention) layer, dense projections int8-during-load */
snprintf(nm,sizeof(nm),"model.layers.%d.self_attn.q_proj.weight", ai);
diff --git a/c/st.h b/c/st.h
index 2fc8b895..5d914550 100644
--- a/c/st.h
+++ b/c/st.h
@@ -33,7 +33,7 @@ typedef struct {
int fd;
int64_t off; /* offset assoluto del dato dentro al file */
int64_t nbytes;
- int dtype; /* 0=BF16 1=F16 2=F32 3=U8/I8 4=F8_E4M3 5=F8_E8M0 6=I64 */
+ int dtype; /* 0=BF16 1=F16 2=F32 3=U8/I8 4=F8_E4M3 5=F8_E8M0 6=I64 7=U32/I32 */
int64_t numel;
int rank;
int64_t shape[ST_MAX_RANK];
@@ -95,8 +95,11 @@ static int st_dtype_code(const char *s) {
!strcmp(s, "float8_e4m3fn")) return 4;
if (!strcmp(s, "F8_E8M0") || !strcmp(s, "F8_E8M0FNU")) return 5;
if (!strcmp(s, "I64") || !strcmp(s, "U64")) return 6;
- /* I32/U32: token ids in reference dumps (tools/make_qwenimage_tiny.py). Same
- * rule as the fp8 codes above: a new number, read raw, refused by the float readers. */
+ /* I32/U32: token ids in reference dumps (tools/make_qwenimage_tiny.py), and
+ * the packed U32 words of MLX affine-quantized checkpoints (Swiftlet qpack
+ * containers included; qwen36's dense affine loader reads them through
+ * st_read_raw after validating the packed geometry). Same rule as the fp8
+ * codes above: a new number, read raw, refused by the float readers. */
if (!strcmp(s, "I32") || !strcmp(s, "U32")) return 7;
fprintf(stderr, "unsupported dtype: %s\n", s); exit(1);
}
diff --git a/c/tests/test_qwen36_dense_affine.c b/c/tests/test_qwen36_dense_affine.c
new file mode 100644
index 00000000..bb82baac
--- /dev/null
+++ b/c/tests/test_qwen36_dense_affine.c
@@ -0,0 +1,270 @@
+/* Dense-bridge gates for qwen36 against an MLX affine model.safetensors (the
+ * dense half of a Swiftlet qpack container). What must hold:
+ *
+ * 1. name resolution -- the engine speaks unprefixed names
+ * (`model.layers.N...`); a multimodal container stores the text stack
+ * under `language_model.`. Both spellings must load, and the plain
+ * spelling must win when it exists (converted snapshots untouched).
+ * 2. U32 indexing -- st_init must index the packed-U32 weight tensors a
+ * quantized container carries (it used to exit(1) at the first one)
+ * with the correct shape/rank, while st_read_f32 still refuses them.
+ * 3. affine expansion -- a `.weight` U32 tensor with `.scales`/`.biases`
+ * siblings expands through the checked affine contract, with bits
+ * (Q4/Q8) and group size DERIVED from the packed shape against the
+ * config-implied element count, and the expanded values equal to
+ * scale*q + bias for the authored bit patterns.
+ * 4. refusals -- a want that the packed shape cannot tile, an orphan U32
+ * weight without siblings, and a plain tensor of the wrong size all
+ * exit non-zero instead of returning plausible rows (fork gates,
+ * POSIX only).
+ *
+ * With argv, the binary becomes the round-trip harness for the Python tool
+ * test (tests/test_make_qwen36_qpack_snap.py):
+ * test_qwen36_dense_affine <dir> <tensor> <want> <expected.f32>
+ * loads <tensor> through the engine's own load_t_n and compares against a
+ * reference computed INDEPENDENTLY in Python -- the cross-implementation
+ * evidence that the C expansion agrees with the format's producer side.
+ */
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+
+#define main qwen36_main_unused
+#include "../qwen36.c"
+#undef main
+
+#include <sys/stat.h>
+#ifndef _WIN32
+#include <sys/wait.h>
+#endif
+
+#define CHECK(condition) do { \
+ if (!(condition)) { \
+ fprintf(stderr, "%s:%d: check failed: %s\n", \
+ __FILE__, __LINE__, #condition); \
+ exit(1); \
+ } \
+} while (0)
+
+/* ---- fixture: a minimal safetensors file with affine triples ------------- */
+
+typedef struct {
+ const char *name, *dtype, *shape;
+ const void *bytes;
+ size_t nbytes;
+} FixtureTensor;
+
+static void write_fixture(const char *path, const FixtureTensor *tensors, int n) {
+ char header[4096];
+ size_t hlen = 0, off = 0;
+ hlen += (size_t)snprintf(header + hlen, sizeof(header) - hlen, "{");
+ for (int i = 0; i < n; i++) {
+ hlen += (size_t)snprintf(header + hlen, sizeof(header) - hlen,
+ "%s\"%s\":{\"dtype\":\"%s\",\"shape\":%s,"
+ "\"data_offsets\":[%zu,%zu]}",
+ i ? "," : "", tensors[i].name, tensors[i].dtype, tensors[i].shape,
+ off, off + tensors[i].nbytes);
+ off += tensors[i].nbytes;
+ }
+ hlen += (size_t)snprintf(header + hlen, sizeof(header) - hlen, "}");
+ CHECK(hlen < sizeof(header));
+ FILE *f = fopen(path, "wb");
+ CHECK(f != NULL);
+ uint64_t h64 = (uint64_t)hlen;
+ CHECK(fwrite(&h64, 8, 1, f) == 1);
+ CHECK(fwrite(header, 1, hlen, f) == hlen);
+ for (int i = 0; i < n; i++)
+ CHECK(tensors[i].nbytes == 0 ||
+ fwrite(tensors[i].bytes, 1, tensors[i].nbytes, f) == tensors[i].nbytes);
+ CHECK(fclose(f) == 0);
+}
+
+static uint16_t bf16_bits(float value) { /* fixture values are bf16-exact */
+ uint32_t bits;
+ memcpy(&bits, &value, sizeof(bits));
+ return (uint16_t)(bits >> 16);
+}
+
+/* Q4 [2,32] logical, gs=16: per (row,group) scale 0.5*(1+idx), bias
+ * 0.25*idx - 1.0 (all bf16-exact). */
+static const uint32_t q4_words[2][4] = {
+ { 0x76543210u, 0xFEDCBA98u, 0x00000000u, 0x11111111u },
+ { 0xAAAAAAAAu, 0x55555555u, 0x0F0F0F0Fu, 0xF0F0F0F0u },
+};
+static float q4_scale(int r, int g) { return 0.5f * (float)(1 + r * 2 + g); }
+static float q4_bias(int r, int g) { return 0.25f * (float)(r * 2 + g) - 1.0f; }
+
+/* Q8 [3,8] logical, gs=8: one group per row. */
+static const uint32_t q8_words[3][2] = {
+ { 0x03020100u, 0x07060504u },
+ { 0xFF800001u, 0x00000000u },
+ { 0x10204080u, 0x01010101u },
+};
+static float q8_scale(int r) { return 1.0f + 0.5f * (float)r; }
+static float q8_bias(int r) { return -0.5f * (float)r; }
+
+static const float plain_bf16_vals[4] = { 1.0f, -2.0f, 0.5f, 4.0f };
+static const float plain_f32_vals[3] = { 0.25f, -0.5f, 8.0f };
+
+static void build_fixture_dir(const char *dir) {
+#ifdef _WIN32
+ _mkdir(dir);
+#else
+ mkdir(dir, 0700);
+#endif
+ static uint16_t q4_scales[4], q4_biases[4], q8_scales[3], q8_biases[3];
+ static uint16_t plain_bf16[4];
+ for (int r = 0; r < 2; r++) for (int g = 0; g < 2; g++) {
+ q4_scales[r * 2 + g] = bf16_bits(q4_scale(r, g));
+ q4_biases[r * 2 + g] = bf16_bits(q4_bias(r, g));
+ }
+ for (int r = 0; r < 3; r++) {
+ q8_scales[r] = bf16_bits(q8_scale(r));
+ q8_biases[r] = bf16_bits(q8_bias(r));
+ }
+ for (int i = 0; i < 4; i++) plain_bf16[i] = bf16_bits(plain_bf16_vals[i]);
+ static const uint32_t orphan_word[1] = { 0x12345678u };
+ const FixtureTensor tensors[] = {
+ { "language_model.model.aff_q4.weight", "U32", "[2,4]",
+ q4_words, sizeof(q4_words) },
+ { "language_model.model.aff_q4.scales", "BF16", "[2,2]",
+ q4_scales, sizeof(q4_scales) },
+ { "language_model.model.aff_q4.biases", "BF16", "[2,2]",
+ q4_biases, sizeof(q4_biases) },
+ { "language_model.model.aff_q8.weight", "U32", "[3,2]",
+ q8_words, sizeof(q8_words) },
+ { "language_model.model.aff_q8.scales", "BF16", "[3,1]",
+ q8_scales, sizeof(q8_scales) },
+ { "language_model.model.aff_q8.biases", "BF16", "[3,1]",
+ q8_biases, sizeof(q8_biases) },
+ { "language_model.model.plain.weight", "BF16", "[4]",
+ plain_bf16, sizeof(plain_bf16) },
+ { "model.unprefixed.weight", "F32", "[3]",
+ plain_f32_vals, sizeof(plain_f32_vals) },
+ { "language_model.model.orphan.weight", "U32", "[1,1]",
+ orphan_word, sizeof(orphan_word) },
+ };
+ char path[512];
+ snprintf(path, sizeof(path), "%s/model.safetensors", dir);
+ write_fixture(path, tensors, (int)(sizeof(tensors) / sizeof(tensors[0])));
+}
+
+/* ---- gates --------------------------------------------------------------- */
+
+static Model g_dm;
+
+static void test_u32_indexed_with_shape(void) {
+ st_tensor *t = st_find(&g_dm.S, "language_model.model.aff_q4.weight");
+ CHECK(t != NULL);
+ CHECK(t->dtype == 7);
+ CHECK(t->rank == 2 && t->shape[0] == 2 && t->shape[1] == 4);
+ CHECK(t->numel == 8 && t->nbytes == 32);
+}
+
+static void test_q4_expansion(void) {
+ float *w = load_t_n(&g_dm, "model.aff_q4.weight", 2 * 32);
+ for (int r = 0; r < 2; r++) for (int i = 0; i < 32; i++) {
+ int g = i / 16;
+ float q = (float)((q4_words[r][i / 8] >> (4 * (i % 8))) & 0xFu);
+ CHECK(w[r * 32 + i] == q4_scale(r, g) * q + q4_bias(r, g));
+ }
+ free(w);
+}
+
+static void test_q8_expansion(void) {
+ float *w = load_t_n(&g_dm, "model.aff_q8.weight", 3 * 8);
+ for (int r = 0; r < 3; r++) for (int i = 0; i < 8; i++) {
+ float q = (float)((q8_words[r][i / 4] >> (8 * (i % 4))) & 0xFFu);
+ CHECK(w[r * 8 + i] == q8_scale(r) * q + q8_bias(r));
+ }
+ free(w);
+}
+
+static void test_prefix_resolution(void) {
+ CHECK(dense_has(&g_dm, "model.plain.weight"));
+ CHECK(dense_has(&g_dm, "model.unprefixed.weight"));
+ CHECK(!dense_has(&g_dm, "model.absent.weight"));
+ float *p = load_t_n(&g_dm, "model.plain.weight", 4);
+ for (int i = 0; i < 4; i++) CHECK(p[i] == plain_bf16_vals[i]);
+ free(p);
+ /* the plain spelling wins when it exists: this fixture only has the
+ * unprefixed one, and it must load without any prefix probing */
+ float *u = load_t_n(&g_dm, "model.unprefixed.weight", 3);
+ for (int i = 0; i < 3; i++) CHECK(u[i] == plain_f32_vals[i]);
+ free(u);
+}
+
+#ifndef _WIN32
+static void expect_load_death(const char *name, int64_t want) {
+ pid_t pid = fork();
+ CHECK(pid >= 0);
+ if (pid == 0) {
+ if (!freopen("/dev/null", "w", stderr)) { /* keep the noise then */ }
+ float *p = load_t_n(&g_dm, name, want);
+ (void)p;
+ _exit(0); /* reaching here means the refusal did not happen */
+ }
+ int status = 0;
+ CHECK(waitpid(pid, &status, 0) == pid);
+ CHECK(WIFEXITED(status) && WEXITSTATUS(status) != 0);
+}
+
+static void test_refusals(void) {
+ expect_load_death("model.aff_q4.weight", 2 * 32 + 1); /* untileable want */
+ expect_load_death("model.aff_q4.weight", 2 * 12); /* neither Q4 nor Q8 */
+ expect_load_death("model.orphan.weight", 8); /* missing siblings */
+ expect_load_death("model.plain.weight", 5); /* plain wrong size */
+ expect_load_death("model.absent.weight", 4); /* missing tensor */
+}
+#endif
+
+/* ---- Python round-trip harness ------------------------------------------ */
+
+static int compare_mode(const char *dir, const char *tensor, int64_t want,
+ const char *expected_path) {
+ st_init(&g_dm.S, dir);
+ float *got = load_t_n(&g_dm, tensor, want);
+ FILE *f = fopen(expected_path, "rb");
+ if (!f) { perror(expected_path); return 1; }
+ float *expected = falloc(want);
+ if (fread(expected, sizeof(float), (size_t)want, f) != (size_t)want) {
+ fprintf(stderr, "%s: short read\n", expected_path); return 1;
+ }
+ fclose(f);
+ double max_diff = 0.0;
+ for (int64_t i = 0; i < want; i++) {
+ double diff = fabs((double)got[i] - (double)expected[i]);
+ if (diff > max_diff) max_diff = diff;
+ if (diff > 1e-5 * (1.0 + fabs((double)expected[i]))) {
+ fprintf(stderr, "%s[%lld]: got %.9g expected %.9g\n",
+ tensor, (long long)i, got[i], expected[i]);
+ return 1;
+ }
+ }
+ printf("round-trip %s: %lld elements, max |diff| %.3g\n",
+ tensor, (long long)want, max_diff);
+ free(got); free(expected);
+ return 0;
+}
+
+int main(int argc, char **argv) {
+ if (argc == 5)
+ return compare_mode(argv[1], argv[2], atoll(argv[3]), argv[4]);
+ if (argc != 1) {
+ fprintf(stderr, "usage: %s [<dir> <tensor> <want> <expected.f32>]\n",
+ argv[0]);
+ return 2;
+ }
+ const char *dir = "tests/tmp_dense_affine";
+ build_fixture_dir(dir);
+ st_init(&g_dm.S, dir);
+ test_u32_indexed_with_shape();
+ test_q4_expansion();
+ test_q8_expansion();
+ test_prefix_resolution();
+#ifndef _WIN32
+ test_refusals();
+#endif
+ printf("test_qwen36_dense_affine: OK\n");
+ return 0;
+}