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

Merge branch 'dev' into sse41-tier-upstream-next-v2 (+214 commits)

Jaapjan Tinbergen2c7979f5a621
diff --cc c/Makefile
index dd78531c,258212e2..0964584f
--- a/c/Makefile
+++ b/c/Makefile
@@@ -503,13 -508,18 +508,19 @@@ TEST_RULES := $(shell sed -n 's|^tests
TEST_EXCLUDE = test_uring test_deepseek_v4 test_v4_ownership test_v4_serve_framing \
test_segment_adapters_registration test_segment_adapters_real \
test_edge_adapters_registration test_edge_adapters_real \
- test_gsgemv_sse41 test_qgemv_sse41 test_olmoe_dot_i8_16_sse41 test_st_f16_bf16_simd_sse41 test_qwen38_tier_engine
- test_gsgemv_sse41 test_qgemv_sse41 test_olmoe_dot_i8_16_sse41 test_qwen38_tier_engine \
++ test_gsgemv_sse41 test_qgemv_sse41 test_olmoe_dot_i8_16_sse41 test_st_f16_bf16_simd_sse41 test_qwen38_tier_engine \
+ test_e8x4g64_loader \
+ test_i4_grouped_sse41 test_i4_grouped_sse41_o1 test_i4_grouped_sse41_no_contract
TEST_BINS = $(addprefix tests/,$(addsuffix $(EXE),$(filter-out $(TEST_EXCLUDE),$(TEST_RULES))))
ifneq (,$(LINUX))
TEST_BINS += tests/test_uring$(EXE)
+ TEST_BINS += tests/test_exact_dot$(EXE)
endif
ifneq (,$(X86_64))
- TEST_BINS += tests/test_gsgemv_sse41$(EXE) tests/test_qgemv_sse41$(EXE) tests/test_olmoe_dot_i8_16_sse41$(EXE) tests/test_st_f16_bf16_simd_sse41$(EXE)
+ TEST_BINS += tests/test_gsgemv_sse41$(EXE) tests/test_qgemv_sse41$(EXE) \
- tests/test_olmoe_dot_i8_16_sse41$(EXE) tests/test_i4_grouped_sse41$(EXE) \
++ tests/test_olmoe_dot_i8_16_sse41$(EXE) tests/test_st_f16_bf16_simd_sse41$(EXE) \
++ tests/test_i4_grouped_sse41$(EXE) \
+ tests/test_i4_grouped_sse41_o1$(EXE) tests/test_i4_grouped_sse41_no_contract$(EXE)
endif
ifeq ($(COLI_V4_SUPPORTED),1)
TEST_BINS += tests/test_v4_hybrid_policy$(EXE) tests/test_k3_fill_budget$(EXE) tests/test_v4_bank_pair$(EXE)
@@@ -1197,16 -1228,11 +1229,16 @@@ tests/test_qwen36_tok_truncated$(EXE):
$(CC) $(QWEN36_CFLAGS) $< $(QWEN36_TIER_SRC) $(CUDA_OBJ) -o $@ $(QWEN36_LDFLAGS)
# push_id / bpe_piece must refuse loudly, not crash, on a failed realloc during growth.
- tests/test_qwen36_encode_oom$(EXE): tests/test_qwen36_encode_oom.c qwen36.c expert_ffn.h qwen36_tier.h st.h json.h compat.h $(QWEN36_TIER_SRC) $(CUDA_OBJ)
+ tests/test_qwen36_encode_oom$(EXE): tests/test_qwen36_encode_oom.c gsgemv.h qgemv.h sse41_kernels.h qwen36.c 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)
+# slot_ensure_int8's wiring onto #1271's unpack_int4_to_int8: regression check
+# that its output matches the old separate scalar nibble-unpack loop it replaced.
+tests/test_qwen36_slot_int8$(EXE): tests/test_qwen36_slot_int8.c qwen36.c 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)
+
# the dense trunk's integer path: quantizer contract, int4 planar packing, dispatch.
- tests/test_qwen36_dense_idot$(EXE): tests/test_qwen36_dense_idot.c qwen36.c expert_ffn.h qwen36_tier.h st.h json.h compat.h $(QWEN36_TIER_SRC) $(CUDA_OBJ)
+ tests/test_qwen36_dense_idot$(EXE): tests/test_qwen36_dense_idot.c gsgemv.h qgemv.h sse41_kernels.h qwen36.c 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)
# #1653: added tokens are split out before the regex pre-tokenizer, HF-style.
diff --cc c/qwen36.c
index 86e20043,d4e49f5d..e7515014
--- a/c/qwen36.c
+++ b/c/qwen36.c
@@@ -660,33 -684,19 +684,38 @@@ typedef struct
/* Gated DeltaNet (linear_attention) dims, read from qwen36_meta.json. */
int dn_vheads, dn_kheads, dn_kdim, dn_vdim, dn_convk, dn_conv_dim;
int expert_gs; /* expert scale group size along input dim; 0 = per-row */
+ /* Mixed expert layout (convert_qwen36.py --down-bits): gate/up stay int4
+ * (ebits, expert_gs), down_proj is int8 with its own group size. One slab
+ * per expert, [gate int4 packed | up int4 packed | down int8], 2*inter*hidden
+ * bytes -- told apart from int4 (1.5x) and int8 (3x) by size, like today. */
+ int expert_down_bits, expert_down_gs;
} Cfg;
+/* ---- Dense int8: a dense matrix that is quantized to int8 during load
+ * (load_tq, below matmul_d) instead of loaded as f32 and quantized in a
+ * separate pass afterward -- so the f32 staging buffer for THIS matrix alone
+ * is what's briefly resident, not every dense matrix in the model at once.
+ * `w` is the f32 copy: kept (and `q`/`sc` left NULL) when COLI_DENSE_I8=0,
+ * the reference/parity path; freed once `q`/`sc` are populated otherwise
+ * (COLI_KEEP_F32=1 keeps it alongside them, for debugging). matmul_d
+ * dispatches on q!=NULL directly -- no pointer-keyed scan. */
+/* q/sc: int8 rows with one scale per row (the classic copy, what the VRAM
+ * tier uploads). q4/sg: the same matrix as int4 planar blocks of 64 with one
+ * scale per group (COLI_DENSE_BITS=4), the layout the K1b grouped kernel
+ * reads; ng = I/64 groups per row. */
+typedef struct { const float *w; int8_t *q; float *sc; int I, O; uint8_t *q4; float *sg; int ng; } QW;
+static void qw_free(QW *w) {
+ free((void*)w->w); free(w->q); free(w->sc); free(w->q4); free(w->sg);
+ w->w = NULL; w->q = NULL; w->sc = NULL; w->q4 = NULL; w->sg = NULL; w->ng = 0;
+}
+
/* ---------- per-layer dense weights ---------- */
typedef struct {
- float *in_ln, *post_ln, *q, *k, *v, *o, *qn, *kn, *gate, *gate_bias;
- float *sh_g, *sh_u, *sh_d, *sh_gate; /* shared expert (dense f32) + shared_expert_gate */
+ float *in_ln, *post_ln, *qn, *kn, *gate_bias;
+ QW q, k, v, o, gate;
+ QW sh_g, sh_u, sh_d; float *sh_gate; /* shared expert (dense, int8-during-load) + shared_expert_gate */
/* Gated DeltaNet (linear_attention) dense weights (f16->f32 via st_read_f32). */
- float *dn_qkv, *dn_z, *dn_b, *dn_a; /* in_proj_qkv/z/b/a */
+ QW dn_qkv, dn_z; float *dn_b, *dn_a; /* in_proj_qkv/z (int8-during-load), b/a (not dense-matmul'd) */
float *dn_conv; /* conv1d.weight [conv_dim, convk] (groups=conv_dim) */
float *dn_dtbias, *dn_alog; /* dt_bias[vh], A_log[vh] */
float *dn_norm; /* RMSNormGated weight [vdim] */
@@@ -1059,11 -1070,24 +1090,18 @@@ static void matmul_qe(float *y, const f
if (g_expert_gs) matmul_q_gs(y, x, q, scale, I, O, g_expert_gs);
else matmul_q(y, x, q, scale, I, O);
}
+ /* down_proj: in the mixed layout it carries its own scale layout (int8, per
+ * row or expert_down_gs), everywhere else it is matmul_qe. */
+ static void matmul_qd(float *y, const float *x, const int8_t *q, const float *scale, int I, int O) {
+ if (!g_expert_mixed) { matmul_qe(y, x, q, scale, I, O); return; }
+ if (g_expert_down_gs) matmul_q_gs(y, x, q, scale, I, O, g_expert_down_gs);
+ else matmul_q(y, x, q, scale, I, O);
+ }
/* ---- Dense int8: per-row quantized copies of the large f32 matrices.
- * matmul_d dispatches via pointer lookup to matmul_q; COLI_DENSE_I8=0 falls
- * back to f32 (reference path for parity tests). ~4x less memory traffic. */
-#define QDW_MAX 1024
-/* q/sc: int8 rows with one scale per row (the classic copy, what the VRAM
- * tier uploads). q4/sg: the same matrix as int4 planar blocks of 64 with one
- * scale per group (COLI_DENSE_BITS=4), the layout the K1b grouped kernel
- * reads; ng = I/64 groups per row. */
-static struct { const float *w; int8_t *q; float *sc; int I, O; uint8_t *q4; float *sg; int ng; } g_qdw[QDW_MAX];
-static int g_qdw_n = 0;
+ * matmul_d dispatches directly off QW.q (no pointer-keyed scan -- see QW,
+ * above Layer); COLI_DENSE_I8=0 falls back to f32 (QW.w, reference path for
+ * parity tests). ~4x less memory traffic. */
/* ---- Dense trunk, integer dot products ------------------------------------
*
* Every dense GEMV of a token (DeltaNet projections and out_proj, attention
@@@ -1263,16 -1234,19 +1252,19 @@@ static void matmul_d(float *y, const fl
free(xq); free(sx); free(xsg); /* out of memory: the f32 path below */
}
if (S > 1 && dense_batch_on())
- matmul_q_batch(y, x, g_qdw[i].q, g_qdw[i].sc, S, I, O);
+ matmul_q_batch(y, x, w->q, w->sc, S, I, O);
else
- for (int s = 0; s < S; s++) matmul_q(y+(int64_t)s*O, x+(int64_t)s*I, g_qdw[i].q, g_qdw[i].sc, I, O);
+ for (int s = 0; s < S; s++) matmul_q(y+(int64_t)s*O, x+(int64_t)s*I, w->q, w->sc, I, O);
return;
}
- matmul(y, x, W, S, I, O);
+ matmul(y, x, w->w, S, I, O);
}
/* A dense matrix the tier placed in VRAM (handle+1 kept in the Layer, 0 = CPU):
- * one GEMV from the device, or 0 and the caller runs matmul_d as before. The
+ * a device matmul, or 0 and the caller runs matmul_d as before. The
* tier turns a failing handle off itself, so the fallback is permanent. */
+ static inline int qtd_batch(int hp1, float *y, const float *x, int S, int I, int O){
+ return hp1 > 0 && qt_dense_matmul_batch(hp1 - 1, y, x, S, I, O);
+ }
static inline int qtd(int hp1, float *y, const float *x, int I, int O){
return hp1 > 0 && qt_dense_matmul(hp1 - 1, y, x, I, O);
}
@@@ -2083,11 -2045,11 +2095,11 @@@ static void attention(Model *m, Layer *
float *q = falloc((int64_t)S*q_out);
float *k = falloc((int64_t)S*kv_out);
float *vv= falloc((int64_t)S*kv_out);
- /* Decode (S == 1): the projections the tier placed answer from VRAM,
- * one GEMV each; a prompt batch keeps the batched CPU matmul. */
- if (!(S == 1 && qtd(l->qth_q, q, x, D, q_out))) matmul_d(q, x, &l->q, S, D, q_out);
- if (!(S == 1 && qtd(l->qth_k, k, x, D, kv_out))) matmul_d(k, x, &l->k, S, D, kv_out);
- if (!(S == 1 && qtd(l->qth_v, vv, x, D, kv_out))) matmul_d(vv, x, &l->v, S, D, kv_out);
+ /* The projections the tier placed answer from VRAM for the whole batch,
+ * with one backend call per matrix; unavailable handles use CPU matmul. */
- if (!qtd_batch(l->qth_q, q, x, S, D, q_out)) matmul_d(q, x, l->q, S, D, q_out);
- if (!qtd_batch(l->qth_k, k, x, S, D, kv_out)) matmul_d(k, x, l->k, S, D, kv_out);
- if (!qtd_batch(l->qth_v, vv, x, S, D, kv_out)) matmul_d(vv, x, l->v, S, D, kv_out);
++ if (!qtd_batch(l->qth_q, q, x, S, D, q_out)) matmul_d(q, x, &l->q, S, D, q_out);
++ if (!qtd_batch(l->qth_k, k, x, S, D, kv_out)) matmul_d(k, x, &l->k, S, D, kv_out);
++ if (!qtd_batch(l->qth_v, vv, x, S, D, kv_out)) matmul_d(vv, x, &l->v, S, D, kv_out);
/* split q into query (first hd) and gate (next gate_dim), both per head */
float *query = falloc((int64_t)S*H*hd);
float *gate = falloc((int64_t)S*H*gate_dim);
@@@ -2149,7 -2111,7 +2161,7 @@@
float g = gate_dim ? gate[o] : 0.f;
ag[o] = ctx[o] * (1.f / (1.f + expf(-g)));
}
- if (!(S == 1 && qtd(l->qth_o, out, ag, H*hd, D))) matmul_d(out, ag, &l->o, S, H*hd, D);
- if (!qtd_batch(l->qth_o, out, ag, S, H*hd, D)) matmul_d(out, ag, l->o, S, H*hd, D);
++ if (!qtd_batch(l->qth_o, out, ag, S, H*hd, D)) matmul_d(out, ag, &l->o, S, H*hd, D);
free(q); free(k); free(vv); free(query); free(gate); free(ctx); free(ag);
}
@@@ -2557,11 -2530,15 +2580,15 @@@ static void deltanet(Model *m, Layer *l
const float *xs = x + (int64_t)s * H;
extern double g_dn_sub[4];
double _d0 = tm_now();
- /* projections (single-token matmuls). One fused GEMV when this layer's
- * dnproj is placed on a GPU, the two CPU matmuls otherwise. */
- if (!qt_dnproj_matmul(layer, qkvz, xs, H, conv_dim + value_dim)) {
+ if (s % B == 0) {
+ int rows = S - s < B ? S - s : B;
+ gpu_block = qt_dnproj_matmul_batch(layer, qkvz, xs, rows, H, proj_dim);
+ }
+ float *qkv = qkvz + (int64_t)(s % B) * proj_dim;
+ float *z = qkv + conv_dim;
+ if (!gpu_block) {
- matmul_d(qkv, xs, l->dn_qkv, 1, H, conv_dim);
- matmul_d(z, xs, l->dn_z, 1, H, value_dim);
+ matmul_d(qkv, xs, &l->dn_qkv, 1, H, conv_dim);
+ matmul_d(z, xs, &l->dn_z, 1, H, value_dim);
}
matmul(b, xs, l->dn_b, 1, H, vh);
matmul(a, xs, l->dn_a, 1, H, vh);
diff --cc c/tests/test_qwen36_dnproj_batch.c
index 00000000,95b976db..60ed8f4d
mode 000000,100644..100644
--- a/c/tests/test_qwen36_dnproj_batch.c
+++ b/c/tests/test_qwen36_dnproj_batch.c
@@@ -1,0 -1,134 +1,140 @@@
+ /* Run DeltaNet itself: batching must preserve outputs, conv history and state. */
+ #define main qwen_main_unused
+ #include "../qwen36.c"
+ #undef main
+
+ /* Define COLI_DNPROJ_REAL_CUDA and link --wrap=coli_cuda_matmul to run
+ * the same state-parity test against the real CUDA backend on Linux. */
+ #ifdef COLI_DNPROJ_REAL_CUDA
+ #include "../backend_cuda.h"
+ int __real_coli_cuda_matmul(ColiCudaTensor **,float *,const float *,const void *,
+ const float *,int,int,int,int,int,int);
+ #define dn_fake_matmul __real_coli_cuda_matmul
+ #define DN_MATMUL __wrap_coli_cuda_matmul
+ #else
+ /* Wrap the shared fake so this test can inject a failure at a block boundary. */
+ #define coli_cuda_matmul dn_fake_matmul
+ #include "qwen36_fake_cuda.h"
+ #undef coli_cuda_matmul
+ #define DN_MATMUL coli_cuda_matmul
+ #endif
+ static int calls, fail_call, max_rows, last_rows;
+ int DN_MATMUL(ColiCudaTensor **t,float *y,const float *x,const void *w,
+ const float *sc,int fmt,int S,int I,int O,int dev,int gs) {
+ calls++; if (S > max_rows) max_rows = S; last_rows = S;
+ if (calls == fail_call) { for(int i=0;i<S*O;i++) y[i] = NAN; return 0; }
+ return dn_fake_matmul(t,y,x,w,sc,fmt,S,I,O,dev,gs);
+ }
+ #include "../qwen36_tier.c"
+
+ enum { H=8, KH=1, VH=2, KD=2, VD=3, C=10, Z=6, O=C+Z, CK=3 };
+ static int failed;
+ #define CHECK(c) do { if(!(c)){fprintf(stderr,"FAIL line %d: %s\n",__LINE__,#c);failed++;} } while(0)
+ static float *weights(int n) {
+ float *p = calloc(n,sizeof(float));
+ for(int i=0;i<n;i++) p[i] = (i%11 - 5)*0.03125f;
+ return p;
+ }
+ static void state(Model *m) {
+ m->DN_rec = calloc(1,sizeof(float *)); m->DN_conv = calloc(1,sizeof(float *));
+ m->DN_rec[0] = calloc(VH*KD*VD,sizeof(float)); m->DN_conv[0] = calloc(C*(CK-1),sizeof(float));
+ }
+ static void clear_state(Model *m) {
+ memset(m->DN_rec[0],0,VH*KD*VD*sizeof(float));
+ memset(m->DN_conv[0],0,C*(CK-1)*sizeof(float));
+ }
+ static void agree(const float *a,const float *b,int n) {
+ for(int i=0;i<n;i++) CHECK(isfinite(a[i]) && isfinite(b[i]) && fabsf(a[i]-b[i]) < 1e-5f);
+ }
+ int main(void) {
+ #ifdef _OPENMP
+ omp_set_num_threads(1);
+ #endif
+ setenv("COLI_DENSE_IDOT","0",1);
+ Model m={0}; Layer l={0};
+ m.c.hidden=H; m.c.dn_kheads=KH; m.c.dn_vheads=VH;
+ m.c.dn_kdim=KD; m.c.dn_vdim=VD; m.c.dn_convk=CK; m.c.dn_conv_dim=C; m.c.eps=1e-6f;
- l.dn_qkv=weights(C*H); l.dn_z=weights(Z*H); l.dn_out=weights(H*Z);
++ /* dn_qkv/dn_z: quantized straight into the QW the Layer holds, the same
++ * shape load_tq uses. dn_out stays unquantized (.w only, .q == NULL) --
++ * the pre-QW test never registered it either, so it always took the f32
++ * matmul_d fallback; both deltanet() calls below share this same l.dn_out,
++ * so which path it takes doesn't affect the cpu/gpu comparison. */
++ { float *w = weights(C*H); qw_quantize(w, H, C, NULL, &l.dn_qkv); free(w); }
++ { float *w = weights(Z*H); qw_quantize(w, H, Z, NULL, &l.dn_z); free(w); }
++ l.dn_out.w=weights(H*Z);
+ l.dn_b=weights(VH*H); l.dn_a=weights(VH*H); l.dn_conv=weights(C*CK);
+ l.dn_alog=weights(VH); l.dn_dtbias=weights(VH); l.dn_norm=weights(VD);
- qdw_register(l.dn_qkv,H,C); qdw_register(l.dn_z,H,Z);
+ int8_t q[O*H]; float sc[O];
- memcpy(q,g_qdw[0].q,C*H); memcpy(q+C*H,g_qdw[1].q,Z*H);
- memcpy(sc,g_qdw[0].sc,C*sizeof(float)); memcpy(sc+C,g_qdw[1].sc,Z*sizeof(float));
++ memcpy(q,l.dn_qkv.q,C*H); memcpy(q+C*H,l.dn_z.q,Z*H);
++ memcpy(sc,l.dn_qkv.sc,C*sizeof(float)); memcpy(sc+C,l.dn_z.sc,Z*sizeof(float));
+ state(&m);
+ enum { S=259 };
+ float *x=weights(S*H), cpu[S*H], gpu[S*H], rec[VH*KD*VD], conv[C*(CK-1)];
+ deltanet(&m,&l,0,x,S,0,cpu);
+ memcpy(rec,m.DN_rec[0],sizeof(rec)); memcpy(conv,m.DN_conv[0],sizeof(conv));
+ #ifdef COLI_DNPROJ_REAL_CUDA
+ int device=0;
+ if(!coli_cuda_init(&device,1)) return 1;
+ #else
+ fake_dense_compute=1;
+ #endif
+ for(int failure=0;failure<=2;failure++) {
+ clear_state(&m); calls=0; max_rows=0; fail_call=failure;
+ CHECK(qt_dnproj_init(0,q,sc,H,O,0));
+ deltanet(&m,&l,0,x,S,0,gpu);
+ CHECK(calls == (failure==1 ? 1 : 2));
+ CHECK(max_rows==256);
+ if(failure!=1) CHECK(last_rows==3);
+ agree(cpu,gpu,S*H); agree(rec,m.DN_rec[0],VH*KD*VD); agree(conv,m.DN_conv[0],C*(CK-1));
+ CHECK(qt_dnproj_ready(0) == !failure);
+ coli_cuda_tensor_free(G_dnp[0].t); memset(&G_dnp[0],0,sizeof(G_dnp[0]));
+ qt_shutdown();
+ }
+ CHECK(dnproj_batch_rows(10000,65536,65536)==64);
+ CHECK(dnproj_batch_rows(1,H,O)==1);
+ /* Decode still uses a single projection and the same state transition. */
+ clear_state(&m); deltanet(&m,&l,0,x,1,0,cpu);
+ memcpy(rec,m.DN_rec[0],sizeof(rec)); memcpy(conv,m.DN_conv[0],sizeof(conv));
+ clear_state(&m); calls=0; fail_call=0;
+ CHECK(qt_dnproj_init(0,q,sc,H,O,0));
+ deltanet(&m,&l,0,x,1,0,gpu);
+ CHECK(calls==1 && last_rows==1);
+ agree(cpu,gpu,H); agree(rec,m.DN_rec[0],VH*KD*VD); agree(conv,m.DN_conv[0],C*(CK-1));
+ coli_cuda_tensor_free(G_dnp[0].t); memset(&G_dnp[0],0,sizeof(G_dnp[0]));
+ qt_shutdown();
+ /* Prefix extension must preserve state across separate calls, including a
+ * split immediately before, on, and after the 256-row block boundary. */
+ const int splits[]={1,255,256,257};
+ for(int split_index=0;split_index<4;split_index++) for(int failure=0;failure<2;failure++){
+ int split=splits[split_index];
+ float cpu_next[H], gpu_next[H];
+ clear_state(&m);
+ deltanet(&m,&l,0,x,S,0,cpu);
+ deltanet(&m,&l,0,x,1,S,cpu_next);
+ memcpy(rec,m.DN_rec[0],sizeof(rec)); memcpy(conv,m.DN_conv[0],sizeof(conv));
+ clear_state(&m);calls=0;
+ fail_call=failure ? (split+255)/256+1 : 0;
+ CHECK(qt_dnproj_init(0,q,sc,H,O,0));
+ deltanet(&m,&l,0,x,split,0,gpu);
+ deltanet(&m,&l,0,x+(size_t)split*H,S-split,split,gpu+(size_t)split*H);
+ CHECK(calls==(split+255)/256+(failure?1:(S-split+255)/256));
+ int before_decode=calls;
+ deltanet(&m,&l,0,x,1,S,gpu_next);
+ CHECK(calls==before_decode+!failure);
+ if(!failure) CHECK(last_rows==1);
+ CHECK(qt_dnproj_ready(0)==!failure);
+ agree(cpu,gpu,S*H); agree(cpu_next,gpu_next,H);
+ agree(rec,m.DN_rec[0],VH*KD*VD); agree(conv,m.DN_conv[0],C*(CK-1));
+ coli_cuda_tensor_free(G_dnp[0].t); memset(&G_dnp[0],0,sizeof(G_dnp[0]));
+ qt_shutdown();
+ }
+ #ifdef COLI_DNPROJ_REAL_CUDA
+ coli_cuda_shutdown();
+ #endif
+ free(x); free(m.DN_rec[0]); free(m.DN_conv[0]); free(m.DN_rec); free(m.DN_conv);
- free(l.dn_qkv); free(l.dn_z); free(l.dn_out); free(l.dn_b); free(l.dn_a); free(l.dn_conv);
++ qw_free(&l.dn_qkv); qw_free(&l.dn_z); free((void*)l.dn_out.w);
++ free(l.dn_b); free(l.dn_a); free(l.dn_conv);
+ free(l.dn_alog); free(l.dn_dtbias); free(l.dn_norm);
- for(int i=0;i<g_qdw_n;i++){free(g_qdw[i].q);free(g_qdw[i].sc);} g_qdw_n=0;
+ printf("DeltaNet batch: %s\n",failed?"FAIL":"outputs and recurrent state match; 259 calls -> 2");
+ return failed!=0;
+ }