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 / src / omega_native_artifact_om.inc

203 lines · 10948 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* The default artifact implementation. All canonical bytes and admission
3 * decisions come from seeded Omega. This file owns pointers, memory and I/O. */
4static const char *const af_suffix[] = {".manifest", ".text.bin", ".cubin", ".qmd0.bin", ".qmd1.bin", ".cbank_args.bin", ".json", ".sha256"};
5typedef struct { unsigned char b[65536]; uint64_t s[128]; } ArtifactWire;
6const char *omega_native_artifact_rc_name(int rc) {
7 static const char *const names[] = {"OK","ARG","FORMAT","CONTRACT","ISA","DIGEST","IO","RELOC","DRIVER","UNSUPPORTED","MEM"};
8 return rc >= 0 && (size_t)rc < sizeof names / sizeof names[0] ? names[rc] : "UNKNOWN";
9}
10static int af_paths(char path[8][1024], const char *prefix, unsigned count) {
11 for (unsigned i = 0; i < count; ++i) {
12 int n = snprintf(path[i], 1024, "%s%s", prefix, af_suffix[i]);
13 if (n < 0 || n >= 1024) return OMEGA_ARTIFACT_E_ARG;
14 }
15 return 0;
16}
17static int af_write(const char *path, const void *data, size_t n) {
18 int fd = open(path, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, 0644);
19 if (fd < 0) return OMEGA_ARTIFACT_E_IO;
20 FILE *f = fdopen(fd, "wb");
21 if (!f) { close(fd); return OMEGA_ARTIFACT_E_IO; }
22 int rc = n && fwrite(data, 1, n, f) != n ? OMEGA_ARTIFACT_E_IO : 0;
23 if (fclose(f)) rc = OMEGA_ARTIFACT_E_IO;
24 return rc;
25}
26static uint8_t *af_read(const char *path, size_t *n) {
27 FILE *f = fopen(path, "rb");
28 if (!f) return NULL;
29 if (fseek(f, 0, SEEK_END)) { fclose(f); return NULL; }
30 long z = ftell(f);
31 if (z < 0 || z > (long)(1u << 26) || fseek(f, 0, SEEK_SET)) { fclose(f); return NULL; }
32 uint8_t *b = malloc((size_t)z + 1);
33 if (!b) { fclose(f); return NULL; }
34 if (z && fread(b, 1, (size_t)z, f) != (size_t)z) { free(b); fclose(f); return NULL; }
35 b[z] = 0; *n = (size_t)z; fclose(f); return b;
36}
37static void af_hex(const void *data, size_t n, char out[65]) {
38 uint8_t d[32]; uint64_t st[8] = {0}, pair;
39 sha256_hash(data, n, d);
40 for (unsigned i = 0; i < 32; ++i) {
41 if (omega_artifact_parse(&pair, 4, d[i], st, 8) != OMEGA_OK) abort();
42 out[2*i] = (char)pair; out[2*i+1] = (char)(pair >> 8);
43 }
44 out[64] = 0;
45}
46static int af_pack(ArtifactWire *w, unsigned k, const char *v, size_t *off) {
47 size_t n = strlen(v);
48 if (n > 20000 - *off) return OMEGA_ARTIFACT_E_ARG;
49 w->s[8+k] = *off; w->s[48+k] = n;
50 memcpy(w->b + *off, v, n); *off += n;
51 return 0;
52}
53static int af_policy(ArtifactWire *w) {
54 uint64_t rc = 0;
55 if (omega_artifact_admit(&rc, w->b, sizeof w->b, w->s, 128) != OMEGA_OK) return OMEGA_ARTIFACT_E_FORMAT;
56 return (int)rc;
57}
58static int af_build(ArtifactWire *w, unsigned mode, size_t *n) {
59 uint64_t ok = 0; w->s[0] = mode;
60 if (omega_artifact_format(&ok, w->b, sizeof w->b, w->s, 128) != OMEGA_OK || !ok) return OMEGA_ARTIFACT_E_ARG;
61 *n = (size_t)w->s[1]; return 0;
62}
63static int af_elf(const uint8_t *cub, size_t cn, const uint8_t *text, size_t tn, const char *fn) {
64 unsigned char b[512] = {0}; uint64_t s[32] = {0}, result = 2;
65 s[1] = cn; s[2] = tn; s[3] = strlen(fn);
66 memcpy(b + 256, fn, (size_t)s[3]);
67 if (cn >= 64) memcpy(b, cub, 64);
68 while (result == 2) {
69 if (omega_artifact_elf(&result, b, sizeof b, s, 32) != OMEGA_OK) return 0;
70 if (result != 2) break;
71 memcpy(b, cub + s[4], (size_t)s[5]);
72 if (s[6]) memcpy(b + 128, text + s[14], (size_t)s[5]);
73 }
74 return (int)result;
75}
76static int af_error(int rc, char *err, size_t cap) {
77 if (err && cap) snprintf(err, cap, "artifact refused: %s", omega_native_artifact_rc_name(rc));
78 return rc;
79}
80int omega_native_artifact_export(const OmegaNativeArtifact *a, const char *prefix,
81 char out_id[OMEGA_ARTIFACT_ID_MAX], char *err, size_t err_cap) {
82 char path[8][1024], hashes[8][65]; SemanticId machine;
83 uint8_t *cub = NULL, *manifest = NULL; size_t cn = 0, mn = 0, n = 0, off = 0;
84 ArtifactWire w = {0}; int rc = 0;
85 if (err && err_cap) err[0] = 0;
86 if (out_id) out_id[0] = 0;
87 if (!a || !prefix || !out_id || !a->op || !a->shape || !a->bounds || !a->contract_name || !a->kernel_function ||
88 !a->text || !a->text_len || !a->params || !a->qmd0 || !a->qmd1 || !a->cbank_args ||
89 !a->semantic_spec_id_hex || !a->contract_id_hex || !a->min_driver || !a->requires || !a->precision ||
90 !a->accumulation || !a->tolerance || !a->source_commit || !a->tool_version || !a->qualification)
91 return af_error(OMEGA_ARTIFACT_E_ARG, err, err_cap);
92 const char *values[35] = {"",a->op,a->shape,a->bounds,a->contract_name,a->contract_id_hex,a->semantic_spec_id_hex,
93 "sm_121","","nvidia-cubin-elf64",a->kernel_function,"","","","","","","zero-va-v1","","","template","","","","","",
94 a->precision,a->accumulation,a->tolerance,a->min_driver,a->requires,a->qualification,a->source_commit,a->tool_version,""};
95 for (unsigned k = 0; k < 35; ++k) if ((rc = af_pack(&w,k,values[k],&off))) return af_error(rc,err,err_cap);
96 w.s[88] = a->text_len; w.s[90] = a->cbank_offset; w.s[91] = a->cbank_words;
97 w.s[92] = a->grid_x; w.s[93] = a->grid_y; w.s[94] = a->block_x; w.s[95] = a->block_y;
98 w.s[96] = a->block_z; w.s[97] = a->shared_bytes; w.s[98] = a->gpr_count;
99 memcpy(w.b+24576,a->qmd0,256); memcpy(w.b+24832,a->qmd1,256);
100 w.s[0] = 10;
101 if ((rc = af_policy(&w))) return af_error(rc,err,err_cap);
102 if ((rc = af_paths(path,prefix,8))) return af_error(rc,err,err_cap);
103 w.s[0] = 12;
104 if ((rc = af_policy(&w))) return af_error(rc,err,err_cap);
105 if (omega_blackwell_get_machine_id(&machine)) return af_error(OMEGA_ARTIFACT_E_ARG,err,err_cap);
106 /* The cubin packer's policy remains in its own seeded Omega unit. */
107 w.s[115] = OMEGA_CUBIN_PARAM_BASE;
108 for (size_t i = 0; i < a->nparams; ++i) {
109 w.s[0] = 14; w.s[113] = a->params[i].offset; w.s[114] = a->params[i].size;
110 if ((rc = af_policy(&w))) return af_error(rc,err,err_cap);
111 }
112 rc = omega_cubin_pack(a->kernel_function,a->text,a->text_len,a->params,a->nparams,(uint32_t)w.s[115],a->gpr_count,64,&cub,&cn,err,err_cap);
113 w.s[0] = 13; w.s[5] = (uint64_t)rc;
114 if ((rc = af_policy(&w))) return af_error(rc,err,err_cap);
115 w.s[0] = 11;
116 if ((rc = af_policy(&w))) goto done;
117 w.s[89] = cn;
118 af_hex(a->text,a->text_len,hashes[0]); af_hex(cub,cn,hashes[1]);
119 af_hex(a->qmd0,384,hashes[2]); af_hex(a->qmd1,384,hashes[3]); af_hex(a->cbank_args,a->cbank_words*4,hashes[4]);
120 /* Hex encoding of an existing semantic id uses the same Omega byte codec. */
121 { uint64_t st[8]={0},pair;
122 for (unsigned i=0;i<32;++i) { if (omega_artifact_parse(&pair,4,machine.bytes[i],st,8)!=OMEGA_OK) abort(); hashes[5][2*i]=(char)pair; hashes[5][2*i+1]=(char)(pair>>8); }
123 hashes[5][64]=0; }
124 values[8]=hashes[5]; values[11]=hashes[0]; values[13]=hashes[1]; values[15]=hashes[2]; values[16]=hashes[3]; values[21]=hashes[4];
125 const unsigned hashed[] = {8,11,13,15,16,21};
126 for (unsigned i=0;i<sizeof hashed/sizeof hashed[0];++i)
127 if ((rc=af_pack(&w,hashed[i],values[hashed[i]],&off))) goto done;
128 if ((rc=af_build(&w,0,&n))) goto done;
129 af_hex(w.b+32768,n,hashes[6]); values[0]=hashes[6];
130 if ((rc=af_pack(&w,0,values[0],&off))) goto done;
131 if ((rc=af_build(&w,1,&mn))) goto done;
132 manifest=malloc(mn); if (!manifest) { rc=OMEGA_ARTIFACT_E_MEM; goto done; }
133 memcpy(manifest,w.b+32768,mn);
134 if ((rc=af_write(path[1],a->text,a->text_len)) || (rc=af_write(path[2],cub,cn)) ||
135 (rc=af_write(path[3],a->qmd0,384)) || (rc=af_write(path[4],a->qmd1,384)) ||
136 (rc=af_write(path[5],a->cbank_args,a->cbank_words*4))) goto done;
137 if ((rc=af_build(&w,2,&n)) || (rc=af_write(path[6],w.b+32768,n)) || (rc=af_write(path[0],manifest,mn))) goto done;
138 af_hex(manifest,mn,hashes[7]);
139 if ((rc=af_pack(&w,35,hashes[7],&off)) || (rc=af_build(&w,4,&n)) || (rc=af_write(path[7],w.b+32768,n))) goto done;
140 memcpy(out_id,hashes[6],65);
141done:
142 free(cub); free(manifest);
143 if (rc) af_error(rc,err,err_cap);
144 return rc;
145}
146static int artifact_admit(const char *prefix, const char *driver, char out_id[65],
147 OmegaNativeArtifactSnapshot *snapshot, char *err, size_t err_cap) {
148 char path[8][1024], kernel[64]={0}; uint8_t *files[6]={0}; size_t sizes[6]={0};
149 ArtifactWire w={0}; int rc=0;
150 if (err && err_cap) err[0]=0;
151 if (out_id) out_id[0]=0;
152 if (snapshot) memset(snapshot,0,sizeof *snapshot);
153 if (!prefix) return OMEGA_ARTIFACT_E_ARG;
154 if ((rc=af_paths(path,prefix,6))) return af_error(rc,err,err_cap);
155 files[0]=af_read(path[0],&sizes[0]);
156 if (!files[0]) return af_error(OMEGA_ARTIFACT_E_IO,err,err_cap);
157 w.s[3]=sizes[0];
158 /* Bound the copy; Omega alone decides whether the manifest fits. */
159 memcpy(w.b,files[0],sizes[0]<8192?sizes[0]:8192);
160 if (driver) {
161 size_t n=strnlen(driver,512); w.s[106]=1; w.s[107]=n;
162 memcpy(w.b+25088,driver,n);
163 }
164 if ((rc=af_policy(&w))) goto done;
165 while (w.s[0]!=5) {
166 if (w.s[0]==1) {
167 char hash[65];
168 size_t start=(size_t)w.s[108];
169 size_t end=(size_t)w.s[109];
170 af_hex(files[0]+start,end-start,hash); memcpy(w.b+26000,hash,64);
171 } else if (w.s[0]==2) {
172 unsigned f=(unsigned)w.s[6]; char hash[65];
173 files[f]=af_read(path[f],&sizes[f]); w.s[5]=files[f]!=NULL; w.s[100+f]=sizes[f];
174 if (files[f]) { af_hex(files[f],sizes[f],hash); memcpy(w.b+26000,hash,64); }
175 } else if (w.s[0]==3) {
176 memcpy(kernel,files[0]+w.s[18],(size_t)w.s[58]);
177 w.s[5]=af_elf(files[2],sizes[2],files[1],sizes[1],kernel);
178 memcpy(w.b+24576,files[3],256); memcpy(w.b+24832,files[4],256);
179 }
180 if ((rc=af_policy(&w))) goto done;
181 }
182 if (out_id) { memcpy(out_id,files[0]+w.s[8],64); out_id[64]=0; }
183done:
184 if (!rc && snapshot) {
185 memcpy(snapshot->bytes,files,sizeof files); memcpy(snapshot->sizes,sizes,sizeof sizes);
186 memcpy(snapshot->kernel_function,kernel,sizeof kernel);
187 memcpy(snapshot->id,files[0]+w.s[8],64); snapshot->id[64]=0;
188 } else for (unsigned i=0;i<6;++i) free(files[i]);
189 if (rc) af_error(rc,err,err_cap);
190 return rc;
191}
192int omega_native_artifact_admit(const char *prefix,const char *driver,char out_id[65],char *err,size_t cap) {
193 return artifact_admit(prefix,driver,out_id,NULL,err,cap);
194}
195int omega_native_artifact_snapshot(const char *prefix,const char *driver,char out_id[65],OmegaNativeArtifactSnapshot *out,char *err,size_t cap) {
196 if (!out) return OMEGA_ARTIFACT_E_ARG;
197 return artifact_admit(prefix,driver,out_id,out,err,cap);
198}
199void omega_native_artifact_snapshot_free(OmegaNativeArtifactSnapshot *snapshot) {
200 if (!snapshot) return;
201 for (unsigned i=0;i<OMEGA_SNAPSHOT_FILES;++i) free(snapshot->bytes[i]);
202 memset(snapshot,0,sizeof *snapshot);
203}