okami.mom/strata

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

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

main
6.1 KiB127 linesraw
1// Capacity 262144, live prefill up to the register boundary, and the legacy fallbacks.
2// Compare selected IDs only: unused output padding is not part of the API contract.
3#define DPCT_PROFILING_ENABLED
4#include <sycl/sycl.hpp>
5#include <dpct/dpct.hpp>
6#include "strata/kernels/qsa.hpp"
7#include "strata/kernels/qsa_select.hpp"
8#include <algorithm>
9#include <cstdio>
10#include <cstdlib>
11#include <random>
12#include <vector>
13
14namespace k = strata::kernels;
15namespace {
16void ck(dpct::err0 e) {
17}
18struct Case { int64_t ctx, queries; int mode; };
19bool check(Case c) {
20    constexpr int64_t stride = 262144 / 4 + 2;
21    const auto s = k::qsa_real_shapes();
22    const int64_t cap = k::qsa_selection_width(k::kTopkMaxCells, s);
23    std::vector<int32_t> steps((size_t)(c.queries * k::kStepCount));
24    std::vector<float> scores((size_t)(c.queries * stride), -999.f);
25    std::mt19937 rng(742913);
26    std::uniform_real_distribution<float> random(0.f, 200.f);
27    for (int64_t i = 0; i < c.queries; ++i) {
28        auto* st = steps.data() + i * k::kStepCount;
29        const int64_t pos = c.ctx - c.queries + i;
30        st[k::kStepPos] = (int32_t)pos;
31        st[k::kStepNKv] = (int32_t)(pos + 1);
32        st[k::kStepNBid] = (int32_t)((pos + 1) / s.idx_block);
33        st[k::kStepWidth] = (int32_t)k::qsa_selection_width(pos + 1, s);
34        for (int64_t j = 0; j <= st[k::kStepNBid]; ++j)
35            scores[(size_t)(i * stride + j)] = c.mode == 1 ? 0.f : c.mode == 2 ? float(j % 7) : random(rng);
36        if ((pos + 1) % s.idx_block)
37            scores[(size_t)(i * stride + st[k::kStepNBid])] += 1e9f;
38    }
39    int64_t active = steps[(size_t)((c.queries - 1) * k::kStepCount + k::kStepNBid)] + 1;
40    if (c.mode == 3) active = 0;
41    if (c.mode == 4) active = stride + 1;
42    float* ds = nullptr;
43    int32_t *dt = nullptr, *da = nullptr, *db = nullptr;
44    ck(DPCT_CHECK_ERROR(ds = sycl::malloc_device<float>(
45                            scores.size(), dpct::get_in_order_queue())));
46    ck(DPCT_CHECK_ERROR(dt = sycl::malloc_device<int32_t>(
47                            steps.size(), dpct::get_in_order_queue())));
48    ck(DPCT_CHECK_ERROR(
49        da = sycl::malloc_device<int32_t>((size_t)(c.queries * cap),
50                                          dpct::get_in_order_queue())));
51    ck(DPCT_CHECK_ERROR(
52        db = sycl::malloc_device<int32_t>((size_t)(c.queries * cap),
53                                          dpct::get_in_order_queue())));
54    /*
55    DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming
56    in the original code the source host memory is pageable memory. If the
57    memory is not pageable, call wait() on event return by memcpy API to ensure
58    synchronization behavior.
59    */
60    ck(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue()).memcpy(
61        ds, scores.data(), scores.size() * sizeof(float)).wait()));
62    /*
63    DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming
64    in the original code the source host memory is pageable memory. If the
65    memory is not pageable, call wait() on event return by memcpy API to ensure
66    synchronization behavior.
67    */
68    ck(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue()).memcpy(
69        dt, steps.data(), steps.size() * sizeof(int32_t)).wait()));
70    // No bound: the exact capacity-based dispatch used by captured decode.
71    k::qsa_block_topk(ds, dt, c.queries, stride, cap, s, da, nullptr);
72    k::qsa_block_topk(ds, dt, c.queries, stride, cap, s, db, nullptr, active);
73    ck(DPCT_CHECK_ERROR(dpct::get_current_device().queues_wait_and_throw()));
74    std::vector<int32_t> a((size_t)(c.queries * cap)), b(a.size());
75    ck(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue())
76                            .memcpy(a.data(), da, a.size() * sizeof(int32_t))
77                            .wait()));
78    ck(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue())
79                            .memcpy(b.data(), db, b.size() * sizeof(int32_t))
80                            .wait()));
81    bool ok = true;
82    for (int64_t i = 0; i < c.queries; ++i) {
83        const int64_t w = steps[(size_t)(i * k::kStepCount + k::kStepWidth)];
84        if (!std::equal(a.begin() + i * cap, a.begin() + i * cap + w, b.begin() + i * cap)) {
85            std::fprintf(stderr, "FAIL ctx=%lld queries=%lld mode=%d row=%lld\n",
86                         (long long)c.ctx, (long long)c.queries, c.mode, (long long)i);
87            ok = false;
88            break;
89        }
90    }
91    ck(DPCT_CHECK_ERROR(sycl::free(ds, dpct::get_in_order_queue())));
92        ck(DPCT_CHECK_ERROR(sycl::free(dt, dpct::get_in_order_queue())));
93        ck(DPCT_CHECK_ERROR(sycl::free(da, dpct::get_in_order_queue())));
94        ck(DPCT_CHECK_ERROR(sycl::free(db, dpct::get_in_order_queue())));
95    return ok;
96}
97}
98
99int main() try {
100    int devices = 0;
101    if (DPCT_CHECK_ERROR(devices = dpct::device_count()) != 0 || devices == 0) {
102        std::puts("SKIP: no CUDA device");
103        return 77;
104    }
105    // the active-bound dispatch is Turing's in the engine; forced here so every CUDA card checks it
106#if defined(_WIN32)
107    _putenv_s("STRATA_TOPK_ACTIVE_ANY", "1");
108#else
109    setenv("STRATA_TOPK_ACTIVE_ANY", "1", 1);
110#endif
111    const Case cases[] = {
112        {1111, 256, 0}, {8192, 256, 0}, {32768, 256, 0}, {131072, 256, 0},
113        {131075, 255, 0}, {135164, 256, 0}, {135168, 256, 0}, {250022, 256, 0},
114        {262144, 256, 0}, {131072, 1, 0}, {131072, 8, 0}, {131072, 257, 0},
115        {131072, 256, 1}, {131072, 256, 2}, {131072, 256, 3}, {131072, 256, 4},
116        // either side of the Turing wide-kernel bound (22,528 blocks), and ties above the register limit
117        {90108, 256, 0}, {90112, 256, 0}, {200000, 256, 2}, {262144, 256, 1},
118    };
119    for (const auto c : cases) if (!check(c)) return 1;
120    std::puts("PASS: 20 top-k active-bound cases, selected IDs bitwise identical");
121    return 0;
122}
123catch (sycl::exception const &exc) {
124  std::cerr << exc.what() << "Exception caught at file:" << __FILE__
125            << ", line:" << __LINE__ << std::endl;
126  std::exit(1);
127}