okami.mom/strata

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

Niko1221Merge f-q4-sycl (41717f29c): the Intel Arc (SYCL) engine follows the compiled geometry, so q4_lite/q4_mid/q4_big build; default build unchanged (CUDA SASS 3573/3573, HIP gfx1201 3920/3920, gfx1151 string offsets only, Metal 85/85 objects, SYCL 22/22 checksums); A750 gate 2 q4_lite 0.00021 vs floor 0.00017; one conflict with f-clean-6-0 in sycl/src/prefill/prefill.cpp resolved as tested by Q4-SYMERGE (Qwen4 prep)db137d917

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