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_numeric_gb10.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_numeric_gb10.c` 335 lines ยท 15222 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* 18 * OMEGA-NUMERIC-0 GB10 executor. Every request passes 19 * omega_numeric_submit_check before any device is opened: unknown ops, 20 * ops without a GB10 encoding (the refused variants such as LDS.U8 or 21 * SHFL_UP; DIV, SQRT, EXP and LOG run as whole-program kernels, 22 * omega_numeric_op_whole_kernel), operand shapes the kernel cannot carry, and a 23 * patch that fails the structural check (shared-memory order and bounds, 24 * barriers, reduction order) are refused here with a message on stderr. The 25 * structural check runs again on the exact QMD that is submitted. The kernel is the calibrated vecadd with the op's patch words from 26 * omega_numeric_patch_words; there is no fallback instruction. 27 * Completion is observed the C3 way, like the ldst, reduce and divsqrt 28 * launchers: a GPU-uncached marker page (marker, then an L2 flush and a 29 * second marker), then the QMD's own release semaphore. A timeout is 30 * uncertain completion and poisons the process (omega_numeric_native.h). 31 */ 32 #include "omega_numeric.h" 33 #include "omega_numeric_divsqrt_gb10.h" 34 #include "omega_blackwell_codegen.h" 35 #include "omega_blackwell_qmd.h" 36 #include "omega_blackwell_submit.h" 37 #include "omega_numeric_native.h" 38 39 #include 40 #include 41 #include 42 #include 43 #include 44 #include "omega_gpu_code_alloc.h" 45 46 #define OMEGA_BW_SETUP_WORDS_COUNT 18 47 /* Device-failure diagnostics (M20 GB10 instrumentation). Every 48 * OMEGA_NUMERIC_ERR_DEVICE return below goes through gb10_devfail, which 49 * prints one GB10_DEVFAIL line to stderr: the step that failed, the driver 50 * return code, errno, the nvrm error text, the wait value and the marker word 51 * for waits, and the elapsed ms since the launch began. The lifecycle wrapper 52 * retains resources after uncertain completion and refuses later numeric 53 * launches. There is no retry and no wait value changes. */ 54 static double gb10_ms_since(const struct timespec *t0) { 55 struct timespec t; 56 timespec_get(&t, TIME_UTC); /* C11; no feature macro needed */ 57 return (double)(t.tv_sec - t0->tv_sec) * 1e3 + (double)(t.tv_nsec - t0->tv_nsec) / 1e6; 58 } 59 60 static int gb10_devfail(const char *fn, const char *step, M16NativeContext *ctx, int do_close, int drv_rc, 61 int saved_errno, long wait_ms, const volatile uint32_t *word, uint32_t want, 62 const struct timespec *t0) { 63 double ms = gb10_ms_since(t0); 64 fprintf(stderr, "GB10_DEVFAIL fn=%s step=%s drv_rc=%d errno=%d rm_err=\"%s\" live_allocs=%u faulted=%u", 65 fn, step, drv_rc, saved_errno, ctx->rm.err, (unsigned)ctx->rm.live_count, (unsigned)ctx->rm.faulted); 66 if (wait_ms >= 0) 67 fprintf(stderr, " wait_ms=%ld word=0x%08x want=0x%08x", wait_ms, word ? (unsigned)*word : 0u, (unsigned)want); 68 fprintf(stderr, " elapsed_ms=%.3f\n", ms); 69 if (do_close) omega_numeric_native_close(ctx); 70 return OMEGA_NUMERIC_ERR_DEVICE; 71 } 72 /* Plain step: drv_rc is the value the call returned. */ 73 #define GB10_FAIL(step, close_, rc_) gb10_devfail(__func__, (step), &ctx, (close_), (rc_), errno, -1, NULL, 0u, &t0) 74 /* Wait step: also log the wait value and the word read. */ 75 #define GB10_FAIL_WAIT(step, rc_, ms_, w_, want_) \ 76 gb10_devfail(__func__, (step), &ctx, 1, (rc_), errno, (long)(ms_), (w_), (want_), &t0) 77 78 static const uint32_t NUMERIC_SETUP_WORDS[OMEGA_BW_SETUP_WORDS_COUNT] = { 79 0x20012061, 0x0000cec0, 0x20012092, 0x00000001, 0x200120a8, 0x0000000f, 0x2001255d, 0x00000003, 80 0x2001255e, 0x20000000, 0x2001255f, 0x000fffff, 0x20012557, 0x00000003, 0x20012558, 0x22000000, 81 0x20012559, 0x00000000, 82 }; 83 84 int omega_gb10_execute_simt_op(const char *op_name, 85 const float *in_a, 86 const float *in_b, 87 const float *in_c, 88 float *out_res, 89 size_t count) { 90 char err[256]; 91 int check = omega_numeric_submit_check(op_name, in_a, in_b, in_c, out_res, count, err, sizeof(err)); 92 if (check != OMEGA_NUMERIC_OK) { 93 fprintf(stderr, "omega_gb10_execute_simt_op: refused before submission: %s\n", err); 94 return check; 95 } 96 const OmegaNumericOpInfo *info = omega_numeric_op_find(op_name); 97 98 if (omega_numeric_op_whole_kernel(info->op)) { 99 /* DIV/SQRT/EXP/LOG: the whole-program kernels of 100 * src/omega_numeric_divsqrt_gb10.c (E1 rows 7 and 10). omega_ds_gb10_run 101 * repeats the argument check and checks the kernel image, its 102 * nvdisasm-verified digest and the QMD before it opens the device. 103 * Inputs and outputs travel as bit patterns. */ 104 OmegaDsOp dop = omega_ds_op_for_numeric(info->op); 105 uint32_t *ua = malloc(count * sizeof(uint32_t)); 106 uint32_t *ub = malloc(count * sizeof(uint32_t)); 107 uint32_t *uo = malloc(count * sizeof(uint32_t)); 108 int drc = OMEGA_NUMERIC_ERR_DEVICE; 109 if (ua && ub && uo) { 110 memcpy(ua, in_a, count * sizeof(float)); 111 if (in_b) memcpy(ub, in_b, count * sizeof(float)); 112 else memset(ub, 0, count * sizeof(uint32_t)); 113 drc = omega_ds_gb10_run(dop, ua, in_b ? ub : NULL, uo, count); 114 if (drc == OMEGA_NUMERIC_OK) memcpy(out_res, uo, count * sizeof(float)); 115 } 116 free(ua); 117 free(ub); 118 free(uo); 119 return drc; 120 } 121 122 struct timespec t0; 123 timespec_get(&t0, TIME_UTC); 124 M16NativeContext ctx; 125 int drc_; 126 if ((drc_ = omega_numeric_native_open(&ctx)) != 0) return GB10_FAIL("open", 0, drc_); 127 if ((drc_ = m16_native_create_channel(&ctx)) != 0) return GB10_FAIL("channel", 1, drc_); 128 129 NvrmMem large_pb; 130 if ((drc_ = nvrm_alloc(&ctx.rm, 0x10000, &large_pb)) != 0) return GB10_FAIL("alloc_pb", 1, drc_); 131 ctx.pb_mem = large_pb; 132 133 size_t bytes = (count * sizeof(float) + 0xfffULL) & ~0xfffULL; 134 if (bytes < 0x1000) bytes = 0x1000; 135 136 NvrmMem code_mem, cbank_mem, a_mem, b_mem, c_mem, out_mem, marker_mem, qmd_mem; 137 if (nvrm_alloc(&ctx.rm, omega_gpu_code_alloc_bytes(0x1000), &code_mem) != 0 || /* 4 KB of code + 2 KB prefetch tail */ 138 nvrm_alloc(&ctx.rm, 0x1000, &cbank_mem) != 0 || 139 nvrm_alloc(&ctx.rm, bytes, &a_mem) != 0 || 140 nvrm_alloc(&ctx.rm, bytes, &b_mem) != 0 || 141 nvrm_alloc(&ctx.rm, bytes, &c_mem) != 0 || 142 nvrm_alloc(&ctx.rm, bytes, &out_mem) != 0 || 143 nvrm_alloc_gpu_uncached(&ctx.rm, 0x1000, &marker_mem) != 0 || 144 nvrm_alloc(&ctx.rm, 0x10000, &qmd_mem) != 0) { 145 return GB10_FAIL("alloc_buffers", 1, -1); 146 } 147 148 memcpy(a_mem.cpu, in_a, count * sizeof(float)); 149 if (in_b) memcpy(b_mem.cpu, in_b, count * sizeof(float)); 150 else memset(b_mem.cpu, 0, count * sizeof(float)); 151 if (in_c) memcpy(c_mem.cpu, in_c, count * sizeof(float)); 152 memset(out_mem.cpu, 0x55, count * sizeof(float)); 153 154 /* Calibrated sm_121 vecadd with this op's patch; no default instruction. */ 155 size_t out_code_len = 0; 156 if (omega_numeric_build_kernel(info->op, code_mem.cpu, 0x1000, &out_code_len) != OMEGA_NUMERIC_OK) { 157 return GB10_FAIL("build_kernel", 1, -1); 158 } 159 160 /* Driver constant bank. Word 223 (c[0x0][0x37c]) is loaded into R1 at 161 * kernel entry; FFMA reads its c operand from R1, so c travels here. */ 162 uint32_t cbank_data[OMEGA_BW_CBANK_DRIVER_WORDS]; 163 omega_blackwell_build_cbank_driver(cbank_data, cbank_mem.va); 164 cbank_data[223] = omega_float_to_bits((info->op == OMEGA_NOP_FFMA) ? in_c[0] : 1.0f); 165 166 uint32_t cbank_args[10]; 167 memset(cbank_args, 0, sizeof(cbank_args)); 168 cbank_args[0] = (uint32_t)a_mem.va; 169 cbank_args[1] = (uint32_t)(a_mem.va >> 32); 170 cbank_args[2] = (uint32_t)b_mem.va; 171 cbank_args[3] = (uint32_t)(b_mem.va >> 32); 172 cbank_args[4] = (uint32_t)out_mem.va; 173 cbank_args[5] = (uint32_t)(out_mem.va >> 32); 174 cbank_args[6] = (uint32_t)count; 175 cbank_args[7] = 0; 176 cbank_args[8] = (uint32_t)c_mem.va; 177 cbank_args[9] = (uint32_t)(c_mem.va >> 32); 178 179 memcpy(cbank_mem.cpu, cbank_data, sizeof(cbank_data)); 180 memcpy((uint8_t *)cbank_mem.cpu + 0x380, cbank_args, sizeof(cbank_args)); 181 182 uint64_t qmd0_va = qmd_mem.va; 183 uint64_t qmd1_va = qmd_mem.va + 0x1000; 184 uint64_t sem_va = qmd_mem.va + 0x2000; 185 uint64_t scratch_va = qmd_mem.va + 0x4000; 186 187 OmegaBlackwellQmdConfig qmd_cfg = { 188 .code_va = code_mem.va, 189 .cbank_va = cbank_mem.va, 190 .scratch_va = scratch_va, 191 .sem_va = sem_va, 192 .qmd0_va = qmd0_va, 193 .qmd1_va = qmd1_va, 194 .num_elements = (uint32_t)count, 195 }; 196 omega_numeric_launch_shape(count, &qmd_cfg.threads_per_block, &qmd_cfg.grid_width); 197 198 uint32_t qmd0_words[OMEGA_BW_QMD_WORDS]; 199 uint32_t qmd1_words[OMEGA_BW_QMD_WORDS]; 200 if (omega_blackwell_build_qmd0(qmd0_words, qmd0_va, qmd1_va) != 0 || 201 omega_blackwell_build_qmd1(qmd1_words, &qmd_cfg) != 0 || 202 omega_blackwell_verify_qmd_invariants(qmd1_words) != 0) { 203 return GB10_FAIL("qmd", 1, -1); 204 } 205 { 206 /* same structural check as submit_check, on the QMD actually submitted */ 207 OmegaNumericPatchInsn patch[OMEGA_NUMERIC_PATCH_MAX]; 208 int np = omega_numeric_patch_words(info->op, patch); 209 char perr[256]; 210 if (omega_numeric_check_patch(info->op, patch, np, qmd1_words, perr, sizeof(perr)) != OMEGA_NUMERIC_OK) { 211 fprintf(stderr, "omega_gb10_execute_simt_op: %s\n", perr); 212 omega_numeric_native_close(&ctx); 213 return OMEGA_NUMERIC_ERR_OPERANDS; 214 } 215 } 216 217 memcpy(qmd_mem.cpu, qmd0_words, sizeof(qmd0_words)); 218 memcpy((uint8_t *)qmd_mem.cpu + 0x1000, qmd1_words, sizeof(qmd1_words)); 219 220 volatile uint32_t *hsem = (volatile uint32_t *)((uint8_t *)qmd_mem.cpu + 0x2000); 221 volatile uint32_t *hmarker = (volatile uint32_t *)marker_mem.cpu; 222 *hsem = 0; 223 *hmarker = 0; 224 #ifndef OMEGA_C3_PROTECT_OFF 225 volatile uint32_t *hmarker2 = (volatile uint32_t *)((uint8_t *)marker_mem.cpu + 0x10); 226 *hmarker2 = 0; 227 #endif 228 __asm__ volatile("dsb sy" ::: "memory"); 229 230 uint32_t pb[1024]; 231 size_t pb_len = 0; 232 233 memcpy(&pb[pb_len], NUMERIC_SETUP_WORDS, sizeof(NUMERIC_SETUP_WORDS)); 234 pb_len += sizeof(NUMERIC_SETUP_WORDS) / 4; 235 236 pb[pb_len++] = nvrm_mthd(1, 0x0188, 2); 237 pb[pb_len++] = (uint32_t)(cbank_mem.va >> 32); 238 pb[pb_len++] = (uint32_t)cbank_mem.va; 239 pb[pb_len++] = nvrm_mthd(1, 0x0180, 2); 240 pb[pb_len++] = 0x00000380; 241 pb[pb_len++] = 0x00000001; 242 pb[pb_len++] = nvrm_mthd(1, 0x01b0, 1); 243 pb[pb_len++] = 0x00000041; 244 pb[pb_len++] = (224 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28); 245 memcpy(&pb[pb_len], cbank_data, 224 * 4); 246 pb_len += 224; 247 248 pb[pb_len++] = nvrm_mthd(1, 0x0188, 2); 249 pb[pb_len++] = (uint32_t)((cbank_mem.va + 0x380) >> 32); 250 pb[pb_len++] = (uint32_t)(cbank_mem.va + 0x380); 251 pb[pb_len++] = nvrm_mthd(1, 0x0180, 2); 252 pb[pb_len++] = 0x00000028; /* 10 words = 40 bytes */ 253 pb[pb_len++] = 0x00000001; 254 pb[pb_len++] = nvrm_mthd(1, 0x01b0, 1); 255 pb[pb_len++] = 0x00000041; 256 pb[pb_len++] = (10 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28); 257 memcpy(&pb[pb_len], cbank_args, 10 * 4); 258 pb_len += 10; 259 260 pb[pb_len++] = (98 << 16) | (1 << 13) | (0x0318 >> 2) | (2u << 28); 261 pb[pb_len++] = (1u << 30) | (uint32_t)((qmd0_va >> 40) & 0x1ff); 262 pb[pb_len++] = (uint32_t)(qmd0_va >> 8); 263 memcpy(&pb[pb_len], qmd0_words, 96 * 4); 264 pb_len += 96; 265 266 pb[pb_len++] = nvrm_mthd(1, 0x0188, 2); 267 pb[pb_len++] = (uint32_t)(sem_va >> 32); 268 pb[pb_len++] = (uint32_t)sem_va; 269 pb[pb_len++] = nvrm_mthd(1, 0x0180, 2); 270 pb[pb_len++] = 0x00000004; 271 pb[pb_len++] = 0x00000001; 272 pb[pb_len++] = nvrm_mthd(1, 0x01b0, 1); 273 pb[pb_len++] = 0x00000041; 274 pb[pb_len++] = (1 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28); 275 pb[pb_len++] = OMEGA_BW_SEMAPHORE_INTERMEDIATE_INIT; 276 277 pb[pb_len++] = (98 << 16) | (1 << 13) | (0x0318 >> 2) | (2u << 28); 278 pb[pb_len++] = (1u << 30) | (uint32_t)((qmd1_va >> 40) & 0x1ff); 279 pb[pb_len++] = (uint32_t)(qmd1_va >> 8); 280 memcpy(&pb[pb_len], qmd1_words, 96 * 4); 281 pb_len += 96; 282 283 pb[pb_len++] = nvrm_mthd(0, 0x005c, 5); 284 pb[pb_len++] = (uint32_t)marker_mem.va; 285 pb[pb_len++] = (uint32_t)(marker_mem.va >> 32); 286 pb[pb_len++] = OMEGA_BW_MARKER_COMPLETION_PAYLOAD; 287 pb[pb_len++] = 0; 288 pb[pb_len++] = 0x1 | (1u << 20); 289 #ifndef OMEGA_C3_PROTECT_OFF 290 /* C3 hardening, the same block as omega_ds_gb10_run (src/omega_numeric_divsqrt_gb10.c) 291 * and run_chunk (src/omega_numeric_reduce_gb10.c): after the WFI marker, flush GPU L2 292 * dirty lines to memory (NVC96F_MEM_OP_A..D = 0x28..0x34, D bits 31:27 OPERATION = 293 * L2_FLUSH_DIRTY 0x10), then a second WFI marker the host waits for before the 294 * readback. The marker page is GPU-uncached (`727dc03`), so this last write becomes 295 * CPU-visible when it lands, not when GPU L2 happens to drain. */ 296 pb[pb_len++] = nvrm_mthd(0, 0x0028, 4); 297 pb[pb_len++] = 0; 298 pb[pb_len++] = 0; 299 pb[pb_len++] = 0; 300 pb[pb_len++] = (0x10u << 27); 301 pb[pb_len++] = nvrm_mthd(0, 0x005c, 5); 302 pb[pb_len++] = (uint32_t)(marker_mem.va + 0x10); 303 pb[pb_len++] = (uint32_t)((marker_mem.va + 0x10) >> 32); 304 pb[pb_len++] = 0x46464646u; 305 pb[pb_len++] = 0; 306 pb[pb_len++] = 0x1 | (1u << 20); 307 #endif 308 309 if ((drc_ = m16_native_submit_methods(&ctx, pb, pb_len)) != 0) { 310 return GB10_FAIL("submit", 1, drc_); 311 } 312 313 /* A timeout is uncertain completion, even after a long wait: the same 600 s cap 314 * as the ldst, reduce and divsqrt launchers; the wrapper poisons the process. */ 315 if ((drc_ = omega_numeric_native_wait(hmarker, OMEGA_BW_MARKER_COMPLETION_PAYLOAD, 600000)) != 0) { 316 return GB10_FAIL_WAIT("marker_wait", drc_, 600000, hmarker, OMEGA_BW_MARKER_COMPLETION_PAYLOAD); 317 } 318 #ifndef OMEGA_C3_PROTECT_OFF 319 if ((drc_ = omega_numeric_native_wait(hmarker2, 0x46464646u, 600000)) != 0) { 320 return GB10_FAIL_WAIT("marker2_wait", drc_, 600000, hmarker2, 0x46464646u); 321 } 322 #endif 323 324 /* Match the reduction path: the host marker alone can precede the 325 * QMD release. Do not expose outputs until both have completed. */ 326 if ((drc_ = omega_numeric_native_wait(hsem, OMEGA_BW_SEMAPHORE_INTERMEDIATE_DONE, 600000)) != 0) { 327 return GB10_FAIL_WAIT("sem_wait", drc_, 600000, hsem, OMEGA_BW_SEMAPHORE_INTERMEDIATE_DONE); 328 } 329 __asm__ volatile("dsb sy" ::: "memory"); 330 331 memcpy(out_res, out_mem.cpu, count * sizeof(float)); 332 333 if (omega_numeric_native_close(&ctx) != 0) return OMEGA_NUMERIC_ERR_DEVICE; 334 return OMEGA_NUMERIC_OK; 335 }