okami.mom/strata

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

waveringanaRestore the DeepSeek V4.1 engine and CPU tests removed upstream8ac8ce32e

main
7.4 KiB137 linesraw
1// Strata. Parity + speed of the gfx12 WMMA trellis GEMV (src/dsv41/ds4_tqw.cuh) on a R9700 (gfx1201):
2//   tqw_hip_test [--k 4,5,6,7] [--code 0|1] [--bench]
3// Parity: random legal streams (any bit pattern is one), host-decoded weights (tqenc::decode_blocks: the table codebook, or the
4// computed code when TQE_CODE=3single, with --code 1) against the kernel's y, relative to sum|w x| per output.
5// --bench: the DeepSeek decode shape (12 gate/up matrices of 2304 x 5120), GB/s of weight bytes.
6#include "../../src/dsv41/ds4_tqw.cuh"
7#include "../../tools/dsv41_lab/wmma_emu.hpp"
8#include "../../tools/trellis/tqenc.hpp"
9
10#include <algorithm>
11#include <chrono>
12#include <cmath>
13#include <cstdio>
14#include <cstdlib>
15#include <cstring>
16#include <random>
17#include <string>
18#include <vector>
19
20using namespace strata::ds4::tqw;
21#define CK(x) do { hipError_t e_ = (x); if (e_ != hipSuccess) { std::printf("HIP error %s at %d\n", hipGetErrorString(e_), __LINE__); std::exit(2); } } while (0)
22
23template <typename T> static T* up(const std::vector<T>& v) { T* d; CK(hipMalloc(&d, v.size() * sizeof(T))); CK(hipMemcpy(d, v.data(), v.size() * sizeof(T), hipMemcpyHostToDevice)); return d; }
24
25template <int K, int NB, int CODE> static void launch(const Job* jd, const uint32_t* lut, int rows, int njobs) {
26    constexpr int T = 2;
27    dim3 grid((rows + 16 * T * 4 - 1) / (16 * T * 4), njobs);
28    gemv<K, NB, T, 1, CODE><<<grid, 128>>>(jd, lut);
29}
30template <int K, int CODE> static void launch_nb(int nb, const Job* jd, const uint32_t* lut, int rows, int njobs) {
31    switch (nb) {
32        case 1: launch<K, 1, CODE>(jd, lut, rows, njobs); break;
33        case 3: launch<K, 3, CODE>(jd, lut, rows, njobs); break;
34        default: launch<K, 5, CODE>(jd, lut, rows, njobs); break;
35    }
36}
37template <int CODE> static void launch_k(int K, int nb, const Job* jd, const uint32_t* lut, int rows, int njobs) {
38    switch (K) {
39        case 4: launch_nb<4, CODE>(nb, jd, lut, rows, njobs); break;
40        case 5: launch_nb<5, CODE>(nb, jd, lut, rows, njobs); break;
41        case 6: launch_nb<6, CODE>(nb, jd, lut, rows, njobs); break;
42        default: launch_nb<7, CODE>(nb, jd, lut, rows, njobs); break;
43    }
44}
45static void run(int code, int K, int nb, const Job* jd, const uint32_t* lut, int rows, int njobs) {
46    if (code) launch_k<1>(K, nb, jd, lut, rows, njobs); else launch_k<0>(K, nb, jd, lut, rows, njobs);
47}
48
49int main(int argc, char** argv) {
50    int code = std::getenv("TQE_CODE") ? 1 : 0;
51    bool bench = false;
52    std::vector<int> Ks = {4, 5, 6, 7};
53    for (int i = 1; i < argc; ++i) {
54        std::string a = argv[i];
55        if (a == "--bench") bench = true;
56        else if (a == "--code" && i + 1 < argc) code = std::atoi(argv[++i]);
57        else if (a == "--k" && i + 1 < argc) { Ks.clear(); for (const char* p = argv[++i]; *p;) { Ks.push_back(std::atoi(p)); while (*p && *p != ',') ++p; if (*p) ++p; } }
58    }
59    std::vector<uint32_t> hl(2048);
60    {
61        const float* pt = tqenc::point_table();
62        for (int i = 0; i < 2048; ++i) hl[i] = tqw::f2h_rn(pt[2 * i]) | ((uint32_t) tqw::f2h_rn(pt[2 * i + 1]) << 16);
63    }
64    const uint32_t* dlut = up(hl);
65    std::mt19937_64 rng(7);
66    int fails = 0;
67    const int types[8] = {0, 0, 0, 0, 225, 224, 145, 146};
68    for (int K : Ks) {
69        const int type = types[K], bsz = 2 + 4 * K;
70        const int rows = 96, in = 1536, nblk = in / 128;
71        std::vector<uint8_t> w((size_t) rows * nblk * bsz);
72        for (auto& b : w) b = (uint8_t) rng();
73        for (int64_t b = 0; b < (int64_t) rows * nblk; ++b) { const uint16_t d = tqw::f2h_rn(0.02f + 0.001f * (float) (rng() % 400)); std::memcpy(&w[(size_t) b * bsz], &d, 2); }
74        std::vector<float> dec((size_t) rows * in);
75        tqenc::decode_blocks(type, w.data(), (int64_t) rows * nblk, dec.data());
76        std::vector<uint32_t> words; std::vector<uint16_t> sc;
77        tqw::repack_tqw(K, w.data(), rows, nblk, words, sc);
78        for (int nb : {1, 3, 5}) {
79            std::vector<float> x((size_t) nb * in);
80            std::uniform_real_distribution<float> u(-1.f, 1.f);
81            for (auto& v : x) v = u(rng);
82            const uint32_t* dw = up(words); const uint16_t* ds = up(sc); const float* dx = up(x);
83            half8_t* xb; CK(hipMalloc(&xb, (size_t) nblk * 8 * 2 * nb * 2 * sizeof(half8_t)));
84            const size_t nall = (size_t) nblk * 8 * 2 * nb;
85            prep_b<<<(unsigned) ((nall + 127) / 128), 128>>>(dx, in, nb, in, xb);
86            float* dy; CK(hipMalloc(&dy, (size_t) nb * rows * 4)); CK(hipMemset(dy, 0, (size_t) nb * rows * 4));
87            std::vector<Job> jv = {Job{dw, ds, xb, dy, rows, in, rows}};
88            const Job* dj = up(jv);
89            run(code, K, nb, dj, dlut, rows, 1);
90            CK(hipDeviceSynchronize());
91            std::vector<float> y((size_t) nb * rows);
92            CK(hipMemcpy(y.data(), dy, y.size() * 4, hipMemcpyDeviceToHost));
93            double worst = 0;
94            for (int c = 0; c < nb; ++c)
95                for (int r = 0; r < rows; ++r) {
96                    double s = 0, sa = 0;
97                    for (int i = 0; i < in; ++i) { const double t = (double) dec[(size_t) r * in + i] * x[(size_t) c * in + i]; s += t; sa += std::fabs(t); }
98                    worst = std::max(worst, std::fabs(y[(size_t) c * rows + r] - s) / (sa + 1e-30));
99                }
100            const bool ok = worst < 3e-7;
101            std::printf("K=%d nb=%d (%s): HIP wmma kernel vs double reference %.2e %s\n", K, nb, code ? "computed" : "table", worst, ok ? "ok" : "FAIL");
102            fails += !ok;
103        }
104    }
105    if (bench) {
106        for (int K : Ks) {
107            const int bsz = 2 + 4 * K, in = 5120, rows = 2304, nblk = in / 128;
108            std::vector<uint8_t> w((size_t) rows * nblk * bsz);
109            for (auto& b : w) b = (uint8_t) rng();
110            std::vector<uint32_t> words; std::vector<uint16_t> sc;
111            tqw::repack_tqw(K, w.data(), rows, nblk, words, sc);
112            for (int nb : {1, 5}) {
113                std::vector<float> x((size_t) nb * in, 0.5f);
114                const float* dx = up(x);
115                half8_t* xb; CK(hipMalloc(&xb, (size_t) nblk * 8 * 2 * nb * 2 * sizeof(half8_t)));
116                const size_t nall = (size_t) nblk * 8 * 2 * nb;
117                prep_b<<<(unsigned) ((nall + 127) / 128), 128>>>(dx, in, nb, in, xb);
118                float* dy; CK(hipMalloc(&dy, (size_t) nb * rows * 4));
119                std::vector<Job> jv;   // 12 distinct buffers, so the L2 cannot serve a repeated matrix
120                for (int j = 0; j < 12; ++j) jv.push_back(Job{up(words), up(sc), xb, dy, rows, in, rows});
121                const Job* dj = up(jv);
122                const double bytes = 12.0 * rows * nblk * bsz;
123                for (int rep = 0; rep < 3; ++rep) run(code, K, nb, dj, dlut, rows, 12);
124                CK(hipDeviceSynchronize());
125                const auto t0 = std::chrono::steady_clock::now();
126                const int reps = 50;
127                for (int rep = 0; rep < reps; ++rep) run(code, K, nb, dj, dlut, rows, 12);
128                CK(hipDeviceSynchronize());
129                const double us = std::chrono::duration<double, std::micro>(std::chrono::steady_clock::now() - t0).count() / reps;
130                std::printf("bench K=%d nb=%d %s: 12 gate/up matrices %.1f us, %.0f GB/s, %.2f Tw/s\n", K, nb, code ? "computed" : "table", us, bytes / us / 1e3,
131                            12.0 * rows * in / us / 1e6);
132            }
133        }
134    }
135    std::printf("%s\n", fails ? "FAILED" : "ALL OK");
136    return fails;
137}