Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/CUDApp/blob/main/src/omega_blackwell_codegen_attn.c # CUDApp CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / src / omega_blackwell_codegen_attn.c` 563 lines · 34916 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* 3 * omega_blackwell_codegen_attn.c: the one-launch decode attention kernel (see the header). Emitter rules follow 4 * omega_blackwell_codegen_moe.c (control words, read barrier on every load, loop live ranges). 5 */ 6 #include "omega_blackwell_codegen_attn.h" 7 #include 8 #include "omega_gpu_om_ir.h" 9 #include "cudapp_native.h" 10 11 #if defined(CUDAPP_MIGRATION_PENDING_C_CODEGEN_ATTN) 12 13 #define CW(stall, wbar, rbar, wait) \ 14 (((uint32_t)(stall) << 9) | ((uint32_t)(wbar) << 14) | ((uint32_t)(rbar) << 17) | ((uint32_t)(wait) << 20)) 15 #define CW_YIELD 0x2000u 16 #define SB_NONE 7u 17 #define SB_GPR 0u 18 #define SB_RD 1u 19 #define SB_UGPR 2u 20 #define NOREG (-1) 21 #define MAX_LOOPS 4 22 23 enum { K_FIXED, K_PRED, K_VAR, K_VAR_MORE, K_UVAR, K_STORE, K_SYNC, K_EXIT, K_BRA, K_BSSY, K_BSYNC }; 24 25 typedef struct { 26 BlackwellIRProgram *p; 27 uint32_t pending; 28 int err; 29 int open_start[MAX_LOOPS]; 30 int depth; 31 } Em; 32 33 static void emit(Em *e, BlackwellIRInsn in, int kind) { 34 uint32_t wait = e->pending; 35 switch (kind) { 36 case K_FIXED: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; 37 case K_PRED: in.control = CW(14, SB_NONE, SB_NONE, wait); e->pending = 0; break; 38 case K_VAR: in.control = CW(4, SB_GPR, SB_RD, wait) | CW_YIELD; e->pending = (1u << SB_GPR) | (1u << SB_RD); break; 39 case K_VAR_MORE: in.control = CW(4, SB_GPR, SB_RD, (e->pending & (1u << SB_RD))) | CW_YIELD; e->pending |= (1u << SB_GPR) | (1u << SB_RD); break; 40 case K_UVAR: in.control = CW(4, SB_UGPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_UGPR; break; 41 case K_STORE: in.control = CW(4, SB_NONE, SB_RD, wait) | CW_YIELD; e->pending = 1u << SB_RD; break; 42 case K_SYNC: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; 43 case K_EXIT: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; 44 case K_BRA: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; 45 case K_BSSY: in.control = CW(1, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; 46 case K_BSYNC: in.control = CW(5, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; 47 default: e->err = -1; return; 48 } 49 if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; 50 } 51 52 static BlackwellIRInsn I0(BlackwellIROpcode op) { 53 BlackwellIRInsn i; memset(&i, 0, sizeof i); 54 i.op = op; i.dst_vreg = NOREG; i.src1_vreg = NOREG; i.src2_vreg = NOREG; i.src3_vreg = NOREG; i.ureg = NOREG; 55 return i; 56 } 57 static int V(Em *e) { int v = omega_bw_ir_alloc_vreg(e->p); if (v < 0) e->err = -1; return v; } 58 static int V64(Em *e) { int v = omega_bw_ir_alloc_vreg64(e->p); if (v < 0) e->err = -1; return v; } 59 static int V128(Em *e) { int v = omega_bw_ir_alloc_vreg128(e->p); if (v < 0) e->err = -1; return v; } 60 static int UV64(Em *e) { int v = omega_bw_ir_alloc_uvreg64(e->p); if (v < 0) e->err = -1; return v; } 61 static int here(const Em *e) { return (int)e->p->count; } 62 63 /* operand with subregister */ 64 typedef struct { int v; unsigned s; } Op; 65 static Op R(int v) { Op o = { v, 0 }; return o; } 66 static Op S(int v, unsigned s) { Op o = { v, s }; return o; } 67 68 static void ldcu64(Em *e, int ud, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDCU64); i.dst_vreg = ud; i.imm = off; i.is_uniform = true; emit(e, i, K_UVAR); } 69 static void ldc64(Em *e, int d, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC64); i.dst_vreg = d; i.imm = off; emit(e, i, K_VAR); } 70 static void ldc32(Em *e, int d, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC); i.dst_vreg = d; i.imm = off; emit(e, i, K_VAR); } 71 static void s2r(Em *e, int d, uint32_t sr) { BlackwellIRInsn i = I0(BW_IR_S2R); i.dst_vreg = d; i.imm = sr; emit(e, i, K_VAR); } 72 static void movi(Em *e, int d, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_MOV_IMM); i.dst_vreg = d; i.imm = imm; emit(e, i, K_FIXED); } 73 static void movrz(Em *e, Op d) { BlackwellIRInsn i = I0(BW_IR_MOV_RZ); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; emit(e, i, K_FIXED); } 74 /* d = a * imm + c */ 75 static void imadi(Em *e, Op d, Op a, uint32_t imm, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = a.v; i.src1_subreg = (uint8_t)a.s; i.imm = imm; i.src3_vreg = c; emit(e, i, K_FIXED); } 76 /* d = a * b + c (registers) */ 77 static void imadr(Em *e, int d, int a, int b, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src3_vreg = c; emit(e, i, K_FIXED); } 78 /* d64 = a * imm + c64 */ 79 static void imadw(Em *e, int d64, int a, uint32_t imm, int c64) { BlackwellIRInsn i = I0(BW_IR_IMAD_WIDE); i.dst_vreg = d64; i.src1_vreg = a; i.imm = imm; i.src3_vreg = c64; emit(e, i, K_FIXED); } 80 static void mov(Em *e, Op d, Op a) { imadi(e, d, a, 1u, NOREG); } 81 static void shrs(Em *e, int d, Op a, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_SHF_R); i.dst_vreg = d; i.src1_vreg = a.v; i.src1_subreg = (uint8_t)a.s; i.imm = imm; emit(e, i, K_FIXED); } 82 static void shr(Em *e, int d, int a, uint32_t imm) { shrs(e, d, R(a), imm); } 83 static void andi(Em *e, Op d, Op a, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_LOP3_AND); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = a.v; i.src1_subreg = (uint8_t)a.s; i.imm = imm; emit(e, i, K_FIXED); } 84 static void xorr(Em *e, Op d, Op a, Op b) { BlackwellIRInsn i = I0(BW_IR_LOP3_XOR); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = a.v; i.src1_subreg = (uint8_t)a.s; i.src2_vreg = b.v; i.src2_subreg = (uint8_t)b.s; emit(e, i, K_FIXED); } 85 static void iadd(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_IADD3); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } 86 static void ldg_wide(Em *e, BlackwellIROpcode op, int d, int addr64, uint32_t imm, int udesc, int first) { 87 BlackwellIRInsn i = I0(op); i.dst_vreg = d; i.src1_vreg = addr64; i.imm = imm; i.ureg = udesc; emit(e, i, first ? K_VAR : K_VAR_MORE); 88 } 89 static void ldg32(Em *e, BlackwellIROpcode op, int d, int addr64, int udesc, int first) { BlackwellIRInsn i = I0(op); i.dst_vreg = d; i.src1_vreg = addr64; i.ureg = udesc; emit(e, i, first ? K_VAR : K_VAR_MORE); } 90 static void stg(Em *e, BlackwellIROpcode op, int addr64, Op val, int udesc) { BlackwellIRInsn i = I0(op); i.src1_vreg = addr64; i.src2_vreg = val.v; i.src2_subreg = (uint8_t)val.s; i.ureg = udesc; emit(e, i, K_STORE); } 91 static void atom_add(Em *e, int d, int addr64, int val, int udesc) { BlackwellIRInsn i = I0(BW_IR_ATOMG_ADD_STRONG_SYS); i.dst_vreg = d; i.src1_vreg = addr64; i.src2_vreg = val; i.ureg = udesc; emit(e, i, K_VAR); } 92 static void membar(Em *e) { emit(e, I0(BW_IR_MEMBAR_ALL_SYS), K_FIXED); } 93 static void lds128(Em *e, int d128, int addr, uint32_t imm, int first) { BlackwellIRInsn i = I0(BW_IR_LDS128); i.dst_vreg = d128; i.src1_vreg = addr; i.imm = imm; emit(e, i, first ? K_VAR : K_VAR_MORE); } 94 static void lds32(Em *e, Op d, int addr, int first) { BlackwellIRInsn i = I0(BW_IR_LDS32); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = addr; emit(e, i, first ? K_VAR : K_VAR_MORE); } 95 static void sts32(Em *e, int addr, Op val) { BlackwellIRInsn i = I0(BW_IR_STS32); i.src1_vreg = addr; i.src2_vreg = val.v; i.src2_subreg = (uint8_t)val.s; emit(e, i, K_STORE); } 96 static void bar_sync(Em *e) { emit(e, I0(BW_IR_BAR_SYNC), K_SYNC); } 97 static void fop(Em *e, BlackwellIROpcode op, Op d, Op a, Op b, Op c) { 98 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; 99 i.src2_vreg = b.v; i.src2_subreg = (uint8_t)b.s; i.src3_vreg = c.v; i.src3_subreg = (uint8_t)c.s; emit(e, i, K_FIXED); 100 } 101 static void fadd(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FADD, d, a, b, R(NOREG)); } 102 static void fsub(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FSUB, d, a, b, R(NOREG)); } 103 static void fmul(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FMUL, d, a, b, R(NOREG)); } 104 static void ffma(Em *e, Op d, Op a, Op b, Op c) { fop(e, BW_IR_FFMA, d, a, b, c); } 105 static void fmaxx(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FMNMX_MAX, d, a, b, R(NOREG)); } 106 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); } 107 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); } 108 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); } 109 static void bra(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); } 110 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; } 111 static void patch_bra(Em *e, int at, int target) { if (!e->err) e->p->insns[at].imm = (uint32_t)(target - at); } 112 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; } 113 static void bsync(Em *e) { BlackwellIRInsn i = I0(BW_IR_BSYNC); i.bar_reg = 0; emit(e, i, K_BSYNC); } 114 static void exit_k(Em *e) { emit(e, I0(BW_IR_EXIT), K_EXIT); } 115 static void tail(Em *e) { 116 exit_k(e); 117 BlackwellIRInsn self = I0(BW_IR_BRA); 118 self.control = 0x000fc000u; 119 if (omega_bw_ir_append(e->p, &self) < 0) e->err = -1; 120 } 121 static void loop_begin(Em *e) { if (e->depth >= MAX_LOOPS) { e->err = -1; return; } e->open_start[e->depth++] = here(e); } 122 static void loop_end(Em *e) { 123 if (e->depth <= 0) { e->err = -1; return; } 124 int start = e->open_start[--e->depth], end = here(e) - 1; 125 OmegaRegAlloc *ra = &e->p->regalloc; 126 for (int v = 0; v < ra->num_vregs; v++) { 127 OmegaLiveInterval *iv = &ra->intervals[v]; 128 if (iv->first_def >= 0 && iv->first_def < start && iv->last_use >= start && iv->last_use < end) iv->last_use = end; 129 } 130 } 131 132 /* A region only some lanes of a warp execute: BSSY ... @P0 BRA join ... BSYNC. P0 true = skip. */ 133 typedef struct { int bssy, bra; } Region; 134 static Region region_skip_if_p0(Em *e) { Region r; r.bssy = bssy_fwd(e); r.bra = bra_if_p0_fwd(e); return r; } 135 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); } 136 137 /* bf16 round-to-nearest-even of f32 x into the low 16 bits of d: r = x + 0x7fff + ((x >> 16) & 1); d = r >> 16 */ 138 static void bf16_of(Em *e, int d, Op x, int c7fff, int t) { 139 shrs(e, t, x, 16); 140 andi(e, R(t), R(t), 1u); 141 iadd(e, t, t, c7fff); 142 fop(e, BW_IR_IADD3, R(d), x, R(t), R(NOREG)); /* IADD3 d = x + t */ 143 shr(e, d, d, 16); 144 } 145 146 static const uint32_t NEG_BIG = 0xF149F2CAu; /* -1e30f */ 147 148 int omega_blackwell_codegen_decode_attn_prog(const OmegaDecodeAttnSpec *spec, BlackwellIRProgram *prog) { 149 if (!spec || !prog || spec->depth < 1 || spec->depth > OMEGA_DATTN_MAX_DEPTH || spec->mutant < 0 || spec->mutant > 4) return -1; 150 omega_bw_ir_init(prog); 151 Em em; memset(&em, 0, sizeof em); em.p = prog; Em *e = &em; 152 const uint32_t D = spec->depth; 153 const int MUT = spec->mutant; 154 155 /* ---- arguments and identities ---- */ 156 int udesc = UV64(e); 157 int pqkv = V64(e), pkc = V64(e), pvc = V64(e), prope = V64(e), pqn = V64(e), pkn = V64(e); 158 int pos = V(e), per = V(e), splits = V(e), kvs = V(e), eps = V(e), qks = V(e); 159 /* pout, ppart, pcnt, oscale are loaded from the constant bank in the epilogue (register pressure: the chip allocator has 128) */ 160 int tid = V(e), cx = V(e), cy = V(e), warp = V(e), lane = V(e), one = V(e), zero = V(e), c7fff = V(e), c16 = V(e); 161 if (e->err) return -1; 162 ldcu64(e, udesc, 0x358); 163 ldc64(e, pqkv, OMEGA_DATTN_ARG_QKV); ldc64(e, pkc, OMEGA_DATTN_ARG_KC); ldc64(e, pvc, OMEGA_DATTN_ARG_VC); 164 ldc64(e, prope, OMEGA_DATTN_ARG_ROPE); ldc64(e, pqn, OMEGA_DATTN_ARG_QN); ldc64(e, pkn, OMEGA_DATTN_ARG_KN); 165 ldc32(e, pos, OMEGA_DATTN_ARG_POS); ldc32(e, per, OMEGA_DATTN_ARG_PER_SPLIT); ldc32(e, splits, OMEGA_DATTN_ARG_SPLITS); 166 ldc32(e, kvs, OMEGA_DATTN_ARG_KV_STRIDE); ldc32(e, eps, OMEGA_DATTN_ARG_EPS); 167 ldc32(e, qks, OMEGA_DATTN_ARG_QK_SCALE); 168 s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y); 169 movi(e, one, 1); movrz(e, R(zero)); movi(e, c7fff, 0x7fffu); movi(e, c16, 16); 170 shr(e, warp, tid, 5); 171 andi(e, R(lane), R(tid), 31u); 172 173 /* range of this CTA: base = cx * per; end = min(base + per, pos + 1) */ 174 int base = V(e), bend = V(e), ctx = V(e), t1 = V(e), t2 = V(e), end = V(e), endm1 = V(e), negendm1 = V(e), splitsm1 = V(e), cyk = V(e); 175 imadr(e, base, cx, per, NOREG); 176 imadi(e, R(bend), R(base), 1u, per); 177 imadi(e, R(ctx), R(pos), 1u, one); 178 imadi(e, R(t1), R(ctx), 0xffffffffu, bend); /* t1 = bend - ctx */ 179 imadi(e, R(t2), R(t1), 0xffffffffu, NOREG); /* t2 = ctx - bend */ 180 shr(e, t2, t2, 31); /* 1 when bend > ctx */ 181 imadr(e, t2, t2, t1, NOREG); /* t2 = over * (bend - ctx) */ 182 imadi(e, R(end), R(t2), 0xffffffffu, bend); /* end = bend - t2 */ 183 imadi(e, R(endm1), R(one), 0xffffffffu, end); 184 imadi(e, R(negendm1), R(endm1), 0xffffffffu, NOREG); 185 imadi(e, R(splitsm1), R(one), 0xffffffffu, splits); 186 imadr(e, cyk, cy, kvs, NOREG); /* byte offset of this KV head's plane */ 187 188 /* ---- prologue: q' for the 8 heads of the group (warp = head) ---- */ 189 int q4 = V128(e), w4 = V128(e), pt4 = V128(e), cos4 = V128(e), sin4 = V128(e), k4 = V128(e), v4 = V128(e); 190 int sq = V(e), ts = V(e), rs = V(e), a0 = V(e), a1 = V(e), a2 = V(e), a3 = V(e), ad64 = V64(e), rope64 = V64(e), sgn = V(e), pl = V(e); 191 if (e->err) return -1; 192 imadi(e, R(t1), R(cy), 8u, warp); /* q head index */ 193 imadi(e, R(t1), R(t1), 512u, NOREG); 194 imadi(e, R(t1), R(lane), 16u, t1); 195 imadw(e, ad64, t1, 1u, pqkv); 196 ldg_wide(e, BW_IR_LDG_E_128, q4, ad64, 0, udesc, 1); 197 imadw(e, ad64, lane, 16u, pqn); 198 ldg_wide(e, BW_IR_LDG_E_128, w4, ad64, 0, udesc, 0); 199 /* rope row for this lane: cos/sin index = lane & 15 (dims 4i..4i+3 for lanes < 16, dims 64 + 4(i-16) above) */ 200 andi(e, R(t2), R(lane), 15u); 201 imadi(e, R(t2), R(t2), 16u, NOREG); 202 imadi(e, R(t2), R(pos), OMEGA_DATTN_ROPE_ROW_BYTES, t2); 203 imadw(e, rope64, t2, 1u, prope); 204 ldg_wide(e, BW_IR_LDG_E_128, cos4, rope64, 0, udesc, 0); 205 ldg_wide(e, BW_IR_LDG_E_128, sin4, rope64, 256, udesc, 0); 206 /* sign of the sin term: lanes < 16 take q[d] * cos - q[d+64] * sin, lanes >= 16 take q[d] * cos + q[d-64] * sin */ 207 imadi(e, R(sgn), R(one), 0xfffffff0u, lane); /* lane - 16 */ 208 shr(e, sgn, sgn, 31); 209 imadi(e, R(sgn), R(sgn), 0x80000000u, NOREG); 210 for (unsigned i = 0; i < 4; i++) xorr(e, S(sin4, i), S(sin4, i), R(sgn)); 211 xorr(e, R(pl), R(lane), R(c16)); /* partner lane */ 212 213 /* RMSNorm of the quad's head: sum of squares over the warp, broadcast through shared memory */ 214 fmul(e, R(sq), S(q4, 0), S(q4, 0)); 215 for (unsigned i = 1; i < 4; i++) ffma(e, R(sq), S(q4, i), S(q4, i), R(sq)); 216 for (uint32_t o = 16; o >= 1; o /= 2) { shfl_down(e, ts, sq, o); fadd(e, R(sq), R(sq), R(ts)); } 217 imadi(e, R(a0), R(warp), 4u, NOREG); imadi(e, R(a0), R(one), OMEGA_DATTN_SM_L, a0); 218 isetp_ge_u32(e, lane, one); 219 { Region r = region_skip_if_p0(e); sts32(e, a0, R(sq)); region_end(e, r); } 220 bar_sync(e); 221 lds32(e, R(sq), a0, 1); 222 movi(e, ts, 0x3c000000u); /* 1/128 */ 223 ffma(e, R(rs), R(sq), R(ts), R(eps)); 224 mufu(e, BW_IR_MUFU_RSQ, R(rs), R(rs)); 225 for (unsigned i = 0; i < 4; i++) { fmul(e, S(q4, i), S(q4, i), R(rs)); fmul(e, S(q4, i), S(q4, i), S(w4, i)); } 226 /* stage, read the partner half, rotate */ 227 imadi(e, R(a0), R(warp), 512u, NOREG); imadi(e, R(a0), R(lane), 16u, a0); /* SM_Q + warp*512 + lane*16 (SM_Q = 0) */ 228 imadi(e, R(a1), R(one), 4u, a0); imadi(e, R(a2), R(one), 8u, a0); imadi(e, R(a3), R(one), 12u, a0); 229 sts32(e, a0, S(q4, 0)); sts32(e, a1, S(q4, 1)); sts32(e, a2, S(q4, 2)); sts32(e, a3, S(q4, 3)); 230 bar_sync(e); 231 imadi(e, R(t2), R(warp), 512u, NOREG); imadi(e, R(t2), R(pl), 16u, t2); 232 lds128(e, pt4, t2, 0, 1); 233 if (MUT != 1) for (unsigned i = 0; i < 4; i++) { fmul(e, R(ts), S(q4, i), S(cos4, i)); ffma(e, S(q4, i), S(pt4, i), S(sin4, i), R(ts)); } 234 for (unsigned i = 0; i < 4; i++) fmul(e, S(q4, i), S(q4, i), R(qks)); 235 bar_sync(e); /* every partner read done before q' overwrites the staging */ 236 sts32(e, a0, S(q4, 0)); sts32(e, a1, S(q4, 1)); sts32(e, a2, S(q4, 2)); sts32(e, a3, S(q4, 3)); 237 238 /* ---- the new token's K (norm + rope) and V, bf16 into the cache by the CTA that owns `pos` ---- */ 239 imadi(e, R(t1), R(cy), 512u, NOREG); imadi(e, R(t1), R(lane), 16u, t1); 240 imadi(e, R(t1), R(one), 32u * 128u * 4u, t1); /* k block of qkv */ 241 imadw(e, ad64, t1, 1u, pqkv); 242 ldg_wide(e, BW_IR_LDG_E_128, k4, ad64, 0, udesc, 1); 243 ldg_wide(e, BW_IR_LDG_E_128, v4, ad64, 4u * 128u * 4u, udesc, 0); /* v block follows the 4 k heads */ 244 imadw(e, ad64, lane, 16u, pkn); 245 ldg_wide(e, BW_IR_LDG_E_128, w4, ad64, 0, udesc, 0); 246 fmul(e, R(sq), S(k4, 0), S(k4, 0)); 247 for (unsigned i = 1; i < 4; i++) ffma(e, R(sq), S(k4, i), S(k4, i), R(sq)); 248 for (uint32_t o = 16; o >= 1; o /= 2) { shfl_down(e, ts, sq, o); fadd(e, R(sq), R(sq), R(ts)); } 249 imadi(e, R(a1), R(warp), 4u, NOREG); imadi(e, R(a1), R(one), OMEGA_DATTN_SM_L, a1); 250 isetp_ge_u32(e, lane, one); 251 { Region r = region_skip_if_p0(e); sts32(e, a1, R(sq)); region_end(e, r); } 252 bar_sync(e); 253 lds32(e, R(sq), a1, 1); 254 movi(e, ts, 0x3c000000u); 255 ffma(e, R(rs), R(sq), R(ts), R(eps)); 256 mufu(e, BW_IR_MUFU_RSQ, R(rs), R(rs)); 257 for (unsigned i = 0; i < 4; i++) { fmul(e, S(k4, i), S(k4, i), R(rs)); fmul(e, S(k4, i), S(k4, i), S(w4, i)); } 258 imadi(e, R(a1), R(lane), 16u, NOREG); imadi(e, R(a1), R(one), OMEGA_DATTN_SM_K, a1); 259 imadi(e, R(a2), R(one), 4u, a1); imadi(e, R(a3), R(one), 8u, a1); imadi(e, R(t2), R(one), 12u, a1); 260 sts32(e, a1, S(k4, 0)); sts32(e, a2, S(k4, 1)); sts32(e, a3, S(k4, 2)); sts32(e, t2, S(k4, 3)); /* every warp writes the same words */ 261 bar_sync(e); 262 imadi(e, R(t2), R(pl), 16u, NOREG); imadi(e, R(t2), R(one), OMEGA_DATTN_SM_K, t2); 263 lds128(e, pt4, t2, 0, 1); 264 if (MUT != 1) for (unsigned i = 0; i < 4; i++) { fmul(e, R(ts), S(k4, i), S(cos4, i)); ffma(e, S(k4, i), S(pt4, i), S(sin4, i), R(ts)); } 265 /* bf16 words: k4 -> (a2, a3), v4 -> (sq, ts) */ 266 { 267 int h0 = V(e), h1 = V(e), tb = V(e); 268 bf16_of(e, h0, S(k4, 0), c7fff, tb); bf16_of(e, h1, S(k4, 1), c7fff, tb); imadi(e, R(a2), R(h1), 65536u, h0); 269 bf16_of(e, h0, S(k4, 2), c7fff, tb); bf16_of(e, h1, S(k4, 3), c7fff, tb); imadi(e, R(a3), R(h1), 65536u, h0); 270 bf16_of(e, h0, S(v4, 0), c7fff, tb); bf16_of(e, h1, S(v4, 1), c7fff, tb); imadi(e, R(sq), R(h1), 65536u, h0); 271 bf16_of(e, h0, S(v4, 2), c7fff, tb); bf16_of(e, h1, S(v4, 3), c7fff, tb); imadi(e, R(ts), R(h1), 65536u, h0); 272 } 273 /* cache row address of position pos for this lane */ 274 imadi(e, R(t1), R(pos), OMEGA_DATTN_ROW_BYTES, cyk); imadi(e, R(t1), R(lane), 8u, t1); /* 4 bf16 = 8 bytes per lane */ 275 /* key = (splits - 1 - cx) + warp: zero only for warp 0 of the last split */ 276 imadi(e, R(t2), R(cx), 0xffffffffu, splitsm1); 277 if (MUT != 3) { 278 int ka = V64(e), kb = V64(e), key = V(e); 279 imadi(e, R(key), R(t2), 1u, warp); 280 isetp_ge_u32(e, key, one); 281 int b1 = bra_if_p0_fwd(e); 282 imadw(e, ka, t1, 1u, pkc); imadw(e, kb, one, 4u, ka); 283 stg(e, BW_IR_STG_E, ka, R(a2), udesc); stg(e, BW_IR_STG_E, kb, R(a3), udesc); 284 patch_bra(e, b1, here(e)); 285 xorr(e, R(key), R(warp), R(one)); imadi(e, R(key), R(t2), 1u, key); 286 isetp_ge_u32(e, key, one); 287 int b2 = bra_if_p0_fwd(e); 288 imadw(e, ka, t1, 1u, pvc); imadw(e, kb, one, 4u, ka); 289 stg(e, BW_IR_STG_E, ka, R(sq), udesc); stg(e, BW_IR_STG_E, kb, R(ts), udesc); 290 patch_bra(e, b2, here(e)); 291 } 292 membar(e); 293 bar_sync(e); 294 295 /* ---- running state ---- */ 296 int acc[8], m[8], l[8], sc[8], dl[8], wd[8], mn = V(e); 297 for (unsigned h = 0; h < 8; h++) { acc[h] = V128(e); m[h] = V(e); l[h] = V(e); sc[h] = V(e); dl[h] = V(e); wd[h] = V(e); } 298 int kq[OMEGA_DATTN_MAX_DEPTH], vq[OMEGA_DATTN_MAX_DEPTH]; 299 for (uint32_t d = 0; d < D; d++) { kq[d] = V128(e); vq[d] = V64(e); } 300 int qa4 = V128(e), qb4 = V128(e); 301 int cb = V(e), tpos = V(e), over = V(e), krow = V64(e), kpf = V64(e), qoff = V(e), j = V(e), sa = V(e), smS = V(e); 302 int n = V(e), vrow = V64(e), vpf = V64(e), pa = V(e), i = V(e), dsum = V(e), lane8 = V(e), negwarp = V(e), ccnt = V(e); 303 if (e->err) return -1; 304 for (unsigned h = 0; h < 8; h++) { for (unsigned q = 0; q < 4; q++) movrz(e, S(acc[h], q)); movi(e, m[h], NEG_BIG); movrz(e, R(l[h])); } 305 movi(e, smS, OMEGA_DATTN_SM_S); 306 imadi(e, R(lane8), R(lane), 8u, NOREG); 307 if (MUT == 2) imadi(e, R(lane8), R(one), OMEGA_DATTN_ROW_BYTES, lane8); 308 imadi(e, R(negwarp), R(warp), 0xffffffffu, NOREG); 309 movi(e, ccnt, 16); 310 mov(e, R(cb), R(base)); 311 312 /* ---- chunk loop ---- */ 313 const int L_chunk = here(e); 314 loop_begin(e); 315 isetp_ge_u32(e, cb, end); 316 const int bra_done = bra_if_p0_fwd(e); 317 /* score pass: thread = position cb + tid, clamped to end - 1 (clamped lanes compute a score nobody reads) */ 318 imadi(e, R(tpos), R(cb), 1u, tid); 319 imadi(e, R(over), R(tpos), 0xffffffffu, endm1); /* endm1 - tpos */ 320 shr(e, over, over, 31); /* 1 when tpos > endm1 */ 321 imadi(e, R(t1), R(tpos), 1u, negendm1); /* tpos - endm1 */ 322 imadr(e, t1, over, t1, NOREG); 323 imadi(e, R(t1), R(t1), 0xffffffffu, tpos); /* clamped position */ 324 imadi(e, R(t1), R(t1), OMEGA_DATTN_ROW_BYTES, cyk); 325 imadw(e, krow, t1, 1u, pkc); 326 for (unsigned h = 0; h < 8; h++) movrz(e, R(sc[h])); 327 for (uint32_t d = 0; d < D; d++) ldg_wide(e, BW_IR_LDG_E_128, kq[d], krow, d * 16u, udesc, d == 0); 328 imadw(e, kpf, one, D * 16u, krow); 329 movrz(e, R(qoff)); movrz(e, R(j)); 330 const int L_q = here(e); 331 loop_begin(e); 332 isetp_ge_u32(e, j, ccnt); 333 const int bra_qend = bra_if_p0_fwd(e); 334 for (unsigned w = 0; w < 4; w++) { imadi(e, R(wd[2 * w]), S(kq[0], w), 65536u, NOREG); andi(e, R(wd[2 * w + 1]), S(kq[0], w), 0xffff0000u); } 335 for (unsigned h = 0; h < 8; h++) { 336 lds128(e, qa4, qoff, h * 512u, 1); lds128(e, qb4, qoff, h * 512u + 16u, 0); 337 for (unsigned q = 0; q < 4; q++) ffma(e, R(sc[h]), R(wd[q]), S(qa4, q), R(sc[h])); 338 for (unsigned q = 0; q < 4; q++) ffma(e, R(sc[h]), R(wd[4 + q]), S(qb4, q), R(sc[h])); 339 } 340 for (uint32_t d = 0; d + 1 < D; d++) for (unsigned w = 0; w < 4; w++) mov(e, S(kq[d], w), S(kq[d + 1], w)); 341 ldg_wide(e, BW_IR_LDG_E_128, kq[D - 1], kpf, 0, udesc, 1); 342 imadw(e, kpf, one, 16u, kpf); 343 imadi(e, R(qoff), R(one), 32u, qoff); 344 imadi(e, R(j), R(one), 1u, j); 345 bra(e, L_q); 346 loop_end(e); 347 patch_bra(e, bra_qend, here(e)); 348 imadi(e, R(sa), R(tid), 32u, smS); 349 for (unsigned h = 0; h < 8; h++) { imadi(e, R(t1), R(one), h * 4u, sa); sts32(e, t1, R(sc[h])); } 350 bar_sync(e); 351 /* V pass: this warp's positions cb + warp + 8 i, i < n = max(0, ceil((min(end, cb + 256) - cb - warp) / 8)) */ 352 imadi(e, R(t1), R(one), OMEGA_DATTN_CHUNK, cb); /* cb + 256 */ 353 imadi(e, R(t2), R(t1), 0xffffffffu, end); /* end - (cb + 256) */ 354 shr(e, over, t2, 31); /* 1 when the chunk is cut short by end */ 355 imadr(e, t2, over, t2, t1); /* cend = min(end, cb + 256) */ 356 imadi(e, R(t1), R(cb), 0xffffffffu, t2); /* cend - cb */ 357 imadi(e, R(t1), R(t1), 1u, negwarp); /* x = end - cb - warp */ 358 shr(e, over, t1, 31); 359 imadi(e, R(n), R(one), 7u, t1); 360 shr(e, n, n, 3); 361 imadr(e, t2, over, n, NOREG); 362 imadi(e, R(n), R(t2), 0xffffffffu, n); 363 imadi(e, R(t1), R(cb), 1u, warp); 364 imadi(e, R(t1), R(t1), OMEGA_DATTN_ROW_BYTES, lane8); 365 imadi(e, R(t1), R(t1), 1u, cyk); 366 imadw(e, vrow, t1, 1u, pvc); 367 for (uint32_t d = 0; d < D; d++) ldg_wide(e, BW_IR_LDG_E_64, vq[d], vrow, d * 8u * OMEGA_DATTN_ROW_BYTES, udesc, d == 0); 368 imadw(e, vpf, one, D * 8u * OMEGA_DATTN_ROW_BYTES, vrow); 369 imadi(e, R(pa), R(warp), 32u, smS); 370 movrz(e, R(i)); 371 const int L_v = here(e); 372 loop_begin(e); 373 isetp_ge_u32(e, i, n); 374 const int bra_vend = bra_if_p0_fwd(e); 375 imadi(e, R(wd[0]), S(vq[0], 0), 65536u, NOREG); andi(e, R(wd[1]), S(vq[0], 0), 0xffff0000u); 376 imadi(e, R(wd[2]), S(vq[0], 1), 65536u, NOREG); andi(e, R(wd[3]), S(vq[0], 1), 0xffff0000u); 377 lds128(e, qa4, pa, 0, 1); lds128(e, qb4, pa, 16, 0); 378 for (unsigned h = 0; h < 8; h++) { fmaxx(e, R(mn), R(m[h]), h < 4 ? S(qa4, h) : S(qb4, h - 4)); fsub(e, R(dl[h]), R(m[h]), R(mn)); mov(e, R(m[h]), R(mn)); } /* dl <= 0; exactly +0 when the max stayed */ 379 fadd(e, R(dsum), R(dl[0]), R(dl[1])); 380 for (unsigned h = 2; h < 8; h++) fadd(e, R(dsum), R(dsum), R(dl[h])); 381 isetp_ge_u32(e, one, dsum); /* 1 >= bits(dsum): no head's max moved (dsum is +0); a negative dsum has the sign bit */ 382 const int bra_nores = bra_if_p0_fwd(e); 383 for (unsigned h = 0; h < 8; h++) mufu(e, BW_IR_MUFU_EX2, R(dl[h]), R(dl[h])); 384 for (unsigned h = 0; h < 8; h++) { for (unsigned q = 0; q < 4; q++) fmul(e, S(acc[h], q), S(acc[h], q), R(dl[h])); fmul(e, R(l[h]), R(l[h]), R(dl[h])); } 385 patch_bra(e, bra_nores, here(e)); 386 for (unsigned h = 0; h < 8; h++) { fsub(e, R(dl[h]), h < 4 ? S(qa4, h) : S(qb4, h - 4), R(m[h])); mufu(e, BW_IR_MUFU_EX2, R(dl[h]), R(dl[h])); } 387 for (unsigned h = 0; h < 8; h++) { for (unsigned q = 0; q < 4; q++) ffma(e, S(acc[h], q), R(dl[h]), R(wd[q]), S(acc[h], q)); fadd(e, R(l[h]), R(l[h]), R(dl[h])); } 388 for (uint32_t d = 0; d + 1 < D; d++) for (unsigned w = 0; w < 2; w++) mov(e, S(vq[d], w), S(vq[d + 1], w)); 389 ldg_wide(e, BW_IR_LDG_E_64, vq[D - 1], vpf, 0, udesc, 1); 390 imadw(e, vpf, one, 8u * OMEGA_DATTN_ROW_BYTES, vpf); 391 imadi(e, R(pa), R(one), 8u * 32u, pa); 392 imadi(e, R(i), R(one), 1u, i); 393 bra(e, L_v); 394 loop_end(e); 395 patch_bra(e, bra_vend, here(e)); 396 bar_sync(e); /* the next score pass overwrites s[][] */ 397 imadi(e, R(cb), R(one), OMEGA_DATTN_CHUNK, cb); 398 bra(e, L_chunk); 399 loop_end(e); 400 patch_bra(e, bra_done, here(e)); 401 402 /* ---- epilogue: combine the warps, write the partial ---- */ 403 int ep[8], ea[8], eb[8]; 404 for (unsigned h = 0; h < 8; h++) { ep[h] = V(e); ea[h] = V(e); eb[h] = V(e); } 405 int pout = V64(e), ppart = V64(e), pcnt = V64(e), oscale = V(e); 406 if (e->err) return -1; 407 ldc64(e, pout, OMEGA_DATTN_ARG_OUT); ldc64(e, ppart, OMEGA_DATTN_ARG_PART); ldc64(e, pcnt, OMEGA_DATTN_ARG_CNT); ldc32(e, oscale, OMEGA_DATTN_ARG_OUT_SCALE); 408 /* per-warp maxes to shared: lane 0 of warp w writes m[h] at SM_M + w*32 + h*4 */ 409 imadi(e, R(a0), R(warp), 32u, NOREG); imadi(e, R(a0), R(one), OMEGA_DATTN_SM_M, a0); 410 isetp_ge_u32(e, lane, one); 411 { Region r = region_skip_if_p0(e); for (unsigned h = 0; h < 8; h++) { imadi(e, R(t1), R(one), h * 4u, a0); sts32(e, t1, R(m[h])); } region_end(e, r); } 412 bar_sync(e); 413 /* lanes 0..7 of every warp: M[lane] = max over the 8 warps; lanes of warp 0 write it at SM_M + 256 + lane*4 */ 414 movi(e, ccnt, 8); 415 isetp_ge_u32(e, lane, ccnt); 416 { 417 Region r = region_skip_if_p0(e); 418 imadi(e, R(a1), R(lane), 4u, NOREG); imadi(e, R(a1), R(one), OMEGA_DATTN_SM_M, a1); 419 for (unsigned w = 0; w < 8; w++) { imadi(e, R(t1), R(one), w * 32u, a1); lds32(e, R(ep[w]), t1, w == 0); } 420 for (unsigned w = 1; w < 8; w++) fmaxx(e, R(ep[0]), R(ep[0]), R(ep[w])); 421 imadi(e, R(t1), R(one), 256u, a1); 422 sts32(e, t1, R(ep[0])); 423 region_end(e, r); 424 } 425 bar_sync(e); 426 movi(e, t1, OMEGA_DATTN_SM_M + 256u); 427 lds128(e, qa4, t1, 0, 1); lds128(e, qb4, t1, 16, 0); 428 for (unsigned h = 0; h < 8; h++) { fsub(e, R(ea[h]), R(m[h]), h < 4 ? S(qa4, h) : S(qb4, h - 4)); mufu(e, BW_IR_MUFU_EX2, R(ea[h]), R(ea[h])); } 429 for (unsigned h = 0; h < 8; h++) { for (unsigned q = 0; q < 4; q++) fmul(e, S(acc[h], q), S(acc[h], q), R(ea[h])); fmul(e, R(l[h]), R(l[h]), R(ea[h])); } 430 /* per-warp l to shared, then the partial's m and l by threads 0..7 */ 431 imadi(e, R(a0), R(warp), 32u, NOREG); imadi(e, R(a0), R(one), OMEGA_DATTN_SM_L, a0); 432 isetp_ge_u32(e, lane, one); 433 { Region r = region_skip_if_p0(e); for (unsigned h = 0; h < 8; h++) { imadi(e, R(t1), R(one), h * 4u, a0); sts32(e, t1, R(l[h])); } region_end(e, r); } 434 bar_sync(e); 435 int pp = V64(e), pa64 = V64(e), pb64 = V64(e); 436 imadr(e, t2, cy, splits, cx); 437 imadi(e, R(t2), R(t2), OMEGA_DATTN_PARTIAL_WORDS * 4u, NOREG); 438 imadw(e, pp, t2, 1u, ppart); 439 isetp_ge_u32(e, tid, ccnt); /* tid >= 8 skips */ 440 { 441 Region r = region_skip_if_p0(e); 442 imadi(e, R(a1), R(tid), 4u, NOREG); imadi(e, R(a1), R(one), OMEGA_DATTN_SM_L, a1); 443 for (unsigned w = 0; w < 8; w++) { imadi(e, R(t1), R(one), w * 32u, a1); lds32(e, R(ep[w]), t1, w == 0); } 444 for (unsigned w = 1; w < 8; w++) fadd(e, R(ep[0]), R(ep[0]), R(ep[w])); 445 imadi(e, R(t1), R(tid), 4u, NOREG); imadi(e, R(t1), R(one), OMEGA_DATTN_SM_M + 256u, t1); 446 lds32(e, R(ep[1]), t1, 1); 447 imadw(e, pa64, tid, 4u, pp); 448 stg(e, BW_IR_STG_E, pa64, R(ep[1]), udesc); /* m[h] */ 449 imadw(e, pb64, one, 32u, pa64); 450 stg(e, BW_IR_STG_E, pb64, R(ep[0]), udesc); /* l[h] */ 451 region_end(e, r); 452 } 453 /* acc: 4 rounds of 2 heads through the s[][] region: r[w][h'][128]; the 8 store and 8 load addresses computed once */ 454 imadi(e, R(a0), R(warp), 1024u, NOREG); imadi(e, R(a0), R(lane), 16u, a0); imadi(e, R(a0), R(one), OMEGA_DATTN_SM_S, a0); 455 imadi(e, R(a1), R(tid), 4u, NOREG); imadi(e, R(a1), R(one), OMEGA_DATTN_SM_S, a1); 456 for (unsigned q = 0; q < 8; q++) imadi(e, R(eb[q]), R(one), (q / 4u) * 512u + (q % 4u) * 4u, a0); 457 for (unsigned w = 0; w < 8; w++) imadi(e, R(ea[w]), R(one), w * 1024u, a1); 458 imadw(e, pa64, tid, 4u, pp); 459 for (unsigned rd = 0; rd < 4; rd++) { 460 for (unsigned hh = 0; hh < 2; hh++) for (unsigned q = 0; q < 4; q++) sts32(e, eb[hh * 4 + q], S(acc[2 * rd + hh], q)); 461 bar_sync(e); 462 for (unsigned w = 0; w < 8; w++) lds32(e, R(ep[w]), ea[w], w == 0); 463 for (unsigned w = 1; w < 8; w++) fadd(e, R(ep[0]), R(ep[0]), R(ep[w])); 464 imadw(e, pb64, one, 64u + rd * 1024u, pa64); 465 stg(e, BW_IR_STG_E, pb64, R(ep[0]), udesc); 466 bar_sync(e); 467 } 468 /* publish, count, the last CTA combines */ 469 membar(e); 470 bar_sync(e); 471 movi(e, t1, OMEGA_DATTN_SM_FLAG); 472 isetp_ge_u32(e, tid, one); 473 { 474 Region r = region_skip_if_p0(e); 475 imadw(e, pb64, cy, 4u, pcnt); 476 atom_add(e, ep[0], pb64, one, udesc); 477 sts32(e, t1, R(ep[0])); 478 region_end(e, r); 479 } 480 bar_sync(e); 481 lds32(e, R(ep[0]), t1, 1); 482 isetp_ge_u32(e, ep[0], splitsm1); 483 const int bra_last = bra_if_p0_fwd(e); 484 exit_k(e); 485 patch_bra(e, bra_last, here(e)); 486 /* combine: thread tid, outputs o = tid + 256 r (head o >> 7, dim o & 127), r = 0..3 */ 487 int ho = V(e), oidx = V(e), ma = V64(e), la = V64(e), aa = V64(e), num = V(e), den = V(e), mx = V(e), s_i = V(e), rr = V(e); 488 if (e->err) return -1; 489 shr(e, ho, tid, 7); 490 mov(e, R(oidx), R(tid)); 491 movrz(e, R(rr)); 492 movi(e, ccnt, 4); 493 imadr(e, t2, cy, splits, NOREG); 494 imadi(e, R(t2), R(t2), OMEGA_DATTN_PARTIAL_WORDS * 4u, NOREG); /* byte offset of partials[cy][0] */ 495 const int L_r = here(e); 496 loop_begin(e); 497 isetp_ge_u32(e, rr, ccnt); 498 const int bra_rend = bra_if_p0_fwd(e); 499 imadi(e, R(t1), R(ho), 4u, t2); imadw(e, ma, t1, 1u, ppart); 500 imadw(e, la, one, 32u, ma); 501 imadi(e, R(t1), R(oidx), 4u, t2); imadw(e, aa, t1, 1u, ppart); imadw(e, aa, one, 64u, aa); 502 movrz(e, R(num)); movrz(e, R(den)); movi(e, mx, NEG_BIG); movrz(e, R(s_i)); 503 const int L_s = here(e); 504 loop_begin(e); 505 isetp_ge_u32(e, s_i, splits); 506 const int bra_send = bra_if_p0_fwd(e); 507 ldg32(e, BW_IR_LDG_STRONG_SYS, ep[0], ma, udesc, 1); 508 ldg32(e, BW_IR_LDG_STRONG_SYS, ep[1], la, udesc, 0); 509 ldg32(e, BW_IR_LDG_STRONG_SYS, ep[2], aa, udesc, 0); 510 if (MUT != 4) { 511 fmaxx(e, R(ep[3]), R(mx), R(ep[0])); 512 fsub(e, R(ep[4]), R(mx), R(ep[3])); mufu(e, BW_IR_MUFU_EX2, R(ep[4]), R(ep[4])); 513 fsub(e, R(ep[5]), R(ep[0]), R(ep[3])); mufu(e, BW_IR_MUFU_EX2, R(ep[5]), R(ep[5])); 514 mov(e, R(mx), R(ep[3])); 515 fmul(e, R(num), R(num), R(ep[4])); fmul(e, R(den), R(den), R(ep[4])); 516 } else { 517 movi(e, ep[5], 0x3f800000u); 518 } 519 ffma(e, R(num), R(ep[5]), R(ep[2]), R(num)); 520 ffma(e, R(den), R(ep[5]), R(ep[1]), R(den)); 521 imadw(e, ma, one, OMEGA_DATTN_PARTIAL_WORDS * 4u, ma); 522 imadw(e, la, one, OMEGA_DATTN_PARTIAL_WORDS * 4u, la); 523 imadw(e, aa, one, OMEGA_DATTN_PARTIAL_WORDS * 4u, aa); 524 imadi(e, R(s_i), R(one), 1u, s_i); 525 bra(e, L_s); 526 loop_end(e); 527 patch_bra(e, bra_send, here(e)); 528 mufu(e, BW_IR_MUFU_RCP, R(den), R(den)); 529 fmul(e, R(num), R(num), R(den)); 530 fmul(e, R(num), R(num), R(oscale)); 531 imadi(e, R(t1), R(cy), 1024u, oidx); 532 imadw(e, pb64, t1, 4u, pout); 533 stg(e, BW_IR_STG_E, pb64, R(num), udesc); 534 imadi(e, R(ho), R(one), 2u, ho); 535 imadi(e, R(oidx), R(one), 256u, oidx); 536 imadi(e, R(rr), R(one), 1u, rr); 537 bra(e, L_r); 538 loop_end(e); 539 patch_bra(e, bra_rend, here(e)); 540 /* thread 0 resets the counter for the next launch (every other CTA of this head has already added) */ 541 isetp_ge_u32(e, tid, one); 542 { Region r = region_skip_if_p0(e); imadw(e, pb64, cy, 4u, pcnt); stg(e, BW_IR_STG_STRONG_SYS, pb64, R(zero), udesc); region_end(e, r); } 543 tail(e); 544 if (e->err) return -1; 545 return omega_bw_regalloc_solve(prog); 546 } 547 #else 548 int omega_blackwell_codegen_decode_attn_prog(const OmegaDecodeAttnSpec *spec, BlackwellIRProgram *prog) { 549 uint64_t error = 0; 550 if (!spec || !prog || spec->depth < 1 || spec->depth > OMEGA_DATTN_MAX_DEPTH || 551 spec->mutant < 0 || spec->mutant > 4) return -1; 552 return omega_gpu_om_ir_replay((OmegaOmIrUnit)omega_cg_attn, 0, spec->depth, 553 (uint64_t)spec->mutant, 0, prog, &error) == 0 ? 0 : -1; 554 } 555 #endif 556 557 int omega_decode_attn_prog_stats(const OmegaDecodeAttnSpec *spec, uint32_t *insns, uint32_t *gprs) { 558 static BlackwellIRProgram prog; 559 if (omega_blackwell_codegen_decode_attn_prog(spec, &prog) != 0) return -1; 560 if (insns) *insns = (uint32_t)prog.count; 561 if (gprs) *gprs = prog.regalloc.peak_gpr_usage; 562 return 0; 563 }