okami.mom/strata

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

Niko1221Merge f-dsv41-rel (0be62ccad) into v0.2.3.1: DeepSeek V4.1-Flash on top of the released 0.2.3 line28ce98bf1

main
6.5 KiB117 linesraw
1// tools/trellis/tqenc_hip_test.cpp - GPU encoder vs the CPU encoder: correctness (GPU bytes decode, by the CPU decoder,
2// to exactly the weights whose error the GPU reported), quality (SQNR, planted recovery) and speed (weights/s).
3// Run under the box locks:  flock -s /data/gpu.lock flock -x ~/locks/gpu3.lock env HIP_VISIBLE_DEVICES=3 ./tqenc_hip_test
4#include "tqenc.hpp"
5
6#include <chrono>
7#include <cmath>
8#include <cstdio>
9#include <cstdlib>
10#include <cstring>
11#include <random>
12#include <vector>
13
14extern "C" double tqh_encode(int type, const float* w, const float* h, int64_t nblocks, uint8_t* dst, int iters, int ncand,
15                             int scale_tries, int device, double* err_out, int64_t* stats, double* timing, int fixed_scale_bits);
16
17using namespace tqenc;
18static int g_fail = 0;
19#define CHECK(c, ...) do { if (!(c)) { ++g_fail; std::printf("  FAIL line %d: %s  ", __LINE__, #c); std::printf(__VA_ARGS__); std::printf("\n"); } } while (0)
20static double now() { return std::chrono::duration<double>(std::chrono::steady_clock::now().time_since_epoch()).count(); }
21static const char* tname(int t) { return t == TQK6 ? "TQK6" : t == TQK7 ? "TQK7" : t == TQK5T ? "TQK5T" : t == TQK4T ? "TQK4T" : t == TQK5X ? "TQK5X" : "TQK4X"; }
22
23int main(int argc, char** argv) {
24    const long big = argc > 1 ? std::atol(argv[1]) : 131072;   // blocks for the speed runs
25    std::mt19937_64 rng(77);
26    std::normal_distribution<float> nd(0.f, 1.f);
27    const int types[6] = {TQK4T, TQK5T, TQK4X, TQK5X, TQK6, TQK7};
28    for (int type : types) {
29        const int bb = block_bytes(type);
30        std::printf("== %s\n", tname(type));
31        // 1. correctness + CPU comparison on 2048 Gaussian blocks, without and with importance weights
32        {
33            const int nb = 2048;
34            std::vector<float> w((size_t) nb * 128), h((size_t) nb * 128);
35            for (auto& v : w) v = 0.02f * nd(rng);
36            for (auto& v : h) v = 0.2f + std::fabs(nd(rng));
37            for (int wi = 0; wi < 2; ++wi) {
38                const float* hp = wi ? h.data() : nullptr;
39                std::vector<uint8_t> g((size_t) nb * bb), c((size_t) nb * bb);
40                std::vector<double> ge(nb), ce(nb);
41                int64_t st[2];
42                double tm[2];
43                const double gt = tqh_encode(type, w.data(), hp, nb, g.data(), 1, 1, 1, 0, ge.data(), st, tm, 0);
44                Params p;
45                p.iters = 1;
46                int64_t nf, zb;
47                const double ct = encode_blocks(type, w.data(), hp, nb, c.data(), p, 8, ce.data(), &nf, &zb);
48                std::vector<float> dec((size_t) nb * 128);
49                decode_blocks(type, g.data(), nb, dec.data());
50                double worst = 0;
51                for (int b = 0; b < nb; ++b) {
52                    double e2 = 0;
53                    for (int i = 0; i < 128; ++i) {
54                        const double d = (double) w[b * 128 + i] - dec[b * 128 + i];
55                        e2 += (hp ? hp[b * 128 + i] : 1.0) * d * d;
56                    }
57                    worst = std::max(worst, std::fabs(e2 - ge[b]) / std::max(1e-30, e2 + ge[b]));
58                }
59                CHECK(worst < 1e-5, "GPU reported error differs from the decoded error: %.2e", worst);
60                int same = 0;
61                for (int b = 0; b < nb; ++b) same += std::memcmp(&g[(size_t) b * bb], &c[(size_t) b * bb], bb) == 0;
62                std::printf("  importance %-3s GPU total error %.6e, CPU %.6e (GPU/CPU %.5f); identical blocks %d/%d; decoded-vs-reported worst %.1e\n",
63                            wi ? "yes" : "no", gt, ct, gt / ct, same, nb, worst);
64                CHECK(gt <= ct * 1.002, "GPU error is more than 0.2%% above the CPU's");
65            }
66        }
67        // 2. planted solutions: weights decoded from random streams must be recovered (the scale is found by the refinement)
68        {
69            const int nb = 2048;
70            std::vector<uint8_t> blk((size_t) nb * bb);
71            for (int b = 0; b < nb; ++b) {
72                const float d = std::ldexp(0.5f + (float) (rng() % 1000) / 1000.f, -8 + (int) (rng() % 6));
73                const uint16_t dh = float_to_half(d);
74                blk[(size_t) b * bb] = dh & 255;
75                blk[(size_t) b * bb + 1] = dh >> 8;
76                for (int i = 2; i < bb; ++i) blk[(size_t) b * bb + i] = (uint8_t) rng();
77            }
78            std::vector<float> w((size_t) nb * 128);
79            decode_blocks(type, blk.data(), nb, w.data());
80            std::vector<uint8_t> g((size_t) nb * bb);
81            std::vector<double> ge(nb);
82            tqh_encode(type, w.data(), nullptr, nb, g.data(), 3, 1, 1, 0, ge.data(), nullptr, nullptr, 0);
83            int ok = 0;
84            for (int b = 0; b < nb; ++b) ok += ge[b] == 0.0;
85            std::printf("  planted: %d/%d blocks recovered exactly (scale unknown)\n", ok, nb);
86            CHECK(ok > nb * 0.9, "planted recovery low");
87        }
88        // 3. speed on big Gaussian batches
89        {
90            std::vector<float> w((size_t) big * 128);
91            for (auto& v : w) v = nd(rng);
92            std::vector<uint8_t> g((size_t) big * bb);
93            double en = 0;
94            for (float v : w) en += (double) v * v;
95            struct Cfg { const char* n; int iters, ncand, tries; } cfgs[3] = {{"fast (1 pass + 1)", 0, 1, 1}, {"default (iters 1)", 1, 1, 1}, {"rot3 iters3", 3, 3, 1}};
96            for (auto& c : cfgs) {
97                double tm[2];
98                int64_t stt[2];
99                const double t0 = now();
100                const double e = tqh_encode(type, w.data(), nullptr, big, g.data(), c.iters, c.ncand, c.tries, 0, nullptr, stt, tm, 0);
101                const double s = now() - t0;
102                {
103                    std::vector<float> dec((size_t) big * 128);
104                    decode_blocks(type, g.data(), big, dec.data());
105                    double e2 = 0;
106                    for (size_t i = 0; i < dec.size(); ++i) e2 += ((double) w[i] - dec[i]) * ((double) w[i] - dec[i]);
107                    std::printf("  [check: energy %.6e, reported error %.6e, decoded error %.6e -> SQNR %.3f dB]\n", en, e, e2, 10 * std::log10(en / e2));
108                }
109                std::printf("  %-18s SQNR %.3f dB  %.2f M weights/s (kernels only %.2f M/s, %.0f passes)  total %.2fs  [zero blocks %lld, nonfinite %lld]\n", c.n,
110                            10 * std::log10(en / e), (double) big * 128 / s / 1e6, (double) big * 128 / tm[0] / 1e6, tm[1], s,
111                            (long long) stt[1], (long long) stt[0]);
112            }
113        }
114    }
115    std::printf("\n%s (%d failures)\n", g_fail ? "FAILED" : "OK", g_fail);
116    return g_fail ? 1 : 0;
117}