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
8.1 KiB178 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#define DPCT_PROFILING_ENABLED
8#include <sycl/sycl.hpp>
9#include <dpct/dpct.hpp>
10#include "strata/kernels/gr.hpp"
11#include "strata/core/geometry.hpp"
12
13#include <cstdint>
14#include <cstdio>
15#include <cstdlib>
16#include <cstring>
17#include <random>
18#include <vector>
19
20namespace k = strata::kernels;
21
22namespace {
23void ck(dpct::err0 e, const char *w) {
24}
25template <typename T>
26T* up(const std::vector<T>& h) {
27    void* p = nullptr;
28    ck(DPCT_CHECK_ERROR(
29           p = (void *)sycl::malloc_device(h.size() * sizeof(T) + 64,
30                                           dpct::get_in_order_queue())),
31       "malloc");
32    /*
33    DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming in
34    the original code the source host memory is pageable memory. If the memory
35    is not pageable, call wait() on event return by memcpy API to ensure
36    synchronization behavior.
37    */
38    ck(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue()).memcpy(
39           p, h.data(), h.size() * sizeof(T)).wait()),
40       "h2d");
41    return (T*) p;
42}
43template <typename T>
44T* dev(size_t n) {
45    void* p = nullptr;
46    ck(DPCT_CHECK_ERROR(p = (void *)sycl::malloc_device(
47                            n * sizeof(T) + 64, dpct::get_in_order_queue())),
48       "malloc");
49    ck(DPCT_CHECK_ERROR(
50           (dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue()).memset(p, 0, n * sizeof(T) + 64).wait()),
51       "memset");
52    return (T*) p;
53}
54template <typename T>
55std::vector<T> down(const T* d, size_t n) {
56    std::vector<T> h(n);
57    ck(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue())
58                            .memcpy(h.data(), d, n * sizeof(T))
59                            .wait()),
60       "d2h");
61    return h;
62}
63uint16_t bf16_of(float f) {
64    uint32_t u;
65    std::memcpy(&u, &f, 4);
66    return (uint16_t) (u >> 16);
67}
68}  // namespace
69
70int main() {
71    const long long n_embd = strata::core::kCompiledGeometry.n_embd, hc = strata::core::kCompiledGeometry.hc, hc_lr = strata::core::kCompiledGeometry.hc_lr, hc_dim = hc * n_embd;   // the compiled shape (Q4-SYCL)
72    const float eps = 1e-6f;
73    std::mt19937 rng(783);
74    std::normal_distribution<float> gauss(0.f, 1.f);
75    k::gr_set_native_mmvf(true);
76
77    std::vector<float> w_norm((size_t) hc_dim);
78    for (auto& x : w_norm) x = 1.0f + 0.1f * gauss(rng);
79    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));
80    for (auto& x : w_down) x = bf16_of(gauss(rng) * 0.05f);
81    for (auto& x : w_up) x = bf16_of(gauss(rng) * 0.05f);
82    for (auto& x : w_inj) x = bf16_of(gauss(rng) * 0.02f);
83    float* d_norm = up(w_norm);
84    uint16_t *d_down = up(w_down), *d_up = up(w_up), *d_inj = up(w_inj);
85
86    const k::GrShapes sh{n_embd, hc, hc_lr};
87    void* ws_raw = nullptr;
88    ck(DPCT_CHECK_ERROR(
89           ws_raw = (void *)sycl::malloc_device(k::gr_workspace_bytes(sh),
90                                                dpct::get_in_order_queue())),
91       "ws");
92    k::GrWorkspace ws;
93    k::gr_workspace_init(sh, ws_raw, ws);
94
95    int cases = 0, bad = 0;
96    for (int T = 1; T <= 8; ++T) {
97        std::vector<float> R((size_t) T * hc_dim);
98        for (int t = 0; t < T; ++t)
99            for (long long c = 0; c < hc; ++c)
100                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));
101        float* d_R = up(R);
102        float* d_xn = dev<float>((size_t) T * hc_dim);
103        float* d_lo = dev<float>((size_t) T * hc_lr);
104        float* d_g = dev<float>((size_t) T * hc_dim);
105        for (int with_inj = 0; with_inj < 2; ++with_inj) {
106            float* d_mixed_m = dev<float>((size_t) T * n_embd);
107            float* d_mixed_s = dev<float>((size_t) T * n_embd);
108            float* d_inject_m = dev<float>((size_t) T * hc);
109            float* d_inject_s = dev<float>((size_t) T * hc);
110            const uint16_t* wi = with_inj ? d_inj : nullptr;
111            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,
112                             with_inj ? d_inject_m : nullptr, T, nullptr);
113            for (int t = 0; t < T; ++t)
114                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,
115                           with_inj ? d_inject_s + (size_t) t * hc : nullptr, nullptr);
116            ck(DPCT_CHECK_ERROR(
117                   dpct::get_current_device().queues_wait_and_throw()),
118               "sync");
119            bool ok = std::memcmp(down(d_mixed_m, (size_t) T * n_embd).data(), down(d_mixed_s, (size_t) T * n_embd).data(),
120                                  (size_t) T * n_embd * 4) == 0;
121            if (with_inj)
122                ok = ok && std::memcmp(down(d_inject_m, (size_t) T * hc).data(), down(d_inject_s, (size_t) T * hc).data(),
123                                       (size_t) T * hc * 4) == 0;
124            ++cases;
125            if (!ok) {
126                std::printf("    *** gr_read_multi T=%d %s differs from gr_read ***\n", T, with_inj ? "with injection" : "final mixer");
127                ++bad;
128            }
129            sycl::free(d_mixed_m, dpct::get_in_order_queue());
130                sycl::free(d_mixed_s, dpct::get_in_order_queue());
131                sycl::free(d_inject_m, dpct::get_in_order_queue());
132                sycl::free(d_inject_s, dpct::get_in_order_queue());
133        }
134
135        // the write: R_out[t] = R[t] + block_out[t] * 2 sigmoid(inject[t] / hc)
136        std::vector<float> bo((size_t) T * n_embd), inj((size_t) T * hc);
137        for (auto& x : bo) x = gauss(rng);
138        for (auto& x : inj) x = gauss(rng) * 3.0f;
139        float* d_bo = up(bo);
140        float* d_in = up(inj);
141        for (int inplace = 0; inplace < 2; ++inplace) {
142            float* d_Rm = up(R);
143            float* d_Rs = up(R);
144            float* d_om = inplace ? d_Rm : dev<float>((size_t) T * hc_dim);
145            float* d_os = inplace ? d_Rs : dev<float>((size_t) T * hc_dim);
146            k::gr_write_multi(d_Rm, d_bo, d_in, sh, d_om, T, nullptr);
147            for (int t = 0; t < T; ++t)
148                k::gr_write(d_Rs + (size_t) t * hc_dim, d_bo + (size_t) t * n_embd, d_in + (size_t) t * hc, sh,
149                            d_os + (size_t) t * hc_dim, nullptr);
150            ck(DPCT_CHECK_ERROR(
151                   dpct::get_current_device().queues_wait_and_throw()),
152               "sync");
153            const bool ok = std::memcmp(down(d_om, (size_t) T * hc_dim).data(), down(d_os, (size_t) T * hc_dim).data(),
154                                        (size_t) T * hc_dim * 4) == 0;
155            ++cases;
156            if (!ok) {
157                std::printf("    *** gr_write_multi T=%d %s differs from gr_write ***\n", T, inplace ? "in place" : "out of place");
158                ++bad;
159            }
160            sycl::free(d_Rm, dpct::get_in_order_queue());
161                sycl::free(d_Rs, dpct::get_in_order_queue());
162            if (!inplace) {
163                sycl::free(d_om, dpct::get_in_order_queue());
164                sycl::free(d_os, dpct::get_in_order_queue());
165            }
166        }
167        sycl::free(d_R, dpct::get_in_order_queue());
168            sycl::free(d_xn, dpct::get_in_order_queue());
169            sycl::free(d_lo, dpct::get_in_order_queue());
170            sycl::free(d_g, dpct::get_in_order_queue());
171            sycl::free(d_bo, dpct::get_in_order_queue());
172            sycl::free(d_in, dpct::get_in_order_queue());
173    }
174    std::printf("gr_multi: %d cases, %d failures\n", cases, bad);
175    if (bad) return 1;
176    std::printf("gr_multi_parity OK\n");
177    return 0;
178}