Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/src/omega_gpu_scan.c # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / src / omega_gpu_scan.c` 332 lines ยท 16059 bytes Raw Blame History 1 /* omega_gpu_scan.c -- see omega_gpu_scan.h. Kernels from Omega's IR (emitter helpers of omega_bw_em.h, the 2 * chip-proven scheduling rules of the tensor-core generators), host references, the level driver, the chip calls. */ 3 #include "omega_gpu_scan.h" 4 #include "omega_bw_em.h" 5 #include "omega_gpu_session.h" 6 7 #include 8 #include 9 10 #define PAD_SUM 0x80000000u /* -0.0: x + -0.0 == x for every x */ 11 #define PAD_MINMAX 0x7fc00000u /* quiet NaN: max(NaN, x) == min(NaN, x) == x */ 12 #define POISON 0x7fa00badu /* a signalling NaN: no FADD or FMNMX writes it, so a word still holding it was never written */ 13 #define TIMEOUT_MS 20000ull 14 15 /* ------------------------------------------------------------------ IR */ 16 17 static void fmnmx(Em *e, BlackwellIROpcode op, int d, int a, int b) { 18 BlackwellIRInsn i = I0(op); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); 19 } 20 typedef struct { int bssy, bra; } Region; 21 /* the lanes with P0 set skip to the region's end; the warp joins again there */ 22 static Region skip_if_p0(Em *e) { Region r; r.bssy = bssy_fwd(e); r.bra = bra_if_p0_fwd(e); return r; } 23 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); } 24 25 static int gen(Em *e, const OmegaScanSpec *sp) { 26 const OmegaScanKind k = sp->kind; 27 const int red = k <= OMEGA_SCAN_K_RED_MIN; 28 int udesc = UV64(e), pin = V64(e), pout = V64(e), ptot = V64(e), len = V(e), tiles = V(e), warps = V(e); 29 int tid = V(e), cta = V(e), lane = V(e), gw = V(e), one = V(e), tile = V(e), el = V(e), v = V(e), t = V(e); 30 int a64 = V64(e), b64 = V64(e), rl = V(e), nz = V(e), c31 = V(e), m1 = V(e), off = V(e), tm1 = V(e); 31 int mask[5]; 32 for (unsigned s = 0; s < 5; s++) mask[s] = k == OMEGA_SCAN_K_TILE ? V(e) : NOREG; 33 if (e->err) return -1; 34 ldcu64(e, udesc, 0x358); 35 ldc64(e, pin, OMEGA_GPU_SCAN_A_IN); ldc64(e, pout, OMEGA_GPU_SCAN_A_OUT); 36 if (k == OMEGA_SCAN_K_TILE) ldc64(e, ptot, OMEGA_GPU_SCAN_A_TOT); 37 ldc32(e, len, OMEGA_GPU_SCAN_A_LEN); ldc32(e, tiles, OMEGA_GPU_SCAN_A_TILES); ldc32(e, warps, OMEGA_GPU_SCAN_A_WARPS); 38 s2r(e, tid, BW_SR_TID_X); s2r(e, cta, BW_SR_CTAID_X); 39 movi(e, one, 1); movi(e, m1, 0xffffffffu); movi(e, nz, PAD_SUM); 40 andi(e, lane, tid, 0, 31u); 41 shr(e, t, tid, 0, 5); 42 imadi(e, gw, cta, 0, OMEGA_GPU_SCAN_THREADS / 32u, t); /* global warp = cta * 8 + warp */ 43 if (k == OMEGA_SCAN_K_ADD) imadi(e, tile, gw, 0, 1u, one); /* the carry starts at tile 1 */ 44 else imadi(e, tile, gw, 0, 1u, NOREG); 45 if (k == OMEGA_SCAN_K_TILE) { 46 /* reversed lanes: lane L holds tile position 31 - L, so SHFL.DOWN by d reads position - d */ 47 movi(e, c31, 31u); 48 lop3(e, rl, lane, c31, NOREG, 0x3cu); /* 31 - L == L ^ 31 for L < 32 */ 49 /* mask[s] = all ones when position - d exists (L + d <= 31), else 0: ((L + d) >> 5) - 1 */ 50 for (unsigned s = 0; s < 5; s++) { 51 movi(e, t, 1u << s); 52 iadd(e, mask[s], lane, t); 53 shr(e, mask[s], mask[s], 0, 5); 54 imadi(e, mask[s], mask[s], 0, 1u, m1); 55 } 56 } 57 58 const int top = here(e); 59 loop_begin(e); 60 isetp_ge_u32(e, tile, tiles); 61 const int done = bra_if_p0_fwd(e); 62 if (red) { 63 const BlackwellIROpcode op = k == OMEGA_SCAN_K_RED_MAX ? BW_IR_FMNMX_MAX : BW_IR_FMNMX_MIN; 64 imadi(e, el, tile, 0, 32u, lane); 65 movi(e, v, k == OMEGA_SCAN_K_RED_SUM ? PAD_SUM : PAD_MINMAX); 66 isetp_ge_u32(e, el, len); 67 { Region r = skip_if_p0(e); imadw(e, a64, el, 4u, pin); ldg32(e, v, a64, udesc, 1); region_end(e, r); } 68 for (uint32_t d = 16; d >= 1; d /= 2) { 69 if (sp->mutant == 1 && d == 1) break; 70 shfl_down(e, t, v, d); 71 if (k == OMEGA_SCAN_K_RED_SUM) fadd(e, v, v, t); else fmnmx(e, op, v, v, t); 72 } 73 isetp_ge_u32(e, lane, one); 74 { Region r = skip_if_p0(e); imadw(e, a64, tile, 4u, pout); stg(e, a64, 0, v, udesc); region_end(e, r); } 75 } else if (k == OMEGA_SCAN_K_TILE) { 76 imadi(e, el, tile, 0, 32u, rl); 77 movi(e, v, PAD_SUM); 78 isetp_ge_u32(e, el, len); 79 { Region r = skip_if_p0(e); imadw(e, a64, el, 4u, pin); ldg32(e, v, a64, udesc, 1); region_end(e, r); } 80 for (unsigned s = 0; s < 5; s++) { 81 if (sp->mutant == 2 && s == 4) break; 82 shfl_down(e, t, v, 1u << s); 83 lop3(e, t, t, mask[s], nz, 0xe2u); /* (t & mask) | (-0.0 & ~mask) */ 84 fadd(e, v, t, v); /* lower position on the left */ 85 } 86 isetp_ge_u32(e, el, len); 87 { Region r = skip_if_p0(e); imadw(e, a64, el, 4u, pout); stg(e, a64, 0, v, udesc); region_end(e, r); } 88 isetp_ge_u32(e, lane, one); /* lane 0 holds position 31: the tile total */ 89 { Region r = skip_if_p0(e); imadw(e, a64, tile, 4u, ptot); stg(e, a64, 0, v, udesc); region_end(e, r); } 90 } else { 91 imadi(e, el, tile, 0, 32u, lane); 92 isetp_ge_u32(e, el, len); 93 { 94 Region r = skip_if_p0(e); 95 if (sp->mutant == 3) imadi(e, tm1, tile, 0, 1u, NOREG); else imadi(e, tm1, tile, 0, 1u, m1); 96 imadw(e, b64, tm1, 4u, pout); imadw(e, a64, el, 4u, pin); 97 ldg32(e, off, b64, udesc, 1); ldg32(e, v, a64, udesc, 0); 98 fadd(e, v, off, v); 99 stg(e, a64, 0, v, udesc); 100 region_end(e, r); 101 } 102 } 103 iadd(e, tile, tile, warps); 104 bra(e, top); 105 loop_end(e); 106 patch_bra(e, done, here(e)); 107 tail(e); 108 return e->err; 109 } 110 111 int omega_gpu_scan_codegen(const OmegaScanSpec *spec, BlackwellIRProgram *prog) { 112 if (!spec || !prog || (unsigned)spec->kind >= OMEGA_SCAN_K_COUNT) return -1; 113 omega_bw_ir_init(prog); 114 Em em; memset(&em, 0, sizeof em); em.p = prog; 115 if (gen(&em, spec) != 0 || em.err || em.depth != 0) return -1; 116 return omega_bw_regalloc_solve(prog); 117 } 118 119 /* ------------------------------------------------------------------ the level driver */ 120 121 uint32_t omega_gpu_scan_grid(size_t len) { 122 size_t tiles = (len + 31) / 32, ctas = (tiles + OMEGA_GPU_SCAN_THREADS / 32u - 1) / (OMEGA_GPU_SCAN_THREADS / 32u); 123 return ctas > OMEGA_GPU_SCAN_MAX_CTAS ? OMEGA_GPU_SCAN_MAX_CTAS : (uint32_t)(ctas ? ctas : 1); 124 } 125 126 static void args_for(uint32_t a[OMEGA_GPU_SCAN_ARGS_WORDS], uint64_t in, uint64_t out, uint64_t tot, size_t len) { 127 memset(a, 0, OMEGA_GPU_SCAN_ARGS_WORDS * 4u); 128 a[0] = (uint32_t)in; a[1] = (uint32_t)(in >> 32); a[2] = (uint32_t)out; a[3] = (uint32_t)(out >> 32); 129 a[4] = (uint32_t)tot; a[5] = (uint32_t)(tot >> 32); 130 a[6] = (uint32_t)len; a[7] = (uint32_t)((len + 31) / 32); a[8] = omega_gpu_scan_grid(len) * (OMEGA_GPU_SCAN_THREADS / 32u); 131 } 132 133 size_t omega_gpu_scan_reduce_scratch_words(size_t n) { 134 size_t w = 0; 135 do { n = (n + 31) / 32; w += n; } while (n > 1); 136 return w; 137 } 138 139 size_t omega_gpu_scan_prefix_scratch_words(size_t n) { 140 size_t w = 0; 141 while (n > 32) { n = (n + 31) / 32; w += n; } 142 return w + 1; /* the last level's single total */ 143 } 144 145 int omega_gpu_scan_drive_reduce(OmegaGpuRedOp op, size_t n, uint64_t in_va, uint64_t scratch_va, uint64_t *out_va, 146 OmegaScanLaunchFn launch, void *ctx, uint32_t *launches) { 147 if ((unsigned)op > OMEGA_GPU_RED_MIN || n == 0 || !launch || !out_va) return OMEGA_GPU_SCAN_BAD_ARGS; 148 if (n > OMEGA_GPU_SCAN_MAX_N) return OMEGA_GPU_SCAN_TOO_LARGE; 149 const OmegaScanKind k = (OmegaScanKind)(OMEGA_SCAN_K_RED_SUM + (int)op); 150 uint64_t in = in_va, out = scratch_va; 151 size_t len = n; 152 uint32_t a[OMEGA_GPU_SCAN_ARGS_WORDS]; 153 do { 154 args_for(a, in, out, 0, len); 155 if (launch(ctx, k, a, omega_gpu_scan_grid(len)) != 0) return OMEGA_GPU_SCAN_CHIP_FAIL; 156 if (launches) (*launches)++; 157 len = (len + 31) / 32; 158 in = out; out += len * 4u; 159 } while (len > 1); 160 *out_va = in; 161 return OMEGA_GPU_SCAN_OK; 162 } 163 164 int omega_gpu_scan_drive_prefix(size_t n, uint64_t in_va, uint64_t out_va, uint64_t scratch_va, 165 OmegaScanLaunchFn launch, void *ctx, uint32_t *launches) { 166 if (n == 0 || !launch) return OMEGA_GPU_SCAN_BAD_ARGS; 167 if (n > OMEGA_GPU_SCAN_MAX_N) return OMEGA_GPU_SCAN_TOO_LARGE; 168 enum { MAXL = 8 }; /* 32^6 >= 2^28 */ 169 uint64_t buf[MAXL], tot[MAXL]; 170 size_t len[MAXL]; 171 unsigned L = 0; 172 uint64_t in = in_va, sp = scratch_va; 173 buf[0] = out_va; len[0] = n; 174 uint32_t a[OMEGA_GPU_SCAN_ARGS_WORDS]; 175 for (;;) { /* forward: scan every level, its totals become the next level (scanned in place) */ 176 size_t nt = (len[L] + 31) / 32; 177 tot[L] = sp; sp += nt * 4u; 178 args_for(a, in, buf[L], tot[L], len[L]); 179 if (launch(ctx, OMEGA_SCAN_K_TILE, a, omega_gpu_scan_grid(len[L])) != 0) return OMEGA_GPU_SCAN_CHIP_FAIL; 180 if (launches) (*launches)++; 181 if (nt == 1) break; 182 if (L + 1 >= MAXL) return OMEGA_GPU_SCAN_TOO_LARGE; 183 buf[L + 1] = tot[L]; len[L + 1] = nt; in = tot[L]; 184 L++; 185 } 186 while (L-- > 0) { /* back down: carry the scanned totals of level L + 1 into level L */ 187 args_for(a, buf[L], buf[L + 1], 0, len[L]); 188 if (launch(ctx, OMEGA_SCAN_K_ADD, a, omega_gpu_scan_grid(len[L])) != 0) return OMEGA_GPU_SCAN_CHIP_FAIL; 189 if (launches) (*launches)++; 190 } 191 return OMEGA_GPU_SCAN_OK; 192 } 193 194 /* ------------------------------------------------------------------ the chip */ 195 196 static struct { 197 int mutant; 198 OmegaGpuMem code[OMEGA_SCAN_K_COUNT]; 199 uint32_t gpr[OMEGA_SCAN_K_COUNT]; 200 int hooked; 201 } g; 202 203 /* lock held; the session calls it right before it closes the device (also in omega_gpu_session_recover): the kernels go 204 * with the device, the next call builds them again on the new one */ 205 static void on_session_close(void) { 206 for (unsigned k = 0; k < OMEGA_SCAN_K_COUNT; k++) if (g.code[k].cpu) omega_gpu_session_free(&g.code[k]); 207 memset(g.code, 0, sizeof g.code); 208 } 209 210 void omega_gpu_scan_test_set_mutant(int mutant) { 211 omega_gpu_session_lock(); 212 for (unsigned k = 0; k < OMEGA_SCAN_K_COUNT; k++) if (g.code[k].cpu) omega_gpu_session_free(&g.code[k]); 213 memset(g.code, 0, sizeof g.code); 214 g.mutant = mutant; 215 omega_gpu_session_unlock(); 216 } 217 218 const char *omega_gpu_scan_rc_name(int rc) { 219 switch (rc) { 220 case OMEGA_GPU_SCAN_OK: return "OK"; 221 case OMEGA_GPU_SCAN_BAD_ARGS: return "BAD_ARGS"; 222 case OMEGA_GPU_SCAN_TOO_LARGE: return "TOO_LARGE"; 223 case OMEGA_GPU_SCAN_CODEGEN_FAIL: return "CODEGEN_FAIL"; 224 case OMEGA_GPU_SCAN_CHIP_FAIL: return "CHIP_FAIL"; 225 default: return "?"; 226 } 227 } 228 229 /* lock held */ 230 static int kernel_locked(OmegaScanKind k) { 231 if (g.code[k].cpu) return OMEGA_GPU_SCAN_OK; 232 if (!g.hooked) { /* without the hook a kernel would outlive its device */ 233 if (omega_gpu_session_on_close(on_session_close) != 0) return OMEGA_GPU_SCAN_CHIP_FAIL; 234 g.hooked = 1; 235 } 236 BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog); 237 if (!prog) return OMEGA_GPU_SCAN_CODEGEN_FAIL; 238 OmegaScanSpec s = { k, g.mutant }; 239 int rc = OMEGA_GPU_SCAN_OK; 240 size_t need = 0; 241 if (omega_gpu_scan_codegen(&s, prog) != 0 || (need = omega_bw_program_code_bytes(prog)) == 0) rc = OMEGA_GPU_SCAN_CODEGEN_FAIL; 242 else if (omega_gpu_session_alloc_code(need, &g.code[k]) != 0) rc = OMEGA_GPU_SCAN_CHIP_FAIL; 243 else { 244 OmegaBlackwellKernel kk; 245 if (omega_blackwell_kernel_encode_into(prog, g.code[k].cpu, need, &kk) != 0 || kk.code_size != need) rc = OMEGA_GPU_SCAN_CODEGEN_FAIL; 246 else if (omega_gpu_session_sync_to_gpu(&g.code[k]) != 0) rc = OMEGA_GPU_SCAN_CHIP_FAIL; 247 else { g.gpr[k] = omega_bw_gpr_declared(kk.gpr_count); if (g.gpr[k] < 64u) g.gpr[k] = 64u; } 248 if (rc != OMEGA_GPU_SCAN_OK) { omega_gpu_session_free(&g.code[k]); memset(&g.code[k], 0, sizeof g.code[k]); } 249 } 250 omega_gpu_host_free(prog); 251 return rc; 252 } 253 254 typedef struct { uint64_t elapsed_ns; } ChipCtx; 255 256 /* lock held */ 257 static int chip_launch(void *ctx, OmegaScanKind k, const uint32_t args[OMEGA_GPU_SCAN_ARGS_WORDS], uint32_t grid_x) { 258 if (kernel_locked(k) != OMEGA_GPU_SCAN_OK) return -1; 259 OmegaGpuLaunch L = { 260 .code_va = g.code[k].va, .gpr_count = g.gpr[k], .threads_x = OMEGA_GPU_SCAN_THREADS, .threads_y = 1, 261 .grid_x = grid_x, .grid_y = 1, .num_elements = (size_t)grid_x * OMEGA_GPU_SCAN_THREADS, 262 .args = args, .n_args = OMEGA_GPU_SCAN_ARGS_WORDS, .timeout_ms = TIMEOUT_MS, 263 }; 264 uint64_t ns = 0; 265 if (omega_gpu_session_launch(&L, &ns, NULL) != 0) return -1; 266 ((ChipCtx *)ctx)->elapsed_ns += ns; 267 return 0; 268 } 269 270 /* lock held: one data buffer [in n | out n | scratch], the input copied in, outputs poisoned */ 271 static int stage_locked(const float *x, size_t n, size_t out_words, size_t scratch_words, OmegaGpuMem *m) { 272 if (!omega_gpu_session_open()) return OMEGA_GPU_SCAN_CHIP_FAIL; 273 if (omega_gpu_session_alloc((n + out_words + scratch_words) * 4u, m) != 0) return OMEGA_GPU_SCAN_CHIP_FAIL; 274 uint32_t *w = m->cpu; 275 memcpy(w, x, n * 4u); 276 for (size_t i = n; i < n + out_words + scratch_words; i++) w[i] = POISON; 277 if (omega_gpu_session_sync_to_gpu(m) != 0) { omega_gpu_session_free(m); return OMEGA_GPU_SCAN_CHIP_FAIL; } 278 return OMEGA_GPU_SCAN_OK; 279 } 280 281 int omega_gpu_reduce(OmegaGpuRedOp op, const float *x, size_t n, float *out, OmegaGpuScanInfo *info) { 282 uint64_t t0 = omega_gpu_session_now_ns(); 283 if (info) memset(info, 0, sizeof *info); 284 if ((unsigned)op > OMEGA_GPU_RED_MIN || !x || !out || n == 0) return OMEGA_GPU_SCAN_BAD_ARGS; 285 if (n > OMEGA_GPU_SCAN_MAX_N) return OMEGA_GPU_SCAN_TOO_LARGE; 286 omega_gpu_session_lock(); 287 OmegaGpuMem m; 288 const size_t sw = omega_gpu_scan_reduce_scratch_words(n); 289 int rc = stage_locked(x, n, 0, sw, &m); 290 if (rc == OMEGA_GPU_SCAN_OK) { 291 ChipCtx c = { 0 }; uint64_t res = 0; uint32_t nl = 0; 292 rc = omega_gpu_scan_drive_reduce(op, n, m.va, m.va + n * 4u, &res, chip_launch, &c, &nl); 293 if (rc == OMEGA_GPU_SCAN_OK && omega_gpu_session_sync_to_cpu(&m) != 0) rc = OMEGA_GPU_SCAN_CHIP_FAIL; 294 if (rc == OMEGA_GPU_SCAN_OK) { 295 uint32_t r; memcpy(&r, (const uint8_t *)m.cpu + (res - m.va), 4); 296 if (r == POISON) { omega_gpu_session_set_error("reduce: the result word was never written"); rc = OMEGA_GPU_SCAN_CHIP_FAIL; } 297 else memcpy(out, &r, 4); 298 } 299 if (info) { info->launches = nl; info->elapsed_ns = c.elapsed_ns; } 300 omega_gpu_session_free(&m); 301 } 302 omega_gpu_session_unlock(); 303 if (info) info->call_ns = omega_gpu_session_now_ns() - t0; 304 return rc; 305 } 306 307 int omega_gpu_prefix_sum(const float *x, size_t n, float *y, OmegaGpuScanInfo *info) { 308 uint64_t t0 = omega_gpu_session_now_ns(); 309 if (info) memset(info, 0, sizeof *info); 310 if (!x || !y || n == 0) return OMEGA_GPU_SCAN_BAD_ARGS; 311 if (n > OMEGA_GPU_SCAN_MAX_N) return OMEGA_GPU_SCAN_TOO_LARGE; 312 omega_gpu_session_lock(); 313 OmegaGpuMem m; 314 int rc = stage_locked(x, n, n, omega_gpu_scan_prefix_scratch_words(n), &m); 315 if (rc == OMEGA_GPU_SCAN_OK) { 316 ChipCtx c = { 0 }; uint32_t nl = 0; 317 rc = omega_gpu_scan_drive_prefix(n, m.va, m.va + n * 4u, m.va + 2 * n * 4u, chip_launch, &c, &nl); 318 if (rc == OMEGA_GPU_SCAN_OK && omega_gpu_session_sync_to_cpu(&m) != 0) rc = OMEGA_GPU_SCAN_CHIP_FAIL; 319 if (rc == OMEGA_GPU_SCAN_OK) { 320 const uint32_t *w = (const uint32_t *)m.cpu + n; 321 size_t unwritten = 0; 322 for (size_t i = 0; i < n; i++) unwritten += w[i] == POISON; 323 if (unwritten) { omega_gpu_session_set_error("prefix sum: output words never written"); rc = OMEGA_GPU_SCAN_CHIP_FAIL; } 324 else memcpy(y, w, n * 4u); 325 } 326 if (info) { info->launches = nl; info->elapsed_ns = c.elapsed_ns; } 327 omega_gpu_session_free(&m); 328 } 329 omega_gpu_session_unlock(); 330 if (info) info->call_ns = omega_gpu_session_now_ns() - t0; 331 return rc; 332 }