okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-vit-exact: the picture reader follows the float32 reference closer on the CPU and on the GPU60d431e2a

main
4.6 KiB91 linesraw
1// tools/vision/own/vit_f16.h - the GPU reader's rules for holding a BF16 matrix as F16 (vit_gpu.cu), plain C++ so the CPU unit test
2// (test_cpu_kernels.cpp) checks them without a GPU.
3#pragma once
4
5#include <cmath>
6#include <cstdint>
7
8#if defined(__CUDACC__) || defined(__HIP__)
9#define VIT_F16_HD __host__ __device__
10#else
11#define VIT_F16_HD
12#endif
13
14namespace strata_vit {
15
16// how a BF16 matrix reaches the GPU's products (STRATA_VIT_BF16_AS_F16)
17enum Bf16Mode {
18    BF16_STORED = 0,     // BF16 weights, BF16 inputs
19    BF16_ROUNDED = 1,    // weights as F16, inputs rounded to BF16 and then written as F16 (the gfx1151 kind: the products see BF16 values)
20    BF16_TO_F16 = 2,     // weights as F16, inputs written as F16 (10-bit mantissa instead of BF16's 7)
21};
22
23// the mode from STRATA_VIT_BF16_AS_F16 (env, nullptr when unset) and the card's default: "2" is BF16_TO_F16; "0", an empty or a non-numeric value
24// BF16_STORED and every other number BF16_ROUNDED, as before the F16 kind existed
25inline int vit_gpu_bf16_mode(const char* env, int card_default) {
26    if (!env) return card_default;
27    long v = 0;
28    bool neg = false;
29    const char* p = env;
30    while (*p == ' ' || *p == '\t') ++p;
31    if (*p == '+' || *p == '-') { neg = *p == '-'; ++p; }
32    for (; *p >= '0' && *p <= '9'; ++p) { v = v * 10 + (*p - '0'); if (v > 1000000) break; }
33    if (neg) v = -v;
34    return v == 0 ? BF16_STORED : v == 2 ? BF16_TO_F16 : BF16_ROUNDED;
35}
36
37// true when the BF16 value b is an F16 value: finite, |v| <= 65504, and no mantissa bit below F16's step (2^-24 under 2^-14).  Every BF16 value
38// with an exponent from -14 to 15 is one (the largest, 65,280, included); below 2^-14 only those whose lowest set bit is at 2^-24 or above.
39VIT_F16_HD inline bool bf16_exact_in_f16(uint16_t b) {
40    const int e = (b >> 7) & 0xff, m = b & 0x7f;
41    if (e == 0xff) return false;                 // inf, nan
42    if (e == 0) return m == 0;                   // +-0; BF16 subnormals (under 1.2e-38) are not F16 values
43    const int E = e - 127;
44    if (E > 15) return false;                    // 65,536 and above
45    if (E >= -17) return true;                   // the 8 mantissa bits (with the leading one) land at 2^(E-7) >= 2^-24
46    if (E < -24) return false;
47    const int s = -17 - E;                       // 1..7 bits under 2^-24
48    return (m & ((1 << s) - 1)) == 0;
49}
50
51// an activation clamped to F16's finite range (NaN stays NaN), before it is written as F16
52VIT_F16_HD inline float f16_clamp(float v) { return v > 65504.0f ? 65504.0f : v < -65504.0f ? -65504.0f : v; }
53
54// true when the BF16 value b lies beyond F16's range (|v| >= 65,536 by its exponent, inf, nan): a matrix with one stays BF16
55VIT_F16_HD inline bool bf16_beyond_f16(uint16_t b) { return ((b >> 7) & 0xff) > 127 + 15; }
56inline long long bf16_count_beyond_f16(const uint16_t* w, long long n) {
57    long long c = 0;
58    for (long long i = 0; i < n; ++i) c += bf16_beyond_f16(w[i]);
59    return c;
60}
61
62// whether a BF16 matrix may be held as F16 in BF16_TO_F16 mode: no weight beyond F16's range.  Weights under 2^-14 that F16 holds only to its
63// 2^-24 step (an error of at most 2^-25 each) are counted for the trace but do not keep the matrix in BF16.
64inline bool bf16_matrix_fits_f16(long long n_overflow) { return n_overflow == 0; }
65
66// the absolute error of a BF16 value F16 cannot hold exactly but holds inside its range (|v| < 2^-14): F16 rounds it to nearest even at its
67// 2^-24 step, so the error is at most 2^-25.  0 for an F16 value or one beyond the range (those are not held as F16).
68inline double bf16_f16_abs_error(uint16_t b) {
69    const int e = (b >> 7) & 0xff, m = b & 0x7f;
70    if (bf16_exact_in_f16(b) || bf16_beyond_f16(b)) return 0.0;
71    if (e == 0) return std::ldexp((double) m, -133);                       // a BF16 subnormal: F16 holds 0
72    const double q = std::ldexp((double) (0x80 | m), e - 127 - 7 + 24);   // the value in units of 2^-24
73    return std::ldexp(std::fabs(q - std::nearbyint(q)), -24);
74}
75
76// counts of one BF16 matrix: weights F16 cannot hold exactly, of those the ones beyond its range (|w| > 65504, inf, nan), and the largest
77// absolute error of the others
78struct Bf16F16Count { long long not_exact = 0, overflow = 0; double max_abs_error = 0.0; };
79inline Bf16F16Count bf16_count_f16(const uint16_t* w, long long n) {
80    Bf16F16Count c;
81    for (long long i = 0; i < n; ++i) {
82        if (bf16_exact_in_f16(w[i])) continue;
83        ++c.not_exact;
84        if (bf16_beyond_f16(w[i])) { ++c.overflow; continue; }
85        const double err = bf16_f16_abs_error(w[i]);
86        if (err > c.max_abs_error) c.max_abs_error = err;
87    }
88    return c;
89}
90
91}  // namespace strata_vit