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_pattn.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* |
| 3 | * omega_blackwell_codegen_pattn.c: causal prefill attention on tensor cores (see the header). |
| 4 | * Emitter rules are those of omega_blackwell_codegen_attn.c / omega_blackwell_codegen_tc.c (control words, the read |
| 5 | * barrier on every load, held scoreboards 3/4 for prefetch loads, loop live ranges, HMMA spacing: >= 4 independent |
| 6 | * HMMAs at stall 8 between dependent ones, stall 15 + NOP 14 before a non-HMMA reader of an HMMA result). |
| 7 | * The prologue's per-head RMSNorm + RoPE is the decode kernel's instruction sequence (omega_blackwell_codegen_attn.c, |
| 8 | * prologue), so the appended K rows match the decode kernel bit for bit on the same chip. |
| 9 | */ |
| 10 | #include "omega_blackwell_codegen_pattn.h" |
| 11 | #include "omega_blackwell_codegen_tc.h" |
| 12 | #include <string.h> |
| 13 | #include "omega_gpu_om_ir.h" |
| 14 | #include "cudapp_native.h" |
| 15 | |
| 16 | int omega_pattn_spec_ok(const OmegaPrefillAttnSpec *s) { |
| 17 | return s && (s->warps == 4u || s->warps == 8u) && s->mutant >= 0 && s->mutant <= 4 && |
| 18 | s->mode <= OMEGA_PATTN_MODE_ATTEND && |
| 19 | (s->mirror == 0 || (s->mirror == 1 && s->mode == OMEGA_PATTN_MODE_APPEND)); |
| 20 | } |
| 21 | |
| 22 | #if defined(CUDAPP_MIGRATION_PENDING_C_CODEGEN_PATTN) |
| 23 | |
| 24 | #define CW(stall, wbar, rbar, wait) \ |
| 25 | (((uint32_t)(stall) << 9) | ((uint32_t)(wbar) << 14) | ((uint32_t)(rbar) << 17) | ((uint32_t)(wait) << 20)) |
| 26 | #define CW_YIELD 0x2000u |
| 27 | #define SB_NONE 7u |
| 28 | #define SB_GPR 0u |
| 29 | #define SB_RD 1u |
| 30 | #define SB_UGPR 2u |
| 31 | #define NOREG (-1) |
| 32 | #define MAX_LOOPS 4 |
| 33 | |
| 34 | enum { K_FIXED, K_PRED, K_VAR, K_VAR_MORE, K_UVAR, K_STORE, K_SYNC, K_EXIT, K_BRA, K_BSSY, K_BSYNC }; |
| 35 | |
| 36 | typedef struct { |
| 37 | BlackwellIRProgram *p; |
| 38 | uint32_t pending, held; |
| 39 | int err; |
| 40 | int open_start[MAX_LOOPS]; |
| 41 | int depth; |
| 42 | } Em; |
| 43 | |
| 44 | static void emit(Em *e, BlackwellIRInsn in, int kind) { |
| 45 | uint32_t wait = e->pending; |
| 46 | switch (kind) { |
| 47 | case K_FIXED: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; |
| 48 | case K_PRED: in.control = CW(14, SB_NONE, SB_NONE, wait); e->pending = 0; break; |
| 49 | case K_VAR: in.control = CW(4, SB_GPR, SB_RD, wait) | CW_YIELD; e->pending = (1u << SB_GPR) | (1u << SB_RD); break; |
| 50 | 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; |
| 51 | case K_UVAR: in.control = CW(4, SB_UGPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_UGPR; break; |
| 52 | case K_STORE: in.control = CW(4, SB_NONE, SB_RD, wait) | CW_YIELD; e->pending = 1u << SB_RD; break; |
| 53 | case K_SYNC: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; |
| 54 | case K_EXIT: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; |
| 55 | case K_BRA: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; |
| 56 | case K_BSSY: in.control = CW(1, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; |
| 57 | case K_BSYNC: in.control = CW(5, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; |
| 58 | default: e->err = -1; return; |
| 59 | } |
| 60 | if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; |
| 61 | } |
| 62 | |
| 63 | static BlackwellIRInsn I0(BlackwellIROpcode op) { |
| 64 | BlackwellIRInsn i; memset(&i, 0, sizeof i); |
| 65 | i.op = op; i.dst_vreg = NOREG; i.src1_vreg = NOREG; i.src2_vreg = NOREG; i.src3_vreg = NOREG; i.ureg = NOREG; |
| 66 | return i; |
| 67 | } |
| 68 | static int V(Em *e) { int v = omega_bw_ir_alloc_vreg(e->p); if (v < 0) e->err = -1; return v; } |
| 69 | static int V64(Em *e) { int v = omega_bw_ir_alloc_vreg64(e->p); if (v < 0) e->err = -1; return v; } |
| 70 | static int V128(Em *e) { int v = omega_bw_ir_alloc_vreg128(e->p); if (v < 0) e->err = -1; return v; } |
| 71 | static int UV64(Em *e) { int v = omega_bw_ir_alloc_uvreg64(e->p); if (v < 0) e->err = -1; return v; } |
| 72 | static int here(const Em *e) { return (int)e->p->count; } |
| 73 | |
| 74 | typedef struct { int v; unsigned s; } Op; |
| 75 | static Op R(int v) { Op o = { v, 0 }; return o; } |
| 76 | static Op S(int v, unsigned s) { Op o = { v, s }; return o; } |
| 77 | |
| 78 | 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); } |
| 79 | 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); } |
| 80 | 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); } |
| 81 | 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); } |
| 82 | static void movi(Em *e, Op d, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_MOV_IMM); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.imm = imm; emit(e, i, K_FIXED); } |
| 83 | 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); } |
| 84 | /* d = a * imm + c */ |
| 85 | 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); } |
| 86 | /* d = a * b + c (registers) */ |
| 87 | 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); } |
| 88 | /* d64 = a * imm + c64 */ |
| 89 | 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); } |
| 90 | static void mov(Em *e, Op d, Op a) { imadi(e, d, a, 1u, NOREG); } |
| 91 | 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); } |
| 92 | static void shr(Em *e, int d, int a, uint32_t imm) { shrs(e, d, R(a), imm); } |
| 93 | 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); } |
| 94 | 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); } |
| 95 | static void fop(Em *e, BlackwellIROpcode op, Op d, Op a, Op b, Op c) { |
| 96 | 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; |
| 97 | 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); |
| 98 | } |
| 99 | static void iadd(Em *e, int d, int a, int b) { fop(e, BW_IR_IADD3, R(d), R(a), R(b), R(NOREG)); } |
| 100 | static void iaddo(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_IADD3, d, a, b, R(NOREG)); } |
| 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, int first) { 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, first ? K_VAR : K_VAR_MORE); } |
| 107 | static void ldg_wide(Em *e, BlackwellIROpcode op, int d, int addr64, uint32_t imm, int udesc, int first) { |
| 108 | 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); |
| 109 | } |
| 110 | static void stg(Em *e, int addr64, uint32_t imm, Op val, int udesc) { BlackwellIRInsn i = I0(BW_IR_STG_E); i.src1_vreg = addr64; i.imm = imm; i.src2_vreg = val.v; i.src2_subreg = (uint8_t)val.s; i.ureg = udesc; emit(e, i, K_STORE); } |
| 111 | static void membar(Em *e) { emit(e, I0(BW_IR_MEMBAR_ALL_SYS), K_FIXED); } |
| 112 | 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); } |
| 113 | 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); } |
| 114 | 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); } |
| 115 | static void bar_sync(Em *e) { emit(e, I0(BW_IR_BAR_SYNC), K_SYNC); } |
| 116 | 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); } |
| 117 | static void shfl_bfly(Em *e, int d, int a, uint32_t lanes) { BlackwellIRInsn i = I0(BW_IR_SHFL_BFLY); i.dst_vreg = d; i.src1_vreg = a; i.imm = lanes & 31u; emit(e, i, K_VAR); } |
| 118 | /* F2FP.BF16.F32.PACK_AB: d = bf16_rn(hi) << 16 | bf16_rn(lo) */ |
| 119 | static void f2fp(Em *e, Op d, Op hi, Op lo) { |
| 120 | BlackwellIRInsn i = I0(BW_IR_F2FP_BF16_PACK); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = hi.v; i.src1_subreg = (uint8_t)hi.s; |
| 121 | i.src2_vreg = lo.v; i.src2_subreg = (uint8_t)lo.s; emit(e, i, K_FIXED); |
| 122 | } |
| 123 | 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); } |
| 124 | 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); } |
| 125 | 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; } |
| 126 | static void patch_bra(Em *e, int at, int target) { if (!e->err) e->p->insns[at].imm = (uint32_t)(target - at); } |
| 127 | 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; } |
| 128 | static void bsync(Em *e) { BlackwellIRInsn i = I0(BW_IR_BSYNC); i.bar_reg = 0; emit(e, i, K_BSYNC); } |
| 129 | static void tail(Em *e) { |
| 130 | emit(e, I0(BW_IR_EXIT), K_EXIT); |
| 131 | BlackwellIRInsn self = I0(BW_IR_BRA); |
| 132 | self.control = 0x000fc000u; |
| 133 | if (omega_bw_ir_append(e->p, &self) < 0) e->err = -1; |
| 134 | } |
| 135 | static void loop_begin(Em *e) { if (e->depth >= MAX_LOOPS) { e->err = -1; return; } e->open_start[e->depth++] = here(e); } |
| 136 | static void loop_end(Em *e) { |
| 137 | if (e->depth <= 0) { e->err = -1; return; } |
| 138 | int start = e->open_start[--e->depth], end = here(e) - 1; |
| 139 | OmegaRegAlloc *ra = &e->p->regalloc; |
| 140 | for (int v = 0; v < ra->num_vregs; v++) { |
| 141 | OmegaLiveInterval *iv = &ra->intervals[v]; |
| 142 | if (iv->first_def >= 0 && iv->first_def < start && iv->last_use >= start && iv->last_use < end) iv->last_use = end; |
| 143 | } |
| 144 | } |
| 145 | typedef struct { int bssy, bra; } Region; |
| 146 | static Region region_skip_if_p0(Em *e) { Region r; r.bssy = bssy_fwd(e); r.bra = bra_if_p0_fwd(e); return r; } |
| 147 | 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); } |
| 148 | |
| 149 | /* HMMA.16816.F32.BF16 d = a * b + c; b is the register pair at b + bsub (0 or 2); c NOREG = RZ */ |
| 150 | static void hmma(Em *e, int d, int a, int b, unsigned bsub, int c, uint32_t stall) { |
| 151 | BlackwellIRInsn i = I0(BW_IR_HMMA_BF16); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src2_subreg = (uint8_t)bsub; i.src3_vreg = c; |
| 152 | i.control = CW(stall, SB_NONE, SB_NONE, e->pending); e->pending = 0; |
| 153 | if (omega_bw_ir_append(e->p, &i) < 0) e->err = -1; |
| 154 | } |
| 155 | static void nop(Em *e, uint32_t stall) { |
| 156 | BlackwellIRInsn i = I0(BW_IR_NOP); i.control = CW(stall, SB_NONE, SB_NONE, e->pending); e->pending = 0; |
| 157 | if (omega_bw_ir_append(e->p, &i) < 0) e->err = -1; |
| 158 | } |
| 159 | /* a prefetch LDS.128: scoreboard sb (3 or 4) is held, not waited for, until consume() */ |
| 160 | static void lds_pf(Em *e, int d, int addr, uint32_t imm, unsigned sb) { |
| 161 | BlackwellIRInsn in = I0(BW_IR_LDS128); in.dst_vreg = d; in.src1_vreg = addr; in.imm = imm; |
| 162 | in.control = CW(4, sb, SB_RD, (e->pending & (1u << SB_RD))) | CW_YIELD; |
| 163 | e->pending |= 1u << SB_RD; e->held |= 1u << sb; |
| 164 | if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; |
| 165 | } |
| 166 | static void consume(Em *e, unsigned sb) { e->pending |= e->held & (1u << sb); e->held &= ~(1u << sb); } |
| 167 | |
| 168 | /* bf16 round-to-nearest-even of f32 x into the low 16 bits of d (the decode kernel's sequence) */ |
| 169 | static void bf16_of(Em *e, int d, Op x, int c7fff, int t) { |
| 170 | shrs(e, t, x, 16); |
| 171 | andi(e, R(t), R(t), 1u); |
| 172 | iadd(e, t, t, c7fff); |
| 173 | fop(e, BW_IR_IADD3, R(d), x, R(t), R(NOREG)); |
| 174 | shr(e, d, d, 16); |
| 175 | } |
| 176 | /* two bf16 of x[q0], x[q0+1] packed (element q0 low) */ |
| 177 | static void bf16_pair(Em *e, Op d, int x4, unsigned q0, int c7fff, int h0, int h1, int tb) { |
| 178 | bf16_of(e, h0, S(x4, q0), c7fff, tb); bf16_of(e, h1, S(x4, q0 + 1), c7fff, tb); imadi(e, d, R(h1), 65536u, h0); |
| 179 | } |
| 180 | |
| 181 | /* shared memory map */ |
| 182 | #define SM_K 0u /* main loop: K block, [s 8][jp 2][lane 32][16 B] */ |
| 183 | #define SM_V 8192u /* main loop: V block, [kq 2][np 8][lane 32][16 B] */ |
| 184 | #define SM_Q 0u /* prologue: Q fragments of 2 tiles, [tile 2][s 8][lane 32][16 B] */ |
| 185 | #define SM_X 8192u /* prologue: per-warp f32 [128] partner staging */ |
| 186 | #define SM_N 12288u /* prologue: per-warp sum of squares */ |
| 187 | static const uint32_t NEG_BIG = 0xF149F2CAu; /* -1e30f */ |
| 188 | |
| 189 | /* The decode kernel's RMSNorm + RoPE of the quad x4 (dims 4*lane .. +3) of one head, in place. sin4 carries the |
| 190 | * sign already (lanes < 16 negative). Two CTA barriers (every warp runs the same number of items). */ |
| 191 | typedef struct { int lane, one, eps, snw, sxl, sxp, sq, ts, rs, a1, pt4; } NR; |
| 192 | static void norm_rope(Em *e, const NR *n, int x4, int w4, int cos4, int sin4, int rope) { |
| 193 | fmul(e, R(n->sq), S(x4, 0), S(x4, 0)); |
| 194 | for (unsigned i = 1; i < 4; i++) ffma(e, R(n->sq), S(x4, i), S(x4, i), R(n->sq)); |
| 195 | for (uint32_t o = 16; o >= 1; o /= 2) { shfl_down(e, n->ts, n->sq, o); fadd(e, R(n->sq), R(n->sq), R(n->ts)); } |
| 196 | isetp_ge_u32(e, n->lane, n->one); |
| 197 | { Region r = region_skip_if_p0(e); sts32(e, n->snw, R(n->sq)); region_end(e, r); } |
| 198 | bar_sync(e); |
| 199 | lds32(e, R(n->sq), n->snw, 1); |
| 200 | movi(e, R(n->ts), 0x3c000000u); /* 1/128 */ |
| 201 | ffma(e, R(n->rs), R(n->sq), R(n->ts), R(n->eps)); |
| 202 | mufu(e, BW_IR_MUFU_RSQ, R(n->rs), R(n->rs), 1); |
| 203 | for (unsigned i = 0; i < 4; i++) { fmul(e, S(x4, i), S(x4, i), R(n->rs)); fmul(e, S(x4, i), S(x4, i), S(w4, i)); } |
| 204 | sts32(e, n->sxl, S(x4, 0)); |
| 205 | for (unsigned i = 1; i < 4; i++) { imadi(e, R(n->a1), R(n->one), 4u * i, n->sxl); sts32(e, n->a1, S(x4, i)); } |
| 206 | bar_sync(e); |
| 207 | lds128(e, n->pt4, n->sxp, 0, 1); |
| 208 | if (rope) for (unsigned i = 0; i < 4; i++) { fmul(e, R(n->ts), S(x4, i), S(cos4, i)); ffma(e, S(x4, i), S(n->pt4, i), S(sin4, i), R(n->ts)); } |
| 209 | } |
| 210 | |
| 211 | |
| 212 | int omega_blackwell_codegen_prefill_attn_prog(const OmegaPrefillAttnSpec *spec, BlackwellIRProgram *prog) { |
| 213 | if (!omega_pattn_spec_ok(spec) || !prog) return -1; |
| 214 | omega_bw_ir_init(prog); |
| 215 | Em em; memset(&em, 0, sizeof em); em.p = prog; Em *e = &em; |
| 216 | const uint32_t W = spec->warps, T = 32u * W, LW = W == 8u ? 3u : 2u; |
| 217 | const uint32_t NKU = 512u / T, NVU = 256u / T, ROUNDS = W / 4u; |
| 218 | const int MUT = spec->mutant; |
| 219 | |
| 220 | /* ---- arguments and identities ---- */ |
| 221 | int udesc = UV64(e); |
| 222 | int pqkv = V64(e), pkc = V64(e), pvc = V64(e), prope = V64(e), pqn = V64(e), pkn = V64(e); |
| 223 | int pos0 = V(e), mc = V(e), kvs = V(e), eps = V(e), qks = V(e); |
| 224 | 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); |
| 225 | int cyk = V(e), nk = V(e), t1 = V(e), t2 = V(e), cxw = V(e); |
| 226 | if (e->err) return -1; |
| 227 | ldcu64(e, udesc, 0x358); |
| 228 | ldc64(e, pqkv, OMEGA_PATTN_ARG_QKV); ldc64(e, pkc, OMEGA_PATTN_ARG_KC); ldc64(e, pvc, OMEGA_PATTN_ARG_VC); |
| 229 | ldc64(e, prope, OMEGA_PATTN_ARG_ROPE); ldc64(e, pqn, OMEGA_PATTN_ARG_QN); ldc64(e, pkn, OMEGA_PATTN_ARG_KN); |
| 230 | ldc32(e, pos0, OMEGA_PATTN_ARG_POS0); ldc32(e, mc, OMEGA_PATTN_ARG_MC); ldc32(e, kvs, OMEGA_PATTN_ARG_KV_STRIDE); |
| 231 | ldc32(e, eps, OMEGA_PATTN_ARG_EPS); ldc32(e, qks, OMEGA_PATTN_ARG_QK_SCALE); |
| 232 | s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y); |
| 233 | movi(e, R(one), 1); movrz(e, R(zero)); movi(e, R(c7fff), 0x7fffu); movi(e, R(c16), 16); |
| 234 | shr(e, warp, tid, 5); |
| 235 | andi(e, R(lane), R(tid), 31u); |
| 236 | imadr(e, cyk, cy, kvs, NOREG); /* byte offset of this KV head's plane */ |
| 237 | imadi(e, R(cxw), R(cx), W, NOREG); /* first chunk token of the CTA */ |
| 238 | /* nk = min(cxw + W, mc) = hi + over * (mc - hi), over = 1 when hi > mc */ |
| 239 | imadi(e, R(t2), R(one), W, cxw); /* hi */ |
| 240 | imadi(e, R(t1), R(t2), 0xffffffffu, mc); /* mc - hi */ |
| 241 | { int ov = V(e); if (e->err) return -1; |
| 242 | shr(e, ov, t1, 31); imadr(e, nk, ov, t1, t2); } |
| 243 | |
| 244 | /* ---- per-lane prologue constants ---- */ |
| 245 | NR n; memset(&n, 0, sizeof n); |
| 246 | n.lane = lane; n.one = one; n.eps = eps; |
| 247 | n.snw = V(e); n.sxl = V(e); n.sxp = V(e); n.sq = V(e); n.ts = V(e); n.rs = V(e); n.a1 = V(e); n.pt4 = V128(e); |
| 248 | int sgn = V(e), ropel = V(e), ad64 = V64(e), rope64 = V64(e), cos4 = V128(e), sin4 = V128(e), tokc = V(e), item = V(e), cnt = V(e); |
| 249 | int h0 = V(e), h1 = V(e), tb = V(e), w0 = V(e), w1 = V(e), w2 = V(e), w3 = V(e); |
| 250 | if (e->err) return -1; |
| 251 | imadi(e, R(n.snw), R(warp), 4u, NOREG); imadi(e, R(n.snw), R(one), SM_N, n.snw); |
| 252 | imadi(e, R(t1), R(warp), 512u, NOREG); imadi(e, R(t1), R(one), SM_X, t1); |
| 253 | imadi(e, R(n.sxl), R(lane), 16u, t1); |
| 254 | { int pl = V(e); if (e->err) return -1; |
| 255 | xorr(e, R(pl), R(lane), R(c16)); imadi(e, R(n.sxp), R(pl), 16u, t1); } |
| 256 | imadi(e, R(sgn), R(one), 0xfffffff0u, lane); shr(e, sgn, sgn, 31); imadi(e, R(sgn), R(sgn), 0x80000000u, NOREG); |
| 257 | andi(e, R(ropel), R(lane), 15u); imadi(e, R(ropel), R(ropel), 16u, NOREG); |
| 258 | |
| 259 | /* loads one token's rope row (position pos0 + tokc) with the sign on sin */ |
| 260 | #define ROPE_LOAD() do { iadd(e, t2, pos0, tokc); imadi(e, R(t2), R(t2), OMEGA_DATTN_ROPE_ROW_BYTES_PA, ropel); imadw(e, rope64, t2, 1u, prope); \ |
| 261 | ldg_wide(e, BW_IR_LDG_E_128, cos4, rope64, 0, udesc, 0); ldg_wide(e, BW_IR_LDG_E_128, sin4, rope64, 256, udesc, 0); } while (0) |
| 262 | #define OMEGA_DATTN_ROPE_ROW_BYTES_PA 512u |
| 263 | /* tokc = min(item + base, lim) for a token index t1 = item + base: tokc = t1 + over * (lim - t1) */ |
| 264 | #define CLAMP_TOK(tok, lim) do { int _d = V(e), _o = V(e); if (e->err) return -1; \ |
| 265 | imadi(e, R(_d), R(tok), 0xffffffffu, lim); shr(e, _o, _d, 31); imadr(e, tokc, _o, _d, tok); } while (0) |
| 266 | |
| 267 | /* ---- K/V append: chunk tokens 0 .. nk-1 (every CTA, identical bits); APPEND mode: only cxw .. nk-1 (one per warp); |
| 268 | * ATTEND mode: none (the APPEND launch wrote them) ---- */ |
| 269 | const bool APPEND = spec->mode == OMEGA_PATTN_MODE_APPEND; |
| 270 | if (spec->mode != OMEGA_PATTN_MODE_ATTEND) { |
| 271 | int kw4 = V128(e), k4 = V128(e), v4 = V128(e), nkm1 = V(e); |
| 272 | if (e->err) return -1; |
| 273 | imadw(e, ad64, lane, 16u, pkn); |
| 274 | ldg_wide(e, BW_IR_LDG_E_128, kw4, ad64, 0, udesc, 1); |
| 275 | imadi(e, R(nkm1), R(one), 0xffffffffu, nk); |
| 276 | if (APPEND) { movi(e, R(cnt), 1u); iadd(e, item, warp, cxw); } |
| 277 | else { imadi(e, R(cnt), R(one), W - 1u, nk); shr(e, cnt, cnt, LW); /* ceil(nk / W) >= 1 */ |
| 278 | mov(e, R(item), R(warp)); } |
| 279 | const int L_k = here(e); |
| 280 | loop_begin(e); |
| 281 | CLAMP_TOK(item, nkm1); |
| 282 | imadi(e, R(t1), R(tokc), OMEGA_PATTN_QKV_WORDS * 4u, NOREG); imadi(e, R(t1), R(cy), 512u, t1); imadi(e, R(t1), R(lane), 16u, t1); |
| 283 | imadi(e, R(t1), R(one), 32u * 128u * 4u, t1); |
| 284 | imadw(e, ad64, t1, 1u, pqkv); |
| 285 | ldg_wide(e, BW_IR_LDG_E_128, k4, ad64, 0, udesc, 1); |
| 286 | ldg_wide(e, BW_IR_LDG_E_128, v4, ad64, 4u * 128u * 4u, udesc, 0); |
| 287 | ROPE_LOAD(); |
| 288 | for (unsigned i = 0; i < 4; i++) xorr(e, S(sin4, i), S(sin4, i), R(sgn)); |
| 289 | norm_rope(e, &n, k4, kw4, cos4, sin4, MUT != 1); |
| 290 | bf16_pair(e, R(w0), k4, 0, c7fff, h0, h1, tb); bf16_pair(e, R(w1), k4, 2, c7fff, h0, h1, tb); |
| 291 | bf16_pair(e, R(w2), v4, 0, c7fff, h0, h1, tb); bf16_pair(e, R(w3), v4, 2, c7fff, h0, h1, tb); |
| 292 | if (MUT != 3) { |
| 293 | int ka = V64(e); if (e->err) return -1; |
| 294 | isetp_ge_u32(e, item, nk); |
| 295 | const int b = bra_if_p0_fwd(e); |
| 296 | iadd(e, t1, pos0, item); imadi(e, R(t1), R(t1), 256u, cyk); imadi(e, R(t1), R(lane), 8u, t1); |
| 297 | imadw(e, ka, t1, 1u, pkc); stg(e, ka, 0, R(w0), udesc); stg(e, ka, 4, R(w1), udesc); |
| 298 | imadw(e, ka, t1, 1u, pvc); stg(e, ka, 0, R(w2), udesc); stg(e, ka, 4, R(w3), udesc); |
| 299 | if (spec->mirror) { /* the same words into the mirror planes */ |
| 300 | int pk2 = V64(e), pv2 = V64(e), kvs2 = V(e); if (e->err) return -1; |
| 301 | ldc64(e, pk2, OMEGA_PATTN_ARG_KC2); ldc64(e, pv2, OMEGA_PATTN_ARG_VC2); ldc32(e, kvs2, OMEGA_PATTN_ARG_KV_STRIDE2); |
| 302 | imadr(e, t2, cy, kvs2, NOREG); |
| 303 | iadd(e, t1, pos0, item); imadi(e, R(t1), R(t1), 256u, t2); imadi(e, R(t1), R(lane), 8u, t1); |
| 304 | imadw(e, ka, t1, 1u, pk2); stg(e, ka, 0, R(w0), udesc); stg(e, ka, 4, R(w1), udesc); |
| 305 | imadw(e, ka, t1, 1u, pv2); stg(e, ka, 0, R(w2), udesc); stg(e, ka, 4, R(w3), udesc); |
| 306 | } |
| 307 | patch_bra(e, b, here(e)); |
| 308 | } |
| 309 | imadi(e, R(item), R(one), W, item); |
| 310 | imadi(e, R(cnt), R(one), 0xffffffffu, cnt); |
| 311 | isetp_ge_u32(e, cnt, one); |
| 312 | bra_if_p0(e, L_k); |
| 313 | loop_end(e); |
| 314 | membar(e); |
| 315 | if (APPEND) { |
| 316 | (void)h0; |
| 317 | tail(e); |
| 318 | if (e->err) return -1; |
| 319 | return omega_bw_regalloc_solve(prog) != 0 ? -1 : 0; |
| 320 | } |
| 321 | bar_sync(e); |
| 322 | } |
| 323 | |
| 324 | /* ---- Q fragments: rounds of 2 tiles (4 tokens) through shared memory ---- */ |
| 325 | int q[8]; |
| 326 | for (unsigned s = 0; s < 8; s++) q[s] = V128(e); |
| 327 | { |
| 328 | int qw4 = V128(e), x4 = V128(e), qlane = V(e), rr = V(e), cr = V(e), mcm1 = V(e), qbase = V(e), jt = V(e), hh = V(e); |
| 329 | if (e->err) return -1; |
| 330 | for (unsigned s = 0; s < 8; s++) for (unsigned k = 0; k < 4; k++) movrz(e, S(q[s], k)); /* defined before the rounds loop */ |
| 331 | imadw(e, ad64, lane, 16u, pqn); |
| 332 | ldg_wide(e, BW_IR_LDG_E_128, qw4, ad64, 0, udesc, 1); |
| 333 | imadi(e, R(mcm1), R(one), 0xffffffffu, mc); |
| 334 | /* lane part of the fragment address: (lane>>2)*512 + (lane&1)*32 + ((lane>>1)&1)*8 */ |
| 335 | shr(e, t1, lane, 2); imadi(e, R(qlane), R(t1), 512u, NOREG); |
| 336 | andi(e, R(t1), R(lane), 1u); imadi(e, R(qlane), R(t1), 32u, qlane); |
| 337 | shr(e, t1, lane, 1); andi(e, R(t1), R(t1), 1u); imadi(e, R(qlane), R(t1), 8u, qlane); |
| 338 | /* this warp's fragment base: tile (warp>>1)&1 of the round */ |
| 339 | shr(e, t1, warp, 1); andi(e, R(t1), R(t1), 1u); imadi(e, R(qbase), R(t1), 4096u, NOREG); imadi(e, R(qbase), R(lane), 16u, qbase); |
| 340 | movrz(e, R(rr)); movi(e, R(cr), ROUNDS); |
| 341 | const int L_r = here(e); |
| 342 | loop_begin(e); |
| 343 | mov(e, R(item), R(warp)); movi(e, R(cnt), 32u / W); |
| 344 | const int L_i = here(e); |
| 345 | loop_begin(e); |
| 346 | shr(e, jt, item, 3); andi(e, R(hh), R(item), 7u); |
| 347 | imadi(e, R(t1), R(rr), 4u, jt); iadd(e, t1, t1, cxw); /* chunk token */ |
| 348 | { int tk = V(e); if (e->err) return -1; mov(e, R(tk), R(t1)); CLAMP_TOK(tk, mcm1); } |
| 349 | imadi(e, R(t1), R(cy), 8u, hh); imadi(e, R(t1), R(t1), 512u, NOREG); |
| 350 | imadi(e, R(t1), R(tokc), OMEGA_PATTN_QKV_WORDS * 4u, t1); imadi(e, R(t1), R(lane), 16u, t1); |
| 351 | imadw(e, ad64, t1, 1u, pqkv); |
| 352 | ldg_wide(e, BW_IR_LDG_E_128, x4, ad64, 0, udesc, 1); |
| 353 | ROPE_LOAD(); |
| 354 | for (unsigned i = 0; i < 4; i++) xorr(e, S(sin4, i), S(sin4, i), R(sgn)); |
| 355 | norm_rope(e, &n, x4, qw4, cos4, sin4, MUT != 1); |
| 356 | for (unsigned i = 0; i < 4; i++) fmul(e, S(x4, i), S(x4, i), R(qks)); |
| 357 | bf16_pair(e, R(w0), x4, 0, c7fff, h0, h1, tb); bf16_pair(e, R(w1), x4, 2, c7fff, h0, h1, tb); |
| 358 | /* dst = qlane + (jt>>1)*4096 + h*64 + (jt&1)*4 */ |
| 359 | shr(e, t1, jt, 1); imadi(e, R(t1), R(t1), 4096u, qlane); imadi(e, R(t1), R(hh), 64u, t1); |
| 360 | andi(e, R(t2), R(jt), 1u); imadi(e, R(t1), R(t2), 4u, t1); |
| 361 | sts32(e, t1, R(w0)); imadi(e, R(t2), R(one), 16u, t1); sts32(e, t2, R(w1)); |
| 362 | imadi(e, R(item), R(one), W, item); |
| 363 | imadi(e, R(cnt), R(one), 0xffffffffu, cnt); |
| 364 | isetp_ge_u32(e, cnt, one); |
| 365 | bra_if_p0(e, L_i); |
| 366 | loop_end(e); |
| 367 | bar_sync(e); |
| 368 | /* the round's warps (warp>>2 == rr; every warp when W = 4) load their 8 fragments */ |
| 369 | int skip = -1; |
| 370 | if (W == 8u) { shr(e, t1, warp, 2); xorr(e, R(t1), R(t1), R(rr)); isetp_ge_u32(e, t1, one); skip = bra_if_p0_fwd(e); } |
| 371 | for (unsigned s = 0; s < 8; s++) lds128(e, q[s], qbase, s * 512u, s == 0); |
| 372 | if (skip >= 0) { nop(e, 1); patch_bra(e, skip, here(e)); } |
| 373 | bar_sync(e); |
| 374 | imadi(e, R(rr), R(one), 1u, rr); |
| 375 | imadi(e, R(cr), R(one), 0xffffffffu, cr); |
| 376 | isetp_ge_u32(e, cr, one); |
| 377 | bra_if_p0(e, L_r); |
| 378 | loop_end(e); |
| 379 | } |
| 380 | #undef ROPE_LOAD |
| 381 | #undef CLAMP_TOK |
| 382 | |
| 383 | /* ---- main loop state ---- */ |
| 384 | int o[8], sc[4], kf[2][2], pp[2], vf[4]; |
| 385 | for (unsigned i = 0; i < 8; i++) o[i] = V128(e); |
| 386 | int mA = V(e), mB = V(e), lA = V(e), lB = V(e), dqa = V(e), gK = V64(e), gV = V64(e), sKb = V(e), sVb = V(e); |
| 387 | int lane16 = V(e), vfb = V(e), negc = V(e), tokA = V(e); |
| 388 | if (e->err) return -1; |
| 389 | { |
| 390 | int a = V(e), c = V(e), tq = V(e); if (e->err) return -1; |
| 391 | shr(e, t1, warp, 1); imadi(e, R(tokA), R(t1), 2u, cxw); /* the tile's first chunk token */ |
| 392 | iadd(e, dqa, pos0, tokA); /* row-A position - kb (kb = 0) */ |
| 393 | /* blocks: kend = pos0 + nk - 1; nblk = kend / 32 + 1 */ |
| 394 | iadd(e, t1, pos0, nk); imadi(e, R(t1), R(one), 0xffffffffu, t1); shr(e, t1, t1, 5); imadi(e, R(cnt), R(one), 1u, t1); |
| 395 | shr(e, a, tid, 4); andi(e, R(c), R(tid), 15u); |
| 396 | /* staging pointers */ |
| 397 | imadw(e, gK, cyk, 1u, pkc); imadw(e, gK, a, 256u, gK); imadi(e, R(t1), R(c), 16u, NOREG); imadw(e, gK, t1, 1u, gK); |
| 398 | imadw(e, gV, cyk, 1u, pvc); imadw(e, gV, a, 512u, gV); imadw(e, gV, t1, 1u, gV); |
| 399 | /* sKb = (c>>1)*1024 + (a&7)*64 + ((a>>3)&1)*8 + (c&1)*4 */ |
| 400 | shr(e, t1, c, 1); imadi(e, R(sKb), R(t1), 1024u, NOREG); |
| 401 | andi(e, R(t1), R(a), 7u); imadi(e, R(sKb), R(t1), 64u, sKb); |
| 402 | shr(e, t1, a, 3); andi(e, R(t1), R(t1), 1u); imadi(e, R(sKb), R(t1), 8u, sKb); |
| 403 | andi(e, R(t2), R(c), 1u); imadi(e, R(sKb), R(t2), 4u, sKb); |
| 404 | /* sVb = SM_V + ((a>>3)*8 + (c>>1))*512 + (a&3)*16 + ((c&1)*2 + ((a>>2)&1))*4 */ |
| 405 | shr(e, t1, a, 3); shr(e, tq, c, 1); imadi(e, R(t1), R(t1), 8u, tq); imadi(e, R(sVb), R(t1), 512u, NOREG); |
| 406 | andi(e, R(t1), R(a), 3u); imadi(e, R(sVb), R(t1), 16u, sVb); |
| 407 | shr(e, t1, a, 2); andi(e, R(t1), R(t1), 1u); imadi(e, R(t1), R(t2), 2u, t1); imadi(e, R(sVb), R(t1), 4u, sVb); |
| 408 | imadi(e, R(sVb), R(one), SM_V, sVb); |
| 409 | imadi(e, R(lane16), R(lane), 16u, NOREG); |
| 410 | andi(e, R(t1), R(warp), 1u); imadi(e, R(vfb), R(t1), 2048u, lane16); imadi(e, R(vfb), R(one), SM_V, vfb); |
| 411 | } |
| 412 | for (unsigned i = 0; i < 8; i++) for (unsigned k = 0; k < 4; k++) movrz(e, S(o[i], k)); |
| 413 | movi(e, R(negc), NEG_BIG); mov(e, R(mA), R(negc)); mov(e, R(mB), R(negc)); movrz(e, R(lA)); movrz(e, R(lB)); |
| 414 | |
| 415 | int kq4[4], va4[2], vb4[2]; |
| 416 | for (unsigned i = 0; i < NKU; i++) kq4[i] = V128(e); |
| 417 | for (unsigned i = 0; i < NVU; i++) { va4[i] = V128(e); vb4[i] = V128(e); } |
| 418 | int ev = V(e), od = V(e), sa = V(e), mxA = V(e), mxB = V(e), alA = V(e), alB = V(e), sh = V(e); |
| 419 | for (unsigned j = 0; j < 4; j++) sc[j] = V128(e); |
| 420 | for (unsigned b = 0; b < 2; b++) { kf[b][0] = V128(e); kf[b][1] = V128(e); pp[b] = V128(e); } |
| 421 | for (unsigned i = 0; i < 4; i++) vf[i] = V128(e); |
| 422 | if (e->err) return -1; |
| 423 | |
| 424 | const int L_m = here(e); |
| 425 | loop_begin(e); |
| 426 | bar_sync(e); /* every warp is done with the previous block (or the prologue) */ |
| 427 | /* ---- stage the block: K units (key a + i*2W, 16-byte chunk c), V units (key pair a + i*2W) ---- */ |
| 428 | for (unsigned i = 0; i < NKU; i++) ldg_wide(e, BW_IR_LDG_E_128, kq4[i], gK, i * 2u * W * 256u, udesc, i == 0); |
| 429 | if (NKU * 4u + NVU * 8u > 16u) { /* W = 4: K first, then V (register budget) */ |
| 430 | for (unsigned i = 0; i < NKU; i++) for (unsigned k = 0; k < 4; k++) { |
| 431 | uint32_t ci = W == 4u ? (i >> 1) * 512u + (i & 1u) * 8u : i * 512u; |
| 432 | imadi(e, R(sa), R(one), ci + 16u * k, sKb); sts32(e, sa, S(kq4[i], k)); |
| 433 | } |
| 434 | } |
| 435 | for (unsigned i = 0; i < NVU; i++) { |
| 436 | ldg_wide(e, BW_IR_LDG_E_128, va4[i], gV, i * 2u * W * 512u, udesc, NKU * 4u + NVU * 8u > 16u && i == 0); |
| 437 | ldg_wide(e, BW_IR_LDG_E_128, vb4[i], gV, i * 2u * W * 512u + 256u, udesc, 0); |
| 438 | } |
| 439 | if (NKU * 4u + NVU * 8u <= 16u) { |
| 440 | for (unsigned i = 0; i < NKU; i++) for (unsigned k = 0; k < 4; k++) { |
| 441 | uint32_t ci = W == 4u ? (i >> 1) * 512u + (i & 1u) * 8u : i * 512u; |
| 442 | imadi(e, R(sa), R(one), ci + 16u * k, sKb); sts32(e, sa, S(kq4[i], k)); |
| 443 | } |
| 444 | } |
| 445 | for (unsigned i = 0; i < NVU; i++) for (unsigned k = 0; k < 4; k++) { |
| 446 | andi(e, R(t1), S(va4[i], k), 0xffffu); imadi(e, R(ev), S(vb4[i], k), 65536u, t1); |
| 447 | shrs(e, t1, S(va4[i], k), 16); andi(e, R(t2), S(vb4[i], k), 0xffff0000u); iadd(e, od, t1, t2); |
| 448 | imadi(e, R(sa), R(one), i * 4096u + (2u * k) * 64u, sVb); sts32(e, sa, R(ev)); |
| 449 | imadi(e, R(sa), R(one), i * 4096u + (2u * k + 1u) * 64u, sVb); sts32(e, sa, R(od)); |
| 450 | } |
| 451 | bar_sync(e); |
| 452 | |
| 453 | /* ---- S = Q K^T: 8 k-steps x 4 n-tiles, K fragments double-buffered on scoreboards 3/4 ---- */ |
| 454 | lds_pf(e, kf[0][0], lane16, SM_K + 0u, 3); lds_pf(e, kf[0][1], lane16, SM_K + 512u, 3); |
| 455 | consume(e, 3); |
| 456 | for (unsigned s = 0; s < 8; s++) { |
| 457 | const unsigned cur = s & 1u, nx = cur ^ 1u, sbn = 3u + nx; |
| 458 | if (s < 7) { lds_pf(e, kf[nx][0], lane16, SM_K + (s + 1u) * 1024u, sbn); lds_pf(e, kf[nx][1], lane16, SM_K + (s + 1u) * 1024u + 512u, sbn); } |
| 459 | hmma(e, sc[0], q[s], kf[cur][0], 0, s ? sc[0] : NOREG, 8); |
| 460 | hmma(e, sc[1], q[s], kf[cur][0], 2, s ? sc[1] : NOREG, 8); |
| 461 | hmma(e, sc[2], q[s], kf[cur][1], 0, s ? sc[2] : NOREG, 8); |
| 462 | hmma(e, sc[3], q[s], kf[cur][1], 2, s ? sc[3] : NOREG, 8); |
| 463 | if (s < 7) consume(e, sbn); |
| 464 | } |
| 465 | nop(e, 15); nop(e, 14); |
| 466 | |
| 467 | /* ---- causal mask: key kb + 8j + 2t + (e&1) > row position (row A for e < 2, A + 1 for e >= 2) ---- */ |
| 468 | if (MUT != 2) { |
| 469 | imadi(e, R(t1), R(one), 0xffffffe1u, dqa); shr(e, t1, t1, 31); /* 1 when kb + 31 > row-A position */ |
| 470 | isetp_ge_u32(e, zero, t1); |
| 471 | const int b = bra_if_p0_fwd(e); |
| 472 | int dql = V(e); if (e->err) return -1; |
| 473 | shr(e, t2, lane16, 3); andi(e, R(t2), R(t2), 6u); imadi(e, R(dql), R(t2), 0xffffffffu, dqa); /* dqa - 2t */ |
| 474 | for (unsigned j = 0; j < 4; j++) for (unsigned k = 0; k < 4; k++) { |
| 475 | int32_t cst = (k >= 2 ? 1 : 0) - 8 * (int32_t)j - (int32_t)(k & 1u); |
| 476 | imadi(e, R(t1), R(one), (uint32_t)cst, dql); shr(e, t1, t1, 31); imadi(e, R(t1), R(t1), NEG_BIG, NOREG); |
| 477 | fadd(e, S(sc[j], k), S(sc[j], k), R(t1)); |
| 478 | } |
| 479 | patch_bra(e, b, here(e)); |
| 480 | } |
| 481 | |
| 482 | /* ---- online softmax (log2 units) ---- */ |
| 483 | fmaxx(e, R(mxA), S(sc[0], 0), S(sc[0], 1)); fmaxx(e, R(mxB), S(sc[0], 2), S(sc[0], 3)); |
| 484 | for (unsigned j = 1; j < 4; j++) { |
| 485 | fmaxx(e, R(mxA), R(mxA), S(sc[j], 0)); fmaxx(e, R(mxA), R(mxA), S(sc[j], 1)); |
| 486 | fmaxx(e, R(mxB), R(mxB), S(sc[j], 2)); fmaxx(e, R(mxB), R(mxB), S(sc[j], 3)); |
| 487 | } |
| 488 | shfl_bfly(e, sh, mxA, 1); fmaxx(e, R(mxA), R(mxA), R(sh)); shfl_bfly(e, sh, mxA, 2); fmaxx(e, R(mxA), R(mxA), R(sh)); |
| 489 | shfl_bfly(e, sh, mxB, 1); fmaxx(e, R(mxB), R(mxB), R(sh)); shfl_bfly(e, sh, mxB, 2); fmaxx(e, R(mxB), R(mxB), R(sh)); |
| 490 | fmaxx(e, R(mxA), R(mxA), R(mA)); fmaxx(e, R(mxB), R(mxB), R(mB)); |
| 491 | fsub(e, R(alA), R(mA), R(mxA)); fsub(e, R(alB), R(mB), R(mxB)); |
| 492 | mov(e, R(mA), R(mxA)); mov(e, R(mB), R(mxB)); |
| 493 | for (unsigned j = 0; j < 4; j++) for (unsigned k = 0; k < 4; k++) fsub(e, S(sc[j], k), S(sc[j], k), R(k < 2 ? mA : mB)); |
| 494 | mufu(e, BW_IR_MUFU_EX2, R(alA), R(alA), 1); mufu(e, BW_IR_MUFU_EX2, R(alB), R(alB), 0); |
| 495 | for (unsigned j = 0; j < 4; j++) for (unsigned k = 0; k < 4; k++) mufu(e, BW_IR_MUFU_EX2, S(sc[j], k), S(sc[j], k), 0); |
| 496 | fmul(e, R(lA), R(lA), R(alA)); fmul(e, R(lB), R(lB), R(alB)); |
| 497 | for (unsigned j = 0; j < 4; j++) for (unsigned k = 0; k < 4; k++) fadd(e, R(k < 2 ? lA : lB), R(k < 2 ? lA : lB), S(sc[j], k)); |
| 498 | if (MUT != 4) for (unsigned i = 0; i < 8; i++) for (unsigned k = 0; k < 4; k++) fmul(e, S(o[i], k), S(o[i], k), R(k < 2 ? alA : alB)); |
| 499 | for (unsigned b = 0; b < 2; b++) { |
| 500 | f2fp(e, S(pp[b], 0), S(sc[2 * b], 1), S(sc[2 * b], 0)); f2fp(e, S(pp[b], 1), S(sc[2 * b], 3), S(sc[2 * b], 2)); |
| 501 | f2fp(e, S(pp[b], 2), S(sc[2 * b + 1], 1), S(sc[2 * b + 1], 0)); f2fp(e, S(pp[b], 3), S(sc[2 * b + 1], 3), S(sc[2 * b + 1], 2)); |
| 502 | } |
| 503 | |
| 504 | /* ---- O += P V: 2 k-steps (16 keys) x 8 n-tiles of this warp's 64 dims ---- */ |
| 505 | for (unsigned kq = 0; kq < 2; kq++) { |
| 506 | for (unsigned np = 0; np < 4; np++) lds128(e, vf[np], vfb, kq * 4096u + np * 512u, np == 0); |
| 507 | for (unsigned np = 0; np < 4; np++) { hmma(e, o[2 * np], pp[kq], vf[np], 0, o[2 * np], 8); hmma(e, o[2 * np + 1], pp[kq], vf[np], 2, o[2 * np + 1], 8); } |
| 508 | } |
| 509 | imadw(e, gK, one, 8192u, gK); imadw(e, gV, one, 8192u, gV); |
| 510 | imadi(e, R(dqa), R(one), 0xffffffe0u, dqa); |
| 511 | imadi(e, R(cnt), R(one), 0xffffffffu, cnt); |
| 512 | isetp_ge_u32(e, cnt, one); |
| 513 | bra_if_p0(e, L_m); |
| 514 | loop_end(e); |
| 515 | |
| 516 | /* ---- epilogue ---- */ |
| 517 | nop(e, 15); nop(e, 14); |
| 518 | { |
| 519 | int pout = V64(e), osc = V(e), po = V64(e), g = V(e), tq = V(e), etid = V(e), ecx = V(e), ecy = V(e), emc = V(e), ewarp = V(e), elane = V(e), etok = V(e); |
| 520 | if (e->err) return -1; |
| 521 | ldc64(e, pout, OMEGA_PATTN_ARG_OUT); ldc32(e, osc, OMEGA_PATTN_ARG_OUT_SCALE); ldc32(e, emc, OMEGA_PATTN_ARG_MC); |
| 522 | s2r(e, etid, BW_SR_TID_X); s2r(e, ecx, BW_SR_CTAID_X); s2r(e, ecy, BW_SR_CTAID_Y); |
| 523 | shr(e, ewarp, etid, 5); andi(e, R(elane), R(etid), 31u); |
| 524 | shr(e, t1, ewarp, 1); imadi(e, R(t1), R(t1), 2u, NOREG); imadi(e, R(etok), R(ecx), W, t1); |
| 525 | shfl_bfly(e, sh, lA, 1); fadd(e, R(lA), R(lA), R(sh)); shfl_bfly(e, sh, lA, 2); fadd(e, R(lA), R(lA), R(sh)); |
| 526 | shfl_bfly(e, sh, lB, 1); fadd(e, R(lB), R(lB), R(sh)); shfl_bfly(e, sh, lB, 2); fadd(e, R(lB), R(lB), R(sh)); |
| 527 | mufu(e, BW_IR_MUFU_RCP, R(lA), R(lA), 1); mufu(e, BW_IR_MUFU_RCP, R(lB), R(lB), 0); |
| 528 | fmul(e, R(lA), R(lA), R(osc)); fmul(e, R(lB), R(lB), R(osc)); |
| 529 | for (unsigned i = 0; i < 8; i++) for (unsigned k = 0; k < 4; k++) fmul(e, S(o[i], k), S(o[i], k), R(k < 2 ? lA : lB)); |
| 530 | /* out + ((tokA*32 + cy*8 + g)*128 + (warp&1)*64 + 2t)*4 */ |
| 531 | shr(e, g, elane, 2); andi(e, R(tq), R(elane), 3u); |
| 532 | imadi(e, R(t1), R(etok), 32u, NOREG); imadi(e, R(t1), R(ecy), 8u, t1); iadd(e, t1, t1, g); imadi(e, R(t1), R(t1), 512u, NOREG); |
| 533 | andi(e, R(t2), R(ewarp), 1u); imadi(e, R(t1), R(t2), 256u, t1); imadi(e, R(t1), R(tq), 8u, t1); |
| 534 | imadw(e, po, t1, 1u, pout); |
| 535 | for (unsigned row = 0; row < 2; row++) { |
| 536 | imadi(e, R(t1), R(one), row, etok); |
| 537 | isetp_ge_u32(e, t1, emc); |
| 538 | const int b = bra_if_p0_fwd(e); |
| 539 | for (unsigned i = 0; i < 8; i++) { stg(e, po, row * 16384u + i * 32u, S(o[i], 2 * row), udesc); stg(e, po, row * 16384u + i * 32u + 4u, S(o[i], 2 * row + 1), udesc); } |
| 540 | patch_bra(e, b, here(e)); |
| 541 | } |
| 542 | } |
| 543 | (void)iaddo; (void)h0; |
| 544 | tail(e); |
| 545 | if (e->err) return -1; |
| 546 | if (omega_bw_regalloc_solve(prog) != 0) return -1; |
| 547 | return 0; |
| 548 | } |
| 549 | #else |
| 550 | int omega_blackwell_codegen_prefill_attn_prog(const OmegaPrefillAttnSpec *spec, BlackwellIRProgram *prog) { |
| 551 | uint64_t error = 0; |
| 552 | if (!omega_pattn_spec_ok(spec) || !prog) return -1; |
| 553 | return omega_gpu_om_ir_replay((OmegaOmIrUnit)omega_cg_pattn, 0, spec->warps, |
| 554 | (uint64_t)spec->mutant, spec->mode | ((uint64_t)spec->mirror << 8), |
| 555 | prog, &error) == 0 ? 0 : -1; |
| 556 | } |
| 557 | #endif |
| 558 | |
| 559 | int omega_prefill_attn_prog_stats(const OmegaPrefillAttnSpec *spec, uint32_t *insns, uint32_t *gprs) { |
| 560 | static BlackwellIRProgram p; |
| 561 | if (omega_blackwell_codegen_prefill_attn_prog(spec, &p) != 0) return -1; |
| 562 | if (insns) *insns = (uint32_t)p.count; |
| 563 | if (gprs) *gprs = p.regalloc.peak_gpr_usage; |
| 564 | return 0; |
| 565 | } |