okami.mom/strata

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

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

main
7.9 KiB151 linesraw
1// route_window_parity - the verify window's batched routing against the per-token calls, bit for bit.
2//
3// `moe_route_window` replaces n per-token (projection + top-k) pairs by one multi-column BF16 projection and one
4// n-token top-k.  Speculative decoding is exact only if a token's route does not depend on how many tokens share
5// its window, so both batched kernels must equal their one-token forms exactly - this checks that on random data
6// shaped like the router (2560 -> 256, top-10) and like the shared-expert gate (2560 -> 1).
7#define DPCT_PROFILING_ENABLED
8#include <sycl/sycl.hpp>
9#include <dpct/dpct.hpp>
10#include "strata/kernels/bf16_gemv.hpp"
11#include "strata/kernels/router_top10.hpp"
12
13#include <cstdint>
14#include <cstdio>
15#include <cstring>
16#include <random>
17#include <vector>
18
19namespace {
20void ck(dpct::err0 e, const char *what) {
21    /*
22    DPCT1009: SYCL reports errors using exceptions and does not use error
23    codes. Please replace the "get_error_string_dummy(...)" with a real
24    error-handling function.
25    */
26}
27uint16_t bf16(float f) { uint32_t i; std::memcpy(&i, &f, 4); return (uint16_t) ((i + ((i >> 16) & 1u) + 0x7FFFu) >> 16); }
28}  // namespace
29
30int main() {
31    const int N = 2560, E = 256, K = 10;
32    int bad = 0;
33    std::mt19937 rng(7);
34    std::normal_distribution<float> nd(0.0f, 1.0f);
35    for (int n_out : {E, 1}) {
36        for (int T : {2, 3, 4, 6}) {
37            std::vector<uint16_t> w((size_t) n_out * N);
38            std::vector<float> x((size_t) T * N);
39            for (auto& v : w) v = bf16(nd(rng) * 0.05f);
40            for (auto& v : x) v = nd(rng);
41            uint16_t* dw; float *dx, *dy1, *dyn;
42            ck(DPCT_CHECK_ERROR(dw = (uint16_t *)sycl::malloc_device(
43                                    w.size() * 2, dpct::get_in_order_queue())),
44               "w");
45                ck(DPCT_CHECK_ERROR(
46                       dx = (float *)sycl::malloc_device(
47                           x.size() * 4, dpct::get_in_order_queue())),
48                   "x");
49            ck(DPCT_CHECK_ERROR(
50                   dy1 = (float *)sycl::malloc_device(
51                       (size_t)T * n_out * 4, dpct::get_in_order_queue())),
52               "y1");
53                ck(DPCT_CHECK_ERROR(
54                       dyn = (float *)sycl::malloc_device(
55                           (size_t)T * n_out * 4, dpct::get_in_order_queue())),
56                   "yn");
57            /*
58            DPCT1114: cudaMemcpy is migrated to asynchronization memcpy,
59            assuming in the original code the source host memory is pageable
60            memory. If the memory is not pageable, call wait() on event return
61            by memcpy API to ensure synchronization behavior.
62            */
63            ck(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue()).memcpy(
64                   dw, w.data(), w.size() * 2).wait()),
65               "up w");
66            /*
67            DPCT1114: cudaMemcpy is migrated to asynchronization memcpy,
68            assuming in the original code the source host memory is pageable
69            memory. If the memory is not pageable, call wait() on event return
70            by memcpy API to ensure synchronization behavior.
71            */
72            ck(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue()).memcpy(
73                   dx, x.data(), x.size() * 4).wait()),
74               "up x");
75            for (int t = 0; t < T; ++t)
76                strata::kernels::bf16_gemv_fp32_mmvf(dx + (size_t) t * N, dw, dy1 + (size_t) t * n_out, N, n_out, nullptr);
77            strata::kernels::bf16_gemv_fp32_mmvf_cols(dx, dw, dyn, N, n_out, T, nullptr);
78            ck(DPCT_CHECK_ERROR(
79                   dpct::get_current_device().queues_wait_and_throw()),
80               "sync");
81            std::vector<float> y1((size_t) T * n_out), yn((size_t) T * n_out);
82            ck(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue())
83                                    .memcpy(y1.data(), dy1, y1.size() * 4)
84                                    .wait()),
85               "down 1");
86            ck(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue())
87                                    .memcpy(yn.data(), dyn, yn.size() * 4)
88                                    .wait()),
89               "down n");
90            const bool same = std::memcmp(y1.data(), yn.data(), y1.size() * 4) == 0;
91            std::printf("  mmvf cols  n_out %3d  T %d  %s\n", n_out, T, same ? "bit-identical" : "*** DIFFERS ***");
92            if (!same) ++bad;
93            if (n_out == E) {
94                int *i1, *in_; float *w1, *wn;
95                ck(DPCT_CHECK_ERROR(
96                       i1 = (int *)sycl::malloc_device(
97                           (size_t)T * K * 4, dpct::get_in_order_queue())),
98                   "i1");
99                    ck(DPCT_CHECK_ERROR(
100                           in_ = (int *)sycl::malloc_device(
101                               (size_t)T * K * 4, dpct::get_in_order_queue())),
102                       "in");
103                ck(DPCT_CHECK_ERROR(
104                       w1 = (float *)sycl::malloc_device(
105                           (size_t)T * K * 4, dpct::get_in_order_queue())),
106                   "w1");
107                    ck(DPCT_CHECK_ERROR(
108                           wn = (float *)sycl::malloc_device(
109                               (size_t)T * K * 4, dpct::get_in_order_queue())),
110                       "wn");
111                for (int t = 0; t < T; ++t)
112                    strata::kernels::router_top10(dy1 + (size_t) t * E, 1, E, K, i1 + t * K, w1 + t * K, nullptr);
113                strata::kernels::router_top10(dy1, T, E, K, in_, wn, nullptr);
114                ck(DPCT_CHECK_ERROR(
115                       dpct::get_current_device().queues_wait_and_throw()),
116                   "sync top");
117                std::vector<int> a((size_t) T * K), b((size_t) T * K);
118                std::vector<float> fa((size_t) T * K), fb((size_t) T * K);
119                ck(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue())
120                                        .memcpy(a.data(), i1, a.size() * 4)
121                                        .wait()),
122                   "ids1");
123                ck(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue())
124                                        .memcpy(b.data(), in_, b.size() * 4)
125                                        .wait()),
126                   "idsn");
127                ck(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue())
128                                        .memcpy(fa.data(), w1, fa.size() * 4)
129                                        .wait()),
130                   "w1");
131                ck(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue())
132                                        .memcpy(fb.data(), wn, fb.size() * 4)
133                                        .wait()),
134                   "wn");
135                const bool s2 = a == b && std::memcmp(fa.data(), fb.data(), fa.size() * 4) == 0;
136                std::printf("  top10 n    T %d            %s\n", T, s2 ? "bit-identical" : "*** DIFFERS ***");
137                if (!s2) ++bad;
138                sycl::free(i1, dpct::get_in_order_queue());
139                    sycl::free(in_, dpct::get_in_order_queue());
140                    sycl::free(w1, dpct::get_in_order_queue());
141                    sycl::free(wn, dpct::get_in_order_queue());
142            }
143            sycl::free(dw, dpct::get_in_order_queue());
144                sycl::free(dx, dpct::get_in_order_queue());
145                sycl::free(dy1, dpct::get_in_order_queue());
146                sycl::free(dyn, dpct::get_in_order_queue());
147        }
148    }
149    std::printf(bad ? "route_window_parity: %d FAILURES\n" : "route_window_parity OK\n", bad);
150    return bad ? 1 : 0;
151}