interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / tests / bw_hmma_model.h

73 lines · 4003 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* bw_hmma_model.h (OWN-30B T3, 2026-10-13): parametrised C model of one HMMA.16816.F32.BF16 dot product
18 * d = c + sum_{k<16} a_k * b_k (a_k, b_k bf16 values given as f32; products are exact in double)
19 * The vendor documents no rounding behaviour for mma (DOCS SILENT, Context7 PTX ISA). Published measurements of earlier
20 * tensor cores (Fasi, Higham, Mikaitis, Pranesh 2021) describe: exact products, alignment of a group of terms to the
21 * largest exponent with the shifted-out bits truncated, exact addition, and a truncating normalisation. This header
22 * implements that family so the chip probe (tests/gpu_tc_probe_test.c) can find the member the GB10 matches bit for bit:
23 * 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
24 * running accumulator in one fused step
25 * extra alignment keeps 24 + extra significant bits below the group's largest exponent (terms truncated toward 0)
26 * rn 0: the fused sum is converted to f32 by truncation (RZ), 1: round to nearest even
27 * Zero, subnormal and huge inputs: products and sums in double; the probe keeps exponents in a range where double is exact.
28 * HM_FMA (group 0) is the plain sequential fmaf chain, for comparison. */
29#ifndef BW_HMMA_MODEL_H
30#define BW_HMMA_MODEL_H
31#include <math.h>
32#include <stdint.h>
33#include <string.h>
34
35typedef struct { int group, extra, rn, nominal, csep; } HmmaModel;
36/* nominal 1: a product's alignment exponent is exp(a) + exp(b) + 1 (its significand taken as [1, 4)), not its true exponent.
37 * csep 1: the group's products are aligned and summed among themselves first; the accumulator is then added exactly and the
38 * total converted once (rn). */
39
40static float hm_to_f32(double v, int rn) {
41 if (rn) return (float)v; /* the C conversion is round to nearest even */
42 float f = (float)v;
43 if ((double)f != v && fabs((double)f) > fabs(v)) f = nextafterf(f, 0.0f); /* round toward zero */
44 return f;
45}
46/* truncate v toward zero to a multiple of 2^qexp */
47static double hm_trunc_to(double v, int qexp) {
48 double q = ldexp(1.0, qexp);
49 return trunc(v / q) * q;
50}
51static float hm_dot16(const HmmaModel *m, const float *a, const float *b, float c) {
52 if (m->group == 0) { float acc = c; for (int k = 0; k < 16; k++) acc = fmaf(a[k], b[k], acc); return acc; }
53 float acc = c;
54 for (int k0 = 0; k0 < 16; k0 += m->group) {
55 double t[17]; int ne[17]; int n = 0;
56 if (!m->csep) { t[n] = (double)acc; { int e; frexp(t[n], &e); ne[n] = e; } n++; }
57 for (int k = k0; k < k0 + m->group; k++) {
58 t[n] = (double)a[k] * (double)b[k];
59 int e; frexp(t[n], &e);
60 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 */
61 ne[n] = e; n++;
62 }
63 int emax = -100000;
64 for (int i = 0; i < n; i++) if (t[i] != 0.0 && ne[i] > emax) emax = ne[i];
65 if (emax == -100000) { if (!m->csep) acc = 0.0f * acc; continue; }
66 double s = 0.0;
67 for (int i = 0; i < n; i++) s += hm_trunc_to(t[i], emax - 24 - m->extra);
68 if (m->csep) s += (double)acc;
69 acc = hm_to_f32(s, m->rn);
70 }
71 return acc;
72}
73#endif