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