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_apiviews.h

717 lines · 21772 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* Host data loops for the elementwise, matmul, gdn, fp8 and scan API residue.
3 * Omega (om/apiviews) is the default. The old C compiles only when
4 * CUDAPP_MIGRATION_PENDING_APIVIEWS is set (the differential oracle) or
5 * CUDAPP_MIGRATION_PENDING_APIVIEWS_DIFF is set (both, abort on mismatch).
6 * Pointer acquisition and the launch stay with the caller. */
7#ifndef OMEGA_GPU_APIVIEWS_H
8#define OMEGA_GPU_APIVIEWS_H
9
10#include "cudapp_native.h"
11#ifndef CUDAPP_MIGRATION_PENDING_C_NO_POSIX_SESSION
12#define CUDAPP_MIGRATION_PENDING_C_NO_POSIX_SESSION 0
13#endif
14#ifdef OMEGA_GPU_NO_POSIX
15#include "omega_native.h"
16#endif
17
18#include <stdint.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include "omega_gpu_hostmath.h"
23
24#if defined(CUDAPP_MIGRATION_PENDING_APIVIEWS_DIFF)
25#define AV_DIFF 1
26#define AV_C 1
27#define AV_OM 1
28#elif defined(CUDAPP_MIGRATION_PENDING_APIVIEWS) || defined(OMEGA_GPU_NO_POSIX)
29#define AV_DIFF 0
30#define AV_C 1
31#define AV_OM 0
32#else
33#define AV_DIFF 0
34#define AV_C 0
35#define AV_OM 1
36#endif
37
38static inline void av_die(const char *what) {
39 fprintf(stderr, "FAIL apiviews C-vs-Omega differential: %s\n", what);
40 abort();
41}
42
43#if defined(CUDAPP_MIGRATION_PENDING_APIVIEWS_DIFF_SELFTEST)
44static int av_flipped;
45static inline void av_flip(void *p, size_t n) {
46 if (av_flipped || !p || n == 0) return;
47 ((unsigned char *)p)[0] ^= 1u;
48 av_flipped = 1;
49}
50#else
51static inline void av_flip(void *p, size_t n) { (void)p; (void)n; }
52#endif
53
54static inline int av_u32_eq(uint32_t a, uint32_t b) { return a == b; }
55
56static inline int av_f32_eq(float a, float b) {
57 uint32_t ua, ub;
58 memcpy(&ua, &a, 4);
59 memcpy(&ub, &b, 4);
60 if (ua == ub) return 1;
61 /* NaN by class only. 0x7fffffff is the CUDApp quiet-NaN comparison convention. */
62 if ((ua & 0x7fffffffu) > 0x7f800000u && (ub & 0x7fffffffu) > 0x7f800000u) return 1;
63 return 0;
64}
65
66static inline void av_need(uint32_t st, uint64_t res) {
67 if (st != OMEGA_OK || res != 0) av_die("omega unit refused");
68}
69
70#if AV_C
71static uint8_t av_e4m3_c(float v) {
72 uint32_t u;
73 memcpy(&u, &v, 4);
74 const uint8_t s = (uint8_t)((u >> 24) & 0x80u);
75 u &= 0x7FFFFFFFu;
76 float a;
77 memcpy(&a, &u, 4);
78 if (!(a < 448.0f)) return (uint8_t)(s | 0x7Eu);
79 const int ex = (int)(u >> 23) - 127;
80 if (ex < -6) {
81 const float x = a * 512.0f;
82 uint32_t xi = (uint32_t)x;
83 const float fr = x - (float)xi;
84 if (fr > 0.5f || (fr == 0.5f && (xi & 1u))) xi++;
85 return (uint8_t)(s | xi);
86 }
87 const uint32_t m = u & 0x7FFFFFu, rb = (m >> 19) & 1u, st = m & 0x7FFFFu;
88 uint32_t q = m >> 20, e = (uint32_t)(ex + 7);
89 if (rb && (st || (q & 1u))) q++;
90 if (q == 8u) { q = 0; e++; }
91 uint32_t code = (e << 3) | q;
92 if (code > 0x7Eu) code = 0x7Eu;
93 return (uint8_t)(s | code);
94}
95
96#endif
97
98#if (!defined(OMEGA_GPU_NO_POSIX) && AV_C) || (defined(OMEGA_GPU_NO_POSIX) && CUDAPP_MIGRATION_PENDING_C_NO_POSIX_SESSION)
99static uint16_t av_bf16_c(uint64_t md, uint32_t u) {
100 if (md == 0) {
101 if ((u & 0x7f800000u) == 0x7f800000u && (u & 0x007fffffu)) return (uint16_t)((u >> 16) | 0x0040u);
102 return (uint16_t)((u + 0x7fffu + ((u >> 16) & 1u)) >> 16);
103 }
104 if ((u & 0x7fffffffu) > 0x7f800000u) return 0x7fc0u;
105 return (uint16_t)((u + 0x7fffu + ((u >> 16) & 1u)) >> 16);
106}
107#endif
108
109#if AV_C
110
111static uint32_t av_gdn_channel_c(uint32_t h, uint32_t s, uint32_t vh) {
112 const uint32_t hk = h / (vh / 16u), D = 128u;
113 if (s < D) return hk * D + s;
114 if (s < 2u * D) return 16u * D + hk * D + (s - D);
115 return 2u * 16u * D + h * D + (s - 2u * D);
116}
117#endif
118
119static inline uint8_t av_e4m3(float v) {
120#if AV_C
121 uint8_t c = av_e4m3_c(v);
122#endif
123#if AV_OM
124 uint32_t bits;
125 memcpy(&bits, &v, 4);
126 uint64_t code = 0;
127 uint32_t st = omega_av_e4m3(&code, bits);
128 if (st != OMEGA_OK) av_die("omega unit refused");
129 uint8_t o = (uint8_t)code;
130 av_flip(&o, 1);
131#endif
132#if AV_DIFF
133 if (c != o) av_die("output bytes differ");
134 return c;
135#elif AV_C
136 return c;
137#else
138 return o;
139#endif
140}
141
142static inline int av_bf16_n(uint64_t md, uint64_t n, const float *x, uint16_t *y) {
143#ifdef OMEGA_GPU_NO_POSIX
144 /* Integer-public freestanding ABI: only view transport remains in C.
145 * The canonical view unit uses a hosted floating-view public ABI. */
146 if (n && (!x || !y)) __builtin_trap();
147 for (uint64_t i = 0; i < n; i++) {
148 uint32_t u;
149 memcpy(&u, &x[i], sizeof u);
150#if CUDAPP_MIGRATION_PENDING_C_NO_POSIX_SESSION
151 y[i] = av_bf16_c(md, u);
152#else
153 uint64_t r = 0;
154 if (omega2_no_avbf(&r, md, u) != OMEGA_OK) __builtin_trap();
155 y[i] = (uint16_t)r;
156#endif
157 }
158#else
159#if AV_C
160 for (uint64_t i = 0; i < n; i++) {
161 uint32_t u;
162 memcpy(&u, &x[i], 4);
163 y[i] = av_bf16_c(md, u);
164 }
165#endif
166#if AV_OM
167 uint64_t res = 1;
168 uint16_t *tmp = y;
169#if AV_DIFF
170 tmp = malloc((size_t)n * 2u);
171 if (!tmp) av_die("omega unit refused");
172#endif
173 uint32_t st = omega_av_bf16(&res, md, x, n, tmp, n);
174 av_need(st, res);
175 av_flip(tmp, (size_t)n * 2u);
176#if AV_DIFF
177 if (memcmp(y, tmp, (size_t)n * 2u) != 0) av_die("output bytes differ");
178 free(tmp);
179#endif
180#endif
181#endif
182 return 0;
183}
184
185static inline int av_widen_n(uint64_t n, const uint16_t *x, float *y) {
186#if AV_C
187 for (uint64_t i = 0; i < n; i++) {
188 uint32_t b = (uint32_t)x[i] << 16;
189 memcpy(&y[i], &b, 4);
190 }
191#endif
192#if AV_OM
193 uint64_t res = 1;
194 float *tmp = y;
195#if AV_DIFF
196 tmp = malloc((size_t)n * 4u);
197 if (!tmp) av_die("omega unit refused");
198#endif
199 uint32_t st = omega_av_widen(&res, x, n, tmp, n);
200 av_need(st, res);
201 av_flip(tmp, (size_t)n * 4u);
202#if AV_DIFF
203 for (uint64_t i = 0; i < n; i++) if (!av_f32_eq(y[i], tmp[i])) av_die("output bytes differ");
204 free(tmp);
205#endif
206#endif
207 return 0;
208}
209
210static inline uint32_t av_gdn_channel(uint32_t h, uint32_t s, uint32_t vh) {
211#if AV_C
212 uint32_t c = av_gdn_channel_c(h, s, vh);
213#endif
214#if AV_OM
215 uint64_t ch = 0;
216 uint32_t st = omega_av_gdnch(&ch, h, s, vh);
217 if (st != OMEGA_OK) av_die("omega unit refused");
218 uint32_t o = (uint32_t)ch;
219 av_flip(&o, 4);
220#endif
221#if AV_DIFF
222 if (c != o) av_die("output bytes differ");
223 return c;
224#elif AV_C
225 return c;
226#else
227 return o;
228#endif
229}
230
231static inline void av_gdn_slot(float *dst, const float src[3]) {
232#if AV_C
233 float c[4] = {src[0], src[1], src[2], 0.0f};
234#endif
235#if AV_OM
236 uint64_t res = 1;
237 float o[4];
238 uint32_t st = omega_av_gdnsl(&res, src, 3, o, 4);
239 av_need(st, res);
240 av_flip(o, sizeof o);
241#if AV_DIFF
242 for (int i = 0; i < 4; i++) if (!av_f32_eq(c[i], o[i])) av_die("output bytes differ");
243 memcpy(dst, c, sizeof c);
244#else
245 memcpy(dst, o, sizeof o);
246#endif
247#else
248 memcpy(dst, c, sizeof c);
249#endif
250}
251
252static inline int av_gdn_scales(uint64_t n, const float *ng, const float *dt, float *sc) {
253#if AV_C
254 for (uint64_t i = 0; i < n; i++) {
255 sc[2 * i] = ng[i];
256 sc[2 * i + 1] = dt[i];
257 }
258#endif
259#if AV_OM
260 uint64_t res = 1;
261 float *tmp = sc;
262#if AV_DIFF
263 tmp = malloc((size_t)n * 2u * 4u);
264 if (!tmp) av_die("omega unit refused");
265#endif
266 uint32_t st = omega_av_gdnsc(&res, ng, n, dt, n, tmp, n * 2u);
267 av_need(st, res);
268 av_flip(tmp, (size_t)n * 8u);
269#if AV_DIFF
270 for (uint64_t i = 0; i < n * 2u; i++) if (!av_f32_eq(sc[i], tmp[i])) av_die("output bytes differ");
271 free(tmp);
272#endif
273#endif
274 return 0;
275}
276
277static inline void av_fp8_group(uint64_t ent[2], uint64_t id, uint64_t wv, uint64_t sv, uint64_t ws, uint64_t ss) {
278#if AV_C
279 uint64_t c0 = wv + id * ws, c1 = sv + id * ss;
280#endif
281#if AV_OM
282 uint64_t res = 1, o[2];
283 uint32_t st = omega_av_fp8en(&res, id, wv, sv, ws, ss, o, 2);
284 av_need(st, res);
285 av_flip(o, sizeof o);
286#endif
287#if AV_DIFF
288 if (c0 != o[0] || c1 != o[1]) av_die("output bytes differ");
289 ent[0] = c0;
290 ent[1] = c1;
291#elif AV_C
292 ent[0] = c0;
293 ent[1] = c1;
294#else
295 ent[0] = o[0];
296 ent[1] = o[1];
297#endif
298}
299
300static inline int av_rope_head(uint64_t n, const float *cs, const float *sn, float *tab) {
301#if AV_C
302 for (uint64_t i = 0; i < n; i++) {
303 tab[i] = cs[i];
304 tab[i + n] = sn[i];
305 }
306#endif
307#if AV_OM
308 uint64_t res = 1;
309 float *tmp = tab;
310#if AV_DIFF
311 tmp = malloc((size_t)n * 2u * 4u);
312 if (!tmp) av_die("omega unit refused");
313#endif
314 uint32_t st = omega_av_rope(&res, cs, n, sn, n, tmp, n * 2u);
315 av_need(st, res);
316 av_flip(tmp, (size_t)n * 8u);
317#if AV_DIFF
318 for (uint64_t i = 0; i < n * 2u; i++) if (!av_f32_eq(tab[i], tmp[i])) av_die("output bytes differ");
319 free(tmp);
320#endif
321#endif
322 return 0;
323}
324
325static inline int av_ew_rec(uint64_t kind, uint64_t n, uint64_t p0, uint64_t p1, uint64_t rec[8]) {
326#if AV_C
327 uint64_t c[8] = {0, 0, 0, 0, 0, 0, 0, 0};
328 int cref = 0;
329 if (n == 0) cref = 1;
330 else if (kind == 0) {
331 uint64_t ni = n > 131072u ? 131072u : n;
332 c[0] = ni;
333 c[1] = (ni + 127u) / 128u;
334 c[2] = ni;
335 c[6] = 128u;
336 } else if (kind == 1) {
337 if (p0 < 2 || (p0 & 1u) || p0 > 2048u) cref = 1;
338 else {
339 uint64_t nh = n > 1024u ? 1024u : n;
340 uint64_t hf = p0 / 2u;
341 c[0] = nh;
342 c[1] = hf;
343 c[2] = p0;
344 c[3] = hf;
345 c[6] = hf;
346 c[7] = nh;
347 }
348 } else if (kind == 2) {
349 if (p1 == 0 || p1 > 16384u) cref = 1;
350 else {
351 uint64_t nr = n > 1024u ? 1024u : n;
352 float inv = 1.0f / (float)p1;
353 uint32_t ib;
354 memcpy(&ib, &inv, 4);
355 c[0] = nr;
356 c[1] = p1 / 128u;
357 c[2] = ib;
358 c[3] = p0;
359 c[4] = p1;
360 c[6] = 128u;
361 c[7] = nr;
362 }
363 } else cref = 1;
364#endif
365#if AV_OM
366 uint64_t res = 1, o[8];
367 memset(o, 0, sizeof o);
368 uint32_t st = omega_av_ewarg(&res, kind, n, p0, p1, o, 8);
369 if (st != OMEGA_OK) av_die("omega unit refused");
370 av_flip(&res, 8);
371#endif
372#if AV_DIFF
373 if ((uint64_t)cref != res) av_die("output bytes differ");
374 if (!cref && memcmp(c, o, sizeof c) != 0) av_die("output bytes differ");
375 if (cref) return 1;
376 memcpy(rec, c, sizeof c);
377 return 0;
378#elif AV_C
379 if (cref) return 1;
380 memcpy(rec, c, sizeof c);
381 return 0;
382#else
383 if (res != 0) return 1;
384 memcpy(rec, o, sizeof o);
385 return 0;
386#endif
387}
388
389static inline int av_scan_tile(uint64_t op, uint64_t n, const float *in, float out[32]) {
390 if (n == 0 || n > 32 || op > 3) return 1;
391#if AV_C
392 float c[32], pad;
393 uint32_t padb = (op == 0 || op == 3) ? 0x80000000u : 0x7fc00000u;
394 memcpy(&pad, &padb, 4);
395 for (uint64_t i = 0; i < 32; i++) c[i] = i < n ? in[i] : pad;
396 if (op == 3) {
397 for (uint64_t t = 0; t < 5; t++) {
398 uint64_t d = 1ull << t;
399 for (uint64_t i = 0; i < 32; i++) {
400 uint64_t k = 31u - i;
401 if (k >= d) c[k] = c[k - d] + c[k];
402 }
403 }
404 } else {
405 for (uint64_t t = 0; t < 5; t++) {
406 uint64_t d = 16ull >> t;
407 for (uint64_t i = 0; i + d < 32; i++) {
408 float a = c[i], b = c[i + d], r;
409 if (op == 0) r = a + b;
410 else if (op == 1) r = __builtin_fmaxf(a, b);
411 else r = __builtin_fminf(a, b);
412 c[i] = r;
413 }
414 }
415 }
416#endif
417#if AV_OM
418 float inn[32], o[32];
419 memset(inn, 0, sizeof inn);
420 for (uint64_t i = 0; i < n; i++) inn[i] = in[i];
421 uint64_t res = 1;
422 uint32_t st = omega_av_scan(&res, op, n, inn, 32, o, 32);
423 av_need(st, res);
424 av_flip(o, sizeof o);
425#endif
426#if AV_DIFF
427 for (int i = 0; i < 32; i++) if (!av_f32_eq(c[i], o[i])) av_die("output bytes differ");
428 memcpy(out, c, sizeof c);
429#elif AV_C
430 memcpy(out, c, sizeof c);
431#else
432 memcpy(out, o, sizeof o);
433#endif
434 return 0;
435}
436
437/* Block iteration stays here: it is pointer acquisition. The amax, the scale and
438 * the 128-wide quantize are the units. */
439static inline int av_fp8_quantize(uint32_t n, uint32_t k, const uint16_t *w, uint8_t *w8, uint16_t *scale) {
440 if (!w || !w8 || !scale || n == 0 || k == 0 || (n % 128u) || (k % 128u)) return 1;
441 const uint32_t kb = k / 128u;
442 for (uint32_t bn = 0; bn < n / 128u; bn++) {
443 for (uint32_t bk = 0; bk < kb; bk++) {
444#if AV_C
445 float amax = 0.0f;
446 int bad = 0;
447 uint16_t sb = 0x3F80u;
448 uint8_t rowb[128][128];
449 for (uint32_t r = 0; r < 128u && !bad; r++) {
450 const uint16_t *row = w + ((size_t)bn * 128u + r) * k + (size_t)bk * 128u;
451 for (uint32_t c = 0; c < 128u; c++) {
452 uint32_t b = (uint32_t)(row[c] & 0x7FFFu) << 16;
453 float f;
454 memcpy(&f, &b, 4);
455 if (!(f <= 3.0e38f)) { bad = 1; break; }
456 if (f > amax) amax = f;
457 }
458 }
459 if (!bad && amax > 0.0f) {
460 const float q = amax / 448.0f;
461 uint32_t b;
462 memcpy(&b, &q, 4);
463 sb = (uint16_t)((b >> 16) + ((b & 0xFFFFu) ? 1u : 0u));
464 }
465 if (!bad) {
466 uint32_t sbits = (uint32_t)sb << 16;
467 float sc;
468 memcpy(&sc, &sbits, 4);
469 for (uint32_t r = 0; r < 128u; r++) {
470 const size_t at = ((size_t)bn * 128u + r) * k + (size_t)bk * 128u;
471 for (uint32_t c = 0; c < 128u; c++) {
472 uint32_t b = (uint32_t)w[at + c] << 16;
473 float f;
474 memcpy(&f, &b, 4);
475 rowb[r][c] = av_e4m3_c(f / sc);
476 }
477 }
478 }
479#endif
480#if AV_OM
481 float acc = 0.0f;
482 int obad = 0;
483 uint16_t osb = 0;
484 uint8_t orow[128][128];
485 for (uint32_t r = 0; r < 128u && !obad; r++) {
486 const uint16_t *row = w + ((size_t)bn * 128u + r) * k + (size_t)bk * 128u;
487 uint64_t res = 1;
488 uint32_t st = omega_av_fp8am(&res, 128, row, 128, &acc, 1);
489 if (st != OMEGA_OK || res == 1) av_die("omega unit refused");
490 if (res == 2) obad = 1;
491 }
492 if (!obad) {
493 uint32_t abits;
494 memcpy(&abits, &acc, 4);
495 uint64_t sb64 = 0;
496 uint32_t st = omega_av_fp8sc(&sb64, abits);
497 if (st != OMEGA_OK) av_die("omega unit refused");
498 osb = (uint16_t)sb64;
499 for (uint32_t r = 0; r < 128u; r++) {
500 const size_t at = ((size_t)bn * 128u + r) * k + (size_t)bk * 128u;
501 const uint16_t *row = w + at;
502 uint64_t res = 1;
503 st = omega_av_fp8q(&res, 128, osb, row, 128, orow[r], 128);
504 av_need(st, res);
505 }
506 av_flip(&osb, 2);
507 av_flip(orow, sizeof orow);
508 }
509#endif
510#if AV_DIFF
511 if (bad != obad) av_die("output bytes differ");
512 if (bad) return 1;
513 if (sb != osb) av_die("output bytes differ");
514 for (uint32_t r = 0; r < 128u; r++) {
515 const size_t at = ((size_t)bn * 128u + r) * k + (size_t)bk * 128u;
516 if (memcmp(rowb[r], orow[r], 128) != 0) av_die("output bytes differ");
517 memcpy(w8 + at, rowb[r], 128);
518 }
519 scale[(size_t)bn * kb + bk] = sb;
520#elif AV_C
521 if (bad) return 1;
522 scale[(size_t)bn * kb + bk] = sb;
523 for (uint32_t r = 0; r < 128u; r++) {
524 const size_t at = ((size_t)bn * 128u + r) * k + (size_t)bk * 128u;
525 memcpy(w8 + at, rowb[r], 128);
526 }
527#else
528 if (obad) return 1;
529 scale[(size_t)bn * kb + bk] = osb;
530 for (uint32_t r = 0; r < 128u; r++) {
531 const size_t at = ((size_t)bn * 128u + r) * k + (size_t)bk * 128u;
532 memcpy(w8 + at, orow[r], 128);
533 }
534#endif
535 }
536 }
537 return 0;
538}
539
540/* GDN tail pack/unpack and negated exponential, plus the fp8 x fold.
541 * 384 is OMEGA_GDN_SLOTS. A production include of the GDN header uses the
542 * macro, so a drift against the Omega unit fails this differential.
543 * WolframLanguageEvaluator: bits32[2^64] -> 1602224128; neg(float(exp(0)))
544 * is the f32 -1 bit pattern 3212836864.
545 */
546#ifdef OMEGA_GDN_SLOTS
547#define AV_GDN_SLOTS OMEGA_GDN_SLOTS
548#else
549#define AV_GDN_SLOTS 384u
550#endif
551#define AV_FP8_FOLD 64
552
553static inline int av_gdn_pack_tail(uint32_t vh, uint32_t conv, const float *tail, float *dev) {
554 if (!tail || !dev || vh == 0 || (vh & 15u) != 0 || conv == 0) return -1;
555#if AV_C
556 float *c = malloc((size_t)vh * AV_GDN_SLOTS * 4u * sizeof *c);
557 if (!c) return -1;
558 for (uint32_t h = 0; h < vh; h++) {
559 for (uint32_t s = 0; s < AV_GDN_SLOTS; s++) {
560 float *d = c + ((size_t)h * AV_GDN_SLOTS + s) * 4u;
561 const uint32_t ch = av_gdn_channel_c(h, s, vh);
562 d[0] = tail[(size_t)0 * conv + ch];
563 d[1] = tail[(size_t)1 * conv + ch];
564 d[2] = tail[(size_t)2 * conv + ch];
565 d[3] = 0.0f;
566 }
567 }
568#endif
569#if AV_OM
570 const uint64_t dn = (uint64_t)vh * AV_GDN_SLOTS * 4u;
571 const uint64_t tn = (uint64_t)3u * conv;
572 uint64_t res = 1;
573 const uint32_t st = omega_av_gdnpt(&res, tail, tn, dev, dn, vh, conv);
574#endif
575#if AV_DIFF
576 av_flip(dev, dn * 4u);
577 int bad = (st != OMEGA_OK || res != 0 || memcmp(dev, c, dn * 4u) != 0);
578 free(c);
579 if (bad) av_die("gdn pack tail");
580 return 0;
581#elif AV_C
582 memcpy(dev, c, (size_t)vh * AV_GDN_SLOTS * 4u * sizeof *c);
583 free(c);
584 return 0;
585#else
586 return (st == OMEGA_OK && res == 0) ? 0 : -1;
587#endif
588}
589
590static inline int av_gdn_unpack_tail(uint32_t vh, uint32_t conv, float *tail, const float *dev) {
591 if (!tail || !dev || vh == 0 || (vh & 15u) != 0 || conv == 0) return -1;
592#if AV_C
593 float *tmp = malloc((size_t)3u * conv * sizeof *tmp);
594 if (!tmp) return -1;
595 memcpy(tmp, tail, (size_t)3u * conv * sizeof *tmp);
596 int crc = 0;
597 for (uint32_t h = 0; h < vh; h++) {
598 for (uint32_t s = 0; s < AV_GDN_SLOTS; s++) {
599 const float *d = dev + ((size_t)h * AV_GDN_SLOTS + s) * 4u;
600 const uint32_t ch = av_gdn_channel_c(h, s, vh);
601 const int first = s >= 256u || (h % (vh / 16u)) == 0;
602 for (uint32_t j = 0; j < 3u; j++) {
603 float *t = &tmp[(size_t)j * conv + ch];
604 if (first) *t = d[j];
605 else if (memcmp(t, &d[j], 4) != 0) crc = -1;
606 }
607 }
608 }
609#endif
610#if AV_OM
611 const uint64_t dn = (uint64_t)vh * AV_GDN_SLOTS * 4u;
612 const uint64_t tn = (uint64_t)3u * conv;
613 uint64_t res = 1;
614 const uint32_t st = omega_av_gdnut(&res, dev, dn, tail, tn, vh, conv);
615#endif
616#if AV_DIFF
617 av_flip(tail, tn * 4u);
618 int bad = (st != OMEGA_OK || (res == 0) != (crc == 0) || memcmp(tail, tmp, tn * 4u) != 0);
619 free(tmp);
620 if (bad) av_die("gdn unpack tail");
621 return crc;
622#elif AV_C
623 memcpy(tail, tmp, (size_t)3u * conv * sizeof *tmp);
624 free(tmp);
625 return crc;
626#else
627 return (st == OMEGA_OK) ? (res == 0 ? 0 : -1) : -1;
628#endif
629}
630
631static inline int av_gdn_negexp(uint32_t n, const float *src, float *dst) {
632 if (n == 0) return 0;
633 if (!src || !dst) return -1;
634#if AV_C
635 float *c = malloc((size_t)n * sizeof *c);
636 if (!c) return -1;
637 for (uint32_t i = 0; i < n; i++) c[i] = -(float)omega_hostmath_exp((double)src[i]);
638#endif
639#if AV_OM
640 uint64_t res = 1;
641 const uint32_t st = omega_av_gdnex(&res, src, n, dst, n);
642#endif
643#if AV_DIFF
644 av_flip(dst, (size_t)n * 4u);
645 int bad = (st != OMEGA_OK || res != 0 || memcmp(dst, c, (size_t)n * 4u) != 0);
646 free(c);
647 if (bad) av_die("gdn negexp");
648 return 0;
649#elif AV_C
650 memcpy(dst, c, (size_t)n * sizeof *c);
651 free(c);
652 return 0;
653#else
654 return (st == OMEGA_OK && res == 0) ? 0 : -1;
655#endif
656}
657
658static inline int av_fp8_pack_x(uint32_t k, const float *src, float *dst) {
659 if (k == 0) return 0;
660 if (!src || !dst) return -1;
661#if AV_C
662 float *c = malloc((size_t)k * sizeof *c);
663 if (!c) return -1;
664 for (uint32_t i = 0; i < k; i++) c[i] = src[i] * omega_hostmath_pow2f(AV_FP8_FOLD);
665#endif
666#if AV_OM
667 uint64_t res = 1;
668 const uint32_t st = omega_av_fp8x(&res, src, k, dst, k);
669#endif
670#if AV_DIFF
671 av_flip(dst, (size_t)k * 4u);
672 int bad = (st != OMEGA_OK || res != 0 || memcmp(dst, c, (size_t)k * 4u) != 0);
673 free(c);
674 if (bad) av_die("fp8 pack x");
675 return 0;
676#elif AV_C
677 memcpy(dst, c, (size_t)k * sizeof *c);
678 free(c);
679 return 0;
680#else
681 return (st == OMEGA_OK && res == 0) ? 0 : -1;
682#endif
683}
684
685static inline int av_fp8_pack_x_bf16(uint32_t k, const uint16_t *src, float *dst) {
686 if (k == 0) return 0;
687 if (!src || !dst) return -1;
688#if AV_C
689 float *c = malloc((size_t)k * sizeof *c);
690 if (!c) return -1;
691 for (uint32_t i = 0; i < k; i++) {
692 uint32_t u = (uint32_t)src[i] << 16;
693 float f;
694 memcpy(&f, &u, 4);
695 c[i] = f * omega_hostmath_pow2f(AV_FP8_FOLD);
696 }
697#endif
698#if AV_OM
699 uint64_t res = 1;
700 const uint32_t st = omega_av_fp8xb(&res, src, k, dst, k);
701#endif
702#if AV_DIFF
703 av_flip(dst, (size_t)k * 4u);
704 int bad = (st != OMEGA_OK || res != 0 || memcmp(dst, c, (size_t)k * 4u) != 0);
705 free(c);
706 if (bad) av_die("fp8 pack x bf16");
707 return 0;
708#elif AV_C
709 memcpy(dst, c, (size_t)k * sizeof *c);
710 free(c);
711 return 0;
712#else
713 return (st == OMEGA_OK && res == 0) ? 0 : -1;
714#endif
715}
716
717#endif