interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / evidence / CODEPAD-PREFETCH-f1d57c5 / red-green-probe / va_camera.c

81 lines · 6036 bytes Raw Blame History
1/* omega#323 camera: replay hd128 gqa_f32 32q8kv ctx 1, 17, 256 in one process (the failing order) and
2 * record every chip allocation (link-time wrap of nvrm_alloc / nvrm_alloc_gpu_uncached / nvrm_free).
3 * The code buffer is found by content (the kernel bytes from codegen). Nothing in the launch path changes.
4 * usage: va_camera <variant-label> [ctx ...] (default ctx list: 1 17 256) */
5#include <math.h>
6#include <stdint.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <time.h>
11#include "nvrm.h"
12#include "omega_gpu_attention_api.h"
13
14int __real_nvrm_alloc(Nvrm *rm, uint64_t size, NvrmMem *out);
15int __real_nvrm_alloc_gpu_uncached(Nvrm *rm, uint64_t size, NvrmMem *out);
16int __real_nvrm_free(Nvrm *rm, NvrmMem *m);
17
18typedef struct { uint64_t va, size; void *cpu; int live; const char *kind; } Rec;
19static Rec g_rec[4096]; static int g_n;
20static void rec_add(const NvrmMem *m, const char *kind) { if (g_n < 4096) g_rec[g_n++] = (Rec){ m->va, m->size, m->cpu, 1, kind }; }
21int __wrap_nvrm_alloc(Nvrm *rm, uint64_t size, NvrmMem *out) { int rc = __real_nvrm_alloc(rm, size, out); if (rc == 0) rec_add(out, "cached"); return rc; }
22int __wrap_nvrm_alloc_gpu_uncached(Nvrm *rm, uint64_t size, NvrmMem *out) { int rc = __real_nvrm_alloc_gpu_uncached(rm, size, out); if (rc == 0) rec_add(out, "uncached"); return rc; }
23int __wrap_nvrm_free(Nvrm *rm, NvrmMem *m) { for (int i = 0; i < g_n; i++) if (g_rec[i].live && g_rec[i].va == m->va) g_rec[i].live = 0; return __real_nvrm_free(rm, m); }
24
25static void utc(char *b, size_t n) { struct timespec t; clock_gettime(CLOCK_REALTIME, &t); struct tm tm; gmtime_r(&t.tv_sec, &tm); strftime(b, n, "%Y-%m-%dT%H:%M:%SZ", &tm); }
26static int live_covering(uint64_t va) { for (int i = 0; i < g_n; i++) if (g_rec[i].live && va >= g_rec[i].va && va < g_rec[i].va + g_rec[i].size) return i; return -1; }
27
28static void snap(const char *site, const uint8_t *kcode, size_t ksize) {
29 char ts[32]; utc(ts, sizeof ts);
30 printf("SNAP site=%s utc=%s allocations=%d\n", site, ts, g_n);
31 for (int i = 0; i < g_n; i++) {
32 int is_code = g_rec[i].live && g_rec[i].size >= ksize && g_rec[i].cpu && memcmp(g_rec[i].cpu, kcode, ksize) == 0;
33 printf(" ALLOC #%d va=0x%llx size=0x%llx end=0x%llx %s %s%s\n", i, (unsigned long long)g_rec[i].va, (unsigned long long)g_rec[i].size,
34 (unsigned long long)(g_rec[i].va + g_rec[i].size), g_rec[i].kind, g_rec[i].live ? "live" : "freed", is_code ? " CODE" : "");
35 if (is_code) {
36 uint64_t end = g_rec[i].va + g_rec[i].size, code_end = g_rec[i].va + ksize;
37 int nxt = live_covering(end), nxt2 = live_covering(code_end + 2048 - 1);
38 printf(" CODE va=0x%llx code_bytes=%zu alloc=0x%llx slack_in_alloc=%llu page_after_alloc=0x%llx mapped_by=%s%d code_end+2047 mapped=%s\n",
39 (unsigned long long)g_rec[i].va, ksize, (unsigned long long)g_rec[i].size, (unsigned long long)(g_rec[i].size - ksize),
40 (unsigned long long)end, nxt >= 0 ? "#" : "NONE", nxt, nxt2 >= 0 ? "yes" : "NO");
41 }
42 }
43 fflush(stdout);
44}
45
46int main(int argc, char **argv) {
47 const char *variant = argc > 1 ? argv[1] : "unlabelled";
48 uint32_t ctxs[16] = { 1, 17, 256 }; int nc = 3;
49 if (argc > 2) { nc = 0; for (int i = 2; i < argc && nc < 16; i++) ctxs[nc++] = (uint32_t)atoi(argv[i]); }
50 const uint32_t NQ = 32, NKV = 8, HD = 128, MAXC = 4096;
51 OmegaBlackwellKernel kk; memset(&kk, 0, sizeof kk);
52 if (omega_gpu_attention_codegen_hd(true, 12, 2, HD, &kk) != 0) { printf("codegen failed\n"); return 2; }
53 printf("VARIANT %s kernel_bytes=%zu\n", variant, (size_t)kk.code_size);
54 float *q = malloc(NQ * HD * 4), *k = malloc((size_t)MAXC * NKV * HD * 4), *v = malloc((size_t)MAXC * NKV * HD * 4), *o = malloc(NQ * HD * 4);
55 uint32_t s = 12345u;
56 for (size_t i = 0; i < NQ * HD; i++) { s = s * 1664525u + 1013904223u; q[i] = (float)((s >> 8) & 0xffff) / 32768.0f - 1.0f; }
57 for (size_t i = 0; i < (size_t)MAXC * NKV * HD; i++) { s = s * 1664525u + 1013904223u; k[i] = (float)((s >> 8) & 0xffff) / 32768.0f - 1.0f;
58 s = s * 1664525u + 1013904223u; v[i] = (float)((s >> 8) & 0xffff) / 32768.0f - 1.0f; }
59 int worst_rc = 0;
60 for (int c = 0; c < nc; c++) {
61 uint32_t ctx = ctxs[c]; char nm[32]; snprintf(nm, sizeof nm, "before_ctx%u", ctx);
62 if (c > 0) snap(nm, kk.code, kk.code_size); else { char ts[32]; utc(ts, sizeof ts); printf("SNAP site=%s utc=%s allocations=%d (device not open yet)\n", nm, ts, g_n); }
63 for (size_t i = 0; i < NQ * HD; i++) o[i] = NAN;
64 OmegaGpuAttnInfo info; memset(&info, 0, sizeof info);
65 int rc = omega_gpu_gqa_attention_f32(q, k, v, ctx, NQ, NKV, HD, o, &info);
66 double worst = 0;
67 for (uint32_t h = 0; h < NQ; h++) {
68 uint32_t kvh = h / (NQ / NKV); double m = -INFINITY, *sc = malloc(ctx * sizeof(double)), l = 0;
69 for (uint32_t t = 0; t < ctx; t++) { double d = 0; for (uint32_t x = 0; x < HD; x++) d += (double)q[h * HD + x] * k[((size_t)t * NKV + kvh) * HD + x]; sc[t] = d / sqrt((double)HD); if (sc[t] > m) m = sc[t]; }
70 for (uint32_t t = 0; t < ctx; t++) { sc[t] = exp(sc[t] - m); l += sc[t]; }
71 for (uint32_t x = 0; x < HD; x++) { double a = 0; for (uint32_t t = 0; t < ctx; t++) a += sc[t] * v[((size_t)t * NKV + kvh) * HD + x]; double e = fabs(a / l - o[h * HD + x]); if (!(e <= worst)) worst = isnan(e) ? INFINITY : e; }
72 free(sc);
73 }
74 char ts[32]; utc(ts, sizeof ts);
75 printf("RESULT variant=%s ctx=%u rc=%d utc_after=%s call_ns=%llu chip_ns=%llu unwritten=%llu max_abs_err=%.3g\n", variant, ctx, rc, ts,
76 (unsigned long long)info.call_ns, (unsigned long long)info.elapsed_ns, (unsigned long long)info.unwritten_words, worst);
77 snprintf(nm, sizeof nm, "after_ctx%u", ctx); snap(nm, kk.code, kk.code_size);
78 if (rc != 0) { worst_rc = rc; break; }
79 }
80 return worst_rc == 0 ? 0 : 1;
81}