Archived
libxe2: add_rmsnorm (schema and row); arch/common: the plain norm and the attention gate without unread work
- libxe2 serves `add_rmsnorm` (device-only; libxe2 now publishes its schema, libr4d's spelling): x += a then rmsnorm of the x written in one pass, at rmsnorm's own bf16 partition, so both outputs are byte-identical to the pair here. NormQuantFP8's plain form takes it where it resolves (one launch instead of `add` + `rmsnorm`, ~150 a round). - AttnGatedFP8: under the plain norm an out projection that reads no codes skips the quantiser after the gate's sigmoid + mul. Greedy output 8/8 identical to the previous commit; decode proxy 81.2 -> 82.5 tok/s; declared ops 2046 -> 1902. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f26eec2633
commit
4511822138
5 files changed
+148
-4
No files matched your search
@@ -403,6 +403,95 @@ int quant_act_i8g(const RadArgs* a, RadStream st) {
|
||||
return quant_act_groups(a, st, 127.0f, "quant_act_i8g");
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ add_rmsnorm */
|
||||
/* x += a (bf16, rounded as `add` rounds it), then y = rmsnorm(x) over the x written, at rmsnorm's
|
||||
* own partition (the bf16 rows above: a work-item eight elements, the same per-item order and the
|
||||
* same group reduction), so both outputs are byte-identical to `add` then `rmsnorm` here. Other
|
||||
* shapes or dtypes: the two kernels in order. */
|
||||
int add_rmsnorm(const RadArgs* a, RadStream st) {
|
||||
if (!a || a->n_t < 4 || !a->t[0].data || !a->t[1].data || !a->t[3].data) return RAD_E_INVAL;
|
||||
const RadTensor* w = rad_arg_in(a, 2);
|
||||
const float wadd = (float)rad_args_getf_or(a, "wadd", 0.0);
|
||||
if (!w && wadd != 0.0f) return RAD_E_INVAL;
|
||||
const int64_t n = rad_args_geti_or(a, "n", w && w->rank ? w->shape[w->rank - 1] : 0);
|
||||
const float eps = (float)rad_args_getf_or(a, "eps", 1e-6);
|
||||
const float mult = (float)rad_args_getf_or(a, "mult", 1.0);
|
||||
if (n <= 0) return RAD_E_SHAPE;
|
||||
Rows x, ad, y;
|
||||
Gain g;
|
||||
if (int rc = rows_view(&a->t[0], n, &x, "add_rmsnorm x"); rc < 0) return rc;
|
||||
if (int rc = rows_view(&a->t[1], n, &ad, "add_rmsnorm a"); rc < 0) return rc;
|
||||
if (int rc = rows_view(&a->t[3], n, &y, "add_rmsnorm y"); rc < 0) return rc;
|
||||
if (int rc = gain_view(w, n, &g, "add_rmsnorm w"); rc < 0) return rc;
|
||||
const int64_t rows = x.rows;
|
||||
if (rows <= 0 || y.rows < rows || ad.rows < rows) return RAD_E_SHAPE;
|
||||
sycl::queue* q = queue_of(st);
|
||||
if (!q) return RAD_E_DEVICE;
|
||||
const int64_t nv = n / 8, wgv = (nv + kSub - 1) / kSub * kSub;
|
||||
const bool fast = !exact_only() && x.dtype == RAD_BF16 && ad.dtype == RAD_BF16 && y.dtype == RAD_BF16 &&
|
||||
n % 8 == 0 && n > 512 && wgv <= 1024 && x.pitch % 8 == 0 && ad.pitch % 8 == 0 &&
|
||||
y.pitch % 8 == 0 && rad_ptr_aligned(x.base, 16) && rad_ptr_aligned(ad.base, 16) &&
|
||||
rad_ptr_aligned(y.base, 16) &&
|
||||
(!g.base || ((g.dtype == RAD_F32 || g.dtype == RAD_BF16) && g.stride == 1));
|
||||
if (!fast) {
|
||||
/* the pair: x += a elementwise, then rmsnorm's own launch over x */
|
||||
int rc = submit([&] {
|
||||
syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)rows * kWg, kWg}, [=](sycl::nd_item<1> it) {
|
||||
const int64_t r = (int64_t)it.get_group(0);
|
||||
for (int64_t i = (int64_t)it.get_local_id(0); i < n; i += (int64_t)kWg) {
|
||||
const float v = rad_load_f32(x.base, x.dtype, r * x.pitch + i) +
|
||||
rad_load_f32(ad.base, ad.dtype, r * ad.pitch + i);
|
||||
rad_store_f32(x.base, x.dtype, r * x.pitch + i, v);
|
||||
}
|
||||
});
|
||||
});
|
||||
if (rc != RAD_OK) return rc;
|
||||
RadArgs b2 = *a;
|
||||
const RadTensor tt[3] = { a->t[0], a->t[2], a->t[3] };
|
||||
b2.t = tt;
|
||||
b2.n_t = 3;
|
||||
return rmsnorm(&b2, st);
|
||||
}
|
||||
using V8 = sycl::vec<uint16_t, 8>;
|
||||
return submit([&] {
|
||||
syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)(rows * wgv), (size_t)wgv},
|
||||
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
|
||||
const int64_t r = (int64_t)it.get_group(0);
|
||||
const int64_t t = (int64_t)it.get_local_id(0);
|
||||
const bool live = t < nv;
|
||||
float v[8];
|
||||
float ss = 0.0f;
|
||||
if (live) {
|
||||
uint16_t* xr = (uint16_t*)x.base + r * x.pitch + t * 8;
|
||||
const V8 u = *(const V8*)xr;
|
||||
const V8 d = *(const V8*)((const uint16_t*)ad.base + r * ad.pitch + t * 8);
|
||||
V8 o;
|
||||
#pragma unroll
|
||||
for (int j = 0; j < 8; ++j) {
|
||||
const float sum = sycl::bit_cast<float>((uint32_t)u[j] << 16) + sycl::bit_cast<float>((uint32_t)d[j] << 16);
|
||||
o[j] = rad_f32_to_bf16(sum);
|
||||
v[j] = sycl::bit_cast<float>((uint32_t)o[j] << 16);
|
||||
ss += v[j] * v[j];
|
||||
}
|
||||
*(V8*)xr = o;
|
||||
}
|
||||
ss = sycl::reduce_over_group(it.get_group(), ss, sycl::plus<float>());
|
||||
if (!live) return;
|
||||
const float s = 1.0f / sycl::sqrt(ss / (float)n + eps);
|
||||
V8 o;
|
||||
#pragma unroll
|
||||
for (int j = 0; j < 8; ++j) {
|
||||
const int64_t i = t * 8 + j;
|
||||
const float gw = !g.base ? 1.0f
|
||||
: (g.dtype == RAD_F32 ? ((const float*)g.base)[i]
|
||||
: sycl::bit_cast<float>((uint32_t)((const uint16_t*)g.base)[i] << 16)) + wadd;
|
||||
o[j] = rad_f32_to_bf16(v[j] * s * gw * mult);
|
||||
}
|
||||
*(V8*)((uint16_t*)y.base + r * y.pitch + t * 8) = o;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ rmsnorm_quant_fp8 */
|
||||
int rmsnorm_quant_fp8(const RadArgs* a, RadStream st) {
|
||||
if (!a || a->n_t < 5) return RAD_E_INVAL;
|
||||
|
||||
Reference in new issue
Block a user