okami.mom/strata

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

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

main
6.5 KiB148 linesraw
1// src/kernels/verify_parity.cpp - batched / split verify-window kernels against the kernels they replace, BITWISE.
2//
3// A verify window's token t must come out bit for bit as it would in a window of any other size (the drafts are
4// accepted exactly when greedy decode would have produced them), so every kernel here is checked against the
5// one it stands in for, on random inputs that include -0.0, denormals, NaN, -inf and large values.  The tests
6// follow eddoursul's fork (MIT; verify_parity.cpp), ported to this tree's kernels.
7#define DPCT_PROFILING_ENABLED
8#include <sycl/sycl.hpp>
9#include <dpct/dpct.hpp>
10#include "strata/kernels/sampler.hpp"
11#include "strata/kernels/verify_kernels.hpp"
12
13#include <algorithm>
14#include <cmath>
15#include <cstdio>
16#include <cstdlib>
17#include <cstring>
18#include <random>
19#include <string>
20#include <vector>
21
22#include <cstdlib>
23#include <cstring>
24#include <random>
25#include <string>
26#include <vector>
27
28namespace {
29
30void check(dpct::err0 e, const char *what) {
31}
32
33template <typename T> T* dev(size_t n) {
34    T* p = nullptr;
35    check(DPCT_CHECK_ERROR(p = (T *)sycl::malloc_device(
36                               n * sizeof(T), dpct::get_in_order_queue())),
37          "cudaMalloc");
38    return p;
39}
40template <typename T> void up(T* d, const std::vector<T>& h) {
41    /*
42    DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming in
43    the original code the source host memory is pageable memory. If the memory
44    is not pageable, call wait() on event return by memcpy API to ensure
45    synchronization behavior.
46    */
47    check(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue()).memcpy(
48              d, h.data(), h.size() * sizeof(T)).wait()),
49          "upload");
50}
51template <typename T> std::vector<T> down(const T* d, size_t n) {
52    std::vector<T> h(n);
53    check(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue())
54                               .memcpy(h.data(), d, n * sizeof(T))
55                               .wait()),
56          "download");
57    return h;
58}
59
60// argmax_rows against sample_tokens' greedy pick and row_top_prob_split against row_top_prob, bitwise: rows of both
61// vocabularies with ties at the maximum (the lowest index wins), NaN, -inf, a row with no value above -inf, and
62// launches repeated on one scratch (each must leave its counters at zero)
63int test_argmax(std::mt19937 &rng, dpct::queue_ptr s) {
64    using namespace strata::kernels;
65    int bad = 0;
66    std::normal_distribution<float> nd(0.0f, 4.0f);
67    uint8_t* sa = dev<uint8_t>(argmax_rows_scratch_bytes(8));   // one scratch for every row count, as in the engine
68    uint8_t* st = dev<uint8_t>(row_top_prob_scratch_bytes(8));
69    check(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue())
70                               .memset(sa, 0, argmax_rows_scratch_bytes(8))
71                               .wait()),
72          "memset");
73    check(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue())
74                               .memset(st, 0, row_top_prob_scratch_bytes(8))
75                               .wait()),
76          "memset");
77    for (const int n : {248320, 40525, 4097, 33}) {
78        for (const int rows : {4, 8, 1, 3, 8, 2}) {
79            std::vector<float> h((size_t) rows * n);
80            for (float& v : h) v = nd(rng);
81            for (int r = 0; r < rows; ++r) {
82                float* row = h.data() + (size_t) r * n;
83                const int a = (int) (rng() % n), b = (int) (rng() % n);
84                row[a] = row[b] = 40.0f;                       // a tie at the maximum
85                row[(int) (rng() % n)] = std::nanf("");        // never picked
86                row[(int) (rng() % n)] = -INFINITY;
87                if (r == 2)                                    // no value above -inf: 0
88                    for (int i = 0; i < n; ++i) row[i] = i % 7 ? -INFINITY : std::nanf("");
89            }
90            float* d_l = dev<float>(h.size());
91            up(d_l, h);
92            int* d_ref = dev<int>(rows);
93            int32_t* d_out = dev<int32_t>(rows);
94            float* d_p1 = dev<float>(rows);
95            float* d_p2 = dev<float>(rows);
96            SamplerParams sp;
97            sp.greedy = true;
98            sp.temperature = 0.0f;
99            sample_tokens(d_l, rows, n, nullptr, 0, sp, d_ref, s);
100            row_top_prob(d_l, rows, n, d_ref, d_p1, s);
101            for (int rep = 0; rep < 3; ++rep) {
102                argmax_rows(d_l, rows, n, sa, d_out, s);
103                row_top_prob_split(d_l, rows, n, d_out, d_p2, st, s);
104                check(DPCT_CHECK_ERROR(s->wait()), "argmax");
105                const std::vector<int> ref = down(d_ref, rows);
106                const std::vector<int32_t> out = down(d_out, rows);
107                const std::vector<float> p1 = down(d_p1, rows), p2 = down(d_p2, rows);
108                for (int r = 0; r < rows; ++r)
109                    if (ref[r] != out[r] || std::memcmp(&p1[r], &p2[r], 4) != 0) {
110                        if (bad < 5)
111                            std::printf("  argmax n %d rows %d row %d: %d / %d, p %.9g / %.9g\n", n, rows, r, ref[r],
112                                        out[r], p1[r], p2[r]);
113                        ++bad;
114                    }
115            }
116            sycl::free(d_l, dpct::get_in_order_queue());
117                sycl::free(d_ref, dpct::get_in_order_queue());
118                sycl::free(d_out, dpct::get_in_order_queue());
119                sycl::free(d_p1, dpct::get_in_order_queue());
120                sycl::free(d_p2, dpct::get_in_order_queue());
121        }
122    }
123    sycl::free(sa, dpct::get_in_order_queue());
124    sycl::free(st, dpct::get_in_order_queue());
125    std::printf("argmax_rows, row_top_prob_split: %s\n",
126                bad ? "MISMATCH" : "bitwise equal to the one-block kernels (ties, NaN, -inf, 1-8 rows on one scratch)");
127    return bad;
128}
129
130}  // namespace
131
132int main(int argc, char** argv) {
133    for (int i = 1; i < argc; ++i) {
134        if (std::string(argv[i]) != "--selftest") {
135            std::fprintf(stderr, "usage: verify_parity [--selftest]\n");
136            return 2;
137        }
138    }
139    std::mt19937 rng(20260925);
140    dpct::queue_ptr s = &dpct::get_in_order_queue();
141    check(DPCT_CHECK_ERROR(s = dpct::get_current_device().create_queue(true)),
142          "stream");
143    int bad = 0;
144    bad += test_argmax(rng, s);
145    dpct::get_current_device().destroy_queue(s);
146    std::printf("verify_parity: %s\n", bad ? "FAIL" : "PASS");
147    return bad ? 1 : 0;
148}