Archived
libxe2: qk_norm_rope, conv1d_dw, conv2d, attn_local_rel rows
The encoder towers' ops that had only a host kernel: on the SYCL backend every op runs on the card, so a container carrying Gemma 4's vision tower (qk_norm_rope, blocks mode, v_norm) or its audio conformer (conv2d subsampler, conv1d_dw, attn_local_rel) refused to start. qk_norm_rope also serves GLM's vision tower (axial) and any QK-norm model that declares the fused op. Each is libref's loop, a work-item an output (a (row, head) for qk_norm_rope and attn_local_rel), for passes that run once a picture or clip. cu_seqlens stays on the card, so each work-item finds its own segment. rad-kbench, Gemma 4 E2B container: qk_norm_rope 80/80, conv1d_dw 64/64, conv2d 34/34, attn_local_rel 64/64; default fixture 2144/2144. Gemma 4 E2B now serves on Intel: text and an image request answer correctly. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ff4b7c9fb4
commit
25c8fbd136
4 files changed
+441
No files matched your search
@@ -201,6 +201,153 @@ int rope(const RadArgs* a, RadStream st) {
|
||||
});
|
||||
}
|
||||
|
||||
/* qk_norm_rope: libref's ref_qk_norm_rope. A work-item a (row, head): the head's RMS norm with
|
||||
* libref's ordered sum and its gain (+ wadd; absent: unit), rounded to the tensor's dtype, then the
|
||||
* rotation: neox / gptj / imrope / axial from rope's frequency table (measured by rot), blocks and
|
||||
* gptj_blocks section by section with inv = 1 / pow(theta, j / sec) in double rounded once. With
|
||||
* v_norm the v heads are normed at unit gain and not turned. For the vision towers (once a picture),
|
||||
* so the plain form. */
|
||||
int qk_norm_rope(const RadArgs* a, RadStream st) {
|
||||
if (!a || a->n_t < 4 || !a->t[0].data) return RAD_E_INVAL;
|
||||
const RadTensor *QKV = &a->t[0], *POS = rad_arg_in(a, 1), *QW = rad_arg_in(a, 2), *KW = rad_arg_in(a, 3);
|
||||
const int64_t head_dim = rad_args_geti_or(a, "head_dim", 0);
|
||||
const int64_t n_head = rad_args_geti_or(a, "n_head", 0);
|
||||
const int64_t n_head_kv = rad_args_geti_or(a, "n_head_kv", 0);
|
||||
const float theta = (float)rad_args_getf_or(a, "theta", 10000.0);
|
||||
const float eps = (float)rad_args_getf_or(a, "eps", 1e-6);
|
||||
const float rscale = (float)rad_args_getf_or(a, "scale", 1.0);
|
||||
const float wadd = (float)rad_args_getf_or(a, "wadd", 0.0);
|
||||
const int v_norm = (int)rad_args_geti_or(a, "v_norm", 0);
|
||||
static const char* kM[] = { "neox", "gptj", "blocks", "gptj_blocks", "imrope", "axial" };
|
||||
const char* ms = rad_args_gets_or(a, "mode", "neox");
|
||||
int mode = -1;
|
||||
for (int i = 0; i < 6; ++i)
|
||||
if (!std::strcmp(ms, kM[i])) mode = i;
|
||||
if (mode < 0) return RAD_E_UNSUPPORTED;
|
||||
const bool blocks = mode == 2 || mode == 3, adjacent = mode == 3, im = mode == 4, axial = mode == 5;
|
||||
int64_t rot = rad_args_geti_or(a, "rotary_dim", head_dim);
|
||||
if (head_dim <= 0 || n_head <= 0 || rot < 0 || rot > head_dim || (rot & 1)) return RAD_E_SHAPE;
|
||||
if ((QW && rad_tensor_numel(QW) < head_dim) || (KW && rad_tensor_numel(KW) < head_dim)) return RAD_E_SHAPE;
|
||||
const bool turn = rot > 0;
|
||||
if (turn && !POS) return RAD_E_INVAL;
|
||||
int64_t sec[8] = {}, comp[8] = {};
|
||||
int n_sec = 0;
|
||||
if (turn && (im || axial || blocks)) {
|
||||
n_sec = sections(a, sec);
|
||||
if (n_sec < (im ? 3 : 1)) return RAD_E_SHAPE;
|
||||
}
|
||||
if (turn && blocks) {
|
||||
int64_t total = 0;
|
||||
for (int c = 0; c < n_sec; ++c) { total += sec[c]; comp[c] = c; }
|
||||
if (2 * total != rot) return RAD_E_SHAPE;
|
||||
if (const char* cs = rad_args_gets_or(a, "comps", nullptr)) {
|
||||
int k = 0;
|
||||
for (const char* v = cs; *v && k < 8;) {
|
||||
char* end = nullptr;
|
||||
const long long x = std::strtoll(v, &end, 10);
|
||||
if (end == v) break;
|
||||
comp[k++] = x;
|
||||
v = end;
|
||||
}
|
||||
if (k != n_sec) return RAD_E_SHAPE;
|
||||
}
|
||||
}
|
||||
const int64_t stride_row = (n_head + 2 * n_head_kv) * head_dim;
|
||||
const bool multi = POS && POS->rank >= 2;
|
||||
const int64_t n_comp = multi ? POS->shape[0] : 1;
|
||||
const int64_t M = !POS ? rad_tensor_numel(QKV) / stride_row : multi ? POS->shape[1] : rad_tensor_numel(POS);
|
||||
if (turn) {
|
||||
if (multi && !blocks && !im && !axial) return RAD_E_SHAPE;
|
||||
if (multi && im && n_comp < 3) return RAD_E_SHAPE;
|
||||
if (multi && axial && n_comp < n_sec) return RAD_E_SHAPE;
|
||||
if (n_comp > 8) return RAD_E_SHAPE;
|
||||
for (int c = 0; blocks && c < n_sec; ++c)
|
||||
if (comp[c] < 0 || comp[c] >= n_comp) return RAD_E_SHAPE;
|
||||
}
|
||||
if (M <= 0 || rad_tensor_numel(QKV) < M * stride_row) return RAD_E_SHAPE;
|
||||
const int64_t rs = row_stride(QKV, stride_row);
|
||||
const int64_t ds = QKV->rank ? QKV->stride[QKV->rank - 1] : 1;
|
||||
Freqs f{};
|
||||
if (turn && !blocks) {
|
||||
const int fm = im ? kImrope : axial ? kAxial : mode; /* neox 0, gptj 1 */
|
||||
if (int rc = freqs(fm, rot, rot, theta, sec, (im && !multi) ? 0 : (im || axial ? n_sec : 0), &f); rc < 0)
|
||||
return rc;
|
||||
}
|
||||
const bool gptj_pairs = mode == 1;
|
||||
const int64_t pcs = multi ? POS->stride[0] : 0, pms = multi ? POS->stride[1] : 0;
|
||||
const Lin pl = POS ? Lin::of(POS) : Lin{};
|
||||
char* base = (char*)QKV->data;
|
||||
const uint32_t dt = QKV->dtype;
|
||||
const char* pbase = POS ? (const char*)POS->data : nullptr;
|
||||
const uint32_t pdt = POS ? POS->dtype : 0;
|
||||
const char* qwb = QW ? (const char*)QW->data : nullptr;
|
||||
const char* kwb = KW ? (const char*)KW->data : nullptr;
|
||||
const uint32_t qwdt = QW ? QW->dtype : 0, kwdt = KW ? KW->dtype : 0;
|
||||
const int64_t qws = QW && QW->rank ? QW->stride[QW->rank - 1] : 1, kws = KW && KW->rank ? KW->stride[KW->rank - 1] : 1;
|
||||
const float div = rscale != 0.0f ? rscale : 1.0f;
|
||||
const int64_t heads = n_head + n_head_kv + (v_norm ? n_head_kv : 0);
|
||||
const int64_t total = M * heads;
|
||||
struct Sec { int64_t sec[8], comp[8]; } sc{};
|
||||
for (int c = 0; c < 8; ++c) { sc.sec[c] = sec[c]; sc.comp[c] = comp[c]; }
|
||||
sycl::queue* q = queue_of(st);
|
||||
if (!q) return RAD_E_DEVICE;
|
||||
const size_t wg = 64;
|
||||
const size_t groups = (size_t)((total + (int64_t)wg - 1) / (int64_t)wg);
|
||||
return submit([&] {
|
||||
syclex::nd_launch(*q, sycl::nd_range<1>{groups * wg, wg}, [=](sycl::nd_item<1> it) {
|
||||
const int64_t g = (int64_t)it.get_global_id(0);
|
||||
if (g >= total) return;
|
||||
const int64_t m = g / heads, h = g % heads;
|
||||
const bool vh = h >= n_head + n_head_kv; /* a v head under v_norm */
|
||||
const int64_t hb = m * rs + (vh ? n_head + n_head_kv + (h - n_head - n_head_kv) : h) * head_dim * ds;
|
||||
const char* wb = vh ? nullptr : (h < n_head ? qwb : kwb);
|
||||
const uint32_t wdt = h < n_head ? qwdt : kwdt;
|
||||
const int64_t wst = h < n_head ? qws : kws;
|
||||
float ss = 0.0f;
|
||||
for (int64_t d = 0; d < head_dim; ++d) {
|
||||
const float x = rad_load_f32(base, dt, hb + d * ds);
|
||||
ss += x * x;
|
||||
}
|
||||
const float s = 1.0f / sycl::sqrt(ss / (float)head_dim + eps);
|
||||
for (int64_t d = 0; d < head_dim; ++d) {
|
||||
const float x = rad_load_f32(base, dt, hb + d * ds) * s;
|
||||
rad_store_f32(base, dt, hb + d * ds, wb ? x * (rad_load_f32(wb, wdt, d * wst) + wadd) : x);
|
||||
}
|
||||
if (!turn || vh) return;
|
||||
auto P = [&](int64_t c) {
|
||||
return multi ? pos_at(pbase, pdt, c * pcs + m * pms, div) : pos_at(pbase, pdt, pl.off(m), div);
|
||||
};
|
||||
if (!blocks) {
|
||||
const int64_t half = rot / 2;
|
||||
for (int64_t i = 0; i < half; ++i) {
|
||||
const float ang = P(f.comp[i]) * f.inv[i];
|
||||
const float cs = (float)sycl::cos((double)ang), sn = (float)sycl::sin((double)ang);
|
||||
const int64_t i0 = gptj_pairs ? 2 * i : i, i1 = gptj_pairs ? 2 * i + 1 : i + half;
|
||||
const float x0 = rad_load_f32(base, dt, hb + i0 * ds), x1 = rad_load_f32(base, dt, hb + i1 * ds);
|
||||
rad_store_f32(base, dt, hb + i0 * ds, x0 * cs - x1 * sn);
|
||||
rad_store_f32(base, dt, hb + i1 * ds, x0 * sn + x1 * cs);
|
||||
}
|
||||
return;
|
||||
}
|
||||
int64_t start = 0;
|
||||
for (int c = 0; c < n_sec; ++c) {
|
||||
const float pc = P(sc.comp[c]);
|
||||
for (int64_t j = 0; j < sc.sec[c]; ++j) {
|
||||
const float inv = 1.0f / (float)sycl::pow((double)theta, (double)((float)j / (float)sc.sec[c]));
|
||||
const float ang = pc * inv;
|
||||
const float cs = (float)sycl::cos((double)ang), sn = (float)sycl::sin((double)ang);
|
||||
const int64_t i0 = adjacent ? start + 2 * j : start + j;
|
||||
const int64_t i1 = adjacent ? start + 2 * j + 1 : start + sc.sec[c] + j;
|
||||
const float x0 = rad_load_f32(base, dt, hb + i0 * ds), x1 = rad_load_f32(base, dt, hb + i1 * ds);
|
||||
rad_store_f32(base, dt, hb + i0 * ds, x0 * cs - x1 * sn);
|
||||
rad_store_f32(base, dt, hb + i1 * ds, x0 * sn + x1 * cs);
|
||||
}
|
||||
start += 2 * sc.sec[c];
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/* 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) {
|
||||
|
||||
Reference in new issue
Block a user