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_artifact_export_test.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* Host-only test for the native artifact exporter and admission. |
| 3 | * |
| 4 | * It proves: byte-deterministic export, identity/digest recomputation, the |
| 5 | * cubin container's ELF and text bindings, refusal of malformed descriptors, |
| 6 | * and rejection of tampered binaries, QMDs, manifests, architectures and |
| 7 | * drivers. It never opens a device: device load/execution remains NOT_RUN and |
| 8 | * is reported by the artifact's qualification field. */ |
| 9 | #include <fcntl.h> |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <sys/stat.h> |
| 14 | #include <unistd.h> |
| 15 | |
| 16 | #include "omega_blackwell_codegen.h" |
| 17 | #include "omega_blackwell_matmul.h" |
| 18 | #include "omega_blackwell_qmd.h" |
| 19 | #include "omega_cubin.h" |
| 20 | #include "omega_gpu_attention_api.h" |
| 21 | #include "omega_native_artifact.h" |
| 22 | #include "sha256.h" |
| 23 | |
| 24 | static int checks, fails; |
| 25 | #define CHECK(cond, ...) do { checks++; if (!(cond)) { fails++; printf("FAIL %s:%d: ", __FILE__, __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0) |
| 26 | |
| 27 | static uint8_t *readf(const char *p, size_t *n) { |
| 28 | FILE *f = fopen(p, "rb"); |
| 29 | long sz; |
| 30 | uint8_t *b; |
| 31 | if (!f) return NULL; |
| 32 | fseek(f, 0, SEEK_END); sz = ftell(f); fseek(f, 0, SEEK_SET); |
| 33 | b = malloc((size_t)sz + 1); if (!b) { fclose(f); return NULL; } |
| 34 | if (sz && fread(b, 1, (size_t)sz, f) != (size_t)sz) { free(b); fclose(f); return NULL; } |
| 35 | b[sz] = 0; *n = (size_t)sz; fclose(f); return b; |
| 36 | } |
| 37 | static int writef(const char *p, const void *d, size_t n) { |
| 38 | FILE *f = NULL; int fd = open(p, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644); |
| 39 | if (fd >= 0) f = fdopen(fd, "wb"); |
| 40 | if (!f) return -1; |
| 41 | if (n && fwrite(d, 1, n, f) != n) { fclose(f); return -1; } |
| 42 | return fclose(f); |
| 43 | } |
| 44 | static int same_file(const char *a, const char *b) { |
| 45 | size_t na, nb; uint8_t *pa = readf(a, &na), *pb = readf(b, &nb); |
| 46 | int eq = pa && pb && na == nb && memcmp(pa, pb, na) == 0; |
| 47 | free(pa); free(pb); return eq; |
| 48 | } |
| 49 | |
| 50 | static int copy_artifact(const char *src, const char *dst) { |
| 51 | const char *suf[] = { ".cubin", ".text.bin", ".qmd0.bin", ".qmd1.bin", ".cbank_args.bin" }; |
| 52 | for (size_t i = 0; i < sizeof suf / sizeof suf[0]; i++) { |
| 53 | char a[600], b[600]; size_t n; |
| 54 | snprintf(a, sizeof a, "%s%s", src, suf[i]); |
| 55 | snprintf(b, sizeof b, "%s%s", dst, suf[i]); |
| 56 | uint8_t *d = readf(a, &n); |
| 57 | if (!d || writef(b, d, n)) { free(d); return -1; } |
| 58 | free(d); |
| 59 | } |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | static void hex32(const uint8_t d[32], char out[65]) { |
| 64 | static const char h[] = "0123456789abcdef"; |
| 65 | for (int i = 0; i < 32; i++) { out[2*i] = h[d[i]>>4]; out[2*i+1] = h[d[i]&15]; } |
| 66 | out[64] = 0; |
| 67 | } |
| 68 | |
| 69 | /* --- minimal ELF reader (little-endian, 64-bit) --- */ |
| 70 | static uint16_t g16(const uint8_t *p) { return (uint16_t)(p[0] | (p[1] << 8)); } |
| 71 | static uint32_t g32(const uint8_t *p) { return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24); } |
| 72 | static uint64_t g64(const uint8_t *p) { return (uint64_t)g32(p) | ((uint64_t)g32(p + 4) << 32); } |
| 73 | |
| 74 | static int elf_section(const uint8_t *e, size_t n, const char *want, const uint8_t **data, size_t *len) { |
| 75 | uint64_t shoff; uint16_t shentsize, shnum, shstrndx; |
| 76 | const uint8_t *sh, *st; size_t stlen; |
| 77 | if (n < 64 || memcmp(e, "\177ELF", 4) != 0) return -1; |
| 78 | shoff = g64(e + 40); shentsize = g16(e + 58); shnum = g16(e + 60); shstrndx = g16(e + 62); |
| 79 | if (shentsize != 64 || shstrndx >= shnum || shoff + (uint64_t)shentsize * shnum > n) return -1; |
| 80 | sh = e + shoff; |
| 81 | st = e + g64(sh + (uint64_t)shstrndx * 64 + 24); |
| 82 | stlen = (size_t)g64(sh + (uint64_t)shstrndx * 64 + 32); |
| 83 | for (uint16_t i = 0; i < shnum; i++) { |
| 84 | const uint8_t *s = sh + (uint64_t)i * 64; |
| 85 | uint32_t no = g32(s); |
| 86 | if (no < stlen && strcmp((const char *)st + no, want) == 0) { |
| 87 | uint64_t off = g64(s + 24), sz = g64(s + 32); |
| 88 | if (off + sz > n) return -1; |
| 89 | *data = e + off; *len = (size_t)sz; return 0; |
| 90 | } |
| 91 | } |
| 92 | return -1; |
| 93 | } |
| 94 | |
| 95 | /* Rewrite one manifest value and recompute artifact_id over the identity block. */ |
| 96 | static int rewrite_manifest(const char *src, const char *dst, const char *key, const char *val) { |
| 97 | size_t n; uint8_t *m = readf(src, &n); char *out; size_t cap = n + strlen(val) + 128, o = 0; |
| 98 | char *p, *idline, *ident, *prov; |
| 99 | if (!m) return -1; |
| 100 | out = malloc(cap); if (!out) { free(m); return -1; } |
| 101 | idline = strstr((char *)m, "artifact_id="); |
| 102 | ident = strstr((char *)m, "\nop="); if (ident) ident++; |
| 103 | prov = strstr((char *)m, "provenance_tool="); |
| 104 | if (!idline || !ident || !prov || prov <= ident) { free(m); free(out); return -1; } |
| 105 | { |
| 106 | size_t klen = strlen(key), vlen = strlen(val); |
| 107 | p = (char *)m; |
| 108 | while (*p) { |
| 109 | char *nl = strchr(p, '\n'); size_t len = nl ? (size_t)(nl - p) : strlen(p); |
| 110 | if ((size_t)len > klen && strncmp(p, key, klen) == 0 && p[klen] == '=') { |
| 111 | memcpy(out + o, key, klen); o += klen; out[o++] = '='; |
| 112 | memcpy(out + o, val, vlen); o += vlen; out[o++] = '\n'; |
| 113 | } else { |
| 114 | memcpy(out + o, p, len); o += len; out[o++] = '\n'; |
| 115 | } |
| 116 | if (!nl) break; |
| 117 | p = nl + 1; |
| 118 | } |
| 119 | (void)vlen; |
| 120 | } |
| 121 | { |
| 122 | uint8_t d[32]; char id[65]; |
| 123 | char *ni = strstr(out, "\nop="); if (ni) ni++; |
| 124 | char *np = strstr(out, "provenance_tool="); |
| 125 | size_t ilen = np && ni ? (size_t)(np - ni) : 0; |
| 126 | char *idp = strstr(out, "artifact_id="); |
| 127 | sha256_hash((const uint8_t *)ni, ilen, d); hex32(d, id); |
| 128 | if (idp) memcpy(idp + strlen("artifact_id="), id, 64); |
| 129 | (void)idline; |
| 130 | } |
| 131 | { |
| 132 | int rc = writef(dst, out, o); |
| 133 | free(m); free(out); return rc; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /* Edit one line and deliberately leave artifact_id untouched: this is the |
| 138 | * naive NOT_RUN -> PASS tamper the artifact id must catch. */ |
| 139 | static int rewrite_manifest_raw(const char *src, const char *dst, const char *key, const char *val) { |
| 140 | size_t n; uint8_t *m = readf(src, &n); char *out; size_t cap = n + strlen(val) + 128, o = 0; |
| 141 | char *p; |
| 142 | if (!m) return -1; |
| 143 | out = malloc(cap); if (!out) { free(m); return -1; } |
| 144 | size_t klen = strlen(key), vlen = strlen(val); |
| 145 | p = (char *)m; |
| 146 | while (*p) { |
| 147 | char *nl = strchr(p, '\n'); size_t len = nl ? (size_t)(nl - p) : strlen(p); |
| 148 | if ((size_t)len > klen && strncmp(p, key, klen) == 0 && p[klen] == '=') { |
| 149 | memcpy(out + o, key, klen); o += klen; out[o++] = '='; |
| 150 | memcpy(out + o, val, vlen); o += vlen; out[o++] = '\n'; |
| 151 | } else { |
| 152 | memcpy(out + o, p, len); o += len; out[o++] = '\n'; |
| 153 | } |
| 154 | if (!nl) break; |
| 155 | p = nl + 1; |
| 156 | } |
| 157 | int rc = writef(dst, out, o); |
| 158 | free(m); free(out); return rc; |
| 159 | } |
| 160 | |
| 161 | static void build_matmul(OmegaBlackwellKernel *k, uint32_t q0[96], uint32_t q1[96], uint32_t cb[10], |
| 162 | OmegaCubinParam ps[3], OmegaBlackwellQmdConfig *cfg, char spec_hex[65]) { |
| 163 | OmegaMatMulSpec spec; |
| 164 | memset(&spec, 0, sizeof spec); |
| 165 | memset(k, 0, sizeof *k); |
| 166 | memset(q0, 0, 96 * 4); memset(q1, 0, 96 * 4); memset(cb, 0, 10 * 4); |
| 167 | omega_matmul_spec_init(&spec, 16, 16, 8, OMEGA_MATMUL_PRECISION_BF16); |
| 168 | omega_blackwell_codegen_matmul(&spec, k); |
| 169 | hex32(spec.spec_id, spec_hex); |
| 170 | omega_blackwell_build_qmd0(q0, 0, 0); |
| 171 | memset(cfg, 0, sizeof *cfg); |
| 172 | cfg->gpr_count = omega_bw_gpr_declared(k->gpr_count); cfg->threads_per_block = 32; cfg->threads_x = 32; |
| 173 | cfg->grid_x = 1; cfg->grid_y = 1; cfg->num_elements = 16 * 8; cfg->shared_bytes = 1024; |
| 174 | omega_blackwell_build_qmd1(q1, cfg); |
| 175 | omega_blackwell_build_cbank_args_matmul(cb, 0, 0, 0, 16, 16, 8); |
| 176 | ps[0] = (OmegaCubinParam){ 0x380, 8, 1 }; |
| 177 | ps[1] = (OmegaCubinParam){ 0x388, 8, 1 }; |
| 178 | ps[2] = (OmegaCubinParam){ 0x390, 8, 1 }; |
| 179 | } |
| 180 | |
| 181 | static void fill_artifact(OmegaNativeArtifact *a, const char *contract, const char *spec_hex, |
| 182 | OmegaBlackwellKernel *k, OmegaCubinParam *ps, size_t nps, |
| 183 | uint32_t *cb, size_t cbn, uint32_t *q0, uint32_t *q1, |
| 184 | const char *op, const char *name, const char *fn, |
| 185 | uint32_t gx, uint32_t gy, uint32_t bx, uint32_t shared) { |
| 186 | memset(a, 0, sizeof *a); |
| 187 | a->op = op; a->shape = "m=16,k=16,n=8"; a->bounds = "m<=8192,k<=16384,n<=8192"; |
| 188 | a->contract_name = name; a->contract_id_hex = contract; |
| 189 | a->semantic_spec_id_hex = spec_hex; |
| 190 | a->kernel_function = fn; a->precision = "bf16"; a->accumulation = "fp32"; |
| 191 | a->tolerance = "abs<=1e-5*max(1,abs(ref))"; |
| 192 | a->text = k->code; a->text_len = k->code_size; |
| 193 | a->params = ps; a->nparams = nps; |
| 194 | a->gpr_count = omega_bw_gpr_declared(k->gpr_count); /* declared, as tools/artifact_export.c */ |
| 195 | a->grid_x = gx; a->grid_y = gy; a->block_x = bx; a->block_y = 1; a->block_z = 1; |
| 196 | a->shared_bytes = shared; a->cbank_offset = 0x380; |
| 197 | a->cbank_args = cb; a->cbank_words = cbn; |
| 198 | a->qmd0 = q0; a->qmd1 = q1; |
| 199 | a->min_driver = "580.173.02"; a->requires = "sm_121,nvidia-cubin-abi-v8"; |
| 200 | a->source_commit = "UNKNOWN"; a->tool_version = "cudapp-artifact-export/1"; |
| 201 | a->qualification = "offline-disassembly=PASS;device-load=NOT_RUN;device-exec=NOT_RUN"; |
| 202 | } |
| 203 | |
| 204 | int main(int argc, char **argv) { |
| 205 | const char *dir = argc > 1 ? argv[1] : "/tmp/artifact-test"; |
| 206 | char p1[512], p2[512], path[600], path2[600], id[65], err[256]; |
| 207 | char contract[65]; |
| 208 | memset(contract, 'a', 64); contract[64] = 0; |
| 209 | mkdir(dir, 0700); |
| 210 | snprintf(p1, sizeof p1, "%s/m1", dir); |
| 211 | snprintf(p2, sizeof p2, "%s/m2", dir); |
| 212 | |
| 213 | OmegaBlackwellKernel k; OmegaCubinParam ps[3]; OmegaBlackwellQmdConfig cfg; |
| 214 | uint32_t q0[96], q1[96], cb[10]; char spec_hex[65]; |
| 215 | build_matmul(&k, q0, q1, cb, ps, &cfg, spec_hex); |
| 216 | CHECK(k.code_size > 0 && k.gpr_count > 0, "matmul codegen failed"); |
| 217 | |
| 218 | OmegaNativeArtifact a; |
| 219 | fill_artifact(&a, contract, spec_hex, &k, ps, 3, cb, 10, q0, q1, |
| 220 | "matmul", "matmul-bf16-v1", "omega_matmul_bf16", 1, 1, 32, 1024); |
| 221 | int rc = omega_native_artifact_export(&a, p1, id, err, sizeof err); |
| 222 | CHECK(rc == OMEGA_ARTIFACT_OK, "export matmul: %s %s", omega_native_artifact_rc_name(rc), err); |
| 223 | if (rc != OMEGA_ARTIFACT_OK) { printf("%d checks, %d failed\n", checks, fails); return 1; } |
| 224 | { |
| 225 | char rid[65]; int r2 = omega_native_artifact_admit(p1, "580.173.02", rid, err, sizeof err); |
| 226 | CHECK(r2 == OMEGA_ARTIFACT_OK && strcmp(rid, id) == 0, "admit fresh: %s %s", omega_native_artifact_rc_name(r2), err); |
| 227 | } |
| 228 | |
| 229 | /* Determinism. */ |
| 230 | { |
| 231 | int r2 = omega_native_artifact_export(&a, p2, id, err, sizeof err); |
| 232 | CHECK(r2 == OMEGA_ARTIFACT_OK, "second export: %s", err); |
| 233 | const char *suf[] = { ".cubin", ".text.bin", ".qmd0.bin", ".qmd1.bin", ".cbank_args.bin", ".manifest", ".json", ".sha256" }; |
| 234 | for (size_t i = 0; i < sizeof suf / sizeof suf[0]; i++) { |
| 235 | snprintf(path, sizeof path, "%s%s", p1, suf[i]); |
| 236 | snprintf(path2, sizeof path2, "%s%s", p2, suf[i]); |
| 237 | CHECK(same_file(path, path2), "non-deterministic %s", suf[i]); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /* Cubin container bindings. */ |
| 242 | { |
| 243 | size_t n; uint8_t *e = readf((snprintf(path, sizeof path, "%s.cubin", p1), path), &n); |
| 244 | const uint8_t *sec; size_t slen; |
| 245 | CHECK(e && n > 64, "cubin readable"); |
| 246 | if (e) { |
| 247 | CHECK(g16(e + 18) == 190, "e_machine %u != EM_CUDA", g16(e + 18)); |
| 248 | CHECK(g32(e + 48) == 0x6007902u, "wrong e_flags 0x%x", g32(e + 48)); |
| 249 | CHECK(elf_section(e, n, ".text.omega_matmul_bf16", &sec, &slen) == 0 && |
| 250 | slen == k.code_size && memcmp(sec, k.code, slen) == 0, "cubin text does not bind the generated bytes"); |
| 251 | CHECK(elf_section(e, n, ".nv.info.omega_matmul_bf16", &sec, &slen) == 0, "missing .nv.info function section"); |
| 252 | free(e); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | /* Tampered files. */ |
| 257 | { |
| 258 | uint8_t *b; size_t n; |
| 259 | b = readf((snprintf(path, sizeof path, "%s.cubin", p1), path), &n); |
| 260 | b[n / 2] ^= 0x5a; writef(path, b, n); |
| 261 | snprintf(path2, sizeof path2, "%s.cubin", p2); |
| 262 | CHECK(omega_native_artifact_admit(p1, "580.173.02", id, err, sizeof err) == OMEGA_ARTIFACT_E_DIGEST, "tampered cubin accepted"); |
| 263 | b = readf(path2, &n); writef(path, b, n); free(b); |
| 264 | } |
| 265 | { |
| 266 | uint8_t *b; size_t n; |
| 267 | b = readf((snprintf(path, sizeof path, "%s.text.bin", p1), path), &n); b[0] ^= 0x01; writef(path, b, n); |
| 268 | CHECK(omega_native_artifact_admit(p1, "580.173.02", id, err, sizeof err) == OMEGA_ARTIFACT_E_DIGEST, "tampered text accepted"); |
| 269 | snprintf(path2, sizeof path2, "%s.text.bin", p2); b = readf(path2, &n); writef(path, b, n); free(b); |
| 270 | } |
| 271 | { |
| 272 | uint8_t *b; size_t n; |
| 273 | b = readf((snprintf(path, sizeof path, "%s.qmd1.bin", p1), path), &n); b[32] ^= 0x01; writef(path, b, n); |
| 274 | CHECK(omega_native_artifact_admit(p1, "580.173.02", id, err, sizeof err) == OMEGA_ARTIFACT_E_DIGEST, "tampered qmd accepted"); |
| 275 | snprintf(path2, sizeof path2, "%s.qmd1.bin", p2); b = readf(path2, &n); writef(path, b, n); free(b); |
| 276 | } |
| 277 | |
| 278 | /* Structured manifest refusals with a recomputed id (digests still match). */ |
| 279 | { |
| 280 | char m0[600], bad[600], badman[620]; |
| 281 | snprintf(m0, sizeof m0, "%s.manifest", p1); |
| 282 | snprintf(bad, sizeof bad, "%s/bad", dir); |
| 283 | snprintf(badman, sizeof badman, "%s.manifest", bad); |
| 284 | CHECK(copy_artifact(p1, bad) == 0, "copy artifact for structured tests"); |
| 285 | CHECK(rewrite_manifest(m0, badman, "isa", "sm_120") == 0, "rewrite isa"); |
| 286 | CHECK(omega_native_artifact_admit(bad, "580.173.02", id, err, sizeof err) == OMEGA_ARTIFACT_E_ISA, "wrong ISA accepted"); |
| 287 | CHECK(rewrite_manifest(m0, badman, "qmd_template", "live-va-v1") == 0, "rewrite qmd_template"); |
| 288 | CHECK(omega_native_artifact_admit(bad, "580.173.02", id, err, sizeof err) == OMEGA_ARTIFACT_E_RELOC, "live-VA QMD accepted"); |
| 289 | CHECK(rewrite_manifest(m0, badman, "kernel_format", "ptx") == 0, "rewrite kernel_format"); |
| 290 | CHECK(omega_native_artifact_admit(bad, "580.173.02", id, err, sizeof err) == OMEGA_ARTIFACT_E_FORMAT, "PTX kernel accepted"); |
| 291 | CHECK(rewrite_manifest(m0, badman, "min_driver", "999.0.0") == 0, "rewrite min_driver"); |
| 292 | CHECK(omega_native_artifact_admit(bad, "580.173.02", id, err, sizeof err) == OMEGA_ARTIFACT_E_DRIVER, "too-new driver requirement accepted"); |
| 293 | CHECK(rewrite_manifest(m0, badman, "contract_id", "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz") == 0, "rewrite contract"); |
| 294 | CHECK(omega_native_artifact_admit(bad, "580.173.02", id, err, sizeof err) == OMEGA_ARTIFACT_E_CONTRACT, "malformed contract id accepted"); |
| 295 | CHECK(rewrite_manifest_raw(m0, badman, "qualification", |
| 296 | "offline-disassembly=PASS;device-load=PASS;device-exec=PASS") == 0, "raw qualification rewrite"); |
| 297 | CHECK(omega_native_artifact_admit(bad, "580.173.02", id, err, sizeof err) == OMEGA_ARTIFACT_E_DIGEST, |
| 298 | "unhashed NOT_RUN -> PASS tamper accepted"); |
| 299 | } |
| 300 | |
| 301 | /* Export refusals. */ |
| 302 | { |
| 303 | char c2[65]; memcpy(c2, contract, 65); c2[63] = 0; |
| 304 | a.contract_id_hex = c2; |
| 305 | CHECK(omega_native_artifact_export(&a, p2, id, err, sizeof err) == OMEGA_ARTIFACT_E_CONTRACT, "short contract id accepted"); |
| 306 | a.contract_id_hex = contract; |
| 307 | a.op = "gemm"; |
| 308 | CHECK(omega_native_artifact_export(&a, p2, id, err, sizeof err) == OMEGA_ARTIFACT_E_UNSUPPORTED, "unknown op accepted"); |
| 309 | a.op = "matmul"; |
| 310 | q1[32] = 0x1234; q1[33] = 0; |
| 311 | CHECK(omega_native_artifact_export(&a, p2, id, err, sizeof err) == OMEGA_ARTIFACT_E_RELOC, "live code VA accepted"); |
| 312 | q1[32] = 0; q1[33] = 0xa << 21; |
| 313 | q1[56] = 0x12345678; |
| 314 | CHECK(omega_native_artifact_export(&a, p2, id, err, sizeof err) == OMEGA_ARTIFACT_E_RELOC, "live scratch VA accepted"); |
| 315 | q1[56] = 0; q1[57] |= 1u; |
| 316 | CHECK(omega_native_artifact_export(&a, p2, id, err, sizeof err) == OMEGA_ARTIFACT_E_RELOC, "live scratch high VA accepted"); |
| 317 | q1[57] &= ~0x7ffffu; q1[59] = 0xdeadbeefu; |
| 318 | CHECK(omega_native_artifact_export(&a, p2, id, err, sizeof err) == OMEGA_ARTIFACT_E_RELOC, "live prefetch VA accepted"); |
| 319 | q1[59] = 0; |
| 320 | } |
| 321 | { |
| 322 | uint8_t *out = NULL; size_t outn = 0; |
| 323 | OmegaCubinParam badp[2] = { { 0x380, 8, 1 }, { 0x384, 8, 1 } }; |
| 324 | CHECK(omega_cubin_pack("k", k.code, k.code_size, badp, 2, 0x398, k.gpr_count, 64, &out, &outn, err, sizeof err) == OMEGA_CUBIN_E_PARAMS, "overlapping params accepted"); |
| 325 | CHECK(omega_cubin_pack("k", k.code, k.code_size - 1, ps, 3, 0x398, k.gpr_count, 64, &out, &outn, err, sizeof err) == OMEGA_CUBIN_E_TEXT, "unaligned text accepted"); |
| 326 | CHECK(omega_cubin_pack("k", k.code, k.code_size, ps, 3, 0x398, 300, 64, &out, &outn, err, sizeof err) == OMEGA_CUBIN_E_GPR, "gpr 300 accepted"); |
| 327 | CHECK(omega_cubin_pack("k", k.code, k.code_size, ps, 3, 0x390, k.gpr_count, 64, &out, &outn, err, sizeof err) == OMEGA_CUBIN_E_CBANK, "too-small cbank accepted"); |
| 328 | CHECK(omega_cubin_pack("1bad", k.code, k.code_size, ps, 3, 0x398, k.gpr_count, 64, &out, &outn, err, sizeof err) == OMEGA_CUBIN_E_ARG, "bad function name accepted"); |
| 329 | } |
| 330 | |
| 331 | /* Attention artifact: the exact kernel the rows API builds (offline codegen). */ |
| 332 | { |
| 333 | OmegaBlackwellKernel ak; memset(&ak, 0, sizeof ak); |
| 334 | int arc = omega_gpu_attention_codegen_hd(true, 12, 3, 128, &ak); |
| 335 | CHECK(arc == 0 && ak.code_size > 0, "attention codegen failed: %d", arc); |
| 336 | if (arc == 0) { |
| 337 | uint32_t aq0[96], aq1[96], acb[16]; |
| 338 | OmegaCubinParam aps[12]; |
| 339 | OmegaBlackwellQmdConfig acfg; |
| 340 | char aspec[65]; |
| 341 | memset(aq0, 0, sizeof aq0); memset(aq1, 0, sizeof aq1); memset(acb, 0, sizeof acb); |
| 342 | memset(&acfg, 0, sizeof acfg); |
| 343 | memset(aspec, 'b', 64); aspec[64] = 0; |
| 344 | omega_blackwell_build_qmd0(aq0, 0, 0); |
| 345 | acfg.gpr_count = omega_bw_gpr_declared(ak.gpr_count); acfg.threads_per_block = 128; acfg.threads_x = 128; |
| 346 | acfg.grid_x = 2; acfg.grid_y = 1; acfg.num_elements = 8 * 128; acfg.shared_bytes = 6656; |
| 347 | omega_blackwell_build_qmd1(aq1, &acfg); |
| 348 | for (int i = 0; i < 4; i++) aps[i] = (OmegaCubinParam){ (uint32_t)(0x380 + 8 * i), 8, 1 }; |
| 349 | for (int i = 0; i < 8; i++) aps[4 + i] = (OmegaCubinParam){ (uint32_t)(0x3a0 + 4 * i), 4, 0 }; |
| 350 | snprintf(p2, sizeof p2, "%s/a1", dir); |
| 351 | fill_artifact(&a, contract, aspec, &ak, aps, 12, acb, 16, aq0, aq1, |
| 352 | "gqa-causal-attention", "gqa-causal-v1", "omega_gqa_attention_causal", 2, 1, 128, 6656); |
| 353 | a.requires = "sm_121,nvidia-cubin-abi-v8"; |
| 354 | int r2 = omega_native_artifact_export(&a, p2, id, err, sizeof err); |
| 355 | CHECK(r2 == OMEGA_ARTIFACT_OK, "attention export: %s %s", omega_native_artifact_rc_name(r2), err); |
| 356 | if (r2 == OMEGA_ARTIFACT_OK) { |
| 357 | int r3 = omega_native_artifact_admit(p2, "580.173.02", id, err, sizeof err); |
| 358 | CHECK(r3 == OMEGA_ARTIFACT_OK, "attention admit: %s %s", omega_native_artifact_rc_name(r3), err); |
| 359 | } |
| 360 | omega_blackwell_kernel_free(&ak); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | omega_blackwell_kernel_free(&k); |
| 365 | printf("test_gpu_artifact_export: %d checks, %d failed\n", checks, fails); |
| 366 | return fails ? 1 : 0; |
| 367 | } |