okami.mom/strata

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

Niko1221Merge f-sycl-prs e106b4c86 into the batch 16 candidate (SYCL pull requests 4185 and 3153)a8a51b280

main
3.6 KiB92 linesraw
1#include "strata/prefill/gemm.hpp"
2#include "strata/sycl_queue.hpp"
3
4#include <bit>
5#include <cmath>
6#include <cstdint>
7#include <cstdio>
8#include <cstring>
9#include <limits>
10#include <string>
11#include <vector>
12
13namespace {
14constexpr int64_t T = 3, N = 5, K = 32, LDY = N + 3;
15constexpr float guard = -123.0f;
16
17uint16_t bf16(float value) {
18    return uint16_t(std::bit_cast<uint32_t>(value) >> 16);
19}
20
21bool run(bool native, int64_t ldx, float beta, int64_t scratch_rows) {
22    auto& queue = dpct::get_in_order_queue();
23    strata::prefill::Gemm gemm;
24    std::string error;
25    if (!gemm.init(&queue, scratch_rows * K, error)) {
26        std::fprintf(stderr, "%s\n", error.c_str());
27        return false;
28    }
29    std::vector<uint16_t> x(size_t(T * ldx), native ? 0x7e00 : 0x7fc0);
30    std::vector<uint16_t> weight(size_t(N * K));
31    std::vector<uint8_t> blocks(size_t(N * 34));
32    const uint16_t half_x[T] = {0x3800, 0x3c00, 0x3e00};
33    for (int64_t t = 0; t < T; ++t)
34        for (int64_t k = 0; k < K; ++k)
35            x[size_t(t * ldx + k)] = native ? half_x[t] : bf16(float(t + 1) / 2);
36    for (int64_t n = 0; n < N; ++n) {
37        for (int64_t k = 0; k < K; ++k) {
38            weight[size_t(n * K + k)] = bf16(float(n + 1) / 4);
39            blocks[size_t(n * 34 + 2 + k)] = uint8_t(n + 1);
40        }
41        const uint16_t scale = 0x2800;  // FP16 1/32, exactly representable.
42        std::memcpy(blocks.data() + n * 34, &scale, sizeof(scale));
43    }
44    std::vector<float> y(size_t(T * LDY), guard);
45    for (int64_t t = 0; t < T; ++t)
46        for (int64_t n = 0; n < N; ++n) y[size_t(t * LDY + n)] = 0.25f;
47    auto* dx = sycl::malloc_device<uint16_t>(x.size(), queue);
48    auto* dw = sycl::malloc_device<uint16_t>(weight.size(), queue);
49    auto* db = sycl::malloc_device<uint8_t>(blocks.size(), queue);
50    auto* dy = sycl::malloc_device<float>(y.size(), queue);
51    queue.memcpy(dx, x.data(), x.size() * sizeof(uint16_t));
52    queue.memcpy(dw, weight.data(), weight.size() * sizeof(uint16_t));
53    queue.memcpy(db, blocks.data(), blocks.size());
54    queue.memcpy(dy, y.data(), y.size() * sizeof(float)).wait();
55    if (native) gemm.native(dx, 8 /* GGML_TYPE_Q8_0 */, db, dy, T, N, K, LDY, beta, ldx);
56    else gemm.bf16(dx, dw, dy, T, N, K, LDY, beta, ldx);
57    queue.memcpy(y.data(), dy, y.size() * sizeof(float)).wait();
58    bool good = true;
59    for (int64_t t = 0; t < T; ++t)
60        for (int64_t n = 0; n < LDY; ++n) {
61            const float expected = n < N
62                ? float(t + 1) / 2 * float(n + 1) * (native ? 1.0f : 8.0f) + beta * 0.25f
63                : guard;
64            const float actual = y[size_t(t * LDY + n)];
65            if (!std::isfinite(actual) || std::abs(actual - expected) > 1e-5f) {
66                std::fprintf(stderr, "%s stride=%lld beta=%.0f scratch=%lld [%lld,%lld]: %.9g != %.9g\n",
67                             native ? "native" : "bf16", (long long)ldx, beta, (long long)scratch_rows,
68                             (long long)t, (long long)n, actual, expected);
69                good = false;
70            }
71        }
72    sycl::free(dx, queue);
73    sycl::free(dw, queue);
74    sycl::free(db, queue);
75    sycl::free(dy, queue);
76    return good;
77}
78}  // namespace
79
80int main() try {
81    for (int64_t stride : {K, K + 8})
82        for (float beta : {0.0f, 1.0f}) {
83            if (!run(false, stride, beta, N)) return 1;
84            for (int64_t scratch_rows : {N, int64_t(2)})
85                if (!run(true, stride, beta, scratch_rows)) return 1;
86        }
87    std::puts("GEMM tight/padded input, padded output, beta 0/1, and native row slices: PASS");
88    return 0;
89} catch (const std::exception& error) {
90    std::fprintf(stderr, "%s\n", error.what());
91    return 1;
92}