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/evidence/FB1-CUT4B-0396844/omega_gpu_matmul_api.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 / evidence / FB1-CUT4B-0396844 / omega_gpu_matmul_api.c` 375 lines ยท 16474 bytes Raw Blame History 1 /* 2 * omega_gpu_matmul_api.c: FB-1 cut 1b, on the shared session since cut 4b. 3 * See the header for the contract. 4 * 5 * Submission path: omega_gpu_session_launch (src/omega_gpu_session.c), the same 6 * words this file emitted in cut 1b (chip PASS FB1-CUT1B-`4345406`): SETUP_WORDS, 7 * shader-cache invalidate, constant bank upload, QMD0/QMD1 launch, first marker 8 * release, then the C3 tail (L2_FLUSH_DIRTY and a second release on uncached 9 * memory the host waits for before reading). Every buffer both sides touch is 10 * GPU-uncached (nvos.h NVOS32_ATTR2_GPU_CACHEABLE: "For system memory this will 11 * not be coherent with direct CPU mappings"). The device, channel, pushbuffer, 12 * constant bank, marker page and QMD page now belong to the session and are 13 * shared with the elementwise and attention APIs; this file keeps the resident 14 * tensors, the kernel cache (host and device copies) and its staging buffers. 15 */ 16 #include "omega_gpu_matmul_api.h" 17 #include "omega_gpu_session.h" 18 #include "omega_blackwell_codegen.h" 19 #include "omega_blackwell_matmul.h" 20 #include "omega_blackwell_qmd.h" 21 #include "omega_blackwell_submit.h" 22 #include "nvrm.h" 23 #include 24 #include 25 #include 26 #include 27 28 #define CACHE_SLOTS 8 29 #define MARKER_TIMEOUT_MS 20000ull /* a 1x2048x128256 call is ~0.1 s; 20 s is a stall, not a slow run */ 30 #define POISON_F32 (-999.0f) 31 #define ORACLE_TOL 1e-5 32 33 struct OmegaGpuTensor { 34 uint32_t k, n, kp, np; 35 NvrmMem mem; /* kp x np bf16 */ 36 }; 37 38 typedef struct { 39 bool valid; 40 uint32_t kp, np, grid_x; 41 int mutant; 42 OmegaMatMulSpec spec; 43 OmegaBlackwellKernel kernel; 44 NvrmMem code; /* resident copy of kernel.code */ 45 } CacheSlot; 46 47 static struct { 48 OmegaGpuScratch a_buf, c_buf; /* activation and result staging, grown to the high-water mark */ 49 uint32_t cta_budget; 50 int oracle, mutant; 51 bool hooked; 52 CacheSlot cache[CACHE_SLOTS]; 53 unsigned cache_next; 54 } g = { .cta_budget = OMEGA_GPU_MATMUL_MAX_CTAS, .oracle = 1 }; 55 56 #define FAILAT(s) (omega_gpu_session_set_error(s), 1) 57 const char *omega_gpu_matmul_last_error(void) { return omega_gpu_session_last_error(); } 58 59 static uint64_t now_ns(void) { 60 struct timespec t; 61 clock_gettime(CLOCK_MONOTONIC, &t); 62 return (uint64_t)t.tv_sec * 1000000000ull + (uint64_t)t.tv_nsec; 63 } 64 65 static uint32_t round_up(uint32_t v, uint32_t q) { return (v + q - 1) / q * q; } 66 67 const char *omega_gpu_matmul_rc_name(int rc) { 68 switch (rc) { 69 case OMEGA_GPU_MATMUL_OK: return "OK"; 70 case OMEGA_GPU_MATMUL_BAD_ARGS: return "BAD_ARGS"; 71 case OMEGA_GPU_MATMUL_TOO_LARGE: return "TOO_LARGE"; 72 case OMEGA_GPU_MATMUL_CODEGEN_FAIL: return "CODEGEN_FAIL"; 73 case OMEGA_GPU_MATMUL_CHIP_FAIL: return "CHIP_FAIL"; 74 case OMEGA_GPU_MATMUL_PARITY_FAIL: return "PARITY_FAIL"; 75 default: return "UNKNOWN"; 76 } 77 } 78 79 int omega_gpu_matmul_is_blocked(void) { return omega_gpu_session_is_blocked(); } 80 81 /* ---- device-side state (caller holds the session lock) ---- */ 82 83 static void cache_clear_locked(void) { 84 for (unsigned i = 0; i < CACHE_SLOTS; i++) { 85 CacheSlot *s = &g.cache[i]; 86 if (!s->valid) continue; 87 omega_blackwell_kernel_free(&s->kernel); 88 omega_gpu_session_free(&s->code); 89 memset(s, 0, sizeof *s); 90 } 91 g.cache_next = 0; 92 } 93 94 /* Called by the session (lock held, device still open) right before it closes. */ 95 static void on_session_close(void) { 96 cache_clear_locked(); 97 omega_gpu_session_scratch_free(&g.a_buf); 98 omega_gpu_session_scratch_free(&g.c_buf); 99 } 100 101 static OmegaGpuSession *dev_open_locked(void) { 102 if (!g.hooked) { (void)omega_gpu_session_on_close(on_session_close); g.hooked = true; } 103 return omega_gpu_session_open(); 104 } 105 106 void omega_gpu_device_close(void) { omega_gpu_session_close(); } 107 108 void omega_gpu_matmul_cache_clear(void) { 109 omega_gpu_session_lock(); 110 cache_clear_locked(); 111 omega_gpu_session_unlock(); 112 } 113 114 void omega_gpu_matmul_set_cta_budget(uint32_t ctas) { 115 omega_gpu_session_lock(); 116 g.cta_budget = ctas ? ctas : OMEGA_GPU_MATMUL_MAX_CTAS; 117 cache_clear_locked(); 118 omega_gpu_session_unlock(); 119 } 120 121 uint32_t omega_gpu_matmul_cta_budget(void) { return g.cta_budget; } 122 123 void omega_gpu_matmul_set_oracle(int on) { g.oracle = on ? 1 : 0; } 124 125 void omega_gpu_matmul_test_set_mutant(int on) { 126 omega_gpu_session_lock(); 127 g.mutant = on ? 1 : 0; 128 cache_clear_locked(); 129 omega_gpu_session_unlock(); 130 } 131 132 /* ---- kernel cache (caller holds the lock, device open) ---- */ 133 134 static int kernel_for(uint32_t kp, uint32_t np, uint32_t grid_x, CacheSlot **out, bool *hit) { 135 for (unsigned i = 0; i < CACHE_SLOTS; i++) { 136 CacheSlot *s = &g.cache[i]; 137 if (s->valid && s->kp == kp && s->np == np && s->grid_x == grid_x && s->mutant == g.mutant) { 138 *out = s; *hit = true; 139 return OMEGA_GPU_MATMUL_OK; 140 } 141 } 142 OmegaMatMulSpec spec; 143 /* the kernel ignores M; 16 keeps the spec inside the tile rule */ 144 if (omega_matmul_spec_init(&spec, 16, kp, np, OMEGA_MATMUL_PRECISION_BF16) != 0) return OMEGA_GPU_MATMUL_TOO_LARGE; 145 OmegaBlackwellKernel kernel; 146 memset(&kernel, 0, sizeof kernel); 147 if (omega_blackwell_codegen_matmul_tensor_loop(&spec, grid_x, g.mutant, &kernel) != 0 || !kernel.code || kernel.code_size == 0) 148 return OMEGA_GPU_MATMUL_CODEGEN_FAIL; 149 NvrmMem code; 150 if (omega_gpu_session_alloc(kernel.code_size, &code) != 0) { omega_blackwell_kernel_free(&kernel); return OMEGA_GPU_MATMUL_CHIP_FAIL; } 151 memcpy(code.cpu, kernel.code, kernel.code_size); 152 CacheSlot *s = &g.cache[g.cache_next]; 153 g.cache_next = (g.cache_next + 1) % CACHE_SLOTS; 154 if (s->valid) { omega_blackwell_kernel_free(&s->kernel); omega_gpu_session_free(&s->code); } 155 s->valid = true; s->kp = kp; s->np = np; s->grid_x = grid_x; s->mutant = g.mutant; 156 s->spec = spec; s->kernel = kernel; s->code = code; 157 *out = s; *hit = false; 158 return OMEGA_GPU_MATMUL_OK; 159 } 160 161 /* ---- one launch (caller holds the lock, device open, not blocked) ---- */ 162 163 static int launch(const CacheSlot *ks, uint64_t a_va, uint64_t b_va, uint64_t c_va, 164 uint32_t grid_x, uint32_t grid_y, uint64_t *elapsed_ns, uint32_t *marker_out) { 165 uint32_t cbank_args[OMEGA_BW_CBANK_MATMUL_ARGS_WORDS]; 166 omega_blackwell_build_cbank_args_matmul(cbank_args, a_va, b_va, c_va, grid_y * 16u, ks->kp, ks->np); 167 OmegaGpuLaunch L = { 168 .code_va = ks->code.va, .gpr_count = ks->kernel.gpr_count, 169 .threads_x = 32, .threads_y = 1, .grid_x = grid_x, .grid_y = grid_y, 170 .num_elements = (size_t)grid_x * grid_y * 128u, 171 .args = cbank_args, .n_args = OMEGA_BW_CBANK_MATMUL_ARGS_WORDS, .timeout_ms = MARKER_TIMEOUT_MS, 172 }; 173 return omega_gpu_session_launch(&L, elapsed_ns, marker_out) == 0 ? OMEGA_GPU_MATMUL_OK : OMEGA_GPU_MATMUL_CHIP_FAIL; 174 } 175 176 /* ---- tensors ---- */ 177 178 void omega_gpu_tensor_shape(const OmegaGpuTensor *t, uint32_t *k, uint32_t *n) { 179 if (k) *k = t ? t->k : 0; 180 if (n) *n = t ? t->n : 0; 181 } 182 183 int omega_gpu_tensor_upload_bf16(uint32_t k, uint32_t n, const uint16_t *b, OmegaGpuTensor **out) { 184 if (out) *out = NULL; 185 if (!b || !out || k == 0 || n == 0) return OMEGA_GPU_MATMUL_BAD_ARGS; 186 if (k > OMEGA_BW_MATMUL_MAX_K || n > OMEGA_BW_MATMUL_MAX_N) return OMEGA_GPU_MATMUL_TOO_LARGE; 187 OmegaGpuTensor *t = calloc(1, sizeof *t); 188 if (!t) return OMEGA_GPU_MATMUL_BAD_ARGS; 189 t->k = k; t->n = n; t->kp = round_up(k, OMEGA_GPU_MATMUL_TILE_K); t->np = round_up(n, OMEGA_GPU_MATMUL_TILE_N); 190 int rc = OMEGA_GPU_MATMUL_OK; 191 omega_gpu_session_lock(); 192 if (!dev_open_locked() || omega_gpu_session_alloc((size_t)t->kp * t->np * 2u, &t->mem) != 0) rc = OMEGA_GPU_MATMUL_CHIP_FAIL; 193 omega_gpu_session_unlock(); 194 if (rc != OMEGA_GPU_MATMUL_OK) { free(t); return rc; } 195 uint16_t *d = t->mem.cpu; 196 if (t->np != n || t->kp != k) memset(d, 0, (size_t)t->kp * t->np * 2u); 197 for (uint32_t r = 0; r < k; r++) memcpy(&d[(size_t)r * t->np], &b[(size_t)r * n], (size_t)n * 2u); 198 __asm__ volatile("dsb sy" ::: "memory"); 199 *out = t; 200 return OMEGA_GPU_MATMUL_OK; 201 } 202 203 int omega_gpu_tensor_upload_f32(uint32_t k, uint32_t n, const float *b, OmegaGpuTensor **out) { 204 if (out) *out = NULL; 205 if (!b || !out || k == 0 || n == 0) return OMEGA_GPU_MATMUL_BAD_ARGS; 206 if (k > OMEGA_BW_MATMUL_MAX_K || n > OMEGA_BW_MATMUL_MAX_N) return OMEGA_GPU_MATMUL_TOO_LARGE; 207 size_t cnt = (size_t)k * n; 208 uint16_t *h = malloc(cnt * sizeof *h); 209 if (!h) return OMEGA_GPU_MATMUL_BAD_ARGS; 210 for (size_t i = 0; i < cnt; i++) h[i] = omega_fp32_to_bf16(b[i]); 211 int rc = omega_gpu_tensor_upload_bf16(k, n, h, out); 212 free(h); 213 return rc; 214 } 215 216 void omega_gpu_tensor_free(OmegaGpuTensor *t) { 217 if (!t) return; 218 omega_gpu_session_lock(); 219 omega_gpu_session_free(&t->mem); 220 omega_gpu_session_unlock(); 221 free(t); 222 } 223 224 /* ---- the matmul ---- */ 225 226 static int matmul_core(uint32_t m, const uint16_t *a, const OmegaGpuTensor *b, float *c, OmegaGpuMatmulInfo *info) { 227 const uint32_t k = b->k, n = b->n, kp = b->kp, np = b->np; 228 const uint32_t mp = round_up(m, OMEGA_GPU_MATMUL_TILE_M), mt = mp / 16, nt = np / 8; 229 omega_gpu_session_lock(); 230 int rc = OMEGA_GPU_MATMUL_OK; 231 if (!dev_open_locked()) { omega_gpu_session_unlock(); return OMEGA_GPU_MATMUL_CHIP_FAIL; } 232 const uint32_t budget = g.cta_budget ? g.cta_budget : OMEGA_GPU_MATMUL_MAX_CTAS; 233 /* rows per launch: all of them unless the row tiles alone exceed the budget */ 234 uint32_t rows_per_launch = mt <= budget ? mp : budget * 16u; 235 uint32_t grid_y_full = rows_per_launch / 16u; 236 uint32_t grid_x = budget / grid_y_full; if (grid_x == 0) grid_x = 1; if (grid_x > nt) grid_x = nt; 237 if (info) { 238 info->m = m; info->k = k; info->n = n; info->padded_m = mp; info->padded_n = np; info->padded_k = kp; 239 info->k_slices = 1; info->rows_per_call = rows_per_launch; info->grid_x = grid_x; info->grid_y = grid_y_full; 240 info->resident = true; info->kernel_cache_hit = true; 241 strncpy(info->target_chip, "NVIDIA DGX Spark (Grace Blackwell GB10)", sizeof info->target_chip - 1); 242 info->sm_architecture = 121; 243 } 244 if (omega_gpu_session_scratch(&g.a_buf, (size_t)mp * kp * 2u) != 0 || omega_gpu_session_scratch(&g.c_buf, (size_t)mp * np * 4u) != 0) { 245 omega_gpu_session_unlock(); return OMEGA_GPU_MATMUL_CHIP_FAIL; 246 } 247 /* activation rows in, zero padded; result rows poisoned */ 248 uint16_t *ad = g.a_buf.mem.cpu; 249 if (mp != m || kp != k) memset(ad, 0, (size_t)mp * kp * 2u); 250 for (uint32_t r = 0; r < m; r++) memcpy(&ad[(size_t)r * kp], &a[(size_t)r * k], (size_t)k * 2u); 251 float *cd = g.c_buf.mem.cpu; 252 for (size_t i = 0; i < (size_t)m * np; i++) cd[i] = POISON_F32; 253 __asm__ volatile("dsb sy" ::: "memory"); 254 255 for (uint32_t r0 = 0; r0 < mp && rc == OMEGA_GPU_MATMUL_OK; r0 += rows_per_launch) { 256 uint32_t rows = mp - r0 < rows_per_launch ? mp - r0 : rows_per_launch; 257 uint32_t grid_y = rows / 16u; 258 CacheSlot *ks = NULL; bool hit = false; 259 rc = kernel_for(kp, np, grid_x, &ks, &hit); 260 if (rc != OMEGA_GPU_MATMUL_OK) break; 261 if (info && !hit) info->kernel_cache_hit = false; 262 rc = launch(ks, g.a_buf.mem.va + (uint64_t)r0 * kp * 2u, b->mem.va, g.c_buf.mem.va + (uint64_t)r0 * np * 4u, 263 grid_x, grid_y, info ? &info->elapsed_ns : NULL, info ? &info->completion_marker : NULL); 264 if (info) info->chip_calls++; 265 } 266 if (rc == OMEGA_GPU_MATMUL_OK) { 267 /* the chip must have written every real output word (poison check, as the engine does) */ 268 size_t unchanged = 0; 269 for (uint32_t r = 0; r < m; r++) { 270 const float *row = &cd[(size_t)r * np]; 271 for (uint32_t j = 0; j < n; j++) if (row[j] == POISON_F32) unchanged++; 272 memcpy(&c[(size_t)r * n], row, (size_t)n * sizeof *c); 273 } 274 if (unchanged && FAILAT("output poison survived")) rc = OMEGA_GPU_MATMUL_CHIP_FAIL; 275 } 276 omega_gpu_session_unlock(); 277 return rc; 278 } 279 280 static int resident_entry(uint32_t m, const uint16_t *a, const OmegaGpuTensor *b, float *c, OmegaGpuMatmulInfo *info) { 281 uint64_t t0 = now_ns(); 282 if (info) memset(info, 0, sizeof *info); 283 if (!a || !b || !c || m == 0 || !b->mem.cpu) return OMEGA_GPU_MATMUL_BAD_ARGS; 284 if (m > OMEGA_BW_MATMUL_MAX_M) return OMEGA_GPU_MATMUL_TOO_LARGE; 285 int rc = matmul_core(m, a, b, c, info); 286 if (info) info->call_ns = now_ns() - t0; 287 return rc; 288 } 289 290 int omega_gpu_matmul_resident_bf16(uint32_t m, const uint16_t *a, const OmegaGpuTensor *b, float *c, 291 OmegaGpuMatmulInfo *info) { 292 return resident_entry(m, a, b, c, info); 293 } 294 295 int omega_gpu_matmul_resident_f32(uint32_t m, const float *a, const OmegaGpuTensor *b, float *c, 296 OmegaGpuMatmulInfo *info) { 297 uint64_t t0 = now_ns(); 298 if (info) memset(info, 0, sizeof *info); 299 if (!a || !b || !c || m == 0 || !b->mem.cpu) return OMEGA_GPU_MATMUL_BAD_ARGS; 300 if (m > OMEGA_BW_MATMUL_MAX_M) return OMEGA_GPU_MATMUL_TOO_LARGE; 301 size_t cnt = (size_t)m * b->k; 302 uint16_t *h = malloc(cnt * sizeof *h); 303 if (!h) return OMEGA_GPU_MATMUL_BAD_ARGS; 304 for (size_t i = 0; i < cnt; i++) h[i] = omega_fp32_to_bf16(a[i]); 305 int rc = matmul_core(m, h, b, c, info); 306 free(h); 307 if (info) info->call_ns = now_ns() - t0; 308 return rc; 309 } 310 311 /* Host oracle: double accumulation of the bf16 inputs; an element mismatches when 312 * its error exceeds ORACLE_TOL of the accumulation scale (sum of |terms|). */ 313 static void oracle(uint32_t m, uint32_t k, uint32_t n, const uint16_t *a, const uint16_t *b, const float *c, OmegaGpuMatmulInfo *info) { 314 float max_abs = 0.0f, max_rel = 0.0f; 315 size_t mism = 0; 316 for (uint32_t i = 0; i < m; i++) { 317 for (uint32_t j = 0; j < n; j++) { 318 double acc = 0.0, scale = 0.0; 319 for (uint32_t t = 0; t < k; t++) { 320 double p = (double)omega_bf16_to_fp32(a[(size_t)i * k + t]) * (double)omega_bf16_to_fp32(b[(size_t)t * n + j]); 321 acc += p; scale += fabs(p); 322 } 323 double d = fabs(acc - (double)c[(size_t)i * n + j]); 324 double rel = d / (scale > 1e-6 ? scale : 1e-6); 325 if ((float)d > max_abs) max_abs = (float)d; 326 if ((float)rel > max_rel) max_rel = (float)rel; 327 if (rel > ORACLE_TOL) mism++; 328 } 329 } 330 info->max_abs_err = max_abs; info->max_rel_err = max_rel; info->mismatch_count = mism; 331 info->oracle_ran = true; info->parity_verified = (mism == 0); 332 } 333 334 int omega_gpu_matmul_bf16(uint32_t m, uint32_t k, uint32_t n, 335 const uint16_t *a, const uint16_t *b, float *c, 336 OmegaGpuMatmulInfo *info) { 337 uint64_t t0 = now_ns(); 338 OmegaGpuMatmulInfo local; 339 if (!info) info = &local; 340 memset(info, 0, sizeof *info); 341 if (!a || !b || !c || m == 0 || k == 0 || n == 0) return OMEGA_GPU_MATMUL_BAD_ARGS; 342 if (m > OMEGA_BW_MATMUL_MAX_M || k > OMEGA_BW_MATMUL_MAX_K || n > OMEGA_BW_MATMUL_MAX_N) 343 return OMEGA_GPU_MATMUL_TOO_LARGE; 344 OmegaGpuTensor *t = NULL; 345 int rc = omega_gpu_tensor_upload_bf16(k, n, b, &t); 346 if (rc != OMEGA_GPU_MATMUL_OK) return rc; 347 rc = matmul_core(m, a, t, c, info); 348 omega_gpu_tensor_free(t); 349 info->resident = false; 350 if (rc == OMEGA_GPU_MATMUL_OK && g.oracle) { 351 oracle(m, k, n, a, b, c, info); 352 if (!info->parity_verified) rc = OMEGA_GPU_MATMUL_PARITY_FAIL; 353 } 354 info->call_ns = now_ns() - t0; 355 return rc; 356 } 357 358 int omega_gpu_matmul_f32(uint32_t m, uint32_t k, uint32_t n, 359 const float *a, const float *b, float *c, 360 OmegaGpuMatmulInfo *info) { 361 if (info) memset(info, 0, sizeof *info); 362 if (!a || !b || !c || m == 0 || k == 0 || n == 0) return OMEGA_GPU_MATMUL_BAD_ARGS; 363 if (m > OMEGA_BW_MATMUL_MAX_M || k > OMEGA_BW_MATMUL_MAX_K || n > OMEGA_BW_MATMUL_MAX_N) 364 return OMEGA_GPU_MATMUL_TOO_LARGE; 365 size_t na = (size_t)m * k, nb = (size_t)k * n; 366 uint16_t *ab = malloc(na * sizeof *ab); 367 uint16_t *bb = malloc(nb * sizeof *bb); 368 if (!ab || !bb) { free(ab); free(bb); return OMEGA_GPU_MATMUL_BAD_ARGS; } 369 for (size_t i = 0; i < na; i++) ab[i] = omega_fp32_to_bf16(a[i]); 370 for (size_t i = 0; i < nb; i++) bb[i] = omega_fp32_to_bf16(b[i]); 371 int rc = omega_gpu_matmul_bf16(m, k, n, ab, bb, c, info); 372 free(ab); 373 free(bb); 374 return rc; 375 }