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_gpu_mathlibs_host.inc

271 lines · 13643 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* Included after the private effect types. No plan/layout arithmetic lives here:
3 * Omega emits bounded unchunked records, then expands each launch on demand. */
4/* Like g_words, this byte-storage workspace is protected by the session lock.
5 * many_build materializes owned launches/tables before any public callback. */
6static uint64_t g_mhplan[8192];
7
8static int rng_args_ok(OmegaGpuRngKind kind, uint32_t nstreams, uint32_t per) {
9 return -(int)omega_mh_rng((unsigned)kind, nstreams, per, OMEGA_GPU_RNG_NORMAL);
10}
11
12int omega_gpu_rng_codegen(OmegaGpuRngKind kind, OmegaGpuMathInfo *info) {
13 if (rng_args_ok(kind, 1, 1)) return OMEGA_GPU_ML_BAD_ARGS;
14 return codegen_only(0, (uint64_t)kind, 0, OMEGA_GPU_RNG_THREADS, info);
15}
16
17int omega_gpu_rng_generate(OmegaGpuRngKind kind, uint64_t seed, uint64_t stream0, uint32_t nstreams,
18 uint32_t offset, uint32_t per_stream, void *out, OmegaGpuMathInfo *info) {
19 if (!out) return OMEGA_GPU_ML_BAD_ARGS;
20 int rc = rng_args_ok(kind, nstreams, per_stream);
21 if (rc) return rc;
22 uint64_t t0 = now_ns();
23 size_t count = omega_mh_rngval(kind, nstreams, per_stream, 0);
24 size_t esize = omega_mh_rngval(kind, nstreams, per_stream, 1);
25 size_t bytes = omega_mh_mul(count, esize);
26 omega_gpu_session_lock();
27 Slot *s;
28 rc = begin(0, (uint64_t)kind, 0, OMEGA_GPU_RNG_THREADS, &s, info);
29 if (!rc && omega_gpu_session_scratch(&g_out, bytes)) rc = OMEGA_GPU_ML_CHIP_FAIL;
30 if (!rc) {
31 uint32_t *dev = (uint32_t *)g_out.mem.cpu;
32 size_t nw = omega_mh_div(bytes, 4);
33 for (size_t i = 0; i < nw; i++) dev[i] = ML_POISON32;
34 if (omega_gpu_session_sync_to_gpu(&g_out.mem)) rc = OMEGA_GPU_ML_CHIP_FAIL;
35 uint32_t args[13];
36 put64(args, g_out.mem.va);
37 for (uint32_t i = 0; i < 4; i++) put64(&args[2 + 2 * i], omega_mh_seed(seed, stream0, i));
38 args[10] = offset; args[11] = per_stream; args[12] = nstreams;
39 if (!rc) rc = launch(s, args, 13, omega_mh_rngval(kind, nstreams, per_stream, 2), count, 0, info);
40 if (!rc && omega_gpu_session_sync_to_cpu(&g_out.mem)) rc = OMEGA_GPU_ML_CHIP_FAIL;
41 if (!rc) {
42 memcpy(out, g_out.mem.cpu, bytes);
43 uint64_t unwritten = 0;
44 for (size_t i = 0; i < count; i++) {
45 uint64_t v = esize == 8 ? ((const uint64_t *)out)[i] : ((const uint32_t *)out)[i];
46 unwritten = omega_mh_add(unwritten, omega_mh_poison(v, 0, esize == 8, 0));
47 }
48 if (info) info->unwritten_words = unwritten;
49 if (unwritten) rc = OMEGA_GPU_ML_UNWRITTEN;
50 }
51 }
52 omega_gpu_session_unlock();
53 if (info) info->call_ns = omega_mh_sub(now_ns(), t0);
54 return rc;
55}
56
57int omega_gpu_fft_c2c(const float *in, float *out, uint32_t n, uint32_t batch, int inverse, OmegaGpuMathInfo *info) {
58 uint64_t v[8] = {0};
59 int rc = -(int)omega_mh_fft((uintptr_t)in, (uintptr_t)out, n, batch, (unsigned)inverse, v);
60 if (rc == -4) return many_basic(OMEGA_GPU_FFT_TYPE_C2C, 1, &n, batch, inverse, in, out, info);
61 if (rc) return rc;
62 float *tw = (float *)malloc(v[3]);
63 if (!tw) return OMEGA_GPU_ML_CHIP_FAIL;
64 rc = omega_gpu_fft_twiddles(n, inverse, tw);
65 if (rc) { free(tw); return rc; }
66 uint64_t t0 = now_ns();
67 omega_gpu_session_lock();
68 Slot *s;
69 rc = begin(1, v[0], (uint64_t)inverse, v[4], &s, info);
70 if (!rc && (omega_gpu_session_scratch(&g_in, v[2]) || omega_gpu_session_scratch(&g_out, v[2]) ||
71 omega_gpu_session_scratch(&g_tw, v[3]))) rc = OMEGA_GPU_ML_CHIP_FAIL;
72 if (!rc) {
73 memcpy(g_in.mem.cpu, in, v[2]);
74 memcpy(g_tw.mem.cpu, tw, v[3]);
75 uint32_t *dev = (uint32_t *)g_out.mem.cpu;
76 for (size_t i = 0; i < v[1]; i++) dev[i] = ML_POISON32;
77 if (omega_gpu_session_sync_to_gpu(&g_in.mem) || omega_gpu_session_sync_to_gpu(&g_tw.mem) ||
78 omega_gpu_session_sync_to_gpu(&g_out.mem)) rc = OMEGA_GPU_ML_CHIP_FAIL;
79 for (uint64_t first = 0; first < batch && !rc; first += OMEGA_GPU_FFT_MAX_CTAS) {
80 uint32_t args[6];
81 uint64_t off = omega_mh_mul(first, v[3]);
82 uint32_t grid = omega_mh_min(omega_mh_sub(batch, first), OMEGA_GPU_FFT_MAX_CTAS);
83 put64(args, omega_mh_add(g_in.mem.va, off));
84 put64(&args[2], omega_mh_add(g_out.mem.va, off));
85 put64(&args[4], g_tw.mem.va);
86 rc = launch(s, args, 6, grid, omega_mh_mul(omega_mh_mul(grid, n), 2), v[3], info);
87 }
88 if (!rc && omega_gpu_session_sync_to_cpu(&g_out.mem)) rc = OMEGA_GPU_ML_CHIP_FAIL;
89 if (!rc) {
90 memcpy(out, dev, v[2]);
91 uint64_t unwritten = 0;
92 for (size_t i = 0; i < v[1]; i++) unwritten = omega_mh_add(unwritten, omega_mh_poison(dev[i], 0, 0, 0));
93 if (info) info->unwritten_words = unwritten;
94 if (unwritten) rc = OMEGA_GPU_ML_UNWRITTEN;
95 }
96 }
97 omega_gpu_session_unlock();
98 free(tw);
99 if (info) info->call_ns = omega_mh_sub(now_ns(), t0);
100 return rc;
101}
102
103int omega_gpu_philox_generate_dist(OmegaGpuPhiloxDist dist, uint64_t seed, uint64_t subseq0, uint32_t nstreams,
104 uint64_t offset, uint32_t per_stream, void *out, OmegaGpuMathInfo *info) {
105 if (!out) return OMEGA_GPU_ML_BAD_ARGS;
106 int rc = -(int)omega_mh_rng((unsigned)dist, nstreams, per_stream, OMEGA_GPU_PHILOX_NORMAL);
107 if (rc) return rc;
108 uint64_t t0 = now_ns();
109 size_t count = omega_mh_phil(dist, nstreams, per_stream, offset, 0);
110 size_t bytes = omega_mh_mul(count, 4);
111 uint32_t esz = omega_mh_phil(dist, nstreams, per_stream, offset, 5);
112 uint32_t *words = (uint32_t *)out;
113 omega_gpu_session_lock();
114 Slot *s;
115 rc = begin(3, dist, omega_mh_phil(dist, nstreams, per_stream, offset, 1), OMEGA_GPU_PHILOX_THREADS, &s, info);
116 if (!rc && omega_gpu_session_scratch(&g_out, bytes)) rc = OMEGA_GPU_ML_CHIP_FAIL;
117 if (!rc) {
118 uint32_t args[11];
119 put64(args, g_out.mem.va); put64(&args[2], seed); put64(&args[4], subseq0);
120 put64(&args[6], omega_mh_phil(dist, nstreams, per_stream, offset, 2));
121 args[8] = omega_mh_phil(dist, nstreams, per_stream, offset, 3);
122 args[9] = nstreams;
123 args[10] = omega_mh_phil(dist, nstreams, per_stream, offset, 4);
124 uint32_t grid = omega_mh_phil(dist, nstreams, per_stream, offset, 6);
125 rc = philox_pass(s, args, grid, count, ML_POISON32, words, info);
126 uint64_t unwritten = 0;
127 if (dist == OMEGA_GPU_PHILOX_RAW) {
128 uint64_t hits = 0;
129 for (size_t i = 0; !rc && i < count; i++) hits = omega_mh_add(hits, omega_mh_poison(words[i], 0, 0, 0));
130 if (!rc && hits) {
131 uint32_t *first = (uint32_t *)malloc(bytes);
132 if (!first) rc = OMEGA_GPU_ML_CHIP_FAIL;
133 if (first) {
134 memcpy(first, words, bytes);
135 rc = philox_pass(s, args, grid, count, ML_POISON32_ALT, words, info);
136 for (size_t i = 0; !rc && i < count; i++)
137 unwritten = omega_mh_add(unwritten, omega_mh_poison(first[i], words[i], 0, 1));
138 free(first);
139 }
140 }
141 } else if (esz == 1) {
142 for (size_t i = 0; !rc && i < count; i++) unwritten = omega_mh_add(unwritten, omega_mh_poison(words[i], 0, 0, 0));
143 } else {
144 const uint64_t *d = (const uint64_t *)out;
145 size_t nd = omega_mh_div(count, 2);
146 for (size_t i = 0; !rc && i < nd; i++) unwritten = omega_mh_add(unwritten, omega_mh_poison(d[i], 0, 1, 0));
147 }
148 if (info) info->unwritten_words = unwritten;
149 if (!rc && unwritten) rc = OMEGA_GPU_ML_UNWRITTEN;
150 }
151 omega_gpu_session_unlock();
152 if (info) info->call_ns = omega_mh_sub(now_ns(), t0);
153 return rc;
154}
155
156static int walk_locked(OmegaGpuFftPlanMode mode, uint32_t ndim, const uint32_t *dims, uint32_t batch, int inverse,
157 size_t bytes[3], OmegaGpuFftLaunchFn fn, void *ctx) {
158 if (!dims || !bytes) return OMEGA_GPU_ML_BAD_ARGS;
159 uint64_t p[8] = {mode, ndim, batch, (unsigned)inverse, 0, 0, 0, 0}, v[8] = {0};
160 if (ndim <= 3) for (uint32_t i = 0; i < ndim; i++) p[4 + i] = dims[i];
161 int rc = -(int)omega_mh_walk(p, v);
162 if (rc) return rc;
163 for (uint32_t i = 0; i < 3; i++) bytes[i] = v[1 + i];
164 rc = omega_gpu_om_ir_data(6, v[4], v[0], (uint64_t)inverse, g_words);
165 if (rc == OMEGA_OM_IR_REFUSED) return OMEGA_GPU_ML_BAD_ARGS;
166 if (rc != OMEGA_OM_IR_OK || !g_words[0] || g_words[0] > 6) return OMEGA_GPU_ML_CODEGEN_FAIL;
167 uint64_t steps[6];
168 size_t nsteps = g_words[0];
169 memcpy(steps, &g_words[1], nsteps * sizeof *steps);
170 for (size_t i = 0; fn && i < nsteps; i++) {
171 uint64_t d[16] = {0};
172 for (uint64_t cursor = 0;; cursor += OMEGA_GPU_FFT_MAX_CTAS) {
173 rc = -(int)omega_mh_walkstep(steps[i], batch, cursor, d);
174 if (rc) return rc;
175 if (cursor >= d[15]) break;
176 OmegaGpuFftLaunch l = {0};
177 l.kind=d[0]; l.a=d[1]; l.b=d[2]; l.src=d[3]; l.dst=d[4];
178 l.src_off=d[5]; l.dst_off=d[6]; l.threads=d[7]; l.grid=d[8];
179 l.shared_bytes=d[9]; l.total=d[10]; l.first=d[11]; l.tw_n=d[12]; l.tw_inverse=d[13]; l.elements=d[14];
180 if (l.tw_n) {
181 if (!cursor) {
182 rc = twiddles_locked(l.a, l.b);
183 if (rc) return rc;
184 }
185 l.tw = g_twf;
186 }
187 rc = fn(ctx, &l);
188 if (rc) return rc;
189 }
190 }
191 return OMEGA_GPU_ML_OK;
192}
193
194static int many_each(uint32_t rank, const Lay *l, const uint32_t *ext, uint32_t batch,
195 int (*f)(void *, uint32_t), void *ctx) {
196 uint64_t layout[8] = {l->base, l->bs, l->st[0], l->st[1], l->st[2], 0, 0, 0};
197 uint64_t shape[4] = {ext[0], ext[1], ext[2], 1};
198 uint64_t count = omega_mh_eachcnt(shape, rank, batch);
199 for (uint64_t i = 0; i < count; i++) {
200 int rc = f(ctx, omega_mh_index(layout, shape, rank, i));
201 if (rc) return rc;
202 }
203 return 0;
204}
205
206static int many_mark(void *ctx, uint32_t idx) {
207 uint8_t *bits = (uint8_t *)ctx;
208 size_t at = omega_mh_div(idx, 8);
209 uint64_t v = omega_mh_fill(bits[at], idx);
210 if (v > UINT8_MAX) return 1;
211 bits[at] = (uint8_t)v;
212 return 0;
213}
214
215static int many_build(const OmegaGpuFftMany *p, int in_place, int alias, Many *m, size_t bytes[OMEGA_GPU_FFT_MANY_BUFS],
216 ManyOut *out) {
217 memset(m, 0, sizeof *m);
218 if (!p || !bytes) return OMEGA_GPU_ML_BAD_ARGS;
219 uint64_t args[32] = {p->rank, p->batch, (unsigned)p->type, (unsigned)p->inverse, in_place, alias};
220 for (uint32_t i = 0; i < 3; i++) { args[6+i]=p->n[i]; args[9+i]=p->inembed[i]; args[14+i]=p->onembed[i]; }
221 args[12]=p->istride; args[13]=p->idist; args[17]=p->ostride; args[18]=p->odist;
222 /* The caller holds the session lock, so the fixed ABI workspace needs
223 * neither a per-plan allocation nor a large per-thread stack frame. */
224 uint64_t *d = g_mhplan;
225 int rc = -(int)omega_mh_many(args, d);
226 if (rc) return rc;
227 ManyOut o = {0};
228 o.rank=d[13]; o.batch=d[14]; o.owords=d[15]; o.oextent=d[16]; o.obasic=d[17];
229 for (uint32_t i=0; i<3; i++) o.oext[i]=d[18+i];
230 o.olay.base=d[21]; o.olay.bs=d[22];
231 for (uint32_t i=0; i<3; i++) o.olay.st[i]=d[23+i];
232 if (!o.obasic) {
233 size_t nb = omega_mh_div(omega_mh_add(o.oextent, 7), 8);
234 uint8_t *bits = (uint8_t *)calloc(nb, 1);
235 if (!bits) rc = OMEGA_GPU_ML_CHIP_FAIL;
236 else { if (many_each(o.rank, &o.olay, o.oext, o.batch, many_mark, bits)) rc = OMEGA_GPU_ML_BAD_ARGS; free(bits); }
237 }
238 if (!rc) {
239 for (uint32_t i=0; i<OMEGA_GPU_FFT_MANY_BUFS; i++) bytes[i]=d[5+i];
240 m->tab = (uint64_t *)malloc(bytes[OMEGA_GPU_FFT_BUF_TABLE]);
241 if (!m->tab) rc = OMEGA_GPU_ML_CHIP_FAIL;
242 }
243 for (uint64_t i=0; !rc && i<d[0]; i++) {
244 const uint64_t *r = &d[32 + 40*i]; /* serialized record addressing, not plan arithmetic */
245 if (r[0] == 10 || r[0] == 11) {
246 if (omega_gpu_om_ir_data(r[0], r[1], r[2], 0, g_words) != OMEGA_OM_IR_OK) rc = OMEGA_GPU_ML_CODEGEN_FAIL;
247 else memcpy((uint8_t *)m->tab + r[5], g_words, omega_mh_mul(r[1], 8));
248 continue;
249 }
250 OmegaGpuFftManyLaunch l = {0};
251 l.kind=r[0]; l.a=r[1]; l.b=r[2]; l.nptr=r[3]; l.nwords=r[4]; l.threads=OMEGA_GPU_FFT_MANY_THREADS;
252 for (uint32_t k=0; k<3; k++) { l.buf[k]=r[7+k]; l.off[k]=r[10+k]; }
253 for (uint32_t k=0; k<18; k++) l.words[k]=r[13+k];
254 l.words[0]=r[5];
255 uint64_t ctas=omega_mh_div(omega_mh_add(r[5], OMEGA_GPU_FFT_MANY_THREADS-1), OMEGA_GPU_FFT_MANY_THREADS);
256 for (uint64_t first=0; !rc && first<ctas; first+=OMEGA_GPU_FFT_MAX_CTAS) {
257 l.grid=omega_mh_chunk(r[5], first, OMEGA_GPU_FFT_MANY_THREADS, r[6], 0);
258 l.words[1]=omega_mh_chunk(r[5], first, OMEGA_GPU_FFT_MANY_THREADS, r[6], 1);
259 l.elements=omega_mh_chunk(r[5], first, OMEGA_GPU_FFT_MANY_THREADS, r[6], 2);
260 if (m->n == m->cap) {
261 size_t cap = m->cap ? omega_mh_mul(m->cap, 2) : 64;
262 OmegaGpuFftManyLaunch *v = (OmegaGpuFftManyLaunch *)realloc(m->l, omega_mh_mul(cap, sizeof *v));
263 if (!v) { rc=OMEGA_GPU_ML_CHIP_FAIL; break; }
264 m->l=v; m->cap=cap;
265 }
266 m->l[m->n++]=l;
267 }
268 }
269 if (!rc && out) *out=o;
270 return rc;
271}