okami.mom/strata

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

Niko1221Merge branch 'f-141-intel' into f-14028f70a52cf

main
5.1 KiB105 linesraw
1// fp64_cost: what does FP64 (emulated on Arc Alchemist and Battlemage) cost in the engine's small reduction kernels?
2// Same one-warp-per-row sum of squares + 1/sqrt as gdn_l2_norm / gdn_out_norm / the PLE rms norm, once with a double
3// accumulator (the engine today) and once in float, plus a silu over n values the way silu_inplace does it.
4//   fp64_cost [launches=4000]     (the A750 needs IGC_EnableDPEmulation=1 OverrideDefaultFP64Settings=1 for the double kernels)
5#include <sycl/sycl.hpp>
6#include <chrono>
7#include <cmath>
8#include <cstdio>
9#include <cstdlib>
10#include <vector>
11
12template <class F>
13static double us_per_launch(sycl::queue& q, int n, F&& f) {
14    for (int i = 0; i < 50; ++i) f();
15    q.wait();
16    const auto t0 = std::chrono::steady_clock::now();
17    for (int i = 0; i < n; ++i) f();
18    q.wait();
19    return std::chrono::duration<double, std::micro>(std::chrono::steady_clock::now() - t0).count() / n;
20}
21
22template <class Acc>
23static void l2norm(sycl::queue& q, float* x, int rows, int cols, float eps) {
24    q.parallel_for(sycl::nd_range<1>(rows * 32, 32), [=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(32)]] {
25        const int row = it.get_group(0);
26        float* p = x + (size_t) row * cols;
27        auto sg = it.get_sub_group();
28        Acc acc = 0;
29        for (int i = it.get_local_id(0); i < cols; i += 32) acc += (Acc) p[i] * (Acc) p[i];
30        for (int off = 16; off > 0; off >>= 1) acc += sycl::shift_group_left(sg, acc, off);
31        acc = sycl::group_broadcast(sg, acc, 0);
32        const float inv = (float) ((Acc) 1 / sycl::sqrt(acc + (Acc) eps));
33        for (int i = it.get_local_id(0); i < cols; i += 32) p[i] *= inv;
34    });
35}
36
37template <class Acc>
38static void silu(sycl::queue& q, float* x, int n) {
39    q.parallel_for(sycl::nd_range<1>((n + 255) / 256 * 256, 256), [=](sycl::nd_item<1> it) {
40        const int i = it.get_global_id(0);
41        if (i >= n) return;
42        const Acc v = (Acc) x[i];
43        x[i] = (float) (v / ((Acc) 1 + sycl::exp(-v)));
44    });
45}
46
47
48// the sampler's tail: top_p cut and temperature draw over k kept logits, one warp (like sampled_tail_warp), 3 exps per element
49template <class Acc>
50static void sampler_tail(sycl::queue& q, const float* sel, int* out, int k, float top_p, float u) {
51    q.parallel_for(sycl::nd_range<1>(32, 32), [=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(32)]] {
52        const int lane = it.get_local_id(0);
53        Acc sum = 0;
54        float mx = sel[0];
55        for (int i = lane; i < k; i += 32) sum += sycl::exp((Acc) sel[i] - (Acc) mx);
56        sum = sycl::reduce_over_group(it.get_sub_group(), sum, sycl::plus<Acc>());
57        Acc cum = 0;
58        int cut = k;
59        if (lane == 0) {
60            for (int i = 0; i < k; ++i) { cum += sycl::exp((Acc) sel[i] - (Acc) mx) / sum; if (cum >= (Acc) top_p) { cut = i + 1; break; } }
61            Acc c2 = 0; int pick = cut - 1;
62            for (int i = 0; i < cut; ++i) { c2 += sycl::exp((Acc) sel[i] - (Acc) mx) / sum; if ((Acc) u < c2) { pick = i; break; } }
63            out[0] = pick;
64        }
65    });
66}
67
68int main(int argc, char** argv) {
69    const int n = argc > 1 ? std::atoi(argv[1]) : 4000;
70    sycl::queue q{sycl::gpu_selector_v, sycl::property::queue::in_order{}};
71    std::printf("device %s, fp64 %s\n", q.get_device().get_info<sycl::info::device::name>().c_str(),
72                q.get_device().has(sycl::aspect::fp64) ? "yes" : "NO (double kernels skipped)");
73    const bool fp64 = q.get_device().has(sycl::aspect::fp64);
74    float* x = sycl::malloc_device<float>(1 << 20, q);
75    q.fill(x, 0.5f, 1 << 20).wait();
76    struct Shape { const char* what; int rows, cols; };
77    const Shape shapes[] = {{"gdn l2 norm (rows 48, cols 128)", 48, 128}, {"gdn out norm (rows 48, cols 128)", 48, 128},
78                            {"ple rms (rows 8, cols 2560)", 8, 2560}, {"wide (rows 6, cols 2560)", 6, 2560}};
79    for (const Shape& s : shapes) {
80        const double f = us_per_launch(q, n, [&] { l2norm<float>(q, x, s.rows, s.cols, 1e-6f); });
81        double d = -1;
82        if (fp64) d = us_per_launch(q, n, [&] { l2norm<double>(q, x, s.rows, s.cols, 1e-6f); });
83        std::printf("%-34s float %6.2f us   double %6.2f us\n", s.what, f, d);
84    }
85    for (int cnt : {2560, 10240, 61440}) {
86        const double f = us_per_launch(q, n, [&] { silu<float>(q, x, cnt); });
87        double d = -1;
88        if (fp64) d = us_per_launch(q, n, [&] { silu<double>(q, x, cnt); });
89        std::printf("silu over %-24d float %6.2f us   double %6.2f us\n", cnt, f, d);
90    }
91    {
92        float* sel = sycl::malloc_device<float>(64, q);
93        int* o = sycl::malloc_device<int>(1, q);
94        std::vector<float> h(64);
95        for (int i = 0; i < 64; ++i) h[i] = 10.f - 0.3f * i;
96        q.memcpy(sel, h.data(), 64 * 4).wait();
97        for (int k : {20, 64}) {
98            const double f = us_per_launch(q, n, [&] { sampler_tail<float>(q, sel, o, k, 0.95f, 0.5f); });
99            double d = -1;
100            if (fp64) d = us_per_launch(q, n, [&] { sampler_tail<double>(q, sel, o, k, 0.95f, 0.5f); });
101            std::printf("sampler tail, k=%-21d float %6.2f us   double %6.2f us\n", k, f, d);
102        }
103    }
104    return 0;
105}