Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/CUDApp/blob/main/tests/om_eng_diff_test.c # CUDApp CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / tests / om_eng_diff_test.c` 761 lines · 34856 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* om_eng_diff_test.c -- differential of om/engine/eng.om against the C oracles it replaced. 3 * 4 * The same driver is built twice (mk/om-qmd.mk, target test-om-eng-diff): 5 * om build default: every shell asks om/engine/eng.om (src/cudapp_om_eng.c); 6 * C build every CUDAPP_MIGRATION_PENDING_C_ defined: every shell runs its C oracle. 7 * Each build prints one transcript line per record. A record is one call of a public API (engine, wait, m16 wait, 8 * m16 release, platform contract, handle table, memory reserve, cached alloc, serving reserve) or of a gate in 9 * src/omega_eng_gates.h, with inputs drawn from a fixed-seed generator before the call, so both builds see the same 10 * inputs whatever path they take. The transcripts must be byte-identical; `--compare A B` names the first record 11 * that differs. 12 * 13 * Every call into the unit goes through --wrap=omega_eng: the om build must reach all ENG_OP_COUNT ops and the C build 14 * none. ENG_TRACE_FLIP flips the answer of one unit call (negative control): the comparison must then fail. 15 * Host only: no device is opened; the session, attention and matmul entry points are recording stubs below. */ 16 #include 17 #include 18 #include 19 #include 20 #include 21 #include 22 #include 23 #include 24 #include 25 26 #include "m16_gpu_wait.h" 27 #include "m16_native.h" 28 #include "omega_gpu_attention_api.h" 29 #include "omega_gpu_engine.h" 30 #include "omega_gpu_matmul_api.h" 31 #include "omega_gpu_mem_cached.h" 32 #include "omega_gpu_mem_reserve.h" 33 #include "omega_gpu_platform.h" 34 #include "omega_gpu_platform_linux.h" 35 #include "omega_gpu_serving.h" 36 #include "omega_gpu_session.h" 37 #include "omega_gpu_wait.h" 38 39 #if defined(CUDAPP_MIGRATION_PENDING_C_LIFECYCLE) 40 #define ENG_C_PART 1 41 #endif 42 #include "omega_eng_dec.h" 43 #include "omega_eng_gates.h" 44 45 /* ---- unit call counter (both builds link with -Wl,--wrap=omega_eng) ---- */ 46 uint32_t __real_omega_eng(uint64_t *result, const void *p0, uint64_t p0_n, void *p1, uint64_t p1_n); 47 uint32_t __wrap_omega_eng(uint64_t *result, const void *p0, uint64_t p0_n, void *p1, uint64_t p1_n); 48 static uint64_t g_opn[ENG_OP_COUNT + 1]; 49 static uint64_t g_calls; 50 uint32_t __wrap_omega_eng(uint64_t *result, const void *p0, uint64_t p0_n, void *p1, uint64_t p1_n) { 51 uint64_t op = ((const uint64_t *)p0)[0]; 52 uint32_t st = __real_omega_eng(result, p0, p0_n, p1, p1_n); 53 g_opn[op < ENG_OP_COUNT ? op : ENG_OP_COUNT]++; 54 g_calls++; 55 #if defined(ENG_TRACE_FLIP) 56 if (g_calls == ENG_TRACE_FLIP) ((uint64_t *)p1)[0] ^= 1u; 57 #endif 58 return st; 59 } 60 61 /* ---- generator ---- */ 62 static uint64_t g_rng = 0x9e3779b97f4a7c15ull; 63 static uint64_t rnd(void) { 64 g_rng ^= g_rng << 13; 65 g_rng ^= g_rng >> 7; 66 g_rng ^= g_rng << 17; 67 return g_rng; 68 } 69 static uint64_t below(uint64_t n) { return n ? rnd() % n : 0; } 70 /* Values at the thresholds the oracles compare against, plus their neighbours. */ 71 static const uint64_t EDGE[] = { 72 0, 1, 2, 3, 4, 5, 6, 7, 12, 13, 48, 49, 50, 63, 64, 65, 127, 128, 129, 150, 151, 255, 256, 4095, 4096, 4097, 73 5000, 8191, 8192, 8193, 16383, 16384, 16385, 262143, 262144, 262145, 0x16000001, 0x16000002, 0x16c0ffee, 74 0x19c0ffee, 0x46464646, 0x7fffffff, 0x80000000, 0x80000001, `2147483647`, 2147483648ull, 2147483649ull, 75 4294967231ull, 4294967232ull, 4294967233ull, 0xfffffffe, 0xffffffff, 0x100000000ull, 0x100000001ull, 76 274877906943ull, 274877906944ull, 274877906945ull, 0x7fffffffffffffffull, 0x8000000000000000ull, 77 0xffffffffffffefffull, 0xfffffffffffff000ull, 0xfffffffffffff001ull, 0xfffffffffffffffeull, 0xffffffffffffffffull}; 78 static uint64_t pick(void) { 79 switch (below(4)) { 80 case 0: return EDGE[below(sizeof EDGE / sizeof EDGE[0])]; 81 case 1: return below(16); 82 case 2: return rnd() & 0xffffffffu; 83 default: return rnd(); 84 } 85 } 86 static uint32_t pick32(void) { return (uint32_t)pick(); } 87 88 static FILE *g_out; 89 static unsigned long g_rec; 90 #define REC(...) do { fprintf(g_out, "r%06lu ", g_rec++); fprintf(g_out, __VA_ARGS__); fputc('\n', g_out); } while (0) 91 92 /* ---- recording stubs: session, attention, matmul ---- */ 93 static char g_err[256]; 94 static char g_log[512]; 95 static size_t g_logn; 96 static void logs(const char *s) { 97 size_t n = strlen(s); 98 if (g_logn + n + 1 >= sizeof g_log) return; 99 memcpy(g_log + g_logn, s, n); 100 g_logn += n; 101 g_log[g_logn] = 0; 102 } 103 static void logu(const char *tag, uint64_t v) { 104 char b[48]; 105 snprintf(b, sizeof b, "%s%" PRIx64 ",", tag, v); 106 logs(b); 107 } 108 static void log_reset(void) { g_logn = 0; g_log[0] = 0; g_err[0] = 0; } 109 110 void omega_gpu_session_set_error(const char *stage) { snprintf(g_err, sizeof g_err, "%s", stage ? stage : "(null)"); } 111 static OmegaGpuSession g_sess; 112 static int g_dma_rc; 113 static uint64_t g_dma_len; 114 static uint32_t g_dma_buf[16]; 115 static int fake_dma_alloc(void *ctx, OmegaGpuDeviceHandle dev, const OmegaGpuBufferDesc *d, OmegaGpuBufferHandle *out, 116 OmegaCpuView *cpu, OmegaGpuVa *gpu, OmegaGpuBusAddr *bus) { 117 (void)ctx; (void)dev; 118 logu("dk", d->kind); logu("dr", d->rights); logu("dc", d->gpu_cache); logu("da", d->align); logu("db", d->bytes); 119 memset(out, 0, sizeof *out); 120 cpu->ptr = g_dma_rc == OMEGA_GPU_PLAT_OK ? g_dma_buf : NULL; 121 cpu->len = (size_t)g_dma_len; 122 gpu->va = 0x100000; 123 bus->iova = 0x100000; 124 return g_dma_rc; 125 } 126 static int fake_dma_free(void *ctx, OmegaGpuBufferHandle b) { (void)ctx; (void)b; logs("free,"); return 0; } 127 static OmegaGpuPlatform g_plat = {.dma_alloc = fake_dma_alloc, .dma_free = fake_dma_free}; 128 OmegaGpuSession *omega_gpu_session_open(void) { logs("open,"); return &g_sess; } 129 const OmegaGpuPlatform *omega_gpu_session_platform(void) { return &g_plat; } 130 131 static int g_attn_rc, g_rows_rc, g_mm_rc; 132 int omega_gpu_attention_reserve(uint32_t max_context, uint32_t max_seqs, uint32_t num_q_heads, uint32_t num_kv_heads, 133 uint32_t head_dim, uint32_t kv_block_size) { 134 logu("ar", max_context); logu("", max_seqs); logu("", num_q_heads); logu("", num_kv_heads); logu("", head_dim); 135 logu("", kv_block_size); 136 return g_attn_rc; 137 } 138 int omega_gpu_attention_reserve_rows(uint32_t max_rows, uint32_t num_q_heads, uint32_t head_dim) { 139 logu("rr", max_rows); logu("", num_q_heads); logu("", head_dim); 140 return g_rows_rc; 141 } 142 void omega_gpu_attention_unreserve(void) { logs("au,"); } 143 int omega_gpu_matmul_reserve(uint32_t max_rows, uint32_t max_k, uint32_t max_n, uint32_t max_n_one_row, uint32_t kernel_slots) { 144 logu("mr", max_rows); logu("", max_k); logu("", max_n); logu("", max_n_one_row); logu("", kernel_slots); 145 return g_mm_rc; 146 } 147 void omega_gpu_matmul_seal(int on) { logu("ms", (uint64_t)on); } 148 void omega_gpu_matmul_unreserve(void) { logs("mu,"); } 149 150 /* ---- engine: scripted backend ---- */ 151 enum { CB_OPEN, CB_CHAN, CB_ALLOC, CB_COPYIN, CB_POISON, CB_BUILD, CB_SUBMIT, CB_WM, CB_WM2, CB_WREL, CB_BAR, CB_COPYOUT, 152 CB_FREE, CB_CLOSE, CB_N }; 153 typedef struct { 154 int rc[CB_N]; 155 int err[CB_N]; 156 uint32_t out_mode; /* 0 poison back, 1 all changed, 2 some changed */ 157 uint32_t diag[4]; 158 const uint32_t *poison; 159 size_t poison_count; 160 } Script; 161 static Script g_sc; 162 static const char CBNAME[CB_N] = {'o', 'c', 'a', 'i', 'p', 'b', 's', 'm', 'n', 'r', 'v', 'x', 'f', 'z'}; 163 static int cb(int which) { 164 char s[2] = {CBNAME[which], 0}; 165 logs(s); 166 errno = g_sc.rc[which] ? g_sc.err[which] : 0; 167 return g_sc.rc[which]; 168 } 169 static int b_open(void *c) { (void)c; return cb(CB_OPEN); } 170 static int b_chan(void *c) { (void)c; return cb(CB_CHAN); } 171 static int b_alloc(void *c, int role, size_t bytes) { (void)c; logu("", (uint64_t)role); logu("", bytes); return cb(CB_ALLOC); } 172 static int b_copyin(void *c, int role, const void *src, size_t bytes) { (void)c; (void)src; logu("", (uint64_t)role); logu("", bytes); return cb(CB_COPYIN); } 173 static int b_poison(void *c, const uint32_t *w, size_t n) { (void)c; (void)w; logu("", n); return cb(CB_POISON); } 174 static int b_build(void *c, const OmegaGpuJob *j) { (void)c; (void)j; return cb(CB_BUILD); } 175 static int b_submit(void *c) { (void)c; return cb(CB_SUBMIT); } 176 static int b_wm(void *c, uint64_t t) { (void)c; logu("", t); return cb(CB_WM); } 177 static int b_wm2(void *c, uint64_t t) { (void)c; logu("", t); return cb(CB_WM2); } 178 static int b_wrel(void *c, uint64_t t) { (void)c; logu("", t); return cb(CB_WREL); } 179 static int b_bar(void *c, uint64_t t) { (void)c; logu("", t); return cb(CB_BAR); } 180 static int b_copyout(void *c, void *dst, size_t bytes) { 181 (void)c; 182 uint32_t *d = dst; 183 size_t i, n = bytes / 4u; 184 for (i = 0; i < n; i++) { 185 uint32_t p = i < g_sc.poison_count && g_sc.poison ? g_sc.poison[i] : 0u; 186 d[i] = g_sc.out_mode == 0 ? p : g_sc.out_mode == 1 ? ~p : ((i & 3u) ? p : ~p); 187 } 188 logu("", bytes); 189 return cb(CB_COPYOUT); 190 } 191 static void b_diag(void *c, OmegaGpuBackendDiag *d) { 192 (void)c; 193 d->drv_rc = (int)g_sc.diag[0]; 194 snprintf(d->drv_text, sizeof d->drv_text, "drv %u", g_sc.diag[1]); 195 d->sync_valid = g_sc.diag[2] & 7u; 196 d->marker = g_sc.diag[3]; 197 d->marker2 = ~g_sc.diag[3]; 198 d->semaphore[0] = g_sc.diag[1]; 199 } 200 static int b_free(void *c, int role) { (void)c; logu("", (uint64_t)role); return cb(CB_FREE); } 201 static int b_close(void *c) { (void)c; return cb(CB_CLOSE); } 202 static const OmegaGpuBackend g_be = {NULL, b_open, b_chan, b_alloc, b_copyin, b_poison, b_build, b_submit, b_wm, b_wm2, 203 b_wrel, b_bar, b_copyout, b_diag, b_free, b_close}; 204 static uint64_t g_ms; 205 static uint64_t g_ms_step; 206 static uint64_t eng_now(void) { g_ms += g_ms_step; return g_ms; } 207 208 static void trace_engine(void) { 209 static uint32_t a[4100], b[4100], o[4100], poison[4100]; 210 static const unsigned char prog[64] = {1}; 211 static const uint32_t COUNTS[] = {0, 1, 2, 63, 64, 65, 1024, 4096, 4097, 0x40000000u, 0xffffffffu}; 212 OmegaGpuJob j; 213 OmegaGpuResult r; 214 uint32_t count = below(3) ? (uint32_t)(1 + below(64)) : COUNTS[below(sizeof COUNTS / sizeof COUNTS[0])]; 215 size_t bytes = (size_t)count * 4u; 216 int fit = count <= 4096u; 217 int i; 218 memset(&j, 0, sizeof j); 219 memset(&g_sc, 0, sizeof g_sc); 220 for (i = 0; i < CB_N; i++) { 221 uint64_t v = below(24); 222 g_sc.rc[i] = v == 0 ? -1 : v == 1 ? OMEGA_GPU_BACKEND_TIMEOUT : v == 2 ? OMEGA_GPU_BACKEND_UNKNOWN_STATE : v == 3 ? 7 : 0; 223 g_sc.err[i] = (int)below(40); 224 } 225 g_sc.out_mode = (uint32_t)below(3); 226 for (i = 0; i < 4; i++) g_sc.diag[i] = (uint32_t)rnd(); 227 for (i = 0; i < 4100; i++) poison[i] = (uint32_t)rnd() | 1u; 228 j.program = below(16) ? prog : NULL; 229 j.program_len = below(16) ? 1 + below(64) : 0; 230 j.layout = below(16) ? OMEGA_GPU_LAYOUT_VECTOR_1D : (int)below(4); 231 j.element_count = count; 232 j.input_a = below(16) ? a : NULL; 233 j.input_b = below(16) ? b : NULL; 234 j.output = below(16) ? (below(16) ? (void *)o : (void *)((char *)o + 2)) : NULL; 235 j.input_a_len = fit && below(12) ? bytes : bytes + 4u; 236 j.input_b_len = fit && below(12) ? bytes : bytes - 4u; 237 j.output_len = fit && below(12) ? bytes : bytes + 8u; 238 j.poison = below(16) ? poison : NULL; 239 j.poison_count = below(12) ? count : count + 1u; 240 j.timeouts.marker_ms = below(4) ? below(6000) : pick(); 241 j.timeouts.release_semaphore_ms = below(4) ? below(6000) : pick(); 242 j.timeouts.marker2_ms = below(4) ? below(6000) : pick(); 243 j.timeouts.visibility_ms = below(4) ? below(6000) : pick(); 244 j.flags = below(4) ? 0u : (uint32_t)below(4); 245 if (!fit) { 246 /* Never let a buffer claim more than it holds: these counts only test the refusals. */ 247 j.input_a_len = 16; 248 j.output_len = 16; 249 } 250 g_sc.poison = j.poison; 251 g_sc.poison_count = j.poison_count; 252 g_ms = 1000; 253 g_ms_step = 1 + below(700); 254 int reset = below(5) != 0; 255 int nullres = below(64) == 0; 256 int nulljob = below(64) == 0; 257 log_reset(); 258 if (reset) omega_gpu_engine_test_reset_block(); 259 memset(&r, 0xa5, sizeof r); 260 int ret = omega_gpu_execute(nulljob ? NULL : &j, nullres ? NULL : &r); 261 if (nullres) { 262 REC("eng ret=%d blk=%d log=%s", ret, omega_gpu_engine_is_blocked(), g_log); 263 return; 264 } 265 REC("eng ret=%d f=%d st=%d step=%d w=%d wt=%" PRIu64 " wd=%" PRIu64 " rc=%d en=%d txt=%s sv=%u m=%u m2=%u s=%u ret=%d cf=%d no=%zu" 266 " un=%" PRIu64 " blk=%d log=%s", 267 ret, r.failure, r.last_state, r.failed_step, r.wait, r.wait_timeout_ms, r.waited_ms, r.drv_rc, r.err_no, 268 r.drv_text, r.sync_valid, r.marker, r.marker2, r.semaphore[0], r.retained, r.cleanup_failed, r.n_outputs, 269 r.n_outputs ? r.output_unchanged_words[0] : 0u, omega_gpu_engine_is_blocked(), g_log); 270 } 271 272 /* ---- waits: scripted word, clock, snapshot, validity, abort ---- */ 273 typedef struct { 274 volatile uint32_t word, m2; 275 uint32_t vals[64], m2v[64]; 276 uint32_t n, i; 277 uint64_t t, step; 278 uint32_t bad_at, abort_at, snap_mode; 279 uint32_t barriers; 280 } WaitScript; 281 static WaitScript g_ws; 282 static uint64_t ws_now(void *c) { WaitScript *s = c; s->t += s->step; return s->t; } 283 static void ws_barrier(void *c) { ((WaitScript *)c)->barriers++; } 284 static void ws_relax(void *c) { 285 WaitScript *s = c; 286 if (s->i + 1 < s->n) s->i++; 287 s->word = s->vals[s->i]; 288 s->m2 = s->m2v[s->i]; 289 } 290 static void ws_snap(void *c, uint32_t out[2]) { 291 WaitScript *s = c; 292 out[0] = s->snap_mode ? s->i / s->snap_mode : 0; 293 out[1] = 7; 294 } 295 static bool ws_valid(void *c, uint32_t v) { WaitScript *s = c; (void)v; return s->i != s->bad_at; } 296 static bool ws_abort(void *c) { WaitScript *s = c; return s->i == s->abort_at; } 297 static void ws_draw(void) { 298 uint32_t k; 299 memset(&g_ws, 0, sizeof g_ws); 300 g_ws.n = 1 + (uint32_t)below(64); 301 uint32_t base = (uint32_t)pick(); 302 for (k = 0; k < g_ws.n; k++) { 303 g_ws.vals[k] = below(3) ? base + k / (1 + (uint32_t)below(4)) : (uint32_t)pick(); 304 g_ws.m2v[k] = below(2) ? 0x5au : (uint32_t)below(3); 305 } 306 g_ws.word = g_ws.vals[0]; 307 g_ws.m2 = g_ws.m2v[0]; 308 g_ws.t = `1000000` + below(1000); 309 g_ws.step = 100000 + below(`20000000`); 310 g_ws.bad_at = below(4) ? 1000u : (uint32_t)below(64); 311 g_ws.abort_at = below(4) ? 1000u : (uint32_t)below(64); 312 g_ws.snap_mode = (uint32_t)below(4); 313 } 314 315 static void trace_wait(int m16) { 316 ws_draw(); 317 uint32_t target = below(2) ? g_ws.vals[below(g_ws.n)] : (uint32_t)pick(); 318 int seq = (int)below(2); 319 uint32_t total = below(4) ? (uint32_t)(1 + below(60)) : (uint32_t)below(3); 320 uint32_t prog = below(4) ? (uint32_t)(1 + below(30)) : 0u; 321 int use_m2 = (int)below(3) == 0, use_snap = (int)below(2), use_valid = (int)below(3) == 0, use_abort = (int)below(3) == 0; 322 uint32_t m2want = below(2) ? 0x5au : (uint32_t)below(3); 323 /* A NULL cfg selects the real monotonic clock and the 5 s / 600 s defaults: not replayable, so not drawn. */ 324 int nullrpt = below(64) == 0, nullcfg = 0; 325 if (total == 0 && prog == 0) prog = 1; /* the defaults (600 s) would make one record run minutes of fake polls */ 326 if (total == 0) total = 1; 327 if (!m16) { 328 omega_gpu_wait_cfg_t cfg; 329 omega_gpu_wait_report_t r; 330 memset(&cfg, 0, sizeof cfg); 331 memset(&r, 0x5a, sizeof r); 332 cfg.total_timeout_ms = total; 333 cfg.progress_timeout_ms = prog; 334 cfg.marker2 = use_m2 ? &g_ws.m2 : NULL; 335 cfg.marker2_want = m2want; 336 cfg.snapshot = use_snap ? ws_snap : NULL; 337 cfg.snapshot_ctx = &g_ws; 338 cfg.valid = use_valid ? ws_valid : NULL; 339 cfg.abort = use_abort ? ws_abort : NULL; 340 cfg.now_ns = ws_now; 341 cfg.barrier = ws_barrier; 342 cfg.relax = ws_relax; 343 cfg.hook_ctx = &g_ws; 344 bool ok = seq ? omega_gpu_wait_sequence(&g_ws.word, target, nullrpt ? NULL : &r, nullcfg ? NULL : &cfg) 345 : omega_gpu_wait_fixed(&g_ws.word, target, nullrpt ? NULL : &r, nullcfg ? NULL : &cfg); 346 if (nullrpt) { REC("wait ok=%d i=%u bar=%u", ok, g_ws.i, g_ws.barriers); return; } 347 REC("wait ok=%d k=%d res=%d exp=%u lo=%u lm2=%u s=%u,%u st=%" PRIu64 " el=%" PRIu64 " lp=%" PRIu64 " pc=%" PRIu64 348 " po=%" PRIu64 " i=%u bar=%u", 349 ok, (int)r.wait_kind, (int)r.result, r.expected, r.last_observed, r.last_marker2, r.snap[0], r.snap[1], 350 r.start_ns, r.elapsed_ns, r.last_progress_ns, r.progress_count, r.polls, g_ws.i, g_ws.barriers); 351 } else { 352 m16_gpu_wait_cfg_t cfg; 353 m16_gpu_wait_report_t r; 354 memset(&cfg, 0, sizeof cfg); 355 memset(&r, 0x5a, sizeof r); 356 cfg.total_timeout_ms = total; 357 cfg.progress_timeout_ms = prog; 358 cfg.marker2 = use_m2 ? &g_ws.m2 : NULL; 359 cfg.marker2_want = m2want; 360 cfg.snapshot = use_snap ? ws_snap : NULL; 361 cfg.snapshot_ctx = &g_ws; 362 cfg.valid = use_valid ? ws_valid : NULL; 363 cfg.abort = use_abort ? ws_abort : NULL; 364 cfg.now_ns = ws_now; 365 cfg.barrier = ws_barrier; 366 cfg.relax = ws_relax; 367 cfg.hook_ctx = &g_ws; 368 bool ok = seq ? m16_gpu_wait_sequence(&g_ws.word, target, nullrpt ? NULL : &r, nullcfg ? NULL : &cfg) 369 : m16_gpu_wait_fixed(&g_ws.word, target, nullrpt ? NULL : &r, nullcfg ? NULL : &cfg); 370 if (nullrpt) { REC("m16wait ok=%d i=%u bar=%u", ok, g_ws.i, g_ws.barriers); return; } 371 REC("m16wait ok=%d k=%d res=%d exp=%u lo=%u lm2=%u s=%u,%u st=%" PRIu64 " el=%" PRIu64 " lp=%" PRIu64 372 " pc=%" PRIu64 " po=%" PRIu64 " i=%u bar=%u fc=%s", 373 ok, (int)r.wait_kind, (int)r.result, r.expected, r.last_observed, r.last_marker2, r.snap[0], r.snap[1], 374 r.start_ns, r.elapsed_ns, r.last_progress_ns, r.progress_count, r.polls, g_ws.i, g_ws.barriers, 375 m16_gpu_wait_failure_class(&r, m2want)); 376 } 377 } 378 379 static void trace_fclass(void) { 380 m16_gpu_wait_report_t r; 381 memset(&r, 0, sizeof r); 382 r.wait_kind = (m16_gpu_wait_kind_t)(1 + below(2)); 383 r.result = (m16_gpu_wait_result_t)below(6); 384 r.expected = pick32(); 385 r.last_observed = below(2) ? r.expected : pick32(); 386 r.last_marker2 = below(2) ? 0x5au : pick32(); 387 r.snap[0] = pick32(); 388 r.snap[1] = pick32(); 389 r.progress_count = below(3) ? below(3) : pick(); 390 r.polls = pick(); 391 uint32_t want = below(2) ? 0x5au : pick32(); 392 /* r is never NULL here: neither the C oracle nor the om shell accepts a NULL report (m16_gpu_wait.h). */ 393 REC("fclass %s", m16_gpu_wait_failure_class(&r, want)); 394 } 395 396 static void trace_release(void) { 397 uint32_t w[10]; 398 int i; 399 for (i = 0; i < 10; i++) w[i] = 0xdeadbeefu; 400 uint64_t va = pick(); 401 uint32_t payload = pick32(), cls = below(2) ? 0xcec0u : pick32(); 402 int nul = below(32) == 0; 403 int n = m16_native_build_release(nul ? NULL : w, va, payload, cls); 404 REC("rel n=%d %08x %08x %08x %08x %08x %08x %08x %08x %08x", n, w[0], w[1], w[2], w[3], w[4], w[5], w[6], w[7], w[8]); 405 } 406 407 /* ---- platform contract and handle table ---- */ 408 static void caps_draw(OmegaGpuPlatformCaps *c) { 409 c->abi_major = below(4) ? 1u : pick32(); 410 c->abi_minor = below(4) ? (uint32_t)below(4) : pick32(); 411 c->services = below(3) ? (uint32_t)(below(2) ? OMEGA_GPU_SVC_REQUIRED_SESSION : OMEGA_GPU_SVC_REQUIRED_V1) ^ (uint32_t)(below(2) << below(13)) 412 : pick32(); 413 c->max_devices = below(2) ? 1u : pick32(); 414 c->max_buffers = pick32(); 415 c->max_queues = pick32(); 416 c->max_inflight = pick32(); 417 c->max_buffer_bytes = pick(); 418 c->max_total_bytes = pick(); 419 c->submit_prefix_words = pick32(); 420 } 421 static void dummy_fn(void) {} 422 static void trace_negotiate(void) { 423 OmegaGpuPlatformCaps p, q; 424 caps_draw(&p); 425 caps_draw(&q); 426 if (below(2)) { q.abi_major = p.abi_major; q.services = p.services & q.services; } 427 int np = below(64) == 0, nq = below(64) == 0; 428 REC("nego %d", omega_gpu_platform_negotiate(np ? NULL : &p, nq ? NULL : &q)); 429 } 430 static void trace_check(void) { 431 OmegaGpuPlatform p; 432 OmegaGpuPlatformCaps c; 433 uint64_t mask = below(3) ? ~below(2) << below(24) : rnd(); 434 int k = 0; 435 memset(&p, 0, sizeof p); 436 #define SETF(m) p.m = ((mask >> (k++)) & 1u) ? (__typeof__(p.m))dummy_fn : NULL 437 SETF(time_now_ms); SETF(wait_ms); SETF(lock); SETF(unlock); SETF(mmio_read32); SETF(mmio_write32); SETF(dma_alloc); 438 SETF(dma_free); SETF(sync_cpu_to_gpu); SETF(sync_gpu_to_cpu); SETF(poll_word); SETF(irq_wait); SETF(device_open); 439 SETF(device_close); SETF(diag_text); SETF(queue_open); SETF(queue_close); SETF(queue_submit); SETF(queue_retire); 440 SETF(time_now_ns); SETF(host_alloc); SETF(host_free); 441 #undef SETF 442 caps_draw(&c); 443 uint32_t req = below(3) ? (below(2) ? OMEGA_GPU_SVC_REQUIRED_SESSION : OMEGA_GPU_SVC_REQUIRED_V1) : pick32(); 444 int np = below(64) == 0, nc = below(64) == 0; 445 REC("pchk %d %d", omega_gpu_platform_check_for(np ? NULL : &p, nc ? NULL : &c, req), 446 omega_gpu_platform_check(np ? NULL : &p, nc ? NULL : &c)); 447 } 448 static void trace_buffer(void) { 449 OmegaGpuBufferDesc d; 450 OmegaGpuPlatformCaps c; 451 caps_draw(&c); 452 d.kind = below(3) ? (uint32_t)(2 + below(3)) : pick32(); 453 d.rights = below(3) ? (uint32_t)below(64) : pick32(); 454 d.gpu_cache = below(4) ? (uint32_t)below(2) : pick32(); 455 d.align = below(3) ? (below(2) ? 0u : 1u << below(16)) : pick32(); 456 d.bytes = below(3) ? below(1u << 20) : pick(); 457 int nd = below(64) == 0, nc = below(64) == 0; 458 REC("buf %d", omega_gpu_platform_check_buffer(nd ? NULL : &d, nc ? NULL : &c)); 459 } 460 static OmegaGpuHandleTable g_ht; 461 static void trace_handles(int fresh) { 462 if (fresh) omega_gpu_handles_init(&g_ht); 463 uint32_t kind = below(4) ? (uint32_t)(1 + below(5)) : pick32(); 464 uint32_t idx = 0xeeeeeeeeu, gen = 0xeeeeeeeeu; 465 int rc; 466 switch (below(3)) { 467 case 0: { 468 uint64_t bytes = below(3) ? 1 + below(1u << 20) : pick(); 469 uint32_t ls = below(3) ? (uint32_t)(1 + below(64)) : pick32(); 470 uint64_t lb = below(3) ? 1 + below(1u << 26) : pick(); 471 int nt = below(128) == 0, ni = below(128) == 0; 472 rc = omega_gpu_handles_open(nt ? NULL : &g_ht, kind, bytes, ls, lb, ni ? NULL : &idx, &gen); 473 REC("hopen %d %u %u nl=%u tb=%" PRIu64, rc, idx, gen, g_ht.nlive, g_ht.total_bytes); 474 break; 475 } 476 case 1: 477 idx = below(3) ? (uint32_t)below(80) : pick32(); 478 gen = below(3) ? (uint32_t)below(4) : pick32(); 479 rc = omega_gpu_handles_check(below(128) ? &g_ht : NULL, idx, gen, kind); 480 REC("hchk %d", rc); 481 break; 482 default: 483 idx = below(3) ? (uint32_t)below(80) : pick32(); 484 gen = below(3) ? (uint32_t)below(4) : pick32(); 485 rc = omega_gpu_handles_close(below(128) ? &g_ht : NULL, idx, gen, kind); 486 REC("hclose %d nl=%u tb=%" PRIu64, rc, g_ht.nlive, g_ht.total_bytes); 487 break; 488 } 489 } 490 491 /* ---- memory reserve, cached alloc, serving reserve ---- */ 492 static char g_meminfo[64]; 493 static void trace_reserve(void) { 494 static const char *ENVS[] = {NULL, "", "0", "1", "512", "4096", "`17592186044415`", "`17592186044416`", "`17592186044417`", 495 "`18446744073709551615`", "12x", "-1", "-2", "-0", "+0", " +512", "\t+1", 496 "`18446744073709551616`", "-`18446744073709551616`", "1 ", " ", "+", "++1", "01", 497 "`000000000000000000000000000000000000000000000000000000000000000000001`", 498 "`999999999999999999999999999999999999999999999999999999999999999999999`"}; 499 const char *e = ENVS[below(sizeof ENVS / sizeof ENVS[0])]; 500 char envb[32]; 501 if (below(3) == 0) { snprintf(envb, sizeof envb, "%" PRIu64, pick()); e = envb; } 502 uint64_t avail = below(3) ? below(1ull << 27) : pick() & 0xffffffffffffull; 503 int has_line = below(8) != 0; 504 int fd = open(g_meminfo, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600); 505 FILE *f = fd < 0 ? NULL : fdopen(fd, "w"); 506 if (!f && fd >= 0) close(fd); 507 if (!f) { fprintf(stderr, "cannot write %s\n", g_meminfo); exit(2); } 508 fprintf(f, "MemTotal: `131072000` kB\n"); 509 if (has_line) { 510 static const char *FMTS[] = {"MemAvailable: %" PRIu64 " kB\n", "MemAvailable:\t+%" PRIu64 "\n", 511 "MemAvailable: -%" PRIu64 " kB\n", "MemAvailable: %" PRIu64 "xyz\n", 512 "MemFree: %" PRIu64 " kB\n", " MemAvailable: %" PRIu64 " kB\n", 513 "memAvailable: %" PRIu64 " kB\n", "MemAvailable: + kB %" PRIu64 "\n", 514 "MemAvailable: `18446744073709551616` kB %" PRIu64 "\n", 515 "MemAvailable: -`18446744073709551616` kB %" PRIu64 "\n", 516 "MemAvailable: 0 kB %" PRIu64 "\n", "MemAvailable: kB %" PRIu64 "\n"}; 517 const char *fmt = FMTS[below(sizeof FMTS / sizeof FMTS[0])]; 518 fprintf(f, fmt, avail); 519 } 520 fclose(f); 521 if (e) setenv("CUDAPP_MEM_RESERVE_MIB", e, 1); else unsetenv("CUDAPP_MEM_RESERVE_MIB"); 522 uint64_t bytes = below(3) ? below(1ull << 37) : pick(); 523 char diag[512]; 524 diag[0] = 0; 525 int rc = omega_gpu_mem_reserve_check(bytes, diag, sizeof diag); 526 REC("resv %d rb=%" PRIu64 " %s", rc, omega_gpu_mem_reserve_bytes(), diag); 527 } 528 static void trace_cached(void) { 529 static const uint64_t B[] = {0, 1, 4095, 4096, 4097, 8191, 8192, 0xfffff000ull, 0xffffffffffffefffull, 530 0xfffffffffffff000ull, 0xfffffffffffff001ull, 0xffffffffffffffffull}; 531 uint64_t bytes = below(2) ? B[below(sizeof B / sizeof B[0])] : (below(2) ? below(1u << 24) : rnd()); 532 OmegaGpuMem m; 533 int nul = below(16) == 0; 534 uint64_t v = below(4); 535 g_dma_rc = v == 0 ? OMEGA_GPU_PLAT_REFUSED_LIMIT : OMEGA_GPU_PLAT_OK; 536 g_dma_len = v == 1 ? 0 : 0xffffffffffffffffull; 537 log_reset(); 538 memset(&m, 0x77, sizeof m); 539 int rc = omega_gpu_session_alloc_cached((size_t)bytes, nul ? NULL : &m); 540 REC("cache %d k=%u sz=%" PRIu64 " err=%s log=%s", rc, nul ? 0u : m.kind, nul ? 0u : m.size, g_err, g_log); 541 } 542 static void trace_serving(void) { 543 OmegaGpuServingBounds b; 544 b.max_context = pick32(); 545 b.max_seqs = below(2) ? (uint32_t)below(3) : pick32(); 546 b.num_q_heads = pick32(); 547 b.num_kv_heads = pick32(); 548 b.head_dim = pick32(); 549 b.kv_block_size = pick32(); 550 b.max_rows = below(2) ? (uint32_t)below(3) : pick32(); 551 b.max_k = pick32(); 552 b.max_n = pick32(); 553 b.max_n_one_row = below(2) ? 0u : pick32(); 554 b.kernel_slots = pick32(); 555 g_attn_rc = below(5) ? 0 : 3; 556 g_rows_rc = below(5) ? 0 : 4; 557 g_mm_rc = below(5) ? 0 : 5; 558 int nul = below(32) == 0; 559 log_reset(); 560 int rc = omega_gpu_reserve_serving(nul ? NULL : &b); 561 REC("serve %d log=%s", rc, g_log); 562 } 563 564 /* ---- gates of src/omega_eng_gates.h ---- */ 565 static void trace_gates(void) { 566 int k = (int)below(16); 567 switch (k) { 568 case 0: case 1: case 2: case 3: { 569 uint32_t kk = below(8) ? (uint32_t)below(17) : pick32(); 570 uint64_t a[6]; 571 int i; 572 for (i = 0; i < 6; i++) a[i] = below(3) ? below(4) : pick(); 573 if (below(2)) a[1] = a[0]; 574 if (below(2)) a[5] = a[4]; 575 if (below(2)) a[3] = a[2]; 576 REC("life %u %d", kk, omega_gate_life(kk, a[0], a[1], a[2], a[3], a[4], a[5])); 577 break; 578 } 579 case 4: { 580 uint32_t kk = (uint32_t)below(4); 581 int64_t a = (int64_t)pick(); 582 REC("reqf %u %d %u", kk, omega_gate_reqf(kk, a), omega_gate_reqf_payload()); 583 break; 584 } 585 case 5: { 586 uint32_t kk = (uint32_t)below(4); 587 uint64_t a = below(2) ? below(3) : pick(), b = below(2) ? below(3) : pick(), c = below(2) ? b : pick(); 588 REC("vis %u %d", kk, omega_gate_vis(kk, a, b, c)); 589 break; 590 } 591 case 6: { 592 uint64_t out = 0x1234, v = pick(); 593 int rc = omega_gate_round_pages(v, &out); 594 REC("pages %d %" PRIu64, rc, out); 595 break; 596 } 597 case 7: { 598 uint32_t g = 0x1234, t = 0x5678; 599 uint64_t need = 0x9abc, n = pick(); 600 int rc = omega_gate_vec_shape(n, &g, &t, &need); 601 REC("vec %d %u %u %" PRIu64, rc, g, t, need); 602 break; 603 } 604 case 8: { 605 uint32_t w9 = below(2) ? 0x6u : pick32(), w15 = pick32(), w16 = pick32(), w17 = below(2) ? 0u : pick32(), 606 w19 = pick32(); 607 uint64_t va = pick(); 608 REC("qmd1 %d", omega_gate_qmd1_release(w9, w15, w16, w17, w19, va)); 609 break; 610 } 611 case 9: { 612 int env = (int)below(2), dev = (int)below(2), flt = (int)below(2), reuse = 0x55; 613 unsigned char e0 = below(2) ? '1' : (unsigned char)below(256); 614 int rc = omega_gate_persist(env, e0, dev, flt, &reuse); 615 REC("persist %d %d", rc, reuse); 616 break; 617 } 618 case 10: { 619 uint32_t t[4] = {1, 2, 3, 4}; 620 int rc = omega_gate_timeouts(t); 621 REC("toms %d %u %u %u %u", rc, t[0], t[1], t[2], t[3]); 622 break; 623 } 624 case 11: { 625 int ptrs = (int)below(2), code = (int)below(2); 626 uint64_t m = pick(), kk = pick(), n = pick(); 627 REC("mmargs %d", omega_gate_matmul_args(ptrs, code, m, kk, n)); 628 break; 629 } 630 case 12: { 631 uint32_t s[4] = {9, 9, 9, 9}; 632 int mma = (int)below(2); 633 uint64_t m = pick(), n = pick(); 634 int rc = omega_gate_matmul_shape(mma, m, n, s); 635 REC("mmshape %d %u %u %u %u", rc, s[0], s[1], s[2], s[3]); 636 break; 637 } 638 case 13: { 639 uint64_t p = below(2) ? below(4) : pick(), f = below(2) ? 1 : pick(), b = below(2) ? 2 : pick(); 640 REC("prec %d", omega_gate_precision(p, f, b)); 641 break; 642 } 643 case 14: { 644 uint32_t kk = below(8) ? (uint32_t)below(6) : pick32(); 645 uint64_t a = below(2) ? below(4) : pick(), b = below(2) ? 0x16000001u : pick(), c = below(2) ? 0x16000002u : pick(), 646 d = below(2) ? 0x46464646u : pick(); 647 int ex = 0x55; 648 int rc = omega_gate_conc(kk, a, b, c, d, &ex); 649 REC("conc %u %d %d", kk, rc, ex); 650 break; 651 } 652 default: { 653 /* op 10, the signed serial compare the waits use: om answer against the C expression of omega_gpu_wait.c */ 654 uint64_t a = pick32(), b = below(2) ? (uint32_t)(a + below(5) - 2u) : pick32(); 655 uint64_t in[2] = {a, b}, w[16]; 656 int ge; 657 if (eng_on()) ge = eng_ask(ENG_OP_SEQGE, in, 2, w) == 0 ? (int)w[0] : -1; 658 else ge = (int32_t)((uint32_t)a - (uint32_t)b) >= 0; 659 REC("seqge %d", ge); 660 break; 661 } 662 } 663 } 664 665 /* Deterministic lifecycle threshold pass. Random draws reach an exact threshold 666 * such as the 256 GiB churn floor too rarely, so every EDGE value is placed in 667 * every argument position of every predicate, over three base vectors. */ 668 static void trace_life_edges(void) { 669 static const uint64_t base[3] = {0, 1, 3}; 670 uint32_t kk; 671 size_t e, bi; 672 int p, i; 673 for (kk = 0; kk < 17; kk++) 674 for (bi = 0; bi < 3; bi++) 675 for (p = 0; p < 6; p++) 676 for (e = 0; e < sizeof EDGE / sizeof EDGE[0]; e++) { 677 uint64_t a[6]; 678 for (i = 0; i < 6; i++) a[i] = base[bi]; 679 a[p] = EDGE[e]; 680 REC("lifee %u %d", kk, omega_gate_life(kk, a[0], a[1], a[2], a[3], a[4], a[5])); 681 } 682 } 683 684 /* ---- compare mode ---- */ 685 static int compare(const char *pa, const char *pb) { 686 FILE *a = fopen(pa, "r"), *b = fopen(pb, "r"); 687 static char la[4096], lb[4096]; 688 unsigned long n = 0; 689 if (!a || !b) { fprintf(stderr, "FAIL cannot open transcripts\n"); return 2; } 690 for (;;) { 691 char *ra = fgets(la, sizeof la, a), *rb = fgets(lb, sizeof lb, b); 692 if (!ra && !rb) break; 693 if (!ra || !rb || strcmp(la, lb) != 0) { 694 printf("FAIL om eng diff: record %lu differs\n om: %s C: %s", n, ra ? la : "(end)\n", rb ? lb : "(end)\n"); 695 return 1; 696 } 697 n++; 698 } 699 printf("PASS om eng diff: %lu records identical (om vs C)\n", n); 700 return 0; 701 } 702 703 int main(int argc, char **argv) { 704 if (argc == 4 && strcmp(argv[1], "--compare") == 0) return compare(argv[2], argv[3]); 705 if (argc != 2) { fprintf(stderr, "usage: %s transcript | --compare om C\n", argv[0]); return 2; } 706 int fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600); 707 g_out = fd < 0 ? NULL : fdopen(fd, "w"); 708 if (!g_out && fd >= 0) close(fd); 709 if (!g_out) return 2; 710 snprintf(g_meminfo, sizeof g_meminfo, "/tmp/om_eng_diff_meminfo.%ld", (long)getpid()); 711 if (omega_gpu_platform_linux_test_set_meminfo(g_meminfo) != 0) return 2; 712 omega_gpu_engine_set_backend(&g_be); 713 omega_gpu_engine_test_set_clock(eng_now); 714 /* A zero timeout still samples the marker. Check the public wrapper and 715 * both sides of the serial half-range through the production binding. */ 716 static const struct { uint32_t observed, target; int expected; } markers[] = { 717 {7, 7, 0}, {0, UINT32_MAX, 0}, {UINT32_MAX, 0, -1}, 718 {0x7fffffffu, 0, 0}, {0x80000000u, 0, -1} 719 }; 720 uint64_t before = g_opn[ENG_OP_SEQGE]; 721 for (size_t j = 0; j < sizeof markers / sizeof markers[0]; j++) { 722 volatile uint32_t marker = markers[j].observed; 723 if (m16_native_wait_marker(&marker, markers[j].target, 0) != markers[j].expected) 724 return 1; 725 } 726 if (eng_on() && g_opn[ENG_OP_SEQGE] == before) { 727 fputs("M16 marker wait bypassed Omega serial ordering\n", stderr); 728 return 1; 729 } 730 int i; 731 for (i = 0; i < 3000; i++) trace_engine(); 732 for (i = 0; i < 2000; i++) trace_wait(0); 733 for (i = 0; i < 2000; i++) trace_wait(1); 734 for (i = 0; i < 1000; i++) trace_fclass(); 735 for (i = 0; i < 1000; i++) trace_release(); 736 for (i = 0; i < 1000; i++) trace_negotiate(); 737 for (i = 0; i < 1000; i++) trace_check(); 738 for (i = 0; i < 1000; i++) trace_buffer(); 739 for (i = 0; i < 2000; i++) trace_handles(i % 250 == 0); 740 for (i = 0; i < 1000; i++) trace_reserve(); 741 for (i = 0; i < 1000; i++) trace_cached(); 742 for (i = 0; i < 1000; i++) trace_serving(); 743 for (i = 0; i < 8000; i++) trace_gates(); 744 trace_life_edges(); 745 unlink(g_meminfo); 746 fclose(g_out); 747 unsigned missing = 0; 748 for (i = 0; i < (int)ENG_OP_COUNT; i++) { 749 fprintf(stderr, "op %2d calls %" PRIu64 "\n", i, g_opn[i]); 750 if (g_opn[i] == 0) missing++; 751 } 752 fprintf(stderr, "unknown-op calls %" PRIu64 "\n", g_opn[ENG_OP_COUNT]); 753 #if defined(ENG_EXPECT_C) 754 if (g_calls != 0) { printf("FAIL C build reached the unit %" PRIu64 " times\n", g_calls); return 1; } 755 printf("C build: %lu records, 0 unit calls\n", g_rec); 756 #else 757 if (missing) { printf("FAIL om build: %u ops never reached\n", missing); return 1; } 758 printf("om build: %lu records, %" PRIu64 " unit calls, all %u ops reached\n", g_rec, g_calls, ENG_OP_COUNT); 759 #endif 760 return 0; 761 }