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 qualified M256 bucket selection

Kenneth-Javier Ortigoza05cc697d9a7a
diff --git a/c/backend_xdna.c b/c/backend_xdna.c
index 76299a12..097dd04f 100644
--- a/c/backend_xdna.c
+++ b/c/backend_xdna.c
@@ -960,6 +960,53 @@ static ColiXdnaHard g_xdna_last_hard = COLI_XDNA_HARD_ELIGIBLE;
static ColiXdnaExec g_xdna_last_exec = COLI_XDNA_EXEC_DECLINED;
static int g_xdna_dispatches, g_xdna_completions, g_xdna_artifact_opens;
static int g_xdna_act_preps, g_xdna_padded_ops, g_xdna_fallbacks;
+
+/* ---- bucket accounting ---------------------------------------------------
+ *
+ * Two buckets are live, so a single padded_ops total no longer identifies what
+ * happened: it cannot say which artifact ran, and it weights a 24-row pad the
+ * same as a 191-row one. Everything below is indexed by BUCKET SLOT, and the
+ * slot mapping is the one place that knows the bucket set.
+ *
+ * Slots, not artifact_m values, because the accounting must stay O(buckets)
+ * rather than sparse-keyed, and because an unknown artifact_m must be a loud
+ * miss rather than a silently created bin. */
+enum { COLI_XDNA_SLOT_M64 = 0, COLI_XDNA_SLOT_M256 = 1, COLI_XDNA_SLOTS = 2 };
+
+static int coli_xdna_bucket_slot(unsigned artifact_m){
+ if(artifact_m == COLI_XDNA_BUCKET_M_SMALL) return COLI_XDNA_SLOT_M64;
+ if(artifact_m == COLI_XDNA_BUCKET_M_LARGE) return COLI_XDNA_SLOT_M256;
+ return -1; /* not a compiled bucket */
+}
+
+static int g_xdna_b_hard_eligible[COLI_XDNA_SLOTS];
+static int g_xdna_b_dispatches[COLI_XDNA_SLOTS];
+static int g_xdna_b_completions[COLI_XDNA_SLOTS];
+static int g_xdna_b_padded_ops[COLI_XDNA_SLOTS];
+static long long g_xdna_b_padded_rows[COLI_XDNA_SLOTS];
+static int g_xdna_b_artifact_opens[COLI_XDNA_SLOTS];
+static int g_xdna_b_switches[COLI_XDNA_SLOTS][COLI_XDNA_SLOTS];
+static int g_xdna_m_above_range_declines;
+
+/* THE BUCKET SELECTOR.
+ *
+ * The smallest compiled bucket that holds `logical_m`, or 0 if none does.
+ * This is SHAPE ELIGIBILITY, not an economic choice: it has no cost model, no
+ * device-load input and no preference. A caller may not override it, and it may
+ * not round a request up into a bucket that was never built -- there is no
+ * M128 artifact, so an S=65 request runs on M256 with 191 padded rows rather
+ * than on something that does not exist.
+ *
+ * Pure: no globals, no device, no registry. The registry lookup that follows
+ * still has to find a row for the returned bucket, so a bucket named here but
+ * absent from the registry declines at gate 6 exactly as an unknown shape
+ * would. */
+static unsigned coli_xdna_bucket_for_logical_m(int logical_m){
+ if(logical_m < COLI_XDNA_LOGICAL_M_MIN) return 0u;
+ if(logical_m <= (int)COLI_XDNA_BUCKET_M_SMALL) return COLI_XDNA_BUCKET_M_SMALL;
+ if(logical_m <= (int)COLI_XDNA_BUCKET_M_LARGE) return COLI_XDNA_BUCKET_M_LARGE;
+ return 0u; /* above every bucket: decline */
+}
static int g_xdna_device_opens, g_xdna_helper_calls, g_xdna_userptr_wraps;
static int g_xdna_wrapper_reuses, g_xdna_wrapper_releases, g_xdna_stale_rejects;
static int g_xdna_output_valid;
@@ -1027,6 +1074,46 @@ int coli_xdna_test_artifact_opens(void){ return g_xdna_artifact_opens; }
int coli_xdna_test_activation_preparations(void){ return g_xdna_act_preps; }
int coli_xdna_test_padded_operations(void){ return g_xdna_padded_ops; }
int coli_xdna_test_fallbacks(void){ return g_xdna_fallbacks; }
+
+/* Per-bucket accessors. An unrecognised artifact M returns 0 -- it is not a
+ * bucket this build compiled, so there is nothing to report and nothing may be
+ * silently folded into a neighbouring bin. */
+int coli_xdna_test_bucket_dispatches(unsigned artifact_m){
+ int sl = coli_xdna_bucket_slot(artifact_m); return sl < 0 ? 0 : g_xdna_b_dispatches[sl];
+}
+int coli_xdna_test_bucket_completions(unsigned artifact_m){
+ int sl = coli_xdna_bucket_slot(artifact_m); return sl < 0 ? 0 : g_xdna_b_completions[sl];
+}
+int coli_xdna_test_bucket_padded_operations(unsigned artifact_m){
+ int sl = coli_xdna_bucket_slot(artifact_m); return sl < 0 ? 0 : g_xdna_b_padded_ops[sl];
+}
+long long coli_xdna_test_bucket_padded_rows(unsigned artifact_m){
+ int sl = coli_xdna_bucket_slot(artifact_m); return sl < 0 ? 0 : g_xdna_b_padded_rows[sl];
+}
+int coli_xdna_test_bucket_artifact_opens(unsigned artifact_m){
+ int sl = coli_xdna_bucket_slot(artifact_m); return sl < 0 ? 0 : g_xdna_b_artifact_opens[sl];
+}
+int coli_xdna_test_bucket_hard_eligible(unsigned artifact_m){
+ int sl = coli_xdna_bucket_slot(artifact_m); return sl < 0 ? 0 : g_xdna_b_hard_eligible[sl];
+}
+int coli_xdna_test_m_above_range_declines(void){ return g_xdna_m_above_range_declines; }
+int coli_xdna_test_bucket_switches(unsigned from_m, unsigned to_m){
+ int a = coli_xdna_bucket_slot(from_m), b = coli_xdna_bucket_slot(to_m);
+ return (a < 0 || b < 0) ? 0 : g_xdna_b_switches[a][b];
+}
+unsigned coli_xdna_test_bucket_for(int logical_m){
+ return coli_xdna_bucket_for_logical_m(logical_m);
+}
+void coli_xdna_test_reset_bucket_counters(void){
+ memset(g_xdna_b_hard_eligible, 0, sizeof g_xdna_b_hard_eligible);
+ memset(g_xdna_b_dispatches, 0, sizeof g_xdna_b_dispatches);
+ memset(g_xdna_b_completions, 0, sizeof g_xdna_b_completions);
+ memset(g_xdna_b_padded_ops, 0, sizeof g_xdna_b_padded_ops);
+ memset(g_xdna_b_padded_rows, 0, sizeof g_xdna_b_padded_rows);
+ memset(g_xdna_b_artifact_opens, 0, sizeof g_xdna_b_artifact_opens);
+ memset(g_xdna_b_switches, 0, sizeof g_xdna_b_switches);
+ g_xdna_m_above_range_declines = 0;
+}
int coli_xdna_test_wrapper_reuses(void){ return g_xdna_wrapper_reuses; }
int coli_xdna_test_wrapper_releases(void){ return g_xdna_wrapper_releases; }
int coli_xdna_test_stale_wrapper_rejects(void){ return g_xdna_stale_rejects; }
@@ -1061,9 +1148,18 @@ static ColiXdnaHard coli_xdna_engine_gates(ColiXdnaFamily family,
if(family != COLI_XDNA_FAMILY_MOE_SHARED_GATE_UP)
return COLI_XDNA_HARD_FAMILY_UNSUPPORTED;
- /* 2 logical M in the range this slice implements */
- if(S < COLI_XDNA_I5_LOGICAL_M_MIN || S > COLI_XDNA_I5_LOGICAL_M_MAX)
+ /* 2 logical M within the range some compiled bucket serves.
+ *
+ * The upper bound is now the LARGEST bucket, not the only one. Below
+ * the minimum and above the largest are the same verdict to the caller
+ * -- M_OUT_OF_RANGE, decline to the current path -- but the
+ * above-the-range case is counted separately, because it is the new
+ * first-decline boundary and a later slice will want to know how often
+ * real work lands there. */
+ if(S < COLI_XDNA_LOGICAL_M_MIN || S > COLI_XDNA_LOGICAL_M_MAX){
+ if(S > COLI_XDNA_LOGICAL_M_MAX) g_xdna_m_above_range_declines++;
return COLI_XDNA_HARD_M_OUT_OF_RANGE;
+ }
/* 3 K / N representable and positive */
if(I <= 0 || O <= 0) return COLI_XDNA_HARD_SHAPE_UNSUPPORTED;
@@ -1092,7 +1188,8 @@ static ColiXdnaHard coli_xdna_engine_gates(ColiXdnaFamily family,
* The bucket is the artifact's M, which is where logical M is mapped. */
ColiXdnaRequest q;
q.family = family;
- q.m = (unsigned)COLI_XDNA_I5_LOGICAL_M_MAX; /* the F3 M64 bucket */
+ q.m = coli_xdna_bucket_for_logical_m(S); /* smallest bucket holding S */
+ if(q.m == 0u) return COLI_XDNA_HARD_M_OUT_OF_RANGE; /* unreachable: gate 2 */
q.k = (unsigned)I; q.n = (unsigned)O;
q.in_dtype = COLI_XDNA_DT_BF16;
q.weight_dtype = COLI_XDNA_DT_BF16;
@@ -1231,6 +1328,15 @@ static ColiXdnaExec coli_xdna_attempt(ColiXdnaFamily family,
g_xdna_last_hard = COLI_XDNA_HARD_ARTIFACT_RUNTIME_UNAVAILABLE;
return (g_xdna_last_exec = COLI_XDNA_EXEC_ARTIFACT_OPEN_FAILED);
}
+ /* Record the transition BEFORE tearing the old artifact down, while
+ * g_lane.m still names the bucket being left. A bucket switch is the
+ * expensive case (shutdown + open + forced re-wrap), so it is counted
+ * rather than inferred from artifact_opens, which also fires on the
+ * very first open and on a K/N change. */
+ if(g_lane.open){
+ int from = coli_xdna_bucket_slot(g_lane.m), to = coli_xdna_bucket_slot(am);
+ if(from >= 0 && to >= 0 && from != to) g_xdna_b_switches[from][to]++;
+ }
if(g_lane.open){ coli_xdna_lane_drop_wrapper(); coli_xdna_helper_shutdown_call(); }
g_lane.open = 0;
g_xdna_helper_calls++; g_xdna_device_opens++;
@@ -1252,6 +1358,7 @@ static ColiXdnaExec coli_xdna_attempt(ColiXdnaFamily family,
g_lane.wrapped = NULL; g_lane.wrapped_gen = 0;
g_xdna_lane_health = COLI_XDNA_LANE_HEALTHY;
g_xdna_artifact_opens++;
+ { int sl = coli_xdna_bucket_slot(am); if(sl >= 0) g_xdna_b_artifact_opens[sl]++; }
}
/* 14 userptr wrap, keyed on (pointer, generation).
@@ -1281,6 +1388,7 @@ static ColiXdnaExec coli_xdna_attempt(ColiXdnaFamily family,
}
g_xdna_last_hard = COLI_XDNA_HARD_ELIGIBLE; /* FULL_XDNA_HARD_ELIGIBLE */
+ { int sl = coli_xdna_bucket_slot(am); if(sl >= 0) g_xdna_b_hard_eligible[sl]++; }
/* ---- activation staging ------------------------------------------------- */
size_t abytes = (size_t)am * (size_t)I * 2u;
@@ -1295,6 +1403,12 @@ static ColiXdnaExec coli_xdna_attempt(ColiXdnaFamily family,
memset(g_lane.act + (size_t)S * (size_t)I, 0,
((size_t)am - (size_t)S) * (size_t)I * 2u);
g_xdna_padded_ops++;
+ /* ROWS as well as OPS: with two buckets the op count no longer says how
+ * much padding was computed. An S=65 operation on M256 pads 191 rows;
+ * an S=63 operation on M64 pads 1. Both are one "padded op". */
+ { int sl = coli_xdna_bucket_slot(am);
+ if(sl >= 0){ g_xdna_b_padded_ops[sl]++;
+ g_xdna_b_padded_rows[sl] += (long long)(am - (unsigned)S); } }
}
g_xdna_act_preps++;
@@ -1312,6 +1426,8 @@ static ColiXdnaExec coli_xdna_attempt(ColiXdnaFamily family,
: COLI_XDNA_EXEC_EXECUTE_FAILED);
}
g_xdna_dispatches++; g_xdna_completions++;
+ { int sl = coli_xdna_bucket_slot(am);
+ if(sl >= 0){ g_xdna_b_dispatches[sl]++; g_xdna_b_completions[sl]++; } }
memcpy(y, g_lane.out, (size_t)S * (size_t)O * 4u); /* logical rows only */
g_xdna_output_valid = 1;
diff --git a/c/backend_xdna.h b/c/backend_xdna.h
index 915256e0..984a64c7 100644
--- a/c/backend_xdna.h
+++ b/c/backend_xdna.h
@@ -389,7 +389,7 @@ const char *coli_xdna_hard_text(ColiXdnaHard h);
* here, because no artifact was ever correctness-qualified against one. */
#define COLI_XDNA_QUALIFIED_GROUP_SIZE 64
-/* The logical-M range this slice implements, against the F3 M64 artifact.
+/* The logical-M range this lane implements, and the artifact buckets serving it.
*
* Row padding is legitimate because C = A x B is row-independent: output row i
* depends only on input row i and on B. N6-A2-A1A qualified the strategy
@@ -398,11 +398,28 @@ const char *coli_xdna_hard_text(ColiXdnaHard h);
* property of PURE GEMM and must never be extended to attention, normalisation
* or routing.
*
- * Row TILING (logical M above the bucket) is equally qualified as a strategy
- * and deliberately NOT implemented here: the smallest first seam is one
- * dispatch. Anything above the range declines to the current path. */
-#define COLI_XDNA_I5_LOGICAL_M_MIN 1
-#define COLI_XDNA_I5_LOGICAL_M_MAX 64
+ * Row TILING (logical M above the largest bucket) is equally qualified as a
+ * strategy and deliberately NOT implemented: one dispatch, one artifact.
+ * Anything above the largest bucket declines to the current path.
+ *
+ * TWO buckets are served, and they are BUCKETS, not a range to interpolate
+ * across. An M bucket is a separately compiled program: N6 saw `wa F6 M256`
+ * fail to compile where its M64 sibling compiled, so a bucket that was never
+ * built cannot be synthesised by rounding. Selection is therefore an exact
+ * lookup of the smallest qualified bucket that holds the logical rows, and a
+ * request between the buckets uses the larger one with zero padding -- never an
+ * unbuilt M128. */
+#define COLI_XDNA_LOGICAL_M_MIN 1
+#define COLI_XDNA_BUCKET_M_SMALL 64
+#define COLI_XDNA_BUCKET_M_LARGE 256
+#define COLI_XDNA_LOGICAL_M_MAX COLI_XDNA_BUCKET_M_LARGE
+
+/* Kept as the historical I5 names so the qualified-range vocabulary of the
+ * earlier slices still resolves. _MAX was BOTH the range end and the bucket
+ * when only one bucket existed; M1 split those meanings, and the name that
+ * used to mean both now means only the small bucket. */
+#define COLI_XDNA_I5_LOGICAL_M_MIN COLI_XDNA_LOGICAL_M_MIN
+#define COLI_XDNA_I5_LOGICAL_M_MAX COLI_XDNA_BUCKET_M_SMALL
typedef enum {
COLI_XDNA_EXEC_OK = 0,
@@ -532,6 +549,38 @@ int coli_xdna_test_artifact_opens(void);
int coli_xdna_test_activation_preparations(void);
int coli_xdna_test_padded_operations(void);
int coli_xdna_test_fallbacks(void); /* candidate returned 0 */
+
+/* PER-BUCKET accounting.
+ *
+ * With one bucket, a single padded_ops total was unambiguous. With two it is
+ * not: an S=200 operation padded to 256 and an S=40 operation padded to 64 are
+ * both "padded" and waste 56 rows versus 24. Worse, the aggregate cannot say
+ * WHICH artifact ran. Every accessor below takes the artifact M so the caller
+ * names the bucket it is asking about; an unknown bucket returns 0 rather than
+ * silently aggregating.
+ *
+ * padded_ROWS is reported as well as padded_OPS because the two answer
+ * different questions: how many operations padded at all, and how much padding
+ * was actually computed. Only the second scales with the waste.
+ *
+ * These are qualification diagnostics, not a product interface. */
+int coli_xdna_test_bucket_dispatches(unsigned artifact_m);
+int coli_xdna_test_bucket_completions(unsigned artifact_m);
+int coli_xdna_test_bucket_padded_operations(unsigned artifact_m);
+long long coli_xdna_test_bucket_padded_rows(unsigned artifact_m);
+int coli_xdna_test_bucket_artifact_opens(unsigned artifact_m);
+int coli_xdna_test_bucket_hard_eligible(unsigned artifact_m);
+/* Logical M above the largest qualified bucket: the new first-decline class. */
+int coli_xdna_test_m_above_range_declines(void);
+/* Bucket transitions actually taken by the lane, counted on artifact reopen. */
+int coli_xdna_test_bucket_switches(unsigned from_m, unsigned to_m);
+/* The bucket the lane would select for a logical M, or 0 if none serves it.
+ * Pure function of the compiled bucket set; touches no device and no registry. */
+unsigned coli_xdna_test_bucket_for(int logical_m);
+/* Zero every per-bucket counter. Tests measure one scenario at a time, and a
+ * cumulative total silently turns "this scenario dispatched once" into "some
+ * earlier scenario also dispatched". */
+void coli_xdna_test_reset_bucket_counters(void);
/* Wrapper accounting. stale_rejects counts the case that made I6 necessary:
* the same address carrying a NEW image, which must force a re-wrap rather
* than be reused. */
diff --git a/c/tests/test_xdna_execution.c b/c/tests/test_xdna_execution.c
index ae837ab7..654f28ef 100644
--- a/c/tests/test_xdna_execution.c
+++ b/c/tests/test_xdna_execution.c
@@ -28,13 +28,14 @@ static void ck(int cond, const char *what){
#define TK 256 /* K -- small so the CPU GEMM in the fake stays quick */
#define TN 128 /* N */
-#define TM 64 /* artifact M, the bucket this slice implements */
+#define TM 64 /* the small artifact bucket */
+#define TM2 256 /* the large artifact bucket (M1) */
static char g_root[1024];
static char g_helper[1024], g_helper_abi1[1024], g_helper_partial[1024];
static char g_xclbin[2048], g_insts[2048];
static char g_xhex[65], g_ihex[65];
-static ColiXdnaArtifact g_test_rows[1];
+static ColiXdnaArtifact g_test_rows[2];
static void hexify(const unsigned char h[32], char out[65]){
static const char *H = "0123456789abcdef";
@@ -76,9 +77,23 @@ static void build_registry(void){
g_test_rows[0].correctness_qualified = 1;
g_test_rows[0].userptr_qualified = 1;
g_test_rows[0].structural_qualified = 1;
- coli_xdna_test_set_registry(g_test_rows, 1);
+
+ /* M1: the large bucket. Same family, same K/N, same dtypes, same artifact
+ * bytes -- only artifact_m differs. Sharing the fixture bytes is deliberate:
+ * the fake helper is generic in m, so any behavioural difference between the
+ * two rows can only come from the bucket, not from the artifact. */
+ g_test_rows[1] = g_test_rows[0];
+ g_test_rows[1].artifact_m = TM2;
+
+ coli_xdna_test_set_registry(g_test_rows, 2);
}
+/* Install only the SMALL bucket, so a request that selects M256 finds no row.
+ * This is the "bucket named by the selector but absent from the registry" case:
+ * it must decline, never fall back to a different bucket. */
+static void registry_small_only(void){ coli_xdna_test_set_registry(g_test_rows, 1); }
+static void registry_both(void){ coli_xdna_test_set_registry(g_test_rows, 2); }
+
/* A deterministic fmt4 tensor with I=K, O=N. */
typedef struct { unsigned char *q4; float *s; int I, O, gs; } Fmt4;
@@ -134,6 +149,7 @@ static void lane_reset(void){
g_xdna_dispatches = g_xdna_completions = g_xdna_artifact_opens = 0;
g_xdna_act_preps = g_xdna_padded_ops = g_xdna_fallbacks = 0;
g_xdna_device_opens = g_xdna_helper_calls = g_xdna_userptr_wraps = 0;
+ coli_xdna_test_reset_bucket_counters();
}
/* Rebind the loader to a helper path and clear its sticky verdict. */
@@ -259,7 +275,7 @@ int main(int argc, char **argv){
COLI_XDNA_HARD_GROUP_SIZE_UNSUPPORTED },
{ "fmt != 4", COLI_XDNA_FAMILY_MOE_SHARED_GATE_UP, 2, TK, TN, 64, TM,
COLI_XDNA_HARD_FORMAT_UNSUPPORTED },
- { "logical M above the qualified range", COLI_XDNA_FAMILY_MOE_SHARED_GATE_UP, 4, TK, TN, 64, TM+1,
+ { "logical M above EVERY bucket", COLI_XDNA_FAMILY_MOE_SHARED_GATE_UP, 4, TK, TN, 64, TM2+1,
COLI_XDNA_HARD_M_OUT_OF_RANGE },
{ "logical M below the qualified range", COLI_XDNA_FAMILY_MOE_SHARED_GATE_UP, 4, TK, TN, 64, 0,
COLI_XDNA_HARD_M_OUT_OF_RANGE },
@@ -505,6 +521,181 @@ int main(int argc, char **argv){
coli_xdna_prepared_release(&slot); free(x); free(y);
}
+ /* ==================================================================
+ * M1 -- DUAL BUCKET SELECTION
+ * ================================================================== */
+ printf("M1 bucket selection -- which artifact serves which logical M\n");
+ {
+ /* The selector is pure, so assert it directly before asserting the
+ * behaviour it drives. If these disagree with the execution results
+ * below, the bug is in the wiring, not in the rule. */
+ struct { int m; unsigned want; const char *what; } sel[] = {
+ { 0, 0u, "M=0 -> no bucket" },
+ { 1, (unsigned)TM, "M=1 -> small" },
+ { 63, (unsigned)TM, "M=63 -> small" },
+ { 64, (unsigned)TM, "M=64 -> small, exact fill" },
+ { 65, (unsigned)TM2, "M=65 -> large (there is no M128 artifact)" },
+ { 66, (unsigned)TM2, "M=66 -> large" },
+ { 128, (unsigned)TM2, "M=128 -> large" },
+ { 255, (unsigned)TM2, "M=255 -> large" },
+ { 256, (unsigned)TM2, "M=256 -> large, exact fill" },
+ { 257, 0u, "M=257 -> no bucket" },
+ { 1000, 0u, "M=1000 -> no bucket" }
+ };
+ for(size_t i=0;i<sizeof sel/sizeof sel[0];i++)
+ ck(coli_xdna_test_bucket_for(sel[i].m)==sel[i].want, sel[i].what);
+ }
+
+ printf("M1 execution per bucket -- dispatch, padding rows, copyback\n");
+ {
+ registry_both();
+ struct { int S; unsigned bucket; long long pad_rows; } cases[] = {
+ { 1, (unsigned)TM, (long long)TM-1 },
+ { 63, (unsigned)TM, 1 },
+ { 64, (unsigned)TM, 0 },
+ { 65, (unsigned)TM2, (long long)TM2-65 },
+ { 128, (unsigned)TM2, (long long)TM2-128 },
+ { 255, (unsigned)TM2, 1 },
+ { 256, (unsigned)TM2, 0 }
+ };
+ for(size_t c=0;c<sizeof cases/sizeof cases[0];c++){
+ int S = cases[c].S; unsigned bk = cases[c].bucket;
+ unsigned other = (bk==(unsigned)TM) ? (unsigned)TM2 : (unsigned)TM;
+ use_helper(g_helper);
+ coli_xdna_test_set_artifact_root(g_root);
+ coli_xdna_test_set_force_execution(1);
+ ColiXdnaPrepared *slot = NULL;
+ float *x = mk_x(S, TK, 4242u + (unsigned)S);
+ size_t want = (size_t)S*TN, guard = TN;
+ float *y = (float*)malloc((want+guard)*sizeof(float));
+ poison(y, want+guard);
+ ColiXdnaExec e = coli_xdna_test_attempt(COLI_XDNA_FAMILY_MOE_SHARED_GATE_UP,
+ &slot, 4, W.q4, W.s, TK, TN, 64, 0, y, x, S);
+ char msg[192];
+ snprintf(msg,sizeof msg,"S=%d -> OK on M%u", S, bk);
+ ck(e==COLI_XDNA_EXEC_OK, msg);
+ snprintf(msg,sizeof msg,"S=%d -> 1 dispatch on M%u", S, bk);
+ ck(coli_xdna_test_bucket_dispatches(bk)==1, msg);
+ snprintf(msg,sizeof msg,"S=%d -> 0 dispatches on the other bucket M%u", S, other);
+ ck(coli_xdna_test_bucket_dispatches(other)==0, msg);
+ snprintf(msg,sizeof msg,"S=%d -> completions match dispatches", S);
+ ck(coli_xdna_test_bucket_completions(bk)==1, msg);
+ snprintf(msg,sizeof msg,"S=%d -> padded rows = %lld on M%u", S, cases[c].pad_rows, bk);
+ ck(coli_xdna_test_bucket_padded_rows(bk)==cases[c].pad_rows, msg);
+ snprintf(msg,sizeof msg,"S=%d -> padded ops = %d", S, cases[c].pad_rows?1:0);
+ ck(coli_xdna_test_bucket_padded_operations(bk)==(cases[c].pad_rows?1:0), msg);
+ int guard_ok = 1;
+ for(size_t i=0;i<guard;i++) if(y[want+i]!=POISON){ guard_ok=0; break; }
+ snprintf(msg,sizeof msg,"S=%d -> guard past %zu floats untouched (no artifact rows escaped)", S, want);
+ ck(guard_ok, msg);
+ int wrote_all = 1;
+ for(size_t i=0;i<want;i++) if(y[i]==POISON){ wrote_all=0; break; }
+ snprintf(msg,sizeof msg,"S=%d -> all %zu logical floats written", S, want);
+ ck(wrote_all, msg);
+ float *REFS = (float*)malloc(want*sizeof(float));
+ oracle(REFS, x, coli_xdna_prepared_image(slot), S, TK, TN);
+ int exact = 1;
+ for(size_t i=0;i<want;i++) if(y[i]!=REFS[i]){ exact=0; break; }
+ snprintf(msg,sizeof msg,"S=%d -> matches the BF16 oracle exactly on M%u", S, bk);
+ ck(exact, msg);
+ free(REFS);
+ coli_xdna_prepared_release(&slot); free(x); free(y);
+ }
+ }
+
+ printf("M1 above every bucket -- the new first decline\n");
+ {
+ registry_both();
+ use_helper(g_helper);
+ coli_xdna_test_set_artifact_root(g_root);
+ coli_xdna_test_set_force_execution(1);
+ ColiXdnaPrepared *slot = NULL;
+ int S = TM2+1;
+ float *x = mk_x(S, TK, 9u);
+ float *y = (float*)malloc((size_t)S*TN*sizeof(float));
+ poison(y, (size_t)S*TN);
+ ColiXdnaExec e = coli_xdna_test_attempt(COLI_XDNA_FAMILY_MOE_SHARED_GATE_UP,
+ &slot, 4, W.q4, W.s, TK, TN, 64, 0, y, x, S);
+ ck(e==COLI_XDNA_EXEC_DECLINED, "S=257 -> DECLINED");
+ ck(coli_xdna_test_last_hard()==COLI_XDNA_HARD_M_OUT_OF_RANGE, "S=257 -> M_OUT_OF_RANGE");
+ ck(coli_xdna_test_bucket_dispatches((unsigned)TM)==0, "S=257 -> no small-bucket dispatch");
+ ck(coli_xdna_test_bucket_dispatches((unsigned)TM2)==0, "S=257 -> no large-bucket dispatch");
+ ck(coli_xdna_test_m_above_range_declines()>0, "S=257 -> counted as above-range");
+ int untouched = 1;
+ for(size_t i=0;i<(size_t)S*TN;i++) if(y[i]!=POISON){ untouched=0; break; }
+ ck(untouched, "S=257 -> caller output untouched");
+ coli_xdna_prepared_release(&slot); free(x); free(y);
+ }
+
+ printf("M1 selector names a bucket the registry does not hold\n");
+ {
+ registry_small_only();
+ use_helper(g_helper);
+ coli_xdna_test_set_artifact_root(g_root);
+ coli_xdna_test_set_force_execution(1);
+ ColiXdnaPrepared *slot = NULL;
+ int S = 65;
+ float *x = mk_x(S, TK, 11u);
+ float *y = (float*)malloc((size_t)S*TN*sizeof(float));
+ poison(y, (size_t)S*TN);
+ ColiXdnaExec e = coli_xdna_test_attempt(COLI_XDNA_FAMILY_MOE_SHARED_GATE_UP,
+ &slot, 4, W.q4, W.s, TK, TN, 64, 0, y, x, S);
+ ck(e==COLI_XDNA_EXEC_DECLINED, "S=65 with no M256 row -> DECLINED");
+ ck(coli_xdna_test_last_hard()==COLI_XDNA_HARD_SHAPE_UNSUPPORTED,
+ "S=65 with no M256 row -> SHAPE_UNSUPPORTED, not a silent downgrade");
+ ck(coli_xdna_test_bucket_dispatches((unsigned)TM)==0,
+ "S=65 with no M256 row -> the small bucket was NOT used instead");
+ int untouched = 1;
+ for(size_t i=0;i<(size_t)S*TN;i++) if(y[i]!=POISON){ untouched=0; break; }
+ ck(untouched, "S=65 with no M256 row -> caller output untouched");
+ coli_xdna_prepared_release(&slot); free(x); free(y);
+ registry_both();
+ }
+
+ printf("M1 bucket switching -- one prepared weight, two artifacts\n");
+ {
+ registry_both();
+ use_helper(g_helper);
+ coli_xdna_test_set_artifact_root(g_root);
+ coli_xdna_test_set_force_execution(1);
+ ColiXdnaPrepared *slot = NULL;
+ int seq[] = { 64, 65, 64, 130, 256, 64 };
+ for(size_t i=0;i<sizeof seq/sizeof seq[0];i++){
+ int S = seq[i];
+ unsigned bk = coli_xdna_test_bucket_for(S);
+ float *x = mk_x(S, TK, 700u+(unsigned)i);
+ float *y = (float*)malloc((size_t)S*TN*sizeof(float));
+ poison(y, (size_t)S*TN);
+ ColiXdnaExec e = coli_xdna_test_attempt(COLI_XDNA_FAMILY_MOE_SHARED_GATE_UP,
+ &slot, 4, W.q4, W.s, TK, TN, 64, 0, y, x, S);
+ char msg[192];
+ snprintf(msg,sizeof msg,"switch step %zu (S=%d) -> OK", i, S);
+ ck(e==COLI_XDNA_EXEC_OK, msg);
+ float *REFS = (float*)malloc((size_t)S*TN*sizeof(float));
+ oracle(REFS, x, coli_xdna_prepared_image(slot), S, TK, TN);
+ int exact = 1;
+ for(size_t j=0;j<(size_t)S*TN;j++) if(y[j]!=REFS[j]){ exact=0; break; }
+ snprintf(msg,sizeof msg,"switch step %zu (S=%d, M%u) -> exact after transition", i, S, bk);
+ ck(exact, msg);
+ free(REFS);
+ snprintf(msg,sizeof msg,"switch step %zu -> still exactly one prepared object", i);
+ ck(coli_xdna_prepared_live_objects()==1, msg);
+ free(x); free(y);
+ }
+ ck(coli_xdna_test_bucket_switches((unsigned)TM,(unsigned)TM2)==2,
+ "small->large transitions counted (2: 64->65 and 64->130)");
+ ck(coli_xdna_test_bucket_switches((unsigned)TM2,(unsigned)TM)==2,
+ "large->small transitions counted (2: 65->64 and 256->64)");
+ ck(coli_xdna_test_bucket_artifact_opens((unsigned)TM2)==2,
+ "130 and 256 share the large bucket -- no reopen between them");
+ ck(coli_xdna_test_bucket_artifact_opens((unsigned)TM)>=1, "small bucket opened");
+ ck(coli_xdna_test_bucket_artifact_opens((unsigned)TM2)>=1, "large bucket opened");
+ ck(coli_xdna_test_stale_wrapper_rejects()==0, "no stale wrapper reuse across buckets");
+ ck(coli_xdna_prepared_live_objects()==1, "bucket switching created no second prepared image");
+ coli_xdna_prepared_release(&slot);
+ ck(coli_xdna_prepared_live_objects()==0, "prepared image released");
+ }
+
coli_xdna_test_set_helper_path(NULL);
coli_xdna_shutdown();
coli_xdna_test_set_registry(NULL,0);
diff --git a/c/tests/test_xdna_failure.c b/c/tests/test_xdna_failure.c
index 2f0f5300..61e43ea4 100644
--- a/c/tests/test_xdna_failure.c
+++ b/c/tests/test_xdna_failure.c
@@ -38,6 +38,7 @@ static void ck(int cond, const char *what){
#define TK 256
#define TN 128
#define TM 64
+#define TM2 256 /* the large artifact bucket (M1) */
/* Failure stages the synthetic helper can be told to produce. Mirrors the enum
* in tests/xdna_fake_helper.c. */
@@ -46,7 +47,7 @@ enum { F_NONE = 0, F_DEVICE, F_ARTIFACT, F_WRAP, F_EXECUTE, F_COMPLETION };
static char g_root[1024], g_helper[1024], g_helper_abi1[1024], g_helper_partial[1024];
static char g_xclbin[2048], g_insts[2048];
static char g_xhex[65], g_ihex[65];
-static ColiXdnaArtifact g_test_rows[1];
+static ColiXdnaArtifact g_test_rows[2];
static HMODULE g_fake;
static void (*p_set_fail)(int);
static void (*p_reset)(void);
@@ -87,7 +88,13 @@ static void build_registry(void){
g_test_rows[0].correctness_qualified = 1;
g_test_rows[0].userptr_qualified = 1;
g_test_rows[0].structural_qualified = 1;
- coli_xdna_test_set_registry(g_test_rows, 1);
+
+ /* M1: the large bucket. Identical in every field but artifact_m, so any
+ * behavioural difference is attributable to the bucket alone. */
+ g_test_rows[1] = g_test_rows[0];
+ g_test_rows[1].artifact_m = TM2;
+
+ coli_xdna_test_set_registry(g_test_rows, 2);
}
static void qt_make(QT *t, int I, int O, int gs, unsigned seed){
@@ -132,6 +139,11 @@ static void arm(int stage){ if(p_set_fail) p_set_fail(stage); }
static void use_helper(const char *path){
coli_xdna_execution_shutdown();
coli_xdna_test_set_force_execution(0);
+ /* Per-bucket counters are cumulative by design; each scenario measures one
+ * operation, so they are zeroed at the same point every other per-run
+ * counter is. Without this, "this scenario dispatched zero times" silently
+ * becomes "some earlier scenario dispatched". */
+ coli_xdna_test_reset_bucket_counters();
coli_xdna_test_set_helper_path(path);
g_fake = NULL; p_set_fail = NULL; p_reset = NULL;
if(coli_xdna_binding() == COLI_XDNA_AVAILABLE){
@@ -224,7 +236,8 @@ int main(int argc, char **argv){
{ "sh_down orientation", COLI_XDNA_FAMILY_MOE_SHARED_GATE_UP, 4,TN,TK,64,0,TM, COLI_XDNA_HARD_SHAPE_UNSUPPORTED },
{ "gs != 64", COLI_XDNA_FAMILY_MOE_SHARED_GATE_UP, 4,TK,TN,32,0,TM, COLI_XDNA_HARD_GROUP_SIZE_UNSUPPORTED },
{ "fmt != 4", COLI_XDNA_FAMILY_MOE_SHARED_GATE_UP, 2,TK,TN,64,0,TM, COLI_XDNA_HARD_FORMAT_UNSUPPORTED },
- { "M above range", COLI_XDNA_FAMILY_MOE_SHARED_GATE_UP, 4,TK,TN,64,0,TM+1, COLI_XDNA_HARD_M_OUT_OF_RANGE },
+ { "M above EVERY bucket", COLI_XDNA_FAMILY_MOE_SHARED_GATE_UP, 4,TK,TN,64,0,TM2+1, COLI_XDNA_HARD_M_OUT_OF_RANGE },
+ { "M=0, below the range", COLI_XDNA_FAMILY_MOE_SHARED_GATE_UP, 4,TK,TN,64,0,0, COLI_XDNA_HARD_M_OUT_OF_RANGE },
{ "K/N mismatch", COLI_XDNA_FAMILY_MOE_SHARED_GATE_UP, 4,TK+8,TN,64,0,TM, COLI_XDNA_HARD_SHAPE_UNSUPPORTED },
/* K1 planar layout: the converter was never qualified against it */
{ "planar fmt4 layout", COLI_XDNA_FAMILY_MOE_SHARED_GATE_UP, 4,TK,TN,64,1,TM, COLI_XDNA_HARD_LAYOUT_UNSUPPORTED }
@@ -361,6 +374,69 @@ int main(int argc, char **argv){
}
/* ================================================================== */
+ /* M1 -- the SAME failure owners, reached through the LARGE bucket.
+ *
+ * The point is not to re-prove the failure taxonomy, which is generic in
+ * artifact_m. It is to prove that an operation which selects M256 arrives at
+ * those same owners, fails closed the same way, and leaves the caller's
+ * buffer and the authoritative fmt4 exactly as untouched. S=65 is used
+ * because it is the first logical M that selects the large bucket. */
+ printf("\nM1 runtime failure stages on the LARGE bucket (S=65 -> M256)\n");
+ {
+ const int S2 = 65;
+ struct { int stage; const char *what; ColiXdnaExec want; ColiXdnaHard hard; } st2[] = {
+ { F_DEVICE, "device init", COLI_XDNA_EXEC_DEVICE_INIT_FAILED, COLI_XDNA_HARD_DEVICE_UNAVAILABLE },
+ { F_ARTIFACT, "artifact runtime open", COLI_XDNA_EXEC_ARTIFACT_OPEN_FAILED, COLI_XDNA_HARD_ARTIFACT_RUNTIME_UNAVAILABLE },
+ { F_WRAP, "userptr wrap", COLI_XDNA_EXEC_WEIGHT_WRAP_FAILED, COLI_XDNA_HARD_WEIGHT_WRAP_UNAVAILABLE },
+ { F_EXECUTE, "dispatch", COLI_XDNA_EXEC_EXECUTE_FAILED, COLI_XDNA_HARD_ELIGIBLE },
+ { F_COMPLETION, "completion", COLI_XDNA_EXEC_COMPLETION_FAILED, COLI_XDNA_HARD_ELIGIBLE }
+ };
+ float *x2 = mk_x(S2, TK, 313u);
+ for(size_t i = 0; i < sizeof st2/sizeof st2[0]; i++){
+ use_helper(g_helper);
+ coli_xdna_test_set_artifact_root(g_root);
+ coli_xdna_test_set_force_execution(1);
+ arm(st2[i].stage);
+ QT T; qt_make(&T, TK, TN, 64, 4242u);
+ float *ref2 = (float*)malloc((size_t)S2*TN*4); matmul_qt(ref2, x2, &T, S2);
+ float *y2 = (float*)malloc((size_t)S2*TN*4); poison_buf(y2,(size_t)S2*TN);
+ int handled = coli_xdna_try_matmul(COLI_XDNA_FAMILY_MOE_SHARED_GATE_UP,&T.xdna,
+ 4,T.q4,T.s,TK,TN,64,0,y2,x2,S2);
+ char m[240];
+ snprintf(m,sizeof m,"M256 %s failure -> not handled", st2[i].what);
+ ck(handled==0,m);
+ snprintf(m,sizeof m,"M256 %s failure -> %s", st2[i].what,
+ coli_xdna_exec_text(coli_xdna_test_last_exec()));
+ ck(coli_xdna_test_last_exec()==st2[i].want, m);
+ snprintf(m,sizeof m,"M256 %s failure -> hard verdict %s", st2[i].what,
+ coli_xdna_hard_text(coli_xdna_test_last_hard()));
+ ck(coli_xdna_test_last_hard()==st2[i].hard, m);
+ snprintf(m,sizeof m,"M256 %s failure -> output not valid", st2[i].what);
+ ck(coli_xdna_test_output_valid()==0, m);
+ snprintf(m,sizeof m,"M256 %s failure -> caller output entirely untouched", st2[i].what);
+ ck(count_poison(y2,(size_t)S2*TN)==(size_t)S2*TN, m);
+ snprintf(m,sizeof m,"M256 %s failure -> zero large-bucket dispatches", st2[i].what);
+ ck(coli_xdna_test_bucket_dispatches((unsigned)TM2)==0, m);
+ snprintf(m,sizeof m,"M256 %s failure -> zero SMALL-bucket dispatches (no downgrade)", st2[i].what);
+ ck(coli_xdna_test_bucket_dispatches((unsigned)TM)==0, m);
+ if(st2[i].stage != F_DEVICE && st2[i].stage != F_ARTIFACT){
+ snprintf(m,sizeof m,"M256 %s failure -> a correct prepared image stays VALID", st2[i].what);
+ ck(coli_xdna_prepared_state(T.xdna)==COLI_XDNA_PREP_VALID, m);
+ }
+ snprintf(m,sizeof m,"M256 %s failure -> authoritative fmt4 unchanged", st2[i].what);
+ ck(fnv(T.q4,q4b)==fnv(W.q4,q4b) && fnv(T.s,scb)==fnv(W.s,scb), m);
+ matmul_qt(y2, x2, &T, S2);
+ snprintf(m,sizeof m,"M256 %s failure -> residual poison 0 after current path", st2[i].what);
+ ck(count_poison(y2,(size_t)S2*TN)==0, m);
+ snprintf(m,sizeof m,"M256 %s failure -> current path result exact", st2[i].what);
+ ck(memcmp(y2,ref2,(size_t)S2*TN*4)==0, m);
+ free(ref2); free(y2);
+ arm(F_NONE);
+ coli_xdna_prepared_release(&T.xdna); free(T.q4); free(T.s);
+ }
+ free(x2);
+ }
+
printf("\nruntime failure stages -- each classified, each falls back exactly\n");
{
struct { int stage; const char *what; ColiXdnaExec want; ColiXdnaHard hard; } st[] = {