Commit e07f2c6cb for llama.cpp
commit e07f2c6cb5a89f7a6439f39656fc24d2ad80455e
Author: cwriter <13705245+cwriter@users.noreply.github.com>
Date: Sun Dec 6 21:05:05 2026 +0100
sycl: XMX enablement 1/4 - XMX Compat layer (#32728)
Co-authored-by: cwriter <cwriter@localhost>
diff --git a/docs/backend/SYCL.md b/docs/backend/SYCL.md
index 2cd5b1ee1..70ccb570c 100644
--- a/docs/backend/SYCL.md
+++ b/docs/backend/SYCL.md
@@ -788,6 +788,7 @@ User can use the device management in [docs/multi-gpu.md](https://github.com/ggm
| GGML_SYCL_SUPPORT_XMX | ON *(default)* \|OFF *(Optional)* | Build the code paths that use XMX (DPAS) instructions. Set OFF for AOT builds targeting iGPUs without XMX, which otherwise fail to compile. |
| GGML_SYCL_GRAPH | ON *(default)* \|OFF *(Optional)* | Enable build with [SYCL Graph extension](https://github.com/intel/llvm/blob/sycl/sycl/doc/extensions/experimental/sycl_ext_oneapi_graph.asciidoc). |
| GGML_SYCL_DNN | ON *(default)* \|OFF *(Optional)* | Enable build with oneDNN. |
+| GGML_SYCL_XMX_EMULATION | OFF *(default)* \|ON *(debug)* | Compute the XMX instructions (ESIMD `dpas`, `joint_matrix`) in software (`xmx-compat.hpp`), so kernels run on devices that lack their XMX shape, e.g. the 8-wide Xe-HPG kernels on Xe2 (see `GGML_SYCL_XMX_EMULATED_HW`); a `joint_matrix` kernel of a sub-group size the device lacks runs on logical sub-groups. Slow. |
| GGML_SYCL_HOST_MEM_FALLBACK | ON *(default)* \|OFF *(Optional)* | Allow host memory fallback when device memory is full during quantized weight reorder. Enables inference to continue at reduced speed (reading over PCIe) instead of failing. Requires Linux kernel 6.8+. |
| GGML_SYCL_SUPPORT_LEVEL_ZERO_API | ON *(default)* \|OFF *(Optional)* | Support to use Level Zero API for device memory allocation. Requires Level Zero headers/library at build time and Intel GPU driver (Level Zero runtime) at run time. Reduces system RAM usage during multi-GPU inference. SYCL backend always runs on Level Zero running time even if it's set as OFF (the SYCL API is then used for memory allocation).|
| CMAKE_C_COMPILER | `icx` *(Linux)*, `icx/cl` *(Windows)* | Set `icx` compiler for SYCL code path. |
@@ -831,6 +832,7 @@ User can use the device management in [docs/multi-gpu.md](https://github.com/ggm
| GGML_SYCL_XMX_GATHER_TYPES | decimal bitmask, all bits set (default) | Weight formats that may use the XMX dequant-GEMM paths, which dequantize weights straight into the XMX tiles. This speeds up prompt processing of MoE models on GPUs with XMX units (Arc A- and B-series, Arc Pro, Data Center GPU Max), for example pp512 of Qwen3-30B-A3B UD-IQ3_XXS by about 50% on an Arc Pro B60. Bits:<br>* 1: IQ4_NL, 2: IQ3_S, 4: IQ4_XS, 8: IQ3_XXS, 16: IQ2_XXS, 32: IQ2_XS, 64: IQ2_S, 128: IQ1_S, 256: IQ1_M<br>* 512: Q8_0, 1024: Q4_K, 2048: Q5_K, 4096: Q6_K, 8192: Q2_K, 16384: Q3_K (MoE `MUL_MAT_ID` only)<br>Add values to combine them, for example `3` for IQ4_NL and IQ3_S; `0` disables the paths. A set bit does not force the path: batches of more than 64 tokens per expert use the library GEMM unless the device schedule width heuristic is overridden with `GGML_SYCL_MMID_SCHED=3`. Row lengths must be a multiple of 256 (32 for IQ4_NL and Q8_0). |
| GGML_SYCL_MMID_ESIMD | 0, 1 (default), 2 | The `MUL_MAT_ID` grouped XMX dequant-GEMM as an ESIMD kernel that dequantizes the weights straight into the `dpas` register operands, with no shared local memory image of them, from either weight layout (the per-expert reorder or the canonical blocks). q8_0, iq4_nl, iq4_xs, iq3_s, iq3_xxs and q2_K to q6_K. It stands in for the 8x16x16 sub-group-16 combination of f16 or bf16 (Xe2, Xe-HPC) and for the 8x8x16 sub-group-8 one (Xe-HPG: Arc A-series, ARL-H; there it works on 16 rows a thread and reads with 1D block loads, since that architecture has half the register file and no 2D block loads); while it is on, a grouped call whose weight type it covers is moved to that combination from any other shape of its type and sub-group size, whatever `GGML_SYCL_XMX_GATHER_SHAPES` allows, because the shape bits choose among `joint_matrix` tiles and the ESIMD kernel tiles on its own. A call it declines runs that combination's `joint_matrix` kernel. 0 uses the `joint_matrix` kernel, 1 the ESIMD one, 2 the ESIMD one summing K in the `joint_matrix` kernel's order (bit-identical output for f16; slower). An AOT build gets its 256-register file through `-ze-opt-enable-auto-large-GRF-mode` (set by the CMake build). Not built with `GGML_SYCL_SUPPORT_XMX=OFF`. |
| GGML_SYCL_MMID_SCHED | decimal bitmask, 1 (default) | Build the grouped MoE routing and tile schedule on the GPU, avoiding the expert-ID host readback and CPU sort.<br>* 1: enable the device schedule.<br>* 2: override the fallback for models with few routed experts by disabling the performance heuristic that rejects batches averaging more than 64 routed rows per expert. This may reduce performance; type, shape, precision and device capability checks still apply.<br>Use `3` to enable the device schedule and override this heuristic, or `0` to use the host schedule. Bit 2 alone does not enable the device schedule. Higher bits are ignored. |
+| GGML_SYCL_XMX_EMULATED_HW | decimal bitmask, 0 (default) | In a `GGML_SYCL_XMX_EMULATION` build (ignored otherwise), adds the XMX combinations of other architectures to the device's own, so `GGML_SYCL_XMX_GATHER_SHAPES` can pick their kernels on this device: 1: Xe-HPG (Arc A-series, ARL-H; the sub-group-8 combinations), 2: Xe2, Xe3, Xe-HPC (the sub-group-16 ones). The device's own combinations stay available, so 0 is the real device. Example, the Xe-HPG f16 kernel on Xe2: `GGML_SYCL_XMX_EMULATED_HW=1 GGML_SYCL_XMX_GATHER_SHAPES=16`. |
| GGML_SYCL_XMX_GATHER_SHAPES | decimal bitmask, 255 (default) | XMX `joint_matrix` combinations the paths of `GGML_SYCL_XMX_GATHER_TYPES` may use; the operand type comes from `GGML_SYCL_DYNAMIC_PRECISION` and the best supported combination is picked automatically (logged as `fg_pick_combo`). Bits:<br>* Xe2, Xe3, Xe-HPC: 1: f16 8x16x16, 2: f16 16x16x16, 4: f16 32x64x16, 8: f16 32x64x32, 32: tf32 8x16x8, 64: bf16 8x16x16<br>* Xe-HPG (Arc A770, ARL-H): 16: f16 8x8x16, 128: bf16 8x8x16<br>Clear a bit to exclude a combination, or set a single bit to force one for testing. |
| GGML_SYCL_DYNAMIC_PRECISION | `F16` (default with `GGML_SYCL_F16=ON`), `BF16`, `TF32` or `F32` (default otherwise) | Operand type of the XMX dequant-GEMM paths (`GGML_SYCL_XMX_GATHER_TYPES`); accumulation is always f32. `F16` is the fastest, but activations above 65504 overflow. `BF16` keeps the f32 range at a 7-bit mantissa, `TF32` keeps the range and the f16 mantissa but is about 30% slower and needs Xe2, Xe3 or Xe-HPC, and `F32` turns the XMX paths off. Ops that request a higher src1 precision ([TAG_GGML_PREC]) get it regardless of this setting. |
| GGML_SYCL_DYNAMIC_REQUIRED_PRECISION | `F32` (default), `TF32`, `BF16` or `F16` | Lowest type the XMX paths may use for an op that requests an F32 src1, such as Mistral 4 `ffn_down_exps`. The default runs such ops on the library f32 GEMM; `TF32` or `BF16` trade mantissa for speed while keeping the f32 range. `F16` ignores the request and can overflow; it is meant for testing only. |
diff --git a/ggml/CMakeLists.txt b/ggml/CMakeLists.txt
index ff567c020..199044080 100644
--- a/ggml/CMakeLists.txt
+++ b/ggml/CMakeLists.txt
@@ -257,6 +257,7 @@ option(GGML_SYCL_GRAPH "ggml: enable graphs in the SYCL bac
option(GGML_SYCL_HOST_MEM_FALLBACK "ggml: allow host memory fallback in SYCL reorder (requires kernel 6.8+)" ON)
option(GGML_SYCL_SUPPORT_LEVEL_ZERO_API "ggml: use Level Zero API in SYCL backend" ON)
option(GGML_SYCL_DNN "ggml: enable oneDNN in the SYCL backend" ON)
+option(GGML_SYCL_XMX_EMULATION "ggml: compute XMX (dpas, joint_matrix) in software in the SYCL backend, for debugging" OFF)
set (GGML_SYCL_TARGET "INTEL" CACHE STRING
"ggml: sycl target device")
set (GGML_SYCL_DEVICE_ARCH "" CACHE STRING
diff --git a/ggml/src/ggml-sycl/CMakeLists.txt b/ggml/src/ggml-sycl/CMakeLists.txt
index 0a5aede0b..9d438a354 100644
--- a/ggml/src/ggml-sycl/CMakeLists.txt
+++ b/ggml/src/ggml-sycl/CMakeLists.txt
@@ -216,6 +216,11 @@ if (GGML_SYCL_SUPPORT_XMX)
add_compile_definitions(GGML_SYCL_SUPPORT_XMX)
endif()
+if (GGML_SYCL_XMX_EMULATION)
+ # xmx-compat.hpp: dpas and joint_matrix in software, so kernels run on devices without their XMX shape
+ add_compile_definitions(GGML_SYCL_XMX_EMULATION)
+endif()
+
if (GGML_SYCL_TARGET STREQUAL "INTEL")
add_compile_definitions(GGML_SYCL_WARP_SIZE=16)
# With AOT the option goes to ocloc inside -options (see GGML_SYCL_DEVICE_ARCH below): a bare
diff --git a/ggml/src/ggml-sycl/common.hpp b/ggml/src/ggml-sycl/common.hpp
index f9b7a2821..236f53810 100644
--- a/ggml/src/ggml-sycl/common.hpp
+++ b/ggml/src/ggml-sycl/common.hpp
@@ -112,6 +112,14 @@ extern int g_ggml_sycl_mmid_sched;
static constexpr int GGML_SYCL_XMX_GATHER_SHAPES_DEFAULT = 0xff;
extern int g_ggml_sycl_xmx_gather_shapes;
+// GGML_SYCL_XMX_EMULATED_HW: architectures whose XMX combinations a GGML_SYCL_XMX_EMULATION build offers next to
+// the device's own, one bit each; GGML_SYCL_XMX_GATHER_SHAPES then chooses among all of them
+enum {
+ GGML_SYCL_XMX_EMULATED_HW_XEHPG = 1 << 0, // Arc A-series, ARL-H: the sub-group 8 combinations
+ GGML_SYCL_XMX_EMULATED_HW_XE2 = 1 << 1, // Xe2, Xe3, Xe-HPC: the sub-group 16 combinations
+};
+extern int g_ggml_sycl_xmx_emulated_hw;
+
// GGML_SYCL_DYNAMIC_PRECISION: operand type of the XMX dequant-GEMM paths. F32 turns them off and
// keeps the library GEMM in f32. A src1 precision request of an op [TAG_GGML_PREC] is always met.
enum ggml_sycl_dynamic_precision {
diff --git a/ggml/src/ggml-sycl/esimd.hpp b/ggml/src/ggml-sycl/esimd.hpp
index 568173e7a..9922e20e0 100644
--- a/ggml/src/ggml-sycl/esimd.hpp
+++ b/ggml/src/ggml-sycl/esimd.hpp
@@ -5,6 +5,7 @@
#include <sycl/ext/intel/experimental/grf_size_properties.hpp>
#include "common.hpp"
+#include "xmx-compat.hpp"
#ifdef GGML_SYCL_MMVQ_HAS_XMX
#include <sycl/ext/intel/esimd/xmx/dpas.hpp>
@@ -613,9 +614,11 @@ inline bool xmx_supported(const void * vx, const int ncols) {
// transposed 2D block load of 16 rows, dword (kd, n) lands at [kd * 16 + n]
// 2D block loads need rows of at least 64 bytes, shorter rows (SR) are read row by row
-template <int W, bool SR>
+// HW: the architecture the kernel is built for (ggml_sycl_xmx::xe2)
+template <typename HW, int W, bool SR>
ESIMD_INLINE simd<uint32_t, W * XMX_ROWS> xmx_load_t(const void * base, unsigned pitch, unsigned h, int x, int y) {
using namespace sycl::ext::intel::esimd;
+ namespace xmx = ggml_sycl_xmx::esimd;
if constexpr (SR) {
simd<uint32_t, W * XMX_ROWS> r;
for (int n = 0; n < XMX_ROWS; ++n) {
@@ -625,7 +628,7 @@ ESIMD_INLINE simd<uint32_t, W * XMX_ROWS> xmx_load_t(const void * base, unsigned
}
return r;
} else {
- return load_2d<uint32_t, W, XMX_ROWS, 1, true, false>((const uint32_t *) base, pitch, h, pitch, x, y);
+ return xmx::load_2d<HW, uint32_t, W, XMX_ROWS, 1, true, false>((const uint32_t *) base, pitch, h, pitch, x, y);
}
}
@@ -736,11 +739,12 @@ template <bool SR> struct xmx_traits_q6_k : xmx_traits_base<SR, 7, xmx_scale_kin
};
// rows: clamped row index of each of the 16 lanes
+ template <typename HW>
static ESIMD_INLINE scales_t load_scales(const ctx & c, int sb, const simd<uint32_t, 16> & rows) {
using namespace sycl::ext::intel::esimd;
scales_t s;
s.dv = convert<float>(gather<sycl::half, 16>(c.dh, (rows * c.bpr + sb) * (uint32_t) sizeof(sycl::half)));
- simd<uint32_t, 64> raw = xmx_load_t<4, SR>(c.scales, c.ps, c.h, sb * 4, c.row0);
+ simd<uint32_t, 64> raw = xmx_load_t<HW, 4, SR>(c.scales, c.ps, c.h, sb * 4, c.row0);
s.sc8 = raw.template bit_cast_view<int8_t>();
return s;
}
@@ -749,11 +753,11 @@ template <bool SR> struct xmx_traits_q6_k : xmx_traits_base<SR, 7, xmx_scale_kin
simd<uint32_t, 128> a, b, h;
};
- static ESIMD_INLINE part_t load_part(const ctx & c, int sb, int part) {
+ template <typename HW> static ESIMD_INLINE part_t load_part(const ctx & c, int sb, int part) {
part_t p;
- p.a = xmx_load_t<8, SR>(c.ql, c.pl, c.h, sb * 32 + part * 16, c.row0);
- p.b = xmx_load_t<8, SR>(c.ql, c.pl, c.h, sb * 32 + part * 16 + 8, c.row0);
- p.h = xmx_load_t<8, SR>(c.qh, c.ph, c.h, sb * 16 + part * 8, c.row0);
+ p.a = xmx_load_t<HW, 8, SR>(c.ql, c.pl, c.h, sb * 32 + part * 16, c.row0);
+ p.b = xmx_load_t<HW, 8, SR>(c.ql, c.pl, c.h, sb * 32 + part * 16 + 8, c.row0);
+ p.h = xmx_load_t<HW, 8, SR>(c.qh, c.ph, c.h, sb * 16 + part * 8, c.row0);
return p;
}
@@ -799,6 +803,7 @@ template <bool SR> struct xmx_traits_q3_k : xmx_traits_base<SR, 16, xmx_scale_ki
using scales_t = xmx_row_scales<16>;
+ template <typename HW>
static ESIMD_INLINE scales_t load_scales(const ctx & c, int sb, const simd<uint32_t, 16> & rows) {
using namespace sycl::ext::intel::esimd;
scales_t s;
@@ -822,10 +827,10 @@ template <bool SR> struct xmx_traits_q3_k : xmx_traits_base<SR, 16, xmx_scale_ki
simd<uint32_t, 128> q, h;
};
- static ESIMD_INLINE part_t load_part(const ctx & c, int sb, int part) {
+ template <typename HW> static ESIMD_INLINE part_t load_part(const ctx & c, int sb, int part) {
part_t p;
- p.q = xmx_load_t<8, SR>(c.qs, c.pq, c.h, sb * 16 + part * 8, c.row0);
- p.h = xmx_load_t<8, SR>(c.hm, c.ph, c.h, sb * 8, c.row0);
+ p.q = xmx_load_t<HW, 8, SR>(c.qs, c.pq, c.h, sb * 16 + part * 8, c.row0);
+ p.h = xmx_load_t<HW, 8, SR>(c.hm, c.ph, c.h, sb * 8, c.row0);
return p;
}
@@ -866,6 +871,7 @@ template <bool QH, bool SR> struct xmx_traits_q45_k : xmx_traits_base<SR, QH ? 1
xmx_row_scales<8> m; // dmin * min of each group
};
+ template <typename HW>
static ESIMD_INLINE scales_t load_scales(const ctx & c, int sb, const simd<uint32_t, 16> & rows) {
using namespace sycl::ext::intel::esimd;
scales_t s;
@@ -895,12 +901,12 @@ template <bool QH, bool SR> struct xmx_traits_q45_k : xmx_traits_base<SR, QH ? 1
simd<uint32_t, 128> a, b, h;
};
- static ESIMD_INLINE part_t load_part(const ctx & c, int sb, int part) {
+ template <typename HW> static ESIMD_INLINE part_t load_part(const ctx & c, int sb, int part) {
part_t p;
- p.a = xmx_load_t<8, SR>(c.qs, c.pq, c.h, sb * 32 + part * 16, c.row0);
- p.b = xmx_load_t<8, SR>(c.qs, c.pq, c.h, sb * 32 + part * 16 + 8, c.row0);
+ p.a = xmx_load_t<HW, 8, SR>(c.qs, c.pq, c.h, sb * 32 + part * 16, c.row0);
+ p.b = xmx_load_t<HW, 8, SR>(c.qs, c.pq, c.h, sb * 32 + part * 16 + 8, c.row0);
if constexpr (QH) {
- p.h = xmx_load_t<8, SR>(c.qh, c.ph, c.h, sb * 8, c.row0);
+ p.h = xmx_load_t<HW, 8, SR>(c.qh, c.ph, c.h, sb * 8, c.row0);
}
return p;
}
@@ -945,11 +951,12 @@ template <bool SR> struct xmx_traits_q2_k : xmx_traits_base<SR, 16, xmx_scale_ki
ESIMD_INLINE simd<uint8_t, 16> byte(int gr) { return code.template select<16, 4>(64 * (gr / 4) + (gr % 4)); }
};
+ template <typename HW>
static ESIMD_INLINE scales_t load_scales(const ctx & c, int sb, const simd<uint32_t, 16> & rows) {
using namespace sycl::ext::intel::esimd;
scales_t s;
simd<uint32_t, 16> bi = rows * c.bpr + sb;
- simd<uint32_t, 64> raw = xmx_load_t<4, SR>(c.sc, c.ps, c.h, sb * 4, c.row0);
+ simd<uint32_t, 64> raw = xmx_load_t<HW, 4, SR>(c.sc, c.ps, c.h, sb * 4, c.row0);
s.code = raw.template bit_cast_view<uint8_t>();
xmx_load_dm(c.dm, bi, s.d, s.dmin);
return s;
@@ -959,9 +966,9 @@ template <bool SR> struct xmx_traits_q2_k : xmx_traits_base<SR, 16, xmx_scale_ki
simd<uint32_t, 128> q;
};
- static ESIMD_INLINE part_t load_part(const ctx & c, int sb, int part) {
+ template <typename HW> static ESIMD_INLINE part_t load_part(const ctx & c, int sb, int part) {
part_t p;
- p.q = xmx_load_t<8, SR>(c.qs, c.pq, c.h, sb * 16 + part * 8, c.row0);
+ p.q = xmx_load_t<HW, 8, SR>(c.qs, c.pq, c.h, sb * 16 + part * 8, c.row0);
return p;
}
@@ -992,6 +999,7 @@ template <bool SR> struct xmx_traits_q8_0 : xmx_traits_base<SR, 16, xmx_scale_ki
xmx_row_scales<8> d; // scale of each of the 8 blocks of a super-block
};
+ template <typename HW>
static ESIMD_INLINE scales_t load_scales(const ctx & c, int sb, const simd<uint32_t, 16> & rows) {
using namespace sycl::ext::intel::esimd;
scales_t s;
@@ -1007,11 +1015,11 @@ template <bool SR> struct xmx_traits_q8_0 : xmx_traits_base<SR, 16, xmx_scale_ki
simd<uint32_t, 128> q[4];
};
- static ESIMD_INLINE part_t load_part(const ctx & c, int sb, int part) {
+ template <typename HW> static ESIMD_INLINE part_t load_part(const ctx & c, int sb, int part) {
part_t p;
#pragma unroll
for (int g = 0; g < 4; ++g) {
- p.q[g] = xmx_load_t<8, SR>(c.qs, c.pq, c.h, sb * 64 + 8 * (4 * part + g), c.row0);
+ p.q[g] = xmx_load_t<HW, 8, SR>(c.qs, c.pq, c.h, sb * 64 + 8 * (4 * part + g), c.row0);
}
return p;
}
@@ -1036,7 +1044,7 @@ ESIMD_INLINE simd<float, RC * NT> xmx_term(const simd<int8_t, 512> & B,
const simd<int8_t, RC * 32> & A,
const simd<float, NT> & s) {
using namespace sycl::ext::intel::esimd;
- namespace xmx = sycl::ext::intel::esimd::xmx;
+ namespace xmx = ggml_sycl_xmx::esimd;
simd<int, RC * NT> C = xmx::dpas<8, RC, int>(B, A);
return convert<float>(C) * s.template replicate<RC>();
@@ -1149,7 +1157,7 @@ template <int N> ESIMD_INLINE simd<float, N> xmx_glu_act(const simd<float, N> &
// weights instead, zeros in B drop the other half of K.
// KS is the most threads that split K for one tile, the launch may use fewer.
// FUSED computes up * act(gate) from the weights vx (up) and vg (gate), reading the activations once.
-template <typename T, int NC, int KS, bool FUSED>
+template <typename HW, typename T, int NC, int KS, bool FUSED>
ESIMD_INLINE void xmx_mul_mat(const void * vx,
const void * vg,
const char * vy,
@@ -1164,6 +1172,9 @@ ESIMD_INLINE void xmx_mul_mat(const void * vx,
const int lid,
const int ks) {
using namespace sycl::ext::intel::esimd;
+ namespace xmx = ggml_sycl_xmx::esimd;
+ // the weights are the 16-column B operand of one dpas (xmx_term): Xe2's width
+ static_assert(HW::dpas_width == XMX_ROWS, "the XMX mat-vec is built for a 16-wide dpas");
constexpr int NT = XMX_ROWS;
constexpr int RC = NC < 8 ? NC : 8;
@@ -1203,27 +1214,28 @@ ESIMD_INLINE void xmx_mul_mat(const void * vx,
}
for (int sb = lid; sb < bpr; sb += ks) {
- auto sc = T::load_scales(c, sb, rows);
+ auto sc = T::template load_scales<HW>(c, sb, rows);
typename T::scales_t scg;
if constexpr (FUSED) {
- scg = T::load_scales(cg, sb, rows);
+ scg = T::template load_scales<HW>(cg, sb, rows);
}
// d of the 8 groups of the super-block, [column * 8 + group]
simd<float, 8 * RC> d8f[NB];
#pragma unroll
for (int b = 0; b < NB; ++b) {
- simd<uint32_t, 8 * RC> ds = load_2d<uint32_t, 8, RC, 1, false, false>(yd, ydw, yh, yp, sb * 8, b * RC);
+ simd<uint32_t, 8 * RC> ds =
+ xmx::load_2d<HW, uint32_t, 8, RC, 1, false, false>(yd, ydw, yh, yp, sb * 8, b * RC);
simd<sycl::half, 16 * RC> dh = ds.template bit_cast_view<sycl::half>();
d8f[b] = convert<float>(simd<sycl::half, 8 * RC>(dh.template select<8 * RC, 2>(0)));
}
#pragma unroll
for (int part = 0; part < 2; ++part) {
- const auto pd = T::load_part(c, sb, part);
+ const auto pd = T::template load_part<HW>(c, sb, part);
typename T::part_t pdg;
if constexpr (FUSED) {
- pdg = T::load_part(cg, sb, part);
+ pdg = T::template load_part<HW>(cg, sb, part);
}
simd<uint32_t, 16 * RC> A2[NB]; // quants of two groups
@@ -1235,15 +1247,15 @@ ESIMD_INLINE void xmx_mul_mat(const void * vx,
if (g % 2 == 0) {
#pragma unroll
for (int b = 0; b < NB; ++b) {
- A2[b] =
- load_2d<uint32_t, 8, RC, 2, false, false>(yq, yqw, yh, yp, sb * 64 + gi * 8, b * RC);
+ A2[b] = xmx::load_2d<HW, uint32_t, 8, RC, 2, false, false>(yq, yqw, yh, yp,
+ sb * 64 + gi * 8, b * RC);
}
}
} else {
#pragma unroll
for (int b = 0; b < NB; ++b) {
- A2[b].template select<RC * 8, 1>(0) =
- load_2d<uint32_t, 8, RC, 1, false, false>(yq, yqw, yh, yp, sb * 64 + gi * 8, b * RC);
+ A2[b].template select<RC * 8, 1>(0) = xmx::load_2d<HW, uint32_t, 8, RC, 1, false, false>(
+ yq, yqw, yh, yp, sb * 64 + gi * 8, b * RC);
}
}
@@ -1302,7 +1314,7 @@ ESIMD_INLINE void xmx_mul_mat(const void * vx,
// KS_MAX bounds the threads that split K for one tile, the launch uses as few as keep every thread to the same
// number of super-blocks as the busiest one
-template <typename T, int NC, int KS_MAX, bool FUSED>
+template <typename HW, typename T, int NC, int KS_MAX, bool FUSED>
static void xmx_mul_mat_launch_ks(const void * vx,
const void * vg,
const char * vy,
@@ -1324,14 +1336,14 @@ static void xmx_mul_mat_launch_ks(const void * vx,
sycl::ext::oneapi::experimental::properties{
sycl::ext::intel::experimental::grf_size<xmx_grf<T, NC, FUSED>()> },
[=](sycl::nd_item<1> it) [[intel::sycl_explicit_simd]] {
- xmx_mul_mat<T, NC, KS_MAX, FUSED>(vx, vg, vy, dst, ncols, nrows, ncv, stride_y_bytes, ldd,
- glu_op, (int) it.get_group(0), (int) it.get_local_id(0),
- ks);
+ xmx_mul_mat<HW, T, NC, KS_MAX, FUSED>(vx, vg, vy, dst, ncols, nrows, ncv, stride_y_bytes,
+ ldd, glu_op, (int) it.get_group(0),
+ (int) it.get_local_id(0), ks);
});
});
}
-template <typename T, int NC, bool FUSED>
+template <typename HW, typename T, int NC, bool FUSED>
static void xmx_mul_mat_launch(const void * vx,
const void * vg,
const char * vy,
@@ -1345,12 +1357,13 @@ static void xmx_mul_mat_launch(const void * vx,
dpct::queue_ptr stream) {
// the fused kernel keeps two sets of accumulators in shared memory, half the threads for the same size
constexpr int ks_max = xmx_n_split<NC>() / (FUSED ? 2 : 1);
- xmx_mul_mat_launch_ks<T, NC, ks_max, FUSED>(vx, vg, vy, dst, ncols, nrows, ncv, stride_y_bytes, ldd, glu_op,
- stream);
+ xmx_mul_mat_launch_ks<HW, T, NC, ks_max, FUSED>(vx, vg, vy, dst, ncols, nrows, ncv, stride_y_bytes, ldd, glu_op,
+ stream);
}
-// FUSED: vx and vg are the up and gate weights, dst gets up * act(gate) with glu_op a ggml_glu_op
-template <typename T, bool FUSED = false>
+// FUSED: vx and vg are the up and gate weights, dst gets up * act(gate) with glu_op a ggml_glu_op. HW: the
+// architecture built for; the kernel exists for Xe2 (16-wide dpas, 2D block loads) only.
+template <typename T, bool FUSED = false, typename HW = ggml_sycl_xmx::xe2>
static void xmx_mul_mat_ncols(const void * vx,
const void * vg,
const char * vy,
@@ -1369,9 +1382,9 @@ static void xmx_mul_mat_ncols(const void * vx,
float * d = dst + (size_t) c0 * ldd;
const int nc = n > 32 ? 64 : (n > 16 ? 32 : (n > 8 ? 16 : n));
switch (nc) {
-#define XMX_CASE(N) \
- case N: \
- xmx_mul_mat_launch<T, N, FUSED>(vx, vg, y, d, ncols, nrows, n, stride_y_bytes, ldd, glu_op, stream); \
+#define XMX_CASE(N) \
+ case N: \
+ xmx_mul_mat_launch<HW, T, N, FUSED>(vx, vg, y, d, ncols, nrows, n, stride_y_bytes, ldd, glu_op, stream); \
break
XMX_CASE(1);
XMX_CASE(2);
@@ -1383,11 +1396,11 @@ static void xmx_mul_mat_ncols(const void * vx,
XMX_CASE(8);
#undef XMX_CASE
// short row variants are for tests, not built for the wide tiles
-#define XMX_CASE_WIDE(N) \
- case N: \
- if constexpr (!T::short_rows && (!FUSED || N <= XMX_FUSED_CHUNK)) { \
- xmx_mul_mat_launch<T, N, FUSED>(vx, vg, y, d, ncols, nrows, n, stride_y_bytes, ldd, glu_op, stream); \
- } \
+#define XMX_CASE_WIDE(N) \
+ case N: \
+ if constexpr (!T::short_rows && (!FUSED || N <= XMX_FUSED_CHUNK)) { \
+ xmx_mul_mat_launch<HW, T, N, FUSED>(vx, vg, y, d, ncols, nrows, n, stride_y_bytes, ldd, glu_op, stream); \
+ } \
break
XMX_CASE_WIDE(16);
XMX_CASE_WIDE(32);
diff --git a/ggml/src/ggml-sycl/fused-gemm-esimd.cpp b/ggml/src/ggml-sycl/fused-gemm-esimd.cpp
index 37f87e377..6fdb28f0b 100644
--- a/ggml/src/ggml-sycl/fused-gemm-esimd.cpp
+++ b/ggml/src/ggml-sycl/fused-gemm-esimd.cpp
@@ -4,6 +4,8 @@
#include <sycl/ext/intel/esimd/xmx/dpas.hpp>
#include <sycl/ext/intel/experimental/grf_size_properties.hpp>
+#include "xmx-compat.hpp"
+
// One thread owns MR rows of one expert's weight and one BN-wide tile of the schedule, over the
// whole K. Per k step (32 values) it loads the rows' stored bits, dequantizes them in registers to
// the f16 A operand of dpas (RC = 8 rows x 16 k, row major) and multiplies it with the packed B,
@@ -31,25 +33,27 @@ static constexpr int GROUPED_GEMM_MR = 32; // rows of one thread
static constexpr int GROUPED_GEMM_RC = 8; // rows of one dpas
static constexpr int GROUPED_GEMM_QK = 32; // values of one k step
-template <int NW_, int MR_, bool L2D_> struct grouped_gemm_arch {
- static constexpr int NW = NW_;
+// HW: the architecture (ggml_sycl_xmx::xe2, xehpg), MR: the rows of one thread
+template <typename HW_, int MR_> struct grouped_gemm_arch {
+ using HW = HW_;
+ static constexpr int NW = HW::dpas_width;
static constexpr int MR = MR_;
- static constexpr bool L2D = L2D_;
+ static constexpr bool L2D = HW::block_2d;
};
-using grouped_gemm_xe2 = grouped_gemm_arch<16, GROUPED_GEMM_MR, true>;
-using grouped_gemm_xehpg = grouped_gemm_arch<8, 16, false>;
+using grouped_gemm_xe2 = grouped_gemm_arch<ggml_sycl_xmx::xe2, GROUPED_GEMM_MR>;
+using grouped_gemm_xehpg = grouped_gemm_arch<ggml_sycl_xmx::xehpg, 16>;
// An AOT build compiles each kernel only for the targets of its dpas width: GGML_SYCL_XMX_AOT_SG for
-// spir64_gen, FG_AOT_SG (fused-gemm.hpp) for intel_gpu_* targets. A build without XMX (GGML_SYCL_SUPPORT_XMX
-// off) compiles none of them
+// spir64_gen, FG_AOT_SG (fused-gemm.hpp) for intel_gpu_* targets, unless XMX is emulated (xmx-compat.hpp).
+// A build without XMX (GGML_SYCL_SUPPORT_XMX off) compiles none of them
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;
+ return A::NW == GGML_SYCL_XMX_AOT_SG || ggml_sycl_xmx::emulated;
#elif defined(FG_AOT_SG)
- return A::NW == FG_AOT_SG;
+ return A::NW == FG_AOT_SG || ggml_sycl_xmx::emulated;
#else
return true;
#endif
@@ -57,6 +61,7 @@ template <typename A> static constexpr bool grouped_gemm_built() {
namespace grouped_gemm_esimd {
using namespace sycl::ext::intel::esimd;
+namespace xmx = ggml_sycl_xmx::esimd;
// f(std::integral_constant<int, I>) for I in [0, N): register selects at a runtime offset become
// indirect accesses, so loops whose index picks registers are spelled out at compile time
@@ -80,7 +85,7 @@ struct field {
// Where one thread's rows sit in an expert's slice, in either layout. nblocks counts the stored blocks
// of the slice, bpr those of one row, bs is the size of one block.
-template <int MR, bool L2D> struct slice {
+template <int MR, typename HW> struct slice {
const uint8_t * x;
int M;
int bpr;
@@ -114,16 +119,16 @@ template <int MR, bool L2D> struct slice {
}
// W dwords of each of the MR rows from byte `inner` (a multiple of 4) of field f of block ib, as
- // [row][W]. SoA with 2D block loads (L2D): one message over the stream. Otherwise the rows' dwords
+ // [row][W]. SoA with 2D block loads (HW::block_2d): one message over the stream. Otherwise the rows' dwords
// are gathered, as [W][row], at offsets rounded down to a dword (every field starts on an even
// byte; the SoA streams are dword aligned), shifted by 16 bits where an AoS row's block starts on
// 2 mod 4, and transposed with strided reads.
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 constexpr (HW::block_2d) {
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);
+ return xmx::load_2d<HW, 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);
@@ -848,7 +853,7 @@ template <typename block_q_t, int KS, typename T, bool AOS, typename A> struct g
ESIMD_INLINE void run(sycl::nd_item<1> it) const {
using namespace sycl::ext::intel::esimd;
- namespace xmx = sycl::ext::intel::esimd::xmx;
+ namespace xmx = ggml_sycl_xmx::esimd;
constexpr int MR = A::MR;
constexpr int NW = A::NW;
constexpr int RC = GROUPED_GEMM_RC;
@@ -875,7 +880,7 @@ template <typename block_q_t, int KS, typename T, bool AOS, typename A> struct g
// the slice of this expert
const int bpr = K / F::qk;
- const grouped_gemm_esimd::slice<MR, A::L2D> sl = {
+ const grouped_gemm_esimd::slice<MR, typename A::HW> sl = {
(const uint8_t *) src0_dd + (size_t) tile.expert * expert_stride,
M,
bpr,
@@ -940,7 +945,8 @@ template <typename block_q_t, int KS, typename T, bool AOS, typename A> struct g
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);
+ b[nt] = xmx::load_2d<typename A::HW, uint32_t, NW, 16>(bp, bw - 1, K / 2 - 1, bw - 1, b0 + nt * NW,
+ kb * 16);
}
} else {
simd<uint32_t, 16 * BN> bm;
@@ -1082,6 +1088,15 @@ static void grouped_gemm_esimd_launch(const char * src0_d
}
}
+template <int NW> using grouped_gemm_arch_of = std::conditional_t<NW == 16, grouped_gemm_xe2, grouped_gemm_xehpg>;
+
+// the shapes the kernel takes: whole row groups and stored blocks, every K-split range non-empty
+// (also in mode 1, so both modes take the same shapes)
+template <typename block_q_t, typename A> static bool grouped_gemm_esimd_shape_ok(int M, int K, bool reordered) {
+ using F = grouped_gemm_esimd_a<block_q_t, A::MR>;
+ return M % A::MR == 0 && F::ok(K, !reordered) && K >= GROUPED_GEMM_QK * GGML_SYCL_GROUPED_GEMM_KSPLIT;
+}
+
template <typename block_q_t, typename T, typename A>
static bool grouped_gemm_esimd_run(const char * src0_dd,
size_t expert_stride,
@@ -1095,11 +1110,9 @@ static bool grouped_gemm_esimd_run(const char * src0_dd,
bool reordered,
int64_t n_tiles,
dpct::queue_ptr stream) {
- using F = grouped_gemm_esimd_a<block_q_t, A::MR>;
- // whole row groups and stored blocks, every K-split range non-empty (also in mode 1, so both
- // modes take the same shapes); B is read with 2D block loads, which want 64-byte aligned
- // surfaces and pitches, or with 16-byte aligned block loads of whole tiles
- if (M % A::MR != 0 || !F::ok(K, !reordered) || K < GROUPED_GEMM_QK * GGML_SYCL_GROUPED_GEMM_KSPLIT ||
+ // B is read with 2D block loads, which want 64-byte aligned surfaces and pitches, or with 16-byte
+ // aligned block loads of whole tiles
+ if (!grouped_gemm_esimd_shape_ok<block_q_t, A>(M, K, reordered) ||
!grouped_gemm_esimd_weights_ok(src0_dd, expert_stride, reordered && A::L2D) || ((uintptr_t) packed % 64) != 0 ||
Npad % 16 != 0) {
return false;
@@ -1130,23 +1143,18 @@ bool ggml_sycl_grouped_gemm_esimd(const char * src0_dd,
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);
- }
+ return grouped_gemm_esimd_run<block_q_t, T, grouped_gemm_arch_of<NW>>(
+ 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) \
+#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)
diff --git a/ggml/src/ggml-sycl/fused-gemm.cpp b/ggml/src/ggml-sycl/fused-gemm.cpp
index 89096d561..871546f4a 100644
--- a/ggml/src/ggml-sycl/fused-gemm.cpp
+++ b/ggml/src/ggml-sycl/fused-gemm.cpp
@@ -2,7 +2,7 @@
#include "fused-gemm-esimd.hpp"
#include "quants.hpp"
-#include <sycl/ext/oneapi/matrix/matrix.hpp>
+#include "xmx-compat.hpp"
#include <algorithm>
#include <string>
@@ -11,7 +11,7 @@
#include <set>
#include <unordered_map>
-namespace mx = sycl::ext::oneapi::experimental::matrix;
+namespace mx = ggml_sycl_xmx::matrix;
// FG_ / fg_ is short for fused GEMM: the weights are dequantized inside the GEMM, into the XMX tiles.
@@ -129,9 +129,10 @@ template <typename F> static void fg_visit_combo(int idx, F && f);
static constexpr int FG_N_COMBOS = 8;
// A spir64_gen AOT build (GGML_SYCL_XMX_AOT_SG) drops the combinations of the other sub-group size
-// entirely: ocloc rejects even an empty kernel that asks for a sub-group size it lacks.
+// entirely: ocloc rejects even an empty kernel that asks for a sub-group size it lacks. An XMX-emulation
+// build asks for hardware sub-groups of 16 (ggml_sycl_xmx::hw_sub_group_size) and keeps them all.
template <int SG> static constexpr bool fg_listed() {
-#if defined(GGML_SYCL_XMX_AOT_SG)
+#if defined(GGML_SYCL_XMX_AOT_SG) && !defined(GGML_SYCL_XMX_EMULATION)
return SG == GGML_SYCL_XMX_AOT_SG;
#else
return true;
@@ -203,8 +204,27 @@ static std::string fg_combo_name(int idx) {
return name;
}
-// Combinations this build has kernels for and the device lists, one bit each. Cached per device:
-// on a mixed box the first caller's verdict is not the others'.
+// The combinations of the architectures GGML_SYCL_XMX_EMULATED_HW selects, one bit each: Xe-HPG's are those
+// on sub-group 8, Xe2's those on sub-group 16
+static int fg_xmx_emulated_hw_combos() {
+ int m = 0;
+ for (int idx = 0; idx < FG_N_COMBOS; ++idx) {
+ fg_visit_combo(idx, [&](auto s) {
+ using S = decltype(s);
+ if (((g_ggml_sycl_xmx_emulated_hw & GGML_SYCL_XMX_EMULATED_HW_XEHPG) && S::SG == 8) ||
+ ((g_ggml_sycl_xmx_emulated_hw & GGML_SYCL_XMX_EMULATED_HW_XE2) && S::SG == 16)) {
+ m |= 1 << idx;
+ }
+ });
+ }
+ return m;
+}
+
+// Combinations this build has kernels for and the device offers, one bit each: those it lists, plus
+// those of the architectures GGML_SYCL_XMX_EMULATED_HW emulates (the device's own stay available), each only if
+// the device has the hardware sub-group size its joint_matrix kernel launches with
+// (ggml_sycl_xmx::hw_sub_group_size: emulated, a logical sub-group of 8 runs on hardware sub-groups of
+// 16). Cached per device: on a mixed box the first caller's verdict is not the others'.
static int fg_device_combos(const sycl::device & dev) {
static std::mutex mtx;
static std::unordered_map<sycl::device, int> known;
@@ -213,24 +233,33 @@ static int fg_device_combos(const sycl::device & dev) {
if (it != known.end()) {
return it->second;
}
- int available = 0;
+ int native = 0, launchable = 0;
try {
+ const auto sg_sizes = dev.get_info<sycl::info::device::sub_group_sizes>();
+ for (int idx = 0; idx < FG_N_COMBOS; ++idx) {
+ fg_visit_combo(idx, [&](auto s) {
+ const size_t hw = ggml_sycl_xmx::hw_sub_group_size(decltype(s)::SG);
+ if (std::find(sg_sizes.begin(), sg_sizes.end(), hw) != sg_sizes.end()) {
+ launchable |= 1 << idx;
+ }
+ });
+ }
const auto combinations = dev.get_info<sycl::ext::oneapi::experimental::info::device::matrix_combinations>();
- const auto sg_sizes = dev.get_info<sycl::info::device::sub_group_sizes>();
for (int idx = 0; idx < FG_N_COMBOS; ++idx) {
fg_visit_combo(idx, [&](auto s) {
- using S = decltype(s);
- const bool sg_ok = std::find(sg_sizes.begin(), sg_sizes.end(), (size_t) S::SG) != sg_sizes.end();
- if (sg_ok && fg_device_has_combo<S>(combinations)) {
- available |= 1 << idx;
+ if (fg_device_has_combo<decltype(s)>(combinations)) {
+ native |= 1 << idx;
}
});
}
} catch (const sycl::exception &) {
- available = 0;
+ native = 0; // no XMX
}
- GGML_LOG_INFO("%s: %s: XMX dequant-GEMM combinations available 0x%x, allowed 0x%x\n", __func__,
- dev.get_info<sycl::info::device::name>().c_str(), available, g_ggml_sycl_xmx_gather_shapes);
+ const int emulated = fg_xmx_emulated_hw_combos();
+ const int available = (native | emulated) & launchable;
+ GGML_LOG_INFO("%s: %s: XMX dequant-GEMM combinations available 0x%x (emulated 0x%x), allowed 0x%x\n", __func__,
+ dev.get_info<sycl::info::device::name>().c_str(), available, available & ~native,
+ g_ggml_sycl_xmx_gather_shapes);
known.emplace(dev, available);
return available;
}
@@ -859,7 +888,7 @@ static void fused_dequant_gemm_tile(
using EA = typename S::EA;
using TA = typename S::ta;
using TB = typename S::tb;
- const auto sg = item.get_sub_group();
+ const auto sg = ggml_sycl_xmx::sub_group_of<S::SG>(item);
const int sg_id = sg.get_group_id()[0];
const int lane = sg.get_local_id()[0];
const int m0 = item.get_group(1) * S::SG_ROWS;
@@ -914,7 +943,7 @@ static void fused_dequant_gemm_tile(
}
}
}
- sycl::group_barrier(sg);
+ ggml_sycl_xmx::group_barrier(sg);
#pragma unroll
for (int kt = 0; kt < FG_BK / S::TK; ++kt) {
@@ -937,7 +966,7 @@ static void fused_dequant_gemm_tile(
}
}
// the next step overwrites tile_a
- sycl::group_barrier(sg);
+ ggml_sycl_xmx::group_barrier(sg);
}
#pragma unroll
@@ -984,7 +1013,7 @@ static void fused_dequant_gemm_launch(const void * src0, const typename S::tsb *
sycl::local_accessor<float, 1> tile_c(FG_KSPLIT * S::SG_ROWS * S::BN, cgh);
cgh.parallel_for(
sycl::nd_range<2>(sycl::range<2>(groups_n, groups_m * S::WG_SIZE), sycl::range<2>(1, S::WG_SIZE)),
- [=](sycl::nd_item<2> item) [[sycl::reqd_sub_group_size(S::SG)]] {
+ [=](sycl::nd_item<2> item) [[sycl::reqd_sub_group_size(ggml_sycl_xmx::hw_sub_group_size(S::SG))]] {
const int n0 = item.get_group(0) * S::BN;
fused_dequant_gemm_tile<S, block_q_t, reordered>((const block_q_t *) src0, packed, dst, M, Npad, K,
ldd, n0, n0, N, tile_a, tile_c, item);
@@ -1013,7 +1042,7 @@ static void grouped_dequant_gemm_launch(const char * src0_dd, const size_t exper
sycl::local_accessor<float, 1> tile_c(FG_KSPLIT * S::SG_ROWS * S::BN, cgh);
cgh.parallel_for(
sycl::nd_range<2>(sycl::range<2>(n_tiles, groups_m * S::WG_SIZE), sycl::range<2>(1, S::WG_SIZE)),
- [=](sycl::nd_item<2> item) [[sycl::reqd_sub_group_size(S::SG)]] {
+ [=](sycl::nd_item<2> item) [[sycl::reqd_sub_group_size(ggml_sycl_xmx::hw_sub_group_size(S::SG))]] {
const int t = item.get_group(0);
const ggml_sycl_grouped_gemm_tile tile = tiles_ptr[t];
if (tile.n0 == tile.n1) {
diff --git a/ggml/src/ggml-sycl/fused-gemm.hpp b/ggml/src/ggml-sycl/fused-gemm.hpp
index e65e98fc8..3062341e7 100644
--- a/ggml/src/ggml-sycl/fused-gemm.hpp
+++ b/ggml/src/ggml-sycl/fused-gemm.hpp
@@ -31,8 +31,9 @@ struct ggml_sycl_grouped_gemm_rows {
# endif
#endif
+// an XMX-emulation build (xmx-compat.hpp) launches every combination on hardware sub-groups of 16
template <int SG> static constexpr bool fg_built() {
-#if defined(FG_AOT_SG)
+#if defined(FG_AOT_SG) && !defined(GGML_SYCL_XMX_EMULATION)
return SG == FG_AOT_SG;
#else
return true;
diff --git a/ggml/src/ggml-sycl/ggml-sycl.cpp b/ggml/src/ggml-sycl/ggml-sycl.cpp
index 9314dcde7..7fbd1d181 100644
--- a/ggml/src/ggml-sycl/ggml-sycl.cpp
+++ b/ggml/src/ggml-sycl/ggml-sycl.cpp
@@ -92,6 +92,7 @@
#include "ggml-sycl/solve_tri.hpp"
#include "ggml-sycl/gated_delta_net.hpp"
#include "ggml-sycl/pool.hpp"
+#include "ggml-sycl/xmx-compat.hpp"
#include "ggml-sycl/cross_entropy_loss.hpp"
#define MEM_SIZE_2M 0x00200000
@@ -122,6 +123,7 @@ int g_ggml_sycl_xmx_glu_max_cols = GGML_SYCL_XMX_GLU_MAX_COLS;
int g_ggml_sycl_prioritize_dmmv = 0;
int g_ggml_sycl_xmx_gather_types = GGML_SYCL_XMX_GATHER_TYPES_DEFAULT;
int g_ggml_sycl_xmx_gather_shapes = GGML_SYCL_XMX_GATHER_SHAPES_DEFAULT;
+int g_ggml_sycl_xmx_emulated_hw = 0;
int g_ggml_sycl_mmid_sched = GGML_SYCL_MMID_SCHED_DEVICE;
int g_ggml_sycl_dynamic_precision = GGML_SYCL_DYNAMIC_PRECISION_DEFAULT;
int g_ggml_sycl_dynamic_required_precision = GGML_SYCL_DYNAMIC_PRECISION_F32;
@@ -461,6 +463,13 @@ static void ggml_check_sycl() try {
g_ggml_sycl_xmx_gather_types = ggml_sycl_get_env("GGML_SYCL_XMX_GATHER_TYPES", GGML_SYCL_XMX_GATHER_TYPES_DEFAULT);
g_ggml_sycl_mmid_sched = ggml_sycl_get_env("GGML_SYCL_MMID_SCHED", GGML_SYCL_MMID_SCHED_DEVICE) & 3;
g_ggml_sycl_xmx_gather_shapes = ggml_sycl_get_env("GGML_SYCL_XMX_GATHER_SHAPES", GGML_SYCL_XMX_GATHER_SHAPES_DEFAULT);
+ g_ggml_sycl_xmx_emulated_hw = ggml_sycl_get_env("GGML_SYCL_XMX_EMULATED_HW", 0);
+ if (g_ggml_sycl_xmx_emulated_hw != 0 && !ggml_sycl_xmx::emulated) {
+ // the emulated architectures' instructions would run on this device's XMX and compute garbage
+ GGML_LOG_WARN("GGML_SYCL_XMX_EMULATED_HW=%d is ignored: it needs a build with GGML_SYCL_XMX_EMULATION\n",
+ g_ggml_sycl_xmx_emulated_hw);
+ g_ggml_sycl_xmx_emulated_hw = 0;
+ }
g_ggml_sycl_dynamic_precision =
ggml_sycl_get_env_precision("GGML_SYCL_DYNAMIC_PRECISION", GGML_SYCL_DYNAMIC_PRECISION_DEFAULT);
g_ggml_sycl_dynamic_required_precision =
@@ -512,6 +521,12 @@ static void ggml_check_sycl() try {
GGML_LOG_INFO(" GGML_SYCL_F16: no\n");
#endif
+#if defined(GGML_SYCL_XMX_EMULATION)
+ GGML_LOG_INFO(" GGML_SYCL_XMX_EMULATION: yes (dpas and joint_matrix in software, slow)\n");
+#else
+ GGML_LOG_INFO(" GGML_SYCL_XMX_EMULATION: no\n");
+#endif
+
#if defined(GGML_SYCL_FORCE_MMQ)
GGML_LOG_INFO(" GGML_SYCL_FORCE_MMQ: yes\n");
#else
@@ -588,6 +603,7 @@ static void ggml_check_sycl() try {
GGML_LOG_INFO(" GGML_SYCL_MMID_SCHED: %d\n", g_ggml_sycl_mmid_sched);
GGML_LOG_INFO(" GGML_SYCL_MMID_ESIMD: %d\n", g_ggml_sycl_mmid_esimd);
GGML_LOG_INFO(" GGML_SYCL_XMX_GATHER_SHAPES: %d\n", g_ggml_sycl_xmx_gather_shapes);
+ GGML_LOG_INFO(" GGML_SYCL_XMX_EMULATED_HW: %d\n", g_ggml_sycl_xmx_emulated_hw);
GGML_LOG_INFO(" GGML_SYCL_DYNAMIC_PRECISION: %s\n",
ggml_sycl_dynamic_precision_names[g_ggml_sycl_dynamic_precision]);
GGML_LOG_INFO(" GGML_SYCL_DYNAMIC_REQUIRED_PRECISION: %s\n",
diff --git a/ggml/src/ggml-sycl/xmx-compat.hpp b/ggml/src/ggml-sycl/xmx-compat.hpp
new file mode 100644
index 000000000..f74f1e395
--- /dev/null
+++ b/ggml/src/ggml-sycl/xmx-compat.hpp
@@ -0,0 +1,307 @@
+#ifndef GGML_SYCL_XMX_COMPAT_HPP
+#define GGML_SYCL_XMX_COMPAT_HPP
+
+// Every XMX instruction of the SYCL backend goes through this layer: the ESIMD dpas as
+// ggml_sycl_xmx::esimd::dpas, the joint_matrix API as ggml_sycl_xmx::matrix. Code takes
+//
+// namespace xmx = ggml_sycl_xmx::esimd; // instead of sycl::ext::intel::esimd::xmx
+// namespace mx = ggml_sycl_xmx::matrix; // instead of sycl::ext::oneapi::experimental::matrix
+//
+// and is written as against the SYCL namespaces. A 2D block load is xmx::load_2d<Arch, ...>, the ESIMD
+// load_2d with the architecture the kernel is built for first (ggml_sycl_xmx::xe2, xehpg): it does not
+// compile for an architecture without 2D block messages. A joint_matrix kernel of sub-group size SG also
+// takes its sub-group and barrier from here (sub_group_of<SG>, group_barrier) and is launched with
+// reqd_sub_group_size(hw_sub_group_size(SG)).
+//
+// By default all of it is the SYCL API itself. With GGML_SYCL_XMX_EMULATION (CMake option, off by
+// default) it computes in software, so a kernel runs on a device without the XMX shape it asks for:
+// the 8-wide dpas of Xe-HPG on Xe2, the 16-wide one on Xe-HPG, joint_matrix shapes a device does not
+// list, on logical sub-groups of a size it lacks. That is for debugging; it is slow.
+
+#include <sycl/sycl.hpp>
+#include <sycl/ext/intel/esimd.hpp>
+#include <sycl/ext/intel/esimd/xmx/dpas.hpp>
+#include <sycl/ext/oneapi/matrix/matrix.hpp>
+
+#include <type_traits>
+
+namespace ggml_sycl_xmx {
+
+#if defined(GGML_SYCL_XMX_EMULATION)
+inline constexpr bool emulated = true;
+#else
+inline constexpr bool emulated = false;
+#endif
+
+// The features of the architecture a kernel is built for: the width of one dpas (the columns of its B
+// operand, the sub-group size of the matching joint_matrix combinations) and whether 2D block messages
+// exist. A kernel templated on one of these takes its dpas width from it and its 2D loads through
+// xmx::load_2d<Arch, ...>.
+template <int DpasWidth, bool Block2D> struct arch {
+ static constexpr int dpas_width = DpasWidth;
+ static constexpr bool block_2d = Block2D;
+};
+
+using xe2 = arch<16, true>; // Xe2, Xe3, Xe-HPC
+using xehpg = arch<8, false>; // Xe-HPG: Arc A-series, ARL-H
+
+#if !defined(GGML_SYCL_XMX_EMULATION)
+
+namespace esimd {
+using sycl::ext::intel::esimd::xmx::dpas;
+} // namespace esimd
+namespace matrix = sycl::ext::oneapi::experimental::matrix;
+
+// The sub-group of a joint_matrix kernel of sub-group size SG: launched with
+// reqd_sub_group_size(hw_sub_group_size(SG)), it takes sub_group_of<SG>(item) and group_barrier().
+constexpr int hw_sub_group_size(int sg) {
+ return sg;
+}
+
+template <int SG, int D> inline sycl::sub_group sub_group_of(const sycl::nd_item<D> & item) {
+ return item.get_sub_group();
+}
+
+using sycl::group_barrier;
+
+#else
+
+// Emulated, the sub-group is logical: SG consecutive work-items of the work-group, so a kernel of a
+// sub-group size the device lacks (8 on Xe2) runs. It is launched on hardware sub-groups of at least
+// 16 work-items, each holding whole logical ones, and the emulated joint_matrix only splits stores
+// over a sub-group. Two logical sub-groups in one hardware sub-group may call group_barrier() a
+// different number of times (the K splits of the grouped GEMM differ by a step): on Intel GPUs a
+// sub-group is one hardware thread and its barrier a fence, which that does not hang, although SYCL
+// does not promise it.
+constexpr int hw_sub_group_size(int sg) {
+ return sg < 16 ? 16 : sg;
+}
+
+template <int SG> struct logical_sub_group {
+ sycl::sub_group hw;
+ int id;
+ int lane;
+
+ sycl::id<1> get_group_id() const { return sycl::id<1>(id); }
+ sycl::id<1> get_local_id() const { return sycl::id<1>(lane); }
+ size_t get_local_linear_id() const { return lane; }
+ sycl::range<1> get_local_range() const { return sycl::range<1>(SG); }
+ size_t get_local_linear_range() const { return SG; }
+};
+
+template <int SG, int D> inline logical_sub_group<SG> sub_group_of(const sycl::nd_item<D> & item) {
+ const int l = (int) item.get_local_linear_id();
+ return { item.get_sub_group(), l / SG, l % SG };
+}
+
+template <int SG> inline void group_barrier(const logical_sub_group<SG> & g) {
+ sycl::group_barrier(g.hw);
+}
+
+inline void group_barrier(const sycl::sub_group & g) {
+ sycl::group_barrier(g);
+}
+
+namespace esimd {
+namespace detail {
+template <typename E> constexpr bool supported() {
+ return std::is_same_v<E, sycl::half> || std::is_same_v<E, sycl::ext::oneapi::bfloat16> ||
+ std::is_same_v<E, int8_t> || std::is_same_v<E, uint8_t>;
+}
+
+// D = C + A B as xmx::dpas computes it: A row major [RC][K], B the VNNI operand [K / OPC][NW][OPC],
+// C and D [RC][NW], K = SD * OPC with OPC the operand elements of a 32-bit channel. Integer operands
+// sum in int32, floating ones in f32; the f32 sum order is not the hardware's.
+template <int SD, int RC, typename T, typename BT, typename AT, int BN, int AN>
+ESIMD_INLINE void mad(sycl::ext::intel::esimd::simd<T, RC * (BN / (SD * (4 / sizeof(BT))))> & D,
+ sycl::ext::intel::esimd::simd<BT, BN> B, sycl::ext::intel::esimd::simd<AT, AN> A) {
+ using namespace sycl::ext::intel::esimd;
+ static_assert(supported<BT>() && supported<AT>() && sizeof(AT) == sizeof(BT), "dpas operand types");
+ constexpr int OPC = 4 / sizeof(BT);
+ constexpr int K = SD * OPC;
+ constexpr int NW = BN / K;
+ static_assert(BN == K * NW && AN == RC * K, "dpas operand sizes");
+ using W = std::conditional_t<std::is_integral_v<BT>, int32_t, float>;
+ simd<W, BN> bw = B;
+ simd<W, AN> aw = A;
+#pragma unroll
+ for (int k = 0; k < K; ++k) {
+ simd<W, NW> bk = bw.template select<NW, OPC>((k / OPC) * NW * OPC + k % OPC);
+#pragma unroll
+ for (int r = 0; r < RC; ++r) {
+ D.template select<NW, 1>(r * NW) += simd<T, NW>(bk * (W) aw[r * K + k]);
+ }
+ }
+}
+} // namespace detail
+
+template <int SD, int RC, typename T, typename CT, typename BT, typename AT, int N, int BN, int AN>
+ESIMD_INLINE sycl::ext::intel::esimd::simd<T, N> dpas(sycl::ext::intel::esimd::simd<CT, N> C,
+ sycl::ext::intel::esimd::simd<BT, BN> B,
+ sycl::ext::intel::esimd::simd<AT, AN> A) {
+ sycl::ext::intel::esimd::simd<T, N> D = C;
+ detail::mad<SD, RC, T, BT, AT, BN, AN>(D, B, A);
+ return D;
+}
+
+template <int SD, int RC, typename T, typename BT, typename AT, int BN, int AN>
+ESIMD_INLINE auto dpas(sycl::ext::intel::esimd::simd<BT, BN> B, sycl::ext::intel::esimd::simd<AT, AN> A) {
+ sycl::ext::intel::esimd::simd<T, RC * (BN / (SD * (4 / sizeof(BT))))> D = 0;
+ detail::mad<SD, RC, T, BT, AT, BN, AN>(D, B, A);
+ return D;
+}
+} // namespace esimd
+
+// joint_matrix in software: every work-item of the group holds the whole matrix, loads all of it
+// and computes all of every product; a store is split over the group's work-items. The enums,
+// tf32 and the combination record are the SYCL ones.
+namespace matrix {
+namespace sm = sycl::ext::oneapi::experimental::matrix;
+using sm::combination;
+using sm::layout;
+using sm::matrix_type;
+using sm::use;
+namespace precision = sm::precision;
+
+namespace detail {
+template <typename T> struct storage {
+ using type = T;
+};
+template <> struct storage<precision::tf32> {
+ using type = float;
+};
+} // namespace detail
+
+template <typename Group, typename T, use Use, size_t Rows, size_t Cols, layout Layout = layout::dynamic>
+struct joint_matrix {
+ using storage_t = typename detail::storage<T>::type;
+ storage_t data[Rows * Cols];
+};
+
+// G, the group a call passes, is the logical sub-group of a kernel (sub_group_of) or a sycl::sub_group
+template <typename G, typename Group, typename T, use U, size_t R, size_t C, layout L, typename V>
+inline void joint_matrix_fill(G, joint_matrix<Group, T, U, R, C, L> & m, const V & v) {
+ using S = typename joint_matrix<Group, T, U, R, C, L>::storage_t;
+#pragma unroll
+ for (size_t i = 0; i < R * C; ++i) {
+ m.data[i] = (S) v;
+ }
+}
+
+namespace detail {
+// element (r, c) of a matrix at p with the given layout and stride; ext_intel_packed is the VNNI
+// layout of B: rows of K / V, each holding the V consecutive k of every column
+template <size_t R, size_t C, typename S, typename P>
+inline void load(S * d, P p, size_t stride, layout l) {
+ constexpr size_t V = 4 / sizeof(S) > 0 ? 4 / sizeof(S) : 1;
+ for (size_t r = 0; r < R; ++r) {
+ for (size_t c = 0; c < C; ++c) {
+ size_t o;
+ if (l == layout::row_major) {
+ o = r * stride + c;
+ } else if (l == layout::col_major) {
+ o = c * stride + r;
+ } else {
+ o = (r / V) * stride + c * V + (r % V);
+ }
+ d[r * C + c] = (S) p[o];
+ }
+ }
+}
+} // namespace detail
+
+// a or b, layout in the type
+template <typename G, typename Group, typename T, use U, size_t R, size_t C, layout L, typename P>
+inline std::enable_if_t<L != layout::dynamic> joint_matrix_load(G, joint_matrix<Group, T, U, R, C, L> & m, P p,
+ size_t stride) {
+ detail::load<R, C>(m.data, p, stride, L);
+}
+
+// accumulator, layout given
+template <typename G, typename Group, typename T, size_t R, size_t C, typename P>
+inline void joint_matrix_load(G, joint_matrix<Group, T, use::accumulator, R, C, layout::dynamic> & m, P p,
+ size_t stride, layout l) {
+ detail::load<R, C>(m.data, p, stride, l);
+}
+
+template <typename G, typename Group, typename T, size_t R, size_t C, typename P>
+inline void joint_matrix_store(G g, const joint_matrix<Group, T, use::accumulator, R, C, layout::dynamic> & m,
+ P p, size_t stride, layout l) {
+ // every work-item holds the same values; each writes its share
+ const size_t n = g.get_local_linear_range();
+ for (size_t i = g.get_local_linear_id(); i < R * C; i += n) {
+ const size_t r = i / C, c = i % C;
+ p[l == layout::col_major ? c * stride + r : r * stride + c] = m.data[i];
+ }
+}
+
+template <typename G, typename Group, typename Td, typename Ta, typename Tb, typename Tc, size_t M, size_t K,
+ size_t N, layout La, layout Lb>
+inline void joint_matrix_mad(G, joint_matrix<Group, Td, use::accumulator, M, N, layout::dynamic> & D,
+ const joint_matrix<Group, Ta, use::a, M, K, La> & A,
+ const joint_matrix<Group, Tb, use::b, K, N, Lb> & B,
+ const joint_matrix<Group, Tc, use::accumulator, M, N, layout::dynamic> & C) {
+ using S = typename joint_matrix<Group, Td, use::accumulator, M, N>::storage_t;
+ for (size_t m = 0; m < M; ++m) {
+ for (size_t n = 0; n < N; ++n) {
+ S s = (S) C.data[m * N + n];
+ for (size_t k = 0; k < K; ++k) {
+ s += (S) A.data[m * K + k] * (S) B.data[k * N + n];
+ }
+ D.data[m * N + n] = s;
+ }
+ }
+}
+} // namespace matrix
+
+#endif // GGML_SYCL_XMX_EMULATION
+
+namespace esimd {
+namespace detail {
+// load_2d computed with masked gathers: BH rows of NB adjacent BW-element blocks from the surface at p
+// (sw + 1 bytes wide, sh + 1 rows, rows sp + 1 bytes apart) at element column x and row y, zero where
+// the block leaves the surface. Transposed (one block) returns the block column major.
+template <typename T, int BW, int BH, int NB, bool Transposed>
+ESIMD_INLINE sycl::ext::intel::esimd::simd<T, BW * BH * NB> load_2d_soft(const T * p,
+ unsigned sw,
+ unsigned sh,
+ unsigned sp,
+ int x,
+ int y) {
+ using namespace sycl::ext::intel::esimd;
+ static_assert(!Transposed || NB == 1, "a transposed 2D load reads one block");
+ constexpr int N = BW * BH * NB;
+ simd<int32_t, N> e(0, 1);
+ simd<int32_t, N> c, r, b = 0;
+ if constexpr (Transposed) {
+ r = e % BH;
+ c = e / BH;
+ } else {
+ c = e % BW;
+ r = (e / BW) % BH;
+ b = e / (BW * BH);
+ }
+ const simd<int32_t, N> xe = x + b * BW + c;
+ const simd<int32_t, N> yr = y + r;
+ const simd_mask<N> ok = (xe >= 0) & (xe * (int) sizeof(T) <= (int) sw) & (yr >= 0) & (yr <= (int) sh);
+ simd<uint32_t, N> off = simd<uint32_t, N>(yr) * (sp + 1) + simd<uint32_t, N>(xe) * (uint32_t) sizeof(T);
+ off.merge(simd<uint32_t, N>(0), !ok);
+ return gather<T, N>(p, off, ok, simd<T, N>(0));
+}
+} // namespace detail
+
+template <typename Arch, typename T, int BW, int BH = 1, int NB = 1, bool Transposed = false, bool Transformed = false>
+ESIMD_INLINE auto load_2d(const T * p, unsigned sw, unsigned sh, unsigned sp, int x, int y) {
+ static_assert(Arch::block_2d, "2D block load in a kernel built for an architecture without 2D block messages");
+ if constexpr (emulated) {
+ static_assert(!Transformed, "the emulated 2D load does not transform (VNNI)");
+ return detail::load_2d_soft<T, BW, BH, NB, Transposed>(p, sw, sh, sp, x, y);
+ } else {
+ return sycl::ext::intel::esimd::load_2d<T, BW, BH, NB, Transposed, Transformed>(p, sw, sh, sp, x, y);
+ }
+}
+} // namespace esimd
+
+} // namespace ggml_sycl_xmx
+
+#endif // GGML_SYCL_XMX_COMPAT_HPP