Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
About Files History Releases Proposed changes Issues For developers
GDN prefill for Qwen3.8-27B on the GB10 (#543)
merged 160ca5830dda6d01 #549 · opened 2026-10-18 13:03 UTC · closed 2026-10-24 17:56 UTC · merged 2026-10-24 17:56 UTC
Imported from GitHub aien-dev/omega-mv#549, by drakestapleton
Base 2ec8fc974d5a2b8766782e4dea787016bc62ac28 · Head 03d975f7e17e915fd3a52a21c0e7b9be7bc13192
Merged on GitHub as 7bda76611e606b4d2b2f0336b2d2dda56dd950a0 ; reachable from main c7acd2607139eedfabeac16b6042cefe8ede431f (observed 2026-11-18T16:35:57Z)
drakestapleton wrote on 2026-10-18 13:03 UTC
Closes #543. Stacked on #548 (GDN decode), which sits on #547. Part of the Qwen3.8-27B swap.
What: the Gated DeltaNet prefill, three launches per layer over the whole prompt:
prep: conv over the carried tail and the prompt, SiLU, l2-normalised q and k, and the decay and beta gates, 16 tokens per block, written as 1552-byte per-token records.
scan: the delta-rule recurrence. Each head's 128x128 state is held in registers (16 quads per thread) and tokens are staged in shared memory 8 at a time. The reductions use SHFL.BFLY (xor 4/8/16). At the end it writes the state and the conv tail.
post: gated RMSNorm times silu(z).
The state and tail it leaves are byte-compatible with the decode step (#548), so generation carries on exactly. A record path puts prefill plus decode into one command list on a reserved scratch; a request past the reservation is refused (OMEGA_GPU_SESSION_SCRATCH_RESERVED) and never silently regrown. SHFL.BFLY goes through the existing tc patch pass, and its count is checked against the IR, so a launch is refused on a mismatch. The warp simulator learns BFLY.
Chip evidence (GB10, evidence/gdn-prefill-20261018 /; binary = base 19bb4437 + change.diff, see provenance.txt; SHA256SUMS):
54/54 PASS against a double oracle. That covers every decode check plus: prefill T=37 then 3 decode steps; T=3 then 1; T=41 at extreme scales (output err about 2-3e-7, tail exact, state about 4e-7); a bit-identical rerun; mutants 1-4 caught; and the record path, prefill 37 + 1 decode step in one command list.
Speed (medians of 5; prep / scan / post):
T prep scan post total per token per layer
512 407.3 us 1439.8 us 230.7 us 2077.8 us 4.06 us
2048 1566.6 us 5362.1 us 851.3 us 7780.0 us 3.80 us
8192 12701.7 us 21624.5 us 7193.5 us 41519.6 us 5.07 us
The scan runs at about 2.4 TFLOP/s (calc:26da03d2 ). The chip's peak for FP32 SIMT is not measured here. The tensor-core GEMM reaches 13.92 TFLOPS measured (#547), which is the case for a chunked tensor-core scan (follow-up issue).
prep and post grow faster than linear from 2048 to 8192 tokens. Not explained yet; the engine will feed the prefill in chunks of 2048 or fewer (state carries across chunks).
"call" time on the host path, including uploads, is much larger than chip time. The engine will use the record path, which has no per-call upload (#642).
Not merging: merges are paused by Drake (2026-10-18 12:00Z).
🤖 Generated with Claude Code
Fetch it
A git bundle of the pull request's head, 47818 bytes.
curl -fsSL https://interplane.io/drakestapleton/omega-mv/patches/160ca5830dda6d01 .bundle -o 160ca5830dda6d01 .bundle
git fetch 160ca5830dda6d01 .bundle refs/pull/549/head
Raw bundle · Diff
Provenance
This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record:
github-import 1
aien-dev/omega-mv
549
03d975f7e17e915fd3a52a21c0e7b9be7bc13192
{"kind":"github-import","owner":"aien-dev","repo":"omega-mv","number":549,"url":"https://github.com/aien-dev/omega-mv/pull/549","export_sha256":"7319147b350ad908bf0b731068ec3be7b8dee848041dc862f71455e8e4680140 ","imported_utc":"2026-11-18T16:35:57Z"}
Changes
diff --git a/.crumb b/.crumb
index 2eabb2db9 ..fcab4771b 100644
--- a/.crumb
+++ b/.crumb
@@ -77,14 +77,14 @@
"purpose": "Directory spec. Purpose not yet described by a human."
},
{
- "digest": "b9cf3d455a68494d3d144976721163f1037ebc8ae64628bae218cb46dfd60129 ",
- "last_commit": "47eec857 ",
+ "digest": "cceb4d16f88ad5ae07eec048bcfbc84bfe1e011f05d9ead33957479fd6dd23bd ",
+ "last_commit": "c480683e ",
"name": "src",
"purpose": "Directory src. Purpose not yet described by a human."
},
{
- "digest": "b9d21f0ce5aac9c60dc57b5b37d9b4d8f3b0df0de30f9e76ab471e4aa8c76b1a ",
- "last_commit": "b07acc26 ",
+ "digest": "edf167b3607974785ec9cb7478d8d26d562cb5c83107322efa79954482d34c39 ",
+ "last_commit": "c480683e ",
"name": "tests",
"purpose": "Directory tests. Purpose not yet described by a human."
},
@@ -95,15 +95,15 @@
"purpose": "Directory tools. Purpose not yet described by a human."
}
],
- "children_root": "sha256:cd50a01f60177bd752a8c81be9addb0397e06de6ad9a9bb2a7dd38d5d0646c7d ",
- "compiled_at": "2026-10-18T13:01:43Z",
+ "children_root": "sha256:90a116089dcc944c5cf4999fa010ba497d8d4f377d9ab94dac3d9beab35975a1 ",
+ "compiled_at": "2026-10-18T13:02:58Z",
"compiler": "crumb-compile/1",
- "digest": "sha256:3e8f3c55262e17ca27117c0519d2ce75c3e15665e4d14fb749d0f94b5e5554ec ",
- "evidence_files": 11157,
- "evidence_root": "sha256:6c25c5a2af2a7392b921cb5ec66c2facffae99cf110c5940a8278452933134ff ",
- "files": 14144,
- "generated_at_commit": "e0f65ff3157d5214cce0a6b7f35263dd73e3e62c ",
- "source_tree": "sha256:5d13a99fb4fe9992574c1ce830ef6716448341845d20d5f729802950c404f3c5 "
+ "digest": "sha256:e4cc6c5c8876c6c15fccdccb65f48f52ef393c5e9ba1d3cc1eda1aa5de33af70 ",
+ "evidence_files": 11170,
+ "evidence_root": "sha256:d84334b20a3fd68396eb9a7fc57b8173504a9f62737374661ea71f1ecea5998c ",
+ "files": 14157,
+ "generated_at_commit": "e7356996557da557d63de0f56558a335b5c25223 ",
+ "source_tree": "sha256:045fc88fe0ffb89013c1c839de6438147f6561bde91be68251300db18489e1a8 "
},
"owner_lane": "",
"provenance": {
diff --git a/README.md b/README.md
index bbdb8dbfa ..3de0ed37b 100644
--- a/README.md
+++ b/README.md
@@ -15,7 +15,7 @@ Research-grade and pre-alpha. Plainly:
- **Omega Systems Core compiler:** OSC-0 is frozen as a design. OSC-1 and OSC-2 are implemented with host receipts and are NOT QUALIFIED. OSC-3 is in progress ([OSC-3-DESIGN](docs/osc/OSC-3-DESIGN.md)). It is not self-hosting and there is no general Omega compiler yet; the old Milestone 6 "self-host" result was a fixed-output self-copy check, not compilation ([note](docs/osc/OSC-1-SELF-HOST-STATEMENT.md)).
- **Reaction runtime:** the R1 to R16 qualification ladder passed on builds that predate the composition modules (omega#126), a historical result. Requalification at `3108fc2 ` recorded R15 FAIL (14 of 16 gates: G1, G15) and R16 FAIL (`evidence/REQUAL-3108fc2 /`); the R16 receipt `22d7a79a ` at `850fc54 ` is invalid as a current claim because its script was later found defective; none of R1 to R16 is requalified on the current candidate, and the ladder rerun is in progress. The R16 loop-site inventory was reconciled on the candidate tree (502 sites, 0 unclassified, 0 stale, PR #279); that is the inventory gate only, not R16. The current living build is IMPLEMENTED / NOT QUALIFIED until re-qualified. Composition, Capability Graph, Skill Router, J-Space and Cortex run on the host; the Fabric interface is a host-only loopback with no network; none of this is on silicon as a qualified system.
- **Numerics (E1):** closed 2026-10-02 on the GB10 chip campaign (Gate 5 26/26 with 47 parity lines, reductions SUM/MAX/MIN/MEAN, nine transcendental ops over all 2^32 inputs each, 148 load/store kernels), consolidated receipt `evidence/E1-CLOSURE/e7851c69d34ac777a9436af16d0bdd5d69264c8528c62d562b600f5d89153061 .json`; exclusions (natural-base EXP/LOG have no GB10 kernel) are listed in [E1_GAP_TABLE](docs/numeric/E1_GAP_TABLE.md).
-- **Qwen3.8-27B kernels (GB10, 2026-10-18):** each one is checked on the chip against a double-precision reference with deliberately broken variants that must fail, and none is joined into a serving engine yet. FP8 matrix-vector and the tensor-core prefill GEMM now take k up to 32768, which covers the 27B down projection at k = 17408 (`evidence/fp8-k32768-20261018 /`, omega#541). The Gated DeltaNet decode step (the recurrent layer used by 48 of the 64 layers) runs as one launch per layer and matches the reference to about 2e-7; it takes 125.7 us per layer, which moves its 6.29 MB of state at 50.1 GB/s against 273 GB/s nominal (`evidence/gdn-decode-20261018 /`, omega#542).
+- **Qwen3.8-27B kernels (GB10, 2026-10-18):** each one is checked on the chip against a double-precision reference with deliberately broken variants that must fail, and none is joined into a serving engine yet. FP8 matrix-vector and the tensor-core prefill GEMM now take k up to 32768, which covers the 27B down projection at k = 17408 (`evidence/fp8-k32768-20261018 /`, omega#541). The Gated DeltaNet decode step (the recurrent layer used by 48 of the 64 layers) runs as one launch per layer and matches the reference to about 2e-7; it takes 125.7 us per layer, which moves its 6.29 MB of state at 50.1 GB/s against 273 GB/s nominal (`evidence/gdn-decode-20261018 /`, omega#542). The DeltaNet prefill reads a whole prompt in three launches per layer and hands its state to the decode step, which carries on exactly; at 2048 tokens it takes 3.80 us per token per layer, and the recurrent scan in it does about 2.4 trillion operations a second, against a chip peak for that kind of math that has not been measured yet (`evidence/gdn-prefill-20261018 /`, omega#543).
- **Hardware:** results on the DGX Spark GB10 are tied to the exact commit in each receipt. Host and emulator passes are never reported as hardware qualification.
The live status is owned by [aien-architecture](https://github.com/aien-spark/aien-architecture): [CURRENT_EXECUTION_PLAN.md](https://github.com/aien-spark/aien-architecture/blob/main/CURRENT_EXECUTION_PLAN.md) and [doctrine/ROADMAP.md](https://github.com/aien-spark/aien-architecture/blob/main/doctrine/ROADMAP.md). This file does not restate numbers that drift. Historical milestone receipts (for example Milestone 4, `OMEGA_SEMANTICS`, 12 of 12 gates) stay in `evidence/` unchanged as history.
diff --git a/evidence/gdn-prefill-20261018 /SHA256SUMS b/evidence/gdn-prefill-20261018 /SHA256SUMS
new file mode 100644
index 000000000 ..e48747d1b
--- /dev/null
+++ b/evidence/gdn-prefill-20261018 /SHA256SUMS
@@ -0,0 +1,12 @@
+0ae4c062502b3d7191df76422cb4c78a3bb28a9eadbcd1f370dd00dba2a8a98f ./base-commit.txt
+8c065519fccafbd041cebde95135a8943ec6748480569a8d7925ced7f508330e ./binary.sha256
+d2529d1d459b6019694993d68b42005f5638eba32cc37dd279854834f98c1afe ./built-from-commit.txt
+b3cba72db37a2732a372693dba9ff9ec5788f670b57a292df3b9bb38928c917d ./change.diff
+3cf44fc69f682142c411e5de91e02a70815233896a86989d04b9c36e34e22d3f ./chip.log
+711c7e4fa5a7d7936bcf0e07007163b3af9d12b87b86c8b26b62b06aa493b92b ./chip-queued.txt
+711c7e4fa5a7d7936bcf0e07007163b3af9d12b87b86c8b26b62b06aa493b92b ./chip-started.txt
+da17bef11bfda10d558195f7f974e703cf97b2db7388466b3ee2c4a15013735e ./device.txt
+409d2c8f88bec05e4e9a4581c0f812deb4b9bd4300eb3613492bfb2c96f00a99 ./host.log
+5eae9dded584abec67865e64c8f4d8730912b41390bd463be66e8e340f60b01c ./provenance.txt
+49f703ed3d1fe8ccfd508a5840ce7e64d0dbd1d6c73976bd82980ed28b77f36d ./receipt-chip.json
+44a4042bd529ce8056c1befd6838cd47b399661b59d4ed68fbead51a2f84a04b ./sim.log
diff --git a/evidence/gdn-prefill-20261018 /base-commit.txt b/evidence/gdn-prefill-20261018 /base-commit.txt
new file mode 100644
index 000000000 ..5cd0c11d9
--- /dev/null
+++ b/evidence/gdn-prefill-20261018 /base-commit.txt
@@ -0,0 +1 @@
+19bb443780ceb64ca26166e278d2572a10c111db
diff --git a/evidence/gdn-prefill-20261018 /binary.sha256 b/evidence/gdn-prefill-20261018 /binary.sha256
new file mode 100644
index 000000000 ..f8b74a96d
--- /dev/null
+++ b/evidence/gdn-prefill-20261018 /binary.sha256
@@ -0,0 +1 @@
+7f1d0b1049b8e9906932005965638b4487a012b539e034f65c029bba4d8e20e4 build/gpu_gdn_test
diff --git a/evidence/gdn-prefill-20261018 /built-from-commit.txt b/evidence/gdn-prefill-20261018 /built-from-commit.txt
new file mode 100644
index 000000000 ..34e2ca494
--- /dev/null
+++ b/evidence/gdn-prefill-20261018 /built-from-commit.txt
@@ -0,0 +1 @@
+5f40bd00435ec5b753587aea0e7a33a6fc895f3c
diff --git a/evidence/gdn-prefill-20261018 /change.diff b/evidence/gdn-prefill-20261018 /change.diff
new file mode 100644
index 000000000 ..5f30385e5
--- /dev/null
+++ b/evidence/gdn-prefill-20261018 /change.diff
@@ -0,0 +1,1203 @@
+diff --git a/src/omega_blackwell_codegen_gdn.c b/src/omega_blackwell_codegen_gdn.c
+index 235bace7 ..68185cf3 100644
+--- a/src/omega_blackwell_codegen_gdn.c
++++ b/src/omega_blackwell_codegen_gdn.c
+@@ -4,6 +4,7 @@
+ * omega_bw_em.h (the state loads are in flight through the conv, norm and scalar phases).
+ */
+ #include "omega_blackwell_codegen_gdn.h"
++#include "omega_blackwell_codegen_tc.h"
+ #include <string.h>
+
+ #define CW(stall, wbar, rbar, wait) \
+@@ -19,10 +20,14 @@
+
+ enum { K_FIXED, K_PRED, K_VAR, K_VAR_MORE, K_UVAR, K_STORE, K_SYNC, K_EXIT, K_BRA, K_BSSY, K_BSYNC };
+
++#define MAX_LOOPS 2
++
+ typedef struct {
+ BlackwellIRProgram *p;
+ uint32_t pending, held;
+ int err;
++ int depth, open_start[MAX_LOOPS];
++ uint32_t placeholders; /* SHFL.BFLY words omega_tc_patch_code must patch */
+ } Em;
+
+ static void emit(Em *e, BlackwellIRInsn in, int kind) {
+@@ -104,6 +109,8 @@ static void fminn(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FMNMX_MIN, d, a, b, R(
+ 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); }
+ 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); }
+ 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); }
++static void bra_if_p0(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.predicate_p0 = true; i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); }
++static int bra_fwd(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = 1; emit(e, i, K_BRA); return at; }
+ 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; }
+ static void patch_bra(Em *e, int at, int target) { if (!e->err) e->p->insns[at].imm = (uint32_t)(target - at); }
+ 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; }
+@@ -119,6 +126,23 @@ static void tail(Em *e) {
+ typedef struct { int bssy, bra; } Region;
+ static Region region_skip_if_p0(Em *e) { Region r; r.bssy = bssy_fwd(e); r.bra = bra_if_p0_fwd(e); return r; }
+ 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); }
++/* Loops: a value defined before the loop and read inside it stays live until the back-branch (the linear-scan
++ * allocator knows no back edges; omega_blackwell_codegen_pattn.c). */
++static void loop_begin(Em *e) { if (e->depth >= MAX_LOOPS) { e->err = -1; return; } e->open_start[e->depth++] = here(e); }
++static void loop_end(Em *e) {
++ if (e->depth <= 0) { e->err = -1; return; }
++ int start = e->open_start[--e->depth], end = here(e) - 1;
++ OmegaRegAlloc *ra = &e->p->regalloc;
++ for (int v = 0; v < ra->num_vregs; v++) {
++ OmegaLiveInterval *iv = &ra->intervals[v];
++ if (iv->first_def >= 0 && iv->first_def < start && iv->last_use >= start && iv->last_use < end) iv->last_use = end;
++ }
++}
++static void mov(Em *e, Op d, Op a) { imadi(e, d, a, 1u, NOREG); }
++/* SHFL.BFLY placeholder (omega_blackwell_codegen_tc.h), patched after encoding */
++static void shfl_bfly(Em *e, int d, int a, uint32_t lanes) { shfl_down(e, d, a, OMEGA_TC_MARK_BFLY | (lanes & 31u)); e->placeholders++; }
++/* butterfly sum over lane strides lo, 2 lo .. hi: every lane of a group ends with the same bits (a + b == b + a) */
++static void bfly_sum(Em *e, int v, int t, uint32_t lo, uint32_t hi) { for (uint32_t o = lo; o <= hi; o *= 2) { shfl_bfly(e, t, v, o); fadd(e, R(v), R(v), R(t)); } }
+ /* warp sum: the total lands in lane 0 */
+ 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)); } }
+ /* lane 0 of every warp stores v to shared [addr] */
+@@ -354,3 +378,394 @@ int omega_gdn_prog_stats(const OmegaGdnSpec *spec, uint32_t *insns, uint32_t *gp
+ if (gprs) *gprs = prog.regalloc.peak_gpr_usage;
+ return 0;
+ }
++
++/* ======== prefill (omega#543): prep, scan, post (layouts in the header) ======== */
++
++/* softplus, decay and beta of one value head (the decode phase C maths): abq = {a, b}, scq = {negA, dt_bias} */
++static void gdn_scalars(Em *e, int MUT, int abq, int scq, int dec, int beta, int t1, int cnl2e, int fone) {
++ int xs = V(e), mn = V(e), sp = V(e), cl2e = V(e);
++ if (e->err) return;
++ fadd(e, R(xs), S(abq, 0), S(scq, 1));
++ movi(e, mn, fbits(-1.0f)); fmul(e, R(mn), R(xs), R(mn)); fminn(e, R(mn), R(xs), R(mn));
++ movi(e, cl2e, fbits(1.44269504f ));
++ 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));
++ mufu(e, BW_IR_MUFU_LG2, R(mn), R(mn));
++ movrz(e, R(sp)); fmaxx(e, R(sp), R(xs), R(sp));
++ movi(e, t1, fbits(0.693147181f )); ffma(e, R(sp), R(mn), R(t1), R(sp));
++ if (MUT == 1) movi(e, dec, fbits(1.0f));
++ 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)); }
++ if (MUT == 2) movi(e, beta, fbits(1.0f));
++ else {
++ fmul(e, R(beta), S(abq, 1), R(cnl2e)); mufu(e, BW_IR_MUFU_EX2, R(beta), R(beta));
++ fadd(e, R(beta), R(beta), R(fone)); mufu(e, BW_IR_MUFU_RCP, R(beta), R(beta));
++ }
++}
++
++int omega_blackwell_codegen_gdn_prep_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog) {
++ if (!spec || !prog || spec->mutant < 0 || spec->mutant > 4) return -1;
++ const int MUT = spec->mutant;
++ const float qs = 0.0883883476f ; /* 1 / sqrt(128) */
++ const uint32_t ROW = OMEGA_GDN_CONV_DIM * 4u, REC_ROW = OMEGA_GDN_VALUE_HEADS * OMEGA_GDN_REC_BYTES;
++ omega_bw_ir_init(prog);
++ Em em; memset(&em, 0, sizeof em); em.p = prog; Em *e = &em;
++
++ int udesc = UV64(e);
++ int c = V(e), cx = V(e), hk = V(e), warp = V(e), lane = V(e), one = V(e), T = V(e), t0 = V(e), n = V(e), t1 = V(e);
++ int pqkv = V64(e), ppar = V64(e), ptail = V64(e), prec = V64(e), pab = V64(e), insc = V(e);
++ int ch[5], tw[5], wq[5], ax[5];
++ for (unsigned i = 0; i < 5; i++) { ch[i] = V(e); tw[i] = V128(e); wq[i] = V128(e); ax[i] = V64(e); }
++ if (e->err) return -1;
++ ldcu64(e, udesc, 0x358);
++ s2r(e, c, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, hk, BW_SR_CTAID_Y);
++ ldc64(e, pqkv, OMEGA_GDN_PREP_ARG_QKV); ldc64(e, ppar, OMEGA_GDN_PREP_ARG_PAR); ldc64(e, ptail, OMEGA_GDN_PREP_ARG_TAIL);
++ ldc64(e, prec, OMEGA_GDN_PREP_ARG_REC); ldc64(e, pab, OMEGA_GDN_PREP_ARG_AB);
++ ldc32(e, T, OMEGA_GDN_PREP_ARG_T); ldc32(e, insc, OMEGA_GDN_PREP_ARG_IN_SCALE);
++ movi(e, one, 1);
++ shr(e, warp, c, 5);
++ andi(e, R(lane), R(c), 31u);
++ imadi(e, R(t0), R(cx), OMEGA_GDN_PREP_TOKENS, NOREG);
++ /* n = min(16, T - t0) >= 1 */
++ imadi(e, R(n), R(t0), 0xffffffffu, T);
++ movi(e, t1, OMEGA_GDN_PREP_TOKENS);
++ isetp_ge_u32(e, t1, n);
++ { int b = bra_if_p0_fwd(e); movi(e, n, OMEGA_GDN_PREP_TOKENS); patch_bra(e, b, here(e)); }
++ /* channels: q hk*128 + c, k 2048 + hk*128 + c, v_i 4096 + (3 hk + i)*128 + c */
++ imadi(e, R(ch[0]), R(hk), OMEGA_GDN_HEAD_DIM, c);
++ imadi(e, R(ch[1]), R(one), 2048u, ch[0]);
++ imadi(e, R(ch[2]), R(hk), 3u * OMEGA_GDN_HEAD_DIM, c); imadi(e, R(ch[2]), R(one), 4096u, ch[2]);
++ imadi(e, R(ch[3]), R(one), OMEGA_GDN_HEAD_DIM, ch[2]);
++ imadi(e, R(ch[4]), R(one), 2u * OMEGA_GDN_HEAD_DIM, ch[2]);
++ /* taps */
++ {
++ int aq = V64(e), av = V64(e);
++ if (e->err) return -1;
++ imadw(e, aq, ch[0], 16u, ppar); imadw(e, av, ch[2], 16u, ppar);
++ ldg_wide(e, BW_IR_LDG_E_128, tw[0], aq, 0, udesc, 1);
++ ldg_wide(e, BW_IR_LDG_E_128, tw[1], aq, 2048u * 16u, udesc, 0);
++ for (unsigned i = 0; i < 3; i++) ldg_wide(e, BW_IR_LDG_E_128, tw[2 + i], av, i * OMEGA_GDN_HEAD_DIM * 16u, udesc, 0);
++ }
++ /* the window {x_{t0-3}, x_{t0-2}, x_{t0-1}}: CTA 0 from the tail, the others from the rows before t0 (uniform) */
++ isetp_ge_u32(e, cx, one);
++ const int b_rows = bra_if_p0_fwd(e);
++ {
++ int at = V64(e);
++ if (e->err) return -1;
++ imadi(e, R(t1), R(hk), 3u * OMEGA_GDN_TAIL_HEAD_BYTES, NOREG);
++ imadw(e, at, t1, 1u, ptail); imadw(e, at, c, 16u, at);
++ ldg_wide(e, BW_IR_LDG_E_128, wq[0], at, 0, udesc, 1);
++ ldg_wide(e, BW_IR_LDG_E_128, wq[1], at, OMEGA_GDN_HEAD_DIM * 16u, udesc, 0);
++ for (unsigned i = 0; i < 3; i++) ldg_wide(e, BW_IR_LDG_E_128, wq[2 + i], at, i * OMEGA_GDN_TAIL_HEAD_BYTES + 256u * 16u, udesc, 0);
++ }
++ const int b_join = bra_fwd(e);
++ patch_bra(e, b_rows, here(e));
++ {
++ int base = V64(e), a[5][3];
++ for (unsigned i = 0; i < 5; i++) for (unsigned r = 0; r < 3; r++) a[i][r] = V64(e);
++ if (e->err) return -1;
++ imadi(e, R(t1), R(one), 0xfffffffdu, t0); /* t0 - 3 (t0 >= 16 here) */
++ imadw(e, base, t1, ROW, pqkv);
++ for (unsigned i = 0; i < 5; i++) {
++ imadw(e, a[i][0], ch[i], 4u, base);
++ imadw(e, a[i][1], one, ROW, a[i][0]); imadw(e, a[i][2], one, ROW, a[i][1]);
++ }
++ for (unsigned i = 0; i < 5; i++) for (unsigned r = 0; r < 3; r++) {
++ BlackwellIRInsn in = I0(BW_IR_LDG_E); in.dst_vreg = wq[i]; in.dst_subreg = (uint8_t)r; in.src1_vreg = a[i][r]; in.ureg = udesc;
++ emit(e, in, (i == 0 && r == 0) ? K_VAR : K_VAR_MORE);
++ }
++ for (unsigned i = 0; i < 5; i++) for (unsigned r = 0; r < 3; r++) fmul(e, S(wq[i], r), S(wq[i], r), R(insc));
++ }
++ patch_bra(e, b_join, here(e));
++
++ /* per-thread addresses at token t0 */
++ int idx = V(e), arec = V64(e), asc = V64(e), aab = V64(e), scq = V64(e);
++ int cnl2e = V(e), fone = V(e), el2 = V(e), cqs = V(e), c3 = V(e), zero = V(e), raq = V(e), rak = V(e);
++ if (e->err) return -1;
++ {
++ int base = V64(e);
++ if (e->err) return -1;
++ imadw(e, base, t0, ROW, pqkv);
++ for (unsigned i = 0; i < 5; i++) imadw(e, ax[i], ch[i], 4u, base);
++ }
++ imadi(e, R(idx), R(t0), OMEGA_GDN_VALUE_HEADS, NOREG); imadi(e, R(idx), R(hk), 3u, idx);
++ imadw(e, arec, idx, OMEGA_GDN_REC_BYTES, prec); imadw(e, arec, c, 4u, arec);
++ imadi(e, R(t1), R(c), 1u, idx); /* the scalar threads' head index t0*48 + 3hk + c */
++ imadw(e, asc, t1, OMEGA_GDN_REC_BYTES, prec);
++ imadw(e, aab, t1, 8u, pab);
++ movi(e, c3, 3u);
++ isetp_ge_u32(e, c, c3);
++ {
++ Region r = region_skip_if_p0(e);
++ int as = V64(e);
++ if (e->err) return -1;
++ imadi(e, R(t1), R(hk), 3u, c);
++ imadw(e, as, t1, 8u, ppar);
++ ldg_wide(e, BW_IR_LDG_E_64, scq, as, OMEGA_GDN_PAR_SCAL, udesc, 1);
++ region_end(e, r);
++ }
++ movi(e, cnl2e, fbits(-1.44269504f )); movi(e, fone, fbits(1.0f)); movi(e, cqs, fbits(qs));
++ ldc32(e, el2, OMEGA_GDN_PREP_ARG_EPS_L2);
++ movi(e, zero, 0u);
++ imadi(e, R(raq), R(warp), 4u, NOREG); imadi(e, R(rak), R(one), 16u, raq);
++
++ const int L = here(e);
++ loop_begin(e);
++ {
++ int x[5], acc[5], sq = V(e), sk = V(e), ts = V(e), rq = V128(e), rk = V128(e), nq = V(e), nk = V(e);
++ for (unsigned i = 0; i < 5; i++) { x[i] = V(e); acc[i] = V(e); }
++ if (e->err) return -1;
++ for (unsigned i = 0; i < 5; i++) ldg32(e, x[i], ax[i], udesc, i == 0);
++ for (unsigned i = 0; i < 5; i++) {
++ fmul(e, R(x[i]), R(x[i]), R(insc));
++ fmul(e, R(acc[i]), S(tw[i], 0), S(wq[i], 0)); ffma(e, R(acc[i]), S(tw[i], 1), S(wq[i], 1), R(acc[i]));
++ ffma(e, R(acc[i]), S(tw[i], 2), S(wq[i], 2), R(acc[i])); ffma(e, R(acc[i]), S(tw[i], 3), R(x[i]), R(acc[i]));
++ silu(e, R(acc[i]), t1, cnl2e, fone);
++ mov(e, S(wq[i], 0), S(wq[i], 1)); mov(e, S(wq[i], 1), S(wq[i], 2)); mov(e, S(wq[i], 2), R(x[i]));
++ }
++ /* l2 norms of q and k across the 4 warps (the decode order: warp sums, then partials 0 + 1 + 2 + 3) */
++ fmul(e, R(sq), R(acc[0]), R(acc[0])); warp_sum(e, sq, ts); lane0_sts(e, lane, one, raq, R(sq));
++ fmul(e, R(sk), R(acc[1]), R(acc[1])); warp_sum(e, sk, ts); lane0_sts(e, lane, one, rak, R(sk));
++ bar_sync(e);
++ lds128(e, rq, zero, 0, 1); lds128(e, rk, zero, 16, 0);
++ fadd(e, R(nq), S(rq, 0), S(rq, 1)); fadd(e, R(nq), R(nq), S(rq, 2)); fadd(e, R(nq), R(nq), S(rq, 3)); fadd(e, R(nq), R(nq), R(el2));
++ fadd(e, R(nk), S(rk, 0), S(rk, 1)); fadd(e, R(nk), R(nk), S(rk, 2)); fadd(e, R(nk), R(nk), S(rk, 3)); fadd(e, R(nk), R(nk), R(el2));
++ mufu(e, BW_IR_MUFU_RSQ, R(nq), R(nq)); mufu(e, BW_IR_MUFU_RSQ, R(nk), R(nk));
++ fmul(e, R(acc[0]), R(acc[0]), R(nq)); fmul(e, R(acc[0]), R(acc[0]), R(cqs));
++ fmul(e, R(acc[1]), R(acc[1]), R(nk));
++ for (unsigned i = 0; i < 3; i++) {
++ stg(e, arec, i * OMEGA_GDN_REC_BYTES, R(acc[0]), udesc);
++ stg(e, arec, i * OMEGA_GDN_REC_BYTES + OMEGA_GDN_REC_K, R(acc[1]), udesc);
++ stg(e, arec, i * OMEGA_GDN_REC_BYTES + OMEGA_GDN_REC_V, R(acc[2 + i]), udesc);
++ }
++ /* threads 0..2: decay and beta of value head 3 hk + c */
++ isetp_ge_u32(e, c, c3);
++ {
++ Region r = region_skip_if_p0(e);
++ int abq = V64(e), dec = V(e), beta = V(e);
++ if (e->err) return -1;
++ ldg_wide(e, BW_IR_LDG_E_64, abq, aab, 0, udesc, 1);
++ gdn_scalars(e, MUT, abq, scq, dec, beta, t1, cnl2e, fone);
++ stg(e, asc, OMEGA_GDN_REC_SC, R(dec), udesc); stg(e, asc, OMEGA_GDN_REC_SC + 4u, R(beta), udesc);
++ region_end(e, r);
++ }
++ bar_sync(e); /* every warp has read the partials before the next token writes them */
++ for (unsigned i = 0; i < 5; i++) imadw(e, ax[i], one, ROW, ax[i]);
++ imadw(e, arec, one, REC_ROW, arec); imadw(e, asc, one, REC_ROW, asc);
++ imadw(e, aab, one, OMEGA_GDN_VALUE_HEADS * 8u, aab);
++ imadi(e, R(n), R(one), 0xffffffffu, n);
++ isetp_ge_u32(e, n, one);
++ bra_if_p0(e, L);
++ }
++ loop_end(e);
++ tail(e);
++ if (e->err) return -1;
++ return omega_bw_regalloc_solve(prog);
++}
++
++int omega_blackwell_codegen_gdn_scan_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog) {
++ if (!spec || !prog || spec->mutant < 0 || spec->mutant > 4) return -1;
++ const int MUT = spec->mutant;
++ const uint32_t ROW = OMEGA_GDN_CONV_DIM * 4u, CHUNK_ROW = OMEGA_GDN_SCAN_CHUNK * OMEGA_GDN_VALUE_HEADS * OMEGA_GDN_REC_BYTES;
++ omega_bw_ir_init(prog);
++ Em em; memset(&em, 0, sizeof em); em.p = prog; Em *e = &em;
++
++ int udesc = UV64(e);
++ int tid = V(e), cx = V(e), cy = V(e), h = V(e), warp = V(e), lane = V(e), one = V(e), cq = V(e), rg = V(e), c0 = V(e);
++ int T = V(e), tm1 = V(e), t = V(e), t1 = V(e), sl = V(e), srec = V(e);
++ int ps = V64(e), sa = V64(e), prec = V64(e), grec = V64(e), gsc = V64(e), po = V64(e), sq[16];
++ for (unsigned r = 0; r < 16; r++) sq[r] = V128(e);
++ if (e->err) return -1;
++ ldcu64(e, udesc, 0x358);
++ s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y);
++ ldc64(e, ps, OMEGA_GDN_SCAN_ARG_STATE); ldc64(e, prec, OMEGA_GDN_SCAN_ARG_REC); ldc64(e, po, OMEGA_GDN_SCAN_ARG_O);
++ ldc32(e, T, OMEGA_GDN_SCAN_ARG_T);
++ movi(e, one, 1);
++ imadi(e, R(h), R(cy), 3u, cx);
++ shr(e, warp, tid, 5);
++ andi(e, R(lane), R(tid), 31u);
++ andi(e, R(cq), R(lane), 3u);
++ shr(e, rg, lane, 2);
++ /* this thread's block of S: rows 16 rg .. +15, columns c0 = 16 warp + 4 cq .. +3 */
++ imadi(e, R(c0), R(warp), 16u, NOREG); imadi(e, R(c0), R(cq), 4u, c0);
++ imadi(e, R(t1), R(h), OMEGA_GDN_STATE_HEAD_BYTES, NOREG);
++ imadi(e, R(t1), R(rg), 16u * OMEGA_GDN_HEAD_DIM * 4u, t1);
++ imadi(e, R(t1), R(c0), 4u, t1);
++ imadw(e, sa, t1, 1u, ps);
++ for (unsigned r = 0; r < 16; r++) ldg_wide(e, BW_IR_LDG_E_128, sq[r], sa, r * OMEGA_GDN_HEAD_DIM * 4u, udesc, r == 0);
++ imadi(e, R(tm1), R(one), 0xffffffffu, T);
++ /* the record copy: warp w brings token tc + w into shared slot w */
++ imadi(e, R(t1), R(warp), OMEGA_GDN_VALUE_HEADS, h);
++ imadw(e, gsc, t1, OMEGA_GDN_REC_BYTES, prec);
++ imadw(e, grec, lane, 16u, gsc);
++ imadi(e, R(srec), R(warp), OMEGA_GDN_REC_BYTES, NOREG);
++ imadi(e, R(sl), R(lane), 16u, srec);
++ /* o row of (token 0, head h), columns c0 .. c0 + 3 */
++ imadi(e, R(t1), R(h), OMEGA_GDN_HEAD_DIM, c0);
++ imadw(e, po, t1, 4u, po);
++ movrz(e, R(t));
++
++ const int L = here(e);
++ loop_begin(e);
++ {
++ int tm = V(e), sb = V(e), ak = V(e), av = V(e), scq = V128(e), vq = V128(e), ts = V(e), kv[4], dl[4], p[4];
++ for (unsigned j = 0; j < 4; j++) { kv[j] = V(e); dl[j] = V(e); p[j] = V(e); }
++ if (e->err) return -1;
++ andi(e, R(tm), R(t), OMEGA_GDN_SCAN_CHUNK - 1u);
++ isetp_ge_u32(e, tm, one);
++ const int b_copy = bra_if_p0_fwd(e);
++ {
++ int rr[4], a = V(e);
++ for (unsigned k = 0; k < 4; k++) rr[k] = V128(e);
++ if (e->err) return -1;
++ bar_sync(e); /* every warp is done with the last chunk */
++ for (unsigned k = 0; k < 3; k++) ldg_wide(e, BW_IR_LDG_E_128, rr[k], grec, k * 512u, udesc, k == 0);
++ ldg_wide(e, BW_IR_LDG_E_128, rr[3], gsc, OMEGA_GDN_REC_SC, udesc, 0);
++ for (unsigned k = 0; k < 3; k++) for (unsigned j = 0; j < 4; j++) {
++ if (k == 0 && j == 0) sts32(e, sl, S(rr[0], 0));
++ else { imadi(e, R(a), R(one), k * 512u + j * 4u, sl); sts32(e, a, S(rr[k], j)); }
++ }
++ for (unsigned j = 0; j < 4; j++) { imadi(e, R(a), R(one), OMEGA_GDN_REC_SC + j * 4u, srec); sts32(e, a, S(rr[3], j)); }
++ bar_sync(e);
++ imadw(e, grec, one, CHUNK_ROW, grec); imadw(e, gsc, one, CHUNK_ROW, gsc);
++ }
++ patch_bra(e, b_copy, here(e));
++ imadi(e, R(sb), R(tm), OMEGA_GDN_REC_BYTES, NOREG);
++ imadi(e, R(ak), R(rg), 16u * 4u, sb);
++ imadi(e, R(av), R(c0), 4u, sb);
++ lds128(e, scq, sb, OMEGA_GDN_REC_SC, 1);
++ for (unsigned r = 0; r < 16; r++) for (unsigned j = 0; j < 4; j++) fmul(e, S(sq[r], j), S(sq[r], j), S(scq, 0));
++ /* kv_j = sum over 128 rows of S[r][j] k[r]: 16 rows here, then lanes ^ 4, 8, 16 (the 8 row groups) */
++ for (unsigned i = 0; i < 4; i++) {
++ int kq = V128(e);
++ if (e->err) return -1;
++ lds128(e, kq, ak, OMEGA_GDN_REC_K + i * 16u, 1);
++ for (unsigned r = 4 * i; r < 4 * i + 4; r++) for (unsigned j = 0; j < 4; j++) {
++ if (r == 0) fmul(e, R(kv[j]), S(sq[0], j), S(kq, 0));
++ else ffma(e, R(kv[j]), S(sq[r], j), S(kq, r % 4), R(kv[j]));
++ }
++ }
++ for (unsigned j = 0; j < 4; j++) bfly_sum(e, kv[j], ts, 4u, 16u);
++ lds128(e, vq, av, OMEGA_GDN_REC_V, 1);
++ 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]), S(scq, 1)); }
++ for (unsigned i = 0; i < 4; i++) {
++ int kq = V128(e);
++ if (e->err) return -1;
++ lds128(e, kq, ak, OMEGA_GDN_REC_K + i * 16u, 1);
++ for (unsigned r = 4 * i; r < 4 * i + 4; r++) for (unsigned j = 0; j < 4; j++) ffma(e, S(sq[r], j), S(kq, r % 4), R(dl[j]), S(sq[r], j));
++ }
++ for (unsigned i = 0; i < 4; i++) {
++ int qq = V128(e);
++ if (e->err) return -1;
++ lds128(e, qq, ak, i * 16u, 1);
++ for (unsigned r = 4 * i; r < 4 * i + 4; r++) for (unsigned j = 0; j < 4; j++) {
++ if (r == 0) fmul(e, R(p[j]), S(sq[0], j), S(qq, 0));
++ else ffma(e, R(p[j]), S(sq[r], j), S(qq, r % 4), R(p[j]));
++ }
++ }
++ for (unsigned j = 0; j < 4; j++) bfly_sum(e, p[j], ts, 4u, 16u);
++ isetp_ge_u32(e, rg, one);
++ {
++ Region r = region_skip_if_p0(e);
++ for (unsigned j = 0; j < 4; j++) stg(e, po, j * 4u, R(p[j]), udesc);
++ region_end(e, r);
++ }
++ imadw(e, po, one, OMEGA_GDN_V_DIM * 4u, po);
++ imadi(e, R(t), R(one), 1u, t);
++ isetp_ge_u32(e, tm1, t);
++ bra_if_p0(e, L);
++ }
++ loop_end(e);
++ 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);
++
++ /* the new tail: qkv rows T-3 .. T-1 (times in_scale) of slot A (tid) and slot V (256 + (tid & 127)), as decode */
++ if (MUT != 4) {
++ int half = V(e), cc = V(e), gA = V(e), gV = V(e), insc = V(e), pqkv = V64(e), ptail = V64(e), base = V64(e);
++ int atA = V64(e), atV = V64(e), aA[3], aV[3], xA[3], xV[3];
++ for (unsigned r = 0; r < 3; r++) { aA[r] = V64(e); aV[r] = V64(e); xA[r] = V(e); xV[r] = V(e); }
++ if (e->err) return -1;
++ ldc64(e, pqkv, OMEGA_GDN_SCAN_ARG_QKV); ldc64(e, ptail, OMEGA_GDN_SCAN_ARG_TAIL); ldc32(e, insc, OMEGA_GDN_SCAN_ARG_IN_SCALE);
++ shr(e, half, tid, 7);
++ andi(e, R(cc), R(tid), 127u);
++ imadi(e, R(gA), R(cy), OMEGA_GDN_HEAD_DIM, tid); imadi(e, R(gA), R(half), 2048u - 128u, gA);
++ imadi(e, R(gV), R(h), OMEGA_GDN_HEAD_DIM, cc); imadi(e, R(gV), R(one), 2u * OMEGA_GDN_KEY_HEADS * OMEGA_GDN_HEAD_DIM, gV);
++ imadi(e, R(t1), R(one), 0xfffffffdu, T);
++ imadw(e, base, t1, ROW, pqkv);
++ imadw(e, aA[0], gA, 4u, base); imadw(e, aV[0], gV, 4u, base);
++ for (unsigned r = 1; r < 3; r++) { imadw(e, aA[r], one, ROW, aA[r - 1]); imadw(e, aV[r], one, ROW, aV[r - 1]); }
++ for (unsigned r = 0; r < 3; r++) { ldg32(e, xA[r], aA[r], udesc, r == 0); ldg32(e, xV[r], aV[r], udesc, 0); }
++ for (unsigned r = 0; r < 3; r++) { fmul(e, R(xA[r]), R(xA[r]), R(insc)); fmul(e, R(xV[r]), R(xV[r]), R(insc)); }
++ imadw(e, atA, tid, 16u, ptail); imadw(e, atA, h, OMEGA_GDN_TAIL_HEAD_BYTES, atA);
++ imadi(e, R(t1), R(one), 256u, cc);
++ imadw(e, atV, t1, 16u, ptail); imadw(e, atV, h, OMEGA_GDN_TAIL_HEAD_BYTES, atV);
++ for (unsigned r = 0; r < 3; r++) { stg(e, atA, 4u * r, R(xA[r]), udesc); stg(e, atV, 4u * r, R(xV[r]), udesc); }
++ }
++ tail(e);
++ if (e->err) return -1;
++ return omega_bw_regalloc_solve(prog);
++}
++
++int omega_blackwell_codegen_gdn_post_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog) {
++ if (!spec || !prog || spec->mutant < 0 || spec->mutant > 4) return -1;
++ const int MUT = spec->mutant;
++ omega_bw_ir_init(prog);
++ Em em; memset(&em, 0, sizeof em); em.p = prog; Em *e = &em;
++
++ int udesc = UV64(e);
++ int tid = V(e), t = V(e), cy = V(e), warp = V(e), lane = V(e), idx = V(e), ss = V(e), ts = V(e), g = V(e), t1 = V(e);
++ int en = V(e), osc = V(e), insc = V(e), cinv = V(e), cnl2e = V(e), fone = V(e);
++ int po = V64(e), pz = V64(e), ppar = V64(e), pout = V64(e), ao = V64(e), az = V64(e), an = V64(e), ay = V64(e);
++ int oq = V128(e), zq = V128(e), nq = V128(e);
++ if (e->err) return -1;
++ ldcu64(e, udesc, 0x358);
++ s2r(e, tid, BW_SR_TID_X); s2r(e, t, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y);
++ ldc64(e, po, OMEGA_GDN_POST_ARG_O); ldc64(e, pz, OMEGA_GDN_POST_ARG_Z); ldc64(e, ppar, OMEGA_GDN_POST_ARG_PAR);
++ ldc64(e, pout, OMEGA_GDN_POST_ARG_OUT);
++ ldc32(e, en, OMEGA_GDN_POST_ARG_EPS_NORM); ldc32(e, insc, OMEGA_GDN_POST_ARG_IN_SCALE); ldc32(e, osc, OMEGA_GDN_POST_ARG_OUT_SCALE);
++ shr(e, warp, tid, 5);
++ andi(e, R(lane), R(tid), 31u);
++ /* row (token t, head 8 cy + warp): o, z and out share the [T][48][128] layout */
++ imadi(e, R(idx), R(cy), 8u, warp);
++ imadi(e, R(idx), R(t), OMEGA_GDN_VALUE_HEADS, idx);
++ imadw(e, ao, idx, OMEGA_GDN_HEAD_DIM * 4u, po); imadw(e, ao, lane, 16u, ao);
++ imadw(e, az, idx, OMEGA_GDN_HEAD_DIM * 4u, pz); imadw(e, az, lane, 16u, az);
++ imadw(e, ay, idx, OMEGA_GDN_HEAD_DIM * 4u, pout); imadw(e, ay, lane, 16u, ay);
++ imadw(e, an, lane, 16u, ppar);
++ ldg_wide(e, BW_IR_LDG_E_128, oq, ao, 0, udesc, 1);
++ ldg_wide(e, BW_IR_LDG_E_128, zq, az, 0, udesc, 0);
++ ldg_wide(e, BW_IR_LDG_E_128, nq, an, OMEGA_GDN_PAR_NORM, udesc, 0);
++ fmul(e, R(ss), S(oq, 0), S(oq, 0));
++ for (unsigned j = 1; j < 4; j++) ffma(e, R(ss), S(oq, j), S(oq, j), R(ss));
++ bfly_sum(e, ss, ts, 1u, 16u);
++ movi(e, cinv, fbits(1.0f / 128.0f));
++ ffma(e, R(ss), R(ss), R(cinv), R(en));
++ mufu(e, BW_IR_MUFU_RSQ, R(ss), R(ss));
++ movi(e, cnl2e, fbits(-1.44269504f )); movi(e, fone, fbits(1.0f));
++ for (unsigned j = 0; j < 4; j++) {
++ fmul(e, R(g), S(zq, j), R(insc));
++ if (MUT != 3) silu(e, R(g), t1, cnl2e, fone);
++ fmul(e, S(oq, j), S(oq, j), R(ss)); fmul(e, S(oq, j), S(oq, j), S(nq, j));
++ fmul(e, S(oq, j), S(oq, j), R(g)); fmul(e, S(oq, j), S(oq, j), R(osc));
++ }
++ for (unsigned j = 0; j < 4; j++) stg(e, ay, j * 4u, S(oq, j), udesc);
++ tail(e);
++ if (e->err) return -1;
++ return omega_bw_regalloc_solve(prog);
++}
++
++/* number of SHFL.BFLY placeholders in a program (what omega_tc_patch_code must report) */
++uint32_t omega_gdn_bfly_count(const BlackwellIRProgram *prog) {
++ uint32_t n = 0;
++ for (size_t i = 0; i < prog->count; i++)
++ if (prog->insns[i].op == BW_IR_SHFL_DOWN && (prog->insns[i].imm & OMEGA_TC_MARK_BFLY)) n++;
++ return n;
++}
++
++int omega_gdn_pf_prog_stats(unsigned kind, const OmegaGdnSpec *spec, uint32_t *insns, uint32_t *gprs) {
++ static BlackwellIRProgram prog;
++ int rc = kind == 0 ? omega_blackwell_codegen_gdn_prep_prog(spec, &prog)
++ : kind == 1 ? omega_blackwell_codegen_gdn_scan_prog(spec, &prog)
++ : kind == 2 ? omega_blackwell_codegen_gdn_post_prog(spec, &prog) : -1;
++ if (rc != 0) return -1;
++ if (insns) *insns = (uint32_t)prog.count;
++ if (gprs) *gprs = prog.regalloc.peak_gpr_usage;
++ return 0;
++}
+diff --git a/src/omega_blackwell_codegen_gdn.h b/src/omega_blackwell_codegen_gdn.h
+index 10b7079d ..8015cc83 100644
+--- a/src/omega_blackwell_codegen_gdn.h
++++ b/src/omega_blackwell_codegen_gdn.h
+@@ -89,4 +89,61 @@ int omega_blackwell_codegen_gdn_prog(const OmegaGdnSpec *spec, BlackwellIRProgra
+ /* Instruction and peak-register counts (host, no device). */
+ int omega_gdn_prog_stats(const OmegaGdnSpec *spec, uint32_t *insns, uint32_t *gprs);
+
++/* ---- prefill (omega#543, 2026-10-18): three launches per layer over T >= 3 new tokens, the same tail and state ----
++ * 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
++ * of the three value heads), sliding each window in registers over 16 tokens (the CTA starting at token 0 takes
++ * its window from the layer's tail, the others from the qkv rows before it), then SiLU, the q/k l2 norms (two
++ * barriers per token) and, threads 0..2, the scalars of the three value heads. It writes one record per (token,
++ * value head): q^ | k^ | v | {decay, beta, 0, 0}. It reads the tail and never writes it.
++ * scan grid 3 x 16 (one CTA per value head), 256 threads. Thread (warp w, lane L) holds S rows 16 (L >> 2) .. +15,
++ * columns 16 w + 4 (L & 3) .. +3 in registers for the whole prompt. Per 8-token chunk every warp copies one
++ * token's record into shared memory (two barriers); per token kv and o are summed across the 8 row groups by
++ * SHFL.BFLY (lanes ^ 4, 8, 16; a butterfly gives every lane the same bits), no barrier. It writes o (before the
++ * norm) per token, the state at the end and the new tail (the last three qkv rows).
++ * post grid (T, 6), 256 threads: warp w normalises (token, head 8 ctaid.y + w): gated RMSNorm, silu(z), out_scale.
++ * The mutants keep the decode meanings: 1 and 2 act in prep, 3 in post, 4 in scan. */
++#define OMEGA_GDN_REC_BYTES 1552u
++#define OMEGA_GDN_REC_K 512u
++#define OMEGA_GDN_REC_V 1024u
++#define OMEGA_GDN_REC_SC 1536u
++#define OMEGA_GDN_PF_MIN_T 3u
++#define OMEGA_GDN_PREP_TOKENS 16u
++#define OMEGA_GDN_PREP_THREADS 128u
++#define OMEGA_GDN_PREP_SHARED_BYTES 32u /* per-warp sums of squares: q [4] | k [4] */
++#define OMEGA_GDN_PREP_ARG_QKV 0x380u /* f32 [T][10240] (times in_scale) */
++#define OMEGA_GDN_PREP_ARG_AB 0x388u /* f32 [T][48][2] */
++#define OMEGA_GDN_PREP_ARG_PAR 0x390u
++#define OMEGA_GDN_PREP_ARG_TAIL 0x398u
++#define OMEGA_GDN_PREP_ARG_REC 0x3a0u /* [T rounded up to 8][48] records */
++#define OMEGA_GDN_PREP_ARG_T 0x3a8u /* u32 */
++#define OMEGA_GDN_PREP_ARG_EPS_L2 0x3acu
++#define OMEGA_GDN_PREP_ARG_IN_SCALE 0x3b0u
++#define OMEGA_GDN_PREP_ARGS_WORDS 13u
++#define OMEGA_GDN_SCAN_CHUNK 8u
++#define OMEGA_GDN_SCAN_SHARED_BYTES (OMEGA_GDN_SCAN_CHUNK * OMEGA_GDN_REC_BYTES) /* 12416 */
++#define OMEGA_GDN_SCAN_ARG_REC 0x380u
++#define OMEGA_GDN_SCAN_ARG_QKV 0x388u
++#define OMEGA_GDN_SCAN_ARG_TAIL 0x390u
++#define OMEGA_GDN_SCAN_ARG_STATE 0x398u
++#define OMEGA_GDN_SCAN_ARG_O 0x3a0u /* f32 [T][48][128] */
++#define OMEGA_GDN_SCAN_ARG_T 0x3a8u
++#define OMEGA_GDN_SCAN_ARG_IN_SCALE 0x3acu
++#define OMEGA_GDN_SCAN_ARGS_WORDS 12u
++#define OMEGA_GDN_POST_ARG_O 0x380u
++#define OMEGA_GDN_POST_ARG_Z 0x388u /* f32 [T][6144] (times in_scale) */
++#define OMEGA_GDN_POST_ARG_PAR 0x390u
++#define OMEGA_GDN_POST_ARG_OUT 0x398u /* f32 [T][6144] */
++#define OMEGA_GDN_POST_ARG_EPS_NORM 0x3a0u
++#define OMEGA_GDN_POST_ARG_IN_SCALE 0x3a4u
++#define OMEGA_GDN_POST_ARG_OUT_SCALE 0x3a8u
++#define OMEGA_GDN_POST_ARGS_WORDS 11u
++#define OMEGA_GDN_POST_GRID_Y 6u
++
++int omega_blackwell_codegen_gdn_prep_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog);
++int omega_blackwell_codegen_gdn_scan_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog);
++int omega_blackwell_codegen_gdn_post_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog);
++/* SHFL.BFLY placeholders in a built program; prefill stats: kind 0 prep, 1 scan, 2 post. */
++uint32_t omega_gdn_bfly_count(const BlackwellIRProgram *prog);
++int omega_gdn_pf_prog_stats(unsigned kind, const OmegaGdnSpec *spec, uint32_t *insns, uint32_t *gprs);
++
+ #endif
+diff --git a/src/omega_gpu_gdn_api.c b/src/omega_gpu_gdn_api.c
+index 6df97a82 ..128d7f98 100644
+--- a/src/omega_gpu_gdn_api.c
++++ b/src/omega_gpu_gdn_api.c
+@@ -6,6 +6,7 @@
+ #include "omega_gpu_gdn_api.h"
+ #include "omega_gpu_session.h"
+ #include "omega_gpu_cmdlist.h"
++#include "omega_blackwell_codegen_tc.h"
+ #include <math.h>
+ #include <stdbool.h>
+ #include <string.h>
+@@ -26,6 +27,7 @@ struct OmegaGpuGdn {
+ OmegaGpuMem tail; /* [layers][OMEGA_GDN_TAIL_BYTES] */
+ OmegaGpuMem state; /* [layers][OMEGA_GDN_STATE_BYTES] */
+ OmegaGpuMem lb; /* launch buffer */
++ OmegaGpuScratch pf; /* prefill scratch (see pf_layout) */
+ };
+
+ typedef struct {
+@@ -36,15 +38,20 @@ typedef struct {
+ uint32_t insns;
+ } Slot;
+
++/* the resident kernels: decode, prefill prep, scan, post */
++enum { KK_DECODE, KK_PREP, KK_SCAN, KK_POST, KK_N };
++static const uint32_t kk_threads[KK_N] = { OMEGA_GDN_THREADS, OMEGA_GDN_PREP_THREADS, OMEGA_GDN_THREADS, OMEGA_GDN_THREADS };
+ static struct {
+ bool hooked;
+- Slot slot;
++ Slot slot[KK_N];
+ int mutant;
+ } g_k;
+ static int g_rec; /* 1 while a record call runs (lock held): kernel_for must not evict */
+
+-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); }
+-static void on_session_close(void) { memset(&g_k.slot, 0, sizeof g_k.slot); }
++static void slot_clear_locked(void) {
++ for (unsigned k = 0; k < KK_N; k++) { if (g_k.slot[k].valid) omega_gpu_session_free(&g_k.slot[k].code); memset(&g_k.slot[k], 0, sizeof g_k.slot[k]); }
++}
++static void on_session_close(void) { memset(g_k.slot, 0, sizeof g_k.slot); }
+ static OmegaGpuSession *dev_open_locked(void) {
+ if (!g_k.hooked) { (void)omega_gpu_session_on_close(on_session_close); g_k.hooked = true; }
+ return omega_gpu_session_open();
+@@ -142,6 +149,7 @@ void omega_gpu_gdn_free(OmegaGpuGdn *g) {
+ if (g->tail.cpu) omega_gpu_session_free(&g->tail);
+ if (g->state.cpu) omega_gpu_session_free(&g->state);
+ if (g->lb.cpu) omega_gpu_session_free(&g->lb);
++ omega_gpu_session_scratch_free(&g->pf);
+ omega_gpu_session_unlock();
+ omega_gpu_host_free(g);
+ }
+@@ -192,24 +200,37 @@ int omega_gpu_gdn_get_state(OmegaGpuGdn *g, uint32_t layer, float *tail, float *
+ }
+
+ /* ---- kernel ---- */
+-static int kernel_for(const OmegaGdnSpec *spec, Slot **out, bool *hit) {
+- Slot *s = &g_k.slot;
++static int kernel_build(unsigned kind, const OmegaGdnSpec *spec, BlackwellIRProgram *prog) {
++ switch (kind) {
++ case KK_DECODE: return omega_blackwell_codegen_gdn_prog(spec, prog);
++ case KK_PREP: return omega_blackwell_codegen_gdn_prep_prog(spec, prog);
++ case KK_SCAN: return omega_blackwell_codegen_gdn_scan_prog(spec, prog);
++ case KK_POST: return omega_blackwell_codegen_gdn_post_prog(spec, prog);
++ default: return -1;
++ }
++}
++static int kernel_for(unsigned kind, const OmegaGdnSpec *spec, Slot **out, bool *hit) {
++ Slot *s = &g_k.slot[kind];
+ if (s->valid && memcmp(&s->spec, spec, sizeof *spec) == 0) { *out = s; *hit = true; return OMEGA_GPU_GDN_OK; }
+ 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; }
+- slot_clear_locked();
++ if (s->valid) omega_gpu_session_free(&s->code);
++ memset(s, 0, sizeof *s);
+ BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog);
+ if (!prog) { omega_gpu_session_set_error("gdn codegen: host memory"); return OMEGA_GPU_GDN_CODEGEN_FAIL; }
+- int cg = omega_blackwell_codegen_gdn_prog(spec, prog);
++ int cg = kernel_build(kind, spec, prog);
+ size_t need = cg == 0 ? omega_bw_program_code_bytes(prog) : 0;
+ 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; }
+- 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; }
+- const uint32_t insns = (uint32_t)prog->count;
++ if ((uint64_t)kk_threads[kind] * 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; }
++ const uint32_t insns = (uint32_t)prog->count, expect = omega_gdn_bfly_count(prog);
+ OmegaGpuMem code;
+ if (omega_gpu_session_alloc_code(need, &code) != 0) { omega_gpu_host_free(prog); return OMEGA_GPU_GDN_CHIP_FAIL; }
+ OmegaBlackwellKernel kernel;
+ cg = omega_blackwell_kernel_encode_into(prog, code.cpu, need, &kernel);
++ /* SHFL.BFLY placeholders become real butterflies (omega_blackwell_codegen_tc.h); the count must match */
++ const int np = cg == 0 ? omega_tc_patch_code(prog, code.cpu, need) : -1;
+ omega_gpu_host_free(prog);
+ 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; }
++ if (np < 0 || (uint32_t)np != expect) { omega_gpu_session_free(&code); omega_gpu_session_set_error("gdn: butterfly patch count mismatch"); return OMEGA_GPU_GDN_CODEGEN_FAIL; }
+ if (omega_gpu_session_sync_to_gpu(&code) != 0) { omega_gpu_session_free(&code); return OMEGA_GPU_GDN_CHIP_FAIL; }
+ memset(s, 0, sizeof *s);
+ s->valid = true; s->spec = *spec; s->kernel = kernel; s->code = code; s->insns = insns;
+@@ -235,7 +256,7 @@ static int gdn_core(OmegaGpuGdn *g, uint32_t layer, const float *qkv, const floa
+ if (!dev_open_locked()) { UNLOCK(); return OMEGA_GPU_GDN_CHIP_FAIL; }
+ Slot *ks = NULL; bool hit = false;
+ g_rec = rec != NULL;
+- rc = kernel_for(&spec, &ks, &hit);
++ rc = kernel_for(KK_DECODE, &spec, &ks, &hit);
+ g_rec = 0;
+ if (rc != OMEGA_GPU_GDN_OK) { UNLOCK(); return rc; }
+ uint8_t *b = g->lb.cpu;
+@@ -308,3 +329,144 @@ int omega_gpu_gdn_read_out(OmegaGpuGdn *g, float *out) {
+ omega_gpu_session_unlock();
+ return rc;
+ }
++
++/* ---- prefill (omega#543) ---- */
++/* scratch sections for T tokens (bytes, 256-aligned): qkv [T][10240] | z [T][6144] | ab [T][48][2] | out [T][6144] |
++ * o [T][6144] | records [T rounded up to 8][48] */
++enum { PF_QKV, PF_Z, PF_AB, PF_OUT, PF_O, PF_REC, PF_N };
++static size_t pf_layout(uint32_t T, size_t off[PF_N]) {
++ const uint64_t T8 = ((uint64_t)T + OMEGA_GDN_SCAN_CHUNK - 1u) / OMEGA_GDN_SCAN_CHUNK * OMEGA_GDN_SCAN_CHUNK;
++ const uint64_t sz[PF_N] = { (uint64_t)T * OMEGA_GDN_CONV_DIM * 4u, (uint64_t)T * OMEGA_GDN_V_DIM * 4u, (uint64_t)T * OMEGA_GDN_VALUE_HEADS * 8u,
++ (uint64_t)T * OMEGA_GDN_V_DIM * 4u, (uint64_t)T * OMEGA_GDN_V_DIM * 4u, T8 * OMEGA_GDN_VALUE_HEADS * OMEGA_GDN_REC_BYTES };
++ size_t at = 0;
++ for (unsigned i = 0; i < PF_N; i++) { off[i] = at; at += (size_t)((sz[i] + 255u) & ~(uint64_t)255u); }
++ return at;
++}
++
++int omega_gpu_gdn_prefill_reserve(OmegaGpuGdn *g, uint32_t max_tokens) {
++ if (!g || max_tokens < OMEGA_GDN_PF_MIN_T || max_tokens > OMEGA_GPU_GDN_PF_MAX_T) return OMEGA_GPU_GDN_BAD_ARGS;
++ size_t off[PF_N];
++ const size_t bytes = pf_layout(max_tokens, off);
++ omega_gpu_session_lock();
++ int rc = OMEGA_GPU_GDN_OK;
++ if (!dev_open_locked()) rc = OMEGA_GPU_GDN_CHIP_FAIL;
++ else {
++ omega_gpu_session_scratch_unreserve(&g->pf);
++ if (omega_gpu_session_scratch_reserve(&g->pf, bytes) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL;
++ }
++ omega_gpu_session_unlock();
++ return rc;
++}
++
++/* rec non-NULL (record path, caller holds the lock): the inputs are device rows, the scratch must already hold T
++ * tokens (omega_gpu_gdn_prefill_reserve), the three launches are appended to rec. */
++static int pf_core(OmegaGpuGdn *g, uint32_t layer, uint32_t T, const float *qkv, const float *z, const float *ab, float eps_l2,
++ float eps_norm, float in_scale, float out_scale, float *out, OmegaGpuGdnPfInfo *info,
++ OmegaGpuCmdList *rec, uint64_t dev_qkv, uint64_t dev_z, uint64_t dev_ab, uint64_t *out_va) {
++ uint64_t c0 = omega_gpu_session_now_ns();
++ if (info) memset(info, 0, sizeof *info);
++ if (!g || layer >= g->layers || T < OMEGA_GDN_PF_MIN_T || T > OMEGA_GPU_GDN_PF_MAX_T) return OMEGA_GPU_GDN_BAD_ARGS;
++ if (rec ? (!dev_qkv || !dev_z || !dev_ab) : (!qkv || !z || !ab || !out)) return OMEGA_GPU_GDN_BAD_ARGS;
++ if (!isfinite(eps_l2) || !isfinite(eps_norm) || !isfinite(in_scale) || !isfinite(out_scale)) return OMEGA_GPU_GDN_BAD_ARGS;
++ OmegaGdnSpec spec; memset(&spec, 0, sizeof spec);
++ spec.mutant = g_k.mutant;
++ size_t off[PF_N];
++ const size_t bytes = pf_layout(T, off);
++
++ if (!rec) omega_gpu_session_lock();
++#define UNLOCK() do { if (!rec) omega_gpu_session_unlock(); } while (0)
++ if (!dev_open_locked()) { UNLOCK(); return OMEGA_GPU_GDN_CHIP_FAIL; }
++ if (rec && (!g->pf.mem.cpu || g->pf.cap < bytes)) {
++ omega_gpu_session_set_error("gdn prefill record: scratch below T tokens (call omega_gpu_gdn_prefill_reserve first)");
++ UNLOCK(); return OMEGA_GPU_GDN_TOO_LARGE;
++ }
++ if (!rec && omega_gpu_session_scratch(&g->pf, bytes) != 0) { UNLOCK(); return OMEGA_GPU_GDN_TOO_LARGE; }
++ Slot *ks[3] = { NULL, NULL, NULL };
++ int rc = OMEGA_GPU_GDN_OK, hits = 0;
++ g_rec = rec != NULL;
++ for (unsigned k = 0; k < 3 && rc == OMEGA_GPU_GDN_OK; k++) { bool hit = false; rc = kernel_for(KK_PREP + k, &spec, &ks[k], &hit); hits += hit; }
++ g_rec = 0;
++ if (rc != OMEGA_GPU_GDN_OK) { UNLOCK(); return rc; }
++ uint8_t *b = g->pf.mem.cpu;
++ const uint64_t va = g->pf.mem.va;
++ float *yo = (float *)(b + off[PF_OUT]);
++ const size_t nout = (size_t)T * OMEGA_GDN_V_DIM;
++ if (!rec) {
++ memcpy(b + off[PF_QKV], qkv, (size_t)T * OMEGA_GDN_CONV_DIM * 4u);
++ memcpy(b + off[PF_Z], z, nout * 4u);
++ memcpy(b + off[PF_AB], ab, (size_t)T * OMEGA_GDN_VALUE_HEADS * 8u);
++ for (size_t i = 0; i < nout; i++) yo[i] = POISON_F32;
++ if (omega_gpu_session_sync_to_gpu(&g->pf.mem) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL;
++ }
++ const uint64_t a_qkv = rec ? dev_qkv : va + off[PF_QKV], a_z = rec ? dev_z : va + off[PF_Z], a_ab = rec ? dev_ab : va + off[PF_AB];
++ const uint64_t a_par = g->par.va + (uint64_t)layer * OMEGA_GDN_PAR_BYTES, a_tail = g->tail.va + (uint64_t)layer * OMEGA_GDN_TAIL_BYTES;
++ const uint64_t a_state = g->state.va + (uint64_t)layer * OMEGA_GDN_STATE_BYTES, a_rec = va + off[PF_REC], a_o = va + off[PF_O];
++ const uint64_t a_out = va + off[PF_OUT];
++ uint32_t ap[OMEGA_GDN_PREP_ARGS_WORDS], as[OMEGA_GDN_SCAN_ARGS_WORDS], aq[OMEGA_GDN_POST_ARGS_WORDS];
++#define A64(arr, i, v) do { (arr)[i] = (uint32_t)(v); (arr)[(i) + 1] = (uint32_t)((v) >> 32); } while (0)
++ A64(ap, 0, a_qkv); A64(ap, 2, a_ab); A64(ap, 4, a_par); A64(ap, 6, a_tail); A64(ap, 8, a_rec);
++ ap[10] = T; memcpy(&ap[11], &eps_l2, 4); memcpy(&ap[12], &in_scale, 4);
++ A64(as, 0, a_rec); A64(as, 2, a_qkv); A64(as, 4, a_tail); A64(as, 6, a_state); A64(as, 8, a_o);
++ as[10] = T; memcpy(&as[11], &in_scale, 4);
++ A64(aq, 0, a_o); A64(aq, 2, a_z); A64(aq, 4, a_par); A64(aq, 6, a_out);
++ memcpy(&aq[8], &eps_norm, 4); memcpy(&aq[9], &in_scale, 4); memcpy(&aq[10], &out_scale, 4);
++#undef A64
++ const uint32_t thr[3] = { OMEGA_GDN_PREP_THREADS, OMEGA_GDN_THREADS, OMEGA_GDN_THREADS };
++ const uint32_t gx[3] = { (T + OMEGA_GDN_PREP_TOKENS - 1u) / OMEGA_GDN_PREP_TOKENS, OMEGA_GDN_GRID_X, T };
++ const uint32_t gy[3] = { OMEGA_GDN_KEY_HEADS, OMEGA_GDN_GRID_Y, OMEGA_GDN_POST_GRID_Y };
++ const uint32_t sh[3] = { OMEGA_GDN_PREP_SHARED_BYTES, OMEGA_GDN_SCAN_SHARED_BYTES, 0 };
++ const uint32_t *args[3] = { ap, as, aq };
++ const uint32_t nargs[3] = { OMEGA_GDN_PREP_ARGS_WORDS, OMEGA_GDN_SCAN_ARGS_WORDS, OMEGA_GDN_POST_ARGS_WORDS };
++ if (info) {
++ info->layer = layer; info->tokens = T; info->kernel_cache_hits = (uint32_t)hits;
++ for (unsigned k = 0; k < 3; k++) { info->insns[k] = ks[k]->insns; info->gprs[k] = ks[k]->kernel.gpr_count; }
++ }
++ for (unsigned k = 0; k < 3 && rc == OMEGA_GPU_GDN_OK; k++) {
++ OmegaGpuLaunch L = {
++ .code_va = ks[k]->code.va, .gpr_count = omega_bw_gpr_declared(ks[k]->kernel.gpr_count),
++ .threads_x = thr[k], .threads_y = 1, .grid_x = gx[k], .grid_y = gy[k],
++ .num_elements = nout,
++ .args = args[k], .n_args = nargs[k], .timeout_ms = MARKER_TIMEOUT_MS,
++ .shared_bytes = sh[k],
++ };
++ if (!omega_bw_gpr_declared_ok(L.gpr_count, ks[k]->kernel.gpr_count)) { omega_gpu_session_set_error("declared registers below peak + 2: refusing to launch"); rc = OMEGA_GPU_GDN_CHIP_FAIL; }
++ else if (rec) { if (omega_gpu_cmdlist_add(rec, &L) < 0) rc = OMEGA_GPU_GDN_CHIP_FAIL; }
++ else if (omega_gpu_session_launch(&L, info ? &info->elapsed_ns[k] : NULL, NULL) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL;
++ }
++ if (rc == OMEGA_GPU_GDN_OK && !rec && omega_gpu_session_sync_to_cpu(&g->pf.mem) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL;
++ if (rc == OMEGA_GPU_GDN_OK && !rec) {
++ size_t unchanged = 0;
++ for (size_t i = 0; i < nout; i++) if (yo[i] == POISON_F32) unchanged++;
++ if (unchanged) { omega_gpu_session_set_error("gdn prefill: output poison survived"); rc = OMEGA_GPU_GDN_CHIP_FAIL; }
++ else memcpy(out, yo, nout * 4u);
++ }
++ if (rc == OMEGA_GPU_GDN_OK && out_va) *out_va = a_out;
++ UNLOCK();
++#undef UNLOCK
++ if (info) info->call_ns = omega_gpu_session_now_ns() - c0;
++ return rc;
++}
++
++int omega_gpu_gdn_prefill(OmegaGpuGdn *g, uint32_t layer, uint32_t tokens, const float *qkv, const float *z, const float *ab,
++ float eps_l2, float eps_norm, float in_scale, float out_scale, float *out, OmegaGpuGdnPfInfo *info) {
++ return pf_core(g, layer, tokens, qkv, z, ab, eps_l2, eps_norm, in_scale, out_scale, out, info, NULL, 0, 0, 0, NULL);
++}
++
++int omega_gpu_gdn_prefill_record(OmegaGpuCmdList *cl, OmegaGpuGdn *g, uint32_t layer, uint32_t tokens, uint64_t qkv_va,
++ uint64_t z_va, uint64_t ab_va, float eps_l2, float eps_norm, float in_scale, float out_scale,
++ uint64_t *out_va) {
++ if (!cl) return OMEGA_GPU_GDN_BAD_ARGS;
++ return pf_core(g, layer, tokens, NULL, NULL, NULL, eps_l2, eps_norm, in_scale, out_scale, NULL, NULL, cl, qkv_va, z_va, ab_va, out_va);
++}
++
++int omega_gpu_gdn_prefill_read_out(OmegaGpuGdn *g, uint32_t tokens, float *out) {
++ if (!g || !out || tokens < OMEGA_GDN_PF_MIN_T || tokens > OMEGA_GPU_GDN_PF_MAX_T) return OMEGA_GPU_GDN_BAD_ARGS;
++ size_t off[PF_N];
++ const size_t bytes = pf_layout(tokens, off);
++ omega_gpu_session_lock();
++ int rc = (!g->pf.mem.cpu || g->pf.cap < bytes) ? OMEGA_GPU_GDN_BAD_ARGS
++ : omega_gpu_session_sync_to_cpu(&g->pf.mem) == 0 ? OMEGA_GPU_GDN_OK : OMEGA_GPU_GDN_CHIP_FAIL;
++ if (rc == OMEGA_GPU_GDN_OK) memcpy(out, (const uint8_t *)g->pf.mem.cpu + off[PF_OUT], (size_t)tokens * OMEGA_GDN_V_DIM * 4u);
++ omega_gpu_session_unlock();
++ return rc;
++}
+diff --git a/src/omega_gpu_gdn_api.h b/src/omega_gpu_gdn_api.h
+index fe387699 ..2d820636 100644
+--- a/src/omega_gpu_gdn_api.h
++++ b/src/omega_gpu_gdn_api.h
+@@ -73,6 +73,35 @@ int omega_gpu_gdn_read_out(OmegaGpuGdn *g, float *out);
+ const char *omega_gpu_gdn_last_error(void);
+ const char *omega_gpu_gdn_rc_name(int rc);
+
++/* ---- prefill (omega#543, 2026-10-18): T >= 3 new tokens of one layer in three launches (prep, scan, post; see
++ * omega_blackwell_codegen_gdn.h), continuing from the layer's tail and state exactly as T decode steps would, and leaving
++ * them where the next prefill chunk or decode step picks them up. A long prompt is prefilled in chunks.
++ * int omega_gpu_gdn_prefill(g, layer, T, qkv [T][10240], z [T][6144], ab [T][48][2], eps_l2, eps_norm, in_scale,
++ * out_scale, out [T][6144], info); the decode step's row layouts, one row per token
++ * int omega_gpu_gdn_prefill_reserve(g, max_tokens); sizes the prefill scratch now and pins it (the record path needs
++ * it: a recorded launch must never see its scratch move); a later prefill of more tokens is refused
++ * int omega_gpu_gdn_prefill_record(cl, g, layer, T, qkv_va, z_va, ab_va, eps_l2, eps_norm, in_scale, out_scale, &out_va);
++ * the three launches appended to a command list (caller holds the lock); out_va = the [T][6144] rows in the scratch
++ * Scratch bytes for T tokens: T * (40960 + 3 * 24576 + 384) + ceil8(T) * 74496 (the per-token records dominate). */
++#define OMEGA_GPU_GDN_PF_MAX_T 65536u
++
++typedef struct {
++ uint32_t layer, tokens;
++ uint32_t insns[3], gprs[3]; /* prep, scan, post */
++ uint64_t elapsed_ns[3]; /* marker-to-marker chip time per launch (host path) */
++ uint64_t call_ns;
++ uint32_t kernel_cache_hits; /* 0..3 */
++} OmegaGpuGdnPfInfo;
++
++int omega_gpu_gdn_prefill(OmegaGpuGdn *g, uint32_t layer, uint32_t tokens, const float *qkv, const float *z, const float *ab,
++ float eps_l2, float eps_norm, float in_scale, float out_scale, float *out, OmegaGpuGdnPfInfo *info);
++int omega_gpu_gdn_prefill_reserve(OmegaGpuGdn *g, uint32_t max_tokens);
++int omega_gpu_gdn_prefill_record(struct OmegaGpuCmdList *cl, OmegaGpuGdn *g, uint32_t layer, uint32_t tokens, uint64_t qkv_va,
++ uint64_t z_va, uint64_t ab_va, float eps_l2, float eps_norm, float in_scale, float out_scale,
++ uint64_t *out_va);
++/* Test helper: the out rows the last prefill wrote (after a record + submit). */
++int omega_gpu_gdn_prefill_read_out(OmegaGpuGdn *g, uint32_t tokens, float *out);
++
+ /* Host layout helpers (no device; the test's simulator path uses them too). */
+ /* The parameter block (OMEGA_GDN_PAR_BYTES) from the checkpoint tensors (see set_layer). -1 on a NULL pointer. */
+ int omega_gdn_pack_params(const float *conv_w, const float *a_log, const float *dt_bias, const float *norm_w, uint8_t *block);
+diff --git a/tests/bw_warp_sim_k.h b/tests/bw_warp_sim_k.h
+index 53fed2db ..d42c89d3 100644
+--- a/tests/bw_warp_sim_k.h
++++ b/tests/bw_warp_sim_k.h
+@@ -276,6 +276,8 @@ static int ws_run_warp(WsSim *s, WsWarp *w, uint32_t warp, uint32_t cx, uint32_t
+ if (mask != live) { ws_fail(s, WS_E_SHFL_SPLIT, pc, "SHFL on a split warp"); return -1; }
+ uint32_t src[WS_WARP];
+ for (int L = 0; L < WS_WARP; L++) src[L] = ws_rd(&w->lane[L], in->src1_vreg, p);
++ /* SHFL.BFLY (imm bit 31, OMEGA_TC_MARK_BFLY: patched in after encoding, omega_blackwell_codegen_tc.h): lane L reads L ^ lane */
++ if (in->imm & 0x80000000u) { for (int L = 0; L < WS_WARP; L++) ws_wr(&w->lane[L], in->dst_vreg, src[L ^ (int)(in->imm & 31)], p); f->pc = pc + 1; break; }
+ for (int L = 0; L < WS_WARP; L++) { int from = L + (int)(in->imm & 31); ws_wr(&w->lane[L], in->dst_vreg, from < WS_WARP ? src[from] : src[L], p); }
+ f->pc = pc + 1;
+ break;
+diff --git a/tests/gpu_gdn_test.c b/tests/gpu_gdn_test.c
+index 7cbe26d7 ..153c7388 100644
+--- a/tests/gpu_gdn_test.c
++++ b/tests/gpu_gdn_test.c
+@@ -12,6 +12,7 @@
+ #include "omega_gpu_gdn_api.h"
+ #include "omega_gpu_session.h"
+ #include "omega_gpu_cmdlist.h"
++#include "omega_blackwell_codegen_tc.h"
+ #include "bw_warp_sim_k.h"
+ #include <inttypes.h>
+ #include <math.h>
+@@ -190,6 +191,23 @@ static void host_only(void) {
+ free(y); inp_free(&in); dstate_free(&d);
+ }
+ layer_free(&L);
++ /* the prefill kernels encode, and every SHFL.BFLY placeholder is patched (the count kernel_for demands) */
++ for (unsigned k = 0; k < 3; k++) {
++ static BlackwellIRProgram p;
++ OmegaGdnSpec spec = { 0 };
++ int rc = k == 0 ? omega_blackwell_codegen_gdn_prep_prog(&spec, &p) : k == 1 ? omega_blackwell_codegen_gdn_scan_prog(&spec, &p)
++ : omega_blackwell_codegen_gdn_post_prog(&spec, &p);
++ size_t need = rc == 0 ? omega_bw_program_code_bytes(&p) : 0;
++ uint8_t *code = need ? malloc(need) : NULL;
++ OmegaBlackwellKernel kern;
++ int enc = code ? omega_blackwell_kernel_encode_into(&p, code, need, &kern) : -1;
++ int np = enc == 0 ? omega_tc_patch_code(&p, code, need) : -1;
++ const uint32_t want = omega_gdn_bfly_count(&p);
++ if (getenv("GDN_DUMP") && code && atoi(getenv("GDN_DUMP")) == (int)k) { FILE *f = fopen("gdn_pf.bin", "wb"); if (f) { fwrite(code, 1, need, f); fclose(f); } }
++ CHECK(rc == 0 && enc == 0 && np >= 0 && (uint32_t)np == want && (k == 0 || want > 0),
++ "prefill kernel %u encodes: %zu insns, %u registers, %d of %u butterflies patched", k, p.count, p.regalloc.peak_gpr_usage, np, want);
++ free(code);
++ }
+ }
+
+ /* ---- simulator ---- */
+@@ -281,6 +299,110 @@ static void sim_battery(void) {
+ printf("sim warp issues: %" PRIu64 "\n", g_sim_issues);
+ }
+
++/* ---- prefill on the simulator (omega#543): prep, scan, post over T tokens against T oracle steps ---- */
++typedef struct { uint32_t off; void *p; size_t bytes; } SimBuf;
++typedef struct { uint32_t off; uint32_t word; } SimWord;
++static int sim_launch(const BlackwellIRProgram *prog, uint32_t threads, uint32_t gx, uint32_t gy, uint32_t smem_bytes, int order,
++ const SimBuf *b, int nb, const SimWord *w, int nw, char *msg, size_t n) {
++ static uint8_t smem[OMEGA_GDN_SCAN_SHARED_BYTES];
++ WsSim s; memset(&s, 0, sizeof s);
++ s.p = prog; s.threads = threads; s.order = order;
++ memset(smem, 0xcd, sizeof smem); /* poison: a read before a write shows */
++ s.smem = smem; s.smem_bytes = smem_bytes ? smem_bytes : 4u;
++ for (int i = 0; i < nb; i++) {
++ const uint64_t a = (uintptr_t)b[i].p;
++ s.cbank[b[i].off / 4] = (uint32_t)a; s.cbank[b[i].off / 4 + 1] = (uint32_t)(a >> 32);
++ s.ranges[i].lo = a; s.ranges[i].hi = a + b[i].bytes;
++ }
++ s.nranges = nb;
++ for (int i = 0; i < nw; i++) s.cbank[w[i].off / 4] = w[i].word;
++ int err = ws_run(&s, gx, gy);
++ if (err) snprintf(msg, n, "%.150s (pc %d)", s.msg, s.err_pc);
++ g_sim_issues += s.warp_issues;
++ return err;
++}
++static uint32_t fw(float f) { uint32_t u; memcpy(&u, &f, 4); return u; }
++static int sim_prefill(int mutant, uint32_t T, float in_scale, float out_scale, Errs *worst, char *msg, size_t n) {
++ static BlackwellIRProgram pp, ps, pq;
++ OmegaGdnSpec spec = { mutant };
++ if (omega_blackwell_codegen_gdn_prep_prog(&spec, &pp) != 0 || omega_blackwell_codegen_gdn_scan_prog(&spec, &ps) != 0 ||
++ omega_blackwell_codegen_gdn_post_prog(&spec, &pq) != 0) { snprintf(msg, n, "codegen refused"); return -1; }
++ Layer L; layer_make(&L, 21);
++ DState d; dstate_init(&d, &L);
++ const uint32_t T8 = (T + OMEGA_GDN_SCAN_CHUNK - 1u) / OMEGA_GDN_SCAN_CHUNK * OMEGA_GDN_SCAN_CHUNK;
++ const size_t s_qkv = (size_t)T * CD * 4u, s_z = (size_t)T * VD * 4u, s_ab = (size_t)T * VH * 8u;
++ const size_t s_rec = (size_t)T8 * VH * OMEGA_GDN_REC_BYTES, s_o = (size_t)T * VD * 4u;
++ float *qkv = aligned_alloc(256, s_qkv), *z = aligned_alloc(256, s_z), *ab = aligned_alloc(256, s_ab);
++ uint8_t *par = aligned_alloc(256, OMEGA_GDN_PAR_BYTES), *tail = aligned_alloc(256, OMEGA_GDN_TAIL_BYTES);
++ uint8_t *state = aligned_alloc(256, OMEGA_GDN_STATE_BYTES), *rec = aligned_alloc(256, s_rec);
++ float *o = aligned_alloc(256, s_o), *out = aligned_alloc(256, s_o), *out0 = malloc(s_o);
++ uint8_t *tail0 = malloc(OMEGA_GDN_TAIL_BYTES), *state0 = malloc(OMEGA_GDN_STATE_BYTES);
++ double *y = malloc((size_t)T * VD * 8);
++ float *tref = malloc((size_t)NT * 4);
++ for (uint32_t t = 0; t < T; t++) {
++ Inp in; inp_make(&in, 1000 + t, in_scale);
++ memcpy(qkv + (size_t)t * CD, in.qkv, CD * 4u); memcpy(z + (size_t)t * VD, in.z, VD * 4u); memcpy(ab + (size_t)t * VH * 2u, in.ab, VH * 8u);
++ oracle_step(&L, &d, &in, in_scale, out_scale, y + (size_t)t * VD);
++ inp_free(&in);
++ }
++ omega_gdn_pack_params(L.conv_w, L.a_log, L.dtb, L.nw, par);
++ int err = 0;
++ for (int order = 0; order < 2 && !err; order++) {
++ omega_gdn_pack_tail(L.tail0, (float *)tail);
++ memcpy(state, L.state0, OMEGA_GDN_STATE_BYTES);
++ memset(rec, 0xcd, s_rec);
++ for (size_t i = 0; i < (size_t)T * VD; i++) { o[i] = -999.0f; out[i] = -999.0f; }
++ const SimBuf bp[] = { { OMEGA_GDN_PREP_ARG_QKV, qkv, s_qkv }, { OMEGA_GDN_PREP_ARG_AB, ab, s_ab }, { OMEGA_GDN_PREP_ARG_PAR, par, OMEGA_GDN_PAR_BYTES },
++ { OMEGA_GDN_PREP_ARG_TAIL, tail, OMEGA_GDN_TAIL_BYTES }, { OMEGA_GDN_PREP_ARG_REC, rec, s_rec } };
++ const SimWord wp[] = { { OMEGA_GDN_PREP_ARG_T, T }, { OMEGA_GDN_PREP_ARG_EPS_L2, fw(EPS_L2) }, { OMEGA_GDN_PREP_ARG_IN_SCALE, fw(in_scale) } };
++ const SimBuf bs[] = { { OMEGA_GDN_SCAN_ARG_REC, rec, s_rec }, { OMEGA_GDN_SCAN_ARG_QKV, qkv, s_qkv }, { OMEGA_GDN_SCAN_ARG_TAIL, tail, OMEGA_GDN_TAIL_BYTES },
++ { OMEGA_GDN_SCAN_ARG_STATE, state, OMEGA_GDN_STATE_BYTES }, { OMEGA_GDN_SCAN_ARG_O, o, s_o } };
++ const SimWord wsc[] = { { OMEGA_GDN_SCAN_ARG_T, T }, { OMEGA_GDN_SCAN_ARG_IN_SCALE, fw(in_scale) } };
++ const SimBuf bq[] = { { OMEGA_GDN_POST_ARG_O, o, s_o }, { OMEGA_GDN_POST_ARG_Z, z, s_z }, { OMEGA_GDN_POST_ARG_PAR, par, OMEGA_GDN_PAR_BYTES },
++ { OMEGA_GDN_POST_ARG_OUT, out, s_o } };
++ const SimWord wq[] = { { OMEGA_GDN_POST_ARG_EPS_NORM, fw(EPS_N) }, { OMEGA_GDN_POST_ARG_IN_SCALE, fw(in_scale) }, { OMEGA_GDN_POST_ARG_OUT_SCALE, fw(out_scale) } };
++ char m2[200] = "";
++ if (sim_launch(&pp, OMEGA_GDN_PREP_THREADS, (T + OMEGA_GDN_PREP_TOKENS - 1u) / OMEGA_GDN_PREP_TOKENS, KH, OMEGA_GDN_PREP_SHARED_BYTES, order, bp, 5, wp, 3, m2, sizeof m2)) { snprintf(msg, n, "prep: %s", m2); err = -1; break; }
++ if (sim_launch(&ps, OMEGA_GDN_THREADS, OMEGA_GDN_GRID_X, OMEGA_GDN_GRID_Y, OMEGA_GDN_SCAN_SHARED_BYTES, order, bs, 5, wsc, 2, m2, sizeof m2)) { snprintf(msg, n, "scan: %s", m2); err = -1; break; }
++ if (sim_launch(&pq, OMEGA_GDN_THREADS, T, OMEGA_GDN_POST_GRID_Y, 0, order, bq, 4, wq, 3, m2, sizeof m2)) { snprintf(msg, n, "post: %s", m2); err = -1; break; }
++ size_t unwritten = 0; for (size_t i = 0; i < (size_t)T * VD; i++) if (out[i] == -999.0f) unwritten++;
++ if (unwritten) { snprintf(msg, n, "%zu output words never written", unwritten); err = -1; break; }
++ if (order == 0) { memcpy(out0, out, s_o); memcpy(tail0, tail, OMEGA_GDN_TAIL_BYTES); memcpy(state0, state, OMEGA_GDN_STATE_BYTES); }
++ else if (memcmp(out0, out, s_o) || memcmp(tail0, tail, OMEGA_GDN_TAIL_BYTES) || memcmp(state0, state, OMEGA_GDN_STATE_BYTES)) {
++ snprintf(msg, n, "the warp order changed the result"); err = -1;
++ }
++ }
++ if (!err) {
++ double *ts = malloc((size_t)NT * 8);
++ for (size_t i = 0; i < NT; i++) ts[i] = d.tail[i];
++ worst->out = nerr(out, y, (size_t)T * VD);
++ worst->tail = omega_gdn_unpack_tail((const float *)tail, tref) != 0 ? INFINITY : nerr(tref, ts, NT);
++ worst->state = nerr((const float *)state, d.state, NS);
++ free(ts);
++ }
++ free(qkv); free(z); free(ab); free(par); free(tail); free(state); free(rec); free(o); free(out); free(out0);
++ free(tail0); free(state0); free(y); free(tref);
++ dstate_free(&d); layer_free(&L);
++ return err;
++}
++static void sim_prefill_battery(void) {
++ struct { int mutant; uint32_t T; float in_scale, out_scale; const char *name; } pts[] = {
++ { 0, 37, 1.0f, 1.0f, "prefill T 37" }, { 0, 3, 1.0f, 1.0f, "prefill T 3" },
++ { 0, 17, 0.0009765625f , 18446744073709551616 .0f, "prefill T 17, in 2^-10, out 2^64" },
++ { 1, 19, 1.0f, 1.0f, "prefill mutant 1 (no decay)" }, { 2, 19, 1.0f, 1.0f, "prefill mutant 2 (beta 1)" },
++ { 3, 19, 1.0f, 1.0f, "prefill mutant 3 (gate z)" }, { 4, 19, 1.0f, 1.0f, "prefill mutant 4 (tail kept)" },
++ };
++ for (size_t i = 0; i < sizeof pts / sizeof pts[0]; i++) {
++ char msg[300] = ""; Errs w = { 0, 0, 0 };
++ double t0 = now_ms();
++ int err = sim_prefill(pts[i].mutant, pts[i].T, pts[i].in_scale, pts[i].out_scale, &w, msg, sizeof msg);
++ 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);
++ CHECK(err == 0, "sim %s ran (%s)", pts[i].name, msg);
++ if (pts[i].mutant == 0) CHECK(errs_ok(&w), "sim %s within tolerance", pts[i].name);
++ else CHECK(!errs_ok(&w), "sim %s caught", pts[i].name);
++ }
++}
++
+ /* ---- chip ---- */
+ static int chip_steps(OmegaGpuGdn *g, uint32_t layer, const Layer *L, DState *d, uint32_t steps, uint32_t seed, float in_scale,
+ float out_scale, Errs *worst, float *last_out) {
+@@ -306,6 +428,175 @@ static int chip_steps(OmegaGpuGdn *g, uint32_t layer, const Layer *L, DState *d,
+ free(out); free(tail); free(state); free(y); free(td);
+ return rc;
+ }
++/* ---- prefill on the chip (omega#543) ---- */
++typedef struct { float *qkv, *z, *ab; } PfIn;
++static void pf_in_make(PfIn *p, uint32_t T, uint32_t seed, float in_scale) {
++ p->qkv = malloc((size_t)T * CD * 4); p->z = malloc((size_t)T * VD * 4); p->ab = malloc((size_t)T * VH * 8);
++ for (uint32_t t = 0; t < T; t++) {
++ Inp in; inp_make(&in, seed + t, in_scale);
++ memcpy(p->qkv + (size_t)t * CD, in.qkv, CD * 4u); memcpy(p->z + (size_t)t * VD, in.z, VD * 4u); memcpy(p->ab + (size_t)t * VH * 2u, in.ab, VH * 8u);
++ inp_free(&in);
++ }
++}
++static void pf_in_free(PfIn *p) { free(p->qkv); free(p->z); free(p->ab); }
++/* oracle over the same T rows; y [T][VD] */
++static void pf_oracle(const Layer *L, DState *d, uint32_t T, uint32_t seed, float in_scale, float out_scale, double *y) {
++ for (uint32_t t = 0; t < T; t++) { Inp in; inp_make(&in, seed + t, in_scale); oracle_step(L, d, &in, in_scale, out_scale, y + (size_t)t * VD); inp_free(&in); }
++}
++/* prefill T tokens on `layer` from its current state, then `dec` decode steps; worst errors over every row, the tail and
++ * the state after the prefill and after each step */
++static int chip_prefill_chain(OmegaGpuGdn *g, uint32_t layer, const Layer *L, uint32_t T, uint32_t dec, uint32_t seed, float in_scale,
++ float out_scale, Errs *worst, float *out_copy) {
++ DState d; dstate_init(&d, L);
++ PfIn p; pf_in_make(&p, T, seed, in_scale);
++ float *out = malloc((size_t)T * VD * 4), *tail = malloc((size_t)NT * 4), *state = malloc((size_t)NS * 4);
++ double *y = malloc((size_t)T * VD * 8), *td = malloc((size_t)NT * 8);
++ memset(worst, 0, sizeof *worst);
++ int rc = omega_gpu_gdn_prefill(g, layer, T, p.qkv, p.z, p.ab, EPS_L2, EPS_N, in_scale, out_scale, out, NULL);
++ if (rc == 0) rc = omega_gpu_gdn_get_state(g, layer, tail, state);
++ if (rc == 0) {
++ pf_oracle(L, &d, T, seed, in_scale, out_scale, y);
++ for (size_t i = 0; i < NT; i++) td[i] = d.tail[i];
++ worst->out = nerr(out, y, (size_t)T * VD); worst->tail = nerr(tail, td, NT); worst->state = nerr(state, d.state, NS);
++ if (out_copy) memcpy(out_copy, out, (size_t)T * VD * 4);
++ }
++ if (rc == 0 && dec) {
++ Errs e;
++ rc = chip_steps(g, layer, L, &d, dec, seed + T, in_scale, out_scale, &e, NULL);
++ if (e.out > worst->out) worst->out = e.out;
++ if (e.tail > worst->tail) worst->tail = e.tail;
++ if (e.state > worst->state) worst->state = e.state;
++ }
++ free(out); free(tail); free(state); free(y); free(td);
++ pf_in_free(&p); dstate_free(&d);
++ return rc;
++}
++static void chip_prefill(OmegaGpuGdn *g, Layer *L, FILE *out, int *first, uint32_t reps) {
++#define PPOINT(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)
++ struct { uint32_t layer, T, dec, seed; float in_scale, out_scale; const char *name; } pp[] = {
++ { 1, 37, 3, 2000, 1.0f, 1.0f, "prefill layer 1, T 37, then 3 decode steps" },
++ { 0, 3, 1, 2100, 1.0f, 1.0f, "prefill layer 0, T 3, then 1 decode step" },
++ { 0, 41, 0, 2200, 0.0009765625f , 18446744073709551616 .0f, "prefill layer 0, T 41, in 2^-10 out 2^64" },
++ };
++ for (size_t i = 0; i < sizeof pp / sizeof pp[0]; i++) {
++ CHECK(omega_gpu_gdn_set_state(g, pp[i].layer, L[pp[i].layer].tail0, L[pp[i].layer].state0) == 0, "set state for %s", pp[i].name);
++ Errs e;
++ int rc = chip_prefill_chain(g, pp[i].layer, &L[pp[i].layer], pp[i].T, pp[i].dec, pp[i].seed, pp[i].in_scale, pp[i].out_scale, &e, NULL);
++ printf("chip %-48s 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() : "");
++ CHECK(rc == 0 && errs_ok(&e), "chip %s", pp[i].name);
++ PPOINT(pp[i].name, e, "");
++ }
++ /* rerun from the same state: bit-identical rows */
++ {
++ float *a = malloc((size_t)37 * VD * 4), *b = malloc((size_t)37 * VD * 4);
++ Errs e1, e2;
++ CHECK(omega_gpu_gdn_set_state(g, 1, L[1].tail0, L[1].state0) == 0, "set state for prefill rerun");
++ int rc = chip_prefill_chain(g, 1, &L[1], 37, 0, 2000, 1.0f, 1.0f, &e1, a);
++ CHECK(omega_gpu_gdn_set_state(g, 1, L[1].tail0, L[1].state0) == 0, "set state for prefill rerun 2");
++ int rc2 = chip_prefill_chain(g, 1, &L[1], 37, 0, 2000, 1.0f, 1.0f, &e2, b);
++ CHECK(rc == 0 && rc2 == 0 && memcmp(a, b, (size_t)37 * VD * 4) == 0, "prefill rerun from the same state is bit-identical");
++ free(a); free(b);
++ }
++ /* mutants */
++ for (int m = 1; m <= 4; m++) {
++ omega_gpu_gdn_test_set_mutant(m);
++ CHECK(omega_gpu_gdn_set_state(g, 1, L[1].tail0, L[1].state0) == 0, "set state prefill mutant %d", m);
++ Errs e;
++ int rc = chip_prefill_chain(g, 1, &L[1], 19, 0, 2300, 1.0f, 1.0f, &e, NULL);
++ char nm[40]; snprintf(nm, sizeof nm, "prefill mutant %d", m);
++ printf("chip %-48s rc=%s err out %.3g tail %.3g state %.3g\n", nm, omega_gpu_gdn_rc_name(rc), e.out, e.tail, e.state);
++ CHECK(rc == 0 && !errs_ok(&e), "chip prefill mutant %d caught", m);
++ PPOINT(nm, e, "");
++ }
++ omega_gpu_gdn_test_set_mutant(0);
++ /* the record path: prefill 37 tokens and one decode step in one command list on layer 0 */
++ {
++ const uint32_t T = 37;
++ CHECK(omega_gpu_gdn_set_state(g, 0, L[0].tail0, L[0].state0) == 0, "set state for prefill record");
++ CHECK(omega_gpu_gdn_prefill_reserve(g, 64) == 0, "prefill reserve 64 tokens");
++ PfIn p; pf_in_make(&p, T, 2400, 1.0f);
++ Inp nx; inp_make(&nx, 2400 + T, 1.0f);
++ DState d; dstate_init(&d, &L[0]);
++ double *y = malloc((size_t)T * VD * 8), *y1 = malloc((size_t)VD * 8);
++ pf_oracle(&L[0], &d, T, 2400, 1.0f, 1.0f, y);
++ oracle_step(&L[0], &d, &nx, 1.0f, 1.0f, y1);
++ /* warm all four kernels on layer 1 (a cache miss inside the record is refused) */
++ float *tmp = malloc((size_t)T * VD * 4);
++ CHECK(omega_gpu_gdn_prefill(g, 1, T, p.qkv, p.z, p.ab, EPS_L2, EPS_N, 1.0f, 1.0f, tmp, NULL) == 0, "prefill warm-up");
++ CHECK(omega_gpu_gdn_step(g, 1, nx.qkv, nx.z, nx.ab, EPS_L2, EPS_N, 1.0f, 1.0f, tmp, NULL) == 0, "decode warm-up");
++ OmegaGpuCmdList *cl = NULL; /* create and free take the session lock themselves (omega_gpu_cmdlist.h) */
++ const int cl_ok = omega_gpu_cmdlist_create(8, &cl) == 0;
++ omega_gpu_session_lock();
++ OmegaGpuMem inb; memset(&inb, 0, sizeof inb);
++ const size_t s_qkv = (size_t)T * CD * 4, s_z = (size_t)T * VD * 4, s_ab = (size_t)T * VH * 8;
++ const size_t o_nx = (s_qkv + s_z + s_ab + 255) & ~(size_t)255;
++ int ok = cl_ok && omega_gpu_session_open() != NULL && omega_gpu_session_alloc(o_nx + (size_t)CD * 4 + VD * 4 + VH * 8, &inb) == 0;
++ if (ok) {
++ uint8_t *c = inb.cpu;
++ memcpy(c, p.qkv, s_qkv); memcpy(c + s_qkv, p.z, s_z); memcpy(c + s_qkv + s_z, p.ab, s_ab);
++ memcpy(c + o_nx, nx.qkv, (size_t)CD * 4); memcpy(c + o_nx + CD * 4, nx.z, (size_t)VD * 4); memcpy(c + o_nx + CD * 4 + VD * 4, nx.ab, VH * 8);
++ ok = omega_gpu_session_sync_to_gpu(&inb) == 0;
++ }
++ uint64_t pf_out = 0, dec_out = 0;
++ if (ok) ok = omega_gpu_gdn_prefill_record(cl, g, 0, T, inb.va, inb.va + s_qkv, inb.va + s_qkv + s_z, EPS_L2, EPS_N, 1.0f, 1.0f, &pf_out) == 0;
++ if (ok) ok = omega_gpu_gdn_record(cl, g, 0, inb.va + o_nx, inb.va + o_nx + CD * 4, inb.va + o_nx + CD * 4 + VD * 4, EPS_L2, EPS_N, 1.0f, 1.0f, &dec_out) == 0;
++ OmegaGpuCmdListOpts opts; memset(&opts, 0, sizeof opts);
++ OmegaGpuCmdListStats stc; memset(&stc, 0, sizeof stc);
++ if (ok) ok = omega_gpu_cmdlist_submit(cl, &opts, &stc) == 0;
++ if (inb.cpu) omega_gpu_session_free(&inb);
++ omega_gpu_session_unlock();
++ if (cl) omega_gpu_cmdlist_free(cl);
++ CHECK(ok, "record prefill + decode step and submit (%s)", ok ? "" : omega_gpu_gdn_last_error());
++ if (ok) {
++ float *t = malloc((size_t)NT * 4), *s = malloc((size_t)NS * 4), *yo = malloc((size_t)T * VD * 4), *y1o = malloc((size_t)VD * 4);
++ double *td = malloc((size_t)NT * 8);
++ ok = omega_gpu_gdn_get_state(g, 0, t, s) == 0 && omega_gpu_gdn_prefill_read_out(g, T, yo) == 0 && omega_gpu_gdn_read_out(g, y1o) == 0;
++ for (size_t i = 0; i < NT; i++) td[i] = d.tail[i];
++ Errs e = { nerr(yo, y, (size_t)T * VD), nerr(t, td, NT), nerr(s, d.state, NS) };
++ const double e1 = nerr(y1o, y1, VD);
++ if (e1 > e.out) e.out = e1;
++ printf("chip %-48s err out %.3g tail %.3g state %.3g\n", "record path, prefill 37 + 1 decode step", e.out, e.tail, e.state);
++ CHECK(ok && errs_ok(&e), "record path: prefill then a decode step match the oracle");
++ PPOINT("record path, prefill 37 + 1 decode step", e, "");
++ free(t); free(s); free(yo); free(y1o); free(td);
++ }
++ free(tmp); free(y); free(y1); dstate_free(&d); pf_in_free(&p); inp_free(&nx);
++ /* the reservation pins the scratch: a longer prefill is refused, not reallocated */
++ PfIn q; pf_in_make(&q, 65, 2500, 1.0f);
++ float *o65 = malloc((size_t)65 * VD * 4);
++ CHECK(omega_gpu_gdn_prefill(g, 1, 65, q.qkv, q.z, q.ab, EPS_L2, EPS_N, 1.0f, 1.0f, o65, NULL) == OMEGA_GPU_GDN_TOO_LARGE, "prefill past the reservation is refused");
++ free(o65); pf_in_free(&q);
++ }
++ /* timing per launch at three prompt lengths (a fresh, unreserved scratch per length) */
++ const uint32_t Ts[3] = { 512, 2048, 8192 };
++ for (unsigned k = 0; k < 3; k++) {
++ const uint32_t T = Ts[k], nrep = reps < 5 ? reps : 5;
++ CHECK(omega_gpu_gdn_prefill_reserve(g, T) == 0, "reserve %u tokens", T);
++ PfIn p; pf_in_make(&p, T, 3000, 1.0f);
++ float *o = malloc((size_t)T * VD * 4);
++ uint64_t e[3][5], c[5];
++ uint32_t done = 0;
++ OmegaGpuGdnPfInfo info;
++ for (uint32_t r = 0; r < nrep; r++) {
++ if (omega_gpu_gdn_prefill(g, 0, T, p.qkv, p.z, p.ab, EPS_L2, EPS_N, 1.0f, 1.0f, o, &info) != 0) break;
++ for (unsigned j = 0; j < 3; j++) e[j][done] = info.elapsed_ns[j];
++ c[done] = info.call_ns; done++;
++ }
++ CHECK(done == nrep, "prefill T %u timing reps ran (%u of %u): %s", T, done, nrep, done == nrep ? "" : omega_gpu_gdn_last_error());
++ if (done) {
++ for (unsigned j = 0; j < 3; j++) qsort(e[j], done, 8, cmp_u64);
++ qsort(c, done, 8, cmp_u64);
++ const double us[3] = { e[0][done / 2] / 1e3, e[1][done / 2] / 1e3, e[2][done / 2] / 1e3 }, tot = us[0] + us[1] + us[2];
++ printf("timing prefill T %u (medians of %u): prep %.1f us, scan %.1f us, post %.1f us, chip total %.1f us = %.2f us/token, call %.1f us; insns %u/%u/%u, registers %u/%u/%u\n",
++ T, done, us[0], us[1], us[2], tot, tot / T, c[done / 2] / 1e3, info.insns[0], info.insns[1], info.insns[2], info.gprs[0], info.gprs[1], info.gprs[2]);
++ if (out) fprintf(out, "%s{\"name\":\"timing prefill\",\"tokens\":%u,\"reps\":%u,\"prep_us\":%.3f,\"scan_us\":%.3f,\"post_us\":%.3f,\"chip_us\":%.3f,\"us_per_token\":%.4f,\"call_us\":%.3f,\"insns\":[%u,%u,%u],\"gprs\":[%u,%u,%u]}",
++ *first ? "" : ",", T, done, us[0], us[1], us[2], tot, tot / T, c[done / 2] / 1e3, info.insns[0], info.insns[1], info.insns[2], info.gprs[0], info.gprs[1], info.gprs[2]);
++ *first = 0;
++ }
++ free(o); pf_in_free(&p);
++ }
++#undef PPOINT
++}
+ static int chip_run(const char *out_path, uint32_t reps) {
+ FILE *out = out_path ? fopen(out_path, "w") : NULL;
+ if (out_path && !out) { printf("cannot write %s\n", out_path); return 1; }
+@@ -382,10 +673,11 @@ static int chip_run(const char *out_path, uint32_t reps) {
+ /* warm the kernel cache outside the record (a miss inside is refused) */
+ float *tmp = malloc((size_t)VD * 4);
+ 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");
++ OmegaGpuCmdList *cl = NULL; /* create and free take the session lock themselves (omega_gpu_cmdlist.h) */
++ const int cl_ok = omega_gpu_cmdlist_create(4, &cl) == 0;
+ omega_gpu_session_lock();
+ OmegaGpuMem inb[2]; memset(inb, 0, sizeof inb);
+- OmegaGpuCmdList *cl = NULL;
+- int ok = omega_gpu_session_open() != NULL;
++ int ok = cl_ok && omega_gpu_session_open() != NULL;
+ const size_t ib = (size_t)CD * 4 + (size_t)VD * 4 + VH * 8;
+ for (int i = 0; ok && i < 2; i++) {
+ ok = omega_gpu_session_alloc(ib, &inb[i]) == 0;
+@@ -396,15 +688,14 @@ static int chip_run(const char *out_path, uint32_t reps) {
+ }
+ }
+ uint64_t out_va = 0;
+- if (ok) ok = omega_gpu_cmdlist_create(4, &cl) == 0;
+ for (int i = 0; ok && i < 2; i++)
+ 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;
+ OmegaGpuCmdListOpts opts; memset(&opts, 0, sizeof opts);
+ OmegaGpuCmdListStats stc; memset(&stc, 0, sizeof stc);
+ if (ok) ok = omega_gpu_cmdlist_submit(cl, &opts, &stc) == 0;
+- if (cl) omega_gpu_cmdlist_free(cl);
+ for (int i = 0; i < 2; i++) if (inb[i].cpu) omega_gpu_session_free(&inb[i]);
+ omega_gpu_session_unlock();
++ if (cl) omega_gpu_cmdlist_free(cl);
+ CHECK(ok, "record two steps and submit (%s)", ok ? "" : omega_gpu_gdn_last_error());
+ if (ok) {
+ float *t = malloc((size_t)NT * 4), *s = malloc((size_t)NS * 4), *yo = malloc((size_t)VD * 4);
+@@ -419,6 +710,7 @@ static int chip_run(const char *out_path, uint32_t reps) {
+ }
+ free(tmp); free(y); dstate_free(&d); inp_free(&in[0]); inp_free(&in[1]);
+ }
++ chip_prefill(g, L, out, &first, reps);
+ /* timing */
+ {
+ Inp in; inp_make(&in, 950, 1.0f);
+@@ -450,6 +742,7 @@ static int chip_run(const char *out_path, uint32_t reps) {
+ }
+
+ int main(int argc, char **argv) {
++ setvbuf(stdout, NULL, _IOLBF, 0); /* a stuck chip run still leaves every finished line in its log */
+ int host = 0, sim = 0; const char *out_path = NULL; uint32_t reps = 0, spin = 2000;
+ for (int i = 1; i < argc; i++) {
+ if (strcmp(argv[i], "--host-only") == 0) host = 1;
+@@ -460,7 +753,7 @@ int main(int argc, char **argv) {
+ else { printf("unknown argument %s\n", argv[i]); return 2; }
+ }
+ if (host) host_only();
+- if (sim) sim_battery();
++ if (sim) { sim_battery(); sim_prefill_battery(); }
+ if (!host && !sim) {
+ (void)omega_gpu_session_set_spin_us(spin);
+ chip_run(out_path, reps ? reps : 50);
diff --git a/evidence/gdn-prefill-20261018 /chip-queued.txt b/evidence/gdn-prefill-20261018 /chip-queued.txt
new file mode 100644
index 000000000 ..7c8069743
--- /dev/null
+++ b/evidence/gdn-prefill-20261018 /chip-queued.txt
@@ -0,0 +1 @@
+2026-10-18T12:58:14Z
diff --git a/evidence/gdn-prefill-20261018 /chip-started.txt b/evidence/gdn-prefill-20261018 /chip-started.txt
new file mode 100644
index 000000000 ..7c8069743
--- /dev/null
+++ b/evidence/gdn-prefill-20261018 /chip-started.txt
@@ -0,0 +1 @@
+2026-10-18T12:58:14Z
diff --git a/evidence/gdn-prefill-20261018 /chip.log b/evidence/gdn-prefill-20261018 /chip.log
new file mode 100644
index 000000000 ..13174215d
--- /dev/null
+++ b/evidence/gdn-prefill-20261018 /chip.log
@@ -0,0 +1,22 @@
+gdn alloc (2 layers): rc=OK 83.1 ms ()
+chip layer 1, 5 steps rc=OK err out 2.34e-07 tail 0 state 3.23e-07
+chip layer 0, 5 steps, in 2^-10 out 2^64 rc=OK err out 3.03e-07 tail 0 state 3.64e-07
+chip mutant 1 rc=OK err out 0.913 tail 0 state 0.983
+chip mutant 2 rc=OK err out 0.509 tail 0 state 0.973
+chip mutant 3 rc=OK err out 1.46 tail 0 state 2.24e-07
+chip mutant 4 rc=OK err out 1.02 tail 1.79 state 0.844
+chip record path, 2 chained steps err out 2.3e-07 tail 0 state 2.19e-07
+chip prefill layer 1, T 37, then 3 decode steps rc=OK err out 2.25e-07 tail 0 state 3.69e-07
+chip prefill layer 0, T 3, then 1 decode step rc=OK err out 2.1e-07 tail 0 state 2.55e-07
+chip prefill layer 0, T 41, in 2^-10 out 2^64 rc=OK err out 3.04e-07 tail 0 state 4.03e-07
+chip prefill mutant 1 rc=OK err out 0.967 tail 0 state 2
+chip prefill mutant 2 rc=OK err out 0.677 tail 0 state 1.68
+chip prefill mutant 3 rc=OK err out 0.969 tail 0 state 5.47e-07
+chip prefill mutant 4 rc=OK err out 3.57e-07 tail 1.79 state 5.47e-07
+chip record path, prefill 37 + 1 decode step err out 2.2e-07 tail 0 state 3.06e-07
+timing prefill T 512 (medians of 5): prep 407.3 us, scan 1439.8 us, post 230.7 us, chip total 2077.8 us = 4.06 us/token, call 5289.8 us; insns 287/534/90, registers 96/104/28
+timing prefill T 2048 (medians of 5): prep 1566.6 us, scan 5362.1 us, post 851.3 us, chip total 7780.0 us = 3.80 us/token, call 21370.1 us; insns 287/534/90, registers 96/104/28
+timing prefill T 8192 (medians of 5): prep 12701.7 us, scan 21624.5 us, post 7193.5 us, chip total 41519.6 us = 5.07 us/token, call 97320.2 us; insns 287/534/90, registers 96/104/28
+timing (medians of 50): chip 124.1 us (state read + write 6.29 MB -> 50.7 GB/s; 273 GB/s nominal), call 127.1 us; 665 insns, 116 registers
+gpu_gdn_test: 54 checks, 0 failed: PASS
+rc=0
diff --git a/evidence/gdn-prefill-20261018 /device.txt b/evidence/gdn-prefill-20261018 /device.txt
new file mode 100644
index 000000000 ..f4adb0869
--- /dev/null
+++ b/evidence/gdn-prefill-20261018 /device.txt
@@ -0,0 +1,3 @@
+Linux spark-b87b 7.0.0-1019-nvidia #19~24.04.2-Ubuntu SMP PREEMPT_DYNAMIC Sun Sep 6 18:35:11 UTC 2 aarch64 aarch64 aarch64 GNU/Linux
+NVIDIA GB10, 580.173.02
+2026-10-18T12:20:35Z
diff --git a/evidence/gdn-prefill-20261018 /host.log b/evidence/gdn-prefill-20261018 /host.log
new file mode 100644
index 000000000 ..7b4ceaca8
--- /dev/null
+++ b/evidence/gdn-prefill-20261018 /host.log
@@ -0,0 +1,2 @@
+kernel: 665 instructions, peak 116 registers (120 declared, 256 threads)
+gpu_gdn_test: 16 checks, 0 failed: PASS
diff --git a/evidence/gdn-prefill-20261018 /provenance.txt b/evidence/gdn-prefill-20261018 /provenance.txt
new file mode 100644
index 000000000 ..5dfc707b3
--- /dev/null
+++ b/evidence/gdn-prefill-20261018 /provenance.txt
@@ -0,0 +1 @@
+chip binary = base-commit.txt (19bb4437 , the first decode commit before the stack was rebased; same patch as 47eec857 ) + change.diff = commit in built-from-commit.txt. Rebasing onto feat/gdn-decode changed no GDN file; the FP8 cap change underneath only raises OMEGA_FP8_MAX_K / OMEGA_TCGEMM_MAX_K.
diff --git a/evidence/gdn-prefill-20261018 /receipt-chip.json b/evidence/gdn-prefill-20261018 /receipt-chip.json
new file mode 100644
index 000000000 ..83e95db4c
--- /dev/null
+++ b/evidence/gdn-prefill-20261018 /receipt-chip.json
@@ -0,0 +1 @@
+{"receipt":"OMEGA_GPU_GDN_V1","reps":50,"tol":{"out":0.0002,"tail":1e-06,"state":2e-05},"points":[{"name":"layer 1, 5 steps","err_out":2.34349e-07,"err_tail":0,"err_state":3.23021e-07},{"name":"layer 0, 5 steps, in 2^-10 out 2^64","err_out":3.03034e-07,"err_tail":0,"err_state":3.63538e-07},{"name":"mutant 1","err_out":0.912734,"err_tail":0,"err_state":0.982901},{"name":"mutant 2","err_out":0.509423,"err_tail":0,"err_state":0.973191},{"name":"mutant 3","err_out":1.46485,"err_tail":0,"err_state":2.23656e-07},{"name":"mutant 4","err_out":1.0234,"err_tail":1.78835,"err_state":0.844121},{"name":"record path, 2 chained steps","err_out":2.29589e-07,"err_tail":0,"err_state":2.19207e-07},{"name":"prefill layer 1, T 37, then 3 decode steps","err_out":2.25483e-07,"err_tail":0,"err_state":3.69095e-07},{"name":"prefill layer 0, T 3, then 1 decode step","err_out":2.10138e-07,"err_tail":0,"err_state":2.54978e-07},{"name":"prefill layer 0, T 41, in 2^-10 out 2^64","err_out":3.04117e-07,"err_tail":0,"err_state":4.02547e-07},{"name":"prefill mutant 1","err_out":0.967332,"err_tail":0,"err_state":2.0048},{"name":"prefill mutant 2","err_out":0.676982,"err_tail":0,"err_state":1.67509},{"name":"prefill mutant 3","err_out":0.968975,"err_tail":0,"err_state":5.46709e-07},{"name":"prefill mutant 4","err_out":3.56939e-07,"err_tail":1.79266,"err_state":5.46709e-07},{"name":"record path, prefill 37 + 1 decode step","err_out":2.19627e-07,"err_tail":0,"err_state":3.05718e-07},{"name":"timing prefill","tokens":512,"reps":5,"prep_us":407.346,"scan_us":1439.799,"post_us":230.689,"chip_us":2077.834,"us_per_token":4.0583,"call_us":5289.769,"insns":[287,534,90],"gprs":[96,104,28]},{"name":"timing prefill","tokens":2048,"reps":5,"prep_us":1566.616,"scan_us":5362.090,"post_us":851.300,"chip_us":7780.006,"us_per_token":3.7988,"call_us":21370.104,"insns":[287,534,90],"gprs":[96,104,28]},{"name":"timing prefill","tokens":8192,"reps":5,"prep_us":12701.679,"scan_us":21624.506,"post_us":7193.459,"chip_us":41519.644,"us_per_token":5.0683,"call_us":97320.190,"insns":[287,534,90],"gprs":[96,104,28]},{"name":"timing","reps":50,"chip_us_median":124.128,"call_us_median":127.073,"state_bytes":6291456 ,"state_gbs":50.69,"nominal_gbs":273,"insns":665,"gprs":116}],"checks":54,"failed":0,"verdict":"PASS"}
diff --git a/evidence/gdn-prefill-20261018 /sim.log b/evidence/gdn-prefill-20261018 /sim.log
new file mode 100644
index 000000000 ..f328b3630
--- /dev/null
+++ b/evidence/gdn-prefill-20261018 /sim.log
@@ -0,0 +1,15 @@
+sim 3 steps err out 2.3e-07 tail 0 state 2.71e-07 (242 ms)
+sim 2 steps, in_scale 2^-10, out_scale 2^64 err out 2.3e-07 tail 0 state 1.67e-07 (131 ms)
+sim mutant 1 (no decay) err out 0.561 tail 0 state 0.652 (131 ms)
+sim mutant 2 (beta 1) err out 0.886 tail 0 state 1.36 (131 ms)
+sim mutant 3 (gate z, not silu z) err out 1.27 tail 0 state 1.67e-07 (128 ms)
+sim mutant 4 (tail not shifted) err out 0.905 tail 1.8 state 0.773 (129 ms)
+sim warp issues: 6601728
+sim prefill T 37 err out 3.31e-07 tail 0 state 2.24e-07 (1150 ms)
+sim prefill T 3 err out 1.92e-07 tail 0 state 2.71e-07 (116 ms)
+sim prefill T 17, in 2^-10, out 2^64 err out 3.31e-07 tail 0 state 2.08e-07 (543 ms)
+sim prefill mutant 1 (no decay) err out 0.801 tail 0 state 1.39 (608 ms)
+sim prefill mutant 2 (beta 1) err out 0.672 tail 0 state 1.31 (602 ms)
+sim prefill mutant 3 (gate z) err out 0.952 tail 0 state 1.98e-07 (598 ms)
+sim prefill mutant 4 (tail kept) err out 3.31e-07 tail 1.79 state 1.98e-07 (596 ms)
+gpu_gdn_test: 26 checks, 0 failed: PASS
diff --git a/src/.crumb b/src/.crumb
index 967498627 ..12f2572a6 100644
--- a/src/.crumb
+++ b/src/.crumb
@@ -182,14 +182,14 @@
}
],
"children_root": "sha256:15012a017b26a269feb256b64875ece2912f5f82c57804baafa3777c4fcb80b9 ",
- "compiled_at": "2026-10-18T13:01:43Z",
+ "compiled_at": "2026-10-18T13:02:58Z",
"compiler": "crumb-compile/1",
- "digest": "sha256:b9cf3d455a68494d3d144976721163f1037ebc8ae64628bae218cb46dfd60129 ",
+ "digest": "sha256:cceb4d16f88ad5ae07eec048bcfbc84bfe1e011f05d9ead33957479fd6dd23bd ",
"evidence_files": 0,
"evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 ",
"files": 541,
- "generated_at_commit": "e0f65ff3157d5214cce0a6b7f35263dd73e3e62c ",
- "source_tree": "sha256:2a38682a0711868c3aa3a757a62e22970b43d5c8807902d4de7c1c90d6f44a07 "
+ "generated_at_commit": "e7356996557da557d63de0f56558a335b5c25223 ",
+ "source_tree": "sha256:ec2bd9c20737f25d4ca6591930cf08010c775a8e6c06e7e961b95569fe49ccc9 "
},
"owner_lane": "",
"provenance": {
diff --git a/src/omega_blackwell_codegen_gdn.c b/src/omega_blackwell_codegen_gdn.c
index 235bace73 ..68185cf38 100644
--- a/src/omega_blackwell_codegen_gdn.c
+++ b/src/omega_blackwell_codegen_gdn.c
@@ -4,6 +4,7 @@
* omega_bw_em.h (the state loads are in flight through the conv, norm and scalar phases).
*/
#include "omega_blackwell_codegen_gdn.h"
+#include "omega_blackwell_codegen_tc.h"
#include <string.h>
#define CW(stall, wbar, rbar, wait) \
@@ -19,10 +20,14 @@
enum { K_FIXED, K_PRED, K_VAR, K_VAR_MORE, K_UVAR, K_STORE, K_SYNC, K_EXIT, K_BRA, K_BSSY, K_BSYNC };
+#define MAX_LOOPS 2
+
typedef struct {
BlackwellIRProgram *p;
uint32_t pending, held;
int err;
+ int depth, open_start[MAX_LOOPS];
+ uint32_t placeholders; /* SHFL.BFLY words omega_tc_patch_code must patch */
} Em;
static void emit(Em *e, BlackwellIRInsn in, int kind) {
@@ -104,6 +109,8 @@ static void fminn(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FMNMX_MIN, d, a, b, R(
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); }
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); }
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); }
+static void bra_if_p0(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.predicate_p0 = true; i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); }
+static int bra_fwd(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = 1; emit(e, i, K_BRA); return at; }
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; }
static void patch_bra(Em *e, int at, int target) { if (!e->err) e->p->insns[at].imm = (uint32_t)(target - at); }
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; }
@@ -119,6 +126,23 @@ static void tail(Em *e) {
typedef struct { int bssy, bra; } Region;
static Region region_skip_if_p0(Em *e) { Region r; r.bssy = bssy_fwd(e); r.bra = bra_if_p0_fwd(e); return r; }
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); }
+/* Loops: a value defined before the loop and read inside it stays live until the back-branch (the linear-scan
+ * allocator knows no back edges; omega_blackwell_codegen_pattn.c). */
+static void loop_begin(Em *e) { if (e->depth >= MAX_LOOPS) { e->err = -1; return; } e->open_start[e->depth++] = here(e); }
+static void loop_end(Em *e) {
+ if (e->depth <= 0) { e->err = -1; return; }
+ int start = e->open_start[--e->depth], end = here(e) - 1;
+ OmegaRegAlloc *ra = &e->p->regalloc;
+ for (int v = 0; v < ra->num_vregs; v++) {
+ OmegaLiveInterval *iv = &ra->intervals[v];
+ if (iv->first_def >= 0 && iv->first_def < start && iv->last_use >= start && iv->last_use < end) iv->last_use = end;
+ }
+}
+static void mov(Em *e, Op d, Op a) { imadi(e, d, a, 1u, NOREG); }
+/* SHFL.BFLY placeholder (omega_blackwell_codegen_tc.h), patched after encoding */
+static void shfl_bfly(Em *e, int d, int a, uint32_t lanes) { shfl_down(e, d, a, OMEGA_TC_MARK_BFLY | (lanes & 31u)); e->placeholders++; }
+/* butterfly sum over lane strides lo, 2 lo .. hi: every lane of a group ends with the same bits (a + b == b + a) */
+static void bfly_sum(Em *e, int v, int t, uint32_t lo, uint32_t hi) { for (uint32_t o = lo; o <= hi; o *= 2) { shfl_bfly(e, t, v, o); fadd(e, R(v), R(v), R(t)); } }
/* warp sum: the total lands in lane 0 */
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)); } }
/* lane 0 of every warp stores v to shared [addr] */
@@ -354,3 +378,394 @@ int omega_gdn_prog_stats(const OmegaGdnSpec *spec, uint32_t *insns, uint32_t *gp
if (gprs) *gprs = prog.regalloc.peak_gpr_usage;
return 0;
}
+
+/* ======== prefill (omega#543): prep, scan, post (layouts in the header) ======== */
+
+/* softplus, decay and beta of one value head (the decode phase C maths): abq = {a, b}, scq = {negA, dt_bias} */
+static void gdn_scalars(Em *e, int MUT, int abq, int scq, int dec, int beta, int t1, int cnl2e, int fone) {
+ int xs = V(e), mn = V(e), sp = V(e), cl2e = V(e);
+ if (e->err) return;
+ fadd(e, R(xs), S(abq, 0), S(scq, 1));
+ movi(e, mn, fbits(-1.0f)); fmul(e, R(mn), R(xs), R(mn)); fminn(e, R(mn), R(xs), R(mn));
+ movi(e, cl2e, fbits(1.44269504f ));
+ 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));
+ mufu(e, BW_IR_MUFU_LG2, R(mn), R(mn));
+ movrz(e, R(sp)); fmaxx(e, R(sp), R(xs), R(sp));
+ movi(e, t1, fbits(0.693147181f )); ffma(e, R(sp), R(mn), R(t1), R(sp));
+ if (MUT == 1) movi(e, dec, fbits(1.0f));
+ 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)); }
+ if (MUT == 2) movi(e, beta, fbits(1.0f));
+ else {
+ fmul(e, R(beta), S(abq, 1), R(cnl2e)); mufu(e, BW_IR_MUFU_EX2, R(beta), R(beta));
+ fadd(e, R(beta), R(beta), R(fone)); mufu(e, BW_IR_MUFU_RCP, R(beta), R(beta));
+ }
+}
+
+int omega_blackwell_codegen_gdn_prep_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog) {
+ if (!spec || !prog || spec->mutant < 0 || spec->mutant > 4) return -1;
+ const int MUT = spec->mutant;
+ const float qs = 0.0883883476f ; /* 1 / sqrt(128) */
+ const uint32_t ROW = OMEGA_GDN_CONV_DIM * 4u, REC_ROW = OMEGA_GDN_VALUE_HEADS * OMEGA_GDN_REC_BYTES;
+ omega_bw_ir_init(prog);
+ Em em; memset(&em, 0, sizeof em); em.p = prog; Em *e = &em;
+
+ int udesc = UV64(e);
+ int c = V(e), cx = V(e), hk = V(e), warp = V(e), lane = V(e), one = V(e), T = V(e), t0 = V(e), n = V(e), t1 = V(e);
+ int pqkv = V64(e), ppar = V64(e), ptail = V64(e), prec = V64(e), pab = V64(e), insc = V(e);
+ int ch[5], tw[5], wq[5], ax[5];
+ for (unsigned i = 0; i < 5; i++) { ch[i] = V(e); tw[i] = V128(e); wq[i] = V128(e); ax[i] = V64(e); }
+ if (e->err) return -1;
+ ldcu64(e, udesc, 0x358);
+ s2r(e, c, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, hk, BW_SR_CTAID_Y);
+ ldc64(e, pqkv, OMEGA_GDN_PREP_ARG_QKV); ldc64(e, ppar, OMEGA_GDN_PREP_ARG_PAR); ldc64(e, ptail, OMEGA_GDN_PREP_ARG_TAIL);
+ ldc64(e, prec, OMEGA_GDN_PREP_ARG_REC); ldc64(e, pab, OMEGA_GDN_PREP_ARG_AB);
+ ldc32(e, T, OMEGA_GDN_PREP_ARG_T); ldc32(e, insc, OMEGA_GDN_PREP_ARG_IN_SCALE);
+ movi(e, one, 1);
+ shr(e, warp, c, 5);
+ andi(e, R(lane), R(c), 31u);
+ imadi(e, R(t0), R(cx), OMEGA_GDN_PREP_TOKENS, NOREG);
+ /* n = min(16, T - t0) >= 1 */
+ imadi(e, R(n), R(t0), 0xffffffffu, T);
+ movi(e, t1, OMEGA_GDN_PREP_TOKENS);
+ isetp_ge_u32(e, t1, n);
+ { int b = bra_if_p0_fwd(e); movi(e, n, OMEGA_GDN_PREP_TOKENS); patch_bra(e, b, here(e)); }
+ /* channels: q hk*128 + c, k 2048 + hk*128 + c, v_i 4096 + (3 hk + i)*128 + c */
+ imadi(e, R(ch[0]), R(hk), OMEGA_GDN_HEAD_DIM, c);
+ imadi(e, R(ch[1]), R(one), 2048u, ch[0]);
+ imadi(e, R(ch[2]), R(hk), 3u * OMEGA_GDN_HEAD_DIM, c); imadi(e, R(ch[2]), R(one), 4096u, ch[2]);
+ imadi(e, R(ch[3]), R(one), OMEGA_GDN_HEAD_DIM, ch[2]);
+ imadi(e, R(ch[4]), R(one), 2u * OMEGA_GDN_HEAD_DIM, ch[2]);
+ /* taps */
+ {
+ int aq = V64(e), av = V64(e);
+ if (e->err) return -1;
+ imadw(e, aq, ch[0], 16u, ppar); imadw(e, av, ch[2], 16u, ppar);
+ ldg_wide(e, BW_IR_LDG_E_128, tw[0], aq, 0, udesc, 1);
+ ldg_wide(e, BW_IR_LDG_E_128, tw[1], aq, 2048u * 16u, udesc, 0);
+ for (unsigned i = 0; i < 3; i++) ldg_wide(e, BW_IR_LDG_E_128, tw[2 + i], av, i * OMEGA_GDN_HEAD_DIM * 16u, udesc, 0);
+ }
+ /* the window {x_{t0-3}, x_{t0-2}, x_{t0-1}}: CTA 0 from the tail, the others from the rows before t0 (uniform) */
+ isetp_ge_u32(e, cx, one);
+ const int b_rows = bra_if_p0_fwd(e);
+ {
+ int at = V64(e);
+ if (e->err) return -1;
+ imadi(e, R(t1), R(hk), 3u * OMEGA_GDN_TAIL_HEAD_BYTES, NOREG);
+ imadw(e, at, t1, 1u, ptail); imadw(e, at, c, 16u, at);
+ ldg_wide(e, BW_IR_LDG_E_128, wq[0], at, 0, udesc, 1);
+ ldg_wide(e, BW_IR_LDG_E_128, wq[1], at, OMEGA_GDN_HEAD_DIM * 16u, udesc, 0);
+ for (unsigned i = 0; i < 3; i++) ldg_wide(e, BW_IR_LDG_E_128, wq[2 + i], at, i * OMEGA_GDN_TAIL_HEAD_BYTES + 256u * 16u, udesc, 0);
+ }
+ const int b_join = bra_fwd(e);
+ patch_bra(e, b_rows, here(e));
+ {
+ int base = V64(e), a[5][3];
+ for (unsigned i = 0; i < 5; i++) for (unsigned r = 0; r < 3; r++) a[i][r] = V64(e);
+ if (e->err) return -1;
+ imadi(e, R(t1), R(one), 0xfffffffdu, t0); /* t0 - 3 (t0 >= 16 here) */
+ imadw(e, base, t1, ROW, pqkv);
+ for (unsigned i = 0; i < 5; i++) {
+ imadw(e, a[i][0], ch[i], 4u, base);
+ imadw(e, a[i][1], one, ROW, a[i][0]); imadw(e, a[i][2], one, ROW, a[i][1]);
+ }
+ for (unsigned i = 0; i < 5; i++) for (unsigned r = 0; r < 3; r++) {
+ BlackwellIRInsn in = I0(BW_IR_LDG_E); in.dst_vreg = wq[i]; in.dst_subreg = (uint8_t)r; in.src1_vreg = a[i][r]; in.ureg = udesc;
+ emit(e, in, (i == 0 && r == 0) ? K_VAR : K_VAR_MORE);
+ }
+ for (unsigned i = 0; i < 5; i++) for (unsigned r = 0; r < 3; r++) fmul(e, S(wq[i], r), S(wq[i], r), R(insc));
+ }
+ patch_bra(e, b_join, here(e));
+
+ /* per-thread addresses at token t0 */
+ int idx = V(e), arec = V64(e), asc = V64(e), aab = V64(e), scq = V64(e);
+ int cnl2e = V(e), fone = V(e), el2 = V(e), cqs = V(e), c3 = V(e), zero = V(e), raq = V(e), rak = V(e);
+ if (e->err) return -1;
+ {
+ int base = V64(e);
+ if (e->err) return -1;
+ imadw(e, base, t0, ROW, pqkv);
+ for (unsigned i = 0; i < 5; i++) imadw(e, ax[i], ch[i], 4u, base);
+ }
+ imadi(e, R(idx), R(t0), OMEGA_GDN_VALUE_HEADS, NOREG); imadi(e, R(idx), R(hk), 3u, idx);
+ imadw(e, arec, idx, OMEGA_GDN_REC_BYTES, prec); imadw(e, arec, c, 4u, arec);
+ imadi(e, R(t1), R(c), 1u, idx); /* the scalar threads' head index t0*48 + 3hk + c */
+ imadw(e, asc, t1, OMEGA_GDN_REC_BYTES, prec);
+ imadw(e, aab, t1, 8u, pab);
+ movi(e, c3, 3u);
+ isetp_ge_u32(e, c, c3);
+ {
+ Region r = region_skip_if_p0(e);
+ int as = V64(e);
+ if (e->err) return -1;
+ imadi(e, R(t1), R(hk), 3u, c);
+ imadw(e, as, t1, 8u, ppar);
+ ldg_wide(e, BW_IR_LDG_E_64, scq, as, OMEGA_GDN_PAR_SCAL, udesc, 1);
+ region_end(e, r);
+ }
+ movi(e, cnl2e, fbits(-1.44269504f )); movi(e, fone, fbits(1.0f)); movi(e, cqs, fbits(qs));
+ ldc32(e, el2, OMEGA_GDN_PREP_ARG_EPS_L2);
+ movi(e, zero, 0u);
+ imadi(e, R(raq), R(warp), 4u, NOREG); imadi(e, R(rak), R(one), 16u, raq);
+
+ const int L = here(e);
+ loop_begin(e);
+ {
+ int x[5], acc[5], sq = V(e), sk = V(e), ts = V(e), rq = V128(e), rk = V128(e), nq = V(e), nk = V(e);
+ for (unsigned i = 0; i < 5; i++) { x[i] = V(e); acc[i] = V(e); }
+ if (e->err) return -1;
+ for (unsigned i = 0; i < 5; i++) ldg32(e, x[i], ax[i], udesc, i == 0);
+ for (unsigned i = 0; i < 5; i++) {
+ fmul(e, R(x[i]), R(x[i]), R(insc));
+ fmul(e, R(acc[i]), S(tw[i], 0), S(wq[i], 0)); ffma(e, R(acc[i]), S(tw[i], 1), S(wq[i], 1), R(acc[i]));
+ ffma(e, R(acc[i]), S(tw[i], 2), S(wq[i], 2), R(acc[i])); ffma(e, R(acc[i]), S(tw[i], 3), R(x[i]), R(acc[i]));
+ silu(e, R(acc[i]), t1, cnl2e, fone);
+ mov(e, S(wq[i], 0), S(wq[i], 1)); mov(e, S(wq[i], 1), S(wq[i], 2)); mov(e, S(wq[i], 2), R(x[i]));
+ }
+ /* l2 norms of q and k across the 4 warps (the decode order: warp sums, then partials 0 + 1 + 2 + 3) */
+ fmul(e, R(sq), R(acc[0]), R(acc[0])); warp_sum(e, sq, ts); lane0_sts(e, lane, one, raq, R(sq));
+ fmul(e, R(sk), R(acc[1]), R(acc[1])); warp_sum(e, sk, ts); lane0_sts(e, lane, one, rak, R(sk));
+ bar_sync(e);
+ lds128(e, rq, zero, 0, 1); lds128(e, rk, zero, 16, 0);
+ fadd(e, R(nq), S(rq, 0), S(rq, 1)); fadd(e, R(nq), R(nq), S(rq, 2)); fadd(e, R(nq), R(nq), S(rq, 3)); fadd(e, R(nq), R(nq), R(el2));
+ fadd(e, R(nk), S(rk, 0), S(rk, 1)); fadd(e, R(nk), R(nk), S(rk, 2)); fadd(e, R(nk), R(nk), S(rk, 3)); fadd(e, R(nk), R(nk), R(el2));
+ mufu(e, BW_IR_MUFU_RSQ, R(nq), R(nq)); mufu(e, BW_IR_MUFU_RSQ, R(nk), R(nk));
+ fmul(e, R(acc[0]), R(acc[0]), R(nq)); fmul(e, R(acc[0]), R(acc[0]), R(cqs));
+ fmul(e, R(acc[1]), R(acc[1]), R(nk));
+ for (unsigned i = 0; i < 3; i++) {
+ stg(e, arec, i * OMEGA_GDN_REC_BYTES, R(acc[0]), udesc);
+ stg(e, arec, i * OMEGA_GDN_REC_BYTES + OMEGA_GDN_REC_K, R(acc[1]), udesc);
+ stg(e, arec, i * OMEGA_GDN_REC_BYTES + OMEGA_GDN_REC_V, R(acc[2 + i]), udesc);
+ }
+ /* threads 0..2: decay and beta of value head 3 hk + c */
+ isetp_ge_u32(e, c, c3);
+ {
+ Region r = region_skip_if_p0(e);
+ int abq = V64(e), dec = V(e), beta = V(e);
+ if (e->err) return -1;
+ ldg_wide(e, BW_IR_LDG_E_64, abq, aab, 0, udesc, 1);
+ gdn_scalars(e, MUT, abq, scq, dec, beta, t1, cnl2e, fone);
+ stg(e, asc, OMEGA_GDN_REC_SC, R(dec), udesc); stg(e, asc, OMEGA_GDN_REC_SC + 4u, R(beta), udesc);
+ region_end(e, r);
+ }
+ bar_sync(e); /* every warp has read the partials before the next token writes them */
+ for (unsigned i = 0; i < 5; i++) imadw(e, ax[i], one, ROW, ax[i]);
+ imadw(e, arec, one, REC_ROW, arec); imadw(e, asc, one, REC_ROW, asc);
+ imadw(e, aab, one, OMEGA_GDN_VALUE_HEADS * 8u, aab);
+ imadi(e, R(n), R(one), 0xffffffffu, n);
+ isetp_ge_u32(e, n, one);
+ bra_if_p0(e, L);
+ }
+ loop_end(e);
+ tail(e);
+ if (e->err) return -1;
+ return omega_bw_regalloc_solve(prog);
+}
+
+int omega_blackwell_codegen_gdn_scan_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog) {
+ if (!spec || !prog || spec->mutant < 0 || spec->mutant > 4) return -1;
+ const int MUT = spec->mutant;
+ const uint32_t ROW = OMEGA_GDN_CONV_DIM * 4u, CHUNK_ROW = OMEGA_GDN_SCAN_CHUNK * OMEGA_GDN_VALUE_HEADS * OMEGA_GDN_REC_BYTES;
+ omega_bw_ir_init(prog);
+ Em em; memset(&em, 0, sizeof em); em.p = prog; Em *e = &em;
+
+ int udesc = UV64(e);
+ int tid = V(e), cx = V(e), cy = V(e), h = V(e), warp = V(e), lane = V(e), one = V(e), cq = V(e), rg = V(e), c0 = V(e);
+ int T = V(e), tm1 = V(e), t = V(e), t1 = V(e), sl = V(e), srec = V(e);
+ int ps = V64(e), sa = V64(e), prec = V64(e), grec = V64(e), gsc = V64(e), po = V64(e), sq[16];
+ for (unsigned r = 0; r < 16; r++) sq[r] = V128(e);
+ if (e->err) return -1;
+ ldcu64(e, udesc, 0x358);
+ s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y);
+ ldc64(e, ps, OMEGA_GDN_SCAN_ARG_STATE); ldc64(e, prec, OMEGA_GDN_SCAN_ARG_REC); ldc64(e, po, OMEGA_GDN_SCAN_ARG_O);
+ ldc32(e, T, OMEGA_GDN_SCAN_ARG_T);
+ movi(e, one, 1);
+ imadi(e, R(h), R(cy), 3u, cx);
+ shr(e, warp, tid, 5);
+ andi(e, R(lane), R(tid), 31u);
+ andi(e, R(cq), R(lane), 3u);
+ shr(e, rg, lane, 2);
+ /* this thread's block of S: rows 16 rg .. +15, columns c0 = 16 warp + 4 cq .. +3 */
+ imadi(e, R(c0), R(warp), 16u, NOREG); imadi(e, R(c0), R(cq), 4u, c0);
+ imadi(e, R(t1), R(h), OMEGA_GDN_STATE_HEAD_BYTES, NOREG);
+ imadi(e, R(t1), R(rg), 16u * OMEGA_GDN_HEAD_DIM * 4u, t1);
+ imadi(e, R(t1), R(c0), 4u, t1);
+ imadw(e, sa, t1, 1u, ps);
+ for (unsigned r = 0; r < 16; r++) ldg_wide(e, BW_IR_LDG_E_128, sq[r], sa, r * OMEGA_GDN_HEAD_DIM * 4u, udesc, r == 0);
+ imadi(e, R(tm1), R(one), 0xffffffffu, T);
+ /* the record copy: warp w brings token tc + w into shared slot w */
+ imadi(e, R(t1), R(warp), OMEGA_GDN_VALUE_HEADS, h);
+ imadw(e, gsc, t1, OMEGA_GDN_REC_BYTES, prec);
+ imadw(e, grec, lane, 16u, gsc);
+ imadi(e, R(srec), R(warp), OMEGA_GDN_REC_BYTES, NOREG);
+ imadi(e, R(sl), R(lane), 16u, srec);
+ /* o row of (token 0, head h), columns c0 .. c0 + 3 */
+ imadi(e, R(t1), R(h), OMEGA_GDN_HEAD_DIM, c0);
+ imadw(e, po, t1, 4u, po);
+ movrz(e, R(t));
+
+ const int L = here(e);
+ loop_begin(e);
+ {
+ int tm = V(e), sb = V(e), ak = V(e), av = V(e), scq = V128(e), vq = V128(e), ts = V(e), kv[4], dl[4], p[4];
+ for (unsigned j = 0; j < 4; j++) { kv[j] = V(e); dl[j] = V(e); p[j] = V(e); }
+ if (e->err) return -1;
+ andi(e, R(tm), R(t), OMEGA_GDN_SCAN_CHUNK - 1u);
+ isetp_ge_u32(e, tm, one);
+ const int b_copy = bra_if_p0_fwd(e);
+ {
+ int rr[4], a = V(e);
+ for (unsigned k = 0; k < 4; k++) rr[k] = V128(e);
+ if (e->err) return -1;
+ bar_sync(e); /* every warp is done with the last chunk */
+ for (unsigned k = 0; k < 3; k++) ldg_wide(e, BW_IR_LDG_E_128, rr[k], grec, k * 512u, udesc, k == 0);
+ ldg_wide(e, BW_IR_LDG_E_128, rr[3], gsc, OMEGA_GDN_REC_SC, udesc, 0);
+ for (unsigned k = 0; k < 3; k++) for (unsigned j = 0; j < 4; j++) {
+ if (k == 0 && j == 0) sts32(e, sl, S(rr[0], 0));
+ else { imadi(e, R(a), R(one), k * 512u + j * 4u, sl); sts32(e, a, S(rr[k], j)); }
+ }
+ for (unsigned j = 0; j < 4; j++) { imadi(e, R(a), R(one), OMEGA_GDN_REC_SC + j * 4u, srec); sts32(e, a, S(rr[3], j)); }
+ bar_sync(e);
+ imadw(e, grec, one, CHUNK_ROW, grec); imadw(e, gsc, one, CHUNK_ROW, gsc);
+ }
+ patch_bra(e, b_copy, here(e));
+ imadi(e, R(sb), R(tm), OMEGA_GDN_REC_BYTES, NOREG);
+ imadi(e, R(ak), R(rg), 16u * 4u, sb);
+ imadi(e, R(av), R(c0), 4u, sb);
+ lds128(e, scq, sb, OMEGA_GDN_REC_SC, 1);
+ for (unsigned r = 0; r < 16; r++) for (unsigned j = 0; j < 4; j++) fmul(e, S(sq[r], j), S(sq[r], j), S(scq, 0));
+ /* kv_j = sum over 128 rows of S[r][j] k[r]: 16 rows here, then lanes ^ 4, 8, 16 (the 8 row groups) */
+ for (unsigned i = 0; i < 4; i++) {
+ int kq = V128(e);
+ if (e->err) return -1;
+ lds128(e, kq, ak, OMEGA_GDN_REC_K + i * 16u, 1);
+ for (unsigned r = 4 * i; r < 4 * i + 4; r++) for (unsigned j = 0; j < 4; j++) {
+ if (r == 0) fmul(e, R(kv[j]), S(sq[0], j), S(kq, 0));
+ else ffma(e, R(kv[j]), S(sq[r], j), S(kq, r % 4), R(kv[j]));
+ }
+ }
+ for (unsigned j = 0; j < 4; j++) bfly_sum(e, kv[j], ts, 4u, 16u);
+ lds128(e, vq, av, OMEGA_GDN_REC_V, 1);
+ 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]), S(scq, 1)); }
+ for (unsigned i = 0; i < 4; i++) {
+ int kq = V128(e);
+ if (e->err) return -1;
+ lds128(e, kq, ak, OMEGA_GDN_REC_K + i * 16u, 1);
+ for (unsigned r = 4 * i; r < 4 * i + 4; r++) for (unsigned j = 0; j < 4; j++) ffma(e, S(sq[r], j), S(kq, r % 4), R(dl[j]), S(sq[r], j));
+ }
+ for (unsigned i = 0; i < 4; i++) {
+ int qq = V128(e);
+ if (e->err) return -1;
+ lds128(e, qq, ak, i * 16u, 1);
+ for (unsigned r = 4 * i; r < 4 * i + 4; r++) for (unsigned j = 0; j < 4; j++) {
+ if (r == 0) fmul(e, R(p[j]), S(sq[0], j), S(qq, 0));
+ else ffma(e, R(p[j]), S(sq[r], j), S(qq, r % 4), R(p[j]));
+ }
+ }
+ for (unsigned j = 0; j < 4; j++) bfly_sum(e, p[j], ts, 4u, 16u);
+ isetp_ge_u32(e, rg, one);
+ {
+ Region r = region_skip_if_p0(e);
+ for (unsigned j = 0; j < 4; j++) stg(e, po, j * 4u, R(p[j]), udesc);
+ region_end(e, r);
+ }
+ imadw(e, po, one, OMEGA_GDN_V_DIM * 4u, po);
+ imadi(e, R(t), R(one), 1u, t);
+ isetp_ge_u32(e, tm1, t);
+ bra_if_p0(e, L);
+ }
+ loop_end(e);
+ 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);
+
+ /* the new tail: qkv rows T-3 .. T-1 (times in_scale) of slot A (tid) and slot V (256 + (tid & 127)), as decode */
+ if (MUT != 4) {
+ int half = V(e), cc = V(e), gA = V(e), gV = V(e), insc = V(e), pqkv = V64(e), ptail = V64(e), base = V64(e);
+ int atA = V64(e), atV = V64(e), aA[3], aV[3], xA[3], xV[3];
+ for (unsigned r = 0; r < 3; r++) { aA[r] = V64(e); aV[r] = V64(e); xA[r] = V(e); xV[r] = V(e); }
+ if (e->err) return -1;
+ ldc64(e, pqkv, OMEGA_GDN_SCAN_ARG_QKV); ldc64(e, ptail, OMEGA_GDN_SCAN_ARG_TAIL); ldc32(e, insc, OMEGA_GDN_SCAN_ARG_IN_SCALE);
+ shr(e, half, tid, 7);
+ andi(e, R(cc), R(tid), 127u);
+ imadi(e, R(gA), R(cy), OMEGA_GDN_HEAD_DIM, tid); imadi(e, R(gA), R(half), 2048u - 128u, gA);
+ imadi(e, R(gV), R(h), OMEGA_GDN_HEAD_DIM, cc); imadi(e, R(gV), R(one), 2u * OMEGA_GDN_KEY_HEADS * OMEGA_GDN_HEAD_DIM, gV);
+ imadi(e, R(t1), R(one), 0xfffffffdu, T);
+ imadw(e, base, t1, ROW, pqkv);
+ imadw(e, aA[0], gA, 4u, base); imadw(e, aV[0], gV, 4u, base);
+ for (unsigned r = 1; r < 3; r++) { imadw(e, aA[r], one, ROW, aA[r - 1]); imadw(e, aV[r], one, ROW, aV[r - 1]); }
+ for (unsigned r = 0; r < 3; r++) { ldg32(e, xA[r], aA[r], udesc, r == 0); ldg32(e, xV[r], aV[r], udesc, 0); }
+ for (unsigned r = 0; r < 3; r++) { fmul(e, R(xA[r]), R(xA[r]), R(insc)); fmul(e, R(xV[r]), R(xV[r]), R(insc)); }
+ imadw(e, atA, tid, 16u, ptail); imadw(e, atA, h, OMEGA_GDN_TAIL_HEAD_BYTES, atA);
+ imadi(e, R(t1), R(one), 256u, cc);
+ imadw(e, atV, t1, 16u, ptail); imadw(e, atV, h, OMEGA_GDN_TAIL_HEAD_BYTES, atV);
+ for (unsigned r = 0; r < 3; r++) { stg(e, atA, 4u * r, R(xA[r]), udesc); stg(e, atV, 4u * r, R(xV[r]), udesc); }
+ }
+ tail(e);
+ if (e->err) return -1;
+ return omega_bw_regalloc_solve(prog);
+}
+
+int omega_blackwell_codegen_gdn_post_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog) {
+ if (!spec || !prog || spec->mutant < 0 || spec->mutant > 4) return -1;
+ const int MUT = spec->mutant;
+ omega_bw_ir_init(prog);
+ Em em; memset(&em, 0, sizeof em); em.p = prog; Em *e = &em;
+
+ int udesc = UV64(e);
+ int tid = V(e), t = V(e), cy = V(e), warp = V(e), lane = V(e), idx = V(e), ss = V(e), ts = V(e), g = V(e), t1 = V(e);
+ int en = V(e), osc = V(e), insc = V(e), cinv = V(e), cnl2e = V(e), fone = V(e);
+ int po = V64(e), pz = V64(e), ppar = V64(e), pout = V64(e), ao = V64(e), az = V64(e), an = V64(e), ay = V64(e);
+ int oq = V128(e), zq = V128(e), nq = V128(e);
+ if (e->err) return -1;
+ ldcu64(e, udesc, 0x358);
+ s2r(e, tid, BW_SR_TID_X); s2r(e, t, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y);
+ ldc64(e, po, OMEGA_GDN_POST_ARG_O); ldc64(e, pz, OMEGA_GDN_POST_ARG_Z); ldc64(e, ppar, OMEGA_GDN_POST_ARG_PAR);
+ ldc64(e, pout, OMEGA_GDN_POST_ARG_OUT);
+ ldc32(e, en, OMEGA_GDN_POST_ARG_EPS_NORM); ldc32(e, insc, OMEGA_GDN_POST_ARG_IN_SCALE); ldc32(e, osc, OMEGA_GDN_POST_ARG_OUT_SCALE);
+ shr(e, warp, tid, 5);
+ andi(e, R(lane), R(tid), 31u);
+ /* row (token t, head 8 cy + warp): o, z and out share the [T][48][128] layout */
+ imadi(e, R(idx), R(cy), 8u, warp);
+ imadi(e, R(idx), R(t), OMEGA_GDN_VALUE_HEADS, idx);
+ imadw(e, ao, idx, OMEGA_GDN_HEAD_DIM * 4u, po); imadw(e, ao, lane, 16u, ao);
+ imadw(e, az, idx, OMEGA_GDN_HEAD_DIM * 4u, pz); imadw(e, az, lane, 16u, az);
+ imadw(e, ay, idx, OMEGA_GDN_HEAD_DIM * 4u, pout); imadw(e, ay, lane, 16u, ay);
+ imadw(e, an, lane, 16u, ppar);
+ ldg_wide(e, BW_IR_LDG_E_128, oq, ao, 0, udesc, 1);
+ ldg_wide(e, BW_IR_LDG_E_128, zq, az, 0, udesc, 0);
+ ldg_wide(e, BW_IR_LDG_E_128, nq, an, OMEGA_GDN_PAR_NORM, udesc, 0);
+ fmul(e, R(ss), S(oq, 0), S(oq, 0));
+ for (unsigned j = 1; j < 4; j++) ffma(e, R(ss), S(oq, j), S(oq, j), R(ss));
+ bfly_sum(e, ss, ts, 1u, 16u);
+ movi(e, cinv, fbits(1.0f / 128.0f));
+ ffma(e, R(ss), R(ss), R(cinv), R(en));
+ mufu(e, BW_IR_MUFU_RSQ, R(ss), R(ss));
+ movi(e, cnl2e, fbits(-1.44269504f )); movi(e, fone, fbits(1.0f));
+ for (unsigned j = 0; j < 4; j++) {
+ fmul(e, R(g), S(zq, j), R(insc));
+ if (MUT != 3) silu(e, R(g), t1, cnl2e, fone);
+ fmul(e, S(oq, j), S(oq, j), R(ss)); fmul(e, S(oq, j), S(oq, j), S(nq, j));
+ fmul(e, S(oq, j), S(oq, j), R(g)); fmul(e, S(oq, j), S(oq, j), R(osc));
+ }
+ for (unsigned j = 0; j < 4; j++) stg(e, ay, j * 4u, S(oq, j), udesc);
+ tail(e);
+ if (e->err) return -1;
+ return omega_bw_regalloc_solve(prog);
+}
+
+/* number of SHFL.BFLY placeholders in a program (what omega_tc_patch_code must report) */
+uint32_t omega_gdn_bfly_count(const BlackwellIRProgram *prog) {
+ uint32_t n = 0;
+ for (size_t i = 0; i < prog->count; i++)
+ if (prog->insns[i].op == BW_IR_SHFL_DOWN && (prog->insns[i].imm & OMEGA_TC_MARK_BFLY)) n++;
+ return n;
+}
+
+int omega_gdn_pf_prog_stats(unsigned kind, const OmegaGdnSpec *spec, uint32_t *insns, uint32_t *gprs) {
+ static BlackwellIRProgram prog;
+ int rc = kind == 0 ? omega_blackwell_codegen_gdn_prep_prog(spec, &prog)
+ : kind == 1 ? omega_blackwell_codegen_gdn_scan_prog(spec, &prog)
+ : kind == 2 ? omega_blackwell_codegen_gdn_post_prog(spec, &prog) : -1;
+ if (rc != 0) return -1;
+ if (insns) *insns = (uint32_t)prog.count;
+ if (gprs) *gprs = prog.regalloc.peak_gpr_usage;
+ return 0;
+}
diff --git a/src/omega_blackwell_codegen_gdn.h b/src/omega_blackwell_codegen_gdn.h
index 10b7079d9 ..8015cc833 100644
--- a/src/omega_blackwell_codegen_gdn.h
+++ b/src/omega_blackwell_codegen_gdn.h
@@ -89,4 +89,61 @@ int omega_blackwell_codegen_gdn_prog(const OmegaGdnSpec *spec, BlackwellIRProgra
/* Instruction and peak-register counts (host, no device). */
int omega_gdn_prog_stats(const OmegaGdnSpec *spec, uint32_t *insns, uint32_t *gprs);
+/* ---- prefill (omega#543, 2026-10-18): three launches per layer over T >= 3 new tokens, the same tail and state ----
+ * 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
+ * of the three value heads), sliding each window in registers over 16 tokens (the CTA starting at token 0 takes
+ * its window from the layer's tail, the others from the qkv rows before it), then SiLU, the q/k l2 norms (two
+ * barriers per token) and, threads 0..2, the scalars of the three value heads. It writes one record per (token,
+ * value head): q^ | k^ | v | {decay, beta, 0, 0}. It reads the tail and never writes it.
+ * scan grid 3 x 16 (one CTA per value head), 256 threads. Thread (warp w, lane L) holds S rows 16 (L >> 2) .. +15,
+ * columns 16 w + 4 (L & 3) .. +3 in registers for the whole prompt. Per 8-token chunk every warp copies one
+ * token's record into shared memory (two barriers); per token kv and o are summed across the 8 row groups by
+ * SHFL.BFLY (lanes ^ 4, 8, 16; a butterfly gives every lane the same bits), no barrier. It writes o (before the
+ * norm) per token, the state at the end and the new tail (the last three qkv rows).
+ * post grid (T, 6), 256 threads: warp w normalises (token, head 8 ctaid.y + w): gated RMSNorm, silu(z), out_scale.
+ * The mutants keep the decode meanings: 1 and 2 act in prep, 3 in post, 4 in scan. */
+#define OMEGA_GDN_REC_BYTES 1552u
+#define OMEGA_GDN_REC_K 512u
+#define OMEGA_GDN_REC_V 1024u
+#define OMEGA_GDN_REC_SC 1536u
+#define OMEGA_GDN_PF_MIN_T 3u
+#define OMEGA_GDN_PREP_TOKENS 16u
+#define OMEGA_GDN_PREP_THREADS 128u
+#define OMEGA_GDN_PREP_SHARED_BYTES 32u /* per-warp sums of squares: q [4] | k [4] */
+#define OMEGA_GDN_PREP_ARG_QKV 0x380u /* f32 [T][10240] (times in_scale) */
+#define OMEGA_GDN_PREP_ARG_AB 0x388u /* f32 [T][48][2] */
+#define OMEGA_GDN_PREP_ARG_PAR 0x390u
+#define OMEGA_GDN_PREP_ARG_TAIL 0x398u
+#define OMEGA_GDN_PREP_ARG_REC 0x3a0u /* [T rounded up to 8][48] records */
+#define OMEGA_GDN_PREP_ARG_T 0x3a8u /* u32 */
+#define OMEGA_GDN_PREP_ARG_EPS_L2 0x3acu
+#define OMEGA_GDN_PREP_ARG_IN_SCALE 0x3b0u
+#define OMEGA_GDN_PREP_ARGS_WORDS 13u
+#define OMEGA_GDN_SCAN_CHUNK 8u
+#define OMEGA_GDN_SCAN_SHARED_BYTES (OMEGA_GDN_SCAN_CHUNK * OMEGA_GDN_REC_BYTES) /* 12416 */
+#define OMEGA_GDN_SCAN_ARG_REC 0x380u
+#define OMEGA_GDN_SCAN_ARG_QKV 0x388u
+#define OMEGA_GDN_SCAN_ARG_TAIL 0x390u
+#define OMEGA_GDN_SCAN_ARG_STATE 0x398u
+#define OMEGA_GDN_SCAN_ARG_O 0x3a0u /* f32 [T][48][128] */
+#define OMEGA_GDN_SCAN_ARG_T 0x3a8u
+#define OMEGA_GDN_SCAN_ARG_IN_SCALE 0x3acu
+#define OMEGA_GDN_SCAN_ARGS_WORDS 12u
+#define OMEGA_GDN_POST_ARG_O 0x380u
+#define OMEGA_GDN_POST_ARG_Z 0x388u /* f32 [T][6144] (times in_scale) */
+#define OMEGA_GDN_POST_ARG_PAR 0x390u
+#define OMEGA_GDN_POST_ARG_OUT 0x398u /* f32 [T][6144] */
+#define OMEGA_GDN_POST_ARG_EPS_NORM 0x3a0u
+#define OMEGA_GDN_POST_ARG_IN_SCALE 0x3a4u
+#define OMEGA_GDN_POST_ARG_OUT_SCALE 0x3a8u
+#define OMEGA_GDN_POST_ARGS_WORDS 11u
+#define OMEGA_GDN_POST_GRID_Y 6u
+
+int omega_blackwell_codegen_gdn_prep_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog);
+int omega_blackwell_codegen_gdn_scan_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog);
+int omega_blackwell_codegen_gdn_post_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog);
+/* SHFL.BFLY placeholders in a built program; prefill stats: kind 0 prep, 1 scan, 2 post. */
+uint32_t omega_gdn_bfly_count(const BlackwellIRProgram *prog);
+int omega_gdn_pf_prog_stats(unsigned kind, const OmegaGdnSpec *spec, uint32_t *insns, uint32_t *gprs);
+
#endif
diff --git a/src/omega_gpu_gdn_api.c b/src/omega_gpu_gdn_api.c
index 43ea540cd ..ca341182c 100644
--- a/src/omega_gpu_gdn_api.c
+++ b/src/omega_gpu_gdn_api.c
@@ -7,6 +7,7 @@
#include "omega_gpu_hostmath.h"
#include "omega_gpu_session.h"
#include "omega_gpu_cmdlist.h"
+#include "omega_blackwell_codegen_tc.h"
#include <math.h>
#include <stdbool.h>
#include <string.h>
@@ -27,6 +28,7 @@ struct OmegaGpuGdn {
OmegaGpuMem tail; /* [layers][OMEGA_GDN_TAIL_BYTES] */
OmegaGpuMem state; /* [layers][OMEGA_GDN_STATE_BYTES] */
OmegaGpuMem lb; /* launch buffer */
+ OmegaGpuScratch pf; /* prefill scratch (see pf_layout) */
};
typedef struct {
@@ -37,15 +39,20 @@ typedef struct {
uint32_t insns;
} Slot;
+/* the resident kernels: decode, prefill prep, scan, post */
+enum { KK_DECODE, KK_PREP, KK_SCAN, KK_POST, KK_N };
+static const uint32_t kk_threads[KK_N] = { OMEGA_GDN_THREADS, OMEGA_GDN_PREP_THREADS, OMEGA_GDN_THREADS, OMEGA_GDN_THREADS };
static struct {
bool hooked;
- Slot slot;
+ Slot slot[KK_N];
int mutant;
} g_k;
static int g_rec; /* 1 while a record call runs (lock held): kernel_for must not evict */
-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); }
-static void on_session_close(void) { memset(&g_k.slot, 0, sizeof g_k.slot); }
+static void slot_clear_locked(void) {
+ for (unsigned k = 0; k < KK_N; k++) { if (g_k.slot[k].valid) omega_gpu_session_free(&g_k.slot[k].code); memset(&g_k.slot[k], 0, sizeof g_k.slot[k]); }
+}
+static void on_session_close(void) { memset(g_k.slot, 0, sizeof g_k.slot); }
static OmegaGpuSession *dev_open_locked(void) {
if (!g_k.hooked) { (void)omega_gpu_session_on_close(on_session_close); g_k.hooked = true; }
return omega_gpu_session_open();
@@ -143,6 +150,7 @@ void omega_gpu_gdn_free(OmegaGpuGdn *g) {
if (g->tail.cpu) omega_gpu_session_free(&g->tail);
if (g->state.cpu) omega_gpu_session_free(&g->state);
if (g->lb.cpu) omega_gpu_session_free(&g->lb);
+ omega_gpu_session_scratch_free(&g->pf);
omega_gpu_session_unlock();
omega_gpu_host_free(g);
}
@@ -193,24 +201,37 @@ int omega_gpu_gdn_get_state(OmegaGpuGdn *g, uint32_t layer, float *tail, float *
}
/* ---- kernel ---- */
-static int kernel_for(const OmegaGdnSpec *spec, Slot **out, bool *hit) {
- Slot *s = &g_k.slot;
+static int kernel_build(unsigned kind, const OmegaGdnSpec *spec, BlackwellIRProgram *prog) {
+ switch (kind) {
+ case KK_DECODE: return omega_blackwell_codegen_gdn_prog(spec, prog);
+ case KK_PREP: return omega_blackwell_codegen_gdn_prep_prog(spec, prog);
+ case KK_SCAN: return omega_blackwell_codegen_gdn_scan_prog(spec, prog);
+ case KK_POST: return omega_blackwell_codegen_gdn_post_prog(spec, prog);
+ default: return -1;
+ }
+}
+static int kernel_for(unsigned kind, const OmegaGdnSpec *spec, Slot **out, bool *hit) {
+ Slot *s = &g_k.slot[kind];
if (s->valid && memcmp(&s->spec, spec, sizeof *spec) == 0) { *out = s; *hit = true; return OMEGA_GPU_GDN_OK; }
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; }
- slot_clear_locked();
+ if (s->valid) omega_gpu_session_free(&s->code);
+ memset(s, 0, sizeof *s);
BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog);
if (!prog) { omega_gpu_session_set_error("gdn codegen: host memory"); return OMEGA_GPU_GDN_CODEGEN_FAIL; }
- int cg = omega_blackwell_codegen_gdn_prog(spec, prog);
+ int cg = kernel_build(kind, spec, prog);
size_t need = cg == 0 ? omega_bw_program_code_bytes(prog) : 0;
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; }
- 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; }
- const uint32_t insns = (uint32_t)prog->count;
+ if ((uint64_t)kk_threads[kind] * 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; }
+ const uint32_t insns = (uint32_t)prog->count, expect = omega_gdn_bfly_count(prog);
OmegaGpuMem code;
if (omega_gpu_session_alloc_code(need, &code) != 0) { omega_gpu_host_free(prog); return OMEGA_GPU_GDN_CHIP_FAIL; }
OmegaBlackwellKernel kernel;
cg = omega_blackwell_kernel_encode_into(prog, code.cpu, need, &kernel);
+ /* SHFL.BFLY placeholders become real butterflies (omega_blackwell_codegen_tc.h); the count must match */
+ const int np = cg == 0 ? omega_tc_patch_code(prog, code.cpu, need) : -1;
omega_gpu_host_free(prog);
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; }
+ if (np < 0 || (uint32_t)np != expect) { omega_gpu_session_free(&code); omega_gpu_session_set_error("gdn: butterfly patch count mismatch"); return OMEGA_GPU_GDN_CODEGEN_FAIL; }
if (omega_gpu_session_sync_to_gpu(&code) != 0) { omega_gpu_session_free(&code); return OMEGA_GPU_GDN_CHIP_FAIL; }
memset(s, 0, sizeof *s);
s->valid = true; s->spec = *spec; s->kernel = kernel; s->code = code; s->insns = insns;
@@ -236,7 +257,7 @@ static int gdn_core(OmegaGpuGdn *g, uint32_t layer, const float *qkv, const floa
if (!dev_open_locked()) { UNLOCK(); return OMEGA_GPU_GDN_CHIP_FAIL; }
Slot *ks = NULL; bool hit = false;
g_rec = rec != NULL;
- rc = kernel_for(&spec, &ks, &hit);
+ rc = kernel_for(KK_DECODE, &spec, &ks, &hit);
g_rec = 0;
if (rc != OMEGA_GPU_GDN_OK) { UNLOCK(); return rc; }
uint8_t *b = g->lb.cpu;
@@ -309,3 +330,144 @@ int omega_gpu_gdn_read_out(OmegaGpuGdn *g, float *out) {
omega_gpu_session_unlock();
return rc;
}
+
+/* ---- prefill (omega#543) ---- */
+/* scratch sections for T tokens (bytes, 256-aligned): qkv [T][10240] | z [T][6144] | ab [T][48][2] | out [T][6144] |
+ * o [T][6144] | records [T rounded up to 8][48] */
+enum { PF_QKV, PF_Z, PF_AB, PF_OUT, PF_O, PF_REC, PF_N };
+static size_t pf_layout(uint32_t T, size_t off[PF_N]) {
+ const uint64_t T8 = ((uint64_t)T + OMEGA_GDN_SCAN_CHUNK - 1u) / OMEGA_GDN_SCAN_CHUNK * OMEGA_GDN_SCAN_CHUNK;
+ const uint64_t sz[PF_N] = { (uint64_t)T * OMEGA_GDN_CONV_DIM * 4u, (uint64_t)T * OMEGA_GDN_V_DIM * 4u, (uint64_t)T * OMEGA_GDN_VALUE_HEADS * 8u,
+ (uint64_t)T * OMEGA_GDN_V_DIM * 4u, (uint64_t)T * OMEGA_GDN_V_DIM * 4u, T8 * OMEGA_GDN_VALUE_HEADS * OMEGA_GDN_REC_BYTES };
+ size_t at = 0;
+ for (unsigned i = 0; i < PF_N; i++) { off[i] = at; at += (size_t)((sz[i] + 255u) & ~(uint64_t)255u); }
+ return at;
+}
+
+int omega_gpu_gdn_prefill_reserve(OmegaGpuGdn *g, uint32_t max_tokens) {
+ if (!g || max_tokens < OMEGA_GDN_PF_MIN_T || max_tokens > OMEGA_GPU_GDN_PF_MAX_T) return OMEGA_GPU_GDN_BAD_ARGS;
+ size_t off[PF_N];
+ const size_t bytes = pf_layout(max_tokens, off);
+ omega_gpu_session_lock();
+ int rc = OMEGA_GPU_GDN_OK;
+ if (!dev_open_locked()) rc = OMEGA_GPU_GDN_CHIP_FAIL;
+ else {
+ omega_gpu_session_scratch_unreserve(&g->pf);
+ if (omega_gpu_session_scratch_reserve(&g->pf, bytes) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL;
+ }
+ omega_gpu_session_unlock();
+ return rc;
+}
+
+/* rec non-NULL (record path, caller holds the lock): the inputs are device rows, the scratch must already hold T
+ * tokens (omega_gpu_gdn_prefill_reserve), the three launches are appended to rec. */
+static int pf_core(OmegaGpuGdn *g, uint32_t layer, uint32_t T, const float *qkv, const float *z, const float *ab, float eps_l2,
+ float eps_norm, float in_scale, float out_scale, float *out, OmegaGpuGdnPfInfo *info,
+ OmegaGpuCmdList *rec, uint64_t dev_qkv, uint64_t dev_z, uint64_t dev_ab, uint64_t *out_va) {
+ uint64_t c0 = omega_gpu_session_now_ns();
+ if (info) memset(info, 0, sizeof *info);
+ if (!g || layer >= g->layers || T < OMEGA_GDN_PF_MIN_T || T > OMEGA_GPU_GDN_PF_MAX_T) return OMEGA_GPU_GDN_BAD_ARGS;
+ if (rec ? (!dev_qkv || !dev_z || !dev_ab) : (!qkv || !z || !ab || !out)) return OMEGA_GPU_GDN_BAD_ARGS;
+ if (!isfinite(eps_l2) || !isfinite(eps_norm) || !isfinite(in_scale) || !isfinite(out_scale)) return OMEGA_GPU_GDN_BAD_ARGS;
+ OmegaGdnSpec spec; memset(&spec, 0, sizeof spec);
+ spec.mutant = g_k.mutant;
+ size_t off[PF_N];
+ const size_t bytes = pf_layout(T, off);
+
+ if (!rec) omega_gpu_session_lock();
+#define UNLOCK() do { if (!rec) omega_gpu_session_unlock(); } while (0)
+ if (!dev_open_locked()) { UNLOCK(); return OMEGA_GPU_GDN_CHIP_FAIL; }
+ if (rec && (!g->pf.mem.cpu || g->pf.cap < bytes)) {
+ omega_gpu_session_set_error("gdn prefill record: scratch below T tokens (call omega_gpu_gdn_prefill_reserve first)");
+ UNLOCK(); return OMEGA_GPU_GDN_TOO_LARGE;
+ }
+ if (!rec && omega_gpu_session_scratch(&g->pf, bytes) != 0) { UNLOCK(); return OMEGA_GPU_GDN_TOO_LARGE; }
+ Slot *ks[3] = { NULL, NULL, NULL };
+ int rc = OMEGA_GPU_GDN_OK, hits = 0;
+ g_rec = rec != NULL;
+ for (unsigned k = 0; k < 3 && rc == OMEGA_GPU_GDN_OK; k++) { bool hit = false; rc = kernel_for(KK_PREP + k, &spec, &ks[k], &hit); hits += hit; }
+ g_rec = 0;
+ if (rc != OMEGA_GPU_GDN_OK) { UNLOCK(); return rc; }
+ uint8_t *b = g->pf.mem.cpu;
+ const uint64_t va = g->pf.mem.va;
+ float *yo = (float *)(b + off[PF_OUT]);
+ const size_t nout = (size_t)T * OMEGA_GDN_V_DIM;
+ if (!rec) {
+ memcpy(b + off[PF_QKV], qkv, (size_t)T * OMEGA_GDN_CONV_DIM * 4u);
+ memcpy(b + off[PF_Z], z, nout * 4u);
+ memcpy(b + off[PF_AB], ab, (size_t)T * OMEGA_GDN_VALUE_HEADS * 8u);
+ for (size_t i = 0; i < nout; i++) yo[i] = POISON_F32;
+ if (omega_gpu_session_sync_to_gpu(&g->pf.mem) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL;
+ }
+ const uint64_t a_qkv = rec ? dev_qkv : va + off[PF_QKV], a_z = rec ? dev_z : va + off[PF_Z], a_ab = rec ? dev_ab : va + off[PF_AB];
+ const uint64_t a_par = g->par.va + (uint64_t)layer * OMEGA_GDN_PAR_BYTES, a_tail = g->tail.va + (uint64_t)layer * OMEGA_GDN_TAIL_BYTES;
+ const uint64_t a_state = g->state.va + (uint64_t)layer * OMEGA_GDN_STATE_BYTES, a_rec = va + off[PF_REC], a_o = va + off[PF_O];
+ const uint64_t a_out = va + off[PF_OUT];
+ uint32_t ap[OMEGA_GDN_PREP_ARGS_WORDS], as[OMEGA_GDN_SCAN_ARGS_WORDS], aq[OMEGA_GDN_POST_ARGS_WORDS];
+#define A64(arr, i, v) do { (arr)[i] = (uint32_t)(v); (arr)[(i) + 1] = (uint32_t)((v) >> 32); } while (0)
+ A64(ap, 0, a_qkv); A64(ap, 2, a_ab); A64(ap, 4, a_par); A64(ap, 6, a_tail); A64(ap, 8, a_rec);
+ ap[10] = T; memcpy(&ap[11], &eps_l2, 4); memcpy(&ap[12], &in_scale, 4);
+ A64(as, 0, a_rec); A64(as, 2, a_qkv); A64(as, 4, a_tail); A64(as, 6, a_state); A64(as, 8, a_o);
+ as[10] = T; memcpy(&as[11], &in_scale, 4);
+ A64(aq, 0, a_o); A64(aq, 2, a_z); A64(aq, 4, a_par); A64(aq, 6, a_out);
+ memcpy(&aq[8], &eps_norm, 4); memcpy(&aq[9], &in_scale, 4); memcpy(&aq[10], &out_scale, 4);
+#undef A64
+ const uint32_t thr[3] = { OMEGA_GDN_PREP_THREADS, OMEGA_GDN_THREADS, OMEGA_GDN_THREADS };
+ const uint32_t gx[3] = { (T + OMEGA_GDN_PREP_TOKENS - 1u) / OMEGA_GDN_PREP_TOKENS, OMEGA_GDN_GRID_X, T };
+ const uint32_t gy[3] = { OMEGA_GDN_KEY_HEADS, OMEGA_GDN_GRID_Y, OMEGA_GDN_POST_GRID_Y };
+ const uint32_t sh[3] = { OMEGA_GDN_PREP_SHARED_BYTES, OMEGA_GDN_SCAN_SHARED_BYTES, 0 };
+ const uint32_t *args[3] = { ap, as, aq };
+ const uint32_t nargs[3] = { OMEGA_GDN_PREP_ARGS_WORDS, OMEGA_GDN_SCAN_ARGS_WORDS, OMEGA_GDN_POST_ARGS_WORDS };
+ if (info) {
+ info->layer = layer; info->tokens = T; info->kernel_cache_hits = (uint32_t)hits;
+ for (unsigned k = 0; k < 3; k++) { info->insns[k] = ks[k]->insns; info->gprs[k] = ks[k]->kernel.gpr_count; }
+ }
+ for (unsigned k = 0; k < 3 && rc == OMEGA_GPU_GDN_OK; k++) {
+ OmegaGpuLaunch L = {
+ .code_va = ks[k]->code.va, .gpr_count = omega_bw_gpr_declared(ks[k]->kernel.gpr_count),
+ .threads_x = thr[k], .threads_y = 1, .grid_x = gx[k], .grid_y = gy[k],
+ .num_elements = nout,
+ .args = args[k], .n_args = nargs[k], .timeout_ms = MARKER_TIMEOUT_MS,
+ .shared_bytes = sh[k],
+ };
+ if (!omega_bw_gpr_declared_ok(L.gpr_count, ks[k]->kernel.gpr_count)) { omega_gpu_session_set_error("declared registers below peak + 2: refusing to launch"); rc = OMEGA_GPU_GDN_CHIP_FAIL; }
+ else if (rec) { if (omega_gpu_cmdlist_add(rec, &L) < 0) rc = OMEGA_GPU_GDN_CHIP_FAIL; }
+ else if (omega_gpu_session_launch(&L, info ? &info->elapsed_ns[k] : NULL, NULL) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL;
+ }
+ if (rc == OMEGA_GPU_GDN_OK && !rec && omega_gpu_session_sync_to_cpu(&g->pf.mem) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL;
+ if (rc == OMEGA_GPU_GDN_OK && !rec) {
+ size_t unchanged = 0;
+ for (size_t i = 0; i < nout; i++) if (yo[i] == POISON_F32) unchanged++;
+ if (unchanged) { omega_gpu_session_set_error("gdn prefill: output poison survived"); rc = OMEGA_GPU_GDN_CHIP_FAIL; }
+ else memcpy(out, yo, nout * 4u);
+ }
+ if (rc == OMEGA_GPU_GDN_OK && out_va) *out_va = a_out;
+ UNLOCK();
+#undef UNLOCK
+ if (info) info->call_ns = omega_gpu_session_now_ns() - c0;
+ return rc;
+}
+
+int omega_gpu_gdn_prefill(OmegaGpuGdn *g, uint32_t layer, uint32_t tokens, const float *qkv, const float *z, const float *ab,
+ float eps_l2, float eps_norm, float in_scale, float out_scale, float *out, OmegaGpuGdnPfInfo *info) {
+ return pf_core(g, layer, tokens, qkv, z, ab, eps_l2, eps_norm, in_scale, out_scale, out, info, NULL, 0, 0, 0, NULL);
+}
+
+int omega_gpu_gdn_prefill_record(OmegaGpuCmdList *cl, OmegaGpuGdn *g, uint32_t layer, uint32_t tokens, uint64_t qkv_va,
+ uint64_t z_va, uint64_t ab_va, float eps_l2, float eps_norm, float in_scale, float out_scale,
+ uint64_t *out_va) {
+ if (!cl) return OMEGA_GPU_GDN_BAD_ARGS;
+ return pf_core(g, layer, tokens, NULL, NULL, NULL, eps_l2, eps_norm, in_scale, out_scale, NULL, NULL, cl, qkv_va, z_va, ab_va, out_va);
+}
+
+int omega_gpu_gdn_prefill_read_out(OmegaGpuGdn *g, uint32_t tokens, float *out) {
+ if (!g || !out || tokens < OMEGA_GDN_PF_MIN_T || tokens > OMEGA_GPU_GDN_PF_MAX_T) return OMEGA_GPU_GDN_BAD_ARGS;
+ size_t off[PF_N];
+ const size_t bytes = pf_layout(tokens, off);
+ omega_gpu_session_lock();
+ int rc = (!g->pf.mem.cpu || g->pf.cap < bytes) ? OMEGA_GPU_GDN_BAD_ARGS
+ : omega_gpu_session_sync_to_cpu(&g->pf.mem) == 0 ? OMEGA_GPU_GDN_OK : OMEGA_GPU_GDN_CHIP_FAIL;
+ if (rc == OMEGA_GPU_GDN_OK) memcpy(out, (const uint8_t *)g->pf.mem.cpu + off[PF_OUT], (size_t)tokens * OMEGA_GDN_V_DIM * 4u);
+ omega_gpu_session_unlock();
+ return rc;
+}
diff --git a/src/omega_gpu_gdn_api.h b/src/omega_gpu_gdn_api.h
index a0624c5fc ..c7de6f1a8 100644
--- a/src/omega_gpu_gdn_api.h
+++ b/src/omega_gpu_gdn_api.h
@@ -73,6 +73,35 @@ int omega_gpu_gdn_read_out(OmegaGpuGdn *g, float *out);
const char *omega_gpu_gdn_last_error(void);
const char *omega_gpu_gdn_rc_name(int rc);
+/* ---- prefill (omega#543, 2026-10-18): T >= 3 new tokens of one layer in three launches (prep, scan, post; see
+ * omega_blackwell_codegen_gdn.h), continuing from the layer's tail and state exactly as T decode steps would, and leaving
+ * them where the next prefill chunk or decode step picks them up. A long prompt is prefilled in chunks.
+ * int omega_gpu_gdn_prefill(g, layer, T, qkv [T][10240], z [T][6144], ab [T][48][2], eps_l2, eps_norm, in_scale,
+ * out_scale, out [T][6144], info); the decode step's row layouts, one row per token
+ * int omega_gpu_gdn_prefill_reserve(g, max_tokens); sizes the prefill scratch now and pins it (the record path needs
+ * it: a recorded launch must never see its scratch move); a later prefill of more tokens is refused
+ * int omega_gpu_gdn_prefill_record(cl, g, layer, T, qkv_va, z_va, ab_va, eps_l2, eps_norm, in_scale, out_scale, &out_va);
+ * the three launches appended to a command list (caller holds the lock); out_va = the [T][6144] rows in the scratch
+ * Scratch bytes for T tokens: T * (40960 + 3 * 24576 + 384) + ceil8(T) * 74496 (the per-token records dominate). */
+#define OMEGA_GPU_GDN_PF_MAX_T 65536u
+
+typedef struct {
+ uint32_t layer, tokens;
+ uint32_t insns[3], gprs[3]; /* prep, scan, post */
+ uint64_t elapsed_ns[3]; /* marker-to-marker chip time per launch (host path) */
+ uint64_t call_ns;
+ uint32_t kernel_cache_hits; /* 0..3 */
+} OmegaGpuGdnPfInfo;
+
+int omega_gpu_gdn_prefill(OmegaGpuGdn *g, uint32_t layer, uint32_t tokens, const float *qkv, const float *z, const float *ab,
+ float eps_l2, float eps_norm, float in_scale, float out_scale, float *out, OmegaGpuGdnPfInfo *info);
+int omega_gpu_gdn_prefill_reserve(OmegaGpuGdn *g, uint32_t max_tokens);
+int omega_gpu_gdn_prefill_record(struct OmegaGpuCmdList *cl, OmegaGpuGdn *g, uint32_t layer, uint32_t tokens, uint64_t qkv_va,
+ uint64_t z_va, uint64_t ab_va, float eps_l2, float eps_norm, float in_scale, float out_scale,
+ uint64_t *out_va);
+/* Test helper: the out rows the last prefill wrote (after a record + submit). */
+int omega_gpu_gdn_prefill_read_out(OmegaGpuGdn *g, uint32_t tokens, float *out);
+
/* Host layout helpers (no device; the test's simulator path uses them too). */
/* The parameter block (OMEGA_GDN_PAR_BYTES) from the checkpoint tensors (see set_layer). -1 on a NULL pointer. */
int omega_gdn_pack_params(const float *conv_w, const float *a_log, const float *dt_bias, const float *norm_w, uint8_t *block);
diff --git a/tests/.crumb b/tests/.crumb
index 4b84223c2 ..0d0790dd8 100644
--- a/tests/.crumb
+++ b/tests/.crumb
@@ -296,14 +296,14 @@
}
],
"children_root": "sha256:ddc3618190458bc5073e3bee67ffccc68a2ee9f065f2cb0d86545386a29fddf8 ",
- "compiled_at": "2026-10-18T13:01:43Z",
+ "compiled_at": "2026-10-18T13:02:58Z",
"compiler": "crumb-compile/1",
- "digest": "sha256:b9d21f0ce5aac9c60dc57b5b37d9b4d8f3b0df0de30f9e76ab471e4aa8c76b1a ",
+ "digest": "sha256:edf167b3607974785ec9cb7478d8d26d562cb5c83107322efa79954482d34c39 ",
"evidence_files": 16,
"evidence_root": "sha256:96d08097822ad95f4e562c19e0ba5df5a035774c48632f1832912c40a27bd27a ",
"files": 1271,
- "generated_at_commit": "e0f65ff3157d5214cce0a6b7f35263dd73e3e62c ",
- "source_tree": "sha256:f29c110581e4482a8255d05cc7e45fefa62ebe34a90d304098ec0d7fab528f6b "
+ "generated_at_commit": "e7356996557da557d63de0f56558a335b5c25223 ",
+ "source_tree": "sha256:d924aecc5ebf4daf3873b99f7b03db23db318b098329b12028f4673a3bc7b289 "
},
"owner_lane": "",
"provenance": {
diff --git a/tests/bw_warp_sim_k.h b/tests/bw_warp_sim_k.h
index 53fed2db8 ..d42c89d32 100644
--- a/tests/bw_warp_sim_k.h
+++ b/tests/bw_warp_sim_k.h
@@ -276,6 +276,8 @@ static int ws_run_warp(WsSim *s, WsWarp *w, uint32_t warp, uint32_t cx, uint32_t
if (mask != live) { ws_fail(s, WS_E_SHFL_SPLIT, pc, "SHFL on a split warp"); return -1; }
uint32_t src[WS_WARP];
for (int L = 0; L < WS_WARP; L++) src[L] = ws_rd(&w->lane[L], in->src1_vreg, p);
+ /* SHFL.BFLY (imm bit 31, OMEGA_TC_MARK_BFLY: patched in after encoding, omega_blackwell_codegen_tc.h): lane L reads L ^ lane */
+ if (in->imm & 0x80000000u) { for (int L = 0; L < WS_WARP; L++) ws_wr(&w->lane[L], in->dst_vreg, src[L ^ (int)(in->imm & 31)], p); f->pc = pc + 1; break; }
for (int L = 0; L < WS_WARP; L++) { int from = L + (int)(in->imm & 31); ws_wr(&w->lane[L], in->dst_vreg, from < WS_WARP ? src[from] : src[L], p); }
f->pc = pc + 1;
break;
diff --git a/tests/gpu_gdn_test.c b/tests/gpu_gdn_test.c
index c969a7a82 ..4aab22816 100644
--- a/tests/gpu_gdn_test.c
+++ b/tests/gpu_gdn_test.c
@@ -13,6 +13,7 @@
#include "omega_gpu_hostmath.h"
#include "omega_gpu_session.h"
#include "omega_gpu_cmdlist.h"
+#include "omega_blackwell_codegen_tc.h"
#include "bw_warp_sim_k.h"
#include <inttypes.h>
#include <math.h>
@@ -195,6 +196,23 @@ static void host_only(void) {
free(y); inp_free(&in); dstate_free(&d);
}
layer_free(&L);
+ /* the prefill kernels encode, and every SHFL.BFLY placeholder is patched (the count kernel_for demands) */
+ for (unsigned k = 0; k < 3; k++) {
+ static BlackwellIRProgram p;
+ OmegaGdnSpec spec = { 0 };
+ int rc = k == 0 ? omega_blackwell_codegen_gdn_prep_prog(&spec, &p) : k == 1 ? omega_blackwell_codegen_gdn_scan_prog(&spec, &p)
+ : omega_blackwell_codegen_gdn_post_prog(&spec, &p);
+ size_t need = rc == 0 ? omega_bw_program_code_bytes(&p) : 0;
+ uint8_t *code = need ? malloc(need) : NULL;
+ OmegaBlackwellKernel kern;
+ int enc = code ? omega_blackwell_kernel_encode_into(&p, code, need, &kern) : -1;
+ int np = enc == 0 ? omega_tc_patch_code(&p, code, need) : -1;
+ const uint32_t want = omega_gdn_bfly_count(&p);
+ if (getenv("GDN_DUMP") && code && atoi(getenv("GDN_DUMP")) == (int)k) { FILE *f = fopen("gdn_pf.bin", "wb"); if (f) { fwrite(code, 1, need, f); fclose(f); } }
+ CHECK(rc == 0 && enc == 0 && np >= 0 && (uint32_t)np == want && (k == 0 || want > 0),
+ "prefill kernel %u encodes: %zu insns, %u registers, %d of %u butterflies patched", k, p.count, p.regalloc.peak_gpr_usage, np, want);
+ free(code);
+ }
}
/* ---- simulator ---- */
@@ -286,6 +304,110 @@ static void sim_battery(void) {
printf("sim warp issues: %" PRIu64 "\n", g_sim_issues);
}
+/* ---- prefill on the simulator (omega#543): prep, scan, post over T tokens against T oracle steps ---- */
+typedef struct { uint32_t off; void *p; size_t bytes; } SimBuf;
+typedef struct { uint32_t off; uint32_t word; } SimWord;
+static int sim_launch(const BlackwellIRProgram *prog, uint32_t threads, uint32_t gx, uint32_t gy, uint32_t smem_bytes, int order,
+ const SimBuf *b, int nb, const SimWord *w, int nw, char *msg, size_t n) {
+ static uint8_t smem[OMEGA_GDN_SCAN_SHARED_BYTES];
+ WsSim s; memset(&s, 0, sizeof s);
+ s.p = prog; s.threads = threads; s.order = order;
+ memset(smem, 0xcd, sizeof smem); /* poison: a read before a write shows */
+ s.smem = smem; s.smem_bytes = smem_bytes ? smem_bytes : 4u;
+ for (int i = 0; i < nb; i++) {
+ const uint64_t a = (uintptr_t)b[i].p;
+ s.cbank[b[i].off / 4] = (uint32_t)a; s.cbank[b[i].off / 4 + 1] = (uint32_t)(a >> 32);
+ s.ranges[i].lo = a; s.ranges[i].hi = a + b[i].bytes;
+ }
+ s.nranges = nb;
+ for (int i = 0; i < nw; i++) s.cbank[w[i].off / 4] = w[i].word;
+ int err = ws_run(&s, gx, gy);
+ if (err) snprintf(msg, n, "%.150s (pc %d)", s.msg, s.err_pc);
+ g_sim_issues += s.warp_issues;
+ return err;
+}
+static uint32_t fw(float f) { uint32_t u; memcpy(&u, &f, 4); return u; }
+static int sim_prefill(int mutant, uint32_t T, float in_scale, float out_scale, Errs *worst, char *msg, size_t n) {
+ static BlackwellIRProgram pp, ps, pq;
+ OmegaGdnSpec spec = { mutant };
+ if (omega_blackwell_codegen_gdn_prep_prog(&spec, &pp) != 0 || omega_blackwell_codegen_gdn_scan_prog(&spec, &ps) != 0 ||
+ omega_blackwell_codegen_gdn_post_prog(&spec, &pq) != 0) { snprintf(msg, n, "codegen refused"); return -1; }
+ Layer L; layer_make(&L, 21);
+ DState d; dstate_init(&d, &L);
+ const uint32_t T8 = (T + OMEGA_GDN_SCAN_CHUNK - 1u) / OMEGA_GDN_SCAN_CHUNK * OMEGA_GDN_SCAN_CHUNK;
+ const size_t s_qkv = (size_t)T * CD * 4u, s_z = (size_t)T * VD * 4u, s_ab = (size_t)T * VH * 8u;
+ const size_t s_rec = (size_t)T8 * VH * OMEGA_GDN_REC_BYTES, s_o = (size_t)T * VD * 4u;
+ float *qkv = aligned_alloc(256, s_qkv), *z = aligned_alloc(256, s_z), *ab = aligned_alloc(256, s_ab);
+ uint8_t *par = aligned_alloc(256, OMEGA_GDN_PAR_BYTES), *tail = aligned_alloc(256, OMEGA_GDN_TAIL_BYTES);
+ uint8_t *state = aligned_alloc(256, OMEGA_GDN_STATE_BYTES), *rec = aligned_alloc(256, s_rec);
+ float *o = aligned_alloc(256, s_o), *out = aligned_alloc(256, s_o), *out0 = malloc(s_o);
+ uint8_t *tail0 = malloc(OMEGA_GDN_TAIL_BYTES), *state0 = malloc(OMEGA_GDN_STATE_BYTES);
+ double *y = malloc((size_t)T * VD * 8);
+ float *tref = malloc((size_t)NT * 4);
+ for (uint32_t t = 0; t < T; t++) {
+ Inp in; inp_make(&in, 1000 + t, in_scale);
+ memcpy(qkv + (size_t)t * CD, in.qkv, CD * 4u); memcpy(z + (size_t)t * VD, in.z, VD * 4u); memcpy(ab + (size_t)t * VH * 2u, in.ab, VH * 8u);
+ oracle_step(&L, &d, &in, in_scale, out_scale, y + (size_t)t * VD);
+ inp_free(&in);
+ }
+ omega_gdn_pack_params(L.conv_w, L.a_log, L.dtb, L.nw, par);
+ int err = 0;
+ for (int order = 0; order < 2 && !err; order++) {
+ omega_gdn_pack_tail(L.tail0, (float *)tail);
+ memcpy(state, L.state0, OMEGA_GDN_STATE_BYTES);
+ memset(rec, 0xcd, s_rec);
+ for (size_t i = 0; i < (size_t)T * VD; i++) { o[i] = -999.0f; out[i] = -999.0f; }
+ const SimBuf bp[] = { { OMEGA_GDN_PREP_ARG_QKV, qkv, s_qkv }, { OMEGA_GDN_PREP_ARG_AB, ab, s_ab }, { OMEGA_GDN_PREP_ARG_PAR, par, OMEGA_GDN_PAR_BYTES },
+ { OMEGA_GDN_PREP_ARG_TAIL, tail, OMEGA_GDN_TAIL_BYTES }, { OMEGA_GDN_PREP_ARG_REC, rec, s_rec } };
+ const SimWord wp[] = { { OMEGA_GDN_PREP_ARG_T, T }, { OMEGA_GDN_PREP_ARG_EPS_L2, fw(EPS_L2) }, { OMEGA_GDN_PREP_ARG_IN_SCALE, fw(in_scale) } };
+ const SimBuf bs[] = { { OMEGA_GDN_SCAN_ARG_REC, rec, s_rec }, { OMEGA_GDN_SCAN_ARG_QKV, qkv, s_qkv }, { OMEGA_GDN_SCAN_ARG_TAIL, tail, OMEGA_GDN_TAIL_BYTES },
+ { OMEGA_GDN_SCAN_ARG_STATE, state, OMEGA_GDN_STATE_BYTES }, { OMEGA_GDN_SCAN_ARG_O, o, s_o } };
+ const SimWord wsc[] = { { OMEGA_GDN_SCAN_ARG_T, T }, { OMEGA_GDN_SCAN_ARG_IN_SCALE, fw(in_scale) } };
+ const SimBuf bq[] = { { OMEGA_GDN_POST_ARG_O, o, s_o }, { OMEGA_GDN_POST_ARG_Z, z, s_z }, { OMEGA_GDN_POST_ARG_PAR, par, OMEGA_GDN_PAR_BYTES },
+ { OMEGA_GDN_POST_ARG_OUT, out, s_o } };
+ const SimWord wq[] = { { OMEGA_GDN_POST_ARG_EPS_NORM, fw(EPS_N) }, { OMEGA_GDN_POST_ARG_IN_SCALE, fw(in_scale) }, { OMEGA_GDN_POST_ARG_OUT_SCALE, fw(out_scale) } };
+ char m2[200] = "";
+ if (sim_launch(&pp, OMEGA_GDN_PREP_THREADS, (T + OMEGA_GDN_PREP_TOKENS - 1u) / OMEGA_GDN_PREP_TOKENS, KH, OMEGA_GDN_PREP_SHARED_BYTES, order, bp, 5, wp, 3, m2, sizeof m2)) { snprintf(msg, n, "prep: %s", m2); err = -1; break; }
+ if (sim_launch(&ps, OMEGA_GDN_THREADS, OMEGA_GDN_GRID_X, OMEGA_GDN_GRID_Y, OMEGA_GDN_SCAN_SHARED_BYTES, order, bs, 5, wsc, 2, m2, sizeof m2)) { snprintf(msg, n, "scan: %s", m2); err = -1; break; }
+ if (sim_launch(&pq, OMEGA_GDN_THREADS, T, OMEGA_GDN_POST_GRID_Y, 0, order, bq, 4, wq, 3, m2, sizeof m2)) { snprintf(msg, n, "post: %s", m2); err = -1; break; }
+ size_t unwritten = 0; for (size_t i = 0; i < (size_t)T * VD; i++) if (out[i] == -999.0f) unwritten++;
+ if (unwritten) { snprintf(msg, n, "%zu output words never written", unwritten); err = -1; break; }
+ if (order == 0) { memcpy(out0, out, s_o); memcpy(tail0, tail, OMEGA_GDN_TAIL_BYTES); memcpy(state0, state, OMEGA_GDN_STATE_BYTES); }
+ else if (memcmp(out0, out, s_o) || memcmp(tail0, tail, OMEGA_GDN_TAIL_BYTES) || memcmp(state0, state, OMEGA_GDN_STATE_BYTES)) {
+ snprintf(msg, n, "the warp order changed the result"); err = -1;
+ }
+ }
+ if (!err) {
+ double *ts = malloc((size_t)NT * 8);
+ for (size_t i = 0; i < NT; i++) ts[i] = d.tail[i];
+ worst->out = nerr(out, y, (size_t)T * VD);
+ worst->tail = omega_gdn_unpack_tail((const float *)tail, tref) != 0 ? INFINITY : nerr(tref, ts, NT);
+ worst->state = nerr((const float *)state, d.state, NS);
+ free(ts);
+ }
+ free(qkv); free(z); free(ab); free(par); free(tail); free(state); free(rec); free(o); free(out); free(out0);
+ free(tail0); free(state0); free(y); free(tref);
+ dstate_free(&d); layer_free(&L);
+ return err;
+}
+static void sim_prefill_battery(void) {
+ struct { int mutant; uint32_t T; float in_scale, out_scale; const char *name; } pts[] = {
+ { 0, 37, 1.0f, 1.0f, "prefill T 37" }, { 0, 3, 1.0f, 1.0f, "prefill T 3" },
+ { 0, 17, 0.0009765625f , 18446744073709551616 .0f, "prefill T 17, in 2^-10, out 2^64" },
+ { 1, 19, 1.0f, 1.0f, "prefill mutant 1 (no decay)" }, { 2, 19, 1.0f, 1.0f, "prefill mutant 2 (beta 1)" },
+ { 3, 19, 1.0f, 1.0f, "prefill mutant 3 (gate z)" }, { 4, 19, 1.0f, 1.0f, "prefill mutant 4 (tail kept)" },
+ };
+ for (size_t i = 0; i < sizeof pts / sizeof pts[0]; i++) {
+ char msg[300] = ""; Errs w = { 0, 0, 0 };
+ double t0 = now_ms();
+ int err = sim_prefill(pts[i].mutant, pts[i].T, pts[i].in_scale, pts[i].out_scale, &w, msg, sizeof msg);
+ 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);
+ CHECK(err == 0, "sim %s ran (%s)", pts[i].name, msg);
+ if (pts[i].mutant == 0) CHECK(errs_ok(&w), "sim %s within tolerance", pts[i].name);
+ else CHECK(!errs_ok(&w), "sim %s caught", pts[i].name);
+ }
+}
+
/* ---- chip ---- */
static int chip_steps(OmegaGpuGdn *g, uint32_t layer, const Layer *L, DState *d, uint32_t steps, uint32_t seed, float in_scale,
float out_scale, Errs *worst, float *last_out) {
@@ -311,6 +433,175 @@ static int chip_steps(OmegaGpuGdn *g, uint32_t layer, const Layer *L, DState *d,
free(out); free(tail); free(state); free(y); free(td);
return rc;
}
+/* ---- prefill on the chip (omega#543) ---- */
+typedef struct { float *qkv, *z, *ab; } PfIn;
+static void pf_in_make(PfIn *p, uint32_t T, uint32_t seed, float in_scale) {
+ p->qkv = malloc((size_t)T * CD * 4); p->z = malloc((size_t)T * VD * 4); p->ab = malloc((size_t)T * VH * 8);
+ for (uint32_t t = 0; t < T; t++) {
+ Inp in; inp_make(&in, seed + t, in_scale);
+ memcpy(p->qkv + (size_t)t * CD, in.qkv, CD * 4u); memcpy(p->z + (size_t)t * VD, in.z, VD * 4u); memcpy(p->ab + (size_t)t * VH * 2u, in.ab, VH * 8u);
+ inp_free(&in);
+ }
+}
+static void pf_in_free(PfIn *p) { free(p->qkv); free(p->z); free(p->ab); }
+/* oracle over the same T rows; y [T][VD] */
+static void pf_oracle(const Layer *L, DState *d, uint32_t T, uint32_t seed, float in_scale, float out_scale, double *y) {
+ for (uint32_t t = 0; t < T; t++) { Inp in; inp_make(&in, seed + t, in_scale); oracle_step(L, d, &in, in_scale, out_scale, y + (size_t)t * VD); inp_free(&in); }
+}
+/* prefill T tokens on `layer` from its current state, then `dec` decode steps; worst errors over every row, the tail and
+ * the state after the prefill and after each step */
+static int chip_prefill_chain(OmegaGpuGdn *g, uint32_t layer, const Layer *L, uint32_t T, uint32_t dec, uint32_t seed, float in_scale,
+ float out_scale, Errs *worst, float *out_copy) {
+ DState d; dstate_init(&d, L);
+ PfIn p; pf_in_make(&p, T, seed, in_scale);
+ float *out = malloc((size_t)T * VD * 4), *tail = malloc((size_t)NT * 4), *state = malloc((size_t)NS * 4);
+ double *y = malloc((size_t)T * VD * 8), *td = malloc((size_t)NT * 8);
+ memset(worst, 0, sizeof *worst);
+ int rc = omega_gpu_gdn_prefill(g, layer, T, p.qkv, p.z, p.ab, EPS_L2, EPS_N, in_scale, out_scale, out, NULL);
+ if (rc == 0) rc = omega_gpu_gdn_get_state(g, layer, tail, state);
+ if (rc == 0) {
+ pf_oracle(L, &d, T, seed, in_scale, out_scale, y);
+ for (size_t i = 0; i < NT; i++) td[i] = d.tail[i];
+ worst->out = nerr(out, y, (size_t)T * VD); worst->tail = nerr(tail, td, NT); worst->state = nerr(state, d.state, NS);
+ if (out_copy) memcpy(out_copy, out, (size_t)T * VD * 4);
+ }
+ if (rc == 0 && dec) {
+ Errs e;
+ rc = chip_steps(g, layer, L, &d, dec, seed + T, in_scale, out_scale, &e, NULL);
+ if (e.out > worst->out) worst->out = e.out;
+ if (e.tail > worst->tail) worst->tail = e.tail;
+ if (e.state > worst->state) worst->state = e.state;
+ }
+ free(out); free(tail); free(state); free(y); free(td);
+ pf_in_free(&p); dstate_free(&d);
+ return rc;
+}
+static void chip_prefill(OmegaGpuGdn *g, Layer *L, FILE *out, int *first, uint32_t reps) {
+#define PPOINT(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)
+ struct { uint32_t layer, T, dec, seed; float in_scale, out_scale; const char *name; } pp[] = {
+ { 1, 37, 3, 2000, 1.0f, 1.0f, "prefill layer 1, T 37, then 3 decode steps" },
+ { 0, 3, 1, 2100, 1.0f, 1.0f, "prefill layer 0, T 3, then 1 decode step" },
+ { 0, 41, 0, 2200, 0.0009765625f , 18446744073709551616 .0f, "prefill layer 0, T 41, in 2^-10 out 2^64" },
+ };
+ for (size_t i = 0; i < sizeof pp / sizeof pp[0]; i++) {
+ CHECK(omega_gpu_gdn_set_state(g, pp[i].layer, L[pp[i].layer].tail0, L[pp[i].layer].state0) == 0, "set state for %s", pp[i].name);
+ Errs e;
+ int rc = chip_prefill_chain(g, pp[i].layer, &L[pp[i].layer], pp[i].T, pp[i].dec, pp[i].seed, pp[i].in_scale, pp[i].out_scale, &e, NULL);
+ printf("chip %-48s 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() : "");
+ CHECK(rc == 0 && errs_ok(&e), "chip %s", pp[i].name);
+ PPOINT(pp[i].name, e, "");
+ }
+ /* rerun from the same state: bit-identical rows */
+ {
+ float *a = malloc((size_t)37 * VD * 4), *b = malloc((size_t)37 * VD * 4);
+ Errs e1, e2;
+ CHECK(omega_gpu_gdn_set_state(g, 1, L[1].tail0, L[1].state0) == 0, "set state for prefill rerun");
+ int rc = chip_prefill_chain(g, 1, &L[1], 37, 0, 2000, 1.0f, 1.0f, &e1, a);
+ CHECK(omega_gpu_gdn_set_state(g, 1, L[1].tail0, L[1].state0) == 0, "set state for prefill rerun 2");
+ int rc2 = chip_prefill_chain(g, 1, &L[1], 37, 0, 2000, 1.0f, 1.0f, &e2, b);
+ CHECK(rc == 0 && rc2 == 0 && memcmp(a, b, (size_t)37 * VD * 4) == 0, "prefill rerun from the same state is bit-identical");
+ free(a); free(b);
+ }
+ /* mutants */
+ for (int m = 1; m <= 4; m++) {
+ omega_gpu_gdn_test_set_mutant(m);
+ CHECK(omega_gpu_gdn_set_state(g, 1, L[1].tail0, L[1].state0) == 0, "set state prefill mutant %d", m);
+ Errs e;
+ int rc = chip_prefill_chain(g, 1, &L[1], 19, 0, 2300, 1.0f, 1.0f, &e, NULL);
+ char nm[40]; snprintf(nm, sizeof nm, "prefill mutant %d", m);
+ printf("chip %-48s rc=%s err out %.3g tail %.3g state %.3g\n", nm, omega_gpu_gdn_rc_name(rc), e.out, e.tail, e.state);
+ CHECK(rc == 0 && !errs_ok(&e), "chip prefill mutant %d caught", m);
+ PPOINT(nm, e, "");
+ }
+ omega_gpu_gdn_test_set_mutant(0);
+ /* the record path: prefill 37 tokens and one decode step in one command list on layer 0 */
+ {
+ const uint32_t T = 37;
+ CHECK(omega_gpu_gdn_set_state(g, 0, L[0].tail0, L[0].state0) == 0, "set state for prefill record");
+ CHECK(omega_gpu_gdn_prefill_reserve(g, 64) == 0, "prefill reserve 64 tokens");
+ PfIn p; pf_in_make(&p, T, 2400, 1.0f);
+ Inp nx; inp_make(&nx, 2400 + T, 1.0f);
+ DState d; dstate_init(&d, &L[0]);
+ double *y = malloc((size_t)T * VD * 8), *y1 = malloc((size_t)VD * 8);
+ pf_oracle(&L[0], &d, T, 2400, 1.0f, 1.0f, y);
+ oracle_step(&L[0], &d, &nx, 1.0f, 1.0f, y1);
+ /* warm all four kernels on layer 1 (a cache miss inside the record is refused) */
+ float *tmp = malloc((size_t)T * VD * 4);
+ CHECK(omega_gpu_gdn_prefill(g, 1, T, p.qkv, p.z, p.ab, EPS_L2, EPS_N, 1.0f, 1.0f, tmp, NULL) == 0, "prefill warm-up");
+ CHECK(omega_gpu_gdn_step(g, 1, nx.qkv, nx.z, nx.ab, EPS_L2, EPS_N, 1.0f, 1.0f, tmp, NULL) == 0, "decode warm-up");
+ OmegaGpuCmdList *cl = NULL; /* create and free take the session lock themselves (omega_gpu_cmdlist.h) */
+ const int cl_ok = omega_gpu_cmdlist_create(8, &cl) == 0;
+ omega_gpu_session_lock();
+ OmegaGpuMem inb; memset(&inb, 0, sizeof inb);
+ const size_t s_qkv = (size_t)T * CD * 4, s_z = (size_t)T * VD * 4, s_ab = (size_t)T * VH * 8;
+ const size_t o_nx = (s_qkv + s_z + s_ab + 255) & ~(size_t)255;
+ int ok = cl_ok && omega_gpu_session_open() != NULL && omega_gpu_session_alloc(o_nx + (size_t)CD * 4 + VD * 4 + VH * 8, &inb) == 0;
+ if (ok) {
+ uint8_t *c = inb.cpu;
+ memcpy(c, p.qkv, s_qkv); memcpy(c + s_qkv, p.z, s_z); memcpy(c + s_qkv + s_z, p.ab, s_ab);
+ memcpy(c + o_nx, nx.qkv, (size_t)CD * 4); memcpy(c + o_nx + CD * 4, nx.z, (size_t)VD * 4); memcpy(c + o_nx + CD * 4 + VD * 4, nx.ab, VH * 8);
+ ok = omega_gpu_session_sync_to_gpu(&inb) == 0;
+ }
+ uint64_t pf_out = 0, dec_out = 0;
+ if (ok) ok = omega_gpu_gdn_prefill_record(cl, g, 0, T, inb.va, inb.va + s_qkv, inb.va + s_qkv + s_z, EPS_L2, EPS_N, 1.0f, 1.0f, &pf_out) == 0;
+ if (ok) ok = omega_gpu_gdn_record(cl, g, 0, inb.va + o_nx, inb.va + o_nx + CD * 4, inb.va + o_nx + CD * 4 + VD * 4, EPS_L2, EPS_N, 1.0f, 1.0f, &dec_out) == 0;
+ OmegaGpuCmdListOpts opts; memset(&opts, 0, sizeof opts);
+ OmegaGpuCmdListStats stc; memset(&stc, 0, sizeof stc);
+ if (ok) ok = omega_gpu_cmdlist_submit(cl, &opts, &stc) == 0;
+ if (inb.cpu) omega_gpu_session_free(&inb);
+ omega_gpu_session_unlock();
+ if (cl) omega_gpu_cmdlist_free(cl);
+ CHECK(ok, "record prefill + decode step and submit (%s)", ok ? "" : omega_gpu_gdn_last_error());
+ if (ok) {
+ float *t = malloc((size_t)NT * 4), *s = malloc((size_t)NS * 4), *yo = malloc((size_t)T * VD * 4), *y1o = malloc((size_t)VD * 4);
+ double *td = malloc((size_t)NT * 8);
+ ok = omega_gpu_gdn_get_state(g, 0, t, s) == 0 && omega_gpu_gdn_prefill_read_out(g, T, yo) == 0 && omega_gpu_gdn_read_out(g, y1o) == 0;
+ for (size_t i = 0; i < NT; i++) td[i] = d.tail[i];
+ Errs e = { nerr(yo, y, (size_t)T * VD), nerr(t, td, NT), nerr(s, d.state, NS) };
+ const double e1 = nerr(y1o, y1, VD);
+ if (e1 > e.out) e.out = e1;
+ printf("chip %-48s err out %.3g tail %.3g state %.3g\n", "record path, prefill 37 + 1 decode step", e.out, e.tail, e.state);
+ CHECK(ok && errs_ok(&e), "record path: prefill then a decode step match the oracle");
+ PPOINT("record path, prefill 37 + 1 decode step", e, "");
+ free(t); free(s); free(yo); free(y1o); free(td);
+ }
+ free(tmp); free(y); free(y1); dstate_free(&d); pf_in_free(&p); inp_free(&nx);
+ /* the reservation pins the scratch: a longer prefill is refused, not reallocated */
+ PfIn q; pf_in_make(&q, 65, 2500, 1.0f);
+ float *o65 = malloc((size_t)65 * VD * 4);
+ CHECK(omega_gpu_gdn_prefill(g, 1, 65, q.qkv, q.z, q.ab, EPS_L2, EPS_N, 1.0f, 1.0f, o65, NULL) == OMEGA_GPU_GDN_TOO_LARGE, "prefill past the reservation is refused");
+ free(o65); pf_in_free(&q);
+ }
+ /* timing per launch at three prompt lengths (a fresh, unreserved scratch per length) */
+ const uint32_t Ts[3] = { 512, 2048, 8192 };
+ for (unsigned k = 0; k < 3; k++) {
+ const uint32_t T = Ts[k], nrep = reps < 5 ? reps : 5;
+ CHECK(omega_gpu_gdn_prefill_reserve(g, T) == 0, "reserve %u tokens", T);
+ PfIn p; pf_in_make(&p, T, 3000, 1.0f);
+ float *o = malloc((size_t)T * VD * 4);
+ uint64_t e[3][5], c[5];
+ uint32_t done = 0;
+ OmegaGpuGdnPfInfo info;
+ for (uint32_t r = 0; r < nrep; r++) {
+ if (omega_gpu_gdn_prefill(g, 0, T, p.qkv, p.z, p.ab, EPS_L2, EPS_N, 1.0f, 1.0f, o, &info) != 0) break;
+ for (unsigned j = 0; j < 3; j++) e[j][done] = info.elapsed_ns[j];
+ c[done] = info.call_ns; done++;
+ }
+ CHECK(done == nrep, "prefill T %u timing reps ran (%u of %u): %s", T, done, nrep, done == nrep ? "" : omega_gpu_gdn_last_error());
+ if (done) {
+ for (unsigned j = 0; j < 3; j++) qsort(e[j], done, 8, cmp_u64);
+ qsort(c, done, 8, cmp_u64);
+ const double us[3] = { e[0][done / 2] / 1e3, e[1][done / 2] / 1e3, e[2][done / 2] / 1e3 }, tot = us[0] + us[1] + us[2];
+ printf("timing prefill T %u (medians of %u): prep %.1f us, scan %.1f us, post %.1f us, chip total %.1f us = %.2f us/token, call %.1f us; insns %u/%u/%u, registers %u/%u/%u\n",
+ T, done, us[0], us[1], us[2], tot, tot / T, c[done / 2] / 1e3, info.insns[0], info.insns[1], info.insns[2], info.gprs[0], info.gprs[1], info.gprs[2]);
+ if (out) fprintf(out, "%s{\"name\":\"timing prefill\",\"tokens\":%u,\"reps\":%u,\"prep_us\":%.3f,\"scan_us\":%.3f,\"post_us\":%.3f,\"chip_us\":%.3f,\"us_per_token\":%.4f,\"call_us\":%.3f,\"insns\":[%u,%u,%u],\"gprs\":[%u,%u,%u]}",
+ *first ? "" : ",", T, done, us[0], us[1], us[2], tot, tot / T, c[done / 2] / 1e3, info.insns[0], info.insns[1], info.insns[2], info.gprs[0], info.gprs[1], info.gprs[2]);
+ *first = 0;
+ }
+ free(o); pf_in_free(&p);
+ }
+#undef PPOINT
+}
static int chip_run(const char *out_path, uint32_t reps) {
FILE *out = out_path ? fopen(out_path, "w") : NULL;
if (out_path && !out) { printf("cannot write %s\n", out_path); return 1; }
@@ -424,6 +715,7 @@ static int chip_run(const char *out_path, uint32_t reps) {
}
free(tmp); free(y); dstate_free(&d); inp_free(&in[0]); inp_free(&in[1]);
}
+ chip_prefill(g, L, out, &first, reps);
/* timing */
{
Inp in; inp_make(&in, 950, 1.0f);
@@ -466,7 +758,7 @@ int main(int argc, char **argv) {
else { printf("unknown argument %s\n", argv[i]); return 2; }
}
if (host) host_only();
- if (sim) sim_battery();
+ if (sim) { sim_battery(); sim_prefill_battery(); }
if (!host && !sim) {
(void)omega_gpu_session_set_spin_us(spin);
chip_run(out_path, reps ? reps : 50);