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
CUDApp / tests / gpu_l2_stale_test.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* 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 |
| 3 | * already read, does the next launch read the old bytes from GPU L2? |
| 4 | * |
| 5 | * Real path only: omega_gpu_fp8_tensor_upload (memcpy into the CPU mapping + omega_gpu_session_sync_to_gpu, which is |
| 6 | * a dsb sy and nothing else) and omega_gpu_fp8_gemv_f32 (one launch, C3 tail, output read back from uncached memory). |
| 7 | * One variable per process: --cached 0|1 (omega_gpu_fp8_set_weight_cache). --cached 0 is the negative control |
| 8 | * (uncached mapping: nvos.h:1117-1118 says only the cached case is not coherent with CPU mappings). |
| 9 | * |
| 10 | * Tensor: 1 expert x N=256 x K=2048 e4m3 (512 KB), scales bf16 1.0, x = 1.0. |
| 11 | * A = every weight +1.0 (0x38) -> every y = +2048. B = every weight -1.0 (0xB8) -> every y = -2048. |
| 12 | * Camera: a row's 2048 bytes are 16 lines of 128 B; a stale line adds +128 instead of -128, so |
| 13 | * stale_lines(row) = (y + 2048) / 256, exact in f32. Any other y is printed as UNDECODED. |
| 14 | * Modes: |
| 15 | * rewrite upload A, GEMV (GPU reads A), host memcpy B into the SAME buffer + sync_to_gpu, then GEMV x reps. |
| 16 | * realloc upload A, GEMV, free A, upload B (fresh buffer: VA printed, same-VA noted), then GEMV x reps. |
| 17 | * The first GEMV on A must read +2048 on every row: that shows the decode can see A's bytes at all. |
| 18 | * Exit 0 = FRESH, 1 = STALE_READ or API failure, 2 = usage. Red receipt (unfixed platform, omega 710d438c): rewrite |
| 19 | * --cached 1 read 4096 of 4096 lines stale on every launch, 5/5; --cached 0 FRESH 5/5. */ |
| 20 | #include "omega_gpu_fp8_api.h" |
| 21 | #include "omega_gpu_fp8_internal.h" |
| 22 | #include "omega_gpu_session.h" |
| 23 | #include <inttypes.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | |
| 28 | /* No copy of the tensor layout: the test reaches the weight buffer through the library-internal accessors |
| 29 | * (omega_gpu_fp8_internal.h), so a change to struct OmegaGpuFp8Tensor cannot desynchronise it. A hand copy did: |
| 30 | * CUDApp added allocation_epoch/owner_generation ahead of w and the copy read allocation_epoch as w.va. */ |
| 31 | |
| 32 | #define N 256u |
| 33 | #define K 2048u |
| 34 | |
| 35 | static int snap(const char *site, const float *y, float expect) { |
| 36 | uint32_t stale_rows = 0, stale_lines = 0, undecoded = 0; float lo = y[0], hi = y[0]; |
| 37 | for (uint32_t r = 0; r < N; r++) { |
| 38 | if (y[r] < lo) lo = y[r]; |
| 39 | if (y[r] > hi) hi = y[r]; |
| 40 | if (expect > 0) { if (y[r] != 2048.0f) undecoded++; continue; } |
| 41 | float s = (y[r] + 2048.0f) / 256.0f; |
| 42 | if (s != (float)(int)s || s < 0 || s > 16) { undecoded++; continue; } |
| 43 | if (s > 0) { stale_rows++; stale_lines += (uint32_t)s; } |
| 44 | } |
| 45 | 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", |
| 46 | site, expect, N, stale_rows, stale_lines, N * 16u, undecoded, lo, hi); |
| 47 | if (undecoded) printf("SNAP site=%s y_row0=%.3f interpretation=UNDECODED\n", site, y[0]); |
| 48 | return expect > 0 ? (undecoded ? -1 : 0) : (int)(stale_lines + undecoded); |
| 49 | } |
| 50 | |
| 51 | int main(int argc, char **argv) { |
| 52 | const char *mode = NULL; int cached = -1; uint32_t reps = 3; |
| 53 | for (int i = 1; i < argc; i++) { |
| 54 | if (strcmp(argv[i], "--mode") == 0 && i + 1 < argc) mode = argv[++i]; |
| 55 | else if (strcmp(argv[i], "--cached") == 0 && i + 1 < argc) cached = atoi(argv[++i]); |
| 56 | else if (strcmp(argv[i], "--reps") == 0 && i + 1 < argc) reps = (uint32_t)atoi(argv[++i]); |
| 57 | else { printf("usage: %s --mode rewrite|realloc --cached 0|1 [--reps R]\n", argv[0]); return 2; } |
| 58 | } |
| 59 | if (!mode || (strcmp(mode, "rewrite") && strcmp(mode, "realloc")) || (cached != 0 && cached != 1) || reps == 0 || reps > 64) { |
| 60 | printf("usage: %s --mode rewrite|realloc --cached 0|1 [--reps R]\n", argv[0]); return 2; |
| 61 | } |
| 62 | (void)omega_gpu_session_set_spin_us(2000); |
| 63 | omega_gpu_fp8_set_weight_cache(cached); |
| 64 | 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); |
| 65 | |
| 66 | uint8_t *wa = malloc((size_t)N * K), *wb = malloc((size_t)N * K); |
| 67 | uint16_t sc[(N / 128) * (K / 128)]; float x[K], y[N]; |
| 68 | memset(wa, 0x38, (size_t)N * K); memset(wb, 0xB8, (size_t)N * K); |
| 69 | for (size_t i = 0; i < sizeof sc / sizeof sc[0]; i++) sc[i] = 0x3F80; |
| 70 | for (uint32_t i = 0; i < K; i++) x[i] = 1.0f; |
| 71 | |
| 72 | OmegaGpuFp8Tensor *t = NULL; |
| 73 | int rc = omega_gpu_fp8_tensor_upload(1, N, K, wa, sc, &t); |
| 74 | if (rc != OMEGA_GPU_FP8_OK) { printf("FAIL upload A: %s\n", omega_gpu_fp8_last_error()); return 1; } |
| 75 | uint64_t va_a = 0, sva = 0; uint8_t *w_cpu = NULL; float *s_cpu = NULL; |
| 76 | if (omega_gpu_fp8_tensor_entry(t, 0, &va_a, &sva) != OMEGA_GPU_FP8_OK || omega_gpu_fp8_tensor_cpu_view(t, 0, &w_cpu, &s_cpu) != OMEGA_GPU_FP8_OK) { |
| 77 | printf("FAIL tensor view: %s\n", omega_gpu_fp8_last_error()); return 1; |
| 78 | } |
| 79 | memset(y, 0, sizeof y); |
| 80 | 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; } |
| 81 | 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; } |
| 82 | |
| 83 | if (strcmp(mode, "rewrite") == 0) { |
| 84 | memcpy(w_cpu, wb, (size_t)N * K); /* what omega_gpu_fp8_tensor_upload does, on the same buffer */ |
| 85 | if (omega_gpu_fp8_tensor_sync_to_gpu(t) != 0) { printf("FAIL sync: %s\n", omega_gpu_session_last_error()); return 1; } |
| 86 | printf("HOST rewrote w (va 0x%" PRIx64 ") with B and synced\n", va_a); |
| 87 | } else { |
| 88 | omega_gpu_fp8_tensor_free(t); t = NULL; |
| 89 | 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; } |
| 90 | uint64_t va_b = 0; |
| 91 | if (omega_gpu_fp8_tensor_entry(t, 0, &va_b, &sva) != OMEGA_GPU_FP8_OK) { printf("FAIL tensor view 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, va_b, va_b == 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 total ? 1 : 0; |
| 105 | } |