interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / evidence / gdn-decode-20261018 / change.diff

1433 lines · 88514 bytes Raw Blame History
1diff --git a/Makefile b/Makefile
2index 3551fea3..94ed0d8e 100644
3--- a/Makefile
4+++ b/Makefile
5@@ -36,6 +36,8 @@ SRCS += src/omega_blackwell_codegen_attn.c src/omega_gpu_decode_attn_api.c
6 SRCS += src/omega_gpu_hostmath.c
7 # OWN-30B cut 8 item 2 (2026-10-13): one-launch MoE router (omega_gpu_router_api.h), new files only
8 SRCS += src/omega_blackwell_codegen_router.c src/omega_gpu_router_api.c
9+# Qwen3.8-27B Gated DeltaNet decode step (omega#542, 2026-10-18): one launch per layer (omega_gpu_gdn_api.h), new files only
10+SRCS += src/omega_blackwell_codegen_gdn.c src/omega_gpu_gdn_api.c
11 # OWN-30B W-L2 (2026-10-13): decode launch collapse, new files only: command list (N launches, one submit) + glue kernels
12 SRCS += src/omega_gpu_cmdlist.c src/omega_blackwell_codegen_dec.c
13 SRCS += src/omega_gpu_mem_cached.c
14@@ -2498,6 +2500,16 @@ test-gpu-router: $(GPU_ROUTER_TEST)
15 ./$(GPU_ROUTER_TEST) --host-only
16 ./$(GPU_ROUTER_TEST) --sim
17
18+# Qwen3.8-27B Gated DeltaNet decode step (omega#542, 2026-10-18): one launch per layer (omega_gpu_gdn_api.h).
19+# test-gpu-gdn is host-only plus the warp simulator over the whole grid; the chip run is ./build/gpu_gdn_test under the GB10 lock.
20+.PHONY: test-gpu-gdn
21+GPU_GDN_TEST = $(OUT_DIR)/gpu_gdn_test
22+$(GPU_GDN_TEST): tests/gpu_gdn_test.c tests/bw_warp_sim_k.h src/omega_gpu_gdn_api.h src/omega_blackwell_codegen_gdn.h $(OUT_DIR)/libomega_gpu.a
23+ $(CC) $(CFLAGS) -ffp-contract=off -Itests -o $@ tests/gpu_gdn_test.c $(OUT_DIR)/libomega_gpu.a -lpthread -lm
24+test-gpu-gdn: $(GPU_GDN_TEST)
25+ ./$(GPU_GDN_TEST) --host-only
26+ ./$(GPU_GDN_TEST) --sim
27+
28 # OWN-30B (2026-10-13): bf16 streaming GEMV, the lm_head (omega_gpu_bf16_gemv_api.h). test-gpu-bf16-gemv is host-only
29 # (emulation, budgets) plus the warp simulator (bit-identical to the host f32 emulation, mutants 1-3 caught); the chip
30 # gate is tools/run_gpu_bf16_gemv_chip.sh under the GB10 lock.
31diff --git a/src/omega_blackwell_codegen.c b/src/omega_blackwell_codegen.c
32index b09fa1e5..fdf1d4be 100644
33--- a/src/omega_blackwell_codegen.c
34+++ b/src/omega_blackwell_codegen.c
35@@ -633,6 +633,14 @@ static int encode_single_insn(const BlackwellIRInsn *insn, const OmegaRegAlloc *
36 w[3] = insn->control ? insn->control : 0x000e2400;
37 break;
38
39+ case BW_IR_MUFU_LG2:
40+ /* MUFU.LG2 Rd, Ra (nvdisasm 13.0 -b SM121 decodes sub-op 0x0c00 as MUFU.LG2) */
41+ w[0] = 0x7308U | ((uint32_t)(dst & 0xff) << 16);
42+ w[1] = (uint32_t)(src1 & 0xff);
43+ w[2] = 0x00000c00;
44+ w[3] = insn->control ? insn->control : 0x000e2400;
45+ break;
46+
47 case BW_IR_MUFU_RSQ:
48 /* MUFU.RSQ Rd, Ra */
49 w[0] = 0x7308U | ((uint32_t)(dst & 0xff) << 16);
50diff --git a/src/omega_blackwell_codegen.h b/src/omega_blackwell_codegen.h
51index cc36e237..c59d1993 100644
52--- a/src/omega_blackwell_codegen.h
53+++ b/src/omega_blackwell_codegen.h
54@@ -100,7 +100,10 @@ typedef enum {
55 BW_IR_LDG_E_128, /* LDG.E.128 Rd..Rd+3, desc[URd][Ra.64+imm] */
56 /* Batched attention loads (2026-10-12). Words matched against nvcc 13.0.88 -arch=sm_121
57 * (omega_blackwell_verify_codegen_fixtures_fragmm). imm = unsigned byte offset < 2^23; dst an aligned quad. */
58- BW_IR_LDS128 /* LDS.128 Rd..Rd+3, [Ra+imm] */
59+ BW_IR_LDS128, /* LDS.128 Rd..Rd+3, [Ra+imm] */
60+ /* Gated DeltaNet softplus (2026-10-18, omega#542): sub-op 0x0c00 checked with nvdisasm 13.0 -b SM121, which decodes
61+ * "MUFU.LG2 R2, R4" (EX2 0x0800, RCP 0x1000, RSQ 0x1400 decode the same way). log2(x), approximate. */
62+ BW_IR_MUFU_LG2 /* MUFU.LG2 Rd, Ra */
63 } BlackwellIROpcode;
64
65 /* Barrier (reconvergence) registers: the sm_121 BSSY/BSYNC words carry a 4-bit barrier id
66diff --git a/src/omega_blackwell_codegen_gdn.c b/src/omega_blackwell_codegen_gdn.c
67new file mode 100644
68index 00000000..235bace7
69--- /dev/null
70+++ b/src/omega_blackwell_codegen_gdn.c
71@@ -0,0 +1,356 @@
72+/*
73+ * omega_blackwell_codegen_gdn.c: the one-launch Gated DeltaNet decode step (see the header). Emitter rules follow
74+ * omega_blackwell_codegen_router.c (control words, read barrier on every load) and the held-scoreboard prefetch of
75+ * omega_bw_em.h (the state loads are in flight through the conv, norm and scalar phases).
76+ */
77+#include "omega_blackwell_codegen_gdn.h"
78+#include <string.h>
79+
80+#define CW(stall, wbar, rbar, wait) \
81+ (((uint32_t)(stall) << 9) | ((uint32_t)(wbar) << 14) | ((uint32_t)(rbar) << 17) | ((uint32_t)(wait) << 20))
82+#define CW_YIELD 0x2000u
83+#define SB_NONE 7u
84+#define SB_GPR 0u
85+#define SB_RD 1u
86+#define SB_UGPR 2u
87+#define SB_STATE 3u /* held: the 16 state quads */
88+#define SB_GATE 4u /* held: z and the norm weight */
89+#define NOREG (-1)
90+
91+enum { K_FIXED, K_PRED, K_VAR, K_VAR_MORE, K_UVAR, K_STORE, K_SYNC, K_EXIT, K_BRA, K_BSSY, K_BSYNC };
92+
93+typedef struct {
94+ BlackwellIRProgram *p;
95+ uint32_t pending, held;
96+ int err;
97+} Em;
98+
99+static void emit(Em *e, BlackwellIRInsn in, int kind) {
100+ uint32_t wait = e->pending;
101+ switch (kind) {
102+ case K_FIXED: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break;
103+ case K_PRED: in.control = CW(14, SB_NONE, SB_NONE, wait); e->pending = 0; break;
104+ case K_VAR: in.control = CW(4, SB_GPR, SB_RD, wait) | CW_YIELD; e->pending = (1u << SB_GPR) | (1u << SB_RD); break;
105+ case K_VAR_MORE: in.control = CW(4, SB_GPR, SB_RD, (e->pending & (1u << SB_RD))) | CW_YIELD; e->pending |= (1u << SB_GPR) | (1u << SB_RD); break;
106+ case K_UVAR: in.control = CW(4, SB_UGPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_UGPR; break;
107+ case K_STORE: in.control = CW(4, SB_NONE, SB_RD, wait) | CW_YIELD; e->pending = 1u << SB_RD; break;
108+ case K_SYNC: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break;
109+ case K_EXIT: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break;
110+ case K_BRA: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break;
111+ case K_BSSY: in.control = CW(1, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break;
112+ case K_BSYNC: in.control = CW(5, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break;
113+ default: e->err = -1; return;
114+ }
115+ if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1;
116+}
117+
118+static BlackwellIRInsn I0(BlackwellIROpcode op) {
119+ BlackwellIRInsn i; memset(&i, 0, sizeof i);
120+ i.op = op; i.dst_vreg = NOREG; i.src1_vreg = NOREG; i.src2_vreg = NOREG; i.src3_vreg = NOREG; i.ureg = NOREG;
121+ return i;
122+}
123+static int V(Em *e) { int v = omega_bw_ir_alloc_vreg(e->p); if (v < 0) e->err = -1; return v; }
124+static int V64(Em *e) { int v = omega_bw_ir_alloc_vreg64(e->p); if (v < 0) e->err = -1; return v; }
125+static int V128(Em *e) { int v = omega_bw_ir_alloc_vreg128(e->p); if (v < 0) e->err = -1; return v; }
126+static int UV64(Em *e) { int v = omega_bw_ir_alloc_uvreg64(e->p); if (v < 0) e->err = -1; return v; }
127+static int here(const Em *e) { return (int)e->p->count; }
128+
129+/* operand with subregister */
130+typedef struct { int v; unsigned s; } Op;
131+static Op R(int v) { Op o = { v, 0 }; return o; }
132+static Op S(int v, unsigned s) { Op o = { v, s }; return o; }
133+
134+static void ldcu64(Em *e, int ud, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDCU64); i.dst_vreg = ud; i.imm = off; i.is_uniform = true; emit(e, i, K_UVAR); }
135+static void ldc64(Em *e, int d, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC64); i.dst_vreg = d; i.imm = off; emit(e, i, K_VAR); }
136+static void ldc32(Em *e, int d, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC); i.dst_vreg = d; i.imm = off; emit(e, i, K_VAR); }
137+static void s2r(Em *e, int d, uint32_t sr) { BlackwellIRInsn i = I0(BW_IR_S2R); i.dst_vreg = d; i.imm = sr; emit(e, i, K_VAR); }
138+static void movi(Em *e, int d, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_MOV_IMM); i.dst_vreg = d; i.imm = imm; emit(e, i, K_FIXED); }
139+static void movrz(Em *e, Op d) { BlackwellIRInsn i = I0(BW_IR_MOV_RZ); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; emit(e, i, K_FIXED); }
140+/* d = a * imm + c */
141+static void imadi(Em *e, Op d, Op a, uint32_t imm, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = a.v; i.src1_subreg = (uint8_t)a.s; i.imm = imm; i.src3_vreg = c; emit(e, i, K_FIXED); }
142+/* d64 = a * imm + c64 */
143+static void imadw(Em *e, int d64, int a, uint32_t imm, int c64) { BlackwellIRInsn i = I0(BW_IR_IMAD_WIDE); i.dst_vreg = d64; i.src1_vreg = a; i.imm = imm; i.src3_vreg = c64; emit(e, i, K_FIXED); }
144+static void shr(Em *e, int d, int a, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_SHF_R); i.dst_vreg = d; i.src1_vreg = a; i.imm = imm; emit(e, i, K_FIXED); }
145+static void andi(Em *e, Op d, Op a, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_LOP3_AND); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = a.v; i.src1_subreg = (uint8_t)a.s; i.imm = imm; emit(e, i, K_FIXED); }
146+static void i2fp(Em *e, int d, int a) { BlackwellIRInsn i = I0(BW_IR_I2FP); i.dst_vreg = d; i.src1_vreg = a; emit(e, i, K_FIXED); }
147+static void ldg_wide(Em *e, BlackwellIROpcode op, int d, int addr64, uint32_t imm, int udesc, int first) {
148+ BlackwellIRInsn i = I0(op); i.dst_vreg = d; i.src1_vreg = addr64; i.imm = imm; i.ureg = udesc; emit(e, i, first ? K_VAR : K_VAR_MORE);
149+}
150+static void ldg32(Em *e, int d, int addr64, int udesc, int first) { BlackwellIRInsn i = I0(BW_IR_LDG_E); i.dst_vreg = d; i.src1_vreg = addr64; i.ureg = udesc; emit(e, i, first ? K_VAR : K_VAR_MORE); }
151+/* A prefetch load: scoreboard `sb` is held, not waited for, until consume() */
152+static void ldg_pf(Em *e, int d, int addr64, uint32_t imm, int udesc, unsigned sb) {
153+ BlackwellIRInsn in = I0(BW_IR_LDG_E_128); in.dst_vreg = d; in.src1_vreg = addr64; in.imm = imm; in.ureg = udesc;
154+ in.control = CW(4, sb, SB_RD, (e->pending & (1u << SB_RD))) | CW_YIELD;
155+ e->pending |= 1u << SB_RD; e->held |= 1u << sb;
156+ if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1;
157+}
158+static void consume(Em *e, unsigned sb) { e->pending |= e->held & (1u << sb); e->held &= ~(1u << sb); }
159+/* STG.E [addr64 + imm], val (imm: signed 24-bit byte offset) */
160+static void stg(Em *e, int addr64, uint32_t imm, Op val, int udesc) { BlackwellIRInsn i = I0(BW_IR_STG_E); i.src1_vreg = addr64; i.src2_vreg = val.v; i.src2_subreg = (uint8_t)val.s; i.imm = imm; i.ureg = udesc; emit(e, i, K_STORE); }
161+static void lds128(Em *e, int d128, int addr, uint32_t imm, int first) { BlackwellIRInsn i = I0(BW_IR_LDS128); i.dst_vreg = d128; i.src1_vreg = addr; i.imm = imm; emit(e, i, first ? K_VAR : K_VAR_MORE); }
162+static void lds32(Em *e, Op d, int addr, int first) { BlackwellIRInsn i = I0(BW_IR_LDS32); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = addr; emit(e, i, first ? K_VAR : K_VAR_MORE); }
163+static void sts32(Em *e, int addr, Op val) { BlackwellIRInsn i = I0(BW_IR_STS32); i.src1_vreg = addr; i.src2_vreg = val.v; i.src2_subreg = (uint8_t)val.s; emit(e, i, K_STORE); }
164+static void bar_sync(Em *e) { emit(e, I0(BW_IR_BAR_SYNC), K_SYNC); }
165+static void fop(Em *e, BlackwellIROpcode op, Op d, Op a, Op b, Op c) {
166+ BlackwellIRInsn i = I0(op); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = a.v; i.src1_subreg = (uint8_t)a.s;
167+ i.src2_vreg = b.v; i.src2_subreg = (uint8_t)b.s; i.src3_vreg = c.v; i.src3_subreg = (uint8_t)c.s; emit(e, i, K_FIXED);
168+}
169+static void fadd(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FADD, d, a, b, R(NOREG)); }
170+static void fsub(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FSUB, d, a, b, R(NOREG)); }
171+static void fmul(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FMUL, d, a, b, R(NOREG)); }
172+static void ffma(Em *e, Op d, Op a, Op b, Op c) { fop(e, BW_IR_FFMA, d, a, b, c); }
173+static void fmaxx(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FMNMX_MAX, d, a, b, R(NOREG)); }
174+static void fminn(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FMNMX_MIN, d, a, b, R(NOREG)); }
175+static void mufu(Em *e, BlackwellIROpcode op, Op d, Op a) { BlackwellIRInsn i = I0(op); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = a.v; i.src1_subreg = (uint8_t)a.s; emit(e, i, K_VAR); }
176+static void shfl_down(Em *e, int d, int a, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_SHFL_DOWN); i.dst_vreg = d; i.src1_vreg = a; i.imm = off; emit(e, i, K_VAR); }
177+static void isetp_ge_u32(Em *e, int a, int b) { BlackwellIRInsn i = I0(BW_IR_ISETP_GE_U32); i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_PRED); }
178+static int bra_if_p0_fwd(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BRA); i.predicate_p0 = true; i.imm = 1; emit(e, i, K_BRA); return at; }
179+static void patch_bra(Em *e, int at, int target) { if (!e->err) e->p->insns[at].imm = (uint32_t)(target - at); }
180+static int bssy_fwd(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BSSY); i.bar_reg = 0; i.imm = 2; emit(e, i, K_BSSY); return at; }
181+static void bsync(Em *e) { BlackwellIRInsn i = I0(BW_IR_BSYNC); i.bar_reg = 0; emit(e, i, K_BSYNC); }
182+static void tail(Em *e) {
183+ emit(e, I0(BW_IR_EXIT), K_EXIT);
184+ BlackwellIRInsn self = I0(BW_IR_BRA);
185+ self.control = 0x000fc000u;
186+ if (omega_bw_ir_append(e->p, &self) < 0) e->err = -1;
187+}
188+
189+/* A region only some lanes of a warp execute: BSSY ... @P0 BRA join ... BSYNC. P0 true = skip. */
190+typedef struct { int bssy, bra; } Region;
191+static Region region_skip_if_p0(Em *e) { Region r; r.bssy = bssy_fwd(e); r.bra = bra_if_p0_fwd(e); return r; }
192+static void region_end(Em *e, Region r) { int at = here(e); bsync(e); patch_bra(e, r.bra, at); patch_bra(e, r.bssy, at + 1); }
193+/* warp sum: the total lands in lane 0 */
194+static void warp_sum(Em *e, int v, int t) { for (uint32_t o = 16; o >= 1; o /= 2) { shfl_down(e, t, v, o); fadd(e, R(v), R(v), R(t)); } }
195+/* lane 0 of every warp stores v to shared [addr] */
196+static void lane0_sts(Em *e, int lane, int one, int addr, Op v) {
197+ isetp_ge_u32(e, lane, one);
198+ Region r = region_skip_if_p0(e); sts32(e, addr, v); region_end(e, r);
199+}
200+/* x = x * sigmoid(x) = x * RCP(1 + EX2(-x * log2 e)); t scratch */
201+static void silu(Em *e, Op x, int t, int nlog2e, int fone) {
202+ fmul(e, R(t), x, R(nlog2e)); mufu(e, BW_IR_MUFU_EX2, R(t), R(t)); fadd(e, R(t), R(t), R(fone));
203+ mufu(e, BW_IR_MUFU_RCP, R(t), R(t)); fmul(e, x, x, R(t));
204+}
205+static uint32_t fbits(float f) { uint32_t u; memcpy(&u, &f, 4); return u; }
206+/* The 8-way cross-warp sum of acc[0..3] (columns 4L..4L+3 of each warp) through shared SM_P: pa = this thread's
207+ * partial slot, la = lane * 16. Every warp ends with the same totals, summed in warp order 0..7. */
208+static void cross_warp_sum(Em *e, int acc[4], int pa, int one, int la) {
209+ int pj = V(e), b0 = V128(e), b1 = V128(e);
210+ if (e->err) return;
211+ sts32(e, pa, R(acc[0]));
212+ for (unsigned j = 1; j < 4; j++) { imadi(e, R(pj), R(one), 4u * j, pa); sts32(e, pj, R(acc[j])); }
213+ bar_sync(e);
214+ for (unsigned w2 = 0; w2 < 8; w2 += 2) {
215+ lds128(e, b0, la, OMEGA_GDN_SM_P + w2 * OMEGA_GDN_HEAD_DIM * 4u, 1);
216+ lds128(e, b1, la, OMEGA_GDN_SM_P + (w2 + 1) * OMEGA_GDN_HEAD_DIM * 4u, 0);
217+ for (unsigned j = 0; j < 4; j++) {
218+ if (w2 == 0) fadd(e, R(acc[j]), S(b0, j), S(b1, j));
219+ else { fadd(e, R(acc[j]), R(acc[j]), S(b0, j)); fadd(e, R(acc[j]), R(acc[j]), S(b1, j)); }
220+ }
221+ }
222+}
223+
224+int omega_blackwell_codegen_gdn_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog) {
225+ if (!spec || !prog || spec->mutant < 0 || spec->mutant > 4) return -1;
226+ const int MUT = spec->mutant;
227+ const uint32_t C_LOG2E = fbits(1.44269504f), C_NLOG2E = fbits(-1.44269504f), C_LN2 = fbits(0.693147181f);
228+ const uint32_t C_ONE = fbits(1.0f), C_M1 = fbits(-1.0f), C_INV128 = fbits(1.0f / 128.0f);
229+ const float qs = 0.0883883476f; /* 1 / sqrt(128) */
230+ omega_bw_ir_init(prog);
231+ Em em; memset(&em, 0, sizeof em); em.p = prog; Em *e = &em;
232+
233+ /* ---- identities; the state address ---- */
234+ int udesc = UV64(e);
235+ int tid = V(e), cx = V(e), cy = V(e), h = V(e), warp = V(e), lane = V(e), one = V(e);
236+ int ps = V64(e), sa = V64(e), toff = V(e), sq[16];
237+ for (unsigned r = 0; r < 16; r++) sq[r] = V128(e);
238+ if (e->err) return -1;
239+ ldcu64(e, udesc, 0x358);
240+ ldc64(e, ps, OMEGA_GDN_ARG_STATE);
241+ s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y);
242+ movi(e, one, 1);
243+ imadi(e, R(h), R(cy), 3u, cx);
244+ shr(e, warp, tid, 5);
245+ andi(e, R(lane), R(tid), 31u);
246+ imadi(e, R(toff), R(h), OMEGA_GDN_STATE_HEAD_BYTES, NOREG);
247+ imadi(e, R(toff), R(warp), 16u * OMEGA_GDN_HEAD_DIM * 4u, toff);
248+ imadi(e, R(toff), R(lane), 16u, toff);
249+ imadw(e, sa, toff, 1u, ps);
250+
251+ /* ---- phase A: conv + SiLU of channel A (q or k) and channel V ---- */
252+ int half = V(e), c = V(e), gi = V(e), slv = V(e);
253+ int pqkv = V64(e), ppar = V64(e), ptail = V64(e);
254+ int axA = V64(e), awA = V64(e), atA = V64(e), axV = V64(e), awV = V64(e), atV = V64(e);
255+ int xA = V(e), xV = V(e), wA = V128(e), wV = V128(e), tA = V128(e), tV = V128(e);
256+ int insc = V(e), accA = V(e), accV = V(e), t1 = V(e), cnl2e = V(e), fone = V(e);
257+ if (e->err) return -1;
258+ ldc64(e, pqkv, OMEGA_GDN_ARG_QKV); ldc64(e, ppar, OMEGA_GDN_ARG_PAR); ldc64(e, ptail, OMEGA_GDN_ARG_TAIL);
259+ ldc32(e, insc, OMEGA_GDN_ARG_IN_SCALE);
260+ shr(e, half, tid, 7);
261+ andi(e, R(c), R(tid), 127u);
262+ imadi(e, R(gi), R(cy), OMEGA_GDN_HEAD_DIM, tid); /* q channel hk*128 + t; k channel 2048 + hk*128 + (t - 128) */
263+ imadi(e, R(gi), R(half), 2048u - 128u, gi);
264+ imadw(e, axA, gi, 4u, pqkv); imadw(e, awA, gi, 16u, ppar);
265+ imadi(e, R(gi), R(h), OMEGA_GDN_HEAD_DIM, c); /* v channel 4096 + h*128 + c */
266+ imadi(e, R(gi), R(one), 2u * OMEGA_GDN_KEY_HEADS * OMEGA_GDN_HEAD_DIM, gi);
267+ imadw(e, axV, gi, 4u, pqkv); imadw(e, awV, gi, 16u, ppar);
268+ imadw(e, atA, tid, 16u, ptail); imadw(e, atA, h, OMEGA_GDN_TAIL_HEAD_BYTES, atA);
269+ imadi(e, R(slv), R(one), 256u, c);
270+ imadw(e, atV, slv, 16u, ptail); imadw(e, atV, h, OMEGA_GDN_TAIL_HEAD_BYTES, atV);
271+ ldg32(e, xA, axA, udesc, 1);
272+ ldg_wide(e, BW_IR_LDG_E_128, wA, awA, 0, udesc, 0);
273+ ldg_wide(e, BW_IR_LDG_E_128, tA, atA, 0, udesc, 0);
274+ ldg32(e, xV, axV, udesc, 0);
275+ ldg_wide(e, BW_IR_LDG_E_128, wV, awV, 0, udesc, 0);
276+ ldg_wide(e, BW_IR_LDG_E_128, tV, atV, 0, udesc, 0);
277+ fmul(e, R(xA), R(xA), R(insc)); fmul(e, R(xV), R(xV), R(insc));
278+ fmul(e, R(accA), S(wA, 0), S(tA, 0)); ffma(e, R(accA), S(wA, 1), S(tA, 1), R(accA));
279+ ffma(e, R(accA), S(wA, 2), S(tA, 2), R(accA)); ffma(e, R(accA), S(wA, 3), R(xA), R(accA));
280+ fmul(e, R(accV), S(wV, 0), S(tV, 0)); ffma(e, R(accV), S(wV, 1), S(tV, 1), R(accV));
281+ ffma(e, R(accV), S(wV, 2), S(tV, 2), R(accV)); ffma(e, R(accV), S(wV, 3), R(xV), R(accV));
282+ movi(e, cnl2e, C_NLOG2E); movi(e, fone, C_ONE);
283+ silu(e, R(accA), t1, cnl2e, fone);
284+ silu(e, R(accV), t1, cnl2e, fone);
285+ /* the state prefetch (here, not earlier: the conv phase is the register peak) */
286+ for (unsigned r = 0; r < 16; r++) ldg_pf(e, sq[r], sa, r * OMEGA_GDN_HEAD_DIM * 4u, udesc, SB_STATE);
287+
288+ /* ---- phase B: l2 norms; q|k and v to shared; the tail shift ---- */
289+ int sqs = V(e), ts = V(e), ra = V(e);
290+ if (e->err) return -1;
291+ fmul(e, R(sqs), R(accA), R(accA));
292+ warp_sum(e, sqs, ts);
293+ imadi(e, R(ra), R(warp), 4u, NOREG); imadi(e, R(ra), R(one), OMEGA_GDN_SM_RED, ra);
294+ lane0_sts(e, lane, one, ra, R(sqs));
295+ imadi(e, R(ra), R(c), 4u, NOREG); imadi(e, R(ra), R(one), OMEGA_GDN_SM_V, ra);
296+ sts32(e, ra, R(accV));
297+ bar_sync(e);
298+ if (MUT != 4) {
299+ stg(e, atA, 0, S(tA, 1), udesc); stg(e, atA, 4, S(tA, 2), udesc); stg(e, atA, 8, R(xA), udesc);
300+ stg(e, atV, 0, S(tV, 1), udesc); stg(e, atV, 4, S(tV, 2), udesc); stg(e, atV, 8, R(xV), udesc);
301+ }
302+ {
303+ int rq = V128(e), el2 = V(e), hf = V(e), fq = V(e);
304+ if (e->err) return -1;
305+ ldc32(e, el2, OMEGA_GDN_ARG_EPS_L2);
306+ imadi(e, R(ra), R(half), 16u, NOREG);
307+ lds128(e, rq, ra, OMEGA_GDN_SM_RED, 1);
308+ fadd(e, R(sqs), S(rq, 0), S(rq, 1)); fadd(e, R(sqs), R(sqs), S(rq, 2)); fadd(e, R(sqs), R(sqs), S(rq, 3));
309+ fadd(e, R(sqs), R(sqs), R(el2));
310+ mufu(e, BW_IR_MUFU_RSQ, R(sqs), R(sqs));
311+ /* q gets 1/sqrt(128) more: factor = qs + half * (1 - qs) */
312+ i2fp(e, hf, half);
313+ movi(e, fq, fbits(qs)); movi(e, t1, fbits(1.0f - qs));
314+ ffma(e, R(fq), R(hf), R(t1), R(fq));
315+ fmul(e, R(accA), R(accA), R(sqs)); fmul(e, R(accA), R(accA), R(fq));
316+ imadi(e, R(ra), R(tid), 4u, NOREG); /* SM_Q = 0, SM_K = SM_Q + 512 */
317+ sts32(e, ra, R(accA));
318+ bar_sync(e); /* q and k are read across warps in phase D (a zero state hides a missing barrier here: 0 * poison = 0) */
319+ }
320+
321+ /* ---- phase C: scalars ---- */
322+ int pab = V64(e), aab = V64(e), asc = V64(e), abq = V64(e), scq = V64(e);
323+ int xs = V(e), mn = V(e), sp = V(e), dec = V(e), beta = V(e), cl2e = V(e);
324+ if (e->err) return -1;
325+ ldc64(e, pab, OMEGA_GDN_ARG_AB);
326+ imadw(e, aab, h, 8u, pab); imadw(e, asc, h, 8u, ppar);
327+ ldg_wide(e, BW_IR_LDG_E_64, abq, aab, 0, udesc, 1);
328+ ldg_wide(e, BW_IR_LDG_E_64, scq, asc, OMEGA_GDN_PAR_SCAL, udesc, 0);
329+ /* softplus(x) = max(x, 0) + ln2 * LG2(1 + EX2(-|x| * log2 e)), x = a + dt_bias */
330+ fadd(e, R(xs), S(abq, 0), S(scq, 1));
331+ movi(e, mn, C_M1); fmul(e, R(mn), R(xs), R(mn)); fminn(e, R(mn), R(xs), R(mn));
332+ movi(e, cl2e, C_LOG2E);
333+ fmul(e, R(mn), R(mn), R(cl2e)); mufu(e, BW_IR_MUFU_EX2, R(mn), R(mn)); fadd(e, R(mn), R(mn), R(fone));
334+ mufu(e, BW_IR_MUFU_LG2, R(mn), R(mn));
335+ movrz(e, R(sp)); fmaxx(e, R(sp), R(xs), R(sp));
336+ movi(e, t1, C_LN2); ffma(e, R(sp), R(mn), R(t1), R(sp));
337+ /* decay = EX2(negA * softplus * log2 e) */
338+ if (MUT == 1) movi(e, dec, C_ONE);
339+ else { fmul(e, R(dec), S(scq, 0), R(sp)); fmul(e, R(dec), R(dec), R(cl2e)); mufu(e, BW_IR_MUFU_EX2, R(dec), R(dec)); }
340+ /* beta = sigmoid(b) */
341+ if (MUT == 2) movi(e, beta, C_ONE);
342+ else {
343+ fmul(e, R(beta), S(abq, 1), R(cnl2e)); mufu(e, BW_IR_MUFU_EX2, R(beta), R(beta));
344+ fadd(e, R(beta), R(beta), R(fone)); mufu(e, BW_IR_MUFU_RCP, R(beta), R(beta));
345+ }
346+
347+ /* ---- phase D: the state update (fresh registers per step: a reused name stays live across all its uses) ---- */
348+ int aw = V(e), pa = V(e), la = V(e), kq[4], kv[4], dl[4], vq = V128(e);
349+ for (unsigned i = 0; i < 4; i++) { kq[i] = V128(e); kv[i] = V(e); dl[i] = V(e); }
350+ if (e->err) return -1;
351+ imadi(e, R(aw), R(warp), 16u * 4u, NOREG);
352+ imadi(e, R(pa), R(warp), OMEGA_GDN_HEAD_DIM * 4u, NOREG); imadi(e, R(pa), R(lane), 16u, pa); imadi(e, R(pa), R(one), OMEGA_GDN_SM_P, pa);
353+ imadi(e, R(la), R(lane), 16u, NOREG);
354+ consume(e, SB_STATE);
355+ for (unsigned r = 0; r < 16; r++) for (unsigned j = 0; j < 4; j++) fmul(e, S(sq[r], j), S(sq[r], j), R(dec));
356+ /* kv partial over this warp's 16 rows: kv_j = sum_r S[r][j] k[r], then the 8-way cross-warp sum */
357+ for (unsigned i = 0; i < 4; i++) lds128(e, kq[i], aw, OMEGA_GDN_SM_K + i * 16u, i == 0);
358+ for (unsigned j = 0; j < 4; j++) fmul(e, R(kv[j]), S(sq[0], j), S(kq[0], 0));
359+ for (unsigned r = 1; r < 16; r++) for (unsigned j = 0; j < 4; j++) ffma(e, R(kv[j]), S(sq[r], j), S(kq[r / 4], r % 4), R(kv[j]));
360+ cross_warp_sum(e, kv, pa, one, la);
361+ /* delta = (v - kv) * beta; S += k (outer) delta; S back to memory */
362+ lds128(e, vq, la, OMEGA_GDN_SM_V, 1);
363+ for (unsigned j = 0; j < 4; j++) { fsub(e, R(dl[j]), S(vq, j), R(kv[j])); fmul(e, R(dl[j]), R(dl[j]), R(beta)); }
364+ {
365+ int k2[4];
366+ for (unsigned i = 0; i < 4; i++) k2[i] = V128(e);
367+ if (e->err) return -1;
368+ for (unsigned i = 0; i < 4; i++) lds128(e, k2[i], aw, OMEGA_GDN_SM_K + i * 16u, i == 0);
369+ for (unsigned r = 0; r < 16; r++) for (unsigned j = 0; j < 4; j++) ffma(e, S(sq[r], j), S(k2[r / 4], r % 4), R(dl[j]), S(sq[r], j));
370+ }
371+ for (unsigned r = 0; r < 16; r++) for (unsigned j = 0; j < 4; j++) stg(e, sa, r * OMEGA_GDN_HEAD_DIM * 4u + j * 4u, S(sq[r], j), udesc);
372+ /* the gate prefetch: z[h] and the norm weight, consumed in phase E */
373+ int pz = V64(e), ppar2 = V64(e), az = V64(e), an = V64(e), zq = V128(e), nq = V128(e);
374+ if (e->err) return -1;
375+ ldc64(e, pz, OMEGA_GDN_ARG_Z); ldc64(e, ppar2, OMEGA_GDN_ARG_PAR);
376+ imadw(e, az, lane, 16u, pz); imadw(e, az, h, OMEGA_GDN_HEAD_DIM * 4u, az);
377+ imadw(e, an, lane, 16u, ppar2);
378+ ldg_pf(e, zq, az, 0, udesc, SB_GATE);
379+ ldg_pf(e, nq, an, OMEGA_GDN_PAR_NORM, udesc, SB_GATE);
380+ /* o partial: o_j = sum_r S[r][j] q[r], then the cross-warp sum */
381+ int p[4];
382+ {
383+ int q2[4];
384+ for (unsigned i = 0; i < 4; i++) { q2[i] = V128(e); p[i] = V(e); }
385+ if (e->err) return -1;
386+ for (unsigned i = 0; i < 4; i++) lds128(e, q2[i], aw, OMEGA_GDN_SM_Q + i * 16u, i == 0);
387+ for (unsigned j = 0; j < 4; j++) fmul(e, R(p[j]), S(sq[0], j), S(q2[0], 0));
388+ for (unsigned r = 1; r < 16; r++) for (unsigned j = 0; j < 4; j++) ffma(e, R(p[j]), S(sq[r], j), S(q2[r / 4], r % 4), R(p[j]));
389+ }
390+ bar_sync(e); /* every warp has read the kv partials */
391+ cross_warp_sum(e, p, pa, one, la);
392+
393+ /* ---- phase E: gated RMSNorm, the output ---- */
394+ int ss = V(e), en = V(e), osc = V(e), pout = V64(e), ao = V64(e), g = V(e);
395+ if (e->err) return -1;
396+ fmul(e, R(ss), R(p[0]), R(p[0]));
397+ for (unsigned j = 1; j < 4; j++) ffma(e, R(ss), R(p[j]), R(p[j]), R(ss));
398+ warp_sum(e, ss, ts);
399+ imadi(e, R(ra), R(warp), 4u, NOREG); imadi(e, R(ra), R(one), OMEGA_GDN_SM_RED2, ra);
400+ lane0_sts(e, lane, one, ra, R(ss));
401+ bar_sync(e);
402+ ldc32(e, en, OMEGA_GDN_ARG_EPS_NORM); ldc32(e, osc, OMEGA_GDN_ARG_OUT_SCALE); ldc64(e, pout, OMEGA_GDN_ARG_OUT);
403+ lds32(e, R(ss), ra, 0);
404+ movi(e, t1, C_INV128);
405+ ffma(e, R(ss), R(ss), R(t1), R(en));
406+ mufu(e, BW_IR_MUFU_RSQ, R(ss), R(ss));
407+ imadw(e, ao, lane, 16u, pout); imadw(e, ao, h, OMEGA_GDN_HEAD_DIM * 4u, ao);
408+ consume(e, SB_GATE);
409+ for (unsigned j = 0; j < 4; j++) {
410+ fmul(e, R(g), S(zq, j), R(insc));
411+ if (MUT != 3) silu(e, R(g), t1, cnl2e, fone);
412+ fmul(e, R(p[j]), R(p[j]), R(ss)); fmul(e, R(p[j]), R(p[j]), S(nq, j));
413+ fmul(e, R(p[j]), R(p[j]), R(g)); fmul(e, R(p[j]), R(p[j]), R(osc));
414+ }
415+ for (unsigned j = 0; j < 4; j++) stg(e, ao, j * 4u, R(p[j]), udesc);
416+ tail(e);
417+ if (e->err) return -1;
418+ return omega_bw_regalloc_solve(prog);
419+}
420+
421+int omega_gdn_prog_stats(const OmegaGdnSpec *spec, uint32_t *insns, uint32_t *gprs) {
422+ static BlackwellIRProgram prog;
423+ if (omega_blackwell_codegen_gdn_prog(spec, &prog) != 0) return -1;
424+ if (insns) *insns = (uint32_t)prog.count;
425+ if (gprs) *gprs = prog.regalloc.peak_gpr_usage;
426+ return 0;
427+}
428diff --git a/src/omega_blackwell_codegen_gdn.h b/src/omega_blackwell_codegen_gdn.h
429new file mode 100644
430index 00000000..10b7079d
431--- /dev/null
432+++ b/src/omega_blackwell_codegen_gdn.h
433@@ -0,0 +1,92 @@
434+#ifndef OMEGA_BLACKWELL_CODEGEN_GDN_H
435+#define OMEGA_BLACKWELL_CODEGEN_GDN_H
436+/*
437+ * omega_blackwell_codegen_gdn: one decode step of a Gated DeltaNet layer of Qwen3.8-27B in ONE launch (omega#542,
438+ * 2026-10-18). The maths is the host reference sc crates/aien-inference-abi/src/qwen38.rs `delta_net`, from the
439+ * projections on: the caller runs in_proj_qkv / in_proj_z (FP8 GEMVs) and the stacked a/b rows (bf16 GEMV) first and
440+ * out_proj (FP8 GEMV) after. Per value head h (48):
441+ * conv channels q[hk], k[hk] (128 each, hk = h / 3) and v[h] (128) of the 10240-wide qkv row: causal depthwise
442+ * conv over {tail0, tail1, tail2, x} with the layer's 4 taps, then SiLU; the tail shifts by one.
443+ * l2norm q and k: x / sqrt(sum x^2 + eps_l2); q also / sqrt(128).
444+ * scalars beta = sigmoid(b[h]); g = negA[h] * softplus(a[h] + dt_bias[h]), negA = -exp(A_log); decay = exp(g).
445+ * state S[h] (128 k x 128 v, f32): S *= decay; kv = S^T k; delta = (v - kv) * beta; S += k (outer) delta;
446+ * o = S^T q.
447+ * output y = o / sqrt(mean(o^2) + eps_norm) * norm_w * silu(z[h]) * out_scale (norm_w shared by the heads).
448+ *
449+ * Grid 3 x 16 (h = ctaid.y * 3 + ctaid.x, hk = ctaid.y), 256 threads, no host round trip, no atomics. Per CTA:
450+ * prefetch thread (warp w, lane L) owns S[h] rows 16w .. 16w+15, columns 4L .. 4L+3: 16 LDG.128 issued first on a
451+ * held scoreboard, consumed at the state phase (the conv, norm and scalar phases hide the latency).
452+ * phase A thread t convolves channel A (slot t: q channel t for t < 128, k channel t - 128 above) and channel V
453+ * (slot 256 + (t & 127), computed by both halves alike); taps and tail are one LDG.128 each.
454+ * phase B sums of squares of q and k per 4-warp half (SHFL + shared), rsqrt, q|k to shared, v to shared; then the
455+ * shifted tails are stored (after the barrier: the two halves read the same v tail slot).
456+ * phase C every thread: a and b (one LDG.64), negA and dt_bias (one LDG.64), softplus via EX2/LG2, decay, beta.
457+ * phase D the state update in registers; kv and o are 8-way cross-warp sums through shared memory; S is stored
458+ * back with 64 STG.E per thread (immediate offsets).
459+ * phase E every warp holds all 128 o values: sum of squares by SHFL, broadcast through shared, gated norm, store.
460+ * Branch-free except the lane-0 shared stores (BSSY / BSYNC regions). Ops used: the router's set plus MUFU.LG2
461+ * (encoding checked with nvdisasm, omega_blackwell_codegen.h) and I2FP.
462+ */
463+#include "omega_blackwell_codegen.h"
464+#include <stdint.h>
465+
466+#define OMEGA_GDN_KEY_HEADS 16u
467+#define OMEGA_GDN_VALUE_HEADS 48u
468+#define OMEGA_GDN_HEAD_DIM 128u /* k and v head dim */
469+#define OMEGA_GDN_CONV_DIM 10240u /* q 2048 | k 2048 | v 6144 */
470+#define OMEGA_GDN_V_DIM (OMEGA_GDN_VALUE_HEADS * OMEGA_GDN_HEAD_DIM) /* 6144 */
471+#define OMEGA_GDN_TAPS 4u
472+#define OMEGA_GDN_SLOTS 384u /* conv channels one head reads: q | k | v */
473+
474+/* per-layer parameter block (bytes): conv taps f32 [10240][4] | {negA, dt_bias} f32 [48][2] | norm_w f32 [128] */
475+#define OMEGA_GDN_PAR_CONV 0u
476+#define OMEGA_GDN_PAR_SCAL 163840u
477+#define OMEGA_GDN_PAR_NORM 164224u
478+#define OMEGA_GDN_PAR_BYTES 164736u
479+/* per-layer conv tail: f32 [48 heads][384 slots][4] = {x_{t-3}, x_{t-2}, x_{t-1}, unused}; each head keeps its own
480+ * copy of the q/k channels of its key head (the three heads of a key head hold equal copies), so no CTA reads a word
481+ * another CTA writes */
482+#define OMEGA_GDN_TAIL_HEAD_BYTES (OMEGA_GDN_SLOTS * 16u) /* 6144 */
483+#define OMEGA_GDN_TAIL_BYTES (OMEGA_GDN_VALUE_HEADS * OMEGA_GDN_TAIL_HEAD_BYTES)
484+/* per-layer state: f32 [48][128 k][128 v], the reference layout */
485+#define OMEGA_GDN_STATE_HEAD_BYTES (OMEGA_GDN_HEAD_DIM * OMEGA_GDN_HEAD_DIM * 4u) /* 65536 */
486+#define OMEGA_GDN_STATE_BYTES (OMEGA_GDN_VALUE_HEADS * OMEGA_GDN_STATE_HEAD_BYTES)
487+
488+/* constant bank: addresses at 0x380 + 8n, then the scalars */
489+#define OMEGA_GDN_ARG_QKV 0x380u /* f32 [10240] in_proj_qkv row (times in_scale) */
490+#define OMEGA_GDN_ARG_Z 0x388u /* f32 [6144] in_proj_z row (times in_scale) */
491+#define OMEGA_GDN_ARG_AB 0x390u /* f32 [48][2] {a[h], b[h]}: the bf16 GEMV of in_proj_a / in_proj_b rows interleaved */
492+#define OMEGA_GDN_ARG_PAR 0x398u /* the layer's parameter block */
493+#define OMEGA_GDN_ARG_TAIL 0x3a0u /* the layer's conv tail */
494+#define OMEGA_GDN_ARG_STATE 0x3a8u /* the layer's state */
495+#define OMEGA_GDN_ARG_OUT 0x3b0u /* f32 [6144] y (the out_proj input) */
496+#define OMEGA_GDN_ARG_EPS_L2 0x3b8u /* f32, 1e-6 in the reference */
497+#define OMEGA_GDN_ARG_EPS_NORM 0x3bcu /* f32, rms_norm_eps */
498+#define OMEGA_GDN_ARG_IN_SCALE 0x3c0u /* f32: qkv and z are multiplied by it on read */
499+#define OMEGA_GDN_ARG_OUT_SCALE 0x3c4u /* f32: y is multiplied by it */
500+#define OMEGA_GDN_ARGS_WORDS 18u
501+
502+/* shared memory map (bytes) */
503+#define OMEGA_GDN_SM_Q 0u /* q[128] f32, normalised and / sqrt(128) */
504+#define OMEGA_GDN_SM_K 512u /* k[128] f32 */
505+#define OMEGA_GDN_SM_V 1024u /* v[128] f32 */
506+#define OMEGA_GDN_SM_RED 1536u /* per-warp sums of squares of q|k [8] */
507+#define OMEGA_GDN_SM_P 1600u /* per-warp partials [8][128] f32 (kv, then o) */
508+#define OMEGA_GDN_SM_RED2 5696u /* per-warp sums of squares of o [8] */
509+#define OMEGA_GDN_SHARED_BYTES 5728u
510+
511+#define OMEGA_GDN_THREADS 256u
512+#define OMEGA_GDN_GRID_X 3u
513+#define OMEGA_GDN_GRID_Y 16u
514+
515+typedef struct {
516+ int mutant; /* TEST ONLY. 0 none; 1 no decay (decay = 1); 2 beta = 1; 3 the output gate is z, not silu(z);
517+ * 4 the conv tail is not shifted (the next step convolves stale history). */
518+} OmegaGdnSpec;
519+
520+/* Builds the register-allocated IR program into *prog. 0 ok, -1 refused. */
521+int omega_blackwell_codegen_gdn_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog);
522+/* Instruction and peak-register counts (host, no device). */
523+int omega_gdn_prog_stats(const OmegaGdnSpec *spec, uint32_t *insns, uint32_t *gprs);
524+
525+#endif
526diff --git a/src/omega_gpu_gdn_api.c b/src/omega_gpu_gdn_api.c
527new file mode 100644
528index 00000000..6df97a82
529--- /dev/null
530+++ b/src/omega_gpu_gdn_api.c
531@@ -0,0 +1,310 @@
532+/*
533+ * omega_gpu_gdn_api.c: resident Gated DeltaNet parameters, tails, states and the one-launch decode step (see the
534+ * header). Session use mirrors omega_gpu_router_api.c: every buffer from omega_gpu_session_alloc (GPU-uncached,
535+ * host-visible), sync_to_gpu after host writes, sync_to_cpu before host reads, one omega_gpu_session_launch per call.
536+ */
537+#include "omega_gpu_gdn_api.h"
538+#include "omega_gpu_session.h"
539+#include "omega_gpu_cmdlist.h"
540+#include <math.h>
541+#include <stdbool.h>
542+#include <string.h>
543+
544+#define MARKER_TIMEOUT_MS 20000ull
545+#define POISON_F32 (-999.0f)
546+
547+/* the launch buffer: one allocation, fixed offsets (bytes) */
548+#define LB_QKV 0u
549+#define LB_Z (LB_QKV + OMEGA_GDN_CONV_DIM * 4u)
550+#define LB_AB (LB_Z + OMEGA_GDN_V_DIM * 4u)
551+#define LB_OUT (LB_AB + OMEGA_GDN_VALUE_HEADS * 8u)
552+#define LB_BYTES (LB_OUT + OMEGA_GDN_V_DIM * 4u)
553+
554+struct OmegaGpuGdn {
555+ uint32_t layers;
556+ OmegaGpuMem par; /* [layers][OMEGA_GDN_PAR_BYTES] */
557+ OmegaGpuMem tail; /* [layers][OMEGA_GDN_TAIL_BYTES] */
558+ OmegaGpuMem state; /* [layers][OMEGA_GDN_STATE_BYTES] */
559+ OmegaGpuMem lb; /* launch buffer */
560+};
561+
562+typedef struct {
563+ bool valid;
564+ OmegaGdnSpec spec;
565+ OmegaBlackwellKernel kernel;
566+ OmegaGpuMem code;
567+ uint32_t insns;
568+} Slot;
569+
570+static struct {
571+ bool hooked;
572+ Slot slot;
573+ int mutant;
574+} g_k;
575+static int g_rec; /* 1 while a record call runs (lock held): kernel_for must not evict */
576+
577+static void slot_clear_locked(void) { if (g_k.slot.valid) omega_gpu_session_free(&g_k.slot.code); memset(&g_k.slot, 0, sizeof g_k.slot); }
578+static void on_session_close(void) { memset(&g_k.slot, 0, sizeof g_k.slot); }
579+static OmegaGpuSession *dev_open_locked(void) {
580+ if (!g_k.hooked) { (void)omega_gpu_session_on_close(on_session_close); g_k.hooked = true; }
581+ return omega_gpu_session_open();
582+}
583+
584+void omega_gpu_gdn_test_set_mutant(int mutant) { omega_gpu_session_lock(); g_k.mutant = mutant; slot_clear_locked(); omega_gpu_session_unlock(); }
585+const char *omega_gpu_gdn_last_error(void) { return omega_gpu_session_last_error(); }
586+const char *omega_gpu_gdn_rc_name(int rc) {
587+ switch (rc) {
588+ case OMEGA_GPU_GDN_OK: return "OK";
589+ case OMEGA_GPU_GDN_BAD_ARGS: return "BAD_ARGS";
590+ case OMEGA_GPU_GDN_TOO_LARGE: return "TOO_LARGE";
591+ case OMEGA_GPU_GDN_CODEGEN_FAIL: return "CODEGEN_FAIL";
592+ case OMEGA_GPU_GDN_CHIP_FAIL: return "CHIP_FAIL";
593+ default: return "?";
594+ }
595+}
596+
597+/* ---- host layout ---- */
598+/* the qkv channel slot s of value head h reads */
599+static uint32_t slot_channel(uint32_t h, uint32_t s) {
600+ const uint32_t hk = h / (OMEGA_GDN_VALUE_HEADS / OMEGA_GDN_KEY_HEADS), D = OMEGA_GDN_HEAD_DIM;
601+ if (s < D) return hk * D + s;
602+ if (s < 2u * D) return OMEGA_GDN_KEY_HEADS * D + hk * D + (s - D);
603+ return 2u * OMEGA_GDN_KEY_HEADS * D + h * D + (s - 2u * D);
604+}
605+
606+int omega_gdn_pack_params(const float *conv_w, const float *a_log, const float *dt_bias, const float *norm_w, uint8_t *block) {
607+ if (!conv_w || !a_log || !dt_bias || !norm_w || !block) return -1;
608+ memcpy(block + OMEGA_GDN_PAR_CONV, conv_w, (size_t)OMEGA_GDN_CONV_DIM * OMEGA_GDN_TAPS * 4u);
609+ float *sc = (float *)(block + OMEGA_GDN_PAR_SCAL);
610+ for (uint32_t h = 0; h < OMEGA_GDN_VALUE_HEADS; h++) { sc[2 * h] = -expf(a_log[h]); sc[2 * h + 1] = dt_bias[h]; }
611+ memcpy(block + OMEGA_GDN_PAR_NORM, norm_w, OMEGA_GDN_HEAD_DIM * 4u);
612+ return 0;
613+}
614+
615+void omega_gdn_pack_tail(const float *tail, float *dev) {
616+ for (uint32_t h = 0; h < OMEGA_GDN_VALUE_HEADS; h++)
617+ for (uint32_t s = 0; s < OMEGA_GDN_SLOTS; s++) {
618+ float *d = dev + ((size_t)h * OMEGA_GDN_SLOTS + s) * 4u;
619+ const uint32_t ch = slot_channel(h, s);
620+ for (uint32_t j = 0; j < 3; j++) d[j] = tail[(size_t)j * OMEGA_GDN_CONV_DIM + ch];
621+ d[3] = 0.0f;
622+ }
623+}
624+
625+int omega_gdn_unpack_tail(const float *dev, float *tail) {
626+ int rc = 0;
627+ for (uint32_t h = 0; h < OMEGA_GDN_VALUE_HEADS; h++)
628+ for (uint32_t s = 0; s < OMEGA_GDN_SLOTS; s++) {
629+ const float *d = dev + ((size_t)h * OMEGA_GDN_SLOTS + s) * 4u;
630+ const uint32_t ch = slot_channel(h, s);
631+ const bool first = s >= 2u * OMEGA_GDN_HEAD_DIM || h % (OMEGA_GDN_VALUE_HEADS / OMEGA_GDN_KEY_HEADS) == 0;
632+ for (uint32_t j = 0; j < 3; j++) {
633+ float *t = &tail[(size_t)j * OMEGA_GDN_CONV_DIM + ch];
634+ if (first) *t = d[j];
635+ else if (memcmp(t, &d[j], 4) != 0) rc = -1;
636+ }
637+ }
638+ return rc;
639+}
640+
641+/* ---- buffers ---- */
642+int omega_gpu_gdn_alloc(uint32_t layers, OmegaGpuGdn **out) {
643+ if (!out || layers == 0 || layers > OMEGA_GPU_GDN_MAX_LAYERS) return OMEGA_GPU_GDN_BAD_ARGS;
644+ *out = NULL;
645+ OmegaGpuGdn *g = omega_gpu_host_calloc(1, sizeof *g);
646+ if (!g) return OMEGA_GPU_GDN_BAD_ARGS;
647+ g->layers = layers;
648+ omega_gpu_session_lock();
649+ int rc = OMEGA_GPU_GDN_OK;
650+ if (!dev_open_locked()) rc = OMEGA_GPU_GDN_CHIP_FAIL;
651+ if (rc == OMEGA_GPU_GDN_OK && omega_gpu_session_alloc((size_t)layers * OMEGA_GDN_PAR_BYTES, &g->par) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL;
652+ if (rc == OMEGA_GPU_GDN_OK && omega_gpu_session_alloc((size_t)layers * OMEGA_GDN_TAIL_BYTES, &g->tail) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL;
653+ if (rc == OMEGA_GPU_GDN_OK && omega_gpu_session_alloc((size_t)layers * OMEGA_GDN_STATE_BYTES, &g->state) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL;
654+ if (rc == OMEGA_GPU_GDN_OK && omega_gpu_session_alloc(LB_BYTES, &g->lb) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL;
655+ if (rc == OMEGA_GPU_GDN_OK) {
656+ memset(g->par.cpu, 0, (size_t)layers * OMEGA_GDN_PAR_BYTES);
657+ memset(g->tail.cpu, 0, (size_t)layers * OMEGA_GDN_TAIL_BYTES);
658+ memset(g->state.cpu, 0, (size_t)layers * OMEGA_GDN_STATE_BYTES);
659+ memset(g->lb.cpu, 0, LB_BYTES);
660+ if (omega_gpu_session_sync_to_gpu(&g->par) != 0 || omega_gpu_session_sync_to_gpu(&g->tail) != 0 ||
661+ omega_gpu_session_sync_to_gpu(&g->state) != 0 || omega_gpu_session_sync_to_gpu(&g->lb) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL;
662+ }
663+ omega_gpu_session_unlock();
664+ if (rc != OMEGA_GPU_GDN_OK) { omega_gpu_gdn_free(g); return rc; }
665+ *out = g;
666+ return OMEGA_GPU_GDN_OK;
667+}
668+
669+void omega_gpu_gdn_free(OmegaGpuGdn *g) {
670+ if (!g) return;
671+ omega_gpu_session_lock();
672+ if (g->par.cpu) omega_gpu_session_free(&g->par);
673+ if (g->tail.cpu) omega_gpu_session_free(&g->tail);
674+ if (g->state.cpu) omega_gpu_session_free(&g->state);
675+ if (g->lb.cpu) omega_gpu_session_free(&g->lb);
676+ omega_gpu_session_unlock();
677+ omega_gpu_host_free(g);
678+}
679+
680+int omega_gpu_gdn_set_layer(OmegaGpuGdn *g, uint32_t layer, const float *conv_w, const float *a_log, const float *dt_bias,
681+ const float *norm_w) {
682+ if (!g || layer >= g->layers) return OMEGA_GPU_GDN_BAD_ARGS;
683+ omega_gpu_session_lock();
684+ int rc = omega_gdn_pack_params(conv_w, a_log, dt_bias, norm_w, (uint8_t *)g->par.cpu + (size_t)layer * OMEGA_GDN_PAR_BYTES) == 0
685+ ? OMEGA_GPU_GDN_OK : OMEGA_GPU_GDN_BAD_ARGS;
686+ if (rc == OMEGA_GPU_GDN_OK && omega_gpu_session_sync_to_gpu(&g->par) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL;
687+ omega_gpu_session_unlock();
688+ return rc;
689+}
690+
691+int omega_gpu_gdn_reset(OmegaGpuGdn *g) {
692+ if (!g) return OMEGA_GPU_GDN_BAD_ARGS;
693+ omega_gpu_session_lock();
694+ memset(g->tail.cpu, 0, (size_t)g->layers * OMEGA_GDN_TAIL_BYTES);
695+ memset(g->state.cpu, 0, (size_t)g->layers * OMEGA_GDN_STATE_BYTES);
696+ int rc = (omega_gpu_session_sync_to_gpu(&g->tail) == 0 && omega_gpu_session_sync_to_gpu(&g->state) == 0) ? OMEGA_GPU_GDN_OK : OMEGA_GPU_GDN_CHIP_FAIL;
697+ omega_gpu_session_unlock();
698+ return rc;
699+}
700+
701+int omega_gpu_gdn_set_state(OmegaGpuGdn *g, uint32_t layer, const float *tail, const float *state) {
702+ if (!g || layer >= g->layers || !tail || !state) return OMEGA_GPU_GDN_BAD_ARGS;
703+ omega_gpu_session_lock();
704+ omega_gdn_pack_tail(tail, (float *)((uint8_t *)g->tail.cpu + (size_t)layer * OMEGA_GDN_TAIL_BYTES));
705+ memcpy((uint8_t *)g->state.cpu + (size_t)layer * OMEGA_GDN_STATE_BYTES, state, OMEGA_GDN_STATE_BYTES);
706+ int rc = (omega_gpu_session_sync_to_gpu(&g->tail) == 0 && omega_gpu_session_sync_to_gpu(&g->state) == 0) ? OMEGA_GPU_GDN_OK : OMEGA_GPU_GDN_CHIP_FAIL;
707+ omega_gpu_session_unlock();
708+ return rc;
709+}
710+
711+int omega_gpu_gdn_get_state(OmegaGpuGdn *g, uint32_t layer, float *tail, float *state) {
712+ if (!g || layer >= g->layers || (!tail && !state)) return OMEGA_GPU_GDN_BAD_ARGS;
713+ omega_gpu_session_lock();
714+ int rc = (omega_gpu_session_sync_to_cpu(&g->tail) == 0 && omega_gpu_session_sync_to_cpu(&g->state) == 0) ? OMEGA_GPU_GDN_OK : OMEGA_GPU_GDN_CHIP_FAIL;
715+ if (rc == OMEGA_GPU_GDN_OK && tail &&
716+ omega_gdn_unpack_tail((const float *)((const uint8_t *)g->tail.cpu + (size_t)layer * OMEGA_GDN_TAIL_BYTES), tail) != 0) {
717+ omega_gpu_session_set_error("gdn: the per-head copies of a key head's conv tail disagree");
718+ rc = OMEGA_GPU_GDN_CHIP_FAIL;
719+ }
720+ if (rc == OMEGA_GPU_GDN_OK && state) memcpy(state, (const uint8_t *)g->state.cpu + (size_t)layer * OMEGA_GDN_STATE_BYTES, OMEGA_GDN_STATE_BYTES);
721+ omega_gpu_session_unlock();
722+ return rc;
723+}
724+
725+/* ---- kernel ---- */
726+static int kernel_for(const OmegaGdnSpec *spec, Slot **out, bool *hit) {
727+ Slot *s = &g_k.slot;
728+ if (s->valid && memcmp(&s->spec, spec, sizeof *spec) == 0) { *out = s; *hit = true; return OMEGA_GPU_GDN_OK; }
729+ if (g_rec && s->valid) { omega_gpu_session_set_error("gdn record: kernel cache miss would evict the resident kernel (warm up before recording)"); return OMEGA_GPU_GDN_TOO_LARGE; }
730+ slot_clear_locked();
731+ BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog);
732+ if (!prog) { omega_gpu_session_set_error("gdn codegen: host memory"); return OMEGA_GPU_GDN_CODEGEN_FAIL; }
733+ int cg = omega_blackwell_codegen_gdn_prog(spec, prog);
734+ size_t need = cg == 0 ? omega_bw_program_code_bytes(prog) : 0;
735+ if (cg != 0 || need == 0) { omega_gpu_host_free(prog); omega_gpu_session_set_error("gdn codegen refused the spec"); return OMEGA_GPU_GDN_CODEGEN_FAIL; }
736+ if ((uint64_t)OMEGA_GDN_THREADS * omega_bw_gpr_declared(prog->regalloc.peak_gpr_usage) > 65536u) { omega_gpu_host_free(prog); omega_gpu_session_set_error("gdn: register file"); return OMEGA_GPU_GDN_TOO_LARGE; }
737+ const uint32_t insns = (uint32_t)prog->count;
738+ OmegaGpuMem code;
739+ if (omega_gpu_session_alloc_code(need, &code) != 0) { omega_gpu_host_free(prog); return OMEGA_GPU_GDN_CHIP_FAIL; }
740+ OmegaBlackwellKernel kernel;
741+ cg = omega_blackwell_kernel_encode_into(prog, code.cpu, need, &kernel);
742+ omega_gpu_host_free(prog);
743+ if (cg != 0 || !kernel.code || kernel.code_size != need) { omega_gpu_session_free(&code); omega_gpu_session_set_error("gdn encode"); return OMEGA_GPU_GDN_CODEGEN_FAIL; }
744+ if (omega_gpu_session_sync_to_gpu(&code) != 0) { omega_gpu_session_free(&code); return OMEGA_GPU_GDN_CHIP_FAIL; }
745+ memset(s, 0, sizeof *s);
746+ s->valid = true; s->spec = *spec; s->kernel = kernel; s->code = code; s->insns = insns;
747+ *out = s; *hit = false;
748+ return OMEGA_GPU_GDN_OK;
749+}
750+
751+/* rec non-NULL (record path, caller holds the session lock): the inputs are the device rows dev_*, the launch is
752+ * appended to rec instead of submitted, nothing is staged, poisoned or read back. */
753+static int gdn_core(OmegaGpuGdn *g, uint32_t layer, const float *qkv, const float *z, const float *ab, float eps_l2,
754+ float eps_norm, float in_scale, float out_scale, float *out, OmegaGpuGdnInfo *info,
755+ OmegaGpuCmdList *rec, uint64_t dev_qkv, uint64_t dev_z, uint64_t dev_ab) {
756+ uint64_t t0 = omega_gpu_session_now_ns();
757+ if (info) memset(info, 0, sizeof *info);
758+ if (!g || layer >= g->layers || (rec ? (!dev_qkv || !dev_z || !dev_ab) : (!qkv || !z || !ab || !out))) return OMEGA_GPU_GDN_BAD_ARGS;
759+ if (!isfinite(eps_l2) || !isfinite(eps_norm) || !isfinite(in_scale) || !isfinite(out_scale)) return OMEGA_GPU_GDN_BAD_ARGS;
760+ OmegaGdnSpec spec; memset(&spec, 0, sizeof spec);
761+ spec.mutant = g_k.mutant;
762+
763+ if (!rec) omega_gpu_session_lock();
764+#define UNLOCK() do { if (!rec) omega_gpu_session_unlock(); } while (0)
765+ int rc = OMEGA_GPU_GDN_OK;
766+ if (!dev_open_locked()) { UNLOCK(); return OMEGA_GPU_GDN_CHIP_FAIL; }
767+ Slot *ks = NULL; bool hit = false;
768+ g_rec = rec != NULL;
769+ rc = kernel_for(&spec, &ks, &hit);
770+ g_rec = 0;
771+ if (rc != OMEGA_GPU_GDN_OK) { UNLOCK(); return rc; }
772+ uint8_t *b = g->lb.cpu;
773+ float *yo = (float *)(b + LB_OUT);
774+ if (!rec) {
775+ memcpy(b + LB_QKV, qkv, OMEGA_GDN_CONV_DIM * 4u);
776+ memcpy(b + LB_Z, z, OMEGA_GDN_V_DIM * 4u);
777+ memcpy(b + LB_AB, ab, OMEGA_GDN_VALUE_HEADS * 8u);
778+ for (size_t i = 0; i < OMEGA_GDN_V_DIM; i++) yo[i] = POISON_F32;
779+ if (omega_gpu_session_sync_to_gpu(&g->lb) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL;
780+ }
781+
782+ uint32_t args[OMEGA_GDN_ARGS_WORDS];
783+#define A64(i, v) do { args[i] = (uint32_t)(v); args[(i) + 1] = (uint32_t)((v) >> 32); } while (0)
784+ A64(0, rec ? dev_qkv : g->lb.va + LB_QKV); A64(2, rec ? dev_z : g->lb.va + LB_Z); A64(4, rec ? dev_ab : g->lb.va + LB_AB);
785+ A64(6, g->par.va + (uint64_t)layer * OMEGA_GDN_PAR_BYTES); A64(8, g->tail.va + (uint64_t)layer * OMEGA_GDN_TAIL_BYTES);
786+ A64(10, g->state.va + (uint64_t)layer * OMEGA_GDN_STATE_BYTES); A64(12, g->lb.va + LB_OUT);
787+#undef A64
788+ memcpy(&args[14], &eps_l2, 4); memcpy(&args[15], &eps_norm, 4); memcpy(&args[16], &in_scale, 4); memcpy(&args[17], &out_scale, 4);
789+ if (info) {
790+ info->layer = layer; info->threads = OMEGA_GDN_THREADS; info->grid_x = OMEGA_GDN_GRID_X; info->grid_y = OMEGA_GDN_GRID_Y;
791+ info->insns = ks->insns; info->gprs = ks->kernel.gpr_count;
792+ info->state_bytes = 2ull * OMEGA_GDN_STATE_BYTES;
793+ info->kernel_cache_hit = hit;
794+ }
795+ if (rc == OMEGA_GPU_GDN_OK) {
796+ OmegaGpuLaunch L = {
797+ .code_va = ks->code.va, .gpr_count = omega_bw_gpr_declared(ks->kernel.gpr_count),
798+ .threads_x = OMEGA_GDN_THREADS, .threads_y = 1, .grid_x = OMEGA_GDN_GRID_X, .grid_y = OMEGA_GDN_GRID_Y,
799+ .num_elements = (size_t)OMEGA_GDN_V_DIM,
800+ .args = args, .n_args = OMEGA_GDN_ARGS_WORDS, .timeout_ms = MARKER_TIMEOUT_MS,
801+ .shared_bytes = OMEGA_GDN_SHARED_BYTES,
802+ };
803+ if (!omega_bw_gpr_declared_ok(L.gpr_count, ks->kernel.gpr_count)) { omega_gpu_session_set_error("declared registers below peak + 2: refusing to launch"); rc = OMEGA_GPU_GDN_CHIP_FAIL; }
804+ else if (rec) { if (omega_gpu_cmdlist_add(rec, &L) < 0) rc = OMEGA_GPU_GDN_CHIP_FAIL; }
805+ else if (omega_gpu_session_launch(&L, info ? &info->elapsed_ns : NULL, info ? &info->completion_marker : NULL) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL;
806+ }
807+ if (rc == OMEGA_GPU_GDN_OK && !rec && omega_gpu_session_sync_to_cpu(&g->lb) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL;
808+ if (rc == OMEGA_GPU_GDN_OK && !rec) {
809+ size_t unchanged = 0;
810+ for (size_t i = 0; i < OMEGA_GDN_V_DIM; i++) if (yo[i] == POISON_F32) unchanged++;
811+ if (unchanged) { omega_gpu_session_set_error("gdn: output poison survived"); rc = OMEGA_GPU_GDN_CHIP_FAIL; }
812+ else memcpy(out, yo, OMEGA_GDN_V_DIM * 4u);
813+ }
814+ UNLOCK();
815+#undef UNLOCK
816+ if (info) info->call_ns = omega_gpu_session_now_ns() - t0;
817+ return rc;
818+}
819+
820+int omega_gpu_gdn_step(OmegaGpuGdn *g, uint32_t layer, const float *qkv, const float *z, const float *ab, float eps_l2,
821+ float eps_norm, float in_scale, float out_scale, float *out, OmegaGpuGdnInfo *info) {
822+ return gdn_core(g, layer, qkv, z, ab, eps_l2, eps_norm, in_scale, out_scale, out, info, NULL, 0, 0, 0);
823+}
824+
825+/* Record path (decode graph): see the header. Caller holds omega_gpu_session_lock(). */
826+int omega_gpu_gdn_record(OmegaGpuCmdList *cl, OmegaGpuGdn *g, uint32_t layer, uint64_t qkv_va, uint64_t z_va, uint64_t ab_va,
827+ float eps_l2, float eps_norm, float in_scale, float out_scale, uint64_t *out_va) {
828+ if (!cl) return OMEGA_GPU_GDN_BAD_ARGS;
829+ int rc = gdn_core(g, layer, NULL, NULL, NULL, eps_l2, eps_norm, in_scale, out_scale, NULL, NULL, cl, qkv_va, z_va, ab_va);
830+ if (rc == OMEGA_GPU_GDN_OK && out_va) *out_va = g->lb.va + LB_OUT;
831+ return rc;
832+}
833+
834+int omega_gpu_gdn_read_out(OmegaGpuGdn *g, float *out) {
835+ if (!g || !out) return OMEGA_GPU_GDN_BAD_ARGS;
836+ omega_gpu_session_lock();
837+ int rc = omega_gpu_session_sync_to_cpu(&g->lb) == 0 ? OMEGA_GPU_GDN_OK : OMEGA_GPU_GDN_CHIP_FAIL;
838+ if (rc == OMEGA_GPU_GDN_OK) memcpy(out, (const uint8_t *)g->lb.cpu + LB_OUT, OMEGA_GDN_V_DIM * 4u);
839+ omega_gpu_session_unlock();
840+ return rc;
841+}
842diff --git a/src/omega_gpu_gdn_api.h b/src/omega_gpu_gdn_api.h
843new file mode 100644
844index 00000000..fe387699
845--- /dev/null
846+++ b/src/omega_gpu_gdn_api.h
847@@ -0,0 +1,86 @@
848+#ifndef OMEGA_GPU_GDN_API_H
849+#define OMEGA_GPU_GDN_API_H
850+/*
851+ * omega_gpu_gdn_api: the Gated DeltaNet decode step of Qwen3.8-27B in one launch per layer (omega#542, 2026-10-18).
852+ * The kernel is described in omega_blackwell_codegen_gdn.h; the maths is sc qwen38.rs `delta_net`.
853+ *
854+ * int omega_gpu_gdn_alloc(uint32_t layers, OmegaGpuGdn **out);
855+ * Device-resident per-layer parameters, conv tails and recurrent states (zeroed), plus the launch buffer.
856+ * int omega_gpu_gdn_set_layer(OmegaGpuGdn *g, uint32_t layer, const float *conv_w, const float *a_log,
857+ * const float *dt_bias, const float *norm_w);
858+ * conv_w: linear_attn.conv1d.weight [10240][1][4] widened to f32 ([10240][4]); a_log, dt_bias: [48];
859+ * norm_w: linear_attn.norm.weight [128]. negA = -expf(a_log) is computed here (float).
860+ * int omega_gpu_gdn_set_state / omega_gpu_gdn_get_state(OmegaGpuGdn *g, uint32_t layer, float *tail, float *state);
861+ * The reference layouts: tail [3][10240] (oldest first, qwen38.rs conv_tail), state [48][128 k][128 v].
862+ * get_state refuses (CHIP_FAIL) when the three per-head copies of a key head's q/k tail disagree.
863+ * int omega_gpu_gdn_reset(OmegaGpuGdn *g); every layer's tail and state to zero (a new sequence)
864+ * int omega_gpu_gdn_step(OmegaGpuGdn *g, uint32_t layer, const float *qkv, const float *z, const float *ab,
865+ * float eps_l2, float eps_norm, float in_scale, float out_scale, float *out,
866+ * OmegaGpuGdnInfo *info);
867+ * qkv [10240] and z [6144] are the in_proj outputs (times 1 / in_scale: the kernel multiplies them by in_scale);
868+ * ab [48][2] = {a[h], b[h]}: the in_proj_a and in_proj_b rows stacked interleaved (row 2h = a_h, 2h + 1 = b_h)
869+ * give it from one bf16 GEMV. out [6144] = the out_proj input times out_scale. Updates the layer's tail and
870+ * state on the device. eps_l2 is 1e-6 in the reference; eps_norm is rms_norm_eps.
871+ * int omega_gpu_gdn_record(cl, g, layer, qkv_va, z_va, ab_va, eps_l2, eps_norm, in_scale, out_scale, &out_va);
872+ * The same launch appended to a command list (caller holds omega_gpu_session_lock()); inputs are device
873+ * addresses, out lands in the launch buffer (out_va). A kernel cache miss that would evict is refused.
874+ *
875+ * Every buffer is GPU-uncached host-visible session memory (nvos.h:1117 rule), launched through
876+ * omega_gpu_session_launch like the router. The output is poisoned before the launch; a surviving poison word is
877+ * CHIP_FAIL.
878+ */
879+#include "omega_blackwell_codegen_gdn.h"
880+#include <stddef.h>
881+#include <stdint.h>
882+
883+enum {
884+ OMEGA_GPU_GDN_OK = 0,
885+ OMEGA_GPU_GDN_BAD_ARGS = -1,
886+ OMEGA_GPU_GDN_TOO_LARGE = -2,
887+ OMEGA_GPU_GDN_CODEGEN_FAIL = -3,
888+ OMEGA_GPU_GDN_CHIP_FAIL = -4
889+};
890+
891+#define OMEGA_GPU_GDN_MAX_LAYERS 64u
892+#define OMEGA_GPU_GDN_TAIL_FLOATS (3u * OMEGA_GDN_CONV_DIM) /* reference tail */
893+#define OMEGA_GPU_GDN_STATE_FLOATS (OMEGA_GDN_VALUE_HEADS * OMEGA_GDN_HEAD_DIM * OMEGA_GDN_HEAD_DIM) /* 786432 */
894+
895+typedef struct OmegaGpuGdn OmegaGpuGdn;
896+
897+typedef struct {
898+ uint32_t layer, threads, grid_x, grid_y, insns, gprs;
899+ uint64_t state_bytes; /* state bytes read and written: 2 * 48 * 128 * 128 * 4 */
900+ uint64_t elapsed_ns; /* marker-to-marker chip time */
901+ uint64_t call_ns; /* whole call */
902+ uint32_t completion_marker;
903+ int kernel_cache_hit;
904+} OmegaGpuGdnInfo;
905+
906+int omega_gpu_gdn_alloc(uint32_t layers, OmegaGpuGdn **out);
907+void omega_gpu_gdn_free(OmegaGpuGdn *g);
908+int omega_gpu_gdn_set_layer(OmegaGpuGdn *g, uint32_t layer, const float *conv_w, const float *a_log, const float *dt_bias,
909+ const float *norm_w);
910+int omega_gpu_gdn_reset(OmegaGpuGdn *g);
911+int omega_gpu_gdn_set_state(OmegaGpuGdn *g, uint32_t layer, const float *tail, const float *state);
912+int omega_gpu_gdn_get_state(OmegaGpuGdn *g, uint32_t layer, float *tail, float *state);
913+int omega_gpu_gdn_step(OmegaGpuGdn *g, uint32_t layer, const float *qkv, const float *z, const float *ab, float eps_l2,
914+ float eps_norm, float in_scale, float out_scale, float *out, OmegaGpuGdnInfo *info);
915+struct OmegaGpuCmdList;
916+int omega_gpu_gdn_record(struct OmegaGpuCmdList *cl, OmegaGpuGdn *g, uint32_t layer, uint64_t qkv_va, uint64_t z_va,
917+ uint64_t ab_va, float eps_l2, float eps_norm, float in_scale, float out_scale, uint64_t *out_va);
918+/* Test helper: the out row the last launch wrote to the launch buffer (after a record + submit, for example). */
919+int omega_gpu_gdn_read_out(OmegaGpuGdn *g, float *out);
920+const char *omega_gpu_gdn_last_error(void);
921+const char *omega_gpu_gdn_rc_name(int rc);
922+
923+/* Host layout helpers (no device; the test's simulator path uses them too). */
924+/* The parameter block (OMEGA_GDN_PAR_BYTES) from the checkpoint tensors (see set_layer). -1 on a NULL pointer. */
925+int omega_gdn_pack_params(const float *conv_w, const float *a_log, const float *dt_bias, const float *norm_w, uint8_t *block);
926+/* reference tail [3][10240] <-> device tail [48][384][4] (unpack: the q/k channels from head 3 * hk; -1 when the
927+ * three copies of a key head disagree bit for bit) */
928+void omega_gdn_pack_tail(const float *tail, float *dev);
929+int omega_gdn_unpack_tail(const float *dev, float *tail);
930+
931+void omega_gpu_gdn_test_set_mutant(int mutant); /* TEST ONLY: OmegaGdnSpec.mutant; clears the kernel cache */
932+
933+#endif
934diff --git a/tests/bw_warp_sim_k.h b/tests/bw_warp_sim_k.h
935index 676b2934..53fed2db 100644
936--- a/tests/bw_warp_sim_k.h
937+++ b/tests/bw_warp_sim_k.h
938@@ -155,7 +155,8 @@ static void ws_lane_exec(WsSim *s, WsLane *l, const BlackwellIRInsn *in, int pc,
939 case BW_IR_ISETP_GE_U32: l->p0 = a >= b; break;
940 case BW_IR_LDG_E: { uint64_t ad = ws_rd64(l, in->src1_vreg, p); if (!ws_mem_ok(s, ad, 4, pc)) return; uint32_t x; memcpy(&x, (const void *)(uintptr_t)ad, 4); ws_wrs(l, in->dst_vreg, in->dst_subreg, x, p); break; }
941 case BW_IR_LDG_E_U16: { uint64_t ad = ws_rd64(l, in->src1_vreg, p); if (!ws_mem_ok(s, ad, 2, pc)) return; uint16_t x; memcpy(&x, (const void *)(uintptr_t)ad, 2); ws_wrs(l, in->dst_vreg, in->dst_subreg, x, p); break; }
942- case BW_IR_STG_E: { uint64_t ad = ws_rd64(l, in->src1_vreg, p); if (!ws_mem_ok(s, ad, 4, pc)) return; memcpy((void *)(uintptr_t)ad, &b, 4); break; }
943+ /* STG.E takes the signed 24-bit byte offset the encoder writes (nvdisasm 13.0 -b SM121 decodes "STG.E desc[UR4][R4.64+0x1e0c], R9", omega#542) */
944+ case BW_IR_STG_E: { uint64_t ad = ws_rd64(l, in->src1_vreg, p) + (uint64_t)(int64_t)(int32_t)in->imm; if (!ws_mem_ok(s, ad, 4, pc)) return; memcpy((void *)(uintptr_t)ad, &b, 4); break; }
945 case BW_IR_LDG_E_64: case BW_IR_LDG_E_128: { unsigned n = in->op == BW_IR_LDG_E_64 ? 8u : 16u; uint64_t ad = ws_rd64(l, in->src1_vreg, p) + (uint64_t)(int64_t)(int32_t)in->imm; if (!ws_mem_ok(s, ad, n, pc)) { if (getenv("WS_DBG")) fprintf(stderr, " tid %u cx %u cy %u\n", tid, cx, cy); return; } if (ad & (n - 1)) { ws_fail(s, WS_E_MEM, pc, "wide global load not naturally aligned"); return; } uint32_t x[4]; memcpy(x, (const void *)(uintptr_t)ad, n); for (unsigned i = 0; i < n / 4; i++) ws_wrs(l, in->dst_vreg, i, x[i], p); break; }
946 case BW_IR_LDS128: { uint32_t ad = a + in->imm; if (ad & 15u) { ws_fail(s, WS_E_SMEM, pc, "LDS.128 address not 16-byte aligned"); return; } for (unsigned i = 0; i < 4; i++) { uint32_t *m = ws_smem(s, ad + 4u * i, pc); if (!m) return; ws_wrs(l, in->dst_vreg, i, *m, p); } break; }
947 case BW_IR_LDS32: { uint32_t *m = ws_smem(s, a, pc); if (!m) return; ws_wrs(l, in->dst_vreg, in->dst_subreg, *m, p); break; }
948@@ -170,6 +171,7 @@ static void ws_lane_exec(WsSim *s, WsLane *l, const BlackwellIRInsn *in, int pc,
949 case BW_IR_MUFU_EX2: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(exp2f(ws_u2f(a))), p); break;
950 case BW_IR_MUFU_RCP: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(1.0f / ws_u2f(a)), p); break;
951 case BW_IR_MUFU_RSQ: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(1.0f / sqrtf(ws_u2f(a))), p); break;
952+ case BW_IR_MUFU_LG2: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(log2f(ws_u2f(a))), p); break;
953 case BW_IR_LDG_STRONG_SYS: { uint64_t ad = ws_rd64(l, in->src1_vreg, p); if (!ws_mem_ok(s, ad, 4, pc)) return; uint32_t x; memcpy(&x, (const void *)(uintptr_t)ad, 4); ws_wrs(l, in->dst_vreg, in->dst_subreg, x, p); break; }
954 case BW_IR_STG_STRONG_SYS: { uint64_t ad = ws_rd64(l, in->src1_vreg, p); if (!ws_mem_ok(s, ad, 4, pc)) return; memcpy((void *)(uintptr_t)ad, &b, 4); break; }
955 case BW_IR_ATOMG_ADD_STRONG_SYS: { uint64_t ad = ws_rd64(l, in->src1_vreg, p); if (!ws_mem_ok(s, ad, 4, pc)) return; uint32_t old; memcpy(&old, (const void *)(uintptr_t)ad, 4); uint32_t nw = old + b; memcpy((void *)(uintptr_t)ad, &nw, 4); ws_wrs(l, in->dst_vreg, in->dst_subreg, old, p); break; }
956diff --git a/tests/gpu_gdn_test.c b/tests/gpu_gdn_test.c
957new file mode 100644
958index 00000000..be60e0bd
959--- /dev/null
960+++ b/tests/gpu_gdn_test.c
961@@ -0,0 +1,472 @@
962+/*
963+ * gpu_gdn_test: the one-launch Gated DeltaNet decode step of Qwen3.8-27B (omega_gpu_gdn_api.h), omega#542.
964+ * --host-only layout helpers, codegen budgets and refusals, the double oracle's delta-rule property
965+ * --sim warp-simulator parity over the whole grid (48 CTAs x 256 threads, both fragment orders bit-identical),
966+ * three chained steps against the double oracle, mutants 1-4 caught
967+ * (default) the chip: two layers (addressing), five chained steps each against the oracle (output, tail, state),
968+ * a rerun from the same state bit-identical, reset, mutants 1-4 caught, the record path (two steps in
969+ * one command list: the second reads the first's state), chip medians (--reps N, --out receipt.json)
970+ * The oracle is sc crates/aien-inference-abi/src/qwen38.rs `delta_net` in double, from the projections on.
971+ * Verdict line last: "gpu_gdn_test: N checks, F failed: PASS|FAIL".
972+ */
973+#include "omega_gpu_gdn_api.h"
974+#include "omega_gpu_session.h"
975+#include "omega_gpu_cmdlist.h"
976+#include "bw_warp_sim_k.h"
977+#include <inttypes.h>
978+#include <math.h>
979+#include <stdio.h>
980+#include <stdlib.h>
981+#include <string.h>
982+#include <time.h>
983+
984+static int g_checks, g_failed;
985+#define CHECK(cond, ...) do { g_checks++; if (!(cond)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0)
986+static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; }
987+static float frand(uint32_t *s, float lo, float hi) { return lo + (hi - lo) * ((float)(lcg(s) >> 8) / 16777216.0f); }
988+static double now_ms(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec * 1e3 + t.tv_nsec / 1e6; }
989+static int cmp_u64(const void *a, const void *b) { uint64_t x = *(const uint64_t *)a, y = *(const uint64_t *)b; return x < y ? -1 : x > y; }
990+
991+#define CD OMEGA_GDN_CONV_DIM
992+#define VH OMEGA_GDN_VALUE_HEADS
993+#define KH OMEGA_GDN_KEY_HEADS
994+#define HD OMEGA_GDN_HEAD_DIM
995+#define VD OMEGA_GDN_V_DIM
996+#define NS OMEGA_GPU_GDN_STATE_FLOATS
997+#define NT OMEGA_GPU_GDN_TAIL_FLOATS
998+static const float EPS_L2 = 1e-6f, EPS_N = 1e-6f;
999+/* tolerances, normalised by the largest reference magnitude of the vector (EX2 / RCP / RSQ / LG2 are approximate on
1000+ * the chip, the oracle is double; the mutants miss by orders of magnitude more, see the receipts) */
1001+#define TOL_OUT 2e-4
1002+#define TOL_STATE 2e-5
1003+#define TOL_TAIL 1e-6
1004+
1005+typedef struct {
1006+ float *conv_w, a_log[VH], dtb[VH], nw[HD];
1007+ float *tail0, *state0; /* initial state, reference layouts */
1008+} Layer;
1009+typedef struct { float *qkv, *z, ab[2 * VH]; } Inp;
1010+
1011+static void layer_make(Layer *L, uint32_t seed) {
1012+ uint32_t s = seed;
1013+ L->conv_w = malloc((size_t)CD * 4 * 4); L->tail0 = malloc((size_t)NT * 4); L->state0 = malloc((size_t)NS * 4);
1014+ for (size_t i = 0; i < (size_t)CD * 4; i++) L->conv_w[i] = frand(&s, -0.6f, 0.6f);
1015+ for (unsigned h = 0; h < VH; h++) { L->a_log[h] = frand(&s, -1.5f, 1.5f); L->dtb[h] = frand(&s, -3.0f, 1.0f); }
1016+ for (unsigned i = 0; i < HD; i++) L->nw[i] = frand(&s, 0.4f, 1.6f);
1017+ for (size_t i = 0; i < NT; i++) L->tail0[i] = frand(&s, -2.0f, 2.0f);
1018+ for (size_t i = 0; i < NS; i++) L->state0[i] = frand(&s, -0.2f, 0.2f);
1019+}
1020+static void layer_free(Layer *L) { free(L->conv_w); free(L->tail0); free(L->state0); }
1021+/* in_scale: the kernel multiplies qkv and z by it, so the stored rows are the true rows / in_scale */
1022+static void inp_make(Inp *in, uint32_t seed, float in_scale) {
1023+ uint32_t s = seed;
1024+ in->qkv = malloc((size_t)CD * 4); in->z = malloc((size_t)VD * 4);
1025+ for (unsigned i = 0; i < CD; i++) in->qkv[i] = frand(&s, -2.5f, 2.5f) / in_scale;
1026+ for (unsigned i = 0; i < VD; i++) in->z[i] = frand(&s, -3.0f, 3.0f) / in_scale;
1027+ for (unsigned h = 0; h < VH; h++) { in->ab[2 * h] = frand(&s, -4.0f, 4.0f); in->ab[2 * h + 1] = frand(&s, -4.0f, 4.0f); }
1028+ /* a few heads at the softplus threshold and past it, and beta near 0 / 1 */
1029+ if (!getenv("GDN_NOFORCE")) { in->ab[0] = 25.0f; in->ab[2] = 19.9f; in->ab[4] = -30.0f; in->ab[3] = 12.0f; in->ab[5] = -12.0f; }
1030+}
1031+static void inp_free(Inp *in) { free(in->qkv); free(in->z); }
1032+
1033+/* ---- the double oracle ---- */
1034+typedef struct { double *tail, *state; } DState;
1035+static void dstate_init(DState *d, const Layer *L) {
1036+ d->tail = malloc((size_t)NT * 8); d->state = malloc((size_t)NS * 8);
1037+ for (size_t i = 0; i < NT; i++) d->tail[i] = L->tail0[i];
1038+ for (size_t i = 0; i < NS; i++) d->state[i] = L->state0[i];
1039+}
1040+static void dstate_free(DState *d) { free(d->tail); free(d->state); }
1041+static double sigm(double x) { return 1.0 / (1.0 + exp(-x)); }
1042+static double softplus_ref(double x) { return x > 20.0 ? x : log1p(exp(x)); }
1043+static void oracle_step(const Layer *L, DState *d, const Inp *in, float in_scale, float out_scale, double *y) {
1044+ static double mixed[CD], q[KH * HD], k[KH * HD];
1045+ for (unsigned ch = 0; ch < CD; ch++) {
1046+ const double x = (double)(in->qkv[ch] * in_scale); /* the kernel's f32 product */
1047+ const float *w = &L->conv_w[(size_t)ch * 4];
1048+ double acc = w[0] * d->tail[ch] + w[1] * d->tail[CD + ch] + w[2] * d->tail[2 * CD + ch] + w[3] * x;
1049+ mixed[ch] = acc * sigm(acc);
1050+ d->tail[ch] = d->tail[CD + ch]; d->tail[CD + ch] = d->tail[2 * CD + ch]; d->tail[2 * CD + ch] = x;
1051+ }
1052+ for (unsigned hk = 0; hk < KH; hk++) {
1053+ double sq = 0, sk = 0;
1054+ for (unsigned i = 0; i < HD; i++) { sq += mixed[hk * HD + i] * mixed[hk * HD + i]; sk += mixed[KH * HD + hk * HD + i] * mixed[KH * HD + hk * HD + i]; }
1055+ const double iq = 1.0 / sqrt(sq + EPS_L2), ik = 1.0 / sqrt(sk + EPS_L2);
1056+ for (unsigned i = 0; i < HD; i++) { q[hk * HD + i] = mixed[hk * HD + i] * iq / sqrt((double)HD); k[hk * HD + i] = mixed[KH * HD + hk * HD + i] * ik; }
1057+ }
1058+ for (unsigned h = 0; h < VH; h++) {
1059+ const unsigned hk = h / (VH / KH);
1060+ const double negA = -(double)expf(L->a_log[h]); /* the API's float negA */
1061+ const double g = (double)(float)negA * softplus_ref((double)in->ab[2 * h] + L->dtb[h]);
1062+ const double decay = exp(g), beta = sigm(in->ab[2 * h + 1]);
1063+ double *S = d->state + (size_t)h * HD * HD, kv[HD], o[HD], dl[HD];
1064+ const double *kk = &k[hk * HD], *qq = &q[hk * HD], *v = &mixed[2 * KH * HD + h * HD];
1065+ for (size_t i = 0; i < (size_t)HD * HD; i++) S[i] *= decay;
1066+ for (unsigned c = 0; c < HD; c++) kv[c] = 0;
1067+ for (unsigned r = 0; r < HD; r++) for (unsigned c = 0; c < HD; c++) kv[c] += S[r * HD + c] * kk[r];
1068+ for (unsigned c = 0; c < HD; c++) dl[c] = (v[c] - kv[c]) * beta;
1069+ for (unsigned r = 0; r < HD; r++) for (unsigned c = 0; c < HD; c++) S[r * HD + c] += kk[r] * dl[c];
1070+ for (unsigned c = 0; c < HD; c++) o[c] = 0;
1071+ for (unsigned r = 0; r < HD; r++) for (unsigned c = 0; c < HD; c++) o[c] += S[r * HD + c] * qq[r];
1072+ double ms = 0; for (unsigned c = 0; c < HD; c++) ms += o[c] * o[c];
1073+ const double inv = 1.0 / sqrt(ms / HD + EPS_N);
1074+ for (unsigned c = 0; c < HD; c++) {
1075+ const double zz = (double)(in->z[h * HD + c] * in_scale);
1076+ y[h * HD + c] = o[c] * inv * L->nw[c] * (zz * sigm(zz)) * out_scale;
1077+ }
1078+ }
1079+}
1080+/* max |got - ref| / max |ref| */
1081+static double nerr(const float *got, const double *ref, size_t n) {
1082+ double e = 0, m = 1e-30;
1083+ for (size_t i = 0; i < n; i++) {
1084+ const double a = fabs((double)got[i] - ref[i]);
1085+ if (!isfinite(a)) { if (getenv("GDN_DBG")) printf(" non-finite at %zu: got %g ref %g\n", i, (double)got[i], ref[i]); return INFINITY; } /* a NaN or inf on either side never passes */
1086+ if (a > e) e = a;
1087+ if (fabs(ref[i]) > m) m = fabs(ref[i]);
1088+ }
1089+ return e / m;
1090+}
1091+typedef struct { double out, tail, state; } Errs;
1092+static int errs_ok(const Errs *e) { return e->out <= TOL_OUT && e->tail <= TOL_TAIL && e->state <= TOL_STATE; }
1093+
1094+/* ---- host-only ---- */
1095+static void host_only(void) {
1096+ /* layout round trip, and the copy check */
1097+ float *t = malloc((size_t)NT * 4), *t2 = malloc((size_t)NT * 4), *dev = malloc(OMEGA_GDN_TAIL_BYTES);
1098+ uint32_t s = 5;
1099+ for (size_t i = 0; i < NT; i++) t[i] = frand(&s, -1, 1);
1100+ omega_gdn_pack_tail(t, dev);
1101+ CHECK(omega_gdn_unpack_tail(dev, t2) == 0 && memcmp(t, t2, (size_t)NT * 4) == 0, "tail pack/unpack round trip");
1102+ dev[(1u * OMEGA_GDN_SLOTS + 7u) * 4u + 1u] += 1.0f; /* head 1 (a second copy of key head 0), q slot 7 */
1103+ CHECK(omega_gdn_unpack_tail(dev, t2) == -1, "unpack refuses disagreeing copies");
1104+ free(t); free(t2); free(dev);
1105+ /* parameter block */
1106+ Layer L; layer_make(&L, 9);
1107+ uint8_t *blk = malloc(OMEGA_GDN_PAR_BYTES);
1108+ CHECK(omega_gdn_pack_params(L.conv_w, L.a_log, L.dtb, L.nw, blk) == 0, "pack params");
1109+ const float *sc = (const float *)(blk + OMEGA_GDN_PAR_SCAL);
1110+ CHECK(sc[2 * 5] == -expf(L.a_log[5]) && sc[2 * 5 + 1] == L.dtb[5], "negA and dt_bias interleaved");
1111+ CHECK(memcmp(blk + OMEGA_GDN_PAR_NORM, L.nw, HD * 4) == 0, "norm weight at its offset");
1112+ CHECK(omega_gdn_pack_params(NULL, L.a_log, L.dtb, L.nw, blk) == -1, "pack params refuses NULL");
1113+ free(blk);
1114+ /* codegen budgets */
1115+ for (int m = 0; m <= 4; m++) {
1116+ OmegaGdnSpec sp = { m }; uint32_t ni = 0, ng = 0;
1117+ int rc = omega_gdn_prog_stats(&sp, &ni, &ng);
1118+ CHECK(rc == 0 && ni <= BW_MAX_IR_INSNS && ng + 2 <= BW_PHYS_GPR_MAX, "mutant %d codegen: rc %d, %u insns, %u registers", m, rc, ni, ng);
1119+ if (m == 0) printf("kernel: %u instructions, peak %u registers (%u declared, %u threads)\n", ni, ng, omega_bw_gpr_declared(ng), OMEGA_GDN_THREADS);
1120+ }
1121+ OmegaGdnSpec bad = { 5 };
1122+ CHECK(omega_gdn_prog_stats(&bad, NULL, NULL) == -1, "mutant 5 refused");
1123+ /* the oracle's delta rule: from S = 0 with beta ~ 1, the step writes v along k, so S^T k = v (|k| = 1) */
1124+ {
1125+ DState d; dstate_init(&d, &L);
1126+ memset(d.state, 0, (size_t)NS * 8);
1127+ Inp in; inp_make(&in, 77, 1.0f);
1128+ for (unsigned h = 0; h < VH; h++) in.ab[2 * h + 1] = 40.0f;
1129+ double *y = malloc((size_t)VD * 8);
1130+ static double mixed_v[CD];
1131+ /* the expected v and k: recompute the conv of this step from the oracle's tail before the step */
1132+ for (unsigned ch = 0; ch < CD; ch++) {
1133+ const float *w = &L.conv_w[(size_t)ch * 4];
1134+ double acc = w[0] * d.tail[ch] + w[1] * d.tail[CD + ch] + w[2] * d.tail[2 * CD + ch] + w[3] * (double)in.qkv[ch];
1135+ mixed_v[ch] = acc * sigm(acc);
1136+ }
1137+ oracle_step(&L, &d, &in, 1.0f, 1.0f, y);
1138+ double worst = 0;
1139+ for (unsigned h = 0; h < VH; h += 7) {
1140+ const unsigned hk = h / 3;
1141+ double nk = 0; for (unsigned i = 0; i < HD; i++) nk += mixed_v[KH * HD + hk * HD + i] * mixed_v[KH * HD + hk * HD + i];
1142+ nk = sqrt(nk + EPS_L2);
1143+ for (unsigned c = 0; c < HD; c++) {
1144+ double kv = 0;
1145+ for (unsigned r = 0; r < HD; r++) kv += d.state[(size_t)h * HD * HD + r * HD + c] * mixed_v[KH * HD + hk * HD + r] / nk;
1146+ const double want = mixed_v[2 * KH * HD + h * HD + c];
1147+ if (fabs(kv - want) > worst) worst = fabs(kv - want);
1148+ }
1149+ }
1150+ CHECK(worst < 1e-6, "oracle delta rule: S^T k = v after one step from zero (worst %.3g)", worst);
1151+ free(y); inp_free(&in); dstate_free(&d);
1152+ }
1153+ layer_free(&L);
1154+}
1155+
1156+/* ---- simulator ---- */
1157+enum { SB_QKV, SB_Z, SB_AB, SB_PAR, SB_TAIL, SB_STATE, SB_OUT, SB_N };
1158+static uint64_t g_sim_issues;
1159+static int sim_run_steps(int mutant, uint32_t steps, float in_scale, float out_scale, Errs *worst, char *msg, size_t n) {
1160+ static BlackwellIRProgram prog;
1161+ OmegaGdnSpec spec = { mutant };
1162+ if (omega_blackwell_codegen_gdn_prog(&spec, &prog) != 0) { snprintf(msg, n, "codegen refused"); return -1; }
1163+ Layer L; layer_make(&L, 21);
1164+ DState d; dstate_init(&d, &L);
1165+ size_t sz[SB_N] = { CD * 4u, VD * 4u, VH * 8u, OMEGA_GDN_PAR_BYTES, OMEGA_GDN_TAIL_BYTES, OMEGA_GDN_STATE_BYTES, VD * 4u };
1166+ uint8_t *buf[SB_N];
1167+ for (int i = 0; i < SB_N; i++) buf[i] = aligned_alloc(256, (sz[i] + 255) & ~(size_t)255);
1168+ omega_gdn_pack_params(L.conv_w, L.a_log, L.dtb, L.nw, buf[SB_PAR]);
1169+ omega_gdn_pack_tail(L.tail0, (float *)buf[SB_TAIL]);
1170+ memcpy(buf[SB_STATE], L.state0, OMEGA_GDN_STATE_BYTES);
1171+ uint8_t *tail_save = malloc(OMEGA_GDN_TAIL_BYTES), *state_save = malloc(OMEGA_GDN_STATE_BYTES), *out_copy = malloc(VD * 4u);
1172+ uint8_t *tail_copy = malloc(OMEGA_GDN_TAIL_BYTES), *state_copy = malloc(OMEGA_GDN_STATE_BYTES);
1173+ double *y = malloc((size_t)VD * 8);
1174+ float *tref = malloc((size_t)NT * 4);
1175+ int err = 0;
1176+ memset(worst, 0, sizeof *worst);
1177+ for (uint32_t st = 0; st < steps && !err; st++) {
1178+ Inp in; inp_make(&in, 1000 + st, in_scale);
1179+ memcpy(buf[SB_QKV], in.qkv, sz[SB_QKV]); memcpy(buf[SB_Z], in.z, sz[SB_Z]); memcpy(buf[SB_AB], in.ab, sz[SB_AB]);
1180+ memcpy(tail_save, buf[SB_TAIL], OMEGA_GDN_TAIL_BYTES); memcpy(state_save, buf[SB_STATE], OMEGA_GDN_STATE_BYTES);
1181+ for (int order = 0; order < 2 && !err; order++) {
1182+ if (order == 1) { memcpy(buf[SB_TAIL], tail_save, OMEGA_GDN_TAIL_BYTES); memcpy(buf[SB_STATE], state_save, OMEGA_GDN_STATE_BYTES); }
1183+ float *yo = (float *)buf[SB_OUT];
1184+ for (unsigned i = 0; i < VD; i++) yo[i] = -999.0f;
1185+ WsSim s; memset(&s, 0, sizeof s);
1186+ s.p = &prog; s.threads = OMEGA_GDN_THREADS; s.order = order;
1187+ static uint8_t smem[OMEGA_GDN_SHARED_BYTES]; s.smem = smem; s.smem_bytes = OMEGA_GDN_SHARED_BYTES;
1188+ for (int i = 0; i < SB_N; i++) {
1189+ const uint64_t a = (uintptr_t)buf[i];
1190+ s.cbank[(0x380 + 8 * i) / 4] = (uint32_t)a; s.cbank[(0x380 + 8 * i) / 4 + 1] = (uint32_t)(a >> 32);
1191+ s.ranges[i].lo = (uintptr_t)buf[i]; s.ranges[i].hi = (uintptr_t)buf[i] + sz[i];
1192+ }
1193+ s.nranges = SB_N;
1194+ memcpy(&s.cbank[OMEGA_GDN_ARG_EPS_L2 / 4], &EPS_L2, 4); memcpy(&s.cbank[OMEGA_GDN_ARG_EPS_NORM / 4], &EPS_N, 4);
1195+ memcpy(&s.cbank[OMEGA_GDN_ARG_IN_SCALE / 4], &in_scale, 4); memcpy(&s.cbank[OMEGA_GDN_ARG_OUT_SCALE / 4], &out_scale, 4);
1196+ err = ws_run(&s, OMEGA_GDN_GRID_X, OMEGA_GDN_GRID_Y);
1197+ if (err) { snprintf(msg, n, "step %u order %d: %s (pc %d)", st, order, s.msg, s.err_pc); break; }
1198+ g_sim_issues += s.warp_issues;
1199+ size_t unwritten = 0; for (unsigned i = 0; i < VD; i++) if (yo[i] == -999.0f) unwritten++;
1200+ if (unwritten) { err = -1; snprintf(msg, n, "%zu output words never written", unwritten); break; }
1201+ if (order == 0) { memcpy(out_copy, yo, VD * 4u); memcpy(tail_copy, buf[SB_TAIL], OMEGA_GDN_TAIL_BYTES); memcpy(state_copy, buf[SB_STATE], OMEGA_GDN_STATE_BYTES); }
1202+ else if (memcmp(out_copy, yo, VD * 4u) || memcmp(tail_copy, buf[SB_TAIL], OMEGA_GDN_TAIL_BYTES) || memcmp(state_copy, buf[SB_STATE], OMEGA_GDN_STATE_BYTES)) {
1203+ err = -1; snprintf(msg, n, "step %u: the fragment order changed the result", st);
1204+ }
1205+ }
1206+ if (err) { inp_free(&in); break; }
1207+ oracle_step(&L, &d, &in, in_scale, out_scale, y);
1208+ double *ts = malloc((size_t)NT * 8);
1209+ for (size_t i = 0; i < NT; i++) ts[i] = d.tail[i];
1210+ Errs e;
1211+ e.out = nerr((const float *)buf[SB_OUT], y, VD);
1212+ if (omega_gdn_unpack_tail((const float *)buf[SB_TAIL], tref) != 0) e.tail = INFINITY;
1213+ else e.tail = nerr(tref, ts, NT);
1214+ e.state = nerr((const float *)buf[SB_STATE], d.state, NS);
1215+ free(ts);
1216+ if (e.out > worst->out) worst->out = e.out;
1217+ if (e.tail > worst->tail) worst->tail = e.tail;
1218+ if (e.state > worst->state) worst->state = e.state;
1219+ inp_free(&in);
1220+ }
1221+ for (int i = 0; i < SB_N; i++) free(buf[i]);
1222+ free(tail_save); free(state_save); free(out_copy); free(tail_copy); free(state_copy); free(y); free(tref);
1223+ dstate_free(&d); layer_free(&L);
1224+ return err;
1225+}
1226+static void sim_battery(void) {
1227+ struct { int mutant; uint32_t steps; float in_scale, out_scale; const char *name; } pts[] = {
1228+ { 0, 3, 1.0f, 1.0f, "3 steps" },
1229+ { 0, 2, 0.0009765625f, 18446744073709551616.0f, "2 steps, in_scale 2^-10, out_scale 2^64" },
1230+ { 1, 2, 1.0f, 1.0f, "mutant 1 (no decay)" }, { 2, 2, 1.0f, 1.0f, "mutant 2 (beta 1)" },
1231+ { 3, 2, 1.0f, 1.0f, "mutant 3 (gate z, not silu z)" }, { 4, 2, 1.0f, 1.0f, "mutant 4 (tail not shifted)" },
1232+ };
1233+ for (size_t i = 0; i < sizeof pts / sizeof pts[0]; i++) {
1234+ char msg[300] = ""; Errs w = { 0, 0, 0 };
1235+ double t0 = now_ms();
1236+ int err = sim_run_steps(pts[i].mutant, pts[i].steps, pts[i].in_scale, pts[i].out_scale, &w, msg, sizeof msg);
1237+ printf("sim %-44s err out %.3g tail %.3g state %.3g (%.0f ms)%s%s\n", pts[i].name, w.out, w.tail, w.state, now_ms() - t0, err ? " ERROR " : "", msg);
1238+ CHECK(err == 0, "sim %s ran (%s)", pts[i].name, msg);
1239+ if (pts[i].mutant == 0) CHECK(errs_ok(&w), "sim %s within tolerance", pts[i].name);
1240+ else CHECK(!errs_ok(&w), "sim %s caught", pts[i].name);
1241+ }
1242+ printf("sim warp issues: %" PRIu64 "\n", g_sim_issues);
1243+}
1244+
1245+/* ---- chip ---- */
1246+static int chip_steps(OmegaGpuGdn *g, uint32_t layer, const Layer *L, DState *d, uint32_t steps, uint32_t seed, float in_scale,
1247+ float out_scale, Errs *worst, float *last_out) {
1248+ float *out = malloc((size_t)VD * 4), *tail = malloc((size_t)NT * 4), *state = malloc((size_t)NS * 4);
1249+ double *y = malloc((size_t)VD * 8), *td = malloc((size_t)NT * 8);
1250+ int rc = 0;
1251+ memset(worst, 0, sizeof *worst);
1252+ for (uint32_t st = 0; st < steps && rc == 0; st++) {
1253+ Inp in; inp_make(&in, seed + st, in_scale);
1254+ rc = omega_gpu_gdn_step(g, layer, in.qkv, in.z, in.ab, EPS_L2, EPS_N, in_scale, out_scale, out, NULL);
1255+ if (rc == 0) rc = omega_gpu_gdn_get_state(g, layer, tail, state);
1256+ if (rc == 0) {
1257+ oracle_step(L, d, &in, in_scale, out_scale, y);
1258+ for (size_t i = 0; i < NT; i++) td[i] = d->tail[i];
1259+ Errs e = { nerr(out, y, VD), nerr(tail, td, NT), nerr(state, d->state, NS) };
1260+ if (e.out > worst->out) worst->out = e.out;
1261+ if (e.tail > worst->tail) worst->tail = e.tail;
1262+ if (e.state > worst->state) worst->state = e.state;
1263+ }
1264+ inp_free(&in);
1265+ }
1266+ if (last_out) memcpy(last_out, out, (size_t)VD * 4);
1267+ free(out); free(tail); free(state); free(y); free(td);
1268+ return rc;
1269+}
1270+static int chip_run(const char *out_path, uint32_t reps) {
1271+ FILE *out = out_path ? fopen(out_path, "w") : NULL;
1272+ if (out_path && !out) { printf("cannot write %s\n", out_path); return 1; }
1273+ if (out) fprintf(out, "{\"receipt\":\"OMEGA_GPU_GDN_V1\",\"reps\":%u,\"tol\":{\"out\":%g,\"tail\":%g,\"state\":%g},\"points\":[", reps, TOL_OUT, TOL_TAIL, TOL_STATE);
1274+ OmegaGpuGdn *g = NULL;
1275+ double t0 = now_ms();
1276+ int rc = omega_gpu_gdn_alloc(2, &g);
1277+ printf("gdn alloc (2 layers): rc=%s %.1f ms (%s)\n", omega_gpu_gdn_rc_name(rc), now_ms() - t0, rc ? omega_gpu_gdn_last_error() : "");
1278+ CHECK(rc == OMEGA_GPU_GDN_OK, "gdn alloc");
1279+ if (rc) { if (out) fprintf(out, "],\"verdict\":\"FAIL\"}\n"); return 1; }
1280+ Layer L[2]; layer_make(&L[0], 31); layer_make(&L[1], 32);
1281+ for (uint32_t l = 0; l < 2; l++) {
1282+ CHECK(omega_gpu_gdn_set_layer(g, l, L[l].conv_w, L[l].a_log, L[l].dtb, L[l].nw) == 0, "set layer %u", l);
1283+ CHECK(omega_gpu_gdn_set_state(g, l, L[l].tail0, L[l].state0) == 0, "set state %u", l);
1284+ }
1285+ int first = 1;
1286+#define POINT(name, e, extra) do { if (out) { fprintf(out, "%s{\"name\":\"%s\",\"err_out\":%.6g,\"err_tail\":%.6g,\"err_state\":%.6g%s}", first ? "" : ",", name, (e).out, (e).tail, (e).state, extra); first = 0; } } while (0)
1287+ /* parity: layer 1 first (addressing), then layer 0 with in/out scales */
1288+ struct { uint32_t layer, steps, seed; float in_scale, out_scale; const char *name; } pp[] = {
1289+ { 1, 5, 500, 1.0f, 1.0f, "layer 1, 5 steps" },
1290+ { 0, 5, 600, 0.0009765625f, 18446744073709551616.0f, "layer 0, 5 steps, in 2^-10 out 2^64" },
1291+ };
1292+ float *o1 = malloc((size_t)VD * 4), *o2 = malloc((size_t)VD * 4);
1293+ for (size_t i = 0; i < 2; i++) {
1294+ DState d; dstate_init(&d, &L[pp[i].layer]);
1295+ Errs e;
1296+ rc = chip_steps(g, pp[i].layer, &L[pp[i].layer], &d, pp[i].steps, pp[i].seed, pp[i].in_scale, pp[i].out_scale, &e, o1);
1297+ printf("chip %-40s rc=%s err out %.3g tail %.3g state %.3g %s\n", pp[i].name, omega_gpu_gdn_rc_name(rc), e.out, e.tail, e.state, rc ? omega_gpu_gdn_last_error() : "");
1298+ CHECK(rc == 0 && errs_ok(&e), "chip %s", pp[i].name);
1299+ POINT(pp[i].name, e, "");
1300+ dstate_free(&d);
1301+ }
1302+ /* rerun from the same state: bit-identical */
1303+ {
1304+ Inp in; inp_make(&in, 700, 1.0f);
1305+ CHECK(omega_gpu_gdn_set_state(g, 1, L[1].tail0, L[1].state0) == 0, "set state for rerun");
1306+ rc = omega_gpu_gdn_step(g, 1, in.qkv, in.z, in.ab, EPS_L2, EPS_N, 1.0f, 1.0f, o1, NULL);
1307+ CHECK(omega_gpu_gdn_set_state(g, 1, L[1].tail0, L[1].state0) == 0, "set state for rerun 2");
1308+ int rc2 = omega_gpu_gdn_step(g, 1, in.qkv, in.z, in.ab, EPS_L2, EPS_N, 1.0f, 1.0f, o2, NULL);
1309+ CHECK(rc == 0 && rc2 == 0 && memcmp(o1, o2, (size_t)VD * 4) == 0, "rerun from the same state is bit-identical");
1310+ inp_free(&in);
1311+ }
1312+ /* reset: state and tail zero */
1313+ {
1314+ float *t = malloc((size_t)NT * 4), *s = malloc((size_t)NS * 4);
1315+ rc = omega_gpu_gdn_reset(g);
1316+ int z = rc == 0 && omega_gpu_gdn_get_state(g, 0, t, s) == 0;
1317+ for (size_t i = 0; z && i < NT; i++) if (t[i] != 0.0f) z = 0;
1318+ for (size_t i = 0; z && i < NS; i++) if (s[i] != 0.0f) z = 0;
1319+ CHECK(z, "reset zeroes the tail and the state");
1320+ free(t); free(s);
1321+ }
1322+ /* mutants: each must miss */
1323+ for (int m = 1; m <= 4; m++) {
1324+ omega_gpu_gdn_test_set_mutant(m);
1325+ CHECK(omega_gpu_gdn_set_state(g, 1, L[1].tail0, L[1].state0) == 0, "set state mutant %d", m);
1326+ DState d; dstate_init(&d, &L[1]);
1327+ Errs e;
1328+ rc = chip_steps(g, 1, &L[1], &d, 2, 800, 1.0f, 1.0f, &e, NULL);
1329+ char nm[32]; snprintf(nm, sizeof nm, "mutant %d", m);
1330+ printf("chip %-40s rc=%s err out %.3g tail %.3g state %.3g\n", nm, omega_gpu_gdn_rc_name(rc), e.out, e.tail, e.state);
1331+ CHECK(rc == 0 && !errs_ok(&e), "chip mutant %d caught", m);
1332+ POINT(nm, e, "");
1333+ dstate_free(&d);
1334+ }
1335+ omega_gpu_gdn_test_set_mutant(0);
1336+ /* the record path: two chained steps in one command list on layer 0 */
1337+ {
1338+ CHECK(omega_gpu_gdn_set_state(g, 0, L[0].tail0, L[0].state0) == 0, "set state for record");
1339+ Inp in[2]; inp_make(&in[0], 900, 1.0f); inp_make(&in[1], 901, 1.0f);
1340+ DState d; dstate_init(&d, &L[0]);
1341+ double *y = malloc((size_t)VD * 8);
1342+ oracle_step(&L[0], &d, &in[0], 1.0f, 1.0f, y); oracle_step(&L[0], &d, &in[1], 1.0f, 1.0f, y);
1343+ /* warm the kernel cache outside the record (a miss inside is refused) */
1344+ float *tmp = malloc((size_t)VD * 4);
1345+ CHECK(omega_gpu_gdn_step(g, 1, in[0].qkv, in[0].z, in[0].ab, EPS_L2, EPS_N, 1.0f, 1.0f, tmp, NULL) == 0, "warm-up step");
1346+ OmegaGpuCmdList *cl = NULL; /* create and free take the session lock themselves (omega_gpu_cmdlist.h) */
1347+ const int cl_ok = omega_gpu_cmdlist_create(4, &cl) == 0;
1348+ omega_gpu_session_lock();
1349+ OmegaGpuMem inb[2]; memset(inb, 0, sizeof inb);
1350+ int ok = cl_ok && omega_gpu_session_open() != NULL;
1351+ const size_t ib = (size_t)CD * 4 + (size_t)VD * 4 + VH * 8;
1352+ for (int i = 0; ok && i < 2; i++) {
1353+ ok = omega_gpu_session_alloc(ib, &inb[i]) == 0;
1354+ if (ok) {
1355+ memcpy(inb[i].cpu, in[i].qkv, (size_t)CD * 4); memcpy((uint8_t *)inb[i].cpu + CD * 4, in[i].z, (size_t)VD * 4);
1356+ memcpy((uint8_t *)inb[i].cpu + CD * 4 + VD * 4, in[i].ab, VH * 8);
1357+ ok = omega_gpu_session_sync_to_gpu(&inb[i]) == 0;
1358+ }
1359+ }
1360+ uint64_t out_va = 0;
1361+ for (int i = 0; ok && i < 2; i++)
1362+ ok = omega_gpu_gdn_record(cl, g, 0, inb[i].va, inb[i].va + CD * 4, inb[i].va + CD * 4 + VD * 4, EPS_L2, EPS_N, 1.0f, 1.0f, &out_va) == 0;
1363+ OmegaGpuCmdListOpts opts; memset(&opts, 0, sizeof opts);
1364+ OmegaGpuCmdListStats stc; memset(&stc, 0, sizeof stc);
1365+ if (ok) ok = omega_gpu_cmdlist_submit(cl, &opts, &stc) == 0;
1366+ for (int i = 0; i < 2; i++) if (inb[i].cpu) omega_gpu_session_free(&inb[i]);
1367+ omega_gpu_session_unlock();
1368+ if (cl) omega_gpu_cmdlist_free(cl);
1369+ CHECK(ok, "record two steps and submit (%s)", ok ? "" : omega_gpu_gdn_last_error());
1370+ if (ok) {
1371+ float *t = malloc((size_t)NT * 4), *s = malloc((size_t)NS * 4), *yo = malloc((size_t)VD * 4);
1372+ double *td = malloc((size_t)NT * 8);
1373+ ok = omega_gpu_gdn_get_state(g, 0, t, s) == 0 && omega_gpu_gdn_read_out(g, yo) == 0; /* the second step's output */
1374+ for (size_t i = 0; i < NT; i++) td[i] = d.tail[i];
1375+ Errs e = { nerr(yo, y, VD), nerr(t, td, NT), nerr(s, d.state, NS) };
1376+ printf("chip %-40s err out %.3g tail %.3g state %.3g\n", "record path, 2 chained steps", e.out, e.tail, e.state);
1377+ CHECK(ok && errs_ok(&e), "record path: two chained steps match the oracle");
1378+ POINT("record path, 2 chained steps", e, "");
1379+ free(t); free(s); free(yo); free(td);
1380+ }
1381+ free(tmp); free(y); dstate_free(&d); inp_free(&in[0]); inp_free(&in[1]);
1382+ }
1383+ /* timing */
1384+ {
1385+ Inp in; inp_make(&in, 950, 1.0f);
1386+ uint64_t *ec = malloc(reps * 8), *ek = malloc(reps * 8);
1387+ uint32_t done = 0;
1388+ OmegaGpuGdnInfo info;
1389+ for (uint32_t r = 0; r < reps; r++) {
1390+ if (omega_gpu_gdn_step(g, 0, in.qkv, in.z, in.ab, EPS_L2, EPS_N, 1.0f, 1.0f, o1, &info) != 0) break;
1391+ ek[done] = info.elapsed_ns; ec[done] = info.call_ns; done++;
1392+ }
1393+ CHECK(done == reps, "timing reps ran (%u of %u)", done, reps);
1394+ if (done) {
1395+ qsort(ek, done, 8, cmp_u64); qsort(ec, done, 8, cmp_u64);
1396+ const double kus = ek[done / 2] / 1e3, cus = ec[done / 2] / 1e3, gbs = (double)info.state_bytes / (ek[done / 2] * 1e-9) / 1e9;
1397+ printf("timing (medians of %u): chip %.1f us (state read + write %.2f MB -> %.1f GB/s; 273 GB/s nominal), call %.1f us; %u insns, %u registers\n",
1398+ done, kus, info.state_bytes / 1e6, gbs, cus, info.insns, info.gprs);
1399+ if (out) fprintf(out, "%s{\"name\":\"timing\",\"reps\":%u,\"chip_us_median\":%.3f,\"call_us_median\":%.3f,\"state_bytes\":%" PRIu64 ",\"state_gbs\":%.2f,\"nominal_gbs\":273,\"insns\":%u,\"gprs\":%u}",
1400+ first ? "" : ",", done, kus, cus, info.state_bytes, gbs, info.insns, info.gprs);
1401+ first = 0;
1402+ }
1403+ free(ec); free(ek); inp_free(&in);
1404+ }
1405+#undef POINT
1406+ free(o1); free(o2);
1407+ layer_free(&L[0]); layer_free(&L[1]);
1408+ omega_gpu_gdn_free(g);
1409+ if (out) { fprintf(out, "],\"checks\":%d,\"failed\":%d,\"verdict\":\"%s\"}\n", g_checks, g_failed, g_failed ? "FAIL" : "PASS"); fclose(out); }
1410+ return 0;
1411+}
1412+
1413+int main(int argc, char **argv) {
1414+ setvbuf(stdout, NULL, _IOLBF, 0); /* a stuck chip run still leaves every finished line in its log */
1415+ int host = 0, sim = 0; const char *out_path = NULL; uint32_t reps = 0, spin = 2000;
1416+ for (int i = 1; i < argc; i++) {
1417+ if (strcmp(argv[i], "--host-only") == 0) host = 1;
1418+ else if (strcmp(argv[i], "--sim") == 0) sim = 1;
1419+ else if (strcmp(argv[i], "--out") == 0 && i + 1 < argc) out_path = argv[++i];
1420+ else if (strcmp(argv[i], "--reps") == 0 && i + 1 < argc) reps = (uint32_t)atoi(argv[++i]);
1421+ else if (strcmp(argv[i], "--spin-us") == 0 && i + 1 < argc) spin = (uint32_t)atoi(argv[++i]);
1422+ else { printf("unknown argument %s\n", argv[i]); return 2; }
1423+ }
1424+ if (host) host_only();
1425+ if (sim) sim_battery();
1426+ if (!host && !sim) {
1427+ (void)omega_gpu_session_set_spin_us(spin);
1428+ chip_run(out_path, reps ? reps : 50);
1429+ omega_gpu_session_close();
1430+ }
1431+ printf("gpu_gdn_test: %d checks, %d failed: %s\n", g_checks, g_failed, g_failed ? "FAIL" : "PASS");
1432+ return g_failed ? 1 : 0;
1433+}