okami.mom/strata

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

Niko1221Merge branch 'f-1402-y' into f-14025af104b51

main
9.3 KiB179 linesraw
1// src/kernels/kv_steps_parity.cpp - the batched K/V append (#783 PR-d): kv_append_q8_steps / kv_append_q4_steps for
2// n = 1..8 cells in one launch against n calls of the single-cell append, memcmp on every pool byte.
3//
4// Cells land at scattered positions through a non-identity page table; the K/V rows are random with small, large and
5// all-zero groups. Three layouts: INT8 K and V, Q4_0 K and V, and the K8V4 hybrid's folded call (the K pool passed as
6// both halves, one plane). GPU, synthetic, no model.
7#define DPCT_PROFILING_ENABLED
8#include <sycl/sycl.hpp>
9#include <dpct/dpct.hpp>
10#include "strata/kernels/kv_q4.hpp"
11#include "strata/kernels/kv_q8.hpp"
12#include "strata/kernels/qsa.hpp"
13
14#include <algorithm>
15#include <cstdio>
16#include <cstdlib>
17#include <cstring>
18#include <random>
19#include <vector>
20
21namespace k = strata::kernels;
22
23namespace {
24int g_fail = 0;
25void ck(dpct::err0 e, const char *w) {
26}
27template <typename T>
28T* dalloc(size_t n) {
29    T* p = nullptr;
30    ck(DPCT_CHECK_ERROR(p = (T *)sycl::malloc_device(
31                            n * sizeof(T) + 64, dpct::get_in_order_queue())),
32       "malloc");
33    ck(DPCT_CHECK_ERROR(
34           (dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue()).memset(p, 0, n * sizeof(T) + 64).wait()),
35       "memset");
36    return p;
37}
38template <typename T>
39std::vector<T> fetch(const T* d, size_t n) {
40    std::vector<T> h(n);
41    ck(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue())
42                            .memcpy(h.data(), d, n * sizeof(T))
43                            .wait()),
44       "d2h");
45    return h;
46}
47template <typename T>
48void zero(T *d, size_t n) {
49    ck(DPCT_CHECK_ERROR(
50           (dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue()).memset(d, 0, n * sizeof(T) + 64).wait()),
51       "zero");
52}
53bool same(const void* a, const void* b, size_t bytes) { return std::memcmp(a, b, bytes) == 0; }
54}  // namespace
55
56int main() {
57    k::QsaShapes s = k::qsa_real_shapes();
58    s.page_size = 64;
59    const int H = (int) s.n_head_kv, D = (int) s.head_dim, P = (int) s.page_size, G = D / k::KV_Q8_GROUP;
60    const int pages = 8, cells = pages * P;
61    std::mt19937 rng(783);
62    std::normal_distribution<float> nd(0.f, 1.f);
63    std::vector<int32_t> table(pages);
64    for (int i = 0; i < pages; ++i) table[i] = (i * 5 + 3) % pages;
65    int32_t* d_table = dalloc<int32_t>(pages);
66    /*
67    DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming in
68    the original code the source host memory is pageable memory. If the memory
69    is not pageable, call wait() on event return by memcpy API to ensure
70    synchronization behavior.
71    */
72    ck(DPCT_CHECK_ERROR(
73           (dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue()).memcpy(d_table, table.data(), pages * 4).wait()),
74       "table");
75
76    const size_t q8n = (size_t) cells * H * D, q8s = (size_t) cells * H * G, q4n = (size_t) cells * H * D;  // q4: <= 144 B per cell-head
77    int8_t *kq1 = dalloc<int8_t>(q8n), *vq1 = dalloc<int8_t>(q8n), *kq2 = dalloc<int8_t>(q8n), *vq2 = dalloc<int8_t>(q8n);
78    uint16_t *ks1 = dalloc<uint16_t>(q8s), *vs1 = dalloc<uint16_t>(q8s), *ks2 = dalloc<uint16_t>(q8s), *vs2 = dalloc<uint16_t>(q8s);
79    uint8_t *k41 = dalloc<uint8_t>(q4n), *v41 = dalloc<uint8_t>(q4n), *k42 = dalloc<uint8_t>(q4n), *v42 = dalloc<uint8_t>(q4n);
80    uint8_t *h41 = dalloc<uint8_t>(q4n), *h42 = dalloc<uint8_t>(q4n);
81    int8_t *hq1 = dalloc<int8_t>(q8n), *hq2 = dalloc<int8_t>(q8n);
82    uint16_t *hs1 = dalloc<uint16_t>(q8s), *hs2 = dalloc<uint16_t>(q8s);
83
84    int total_cases = 0;
85    for (int n = 1; n <= 8; ++n) {
86        for (int round = 0; round < 3; ++round) {
87            zero(kq1, q8n); zero(vq1, q8n); zero(kq2, q8n); zero(vq2, q8n);
88            zero(ks1, q8s); zero(vs1, q8s); zero(ks2, q8s); zero(vs2, q8s);
89            zero(k41, q4n); zero(v41, q4n); zero(k42, q4n); zero(v42, q4n); zero(h41, q4n); zero(h42, q4n);
90            zero(hq1, q8n); zero(hq2, q8n); zero(hs1, q8s); zero(hs2, q8s);
91            std::vector<int> positions(cells);
92            for (int i = 0; i < cells; ++i) positions[i] = i;
93            std::shuffle(positions.begin(), positions.end(), rng);
94            std::vector<float> hk((size_t) n * H * D), hv(hk.size());
95            for (int t = 0; t < n; ++t) {
96                const float scale = (t % 3 == 0) ? 1e-3f : (t % 5 == 0 ? 40.f : 1.f);
97                for (int i = 0; i < H * D; ++i) {
98                    hk[(size_t) t * H * D + i] = nd(rng) * scale;
99                    hv[(size_t) t * H * D + i] = nd(rng) * scale;
100                }
101                if (t % 4 == 1) std::fill(hk.begin() + (size_t) t * H * D, hk.begin() + (size_t) t * H * D + 64, 0.f);
102            }
103            std::vector<int32_t> hstep((size_t) n * k::kStepCount, 0);
104            for (int t = 0; t < n; ++t) {
105                hstep[(size_t) t * k::kStepCount + k::kStepPos] = positions[t];
106                hstep[(size_t) t * k::kStepCount + 1] = positions[t] + 1;
107            }
108            float *d_k = dalloc<float>(hk.size()), *d_v = dalloc<float>(hv.size());
109            int32_t* d_step = dalloc<int32_t>(hstep.size());
110            /*
111            DPCT1114: cudaMemcpy is migrated to asynchronization memcpy,
112            assuming in the original code the source host memory is pageable
113            memory. If the memory is not pageable, call wait() on event return
114            by memcpy API to ensure synchronization behavior.
115            */
116            ck(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue()).memcpy(
117                   d_k, hk.data(), hk.size() * 4).wait()),
118               "k");
119            /*
120            DPCT1114: cudaMemcpy is migrated to asynchronization memcpy,
121            assuming in the original code the source host memory is pageable
122            memory. If the memory is not pageable, call wait() on event return
123            by memcpy API to ensure synchronization behavior.
124            */
125            ck(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue()).memcpy(
126                   d_v, hv.data(), hv.size() * 4).wait()),
127               "v");
128            /*
129            DPCT1114: cudaMemcpy is migrated to asynchronization memcpy,
130            assuming in the original code the source host memory is pageable
131            memory. If the memory is not pageable, call wait() on event return
132            by memcpy API to ensure synchronization behavior.
133            */
134            ck(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue()).memcpy(
135                   d_step, hstep.data(), hstep.size() * 4).wait()),
136               "step");
137
138            for (int t = 0; t < n; ++t) {
139                const int32_t* st = d_step + (size_t) t * k::kStepCount;
140                const float* kc = d_k + (size_t) t * H * D;
141                const float* vc = d_v + (size_t) t * H * D;
142                k::kv_append_q8_step(kq1, vq1, ks1, vs1, d_table, st, kc, vc, s, nullptr);
143                k::kv_append_q4_step(k41, v41, d_table, st, kc, vc, s, nullptr);
144                k::kv_append_q8_step(hq1, hq1, hs1, hs1, d_table, st, kc, kc, s, nullptr);      // the hybrid's folded K call
145                k::kv_append_q4_step(h41, h41, d_table, st, vc, vc, s, nullptr);                // ... and its folded V call
146            }
147            k::kv_append_q8_steps(kq2, vq2, ks2, vs2, d_table, d_step, k::kStepCount, d_k, d_v, H * D, n, s, nullptr, nullptr);
148            k::kv_append_q4_steps(k42, v42, d_table, d_step, k::kStepCount, n, d_k, d_v, s, nullptr);
149            k::kv_append_q8_steps(hq2, hq2, hs2, hs2, d_table, d_step, k::kStepCount, d_k, d_k, H * D, n, s, nullptr, nullptr);
150            k::kv_append_q4_steps(h42, h42, d_table, d_step, k::kStepCount, n, d_v, d_v, s, nullptr);
151            ck(DPCT_CHECK_ERROR(
152                   dpct::get_current_device().queues_wait_and_throw()),
153               "sync");
154
155            struct Chk { const char* name; bool ok; };
156            const Chk chks[] = {
157                {"INT8 K/V codes", same(fetch(kq1, q8n).data(), fetch(kq2, q8n).data(), q8n) && same(fetch(vq1, q8n).data(), fetch(vq2, q8n).data(), q8n)},
158                {"INT8 K/V scales", same(fetch(ks1, q8s).data(), fetch(ks2, q8s).data(), q8s * 2) && same(fetch(vs1, q8s).data(), fetch(vs2, q8s).data(), q8s * 2)},
159                {"Q4_0 K/V", same(fetch(k41, q4n).data(), fetch(k42, q4n).data(), q4n) && same(fetch(v41, q4n).data(), fetch(v42, q4n).data(), q4n)},
160                {"hybrid K (folded INT8)", same(fetch(hq1, q8n).data(), fetch(hq2, q8n).data(), q8n) && same(fetch(hs1, q8s).data(), fetch(hs2, q8s).data(), q8s * 2)},
161                {"hybrid V (folded Q4_0)", same(fetch(h41, q4n).data(), fetch(h42, q4n).data(), q4n)},
162            };
163            for (const Chk& c : chks) {
164                ++total_cases;
165                if (!c.ok) {
166                    std::printf("    *** n=%d round %d: %s differ from the per-cell appends ***\n", n, round, c.name);
167                    ++g_fail;
168                }
169            }
170            sycl::free(d_k, dpct::get_in_order_queue());
171                sycl::free(d_v, dpct::get_in_order_queue());
172                sycl::free(d_step, dpct::get_in_order_queue());
173        }
174    }
175    std::printf("kv_steps: %d checks, %d failures\n", total_cases, g_fail);
176    if (g_fail) return 1;
177    std::printf("kv_steps_parity OK\n");
178    return 0;
179}