okami.mom/strata

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

Niko1221Merge f-037-arc (5eaee81a4)86bed1c92

main
10.0 KiB199 linesraw
1// A770 port: grouped FP16 XMX GEMM for the prompt path's experts (STRATA_PF_XMX=1).
2// Y[r][n] = sum_k X[r][k] * W_e[n][k] for each expert e of a group, its rows r in [row0 + off[e], row0 + off[e+1]).
3// One launch per group instead of one oneMKL call per expert.  joint_matrix 8x8x16 (DG2), SLM-tiled, 256 GRF.
4// Measured standalone on the A770 (64 experts, gamma-distributed rows, vs per-expert oneMKL): down 6.1x at mean 40
5// rows, 2.8x at 160, 1.75x at 320; gate/up 3.0x / 1.46x / 0.79x (oneMKL wins above ~250 rows: the caller picks).
6#include <sycl/sycl.hpp>
7#include "strata/kprof.hpp"
8#include <sycl/ext/oneapi/matrix/matrix.hpp>
9#include <sycl/ext/intel/experimental/grf_size_properties.hpp>
10#include <cstdlib>
11#include <cstdio>
12#include "strata/sycl_queue.hpp"
13#include "strata/prefill/xmx_moe.hpp"
14
15namespace strata::prefill::xmx {
16namespace {
17using half = sycl::half;
18namespace jm = sycl::ext::oneapi::experimental::matrix;
19// Sub-group width = the XMX tile's N: 8 on Alchemist (DG2, joint_matrix 8x8x16), 16 on Xe2 (Battlemage, 8x16x16; the
20// CMake switch STRATA_XMX_SG16 for an AOT bmg / lnl build).  A build has one shape: the other is not valid there.
21#ifdef STRATA_XMX_SG16
22constexpr int kSgW = 16;
23#else
24constexpr int kSgW = 8;
25#endif
26
27struct Grp {
28    const half* w[kMaxGroup];
29    int off[kMaxGroup + 1];     // rows, relative to row0
30    int tstart[kMaxGroup + 1];  // first M-tile of each expert
31    int n;
32};
33
34template <int SGM_, int SGN_, int MT_, int NT_, int KS_>
35struct Cfg {
36    static constexpr int SGM = SGM_, SGN = SGN_, MT = MT_, NT = NT_, KS = KS_;
37    static constexpr int SG = kSgW;
38    static constexpr int BM = SGM * MT * 8, BN = SGN * NT * kSgW, NSG = SGM * SGN, WG = NSG * SG;
39};
40
41template <class C, class Name>
42void launch(sycl::queue& q, const half* X, float* Y, const Grp& gp, int ntiles, int N, int K) {
43    constexpr int BM = C::BM, BN = C::BN, KS = C::KS, WG = C::WG, SG = C::SG;
44    constexpr int LDA = KS + 8, LDB = KS + 8;
45    const int nb = N / BN;
46#ifndef STRATA_NO_DG2_XMX
47    constexpr int TN = kSgW;
48    strata::kprof::kp(&q, __FILE__, __func__, __LINE__)->submit([&](sycl::handler& h) {
49        sycl::local_accessor<half, 1> sa(BM * LDA, h), sb(BN * LDB, h);
50        auto props = sycl::ext::oneapi::experimental::properties{sycl::ext::intel::experimental::grf_size<256>};
51        h.parallel_for<Name>(sycl::nd_range<2>({(size_t) ntiles, (size_t) nb * WG}, {1, (size_t) WG}), props,
52                             [=](sycl::nd_item<2> it) [[sycl::reqd_sub_group_size(kSgW)]] {
53            auto sg = it.get_sub_group();
54            const int t = (int) it.get_group(0);
55            int e = 0;
56            while (e + 1 < gp.n && t >= gp.tstart[e + 1]) ++e;
57            const int m0 = (t - gp.tstart[e]) * BM;
58            const int rows = sycl::min(BM, gp.off[e + 1] - gp.off[e] - m0);
59            const half* Xe = X + ((size_t) gp.off[e] + m0) * K;
60            float* Ye = Y + ((size_t) gp.off[e] + m0) * N;
61            const half* We = gp.w[e];
62            const int n0 = (int) it.get_group(1) * BN;
63            const int lid = (int) it.get_local_id(1), sgid = lid / SG;
64            const int sgm = sgid / C::SGN, sgn = sgid % C::SGN;
65            jm::joint_matrix<sycl::sub_group, float, jm::use::accumulator, 8, TN> acc[C::MT][C::NT];
66            for (int i = 0; i < C::MT; ++i)
67                for (int j = 0; j < C::NT; ++j) jm::joint_matrix_fill(sg, acc[i][j], 0.0f);
68            half* pa = sa.template get_multi_ptr<sycl::access::decorated::no>().get();
69            half* pb = sb.template get_multi_ptr<sycl::access::decorated::no>().get();
70            for (int k0 = 0; k0 < K; k0 += KS) {
71                constexpr int VA = BM * KS / 8, VB = BN * KS / 8;
72                for (int v = lid; v < VA; v += WG) {
73                    const int r = v / (KS / 8), c = (v % (KS / 8)) * 8;
74                    sycl::vec<half, 8> x(0);
75                    if (r < rows) x = *reinterpret_cast<const sycl::vec<half, 8>*>(Xe + (size_t) r * K + k0 + c);
76                    *reinterpret_cast<sycl::vec<half, 8>*>(pa + r * LDA + c) = x;
77                }
78                for (int v = lid; v < VB; v += WG) {
79                    const int r = v / (KS / 8), c = (v % (KS / 8)) * 8;
80                    *reinterpret_cast<sycl::vec<half, 8>*>(pb + r * LDB + c) =
81                        *reinterpret_cast<const sycl::vec<half, 8>*>(We + (size_t) (n0 + r) * K + k0 + c);
82                }
83                it.barrier(sycl::access::fence_space::local_space);
84                for (int kk = 0; kk < KS; kk += 16) {
85                    jm::joint_matrix<sycl::sub_group, half, jm::use::a, 8, 16, jm::layout::row_major> a[C::MT];
86                    jm::joint_matrix<sycl::sub_group, half, jm::use::b, 16, TN, jm::layout::col_major> b[C::NT];
87                    for (int i = 0; i < C::MT; ++i)
88                        jm::joint_matrix_load(sg, a[i], sa.template get_multi_ptr<sycl::access::decorated::no>() +
89                                                            (sgm * C::MT * 8 + i * 8) * LDA + kk, LDA);
90                    for (int j = 0; j < C::NT; ++j)
91                        jm::joint_matrix_load(sg, b[j], sb.template get_multi_ptr<sycl::access::decorated::no>() +
92                                                            (sgn * C::NT * TN + j * TN) * LDB + kk, LDB);
93                    for (int i = 0; i < C::MT; ++i)
94                        for (int j = 0; j < C::NT; ++j) jm::joint_matrix_mad(sg, acc[i][j], a[i], b[j], acc[i][j]);
95                }
96                it.barrier(sycl::access::fence_space::local_space);
97            }
98            for (int i = 0; i < C::MT; ++i) {
99                const int r = sgm * C::MT * 8 + i * 8;
100                if (r >= rows) continue;
101                for (int j = 0; j < C::NT; ++j) {
102                    const int c = n0 + sgn * C::NT * TN + j * TN;
103                    if (r + 8 <= rows) {
104                        jm::joint_matrix_store(sg, acc[i][j],
105                            sycl::address_space_cast<sycl::access::address_space::global_space, sycl::access::decorated::no>(
106                                Ye + (size_t) r * N + c), N, jm::layout::row_major);
107                    } else {   // partial rows: through this sub-group's own SLM slice
108                        float* st = reinterpret_cast<float*>(pa) + sgid * 8 * TN;
109                        jm::joint_matrix_store(sg, acc[i][j],
110                            sycl::address_space_cast<sycl::access::address_space::local_space, sycl::access::decorated::no>(st),
111                            TN, jm::layout::row_major);
112                        sycl::group_barrier(sg);
113                        const int lim = rows - r, ln = (int) sg.get_local_linear_id();
114                        for (int rr = 0; rr < lim; ++rr) Ye[(size_t) (r + rr) * N + c + ln] = st[rr * TN + ln];
115                        sycl::group_barrier(sg);
116                    }
117                }
118            }
119        });
120    });
121#else
122    (void) q; (void) X; (void) Y; (void) gp; (void) ntiles; (void) N; (void) K;
123    std::fprintf(stderr, "grouped XMX: the 8x8x16 / sub-group 8 kernel is not built for this device (STRATA_NO_DG2_XMX)\n");
124    std::exit(1);
125#endif
126}
127
128class k_small; class k_big;
129#ifdef STRATA_XMX_SG16
130using Small = Cfg<2, 4, 4, 2, 32>;   // 64 x 128 tile: few rows per expert (8 sub-groups of 16)
131using Big = Cfg<4, 2, 4, 4, 32>;     // 128 x 128 tile
132#else
133using Small = Cfg<2, 8, 4, 2, 32>;   // 64 x 128 tile: few rows per expert
134using Big = Cfg<4, 4, 4, 4, 32>;     // 128 x 128 tile
135#endif
136
137template <class C>
138int fill(Grp& gp, const int32_t* cnt, int n) {
139    int t = 0, o = 0;
140    for (int i = 0; i < n; ++i) {
141        gp.off[i] = o; gp.tstart[i] = t;
142        o += cnt[i]; t += (cnt[i] + C::BM - 1) / C::BM;
143    }
144    gp.off[n] = o; gp.tstart[n] = t; gp.n = n;
145    return t;
146}
147}  // namespace
148
149int mode() {
150    // The kernels are joint_matrix 8x8x16 on sub-group 8 (DG2, Arc A-series) or, in a STRATA_XMX_SG16 build, 8x16x16 on
151    // sub-group 16 (Xe2, Battlemage).
152    static const int v = [] {
153        const char* e = std::getenv("STRATA_PF_XMX");
154        // 0.1.48 (owner approved 2026-10-18): mode 2 (the fused grouped XMX launch) is the default on Arc Alchemist (A-series): A750 prompts +30..39% (4K), +32..35% (1-2K tokens),
155        // first-token KL 0.002 against a 0.0005 floor, HumanEval / reasoning / agent / CJK / needles inside the run-to-run spread; Battlemage is
156        // 7-11% slower with it, so it stays off there.  STRATA_PF_XMX=0 turns it off, 1 is the dequantize + grouped XMX variant.
157        const bool alchemist = strata::intel_gpu_gen(strata::q_of(nullptr)->get_device()) == strata::IntelGpuGen::Alchemist;
158        const int want = e ? std::atoi(e) : alchemist ? 2 : 0;
159        if (want == 0) return 0;
160#ifdef STRATA_NO_DG2_XMX
161        std::fprintf(stderr, "STRATA_PF_XMX ignored: this build is AOT for a device without the sub-group 8 XMX kernels\n");
162        return 0;
163#else
164#ifndef STRATA_XMX_SG16
165        if (strata::intel_gpu_gen(strata::q_of(nullptr)->get_device()) == strata::IntelGpuGen::Battlemage) {
166            std::fprintf(stderr, "STRATA_PF_XMX ignored: this build has the sub-group 8 / 8x8x16 kernels (Arc A-series); Battlemage needs the AOT bmg build\n");
167            return 0;
168        }
169#endif
170        return want;
171#endif
172    }();
173    return v;
174}
175bool enabled() { return mode() != 0; }
176
177int gu_max_mean() {
178    static const int v = [] { const char* e = std::getenv("STRATA_PF_XMX_GU_MAX"); return e ? std::atoi(e) : 250; }();
179    return v;
180}
181
182void grouped_f16(const uint16_t* X, const uint16_t* const* W, const int32_t* cnt, int n, float* Y, int N, int K,
183                 void* stream) {
184    if (n <= 0) return;
185    sycl::queue& q = *strata::q_of(stream);
186    Grp gp{};
187    int64_t tot = 0;
188    for (int i = 0; i < n; ++i) { gp.w[i] = reinterpret_cast<const half*>(W[i]); tot += cnt[i]; }
189    if (tot <= 0) return;
190    const half* Xh = reinterpret_cast<const half*>(X);
191    if (tot / n < 96) {
192        const int nt = fill<Small>(gp, cnt, n);
193        launch<Small, k_small>(q, Xh, Y, gp, nt, N, K);
194    } else {
195        const int nt = fill<Big>(gp, cnt, n);
196        launch<Big, k_big>(q, Xh, Y, gp, nt, N, K);
197    }
198}
199}  // namespace strata::prefill::xmx