Archived
libxe2: silu_mul, add, mul and sigmoid eight bf16 values a work-item; sigmoid's float exp
The scalar elementwise kernels spent their prefill time in int64 divides and generic accessors (silu_mul ~0.7 ms a call). bf16 rows now take a 2-D range of eight columns a work-item with 16-byte loads and stores, the same per-element arithmetic. sigmoid with a bf16 output takes the float exp, libref's double chain again only near a bf16 tie (as silu does). kbench add/mul/sigmoid bit-exact; greedy 8/8 and the 2234-token prompt's continuation identical. 2234-token prompt 960 -> 1001 tok/s; decode proxy 82.5 -> 83.1. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e6f4862c7e
commit
531fda7313
2 files changed
+82
-1
No files matched your search
@@ -38,6 +38,31 @@ int elementwise2(const RadArgs* a, RadStream s, bool mul) {
|
||||
sycl::queue* q = queue_of(s);
|
||||
if (!q) return RAD_E_DEVICE;
|
||||
const int64_t total = rows * n;
|
||||
/* bf16 rows, 8-aligned, no divisor: a work-item eight columns of a row, the same sums */
|
||||
if (div == 1.0f && ya.dtype == RAD_BF16 && aa.dtype == RAD_BF16 && ba.dtype == RAD_BF16 && n % 8 == 0 &&
|
||||
ya.pitch % 8 == 0 && aa.pitch % 8 == 0 && ba.pitch % 8 == 0 && rad_ptr_aligned(ya.base, 16) &&
|
||||
rad_ptr_aligned(aa.base, 16) && rad_ptr_aligned(ba.base, 16) && n / 8 < (int64_t)1 << 30) {
|
||||
using V8 = sycl::vec<uint16_t, 8>;
|
||||
const int n8 = (int)(n / 8);
|
||||
const size_t wg8 = 128;
|
||||
return submit([&] {
|
||||
syclex::nd_launch(*q, sycl::nd_range<2>{{(size_t)rows, (size_t)((n8 + wg8 - 1) / wg8 * wg8)}, {1, wg8}},
|
||||
[=](sycl::nd_item<2> it) {
|
||||
const int c8 = (int)it.get_global_id(1);
|
||||
if (c8 >= n8) return;
|
||||
const int64_t r = (int64_t)it.get_global_id(0);
|
||||
const V8 u = *(const V8*)((const uint16_t*)aa.base + r * aa.pitch + (int64_t)c8 * 8);
|
||||
const V8 v = *(const V8*)((const uint16_t*)ba.base + r * ba.pitch + (int64_t)c8 * 8);
|
||||
V8 o;
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 8; ++e) {
|
||||
const float p = sycl::bit_cast<float>((uint32_t)u[e] << 16), w = sycl::bit_cast<float>((uint32_t)v[e] << 16);
|
||||
o[e] = rad_f32_to_bf16(mul ? p * w : p + w);
|
||||
}
|
||||
*(V8*)((uint16_t*)ya.base + r * ya.pitch + (int64_t)c8 * 8) = o;
|
||||
});
|
||||
});
|
||||
}
|
||||
const size_t wg = 256;
|
||||
const size_t groups = (size_t)((total + (int64_t)wg - 1) / (int64_t)wg);
|
||||
return submit([&] {
|
||||
@@ -67,6 +92,31 @@ int sigmoid(const RadArgs* a, RadStream s) {
|
||||
const Lin x = Lin::of(X), y = Lin::of(Y);
|
||||
sycl::queue* q = queue_of(s);
|
||||
if (!q) return RAD_E_DEVICE;
|
||||
/* a bf16 result from a float exp, libref's double chain again only near a rounding tie
|
||||
* (near_bf16_tie): the same bf16 at a fraction of the double exp's cost */
|
||||
const bool fexp = y.dtype == RAD_BF16 && !exact_only();
|
||||
if (fexp && x.dtype == RAD_BF16 && x.contig && y.contig && nel % 8 == 0 && rad_ptr_aligned(x.base, 16) &&
|
||||
rad_ptr_aligned(y.base, 16)) {
|
||||
using V8 = sycl::vec<uint16_t, 8>;
|
||||
const int64_t n8 = nel / 8;
|
||||
const size_t wg = 128, groups = (size_t)((n8 + (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 i = (int64_t)it.get_global_id(0);
|
||||
if (i >= n8) return;
|
||||
const V8 u = ((const V8*)x.base)[i];
|
||||
V8 o;
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 8; ++e) {
|
||||
const float v = sycl::bit_cast<float>((uint32_t)u[e] << 16);
|
||||
float r = sigmoid_fexp(v);
|
||||
if (near_bf16_tie(r)) r = 1.0f / (1.0f + (float)sycl::exp((double)-v));
|
||||
o[e] = rad_f32_to_bf16(r);
|
||||
}
|
||||
((V8*)y.base)[i] = o;
|
||||
});
|
||||
});
|
||||
}
|
||||
const size_t wg = 256;
|
||||
const size_t groups = (size_t)((nel + (int64_t)wg - 1) / (int64_t)wg);
|
||||
return submit([&] {
|
||||
@@ -74,7 +124,9 @@ int sigmoid(const RadArgs* a, RadStream s) {
|
||||
const int64_t i = (int64_t)it.get_global_id(0);
|
||||
if (i >= nel) return;
|
||||
const float v = rad_load_f32(x.base, x.dtype, x.off(i));
|
||||
rad_store_f32(y.base, y.dtype, y.off(i), 1.0f / (1.0f + (float)sycl::exp((double)-v)));
|
||||
float r = fexp ? sigmoid_fexp(v) : 0.0f;
|
||||
if (!fexp || near_bf16_tie(r)) r = 1.0f / (1.0f + (float)sycl::exp((double)-v));
|
||||
rad_store_f32(y.base, y.dtype, y.off(i), r);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -290,6 +290,35 @@ int silu_mul(const RadArgs* a, RadStream st) {
|
||||
if (!q) return RAD_E_DEVICE;
|
||||
const int64_t total = rows * n;
|
||||
const bool bf16_out = y.dtype == RAD_BF16 && !exact_only();
|
||||
/* bf16 rows, 8-aligned: a work-item eight columns of a row, 16-byte loads and store, the
|
||||
* same per-element arithmetic (the scalar form's int64 divide a value bounded it at ~0.7 ms
|
||||
* a prefill call) */
|
||||
if (bf16_out && gu.dtype == RAD_BF16 && n % 8 == 0 && gu.pitch % 8 == 0 && y.pitch % 8 == 0 &&
|
||||
rad_ptr_aligned(gu.base, 16) && rad_ptr_aligned(y.base, 16) && n / 8 < (int64_t)1 << 30) {
|
||||
using V8 = sycl::vec<uint16_t, 8>;
|
||||
const int n8 = (int)(n / 8);
|
||||
const size_t wg8 = 128;
|
||||
return submit([&] {
|
||||
syclex::nd_launch(*q, sycl::nd_range<2>{{(size_t)rows, (size_t)((n8 + wg8 - 1) / wg8 * wg8)}, {1, wg8}},
|
||||
[=](sycl::nd_item<2> it) {
|
||||
const int c8 = (int)it.get_global_id(1);
|
||||
if (c8 >= n8) return;
|
||||
const int64_t r = (int64_t)it.get_global_id(0);
|
||||
const uint16_t* gp = (const uint16_t*)gu.base + r * gu.pitch + (int64_t)c8 * 8;
|
||||
const V8 xg = *(const V8*)gp, xu = *(const V8*)(gp + n);
|
||||
V8 o;
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 8; ++e) {
|
||||
const float x = sycl::bit_cast<float>((uint32_t)xg[e] << 16);
|
||||
const float up = sycl::bit_cast<float>((uint32_t)xu[e] << 16);
|
||||
float v = x * sigmoid_fexp(x) * up;
|
||||
if (near_bf16_tie(v)) v = x * (1.0f / (1.0f + (float)sycl::exp((double)-x))) * up;
|
||||
o[e] = rad_f32_to_bf16(v);
|
||||
}
|
||||
*(V8*)((uint16_t*)y.base + r * y.pitch + (int64_t)c8 * 8) = o;
|
||||
});
|
||||
});
|
||||
}
|
||||
const size_t wg = 256, groups = (size_t)((total + 255) / 256);
|
||||
return submit([&] {
|
||||
syclex::nd_launch(*q, sycl::nd_range<1>{groups * wg, wg}, [=](sycl::nd_item<1> it) {
|
||||
|
||||
Reference in new issue
Block a user