okami.mom/strata

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

Niko1221verify_batch_parity: take the toolkit arguments of the PLE rule from the engine's own callb90297368

main
8.3 KiB188 linesraw
1// src/kernels/verify_batch_parity.cpp - the verify window's one-launch forms (the hyper-connection write, the PLE post-ops) against the per-token launches they
2// replace, bitwise.
3//
4//     build/verify_batch_parity
5//
6// 1. native_gr_post_multi against n native_gr_post calls (the hyper-connection write before the PLE block).
7// 2. native_ple_postops_batch_snap (STRATA_DF_PLE) against the per-token loop of the verify window: per token
8//    native_ple_postops, ple_history_advance and a copy of the history (the commit's snapshot); the results, the final
9//    history and every snapshot, for windows of 2..8 tokens.
10//    Skipped where the engine switches the batched PLE post-ops off itself (an engine built with CUDA 12.8 on a compute
11//    capability 12.0 card: sm120_series.hpp ple_batch_resolve, the rule the prompt path uses; STRATA_PLE_BATCH=1 runs it anyway).
12// GPU, synthetic, no model.
13#include "strata/kernels/f16_bits.hpp"
14#include "strata/kernels/kv_q4.hpp"
15#include "strata/kernels/kv_stream.hpp"
16#include "strata/kernels/native_gr_postops.hpp"
17#include "strata/kernels/native_ple_postops.hpp"
18#include "strata/kernels/ngram.hpp"
19#include "strata/kernels/ple.hpp"
20#include "strata/kernels/qsa.hpp"
21#include "strata/core/sm120_series.hpp"
22
23#include <cuda_runtime.h>
24
25#include <cstdint>
26#include <cstdio>
27#include <cstdlib>
28#include <cstring>
29#include <random>
30#include <vector>
31
32namespace k = strata::kernels;
33
34namespace {
35
36int g_fail = 0;
37
38void ck(cudaError_t e, const char* what) {
39    if (e != cudaSuccess) {
40        std::printf("CUDA: %s: %s\n", what, cudaGetErrorString(e));
41        std::exit(2);
42    }
43}
44
45// The set-up copies and fills run on the legacy default stream, which does NOT order work on the non-blocking stream
46// the kernels run on (and cudaMemset / a pageable cudaMemcpy may return before the device has finished them): each
47// one is completed here, or a kernel could read its inputs - or have its outputs overwritten - while they land.
48template <typename T>
49T* dalloc(size_t n, int fill = 0) {
50    T* p = nullptr;
51    ck(cudaMalloc(&p, n * sizeof(T) + 64), "malloc");
52    ck(cudaMemset(p, fill, n * sizeof(T) + 64), "memset");
53    ck(cudaDeviceSynchronize(), "memset sync");
54    return p;
55}
56
57template <typename T>
58std::vector<T> read(const T* d, size_t n) {
59    std::vector<T> h(n);
60    ck(cudaMemcpy(h.data(), d, n * sizeof(T), cudaMemcpyDeviceToHost), "read");
61    return h;
62}
63
64template <typename T>
65T* upload(const std::vector<T>& h) {
66    T* d = dalloc<T>(h.size());
67    ck(cudaMemcpy(d, h.data(), h.size() * sizeof(T), cudaMemcpyHostToDevice), "upload");
68    ck(cudaDeviceSynchronize(), "upload sync");
69    return d;
70}
71
72void report(const char* what, bool ok) {
73    std::printf("%-64s %s\n", what, ok ? "bitwise equal" : "DIFFERENT");
74    if (!ok) ++g_fail;
75}
76
77// ---- 1. the hyper-connection write
78void gr_post(cudaStream_t s) {
79    const int N = 2560, HC = 4;
80    std::mt19937 rng(12);
81    std::normal_distribution<float> nd(0.f, 1.f);
82    for (int n : {2, 4, 8}) {
83        std::vector<float> r((size_t) n * HC * N), bo((size_t) n * N), inj((size_t) n * HC);
84        for (auto& x : r) x = nd(rng);
85        for (auto& x : bo) x = nd(rng);
86        for (auto& x : inj) x = 3.f * nd(rng);
87        float* r0 = upload(r);
88        float* r1 = upload(r);
89        float* d_bo = upload(bo);
90        float* d_inj = upload(inj);
91        for (int t = 0; t < n; ++t)
92            k::native_gr_post(r0 + (size_t) t * HC * N, d_bo + (size_t) t * N, d_inj + (size_t) t * HC,
93                              r0 + (size_t) t * HC * N, N, HC, s);
94        k::native_gr_post_multi(r1, d_bo, d_inj, r1, N, HC, n, (long long) HC * N, N, HC, s);
95        ck(cudaStreamSynchronize(s), "gr post");
96        char what[128];
97        std::snprintf(what, sizeof what, "native_gr_post_multi, %d tokens (in place)", n);
98        report(what, read(r0, r.size()) == read(r1, r.size()));
99        cudaFree(r0); cudaFree(r1); cudaFree(d_bo); cudaFree(d_inj);
100    }
101}
102
103// ---- 2. the PLE post-ops of a window, with the commit's history snapshots
104void ple(cudaStream_t s) {
105    constexpr int N = k::NG_N_EMBD, D = k::NG_HC_DIM, HS = k::NG_HIST * k::NG_HC_DIM;
106    std::mt19937 rng(13);
107    std::normal_distribution<float> nd(0.f, 1.f);
108    std::vector<float> nk(D), nq(D), nc(D);
109    for (auto* v : {&nk, &nq, &nc})
110        for (auto& x : *v) x = 1.f + 0.1f * nd(rng);
111    std::vector<uint16_t> conv((size_t) 4 * D);
112    for (auto& x : conv) x = k::f16_from_f32(0.3f * nd(rng));
113    k::PleWeights w;
114    w.norm_key = upload(nk);
115    w.norm_query = upload(nq);
116    w.norm_conv = upload(nc);
117    w.conv1d_f16 = upload(conv);
118    std::vector<float> hist0((size_t) HS);
119    for (auto& x : hist0) x = nd(rng);
120    for (int n : {2, 3, 4, 8}) {
121        std::vector<float> key((size_t) n * D), hid((size_t) n * D), val((size_t) n * N);
122        for (auto& x : key) x = 4.f * nd(rng);
123        for (auto& x : hid) x = nd(rng);
124        for (auto& x : val) x = nd(rng);
125        // per token, as the verify window's loop: native_ple_postops (ple_block_projected's buffers), the history
126        // advance and the snapshot copy
127        float* key0 = upload(key);
128        float* hid0 = upload(hid);
129        float* val0 = upload(val);
130        float* hist_a = upload(hist0);
131        float* snap_a = dalloc<float>((size_t) n * HS);
132        float *q = dalloc<float>(D), *nrm = dalloc<float>(D), *g = dalloc<float>(4), *gd = dalloc<float>(D);
133        float* cv = dalloc<float>(D);
134        for (int t = 0; t < n; ++t) {
135            k::NativePlePostopsBuffers b{q, nrm, g, gd, nrm, cv, hid0 + (size_t) t * D};
136            k::native_ple_postops(key0 + (size_t) t * D, hid0 + (size_t) t * D, val0 + (size_t) t * N, hist_a, w, b, s);
137            k::ple_history_advance(hist_a, nrm, s);
138            ck(cudaMemcpyAsync(snap_a + (size_t) t * HS, hist_a, (size_t) HS * 4, cudaMemcpyDeviceToDevice, s), "snap");
139        }
140        // the window's one pass
141        float* key1 = upload(key);
142        float* hid1 = upload(hid);
143        float* val1 = upload(val);
144        float* hist_b = upload(hist0);
145        float* snap_b = dalloc<float>((size_t) n * HS);
146        float *qn = dalloc<float>((size_t) n * D), *gated = dalloc<float>((size_t) n * D), *gate = dalloc<float>((size_t) n * 4);
147        k::native_ple_postops_batch_snap(key1, hid1, val1, hist_b, w, qn, gated, gate, n, snap_b, s);
148        ck(cudaStreamSynchronize(s), "ple");
149        char what[128];
150        std::snprintf(what, sizeof what, "native_ple_postops_batch_snap, %d tokens: results", n);
151        report(what, read(hid0, hid.size()) == read(hid1, hid.size()));
152        std::snprintf(what, sizeof what, "native_ple_postops_batch_snap, %d tokens: history", n);
153        report(what, read(hist_a, (size_t) HS) == read(hist_b, (size_t) HS));
154        std::snprintf(what, sizeof what, "native_ple_postops_batch_snap, %d tokens: snapshots", n);
155        report(what, read(snap_a, (size_t) n * HS) == read(snap_b, (size_t) n * HS));
156        for (float* p : {key0, hid0, val0, hist_a, snap_a, q, nrm, g, gd, cv, key1, hid1, val1, hist_b, snap_b, qn, gated,
157                         gate})
158            cudaFree(p);
159    }
160}
161
162}  // namespace
163
164int main() {
165    cudaDeviceProp prop{};
166    int dev = 0;
167    ck(cudaGetDevice(&dev), "device");
168    ck(cudaGetDeviceProperties(&prop, dev), "props");
169    std::printf("verify_batch_parity: %s (sm_%d%d)\n", prop.name, prop.major, prop.minor);
170    cudaStream_t s = nullptr;
171    ck(cudaStreamCreateWithFlags(&s, cudaStreamNonBlocking), "stream");
172    gr_post(s);
173    // the engine's own rule for the batched PLE post-ops (prefill.cpp ple_batch_enabled, same toolkit arguments): off for a CUDA 12.8 build on a 12.0 card unless STRATA_PLE_BATCH says otherwise
174#if defined(CUDART_VERSION) && !defined(STRATA_USE_HIP) && !defined(__HIPCC__)
175    constexpr int kRt = CUDART_VERSION;
176    constexpr bool kHip = false;
177#else
178    constexpr int kRt = 0;      // a HIP build has no CUDA toolkit version: the engine passes 0 and hip = true
179    constexpr bool kHip = true;
180#endif
181    if (strata::ple_batch_resolve(std::getenv("STRATA_PLE_BATCH"), kRt, kHip, prop.major, prop.minor))
182        ple(s);
183    else
184        std::printf("SKIP native_ple_postops_batch_snap: built with CUDA 12.8 for RTX 50: the batched PLE post-ops are switched off in this build "
185                    "(STRATA_PLE_BATCH=1 runs the step anyway)\n");
186    std::printf("verify_batch_parity: %s\n", g_fail ? "FAIL" : "PASS");
187    return g_fail ? 1 : 0;
188}