omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / evidence / FB1-CUT4-0d0241a / omega_gpu_elementwise_api.c
| 1 | /* omega_gpu_elementwise_api.c: FB-1 cut 4 (2026-10-04). See the header. |
| 2 | * |
| 3 | * Layout of this file: (1) kernel codegen on top of the BlackwellIR, with a |
| 4 | * small scheduler that writes every control word; (2) one launcher cloned from |
| 5 | * the chip-proven tensor matmul path (omega_blackwell_execute_matmul_tensor); |
| 6 | * (3) the public entry points, which split work into <= OMEGA_GPU_EW_MAX_CTAS |
| 7 | * launches and run the host-side bookkeeping. |
| 8 | * |
| 9 | * Scheduling rules (chip-learned, src/omega_numeric.c omega_numeric_check_patch): |
| 10 | * - a variable-latency result (LDG, LDS, LDC, LDCU, S2R, SHFL, MUFU) arrives through |
| 11 | * a scoreboard; the producer stalls >= 2 and the consumer waits on that SB; |
| 12 | * - a fixed-latency result (FADD FMUL FFMA IMAD IADD3 MOV LOP3 SHF) has no |
| 13 | * scoreboard: the producer stalls >= 5 before its consumer (we use 6); |
| 14 | * - a predicate producer (ISETP) stalls 13 before @P0 use (the codegen default); |
| 15 | * - stores (STG, STS) set a read barrier so the next instruction cannot overwrite |
| 16 | * the source register early. |
| 17 | * The emitter below serialises: every instruction waits on everything still |
| 18 | * pending. Slow and safe; speed is a later cut. |
| 19 | */ |
| 20 | #include "omega_gpu_elementwise_api.h" |
| 21 | #include "omega_blackwell_codegen.h" |
| 22 | #include "omega_blackwell_qmd.h" |
| 23 | #include "omega_blackwell_submit.h" |
| 24 | #include "m16_native.h" |
| 25 | #include "sha256.h" |
| 26 | #include <pthread.h> |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include <time.h> |
| 31 | |
| 32 | /* ------------------------------------------------------------------ codegen */ |
| 33 | |
| 34 | #define CW(stall, wbar, rbar, wait) \ |
| 35 | (((uint32_t)(stall) << 9) | ((uint32_t)(wbar) << 14) | ((uint32_t)(rbar) << 17) | ((uint32_t)(wait) << 20)) |
| 36 | #define CW_YIELD 0x2000u |
| 37 | #define SB_NONE 7u |
| 38 | #define SB_GPR 0u /* variable-latency GPR results */ |
| 39 | #define SB_RD 1u /* store source-register read barrier */ |
| 40 | #define SB_UGPR 2u /* uniform-register results (LDCU) */ |
| 41 | |
| 42 | enum { K_FIXED, K_PRED, K_VAR, K_UVAR, K_STORE, K_SYNC, K_EXIT, K_BRA }; |
| 43 | |
| 44 | #define EW_MAX_LOOPS 4 |
| 45 | typedef struct { |
| 46 | BlackwellIRProgram *p; |
| 47 | uint32_t pending; /* scoreboards set and not yet waited on */ |
| 48 | int err; |
| 49 | int loop_start[EW_MAX_LOOPS], loop_end[EW_MAX_LOOPS]; |
| 50 | int n_loops; |
| 51 | } Em; |
| 52 | |
| 53 | static void emit(Em *e, BlackwellIRInsn in, int kind) { |
| 54 | uint32_t wait = e->pending; |
| 55 | switch (kind) { |
| 56 | case K_FIXED: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; |
| 57 | case K_PRED: in.control = CW(13, SB_NONE, SB_NONE, wait); e->pending = 0; break; |
| 58 | case K_VAR: in.control = CW(4, SB_GPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_GPR; break; |
| 59 | case K_UVAR: in.control = CW(4, SB_UGPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_UGPR; break; |
| 60 | case K_STORE: in.control = CW(4, SB_NONE, SB_RD, wait) | CW_YIELD; e->pending = 1u << SB_RD; break; |
| 61 | case K_SYNC: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; |
| 62 | /* a predicated EXIT or BRA that falls through must not be the only waiter */ |
| 63 | case K_EXIT: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; |
| 64 | case K_BRA: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; |
| 65 | default: e->err = -1; return; |
| 66 | } |
| 67 | if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; /* append returns the index */ |
| 68 | } |
| 69 | |
| 70 | static int V(Em *e) { int v = omega_bw_ir_alloc_vreg(e->p); if (v < 0) e->err = -1; return v; } |
| 71 | static int V64(Em *e) { int v = omega_bw_ir_alloc_vreg64(e->p); if (v < 0) e->err = -1; return v; } |
| 72 | static int UV64(Em *e) { int v = omega_bw_ir_alloc_uvreg64(e->p); if (v < 0) e->err = -1; return v; } |
| 73 | static int here(const Em *e) { return (int)e->p->count; } |
| 74 | |
| 75 | #define NOREG -1 |
| 76 | static BlackwellIRInsn I0(BlackwellIROpcode op) { |
| 77 | BlackwellIRInsn in; memset(&in, 0, sizeof in); |
| 78 | in.op = op; in.dst_vreg = NOREG; in.src1_vreg = NOREG; in.src2_vreg = NOREG; in.src3_vreg = NOREG; in.ureg = NOREG; |
| 79 | return in; |
| 80 | } |
| 81 | static void ldcu64(Em *e, int udst, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDCU64); i.dst_vreg = udst; i.imm = off; i.is_uniform = true; emit(e, i, K_UVAR); } |
| 82 | static void ldc64(Em *e, int dst64, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC64); i.dst_vreg = dst64; i.imm = off; emit(e, i, K_VAR); } |
| 83 | static void ldc32(Em *e, int dst, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC); i.dst_vreg = dst; i.imm = off; emit(e, i, K_VAR); } |
| 84 | static void s2r(Em *e, int dst, uint32_t sr) { BlackwellIRInsn i = I0(BW_IR_S2R); i.dst_vreg = dst; i.imm = sr; emit(e, i, K_VAR); } |
| 85 | static void movi(Em *e, int dst, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_MOV_IMM); i.dst_vreg = dst; i.imm = imm; emit(e, i, K_FIXED); } |
| 86 | static void movf(Em *e, int dst, float f) { uint32_t u; memcpy(&u, &f, 4); movi(e, dst, u); } |
| 87 | static void movrz(Em *e, int dst) { BlackwellIRInsn i = I0(BW_IR_MOV_RZ); i.dst_vreg = dst; emit(e, i, K_FIXED); } |
| 88 | /* dst = a * imm + c (c may be NOREG for RZ) */ |
| 89 | static void imadi(Em *e, int dst, int a, uint32_t imm, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = dst; i.src1_vreg = a; i.imm = imm; i.src3_vreg = c; emit(e, i, K_FIXED); } |
| 90 | /* dst = a * b + c (registers) */ |
| 91 | static void imadr(Em *e, int dst, int a, int b, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = dst; i.src1_vreg = a; i.src2_vreg = b; i.src3_vreg = c; emit(e, i, K_FIXED); } |
| 92 | static void iadd3(Em *e, int dst, int a, int b) { BlackwellIRInsn i = I0(BW_IR_IADD3); i.dst_vreg = dst; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } |
| 93 | static void lop3xor(Em *e, int dst, int a, int b) { BlackwellIRInsn i = I0(BW_IR_LOP3_XOR); i.dst_vreg = dst; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } |
| 94 | /* dst64 = idx * 4 + ptr64 (byte address of element idx) */ |
| 95 | static void addr4(Em *e, int dst64, int idx, int ptr64) { BlackwellIRInsn i = I0(BW_IR_IMAD_WIDE); i.dst_vreg = dst64; i.src1_vreg = idx; i.imm = 4; i.src3_vreg = ptr64; emit(e, i, K_FIXED); } |
| 96 | static void ldg(Em *e, int dst, int addr64, int udesc) { BlackwellIRInsn i = I0(BW_IR_LDG_E); i.dst_vreg = dst; i.src1_vreg = addr64; i.ureg = udesc; emit(e, i, K_VAR); } |
| 97 | static void stg(Em *e, int addr64, int val, int udesc) { BlackwellIRInsn i = I0(BW_IR_STG_E); i.src1_vreg = addr64; i.src2_vreg = val; i.ureg = udesc; emit(e, i, K_STORE); } |
| 98 | static void fadd(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FADD); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } |
| 99 | static void fsub(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FSUB); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } |
| 100 | static void fmul(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FMUL); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } |
| 101 | static void ffma(Em *e, int d, int a, int b, int c) { BlackwellIRInsn i = I0(BW_IR_FFMA); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src3_vreg = c; emit(e, i, K_FIXED); } |
| 102 | static void mufu(Em *e, BlackwellIROpcode op, int d, int a) { BlackwellIRInsn i = I0(op); i.dst_vreg = d; i.src1_vreg = a; emit(e, i, K_VAR); } |
| 103 | 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); } |
| 104 | static void sts32(Em *e, int addr, int val) { BlackwellIRInsn i = I0(BW_IR_STS32); i.src1_vreg = addr; i.src2_vreg = val; emit(e, i, K_STORE); } |
| 105 | static void lds32(Em *e, int d, int addr) { BlackwellIRInsn i = I0(BW_IR_LDS32); i.dst_vreg = d; i.src1_vreg = addr; emit(e, i, K_VAR); } |
| 106 | static void bar_sync(Em *e) { emit(e, I0(BW_IR_BAR_SYNC), K_SYNC); } |
| 107 | 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); } |
| 108 | static void exit_if_p0(Em *e) { BlackwellIRInsn i = I0(BW_IR_EXIT); i.predicate_p0 = true; emit(e, i, K_EXIT); } |
| 109 | static void bra_back_if_not_p0(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.predicate_p0 = true; i.predicate_not = true; i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); } |
| 110 | static void tail(Em *e) { |
| 111 | emit(e, I0(BW_IR_EXIT), K_EXIT); |
| 112 | BlackwellIRInsn self = I0(BW_IR_BRA); /* the self-branch every kernel here ends with */ |
| 113 | self.control = 0x000fc000u; |
| 114 | if (omega_bw_ir_append(e->p, &self) < 0) e->err = -1; |
| 115 | } |
| 116 | static void loop_begin(Em *e) { if (e->n_loops < EW_MAX_LOOPS) e->loop_start[e->n_loops] = here(e); } |
| 117 | /* Closing a loop: every value defined before the loop and read inside it stays |
| 118 | * live until the back-branch, otherwise the linear-scan allocator (which knows |
| 119 | * nothing about back-edges) would hand its register to a body temporary whose |
| 120 | * first definition comes after the value's last textual use (e.g. the scale y |
| 121 | * in rmsnorm pass 2), and the next iteration would read garbage. */ |
| 122 | static void loop_end(Em *e) { |
| 123 | if (e->n_loops >= EW_MAX_LOOPS) { e->err = -1; return; } |
| 124 | int start = e->loop_start[e->n_loops], end = here(e) - 1; |
| 125 | e->loop_end[e->n_loops] = end; |
| 126 | e->n_loops++; |
| 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 | /* Kernel argument words (constant bank 0, same slots as the matmul path): |
| 135 | * 0x380 a, 0x388 b, 0x390 c (64-bit each), then four 32-bit parameters. */ |
| 136 | #define ARG_A 0x380u |
| 137 | #define ARG_B 0x388u |
| 138 | #define ARG_C 0x390u |
| 139 | #define ARG_P0 0x398u |
| 140 | #define ARG_P1 0x39cu |
| 141 | #define ARG_P2 0x3a0u |
| 142 | #define ARG_P3 0x3a4u |
| 143 | #define DESC_OFF 0x358u |
| 144 | |
| 145 | typedef struct { int udesc, pa, pb, pc, tid, cta; } Pro; |
| 146 | static Pro prologue(Em *e) { |
| 147 | Pro r; |
| 148 | r.udesc = UV64(e); r.pa = V64(e); r.pb = V64(e); r.pc = V64(e); r.tid = V(e); r.cta = V(e); |
| 149 | ldcu64(e, r.udesc, DESC_OFF); |
| 150 | ldc64(e, r.pa, ARG_A); ldc64(e, r.pb, ARG_B); ldc64(e, r.pc, ARG_C); |
| 151 | s2r(e, r.tid, BW_SR_TID_X); s2r(e, r.cta, BW_SR_CTAID_X); |
| 152 | return r; |
| 153 | } |
| 154 | |
| 155 | /* out[i] = 2^a[i] for i < n (n = P0). Threads 128, grid ceil(n/128). Mutant: 2^(a+1). */ |
| 156 | static void gen_ex2(Em *e, int mutant) { |
| 157 | Pro p = prologue(e); |
| 158 | int n = V(e), i = V(e), off = V64(e), x = V(e), y = V(e); |
| 159 | ldc32(e, n, ARG_P0); |
| 160 | imadi(e, i, p.cta, OMEGA_GPU_EW_THREADS, p.tid); |
| 161 | isetp_ge_u32(e, i, n); |
| 162 | exit_if_p0(e); |
| 163 | addr4(e, off, i, p.pa); |
| 164 | ldg(e, x, off, p.udesc); |
| 165 | if (mutant) { int one = V(e); movf(e, one, 1.0f); fadd(e, x, x, one); } |
| 166 | mufu(e, BW_IR_MUFU_EX2, y, x); |
| 167 | addr4(e, off, i, p.pc); |
| 168 | stg(e, off, y, p.udesc); |
| 169 | tail(e); |
| 170 | } |
| 171 | |
| 172 | /* out[i] = a[i ^ 127] within each CTA of 128 (n % 128 == 0). STS own word, BAR.SYNC, |
| 173 | * LDS the partner word. Mutant: LDS own word (no exchange). */ |
| 174 | static void gen_xchg(Em *e, int mutant) { |
| 175 | Pro p = prologue(e); |
| 176 | int i = V(e), off = V64(e), x = V(e), saddr = V(e), paddr = V(e), mask = V(e), y = V(e); |
| 177 | imadi(e, i, p.cta, OMEGA_GPU_EW_THREADS, p.tid); |
| 178 | addr4(e, off, i, p.pa); |
| 179 | ldg(e, x, off, p.udesc); |
| 180 | imadi(e, saddr, p.tid, 4, NOREG); |
| 181 | sts32(e, saddr, x); |
| 182 | bar_sync(e); |
| 183 | if (mutant) { |
| 184 | lds32(e, y, saddr); |
| 185 | } else { |
| 186 | movi(e, mask, OMEGA_GPU_EW_THREADS - 1); |
| 187 | lop3xor(e, paddr, p.tid, mask); |
| 188 | imadi(e, paddr, paddr, 4, NOREG); |
| 189 | lds32(e, y, paddr); |
| 190 | } |
| 191 | addr4(e, off, i, p.pc); |
| 192 | stg(e, off, y, p.udesc); |
| 193 | tail(e); |
| 194 | } |
| 195 | |
| 196 | /* One CTA of 128 threads per row. P0 = dim/128 (loop count), P1 = bits(1/dim), |
| 197 | * P2 = bits(eps), P3 = dim. Mutant: the warp tree drops the last SHFL step. */ |
| 198 | static void gen_rmsnorm(Em *e, int mutant) { |
| 199 | Pro p = prologue(e); |
| 200 | int nk = V(e), invd = V(e), eps = V(e), dim = V(e); |
| 201 | int rowbase = V(e), idx = V(e), col = V(e), k = V(e), acc = V(e), cT = V(e), c1 = V(e); |
| 202 | int off = V64(e), x = V(e), t = V(e); |
| 203 | ldc32(e, nk, ARG_P0); ldc32(e, invd, ARG_P1); ldc32(e, eps, ARG_P2); ldc32(e, dim, ARG_P3); |
| 204 | imadr(e, rowbase, p.cta, dim, NOREG); |
| 205 | iadd3(e, idx, rowbase, p.tid); |
| 206 | movrz(e, acc); movrz(e, k); |
| 207 | movi(e, cT, OMEGA_GPU_EW_THREADS); movi(e, c1, 1); |
| 208 | loop_begin(e); |
| 209 | int l1 = here(e); |
| 210 | addr4(e, off, idx, p.pa); |
| 211 | ldg(e, x, off, p.udesc); |
| 212 | ffma(e, acc, x, x, acc); |
| 213 | iadd3(e, idx, idx, cT); |
| 214 | iadd3(e, k, k, c1); |
| 215 | isetp_ge_u32(e, k, nk); |
| 216 | bra_back_if_not_p0(e, l1); |
| 217 | loop_end(e); |
| 218 | static const uint32_t steps[5] = { 16, 8, 4, 2, 1 }; |
| 219 | for (int s = 0; s < (mutant ? 4 : 5); s++) { shfl_down(e, t, acc, steps[s]); fadd(e, acc, acc, t); } |
| 220 | int saddr = V(e); |
| 221 | imadi(e, saddr, p.tid, 4, NOREG); |
| 222 | sts32(e, saddr, acc); |
| 223 | bar_sync(e); |
| 224 | int a0 = V(e), a1 = V(e), a2 = V(e), a3 = V(e), s0 = V(e), s1 = V(e), s2 = V(e), s3 = V(e), sum = V(e); |
| 225 | movrz(e, a0); movi(e, a1, 128); movi(e, a2, 256); movi(e, a3, 384); |
| 226 | lds32(e, s0, a0); lds32(e, s1, a1); lds32(e, s2, a2); lds32(e, s3, a3); |
| 227 | fadd(e, sum, s0, s1); fadd(e, sum, sum, s2); fadd(e, sum, sum, s3); |
| 228 | int v = V(e), y = V(e), u = V(e), cmh = V(e), c15 = V(e); |
| 229 | fmul(e, v, sum, invd); |
| 230 | fadd(e, v, v, eps); |
| 231 | mufu(e, BW_IR_MUFU_RSQ, y, v); |
| 232 | movf(e, cmh, -0.5f); movf(e, c15, 1.5f); |
| 233 | fmul(e, u, v, y); fmul(e, u, u, y); ffma(e, u, u, cmh, c15); fmul(e, y, y, u); |
| 234 | int offw = V64(e), offc = V64(e), w = V(e), o = V(e); |
| 235 | iadd3(e, idx, rowbase, p.tid); |
| 236 | imadi(e, col, p.tid, 1, NOREG); /* col = tid (the encoder maps a missing src2 to R0, not RZ, so no IADD3 here) */ |
| 237 | movrz(e, k); |
| 238 | loop_begin(e); |
| 239 | int l2 = here(e); |
| 240 | addr4(e, off, idx, p.pa); |
| 241 | ldg(e, x, off, p.udesc); |
| 242 | addr4(e, offw, col, p.pb); |
| 243 | ldg(e, w, offw, p.udesc); |
| 244 | fmul(e, o, x, y); |
| 245 | fmul(e, o, o, w); |
| 246 | addr4(e, offc, idx, p.pc); |
| 247 | stg(e, offc, o, p.udesc); |
| 248 | iadd3(e, idx, idx, cT); |
| 249 | iadd3(e, col, col, cT); |
| 250 | iadd3(e, k, k, c1); |
| 251 | isetp_ge_u32(e, k, nk); |
| 252 | bra_back_if_not_p0(e, l2); |
| 253 | loop_end(e); |
| 254 | tail(e); |
| 255 | } |
| 256 | |
| 257 | /* One CTA of head_dim/2 threads per head. P0 = head_dim, P1 = head_dim/2. |
| 258 | * a = vector, b = table (cos in the first half of each head, sin in the second). |
| 259 | * Mutant: out0 = q0*cos + q1*sin. */ |
| 260 | static void gen_rope(Em *e, int mutant) { |
| 261 | Pro p = prologue(e); |
| 262 | int hd = V(e), half = V(e), i0 = V(e), i1 = V(e), off0 = V64(e), off1 = V64(e); |
| 263 | int q0 = V(e), q1 = V(e), c = V(e), s = V(e), t0 = V(e), t1 = V(e), o0 = V(e), o1 = V(e); |
| 264 | ldc32(e, hd, ARG_P0); ldc32(e, half, ARG_P1); |
| 265 | imadr(e, i0, p.cta, hd, p.tid); |
| 266 | iadd3(e, i1, i0, half); |
| 267 | addr4(e, off0, i0, p.pa); ldg(e, q0, off0, p.udesc); |
| 268 | addr4(e, off1, i1, p.pa); ldg(e, q1, off1, p.udesc); |
| 269 | addr4(e, off0, i0, p.pb); ldg(e, c, off0, p.udesc); |
| 270 | addr4(e, off1, i1, p.pb); ldg(e, s, off1, p.udesc); |
| 271 | fmul(e, t0, q0, c); fmul(e, t1, q1, s); |
| 272 | if (mutant) fadd(e, o0, t0, t1); else fsub(e, o0, t0, t1); |
| 273 | fmul(e, t0, q0, s); fmul(e, t1, q1, c); |
| 274 | fadd(e, o1, t0, t1); |
| 275 | addr4(e, off0, i0, p.pc); stg(e, off0, o0, p.udesc); |
| 276 | addr4(e, off1, i1, p.pc); stg(e, off1, o1, p.udesc); |
| 277 | tail(e); |
| 278 | } |
| 279 | |
| 280 | /* out[i] = g/(1+2^(g*-log2e)) * up for i < n (P0). Mutant: 2^(g*+log2e). */ |
| 281 | static void gen_swiglu(Em *e, int mutant) { |
| 282 | Pro p = prologue(e); |
| 283 | int n = V(e), i = V(e), off = V64(e), g = V(e), u = V(e), k = V(e), t = V(e), ex = V(e), d = V(e), r = V(e), one = V(e), o = V(e); |
| 284 | ldc32(e, n, ARG_P0); |
| 285 | imadi(e, i, p.cta, OMEGA_GPU_EW_THREADS, p.tid); |
| 286 | isetp_ge_u32(e, i, n); |
| 287 | exit_if_p0(e); |
| 288 | addr4(e, off, i, p.pa); ldg(e, g, off, p.udesc); |
| 289 | addr4(e, off, i, p.pb); ldg(e, u, off, p.udesc); |
| 290 | movf(e, k, mutant ? 1.4426950408889634f : -1.4426950408889634f); |
| 291 | movf(e, one, 1.0f); |
| 292 | fmul(e, t, g, k); |
| 293 | mufu(e, BW_IR_MUFU_EX2, ex, t); |
| 294 | fadd(e, d, ex, one); |
| 295 | mufu(e, BW_IR_MUFU_RCP, r, d); |
| 296 | fmul(e, o, g, r); |
| 297 | fmul(e, o, o, u); |
| 298 | addr4(e, off, i, p.pc); |
| 299 | stg(e, off, o, p.udesc); |
| 300 | tail(e); |
| 301 | } |
| 302 | |
| 303 | /* A value first defined inside a loop body may not be read after the loop |
| 304 | * before it is written again: the allocator could have let a later body |
| 305 | * temporary share its register, and the last iteration's value would be lost. */ |
| 306 | static int check_loop_invariant(const Em *e) { |
| 307 | const OmegaRegAlloc *ra = &e->p->regalloc; |
| 308 | for (int L = 0; L < e->n_loops; L++) |
| 309 | for (int v = 0; v < ra->num_vregs; v++) { |
| 310 | int fd = ra->intervals[v].first_def; |
| 311 | if (fd < e->loop_start[L] || fd > e->loop_end[L]) continue; |
| 312 | for (size_t i = (size_t)e->loop_end[L] + 1; i < e->p->count; i++) { |
| 313 | const BlackwellIRInsn *in = &e->p->insns[i]; |
| 314 | if (in->src1_vreg == v || in->src2_vreg == v || in->src3_vreg == v) return -1; |
| 315 | if (in->dst_vreg == v && !in->is_uniform) break; |
| 316 | } |
| 317 | } |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | #define EW_GPR_BUDGET 64u /* the QMD declares 64 registers, as the matmul launcher does */ |
| 322 | |
| 323 | static int build_kernel(OmegaGpuEwOp op, int mutant, OmegaBlackwellKernel *kernel) { |
| 324 | BlackwellIRProgram *prog = calloc(1, sizeof *prog); |
| 325 | if (!prog) return OMEGA_GPU_EW_CODEGEN_FAIL; |
| 326 | omega_bw_ir_init(prog); |
| 327 | Em e; memset(&e, 0, sizeof e); e.p = prog; |
| 328 | switch (op) { |
| 329 | case OMEGA_GPU_EW_EX2: gen_ex2(&e, mutant); break; |
| 330 | case OMEGA_GPU_EW_XCHG: gen_xchg(&e, mutant); break; |
| 331 | case OMEGA_GPU_EW_RMSNORM: gen_rmsnorm(&e, mutant); break; |
| 332 | case OMEGA_GPU_EW_ROPE: gen_rope(&e, mutant); break; |
| 333 | case OMEGA_GPU_EW_SWIGLU: gen_swiglu(&e, mutant); break; |
| 334 | default: e.err = -1; break; |
| 335 | } |
| 336 | int rc = OMEGA_GPU_EW_CODEGEN_FAIL; |
| 337 | int ra_rc = (e.err == 0) ? omega_bw_regalloc_solve(prog) : -1; |
| 338 | int loop_rc = (ra_rc == 0) ? check_loop_invariant(&e) : -1; |
| 339 | #ifdef OMEGA_EW_DEBUG |
| 340 | fprintf(stderr, "ew codegen op=%d mutant=%d emit_err=%d insns=%zu vregs=%d regalloc=%d loops=%d peak_gpr=%u\n", |
| 341 | (int)op, mutant, e.err, prog->count, prog->regalloc.num_vregs, ra_rc, loop_rc, prog->regalloc.peak_gpr_usage); |
| 342 | #endif |
| 343 | if (e.err == 0 && ra_rc == 0 && loop_rc == 0 && prog->regalloc.peak_gpr_usage <= EW_GPR_BUDGET) { |
| 344 | size_t max_bytes = (size_t)BW_MAX_IR_INSNS * 16; |
| 345 | memset(kernel, 0, sizeof *kernel); |
| 346 | kernel->code = malloc(max_bytes); |
| 347 | size_t emitted = 0; |
| 348 | if (kernel->code && omega_bw_encode_program(prog, kernel->code, max_bytes, &emitted) == 0) { |
| 349 | kernel->code_size = emitted; |
| 350 | kernel->insn_count = emitted / 16; |
| 351 | kernel->gpr_count = prog->regalloc.peak_gpr_usage; |
| 352 | kernel->uniform_gpr_count = prog->regalloc.peak_ugpr_usage; |
| 353 | sha256_hash(kernel->code, kernel->code_size, kernel->code_digest); |
| 354 | rc = OMEGA_GPU_EW_OK; |
| 355 | } else { |
| 356 | #ifdef OMEGA_EW_DEBUG |
| 357 | fprintf(stderr, "ew codegen op=%d: encode failed (an instruction form the encoder refuses)\n", (int)op); |
| 358 | #endif |
| 359 | free(kernel->code); kernel->code = NULL; |
| 360 | } |
| 361 | } |
| 362 | free(prog); |
| 363 | return rc; |
| 364 | } |
| 365 | |
| 366 | /* --------------------------------------------------------- kernel cache */ |
| 367 | |
| 368 | static int g_mutant_op; /* 0 = none */ |
| 369 | typedef struct { int used; int op; int mutant; OmegaBlackwellKernel kernel; } Slot; |
| 370 | static Slot g_cache[8]; |
| 371 | static pthread_mutex_t g_mu = PTHREAD_MUTEX_INITIALIZER; |
| 372 | |
| 373 | void omega_gpu_elementwise_cache_clear(void) { |
| 374 | pthread_mutex_lock(&g_mu); |
| 375 | for (size_t i = 0; i < sizeof g_cache / sizeof g_cache[0]; i++) |
| 376 | if (g_cache[i].used) { free(g_cache[i].kernel.code); memset(&g_cache[i], 0, sizeof g_cache[i]); } |
| 377 | pthread_mutex_unlock(&g_mu); |
| 378 | } |
| 379 | |
| 380 | void omega_gpu_elementwise_test_set_mutant(int op) { |
| 381 | pthread_mutex_lock(&g_mu); |
| 382 | g_mutant_op = op; |
| 383 | pthread_mutex_unlock(&g_mu); |
| 384 | omega_gpu_elementwise_cache_clear(); |
| 385 | } |
| 386 | |
| 387 | static int kernel_for(OmegaGpuEwOp op, OmegaBlackwellKernel *out, bool *hit) { |
| 388 | pthread_mutex_lock(&g_mu); |
| 389 | int mutant = (g_mutant_op == (int)op); |
| 390 | for (size_t i = 0; i < sizeof g_cache / sizeof g_cache[0]; i++) |
| 391 | if (g_cache[i].used && g_cache[i].op == (int)op && g_cache[i].mutant == mutant) { |
| 392 | *out = g_cache[i].kernel; *hit = true; pthread_mutex_unlock(&g_mu); return OMEGA_GPU_EW_OK; |
| 393 | } |
| 394 | OmegaBlackwellKernel k; |
| 395 | int rc = build_kernel(op, mutant, &k); |
| 396 | if (rc == OMEGA_GPU_EW_OK) { |
| 397 | for (size_t i = 0; i < sizeof g_cache / sizeof g_cache[0]; i++) |
| 398 | if (!g_cache[i].used) { g_cache[i].used = 1; g_cache[i].op = (int)op; g_cache[i].mutant = mutant; g_cache[i].kernel = k; break; } |
| 399 | *out = k; *hit = false; |
| 400 | } |
| 401 | pthread_mutex_unlock(&g_mu); |
| 402 | return rc; |
| 403 | } |
| 404 | |
| 405 | int omega_gpu_elementwise_codegen(OmegaGpuEwOp op, const uint32_t *dims, float eps, OmegaBlackwellKernel *kernel) { |
| 406 | (void)dims; (void)eps; /* kernels read their shape from the argument words */ |
| 407 | if (!kernel) return OMEGA_GPU_EW_BAD_ARGS; |
| 408 | pthread_mutex_lock(&g_mu); |
| 409 | int mutant = (g_mutant_op == (int)op); |
| 410 | pthread_mutex_unlock(&g_mu); |
| 411 | return build_kernel(op, mutant, kernel); |
| 412 | } |
| 413 | |
| 414 | /* ------------------------------------------------------------ launcher */ |
| 415 | |
| 416 | #define EW_POISON 0xffbadbadu /* a NaN pattern no kernel here produces on purpose */ |
| 417 | #define EW_MARKER2_PAYLOAD 0x46464646u /* same value as omega_blackwell_submit.c and the engine */ |
| 418 | #define EW_WAIT_MS 600000ull /* like main's load/store launchers; a 5 s wait failed under contention (omega#198) */ |
| 419 | |
| 420 | static const uint32_t SETUP_WORDS[18] = { |
| 421 | 0x20012061, 0x0000cec0, 0x20012092, 0x00000001, 0x200120a8, 0x0000000f, 0x2001255d, 0x00000003, |
| 422 | 0x2001255e, 0x20000000, 0x2001255f, 0x000fffff, 0x20012557, 0x00000003, 0x20012558, 0x22000000, |
| 423 | 0x20012559, 0x00000000, |
| 424 | }; |
| 425 | |
| 426 | static uint64_t now_ns(void) { |
| 427 | struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); |
| 428 | return (uint64_t)t.tv_sec * 1000000000ULL + (uint64_t)t.tv_nsec; |
| 429 | } |
| 430 | static size_t page_round(size_t b) { b = (b + 0xFFFULL) & ~0xFFFULL; return b < 0x1000 ? 0x1000 : b; } |
| 431 | |
| 432 | /* One launch: a and b in, c out (c_bytes/4 words, poison-filled first). Cloned |
| 433 | * from omega_blackwell_execute_matmul_tensor: every buffer GPU-uncached, first |
| 434 | * marker, L2_FLUSH_DIRTY, second marker, then readback and the poison scan. */ |
| 435 | static int run_abc(const OmegaBlackwellKernel *kernel, |
| 436 | const void *a, size_t a_bytes, const void *b, size_t b_bytes, |
| 437 | void *c, size_t c_bytes, uint32_t threads_x, uint32_t grid_x, |
| 438 | const uint32_t params[4], OmegaGpuEwInfo *info) { |
| 439 | M16NativeContext ctx; |
| 440 | if (m16_native_open(&ctx) != 0) return OMEGA_GPU_EW_CHIP_FAIL; |
| 441 | if (m16_native_create_channel(&ctx) != 0) { m16_native_close(&ctx); return OMEGA_GPU_EW_CHIP_FAIL; } |
| 442 | NvrmMem large_pb; |
| 443 | if (nvrm_alloc_gpu_uncached(&ctx.rm, 0x10000, &large_pb) != 0) { m16_native_close(&ctx); return OMEGA_GPU_EW_CHIP_FAIL; } |
| 444 | ctx.pb_mem = large_pb; |
| 445 | |
| 446 | NvrmMem code_mem, cbank_mem, a_mem, b_mem, c_mem, marker_mem, qmd_mem; |
| 447 | #define EW_ALLOC(mem, bytes) if (nvrm_alloc_gpu_uncached(&ctx.rm, (bytes), &(mem)) != 0) { m16_native_close(&ctx); return OMEGA_GPU_EW_CHIP_FAIL; } |
| 448 | EW_ALLOC(code_mem, page_round(kernel->code_size)); |
| 449 | EW_ALLOC(cbank_mem, 0x1000); |
| 450 | EW_ALLOC(a_mem, page_round(a_bytes)); |
| 451 | EW_ALLOC(b_mem, page_round(b_bytes)); |
| 452 | EW_ALLOC(c_mem, page_round(c_bytes)); |
| 453 | EW_ALLOC(marker_mem, 0x1000); |
| 454 | EW_ALLOC(qmd_mem, 0x10000); |
| 455 | #undef EW_ALLOC |
| 456 | memcpy(code_mem.cpu, kernel->code, kernel->code_size); |
| 457 | memcpy(a_mem.cpu, a, a_bytes); |
| 458 | memcpy(b_mem.cpu, b, b_bytes); |
| 459 | size_t c_words = c_bytes / 4; |
| 460 | uint32_t *c_dev = (uint32_t *)c_mem.cpu; |
| 461 | for (size_t i = 0; i < c_words; i++) c_dev[i] = EW_POISON; |
| 462 | |
| 463 | uint32_t cbank_data[OMEGA_BW_CBANK_DRIVER_WORDS]; |
| 464 | omega_blackwell_build_cbank_driver_2d(cbank_data, cbank_mem.va, threads_x, 1, grid_x, 1); |
| 465 | uint32_t cbank_args[OMEGA_BW_CBANK_MATMUL_ARGS_WORDS]; |
| 466 | cbank_args[0] = (uint32_t)a_mem.va; cbank_args[1] = (uint32_t)(a_mem.va >> 32); |
| 467 | cbank_args[2] = (uint32_t)b_mem.va; cbank_args[3] = (uint32_t)(b_mem.va >> 32); |
| 468 | cbank_args[4] = (uint32_t)c_mem.va; cbank_args[5] = (uint32_t)(c_mem.va >> 32); |
| 469 | cbank_args[6] = params[0]; cbank_args[7] = params[1]; cbank_args[8] = params[2]; cbank_args[9] = params[3]; |
| 470 | memcpy(cbank_mem.cpu, cbank_data, sizeof(cbank_data)); |
| 471 | memcpy((uint8_t *)cbank_mem.cpu + 0x380, cbank_args, sizeof(cbank_args)); |
| 472 | |
| 473 | uint64_t qmd0_va = qmd_mem.va, qmd1_va = qmd_mem.va + 0x1000, sem_va = qmd_mem.va + 0x2000, scratch_va = qmd_mem.va + 0x4000; |
| 474 | OmegaBlackwellQmdConfig qmd_cfg = { |
| 475 | .code_va = code_mem.va, .cbank_va = cbank_mem.va, .scratch_va = scratch_va, .sem_va = sem_va, |
| 476 | .qmd0_va = qmd0_va, .qmd1_va = qmd1_va, .num_elements = c_words, .threads_per_block = threads_x, |
| 477 | .grid_width = grid_x, .threads_x = threads_x, .threads_y = 1, .grid_x = grid_x, .grid_y = 1, |
| 478 | .gpr_count = EW_GPR_BUDGET |
| 479 | }; |
| 480 | uint32_t qmd0_words[OMEGA_BW_QMD_WORDS], qmd1_words[OMEGA_BW_QMD_WORDS]; |
| 481 | omega_blackwell_build_qmd0(qmd0_words, qmd0_va, qmd1_va); |
| 482 | omega_blackwell_build_qmd1(qmd1_words, &qmd_cfg); |
| 483 | if (omega_blackwell_verify_qmd_invariants(qmd1_words) != 0) { m16_native_close(&ctx); return OMEGA_GPU_EW_CHIP_FAIL; } |
| 484 | memcpy(qmd_mem.cpu, qmd0_words, sizeof(qmd0_words)); |
| 485 | memcpy((uint8_t *)qmd_mem.cpu + 0x1000, qmd1_words, sizeof(qmd1_words)); |
| 486 | |
| 487 | volatile uint32_t *hsem = (volatile uint32_t *)((uint8_t *)qmd_mem.cpu + 0x2000); |
| 488 | volatile uint32_t *hmarker = (volatile uint32_t *)marker_mem.cpu; |
| 489 | volatile uint32_t *hmarker2 = (volatile uint32_t *)((uint8_t *)marker_mem.cpu + 0x10); |
| 490 | *hsem = 0; *hmarker = 0; *hmarker2 = 0; |
| 491 | __asm__ volatile("dsb sy" ::: "memory"); |
| 492 | |
| 493 | uint32_t pb[1024]; |
| 494 | size_t n = 0; |
| 495 | memcpy(&pb[n], SETUP_WORDS, sizeof(SETUP_WORDS)); n += sizeof(SETUP_WORDS) / 4; |
| 496 | /* Every launch uploads fresh code into a freshly allocated page, so the code address |
| 497 | * of the previous (different) kernel is routinely reused: exactly the stale-SM-icache |
| 498 | * case cut 1b hit. Invalidate before the kernel (constants in omega_blackwell_submit.h). */ |
| 499 | pb[n++] = nvrm_mthd(1, OMEGA_BW_MTHD_INVALIDATE_SHADER_CACHES, 1); pb[n++] = OMEGA_BW_INVALIDATE_SHADER_CACHES_ALL; |
| 500 | pb[n++] = nvrm_mthd(1, 0x0188, 2); pb[n++] = (uint32_t)(cbank_mem.va >> 32); pb[n++] = (uint32_t)cbank_mem.va; |
| 501 | pb[n++] = nvrm_mthd(1, 0x0180, 2); pb[n++] = 0x00000380; pb[n++] = 0x00000001; |
| 502 | pb[n++] = nvrm_mthd(1, 0x01b0, 1); pb[n++] = 0x00000041; |
| 503 | pb[n++] = (224 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28); |
| 504 | memcpy(&pb[n], cbank_data, 224 * 4); n += 224; |
| 505 | pb[n++] = nvrm_mthd(1, 0x0188, 2); pb[n++] = (uint32_t)((cbank_mem.va + 0x380) >> 32); pb[n++] = (uint32_t)(cbank_mem.va + 0x380); |
| 506 | pb[n++] = nvrm_mthd(1, 0x0180, 2); pb[n++] = 0x00000028; pb[n++] = 0x00000001; |
| 507 | pb[n++] = nvrm_mthd(1, 0x01b0, 1); pb[n++] = 0x00000041; |
| 508 | pb[n++] = (10 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28); |
| 509 | memcpy(&pb[n], cbank_args, 10 * 4); n += 10; |
| 510 | pb[n++] = (98 << 16) | (1 << 13) | (0x0318 >> 2) | (2u << 28); |
| 511 | pb[n++] = (1u << 30) | (uint32_t)((qmd0_va >> 40) & 0x1ff); pb[n++] = (uint32_t)(qmd0_va >> 8); |
| 512 | memcpy(&pb[n], qmd0_words, 96 * 4); n += 96; |
| 513 | pb[n++] = nvrm_mthd(1, 0x0188, 2); pb[n++] = (uint32_t)(sem_va >> 32); pb[n++] = (uint32_t)sem_va; |
| 514 | pb[n++] = nvrm_mthd(1, 0x0180, 2); pb[n++] = 0x00000004; pb[n++] = 0x00000001; |
| 515 | pb[n++] = nvrm_mthd(1, 0x01b0, 1); pb[n++] = 0x00000041; |
| 516 | pb[n++] = (1 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28); |
| 517 | pb[n++] = OMEGA_BW_SEMAPHORE_INTERMEDIATE_INIT; |
| 518 | pb[n++] = (98 << 16) | (1 << 13) | (0x0318 >> 2) | (2u << 28); |
| 519 | pb[n++] = (1u << 30) | (uint32_t)((qmd1_va >> 40) & 0x1ff); pb[n++] = (uint32_t)(qmd1_va >> 8); |
| 520 | memcpy(&pb[n], qmd1_words, 96 * 4); n += 96; |
| 521 | pb[n++] = nvrm_mthd(0, 0x005c, 5); |
| 522 | pb[n++] = (uint32_t)marker_mem.va; pb[n++] = (uint32_t)(marker_mem.va >> 32); |
| 523 | pb[n++] = OMEGA_BW_MARKER_COMPLETION_PAYLOAD; pb[n++] = 0; pb[n++] = 0x1 | (1u << 20); |
| 524 | /* C3 tail: L2_FLUSH_DIRTY, then the second release the host waits for before reading */ |
| 525 | pb[n++] = nvrm_mthd(0, 0x0028, 4); pb[n++] = 0; pb[n++] = 0; pb[n++] = 0; pb[n++] = (0x10u << 27); |
| 526 | pb[n++] = nvrm_mthd(0, 0x005c, 5); |
| 527 | pb[n++] = (uint32_t)(marker_mem.va + 0x10); pb[n++] = (uint32_t)((marker_mem.va + 0x10) >> 32); |
| 528 | pb[n++] = EW_MARKER2_PAYLOAD; pb[n++] = 0; pb[n++] = 0x1 | (1u << 20); |
| 529 | |
| 530 | uint64_t t0 = now_ns(); |
| 531 | if (m16_native_submit_methods(&ctx, pb, n) != 0) { m16_native_close(&ctx); return OMEGA_GPU_EW_CHIP_FAIL; } |
| 532 | if (m16_native_wait_marker(hmarker, OMEGA_BW_MARKER_COMPLETION_PAYLOAD, EW_WAIT_MS) != 0) { m16_native_close(&ctx); return OMEGA_GPU_EW_CHIP_FAIL; } |
| 533 | if (m16_native_wait_marker(hmarker2, EW_MARKER2_PAYLOAD, EW_WAIT_MS) != 0) { m16_native_close(&ctx); return OMEGA_GPU_EW_CHIP_FAIL; } |
| 534 | __asm__ volatile("dsb sy" ::: "memory"); |
| 535 | uint64_t t1 = now_ns(); |
| 536 | if (*hsem != OMEGA_BW_SEMAPHORE_INTERMEDIATE_DONE) { m16_native_close(&ctx); return OMEGA_GPU_EW_CHIP_FAIL; } |
| 537 | |
| 538 | memcpy(c, c_mem.cpu, c_bytes); |
| 539 | uint32_t unwritten = 0; |
| 540 | for (size_t i = 0; i < c_words; i++) if (((uint32_t *)c)[i] == EW_POISON) unwritten++; |
| 541 | if (info) { |
| 542 | info->chip_calls++; |
| 543 | info->elapsed_ns += (t1 > t0) ? (t1 - t0) : 0; |
| 544 | info->completion_marker = *hmarker; |
| 545 | info->unwritten_words += unwritten; |
| 546 | info->threads_per_cta = threads_x; |
| 547 | info->ctas_last_launch = grid_x; |
| 548 | } |
| 549 | m16_native_close(&ctx); |
| 550 | return unwritten ? OMEGA_GPU_EW_UNWRITTEN : OMEGA_GPU_EW_OK; |
| 551 | } |
| 552 | |
| 553 | /* ------------------------------------------------------- public entries */ |
| 554 | |
| 555 | static void info_begin(OmegaGpuEwInfo *info, const OmegaBlackwellKernel *k, bool hit) { |
| 556 | if (!info) return; |
| 557 | memset(info, 0, sizeof *info); |
| 558 | info->kernel_cache_hit = hit; |
| 559 | info->gpr_count = k->gpr_count; |
| 560 | info->insn_count = (uint32_t)k->insn_count; |
| 561 | snprintf(info->target_chip, sizeof info->target_chip, "NVIDIA DGX Spark (Grace Blackwell GB10)"); |
| 562 | info->sm_architecture = 121; |
| 563 | } |
| 564 | static uint32_t f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } |
| 565 | |
| 566 | int omega_gpu_rmsnorm_f32(uint32_t rows, uint32_t dim, const float *x, const float *weight, |
| 567 | float eps, float *out, OmegaGpuEwInfo *info) { |
| 568 | if (!x || !weight || !out || rows == 0 || dim == 0 || dim % OMEGA_GPU_EW_THREADS != 0) return OMEGA_GPU_EW_BAD_ARGS; |
| 569 | if (dim > OMEGA_GPU_EW_MAX_DIM) return OMEGA_GPU_EW_TOO_LARGE; |
| 570 | OmegaBlackwellKernel k; bool hit = false; |
| 571 | int rc = kernel_for(OMEGA_GPU_EW_RMSNORM, &k, &hit); |
| 572 | if (rc != OMEGA_GPU_EW_OK) return rc; |
| 573 | info_begin(info, &k, hit); |
| 574 | uint32_t params[4] = { dim / OMEGA_GPU_EW_THREADS, f2u(1.0f / (float)dim), f2u(eps), dim }; |
| 575 | for (uint32_t r0 = 0; r0 < rows; r0 += OMEGA_GPU_EW_MAX_CTAS) { |
| 576 | uint32_t nr = rows - r0 < OMEGA_GPU_EW_MAX_CTAS ? rows - r0 : OMEGA_GPU_EW_MAX_CTAS; |
| 577 | size_t bytes = (size_t)nr * dim * 4; |
| 578 | rc = run_abc(&k, x + (size_t)r0 * dim, bytes, weight, (size_t)dim * 4, out + (size_t)r0 * dim, bytes, |
| 579 | OMEGA_GPU_EW_THREADS, nr, params, info); |
| 580 | if (rc != OMEGA_GPU_EW_OK) return rc; |
| 581 | } |
| 582 | return OMEGA_GPU_EW_OK; |
| 583 | } |
| 584 | |
| 585 | int omega_gpu_rope_f32(uint32_t heads, uint32_t head_dim, const float *v, |
| 586 | const float *cos_half, const float *sin_half, float *out, OmegaGpuEwInfo *info) { |
| 587 | if (!v || !cos_half || !sin_half || !out || heads == 0 || head_dim < 2 || (head_dim & 1)) return OMEGA_GPU_EW_BAD_ARGS; |
| 588 | if (head_dim > 2048) return OMEGA_GPU_EW_TOO_LARGE; |
| 589 | OmegaBlackwellKernel k; bool hit = false; |
| 590 | int rc = kernel_for(OMEGA_GPU_EW_ROPE, &k, &hit); |
| 591 | if (rc != OMEGA_GPU_EW_OK) return rc; |
| 592 | info_begin(info, &k, hit); |
| 593 | uint32_t half = head_dim / 2; |
| 594 | uint32_t hchunk = OMEGA_GPU_EW_MAX_CTAS; |
| 595 | size_t chunk_elems = (size_t)hchunk * head_dim; |
| 596 | float *table = malloc(chunk_elems * sizeof *table); |
| 597 | float *tmp = malloc(chunk_elems * sizeof *tmp); |
| 598 | if (!table || !tmp) { free(table); free(tmp); return OMEGA_GPU_EW_CHIP_FAIL; } |
| 599 | for (uint32_t h = 0; h < hchunk; h++) { |
| 600 | memcpy(table + (size_t)h * head_dim, cos_half, half * sizeof *table); |
| 601 | memcpy(table + (size_t)h * head_dim + half, sin_half, half * sizeof *table); |
| 602 | } |
| 603 | uint32_t params[4] = { head_dim, half, 0, 0 }; |
| 604 | for (uint32_t h0 = 0; h0 < heads; h0 += hchunk) { |
| 605 | uint32_t nh = heads - h0 < hchunk ? heads - h0 : hchunk; |
| 606 | size_t bytes = (size_t)nh * head_dim * 4; |
| 607 | /* through tmp so out may alias v */ |
| 608 | rc = run_abc(&k, v + (size_t)h0 * head_dim, bytes, table, bytes, tmp, bytes, half, nh, params, info); |
| 609 | if (rc != OMEGA_GPU_EW_OK) break; |
| 610 | memcpy(out + (size_t)h0 * head_dim, tmp, bytes); |
| 611 | } |
| 612 | free(table); free(tmp); |
| 613 | return rc; |
| 614 | } |
| 615 | |
| 616 | static int run_1d(OmegaGpuEwOp op, uint32_t n, const void *a, const void *b, void *c, OmegaGpuEwInfo *info) { |
| 617 | OmegaBlackwellKernel k; bool hit = false; |
| 618 | int rc = kernel_for(op, &k, &hit); |
| 619 | if (rc != OMEGA_GPU_EW_OK) return rc; |
| 620 | info_begin(info, &k, hit); |
| 621 | const uint32_t chunk = OMEGA_GPU_EW_MAX_CTAS * OMEGA_GPU_EW_THREADS; |
| 622 | for (uint32_t i0 = 0; i0 < n; i0 += chunk) { |
| 623 | uint32_t ni = n - i0 < chunk ? n - i0 : chunk; |
| 624 | uint32_t grid = (ni + OMEGA_GPU_EW_THREADS - 1) / OMEGA_GPU_EW_THREADS; |
| 625 | uint32_t params[4] = { ni, 0, 0, 0 }; |
| 626 | size_t bytes = (size_t)ni * 4; |
| 627 | rc = run_abc(&k, (const uint8_t *)a + (size_t)i0 * 4, bytes, b ? (const uint8_t *)b + (size_t)i0 * 4 : a, bytes, |
| 628 | (uint8_t *)c + (size_t)i0 * 4, bytes, OMEGA_GPU_EW_THREADS, grid, params, info); |
| 629 | if (rc != OMEGA_GPU_EW_OK) return rc; |
| 630 | } |
| 631 | return OMEGA_GPU_EW_OK; |
| 632 | } |
| 633 | |
| 634 | int omega_gpu_swiglu_f32(uint32_t n, const float *gate, const float *up, float *out, OmegaGpuEwInfo *info) { |
| 635 | if (!gate || !up || !out || n == 0) return OMEGA_GPU_EW_BAD_ARGS; |
| 636 | return run_1d(OMEGA_GPU_EW_SWIGLU, n, gate, up, out, info); |
| 637 | } |
| 638 | int omega_gpu_ex2_f32(uint32_t n, const float *x, float *out, OmegaGpuEwInfo *info) { |
| 639 | if (!x || !out || n == 0) return OMEGA_GPU_EW_BAD_ARGS; |
| 640 | return run_1d(OMEGA_GPU_EW_EX2, n, x, NULL, out, info); |
| 641 | } |
| 642 | int omega_gpu_shared_xchg_u32(uint32_t n, const uint32_t *x, uint32_t *out, OmegaGpuEwInfo *info) { |
| 643 | if (!x || !out || n == 0 || n % OMEGA_GPU_EW_THREADS != 0) return OMEGA_GPU_EW_BAD_ARGS; |
| 644 | return run_1d(OMEGA_GPU_EW_XCHG, n, x, NULL, out, info); |
| 645 | } |
| 646 | |
| 647 | const char *omega_gpu_elementwise_rc_name(int rc) { |
| 648 | switch (rc) { |
| 649 | case OMEGA_GPU_EW_OK: return "OK"; |
| 650 | case OMEGA_GPU_EW_BAD_ARGS: return "BAD_ARGS"; |
| 651 | case OMEGA_GPU_EW_TOO_LARGE: return "TOO_LARGE"; |
| 652 | case OMEGA_GPU_EW_CODEGEN_FAIL: return "CODEGEN_FAIL"; |
| 653 | case OMEGA_GPU_EW_CHIP_FAIL: return "CHIP_FAIL"; |
| 654 | case OMEGA_GPU_EW_UNWRITTEN: return "UNWRITTEN"; |
| 655 | default: return "UNKNOWN"; |
| 656 | } |
| 657 | } |