okami.mom/strata

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

Niko1221Merge f-142b-intel into f-1425a90e36cb

main
3.3 KiB61 linesraw
1// Test paired mapped transfers, odd tails, unaligned scalar fallback, and
2// the checksum contracts of all three publish variants over repeated rings.
3#include <sycl/sycl.hpp>
4#include <dpct/dpct.hpp>
5#include "strata/sycl_queue.hpp"
6#include "strata/kernels/elementwise.hpp"
7#include <cstdio>
8#include <cstring>
9#include <vector>
10
11int main() {
12    auto& q = *strata::q_of(nullptr);
13    using namespace strata::kernels;
14    constexpr int capacity = 2560, k = 10, experts = 64;
15    auto* hx = (float*)strata::host_malloc_polled((capacity + 4)*sizeof(float), q);
16    auto* hi = (int32_t*)strata::host_malloc_polled(k*sizeof(int32_t), q);
17    auto* hw = (float*)strata::host_malloc_polled(k*sizeof(float), q);
18    auto* seq = (uint32_t*)strata::host_malloc_polled(64, q);
19    auto* dx = sycl::malloc_device<float>(capacity, q);
20    auto* di = sycl::malloc_device<int32_t>(k, q);
21    auto* dw = sycl::malloc_device<float>(k, q);
22    auto* dr = sycl::malloc_device<int32_t>(experts, q);
23    std::vector<float> x(capacity), weights(k), copied(capacity);
24    std::vector<int32_t> ids(k);
25    for (int j = 0; j < capacity; ++j) x[j] = (j % 123 - 61)*0.25f;
26    for (int j = 0; j < k; ++j) { ids[j] = j*3; weights[j] = (j + 1)*0.03125f; }
27    q.memcpy(dx, x.data(), x.size()*sizeof(float));
28    q.memcpy(di, ids.data(), k*sizeof(int32_t));
29    q.memcpy(dw, weights.data(), k*sizeof(float));
30    q.memset(dr, 0, experts*sizeof(int32_t));
31    q.wait_and_throw();
32    int failed = 0;
33    for (int n : {1, 3, 17, 2559, 2560})
34        for (int offset : {0, 1})
35            for (int variant = 0; variant < 3; ++variant) {
36                std::memset(seq, 0, 64);
37                for (uint32_t ring = 1; ring <= 12; ++ring) {
38                    std::memset(hx, 0x7f, (capacity + 4)*sizeof(float));
39                    std::memset(hi, 0, k*sizeof(int32_t));
40                    std::memset(hw, 0, k*sizeof(float));
41                    if (variant == 0) doorbell_publish(dx, di, dw, n, k, hx+offset, hi, hw, seq, &q);
42                    else if (variant == 1) doorbell_publish_res(dx, di, dr, experts, n, k, hx+offset, hi, seq, &q);
43                    else doorbell_publish_value(dx, di, dw, n, k, hx+offset, hi, hw, seq, ring, &q);
44                    q.wait_and_throw();
45                    bool ok = seq[0] == ring && !std::memcmp(hx+offset, x.data(), n*sizeof(float)) &&
46                        !std::memcmp(hi, ids.data(), k*sizeof(int32_t)) &&
47                        doorbell_payload_ready(seq, hx+offset, n, hi, hw, k, ring);
48                    if (variant != 1) ok = ok && !std::memcmp(hw, weights.data(), k*sizeof(float));
49                    if (!ok) { ++failed; std::printf("FAIL n=%d offset=%d variant=%d ring=%u\n", n, offset, variant, ring); }
50                }
51            }
52    std::memcpy(hx, x.data(), x.size()*sizeof(float));
53    copy_from_mapped(dx, hx, capacity, &q);
54    q.memcpy(copied.data(), dx, capacity*sizeof(float)).wait();
55    if (std::memcmp(copied.data(), x.data(), capacity*sizeof(float))) ++failed;
56    for (void* p : {static_cast<void*>(hx), static_cast<void*>(hi), static_cast<void*>(hw), static_cast<void*>(seq)})
57        strata::host_free_polled(p, q);
58    for (void* p : {static_cast<void*>(dx), static_cast<void*>(di), static_cast<void*>(dw), static_cast<void*>(dr)}) sycl::free(p, q);
59    std::printf("paired payload and mapped read: %d failures\n", failed);
60    return failed != 0;
61}