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
82 lines
2.7 KiB
C++
82 lines
2.7 KiB
C++
/* xe2_common.cpp -- the shared helpers of xe2_common.h. */
|
|
#include "xe2_common.h"
|
|
|
|
#include <mutex>
|
|
#include <vector>
|
|
|
|
#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;
|
|
}
|
|
|
|
float* workspace(sycl::queue& q, size_t floats) {
|
|
struct Ws { sycl::queue* q; float* p; size_t n; };
|
|
static std::mutex mu;
|
|
static std::vector<Ws> all;
|
|
std::lock_guard<std::mutex> lk(mu);
|
|
for (Ws& w : all) {
|
|
if (w.q != &q) continue;
|
|
if (w.n >= floats) return w.p;
|
|
q.wait();
|
|
sycl::free(w.p, q);
|
|
w.p = sycl::malloc_device<float>(floats, q);
|
|
w.n = w.p ? floats : 0;
|
|
if (!w.p) refuse("workspace: %zu floats", floats);
|
|
return w.p;
|
|
}
|
|
float* p = sycl::malloc_device<float>(floats, q);
|
|
if (!p) { refuse("workspace: %zu floats", floats); return nullptr; }
|
|
all.push_back({ &q, p, floats });
|
|
return p;
|
|
}
|
|
|
|
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
|