Archived
libxe2: fused attention prologue (qkv_norm_rope, qkv_norm_rope_kv_store)
The one-launch prologue that Gemma 3, Gemma 4, Hunyuan v3 and the DFlash2 drafter use where a kernel library declares it (libr4d did; libxe2 did not, so those models ran the unfused five or six launches a layer). docs/OPS.md requires it byte-identical to the unfused ops, so each step is libxe2's own: xe2_rmsnorm's bf16 head-row norm (its partition and reduce_over_group; XE2_EXACT=1 libref's ascending sum, as xe2_rmsnorm then takes), the bf16 rounding the norm stores, xe2_rope's arithmetic on its frequency table, xe2_kv_store's conversion. A sub-group a (token, head), the head staged in local memory for the rotary pairs. The schemas are libr4d's. XE2_NO_FUSED_PROLOGUE=1 leaves the two rows undeclared, so an architecture takes its unfused chain: the A/B, and an escape hatch. Gemma 4 E2B: greedy outputs identical with and without (4 requests), decode 58.0 -> 58.6 tok/s. A random Gemma 3 (v_norm 0): identical, 1570 -> 1600 tok/s. Default fixture 2144/2144. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
25c8fbd136
commit
0c7606423b
3 files changed
+366
-5
No files matched your search
@@ -28,6 +28,8 @@ int logit_softcap(const RadArgs* a, RadStream s);
|
||||
int rel_sinusoid(const RadArgs* a, RadStream s);
|
||||
int pool_rows(const RadArgs* a, RadStream s);
|
||||
int qk_norm_rope(const RadArgs* a, RadStream s);
|
||||
int qkv_norm_rope(const RadArgs* a, RadStream s);
|
||||
int qkv_norm_rope_kv_store(const RadArgs* a, RadStream s);
|
||||
int conv1d_dw(const RadArgs* a, RadStream s);
|
||||
int conv2d(const RadArgs* a, RadStream s);
|
||||
int attn_local_rel(const RadArgs* a, RadStream s);
|
||||
|
||||
@@ -348,6 +348,254 @@ int qk_norm_rope(const RadArgs* a, RadStream st) {
|
||||
});
|
||||
}
|
||||
|
||||
/* qkv_norm_rope / qkv_norm_rope_kv_store: the attention prologue over separate q, k and v (Gemma
|
||||
* 3 and 4, a DFlash2 drafter, Hunyuan v3) in one launch: the value norm (rmsnorm_p), the q and k
|
||||
* norms (rmsnorm a head), the two rotaries and, with _kv_store, kv_store of the finished k and v.
|
||||
* docs/OPS.md requires it byte-identical to those ops, so each step is libxe2's own: a head's norm
|
||||
* is xe2_rmsnorm's bf16 head-row form (a lane n/16 contiguous elements, its squares in order, the
|
||||
* lanes by reduce_over_group; under XE2_EXACT=1 libref's ascending sum, as xe2_rmsnorm then takes),
|
||||
* rounded to bf16 as the norm stores it; the rotary is xe2_rope's arithmetic on its frequency table;
|
||||
* the store converts as xe2_kv_store does. A sub-group a (token, head); the head is staged in local
|
||||
* memory so a rotary pair may sit on any two lanes. Five or six launches a layer become one: on
|
||||
* Gemma 4 E2B's 35 layers that is ~175 fewer a decode step. */
|
||||
namespace {
|
||||
|
||||
constexpr int kQnrSg = 4; /* sub-groups (tasks) a work-group */
|
||||
|
||||
struct QnrHead {
|
||||
char* base = nullptr; /* bf16 rows */
|
||||
int64_t pitch = 0; /* elements */
|
||||
};
|
||||
|
||||
int qkv_norm_rope_impl(const RadArgs* a, RadStream st, bool store) {
|
||||
if (!a || a->n_t < (store ? 8 : 6)) return RAD_E_INVAL;
|
||||
const RadTensor *Q = rad_arg_in(a, 0), *K = rad_arg_in(a, 1), *V = rad_arg_in(a, 2);
|
||||
const RadTensor *POS = rad_arg_in(a, 3), *QW = rad_arg_in(a, 4), *KW = rad_arg_in(a, 5);
|
||||
if (!Q || !K || !V || !POS || !QW || !KW) return RAD_E_INVAL;
|
||||
if (Q->dtype != RAD_BF16 || K->dtype != RAD_BF16 || V->dtype != RAD_BF16)
|
||||
return refuse("qkv_norm_rope: q, k and v must be bf16");
|
||||
if (QW->dtype != KW->dtype || (QW->dtype != RAD_BF16 && QW->dtype != RAD_F32))
|
||||
return refuse("qkv_norm_rope: gains must both be bf16 or f32");
|
||||
const int64_t hd = rad_args_geti_or(a, "head_dim", 0);
|
||||
const int64_t nq = rad_args_geti_or(a, "n_head", 0);
|
||||
const int64_t nkv = rad_args_geti_or(a, "n_head_kv", 0);
|
||||
int64_t rot = rad_args_geti_or(a, "rotary_dim", hd);
|
||||
const int vk = (int)rad_args_geti_or(a, "v_from_k", 0);
|
||||
const int vn = (int)rad_args_geti_or(a, "v_norm", 1);
|
||||
if (hd != 128 && hd != 256 && hd != 512) return refuse("qkv_norm_rope: head_dim %lld", (long long)hd);
|
||||
if (vk && !vn) return RAD_E_INVAL;
|
||||
if (nq < 0 || nkv <= 0 || rot <= 0 || rot > hd || (rot & 1)) return RAD_E_SHAPE;
|
||||
if (rad_tensor_numel(QW) < hd || rad_tensor_numel(KW) < hd) return RAD_E_SHAPE;
|
||||
if (QW->rank && QW->stride[QW->rank - 1] != 1) return RAD_E_STRIDE;
|
||||
if (KW->rank && KW->stride[KW->rank - 1] != 1) return RAD_E_STRIDE;
|
||||
const char* mode = rad_args_gets_or(a, "mode", "neox");
|
||||
if (std::strcmp(mode, "neox")) return RAD_E_UNSUPPORTED;
|
||||
if (rad_args_getf_or(a, "yarn_factor", 0.0) != 0.0) return RAD_E_UNSUPPORTED;
|
||||
if (POS->rank > 1) return RAD_E_SHAPE;
|
||||
int64_t den;
|
||||
if (int rc = ladder(a, rot, hd, &den); rc < 0) return rc;
|
||||
const int64_t nfreq = rot / 2;
|
||||
if (den == hd && rot < hd) rot = hd; /* xe2_rope's widening; frequencies past nfreq are 0 */
|
||||
Freqs f{};
|
||||
if (int rc = freqs(kNeox, rot, den, (float)rad_args_getf_or(a, "theta", 10000.0), nullptr, 0, &f); rc < 0)
|
||||
return rc;
|
||||
for (int64_t i = nfreq; i < rot / 2; ++i) f.inv[i] = 0.0f;
|
||||
const float eps = (float)rad_args_getf_or(a, "eps", 1e-6);
|
||||
const float wadd = (float)rad_args_getf_or(a, "wadd", 0.0);
|
||||
const float rsc = (float)rad_args_getf_or(a, "scale", 1.0);
|
||||
const float div = rsc != 0.0f ? rsc : 1.0f;
|
||||
|
||||
Rows q{}, k{}, v{};
|
||||
if (nq) {
|
||||
if (int rc = rows_view(Q, nq * hd, &q, "qkv_norm_rope q"); rc < 0) return rc;
|
||||
}
|
||||
if (int rc = rows_view(K, nkv * hd, &k, "qkv_norm_rope k"); rc < 0) return rc;
|
||||
if (int rc = rows_view(V, nkv * hd, &v, "qkv_norm_rope v"); rc < 0) return rc;
|
||||
const int64_t M = nq ? q.rows : k.rows;
|
||||
const int64_t npos = rad_tensor_numel(POS);
|
||||
if (npos <= 0 || M < npos || k.rows != M || v.rows != M) return RAD_E_SHAPE;
|
||||
auto vec_ok = [](const Rows& r) { return r.pitch % 8 == 0 && rad_ptr_aligned(r.base, 16); };
|
||||
if ((nq && !vec_ok(q)) || !vec_ok(k) || !vec_ok(v)) return refuse("qkv_norm_rope: rows not 16-byte aligned");
|
||||
|
||||
/* the cache, as xe2_kv_store reads it */
|
||||
const char* sm = nullptr;
|
||||
uint32_t smdt = 0;
|
||||
int64_t nslot = 0, bs = 0, nblk = 0, csb = 0, csh = 0, cso = 0, csd = 1;
|
||||
char* cb = nullptr;
|
||||
uint32_t cdt = 0;
|
||||
float kinv = 1.0f, vinv = 1.0f;
|
||||
if (store) {
|
||||
const RadTensor *SM = rad_arg_in(a, 6), *KV = rad_arg_in(a, 7);
|
||||
if (!SM || !KV) return RAD_E_INVAL;
|
||||
if (!rad_tensor_is_contiguous(SM)) return refuse("qkv_norm_rope_kv_store: slot_mapping not contiguous");
|
||||
bs = rad_args_geti_or(a, "block_size", 0);
|
||||
if (bs <= 0) return RAD_E_SHAPE;
|
||||
nslot = rad_tensor_numel(SM);
|
||||
if (nslot <= 0 || nslot > M) return RAD_E_SHAPE;
|
||||
if (KV->rank == 4) {
|
||||
if (KV->shape[1] != nkv || KV->shape[2] != bs || KV->shape[3] < 2 * hd) return RAD_E_SHAPE;
|
||||
nblk = KV->shape[0];
|
||||
csb = KV->stride[0]; csh = KV->stride[1]; cso = KV->stride[2]; csd = KV->stride[3];
|
||||
} else {
|
||||
cso = 2 * hd; csh = bs * cso; csb = nkv * csh;
|
||||
}
|
||||
const bool fp8 = KV->dtype == RAD_F8E4M3 || KV->dtype == RAD_F8E5M2;
|
||||
kinv = fp8 ? 1.0f / (float)rad_args_getf_or(a, "k_scale", 1.0) : 1.0f;
|
||||
vinv = fp8 ? 1.0f / (float)rad_args_getf_or(a, "v_scale", 1.0) : 1.0f;
|
||||
sm = (const char*)SM->data;
|
||||
smdt = SM->dtype;
|
||||
cb = (char*)KV->data;
|
||||
cdt = KV->dtype;
|
||||
}
|
||||
/* v's own heads: normed (and stored), stored only, or with neither nothing to do */
|
||||
const int64_t heads = nq + nkv + ((vk || (!vn && !store)) ? 0 : nkv);
|
||||
const int64_t total = M * heads;
|
||||
const bool exact = exact_only();
|
||||
const int nv = (int)(hd / 128); /* 16-byte vectors a lane */
|
||||
const char* qwb = (const char*)QW->data;
|
||||
const char* kwb = (const char*)KW->data;
|
||||
const bool wf32 = QW->dtype == RAD_F32;
|
||||
const char* pb = (const char*)POS->data;
|
||||
const uint32_t pdt = POS->dtype;
|
||||
const Lin pl = Lin::of(POS);
|
||||
sycl::queue* qu = queue_of(st);
|
||||
if (!qu) return RAD_E_DEVICE;
|
||||
using V8 = sycl::vec<uint16_t, 8>;
|
||||
const size_t wg = (size_t)kQnrSg * kSub;
|
||||
const size_t groups = (size_t)((total + kQnrSg - 1) / kQnrSg);
|
||||
return submit([&] {
|
||||
syclex::nd_launch(*qu, 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 int sgi = (int)sg.get_group_linear_id();
|
||||
float* lbuf = *sycl::ext::oneapi::group_local_memory_for_overwrite<float[kQnrSg * 512]>(it.get_group());
|
||||
float* buf = lbuf + sgi * 512;
|
||||
const int64_t task = (int64_t)it.get_group(0) * kQnrSg + sgi;
|
||||
if (task >= total) return; /* uniform over the sub-group */
|
||||
const int64_t m = task / heads, hh = task % heads;
|
||||
const int l = (int)sg.get_local_linear_id();
|
||||
const int per = nv * 8; /* contiguous elements a lane */
|
||||
|
||||
/* the head's raw values, a lane's run of `per` */
|
||||
auto load = [&](const char* base, int64_t off, float* x) {
|
||||
for (int u = 0; u < 4; ++u) {
|
||||
if (u >= nv) break;
|
||||
const V8 w = *(const V8*)((const uint16_t*)base + off + (l * nv + u) * 8);
|
||||
for (int j = 0; j < 8; ++j) x[u * 8 + j] = sycl::bit_cast<float>((uint32_t)w[j] << 16);
|
||||
}
|
||||
};
|
||||
/* xe2_rmsnorm on one head row: y = bf16(x * s * gw * mult), mult 1 */
|
||||
auto norm = [&](const float* x, const char* gb, float* y) {
|
||||
float ss = 0.0f;
|
||||
if (!exact) {
|
||||
for (int e = 0; e < 32; ++e) {
|
||||
if (e >= per) break;
|
||||
ss += x[e] * x[e];
|
||||
}
|
||||
ss = sycl::reduce_over_group(sg, ss, sycl::plus<float>());
|
||||
} else {
|
||||
for (int L = 0; L < kSub; ++L)
|
||||
for (int e = 0; e < 32; ++e) {
|
||||
if (e >= per) break;
|
||||
const float xv = sycl::select_from_group(sg, x[e], L);
|
||||
ss += xv * xv;
|
||||
}
|
||||
}
|
||||
const float s = 1.0f / sycl::sqrt(ss / (float)hd + eps);
|
||||
const float mult = 1.0f;
|
||||
for (int e = 0; e < 32; ++e) {
|
||||
if (e >= per) break;
|
||||
const int64_t i = (int64_t)l * per + e;
|
||||
const float gw = !gb ? 1.0f
|
||||
: (wf32 ? ((const float*)gb)[i]
|
||||
: sycl::bit_cast<float>((uint32_t)((const uint16_t*)gb)[i] << 16)) + wadd;
|
||||
y[e] = rad_bf16_to_f32(rad_f32_to_bf16(x[e] * s * gw * mult));
|
||||
}
|
||||
};
|
||||
auto put = [&](char* base, int64_t off, const float* y) {
|
||||
for (int u = 0; u < 4; ++u) {
|
||||
if (u >= nv) break;
|
||||
V8 o;
|
||||
for (int j = 0; j < 8; ++j) o[j] = rad_f32_to_bf16(y[u * 8 + j]);
|
||||
*(V8*)((uint16_t*)base + off + (l * nv + u) * 8) = o;
|
||||
}
|
||||
};
|
||||
/* the cache slot of row m, or -1 */
|
||||
int64_t cbase = -1;
|
||||
if (store && m < nslot) {
|
||||
const int64_t slot = ld_index(smdt, sm, m);
|
||||
if (slot >= 0) {
|
||||
const int64_t blk = slot / bs, off = slot % bs;
|
||||
if (nblk <= 0 || blk < nblk) cbase = blk * csb + off * cso;
|
||||
}
|
||||
}
|
||||
auto cache = [&](int64_t h, int64_t col0, const float* y, float inv) {
|
||||
if (cbase < 0) return;
|
||||
for (int e = 0; e < 32; ++e) {
|
||||
if (e >= per) break;
|
||||
const int64_t d = (int64_t)l * per + e;
|
||||
rad_store_f32(cb, cdt, cbase + h * csh + (col0 + d) * csd, y[e] * inv);
|
||||
}
|
||||
};
|
||||
|
||||
float x[32], y[32];
|
||||
if (hh >= nq + nkv) { /* a value head: rmsnorm_p in place, or only stored */
|
||||
const int64_t h = hh - nq - nkv;
|
||||
const int64_t off = m * v.pitch + h * hd;
|
||||
load(v.base, off, x);
|
||||
if (vn) {
|
||||
norm(x, nullptr, y);
|
||||
put(v.base, off, y);
|
||||
cache(h, hd, y, vinv);
|
||||
} else {
|
||||
cache(h, hd, x, vinv);
|
||||
}
|
||||
return;
|
||||
}
|
||||
const bool isq = hh < nq;
|
||||
const int64_t h = isq ? hh : hh - nq;
|
||||
const Rows& r = isq ? q : k;
|
||||
const int64_t off = m * r.pitch + h * hd;
|
||||
load(r.base, off, x);
|
||||
if (vk && !isq) { /* v = rmsnorm_p(raw k), before the k norm */
|
||||
norm(x, nullptr, y);
|
||||
put(v.base, m * v.pitch + h * hd, y);
|
||||
cache(h, hd, y, vinv);
|
||||
}
|
||||
norm(x, isq ? qwb : kwb, y);
|
||||
if (m < npos) {
|
||||
for (int e = 0; e < 32; ++e) {
|
||||
if (e >= per) break;
|
||||
buf[l * per + e] = y[e];
|
||||
}
|
||||
sycl::group_barrier(sg);
|
||||
const float p = pos_at(pb, pdt, pl.off(m), div);
|
||||
const int64_t half = rot / 2;
|
||||
for (int64_t i = l; i < half; i += kSub) {
|
||||
const float ang = p * f.inv[i];
|
||||
const float cs = (float)sycl::cos((double)ang) * 1.0f;
|
||||
const float sn = (float)sycl::sin((double)ang) * 1.0f;
|
||||
const float x0 = buf[i], x1 = buf[i + half];
|
||||
buf[i] = rad_bf16_to_f32(rad_f32_to_bf16(x0 * cs - x1 * sn));
|
||||
buf[i + half] = rad_bf16_to_f32(rad_f32_to_bf16(x0 * sn + x1 * cs));
|
||||
}
|
||||
sycl::group_barrier(sg);
|
||||
for (int e = 0; e < 32; ++e) {
|
||||
if (e >= per) break;
|
||||
y[e] = buf[l * per + e];
|
||||
}
|
||||
}
|
||||
put(r.base, off, y);
|
||||
if (!isq) cache(h, 0, y, kinv);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int qkv_norm_rope(const RadArgs* a, RadStream st) { return qkv_norm_rope_impl(a, st, false); }
|
||||
int qkv_norm_rope_kv_store(const RadArgs* a, RadStream st) { return qkv_norm_rope_impl(a, st, true); }
|
||||
|
||||
/* rope_table: cos and sin rows for each position, cs[pos * rot + i] and cs[pos * rot + half + i],
|
||||
* as libref's ref_rope_table (a position outside the table is skipped). */
|
||||
int rope_table(const RadArgs* a, RadStream st) {
|
||||
|
||||
+116
-5
@@ -6,6 +6,9 @@
|
||||
* row here, and on the SYCL backend the engine stops at startup naming it (docs/SYCL_PORTING.md
|
||||
* §4.4).
|
||||
*/
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
#include "rad_abi.h"
|
||||
#include "rad_abi_layout.h"
|
||||
#include "xe2_ops.h"
|
||||
@@ -17,6 +20,8 @@ const RadConstraint cRopeModes[] = { RAD_CIN("mode", "neox gptj mrope imrope axi
|
||||
const RadConstraint cFloatDtype[] = { RAD_CIN("dtype", "bf16 f16 fp16 f32 fp32") };
|
||||
/* bf16 operands, any M (xe2_gemm_nt_bias picks its GEMM by M). */
|
||||
const RadConstraint cBf16[] = { RAD_CIN("dtype", "bf16") };
|
||||
/* the fused attention prologue: bf16 heads of 128, 256 or 512 */
|
||||
const RadConstraint cQnr[] = { RAD_CIN("dtype", "bf16"), RAD_CDIV("head_dim", 128), RAD_CLE("head_dim", 512) };
|
||||
/* The decode band of the skinny bf16 GEMV (xe2_gemv.cpp). */
|
||||
const RadConstraint cGemvBf16[] = { RAD_CIN("dtype", "bf16"), RAD_CLE("M", 16) };
|
||||
/* The prefill band of the bf16 XMX GEMM (xe2_gemm_xmx.cpp). */
|
||||
@@ -124,6 +129,17 @@ const RadKernelInfo kKernels[] = {
|
||||
.computes = "libref's per-head RMS norm (gain + wadd, or unit), rounded, then rotation: neox, gptj, blocks, gptj_blocks, imrope, axial; v_norm",
|
||||
.shape = "head_dim, rotary_dim <= 512; a work-item a (row, head)", .dtypes = "any float qkv, gains",
|
||||
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::qk_norm_rope },
|
||||
{ .name = "xe2_qkv_norm_rope", .op = "qkv_norm_rope", .family = "rope",
|
||||
.computes = "rmsnorm_p (v, or v from the raw k), the q and k rmsnorms and the two NeoX rotaries in one "
|
||||
"launch, byte-identical to libxe2's five (its bf16 head-row norm; XE2_EXACT=1 libref's order)",
|
||||
.shape = "head_dim 128, 256 or 512; q, k, v rows 16-byte aligned; positions [M]",
|
||||
.dtypes = "bf16 q, k, v; bf16 or f32 gains",
|
||||
.domain = RAD_DOMAIN_DEVICE, .constraints = cQnr, .n_constraints = 3, .launch = xe2::qkv_norm_rope },
|
||||
{ .name = "xe2_qkv_norm_rope_kv_store", .op = "qkv_norm_rope_kv_store", .family = "rope",
|
||||
.computes = "xe2_qkv_norm_rope, then xe2_kv_store of the finished k and v heads from the same launch",
|
||||
.shape = "as xe2_qkv_norm_rope; any paged cache xe2_kv_store takes",
|
||||
.dtypes = "bf16 q, k, v; a bf16 or fp8 cache",
|
||||
.domain = RAD_DOMAIN_DEVICE, .constraints = cQnr, .n_constraints = 3, .launch = xe2::qkv_norm_rope_kv_store },
|
||||
{ .name = "xe2_conv1d_dw", .op = "conv1d_dw", .family = "conv",
|
||||
.computes = "causal depthwise conv over time within cu segments, f32 in tap order, + bias, one rounding",
|
||||
.shape = "rows of n, k taps", .dtypes = "any float dtype",
|
||||
@@ -276,7 +292,7 @@ const RadKernelInfo kKernels[] = {
|
||||
.computes = "split-KV paged attention with an online softmax: a work-group a (row, kv head, "
|
||||
"key partition), its sub-groups the kv head's query heads, partitions merged from "
|
||||
"scratch (not libref's order). XE2_EXACT=1 runs xe2_attn_paged_ref instead",
|
||||
.shape = "head_dim a multiple of 16 up to 256 (else the exact kernel); any gqa, causal or "
|
||||
.shape = "head_dim a multiple of 16 up to 512 (else the exact kernel); any gqa, causal or "
|
||||
"not, sliding window, ragged batches",
|
||||
.dtypes = "bf16 query, bf16 or E4M3 cache with per-(seq, head) descales, bf16 out",
|
||||
.domain = RAD_DOMAIN_DEVICE, .priority = 10, .launch = xe2::attn_paged_fast,
|
||||
@@ -540,11 +556,81 @@ const RadParamSpec pAddRmsNorm[] = {
|
||||
const RadOperandSpec oAddRmsNorm[] = {
|
||||
{ "x", RAD_OPD_INOUT, 0 }, { "a", RAD_OPD_IN, 0 }, { "w", RAD_OPD_WEIGHT, 0 }, { "y", RAD_OPD_OUT, 0 },
|
||||
};
|
||||
/* libr4d's schemas for the fused attention prologue (r4d_rows.cpp), which libxe2 now also declares */
|
||||
const RadParamSpec pQkvNormRope[] = {
|
||||
{ "M", RAD_P_INT, 1 },
|
||||
{ "head_dim", RAD_P_INT, 1 },
|
||||
{ "n_head", RAD_P_INT, 1 },
|
||||
{ "n_head_kv", RAD_P_INT, 1 },
|
||||
{ "theta", RAD_P_F64, 1 },
|
||||
{ "eps", RAD_P_F64, 1 },
|
||||
{ "scale", RAD_P_F64, 0 },
|
||||
{ "mode", RAD_P_STR, 0 },
|
||||
{ "rotary_dim", RAD_P_INT, 0 },
|
||||
{ "ladder", RAD_P_STR, 0 },
|
||||
{ "wadd", RAD_P_F64, 0 },
|
||||
{ "v_from_k", RAD_P_INT, 0 },
|
||||
/* 0 leaves v unnormed (Gemma 3; a Qwen3-shaped layer); absent, 1 */
|
||||
{ "v_norm", RAD_P_INT, 0 },
|
||||
{ "dtype", RAD_P_STR, 1 },
|
||||
};
|
||||
const RadOperandSpec oQkvNormRope[] = {
|
||||
{ "q", RAD_OPD_INOUT, 0 },
|
||||
{ "k", RAD_OPD_INOUT, 0 },
|
||||
{ "v", RAD_OPD_INOUT, 0 }, /* written from the raw k under v_from_k */
|
||||
{ "positions", RAD_OPD_IN, 0 },
|
||||
{ "q_w", RAD_OPD_WEIGHT, 0 },
|
||||
{ "k_w", RAD_OPD_WEIGHT, 0 },
|
||||
};
|
||||
/* `qkv_norm_rope_kv_store` is qkv_norm_rope followed by kv_store in one launch: the prologue's
|
||||
* operands, then kv_store's slot mapping and paged cache; every parameter is one of the two ops'.
|
||||
* Byte-identical to the pair (docs/OPS.md). */
|
||||
const RadParamSpec pQkvNormRopeKv[] = {
|
||||
{ "M", RAD_P_INT, 1 },
|
||||
{ "head_dim", RAD_P_INT, 1 },
|
||||
{ "n_head", RAD_P_INT, 1 },
|
||||
{ "n_head_kv", RAD_P_INT, 1 },
|
||||
{ "theta", RAD_P_F64, 1 },
|
||||
{ "eps", RAD_P_F64, 1 },
|
||||
{ "scale", RAD_P_F64, 0 },
|
||||
{ "mode", RAD_P_STR, 0 },
|
||||
{ "rotary_dim", RAD_P_INT, 0 },
|
||||
{ "ladder", RAD_P_STR, 0 },
|
||||
{ "wadd", RAD_P_F64, 0 },
|
||||
{ "v_from_k", RAD_P_INT, 0 },
|
||||
{ "dtype", RAD_P_STR, 1 },
|
||||
{ "v_norm", RAD_P_INT, 0 },
|
||||
{ "block_size", RAD_P_INT, 1 },
|
||||
{ "kv_dtype", RAD_P_STR, 1 },
|
||||
{ "k_scale", RAD_P_F64, 0 },
|
||||
{ "v_scale", RAD_P_F64, 0 },
|
||||
};
|
||||
const RadOperandSpec oQkvNormRopeKv[] = {
|
||||
{ "q", RAD_OPD_INOUT, 0 },
|
||||
{ "k", RAD_OPD_INOUT, 0 },
|
||||
{ "v", RAD_OPD_INOUT, 0 },
|
||||
{ "positions", RAD_OPD_IN, 0 },
|
||||
{ "q_w", RAD_OPD_WEIGHT, 0 },
|
||||
{ "k_w", RAD_OPD_WEIGHT, 0 },
|
||||
{ "slot_mapping", RAD_OPD_IN, 0 },
|
||||
{ "kv_cache", RAD_OPD_INOUT, 0 },
|
||||
};
|
||||
|
||||
|
||||
const RadOpSchema kSchemas[] = {
|
||||
{ "add_rmsnorm", pAddRmsNorm, (int)(sizeof pAddRmsNorm / sizeof pAddRmsNorm[0]), oAddRmsNorm,
|
||||
(int)(sizeof oAddRmsNorm / sizeof oAddRmsNorm[0]),
|
||||
"x = x + a, then y = rmsnorm(x) * (w + wadd) * mult over the x written: a residual add and the "
|
||||
"next norm in one pass, byte-identical to add then rmsnorm." },
|
||||
{ "qkv_norm_rope", pQkvNormRope, (int)(sizeof pQkvNormRope / sizeof pQkvNormRope[0]), oQkvNormRope,
|
||||
(int)(sizeof oQkvNormRope / sizeof oQkvNormRope[0]),
|
||||
"per-head q and k RMS norms, the weightless v norm and the rotary of q and k over separate "
|
||||
"q, k and v projections, in one dispatch; v from the raw k under v_from_k. n_head 0 "
|
||||
"leaves q unread (the k and v heads alone)." },
|
||||
{ "qkv_norm_rope_kv_store", pQkvNormRopeKv, (int)(sizeof pQkvNormRopeKv / sizeof pQkvNormRopeKv[0]),
|
||||
oQkvNormRopeKv, (int)(sizeof oQkvNormRopeKv / sizeof oQkvNormRopeKv[0]),
|
||||
"qkv_norm_rope, then kv_store of the finished k and v heads into the paged cache, in one "
|
||||
"launch." },
|
||||
};
|
||||
|
||||
const RadPluginInfo kInfo = {
|
||||
@@ -553,6 +639,31 @@ const RadPluginInfo kInfo = {
|
||||
"intel_gpu_bmg_g31",
|
||||
};
|
||||
|
||||
/* XE2_NO_FUSED_PROLOGUE=1 leaves the fused attention prologue undeclared, so an architecture takes
|
||||
* its unfused chain: the A/B for its byte-identity, and an escape hatch. */
|
||||
bool hidden(const char* op) {
|
||||
static const bool off = std::getenv("XE2_NO_FUSED_PROLOGUE") != nullptr;
|
||||
return off && (!std::strcmp(op, "qkv_norm_rope") || !std::strcmp(op, "qkv_norm_rope_kv_store"));
|
||||
}
|
||||
const std::vector<const RadKernelInfo*>& kernels() {
|
||||
static const std::vector<const RadKernelInfo*> v = [] {
|
||||
std::vector<const RadKernelInfo*> r;
|
||||
for (const RadKernelInfo& k : kKernels)
|
||||
if (!hidden(k.op)) r.push_back(&k);
|
||||
return r;
|
||||
}();
|
||||
return v;
|
||||
}
|
||||
const std::vector<const RadOpSchema*>& schemas() {
|
||||
static const std::vector<const RadOpSchema*> v = [] {
|
||||
std::vector<const RadOpSchema*> r;
|
||||
for (const RadOpSchema& o : kSchemas)
|
||||
if (!hidden(o.op)) r.push_back(&o);
|
||||
return r;
|
||||
}();
|
||||
return v;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
extern "C" {
|
||||
@@ -563,16 +674,16 @@ __attribute__((visibility("default"))) uint32_t rad_plugin_abi_version(void) {
|
||||
__attribute__((visibility("default"))) uint64_t rad_plugin_abi_layout(void) { return rad_abi_layout(); }
|
||||
__attribute__((visibility("default"))) const RadPluginInfo* rad_plugin_info(void) { return &kInfo; }
|
||||
__attribute__((visibility("default"))) int rad_kernel_schema_count(void) {
|
||||
return (int)(sizeof kSchemas / sizeof kSchemas[0]);
|
||||
return (int)schemas().size();
|
||||
}
|
||||
__attribute__((visibility("default"))) const RadOpSchema* rad_kernel_schema_at(int i) {
|
||||
return i >= 0 && i < rad_kernel_schema_count() ? &kSchemas[i] : nullptr;
|
||||
return i >= 0 && i < rad_kernel_schema_count() ? schemas()[(size_t)i] : nullptr;
|
||||
}
|
||||
__attribute__((visibility("default"))) int rad_kernel_count(void) {
|
||||
return (int)(sizeof kKernels / sizeof kKernels[0]);
|
||||
return (int)kernels().size();
|
||||
}
|
||||
__attribute__((visibility("default"))) const RadKernelInfo* rad_kernel_at(int i) {
|
||||
return i >= 0 && i < rad_kernel_count() ? &kKernels[i] : nullptr;
|
||||
return i >= 0 && i < rad_kernel_count() ? kernels()[(size_t)i] : nullptr;
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
Reference in new issue
Block a user