okami.mom/strata

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

Niko1221perf(verify): resident_plan as a parallel grouping and prefix scan (#783 PR-e)fe4de5de8

main
7.3 KiB151 linesraw
1// src/kernels/resident_plan_parity.cpp - the verify window's device-built expert plan (#783 PR-e: the parallel
2// grouping and the prefix scan that replaced thread 0's serial pass) against a host replay of its contract:
3// distinct experts in routing order, each with its entries ascending, `ptr[g]` = the expert's slot, counts / start /
4// dst / tok / start2 in the host pool's layout.
5//
6// n = 1..80 entries (a window of up to kVerifyMaxT tokens x 10 experts), ids drawn from pools of 3..512 experts so
7// duplicates run from "every entry the same expert" to "all distinct", with and without a slot table (`slot_off`).
8// Both modes are run: with a `skip` word (ring when the plan was built, 0 when an expert was not resident - and
9// then the plan is untouched) and without one (the all-resident graph: a non-resident expert gives an empty plan and
10// sets *plan_err).
11// Every word of the plan the host pool would read is compared with memcmp. GPU, synthetic, no model.
12#include "strata/kernels/verify_kernels.hpp"
13
14#include <cuda_runtime.h>
15
16#include <cstdint>
17#include <cstdio>
18#include <cstdlib>
19#include <cstring>
20#include <random>
21#include <vector>
22
23namespace k = strata::kernels;
24
25namespace {
26void ck(cudaError_t e, const char* w) {
27    if (e != cudaSuccess) {
28        std::fprintf(stderr, "%s: %s\n", w, cudaGetErrorString(e));
29        std::exit(2);
30    }
31}
32}  // namespace
33
34int main() {
35    const int K = 10, NE = 512;
36    const long long capx = (long long) k::kVerifyMaxT * K;
37    const long long ptr_off = ((4 + (capx + 1) + 2 * capx) + 1) & ~1ll;
38    const size_t plan_words = (size_t) (ptr_off + 4 * capx + capx + 16);
39    std::mt19937 rng(783);
40    const long long blob = 1337;
41    uint8_t* const cache_base = reinterpret_cast<uint8_t*>(uintptr_t(0x40000000));   // never dereferenced
42
43    int32_t *d_ids, *d_res, *d_plan;
44    unsigned long long* d_off;
45    uint32_t* d_skip;
46    ck(cudaMalloc(&d_ids, 128 * 4), "ids");
47    ck(cudaMalloc(&d_res, NE * 4), "res");
48    ck(cudaMalloc(&d_off, 1024 * 8), "off");
49    ck(cudaMalloc(&d_plan, plan_words * 4), "plan");
50    ck(cudaMalloc(&d_skip, 4), "skip");
51    uint32_t* d_err = nullptr;
52    ck(cudaMalloc(&d_err, 4), "err");
53
54    std::vector<int32_t> res(NE);
55    for (int e = 0; e < NE; ++e) res[e] = (e * 37 + 11) % 1000;      // distinct slots, not the identity
56    std::vector<unsigned long long> off(1024);
57    for (int s = 0; s < 1024; ++s) off[s] = (unsigned long long) s * 4096 + (s % 7) * 64;
58    ck(cudaMemcpy(d_res, res.data(), NE * 4, cudaMemcpyHostToDevice), "res");
59    ck(cudaMemcpy(d_off, off.data(), 1024 * 8, cudaMemcpyHostToDevice), "off");
60
61    int cases = 0, bad = 0;
62    for (int n = 1; n <= 80; ++n) {
63        for (int variant = 0; variant < 4; ++variant) {
64            const int pool = variant == 0 ? 3 : (variant == 1 ? 12 : (variant == 2 ? 40 : 512));
65            std::vector<int32_t> ids(n);
66            for (int i = 0; i < n; ++i) ids[i] = (int32_t) ((rng() % pool) * (512 / pool));
67            const bool use_off = (n + variant) % 2 == 1;
68            ck(cudaMemcpy(d_ids, ids.data(), n * 4, cudaMemcpyHostToDevice), "ids");
69
70            // host replay
71            std::vector<int32_t> counts = {0, n, 0}, start, dst(n), tok(n);
72            std::vector<unsigned long long> ptr;
73            std::vector<int> first_seen;     // group -> expert
74            for (int i = 0; i < n; ++i) {
75                int g = -1;
76                for (size_t q = 0; q < first_seen.size(); ++q)
77                    if (first_seen[q] == ids[i]) g = (int) q;
78                if (g < 0) { first_seen.push_back(ids[i]); g = (int) first_seen.size() - 1; }
79            }
80            const int groups = (int) first_seen.size();
81            counts[0] = groups;
82            start.assign(groups + 1, 0);
83            int pos = 0;
84            for (int g = 0; g < groups; ++g) {
85                start[g] = pos;
86                ptr.push_back((unsigned long long) (uintptr_t) cache_base +
87                              (use_off ? off[res[first_seen[g]]] : (unsigned long long) res[first_seen[g]] * (unsigned long long) blob));
88                for (int i = 0; i < n; ++i)
89                    if (ids[i] == first_seen[g]) { dst[pos] = i; tok[pos] = i / K; ++pos; }
90            }
91            start[groups] = n;
92
93            for (int mode = 0; mode < 2; ++mode) {   // 0: general kernel with a skip word, 1: all resident
94                ck(cudaMemset(d_plan, 0xAB, plan_words * 4), "fill");
95                ck(cudaMemset(d_skip, 0, 4), "skip0");
96                ck(cudaMemset(d_err, 0, 4), "err0");
97                k::resident_plan(d_ids, n, K, d_res, NE, cache_base, use_off ? d_off : nullptr, blob, d_plan, capx,
98                                 mode == 0 ? d_skip : nullptr, 7u, nullptr, d_err);
99                ck(cudaDeviceSynchronize(), "sync");
100                std::vector<int32_t> plan(plan_words);
101                ck(cudaMemcpy(plan.data(), d_plan, plan_words * 4, cudaMemcpyDeviceToHost), "plan");
102                uint32_t skip = 0;
103                ck(cudaMemcpy(&skip, d_skip, 4, cudaMemcpyDeviceToHost), "skipr");
104                bool ok = mode == 1 || skip == 7u;
105                ok = ok && std::memcmp(plan.data(), counts.data(), 3 * 4) == 0;
106                ok = ok && std::memcmp(plan.data() + 4, start.data(), (size_t) (groups + 1) * 4) == 0;
107                const int32_t* pdst = plan.data() + 4 + capx + 1;
108                ok = ok && std::memcmp(pdst, dst.data(), (size_t) n * 4) == 0;
109                ok = ok && std::memcmp(pdst + capx, tok.data(), (size_t) n * 4) == 0;
110                ok = ok && std::memcmp(plan.data() + ptr_off, ptr.data(), (size_t) groups * 8) == 0;
111                ok = ok && plan[(size_t) (ptr_off + 4 * capx)] == n;
112                ++cases;
113                if (!ok) {
114                    std::printf("    *** n=%d pool=%d %s %s: the plan differs from the host replay ***\n", n, pool,
115                                use_off ? "slot_off" : "blob", mode ? "all_resident" : "general");
116                    ++bad;
117                }
118            }
119        }
120    }
121
122    // a routed expert that is not resident: the general kernel leaves the plan alone and zeroes *skip
123    {
124        std::vector<int32_t> res2 = res;
125        res2[5] = -1;
126        ck(cudaMemcpy(d_res, res2.data(), NE * 4, cudaMemcpyHostToDevice), "res2");
127        const int32_t ids[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
128        ck(cudaMemcpy(d_ids, ids, sizeof ids, cudaMemcpyHostToDevice), "ids");
129        ck(cudaMemset(d_plan, 0xAB, plan_words * 4), "fill");
130        const uint32_t seven = 7;
131        ck(cudaMemcpy(d_skip, &seven, 4, cudaMemcpyHostToDevice), "skip7");
132        k::resident_plan(d_ids, 10, K, d_res, NE, cache_base, nullptr, blob, d_plan, capx, d_skip, 7u, nullptr, d_err);
133        ck(cudaDeviceSynchronize(), "sync");
134        std::vector<int32_t> plan(plan_words);
135        ck(cudaMemcpy(plan.data(), d_plan, plan_words * 4, cudaMemcpyDeviceToHost), "plan");
136        uint32_t skip = 99;
137        ck(cudaMemcpy(&skip, d_skip, 4, cudaMemcpyDeviceToHost), "skipr");
138        bool untouched = true;
139        for (int32_t w : plan) untouched = untouched && (uint32_t) w == 0xABABABABu;
140        ++cases;
141        if (skip != 0 || !untouched) {
142            std::printf("    *** a non-resident expert: skip=%u untouched=%d (want 0 / 1) ***\n", skip, (int) untouched);
143            ++bad;
144        }
145    }
146
147    std::printf("resident_plan: %d cases, %d failures\n", cases, bad);
148    if (bad) return 1;
149    std::printf("resident_plan_parity OK\n");
150    return 0;
151}