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 / evidence / OWN30B-REQUAL-MAIN-710d438c-20261015 / omega457 / src / l2_stale_test.c
| 1 | /* l2_stale_test: omega #457. After the host rewrites a GPU-cached (CPU_WRITE | GPU_READ) buffer that the GPU has |
| 2 | * already read, does the next launch read the old bytes from GPU L2? |
| 3 | * |
| 4 | * Real path only: omega_gpu_fp8_tensor_upload (memcpy into the CPU mapping + omega_gpu_session_sync_to_gpu, which is |
| 5 | * a dsb sy and nothing else) and omega_gpu_fp8_gemv_f32 (one launch, C3 tail, output read back from uncached memory). |
| 6 | * One variable per process: --cached 0|1 (omega_gpu_fp8_set_weight_cache). --cached 0 is the negative control |
| 7 | * (uncached mapping: nvos.h:1117-1118 says only the cached case is not coherent with CPU mappings). |
| 8 | * |
| 9 | * Tensor: 1 expert x N=256 x K=2048 e4m3 (512 KB), scales bf16 1.0, x = 1.0. |
| 10 | * A = every weight +1.0 (0x38) -> every y = +2048. B = every weight -1.0 (0xB8) -> every y = -2048. |
| 11 | * Camera: a row's 2048 bytes are 16 lines of 128 B; a stale line adds +128 instead of -128, so |
| 12 | * stale_lines(row) = (y + 2048) / 256, exact in f32. Any other y is printed as UNDECODED. |
| 13 | * Modes: |
| 14 | * rewrite upload A, GEMV (GPU reads A), host memcpy B into the SAME buffer + sync_to_gpu, then GEMV x reps. |
| 15 | * realloc upload A, GEMV, free A, upload B (fresh buffer: VA printed, same-VA noted), then GEMV x reps. |
| 16 | * The first GEMV on A must read +2048 on every row: that shows the decode can see A's bytes at all. |
| 17 | * Exit 0 = ran (verdict in the STALE line), 1 = API failure, 2 = usage. */ |
| 18 | #include "omega_gpu_fp8_api.h" |
| 19 | #include "omega_gpu_session.h" |
| 20 | #include <inttypes.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | |
| 25 | /* Copied verbatim from omega src/omega_gpu_fp8_api.c:20-24 (frozen candidate, not modified). Checked at run time |
| 26 | * against the public omega_gpu_fp8_tensor_entry() VAs; a mismatch aborts. */ |
| 27 | struct OmegaGpuFp8Tensor { |
| 28 | uint32_t experts, n, k; |
| 29 | OmegaGpuMem w; /* experts * n * k e4m3 bytes, row-major */ |
| 30 | OmegaGpuMem s; /* experts * (n/128) * (k/128) f32 (scale * 2^56) */ |
| 31 | }; |
| 32 | |
| 33 | #define N 256u |
| 34 | #define K 2048u |
| 35 | |
| 36 | static int snap(const char *site, const float *y, float expect) { |
| 37 | uint32_t stale_rows = 0, stale_lines = 0, undecoded = 0; float lo = y[0], hi = y[0]; |
| 38 | for (uint32_t r = 0; r < N; r++) { |
| 39 | if (y[r] < lo) lo = y[r]; |
| 40 | if (y[r] > hi) hi = y[r]; |
| 41 | if (expect > 0) { if (y[r] != 2048.0f) undecoded++; continue; } |
| 42 | float s = (y[r] + 2048.0f) / 256.0f; |
| 43 | if (s != (float)(int)s || s < 0 || s > 16) { undecoded++; continue; } |
| 44 | if (s > 0) { stale_rows++; stale_lines += (uint32_t)s; } |
| 45 | } |
| 46 | printf("SNAP site=%s expected=%.0f rows=%u stale_rows=%u stale_lines=%u of %u undecoded=%u y_min=%.1f y_max=%.1f\n", |
| 47 | site, expect, N, stale_rows, stale_lines, N * 16u, undecoded, lo, hi); |
| 48 | if (undecoded) printf("SNAP site=%s y_row0=%.3f interpretation=UNDECODED\n", site, y[0]); |
| 49 | return expect > 0 ? (undecoded ? -1 : 0) : (int)(stale_lines + undecoded); |
| 50 | } |
| 51 | |
| 52 | int main(int argc, char **argv) { |
| 53 | const char *mode = NULL; int cached = -1; uint32_t reps = 3; |
| 54 | for (int i = 1; i < argc; i++) { |
| 55 | if (strcmp(argv[i], "--mode") == 0 && i + 1 < argc) mode = argv[++i]; |
| 56 | else if (strcmp(argv[i], "--cached") == 0 && i + 1 < argc) cached = atoi(argv[++i]); |
| 57 | else if (strcmp(argv[i], "--reps") == 0 && i + 1 < argc) reps = (uint32_t)atoi(argv[++i]); |
| 58 | else { printf("usage: %s --mode rewrite|realloc --cached 0|1 [--reps R]\n", argv[0]); return 2; } |
| 59 | } |
| 60 | if (!mode || (strcmp(mode, "rewrite") && strcmp(mode, "realloc")) || (cached != 0 && cached != 1) || reps == 0 || reps > 64) { |
| 61 | printf("usage: %s --mode rewrite|realloc --cached 0|1 [--reps R]\n", argv[0]); return 2; |
| 62 | } |
| 63 | (void)omega_gpu_session_set_spin_us(2000); |
| 64 | omega_gpu_fp8_set_weight_cache(cached); |
| 65 | printf("RUN mode=%s cached=%d weight_cache_now=%d n=%u k=%u reps=%u\n", mode, cached, omega_gpu_fp8_weight_cache(), N, K, reps); |
| 66 | |
| 67 | uint8_t *wa = malloc((size_t)N * K), *wb = malloc((size_t)N * K); |
| 68 | uint16_t sc[(N / 128) * (K / 128)]; float x[K], y[N]; |
| 69 | memset(wa, 0x38, (size_t)N * K); memset(wb, 0xB8, (size_t)N * K); |
| 70 | for (size_t i = 0; i < sizeof sc / sizeof sc[0]; i++) sc[i] = 0x3F80; |
| 71 | for (uint32_t i = 0; i < K; i++) x[i] = 1.0f; |
| 72 | |
| 73 | OmegaGpuFp8Tensor *t = NULL; |
| 74 | int rc = omega_gpu_fp8_tensor_upload(1, N, K, wa, sc, &t); |
| 75 | if (rc != OMEGA_GPU_FP8_OK) { printf("FAIL upload A: %s\n", omega_gpu_fp8_last_error()); return 1; } |
| 76 | uint64_t wva = 0, sva = 0; |
| 77 | if (omega_gpu_fp8_tensor_entry(t, 0, &wva, &sva) != 0 || wva != t->w.va || sva != t->s.va || !t->w.cpu) { |
| 78 | printf("FAIL mirror struct does not match omega (entry w_va 0x%" PRIx64 " vs mirror 0x%" PRIx64 ")\n", wva, t->w.va); return 1; |
| 79 | } |
| 80 | const uint64_t va_a = t->w.va; |
| 81 | memset(y, 0, sizeof y); |
| 82 | if ((rc = omega_gpu_fp8_gemv_f32(t, 0, x, y, NULL)) != OMEGA_GPU_FP8_OK) { printf("FAIL gemv A: %s\n", omega_gpu_fp8_last_error()); return 1; } |
| 83 | if (snap("gemv-A", y, 2048.0f) != 0) { printf("STALE verdict=INVALID reason=first read of A is not +2048 (decode cannot see A)\n"); return 1; } |
| 84 | |
| 85 | if (strcmp(mode, "rewrite") == 0) { |
| 86 | memcpy(t->w.cpu, wb, (size_t)N * K); /* what omega_gpu_fp8_tensor_upload does, on the same buffer */ |
| 87 | if (omega_gpu_session_sync_to_gpu(&t->w) != 0) { printf("FAIL sync: %s\n", omega_gpu_session_last_error()); return 1; } |
| 88 | printf("HOST rewrote w (va 0x%" PRIx64 ") with B and synced\n", va_a); |
| 89 | } else { |
| 90 | omega_gpu_fp8_tensor_free(t); t = NULL; |
| 91 | if ((rc = omega_gpu_fp8_tensor_upload(1, N, K, wb, sc, &t)) != OMEGA_GPU_FP8_OK) { printf("FAIL upload B: %s\n", omega_gpu_fp8_last_error()); return 1; } |
| 92 | printf("HOST freed A (va 0x%" PRIx64 "), uploaded B at va 0x%" PRIx64 " same_va=%s phys=UNKNOWN reason=no physical address in the API\n", |
| 93 | va_a, t->w.va, t->w.va == va_a ? "yes" : "no"); |
| 94 | } |
| 95 | uint32_t total = 0; |
| 96 | for (uint32_t r = 0; r < reps; r++) { |
| 97 | char site[32]; snprintf(site, sizeof site, "gemv-B-%u", r + 1); |
| 98 | for (uint32_t i = 0; i < N; i++) y[i] = 12345.0f; |
| 99 | if ((rc = omega_gpu_fp8_gemv_f32(t, 0, x, y, NULL)) != OMEGA_GPU_FP8_OK) { printf("FAIL gemv B: %s\n", omega_gpu_fp8_last_error()); return 1; } |
| 100 | total += (uint32_t)snap(site, y, -2048.0f); |
| 101 | } |
| 102 | printf("STALE mode=%s cached=%d stale_or_undecoded_lines_total=%u verdict=%s\n", mode, cached, total, total ? "STALE_READ" : "FRESH"); |
| 103 | omega_gpu_fp8_tensor_free(t); free(wa); free(wb); |
| 104 | return 0; |
| 105 | } |