omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / evidence / gdn-prefill-20261018 / change.diff
| 1 | diff --git a/src/omega_blackwell_codegen_gdn.c b/src/omega_blackwell_codegen_gdn.c |
| 2 | index 235bace7..68185cf3 100644 |
| 3 | --- a/src/omega_blackwell_codegen_gdn.c |
| 4 | +++ b/src/omega_blackwell_codegen_gdn.c |
| 5 | @@ -4,6 +4,7 @@ |
| 6 | * omega_bw_em.h (the state loads are in flight through the conv, norm and scalar phases). |
| 7 | */ |
| 8 | #include "omega_blackwell_codegen_gdn.h" |
| 9 | +#include "omega_blackwell_codegen_tc.h" |
| 10 | #include <string.h> |
| 11 | |
| 12 | #define CW(stall, wbar, rbar, wait) \ |
| 13 | @@ -19,10 +20,14 @@ |
| 14 | |
| 15 | enum { K_FIXED, K_PRED, K_VAR, K_VAR_MORE, K_UVAR, K_STORE, K_SYNC, K_EXIT, K_BRA, K_BSSY, K_BSYNC }; |
| 16 | |
| 17 | +#define MAX_LOOPS 2 |
| 18 | + |
| 19 | typedef struct { |
| 20 | BlackwellIRProgram *p; |
| 21 | uint32_t pending, held; |
| 22 | int err; |
| 23 | + int depth, open_start[MAX_LOOPS]; |
| 24 | + uint32_t placeholders; /* SHFL.BFLY words omega_tc_patch_code must patch */ |
| 25 | } Em; |
| 26 | |
| 27 | static void emit(Em *e, BlackwellIRInsn in, int kind) { |
| 28 | @@ -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( |
| 29 | 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); } |
| 30 | 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); } |
| 31 | 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); } |
| 32 | +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); } |
| 33 | +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; } |
| 34 | 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; } |
| 35 | static void patch_bra(Em *e, int at, int target) { if (!e->err) e->p->insns[at].imm = (uint32_t)(target - at); } |
| 36 | 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; } |
| 37 | @@ -119,6 +126,23 @@ static void tail(Em *e) { |
| 38 | typedef struct { int bssy, bra; } Region; |
| 39 | static Region region_skip_if_p0(Em *e) { Region r; r.bssy = bssy_fwd(e); r.bra = bra_if_p0_fwd(e); return r; } |
| 40 | 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); } |
| 41 | +/* Loops: a value defined before the loop and read inside it stays live until the back-branch (the linear-scan |
| 42 | + * allocator knows no back edges; omega_blackwell_codegen_pattn.c). */ |
| 43 | +static void loop_begin(Em *e) { if (e->depth >= MAX_LOOPS) { e->err = -1; return; } e->open_start[e->depth++] = here(e); } |
| 44 | +static void loop_end(Em *e) { |
| 45 | + if (e->depth <= 0) { e->err = -1; return; } |
| 46 | + int start = e->open_start[--e->depth], end = here(e) - 1; |
| 47 | + OmegaRegAlloc *ra = &e->p->regalloc; |
| 48 | + for (int v = 0; v < ra->num_vregs; v++) { |
| 49 | + OmegaLiveInterval *iv = &ra->intervals[v]; |
| 50 | + if (iv->first_def >= 0 && iv->first_def < start && iv->last_use >= start && iv->last_use < end) iv->last_use = end; |
| 51 | + } |
| 52 | +} |
| 53 | +static void mov(Em *e, Op d, Op a) { imadi(e, d, a, 1u, NOREG); } |
| 54 | +/* SHFL.BFLY placeholder (omega_blackwell_codegen_tc.h), patched after encoding */ |
| 55 | +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++; } |
| 56 | +/* butterfly sum over lane strides lo, 2 lo .. hi: every lane of a group ends with the same bits (a + b == b + a) */ |
| 57 | +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)); } } |
| 58 | /* warp sum: the total lands in lane 0 */ |
| 59 | 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)); } } |
| 60 | /* lane 0 of every warp stores v to shared [addr] */ |
| 61 | @@ -354,3 +378,394 @@ int omega_gdn_prog_stats(const OmegaGdnSpec *spec, uint32_t *insns, uint32_t *gp |
| 62 | if (gprs) *gprs = prog.regalloc.peak_gpr_usage; |
| 63 | return 0; |
| 64 | } |
| 65 | + |
| 66 | +/* ======== prefill (omega#543): prep, scan, post (layouts in the header) ======== */ |
| 67 | + |
| 68 | +/* softplus, decay and beta of one value head (the decode phase C maths): abq = {a, b}, scq = {negA, dt_bias} */ |
| 69 | +static void gdn_scalars(Em *e, int MUT, int abq, int scq, int dec, int beta, int t1, int cnl2e, int fone) { |
| 70 | + int xs = V(e), mn = V(e), sp = V(e), cl2e = V(e); |
| 71 | + if (e->err) return; |
| 72 | + fadd(e, R(xs), S(abq, 0), S(scq, 1)); |
| 73 | + movi(e, mn, fbits(-1.0f)); fmul(e, R(mn), R(xs), R(mn)); fminn(e, R(mn), R(xs), R(mn)); |
| 74 | + movi(e, cl2e, fbits(1.44269504f)); |
| 75 | + 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)); |
| 76 | + mufu(e, BW_IR_MUFU_LG2, R(mn), R(mn)); |
| 77 | + movrz(e, R(sp)); fmaxx(e, R(sp), R(xs), R(sp)); |
| 78 | + movi(e, t1, fbits(0.693147181f)); ffma(e, R(sp), R(mn), R(t1), R(sp)); |
| 79 | + if (MUT == 1) movi(e, dec, fbits(1.0f)); |
| 80 | + 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)); } |
| 81 | + if (MUT == 2) movi(e, beta, fbits(1.0f)); |
| 82 | + else { |
| 83 | + fmul(e, R(beta), S(abq, 1), R(cnl2e)); mufu(e, BW_IR_MUFU_EX2, R(beta), R(beta)); |
| 84 | + fadd(e, R(beta), R(beta), R(fone)); mufu(e, BW_IR_MUFU_RCP, R(beta), R(beta)); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +int omega_blackwell_codegen_gdn_prep_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog) { |
| 89 | + if (!spec || !prog || spec->mutant < 0 || spec->mutant > 4) return -1; |
| 90 | + const int MUT = spec->mutant; |
| 91 | + const float qs = 0.0883883476f; /* 1 / sqrt(128) */ |
| 92 | + const uint32_t ROW = OMEGA_GDN_CONV_DIM * 4u, REC_ROW = OMEGA_GDN_VALUE_HEADS * OMEGA_GDN_REC_BYTES; |
| 93 | + omega_bw_ir_init(prog); |
| 94 | + Em em; memset(&em, 0, sizeof em); em.p = prog; Em *e = &em; |
| 95 | + |
| 96 | + int udesc = UV64(e); |
| 97 | + 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); |
| 98 | + int pqkv = V64(e), ppar = V64(e), ptail = V64(e), prec = V64(e), pab = V64(e), insc = V(e); |
| 99 | + int ch[5], tw[5], wq[5], ax[5]; |
| 100 | + for (unsigned i = 0; i < 5; i++) { ch[i] = V(e); tw[i] = V128(e); wq[i] = V128(e); ax[i] = V64(e); } |
| 101 | + if (e->err) return -1; |
| 102 | + ldcu64(e, udesc, 0x358); |
| 103 | + s2r(e, c, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, hk, BW_SR_CTAID_Y); |
| 104 | + ldc64(e, pqkv, OMEGA_GDN_PREP_ARG_QKV); ldc64(e, ppar, OMEGA_GDN_PREP_ARG_PAR); ldc64(e, ptail, OMEGA_GDN_PREP_ARG_TAIL); |
| 105 | + ldc64(e, prec, OMEGA_GDN_PREP_ARG_REC); ldc64(e, pab, OMEGA_GDN_PREP_ARG_AB); |
| 106 | + ldc32(e, T, OMEGA_GDN_PREP_ARG_T); ldc32(e, insc, OMEGA_GDN_PREP_ARG_IN_SCALE); |
| 107 | + movi(e, one, 1); |
| 108 | + shr(e, warp, c, 5); |
| 109 | + andi(e, R(lane), R(c), 31u); |
| 110 | + imadi(e, R(t0), R(cx), OMEGA_GDN_PREP_TOKENS, NOREG); |
| 111 | + /* n = min(16, T - t0) >= 1 */ |
| 112 | + imadi(e, R(n), R(t0), 0xffffffffu, T); |
| 113 | + movi(e, t1, OMEGA_GDN_PREP_TOKENS); |
| 114 | + isetp_ge_u32(e, t1, n); |
| 115 | + { int b = bra_if_p0_fwd(e); movi(e, n, OMEGA_GDN_PREP_TOKENS); patch_bra(e, b, here(e)); } |
| 116 | + /* channels: q hk*128 + c, k 2048 + hk*128 + c, v_i 4096 + (3 hk + i)*128 + c */ |
| 117 | + imadi(e, R(ch[0]), R(hk), OMEGA_GDN_HEAD_DIM, c); |
| 118 | + imadi(e, R(ch[1]), R(one), 2048u, ch[0]); |
| 119 | + imadi(e, R(ch[2]), R(hk), 3u * OMEGA_GDN_HEAD_DIM, c); imadi(e, R(ch[2]), R(one), 4096u, ch[2]); |
| 120 | + imadi(e, R(ch[3]), R(one), OMEGA_GDN_HEAD_DIM, ch[2]); |
| 121 | + imadi(e, R(ch[4]), R(one), 2u * OMEGA_GDN_HEAD_DIM, ch[2]); |
| 122 | + /* taps */ |
| 123 | + { |
| 124 | + int aq = V64(e), av = V64(e); |
| 125 | + if (e->err) return -1; |
| 126 | + imadw(e, aq, ch[0], 16u, ppar); imadw(e, av, ch[2], 16u, ppar); |
| 127 | + ldg_wide(e, BW_IR_LDG_E_128, tw[0], aq, 0, udesc, 1); |
| 128 | + ldg_wide(e, BW_IR_LDG_E_128, tw[1], aq, 2048u * 16u, udesc, 0); |
| 129 | + 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); |
| 130 | + } |
| 131 | + /* the window {x_{t0-3}, x_{t0-2}, x_{t0-1}}: CTA 0 from the tail, the others from the rows before t0 (uniform) */ |
| 132 | + isetp_ge_u32(e, cx, one); |
| 133 | + const int b_rows = bra_if_p0_fwd(e); |
| 134 | + { |
| 135 | + int at = V64(e); |
| 136 | + if (e->err) return -1; |
| 137 | + imadi(e, R(t1), R(hk), 3u * OMEGA_GDN_TAIL_HEAD_BYTES, NOREG); |
| 138 | + imadw(e, at, t1, 1u, ptail); imadw(e, at, c, 16u, at); |
| 139 | + ldg_wide(e, BW_IR_LDG_E_128, wq[0], at, 0, udesc, 1); |
| 140 | + ldg_wide(e, BW_IR_LDG_E_128, wq[1], at, OMEGA_GDN_HEAD_DIM * 16u, udesc, 0); |
| 141 | + 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); |
| 142 | + } |
| 143 | + const int b_join = bra_fwd(e); |
| 144 | + patch_bra(e, b_rows, here(e)); |
| 145 | + { |
| 146 | + int base = V64(e), a[5][3]; |
| 147 | + for (unsigned i = 0; i < 5; i++) for (unsigned r = 0; r < 3; r++) a[i][r] = V64(e); |
| 148 | + if (e->err) return -1; |
| 149 | + imadi(e, R(t1), R(one), 0xfffffffdu, t0); /* t0 - 3 (t0 >= 16 here) */ |
| 150 | + imadw(e, base, t1, ROW, pqkv); |
| 151 | + for (unsigned i = 0; i < 5; i++) { |
| 152 | + imadw(e, a[i][0], ch[i], 4u, base); |
| 153 | + imadw(e, a[i][1], one, ROW, a[i][0]); imadw(e, a[i][2], one, ROW, a[i][1]); |
| 154 | + } |
| 155 | + for (unsigned i = 0; i < 5; i++) for (unsigned r = 0; r < 3; r++) { |
| 156 | + 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; |
| 157 | + emit(e, in, (i == 0 && r == 0) ? K_VAR : K_VAR_MORE); |
| 158 | + } |
| 159 | + 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)); |
| 160 | + } |
| 161 | + patch_bra(e, b_join, here(e)); |
| 162 | + |
| 163 | + /* per-thread addresses at token t0 */ |
| 164 | + int idx = V(e), arec = V64(e), asc = V64(e), aab = V64(e), scq = V64(e); |
| 165 | + 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); |
| 166 | + if (e->err) return -1; |
| 167 | + { |
| 168 | + int base = V64(e); |
| 169 | + if (e->err) return -1; |
| 170 | + imadw(e, base, t0, ROW, pqkv); |
| 171 | + for (unsigned i = 0; i < 5; i++) imadw(e, ax[i], ch[i], 4u, base); |
| 172 | + } |
| 173 | + imadi(e, R(idx), R(t0), OMEGA_GDN_VALUE_HEADS, NOREG); imadi(e, R(idx), R(hk), 3u, idx); |
| 174 | + imadw(e, arec, idx, OMEGA_GDN_REC_BYTES, prec); imadw(e, arec, c, 4u, arec); |
| 175 | + imadi(e, R(t1), R(c), 1u, idx); /* the scalar threads' head index t0*48 + 3hk + c */ |
| 176 | + imadw(e, asc, t1, OMEGA_GDN_REC_BYTES, prec); |
| 177 | + imadw(e, aab, t1, 8u, pab); |
| 178 | + movi(e, c3, 3u); |
| 179 | + isetp_ge_u32(e, c, c3); |
| 180 | + { |
| 181 | + Region r = region_skip_if_p0(e); |
| 182 | + int as = V64(e); |
| 183 | + if (e->err) return -1; |
| 184 | + imadi(e, R(t1), R(hk), 3u, c); |
| 185 | + imadw(e, as, t1, 8u, ppar); |
| 186 | + ldg_wide(e, BW_IR_LDG_E_64, scq, as, OMEGA_GDN_PAR_SCAL, udesc, 1); |
| 187 | + region_end(e, r); |
| 188 | + } |
| 189 | + movi(e, cnl2e, fbits(-1.44269504f)); movi(e, fone, fbits(1.0f)); movi(e, cqs, fbits(qs)); |
| 190 | + ldc32(e, el2, OMEGA_GDN_PREP_ARG_EPS_L2); |
| 191 | + movi(e, zero, 0u); |
| 192 | + imadi(e, R(raq), R(warp), 4u, NOREG); imadi(e, R(rak), R(one), 16u, raq); |
| 193 | + |
| 194 | + const int L = here(e); |
| 195 | + loop_begin(e); |
| 196 | + { |
| 197 | + 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); |
| 198 | + for (unsigned i = 0; i < 5; i++) { x[i] = V(e); acc[i] = V(e); } |
| 199 | + if (e->err) return -1; |
| 200 | + for (unsigned i = 0; i < 5; i++) ldg32(e, x[i], ax[i], udesc, i == 0); |
| 201 | + for (unsigned i = 0; i < 5; i++) { |
| 202 | + fmul(e, R(x[i]), R(x[i]), R(insc)); |
| 203 | + 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])); |
| 204 | + 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])); |
| 205 | + silu(e, R(acc[i]), t1, cnl2e, fone); |
| 206 | + 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])); |
| 207 | + } |
| 208 | + /* l2 norms of q and k across the 4 warps (the decode order: warp sums, then partials 0 + 1 + 2 + 3) */ |
| 209 | + fmul(e, R(sq), R(acc[0]), R(acc[0])); warp_sum(e, sq, ts); lane0_sts(e, lane, one, raq, R(sq)); |
| 210 | + fmul(e, R(sk), R(acc[1]), R(acc[1])); warp_sum(e, sk, ts); lane0_sts(e, lane, one, rak, R(sk)); |
| 211 | + bar_sync(e); |
| 212 | + lds128(e, rq, zero, 0, 1); lds128(e, rk, zero, 16, 0); |
| 213 | + 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)); |
| 214 | + 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)); |
| 215 | + mufu(e, BW_IR_MUFU_RSQ, R(nq), R(nq)); mufu(e, BW_IR_MUFU_RSQ, R(nk), R(nk)); |
| 216 | + fmul(e, R(acc[0]), R(acc[0]), R(nq)); fmul(e, R(acc[0]), R(acc[0]), R(cqs)); |
| 217 | + fmul(e, R(acc[1]), R(acc[1]), R(nk)); |
| 218 | + for (unsigned i = 0; i < 3; i++) { |
| 219 | + stg(e, arec, i * OMEGA_GDN_REC_BYTES, R(acc[0]), udesc); |
| 220 | + stg(e, arec, i * OMEGA_GDN_REC_BYTES + OMEGA_GDN_REC_K, R(acc[1]), udesc); |
| 221 | + stg(e, arec, i * OMEGA_GDN_REC_BYTES + OMEGA_GDN_REC_V, R(acc[2 + i]), udesc); |
| 222 | + } |
| 223 | + /* threads 0..2: decay and beta of value head 3 hk + c */ |
| 224 | + isetp_ge_u32(e, c, c3); |
| 225 | + { |
| 226 | + Region r = region_skip_if_p0(e); |
| 227 | + int abq = V64(e), dec = V(e), beta = V(e); |
| 228 | + if (e->err) return -1; |
| 229 | + ldg_wide(e, BW_IR_LDG_E_64, abq, aab, 0, udesc, 1); |
| 230 | + gdn_scalars(e, MUT, abq, scq, dec, beta, t1, cnl2e, fone); |
| 231 | + stg(e, asc, OMEGA_GDN_REC_SC, R(dec), udesc); stg(e, asc, OMEGA_GDN_REC_SC + 4u, R(beta), udesc); |
| 232 | + region_end(e, r); |
| 233 | + } |
| 234 | + bar_sync(e); /* every warp has read the partials before the next token writes them */ |
| 235 | + for (unsigned i = 0; i < 5; i++) imadw(e, ax[i], one, ROW, ax[i]); |
| 236 | + imadw(e, arec, one, REC_ROW, arec); imadw(e, asc, one, REC_ROW, asc); |
| 237 | + imadw(e, aab, one, OMEGA_GDN_VALUE_HEADS * 8u, aab); |
| 238 | + imadi(e, R(n), R(one), 0xffffffffu, n); |
| 239 | + isetp_ge_u32(e, n, one); |
| 240 | + bra_if_p0(e, L); |
| 241 | + } |
| 242 | + loop_end(e); |
| 243 | + tail(e); |
| 244 | + if (e->err) return -1; |
| 245 | + return omega_bw_regalloc_solve(prog); |
| 246 | +} |
| 247 | + |
| 248 | +int omega_blackwell_codegen_gdn_scan_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog) { |
| 249 | + if (!spec || !prog || spec->mutant < 0 || spec->mutant > 4) return -1; |
| 250 | + const int MUT = spec->mutant; |
| 251 | + const uint32_t ROW = OMEGA_GDN_CONV_DIM * 4u, CHUNK_ROW = OMEGA_GDN_SCAN_CHUNK * OMEGA_GDN_VALUE_HEADS * OMEGA_GDN_REC_BYTES; |
| 252 | + omega_bw_ir_init(prog); |
| 253 | + Em em; memset(&em, 0, sizeof em); em.p = prog; Em *e = &em; |
| 254 | + |
| 255 | + int udesc = UV64(e); |
| 256 | + 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); |
| 257 | + int T = V(e), tm1 = V(e), t = V(e), t1 = V(e), sl = V(e), srec = V(e); |
| 258 | + int ps = V64(e), sa = V64(e), prec = V64(e), grec = V64(e), gsc = V64(e), po = V64(e), sq[16]; |
| 259 | + for (unsigned r = 0; r < 16; r++) sq[r] = V128(e); |
| 260 | + if (e->err) return -1; |
| 261 | + ldcu64(e, udesc, 0x358); |
| 262 | + s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y); |
| 263 | + ldc64(e, ps, OMEGA_GDN_SCAN_ARG_STATE); ldc64(e, prec, OMEGA_GDN_SCAN_ARG_REC); ldc64(e, po, OMEGA_GDN_SCAN_ARG_O); |
| 264 | + ldc32(e, T, OMEGA_GDN_SCAN_ARG_T); |
| 265 | + movi(e, one, 1); |
| 266 | + imadi(e, R(h), R(cy), 3u, cx); |
| 267 | + shr(e, warp, tid, 5); |
| 268 | + andi(e, R(lane), R(tid), 31u); |
| 269 | + andi(e, R(cq), R(lane), 3u); |
| 270 | + shr(e, rg, lane, 2); |
| 271 | + /* this thread's block of S: rows 16 rg .. +15, columns c0 = 16 warp + 4 cq .. +3 */ |
| 272 | + imadi(e, R(c0), R(warp), 16u, NOREG); imadi(e, R(c0), R(cq), 4u, c0); |
| 273 | + imadi(e, R(t1), R(h), OMEGA_GDN_STATE_HEAD_BYTES, NOREG); |
| 274 | + imadi(e, R(t1), R(rg), 16u * OMEGA_GDN_HEAD_DIM * 4u, t1); |
| 275 | + imadi(e, R(t1), R(c0), 4u, t1); |
| 276 | + imadw(e, sa, t1, 1u, ps); |
| 277 | + 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); |
| 278 | + imadi(e, R(tm1), R(one), 0xffffffffu, T); |
| 279 | + /* the record copy: warp w brings token tc + w into shared slot w */ |
| 280 | + imadi(e, R(t1), R(warp), OMEGA_GDN_VALUE_HEADS, h); |
| 281 | + imadw(e, gsc, t1, OMEGA_GDN_REC_BYTES, prec); |
| 282 | + imadw(e, grec, lane, 16u, gsc); |
| 283 | + imadi(e, R(srec), R(warp), OMEGA_GDN_REC_BYTES, NOREG); |
| 284 | + imadi(e, R(sl), R(lane), 16u, srec); |
| 285 | + /* o row of (token 0, head h), columns c0 .. c0 + 3 */ |
| 286 | + imadi(e, R(t1), R(h), OMEGA_GDN_HEAD_DIM, c0); |
| 287 | + imadw(e, po, t1, 4u, po); |
| 288 | + movrz(e, R(t)); |
| 289 | + |
| 290 | + const int L = here(e); |
| 291 | + loop_begin(e); |
| 292 | + { |
| 293 | + 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]; |
| 294 | + for (unsigned j = 0; j < 4; j++) { kv[j] = V(e); dl[j] = V(e); p[j] = V(e); } |
| 295 | + if (e->err) return -1; |
| 296 | + andi(e, R(tm), R(t), OMEGA_GDN_SCAN_CHUNK - 1u); |
| 297 | + isetp_ge_u32(e, tm, one); |
| 298 | + const int b_copy = bra_if_p0_fwd(e); |
| 299 | + { |
| 300 | + int rr[4], a = V(e); |
| 301 | + for (unsigned k = 0; k < 4; k++) rr[k] = V128(e); |
| 302 | + if (e->err) return -1; |
| 303 | + bar_sync(e); /* every warp is done with the last chunk */ |
| 304 | + for (unsigned k = 0; k < 3; k++) ldg_wide(e, BW_IR_LDG_E_128, rr[k], grec, k * 512u, udesc, k == 0); |
| 305 | + ldg_wide(e, BW_IR_LDG_E_128, rr[3], gsc, OMEGA_GDN_REC_SC, udesc, 0); |
| 306 | + for (unsigned k = 0; k < 3; k++) for (unsigned j = 0; j < 4; j++) { |
| 307 | + if (k == 0 && j == 0) sts32(e, sl, S(rr[0], 0)); |
| 308 | + else { imadi(e, R(a), R(one), k * 512u + j * 4u, sl); sts32(e, a, S(rr[k], j)); } |
| 309 | + } |
| 310 | + 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)); } |
| 311 | + bar_sync(e); |
| 312 | + imadw(e, grec, one, CHUNK_ROW, grec); imadw(e, gsc, one, CHUNK_ROW, gsc); |
| 313 | + } |
| 314 | + patch_bra(e, b_copy, here(e)); |
| 315 | + imadi(e, R(sb), R(tm), OMEGA_GDN_REC_BYTES, NOREG); |
| 316 | + imadi(e, R(ak), R(rg), 16u * 4u, sb); |
| 317 | + imadi(e, R(av), R(c0), 4u, sb); |
| 318 | + lds128(e, scq, sb, OMEGA_GDN_REC_SC, 1); |
| 319 | + 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)); |
| 320 | + /* kv_j = sum over 128 rows of S[r][j] k[r]: 16 rows here, then lanes ^ 4, 8, 16 (the 8 row groups) */ |
| 321 | + for (unsigned i = 0; i < 4; i++) { |
| 322 | + int kq = V128(e); |
| 323 | + if (e->err) return -1; |
| 324 | + lds128(e, kq, ak, OMEGA_GDN_REC_K + i * 16u, 1); |
| 325 | + for (unsigned r = 4 * i; r < 4 * i + 4; r++) for (unsigned j = 0; j < 4; j++) { |
| 326 | + if (r == 0) fmul(e, R(kv[j]), S(sq[0], j), S(kq, 0)); |
| 327 | + else ffma(e, R(kv[j]), S(sq[r], j), S(kq, r % 4), R(kv[j])); |
| 328 | + } |
| 329 | + } |
| 330 | + for (unsigned j = 0; j < 4; j++) bfly_sum(e, kv[j], ts, 4u, 16u); |
| 331 | + lds128(e, vq, av, OMEGA_GDN_REC_V, 1); |
| 332 | + 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)); } |
| 333 | + for (unsigned i = 0; i < 4; i++) { |
| 334 | + int kq = V128(e); |
| 335 | + if (e->err) return -1; |
| 336 | + lds128(e, kq, ak, OMEGA_GDN_REC_K + i * 16u, 1); |
| 337 | + 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)); |
| 338 | + } |
| 339 | + for (unsigned i = 0; i < 4; i++) { |
| 340 | + int qq = V128(e); |
| 341 | + if (e->err) return -1; |
| 342 | + lds128(e, qq, ak, i * 16u, 1); |
| 343 | + for (unsigned r = 4 * i; r < 4 * i + 4; r++) for (unsigned j = 0; j < 4; j++) { |
| 344 | + if (r == 0) fmul(e, R(p[j]), S(sq[0], j), S(qq, 0)); |
| 345 | + else ffma(e, R(p[j]), S(sq[r], j), S(qq, r % 4), R(p[j])); |
| 346 | + } |
| 347 | + } |
| 348 | + for (unsigned j = 0; j < 4; j++) bfly_sum(e, p[j], ts, 4u, 16u); |
| 349 | + isetp_ge_u32(e, rg, one); |
| 350 | + { |
| 351 | + Region r = region_skip_if_p0(e); |
| 352 | + for (unsigned j = 0; j < 4; j++) stg(e, po, j * 4u, R(p[j]), udesc); |
| 353 | + region_end(e, r); |
| 354 | + } |
| 355 | + imadw(e, po, one, OMEGA_GDN_V_DIM * 4u, po); |
| 356 | + imadi(e, R(t), R(one), 1u, t); |
| 357 | + isetp_ge_u32(e, tm1, t); |
| 358 | + bra_if_p0(e, L); |
| 359 | + } |
| 360 | + loop_end(e); |
| 361 | + 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); |
| 362 | + |
| 363 | + /* the new tail: qkv rows T-3 .. T-1 (times in_scale) of slot A (tid) and slot V (256 + (tid & 127)), as decode */ |
| 364 | + if (MUT != 4) { |
| 365 | + 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); |
| 366 | + int atA = V64(e), atV = V64(e), aA[3], aV[3], xA[3], xV[3]; |
| 367 | + for (unsigned r = 0; r < 3; r++) { aA[r] = V64(e); aV[r] = V64(e); xA[r] = V(e); xV[r] = V(e); } |
| 368 | + if (e->err) return -1; |
| 369 | + 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); |
| 370 | + shr(e, half, tid, 7); |
| 371 | + andi(e, R(cc), R(tid), 127u); |
| 372 | + imadi(e, R(gA), R(cy), OMEGA_GDN_HEAD_DIM, tid); imadi(e, R(gA), R(half), 2048u - 128u, gA); |
| 373 | + 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); |
| 374 | + imadi(e, R(t1), R(one), 0xfffffffdu, T); |
| 375 | + imadw(e, base, t1, ROW, pqkv); |
| 376 | + imadw(e, aA[0], gA, 4u, base); imadw(e, aV[0], gV, 4u, base); |
| 377 | + 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]); } |
| 378 | + for (unsigned r = 0; r < 3; r++) { ldg32(e, xA[r], aA[r], udesc, r == 0); ldg32(e, xV[r], aV[r], udesc, 0); } |
| 379 | + 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)); } |
| 380 | + imadw(e, atA, tid, 16u, ptail); imadw(e, atA, h, OMEGA_GDN_TAIL_HEAD_BYTES, atA); |
| 381 | + imadi(e, R(t1), R(one), 256u, cc); |
| 382 | + imadw(e, atV, t1, 16u, ptail); imadw(e, atV, h, OMEGA_GDN_TAIL_HEAD_BYTES, atV); |
| 383 | + 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); } |
| 384 | + } |
| 385 | + tail(e); |
| 386 | + if (e->err) return -1; |
| 387 | + return omega_bw_regalloc_solve(prog); |
| 388 | +} |
| 389 | + |
| 390 | +int omega_blackwell_codegen_gdn_post_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog) { |
| 391 | + if (!spec || !prog || spec->mutant < 0 || spec->mutant > 4) return -1; |
| 392 | + const int MUT = spec->mutant; |
| 393 | + omega_bw_ir_init(prog); |
| 394 | + Em em; memset(&em, 0, sizeof em); em.p = prog; Em *e = &em; |
| 395 | + |
| 396 | + int udesc = UV64(e); |
| 397 | + 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); |
| 398 | + int en = V(e), osc = V(e), insc = V(e), cinv = V(e), cnl2e = V(e), fone = V(e); |
| 399 | + 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); |
| 400 | + int oq = V128(e), zq = V128(e), nq = V128(e); |
| 401 | + if (e->err) return -1; |
| 402 | + ldcu64(e, udesc, 0x358); |
| 403 | + s2r(e, tid, BW_SR_TID_X); s2r(e, t, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y); |
| 404 | + ldc64(e, po, OMEGA_GDN_POST_ARG_O); ldc64(e, pz, OMEGA_GDN_POST_ARG_Z); ldc64(e, ppar, OMEGA_GDN_POST_ARG_PAR); |
| 405 | + ldc64(e, pout, OMEGA_GDN_POST_ARG_OUT); |
| 406 | + 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); |
| 407 | + shr(e, warp, tid, 5); |
| 408 | + andi(e, R(lane), R(tid), 31u); |
| 409 | + /* row (token t, head 8 cy + warp): o, z and out share the [T][48][128] layout */ |
| 410 | + imadi(e, R(idx), R(cy), 8u, warp); |
| 411 | + imadi(e, R(idx), R(t), OMEGA_GDN_VALUE_HEADS, idx); |
| 412 | + imadw(e, ao, idx, OMEGA_GDN_HEAD_DIM * 4u, po); imadw(e, ao, lane, 16u, ao); |
| 413 | + imadw(e, az, idx, OMEGA_GDN_HEAD_DIM * 4u, pz); imadw(e, az, lane, 16u, az); |
| 414 | + imadw(e, ay, idx, OMEGA_GDN_HEAD_DIM * 4u, pout); imadw(e, ay, lane, 16u, ay); |
| 415 | + imadw(e, an, lane, 16u, ppar); |
| 416 | + ldg_wide(e, BW_IR_LDG_E_128, oq, ao, 0, udesc, 1); |
| 417 | + ldg_wide(e, BW_IR_LDG_E_128, zq, az, 0, udesc, 0); |
| 418 | + ldg_wide(e, BW_IR_LDG_E_128, nq, an, OMEGA_GDN_PAR_NORM, udesc, 0); |
| 419 | + fmul(e, R(ss), S(oq, 0), S(oq, 0)); |
| 420 | + for (unsigned j = 1; j < 4; j++) ffma(e, R(ss), S(oq, j), S(oq, j), R(ss)); |
| 421 | + bfly_sum(e, ss, ts, 1u, 16u); |
| 422 | + movi(e, cinv, fbits(1.0f / 128.0f)); |
| 423 | + ffma(e, R(ss), R(ss), R(cinv), R(en)); |
| 424 | + mufu(e, BW_IR_MUFU_RSQ, R(ss), R(ss)); |
| 425 | + movi(e, cnl2e, fbits(-1.44269504f)); movi(e, fone, fbits(1.0f)); |
| 426 | + for (unsigned j = 0; j < 4; j++) { |
| 427 | + fmul(e, R(g), S(zq, j), R(insc)); |
| 428 | + if (MUT != 3) silu(e, R(g), t1, cnl2e, fone); |
| 429 | + fmul(e, S(oq, j), S(oq, j), R(ss)); fmul(e, S(oq, j), S(oq, j), S(nq, j)); |
| 430 | + fmul(e, S(oq, j), S(oq, j), R(g)); fmul(e, S(oq, j), S(oq, j), R(osc)); |
| 431 | + } |
| 432 | + for (unsigned j = 0; j < 4; j++) stg(e, ay, j * 4u, S(oq, j), udesc); |
| 433 | + tail(e); |
| 434 | + if (e->err) return -1; |
| 435 | + return omega_bw_regalloc_solve(prog); |
| 436 | +} |
| 437 | + |
| 438 | +/* number of SHFL.BFLY placeholders in a program (what omega_tc_patch_code must report) */ |
| 439 | +uint32_t omega_gdn_bfly_count(const BlackwellIRProgram *prog) { |
| 440 | + uint32_t n = 0; |
| 441 | + for (size_t i = 0; i < prog->count; i++) |
| 442 | + if (prog->insns[i].op == BW_IR_SHFL_DOWN && (prog->insns[i].imm & OMEGA_TC_MARK_BFLY)) n++; |
| 443 | + return n; |
| 444 | +} |
| 445 | + |
| 446 | +int omega_gdn_pf_prog_stats(unsigned kind, const OmegaGdnSpec *spec, uint32_t *insns, uint32_t *gprs) { |
| 447 | + static BlackwellIRProgram prog; |
| 448 | + int rc = kind == 0 ? omega_blackwell_codegen_gdn_prep_prog(spec, &prog) |
| 449 | + : kind == 1 ? omega_blackwell_codegen_gdn_scan_prog(spec, &prog) |
| 450 | + : kind == 2 ? omega_blackwell_codegen_gdn_post_prog(spec, &prog) : -1; |
| 451 | + if (rc != 0) return -1; |
| 452 | + if (insns) *insns = (uint32_t)prog.count; |
| 453 | + if (gprs) *gprs = prog.regalloc.peak_gpr_usage; |
| 454 | + return 0; |
| 455 | +} |
| 456 | diff --git a/src/omega_blackwell_codegen_gdn.h b/src/omega_blackwell_codegen_gdn.h |
| 457 | index 10b7079d..8015cc83 100644 |
| 458 | --- a/src/omega_blackwell_codegen_gdn.h |
| 459 | +++ b/src/omega_blackwell_codegen_gdn.h |
| 460 | @@ -89,4 +89,61 @@ int omega_blackwell_codegen_gdn_prog(const OmegaGdnSpec *spec, BlackwellIRProgra |
| 461 | /* Instruction and peak-register counts (host, no device). */ |
| 462 | int omega_gdn_prog_stats(const OmegaGdnSpec *spec, uint32_t *insns, uint32_t *gprs); |
| 463 | |
| 464 | +/* ---- prefill (omega#543, 2026-10-18): three launches per layer over T >= 3 new tokens, the same tail and state ---- |
| 465 | + * 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 |
| 466 | + * of the three value heads), sliding each window in registers over 16 tokens (the CTA starting at token 0 takes |
| 467 | + * its window from the layer's tail, the others from the qkv rows before it), then SiLU, the q/k l2 norms (two |
| 468 | + * barriers per token) and, threads 0..2, the scalars of the three value heads. It writes one record per (token, |
| 469 | + * value head): q^ | k^ | v | {decay, beta, 0, 0}. It reads the tail and never writes it. |
| 470 | + * scan grid 3 x 16 (one CTA per value head), 256 threads. Thread (warp w, lane L) holds S rows 16 (L >> 2) .. +15, |
| 471 | + * columns 16 w + 4 (L & 3) .. +3 in registers for the whole prompt. Per 8-token chunk every warp copies one |
| 472 | + * token's record into shared memory (two barriers); per token kv and o are summed across the 8 row groups by |
| 473 | + * SHFL.BFLY (lanes ^ 4, 8, 16; a butterfly gives every lane the same bits), no barrier. It writes o (before the |
| 474 | + * norm) per token, the state at the end and the new tail (the last three qkv rows). |
| 475 | + * post grid (T, 6), 256 threads: warp w normalises (token, head 8 ctaid.y + w): gated RMSNorm, silu(z), out_scale. |
| 476 | + * The mutants keep the decode meanings: 1 and 2 act in prep, 3 in post, 4 in scan. */ |
| 477 | +#define OMEGA_GDN_REC_BYTES 1552u |
| 478 | +#define OMEGA_GDN_REC_K 512u |
| 479 | +#define OMEGA_GDN_REC_V 1024u |
| 480 | +#define OMEGA_GDN_REC_SC 1536u |
| 481 | +#define OMEGA_GDN_PF_MIN_T 3u |
| 482 | +#define OMEGA_GDN_PREP_TOKENS 16u |
| 483 | +#define OMEGA_GDN_PREP_THREADS 128u |
| 484 | +#define OMEGA_GDN_PREP_SHARED_BYTES 32u /* per-warp sums of squares: q [4] | k [4] */ |
| 485 | +#define OMEGA_GDN_PREP_ARG_QKV 0x380u /* f32 [T][10240] (times in_scale) */ |
| 486 | +#define OMEGA_GDN_PREP_ARG_AB 0x388u /* f32 [T][48][2] */ |
| 487 | +#define OMEGA_GDN_PREP_ARG_PAR 0x390u |
| 488 | +#define OMEGA_GDN_PREP_ARG_TAIL 0x398u |
| 489 | +#define OMEGA_GDN_PREP_ARG_REC 0x3a0u /* [T rounded up to 8][48] records */ |
| 490 | +#define OMEGA_GDN_PREP_ARG_T 0x3a8u /* u32 */ |
| 491 | +#define OMEGA_GDN_PREP_ARG_EPS_L2 0x3acu |
| 492 | +#define OMEGA_GDN_PREP_ARG_IN_SCALE 0x3b0u |
| 493 | +#define OMEGA_GDN_PREP_ARGS_WORDS 13u |
| 494 | +#define OMEGA_GDN_SCAN_CHUNK 8u |
| 495 | +#define OMEGA_GDN_SCAN_SHARED_BYTES (OMEGA_GDN_SCAN_CHUNK * OMEGA_GDN_REC_BYTES) /* 12416 */ |
| 496 | +#define OMEGA_GDN_SCAN_ARG_REC 0x380u |
| 497 | +#define OMEGA_GDN_SCAN_ARG_QKV 0x388u |
| 498 | +#define OMEGA_GDN_SCAN_ARG_TAIL 0x390u |
| 499 | +#define OMEGA_GDN_SCAN_ARG_STATE 0x398u |
| 500 | +#define OMEGA_GDN_SCAN_ARG_O 0x3a0u /* f32 [T][48][128] */ |
| 501 | +#define OMEGA_GDN_SCAN_ARG_T 0x3a8u |
| 502 | +#define OMEGA_GDN_SCAN_ARG_IN_SCALE 0x3acu |
| 503 | +#define OMEGA_GDN_SCAN_ARGS_WORDS 12u |
| 504 | +#define OMEGA_GDN_POST_ARG_O 0x380u |
| 505 | +#define OMEGA_GDN_POST_ARG_Z 0x388u /* f32 [T][6144] (times in_scale) */ |
| 506 | +#define OMEGA_GDN_POST_ARG_PAR 0x390u |
| 507 | +#define OMEGA_GDN_POST_ARG_OUT 0x398u /* f32 [T][6144] */ |
| 508 | +#define OMEGA_GDN_POST_ARG_EPS_NORM 0x3a0u |
| 509 | +#define OMEGA_GDN_POST_ARG_IN_SCALE 0x3a4u |
| 510 | +#define OMEGA_GDN_POST_ARG_OUT_SCALE 0x3a8u |
| 511 | +#define OMEGA_GDN_POST_ARGS_WORDS 11u |
| 512 | +#define OMEGA_GDN_POST_GRID_Y 6u |
| 513 | + |
| 514 | +int omega_blackwell_codegen_gdn_prep_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog); |
| 515 | +int omega_blackwell_codegen_gdn_scan_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog); |
| 516 | +int omega_blackwell_codegen_gdn_post_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog); |
| 517 | +/* SHFL.BFLY placeholders in a built program; prefill stats: kind 0 prep, 1 scan, 2 post. */ |
| 518 | +uint32_t omega_gdn_bfly_count(const BlackwellIRProgram *prog); |
| 519 | +int omega_gdn_pf_prog_stats(unsigned kind, const OmegaGdnSpec *spec, uint32_t *insns, uint32_t *gprs); |
| 520 | + |
| 521 | #endif |
| 522 | diff --git a/src/omega_gpu_gdn_api.c b/src/omega_gpu_gdn_api.c |
| 523 | index 6df97a82..128d7f98 100644 |
| 524 | --- a/src/omega_gpu_gdn_api.c |
| 525 | +++ b/src/omega_gpu_gdn_api.c |
| 526 | @@ -6,6 +6,7 @@ |
| 527 | #include "omega_gpu_gdn_api.h" |
| 528 | #include "omega_gpu_session.h" |
| 529 | #include "omega_gpu_cmdlist.h" |
| 530 | +#include "omega_blackwell_codegen_tc.h" |
| 531 | #include <math.h> |
| 532 | #include <stdbool.h> |
| 533 | #include <string.h> |
| 534 | @@ -26,6 +27,7 @@ struct OmegaGpuGdn { |
| 535 | OmegaGpuMem tail; /* [layers][OMEGA_GDN_TAIL_BYTES] */ |
| 536 | OmegaGpuMem state; /* [layers][OMEGA_GDN_STATE_BYTES] */ |
| 537 | OmegaGpuMem lb; /* launch buffer */ |
| 538 | + OmegaGpuScratch pf; /* prefill scratch (see pf_layout) */ |
| 539 | }; |
| 540 | |
| 541 | typedef struct { |
| 542 | @@ -36,15 +38,20 @@ typedef struct { |
| 543 | uint32_t insns; |
| 544 | } Slot; |
| 545 | |
| 546 | +/* the resident kernels: decode, prefill prep, scan, post */ |
| 547 | +enum { KK_DECODE, KK_PREP, KK_SCAN, KK_POST, KK_N }; |
| 548 | +static const uint32_t kk_threads[KK_N] = { OMEGA_GDN_THREADS, OMEGA_GDN_PREP_THREADS, OMEGA_GDN_THREADS, OMEGA_GDN_THREADS }; |
| 549 | static struct { |
| 550 | bool hooked; |
| 551 | - Slot slot; |
| 552 | + Slot slot[KK_N]; |
| 553 | int mutant; |
| 554 | } g_k; |
| 555 | static int g_rec; /* 1 while a record call runs (lock held): kernel_for must not evict */ |
| 556 | |
| 557 | -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); } |
| 558 | -static void on_session_close(void) { memset(&g_k.slot, 0, sizeof g_k.slot); } |
| 559 | +static void slot_clear_locked(void) { |
| 560 | + 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]); } |
| 561 | +} |
| 562 | +static void on_session_close(void) { memset(g_k.slot, 0, sizeof g_k.slot); } |
| 563 | static OmegaGpuSession *dev_open_locked(void) { |
| 564 | if (!g_k.hooked) { (void)omega_gpu_session_on_close(on_session_close); g_k.hooked = true; } |
| 565 | return omega_gpu_session_open(); |
| 566 | @@ -142,6 +149,7 @@ void omega_gpu_gdn_free(OmegaGpuGdn *g) { |
| 567 | if (g->tail.cpu) omega_gpu_session_free(&g->tail); |
| 568 | if (g->state.cpu) omega_gpu_session_free(&g->state); |
| 569 | if (g->lb.cpu) omega_gpu_session_free(&g->lb); |
| 570 | + omega_gpu_session_scratch_free(&g->pf); |
| 571 | omega_gpu_session_unlock(); |
| 572 | omega_gpu_host_free(g); |
| 573 | } |
| 574 | @@ -192,24 +200,37 @@ int omega_gpu_gdn_get_state(OmegaGpuGdn *g, uint32_t layer, float *tail, float * |
| 575 | } |
| 576 | |
| 577 | /* ---- kernel ---- */ |
| 578 | -static int kernel_for(const OmegaGdnSpec *spec, Slot **out, bool *hit) { |
| 579 | - Slot *s = &g_k.slot; |
| 580 | +static int kernel_build(unsigned kind, const OmegaGdnSpec *spec, BlackwellIRProgram *prog) { |
| 581 | + switch (kind) { |
| 582 | + case KK_DECODE: return omega_blackwell_codegen_gdn_prog(spec, prog); |
| 583 | + case KK_PREP: return omega_blackwell_codegen_gdn_prep_prog(spec, prog); |
| 584 | + case KK_SCAN: return omega_blackwell_codegen_gdn_scan_prog(spec, prog); |
| 585 | + case KK_POST: return omega_blackwell_codegen_gdn_post_prog(spec, prog); |
| 586 | + default: return -1; |
| 587 | + } |
| 588 | +} |
| 589 | +static int kernel_for(unsigned kind, const OmegaGdnSpec *spec, Slot **out, bool *hit) { |
| 590 | + Slot *s = &g_k.slot[kind]; |
| 591 | if (s->valid && memcmp(&s->spec, spec, sizeof *spec) == 0) { *out = s; *hit = true; return OMEGA_GPU_GDN_OK; } |
| 592 | 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; } |
| 593 | - slot_clear_locked(); |
| 594 | + if (s->valid) omega_gpu_session_free(&s->code); |
| 595 | + memset(s, 0, sizeof *s); |
| 596 | BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog); |
| 597 | if (!prog) { omega_gpu_session_set_error("gdn codegen: host memory"); return OMEGA_GPU_GDN_CODEGEN_FAIL; } |
| 598 | - int cg = omega_blackwell_codegen_gdn_prog(spec, prog); |
| 599 | + int cg = kernel_build(kind, spec, prog); |
| 600 | size_t need = cg == 0 ? omega_bw_program_code_bytes(prog) : 0; |
| 601 | 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; } |
| 602 | - 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; } |
| 603 | - const uint32_t insns = (uint32_t)prog->count; |
| 604 | + 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; } |
| 605 | + const uint32_t insns = (uint32_t)prog->count, expect = omega_gdn_bfly_count(prog); |
| 606 | OmegaGpuMem code; |
| 607 | if (omega_gpu_session_alloc_code(need, &code) != 0) { omega_gpu_host_free(prog); return OMEGA_GPU_GDN_CHIP_FAIL; } |
| 608 | OmegaBlackwellKernel kernel; |
| 609 | cg = omega_blackwell_kernel_encode_into(prog, code.cpu, need, &kernel); |
| 610 | + /* SHFL.BFLY placeholders become real butterflies (omega_blackwell_codegen_tc.h); the count must match */ |
| 611 | + const int np = cg == 0 ? omega_tc_patch_code(prog, code.cpu, need) : -1; |
| 612 | omega_gpu_host_free(prog); |
| 613 | 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; } |
| 614 | + 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; } |
| 615 | if (omega_gpu_session_sync_to_gpu(&code) != 0) { omega_gpu_session_free(&code); return OMEGA_GPU_GDN_CHIP_FAIL; } |
| 616 | memset(s, 0, sizeof *s); |
| 617 | s->valid = true; s->spec = *spec; s->kernel = kernel; s->code = code; s->insns = insns; |
| 618 | @@ -235,7 +256,7 @@ static int gdn_core(OmegaGpuGdn *g, uint32_t layer, const float *qkv, const floa |
| 619 | if (!dev_open_locked()) { UNLOCK(); return OMEGA_GPU_GDN_CHIP_FAIL; } |
| 620 | Slot *ks = NULL; bool hit = false; |
| 621 | g_rec = rec != NULL; |
| 622 | - rc = kernel_for(&spec, &ks, &hit); |
| 623 | + rc = kernel_for(KK_DECODE, &spec, &ks, &hit); |
| 624 | g_rec = 0; |
| 625 | if (rc != OMEGA_GPU_GDN_OK) { UNLOCK(); return rc; } |
| 626 | uint8_t *b = g->lb.cpu; |
| 627 | @@ -308,3 +329,144 @@ int omega_gpu_gdn_read_out(OmegaGpuGdn *g, float *out) { |
| 628 | omega_gpu_session_unlock(); |
| 629 | return rc; |
| 630 | } |
| 631 | + |
| 632 | +/* ---- prefill (omega#543) ---- */ |
| 633 | +/* scratch sections for T tokens (bytes, 256-aligned): qkv [T][10240] | z [T][6144] | ab [T][48][2] | out [T][6144] | |
| 634 | + * o [T][6144] | records [T rounded up to 8][48] */ |
| 635 | +enum { PF_QKV, PF_Z, PF_AB, PF_OUT, PF_O, PF_REC, PF_N }; |
| 636 | +static size_t pf_layout(uint32_t T, size_t off[PF_N]) { |
| 637 | + const uint64_t T8 = ((uint64_t)T + OMEGA_GDN_SCAN_CHUNK - 1u) / OMEGA_GDN_SCAN_CHUNK * OMEGA_GDN_SCAN_CHUNK; |
| 638 | + 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, |
| 639 | + (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 }; |
| 640 | + size_t at = 0; |
| 641 | + for (unsigned i = 0; i < PF_N; i++) { off[i] = at; at += (size_t)((sz[i] + 255u) & ~(uint64_t)255u); } |
| 642 | + return at; |
| 643 | +} |
| 644 | + |
| 645 | +int omega_gpu_gdn_prefill_reserve(OmegaGpuGdn *g, uint32_t max_tokens) { |
| 646 | + if (!g || max_tokens < OMEGA_GDN_PF_MIN_T || max_tokens > OMEGA_GPU_GDN_PF_MAX_T) return OMEGA_GPU_GDN_BAD_ARGS; |
| 647 | + size_t off[PF_N]; |
| 648 | + const size_t bytes = pf_layout(max_tokens, off); |
| 649 | + omega_gpu_session_lock(); |
| 650 | + int rc = OMEGA_GPU_GDN_OK; |
| 651 | + if (!dev_open_locked()) rc = OMEGA_GPU_GDN_CHIP_FAIL; |
| 652 | + else { |
| 653 | + omega_gpu_session_scratch_unreserve(&g->pf); |
| 654 | + if (omega_gpu_session_scratch_reserve(&g->pf, bytes) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL; |
| 655 | + } |
| 656 | + omega_gpu_session_unlock(); |
| 657 | + return rc; |
| 658 | +} |
| 659 | + |
| 660 | +/* rec non-NULL (record path, caller holds the lock): the inputs are device rows, the scratch must already hold T |
| 661 | + * tokens (omega_gpu_gdn_prefill_reserve), the three launches are appended to rec. */ |
| 662 | +static int pf_core(OmegaGpuGdn *g, uint32_t layer, uint32_t T, const float *qkv, const float *z, const float *ab, float eps_l2, |
| 663 | + float eps_norm, float in_scale, float out_scale, float *out, OmegaGpuGdnPfInfo *info, |
| 664 | + OmegaGpuCmdList *rec, uint64_t dev_qkv, uint64_t dev_z, uint64_t dev_ab, uint64_t *out_va) { |
| 665 | + uint64_t c0 = omega_gpu_session_now_ns(); |
| 666 | + if (info) memset(info, 0, sizeof *info); |
| 667 | + if (!g || layer >= g->layers || T < OMEGA_GDN_PF_MIN_T || T > OMEGA_GPU_GDN_PF_MAX_T) return OMEGA_GPU_GDN_BAD_ARGS; |
| 668 | + if (rec ? (!dev_qkv || !dev_z || !dev_ab) : (!qkv || !z || !ab || !out)) return OMEGA_GPU_GDN_BAD_ARGS; |
| 669 | + if (!isfinite(eps_l2) || !isfinite(eps_norm) || !isfinite(in_scale) || !isfinite(out_scale)) return OMEGA_GPU_GDN_BAD_ARGS; |
| 670 | + OmegaGdnSpec spec; memset(&spec, 0, sizeof spec); |
| 671 | + spec.mutant = g_k.mutant; |
| 672 | + size_t off[PF_N]; |
| 673 | + const size_t bytes = pf_layout(T, off); |
| 674 | + |
| 675 | + if (!rec) omega_gpu_session_lock(); |
| 676 | +#define UNLOCK() do { if (!rec) omega_gpu_session_unlock(); } while (0) |
| 677 | + if (!dev_open_locked()) { UNLOCK(); return OMEGA_GPU_GDN_CHIP_FAIL; } |
| 678 | + if (rec && (!g->pf.mem.cpu || g->pf.cap < bytes)) { |
| 679 | + omega_gpu_session_set_error("gdn prefill record: scratch below T tokens (call omega_gpu_gdn_prefill_reserve first)"); |
| 680 | + UNLOCK(); return OMEGA_GPU_GDN_TOO_LARGE; |
| 681 | + } |
| 682 | + if (!rec && omega_gpu_session_scratch(&g->pf, bytes) != 0) { UNLOCK(); return OMEGA_GPU_GDN_TOO_LARGE; } |
| 683 | + Slot *ks[3] = { NULL, NULL, NULL }; |
| 684 | + int rc = OMEGA_GPU_GDN_OK, hits = 0; |
| 685 | + g_rec = rec != NULL; |
| 686 | + 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; } |
| 687 | + g_rec = 0; |
| 688 | + if (rc != OMEGA_GPU_GDN_OK) { UNLOCK(); return rc; } |
| 689 | + uint8_t *b = g->pf.mem.cpu; |
| 690 | + const uint64_t va = g->pf.mem.va; |
| 691 | + float *yo = (float *)(b + off[PF_OUT]); |
| 692 | + const size_t nout = (size_t)T * OMEGA_GDN_V_DIM; |
| 693 | + if (!rec) { |
| 694 | + memcpy(b + off[PF_QKV], qkv, (size_t)T * OMEGA_GDN_CONV_DIM * 4u); |
| 695 | + memcpy(b + off[PF_Z], z, nout * 4u); |
| 696 | + memcpy(b + off[PF_AB], ab, (size_t)T * OMEGA_GDN_VALUE_HEADS * 8u); |
| 697 | + for (size_t i = 0; i < nout; i++) yo[i] = POISON_F32; |
| 698 | + if (omega_gpu_session_sync_to_gpu(&g->pf.mem) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL; |
| 699 | + } |
| 700 | + 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]; |
| 701 | + 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; |
| 702 | + 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]; |
| 703 | + const uint64_t a_out = va + off[PF_OUT]; |
| 704 | + uint32_t ap[OMEGA_GDN_PREP_ARGS_WORDS], as[OMEGA_GDN_SCAN_ARGS_WORDS], aq[OMEGA_GDN_POST_ARGS_WORDS]; |
| 705 | +#define A64(arr, i, v) do { (arr)[i] = (uint32_t)(v); (arr)[(i) + 1] = (uint32_t)((v) >> 32); } while (0) |
| 706 | + 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); |
| 707 | + ap[10] = T; memcpy(&ap[11], &eps_l2, 4); memcpy(&ap[12], &in_scale, 4); |
| 708 | + 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); |
| 709 | + as[10] = T; memcpy(&as[11], &in_scale, 4); |
| 710 | + A64(aq, 0, a_o); A64(aq, 2, a_z); A64(aq, 4, a_par); A64(aq, 6, a_out); |
| 711 | + memcpy(&aq[8], &eps_norm, 4); memcpy(&aq[9], &in_scale, 4); memcpy(&aq[10], &out_scale, 4); |
| 712 | +#undef A64 |
| 713 | + const uint32_t thr[3] = { OMEGA_GDN_PREP_THREADS, OMEGA_GDN_THREADS, OMEGA_GDN_THREADS }; |
| 714 | + const uint32_t gx[3] = { (T + OMEGA_GDN_PREP_TOKENS - 1u) / OMEGA_GDN_PREP_TOKENS, OMEGA_GDN_GRID_X, T }; |
| 715 | + const uint32_t gy[3] = { OMEGA_GDN_KEY_HEADS, OMEGA_GDN_GRID_Y, OMEGA_GDN_POST_GRID_Y }; |
| 716 | + const uint32_t sh[3] = { OMEGA_GDN_PREP_SHARED_BYTES, OMEGA_GDN_SCAN_SHARED_BYTES, 0 }; |
| 717 | + const uint32_t *args[3] = { ap, as, aq }; |
| 718 | + const uint32_t nargs[3] = { OMEGA_GDN_PREP_ARGS_WORDS, OMEGA_GDN_SCAN_ARGS_WORDS, OMEGA_GDN_POST_ARGS_WORDS }; |
| 719 | + if (info) { |
| 720 | + info->layer = layer; info->tokens = T; info->kernel_cache_hits = (uint32_t)hits; |
| 721 | + for (unsigned k = 0; k < 3; k++) { info->insns[k] = ks[k]->insns; info->gprs[k] = ks[k]->kernel.gpr_count; } |
| 722 | + } |
| 723 | + for (unsigned k = 0; k < 3 && rc == OMEGA_GPU_GDN_OK; k++) { |
| 724 | + OmegaGpuLaunch L = { |
| 725 | + .code_va = ks[k]->code.va, .gpr_count = omega_bw_gpr_declared(ks[k]->kernel.gpr_count), |
| 726 | + .threads_x = thr[k], .threads_y = 1, .grid_x = gx[k], .grid_y = gy[k], |
| 727 | + .num_elements = nout, |
| 728 | + .args = args[k], .n_args = nargs[k], .timeout_ms = MARKER_TIMEOUT_MS, |
| 729 | + .shared_bytes = sh[k], |
| 730 | + }; |
| 731 | + 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; } |
| 732 | + else if (rec) { if (omega_gpu_cmdlist_add(rec, &L) < 0) rc = OMEGA_GPU_GDN_CHIP_FAIL; } |
| 733 | + else if (omega_gpu_session_launch(&L, info ? &info->elapsed_ns[k] : NULL, NULL) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL; |
| 734 | + } |
| 735 | + if (rc == OMEGA_GPU_GDN_OK && !rec && omega_gpu_session_sync_to_cpu(&g->pf.mem) != 0) rc = OMEGA_GPU_GDN_CHIP_FAIL; |
| 736 | + if (rc == OMEGA_GPU_GDN_OK && !rec) { |
| 737 | + size_t unchanged = 0; |
| 738 | + for (size_t i = 0; i < nout; i++) if (yo[i] == POISON_F32) unchanged++; |
| 739 | + if (unchanged) { omega_gpu_session_set_error("gdn prefill: output poison survived"); rc = OMEGA_GPU_GDN_CHIP_FAIL; } |
| 740 | + else memcpy(out, yo, nout * 4u); |
| 741 | + } |
| 742 | + if (rc == OMEGA_GPU_GDN_OK && out_va) *out_va = a_out; |
| 743 | + UNLOCK(); |
| 744 | +#undef UNLOCK |
| 745 | + if (info) info->call_ns = omega_gpu_session_now_ns() - c0; |
| 746 | + return rc; |
| 747 | +} |
| 748 | + |
| 749 | +int omega_gpu_gdn_prefill(OmegaGpuGdn *g, uint32_t layer, uint32_t tokens, const float *qkv, const float *z, const float *ab, |
| 750 | + float eps_l2, float eps_norm, float in_scale, float out_scale, float *out, OmegaGpuGdnPfInfo *info) { |
| 751 | + return pf_core(g, layer, tokens, qkv, z, ab, eps_l2, eps_norm, in_scale, out_scale, out, info, NULL, 0, 0, 0, NULL); |
| 752 | +} |
| 753 | + |
| 754 | +int omega_gpu_gdn_prefill_record(OmegaGpuCmdList *cl, OmegaGpuGdn *g, uint32_t layer, uint32_t tokens, uint64_t qkv_va, |
| 755 | + uint64_t z_va, uint64_t ab_va, float eps_l2, float eps_norm, float in_scale, float out_scale, |
| 756 | + uint64_t *out_va) { |
| 757 | + if (!cl) return OMEGA_GPU_GDN_BAD_ARGS; |
| 758 | + 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); |
| 759 | +} |
| 760 | + |
| 761 | +int omega_gpu_gdn_prefill_read_out(OmegaGpuGdn *g, uint32_t tokens, float *out) { |
| 762 | + if (!g || !out || tokens < OMEGA_GDN_PF_MIN_T || tokens > OMEGA_GPU_GDN_PF_MAX_T) return OMEGA_GPU_GDN_BAD_ARGS; |
| 763 | + size_t off[PF_N]; |
| 764 | + const size_t bytes = pf_layout(tokens, off); |
| 765 | + omega_gpu_session_lock(); |
| 766 | + int rc = (!g->pf.mem.cpu || g->pf.cap < bytes) ? OMEGA_GPU_GDN_BAD_ARGS |
| 767 | + : omega_gpu_session_sync_to_cpu(&g->pf.mem) == 0 ? OMEGA_GPU_GDN_OK : OMEGA_GPU_GDN_CHIP_FAIL; |
| 768 | + 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); |
| 769 | + omega_gpu_session_unlock(); |
| 770 | + return rc; |
| 771 | +} |
| 772 | diff --git a/src/omega_gpu_gdn_api.h b/src/omega_gpu_gdn_api.h |
| 773 | index fe387699..2d820636 100644 |
| 774 | --- a/src/omega_gpu_gdn_api.h |
| 775 | +++ b/src/omega_gpu_gdn_api.h |
| 776 | @@ -73,6 +73,35 @@ int omega_gpu_gdn_read_out(OmegaGpuGdn *g, float *out); |
| 777 | const char *omega_gpu_gdn_last_error(void); |
| 778 | const char *omega_gpu_gdn_rc_name(int rc); |
| 779 | |
| 780 | +/* ---- prefill (omega#543, 2026-10-18): T >= 3 new tokens of one layer in three launches (prep, scan, post; see |
| 781 | + * omega_blackwell_codegen_gdn.h), continuing from the layer's tail and state exactly as T decode steps would, and leaving |
| 782 | + * them where the next prefill chunk or decode step picks them up. A long prompt is prefilled in chunks. |
| 783 | + * int omega_gpu_gdn_prefill(g, layer, T, qkv [T][10240], z [T][6144], ab [T][48][2], eps_l2, eps_norm, in_scale, |
| 784 | + * out_scale, out [T][6144], info); the decode step's row layouts, one row per token |
| 785 | + * int omega_gpu_gdn_prefill_reserve(g, max_tokens); sizes the prefill scratch now and pins it (the record path needs |
| 786 | + * it: a recorded launch must never see its scratch move); a later prefill of more tokens is refused |
| 787 | + * 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); |
| 788 | + * the three launches appended to a command list (caller holds the lock); out_va = the [T][6144] rows in the scratch |
| 789 | + * Scratch bytes for T tokens: T * (40960 + 3 * 24576 + 384) + ceil8(T) * 74496 (the per-token records dominate). */ |
| 790 | +#define OMEGA_GPU_GDN_PF_MAX_T 65536u |
| 791 | + |
| 792 | +typedef struct { |
| 793 | + uint32_t layer, tokens; |
| 794 | + uint32_t insns[3], gprs[3]; /* prep, scan, post */ |
| 795 | + uint64_t elapsed_ns[3]; /* marker-to-marker chip time per launch (host path) */ |
| 796 | + uint64_t call_ns; |
| 797 | + uint32_t kernel_cache_hits; /* 0..3 */ |
| 798 | +} OmegaGpuGdnPfInfo; |
| 799 | + |
| 800 | +int omega_gpu_gdn_prefill(OmegaGpuGdn *g, uint32_t layer, uint32_t tokens, const float *qkv, const float *z, const float *ab, |
| 801 | + float eps_l2, float eps_norm, float in_scale, float out_scale, float *out, OmegaGpuGdnPfInfo *info); |
| 802 | +int omega_gpu_gdn_prefill_reserve(OmegaGpuGdn *g, uint32_t max_tokens); |
| 803 | +int omega_gpu_gdn_prefill_record(struct OmegaGpuCmdList *cl, OmegaGpuGdn *g, uint32_t layer, uint32_t tokens, uint64_t qkv_va, |
| 804 | + uint64_t z_va, uint64_t ab_va, float eps_l2, float eps_norm, float in_scale, float out_scale, |
| 805 | + uint64_t *out_va); |
| 806 | +/* Test helper: the out rows the last prefill wrote (after a record + submit). */ |
| 807 | +int omega_gpu_gdn_prefill_read_out(OmegaGpuGdn *g, uint32_t tokens, float *out); |
| 808 | + |
| 809 | /* Host layout helpers (no device; the test's simulator path uses them too). */ |
| 810 | /* The parameter block (OMEGA_GDN_PAR_BYTES) from the checkpoint tensors (see set_layer). -1 on a NULL pointer. */ |
| 811 | int omega_gdn_pack_params(const float *conv_w, const float *a_log, const float *dt_bias, const float *norm_w, uint8_t *block); |
| 812 | diff --git a/tests/bw_warp_sim_k.h b/tests/bw_warp_sim_k.h |
| 813 | index 53fed2db..d42c89d3 100644 |
| 814 | --- a/tests/bw_warp_sim_k.h |
| 815 | +++ b/tests/bw_warp_sim_k.h |
| 816 | @@ -276,6 +276,8 @@ static int ws_run_warp(WsSim *s, WsWarp *w, uint32_t warp, uint32_t cx, uint32_t |
| 817 | if (mask != live) { ws_fail(s, WS_E_SHFL_SPLIT, pc, "SHFL on a split warp"); return -1; } |
| 818 | uint32_t src[WS_WARP]; |
| 819 | for (int L = 0; L < WS_WARP; L++) src[L] = ws_rd(&w->lane[L], in->src1_vreg, p); |
| 820 | + /* SHFL.BFLY (imm bit 31, OMEGA_TC_MARK_BFLY: patched in after encoding, omega_blackwell_codegen_tc.h): lane L reads L ^ lane */ |
| 821 | + 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; } |
| 822 | 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); } |
| 823 | f->pc = pc + 1; |
| 824 | break; |
| 825 | diff --git a/tests/gpu_gdn_test.c b/tests/gpu_gdn_test.c |
| 826 | index 7cbe26d7..153c7388 100644 |
| 827 | --- a/tests/gpu_gdn_test.c |
| 828 | +++ b/tests/gpu_gdn_test.c |
| 829 | @@ -12,6 +12,7 @@ |
| 830 | #include "omega_gpu_gdn_api.h" |
| 831 | #include "omega_gpu_session.h" |
| 832 | #include "omega_gpu_cmdlist.h" |
| 833 | +#include "omega_blackwell_codegen_tc.h" |
| 834 | #include "bw_warp_sim_k.h" |
| 835 | #include <inttypes.h> |
| 836 | #include <math.h> |
| 837 | @@ -190,6 +191,23 @@ static void host_only(void) { |
| 838 | free(y); inp_free(&in); dstate_free(&d); |
| 839 | } |
| 840 | layer_free(&L); |
| 841 | + /* the prefill kernels encode, and every SHFL.BFLY placeholder is patched (the count kernel_for demands) */ |
| 842 | + for (unsigned k = 0; k < 3; k++) { |
| 843 | + static BlackwellIRProgram p; |
| 844 | + OmegaGdnSpec spec = { 0 }; |
| 845 | + int rc = k == 0 ? omega_blackwell_codegen_gdn_prep_prog(&spec, &p) : k == 1 ? omega_blackwell_codegen_gdn_scan_prog(&spec, &p) |
| 846 | + : omega_blackwell_codegen_gdn_post_prog(&spec, &p); |
| 847 | + size_t need = rc == 0 ? omega_bw_program_code_bytes(&p) : 0; |
| 848 | + uint8_t *code = need ? malloc(need) : NULL; |
| 849 | + OmegaBlackwellKernel kern; |
| 850 | + int enc = code ? omega_blackwell_kernel_encode_into(&p, code, need, &kern) : -1; |
| 851 | + int np = enc == 0 ? omega_tc_patch_code(&p, code, need) : -1; |
| 852 | + const uint32_t want = omega_gdn_bfly_count(&p); |
| 853 | + 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); } } |
| 854 | + CHECK(rc == 0 && enc == 0 && np >= 0 && (uint32_t)np == want && (k == 0 || want > 0), |
| 855 | + "prefill kernel %u encodes: %zu insns, %u registers, %d of %u butterflies patched", k, p.count, p.regalloc.peak_gpr_usage, np, want); |
| 856 | + free(code); |
| 857 | + } |
| 858 | } |
| 859 | |
| 860 | /* ---- simulator ---- */ |
| 861 | @@ -281,6 +299,110 @@ static void sim_battery(void) { |
| 862 | printf("sim warp issues: %" PRIu64 "\n", g_sim_issues); |
| 863 | } |
| 864 | |
| 865 | +/* ---- prefill on the simulator (omega#543): prep, scan, post over T tokens against T oracle steps ---- */ |
| 866 | +typedef struct { uint32_t off; void *p; size_t bytes; } SimBuf; |
| 867 | +typedef struct { uint32_t off; uint32_t word; } SimWord; |
| 868 | +static int sim_launch(const BlackwellIRProgram *prog, uint32_t threads, uint32_t gx, uint32_t gy, uint32_t smem_bytes, int order, |
| 869 | + const SimBuf *b, int nb, const SimWord *w, int nw, char *msg, size_t n) { |
| 870 | + static uint8_t smem[OMEGA_GDN_SCAN_SHARED_BYTES]; |
| 871 | + WsSim s; memset(&s, 0, sizeof s); |
| 872 | + s.p = prog; s.threads = threads; s.order = order; |
| 873 | + memset(smem, 0xcd, sizeof smem); /* poison: a read before a write shows */ |
| 874 | + s.smem = smem; s.smem_bytes = smem_bytes ? smem_bytes : 4u; |
| 875 | + for (int i = 0; i < nb; i++) { |
| 876 | + const uint64_t a = (uintptr_t)b[i].p; |
| 877 | + s.cbank[b[i].off / 4] = (uint32_t)a; s.cbank[b[i].off / 4 + 1] = (uint32_t)(a >> 32); |
| 878 | + s.ranges[i].lo = a; s.ranges[i].hi = a + b[i].bytes; |
| 879 | + } |
| 880 | + s.nranges = nb; |
| 881 | + for (int i = 0; i < nw; i++) s.cbank[w[i].off / 4] = w[i].word; |
| 882 | + int err = ws_run(&s, gx, gy); |
| 883 | + if (err) snprintf(msg, n, "%.150s (pc %d)", s.msg, s.err_pc); |
| 884 | + g_sim_issues += s.warp_issues; |
| 885 | + return err; |
| 886 | +} |
| 887 | +static uint32_t fw(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } |
| 888 | +static int sim_prefill(int mutant, uint32_t T, float in_scale, float out_scale, Errs *worst, char *msg, size_t n) { |
| 889 | + static BlackwellIRProgram pp, ps, pq; |
| 890 | + OmegaGdnSpec spec = { mutant }; |
| 891 | + if (omega_blackwell_codegen_gdn_prep_prog(&spec, &pp) != 0 || omega_blackwell_codegen_gdn_scan_prog(&spec, &ps) != 0 || |
| 892 | + omega_blackwell_codegen_gdn_post_prog(&spec, &pq) != 0) { snprintf(msg, n, "codegen refused"); return -1; } |
| 893 | + Layer L; layer_make(&L, 21); |
| 894 | + DState d; dstate_init(&d, &L); |
| 895 | + const uint32_t T8 = (T + OMEGA_GDN_SCAN_CHUNK - 1u) / OMEGA_GDN_SCAN_CHUNK * OMEGA_GDN_SCAN_CHUNK; |
| 896 | + const size_t s_qkv = (size_t)T * CD * 4u, s_z = (size_t)T * VD * 4u, s_ab = (size_t)T * VH * 8u; |
| 897 | + const size_t s_rec = (size_t)T8 * VH * OMEGA_GDN_REC_BYTES, s_o = (size_t)T * VD * 4u; |
| 898 | + float *qkv = aligned_alloc(256, s_qkv), *z = aligned_alloc(256, s_z), *ab = aligned_alloc(256, s_ab); |
| 899 | + uint8_t *par = aligned_alloc(256, OMEGA_GDN_PAR_BYTES), *tail = aligned_alloc(256, OMEGA_GDN_TAIL_BYTES); |
| 900 | + uint8_t *state = aligned_alloc(256, OMEGA_GDN_STATE_BYTES), *rec = aligned_alloc(256, s_rec); |
| 901 | + float *o = aligned_alloc(256, s_o), *out = aligned_alloc(256, s_o), *out0 = malloc(s_o); |
| 902 | + uint8_t *tail0 = malloc(OMEGA_GDN_TAIL_BYTES), *state0 = malloc(OMEGA_GDN_STATE_BYTES); |
| 903 | + double *y = malloc((size_t)T * VD * 8); |
| 904 | + float *tref = malloc((size_t)NT * 4); |
| 905 | + for (uint32_t t = 0; t < T; t++) { |
| 906 | + Inp in; inp_make(&in, 1000 + t, in_scale); |
| 907 | + 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); |
| 908 | + oracle_step(&L, &d, &in, in_scale, out_scale, y + (size_t)t * VD); |
| 909 | + inp_free(&in); |
| 910 | + } |
| 911 | + omega_gdn_pack_params(L.conv_w, L.a_log, L.dtb, L.nw, par); |
| 912 | + int err = 0; |
| 913 | + for (int order = 0; order < 2 && !err; order++) { |
| 914 | + omega_gdn_pack_tail(L.tail0, (float *)tail); |
| 915 | + memcpy(state, L.state0, OMEGA_GDN_STATE_BYTES); |
| 916 | + memset(rec, 0xcd, s_rec); |
| 917 | + for (size_t i = 0; i < (size_t)T * VD; i++) { o[i] = -999.0f; out[i] = -999.0f; } |
| 918 | + 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 }, |
| 919 | + { OMEGA_GDN_PREP_ARG_TAIL, tail, OMEGA_GDN_TAIL_BYTES }, { OMEGA_GDN_PREP_ARG_REC, rec, s_rec } }; |
| 920 | + 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) } }; |
| 921 | + 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 }, |
| 922 | + { OMEGA_GDN_SCAN_ARG_STATE, state, OMEGA_GDN_STATE_BYTES }, { OMEGA_GDN_SCAN_ARG_O, o, s_o } }; |
| 923 | + const SimWord wsc[] = { { OMEGA_GDN_SCAN_ARG_T, T }, { OMEGA_GDN_SCAN_ARG_IN_SCALE, fw(in_scale) } }; |
| 924 | + 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 }, |
| 925 | + { OMEGA_GDN_POST_ARG_OUT, out, s_o } }; |
| 926 | + 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) } }; |
| 927 | + char m2[200] = ""; |
| 928 | + 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; } |
| 929 | + 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; } |
| 930 | + 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; } |
| 931 | + size_t unwritten = 0; for (size_t i = 0; i < (size_t)T * VD; i++) if (out[i] == -999.0f) unwritten++; |
| 932 | + if (unwritten) { snprintf(msg, n, "%zu output words never written", unwritten); err = -1; break; } |
| 933 | + if (order == 0) { memcpy(out0, out, s_o); memcpy(tail0, tail, OMEGA_GDN_TAIL_BYTES); memcpy(state0, state, OMEGA_GDN_STATE_BYTES); } |
| 934 | + else if (memcmp(out0, out, s_o) || memcmp(tail0, tail, OMEGA_GDN_TAIL_BYTES) || memcmp(state0, state, OMEGA_GDN_STATE_BYTES)) { |
| 935 | + snprintf(msg, n, "the warp order changed the result"); err = -1; |
| 936 | + } |
| 937 | + } |
| 938 | + if (!err) { |
| 939 | + double *ts = malloc((size_t)NT * 8); |
| 940 | + for (size_t i = 0; i < NT; i++) ts[i] = d.tail[i]; |
| 941 | + worst->out = nerr(out, y, (size_t)T * VD); |
| 942 | + worst->tail = omega_gdn_unpack_tail((const float *)tail, tref) != 0 ? INFINITY : nerr(tref, ts, NT); |
| 943 | + worst->state = nerr((const float *)state, d.state, NS); |
| 944 | + free(ts); |
| 945 | + } |
| 946 | + free(qkv); free(z); free(ab); free(par); free(tail); free(state); free(rec); free(o); free(out); free(out0); |
| 947 | + free(tail0); free(state0); free(y); free(tref); |
| 948 | + dstate_free(&d); layer_free(&L); |
| 949 | + return err; |
| 950 | +} |
| 951 | +static void sim_prefill_battery(void) { |
| 952 | + struct { int mutant; uint32_t T; float in_scale, out_scale; const char *name; } pts[] = { |
| 953 | + { 0, 37, 1.0f, 1.0f, "prefill T 37" }, { 0, 3, 1.0f, 1.0f, "prefill T 3" }, |
| 954 | + { 0, 17, 0.0009765625f, 18446744073709551616.0f, "prefill T 17, in 2^-10, out 2^64" }, |
| 955 | + { 1, 19, 1.0f, 1.0f, "prefill mutant 1 (no decay)" }, { 2, 19, 1.0f, 1.0f, "prefill mutant 2 (beta 1)" }, |
| 956 | + { 3, 19, 1.0f, 1.0f, "prefill mutant 3 (gate z)" }, { 4, 19, 1.0f, 1.0f, "prefill mutant 4 (tail kept)" }, |
| 957 | + }; |
| 958 | + for (size_t i = 0; i < sizeof pts / sizeof pts[0]; i++) { |
| 959 | + char msg[300] = ""; Errs w = { 0, 0, 0 }; |
| 960 | + double t0 = now_ms(); |
| 961 | + int err = sim_prefill(pts[i].mutant, pts[i].T, pts[i].in_scale, pts[i].out_scale, &w, msg, sizeof msg); |
| 962 | + 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); |
| 963 | + CHECK(err == 0, "sim %s ran (%s)", pts[i].name, msg); |
| 964 | + if (pts[i].mutant == 0) CHECK(errs_ok(&w), "sim %s within tolerance", pts[i].name); |
| 965 | + else CHECK(!errs_ok(&w), "sim %s caught", pts[i].name); |
| 966 | + } |
| 967 | +} |
| 968 | + |
| 969 | /* ---- chip ---- */ |
| 970 | static int chip_steps(OmegaGpuGdn *g, uint32_t layer, const Layer *L, DState *d, uint32_t steps, uint32_t seed, float in_scale, |
| 971 | float out_scale, Errs *worst, float *last_out) { |
| 972 | @@ -306,6 +428,175 @@ static int chip_steps(OmegaGpuGdn *g, uint32_t layer, const Layer *L, DState *d, |
| 973 | free(out); free(tail); free(state); free(y); free(td); |
| 974 | return rc; |
| 975 | } |
| 976 | +/* ---- prefill on the chip (omega#543) ---- */ |
| 977 | +typedef struct { float *qkv, *z, *ab; } PfIn; |
| 978 | +static void pf_in_make(PfIn *p, uint32_t T, uint32_t seed, float in_scale) { |
| 979 | + p->qkv = malloc((size_t)T * CD * 4); p->z = malloc((size_t)T * VD * 4); p->ab = malloc((size_t)T * VH * 8); |
| 980 | + for (uint32_t t = 0; t < T; t++) { |
| 981 | + Inp in; inp_make(&in, seed + t, in_scale); |
| 982 | + 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); |
| 983 | + inp_free(&in); |
| 984 | + } |
| 985 | +} |
| 986 | +static void pf_in_free(PfIn *p) { free(p->qkv); free(p->z); free(p->ab); } |
| 987 | +/* oracle over the same T rows; y [T][VD] */ |
| 988 | +static void pf_oracle(const Layer *L, DState *d, uint32_t T, uint32_t seed, float in_scale, float out_scale, double *y) { |
| 989 | + 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); } |
| 990 | +} |
| 991 | +/* prefill T tokens on `layer` from its current state, then `dec` decode steps; worst errors over every row, the tail and |
| 992 | + * the state after the prefill and after each step */ |
| 993 | +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, |
| 994 | + float out_scale, Errs *worst, float *out_copy) { |
| 995 | + DState d; dstate_init(&d, L); |
| 996 | + PfIn p; pf_in_make(&p, T, seed, in_scale); |
| 997 | + float *out = malloc((size_t)T * VD * 4), *tail = malloc((size_t)NT * 4), *state = malloc((size_t)NS * 4); |
| 998 | + double *y = malloc((size_t)T * VD * 8), *td = malloc((size_t)NT * 8); |
| 999 | + memset(worst, 0, sizeof *worst); |
| 1000 | + 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); |
| 1001 | + if (rc == 0) rc = omega_gpu_gdn_get_state(g, layer, tail, state); |
| 1002 | + if (rc == 0) { |
| 1003 | + pf_oracle(L, &d, T, seed, in_scale, out_scale, y); |
| 1004 | + for (size_t i = 0; i < NT; i++) td[i] = d.tail[i]; |
| 1005 | + worst->out = nerr(out, y, (size_t)T * VD); worst->tail = nerr(tail, td, NT); worst->state = nerr(state, d.state, NS); |
| 1006 | + if (out_copy) memcpy(out_copy, out, (size_t)T * VD * 4); |
| 1007 | + } |
| 1008 | + if (rc == 0 && dec) { |
| 1009 | + Errs e; |
| 1010 | + rc = chip_steps(g, layer, L, &d, dec, seed + T, in_scale, out_scale, &e, NULL); |
| 1011 | + if (e.out > worst->out) worst->out = e.out; |
| 1012 | + if (e.tail > worst->tail) worst->tail = e.tail; |
| 1013 | + if (e.state > worst->state) worst->state = e.state; |
| 1014 | + } |
| 1015 | + free(out); free(tail); free(state); free(y); free(td); |
| 1016 | + pf_in_free(&p); dstate_free(&d); |
| 1017 | + return rc; |
| 1018 | +} |
| 1019 | +static void chip_prefill(OmegaGpuGdn *g, Layer *L, FILE *out, int *first, uint32_t reps) { |
| 1020 | +#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) |
| 1021 | + struct { uint32_t layer, T, dec, seed; float in_scale, out_scale; const char *name; } pp[] = { |
| 1022 | + { 1, 37, 3, 2000, 1.0f, 1.0f, "prefill layer 1, T 37, then 3 decode steps" }, |
| 1023 | + { 0, 3, 1, 2100, 1.0f, 1.0f, "prefill layer 0, T 3, then 1 decode step" }, |
| 1024 | + { 0, 41, 0, 2200, 0.0009765625f, 18446744073709551616.0f, "prefill layer 0, T 41, in 2^-10 out 2^64" }, |
| 1025 | + }; |
| 1026 | + for (size_t i = 0; i < sizeof pp / sizeof pp[0]; i++) { |
| 1027 | + 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); |
| 1028 | + Errs e; |
| 1029 | + 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); |
| 1030 | + 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() : ""); |
| 1031 | + CHECK(rc == 0 && errs_ok(&e), "chip %s", pp[i].name); |
| 1032 | + PPOINT(pp[i].name, e, ""); |
| 1033 | + } |
| 1034 | + /* rerun from the same state: bit-identical rows */ |
| 1035 | + { |
| 1036 | + float *a = malloc((size_t)37 * VD * 4), *b = malloc((size_t)37 * VD * 4); |
| 1037 | + Errs e1, e2; |
| 1038 | + CHECK(omega_gpu_gdn_set_state(g, 1, L[1].tail0, L[1].state0) == 0, "set state for prefill rerun"); |
| 1039 | + int rc = chip_prefill_chain(g, 1, &L[1], 37, 0, 2000, 1.0f, 1.0f, &e1, a); |
| 1040 | + CHECK(omega_gpu_gdn_set_state(g, 1, L[1].tail0, L[1].state0) == 0, "set state for prefill rerun 2"); |
| 1041 | + int rc2 = chip_prefill_chain(g, 1, &L[1], 37, 0, 2000, 1.0f, 1.0f, &e2, b); |
| 1042 | + CHECK(rc == 0 && rc2 == 0 && memcmp(a, b, (size_t)37 * VD * 4) == 0, "prefill rerun from the same state is bit-identical"); |
| 1043 | + free(a); free(b); |
| 1044 | + } |
| 1045 | + /* mutants */ |
| 1046 | + for (int m = 1; m <= 4; m++) { |
| 1047 | + omega_gpu_gdn_test_set_mutant(m); |
| 1048 | + CHECK(omega_gpu_gdn_set_state(g, 1, L[1].tail0, L[1].state0) == 0, "set state prefill mutant %d", m); |
| 1049 | + Errs e; |
| 1050 | + int rc = chip_prefill_chain(g, 1, &L[1], 19, 0, 2300, 1.0f, 1.0f, &e, NULL); |
| 1051 | + char nm[40]; snprintf(nm, sizeof nm, "prefill mutant %d", m); |
| 1052 | + 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); |
| 1053 | + CHECK(rc == 0 && !errs_ok(&e), "chip prefill mutant %d caught", m); |
| 1054 | + PPOINT(nm, e, ""); |
| 1055 | + } |
| 1056 | + omega_gpu_gdn_test_set_mutant(0); |
| 1057 | + /* the record path: prefill 37 tokens and one decode step in one command list on layer 0 */ |
| 1058 | + { |
| 1059 | + const uint32_t T = 37; |
| 1060 | + CHECK(omega_gpu_gdn_set_state(g, 0, L[0].tail0, L[0].state0) == 0, "set state for prefill record"); |
| 1061 | + CHECK(omega_gpu_gdn_prefill_reserve(g, 64) == 0, "prefill reserve 64 tokens"); |
| 1062 | + PfIn p; pf_in_make(&p, T, 2400, 1.0f); |
| 1063 | + Inp nx; inp_make(&nx, 2400 + T, 1.0f); |
| 1064 | + DState d; dstate_init(&d, &L[0]); |
| 1065 | + double *y = malloc((size_t)T * VD * 8), *y1 = malloc((size_t)VD * 8); |
| 1066 | + pf_oracle(&L[0], &d, T, 2400, 1.0f, 1.0f, y); |
| 1067 | + oracle_step(&L[0], &d, &nx, 1.0f, 1.0f, y1); |
| 1068 | + /* warm all four kernels on layer 1 (a cache miss inside the record is refused) */ |
| 1069 | + float *tmp = malloc((size_t)T * VD * 4); |
| 1070 | + 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"); |
| 1071 | + 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"); |
| 1072 | + OmegaGpuCmdList *cl = NULL; /* create and free take the session lock themselves (omega_gpu_cmdlist.h) */ |
| 1073 | + const int cl_ok = omega_gpu_cmdlist_create(8, &cl) == 0; |
| 1074 | + omega_gpu_session_lock(); |
| 1075 | + OmegaGpuMem inb; memset(&inb, 0, sizeof inb); |
| 1076 | + const size_t s_qkv = (size_t)T * CD * 4, s_z = (size_t)T * VD * 4, s_ab = (size_t)T * VH * 8; |
| 1077 | + const size_t o_nx = (s_qkv + s_z + s_ab + 255) & ~(size_t)255; |
| 1078 | + 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; |
| 1079 | + if (ok) { |
| 1080 | + uint8_t *c = inb.cpu; |
| 1081 | + memcpy(c, p.qkv, s_qkv); memcpy(c + s_qkv, p.z, s_z); memcpy(c + s_qkv + s_z, p.ab, s_ab); |
| 1082 | + 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); |
| 1083 | + ok = omega_gpu_session_sync_to_gpu(&inb) == 0; |
| 1084 | + } |
| 1085 | + uint64_t pf_out = 0, dec_out = 0; |
| 1086 | + 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; |
| 1087 | + 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; |
| 1088 | + OmegaGpuCmdListOpts opts; memset(&opts, 0, sizeof opts); |
| 1089 | + OmegaGpuCmdListStats stc; memset(&stc, 0, sizeof stc); |
| 1090 | + if (ok) ok = omega_gpu_cmdlist_submit(cl, &opts, &stc) == 0; |
| 1091 | + if (inb.cpu) omega_gpu_session_free(&inb); |
| 1092 | + omega_gpu_session_unlock(); |
| 1093 | + if (cl) omega_gpu_cmdlist_free(cl); |
| 1094 | + CHECK(ok, "record prefill + decode step and submit (%s)", ok ? "" : omega_gpu_gdn_last_error()); |
| 1095 | + if (ok) { |
| 1096 | + 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); |
| 1097 | + double *td = malloc((size_t)NT * 8); |
| 1098 | + 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; |
| 1099 | + for (size_t i = 0; i < NT; i++) td[i] = d.tail[i]; |
| 1100 | + Errs e = { nerr(yo, y, (size_t)T * VD), nerr(t, td, NT), nerr(s, d.state, NS) }; |
| 1101 | + const double e1 = nerr(y1o, y1, VD); |
| 1102 | + if (e1 > e.out) e.out = e1; |
| 1103 | + printf("chip %-48s err out %.3g tail %.3g state %.3g\n", "record path, prefill 37 + 1 decode step", e.out, e.tail, e.state); |
| 1104 | + CHECK(ok && errs_ok(&e), "record path: prefill then a decode step match the oracle"); |
| 1105 | + PPOINT("record path, prefill 37 + 1 decode step", e, ""); |
| 1106 | + free(t); free(s); free(yo); free(y1o); free(td); |
| 1107 | + } |
| 1108 | + free(tmp); free(y); free(y1); dstate_free(&d); pf_in_free(&p); inp_free(&nx); |
| 1109 | + /* the reservation pins the scratch: a longer prefill is refused, not reallocated */ |
| 1110 | + PfIn q; pf_in_make(&q, 65, 2500, 1.0f); |
| 1111 | + float *o65 = malloc((size_t)65 * VD * 4); |
| 1112 | + 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"); |
| 1113 | + free(o65); pf_in_free(&q); |
| 1114 | + } |
| 1115 | + /* timing per launch at three prompt lengths (a fresh, unreserved scratch per length) */ |
| 1116 | + const uint32_t Ts[3] = { 512, 2048, 8192 }; |
| 1117 | + for (unsigned k = 0; k < 3; k++) { |
| 1118 | + const uint32_t T = Ts[k], nrep = reps < 5 ? reps : 5; |
| 1119 | + CHECK(omega_gpu_gdn_prefill_reserve(g, T) == 0, "reserve %u tokens", T); |
| 1120 | + PfIn p; pf_in_make(&p, T, 3000, 1.0f); |
| 1121 | + float *o = malloc((size_t)T * VD * 4); |
| 1122 | + uint64_t e[3][5], c[5]; |
| 1123 | + uint32_t done = 0; |
| 1124 | + OmegaGpuGdnPfInfo info; |
| 1125 | + for (uint32_t r = 0; r < nrep; r++) { |
| 1126 | + 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; |
| 1127 | + for (unsigned j = 0; j < 3; j++) e[j][done] = info.elapsed_ns[j]; |
| 1128 | + c[done] = info.call_ns; done++; |
| 1129 | + } |
| 1130 | + CHECK(done == nrep, "prefill T %u timing reps ran (%u of %u): %s", T, done, nrep, done == nrep ? "" : omega_gpu_gdn_last_error()); |
| 1131 | + if (done) { |
| 1132 | + for (unsigned j = 0; j < 3; j++) qsort(e[j], done, 8, cmp_u64); |
| 1133 | + qsort(c, done, 8, cmp_u64); |
| 1134 | + 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]; |
| 1135 | + 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", |
| 1136 | + 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]); |
| 1137 | + 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]}", |
| 1138 | + *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]); |
| 1139 | + *first = 0; |
| 1140 | + } |
| 1141 | + free(o); pf_in_free(&p); |
| 1142 | + } |
| 1143 | +#undef PPOINT |
| 1144 | +} |
| 1145 | static int chip_run(const char *out_path, uint32_t reps) { |
| 1146 | FILE *out = out_path ? fopen(out_path, "w") : NULL; |
| 1147 | if (out_path && !out) { printf("cannot write %s\n", out_path); return 1; } |
| 1148 | @@ -382,10 +673,11 @@ static int chip_run(const char *out_path, uint32_t reps) { |
| 1149 | /* warm the kernel cache outside the record (a miss inside is refused) */ |
| 1150 | float *tmp = malloc((size_t)VD * 4); |
| 1151 | 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"); |
| 1152 | + OmegaGpuCmdList *cl = NULL; /* create and free take the session lock themselves (omega_gpu_cmdlist.h) */ |
| 1153 | + const int cl_ok = omega_gpu_cmdlist_create(4, &cl) == 0; |
| 1154 | omega_gpu_session_lock(); |
| 1155 | OmegaGpuMem inb[2]; memset(inb, 0, sizeof inb); |
| 1156 | - OmegaGpuCmdList *cl = NULL; |
| 1157 | - int ok = omega_gpu_session_open() != NULL; |
| 1158 | + int ok = cl_ok && omega_gpu_session_open() != NULL; |
| 1159 | const size_t ib = (size_t)CD * 4 + (size_t)VD * 4 + VH * 8; |
| 1160 | for (int i = 0; ok && i < 2; i++) { |
| 1161 | ok = omega_gpu_session_alloc(ib, &inb[i]) == 0; |
| 1162 | @@ -396,15 +688,14 @@ static int chip_run(const char *out_path, uint32_t reps) { |
| 1163 | } |
| 1164 | } |
| 1165 | uint64_t out_va = 0; |
| 1166 | - if (ok) ok = omega_gpu_cmdlist_create(4, &cl) == 0; |
| 1167 | for (int i = 0; ok && i < 2; i++) |
| 1168 | 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; |
| 1169 | OmegaGpuCmdListOpts opts; memset(&opts, 0, sizeof opts); |
| 1170 | OmegaGpuCmdListStats stc; memset(&stc, 0, sizeof stc); |
| 1171 | if (ok) ok = omega_gpu_cmdlist_submit(cl, &opts, &stc) == 0; |
| 1172 | - if (cl) omega_gpu_cmdlist_free(cl); |
| 1173 | for (int i = 0; i < 2; i++) if (inb[i].cpu) omega_gpu_session_free(&inb[i]); |
| 1174 | omega_gpu_session_unlock(); |
| 1175 | + if (cl) omega_gpu_cmdlist_free(cl); |
| 1176 | CHECK(ok, "record two steps and submit (%s)", ok ? "" : omega_gpu_gdn_last_error()); |
| 1177 | if (ok) { |
| 1178 | float *t = malloc((size_t)NT * 4), *s = malloc((size_t)NS * 4), *yo = malloc((size_t)VD * 4); |
| 1179 | @@ -419,6 +710,7 @@ static int chip_run(const char *out_path, uint32_t reps) { |
| 1180 | } |
| 1181 | free(tmp); free(y); dstate_free(&d); inp_free(&in[0]); inp_free(&in[1]); |
| 1182 | } |
| 1183 | + chip_prefill(g, L, out, &first, reps); |
| 1184 | /* timing */ |
| 1185 | { |
| 1186 | Inp in; inp_make(&in, 950, 1.0f); |
| 1187 | @@ -450,6 +742,7 @@ static int chip_run(const char *out_path, uint32_t reps) { |
| 1188 | } |
| 1189 | |
| 1190 | int main(int argc, char **argv) { |
| 1191 | + setvbuf(stdout, NULL, _IOLBF, 0); /* a stuck chip run still leaves every finished line in its log */ |
| 1192 | int host = 0, sim = 0; const char *out_path = NULL; uint32_t reps = 0, spin = 2000; |
| 1193 | for (int i = 1; i < argc; i++) { |
| 1194 | if (strcmp(argv[i], "--host-only") == 0) host = 1; |
| 1195 | @@ -460,7 +753,7 @@ int main(int argc, char **argv) { |
| 1196 | else { printf("unknown argument %s\n", argv[i]); return 2; } |
| 1197 | } |
| 1198 | if (host) host_only(); |
| 1199 | - if (sim) sim_battery(); |
| 1200 | + if (sim) { sim_battery(); sim_prefill_battery(); } |
| 1201 | if (!host && !sim) { |
| 1202 | (void)omega_gpu_session_set_spin_us(spin); |
| 1203 | chip_run(out_path, reps ? reps : 50); |