okami.mom/strata

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

Guillaume PUTIERbf16: a NaN stays a quiet NaN in f32 -> bf16, as in ggmle2b0cbcde

main
3.6 KiB80 linesraw
1// src/kernels/bf16_bits_test.cpp - `bf16_bits.hpp` against ggml's rule, over EVERY f32 bit pattern (CPU only).
2//
3// the rounding add `i + 0x7FFF + bit16` carries out of the mantissa for a NaN, so 0x7FFFFFFF came back as
4// -0 and 0x7F800001 as +inf - an upstream NaN was masked instead of propagated.  `ggml_compute_fp32_to_bf16`
5// (ggml-impl.h at the pinned llama.cpp) tests the NaN first and returns `(i >> 16) | 64`.  Three checks, all
6// exhaustive (2^32 inputs, a few seconds):
7//
8//   1. the header equals ggml's function, transcribed below, on every input;
9//   2. on every NON-NaN input it equals the previous rounding, so the change cannot move a finite value or an
10//      infinity by a single bit;
11//   3. every NaN input gives a quiet NaN of the same sign.
12//
13// And the round trip over all 65,536 bf16 patterns: a non-NaN bf16 survives f32 and back unchanged.
14#include "strata/kernels/bf16_bits.hpp"
15
16#include <cstdint>
17#include <cstdio>
18#include <cstring>
19
20namespace {
21
22/// ggml_compute_fp32_to_bf16, ggml/src/ggml-impl.h (MIT, the ggml authors), on raw bits.
23uint16_t ggml_bf16(uint32_t u) {
24    if ((u & 0x7fffffffu) > 0x7f800000u) return (uint16_t) ((u >> 16) | 64);
25    return (uint16_t) ((u + (0x7fffu + ((u >> 16) & 1u))) >> 16);
26}
27
28/// The rule before this fix, kept only to prove the change is confined to NaN inputs.
29uint16_t previous_bf16(uint32_t u) {
30    u = (u + ((u >> 16) & 1u) + 0x7FFFu) & 0xFFFF0000u;
31    return (uint16_t) (u >> 16);
32}
33
34bool is_nan_bits(uint32_t u) { return (u & 0x7fffffffu) > 0x7f800000u; }
35
36}  // namespace
37
38int main() {
39    unsigned long long vs_ggml = 0, vs_previous = 0, nan_lost = 0, nan_inputs = 0, previous_masked = 0;
40    uint32_t first_bad = 0;
41    bool have_bad = false;
42    for (uint64_t k = 0; k <= 0xFFFFFFFFull; ++k) {
43        const uint32_t u = (uint32_t) k;
44        float f;
45        std::memcpy(&f, &u, 4);
46        const uint16_t got = strata::kernels::bf16_from_f32(f);
47        if (got != ggml_bf16(u)) {
48            ++vs_ggml;
49            if (!have_bad) { first_bad = u; have_bad = true; }
50        }
51        if (is_nan_bits(u)) {
52            ++nan_inputs;
53            const uint32_t back = (uint32_t) got << 16;
54            // a quiet NaN (bit 22 of the f32) with the input's sign
55            if (!is_nan_bits(back) || (back & 0x00400000u) == 0 || (back >> 31) != (u >> 31)) ++nan_lost;
56            if (!is_nan_bits((uint32_t) previous_bf16(u) << 16)) ++previous_masked;
57        } else if (got != previous_bf16(u)) {
58            ++vs_previous;
59            if (!have_bad) { first_bad = u; have_bad = true; }
60        }
61    }
62    std::printf("  f32 -> bf16 over 2^32 inputs: %llu differ from ggml, %llu non-NaN differ from the previous rule\n",
63                vs_ggml, vs_previous);
64    std::printf("  NaN inputs: %llu, not a quiet same-sign NaN now: %llu (the previous rule masked %llu of them)\n",
65                nan_inputs, nan_lost, previous_masked);
66    if (have_bad) std::printf("    first mismatch at input 0x%08x\n", first_bad);
67
68    unsigned long long trip_bad = 0;
69    for (uint32_t h = 0; h <= 0xFFFFu; ++h) {
70        const float f = strata::kernels::f32_from_bf16((uint16_t) h);
71        const uint16_t back = strata::kernels::bf16_from_f32(f);
72        const bool nan = is_nan_bits(h << 16);
73        if (back != (nan ? (uint16_t) (h | 64u) : (uint16_t) h)) ++trip_bad;
74    }
75    std::printf("  bf16 -> f32 -> bf16 over 65536 patterns: %llu wrong\n", trip_bad);
76
77    const bool ok = vs_ggml == 0 && vs_previous == 0 && nan_lost == 0 && trip_bad == 0 && previous_masked > 0;
78    std::printf("\nbf16_bits_test %s\n", ok ? "OK" : "FAILED");
79    return ok ? 0 : 1;
80}