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 / bw_qmma_model.h
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* bw_qmma_model.h (OWN-30B L1 GEMM, W-L1G, 2026-10-14): parametrised C model of one QMMA.16832.F32.E4M3.E4M3 dot product |
| 3 | * d = c + sum_{k<32} a_k * b_k (a_k, b_k e4m3 values given as f32; products exact in double) |
| 4 | * Same family as tests/bw_hmma_model.h (chip-exact for HMMA: evidence bf9b0378) widened for FP8: the vendor documents no |
| 5 | * rounding behaviour (DOCS SILENT), and FP8 tensor cores of earlier chips are reported to keep fewer accumulator bits than |
| 6 | * f32 (UNVERIFIED for GB10), so `keep` spans 10 .. 32 significant bits below the group's largest exponent. |
| 7 | * group G in {1, 2, 4, 8, 16, 32}: products consumed in 32/G groups, k order, each added to the accumulator in one step |
| 8 | * keep bits kept below the group's largest exponent (terms truncated toward 0) |
| 9 | * rn 0: the fused sum converts to f32 by truncation, 1: round to nearest even |
| 10 | * nominal 1: a product's alignment exponent is exp(a) + exp(b) (frexp), 2: that minus 1 |
| 11 | * csep 1: products aligned among themselves, the accumulator added exactly, one conversion */ |
| 12 | #ifndef BW_QMMA_MODEL_H |
| 13 | #define BW_QMMA_MODEL_H |
| 14 | #include <math.h> |
| 15 | #include <stdint.h> |
| 16 | #include <string.h> |
| 17 | |
| 18 | typedef struct { int group, keep, rn, nominal, csep; } QmmaModel; |
| 19 | |
| 20 | static float qm_to_f32(double v, int rn) { |
| 21 | if (rn) return (float)v; |
| 22 | float f = (float)v; |
| 23 | if ((double)f != v && fabs((double)f) > fabs(v)) f = nextafterf(f, 0.0f); |
| 24 | return f; |
| 25 | } |
| 26 | static double qm_trunc_to(double v, int qexp) { double q = ldexp(1.0, qexp); return trunc(v / q) * q; } |
| 27 | static float qm_dot32(const QmmaModel *m, const float *a, const float *b, float c) { |
| 28 | if (m->group == 0) { float acc = c; for (int k = 0; k < 32; k++) acc = fmaf(a[k], b[k], acc); return acc; } |
| 29 | float acc = c; |
| 30 | for (int k0 = 0; k0 < 32; k0 += m->group) { |
| 31 | double t[33]; int ne[33]; int n = 0; |
| 32 | if (!m->csep) { t[n] = (double)acc; { int e; frexp(t[n], &e); ne[n] = e; } n++; } |
| 33 | for (int k = k0; k < k0 + m->group; k++) { |
| 34 | t[n] = (double)a[k] * (double)b[k]; |
| 35 | int e; frexp(t[n], &e); |
| 36 | if (m->nominal && t[n] != 0.0) { int ea, eb; frexp(a[k], &ea); frexp(b[k], &eb); e = ea + eb - (m->nominal == 2); } |
| 37 | ne[n] = e; n++; |
| 38 | } |
| 39 | int emax = -100000; |
| 40 | for (int i = 0; i < n; i++) if (t[i] != 0.0 && ne[i] > emax) emax = ne[i]; |
| 41 | if (emax == -100000) { if (!m->csep) acc = 0.0f * acc; continue; } |
| 42 | double s = 0.0; |
| 43 | for (int i = 0; i < n; i++) s += qm_trunc_to(t[i], emax - m->keep); |
| 44 | if (m->csep) s += (double)acc; |
| 45 | acc = qm_to_f32(s, m->rn); |
| 46 | } |
| 47 | return acc; |
| 48 | } |
| 49 | |
| 50 | /* e4m3 (OCP FP8, bias 7, no infinities, 0x7f / 0xff NaN) */ |
| 51 | static float qm_e4m3(uint8_t c) { |
| 52 | int s = c >> 7, e = (c >> 3) & 15, mm = c & 7; |
| 53 | float v = e ? ldexpf(1.0f + (float)mm / 8.0f, e - 7) : ldexpf((float)mm / 8.0f, -6); |
| 54 | return s ? -v : v; |
| 55 | } |
| 56 | /* f32 -> e4m3, round to nearest even, saturate to +-448 (PTX cvt.rn.satfinite; NaN handled by the caller) */ |
| 57 | static uint8_t qm_e4m3_rne_sat(float f) { |
| 58 | uint8_t s = signbit(f) ? 0x80 : 0; double a = fabs((double)f); |
| 59 | if (isinf(f) || a > 448.0) return s | 0x7e; |
| 60 | if (a < ldexp(1.0, -6)) return s | (uint8_t)rint(a * 512.0); |
| 61 | int e; double m = frexp(a, &e); m *= 2.0; e -= 1; /* a = m * 2^e, m in [1, 2) */ |
| 62 | double mm = rint((m - 1.0) * 8.0); |
| 63 | if (mm >= 8.0) { mm = 0.0; e++; } |
| 64 | if (e + 7 > 15 || (((e + 7) << 3) | (int)mm) > 0x7e) return s | 0x7e; |
| 65 | return s | (uint8_t)(((e + 7) << 3) | (int)mm); |
| 66 | } |
| 67 | #endif |