okami.mom/strata

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

Niko1221Merge f-bw-nvfp4-m17 b933674ed into the batch 19 candidate (NVFP4 prompt experts on the int8 kernels, the bench's NVFP4 row, the staged group entry for NVFP4 layers, the start-up line's NVFP4 count)52d2d3ee2

main
7.8 KiB156 linesraw
1// tests/quant/nvfp4_decode_model_test.cpp - the CPU model of the fused prompt kernels' NVFP4 weight decode (src/prefill/moe_fused_iq.cu: load_unit /
2// convert<T_NVFP4>, ue4m3_half) against the engine's own CPU dequantizer (strata::quant, which is ggml's dequantize_row_nvfp4), no GPU, no ggml.
3//
4// The kernels turn a 36-byte block {uint8 d[4] (UE4M3, one per 16 values); uint8 qs[32] (E2M1 codes, split-half inside each 16-value group: byte j
5// of a group holds value j in its low and value 8 + j in its high nibble)} into, per 32-value sub-block ib (0, 1): eight 32-bit words of int8 (natural
6// value order; the E2M1 value doubled: 0, 1, 2, 3, 4, 6, 8, 12, signs by bit 3) and two scales s0 (values 0-15) and s1 (16-31) = the UE4M3 byte
7// halved.  The weight is s * q, exact in FP32.  This file holds the same decode written from the format's definition (not from the kernel's bit
8// tricks) and checks, for every scale byte and every code and for seeded random blocks, that
9//   - the kernel's scale construction (the bit form of ue4m3_half, copied here word for word) equals the definition for all 256 bytes,
10//   - model value s * q equals strata::quant::dequant_row (bit for bit),
11//   - the int8 sums the mma forms stay inside 2^22 (the MAGIC-bias trick of the kernels needs |sum q a| < 2^22): worst case 16 * 12 * 127 per scale group.
12// The GPU side of the same statement (the real kernel code against ggml's own dequantizer) is tests/cuda/prefill_decode_golden.cu.
13#include <cmath>
14#include <cstdint>
15#include <cstdio>
16#include <cstring>
17#include <vector>
18
19#include "strata/quant/dequant.hpp"
20#include "strata/quant/types.hpp"
21
22namespace sq = strata::quant;
23
24static int g_fail = 0;
25#define CHECK(c, ...)                                  \
26    do {                                               \
27        if (!(c)) {                                    \
28            ++g_fail;                                  \
29            if (g_fail < 20) {                         \
30                std::printf("FAIL line %d: ", __LINE__); \
31                std::printf(__VA_ARGS__);              \
32                std::printf("\n");                     \
33            }                                          \
34        }                                              \
35    } while (0)
36
37namespace {
38
39// the format's definition: E2M1 (1 sign, 2 exponent, 1 mantissa bit), doubled, as an int
40int e2m1_doubled(int code) {
41    const int s = (code >> 3) & 1, e = (code >> 1) & 3, m = code & 1;
42    // value = (-1)^s * (e == 0 ? m * 0.5 : (1 + m / 2) * 2^(e - 1)); doubled: e == 0: m, else (2 + m) * 2^(e - 1)
43    const int mag = e == 0 ? m : (2 + m) << (e - 1);
44    return s ? -mag : mag;
45}
46// UE4M3 (4 exponent bits bias 7, 3 mantissa bits, no sign, no infinity, 0x7F = NaN) halved; NaN reads as 0 (ggml's CPU and CUDA decoders)
47float ue4m3_half_def(uint8_t x) {
48    if (x == 0 || x == 0x7F) return 0.0f;
49    const int e = (x >> 3) & 0xF, m = x & 7;
50    const double v = e == 0 ? std::ldexp((double) m, -9) : std::ldexp(1.0 + m / 8.0, e - 7);
51    return (float) (v * 0.5);
52}
53// moe_fused_iq.cu's ue4m3_half, the same statements
54float ue4m3_half_kernel(uint32_t x) {
55    x &= 0xFF;
56    if (x == 0 || x == 0x7F) return 0.0f;
57    const uint32_t ex = (x >> 3) & 0xF, man = x & 0x7;
58    const uint32_t bits = ((ex + 119u) << 23) | (man << 20);
59    float f;
60    std::memcpy(&f, &bits, 4);
61    return ex == 0 ? (float) man * 0.0009765625f : f;
62}
63
64struct Sub {
65    int8_t q[32];
66    float s0, s1;
67};
68// sub-block ib of one block, from the definition
69Sub model_sub(const uint8_t* blk, int ib) {
70    Sub r;
71    for (int g = 0; g < 2; ++g) {          // the two 16-value groups of the sub-block
72        const int grp = 2 * ib + g;        // group 0..3 of the block
73        for (int j = 0; j < 8; ++j) {
74            const uint8_t b = blk[4 + 8 * grp + j];
75            r.q[16 * g + j] = (int8_t) e2m1_doubled(b & 15);
76            r.q[16 * g + 8 + j] = (int8_t) e2m1_doubled(b >> 4);
77        }
78    }
79    r.s0 = ue4m3_half_def(blk[2 * ib]);
80    r.s1 = ue4m3_half_def(blk[2 * ib + 1]);
81    return r;
82}
83
84struct SplitMix {
85    uint64_t s;
86    uint64_t next() {
87        s += 0x9e3779b97f4a7c15ull;
88        uint64_t z = s;
89        z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ull;
90        z = (z ^ (z >> 27)) * 0x94d049bb133111ebull;
91        return z ^ (z >> 31);
92    }
93};
94
95}  // namespace
96
97int main() {
98    // 1. the scale byte: the kernel's bit construction against the definition, all 256 values
99    for (int x = 0; x < 256; ++x) {
100        const float a = ue4m3_half_def((uint8_t) x), b = ue4m3_half_kernel((uint32_t) x);
101        CHECK(std::memcmp(&a, &b, 4) == 0, "UE4M3 byte 0x%02x: definition %.9g, kernel form %.9g", x, a, b);
102    }
103    // the format's own anchors: 0x38 = 1.0 (exp 7, man 0) -> 0.5 halved; 0x7E = 448 -> 224; 0x01 = 2^-9 -> 2^-10
104    CHECK(ue4m3_half_def(0x38) == 0.5f && ue4m3_half_def(0x7E) == 224.0f && ue4m3_half_def(0x01) == std::ldexp(1.0f, -10), "anchors");
105
106    // 2. every (scale byte, code) pair through the engine's dequantizer: one block whose four scale bytes and all 64 codes are controlled
107    int64_t pairs = 0;
108    for (int x = 0; x < 256; ++x) {
109        uint8_t blk[36];
110        for (int s = 0; s < 4; ++s) blk[s] = (uint8_t) x;
111        for (int j = 0; j < 32; ++j) blk[4 + j] = (uint8_t) (((j & 7) * 2) | (((j & 7) * 2 + 1) << 4));   // codes 0..15, each value position gets some code
112        // rotate the codes over the 32 bytes so that over the 16 rounds every code meets every position
113        for (int rot = 0; rot < 16; ++rot) {
114            for (int j = 0; j < 32; ++j) blk[4 + j] = (uint8_t) (((j + rot) & 15) | (((j + 2 * rot + 7) & 15) << 4));
115            float ref[64];
116            CHECK(sq::dequant_row(sq::Type::NVFP4, blk, ref, 64), "dequant_row NVFP4");
117            for (int ib = 0; ib < 2; ++ib) {
118                const Sub m = model_sub(blk, ib);
119                for (int j = 0; j < 32; ++j) {
120                    const float mine = (j < 16 ? m.s0 : m.s1) * (float) m.q[j], want = ref[32 * ib + j];
121                    CHECK(std::memcmp(&mine, &want, 4) == 0 || (mine == 0.0f && want == 0.0f), "scale 0x%02x rot %d sub %d value %d: model %.9g dequant %.9g", x, rot, ib, j, mine, want);
122                    ++pairs;
123                }
124            }
125        }
126    }
127    // 3. seeded random blocks (every byte random, as the golden test's): 4096 blocks, and the integer sums' bound
128    SplitMix g{0x4e564650344d3300ull};
129    int64_t maxsum = 0;
130    for (int b = 0; b < 4096; ++b) {
131        uint8_t blk[36];
132        for (int i = 0; i < 36; i += 8) {
133            const uint64_t r = g.next();
134            for (int k = 0; k < 8 && i + k < 36; ++k) blk[i + k] = (uint8_t) (r >> (8 * k));
135        }
136        float ref[64];
137        CHECK(sq::dequant_row(sq::Type::NVFP4, blk, ref, 64), "dequant_row NVFP4 (random)");
138        for (int ib = 0; ib < 2; ++ib) {
139            const Sub m = model_sub(blk, ib);
140            for (int j = 0; j < 32; ++j) {
141                const float mine = (j < 16 ? m.s0 : m.s1) * (float) m.q[j], want = ref[32 * ib + j];
142                CHECK(std::memcmp(&mine, &want, 4) == 0 || (mine == 0.0f && want == 0.0f), "random block %d sub %d value %d: model %.9g dequant %.9g", b, ib, j, mine, want);
143            }
144            // worst int8 sum of the kernel's m16n8k16 (one scale group of 16) against codes +-127
145            for (int h = 0; h < 2; ++h) {
146                int64_t sum = 0;
147                for (int j = 16 * h; j < 16 * h + 16; ++j) sum += std::abs((int) m.q[j]) * 127;
148                if (sum > maxsum) maxsum = sum;
149            }
150        }
151    }
152    CHECK(maxsum <= 16 * 12 * 127 && 16 * 12 * 127 < (1 << 22), "int8 sum bound %lld", (long long) maxsum);
153    std::printf("nvfp4_decode_model_test: %s (%lld value pairs, scale bytes 256 of 256, max |sum q a| per 16 values %lld of 2^22)\n", g_fail ? "FAIL" : "ok",
154                (long long) pairs, (long long) maxsum);
155    return g_fail ? 1 : 0;
156}