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_hmma_model.h
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* bw_hmma_model.h (OWN-30B T3, 2026-10-13): parametrised C model of one HMMA.16816.F32.BF16 dot product |
| 3 | * d = c + sum_{k<16} a_k * b_k (a_k, b_k bf16 values given as f32; products are exact in double) |
| 4 | * The vendor documents no rounding behaviour for mma (DOCS SILENT, Context7 PTX ISA). Published measurements of earlier |
| 5 | * tensor cores (Fasi, Higham, Mikaitis, Pranesh 2021) describe: exact products, alignment of a group of terms to the |
| 6 | * largest exponent with the shifted-out bits truncated, exact addition, and a truncating normalisation. This header |
| 7 | * implements that family so the chip probe (tests/gpu_tc_probe_test.c) can find the member the GB10 matches bit for bit: |
| 8 | * group G in {1, 2, 4, 8, 16}: the 16 products are consumed in 16/G groups, k order; each group is added to the |
| 9 | * running accumulator in one fused step |
| 10 | * extra alignment keeps 24 + extra significant bits below the group's largest exponent (terms truncated toward 0) |
| 11 | * rn 0: the fused sum is converted to f32 by truncation (RZ), 1: round to nearest even |
| 12 | * Zero, subnormal and huge inputs: products and sums in double; the probe keeps exponents in a range where double is exact. |
| 13 | * HM_FMA (group 0) is the plain sequential fmaf chain, for comparison. */ |
| 14 | #ifndef BW_HMMA_MODEL_H |
| 15 | #define BW_HMMA_MODEL_H |
| 16 | #include <math.h> |
| 17 | #include <stdint.h> |
| 18 | #include <string.h> |
| 19 | |
| 20 | typedef struct { int group, extra, rn, nominal, csep; } HmmaModel; |
| 21 | /* nominal 1: a product's alignment exponent is exp(a) + exp(b) + 1 (its significand taken as [1, 4)), not its true exponent. |
| 22 | * csep 1: the group's products are aligned and summed among themselves first; the accumulator is then added exactly and the |
| 23 | * total converted once (rn). */ |
| 24 | |
| 25 | static float hm_to_f32(double v, int rn) { |
| 26 | if (rn) return (float)v; /* the C conversion is round to nearest even */ |
| 27 | float f = (float)v; |
| 28 | if ((double)f != v && fabs((double)f) > fabs(v)) f = nextafterf(f, 0.0f); /* round toward zero */ |
| 29 | return f; |
| 30 | } |
| 31 | /* truncate v toward zero to a multiple of 2^qexp */ |
| 32 | static double hm_trunc_to(double v, int qexp) { |
| 33 | double q = ldexp(1.0, qexp); |
| 34 | return trunc(v / q) * q; |
| 35 | } |
| 36 | static float hm_dot16(const HmmaModel *m, const float *a, const float *b, float c) { |
| 37 | if (m->group == 0) { float acc = c; for (int k = 0; k < 16; k++) acc = fmaf(a[k], b[k], acc); return acc; } |
| 38 | float acc = c; |
| 39 | for (int k0 = 0; k0 < 16; k0 += m->group) { |
| 40 | double t[17]; int ne[17]; int n = 0; |
| 41 | if (!m->csep) { t[n] = (double)acc; { int e; frexp(t[n], &e); ne[n] = e; } n++; } |
| 42 | for (int k = k0; k < k0 + m->group; k++) { |
| 43 | t[n] = (double)a[k] * (double)b[k]; |
| 44 | int e; frexp(t[n], &e); |
| 45 | if (m->nominal && t[n] != 0.0) { int ea, eb; frexp(a[k], &ea); frexp(b[k], &eb); e = ea + eb - (m->nominal == 2); } /* |a| < 2^ea, |b| < 2^eb; mode 2: exponent of the product of the leading bits, frexp convention */ |
| 46 | ne[n] = e; n++; |
| 47 | } |
| 48 | int emax = -100000; |
| 49 | for (int i = 0; i < n; i++) if (t[i] != 0.0 && ne[i] > emax) emax = ne[i]; |
| 50 | if (emax == -100000) { if (!m->csep) acc = 0.0f * acc; continue; } |
| 51 | double s = 0.0; |
| 52 | for (int i = 0; i < n; i++) s += hm_trunc_to(t[i], emax - 24 - m->extra); |
| 53 | if (m->csep) s += (double)acc; |
| 54 | acc = hm_to_f32(s, m->rn); |
| 55 | } |
| 56 | return acc; |
| 57 | } |
| 58 | #endif |