omega-bridge
Omega-first language interoperability: checked conversion backends and native language bindings
Brought by drakestapleton
omega-bridge / conformance / omega_runner.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* Omega-side conformance runner for the frozen matmul and causal GQA contracts. |
| 3 | * |
| 4 | * Generates the same deterministic inputs as conformance/oracle.py (splitmix64, |
| 5 | * exact multiples of 1/64 for bf16 and 1/1024 for f32), executes through the |
| 6 | * existing CUDApp API, and writes raw f32 output plus run metadata. It does not |
| 7 | * call the Python oracle and does not judge itself. |
| 8 | * |
| 9 | * omega_runner matmul --m 4 --k 5 --n 3 --seed 680 --out PREFIX [--device] |
| 10 | * omega_runner attention --base 1 --rows 2 --hq 4 --hkv 2 --hd 128 \ |
| 11 | * --seed 2810054072 --out PREFIX [--device] |
| 12 | * |
| 13 | * Without --device the matmul path uses the CUDApp host oracle only to validate |
| 14 | * wiring; that is NOT device evidence. Attention has no host path here and |
| 15 | * exits 4 (NOT_RUN). A --device run requires SPARK_WINDOW. */ |
| 16 | #include <stdint.h> |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include <time.h> |
| 21 | #include <unistd.h> |
| 22 | |
| 23 | #include "omega_blackwell_matmul.h" |
| 24 | #include "omega_gpu_attention_api.h" |
| 25 | #include "omega_gpu_matmul_api.h" |
| 26 | #include "sha256.h" |
| 27 | |
| 28 | #define MASK64 ((uint64_t)-1) |
| 29 | |
| 30 | static uint64_t rng_state; |
| 31 | |
| 32 | static uint64_t next_u64(void) { |
| 33 | rng_state += 0x9E3779B97F4A7C15ull; |
| 34 | uint64_t z = rng_state; |
| 35 | z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ull; |
| 36 | z = (z ^ (z >> 27)) * 0x94D049BB133111EBull; |
| 37 | return z ^ (z >> 31); |
| 38 | } |
| 39 | |
| 40 | static float next_bf16_value(void) { |
| 41 | return (float)((int64_t)(next_u64() % 511ull) - 255) / 64.0f; |
| 42 | } |
| 43 | |
| 44 | static float next_f32_value(void) { |
| 45 | return (float)((int64_t)(next_u64() % 4097ull) - 2048) / 1024.0f; |
| 46 | } |
| 47 | |
| 48 | static double now_s(void) { |
| 49 | struct timespec ts; |
| 50 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 51 | return (double)ts.tv_sec + (double)ts.tv_nsec / 1e9; |
| 52 | } |
| 53 | |
| 54 | static uint32_t uarg(const char *s) { |
| 55 | char *e = NULL; |
| 56 | unsigned long v = strtoul(s, &e, 0); |
| 57 | return (e && *e == 0) ? (uint32_t)v : 0xffffffffu; |
| 58 | } |
| 59 | |
| 60 | static int write_bin(const char *prefix, const float *x, size_t n) { |
| 61 | char p[1024]; |
| 62 | snprintf(p, sizeof p, "%s.bin", prefix); |
| 63 | FILE *f = fopen(p, "wb"); |
| 64 | if (!f) return -1; |
| 65 | size_t wrote = fwrite(x, sizeof(float), n, f); |
| 66 | return (wrote == n && fclose(f) == 0) ? 0 : -1; |
| 67 | } |
| 68 | |
| 69 | static void hex32(const uint8_t d[32], char out[65]) { |
| 70 | static const char h[] = "0123456789abcdef"; |
| 71 | for (int i = 0; i < 32; i++) { out[2 * i] = h[d[i] >> 4]; out[2 * i + 1] = h[d[i] & 15]; } |
| 72 | out[64] = 0; |
| 73 | } |
| 74 | |
| 75 | static int write_meta(const char *prefix, const char *runner, const char *dev, const char *win, |
| 76 | const char *shape, const char *contract, double t, const char *fields, const float *out, size_t n) { |
| 77 | char p[1024], h[65]; |
| 78 | uint8_t d[32]; |
| 79 | sha256_hash((const uint8_t *)out, n * sizeof(float), d); |
| 80 | hex32(d, h); |
| 81 | snprintf(p, sizeof p, "%s.json", prefix); |
| 82 | FILE *f = fopen(p, "wb"); |
| 83 | if (!f) return -1; |
| 84 | fprintf(f, "{\n \"runner\": \"%s\",\n \"device\": \"%s\",\n \"shape\": %s,\n" |
| 85 | " \"contract_id\": \"%s\",\n" |
| 86 | " \"seconds\": %.9f,\n \"spark_window\": %s%s%s,\n \"output_sha256\": \"%s\"%s\n}\n", |
| 87 | runner, dev, shape, contract, t, |
| 88 | win ? "\"" : "", win ? win : "null", win ? "\"" : "", |
| 89 | h, fields); |
| 90 | return fclose(f) == 0 ? 0 : -1; |
| 91 | } |
| 92 | |
| 93 | int main(int argc, char **argv) { |
| 94 | if (argc < 3) { |
| 95 | fprintf(stderr, "usage: omega_runner matmul|attention ... --out PREFIX [--device]\n"); |
| 96 | return 2; |
| 97 | } |
| 98 | const char *op = argv[1]; |
| 99 | const char *out = NULL; |
| 100 | int device = 0; |
| 101 | uint32_t m = 0, k = 0, n = 0, base = 0, rows = 0, hq = 0, hkv = 0, hd = 0; |
| 102 | uint64_t seed = 0; |
| 103 | for (int i = 2; i < argc; i++) { |
| 104 | if (strcmp(argv[i], "--out") == 0 && i + 1 < argc) out = argv[++i]; |
| 105 | else if (strcmp(argv[i], "--device") == 0) device = 1; |
| 106 | else if (strcmp(argv[i], "--m") == 0 && i + 1 < argc) m = uarg(argv[++i]); |
| 107 | else if (strcmp(argv[i], "--k") == 0 && i + 1 < argc) k = uarg(argv[++i]); |
| 108 | else if (strcmp(argv[i], "--n") == 0 && i + 1 < argc) n = uarg(argv[++i]); |
| 109 | else if (strcmp(argv[i], "--base") == 0 && i + 1 < argc) base = uarg(argv[++i]); |
| 110 | else if (strcmp(argv[i], "--rows") == 0 && i + 1 < argc) rows = uarg(argv[++i]); |
| 111 | else if (strcmp(argv[i], "--hq") == 0 && i + 1 < argc) hq = uarg(argv[++i]); |
| 112 | else if (strcmp(argv[i], "--hkv") == 0 && i + 1 < argc) hkv = uarg(argv[++i]); |
| 113 | else if (strcmp(argv[i], "--hd") == 0 && i + 1 < argc) hd = uarg(argv[++i]); |
| 114 | else if (strcmp(argv[i], "--seed") == 0 && i + 1 < argc) seed = strtoull(argv[++i], NULL, 10); |
| 115 | else { fprintf(stderr, "unknown argument %s\n", argv[i]); return 2; } |
| 116 | } |
| 117 | if (!out || m == 0xffffffffu || k == 0xffffffffu || n == 0xffffffffu) { |
| 118 | fprintf(stderr, "missing --out or invalid numeric argument\n"); |
| 119 | return 2; |
| 120 | } |
| 121 | const char *win = getenv("SPARK_WINDOW"); |
| 122 | if (device && !win) { |
| 123 | fprintf(stderr, "device run refused: SPARK_WINDOW is not set (no authorized window)\n"); |
| 124 | return 3; |
| 125 | } |
| 126 | if (strcmp(op, "matmul") == 0) { |
| 127 | if (!m || !k || !n) { fprintf(stderr, "matmul needs --m --k --n\n"); return 2; } |
| 128 | uint16_t *a = malloc((size_t)m * k * 2), *b = malloc((size_t)k * n * 2); |
| 129 | float *c = malloc((size_t)m * n * 4); |
| 130 | if (!a || !b || !c) return 4; |
| 131 | rng_state = seed; |
| 132 | for (size_t i = 0; i < (size_t)m * k; i++) a[i] = omega_fp32_to_bf16(next_bf16_value()); |
| 133 | for (size_t i = 0; i < (size_t)k * n; i++) b[i] = omega_fp32_to_bf16(next_bf16_value()); |
| 134 | double t0 = now_s(); |
| 135 | int rc; |
| 136 | char fields[512] = ""; |
| 137 | if (device) { |
| 138 | OmegaGpuMatmulInfo info; |
| 139 | memset(&info, 0, sizeof info); |
| 140 | rc = omega_gpu_matmul_bf16(m, k, n, a, b, c, &info); |
| 141 | snprintf(fields, sizeof fields, |
| 142 | ",\n \"omega_rc\": %d,\n \"target_chip\": \"%s\",\n \"sm_architecture\": %u,\n" |
| 143 | " \"call_ns\": %llu,\n \"parity_verified\": %s,\n \"max_abs_err\": %.9g", |
| 144 | rc, info.target_chip, info.sm_architecture, |
| 145 | (unsigned long long)info.call_ns, info.parity_verified ? "true" : "false", |
| 146 | (double)info.max_abs_err); |
| 147 | if (rc != OMEGA_GPU_MATMUL_OK) { fprintf(stderr, "matmul rc=%d\n", rc); return 5; } |
| 148 | } else { |
| 149 | rc = omega_matmul_cpu_oracle_bf16(a, b, c, m, k, n); |
| 150 | if (rc != 0) { fprintf(stderr, "host oracle rc=%d\n", rc); return 5; } |
| 151 | snprintf(fields, sizeof fields, ",\n \"omega_rc\": %d,\n \"host_wiring_only\": true", rc); |
| 152 | } |
| 153 | double t = now_s() - t0; |
| 154 | if (write_bin(out, c, (size_t)m * n)) return 4; |
| 155 | char shape[128]; |
| 156 | snprintf(shape, sizeof shape, "[%u, %u]", m, n); |
| 157 | if (write_meta(out, "omega-native", device ? "gpu" : "host-wiring", win, shape, "647116fc1ba1e1394982fc8ba181ce565293f160a6b56afc0f02b2128a675704", t, fields, c, (size_t)m * n)) return 4; |
| 158 | free(a); free(b); free(c); |
| 159 | return 0; |
| 160 | } else if (strcmp(op, "attention") == 0) { |
| 161 | if (!rows || !hq || !hkv || !hd) { fprintf(stderr, "attention needs --rows --hq --hkv --hd\n"); return 2; } |
| 162 | if (!device) { fprintf(stderr, "attention host path NOT_RUN\n"); return 4; } |
| 163 | uint32_t t = base + rows; |
| 164 | float *q = malloc((size_t)rows * hq * hd * 4); |
| 165 | float *kk = malloc((size_t)t * hkv * hd * 4); |
| 166 | float *vv = malloc((size_t)t * hkv * hd * 4); |
| 167 | float *o = malloc((size_t)rows * hq * hd * 4); |
| 168 | if (!q || !kk || !vv || !o) return 4; |
| 169 | rng_state = seed; |
| 170 | for (size_t i = 0; i < (size_t)rows * hq * hd; i++) q[i] = next_f32_value(); |
| 171 | for (size_t i = 0; i < (size_t)t * hkv * hd; i++) kk[i] = next_f32_value(); |
| 172 | for (size_t i = 0; i < (size_t)t * hkv * hd; i++) vv[i] = next_f32_value(); |
| 173 | double t0 = now_s(); |
| 174 | OmegaGpuAttnInfo info; |
| 175 | memset(&info, 0, sizeof info); |
| 176 | int rc = omega_gpu_gqa_attention_rows_f32(q, kk, vv, base, rows, hq, hkv, hd, o, &info); |
| 177 | double secs = now_s() - t0; |
| 178 | if (rc != OMEGA_GPU_ATTN_OK) { fprintf(stderr, "attention rc=%d\n", rc); return 5; } |
| 179 | char fields[512]; |
| 180 | snprintf(fields, sizeof fields, |
| 181 | ",\n \"omega_rc\": %d,\n \"target_chip\": \"%s\",\n \"sm_architecture\": %u,\n" |
| 182 | " \"call_ns\": %llu,\n \"gpr_count\": %u,\n \"insn_count\": %u", |
| 183 | rc, info.target_chip, info.sm_architecture, (unsigned long long)info.call_ns, |
| 184 | info.gpr_count, info.insn_count); |
| 185 | if (write_bin(out, o, (size_t)rows * hq * hd)) return 4; |
| 186 | char shape[128]; |
| 187 | snprintf(shape, sizeof shape, "[%u, %u, %u]", rows, hq, hd); |
| 188 | if (write_meta(out, "omega-native", "gpu", win, shape, "003696e422bba7274877ba9e01a61076ea0f862f32760cbaa06c60f85a115359", secs, fields, o, (size_t)rows * hq * hd)) return 4; |
| 189 | free(q); free(kk); free(vv); free(o); |
| 190 | return 0; |
| 191 | } |
| 192 | fprintf(stderr, "unknown op %s\n", op); |
| 193 | return 2; |
| 194 | } |