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_common.cpp
T
claudeandClaude Opus 5.5 15944be7e3 libxe2: fast decode kernels for Qwen3.8-27B-PARO-M2XFP4 -- 0.1 -> 12.1 tok/s on the B65
- m2xfp4 GEMV (M <= 16): e2m1 decoded to fp16 by bits alone ((mag << 9) | sign << 15 is exactly
  e2m1 * 2^-14; the card keeps fp16 subnormals), K permuted (u, u + 4) inside each 8 so a code
  pair is one shift and mask, the activation as fp16(a * a_scale / R * 2^14) in that order, half2
  subgroup and group sums, f32 across groups; four columns a sub-group at decode. The scalar
  form was ALU-bound (~10 ops a weight, 80 ms a token); this one runs at 415-525 GB/s on the
  27B's shapes in a standalone probe. rad-kbench against libref on the container: rel_l2 ~5e-5 to
  ~3e-3, tolerance 4e-2.
- m2xfp4 prefill (M >= 17): the weight decoded to bf16 (exact) and the activation to
  bf16(e4m3 * scale), then the bf16 XMX GEMM per part.
- givens_dense_quant: a sub-group an output, the rotation row read coalesced, libref's double sum
  as a float pair (two-product and two-sum, ~48 bits); the double form stays for XE2_EXACT=1.
  146 -> 5.4 ms a token.
- the joined fp8 head (logits_gemm, M <= 16): E4M3 to fp16 by bits ((b & 0x7F) << 7 is E4M3 *
  2^-8), bytes (0, 2), (1, 3) as half pairs; an i8 head or a narrow scale block element by element.
  43 -> 3.9 ms.
- gated delta net, still bit-identical to libref: the decode step split over 16-column blocks
  (384 work-groups, not 48) with the gated norm in a second kernel and the anchor replay its own
  launch (block 0 rewrites the factors other blocks read); the chunked scan and the kkt solve
  work-group parallel; persistent staging (workspace slots) instead of a malloc and a wait a call.
- rmsnorm_quant_fp8: the row in local memory, loads issued before stores, the ordered sum fed by
  sub-group broadcasts -- still libref's chain (a probe puts that chain's floor at ~32 us for 5120).
- round_dt converts the float dtypes directly.

rad-kbench: 1410/1410 on the fixture; the 27B container's M <= 32 bands 77/77. Decode (greedy, no
speculation) 12.1 tok/s, coherent text; with the DFlash2 drafter 5.3 tok/s and the same text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1
2026-10-24 03:22:48 +02:00

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, int slot) {
struct Ws { sycl::queue* q; int slot; 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 || w.slot != slot) 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, slot, 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