okami.mom/strata

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

Niko1221Trial2 merge f-0291-pr-nv (97b3d614f)1306424be

main
4.7 KiB114 linesraw
1// tests/cuda/hc_q8_stream_test.cu - `hc_q8_to_bf16` expands on the stream it is given.  The layer-major prompt walk
2// called `bf16_proj` for its hyper-connection projections without a stream, so under STRATA_HC_REQ8 the int8 expansion
3// ran on the default stream while the GEMMs read it on another.  This pins the property that failed; it does not cover
4// the call sites themselves (`bf16_proj` is file-static, so no test reaches it).
5//
6// Exit 0 pass, 1 fail, 77 no CUDA device (ctest SKIP_RETURN_CODE).
7#include "strata/kernels/fused_gr.hpp"
8
9#include <cuda_runtime.h>
10
11#include <cstdint>
12#include <cstdio>
13#include <cstring>
14#include <cstdlib>
15#include <random>
16#include <vector>
17
18namespace {
19
20int fails = 0;
21
22bool ck(cudaError_t e, const char* what) {
23    if (e != cudaSuccess) std::printf("FAIL: %s: %s\n", what, cudaGetErrorString(e));
24    return e == cudaSuccess;
25}
26
27// the kernel's own formula, on the host: bf16 round-to-nearest-even of (scale * code)
28std::vector<uint16_t> reference(const std::vector<int8_t>& q, const std::vector<float>& sc) {
29    std::vector<uint16_t> out(q.size());
30    for (size_t i = 0; i < q.size(); ++i) {
31        const float v = sc[i / 32] * (float) q[i];
32        uint32_t b;
33        std::memcpy(&b, &v, 4);
34        b += 0x7fffu + ((b >> 16) & 1u);
35        out[i] = (uint16_t) (b >> 16);
36    }
37    return out;
38}
39
40// keeps its stream busy for a few ms: bounded on purpose, so the test cannot hang on a host/device ordering mistake
41// (a host copy that releases a spinning kernel would itself wait for that stream)
42__global__ void spin_kernel() {
43#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 700
44    for (int i = 0; i < 20000; ++i) __nanosleep(1000);
45#else
46    // sm_60/61 have no __nanosleep: spin on the clock for about the same few ms
47    const long long t0 = clock64();
48    while (clock64() - t0 < 20000000LL) {}
49#endif
50}
51
52// one case: enqueue the expansion on `A`, optionally behind a spin that holds `A` busy, and read it back ordered only by
53// an event on `A` (never by a device-wide synchronize, which would hide exactly the defect this guards)
54void check(bool with_spin, int n_blocks, uint32_t seed) {
55    const long long n = (long long) n_blocks * 32;
56    std::mt19937 g(seed);
57    std::uniform_int_distribution<int> code(-127, 127);
58    std::normal_distribution<float> scale(0.0f, 0.02f);
59    std::vector<int8_t> q((size_t) n);
60    std::vector<float> sc((size_t) n_blocks);
61    for (auto& v : q) v = (int8_t) code(g);
62    for (auto& v : sc) v = scale(g);
63    const std::vector<uint16_t> want = reference(q, sc);
64
65    int8_t* d_q = nullptr;
66    float* d_sc = nullptr;
67    uint16_t* d_out = nullptr;
68    cudaStream_t a = nullptr;
69    cudaEvent_t ev = nullptr;
70    bool ok = ck(cudaStreamCreate(&a), "stream") && ck(cudaEventCreateWithFlags(&ev, cudaEventDisableTiming), "event") &&
71              ck(cudaMalloc(&d_q, (size_t) n), "alloc q") && ck(cudaMalloc(&d_sc, (size_t) sc.size() * 4), "alloc sc") &&
72              ck(cudaMalloc(&d_out, (size_t) n * 2), "alloc out");
73    if (ok) {
74        ck(cudaMemcpy(d_q, q.data(), (size_t) n, cudaMemcpyHostToDevice), "upload q");
75        ck(cudaMemcpy(d_sc, sc.data(), sc.size() * 4, cudaMemcpyHostToDevice), "upload sc");
76        ck(cudaMemset(d_out, 0xff, (size_t) n * 2), "poison out");
77        if (with_spin) spin_kernel<<<1, 1, 0, a>>>();
78        strata::kernels::hc_q8_to_bf16(d_q, d_sc, d_out, n, a);
79        // the readback is ordered by the expansion's stream and by nothing else
80        ck(cudaEventRecord(ev, a), "record");
81        ck(cudaStreamWaitEvent(nullptr, ev, 0), "wait on the default stream");
82        std::vector<uint16_t> got((size_t) n);
83        ck(cudaMemcpy(got.data(), d_out, (size_t) n * 2, cudaMemcpyDeviceToHost), "download");
84        size_t bad = 0;
85        for (size_t i = 0; i < got.size(); ++i) bad += got[i] != want[i];
86        const bool pass = bad == 0;
87        std::printf("hc_q8_to_bf16 %s, %d blocks, stream A: %s (%zu of %zu differ)\n",
88                    with_spin ? "behind a busy stream" : "plain", n_blocks, pass ? "ok" : "FAIL", bad, got.size());
89        fails += pass ? 0 : 1;
90    } else {
91        ++fails;
92    }
93    if (a) cudaStreamSynchronize(a);
94    if (ev) cudaEventDestroy(ev);
95    if (a) cudaStreamDestroy(a);
96    if (d_q) cudaFree(d_q);
97    if (d_sc) cudaFree(d_sc);
98    if (d_out) cudaFree(d_out);
99}
100
101}  // namespace
102
103int main() {
104    int devs = 0;
105    if (cudaGetDeviceCount(&devs) != cudaSuccess || devs < 1) {
106        std::printf("no CUDA device\n");
107        return 77;
108    }
109    check(false, 320, 1);       // the HC down projection's shape: 320 rows of 10240
110    check(true, 320, 2);        // the same, with work already queued on that stream
111    check(true, 4096, 3);       // a bigger matrix, so the launch shape changes
112    std::printf(fails == 0 ? "PASS\n" : "FAILED\n");
113    return fails == 0 ? 0 : 1;
114}