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 / q38-decode-speed-chip-20261019T0200Z / worktree.diff

472 lines · 30137 bytes Raw Blame History
1diff --git a/src/omega_blackwell_codegen_gdn.c b/src/omega_blackwell_codegen_gdn.c
2index 68185cf3..c9c6a26a 100644
3--- a/src/omega_blackwell_codegen_gdn.c
4+++ b/src/omega_blackwell_codegen_gdn.c
5@@ -175,7 +175,7 @@ static void cross_warp_sum(Em *e, int acc[4], int pa, int one, int la) {
6 }
7
8 int omega_blackwell_codegen_gdn_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog) {
9- if (!spec || !prog || spec->mutant < 0 || spec->mutant > 4) return -1;
10+ if (!spec || !prog || spec->copy || spec->mutant < 0 || spec->mutant > 4) return -1;
11 const int MUT = spec->mutant;
12 const uint32_t C_LOG2E = fbits(1.44269504f), C_NLOG2E = fbits(-1.44269504f), C_LN2 = fbits(0.693147181f);
13 const uint32_t C_ONE = fbits(1.0f), C_M1 = fbits(-1.0f), C_INV128 = fbits(1.0f / 128.0f);
14@@ -379,6 +379,37 @@ int omega_gdn_prog_stats(const OmegaGdnSpec *spec, uint32_t *insns, uint32_t *gp
15 return 0;
16 }
17
18+/* ======== state copy / zero (see the header) ======== */
19+int omega_blackwell_codegen_gdn_copy_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog) {
20+ if (!spec || !prog || spec->mutant || (spec->copy != OMEGA_GDN_COPY && spec->copy != OMEGA_GDN_ZERO)) return -1;
21+ omega_bw_ir_init(prog);
22+ Em em; memset(&em, 0, sizeof em); em.p = prog; Em *e = &em;
23+ int udesc = UV64(e), tid = V(e), cx = V(e), w = V(e), pd = V64(e), ad = V64(e), v[4];
24+ for (unsigned j = 0; j < 4; j++) v[j] = V(e);
25+ if (e->err) return -1;
26+ ldcu64(e, udesc, 0x358);
27+ ldc64(e, pd, OMEGA_GDN_COPY_ARG_DST);
28+ s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X);
29+ imadi(e, R(w), R(cx), OMEGA_GDN_COPY_WORDS_PER_CTA, tid);
30+ imadw(e, ad, w, 4u, pd);
31+ if (spec->copy == OMEGA_GDN_COPY) {
32+ /* the 32-bit LDG.E carries no immediate: one address per word */
33+ int ps = V64(e), one = V(e), wj[4], as[4];
34+ for (unsigned j = 0; j < 4; j++) { wj[j] = V(e); as[j] = V64(e); }
35+ if (e->err) return -1;
36+ ldc64(e, ps, OMEGA_GDN_COPY_ARG_SRC);
37+ movi(e, one, 1);
38+ for (unsigned j = 0; j < 4; j++) { imadi(e, R(wj[j]), R(one), j * OMEGA_GDN_COPY_THREADS, w); imadw(e, as[j], wj[j], 4u, ps); }
39+ for (unsigned j = 0; j < 4; j++) ldg_wide(e, BW_IR_LDG_E, v[j], as[j], 0, udesc, j == 0);
40+ } else {
41+ for (unsigned j = 0; j < 4; j++) movrz(e, R(v[j]));
42+ }
43+ for (unsigned j = 0; j < 4; j++) stg(e, ad, j * OMEGA_GDN_COPY_THREADS * 4u, R(v[j]), udesc);
44+ tail(e);
45+ if (e->err) return -1;
46+ return omega_bw_regalloc_solve(prog);
47+}
48+
49 /* ======== prefill (omega#543): prep, scan, post (layouts in the header) ======== */
50
51 /* softplus, decay and beta of one value head (the decode phase C maths): abq = {a, b}, scq = {negA, dt_bias} */
52@@ -402,7 +433,7 @@ static void gdn_scalars(Em *e, int MUT, int abq, int scq, int dec, int beta, int
53 }
54
55 int omega_blackwell_codegen_gdn_prep_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog) {
56- if (!spec || !prog || spec->mutant < 0 || spec->mutant > 4) return -1;
57+ if (!spec || !prog || spec->copy || spec->mutant < 0 || spec->mutant > 4) return -1;
58 const int MUT = spec->mutant;
59 const float qs = 0.0883883476f; /* 1 / sqrt(128) */
60 const uint32_t ROW = OMEGA_GDN_CONV_DIM * 4u, REC_ROW = OMEGA_GDN_VALUE_HEADS * OMEGA_GDN_REC_BYTES;
61@@ -562,7 +593,7 @@ int omega_blackwell_codegen_gdn_prep_prog(const OmegaGdnSpec *spec, BlackwellIRP
62 }
63
64 int omega_blackwell_codegen_gdn_scan_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog) {
65- if (!spec || !prog || spec->mutant < 0 || spec->mutant > 4) return -1;
66+ if (!spec || !prog || spec->copy || spec->mutant < 0 || spec->mutant > 4) return -1;
67 const int MUT = spec->mutant;
68 const uint32_t ROW = OMEGA_GDN_CONV_DIM * 4u, CHUNK_ROW = OMEGA_GDN_SCAN_CHUNK * OMEGA_GDN_VALUE_HEADS * OMEGA_GDN_REC_BYTES;
69 omega_bw_ir_init(prog);
70@@ -704,7 +735,7 @@ int omega_blackwell_codegen_gdn_scan_prog(const OmegaGdnSpec *spec, BlackwellIRP
71 }
72
73 int omega_blackwell_codegen_gdn_post_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog) {
74- if (!spec || !prog || spec->mutant < 0 || spec->mutant > 4) return -1;
75+ if (!spec || !prog || spec->copy || spec->mutant < 0 || spec->mutant > 4) return -1;
76 const int MUT = spec->mutant;
77 omega_bw_ir_init(prog);
78 Em em; memset(&em, 0, sizeof em); em.p = prog; Em *e = &em;
79diff --git a/src/omega_blackwell_codegen_gdn.h b/src/omega_blackwell_codegen_gdn.h
80index 8015cc83..ca77a084 100644
81--- a/src/omega_blackwell_codegen_gdn.h
82+++ b/src/omega_blackwell_codegen_gdn.h
83@@ -82,6 +82,7 @@
84 typedef struct {
85 int mutant; /* TEST ONLY. 0 none; 1 no decay (decay = 1); 2 beta = 1; 3 the output gate is z, not silu(z);
86 * 4 the conv tail is not shifted (the next step convolves stale history). */
87+ int copy; /* 0: the GDN kernels. OMEGA_GDN_COPY / OMEGA_GDN_ZERO: omega_blackwell_codegen_gdn_copy_prog */
88 } OmegaGdnSpec;
89
90 /* Builds the register-allocated IR program into *prog. 0 ok, -1 refused. */
91@@ -89,6 +90,20 @@ int omega_blackwell_codegen_gdn_prog(const OmegaGdnSpec *spec, BlackwellIRProgra
92 /* Instruction and peak-register counts (host, no device). */
93 int omega_gdn_prog_stats(const OmegaGdnSpec *spec, uint32_t *insns, uint32_t *gprs);
94
95+/* ---- state copy / zero (qwen38/decode-speed, 2026-10-19): the tail and state live in GPU-cached memory (nvos.h
96+ * NVOS32_ATTR2_GPU_CACHEABLE_YES: "For system memory this will not be coherent with direct CPU mappings"), so the host
97+ * never writes or reads them directly: reset zeroes them on the chip, set_state / get_state copy through an uncached
98+ * staging buffer. One launch moves 1024 words per CTA (grid words / 1024, 256 threads, thread t word 1024 c + 256 j + t,
99+ * j = 0..3: every LDG.E / STG.E of a warp covers 128 contiguous bytes). COPY: dst[i] = src[i]; ZERO: dst[i] = 0. */
100+#define OMEGA_GDN_COPY 1
101+#define OMEGA_GDN_ZERO 2
102+#define OMEGA_GDN_COPY_WORDS_PER_CTA 1024u
103+#define OMEGA_GDN_COPY_THREADS 256u
104+#define OMEGA_GDN_COPY_ARG_SRC 0x380u /* COPY only */
105+#define OMEGA_GDN_COPY_ARG_DST 0x388u
106+#define OMEGA_GDN_COPY_ARGS_WORDS 4u
107+int omega_blackwell_codegen_gdn_copy_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog);
108+
109 /* ---- prefill (omega#543, 2026-10-18): three launches per layer over T >= 3 new tokens, the same tail and state ----
110 * prep grid (ceil(T / 16), 16 key heads), 128 threads. Thread c convolves 5 channels of its key head (q c, k c and v c
111 * of the three value heads), sliding each window in registers over 16 tokens (the CTA starting at token 0 takes
112diff --git a/src/omega_gpu_fp8_api.c b/src/omega_gpu_fp8_api.c
113index adfe4eed..55614efe 100644
114--- a/src/omega_gpu_fp8_api.c
115+++ b/src/omega_gpu_fp8_api.c
116@@ -46,7 +46,7 @@ static struct {
117 int sched, sched_set; /* CUT5: kernel schedule 0 (default) or 1 (pipelined decode + stall pass), variant 1 only; env AIEN_OMEGA_FP8_SCHED when never set */
118 int coal, coal_set, wfirst, wfirst_set; /* CUT5 variants A (coalesced output) and B (weights before x), variant 1 only; env AIEN_OMEGA_FP8_COAL / AIEN_OMEGA_FP8_WFIRST when never set */
119 int deep, deep_set; /* SPEED-1B: automatic deep kernel for single projections whenever it fits (1, default) or never (0); env AIEN_OMEGA_FP8_DEEP when never set */
120- int bigk, bigk_set; /* qwen38/decode-speed: single projections with k > 4096 (Qwen3.8-27B) get 16 warps and a partial-depth deep kernel when x fits; env AIEN_OMEGA_FP8_BIGK when never set */
121+ int bigk, bigk_set; /* qwen38/decode-speed: single projections with k > 4096 (Qwen3.8-27B) get 16 warps and a partial-depth deep kernel when x fits; default on, env AIEN_OMEGA_FP8_BIGK when never set */
122 bool hooked;
123 Slot cache[CACHE_SLOTS];
124 unsigned next;
125@@ -148,7 +148,7 @@ int omega_gpu_fp8_wfirst(void) { return flag_now(&g.wfirst, &g.wfirst_set, "AIEN
126 void omega_gpu_fp8_set_deep(int on) { flag_set(&g.deep, &g.deep_set, on); }
127 int omega_gpu_fp8_deep(void) { return flag_now(&g.deep, &g.deep_set, "AIEN_OMEGA_FP8_DEEP", 1); }
128 void omega_gpu_fp8_set_bigk(int on) { flag_set(&g.bigk, &g.bigk_set, on); }
129-int omega_gpu_fp8_bigk(void) { return flag_now(&g.bigk, &g.bigk_set, "AIEN_OMEGA_FP8_BIGK", 0); }
130+int omega_gpu_fp8_bigk(void) { return flag_now(&g.bigk, &g.bigk_set, "AIEN_OMEGA_FP8_BIGK", 1); }
131 /* codegen by variant */
132 static int prog_for(int variant, const OmegaFp8GemvSpec *spec, BlackwellIRProgram *prog) {
133 return variant == 2 ? omega_blackwell_codegen_fp8_gemv2_prog(spec, prog) : omega_blackwell_codegen_fp8_gemv_prog(spec, prog);
134diff --git a/src/omega_gpu_fp8_api.h b/src/omega_gpu_fp8_api.h
135index 8741a486..5d3819ae 100644
136--- a/src/omega_gpu_fp8_api.h
137+++ b/src/omega_gpu_fp8_api.h
138@@ -176,7 +176,8 @@ void omega_gpu_fp8_set_deep(int on);
139 int omega_gpu_fp8_deep(void);
140 /* Big-k rule (qwen38/decode-speed): single projections with k > 4096 get 16 warps per CTA and, when x fits the launch
141 * descriptor's shared memory, the deep kernel at the largest step count that divides the row within the lane budget
142- * (k 5120: 5 steps, k 6144: 6). 0 (default until measured in the whole model) or 1; env AIEN_OMEGA_FP8_BIGK when never set. */
143+ * (k 5120: 5 steps, k 6144: 6). 1 (default since 2026-10-19: Qwen3.8-27B whole model 151.6 -> 142-143 ms per token,
144+ * golden 24/24) or 0; env AIEN_OMEGA_FP8_BIGK when never set. Shapes with k <= 4096 (the 30B lane) are unchanged. */
145 void omega_gpu_fp8_set_bigk(int on);
146 int omega_gpu_fp8_bigk(void);
147 /* Host-only: the spec the automatic choice makes for a call of k columns, n rows, `groups` experts under the current knobs
148diff --git a/src/omega_gpu_gdn_api.c b/src/omega_gpu_gdn_api.c
149index ca341182..e35d03c7 100644
150--- a/src/omega_gpu_gdn_api.c
151+++ b/src/omega_gpu_gdn_api.c
152@@ -1,7 +1,8 @@
153 /*
154 * omega_gpu_gdn_api.c: resident Gated DeltaNet parameters, tails, states and the one-launch decode step (see the
155- * header). Session use mirrors omega_gpu_router_api.c: every buffer from omega_gpu_session_alloc (GPU-uncached,
156- * host-visible), sync_to_gpu after host writes, sync_to_cpu before host reads, one omega_gpu_session_launch per call.
157+ * header). Session use mirrors omega_gpu_router_api.c: buffers from omega_gpu_session_alloc (GPU-uncached, host-visible),
158+ * sync_to_gpu after host writes, sync_to_cpu before host reads, one omega_gpu_session_launch per call; the tails and states
159+ * are the exception (GPU-cached, reached by the host only through the copy / zero launches).
160 */
161 #include "omega_gpu_gdn_api.h"
162 #include "omega_gpu_hostmath.h"
163@@ -11,6 +12,8 @@
164 #include <math.h>
165 #include <stdbool.h>
166 #include <string.h>
167+#include <stdlib.h>
168+#include "omega_gpu_mem_cached.h"
169
170 #define MARKER_TIMEOUT_MS 20000ull
171 #define POISON_F32 (-999.0f)
172@@ -28,6 +31,8 @@ struct OmegaGpuGdn {
173 OmegaGpuMem tail; /* [layers][OMEGA_GDN_TAIL_BYTES] */
174 OmegaGpuMem state; /* [layers][OMEGA_GDN_STATE_BYTES] */
175 OmegaGpuMem lb; /* launch buffer */
176+ int cached; /* tail and state GPU-cached: the host reaches them only through the chip (reset, stage) */
177+ OmegaGpuMem stage; /* cached: uncached staging for set_state / get_state, [state of one layer][tail of one layer] */
178 OmegaGpuScratch pf; /* prefill scratch (see pf_layout) */
179 };
180
181@@ -39,9 +44,10 @@ typedef struct {
182 uint32_t insns;
183 } Slot;
184
185-/* the resident kernels: decode, prefill prep, scan, post */
186-enum { KK_DECODE, KK_PREP, KK_SCAN, KK_POST, KK_N };
187-static const uint32_t kk_threads[KK_N] = { OMEGA_GDN_THREADS, OMEGA_GDN_PREP_THREADS, OMEGA_GDN_THREADS, OMEGA_GDN_THREADS };
188+/* the resident kernels: decode, prefill prep, scan, post; the state copy and zero */
189+enum { KK_DECODE, KK_PREP, KK_SCAN, KK_POST, KK_COPY, KK_ZERO, KK_N };
190+static const uint32_t kk_threads[KK_N] = { OMEGA_GDN_THREADS, OMEGA_GDN_PREP_THREADS, OMEGA_GDN_THREADS, OMEGA_GDN_THREADS,
191+ OMEGA_GDN_COPY_THREADS, OMEGA_GDN_COPY_THREADS };
192 static struct {
193 bool hooked;
194 Slot slot[KK_N];
195@@ -116,6 +122,8 @@ int omega_gdn_unpack_tail(const float *dev, float *tail) {
196 }
197
198 /* ---- buffers ---- */
199+static int zero_ts_locked(OmegaGpuGdn *g);
200+static int move_locked(unsigned kind, uint64_t src, uint64_t dst, size_t bytes);
201 int omega_gpu_gdn_alloc(uint32_t layers, OmegaGpuGdn **out) {
202 if (!out || layers == 0 || layers > OMEGA_GPU_GDN_MAX_LAYERS) return OMEGA_GPU_GDN_BAD_ARGS;
203 *out = NULL;
204@@ -126,17 +134,22 @@ int omega_gpu_gdn_alloc(uint32_t layers, OmegaGpuGdn **out) {
205 int rc = OMEGA_GPU_GDN_OK;
206 if (!dev_open_locked()) rc = OMEGA_GPU_GDN_CHIP_FAIL;
207 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;
208- 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;
209- 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;
210+ /* tail and state GPU-cached unless AIEN_OMEGA_GDN_STATE_CACHED=0 (measured: the decode step 129 -> 43 us per layer,
211+ * runs/probe-gdn): the step's 64 scalar state stores per thread merge in L2 instead of going to DRAM one by one */
212+ const char *pc = getenv("AIEN_OMEGA_GDN_STATE_CACHED");
213+ g->cached = !(pc && pc[0] == '0' && pc[1] == 0);
214+ int (*const alloc_ts)(size_t, OmegaGpuMem *) = g->cached ? omega_gpu_session_alloc_cached : omega_gpu_session_alloc;
215+ if (rc == OMEGA_GPU_GDN_OK && alloc_ts((size_t)layers * OMEGA_GDN_TAIL_BYTES, &g->tail) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL;
216+ if (rc == OMEGA_GPU_GDN_OK && alloc_ts((size_t)layers * OMEGA_GDN_STATE_BYTES, &g->state) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL;
217 if (rc == OMEGA_GPU_GDN_OK && omega_gpu_session_alloc(LB_BYTES, &g->lb) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL;
218 if (rc == OMEGA_GPU_GDN_OK) {
219 memset(g->par.cpu, 0, (size_t)layers * OMEGA_GDN_PAR_BYTES);
220- memset(g->tail.cpu, 0, (size_t)layers * OMEGA_GDN_TAIL_BYTES);
221- memset(g->state.cpu, 0, (size_t)layers * OMEGA_GDN_STATE_BYTES);
222 memset(g->lb.cpu, 0, LB_BYTES);
223- if (omega_gpu_session_sync_to_gpu(&g->par) != 0 || omega_gpu_session_sync_to_gpu(&g->tail) != 0 ||
224- omega_gpu_session_sync_to_gpu(&g->state) != 0 || omega_gpu_session_sync_to_gpu(&g->lb) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL;
225+ if (omega_gpu_session_sync_to_gpu(&g->par) != 0 || omega_gpu_session_sync_to_gpu(&g->lb) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL;
226 }
227+ /* tail and state start at zero (cached: zeroed by the chip, since a recycled page may still have lines in L2 that a
228+ * CPU write would not reach) */
229+ if (rc == OMEGA_GPU_GDN_OK) rc = zero_ts_locked(g);
230 omega_gpu_session_unlock();
231 if (rc != OMEGA_GPU_GDN_OK) { omega_gpu_gdn_free(g); return rc; }
232 *out = g;
233@@ -150,6 +163,7 @@ void omega_gpu_gdn_free(OmegaGpuGdn *g) {
234 if (g->tail.cpu) omega_gpu_session_free(&g->tail);
235 if (g->state.cpu) omega_gpu_session_free(&g->state);
236 if (g->lb.cpu) omega_gpu_session_free(&g->lb);
237+ if (g->stage.cpu) omega_gpu_session_free(&g->stage);
238 omega_gpu_session_scratch_free(&g->pf);
239 omega_gpu_session_unlock();
240 omega_gpu_host_free(g);
241@@ -169,19 +183,36 @@ int omega_gpu_gdn_set_layer(OmegaGpuGdn *g, uint32_t layer, const float *conv_w,
242 int omega_gpu_gdn_reset(OmegaGpuGdn *g) {
243 if (!g) return OMEGA_GPU_GDN_BAD_ARGS;
244 omega_gpu_session_lock();
245- memset(g->tail.cpu, 0, (size_t)g->layers * OMEGA_GDN_TAIL_BYTES);
246- memset(g->state.cpu, 0, (size_t)g->layers * OMEGA_GDN_STATE_BYTES);
247- 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;
248+ int rc = dev_open_locked() ? zero_ts_locked(g) : OMEGA_GPU_GDN_CHIP_FAIL;
249 omega_gpu_session_unlock();
250 return rc;
251 }
252
253+/* cached: the staging buffer [state][tail] of one layer (uncached), made on first use */
254+static int stage_locked(OmegaGpuGdn *g) {
255+ if (g->stage.cpu) return OMEGA_GPU_GDN_OK;
256+ return omega_gpu_session_alloc((size_t)OMEGA_GDN_STATE_BYTES + OMEGA_GDN_TAIL_BYTES, &g->stage) == 0 ? OMEGA_GPU_GDN_OK : OMEGA_GPU_GDN_CHIP_FAIL;
257+}
258+
259 int omega_gpu_gdn_set_state(OmegaGpuGdn *g, uint32_t layer, const float *tail, const float *state) {
260 if (!g || layer >= g->layers || !tail || !state) return OMEGA_GPU_GDN_BAD_ARGS;
261 omega_gpu_session_lock();
262- omega_gdn_pack_tail(tail, (float *)((uint8_t *)g->tail.cpu + (size_t)layer * OMEGA_GDN_TAIL_BYTES));
263- memcpy((uint8_t *)g->state.cpu + (size_t)layer * OMEGA_GDN_STATE_BYTES, state, OMEGA_GDN_STATE_BYTES);
264- 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;
265+ int rc = OMEGA_GPU_GDN_OK;
266+ const size_t so = (size_t)layer * OMEGA_GDN_STATE_BYTES, to = (size_t)layer * OMEGA_GDN_TAIL_BYTES;
267+ if (!g->cached) {
268+ omega_gdn_pack_tail(tail, (float *)((uint8_t *)g->tail.cpu + to));
269+ memcpy((uint8_t *)g->state.cpu + so, state, OMEGA_GDN_STATE_BYTES);
270+ 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;
271+ } else {
272+ rc = dev_open_locked() ? stage_locked(g) : OMEGA_GPU_GDN_CHIP_FAIL;
273+ if (rc == OMEGA_GPU_GDN_OK) {
274+ memcpy(g->stage.cpu, state, OMEGA_GDN_STATE_BYTES);
275+ omega_gdn_pack_tail(tail, (float *)((uint8_t *)g->stage.cpu + OMEGA_GDN_STATE_BYTES));
276+ if (omega_gpu_session_sync_to_gpu(&g->stage) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL;
277+ }
278+ if (rc == OMEGA_GPU_GDN_OK) rc = move_locked(KK_COPY, g->stage.va, g->state.va + so, OMEGA_GDN_STATE_BYTES);
279+ if (rc == OMEGA_GPU_GDN_OK) rc = move_locked(KK_COPY, g->stage.va + OMEGA_GDN_STATE_BYTES, g->tail.va + to, OMEGA_GDN_TAIL_BYTES);
280+ }
281 omega_gpu_session_unlock();
282 return rc;
283 }
284@@ -189,13 +220,24 @@ int omega_gpu_gdn_set_state(OmegaGpuGdn *g, uint32_t layer, const float *tail, c
285 int omega_gpu_gdn_get_state(OmegaGpuGdn *g, uint32_t layer, float *tail, float *state) {
286 if (!g || layer >= g->layers || (!tail && !state)) return OMEGA_GPU_GDN_BAD_ARGS;
287 omega_gpu_session_lock();
288- 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;
289- if (rc == OMEGA_GPU_GDN_OK && tail &&
290- omega_gdn_unpack_tail((const float *)((const uint8_t *)g->tail.cpu + (size_t)layer * OMEGA_GDN_TAIL_BYTES), tail) != 0) {
291+ int rc = OMEGA_GPU_GDN_OK;
292+ const size_t so = (size_t)layer * OMEGA_GDN_STATE_BYTES, to = (size_t)layer * OMEGA_GDN_TAIL_BYTES;
293+ const uint8_t *sp, *tp;
294+ if (!g->cached) {
295+ 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;
296+ sp = (const uint8_t *)g->state.cpu + so; tp = (const uint8_t *)g->tail.cpu + to;
297+ } else {
298+ rc = dev_open_locked() ? stage_locked(g) : OMEGA_GPU_GDN_CHIP_FAIL;
299+ if (rc == OMEGA_GPU_GDN_OK) rc = move_locked(KK_COPY, g->state.va + so, g->stage.va, OMEGA_GDN_STATE_BYTES);
300+ if (rc == OMEGA_GPU_GDN_OK) rc = move_locked(KK_COPY, g->tail.va + to, g->stage.va + OMEGA_GDN_STATE_BYTES, OMEGA_GDN_TAIL_BYTES);
301+ if (rc == OMEGA_GPU_GDN_OK && omega_gpu_session_sync_to_cpu(&g->stage) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL;
302+ sp = g->stage.cpu; tp = sp + OMEGA_GDN_STATE_BYTES;
303+ }
304+ if (rc == OMEGA_GPU_GDN_OK && tail && omega_gdn_unpack_tail((const float *)tp, tail) != 0) {
305 omega_gpu_session_set_error("gdn: the per-head copies of a key head's conv tail disagree");
306 rc = OMEGA_GPU_GDN_CHIP_FAIL;
307 }
308- 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);
309+ if (rc == OMEGA_GPU_GDN_OK && state) memcpy(state, sp, OMEGA_GDN_STATE_BYTES);
310 omega_gpu_session_unlock();
311 return rc;
312 }
313@@ -207,6 +249,7 @@ static int kernel_build(unsigned kind, const OmegaGdnSpec *spec, BlackwellIRProg
314 case KK_PREP: return omega_blackwell_codegen_gdn_prep_prog(spec, prog);
315 case KK_SCAN: return omega_blackwell_codegen_gdn_scan_prog(spec, prog);
316 case KK_POST: return omega_blackwell_codegen_gdn_post_prog(spec, prog);
317+ case KK_COPY: case KK_ZERO: return omega_blackwell_codegen_gdn_copy_prog(spec, prog);
318 default: return -1;
319 }
320 }
321@@ -239,6 +282,37 @@ static int kernel_for(unsigned kind, const OmegaGdnSpec *spec, Slot **out, bool
322 return OMEGA_GPU_GDN_OK;
323 }
324
325+
326+/* One copy (KK_COPY: dst = src) or zero (KK_ZERO: dst = 0, src unused) launch over `bytes` (a multiple of 4096), submitted
327+ * and waited for. Caller holds the session lock with the device open. */
328+static int move_locked(unsigned kind, uint64_t src, uint64_t dst, size_t bytes) {
329+ const size_t per = (size_t)OMEGA_GDN_COPY_WORDS_PER_CTA * 4u;
330+ if ((kind != KK_COPY && kind != KK_ZERO) || !dst || bytes == 0 || bytes % per || bytes / per > 0x7fffffffu) return OMEGA_GPU_GDN_BAD_ARGS;
331+ OmegaGdnSpec spec; memset(&spec, 0, sizeof spec);
332+ spec.copy = kind == KK_COPY ? OMEGA_GDN_COPY : OMEGA_GDN_ZERO;
333+ Slot *ks = NULL; bool hit = false;
334+ int rc = kernel_for(kind, &spec, &ks, &hit);
335+ if (rc != OMEGA_GPU_GDN_OK) return rc;
336+ uint32_t args[OMEGA_GDN_COPY_ARGS_WORDS] = { (uint32_t)src, (uint32_t)(src >> 32), (uint32_t)dst, (uint32_t)(dst >> 32) };
337+ OmegaGpuLaunch L = {
338+ .code_va = ks->code.va, .gpr_count = omega_bw_gpr_declared(ks->kernel.gpr_count),
339+ .threads_x = OMEGA_GDN_COPY_THREADS, .threads_y = 1, .grid_x = (uint32_t)(bytes / per), .grid_y = 1,
340+ .num_elements = bytes / 4u, .args = args, .n_args = OMEGA_GDN_COPY_ARGS_WORDS, .timeout_ms = MARKER_TIMEOUT_MS,
341+ };
342+ 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"); return OMEGA_GPU_GDN_CHIP_FAIL; }
343+ return omega_gpu_session_launch(&L, NULL, NULL) == 0 ? OMEGA_GPU_GDN_OK : OMEGA_GPU_GDN_CHIP_FAIL;
344+}
345+
346+/* Every layer's tail and state to zero: cached on the chip, uncached by the host. Caller holds the lock, device open. */
347+static int zero_ts_locked(OmegaGpuGdn *g) {
348+ const size_t tb = (size_t)g->layers * OMEGA_GDN_TAIL_BYTES, sb = (size_t)g->layers * OMEGA_GDN_STATE_BYTES;
349+ if (!g->cached) {
350+ memset(g->tail.cpu, 0, tb); memset(g->state.cpu, 0, sb);
351+ return (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;
352+ }
353+ int rc = move_locked(KK_ZERO, 0, g->tail.va, tb);
354+ return rc == OMEGA_GPU_GDN_OK ? move_locked(KK_ZERO, 0, g->state.va, sb) : rc;
355+}
356 /* rec non-NULL (record path, caller holds the session lock): the inputs are the device rows dev_*, the launch is
357 * appended to rec instead of submitted, nothing is staged, poisoned or read back. */
358 static int gdn_core(OmegaGpuGdn *g, uint32_t layer, const float *qkv, const float *z, const float *ab, float eps_l2,
359diff --git a/src/omega_gpu_gdn_api.h b/src/omega_gpu_gdn_api.h
360index c7de6f1a..3dae79b1 100644
361--- a/src/omega_gpu_gdn_api.h
362+++ b/src/omega_gpu_gdn_api.h
363@@ -6,6 +6,9 @@
364 *
365 * int omega_gpu_gdn_alloc(uint32_t layers, OmegaGpuGdn **out);
366 * Device-resident per-layer parameters, conv tails and recurrent states (zeroed), plus the launch buffer.
367+ * The tails and states are GPU-cached (2026-10-19; AIEN_OMEGA_GDN_STATE_CACHED=0 keeps them uncached): the host
368+ * never touches them directly, reset zeroes them with a chip launch and set_state / get_state copy one layer through
369+ * an uncached staging buffer with chip launches (omega_blackwell_codegen_gdn.h, state copy / zero).
370 * int omega_gpu_gdn_set_layer(OmegaGpuGdn *g, uint32_t layer, const float *conv_w, const float *a_log,
371 * const float *dt_bias, const float *norm_w);
372 * conv_w: linear_attn.conv1d.weight [10240][1][4] widened to f32 ([10240][4]); a_log, dt_bias: [48];
373diff --git a/tests/gpu_fp8_api_test.c b/tests/gpu_fp8_api_test.c
374index 0825cc4f..2370dbfb 100644
375--- a/tests/gpu_fp8_api_test.c
376+++ b/tests/gpu_fp8_api_test.c
377@@ -297,7 +297,7 @@ static void bigk_check(void) {
378 OmegaFp8GemvSpec g8; omega_gpu_fp8_spec_for(5120, 34816, 8, &g8);
379 CHECK(g8.warps == 32u && g8.depth == 0u, "bigk %d: grouped launches keep 32 warps, classic (warps %u depth %u)", on, g8.warps, g8.depth);
380 }
381- omega_gpu_fp8_set_bigk(0);
382+ omega_gpu_fp8_set_bigk(1); /* the default */
383 }
384
385 static void host_only(void) {
386diff --git a/tests/gpu_gdn_test.c b/tests/gpu_gdn_test.c
387index 4aab2281..3fd79d92 100644
388--- a/tests/gpu_gdn_test.c
389+++ b/tests/gpu_gdn_test.c
390@@ -158,13 +158,22 @@ static void host_only(void) {
391 free(blk);
392 /* codegen budgets */
393 for (int m = 0; m <= 4; m++) {
394- OmegaGdnSpec sp = { m }; uint32_t ni = 0, ng = 0;
395+ OmegaGdnSpec sp = { .mutant = m }; uint32_t ni = 0, ng = 0;
396 int rc = omega_gdn_prog_stats(&sp, &ni, &ng);
397 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);
398 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);
399 }
400- OmegaGdnSpec bad = { 5 };
401+ OmegaGdnSpec bad = { .mutant = 5 };
402 CHECK(omega_gdn_prog_stats(&bad, NULL, NULL) == -1, "mutant 5 refused");
403+ /* the state copy / zero programs (cached tail and state) */
404+ {
405+ static BlackwellIRProgram cp;
406+ OmegaGdnSpec c1 = { .copy = OMEGA_GDN_COPY }, c2 = { .copy = OMEGA_GDN_ZERO }, c3 = { .copy = 3 }, cm = { .mutant = 1, .copy = OMEGA_GDN_COPY };
407+ CHECK(omega_blackwell_codegen_gdn_copy_prog(&c1, &cp) == 0 && cp.regalloc.peak_gpr_usage + 2 <= BW_PHYS_GPR_MAX, "copy codegen");
408+ CHECK(omega_blackwell_codegen_gdn_copy_prog(&c2, &cp) == 0, "zero codegen");
409+ CHECK(omega_blackwell_codegen_gdn_copy_prog(&c3, &cp) == -1 && omega_blackwell_codegen_gdn_copy_prog(&cm, &cp) == -1, "copy codegen refuses kind 3 and a mutant");
410+ CHECK(omega_gdn_prog_stats(&c1, NULL, NULL) == -1, "decode codegen refuses a copy spec");
411+ }
412 /* the oracle's delta rule: from S = 0 with beta ~ 1, the step writes v along k, so S^T k = v (|k| = 1) */
413 {
414 DState d; dstate_init(&d, &L);
415@@ -220,7 +229,7 @@ enum { SB_QKV, SB_Z, SB_AB, SB_PAR, SB_TAIL, SB_STATE, SB_OUT, SB_N };
416 static uint64_t g_sim_issues;
417 static int sim_run_steps(int mutant, uint32_t steps, float in_scale, float out_scale, Errs *worst, char *msg, size_t n) {
418 static BlackwellIRProgram prog;
419- OmegaGdnSpec spec = { mutant };
420+ OmegaGdnSpec spec = { .mutant = mutant };
421 if (omega_blackwell_codegen_gdn_prog(&spec, &prog) != 0) { snprintf(msg, n, "codegen refused"); return -1; }
422 Layer L; layer_make(&L, 21);
423 DState d; dstate_init(&d, &L);
424@@ -329,7 +338,7 @@ static int sim_launch(const BlackwellIRProgram *prog, uint32_t threads, uint32_t
425 static uint32_t fw(float f) { uint32_t u; memcpy(&u, &f, 4); return u; }
426 static int sim_prefill(int mutant, uint32_t T, float in_scale, float out_scale, Errs *worst, char *msg, size_t n) {
427 static BlackwellIRProgram pp, ps, pq;
428- OmegaGdnSpec spec = { mutant };
429+ OmegaGdnSpec spec = { .mutant = mutant };
430 if (omega_blackwell_codegen_gdn_prep_prog(&spec, &pp) != 0 || omega_blackwell_codegen_gdn_scan_prog(&spec, &ps) != 0 ||
431 omega_blackwell_codegen_gdn_post_prog(&spec, &pq) != 0) { snprintf(msg, n, "codegen refused"); return -1; }
432 Layer L; layer_make(&L, 21);
433@@ -408,6 +417,30 @@ static void sim_prefill_battery(void) {
434 }
435 }
436
437+/* ---- the state copy / zero on the simulator: 3 CTAs; a 2-CTA launch leaves the last 1024 words (negative control) ---- */
438+static void sim_copy_battery(void) {
439+ static BlackwellIRProgram cp;
440+ enum { W = 3 * OMEGA_GDN_COPY_WORDS_PER_CTA };
441+ static uint32_t src[W], dst[W];
442+ uint32_t x = 12345u;
443+ for (unsigned i = 0; i < W; i++) { x = x * 1664525u + 1013904223u; src[i] = x; }
444+ char msg[200] = "";
445+ for (int kind = OMEGA_GDN_COPY; kind <= OMEGA_GDN_ZERO; kind++) {
446+ OmegaGdnSpec sp = { .copy = kind };
447+ CHECK(omega_blackwell_codegen_gdn_copy_prog(&sp, &cp) == 0, "copy kind %d codegen", kind);
448+ for (uint32_t gx = 3; gx >= 2; gx--) {
449+ memset(dst, 0xcd, sizeof dst);
450+ SimBuf b[2] = { { OMEGA_GDN_COPY_ARG_SRC, src, sizeof src }, { OMEGA_GDN_COPY_ARG_DST, dst, sizeof dst } };
451+ int err = sim_launch(&cp, OMEGA_GDN_COPY_THREADS, gx, 1, 0, 0, b, 2, NULL, 0, msg, sizeof msg);
452+ unsigned bad = 0;
453+ for (unsigned i = 0; i < W; i++) if (dst[i] != (kind == OMEGA_GDN_COPY ? src[i] : 0u)) bad++;
454+ CHECK(err == 0, "sim copy kind %d grid %u ran (%s)", kind, gx, msg);
455+ if (gx == 3) CHECK(bad == 0, "sim copy kind %d: every word (%u wrong)", kind, bad);
456+ else CHECK(bad == OMEGA_GDN_COPY_WORDS_PER_CTA, "sim copy kind %d grid 2 caught: %u words untouched", kind, bad);
457+ }
458+ }
459+}
460+
461 /* ---- chip ---- */
462 static int chip_steps(OmegaGpuGdn *g, uint32_t layer, const Layer *L, DState *d, uint32_t steps, uint32_t seed, float in_scale,
463 float out_scale, Errs *worst, float *last_out) {
464@@ -758,7 +791,7 @@ int main(int argc, char **argv) {
465 else { printf("unknown argument %s\n", argv[i]); return 2; }
466 }
467 if (host) host_only();
468- if (sim) { sim_battery(); sim_prefill_battery(); }
469+ if (sim) { sim_battery(); sim_prefill_battery(); sim_copy_battery(); }
470 if (!host && !sim) {
471 (void)omega_gpu_session_set_spin_us(spin);
472 chip_run(out_path, reps ? reps : 50);