okami.mom/strata

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

Niko1221Merge f-own-4x 9e19948ff into f-own-int (round 16: Gyro trellis experts on the fused prompt path, opt-in STRATA_PF_FUSED_TQ=1; the golden dot test's fixed draw order)2ec7c3443

main
12.0 KiB224 linesraw
1// The split engine's decode dots (src/qdot.cuh) against llama.cpp's own (ggml-cuda vecdotq.cuh, the vendored third_party/strata-ggml pin) on random
2// blocks: every one of the 11 formats the engine runs, every block of a random matrix, every (kbx, iqs) the decode kernels call with; the floats must
3// be bit for bit the same. Prints one line per format and exits 1 on any difference.
4//   split_qdot_test [blocks per format]      (CUDA: built with the split; HIP: the same sources with -DFNX_HIP=1 GGML_USE_HIP=1)
5//
6// ORACLE NOTE (own-stack plan): this is the one file of the split engine that still includes ggml-cuda (vecdotq.cuh, as the reference). When the
7// vendored ggml tree is removed, this test goes with it: by then qdot.cuh has been proved equal on CUDA and HIP and the engine's identity runs
8// guard it (the proposal in phaseA-tests/par/RESULTS-OWN-4S.md; frozen reference vectors are the alternative if the coordinator wants a test left).
9// That alternative is taken: split_qdot_test --write-golden <file> writes the digests of qdot_golden.cuh's inputs (only when every dot equals ggml's),
10// --regen-golden <file> checks the stored file comes out byte for byte again, and split_qdot_golden (qdot_golden.cu, no ggml) checks qdot.cuh against
11// the file: that test stays when this one goes.
12#include <cuda_fp16.h>
13#include <cuda_runtime.h>
14
15#include <cstdint>
16#include <cstdio>
17#include <cstdlib>
18#include <cstring>
19#include <random>
20#include <vector>
21
22#if FNX_HIP
23// as the engine had it before qdot.cuh: ggml's HIP vendor header defines these three itself
24#undef __vsubss4
25#undef __vsub4
26#undef __vcmpne4
27#undef __shfl_sync
28#undef __shfl_up_sync
29#undef __shfl_xor_sync
30#endif
31#include "common.cuh"
32#include "vecdotq.cuh"
33#if FNX_HIP
34#undef cudaFuncSetAttribute
35#endif
36
37#include "qdot.cuh"
38#include "qdot_golden.cuh"
39
40namespace {
41
42using namespace fnx;
43
44#define FNX_QD_PAIR(T, NAME, VDRG)                                                                                                   \
45    template <> struct Pair<T> {                                                                                                      \
46        static constexpr int vdr = VDRG;                                                                                              \
47        __device__ static float ggml(const void* v, const block_q8_1* y, int k, int i) { return ::NAME(v, y, k, i); }                \
48        __device__ static float ours(const void* v, const block_q8_1* y, int k, int i) { return fnx::NAME(v, y, k, i); }             \
49    };
50
51template <GgmlType T> struct Pair;
52FNX_QD_PAIR(T_Q4_0, vec_dot_q4_0_q8_1, VDR_Q4_0_Q8_1_MMVQ)
53FNX_QD_PAIR(T_Q5_1, vec_dot_q5_1_q8_1, VDR_Q5_1_Q8_1_MMVQ)
54FNX_QD_PAIR(T_Q8_0, vec_dot_q8_0_q8_1, VDR_Q8_0_Q8_1_MMVQ)
55FNX_QD_PAIR(T_Q4_K, vec_dot_q4_K_q8_1, VDR_Q4_K_Q8_1_MMVQ)
56FNX_QD_PAIR(T_Q5_K, vec_dot_q5_K_q8_1, VDR_Q5_K_Q8_1_MMVQ)
57FNX_QD_PAIR(T_Q6_K, vec_dot_q6_K_q8_1, VDR_Q6_K_Q8_1_MMVQ)
58FNX_QD_PAIR(T_IQ4_NL, vec_dot_iq4_nl_q8_1, VDR_IQ4_NL_Q8_1_MMVQ)
59FNX_QD_PAIR(T_IQ4_XS, vec_dot_iq4_xs_q8_1, VDR_IQ4_XS_Q8_1_MMVQ)
60FNX_QD_PAIR(T_IQ3_S, vec_dot_iq3_s_q8_1, VDR_IQ3_S_Q8_1_MMVQ)
61FNX_QD_PAIR(T_IQ3_XXS, vec_dot_iq3_xxs_q8_1, VDR_IQ3_XXS_Q8_1_MMVQ)
62FNX_QD_PAIR(T_IQ2_S, vec_dot_iq2_s_q8_1, VDR_IQ2_S_Q8_1_MMVQ)
63
64// one thread per (weight block, lane): the call the decode kernels make (kernels.cu k_mmvq: y + kbx * (qk / QK8_1), iqs = vdr * lane)
65template <GgmlType T>
66__global__ void k_cmp(const char* w, const block_q8_1* y, int nblocks, unsigned* bad, unsigned* checked, unsigned* trait_bad) {
67    constexpr int qk = qtraits<T>::qk, qi = qtraits<T>::qi, per = qi / Pair<T>::vdr;
68    if (blockIdx.x == 0 && threadIdx.x == 0 &&
69        (qk != ggml_cuda_type_traits<(ggml_type) T>::qk || qi != ggml_cuda_type_traits<(ggml_type) T>::qi)) atomicAdd(trait_bad, 1u);
70    const int e = blockIdx.x * blockDim.x + threadIdx.x;
71    if (e >= nblocks * per) return;
72    const int kbx = e / per, iqs = Pair<T>::vdr * (e % per);
73    const block_q8_1* yb = y + (size_t) kbx * (qk / QK8_1);
74    const float a = Pair<T>::ggml(w, yb, kbx, iqs), b = Pair<T>::ours(w, yb, kbx, iqs);
75    atomicAdd(checked, 1u);
76    if (__float_as_uint(a) != __float_as_uint(b)) atomicAdd(bad, 1u);
77}
78
79__half finite_half(std::mt19937& rng) { return __float2half((float) ((int) (rng() % 2001) - 1000) / 400.0f); }
80
81// B: block bytes; h16: byte offsets of the block's fp16 fields (made finite: random bytes can be NaN / inf there)
82template <GgmlType T>
83bool run(const char* name, int B, std::vector<int> h16, int nblocks, unsigned seed, int our_vdr) {
84    constexpr int qk = qtraits<T>::qk, qi = qtraits<T>::qi;
85    if (our_vdr != Pair<T>::vdr) {
86        std::printf("%s: VDR %d, ggml's %d  <-- MISMATCH\n", name, our_vdr, Pair<T>::vdr);
87        return false;
88    }
89    std::mt19937 rng(seed);
90    std::vector<unsigned char> w((size_t) nblocks * B);
91    for (auto& c : w) c = (unsigned char) rng();
92    for (int k = 0; k < nblocks; ++k)
93        for (int off : h16) {
94            const __half h = finite_half(rng);
95            std::memcpy(&w[(size_t) k * B + off], &h, 2);
96        }
97    std::vector<block_q8_1> y((size_t) nblocks * (qk / QK8_1));
98    for (auto& b : y) {
99        for (int i = 0; i < 32; ++i) b.qs[i] = (int8_t) (int) (rng() % 255 - 127);
100        // s drawn first, then d (qdot_golden.cuh make_inputs: the same draws, a fixed order on every compiler)
101        const float s = (float) ((int) (rng() % 200) - 100);
102        const float d = (float) (rng() % 1000) / 500.0f;
103        b.ds = make_half2(__float2half(d), __float2half(s));
104    }
105    char* dw;
106    block_q8_1* dy;
107    unsigned *dbad, *dchk, *dtr;
108    cudaMalloc(&dw, w.size());
109    cudaMalloc(&dy, y.size() * sizeof(block_q8_1));
110    cudaMalloc(&dbad, 4);
111    cudaMalloc(&dchk, 4);
112    cudaMalloc(&dtr, 4);
113    cudaMemcpy(dw, w.data(), w.size(), cudaMemcpyHostToDevice);
114    cudaMemcpy(dy, y.data(), y.size() * sizeof(block_q8_1), cudaMemcpyHostToDevice);
115    cudaMemset(dbad, 0, 4);
116    cudaMemset(dchk, 0, 4);
117    cudaMemset(dtr, 0, 4);
118    const int per = qi / Pair<T>::vdr;
119    k_cmp<T><<<(nblocks * per + 255) / 256, 256>>>(dw, dy, nblocks, dbad, dchk, dtr);
120    const cudaError_t e = cudaDeviceSynchronize();
121    unsigned bad = 0, chk = 0, tr = 0;
122    cudaMemcpy(&bad, dbad, 4, cudaMemcpyDeviceToHost);
123    cudaMemcpy(&chk, dchk, 4, cudaMemcpyDeviceToHost);
124    cudaMemcpy(&tr, dtr, 4, cudaMemcpyDeviceToHost);
125    cudaFree(dw); cudaFree(dy); cudaFree(dbad); cudaFree(dchk); cudaFree(dtr);
126    const bool ok = e == cudaSuccess && bad == 0 && tr == 0 && chk == (unsigned) nblocks * per;
127    std::printf("%-8s %9u dots compared, %u differ, traits %s%s%s\n", name, chk, bad, tr ? "DIFFER" : "same", e == cudaSuccess ? "" : "  cuda error: ",
128                e == cudaSuccess ? (ok ? "" : "  <-- MISMATCH") : cudaGetErrorString(e));
129    return ok;
130}
131
132// --write-golden / --regen-golden: ggml's dots on qdot_golden.cuh's inputs, in its order (the oracle side of the stored digests)
133template <GgmlType T> __global__ void k_ggml(const char* w, const block_q8_1* y, int nblocks, float* out) {
134    constexpr int qk = qtraits<T>::qk, per = qtraits<T>::qi / Pair<T>::vdr;
135    const int e = blockIdx.x * blockDim.x + threadIdx.x;
136    if (e >= nblocks * per) return;
137    const int kbx = e / per, iqs = Pair<T>::vdr * (e % per);
138    out[e] = Pair<T>::ggml(w, y + (size_t) kbx * (qk / QK8_1), kbx, iqs);
139}
140struct Writer {
141    std::string text;
142    int differ = 0, lines = 0;
143    template <GgmlType T> bool operator()(const fnx_golden::Format& f, const fnx_golden::Inputs& in) {
144        static_assert(Pair<T>::vdr == fnx_golden::Dot<T>::vdr, "the same VDR on both sides");
145        const std::vector<float> ours = fnx_golden::run_ours<T>(in, f.nblocks);
146        std::vector<float> ref(ours.size());
147        char* dw = nullptr;
148        block_q8_1* dy = nullptr;
149        float* dout = nullptr;
150        bool ok = !ours.empty() && cudaMalloc(&dw, in.w.size()) == cudaSuccess &&
151                  cudaMalloc(&dy, in.y.size() * sizeof(block_q8_1)) == cudaSuccess && cudaMalloc(&dout, ref.size() * 4) == cudaSuccess;
152        if (ok) {
153            cudaMemcpy(dw, in.w.data(), in.w.size(), cudaMemcpyHostToDevice);
154            cudaMemcpy(dy, in.y.data(), in.y.size() * sizeof(block_q8_1), cudaMemcpyHostToDevice);
155            k_ggml<T><<<((int) ref.size() + 255) / 256, 256>>>(dw, dy, f.nblocks, dout);
156            ok = cudaDeviceSynchronize() == cudaSuccess && cudaMemcpy(ref.data(), dout, ref.size() * 4, cudaMemcpyDeviceToHost) == cudaSuccess;
157        }
158        cudaFree(dw); cudaFree(dy); cudaFree(dout);
159        if (!ok) { std::printf("%-8s cuda error\n", f.name); ++differ; return false; }
160        int bad = 0;
161        for (size_t i = 0; i < ours.size(); ++i) bad += std::memcmp(&ours[i], &ref[i], 4) != 0;
162        differ += bad;
163        const std::vector<fnx_golden::Line> g = fnx_golden::group_lines("cuda", f.name, in, ours);
164        for (const fnx_golden::Line& l : g) text += fnx_golden::line_text(l) + "\n";
165        lines += (int) g.size();
166        std::printf("%-8s %7zu dots against ggml: %d differ, %zu groups\n", f.name, ours.size(), bad, g.size());
167        return bad == 0;
168    }
169};
170int golden(const char* mode, const char* path) {
171    Writer wr;
172    wr.text = fnx_golden::kHeader;
173    const bool same = fnx_golden::for_each_format(wr);
174    if (!same || wr.differ) { std::printf("qdot.cuh differs from ggml on %d dots: nothing written\n", wr.differ); return 1; }
175    if (std::strcmp(mode, "--write-golden") == 0) {
176        FILE* f = std::fopen(path, "wb");
177        if (!f || std::fwrite(wr.text.data(), 1, wr.text.size(), f) != wr.text.size()) { std::printf("cannot write %s\n", path); return 1; }
178        std::fclose(f);
179        std::printf("wrote %s: %d groups, %zu bytes\n", path, wr.lines, wr.text.size());
180        return 0;
181    }
182    // --regen-golden: the cuda lines and the header must come out byte for byte as stored (lines of other families are kept as they are)
183    std::string stored;
184    if (FILE* f = std::fopen(path, "rb")) {
185        char b[4096];
186        size_t k;
187        while ((k = std::fread(b, 1, sizeof b, f)) > 0) stored.append(b, k);
188        std::fclose(f);
189    } else { std::printf("cannot read %s\n", path); return 1; }
190    std::string kept;   // the stored text without other families' lines
191    for (size_t p = 0; p < stored.size();) {
192        size_t e = stored.find('\n', p);
193        e = e == std::string::npos ? stored.size() : e + 1;
194        const std::string ln = stored.substr(p, e - p);
195        if (ln.rfind("hip ", 0) != 0) kept += ln;
196        p = e;
197    }
198    const bool eq = kept == wr.text;
199    std::printf("regenerated %d groups, %zu bytes: %s the stored file (%zu bytes without other families' lines)\n", wr.lines, wr.text.size(),
200                eq ? "byte for byte equal to" : "DIFFERENT FROM", kept.size());
201    return eq ? 0 : 1;
202}
203
204}  // namespace
205
206int main(int argc, char** argv) {
207    // split_qdot_test --write-golden <file> | --regen-golden <file>: the digests of qdot_golden.cuh (see there); otherwise the comparison below
208    if (argc > 2 && (std::strcmp(argv[1], "--write-golden") == 0 || std::strcmp(argv[1], "--regen-golden") == 0)) return golden(argv[1], argv[2]);
209    const int n = argc > 1 ? std::atoi(argv[1]) : 1 << 18;
210    bool ok = true;
211    ok &= run<T_Q4_0>("Q4_0", sizeof(block_q4_0), {0}, n * 8, 1, VDR_Q4_0);
212    ok &= run<T_Q5_1>("Q5_1", sizeof(block_q5_1), {0, 2}, n * 8, 2, VDR_Q5_1);
213    ok &= run<T_Q8_0>("Q8_0", sizeof(block_q8_0), {0}, n * 8, 3, VDR_Q8_0);
214    ok &= run<T_Q4_K>("Q4_K", sizeof(block_q4_K), {0, 2}, n, 4, VDR_Q4_K);
215    ok &= run<T_Q5_K>("Q5_K", sizeof(block_q5_K), {0, 2}, n, 5, VDR_Q5_K);
216    ok &= run<T_Q6_K>("Q6_K", sizeof(block_q6_K), {(int) sizeof(block_q6_K) - 2}, n, 6, VDR_Q6_K);
217    ok &= run<T_IQ4_NL>("IQ4_NL", sizeof(block_iq4_nl), {0}, n * 8, 7, VDR_IQ4_NL);
218    ok &= run<T_IQ4_XS>("IQ4_XS", sizeof(block_iq4_xs), {0}, n, 8, VDR_IQ4_XS);
219    ok &= run<T_IQ3_S>("IQ3_S", sizeof(block_iq3_s), {0}, n, 9, VDR_IQ3_S);
220    ok &= run<T_IQ3_XXS>("IQ3_XXS", sizeof(block_iq3_xxs), {0}, n, 10, VDR_IQ3_XXS);
221    ok &= run<T_IQ2_S>("IQ2_S", sizeof(block_iq2_s), {0}, n, 11, VDR_IQ2_S);
222    std::printf(ok ? "qdot_test: PASS\n" : "qdot_test: FAIL\n");
223    return ok ? 0 : 1;
224}