interplane.Get Omega
Read in your language

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

branch main Branches Tags History

CUDApp / tests / gpu_fp8_slots_test.c

352 lines · 23150 bytes Raw Blame History
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
29static 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
32static 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. */
41typedef 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 */
48static 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
56static 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
61static 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
84static double now_s(void) { struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); return (double)ts.tv_sec + ts.tv_nsec * 1e-9; }
85
86static 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 /* uint32 wrap: pins and pinned_now are uint32_t, so a pin at pinned_now = 0xffffffff wraps it to 0 and leaves
125 * pinned_high alone (src/omega_fp8_slot_table.c at f1b50cbf). Before the apish_slot.om fix the Omega unit
126 * compared in 64 bits and stored pinned_high = 2^32 truncated to 0. */
127 {
128 OmegaFp8SlotTable w; uint64_t vw = 0;
129 CHECK(omega_fp8_slot_table_init(&w, 1) == 0 && omega_fp8_slot_begin_load(&w, 0) == 0 &&
130 omega_fp8_slot_finish_load(&w, 0, 7, 1, &vw) == 0, "wrap table loaded");
131 w.rec[0].pins = 0xffffffffu; w.stats.pinned_now = 0xffffffffu; w.stats.pinned_high = 1;
132 OmegaFp8SlotStats ws;
133 CHECK(omega_fp8_slot_pin(&w, 0, vw) == 0, "pin at pinned_now 0xffffffff");
134 omega_fp8_slot_table_stats(&w, &ws);
135 CHECK(w.rec[0].pins == 0 && ws.pinned_now == 0 && ws.pinned_high == 1,
136 "uint32 wrap: pins %u pinned_now %u pinned_high %u (want 0 0 1)", w.rec[0].pins, ws.pinned_now, ws.pinned_high);
137 w.rec[0].pins = 0; w.stats.pinned_now = 0;
138 omega_fp8_slot_table_fini(&w);
139 }
140
141 /* threaded race: 4 readers pin slot 0 at its current version; one writer reloads it as fast as it can. The shadow
142 * stands for the slot's bytes: the writer bumps it between begin_load and finish_load (while the slot is hidden).
143 * The writer runs until RELOADS reloads and PINS reader pins happened (60 s cap, which only a hung table reaches). */
144 enum { RELOADS = 10000, PINS = 1000 };
145 for (int mutant = 0; mutant <= 1; mutant++) {
146 OmegaFp8SlotTable rt; omega_fp8_slot_table_init(&rt, 1);
147 omega_fp8_slot_table_set_mutant(&rt, mutant);
148 uint64_t shadow = 0, v = 0;
149 omega_fp8_slot_begin_load(&rt, 0); omega_fp8_slot_finish_load(&rt, 0, 0, 1, &v);
150 __atomic_store_n(&shadow, 0, __ATOMIC_RELEASE);
151 Race r = { .t = &rt, .shadow_version = &shadow, .m = PTHREAD_MUTEX_INITIALIZER, .cv = PTHREAD_COND_INITIALIZER };
152 pthread_t th[4];
153 for (int i = 0; i < 4; i++) pthread_create(&th[i], NULL, reader, &r);
154 uint64_t reloads = 0, busy = 0;
155 const double t0 = now_s();
156 for (uint64_t i = 0; reloads < RELOADS || __atomic_load_n(&r.pins_ok, __ATOMIC_RELAXED) < PINS; i++) {
157 if ((i & 1023) == 0 && now_s() - t0 > 60.0) break;
158 const int rc = omega_fp8_slot_begin_load(&rt, 0);
159 if (rc == 0) {
160 __atomic_store_n(&shadow, 0xDEADull << 32, __ATOMIC_RELEASE); /* "bytes" half-written */
161 __atomic_store_n(&shadow, i + 1, __ATOMIC_RELEASE); /* "bytes" complete: the content of key i + 1 */
162 uint64_t nv = 0; omega_fp8_slot_finish_load(&rt, 0, i + 1, 1, &nv);
163 reloads++;
164 } else busy++;
165 pthread_mutex_lock(&r.m);
166 r.attempts = i + 1; r.reloads = reloads;
167 pthread_cond_broadcast(&r.cv);
168 pthread_mutex_unlock(&r.m);
169 }
170 pthread_mutex_lock(&r.m); r.stop = 1; pthread_cond_broadcast(&r.cv); pthread_mutex_unlock(&r.m);
171 for (int i = 0; i < 4; i++) pthread_join(th[i], NULL);
172 const int ran = reloads >= RELOADS && r.pins_ok >= PINS;
173 if (!mutant)
174 CHECK(ran && r.observed_rewrites == 0,
175 "race: %" PRIu64 " reloads, %" PRIu64 " busy refusals, %" PRIu64 " reader pins, rewrites seen under a pin = %" PRIu64,
176 reloads, busy, r.pins_ok, r.observed_rewrites);
177 else
178 CHECK(ran && r.observed_rewrites > 0,
179 "mutant (ignores pins): %" PRIu64 " reloads, %" PRIu64 " reader pins, rewrites seen under a pin = %" PRIu64 " (the race check has teeth)",
180 reloads, r.pins_ok, r.observed_rewrites);
181 omega_fp8_slot_table_fini(&rt);
182 pthread_cond_destroy(&r.cv); pthread_mutex_destroy(&r.m);
183 }
184 return 0;
185}
186
187/* ---------------- chip ---------------- */
188
189static void fill_expert(uint32_t seed, uint32_t n, uint32_t k, uint8_t *w, uint16_t *s) {
190 uint32_t st = seed * 2654435761u + 7u;
191 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; }
192 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 */
193}
194
195static int chip_shape(uint32_t n, uint32_t k, uint32_t E, uint32_t S, uint32_t rounds, int mutant) {
196 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)" : "");
197 const size_t wb = (size_t)n * k, sc = (size_t)(n / 128) * (k / 128);
198 uint8_t *w = malloc(wb * E); uint16_t *s = malloc(sc * 2 * E);
199 float *x = malloc((size_t)k * 4 * 8), *yr = malloc((size_t)n * 4 * 8), *yp = malloc((size_t)n * 4 * 8);
200 if (!w || !s || !x || !yr || !yp) return 1;
201 for (uint32_t e = 0; e < E; e++) fill_expert(e + n, n, k, w + wb * e, s + sc * e);
202 uint32_t st = 99;
203 for (uint32_t i = 0; i < k * 8; i++) x[i] = (float)(lcg(&st) >> 8) / 16777216.0f * 2.0f - 1.0f;
204 OmegaGpuFp8Tensor *res = NULL;
205 int rc = omega_gpu_fp8_tensor_upload(E, n, k, w, s, &res);
206 if (rc != 0) { printf("FAIL resident upload: %s (%s)\n", omega_gpu_fp8_rc_name(rc), omega_gpu_fp8_last_error()); return 1; }
207 OmegaGpuAllocStats a0; omega_gpu_session_alloc_stats(&a0);
208 OmegaGpuFp8Slots *p = NULL;
209 rc = omega_gpu_fp8_slots_alloc(S, n, k, &p);
210 OmegaGpuAllocStats a1; omega_gpu_session_alloc_stats(&a1);
211 if (rc != 0) { printf("FAIL pool alloc: %s (%s)\n", omega_gpu_fp8_rc_name(rc), omega_gpu_fp8_last_error()); return 1; }
212 OmegaGpuFp8SlotsStats ps; omega_gpu_fp8_slots_stats(p, &ps);
213 CHECK(a1.alloc_bytes - a0.alloc_bytes >= ps.device_bytes && a1.alloc_bytes - a0.alloc_bytes <= ps.device_bytes + 2u * 65536u,
214 "pool driver bytes %" PRIu64 " for %" PRIu64 " payload bytes (page rounding only)", a1.alloc_bytes - a0.alloc_bytes, ps.device_bytes);
215 uint64_t *ver = calloc(S, 8); uint32_t *held = calloc(S, 4);
216 for (uint32_t sl = 0; sl < S; sl++) {
217 held[sl] = sl % E;
218 rc = omega_gpu_fp8_slots_load(p, sl, held[sl], w + wb * held[sl], s + sc * held[sl], &ver[sl]);
219 if (rc != 0) { printf("FAIL load slot %u: %s\n", sl, omega_gpu_fp8_slots_rc_name(rc)); return 1; }
220 }
221 /* 1 + 2: parity after every reload; groups = min(S, 8) slots per launch, shared x row (gate|up) or a row per group (down) */
222 const uint32_t G = S < 8 ? S : 8;
223 uint32_t mism = 0, launches = 0;
224 for (uint32_t r = 0; r <= rounds && !mutant; r++) {
225 if (r > 0) { /* reload one slot with the next expert in the cycle */
226 uint32_t sl = (r - 1) % S; held[sl] = (held[sl] + 1 + (r % 3)) % E;
227 rc = omega_gpu_fp8_slots_load(p, sl, held[sl], w + wb * held[sl], s + sc * held[sl], &ver[sl]);
228 if (rc != 0) { printf("FAIL reload r%u: %s\n", r, omega_gpu_fp8_slots_rc_name(rc)); mism++; break; }
229 }
230 uint32_t sls[8], ids[8]; uint64_t vs[8];
231 for (uint32_t g = 0; g < G; g++) { sls[g] = (r + g) % S; vs[g] = ver[sls[g]]; ids[g] = held[sls[g]]; }
232 const uint32_t xr = (r & 1) ? G : 1;
233 int a = omega_gpu_fp8_gemv_grouped_f32(res, G, ids, x, xr, yr, NULL);
234 int b = omega_gpu_fp8_slots_gemv_grouped_f32(p, G, sls, vs, x, xr, yp, NULL);
235 launches += 2;
236 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; }
237 if (memcmp(yr, yp, (size_t)G * n * 4) != 0) mism++;
238 }
239 if (!mutant) CHECK(mism == 0, "parity: %u launches, pool y bit-identical to the resident tensor after every reload", launches);
240
241 /* 3: refusals on the chip pool */
242 if (!mutant) {
243 CHECK(omega_gpu_fp8_slots_pin(p, 0, ver[0]) == 0, "pin slot 0");
244 uint64_t vx = 0;
245 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");
246 CHECK(omega_gpu_fp8_slots_evict(p, 0) == OMEGA_GPU_FP8_SLOTS_BUSY, "evict of a pinned slot refused BUSY");
247 omega_gpu_fp8_slots_unpin(p, 0, ver[0]);
248 uint64_t wva, sva;
249 CHECK(omega_gpu_fp8_slots_entry(p, 0, ver[0], &wva, &sva) == OMEGA_GPU_FP8_BAD_ARGS, "entry of an unpinned slot refused");
250 uint64_t old = ver[0];
251 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");
252 uint32_t sl0 = 0;
253 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)");
254 uint8_t *bad = malloc(wb); memcpy(bad, w, wb); bad[wb / 2] = 0x7F;
255 uint64_t vb = 1;
256 CHECK(omega_gpu_fp8_slots_load(p, 1, 9, bad, s, &vb) == OMEGA_GPU_FP8_NAN_WEIGHT && vb == 0, "NaN weight refused");
257 OmegaGpuFp8SlotInfo inf; omega_gpu_fp8_slots_query(p, 1, &inf);
258 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");
259 free(bad);
260 rc = omega_gpu_fp8_slots_load(p, 1, held[1], w + wb * held[1], s + sc * held[1], &ver[1]);
261 if (rc != 0) g_failed++;
262 }
263
264 /* 4: reader fence through a recorded command list */
265 {
266 if (mutant) omega_gpu_fp8_slots_test_set_mutant(p, 1);
267 const uint32_t A = held[0], B = (held[0] + 1) % E;
268 OmegaGpuCmdList *cl = NULL; OmegaGpuMem tab, xd, yd;
269 if (omega_gpu_cmdlist_create(4, &cl) != 0) { printf("FAIL cmdlist create: %s\n", omega_gpu_session_last_error()); return 1; }
270 omega_gpu_session_lock();
271 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);
272 omega_gpu_session_unlock();
273 if (arc) { printf("FAIL alloc: %s\n", omega_gpu_session_last_error()); return 1; }
274 omega_fp8_pack_x_f32(x, k, xd.cpu);
275 float *y = yd.cpu; for (uint32_t i = 0; i < n; i++) y[i] = -999.0f;
276 /* reference for A and B on the resident tensor */
277 float *ya = malloc((size_t)n * 4), *yb = malloc((size_t)n * 4);
278 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);
279 CHECK(r1 == 0 && r2 == 0 && memcmp(ya, yb, (size_t)n * 4) != 0, "references: experts %u and %u give different y", A, B);
280 /* warm the kernel (grouped 1, x_rows 1) through the pool before recording */
281 uint32_t sl0 = 0; float *yw = malloc((size_t)n * 4);
282 (void)omega_gpu_fp8_slots_gemv_grouped_f32(p, 1, &sl0, &ver[0], x, 1, yw, NULL);
283 int ok = omega_gpu_fp8_slots_pin(p, 0, ver[0]) == 0;
284 uint64_t *tw = tab.cpu;
285 ok = ok && omega_gpu_fp8_slots_entry(p, 0, ver[0], &tw[0], &tw[1]) == 0;
286 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;
287 omega_gpu_session_lock();
288 omega_gpu_cmdlist_reset(cl);
289 ok = ok && omega_gpu_fp8_gemv_grouped_record(cl, omega_gpu_fp8_slots_tensor(p), 1, tab.va, xd.va, 1, yd.va) == 0;
290 omega_gpu_session_unlock();
291 /* the replacement attempt: between record and submit (the list may read the slot from here until submit returns) */
292 uint64_t vnew = 0;
293 int lrc = omega_gpu_fp8_slots_load(p, 0, B, w + wb * B, s + sc * B, &vnew);
294 if (!mutant) CHECK(lrc == OMEGA_GPU_FP8_SLOTS_BUSY, "load into the slot a recorded list reads: refused BUSY");
295 else printf("info mutant: load into the pinned slot returned %s\n", omega_gpu_fp8_slots_rc_name(lrc));
296 OmegaGpuCmdListOpts o; memset(&o, 0, sizeof o); o.mode = OMEGA_GPU_CMDLIST_PCAS;
297 OmegaGpuCmdListStats cs; memset(&cs, 0, sizeof cs);
298 omega_gpu_session_lock();
299 ok = ok && omega_gpu_cmdlist_submit(cl, &o, &cs) == 0;
300 omega_gpu_session_unlock();
301 ok = ok && omega_gpu_session_sync_to_cpu(&yd) == 0;
302 (void)omega_gpu_fp8_slots_unpin(p, 0, ver[0]);
303 const int same_a = ok && memcmp(y, ya, (size_t)n * 4) == 0, same_b = ok && memcmp(y, yb, (size_t)n * 4) == 0;
304 if (!mutant) CHECK(ok && same_a, "the list read the pinned expert %u bit-exactly (submit ok=%d)", A, ok);
305 else {
306 CHECK(ok && same_a, "NEGATIVE CONTROL: list output equals pinned expert %u (expected to FAIL under the mutant; read B=%d)", A, same_b);
307 }
308 free(ya); free(yb); free(yw);
309 omega_gpu_session_lock(); omega_gpu_session_free(&tab); omega_gpu_session_free(&xd); omega_gpu_session_free(&yd); omega_gpu_session_unlock();
310 omega_gpu_cmdlist_free(cl);
311 if (mutant) omega_gpu_fp8_slots_test_set_mutant(p, 0);
312 }
313
314 omega_gpu_fp8_slots_stats(p, &ps);
315 printf("stats slots=%u device_bytes=%" PRIu64 " loads=%" PRIu64 " load_failures=%" PRIu64 " busy=%" PRIu64 " stale=%" PRIu64
316 " pins=%" PRIu64 " pinned_now=%u pinned_high=%u bytes_loaded=%" PRIu64 " load_ns=%" PRIu64 " load_GBps=%.3f\n",
317 ps.slots, ps.device_bytes, ps.loads, ps.load_failures, ps.busy_refusals, ps.stale_refusals, ps.pins, ps.pinned_now,
318 ps.pinned_high, ps.bytes_loaded, ps.load_ns, ps.load_ns ? (double)ps.bytes_loaded / (double)ps.load_ns : 0.0);
319 CHECK(ps.pinned_now == 0 && ps.pinned_high <= G, "no pin leaked (pinned_now 0, high-water %u <= %u)", ps.pinned_high, G);
320 OmegaGpuAllocStats a2; omega_gpu_session_alloc_stats(&a2);
321 CHECK(omega_gpu_fp8_slots_free(p) == 0, "pool freed");
322 OmegaGpuAllocStats a3; omega_gpu_session_alloc_stats(&a3);
323 CHECK(a3.frees >= a2.frees + 2, "pool returned its 2 driver buffers (frees %" PRIu64 " -> %" PRIu64 ")", a2.frees, a3.frees);
324 omega_gpu_fp8_tensor_free(res);
325 free(w); free(s); free(x); free(yr); free(yp); free(ver); free(held);
326 return 0;
327}
328
329int main(int argc, char **argv) {
330 int host = 0, chip = 0, mutant = 0; uint32_t rounds = 64;
331 for (int i = 1; i < argc; i++) {
332 if (!strcmp(argv[i], "--host-only")) host = 1;
333 else if (!strcmp(argv[i], "--chip")) chip = 1;
334 else if (!strcmp(argv[i], "--mutant")) mutant = 1;
335 else if (!strcmp(argv[i], "--rounds") && i + 1 < argc) rounds = (uint32_t)atoi(argv[++i]);
336 else { printf("usage: %s --host-only | --chip [--rounds R] [--mutant]\n", argv[0]); return 2; }
337 }
338 if (!host && !chip) { printf("usage: %s --host-only | --chip [--rounds R] [--mutant]\n", argv[0]); return 2; }
339 if (host) host_tests();
340 if (chip) {
341 (void)omega_gpu_session_set_spin_us(2000);
342 chip_shape(1536, 2048, 12, 5, rounds, mutant); /* gate|up */
343 if (!mutant) chip_shape(2048, 768, 12, 5, rounds, 0); /* down */
344 if (omega_gpu_session_is_blocked()) { printf("FAIL session latched\n"); g_failed++; }
345 }
346 if (mutant) {
347 printf("GPU_FP8_SLOTS_NEGATIVE_CONTROL: %s (%d of %d checks failed)\n", g_failed ? "CAUGHT" : "NOT CAUGHT", g_failed, g_checks);
348 return g_failed ? 1 : 0;
349 }
350 printf("GPU_FP8_SLOTS: %s (%d checks, %d failed)\n", g_failed ? "FAIL" : "PASS", g_checks, g_failed);
351 return g_failed ? 1 : 0;
352}