okami.mom/strata

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

Niko1221Merge f-027-gh-eng: GitHub engine items for 0.2.76ca60e9a0

main
6.3 KiB121 linesraw
1// src/prefill/gemm_dequant_side_parity.cu - STRATA_DEQUANT_SIDE: Gemm::native with the dense-weight dequant on a side
2// stream into two scratches (Gemm::set_dequant_side) against the serial form, bitwise (GPU, synthetic, no model).
3//
4// A run of dense projections (Q4_K and Q6_K weights of the engine's shapes, one of them larger than the scratch so it takes
5// the sliced path, which keeps the serial form) is executed twice from the same inputs: once serially, once with the side
6// stream.  Every output must be identical bit for bit.  Also a run with a long queue of compute work ahead of each GEMM, so
7// the side dequant really overlaps it.  Exit 77 without a CUDA device.
8#include "strata/prefill/gemm.hpp"
9
10#include <cuda_fp16.h>
11#include <cuda_runtime.h>
12
13#include <cstdint>
14#include <cstdio>
15#include <cstring>
16#include <random>
17#include <string>
18#include <vector>
19
20namespace {
21__global__ void fill_f16(__half* p, int64_t n, unsigned seed, float scale) {
22    const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x;
23    if (i >= n) return;
24    unsigned x = (unsigned) i * 2654435761u + seed;
25    x ^= x >> 16; x *= 2246822519u; x ^= x >> 13; x *= 3266489917u; x ^= x >> 16;
26    p[i] = __float2half(((float) (x & 0xffff) / 32768.0f - 1.0f) * scale);
27}
28// busy work on the compute stream ahead of a GEMM
29__global__ void spin(float* p, int64_t n, int iters) {
30    const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x;
31    if (i >= n) return;
32    float v = p[i];
33    for (int k = 0; k < iters; ++k) v = v * 1.0000001f + 1e-7f;
34    p[i] = v;
35}
36bool ok(cudaError_t e, const char* what) {
37    if (e != cudaSuccess) std::printf("FAIL: %s: %s\n", what, cudaGetErrorString(e));
38    return e == cudaSuccess;
39}
40uint16_t sane_half(std::mt19937& rng) {
41    const uint32_t r = rng();
42    return (uint16_t) (((r >> 31) << 15) | ((5u + (r >> 10) % 5u) << 10) | (r & 0x3ffu));
43}
44struct Weight { int type, bytes_per_block; int at[2]; int64_t N, K; void* dev = nullptr; };
45}  // namespace
46
47int main() {
48    setvbuf(stdout, nullptr, _IONBF, 0);
49    int n_dev = 0;
50    if (cudaGetDeviceCount(&n_dev) != cudaSuccess || n_dev < 1) { std::printf("no CUDA device (skipped)\n"); return 77; }
51    cudaStream_t st = nullptr;
52    if (!ok(cudaStreamCreate(&st), "stream")) return 1;
53    const int64_t T = 512;
54    const int64_t scratch_elems = 2560LL * 4096;   // 6144 x 2560 does not fit: the sliced path
55    strata::prefill::Gemm gemm;
56    std::string err;
57    if (!gemm.init(st, scratch_elems, err)) { std::printf("FAIL: %s\n", err.c_str()); return 1; }
58    uint16_t* scratch2 = nullptr;
59    if (!ok(cudaMalloc((void**) &scratch2, (size_t) scratch_elems * 2), "scratch2")) return 1;
60
61    std::vector<Weight> ws = {
62        {12, 144, {0, 2}, 10240, 2560}, {14, 210, {208, -1}, 2560, 6144}, {12, 144, {0, 2}, 6144, 2560},
63        {14, 210, {208, -1}, 3072, 2560}, {12, 144, {0, 2}, 2560, 4096}, {13, 176, {0, 2}, 2560, 2560},
64        {12, 144, {0, 2}, 512, 2560},    {14, 210, {208, -1}, 12288, 2560},
65    };
66    std::mt19937 rng(20261026u);
67    for (Weight& w : ws) {
68        const size_t blocks = (size_t) (w.N * w.K / 256), bytes = blocks * (size_t) w.bytes_per_block;
69        std::vector<uint8_t> h(bytes);
70        for (auto& b : h) b = (uint8_t) (rng() & 0xff);
71        for (size_t k = 0; k < blocks; ++k)
72            for (int at : w.at)
73                if (at >= 0) { const uint16_t v = sane_half(rng); std::memcpy(&h[k * (size_t) w.bytes_per_block + (size_t) at], &v, 2); }
74        if (!ok(cudaMalloc(&w.dev, bytes), "W") || !ok(cudaMemcpy(w.dev, h.data(), bytes, cudaMemcpyHostToDevice), "W copy")) return 1;
75    }
76    int64_t max_k = 0, max_n = 0;
77    for (const Weight& w : ws) { max_k = std::max(max_k, w.K); max_n = std::max(max_n, w.N); }
78    uint16_t* X = nullptr;
79    float *busy = nullptr, *Y[2] = {nullptr, nullptr};
80    const int64_t busy_n = 1 << 22;
81    if (!ok(cudaMalloc((void**) &X, (size_t) T * max_k * 2), "X") || !ok(cudaMalloc((void**) &busy, (size_t) busy_n * 4), "busy") ||
82        !ok(cudaMalloc((void**) &Y[0], (size_t) T * max_n * 4 * ws.size()), "Y0") ||
83        !ok(cudaMalloc((void**) &Y[1], (size_t) T * max_n * 4 * ws.size()), "Y1"))
84        return 1;
85    fill_f16<<<(unsigned) ((T * max_k + 255) / 256), 256, 0, st>>>((__half*) X, T * max_k, 17u, 2.0f);
86    cudaMemset(busy, 0, (size_t) busy_n * 4);
87    if (!ok(cudaStreamSynchronize(st), "fill")) return 1;
88
89    int bad = 0;
90    for (int busy_iters : {0, 20000}) {
91        for (int side = 0; side < 2; ++side) {
92            std::string e2;
93            if (!gemm.set_dequant_side(side ? scratch2 : nullptr, e2)) { std::printf("FAIL: set_dequant_side: %s\n", e2.c_str()); return 1; }
94            cudaMemset(Y[side], 0xff, (size_t) T * max_n * 4 * ws.size());
95            for (int round = 0; round < 3; ++round)   // the scratch parity of the second round differs from the first
96                for (size_t i = 0; i < ws.size(); ++i) {
97                    if (busy_iters) spin<<<(unsigned) ((busy_n + 255) / 256), 256, 0, st>>>(busy, busy_n, busy_iters);
98                    gemm.native(X, ws[i].type, ws[i].dev, Y[side] + (size_t) i * T * max_n, T, ws[i].N, ws[i].K, max_n, 0.0f);
99                }
100            if (!ok(cudaStreamSynchronize(st), "run") || !ok(cudaDeviceSynchronize(), "device")) return 1;
101        }
102        std::vector<uint8_t> a((size_t) T * max_n * 4 * ws.size()), b(a.size());
103        cudaMemcpy(a.data(), Y[0], a.size(), cudaMemcpyDeviceToHost);
104        cudaMemcpy(b.data(), Y[1], b.size(), cudaMemcpyDeviceToHost);
105        for (size_t i = 0; i < ws.size(); ++i) {
106            // the written part of the output: T rows of N floats at stride max_n
107            long long diff = 0;
108            for (int64_t r = 0; r < T; ++r)
109                if (std::memcmp(&a[((size_t) i * T * max_n + (size_t) r * max_n) * 4], &b[((size_t) i * T * max_n + (size_t) r * max_n) * 4],
110                                (size_t) ws[i].N * 4) != 0)
111                    ++diff;
112            const bool sliced = ws[i].N * ws[i].K > scratch_elems;
113            std::printf("  busy %-6d type %2d N %5lld K %4lld%s: %s\n", busy_iters, ws[i].type, (long long) ws[i].N, (long long) ws[i].K,
114                        sliced ? " (sliced)" : "", diff ? "*** DIFFERS ***" : "bitwise equal");
115            if (diff) ++bad;
116        }
117    }
118    gemm.set_dequant_side(nullptr, err);
119    std::printf("gemm_dequant_side_parity: %s\n", bad ? "FAILED" : "OK");
120    return bad ? 1 : 0;
121}