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 / gpu / amd / gfx942 / fp8gemv / plain_ref.h

107 lines · 3626 bytes Raw Blame History
1// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2#pragma once
3// Column-order FP8 reference used by the device check in time_fp8gemv.cpp.
4// The decode and the bound are the ones check.om checks: fnuz / allow / order.
5// FNUZ NaN is only 0x80. 0xFF is -240. No epilogue and no bf16: the device
6// arms that this compares against leave those flags clear.
7
8#include <cstdint>
9#include <cstring>
10#include <cmath>
11
12inline uint32_t f32_bits(float x) {
13 uint32_t u = 0;
14 std::memcpy(&u, &x, sizeof(u));
15 return u;
16}
17
18inline float bits_f32(uint32_t u) {
19 float x = 0.f;
20 std::memcpy(&x, &u, sizeof(x));
21 return x;
22}
23
24// check.om fnuzden, then the normal path (exp + 119, mantissa in bits 22:20).
25inline uint32_t fnuz_bits(uint8_t b) {
26 if (b == 0x80) return 0x7fc00000u;
27 uint32_t sign = static_cast<uint32_t>(b & 0x80u) << 24;
28 uint32_t mag = b & 0x7fu;
29 if (mag == 0) return sign;
30 uint32_t exp = mag >> 3;
31 uint32_t mant = mag & 7u;
32 if (exp == 0) {
33 static const uint32_t den[8] = {
34 0, 0x3a800000u, 0x3b000000u, 0x3b400000u,
35 0x3b800000u, 0x3ba00000u, 0x3bc00000u, 0x3be00000u};
36 return sign | den[mant];
37 }
38 return sign | ((exp + 119u) << 23) | (mant << 20);
39}
40
41// check.om i2f. n has to fit in 24 bits, which K+1 does for the listed shapes.
42inline uint32_t i2f_bits(uint32_t n) {
43 if (n == 0) return 0;
44 uint32_t exp = 0;
45 uint32_t t = n;
46 while (t >= 2u && exp < 24u) {
47 t >>= 1;
48 exp += 1;
49 }
50 uint32_t frac = n - (1u << exp);
51 uint32_t mant = frac << (23u - exp);
52 return ((exp + 127u) << 23) | mant;
53}
54
55struct PlainRow {
56 uint32_t bits;
57 uint32_t sumabs;
58};
59
60// s = fma(decode(w[c]), x[c], s) for c in 0..k-1, then one scale multiply.
61// sumabs is the f32 sum of |product| before that scale, which is what allow()
62// multiplies by |scale|.
63inline PlainRow plain_row(const uint8_t* w, const uint32_t* x, uint32_t k,
64 uint32_t scale) {
65 float s = 0.f;
66 float ab = 0.f;
67 for (uint32_t c = 0; c < k; ++c) {
68 float wv = bits_f32(fnuz_bits(w[c]));
69 float xv = bits_f32(x[c]);
70 float prod = std::fmaf(wv, xv, 0.f);
71 ab = std::fmaf(ab, 1.f, bits_f32(f32_bits(prod) & 0x7fffffffu));
72 s = std::fmaf(wv, xv, s);
73 }
74 s = std::fmaf(s, bits_f32(scale), 0.f);
75 PlainRow row;
76 row.bits = f32_bits(s);
77 row.sumabs = f32_bits(ab);
78 return row;
79}
80
81// allow(), then the 8 ulps of |got|+|plain| that order() adds. No fuse.
82inline uint32_t tolerance_bits(uint32_t sumabs, uint32_t k, uint32_t scale,
83 uint32_t got, uint32_t plain) {
84 float b = bits_f32(sumabs);
85 b = std::fmaf(b, bits_f32(i2f_bits(k + 1u)), 0.f);
86 b = std::fmaf(b, bits_f32(0x34000000u), 0.f);
87 b = std::fmaf(b, bits_f32(scale & 0x7fffffffu), 0.f);
88 b = std::fmaf(b, bits_f32(0x40000000u), 0.f);
89 float span = std::fmaf(bits_f32(got & 0x7fffffffu), 1.f,
90 bits_f32(plain & 0x7fffffffu));
91 float slack = std::fmaf(span, bits_f32(0x35800000u), 0.f);
92 b = std::fmaf(b, 1.f, slack);
93 return f32_bits(b);
94}
95
96// Non-finite results must be the same bits. Finite results must sit inside
97// tolerance_bits. A NaN bound does not come out of the expression above for
98// a finite sumabs.
99inline bool within_plain(uint32_t got, uint32_t plain, uint32_t bound) {
100 uint32_t ga = got & 0x7fffffffu;
101 uint32_t pa = plain & 0x7fffffffu;
102 if (ga >= 0x7f800000u || pa >= 0x7f800000u) return got == plain;
103 uint32_t diff = f32_bits(std::fmaf(bits_f32(got), 1.f,
104 bits_f32(plain ^ 0x80000000u))) &
105 0x7fffffffu;
106 return !(bits_f32(diff) > bits_f32(bound));
107}