libxe2: the Intel kernel library's skeleton, and its first op, add

libxe2 is a kernel plugin like any other, built by rad_add_plugin from the icpx sub-build into the
build tree's $RADIANCE_HOME. It declares no schemas (every op it serves is libref's vocabulary), only
device rows, written with designated initialisers. xe2_common.h holds what every kernel needs: the
launch's queue (rad_sycl.h), row views over padded operands, refusals that name the geometry, and
libref's own dtype conversions so results round where the reference's do.

add passes rad-kbench against libref at tolerance zero, on the recorded fixture (30 cases) and on
MiniCPM5-2B's container (20 cases), inside guarded allocations. In a SYCL build the kbench replays
run libxe2 on the card, so they are labelled sycl there, as they are gpu in a HIP build.

scripts/sycl/notify.sh posts to the Discord webhook when the port needs the user.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1
This commit is contained in:
claudeandClaude Opus 5.5 committed 2026-10-23 15:55:39 +02:00
1 parent 8addda231b
commit ea065f596c
11 files changed
+362 -10

No files matched your search

+34
View File
@@ -0,0 +1,34 @@
# libxe2 -- radiance's kernel library for Intel Xe2 GPUs (Battlemage; the Arc Pro B65), in SYCL.
# docs/SYCL_PORTING.md has the design, docs/SYCL_PROGRESS.md the state.
#
# Links radiance::abi and the SYCL runtime, nothing of radiance's. Built by icpx: in radiance's tree
# through the sycl/ sub-build, or standalone against an installed radiance:
# cmake -S libxe2 -B build -DCMAKE_CXX_COMPILER=icpx -DCMAKE_PREFIX_PATH=<radiance prefix>
if(NOT COMMAND rad_add_plugin)
cmake_minimum_required(VERSION 3.21)
project(libxe2 LANGUAGES CXX)
find_package(radiance REQUIRED)
rad_plugin_defaults()
endif()
if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "IntelLLVM")
message(FATAL_ERROR "libxe2 is SYCL and needs icpx (-DCMAKE_CXX_COMPILER=icpx); this configure "
"has ${CMAKE_CXX_COMPILER_ID}.")
endif()
if(NOT RAD_SYCL_TARGETS)
set(RAD_SYCL_TARGETS "intel_gpu_bmg_g31")
endif()
file(GLOB XE2_SRC CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
rad_add_plugin(libxe2 kernels SOURCES ${XE2_SRC})
# -ffp-contract=off is a correctness flag here as in libref: kernels are checked against it, so
# `a*b + c` rounds twice on both sides. It follows -ffp-model=precise, which would otherwise set
# contraction back on.
set(_xe2_sycl -fsycl "-fsycl-targets=${RAD_SYCL_TARGETS}")
target_compile_options(libxe2 PRIVATE ${_xe2_sycl} -ffp-model=precise -ffp-contract=off -Wno-overriding-option
-fvisibility=hidden)
get_filename_component(_xe2_icpx_bin "${CMAKE_CXX_COMPILER}" DIRECTORY)
get_filename_component(_xe2_oneapi_lib "${_xe2_icpx_bin}/../lib" ABSOLUTE)
target_link_options(libxe2 PRIVATE ${_xe2_sycl} "LINKER:-rpath,${_xe2_oneapi_lib}"
"LINKER:--no-undefined")
+57
View File
@@ -0,0 +1,57 @@
/* xe2_common.cpp -- the shared helpers of xe2_common.h. */
#include "xe2_common.h"
#include <cstdarg>
namespace xe2 {
namespace {
thread_local char t_refusal[512] = "";
}
const char* last_refusal() { return t_refusal; }
int refuse(const char* fmt, ...) {
va_list ap;
va_start(ap, fmt);
std::vsnprintf(t_refusal, sizeof t_refusal, fmt, ap);
va_end(ap);
std::fprintf(stderr, "libxe2: %s\n", t_refusal);
return RAD_E_UNSUPPORTED;
}
sycl::queue* queue_of(RadStream s) {
sycl::queue* q = rad_sycl_queue(s);
if (!q) refuse("the stream is not a SYCL stream of version %u (another backend?)",
RAD_SYCL_STREAM_VERSION);
return q;
}
int rows_view(const RadTensor* t, int64_t n, Rows* out, const char* what) {
if (!t || !t->data || n <= 0) return refuse("%s: missing operand or n <= 0", what);
out->base = (char*)t->data;
out->dtype = t->dtype;
out->rows = rad_tensor_numel(t) / n;
if (t->rank >= 2 && t->shape[t->rank - 1] == n) {
if (t->stride[t->rank - 1] != 1) return refuse("%s: last-axis stride %lld, not 1", what,
(long long)t->stride[t->rank - 1]);
/* Every leading axis must step by the one below it times its extent, so a row index maps
* to one pitch (libref's rowoff walks them; here they must agree). */
const int64_t pitch = t->stride[t->rank - 2];
int64_t span = pitch;
for (int i = (int)t->rank - 3; i >= 0; --i) {
span *= t->shape[i + 1];
if (t->shape[i] != 1 && t->stride[i] != span)
return refuse("%s: leading axes do not collapse to one row pitch", what);
}
out->pitch = pitch;
return RAD_OK;
}
if (!rad_tensor_is_contiguous(t))
return refuse("%s: read as rows of %lld but neither its last extent nor contiguous", what,
(long long)n);
out->pitch = n;
return RAD_OK;
}
} // namespace xe2
+67
View File
@@ -0,0 +1,67 @@
/* 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>
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();
int refuse(const char* fmt, ...) __attribute__((format(printf, 1, 2)));
/* 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);
/* 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;
}
/* Rounds `v` through a store and load of `dt`, as libref's round_dt does. */
inline float round_dt(uint32_t dt, float v) {
uint64_t buf = 0;
rad_store_f32(&buf, dt, 0, v);
return rad_load_f32(&buf, dt, 0);
}
} // namespace xe2
+47
View File
@@ -0,0 +1,47 @@
/* xe2_elementwise.cpp -- elementwise ops, one work-item an element.
*
* add: y = a + b, b broadcast along rows when it holds one row (a bias add), and with `div`,
* y = rd(rd(a + b) / div), rd being y's dtype: libref's elementwise2 (ref_elementwise.cpp).
*/
#include "xe2_common.h"
#include "xe2_ops.h"
namespace xe2 {
int add(const RadArgs* a, RadStream s) {
if (!rad_args_have(a, 3)) return refuse("add: missing operand");
const RadTensor *A = &a->t[0], *B = &a->t[1], *Y = &a->t[2];
const int64_t n = rad_args_geti_or(a, "n", rad_tensor_numel(Y));
const float div = (float)rad_args_getf_or(a, "div", 1.0);
if (!(div != 0.0f) || !sycl::isfinite(div)) return RAD_E_INVAL;
if (n <= 0) return RAD_E_SHAPE;
Rows ya, aa, ba;
if (int rc = rows_view(Y, n, &ya, "add y"); rc < 0) return rc;
const int64_t rows = ya.rows;
if (rows <= 0 || rad_tensor_numel(A) < rows * n) return RAD_E_SHAPE;
const bool bcast = rad_tensor_numel(B) == n && rows > 1;
if (!bcast && rad_tensor_numel(B) < rows * n) return RAD_E_SHAPE;
if (int rc = rows_view(A, n, &aa, "add a"); rc < 0) return rc;
if (int rc = rows_view(B, n, &ba, "add b"); rc < 0) return rc;
if (bcast) ba.pitch = 0;
sycl::queue* q = queue_of(s);
if (!q) return RAD_E_DEVICE;
const int64_t total = rows * n;
const size_t wg = 256;
const size_t groups = (size_t)((total + (int64_t)wg - 1) / (int64_t)wg);
return submit([&] {
syclex::nd_launch(*q, sycl::nd_range<1>{groups * wg, wg}, [=](sycl::nd_item<1> it) {
const int64_t g = (int64_t)it.get_global_id(0);
if (g >= total) return;
const int64_t r = g / n, i = g % n;
const float u = rad_load_f32(aa.base, aa.dtype, r * aa.pitch + i);
const float v = rad_load_f32(ba.base, ba.dtype, r * ba.pitch + i);
float x = u + v;
if (div != 1.0f) x = round_dt(ya.dtype, x) / div;
rad_store_f32(ya.base, ya.dtype, r * ya.pitch + i, x);
});
});
}
} // namespace xe2
+9
View File
@@ -0,0 +1,9 @@
/* xe2_ops.h -- every libxe2 launch, by op. The rows that select them are in xe2_rows.cpp. */
#pragma once
#include "rad_abi.h"
namespace xe2 {
int add(const RadArgs* a, RadStream s);
} // namespace xe2
+48
View File
@@ -0,0 +1,48 @@
/* xe2_rows.cpp -- libxe2's plugin exports: identity and the kernel rows the engine selects from.
*
* libxe2 is radiance's kernel library for Intel Xe2 GPUs (the Arc Pro B65, Battlemage G31) through
* SYCL. It defines no op schemas of its own: every op it serves is in the conventional vocabulary,
* whose schemas libref fixes (docs/OPS.md). All rows are device-domain; an op libxe2 lacks has no
* row here, and on the SYCL backend the engine stops at startup naming it (docs/SYCL_PORTING.md
* §4.4).
*/
#include "rad_abi.h"
#include "xe2_ops.h"
namespace {
/* Rows by field name (C++20 designated initialisers must follow declaration order, so the compiler
* checks it), unlike a positional table where a shifted field can still compile. */
const RadKernelInfo kKernels[] = {
{ .name = "xe2_add", .op = "add", .family = "elem",
.computes = "elementwise sum, b broadcast along rows when it is one row; optional div",
.shape = "any n and row count; rows may be padded, the last axis dense",
.dtypes = "any float dtype in and out (bf16, f16, f32, fp8), f32 arithmetic, rounded as libref",
.domain = RAD_DOMAIN_DEVICE, .priority = 0,
.launch = xe2::add },
};
const RadPluginInfo kInfo = {
RAD_PLUGIN_KERNEL, "libxe2", "0.1",
"Intel Xe2 (Battlemage) kernels in SYCL, checked against libref",
"intel_gpu_bmg_g31",
};
} // namespace
extern "C" {
__attribute__((visibility("default"))) uint32_t rad_plugin_abi_version(void) {
return RAD_ABI_VERSION;
}
__attribute__((visibility("default"))) const RadPluginInfo* rad_plugin_info(void) { return &kInfo; }
__attribute__((visibility("default"))) int rad_kernel_schema_count(void) { return 0; }
__attribute__((visibility("default"))) const RadOpSchema* rad_kernel_schema_at(int) { return nullptr; }
__attribute__((visibility("default"))) int rad_kernel_count(void) {
return (int)(sizeof kKernels / sizeof kKernels[0]);
}
__attribute__((visibility("default"))) const RadKernelInfo* rad_kernel_at(int i) {
return i >= 0 && i < rad_kernel_count() ? &kKernels[i] : nullptr;
}
} // extern "C"