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_blackwell_submit.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_blackwell_submit.c` 740 lines ยท 32217 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 #include 18 #include "omega_blackwell_submit.h" 19 #include "omega_blackwell_qmd.h" 20 #include "omega_blackwell_encoder.h" 21 #include "omega_blackwell_engine.h" 22 #include "omega_gpu_engine.h" 23 #include "m16_native.h" 24 #include "omega_gpu_wait.h" 25 #include 26 #include 27 #include 28 #include 29 #include 30 #include "omega_gpu_code_alloc.h" 31 32 /* Second completion marker payload for the C3 tail (marker page + 0x10), the same value as 33 * src/omega_blackwell_engine.c BE_MARKER2_PAYLOAD and src/omega_numeric_divsqrt_gb10.c:1152. */ 34 35 static const uint32_t SETUP_WORDS[18] = { 36 0x20012061, 0x0000cec0, 0x20012092, 0x00000001, 0x200120a8, 0x0000000f, 0x2001255d, 0x00000003, 37 0x2001255e, 0x20000000, 0x2001255f, 0x000fffff, 0x20012557, 0x00000003, 0x20012558, 0x22000000, 38 0x20012559, 0x00000000, 39 }; 40 41 static uint64_t current_time_ns(void) { 42 struct timespec t; 43 clock_gettime(CLOCK_MONOTONIC, &t); 44 return (uint64_t)t.tv_sec * 1000000000ULL + (uint64_t)t.tv_nsec; 45 } 46 47 int omega_blackwell_execute_vector(const OmegaVectorSpec *spec, 48 const OmegaBlackwellRealization *real, 49 const uint32_t *h_a, 50 const uint32_t *h_b, 51 uint32_t *h_c_out, 52 OmegaBlackwellVectorExecution *exec_info) { 53 if (!spec || !real || !h_a || !h_b || !h_c_out) return -1; 54 uint32_t n = spec->element_count; 55 if (n == 0) return -1; 56 57 if (exec_info) { 58 memset(exec_info, 0, sizeof(*exec_info)); 59 strncpy(exec_info->target_chip, "NVIDIA DGX Spark (Grace Blackwell GB10)", sizeof(exec_info->target_chip) - 1); 60 exec_info->sm_architecture = real->sm_architecture; 61 exec_info->element_count = n; 62 exec_info->zero_libcuda_linkage = (omega_blackwell_verify_zero_libcuda_linkage(NULL) == 0); 63 exec_info->zero_cuda_symbols = (omega_blackwell_verify_zero_cuda_symbols(NULL) == 0); 64 exec_info->zero_libcuda_runtime = (omega_blackwell_verify_zero_libcuda_runtime() == 0); 65 } 66 /* This executor no longer talks to the device itself. It builds the program 67 * and the poison words on the host, hands the job to omega_gpu_execute with 68 * the Blackwell backend (src/omega_blackwell_engine.c), and then does the 69 * arithmetic check here. The engine only checks that no output word is still 70 * equal to its poison word; it never judges the math. */ 71 72 /* Program bytes. Capacity 0x1000 as before (the code page was 0x1000). */ 73 uint8_t code[0x1000]; 74 size_t encoded_len = 0; 75 if (omega_blackwell_encode_vecadd(code, sizeof(code), &encoded_len) != 0 || encoded_len == 0) return -1; 76 77 /* Poison words. The expected output word is computed here, on the host, 78 * before launch: expected[i] = (a[i] + b[i]) mod 2^32 (the same sum 79 * omega_vector_verify_oracle uses). The poison word is the bit complement of 80 * it. A word and its bit complement always differ, so a poison word can 81 * never equal the expected output word at the same position. If the device 82 * leaves a word unwritten the engine sees poison and reports 83 * OUTPUT_UNCHANGED. (This replaces the old constant OMEGA_VECTOR_POISON_VALUE, 84 * which could in principle collide with a legitimate sum.) */ 85 uint32_t *poison = (uint32_t *)malloc((size_t)n * sizeof(uint32_t)); 86 if (!poison) return -1; 87 for (size_t i = 0; i < n; i++) { 88 poison[i] = ~(uint32_t)(h_a[i] + h_b[i]); 89 } 90 91 OmegaGpuJob job; 92 OmegaGpuResult res; 93 memset(&job, 0, sizeof(job)); 94 job.program = code; 95 job.program_len = encoded_len; 96 job.layout = OMEGA_GPU_LAYOUT_VECTOR_1D; 97 job.element_count = n; 98 job.input_a = h_a; 99 job.input_a_len = (size_t)n * sizeof(uint32_t); 100 job.input_b = h_b; 101 job.input_b_len = (size_t)n * sizeof(uint32_t); 102 job.output = h_c_out; 103 job.output_len = (size_t)n * sizeof(uint32_t); 104 job.poison = poison; 105 job.poison_count = n; 106 /* 5000 ms is the marker wait this executor always used (the old 107 * m16_native_wait_marker(..., 5000) call). The same limit is used for the 108 * second marker and the semaphore, which the old code did not wait for, and 109 * for the barrier, which has no wait. flags 0 means every protection ON: 110 * the L2 flush and second marker are now part of this launch. */ 111 job.timeouts.marker_ms = 5000; 112 job.timeouts.release_semaphore_ms = 5000; 113 job.timeouts.marker2_ms = 5000; 114 job.timeouts.visibility_ms = 5000; 115 job.flags = 0; 116 117 omega_gpu_engine_set_backend(omega_blackwell_engine_backend()); 118 int erc = omega_gpu_execute(&job, &res); 119 free(poison); 120 if (erc != OMEGA_GPU_ENGINE_OK) { 121 fprintf(stderr, "GB10_VECTOR_FAIL failure=%s step=%s state=%s drv_rc=%d errno=%d text=\"%s\"\n", 122 omega_gpu_engine_failure_name(res.failure), omega_gpu_engine_step_name(res.failed_step), 123 omega_gpu_engine_state_name(res.last_state), res.drv_rc, res.err_no, res.drv_text); 124 return -1; 125 } 126 127 /* The engine returns only pass or fail. The marker, the semaphore and the 128 * timestamps come from what the backend observed in its own wait callbacks 129 * (the engine result carries them only on failure). Both values were 130 * already required by the backend; checked again here because the gates 131 * depend on them: marker payload 0x44444444, semaphore DONE (6). */ 132 OmegaBlackwellEngineRunInfo info; 133 omega_blackwell_engine_run_info(&info); 134 if (info.marker != OMEGA_BW_MARKER_COMPLETION_PAYLOAD) return -1; 135 if (info.semaphore != OMEGA_BW_SEMAPHORE_INTERMEDIATE_DONE) return -1; 136 137 /* Verify against OMEGA semantic oracle */ 138 size_t mismatch_idx = 0; 139 int parity_res = omega_vector_verify_oracle(h_a, h_b, h_c_out, n, &mismatch_idx); 140 141 if (exec_info) { 142 exec_info->launch_timestamp_ns = info.launch_ns; 143 exec_info->completion_timestamp_ns = info.marker_done_ns; 144 exec_info->elapsed_ns = (info.marker_done_ns > info.launch_ns) ? (info.marker_done_ns - info.launch_ns) : 0; 145 exec_info->completion_marker = info.marker; 146 exec_info->intermediate_semaphore = info.semaphore; 147 exec_info->parity_verified = (parity_res == 0); 148 } 149 150 return parity_res; 151 } 152 153 /* CHIPWAIT-2: the two direct launchers wait through the shared primitive 154 * (src/omega_gpu_wait.c): exact-value wait on the marker with marker2 required 155 * (marker2 lands only after the L2 flush, so the output is in memory), progress 156 * stall 5000 ms (the old budget), hard total 600000 ms. One tagged line on failure. 157 * The engine-routed launcher keeps the engine's own timed waits. */ 158 static int bw_wait_one(const char *site, const char *what, volatile uint32_t *w, uint32_t want, 159 volatile uint32_t *m2, uint32_t m2want) { 160 omega_gpu_wait_cfg_t cfg; 161 memset(&cfg, 0, sizeof(cfg)); 162 cfg.progress_timeout_ms = 5000; 163 cfg.total_timeout_ms = 600000; 164 cfg.marker2 = m2; 165 cfg.marker2_want = m2want; 166 omega_gpu_wait_report_t r; 167 if (omega_gpu_wait_fixed(w, want, &r, &cfg)) return 0; 168 fprintf(stderr, 169 "BW_WAIT_FAIL site=%s wait=%s kind=%s result=%s expected=0x%x observed=0x%x " 170 "marker2_observed=0x%x elapsed_ns=%llu progress_count=%llu polls=%llu\n", 171 site, what, omega_gpu_wait_kind_name(r.wait_kind), omega_gpu_wait_result_name(r.result), 172 r.expected, r.last_observed, r.last_marker2, (unsigned long long)r.elapsed_ns, 173 (unsigned long long)r.progress_count, (unsigned long long)r.polls); 174 return -1; 175 } 176 177 int omega_blackwell_execute_matmul(const OmegaMatMulSpec *spec, 178 const OmegaBlackwellKernel *kernel, 179 const uint32_t *h_a, 180 const uint32_t *h_b, 181 uint32_t *h_c_out, 182 OmegaBlackwellMatMulExecution *exec_info) { 183 if (!spec || !kernel || !h_a || !h_b || !h_c_out) return -1; 184 if (!kernel->code || kernel->code_size == 0) return -1; 185 if (spec->m == 0 || spec->k == 0 || spec->n == 0) return -1; 186 187 if (exec_info) { 188 memset(exec_info, 0, sizeof(*exec_info)); 189 strncpy(exec_info->target_chip, "NVIDIA DGX Spark (Grace Blackwell GB10)", sizeof(exec_info->target_chip) - 1); 190 exec_info->sm_architecture = 121; 191 exec_info->m = spec->m; 192 exec_info->k = spec->k; 193 exec_info->n = spec->n; 194 exec_info->zero_libcuda_linkage = (omega_blackwell_verify_zero_libcuda_linkage(NULL) == 0); 195 exec_info->zero_cuda_symbols = (omega_blackwell_verify_zero_cuda_symbols(NULL) == 0); 196 exec_info->zero_libcuda_runtime = (omega_blackwell_verify_zero_libcuda_runtime() == 0); 197 } 198 199 M16NativeContext ctx; 200 if (m16_native_open(&ctx) != 0) { return -1; } 201 if (m16_native_create_channel(&ctx) != 0) { m16_native_close(&ctx); return -1; } 202 203 /* GB10: every buffer the CPU and GPU both touch is GPU-uncached. A plain nvrm_alloc forces 204 * GPU_CACHEABLE_YES, which nvos.h:1115-1118 says is not coherent with CPU mappings on 205 * system memory (R11 wrong results under load, hive-phases I37). */ 206 NvrmMem large_pb; 207 if (nvrm_alloc_gpu_uncached(&ctx.rm, 0x10000, &large_pb) != 0) { m16_native_close(&ctx); return -1; } 208 ctx.pb_mem = large_pb; 209 210 /* Sizing buffer allocations */ 211 size_t a_bytes = ((size_t)spec->m * spec->k * sizeof(uint32_t) + 0xFFFULL) & ~0xFFFULL; 212 if (a_bytes < 0x1000) a_bytes = 0x1000; 213 size_t b_bytes = ((size_t)spec->k * spec->n * sizeof(uint32_t) + 0xFFFULL) & ~0xFFFULL; 214 if (b_bytes < 0x1000) b_bytes = 0x1000; 215 size_t c_bytes = ((size_t)spec->m * spec->n * sizeof(uint32_t) + 0xFFFULL) & ~0xFFFULL; 216 if (c_bytes < 0x1000) c_bytes = 0x1000; 217 size_t code_bytes = omega_gpu_code_alloc_bytes(kernel->code_size); /* code + 2 KB prefetch tail, page rounded */ 218 if (code_bytes == 0) return -1; 219 220 NvrmMem code_mem, cbank_mem, a_mem, b_mem, c_mem, marker_mem, qmd_mem; 221 if (nvrm_alloc_gpu_uncached(&ctx.rm, code_bytes, &code_mem) != 0) { m16_native_close(&ctx); return -1; } 222 if (nvrm_alloc_gpu_uncached(&ctx.rm, 0x1000, &cbank_mem) != 0) { m16_native_close(&ctx); return -1; } 223 if (nvrm_alloc_gpu_uncached(&ctx.rm, a_bytes, &a_mem) != 0) { m16_native_close(&ctx); return -1; } 224 if (nvrm_alloc_gpu_uncached(&ctx.rm, b_bytes, &b_mem) != 0) { m16_native_close(&ctx); return -1; } 225 if (nvrm_alloc_gpu_uncached(&ctx.rm, c_bytes, &c_mem) != 0) { m16_native_close(&ctx); return -1; } 226 if (nvrm_alloc_gpu_uncached(&ctx.rm, 0x1000, &marker_mem) != 0) { m16_native_close(&ctx); return -1; } 227 if (nvrm_alloc_gpu_uncached(&ctx.rm, 0x10000, &qmd_mem) != 0) { m16_native_close(&ctx); return -1; } 228 229 /* Copy dynamic machine code into code_mem */ 230 memcpy(code_mem.cpu, kernel->code, kernel->code_size); 231 memset((uint8_t *)code_mem.cpu + kernel->code_size, 0, code_bytes - kernel->code_size); 232 233 /* Populate input buffers A, B and initialize C with poison */ 234 memcpy(a_mem.cpu, h_a, (size_t)spec->m * spec->k * sizeof(uint32_t)); 235 memcpy(b_mem.cpu, h_b, (size_t)spec->k * spec->n * sizeof(uint32_t)); 236 uint32_t *c_dev = (uint32_t *)c_mem.cpu; 237 for (size_t i = 0; i < (size_t)spec->m * spec->n; i++) { 238 c_dev[i] = OMEGA_VECTOR_POISON_VALUE; 239 } 240 241 /* 2D Tile Configuration: 16x16 threads per block */ 242 uint32_t threads_x = 16; 243 uint32_t threads_y = 16; 244 uint32_t grid_x = (spec->n + 15) / 16; 245 uint32_t grid_y = (spec->m + 15) / 16; 246 247 uint32_t cbank_data[OMEGA_BW_CBANK_DRIVER_WORDS]; 248 omega_blackwell_build_cbank_driver_2d(cbank_data, cbank_mem.va, threads_x, threads_y, grid_x, grid_y); 249 250 uint32_t cbank_args[OMEGA_BW_CBANK_MATMUL_ARGS_WORDS]; 251 omega_blackwell_build_cbank_args_matmul(cbank_args, a_mem.va, b_mem.va, c_mem.va, spec->m, spec->k, spec->n); 252 253 memcpy(cbank_mem.cpu, cbank_data, sizeof(cbank_data)); 254 memcpy((uint8_t *)cbank_mem.cpu + 0x380, cbank_args, sizeof(cbank_args)); 255 256 uint64_t qmd0_va = qmd_mem.va; 257 uint64_t qmd1_va = qmd_mem.va + 0x1000; 258 uint64_t sem_va = qmd_mem.va + 0x2000; 259 uint64_t scratch_va = qmd_mem.va + 0x4000; 260 261 OmegaBlackwellQmdConfig qmd_cfg = { 262 .code_va = code_mem.va, 263 .cbank_va = cbank_mem.va, 264 .scratch_va = scratch_va, 265 .sem_va = sem_va, 266 .qmd0_va = qmd0_va, 267 .qmd1_va = qmd1_va, 268 .num_elements = spec->m * spec->n, 269 .threads_per_block = 256, 270 .grid_width = grid_x * grid_y, 271 .threads_x = threads_x, 272 .threads_y = threads_y, 273 .grid_x = grid_x, 274 .grid_y = grid_y, 275 .gpr_count = (kernel->gpr_count > 16) ? ((kernel->gpr_count + 15) & ~15U) : 32 276 }; 277 278 uint32_t qmd0_words[OMEGA_BW_QMD_WORDS]; 279 uint32_t qmd1_words[OMEGA_BW_QMD_WORDS]; 280 omega_blackwell_build_qmd0(qmd0_words, qmd0_va, qmd1_va); 281 omega_blackwell_build_qmd1(qmd1_words, &qmd_cfg); 282 283 if (omega_blackwell_verify_qmd_invariants(qmd1_words) != 0) { m16_native_close(&ctx); return -1; } 284 285 memcpy(qmd_mem.cpu, qmd0_words, sizeof(qmd0_words)); 286 memcpy((uint8_t *)qmd_mem.cpu + 0x1000, qmd1_words, sizeof(qmd1_words)); 287 288 volatile uint32_t *hsem = (volatile uint32_t *)((uint8_t *)qmd_mem.cpu + 0x2000); 289 volatile uint32_t *hmarker = (volatile uint32_t *)marker_mem.cpu; 290 volatile uint32_t *hmarker2 = (volatile uint32_t *)((uint8_t *)marker_mem.cpu + 0x10); 291 *hsem = 0; 292 *hmarker = 0; 293 *hmarker2 = 0; 294 __asm__ volatile("dsb sy" ::: "memory"); 295 296 /* Assemble unified pushbuffer */ 297 uint32_t pb[1024]; 298 size_t pb_len = 0; 299 300 /* 1. Setup words (18 words) */ 301 memcpy(&pb[pb_len], SETUP_WORDS, sizeof(SETUP_WORDS)); 302 pb_len += sizeof(SETUP_WORDS) / 4; 303 304 /* 2. Cbank driver data upload (224 words via DMA) */ 305 pb[pb_len++] = nvrm_mthd(1, 0x0188, 2); 306 pb[pb_len++] = (uint32_t)(cbank_mem.va >> 32); 307 pb[pb_len++] = (uint32_t)cbank_mem.va; 308 pb[pb_len++] = nvrm_mthd(1, 0x0180, 2); 309 pb[pb_len++] = 0x00000380; 310 pb[pb_len++] = 0x00000001; 311 pb[pb_len++] = nvrm_mthd(1, 0x01b0, 1); 312 pb[pb_len++] = 0x00000041; 313 pb[pb_len++] = (224 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28); 314 memcpy(&pb[pb_len], cbank_data, 224 * 4); 315 pb_len += 224; 316 317 /* 3. Kernel args upload (10 words via DMA = 40 = 0x28 bytes) */ 318 pb[pb_len++] = nvrm_mthd(1, 0x0188, 2); 319 pb[pb_len++] = (uint32_t)((cbank_mem.va + 0x380) >> 32); 320 pb[pb_len++] = (uint32_t)(cbank_mem.va + 0x380); 321 pb[pb_len++] = nvrm_mthd(1, 0x0180, 2); 322 pb[pb_len++] = 0x00000028; 323 pb[pb_len++] = 0x00000001; 324 pb[pb_len++] = nvrm_mthd(1, 0x01b0, 1); 325 pb[pb_len++] = 0x00000041; 326 pb[pb_len++] = (10 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28); 327 memcpy(&pb[pb_len], cbank_args, 10 * 4); 328 pb_len += 10; 329 330 /* 4. Inline QMD 0 (98 words: 2 addr + 96 QMD words) */ 331 pb[pb_len++] = (98 << 16) | (1 << 13) | (0x0318 >> 2) | (2u << 28); 332 pb[pb_len++] = (1u << 30) | (uint32_t)((qmd0_va >> 40) & 0x1ff); 333 pb[pb_len++] = (uint32_t)(qmd0_va >> 8); 334 memcpy(&pb[pb_len], qmd0_words, 96 * 4); 335 pb_len += 96; 336 337 /* 5. Intermediate semaphore upload (1 word = 5 via DMA) */ 338 pb[pb_len++] = nvrm_mthd(1, 0x0188, 2); 339 pb[pb_len++] = (uint32_t)(sem_va >> 32); 340 pb[pb_len++] = (uint32_t)sem_va; 341 pb[pb_len++] = nvrm_mthd(1, 0x0180, 2); 342 pb[pb_len++] = 0x00000004; 343 pb[pb_len++] = 0x00000001; 344 pb[pb_len++] = nvrm_mthd(1, 0x01b0, 1); 345 pb[pb_len++] = 0x00000041; 346 pb[pb_len++] = (1 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28); 347 pb[pb_len++] = OMEGA_BW_SEMAPHORE_INTERMEDIATE_INIT; 348 349 /* 6. Inline QMD 1 (98 words: 2 addr + 96 QMD words) */ 350 pb[pb_len++] = (98 << 16) | (1 << 13) | (0x0318 >> 2) | (2u << 28); 351 pb[pb_len++] = (1u << 30) | (uint32_t)((qmd1_va >> 40) & 0x1ff); 352 pb[pb_len++] = (uint32_t)(qmd1_va >> 8); 353 memcpy(&pb[pb_len], qmd1_words, 96 * 4); 354 pb_len += 96; 355 356 /* 7. Subchannel 0 completion release (WFI) */ 357 pb[pb_len++] = nvrm_mthd(0, 0x005c, 5); 358 pb[pb_len++] = (uint32_t)marker_mem.va; 359 pb[pb_len++] = (uint32_t)(marker_mem.va >> 32); 360 pb[pb_len++] = OMEGA_BW_MARKER_COMPLETION_PAYLOAD; 361 pb[pb_len++] = 0; 362 pb[pb_len++] = 0x1 | (1u << 20); // RELEASE | WFI 363 364 /* 8. C3 tail, the same words as src/omega_numeric_gb10.c and 365 * src/omega_blackwell_engine.c: flush GPU L2 dirty lines (NVC96F_MEM_OP_A..D = 366 * 0x28..0x34, D bits 31:27 OPERATION = L2_FLUSH_DIRTY 0x10), then a second WFI 367 * release at marker page + 0x10 that the host waits for before the readback. */ 368 pb[pb_len++] = nvrm_mthd(0, 0x0028, 4); 369 pb[pb_len++] = 0; 370 pb[pb_len++] = 0; 371 pb[pb_len++] = 0; 372 pb[pb_len++] = (0x10u << 27); 373 pb[pb_len++] = nvrm_mthd(0, 0x005c, 5); 374 pb[pb_len++] = (uint32_t)(marker_mem.va + 0x10); 375 pb[pb_len++] = (uint32_t)((marker_mem.va + 0x10) >> 32); 376 pb[pb_len++] = OMEGA_BW_MARKER2_PAYLOAD; 377 pb[pb_len++] = 0; 378 pb[pb_len++] = 0x1 | (1u << 20); // RELEASE | WFI 379 380 /* Submit methods to hardware GPFIFO ring */ 381 uint64_t t_start = current_time_ns(); 382 if (m16_native_submit_methods(&ctx, pb, pb_len) != 0) { m16_native_close(&ctx); return -1; } 383 384 /* Wait for hardware completion marker */ 385 /* CHIPWAIT-2: marker with marker2 required (C3: the second release lands only after the L2 flush). */ 386 if (bw_wait_one(__func__, "marker", hmarker, OMEGA_BW_MARKER_COMPLETION_PAYLOAD, hmarker2, OMEGA_BW_MARKER2_PAYLOAD) != 0) { m16_native_close(&ctx); return -1; } 387 __asm__ volatile("dsb sy" ::: "memory"); 388 uint64_t t_end = current_time_ns(); 389 390 /* Validate intermediate semaphore updated to 6 */ 391 if (*hsem != OMEGA_BW_SEMAPHORE_INTERMEDIATE_DONE) { m16_native_close(&ctx); return -1; } 392 393 /* Copy device output back to host */ 394 memcpy(h_c_out, c_mem.cpu, (size_t)spec->m * spec->n * sizeof(uint32_t)); 395 396 /* Verify against OMEGA MatMul mathematical reference oracle */ 397 uint32_t *h_expected = (uint32_t *)malloc((size_t)spec->m * spec->n * sizeof(uint32_t)); 398 if (!h_expected) { m16_native_close(&ctx); return -1; } 399 400 omega_matmul_cpu_oracle_i32(h_a, h_b, h_expected, spec->m, spec->k, spec->n); 401 402 size_t mismatches = 0; 403 for (size_t i = 0; i < (size_t)spec->m * spec->n; i++) { 404 if (h_c_out[i] != h_expected[i]) { 405 mismatches++; 406 } 407 } 408 free(h_expected); 409 410 if (exec_info) { 411 exec_info->launch_timestamp_ns = t_start; 412 exec_info->completion_timestamp_ns = t_end; 413 exec_info->elapsed_ns = (t_end > t_start) ? (t_end - t_start) : 0; 414 exec_info->completion_marker = *hmarker; 415 exec_info->intermediate_semaphore = *hsem; 416 exec_info->mismatch_count = mismatches; 417 exec_info->parity_verified = (mismatches == 0); 418 } 419 420 m16_native_close(&ctx); 421 return (mismatches == 0) ? 0 : -1; 422 } 423 424 425 int omega_blackwell_execute_matmul_tensor(const OmegaMatMulSpec *spec, 426 const OmegaBlackwellKernel *kernel, 427 const void *h_a, 428 const void *h_b, 429 float *h_c_out, 430 OmegaBlackwellMatMulExecution *exec_info, 431 float *out_max_abs_err, 432 float *out_max_rel_err) { 433 if (!spec || !kernel || !h_a || !h_b || !h_c_out) return -1; 434 if (!kernel->code || kernel->code_size == 0) return -1; 435 if (spec->m == 0 || spec->k == 0 || spec->n == 0) return -1; 436 if (spec->precision != OMEGA_MATMUL_PRECISION_FP16 && spec->precision != OMEGA_MATMUL_PRECISION_BF16) return -1; 437 438 if (exec_info) { 439 memset(exec_info, 0, sizeof(*exec_info)); 440 strncpy(exec_info->target_chip, "NVIDIA DGX Spark (Grace Blackwell GB10)", sizeof(exec_info->target_chip) - 1); 441 exec_info->sm_architecture = 121; 442 exec_info->m = spec->m; 443 exec_info->k = spec->k; 444 exec_info->n = spec->n; 445 exec_info->zero_libcuda_linkage = (omega_blackwell_verify_zero_libcuda_linkage(NULL) == 0); 446 exec_info->zero_cuda_symbols = (omega_blackwell_verify_zero_cuda_symbols(NULL) == 0); 447 exec_info->zero_libcuda_runtime = (omega_blackwell_verify_zero_libcuda_runtime() == 0); 448 } 449 450 M16NativeContext ctx; 451 if (m16_native_open(&ctx) != 0) { return -1; } 452 if (m16_native_create_channel(&ctx) != 0) { m16_native_close(&ctx); return -1; } 453 454 /* GB10: all buffers GPU-uncached, see omega_blackwell_execute_matmul. */ 455 NvrmMem large_pb; 456 if (nvrm_alloc_gpu_uncached(&ctx.rm, 0x10000, &large_pb) != 0) { m16_native_close(&ctx); return -1; } 457 ctx.pb_mem = large_pb; 458 459 size_t a_elems = (size_t)spec->m * spec->k; 460 size_t b_elems = (size_t)spec->k * spec->n; 461 size_t c_elems = (size_t)spec->m * spec->n; 462 463 size_t a_bytes = ((a_elems * 2) + 0xFFFULL) & ~0xFFFULL; 464 if (a_bytes < 0x1000) a_bytes = 0x1000; 465 size_t b_bytes = ((b_elems * 2) + 0xFFFULL) & ~0xFFFULL; 466 if (b_bytes < 0x1000) b_bytes = 0x1000; 467 size_t c_bytes = ((c_elems * 4) + 0xFFFULL) & ~0xFFFULL; 468 if (c_bytes < 0x1000) c_bytes = 0x1000; 469 size_t code_bytes = omega_gpu_code_alloc_bytes(kernel->code_size); /* code + 2 KB prefetch tail, page rounded */ 470 if (code_bytes == 0) return -1; 471 472 NvrmMem code_mem, cbank_mem, a_mem, b_mem, c_mem, marker_mem, qmd_mem; 473 if (nvrm_alloc_gpu_uncached(&ctx.rm, code_bytes, &code_mem) != 0) { m16_native_close(&ctx); return -1; } 474 if (nvrm_alloc_gpu_uncached(&ctx.rm, 0x1000, &cbank_mem) != 0) { m16_native_close(&ctx); return -1; } 475 if (nvrm_alloc_gpu_uncached(&ctx.rm, a_bytes, &a_mem) != 0) { m16_native_close(&ctx); return -1; } 476 if (nvrm_alloc_gpu_uncached(&ctx.rm, b_bytes, &b_mem) != 0) { m16_native_close(&ctx); return -1; } 477 if (nvrm_alloc_gpu_uncached(&ctx.rm, c_bytes, &c_mem) != 0) { m16_native_close(&ctx); return -1; } 478 if (nvrm_alloc_gpu_uncached(&ctx.rm, 0x1000, &marker_mem) != 0) { m16_native_close(&ctx); return -1; } 479 if (nvrm_alloc_gpu_uncached(&ctx.rm, 0x10000, &qmd_mem) != 0) { m16_native_close(&ctx); return -1; } 480 481 /* Copy dynamic machine code into code_mem */ 482 memcpy(code_mem.cpu, kernel->code, kernel->code_size); 483 memset((uint8_t *)code_mem.cpu + kernel->code_size, 0, code_bytes - kernel->code_size); 484 485 /* Populate input buffers A, B and initialize C with poison */ 486 memcpy(a_mem.cpu, h_a, a_elems * 2); 487 memcpy(b_mem.cpu, h_b, b_elems * 2); 488 float *c_dev = (float *)c_mem.cpu; 489 for (size_t i = 0; i < c_elems; i++) { 490 c_dev[i] = -999.0f; 491 } 492 493 uint32_t threads_x = 32; 494 uint32_t threads_y = 1; 495 uint32_t grid_x = spec->n / 8; 496 uint32_t grid_y = spec->m / 16; 497 if (grid_x == 0) grid_x = 1; 498 if (grid_y == 0) grid_y = 1; 499 500 uint32_t cbank_data[OMEGA_BW_CBANK_DRIVER_WORDS]; 501 omega_blackwell_build_cbank_driver_2d(cbank_data, cbank_mem.va, threads_x, threads_y, grid_x, grid_y); 502 503 uint32_t cbank_args[OMEGA_BW_CBANK_MATMUL_ARGS_WORDS]; 504 omega_blackwell_build_cbank_args_matmul(cbank_args, a_mem.va, b_mem.va, c_mem.va, spec->m, spec->k, spec->n); 505 506 memcpy(cbank_mem.cpu, cbank_data, sizeof(cbank_data)); 507 memcpy((uint8_t *)cbank_mem.cpu + 0x380, cbank_args, sizeof(cbank_args)); 508 509 uint64_t qmd0_va = qmd_mem.va; 510 uint64_t qmd1_va = qmd_mem.va + 0x1000; 511 uint64_t sem_va = qmd_mem.va + 0x2000; 512 uint64_t scratch_va = qmd_mem.va + 0x4000; 513 514 OmegaBlackwellQmdConfig qmd_cfg = { 515 .code_va = code_mem.va, 516 .cbank_va = cbank_mem.va, 517 .scratch_va = scratch_va, 518 .sem_va = sem_va, 519 .qmd0_va = qmd0_va, 520 .qmd1_va = qmd1_va, 521 .num_elements = c_elems, 522 .threads_per_block = 32, 523 .grid_width = grid_x * grid_y, 524 .threads_x = threads_x, 525 .threads_y = threads_y, 526 .grid_x = grid_x, 527 .grid_y = grid_y, 528 .gpr_count = 64 529 }; 530 531 uint32_t qmd0_words[OMEGA_BW_QMD_WORDS]; 532 uint32_t qmd1_words[OMEGA_BW_QMD_WORDS]; 533 omega_blackwell_build_qmd0(qmd0_words, qmd0_va, qmd1_va); 534 omega_blackwell_build_qmd1(qmd1_words, &qmd_cfg); 535 536 if (omega_blackwell_verify_qmd_invariants(qmd1_words) != 0) { m16_native_close(&ctx); return -1; } 537 538 memcpy(qmd_mem.cpu, qmd0_words, sizeof(qmd0_words)); 539 memcpy((uint8_t *)qmd_mem.cpu + 0x1000, qmd1_words, sizeof(qmd1_words)); 540 541 volatile uint32_t *hsem = (volatile uint32_t *)((uint8_t *)qmd_mem.cpu + 0x2000); 542 volatile uint32_t *hmarker = (volatile uint32_t *)marker_mem.cpu; 543 volatile uint32_t *hmarker2 = (volatile uint32_t *)((uint8_t *)marker_mem.cpu + 0x10); 544 *hsem = 0; 545 *hmarker = 0; 546 *hmarker2 = 0; 547 __asm__ volatile("dsb sy" ::: "memory"); 548 549 uint32_t pb[1024]; 550 size_t pb_len = 0; 551 552 memcpy(&pb[pb_len], SETUP_WORDS, sizeof(SETUP_WORDS)); 553 pb_len += sizeof(SETUP_WORDS) / 4; 554 555 pb[pb_len++] = nvrm_mthd(1, 0x0188, 2); 556 pb[pb_len++] = (uint32_t)(cbank_mem.va >> 32); 557 pb[pb_len++] = (uint32_t)cbank_mem.va; 558 pb[pb_len++] = nvrm_mthd(1, 0x0180, 2); 559 pb[pb_len++] = 0x00000380; 560 pb[pb_len++] = 0x00000001; 561 pb[pb_len++] = nvrm_mthd(1, 0x01b0, 1); 562 pb[pb_len++] = 0x00000041; 563 pb[pb_len++] = (224 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28); 564 memcpy(&pb[pb_len], cbank_data, 224 * 4); 565 pb_len += 224; 566 567 pb[pb_len++] = nvrm_mthd(1, 0x0188, 2); 568 pb[pb_len++] = (uint32_t)((cbank_mem.va + 0x380) >> 32); 569 pb[pb_len++] = (uint32_t)(cbank_mem.va + 0x380); 570 pb[pb_len++] = nvrm_mthd(1, 0x0180, 2); 571 pb[pb_len++] = 0x00000028; 572 pb[pb_len++] = 0x00000001; 573 pb[pb_len++] = nvrm_mthd(1, 0x01b0, 1); 574 pb[pb_len++] = 0x00000041; 575 pb[pb_len++] = (10 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28); 576 memcpy(&pb[pb_len], cbank_args, 10 * 4); 577 pb_len += 10; 578 579 pb[pb_len++] = (98 << 16) | (1 << 13) | (0x0318 >> 2) | (2u << 28); 580 pb[pb_len++] = (1u << 30) | (uint32_t)((qmd0_va >> 40) & 0x1ff); 581 pb[pb_len++] = (uint32_t)(qmd0_va >> 8); 582 memcpy(&pb[pb_len], qmd0_words, 96 * 4); 583 pb_len += 96; 584 585 pb[pb_len++] = nvrm_mthd(1, 0x0188, 2); 586 pb[pb_len++] = (uint32_t)(sem_va >> 32); 587 pb[pb_len++] = (uint32_t)sem_va; 588 pb[pb_len++] = nvrm_mthd(1, 0x0180, 2); 589 pb[pb_len++] = 0x00000004; 590 pb[pb_len++] = 0x00000001; 591 pb[pb_len++] = nvrm_mthd(1, 0x01b0, 1); 592 pb[pb_len++] = 0x00000041; 593 pb[pb_len++] = (1 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28); 594 pb[pb_len++] = OMEGA_BW_SEMAPHORE_INTERMEDIATE_INIT; 595 596 pb[pb_len++] = (98 << 16) | (1 << 13) | (0x0318 >> 2) | (2u << 28); 597 pb[pb_len++] = (1u << 30) | (uint32_t)((qmd1_va >> 40) & 0x1ff); 598 pb[pb_len++] = (uint32_t)(qmd1_va >> 8); 599 memcpy(&pb[pb_len], qmd1_words, 96 * 4); 600 pb_len += 96; 601 602 pb[pb_len++] = nvrm_mthd(0, 0x005c, 5); 603 pb[pb_len++] = (uint32_t)marker_mem.va; 604 pb[pb_len++] = (uint32_t)(marker_mem.va >> 32); 605 pb[pb_len++] = OMEGA_BW_MARKER_COMPLETION_PAYLOAD; 606 pb[pb_len++] = 0; 607 pb[pb_len++] = 0x1 | (1u << 20); 608 609 /* C3 tail, same words as omega_blackwell_execute_matmul above: L2_FLUSH_DIRTY, then a 610 * second WFI release at marker page + 0x10 that the host waits for before the readback. */ 611 pb[pb_len++] = nvrm_mthd(0, 0x0028, 4); 612 pb[pb_len++] = 0; 613 pb[pb_len++] = 0; 614 pb[pb_len++] = 0; 615 pb[pb_len++] = (0x10u << 27); 616 pb[pb_len++] = nvrm_mthd(0, 0x005c, 5); 617 pb[pb_len++] = (uint32_t)(marker_mem.va + 0x10); 618 pb[pb_len++] = (uint32_t)((marker_mem.va + 0x10) >> 32); 619 pb[pb_len++] = OMEGA_BW_MARKER2_PAYLOAD; 620 pb[pb_len++] = 0; 621 pb[pb_len++] = 0x1 | (1u << 20); 622 623 uint64_t t_start = current_time_ns(); 624 if (m16_native_submit_methods(&ctx, pb, pb_len) != 0) { m16_native_close(&ctx); return -1; } 625 626 /* CHIPWAIT-2: marker with marker2 required (C3: the second release lands only after the L2 flush). */ 627 if (bw_wait_one(__func__, "marker", hmarker, OMEGA_BW_MARKER_COMPLETION_PAYLOAD, hmarker2, OMEGA_BW_MARKER2_PAYLOAD) != 0) { m16_native_close(&ctx); return -1; } 628 __asm__ volatile("dsb sy" ::: "memory"); 629 uint64_t t_end = current_time_ns(); 630 631 if (*hsem != OMEGA_BW_SEMAPHORE_INTERMEDIATE_DONE) { m16_native_close(&ctx); return -1; } 632 633 memcpy(h_c_out, c_mem.cpu, c_elems * sizeof(float)); 634 635 float *h_expected = (float *)malloc(c_elems * sizeof(float)); 636 if (!h_expected) { m16_native_close(&ctx); return -1; } 637 638 if (spec->precision == OMEGA_MATMUL_PRECISION_FP16) { 639 omega_matmul_cpu_oracle_f16((const uint16_t *)h_a, (const uint16_t *)h_b, h_expected, spec->m, spec->k, spec->n); 640 } else { 641 omega_matmul_cpu_oracle_bf16((const uint16_t *)h_a, (const uint16_t *)h_b, h_expected, spec->m, spec->k, spec->n); 642 } 643 644 float max_abs = 0.0f; 645 float max_rel = 0.0f; 646 size_t mismatches = 0; 647 for (size_t i = 0; i < c_elems; i++) { 648 float exp = h_expected[i]; 649 float act = h_c_out[i]; 650 float diff = fabsf(exp - act); 651 float rel = (fabsf(exp) > 1e-6f) ? (diff / fabsf(exp)) : diff; 652 if (diff > max_abs) max_abs = diff; 653 if (rel > max_rel) max_rel = rel; 654 if (diff > 1e-4f) { 655 mismatches++; 656 } 657 } 658 free(h_expected); 659 660 if (out_max_abs_err) *out_max_abs_err = max_abs; 661 if (out_max_rel_err) *out_max_rel_err = max_rel; 662 663 if (exec_info) { 664 exec_info->launch_timestamp_ns = t_start; 665 exec_info->completion_timestamp_ns = t_end; 666 exec_info->elapsed_ns = (t_end > t_start) ? (t_end - t_start) : 0; 667 exec_info->completion_marker = *hmarker; 668 exec_info->intermediate_semaphore = *hsem; 669 exec_info->mismatch_count = mismatches; 670 exec_info->parity_verified = (mismatches == 0); 671 } 672 673 m16_native_close(&ctx); 674 return (mismatches == 0) ? 0 : -1; 675 } 676 677 int omega_blackwell_verify_zero_libcuda_linkage(const char *binary_path) { 678 char cmd[512]; 679 if (binary_path) { 680 snprintf(cmd, sizeof(cmd), "ldd %s 2>/dev/null", binary_path); 681 } else { 682 snprintf(cmd, sizeof(cmd), "ldd /proc/self/exe 2>/dev/null"); 683 } 684 685 FILE *f = popen(cmd, "r"); 686 if (!f) return -1; 687 688 char line[512]; 689 bool found_cuda = false; 690 while (fgets(line, sizeof(line), f)) { 691 if (strstr(line, "libcuda.so") || strstr(line, "libcudart.so")) { 692 found_cuda = true; 693 break; 694 } 695 } 696 pclose(f); 697 return found_cuda ? -1 : 0; 698 } 699 700 int omega_blackwell_verify_zero_cuda_symbols(const char *binary_path) { 701 char cmd[512]; 702 if (binary_path) { 703 snprintf(cmd, sizeof(cmd), "nm -u %s 2>/dev/null", binary_path); 704 } else { 705 snprintf(cmd, sizeof(cmd), "nm -u /proc/self/exe 2>/dev/null"); 706 } 707 708 FILE *f = popen(cmd, "r"); 709 if (!f) return -1; 710 711 char line[512]; 712 bool found_cuda_sym = false; 713 while (fgets(line, sizeof(line), f)) { 714 char sym[256]; 715 if (sscanf(line, " %*s %255s", sym) == 1 || sscanf(line, "%255s", sym) == 1) { 716 if (strncmp(sym, "cu", 2) == 0 || strncmp(sym, "cuda", 4) == 0) { 717 found_cuda_sym = true; 718 break; 719 } 720 } 721 } 722 pclose(f); 723 return found_cuda_sym ? -1 : 0; 724 } 725 726 int omega_blackwell_verify_zero_libcuda_runtime(void) { 727 FILE *f = fopen("/proc/self/maps", "r"); 728 if (!f) return -1; 729 730 char line[512]; 731 bool found_cuda = false; 732 while (fgets(line, sizeof(line), f)) { 733 if (strstr(line, "libcuda") || strstr(line, "libcudart")) { 734 found_cuda = true; 735 break; 736 } 737 } 738 fclose(f); 739 return found_cuda ? -1 : 0; 740 }