Archived
8fced2e turned on -ze-intel-enable-auto-large-GRF-mode for all of libxe2, which gave the flash
attention kernels the 256 registers their grf_size<256> asks for (AOT never honours the property:
sycl-post-link records it on the image, and only the runtime's JIT reads it). The auto mode also
moved ~25 SIMT kernels that never spilled at 128 to 256 and half their threads: gdn_conv_prep
434 -> 687 us a call at a 2k prefill.
The attention source is now its own shared library, xe2_grf256, linked with
-ze-opt-large-register-file, and the rest of libxe2 is back on the default it was tuned under.
libxe2 needs it from kernels/libxe2.d ($ORIGIN rpath; beside the plugins, not among them, where
the loader would take it for one); both installs carry it. refuse() is exported from libxe2 so the
helper's refusals still reach last_refusal().
rad-kbench replay (libxe2, libref, spechub) 1410/1410, attn --all-rows 227/227, and from an
installed tree. qwen3.8-27b-w4a16 (mtp4fc), fresh prompts, against the auto mode: prefill ~2.1k
tokens 1381 -> 1407 tok/s, ~6.9k 1326 -> 1347; decode 24 corpus prompts 90.4 -> 90.6; greedy 8/8.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
216 lines
9.8 KiB
C++
216 lines
9.8 KiB
C++
/* xe2_common.h -- what every libxe2 kernel shares: the queue a launch submits to, row geometry,
|
|
* and the dtype conversions.
|
|
*
|
|
* Correctness first (docs/SYCL_PORTING.md §4.8): the conversions are rad_plugin.h's, the ones
|
|
* libref computes with, so a result rounds where the reference's does. Device code is compiled
|
|
* with -ffp-contract=off for the same reason (libxe2/CMakeLists.txt). A kernel that cannot serve
|
|
* the geometry it is handed returns RAD_E_UNSUPPORTED and names why through xe2_refuse; it never
|
|
* hands the work to the host.
|
|
*/
|
|
#pragma once
|
|
#include "rad_abi.h"
|
|
#include "rad_plugin.h"
|
|
#include "rad_sycl.h"
|
|
|
|
#include <sycl/sycl.hpp>
|
|
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
|
|
/* The SPIR-V builtin behind dpas_f16 below, declared at global scope so it keeps its name. */
|
|
typedef short xe2_short8 __attribute__((ext_vector_type(8)));
|
|
typedef int xe2_int8 __attribute__((ext_vector_type(8)));
|
|
typedef float xe2_float8 __attribute__((ext_vector_type(8)));
|
|
extern SYCL_EXTERNAL xe2_float8 __spirv_SubgroupMatrixMultiplyAccumulateINTEL(int, xe2_short8, xe2_int8,
|
|
xe2_float8, int);
|
|
extern SYCL_EXTERNAL xe2_int8 __spirv_SubgroupMatrixMultiplyAccumulateINTEL(int, xe2_short8, xe2_int8,
|
|
xe2_int8, int);
|
|
|
|
namespace xe2 {
|
|
|
|
namespace syclex = sycl::ext::oneapi::experimental;
|
|
|
|
/* The sub-group every libxe2 kernel is written for: Xe2's native SIMD16. */
|
|
constexpr int kSub = 16;
|
|
|
|
/* Why the last launch on this thread refused, for the engine's error report and the tests. */
|
|
const char* last_refusal();
|
|
/* Exported: xe2_grf256 (the large-register kernels, a library of their own; CMakeLists.txt) refuses
|
|
* through libxe2's, so last_refusal() sees its reasons too. */
|
|
int refuse(const char* fmt, ...) __attribute__((format(printf, 1, 2), visibility("default")));
|
|
|
|
/* A row-major view of an operand read as rows of `n` elements: the base, and the element distance
|
|
* between consecutive rows. A tensor whose last axis is `n` may have padded rows, provided every
|
|
* leading axis collapses to one uniform pitch; anything else (`n` not the last extent) must be
|
|
* contiguous and is read as rows of `n`. The last axis must have stride 1. */
|
|
struct Rows {
|
|
char* base = nullptr;
|
|
int64_t pitch = 0; /* elements */
|
|
int64_t rows = 0;
|
|
uint32_t dtype = 0;
|
|
};
|
|
|
|
/* Fills `out` for `t` read as rows of `n`; RAD_E_UNSUPPORTED (with a reason) for a layout the
|
|
* kernels do not stride over. `rows` is numel / n, as libref counts them. */
|
|
int rows_view(const RadTensor* t, int64_t n, Rows* out, const char* what);
|
|
|
|
/* The queue behind a launch's stream, or null with a reason (not a SYCL stream). */
|
|
sycl::queue* queue_of(RadStream s);
|
|
|
|
/* A device workspace of at least `floats` floats for `q`, kept between launches and grown on
|
|
* demand (libref's ws_get, for kernels whose work-items each need a scratch block). The queue is
|
|
* in-order, so consecutive launches can share it; growing waits for the queue first. `slot` picks
|
|
* one of a few independent buffers, for an op that needs two at once. Null with a reason when the
|
|
* allocation fails. */
|
|
float* workspace(sycl::queue& q, size_t floats, int slot = 0);
|
|
|
|
/* Batched fp16 XMX GEMM into f32 (xe2_gemm_xmx.cpp): C rows padded up to a multiple of 128
|
|
* are written, so C + b * c_bs must hold ceil(M / 128) * 128 rows of N. */
|
|
int xmx_f16_batched(sycl::queue& q, const sycl::half* ab, int64_t ars, int64_t a_bs, const sycl::half* bb,
|
|
int64_t brs, int64_t b_bs, float* cb, int64_t c_bs, int64_t M, int64_t N, int64_t K,
|
|
int64_t batch);
|
|
/* fp16 XMX GEMM with a per-row and per-column output factor (xe2_gemm_xmx.cpp). */
|
|
int xmx_f16_scaled(sycl::queue& q, const sycl::half* ab, int64_t ars, const sycl::half* bb, int64_t brs,
|
|
char* yb, uint32_t ydt, int64_t yrs, int64_t ycs, int64_t M, int64_t mpad, int64_t N,
|
|
int64_t K, const float* rs, const float* cs);
|
|
|
|
/* One DPAS on operands held in registers (SPV_INTEL_subgroup_matrix_multiply_accumulate; libxe2's
|
|
* link enables it), sub-group 16, fp16 8x16 by 16x16 into f32: lane k holds A[0..7][k] in `a`, lane
|
|
* n holds B's column n as eight dwords of pairs (B[2i][n], B[2i+1][n]) in `b`, and C[0..7][n] in
|
|
* `c`. joint_matrix reaches the same instruction only through memory. */
|
|
inline xe2_float8 dpas_f16(xe2_short8 a, xe2_int8 b, xe2_float8 c) {
|
|
return __spirv_SubgroupMatrixMultiplyAccumulateINTEL(16, a, b, c, 0xC00); /* A, B packed fp16 */
|
|
}
|
|
/* The int8 form, K = 32, exact in int32: lane l holds A[0..7][2l, 2l + 1] (a pair of int8 a short),
|
|
* lane n B's column n as eight dwords of four consecutive k; A signed, B unsigned (0x31). */
|
|
inline xe2_int8 dpas_s8u8(xe2_short8 a, xe2_int8 b, xe2_int8 c) {
|
|
return __spirv_SubgroupMatrixMultiplyAccumulateINTEL(32, a, b, c, 0x31);
|
|
}
|
|
|
|
/* Runs `submit`, turning a SYCL exception into RAD_E_DEVICE with its message. */
|
|
template <typename F>
|
|
int submit(F&& f) {
|
|
try {
|
|
f();
|
|
} catch (const sycl::exception& e) {
|
|
return refuse("SYCL: %s", e.what());
|
|
}
|
|
return RAD_OK;
|
|
}
|
|
|
|
/* A tensor's element addressing for kernels that walk it in logical (row-major) order: libref's
|
|
* offlin, copied by value into the kernel. Contiguous costs nothing; a strided view (the q slice of
|
|
* a fused qkv row, which `cast` copies out) decomposes the index over its axes. */
|
|
struct Lin {
|
|
char* base = nullptr;
|
|
uint32_t dtype = 0;
|
|
uint32_t rank = 0;
|
|
bool contig = true;
|
|
int64_t shape[RAD_MAX_RANK] = {};
|
|
int64_t stride[RAD_MAX_RANK] = {};
|
|
|
|
static Lin of(const RadTensor* t) {
|
|
Lin l;
|
|
l.base = (char*)t->data;
|
|
l.dtype = t->dtype;
|
|
l.rank = t->rank;
|
|
int64_t acc = 1;
|
|
for (int i = (int)t->rank - 1; i >= 0; --i) {
|
|
l.shape[i] = t->shape[i];
|
|
l.stride[i] = t->stride[i];
|
|
if (t->shape[i] != 1 && t->stride[i] != acc) l.contig = false;
|
|
acc *= t->shape[i];
|
|
}
|
|
return l;
|
|
}
|
|
int64_t off(int64_t lin) const {
|
|
if (contig || rank == 0) return lin;
|
|
int64_t o = 0;
|
|
for (int i = (int)rank - 1; i >= 0; --i) {
|
|
const int64_t s = shape[i];
|
|
if (s <= 0) return 0;
|
|
o += (lin % s) * stride[i];
|
|
lin /= s;
|
|
}
|
|
return o;
|
|
}
|
|
};
|
|
|
|
/* An index or position element as libref's ld_int reads it. */
|
|
inline int64_t ld_index(uint32_t dt, const void* p, int64_t i) {
|
|
switch (dt) {
|
|
case RAD_I32: return (int64_t)((const int32_t*)p)[i];
|
|
case RAD_U32: return (int64_t)((const uint32_t*)p)[i];
|
|
case RAD_I64: return ((const int64_t*)p)[i];
|
|
case RAD_I16: return (int64_t)((const int16_t*)p)[i];
|
|
case RAD_I8: return (int64_t)((const int8_t*)p)[i];
|
|
case RAD_U8: return (int64_t)((const uint8_t*)p)[i];
|
|
default: return 0;
|
|
}
|
|
}
|
|
|
|
/* The top-k selection key (sample_topk, sample_logprobs, row_topk): the value's order-preserving
|
|
* bits above the complemented index, so the largest key is the largest value and, among equal
|
|
* values, the lowest index -- libref's topk_next. -0 is folded to +0 (libref compares floats);
|
|
* NaN is key 0, never selected. */
|
|
inline uint64_t sel_key(float v, int64_t i) {
|
|
if (sycl::isnan(v)) return 0;
|
|
if (v == 0.0f) v = 0.0f;
|
|
uint32_t b;
|
|
std::memcpy(&b, &v, 4);
|
|
const uint32_t ord = (b & 0x80000000u) ? ~b : (b | 0x80000000u);
|
|
return ((uint64_t)ord << 32) | (uint64_t)(~(uint32_t)i);
|
|
}
|
|
inline int64_t key_index(uint64_t k) { return (int64_t)(~(uint32_t)k); }
|
|
inline float key_value(uint64_t k) {
|
|
const uint32_t ord = (uint32_t)(k >> 32);
|
|
const uint32_t b = (ord & 0x80000000u) ? (ord & 0x7fffffffu) : ~ord;
|
|
float v;
|
|
std::memcpy(&v, &b, 4);
|
|
return v;
|
|
}
|
|
|
|
/* Stores an integer index in an integer dtype (the counterpart of ld_index). */
|
|
inline void st_index(uint32_t dt, void* p, int64_t i, int64_t v) {
|
|
switch (dt) {
|
|
case RAD_I32: ((int32_t*)p)[i] = (int32_t)v; break;
|
|
case RAD_U32: ((uint32_t*)p)[i] = (uint32_t)v; break;
|
|
case RAD_I64: ((int64_t*)p)[i] = v; break;
|
|
case RAD_I16: ((int16_t*)p)[i] = (int16_t)v; break;
|
|
default: break;
|
|
}
|
|
}
|
|
|
|
/* libref takes exp in double and rounds it once (sigmoid, silu); on this card that is ~3x the cost of
|
|
* the rest of an activation. Rounded to bf16, two f32 values a few ulps apart give the same bf16
|
|
* unless they straddle a rounding tie, so a kernel may compute the value with a float exp (a few ulps
|
|
* from libref's chain) and run libref's chain again only where near_bf16_tie says the result lies
|
|
* within 16 ulps of a tie -- or is not finite. The bf16 is then libref's (probe silu.cpp: 0 of 21M
|
|
* silu outputs differ, at a third of the double form's time; the float chain alone, 85). */
|
|
inline bool near_bf16_tie(float s) {
|
|
const uint32_t u = sycl::bit_cast<uint32_t>(s);
|
|
const uint32_t lo = u & 0xFFFFu;
|
|
const uint32_t d = lo > 0x8000u ? lo - 0x8000u : 0x8000u - lo;
|
|
return d <= 16u || (u & 0x7F800000u) == 0x7F800000u;
|
|
}
|
|
/* 1 / (1 + e^-x) with a float exp: the fast half of a near_bf16_tie pair. */
|
|
inline float sigmoid_fexp(float x) { return 1.0f / (1.0f + sycl::exp(-x)); }
|
|
|
|
/* Rounds `v` through a store and load of `dt`, as libref's round_dt does. The float dtypes the
|
|
* kernels meet are converted directly: a round trip through a stack buffer can land in private
|
|
* (scratch) memory on the card, a global-memory round trip an element. */
|
|
inline float round_dt(uint32_t dt, float v) {
|
|
switch (dt) {
|
|
case RAD_F32: return v;
|
|
case RAD_BF16: return rad_bf16_to_f32(rad_f32_to_bf16(v));
|
|
case RAD_F16: return rad_f16_to_f32(rad_f32_to_f16(v));
|
|
default: break;
|
|
}
|
|
uint64_t buf = 0;
|
|
rad_store_f32(&buf, dt, 0, v);
|
|
return rad_load_f32(&buf, dt, 0);
|
|
}
|
|
|
|
} // namespace xe2
|