okami.mom/strata

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

Niko1221Merge f-nvfp4 into f-144-rel66db34dc4

main
5.6 KiB107 linesraw
1// src/kernels/nvfp4_expert_gpu_parity.cpp - the decode path's GPU experts (native_expert_grouped) on real NVFP4
2// blobs with their global scales, against a double-precision expert.
3//
4//     nvfp4_expert_gpu_parity pack/experts.bin [layer expert ...]
5//
6// Eight tokens of N(0,1) activations through one expert: q8_1 input, gate/up with s_gate / s_up, SwiGLU, q8_1 of the
7// hidden, down with s_down on its output. Only the two activation roundings separate it from the reference (~1%).
8#include "strata/kernels/iq_kernels.hpp"
9
10#include "ggml.h"
11
12#include <cuda_runtime.h>
13
14#include <cmath>
15#include <cstdio>
16#include <cstdlib>
17#include <cstring>
18#include <fstream>
19#include <random>
20#include <vector>
21
22namespace k = strata::kernels;
23
24namespace {
25void ck(cudaError_t e, const char* what) {
26    if (e != cudaSuccess) { std::fprintf(stderr, "%s: %s\n", what, cudaGetErrorString(e)); std::exit(1); }
27}
28}  // namespace
29
30int main(int argc, char** argv) {
31    if (argc < 2) { std::fprintf(stderr, "usage: nvfp4_expert_gpu_parity pack/experts.bin [layer expert ...]\n"); return 2; }
32    constexpr int64_t N = 2560, FF = 640;
33    constexpr int T = 8;
34    const k::NativeExpertLayout L = k::native_expert_layout(40, 40, N, FF);
35    const size_t layer_bytes = 512 * L.bytes;          // L.bytes counts the 16-byte tail
36    std::vector<int> picks;
37    for (int i = 2; i + 1 < argc; i += 2) { picks.push_back(std::atoi(argv[i])); picks.push_back(std::atoi(argv[i + 1])); }
38    if (picks.empty()) picks = {0, 0, 0, 7, 12, 100, 24, 300, 40, 5, 47, 511};
39    std::ifstream f(argv[1], std::ios::binary);
40    std::mt19937 rng(9);
41    std::normal_distribution<float> nd(0.f, 1.f);
42    const ggml_type_traits* tt = ggml_get_type_traits(GGML_TYPE_NVFP4);
43    double worst = 0;
44    for (size_t pi = 0; pi < picks.size(); pi += 2) {
45        const int layer = picks[pi], e = picks[pi + 1];
46        std::vector<uint8_t> blob(L.bytes);
47        f.seekg((std::streamoff) (layer * layer_bytes + (size_t) e * L.bytes));
48        if (!f.read((char*) blob.data(), (std::streamsize) blob.size())) { std::fprintf(stderr, "cannot read\n"); return 2; }
49        float tail[4];
50        std::memcpy(tail, blob.data() + L.tail_off, sizeof tail);
51        std::vector<float> wg((size_t) FF * N), wu((size_t) FF * N), wd((size_t) N * FF), x((size_t) T * N);
52        tt->to_float(blob.data(), wg.data(), FF * N);
53        tt->to_float(blob.data() + L.up_off, wu.data(), FF * N);
54        tt->to_float(blob.data() + L.down_off, wd.data(), N * FF);
55        for (float& v : x) v = nd(rng);
56        std::vector<double> ref((size_t) T * N);
57        for (int t = 0; t < T; ++t) {
58            std::vector<double> h(FF);
59            for (int64_t r = 0; r < FF; ++r) {
60                double g = 0, u = 0;
61                for (int64_t c = 0; c < N; ++c) {
62                    g += (double) wg[(size_t) r * N + c] * x[(size_t) t * N + c];
63                    u += (double) wu[(size_t) r * N + c] * x[(size_t) t * N + c];
64                }
65                g *= tail[0];
66                u *= tail[1];
67                h[r] = g / (1 + std::exp(-g)) * u;
68            }
69            for (int64_t r = 0; r < N; ++r) {
70                double s = 0;
71                for (int64_t c = 0; c < FF; ++c) s += (double) wd[(size_t) r * FF + c] * h[c];
72                ref[(size_t) t * N + r] = s * tail[2];
73            }
74        }
75        uint8_t* db; float *dx, *dout; void *dxq, *scr; unsigned long long* dptr; int32_t *dstart, *dng, *ddst, *dtok;
76        ck(cudaMalloc(&db, blob.size()), "blob");
77        ck(cudaMemcpy(db, blob.data(), blob.size(), cudaMemcpyHostToDevice), "blob");
78        ck(cudaMalloc(&dx, x.size() * 4), "x");
79        ck(cudaMemcpy(dx, x.data(), x.size() * 4, cudaMemcpyHostToDevice), "x");
80        ck(cudaMalloc(&dxq, (size_t) T * N / 32 * 36), "xq");
81        ck(cudaMalloc(&dout, (size_t) T * N * 4), "out");
82        ck(cudaMalloc(&scr, k::native_expert_scratch_bytes(T, FF)), "scratch");
83        const unsigned long long ptr = (unsigned long long) db;
84        const int32_t start[2] = {0, T}, ng = 1, ids[T] = {0, 1, 2, 3, 4, 5, 6, 7};
85        ck(cudaMalloc(&dptr, 8), "p"); ck(cudaMemcpy(dptr, &ptr, 8, cudaMemcpyHostToDevice), "p");
86        ck(cudaMalloc(&dstart, 8), "s"); ck(cudaMemcpy(dstart, start, 8, cudaMemcpyHostToDevice), "s");
87        ck(cudaMalloc(&dng, 4), "n"); ck(cudaMemcpy(dng, &ng, 4, cudaMemcpyHostToDevice), "n");
88        ck(cudaMalloc(&ddst, 4 * T), "d"); ck(cudaMemcpy(ddst, ids, 4 * T, cudaMemcpyHostToDevice), "d");
89        ck(cudaMalloc(&dtok, 4 * T), "t"); ck(cudaMemcpy(dtok, ids, 4 * T, cudaMemcpyHostToDevice), "t");
90        k::quantize_q8_1_rows(dx, T, N, dxq, nullptr);
91        k::native_expert_grouped(L, dptr, dstart, dng, ddst, dtok, 1, T, dxq, scr, dout, nullptr);
92        ck(cudaDeviceSynchronize(), "run");
93        std::vector<float> got((size_t) T * N);
94        ck(cudaMemcpy(got.data(), dout, got.size() * 4, cudaMemcpyDeviceToHost), "out");
95        for (void* p : {(void*) db, (void*) dx, dxq, (void*) dout, scr, (void*) dptr, (void*) dstart, (void*) dng,
96                        (void*) ddst, (void*) dtok})
97            cudaFree(p);
98        double num = 0, den = 0;
99        for (size_t i = 0; i < got.size(); ++i) { num += (got[i] - ref[i]) * (got[i] - ref[i]); den += ref[i] * ref[i]; }
100        const double rel = std::sqrt(num / den);
101        worst = std::fmax(worst, rel);
102        std::printf("layer %2d expert %3d: s_gate %.3g s_up %.3g s_down %.3g -> expert output rel. error %.3f%%\n", layer,
103                    e, tail[0], tail[1], tail[2], 100 * rel);
104    }
105    std::printf("RESULT: %s (worst %.3f%%)\n", worst < 0.03 ? "ok" : "TOO FAR", 100 * worst);
106    return worst < 0.03 ? 0 : 1;
107}