okami.mom/strata

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

Niko1221Trial2 merge f-0291-pr-intel (2b4d43bac)5438023a6

main
9.8 KiB201 linesraw
1// src/kernels/fused_gr_bench.cpp - fused_gr_read_multi: the AMD fast kernels (STRATA_GR_FAST) against the old
2// ones, bit for bit on every output (lo, rs, inject, mixed, the in-place R), and each one's time.
3//
4//     build/fused_gr_bench [iters] [T min] [T max]
5// On SYCL the fast setter is a no-op; compare hashes across builds or environment settings.
6#define DPCT_PROFILING_ENABLED
7#include <sycl/sycl.hpp>
8#include <dpct/dpct.hpp>
9#include "strata/kernels/fused_gr.hpp"
10
11#include <cmath>
12#include <cstdint>
13#include <cstdio>
14#include <cstdlib>
15#include <cstring>
16#include <random>
17#include <vector>
18
19namespace K = strata::kernels;
20
21static uint16_t bf16(float x) {
22    uint32_t u;
23    std::memcpy(&u, &x, 4);
24    return (uint16_t) ((u + 0x7fff + ((u >> 16) & 1)) >> 16);
25}
26
27int main(int argc, char** argv) {
28    setvbuf(stdout, nullptr, _IONBF, 0);
29    const int iters = argc > 1 ? std::atoi(argv[1]) : 500;
30    const int t_lo = argc > 2 ? std::atoi(argv[2]) : 1, t_hi = argc > 3 ? std::atoi(argv[3]) : 6;
31    const int N = 2560, HC = 4, D = N * HC, LR = 320, TM = K::kFusedGrMaxT;
32    std::mt19937 rng(7);
33    std::normal_distribution<float> nd(0.f, 1.f);
34    auto fill = [&](std::vector<float>& v, float sc) { for (auto& x : v) x = sc * nd(rng); };
35    std::vector<float> R((size_t) TM * D), bo((size_t) TM * N), inj((size_t) TM * HC), wn(D);
36    fill(R, 1.0f); fill(bo, 0.5f); fill(inj, 1.0f); fill(wn, 0.3f);
37    for (auto& x : wn) x += 1.0f;
38    std::vector<uint16_t> wd((size_t) LR * D), wu((size_t) D * LR), wi((size_t) HC * D);
39    for (auto& x : wd) x = bf16(0.02f * nd(rng));
40    for (auto& x : wu) x = bf16(0.05f * nd(rng));
41    for (auto& x : wi) x = bf16(0.02f * nd(rng));
42    float *dR, *dbo, *dinj, *dwn, *dxn, *dlo, *drs, *dio, *dmix;
43    uint16_t *dwd, *dwu, *dwi;
44    dR = (float *)sycl::malloc_device(R.size() * 4, dpct::get_in_order_queue());
45    dbo =
46        (float *)sycl::malloc_device(bo.size() * 4, dpct::get_in_order_queue());
47    dinj = (float *)sycl::malloc_device(inj.size() * 4,
48                                        dpct::get_in_order_queue());
49    dwn =
50        (float *)sycl::malloc_device(wn.size() * 4, dpct::get_in_order_queue());
51    dxn = (float *)sycl::malloc_device((size_t)TM * D * 4,
52                                       dpct::get_in_order_queue());
53    dlo = (float *)sycl::malloc_device((size_t)TM * LR * 4,
54                                       dpct::get_in_order_queue());
55    drs = (float *)sycl::malloc_device((size_t)TM * HC * 4,
56                                       dpct::get_in_order_queue());
57    dio = (float *)sycl::malloc_device((size_t)TM * HC * 4,
58                                       dpct::get_in_order_queue());
59    dmix = (float *)sycl::malloc_device((size_t)TM * N * 4,
60                                        dpct::get_in_order_queue());
61    dwd = (uint16_t *)sycl::malloc_device(wd.size() * 2,
62                                          dpct::get_in_order_queue());
63    dwu = (uint16_t *)sycl::malloc_device(wu.size() * 2,
64                                          dpct::get_in_order_queue());
65    dwi = (uint16_t *)sycl::malloc_device(wi.size() * 2,
66                                          dpct::get_in_order_queue());
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    dpct::get_in_order_queue().memcpy(dbo, bo.data(), bo.size() * 4).wait();
74    /*
75    DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming
76    in the original code the source host memory is pageable memory. If the
77    memory is not pageable, call wait() on event return by memcpy API to ensure
78    synchronization behavior.
79    */
80    dpct::get_in_order_queue().memcpy(dinj, inj.data(), inj.size() * 4).wait();
81    /*
82    DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming
83    in the original code the source host memory is pageable memory. If the
84    memory is not pageable, call wait() on event return by memcpy API to ensure
85    synchronization behavior.
86    */
87    dpct::get_in_order_queue().memcpy(dwn, wn.data(), wn.size() * 4).wait();
88    /*
89    DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming
90    in the original code the source host memory is pageable memory. If the
91    memory is not pageable, call wait() on event return by memcpy API to ensure
92    synchronization behavior.
93    */
94    dpct::get_in_order_queue().memcpy(dwd, wd.data(), wd.size() * 2).wait();
95    /*
96    DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming
97    in the original code the source host memory is pageable memory. If the
98    memory is not pageable, call wait() on event return by memcpy API to ensure
99    synchronization behavior.
100    */
101    dpct::get_in_order_queue().memcpy(dwu, wu.data(), wu.size() * 2).wait();
102    /*
103    DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming
104    in the original code the source host memory is pageable memory. If the
105    memory is not pageable, call wait() on event return by memcpy API to ensure
106    synchronization behavior.
107    */
108    dpct::get_in_order_queue().memcpy(dwi, wi.data(), wi.size() * 2).wait();
109    dpct::queue_ptr s;
110    s = dpct::get_current_device().create_queue(true);
111    dpct::event_ptr e0, e1;
112    e0 = new sycl::event();
113    e1 = new sycl::event();
114    int failures = 0;
115    for (int T = t_lo; T <= t_hi; ++T)
116        for (int apply = 0; apply < 2; ++apply)
117            for (int inject = 0; inject < 2; ++inject) {
118                K::FusedGrArgs a[TM];
119                for (int t = 0; t < T; ++t) {
120                    a[t].R = dR + (size_t) t * D; a[t].R_out = dR + (size_t) t * D; a[t].apply = apply;
121                    a[t].bo_prev = dbo + (size_t) t * N; a[t].inj_prev = dinj + (size_t) t * HC;
122                    a[t].w_norm = dwn; a[t].w_down = dwd; a[t].w_up = dwu; a[t].w_inject = inject ? dwi : nullptr;
123                    a[t].lo = dlo + (size_t) t * LR; a[t].rs = drs + (size_t) t * HC;
124                    a[t].inject_out = dio + (size_t) t * HC; a[t].mixed = dmix + (size_t) t * N;
125                }
126                std::vector<float> out[2];
127                for (int f = 0; f < 2; ++f) {
128                    dpct::get_in_order_queue()
129                        .memcpy(dR, R.data(), R.size() * 4)
130                        .wait();
131                    dpct::get_in_order_queue()
132                        .memset(dlo, 0xff, (size_t)TM * LR * 4)
133                        .wait();
134                    dpct::get_in_order_queue()
135                        .memset(drs, 0xff, (size_t)TM * HC * 4)
136                        .wait();
137                    dpct::get_in_order_queue()
138                        .memset(dio, 0xff, (size_t)TM * HC * 4)
139                        .wait();
140                    dpct::get_in_order_queue()
141                        .memset(dmix, 0xff, (size_t)TM * N * 4)
142                        .wait();
143                    K::fused_gr_set_fast(f);
144                    K::fused_gr_read_multi(a, T, dxn, s);
145                    s->wait();
146                    auto& o = out[f];
147                    o.resize((size_t) TM * (D + LR + HC + HC + N));
148                    float* p = o.data();
149                    dpct::get_in_order_queue()
150                        .memcpy(p, dR, (size_t)TM * D * 4)
151                        .wait();
152                        p += (size_t)TM * D;
153                    dpct::get_in_order_queue()
154                        .memcpy(p, dlo, (size_t)TM * LR * 4)
155                        .wait();
156                        p += (size_t)TM * LR;
157                    dpct::get_in_order_queue()
158                        .memcpy(p, drs, (size_t)TM * HC * 4)
159                        .wait();
160                        p += (size_t)TM * HC;
161                    dpct::get_in_order_queue()
162                        .memcpy(p, dio, (size_t)TM * HC * 4)
163                        .wait();
164                        p += (size_t)TM * HC;
165                    dpct::get_in_order_queue()
166                        .memcpy(p, dmix, (size_t)TM * N * 4)
167                        .wait();
168                }
169                const bool same = std::memcmp(out[0].data(), out[1].data(), out[0].size() * 4) == 0;
170                double us[2] = {1e30, 1e30};
171                for (int round = 0; round < 3; ++round)
172                    for (int f = 0; f < 2; ++f) {
173                        K::fused_gr_set_fast(f);
174                        for (int i = 0; i < 20; ++i) K::fused_gr_read_multi(a, T, dxn, s);
175                        dpct::sync_barrier(e0, s);
176                        for (int i = 0; i < iters; ++i) K::fused_gr_read_multi(a, T, dxn, s);
177                        dpct::sync_barrier(e1, s);
178                        e1->wait_and_throw();
179                        float ms = 0;
180                        ms = (e1->get_profiling_info<
181                                  sycl::info::event_profiling::command_end>() -
182                              e0->get_profiling_info<
183                                  sycl::info::event_profiling::
184                                      command_start>()) /
185                             1000000.0f;
186                        us[f] = std::fmin(us[f], 1e3 * ms / iters);
187                    }
188                uint64_t hash = 1469598103934665603ull;
189                for (float value : out[1]) {
190                    uint32_t bits;
191                    std::memcpy(&bits, &value, sizeof(bits));
192                    hash = (hash ^ bits) * 1099511628211ull;
193                }
194                std::printf("T %d apply %d inject %d | old %6.1f us  fast %6.1f us | %s | hash %016llx\n",
195                            T, apply, inject, us[0], us[1], same ? "bitwise equal" : "DIFFERS",
196                            (unsigned long long) hash);
197                if (!same) ++failures;
198            }
199    std::printf("fused_gr_bench: %d differing\n", failures);
200    return failures ? 1 : 0;
201}