okami.mom/strata

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

waveringanaedge0-strata: add the engine for Edge0 145B and Edge0 120Bfd19246dc

main
9.4 KiB188 linesraw
1// q3k_real_ref.cpp - CPU-official vs GPU: the Q3_K gate/up (and full native_expert_grouped) on a REAL
2// v7_l1280_q3k expert blob.  The reference uses the OFFICIAL ggml dequant (computed on HPC-1 by
3// iq3tool_q3k, linked ggml-quants.c) for the weights, and the engine's own quantize_q8_1_rows for the
4// activations - so only the Q3_K (gu) / IQ4_NL (dn) GPU dot products are under test.
5#include "strata/kernels/iq_kernels.hpp"
6#include "strata/kernels/f16_bits.hpp"
7
8#include <cuda_runtime.h>
9
10#include <cmath>
11#include <cstdio>
12#include <cstdlib>
13#include <cstring>
14#include <vector>
15
16namespace k = strata::kernels;
17
18static void ck(cudaError_t e, const char* w) {
19    if (e != cudaSuccess) { std::fprintf(stderr, "CUDA %s: %s\n", w, cudaGetErrorString(e)); std::exit(2); }
20}
21
22static std::vector<float> readf(const char* p, size_t n) {
23    std::vector<float> v(n);
24    FILE* f = std::fopen(p, "rb");
25    if (!f) { std::fprintf(stderr, "cannot open %s\n", p); std::exit(2); }
26    std::fread(v.data(), 4, n, f);
27    std::fclose(f);
28    return v;
29}
30
31// q8_1 blocks (fp16 d, fp16 s, 32 int8) back to doubles
32static std::vector<double> dequant_q8_1(const std::vector<uint8_t>& q, size_t n) {
33    std::vector<double> v(n);
34    for (size_t b = 0; b < n / 32; ++b) {
35        uint16_t dh;
36        std::memcpy(&dh, &q[b * 36], 2);
37        const double d = (double) k::f32_from_f16(dh);
38        for (int i = 0; i < 32; ++i) v[b * 32 + i] = d * (int8_t) q[b * 36 + 4 + i];
39    }
40    return v;
41}
42
43int main() {
44    const int64_t H = 1280, FF = 640;   // expert_embd 1280, expert_ff 640
45    const int gu_type = 11, dn_type = 20;
46    const size_t gu_bytes = 704000, dn_bytes = 460800, blob_bytes = gu_bytes + dn_bytes;
47
48    cudaStream_t s;
49    ck(cudaStreamCreate(&s), "stream");
50
51    // ---- official ggml dequant (produced on HPC-1 by iq3tool_q3k dequant) ----
52    const std::vector<float> wf_gu = readf("/home/user/ref_l0e0_gu.f32", (size_t)(2 * FF) * H);   // 1280x1280
53    const std::vector<float> wf_dn = readf("/home/user/ref_l0e0_dn.f32", (size_t) H * FF);         // 1280x640
54
55    // ---- real Q3_K expert blob (layer 0, expert 0) ----
56    std::vector<uint8_t> blob(blob_bytes);
57    {
58        FILE* f = std::fopen("/home/user/models/v7_l1280_q3k_pack/latent_a_pack/experts.bin", "rb");
59        if (!f) { std::fprintf(stderr, "cannot open experts.bin\n"); return 2; }
60        std::fread(blob.data(), 1, blob_bytes, f);
61        std::fclose(f);
62    }
63
64    // ---- a small deterministic activation (NOT lossless; the reference reuses the engine's q8_1) ----
65    std::vector<float> x((size_t) H);
66    for (int j = 0; j < H; ++j) x[(size_t) j] = (float)(0.6 * std::sin(0.13 * (double) j) + 0.2 * std::cos(0.07 * (double) j));
67
68    // ---- device side ----
69    uint8_t* dblob = nullptr; ck(cudaMalloc((void**) &dblob, blob_bytes), "blob");
70    ck(cudaMemcpy(dblob, blob.data(), blob_bytes, cudaMemcpyHostToDevice), "h2d blob");
71
72    // ---- GPU dequant vs OFFICIAL dequant (localizes dequant vs dot vs down) ----
73    {
74        float* gpu_gu = nullptr; ck(cudaMalloc((void**) &gpu_gu, (size_t)(2 * FF) * H * 4), "gpu_gu");
75        k::iq_dequant_f32(11, dblob, (int64_t)(2 * FF) * H, gpu_gu, s);
76        std::vector<float> gg((size_t)(2 * FF) * H);
77        ck(cudaMemcpy(gg.data(), gpu_gu, gg.size() * 4, cudaMemcpyDeviceToHost), "d2h gpu_gu");
78        double mg = 0, mg_rel = 0; int bg = 0;
79        for (size_t i = 0; i < gg.size(); ++i) {
80            double d = std::fabs((double) gg[i] - (double) wf_gu[i]);
81            mg = std::max(mg, d); mg_rel += d / (1.0 + std::fabs((double) wf_gu[i]));
82            if (d > 1e-3 * (1.0 + std::fabs((double) wf_gu[i]))) ++bg;
83        }
84        std::printf("  [gu dequant] max_abs %.6f mean_rel %.2e bad %zu/%zu\n", mg, mg_rel / gg.size(), (size_t) bg, gg.size());
85
86        float* gpu_dn = nullptr; ck(cudaMalloc((void**) &gpu_dn, (size_t) H * FF * 4), "gpu_dn");
87        k::iq_dequant_f32(20, dblob + gu_bytes, (int64_t) H * FF, gpu_dn, s);
88        std::vector<float> gd((size_t) H * FF);
89        ck(cudaMemcpy(gd.data(), gpu_dn, gd.size() * 4, cudaMemcpyDeviceToHost), "d2h gpu_dn");
90        double md = 0, md_rel = 0; int bd = 0;
91        for (size_t i = 0; i < gd.size(); ++i) {
92            double d = std::fabs((double) gd[i] - (double) wf_dn[i]);
93            md = std::max(md, d); md_rel += d / (1.0 + std::fabs((double) wf_dn[i]));
94            if (d > 1e-3 * (1.0 + std::fabs((double) wf_dn[i]))) ++bd;
95        }
96        std::printf("  [dn dequant] max_abs %.6f mean_rel %.2e bad %zu/%zu\n", md, md_rel / gd.size(), (size_t) bd, gd.size());
97        cudaFree(gpu_gu); cudaFree(gpu_dn);
98    }
99    unsigned long long* dptr = nullptr; ck(cudaMalloc((void**) &dptr, 8), "ptr");
100    unsigned long long ptr = (unsigned long long) dblob;
101    ck(cudaMemcpy(dptr, &ptr, 8, cudaMemcpyHostToDevice), "h2d ptr");
102    int32_t *dstart = nullptr, *dn_g = nullptr, *ddst = nullptr, *dtok = nullptr;
103    ck(cudaMalloc((void**) &dstart, 8), "start"); ck(cudaMalloc((void**) &dn_g, 4), "ng");
104    ck(cudaMalloc((void**) &ddst, 4), "dst"); ck(cudaMalloc((void**) &dtok, 4), "tok");
105    int32_t start[2] = {0, 1}, ng = 1, dst = 0, tok = 0;
106    ck(cudaMemcpy(dstart, start, 8, cudaMemcpyHostToDevice), "h2d start");
107    ck(cudaMemcpy(dn_g, &ng, 4, cudaMemcpyHostToDevice), "h2d ng");
108    ck(cudaMemcpy(ddst, &dst, 4, cudaMemcpyHostToDevice), "h2d dst");
109    ck(cudaMemcpy(dtok, &tok, 4, cudaMemcpyHostToDevice), "h2d tok");
110
111    float* dx = nullptr; ck(cudaMalloc((void**) &dx, (size_t) H * 4), "x");
112    ck(cudaMemcpy(dx, x.data(), (size_t) H * 4, cudaMemcpyHostToDevice), "h2d x");
113    const size_t xq_bytes = (size_t)(H / 32) * 36;
114    uint8_t* dxq = nullptr; ck(cudaMalloc((void**) &dxq, xq_bytes), "xq");
115    k::quantize_q8_1_rows(dx, 1, H, dxq, s);
116
117    const k::NativeExpertLayout L = k::native_expert_layout(gu_type, dn_type, H, FF);
118    uint8_t* dscr = nullptr; ck(cudaMalloc((void**) &dscr, k::native_expert_scratch_bytes(1, FF)), "scratch");
119    float* dout = nullptr; ck(cudaMalloc((void**) &dout, (size_t) H * 4), "out");
120    k::native_expert_grouped(L, dptr, dstart, dn_g, ddst, dtok, 1, 1, dxq, dscr, dout, s);
121    ck(cudaStreamSynchronize(s), "sync");
122
123    std::vector<float> out_gpu((size_t) H);
124    ck(cudaMemcpy(out_gpu.data(), dout, (size_t) H * 4, cudaMemcpyDeviceToHost), "d2h out");
125
126    // ---- reference (official weights x engine q8_1) ----
127    std::vector<uint8_t> xq(xq_bytes);
128    ck(cudaMemcpy(xq.data(), dxq, xq_bytes, cudaMemcpyDeviceToHost), "d2h xq");
129    const std::vector<double> xd = dequant_q8_1(xq, (size_t) H);
130
131    std::vector<double> gate((size_t) FF), up((size_t) FF);
132    for (int r = 0; r < FF; ++r) {
133        double g = 0, u = 0;
134        for (int c = 0; c < H; ++c) {
135            g += (double) wf_gu[(size_t) r * H + c] * xd[(size_t) c];
136            u += (double) wf_gu[((size_t) FF + r) * H + c] * xd[(size_t) c];
137        }
138        gate[(size_t) r] = g; up[(size_t) r] = u;
139    }
140
141    std::vector<float> hf((size_t) FF);
142    for (int r = 0; r < FF; ++r) {
143        const double silu = gate[(size_t) r] / (1.0 + std::exp(-gate[(size_t) r]));
144        hf[(size_t) r] = (float)(silu * up[(size_t) r]);
145    }
146    float* dh = nullptr; ck(cudaMalloc((void**) &dh, (size_t) FF * 4), "h");
147    ck(cudaMemcpy(dh, hf.data(), (size_t) FF * 4, cudaMemcpyHostToDevice), "h2d h");
148    const size_t hq_bytes = (size_t)(FF / 32) * 36;
149    uint8_t* dhq = nullptr; ck(cudaMalloc((void**) &dhq, hq_bytes), "hq");
150    k::quantize_q8_1_rows(dh, 1, FF, dhq, s);
151    std::vector<uint8_t> hq(hq_bytes);
152    ck(cudaMemcpy(hq.data(), dhq, hq_bytes, cudaMemcpyDeviceToHost), "d2h hq");
153    const std::vector<double> hd = dequant_q8_1(hq, (size_t) FF);
154
155    std::vector<double> out_ref((size_t) H);
156    for (int r = 0; r < H; ++r) {
157        double o = 0;
158        for (int c = 0; c < FF; ++c) o += (double) wf_dn[(size_t) r * FF + c] * hd[(size_t) c];
159        out_ref[(size_t) r] = o;
160    }
161
162    // ---- compare ----
163    double max_abs = 0, sum_abs = 0, sum_ref = 0;
164    int bad = 0;
165    for (int r = 0; r < H; ++r) {
166        const double d = std::fabs((double) out_gpu[(size_t) r] - out_ref[(size_t) r]);
167        max_abs = std::max(max_abs, d);
168        sum_abs += d;
169        sum_ref += std::fabs(out_ref[(size_t) r]);
170        if (d > 1e-3 * (1.0 + std::fabs(out_ref[(size_t) r]))) ++bad;
171    }
172    std::printf("q3k_real_ref: H=%lld FF=%lld\n", (long long) H, (long long) FF);
173    std::printf("  gate rms %.4f  up rms %.4f  h rms %.4f\n",
174                std::sqrt([&]{double s2=0;for(double v:gate)s2+=v*v;return s2/(double)FF;}( )),
175                std::sqrt([&]{double s2=0;for(double v:up)s2+=v*v;return s2/(double)FF;}( )),
176                std::sqrt([&]{double s2=0;for(double v:out_ref)s2+=v*v;return s2/(double)H;}( )));
177    std::printf("  out: gpu[0..4] = %.5f %.5f %.5f %.5f %.5f\n", out_gpu[0], out_gpu[1], out_gpu[2], out_gpu[3], out_gpu[4]);
178    std::printf("  out: ref[0..4] = %.5f %.5f %.5f %.5f %.5f\n", out_ref[0], out_ref[1], out_ref[2], out_ref[3], out_ref[4]);
179    std::printf("  max_abs_diff %.6f  mean_abs_diff %.6f  mean_ref %.6f  bad_rows %d/%d\n",
180                max_abs, sum_abs / (double) H, sum_ref / (double) H, bad, (int) H);
181    const bool ok = max_abs < 1e-3 * (1.0 + sum_ref / (double) H);
182    std::printf("q3k_real_ref: %s\n", ok ? "MATCH (GPU == official reference)" : "MISMATCH (GPU differs from official)");
183
184    cudaFree(dblob); cudaFree(dptr); cudaFree(dstart); cudaFree(dn_g); cudaFree(ddst); cudaFree(dtok);
185    cudaFree(dx); cudaFree(dxq); cudaFree(dscr); cudaFree(dout); cudaFree(dh); cudaFree(dhq);
186    cudaStreamDestroy(s);
187    return ok ? 0 : 1;
188}