libxe2: the norm and the fp8 quantisers 3-4x faster at decode, still bit-identical to libref

At one row a work-group, rmsnorm_quant_fp8 took 108 us and gated_quant_fp8 84 us a call (26% and 10%
of a decode step at a 1.8k context). The ordered sum of squares now reads its squares eight at a
time and adds them in the same order; quant_group computes a group's values once into registers and
writes out_bf16 itself; gated_quant_fp8 spreads a row's independent groups over ceil(groups/16)
work-groups. rad-kbench: 208/208 and 206/206 at rel_l2 0.

scripts/sycl/profile_decode.sh isolates decode steps' per-op device time (two profiled runs of the
same request, 1 and 1 + STEPS tokens, differenced). Decode at 1.8k context: 34.8 -> 26.6 ms of
device time a step; the two ops 9.1 -> 3.7 and 3.5 -> 0.9 ms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1
This commit is contained in:
pascallinxweilerandClaude Opus 5.5 committed 2026-10-23 20:47:13 +02:00
1 parent 880c218375
commit f5614a03bb
3 files changed
+131 -31

No files matched your search

+61 -31
View File
@@ -55,30 +55,64 @@ 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)
for (int64_t i = 0; i < cn; ++i) ss += sq[i];
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. */
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 < cn; ++i) ss += sq[i];
}
sycl::group_barrier(grp);
}
return sycl::group_broadcast(grp, ss, 0);
}
/* One group's codes and scale, by the sub-group `sg` over values v(k0 + i), i < len.
* Returns the scale (the same in every lane). */
/* One group's codes and scale, by the sub-group `sg` over values v(k0 + i), i < len; with
* `obase`, each value is also stored there (out_bf16). A group of up to kCached * 16 values is
* computed once into registers, so the amax pass and the codes read the same floats without
* evaluating v twice (for gated_quant_fp8 that is a double exp an element); a wider group
* evaluates v in each pass, which gives the same floats. Returns the scale (the same in every
* lane). */
constexpr int kCached = 8;
template <class V>
inline float quant_group(const sycl::sub_group& sg, const V& v, int64_t k0, int64_t len, float top,
char* qbase, uint32_t qdt, int64_t qoff) {
char* qbase, uint32_t qdt, int64_t qoff, char* obase = nullptr,
uint32_t odt = 0, int64_t ooff = 0) {
const int lane = (int)sg.get_local_linear_id();
auto code = [&](int64_t i, float x, float inv) {
float c = x * inv;
if (c > top) c = top;
if (c < -top) c = -top;
rad_store_f32(qbase, qdt, qoff + k0 + i, c);
if (obase) rad_store_f32(obase, odt, ooff + k0 + i, x);
};
float amax = 0.0f;
if (len <= (int64_t)kCached * kSub) {
float c[kCached];
for (int j = 0; j < kCached; ++j) {
const int64_t i = lane + (int64_t)j * kSub;
c[j] = i < len ? v(k0 + i) : 0.0f;
amax = sycl::fmax(amax, sycl::fabs(c[j]));
}
amax = sycl::reduce_over_group(sg, amax, sycl::maximum<float>());
const float sc = amax > 0.0f ? amax * (1.0f / top) : 1.0f;
const float inv = 1.0f / sc;
for (int j = 0; j < kCached; ++j) {
const int64_t i = lane + (int64_t)j * kSub;
if (i < len) code(i, c[j], inv);
}
return sc;
}
for (int64_t i = lane; i < len; i += kSub) amax = sycl::fmax(amax, sycl::fabs(v(k0 + i)));
amax = sycl::reduce_over_group(sg, amax, sycl::maximum<float>());
const float sc = amax > 0.0f ? amax * (1.0f / top) : 1.0f;
const float inv = 1.0f / sc;
for (int64_t i = lane; i < len; i += kSub) {
float c = v(k0 + i) * inv;
if (c > top) c = top;
if (c < -top) c = -top;
rad_store_f32(qbase, qdt, qoff + k0 + i, c);
}
for (int64_t i = lane; i < len; i += kSub) code(i, v(k0 + i), inv);
return sc;
}
@@ -237,14 +271,12 @@ int rmsnorm_quant_fp8(const RadArgs* a, RadStream st) {
const float w = rad_load_f32(gw.base, gw.dtype, i * gw.stride) + wadd;
return rad_bf16_to_f32(rad_f32_to_bf16(src(i) * rs * w));
};
if (has_ob)
for (int64_t i = t; i < n; i += kWg)
rad_store_f32(ob.base, ob.dtype, r * ob.pitch + i, v(i));
const sycl::sub_group sg = it.get_sub_group();
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);
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);
}
@@ -280,29 +312,27 @@ int gated_quant_fp8(const RadArgs* a, RadStream st) {
const float top = Q->dtype == RAD_I8 ? 127.0f : 448.0f;
sycl::queue* q = queue_of(st);
if (!q) return RAD_E_DEVICE;
/* The groups are independent, so a row spreads over ceil(ngrp / 16) work-groups, one group a
* sub-group: at decode (one row of 48 groups) three work-groups share the row, not one. */
constexpr int kSubs = (int)(kWg / kSub);
const int64_t per_row = (ngrp + kSubs - 1) / kSubs;
return submit([&] {
syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)rows * kWg, kWg},
syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)(rows * per_row) * 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);
const int64_t r = (int64_t)it.get_group(0) / per_row;
const sycl::sub_group sg = it.get_sub_group();
const int64_t gi = ((int64_t)it.get_group(0) % per_row) * kSubs + (int64_t)sg.get_group_linear_id();
if (gi >= ngrp) return; /* uniform across the sub-group */
auto v = [&](int64_t i) {
const float gt = rad_load_f32(gu.base, gu.dtype, r * gu.pitch + i);
const float up = rad_load_f32(gu.base, gu.dtype, r * gu.pitch + n + i);
const float sg = sigmoidf(gt);
return rad_bf16_to_f32(rad_f32_to_bf16((act == 0 ? gt * sg : sg) * up));
const float sgm = sigmoidf(gt);
return rad_bf16_to_f32(rad_f32_to_bf16((act == 0 ? gt * sgm : sgm) * up));
};
if (has_ob)
for (int64_t i = t; i < n; i += kWg)
rad_store_f32(ob.base, ob.dtype, r * ob.pitch + i, v(i));
const sycl::sub_group sg = it.get_sub_group();
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);
if (sg.get_local_linear_id() == 0)
rad_store_f32(sv.base, sv.dtype, r * sv.pitch + gi, sc);
}
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);
});
});
}