okami.mom/strata

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

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

main
11.3 KiB227 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#define DPCT_PROFILING_ENABLED
13#include <sycl/sycl.hpp>
14#include <dpct/dpct.hpp>
15#include "strata/kernels/verify_kernels.hpp"
16
17#include <cstdint>
18#include <cstdio>
19#include <cstdlib>
20#include <cstring>
21#include <random>
22#include <vector>
23
24namespace k = strata::kernels;
25
26namespace {
27void ck(dpct::err0 e, const char *w) {
28}
29}  // namespace
30
31int main() {
32    const int K = 10, NE = 512;
33    const long long capx = (long long) k::kVerifyMaxT * K;
34    const long long ptr_off = ((4 + (capx + 1) + 2 * capx) + 1) & ~1ll;
35    const size_t plan_words = (size_t) (ptr_off + 4 * capx + capx + 16);
36    std::mt19937 rng(783);
37    const long long blob = 1337;
38    uint8_t* const cache_base = reinterpret_cast<uint8_t*>(uintptr_t(0x40000000));   // never dereferenced
39
40    int32_t *d_ids, *d_res, *d_plan;
41    unsigned long long* d_off;
42    uint32_t* d_skip;
43    ck(DPCT_CHECK_ERROR(d_ids = (int32_t *)sycl::malloc_device(
44                            128 * 4, dpct::get_in_order_queue())),
45       "ids");
46    ck(DPCT_CHECK_ERROR(d_res = (int32_t *)sycl::malloc_device(
47                            NE * 4, dpct::get_in_order_queue())),
48       "res");
49    ck(DPCT_CHECK_ERROR(d_off = (unsigned long long *)sycl::malloc_device(
50                            1024 * 8, dpct::get_in_order_queue())),
51       "off");
52    ck(DPCT_CHECK_ERROR(d_plan = (int32_t *)sycl::malloc_device(
53                            plan_words * 4, dpct::get_in_order_queue())),
54       "plan");
55    ck(DPCT_CHECK_ERROR(d_skip = (uint32_t *)sycl::malloc_device(
56                            4, dpct::get_in_order_queue())),
57       "skip");
58    uint32_t* d_err = nullptr;
59    ck(DPCT_CHECK_ERROR(d_err = (uint32_t *)sycl::malloc_device(
60                            4, dpct::get_in_order_queue())),
61       "err");
62
63    std::vector<int32_t> res(NE);
64    for (int e = 0; e < NE; ++e) res[e] = (e * 37 + 11) % 1000;      // distinct slots, not the identity
65    std::vector<unsigned long long> off(1024);
66    for (int s = 0; s < 1024; ++s) off[s] = (unsigned long long) s * 4096 + (s % 7) * 64;
67    /*
68    DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming
69    in the original code the source host memory is pageable memory. If the
70    memory is not pageable, call wait() on event return by memcpy API to ensure
71    synchronization behavior.
72    */
73    ck(DPCT_CHECK_ERROR(
74           (dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue()).memcpy(d_res, res.data(), NE * 4).wait()),
75       "res");
76    /*
77    DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming
78    in the original code the source host memory is pageable memory. If the
79    memory is not pageable, call wait() on event return by memcpy API to ensure
80    synchronization behavior.
81    */
82    ck(DPCT_CHECK_ERROR(
83           (dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue()).memcpy(d_off, off.data(), 1024 * 8).wait()),
84       "off");
85
86    int cases = 0, bad = 0;
87    for (int n = 1; n <= 80; ++n) {
88        for (int variant = 0; variant < 4; ++variant) {
89            const int pool = variant == 0 ? 3 : (variant == 1 ? 12 : (variant == 2 ? 40 : 512));
90            std::vector<int32_t> ids(n);
91            for (int i = 0; i < n; ++i) ids[i] = (int32_t) ((rng() % pool) * (512 / pool));
92            const bool use_off = (n + variant) % 2 == 1;
93            /*
94            DPCT1114: cudaMemcpy is migrated to asynchronization memcpy,
95            assuming in the original code the source host memory is pageable
96            memory. If the memory is not pageable, call wait() on event return
97            by memcpy API to ensure synchronization behavior.
98            */
99            ck(DPCT_CHECK_ERROR(
100                   (dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue()).memcpy(d_ids, ids.data(), n * 4).wait()),
101               "ids");
102
103            // host replay
104            std::vector<int32_t> counts = {0, n, 0}, start, dst(n), tok(n);
105            std::vector<unsigned long long> ptr;
106            std::vector<int> first_seen;     // group -> expert
107            for (int i = 0; i < n; ++i) {
108                int g = -1;
109                for (size_t q = 0; q < first_seen.size(); ++q)
110                    if (first_seen[q] == ids[i]) g = (int) q;
111                if (g < 0) { first_seen.push_back(ids[i]); g = (int) first_seen.size() - 1; }
112            }
113            const int groups = (int) first_seen.size();
114            counts[0] = groups;
115            start.assign(groups + 1, 0);
116            int pos = 0;
117            for (int g = 0; g < groups; ++g) {
118                start[g] = pos;
119                ptr.push_back((unsigned long long) (uintptr_t) cache_base +
120                              (use_off ? off[res[first_seen[g]]] : (unsigned long long) res[first_seen[g]] * (unsigned long long) blob));
121                for (int i = 0; i < n; ++i)
122                    if (ids[i] == first_seen[g]) { dst[pos] = i; tok[pos] = i / K; ++pos; }
123            }
124            start[groups] = n;
125
126            for (int mode = 0; mode < 2; ++mode) {   // 0: general kernel with a skip word, 1: all resident
127                ck(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue())
128                                        .memset(d_plan, 0xAB, plan_words * 4)
129                                        .wait()),
130                   "fill");
131                ck(DPCT_CHECK_ERROR(
132                       (dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue()).memset(d_skip, 0, 4).wait()),
133                   "skip0");
134                ck(DPCT_CHECK_ERROR(
135                       (dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue()).memset(d_err, 0, 4).wait()),
136                   "err0");
137                k::resident_plan(d_ids, n, K, d_res, NE, cache_base, use_off ? d_off : nullptr, blob, d_plan, capx,
138                                 mode == 0 ? d_skip : nullptr, 7u, nullptr, d_err);
139                ck(DPCT_CHECK_ERROR(
140                       dpct::get_current_device().queues_wait_and_throw()),
141                   "sync");
142                std::vector<int32_t> plan(plan_words);
143                ck(DPCT_CHECK_ERROR(
144                       (dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue())
145                           .memcpy(plan.data(), d_plan, plan_words * 4)
146                           .wait()),
147                   "plan");
148                uint32_t skip = 0;
149                ck(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue())
150                                        .memcpy(&skip, d_skip, 4)
151                                        .wait()),
152                   "skipr");
153                bool ok = mode == 1 || skip == 7u;
154                ok = ok && std::memcmp(plan.data(), counts.data(), 3 * 4) == 0;
155                ok = ok && std::memcmp(plan.data() + 4, start.data(), (size_t) (groups + 1) * 4) == 0;
156                const int32_t* pdst = plan.data() + 4 + capx + 1;
157                ok = ok && std::memcmp(pdst, dst.data(), (size_t) n * 4) == 0;
158                ok = ok && std::memcmp(pdst + capx, tok.data(), (size_t) n * 4) == 0;
159                ok = ok && std::memcmp(plan.data() + ptr_off, ptr.data(), (size_t) groups * 8) == 0;
160                ok = ok && plan[(size_t) (ptr_off + 4 * capx)] == n;
161                ++cases;
162                if (!ok) {
163                    std::printf("    *** n=%d pool=%d %s %s: the plan differs from the host replay ***\n", n, pool,
164                                use_off ? "slot_off" : "blob", mode ? "all_resident" : "general");
165                    ++bad;
166                }
167            }
168        }
169    }
170
171    // a routed expert that is not resident: the general kernel leaves the plan alone and zeroes *skip
172    {
173        std::vector<int32_t> res2 = res;
174        res2[5] = -1;
175        /*
176        DPCT1114: cudaMemcpy is migrated to asynchronization memcpy,
177        assuming in the original code the source host memory is pageable memory.
178        If the memory is not pageable, call wait() on event return by memcpy API
179        to ensure synchronization behavior.
180        */
181        ck(DPCT_CHECK_ERROR(
182               (dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue()).memcpy(d_res, res2.data(), NE * 4).wait()),
183           "res2");
184        const int32_t ids[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
185        /*
186        DPCT1114: cudaMemcpy is migrated to asynchronization memcpy,
187        assuming in the original code the source host memory is pageable memory.
188        If the memory is not pageable, call wait() on event return by memcpy API
189        to ensure synchronization behavior.
190        */
191        ck(DPCT_CHECK_ERROR(
192               (dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue()).memcpy(d_ids, ids, sizeof ids).wait()),
193           "ids");
194        ck(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue())
195                                .memset(d_plan, 0xAB, plan_words * 4)
196                                .wait()),
197           "fill");
198        const uint32_t seven = 7;
199        ck(DPCT_CHECK_ERROR(
200               (dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue()).memcpy(d_skip, &seven, 4).wait()),
201           "skip7");
202        k::resident_plan(d_ids, 10, K, d_res, NE, cache_base, nullptr, blob, d_plan, capx, d_skip, 7u, nullptr, d_err);
203        ck(DPCT_CHECK_ERROR(dpct::get_current_device().queues_wait_and_throw()),
204           "sync");
205        std::vector<int32_t> plan(plan_words);
206        ck(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue())
207                                .memcpy(plan.data(), d_plan, plan_words * 4)
208                                .wait()),
209           "plan");
210        uint32_t skip = 99;
211        ck(DPCT_CHECK_ERROR(
212               (dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue()).memcpy(&skip, d_skip, 4).wait()),
213           "skipr");
214        bool untouched = true;
215        for (int32_t w : plan) untouched = untouched && (uint32_t) w == 0xABABABABu;
216        ++cases;
217        if (skip != 0 || !untouched) {
218            std::printf("    *** a non-resident expert: skip=%u untouched=%d (want 0 / 1) ***\n", skip, (int) untouched);
219            ++bad;
220        }
221    }
222
223    std::printf("resident_plan: %d cases, %d failures\n", cases, bad);
224    if (bad) return 1;
225    std::printf("resident_plan_parity OK\n");
226    return 0;
227}