Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.
C++
sycl/A770 prefill: grouped XMX expert GEMM (opt-in STRATA_PF_XMX=1/2)
Andy Slaterce60cc373fe1
diff --git a/include/strata/kernels/iq_kernels.hpp b/include/strata/kernels/iq_kernels.hppindex 2c38f6f98..bd7599f7b 100644--- a/include/strata/kernels/iq_kernels.hpp+++ b/include/strata/kernels/iq_kernels.hpp@@ -53,6 +53,14 @@ bool native_expert_supported(int gu_type, int d_type, int64_t n_embd, int64_t n_/// Bytes of scratch `native_expert_grouped` needs for `cap_entries` entries.size_t native_expert_scratch_bytes(int64_t cap_entries, int64_t n_ff);+/// A770 port (STRATA_PF_XMX=2): up to kXmxGroupMax experts' products in one XMX launch, the weights decoded from the+/// quantized rows into local memory. Expert i: Y_i[cnt[i]][N] = X_i . W_i^T (X FP16, Y FP32, rows back to back).+/// gate/up: b != nullptr (a = gate, b = up rows of n_embd = K; the output's virtual rows interleave gate/up);+/// down: b == nullptr, a = the [N][K] rows. N a multiple of 128, K of 32 (and a whole number of rows per 256 block).+constexpr int kXmxGroupMax = 16;+void iq_xmx_grouped(int ty, const uint8_t* const* a, const uint8_t* const* b, const int32_t* cnt, int n,+ const uint16_t* X, float* Y, int N, int K, void* stream);+/// Grouped experts in the native format: group g's blob at device address grp_ptr[g]; its entries/// [grp_start[g], grp_start[g+1]) read token ent_tok[e]'s q8_1 activation (n_embd/32 blocks per token in x_q8_1)/// and write row ent_dst[e] of `out` (n_embd floats). Counts are read on the device.diff --git a/include/strata/prefill/xmx_moe.hpp b/include/strata/prefill/xmx_moe.hppnew file mode 100644index 000000000..b840db886--- /dev/null+++ b/include/strata/prefill/xmx_moe.hpp@@ -0,0 +1,20 @@+// A770 port: grouped FP16 XMX GEMM for the prompt path's experts (STRATA_PF_XMX=1). See sycl/src/prefill/xmx_moe.dp.cpp.+#pragma once+#include <cstdint>++namespace strata::prefill::xmx {+constexpr int kMaxGroup = 16;++/// STRATA_PF_XMX=1: the FP16 expert path dequantizes a group of up to kMaxGroup experts, then runs each product+/// over the group in one launch.+bool enabled();+/// 0 off, 1 dequantize to FP16 then grouped XMX GEMM, 2 grouped XMX with the IQ decode fused (streamed walk only)+int mode();+/// gate/up on XMX only while the group's mean rows per expert stay below this (STRATA_PF_XMX_GU_MAX, default 250)+int gu_max_mean();++/// For each expert i < n: Y_i = X_i . W_i^T, X_i = cnt[i] FP16 rows of K, packed back to back from X (Y likewise,+/// N floats per row); W_i row-major [N][K] FP16. N a multiple of 128, K of 32. Enqueued on `stream`, no sync.+void grouped_f16(const uint16_t* X, const uint16_t* const* W, const int32_t* cnt, int n, float* Y, int N, int K,+ void* stream);+} // namespace strata::prefill::xmxdiff --git a/sycl/CMakeLists.txt b/sycl/CMakeLists.txtindex 44fa4924b..818571fff 100644--- a/sycl/CMakeLists.txt+++ b/sycl/CMakeLists.txt@@ -223,7 +223,8 @@ target_link_libraries(strata_engine PUBLIC strata_core strata_kernels strata_ker# cuBLAS -> oneMKL through dpct::blas (needs the MKL SYCL BLAS domain)strata_resolve(_prefill src/prefill/gemm.cu src/prefill/kernels.cu src/prefill/prefill.cpp)add_library(strata_prefill STATIC ${_prefill}- ${CMAKE_CURRENT_SOURCE_DIR}/src/prefill/moe_fused_stub.cpp) # upstream's fused int8 kernels: not built here+ ${CMAKE_CURRENT_SOURCE_DIR}/src/prefill/moe_fused_stub.cpp # upstream's fused int8 kernels: not built here+ ${CMAKE_CURRENT_SOURCE_DIR}/src/prefill/xmx_moe.dp.cpp) # A770 port: grouped XMX expert GEMMtarget_compile_options(strata_prefill PUBLIC -qmkl=sequential)target_link_options(strata_prefill PUBLIC -qmkl=sequential)target_link_libraries(strata_prefill PUBLIC strata_engine)@@ -301,6 +302,8 @@ if(STRATA_SYCL_PARITY)target_link_libraries(gr_bench PRIVATE strata_kernels)add_executable(xmx_gemm_bench ${CMAKE_CURRENT_SOURCE_DIR}/src/kernels/xmx_gemm_bench.cpp) # XMX quantized GEMM vs dequant+oneMKLtarget_link_libraries(xmx_gemm_bench PRIVATE strata_kernels strata_prefill) # -qmkl for the oneMKL reference+ add_executable(xmx_group_parity ${CMAKE_CURRENT_SOURCE_DIR}/src/kernels/xmx_group_parity.cpp) # A770: fused grouped XMX+ target_link_libraries(xmx_group_parity PRIVATE strata_kernels strata_prefill)# one native expert three ways on real GGUF rows (ggml reference, CPU path, GPU native_expert_grouped):# native_expert_parity <shard1.gguf> [layer ...]if(STRATA_NATIVE_EXPERTS)diff --git a/sycl/src/kernels/cuda/iq_kernels.dp.cpp b/sycl/src/kernels/cuda/iq_kernels.dp.cppindex 3110c887a..57afe8bad 100644--- a/sycl/src/kernels/cuda/iq_kernels.dp.cpp+++ b/sycl/src/kernels/cuda/iq_kernels.dp.cpp@@ -19,6 +19,7 @@#include "ggml-common.h"#include <sycl/ext/oneapi/matrix/matrix.hpp>+#include <sycl/ext/intel/experimental/grf_size_properties.hpp>#include <algorithm>#include <type_traits>#include <cstdio>@@ -5189,6 +5190,147 @@ void native_expert_grouped(const NativeExpertLayout& L, const unsigned long long}+// A770 port (STRATA_PF_XMX=2): a group of experts' products in ONE launch, the weight tiles decoded straight from+// the quantized blobs into local memory (no FP16 copy of the expert in global memory). Sub-group 8, joint_matrix+// 8x8x16 (DG2), 256 GRF. Per expert i of the group: Y_i = X_i . W_i^T, rows back to back (cnt[i] each).+// gate/up: up_i != nullptr, virtual row 2r = gate row r, 2r+1 = up row r (iq_dequant_gu_f16's interleave).+namespace xmxg {+namespace jm = sycl::ext::oneapi::experimental::matrix;+struct Grp {+ const uint8_t* a[kXmxGroupMax];+ const uint8_t* b[kXmxGroupMax];+ int off[kXmxGroupMax + 1];+ int tstart[kXmxGroupMax + 1];+ int n;+};+template <int SGM_, int SGN_, int MT_, int NT_>+struct Cfg {+ static constexpr int SGM = SGM_, SGN = SGN_, MT = MT_, NT = NT_, KS = 32, SG = 8;+ static constexpr int BM = SGM * MT * 8, BN = SGN * NT * 8, WG = SGM * SGN * 8;+};+template <class C, class Name>+void launch(sycl::queue& q, int ty, bool gu, const sycl::half* X, float* Y, const Grp& gp, int ntiles, int N, int K) {+ constexpr int BM = C::BM, BN = C::BN, KS = C::KS, WG = C::WG, SG = C::SG;+ constexpr int LDA = KS + 8, LDB = KS + 8;+ const int nb = N / BN;+ q.submit([&](sycl::handler& h) {+ sycl::local_accessor<sycl::half, 1> sa(BM * LDA, h), sb(BN * LDB, h);+ auto props = sycl::ext::oneapi::experimental::properties{sycl::ext::intel::experimental::grf_size<256>};+ h.parallel_for<Name>(sycl::nd_range<2>({(size_t) ntiles, (size_t) nb * WG}, {1, (size_t) WG}), props,+ [=](sycl::nd_item<2> it) [[sycl::reqd_sub_group_size(8)]] {+ auto sg = it.get_sub_group();+ const int t = (int) it.get_group(0);+ int e = 0;+ while (e + 1 < gp.n && t >= gp.tstart[e + 1]) ++e;+ const int m0 = (t - gp.tstart[e]) * BM;+ const int rows = sycl::min(BM, gp.off[e + 1] - gp.off[e] - m0);+ const sycl::half* Xe = X + ((size_t) gp.off[e] + m0) * K;+ float* Ye = Y + ((size_t) gp.off[e] + m0) * N;+ const uint8_t* wa = gp.a[e];+ const uint8_t* wb = gp.b[e];+ const int n0 = (int) it.get_group(1) * BN;+ const int lid = (int) it.get_local_id(1), sgid = lid / SG;+ const int sgm = sgid / C::SGN, sgn = sgid % C::SGN;+ jm::joint_matrix<sycl::sub_group, float, jm::use::accumulator, 8, 8> acc[C::MT][C::NT];+ for (int i = 0; i < C::MT; ++i)+ for (int j = 0; j < C::NT; ++j) jm::joint_matrix_fill(sg, acc[i][j], 0.0f);+ sycl::half* pa = sa.template get_multi_ptr<sycl::access::decorated::no>().get();+ sycl::half* pb = sb.template get_multi_ptr<sycl::access::decorated::no>().get();+ for (int k0 = 0; k0 < K; k0 += KS) {+ constexpr int VA = BM * KS / 8;+ for (int v = lid; v < VA; v += WG) {+ const int r = v / (KS / 8), c = (v % (KS / 8)) * 8;+ sycl::vec<sycl::half, 8> x(0);+ if (r < rows) x = *reinterpret_cast<const sycl::vec<sycl::half, 8>*>(Xe + (size_t) r * K + k0 + c);+ *reinterpret_cast<sycl::vec<sycl::half, 8>*>(pa + r * LDA + c) = x;+ }+ // B: row r of the tile, 32 values from k0 = 4 runs of 8, each one thread of the format's decoder+ for (int v = lid; v < BN * 4; v += WG) {+ const int r = v >> 2, sub = v & 3, nr = n0 + r;+ const uint8_t* base = gu ? ((nr & 1) ? wb : wa) : wa;+ const int64_t row = gu ? (nr >> 1) : nr;+ const int64_t flat = row * K + k0; // a multiple of 32+ const int64_t ibs = flat >> 8;+ const int o32 = (int) (flat & 255), o = o32 + 8 * sub;+ const int tid = ty == 42 ? ((o >> 6) * 8 + ((o & 63) >> 3)) : (sub * 8 + (o32 >> 5));+ dq_dispatch<sycl::half>(ty, base, ibs, pb + r * LDB - o32, tid);+ }+ it.barrier(sycl::access::fence_space::local_space);+ for (int kk = 0; kk < KS; kk += 16) {+ jm::joint_matrix<sycl::sub_group, sycl::half, jm::use::a, 8, 16, jm::layout::row_major> am[C::MT];+ jm::joint_matrix<sycl::sub_group, sycl::half, jm::use::b, 16, 8, jm::layout::col_major> bm[C::NT];+ for (int i = 0; i < C::MT; ++i)+ jm::joint_matrix_load(sg, am[i], sa.template get_multi_ptr<sycl::access::decorated::no>() ++ (sgm * C::MT * 8 + i * 8) * LDA + kk, LDA);+ for (int j = 0; j < C::NT; ++j)+ jm::joint_matrix_load(sg, bm[j], sb.template get_multi_ptr<sycl::access::decorated::no>() ++ (sgn * C::NT * 8 + j * 8) * LDB + kk, LDB);+ for (int i = 0; i < C::MT; ++i)+ for (int j = 0; j < C::NT; ++j) jm::joint_matrix_mad(sg, acc[i][j], am[i], bm[j], acc[i][j]);+ }+ it.barrier(sycl::access::fence_space::local_space);+ }+ for (int i = 0; i < C::MT; ++i) {+ const int r = sgm * C::MT * 8 + i * 8;+ if (r >= rows) continue;+ for (int j = 0; j < C::NT; ++j) {+ const int c = n0 + sgn * C::NT * 8 + j * 8;+ if (r + 8 <= rows) {+ jm::joint_matrix_store(sg, acc[i][j],+ sycl::address_space_cast<sycl::access::address_space::global_space, sycl::access::decorated::no>(+ Ye + (size_t) r * N + c), N, jm::layout::row_major);+ } else {+ float* st = reinterpret_cast<float*>(pa) + sgid * 64;+ jm::joint_matrix_store(sg, acc[i][j],+ sycl::address_space_cast<sycl::access::address_space::local_space, sycl::access::decorated::no>(st),+ 8, jm::layout::row_major);+ sycl::group_barrier(sg);+ const int lim = rows - r, ln = (int) sg.get_local_linear_id();+ for (int rr = 0; rr < lim; ++rr) Ye[(size_t) (r + rr) * N + c + ln] = st[rr * 8 + ln];+ sycl::group_barrier(sg);+ }+ }+ }+ });+ });+}+class k_xg_small; class k_xg_big;+using Small = Cfg<2, 8, 4, 2>; // 64 x 128+using Big = Cfg<4, 4, 4, 4>; // 128 x 128+template <class C>+int fill(Grp& gp, const int32_t* cnt, int n) {+ int t = 0, o = 0;+ for (int i = 0; i < n; ++i) { gp.off[i] = o; gp.tstart[i] = t; o += cnt[i]; t += (cnt[i] + C::BM - 1) / C::BM; }+ gp.off[n] = o; gp.tstart[n] = t; gp.n = n;+ return t;+}+} // namespace xmxg++void iq_xmx_grouped(int ty, const uint8_t* const* a, const uint8_t* const* b, const int32_t* cnt, int n,+ const uint16_t* X, float* Y, int N, int K, void* stream) {+ if (n <= 0) return;+ if (n > kXmxGroupMax || N % 128 != 0 || K % 32 != 0 || !is_iq(ty)) {+ std::fprintf(stderr, "iq_xmx_grouped: bad arguments (type %d, n %d, N %d, K %d)\n", ty, n, N, K);+ std::exit(1);+ }+ xmxg::Grp gp{};+ int64_t tot = 0;+ for (int i = 0; i < n; ++i) { gp.a[i] = a[i]; gp.b[i] = b ? b[i] : nullptr; tot += cnt[i]; }+ if (tot <= 0) return;+ sycl::queue& q = *strata::q_of(stream);+ const auto* Xh = reinterpret_cast<const sycl::half*>(X);+ const bool gu = b != nullptr;+ if (tot / n < 96) {+ const int nt = xmxg::fill<xmxg::Small>(gp, cnt, n);+ xmxg::launch<xmxg::Small, xmxg::k_xg_small>(q, ty, gu, Xh, Y, gp, nt, N, K);+ } else {+ const int nt = xmxg::fill<xmxg::Big>(gp, cnt, n);+ xmxg::launch<xmxg::Big, xmxg::k_xg_big>(q, ty, gu, Xh, Y, gp, nt, N, K);+ }+ check("iq_xmx_grouped");+}++// ================================================================ SYCL port: XMX GEMM straight from the quantized rows//// The prompt path dequantizes every routed expert to FP16 (8 MB written, then read back by oneMKL) before its GEMMs:diff --git a/sycl/src/kernels/xmx_group_parity.cpp b/sycl/src/kernels/xmx_group_parity.cppnew file mode 100644index 000000000..d99f8c238--- /dev/null+++ b/sycl/src/kernels/xmx_group_parity.cpp@@ -0,0 +1,112 @@+// xmx_group_parity: iq_xmx_grouped (the A770 port's fused decode + grouped XMX) against the engine's dequant ++// oneMKL per expert, on random expert blobs of each format the native packs use. Also times both.+// xmx_group_parity [mean_rows=160] [n=16]+#include "strata/kernels/iq_kernels.hpp"+#include <sycl/sycl.hpp>+#include <dpct/dpct.hpp>+#include <dpct/blas_utils.hpp>+#include <chrono>+#include <cmath>+#include <cstdio>+#include <cstdlib>+#include <random>+#include <vector>+static uint16_t f2h(float f) { return sycl::bit_cast<uint16_t>(sycl::half(f)); }+int main(int argc, char** argv) {+ const int mean = argc > 1 ? std::atoi(argv[1]) : 160;+ const int n = argc > 2 ? std::atoi(argv[2]) : 16;+ sycl::queue* q = &dpct::get_in_order_queue();+ dpct::blas::descriptor_ptr hd = new dpct::blas::descriptor();+ hd->set_queue(q);+ std::mt19937 rng(3);+ std::vector<int32_t> cnt(n);+ std::gamma_distribution<double> gd(1.5, mean / 1.5);+ int64_t tot = 0;+ for (auto& c : cnt) { c = std::max(1, (int) gd(rng)); tot += c; }+ int fails = 0;+ struct Case { int ty; bool gu; };+ // the formats of the native packs' experts that the prompt path's fused XMX takes (prefill.cpp xmx2_fmt)+ for (Case cs : {Case{18, true}, Case{21, true}, Case{22, true}, Case{23, true}, Case{20, false}, Case{42, false}}) {+ const int64_t K = cs.gu ? 2560 : 640, N = cs.gu ? 1280 : 2560, n_ff = 640;+ const int blk_elems = cs.ty == 20 ? 32 : cs.ty == 42 ? 64 : 256;+ const size_t blk_bytes = strata::kernels::iq_row_bytes(cs.ty, blk_elems);+ const size_t mat_bytes = strata::kernels::iq_row_bytes(cs.ty, K) * (cs.gu ? n_ff : N);+ if (blk_bytes == 0 || mat_bytes == 0) { std::printf("type %d: not supported\n", cs.ty); continue; }+ auto blob = [&]() {+ std::vector<uint8_t> b(mat_bytes);+ for (auto& x : b) x = (uint8_t) rng();+ if (cs.ty != 29) // d = 0.0625 at each block's start (IQ1_M keeps its scale in the scales field)+ for (size_t o = 0; o + 1 < b.size(); o += blk_bytes) { b[o] = 0x00; b[o + 1] = 0x2C; }+ return b;+ };+ std::vector<uint8_t*> dg(n), du(n);+ std::vector<const uint8_t*> pg(n), pu(n);+ for (int i = 0; i < n; ++i) {+ auto g = blob();+ dg[i] = sycl::malloc_device<uint8_t>(mat_bytes, *q);+ q->memcpy(dg[i], g.data(), mat_bytes);+ pg[i] = dg[i];+ if (cs.gu) {+ auto u = blob();+ du[i] = sycl::malloc_device<uint8_t>(mat_bytes, *q);+ q->memcpy(du[i], u.data(), mat_bytes);+ pu[i] = du[i];+ }+ }+ std::vector<uint16_t> hx((size_t) tot * K);+ std::normal_distribution<float> nd(0.f, 1.f);+ for (auto& x : hx) x = f2h(nd(rng) * 0.25f);+ uint16_t* dx = sycl::malloc_device<uint16_t>(hx.size(), *q);+ q->memcpy(dx, hx.data(), hx.size() * 2);+ uint16_t* dw = sycl::malloc_device<uint16_t>((size_t) N * K, *q);+ float* yr = sycl::malloc_device<float>((size_t) tot * N, *q);+ float* yx = sycl::malloc_device<float>((size_t) tot * N, *q);+ q->wait();+ auto ref = [&]() {+ int64_t o = 0;+ for (int i = 0; i < n; ++i) {+ if (cs.gu) strata::kernels::iq_dequant_gu_f16(cs.ty, dg[i], du[i], n_ff, K, dw, q);+ else strata::kernels::iq_dequant_f16(cs.ty, dg[i], N * K, dw, q);+ const float a = 1.f, b = 0.f;+ dpct::blas::gemm(hd, oneapi::mkl::transpose::trans, oneapi::mkl::transpose::nontrans, (int) N, cnt[i],+ (int) K, &a, dw, dpct::library_data_t::real_half, (int) K, dx + o * K,+ dpct::library_data_t::real_half, (int) K, &b, yr + o * N,+ dpct::library_data_t::real_float, (int) N, dpct::compute_type::f32);+ o += cnt[i];+ }+ };+ auto mine = [&]() {+ strata::kernels::iq_xmx_grouped(cs.ty, pg.data(), cs.gu ? pu.data() : nullptr, cnt.data(), n, dx, yx,+ (int) N, (int) K, q);+ };+ auto tm = [&](auto f) {+ f(); q->wait();+ const auto a = std::chrono::steady_clock::now();+ for (int r = 0; r < 10; ++r) f();+ q->wait();+ return std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - a).count() / 10;+ };+ const double t_ref = tm(ref), t_x = tm(mine);+ std::vector<float> a((size_t) tot * N), b((size_t) tot * N);+ q->memcpy(a.data(), yr, a.size() * 4);+ q->memcpy(b.data(), yx, b.size() * 4).wait();+ double e = 0, mx = 0;+ size_t bad = 0, bad_ref = 0, mism = 0;+ for (size_t i = 0; i < a.size(); ++i) {+ if (!std::isfinite(a[i])) { ++bad_ref; if (std::isfinite(b[i])) ++mism; continue; }+ if (!std::isfinite(b[i])) { ++bad; continue; }+ e = std::max(e, (double) std::fabs(a[i] - b[i]));+ mx = std::max(mx, (double) std::fabs(a[i]));+ }+ if (bad_ref) std::printf(" (reference non-finite %zu of %zu, fused finite there %zu)\n", bad_ref, a.size(), mism);+ const bool ok = bad == 0 && mism == 0 && mx > 0 && e <= 2e-3 * mx;+ fails += !ok;+ std::printf("type %2d %-7s rows %5lld: dequant+oneMKL %7.3f ms | fused xmx %7.3f ms (%.2fx) relerr %.1e %s\n",+ cs.ty, cs.gu ? "gate/up" : "down", (long long) tot, t_ref, t_x, t_ref / t_x, e / (mx > 0 ? mx : 1),+ ok ? "OK" : "FAIL");+ for (int i = 0; i < n; ++i) { sycl::free(dg[i], *q); if (cs.gu) sycl::free(du[i], *q); }+ sycl::free(dx, *q); sycl::free(dw, *q); sycl::free(yr, *q); sycl::free(yx, *q);+ }+ std::printf("%s\n", fails ? "FAILED" : "all OK");+ return fails ? 1 : 0;+}diff --git a/sycl/src/prefill/prefill.cpp b/sycl/src/prefill/prefill.cppindex ca16304c3..1e35e68d3 100644--- a/sycl/src/prefill/prefill.cpp+++ b/sycl/src/prefill/prefill.cpp@@ -3,6 +3,7 @@#include <sycl/sycl.hpp>#include <dpct/dpct.hpp>#include "strata/sycl_queue.hpp"+#include "strata/prefill/xmx_moe.hpp"#include "strata/prefill/prefill.hpp"#include "strata/core/gguf_expert_source.hpp"#include "../../../src/prefill/mmq_resident_sort.hpp"@@ -223,7 +224,12 @@ inline int ring_slots(size_t T) {const int big = r < 16 ? 16 : r > ring_cap() ? ring_cap() : r;return (int64_t) T >= stream_all_min() ? big : STAGE;}+// A770 port: STRATA_PF_XMX=2 holds up to a group of staged experts until its launch, so routed-only staging needs+// that many more slots for the lookahead to keep copying (the slots are only reused once the group's event is in)+inline int stage_slots() { return strata::prefill::xmx::mode() == 2 ? STAGE + strata::prefill::xmx::kMaxGroup : STAGE; }constexpr int DQ = 2; // dequantized-expert ring (FP16 gate/up + down)+// A770 port: STRATA_PF_XMX=1 dequantizes a whole group of experts, then one grouped XMX launch per product+inline int dq_slots() { return strata::prefill::xmx::mode() == 1 ? strata::prefill::xmx::kMaxGroup : DQ; }// The BF16-weight projections (hyper-connection, SSM alpha/beta, indexer, router, shared gate, PLE key/value) take// BF16 activations here and FP32 ones in decode. STRATA_PREFILL_BF16X2=1 adds each activation's BF16 remainder as a// second GEMM (Y = W.hi + W.lo, ~16 mantissa bits): a router that picks its top 10 from the same x decode would.@@ -672,8 +678,8 @@ struct Prefill::Impl {int32_t* grp_host = nullptr;int32_t* grp_dev = nullptr; // its device aliassize_t grp_n = 0, grp_tk = 0; // int32s allocated; T_max * K (the offset of slot, and of src past it)- uint16_t* dq_gu[DQ] = {};- uint16_t* dq_d[DQ] = {};+ uint16_t* dq_gu[strata::prefill::xmx::kMaxGroup] = {};+ uint16_t* dq_d[strata::prefill::xmx::kMaxGroup] = {};uint8_t* stage_dev[RING_MAX] = {};int ring = STAGE; // the slots of this layout's ring (ring_slots)// this layout's GU, H and Xq are the fused path's (moe_bufs' `fused`), so a layer that runs MMQ or the FP16 path may@@ -1187,7 +1193,7 @@ bool Prefill::carve(size_t T, void* alloc) {}if (base == nullptr) ok = false;}- for (int i = 0; i < DQ; ++i) { m.dq_gu[i] = o.take<uint16_t>(1280 * 2560, ok); m.dq_d[i] = o.take<uint16_t>(2560 * 640, ok); }+ for (int i = 0; i < dq_slots(); ++i) { m.dq_gu[i] = o.take<uint16_t>(1280 * 2560, ok); m.dq_d[i] = o.take<uint16_t>(2560 * 640, ok); }if (mmq_plan().any) {const MmqPlan& mp = mmq_plan();m.ids_identity = o.take<int32_t>(T * K, ok);@@ -1199,18 +1205,19 @@ bool Prefill::carve(size_t T, void* alloc) {if (!m.mmq_ctx) m.mmq_ctx = std::make_unique<mmq::Context>();}m.ring = ring_slots(T);- if (o.base == nullptr && m.ring > 0) {+ const int ring_n = std::max(m.ring, stage_slots()); // A770 port: STRATA_PF_XMX=2 holds a group of staged experts+ if (o.base == nullptr && ring_n > 0) {// OWNED buffers: the ring in ONE allocation. 384 separate 2.7 MiB cudaMallocs each round up to a 2 MiB page// (~1.3 MiB a slot, ~0.5 GiB in all) that no count ever saw. A borrowed region keeps its per-slot layout (and// so its price, `bytes_needed`: the loans' slot counts do not move).- uint8_t* ring_base = o.take<uint8_t>((size_t) m.ring * (size_t) MAXBLOB(), ok);- for (int i = 0; ok && i < m.ring; ++i) {+ uint8_t* ring_base = o.take<uint8_t>((size_t) ring_n * (size_t) MAXBLOB(), ok);+ for (int i = 0; ok && i < ring_n; ++i) {m.stage_dev[i] = ring_base + (size_t) i * (size_t) MAXBLOB();m.stage_live[i] = false;m.used_of[i] = i;}} else {- for (int i = 0; i < m.ring; ++i) {+ for (int i = 0; i < ring_n; ++i) {m.stage_dev[i] = o.take<uint8_t>((size_t) MAXBLOB(), ok);m.stage_live[i] = false; // a new buffer: nothing of an earlier layout to wait form.used_of[i] = i;@@ -1871,7 +1878,7 @@ uint64_t Prefill::bytes_needed_impl(const core::ModelGeometry& g, const core::Seconst int64_t max_blocks = ss.qsa_states[ss.qsa_primary()].max_cells / s.idx_block + 2;o.take<uint8_t>((size_t) std::max({gdn_set_bytes(T), qsa_set_bytes(T, cap, max_blocks, 256, 32, s),moe_set_bytes(T, g.n_expert, fused_layout(T, src))}), ok);- for (int i = 0; i < DQ; ++i) { o.take<uint16_t>(1280 * 2560, ok); o.take<uint16_t>(2560 * 640, ok); }+ for (int i = 0; i < dq_slots(); ++i) { o.take<uint16_t>(1280 * 2560, ok); o.take<uint16_t>(2560 * 640, ok); }if (mmq_plan().any) {const MmqPlan& mp = mmq_plan();o.take<int32_t>(T * K, ok);@@ -1879,10 +1886,11 @@ uint64_t Prefill::bytes_needed_impl(const core::ModelGeometry& g, const core::Seo.take<uint8_t>(MMQ_GROUP * mp.gu_max + MMQ_TAIL, ok);o.take<uint8_t>(MMQ_GROUP * mp.d_max + MMQ_TAIL, ok);}+ const int ring_n = std::max(ring_slots(T), stage_slots());if (owned_pages) {- if (ring_slots(T) > 0) o.take<uint8_t>((size_t) ring_slots(T) * (size_t) MAXBLOB(), ok); // one allocation+ if (ring_n > 0) o.take<uint8_t>((size_t) ring_n * (size_t) MAXBLOB(), ok); // one allocation} else {- for (int i = 0; i < ring_slots(T); ++i) o.take<uint8_t>((size_t) MAXBLOB(), ok);+ for (int i = 0; i < ring_n; ++i) o.take<uint8_t>((size_t) MAXBLOB(), ok);}f(T * N);f((size_t) strata::kernels::NG_HC_DIM);@@ -3809,7 +3817,7 @@ bool Prefill::run_impl(const int64_t *tokens, int64_t n, int64_t pos0,const bool resident = m.host_res && m.cache && m.host_res[(size_t) l * m.g->n_expert + e] >= 0;if (resident) return true;const int sl = stage_next;- stage_next = (stage_next + 1) % STAGE;+ stage_next = (stage_next + 1) % stage_slots();const auto th = Clock::now();const bool pinned = m.src->pinned(l, e); // pinned: never transientconst uint8_t* b = pinned ? m.src->blob(l, e) : nullptr;@@ -3911,6 +3919,82 @@ bool Prefill::run_impl(const int64_t *tokens, int64_t n, int64_t pos0,std::exit(1);}};+ // A770 port, STRATA_PF_XMX=2 (streamed walk, native pack, FP16 path): the experts' quantized+ // blobs are held until a group of kXmxGroupMax, then each product runs over the group in one+ // launch that decodes the weights itself. Their ring slots are released by ONE event after+ // the group's down product, and nothing is given back to the issuer while any are held.+ // (the formats the parity test covers: gate/up IQ3_XXS / IQ3_S / IQ2_S / IQ4_XS, down IQ4_NL / Q2_0;+ // a layer in another format keeps the dequantize + oneMKL path)+ auto xmx2_fmt = [&]() {+ if (!lay.native) return false;+ const int gt = lay.fmt[(size_t) l].gu_type, dt = lay.fmt[(size_t) l].d_type;+ return (gt == 18 || gt == 21 || gt == 22 || gt == 23) && (dt == 20 || dt == 42);+ };+ const bool xmx2 = strata::prefill::xmx::mode() == 2 && !use_mmq && xmx2_fmt();+ if (strata::prefill::xmx::mode() == 2 && !xmx2) {+ static bool said = false;+ if (!said) {+ said = true;+ std::fprintf(stderr, "strata: STRATA_PF_XMX=2 not taken at layer %lld (stream_all %d, mmq %d, "+ "native %d, formats %d/%d)\n", (long long) l, (int) stream_all,+ (int) use_mmq, (int) lay.native, lay.native ? lay.fmt[(size_t) l].gu_type : -1,+ lay.native ? lay.fmt[(size_t) l].d_type : -1);+ }+ }+ const uint8_t* xg_g[strata::kernels::kXmxGroupMax];+ const uint8_t* xg_u[strata::kernels::kXmxGroupMax];+ const uint8_t* xg_d[strata::kernels::kXmxGroupMax];+ int32_t xg_c[strata::kernels::kXmxGroupMax];+ int xg_slots[strata::kernels::kXmxGroupMax];+ int xg_n = 0, xg_ns = 0;+ int64_t xg_r0 = 0;+ auto xflush = [&]() {+ if (xg_n == 0) return;+ if (static bool said = false; !said) {+ said = true;+ std::fprintf(stderr, "strata: prompt experts on the fused grouped XMX (STRATA_PF_XMX=2)\n");+ }+ const auto& f = lay.fmt[(size_t) l];+ int64_t nr = 0;+ for (int i = 0; i < xg_n; ++i) nr += xg_c[i];+ // measured (xmx_group_parity, 16 experts): the fused launch wins below ~200 rows per+ // expert, dequantize + oneMKL above (gate/up 0.67-0.76x at 320); STRATA_PF_XMX_ROWS moves it+ static const int xg_max = [] {+ const char* v = std::getenv("STRATA_PF_XMX_ROWS");+ return v ? std::atoi(v) : 200;+ }();+ if (nr / xg_n < xg_max) {+ pt.mark(kPfGemmGU, cs);+ strata::kernels::iq_xmx_grouped(f.gu_type, xg_g, xg_u, xg_c, xg_n, m.Xs + xg_r0 * N,+ m.GU + xg_r0 * 1280, 1280, (int) N, m.cs);+ swiglu_interleaved(m.GU + xg_r0 * 1280, m.Hh + xg_r0 * 640, nr, m.cs);+ pt.mark(kPfGemmD, cs);+ strata::kernels::iq_xmx_grouped(f.d_type, xg_d, nullptr, xg_c, xg_n, m.Hh + xg_r0 * 640,+ m.Dm + xg_r0 * N, (int) N, 640, m.cs);+ } else {+ int64_t o = xg_r0;+ for (int i = 0; i < xg_n; ++i) {+ const int q = i % DQ;+ pt.mark(kPfDequant, cs);+ strata::kernels::iq_dequant_gu_f16(f.gu_type, xg_g[i], xg_u[i], f.n_ff, f.n_embd,+ m.dq_gu[q], m.cs);+ strata::kernels::iq_dequant_f16(f.d_type, xg_d[i], f.n_embd * f.n_ff, m.dq_d[q], m.cs);+ pt.mark(kPfGemmGU, cs);+ m.gemm.f16(m.Xs + o * N, m.dq_gu[q], m.GU + o * 1280, xg_c[i], 1280, N);+ swiglu_interleaved(m.GU + o * 1280, m.Hh + o * 640, xg_c[i], m.cs);+ pt.mark(kPfGemmD, cs);+ m.gemm.f16(m.Hh + o * 640, m.dq_d[q], m.Dm + o * N, xg_c[i], N, 640);+ o += xg_c[i];+ }+ }+ if (xg_ns > 0) {+ const int rel = xg_slots[xg_ns - 1];+ dpct::sync_barrier(m.used[rel], m.cs);+ for (int i = 0; i < xg_ns; ++i) m.used_of[xg_slots[i]] = rel;+ }+ xg_n = 0;+ xg_ns = 0;+ };// one expert's products from its blob on the device; `slot` (a ring slot, or -1 for a resident// expert) is released once the blob is readauto compute = [&](size_t j, const uint8_t *blob_dev,@@ -3968,7 +4052,20 @@ bool Prefill::run_impl(const int64_t *tokens, int64_t n, int64_t pos0,m.mmq_ctx->run(dn, m.cs);return true;}- const int q = (int) (j % DQ);+ if (xmx2) {+ const auto& f = lay.fmt[(size_t) l];+ if (xg_n == 0) xg_r0 = m.off[(size_t) e];+ xg_g[xg_n] = blob_dev;+ xg_u[xg_n] = blob_dev + f.up_off;+ xg_d[xg_n] = blob_dev + f.down_off;+ xg_c[xg_n] = m.cnt[(size_t) e];+ ++xg_n;+ if (slot >= 0) xg_slots[xg_ns++] = slot;+ if (xg_n == strata::kernels::kXmxGroupMax || j + 1 == order.size()) xflush();+ return true;+ }+ const bool xmx_on = strata::prefill::xmx::mode() == 1;+ const int q = (int) (j % (size_t) dq_slots());if (lay.native) {// plan v0.3 P6: a native pack's layer, dequantized by llama.cpp's own formulasconst auto& f = lay.fmt[(size_t) l];@@ -3982,6 +4079,43 @@ bool Prefill::run_impl(const int64_t *tokens, int64_t n, int64_t pos0,dpct::sync_barrier(m.used[slot], m.cs);m.used_of[slot] = slot;}+ if (xmx_on) {+ if (static bool said = false; !said) {+ said = true;+ std::fprintf(stderr, "strata: prompt experts on the grouped XMX GEMM (STRATA_PF_XMX=1)\n");+ }+ // A770 port: the group's experts sit in dq slots 0..q, their rows back to back from+ // the first one's (the order is by id and so are the offsets)+ if (q + 1 < dq_slots() && j + 1 < order.size()) return true;+ const size_t j0 = j - (size_t) q;+ const int ng = q + 1;+ int32_t gc[strata::prefill::xmx::kMaxGroup];+ const uint16_t* wg[strata::prefill::xmx::kMaxGroup];+ const uint16_t* wd[strata::prefill::xmx::kMaxGroup];+ int64_t nr = 0;+ for (int i = 0; i < ng; ++i) {+ gc[i] = m.cnt[(size_t) order[j0 + (size_t) i]];+ wg[i] = m.dq_gu[i]; wd[i] = m.dq_d[i];+ nr += gc[i];+ }+ const int64_t r0 = m.off[(size_t) order[j0]];+ pt.mark(kPfGemmGU, cs);+ if (nr / ng < strata::prefill::xmx::gu_max_mean()) {+ strata::prefill::xmx::grouped_f16(m.Xs + r0 * N, wg, gc, ng, m.GU + r0 * 1280, 1280,+ (int) N, m.cs);+ } else {+ int64_t o = r0;+ for (int i = 0; i < ng; ++i) {+ m.gemm.f16(m.Xs + o * N, wg[i], m.GU + o * 1280, gc[i], 1280, N);+ o += gc[i];+ }+ }+ swiglu_interleaved(m.GU + r0 * 1280, m.Hh + r0 * 640, nr, m.cs);+ pt.mark(kPfGemmD, cs);+ strata::prefill::xmx::grouped_f16(m.Hh + r0 * 640, wd, gc, ng, m.Dm + r0 * N, (int) N, 640,+ m.cs);+ return true;+ }const int64_t o0 = m.off[(size_t) e], ne = m.cnt[(size_t) e];pt.mark(kPfGemmGU, cs);m.gemm.f16(m.Xs + o0 * N, m.dq_gu[q], m.GU + o0 * 1280, ne, 1280, N);@@ -4025,6 +4159,7 @@ bool Prefill::run_impl(const int64_t *tokens, int64_t n, int64_t pos0,try {while (k < kend && seq[k].e < e_stop) {if (gg_nslots > 0) flush(); // the open group's slots get their event first+ if (xg_n > 0) xflush(); // the open group's slots get their event firstconst int sl = (int) (k % (size_t) m.ring);dpct::sync_barrier(m.used[sl], m.cs);m.used_of[sl] = sl;@@ -4053,7 +4188,7 @@ bool Prefill::run_impl(const int64_t *tokens, int64_t n, int64_t pos0,}if (!compute(j, m.stage_dev[sl], sl)) return false;consumed = ++k;- if (!group_gather || gg_nslots == 0) give_back(consumed); // its group was gathered+ if ((!group_gather || gg_nslots == 0) && xg_ns == 0) give_back(consumed); // its group was gathered} else {const bool r0 = m.host_res && m.cache && m.host_res[(size_t) l * m.g->n_expert + e] >= 0;const uint8_t* bp = r0 ? m.cache->device_slot(m.host_res[(size_t) l * m.g->n_expert + e])@@ -4065,6 +4200,8 @@ bool Prefill::run_impl(const int64_t *tokens, int64_t n, int64_t pos0,}}release_to(m.g->n_expert);+ xflush();+ give_back(consumed);}}pt.mark(kPfCombine, cs);diff --git a/sycl/src/prefill/xmx_moe.dp.cpp b/sycl/src/prefill/xmx_moe.dp.cppnew file mode 100644index 000000000..f0418902b--- /dev/null+++ b/sycl/src/prefill/xmx_moe.dp.cpp@@ -0,0 +1,156 @@+// A770 port: grouped FP16 XMX GEMM for the prompt path's experts (STRATA_PF_XMX=1).+// 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]).+// One launch per group instead of one oneMKL call per expert. joint_matrix 8x8x16 (DG2), SLM-tiled, 256 GRF.+// Measured standalone on the A770 (64 experts, gamma-distributed rows, vs per-expert oneMKL): down 6.1x at mean 40+// 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).+#include <sycl/sycl.hpp>+#include <sycl/ext/oneapi/matrix/matrix.hpp>+#include <sycl/ext/intel/experimental/grf_size_properties.hpp>+#include <cstdlib>+#include "strata/sycl_queue.hpp"+#include "strata/prefill/xmx_moe.hpp"++namespace strata::prefill::xmx {+namespace {+using half = sycl::half;+namespace jm = sycl::ext::oneapi::experimental::matrix;++struct Grp {+ const half* w[kMaxGroup];+ int off[kMaxGroup + 1]; // rows, relative to row0+ int tstart[kMaxGroup + 1]; // first M-tile of each expert+ int n;+};++template <int SGM_, int SGN_, int MT_, int NT_, int KS_>+struct Cfg {+ static constexpr int SGM = SGM_, SGN = SGN_, MT = MT_, NT = NT_, KS = KS_;+ static constexpr int SG = 8;+ static constexpr int BM = SGM * MT * 8, BN = SGN * NT * 8, NSG = SGM * SGN, WG = NSG * SG;+};++template <class C, class Name>+void launch(sycl::queue& q, const half* X, float* Y, const Grp& gp, int ntiles, int N, int K) {+ constexpr int BM = C::BM, BN = C::BN, KS = C::KS, WG = C::WG, SG = C::SG;+ constexpr int LDA = KS + 8, LDB = KS + 8;+ const int nb = N / BN;+ q.submit([&](sycl::handler& h) {+ sycl::local_accessor<half, 1> sa(BM * LDA, h), sb(BN * LDB, h);+ auto props = sycl::ext::oneapi::experimental::properties{sycl::ext::intel::experimental::grf_size<256>};+ h.parallel_for<Name>(sycl::nd_range<2>({(size_t) ntiles, (size_t) nb * WG}, {1, (size_t) WG}), props,+ [=](sycl::nd_item<2> it) [[sycl::reqd_sub_group_size(8)]] {+ auto sg = it.get_sub_group();+ const int t = (int) it.get_group(0);+ int e = 0;+ while (e + 1 < gp.n && t >= gp.tstart[e + 1]) ++e;+ const int m0 = (t - gp.tstart[e]) * BM;+ const int rows = sycl::min(BM, gp.off[e + 1] - gp.off[e] - m0);+ const half* Xe = X + ((size_t) gp.off[e] + m0) * K;+ float* Ye = Y + ((size_t) gp.off[e] + m0) * N;+ const half* We = gp.w[e];+ const int n0 = (int) it.get_group(1) * BN;+ const int lid = (int) it.get_local_id(1), sgid = lid / SG;+ const int sgm = sgid / C::SGN, sgn = sgid % C::SGN;+ jm::joint_matrix<sycl::sub_group, float, jm::use::accumulator, 8, 8> acc[C::MT][C::NT];+ for (int i = 0; i < C::MT; ++i)+ for (int j = 0; j < C::NT; ++j) jm::joint_matrix_fill(sg, acc[i][j], 0.0f);+ half* pa = sa.template get_multi_ptr<sycl::access::decorated::no>().get();+ half* pb = sb.template get_multi_ptr<sycl::access::decorated::no>().get();+ for (int k0 = 0; k0 < K; k0 += KS) {+ constexpr int VA = BM * KS / 8, VB = BN * KS / 8;+ for (int v = lid; v < VA; v += WG) {+ const int r = v / (KS / 8), c = (v % (KS / 8)) * 8;+ sycl::vec<half, 8> x(0);+ if (r < rows) x = *reinterpret_cast<const sycl::vec<half, 8>*>(Xe + (size_t) r * K + k0 + c);+ *reinterpret_cast<sycl::vec<half, 8>*>(pa + r * LDA + c) = x;+ }+ for (int v = lid; v < VB; v += WG) {+ const int r = v / (KS / 8), c = (v % (KS / 8)) * 8;+ *reinterpret_cast<sycl::vec<half, 8>*>(pb + r * LDB + c) =+ *reinterpret_cast<const sycl::vec<half, 8>*>(We + (size_t) (n0 + r) * K + k0 + c);+ }+ it.barrier(sycl::access::fence_space::local_space);+ for (int kk = 0; kk < KS; kk += 16) {+ jm::joint_matrix<sycl::sub_group, half, jm::use::a, 8, 16, jm::layout::row_major> a[C::MT];+ jm::joint_matrix<sycl::sub_group, half, jm::use::b, 16, 8, jm::layout::col_major> b[C::NT];+ for (int i = 0; i < C::MT; ++i)+ jm::joint_matrix_load(sg, a[i], sa.template get_multi_ptr<sycl::access::decorated::no>() ++ (sgm * C::MT * 8 + i * 8) * LDA + kk, LDA);+ for (int j = 0; j < C::NT; ++j)+ jm::joint_matrix_load(sg, b[j], sb.template get_multi_ptr<sycl::access::decorated::no>() ++ (sgn * C::NT * 8 + j * 8) * LDB + kk, LDB);+ for (int i = 0; i < C::MT; ++i)+ for (int j = 0; j < C::NT; ++j) jm::joint_matrix_mad(sg, acc[i][j], a[i], b[j], acc[i][j]);+ }+ it.barrier(sycl::access::fence_space::local_space);+ }+ for (int i = 0; i < C::MT; ++i) {+ const int r = sgm * C::MT * 8 + i * 8;+ if (r >= rows) continue;+ for (int j = 0; j < C::NT; ++j) {+ const int c = n0 + sgn * C::NT * 8 + j * 8;+ if (r + 8 <= rows) {+ jm::joint_matrix_store(sg, acc[i][j],+ sycl::address_space_cast<sycl::access::address_space::global_space, sycl::access::decorated::no>(+ Ye + (size_t) r * N + c), N, jm::layout::row_major);+ } else { // partial rows: through this sub-group's own SLM slice+ float* st = reinterpret_cast<float*>(pa) + sgid * 64;+ jm::joint_matrix_store(sg, acc[i][j],+ sycl::address_space_cast<sycl::access::address_space::local_space, sycl::access::decorated::no>(st),+ 8, jm::layout::row_major);+ sycl::group_barrier(sg);+ const int lim = rows - r, ln = (int) sg.get_local_linear_id();+ for (int rr = 0; rr < lim; ++rr) Ye[(size_t) (r + rr) * N + c + ln] = st[rr * 8 + ln];+ sycl::group_barrier(sg);+ }+ }+ }+ });+ });+}++class k_small; class k_big;+using Small = Cfg<2, 8, 4, 2, 32>; // 64 x 128 tile: few rows per expert+using Big = Cfg<4, 4, 4, 4, 32>; // 128 x 128 tile++template <class C>+int fill(Grp& gp, const int32_t* cnt, int n) {+ int t = 0, o = 0;+ for (int i = 0; i < n; ++i) {+ gp.off[i] = o; gp.tstart[i] = t;+ o += cnt[i]; t += (cnt[i] + C::BM - 1) / C::BM;+ }+ gp.off[n] = o; gp.tstart[n] = t; gp.n = n;+ return t;+}+} // namespace++int mode() {+ static const int v = [] { const char* e = std::getenv("STRATA_PF_XMX"); return e ? std::atoi(e) : 0; }();+ return v;+}+bool enabled() { return mode() != 0; }++int gu_max_mean() {+ static const int v = [] { const char* e = std::getenv("STRATA_PF_XMX_GU_MAX"); return e ? std::atoi(e) : 250; }();+ return v;+}++void grouped_f16(const uint16_t* X, const uint16_t* const* W, const int32_t* cnt, int n, float* Y, int N, int K,+ void* stream) {+ if (n <= 0) return;+ sycl::queue& q = *strata::q_of(stream);+ Grp gp{};+ int64_t tot = 0;+ for (int i = 0; i < n; ++i) { gp.w[i] = reinterpret_cast<const half*>(W[i]); tot += cnt[i]; }+ if (tot <= 0) return;+ const half* Xh = reinterpret_cast<const half*>(X);+ if (tot / n < 96) {+ const int nt = fill<Small>(gp, cnt, n);+ launch<Small, k_small>(q, Xh, Y, gp, nt, N, K);+ } else {+ const int nt = fill<Big>(gp, cnt, n);+ launch<Big, k_big>(q, Xh, Y, gp, nt, N, K);+ }+}+} // namespace strata::prefill::xmx