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 / tests / gpu_fp8_slots_test.c
| 1 | /* gpu_fp8_slots_test: Expert Ribbon P1 (aienix#4 / omega#533), versioned replaceable FP8 expert slots. |
| 2 | * |
| 3 | * --host-only the slot state machine (omega_fp8_slot_table.c) without a device: publish only after a finished load, |
| 4 | * stale handles, double load, load/evict while pinned, ABA across reuse, failed loads, a threaded race |
| 5 | * (readers pin/unpin while a writer reloads; a reader must never observe its slot rewritten), mutant. |
| 6 | * --chip the pool on the GB10 (caller holds the GB10 lock): |
| 7 | * 1. parity: E random experts resident (one tensor) vs the same experts loaded into a smaller pool, |
| 8 | * grouped launch, y bit-identical, gate|up (1536x2048) and down (2048x768) shapes; |
| 9 | * 2. churn: --rounds reloads cycling every slot through every expert, parity after each reload; |
| 10 | * 3. refusals on the chip pool: load while pinned BUSY, pin of a replaced version STALE, entry unpinned; |
| 11 | * 4. reader fence through a recorded command list: the list's group table points at a pinned slot; a |
| 12 | * load into that slot before submit must be refused and the list must read the pinned expert; |
| 13 | * NEGATIVE CONTROL (--mutant): the pool ignores pins, the load goes through and the list reads the |
| 14 | * replaced weights, which the parity check must catch (exit 1 = caught); |
| 15 | * 5. memory: pool device bytes vs the session's driver allocation counter, pinned high-water. |
| 16 | * Exit 0 PASS, 1 FAIL (or, with --mutant, CAUGHT), 2 usage. Verdict line last. */ |
| 17 | #include "omega_fp8_slot_table.h" |
| 18 | #include "omega_gpu_fp8_slots.h" |
| 19 | #include "omega_gpu_fp8_api.h" |
| 20 | #include "omega_gpu_session.h" |
| 21 | #include "omega_gpu_cmdlist.h" |
| 22 | #include <inttypes.h> |
| 23 | #include <pthread.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <time.h> |
| 28 | |
| 29 | static int g_failed, g_checks; |
| 30 | #define CHECK(c, ...) do { g_checks++; if (c) { printf("ok "); } else { printf("FAIL "); g_failed++; } printf(__VA_ARGS__); printf("\n"); } while (0) |
| 31 | |
| 32 | static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; } |
| 33 | |
| 34 | /* ---------------- host-only: the state machine ---------------- */ |
| 35 | |
| 36 | /* The race must not depend on how the threads are scheduled (a 1-CPU taskset starved the writer; a loaded host hid the |
| 37 | * mutant). The writer publishes every reload attempt under m; a reader holds its pin until two attempts have finished |
| 38 | * after it pinned, so at least one begin_load runs entirely inside the pin, and pins again only after the writer |
| 39 | * published a reload, so pins always drain and the writer always gets through. Waits block on cv (no spinning or |
| 40 | * yielding), so a host with every CPU busy only slows the hand-offs, it cannot starve them. */ |
| 41 | typedef struct { |
| 42 | OmegaFp8SlotTable *t; uint64_t *shadow_version; |
| 43 | pthread_mutex_t m; pthread_cond_t cv; int stop; uint64_t attempts, reloads; /* guarded by m */ |
| 44 | uint64_t observed_rewrites, pins_ok; /* atomic */ |
| 45 | } Race; |
| 46 | |
| 47 | /* waits until *ctr >= target or stop; returns the value of *ctr seen */ |
| 48 | static uint64_t race_wait(Race *r, const uint64_t *ctr, uint64_t target) { |
| 49 | pthread_mutex_lock(&r->m); |
| 50 | while (*ctr < target && !r->stop) pthread_cond_wait(&r->cv, &r->m); |
| 51 | const uint64_t v = *ctr; |
| 52 | pthread_mutex_unlock(&r->m); |
| 53 | return v; |
| 54 | } |
| 55 | |
| 56 | static int race_stopped(Race *r) { |
| 57 | pthread_mutex_lock(&r->m); const int s = r->stop; pthread_mutex_unlock(&r->m); |
| 58 | return s; |
| 59 | } |
| 60 | |
| 61 | static void *reader(void *arg) { |
| 62 | Race *r = arg; |
| 63 | uint64_t after = 0; int pinned_once = 0; |
| 64 | while (!race_stopped(r)) { |
| 65 | if (pinned_once) race_wait(r, &r->reloads, after + 1); |
| 66 | OmegaFp8SlotRec rec; |
| 67 | if (omega_fp8_slot_query(r->t, 0, &rec) != 0) continue; |
| 68 | if (rec.state != OMEGA_FP8_SLOT_READY) continue; |
| 69 | if (omega_fp8_slot_pin(r->t, 0, rec.version) != 0) continue; |
| 70 | pinned_once = 1; |
| 71 | __atomic_fetch_add(&r->pins_ok, 1, __ATOMIC_RELAXED); |
| 72 | /* while pinned, the shadow (the slot bytes) must keep holding the content of the pinned key */ |
| 73 | uint64_t seen = __atomic_load_n(r->shadow_version, __ATOMIC_ACQUIRE) != rec.key; |
| 74 | const uint64_t a0 = race_wait(r, &r->attempts, 0); |
| 75 | race_wait(r, &r->attempts, a0 + 2); |
| 76 | seen += __atomic_load_n(r->shadow_version, __ATOMIC_ACQUIRE) != rec.key; |
| 77 | if (seen) __atomic_fetch_add(&r->observed_rewrites, seen, __ATOMIC_RELAXED); |
| 78 | after = race_wait(r, &r->reloads, 0); |
| 79 | (void)omega_fp8_slot_unpin(r->t, 0, rec.version); |
| 80 | } |
| 81 | return NULL; |
| 82 | } |
| 83 | |
| 84 | static double now_s(void) { struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); return (double)ts.tv_sec + ts.tv_nsec * 1e-9; } |
| 85 | |
| 86 | static int host_tests(void) { |
| 87 | OmegaFp8SlotTable t; |
| 88 | CHECK(omega_fp8_slot_table_init(&t, 4) == 0, "init 4 slots"); |
| 89 | uint64_t v1 = 0, v2 = 0, v3 = 0; |
| 90 | CHECK(omega_fp8_slot_pin(&t, 0, 1) == OMEGA_FP8_SLOT_STALE, "pin of a never-loaded slot is STALE"); |
| 91 | CHECK(omega_fp8_slot_begin_load(&t, 0) == 0, "begin load slot 0"); |
| 92 | CHECK(omega_fp8_slot_begin_load(&t, 0) == OMEGA_FP8_SLOT_BUSY, "double load refused BUSY"); |
| 93 | CHECK(omega_fp8_slot_pin(&t, 0, 1) == OMEGA_FP8_SLOT_STALE, "a LOADING slot cannot be pinned (not visible before publish)"); |
| 94 | CHECK(omega_fp8_slot_finish_load(&t, 0, 100, 1, &v1) == 0 && v1 == 1, "publish -> version 1"); |
| 95 | CHECK(omega_fp8_slot_finish_load(&t, 0, 100, 1, &v2) == OMEGA_FP8_SLOT_BAD_ARGS && v2 == 0, "finish without begin refused"); |
| 96 | CHECK(omega_fp8_slot_pin(&t, 0, v1) == 0, "pin v1"); |
| 97 | CHECK(omega_fp8_slot_begin_load(&t, 0) == OMEGA_FP8_SLOT_BUSY, "reload while pinned refused BUSY"); |
| 98 | CHECK(omega_fp8_slot_evict(&t, 0) == OMEGA_FP8_SLOT_BUSY, "evict while pinned refused BUSY"); |
| 99 | CHECK(omega_fp8_slot_unpin(&t, 0, v1 + 7) == OMEGA_FP8_SLOT_BAD_ARGS, "unpin with a wrong version refused"); |
| 100 | CHECK(omega_fp8_slot_unpin(&t, 0, v1) == 0, "unpin v1"); |
| 101 | CHECK(omega_fp8_slot_unpin(&t, 0, v1) == OMEGA_FP8_SLOT_BAD_ARGS, "unpin without a pin refused"); |
| 102 | CHECK(omega_fp8_slot_begin_load(&t, 0) == 0 && omega_fp8_slot_finish_load(&t, 0, 200, 1, &v2) == 0 && v2 > v1, "reload -> newer version"); |
| 103 | CHECK(omega_fp8_slot_pin(&t, 0, v1) == OMEGA_FP8_SLOT_STALE, "stale handle (old version) refused after reload"); |
| 104 | /* ABA: slot 0 holds key 200; load key 100 again: the version must differ from v1 */ |
| 105 | CHECK(omega_fp8_slot_begin_load(&t, 0) == 0 && omega_fp8_slot_finish_load(&t, 0, 100, 1, &v3) == 0 && v3 != v1 && v3 > v2, |
| 106 | "same key reloaded gets a fresh version (no ABA): v1=%" PRIu64 " v3=%" PRIu64, v1, v3); |
| 107 | CHECK(omega_fp8_slot_begin_load(&t, 1) == 0 && omega_fp8_slot_finish_load(&t, 1, 5, 0, &v2) == 0 && v2 == 0, "failed load publishes nothing"); |
| 108 | OmegaFp8SlotRec rec; |
| 109 | CHECK(omega_fp8_slot_query(&t, 1, &rec) == 0 && rec.state == OMEGA_FP8_SLOT_EMPTY, "failed load leaves the slot EMPTY"); |
| 110 | CHECK(omega_fp8_slot_evict(&t, 0) == 0 && omega_fp8_slot_pin(&t, 0, v3) == OMEGA_FP8_SLOT_STALE, "evicted slot cannot be pinned"); |
| 111 | CHECK(omega_fp8_slot_begin_load(&t, 9) == OMEGA_FP8_SLOT_BAD_ARGS, "out-of-range slot refused"); |
| 112 | /* repeated evict/reinsert: versions strictly increase */ |
| 113 | uint64_t last = v3; int mono = 1; |
| 114 | for (int i = 0; i < 1000; i++) { |
| 115 | uint64_t v = 0; |
| 116 | if (omega_fp8_slot_begin_load(&t, (uint32_t)(i % 4)) != 0 || omega_fp8_slot_finish_load(&t, (uint32_t)(i % 4), (uint64_t)i, 1, &v) != 0 || v <= last) mono = 0; |
| 117 | last = v; |
| 118 | if (i % 3 == 0 && omega_fp8_slot_evict(&t, (uint32_t)(i % 4)) != 0) mono = 0; |
| 119 | } |
| 120 | CHECK(mono, "1000 evict/reinsert cycles: versions strictly increase"); |
| 121 | CHECK(omega_fp8_slot_table_idle(&t), "idle when nothing is pinned or loading"); |
| 122 | omega_fp8_slot_table_fini(&t); |
| 123 | |
| 124 | /* threaded race: 4 readers pin slot 0 at its current version; one writer reloads it as fast as it can. The shadow |
| 125 | * stands for the slot's bytes: the writer bumps it between begin_load and finish_load (while the slot is hidden). |
| 126 | * The writer runs until RELOADS reloads and PINS reader pins happened (60 s cap, which only a hung table reaches). */ |
| 127 | enum { RELOADS = 10000, PINS = 1000 }; |
| 128 | for (int mutant = 0; mutant <= 1; mutant++) { |
| 129 | OmegaFp8SlotTable rt; omega_fp8_slot_table_init(&rt, 1); |
| 130 | omega_fp8_slot_table_set_mutant(&rt, mutant); |
| 131 | uint64_t shadow = 0, v = 0; |
| 132 | omega_fp8_slot_begin_load(&rt, 0); omega_fp8_slot_finish_load(&rt, 0, 0, 1, &v); |
| 133 | __atomic_store_n(&shadow, 0, __ATOMIC_RELEASE); |
| 134 | Race r = { .t = &rt, .shadow_version = &shadow, .m = PTHREAD_MUTEX_INITIALIZER, .cv = PTHREAD_COND_INITIALIZER }; |
| 135 | pthread_t th[4]; |
| 136 | for (int i = 0; i < 4; i++) pthread_create(&th[i], NULL, reader, &r); |
| 137 | uint64_t reloads = 0, busy = 0; |
| 138 | const double t0 = now_s(); |
| 139 | for (uint64_t i = 0; reloads < RELOADS || __atomic_load_n(&r.pins_ok, __ATOMIC_RELAXED) < PINS; i++) { |
| 140 | if ((i & 1023) == 0 && now_s() - t0 > 60.0) break; |
| 141 | const int rc = omega_fp8_slot_begin_load(&rt, 0); |
| 142 | if (rc == 0) { |
| 143 | __atomic_store_n(&shadow, 0xDEADull << 32, __ATOMIC_RELEASE); /* "bytes" half-written */ |
| 144 | __atomic_store_n(&shadow, i + 1, __ATOMIC_RELEASE); /* "bytes" complete: the content of key i + 1 */ |
| 145 | uint64_t nv = 0; omega_fp8_slot_finish_load(&rt, 0, i + 1, 1, &nv); |
| 146 | reloads++; |
| 147 | } else busy++; |
| 148 | pthread_mutex_lock(&r.m); |
| 149 | r.attempts = i + 1; r.reloads = reloads; |
| 150 | pthread_cond_broadcast(&r.cv); |
| 151 | pthread_mutex_unlock(&r.m); |
| 152 | } |
| 153 | pthread_mutex_lock(&r.m); r.stop = 1; pthread_cond_broadcast(&r.cv); pthread_mutex_unlock(&r.m); |
| 154 | for (int i = 0; i < 4; i++) pthread_join(th[i], NULL); |
| 155 | const int ran = reloads >= RELOADS && r.pins_ok >= PINS; |
| 156 | if (!mutant) |
| 157 | CHECK(ran && r.observed_rewrites == 0, |
| 158 | "race: %" PRIu64 " reloads, %" PRIu64 " busy refusals, %" PRIu64 " reader pins, rewrites seen under a pin = %" PRIu64, |
| 159 | reloads, busy, r.pins_ok, r.observed_rewrites); |
| 160 | else |
| 161 | CHECK(ran && r.observed_rewrites > 0, |
| 162 | "mutant (ignores pins): %" PRIu64 " reloads, %" PRIu64 " reader pins, rewrites seen under a pin = %" PRIu64 " (the race check has teeth)", |
| 163 | reloads, r.pins_ok, r.observed_rewrites); |
| 164 | omega_fp8_slot_table_fini(&rt); |
| 165 | pthread_cond_destroy(&r.cv); pthread_mutex_destroy(&r.m); |
| 166 | } |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | /* ---------------- chip ---------------- */ |
| 171 | |
| 172 | static void fill_expert(uint32_t seed, uint32_t n, uint32_t k, uint8_t *w, uint16_t *s) { |
| 173 | uint32_t st = seed * 2654435761u + 7u; |
| 174 | for (size_t i = 0; i < (size_t)n * k; i++) { uint8_t b = (uint8_t)(lcg(&st) >> 24); w[i] = (b & 0x7F) == 0x7F ? 0x3C : b; } |
| 175 | for (size_t i = 0; i < (size_t)(n / 128) * (k / 128); i++) s[i] = (uint16_t)(0x3B00u + (lcg(&st) >> 24) % 0x200u); /* bf16 ~2^-9..2^-7 */ |
| 176 | } |
| 177 | |
| 178 | static int chip_shape(uint32_t n, uint32_t k, uint32_t E, uint32_t S, uint32_t rounds, int mutant) { |
| 179 | printf("== shape n=%u k=%u, %u resident experts, pool of %u slots, %u churn rounds%s\n", n, k, E, S, rounds, mutant ? " (MUTANT)" : ""); |
| 180 | const size_t wb = (size_t)n * k, sc = (size_t)(n / 128) * (k / 128); |
| 181 | uint8_t *w = malloc(wb * E); uint16_t *s = malloc(sc * 2 * E); |
| 182 | float *x = malloc((size_t)k * 4 * 8), *yr = malloc((size_t)n * 4 * 8), *yp = malloc((size_t)n * 4 * 8); |
| 183 | if (!w || !s || !x || !yr || !yp) return 1; |
| 184 | for (uint32_t e = 0; e < E; e++) fill_expert(e + n, n, k, w + wb * e, s + sc * e); |
| 185 | uint32_t st = 99; |
| 186 | for (uint32_t i = 0; i < k * 8; i++) x[i] = (float)(lcg(&st) >> 8) / 16777216.0f * 2.0f - 1.0f; |
| 187 | OmegaGpuFp8Tensor *res = NULL; |
| 188 | int rc = omega_gpu_fp8_tensor_upload(E, n, k, w, s, &res); |
| 189 | if (rc != 0) { printf("FAIL resident upload: %s (%s)\n", omega_gpu_fp8_rc_name(rc), omega_gpu_fp8_last_error()); return 1; } |
| 190 | OmegaGpuAllocStats a0; omega_gpu_session_alloc_stats(&a0); |
| 191 | OmegaGpuFp8Slots *p = NULL; |
| 192 | rc = omega_gpu_fp8_slots_alloc(S, n, k, &p); |
| 193 | OmegaGpuAllocStats a1; omega_gpu_session_alloc_stats(&a1); |
| 194 | if (rc != 0) { printf("FAIL pool alloc: %s (%s)\n", omega_gpu_fp8_rc_name(rc), omega_gpu_fp8_last_error()); return 1; } |
| 195 | OmegaGpuFp8SlotsStats ps; omega_gpu_fp8_slots_stats(p, &ps); |
| 196 | CHECK(a1.alloc_bytes - a0.alloc_bytes >= ps.device_bytes && a1.alloc_bytes - a0.alloc_bytes <= ps.device_bytes + 2u * 65536u, |
| 197 | "pool driver bytes %" PRIu64 " for %" PRIu64 " payload bytes (page rounding only)", a1.alloc_bytes - a0.alloc_bytes, ps.device_bytes); |
| 198 | uint64_t *ver = calloc(S, 8); uint32_t *held = calloc(S, 4); |
| 199 | for (uint32_t sl = 0; sl < S; sl++) { |
| 200 | held[sl] = sl % E; |
| 201 | rc = omega_gpu_fp8_slots_load(p, sl, held[sl], w + wb * held[sl], s + sc * held[sl], &ver[sl]); |
| 202 | if (rc != 0) { printf("FAIL load slot %u: %s\n", sl, omega_gpu_fp8_slots_rc_name(rc)); return 1; } |
| 203 | } |
| 204 | /* 1 + 2: parity after every reload; groups = min(S, 8) slots per launch, shared x row (gate|up) or a row per group (down) */ |
| 205 | const uint32_t G = S < 8 ? S : 8; |
| 206 | uint32_t mism = 0, launches = 0; |
| 207 | for (uint32_t r = 0; r <= rounds && !mutant; r++) { |
| 208 | if (r > 0) { /* reload one slot with the next expert in the cycle */ |
| 209 | uint32_t sl = (r - 1) % S; held[sl] = (held[sl] + 1 + (r % 3)) % E; |
| 210 | rc = omega_gpu_fp8_slots_load(p, sl, held[sl], w + wb * held[sl], s + sc * held[sl], &ver[sl]); |
| 211 | if (rc != 0) { printf("FAIL reload r%u: %s\n", r, omega_gpu_fp8_slots_rc_name(rc)); mism++; break; } |
| 212 | } |
| 213 | uint32_t sls[8], ids[8]; uint64_t vs[8]; |
| 214 | for (uint32_t g = 0; g < G; g++) { sls[g] = (r + g) % S; vs[g] = ver[sls[g]]; ids[g] = held[sls[g]]; } |
| 215 | const uint32_t xr = (r & 1) ? G : 1; |
| 216 | int a = omega_gpu_fp8_gemv_grouped_f32(res, G, ids, x, xr, yr, NULL); |
| 217 | int b = omega_gpu_fp8_slots_gemv_grouped_f32(p, G, sls, vs, x, xr, yp, NULL); |
| 218 | launches += 2; |
| 219 | if (a != 0 || b != 0) { printf("FAIL launch r%u: resident %s pool %s (%s)\n", r, omega_gpu_fp8_rc_name(a), omega_gpu_fp8_slots_rc_name(b), omega_gpu_fp8_last_error()); mism++; break; } |
| 220 | if (memcmp(yr, yp, (size_t)G * n * 4) != 0) mism++; |
| 221 | } |
| 222 | if (!mutant) CHECK(mism == 0, "parity: %u launches, pool y bit-identical to the resident tensor after every reload", launches); |
| 223 | |
| 224 | /* 3: refusals on the chip pool */ |
| 225 | if (!mutant) { |
| 226 | CHECK(omega_gpu_fp8_slots_pin(p, 0, ver[0]) == 0, "pin slot 0"); |
| 227 | uint64_t vx = 0; |
| 228 | CHECK(omega_gpu_fp8_slots_load(p, 0, 7, w, s, &vx) == OMEGA_GPU_FP8_SLOTS_BUSY && vx == 0, "load into a pinned slot refused BUSY"); |
| 229 | CHECK(omega_gpu_fp8_slots_evict(p, 0) == OMEGA_GPU_FP8_SLOTS_BUSY, "evict of a pinned slot refused BUSY"); |
| 230 | omega_gpu_fp8_slots_unpin(p, 0, ver[0]); |
| 231 | uint64_t wva, sva; |
| 232 | CHECK(omega_gpu_fp8_slots_entry(p, 0, ver[0], &wva, &sva) == OMEGA_GPU_FP8_BAD_ARGS, "entry of an unpinned slot refused"); |
| 233 | uint64_t old = ver[0]; |
| 234 | CHECK(omega_gpu_fp8_slots_load(p, 0, held[0], w + wb * held[0], s + sc * held[0], &ver[0]) == 0 && ver[0] > old, "reload slot 0 -> newer version"); |
| 235 | uint32_t sl0 = 0; |
| 236 | CHECK(omega_gpu_fp8_slots_gemv_grouped_f32(p, 1, &sl0, &old, x, 1, yp, NULL) == OMEGA_GPU_FP8_SLOTS_STALE, "launch with the replaced version refused STALE (nothing launched)"); |
| 237 | uint8_t *bad = malloc(wb); memcpy(bad, w, wb); bad[wb / 2] = 0x7F; |
| 238 | uint64_t vb = 1; |
| 239 | CHECK(omega_gpu_fp8_slots_load(p, 1, 9, bad, s, &vb) == OMEGA_GPU_FP8_NAN_WEIGHT && vb == 0, "NaN weight refused"); |
| 240 | OmegaGpuFp8SlotInfo inf; omega_gpu_fp8_slots_query(p, 1, &inf); |
| 241 | CHECK(inf.state == 0 && omega_gpu_fp8_slots_pin(p, 1, ver[1]) == OMEGA_GPU_FP8_SLOTS_STALE, "refused load leaves the slot EMPTY; its old version cannot be pinned"); |
| 242 | free(bad); |
| 243 | rc = omega_gpu_fp8_slots_load(p, 1, held[1], w + wb * held[1], s + sc * held[1], &ver[1]); |
| 244 | if (rc != 0) g_failed++; |
| 245 | } |
| 246 | |
| 247 | /* 4: reader fence through a recorded command list */ |
| 248 | { |
| 249 | if (mutant) omega_gpu_fp8_slots_test_set_mutant(p, 1); |
| 250 | const uint32_t A = held[0], B = (held[0] + 1) % E; |
| 251 | OmegaGpuCmdList *cl = NULL; OmegaGpuMem tab, xd, yd; |
| 252 | if (omega_gpu_cmdlist_create(4, &cl) != 0) { printf("FAIL cmdlist create: %s\n", omega_gpu_session_last_error()); return 1; } |
| 253 | omega_gpu_session_lock(); |
| 254 | int arc = omega_gpu_session_alloc(16u, &tab) | omega_gpu_session_alloc((size_t)k * 4u, &xd) | omega_gpu_session_alloc((size_t)n * 4u, &yd); |
| 255 | omega_gpu_session_unlock(); |
| 256 | if (arc) { printf("FAIL alloc: %s\n", omega_gpu_session_last_error()); return 1; } |
| 257 | omega_fp8_pack_x_f32(x, k, xd.cpu); |
| 258 | float *y = yd.cpu; for (uint32_t i = 0; i < n; i++) y[i] = -999.0f; |
| 259 | /* reference for A and B on the resident tensor */ |
| 260 | float *ya = malloc((size_t)n * 4), *yb = malloc((size_t)n * 4); |
| 261 | int r1 = omega_gpu_fp8_gemv_grouped_f32(res, 1, &A, x, 1, ya, NULL), r2 = omega_gpu_fp8_gemv_grouped_f32(res, 1, &B, x, 1, yb, NULL); |
| 262 | CHECK(r1 == 0 && r2 == 0 && memcmp(ya, yb, (size_t)n * 4) != 0, "references: experts %u and %u give different y", A, B); |
| 263 | /* warm the kernel (grouped 1, x_rows 1) through the pool before recording */ |
| 264 | uint32_t sl0 = 0; float *yw = malloc((size_t)n * 4); |
| 265 | (void)omega_gpu_fp8_slots_gemv_grouped_f32(p, 1, &sl0, &ver[0], x, 1, yw, NULL); |
| 266 | int ok = omega_gpu_fp8_slots_pin(p, 0, ver[0]) == 0; |
| 267 | uint64_t *tw = tab.cpu; |
| 268 | ok = ok && omega_gpu_fp8_slots_entry(p, 0, ver[0], &tw[0], &tw[1]) == 0; |
| 269 | ok = ok && omega_gpu_session_sync_to_gpu(&tab) == 0 && omega_gpu_session_sync_to_gpu(&xd) == 0 && omega_gpu_session_sync_to_gpu(&yd) == 0; |
| 270 | omega_gpu_session_lock(); |
| 271 | omega_gpu_cmdlist_reset(cl); |
| 272 | ok = ok && omega_gpu_fp8_gemv_grouped_record(cl, omega_gpu_fp8_slots_tensor(p), 1, tab.va, xd.va, 1, yd.va) == 0; |
| 273 | omega_gpu_session_unlock(); |
| 274 | /* the replacement attempt: between record and submit (the list may read the slot from here until submit returns) */ |
| 275 | uint64_t vnew = 0; |
| 276 | int lrc = omega_gpu_fp8_slots_load(p, 0, B, w + wb * B, s + sc * B, &vnew); |
| 277 | if (!mutant) CHECK(lrc == OMEGA_GPU_FP8_SLOTS_BUSY, "load into the slot a recorded list reads: refused BUSY"); |
| 278 | else printf("info mutant: load into the pinned slot returned %s\n", omega_gpu_fp8_slots_rc_name(lrc)); |
| 279 | OmegaGpuCmdListOpts o; memset(&o, 0, sizeof o); o.mode = OMEGA_GPU_CMDLIST_PCAS; |
| 280 | OmegaGpuCmdListStats cs; memset(&cs, 0, sizeof cs); |
| 281 | omega_gpu_session_lock(); |
| 282 | ok = ok && omega_gpu_cmdlist_submit(cl, &o, &cs) == 0; |
| 283 | omega_gpu_session_unlock(); |
| 284 | ok = ok && omega_gpu_session_sync_to_cpu(&yd) == 0; |
| 285 | (void)omega_gpu_fp8_slots_unpin(p, 0, ver[0]); |
| 286 | const int same_a = ok && memcmp(y, ya, (size_t)n * 4) == 0, same_b = ok && memcmp(y, yb, (size_t)n * 4) == 0; |
| 287 | if (!mutant) CHECK(ok && same_a, "the list read the pinned expert %u bit-exactly (submit ok=%d)", A, ok); |
| 288 | else { |
| 289 | CHECK(ok && same_a, "NEGATIVE CONTROL: list output equals pinned expert %u (expected to FAIL under the mutant; read B=%d)", A, same_b); |
| 290 | } |
| 291 | free(ya); free(yb); free(yw); |
| 292 | omega_gpu_session_lock(); omega_gpu_session_free(&tab); omega_gpu_session_free(&xd); omega_gpu_session_free(&yd); omega_gpu_session_unlock(); |
| 293 | omega_gpu_cmdlist_free(cl); |
| 294 | if (mutant) omega_gpu_fp8_slots_test_set_mutant(p, 0); |
| 295 | } |
| 296 | |
| 297 | omega_gpu_fp8_slots_stats(p, &ps); |
| 298 | printf("stats slots=%u device_bytes=%" PRIu64 " loads=%" PRIu64 " load_failures=%" PRIu64 " busy=%" PRIu64 " stale=%" PRIu64 |
| 299 | " pins=%" PRIu64 " pinned_now=%u pinned_high=%u bytes_loaded=%" PRIu64 " load_ns=%" PRIu64 " load_GBps=%.3f\n", |
| 300 | ps.slots, ps.device_bytes, ps.loads, ps.load_failures, ps.busy_refusals, ps.stale_refusals, ps.pins, ps.pinned_now, |
| 301 | ps.pinned_high, ps.bytes_loaded, ps.load_ns, ps.load_ns ? (double)ps.bytes_loaded / (double)ps.load_ns : 0.0); |
| 302 | CHECK(ps.pinned_now == 0 && ps.pinned_high <= G, "no pin leaked (pinned_now 0, high-water %u <= %u)", ps.pinned_high, G); |
| 303 | OmegaGpuAllocStats a2; omega_gpu_session_alloc_stats(&a2); |
| 304 | CHECK(omega_gpu_fp8_slots_free(p) == 0, "pool freed"); |
| 305 | OmegaGpuAllocStats a3; omega_gpu_session_alloc_stats(&a3); |
| 306 | CHECK(a3.frees >= a2.frees + 2, "pool returned its 2 driver buffers (frees %" PRIu64 " -> %" PRIu64 ")", a2.frees, a3.frees); |
| 307 | omega_gpu_fp8_tensor_free(res); |
| 308 | free(w); free(s); free(x); free(yr); free(yp); free(ver); free(held); |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | int main(int argc, char **argv) { |
| 313 | int host = 0, chip = 0, mutant = 0; uint32_t rounds = 64; |
| 314 | for (int i = 1; i < argc; i++) { |
| 315 | if (!strcmp(argv[i], "--host-only")) host = 1; |
| 316 | else if (!strcmp(argv[i], "--chip")) chip = 1; |
| 317 | else if (!strcmp(argv[i], "--mutant")) mutant = 1; |
| 318 | else if (!strcmp(argv[i], "--rounds") && i + 1 < argc) rounds = (uint32_t)atoi(argv[++i]); |
| 319 | else { printf("usage: %s --host-only | --chip [--rounds R] [--mutant]\n", argv[0]); return 2; } |
| 320 | } |
| 321 | if (!host && !chip) { printf("usage: %s --host-only | --chip [--rounds R] [--mutant]\n", argv[0]); return 2; } |
| 322 | if (host) host_tests(); |
| 323 | if (chip) { |
| 324 | (void)omega_gpu_session_set_spin_us(2000); |
| 325 | chip_shape(1536, 2048, 12, 5, rounds, mutant); /* gate|up */ |
| 326 | if (!mutant) chip_shape(2048, 768, 12, 5, rounds, 0); /* down */ |
| 327 | if (omega_gpu_session_is_blocked()) { printf("FAIL session latched\n"); g_failed++; } |
| 328 | } |
| 329 | if (mutant) { |
| 330 | printf("GPU_FP8_SLOTS_NEGATIVE_CONTROL: %s (%d of %d checks failed)\n", g_failed ? "CAUGHT" : "NOT CAUGHT", g_failed, g_checks); |
| 331 | return g_failed ? 1 : 0; |
| 332 | } |
| 333 | printf("GPU_FP8_SLOTS: %s (%d checks, %d failed)\n", g_failed ? "FAIL" : "PASS", g_checks, g_failed); |
| 334 | return g_failed ? 1 : 0; |
| 335 | } |