Archived
Kernels, each libref's arithmetic in libref's order: - gemm_nt_q at m2xfp4 (xe2_gemm_m2.cpp) with libref's m2 layout hooks, and givens_dense_quant (xe2_givens.cpp, the rotation in double as libref's) - the gated delta net (xe2_gdn.cpp): gdn_conv_prep, gdn_conv_update, gdn_kkt_solve, gdn_chunk_scan, gdn_recurrent_update (per-candidate and anchor state forms), gdn_gated_rmsnorm, gdn_conv_recurrent_update; each work-item one iteration of libref's parallel loop, the per-(sequence, head) workspaces in a shared device workspace (xe2_common: workspace()) - rope's multi-component modes (mrope, imrope, axial) and rope_table - logits_gemm over an fp8 128x128 joined head, gemm_nt_bias with its epilogue - mul, sigmoid, scatter_rows, layernorm, grid_embed, attn_dense (the vision tower) - dflash_conv and logit_rerank (plain and joined head) for the DFlash2 drafter libref's gemm_m2 gains the parts (pb1/pb2) every other ParoQuant grid and libr4d's mx_parts already define -- weight rows [0, e1), [e1, e2), [e2, N) read activation copies 0, 1, 2 side by side -- since this model's fused projections are rotated per piece and the oracle declined them; its shape hook widens a and a_scale by the part count. Both m2 arms now take the stored codes-then-meta blob as the engine describes it, u8 [N, K], beside the shape hook's rank-1 form (libref refused the engine's launch; only libr4d had been run on it). rad-kbench on the fixture: every new row at rel_l2 0 (90 gdn/givens cases, 108 rope, 80 elementwise, 20 attn_dense, the drafter's and the heads'). On the B65 the model starts with every op on the card and answers "The capital of France is" -> " Paris.\nThe capital of Germany is Berlin.\nThe capital of Italy is" (greedy), at 0.1 tok/s: the correctness-first kernels. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1
114 lines
5.5 KiB
C++
114 lines
5.5 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; }
|
|
|
|
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;
|
|
const bool int_pair = is_int(X->dtype) && is_int(Y->dtype);
|
|
/* Either side may be a strided view: the graph casts q out of the fused qkv row, 2048 of
|
|
* every 2560 elements. Elements are walked in logical order through libref's offlin. */
|
|
const Lin x = Lin::of(X), y = Lin::of(Y);
|
|
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;
|
|
const int64_t xo = x.off(i), yo = y.off(i);
|
|
if (int_pair) st_index(y.dtype, y.base, yo, ld_index(x.dtype, x.base, xo));
|
|
else rad_store_f32(y.base, y.dtype, yo, rad_load_f32(x.base, x.dtype, xo));
|
|
});
|
|
});
|
|
}
|
|
|
|
} // namespace xe2
|