#include "fused-gemm-esimd.hpp"
#include <sycl/ext/intel/esimd.hpp>
#include <sycl/ext/intel/esimd/xmx/dpas.hpp>
#include <sycl/ext/intel/experimental/grf_size_properties.hpp>
static constexpr int GGML_SYCL_GROUPED_GEMM_KSPLIT = FG_KSPLIT;
static constexpr int GGML_SYCL_GROUPED_GEMM_BN = GGML_SYCL_FG_BN;
static constexpr int GROUPED_GEMM_MR = 32; static constexpr int GROUPED_GEMM_RC = 8; static constexpr int GROUPED_GEMM_QK = 32;
template <int NW_, int MR_, bool L2D_> struct grouped_gemm_arch {
static constexpr int NW = NW_;
static constexpr int MR = MR_;
static constexpr bool L2D = L2D_;
};
using grouped_gemm_xe2 = grouped_gemm_arch<16, GROUPED_GEMM_MR, true>;
using grouped_gemm_xehpg = grouped_gemm_arch<8, 16, false>;
template <typename A> static constexpr bool grouped_gemm_built() {
#if !defined(GGML_SYCL_SUPPORT_XMX)
return false;
#elif defined(GGML_SYCL_XMX_AOT_SG)
return A::NW == GGML_SYCL_XMX_AOT_SG;
#elif defined(FG_AOT_SG)
return A::NW == FG_AOT_SG;
#else
return true;
#endif
}
namespace grouped_gemm_esimd {
using namespace sycl::ext::intel::esimd;
template <int I, int N> struct static_for {
template <typename Fn> static ESIMD_INLINE void run(Fn & f) {
if constexpr (I < N) {
f(std::integral_constant<int, I>{});
static_for<I + 1, N>::run(f);
}
}
};
struct field {
int pre;
int size;
int aos;
};
template <int MR, bool L2D> struct slice {
const uint8_t * x;
int M;
int bpr;
int nblocks;
int m0;
bool aos;
int bs;
ESIMD_INLINE simd<uint32_t, MR> at(const field & f, const int ib, const int inner) const {
const int stride = aos ? bs : f.size;
simd<uint32_t, MR> o(0, (uint32_t) (bpr * stride));
return o + (uint32_t) ((aos ? f.aos : nblocks * f.pre) + (m0 * bpr + ib) * stride + inner);
}
ESIMD_INLINE bool dword_aligned(const field & f, const int inner) const {
return !aos || (bs % 4 == 0 && (f.aos + inner) % 4 == 0);
}
ESIMD_INLINE simd<uint16_t, MR> u16(const field & f, const int ib, const int inner) const {
return gather<uint16_t, MR>((const uint16_t *) x, at(f, ib, inner));
}
ESIMD_INLINE simd<uint32_t, MR> u32(const field & f, const int ib, const int inner) const {
if (dword_aligned(f, inner)) {
return gather<uint32_t, MR>((const uint32_t *) x, at(f, ib, inner));
}
return rows<1>(f, ib, inner);
}
template <int W> ESIMD_INLINE simd<uint32_t, MR * W> rows(const field & f, const int ib, const int inner) const {
if constexpr (L2D) {
if (!aos) {
const unsigned pitch = (unsigned) (bpr * f.size);
return load_2d<uint32_t, W, MR>((const uint32_t *) (x + (size_t) nblocks * f.pre), pitch - 1, M - 1,
pitch - 1, (ib * f.size + inner) / 4, m0);
}
}
const simd<uint32_t, MR> o = at(f, ib, inner);
simd<uint32_t, MR * W> g;
if (dword_aligned(f, inner)) {
g = gather<uint32_t, MR * W, W>((const uint32_t *) x, o);
} else {
const simd<uint32_t, MR> oa = o & ~3u;
const simd<uint32_t, MR> sh = (o & 2u) << 3;
const simd<uint32_t, MR> hm = 0u - (sh >> 4);
simd<uint32_t, MR * W> lo = gather<uint32_t, MR * W, W>((const uint32_t *) x, oa);
const simd<uint32_t, MR> tail = gather<uint32_t, MR>((const uint32_t *) x, oa + ((4u * W) & hm));
auto shift = [&](auto vc) SYCL_ESIMD_FUNCTION {
constexpr int v = decltype(vc)::value;
simd<uint32_t, MR> a = lo.template select<MR, 1>(v * MR);
simd<uint32_t, MR> b;
if constexpr (v + 1 < W) {
b = lo.template select<MR, 1>((v + 1) * MR);
} else {
b = tail;
}
g.template select<MR, 1>(v * MR) = (a >> sh) | ((b << 16) & hm);
};
static_for<0, W>::run(shift);
}
if constexpr (W == 1) {
return g;
} else {
simd<uint32_t, MR * W> t;
auto row = [&](auto rc) SYCL_ESIMD_FUNCTION {
constexpr int r = decltype(rc)::value;
t.template select<W, 1>(r * W) = g.template select<W, MR>(r);
};
static_for<0, MR>::run(row);
return t;
}
}
};
using a_halves = simd<sycl::half, GROUPED_GEMM_RC * GROUPED_GEMM_QK>;
ESIMD_INLINE simd<sycl::half, GROUPED_GEMM_RC * 16> scale_half(simd<float, GROUPED_GEMM_RC * 16> v,
simd<float, GROUPED_GEMM_RC> d) {
v *= d.template replicate_vs_w_hs<GROUPED_GEMM_RC, 1, 16, 0>(0);
return v;
}
template <typename T> ESIMD_INLINE void iq4_pair_table_to_slm() {
static_assert(sizeof(T) == 2, "two values a dword");
simd<uint32_t, 16> lo;
#pragma unroll
for (int i = 0; i < 16; ++i) {
lo[i] = sycl::bit_cast<uint16_t>((T) kvalues_iq4nl[i]);
}
#pragma unroll
for (int hi = 0; hi < 16; ++hi) {
slm_block_store<uint32_t, 16>(hi * 64, lo | ((uint32_t) sycl::bit_cast<uint16_t>((T) kvalues_iq4nl[hi]) << 16));
}
}
template <typename T>
ESIMD_INLINE simd<T, GROUPED_GEMM_RC * GROUPED_GEMM_QK> iq4_codes(simd<uint32_t, GROUPED_GEMM_RC * 4> q) {
simd<uint32_t, GROUPED_GEMM_RC * 16> off = q.template bit_cast_view<uint8_t>();
simd<uint32_t, GROUPED_GEMM_RC * 16> pr = slm_gather<uint32_t, GROUPED_GEMM_RC * 16>(off * 4);
auto pv = pr.template bit_cast_view<T>();
simd<T, GROUPED_GEMM_RC * GROUPED_GEMM_QK> v;
v.template select<GROUPED_GEMM_RC * 16, 1>(0) = pv.template select<GROUPED_GEMM_RC * 16, 2>(0);
v.template select<GROUPED_GEMM_RC * 16, 1>(GROUPED_GEMM_RC * 16) = pv.template select<GROUPED_GEMM_RC * 16, 2>(1);
return v;
}
constexpr bool surface_ok(const int pitch) {
return pitch % 16 == 0 && pitch >= 64;
}
template <int MR> ESIMD_INLINE simd<uint32_t, MR> byte_of(simd<uint32_t, MR> w, const int i) {
return (w >> (8 * i)) & 0xffu;
}
template <int MR> ESIMD_INLINE simd<uint32_t, MR> dword_of(simd<uint32_t, MR> * w, const int i) {
simd<uint32_t, MR> r = w[0];
if (i == 1) {
r = w[1];
} else if (i == 2) {
r = w[2];
} else if (i == 3) {
r = w[3];
}
return r;
}
ESIMD_INLINE simd<float, GROUPED_GEMM_RC * GROUPED_GEMM_QK> byte_codes(simd<uint32_t, GROUPED_GEMM_RC * 8> q) {
auto bm = q.template bit_cast_view<uint8_t, GROUPED_GEMM_RC, GROUPED_GEMM_QK>();
simd<float, GROUPED_GEMM_RC * GROUPED_GEMM_QK> v;
v.template select<GROUPED_GEMM_RC * 16, 1>(0) = bm.template select<GROUPED_GEMM_RC, 1, 16, 1>(0, 0);
v.template select<GROUPED_GEMM_RC * 16, 1>(GROUPED_GEMM_RC * 16) =
bm.template select<GROUPED_GEMM_RC, 1, 16, 1>(0, 16);
return v;
}
ESIMD_INLINE simd<sycl::half, GROUPED_GEMM_RC * 16> scale_min_half(simd<float, GROUPED_GEMM_RC * 16> v,
simd<float, GROUPED_GEMM_RC> d,
simd<float, GROUPED_GEMM_RC> m) {
v = v * d.template replicate_vs_w_hs<GROUPED_GEMM_RC, 1, 16, 0>(0) -
m.template replicate_vs_w_hs<GROUPED_GEMM_RC, 1, 16, 0>(0);
return v;
}
ESIMD_INLINE a_halves halves_dm(simd<uint32_t, GROUPED_GEMM_RC * 8> q,
simd<float, GROUPED_GEMM_RC> d0,
simd<float, GROUPED_GEMM_RC> m0,
simd<float, GROUPED_GEMM_RC> d1,
simd<float, GROUPED_GEMM_RC> m1,
const float bias = 0.0f) {
simd<float, GROUPED_GEMM_RC * GROUPED_GEMM_QK> c = byte_codes(q);
if (bias != 0.0f) {
c -= bias;
}
a_halves v;
v.template select<GROUPED_GEMM_RC * 16, 1>(0) =
scale_min_half(c.template select<GROUPED_GEMM_RC * 16, 1>(0), d0, m0);
v.template select<GROUPED_GEMM_RC * 16, 1>(GROUPED_GEMM_RC * 16) =
scale_min_half(c.template select<GROUPED_GEMM_RC * 16, 1>(GROUPED_GEMM_RC * 16), d1, m1);
return v;
}
ESIMD_INLINE a_halves grid_signed(simd<uint32_t, GROUPED_GEMM_RC * 8> ge,
simd<uint32_t, GROUPED_GEMM_RC> signs,
simd<float, GROUPED_GEMM_RC> d) {
auto gm = ge.template bit_cast_view<uint32_t, GROUPED_GEMM_RC, 8>();
simd<uint32_t, GROUPED_GEMM_RC * 16> sg = signs.template replicate_vs_w_hs<GROUPED_GEMM_RC, 1, 16, 0>(0);
simd<uint32_t, GROUPED_GEMM_RC * 16> k(0, 1);
k &= 15;
a_halves v;
#pragma unroll
for (int h = 0; h < 2; ++h) {
simd<uint32_t, GROUPED_GEMM_RC * 4> eh = gm.template select<GROUPED_GEMM_RC, 1, 4, 1>(0, 4 * h);
simd<float, GROUPED_GEMM_RC * 16> f = eh.template bit_cast_view<uint8_t>();
f.merge(-f, ((sg >> (k + 16 * h)) & 1) != 0);
v.template select<GROUPED_GEMM_RC * 16, 1>(h * GROUPED_GEMM_RC * 16) = scale_half(f, d);
}
return v;
}
}
template <typename block_q_t, int MR = GROUPED_GEMM_MR> struct grouped_gemm_esimd_a {
static constexpr bool supported = false;
};
template <int MR> struct grouped_gemm_esimd_a<block_q8_0, MR> {
static constexpr bool supported = true;
static constexpr int qk = QK8_0;
static constexpr bool whole = true; static constexpr int slm_bytes = 0;
static ESIMD_INLINE void init_slm() {}
struct raw {
grouped_gemm_esimd::simd<uint32_t, MR * 8> q;
grouped_gemm_esimd::simd<uint16_t, MR> d;
};
static constexpr grouped_gemm_esimd::field QS = { 0, QK8_0, 2 }, D = { QK8_0, 2, 0 };
static bool ok(const int K, const bool aos) { return K % qk == 0 && (aos || K >= 64); }
struct sb_t {};
template <typename Sl> static ESIMD_INLINE void load_sb(sb_t &, const Sl &, const int) {}
template <typename Sl> static ESIMD_INLINE void load(raw & r, sb_t &, const Sl & s, const int kb, const int) {
r.q = s.template rows<8>(QS, kb, 0);
r.d = s.u16(D, kb, 0);
}
static ESIMD_INLINE grouped_gemm_esimd::simd<sycl::half, MR * GROUPED_GEMM_QK> decode_all(raw & r) {
using namespace grouped_gemm_esimd;
simd<sycl::half, MR * GROUPED_GEMM_QK> av = r.q.template bit_cast_view<int8_t>();
simd<sycl::half, MR> dh = r.d.template bit_cast_view<sycl::half>();
av *= dh.template replicate_vs_w_hs<MR, 1, GROUPED_GEMM_QK, 0>(0);
return av;
}
};
template <int MR> struct grouped_gemm_esimd_a<block_iq4_nl, MR> {
static constexpr bool supported = true;
static constexpr int qk = QK4_NL;
static constexpr bool whole = false;
static constexpr int slm_bytes = 256 * sizeof(uint32_t);
static ESIMD_INLINE void init_slm() { grouped_gemm_esimd::iq4_pair_table_to_slm<sycl::half>(); }
struct raw {
grouped_gemm_esimd::simd<uint32_t, MR * 4> q;
grouped_gemm_esimd::simd<sycl::half, MR> d;
};
static constexpr grouped_gemm_esimd::field QS = { 0, QK4_NL / 2, 2 }, D = { QK4_NL / 2, 2, 0 };
static bool ok(const int K, const bool aos) { return K % qk == 0 && (aos || K / 2 >= 64); }
struct sb_t {};
template <typename Sl> static ESIMD_INLINE void load_sb(sb_t &, const Sl &, const int) {}
template <typename Sl> static ESIMD_INLINE void load(raw & r, sb_t &, const Sl & s, const int kb, const int) {
using namespace grouped_gemm_esimd;
r.q = s.template rows<4>(QS, kb, 0);
simd<uint16_t, MR> d = s.u16(D, kb, 0);
r.d = d.template bit_cast_view<sycl::half>();
}
template <int g> static ESIMD_INLINE grouped_gemm_esimd::a_halves decode(raw & r) {
using namespace grouped_gemm_esimd;
a_halves v = iq4_codes<sycl::half>(r.q.template select<GROUPED_GEMM_RC * 4, 1>(g * GROUPED_GEMM_RC * 4));
simd<sycl::half, GROUPED_GEMM_RC> dg = r.d.template select<GROUPED_GEMM_RC, 1>(g * GROUPED_GEMM_RC);
simd<sycl::half, GROUPED_GEMM_RC * 16> dr = dg.template replicate_vs_w_hs<GROUPED_GEMM_RC, 1, 16, 0>(0);
v.template select<GROUPED_GEMM_RC * 16, 1>(0) *= dr;
v.template select<GROUPED_GEMM_RC * 16, 1>(GROUPED_GEMM_RC * 16) *= dr;
return v;
}
};
template <int MR> struct grouped_gemm_esimd_a<block_iq4_xs, MR> {
static constexpr bool supported = true;
static constexpr int qk = QK_K;
static constexpr bool whole = false;
static constexpr int slm_bytes = 256 * sizeof(uint32_t);
static ESIMD_INLINE void init_slm() { grouped_gemm_esimd::iq4_pair_table_to_slm<int16_t>(); }
struct raw {
grouped_gemm_esimd::simd<uint32_t, MR * 4> q;
grouped_gemm_esimd::simd<float, MR> d;
};
struct sb_t {
grouped_gemm_esimd::simd<float, MR> d;
grouped_gemm_esimd::simd<uint32_t, MR> sh; grouped_gemm_esimd::simd<uint32_t, MR> sl; };
static constexpr grouped_gemm_esimd::field QS = { 0, QK_K / 2, 8 }, D = { QK_K / 2, 2, 0 },
SH = { QK_K / 2 + 2, 2, 2 }, SL = { QK_K / 2 + 4, 4, 4 };
static bool ok(const int K, const bool) { return K % qk == 0; }
template <typename Sl> static ESIMD_INLINE void load_sb(sb_t & sb, const Sl & s, const int ib) {
using namespace grouped_gemm_esimd;
simd<uint16_t, MR> dh = s.u16(D, ib, 0);
sb.d = simd<sycl::half, MR>(dh.template bit_cast_view<sycl::half>());
sb.sh = s.u16(SH, ib, 0);
sb.sl = s.u32(SL, ib, 0);
}
template <typename Sl> static ESIMD_INLINE void load(raw & r, sb_t & sb, const Sl & s, const int kb, const int j) {
using namespace grouped_gemm_esimd;
r.q = s.template rows<4>(QS, kb / 8, 16 * j);
simd<int32_t, MR> sc = simd<int32_t, MR>(((sb.sl >> (4 * j)) & 0xf) | (((sb.sh >> (2 * j)) & 3) << 4)) - 32;
r.d = sb.d * simd<float, MR>(sc);
}
template <int g> static ESIMD_INLINE grouped_gemm_esimd::a_halves decode(raw & r) {
using namespace grouped_gemm_esimd;
simd<int16_t, GROUPED_GEMM_RC * GROUPED_GEMM_QK> c =
iq4_codes<int16_t>(r.q.template select<GROUPED_GEMM_RC * 4, 1>(g * GROUPED_GEMM_RC * 4));
simd<float, GROUPED_GEMM_RC> dg = r.d.template select<GROUPED_GEMM_RC, 1>(g * GROUPED_GEMM_RC);
a_halves v;
v.template select<GROUPED_GEMM_RC * 16, 1>(0) = scale_half(c.template select<GROUPED_GEMM_RC * 16, 1>(0), dg);
v.template select<GROUPED_GEMM_RC * 16, 1>(GROUPED_GEMM_RC * 16) =
scale_half(c.template select<GROUPED_GEMM_RC * 16, 1>(GROUPED_GEMM_RC * 16), dg);
return v;
}
};
template <int MR> struct grouped_gemm_esimd_a<block_iq3_s, MR> {
static constexpr bool supported = true;
static constexpr int qk = QK_K;
static constexpr bool whole = false;
static constexpr int slm_bytes = 512 * sizeof(uint32_t);
static ESIMD_INLINE void init_slm() {
using namespace grouped_gemm_esimd;
#pragma unroll
for (int i = 0; i < 512; i += 64) {
slm_block_store<uint32_t, 64>(i * 4, block_load<uint32_t, 64>(iq3s_grid + i));
}
}
struct raw {
grouped_gemm_esimd::simd<uint32_t, MR * 2> q; grouped_gemm_esimd::simd<uint32_t, MR> qh; grouped_gemm_esimd::simd<uint32_t, MR> signs; grouped_gemm_esimd::simd<float, MR> d;
};
struct sb_t {
grouped_gemm_esimd::simd<float, MR> d;
grouped_gemm_esimd::simd<uint32_t, MR> sc; };
static constexpr int META = QK_K / 4 + QK_K / 32 + QK_K / 8;
static constexpr grouped_gemm_esimd::field QS = { 0, QK_K / 4, 2 }, QH = { QK_K / 4, QK_K / 32, 2 + QK_K / 4 },
SG = { QK_K / 4 + QK_K / 32, QK_K / 8, 2 + QK_K / 4 + QK_K / 32 },
MD = { META, 2 + IQ3S_N_SCALE, 0 },
MS = { META, 2 + IQ3S_N_SCALE, META };
static bool ok(const int K, const bool aos) {
return K % qk == 0 && (aos || ((K / QK_K * 8) % 16 == 0 && K / QK_K * 8 >= 64));
}
template <typename Sl> static ESIMD_INLINE void load_sb(sb_t & sb, const Sl & s, const int ib) {
using namespace grouped_gemm_esimd;
simd<uint16_t, MR> dh = s.u16(MD, ib, 0);
simd<uint32_t, MR> s0 = s.u16(MS, ib, 2);
simd<uint32_t, MR> s1 = s.u16(MS, ib, 4);
sb.d = simd<sycl::half, MR>(dh.template bit_cast_view<sycl::half>());
sb.sc = s0 | (s1 << 16);
}
template <typename Sl> static ESIMD_INLINE void load(raw & r, sb_t & sb, const Sl & s, const int kb, const int j) {
using namespace grouped_gemm_esimd;
const int ib = kb / 8;
r.q = s.template rows<2>(QS, ib, 8 * j);
r.qh = (s.template rows<1>(QH, ib, 4 * (j / 4)) >> (8 * (j % 4))) & 0xffu;
r.signs = s.template rows<1>(SG, ib, 4 * j);
r.d = sb.d * simd<float, MR>(simd<int32_t, MR>(1 + 2 * ((sb.sc >> (4 * j)) & 0xf)));
}
template <int g> static ESIMD_INLINE grouped_gemm_esimd::a_halves decode(raw & r) {
using namespace grouped_gemm_esimd;
simd<uint16_t, GROUPED_GEMM_RC * 8> qb =
r.q.template select<GROUPED_GEMM_RC * 2, 1>(g * GROUPED_GEMM_RC * 2).template bit_cast_view<uint8_t>();
simd<uint32_t, GROUPED_GEMM_RC> qr = r.qh.template select<GROUPED_GEMM_RC, 1>(g * GROUPED_GEMM_RC);
simd<uint32_t, GROUPED_GEMM_RC * 8> qh = qr.template replicate_vs_w_hs<GROUPED_GEMM_RC, 1, 8, 0>(0);
simd<uint32_t, GROUPED_GEMM_RC * 8> j(0, 1);
j &= 7;
simd<uint32_t, GROUPED_GEMM_RC * 8> idx = simd<uint32_t, GROUPED_GEMM_RC * 8>(qb) | (((qh >> j) & 1) << 8);
simd<uint32_t, GROUPED_GEMM_RC * 8> ge = slm_gather<uint32_t, GROUPED_GEMM_RC * 8>(idx * 4);
return grid_signed(ge, r.signs.template select<GROUPED_GEMM_RC, 1>(g * GROUPED_GEMM_RC),
r.d.template select<GROUPED_GEMM_RC, 1>(g * GROUPED_GEMM_RC));
}
};
template <int MR> struct grouped_gemm_esimd_a<block_q4_K, MR> {
static constexpr bool supported = true;
static constexpr int qk = QK_K;
static constexpr bool whole = false;
static constexpr int slm_bytes = 0;
static ESIMD_INLINE void init_slm() {}
struct raw {
grouped_gemm_esimd::simd<uint32_t, MR * 8> q;
grouped_gemm_esimd::simd<float, MR> d, m;
};
struct sb_t {
grouped_gemm_esimd::simd<uint32_t, MR> sc[3]; grouped_gemm_esimd::simd<float, MR> dall, dmin;
};
static constexpr grouped_gemm_esimd::field QS = { 0, QK_K / 2, 16 }, SC = { QK_K / 2, K_SCALE_SIZE, 4 },
DM = { QK_K / 2 + K_SCALE_SIZE, 4, 0 };
static bool ok(const int K, const bool aos) {
return K % qk == 0 && (aos || grouped_gemm_esimd::surface_ok(K / 2));
}
template <typename Sl>
static ESIMD_INLINE void load_sb(sb_t & sb,
const Sl & s,
const int ib,
const grouped_gemm_esimd::field & sc = SC,
const grouped_gemm_esimd::field & dmf = DM) {
using namespace grouped_gemm_esimd;
#pragma unroll
for (int w = 0; w < 3; ++w) {
sb.sc[w] = s.u32(sc, ib, 4 * w);
}
simd<uint32_t, MR> dm = s.u32(dmf, ib, 0);
auto dh = dm.template bit_cast_view<sycl::half>();
sb.dall = simd<sycl::half, MR>(dh.template select<MR, 2>(0));
sb.dmin = simd<sycl::half, MR>(dh.template select<MR, 2>(1));
}
static ESIMD_INLINE void scale_min(sb_t & sb,
const int j,
grouped_gemm_esimd::simd<float, MR> & d,
grouped_gemm_esimd::simd<float, MR> & m) {
using namespace grouped_gemm_esimd;
simd<uint32_t, MR> sc, mb;
if (j < 4) {
sc = byte_of(sb.sc[0], j) & 63;
mb = byte_of(sb.sc[1], j) & 63;
} else {
sc = (byte_of(sb.sc[2], j - 4) & 0xf) | ((byte_of(sb.sc[0], j - 4) >> 6) << 4);
mb = (byte_of(sb.sc[2], j - 4) >> 4) | ((byte_of(sb.sc[1], j - 4) >> 6) << 4);
}
d = sb.dall * simd<float, MR>(sc);
m = sb.dmin * simd<float, MR>(mb);
}
template <typename Sl> static ESIMD_INLINE void load(raw & r, sb_t & sb, const Sl & s, const int kb, const int j) {
using namespace grouped_gemm_esimd;
r.q = (s.template rows<8>(QS, kb / 8, 32 * (j / 2)) >> (4 * (j & 1))) & 0x0F0F0F0Fu;
scale_min(sb, j, r.d, r.m);
}
template <int g> static ESIMD_INLINE grouped_gemm_esimd::a_halves decode(raw & r) {
using namespace grouped_gemm_esimd;
simd<float, GROUPED_GEMM_RC> d = r.d.template select<GROUPED_GEMM_RC, 1>(g * GROUPED_GEMM_RC),
m = r.m.template select<GROUPED_GEMM_RC, 1>(g * GROUPED_GEMM_RC);
return halves_dm(r.q.template select<GROUPED_GEMM_RC * 8, 1>(g * GROUPED_GEMM_RC * 8), d, m, d, m);
}
};
template <int MR> struct grouped_gemm_esimd_a<block_q5_K, MR> {
using q4 = grouped_gemm_esimd_a<block_q4_K, MR>;
static constexpr bool supported = true;
static constexpr int qk = QK_K;
static constexpr bool whole = false;
static constexpr int slm_bytes = 0;
static ESIMD_INLINE void init_slm() {}
using raw = typename q4::raw;
using sb_t = typename q4::sb_t;
static constexpr grouped_gemm_esimd::field QS = { 0, QK_K / 2, 48 }, QH = { QK_K / 2, QK_K / 8, 16 },
SC = { QK_K / 2 + QK_K / 8, K_SCALE_SIZE, 4 },
DM = { QK_K / 2 + QK_K / 8 + K_SCALE_SIZE, 4, 0 };
static bool ok(const int K, const bool aos) {
return K % qk == 0 && (aos || (grouped_gemm_esimd::surface_ok(K / 2) && grouped_gemm_esimd::surface_ok(K / 8)));
}
template <typename Sl> static ESIMD_INLINE void load_sb(sb_t & sb, const Sl & s, const int ib) { q4::load_sb(sb, s, ib, SC, DM); }
template <typename Sl> static ESIMD_INLINE void load(raw & r, sb_t & sb, const Sl & s, const int kb, const int j) {
using namespace grouped_gemm_esimd;
const int ib = kb / 8;
simd<uint32_t, MR * 8> qh = s.template rows<8>(QH, ib, 0);
r.q = ((s.template rows<8>(QS, ib, 32 * (j / 2)) >> (4 * (j & 1))) & 0x0F0F0F0Fu) |
(((qh >> j) & 0x01010101u) << 4);
q4::scale_min(sb, j, r.d, r.m);
}
template <int g> static ESIMD_INLINE grouped_gemm_esimd::a_halves decode(raw & r) {
return q4::template decode<g>(r);
}
};
template <int MR> struct grouped_gemm_esimd_a<block_q6_K, MR> {
static constexpr bool supported = true;
static constexpr int qk = QK_K;
static constexpr bool whole = false;
static constexpr int slm_bytes = 0;
static ESIMD_INLINE void init_slm() {}
struct raw {
grouped_gemm_esimd::simd<uint32_t, MR * 8> q;
grouped_gemm_esimd::simd<float, MR> d0, d1; };
struct sb_t {
grouped_gemm_esimd::simd<uint32_t, MR> sc[4]; grouped_gemm_esimd::simd<float, MR> d;
};
static constexpr grouped_gemm_esimd::field QL = { 0, QK_K / 2, 0 }, QH = { QK_K / 2, QK_K / 4, QK_K / 2 },
SC = { QK_K / 2 + QK_K / 4, QK_K / 16, QK_K / 2 + QK_K / 4 },
D = { QK_K / 2 + QK_K / 4 + QK_K / 16, 2,
QK_K / 2 + QK_K / 4 + QK_K / 16 };
static bool ok(const int K, const bool aos) {
return K % qk == 0 && (aos || (grouped_gemm_esimd::surface_ok(K / 2) && grouped_gemm_esimd::surface_ok(K / 4)));
}
template <typename Sl> static ESIMD_INLINE void load_sb(sb_t & sb, const Sl & s, const int ib) {
using namespace grouped_gemm_esimd;
#pragma unroll
for (int w = 0; w < 4; ++w) {
sb.sc[w] = s.u32(SC, ib, 4 * w);
}
simd<uint16_t, MR> dh = s.u16(D, ib, 0);
sb.d = simd<sycl::half, MR>(dh.template bit_cast_view<sycl::half>());
}
template <typename Sl> static ESIMD_INLINE void load(raw & r, sb_t & sb, const Sl & s, const int kb, const int j) {
using namespace grouped_gemm_esimd;
const int ib = kb / 8, h = j / 4, q = j % 4;
simd<uint32_t, MR * 8> ql = s.template rows<8>(QL, ib, 64 * h + 32 * (q & 1));
simd<uint32_t, MR * 8> qh = s.template rows<8>(QH, ib, 32 * h);
r.q = ((ql >> (4 * (q >> 1))) & 0x0F0F0F0Fu) | (((qh >> (2 * q)) & 0x03030303u) << 4);
simd<uint32_t, MR> w = dword_of(sb.sc, j / 2);
simd<int32_t, MR> s0 = simd<int32_t, MR>(w << (24 - 16 * (j % 2))) >> 24;
simd<int32_t, MR> s1 = simd<int32_t, MR>(w << (16 - 16 * (j % 2))) >> 24;
r.d0 = sb.d * simd<float, MR>(s0);
r.d1 = sb.d * simd<float, MR>(s1);
}
template <int g> static ESIMD_INLINE grouped_gemm_esimd::a_halves decode(raw & r) {
using namespace grouped_gemm_esimd;
simd<float, GROUPED_GEMM_RC> z = 0.0f;
return halves_dm(r.q.template select<GROUPED_GEMM_RC * 8, 1>(g * GROUPED_GEMM_RC * 8),
r.d0.template select<GROUPED_GEMM_RC, 1>(g * GROUPED_GEMM_RC), z,
r.d1.template select<GROUPED_GEMM_RC, 1>(g * GROUPED_GEMM_RC), z, 32.0f);
}
};
template <int MR> struct grouped_gemm_esimd_a<block_q2_K, MR> {
static constexpr bool supported = true;
static constexpr int qk = QK_K;
static constexpr bool whole = false;
static constexpr int slm_bytes = 0;
static ESIMD_INLINE void init_slm() {}
struct raw {
grouped_gemm_esimd::simd<uint32_t, MR * 8> q;
grouped_gemm_esimd::simd<float, MR> d0, m0, d1, m1;
};
struct sb_t {
grouped_gemm_esimd::simd<uint32_t, MR> sc[4];
grouped_gemm_esimd::simd<float, MR> d, dmin;
};
static constexpr grouped_gemm_esimd::field QS = { 0, QK_K / 4, QK_K / 16 }, SC = { QK_K / 4, QK_K / 16, 0 },
DM = { QK_K / 4 + QK_K / 16, 4, QK_K / 4 + QK_K / 16 };
static bool ok(const int K, const bool aos) {
return K % qk == 0 && (aos || grouped_gemm_esimd::surface_ok(K / 4));
}
template <typename Sl> static ESIMD_INLINE void load_sb(sb_t & sb, const Sl & s, const int ib) {
using namespace grouped_gemm_esimd;
#pragma unroll
for (int w = 0; w < 4; ++w) {
sb.sc[w] = s.u32(SC, ib, 4 * w);
}
simd<uint32_t, MR> dm = s.u32(DM, ib, 0);
auto dh = dm.template bit_cast_view<sycl::half>();
sb.d = simd<sycl::half, MR>(dh.template select<MR, 2>(0));
sb.dmin = simd<sycl::half, MR>(dh.template select<MR, 2>(1));
}
template <typename Sl> static ESIMD_INLINE void load(raw & r, sb_t & sb, const Sl & s, const int kb, const int j) {
using namespace grouped_gemm_esimd;
r.q = (s.template rows<8>(QS, kb / 8, 32 * (j / 4)) >> (2 * (j % 4))) & 0x03030303u;
simd<uint32_t, MR> w = dword_of(sb.sc, j / 2);
simd<uint32_t, MR> s0 = byte_of(w, 2 * (j % 2)), s1 = byte_of(w, 2 * (j % 2) + 1);
r.d0 = sb.d * simd<float, MR>(s0 & 0xf);
r.m0 = sb.dmin * simd<float, MR>(s0 >> 4);
r.d1 = sb.d * simd<float, MR>(s1 & 0xf);
r.m1 = sb.dmin * simd<float, MR>(s1 >> 4);
}
template <int g> static ESIMD_INLINE grouped_gemm_esimd::a_halves decode(raw & r) {
using namespace grouped_gemm_esimd;
return halves_dm(r.q.template select<GROUPED_GEMM_RC * 8, 1>(g * GROUPED_GEMM_RC * 8),
r.d0.template select<GROUPED_GEMM_RC, 1>(g * GROUPED_GEMM_RC),
r.m0.template select<GROUPED_GEMM_RC, 1>(g * GROUPED_GEMM_RC),
r.d1.template select<GROUPED_GEMM_RC, 1>(g * GROUPED_GEMM_RC),
r.m1.template select<GROUPED_GEMM_RC, 1>(g * GROUPED_GEMM_RC));
}
};
template <int MR> struct grouped_gemm_esimd_a<block_q3_K, MR> {
static constexpr bool supported = true;
static constexpr int qk = QK_K;
static constexpr bool whole = false;
static constexpr int slm_bytes = 0;
static ESIMD_INLINE void init_slm() {}
struct raw {
grouped_gemm_esimd::simd<uint32_t, MR * 8> q;
grouped_gemm_esimd::simd<float, MR> d0, d1;
};
struct sb_t {
grouped_gemm_esimd::simd<uint32_t, MR> sc[4]; grouped_gemm_esimd::simd<float, MR> d;
};
static constexpr grouped_gemm_esimd::field QS = { 0, QK_K / 4, QK_K / 8 }, HM = { QK_K / 4, QK_K / 8, 0 },
SC = { QK_K / 4 + QK_K / 8, 12, QK_K / 4 + QK_K / 8 },
D = { QK_K / 4 + QK_K / 8 + 12, 2, QK_K / 4 + QK_K / 8 + 12 };
static bool ok(const int K, const bool aos) {
return K % qk == 0 && (aos || (grouped_gemm_esimd::surface_ok(K / 4) && grouped_gemm_esimd::surface_ok(K / 8)));
}
template <typename Sl> static ESIMD_INLINE void load_sb(sb_t & sb, const Sl & s, const int ib) {
using namespace grouped_gemm_esimd;
#pragma unroll
for (int w = 0; w < 3; ++w) {
sb.sc[w] = s.u32(SC, ib, 4 * w);
}
sb.sc[3] = 0;
simd<uint16_t, MR> dh = s.u16(D, ib, 0);
sb.d = simd<sycl::half, MR>(dh.template bit_cast_view<sycl::half>());
}
static ESIMD_INLINE grouped_gemm_esimd::simd<float, MR> scale(sb_t & sb, const int sidx) {
using namespace grouped_gemm_esimd;
simd<uint32_t, MR> lo = (byte_of(dword_of(sb.sc, (sidx % 8) / 4), sidx % 4) >> (4 * (sidx / 8))) & 0xf;
simd<uint32_t, MR> hi = (byte_of(sb.sc[2], sidx % 4) >> (2 * (sidx / 4))) & 3;
return sb.d * simd<float, MR>(simd<int32_t, MR>(lo | (hi << 4)) - 32);
}
template <typename Sl> static ESIMD_INLINE void load(raw & r, sb_t & sb, const Sl & s, const int kb, const int j) {
using namespace grouped_gemm_esimd;
const int ib = kb / 8;
simd<uint32_t, MR * 8> hm = s.template rows<8>(HM, ib, 0);
r.q = ((s.template rows<8>(QS, ib, 32 * (j / 4)) >> (2 * (j % 4))) & 0x03030303u) |
(((hm >> j) & 0x01010101u) << 2);
r.d0 = scale(sb, 2 * j);
r.d1 = scale(sb, 2 * j + 1);
}
template <int g> static ESIMD_INLINE grouped_gemm_esimd::a_halves decode(raw & r) {
using namespace grouped_gemm_esimd;
simd<float, GROUPED_GEMM_RC> z = 0.0f;
return halves_dm(r.q.template select<GROUPED_GEMM_RC * 8, 1>(g * GROUPED_GEMM_RC * 8),
r.d0.template select<GROUPED_GEMM_RC, 1>(g * GROUPED_GEMM_RC), z,
r.d1.template select<GROUPED_GEMM_RC, 1>(g * GROUPED_GEMM_RC), z, 4.0f);
}
};
template <int MR> struct grouped_gemm_esimd_a<block_iq3_xxs, MR> {
static constexpr bool supported = true;
static constexpr int qk = QK_K;
static constexpr bool whole = false;
static constexpr int slm_bytes = 256 * sizeof(uint32_t);
static ESIMD_INLINE void init_slm() {
using namespace grouped_gemm_esimd;
#pragma unroll
for (int i = 0; i < 256; i += 64) {
slm_block_store<uint32_t, 64>(i * 4, block_load<uint32_t, 64>(iq3xxs_grid + i));
}
}
struct raw {
grouped_gemm_esimd::simd<uint32_t, MR * 2> q; grouped_gemm_esimd::simd<uint32_t, MR> aux;
grouped_gemm_esimd::simd<float, MR> d;
};
struct sb_t {
grouped_gemm_esimd::simd<float, MR> d;
};
static constexpr grouped_gemm_esimd::field QS = { 0, 3 * QK_K / 8, 2 }, D = { 3 * QK_K / 8, 2, 0 };
static bool ok(const int K, const bool aos) {
return K % qk == 0 && (aos || grouped_gemm_esimd::surface_ok(K / QK_K * 96));
}
template <typename Sl> static ESIMD_INLINE void load_sb(sb_t & sb, const Sl & s, const int ib) {
using namespace grouped_gemm_esimd;
simd<uint16_t, MR> dh = s.u16(D, ib, 0);
sb.d = simd<sycl::half, MR>(dh.template bit_cast_view<sycl::half>());
}
template <typename Sl> static ESIMD_INLINE void load(raw & r, sb_t & sb, const Sl & s, const int kb, const int j) {
using namespace grouped_gemm_esimd;
const int ib = kb / 8;
r.q = s.template rows<2>(QS, ib, 8 * j);
r.aux = s.template rows<1>(QS, ib, 64 + 4 * j);
r.d = sb.d * (0.5f + simd<float, MR>(r.aux >> 28)) * 0.5f;
}
template <int g> static ESIMD_INLINE grouped_gemm_esimd::a_halves decode(raw & r) {
using namespace grouped_gemm_esimd;
simd<uint32_t, GROUPED_GEMM_RC * 8> qb =
r.q.template select<GROUPED_GEMM_RC * 2, 1>(g * GROUPED_GEMM_RC * 2).template bit_cast_view<uint8_t>();
simd<uint32_t, GROUPED_GEMM_RC * 8> ge = slm_gather<uint32_t, GROUPED_GEMM_RC * 8>(qb * 4);
simd<uint32_t, GROUPED_GEMM_RC> ax = r.aux.template select<GROUPED_GEMM_RC, 1>(g * GROUPED_GEMM_RC);
simd<uint32_t, GROUPED_GEMM_RC> sg = 0;
#pragma unroll
for (int il = 0; il < 4; ++il) {
simd<uint32_t, GROUPED_GEMM_RC> si = (ax >> (7 * il)) & 127;
sg |= (si | ((cbit(si) & 1) << 7)) << (8 * il);
}
return grid_signed(ge, sg, r.d.template select<GROUPED_GEMM_RC, 1>(g * GROUPED_GEMM_RC));
}
};
template <typename block_q_t, int KS, typename T, bool AOS, typename A> struct grouped_gemm_esimd_kernel {
using F = grouped_gemm_esimd_a<block_q_t, A::MR>;
const char * src0_dd;
size_t expert_stride;
const ggml_sycl_grouped_gemm_tile * tiles;
const T * packed;
float * dst;
int M;
int Npad;
int K;
ggml_sycl_grouped_gemm_rows dst_rows;
void operator()(sycl::nd_item<1> it) const SYCL_ESIMD_KERNEL {
if constexpr (grouped_gemm_built<A>()) {
run(it);
}
}
ESIMD_INLINE void run(sycl::nd_item<1> it) const {
using namespace sycl::ext::intel::esimd;
namespace xmx = sycl::ext::intel::esimd::xmx;
constexpr int MR = A::MR;
constexpr int NW = A::NW;
constexpr int RC = GROUPED_GEMM_RC;
constexpr int RG = MR / RC; constexpr int BN = GGML_SYCL_GROUPED_GEMM_BN;
constexpr int NT = BN / NW;
if constexpr (F::slm_bytes > 0) {
slm_init<F::slm_bytes>();
F::init_slm();
}
const int mblocks = M / MR;
const int gid = it.get_global_id(0);
const int t = gid / mblocks;
const int m0 = (gid % mblocks) * MR;
const ggml_sycl_grouped_gemm_tile tile = tiles[t];
if (tile.n1 <= tile.n0) {
return;
}
const int b0 = t * BN;
const int bpr = K / F::qk;
const grouped_gemm_esimd::slice<MR, A::L2D> sl = {
(const uint8_t *) src0_dd + (size_t) tile.expert * expert_stride,
M,
bpr,
M * bpr,
m0,
AOS,
(int) sizeof(block_q_t)
};
const uint32_t * bp = (const uint32_t *) packed;
const unsigned bw = (unsigned) Npad * 4; const int nstep = K / GROUPED_GEMM_QK;
simd<float, RC * NW> acc[RG][NT];
simd<float, RC * NW> total[KS > 1 ? RG : 1][KS > 1 ? NT : 1];
#pragma unroll
for (int g = 0; g < RG; ++g) {
#pragma unroll
for (int nt = 0; nt < NT; ++nt) {
acc[g][nt] = 0.0f;
if constexpr (KS > 1) {
total[g][nt] = 0.0f;
}
}
}
const int ncols = tile.n1 - tile.n0;
simd_mask<BN> cm = simd<int32_t, BN>(0, 1) < ncols;
simd<int32_t, BN> i2 = 0, r1 = 0; if (dst_rows.map) {
const int32_t * mp = (const int32_t *) (dst_rows.map + tile.n0);
simd<uint32_t, BN> mo(0, sizeof(mmid_row_mapping));
simd<int32_t, BN> i1 = gather<int32_t, BN>(mp, mo, cm);
i2 = gather<int32_t, BN>(mp + 1, mo, cm);
r1 = i1 % simd<int32_t, BN>((int32_t) dst_rows.ne1);
}
constexpr int KPB = F::qk / GROUPED_GEMM_QK; int split = 0;
int split_end = KS > 1 ? nstep / KS : nstep;
typename F::sb_t sb;
for (int kb = 0; kb < nstep; ++kb) {
const int j = kb % KPB;
if (j == 0) {
F::load_sb(sb, sl, kb / KPB);
}
typename F::raw r;
F::load(r, sb, sl, kb, j);
simd<uint32_t, 16 * NW> b[NT]; if constexpr (A::L2D) {
#pragma unroll
for (int nt = 0; nt < NT; ++nt) {
b[nt] = load_2d<uint32_t, NW, 16>(bp, bw - 1, K / 2 - 1, bw - 1, b0 + nt * NW, kb * 16);
}
} else {
simd<uint32_t, 16 * BN> bm;
#pragma unroll
for (int r = 0; r < 16; ++r) {
bm.template select<BN, 1>(r * BN) =
block_load<uint32_t, BN>(bp + (size_t) (kb * 16 + r) * Npad + b0, overaligned_tag<16>{});
}
auto bv = bm.template bit_cast_view<uint32_t, 16, BN>();
#pragma unroll
for (int nt = 0; nt < NT; ++nt) {
b[nt] = bv.template select<16, 1, NW, 1>(0, nt * NW);
}
}
simd<sycl::half, MR * GROUPED_GEMM_QK> all;
if constexpr (F::whole) {
all = F::decode_all(r);
}
auto row_group = [&](auto gc) SYCL_ESIMD_FUNCTION {
constexpr int g = decltype(gc)::value;
grouped_gemm_esimd::a_halves av;
if constexpr (F::whole) {
auto am = all.template bit_cast_view<sycl::half, MR, GROUPED_GEMM_QK>();
av.template select<RC * 16, 1>(0) = am.template select<RC, 1, 16, 1>(g * RC, 0);
av.template select<RC * 16, 1>(RC * 16) = am.template select<RC, 1, 16, 1>(g * RC, 16);
} else {
av = F::template decode<g>(r);
}
#pragma unroll
for (int h = 0; h < 2; ++h) {
simd<sycl::half, RC * 16> ah = av.template select<RC * 16, 1>(h * RC * 16);
simd<T, RC * 16> at;
if constexpr (std::is_same_v<T, sycl::half>) {
at = ah;
} else {
at = simd<float, RC * 16>(ah);
}
#pragma unroll
for (int nt = 0; nt < NT; ++nt) {
simd<uint32_t, 8 * NW> bh32 = b[nt].template select<8 * NW, 1>(h * 8 * NW);
simd<T, 16 * NW> bh = bh32.template bit_cast_view<T>();
acc[g][nt] = xmx::dpas<8, RC, float, float>(acc[g][nt], bh, at);
}
}
};
grouped_gemm_esimd::static_for<0, RG>::run(row_group);
if constexpr (KS > 1) {
if (kb + 1 == split_end) {
#pragma unroll
for (int g = 0; g < RG; ++g) {
#pragma unroll
for (int nt = 0; nt < NT; ++nt) {
total[g][nt] += acc[g][nt];
acc[g][nt] = 0.0f;
}
}
++split;
split_end = ((split + 1) * nstep) / KS;
}
}
}
(void) split;
(void) split_end;
#pragma unroll
for (int c = 0; c < BN; ++c) {
if (c >= ncols) {
continue;
}
simd<float, MR> col;
#pragma unroll
for (int g = 0; g < RG; ++g) {
if constexpr (KS > 1) {
col.template select<RC, 1>(g * RC) = total[g][c / NW].template select<RC, NW>(c % NW);
} else {
col.template select<RC, 1>(g * RC) = acc[g][c / NW].template select<RC, NW>(c % NW);
}
}
const int n = tile.n0 + c;
if (dst_rows.map) {
float * out = (float *) (dst_rows.base + (size_t) r1[c] * dst_rows.nb1 + i2[c] * dst_rows.nb2) + m0;
block_store<float, MR>(out, col, element_aligned_tag{});
} else {
block_store<float, MR>(dst + (size_t) n * M + m0, col, element_aligned_tag{});
}
}
}
auto get(syclexp::properties_tag) const { return syclexp::properties{ sycl::ext::intel::experimental::grf_size<256> }; }
};
static bool grouped_gemm_esimd_weights_ok(const char * w, const size_t expert_stride, const bool l2d) {
const size_t a = l2d ? 64 : 4;
return (uintptr_t) w % a == 0 && expert_stride % a == 0;
}
static int ggml_sycl_grouped_gemm_esimd_mode() {
return g_ggml_sycl_mmid_esimd;
}
bool ggml_sycl_grouped_gemm_esimd_enabled() {
#if defined(GGML_SYCL_SUPPORT_XMX)
return g_ggml_sycl_enable_esimd && ggml_sycl_grouped_gemm_esimd_mode() != 0;
#else
return false;
#endif
}
template <typename block_q_t, typename T, bool AOS, typename A>
static void grouped_gemm_esimd_launch(const char * src0_dd,
size_t expert_stride,
const ggml_sycl_grouped_gemm_tile * tiles,
const T * packed,
float * dst,
int M,
int Npad,
int K,
const ggml_sycl_grouped_gemm_rows & dst_rows,
int64_t n_tiles,
dpct::queue_ptr stream) {
const sycl::nd_range<1> range((size_t) n_tiles * (M / A::MR), 1);
if (ggml_sycl_grouped_gemm_esimd_mode() == 2) {
stream->parallel_for(range, grouped_gemm_esimd_kernel<block_q_t, GGML_SYCL_GROUPED_GEMM_KSPLIT, T, AOS, A>{
src0_dd, expert_stride, tiles, packed, dst, M, Npad, K, dst_rows });
} else {
stream->parallel_for(range, grouped_gemm_esimd_kernel<block_q_t, 1, T, AOS, A>{
src0_dd, expert_stride, tiles, packed, dst, M, Npad, K, dst_rows });
}
}
template <typename block_q_t, typename T, typename A>
static bool grouped_gemm_esimd_run(const char * src0_dd,
size_t expert_stride,
const ggml_sycl_grouped_gemm_tile * tiles,
const T * packed,
float * dst,
int M,
int Npad,
int K,
const ggml_sycl_grouped_gemm_rows & dst_rows,
bool reordered,
int64_t n_tiles,
dpct::queue_ptr stream) {
using F = grouped_gemm_esimd_a<block_q_t, A::MR>;
if (M % A::MR != 0 || !F::ok(K, !reordered) || K < GROUPED_GEMM_QK * GGML_SYCL_GROUPED_GEMM_KSPLIT ||
!grouped_gemm_esimd_weights_ok(src0_dd, expert_stride, reordered && A::L2D) || ((uintptr_t) packed % 64) != 0 ||
Npad % 16 != 0) {
return false;
}
if (reordered) {
grouped_gemm_esimd_launch<block_q_t, T, false, A>(src0_dd, expert_stride, tiles, packed, dst, M, Npad, K,
dst_rows, n_tiles, stream);
} else {
grouped_gemm_esimd_launch<block_q_t, T, true, A>(src0_dd, expert_stride, tiles, packed, dst, M, Npad, K,
dst_rows, n_tiles, stream);
}
return true;
}
template <typename block_q_t, typename T, int NW>
bool ggml_sycl_grouped_gemm_esimd(const char * src0_dd,
size_t expert_stride,
const ggml_sycl_grouped_gemm_tile * tiles,
const T * packed,
float * dst,
int M,
int Npad,
int K,
const ggml_sycl_grouped_gemm_rows & dst_rows,
bool reordered,
int64_t n_tiles,
dpct::queue_ptr stream) {
static_assert(grouped_gemm_esimd_a<block_q_t>::supported == ggml_sycl_grouped_gemm_esimd_has<block_q_t>,
"the header's format list and the formats implemented here must agree");
static_assert(NW == 8 || NW == 16, "the dpas widths of Xe-HPG and of Xe2 / Xe-HPC");
if constexpr (NW == 16) {
return grouped_gemm_esimd_run<block_q_t, T, grouped_gemm_xe2>(src0_dd, expert_stride, tiles, packed, dst, M,
Npad, K, dst_rows, reordered, n_tiles, stream);
} else {
return grouped_gemm_esimd_run<block_q_t, T, grouped_gemm_xehpg>(src0_dd, expert_stride, tiles, packed, dst, M,
Npad, K, dst_rows, reordered, n_tiles, stream);
}
}
#define GROUPED_GEMM_ESIMD_INSTANCE_T(block_q_t, T, NW) \
template bool ggml_sycl_grouped_gemm_esimd<block_q_t, T, NW>( \
const char *, size_t, const ggml_sycl_grouped_gemm_tile *, const T *, float *, int, int, int, \
const ggml_sycl_grouped_gemm_rows &, bool, int64_t, dpct::queue_ptr);
#define GROUPED_GEMM_ESIMD_INSTANCE(block_q_t) \
GROUPED_GEMM_ESIMD_INSTANCE_T(block_q_t, sycl::half, 16) \
GROUPED_GEMM_ESIMD_INSTANCE_T(block_q_t, sycl::ext::oneapi::bfloat16, 16) \
GROUPED_GEMM_ESIMD_INSTANCE_T(block_q_t, sycl::half, 8) \
GROUPED_GEMM_ESIMD_INSTANCE_T(block_q_t, sycl::ext::oneapi::bfloat16, 8)
GROUPED_GEMM_ESIMD_INSTANCE(block_q8_0)
GROUPED_GEMM_ESIMD_INSTANCE(block_iq4_nl)
GROUPED_GEMM_ESIMD_INSTANCE(block_iq4_xs)
GROUPED_GEMM_ESIMD_INSTANCE(block_iq3_s)
GROUPED_GEMM_ESIMD_INSTANCE(block_iq3_xxs)
GROUPED_GEMM_ESIMD_INSTANCE(block_q3_K)
GROUPED_GEMM_ESIMD_INSTANCE(block_q2_K)
GROUPED_GEMM_ESIMD_INSTANCE(block_q6_K)
GROUPED_GEMM_ESIMD_INSTANCE(block_q5_K)
GROUPED_GEMM_ESIMD_INSTANCE(block_q4_K)