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

Add PR2's own new test/bench sources, never committed until now

Jaapjan Tinbergencb7efa9812a2
diff --git a/c/tests/bench_load_tq_peak_rss.c b/c/tests/bench_load_tq_peak_rss.c
new file mode 100644
index 00000000..d3ab1a52
--- /dev/null
+++ b/c/tests/bench_load_tq_peak_rss.c
@@ -0,0 +1,108 @@
+/* Synthetic peak-RSS proxy for the dense-int8-during-load change (QW/load_tq
+ * in qwen36.c). NOT a test gate, NOT an end-to-end model-load benchmark: no
+ * full-size Qwen3.6 checkpoint (~70 GB) is available in this environment, so
+ * this isolates the actual malloc/free pattern the refactor changes, at
+ * matrix sizes representative of Qwen3.6-35B-A3B's dense projections
+ * (hidden=4096-ish square matrices), repeated for a layer count comparable to
+ * a real model (48 dense matrices -- roughly q/k/v/o/gate/shared x 8 layers).
+ *
+ * OLD pattern (qdw_register, pre-#1271-adjacent refactor): allocate+read every
+ * matrix's f32 copy first, THEN make a second pass quantizing all of them --
+ * so all N f32 copies and all N int8 copies are resident at the same time at
+ * the crossover point.
+ * NEW pattern (load_tq): allocate+read one matrix's f32 copy, quantize it,
+ * free the f32 copy, move to the next -- at most one f32 copy is ever
+ * resident alongside the accumulating int8 copies.
+ *
+ * Reports peak RSS (Linux /proc/self/status VmHWM) for both patterns in
+ * separate process runs (VmHWM is monotonic non-decreasing for a process's
+ * lifetime, so OLD and NEW must run as separate processes, not sequentially
+ * in one -- an in-process "old then new" run would report the OLD pattern's
+ * peak forever after). Build on demand:
+ *
+ * make tests/bench_load_tq_peak_rss ARCH=native
+ * ./tests/bench_load_tq_peak_rss old
+ * ./tests/bench_load_tq_peak_rss new
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+#include <stdint.h>
+
+enum { HIDDEN = 4096, OUT = 4096, N_MATRICES = 48 };
+
+static long vmhwm_kb(void) {
+ FILE *f = fopen("/proc/self/status", "r");
+ if (!f) return -1;
+ char line[256]; long kb = -1;
+ while (fgets(line, sizeof line, f)) {
+ if (!strncmp(line, "VmHWM:", 6)) { sscanf(line + 6, "%ld", &kb); break; }
+ }
+ fclose(f);
+ return kb;
+}
+
+static void fill(float *w, int64_t n, int salt) {
+ for (int64_t i = 0; i < n; i++) w[i] = (float)(((i * 2654435761u + salt) % 2003) - 1000) * 0.001f;
+}
+
+static void quantize_row_major(const float *w, int I, int O, int8_t *q, float *sc) {
+ for (int o = 0; o < O; o++) {
+ const float *r = w + (int64_t)o * I; float am = 0.f;
+ for (int i = 0; i < I; i++) { float a = fabsf(r[i]); if (a > am) am = a; }
+ float s = am > 1e-12f ? am / 127.f : 1.f; sc[o] = s; float inv = 1.f / s;
+ int8_t *d = q + (int64_t)o * I;
+ for (int i = 0; i < I; i++) { int v = (int)lrintf(r[i] * inv); if (v > 127) v = 127; if (v < -127) v = -127; d[i] = (int8_t)v; }
+ }
+}
+
+static void run_old(void) {
+ int64_t n = (int64_t)HIDDEN * OUT;
+ float **f32s = malloc(N_MATRICES * sizeof(float*));
+ /* pass 1: "load" every matrix's f32 copy (as model_init_range used to,
+ * for every dense matrix in the whole model, before any quantization) */
+ for (int m = 0; m < N_MATRICES; m++) {
+ f32s[m] = malloc((size_t)n * sizeof(float));
+ fill(f32s[m], n, m);
+ }
+ printf("after loading all %d f32 matrices: VmHWM=%ld MiB\n", N_MATRICES, vmhwm_kb() / 1024);
+ /* pass 2: quantize every matrix (as the old post-hoc qdw_register loop
+ * did), freeing each f32 copy only after ALL quantization is done */
+ int8_t **qs = malloc(N_MATRICES * sizeof(int8_t*));
+ float **scs = malloc(N_MATRICES * sizeof(float*));
+ for (int m = 0; m < N_MATRICES; m++) {
+ qs[m] = malloc((size_t)n);
+ scs[m] = malloc((size_t)OUT * sizeof(float));
+ quantize_row_major(f32s[m], HIDDEN, OUT, qs[m], scs[m]);
+ }
+ printf("after quantizing all (pre-free): VmHWM=%ld MiB\n", vmhwm_kb() / 1024);
+ for (int m = 0; m < N_MATRICES; m++) free(f32s[m]);
+ printf("[old pattern] peak VmHWM=%ld MiB\n", vmhwm_kb() / 1024);
+}
+
+static void run_new(void) {
+ int64_t n = (int64_t)HIDDEN * OUT;
+ int8_t **qs = malloc(N_MATRICES * sizeof(int8_t*));
+ float **scs = malloc(N_MATRICES * sizeof(float*));
+ /* load_tq: read one matrix's f32 copy, quantize it, free it, next matrix */
+ for (int m = 0; m < N_MATRICES; m++) {
+ float *w = malloc((size_t)n * sizeof(float));
+ fill(w, n, m);
+ qs[m] = malloc((size_t)n);
+ scs[m] = malloc((size_t)OUT * sizeof(float));
+ quantize_row_major(w, HIDDEN, OUT, qs[m], scs[m]);
+ free(w);
+ }
+ printf("[new pattern] peak VmHWM=%ld MiB\n", vmhwm_kb() / 1024);
+}
+
+int main(int argc, char **argv) {
+ if (argc != 2 || (strcmp(argv[1], "old") && strcmp(argv[1], "new"))) {
+ fprintf(stderr, "usage: %s old|new\n", argv[0]); return 2;
+ }
+ printf("N_MATRICES=%d each %dx%d f32 (%.1f MiB) -- %s pattern\n",
+ N_MATRICES, HIDDEN, OUT, (double)HIDDEN * OUT * 4 / 1048576.0, argv[1]);
+ if (!strcmp(argv[1], "old")) run_old(); else run_new();
+ return 0;
+}
diff --git a/c/tests/bench_st_f16_bf16_simd.c b/c/tests/bench_st_f16_bf16_simd.c
new file mode 100644
index 00000000..a698f041
--- /dev/null
+++ b/c/tests/bench_st_f16_bf16_simd.c
@@ -0,0 +1,72 @@
+/* Local A/B for st.h's bf16_to_f32_bulk/f16_to_f32_bulk vectorized tiers vs the
+ * scalar per-element reference (bf16_to_f32/f16_to_f32) they replace at the
+ * st_read_f32/st_read_slice_f32 call sites. Not a test gate (see bench_idot,
+ * bench_mla_simd, bench_gemv_stream for the same pattern) -- build on demand:
+ *
+ * make tests/bench_st_f16_bf16_simd ARCH=native
+ * ./tests/bench_st_f16_bf16_simd
+ */
+#include "../st.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
+enum { N = 4 * 1024 * 1024, REPS = 5 };
+
+static double now_s(void) {
+ struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts);
+ return (double)ts.tv_sec + (double)ts.tv_nsec * 1e-9;
+}
+
+static double scalar_bf16(const uint16_t *src, float *dst, int64_t n) {
+ double t0 = now_s();
+ for (int64_t i = 0; i < n; i++) dst[i] = bf16_to_f32(src[i]);
+ return now_s() - t0;
+}
+static double scalar_f16(const uint16_t *src, float *dst, int64_t n) {
+ double t0 = now_s();
+ for (int64_t i = 0; i < n; i++) dst[i] = f16_to_f32(src[i]);
+ return now_s() - t0;
+}
+static double bulk_bf16(const uint16_t *src, float *dst, int64_t n) {
+ double t0 = now_s(); bf16_to_f32_bulk(src, dst, n); return now_s() - t0;
+}
+static double bulk_f16(const uint16_t *src, float *dst, int64_t n) {
+ double t0 = now_s(); f16_to_f32_bulk(src, dst, n); return now_s() - t0;
+}
+
+int main(void) {
+ uint16_t *src = malloc((size_t)N * sizeof(uint16_t));
+ float *a = malloc((size_t)N * sizeof(float));
+ float *b = malloc((size_t)N * sizeof(float));
+ if (!src || !a || !b) { fprintf(stderr, "OOM\n"); return 2; }
+ /* representative mix: mostly normal-range values (real weight magnitudes),
+ * a scattering of exact zeros, occasional subnormal/inf/nan bit patterns --
+ * not the uniform all-65536-once sweep the exactness test uses. */
+ for (int64_t i = 0; i < N; i++) {
+ int64_t m = i % 97;
+ if (m == 0) src[i] = 0;
+ else if (m == 1) src[i] = 0x7C00; /* +inf */
+ else if (m == 2) src[i] = 0x03FF; /* max subnormal */
+ else src[i] = (uint16_t)((i * 2654435761u) & 0x7BFF);
+ }
+
+ printf("st f16/bf16 simd bench: N=%d elements (%.1f MiB src)\n", N, N * sizeof(uint16_t) / 1048576.0);
+
+ double ts = 0, tb = 0;
+ for (int r = 0; r < REPS; r++) {
+ if (r & 1) { tb += bulk_bf16(src, b, N); ts += scalar_bf16(src, a, N); }
+ else { ts += scalar_bf16(src, a, N); tb += bulk_bf16(src, b, N); }
+ }
+ printf("bf16->f32: scalar %.6f s bulk %.6f s speedup %.2fx\n", ts / REPS, tb / REPS, ts / tb);
+
+ ts = 0; tb = 0;
+ for (int r = 0; r < REPS; r++) {
+ if (r & 1) { tb += bulk_f16(src, b, N); ts += scalar_f16(src, a, N); }
+ else { ts += scalar_f16(src, a, N); tb += bulk_f16(src, b, N); }
+ }
+ printf("f16->f32: scalar %.6f s bulk %.6f s speedup %.2fx\n", ts / REPS, tb / REPS, ts / tb);
+
+ free(src); free(a); free(b);
+ return 0;
+}
diff --git a/c/tests/test_qwen36_slot_int8.c b/c/tests/test_qwen36_slot_int8.c
new file mode 100644
index 00000000..58d1c2e6
--- /dev/null
+++ b/c/tests/test_qwen36_slot_int8.c
@@ -0,0 +1,96 @@
+/* Regression gate for wiring slot_ensure_int8() onto #1271's unpack_int4_to_int8
+ * (the same branchless AVX2/NEON/scalar kernel load_expert_merged already uses
+ * for the container's int4 read) instead of its own separate scalar
+ * nibble-unpack loop. Integer nibble extraction + sign-extend is bit-exact by
+ * construction (no reduction, no rounding), so this compares the NEW
+ * slot_ensure_int8 (calling unpack_int4_to_int8) against a copy of the OLD
+ * scalar loop it replaced -- not just "it compiles", a real regression check
+ * that the two nibble-unpack formulas produce identical bytes. */
+#define main qwen36_main_unused
+#include "../qwen36.c"
+#undef main
+
+static int failures;
+#define CHECK(cond, ...) do { if (!(cond)) { \
+ fprintf(stderr,"FAIL %s:%d: ",__FILE__,__LINE__); \
+ fprintf(stderr,__VA_ARGS__); fputc('\n',stderr); failures++; } } while (0)
+
+/* Verbatim copy of slot_ensure_int8's pre-#1271 scalar loop (the code this
+ * change replaces), used here only as the regression oracle. */
+static void old_unpack(int8_t *dst, const uint8_t *p, int64_t len) {
+ for (int64_t i = 0; i < len; i += 2) {
+ uint8_t b = p[i >> 1];
+ int8_t lo = (int8_t)(b & 0xF); if (lo & 8) lo -= 16;
+ int8_t hi = (int8_t)((b >> 4) & 0xF); if (hi & 8) hi -= 16;
+ dst[i] = lo; dst[i + 1] = hi;
+ }
+}
+
+static uint8_t *random_packed(int64_t nbytes, uint32_t seed) {
+ uint8_t *p = malloc((size_t)nbytes);
+ uint32_t x = seed ? seed : 1;
+ for (int64_t i = 0; i < nbytes; i++) {
+ x ^= x << 13; x ^= x >> 17; x ^= x << 5; /* xorshift32 */
+ p[i] = (uint8_t)x;
+ }
+ return p;
+}
+
+/* inter/hidden chosen so ng=inter*hidden and nd=hidden*inter land on both
+ * sides of the AVX2 16-byte (32-element) vector boundary across the three
+ * segments (g/u sized ng, d sized nd). */
+static void one_case(int inter, int hidden, const char *label) {
+ Model m; memset(&m, 0, sizeof(m));
+ m.c.inter = inter; m.c.hidden = hidden;
+ int64_t ng = (int64_t)inter * hidden, nd = (int64_t)hidden * inter;
+
+ Slot s; memset(&s, 0, sizeof(s));
+ s.g4 = random_packed(ng / 2, 1001);
+ s.u4 = random_packed(ng / 2, 2002);
+ s.d4 = random_packed(nd / 2, 3003);
+
+ slot_ensure_int8(&m, &s);
+ CHECK(s.g != NULL, "%s: slot_ensure_int8 did not populate s->g", label);
+ if (!s.g) return;
+
+ int8_t *exp_g = malloc((size_t)ng), *exp_u = malloc((size_t)ng), *exp_d = malloc((size_t)nd);
+ old_unpack(exp_g, s.g4, ng);
+ old_unpack(exp_u, s.u4, ng);
+ old_unpack(exp_d, s.d4, nd);
+
+ CHECK(!memcmp(s.g, exp_g, (size_t)ng), "%s: gate segment differs from old scalar loop", label);
+ CHECK(!memcmp(s.u, exp_u, (size_t)ng), "%s: up segment differs from old scalar loop", label);
+ CHECK(!memcmp(s.d, exp_d, (size_t)nd), "%s: down segment differs from old scalar loop", label);
+ printf("qwen36 slot_ensure_int8 exact: %s inter=%d hidden=%d (ng=%lld nd=%lld)\n",
+ label, inter, hidden, (long long)ng, (long long)nd);
+
+ free(exp_g); free(exp_u); free(exp_d);
+ free(s.g); /* s.u/s.d point into the same block as s.g (slot_ensure_int8's `w`) */
+ free(s.g4); free(s.u4); free(s.d4);
+}
+
+int main(void) {
+ one_case(4, 8, "tiny, well under one vector"); /* ng=32, nd=32 */
+ one_case(16, 16, "exact AVX2 vector boundary"); /* ng=256, nd=256, /16=16 (mult of 16-byte block) */
+ one_case(17, 16, "vector body + scalar tail"); /* ng=272 -> nb=136, not mult of 16 */
+ one_case(37, 41, "odd, real-expert-shaped"); /* ng=1517, nd=1517 */
+ one_case(2048, 512, "large, real Qwen3.6 expert scale"); /* ng=1048576, nd=1048576 */
+
+ /* re-entrancy guard: slot_ensure_int8 must no-op when s->g is already set
+ * (the "rematerialize only if evicted" contract) -- unrelated to which
+ * unpack kernel runs, but worth pinning since this function was touched */
+ {
+ Model m; memset(&m, 0, sizeof(m)); m.c.inter = 4; m.c.hidden = 8;
+ Slot s; memset(&s, 0, sizeof(s));
+ s.g4 = random_packed(16, 42); s.u4 = random_packed(16, 43); s.d4 = random_packed(16, 44);
+ slot_ensure_int8(&m, &s);
+ int8_t *g_before = s.g;
+ slot_ensure_int8(&m, &s); /* should be a no-op: s->g already non-NULL */
+ CHECK(s.g == g_before, "slot_ensure_int8 re-ran when s->g was already set");
+ free(s.g); free(s.g4); free(s.u4); free(s.d4);
+ }
+
+ if (failures) { fprintf(stderr, "qwen36 slot_ensure_int8: %d failure(s)\n", failures); return 1; }
+ puts("qwen36 slot_ensure_int8: ok");
+ return 0;
+}
diff --git a/c/tests/test_st_f16_bf16_simd.c b/c/tests/test_st_f16_bf16_simd.c
new file mode 100644
index 00000000..8ac39bac
--- /dev/null
+++ b/c/tests/test_st_f16_bf16_simd.c
@@ -0,0 +1,76 @@
+/* Exhaustive bit-exact gate for st.h's bf16_to_f32_bulk / f16_to_f32_bulk
+ * (AVX2/SSE4.1/scalar tiers). BF16->F32 and F16->F32 are lossless IEEE-754
+ * widenings: for every one of the 65536 possible input bit patterns -- every
+ * normal and subnormal exponent, +-0, +-inf, and every NaN payload -- there
+ * is exactly one correct output, so there is no tolerance to allow: the
+ * vectorized tiers must produce bit-identical floats to the scalar reference
+ * (bf16_to_f32/f16_to_f32) for the entire input space, not just a sample.
+ *
+ * Runs the full 65536-value sweep (which lands on an exact multiple of both
+ * the AVX2 8-wide and SSE4.1 4-wide vector width) and a set of odd-length,
+ * odd-offset slices of the same data so the scalar remainder tail in each
+ * tier's loop is also exercised, not just the vectorized body. */
+#include "../st.h"
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+static int failures;
+#define CHECK(cond, ...) do { if (!(cond)) { \
+ fprintf(stderr, "FAIL %s:%d: ", __FILE__, __LINE__); \
+ fprintf(stderr, __VA_ARGS__); fputc('\n', stderr); failures++; } } while (0)
+
+static int32_t bits_of(float f) { int32_t u; memcpy(&u, &f, 4); return u; }
+
+static void check_range(const uint16_t *src, int64_t n, const char *label) {
+ float *bulk_bf = malloc((size_t)n * sizeof(float));
+ float *bulk_f16 = malloc((size_t)n * sizeof(float));
+ if (!bulk_bf || !bulk_f16) { fprintf(stderr, "OOM\n"); exit(2); }
+ bf16_to_f32_bulk(src, bulk_bf, n);
+ f16_to_f32_bulk(src, bulk_f16, n);
+ int64_t bf_diffs = 0, f16_diffs = 0;
+ int64_t bf_first = -1, f16_first = -1;
+ for (int64_t i = 0; i < n; i++) {
+ float ref_bf = bf16_to_f32(src[i]);
+ float ref_f16 = f16_to_f32(src[i]);
+ if (bits_of(ref_bf) != bits_of(bulk_bf[i])) { bf_diffs++; if (bf_first < 0) bf_first = i; }
+ if (bits_of(ref_f16) != bits_of(bulk_f16[i])) { f16_diffs++; if (f16_first < 0) f16_first = i; }
+ }
+ if (bf_diffs) fprintf(stderr, " bf16 first mismatch idx=%lld h=0x%04x scalar=0x%08x bulk=0x%08x\n",
+ (long long)bf_first, src[bf_first], bits_of(bf16_to_f32(src[bf_first])), bits_of(bulk_bf[bf_first]));
+ if (f16_diffs) fprintf(stderr, " f16 first mismatch idx=%lld h=0x%04x scalar=0x%08x bulk=0x%08x\n",
+ (long long)f16_first, src[f16_first], bits_of(f16_to_f32(src[f16_first])), bits_of(bulk_f16[f16_first]));
+ CHECK(bf_diffs == 0, "%s: bf16_to_f32_bulk %lld/%lld mismatches", label, (long long)bf_diffs, (long long)n);
+ CHECK(f16_diffs == 0, "%s: f16_to_f32_bulk %lld/%lld mismatches", label, (long long)f16_diffs, (long long)n);
+ free(bulk_bf); free(bulk_f16);
+}
+
+int main(void) {
+ /* every possible 16-bit pattern, once each -- covers +-0, every normal
+ * and subnormal exponent, +-inf, and every NaN payload for both formats */
+ uint16_t *all = malloc(65536 * sizeof(uint16_t));
+ if (!all) { fprintf(stderr, "OOM\n"); return 2; }
+ for (int32_t h = 0; h <= 0xFFFF; h++) all[h] = (uint16_t)h;
+
+ check_range(all, 65536, "full sweep");
+ printf("st f16/bf16 simd: exhaustive 65536/65536 patterns exact\n");
+
+ /* odd offsets/lengths so the scalar remainder tail (n%8 for AVX2, n%4 for
+ * SSE4.1) actually runs, on real (not synthetic-zero) data */
+ static const int64_t offs[] = {0, 1, 2, 3, 5, 7, 9, 13, 15, 16, 17, 100, 12345};
+ static const int64_t lens[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 15, 16, 17, 65423, 65535};
+ for (size_t oi = 0; oi < sizeof(offs)/sizeof(offs[0]); oi++) {
+ for (size_t li = 0; li < sizeof(lens)/sizeof(lens[0]); li++) {
+ int64_t off = offs[oi], len = lens[li];
+ if (off + len > 65536) continue;
+ char label[64]; snprintf(label, sizeof label, "off=%lld len=%lld", (long long)off, (long long)len);
+ check_range(all + off, len, label);
+ }
+ }
+ printf("st f16/bf16 simd: tail-remainder slices ok\n");
+
+ free(all);
+ if (failures) { fprintf(stderr, "st f16/bf16 simd: %d failure(s)\n", failures); return 1; }
+ puts("st f16/bf16 simd: ok");
+ return 0;
+}