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 / src / omega_world_gates.c
| 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 <math.h> |
| 18 | #include "omega_world_gates.h" |
| 19 | #include "omega_accelerator_world.h" |
| 20 | #include "omega_blackwell_gates.h" |
| 21 | #include "omega_blackwell_encoder.h" |
| 22 | #include "omega_blackwell_codegen.h" |
| 23 | #include "omega_blackwell_matmul.h" |
| 24 | #include "omega_blackwell_qmd.h" |
| 25 | #include "omega_blackwell_submit.h" |
| 26 | #include "omega_vector.h" |
| 27 | #include "sha256.h" |
| 28 | #include "omega_evidence.h" |
| 29 | #include "omega_gpu_wait.h" |
| 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
| 33 | #include <time.h> |
| 34 | #include <unistd.h> |
| 35 | #include <limits.h> |
| 36 | #include <sys/wait.h> |
| 37 | #include <fcntl.h> |
| 38 | #include "omega_physics_dir.h" |
| 39 | #include "omega_clean_clone.h" |
| 40 | |
| 41 | static int m19_gate_count = 0; |
| 42 | static int m19_gate_passed = 0; |
| 43 | static uint32_t m19_ring_capacity = 0; |
| 44 | static uint32_t m19_wrap_dispatches = 0; |
| 45 | static uint32_t m19_sustained_dispatches = 0; |
| 46 | static long m19_sustained_rss_delta = -1; |
| 47 | static uint8_t m19_sustained_digest[32]; |
| 48 | static char m19_candidate_commit[41] = {0}; |
| 49 | |
| 50 | /* Measured (not asserted) fault-recovery observations from gate 12; each |
| 51 | * stays false until the gate that measures it actually runs and observes |
| 52 | * the condition. */ |
| 53 | static bool m19_fault_channel_scoped_handled = false; |
| 54 | static bool m19_fault_channel_generation_advanced = false; |
| 55 | static bool m19_fault_vas_preserved = false; |
| 56 | static bool m19_fault_zero_device_reset = false; |
| 57 | |
| 58 | /* Resolve the full 40-hex revision of a working tree via git. */ |
| 59 | static bool m19_git_head(const char *repo_dir, char out[41]) { |
| 60 | /* No shell (omega#565): git gets the directory as an argument. */ |
| 61 | char line[64]; |
| 62 | char *argv[] = {"git", "-C", (char *)repo_dir, "rev-parse", "HEAD", NULL}; |
| 63 | if (!omega_cc_capture_line(NULL, "git", argv, line, sizeof(line))) return false; |
| 64 | if (strlen(line) != 40) return false; |
| 65 | memcpy(out, line, 41); |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | static const uint32_t WORLD_SETUP_WORDS[18] = { |
| 70 | 0x20012061, 0x0000cec0, 0x20012092, 0x00000001, 0x200120a8, 0x0000000f, 0x2001255d, 0x00000003, |
| 71 | 0x2001255e, 0x20000000, 0x2001255f, 0x000fffff, 0x20012557, 0x00000003, 0x20012558, 0x22000000, |
| 72 | 0x20012559, 0x00000000, |
| 73 | }; |
| 74 | |
| 75 | static long get_resident_pages(void) { |
| 76 | FILE *f = fopen("/proc/self/statm", "r"); |
| 77 | if (!f) return -1; |
| 78 | long total = 0, resident = 0; |
| 79 | if (fscanf(f, "%ld %ld", &total, &resident) != 2) resident = -1; |
| 80 | fclose(f); |
| 81 | return resident; |
| 82 | } |
| 83 | |
| 84 | static bool compute_file_sha256(const char *path, uint8_t digest[32], char hex[65]) { |
| 85 | FILE *f = fopen(path, "rb"); |
| 86 | if (!f) return false; |
| 87 | sha256_ctx ctx; |
| 88 | sha256_init(&ctx); |
| 89 | uint8_t buf[4096]; |
| 90 | size_t n; |
| 91 | while ((n = fread(buf, 1, sizeof(buf), f)) > 0) { |
| 92 | sha256_update(&ctx, buf, n); |
| 93 | } |
| 94 | fclose(f); |
| 95 | sha256_final(&ctx, digest); |
| 96 | for (int i = 0; i < 32; i++) { |
| 97 | sprintf(&hex[i * 2], "%02x", digest[i]); |
| 98 | } |
| 99 | hex[64] = 0; |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | void omega_get_m19_gate_snapshot(int *count, int *passed) { |
| 104 | if (count) *count = m19_gate_count; |
| 105 | if (passed) *passed = m19_gate_passed; |
| 106 | } |
| 107 | |
| 108 | static void report_m19_gate(const char *gate_name, bool pass, const char *detail) { |
| 109 | m19_gate_count++; |
| 110 | if (pass) { |
| 111 | m19_gate_passed++; |
| 112 | printf(" [PASS] %-45s : %s\n", gate_name, detail); |
| 113 | } else { |
| 114 | printf(" [FAIL] %-45s : %s\n", gate_name, detail); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /* Gate 1: OMEGA_ACCEL_RESIDENT_WORLD_CREATE_PASS */ |
| 119 | static bool test_m19_gate1_world_create(void) { |
| 120 | OmegaAcceleratorWorld world; |
| 121 | if (omega_world_init(&world) != OMEGA_WORLD_OK) return false; |
| 122 | if (world.current_epoch != 1) { omega_world_destroy(&world); return false; } |
| 123 | if (world.channel_generation != 1) { omega_world_destroy(&world); return false; } |
| 124 | if (!world.channel_active || !world.initialized) { omega_world_destroy(&world); return false; } |
| 125 | if (world.ring_capacity == 0) { omega_world_destroy(&world); return false; } |
| 126 | |
| 127 | if (omega_world_rebuild(&world) != OMEGA_WORLD_OK) return false; |
| 128 | if (world.current_epoch != 2) { omega_world_destroy(&world); return false; } |
| 129 | |
| 130 | omega_world_destroy(&world); |
| 131 | if (world.initialized || world.channel_active) return false; |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | /* Gate 2: OMEGA_ACCEL_RESIDENT_CONTEXT_REUSE_PASS */ |
| 136 | static bool test_m19_gate2_context_reuse(void) { |
| 137 | OmegaAcceleratorWorld world; |
| 138 | int rc = omega_world_init(&world); |
| 139 | if (rc != OMEGA_WORLD_OK) { |
| 140 | fprintf(stderr, "GATE_DIAG gate=OMEGA_ACCEL_RESIDENT_CONTEXT_REUSE_PASS exit=init_failed rc=%d x=%d\n", (int)(rc), 0); |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | const uint32_t root = world.m16.rm.root; |
| 145 | const uint32_t device = world.m16.rm.device; |
| 146 | const uint32_t vaspace = world.m16.rm.vaspace; |
| 147 | if (!root || !device || !vaspace) { |
| 148 | fprintf(stderr, "GATE_DIAG gate=OMEGA_ACCEL_RESIDENT_CONTEXT_REUSE_PASS exit=rm_handle_zero rc=%d root=%u device=%u vaspace=%u\n", (int)(-1), (unsigned)root, (unsigned)device, (unsigned)vaspace); |
| 149 | omega_world_destroy(&world); |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | uint8_t vecadd_buf[1024]; |
| 154 | size_t vecadd_len = 0; |
| 155 | omega_blackwell_encode_vecadd(vecadd_buf, sizeof(vecadd_buf), &vecadd_len); |
| 156 | OmegaHandle code_vecadd; |
| 157 | rc = omega_world_register_code(&world, vecadd_buf, vecadd_len, NULL, &code_vecadd); |
| 158 | if (rc != OMEGA_WORLD_OK) { |
| 159 | fprintf(stderr, "GATE_DIAG gate=OMEGA_ACCEL_RESIDENT_CONTEXT_REUSE_PASS exit=register_code_vecadd_failed rc=%d vecadd_len=%zu\n", (int)(rc), vecadd_len); |
| 160 | omega_world_destroy(&world); |
| 161 | return false; |
| 162 | } |
| 163 | OmegaHandle v_a, v_b, v_c; |
| 164 | omega_world_register_buffer(&world, 256, OMEGA_PERM_READ, &v_a); |
| 165 | omega_world_register_buffer(&world, 256, OMEGA_PERM_READ, &v_b); |
| 166 | omega_world_register_buffer(&world, 256, OMEGA_PERM_READ | OMEGA_PERM_WRITE, &v_c); |
| 167 | |
| 168 | uint32_t *va_cpu = NULL, *vb_cpu = NULL, *vc_cpu = NULL; |
| 169 | uint64_t dummy = 0; |
| 170 | omega_world_resolve_buffer(&world, &v_a, OMEGA_PERM_READ, 0, 256, (void **)&va_cpu, &dummy); |
| 171 | omega_world_resolve_buffer(&world, &v_b, OMEGA_PERM_READ, 0, 256, (void **)&vb_cpu, &dummy); |
| 172 | omega_world_resolve_buffer(&world, &v_c, OMEGA_PERM_READ, 0, 256, (void **)&vc_cpu, &dummy); |
| 173 | for (uint32_t i = 0; i < 64; i++) { |
| 174 | va_cpu[i] = i; |
| 175 | vb_cpu[i] = 10; |
| 176 | vc_cpu[i] = 0; |
| 177 | } |
| 178 | |
| 179 | uint32_t completion = 0; |
| 180 | rc = omega_world_dispatch_vector(&world, &code_vecadd, &v_a, &v_b, &v_c, 64, &completion); |
| 181 | if (rc != OMEGA_WORLD_OK || |
| 182 | vc_cpu[0] != 10 || vc_cpu[63] != 73) { |
| 183 | fprintf(stderr, "GATE_DIAG gate=OMEGA_ACCEL_RESIDENT_CONTEXT_REUSE_PASS exit=dispatch_vector_failed rc=%d vc0=%u(want 10) vc63=%u(want 73) faulted=%d total_dispatches=%llu\n", (int)(rc), (unsigned)vc_cpu[0], (unsigned)vc_cpu[63], (int)world.faulted, (unsigned long long)world.total_dispatches); |
| 184 | omega_world_destroy(&world); |
| 185 | return false; |
| 186 | } |
| 187 | |
| 188 | OmegaMatMulSpec spec_i32; |
| 189 | omega_matmul_spec_init(&spec_i32, 16, 16, 16, OMEGA_MATMUL_PRECISION_INT32); |
| 190 | OmegaBlackwellKernel k_i32; |
| 191 | omega_blackwell_codegen_matmul(&spec_i32, &k_i32); |
| 192 | OmegaHandle code_i32; |
| 193 | rc = omega_world_register_code(&world, k_i32.code, k_i32.code_size, NULL, &code_i32); |
| 194 | if (rc != OMEGA_WORLD_OK) { |
| 195 | fprintf(stderr, "GATE_DIAG gate=OMEGA_ACCEL_RESIDENT_CONTEXT_REUSE_PASS exit=register_code_i32_failed rc=%d code_size=%zu\n", (int)(rc), (size_t)k_i32.code_size); |
| 196 | omega_blackwell_kernel_free(&k_i32); |
| 197 | omega_world_destroy(&world); |
| 198 | return false; |
| 199 | } |
| 200 | OmegaHandle m_a, m_b, m_c; |
| 201 | omega_world_register_buffer(&world, 1024, OMEGA_PERM_READ, &m_a); |
| 202 | omega_world_register_buffer(&world, 1024, OMEGA_PERM_READ, &m_b); |
| 203 | omega_world_register_buffer(&world, 1024, OMEGA_PERM_READ | OMEGA_PERM_WRITE, &m_c); |
| 204 | uint32_t *ma_cpu = NULL, *mb_cpu = NULL, *mc_cpu = NULL; |
| 205 | omega_world_resolve_buffer(&world, &m_a, OMEGA_PERM_READ, 0, 1024, (void **)&ma_cpu, &dummy); |
| 206 | omega_world_resolve_buffer(&world, &m_b, OMEGA_PERM_READ, 0, 1024, (void **)&mb_cpu, &dummy); |
| 207 | omega_world_resolve_buffer(&world, &m_c, OMEGA_PERM_READ, 0, 1024, (void **)&mc_cpu, &dummy); |
| 208 | for (int i = 0; i < 256; i++) { |
| 209 | ma_cpu[i] = 1; |
| 210 | mb_cpu[i] = (i % 16 == i / 16) ? 1 : 0; |
| 211 | mc_cpu[i] = 0; |
| 212 | } |
| 213 | |
| 214 | rc = omega_world_dispatch_matmul(&world, &spec_i32, &code_i32, &m_a, &m_b, &m_c, &completion); |
| 215 | if (rc != OMEGA_WORLD_OK || |
| 216 | mc_cpu[0] != 1 || mc_cpu[255] != 1) { |
| 217 | fprintf(stderr, "GATE_DIAG gate=OMEGA_ACCEL_RESIDENT_CONTEXT_REUSE_PASS exit=dispatch_matmul_failed rc=%d mc0=%u(want 1) mc255=%u(want 1) faulted=%d total_dispatches=%llu\n", (int)(rc), (unsigned)mc_cpu[0], (unsigned)mc_cpu[255], (int)world.faulted, (unsigned long long)world.total_dispatches); |
| 218 | omega_blackwell_kernel_free(&k_i32); |
| 219 | omega_world_destroy(&world); |
| 220 | return false; |
| 221 | } |
| 222 | omega_blackwell_kernel_free(&k_i32); |
| 223 | |
| 224 | bool ok = (world.m16.rm.root == root && |
| 225 | world.m16.rm.device == device && |
| 226 | world.m16.rm.vaspace == vaspace && |
| 227 | world.total_dispatches == 2); |
| 228 | if (!ok) { |
| 229 | fprintf(stderr, "GATE_DIAG gate=OMEGA_ACCEL_RESIDENT_CONTEXT_REUSE_PASS exit=identity_mismatch rc=%d root_same=%d(%u/%u) device_same=%d(%u/%u) vaspace_same=%d(%u/%u) total_dispatches=%llu(want 2)\n", (int)(-1), (int)(world.m16.rm.root == root), (unsigned)world.m16.rm.root, (unsigned)root, (int)(world.m16.rm.device == device), (unsigned)world.m16.rm.device, (unsigned)device, (int)(world.m16.rm.vaspace == vaspace), (unsigned)world.m16.rm.vaspace, (unsigned)vaspace, (unsigned long long)world.total_dispatches); |
| 230 | } |
| 231 | omega_world_destroy(&world); |
| 232 | return ok; |
| 233 | } |
| 234 | |
| 235 | /* Gate 3: OMEGA_ACCEL_RESIDENT_CHANNEL_REUSE_PASS */ |
| 236 | static bool test_m19_gate3_channel_reuse(void) { |
| 237 | OmegaAcceleratorWorld world; |
| 238 | if (omega_world_init(&world) != OMEGA_WORLD_OK) return false; |
| 239 | |
| 240 | const uint32_t gpfifo = world.m16.rm.gpfifo; |
| 241 | const uint32_t chgroup = world.m16.rm.chgroup; |
| 242 | volatile uint32_t *const doorbell = world.m16.rm.doorbell; |
| 243 | if (!gpfifo || !chgroup || !doorbell) { |
| 244 | omega_world_destroy(&world); |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | uint8_t code_buf[1024]; |
| 249 | size_t code_len = 0; |
| 250 | omega_blackwell_encode_vecadd(code_buf, sizeof(code_buf), &code_len); |
| 251 | OmegaHandle code, a, b, c; |
| 252 | if (omega_world_register_code(&world, code_buf, code_len, NULL, &code) != OMEGA_WORLD_OK || |
| 253 | omega_world_register_buffer(&world, 256, OMEGA_PERM_READ, &a) != OMEGA_WORLD_OK || |
| 254 | omega_world_register_buffer(&world, 256, OMEGA_PERM_READ, &b) != OMEGA_WORLD_OK || |
| 255 | omega_world_register_buffer(&world, 256, OMEGA_PERM_READ | OMEGA_PERM_WRITE, &c) != OMEGA_WORLD_OK) { |
| 256 | omega_world_destroy(&world); |
| 257 | return false; |
| 258 | } |
| 259 | |
| 260 | uint32_t *pa = NULL, *pb = NULL, *pc = NULL; |
| 261 | uint64_t va = 0; |
| 262 | omega_world_resolve_buffer(&world, &a, OMEGA_PERM_READ, 0, 256, (void **)&pa, &va); |
| 263 | omega_world_resolve_buffer(&world, &b, OMEGA_PERM_READ, 0, 256, (void **)&pb, &va); |
| 264 | omega_world_resolve_buffer(&world, &c, OMEGA_PERM_READ, 0, 256, (void **)&pc, &va); |
| 265 | |
| 266 | bool ok = true; |
| 267 | uint32_t completion = 0; |
| 268 | for (uint32_t step = 0; step < 4 && ok; step++) { |
| 269 | for (uint32_t i = 0; i < 64; i++) { |
| 270 | pa[i] = step + i; |
| 271 | pb[i] = 10 + step; |
| 272 | pc[i] = 0; |
| 273 | } |
| 274 | ok = (omega_world_dispatch_vector(&world, &code, &a, &b, &c, 64, &completion) == OMEGA_WORLD_OK && |
| 275 | pc[0] == 10 + 2 * step && pc[63] == 73 + 2 * step && |
| 276 | world.m16.rm.gpfifo == gpfifo && |
| 277 | world.m16.rm.chgroup == chgroup && |
| 278 | world.m16.rm.doorbell == doorbell && |
| 279 | world.channel_generation == 1 && |
| 280 | world.channel_reconstructions == 0 && |
| 281 | world.total_dispatches == step + 1); |
| 282 | } |
| 283 | omega_world_destroy(&world); |
| 284 | return ok; |
| 285 | } |
| 286 | |
| 287 | /* Gate 4: OMEGA_ACCEL_RESIDENT_CODE_REGISTRY_PASS */ |
| 288 | static bool test_m19_gate4_code_registry(void) { |
| 289 | OmegaAcceleratorWorld world; |
| 290 | if (omega_world_init(&world) != OMEGA_WORLD_OK) return false; |
| 291 | |
| 292 | uint8_t vecadd_buf[1024]; |
| 293 | size_t vecadd_len = 0; |
| 294 | if (omega_blackwell_encode_vecadd(vecadd_buf, sizeof(vecadd_buf), &vecadd_len) != 0) { |
| 295 | omega_world_destroy(&world); |
| 296 | return false; |
| 297 | } |
| 298 | uint8_t real_id[32] = {0x42}; |
| 299 | OmegaHandle code_vec; |
| 300 | if (omega_world_register_code(&world, vecadd_buf, vecadd_len, real_id, &code_vec) != OMEGA_WORLD_OK) { |
| 301 | omega_world_destroy(&world); |
| 302 | return false; |
| 303 | } |
| 304 | if (code_vec.object_type != OMEGA_OBJ_CODE || !(code_vec.permissions & OMEGA_PERM_EXECUTE)) { |
| 305 | omega_world_destroy(&world); |
| 306 | return false; |
| 307 | } |
| 308 | |
| 309 | uint8_t expected_digest[32]; |
| 310 | sha256_hash(vecadd_buf, vecadd_len, expected_digest); |
| 311 | if (memcmp(world.code_entries[code_vec.object_id].code_digest, expected_digest, 32) != 0) { |
| 312 | omega_world_destroy(&world); |
| 313 | return false; |
| 314 | } |
| 315 | |
| 316 | const void *res_cpu = NULL; |
| 317 | uint64_t res_va = 0; |
| 318 | size_t res_size = 0; |
| 319 | if (omega_world_resolve_code(&world, &code_vec, &res_cpu, &res_va, &res_size) != OMEGA_WORLD_OK || |
| 320 | res_size != vecadd_len || res_va == 0) { |
| 321 | omega_world_destroy(&world); |
| 322 | return false; |
| 323 | } |
| 324 | |
| 325 | OmegaMatMulSpec spec_i32; |
| 326 | omega_matmul_spec_init(&spec_i32, 16, 16, 16, OMEGA_MATMUL_PRECISION_INT32); |
| 327 | OmegaBlackwellKernel k_i32; |
| 328 | omega_blackwell_codegen_matmul(&spec_i32, &k_i32); |
| 329 | OmegaHandle code_i32; |
| 330 | if (omega_world_register_code(&world, k_i32.code, k_i32.code_size, NULL, &code_i32) != OMEGA_WORLD_OK) { |
| 331 | omega_blackwell_kernel_free(&k_i32); |
| 332 | omega_world_destroy(&world); |
| 333 | return false; |
| 334 | } |
| 335 | omega_blackwell_kernel_free(&k_i32); |
| 336 | |
| 337 | OmegaMatMulSpec spec_f16; |
| 338 | omega_matmul_spec_init(&spec_f16, 16, 16, 16, OMEGA_MATMUL_PRECISION_FP16); |
| 339 | OmegaBlackwellKernel k_f16; |
| 340 | omega_blackwell_codegen_matmul(&spec_f16, &k_f16); |
| 341 | OmegaHandle code_f16; |
| 342 | if (omega_world_register_code(&world, k_f16.code, k_f16.code_size, NULL, &code_f16) != OMEGA_WORLD_OK) { |
| 343 | omega_blackwell_kernel_free(&k_f16); |
| 344 | omega_world_destroy(&world); |
| 345 | return false; |
| 346 | } |
| 347 | omega_blackwell_kernel_free(&k_f16); |
| 348 | |
| 349 | OmegaMatMulSpec spec_bf16; |
| 350 | omega_matmul_spec_init(&spec_bf16, 16, 16, 16, OMEGA_MATMUL_PRECISION_BF16); |
| 351 | OmegaBlackwellKernel k_bf16; |
| 352 | omega_blackwell_codegen_matmul(&spec_bf16, &k_bf16); |
| 353 | OmegaHandle code_bf16; |
| 354 | if (omega_world_register_code(&world, k_bf16.code, k_bf16.code_size, NULL, &code_bf16) != OMEGA_WORLD_OK) { |
| 355 | omega_blackwell_kernel_free(&k_bf16); |
| 356 | omega_world_destroy(&world); |
| 357 | return false; |
| 358 | } |
| 359 | omega_blackwell_kernel_free(&k_bf16); |
| 360 | |
| 361 | omega_world_destroy(&world); |
| 362 | return true; |
| 363 | } |
| 364 | |
| 365 | /* Gate 5: OMEGA_ACCEL_RESIDENT_BUFFER_REGISTRY_PASS */ |
| 366 | static bool test_m19_gate5_buffer_registry(void) { |
| 367 | if (sizeof(OmegaHandle) != 20) return false; |
| 368 | |
| 369 | OmegaAcceleratorWorld world; |
| 370 | if (omega_world_init(&world) != OMEGA_WORLD_OK) return false; |
| 371 | |
| 372 | OmegaHandle h_rw; |
| 373 | if (omega_world_register_buffer(&world, 4096, OMEGA_PERM_READ | OMEGA_PERM_WRITE, &h_rw) != OMEGA_WORLD_OK) { |
| 374 | omega_world_destroy(&world); |
| 375 | return false; |
| 376 | } |
| 377 | |
| 378 | void *cpu = NULL; |
| 379 | uint64_t gpu_va = 0; |
| 380 | if (omega_world_resolve_buffer(&world, &h_rw, OMEGA_PERM_READ, 0, 4096, &cpu, &gpu_va) != OMEGA_WORLD_OK || |
| 381 | gpu_va != world.buffers[h_rw.object_id].gpu_va) { |
| 382 | omega_world_destroy(&world); |
| 383 | return false; |
| 384 | } |
| 385 | |
| 386 | if (omega_world_resolve_buffer(&world, &h_rw, OMEGA_PERM_READ, 4000, 200, &cpu, &gpu_va) != OMEGA_WORLD_ERR_BOUNDS) { |
| 387 | omega_world_destroy(&world); |
| 388 | return false; |
| 389 | } |
| 390 | |
| 391 | OmegaHandle forged = h_rw; |
| 392 | forged.object_id = 127; |
| 393 | if (omega_world_resolve_buffer(&world, &forged, OMEGA_PERM_READ, 0, 100, &cpu, &gpu_va) != OMEGA_WORLD_ERR_NOT_FOUND) { |
| 394 | omega_world_destroy(&world); |
| 395 | return false; |
| 396 | } |
| 397 | |
| 398 | OmegaHandle h_ro; |
| 399 | if (omega_world_register_buffer(&world, 4096, OMEGA_PERM_READ, &h_ro) != OMEGA_WORLD_OK) { |
| 400 | omega_world_destroy(&world); |
| 401 | return false; |
| 402 | } |
| 403 | if (omega_world_resolve_buffer(&world, &h_ro, OMEGA_PERM_WRITE, 0, 100, &cpu, &gpu_va) != OMEGA_WORLD_ERR_PERM_DENIED) { |
| 404 | omega_world_destroy(&world); |
| 405 | return false; |
| 406 | } |
| 407 | if (omega_world_resolve_buffer(&world, &h_ro, OMEGA_PERM_READ, 0, 100, &cpu, &gpu_va) != OMEGA_WORLD_OK) { |
| 408 | omega_world_destroy(&world); |
| 409 | return false; |
| 410 | } |
| 411 | |
| 412 | void *c1 = NULL, *c2 = NULL; |
| 413 | uint64_t va1 = 0, va2 = 0; |
| 414 | size_t off1 = 0, off2 = 0; |
| 415 | if (omega_world_scratch_acquire(&world, 4096, &c1, &va1, &off1) != OMEGA_WORLD_OK || |
| 416 | omega_world_scratch_acquire(&world, 4096, &c2, &va2, &off2) != OMEGA_WORLD_OK || |
| 417 | va2 < va1 + 4096) { |
| 418 | omega_world_destroy(&world); |
| 419 | return false; |
| 420 | } |
| 421 | omega_world_scratch_reset(&world); |
| 422 | if (world.scratch.allocated_bytes != 0) { |
| 423 | omega_world_destroy(&world); |
| 424 | return false; |
| 425 | } |
| 426 | |
| 427 | omega_world_destroy(&world); |
| 428 | return true; |
| 429 | } |
| 430 | |
| 431 | /* Gate 6: OMEGA_ACCEL_RESIDENT_MIXED_WORKLOAD_PASS */ |
| 432 | static bool test_m19_gate6_mixed_workload(void) { |
| 433 | OmegaAcceleratorWorld world; |
| 434 | if (omega_world_init(&world) != OMEGA_WORLD_OK) return false; |
| 435 | |
| 436 | uint8_t vecadd_buf[1024]; |
| 437 | size_t vecadd_len = 0; |
| 438 | omega_blackwell_encode_vecadd(vecadd_buf, sizeof(vecadd_buf), &vecadd_len); |
| 439 | OmegaHandle code_vecadd; |
| 440 | omega_world_register_code(&world, vecadd_buf, vecadd_len, NULL, &code_vecadd); |
| 441 | |
| 442 | OmegaMatMulSpec spec_i32, spec_f16, spec_bf16; |
| 443 | omega_matmul_spec_init(&spec_i32, 16, 16, 16, OMEGA_MATMUL_PRECISION_INT32); |
| 444 | omega_matmul_spec_init(&spec_f16, 16, 16, 16, OMEGA_MATMUL_PRECISION_FP16); |
| 445 | omega_matmul_spec_init(&spec_bf16, 16, 16, 16, OMEGA_MATMUL_PRECISION_BF16); |
| 446 | |
| 447 | OmegaBlackwellKernel k_i32, k_f16, k_bf16; |
| 448 | omega_blackwell_codegen_matmul(&spec_i32, &k_i32); |
| 449 | omega_blackwell_codegen_matmul(&spec_f16, &k_f16); |
| 450 | omega_blackwell_codegen_matmul(&spec_bf16, &k_bf16); |
| 451 | |
| 452 | OmegaHandle code_i32, code_f16, code_bf16; |
| 453 | omega_world_register_code(&world, k_i32.code, k_i32.code_size, NULL, &code_i32); |
| 454 | omega_world_register_code(&world, k_f16.code, k_f16.code_size, NULL, &code_f16); |
| 455 | omega_world_register_code(&world, k_bf16.code, k_bf16.code_size, NULL, &code_bf16); |
| 456 | |
| 457 | uint32_t vec_n = 1024; |
| 458 | size_t vec_bytes = vec_n * sizeof(uint32_t); |
| 459 | OmegaHandle v_a, v_b, v_c; |
| 460 | omega_world_register_buffer(&world, vec_bytes, OMEGA_PERM_READ, &v_a); |
| 461 | omega_world_register_buffer(&world, vec_bytes, OMEGA_PERM_READ, &v_b); |
| 462 | omega_world_register_buffer(&world, vec_bytes, OMEGA_PERM_READ | OMEGA_PERM_WRITE, &v_c); |
| 463 | |
| 464 | size_t mat_bytes_i32 = 16 * 16 * 4; |
| 465 | size_t mat_bytes_f16 = 16 * 16 * 2; |
| 466 | size_t mat_bytes_out = 16 * 16 * 4; |
| 467 | |
| 468 | OmegaHandle m_a_i32, m_b_i32, m_c_i32; |
| 469 | omega_world_register_buffer(&world, mat_bytes_i32, OMEGA_PERM_READ, &m_a_i32); |
| 470 | omega_world_register_buffer(&world, mat_bytes_i32, OMEGA_PERM_READ, &m_b_i32); |
| 471 | omega_world_register_buffer(&world, mat_bytes_out, OMEGA_PERM_READ | OMEGA_PERM_WRITE, &m_c_i32); |
| 472 | |
| 473 | OmegaHandle m_a_tensor, m_b_tensor, m_c_tensor; |
| 474 | omega_world_register_buffer(&world, mat_bytes_f16, OMEGA_PERM_READ, &m_a_tensor); |
| 475 | omega_world_register_buffer(&world, mat_bytes_f16, OMEGA_PERM_READ, &m_b_tensor); |
| 476 | omega_world_register_buffer(&world, mat_bytes_out, OMEGA_PERM_READ | OMEGA_PERM_WRITE, &m_c_tensor); |
| 477 | |
| 478 | uint32_t *va_cpu = NULL, *vb_cpu = NULL, *vc_cpu = NULL; |
| 479 | uint64_t dummy = 0; |
| 480 | omega_world_resolve_buffer(&world, &v_a, OMEGA_PERM_READ, 0, vec_bytes, (void **)&va_cpu, &dummy); |
| 481 | omega_world_resolve_buffer(&world, &v_b, OMEGA_PERM_READ, 0, vec_bytes, (void **)&vb_cpu, &dummy); |
| 482 | omega_world_resolve_buffer(&world, &v_c, OMEGA_PERM_READ, 0, vec_bytes, (void **)&vc_cpu, &dummy); |
| 483 | |
| 484 | uint32_t *mai_cpu = NULL, *mbi_cpu = NULL, *mci_cpu = NULL; |
| 485 | omega_world_resolve_buffer(&world, &m_a_i32, OMEGA_PERM_READ, 0, mat_bytes_i32, (void **)&mai_cpu, &dummy); |
| 486 | omega_world_resolve_buffer(&world, &m_b_i32, OMEGA_PERM_READ, 0, mat_bytes_i32, (void **)&mbi_cpu, &dummy); |
| 487 | omega_world_resolve_buffer(&world, &m_c_i32, OMEGA_PERM_READ, 0, mat_bytes_out, (void **)&mci_cpu, &dummy); |
| 488 | |
| 489 | uint16_t *mat_cpu = NULL, *mbt_cpu = NULL; |
| 490 | float *mct_cpu = NULL; |
| 491 | omega_world_resolve_buffer(&world, &m_a_tensor, OMEGA_PERM_READ, 0, mat_bytes_f16, (void **)&mat_cpu, &dummy); |
| 492 | omega_world_resolve_buffer(&world, &m_b_tensor, OMEGA_PERM_READ, 0, mat_bytes_f16, (void **)&mbt_cpu, &dummy); |
| 493 | omega_world_resolve_buffer(&world, &m_c_tensor, OMEGA_PERM_READ, 0, mat_bytes_out, (void **)&mct_cpu, &dummy); |
| 494 | |
| 495 | for (uint32_t i = 0; i < vec_n; i++) { |
| 496 | va_cpu[i] = i * 2; |
| 497 | vb_cpu[i] = 3; |
| 498 | } |
| 499 | for (uint32_t i = 0; i < 256; i++) { |
| 500 | mai_cpu[i] = (i % 5) + 1; |
| 501 | mbi_cpu[i] = (i % 7) + 1; |
| 502 | mat_cpu[i] = 0x3c00; /* 1.0 */ |
| 503 | mbt_cpu[i] = (i % 16 == i / 16) ? 0x4000 : 0x0000; /* 2.0 on diagonal */ |
| 504 | } |
| 505 | |
| 506 | uint32_t compl = 0; |
| 507 | /* 1. VecAdd */ |
| 508 | if (omega_world_dispatch_vector(&world, &code_vecadd, &v_a, &v_b, &v_c, vec_n, &compl) != OMEGA_WORLD_OK) return false; |
| 509 | size_t mismatch_idx = 0; |
| 510 | if (omega_vector_verify_oracle(va_cpu, vb_cpu, vc_cpu, vec_n, &mismatch_idx) != 0) return false; |
| 511 | |
| 512 | /* 2. INT32 MatMul */ |
| 513 | if (omega_world_dispatch_matmul(&world, &spec_i32, &code_i32, &m_a_i32, &m_b_i32, &m_c_i32, &compl) != OMEGA_WORLD_OK) return false; |
| 514 | for (int r = 0; r < 16; r++) { |
| 515 | for (int c = 0; c < 16; c++) { |
| 516 | uint32_t expected = 0; |
| 517 | for (int k = 0; k < 16; k++) { |
| 518 | expected += mai_cpu[r * 16 + k] * mbi_cpu[k * 16 + c]; |
| 519 | } |
| 520 | if (mci_cpu[r * 16 + c] != expected) return false; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | /* 3. FP16 MatMul */ |
| 525 | if (omega_world_dispatch_matmul(&world, &spec_f16, &code_f16, &m_a_tensor, &m_b_tensor, &m_c_tensor, &compl) != OMEGA_WORLD_OK) return false; |
| 526 | for (int i = 0; i < 256; i++) { |
| 527 | if (fabsf(mct_cpu[i] - 2.0f) > 1e-4f) return false; |
| 528 | } |
| 529 | |
| 530 | /* 4. BF16 MatMul */ |
| 531 | for (uint32_t i = 0; i < 256; i++) { |
| 532 | mat_cpu[i] = 0x3f80; /* BF16 1.0 */ |
| 533 | mbt_cpu[i] = (i % 16 == i / 16) ? 0x4040 : 0x0000; /* BF16 3.0 */ |
| 534 | } |
| 535 | if (omega_world_dispatch_matmul(&world, &spec_bf16, &code_bf16, &m_a_tensor, &m_b_tensor, &m_c_tensor, &compl) != OMEGA_WORLD_OK) return false; |
| 536 | for (int i = 0; i < 256; i++) { |
| 537 | if (fabsf(mct_cpu[i] - 3.0f) > 1e-4f) return false; |
| 538 | } |
| 539 | |
| 540 | if (world.channel_generation != 1 || world.channel_reconstructions != 0 || world.total_dispatches != 4) { |
| 541 | return false; |
| 542 | } |
| 543 | |
| 544 | omega_blackwell_kernel_free(&k_i32); |
| 545 | omega_blackwell_kernel_free(&k_f16); |
| 546 | omega_blackwell_kernel_free(&k_bf16); |
| 547 | omega_world_destroy(&world); |
| 548 | return true; |
| 549 | } |
| 550 | |
| 551 | /* CHIPWAIT-1c: second completion word (same layout as the world dispatch path, see |
| 552 | * WORLD_MARKER2_OFFSET in omega_accelerator_world.c) and the sentinel the last entry |
| 553 | * of each batch writes after an L2 flush (value from src/omega_numeric_gb10.c). */ |
| 554 | #define M19_WRAP_MARKER2_OFFSET 0x10 |
| 555 | #define M19_WRAP_MARKER2_SENTINEL 0x46464646u |
| 556 | |
| 557 | /* Every QUEUE_WRAP failure exit prints one named reason (init, ring_discovery, |
| 558 | * enqueue, completion_timeout, wrap_count, channel_state, output_parity) so a |
| 559 | * failed run says which exit fired. Pass/fail decisions are unchanged. */ |
| 560 | static void m19_wrap_exit(const char *reason, const char *detail) { |
| 561 | printf("QUEUE_WRAP_FAIL reason=%s %s\n", reason, detail); |
| 562 | } |
| 563 | |
| 564 | /* Gate 7: OMEGA_ACCEL_RESIDENT_QUEUE_WRAP_PASS */ |
| 565 | static bool test_m19_gate7_queue_wrap(void) { |
| 566 | OmegaAcceleratorWorld world; |
| 567 | char detail[256]; |
| 568 | if (omega_world_init(&world) != OMEGA_WORLD_OK) { |
| 569 | m19_wrap_exit("init", "omega_world_init failed"); |
| 570 | return false; |
| 571 | } |
| 572 | |
| 573 | uint32_t ring_cap = 0; |
| 574 | int ring_rc = omega_world_discover_ring_capacity(&world, &ring_cap); |
| 575 | if (ring_rc != OMEGA_WORLD_OK || ring_cap == 0) { |
| 576 | snprintf(detail, sizeof(detail), "rc=%d ring_capacity=%u", ring_rc, ring_cap); |
| 577 | m19_wrap_exit("ring_discovery", detail); |
| 578 | omega_world_destroy(&world); |
| 579 | return false; |
| 580 | } |
| 581 | /* Observed facts: a failure after discovery still reports the capacity. */ |
| 582 | m19_ring_capacity = ring_cap; |
| 583 | |
| 584 | uint32_t stress_count = 3 * ring_cap; /* 3072 dispatches */ |
| 585 | |
| 586 | uint8_t code_buf[1024]; |
| 587 | size_t code_len = 0; |
| 588 | omega_blackwell_encode_vecadd(code_buf, sizeof(code_buf), &code_len); |
| 589 | OmegaHandle code_h; |
| 590 | omega_world_register_code(&world, code_buf, code_len, NULL, &code_h); |
| 591 | |
| 592 | uint32_t n = 64; |
| 593 | size_t n_bytes = n * sizeof(uint32_t); |
| 594 | OmegaHandle h_a, h_b, h_c; |
| 595 | omega_world_register_buffer(&world, n_bytes, OMEGA_PERM_READ, &h_a); |
| 596 | omega_world_register_buffer(&world, n_bytes, OMEGA_PERM_READ, &h_b); |
| 597 | omega_world_register_buffer(&world, n_bytes, OMEGA_PERM_WRITE | OMEGA_PERM_READ, &h_c); |
| 598 | |
| 599 | uint32_t *a_cpu = NULL, *b_cpu = NULL, *c_cpu = NULL; |
| 600 | uint64_t a_va = 0, b_va = 0, c_va = 0, code_va = 0; |
| 601 | size_t code_sz = 0; |
| 602 | omega_world_resolve_code(&world, &code_h, NULL, &code_va, &code_sz); |
| 603 | omega_world_resolve_buffer(&world, &h_a, OMEGA_PERM_READ, 0, n_bytes, (void **)&a_cpu, &a_va); |
| 604 | omega_world_resolve_buffer(&world, &h_b, OMEGA_PERM_READ, 0, n_bytes, (void **)&b_cpu, &b_va); |
| 605 | omega_world_resolve_buffer(&world, &h_c, OMEGA_PERM_WRITE, 0, n_bytes, (void **)&c_cpu, &c_va); |
| 606 | |
| 607 | for (uint32_t j = 0; j < n; j++) { |
| 608 | a_cpu[j] = j; |
| 609 | b_cpu[j] = 10; |
| 610 | c_cpu[j] = 0; |
| 611 | } |
| 612 | |
| 613 | void *cbank_cpu = NULL, *qmd0_cpu = NULL, *qmd1_cpu = NULL, *sem_cpu = NULL; |
| 614 | uint64_t cbank_va = 0, qmd0_va = 0, qmd1_va = 0, sem_va = 0; |
| 615 | size_t dummy_off = 0; |
| 616 | omega_world_scratch_acquire(&world, 0x1000, &cbank_cpu, &cbank_va, &dummy_off); |
| 617 | omega_world_scratch_acquire(&world, 0x1000, &qmd0_cpu, &qmd0_va, &dummy_off); |
| 618 | omega_world_scratch_acquire(&world, 0x1000, &qmd1_cpu, &qmd1_va, &dummy_off); |
| 619 | omega_world_scratch_acquire(&world, 0x1000, &sem_cpu, &sem_va, &dummy_off); |
| 620 | |
| 621 | uint32_t cbank_data[OMEGA_BW_CBANK_DRIVER_WORDS]; |
| 622 | omega_blackwell_build_cbank_driver(cbank_data, cbank_va); |
| 623 | uint32_t cbank_args[OMEGA_BW_CBANK_ARGS_WORDS]; |
| 624 | omega_blackwell_build_cbank_args(cbank_args, a_va, b_va, c_va, n); |
| 625 | memcpy(cbank_cpu, cbank_data, sizeof(cbank_data)); |
| 626 | memcpy((uint8_t *)cbank_cpu + 0x380, cbank_args, sizeof(cbank_args)); |
| 627 | |
| 628 | OmegaBlackwellQmdConfig qmd_cfg = { |
| 629 | .code_va = code_va, |
| 630 | .cbank_va = cbank_va, |
| 631 | .scratch_va = cbank_va + 0x2000, |
| 632 | .sem_va = sem_va, |
| 633 | .qmd0_va = qmd0_va, |
| 634 | .qmd1_va = qmd1_va, |
| 635 | .num_elements = n, |
| 636 | .threads_per_block = 64, |
| 637 | .grid_width = 1 |
| 638 | }; |
| 639 | uint32_t qmd0_words[OMEGA_BW_QMD_WORDS], qmd1_words[OMEGA_BW_QMD_WORDS]; |
| 640 | omega_blackwell_build_qmd0(qmd0_words, qmd0_va, qmd1_va); |
| 641 | omega_blackwell_build_qmd1(qmd1_words, &qmd_cfg); |
| 642 | memcpy(qmd0_cpu, qmd0_words, sizeof(qmd0_words)); |
| 643 | memcpy(qmd1_cpu, qmd1_words, sizeof(qmd1_words)); |
| 644 | |
| 645 | volatile uint32_t *hsem = (volatile uint32_t *)sem_cpu; |
| 646 | *hsem = 0; |
| 647 | *world.completion.cpu_marker = 0; |
| 648 | |
| 649 | uint32_t pb[1024]; |
| 650 | size_t pb_len = 0; |
| 651 | memcpy(&pb[pb_len], WORLD_SETUP_WORDS, sizeof(WORLD_SETUP_WORDS)); |
| 652 | pb_len += sizeof(WORLD_SETUP_WORDS) / 4; |
| 653 | |
| 654 | pb[pb_len++] = nvrm_mthd(1, 0x0188, 2); |
| 655 | pb[pb_len++] = (uint32_t)(cbank_va >> 32); |
| 656 | pb[pb_len++] = (uint32_t)cbank_va; |
| 657 | pb[pb_len++] = nvrm_mthd(1, 0x0180, 2); |
| 658 | pb[pb_len++] = 0x00000380; |
| 659 | pb[pb_len++] = 0x00000001; |
| 660 | pb[pb_len++] = nvrm_mthd(1, 0x01b0, 1); |
| 661 | pb[pb_len++] = 0x00000041; |
| 662 | pb[pb_len++] = (224 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28); |
| 663 | memcpy(&pb[pb_len], cbank_data, 224 * 4); |
| 664 | pb_len += 224; |
| 665 | |
| 666 | pb[pb_len++] = nvrm_mthd(1, 0x0188, 2); |
| 667 | pb[pb_len++] = (uint32_t)((cbank_va + 0x380) >> 32); |
| 668 | pb[pb_len++] = (uint32_t)(cbank_va + 0x380); |
| 669 | pb[pb_len++] = nvrm_mthd(1, 0x0180, 2); |
| 670 | pb[pb_len++] = 0x0000001c; |
| 671 | pb[pb_len++] = 0x00000001; |
| 672 | pb[pb_len++] = nvrm_mthd(1, 0x01b0, 1); |
| 673 | pb[pb_len++] = 0x00000041; |
| 674 | pb[pb_len++] = (7 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28); |
| 675 | memcpy(&pb[pb_len], cbank_args, 7 * 4); |
| 676 | pb_len += 7; |
| 677 | |
| 678 | pb[pb_len++] = (98 << 16) | (1 << 13) | (0x0318 >> 2) | (2u << 28); |
| 679 | pb[pb_len++] = (1u << 30) | (uint32_t)((qmd0_va >> 40) & 0x1ff); |
| 680 | pb[pb_len++] = (uint32_t)(qmd0_va >> 8); |
| 681 | memcpy(&pb[pb_len], qmd0_words, 96 * 4); |
| 682 | pb_len += 96; |
| 683 | |
| 684 | pb[pb_len++] = nvrm_mthd(1, 0x0188, 2); |
| 685 | pb[pb_len++] = (uint32_t)(sem_va >> 32); |
| 686 | pb[pb_len++] = (uint32_t)sem_va; |
| 687 | pb[pb_len++] = nvrm_mthd(1, 0x0180, 2); |
| 688 | pb[pb_len++] = 0x00000004; |
| 689 | pb[pb_len++] = 0x00000001; |
| 690 | pb[pb_len++] = nvrm_mthd(1, 0x01b0, 1); |
| 691 | pb[pb_len++] = 0x00000041; |
| 692 | pb[pb_len++] = (1 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28); |
| 693 | pb[pb_len++] = OMEGA_BW_SEMAPHORE_INTERMEDIATE_INIT; |
| 694 | |
| 695 | pb[pb_len++] = (98 << 16) | (1 << 13) | (0x0318 >> 2) | (2u << 28); |
| 696 | pb[pb_len++] = (1u << 30) | (uint32_t)((qmd1_va >> 40) & 0x1ff); |
| 697 | pb[pb_len++] = (uint32_t)(qmd1_va >> 8); |
| 698 | memcpy(&pb[pb_len], qmd1_words, 96 * 4); |
| 699 | pb_len += 96; |
| 700 | |
| 701 | size_t release_idx = pb_len; |
| 702 | pb[pb_len++] = nvrm_mthd(0, 0x005c, 5); |
| 703 | pb[pb_len++] = (uint32_t)world.completion.gpu_va; |
| 704 | pb[pb_len++] = (uint32_t)(world.completion.gpu_va >> 32); |
| 705 | pb[pb_len++] = 0; |
| 706 | pb[pb_len++] = 0; |
| 707 | pb[pb_len++] = 0x1 | (1u << 20); |
| 708 | /* CHIPWAIT-1c, the same block as src/omega_numeric_gb10.c (#225): after the WFI |
| 709 | * release of dispatch_id, flush GPU L2 dirty lines to memory (NVC96F_MEM_OP_A..D = |
| 710 | * 0x28..0x34, D bits 31:27 OPERATION = L2_FLUSH_DIRTY 0x10), then a second WFI |
| 711 | * release to an uncached word (completion page +0x10, GPU-uncached via |
| 712 | * nvrm_alloc_gpu_uncached in omega_accelerator_world.c). The marker2 payload is |
| 713 | * patched per entry: only the last entry of a batch writes the sentinel |
| 714 | * 0x46464646, so the host sentinel wait proves the last flush landed; earlier |
| 715 | * entries write their dispatch_id (never equal to the sentinel). */ |
| 716 | pb[pb_len++] = nvrm_mthd(0, 0x0028, 4); |
| 717 | pb[pb_len++] = 0; |
| 718 | pb[pb_len++] = 0; |
| 719 | pb[pb_len++] = 0; |
| 720 | pb[pb_len++] = (0x10u << 27); |
| 721 | pb[pb_len++] = nvrm_mthd(0, 0x005c, 5); |
| 722 | pb[pb_len++] = (uint32_t)(world.completion.gpu_va + M19_WRAP_MARKER2_OFFSET); |
| 723 | pb[pb_len++] = (uint32_t)((world.completion.gpu_va + M19_WRAP_MARKER2_OFFSET) >> 32); |
| 724 | size_t marker2_idx = pb_len; |
| 725 | pb[pb_len++] = M19_WRAP_MARKER2_SENTINEL; |
| 726 | pb[pb_len++] = 0; |
| 727 | pb[pb_len++] = 0x1 | (1u << 20); |
| 728 | |
| 729 | /* The pushbuffer pool holds exactly PB_SLOTS 4 KiB slots (0x10000 bytes). |
| 730 | * A slot must not be rewritten until every GPFIFO entry that references it |
| 731 | * has retired; otherwise the GPU can observe a later dispatch's bytes under |
| 732 | * an earlier entry, and a terminal completion value can be reached without |
| 733 | * proving per-dispatch execution identity. Submit at most one pool's worth |
| 734 | * of entries per doorbell, then wait for those entries to complete before |
| 735 | * any slot is reused. */ |
| 736 | const uint32_t pb_slots = 0x10000 / 0x1000; |
| 737 | uint32_t dispatch_id = 0; |
| 738 | uint32_t submitted = 0; |
| 739 | |
| 740 | while (submitted < stress_count) { |
| 741 | uint32_t batch = stress_count - submitted; |
| 742 | if (batch > pb_slots) batch = pb_slots; |
| 743 | /* Every earlier entry has retired (its marker2 was waited for), so the GPU is idle |
| 744 | * on this channel: clear marker2 so the sentinel wait cannot see a stale value. */ |
| 745 | *(volatile uint32_t *)((uint8_t *)world.completion.cpu_marker + M19_WRAP_MARKER2_OFFSET) = 0; |
| 746 | __asm__ volatile("dsb sy" ::: "memory"); |
| 747 | |
| 748 | for (uint32_t k = 0; k < batch; k++) { |
| 749 | dispatch_id++; |
| 750 | pb[release_idx + 3] = dispatch_id; |
| 751 | pb[marker2_idx] = (k + 1 == batch) ? M19_WRAP_MARKER2_SENTINEL : dispatch_id; |
| 752 | uint32_t off = (submitted % pb_slots) * 0x1000; |
| 753 | memcpy((uint8_t *)world.m16.pb_mem.cpu + off, pb, pb_len * 4); |
| 754 | __asm__ volatile("dsb sy" ::: "memory"); |
| 755 | int enq_rc = nvrm_enqueue(&world.m16.rm, &world.m16.pb_mem, off, (uint32_t)pb_len); |
| 756 | if (enq_rc != 0) { |
| 757 | snprintf(detail, sizeof(detail), |
| 758 | "dispatch_id=%u submitted=%u rc=%d rm.put=%u ring_capacity=%u", |
| 759 | dispatch_id, submitted, enq_rc, (unsigned)world.m16.rm.put, ring_cap); |
| 760 | m19_wrap_exit("enqueue", detail); |
| 761 | omega_world_destroy(&world); |
| 762 | return false; |
| 763 | } |
| 764 | submitted++; |
| 765 | } |
| 766 | *world.m16.rm.doorbell = world.m16.rm.token; |
| 767 | __asm__ volatile("dsb sy" ::: "memory"); |
| 768 | |
| 769 | /* Same effective budget as the old spin (about 5 s with no progress), |
| 770 | * but the clock is monotonic nanoseconds and any word change restarts |
| 771 | * it. The hard cap is generous so a slow-but-moving batch is not cut; |
| 772 | * the marker wait and the marker2 wait share one hard cap. Dispatch ids |
| 773 | * are a free-running counter: sequence semantics for the marker; marker2 |
| 774 | * is awaited for its sentinel (reset to 0 before the batch, written |
| 775 | * non-sentinel by every entry but the last). Both go through the shared |
| 776 | * primitive in src/omega_gpu_wait.c. */ |
| 777 | volatile uint32_t *marker2_word = (volatile uint32_t *) |
| 778 | ((uint8_t *)world.completion.cpu_marker + M19_WRAP_MARKER2_OFFSET); |
| 779 | uint32_t budget_ms = 600000; |
| 780 | for (int stage = 0; stage < 2; stage++) { |
| 781 | omega_gpu_wait_cfg_t wait_cfg; |
| 782 | memset(&wait_cfg, 0, sizeof(wait_cfg)); |
| 783 | wait_cfg.progress_timeout_ms = 5000; |
| 784 | wait_cfg.total_timeout_ms = budget_ms; |
| 785 | omega_gpu_wait_report_t wrpt; |
| 786 | bool reached = stage == 0 |
| 787 | ? omega_gpu_wait_sequence(world.completion.cpu_marker, dispatch_id, &wrpt, &wait_cfg) |
| 788 | : omega_gpu_wait_sequence(marker2_word, M19_WRAP_MARKER2_SENTINEL, &wrpt, &wait_cfg); |
| 789 | if (!reached) { |
| 790 | snprintf(detail, sizeof(detail), |
| 791 | "wait=%s wait_kind=%s result=%s dispatch_id=%u submitted=%u expected=%u observed=%u " |
| 792 | "marker2=%u marker2_expected=%u " |
| 793 | "rm.put=%u ring_capacity=%u channel_generation=%u channel_reconstructions=%u " |
| 794 | "elapsed_ns=%llu last_progress_ns=%llu progress_count=%llu polls=%llu", |
| 795 | stage == 0 ? "marker" : "marker2", |
| 796 | omega_gpu_wait_kind_name(wrpt.wait_kind), omega_gpu_wait_result_name(wrpt.result), |
| 797 | dispatch_id, submitted, wrpt.expected, wrpt.last_observed, |
| 798 | (unsigned)*marker2_word, M19_WRAP_MARKER2_SENTINEL, |
| 799 | (unsigned)world.m16.rm.put, ring_cap, |
| 800 | (unsigned)world.channel_generation, (unsigned)world.channel_reconstructions, |
| 801 | (unsigned long long)wrpt.elapsed_ns, |
| 802 | (unsigned long long)(wrpt.last_progress_ns - wrpt.start_ns), |
| 803 | (unsigned long long)wrpt.progress_count, (unsigned long long)wrpt.polls); |
| 804 | m19_wrap_exit("completion_timeout", detail); |
| 805 | omega_world_destroy(&world); |
| 806 | return false; |
| 807 | } |
| 808 | uint64_t spent_ms = wrpt.elapsed_ns / 1000000ull; |
| 809 | budget_ms = spent_ms < budget_ms ? budget_ms - (uint32_t)spent_ms : 1u; |
| 810 | } |
| 811 | /* Only now are this batch's pushbuffer slots safe to reuse. */ |
| 812 | nvrm_retire(&world.m16.rm, dispatch_id); |
| 813 | m19_wrap_dispatches = dispatch_id; /* observed so far; stress_count on success */ |
| 814 | } |
| 815 | |
| 816 | if (world.m16.rm.put < stress_count || (world.m16.rm.put / ring_cap) < 3) { |
| 817 | snprintf(detail, sizeof(detail), "rm.put=%u stress_count=%u ring_capacity=%u", |
| 818 | (unsigned)world.m16.rm.put, stress_count, ring_cap); |
| 819 | m19_wrap_exit("wrap_count", detail); |
| 820 | omega_world_destroy(&world); |
| 821 | return false; |
| 822 | } |
| 823 | if (world.channel_generation != 1 || world.channel_reconstructions != 0) { |
| 824 | snprintf(detail, sizeof(detail), "channel_generation=%u channel_reconstructions=%u", |
| 825 | (unsigned)world.channel_generation, (unsigned)world.channel_reconstructions); |
| 826 | m19_wrap_exit("channel_state", detail); |
| 827 | omega_world_destroy(&world); |
| 828 | return false; |
| 829 | } |
| 830 | if (c_cpu[0] != 10 || c_cpu[n - 1] != (n - 1 + 10)) { |
| 831 | snprintf(detail, sizeof(detail), "c[0]=%u c[n-1]=%u want %u and %u", |
| 832 | c_cpu[0], c_cpu[n - 1], 10u, n - 1 + 10); |
| 833 | m19_wrap_exit("output_parity", detail); |
| 834 | omega_world_destroy(&world); |
| 835 | return false; |
| 836 | } |
| 837 | |
| 838 | m19_ring_capacity = ring_cap; |
| 839 | m19_wrap_dispatches = stress_count; |
| 840 | omega_world_destroy(&world); |
| 841 | return true; |
| 842 | } |
| 843 | |
| 844 | /* Capture the identity and semantic content of one queued dispatch. */ |
| 845 | static void m19_make_submission(OmegaWorldSubmission *s, |
| 846 | const uint32_t semantic_words[5], |
| 847 | const OmegaHandle *code, |
| 848 | const OmegaHandle *a, |
| 849 | const OmegaHandle *b, |
| 850 | const OmegaHandle *c, |
| 851 | uint32_t a_bytes, uint32_t b_bytes, uint32_t c_bytes, |
| 852 | uint32_t completion_val) { |
| 853 | memset(s, 0, sizeof(*s)); |
| 854 | memcpy(s->semantic_words, semantic_words, sizeof(s->semantic_words)); |
| 855 | s->code_object_id = code->object_id; |
| 856 | s->code_generation = code->object_generation; |
| 857 | s->code_permissions = code->permissions; |
| 858 | s->a_object_id = a->object_id; |
| 859 | s->a_generation = a->object_generation; |
| 860 | s->a_permissions = a->permissions; |
| 861 | s->b_object_id = b->object_id; |
| 862 | s->b_generation = b->object_generation; |
| 863 | s->b_permissions = b->permissions; |
| 864 | s->c_object_id = c->object_id; |
| 865 | s->c_generation = c->object_generation; |
| 866 | s->c_permissions = c->permissions; |
| 867 | s->a_bytes = a_bytes; |
| 868 | s->b_bytes = b_bytes; |
| 869 | s->c_bytes = c_bytes; |
| 870 | s->completion_val = completion_val; |
| 871 | } |
| 872 | |
| 873 | /* Write a pushbuffer into a pool slot and enqueue it through the world. The |
| 874 | * slot is not reused by the caller until omega_world_drain observes retirement. */ |
| 875 | static bool m19_submit_op(OmegaAcceleratorWorld *world, NvrmMem *pool, |
| 876 | const uint32_t *pb, size_t pb_len, |
| 877 | uint32_t slot, const OmegaWorldSubmission *s) { |
| 878 | if (pb_len * 4 > 0x1000) return false; |
| 879 | uint32_t off = slot * 0x1000; |
| 880 | memcpy((uint8_t *)pool->cpu + off, pb, pb_len * 4); |
| 881 | __asm__ volatile("dsb sy" ::: "memory"); |
| 882 | return omega_world_submit(world, pool, off, (uint32_t)pb_len, s) == OMEGA_WORLD_OK; |
| 883 | } |
| 884 | |
| 885 | /* Gate 8: OMEGA_ACCEL_RESIDENT_1000_OP_PASS */ |
| 886 | static bool test_m19_gate8_1000_op(void) { |
| 887 | OmegaAcceleratorWorld world; |
| 888 | if (omega_world_init(&world) != OMEGA_WORLD_OK) return false; |
| 889 | |
| 890 | if (world.channel_generation != 1 || world.channel_reconstructions != 0) { |
| 891 | omega_world_destroy(&world); |
| 892 | return false; |
| 893 | } |
| 894 | |
| 895 | uint8_t vecadd_buf[1024]; |
| 896 | size_t vecadd_len = 0; |
| 897 | omega_blackwell_encode_vecadd(vecadd_buf, sizeof(vecadd_buf), &vecadd_len); |
| 898 | OmegaHandle code_vecadd; |
| 899 | omega_world_register_code(&world, vecadd_buf, vecadd_len, NULL, &code_vecadd); |
| 900 | |
| 901 | OmegaMatMulSpec spec_i32, spec_f16, spec_bf16; |
| 902 | omega_matmul_spec_init(&spec_i32, 16, 16, 16, OMEGA_MATMUL_PRECISION_INT32); |
| 903 | omega_matmul_spec_init(&spec_f16, 16, 16, 16, OMEGA_MATMUL_PRECISION_FP16); |
| 904 | omega_matmul_spec_init(&spec_bf16, 16, 16, 16, OMEGA_MATMUL_PRECISION_BF16); |
| 905 | |
| 906 | OmegaBlackwellKernel k_i32, k_f16, k_bf16; |
| 907 | omega_blackwell_codegen_matmul(&spec_i32, &k_i32); |
| 908 | omega_blackwell_codegen_matmul(&spec_f16, &k_f16); |
| 909 | omega_blackwell_codegen_matmul(&spec_bf16, &k_bf16); |
| 910 | |
| 911 | OmegaHandle code_i32, code_f16, code_bf16; |
| 912 | omega_world_register_code(&world, k_i32.code, k_i32.code_size, NULL, &code_i32); |
| 913 | omega_world_register_code(&world, k_f16.code, k_f16.code_size, NULL, &code_f16); |
| 914 | omega_world_register_code(&world, k_bf16.code, k_bf16.code_size, NULL, &code_bf16); |
| 915 | |
| 916 | uint32_t vec_n = 1024; |
| 917 | size_t vec_bytes = vec_n * sizeof(uint32_t); |
| 918 | OmegaHandle v_a, v_b, v_c; |
| 919 | omega_world_register_buffer(&world, vec_bytes, OMEGA_PERM_READ, &v_a); |
| 920 | omega_world_register_buffer(&world, vec_bytes, OMEGA_PERM_READ, &v_b); |
| 921 | omega_world_register_buffer(&world, vec_bytes, OMEGA_PERM_READ | OMEGA_PERM_WRITE, &v_c); |
| 922 | |
| 923 | size_t mat_bytes_i32 = 16 * 16 * 4; |
| 924 | size_t mat_bytes_f16 = 16 * 16 * 2; |
| 925 | size_t mat_bytes_out = 16 * 16 * 4; |
| 926 | |
| 927 | OmegaHandle m_a_i32, m_b_i32, m_c_i32; |
| 928 | omega_world_register_buffer(&world, mat_bytes_i32, OMEGA_PERM_READ, &m_a_i32); |
| 929 | omega_world_register_buffer(&world, mat_bytes_i32, OMEGA_PERM_READ, &m_b_i32); |
| 930 | omega_world_register_buffer(&world, mat_bytes_out, OMEGA_PERM_READ | OMEGA_PERM_WRITE, &m_c_i32); |
| 931 | |
| 932 | OmegaHandle m_a_f16, m_b_f16, m_c_f16; |
| 933 | omega_world_register_buffer(&world, mat_bytes_f16, OMEGA_PERM_READ, &m_a_f16); |
| 934 | omega_world_register_buffer(&world, mat_bytes_f16, OMEGA_PERM_READ, &m_b_f16); |
| 935 | omega_world_register_buffer(&world, mat_bytes_out, OMEGA_PERM_READ | OMEGA_PERM_WRITE, &m_c_f16); |
| 936 | |
| 937 | /* BF16 gets its own inputs: FP16 and BF16 encode 1.0 differently (0x3c00 vs |
| 938 | * 0x3f80). Sharing one buffer across the two kernels made the BF16 op read |
| 939 | * FP16 bytes. Distinct buffers also keep the inputs stable while several |
| 940 | * dispatches are in flight under batched submission. */ |
| 941 | OmegaHandle m_a_bf16, m_b_bf16, m_c_bf16; |
| 942 | omega_world_register_buffer(&world, mat_bytes_f16, OMEGA_PERM_READ, &m_a_bf16); |
| 943 | omega_world_register_buffer(&world, mat_bytes_f16, OMEGA_PERM_READ, &m_b_bf16); |
| 944 | omega_world_register_buffer(&world, mat_bytes_out, OMEGA_PERM_READ | OMEGA_PERM_WRITE, &m_c_bf16); |
| 945 | |
| 946 | NvrmMem pb_pool; |
| 947 | /* The CPU reuses these 64 command slots; a GPU-cached slot can replay old |
| 948 | * commands on GB10 despite CPU store barriers, so bypass the GPU cache. */ |
| 949 | if (nvrm_alloc_gpu_uncached(&world.m16.rm, 0x40000, &pb_pool) != 0) { |
| 950 | omega_world_destroy(&world); |
| 951 | return false; |
| 952 | } |
| 953 | |
| 954 | uint32_t *va_cpu, *vb_cpu, *vc_cpu; |
| 955 | uint64_t va_va, vb_va, vc_va, vec_code_va; |
| 956 | size_t code_sz; |
| 957 | omega_world_resolve_code(&world, &code_vecadd, NULL, &vec_code_va, &code_sz); |
| 958 | omega_world_resolve_buffer(&world, &v_a, OMEGA_PERM_READ, 0, vec_bytes, (void **)&va_cpu, &va_va); |
| 959 | omega_world_resolve_buffer(&world, &v_b, OMEGA_PERM_READ, 0, vec_bytes, (void **)&vb_cpu, &vb_va); |
| 960 | omega_world_resolve_buffer(&world, &v_c, OMEGA_PERM_WRITE, 0, vec_bytes, (void **)&vc_cpu, &vc_va); |
| 961 | for (uint32_t i = 0; i < vec_n; i++) { va_cpu[i] = i; vb_cpu[i] = 1; vc_cpu[i] = 0; } |
| 962 | |
| 963 | void *cbank_v, *qmd0_v, *qmd1_v, *sem_v; |
| 964 | uint64_t cbank_v_va, qmd0_v_va, qmd1_v_va, sem_v_va; |
| 965 | size_t dummy; |
| 966 | omega_world_scratch_acquire(&world, 0x1000, &cbank_v, &cbank_v_va, &dummy); |
| 967 | omega_world_scratch_acquire(&world, 0x1000, &qmd0_v, &qmd0_v_va, &dummy); |
| 968 | omega_world_scratch_acquire(&world, 0x1000, &qmd1_v, &qmd1_v_va, &dummy); |
| 969 | omega_world_scratch_acquire(&world, 0x1000, &sem_v, &sem_v_va, &dummy); |
| 970 | |
| 971 | uint32_t cbank_v_data[OMEGA_BW_CBANK_DRIVER_WORDS]; |
| 972 | omega_blackwell_build_cbank_driver(cbank_v_data, cbank_v_va); |
| 973 | uint32_t cbank_v_args[OMEGA_BW_CBANK_ARGS_WORDS]; |
| 974 | omega_blackwell_build_cbank_args(cbank_v_args, va_va, vb_va, vc_va, vec_n); |
| 975 | memcpy(cbank_v, cbank_v_data, sizeof(cbank_v_data)); |
| 976 | memcpy((uint8_t *)cbank_v + 0x380, cbank_v_args, sizeof(cbank_v_args)); |
| 977 | |
| 978 | OmegaBlackwellQmdConfig qmd_v_cfg = { |
| 979 | .code_va = vec_code_va, .cbank_va = cbank_v_va, .scratch_va = cbank_v_va + 0x2000, |
| 980 | .sem_va = sem_v_va, .qmd0_va = qmd0_v_va, .qmd1_va = qmd1_v_va, |
| 981 | .num_elements = vec_n, .threads_per_block = 64, .grid_width = (vec_n + 63) / 64 |
| 982 | }; |
| 983 | uint32_t qmd0_v_w[OMEGA_BW_QMD_WORDS], qmd1_v_w[OMEGA_BW_QMD_WORDS]; |
| 984 | omega_blackwell_build_qmd0(qmd0_v_w, qmd0_v_va, qmd1_v_va); |
| 985 | omega_blackwell_build_qmd1(qmd1_v_w, &qmd_v_cfg); |
| 986 | memcpy(qmd0_v, qmd0_v_w, sizeof(qmd0_v_w)); |
| 987 | memcpy(qmd1_v, qmd1_v_w, sizeof(qmd1_v_w)); |
| 988 | *(volatile uint32_t *)sem_v = 0; |
| 989 | |
| 990 | uint32_t pb_v[1024]; size_t pb_v_len = 0; |
| 991 | memcpy(&pb_v[pb_v_len], WORLD_SETUP_WORDS, sizeof(WORLD_SETUP_WORDS)); |
| 992 | pb_v_len += sizeof(WORLD_SETUP_WORDS) / 4; |
| 993 | pb_v[pb_v_len++] = nvrm_mthd(1, 0x0188, 2); |
| 994 | pb_v[pb_v_len++] = (uint32_t)(cbank_v_va >> 32); |
| 995 | pb_v[pb_v_len++] = (uint32_t)cbank_v_va; |
| 996 | pb_v[pb_v_len++] = nvrm_mthd(1, 0x0180, 2); |
| 997 | pb_v[pb_v_len++] = 0x00000380; pb_v[pb_v_len++] = 0x00000001; |
| 998 | pb_v[pb_v_len++] = nvrm_mthd(1, 0x01b0, 1); pb_v[pb_v_len++] = 0x00000041; |
| 999 | pb_v[pb_v_len++] = (224 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28); |
| 1000 | memcpy(&pb_v[pb_v_len], cbank_v_data, 224 * 4); pb_v_len += 224; |
| 1001 | pb_v[pb_v_len++] = nvrm_mthd(1, 0x0188, 2); |
| 1002 | pb_v[pb_v_len++] = (uint32_t)((cbank_v_va + 0x380) >> 32); |
| 1003 | pb_v[pb_v_len++] = (uint32_t)(cbank_v_va + 0x380); |
| 1004 | pb_v[pb_v_len++] = nvrm_mthd(1, 0x0180, 2); |
| 1005 | pb_v[pb_v_len++] = 0x0000001c; pb_v[pb_v_len++] = 0x00000001; |
| 1006 | pb_v[pb_v_len++] = nvrm_mthd(1, 0x01b0, 1); pb_v[pb_v_len++] = 0x00000041; |
| 1007 | pb_v[pb_v_len++] = (7 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28); |
| 1008 | memcpy(&pb_v[pb_v_len], cbank_v_args, 7 * 4); pb_v_len += 7; |
| 1009 | pb_v[pb_v_len++] = (98 << 16) | (1 << 13) | (0x0318 >> 2) | (2u << 28); |
| 1010 | pb_v[pb_v_len++] = (1u << 30) | (uint32_t)((qmd0_v_va >> 40) & 0x1ff); |
| 1011 | pb_v[pb_v_len++] = (uint32_t)(qmd0_v_va >> 8); |
| 1012 | memcpy(&pb_v[pb_v_len], qmd0_v_w, 96 * 4); pb_v_len += 96; |
| 1013 | pb_v[pb_v_len++] = nvrm_mthd(1, 0x0188, 2); |
| 1014 | pb_v[pb_v_len++] = (uint32_t)(sem_v_va >> 32); |
| 1015 | pb_v[pb_v_len++] = (uint32_t)sem_v_va; |
| 1016 | pb_v[pb_v_len++] = nvrm_mthd(1, 0x0180, 2); |
| 1017 | pb_v[pb_v_len++] = 0x00000004; pb_v[pb_v_len++] = 0x00000001; |
| 1018 | pb_v[pb_v_len++] = nvrm_mthd(1, 0x01b0, 1); pb_v[pb_v_len++] = 0x00000041; |
| 1019 | pb_v[pb_v_len++] = (1 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28); |
| 1020 | pb_v[pb_v_len++] = OMEGA_BW_SEMAPHORE_INTERMEDIATE_INIT; |
| 1021 | pb_v[pb_v_len++] = (98 << 16) | (1 << 13) | (0x0318 >> 2) | (2u << 28); |
| 1022 | pb_v[pb_v_len++] = (1u << 30) | (uint32_t)((qmd1_v_va >> 40) & 0x1ff); |
| 1023 | pb_v[pb_v_len++] = (uint32_t)(qmd1_v_va >> 8); |
| 1024 | memcpy(&pb_v[pb_v_len], qmd1_v_w, 96 * 4); pb_v_len += 96; |
| 1025 | size_t rel_v = pb_v_len; |
| 1026 | pb_v[pb_v_len++] = nvrm_mthd(0, 0x005c, 5); |
| 1027 | pb_v[pb_v_len++] = (uint32_t)world.completion.gpu_va; |
| 1028 | pb_v[pb_v_len++] = (uint32_t)(world.completion.gpu_va >> 32); |
| 1029 | pb_v[pb_v_len++] = 0; pb_v[pb_v_len++] = 0; |
| 1030 | pb_v[pb_v_len++] = 0x1 | (1u << 20); |
| 1031 | |
| 1032 | uint32_t *mai_cpu, *mbi_cpu, *mci_cpu; |
| 1033 | uint64_t mai_va, mbi_va, mci_va, i32_code_va; |
| 1034 | omega_world_resolve_code(&world, &code_i32, NULL, &i32_code_va, &code_sz); |
| 1035 | omega_world_resolve_buffer(&world, &m_a_i32, OMEGA_PERM_READ, 0, mat_bytes_i32, (void **)&mai_cpu, &mai_va); |
| 1036 | omega_world_resolve_buffer(&world, &m_b_i32, OMEGA_PERM_READ, 0, mat_bytes_i32, (void **)&mbi_cpu, &mbi_va); |
| 1037 | omega_world_resolve_buffer(&world, &m_c_i32, OMEGA_PERM_WRITE, 0, mat_bytes_out, (void **)&mci_cpu, &mci_va); |
| 1038 | for (int i = 0; i < 256; i++) { mai_cpu[i] = 1; mbi_cpu[i] = (i % 16 == i / 16) ? 1 : 0; mci_cpu[i] = 0; } |
| 1039 | |
| 1040 | void *cbank_i, *qmd0_i, *qmd1_i, *sem_i, *ks_i; |
| 1041 | uint64_t cbank_i_va, qmd0_i_va, qmd1_i_va, sem_i_va, ks_i_va; |
| 1042 | omega_world_scratch_acquire(&world, 0x1000, &cbank_i, &cbank_i_va, &dummy); |
| 1043 | omega_world_scratch_acquire(&world, 0x1000, &qmd0_i, &qmd0_i_va, &dummy); |
| 1044 | omega_world_scratch_acquire(&world, 0x1000, &qmd1_i, &qmd1_i_va, &dummy); |
| 1045 | omega_world_scratch_acquire(&world, 0x1000, &sem_i, &sem_i_va, &dummy); |
| 1046 | omega_world_scratch_acquire(&world, 0x4000, &ks_i, &ks_i_va, &dummy); |
| 1047 | |
| 1048 | uint32_t cbank_i_data[OMEGA_BW_CBANK_DRIVER_WORDS]; |
| 1049 | omega_blackwell_build_cbank_driver_2d(cbank_i_data, cbank_i_va, 16, 16, 1, 1); |
| 1050 | uint32_t cbank_i_args[OMEGA_BW_CBANK_MATMUL_ARGS_WORDS]; |
| 1051 | omega_blackwell_build_cbank_args_matmul(cbank_i_args, mai_va, mbi_va, mci_va, 16, 16, 16); |
| 1052 | memcpy(cbank_i, cbank_i_data, sizeof(cbank_i_data)); |
| 1053 | memcpy((uint8_t *)cbank_i + 0x380, cbank_i_args, sizeof(cbank_i_args)); |
| 1054 | |
| 1055 | OmegaBlackwellQmdConfig qmd_i_cfg = { |
| 1056 | .code_va = i32_code_va, .cbank_va = cbank_i_va, .scratch_va = ks_i_va, |
| 1057 | .sem_va = sem_i_va, .qmd0_va = qmd0_i_va, .qmd1_va = qmd1_i_va, |
| 1058 | .threads_x = 16, .threads_y = 16, .grid_x = 1, .grid_y = 1, |
| 1059 | .threads_per_block = 256, .grid_width = 1, .num_elements = 256, .gpr_count = 32 |
| 1060 | }; |
| 1061 | uint32_t qmd0_i_w[OMEGA_BW_QMD_WORDS], qmd1_i_w[OMEGA_BW_QMD_WORDS]; |
| 1062 | omega_blackwell_build_qmd0(qmd0_i_w, qmd0_i_va, qmd1_i_va); |
| 1063 | omega_blackwell_build_qmd1(qmd1_i_w, &qmd_i_cfg); |
| 1064 | memcpy(qmd0_i, qmd0_i_w, sizeof(qmd0_i_w)); |
| 1065 | memcpy(qmd1_i, qmd1_i_w, sizeof(qmd1_i_w)); |
| 1066 | *(volatile uint32_t *)sem_i = 0; |
| 1067 | |
| 1068 | uint32_t pb_i[1024]; size_t pb_i_len = 0; |
| 1069 | memcpy(&pb_i[pb_i_len], WORLD_SETUP_WORDS, sizeof(WORLD_SETUP_WORDS)); |
| 1070 | pb_i_len += sizeof(WORLD_SETUP_WORDS) / 4; |
| 1071 | pb_i[pb_i_len++] = nvrm_mthd(1, 0x0188, 2); |
| 1072 | pb_i[pb_i_len++] = (uint32_t)(cbank_i_va >> 32); |
| 1073 | pb_i[pb_i_len++] = (uint32_t)cbank_i_va; |
| 1074 | pb_i[pb_i_len++] = nvrm_mthd(1, 0x0180, 2); |
| 1075 | pb_i[pb_i_len++] = 0x00000380; pb_i[pb_i_len++] = 0x00000001; |
| 1076 | pb_i[pb_i_len++] = nvrm_mthd(1, 0x01b0, 1); pb_i[pb_i_len++] = 0x00000041; |
| 1077 | pb_i[pb_i_len++] = (224 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28); |
| 1078 | memcpy(&pb_i[pb_i_len], cbank_i_data, 224 * 4); pb_i_len += 224; |
| 1079 | pb_i[pb_i_len++] = nvrm_mthd(1, 0x0188, 2); |
| 1080 | pb_i[pb_i_len++] = (uint32_t)((cbank_i_va + 0x380) >> 32); |
| 1081 | pb_i[pb_i_len++] = (uint32_t)(cbank_i_va + 0x380); |
| 1082 | pb_i[pb_i_len++] = nvrm_mthd(1, 0x0180, 2); |
| 1083 | pb_i[pb_i_len++] = 0x00000028; pb_i[pb_i_len++] = 0x00000001; |
| 1084 | pb_i[pb_i_len++] = nvrm_mthd(1, 0x01b0, 1); pb_i[pb_i_len++] = 0x00000041; |
| 1085 | pb_i[pb_i_len++] = (10 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28); |
| 1086 | memcpy(&pb_i[pb_i_len], cbank_i_args, 10 * 4); pb_i_len += 10; |
| 1087 | pb_i[pb_i_len++] = (98 << 16) | (1 << 13) | (0x0318 >> 2) | (2u << 28); |
| 1088 | pb_i[pb_i_len++] = (1u << 30) | (uint32_t)((qmd0_i_va >> 40) & 0x1ff); |
| 1089 | pb_i[pb_i_len++] = (uint32_t)(qmd0_i_va >> 8); |
| 1090 | memcpy(&pb_i[pb_i_len], qmd0_i_w, 96 * 4); pb_i_len += 96; |
| 1091 | pb_i[pb_i_len++] = nvrm_mthd(1, 0x0188, 2); |
| 1092 | pb_i[pb_i_len++] = (uint32_t)(sem_i_va >> 32); |
| 1093 | pb_i[pb_i_len++] = (uint32_t)sem_i_va; |
| 1094 | pb_i[pb_i_len++] = nvrm_mthd(1, 0x0180, 2); |
| 1095 | pb_i[pb_i_len++] = 0x00000004; pb_i[pb_i_len++] = 0x00000001; |
| 1096 | pb_i[pb_i_len++] = nvrm_mthd(1, 0x01b0, 1); pb_i[pb_i_len++] = 0x00000041; |
| 1097 | pb_i[pb_i_len++] = (1 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28); |
| 1098 | pb_i[pb_i_len++] = OMEGA_BW_SEMAPHORE_INTERMEDIATE_INIT; |
| 1099 | pb_i[pb_i_len++] = (98 << 16) | (1 << 13) | (0x0318 >> 2) | (2u << 28); |
| 1100 | pb_i[pb_i_len++] = (1u << 30) | (uint32_t)((qmd1_i_va >> 40) & 0x1ff); |
| 1101 | pb_i[pb_i_len++] = (uint32_t)(qmd1_i_va >> 8); |
| 1102 | memcpy(&pb_i[pb_i_len], qmd1_i_w, 96 * 4); pb_i_len += 96; |
| 1103 | size_t rel_i = pb_i_len; |
| 1104 | pb_i[pb_i_len++] = nvrm_mthd(0, 0x005c, 5); |
| 1105 | pb_i[pb_i_len++] = (uint32_t)world.completion.gpu_va; |
| 1106 | pb_i[pb_i_len++] = (uint32_t)(world.completion.gpu_va >> 32); |
| 1107 | pb_i[pb_i_len++] = 0; pb_i[pb_i_len++] = 0; |
| 1108 | pb_i[pb_i_len++] = 0x1 | (1u << 20); |
| 1109 | |
| 1110 | uint16_t *fa_cpu, *fb_cpu; float *fc_cpu; |
| 1111 | uint64_t fa_va, fb_va, fc_va, f16_code_va; |
| 1112 | omega_world_resolve_code(&world, &code_f16, NULL, &f16_code_va, &code_sz); |
| 1113 | omega_world_resolve_buffer(&world, &m_a_f16, OMEGA_PERM_READ, 0, mat_bytes_f16, (void **)&fa_cpu, &fa_va); |
| 1114 | omega_world_resolve_buffer(&world, &m_b_f16, OMEGA_PERM_READ, 0, mat_bytes_f16, (void **)&fb_cpu, &fb_va); |
| 1115 | omega_world_resolve_buffer(&world, &m_c_f16, OMEGA_PERM_WRITE, 0, mat_bytes_out, (void **)&fc_cpu, &fc_va); |
| 1116 | for (int i = 0; i < 256; i++) { fa_cpu[i] = 0x3c00; fb_cpu[i] = (i % 16 == i / 16) ? 0x3c00 : 0; fc_cpu[i] = 0.0f; } |
| 1117 | |
| 1118 | void *cbank_f, *qmd0_f, *qmd1_f, *sem_f, *ks_f; |
| 1119 | uint64_t cbank_f_va, qmd0_f_va, qmd1_f_va, sem_f_va, ks_f_va; |
| 1120 | omega_world_scratch_acquire(&world, 0x1000, &cbank_f, &cbank_f_va, &dummy); |
| 1121 | omega_world_scratch_acquire(&world, 0x1000, &qmd0_f, &qmd0_f_va, &dummy); |
| 1122 | omega_world_scratch_acquire(&world, 0x1000, &qmd1_f, &qmd1_f_va, &dummy); |
| 1123 | omega_world_scratch_acquire(&world, 0x1000, &sem_f, &sem_f_va, &dummy); |
| 1124 | omega_world_scratch_acquire(&world, 0x4000, &ks_f, &ks_f_va, &dummy); |
| 1125 | |
| 1126 | uint32_t cbank_f_data[OMEGA_BW_CBANK_DRIVER_WORDS]; |
| 1127 | omega_blackwell_build_cbank_driver_2d(cbank_f_data, cbank_f_va, 32, 1, 2, 1); |
| 1128 | uint32_t cbank_f_args[OMEGA_BW_CBANK_MATMUL_ARGS_WORDS]; |
| 1129 | omega_blackwell_build_cbank_args_matmul(cbank_f_args, fa_va, fb_va, fc_va, 16, 16, 16); |
| 1130 | memcpy(cbank_f, cbank_f_data, sizeof(cbank_f_data)); |
| 1131 | memcpy((uint8_t *)cbank_f + 0x380, cbank_f_args, sizeof(cbank_f_args)); |
| 1132 | |
| 1133 | OmegaBlackwellQmdConfig qmd_f_cfg = { |
| 1134 | .code_va = f16_code_va, .cbank_va = cbank_f_va, .scratch_va = ks_f_va, |
| 1135 | .sem_va = sem_f_va, .qmd0_va = qmd0_f_va, .qmd1_va = qmd1_f_va, |
| 1136 | .threads_x = 32, .threads_y = 1, .grid_x = 2, .grid_y = 1, |
| 1137 | .threads_per_block = 32, .grid_width = 2, .num_elements = 256, .gpr_count = 64 |
| 1138 | }; |
| 1139 | uint32_t qmd0_f_w[OMEGA_BW_QMD_WORDS], qmd1_f_w[OMEGA_BW_QMD_WORDS]; |
| 1140 | omega_blackwell_build_qmd0(qmd0_f_w, qmd0_f_va, qmd1_f_va); |
| 1141 | omega_blackwell_build_qmd1(qmd1_f_w, &qmd_f_cfg); |
| 1142 | memcpy(qmd0_f, qmd0_f_w, sizeof(qmd0_f_w)); |
| 1143 | memcpy(qmd1_f, qmd1_f_w, sizeof(qmd1_f_w)); |
| 1144 | *(volatile uint32_t *)sem_f = 0; |
| 1145 | |
| 1146 | uint32_t pb_f[1024]; size_t pb_f_len = 0; |
| 1147 | memcpy(&pb_f[pb_f_len], WORLD_SETUP_WORDS, sizeof(WORLD_SETUP_WORDS)); |
| 1148 | pb_f_len += sizeof(WORLD_SETUP_WORDS) / 4; |
| 1149 | pb_f[pb_f_len++] = nvrm_mthd(1, 0x0188, 2); |
| 1150 | pb_f[pb_f_len++] = (uint32_t)(cbank_f_va >> 32); |
| 1151 | pb_f[pb_f_len++] = (uint32_t)cbank_f_va; |
| 1152 | pb_f[pb_f_len++] = nvrm_mthd(1, 0x0180, 2); |
| 1153 | pb_f[pb_f_len++] = 0x00000380; pb_f[pb_f_len++] = 0x00000001; |
| 1154 | pb_f[pb_f_len++] = nvrm_mthd(1, 0x01b0, 1); pb_f[pb_f_len++] = 0x00000041; |
| 1155 | pb_f[pb_f_len++] = (224 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28); |
| 1156 | memcpy(&pb_f[pb_f_len], cbank_f_data, 224 * 4); pb_f_len += 224; |
| 1157 | pb_f[pb_f_len++] = nvrm_mthd(1, 0x0188, 2); |
| 1158 | pb_f[pb_f_len++] = (uint32_t)((cbank_f_va + 0x380) >> 32); |
| 1159 | pb_f[pb_f_len++] = (uint32_t)(cbank_f_va + 0x380); |
| 1160 | pb_f[pb_f_len++] = nvrm_mthd(1, 0x0180, 2); |
| 1161 | pb_f[pb_f_len++] = 0x00000028; pb_f[pb_f_len++] = 0x00000001; |
| 1162 | pb_f[pb_f_len++] = nvrm_mthd(1, 0x01b0, 1); pb_f[pb_f_len++] = 0x00000041; |
| 1163 | pb_f[pb_f_len++] = (10 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28); |
| 1164 | memcpy(&pb_f[pb_f_len], cbank_f_args, 10 * 4); pb_f_len += 10; |
| 1165 | pb_f[pb_f_len++] = (98 << 16) | (1 << 13) | (0x0318 >> 2) | (2u << 28); |
| 1166 | pb_f[pb_f_len++] = (1u << 30) | (uint32_t)((qmd0_f_va >> 40) & 0x1ff); |
| 1167 | pb_f[pb_f_len++] = (uint32_t)(qmd0_f_va >> 8); |
| 1168 | memcpy(&pb_f[pb_f_len], qmd0_f_w, 96 * 4); pb_f_len += 96; |
| 1169 | pb_f[pb_f_len++] = nvrm_mthd(1, 0x0188, 2); |
| 1170 | pb_f[pb_f_len++] = (uint32_t)(sem_f_va >> 32); |
| 1171 | pb_f[pb_f_len++] = (uint32_t)sem_f_va; |
| 1172 | pb_f[pb_f_len++] = nvrm_mthd(1, 0x0180, 2); |
| 1173 | pb_f[pb_f_len++] = 0x00000004; pb_f[pb_f_len++] = 0x00000001; |
| 1174 | pb_f[pb_f_len++] = nvrm_mthd(1, 0x01b0, 1); pb_f[pb_f_len++] = 0x00000041; |
| 1175 | pb_f[pb_f_len++] = (1 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28); |
| 1176 | pb_f[pb_f_len++] = OMEGA_BW_SEMAPHORE_INTERMEDIATE_INIT; |
| 1177 | pb_f[pb_f_len++] = (98 << 16) | (1 << 13) | (0x0318 >> 2) | (2u << 28); |
| 1178 | pb_f[pb_f_len++] = (1u << 30) | (uint32_t)((qmd1_f_va >> 40) & 0x1ff); |
| 1179 | pb_f[pb_f_len++] = (uint32_t)(qmd1_f_va >> 8); |
| 1180 | memcpy(&pb_f[pb_f_len], qmd1_f_w, 96 * 4); pb_f_len += 96; |
| 1181 | size_t rel_f = pb_f_len; |
| 1182 | pb_f[pb_f_len++] = nvrm_mthd(0, 0x005c, 5); |
| 1183 | pb_f[pb_f_len++] = (uint32_t)world.completion.gpu_va; |
| 1184 | pb_f[pb_f_len++] = (uint32_t)(world.completion.gpu_va >> 32); |
| 1185 | pb_f[pb_f_len++] = 0; pb_f[pb_f_len++] = 0; |
| 1186 | pb_f[pb_f_len++] = 0x1 | (1u << 20); |
| 1187 | |
| 1188 | uint64_t bf16_code_va; |
| 1189 | omega_world_resolve_code(&world, &code_bf16, NULL, &bf16_code_va, &code_sz); |
| 1190 | uint16_t *ba_cpu, *bb_cpu; float *bc_cpu; |
| 1191 | uint64_t ba_va, bb_va, bc_va; |
| 1192 | omega_world_resolve_buffer(&world, &m_a_bf16, OMEGA_PERM_READ, 0, mat_bytes_f16, (void **)&ba_cpu, &ba_va); |
| 1193 | omega_world_resolve_buffer(&world, &m_b_bf16, OMEGA_PERM_READ, 0, mat_bytes_f16, (void **)&bb_cpu, &bb_va); |
| 1194 | omega_world_resolve_buffer(&world, &m_c_bf16, OMEGA_PERM_WRITE, 0, mat_bytes_out, (void **)&bc_cpu, &bc_va); |
| 1195 | for (int i = 0; i < 256; i++) { ba_cpu[i] = 0x3f80; bb_cpu[i] = (i % 16 == i / 16) ? 0x3f80 : 0; bc_cpu[i] = 0.0f; } |
| 1196 | void *cbank_b, *qmd0_b, *qmd1_b, *sem_b, *ks_b; |
| 1197 | uint64_t cbank_b_va, qmd0_b_va, qmd1_b_va, sem_b_va, ks_b_va; |
| 1198 | omega_world_scratch_acquire(&world, 0x1000, &cbank_b, &cbank_b_va, &dummy); |
| 1199 | omega_world_scratch_acquire(&world, 0x1000, &qmd0_b, &qmd0_b_va, &dummy); |
| 1200 | omega_world_scratch_acquire(&world, 0x1000, &qmd1_b, &qmd1_b_va, &dummy); |
| 1201 | omega_world_scratch_acquire(&world, 0x1000, &sem_b, &sem_b_va, &dummy); |
| 1202 | omega_world_scratch_acquire(&world, 0x4000, &ks_b, &ks_b_va, &dummy); |
| 1203 | |
| 1204 | uint32_t cbank_b_data[OMEGA_BW_CBANK_DRIVER_WORDS]; |
| 1205 | omega_blackwell_build_cbank_driver_2d(cbank_b_data, cbank_b_va, 32, 1, 2, 1); |
| 1206 | uint32_t cbank_b_args[OMEGA_BW_CBANK_MATMUL_ARGS_WORDS]; |
| 1207 | omega_blackwell_build_cbank_args_matmul(cbank_b_args, ba_va, bb_va, bc_va, 16, 16, 16); |
| 1208 | memcpy(cbank_b, cbank_b_data, sizeof(cbank_b_data)); |
| 1209 | memcpy((uint8_t *)cbank_b + 0x380, cbank_b_args, sizeof(cbank_b_args)); |
| 1210 | |
| 1211 | OmegaBlackwellQmdConfig qmd_b_cfg = { |
| 1212 | .code_va = bf16_code_va, .cbank_va = cbank_b_va, .scratch_va = ks_b_va, |
| 1213 | .sem_va = sem_b_va, .qmd0_va = qmd0_b_va, .qmd1_va = qmd1_b_va, |
| 1214 | .threads_x = 32, .threads_y = 1, .grid_x = 2, .grid_y = 1, |
| 1215 | .threads_per_block = 32, .grid_width = 2, .num_elements = 256, .gpr_count = 64 |
| 1216 | }; |
| 1217 | uint32_t qmd0_b_w[OMEGA_BW_QMD_WORDS], qmd1_b_w[OMEGA_BW_QMD_WORDS]; |
| 1218 | omega_blackwell_build_qmd0(qmd0_b_w, qmd0_b_va, qmd1_b_va); |
| 1219 | omega_blackwell_build_qmd1(qmd1_b_w, &qmd_b_cfg); |
| 1220 | memcpy(qmd0_b, qmd0_b_w, sizeof(qmd0_b_w)); |
| 1221 | memcpy(qmd1_b, qmd1_b_w, sizeof(qmd1_b_w)); |
| 1222 | *(volatile uint32_t *)sem_b = 0; |
| 1223 | |
| 1224 | uint32_t pb_b[1024]; size_t pb_b_len = 0; |
| 1225 | memcpy(&pb_b[pb_b_len], WORLD_SETUP_WORDS, sizeof(WORLD_SETUP_WORDS)); |
| 1226 | pb_b_len += sizeof(WORLD_SETUP_WORDS) / 4; |
| 1227 | pb_b[pb_b_len++] = nvrm_mthd(1, 0x0188, 2); |
| 1228 | pb_b[pb_b_len++] = (uint32_t)(cbank_b_va >> 32); |
| 1229 | pb_b[pb_b_len++] = (uint32_t)cbank_b_va; |
| 1230 | pb_b[pb_b_len++] = nvrm_mthd(1, 0x0180, 2); |
| 1231 | pb_b[pb_b_len++] = 0x00000380; pb_b[pb_b_len++] = 0x00000001; |
| 1232 | pb_b[pb_b_len++] = nvrm_mthd(1, 0x01b0, 1); pb_b[pb_b_len++] = 0x00000041; |
| 1233 | pb_b[pb_b_len++] = (224 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28); |
| 1234 | memcpy(&pb_b[pb_b_len], cbank_b_data, 224 * 4); pb_b_len += 224; |
| 1235 | pb_b[pb_b_len++] = nvrm_mthd(1, 0x0188, 2); |
| 1236 | pb_b[pb_b_len++] = (uint32_t)((cbank_b_va + 0x380) >> 32); |
| 1237 | pb_b[pb_b_len++] = (uint32_t)(cbank_b_va + 0x380); |
| 1238 | pb_b[pb_b_len++] = nvrm_mthd(1, 0x0180, 2); |
| 1239 | pb_b[pb_b_len++] = 0x00000028; pb_b[pb_b_len++] = 0x00000001; |
| 1240 | pb_b[pb_b_len++] = nvrm_mthd(1, 0x01b0, 1); pb_b[pb_b_len++] = 0x00000041; |
| 1241 | pb_b[pb_b_len++] = (10 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28); |
| 1242 | memcpy(&pb_b[pb_b_len], cbank_b_args, 10 * 4); pb_b_len += 10; |
| 1243 | pb_b[pb_b_len++] = (98 << 16) | (1 << 13) | (0x0318 >> 2) | (2u << 28); |
| 1244 | pb_b[pb_b_len++] = (1u << 30) | (uint32_t)((qmd0_b_va >> 40) & 0x1ff); |
| 1245 | pb_b[pb_b_len++] = (uint32_t)(qmd0_b_va >> 8); |
| 1246 | memcpy(&pb_b[pb_b_len], qmd0_b_w, 96 * 4); pb_b_len += 96; |
| 1247 | pb_b[pb_b_len++] = nvrm_mthd(1, 0x0188, 2); |
| 1248 | pb_b[pb_b_len++] = (uint32_t)(sem_b_va >> 32); |
| 1249 | pb_b[pb_b_len++] = (uint32_t)sem_b_va; |
| 1250 | pb_b[pb_b_len++] = nvrm_mthd(1, 0x0180, 2); |
| 1251 | pb_b[pb_b_len++] = 0x00000004; pb_b[pb_b_len++] = 0x00000001; |
| 1252 | pb_b[pb_b_len++] = nvrm_mthd(1, 0x01b0, 1); pb_b[pb_b_len++] = 0x00000041; |
| 1253 | pb_b[pb_b_len++] = (1 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28); |
| 1254 | pb_b[pb_b_len++] = OMEGA_BW_SEMAPHORE_INTERMEDIATE_INIT; |
| 1255 | pb_b[pb_b_len++] = (98 << 16) | (1 << 13) | (0x0318 >> 2) | (2u << 28); |
| 1256 | pb_b[pb_b_len++] = (1u << 30) | (uint32_t)((qmd1_b_va >> 40) & 0x1ff); |
| 1257 | pb_b[pb_b_len++] = (uint32_t)(qmd1_b_va >> 8); |
| 1258 | memcpy(&pb_b[pb_b_len], qmd1_b_w, 96 * 4); pb_b_len += 96; |
| 1259 | size_t rel_b = pb_b_len; |
| 1260 | pb_b[pb_b_len++] = nvrm_mthd(0, 0x005c, 5); |
| 1261 | pb_b[pb_b_len++] = (uint32_t)world.completion.gpu_va; |
| 1262 | pb_b[pb_b_len++] = (uint32_t)(world.completion.gpu_va >> 32); |
| 1263 | pb_b[pb_b_len++] = 0; pb_b[pb_b_len++] = 0; |
| 1264 | pb_b[pb_b_len++] = 0x1 | (1u << 20); |
| 1265 | |
| 1266 | /* Semantic descriptors are retained per dispatch and folded into the |
| 1267 | * rolling digest by the world only after completion is observed. */ |
| 1268 | const uint32_t v_words[5] = {1, vec_n, 0, 0, 0}; |
| 1269 | const uint32_t i_words[5] = {2, 16, 16, 16, OMEGA_MATMUL_PRECISION_INT32}; |
| 1270 | const uint32_t f_words[5] = {2, 16, 16, 16, OMEGA_MATMUL_PRECISION_FP16}; |
| 1271 | const uint32_t b_words[5] = {2, 16, 16, 16, OMEGA_MATMUL_PRECISION_BF16}; |
| 1272 | |
| 1273 | /* pb_pool is 0x40000 bytes = 64 slots of 0x1000. A slot and a writable |
| 1274 | * result buffer must not be reused until the earlier dispatch retires and |
| 1275 | * its result digest is committed. Each group has four distinct outputs. */ |
| 1276 | const uint32_t pb_slots = 0x40000 / 0x1000; |
| 1277 | uint32_t dispatch_id = 0; |
| 1278 | uint32_t in_flight = 0; |
| 1279 | OmegaWorldSubmission sub; |
| 1280 | |
| 1281 | long rss_before = get_resident_pages(); |
| 1282 | |
| 1283 | for (uint32_t cycle = 0; cycle < 250; cycle++) { |
| 1284 | /* 1. VecAdd */ |
| 1285 | dispatch_id++; |
| 1286 | pb_v[rel_v + 3] = dispatch_id; |
| 1287 | m19_make_submission(&sub, v_words, &code_vecadd, &v_a, &v_b, &v_c, |
| 1288 | (uint32_t)vec_bytes, (uint32_t)vec_bytes, (uint32_t)vec_bytes, |
| 1289 | dispatch_id); |
| 1290 | if (!m19_submit_op(&world, &pb_pool, pb_v, pb_v_len, |
| 1291 | (dispatch_id - 1) % pb_slots, &sub)) { |
| 1292 | omega_world_destroy(&world); |
| 1293 | return false; |
| 1294 | } |
| 1295 | in_flight++; |
| 1296 | |
| 1297 | /* 2. INT32 MatMul */ |
| 1298 | dispatch_id++; |
| 1299 | pb_i[rel_i + 3] = dispatch_id; |
| 1300 | m19_make_submission(&sub, i_words, &code_i32, &m_a_i32, &m_b_i32, &m_c_i32, |
| 1301 | (uint32_t)mat_bytes_i32, (uint32_t)mat_bytes_i32, (uint32_t)mat_bytes_out, |
| 1302 | dispatch_id); |
| 1303 | if (!m19_submit_op(&world, &pb_pool, pb_i, pb_i_len, |
| 1304 | (dispatch_id - 1) % pb_slots, &sub)) { |
| 1305 | omega_world_destroy(&world); |
| 1306 | return false; |
| 1307 | } |
| 1308 | in_flight++; |
| 1309 | |
| 1310 | /* 3. FP16 MatMul */ |
| 1311 | dispatch_id++; |
| 1312 | pb_f[rel_f + 3] = dispatch_id; |
| 1313 | m19_make_submission(&sub, f_words, &code_f16, &m_a_f16, &m_b_f16, &m_c_f16, |
| 1314 | (uint32_t)mat_bytes_f16, (uint32_t)mat_bytes_f16, (uint32_t)mat_bytes_out, |
| 1315 | dispatch_id); |
| 1316 | if (!m19_submit_op(&world, &pb_pool, pb_f, pb_f_len, |
| 1317 | (dispatch_id - 1) % pb_slots, &sub)) { |
| 1318 | omega_world_destroy(&world); |
| 1319 | return false; |
| 1320 | } |
| 1321 | in_flight++; |
| 1322 | |
| 1323 | /* 4. BF16 MatMul */ |
| 1324 | dispatch_id++; |
| 1325 | pb_b[rel_b + 3] = dispatch_id; |
| 1326 | m19_make_submission(&sub, b_words, &code_bf16, &m_a_bf16, &m_b_bf16, &m_c_bf16, |
| 1327 | (uint32_t)mat_bytes_f16, (uint32_t)mat_bytes_f16, (uint32_t)mat_bytes_out, |
| 1328 | dispatch_id); |
| 1329 | if (!m19_submit_op(&world, &pb_pool, pb_b, pb_b_len, |
| 1330 | (dispatch_id - 1) % pb_slots, &sub)) { |
| 1331 | omega_world_destroy(&world); |
| 1332 | return false; |
| 1333 | } |
| 1334 | in_flight++; |
| 1335 | |
| 1336 | if (in_flight >= 4) { |
| 1337 | if (omega_world_ring(&world) != OMEGA_WORLD_OK || |
| 1338 | omega_world_drain(&world, dispatch_id, 10000) < 0) { |
| 1339 | omega_world_destroy(&world); |
| 1340 | return false; |
| 1341 | } |
| 1342 | in_flight = 0; |
| 1343 | } |
| 1344 | } |
| 1345 | if (in_flight > 0) { |
| 1346 | if (omega_world_ring(&world) != OMEGA_WORLD_OK || |
| 1347 | omega_world_drain(&world, dispatch_id, 10000) < 0) { |
| 1348 | omega_world_destroy(&world); |
| 1349 | return false; |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | if (world.channel_generation != 1 || world.channel_reconstructions != 0) { |
| 1354 | omega_world_destroy(&world); |
| 1355 | return false; |
| 1356 | } |
| 1357 | for (uint32_t i = 0; i < vec_n; i++) { |
| 1358 | if (vc_cpu[i] != i + 1) { omega_world_destroy(&world); return false; } |
| 1359 | } |
| 1360 | for (uint32_t i = 0; i < 256; i++) { |
| 1361 | if (mci_cpu[i] != 1 || |
| 1362 | fabsf(fc_cpu[i] - 1.0f) > 1e-4f || |
| 1363 | fabsf(bc_cpu[i] - 1.0f) > 1e-4f) { |
| 1364 | omega_world_destroy(&world); |
| 1365 | return false; |
| 1366 | } |
| 1367 | } |
| 1368 | /* Authoritative accounting: advanced only on observed completion. */ |
| 1369 | if (world.total_dispatches != 1000 || world.sequence_number != 1000) { |
| 1370 | omega_world_destroy(&world); |
| 1371 | return false; |
| 1372 | } |
| 1373 | |
| 1374 | long rss_after = get_resident_pages(); |
| 1375 | |
| 1376 | m19_sustained_dispatches = (uint32_t)world.total_dispatches; |
| 1377 | m19_sustained_rss_delta = (rss_before >= 0 && rss_after >= 0) ? (rss_after - rss_before) : 0; |
| 1378 | memcpy(m19_sustained_digest, world.rolling_state_digest, 32); |
| 1379 | |
| 1380 | omega_blackwell_kernel_free(&k_i32); |
| 1381 | omega_blackwell_kernel_free(&k_f16); |
| 1382 | omega_blackwell_kernel_free(&k_bf16); |
| 1383 | omega_world_destroy(&world); |
| 1384 | return true; |
| 1385 | } |
| 1386 | |
| 1387 | /* Silicon test, not a canonical M19 gate: completion payloads deliberately |
| 1388 | * differ from the GPFIFO put sequence, proving retirement follows queue |
| 1389 | * accounting (gp_seq) rather than completion identity. */ |
| 1390 | static bool test_m19_drain_decoupling(void) { |
| 1391 | OmegaAcceleratorWorld world; |
| 1392 | if (omega_world_init(&world) != OMEGA_WORLD_OK) return false; |
| 1393 | |
| 1394 | uint8_t code_buf[1024]; |
| 1395 | size_t code_len = 0; |
| 1396 | if (omega_blackwell_encode_vecadd(code_buf, sizeof(code_buf), &code_len) != 0) { |
| 1397 | omega_world_destroy(&world); |
| 1398 | return false; |
| 1399 | } |
| 1400 | OmegaHandle code, a, b, c; |
| 1401 | if (omega_world_register_code(&world, code_buf, code_len, NULL, &code) != OMEGA_WORLD_OK || |
| 1402 | omega_world_register_buffer(&world, 256, OMEGA_PERM_READ, &a) != OMEGA_WORLD_OK || |
| 1403 | omega_world_register_buffer(&world, 256, OMEGA_PERM_READ, &b) != OMEGA_WORLD_OK || |
| 1404 | omega_world_register_buffer(&world, 256, OMEGA_PERM_READ | OMEGA_PERM_WRITE, &c) != OMEGA_WORLD_OK) { |
| 1405 | omega_world_destroy(&world); |
| 1406 | return false; |
| 1407 | } |
| 1408 | uint32_t *pa = NULL, *pb = NULL; |
| 1409 | uint64_t va; |
| 1410 | if (omega_world_resolve_buffer(&world, &a, OMEGA_PERM_READ, 0, 256, (void **)&pa, &va) != OMEGA_WORLD_OK || |
| 1411 | omega_world_resolve_buffer(&world, &b, OMEGA_PERM_READ, 0, 256, (void **)&pb, &va) != OMEGA_WORLD_OK) { |
| 1412 | omega_world_destroy(&world); |
| 1413 | return false; |
| 1414 | } |
| 1415 | for (int i = 0; i < 64; i++) { pa[i] = (uint32_t)i; pb[i] = 1; } |
| 1416 | |
| 1417 | /* Minimal pushbuffer: channel setup + completion release only. This |
| 1418 | * exercises submission/completion accounting, not kernel execution. */ |
| 1419 | uint32_t tpb[32]; |
| 1420 | size_t tpb_len = 0; |
| 1421 | memcpy(&tpb[tpb_len], WORLD_SETUP_WORDS, sizeof(WORLD_SETUP_WORDS)); |
| 1422 | tpb_len += sizeof(WORLD_SETUP_WORDS) / 4; |
| 1423 | size_t trel = tpb_len; |
| 1424 | tpb[tpb_len++] = nvrm_mthd(0, 0x005c, 5); |
| 1425 | tpb[tpb_len++] = (uint32_t)world.completion.gpu_va; |
| 1426 | tpb[tpb_len++] = (uint32_t)(world.completion.gpu_va >> 32); |
| 1427 | tpb[tpb_len++] = 0; |
| 1428 | tpb[tpb_len++] = 0; |
| 1429 | tpb[tpb_len++] = 0x1 | (1u << 20); |
| 1430 | |
| 1431 | const uint32_t words[5] = {7, 0, 0, 0, 0}; |
| 1432 | const uint32_t base = 500; |
| 1433 | uint32_t put0 = world.m16.rm.put; |
| 1434 | for (uint32_t i = 0; i < 3; i++) { |
| 1435 | uint32_t payload = base + 1 + i; |
| 1436 | tpb[trel + 3] = payload; |
| 1437 | OmegaWorldSubmission sub; |
| 1438 | m19_make_submission(&sub, words, &code, &a, &b, &c, 256, 256, 256, payload); |
| 1439 | if (!m19_submit_op(&world, &world.m16.pb_mem, tpb, tpb_len, i, &sub)) { |
| 1440 | omega_world_destroy(&world); |
| 1441 | return false; |
| 1442 | } |
| 1443 | } |
| 1444 | if (omega_world_ring(&world) != OMEGA_WORLD_OK) { omega_world_destroy(&world); return false; } |
| 1445 | int committed = omega_world_drain(&world, base + 3, 10000); |
| 1446 | bool ok = (committed == 3 && |
| 1447 | world.total_dispatches == 3 && |
| 1448 | world.sequence_number == 3 && |
| 1449 | world.m16.rm.put == put0 + 3 && |
| 1450 | world.m16.rm.retired == put0 + 3 && |
| 1451 | *world.completion.cpu_marker == base + 3); |
| 1452 | omega_world_destroy(&world); |
| 1453 | return ok; |
| 1454 | } |
| 1455 | |
| 1456 | int run_m19_drain_decoupling(void) { |
| 1457 | bool pass = test_m19_drain_decoupling(); |
| 1458 | printf("M19 drain decoupling (payloads 501-503, put 1-3): %s\n", pass ? "PASS" : "FAIL"); |
| 1459 | return pass ? 0 : 1; |
| 1460 | } |
| 1461 | |
| 1462 | /* Gate 9: OMEGA_ACCEL_RESIDENT_GENERATION_PASS */ |
| 1463 | static bool test_m19_gate9_generation(void) { |
| 1464 | OmegaAcceleratorWorld world; |
| 1465 | if (omega_world_init(&world) != OMEGA_WORLD_OK) return false; |
| 1466 | |
| 1467 | OmegaHandle h1; |
| 1468 | if (omega_world_register_buffer(&world, 4096, OMEGA_PERM_READ | OMEGA_PERM_WRITE, &h1) != OMEGA_WORLD_OK) { |
| 1469 | omega_world_destroy(&world); |
| 1470 | return false; |
| 1471 | } |
| 1472 | if (h1.world_epoch != world.handle_epoch || h1.object_id != 0 || h1.object_generation != 1) { |
| 1473 | omega_world_destroy(&world); |
| 1474 | return false; |
| 1475 | } |
| 1476 | |
| 1477 | if (omega_world_validate_handle(&world, &h1, OMEGA_OBJ_BUFFER, OMEGA_PERM_READ) != OMEGA_WORLD_OK) { |
| 1478 | omega_world_destroy(&world); |
| 1479 | return false; |
| 1480 | } |
| 1481 | |
| 1482 | if (omega_world_revoke_buffer(&world, &h1) != OMEGA_WORLD_OK) { |
| 1483 | omega_world_destroy(&world); |
| 1484 | return false; |
| 1485 | } |
| 1486 | |
| 1487 | if (omega_world_validate_handle(&world, &h1, OMEGA_OBJ_BUFFER, OMEGA_PERM_READ) == OMEGA_WORLD_OK) { |
| 1488 | omega_world_destroy(&world); |
| 1489 | return false; |
| 1490 | } |
| 1491 | |
| 1492 | OmegaHandle h2; |
| 1493 | if (omega_world_register_buffer(&world, 4096, OMEGA_PERM_READ | OMEGA_PERM_WRITE, &h2) != OMEGA_WORLD_OK) { |
| 1494 | omega_world_destroy(&world); |
| 1495 | return false; |
| 1496 | } |
| 1497 | if (h2.object_id != 0 || h2.object_generation != 2) { |
| 1498 | omega_world_destroy(&world); |
| 1499 | return false; |
| 1500 | } |
| 1501 | |
| 1502 | if (omega_world_validate_handle(&world, &h1, OMEGA_OBJ_BUFFER, OMEGA_PERM_READ) == OMEGA_WORLD_OK || |
| 1503 | omega_world_validate_handle(&world, &h2, OMEGA_OBJ_BUFFER, OMEGA_PERM_READ) != OMEGA_WORLD_OK) { |
| 1504 | omega_world_destroy(&world); |
| 1505 | return false; |
| 1506 | } |
| 1507 | |
| 1508 | if (omega_world_rebuild(&world) != OMEGA_WORLD_OK) return false; |
| 1509 | if (omega_world_validate_handle(&world, &h2, OMEGA_OBJ_BUFFER, OMEGA_PERM_READ) != OMEGA_WORLD_ERR_STALE_EPOCH) { |
| 1510 | omega_world_destroy(&world); |
| 1511 | return false; |
| 1512 | } |
| 1513 | |
| 1514 | omega_world_destroy(&world); |
| 1515 | return true; |
| 1516 | } |
| 1517 | |
| 1518 | /* Gate 10: OMEGA_ACCEL_RESIDENT_STALE_HANDLE_REFUSAL_PASS */ |
| 1519 | static bool test_m19_gate10_stale_handle_refusal(void) { |
| 1520 | OmegaAcceleratorWorld world; |
| 1521 | if (omega_world_init(&world) != OMEGA_WORLD_OK) return false; |
| 1522 | |
| 1523 | OmegaHandle h; |
| 1524 | omega_world_register_buffer(&world, 4096, OMEGA_PERM_READ | OMEGA_PERM_WRITE, &h); |
| 1525 | |
| 1526 | OmegaHandle bad_gen = h; |
| 1527 | bad_gen.object_generation = 999; |
| 1528 | if (omega_world_validate_handle(&world, &bad_gen, OMEGA_OBJ_BUFFER, OMEGA_PERM_READ) != OMEGA_WORLD_ERR_STALE_GEN) { |
| 1529 | omega_world_destroy(&world); |
| 1530 | return false; |
| 1531 | } |
| 1532 | |
| 1533 | OmegaHandle bad_epoch = h; |
| 1534 | bad_epoch.world_epoch = 999; |
| 1535 | if (omega_world_validate_handle(&world, &bad_epoch, OMEGA_OBJ_BUFFER, OMEGA_PERM_READ) != OMEGA_WORLD_ERR_STALE_EPOCH) { |
| 1536 | omega_world_destroy(&world); |
| 1537 | return false; |
| 1538 | } |
| 1539 | |
| 1540 | OmegaHandle h_rev; |
| 1541 | omega_world_register_buffer(&world, 4096, OMEGA_PERM_READ, &h_rev); |
| 1542 | omega_world_revoke_buffer(&world, &h_rev); |
| 1543 | if (omega_world_validate_handle(&world, &h_rev, OMEGA_OBJ_BUFFER, OMEGA_PERM_READ) == OMEGA_WORLD_OK) { |
| 1544 | omega_world_destroy(&world); |
| 1545 | return false; |
| 1546 | } |
| 1547 | |
| 1548 | if (omega_world_validate_handle(&world, &h, OMEGA_OBJ_CODE, OMEGA_PERM_READ) != OMEGA_WORLD_ERR_INVALID_ARG) { |
| 1549 | omega_world_destroy(&world); |
| 1550 | return false; |
| 1551 | } |
| 1552 | |
| 1553 | if (omega_world_validate_handle(&world, &h, OMEGA_OBJ_BUFFER, OMEGA_PERM_EXECUTE) != OMEGA_WORLD_ERR_PERM_DENIED) { |
| 1554 | omega_world_destroy(&world); |
| 1555 | return false; |
| 1556 | } |
| 1557 | |
| 1558 | void *cpu = NULL; |
| 1559 | uint64_t va = 0; |
| 1560 | if (omega_world_resolve_buffer(&world, &h, OMEGA_PERM_READ, 4090, 100, &cpu, &va) != OMEGA_WORLD_ERR_BOUNDS) { |
| 1561 | omega_world_destroy(&world); |
| 1562 | return false; |
| 1563 | } |
| 1564 | |
| 1565 | omega_world_destroy(&world); |
| 1566 | return true; |
| 1567 | } |
| 1568 | |
| 1569 | /* Gate 11: OMEGA_ACCEL_RESIDENT_REVOCATION_PASS */ |
| 1570 | static bool test_m19_gate11_revocation(void) { |
| 1571 | OmegaAcceleratorWorld world; |
| 1572 | if (omega_world_init(&world) != OMEGA_WORLD_OK) return false; |
| 1573 | |
| 1574 | OmegaHandle buf_h; |
| 1575 | if (omega_world_register_buffer(&world, 4096, OMEGA_PERM_READ | OMEGA_PERM_WRITE, &buf_h) != OMEGA_WORLD_OK) { |
| 1576 | omega_world_destroy(&world); |
| 1577 | return false; |
| 1578 | } |
| 1579 | void *cpu = NULL; |
| 1580 | uint64_t va = 0; |
| 1581 | if (omega_world_resolve_buffer(&world, &buf_h, OMEGA_PERM_READ, 0, 100, &cpu, &va) != OMEGA_WORLD_OK) { |
| 1582 | omega_world_destroy(&world); |
| 1583 | return false; |
| 1584 | } |
| 1585 | |
| 1586 | if (omega_world_revoke_buffer(&world, &buf_h) != OMEGA_WORLD_OK) { |
| 1587 | omega_world_destroy(&world); |
| 1588 | return false; |
| 1589 | } |
| 1590 | |
| 1591 | if (omega_world_resolve_buffer(&world, &buf_h, OMEGA_PERM_READ, 0, 100, &cpu, &va) == OMEGA_WORLD_OK || |
| 1592 | omega_world_validate_handle(&world, &buf_h, OMEGA_OBJ_BUFFER, OMEGA_PERM_READ) == OMEGA_WORLD_OK) { |
| 1593 | omega_world_destroy(&world); |
| 1594 | return false; |
| 1595 | } |
| 1596 | |
| 1597 | uint8_t code_buf[1024]; |
| 1598 | size_t code_len = 0; |
| 1599 | omega_blackwell_encode_vecadd(code_buf, sizeof(code_buf), &code_len); |
| 1600 | OmegaHandle code_h; |
| 1601 | if (omega_world_register_code(&world, code_buf, code_len, NULL, &code_h) != OMEGA_WORLD_OK) { |
| 1602 | omega_world_destroy(&world); |
| 1603 | return false; |
| 1604 | } |
| 1605 | const void *res_cpu = NULL; |
| 1606 | uint64_t res_va = 0; |
| 1607 | size_t res_size = 0; |
| 1608 | if (omega_world_resolve_code(&world, &code_h, &res_cpu, &res_va, &res_size) != OMEGA_WORLD_OK) { |
| 1609 | omega_world_destroy(&world); |
| 1610 | return false; |
| 1611 | } |
| 1612 | |
| 1613 | if (omega_world_revoke_code(&world, &code_h) != OMEGA_WORLD_OK) { |
| 1614 | omega_world_destroy(&world); |
| 1615 | return false; |
| 1616 | } |
| 1617 | if (omega_world_resolve_code(&world, &code_h, &res_cpu, &res_va, &res_size) == OMEGA_WORLD_OK) { |
| 1618 | omega_world_destroy(&world); |
| 1619 | return false; |
| 1620 | } |
| 1621 | |
| 1622 | omega_world_destroy(&world); |
| 1623 | return true; |
| 1624 | } |
| 1625 | |
| 1626 | /* Gate 12: OMEGA_ACCEL_RESIDENT_FAULT_RECOVERY_PASS */ |
| 1627 | static bool test_m19_gate12_fault_recovery(void) { |
| 1628 | OmegaAcceleratorWorld world; |
| 1629 | if (omega_world_init(&world) != OMEGA_WORLD_OK) return false; |
| 1630 | |
| 1631 | uint8_t vecadd_buf[1024]; |
| 1632 | size_t vecadd_len = 0; |
| 1633 | omega_blackwell_encode_vecadd(vecadd_buf, sizeof(vecadd_buf), &vecadd_len); |
| 1634 | OmegaHandle code_h; |
| 1635 | omega_world_register_code(&world, vecadd_buf, vecadd_len, NULL, &code_h); |
| 1636 | |
| 1637 | OmegaHandle h_a, h_b, h_c; |
| 1638 | omega_world_register_buffer(&world, 4096, OMEGA_PERM_READ, &h_a); |
| 1639 | omega_world_register_buffer(&world, 4096, OMEGA_PERM_READ, &h_b); |
| 1640 | omega_world_register_buffer(&world, 4096, OMEGA_PERM_READ | OMEGA_PERM_WRITE, &h_c); |
| 1641 | |
| 1642 | uint32_t *a_cpu = NULL, *b_cpu = NULL, *c_cpu = NULL; |
| 1643 | uint64_t dummy = 0; |
| 1644 | omega_world_resolve_buffer(&world, &h_a, OMEGA_PERM_READ, 0, 4096, (void **)&a_cpu, &dummy); |
| 1645 | omega_world_resolve_buffer(&world, &h_b, OMEGA_PERM_READ, 0, 4096, (void **)&b_cpu, &dummy); |
| 1646 | omega_world_resolve_buffer(&world, &h_c, OMEGA_PERM_READ, 0, 4096, (void **)&c_cpu, &dummy); |
| 1647 | |
| 1648 | a_cpu[0] = 50; b_cpu[0] = 25; c_cpu[0] = 0; |
| 1649 | uint32_t compl = 0; |
| 1650 | if (omega_world_dispatch_vector(&world, &code_h, &h_a, &h_b, &h_c, 64, &compl) != OMEGA_WORLD_OK || |
| 1651 | c_cpu[0] != 75) { |
| 1652 | omega_world_destroy(&world); |
| 1653 | return false; |
| 1654 | } |
| 1655 | |
| 1656 | uint32_t orig_gen = world.channel_generation; |
| 1657 | uint32_t root = world.m16.rm.root; |
| 1658 | uint32_t device = world.m16.rm.device; |
| 1659 | uint32_t vaspace = world.m16.rm.vaspace; |
| 1660 | uint32_t put_before = world.m16.rm.put; |
| 1661 | uint64_t completed_before = world.total_dispatches; |
| 1662 | |
| 1663 | world.channel_active = false; |
| 1664 | if (omega_world_dispatch_vector(&world, &code_h, &h_a, &h_b, &h_c, 64, &compl) != OMEGA_WORLD_ERR_HARDWARE || |
| 1665 | world.m16.rm.put != put_before || world.total_dispatches != completed_before) { |
| 1666 | omega_world_destroy(&world); |
| 1667 | return false; |
| 1668 | } |
| 1669 | |
| 1670 | if (omega_world_recover_channel_fault(&world) != OMEGA_WORLD_OK) { |
| 1671 | omega_world_destroy(&world); |
| 1672 | return false; |
| 1673 | } |
| 1674 | |
| 1675 | if (world.channel_generation != orig_gen + 1 || |
| 1676 | world.channel_reconstructions != 1 || |
| 1677 | !world.channel_active || |
| 1678 | world.m16.rm.root != root || |
| 1679 | world.m16.rm.device != device || |
| 1680 | world.m16.rm.vaspace != vaspace) { |
| 1681 | omega_world_destroy(&world); |
| 1682 | return false; |
| 1683 | } |
| 1684 | |
| 1685 | a_cpu[0] = 100; b_cpu[0] = 200; c_cpu[0] = 0; |
| 1686 | if (omega_world_dispatch_vector(&world, &code_h, &h_a, &h_b, &h_c, 64, &compl) != OMEGA_WORLD_OK || |
| 1687 | c_cpu[0] != 300) { |
| 1688 | omega_world_destroy(&world); |
| 1689 | return false; |
| 1690 | } |
| 1691 | |
| 1692 | /* Every condition below was already checked above; record what was |
| 1693 | * actually observed for the receipt instead of asserting it there. */ |
| 1694 | m19_fault_channel_scoped_handled = true; |
| 1695 | m19_fault_channel_generation_advanced = (world.channel_generation == orig_gen + 1); |
| 1696 | m19_fault_vas_preserved = (world.m16.rm.root == root && world.m16.rm.device == device && world.m16.rm.vaspace == vaspace); |
| 1697 | m19_fault_zero_device_reset = true; /* no device-reset path was invoked on this route */ |
| 1698 | |
| 1699 | omega_world_destroy(&world); |
| 1700 | return true; |
| 1701 | } |
| 1702 | |
| 1703 | /* Gate 13: OMEGA_ACCEL_RESIDENT_MEMORY_BOUND_PASS */ |
| 1704 | static bool test_m19_gate13_memory_bound(void) { |
| 1705 | if (m19_sustained_dispatches < 1000 || m19_sustained_rss_delta < 0 || |
| 1706 | m19_sustained_rss_delta > 1) return false; |
| 1707 | |
| 1708 | OmegaAcceleratorWorld world; |
| 1709 | if (omega_world_init(&world) != OMEGA_WORLD_OK) return false; |
| 1710 | |
| 1711 | uint8_t vecadd_buf[1024]; |
| 1712 | size_t vecadd_len = 0; |
| 1713 | omega_blackwell_encode_vecadd(vecadd_buf, sizeof(vecadd_buf), &vecadd_len); |
| 1714 | OmegaHandle code_h; |
| 1715 | omega_world_register_code(&world, vecadd_buf, vecadd_len, NULL, &code_h); |
| 1716 | |
| 1717 | OmegaHandle h_a, h_b, h_c; |
| 1718 | omega_world_register_buffer(&world, 4096, OMEGA_PERM_READ, &h_a); |
| 1719 | omega_world_register_buffer(&world, 4096, OMEGA_PERM_READ, &h_b); |
| 1720 | omega_world_register_buffer(&world, 4096, OMEGA_PERM_READ | OMEGA_PERM_WRITE, &h_c); |
| 1721 | |
| 1722 | uint32_t compl = 0; |
| 1723 | for (int i = 0; i < 10; i++) { |
| 1724 | omega_world_dispatch_vector(&world, &code_h, &h_a, &h_b, &h_c, 64, &compl); |
| 1725 | } |
| 1726 | long rss_before = get_resident_pages(); |
| 1727 | for (int i = 0; i < 200; i++) { |
| 1728 | omega_world_dispatch_vector(&world, &code_h, &h_a, &h_b, &h_c, 64, &compl); |
| 1729 | } |
| 1730 | long rss_after = get_resident_pages(); |
| 1731 | omega_world_destroy(&world); |
| 1732 | if (rss_before < 0 || rss_after < 0) return false; |
| 1733 | return ((rss_after - rss_before) <= 1); |
| 1734 | } |
| 1735 | |
| 1736 | /* Gate 14: OMEGA_ACCEL_RESIDENT_STATE_DIGEST_PASS */ |
| 1737 | static bool test_m19_gate14_state_digest(void) { |
| 1738 | const uint8_t zero[32] = {0}; |
| 1739 | if (m19_sustained_dispatches != 1000 || |
| 1740 | memcmp(m19_sustained_digest, zero, sizeof(zero)) == 0) return false; |
| 1741 | |
| 1742 | uint8_t digest1[32], digest2[32]; |
| 1743 | for (int run = 0; run < 2; run++) { |
| 1744 | OmegaAcceleratorWorld world; |
| 1745 | if (omega_world_init(&world) != OMEGA_WORLD_OK) return false; |
| 1746 | |
| 1747 | uint8_t sem_id[32] = {1, 2, 3}; |
| 1748 | uint8_t real_id[32] = {4, 5, 6}; |
| 1749 | uint8_t code_dig[32] = {7, 8, 9}; |
| 1750 | uint8_t input_dig[32] = {10, 11, 12}; |
| 1751 | uint32_t pairs[4] = {0, 1, 1, 1}; |
| 1752 | |
| 1753 | for (uint32_t op = 1; op <= 10; op++) { |
| 1754 | uint8_t res_dig[32] = { (uint8_t)op }; |
| 1755 | omega_world_update_digest(&world, sem_id, real_id, code_dig, input_dig, pairs, 2, 0x1f, op, res_dig, 0); |
| 1756 | } |
| 1757 | |
| 1758 | if (run == 0) { |
| 1759 | memcpy(digest1, world.rolling_state_digest, 32); |
| 1760 | } else { |
| 1761 | memcpy(digest2, world.rolling_state_digest, 32); |
| 1762 | } |
| 1763 | omega_world_destroy(&world); |
| 1764 | } |
| 1765 | |
| 1766 | return (memcmp(digest1, digest2, 32) == 0); |
| 1767 | } |
| 1768 | |
| 1769 | /* Gate 15: OMEGA_ACCEL_RESIDENT_ZERO_LIBCUDA_PASS */ |
| 1770 | static bool test_m19_gate15_zero_libcuda(void) { |
| 1771 | if (omega_blackwell_verify_zero_libcuda_linkage(NULL) != 0) return false; |
| 1772 | if (omega_blackwell_verify_zero_cuda_symbols(NULL) != 0) return false; |
| 1773 | if (omega_blackwell_verify_zero_libcuda_runtime() != 0) return false; |
| 1774 | return true; |
| 1775 | } |
| 1776 | |
| 1777 | |
| 1778 | /* Start one program directly, with an optional working directory, one extra |
| 1779 | * environment value, and stdout+stderr sent to log_path. No shell is involved, |
| 1780 | * so a path, a commit id or a directory name passed here stays an argument and |
| 1781 | * can never become command text. Returns the exit status, or -1 if it did not run. */ |
| 1782 | static int m19_run(char *const argv[], const char *dir, const char *log_path, |
| 1783 | const char *e1_name, const char *e1_value, |
| 1784 | const char *e2_name, const char *e2_value) { |
| 1785 | if (!argv || !argv[0]) return -1; |
| 1786 | pid_t pid = fork(); |
| 1787 | if (pid < 0) return -1; |
| 1788 | if (pid == 0) { |
| 1789 | if (dir && chdir(dir) != 0) _exit(127); |
| 1790 | if (e1_name && e1_value && setenv(e1_name, e1_value, 1) != 0) _exit(127); |
| 1791 | if (e2_name && e2_value && setenv(e2_name, e2_value, 1) != 0) _exit(127); |
| 1792 | if (log_path) { |
| 1793 | int fd = open(log_path, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 1794 | if (fd < 0) _exit(127); |
| 1795 | if (dup2(fd, STDOUT_FILENO) < 0 || dup2(fd, STDERR_FILENO) < 0) _exit(127); |
| 1796 | if (fd > STDERR_FILENO) close(fd); |
| 1797 | } |
| 1798 | execvp(argv[0], argv); |
| 1799 | _exit(127); |
| 1800 | } |
| 1801 | int status = 0; |
| 1802 | if (waitpid(pid, &status, 0) < 0) return -1; |
| 1803 | if (WIFEXITED(status)) return WEXITSTATUS(status); |
| 1804 | return -1; |
| 1805 | } |
| 1806 | /* Echo the tail of the nested clean-clone log to stderr. The file is read |
| 1807 | * directly: building a shell command out of a path is both flagged by static |
| 1808 | * analysis and a way for a crafted path to run something unintended. */ |
| 1809 | static void m19_echo_log_tail(const char *dir) { |
| 1810 | char path[PATH_MAX]; |
| 1811 | int n = snprintf(path, sizeof(path), "%s/qualification.log", dir); |
| 1812 | if (n < 0 || (size_t)n >= sizeof(path)) return; |
| 1813 | FILE *f = fopen(path, "rb"); |
| 1814 | if (!f) return; |
| 1815 | char buf[2048]; |
| 1816 | size_t want = sizeof(buf) - 1; |
| 1817 | if (fseek(f, 0, SEEK_END) != 0) { fclose(f); return; } |
| 1818 | long end = ftell(f); |
| 1819 | if (end < 0) { fclose(f); return; } |
| 1820 | long start = end > (long)want ? end - (long)want : 0; |
| 1821 | if (fseek(f, start, SEEK_SET) != 0) { fclose(f); return; } |
| 1822 | size_t got = fread(buf, 1, want, f); |
| 1823 | fclose(f); |
| 1824 | if (got == 0) return; |
| 1825 | buf[got] = '\0'; |
| 1826 | fputs(buf, stderr); |
| 1827 | if (buf[got - 1] != '\n') fputc('\n', stderr); |
| 1828 | } |
| 1829 | |
| 1830 | /* Remove the built copy under the private clone folder; qualification.log stays |
| 1831 | * beside it (omega#526: five such copies per day filled /tmp). */ |
| 1832 | static void m19_clean_clone_remove_checkout(const char *checkout) { |
| 1833 | /* No shell: a file-tree walk that never follows a symlink (omega#565). */ |
| 1834 | if (omega_cc_remove_tree(checkout) != 0) { |
| 1835 | fprintf(stderr, "M19 clean clone: could not remove %s\n", checkout); |
| 1836 | } |
| 1837 | } |
| 1838 | |
| 1839 | /* Gate 16: OMEGA_ACCEL_RESIDENT_CLEAN_CLONE_PASS */ |
| 1840 | static bool test_m19_gate16_clean_clone(void) { |
| 1841 | char src_sha[41] = {0}; |
| 1842 | if (!m19_git_head(".", src_sha)) return false; |
| 1843 | const char *record = getenv("OMEGA_QUAL_RECORD"); |
| 1844 | const char *candidate = getenv("OMEGA_M19R_CANDIDATE"); |
| 1845 | if ((record && strcmp(record, "1") == 0) || candidate) { |
| 1846 | if (!candidate || strlen(candidate) != 40 || |
| 1847 | strspn(candidate, "0123456789abcdefABCDEF") != 40 || |
| 1848 | strcmp(candidate, src_sha) != 0 || omega_evidence_tree_dirty()) return false; |
| 1849 | } |
| 1850 | |
| 1851 | if (getenv("OMEGA_IN_CLEAN_CLONE") != NULL) { |
| 1852 | /* Nested run: record the revision this clone was built from. */ |
| 1853 | memcpy(m19_candidate_commit, src_sha, sizeof(m19_candidate_commit)); |
| 1854 | return true; |
| 1855 | } |
| 1856 | if (omega_evidence_tree_dirty()) { |
| 1857 | return false; |
| 1858 | } |
| 1859 | char clone_template[] = "/tmp/omega_clean_m19_XXXXXX"; |
| 1860 | char *clone = mkdtemp(clone_template); |
| 1861 | if (!clone) return false; |
| 1862 | char checkout[512]; |
| 1863 | int cl = snprintf(checkout, sizeof(checkout), "%s/checkout", clone); |
| 1864 | if (cl < 0 || (size_t)cl >= sizeof(checkout)) return false; |
| 1865 | |
| 1866 | /* Resolve the PHYSICS_DIR this process runs with (omega_physics_dir.h), as an |
| 1867 | * absolute path, so the nested build (running from a different cwd) |
| 1868 | * finds the same pinned physics checkout instead of silently falling |
| 1869 | * back to the Makefile's default. */ |
| 1870 | char physics_dir_resolved[PATH_MAX], physics_err[OMEGA_PHYSICS_ERR_SIZE]; |
| 1871 | if (!omega_physics_dir_resolve_pinned(physics_dir_resolved, sizeof(physics_dir_resolved), 0, physics_err, sizeof(physics_err))) { |
| 1872 | fprintf(stderr, "%s\n", physics_err); |
| 1873 | return false; |
| 1874 | } |
| 1875 | |
| 1876 | /* OUT_DIR=build on the clone's make: tests/gates/run.sh exports a per-step OUT_DIR |
| 1877 | * (omega#511), which the clone would inherit and build into, while the line below runs |
| 1878 | * ./build/omegatool. The clone is its own directory, so its default build/ is private. */ |
| 1879 | /* Each step is started directly, without a shell. The checkout path, the commit id |
| 1880 | * and the physics path are arguments to a program, never words in a command string. */ |
| 1881 | char log_path[PATH_MAX]; |
| 1882 | int lp = snprintf(log_path, sizeof(log_path), "%s/qualification.log", clone); |
| 1883 | if (lp < 0 || (size_t)lp >= sizeof(log_path)) return false; |
| 1884 | |
| 1885 | char *clone_argv[] = { (char *)"git", (char *)"clone", (char *)"--quiet", |
| 1886 | (char *)"--no-hardlinks", (char *)".", checkout, NULL }; |
| 1887 | char *clean_argv[] = { (char *)"make", (char *)"OUT_DIR=build", (char *)"clean", NULL }; |
| 1888 | char *build_argv[] = { (char *)"make", (char *)"-j", (char *)"OUT_DIR=build", NULL }; |
| 1889 | char *gates_argv[] = { (char *)"./build/omegatool", (char *)"--run-m19-gates", NULL }; |
| 1890 | |
| 1891 | /* No value read from a file goes on a command line. The clone carries the source's own |
| 1892 | * head, so the commit id is never passed as an argument; the check further down still |
| 1893 | * refuses a clone whose head is not src_sha. The physics path travels in the environment, |
| 1894 | * which the Makefile's `PHYSICS_DIR ?= ../physics` picks up. */ |
| 1895 | bool nested_ok = m19_run(clone_argv, ".", "/dev/null", NULL, NULL, NULL, NULL) == 0 && |
| 1896 | m19_run(clean_argv, checkout, "/dev/null", NULL, NULL, NULL, NULL) == 0 && |
| 1897 | m19_run(build_argv, checkout, "/dev/null", |
| 1898 | "PHYSICS_DIR", physics_dir_resolved, NULL, NULL) == 0 && |
| 1899 | m19_run(gates_argv, checkout, log_path, |
| 1900 | "OMEGA_IN_CLEAN_CLONE", "1", NULL, NULL) == 0; |
| 1901 | if (!nested_ok) { |
| 1902 | /* The nested clone builds and runs into qualification.log. A failure here used to report |
| 1903 | * only "[FAIL]" with no reason while the log stayed inside the runner's temp directory. |
| 1904 | * Echo the tail to stderr, which the gate runner captures into the evidence record. */ |
| 1905 | fprintf(stderr, "M19 gate 16: nested build or nested run failed; tail of %s:\n", log_path); |
| 1906 | m19_echo_log_tail(clone); |
| 1907 | m19_clean_clone_remove_checkout(checkout); |
| 1908 | return false; |
| 1909 | } |
| 1910 | |
| 1911 | /* The built checkout must be exactly the named candidate revision. */ |
| 1912 | char clone_sha[41] = {0}; |
| 1913 | bool same = m19_git_head(checkout, clone_sha) && strcmp(clone_sha, src_sha) == 0; |
| 1914 | m19_clean_clone_remove_checkout(checkout); |
| 1915 | if (!same) return false; |
| 1916 | |
| 1917 | memcpy(m19_candidate_commit, src_sha, sizeof(m19_candidate_commit)); |
| 1918 | return true; |
| 1919 | } |
| 1920 | |
| 1921 | /* Gate 17: OMEGA_ACCEL_RESIDENT_REGRESSION_PASS |
| 1922 | * |
| 1923 | * Previously this only called run_m18_gates(), which itself only chains |
| 1924 | * run_m17_gates() as ITS regression gate -- so a receipt claiming "157 |
| 1925 | * prior gates passing" was only ever backed by 36 gates actually |
| 1926 | * executing (18 M17 + 18 M18). Actually execute every prior suite (M4, |
| 1927 | * M5..M15, M17, M18) and sum what really ran. */ |
| 1928 | static int m19_regression_total = 0; |
| 1929 | static int m19_regression_passed = 0; |
| 1930 | |
| 1931 | static bool test_m19_gate17_regression(void) { |
| 1932 | int total = 0, passed = 0, c = 0, p = 0; |
| 1933 | |
| 1934 | (void)omega_run_m4_gates(); omega_get_gate_snapshot(&c, &p); total += c; passed += p; |
| 1935 | (void)omega_run_m5_gates(); omega_get_gate_snapshot(&c, &p); total += c; passed += p; |
| 1936 | (void)omega_run_m6_gates(); omega_get_gate_snapshot(&c, &p); total += c; passed += p; |
| 1937 | (void)omega_run_m7_gates(); omega_get_gate_snapshot(&c, &p); total += c; passed += p; |
| 1938 | (void)omega_run_m8_gates(); omega_get_gate_snapshot(&c, &p); total += c; passed += p; |
| 1939 | (void)omega_run_m9_gates(); omega_get_gate_snapshot(&c, &p); total += c; passed += p; |
| 1940 | (void)omega_run_m10_gates(); omega_get_gate_snapshot(&c, &p); total += c; passed += p; |
| 1941 | (void)omega_run_m11_gates(); omega_get_gate_snapshot(&c, &p); total += c; passed += p; |
| 1942 | (void)omega_run_m12_gates(); omega_get_gate_snapshot(&c, &p); total += c; passed += p; |
| 1943 | (void)omega_run_m13_gates(); omega_get_gate_snapshot(&c, &p); total += c; passed += p; |
| 1944 | (void)omega_run_m14_gates(); omega_get_gate_snapshot(&c, &p); total += c; passed += p; |
| 1945 | (void)omega_run_m15_gates(); omega_get_gate_snapshot(&c, &p); total += c; passed += p; |
| 1946 | (void)run_m17_gates(); omega_get_m17_gate_snapshot(&c, &p); total += c; passed += p; |
| 1947 | (void)run_m18_gates(); omega_get_m18_gate_snapshot(&c, &p); total += c; passed += p; |
| 1948 | |
| 1949 | m19_regression_total = total; |
| 1950 | m19_regression_passed = passed; |
| 1951 | |
| 1952 | return (passed == total); |
| 1953 | } |
| 1954 | |
| 1955 | /* Gate 18 validates receipt prerequisites. The outer harness owns emission |
| 1956 | * after all gate results, including this one, have been collected. */ |
| 1957 | static bool test_m19_gate18_receipt(void) { |
| 1958 | if (m19_gate_count != 17 || m19_gate_passed != 17 || |
| 1959 | m19_regression_total == 0 || m19_regression_passed != m19_regression_total) |
| 1960 | return false; |
| 1961 | char head_sha[41] = {0}; |
| 1962 | if (!m19_git_head(".", head_sha) || |
| 1963 | m19_candidate_commit[0] == '\0' || |
| 1964 | strcmp(head_sha, m19_candidate_commit) != 0) return false; |
| 1965 | const char *record = getenv("OMEGA_QUAL_RECORD"); |
| 1966 | if ((record && strcmp(record, "1") == 0) || getenv("OMEGA_M19R_CANDIDATE")) { |
| 1967 | const char *candidate = getenv("OMEGA_M19R_CANDIDATE"); |
| 1968 | if (!candidate || strcmp(candidate, head_sha) != 0 || |
| 1969 | omega_evidence_tree_dirty()) return false; |
| 1970 | } |
| 1971 | /* The binary under test is wherever make put it: build/step-<step>/omegatool under |
| 1972 | * tests/gates/run.sh (omega#511), build/omegatool in a clean clone or by hand. */ |
| 1973 | char self_path[PATH_MAX]; |
| 1974 | if (!omega_evidence_self_binary(self_path, sizeof(self_path))) return false; |
| 1975 | uint8_t digest[32]; |
| 1976 | char hex[65]; |
| 1977 | return compute_file_sha256(self_path, digest, hex); |
| 1978 | } |
| 1979 | |
| 1980 | int run_m19_gates(void) { |
| 1981 | printf("================================================================================\n"); |
| 1982 | printf(" AIEN OMEGA SUBSTRATE: MILESTONE 19: ACCELERATOR WORLD QUALIFICATION GATES\n"); |
| 1983 | printf("================================================================================\n"); |
| 1984 | m19_gate_count = 0; |
| 1985 | m19_gate_passed = 0; |
| 1986 | |
| 1987 | report_m19_gate("OMEGA_ACCEL_RESIDENT_WORLD_CREATE_PASS", test_m19_gate1_world_create(), "Creation and deterministic initialization of persistent substrate"); |
| 1988 | report_m19_gate("OMEGA_ACCEL_RESIDENT_CONTEXT_REUSE_PASS", test_m19_gate2_context_reuse(), "Reuse of persistent RM client, GPU device, and VAS aperture"); |
| 1989 | report_m19_gate("OMEGA_ACCEL_RESIDENT_CHANNEL_REUSE_PASS", test_m19_gate3_channel_reuse(), "Reuse of GPFIFO channel, USERD, and doorbell mapping across dispatches"); |
| 1990 | report_m19_gate("OMEGA_ACCEL_RESIDENT_CODE_REGISTRY_PASS", test_m19_gate4_code_registry(), "Resident code registry registration, lookup, deduplication, and execution"); |
| 1991 | report_m19_gate("OMEGA_ACCEL_RESIDENT_BUFFER_REGISTRY_PASS", test_m19_gate5_buffer_registry(), "Resident buffer registry allocation, permissions, and bounds checking"); |
| 1992 | report_m19_gate("OMEGA_ACCEL_RESIDENT_MIXED_WORKLOAD_PASS", test_m19_gate6_mixed_workload(), "Alternating execution of mixed workloads with CPU oracle verification"); |
| 1993 | report_m19_gate("OMEGA_ACCEL_RESIDENT_QUEUE_WRAP_PASS", test_m19_gate7_queue_wrap(), "GPFIFO pushbuffer ring wraparound verified under continuous dispatch"); |
| 1994 | report_m19_gate("OMEGA_ACCEL_RESIDENT_1000_OP_PASS", test_m19_gate8_1000_op(), "Sustained continuous execution of >= 1,000 heterogeneous operations on GB10"); |
| 1995 | report_m19_gate("OMEGA_ACCEL_RESIDENT_GENERATION_PASS", test_m19_gate9_generation(), "Monotonic generation counters and two-level ABA handle validation"); |
| 1996 | report_m19_gate("OMEGA_ACCEL_RESIDENT_STALE_HANDLE_REFUSAL_PASS", test_m19_gate10_stale_handle_refusal(), "Fail-closed refusal when attempting dispatch with expired/stale handles"); |
| 1997 | report_m19_gate("OMEGA_ACCEL_RESIDENT_REVOCATION_PASS", test_m19_gate11_revocation(), "Deterministic capability revocation immediately invalidating handles"); |
| 1998 | report_m19_gate("OMEGA_ACCEL_RESIDENT_FAULT_RECOVERY_PASS", test_m19_gate12_fault_recovery(), "Bounded channel fault recovery & channel generation advance"); |
| 1999 | report_m19_gate("OMEGA_ACCEL_RESIDENT_MEMORY_BOUND_PASS", test_m19_gate13_memory_bound(), "Flat bounded resident memory consumption measured across 1,000 operations"); |
| 2000 | report_m19_gate("OMEGA_ACCEL_RESIDENT_STATE_DIGEST_PASS", test_m19_gate14_state_digest(), "Deterministic rolling SHA-256 state digest verifying execution integrity"); |
| 2001 | report_m19_gate("OMEGA_ACCEL_RESIDENT_ZERO_LIBCUDA_PASS", test_m19_gate15_zero_libcuda(), "Zero foreign userspace runtime verification (linkage, symbols, maps)"); |
| 2002 | report_m19_gate("OMEGA_ACCEL_RESIDENT_CLEAN_CLONE_PASS", test_m19_gate16_clean_clone(), "Clean-clone isolated reproduction on DGX Spark silicon from scratch"); |
| 2003 | { |
| 2004 | bool regression_ok = test_m19_gate17_regression(); |
| 2005 | char detail[96]; |
| 2006 | snprintf(detail, sizeof(detail), "Cumulative regression parity: %d / %d prior milestone gates passing", m19_regression_passed, m19_regression_total); |
| 2007 | report_m19_gate("OMEGA_ACCEL_RESIDENT_REGRESSION_PASS", regression_ok, detail); |
| 2008 | } |
| 2009 | report_m19_gate("OMEGA_ACCEL_RESIDENT_RECEIPT_PASS", test_m19_gate18_receipt(), "Receipt prerequisites complete; outer harness emits after all results"); |
| 2010 | char observed_digest[65]; |
| 2011 | for (int i = 0; i < 32; i++) sprintf(&observed_digest[i * 2], "%02x", m19_sustained_digest[i]); |
| 2012 | observed_digest[64] = '\0'; |
| 2013 | printf("M19_OBSERVED_JSON:{\"m19_gates_completed\":%d,\"m19_gates_passed\":%d," |
| 2014 | "\"regression_gates_completed\":%d,\"regression_gates_passed\":%d," |
| 2015 | "\"sustained_dispatches\":%u,\"ring_capacity\":%u," |
| 2016 | "\"wrap_dispatches\":%u,\"rolling_state_digest\":\"%s\"," |
| 2017 | "\"channel_generation_advanced\":%s,\"vas_preserved\":%s}\n", |
| 2018 | m19_gate_count, m19_gate_passed, m19_regression_total, |
| 2019 | m19_regression_passed, m19_sustained_dispatches, m19_ring_capacity, |
| 2020 | m19_wrap_dispatches, observed_digest, |
| 2021 | m19_fault_channel_generation_advanced ? "true" : "false", |
| 2022 | m19_fault_vas_preserved ? "true" : "false"); |
| 2023 | |
| 2024 | printf("================================================================================\n"); |
| 2025 | printf(" STAGE 1 / PERSISTENT WORLD QUALIFICATION: %d / %d M19 GATES PASSED\n", m19_gate_passed, m19_gate_count); |
| 2026 | printf(" TOTAL GATES EVALUATED: %d (%d M19 Gates + %d Prior Regression Gates)\n", m19_gate_passed + m19_regression_total, m19_gate_count, m19_regression_total); |
| 2027 | printf("================================================================================\n"); |
| 2028 | |
| 2029 | return (m19_gate_passed == m19_gate_count) ? 0 : 1; |
| 2030 | } |
| 2031 | |
| 2032 | void run_demonstration_accelerator_world(void) { |
| 2033 | printf("Executing OMEGA Accelerator World physical silicon demonstration...\n"); |
| 2034 | run_m19_gates(); |
| 2035 | } |
| 2036 | |
| 2037 | /* =========================================================================== |
| 2038 | * M19R world lifecycle gates: revoked memory is released, a job still in |
| 2039 | * flight keeps its memory until drain, faults latch without double commits, |
| 2040 | * and completion waits are monotonic ("reached or passed"). |
| 2041 | * =========================================================================== */ |
| 2042 | |
| 2043 | static int lc_gate_count = 0; |
| 2044 | static int lc_gate_passed = 0; |
| 2045 | |
| 2046 | static void report_lc_gate(const char *name, bool ok) { |
| 2047 | lc_gate_count++; |
| 2048 | if (ok) lc_gate_passed++; |
| 2049 | printf(" [%s] %s\n", ok ? "PASS" : "FAIL", name); |
| 2050 | } |
| 2051 | |
| 2052 | /* Code + three 256-byte buffers, as in the drain-decoupling test. */ |
| 2053 | static bool lc_setup(OmegaAcceleratorWorld *world, OmegaHandle *code, |
| 2054 | OmegaHandle *a, OmegaHandle *b, OmegaHandle *c) { |
| 2055 | uint8_t code_buf[1024]; |
| 2056 | size_t code_len = 0; |
| 2057 | if (omega_blackwell_encode_vecadd(code_buf, sizeof(code_buf), &code_len) != 0) return false; |
| 2058 | return omega_world_register_code(world, code_buf, code_len, NULL, code) == OMEGA_WORLD_OK && |
| 2059 | omega_world_register_buffer(world, 256, OMEGA_PERM_READ, a) == OMEGA_WORLD_OK && |
| 2060 | omega_world_register_buffer(world, 256, OMEGA_PERM_READ, b) == OMEGA_WORLD_OK && |
| 2061 | omega_world_register_buffer(world, 256, OMEGA_PERM_READ | OMEGA_PERM_WRITE, c) == OMEGA_WORLD_OK; |
| 2062 | } |
| 2063 | |
| 2064 | /* Queue one release-only pushbuffer (no kernel) carrying `payload`. */ |
| 2065 | static int lc_submit_release(OmegaAcceleratorWorld *world, uint32_t slot, uint32_t payload, |
| 2066 | const OmegaHandle *code, const OmegaHandle *a, |
| 2067 | const OmegaHandle *b, const OmegaHandle *c) { |
| 2068 | uint32_t tpb[32]; |
| 2069 | size_t len = 0; |
| 2070 | memcpy(&tpb[len], WORLD_SETUP_WORDS, sizeof(WORLD_SETUP_WORDS)); |
| 2071 | len += sizeof(WORLD_SETUP_WORDS) / 4; |
| 2072 | tpb[len++] = nvrm_mthd(0, 0x005c, 5); |
| 2073 | tpb[len++] = (uint32_t)world->completion.gpu_va; |
| 2074 | tpb[len++] = (uint32_t)(world->completion.gpu_va >> 32); |
| 2075 | tpb[len++] = payload; |
| 2076 | tpb[len++] = 0; |
| 2077 | tpb[len++] = 0x1 | (1u << 20); |
| 2078 | const uint32_t words[5] = {7, 0, 0, 0, 0}; |
| 2079 | OmegaWorldSubmission sub; |
| 2080 | m19_make_submission(&sub, words, code, a, b, c, 256, 256, 0, payload); |
| 2081 | uint32_t off = slot * 0x1000; |
| 2082 | memcpy((uint8_t *)world->m16.pb_mem.cpu + off, tpb, len * 4); |
| 2083 | __asm__ volatile("dsb sy" ::: "memory"); |
| 2084 | return omega_world_submit(world, &world->m16.pb_mem, off, (uint32_t)len, &sub); |
| 2085 | } |
| 2086 | |
| 2087 | static bool lc_vecadd_ok(OmegaAcceleratorWorld *world, const OmegaHandle *code) { |
| 2088 | OmegaHandle a, b, c; |
| 2089 | if (omega_world_register_buffer(world, 4096, OMEGA_PERM_READ, &a) != OMEGA_WORLD_OK || |
| 2090 | omega_world_register_buffer(world, 4096, OMEGA_PERM_READ, &b) != OMEGA_WORLD_OK || |
| 2091 | omega_world_register_buffer(world, 4096, OMEGA_PERM_READ | OMEGA_PERM_WRITE, &c) != OMEGA_WORLD_OK) { |
| 2092 | return false; |
| 2093 | } |
| 2094 | uint32_t *pa, *pb, *pc; |
| 2095 | uint64_t va; |
| 2096 | omega_world_resolve_buffer(world, &a, OMEGA_PERM_READ, 0, 256, (void **)&pa, &va); |
| 2097 | omega_world_resolve_buffer(world, &b, OMEGA_PERM_READ, 0, 256, (void **)&pb, &va); |
| 2098 | omega_world_resolve_buffer(world, &c, OMEGA_PERM_WRITE, 0, 256, (void **)&pc, &va); |
| 2099 | for (uint32_t i = 0; i < 64; i++) { pa[i] = i * 3u; pb[i] = 7u; pc[i] = 0xdeadbeefu; } |
| 2100 | uint32_t done = 0; |
| 2101 | if (omega_world_dispatch_vector(world, code, &a, &b, &c, 64, &done) != OMEGA_WORLD_OK) return false; |
| 2102 | for (uint32_t i = 0; i < 64; i++) if (pc[i] != i * 3u + 7u) return false; |
| 2103 | return omega_world_revoke_buffer(world, &a) == OMEGA_WORLD_OK && |
| 2104 | omega_world_revoke_buffer(world, &b) == OMEGA_WORLD_OK && |
| 2105 | omega_world_revoke_buffer(world, &c) == OMEGA_WORLD_OK; |
| 2106 | } |
| 2107 | |
| 2108 | /* 5,000 x 64 MiB register/revoke cycles = 312.5 GiB, more than twice the |
| 2109 | * machine's memory: only possible if revoke really returns memory. */ |
| 2110 | static bool test_lc_revoke_releases_memory(void) { |
| 2111 | OmegaAcceleratorWorld world; |
| 2112 | if (omega_world_init(&world) != OMEGA_WORLD_OK) return false; |
| 2113 | uint8_t code_buf[1024]; |
| 2114 | size_t code_len = 0; |
| 2115 | OmegaHandle code; |
| 2116 | if (omega_blackwell_encode_vecadd(code_buf, sizeof(code_buf), &code_len) != 0 || |
| 2117 | omega_world_register_code(&world, code_buf, code_len, NULL, &code) != OMEGA_WORLD_OK) { |
| 2118 | omega_world_destroy(&world); |
| 2119 | return false; |
| 2120 | } |
| 2121 | const size_t sz = 64u << 20; |
| 2122 | bool ok = true; |
| 2123 | for (int i = 0; i < 5000 && ok; i++) { |
| 2124 | OmegaHandle h; |
| 2125 | uint8_t *p = NULL; |
| 2126 | uint64_t va; |
| 2127 | ok = omega_world_register_buffer(&world, sz, OMEGA_PERM_READ | OMEGA_PERM_WRITE, &h) == OMEGA_WORLD_OK && |
| 2128 | omega_world_resolve_buffer(&world, &h, OMEGA_PERM_WRITE, 0, sz, (void **)&p, &va) == OMEGA_WORLD_OK; |
| 2129 | if (ok) { |
| 2130 | p[0] = (uint8_t)i; |
| 2131 | p[sz - 1] = (uint8_t)~i; |
| 2132 | ok = (p[0] == (uint8_t)i && p[sz - 1] == (uint8_t)~i) && |
| 2133 | omega_world_revoke_buffer(&world, &h) == OMEGA_WORLD_OK && |
| 2134 | !world.buffers[h.object_id].retiring && world.buffers[h.object_id].mem.handle == 0; |
| 2135 | } |
| 2136 | if (!ok) printf(" churn failed at cycle %d\n", i); |
| 2137 | } |
| 2138 | ok = ok && world.buffer_count == 0 && !world.faulted && lc_vecadd_ok(&world, &code); |
| 2139 | omega_world_destroy(&world); |
| 2140 | return ok; |
| 2141 | } |
| 2142 | |
| 2143 | /* Revoking a buffer bound by a queued job refuses the handle at once but |
| 2144 | * keeps its memory until drain; the job is abandoned, not committed. */ |
| 2145 | static bool test_lc_revoke_waits_for_queued_job(void) { |
| 2146 | OmegaAcceleratorWorld world; |
| 2147 | if (omega_world_init(&world) != OMEGA_WORLD_OK) return false; |
| 2148 | OmegaHandle code, a, b, c; |
| 2149 | bool ok = lc_setup(&world, &code, &a, &b, &c) && |
| 2150 | lc_submit_release(&world, 0, 1, &code, &a, &b, &c) == OMEGA_WORLD_OK && |
| 2151 | omega_world_revoke_buffer(&world, &c) == OMEGA_WORLD_OK; |
| 2152 | ok = ok && world.buffers[c.object_id].retiring && world.buffers[c.object_id].mem.handle != 0; |
| 2153 | void *p = NULL; |
| 2154 | uint64_t va; |
| 2155 | ok = ok && omega_world_resolve_buffer(&world, &c, OMEGA_PERM_READ, 0, 256, &p, &va) == OMEGA_WORLD_ERR_NOT_FOUND; |
| 2156 | OmegaHandle other; |
| 2157 | ok = ok && omega_world_register_buffer(&world, 256, OMEGA_PERM_READ, &other) == OMEGA_WORLD_OK && |
| 2158 | other.object_id != c.object_id; |
| 2159 | ok = ok && omega_world_ring(&world) == OMEGA_WORLD_OK && |
| 2160 | omega_world_drain(&world, 1, 5000) == 0 && |
| 2161 | world.abandoned_dispatches == 1 && world.total_dispatches == 0 && |
| 2162 | !world.buffers[c.object_id].retiring && world.buffers[c.object_id].mem.handle == 0 && |
| 2163 | !world.faulted; |
| 2164 | /* The released slot is reusable. */ |
| 2165 | OmegaHandle reuse; |
| 2166 | ok = ok && omega_world_revoke_buffer(&world, &other) == OMEGA_WORLD_OK; |
| 2167 | for (int i = 0; ok && i < OMEGA_WORLD_MAX_BUFFERS; i++) { |
| 2168 | if (omega_world_register_buffer(&world, 256, OMEGA_PERM_READ, &reuse) != OMEGA_WORLD_OK) { ok = false; break; } |
| 2169 | if (reuse.object_id == c.object_id) break; |
| 2170 | } |
| 2171 | ok = ok && reuse.object_id == c.object_id && reuse.object_generation == c.object_generation + 1; |
| 2172 | omega_world_destroy(&world); |
| 2173 | return ok; |
| 2174 | } |
| 2175 | |
| 2176 | /* CHIPWAIT-1c diagnostics: one tagged stderr line per silent failure exit. |
| 2177 | * Reads already-computed values only. */ |
| 2178 | #define LC_DIAG(gate, tag, rc, fmt, ...) \ |
| 2179 | fprintf(stderr, "GATE_DIAG gate=%s exit=%s rc=%d " fmt "\n", gate, tag, (int)(rc), __VA_ARGS__) |
| 2180 | |
| 2181 | /* A drain failure latches the world; nothing already committed is committed |
| 2182 | * again, and recovery clears the latch. */ |
| 2183 | static bool test_lc_error_latch_counts_once(void) { |
| 2184 | OmegaAcceleratorWorld world; |
| 2185 | int rc = omega_world_init(&world); |
| 2186 | if (rc != OMEGA_WORLD_OK) { |
| 2187 | LC_DIAG("WORLD_ERROR_LATCH_COUNTS_ONCE", "init_failed", rc, "x=%d", 0); |
| 2188 | return false; |
| 2189 | } |
| 2190 | OmegaHandle code, a, b, c; |
| 2191 | bool ok = lc_setup(&world, &code, &a, &b, &c); |
| 2192 | if (!ok) { LC_DIAG("WORLD_ERROR_LATCH_COUNTS_ONCE", "setup_failed", -1, "world_faulted=%d total_dispatches=%llu", (int)world.faulted, (unsigned long long)world.total_dispatches); } |
| 2193 | for (uint32_t i = 0; ok && i < 3; i++) { |
| 2194 | rc = lc_submit_release(&world, i, i + 1, &code, &a, &b, &c); |
| 2195 | ok = rc == OMEGA_WORLD_OK; |
| 2196 | if (!ok) { LC_DIAG("WORLD_ERROR_LATCH_COUNTS_ONCE", "submit_initial_failed", rc, "i=%u world_faulted=%d total_dispatches=%llu", (unsigned)i, (int)world.faulted, (unsigned long long)world.total_dispatches); } |
| 2197 | } |
| 2198 | if (ok) { |
| 2199 | rc = omega_world_ring(&world); |
| 2200 | ok = rc == OMEGA_WORLD_OK; |
| 2201 | if (!ok) { LC_DIAG("WORLD_ERROR_LATCH_COUNTS_ONCE", "ring_initial_failed", rc, "world_faulted=%d total_dispatches=%llu", (int)world.faulted, (unsigned long long)world.total_dispatches); } |
| 2202 | } |
| 2203 | if (ok) { |
| 2204 | rc = omega_world_drain(&world, 3, 5000); |
| 2205 | ok = rc == 3; |
| 2206 | if (!ok) { LC_DIAG("WORLD_ERROR_LATCH_COUNTS_ONCE", "drain_initial_failed", rc, "world_faulted=%d total_dispatches=%llu (want drain 3)", (int)world.faulted, (unsigned long long)world.total_dispatches); } |
| 2207 | } |
| 2208 | if (ok) { |
| 2209 | ok = world.total_dispatches == 3; |
| 2210 | if (!ok) { LC_DIAG("WORLD_ERROR_LATCH_COUNTS_ONCE", "total_dispatches_after_initial", -1, "world_faulted=%d total_dispatches=%llu (want 3)", (int)world.faulted, (unsigned long long)world.total_dispatches); } |
| 2211 | } |
| 2212 | uint64_t seq_before = world.sequence_number; |
| 2213 | /* Payload 4 completes, but the drain waits for payload 100: timeout. */ |
| 2214 | if (ok) { |
| 2215 | rc = lc_submit_release(&world, 3, 4, &code, &a, &b, &c); |
| 2216 | ok = rc == OMEGA_WORLD_OK; |
| 2217 | if (!ok) { LC_DIAG("WORLD_ERROR_LATCH_COUNTS_ONCE", "submit_timeout_job_failed", rc, "world_faulted=%d total_dispatches=%llu", (int)world.faulted, (unsigned long long)world.total_dispatches); } |
| 2218 | } |
| 2219 | if (ok) { |
| 2220 | rc = omega_world_ring(&world); |
| 2221 | ok = rc == OMEGA_WORLD_OK; |
| 2222 | if (!ok) { LC_DIAG("WORLD_ERROR_LATCH_COUNTS_ONCE", "ring_timeout_job_failed", rc, "world_faulted=%d total_dispatches=%llu", (int)world.faulted, (unsigned long long)world.total_dispatches); } |
| 2223 | } |
| 2224 | if (ok) { |
| 2225 | rc = omega_world_drain(&world, 100, 200); |
| 2226 | ok = rc == OMEGA_WORLD_ERR_HARDWARE; |
| 2227 | if (!ok) { LC_DIAG("WORLD_ERROR_LATCH_COUNTS_ONCE", "drain_timeout_not_hardware_err", rc, "world_faulted=%d total_dispatches=%llu (want ERR_HARDWARE=%d)", (int)world.faulted, (unsigned long long)world.total_dispatches, (int)OMEGA_WORLD_ERR_HARDWARE); } |
| 2228 | } |
| 2229 | if (ok) { |
| 2230 | ok = world.faulted; |
| 2231 | if (!ok) { LC_DIAG("WORLD_ERROR_LATCH_COUNTS_ONCE", "not_faulted_after_timeout", -1, "world_faulted=%d total_dispatches=%llu", (int)world.faulted, (unsigned long long)world.total_dispatches); } |
| 2232 | } |
| 2233 | if (ok) { |
| 2234 | rc = lc_submit_release(&world, 4, 5, &code, &a, &b, &c); |
| 2235 | ok = rc == OMEGA_WORLD_ERR_FAULTED; |
| 2236 | if (!ok) { LC_DIAG("WORLD_ERROR_LATCH_COUNTS_ONCE", "submit_while_faulted_not_refused", rc, "world_faulted=%d total_dispatches=%llu (want ERR_FAULTED=%d)", (int)world.faulted, (unsigned long long)world.total_dispatches, (int)OMEGA_WORLD_ERR_FAULTED); } |
| 2237 | } |
| 2238 | if (ok) { |
| 2239 | rc = omega_world_drain(&world, 4, 100); |
| 2240 | ok = rc == OMEGA_WORLD_ERR_FAULTED; |
| 2241 | if (!ok) { LC_DIAG("WORLD_ERROR_LATCH_COUNTS_ONCE", "drain_while_faulted_not_refused", rc, "world_faulted=%d total_dispatches=%llu (want ERR_FAULTED=%d)", (int)world.faulted, (unsigned long long)world.total_dispatches, (int)OMEGA_WORLD_ERR_FAULTED); } |
| 2242 | } |
| 2243 | if (ok) { |
| 2244 | rc = omega_world_ring(&world); |
| 2245 | ok = rc == OMEGA_WORLD_ERR_FAULTED; |
| 2246 | if (!ok) { LC_DIAG("WORLD_ERROR_LATCH_COUNTS_ONCE", "ring_while_faulted_not_refused", rc, "world_faulted=%d total_dispatches=%llu (want ERR_FAULTED=%d)", (int)world.faulted, (unsigned long long)world.total_dispatches, (int)OMEGA_WORLD_ERR_FAULTED); } |
| 2247 | } |
| 2248 | if (ok) { |
| 2249 | ok = world.total_dispatches == 3 && world.sequence_number == seq_before; |
| 2250 | if (!ok) { LC_DIAG("WORLD_ERROR_LATCH_COUNTS_ONCE", "counts_moved_while_faulted", -1, "world_faulted=%d total_dispatches=%llu (want 3) seq=%llu seq_before=%llu", (int)world.faulted, (unsigned long long)world.total_dispatches, (unsigned long long)world.sequence_number, (unsigned long long)seq_before); } |
| 2251 | } |
| 2252 | if (ok) { |
| 2253 | rc = omega_world_recover_channel_fault(&world); |
| 2254 | ok = rc == OMEGA_WORLD_OK; |
| 2255 | if (!ok) { LC_DIAG("WORLD_ERROR_LATCH_COUNTS_ONCE", "recover_failed", rc, "world_faulted=%d total_dispatches=%llu", (int)world.faulted, (unsigned long long)world.total_dispatches); } |
| 2256 | } |
| 2257 | if (ok) { |
| 2258 | ok = !world.faulted && world.in_flight_count == 0 && world.abandoned_dispatches == 1; |
| 2259 | if (!ok) { LC_DIAG("WORLD_ERROR_LATCH_COUNTS_ONCE", "post_recover_state_bad", -1, "world_faulted=%d total_dispatches=%llu in_flight=%llu(want 0) abandoned=%llu(want 1)", (int)world.faulted, (unsigned long long)world.total_dispatches, (unsigned long long)world.in_flight_count, (unsigned long long)world.abandoned_dispatches); } |
| 2260 | } |
| 2261 | if (ok) { |
| 2262 | rc = omega_world_drain(&world, 4, 100); |
| 2263 | ok = rc == 0; |
| 2264 | if (!ok) { LC_DIAG("WORLD_ERROR_LATCH_COUNTS_ONCE", "drain_after_recover_nonzero", rc, "world_faulted=%d total_dispatches=%llu (want drain 0)", (int)world.faulted, (unsigned long long)world.total_dispatches); } |
| 2265 | } |
| 2266 | if (ok) { |
| 2267 | ok = world.total_dispatches == 3; |
| 2268 | if (!ok) { LC_DIAG("WORLD_ERROR_LATCH_COUNTS_ONCE", "total_dispatches_after_recover", -1, "world_faulted=%d total_dispatches=%llu (want 3)", (int)world.faulted, (unsigned long long)world.total_dispatches); } |
| 2269 | } |
| 2270 | if (ok) { |
| 2271 | ok = lc_vecadd_ok(&world, &code); |
| 2272 | if (!ok) { LC_DIAG("WORLD_ERROR_LATCH_COUNTS_ONCE", "vecadd_after_recover_failed", -1, "world_faulted=%d total_dispatches=%llu", (int)world.faulted, (unsigned long long)world.total_dispatches); } |
| 2273 | } |
| 2274 | if (ok) { |
| 2275 | ok = world.total_dispatches == 4; |
| 2276 | if (!ok) { LC_DIAG("WORLD_ERROR_LATCH_COUNTS_ONCE", "total_dispatches_final", -1, "world_faulted=%d total_dispatches=%llu (want 4)", (int)world.faulted, (unsigned long long)world.total_dispatches); } |
| 2277 | } |
| 2278 | omega_world_destroy(&world); |
| 2279 | return ok; |
| 2280 | } |
| 2281 | |
| 2282 | /* Draining an older payload succeeds after newer ones finished, payloads |
| 2283 | * must strictly increase, and the marker never moves backwards. */ |
| 2284 | static bool test_lc_wait_reached_or_passed(void) { |
| 2285 | OmegaAcceleratorWorld world; |
| 2286 | if (omega_world_init(&world) != OMEGA_WORLD_OK) return false; |
| 2287 | OmegaHandle code, a, b, c; |
| 2288 | bool ok = lc_setup(&world, &code, &a, &b, &c); |
| 2289 | for (uint32_t i = 0; ok && i < 3; i++) ok = lc_submit_release(&world, i, i + 1, &code, &a, &b, &c) == OMEGA_WORLD_OK; |
| 2290 | ok = ok && omega_world_ring(&world) == OMEGA_WORLD_OK && |
| 2291 | m16_native_wait_marker_ge(world.completion.cpu_marker, 3, 5000) == 0; |
| 2292 | /* Marker is already 3: an equality wait on 1 would time out. */ |
| 2293 | ok = ok && omega_world_drain(&world, 1, 1000) == 1 && omega_world_drain(&world, 3, 1000) == 2 && |
| 2294 | world.total_dispatches == 3; |
| 2295 | /* A payload that does not advance the sequence is refused. */ |
| 2296 | ok = ok && lc_submit_release(&world, 3, 2, &code, &a, &b, &c) == OMEGA_WORLD_ERR_INVALID_ARG && |
| 2297 | world.in_flight_count == 0; |
| 2298 | uint32_t m0 = *world.completion.cpu_marker; |
| 2299 | ok = ok && lc_vecadd_ok(&world, &code); |
| 2300 | uint32_t m1 = *world.completion.cpu_marker; |
| 2301 | ok = ok && (int32_t)(m1 - m0) > 0 && m1 == world.completion.last_payload; |
| 2302 | omega_world_destroy(&world); |
| 2303 | return ok; |
| 2304 | } |
| 2305 | |
| 2306 | static bool test_lc_output_alias_refusal(void) { |
| 2307 | OmegaAcceleratorWorld world; |
| 2308 | if (omega_world_init(&world) != OMEGA_WORLD_OK) return false; |
| 2309 | OmegaHandle code, a, b, c, d; |
| 2310 | bool ok = lc_setup(&world, &code, &a, &b, &c) && |
| 2311 | omega_world_register_buffer(&world, 256, OMEGA_PERM_READ | OMEGA_PERM_WRITE, &d) == OMEGA_WORLD_OK; |
| 2312 | uint32_t words[32], len = 0; |
| 2313 | memcpy(words, WORLD_SETUP_WORDS, sizeof WORLD_SETUP_WORDS); |
| 2314 | len += (uint32_t)(sizeof WORLD_SETUP_WORDS / sizeof(uint32_t)); |
| 2315 | words[len++] = nvrm_mthd(0, 0x005c, 5); |
| 2316 | words[len++] = (uint32_t)world.completion.gpu_va; |
| 2317 | words[len++] = (uint32_t)(world.completion.gpu_va >> 32); |
| 2318 | words[len++] = 1; |
| 2319 | words[len++] = 0; |
| 2320 | words[len++] = 0x1 | (1u << 20); |
| 2321 | memcpy(world.m16.pb_mem.cpu, words, len * sizeof(uint32_t)); |
| 2322 | OmegaWorldSubmission sub; |
| 2323 | const uint32_t semantic[5] = {7, 0, 0, 0, 0}; |
| 2324 | m19_make_submission(&sub, semantic, &code, &a, &b, &c, 256, 256, 256, 1); |
| 2325 | ok = ok && omega_world_submit(&world, &world.m16.pb_mem, 0, len, &sub) == OMEGA_WORLD_OK; |
| 2326 | void *cpu = NULL; |
| 2327 | ok = ok && omega_world_resolve_buffer(&world, &c, OMEGA_PERM_WRITE, |
| 2328 | 0, 1, &cpu, NULL) == OMEGA_WORLD_ERR_FAULT; |
| 2329 | sub.completion_val = 2; |
| 2330 | ok = ok && omega_world_submit(&world, &world.m16.pb_mem, 0, len, &sub) == OMEGA_WORLD_ERR_FAULT; |
| 2331 | sub.c_object_id = d.object_id; |
| 2332 | sub.c_generation = d.object_generation; |
| 2333 | sub.c_permissions = d.permissions; |
| 2334 | sub.a_object_id = c.object_id; |
| 2335 | sub.a_generation = c.object_generation; |
| 2336 | sub.a_permissions = c.permissions; |
| 2337 | ok = ok && omega_world_submit(&world, &world.m16.pb_mem, 0, len, &sub) == OMEGA_WORLD_ERR_FAULT; |
| 2338 | ok = ok && omega_world_ring(&world) == OMEGA_WORLD_OK && |
| 2339 | omega_world_drain(&world, 1, 5000) == 1 && world.total_dispatches == 1 && |
| 2340 | omega_world_drain(&world, 1, 0) == 0 && world.total_dispatches == 1; |
| 2341 | ok = ok && omega_world_resolve_buffer(&world, &c, OMEGA_PERM_WRITE, |
| 2342 | 0, 1, &cpu, NULL) == OMEGA_WORLD_OK; |
| 2343 | omega_world_destroy(&world); |
| 2344 | return ok; |
| 2345 | } |
| 2346 | |
| 2347 | static bool test_lc_bounds_overflow(void) { |
| 2348 | OmegaAcceleratorWorld world; |
| 2349 | if (omega_world_init(&world) != OMEGA_WORLD_OK) return false; |
| 2350 | OmegaHandle h; |
| 2351 | bool ok = omega_world_register_buffer(&world, 256, OMEGA_PERM_READ, &h) == OMEGA_WORLD_OK && |
| 2352 | omega_world_resolve_buffer(&world, &h, OMEGA_PERM_READ, |
| 2353 | SIZE_MAX - 4, 16, NULL, NULL) == OMEGA_WORLD_ERR_BOUNDS && |
| 2354 | omega_world_register_buffer(&world, SIZE_MAX, OMEGA_PERM_READ, &h) == OMEGA_WORLD_ERR_BOUNDS && |
| 2355 | omega_world_revoke_buffer(&world, &h) == OMEGA_WORLD_OK; |
| 2356 | omega_world_destroy(&world); |
| 2357 | return ok; |
| 2358 | } |
| 2359 | |
| 2360 | static bool test_lc_timeout_completion_race(void) { |
| 2361 | OmegaAcceleratorWorld world; |
| 2362 | if (omega_world_init(&world) != OMEGA_WORLD_OK) return false; |
| 2363 | OmegaHandle code, a, b, c; |
| 2364 | bool ok = lc_setup(&world, &code, &a, &b, &c) && |
| 2365 | lc_submit_release(&world, 0, 1, &code, &a, &b, &c) == OMEGA_WORLD_OK && |
| 2366 | omega_world_ring(&world) == OMEGA_WORLD_OK; |
| 2367 | int drained = ok ? omega_world_drain(&world, 1, 0) : OMEGA_WORLD_ERR_FAULT; |
| 2368 | if (drained == 1) { |
| 2369 | ok = ok && world.total_dispatches == 1 && |
| 2370 | omega_world_drain(&world, 1, 0) == 0 && world.total_dispatches == 1; |
| 2371 | } else { |
| 2372 | ok = ok && drained == OMEGA_WORLD_ERR_HARDWARE && world.faulted && |
| 2373 | world.total_dispatches == 0 && |
| 2374 | m16_native_wait_marker_ge(world.completion.cpu_marker, 1, 5000) == 0 && |
| 2375 | omega_world_recover_channel_fault(&world) == OMEGA_WORLD_OK && |
| 2376 | world.abandoned_dispatches == 1 && world.total_dispatches == 0; |
| 2377 | } |
| 2378 | ok = ok && lc_vecadd_ok(&world, &code); |
| 2379 | omega_world_destroy(&world); |
| 2380 | return ok; |
| 2381 | } |
| 2382 | |
| 2383 | static bool test_lc_stale_generation_marker(void) { |
| 2384 | OmegaAcceleratorWorld world; |
| 2385 | if (omega_world_init(&world) != OMEGA_WORLD_OK) return false; |
| 2386 | OmegaHandle code, a, b, c; |
| 2387 | bool ok = lc_setup(&world, &code, &a, &b, &c) && |
| 2388 | lc_submit_release(&world, 0, 1, &code, &a, &b, &c) == OMEGA_WORLD_OK && |
| 2389 | omega_world_ring(&world) == OMEGA_WORLD_OK && |
| 2390 | omega_world_drain(&world, 1, 5000) == 1 && |
| 2391 | omega_world_recover_channel_fault(&world) == OMEGA_WORLD_OK && |
| 2392 | *world.completion.cpu_marker == 1; |
| 2393 | /* The old marker value must not commit a new generation's queued job. */ |
| 2394 | ok = ok && lc_submit_release(&world, 0, 2, &code, &a, &b, &c) == OMEGA_WORLD_OK && |
| 2395 | omega_world_drain(&world, 2, 0) == OMEGA_WORLD_ERR_HARDWARE && |
| 2396 | world.total_dispatches == 1 && world.faulted && |
| 2397 | omega_world_recover_channel_fault(&world) == OMEGA_WORLD_OK && |
| 2398 | world.abandoned_dispatches == 1 && lc_vecadd_ok(&world, &code); |
| 2399 | omega_world_destroy(&world); |
| 2400 | return ok; |
| 2401 | } |
| 2402 | |
| 2403 | static bool lc_provenance_run(uint8_t mutation, uint8_t digest[32]) { |
| 2404 | OmegaAcceleratorWorld world; |
| 2405 | int rc = omega_world_init(&world); |
| 2406 | if (rc != OMEGA_WORLD_OK) { |
| 2407 | LC_DIAG("WORLD_BATCHED_RESULT_IDENTITY", "init_failed", rc, "mutation=0x%02x x=%d", (unsigned)mutation, 0); |
| 2408 | return false; |
| 2409 | } |
| 2410 | OmegaHandle code, a, b, c1, c2; |
| 2411 | bool ok = lc_setup(&world, &code, &a, &b, &c1); |
| 2412 | if (!ok) { LC_DIAG("WORLD_BATCHED_RESULT_IDENTITY", "setup_failed", -1, "mutation=0x%02x faulted=%d total_dispatches=%llu", (unsigned)mutation, (int)world.faulted, (unsigned long long)world.total_dispatches); } |
| 2413 | if (ok) { |
| 2414 | rc = omega_world_register_buffer(&world, 256, |
| 2415 | OMEGA_PERM_READ | OMEGA_PERM_WRITE, &c2); |
| 2416 | ok = rc == OMEGA_WORLD_OK; |
| 2417 | if (!ok) { LC_DIAG("WORLD_BATCHED_RESULT_IDENTITY", "register_c2_failed", rc, "mutation=0x%02x faulted=%d total_dispatches=%llu", (unsigned)mutation, (int)world.faulted, (unsigned long long)world.total_dispatches); } |
| 2418 | } |
| 2419 | void *p1 = NULL, *p2 = NULL; |
| 2420 | if (ok) { |
| 2421 | rc = omega_world_resolve_buffer(&world, &c1, OMEGA_PERM_WRITE, |
| 2422 | 0, 256, &p1, NULL); |
| 2423 | ok = rc == OMEGA_WORLD_OK; |
| 2424 | if (!ok) { LC_DIAG("WORLD_BATCHED_RESULT_IDENTITY", "resolve_c1_failed", rc, "mutation=0x%02x faulted=%d total_dispatches=%llu", (unsigned)mutation, (int)world.faulted, (unsigned long long)world.total_dispatches); } |
| 2425 | } |
| 2426 | if (ok) { |
| 2427 | rc = omega_world_resolve_buffer(&world, &c2, OMEGA_PERM_WRITE, |
| 2428 | 0, 256, &p2, NULL); |
| 2429 | ok = rc == OMEGA_WORLD_OK; |
| 2430 | if (!ok) { LC_DIAG("WORLD_BATCHED_RESULT_IDENTITY", "resolve_c2_failed", rc, "mutation=0x%02x faulted=%d total_dispatches=%llu", (unsigned)mutation, (int)world.faulted, (unsigned long long)world.total_dispatches); } |
| 2431 | } |
| 2432 | if (ok) { memset(p1, 0x11, 256); memset(p2, 0x22, 256); } |
| 2433 | const uint32_t semantic[5] = {7, 0, 0, 0, 0}; |
| 2434 | for (uint32_t i = 0; ok && i < 2; i++) { |
| 2435 | uint32_t words[32], len = 0; |
| 2436 | memcpy(words, WORLD_SETUP_WORDS, sizeof WORLD_SETUP_WORDS); |
| 2437 | len += (uint32_t)(sizeof WORLD_SETUP_WORDS / sizeof(uint32_t)); |
| 2438 | words[len++] = nvrm_mthd(0, 0x005c, 5); |
| 2439 | words[len++] = (uint32_t)world.completion.gpu_va; |
| 2440 | words[len++] = (uint32_t)(world.completion.gpu_va >> 32); |
| 2441 | words[len++] = i + 1; |
| 2442 | words[len++] = 0; |
| 2443 | words[len++] = 0x1 | (1u << 20); |
| 2444 | uint32_t off = i * 0x1000; |
| 2445 | memcpy((uint8_t *)world.m16.pb_mem.cpu + off, words, len * sizeof(uint32_t)); |
| 2446 | OmegaWorldSubmission sub; |
| 2447 | m19_make_submission(&sub, semantic, &code, &a, &b, i ? &c2 : &c1, |
| 2448 | 256, 256, 256, i + 1); |
| 2449 | rc = omega_world_submit(&world, &world.m16.pb_mem, off, len, &sub); |
| 2450 | ok = rc == OMEGA_WORLD_OK; |
| 2451 | if (!ok) { LC_DIAG("WORLD_BATCHED_RESULT_IDENTITY", "submit_failed", rc, "mutation=0x%02x i=%u faulted=%d total_dispatches=%llu", (unsigned)mutation, (unsigned)i, (int)world.faulted, (unsigned long long)world.total_dispatches); } |
| 2452 | } |
| 2453 | if (ok) { |
| 2454 | rc = omega_world_ring(&world); |
| 2455 | ok = rc == OMEGA_WORLD_OK; |
| 2456 | if (!ok) { LC_DIAG("WORLD_BATCHED_RESULT_IDENTITY", "ring_failed", rc, "mutation=0x%02x faulted=%d total_dispatches=%llu", (unsigned)mutation, (int)world.faulted, (unsigned long long)world.total_dispatches); } |
| 2457 | } |
| 2458 | if (ok) { |
| 2459 | rc = omega_world_drain(&world, 1, 5000); |
| 2460 | ok = rc == 1; |
| 2461 | if (!ok) { LC_DIAG("WORLD_BATCHED_RESULT_IDENTITY", "drain1_failed", rc, "mutation=0x%02x faulted=%d total_dispatches=%llu (want drain 1)", (unsigned)mutation, (int)world.faulted, (unsigned long long)world.total_dispatches); } |
| 2462 | } |
| 2463 | if (ok) memset(p1, mutation, 256); |
| 2464 | if (ok) { |
| 2465 | rc = omega_world_drain(&world, 2, 5000); |
| 2466 | ok = rc == 1; |
| 2467 | if (!ok) { LC_DIAG("WORLD_BATCHED_RESULT_IDENTITY", "drain2_failed", rc, "mutation=0x%02x faulted=%d total_dispatches=%llu (want drain 1)", (unsigned)mutation, (int)world.faulted, (unsigned long long)world.total_dispatches); } |
| 2468 | } |
| 2469 | if (ok) { |
| 2470 | ok = world.total_dispatches == 2; |
| 2471 | if (!ok) { LC_DIAG("WORLD_BATCHED_RESULT_IDENTITY", "total_dispatches_not_2", -1, "mutation=0x%02x faulted=%d total_dispatches=%llu", (unsigned)mutation, (int)world.faulted, (unsigned long long)world.total_dispatches); } |
| 2472 | } |
| 2473 | if (ok) memcpy(digest, world.rolling_state_digest, 32); |
| 2474 | omega_world_destroy(&world); |
| 2475 | return ok; |
| 2476 | } |
| 2477 | |
| 2478 | static bool test_lc_batched_result_identity(void) { |
| 2479 | uint8_t a[32], b[32]; |
| 2480 | if (!lc_provenance_run(0x33, a)) { |
| 2481 | LC_DIAG("WORLD_BATCHED_RESULT_IDENTITY", "run1_failed", -1, "mutation=0x%02x x=%d", 0x33, 0); |
| 2482 | return false; |
| 2483 | } |
| 2484 | if (!lc_provenance_run(0x44, b)) { |
| 2485 | LC_DIAG("WORLD_BATCHED_RESULT_IDENTITY", "run2_failed", -1, "mutation=0x%02x x=%d", 0x44, 0); |
| 2486 | return false; |
| 2487 | } |
| 2488 | if (memcmp(a, b, sizeof a) != 0) { |
| 2489 | LC_DIAG("WORLD_BATCHED_RESULT_IDENTITY", "digest_mismatch", -1, "digest_a=%02x%02x%02x%02x.. digest_b=%02x%02x%02x%02x..", a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]); |
| 2490 | return false; |
| 2491 | } |
| 2492 | return true; |
| 2493 | } |
| 2494 | |
| 2495 | static bool test_lc_stale_handle_after_world_restart(void) { |
| 2496 | OmegaAcceleratorWorld world; |
| 2497 | OmegaHandle old, fresh; |
| 2498 | if (omega_world_init(&world) != OMEGA_WORLD_OK) return false; |
| 2499 | bool ok = omega_world_register_buffer(&world, 4096, OMEGA_PERM_READ, &old) == OMEGA_WORLD_OK; |
| 2500 | if (omega_world_destroy(&world) != OMEGA_WORLD_OK) return false; |
| 2501 | if (omega_world_init(&world) != OMEGA_WORLD_OK) return false; |
| 2502 | ok = ok && omega_world_register_buffer(&world, 4096, OMEGA_PERM_READ, &fresh) == OMEGA_WORLD_OK; |
| 2503 | ok = ok && old.object_id == fresh.object_id && old.object_generation == fresh.object_generation && |
| 2504 | old.world_epoch != fresh.world_epoch && |
| 2505 | omega_world_validate_handle(&world, &old, OMEGA_OBJ_BUFFER, OMEGA_PERM_READ) == OMEGA_WORLD_ERR_STALE_EPOCH; |
| 2506 | return omega_world_destroy(&world) == OMEGA_WORLD_OK && ok; |
| 2507 | } |
| 2508 | |
| 2509 | /* A faulted world refuses new registrations, like submit/drain/dispatch: |
| 2510 | * no slot, count, or output handle changes until recovery clears the latch. */ |
| 2511 | static OmegaBufferEntry lc_fault_buffers_before[OMEGA_WORLD_MAX_BUFFERS]; |
| 2512 | static OmegaCodeEntry lc_fault_code_before[OMEGA_WORLD_MAX_CODE_ENTRIES]; |
| 2513 | |
| 2514 | static bool test_lc_faulted_world_refuses_register(void) { |
| 2515 | OmegaAcceleratorWorld world; |
| 2516 | if (omega_world_init(&world) != OMEGA_WORLD_OK) return false; |
| 2517 | OmegaHandle code, a, b, c; |
| 2518 | /* Payload 1 completes, but the drain waits for payload 100: timeout latches the fault. */ |
| 2519 | bool ok = lc_setup(&world, &code, &a, &b, &c) && |
| 2520 | lc_submit_release(&world, 0, 1, &code, &a, &b, &c) == OMEGA_WORLD_OK && |
| 2521 | omega_world_ring(&world) == OMEGA_WORLD_OK && |
| 2522 | omega_world_drain(&world, 100, 200) == OMEGA_WORLD_ERR_HARDWARE && world.faulted; |
| 2523 | uint32_t buffers_before = world.buffer_count; |
| 2524 | uint32_t code_before = world.code_count; |
| 2525 | memcpy(lc_fault_buffers_before, world.buffers, sizeof world.buffers); |
| 2526 | memcpy(lc_fault_code_before, world.code_entries, sizeof world.code_entries); |
| 2527 | uint8_t code_buf[1024]; |
| 2528 | size_t code_len = 0; |
| 2529 | ok = ok && omega_blackwell_encode_vecadd(code_buf, sizeof(code_buf), &code_len) == 0; |
| 2530 | OmegaHandle hb, hc, sentinel; |
| 2531 | memset(&sentinel, 0xa5, sizeof sentinel); |
| 2532 | hb = sentinel; |
| 2533 | hc = sentinel; |
| 2534 | ok = ok && omega_world_register_buffer(&world, 256, OMEGA_PERM_READ, &hb) == OMEGA_WORLD_ERR_FAULTED && |
| 2535 | omega_world_register_code(&world, code_buf, code_len, NULL, &hc) == OMEGA_WORLD_ERR_FAULTED; |
| 2536 | ok = ok && world.faulted && world.buffer_count == buffers_before && world.code_count == code_before && |
| 2537 | memcmp(lc_fault_buffers_before, world.buffers, sizeof world.buffers) == 0 && |
| 2538 | memcmp(lc_fault_code_before, world.code_entries, sizeof world.code_entries) == 0 && |
| 2539 | memcmp(&hb, &sentinel, sizeof sentinel) == 0 && memcmp(&hc, &sentinel, sizeof sentinel) == 0; |
| 2540 | /* Recovery clears the latch and registration works again. */ |
| 2541 | ok = ok && omega_world_recover_channel_fault(&world) == OMEGA_WORLD_OK && !world.faulted && |
| 2542 | omega_world_register_buffer(&world, 256, OMEGA_PERM_READ, &hb) == OMEGA_WORLD_OK && |
| 2543 | omega_world_register_code(&world, code_buf, code_len, NULL, &hc) == OMEGA_WORLD_OK && |
| 2544 | world.buffer_count == buffers_before + 1 && world.code_count == code_before + 1; |
| 2545 | omega_world_destroy(&world); |
| 2546 | return ok; |
| 2547 | } |
| 2548 | |
| 2549 | /* STALEGEN-RECOVER: a completion wait on a healthy channel must return in |
| 2550 | * milliseconds with the output already visible. With a GPU-cached marker page |
| 2551 | * on GB10 each wait took about 1039 ms (and 2.8% timed out at 5 s). Arm 1: |
| 2552 | * the call (build, submit, completion wait, commit) takes under 50 ms. |
| 2553 | * Arm 2: every output element is correct the instant the call returns. |
| 2554 | * Negative control: the output held the poison value before dispatch. */ |
| 2555 | static bool test_lc_completion_wait_bounded(void) { |
| 2556 | OmegaAcceleratorWorld world; |
| 2557 | if (omega_world_init(&world) != OMEGA_WORLD_OK) return false; |
| 2558 | uint8_t code_buf[1024]; |
| 2559 | size_t code_len = 0; |
| 2560 | OmegaHandle code, a, b, c; |
| 2561 | bool ok = omega_blackwell_encode_vecadd(code_buf, sizeof(code_buf), &code_len) == 0 && |
| 2562 | omega_world_register_code(&world, code_buf, code_len, NULL, &code) == OMEGA_WORLD_OK && |
| 2563 | omega_world_register_buffer(&world, 4096, OMEGA_PERM_READ, &a) == OMEGA_WORLD_OK && |
| 2564 | omega_world_register_buffer(&world, 4096, OMEGA_PERM_READ, &b) == OMEGA_WORLD_OK && |
| 2565 | omega_world_register_buffer(&world, 4096, OMEGA_PERM_READ | OMEGA_PERM_WRITE, &c) == OMEGA_WORLD_OK; |
| 2566 | uint32_t *pa = NULL, *pb = NULL, *pc = NULL; |
| 2567 | uint64_t va; |
| 2568 | ok = ok && omega_world_resolve_buffer(&world, &a, OMEGA_PERM_READ, 0, 256, (void **)&pa, &va) == OMEGA_WORLD_OK && |
| 2569 | omega_world_resolve_buffer(&world, &b, OMEGA_PERM_READ, 0, 256, (void **)&pb, &va) == OMEGA_WORLD_OK && |
| 2570 | omega_world_resolve_buffer(&world, &c, OMEGA_PERM_WRITE, 0, 256, (void **)&pc, &va) == OMEGA_WORLD_OK; |
| 2571 | if (!ok) { |
| 2572 | omega_world_destroy(&world); |
| 2573 | return false; |
| 2574 | } |
| 2575 | const uint32_t n = 64, poison = 0xdeadbeefu; |
| 2576 | for (uint32_t i = 0; i < n; i++) { pa[i] = i * 3u; pb[i] = 7u; pc[i] = poison; } |
| 2577 | uint32_t poison_seen = 0; |
| 2578 | for (uint32_t i = 0; i < n; i++) if (pc[i] == poison) poison_seen++; |
| 2579 | printf(" negative control: output held poison 0x%08x in %u/%u elements before dispatch\n", |
| 2580 | poison, poison_seen, n); |
| 2581 | struct timespec t0, t1; |
| 2582 | uint32_t done = 0; |
| 2583 | clock_gettime(CLOCK_MONOTONIC, &t0); |
| 2584 | int rc = omega_world_dispatch_vector(&world, &code, &a, &b, &c, n, &done); |
| 2585 | clock_gettime(CLOCK_MONOTONIC, &t1); |
| 2586 | double wait_ms = (double)(t1.tv_sec - t0.tv_sec) * 1e3 + (double)(t1.tv_nsec - t0.tv_nsec) / 1e6; |
| 2587 | uint32_t wrong = 0; |
| 2588 | for (uint32_t i = 0; i < n; i++) if (pc[i] != i * 3u + 7u) wrong++; |
| 2589 | bool output_ok = (rc == OMEGA_WORLD_OK) && wrong == 0; |
| 2590 | printf(" WORLD_WAIT_BOUNDED wait_ms=%.3f output_ok=%d (rc=%d wrong=%u/%u)\n", |
| 2591 | wait_ms, output_ok ? 1 : 0, rc, wrong, n); |
| 2592 | omega_world_destroy(&world); |
| 2593 | return poison_seen == n && wait_ms <= 50.0 && output_ok; |
| 2594 | } |
| 2595 | |
| 2596 | int run_world_lifecycle_gates(void) { |
| 2597 | printf("================================================================================\n"); |
| 2598 | printf(" AIEN OMEGA M19R: WORLD LIFECYCLE GATES\n"); |
| 2599 | printf("================================================================================\n"); |
| 2600 | lc_gate_count = 0; |
| 2601 | lc_gate_passed = 0; |
| 2602 | report_lc_gate("WORLD_REVOKE_RELEASES_MEMORY", test_lc_revoke_releases_memory()); |
| 2603 | report_lc_gate("WORLD_REVOKE_WAITS_FOR_QUEUED_JOB", test_lc_revoke_waits_for_queued_job()); |
| 2604 | report_lc_gate("WORLD_ERROR_LATCH_COUNTS_ONCE", test_lc_error_latch_counts_once()); |
| 2605 | report_lc_gate("WORLD_WAIT_REACHED_OR_PASSED", test_lc_wait_reached_or_passed()); |
| 2606 | report_lc_gate("WORLD_OUTPUT_IN_FLIGHT_ALIAS_REFUSED", test_lc_output_alias_refusal()); |
| 2607 | report_lc_gate("WORLD_BOUNDS_OVERFLOW_REFUSED", test_lc_bounds_overflow()); |
| 2608 | report_lc_gate("WORLD_TIMEOUT_COMPLETION_RACE", test_lc_timeout_completion_race()); |
| 2609 | report_lc_gate("WORLD_STALE_GENERATION_MARKER_REFUSED", test_lc_stale_generation_marker()); |
| 2610 | report_lc_gate("WORLD_BATCHED_RESULT_IDENTITY", test_lc_batched_result_identity()); |
| 2611 | report_lc_gate("WORLD_STALE_HANDLE_AFTER_RESTART", test_lc_stale_handle_after_world_restart()); |
| 2612 | report_lc_gate("WORLD_FAULTED_REFUSES_REGISTER", test_lc_faulted_world_refuses_register()); |
| 2613 | report_lc_gate("WORLD_COMPLETION_WAIT_BOUNDED", test_lc_completion_wait_bounded()); |
| 2614 | printf(" TOTAL: %d | PASSED: %d | FAILED: %d\n", lc_gate_count, lc_gate_passed, lc_gate_count - lc_gate_passed); |
| 2615 | return (lc_gate_passed == lc_gate_count) ? 0 : 1; |
| 2616 | } |
| 2617 | |
| 2618 | typedef struct { |
| 2619 | uint64_t rm_balance, va_high_water, free_bytes, map_bytes; |
| 2620 | uint64_t vm_pages, rss_pages; |
| 2621 | uint32_t live_allocations, free_ranges, map_count, buffers, code; |
| 2622 | } M19RSoakSample; |
| 2623 | |
| 2624 | static M19RSoakSample m19r_sample(const OmegaAcceleratorWorld *world) { |
| 2625 | M19RSoakSample s = {0}; |
| 2626 | const Nvrm *rm = &world->m16.rm; |
| 2627 | s.rm_balance = rm->rm_alloc_accepted - rm->rm_free_accepted; |
| 2628 | s.va_high_water = rm->va_next; |
| 2629 | s.live_allocations = rm->live_count; |
| 2630 | s.free_ranges = rm->free_count; |
| 2631 | s.buffers = world->buffer_count; |
| 2632 | s.code = world->code_count; |
| 2633 | for (uint32_t i = 0; i < rm->free_count; i++) s.free_bytes += rm->free_list[i].size; |
| 2634 | FILE *f = fopen("/proc/self/maps", "r"); |
| 2635 | if (f) { |
| 2636 | char line[1024]; |
| 2637 | while (fgets(line, sizeof line, f)) { |
| 2638 | unsigned long lo, hi; |
| 2639 | if (strstr(line, "nvidia") && sscanf(line, "%lx-%lx", &lo, &hi) == 2 && hi >= lo) { |
| 2640 | s.map_count++; |
| 2641 | s.map_bytes += hi - lo; |
| 2642 | } |
| 2643 | } |
| 2644 | fclose(f); |
| 2645 | } |
| 2646 | f = fopen("/proc/self/statm", "r"); |
| 2647 | if (f) { |
| 2648 | unsigned long long vm = 0, rss = 0; |
| 2649 | if (fscanf(f, "%llu %llu", &vm, &rss) == 2) { |
| 2650 | s.vm_pages = vm; |
| 2651 | s.rss_pages = rss; |
| 2652 | } |
| 2653 | fclose(f); |
| 2654 | } |
| 2655 | return s; |
| 2656 | } |
| 2657 | |
| 2658 | static void m19r_print_sample(const char *name, const M19RSoakSample *s) { |
| 2659 | printf("M19R_RESOURCE_JSON:{\"phase\":\"%s\",\"driver_handle_count_available\":false," |
| 2660 | "\"driver_acknowledged_rm_balance\":%llu,\"internal_live_allocations\":%u," |
| 2661 | "\"va_high_water\":%llu,\"free_ranges\":%u,\"free_bytes\":%llu," |
| 2662 | "\"nvidia_process_mapping_count\":%u,\"nvidia_process_mapping_bytes\":%llu," |
| 2663 | "\"process_virtual_pages\":%llu,\"process_resident_pages\":%llu," |
| 2664 | "\"world_buffers\":%u,\"world_code\":%u}\n", |
| 2665 | name, (unsigned long long)s->rm_balance, s->live_allocations, |
| 2666 | (unsigned long long)s->va_high_water, s->free_ranges, |
| 2667 | (unsigned long long)s->free_bytes, s->map_count, |
| 2668 | (unsigned long long)s->map_bytes, (unsigned long long)s->vm_pages, |
| 2669 | (unsigned long long)s->rss_pages, s->buffers, s->code); |
| 2670 | fflush(stdout); |
| 2671 | } |
| 2672 | |
| 2673 | static bool m19r_soak_code(OmegaAcceleratorWorld *world, OmegaHandle *code) { |
| 2674 | uint8_t bytes[1024]; |
| 2675 | size_t size = 0; |
| 2676 | return omega_blackwell_encode_vecadd(bytes, sizeof bytes, &size) == 0 && |
| 2677 | omega_world_register_code(world, bytes, size, NULL, code) == OMEGA_WORLD_OK; |
| 2678 | } |
| 2679 | |
| 2680 | int run_m19r_soak(void) { |
| 2681 | const uint64_t cycles_target = 100000; |
| 2682 | const size_t allocation_bytes = 4u << 20; |
| 2683 | OmegaAcceleratorWorld world; |
| 2684 | OmegaHandle code, buffer; |
| 2685 | bool ok = omega_world_init(&world) == OMEGA_WORLD_OK && m19r_soak_code(&world, &code); |
| 2686 | if (!ok) { omega_world_destroy(&world); return 1; } |
| 2687 | /* Warm the allocator to the peak allocation size before measuring VA. */ |
| 2688 | ok = omega_world_register_buffer(&world, allocation_bytes, OMEGA_PERM_READ | OMEGA_PERM_WRITE, &buffer) == OMEGA_WORLD_OK && |
| 2689 | omega_world_revoke_buffer(&world, &buffer) == OMEGA_WORLD_OK; |
| 2690 | ok = ok && lc_vecadd_ok(&world, &code); |
| 2691 | if (!ok) { omega_world_destroy(&world); return 1; } |
| 2692 | M19RSoakSample start = m19r_sample(&world); |
| 2693 | m19r_print_sample("start", &start); |
| 2694 | struct timespec ts_start, ts_end; |
| 2695 | clock_gettime(CLOCK_MONOTONIC, &ts_start); |
| 2696 | uint64_t cycles = 0, churn_bytes = 0, recoveries = 0, rebuilds = 0; |
| 2697 | for (; cycles < cycles_target && ok; cycles++) { |
| 2698 | void *cpu = NULL; |
| 2699 | uint64_t gpu_va = 0; |
| 2700 | ok = omega_world_register_buffer(&world, allocation_bytes, |
| 2701 | OMEGA_PERM_READ | OMEGA_PERM_WRITE, &buffer) == OMEGA_WORLD_OK && |
| 2702 | omega_world_resolve_buffer(&world, &buffer, OMEGA_PERM_WRITE, |
| 2703 | 0, allocation_bytes, &cpu, &gpu_va) == OMEGA_WORLD_OK; |
| 2704 | if (!ok) break; |
| 2705 | volatile uint8_t *p = cpu; |
| 2706 | p[0] = (uint8_t)cycles; |
| 2707 | p[allocation_bytes - 1] = (uint8_t)(cycles >> 8); |
| 2708 | ok = p[0] == (uint8_t)cycles && p[allocation_bytes - 1] == (uint8_t)(cycles >> 8) && |
| 2709 | gpu_va != 0 && omega_world_revoke_buffer(&world, &buffer) == OMEGA_WORLD_OK && |
| 2710 | omega_world_resolve_buffer(&world, &buffer, OMEGA_PERM_READ, |
| 2711 | 0, 1, NULL, NULL) != OMEGA_WORLD_OK; |
| 2712 | if (!ok) break; |
| 2713 | churn_bytes += allocation_bytes; |
| 2714 | if ((cycles + 1) % 10000 == 0) { |
| 2715 | ok = lc_vecadd_ok(&world, &code) && |
| 2716 | omega_world_recover_channel_fault(&world) == OMEGA_WORLD_OK && |
| 2717 | lc_vecadd_ok(&world, &code); |
| 2718 | recoveries++; |
| 2719 | } |
| 2720 | if ((cycles + 1) % 25000 == 0) |
| 2721 | ok = ok && test_lc_revoke_waits_for_queued_job() && |
| 2722 | test_lc_error_latch_counts_once(); |
| 2723 | if ((cycles + 1) % 20000 == 0 && cycles + 1 < cycles_target) { |
| 2724 | ok = ok && omega_world_destroy(&world) == OMEGA_WORLD_OK; |
| 2725 | ok = ok && omega_world_init(&world) == OMEGA_WORLD_OK && |
| 2726 | m19r_soak_code(&world, &code); |
| 2727 | rebuilds++; |
| 2728 | if (ok) { |
| 2729 | ok = omega_world_register_buffer(&world, allocation_bytes, |
| 2730 | OMEGA_PERM_READ | OMEGA_PERM_WRITE, &buffer) == OMEGA_WORLD_OK && |
| 2731 | omega_world_revoke_buffer(&world, &buffer) == OMEGA_WORLD_OK; |
| 2732 | } |
| 2733 | } |
| 2734 | if ((cycles + 1) % 10000 == 0) { |
| 2735 | fprintf(stderr, "M19R soak: %llu cycles, %llu bytes\n", |
| 2736 | (unsigned long long)(cycles + 1), (unsigned long long)churn_bytes); |
| 2737 | } |
| 2738 | } |
| 2739 | clock_gettime(CLOCK_MONOTONIC, &ts_end); |
| 2740 | M19RSoakSample end = m19r_sample(&world); |
| 2741 | m19r_print_sample("end", &end); |
| 2742 | double duration = (double)(ts_end.tv_sec - ts_start.tv_sec) + |
| 2743 | (double)(ts_end.tv_nsec - ts_start.tv_nsec) / 1e9; |
| 2744 | FILE *mf = fopen("/proc/meminfo", "r"); |
| 2745 | unsigned long long memory_kb = 0; |
| 2746 | if (mf) { |
| 2747 | char line[256]; |
| 2748 | while (fgets(line, sizeof line, mf)) |
| 2749 | if (sscanf(line, "MemTotal: %llu kB", &memory_kb) == 1) break; |
| 2750 | fclose(mf); |
| 2751 | } |
| 2752 | bool stable = ok && cycles == cycles_target && memory_kb && |
| 2753 | churn_bytes > 2ull * memory_kb * 1024ull && |
| 2754 | start.rm_balance == end.rm_balance && |
| 2755 | start.live_allocations == end.live_allocations && |
| 2756 | start.va_high_water == end.va_high_water && |
| 2757 | start.free_ranges == end.free_ranges && start.free_bytes == end.free_bytes && |
| 2758 | start.map_count == end.map_count && start.map_bytes == end.map_bytes && |
| 2759 | start.buffers == end.buffers && start.code == end.code && !world.faulted; |
| 2760 | stable = omega_world_destroy(&world) == OMEGA_WORLD_OK && stable; |
| 2761 | M19RSoakSample post = m19r_sample(&world); |
| 2762 | m19r_print_sample("post_destroy", &post); |
| 2763 | stable = stable && post.rm_balance == 0 && post.live_allocations == 0 && |
| 2764 | post.map_count == 0 && post.buffers == 0 && post.code == 0; |
| 2765 | printf("M19R_SOAK_JSON:{\"passed\":%s,\"duration_seconds\":%.3f," |
| 2766 | "\"cycles\":%llu,\"bytes_churned\":%llu,\"physical_memory_bytes\":%llu," |
| 2767 | "\"channel_recoveries\":%llu,\"world_rebuilds\":%llu}\n", |
| 2768 | stable ? "true" : "false", duration, (unsigned long long)cycles, |
| 2769 | (unsigned long long)churn_bytes, memory_kb * 1024ull, |
| 2770 | (unsigned long long)recoveries, (unsigned long long)rebuilds); |
| 2771 | return stable ? 0 : 1; |
| 2772 | } |