okami.mom/strata

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

Niko1221Experimental speed projection: an optional control vector (EXPERIMENTAL, off by default)6abee7f39

main
9.4 KiB223 linesraw
1// src/kernels/cvec_parity.cpp - the control vector kernel (strata/kernels/cvec.hpp) against a host reference.
2//   1. project: h - s (h.v) v per stream and token, against double precision; the steered component is gone.
3//   2. add: h + d.
4//   3. switched off: R unchanged without a pending write; with one, R is BITWISE the write the fused read folds
5//      (fused_gr_read with apply), which is what keeps a loaded-but-off vector identical to the stock engine.
6//   4. a layer without a direction is untouched.
7
8#include "strata/kernels/cvec.hpp"
9#include "strata/kernels/fused_gr.hpp"
10
11#include <cuda_runtime.h>
12
13#include <cmath>
14#include <cstdio>
15#include <cstdlib>
16#include <cstring>
17#include <random>
18#include <vector>
19
20namespace k = strata::kernels;
21
22namespace {
23
24int g_fail = 0;
25
26void ck(cudaError_t e, const char* w) {
27    if (e != cudaSuccess) {
28        std::fprintf(stderr, "CUDA error in %s: %s\n", w, cudaGetErrorString(e));
29        std::exit(2);
30    }
31}
32
33template <typename T>
34T* dalloc(size_t n) {
35    T* p = nullptr;
36    ck(cudaMalloc(&p, n * sizeof(T)), "malloc");
37    return p;
38}
39
40template <typename T>
41void up(T* d, const std::vector<T>& h) { ck(cudaMemcpy(d, h.data(), h.size() * sizeof(T), cudaMemcpyHostToDevice), "up"); }
42template <typename T>
43std::vector<T> down(const T* d, size_t n) {
44    std::vector<T> h(n);
45    ck(cudaMemcpy(h.data(), d, n * sizeof(T), cudaMemcpyDeviceToHost), "down");
46    return h;
47}
48
49void check(bool ok, const char* what) {
50    std::printf("  %-66s %s\n", what, ok ? "ok" : "FAIL");
51    if (!ok) ++g_fail;
52}
53
54}  // namespace
55
56int main() {
57    constexpr int64_t L = 48, N = 2560, HC = 4, LR = 320, T = 5, D = HC * N;
58    constexpr int64_t kLayer = 7, kOff = 3;   // steered / not steered
59    std::mt19937 rng(1234);
60    std::normal_distribution<float> nd(0.0f, 1.0f);
61
62    // a unit direction at kLayer with a norm-2 scaled one's s = 2; nothing at kOff
63    std::vector<float> dir((size_t) (L * N), 0.0f), s((size_t) L, 0.0f);
64    {
65        double nrm = 0.0;
66        std::vector<float> v((size_t) N);
67        for (auto& x : v) { x = nd(rng); nrm += (double) x * x; }
68        nrm = std::sqrt(nrm);
69        for (int64_t j = 0; j < N; ++j) dir[(size_t) (kLayer * N + j)] = (float) (v[(size_t) j] / nrm);
70        s[(size_t) kLayer] = 2.0f;
71    }
72    std::string err;
73    if (!k::cvec_upload(dir, s, /*project*/ 0, 4, 44, N, HC, err)) { std::fprintf(stderr, "%s\n", err.c_str()); return 2; }
74    check(k::cvec().covers(kLayer) && !k::cvec().covers(kOff) && !k::cvec().covers(45), "covers(): steered layers only");
75
76    std::vector<float> R((size_t) (T * D)), bo((size_t) (T * N)), inj((size_t) (T * HC));
77    for (auto& x : R) x = nd(rng) * 3.0f;
78    for (auto& x : bo) x = nd(rng);
79    for (auto& x : inj) x = nd(rng);
80    float* dR = dalloc<float>(R.size());
81    float* dbo = dalloc<float>(bo.size());
82    float* dinj = dalloc<float>(inj.size());
83    up(dbo, bo);
84    up(dinj, inj);
85
86    // ---- 1. project, no pending write
87    std::printf("project\n");
88    up(dR, R);
89    k::cvec_apply(dR, kLayer, T, D, nullptr, 0, nullptr, 0, false, nullptr);
90    ck(cudaDeviceSynchronize(), "project");
91    {
92        const auto got = down(dR, R.size());
93        double worst = 0.0, worst_dot = 0.0;
94        for (int64_t t = 0; t < T; ++t)
95            for (int64_t c = 0; c < HC; ++c) {
96                const float* h = R.data() + t * D + c * N;
97                const float* v = dir.data() + kLayer * N;
98                double dot = 0.0;
99                for (int64_t j = 0; j < N; ++j) dot += (double) h[j] * v[j];
100                double after = 0.0;
101                for (int64_t j = 0; j < N; ++j) {
102                    const double want = h[j] - 2.0 * dot * v[j];
103                    const double g = got[(size_t) (t * D + c * N + j)];
104                    worst = std::fmax(worst, std::fabs(g - want));
105                    after += g * v[j];
106                }
107                // s = 2 reflects the component: h.v -> -h.v
108                worst_dot = std::fmax(worst_dot, std::fabs(after + dot));
109            }
110        std::printf("    max |err| %.3g, max |h'.v + h.v| %.3g\n", worst, worst_dot);
111        check(worst < 1e-4, "project matches h - s (h.v) v (s = 2)");
112        check(worst_dot < 1e-3, "the component along v is reflected (s = 2)");
113    }
114    // s = 1 removes it
115    s[(size_t) kLayer] = 1.0f;
116    if (!k::cvec_upload(dir, s, 0, 4, 44, N, HC, err)) return 2;
117    up(dR, R);
118    k::cvec_apply(dR, kLayer, T, D, nullptr, 0, nullptr, 0, false, nullptr);
119    ck(cudaDeviceSynchronize(), "project s=1");
120    {
121        const auto got = down(dR, R.size());
122        double worst = 0.0;
123        for (int64_t t = 0; t < T; ++t)
124            for (int64_t c = 0; c < HC; ++c) {
125                double after = 0.0;
126                for (int64_t j = 0; j < N; ++j) after += (double) got[(size_t) (t * D + c * N + j)] * dir[(size_t) (kLayer * N + j)];
127                worst = std::fmax(worst, std::fabs(after));
128            }
129        std::printf("    max |h'.v| %.3g\n", worst);
130        check(worst < 1e-3, "s = 1 projects the direction out");
131    }
132
133    // ---- 4. a layer without a direction
134    up(dR, R);
135    k::cvec_apply(dR, kOff, T, D, nullptr, 0, nullptr, 0, false, nullptr);
136    ck(cudaDeviceSynchronize(), "off layer");
137    check(std::memcmp(down(dR, R.size()).data(), R.data(), R.size() * 4) == 0, "a layer without a direction is untouched");
138
139    // ---- 3. switched off
140    std::printf("switched off\n");
141    k::cvec_set_enabled(false);
142    check(!k::cvec_enabled(), "cvec_enabled() follows the switch");
143    up(dR, R);
144    k::cvec_apply(dR, kLayer, T, D, nullptr, 0, nullptr, 0, false, nullptr);
145    ck(cudaDeviceSynchronize(), "off");
146    check(std::memcmp(down(dR, R.size()).data(), R.data(), R.size() * 4) == 0, "off, no pending write: R unchanged");
147    {
148        // the fused read's folded write, one token at a time, against the kernel's write-only pass
149        std::vector<uint16_t> wd((size_t) (LR * D)), wu((size_t) (D * LR)), wi((size_t) (HC * D));
150        std::vector<float> wn((size_t) D);
151        auto bf = [&](float x) { uint32_t u; std::memcpy(&u, &x, 4); return (uint16_t) (u >> 16); };
152        for (auto& x : wd) x = bf(nd(rng) * 0.02f);
153        for (auto& x : wu) x = bf(nd(rng) * 0.02f);
154        for (auto& x : wi) x = bf(nd(rng) * 0.02f);
155        for (auto& x : wn) x = 1.0f + 0.1f * nd(rng);
156        uint16_t* dwd = dalloc<uint16_t>(wd.size());
157        uint16_t* dwu = dalloc<uint16_t>(wu.size());
158        uint16_t* dwi = dalloc<uint16_t>(wi.size());
159        float* dwn = dalloc<float>(wn.size());
160        up(dwd, wd); up(dwu, wu); up(dwi, wi); up(dwn, wn);
161        float* lo = dalloc<float>(LR);
162        float* rs = dalloc<float>(HC);
163        float* inj_out = dalloc<float>(HC);
164        float* mixed = dalloc<float>(N);
165        float* dF = dalloc<float>(R.size());
166        up(dF, R);
167        for (int64_t t = 0; t < T; ++t) {
168            k::FusedGrArgs a;
169            a.R = dF + t * D; a.R_out = dF + t * D; a.apply = true;
170            a.bo_prev = dbo + t * N; a.inj_prev = dinj + t * HC;
171            a.w_norm = dwn; a.w_down = dwd; a.w_up = dwu; a.w_inject = dwi;
172            a.lo = lo; a.rs = rs; a.inject_out = inj_out; a.mixed = mixed;
173            k::fused_gr_read(a, nullptr);
174        }
175        up(dR, R);
176        k::cvec_apply(dR, kLayer, T, D, dbo, N, dinj, HC, true, nullptr);
177        ck(cudaDeviceSynchronize(), "write only");
178        const auto fused = down(dF, R.size()), mine = down(dR, R.size());
179        check(std::memcmp(fused.data(), mine.data(), R.size() * 4) == 0,
180              "off, pending write: bitwise the fused read's folded write");
181        k::cvec_set_enabled(true);
182        // on, with the write: the write, then the projection (reference from the fused result)
183        up(dR, R);
184        k::cvec_apply(dR, kLayer, T, D, dbo, N, dinj, HC, true, nullptr);
185        ck(cudaDeviceSynchronize(), "write+project");
186        const auto both = down(dR, R.size());
187        double worst = 0.0;
188        for (int64_t t = 0; t < T; ++t)
189            for (int64_t c = 0; c < HC; ++c) {
190                const float* h = fused.data() + t * D + c * N;
191                const float* v = dir.data() + kLayer * N;
192                double dot = 0.0;
193                for (int64_t j = 0; j < N; ++j) dot += (double) h[j] * v[j];
194                for (int64_t j = 0; j < N; ++j)
195                    worst = std::fmax(worst, std::fabs(both[(size_t) (t * D + c * N + j)] - (h[j] - dot * v[j])));
196            }
197        std::printf("    write + project: max |err| %.3g\n", worst);
198        check(worst < 1e-4, "on, pending write: the write, then the projection");
199    }
200
201    // ---- 2. add
202    std::printf("add\n");
203    for (int64_t j = 0; j < N; ++j) dir[(size_t) (kLayer * N + j)] *= 0.1f;   // d = 0.1 v, s = 1
204    s[(size_t) kLayer] = 1.0f;
205    if (!k::cvec_upload(dir, s, /*add*/ 1, 4, 44, N, HC, err)) return 2;
206    up(dR, R);
207    k::cvec_apply(dR, kLayer, T, D, nullptr, 0, nullptr, 0, false, nullptr);
208    ck(cudaDeviceSynchronize(), "add");
209    {
210        const auto got = down(dR, R.size());
211        bool same = true;
212        for (int64_t t = 0; t < T; ++t)
213            for (int64_t c = 0; c < HC; ++c)
214                for (int64_t j = 0; j < N; ++j) {
215                    const size_t i = (size_t) (t * D + c * N + j);
216                    same = same && got[i] == R[i] + dir[(size_t) (kLayer * N + j)];
217                }
218        check(same, "add is h + d in every stream (bitwise)");
219    }
220
221    std::printf(g_fail ? "cvec_parity: %d FAILED\n" : "cvec_parity: all passed\n", g_fail);
222    return g_fail ? 1 : 0;
223}