This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/libxe2/xe2_gather.cpp
T
pascallinxweilerandClaude Opus 5.5 fca4cbe3b5 libxe2: rope, embed_lookup, gather_rows, cast
rope builds its frequency ladder on the host per launch exactly as libref's rope_freqs does (a
double pow, then YaRN or llama3 from rad_yarn.h) and takes cos and sin of the f32 angle in double on
the card, as libref does; positions reach 131072 at MiniCPM5's context. neox and gptj with one
position component; the row says so with a mode constraint and the launch refuses anything else by
name.

The gathers write a zero row for an index outside the table, libr4d's rule: libref instead checks
every index on the host before running, which a launch cannot do without waiting on its stream.

rad-kbench against libref: fixture 127/127 and MiniCPM5-2B 94/94, rel_l2 0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1
2026-10-23 16:05:59 +02:00

125 lines
5.7 KiB
C++

/* xe2_gather.cpp -- row gathers and the dtype cast.
*
* embed_lookup x[m, :] = wte[tok[m] - vocab_offset, :] * mult
* gather_rows y[i, :] = x[idx[i], :]
* cast y = x in y's dtype; integer to integer bypasses f32, as libref's does
*
* Index checks differ from libref's in one way, by necessity: libref reads every index on the host
* before running and returns RAD_E_INVAL for one past the table, but a launch cannot read device
* memory without waiting for the stream, which a launch must not do. Here such a row is written as
* zeros, the rule libr4d's embed_lookup_bf16 states ("a negative or out-of-range id writes a zero
* row"). A negative index is a zero row in both.
*/
#include "xe2_common.h"
#include "xe2_ops.h"
namespace xe2 {
namespace {
bool is_int(uint32_t dt) { return dt == RAD_I32 || dt == RAD_I64 || dt == RAD_U32 || dt == RAD_I16; }
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;
}
}
constexpr size_t kWg = 256;
} // namespace
/* ------------------------------------------------------------------ embed_lookup */
int embed_lookup(const RadArgs* a, RadStream st) {
if (!rad_args_have(a, 3)) return RAD_E_INVAL;
const RadTensor *TOK = &a->t[0], *WTE = &a->t[1], *X = &a->t[2];
const int64_t n_embd = rad_args_geti_or(a, "n_embd", WTE->rank >= 2 ? WTE->shape[WTE->rank - 1] : 0);
const int64_t n_vocab = rad_args_geti_or(a, "n_vocab", WTE->rank >= 2 ? WTE->shape[WTE->rank - 2] : 0);
const int64_t voff = rad_args_geti_or(a, "vocab_offset", 0);
const float mult = (float)rad_args_getf_or(a, "mult", 1.0);
if (n_embd <= 0 || n_vocab <= 0 || voff < 0) return RAD_E_SHAPE;
Rows x;
if (int rc = rows_view(X, n_embd, &x, "embed_lookup x"); rc < 0) return rc;
const int64_t M = x.rows;
if (M <= 0 || rad_tensor_numel(TOK) < M || rad_tensor_numel(WTE) < n_vocab * n_embd)
return RAD_E_SHAPE;
if (!rad_tensor_is_contiguous(TOK)) return refuse("embed_lookup: token ids not contiguous");
const int64_t ws = WTE->rank >= 2 ? WTE->stride[WTE->rank - 2] : n_embd;
const int64_t wc = WTE->rank >= 2 ? WTE->stride[WTE->rank - 1] : 1;
const char* wb = (const char*)WTE->data;
const uint32_t wdt = WTE->dtype;
const char* tb = (const char*)TOK->data;
const uint32_t tdt = TOK->dtype;
sycl::queue* q = queue_of(st);
if (!q) return RAD_E_DEVICE;
return submit([&] {
syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)M * kWg, kWg}, [=](sycl::nd_item<1> it) {
const int64_t m = (int64_t)it.get_group(0);
const int64_t id = ld_index(tdt, tb, m) - voff;
const bool have = id >= 0 && id < n_vocab;
for (int64_t i = (int64_t)it.get_local_id(0); i < n_embd; i += kWg)
rad_store_f32(x.base, x.dtype, m * x.pitch + i,
have ? rad_load_f32(wb, wdt, id * ws + i * wc) * mult : 0.0f);
});
});
}
/* ------------------------------------------------------------------ gather_rows */
int gather_rows(const RadArgs* a, RadStream st) {
if (!rad_args_have(a, 3)) return RAD_E_INVAL;
const RadTensor *X = &a->t[0], *IDX = &a->t[1], *Y = &a->t[2];
const int64_t n = rad_args_geti_or(a, "n", Y->rank >= 1 ? Y->shape[Y->rank - 1] : 0);
if (n <= 0) return RAD_E_SHAPE;
Rows x, y;
if (int rc = rows_view(Y, n, &y, "gather_rows y"); rc < 0) return rc;
if (int rc = rows_view(X, n, &x, "gather_rows x"); rc < 0) return rc;
const int64_t M = y.rows, src_rows = x.rows;
if (M <= 0 || src_rows <= 0 || rad_tensor_numel(IDX) < M) return RAD_E_SHAPE;
if (!rad_tensor_is_contiguous(IDX)) return refuse("gather_rows: idx not contiguous");
const char* ib = (const char*)IDX->data;
const uint32_t idt = IDX->dtype;
sycl::queue* q = queue_of(st);
if (!q) return RAD_E_DEVICE;
return submit([&] {
syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)M * kWg, kWg}, [=](sycl::nd_item<1> it) {
const int64_t m = (int64_t)it.get_group(0);
const int64_t r = ld_index(idt, ib, m);
const bool have = r >= 0 && r < src_rows;
for (int64_t i = (int64_t)it.get_local_id(0); i < n; i += kWg)
rad_store_f32(y.base, y.dtype, m * y.pitch + i,
have ? rad_load_f32(x.base, x.dtype, r * x.pitch + i) : 0.0f);
});
});
}
/* ------------------------------------------------------------------ cast */
int cast(const RadArgs* a, RadStream st) {
if (!rad_args_have(a, 2)) return RAD_E_INVAL;
const RadTensor *X = &a->t[0], *Y = &a->t[1];
const int64_t nel = rad_tensor_numel(Y);
if (nel <= 0 || rad_tensor_numel(X) < nel) return RAD_E_SHAPE;
if (!rad_tensor_is_contiguous(X) || !rad_tensor_is_contiguous(Y))
return refuse("cast: strided operands are not served");
const bool int_pair = is_int(X->dtype) && is_int(Y->dtype);
const char* xb = (const char*)X->data;
char* yb = (char*)Y->data;
const uint32_t xdt = X->dtype, ydt = Y->dtype;
sycl::queue* q = queue_of(st);
if (!q) return RAD_E_DEVICE;
const size_t groups = (size_t)((nel + (int64_t)kWg - 1) / (int64_t)kWg);
return submit([&] {
syclex::nd_launch(*q, sycl::nd_range<1>{groups * kWg, kWg}, [=](sycl::nd_item<1> it) {
const int64_t i = (int64_t)it.get_global_id(0);
if (i >= nel) return;
if (int_pair) st_index(ydt, yb, i, ld_index(xdt, xb, i));
else rad_store_f32(yb, ydt, i, rad_load_f32(xb, xdt, i));
});
});
}
} // namespace xe2