interplane.Get Omega
Read in your language

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

branch main Branches Tags History

CUDApp / tools / artifact_export.c

336 lines · 17337 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* artifact-export: offline handoff for one CUDApp-generated GPU realization.
3 *
4 * artifact-export matmul --m M --k K --n N --contract-id HEX --out PREFIX --commit HEX
5 * artifact-export attention --q-heads HQ --kv-heads HKV --contract-id HEX --out PREFIX --commit HEX
6 *
7 * Opens no device. Emits a cubin, zero-VA QMD templates, cbank bytes, a
8 * canonical manifest and a JSON mirror. An artifact is immutable: export
9 * refuses (I/O) when any PREFIX.* output already exists or is a symlink, and
10 * refuses a PREFIX too long for every output path.
11 * Exit: 0 ok, 2 usage, 3 refusal, 4 I/O. */
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15
16#include "omega_blackwell_codegen.h"
17#include "omega_blackwell_matmul.h"
18#include "omega_blackwell_qmd.h"
19#include "omega_gpu_attention_api.h"
20#include "omega_gpu_matmul_api.h"
21#include "omega_native_artifact.h"
22#include "sha256.h"
23#ifdef CUDAPP_MIGRATION_PENDING_C_TOOLS_ARTEXP
24static void usage(void) {
25 fprintf(stderr,
26 "usage: artifact-export matmul --m M --k K --n N --contract-id HEX --out PREFIX --commit HEX\n"
27 " artifact-export attention --q-heads H --kv-heads H --contract-id HEX --out PREFIX --commit HEX\n");
28}
29
30static uint32_t num(const char *s) {
31 char *e = NULL;
32 unsigned long v = strtoul(s, &e, 0);
33 return (e && *e == 0 && v <= 0xfffffffful) ? (uint32_t)v : 0;
34}
35
36static void hex32(const uint8_t b[32], char out[65]) {
37 static const char h[] = "0123456789abcdef";
38 for (int i = 0; i < 32; i++) { out[2 * i] = h[b[i] >> 4]; out[2 * i + 1] = h[b[i] & 15]; }
39 out[64] = 0;
40}
41
42int main(int argc, char **argv) {
43 const char *mode, *out = NULL, *contract = NULL, *commit = NULL;
44 uint32_t m = 0, k = 0, n = 0, hq = 0, hkv = 0;
45 if (argc < 2) { usage(); return 2; }
46 mode = argv[1];
47 for (int i = 2; i < argc; i++) {
48 if (strcmp(argv[i], "--out") == 0 && i + 1 < argc) out = argv[++i];
49 else if (strcmp(argv[i], "--contract-id") == 0 && i + 1 < argc) contract = argv[++i];
50 else if (strcmp(argv[i], "--commit") == 0 && i + 1 < argc) commit = argv[++i];
51 else if (strcmp(argv[i], "--m") == 0 && i + 1 < argc) m = num(argv[++i]);
52 else if (strcmp(argv[i], "--k") == 0 && i + 1 < argc) k = num(argv[++i]);
53 else if (strcmp(argv[i], "--n") == 0 && i + 1 < argc) n = num(argv[++i]);
54 else if (strcmp(argv[i], "--q-heads") == 0 && i + 1 < argc) hq = num(argv[++i]);
55 else if (strcmp(argv[i], "--kv-heads") == 0 && i + 1 < argc) hkv = num(argv[++i]);
56 else { usage(); return 2; }
57 }
58 if (!out || !contract || strlen(contract) != 64) { usage(); return 2; }
59 if (!commit || strlen(commit) != 40) { fprintf(stderr, "--commit must be a 40-hex source revision\n"); usage(); return 2; }
60 for (const char *h = commit; *h; h++) {
61 if (!((*h >= '0' && *h <= '9') || (*h >= 'a' && *h <= 'f'))) {
62 fprintf(stderr, "--commit must be lowercase 40-hex\n"); return 2;
63 }
64 }
65 /* The frozen v1 ids and bounds. An export under a frozen id but outside the
66 * frozen range would let a consumer admit an artifact the contract forbids
67 * (REVIEW C-11). */
68 if (strcmp(mode, "matmul") == 0) {
69 if (strcmp(contract, "647116fc1ba1e1394982fc8ba181ce565293f160a6b56afc0f02b2128a675704") != 0) {
70 fprintf(stderr, "contract id is not matmul-bf16-v1\n"); return 3;
71 }
72 if (m > 8192u || k > 16384u || n > 8192u) {
73 fprintf(stderr, "shape outside matmul-bf16-v1 bounds (m<=8192,k<=16384,n<=8192)\n"); return 3;
74 }
75 } else if (strcmp(mode, "attention") == 0) {
76 if (strcmp(contract, "003696e422bba7274877ba9e01a61076ea0f862f32760cbaa06c60f85a115359") != 0) {
77 fprintf(stderr, "contract id is not gqa-causal-v1\n"); return 3;
78 }
79 if (hq > 64u || hkv > 64u) {
80 fprintf(stderr, "heads outside gqa-causal-v1 bounds (hq<=64,hkv<=64)\n"); return 3;
81 }
82 }
83
84 OmegaNativeArtifact a;
85 memset(&a, 0, sizeof a);
86 OmegaBlackwellKernel kern;
87 memset(&kern, 0, sizeof kern);
88 uint32_t q0[OMEGA_ARTIFACT_QMD_WORDS], q1[OMEGA_ARTIFACT_QMD_WORDS];
89 uint32_t cbank[32];
90 OmegaCubinParam params[16];
91 size_t nparams = 0;
92 char spec_hex[65], *spec_hexp = spec_hex;
93 memset(q0, 0, sizeof q0); memset(q1, 0, sizeof q1); memset(cbank, 0, sizeof cbank);
94 OmegaBlackwellQmdConfig cfg;
95 memset(&cfg, 0, sizeof cfg);
96
97 if (strcmp(mode, "matmul") == 0) {
98 OmegaMatMulSpec spec;
99 memset(&spec, 0, sizeof spec);
100 if (!m || !k || !n) { usage(); return 2; }
101 /* Both kernels below tile C in 16 x 8 blocks and step K by 16; they have no edge handling. */
102 if (m % 16u || n % 8u || k % 16u) {
103 fprintf(stderr, "matmul-bf16-v1 export needs m%%16 == 0, n%%8 == 0, k%%16 == 0 (got %ux%ux%u)\n", m, k, n);
104 return 3;
105 }
106 if (omega_matmul_spec_init(&spec, m, k, n, OMEGA_MATMUL_PRECISION_BF16) != 0) { fprintf(stderr, "spec refused\n"); return 3; }
107 /* K = 16: the single-tile kernel (the G2 W1 artifact, evidence/MAX-INTEROP/G2). Its CTA (x, y) computes
108 * exactly the 16 x 8 tile at row 16y, column 8x and has no column loop, so grid_x must be n / 8; the
109 * exporter used to write grid_x = 1, which leaves columns 8..n-1 of C unwritten whenever n > 8.
110 * K > 16: the looped kernel (omega-mv 5e29b826, FB-1 cut 1b, chip parity 18/18), which accumulates the
111 * whole K on the chip; its CTA (x, y) owns row tile y and column tiles x, x + grid_x, ... The single-tile
112 * kernel refuses K != 16, so without the looped kernel every export above K = 16 inside the contract's
113 * k <= 16384 was refused. Both read row-major A (m x k bf16) and B (k x n bf16) and write row-major
114 * C (m x n f32) through the same three pointer words. The looped grid_x follows the matmul API's CTA
115 * budget (launch_shape). */
116 uint32_t gx = n / 8u;
117 if (k == 16u) {
118 if (omega_blackwell_codegen_matmul(&spec, &kern) != 0) { fprintf(stderr, "codegen refused\n"); return 3; }
119 } else {
120 uint32_t budget = OMEGA_GPU_MATMUL_MAX_CTAS / (m / 16u);
121 if (budget == 0) budget = 1;
122 if (gx > budget) gx = budget;
123 if (omega_blackwell_codegen_matmul_tensor_loop(&spec, gx, 0, &kern) != 0) { fprintf(stderr, "codegen refused\n"); return 3; }
124 }
125 hex32(spec.spec_id, spec_hex);
126 cfg.threads_per_block = 32; cfg.threads_x = 32;
127 cfg.grid_x = gx; cfg.grid_y = m / 16u;
128 cfg.num_elements = m * n; cfg.shared_bytes = 1024;
129 omega_blackwell_build_cbank_args_matmul(cbank, 0, 0, 0, m, k, n);
130 params[0] = (OmegaCubinParam){ 0x380, 8, 1 };
131 params[1] = (OmegaCubinParam){ 0x388, 8, 1 };
132 params[2] = (OmegaCubinParam){ 0x390, 8, 1 };
133 nparams = 3;
134 a.op = "matmul"; a.contract_name = "matmul-bf16-v1";
135 a.precision = "bf16"; a.accumulation = "fp32";
136 a.tolerance = "abs<=1e-5*max(1,abs(ref))";
137 a.grid_x = gx; a.grid_y = cfg.grid_y; a.block_x = 32; a.block_y = 1; a.block_z = 1;
138 a.shared_bytes = 1024; a.cbank_words = 10;
139 } else if (strcmp(mode, "attention") == 0) {
140 if (!hq || !hkv || hq % hkv != 0) { usage(); return 2; }
141 uint32_t ratio = hq / hkv, log2gqa = 0;
142 while ((1u << log2gqa) < ratio) log2gqa++;
143 if ((1u << log2gqa) != ratio) { fprintf(stderr, "GQA ratio is not a power of two\n"); return 3; }
144 int rc = omega_gpu_attention_codegen_hd(true, 12, log2gqa, 128, &kern);
145 if (rc != 0) { fprintf(stderr, "attention codegen refused: %d\n", rc); return 3; }
146 {
147 char specbuf[160];
148 uint8_t d[32];
149 int n = snprintf(specbuf, sizeof specbuf,
150 "omega-attention-spec/1\nhead_dim=128\nq_heads=%u\nkv_heads=%u\nlog2_gqa=%u\n",
151 hq, hkv, log2gqa);
152 sha256_hash((const uint8_t *)specbuf, (size_t)n, d);
153 for (int i = 0; i < 32; i++) sprintf(spec_hex + 2 * i, "%02x", d[i]);
154 spec_hex[64] = 0;
155 }
156 (void)spec_hexp;
157 uint32_t hpc = omega_gpu_attention_heads_per_cta(128, log2gqa);
158 uint32_t shared = (3u * hpc + 1u) * 128u * 4u;
159 cfg.threads_per_block = 128; cfg.threads_x = 128;
160 cfg.grid_x = hq / hpc; cfg.grid_y = 1;
161 cfg.num_elements = hq * 128u; cfg.shared_bytes = shared;
162 params[0] = (OmegaCubinParam){ 0x380, 8, 1 };
163 params[1] = (OmegaCubinParam){ 0x388, 8, 1 };
164 params[2] = (OmegaCubinParam){ 0x390, 8, 1 };
165 params[3] = (OmegaCubinParam){ 0x398, 8, 1 };
166 for (int i = 0; i < 8; i++) params[4 + i] = (OmegaCubinParam){ (uint32_t)(0x3a0 + 4 * i), 4, 0 };
167 nparams = 12;
168 a.op = "gqa-causal-attention"; a.contract_name = "gqa-causal-v1";
169 a.precision = "f32-kv"; a.accumulation = "fp32";
170 a.tolerance = "rel<=1e-3,abs<=1e-4";
171 a.grid_x = cfg.grid_x; a.grid_y = 1; a.block_x = 128; a.block_y = 1; a.block_z = 1;
172 a.shared_bytes = shared; a.cbank_words = 16;
173 /* Offline template: pointers and runtime strides are loader-supplied;
174 * the query-head count is a contract constant, so bind it. */
175 cbank[14] = hq;
176 } else {
177 usage(); return 2;
178 }
179 /* Declare peak + 2 rounded up to 8, as every launcher does. A cubin whose REGCOUNT is the allocator
180 * peak loads but faults at run (GB10, G2 window 20261030T235858Z-10fa8c: E5 at peak 32 gave 715,
181 * H1/H2 at 40 and H4 at 34 ran bit-exact; evidence/MAX-INTEROP/G2). */
182 cfg.gpr_count = omega_bw_gpr_declared(kern.gpr_count);
183 if (!omega_bw_gpr_declared_ok(cfg.gpr_count, kern.gpr_count)) { fprintf(stderr, "declared registers refused\n"); return 3; }
184 if (omega_blackwell_build_qmd0(q0, 0, 0) != 0 || omega_blackwell_build_qmd1(q1, &cfg) != 0) {
185 fprintf(stderr, "QMD template refused\n"); return 3;
186 }
187 static char shapebuf[128], boundsbuf[128];
188 if (strcmp(mode, "matmul") == 0) {
189 snprintf(shapebuf, sizeof shapebuf, "m=%u,k=%u,n=%u", m, k, n);
190 snprintf(boundsbuf, sizeof boundsbuf, "m<=8192,k<=16384,n<=8192");
191 } else {
192 snprintf(shapebuf, sizeof shapebuf, "r>=1,base=0,hq=%u,hkv=%u,hd=128", hq, hkv);
193 snprintf(boundsbuf, sizeof boundsbuf, "hq<=64,hkv<=64,hd=128,rows+base<=4096");
194 }
195 a.shape = shapebuf;
196 a.bounds = boundsbuf;
197 a.contract_id_hex = contract;
198 a.semantic_spec_id_hex = spec_hex;
199 a.kernel_function = (strcmp(mode, "matmul") == 0) ? "omega_matmul_bf16" : "omega_gqa_attention_causal";
200 a.text = kern.code; a.text_len = kern.code_size;
201 a.params = params; a.nparams = nparams;
202 a.gpr_count = cfg.gpr_count;
203 a.cbank_offset = 0x380;
204 a.cbank_args = cbank;
205 a.qmd0 = q0; a.qmd1 = q1;
206 a.min_driver = "580.173.02";
207 a.requires = "sm_121,nvidia-cubin-abi-v8";
208 a.source_commit = commit;
209 a.tool_version = "cudapp-artifact-export/1";
210 a.qualification = "offline-disassembly=PASS;device-load=NOT_RUN;device-exec=NOT_RUN";
211
212 char id[OMEGA_ARTIFACT_ID_MAX], err[256];
213 int rc = omega_native_artifact_export(&a, out, id, err, sizeof err);
214 if (rc != OMEGA_ARTIFACT_OK) {
215 fprintf(stderr, "artifact-export refused: %s: %s\n", omega_native_artifact_rc_name(rc), err);
216 omega_blackwell_kernel_free(&kern);
217 return 3;
218 }
219 printf("artifact_id=%s\nop=%s\ncontract_id=%s\ncubin=%s.cubin\n", id, a.op, contract, out);
220 omega_blackwell_kernel_free(&kern);
221 return 0;
222}
223#else
224/* Reading the command line, every check against the frozen v1 contracts, the kernel choice and its
225 * grid, the launch configuration and parameter table, the attention semantic-spec text, the manifest
226 * texts, every message and the exit status are om/tools/artexp.om. This shell calls the library when
227 * the unit asks and writes the unit's text. The former C above keeps its line numbers. */
228#include "cudapp_native.h"
229
230static uint8_t tok[262144], txt[262144];
231
232int main(int argc, char **argv) {
233 const uint64_t f[8] = { OMEGA_GPU_MATMUL_MAX_CTAS, OMEGA_ARTIFACT_OK };
234 uint64_t s[128];
235 OmegaMatMulSpec spec;
236 OmegaBlackwellKernel kern;
237 OmegaBlackwellQmdConfig cfg;
238 OmegaNativeArtifact a;
239 uint32_t q0[OMEGA_ARTIFACT_QMD_WORDS], q1[OMEGA_ARTIFACT_QMD_WORDS], cbank[32];
240 OmegaCubinParam params[16];
241 memset(s, 0, sizeof s); memset(&spec, 0, sizeof spec); memset(&kern, 0, sizeof kern);
242 memset(&cfg, 0, sizeof cfg); memset(&a, 0, sizeof a);
243 memset(q0, 0, sizeof q0); memset(q1, 0, sizeof q1); memset(cbank, 0, sizeof cbank);
244 memset(params, 0, sizeof params);
245 s[3] = (uint64_t)argc;
246 for (;;) {
247 uint64_t act = 0;
248 if (omega_artexp(&act, f, 8, s, 128, tok, sizeof tok, txt, sizeof txt) != 0) abort();
249 switch (act & 255) {
250 case 1: {
251 /* Linux caps one argument at MAX_ARG_STRLEN (131072 bytes with its NUL), well inside tok. */
252 const char *arg = argv[s[1]];
253 size_t n = strnlen(arg, sizeof tok);
254 if (n == sizeof tok) abort();
255 memcpy(tok, arg, n + 1);
256 s[2] = n;
257 break;
258 }
259 case 2:
260 fwrite(txt, 1, (size_t)(act >> 8), stderr);
261 omega_blackwell_kernel_free(&kern);
262 return (int)s[25];
263 case 3:
264 s[18] = (uint64_t)(int64_t)omega_matmul_spec_init(&spec, (uint32_t)s[13], (uint32_t)s[14], (uint32_t)s[15],
265 OMEGA_MATMUL_PRECISION_BF16);
266 break;
267 case 4: s[18] = (uint64_t)(int64_t)omega_blackwell_codegen_matmul(&spec, &kern); break;
268 case 5:
269 s[18] = (uint64_t)(int64_t)omega_blackwell_codegen_matmul_tensor_loop(&spec, (uint32_t)s[19], 0, &kern);
270 break;
271 case 6: memcpy(tok, spec.spec_id, 32); break;
272 case 7:
273 omega_blackwell_build_cbank_args_matmul(cbank, 0, 0, 0, (uint32_t)s[13], (uint32_t)s[14], (uint32_t)s[15]);
274 break;
275 case 8: s[18] = (uint64_t)(int64_t)omega_gpu_attention_codegen_hd(true, 12, (uint32_t)s[20], 128, &kern); break;
276 case 9: sha256_hash(txt, (size_t)(act >> 8), tok); break;
277 case 10: s[21] = omega_gpu_attention_heads_per_cta(128, (uint32_t)s[20]); break;
278 case 11: cbank[14] = (uint32_t)s[16]; break;
279 case 12:
280 s[22] = kern.gpr_count;
281 s[23] = omega_bw_gpr_declared(kern.gpr_count);
282 s[24] = omega_bw_gpr_declared_ok((uint32_t)s[23], kern.gpr_count) != 0;
283 break;
284 case 13:
285 cfg.threads_per_block = (uint32_t)s[32]; cfg.threads_x = (uint32_t)s[33];
286 cfg.grid_x = (uint32_t)s[34]; cfg.grid_y = (uint32_t)s[35];
287 cfg.num_elements = (uint32_t)s[36]; cfg.shared_bytes = (uint32_t)s[37];
288 cfg.gpr_count = (uint32_t)s[38];
289 s[18] = omega_blackwell_build_qmd0(q0, 0, 0) != 0 || omega_blackwell_build_qmd1(q1, &cfg) != 0;
290 break;
291 case 14: {
292 const char *v[13], *c = (const char *)txt;
293 for (int i = 0; i < 13; i++) { v[i] = c; c += strlen(c) + 1; }
294 a.op = v[0]; a.contract_name = v[1]; a.precision = v[2]; a.accumulation = v[3]; a.tolerance = v[4];
295 a.shape = v[5]; a.bounds = v[6]; a.semantic_spec_id_hex = v[7]; a.kernel_function = v[8];
296 a.min_driver = v[9]; a.requires = v[10]; a.tool_version = v[11]; a.qualification = v[12];
297 a.contract_id_hex = argv[s[7]];
298 a.source_commit = argv[s[10]];
299 for (uint64_t i = 0; i < s[47]; i++) {
300 uint64_t w = s[64 + i];
301 params[i] = (OmegaCubinParam){ (uint32_t)(w & 0xffff), (uint32_t)((w >> 16) & 0xffff), (uint8_t)(w >> 32) };
302 }
303 a.params = params; a.nparams = (size_t)s[47];
304 a.text = kern.code; a.text_len = kern.code_size;
305 a.gpr_count = cfg.gpr_count;
306 a.grid_x = (uint32_t)s[40]; a.grid_y = (uint32_t)s[41];
307 a.block_x = (uint32_t)s[42]; a.block_y = (uint32_t)s[43]; a.block_z = (uint32_t)s[44];
308 a.shared_bytes = (uint32_t)s[45];
309 a.cbank_offset = 0x380;
310 a.cbank_args = cbank; a.cbank_words = (size_t)s[46];
311 a.qmd0 = q0; a.qmd1 = q1;
312 char id[OMEGA_ARTIFACT_ID_MAX], err[256];
313 id[0] = 0; err[0] = 0;
314 int rc = omega_native_artifact_export(&a, argv[s[6]], id, err, sizeof err);
315 s[18] = (uint64_t)(int64_t)rc;
316 const char *part[3] = { id, argv[s[7]], argv[s[6]] };
317 if (rc != OMEGA_ARTIFACT_OK) { part[0] = omega_native_artifact_rc_name(rc); part[1] = err; }
318 size_t at = 0;
319 for (int i = 0; i < 3; i++) {
320 size_t n = strlen(part[i]);
321 if (at + n + 1 > sizeof tok) abort();
322 memcpy(tok + at, part[i], n + 1);
323 at += n + 1;
324 }
325 break;
326 }
327 case 15:
328 fwrite(txt, 1, (size_t)(act >> 8), stdout);
329 omega_blackwell_kernel_free(&kern);
330 return 0;
331 default:
332 abort();
333 }
334 }
335}
336#endif