okami.mom/strata

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

Niko1221Merge branch 'f-140' into f-140-j0fff115df

main
4.7 KiB109 linesraw
1// src/kernels/dequant_bf16_test.cpp - plan v0.3 P5: the device dequantizers against the artifact's validated CPU
2// dequantizers (`strata/artifact/dequant.hpp`), on one real tensor of every type the model uses.
3//
4//     dequant_bf16_test SHARD1.gguf [SHARD2.gguf]
5//
6// For each type: the first 4 rows of the first tensor of that type, FP32 path compared with a relative tolerance of
7// 1e-6 (the products are the reference's, reordered at most), BF16 path within half a BF16 ulp of the CPU value.
8#include "strata/artifact/dequant.hpp"
9#include "strata/artifact/gguf_reader.hpp"
10#include "strata/kernels/dequant_bf16.hpp"
11
12#include <cuda_runtime.h>
13
14#include <cmath>
15#include <cstdio>
16#include <cstring>
17#include <map>
18#include <string>
19#include <vector>
20
21namespace {
22
23bool cpu_block(int type, const uint8_t* b, float* out) {
24    using namespace strata;
25    switch (type) {
26    case 2: dequantize_q4_0(b, out); return true;
27    case 6: dequantize_q5_0(b, out); return true;
28    case 7: dequantize_q5_1(b, out); return true;
29    case 8: dequantize_q8_0(b, out); return true;
30    case 11: dequantize_q3_K(b, out); return true;
31    case 12: dequantize_q4_K(b, out); return true;
32    case 13: dequantize_q5_K(b, out); return true;
33    case 14: dequantize_q6_K(b, out); return true;
34    case 20: dequantize_iq4_nl(b, out); return true;
35    case 23: dequantize_iq4_xs(b, out); return true;
36    case 42: dequantize_q2_0(b, out); return true;
37    default: return false;
38    }
39}
40
41float bf16_to_f32(uint16_t h) {
42    uint32_t u = (uint32_t) h << 16;
43    float f;
44    std::memcpy(&f, &u, 4);
45    return f;
46}
47
48}  // namespace
49
50int main(int argc, char** argv) {
51    if (argc < 2) {
52        std::fprintf(stderr, "usage: dequant_bf16_test SHARD.gguf [SHARD2.gguf]\n");
53        return 2;
54    }
55    std::map<int, bool> done;
56    int fails = 0, checked = 0;
57    for (int a = 1; a < argc; ++a) {
58        strata::GgufFile gguf(argv[a]);
59        for (const auto& t : gguf.tensors()) {
60            const int type = (int) t.type;
61            if (done.count(type) || !strata::kernels::dequant_bf16_supported(type) || t.shape.size() != 2) continue;
62            int be = 0, bb = 0;
63            if (!strata::block_geometry(t.type, be, bb)) continue;
64            const int64_t cols = (int64_t) t.shape[0];
65            const int64_t rows = (std::min<int64_t>)(4, (int64_t) t.shape[1]);
66            const int64_t row_bytes = cols / be * bb;
67            const uint8_t* host = (const uint8_t*) gguf.tensor_data(t);
68            void* d_blocks = nullptr;
69            float* d_f = nullptr;
70            uint16_t* d_h = nullptr;
71            cudaMalloc(&d_blocks, (size_t) (rows * row_bytes));
72            cudaMalloc(&d_f, (size_t) (rows * cols) * 4);
73            cudaMalloc(&d_h, (size_t) (rows * cols) * 2);
74            cudaMemcpy(d_blocks, host, (size_t) (rows * row_bytes), cudaMemcpyHostToDevice);
75            strata::kernels::dequant_f32(type, d_blocks, 0, rows, cols, d_f, nullptr);
76            strata::kernels::dequant_bf16(type, d_blocks, 0, rows, cols, d_h, nullptr);
77            std::vector<float> gf((size_t) (rows * cols));
78            std::vector<uint16_t> gh((size_t) (rows * cols));
79            cudaMemcpy(gf.data(), d_f, gf.size() * 4, cudaMemcpyDeviceToHost);
80            cudaMemcpy(gh.data(), d_h, gh.size() * 2, cudaMemcpyDeviceToHost);
81            cudaFree(d_blocks);
82            cudaFree(d_f);
83            cudaFree(d_h);
84            std::vector<float> ref((size_t) (rows * cols));
85            for (int64_t r = 0; r < rows; ++r)
86                for (int64_t b = 0; b < cols / be; ++b)
87                    cpu_block(type, host + r * row_bytes + b * bb, ref.data() + r * cols + b * be);
88            int64_t bad_f = 0, bad_h = 0;
89            double worst = 0;
90            for (size_t i = 0; i < ref.size(); ++i) {
91                const double e = std::fabs((double) gf[i] - ref[i]);
92                const double tol = 1e-6 * std::fabs((double) ref[i]) + 1e-30;
93                if (e > tol) ++bad_f;
94                worst = (std::max)(worst, e / (std::fabs((double) ref[i]) + 1e-30));
95                const double eh = std::fabs((double) bf16_to_f32(gh[i]) - ref[i]);
96                if (eh > std::ldexp(std::fabs((double) ref[i]), -8) + 1e-30) ++bad_h;
97            }
98            ++checked;
99            done[type] = true;
100            const bool ok = bad_f == 0 && bad_h == 0;
101            if (!ok) ++fails;
102            std::printf("type %2d  %-40s %lld x %lld: f32 %s (%lld off, worst rel %.2e), bf16 %s (%lld off)\n", type,
103                        t.name.c_str(), (long long) rows, (long long) cols, bad_f ? "FAIL" : "ok", (long long) bad_f,
104                        worst, bad_h ? "FAIL" : "ok", (long long) bad_h);
105        }
106    }
107    std::printf("dequant_bf16_test: %d types checked, %s\n", checked, fails ? "FAILED" : "OK");
108    return fails ? 1 : 0;
109}