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
CUDApp / src / omega_blackwell_codegen_attn256.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* |
| 3 | * omega_blackwell_codegen_attn256.c: the one-launch decode attention kernel for head_dim 256 (see the header). The |
| 4 | * emitter helpers are the ones of omega_blackwell_codegen_attn.c (control words, read barrier on every load, loop |
| 5 | * live ranges); the program follows that kernel with the 27B geometry and the dim-split V pass. |
| 6 | */ |
| 7 | #include "omega_blackwell_codegen_attn256.h" |
| 8 | #include "omega_blackwell_codegen_attn.h" |
| 9 | #include <string.h> |
| 10 | |
| 11 | #ifndef CUDAPP_MIGRATION_PENDING_C_CODEGEN_ATTN256 |
| 12 | #define CUDAPP_MIGRATION_PENDING_C_CODEGEN_ATTN256 0 |
| 13 | #endif |
| 14 | #if CUDAPP_MIGRATION_PENDING_C_CODEGEN_ATTN256 |
| 15 | #define CW(stall, wbar, rbar, wait) \ |
| 16 | (((uint32_t)(stall) << 9) | ((uint32_t)(wbar) << 14) | ((uint32_t)(rbar) << 17) | ((uint32_t)(wait) << 20)) |
| 17 | #define CW_YIELD 0x2000u |
| 18 | #define SB_NONE 7u |
| 19 | #define SB_GPR 0u |
| 20 | #define SB_RD 1u |
| 21 | #define SB_UGPR 2u |
| 22 | #define NOREG (-1) |
| 23 | #define MAX_LOOPS 4 |
| 24 | |
| 25 | enum { K_FIXED, K_PRED, K_VAR, K_VAR_MORE, K_UVAR, K_STORE, K_SYNC, K_EXIT, K_BRA, K_BSSY, K_BSYNC }; |
| 26 | |
| 27 | typedef struct { |
| 28 | BlackwellIRProgram *p; |
| 29 | uint32_t pending; |
| 30 | int err; |
| 31 | int open_start[MAX_LOOPS]; |
| 32 | int depth; |
| 33 | } Em; |
| 34 | |
| 35 | static void emit(Em *e, BlackwellIRInsn in, int kind) { |
| 36 | uint32_t wait = e->pending; |
| 37 | switch (kind) { |
| 38 | case K_FIXED: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; |
| 39 | case K_PRED: in.control = CW(14, SB_NONE, SB_NONE, wait); e->pending = 0; break; |
| 40 | case K_VAR: in.control = CW(4, SB_GPR, SB_RD, wait) | CW_YIELD; e->pending = (1u << SB_GPR) | (1u << SB_RD); break; |
| 41 | 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; |
| 42 | case K_UVAR: in.control = CW(4, SB_UGPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_UGPR; break; |
| 43 | case K_STORE: in.control = CW(4, SB_NONE, SB_RD, wait) | CW_YIELD; e->pending = 1u << SB_RD; break; |
| 44 | case K_SYNC: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; |
| 45 | case K_EXIT: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; |
| 46 | case K_BRA: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; |
| 47 | case K_BSSY: in.control = CW(1, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; |
| 48 | case K_BSYNC: in.control = CW(5, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; |
| 49 | default: e->err = -1; return; |
| 50 | } |
| 51 | if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; |
| 52 | } |
| 53 | |
| 54 | static BlackwellIRInsn I0(BlackwellIROpcode op) { |
| 55 | BlackwellIRInsn i; memset(&i, 0, sizeof i); |
| 56 | i.op = op; i.dst_vreg = NOREG; i.src1_vreg = NOREG; i.src2_vreg = NOREG; i.src3_vreg = NOREG; i.ureg = NOREG; |
| 57 | return i; |
| 58 | } |
| 59 | static int V(Em *e) { int v = omega_bw_ir_alloc_vreg(e->p); if (v < 0) e->err = -1; return v; } |
| 60 | static int V64(Em *e) { int v = omega_bw_ir_alloc_vreg64(e->p); if (v < 0) e->err = -1; return v; } |
| 61 | static int V128(Em *e) { int v = omega_bw_ir_alloc_vreg128(e->p); if (v < 0) e->err = -1; return v; } |
| 62 | static int UV64(Em *e) { int v = omega_bw_ir_alloc_uvreg64(e->p); if (v < 0) e->err = -1; return v; } |
| 63 | static int here(const Em *e) { return (int)e->p->count; } |
| 64 | |
| 65 | /* operand with subregister */ |
| 66 | typedef struct { int v; unsigned s; } Op; |
| 67 | static Op R(int v) { Op o = { v, 0 }; return o; } |
| 68 | static Op S(int v, unsigned s) { Op o = { v, s }; return o; } |
| 69 | |
| 70 | 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); } |
| 71 | 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); } |
| 72 | 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); } |
| 73 | 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); } |
| 74 | 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); } |
| 75 | 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); } |
| 76 | /* d = a * imm + c */ |
| 77 | 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); } |
| 78 | /* d = a * b + c (registers) */ |
| 79 | 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); } |
| 80 | /* d64 = a * imm + c64 */ |
| 81 | 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); } |
| 82 | static void mov(Em *e, Op d, Op a) { imadi(e, d, a, 1u, NOREG); } |
| 83 | 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); } |
| 84 | static void shr(Em *e, int d, int a, uint32_t imm) { shrs(e, d, R(a), imm); } |
| 85 | 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); } |
| 86 | 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); } |
| 87 | 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); } |
| 88 | static void ldg_wide(Em *e, BlackwellIROpcode op, int d, int addr64, uint32_t imm, int udesc, int first) { |
| 89 | 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); |
| 90 | } |
| 91 | 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); } |
| 92 | 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); } |
| 93 | 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); } |
| 94 | static void membar(Em *e) { emit(e, I0(BW_IR_MEMBAR_ALL_SYS), K_FIXED); } |
| 95 | 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); } |
| 96 | 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); } |
| 97 | 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); } |
| 98 | static void bar_sync(Em *e) { emit(e, I0(BW_IR_BAR_SYNC), K_SYNC); } |
| 99 | static void fop(Em *e, BlackwellIROpcode op, Op d, Op a, Op b, Op c) { |
| 100 | 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; |
| 101 | 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); |
| 102 | } |
| 103 | static void fadd(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FADD, d, a, b, R(NOREG)); } |
| 104 | static void fsub(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FSUB, d, a, b, R(NOREG)); } |
| 105 | static void fmul(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FMUL, d, a, b, R(NOREG)); } |
| 106 | static void ffma(Em *e, Op d, Op a, Op b, Op c) { fop(e, BW_IR_FFMA, d, a, b, c); } |
| 107 | static void fmaxx(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FMNMX_MAX, d, a, b, R(NOREG)); } |
| 108 | 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); } |
| 109 | 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); } |
| 110 | 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); } |
| 111 | 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); } |
| 112 | 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; } |
| 113 | static void patch_bra(Em *e, int at, int target) { if (!e->err) e->p->insns[at].imm = (uint32_t)(target - at); } |
| 114 | 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; } |
| 115 | static void bsync(Em *e) { BlackwellIRInsn i = I0(BW_IR_BSYNC); i.bar_reg = 0; emit(e, i, K_BSYNC); } |
| 116 | static void exit_k(Em *e) { emit(e, I0(BW_IR_EXIT), K_EXIT); } |
| 117 | static void tail(Em *e) { |
| 118 | exit_k(e); |
| 119 | BlackwellIRInsn self = I0(BW_IR_BRA); |
| 120 | self.control = 0x000fc000u; |
| 121 | if (omega_bw_ir_append(e->p, &self) < 0) e->err = -1; |
| 122 | } |
| 123 | static void loop_begin(Em *e) { if (e->depth >= MAX_LOOPS) { e->err = -1; return; } e->open_start[e->depth++] = here(e); } |
| 124 | static void loop_end(Em *e) { |
| 125 | if (e->depth <= 0) { e->err = -1; return; } |
| 126 | int start = e->open_start[--e->depth], end = here(e) - 1; |
| 127 | OmegaRegAlloc *ra = &e->p->regalloc; |
| 128 | for (int v = 0; v < ra->num_vregs; v++) { |
| 129 | OmegaLiveInterval *iv = &ra->intervals[v]; |
| 130 | if (iv->first_def >= 0 && iv->first_def < start && iv->last_use >= start && iv->last_use < end) iv->last_use = end; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /* A region only some lanes of a warp execute: BSSY ... @P0 BRA join ... BSYNC. P0 true = skip. */ |
| 135 | typedef struct { int bssy, bra; } Region; |
| 136 | static Region region_skip_if_p0(Em *e) { Region r; r.bssy = bssy_fwd(e); r.bra = bra_if_p0_fwd(e); return r; } |
| 137 | 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); } |
| 138 | |
| 139 | /* bf16 round-to-nearest-even of f32 x into the low 16 bits of d: r = x + 0x7fff + ((x >> 16) & 1); d = r >> 16 */ |
| 140 | static void bf16_of(Em *e, int d, Op x, int c7fff, int t) { |
| 141 | shrs(e, t, x, 16); |
| 142 | andi(e, R(t), R(t), 1u); |
| 143 | iadd(e, t, t, c7fff); |
| 144 | fop(e, BW_IR_IADD3, R(d), x, R(t), R(NOREG)); /* IADD3 d = x + t */ |
| 145 | shr(e, d, d, 16); |
| 146 | } |
| 147 | |
| 148 | static const uint32_t NEG_BIG = 0xF149F2CAu; /* -1e30f */ |
| 149 | |
| 150 | int omega_dattn256_shape(const OmegaDecodeAttn256Spec *spec, OmegaDattn256Shape *s) { |
| 151 | const uint32_t qh = spec->q_heads ? spec->q_heads : OMEGA_DATTN256_Q_HEADS; |
| 152 | const uint32_t kvh = spec->kv_heads ? spec->kv_heads : OMEGA_DATTN256_KV_HEADS; |
| 153 | const uint32_t h = spec->q_per_cta ? spec->q_per_cta : OMEGA_DATTN256_Q_PER_KV; |
| 154 | if (h < 1u || h > 6u || qh % h || kvh == 0 || (qh / h) % kvh) return -1; |
| 155 | const uint32_t g = (qh / h) / kvh; |
| 156 | if (g != 1u && g != 2u) return -1; |
| 157 | s->qh = qh; s->kvh = kvh; s->h = h; s->vy = qh / h; s->g = g; |
| 158 | s->qkv_words = (qh + 2u * kvh) * 256u; s->out_words = qh * 256u; |
| 159 | return 0; |
| 160 | } |
| 161 | #define RB OMEGA_DATTN256_ROW_BYTES |
| 162 | #define PBYTES (OMEGA_DATTN256_PARTIAL_WORDS * 4u) |
| 163 | |
| 164 | /* 8 consecutive f32 words (two quads) to shared at base + 0 .. 28 */ |
| 165 | static void sts8(Em *e, int base, int one, int t, int qa, int qb) { |
| 166 | for (unsigned i = 0; i < 8; i++) { imadi(e, R(t), R(one), i * 4u, base); sts32(e, t, i < 4 ? S(qa, i) : S(qb, i - 4)); } |
| 167 | } |
| 168 | /* sum of squares of 8 values, warp-reduced, broadcast through shared word `addr` (lane 0 writes), rs = RSQ(sum/256 + eps) */ |
| 169 | static void rms_scale(Em *e, int qa, int qb, int sq, int ts, int rs, int addr, int lane, int one, int eps) { |
| 170 | fmul(e, R(sq), S(qa, 0), S(qa, 0)); |
| 171 | for (unsigned i = 1; i < 4; i++) ffma(e, R(sq), S(qa, i), S(qa, i), R(sq)); |
| 172 | for (unsigned i = 0; i < 4; i++) ffma(e, R(sq), S(qb, i), S(qb, i), R(sq)); |
| 173 | for (uint32_t o = 16; o >= 1; o /= 2) { shfl_down(e, ts, sq, o); fadd(e, R(sq), R(sq), R(ts)); } |
| 174 | isetp_ge_u32(e, lane, one); |
| 175 | { Region r = region_skip_if_p0(e); sts32(e, addr, R(sq)); region_end(e, r); } |
| 176 | bar_sync(e); |
| 177 | lds32(e, R(sq), addr, 1); |
| 178 | movi(e, ts, 0x3b800000u); /* 1/256 */ |
| 179 | ffma(e, R(rs), R(sq), R(ts), R(eps)); |
| 180 | mufu(e, BW_IR_MUFU_RSQ, R(rs), R(rs)); |
| 181 | } |
| 182 | |
| 183 | int omega_blackwell_codegen_decode_attn256_prog(const OmegaDecodeAttn256Spec *spec, BlackwellIRProgram *prog) { |
| 184 | if (!spec || !prog || spec->depth < 1 || spec->depth > OMEGA_DATTN256_MAX_DEPTH || spec->mutant < 0 || spec->mutant > 5 || spec->prep > 1 || spec->multi > 1) return -1; |
| 185 | omega_bw_ir_init(prog); |
| 186 | Em em; memset(&em, 0, sizeof em); em.p = prog; Em *e = &em; |
| 187 | const uint32_t D = spec->depth; |
| 188 | OmegaDattn256Shape SH; |
| 189 | if (omega_dattn256_shape(spec, &SH)) return -1; |
| 190 | const uint32_t H = SH.h; /* q heads per CTA row */ |
| 191 | const int OLD = SH.qh == OMEGA_DATTN256_Q_HEADS && SH.kvh == OMEGA_DATTN256_KV_HEADS && H == OMEGA_DATTN256_Q_PER_KV; |
| 192 | const int MUT = spec->mutant; |
| 193 | |
| 194 | /* ---- arguments and identities ---- */ |
| 195 | int udesc = UV64(e); |
| 196 | int pqkv = V64(e), pkc = V64(e), pvc = V64(e), prope = V64(e), pqn = V64(e); |
| 197 | int pos = V(e), per = V(e), splits = V(e), kvs = V(e), eps = V(e), qks = V(e); |
| 198 | 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); |
| 199 | if (e->err) return -1; |
| 200 | ldcu64(e, udesc, 0x358); |
| 201 | ldc64(e, pqkv, OMEGA_DATTN256_ARG_QKV); ldc64(e, pkc, OMEGA_DATTN256_ARG_KC); ldc64(e, pvc, OMEGA_DATTN256_ARG_VC); |
| 202 | ldc64(e, prope, OMEGA_DATTN256_ARG_ROPE); ldc64(e, pqn, OMEGA_DATTN256_ARG_QN); /* k_norm is read at q_norm + 1024 bytes (ARG_KN is informational) */ |
| 203 | ldc32(e, pos, OMEGA_DATTN256_ARG_POS); ldc32(e, per, OMEGA_DATTN256_ARG_PER_SPLIT); ldc32(e, splits, OMEGA_DATTN256_ARG_SPLITS); |
| 204 | ldc32(e, kvs, OMEGA_DATTN256_ARG_KV_STRIDE); ldc32(e, eps, OMEGA_DATTN256_ARG_EPS); |
| 205 | ldc32(e, qks, OMEGA_DATTN256_ARG_QK_SCALE); |
| 206 | s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y); |
| 207 | if (spec->prep && !spec->multi) { /* prefill prep (#545): CTA cx = token cx at position pos0 + cx, its qkv row */ |
| 208 | iadd(e, pos, pos, cx); |
| 209 | { int rowoff = V(e); if (e->err) return -1; imadi(e, R(rowoff), R(cx), SH.qkv_words * 4u, NOREG); imadw(e, pqkv, rowoff, 1u, pqkv); } |
| 210 | } |
| 211 | movi(e, one, 1); movrz(e, R(zero)); movi(e, c7fff, 0x7fffu); movi(e, c16, 16); |
| 212 | shr(e, warp, tid, 5); |
| 213 | andi(e, R(lane), R(tid), 31u); |
| 214 | if (spec->multi) { /* L369 multi-token: the token, its position and qkv row (ARG_XSTRIDE bytes per row) */ |
| 215 | int tok = V(e), xs = V(e); if (e->err) return -1; |
| 216 | ldc32(e, xs, OMEGA_DATTN256_ARG_XSTRIDE); |
| 217 | if (spec->prep) mov(e, R(tok), R(cx)); /* prep: CTA (token, row) */ |
| 218 | else { /* attend: CTA (split, token * vy + row) */ |
| 219 | /* Refuse non-powers before the logarithm can shift by 32. */ |
| 220 | if (SH.vy & (SH.vy - 1u)) return -1; |
| 221 | uint32_t lg = 0; while ((1u << lg) < SH.vy) lg++; |
| 222 | shr(e, tok, cy, lg); andi(e, R(cy), R(cy), SH.vy - 1u); |
| 223 | } |
| 224 | iadd(e, pos, pos, tok); |
| 225 | imadr(e, xs, tok, xs, NOREG); imadw(e, pqkv, xs, 1u, pqkv); |
| 226 | if (!spec->prep) { /* this token's splits (per_split 256): CTAs past them exit */ |
| 227 | shr(e, splits, pos, 8); imadi(e, R(splits), R(splits), 1u, one); |
| 228 | imadi(e, R(xs), R(one), 0xffffffffu, splits); |
| 229 | isetp_ge_u32(e, xs, cx); |
| 230 | int bk = bra_if_p0_fwd(e); exit_k(e); patch_bra(e, bk, here(e)); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | /* range of this CTA: base = cx * per; end = min(base + per, pos + 1) */ |
| 235 | 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); |
| 236 | imadr(e, base, cx, per, NOREG); |
| 237 | imadi(e, R(bend), R(base), 1u, per); |
| 238 | imadi(e, R(ctx), R(pos), 1u, one); |
| 239 | imadi(e, R(t1), R(ctx), 0xffffffffu, bend); /* t1 = bend - ctx */ |
| 240 | imadi(e, R(t2), R(t1), 0xffffffffu, NOREG); /* t2 = ctx - bend */ |
| 241 | shr(e, t2, t2, 31); /* 1 when bend > ctx */ |
| 242 | imadr(e, t2, t2, t1, NOREG); /* t2 = over * (bend - ctx) */ |
| 243 | imadi(e, R(end), R(t2), 0xffffffffu, bend); /* end = bend - t2 */ |
| 244 | imadi(e, R(endm1), R(one), 0xffffffffu, end); |
| 245 | imadi(e, R(negendm1), R(endm1), 0xffffffffu, NOREG); |
| 246 | imadi(e, R(splitsm1), R(one), 0xffffffffu, splits); |
| 247 | if (SH.g == 1u) imadr(e, cyk, cy, kvs, NOREG); /* byte offset of this KV head's plane */ |
| 248 | else { shr(e, t1, cy, 1); imadr(e, cyk, t1, kvs, NOREG); } /* G = 2: KV head cy / 2 */ |
| 249 | |
| 250 | /* ---- prologue: one norm + rope block for all 8 warps. Warp h < 6 builds q'[h] (head cy*6+h, q_norm, * qk_scale); |
| 251 | * warp 6 builds k_new (k[cy], k_norm = q_norm + 1024 bytes, scale 1); warp 7 runs the block on v[cy] (the result is |
| 252 | * discarded) and reloads the raw v at the store. Warps 6, 7 stage in the s[][] region (unused until the chunk loop). ---- */ |
| 253 | int qa = V128(e), qb = V128(e), wa = V128(e), wb = V128(e), pa_ = V128(e), pb_ = V128(e); |
| 254 | int cosa = V128(e), cosb = V128(e), sina = V128(e), sinb = V128(e); |
| 255 | int sq = V(e), ts = V(e), rs = V(e), a0 = V(e), a1 = V(e), ad64 = V64(e), rope64 = V64(e), sgn = V(e), pl = V(e), f = V(e), voff = V(e), qksw = V(e); |
| 256 | if (e->err) return -1; |
| 257 | movi(e, t1, 5u); imadi(e, R(t1), R(warp), 0xffffffffu, t1); /* 5 - warp */ |
| 258 | shr(e, f, t1, 31); /* f = 1 for warps 6, 7 */ |
| 259 | /* byte offset of the vector in qkv: q head cy*6 + warp at (cy*6 + warp)*1024; k[cy] at 24576 + cy*1024 = warp*4096 + cy*1024 |
| 260 | * for warp 6; v[cy] at 28672 + cy*1024 = warp*4096 + cy*1024 for warp 7 */ |
| 261 | imadi(e, R(t1), R(cy), H, warp); imadi(e, R(t1), R(t1), 1024u, NOREG); |
| 262 | if (OLD) { imadi(e, R(t2), R(warp), 4096u, NOREG); imadi(e, R(t2), R(cy), 1024u, t2); } |
| 263 | else { /* q_heads*1024 + (warp - 6)*kv_heads*1024 + (cy / G)*1024 */ |
| 264 | if (SH.g == 1u) mov(e, R(t2), R(cy)); else shr(e, t2, cy, 1); |
| 265 | imadi(e, R(t2), R(t2), 1024u, NOREG); |
| 266 | imadi(e, R(t2), R(warp), SH.kvh * 1024u, t2); |
| 267 | imadi(e, R(t2), R(one), (SH.qh - 6u * SH.kvh) * 1024u, t2); |
| 268 | } |
| 269 | imadi(e, R(t2), R(t1), 0xffffffffu, t2); /* off_kv - off_q */ |
| 270 | imadr(e, voff, f, t2, t1); /* f ? off_kv : off_q */ |
| 271 | imadi(e, R(voff), R(lane), 32u, voff); /* + this lane's 8 dims */ |
| 272 | imadw(e, ad64, voff, 1u, pqkv); |
| 273 | ldg_wide(e, BW_IR_LDG_E_128, qa, ad64, 0, udesc, 1); |
| 274 | ldg_wide(e, BW_IR_LDG_E_128, qb, ad64, 16, udesc, 0); |
| 275 | imadi(e, R(t2), R(f), 1024u, NOREG); imadi(e, R(t2), R(lane), 32u, t2); /* q_norm, or k_norm 1024 bytes after it */ |
| 276 | imadw(e, ad64, t2, 1u, pqn); |
| 277 | ldg_wide(e, BW_IR_LDG_E_128, wa, ad64, 0, udesc, 0); |
| 278 | ldg_wide(e, BW_IR_LDG_E_128, wb, ad64, 16, udesc, 0); |
| 279 | /* rope row for this lane: pair index = (lane & 15) * 8 .. +7 (dims 8i..8i+7 for lanes < 16, dims 128 + 8(i-16) above) */ |
| 280 | andi(e, R(t2), R(lane), 15u); |
| 281 | imadi(e, R(t2), R(t2), 32u, NOREG); |
| 282 | imadi(e, R(t2), R(pos), OMEGA_DATTN256_ROPE_ROW_BYTES, t2); |
| 283 | imadw(e, rope64, t2, 1u, prope); |
| 284 | ldg_wide(e, BW_IR_LDG_E_128, cosa, rope64, 0, udesc, 0); |
| 285 | ldg_wide(e, BW_IR_LDG_E_128, cosb, rope64, 16, udesc, 0); |
| 286 | ldg_wide(e, BW_IR_LDG_E_128, sina, rope64, 512, udesc, 0); |
| 287 | ldg_wide(e, BW_IR_LDG_E_128, sinb, rope64, 528, udesc, 0); |
| 288 | /* sign of the sin term: lanes < 16 take x[d] * cos - x[d+128] * sin, lanes >= 16 take x[d] * cos + x[d-128] * sin */ |
| 289 | imadi(e, R(sgn), R(one), 0xfffffff0u, lane); /* lane - 16 */ |
| 290 | shr(e, sgn, sgn, 31); |
| 291 | imadi(e, R(sgn), R(sgn), 0x80000000u, NOREG); |
| 292 | for (unsigned i = 0; i < 4; i++) { xorr(e, S(sina, i), S(sina, i), R(sgn)); xorr(e, S(sinb, i), S(sinb, i), R(sgn)); } |
| 293 | xorr(e, R(pl), R(lane), R(c16)); /* partner lane */ |
| 294 | |
| 295 | /* RMSNorm of the lane's 8 dims: sum of squares over the warp, broadcast through shared memory */ |
| 296 | imadi(e, R(a0), R(warp), 4u, NOREG); imadi(e, R(a0), R(one), OMEGA_DATTN256_SM_L, a0); |
| 297 | rms_scale(e, qa, qb, sq, ts, rs, a0, lane, one, eps); |
| 298 | for (unsigned i = 0; i < 4; i++) { fmul(e, S(qa, i), S(qa, i), R(rs)); fmul(e, S(qa, i), S(qa, i), S(wa, i)); fmul(e, S(qb, i), S(qb, i), R(rs)); fmul(e, S(qb, i), S(qb, i), S(wb, i)); } |
| 299 | /* stage, read the partner half, rotate */ |
| 300 | imadi(e, R(a0), R(warp), 1024u, NOREG); imadi(e, R(a0), R(lane), 32u, a0); /* SM_Q + warp*1024 + lane*32 (SM_Q = 0) */ |
| 301 | sts8(e, a0, one, t1, qa, qb); |
| 302 | bar_sync(e); |
| 303 | imadi(e, R(t2), R(warp), 1024u, NOREG); imadi(e, R(t2), R(pl), 32u, t2); |
| 304 | lds128(e, pa_, t2, 0, 1); lds128(e, pb_, t2, 16, 0); |
| 305 | if (MUT != 1) for (unsigned i = 0; i < 4; i++) { |
| 306 | fmul(e, R(ts), S(qa, i), S(cosa, i)); ffma(e, S(qa, i), S(pa_, i), S(sina, i), R(ts)); |
| 307 | fmul(e, R(ts), S(qb, i), S(cosb, i)); ffma(e, S(qb, i), S(pb_, i), S(sinb, i), R(ts)); |
| 308 | } |
| 309 | /* qksw = qk_scale for the q warps, 1.0f for warps 6, 7 (integer select on the bit patterns) */ |
| 310 | movi(e, ts, 0x3f800000u); imadi(e, R(ts), R(qks), 0xffffffffu, ts); imadr(e, qksw, f, ts, qks); |
| 311 | for (unsigned i = 0; i < 4; i++) { fmul(e, S(qa, i), S(qa, i), R(qksw)); fmul(e, S(qb, i), S(qb, i), R(qksw)); } |
| 312 | bar_sync(e); /* every partner read done before q' overwrites the staging */ |
| 313 | sts8(e, a0, one, t1, qa, qb); /* warps 6, 7 write scratch inside s[][] */ |
| 314 | if (spec->prep && !spec->multi) { |
| 315 | /* q' as bf16 in the attend kernel's A-fragment layout (omega_blackwell_codegen_pattn256.h, OMEGA_PATTN256_QF_*): |
| 316 | * word i of lane L (dims 8L+2i, +1) goes to ((((cx>>1)*4 + cy)*6 + warp)*16 + (L>>1))*64 + i*16 + ((L&1)*2 + (cx&1))*4. |
| 317 | * Warps 6, 7 (k, v) skip it. */ |
| 318 | int qw = V128(e), pqf = V64(e), qa64 = V64(e), h0q = V(e), h1q = V(e), tbq = V(e), fq = f; if (e->err) return -1; |
| 319 | if (H < 6u) { /* warps H .. 5 hold no head of this row: they skip q' too */ |
| 320 | fq = V(e); if (e->err) return -1; |
| 321 | movi(e, t1, H - 1u); imadi(e, R(t1), R(warp), 0xffffffffu, t1); shr(e, fq, t1, 31); |
| 322 | } |
| 323 | isetp_ge_u32(e, fq, one); |
| 324 | int bq = bra_if_p0_fwd(e); |
| 325 | for (unsigned w = 0; w < 4; w++) { |
| 326 | int src = w < 2 ? qa : qb; unsigned s0 = (2 * w) % 4; |
| 327 | bf16_of(e, h0q, S(src, s0), c7fff, tbq); bf16_of(e, h1q, S(src, s0 + 1), c7fff, tbq); imadi(e, S(qw, w), R(h1q), 65536u, h0q); |
| 328 | } |
| 329 | ldc64(e, pqf, OMEGA_DATTN256_ARG_OUT); |
| 330 | shr(e, t1, cx, 1); imadi(e, R(t1), R(t1), SH.vy, cy); imadi(e, R(t1), R(t1), H, warp); |
| 331 | shr(e, t2, lane, 1); imadi(e, R(t1), R(t1), 16u, t2); imadi(e, R(t1), R(t1), 64u, NOREG); |
| 332 | andi(e, R(t2), R(lane), 1u); imadi(e, R(t2), R(t2), 2u, NOREG); |
| 333 | { int cb = V(e); if (e->err) return -1; andi(e, R(cb), R(cx), 1u); iadd(e, t2, t2, cb); } |
| 334 | imadi(e, R(t1), R(t2), 4u, t1); |
| 335 | for (unsigned w = 0; w < 4; w++) { imadi(e, R(t2), R(one), 16u * w, t1); imadw(e, qa64, t2, 1u, pqf); stg(e, BW_IR_STG_E, qa64, S(qw, w), udesc); } |
| 336 | patch_bra(e, bq, here(e)); |
| 337 | } |
| 338 | |
| 339 | /* ---- the new token's K (warp 6: the rotated k) and V (warp 7: the raw v), bf16 into the cache by the CTA that owns `pos` ---- */ |
| 340 | if (MUT != 3 && !(spec->multi && !spec->prep)) { /* multi attend: the prep launch wrote every token's K/V */ |
| 341 | int sa64 = V64(e), sb64 = V64(e), key = V(e), h0 = V(e), h1 = V(e), tb = V(e); |
| 342 | /* key = (splits - 1 - cx) + 1 - f: zero only for warps 6, 7 of the last split */ |
| 343 | imadi(e, R(t2), R(cx), 0xffffffffu, splitsm1); |
| 344 | imadi(e, R(t2), R(one), 1u, t2); |
| 345 | if (spec->prep) imadi(e, R(key), R(f), 0xffffffffu, one); /* prep: every CTA owns its token: key = 1 - f */ |
| 346 | else imadi(e, R(key), R(f), 0xffffffffu, t2); |
| 347 | isetp_ge_u32(e, key, one); |
| 348 | int b1 = bra_if_p0_fwd(e); |
| 349 | /* cache row address of position pos for this lane: 8 bf16 = 16 bytes per lane; K plane, V plane for warp 7 */ |
| 350 | imadi(e, R(t1), R(pos), RB, cyk); imadi(e, R(t1), R(lane), 16u, t1); |
| 351 | imadw(e, sa64, t1, 1u, pkc); |
| 352 | movi(e, t2, 7u); imadi(e, R(t2), R(warp), 0xffffffffu, t2); /* 7 - warp: zero for warp 7 */ |
| 353 | isetp_ge_u32(e, t2, one); |
| 354 | { Region r = region_skip_if_p0(e); |
| 355 | imadw(e, sb64, voff, 1u, pqkv); |
| 356 | ldg_wide(e, BW_IR_LDG_E_128, qa, sb64, 0, udesc, 1); |
| 357 | ldg_wide(e, BW_IR_LDG_E_128, qb, sb64, 16, udesc, 0); |
| 358 | imadw(e, sa64, t1, 1u, pvc); |
| 359 | region_end(e, r); } |
| 360 | for (unsigned w = 0; w < 4; w++) { |
| 361 | int src = w < 2 ? qa : qb; unsigned s0 = (2 * w) % 4; |
| 362 | bf16_of(e, h0, S(src, s0), c7fff, tb); bf16_of(e, h1, S(src, s0 + 1), c7fff, tb); imadi(e, S(wa, w), R(h1), 65536u, h0); |
| 363 | } |
| 364 | for (unsigned w = 0; w < 4; w++) { imadw(e, sb64, one, 4u * w, sa64); stg(e, BW_IR_STG_E, sb64, S(wa, w), udesc); } |
| 365 | patch_bra(e, b1, here(e)); |
| 366 | } |
| 367 | membar(e); |
| 368 | if (spec->prep) { /* prep ends here: the K/V rows and q' are written */ |
| 369 | tail(e); |
| 370 | if (e->err) return -1; |
| 371 | return omega_bw_regalloc_solve(prog); |
| 372 | } |
| 373 | bar_sync(e); |
| 374 | |
| 375 | /* ---- running state ---- */ |
| 376 | int acc[6], m[6], l[6], sc[6], dl[6], wd[8], mn = V(e); |
| 377 | for (unsigned h = 0; h < H; h++) { acc[h] = V128(e); m[h] = V(e); l[h] = V(e); sc[h] = V(e); dl[h] = V(e); } |
| 378 | for (unsigned q = 0; q < 8; q++) wd[q] = V(e); |
| 379 | int kq[OMEGA_DATTN256_MAX_DEPTH], vq[OMEGA_DATTN256_MAX_DEPTH]; |
| 380 | for (uint32_t d = 0; d < D; d++) { kq[d] = V128(e); vq[d] = V64(e); } |
| 381 | int qa4 = V128(e), qb4 = V128(e); |
| 382 | 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); |
| 383 | int n = V(e), vrow = V64(e), vpf = V64(e), pa = V(e), i = V(e), dsum = V(e), hoff = V(e), wp = V(e), negwp = V(e), ccnt = V(e); |
| 384 | if (e->err) return -1; |
| 385 | for (unsigned h = 0; h < H; 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])); } |
| 386 | movi(e, smS, OMEGA_DATTN256_SM_S); |
| 387 | shr(e, wp, warp, 1); /* warp pair index 0..3 */ |
| 388 | imadi(e, R(negwp), R(wp), 0xffffffffu, NOREG); |
| 389 | andi(e, R(hoff), R(warp), 1u); |
| 390 | imadi(e, R(hoff), R(hoff), 256u, NOREG); /* byte offset of this warp's dim half in the row */ |
| 391 | imadi(e, R(hoff), R(lane), 8u, hoff); /* + 4 bf16 = 8 bytes per lane */ |
| 392 | if (MUT == 2) imadi(e, R(hoff), R(one), RB, hoff); |
| 393 | movi(e, ccnt, 32); |
| 394 | mov(e, R(cb), R(base)); |
| 395 | |
| 396 | /* ---- chunk loop ---- */ |
| 397 | const int L_chunk = here(e); |
| 398 | loop_begin(e); |
| 399 | isetp_ge_u32(e, cb, end); |
| 400 | const int bra_done = bra_if_p0_fwd(e); |
| 401 | /* score pass: thread = position cb + tid, clamped to end - 1 (clamped lanes compute a score nobody reads) */ |
| 402 | imadi(e, R(tpos), R(cb), 1u, tid); |
| 403 | imadi(e, R(over), R(tpos), 0xffffffffu, endm1); /* endm1 - tpos */ |
| 404 | shr(e, over, over, 31); /* 1 when tpos > endm1 */ |
| 405 | imadi(e, R(t1), R(tpos), 1u, negendm1); /* tpos - endm1 */ |
| 406 | imadr(e, t1, over, t1, NOREG); |
| 407 | imadi(e, R(t1), R(t1), 0xffffffffu, tpos); /* clamped position */ |
| 408 | imadi(e, R(t1), R(t1), RB, cyk); |
| 409 | imadw(e, krow, t1, 1u, pkc); |
| 410 | for (unsigned h = 0; h < H; h++) movrz(e, R(sc[h])); |
| 411 | for (uint32_t d = 0; d < D; d++) ldg_wide(e, BW_IR_LDG_E_128, kq[d], krow, d * 16u, udesc, d == 0); |
| 412 | imadw(e, kpf, one, D * 16u, krow); |
| 413 | movrz(e, R(qoff)); movrz(e, R(j)); |
| 414 | const int L_q = here(e); |
| 415 | loop_begin(e); |
| 416 | isetp_ge_u32(e, j, ccnt); |
| 417 | const int bra_qend = bra_if_p0_fwd(e); |
| 418 | 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); } |
| 419 | for (unsigned h = 0; h < H; h++) { |
| 420 | lds128(e, qa4, qoff, h * 1024u, 1); lds128(e, qb4, qoff, h * 1024u + 16u, 0); |
| 421 | for (unsigned q = 0; q < 4; q++) ffma(e, R(sc[h]), R(wd[q]), S(qa4, q), R(sc[h])); |
| 422 | for (unsigned q = 0; q < 4; q++) ffma(e, R(sc[h]), R(wd[4 + q]), S(qb4, q), R(sc[h])); |
| 423 | } |
| 424 | 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)); |
| 425 | ldg_wide(e, BW_IR_LDG_E_128, kq[D - 1], kpf, 0, udesc, 1); |
| 426 | imadw(e, kpf, one, 16u, kpf); |
| 427 | imadi(e, R(qoff), R(one), 32u, qoff); |
| 428 | imadi(e, R(j), R(one), 1u, j); |
| 429 | bra(e, L_q); |
| 430 | loop_end(e); |
| 431 | patch_bra(e, bra_qend, here(e)); |
| 432 | imadi(e, R(sa), R(tid), 32u, smS); |
| 433 | for (unsigned h = 0; h < H; h++) { imadi(e, R(t1), R(one), h * 4u, sa); sts32(e, t1, R(sc[h])); } |
| 434 | bar_sync(e); |
| 435 | /* V pass: this warp's positions cb + wp + 4 i, i < n = max(0, ceil((min(end, cb + 256) - cb - wp) / 4)) */ |
| 436 | imadi(e, R(t1), R(one), OMEGA_DATTN256_CHUNK, cb); /* cb + 256 */ |
| 437 | imadi(e, R(t2), R(t1), 0xffffffffu, end); /* end - (cb + 256) */ |
| 438 | shr(e, over, t2, 31); /* 1 when the chunk is cut short by end */ |
| 439 | imadr(e, t2, over, t2, t1); /* cend = min(end, cb + 256) */ |
| 440 | imadi(e, R(t1), R(cb), 0xffffffffu, t2); /* cend - cb */ |
| 441 | imadi(e, R(t1), R(t1), 1u, negwp); /* x = cend - cb - wp */ |
| 442 | shr(e, over, t1, 31); |
| 443 | imadi(e, R(n), R(one), 3u, t1); |
| 444 | shr(e, n, n, 2); |
| 445 | imadr(e, t2, over, n, NOREG); |
| 446 | imadi(e, R(n), R(t2), 0xffffffffu, n); |
| 447 | imadi(e, R(t1), R(cb), 1u, wp); |
| 448 | imadi(e, R(t1), R(t1), RB, hoff); |
| 449 | imadi(e, R(t1), R(t1), 1u, cyk); |
| 450 | imadw(e, vrow, t1, 1u, pvc); |
| 451 | for (uint32_t d = 0; d < D; d++) ldg_wide(e, BW_IR_LDG_E_64, vq[d], vrow, d * 4u * RB, udesc, d == 0); |
| 452 | imadw(e, vpf, one, D * 4u * RB, vrow); |
| 453 | imadi(e, R(pa), R(wp), 32u, smS); |
| 454 | movrz(e, R(i)); |
| 455 | const int L_v = here(e); |
| 456 | loop_begin(e); |
| 457 | isetp_ge_u32(e, i, n); |
| 458 | const int bra_vend = bra_if_p0_fwd(e); |
| 459 | imadi(e, R(wd[0]), S(vq[0], 0), 65536u, NOREG); andi(e, R(wd[1]), S(vq[0], 0), 0xffff0000u); |
| 460 | imadi(e, R(wd[2]), S(vq[0], 1), 65536u, NOREG); andi(e, R(wd[3]), S(vq[0], 1), 0xffff0000u); |
| 461 | lds128(e, qa4, pa, 0, 1); lds128(e, qb4, pa, 16, 0); |
| 462 | #define SH(h) ((h) < 4 ? S(qa4, (h)) : S(qb4, (h) - 4)) |
| 463 | for (unsigned h = 0; h < H; h++) { fmaxx(e, R(mn), R(m[h]), SH(h)); 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 */ |
| 464 | fadd(e, R(dsum), R(dl[0]), R(dl[1])); |
| 465 | for (unsigned h = 2; h < H; h++) fadd(e, R(dsum), R(dsum), R(dl[h])); |
| 466 | isetp_ge_u32(e, one, dsum); /* 1 >= bits(dsum): no head's max moved (dsum is +0); a negative dsum has the sign bit */ |
| 467 | const int bra_nores = bra_if_p0_fwd(e); |
| 468 | for (unsigned h = 0; h < H; h++) mufu(e, BW_IR_MUFU_EX2, R(dl[h]), R(dl[h])); |
| 469 | for (unsigned h = 0; h < H; 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])); } |
| 470 | patch_bra(e, bra_nores, here(e)); |
| 471 | for (unsigned h = 0; h < H; h++) { fsub(e, R(dl[h]), SH(h), R(m[h])); mufu(e, BW_IR_MUFU_EX2, R(dl[h]), R(dl[h])); } |
| 472 | #undef SH |
| 473 | for (unsigned h = 0; h < H; 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])); } |
| 474 | 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)); |
| 475 | ldg_wide(e, BW_IR_LDG_E_64, vq[D - 1], vpf, 0, udesc, 1); |
| 476 | imadw(e, vpf, one, 4u * RB, vpf); |
| 477 | imadi(e, R(pa), R(one), 4u * 32u, pa); |
| 478 | imadi(e, R(i), R(one), 1u, i); |
| 479 | bra(e, L_v); |
| 480 | loop_end(e); |
| 481 | patch_bra(e, bra_vend, here(e)); |
| 482 | bar_sync(e); /* the next score pass overwrites s[][] */ |
| 483 | imadi(e, R(cb), R(one), OMEGA_DATTN256_CHUNK, cb); |
| 484 | bra(e, L_chunk); |
| 485 | loop_end(e); |
| 486 | patch_bra(e, bra_done, here(e)); |
| 487 | |
| 488 | /* ---- epilogue: combine the warps, write the partial ---- */ |
| 489 | int ep[8], ea[8], eb[8]; |
| 490 | for (unsigned h = 0; h < 8; h++) { ep[h] = V(e); ea[h] = V(e); eb[h] = V(e); } |
| 491 | int pout = V64(e), ppart = V64(e), pcnt = V64(e), oscale = V(e); |
| 492 | if (e->err) return -1; |
| 493 | ldc64(e, pout, OMEGA_DATTN256_ARG_OUT); ldc64(e, ppart, OMEGA_DATTN256_ARG_PART); ldc64(e, pcnt, OMEGA_DATTN256_ARG_CNT); ldc32(e, oscale, OMEGA_DATTN256_ARG_OUT_SCALE); |
| 494 | if (spec->multi) { /* the token's output row, partials block and counters (ctaid.y re-read: no live register) */ |
| 495 | int tk = V(e), so = V(e); if (e->err) return -1; |
| 496 | uint32_t lg = 0; while ((1u << lg) < SH.vy) lg++; |
| 497 | s2r(e, tk, BW_SR_CTAID_Y); shr(e, tk, tk, lg); |
| 498 | ldc32(e, so, OMEGA_DATTN256_ARG_OSTRIDE); imadr(e, so, tk, so, NOREG); imadw(e, pout, so, 1u, pout); |
| 499 | ldc32(e, so, OMEGA_DATTN256_ARG_PSTRIDE); imadr(e, so, tk, so, NOREG); imadw(e, ppart, so, 1u, ppart); |
| 500 | imadi(e, R(so), R(tk), SH.vy * 4u, NOREG); imadw(e, pcnt, so, 1u, pcnt); |
| 501 | } |
| 502 | /* per-warp maxes to shared: lane 0 of warp w writes m[h] at SM_M + w*32 + h*4 */ |
| 503 | imadi(e, R(a0), R(warp), 32u, NOREG); imadi(e, R(a0), R(one), OMEGA_DATTN256_SM_M, a0); |
| 504 | isetp_ge_u32(e, lane, one); |
| 505 | { Region r = region_skip_if_p0(e); for (unsigned h = 0; h < H; h++) { imadi(e, R(t1), R(one), h * 4u, a0); sts32(e, t1, R(m[h])); } region_end(e, r); } |
| 506 | bar_sync(e); |
| 507 | /* lanes 0..5 of every warp: M[lane] = max over the 8 warps; lanes of warp 0 write it at SM_M + 256 + lane*4 */ |
| 508 | movi(e, ccnt, H); |
| 509 | isetp_ge_u32(e, lane, ccnt); |
| 510 | { |
| 511 | Region r = region_skip_if_p0(e); |
| 512 | imadi(e, R(a1), R(lane), 4u, NOREG); imadi(e, R(a1), R(one), OMEGA_DATTN256_SM_M, a1); |
| 513 | for (unsigned w = 0; w < 8; w++) { imadi(e, R(t1), R(one), w * 32u, a1); lds32(e, R(ep[w]), t1, w == 0); } |
| 514 | for (unsigned w = 1; w < 8; w++) fmaxx(e, R(ep[0]), R(ep[0]), R(ep[w])); |
| 515 | imadi(e, R(t1), R(one), 256u, a1); |
| 516 | sts32(e, t1, R(ep[0])); |
| 517 | region_end(e, r); |
| 518 | } |
| 519 | bar_sync(e); |
| 520 | movi(e, t1, OMEGA_DATTN256_SM_M + 256u); |
| 521 | lds128(e, qa4, t1, 0, 1); lds128(e, qb4, t1, 16, 0); |
| 522 | for (unsigned h = 0; h < H; 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])); } |
| 523 | for (unsigned h = 0; h < H; 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])); } |
| 524 | /* per-warp l to shared, then the partial's m and l by threads 0..5 (l summed over the even warps: the odd ones repeat them) */ |
| 525 | imadi(e, R(a0), R(warp), 32u, NOREG); imadi(e, R(a0), R(one), OMEGA_DATTN256_SM_L, a0); |
| 526 | isetp_ge_u32(e, lane, one); |
| 527 | { Region r = region_skip_if_p0(e); for (unsigned h = 0; h < H; h++) { imadi(e, R(t1), R(one), h * 4u, a0); sts32(e, t1, R(l[h])); } region_end(e, r); } |
| 528 | bar_sync(e); |
| 529 | int pp = V64(e), pa64 = V64(e), pb64 = V64(e); |
| 530 | imadr(e, t2, cy, splits, cx); |
| 531 | imadi(e, R(t2), R(t2), PBYTES, NOREG); |
| 532 | imadw(e, pp, t2, 1u, ppart); |
| 533 | isetp_ge_u32(e, tid, ccnt); /* tid >= 6 skips */ |
| 534 | { |
| 535 | Region r = region_skip_if_p0(e); |
| 536 | imadi(e, R(a1), R(tid), 4u, NOREG); imadi(e, R(a1), R(one), OMEGA_DATTN256_SM_L, a1); |
| 537 | for (unsigned w = 0; w < 4; w++) { imadi(e, R(t1), R(one), w * 64u, a1); lds32(e, R(ep[w]), t1, w == 0); } |
| 538 | for (unsigned w = 1; w < 4; w++) fadd(e, R(ep[0]), R(ep[0]), R(ep[w])); |
| 539 | imadi(e, R(t1), R(tid), 4u, NOREG); imadi(e, R(t1), R(one), OMEGA_DATTN256_SM_M + 256u, t1); |
| 540 | lds32(e, R(ep[1]), t1, 1); |
| 541 | imadw(e, pa64, tid, 4u, pp); |
| 542 | stg(e, BW_IR_STG_E, pa64, R(ep[1]), udesc); /* m[h] */ |
| 543 | imadw(e, pb64, one, 4u * H, pa64); |
| 544 | stg(e, BW_IR_STG_E, pb64, R(ep[0]), udesc); /* l[h] */ |
| 545 | region_end(e, r); |
| 546 | } |
| 547 | /* acc: 6 rounds of one head through the s[][] region as r[w][256]: warp w writes its dim half, thread tid sums the |
| 548 | * 4 warps that hold dim tid (warps (tid >> 7) + 2k); the store and load addresses computed once */ |
| 549 | imadi(e, R(a0), R(warp), 1024u, NOREG); imadi(e, R(a0), R(lane), 16u, a0); |
| 550 | andi(e, R(t1), R(warp), 1u); imadi(e, R(a0), R(t1), 512u, a0); imadi(e, R(a0), R(one), OMEGA_DATTN256_SM_S, a0); |
| 551 | shr(e, t1, tid, 7); |
| 552 | if (MUT == 5) movrz(e, R(t1)); |
| 553 | imadi(e, R(a1), R(t1), 1024u, NOREG); imadi(e, R(a1), R(tid), 4u, a1); imadi(e, R(a1), R(one), OMEGA_DATTN256_SM_S, a1); |
| 554 | for (unsigned q = 0; q < 4; q++) imadi(e, R(eb[q]), R(one), q * 4u, a0); |
| 555 | for (unsigned w = 0; w < 4; w++) imadi(e, R(ea[w]), R(one), w * 2048u, a1); |
| 556 | imadw(e, pa64, tid, 4u, pp); |
| 557 | for (unsigned rd = 0; rd < H; rd++) { |
| 558 | for (unsigned q = 0; q < 4; q++) sts32(e, eb[q], S(acc[rd], q)); |
| 559 | bar_sync(e); |
| 560 | for (unsigned w = 0; w < 4; w++) lds32(e, R(ep[w]), ea[w], w == 0); |
| 561 | for (unsigned w = 1; w < 4; w++) fadd(e, R(ep[0]), R(ep[0]), R(ep[w])); |
| 562 | imadw(e, pb64, one, 8u * H + rd * 1024u, pa64); |
| 563 | stg(e, BW_IR_STG_E, pb64, R(ep[0]), udesc); |
| 564 | bar_sync(e); |
| 565 | } |
| 566 | /* publish, count, the last CTA combines */ |
| 567 | membar(e); |
| 568 | bar_sync(e); |
| 569 | movi(e, t1, OMEGA_DATTN256_SM_FLAG); |
| 570 | isetp_ge_u32(e, tid, one); |
| 571 | { |
| 572 | Region r = region_skip_if_p0(e); |
| 573 | imadw(e, pb64, cy, 4u, pcnt); |
| 574 | atom_add(e, ep[0], pb64, one, udesc); |
| 575 | sts32(e, t1, R(ep[0])); |
| 576 | region_end(e, r); |
| 577 | } |
| 578 | bar_sync(e); |
| 579 | lds32(e, R(ep[0]), t1, 1); |
| 580 | isetp_ge_u32(e, ep[0], splitsm1); |
| 581 | const int bra_last = bra_if_p0_fwd(e); |
| 582 | exit_k(e); |
| 583 | patch_bra(e, bra_last, here(e)); |
| 584 | /* combine: thread tid, outputs o = tid + 256 r (head r, dim tid), r = 0..5 */ |
| 585 | 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); |
| 586 | if (e->err) return -1; |
| 587 | movrz(e, R(ho)); |
| 588 | mov(e, R(oidx), R(tid)); |
| 589 | movrz(e, R(rr)); |
| 590 | movi(e, ccnt, H); |
| 591 | imadr(e, t2, cy, splits, NOREG); |
| 592 | imadi(e, R(t2), R(t2), PBYTES, NOREG); /* byte offset of partials[cy][0] */ |
| 593 | const int L_r = here(e); |
| 594 | loop_begin(e); |
| 595 | isetp_ge_u32(e, rr, ccnt); |
| 596 | const int bra_rend = bra_if_p0_fwd(e); |
| 597 | imadi(e, R(t1), R(ho), 4u, t2); imadw(e, ma, t1, 1u, ppart); |
| 598 | imadw(e, la, one, 4u * H, ma); |
| 599 | imadi(e, R(t1), R(oidx), 4u, t2); imadw(e, aa, t1, 1u, ppart); imadw(e, aa, one, 8u * H, aa); |
| 600 | movrz(e, R(num)); movrz(e, R(den)); movi(e, mx, NEG_BIG); movrz(e, R(s_i)); |
| 601 | const int L_s = here(e); |
| 602 | loop_begin(e); |
| 603 | isetp_ge_u32(e, s_i, splits); |
| 604 | const int bra_send = bra_if_p0_fwd(e); |
| 605 | ldg32(e, BW_IR_LDG_STRONG_SYS, ep[0], ma, udesc, 1); |
| 606 | ldg32(e, BW_IR_LDG_STRONG_SYS, ep[1], la, udesc, 0); |
| 607 | ldg32(e, BW_IR_LDG_STRONG_SYS, ep[2], aa, udesc, 0); |
| 608 | if (MUT != 4) { |
| 609 | fmaxx(e, R(ep[3]), R(mx), R(ep[0])); |
| 610 | fsub(e, R(ep[4]), R(mx), R(ep[3])); mufu(e, BW_IR_MUFU_EX2, R(ep[4]), R(ep[4])); |
| 611 | fsub(e, R(ep[5]), R(ep[0]), R(ep[3])); mufu(e, BW_IR_MUFU_EX2, R(ep[5]), R(ep[5])); |
| 612 | mov(e, R(mx), R(ep[3])); |
| 613 | fmul(e, R(num), R(num), R(ep[4])); fmul(e, R(den), R(den), R(ep[4])); |
| 614 | } else { |
| 615 | movi(e, ep[5], 0x3f800000u); |
| 616 | } |
| 617 | ffma(e, R(num), R(ep[5]), R(ep[2]), R(num)); |
| 618 | ffma(e, R(den), R(ep[5]), R(ep[1]), R(den)); |
| 619 | imadw(e, ma, one, PBYTES, ma); |
| 620 | imadw(e, la, one, PBYTES, la); |
| 621 | imadw(e, aa, one, PBYTES, aa); |
| 622 | imadi(e, R(s_i), R(one), 1u, s_i); |
| 623 | bra(e, L_s); |
| 624 | loop_end(e); |
| 625 | patch_bra(e, bra_send, here(e)); |
| 626 | mufu(e, BW_IR_MUFU_RCP, R(den), R(den)); |
| 627 | fmul(e, R(num), R(num), R(den)); |
| 628 | fmul(e, R(num), R(num), R(oscale)); |
| 629 | imadi(e, R(t1), R(cy), H * 256u, oidx); |
| 630 | imadw(e, pb64, t1, 4u, pout); |
| 631 | stg(e, BW_IR_STG_E, pb64, R(num), udesc); |
| 632 | imadi(e, R(ho), R(one), 1u, ho); |
| 633 | imadi(e, R(oidx), R(one), 256u, oidx); |
| 634 | imadi(e, R(rr), R(one), 1u, rr); |
| 635 | bra(e, L_r); |
| 636 | loop_end(e); |
| 637 | patch_bra(e, bra_rend, here(e)); |
| 638 | /* thread 0 resets the counter for the next launch (every other CTA of this head has already added) */ |
| 639 | isetp_ge_u32(e, tid, one); |
| 640 | { 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); } |
| 641 | tail(e); |
| 642 | if (e->err) return -1; |
| 643 | return omega_bw_regalloc_solve(prog); |
| 644 | } |
| 645 | |
| 646 | #else /* default: first-party seeded Omega; C above is differential-only */ |
| 647 | #include "omega_codegen_rest_wire.h" |
| 648 | int omega_dattn256_shape(const OmegaDecodeAttn256Spec *spec, OmegaDattn256Shape *s) { |
| 649 | uint64_t meta[OMEGA_OM_IR_META] = {0}; |
| 650 | if (omega_rest_query(omega_rest_attn256_wire, 0, spec, s, meta) != 0) return -1; |
| 651 | if (meta[48] == 0 && s) { |
| 652 | s->qh = (uint32_t)meta[32]; |
| 653 | s->kvh = (uint32_t)meta[33]; |
| 654 | s->h = (uint32_t)meta[34]; |
| 655 | s->vy = (uint32_t)meta[35]; |
| 656 | s->g = (uint32_t)meta[36]; |
| 657 | s->qkv_words = (uint32_t)meta[37]; |
| 658 | s->out_words = (uint32_t)meta[38]; |
| 659 | } |
| 660 | return (int)(int64_t)meta[48]; |
| 661 | } |
| 662 | int omega_blackwell_codegen_decode_attn256_prog(const OmegaDecodeAttn256Spec *spec, BlackwellIRProgram *prog) { |
| 663 | return omega_gpu_om_ir_replay(omega_rest_attn256_wire, 1, (uintptr_t)spec, 0, 0, prog, NULL) == 0 ? 0 : -1; |
| 664 | } |
| 665 | #endif /* CUDAPP_MIGRATION_PENDING_C_CODEGEN_ATTN256 */ |
| 666 | |
| 667 | int omega_decode_attn256_prog_stats(const OmegaDecodeAttn256Spec *spec, uint32_t *insns, uint32_t *gprs) { |
| 668 | static BlackwellIRProgram prog; |
| 669 | if (omega_blackwell_codegen_decode_attn256_prog(spec, &prog) != 0) return -1; |
| 670 | if (insns) *insns = (uint32_t)prog.count; |
| 671 | if (gprs) *gprs = prog.regalloc.peak_gpr_usage; |
| 672 | return 0; |
| 673 | } |