okami.mom/strata

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

Niko1221perf(gr): multi-token hyper-connection read/write, exact-T GR and BF16 projections (#783 PR-g)104a8485e

main
6.3 KiB143 linesraw
1// src/kernels/gr_multi_parity.cpp - the multi-token hyper-connection read and write (#783 PR-g: gr_read_multi,
2// gr_write_multi and the exact-T BF16 projections under them) against the single-token calls, memcmp.
3//
4// Real geometry (n_embd 2560, hc 4, hc_lr 320), the pinned native MMVF path, every T = 1..8, with and without an
5// injection weight (a layer read vs the final mixer), and the write both out of place and in place (R_out == R).
6// Each output of the multi call must equal the token-by-token one bit for bit. GPU, synthetic, no model.
7#include "strata/kernels/gr.hpp"
8
9#include <cuda_runtime.h>
10
11#include <cstdint>
12#include <cstdio>
13#include <cstdlib>
14#include <cstring>
15#include <random>
16#include <vector>
17
18namespace k = strata::kernels;
19
20namespace {
21void ck(cudaError_t e, const char* w) {
22    if (e != cudaSuccess) {
23        std::fprintf(stderr, "%s: %s\n", w, cudaGetErrorString(e));
24        std::exit(2);
25    }
26}
27template <typename T>
28T* up(const std::vector<T>& h) {
29    void* p = nullptr;
30    ck(cudaMalloc(&p, h.size() * sizeof(T) + 64), "malloc");
31    ck(cudaMemcpy(p, h.data(), h.size() * sizeof(T), cudaMemcpyHostToDevice), "h2d");
32    return (T*) p;
33}
34template <typename T>
35T* dev(size_t n) {
36    void* p = nullptr;
37    ck(cudaMalloc(&p, n * sizeof(T) + 64), "malloc");
38    ck(cudaMemset(p, 0, n * sizeof(T) + 64), "memset");
39    return (T*) p;
40}
41template <typename T>
42std::vector<T> down(const T* d, size_t n) {
43    std::vector<T> h(n);
44    ck(cudaMemcpy(h.data(), d, n * sizeof(T), cudaMemcpyDeviceToHost), "d2h");
45    return h;
46}
47uint16_t bf16_of(float f) {
48    uint32_t u;
49    std::memcpy(&u, &f, 4);
50    return (uint16_t) (u >> 16);
51}
52}  // namespace
53
54int main() {
55    const long long n_embd = 2560, hc = 4, hc_lr = 320, hc_dim = hc * n_embd;
56    const float eps = 1e-6f;
57    std::mt19937 rng(783);
58    std::normal_distribution<float> gauss(0.f, 1.f);
59    k::gr_set_native_mmvf(true);
60
61    std::vector<float> w_norm((size_t) hc_dim);
62    for (auto& x : w_norm) x = 1.0f + 0.1f * gauss(rng);
63    std::vector<uint16_t> w_down((size_t) (hc_lr * hc_dim)), w_up((size_t) (hc_dim * hc_lr)), w_inj((size_t) (hc * hc_dim));
64    for (auto& x : w_down) x = bf16_of(gauss(rng) * 0.05f);
65    for (auto& x : w_up) x = bf16_of(gauss(rng) * 0.05f);
66    for (auto& x : w_inj) x = bf16_of(gauss(rng) * 0.02f);
67    float* d_norm = up(w_norm);
68    uint16_t *d_down = up(w_down), *d_up = up(w_up), *d_inj = up(w_inj);
69
70    const k::GrShapes sh{n_embd, hc, hc_lr};
71    void* ws_raw = nullptr;
72    ck(cudaMalloc(&ws_raw, k::gr_workspace_bytes(sh)), "ws");
73    k::GrWorkspace ws;
74    k::gr_workspace_init(sh, ws_raw, ws);
75
76    int cases = 0, bad = 0;
77    for (int T = 1; T <= 8; ++T) {
78        std::vector<float> R((size_t) T * hc_dim);
79        for (int t = 0; t < T; ++t)
80            for (long long c = 0; c < hc; ++c)
81                for (long long d = 0; d < n_embd; ++d) R[(size_t) t * hc_dim + c * n_embd + d] = gauss(rng) * (float) (1 << (2 * c));
82        float* d_R = up(R);
83        float* d_xn = dev<float>((size_t) T * hc_dim);
84        float* d_lo = dev<float>((size_t) T * hc_lr);
85        float* d_g = dev<float>((size_t) T * hc_dim);
86        for (int with_inj = 0; with_inj < 2; ++with_inj) {
87            float* d_mixed_m = dev<float>((size_t) T * n_embd);
88            float* d_mixed_s = dev<float>((size_t) T * n_embd);
89            float* d_inject_m = dev<float>((size_t) T * hc);
90            float* d_inject_s = dev<float>((size_t) T * hc);
91            const uint16_t* wi = with_inj ? d_inj : nullptr;
92            k::gr_read_multi(d_R, d_norm, d_down, d_up, wi, eps, sh, ws, d_xn, d_lo, d_g, d_mixed_m,
93                             with_inj ? d_inject_m : nullptr, T, nullptr);
94            for (int t = 0; t < T; ++t)
95                k::gr_read(d_R + (size_t) t * hc_dim, d_norm, d_down, d_up, wi, eps, sh, ws, d_mixed_s + (size_t) t * n_embd,
96                           with_inj ? d_inject_s + (size_t) t * hc : nullptr, nullptr);
97            ck(cudaDeviceSynchronize(), "sync");
98            bool ok = std::memcmp(down(d_mixed_m, (size_t) T * n_embd).data(), down(d_mixed_s, (size_t) T * n_embd).data(),
99                                  (size_t) T * n_embd * 4) == 0;
100            if (with_inj)
101                ok = ok && std::memcmp(down(d_inject_m, (size_t) T * hc).data(), down(d_inject_s, (size_t) T * hc).data(),
102                                       (size_t) T * hc * 4) == 0;
103            ++cases;
104            if (!ok) {
105                std::printf("    *** gr_read_multi T=%d %s differs from gr_read ***\n", T, with_inj ? "with injection" : "final mixer");
106                ++bad;
107            }
108            cudaFree(d_mixed_m); cudaFree(d_mixed_s); cudaFree(d_inject_m); cudaFree(d_inject_s);
109        }
110
111        // the write: R_out[t] = R[t] + block_out[t] * 2 sigmoid(inject[t] / hc)
112        std::vector<float> bo((size_t) T * n_embd), inj((size_t) T * hc);
113        for (auto& x : bo) x = gauss(rng);
114        for (auto& x : inj) x = gauss(rng) * 3.0f;
115        float* d_bo = up(bo);
116        float* d_in = up(inj);
117        for (int inplace = 0; inplace < 2; ++inplace) {
118            float* d_Rm = up(R);
119            float* d_Rs = up(R);
120            float* d_om = inplace ? d_Rm : dev<float>((size_t) T * hc_dim);
121            float* d_os = inplace ? d_Rs : dev<float>((size_t) T * hc_dim);
122            k::gr_write_multi(d_Rm, d_bo, d_in, sh, d_om, T, nullptr);
123            for (int t = 0; t < T; ++t)
124                k::gr_write(d_Rs + (size_t) t * hc_dim, d_bo + (size_t) t * n_embd, d_in + (size_t) t * hc, sh,
125                            d_os + (size_t) t * hc_dim, nullptr);
126            ck(cudaDeviceSynchronize(), "sync");
127            const bool ok = std::memcmp(down(d_om, (size_t) T * hc_dim).data(), down(d_os, (size_t) T * hc_dim).data(),
128                                        (size_t) T * hc_dim * 4) == 0;
129            ++cases;
130            if (!ok) {
131                std::printf("    *** gr_write_multi T=%d %s differs from gr_write ***\n", T, inplace ? "in place" : "out of place");
132                ++bad;
133            }
134            cudaFree(d_Rm); cudaFree(d_Rs);
135            if (!inplace) { cudaFree(d_om); cudaFree(d_os); }
136        }
137        cudaFree(d_R); cudaFree(d_xn); cudaFree(d_lo); cudaFree(d_g); cudaFree(d_bo); cudaFree(d_in);
138    }
139    std::printf("gr_multi: %d cases, %d failures\n", cases, bad);
140    if (bad) return 1;
141    std::printf("gr_multi_parity OK\n");
142    return 0;
143}