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

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