okami.mom/strata

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

Niko1221sycl/src/kernels/xmx_int8_bench.cpp: DPAS int8 vs FP16 expert GEMM microbench (standalone, not in the build)24efc6e33

main
14.7 KiB226 linesraw
1// xmx_int8_bench (0.1.44): what a DPAS int8 expert GEMM costs next to the FP16 joint_matrix one, on one device.
2//   xmx_int8_bench [M=160] [experts=64] [reps=20]      build: -DSTRATA_XMX_SG16 for Xe2 (8x16x32), none for DG2 (8x8x32)
3// Shape of a Strata expert: gate+up N=1280, K=2560 (down: N=2560, K=640).  Modes:
4//   f16    : half A (M x K) and half W (N x K), float accumulate - the GEMM the current dequant + oneMKL path runs
5//   i8     : int8 A and W, int32 accumulate over the whole K (no per-block scale): the DPAS floor
6//   i8blk  : int8 A and W, one DPAS per 32 of K into a zeroed int32 tile, then acc += float(tile) * dw[n] * dx[m][kb]
7//            (the per-32 scales of the activations and of the IQ weights): the real fused-kernel epilogue
8// Weights are random int8 (decode cost excluded; a real kernel adds the IQ grid decode).  E different experts, so the
9// weights come from DRAM, not L2.  Prints us per expert and the effective TOPS; checks i8blk against a host reference.
10#include <sycl/sycl.hpp>
11#include <sycl/ext/oneapi/matrix/matrix.hpp>
12#include <sycl/ext/intel/experimental/grf_size_properties.hpp>
13#include <chrono>
14#include <cmath>
15#include <cstdio>
16#include <cstdlib>
17#include <random>
18#include <vector>
19
20namespace jm = sycl::ext::oneapi::experimental::matrix;
21using half = sycl::half;
22#ifdef STRATA_XMX_SG16
23constexpr int SGW = 16;
24#else
25constexpr int SGW = 8;
26#endif
27constexpr int SGM = 2, SGN = 4, MT = 4, NT = 2;          // 8 sub-groups: tile 64 x (4*2*SGW)
28constexpr int BM = SGM * MT * 8, BN = SGN * NT * SGW, NSG = SGM * SGN, WG = NSG * SGW;
29
30// mode 0: f16
31template <class T>
32struct Slm;
33void run_f16(sycl::queue& q, const half* A, const half* W, float* C, int M, int N, int K, int E) {
34    constexpr int KS = 32, LD = KS + 8;
35    const int nmt = (M + BM - 1) / BM, nnt = N / BN;
36    q.submit([&](sycl::handler& h) {
37        sycl::local_accessor<half, 1> sa(BM * LD, h), sb(BN * LD, h);
38        auto props = sycl::ext::oneapi::experimental::properties{sycl::ext::intel::experimental::grf_size<256>};
39        h.parallel_for<class k_f16>(sycl::nd_range<3>({(size_t) E, (size_t) nmt, (size_t) nnt * WG}, {1, 1, (size_t) WG}), props,
40                                    [=](sycl::nd_item<3> it) [[sycl::reqd_sub_group_size(SGW)]] {
41            auto sg = it.get_sub_group();
42            const int e = (int) it.get_group(0), m0 = (int) it.get_group(1) * BM, n0 = (int) it.get_group(2) * BN;
43            const int lid = (int) it.get_local_id(2), sgid = lid / SGW, sgm = sgid / SGN, sgn = sgid % SGN;
44            const half* We = W + (size_t) e * N * K;
45            jm::joint_matrix<sycl::sub_group, float, jm::use::accumulator, 8, SGW> acc[MT][NT];
46            for (int i = 0; i < MT; ++i) for (int j = 0; j < NT; ++j) jm::joint_matrix_fill(sg, acc[i][j], 0.0f);
47            half* pa = sa.template get_multi_ptr<sycl::access::decorated::no>().get();
48            half* pb = sb.template get_multi_ptr<sycl::access::decorated::no>().get();
49            for (int k0 = 0; k0 < K; k0 += KS) {
50                for (int v = lid; v < BM * KS / 8; v += WG) {
51                    const int r = v / (KS / 8), c = (v % (KS / 8)) * 8;
52                    sycl::vec<half, 8> x(0);
53                    if (m0 + r < M) x = *reinterpret_cast<const sycl::vec<half, 8>*>(A + (size_t) (m0 + r) * K + k0 + c);
54                    *reinterpret_cast<sycl::vec<half, 8>*>(pa + r * LD + c) = x;
55                }
56                for (int v = lid; v < BN * KS / 8; v += WG) {
57                    const int r = v / (KS / 8), c = (v % (KS / 8)) * 8;
58                    *reinterpret_cast<sycl::vec<half, 8>*>(pb + r * LD + c) =
59                        *reinterpret_cast<const sycl::vec<half, 8>*>(We + (size_t) (n0 + r) * K + k0 + c);
60                }
61                it.barrier(sycl::access::fence_space::local_space);
62                for (int kk = 0; kk < KS; kk += 16) {
63                    jm::joint_matrix<sycl::sub_group, half, jm::use::a, 8, 16, jm::layout::row_major> a[MT];
64                    jm::joint_matrix<sycl::sub_group, half, jm::use::b, 16, SGW, jm::layout::col_major> b[NT];
65                    for (int i = 0; i < MT; ++i)
66                        jm::joint_matrix_load(sg, a[i], sa.template get_multi_ptr<sycl::access::decorated::no>() + (sgm * MT * 8 + i * 8) * LD + kk, LD);
67                    for (int j = 0; j < NT; ++j)
68                        jm::joint_matrix_load(sg, b[j], sb.template get_multi_ptr<sycl::access::decorated::no>() + (sgn * NT * SGW + j * SGW) * LD + kk, LD);
69                    for (int i = 0; i < MT; ++i) for (int j = 0; j < NT; ++j) jm::joint_matrix_mad(sg, acc[i][j], a[i], b[j], acc[i][j]);
70                }
71                it.barrier(sycl::access::fence_space::local_space);
72            }
73            for (int i = 0; i < MT; ++i) {
74                const int r = m0 + sgm * MT * 8 + i * 8;
75                if (r + 8 > M) continue;   // the bench uses M a multiple of 8; rows beyond are zero-padded work only
76                for (int j = 0; j < NT; ++j)
77                    jm::joint_matrix_store(sg, acc[i][j], sycl::address_space_cast<sycl::access::address_space::global_space, sycl::access::decorated::no>(
78                        C + ((size_t) e * M + r) * N + n0 + sgn * NT * SGW + j * SGW), N, jm::layout::row_major);
79            }
80        });
81    });
82}
83
84// mode 1/2: int8.  BLK: per-32 float epilogue (dw: [E][N][K/32] float, dx: [M][K/32] float)
85constexpr int KI = 32;   // joint_matrix int8 K
86template <bool BLK, class Name>
87void run_i8(sycl::queue& q, const int8_t* A, const int8_t* W, const float* dw, const float* dx, float* C, int M, int N, int K, int E) {
88    constexpr int KS = 64, LD = KS + 16;   // bytes
89    const int nmt = (M + BM - 1) / BM, nnt = N / BN, nkb = K / 32;
90    q.submit([&](sycl::handler& h) {
91        sycl::local_accessor<int8_t, 1> sa(BM * LD, h), sb(BN * LD, h);
92        sycl::local_accessor<float, 1> sdw(BN * 2, h), sdx(BM * 2, h);
93        auto props = sycl::ext::oneapi::experimental::properties{sycl::ext::intel::experimental::grf_size<256>};
94        h.parallel_for<Name>(sycl::nd_range<3>({(size_t) E, (size_t) nmt, (size_t) nnt * WG}, {1, 1, (size_t) WG}), props,
95                             [=](sycl::nd_item<3> it) [[sycl::reqd_sub_group_size(SGW)]] {
96            auto sg = it.get_sub_group();
97            const int e = (int) it.get_group(0), m0 = (int) it.get_group(1) * BM, n0 = (int) it.get_group(2) * BN;
98            const int lid = (int) it.get_local_id(2), sgid = lid / SGW, sgm = sgid / SGN, sgn = sgid % SGN;
99            const int8_t* We = W + (size_t) e * N * K;
100            jm::joint_matrix<sycl::sub_group, float, jm::use::accumulator, 8, SGW> acc[MT][NT];
101            jm::joint_matrix<sycl::sub_group, int32_t, jm::use::accumulator, 8, SGW> tmp[MT][NT];
102            for (int i = 0; i < MT; ++i) for (int j = 0; j < NT; ++j) {
103                jm::joint_matrix_fill(sg, acc[i][j], 0.0f);
104                jm::joint_matrix_fill(sg, tmp[i][j], 0);
105            }
106            int8_t* pa = sa.template get_multi_ptr<sycl::access::decorated::no>().get();
107            int8_t* pb = sb.template get_multi_ptr<sycl::access::decorated::no>().get();
108            float* pdw = sdw.template get_multi_ptr<sycl::access::decorated::no>().get();
109            float* pdx = sdx.template get_multi_ptr<sycl::access::decorated::no>().get();
110            for (int k0 = 0; k0 < K; k0 += KS) {
111                for (int v = lid; v < BM * KS / 16; v += WG) {
112                    const int r = v / (KS / 16), c = (v % (KS / 16)) * 16;
113                    sycl::vec<int8_t, 16> x(0);
114                    if (m0 + r < M) x = *reinterpret_cast<const sycl::vec<int8_t, 16>*>(A + (size_t) (m0 + r) * K + k0 + c);
115                    *reinterpret_cast<sycl::vec<int8_t, 16>*>(pa + r * LD + c) = x;
116                }
117                for (int v = lid; v < BN * KS / 16; v += WG) {
118                    const int r = v / (KS / 16), c = (v % (KS / 16)) * 16;
119                    *reinterpret_cast<sycl::vec<int8_t, 16>*>(pb + r * LD + c) =
120                        *reinterpret_cast<const sycl::vec<int8_t, 16>*>(We + (size_t) (n0 + r) * K + k0 + c);
121                }
122                if (BLK) {
123                    for (int v = lid; v < BN * 2; v += WG) pdw[v] = dw[((size_t) e * N + n0 + v / 2) * nkb + k0 / 32 + v % 2];
124                    for (int v = lid; v < BM * 2; v += WG) pdx[v] = (m0 + v / 2 < M) ? dx[(size_t) (m0 + v / 2) * nkb + k0 / 32 + v % 2] : 0.f;
125                }
126                it.barrier(sycl::access::fence_space::local_space);
127                for (int kk = 0; kk < KS; kk += KI) {
128                    jm::joint_matrix<sycl::sub_group, int8_t, jm::use::a, 8, KI, jm::layout::row_major> a[MT];
129                    jm::joint_matrix<sycl::sub_group, int8_t, jm::use::b, KI, SGW, jm::layout::col_major> b[NT];
130                    for (int i = 0; i < MT; ++i)
131                        jm::joint_matrix_load(sg, a[i], sa.template get_multi_ptr<sycl::access::decorated::no>() + (sgm * MT * 8 + i * 8) * LD + kk, LD);
132                    for (int j = 0; j < NT; ++j)
133                        jm::joint_matrix_load(sg, b[j], sb.template get_multi_ptr<sycl::access::decorated::no>() + (sgn * NT * SGW + j * SGW) * LD + kk, LD);
134                    if (BLK) {
135                        for (int i = 0; i < MT; ++i) for (int j = 0; j < NT; ++j) {
136                            jm::joint_matrix_fill(sg, tmp[i][j], 0);
137                            jm::joint_matrix_mad(sg, tmp[i][j], a[i], b[j], tmp[i][j]);
138                            const float sw = pdw[(sgn * NT * SGW + j * SGW + (int) sg.get_local_linear_id()) * 2 + kk / 32];
139                            const int rbase = sgm * MT * 8 + i * 8;
140                            int ei = 0;
141                            jm::joint_matrix_apply(sg, tmp[i][j], acc[i][j], [&](int32_t& t, float& ac) {
142                                ac = sycl::fma((float) t * sw, pdx[(rbase + ei) * 2 + kk / 32], ac);
143                                ++ei;
144                            });
145                        }
146                    } else {
147                        for (int i = 0; i < MT; ++i) for (int j = 0; j < NT; ++j) jm::joint_matrix_mad(sg, tmp[i][j], a[i], b[j], tmp[i][j]);
148                    }
149                }
150                it.barrier(sycl::access::fence_space::local_space);
151            }
152            for (int i = 0; i < MT; ++i) {
153                const int r = m0 + sgm * MT * 8 + i * 8;
154                if (r + 8 > M) continue;
155                for (int j = 0; j < NT; ++j) {
156                    if (!BLK) {   // int32 -> float through a per-element copy
157                        jm::joint_matrix_apply(sg, tmp[i][j], acc[i][j], [&](int32_t& t, float& ac) { ac = (float) t; });
158                    }
159                    jm::joint_matrix_store(sg, acc[i][j], sycl::address_space_cast<sycl::access::address_space::global_space, sycl::access::decorated::no>(
160                        C + ((size_t) e * M + r) * N + n0 + sgn * NT * SGW + j * SGW), N, jm::layout::row_major);
161                }
162            }
163        });
164    });
165}
166class k_i8; class k_i8blk;
167
168int main(int argc, char** argv) {
169    const int M = argc > 1 ? std::atoi(argv[1]) : 160;
170    const int E = argc > 2 ? std::atoi(argv[2]) : 64;
171    const int reps = argc > 3 ? std::atoi(argv[3]) : 20;
172    sycl::queue q{sycl::gpu_selector_v, sycl::property::queue::in_order()};
173    std::printf("device %s, sub-group %d, M %d, %d experts\n", q.get_device().get_info<sycl::info::device::name>().c_str(), SGW, M, E);
174    struct Shape { const char* nm; int N, K; } shapes[2] = {{"gate+up", 1280, 2560}, {"down", 2560, 640}};
175    std::mt19937 rng(5);
176    for (const Shape& sh : shapes) {
177        const int N = sh.N, K = sh.K, nkb = K / 32;
178        const size_t nw = (size_t) E * N * K, na = (size_t) M * K;
179        std::vector<int8_t> hA(na), hW(nw);
180        std::vector<half> hAh(na);
181        for (size_t i = 0; i < na; ++i) { hA[i] = (int8_t) ((int) (rng() % 255) - 127); hAh[i] = half((float) hA[i] / 64.f); }
182        for (auto& x : hW) x = (int8_t) ((int) (rng() % 31) - 15);
183        std::vector<float> hdw((size_t) E * N * nkb), hdx((size_t) M * nkb);
184        for (auto& x : hdw) x = 0.01f * (1 + rng() % 8);
185        for (auto& x : hdx) x = 0.02f * (1 + rng() % 8);
186        int8_t *dA = sycl::malloc_device<int8_t>(na, q), *dW = sycl::malloc_device<int8_t>(nw, q);
187        half *dAh = sycl::malloc_device<half>(na, q), *dWh = sycl::malloc_device<half>(nw, q);
188        float *ddw = sycl::malloc_device<float>(hdw.size(), q), *ddx = sycl::malloc_device<float>(hdx.size(), q);
189        float* dC = sycl::malloc_device<float>((size_t) E * M * N, q);
190        q.memcpy(dA, hA.data(), na).wait(); q.memcpy(dW, hW.data(), nw).wait();
191        q.memcpy(dAh, hAh.data(), na * 2).wait();
192        { std::vector<half> t(nw); for (size_t i = 0; i < nw; ++i) t[i] = half((float) hW[i]); q.memcpy(dWh, t.data(), nw * 2).wait(); }
193        q.memcpy(ddw, hdw.data(), hdw.size() * 4).wait(); q.memcpy(ddx, hdx.data(), hdx.size() * 4).wait();
194        auto time = [&](auto&& fn) {
195            fn(); q.wait();
196            double best = 1e30;
197            for (int r = 0; r < reps; ++r) {
198                const auto t0 = std::chrono::steady_clock::now();
199                fn(); q.wait();
200                best = std::min(best, std::chrono::duration<double, std::micro>(std::chrono::steady_clock::now() - t0).count());
201            }
202            return best / E;   // us per expert (launch overhead amortised over E experts)
203        };
204        const double ops = 2.0 * M * N * K;
205        const double t0 = time([&] { run_f16(q, dAh, dWh, dC, M, N, K, E); });
206        const double t1 = time([&] { run_i8<false, k_i8>(q, dA, dW, ddw, ddx, dC, M, N, K, E); });
207        const double t2 = time([&] { run_i8<true, k_i8blk>(q, dA, dW, ddw, ddx, dC, M, N, K, E); });
208        // check i8blk expert 3 against a host reference
209        std::vector<float> hC((size_t) M * N);
210        q.memcpy(hC.data(), dC + (size_t) 3 * M * N, hC.size() * 4).wait();
211        double maxerr = 0, maxref = 0;
212        for (int m = 0; m < M; m += 7) for (int n = 0; n < N; n += 11) {
213            double ref = 0;
214            for (int kb = 0; kb < nkb; ++kb) {
215                long long s = 0;
216                for (int k = 0; k < 32; ++k) s += (long long) hA[(size_t) m * K + kb * 32 + k] * hW[((size_t) 3 * N + n) * K + kb * 32 + k];
217                ref += (double) s * hdw[((size_t) 3 * N + n) * nkb + kb] * hdx[(size_t) m * nkb + kb];
218            }
219            maxref = std::max(maxref, std::fabs(ref)); maxerr = std::max(maxerr, std::fabs(ref - hC[(size_t) m * N + n]));
220        }
221        std::printf("%-8s N %d K %d: f16 %.1f us/expert (%.1f TFLOPs), i8 %.1f us (%.1f TOPS), i8blk %.1f us (%.1f TOPS); i8blk max err %.3g of %.3g\n",
222                    sh.nm, N, K, t0, ops / t0 / 1e6, t1, ops / t1 / 1e6, t2, ops / t2 / 1e6, maxerr, maxref);
223        sycl::free(dA, q); sycl::free(dW, q); sycl::free(dAh, q); sycl::free(dWh, q); sycl::free(ddw, q); sycl::free(ddx, q); sycl::free(dC, q);
224    }
225    return 0;
226}