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(xdna): add fmt4 weight preparation

Kenneth-Javier Ortigozad5e23fe44f6a
diff --git a/GPU_BACKENDS.md b/GPU_BACKENDS.md
index 57451e5f..ca7d22a7 100644
--- a/GPU_BACKENDS.md
+++ b/GPU_BACKENDS.md
@@ -374,3 +374,62 @@ missing one.
There is still **no fmt4 β†’ BF16 conversion**: filling the destination is the next
slice's work.
+
+### fmt4 to BF16 preparation
+
+The converter turns the authoritative fmt=4 grouped-int4 weight into the prepared
+BF16 image, writing straight into the aligned destination. The source is read and
+never modified.
+
+Semantics are taken from the production kernel (`quant.h`, `matmul_i4_grouped`),
+not from a description of it:
+
+```
+rb = (I+1)/2 bytes per output row
+ng = (I+gs-1)/gs groups per output row
+row = q4 + o*rb weights are stored [O][I]
+scl = scale + o*ng
+byte = row[i>>1]
+nib = (i&1) ? byte>>4 : byte&0x0F even i low nibble, odd i high nibble
+value = (nib - 8) * scl[i/gs]
+```
+
+The destination is BF16 `B[K,N]` with `K = I` and `N = O`, so each value is
+written to `dst[i*O + o]`. The `[O,I]` to `[I,O]` transform is inherent in that
+addressing and needs no intermediate matrix: values pass through a single float
+scalar, so **no full-sized FP32 image is ever allocated**. The only allocation a
+conversion makes is the BF16 destination itself.
+
+Float to BF16 uses round-to-nearest-even, not truncation. The two differ on
+exact ties, and the qualified image rounds.
+
+### Publication and failure
+
+```
+validate source -> begin -> PREPARING -> convert -> publish success -> PREPARED_VALID
+ \
+ -> publish failure -> PREPARED_INVALID
+```
+
+Everything that can be rejected is rejected *before* the cycle opens, so a bad
+source never strands an object in `PREPARING`, and no path returns while still
+`PREPARING`.
+
+A mid-conversion failure leaves genuinely partial bytes β€” some elements
+converted, the rest whatever they held β€” and forces `PREPARED_INVALID`. Those
+bytes carry no authority: state decides validity, not contents, and the partial
+image cannot be published. The authoritative fmt=4 weight is untouched by any of
+this.
+
+A failed buffer keeps its capacity and can be reused, but only through a
+**complete** re-preparation: `INVALID -> PREPARING -> VALID`, never a shortcut.
+A re-prepared image is bit-identical to one prepared from scratch.
+
+The conversion is currently serial. The frozen contract permits parallelisation
+but does not require it, and correctness and publication semantics come first; a
+6144x2048 weight converts in well under a tenth of a second.
+
+Conversion needs no helper, no artifact, no XRT and no device β€” it is engine
+representation logic, and the helper knows nothing about nibble packing or group
+scales. **There is still no XDNA execution**: connecting a real operation to the
+device is the next slice's work.
diff --git a/c/backend_xdna.c b/c/backend_xdna.c
index a0031e77..d472d342 100644
--- a/c/backend_xdna.c
+++ b/c/backend_xdna.c
@@ -690,9 +690,128 @@ unsigned coli_xdna_prepared_n(const ColiXdnaPrepared *p){ return p ? p->n : 0u;
size_t coli_xdna_prepared_total_bytes(void){ return g_xdna_prepared_bytes; }
int coli_xdna_prepared_live_objects(void){ return g_xdna_prepared_objects; }
-/* This slice opens no device, calls no helper entry point, wraps no pointer and
- * converts nothing. The counters make that assertable rather than merely stated. */
+const void *coli_xdna_prepared_image(const ColiXdnaPrepared *p){
+ /* Only a published image is readable. An unpublished or revoked one returns
+ * NULL rather than a pointer to bytes with no authority. */
+ if(!p || p->state != COLI_XDNA_PREP_VALID) return NULL;
+ return p->base;
+}
+const void *coli_xdna_prepared_image_unchecked(const ColiXdnaPrepared *p){
+ return p ? p->base : NULL;
+}
+
+/* ======================================================================
+ * fmt=4 grouped int4 -> BF16 B[K,N]
+ *
+ * Semantics are taken from the production kernel (quant.h matmul_i4_grouped),
+ * not from a specification of it:
+ *
+ * rb = (I+1)/2 bytes per output row
+ * ng = (I+gs-1)/gs groups per output row
+ * row = q4 + o*rb weights are stored [O][I]
+ * scl = scale + o*ng
+ * byte = row[i>>1]
+ * nib = (i&1) ? byte>>4 : byte&0x0F even i low, odd i high
+ * value = (nib - 8) * scl[i/gs]
+ *
+ * The destination is BF16 B[K,N] with K=I and N=O, so the write is
+ * dst[i*O + o] -- the [O,I] -> [I,O] transform is inherent in the addressing
+ * and needs no intermediate matrix. Values are converted one at a time through
+ * a float scalar, so no full-sized FP32 image is ever allocated.
+ * ====================================================================== */
+
+/* Test-only. Production never sets this; there is no env var or flag for it. */
+static int g_xdna_convert_fail_pct;
+/* How many conversions have been performed. I1-I3 pinned this at zero because
+ * no converter existed; from I4 it counts real work, which is what makes
+ * "conversion happened" and "device work happened" separately assertable. */
+static int g_xdna_conversions;
+void coli_xdna_test_set_convert_fail_pct(int pct){
+ g_xdna_convert_fail_pct = (pct > 0 && pct < 100) ? pct : 0;
+}
+
+const char *coli_xdna_prep_result_text(ColiXdnaPrepResult r){
+ switch(r){
+ case COLI_XDNA_PREP_OK: return "OK";
+ case COLI_XDNA_PREP_ERR_UNSUPPORTED_FORMAT:return "UNSUPPORTED_SOURCE_FORMAT";
+ case COLI_XDNA_PREP_ERR_INVALID_SOURCE: return "INVALID_SOURCE";
+ case COLI_XDNA_PREP_ERR_SIZE: return "SIZE_OVERFLOW";
+ case COLI_XDNA_PREP_ERR_ALLOC: return "ALLOCATION_FAILED";
+ case COLI_XDNA_PREP_ERR_FAILED: return "PREPARATION_FAILED";
+ case COLI_XDNA_PREP_ERR_STATE: return "STATE_ERROR";
+ }
+ return "UNKNOWN";
+}
+
+/* float -> bfloat16, round to nearest even. Truncation would be a different
+ * function and a different image; the qualified oracle rounds. */
+static unsigned short coli_xdna_f2b(float f){
+ unsigned int u;
+ memcpy(&u, &f, sizeof u);
+ u += 0x7FFFu + ((u >> 16) & 1u);
+ return (unsigned short)(u >> 16);
+}
+
+ColiXdnaPrepResult coli_xdna_prepare_from_fmt4(ColiXdnaPrepared *p,
+ int fmt,
+ const unsigned char *q4,
+ const float *scale,
+ int I, int O, int gs){
+ if(!p) return COLI_XDNA_PREP_ERR_STATE;
+
+ /* Everything that can be rejected is rejected BEFORE the cycle opens, so a
+ * bad source never leaves an object stranded in PREPARING or INVALID. */
+ if(fmt != 4) return COLI_XDNA_PREP_ERR_UNSUPPORTED_FORMAT;
+ if(!q4 || !scale) return COLI_XDNA_PREP_ERR_INVALID_SOURCE;
+ if(I <= 0 || O <= 0 || gs <= 0) return COLI_XDNA_PREP_ERR_INVALID_SOURCE;
+
+ size_t need = 0;
+ if(!coli_xdna_payload_bytes((unsigned)I, (unsigned)O, COLI_XDNA_DT_BF16, &need))
+ return COLI_XDNA_PREP_ERR_SIZE;
+
+ if(!coli_xdna_prepare_begin(p, (unsigned)I, (unsigned)O, COLI_XDNA_DT_BF16))
+ return COLI_XDNA_PREP_ERR_ALLOC;
+
+ unsigned short *dst = (unsigned short *)coli_xdna_prepare_dest(p);
+ if(!dst){ /* cannot happen; never leave PREPARING */
+ coli_xdna_prepare_publish_failure(p);
+ return COLI_XDNA_PREP_ERR_STATE;
+ }
+
+ const size_t rb = ((size_t)I + 1u) / 2u;
+ const size_t ng = ((size_t)I + (size_t)gs - 1u) / (size_t)gs;
+ const int fail_after = g_xdna_convert_fail_pct
+ ? (int)(((long)O * g_xdna_convert_fail_pct) / 100) : -1;
+
+ for(int o = 0; o < O; o++){
+ if(fail_after >= 0 && o == fail_after){
+ /* Deterministic mid-conversion failure: rows before this one are
+ * genuinely converted, the rest keep whatever they held. */
+ coli_xdna_prepare_publish_failure(p);
+ return COLI_XDNA_PREP_ERR_FAILED;
+ }
+ const unsigned char *row = q4 + (size_t)o * rb;
+ const float *scl = scale + (size_t)o * ng;
+ for(int i = 0; i < I; i++){
+ unsigned char byte = row[(size_t)i >> 1];
+ int nib = (i & 1) ? (int)(byte >> 4) : (int)(byte & 0x0F);
+ dst[(size_t)i * (size_t)O + (size_t)o] =
+ coli_xdna_f2b((float)(nib - 8) * scl[(size_t)i / (size_t)gs]);
+ }
+ }
+
+ if(!coli_xdna_prepare_publish_success(p)){
+ coli_xdna_prepare_publish_failure(p);
+ return COLI_XDNA_PREP_ERR_STATE;
+ }
+ g_xdna_conversions++;
+ return COLI_XDNA_PREP_OK;
+}
+
+/* This slice opens no device, calls no helper entry point and wraps no pointer.
+ * The counters make that assertable rather than merely stated. Conversion is now
+ * implemented, so its counter is no longer pinned at zero. */
int coli_xdna_test_device_opens(void){ return 0; }
int coli_xdna_test_helper_calls(void){ return 0; }
int coli_xdna_test_userptr_wraps(void){ return 0; }
-int coli_xdna_test_conversions(void){ return 0; }
+int coli_xdna_test_conversions(void){ return g_xdna_conversions; }
diff --git a/c/backend_xdna.h b/c/backend_xdna.h
index fd5dea72..c0c87352 100644
--- a/c/backend_xdna.h
+++ b/c/backend_xdna.h
@@ -264,6 +264,44 @@ int coli_xdna_prepared_invalidate(ColiXdnaPrepared *p);
* Validity and allocation lifetime are separate, so this is separate too. */
void coli_xdna_prepared_free_buffer(ColiXdnaPrepared *p);
+/* -- fmt4 -> BF16 preparation ---------------------------------------------
+ *
+ * Turns the authoritative fmt=4 grouped-int4 weight into the prepared BF16
+ * B[K,N] image, writing straight into the aligned destination above. The source
+ * is read and never modified.
+ *
+ * This is engine representation logic and stays on the C side: the helper knows
+ * nothing about nibble packing, group scales or tensor semantics. */
+
+typedef enum {
+ COLI_XDNA_PREP_OK = 0,
+ COLI_XDNA_PREP_ERR_UNSUPPORTED_FORMAT, /* not fmt=4 */
+ COLI_XDNA_PREP_ERR_INVALID_SOURCE, /* NULL pointers, zero dims, bad gs */
+ COLI_XDNA_PREP_ERR_SIZE, /* dimensions not representable */
+ COLI_XDNA_PREP_ERR_ALLOC, /* destination could not be obtained */
+ COLI_XDNA_PREP_ERR_FAILED, /* conversion failed after it began */
+ COLI_XDNA_PREP_ERR_STATE /* no object, or it cannot begin */
+} ColiXdnaPrepResult;
+
+const char *coli_xdna_prep_result_text(ColiXdnaPrepResult r);
+
+/* Prepared dimensions derive from the tensor: K = I, N = O. They are not
+ * caller-supplied, so they cannot silently disagree with the source.
+ *
+ * Publishes VALID only after the whole conversion succeeds. Any failure after
+ * the cycle opens leaves PREPARED_INVALID with whatever partial bytes were
+ * written -- which carry no authority, because state decides validity, not
+ * contents. Never returns while still PREPARING. */
+ColiXdnaPrepResult coli_xdna_prepare_from_fmt4(ColiXdnaPrepared *p,
+ int fmt,
+ const unsigned char *q4,
+ const float *scale,
+ int I, int O, int gs);
+
+/* The prepared image, readable only when the state is VALID. NULL otherwise, so
+ * an unpublished or revoked image cannot be consumed by accident. */
+const void *coli_xdna_prepared_image(const ColiXdnaPrepared *p);
+
ColiXdnaPrepState coli_xdna_prepared_state(const ColiXdnaPrepared *p);
/* Logical payload bytes currently allocated: K*N*sizeof(bf16), not a rounded
* allocator reservation. Non-zero for a retained INVALID buffer. */
@@ -284,6 +322,17 @@ int coli_xdna_prepared_live_objects(void);
/* Install a registry for tests; NULL restores the production table. */
void coli_xdna_test_set_registry(const ColiXdnaArtifact *rows, int count);
+
+/* Deterministic mid-conversion fault injection, for tests only. 0 disables it;
+ * 1..99 fails after approximately that percentage of the conversion's rows.
+ * Named coli_xdna_test_* like every other seam here, and never referenced by
+ * production paths -- there is no environment variable or CLI flag that can
+ * reach it. */
+void coli_xdna_test_set_convert_fail_pct(int pct);
+/* The destination regardless of state, so a test can inspect the partial bytes
+ * a failed conversion left behind. Production code uses
+ * coli_xdna_prepared_image(), which returns NULL unless the image is VALID. */
+const void *coli_xdna_prepared_image_unchecked(const ColiXdnaPrepared *p);
/* All four must stay 0 for the whole of this slice. */
int coli_xdna_test_device_opens(void);
int coli_xdna_test_helper_calls(void);
diff --git a/c/tests/test_xdna_prepared_state.c b/c/tests/test_xdna_prepared_state.c
index b64c7fc5..944e01c6 100644
--- a/c/tests/test_xdna_prepared_state.c
+++ b/c/tests/test_xdna_prepared_state.c
@@ -338,7 +338,325 @@ static void test_no_runtime_readiness(void){
ck(!strcmp(coli_xdna_prep_text(COLI_XDNA_PREP_VALID), "PREPARED_VALID"),
"the label says PREPARED, not READY");
ck(coli_xdna_test_userptr_wraps() == 0, "no userptr wrap exists");
- ck(coli_xdna_test_conversions() == 0, "no fmt4 conversion was performed");
+ /* Conversions DO happen from I4 onward -- the counter now reports real work
+ * rather than being pinned at zero. What must stay zero is device work. */
+ ck(coli_xdna_test_conversions() > 0, "conversions were performed and counted");
+ ck(coli_xdna_test_device_opens() == 0, "still no device opened");
+}
+
+
+/* ---- fmt4 -> BF16 conversion ------------------------------------------- */
+
+/* Independent reference. Deliberately written from the fmt=4 contract rather
+ * than by calling the production converter, so an indexing or layout mistake in
+ * the converter cannot hide by being reproduced here. */
+static unsigned short ref_f2b(float f){
+ unsigned int u; memcpy(&u, &f, 4);
+ u += 0x7FFFu + ((u >> 16) & 1u);
+ return (unsigned short)(u >> 16);
+}
+static void ref_convert(unsigned short *dst, const unsigned char *q4,
+ const float *scale, int I, int O, int gs){
+ int rb = (I + 1) / 2, ng = (I + gs - 1) / gs;
+ for(int o = 0; o < O; o++){
+ const unsigned char *w = q4 + (size_t)o * rb;
+ const float *scl = scale + (size_t)o * ng;
+ for(int i = 0; i < I; i++){
+ unsigned char byte = w[i >> 1];
+ int nib = (i & 1) ? (int)(byte >> 4) : (int)(byte & 0x0F);
+ dst[(size_t)i * O + o] = ref_f2b((float)(nib - 8) * scl[i / gs]);
+ }
+ }
+}
+
+static void fill_src(unsigned char *q4, float *scale, int I, int O, int gs, unsigned seed){
+ int rb = (I + 1) / 2, ng = (I + gs - 1) / gs;
+ unsigned s = seed ? seed : 1u;
+ for(size_t k = 0; k < (size_t)O * rb; k++){ s = s*1664525u+1013904223u; q4[k] = (unsigned char)(s >> 24); }
+ for(size_t k = 0; k < (size_t)O * ng; k++){ s = s*1664525u+1013904223u;
+ scale[k] = 0.25f * (float)(1 + ((s >> 26) & 3)); }
+}
+
+static void test_bf16_rounding(void){
+ printf("BF16 rounding\n");
+ /* The sealed contract is round-to-nearest-even on the 16-bit boundary, not
+ * truncation. These are the cases where the two differ. */
+ union { unsigned int u; float f; } v;
+
+ v.u = 0x00000000u; ck(ref_f2b(v.f) == 0x0000, "+0 -> 0x0000");
+ v.u = 0x80000000u; ck(ref_f2b(v.f) == 0x8000, "-0 -> 0x8000 (sign preserved)");
+ ck(ref_f2b(1.0f) == 0x3F80, "1.0 -> 0x3F80");
+ ck(ref_f2b(-1.0f) == 0xBF80, "-1.0 -> 0xBF80");
+ ck(ref_f2b(2.0f) == 0x4000, "2.0 -> 0x4000");
+
+ /* Exactly halfway: 0x3F808000 rounds to even -> 0x3F80, not up. */
+ v.u = 0x3F808000u; ck(ref_f2b(v.f) == 0x3F80, "tie rounds to even (down)");
+ /* Halfway with an odd LSB rounds up. */
+ v.u = 0x3F818000u; ck(ref_f2b(v.f) == 0x3F82, "tie with odd LSB rounds up");
+ /* Just above halfway always rounds up. */
+ v.u = 0x3F808001u; ck(ref_f2b(v.f) == 0x3F81, "above tie rounds up");
+ /* Truncation would give 0x3F80 for all three; it does not here. */
+
+ /* Every value this converter can actually produce is (nib-8)*scale with
+ * nib-8 in [-8,7] and a positive scale, so it is exactly representable and
+ * rounding never fires in practice -- but the rule must still be correct. */
+ ck(ref_f2b(-8.0f * 0.25f) == ref_f2b(-2.0f), "representative decoded value");
+}
+
+static void test_conversion_matches_reference(void){
+ printf("conversion vs independent reference\n");
+ struct { int I, O, gs; const char *what; } cases[] = {
+ { 64, 4, 64, "one full group, 4 rows" },
+ { 63, 3, 64, "odd I, partial group" },
+ { 65, 2, 64, "I just past a group boundary" },
+ { 128, 5, 64, "two full groups" },
+ { 1, 1, 64, "single element" },
+ { 7, 3, 64, "odd I, several rows" },
+ { 130, 3, 64, "partial trailing group" },
+ };
+ for(size_t c = 0; c < sizeof cases / sizeof cases[0]; c++){
+ int I = cases[c].I, O = cases[c].O, gs = cases[c].gs;
+ int rb = (I + 1) / 2, ng = (I + gs - 1) / gs;
+ unsigned char *q4 = (unsigned char*)malloc((size_t)O * rb);
+ float *scale = (float*)malloc((size_t)O * ng * sizeof(float));
+ fill_src(q4, scale, I, O, gs, 1000u + (unsigned)c);
+
+ unsigned short *want = (unsigned short*)malloc((size_t)I * O * 2);
+ ref_convert(want, q4, scale, I, O, gs);
+
+ ColiXdnaPrepared *p = coli_xdna_prepared_create();
+ ColiXdnaPrepResult r = coli_xdna_prepare_from_fmt4(p, 4, q4, scale, I, O, gs);
+
+ int ok = (r == COLI_XDNA_PREP_OK)
+ && coli_xdna_prepared_state(p) == COLI_XDNA_PREP_VALID
+ && coli_xdna_prepared_bytes(p) == (size_t)I * O * 2
+ && coli_xdna_prepared_k(p) == (unsigned)I
+ && coli_xdna_prepared_n(p) == (unsigned)O;
+ size_t mism = 0;
+ if(ok){
+ const unsigned short *got = (const unsigned short*)coli_xdna_prepared_image(p);
+ for(size_t e = 0; e < (size_t)I * O; e++) if(got[e] != want[e]) mism++;
+ }
+ char msg[128];
+ snprintf(msg, sizeof msg, "%s (I=%d O=%d)", cases[c].what, I, O);
+ ck(ok && mism == 0, msg);
+
+ coli_xdna_prepared_release(&p);
+ free(q4); free(scale); free(want);
+ }
+}
+
+static void test_layout_transpose(void){
+ printf("layout transformation\n");
+ /* A hand-checkable 2x3: source is [O][I], destination must be [I][O]. */
+ int I = 2, O = 3, gs = 64;
+ unsigned char q4[3]; /* rb = 1 byte per row */
+ float scale[3];
+ /* row o: low nibble = element i=0, high nibble = element i=1 */
+ q4[0] = (unsigned char)((10u << 4) | 9u); /* o=0: i0 nib 9, i1 nib 10 */
+ q4[1] = (unsigned char)(( 8u << 4) | 7u); /* o=1: i0 nib 7, i1 nib 8 */
+ q4[2] = (unsigned char)(( 0u << 4) | 15u); /* o=2: i0 nib 15, i1 nib 0 */
+ scale[0] = 1.0f; scale[1] = 2.0f; scale[2] = 0.5f;
+
+ ColiXdnaPrepared *p = coli_xdna_prepared_create();
+ ck(coli_xdna_prepare_from_fmt4(p, 4, q4, scale, I, O, gs) == COLI_XDNA_PREP_OK, "convert");
+ const unsigned short *b = (const unsigned short*)coli_xdna_prepared_image(p);
+
+ /* dst[i*O + o] */
+ ck(b[0*O + 0] == ref_f2b((9.0f - 8.0f) * 1.0f), "B[0][0] = (nib 9 - 8) * 1.0");
+ ck(b[0*O + 1] == ref_f2b((7.0f - 8.0f) * 2.0f), "B[0][1] = (nib 7 - 8) * 2.0");
+ ck(b[0*O + 2] == ref_f2b((15.0f - 8.0f) * 0.5f), "B[0][2] = (nib 15 - 8) * 0.5");
+ ck(b[1*O + 0] == ref_f2b((10.0f - 8.0f) * 1.0f), "B[1][0] = (nib 10 - 8) * 1.0");
+ ck(b[1*O + 1] == ref_f2b(( 8.0f - 8.0f) * 2.0f), "B[1][1] = (nib 8 - 8) * 2.0 = +0");
+ ck(b[1*O + 2] == ref_f2b(( 0.0f - 8.0f) * 0.5f), "B[1][2] = (nib 0 - 8) * 0.5");
+ coli_xdna_prepared_release(&p);
+}
+
+static void test_scale_group_boundary(void){
+ printf("scale group indexing\n");
+ /* gs=64 with I=130: groups are [0,64), [64,128), [128,130). Element 63 must
+ * use scale 0, element 64 scale 1, element 128 scale 2. */
+ int I = 130, O = 1, gs = 64;
+ int rb = (I + 1) / 2, ng = (I + gs - 1) / gs;
+ unsigned char *q4 = (unsigned char*)calloc(1, (size_t)O * rb);
+ float scale[3] = { 1.0f, 4.0f, 16.0f };
+ ck(ng == 3, "three groups for I=130, gs=64");
+ for(int k = 0; k < rb; k++) q4[k] = 0x99; /* every nibble = 9 -> value +1 */
+
+ ColiXdnaPrepared *p = coli_xdna_prepared_create();
+ ck(coli_xdna_prepare_from_fmt4(p, 4, q4, scale, I, O, gs) == COLI_XDNA_PREP_OK, "convert");
+ const unsigned short *b = (const unsigned short*)coli_xdna_prepared_image(p);
+ ck(b[63] == ref_f2b(1.0f), "element 63 uses group 0 scale");
+ ck(b[64] == ref_f2b(4.0f), "element 64 uses group 1 scale");
+ ck(b[127] == ref_f2b(4.0f), "element 127 still group 1");
+ ck(b[128] == ref_f2b(16.0f), "element 128 uses group 2 scale");
+ ck(b[129] == ref_f2b(16.0f), "element 129 in the short trailing group");
+ coli_xdna_prepared_release(&p);
+ free(q4);
+}
+
+static void test_source_rejection(void){
+ printf("source validation\n");
+ int I = 64, O = 2, gs = 64;
+ unsigned char q4[64]; float scale[2];
+ fill_src(q4, scale, I, O, gs, 7u);
+ ColiXdnaPrepared *p = coli_xdna_prepared_create();
+
+ ck(coli_xdna_prepare_from_fmt4(p, 2, q4, scale, I, O, gs)
+ == COLI_XDNA_PREP_ERR_UNSUPPORTED_FORMAT, "fmt=2 rejected");
+ ck(coli_xdna_prepare_from_fmt4(p, 4, NULL, scale, I, O, gs)
+ == COLI_XDNA_PREP_ERR_INVALID_SOURCE, "missing q4 rejected");
+ ck(coli_xdna_prepare_from_fmt4(p, 4, q4, NULL, I, O, gs)
+ == COLI_XDNA_PREP_ERR_INVALID_SOURCE, "missing scales rejected");
+ ck(coli_xdna_prepare_from_fmt4(p, 4, q4, scale, 0, O, gs)
+ == COLI_XDNA_PREP_ERR_INVALID_SOURCE, "I=0 rejected");
+ ck(coli_xdna_prepare_from_fmt4(p, 4, q4, scale, I, 0, gs)
+ == COLI_XDNA_PREP_ERR_INVALID_SOURCE, "O=0 rejected");
+ ck(coli_xdna_prepare_from_fmt4(p, 4, q4, scale, I, O, 0)
+ == COLI_XDNA_PREP_ERR_INVALID_SOURCE, "gs=0 rejected");
+ ck(coli_xdna_prepare_from_fmt4(NULL, 4, q4, scale, I, O, gs)
+ == COLI_XDNA_PREP_ERR_STATE, "NULL prepared object rejected");
+ /* Dimensions are int, so on a 64-bit host the largest reachable product is
+ * about 4.6e18 elements -- below SIZE_MAX/2. A genuine size_t overflow is
+ * therefore UNREACHABLE through this API here, and the ERR_SIZE guard is
+ * defence for platforms with a narrower size_t. What is reachable is an
+ * enormous but representable request, and that must fail as an allocation
+ * failure rather than be misreported as an arithmetic one. */
+ ck(coli_xdna_prepare_from_fmt4(p, 4, q4, scale, 0x40000000, 0x40000000, gs)
+ == COLI_XDNA_PREP_ERR_ALLOC, "unsatisfiable request fails as ALLOC, not SIZE");
+
+ ck(coli_xdna_prepared_state(p) == COLI_XDNA_PREP_UNPREPARED,
+ "every rejection leaves the object UNPREPARED");
+ ck(coli_xdna_prepared_bytes(p) == 0, "and accounts no bytes");
+ coli_xdna_prepared_release(&p);
+}
+
+/* ---- mid-conversion failure -------------------------------------------- */
+#define POISON16 0xDEADu
+
+static void test_injected_failure(void){
+ printf("mid-conversion failure\n");
+ int I = 128, O = 8, gs = 64;
+ int rb = (I + 1) / 2, ng = (I + gs - 1) / gs;
+ unsigned char *q4 = (unsigned char*)malloc((size_t)O * rb);
+ float *scale = (float*)malloc((size_t)O * ng * sizeof(float));
+ fill_src(q4, scale, I, O, gs, 31u);
+ size_t total = (size_t)I * O;
+
+ /* The gold image, and proof the poison value cannot occur in it. */
+ unsigned short *gold = (unsigned short*)malloc(total * 2);
+ ref_convert(gold, q4, scale, I, O, gs);
+ size_t collide = 0;
+ for(size_t e = 0; e < total; e++) if(gold[e] == POISON16) collide++;
+ ck(collide == 0, "poison value cannot occur in a valid image");
+
+ unsigned char q4_before[4096]; float sc_before[64];
+ memcpy(q4_before, q4, (size_t)O * rb);
+ memcpy(sc_before, scale, (size_t)O * ng * sizeof(float));
+
+ const int pct[3] = { 25, 50, 75 };
+ for(int c = 0; c < 3; c++){
+ ColiXdnaPrepared *p = coli_xdna_prepared_create();
+ /* Allocate first so the destination can be poisoned before conversion. */
+ ck(coli_xdna_prepare_begin(p, (unsigned)I, (unsigned)O, COLI_XDNA_DT_BF16) == 1, "begin");
+ unsigned short *d = (unsigned short*)coli_xdna_prepare_dest(p);
+ for(size_t e = 0; e < total; e++) d[e] = POISON16;
+ coli_xdna_prepare_publish_failure(p); /* park it INVALID with capacity */
+
+ coli_xdna_test_set_convert_fail_pct(pct[c]);
+ ColiXdnaPrepResult r = coli_xdna_prepare_from_fmt4(p, 4, q4, scale, I, O, gs);
+ coli_xdna_test_set_convert_fail_pct(0);
+
+ char m[96];
+ snprintf(m, sizeof m, "%d%%: converter reports failure", pct[c]);
+ ck(r == COLI_XDNA_PREP_ERR_FAILED, m);
+ snprintf(m, sizeof m, "%d%%: state is PREPARED_INVALID", pct[c]);
+ ck(coli_xdna_prepared_state(p) == COLI_XDNA_PREP_INVALID, m);
+
+ /* The destination is inspected through the test-only accessor: an
+ * INVALID image exposes no writable destination by contract. */
+ const unsigned short *b = (const unsigned short*)coli_xdna_prepared_image_unchecked(p);
+ size_t written = 0, poison = 0;
+ for(size_t e = 0; e < total; e++){
+ if(b[e] == POISON16) poison++;
+ else if(b[e] == gold[e]) written++;
+ }
+ snprintf(m, sizeof m, "%d%%: %zu converted, %zu poison remain", pct[c], written, poison);
+ ck(written > 0 && poison > 0, m);
+
+ snprintf(m, sizeof m, "%d%%: INVALID cannot publish VALID", pct[c]);
+ ck(coli_xdna_prepare_publish_success(p) == 0, m);
+
+ snprintf(m, sizeof m, "%d%%: fmt4 source unchanged", pct[c]);
+ ck(memcmp(q4, q4_before, (size_t)O * rb) == 0
+ && memcmp(scale, sc_before, (size_t)O * ng * sizeof(float)) == 0, m);
+
+ /* Complete re-preparation over the same allocation. */
+ ck(coli_xdna_prepare_from_fmt4(p, 4, q4, scale, I, O, gs) == COLI_XDNA_PREP_OK,
+ " complete reprepare succeeds");
+ ck(coli_xdna_prepared_state(p) == COLI_XDNA_PREP_VALID, " state VALID again");
+ const unsigned short *g2 = (const unsigned short*)coli_xdna_prepared_image(p);
+ size_t bad = 0, left = 0;
+ for(size_t e = 0; e < total; e++){ if(g2[e] != gold[e]) bad++; if(g2[e] == POISON16) left++; }
+ ck(bad == 0 && left == 0, " reprepared image bit-exact, no residual poison");
+
+ coli_xdna_prepared_release(&p);
+ }
+ free(q4); free(scale); free(gold);
+}
+
+static void test_success_failure_success(void){
+ printf("success -> failure -> success\n");
+ int I = 64, O = 4, gs = 64;
+ int rb = (I + 1) / 2, ng = (I + gs - 1) / gs;
+ unsigned char *q4 = (unsigned char*)malloc((size_t)O * rb);
+ float *scale = (float*)malloc((size_t)O * ng * sizeof(float));
+ fill_src(q4, scale, I, O, gs, 77u);
+ size_t total = (size_t)I * O;
+ unsigned short *gold = (unsigned short*)malloc(total * 2);
+ ref_convert(gold, q4, scale, I, O, gs);
+
+ ColiXdnaPrepared *p = coli_xdna_prepared_create();
+
+ ck(coli_xdna_prepare_from_fmt4(p, 4, q4, scale, I, O, gs) == COLI_XDNA_PREP_OK, "first prepare");
+ ck(memcmp(coli_xdna_prepared_image(p), gold, total * 2) == 0, "first image bit-exact");
+
+ coli_xdna_test_set_convert_fail_pct(50);
+ ck(coli_xdna_prepare_from_fmt4(p, 4, q4, scale, I, O, gs) == COLI_XDNA_PREP_ERR_FAILED,
+ "injected failure on the second attempt");
+ coli_xdna_test_set_convert_fail_pct(0);
+ ck(coli_xdna_prepared_state(p) == COLI_XDNA_PREP_INVALID, "state INVALID");
+ ck(coli_xdna_prepared_image(p) == NULL, "an INVALID image is not readable as prepared");
+
+ ck(coli_xdna_prepare_from_fmt4(p, 4, q4, scale, I, O, gs) == COLI_XDNA_PREP_OK, "third prepare");
+ ck(coli_xdna_prepared_state(p) == COLI_XDNA_PREP_VALID, "state VALID");
+ ck(memcmp(coli_xdna_prepared_image(p), gold, total * 2) == 0,
+ "third image bit-exact, identical to the first");
+
+ coli_xdna_prepared_release(&p);
+ free(q4); free(scale); free(gold);
+}
+
+static void test_no_fp32_image(void){
+ printf("no full FP32 intermediate\n");
+ /* The only allocation a conversion may make is the BF16 destination itself.
+ * Engine-side accounting reports exactly that and nothing more. */
+ int I = 256, O = 16, gs = 64;
+ int rb = (I + 1) / 2, ng = (I + gs - 1) / gs;
+ unsigned char *q4 = (unsigned char*)malloc((size_t)O * rb);
+ float *scale = (float*)malloc((size_t)O * ng * sizeof(float));
+ fill_src(q4, scale, I, O, gs, 5u);
+
+ ColiXdnaPrepared *p = coli_xdna_prepared_create();
+ ck(coli_xdna_prepare_from_fmt4(p, 4, q4, scale, I, O, gs) == COLI_XDNA_PREP_OK, "convert");
+ ck(coli_xdna_prepared_total_bytes() == (size_t)I * O * 2,
+ "accounted bytes are exactly the BF16 destination");
+ ck(coli_xdna_prepared_bytes(p) == (size_t)I * O * 2, "and nothing else was retained");
+ ck(coli_xdna_pointer_alignment_ok(coli_xdna_prepared_image(p)) == 1,
+ "destination still 4096-aligned after conversion");
+ coli_xdna_prepared_release(&p);
+ ck(coli_xdna_prepared_total_bytes() == 0, "released cleanly");
+ free(q4); free(scale);
}
int main(void){
@@ -354,6 +672,14 @@ int main(void){
test_stress();
test_fmt4_immutability();
test_independence();
+ test_bf16_rounding();
+ test_conversion_matches_reference();
+ test_layout_transpose();
+ test_scale_group_boundary();
+ test_source_rejection();
+ test_injected_failure();
+ test_success_failure_success();
+ test_no_fp32_image();
test_no_runtime_readiness();
printf("test_xdna_prepared_state: %s\n", g_fail ? "FAIL" : "ok");
diff --git a/c/tests/test_xdna_qt_state.c b/c/tests/test_xdna_qt_state.c
index 5a36ab89..e4a37ad7 100644
--- a/c/tests/test_xdna_qt_state.c
+++ b/c/tests/test_xdna_qt_state.c
@@ -89,6 +89,48 @@ int main(void){
qt_xdna_reset(&plain);
ck(plain.xdna == NULL, "reset of a tensor with no derived state is safe");
+
+ /* ---- cross-check against the PRODUCTION decode kernel ----------------
+ * The strongest oracle available: quant.h's matmul_i4_grouped is the code
+ * the engine actually computes with. Feeding it a one-hot activation makes
+ * y[o] equal the decoded weight at (o, i), so the prepared image can be
+ * checked against the real kernel rather than against a second copy of my
+ * own reading of the format. */
+ printf("cross-check vs production matmul_i4_grouped\n");
+ {
+ const int I = 70, O = 5, gs = 64; /* odd-ish I, partial 2nd group */
+ const int rb = (I + 1) / 2, ng = (I + gs - 1) / gs;
+ unsigned char *pq4 = (unsigned char*)malloc((size_t)O * rb);
+ float *psc = (float*)malloc((size_t)O * ng * sizeof(float));
+ unsigned st = 4242u;
+ for(size_t k = 0; k < (size_t)O * rb; k++){ st = st*1664525u+1013904223u; pq4[k] = (unsigned char)(st >> 24); }
+ for(size_t k = 0; k < (size_t)O * ng; k++){ st = st*1664525u+1013904223u;
+ psc[k] = 0.25f * (float)(1 + ((st >> 26) & 3)); }
+
+ ColiXdnaPrepared *pp = coli_xdna_prepared_create();
+ ck(coli_xdna_prepare_from_fmt4(pp, 4, pq4, psc, I, O, gs) == COLI_XDNA_PREP_OK,
+ "prepare from fmt4");
+ const unsigned short *img = (const unsigned short*)coli_xdna_prepared_image(pp);
+
+ float *x = (float*)calloc((size_t)I, sizeof(float));
+ float *y = (float*)malloc((size_t)O * sizeof(float));
+ size_t mism = 0;
+ for(int i = 0; i < I; i++){
+ memset(x, 0, (size_t)I * sizeof(float));
+ x[i] = 1.0f; /* one-hot: y[o] = W[o][i] */
+ matmul_i4_grouped(y, x, pq4, psc, 1, I, O, gs);
+ for(int o = 0; o < O; o++){
+ unsigned int u; memcpy(&u, &y[o], 4);
+ u += 0x7FFFu + ((u >> 16) & 1u);
+ unsigned short want = (unsigned short)(u >> 16);
+ if(img[(size_t)i * O + o] != want) mism++;
+ }
+ }
+ ck(mism == 0, "prepared image matches the production kernel decode exactly");
+ free(x); free(y); free(pq4); free(psc);
+ coli_xdna_prepared_release(&pp);
+ }
+
ck(coli_xdna_prepared_live_objects() == 0, "no object leaked");
ck(coli_xdna_prepared_total_bytes() == 0, "no bytes leaked");