okami.mom/strata

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

Niko1221Merge branch 'f-139b-old' into f-139-relbd34d0d87

main
4.2 KiB75 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#include "strata/kernels/bf16_gemv.hpp"
8#include "strata/kernels/router_top10.hpp"
9
10#include <cuda_runtime.h>
11
12#include <cstdint>
13#include <cstdio>
14#include <cstring>
15#include <random>
16#include <vector>
17
18namespace {
19void ck(cudaError_t e, const char* what) {
20    if (e != cudaSuccess) { std::fprintf(stderr, "%s: %s\n", what, cudaGetErrorString(e)); std::exit(2); }
21}
22uint16_t bf16(float f) { uint32_t i; std::memcpy(&i, &f, 4); return (uint16_t) ((i + ((i >> 16) & 1u) + 0x7FFFu) >> 16); }
23}  // namespace
24
25int main() {
26    const int N = 2560, E = 256, K = 10;
27    int bad = 0;
28    std::mt19937 rng(7);
29    std::normal_distribution<float> nd(0.0f, 1.0f);
30    for (int n_out : {E, 1}) {
31        for (int T : {2, 3, 4, 6}) {
32            std::vector<uint16_t> w((size_t) n_out * N);
33            std::vector<float> x((size_t) T * N);
34            for (auto& v : w) v = bf16(nd(rng) * 0.05f);
35            for (auto& v : x) v = nd(rng);
36            uint16_t* dw; float *dx, *dy1, *dyn;
37            ck(cudaMalloc(&dw, w.size() * 2), "w"); ck(cudaMalloc(&dx, x.size() * 4), "x");
38            ck(cudaMalloc(&dy1, (size_t) T * n_out * 4), "y1"); ck(cudaMalloc(&dyn, (size_t) T * n_out * 4), "yn");
39            ck(cudaMemcpy(dw, w.data(), w.size() * 2, cudaMemcpyHostToDevice), "up w");
40            ck(cudaMemcpy(dx, x.data(), x.size() * 4, cudaMemcpyHostToDevice), "up x");
41            for (int t = 0; t < T; ++t)
42                strata::kernels::bf16_gemv_fp32_mmvf(dx + (size_t) t * N, dw, dy1 + (size_t) t * n_out, N, n_out, nullptr);
43            strata::kernels::bf16_gemv_fp32_mmvf_cols(dx, dw, dyn, N, n_out, T, nullptr);
44            ck(cudaDeviceSynchronize(), "sync");
45            std::vector<float> y1((size_t) T * n_out), yn((size_t) T * n_out);
46            ck(cudaMemcpy(y1.data(), dy1, y1.size() * 4, cudaMemcpyDeviceToHost), "down 1");
47            ck(cudaMemcpy(yn.data(), dyn, yn.size() * 4, cudaMemcpyDeviceToHost), "down n");
48            const bool same = std::memcmp(y1.data(), yn.data(), y1.size() * 4) == 0;
49            std::printf("  mmvf cols  n_out %3d  T %d  %s\n", n_out, T, same ? "bit-identical" : "*** DIFFERS ***");
50            if (!same) ++bad;
51            if (n_out == E) {
52                int *i1, *in_; float *w1, *wn;
53                ck(cudaMalloc(&i1, (size_t) T * K * 4), "i1"); ck(cudaMalloc(&in_, (size_t) T * K * 4), "in");
54                ck(cudaMalloc(&w1, (size_t) T * K * 4), "w1"); ck(cudaMalloc(&wn, (size_t) T * K * 4), "wn");
55                for (int t = 0; t < T; ++t)
56                    strata::kernels::router_top10(dy1 + (size_t) t * E, 1, E, K, i1 + t * K, w1 + t * K, nullptr);
57                strata::kernels::router_top10(dy1, T, E, K, in_, wn, nullptr);
58                ck(cudaDeviceSynchronize(), "sync top");
59                std::vector<int> a((size_t) T * K), b((size_t) T * K);
60                std::vector<float> fa((size_t) T * K), fb((size_t) T * K);
61                ck(cudaMemcpy(a.data(), i1, a.size() * 4, cudaMemcpyDeviceToHost), "ids1");
62                ck(cudaMemcpy(b.data(), in_, b.size() * 4, cudaMemcpyDeviceToHost), "idsn");
63                ck(cudaMemcpy(fa.data(), w1, fa.size() * 4, cudaMemcpyDeviceToHost), "w1");
64                ck(cudaMemcpy(fb.data(), wn, fb.size() * 4, cudaMemcpyDeviceToHost), "wn");
65                const bool s2 = a == b && std::memcmp(fa.data(), fb.data(), fa.size() * 4) == 0;
66                std::printf("  top10 n    T %d            %s\n", T, s2 ? "bit-identical" : "*** DIFFERS ***");
67                if (!s2) ++bad;
68                cudaFree(i1); cudaFree(in_); cudaFree(w1); cudaFree(wn);
69            }
70            cudaFree(dw); cudaFree(dx); cudaFree(dy1); cudaFree(dyn);
71        }
72    }
73    std::printf(bad ? "route_window_parity: %d FAILURES\n" : "route_window_parity OK\n", bad);
74    return bad ? 1 : 0;
75}