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
This commit is contained in:
claudeandClaude Opus 5.5 committed 2026-10-24 03:22:48 +02:00
1 parent 3158324bdb
commit 15944be7e3
9 files changed
+1030 -35

No files matched your search

+4 -4
View File
@@ -30,13 +30,13 @@ sycl::queue* queue_of(RadStream s) {
return q;
}
float* workspace(sycl::queue& q, size_t floats) {
struct Ws { sycl::queue* q; float* p; size_t n; };
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) continue;
if (w.q != &q || w.slot != slot) continue;
if (w.n >= floats) return w.p;
q.wait();
sycl::free(w.p, q);
@@ -47,7 +47,7 @@ float* workspace(sycl::queue& q, size_t floats) {
}
float* p = sycl::malloc_device<float>(floats, q);
if (!p) { refuse("workspace: %zu floats", floats); return nullptr; }
all.push_back({ &q, p, floats });
all.push_back({ &q, slot, p, floats });
return p;
}
+13 -4
View File
@@ -49,9 +49,10 @@ sycl::queue* queue_of(RadStream s);
/* A device workspace of at least `floats` floats for `q`, kept between launches and grown on
* demand (libref's ws_get, for kernels whose work-items each need a scratch block). The queue is
* in-order, so consecutive launches can share it; growing waits for the queue first. Null with a
* reason when the allocation fails. */
float* workspace(sycl::queue& q, size_t floats);
* in-order, so consecutive launches can share it; growing waits for the queue first. `slot` picks
* one of a few independent buffers, for an op that needs two at once. Null with a reason when the
* allocation fails. */
float* workspace(sycl::queue& q, size_t floats, int slot = 0);
/* Runs `submit`, turning a SYCL exception into RAD_E_DEVICE with its message. */
template <typename F>
@@ -147,8 +148,16 @@ inline void st_index(uint32_t dt, void* p, int64_t i, int64_t v) {
}
}
/* Rounds `v` through a store and load of `dt`, as libref's round_dt does. */
/* Rounds `v` through a store and load of `dt`, as libref's round_dt does. The float dtypes the
* kernels meet are converted directly: a round trip through a stack buffer can land in private
* (scratch) memory on the card, a global-memory round trip an element. */
inline float round_dt(uint32_t dt, float v) {
switch (dt) {
case RAD_F32: return v;
case RAD_BF16: return rad_bf16_to_f32(rad_f32_to_bf16(v));
case RAD_F16: return rad_f16_to_f32(rad_f32_to_f16(v));
default: break;
}
uint64_t buf = 0;
rad_store_f32(&buf, dt, 0, v);
return rad_load_f32(&buf, dt, 0);
+388 -18
View File
@@ -349,6 +349,59 @@ int gdn_kkt_solve(const RadArgs* a, RadStream st) {
const IV cuv = iv_of(cu);
sycl::queue* qu = queue_of(st);
if (!qu) return RAD_E_DEVICE;
if (chunk <= 256 && chunk % kSub == 0) {
/* A work-group a (sequence, v head), a work-item a column j of the chunk: for each row i,
* work-item j forms row[j] (its sum over d in order) and then column j of the inverse,
* s[j] = delta - sum over l of row[l] A[l][j] in l order -- libref's chains, spread over j. */
const int64_t need = chunk * chunk + chunk * K;
float* ws = workspace(*qu, (size_t)(N * H * need));
if (!ws) return RAD_E_DEVICE;
const size_t wg = (size_t)chunk;
return submit([&] {
qu->submit([&](sycl::handler& hd) {
sycl::local_accessor<float, 1> row(sycl::range<1>((size_t)chunk), hd);
sycl::local_accessor<float, 1> gcl(sycl::range<1>((size_t)chunk), hd);
hd.parallel_for(sycl::nd_range<1>{(size_t)(N * H) * wg, wg}, [=](sycl::nd_item<1> it) {
const int64_t nh = (int64_t)it.get_group(0);
const int64_t n = nh / H, h = nh % H, hk = h / grp;
const int64_t j = (int64_t)it.get_local_id(0);
auto grpw = it.get_group();
const int64_t bos = cuv.at(n), slen = cuv.at(n + 1) - bos;
float* Am = ws + nh * need;
float* kc = Am + chunk * chunk;
for (int64_t c0 = 0; c0 < slen; c0 += chunk) {
const int64_t L = (c0 + chunk < slen) ? chunk : slen - c0;
sycl::group_barrier(grpw);
if (j < L) {
const int64_t tj = bos + c0 + j;
gcl[j] = gs.ld(at2(gv, tj, h));
for (int64_t d = 0; d < K; ++d) kc[j * K + d] = ks.ld(at3(kv, tj, hk, d));
}
sycl::group_barrier(grpw);
for (int64_t i = 0; i < L; ++i) {
const int64_t ti = bos + c0 + i;
if (j < i) {
const float bi = bs.ld(at2(bv, ti, h));
float rj = 0.0f;
for (int64_t d = 0; d < K; ++d) rj += kc[i * K + d] * kc[j * K + d];
row[j] = bi * rj * rexp(gcl[i] - gcl[j]);
}
sycl::group_barrier(grpw);
if (j <= i) {
float sj = (i == j) ? 1.0f : 0.0f;
for (int64_t l = j; l < i; ++l) sj -= row[l] * Am[l * chunk + j];
Am[i * chunk + j] = sj;
} else {
Am[i * chunk + j] = 0.0f;
}
As.st(at3(Av, ti, h, j), Am[i * chunk + j]);
sycl::group_barrier(grpw);
}
}
});
});
});
}
const int64_t need = chunk * chunk + 3 * chunk + 2 * chunk * K;
float* ws = workspace(*qu, (size_t)(N * H * need));
if (!ws) return RAD_E_DEVICE;
@@ -442,6 +495,106 @@ int gdn_chunk_scan(const RadArgs* a, RadStream st) {
const IV cuv = iv_of(cu);
sycl::queue* qu = queue_of(st);
if (!qu) return RAD_E_DEVICE;
if (V % kSub == 0 && V <= 256 && chunk <= V && K * V <= 128 * 128) {
/* A work-group a (sequence, v head), a work-item a value column e (and, for the Q K^T
* rows, a chunk position j): the state in local memory, the chunk's widened operands in
* the workspace. Every element is libref's chain in libref's order: the sums over d for
* K S^T, Q S^T and the rows, over j for V' and O, and the state's decay then sum over j. */
const int64_t need = 3 * chunk * V + 2 * chunk * K + chunk * chunk;
float* ws = workspace(*qu, (size_t)(N * H * need));
if (!ws) return RAD_E_DEVICE;
const size_t wg = (size_t)V;
return submit([&] {
qu->submit([&](sycl::handler& hd) {
sycl::local_accessor<float, 1> St(sycl::range<1>((size_t)(K * V)), hd);
sycl::local_accessor<float, 1> gcb(sycl::range<1>((size_t)(2 * chunk)), hd);
hd.parallel_for(sycl::nd_range<1>{(size_t)(N * H) * wg, wg}, [=](sycl::nd_item<1> it) {
const int64_t nh = (int64_t)it.get_group(0);
const int64_t n = nh / H, h = nh % H, hk = h / grp;
const int64_t e = (int64_t)it.get_local_id(0);
auto gw = it.get_group();
const int64_t bos = cuv.at(n), slen = cuv.at(n + 1) - bos;
const int64_t sn = has_si ? sis.li(n * sw) : n;
if (sn < 0) return; /* uniform */
float* Wm = ws + nh * need;
float* Vp = Wm + chunk * V;
float* vc = Vp + chunk * V;
float* kc = vc + chunk * V;
float* qc = kc + chunk * K;
float* R = qc + chunk * K; /* [chunk, chunk]: row i of sA */
float* gc = &gcb[0];
float* bc = gc + chunk;
for (int64_t d = 0; d < K; ++d) St[d * V + e] = has_h0 ? h0s.ld(at4(h0v, sn, h, e, d)) : 0.0f;
for (int64_t c0 = 0; c0 < slen; c0 += chunk) {
const int64_t L = (c0 + chunk < slen) ? chunk : slen - c0;
sycl::group_barrier(gw);
for (int64_t j = 0; j < L; ++j) {
const int64_t tj = bos + c0 + j;
vc[j * V + e] = vs.ld(at3(vv, tj, h, e));
for (int64_t d = e; d < K; d += V) {
kc[j * K + d] = ks.ld(at3(kv, tj, hk, d));
qc[j * K + d] = qs.ld(at3(qv, tj, hk, d));
}
}
if (e < L) {
const int64_t tj = bos + c0 + e;
gc[e] = gs.ld(at2(gv, tj, h));
bc[e] = bs.ld(at2(bv, tj, h));
}
sycl::group_barrier(gw);
const float glast = gc[L - 1];
/* W = beta . (V - e^{g_j} . (K S^T)) */
for (int64_t j = 0; j < L; ++j) {
const float ge = rexp(gc[j]);
float acc = 0.0f;
for (int64_t d = 0; d < K; ++d) acc += kc[j * K + d] * St[d * V + e];
Wm[j * V + e] = bc[j] * (vc[j * V + e] - ge * acc);
}
/* V' = A W (lane e, sum over j <= i in order) */
for (int64_t i = 0; i < L; ++i) {
const int64_t ti = bos + c0 + i;
float vp = 0.0f;
for (int64_t j = 0; j <= i; ++j) vp += As.ld(at3(Av, ti, h, j)) * Wm[j * V + e];
Vp[i * V + e] = vp;
}
/* the sA rows, lane j: R[i][j] = scale e^{g_i - g_j} sum_d q_i k_j */
if (e < L)
for (int64_t i = e; i < L; ++i) {
float rj = 0.0f;
for (int64_t d = 0; d < K; ++d) rj += qc[i * K + d] * kc[e * K + d];
R[i * chunk + e] = scale * rexp(gc[i] - gc[e]) * rj;
}
sycl::group_barrier(gw);
/* O = scale . e^{g_i} . (Q S^T) + sA V' */
for (int64_t i = 0; i < L; ++i) {
const int64_t ti = bos + c0 + i;
const float sg = scale * rexp(gc[i]);
float inr = 0.0f;
for (int64_t d = 0; d < K; ++d) inr += qc[i * K + d] * St[d * V + e];
float acc = sg * inr;
for (int64_t j = 0; j <= i; ++j) acc += R[i * chunk + j] * Vp[j * V + e];
os.st(at3(ov, ti, h, e), acc);
}
sycl::group_barrier(gw);
/* S = e^{g_last} S + V'^T (e^{g_last - g_j} . K) */
for (int64_t j = 0; j < L; ++j) {
const float gsc = rexp(glast - gc[j]);
for (int64_t d = e; d < K; d += V) kc[j * K + d] = gsc * kc[j * K + d];
}
sycl::group_barrier(gw);
const float el = rexp(glast);
for (int64_t d = 0; d < K; ++d) {
float sr = el * St[d * V + e];
for (int64_t j = 0; j < L; ++j) sr += Vp[j * V + e] * kc[j * K + d];
St[d * V + e] = sr;
}
}
if (has_ht)
for (int64_t d = 0; d < K; ++d) hts.st(at4(htv, sn, h, e, d), St[d * V + e]);
});
});
});
}
const int64_t need = K * V + 3 * chunk * V + 3 * chunk * K + chunk * chunk + 3 * chunk + 2 * V;
float* ws = workspace(*qu, (size_t)(N * H * need));
if (!ws) return RAD_E_DEVICE;
@@ -725,7 +878,7 @@ inline void recurrent(const RecIO& r, int64_t n, int64_t h, float* wsp) {
/* Builds RecIO from libref's operand list (q, k, v, a, b, A_log, dt_bias, state, state_idx,
* num_accepted, cu, z, norm_w, o[, o_q, o_scale]). */
int rec_io(const RadArgs* a, const RadTensor* t, int nt, RecIO* r, int64_t* n_seq) {
int rec_io(const RadArgs* a, const RadTensor* t, int nt, RecIO* r, int64_t* n_seq, int64_t* tk_out) {
if (nt < 14) return RAD_E_INVAL;
const RadTensor *q = &t[0], *k = &t[1], *v = &t[2], *av = &t[3], *bvt = &t[4];
const RadTensor *state = &t[7], *o = &t[13];
@@ -754,6 +907,7 @@ int rec_io(const RadArgs* a, const RadTensor* t, int nt, RecIO* r, int64_t* n_se
if (Hv <= 0) return RAD_E_SHAPE;
const int64_t Tk = rad_tensor_numel(v) / (Hv * V);
if (Tk <= 0) return RAD_E_SHAPE;
*tk_out = Tk;
const int64_t Hg = rad_tensor_numel(q) / (Tk * K);
if (Hg <= 0 || Hv % Hg != 0) return RAD_E_SHAPE;
r->Hv = Hv;
@@ -795,7 +949,229 @@ int rec_io(const RadArgs* a, const RadTensor* t, int nt, RecIO* r, int64_t* n_se
return RAD_OK;
}
int launch_recurrent(sycl::queue& qu, const RecIO& r, int64_t n_seq) {
/* The same step spread over value columns. Kernel A: a 16-lane work-group a (sequence, v head,
* block of 16 columns e), its 128 x 16 slice of the state in local memory (read and written a
* state row at a time with the lanes along d, coalesced): every chain libref forms runs over d, so
* each column is libref's arithmetic, and the un-normalised output column goes to a scratch row.
* Kernel B: a work-group a (token, v head) for the gated RMS norm -- its sum over e serial in e
* order on one work-item, as libref's -- and the folded fp8 group. 384 work-groups a 27B layer
* instead of 48. */
constexpr int64_t kCb = 16; /* columns a work-group of kernel A */
/* Whether sequence n is stepped at all -- the early returns of libref's loop, which leave its o
* rows untouched. */
inline bool seq_active(const RecIO& r, int64_t n) {
const int64_t q_len = r.cu.at(n + 1) - r.cu.at(n);
if (q_len <= 0 && !r.anchor) return false;
const int64_t na = r.nacc.on ? r.nacc.at(n) : 1;
if (r.anchor) {
if (q_len > r.al.tmax || na > r.al.tmax) return false;
for (int c = 0; c < 6; ++c)
if (r.sidx.li(at2(r.si, n, c)) < 0) return false;
return true;
}
const int64_t rd = na > 0 ? na - 1 : 0;
const int64_t slot_in = r.has_si ? r.sidx.li(at2(r.si, n, rd < r.sw ? rd : r.sw - 1)) : n;
return slot_in >= 0;
}
constexpr int64_t kWgMaxV = 256;
constexpr int64_t kWgMaxK = 256;
int launch_recurrent_wg(sycl::queue& qu, const RecIO& r, int64_t n_seq, int64_t Tk) {
const int64_t K = r.K, V = r.V, Hv = r.Hv;
const int64_t nblk = V / kCb;
float* orow = workspace(qu, (size_t)(Tk * Hv * V + 64), 3);
if (!orow) return RAD_E_DEVICE;
/* Under `anchor` the replay is its own launch (phase 0): every block reads the stamp and the
* previous step's factors before any block of the head overwrites them in phase 1. */
int rc = RAD_OK;
for (int phase = r.anchor ? 0 : 1; phase <= 1 && rc >= 0; ++phase)
rc = submit([&] {
qu.submit([&](sycl::handler& hd) {
sycl::local_accessor<float, 1> St(sycl::range<1>((size_t)(K * kCb)), hd);
sycl::local_accessor<float, 1> vec(sycl::range<1>((size_t)(4 * K)), hd);
hd.parallel_for(sycl::nd_range<1>{(size_t)(n_seq * Hv * nblk) * kCb, (size_t)kCb},
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
const int64_t gid = (int64_t)it.get_group(0);
const int64_t blk = gid % nblk, nh = gid / nblk;
const int64_t n = nh / Hv, h = nh % Hv, hk = h / r.grp;
const int64_t l = (int64_t)it.get_local_id(0);
const int64_t e0 = blk * kCb, e = e0 + l;
auto grp = it.get_group();
float* qn = &vec[0];
float* kn = qn + K;
float* qraw = kn + K;
float* kraw = qraw + K;
const int64_t bos = r.cu.at(n), q_len = r.cu.at(n + 1) - bos;
if (q_len <= 0 && !r.anchor) return; /* uniform */
const int64_t na = r.nacc.on ? r.nacc.at(n) : 1;
const int64_t rd = na > 0 ? na - 1 : 0;
const Anchor& al = r.al;
int64_t col[6] = { 0, 0, 0, 0, 0, 0 };
int64_t slot_in = 0;
if (r.anchor) {
if (q_len > al.tmax || na > al.tmax) return;
for (int c = 0; c < 6; ++c) col[c] = r.sidx.li(at2(r.si, n, c));
bool pad = false;
for (int c = 0; c < 6; ++c) pad = pad || col[c] < 0;
if (pad) return;
if (phase == 0) {
const int64_t fb0 = at4(r.stv, col[4], h, 0, 0);
const int m = (int)(r.state.ld(fb0 + al.mark) + 0.5f) - 1;
slot_in = col[3 - ((m >= 0 && m <= 2) ? m : 1)];
} else {
slot_in = col[2]; /* the anchor phase 0 published */
}
} else {
slot_in = r.has_si ? r.sidx.li(at2(r.si, n, rd < r.sw ? rd : r.sw - 1)) : n;
if (slot_in < 0) return;
}
const int64_t fb = r.anchor ? at4(r.stv, col[4], h, 0, 0) : 0;
const int64_t fbs[3] = { r.anchor ? at4(r.stv, col[3], h, 0, 0) : 0, fb,
r.anchor ? at4(r.stv, col[5], h, 0, 0) : 0 };
const bool dup_f = r.anchor && (col[3] != col[4] || col[5] != col[4]);
auto load_state = [&](int64_t slot) {
for (int64_t ee = 0; ee < kCb; ++ee)
for (int64_t d = l; d < K; d += kCb) St[d * kCb + ee] = r.state.ld(at4(r.stv, slot, h, e0 + ee, d));
};
auto store_state = [&](int64_t slot) {
for (int64_t ee = 0; ee < kCb; ++ee)
for (int64_t d = l; d < K; d += kCb) r.state.st(at4(r.stv, slot, h, e0 + ee, d), St[d * kCb + ee]);
};
load_state(slot_in);
sycl::group_barrier(grp);
const float alog = r.alog.p ? r.alog.ld(h * r.alog_s) : 0.0f;
const float dtb = r.dtb.p ? r.dtb.ld(h * r.dtb_s) : 0.0f;
auto update_col = [&](float g, float bt, float ve) {
const float dec = rexp(g);
float sk2 = 0.0f;
for (int64_t d = 0; d < K; ++d) {
St[d * kCb + l] *= dec;
sk2 += St[d * kCb + l] * kn[d];
}
const float uu = bt * (ve - sk2);
for (int64_t d = 0; d < K; ++d) St[d * kCb + l] += uu * kn[d];
};
if (r.anchor && phase == 0) {
for (int64_t t = 0; t < na; ++t) {
const int64_t f = fb + t * al.fstr;
for (int64_t gi = 0; gi < al.groups; ++gi) {
sycl::group_barrier(grp);
for (int64_t d = l; d < K; d += kCb) kn[d] = r.state.ld(f + gi * K + d);
sycl::group_barrier(grp);
if (e >= 32 * gi && e < sycl::min(V, 32 * gi + 32))
update_col(r.state.ld(f + al.gb + 2 * gi), r.state.ld(f + al.gb + 2 * gi + 1),
r.state.ld(f + al.vb + e));
}
}
sycl::group_barrier(grp);
store_state(col[2]);
return;
}
if (r.anchor && blk == 0 && l == 0)
for (int c = 0; c < 3; ++c) r.state.st(fbs[c] + al.mark, (float)(c + 1));
for (int64_t t = 0; t < q_len; ++t) {
const int64_t tok = bos + t;
const float gv = -rexp(alog) * softplus(r.av.ld(at2(r.a2, tok, h)) + dtb, r.spthr);
const float bt = sigm(r.bv.ld(at2(r.b2, tok, h)));
sycl::group_barrier(grp);
for (int64_t d = l; d < K; d += kCb) {
qraw[d] = r.q.ld(at3(r.qv, tok, hk, d));
kraw[d] = r.k.ld(at3(r.kv, tok, hk, d));
}
sycl::group_barrier(grp);
float sq = 0.0f, sk = 0.0f; /* every work-item, the same chain */
for (int64_t d = 0; d < K; ++d) {
sq += qraw[d] * qraw[d];
sk += kraw[d] * kraw[d];
}
const float qs = 1.0f / sycl::sqrt(sq + r.l2eps) * r.scale;
const float ks = 1.0f / sycl::sqrt(sk + r.l2eps);
for (int64_t d = l; d < K; d += kCb) {
qn[d] = qraw[d] * qs;
kn[d] = kraw[d] * ks;
}
sycl::group_barrier(grp);
update_col(gv, bt, r.v.ld(at3(r.vv, tok, h, e)));
float oe = 0.0f;
for (int64_t d = 0; d < K; ++d) oe += St[d * kCb + l] * qn[d];
orow[(tok * Hv + h) * V + e] = oe;
if (r.anchor)
for (int c = 0; c < 3; ++c) {
if (c != 1 && !dup_f) continue;
const int64_t f = fbs[c] + t * al.fstr;
if (blk == 0)
for (int64_t gi = 0; gi < al.groups; ++gi) {
for (int64_t d = l; d < K; d += kCb) r.state.st(f + gi * K + d, kn[d]);
if (l == 0) {
r.state.st(f + al.gb + 2 * gi, gv);
r.state.st(f + al.gb + 2 * gi + 1, bt);
}
}
r.state.st(f + al.vb + e, r.v.ld(at3(r.vv, tok, h, e)));
}
if (r.anchor) continue;
const int64_t so = r.has_si ? r.sidx.li(at2(r.si, n, t < r.sw ? t : r.sw - 1)) : n;
if (so >= 0) { /* uniform */
sycl::group_barrier(grp);
store_state(so);
}
}
});
});
});
if (rc < 0) return rc;
/* kernel B: the gated RMS norm and the fp8 group, a work-group a (token, head) */
return submit([&] {
qu.submit([&](sycl::handler& hd) {
sycl::local_accessor<float, 1> ob(sycl::range<1>((size_t)V), hd);
hd.parallel_for(sycl::nd_range<1>{(size_t)(Tk * Hv) * (size_t)V, (size_t)V}, [=](sycl::nd_item<1> it) {
const int64_t th = (int64_t)it.get_group(0);
const int64_t tok = th / Hv, h = th % Hv;
const int64_t e = (int64_t)it.get_local_id(0);
auto grp = it.get_group();
/* the token's sequence, and whether kernel A stepped it */
int64_t n = 0;
while (n + 1 < n_seq && r.cu.at(n + 1) <= tok) ++n;
if (!seq_active(r, n)) return; /* uniform: libref leaves these rows as they are */
const float oe = orow[th * V + e];
ob[e] = oe;
sycl::group_barrier(grp);
float sc = 0.0f;
if (e == 0) {
float sq2 = 0.0f;
for (int64_t i = 0; i < V; ++i) sq2 += ob[i] * ob[i];
sc = 1.0f / sycl::sqrt(sq2 / (float)V + r.neps);
}
sc = sycl::group_broadcast(grp, sc, 0);
float x = oe * sc;
if (r.has_nw) x *= r.nw.ld(e * r.nw_s);
if (r.has_z) {
const float zz = r.z.ld(at3(r.zv, tok, h, e));
x *= (r.nact == 1 ? sigm(zz) : silu(zz));
}
r.o.st(at3(r.ov, tok, h, e), x);
if (r.has_q8) {
const float stored = r.o.ld(at3(r.ov, tok, h, e));
const float amax = sycl::reduce_over_group(grp, sycl::fabs(stored), sycl::maximum<float>());
const float qsc = amax > 0.0f ? amax * (1.0f / 448.0f) : 1.0f;
const float inv = 1.0f / qsc;
float c = stored * inv;
if (c > 448.0f) c = 448.0f;
if (c < -448.0f) c = -448.0f;
r.oq.st(tok * r.oq_rs + h * V * r.oq_cs + e * r.oq_cs, c);
if (e == 0) r.osc.st(tok * r.osc_rs + h * r.osc_cs, qsc);
}
});
});
});
}
int launch_recurrent(sycl::queue& qu, const RecIO& r, int64_t n_seq, int64_t Tk) {
/* Kernel B covers every token row: a row no sequence owns this step (cu stops short of T) is
* normed from a stale scratch row, so the split form is taken only when cu covers all of T,
* which the engine's decode always does; otherwise the serial form below. */
if (r.V % kCb == 0 && r.V <= kWgMaxV && r.K <= kWgMaxK && Tk > 0) return launch_recurrent_wg(qu, r, n_seq, Tk);
const int64_t need = r.V * r.K + 2 * r.K + 3 * r.V;
const int64_t work = n_seq * r.Hv;
float* ws = workspace(qu, (size_t)(work * need));
@@ -816,11 +1192,11 @@ int launch_recurrent(sycl::queue& qu, const RecIO& r, int64_t n_seq) {
int gdn_recurrent_update(const RadArgs* a, RadStream st) {
if (!a) return RAD_E_INVAL;
RecIO r{};
int64_t n_seq = 0;
if (int rc = rec_io(a, a->t, a->n_t, &r, &n_seq); rc < 0) return rc;
int64_t n_seq = 0, Tk = 0;
if (int rc = rec_io(a, a->t, a->n_t, &r, &n_seq, &Tk); rc < 0) return rc;
sycl::queue* qu = queue_of(st);
if (!qu) return RAD_E_DEVICE;
return launch_recurrent(*qu, r, n_seq);
return launch_recurrent(*qu, r, n_seq, Tk);
}
/* ------------------------------------------------------------------ gdn_gated_rmsnorm */
@@ -899,11 +1275,12 @@ int gdn_conv_recurrent_update(const RadArgs* a, RadStream st) {
if (!qu) return RAD_E_DEVICE;
const int64_t esz = (rad_dtype_bits(x->dtype) + 7) / 8;
const int64_t nq = T * Hg * K, nv = T * Hv * V;
/* The staging rides the workspace's tail-free head: q, k, v back to back, 16-byte aligned. */
/* The staging: q, k, v back to back, 16-byte aligned, in workspace slot 1 (the recurrence may
* take slot 0). */
auto up16 = [](int64_t b) { return (b + 15) / 16 * 16; };
const int64_t bq = up16(nq * esz), bv = up16(nv * esz);
char* stage = (char*)sycl::malloc_device(size_t(2 * bq + bv), *qu);
if (!stage) return refuse("gdn_conv_recurrent_update: no staging memory");
char* stage = (char*)workspace(*qu, (size_t)((2 * bq + bv) / 4 + 4), 1);
if (!stage) return RAD_E_DEVICE;
auto mk = [&](char* d, int64_t h, int64_t w) {
RadTensor t{};
t.data = d; t.dtype = x->dtype; t.rank = 3;
@@ -931,17 +1308,10 @@ int gdn_conv_recurrent_update(const RadArgs* a, RadStream st) {
a->t[11], a->t[12], a->t[13], a->t[14], a->t[15],
a->n_t > 16 ? a->t[16] : RadTensor{}, a->n_t > 17 ? a->t[17] : RadTensor{} };
RecIO r{};
int64_t n_seq = 0;
rc = rec_io(a, rt, 16, &r, &n_seq);
if (rc == RAD_OK) rc = launch_recurrent(*qu, r, n_seq);
int64_t n_seq = 0, Tk = 0;
rc = rec_io(a, rt, 16, &r, &n_seq, &Tk);
if (rc == RAD_OK) rc = launch_recurrent(*qu, r, n_seq, Tk);
}
/* The staging is freed once the queue has passed the two launches. */
try {
qu->wait();
} catch (const sycl::exception& e) {
rc = refuse("SYCL: %s", e.what());
}
sycl::free(stage, *qu);
return rc;
}
+132
View File
@@ -174,6 +174,138 @@ int logits_gemm_head(const RadArgs* a, RadStream s) {
return gemm_plain(a, s, "n_vocab", "n_embd", "logits_gemm", false, true);
}
/* The joined fp8 head at M <= 16 (decode, verify): a sub-group a vocab column, each lane 16 E4M3
* codes a step (one 16-byte load) inside one scale block, summed against the f32 activation and
* scaled once: libref's products code * scale * a regrouped, held to the gemm tolerance and the
* KL bound like the other fast GEMVs. The activation is widened to f32 once into the workspace. */
inline float e4m3f(uint8_t b) {
const uint32_t sgn = (uint32_t)(b & 0x80) << 24;
const uint32_t e = (b >> 3) & 0xF, m = b & 7;
if ((b & 0x7F) == 0x7F) return sycl::bit_cast<float>(sgn | 0x7FC00000u);
if (e == 0) {
const float v = (float)m * (1.0f / 512.0f);
return sgn ? -v : v;
}
return sycl::bit_cast<float>(sgn | ((e + 120) << 23) | (m << 20));
}
/* E4M3 codes as fp16 by bits: ((b & 0x7F) << 7) | ((b & 0x80) << 8) is exactly E4M3(b) * 2^-8
* (its subnormals land on fp16 subnormals), so bytes (0, 2) and (1, 3) of a word are each a half
* pair after two masks and shifts. The activation is laid out in that order, as
* fp16(x / R * 2^8), R = max|x| / 2 of the row (|a16| <= 512); a lane's sixteen products sum in
* half2, scaled once by the block's scale in f32. An i8 head, or a scale block narrower than 16
* columns, takes the f32 form. */
template <int MM>
int head_gemv(sycl::queue& q, const sycl::half* a16, const float* rr, const char* bb, char* yb,
uint32_t ydt, int64_t N, int64_t K, int64_t yrs, int64_t ycs, int mm) {
constexpr size_t wg = 256;
const size_t groups = (size_t)((N + 15) / 16);
return submit([&] {
syclex::nd_launch(q, sycl::nd_range<1>{groups * wg, wg},
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
const sycl::sub_group sg = it.get_sub_group();
const int64_t n = (int64_t)it.get_group(0) * 16 + (int64_t)sg.get_group_linear_id();
if (n >= N) return;
const HPlane cp = hplane(bb, "codes"), sp = hplane(bb, "scale");
if (!cp.p || !sp.p) return; /* the layout hook guarantees a joined head */
const int lane = (int)sg.get_local_linear_id();
if (cp.dt != RAD_F8E4M3 || !(sp.bc == 0 || sp.bc % 16 == 0)) {
/* an i8 head or a narrow scale block: element by element in f32 (a16 carries
* 2^8 / R, its permutation undone per element) */
float accg[MM];
for (int r = 0; r < MM; ++r) accg[r] = 0.0f;
const char* crow = cp.p + n * cp.row_bytes;
for (int64_t k = lane; k < K; k += kSub) {
const float w = rad_load_f32(crow, cp.dt, k) * sp.cover(n, k);
const int64_t pos = (k & ~(int64_t)3) + ((k & 3) == 1 ? 2 : (k & 3) == 2 ? 1 : (k & 3));
for (int r = 0; r < MM; ++r) {
if (r >= mm) break;
accg[r] += (float)a16[r * K + pos] * w;
}
}
for (int r = 0; r < MM; ++r) {
if (r >= mm) break;
const float v = sycl::reduce_over_group(sg, accg[r], sycl::plus<float>());
if (lane == 0) rad_store_f32(yb, ydt, r * yrs + n * ycs, v * rr[r] * (1.0f / 256.0f));
}
return;
}
const uint8_t* crow = (const uint8_t*)cp.p + n * cp.row_bytes;
using H2 = sycl::vec<sycl::half, 2>;
float acc[MM];
for (int r = 0; r < MM; ++r) acc[r] = 0.0f;
for (int64_t k = (int64_t)lane * 16; k < K; k += 16 * kSub) {
const sycl::vec<uint32_t, 4> c = *(const sycl::vec<uint32_t, 4>*)(crow + k);
H2 w2[8];
for (int j = 0; j < 4; ++j) {
const uint32_t x = c[j] & 0x00FF00FFu, y = (c[j] >> 8) & 0x00FF00FFu;
w2[2 * j] = sycl::bit_cast<H2>(((x & 0x007F007Fu) << 7) | ((x & 0x00800080u) << 8));
w2[2 * j + 1] = sycl::bit_cast<H2>(((y & 0x007F007Fu) << 7) | ((y & 0x00800080u) << 8));
}
const float sc = sp.cover(n, k);
for (int r = 0; r < MM; ++r) {
if (r >= mm) break;
const sycl::vec<uint32_t, 8> aw = *(const sycl::vec<uint32_t, 8>*)(a16 + r * K + k);
H2 p2(0.0f, 0.0f);
for (int u = 0; u < 8; ++u) p2 = sycl::fma(sycl::bit_cast<H2>(aw[u]), w2[u], p2);
acc[r] += ((float)p2[0] + (float)p2[1]) * sc;
}
}
for (int r = 0; r < MM; ++r) {
if (r >= mm) break;
const float v = sycl::reduce_over_group(sg, acc[r], sycl::plus<float>());
if (lane == 0) rad_store_f32(yb, ydt, r * yrs + n * ycs, v * rr[r]);
}
});
});
}
int logits_gemm_head_gemv(const RadArgs* a, RadStream st) {
if (exact_only()) return logits_gemm_head(a, st);
if (!a || a->n_t < 3 || !a->t[0].data || !a->t[1].data || !a->t[2].data) return RAD_E_INVAL;
const RadTensor *A = &a->t[0], *B = &a->t[1], *Y = &a->t[2];
const int64_t K = rad_args_geti_or(a, "n_embd", 0), N = rad_args_geti_or(a, "n_vocab", 0);
if (K <= 0 || N <= 0) return RAD_E_SHAPE;
const int64_t M = rad_tensor_numel(Y) / N;
if (M < 1 || M > 16 || K % 128 || !is_float(A->dtype) || !rad_ptr_aligned(B->data, 16))
return logits_gemm_head(a, st);
const int64_t ars = row_stride(A, K), acs = A->rank >= 2 ? A->stride[A->rank - 1] : 1;
const int64_t yrs = row_stride(Y, N), ycs = Y->rank >= 2 ? Y->stride[Y->rank - 1] : 1;
sycl::queue* q = queue_of(st);
if (!q) return RAD_E_DEVICE;
const int64_t abytes = (M * K * 2 + 255) / 256 * 256;
float* ws = workspace(*q, (size_t)(abytes / 4 + M + 64));
if (!ws) return RAD_E_DEVICE;
sycl::half* a16 = (sycl::half*)ws;
float* rr = (float*)((char*)ws + abytes);
const char* ab = (const char*)A->data;
const uint32_t adt = A->dtype;
int rc = submit([&] {
q->parallel_for(sycl::nd_range<1>{(size_t)M * 256, 256}, [=](sycl::nd_item<1> it) {
const int64_t m = (int64_t)it.get_group(0);
const int64_t t = (int64_t)it.get_local_id(0);
float mx = 0.0f;
for (int64_t k = t; k < K; k += 256) mx = sycl::fmax(mx, sycl::fabs(rad_load_f32(ab, adt, m * ars + k * acs)));
mx = sycl::reduce_over_group(it.get_group(), mx, sycl::maximum<float>());
const float R = mx > 0.0f ? mx * 0.5f : 1.0f;
if (t == 0) rr[m] = R;
const float f = 256.0f / R;
/* position p of each 4 holds element (p % 2) * 2 + (p / 2): bytes (0, 2), (1, 3) */
for (int64_t k = t; k < K; k += 256) {
const int64_t p = k & 3, src = (k & ~(int64_t)3) + (p & 1) * 2 + (p >> 1);
a16[m * K + k] = (sycl::half)(rad_load_f32(ab, adt, m * ars + src * acs) * f);
}
});
});
if (rc < 0) return rc;
const char* bb = (const char*)B->data;
char* yb = (char*)Y->data;
const int mm = (int)M;
if (mm == 1) return head_gemv<1>(*q, a16, rr, bb, yb, Y->dtype, N, K, yrs, ycs, mm);
if (mm <= 8) return head_gemv<8>(*q, a16, rr, bb, yb, Y->dtype, N, K, yrs, ycs, mm);
return head_gemv<16>(*q, a16, rr, bb, yb, Y->dtype, N, K, yrs, ycs, mm);
}
/* libref's ref_layout_head: operand 1 (the head) of codes and one scale, joined. */
int head_layout(const RadParam*, int, int operand, const RadEncoding* enc, const int* sel,
const RadTensor* planes, int n, RadLayout* out) {
+301
View File
@@ -0,0 +1,301 @@
/* xe2_gemv_m2.cpp -- the fast gemm_nt_q at m2xfp4 for M <= 16: decode and the speculative verify,
* bandwidth-bound by construction (a 27B step reads ~14 GB of these weights).
*
* One 16-lane sub-group an output column. Lane l walks the 32-column groups l, l + 16, ...: per
* group one 16-byte load of codes (32 e2m1 nibbles), the group's E8M0 exponent and meta byte, and
* 32 bytes of each activation row's E4M3 codes. A group's four 8-column subgroups are summed as
* sum(a * e2m1(code)) and scaled once by a_scale[m][k/128] * 2^(e-127) * (1 + k_s/4); the lane's
* groups add up in order and the sub-group reduces them as a tree. So the result is libref's
* product (each weight is e2m1 * 2^e * (1 + k/4), exact in f32) summed in another order: checked
* against libref at rad-kbench's gemm tolerance and the KL bound (docs/SYCL_PROGRESS.md D13), as
* the bf16 GEMV is. XE2_EXACT=1 sends every launch to the correctness-first kernel.
*
* Parts (pb1/pb2): a column reads the activation copy its part names, as xe2_gemm_m2.cpp does.
* Takes K a multiple of 32, a dense activation row and codes rows of K/2 bytes 16-byte aligned;
* anything else falls back to the exact kernel.
*/
#include "xe2_common.h"
#include "xe2_ops.h"
#include <algorithm>
namespace xe2 {
namespace {
using half = sycl::half;
constexpr int kMaxM = 16;
constexpr int kColsM1 = 4; /* output columns a sub-group at M = 1 */
constexpr size_t kWg = 256; /* 16 sub-groups */
int64_t row_stride(const RadTensor* t, int64_t cols) {
if (!t || t->rank < 2) return cols;
int64_t span = 1;
for (int i = (int)t->rank - 1; i >= 0; --i) {
span *= t->shape[i];
if (span == cols) return (i > 0) ? t->stride[i - 1] : cols;
if (span > cols) break;
}
return t->stride[t->rank - 2];
}
/* E4M3 to f32: sign, 4 exponent bits (bias 7), 3 mantissa bits; 0x7F / 0xFF are NaN. */
inline float e4m3(uint8_t b) {
const uint32_t s = (uint32_t)(b & 0x80) << 24;
const uint32_t e = (b >> 3) & 0xF, m = b & 7;
if ((b & 0x7F) == 0x7F) return sycl::bit_cast<float>(s | 0x7FC00000u);
if (e == 0) {
const float v = (float)m * (1.0f / 512.0f); /* 2^-6 * m / 8 */
return s ? -v : v;
}
return sycl::bit_cast<float>(s | ((e + 120) << 23) | (m << 20));
}
/* The weights as fp16 by bits alone: for a code (s, m2 m1 m0) the half with magnitude bits
* m2 m1 m0 << 9 and sign bit 15 is exactly e2m1(code) * 2^-14 -- 0.5 * 2^-14 is the subnormal
* 0x0200 -- so a pair of codes is two shifts, two masks and an or (the card's fp16 keeps
* subnormals; m2bench in scratch measured it). Pairs are the codes (u, u + 4) of a 32-bit word,
* so the activation is laid out with K permuted alike within each 8. The activation carries the
* 2^14: a16 = fp16(a * a_scale / R * 2^14), R = 224 * the row's largest scale, so |a16| <= 2^15.
* A subgroup's four pairs accumulate in half2, times (1 + k/4) into the group's half2, which is
* widened and scaled by 2^(e-127) into the f32 sum: fp16 sums of at most 32 products. */
template <int MM, int NC>
int launch(sycl::queue& q, const half* a16, const float* rr, const uint8_t* codes0,
const uint8_t* meta0, const uint8_t* expe0, char* yb, uint32_t ydt, int64_t N, int64_t K,
int64_t acols, int64_t brs, int64_t yrs, int64_t ycs, int mm, int64_t e1, int64_t e2) {
const int64_t per_wg = 16 * NC;
const size_t groups = (size_t)((N + per_wg - 1) / per_wg);
const int64_t kg = K / 32;
return submit([&] {
syclex::nd_launch(q, sycl::nd_range<1>{groups * kWg, kWg},
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
const sycl::sub_group sg = it.get_sub_group();
const int64_t n0 = ((int64_t)it.get_group(0) * 16 + (int64_t)sg.get_group_linear_id()) * NC;
if (n0 >= N) return; /* uniform across the sub-group */
const int lane = (int)sg.get_local_linear_id();
/* The columns of one sub-group share a part: the part bounds are multiples of 16 and
* NC divides 16, so n0 .. n0 + NC - 1 never straddle one. */
const int nc = (int)sycl::min((int64_t)NC, N - n0);
const int64_t part = n0 < e1 ? 0 : (n0 < e2 ? 1 : 2);
const half* arow0 = a16 + part * K;
using H2 = sycl::vec<half, 2>;
float acc[NC][MM];
for (int c = 0; c < NC; ++c)
for (int r = 0; r < MM; ++r) acc[c][r] = 0.0f;
for (int64_t g = lane; g < kg; g += kSub) {
for (int c = 0; c < NC; ++c) {
const int64_t n = n0 + (c < nc ? c : 0);
const sycl::vec<uint32_t, 4> cw = *(const sycl::vec<uint32_t, 4>*)(codes0 + n * (K / 2) + g * 16);
const float ev = sycl::ldexp(1.0f, (int)expe0[n * brs + g] - 127);
const uint32_t mt = meta0[n * kg + g];
H2 w2[16];
H2 kmh[4];
for (int j = 0; j < 4; ++j) {
const uint32_t word = cw[j];
for (int u = 0; u < 4; ++u) {
const uint32_t mb = u < 3 ? (word << (9 - 4 * u)) & 0x0E000E00u : (word >> 3) & 0x0E000E00u;
const uint32_t sb = (word << (12 - 4 * u)) & 0x80008000u;
w2[j * 4 + u] = sycl::bit_cast<H2>(mb | sb);
}
const half k1 = (half)(1.0f + 0.25f * (float)((mt >> (2 * j)) & 3));
kmh[j] = H2(k1, k1);
}
for (int r = 0; r < MM; ++r) {
if (r >= mm) break;
const sycl::vec<uint32_t, 16> aw = *(const sycl::vec<uint32_t, 16>*)(arow0 + r * acols + g * 32);
H2 pg(0.0f, 0.0f);
for (int j = 0; j < 4; ++j) {
H2 p2(0.0f, 0.0f);
for (int u = 0; u < 4; ++u) p2 = sycl::fma(sycl::bit_cast<H2>(aw[j * 4 + u]), w2[j * 4 + u], p2);
pg = sycl::fma(p2, kmh[j], pg);
}
acc[c][r] += ((float)pg[0] + (float)pg[1]) * ev;
}
}
}
for (int c = 0; c < NC; ++c) {
if (c >= nc) break;
for (int r = 0; r < MM; ++r) {
if (r >= mm) break;
const float v = sycl::reduce_over_group(sg, acc[c][r], sycl::plus<float>());
if (lane == 0) rad_store_f32(yb, ydt, r * yrs + (n0 + c) * ycs, v * rr[r]);
}
}
});
});
}
} // namespace
int gemm_nt_q_m2_gemv(const RadArgs* a, RadStream st) {
if (exact_only()) return gemm_nt_q_m2(a, st);
if (!a || a->n_t < 5) return RAD_E_INVAL;
const RadTensor *A = &a->t[0], *B = &a->t[2], *Y = &a->t[4];
const RadTensor *AS = rad_arg_in(a, 1), *BS = rad_arg_in(a, 3);
if (!A->data || !B->data || !Y->data || !AS || !BS || !AS->data || !BS->data) return RAD_E_INVAL;
const int64_t K = rad_args_geti_or(a, "K", 0), N = rad_args_geti_or(a, "N", 0);
if (K <= 0 || K % 32 || N <= 0) return gemm_nt_q_m2(a, st);
const int64_t M = rad_tensor_numel(Y) / N;
if (M < 1 || M > kMaxM || rad_arg_in(a, 5)) return gemm_nt_q_m2(a, st);
const int64_t pb1 = rad_args_geti_or(a, "pb1", 0), pb2 = rad_args_geti_or(a, "pb2", 0);
const int64_t e1 = pb1 > 0 ? std::min<int64_t>(pb1, N) : N;
const int64_t e2 = pb2 > 0 ? std::max<int64_t>(e1, std::min<int64_t>(pb2, N)) : N;
const int64_t parts = e2 < N ? 3 : (e1 < N ? 2 : 1);
const int64_t kb = K / 32, akb = (K + 127) / 128;
const int64_t acols = parts * K, scols = parts * akb;
const bool blob = (B->rank == 1 && rad_tensor_numel(B) == N * (K / 2 + kb)) ||
(B->rank == 2 && B->shape[0] == N && B->shape[1] == K);
const int64_t ars = row_stride(A, acols), srs = row_stride(AS, scols), brs = row_stride(BS, kb);
const int64_t yrs = row_stride(Y, N), ycs = Y->rank >= 2 ? Y->stride[Y->rank - 1] : 1;
const bool ok = blob && A->dtype == RAD_F8E4M3 && B->dtype == RAD_U8 && BS->dtype == RAD_E8M0 &&
(A->rank < 2 || A->stride[A->rank - 1] == 1) &&
(AS->rank < 2 || AS->stride[AS->rank - 1] == 1) && ars % 16 == 0 &&
rad_ptr_aligned(A->data, 16) && rad_ptr_aligned(B->data, 16) &&
(K / 2) % 16 == 0 && rad_tensor_numel(A) >= M * acols &&
rad_tensor_numel(AS) >= M * scols && rad_tensor_numel(BS) >= N * kb &&
(!(pb1 > 0 || pb2 > 0) || ((e1 % 16) == 0 && (e2 % 16) == 0 && !(e1 == e2 && e2 < N)));
if (!ok) return gemm_nt_q_m2(a, st);
sycl::queue* q = queue_of(st);
if (!q) return RAD_E_DEVICE;
const uint8_t* codes0 = (const uint8_t*)B->data;
const uint8_t* meta0 = codes0 + N * (K / 2);
const int mm = (int)M;
/* The activation once as fp16 in the weight pairs' order, with its row factor R. */
const int64_t abytes = (M * acols * 2 + 255) / 256 * 256;
float* ws = workspace(*q, (size_t)(abytes / 4 + M + 64));
if (!ws) return RAD_E_DEVICE;
half* a16 = (half*)ws;
float* rr = (float*)((char*)ws + abytes);
{
const uint8_t* ab = (const uint8_t*)A->data;
const char* sab = (const char*)AS->data;
const uint32_t sadt = AS->dtype;
const int rc = submit([&] {
q->parallel_for(sycl::nd_range<1>{(size_t)M * 256, 256}, [=](sycl::nd_item<1> it) {
const int64_t m = (int64_t)it.get_group(0);
const int64_t t = (int64_t)it.get_local_id(0);
float mx = 0.0f;
for (int64_t c = t; c < scols; c += 256) mx = sycl::fmax(mx, rad_load_f32(sab, sadt, m * srs + c));
mx = sycl::reduce_over_group(it.get_group(), mx, sycl::maximum<float>());
const float R = mx > 0.0f ? 224.0f * mx : 1.0f;
if (t == 0) rr[m] = R;
const float f = 16384.0f / R;
for (int64_t c = t; c < acols; c += 256) {
const int64_t p = c & 7, src = (c & ~(int64_t)7) + (p >> 1) + 4 * (p & 1);
a16[m * acols + c] = (half)(e4m3(ab[m * ars + src]) * rad_load_f32(sab, sadt, m * srs + src / 128) * f);
}
});
});
if (rc < 0) return rc;
}
auto go = [&](auto tag, auto cols) {
constexpr int MM = decltype(tag)::value, NC = decltype(cols)::value;
return launch<MM, NC>(*q, a16, rr, codes0, meta0, (const uint8_t*)BS->data, (char*)Y->data, Y->dtype,
N, K, acols, brs, yrs, ycs, mm, e1, e2);
};
/* Columns a sub-group: four at decode (the activation read once for four weight rows), fewer
* as the rows' accumulators grow. */
using I = std::integral_constant<int, 1>;
if (mm == 1) return go(I{}, std::integral_constant<int, kColsM1>{});
if (mm <= 4) return go(std::integral_constant<int, 4>{}, std::integral_constant<int, 2>{});
if (mm <= 8) return go(std::integral_constant<int, 8>{}, std::integral_constant<int, 2>{});
return go(std::integral_constant<int, 16>{}, I{});
}
} // namespace xe2
/* ------------------------------------------------------------------ prefill: through bf16 XMX */
/* For M > 16 the m2xfp4 weight is decoded to bf16 -- exact: e2m1 * (1 + k/4) has at most four
* significant bits and 2^(e-127) is within bf16's exponent -- and the activation rescaled to
* bf16(e4m3 * a_scale), one rounding of 2^-9 beside the activation's own E4M3 step of 2^-4. Then
* each part is gemm_nt_xmx over its column slice, which takes M8 tiles on XMX and the tail rows on
* the bf16 GEMV. The products differ from libref's by that one rounding and the summation order:
* held to rad-kbench's gemm tolerance and the KL bound. */
namespace xe2 {
int gemm_nt_q_m2_xmx(const RadArgs* a, RadStream st) {
if (exact_only()) return gemm_nt_q_m2(a, st);
if (!a || a->n_t < 5) return RAD_E_INVAL;
const RadTensor *A = &a->t[0], *B = &a->t[2], *Y = &a->t[4];
const RadTensor *AS = rad_arg_in(a, 1), *BS = rad_arg_in(a, 3);
if (!A->data || !B->data || !Y->data || !AS || !BS || !AS->data || !BS->data) return RAD_E_INVAL;
const int64_t K = rad_args_geti_or(a, "K", 0), N = rad_args_geti_or(a, "N", 0);
if (K <= 0 || K % 32 || N <= 0 || rad_arg_in(a, 5)) return gemm_nt_q_m2(a, st);
const int64_t M = rad_tensor_numel(Y) / N;
if (M < 1) return RAD_E_SHAPE;
const int64_t pb1 = rad_args_geti_or(a, "pb1", 0), pb2 = rad_args_geti_or(a, "pb2", 0);
const int64_t e1 = pb1 > 0 ? std::min<int64_t>(pb1, N) : N;
const int64_t e2 = pb2 > 0 ? std::max<int64_t>(e1, std::min<int64_t>(pb2, N)) : N;
const int64_t parts = e2 < N ? 3 : (e1 < N ? 2 : 1);
const int64_t kb = K / 32, akb = (K + 127) / 128;
const int64_t acols = parts * K, scols = parts * akb;
const bool blob = (B->rank == 1 && rad_tensor_numel(B) == N * (K / 2 + kb)) ||
(B->rank == 2 && B->shape[0] == N && B->shape[1] == K);
const int64_t ars = row_stride(A, acols), srs = row_stride(AS, scols), brs = row_stride(BS, kb);
const int64_t yrs = row_stride(Y, N), ycs = Y->rank >= 2 ? Y->stride[Y->rank - 1] : 1;
const bool ok = blob && A->dtype == RAD_F8E4M3 && B->dtype == RAD_U8 && BS->dtype == RAD_E8M0 &&
(A->rank < 2 || A->stride[A->rank - 1] == 1) &&
(AS->rank < 2 || AS->stride[AS->rank - 1] == 1) &&
rad_tensor_numel(A) >= M * acols && rad_tensor_numel(AS) >= M * scols &&
rad_tensor_numel(BS) >= N * kb &&
(!(pb1 > 0 || pb2 > 0) || ((e1 % 16) == 0 && (e2 % 16) == 0 && !(e1 == e2 && e2 < N)));
if (!ok) return gemm_nt_q_m2(a, st);
sycl::queue* q = queue_of(st);
if (!q) return RAD_E_DEVICE;
/* The workspace: the bf16 activation [M, parts * K], then the bf16 weight [N, K]. */
const int64_t abytes = (M * acols * 2 + 255) / 256 * 256;
float* ws = workspace(*q, (size_t)((abytes + N * K * 2) / 4 + 64));
if (!ws) return RAD_E_DEVICE;
uint16_t* a16 = (uint16_t*)ws;
uint16_t* w16 = (uint16_t*)((char*)ws + abytes);
const uint8_t* ab = (const uint8_t*)A->data;
const char* sab = (const char*)AS->data;
const uint32_t sadt = AS->dtype;
const uint8_t* codes0 = (const uint8_t*)B->data;
const uint8_t* meta0 = codes0 + N * (K / 2);
const uint8_t* expe0 = (const uint8_t*)BS->data;
int rc = submit([&] {
const int64_t na = M * acols;
q->parallel_for(sycl::range<1>((size_t)na), [=](sycl::id<1> i) {
const int64_t m = (int64_t)i[0] / acols, c = (int64_t)i[0] % acols;
const float v = e4m3(ab[m * ars + c]) * rad_load_f32(sab, sadt, m * srs + c / 128);
a16[m * acols + c] = rad_f32_to_bf16(v);
});
const int64_t nw = N * (K / 2); /* a work-item a code byte: two weights */
q->parallel_for(sycl::range<1>((size_t)nw), [=](sycl::id<1> i) {
const int64_t n = (int64_t)i[0] / (K / 2), j = (int64_t)i[0] % (K / 2);
const uint8_t byte = codes0[n * (K / 2) + j];
constexpr float lut[16] = { 0.0f, 0.5f, 1.0f, 1.5f, 2.0f, 3.0f, 4.0f, 6.0f,
-0.0f, -0.5f, -1.0f, -1.5f, -2.0f, -3.0f, -4.0f, -6.0f };
for (int h = 0; h < 2; ++h) {
const int64_t k = 2 * j + h;
const float ev = sycl::ldexp(1.0f, (int)expe0[n * brs + (k >> 5)] - 127);
const float km = 1.0f + 0.25f * (float)((meta0[n * kb + (k >> 5)] >> (2 * ((k & 31) >> 3))) & 3);
w16[n * K + k] = rad_f32_to_bf16(lut[(byte >> (4 * h)) & 0xF] * ev * km);
}
});
});
if (rc < 0) return rc;
const int64_t bound[4] = { 0, e1, e2, N };
for (int p = 0; p < 3 && rc >= 0; ++p) {
const int64_t n0 = bound[p], n1 = bound[p + 1];
if (n1 <= n0) continue;
const int64_t pi = p == 0 ? 0 : (p == 1 ? 1 : (parts == 3 ? 2 : 1));
RadTensor t[3] = {};
t[0].data = a16 + pi * K; t[0].dtype = RAD_BF16; t[0].rank = 2;
t[0].shape[0] = M; t[0].shape[1] = K; t[0].stride[0] = acols; t[0].stride[1] = 1;
t[1].data = w16 + n0 * K; t[1].dtype = RAD_BF16; t[1].rank = 2;
t[1].shape[0] = n1 - n0; t[1].shape[1] = K; t[1].stride[0] = K; t[1].stride[1] = 1;
t[2].data = (char*)Y->data + (n0 * ycs) * ((rad_dtype_bits(Y->dtype) + 7) / 8);
t[2].dtype = Y->dtype; t[2].rank = 2;
t[2].shape[0] = M; t[2].shape[1] = n1 - n0; t[2].stride[0] = yrs; t[2].stride[1] = ycs;
RadParam pr[2] = { RAD_INT("N", n1 - n0), RAD_INT("K", K) };
RadArgs b = *a;
b.t = t; b.n_t = 3; b.p = pr; b.n_p = 2;
rc = gemm_nt_xmx(&b, st);
}
return rc;
}
} // namespace xe2
+83 -2
View File
@@ -7,8 +7,9 @@
* scale = amax(|y|) / qmax (1 for an all-zero group), code = clamp(y / scale, +-qmax)
*
* qmax 448 for an E4M3 `q`, 127 (rounded to nearest even) for an i8 one; `s` holds a scale per
* (row, part, group). One work-group a (row, part, group): the inputs in local memory, a work-item
* an output j, its dot in double in libref's order, so the codes and scales are libref's.
* (row, part, group). Two forms: the fast one below (a float-pair sum; the card's f64 is slow) and
* the exact one (XE2_EXACT=1, or a rotation that is not f16): a work-item an output j, its dot in
* double in libref's order, so the codes and scales are libref's.
*/
#include "xe2_common.h"
#include "xe2_ops.h"
@@ -59,6 +60,86 @@ int givens_dense_quant(const RadArgs* a, RadStream st) {
const uint32_t xdt = X->dtype, tdt = T->dtype, ddt = D->dtype, qdt = Q->dtype, sdt = S->dtype;
sycl::queue* q = queue_of(st);
if (!q) return RAD_E_DEVICE;
if (tdt == RAD_F16 && rad_ptr_aligned(T->data, 16)) {
/* The fast form. A work-group a (part, group) and up to kRows rows; sub-group s takes the
* outputs j = 16 s .. 16 s + 15, and for each its lanes read row j of T in one coalesced
* 256-byte step (8 f16 a lane). libref's double sum becomes a float pair: each lane's eight
* products split exactly (two-product by fma) and added by two-sum, then the sixteen pairs
* combined in a fixed tree, also by two-sum -- ~48 significant bits, rounded once to f32:
* libref's y unless the exact sum sits within ~2^-24 ulp of an f32 rounding boundary. So
* this is a fast kernel, held to rad-kbench's tolerance and the KL bound; XE2_EXACT=1 takes
* the double form below. */
constexpr int64_t kRows = 16;
const int64_t rblocks = (rows + kRows - 1) / kRows;
const size_t groups = (size_t)(P * ngrp * rblocks);
if (!exact_only())
return submit([&] {
q->submit([&](sycl::handler& h) {
sycl::local_accessor<float, 1> in(sycl::range<1>(kG), h);
sycl::local_accessor<float, 1> yv(sycl::range<1>(kG), h);
h.parallel_for(sycl::nd_range<1>{groups * kG, kG},
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
const int64_t gid = (int64_t)it.get_group(0);
const int64_t rb = gid % rblocks, pg = gid / rblocks;
const int64_t gi = pg % ngrp, p = pg / ngrp;
const int t = (int)it.get_local_id(0);
auto grp = it.get_group();
const sycl::sub_group sg = it.get_sub_group();
const int sgi = (int)sg.get_group_linear_id(), lane = (int)sg.get_local_linear_id();
const uint16_t* t0 = (const uint16_t*)tb + (p * ngrp + gi) * kG * kG;
const int64_t c = gi * kG + t;
const float dsc = rad_load_f32(db, ddt, p * n + c);
auto two_sum = [](float a, float b, float& s, float& e) {
s = a + b;
const float bb = s - a;
e = (a - (s - bb)) + (b - bb);
};
for (int64_t r = rb * kRows; r < sycl::min(rows, rb * kRows + kRows); ++r) {
sycl::group_barrier(grp);
in[t] = rad_load_f32(xb, xdt, r * xrs + c * xcs) / dsc;
sycl::group_barrier(grp);
float xin[8];
for (int u = 0; u < 8; ++u) xin[u] = in[lane * 8 + u];
for (int jj = 0; jj < 16; ++jj) {
const int j = sgi * 16 + jj;
const sycl::vec<uint16_t, 8> tv = *(const sycl::vec<uint16_t, 8>*)(t0 + j * kG + lane * 8);
float hi = 0.0f, lo = 0.0f;
for (int u = 0; u < 8; ++u) {
const float tw = (float)sycl::bit_cast<sycl::half>(tv[u]);
const float pr = tw * xin[u];
const float pe = sycl::fma(tw, xin[u], -pr);
float sv, se;
two_sum(hi, pr, sv, se);
hi = sv;
lo += se + pe;
}
for (int off = 8; off > 0; off >>= 1) {
const float h2 = sycl::shift_group_left(sg, hi, off);
const float l2 = sycl::shift_group_left(sg, lo, off);
float sv, se;
two_sum(hi, h2, sv, se);
hi = sv;
lo = lo + l2 + se;
}
if (lane == 0) yv[j] = hi + lo;
}
sycl::group_barrier(grp);
const float y = yv[t];
const float amax = sycl::reduce_over_group(grp, sycl::fabs(y), sycl::maximum<float>());
const float scl = amax > 0.0f ? amax * (1.0f / qmax) : 1.0f;
const float inv = 1.0f / scl;
float v = y * inv;
if (v > qmax) v = qmax;
if (v < -qmax) v = -qmax;
if (i8) v = sycl::rint(v);
rad_store_f32(qb, qdt, r * qrs + (p * n + c) * qcs, v);
if (t == 0) rad_store_f32(sb, sdt, r * srs + (p * ngrp + gi) * scs, scl);
}
});
});
});
}
/* The exact form, libref's double sum in libref's order: a work-group a (row, part, group). */
const size_t groups = (size_t)(rows * P * ngrp);
return submit([&] {
q->submit([&](sycl::handler& h) {
+77 -7
View File
@@ -28,6 +28,7 @@ namespace xe2 {
namespace {
constexpr size_t kWg = 256; /* work-items a row */
constexpr int64_t kRowMax = 8192; /* a row rmsnorm_quant_fp8 keeps in local memory */
/* sigmoid exactly as libref's act_sigmoid: exp in double, rounded once to f32 (ref_exp). An f32
* exp is within an ulp, inside rad-kbench's tolerance, but an ulp in silu now and then moves the
@@ -55,14 +56,16 @@ inline float ordered_sumsq(const sycl::nd_item<1>& it, const F& src, int64_t n)
sq[i] = v * v;
}
sycl::group_barrier(grp);
if (t == 0) {
/* Eight independent loads a step, then the eight adds in index order: the same sum,
* without waiting on one local-memory load per addition. */
const sycl::sub_group sg = it.get_sub_group();
if (sg.get_group_linear_id() == 0) {
/* Sub-group 0 takes the squares sixteen at a time, one a lane, and every lane adds them
* in index order from the lanes' registers: the same chain of additions as libref's
* serial loop, without a local-memory load per addition. */
const int lane = (int)sg.get_local_linear_id();
int64_t i = 0;
for (; i + 8 <= cn; i += 8) {
float b[8];
for (int j = 0; j < 8; ++j) b[j] = sq[i + j];
for (int j = 0; j < 8; ++j) ss += b[j];
for (; i + kSub <= cn; i += kSub) {
const float v = sq[i + lane];
for (int j = 0; j < kSub; ++j) ss += sycl::select_from_group(sg, v, j);
}
for (; i < cn; ++i) ss += sq[i];
}
@@ -245,6 +248,73 @@ int rmsnorm_quant_fp8(const RadArgs* a, RadStream st) {
sycl::queue* q = queue_of(st);
if (!q) return RAD_E_DEVICE;
constexpr int kSubs = (int)(kWg / kSub);
if (n <= kRowMax) {
/* A row up to kRowMax in local memory: the (narrowed) residual sum and its squares are
* formed once from one read of x and the residual, the sum stored once; sub-group 0 adds
* the squares in index order through register broadcasts (libref's chain); the groups
* read their values from local memory. The same arithmetic as the general form below,
* without re-reading what the work-group just stored. */
return submit([&] {
q->submit([&](sycl::handler& h) {
sycl::local_accessor<float, 1> sv_(sycl::range<1>((size_t)n), h);
sycl::local_accessor<float, 1> sq(sycl::range<1>((size_t)n), h);
sycl::local_accessor<float, 1> wl(sycl::range<1>((size_t)n), h);
h.parallel_for(sycl::nd_range<1>{(size_t)rows * kWg, kWg},
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
const int64_t r = (int64_t)it.get_group(0);
const int t = (int)it.get_local_id(0);
auto grp = it.get_group();
/* Every load of the work-item first, then the stores: interleaved, each load would
* wait for the store before it (the residual may alias x as far as the compiler
* knows), a memory round trip an element. */
constexpr int kPer = (int)(kRowMax / kWg);
float xv[kPer], rv[kPer], wv[kPer];
for (int j = 0; j < kPer; ++j) {
const int64_t i = t + (int64_t)j * kWg;
if (i >= n) break;
xv[j] = rad_load_f32(x.base, x.dtype, r * x.pitch + i);
rv[j] = has_res ? rad_load_f32(res.base, res.dtype, r * res.pitch + i) : 0.0f;
wv[j] = rad_load_f32(gw.base, gw.dtype, i * gw.stride) + wadd;
}
for (int j = 0; j < kPer; ++j) {
const int64_t i = t + (int64_t)j * kWg;
if (i >= n) break;
float v = xv[j];
if (has_res) {
const float s0 = v + rv[j];
rad_store_f32(res.base, res.dtype, r * res.pitch + i, s0);
v = round_dt(res.dtype, s0); /* what reading it back gives */
}
sv_[i] = v;
sq[i] = v * v;
wl[i] = wv[j];
}
sycl::group_barrier(grp);
const sycl::sub_group sg = it.get_sub_group();
float ss = 0.0f;
if (sg.get_group_linear_id() == 0) {
const int lane = (int)sg.get_local_linear_id();
int64_t i = 0;
for (; i + kSub <= n; i += kSub) {
const float v = sq[i + lane];
for (int j = 0; j < kSub; ++j) ss += sycl::select_from_group(sg, v, j);
}
for (; i < n; ++i) ss += sq[i];
}
ss = sycl::group_broadcast(grp, ss, 0);
const float rs = 1.0f / sycl::sqrt(ss / (float)n + eps);
auto v = [&](int64_t i) { return rad_bf16_to_f32(rad_f32_to_bf16(sv_[i] * rs * wl[i])); };
const int sgi = (int)sg.get_group_linear_id();
for (int64_t gi = sgi; gi < ngrp; gi += kSubs) {
const int64_t k0 = gi * g, len = sycl::min(g, n - k0);
const float sc = quant_group(sg, v, k0, len, top, qv.base, qv.dtype, r * qv.pitch,
has_ob ? ob.base : nullptr, ob.dtype, r * ob.pitch);
if (sg.get_local_linear_id() == 0) rad_store_f32(sv.base, sv.dtype, r * sv.pitch + gi, sc);
}
});
});
});
}
return submit([&] {
syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)rows * kWg, kWg},
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
+3
View File
@@ -68,6 +68,8 @@ int gemm_zp_unrelayout(const RadParam* p, int n_p, int operand, const RadEncodin
const int* sel, const void* src, int64_t src_bytes, const RadTensor* planes,
int n);
int gemm_nt_q_m2(const RadArgs* a, RadStream s);
int gemm_nt_q_m2_gemv(const RadArgs* a, RadStream s);
int gemm_nt_q_m2_xmx(const RadArgs* a, RadStream s);
int givens_dense_quant(const RadArgs* a, RadStream s);
int gemm_nt_bias(const RadArgs* a, RadStream s);
int dflash_conv(const RadArgs* a, RadStream s);
@@ -75,6 +77,7 @@ int attn_dense(const RadArgs* a, RadStream s);
int logit_rerank(const RadArgs* a, RadStream s);
int logit_rerank_head(const RadArgs* a, RadStream s);
int logits_gemm_head(const RadArgs* a, RadStream s);
int logits_gemm_head_gemv(const RadArgs* a, RadStream s);
int head_layout(const RadParam* p, int n_p, int operand, const RadEncoding* enc, const int* sel,
const RadTensor* planes, int n, RadLayout* out);
int head_relayout(const RadParam* p, int n_p, int operand, const RadEncoding* enc, const int* sel,
+29
View File
@@ -26,6 +26,12 @@ const RadConstraint cFp8Gated[] = { RAD_CIN("dtype", "fp8a8 fp8a8f32 fp8a16"), R
const RadConstraint cZpI8Dtype[] = { RAD_CIN("dtype", "w2a8 w4a8_asym") };
/* The M2XFP4 grid over givens_dense_quant's fp8 activation (xe2_gemm_m2.cpp). */
const RadConstraint cM2Dtype[] = { RAD_CIN("dtype", "m2xfp4") };
/* The fp8 head's decode band. */
const RadConstraint cHeadGemv[] = { RAD_CLE("M", 16) };
/* Its decode / verify band (xe2_gemv_m2.cpp). */
const RadConstraint cM2Gemv[] = { RAD_CIN("dtype", "m2xfp4"), RAD_CLE("M", 16) };
/* Its prefill band (bf16 XMX, xe2_gemv_m2.cpp). */
const RadConstraint cM2Xmx[] = { RAD_CIN("dtype", "m2xfp4"), RAD_CGE("M", 17) };
/* 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. */
@@ -263,6 +269,22 @@ const RadKernelInfo kKernels[] = {
.domain = RAD_DOMAIN_DEVICE, .constraints = cZpI8Dtype, .n_constraints = 1,
.launch = xe2::gemm_nt_q_zp, .layout = xe2::gemm_zp_layout,
.relayout = xe2::gemm_zp_relayout, .unrelayout = xe2::gemm_zp_unrelayout },
{ .name = "xe2_gemm_nt_q_m2_gemv", .op = "gemm_nt_q", .family = "gemm",
.computes = "Y = A B^T over the M2XFP4 grid for M <= 16: e2m1 to fp16 by bits, fp16 activation "
"and subgroup sums, f32 group sums; libref's products regrouped",
.shape = "M <= 16; K a multiple of 32, group 32",
.dtypes = "m2xfp4 weight, E4M3 activation with a scale per 128 columns",
.domain = RAD_DOMAIN_DEVICE, .priority = 10, .constraints = cM2Gemv, .n_constraints = 2,
.launch = xe2::gemm_nt_q_m2_gemv, .layout = xe2::gemm_m2_layout,
.relayout = xe2::gemm_m2_relayout, .unrelayout = xe2::gemm_m2_unrelayout },
{ .name = "xe2_gemm_nt_q_m2_xmx", .op = "gemm_nt_q", .family = "gemm",
.computes = "Y = A B^T over the M2XFP4 grid for M >= 17: the weight decoded to bf16 (exact), "
"the activation to bf16(e4m3 * scale), then the bf16 XMX GEMM per part",
.shape = "M >= 17; K a multiple of 32, group 32",
.dtypes = "m2xfp4 weight, E4M3 activation with a scale per 128 columns",
.domain = RAD_DOMAIN_DEVICE, .priority = 5, .constraints = cM2Xmx, .n_constraints = 2,
.launch = xe2::gemm_nt_q_m2_xmx, .layout = xe2::gemm_m2_layout,
.relayout = xe2::gemm_m2_relayout, .unrelayout = xe2::gemm_m2_unrelayout },
{ .name = "xe2_gemm_nt_q_m2_ref", .op = "gemm_nt_q", .family = "gemm",
.computes = "Y = A B^T over the M2XFP4 grid against givens_dense_quant's fp8 activation, "
"libref's gemm_m2 per output; the correctness-first form",
@@ -300,6 +322,13 @@ const RadKernelInfo kKernels[] = {
.computes = "Y = A B^T + bias, then gelu / gelu_tanh and a residual; libref's order",
.shape = "any M, N, K", .dtypes = "float A and B",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::gemm_nt_bias },
{ .name = "xe2_logits_gemm_head_gemv", .op = "logits_gemm", .family = "vocab",
.computes = "the lm_head over a joined E4M3 / int8 head at M <= 16: a sub-group a column, "
"16 codes a lane step inside a scale block",
.shape = "M <= 16, n_embd a multiple of 128", .dtypes = "float A; E4M3 codes, float block scale",
.domain = RAD_DOMAIN_DEVICE, .priority = 10, .constraints = cHeadGemv, .n_constraints = 1,
.launch = xe2::logits_gemm_head_gemv, .layout = xe2::head_layout,
.relayout = xe2::head_relayout, .unrelayout = xe2::head_unrelayout },
{ .name = "xe2_logits_gemm_head_ref", .op = "logits_gemm", .family = "vocab",
.computes = "the lm_head over a head of E4M3 / int8 codes with a scale plane, joined; each "
"weight decoded to code * scale, then libref's plain product",