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_pf_mem_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_pf_mem_test.c (OWN-30B T3b, 2026-10-13): device-resident prefill memory. |
| 18 | * --host-only the copy kernel in the warp simulator (exact copy, nothing past `units` written) |
| 19 | * (default) the chip (caller holds flock /tmp/aien-gb10.lock): |
| 20 | * 1. upload/download round trip of 96 MB through a GPU-cached device buffer (bit-exact; the host buffer is poisoned |
| 21 | * first, so a download that did not run is caught), and a second upload over the same buffer read back exactly |
| 22 | * (a stale L2 line would show here: the GPU rewrote lines it had cached); |
| 23 | * 2. read bandwidth, uncached (session buffer) vs cached (device buffer): every CTA distinct (DRAM) and every CTA the |
| 24 | * same bytes (cache reuse), 5 runs each, median; every XOR result checked. |
| 25 | * Verdict line last: "GPU_PF_MEM: PASS|FAIL". */ |
| 26 | #include "omega_gpu_prefill.h" |
| 27 | #include "omega_blackwell_codegen_pf.h" |
| 28 | #include "omega_blackwell_codegen_tc.h" |
| 29 | #include "bw_warp_sim_tc.h" |
| 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
| 33 | |
| 34 | static int cmp_u64(const void *a, const void *b) { uint64_t x = *(const uint64_t *)a, y = *(const uint64_t *)b; return x < y ? -1 : x > y; } |
| 35 | |
| 36 | static int host_only(void) { |
| 37 | static BlackwellIRProgram p; |
| 38 | if (omega_blackwell_codegen_pf_copy_prog(&p) != 0) { printf("copy codegen refused\n"); return 1; } |
| 39 | enum { U = 1000, CAP = 1100 }; |
| 40 | static uint32_t src[CAP * 4], dst[CAP * 4]; |
| 41 | for (int i = 0; i < CAP * 4; i++) { src[i] = 0x9e3779b9u * (uint32_t)i; dst[i] = 0xdeadbeefu; } |
| 42 | static WsSim s; memset(&s, 0, sizeof s); s.p = &p; s.threads = OMEGA_PF_COPY_THREADS; |
| 43 | uint64_t d = (uintptr_t)dst, sv = (uintptr_t)src; |
| 44 | s.cbank[0x380 / 4] = (uint32_t)d; s.cbank[0x384 / 4] = (uint32_t)(d >> 32); s.cbank[0x388 / 4] = (uint32_t)sv; s.cbank[0x38c / 4] = (uint32_t)(sv >> 32); s.cbank[0x390 / 4] = U; |
| 45 | s.ranges[0].lo = d; s.ranges[0].hi = d + sizeof dst; s.ranges[1].lo = sv; s.ranges[1].hi = sv + sizeof src; s.nranges = 2; |
| 46 | int err = ws_run(&s, (U + OMEGA_PF_COPY_THREADS - 1) / OMEGA_PF_COPY_THREADS, 1); |
| 47 | size_t bad = 0, past = 0; |
| 48 | for (int i = 0; i < CAP * 4; i++) { if (i < U * 4 && dst[i] != src[i]) bad++; if (i >= U * 4 && dst[i] != 0xdeadbeefu) past++; } |
| 49 | printf("sim copy: %s, peak %u registers, %zu wrong, %zu written past the end\n", err ? s.msg : "ran", p.regalloc.peak_gpr_usage, bad, past); |
| 50 | return (err || bad || past) ? 1 : 0; |
| 51 | } |
| 52 | |
| 53 | typedef struct { uint32_t iters, groups, threads; } BwSpec; |
| 54 | static int gen_bw(const OmegaPfKey *k, void *prog) { return omega_blackwell_codegen_tc_bw_prog(k->words[0], k->words[1], k->words[2], prog); } |
| 55 | |
| 56 | int main(int argc, char **argv) { |
| 57 | int fail = host_only(); |
| 58 | if (argc > 1 && !strcmp(argv[1], "--host-only")) { printf("GPU_PF_MEM: %s\n", fail ? "FAIL" : "PASS"); return fail ? 1 : 0; } |
| 59 | if (fail) { printf("GPU_PF_MEM: FAIL\n"); return 1; } |
| 60 | |
| 61 | /* 1. round trip */ |
| 62 | const size_t RB = 96ull << 20; |
| 63 | uint32_t *a = malloc(RB), *b = malloc(RB); |
| 64 | if (!a || !b) return 1; |
| 65 | for (size_t i = 0; i < RB / 4; i++) a[i] = (uint32_t)(i * 2654435761u) ^ 0x5bd1e995u; |
| 66 | OmegaGpuMem dev; |
| 67 | int rc = omega_gpu_pf_alloc_dev(RB, &dev); |
| 68 | if (rc) { printf("alloc_dev: %s (%s)\n", omega_gpu_pf_rc_name(rc), omega_gpu_session_last_error()); printf("GPU_PF_MEM: FAIL\n"); return 1; } |
| 69 | memset(b, 0xa5, RB); |
| 70 | rc = omega_gpu_pf_upload(&dev, 0, a, RB); |
| 71 | if (!rc) rc = omega_gpu_pf_download(&dev, 0, b, RB); |
| 72 | size_t bad = 0; for (size_t i = 0; i < RB / 4; i++) if (a[i] != b[i]) bad++; |
| 73 | printf("round trip 1 (%zu MB, cached device buffer): rc %s, %zu wrong words\n", RB >> 20, omega_gpu_pf_rc_name(rc), bad); |
| 74 | if (rc || bad) fail++; |
| 75 | for (size_t i = 0; i < RB / 4; i++) a[i] = ~a[i] + (uint32_t)i; |
| 76 | memset(b, 0x5a, RB); |
| 77 | rc = omega_gpu_pf_upload(&dev, 0, a, RB); |
| 78 | if (!rc) rc = omega_gpu_pf_download(&dev, 0, b, RB); |
| 79 | bad = 0; for (size_t i = 0; i < RB / 4; i++) if (a[i] != b[i]) bad++; |
| 80 | printf("round trip 2 (rewritten by the GPU over cached lines): rc %s, %zu wrong words\n", omega_gpu_pf_rc_name(rc), bad); |
| 81 | if (rc || bad) fail++; |
| 82 | omega_gpu_pf_free(&dev); |
| 83 | |
| 84 | /* 2. bandwidth: uncached vs cached, distinct vs same */ |
| 85 | const uint32_t T = 256, grid = 384; |
| 86 | for (int cached = 0; cached < 2 && !fail; cached++) for (int same = 0; same < 2 && !fail; same++) { |
| 87 | const uint32_t iters = same ? 8u : 32u, groups = same ? 1u : grid; |
| 88 | const size_t region = (size_t)iters * T * 128u, sb = region * groups, ob = (size_t)grid * T * 4u; |
| 89 | OmegaGpuMem src = { 0 }, out = { 0 }; |
| 90 | uint32_t *pat = malloc(sb); |
| 91 | for (size_t q = 0; q < sb / 4; q++) pat[q] = (uint32_t)(q * 2654435761u); |
| 92 | rc = cached ? omega_gpu_pf_alloc_dev(sb, &src) : omega_gpu_pf_alloc_host(sb, &src); |
| 93 | if (!rc) rc = omega_gpu_pf_alloc_host(ob, &out); |
| 94 | if (!rc) { if (cached) rc = omega_gpu_pf_upload(&src, 0, pat, sb); else { memcpy(src.cpu, pat, sb); } } |
| 95 | uint32_t *want = calloc((size_t)grid * T, 4); |
| 96 | for (uint32_t c = 0; c < grid; c++) for (uint32_t t = 0; t < T; t++) { uint32_t x = 0; const uint32_t *r = pat + (size_t)(groups == 1 ? 0 : c) * region / 4; |
| 97 | for (uint32_t it = 0; it < iters; it++) for (uint32_t q = 0; q < 8; q++) for (uint32_t s = 0; s < 4; s++) x ^= r[((size_t)it * T * 128u + (size_t)q * T * 16u + t * 16u) / 4 + s]; |
| 98 | want[(size_t)c * T + t] = x; } |
| 99 | uint64_t ns[5] = { 0 }; size_t wrong = 0; |
| 100 | OmegaPfKey key; memset(&key, 0, sizeof key); key.kind = 0x100u; key.words[0] = iters; key.words[1] = groups; key.words[2] = T; |
| 101 | for (int r = 0; r < 5 && !rc; r++) { |
| 102 | uint32_t *o = out.cpu; memset(o, 0, ob); |
| 103 | uint32_t args[4] = { (uint32_t)src.va, (uint32_t)(src.va >> 32), (uint32_t)out.va, (uint32_t)(out.va >> 32) }; |
| 104 | omega_gpu_session_lock(); |
| 105 | OmegaPfKernel k; rc = omega_gpu_pf_open_locked(); |
| 106 | if (!rc) rc = omega_gpu_pf_kernel_locked(&key, gen_bw, &k); |
| 107 | if (!rc && omega_gpu_session_sync_to_gpu(&out) != 0) rc = OMEGA_GPU_PF_CHIP_FAIL; |
| 108 | if (!rc) rc = omega_gpu_pf_launch_locked(&k, T, grid, 1, 0, args, 4, &ns[r]); |
| 109 | if (!rc && omega_gpu_session_sync_to_cpu(&out) != 0) rc = OMEGA_GPU_PF_CHIP_FAIL; |
| 110 | omega_gpu_session_unlock(); |
| 111 | if (!rc) for (size_t q = 0; q < (size_t)grid * T; q++) if (o[q] != want[q]) wrong++; |
| 112 | } |
| 113 | qsort(ns, 5, 8, cmp_u64); |
| 114 | const double bytes = (double)grid * iters * T * 128.0; |
| 115 | printf("bw %s %s: rc %s, median %.1f us, %.1f GB/s, %zu wrong\n", cached ? "cached " : "uncached", same ? "same-1MB" : "distinct", omega_gpu_pf_rc_name(rc), ns[2] / 1e3, bytes / ns[2], wrong); |
| 116 | if (rc || wrong) { fail++; printf(" error: %s\n", omega_gpu_session_last_error()); } |
| 117 | free(pat); free(want); omega_gpu_pf_free(&src); omega_gpu_pf_free(&out); |
| 118 | } |
| 119 | free(a); free(b); |
| 120 | printf("GPU_PF_MEM: %s\n", fail ? "FAIL" : "PASS"); |
| 121 | return fail ? 1 : 0; |
| 122 | } |