Archived
libxe2, arch/common: silu_mul for 4-bit down projections, float exp at the tie rule; head-row norms vectorized; attention SLM by gqa
- Under Geom::plain_norm a down projection that reads the bf16 plane and no codes (w4a16 / w4a8i8 quantise their own activation) takes `silu_mul` instead of gated_quant_fp8 (the E4M3 pass nobody read: 17 us a call, 64 a round). - silu_mul: float exp, libref's double chain again only near a bf16 tie (near_bf16_tie, as the gdn activations): op oracle 300/300 bit-identical at tolerance 0. - rmsnorm's bf16 head rows (n = 128..512): a lane its contiguous slice by 16-byte loads (16 us -> a few us a q / k norm call). rad-kbench rmsnorm 240/240. - the flash kernel's staged queries sized for gqa <= 8 (24 of 64 KiB a work-group). Greedy decode proxy (depth 5, mtp4, draft vocab 131072): 74.3 -> 81.2 tok/s. KL against the exact reference 0.00231 (was 0.00239), top-1 96.70%. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2d2356ad4b
commit
f26eec2633
4 files changed
+63
-7
No files matched your search
@@ -124,6 +124,9 @@ inline int MlpFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, co
|
|||||||
const char* down_base = nm.f("blk.%d.ffn_down", l);
|
const char* down_base = nm.f("blk.%d.ffn_down", l);
|
||||||
RAD_ARCH_TRY(down.probe(b, nm, g, down_base, { src.down }));
|
RAD_ARCH_TRY(down.probe(b, nm, g, down_base, { src.down }));
|
||||||
gq_ffn.fuse_clamped = swiglu_quant;
|
gq_ffn.fuse_clamped = swiglu_quant;
|
||||||
|
/* Under Geom::plain_norm a down projection that reads the bf16 plane and no codes (a 4-bit
|
||||||
|
* one, which quantises its own activation) takes the plain product: `silu_mul`, no E4M3 pass. */
|
||||||
|
if (g.plain_norm && down.codes_free && down.reads_x && swiglu_limit <= 0) gq_ffn.silu_only = true;
|
||||||
RAD_ARCH_TRY(gq_ffn.declare(b, g, w.gate_up, w.ffn, g.n_ff, "silu", 0,
|
RAD_ARCH_TRY(gq_ffn.declare(b, g, w.gate_up, w.ffn, g.n_ff, "silu", 0,
|
||||||
down.reads_x && !(w.ffn.q8_fed && down.probed_i8), false,
|
down.reads_x && !(w.ffn.q8_fed && down.probed_i8), false,
|
||||||
swiglu_limit));
|
swiglu_limit));
|
||||||
|
|||||||
@@ -41,7 +41,9 @@ inline esimd::simd<uint16_t, 128> bf16_of(esimd::simd<float, 128> v) SYCL_ESIMD_
|
|||||||
* (row block, kv head, partition) -- one 8-row block alone is kv_heads work-groups of gqa threads,
|
* (row block, kv head, partition) -- one 8-row block alone is kv_heads work-groups of gqa threads,
|
||||||
* each walking the whole context. A partition's unnormalised state (m, l, O) goes to scratch,
|
* each walking the whole context. A partition's unnormalised state (m, l, O) goes to scratch,
|
||||||
* [row][head][P] of (m, l) in `ml` and of HD floats in `op`, and merge_parts adds them. */
|
* [row][head][P] of (m, l) in `ml` and of HD floats in `op`, and merge_parts adds them. */
|
||||||
template <int HD, bool SPLIT>
|
/* MAXG: the staged queries' local memory, a thread each (16 x 4 KiB was 64 KiB a work-group, two
|
||||||
|
* work-groups an Xe-core) */
|
||||||
|
template <int HD, bool SPLIT, int MAXG = kMaxGqa>
|
||||||
int launch(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs, const uint16_t* cb, int64_t sb,
|
int launch(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs, const uint16_t* cb, int64_t sb,
|
||||||
int64_t sh, int64_t so, const char* btb, uint32_t btdt, int64_t bt_cs, const char* sub, uint32_t sudt,
|
int64_t sh, int64_t so, const char* btb, uint32_t btdt, int64_t bt_cs, const char* sub, uint32_t sudt,
|
||||||
int64_t rows, int64_t kv_heads, int gqa, float scale, int64_t jcap, uint16_t* ob, int64_t o_rs,
|
int64_t rows, int64_t kv_heads, int gqa, float scale, int64_t jcap, uint16_t* ob, int64_t o_rs,
|
||||||
@@ -55,7 +57,7 @@ int launch(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs, const
|
|||||||
q.parallel_for(sycl::nd_range<1>{(size_t)(nqb * kv_heads * NP * gqa), (size_t)gqa}, props,
|
q.parallel_for(sycl::nd_range<1>{(size_t)(nqb * kv_heads * NP * gqa), (size_t)gqa}, props,
|
||||||
[=](sycl::nd_item<1> it) SYCL_ESIMD_KERNEL {
|
[=](sycl::nd_item<1> it) SYCL_ESIMD_KERNEL {
|
||||||
using namespace esimd;
|
using namespace esimd;
|
||||||
slm_init<kMaxGqa * kQBytes>();
|
slm_init<MAXG * kQBytes>();
|
||||||
const int t = (int)it.get_local_id(0);
|
const int t = (int)it.get_local_id(0);
|
||||||
const int64_t wg0 = (int64_t)it.get_group(0);
|
const int64_t wg0 = (int64_t)it.get_group(0);
|
||||||
const int part = (int)(wg0 % NP);
|
const int part = (int)(wg0 % NP);
|
||||||
@@ -222,7 +224,7 @@ int attn_flash_esimd(sycl::queue& q, int64_t head_dim, const uint16_t* qb, int64
|
|||||||
if (!scratch || scratch_bytes < need || !rad_ptr_aligned(scratch, 64)) return RAD_E_UNSUPPORTED;
|
if (!scratch || scratch_bytes < need || !rad_ptr_aligned(scratch, 64)) return RAD_E_UNSUPPORTED;
|
||||||
float* op = (float*)scratch;
|
float* op = (float*)scratch;
|
||||||
float* ml = op + n * 256;
|
float* ml = op + n * 256;
|
||||||
int rc = launch<256, true>(q, qb, q_rs, q_hs, cb, sb, sh, so, btb, btdt, bt_cs, sub, sudt, rows, kv_heads,
|
int rc = (gqa <= 8 ? launch<256, true, 8> : launch<256, true, kMaxGqa>)(q, qb, q_rs, q_hs, cb, sb, sh, so, btb, btdt, bt_cs, sub, sudt, rows, kv_heads,
|
||||||
(int)gqa, scale, jcap, ob, o_rs, o_hs, kMaxP, ml, op);
|
(int)gqa, scale, jcap, ob, o_rs, o_hs, kMaxP, ml, op);
|
||||||
if (rc != RAD_OK) return rc;
|
if (rc != RAD_OK) return rc;
|
||||||
return submit([&] {
|
return submit([&] {
|
||||||
@@ -252,8 +254,8 @@ int attn_flash_esimd(sycl::queue& q, int64_t head_dim, const uint16_t* qb, int64
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return launch<256, false>(q, qb, q_rs, q_hs, cb, sb, sh, so, btb, btdt, bt_cs, sub, sudt, rows, kv_heads, (int)gqa,
|
return (gqa <= 8 ? launch<256, false, 8> : launch<256, false, kMaxGqa>)(q, qb, q_rs, q_hs, cb, sb, sh, so, btb, btdt, bt_cs, sub, sudt, rows, kv_heads, (int)gqa,
|
||||||
scale, jcap, ob, o_rs, o_hs);
|
scale, jcap, ob, o_rs, o_hs, 1, nullptr, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace xe2
|
} // namespace xe2
|
||||||
@@ -289,6 +289,7 @@ int silu_mul(const RadArgs* a, RadStream st) {
|
|||||||
sycl::queue* q = queue_of(st);
|
sycl::queue* q = queue_of(st);
|
||||||
if (!q) return RAD_E_DEVICE;
|
if (!q) return RAD_E_DEVICE;
|
||||||
const int64_t total = rows * n;
|
const int64_t total = rows * n;
|
||||||
|
const bool bf16_out = y.dtype == RAD_BF16 && !exact_only();
|
||||||
const size_t wg = 256, groups = (size_t)((total + 255) / 256);
|
const size_t wg = 256, groups = (size_t)((total + 255) / 256);
|
||||||
return submit([&] {
|
return submit([&] {
|
||||||
syclex::nd_launch(*q, sycl::nd_range<1>{groups * wg, wg}, [=](sycl::nd_item<1> it) {
|
syclex::nd_launch(*q, sycl::nd_range<1>{groups * wg, wg}, [=](sycl::nd_item<1> it) {
|
||||||
@@ -297,8 +298,11 @@ int silu_mul(const RadArgs* a, RadStream st) {
|
|||||||
const int64_t r = g / n, i = g % n;
|
const int64_t r = g / n, i = g % n;
|
||||||
const float x = rad_load_f32(gu.base, gu.dtype, r * gu.pitch + i);
|
const float x = 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 up = rad_load_f32(gu.base, gu.dtype, r * gu.pitch + n + i);
|
||||||
const float sg = 1.0f / (1.0f + (float)sycl::exp((double)-x));
|
/* a bf16 result from a float exp, libref's double chain again only near a rounding
|
||||||
rad_store_f32(y.base, y.dtype, r * y.pitch + i, x * sg * up);
|
* tie (near_bf16_tie): the same bf16 */
|
||||||
|
float v = x * sigmoid_fexp(x) * up;
|
||||||
|
if (!bf16_out || near_bf16_tie(v)) v = x * (1.0f / (1.0f + (float)sycl::exp((double)-x))) * up;
|
||||||
|
rad_store_f32(y.base, y.dtype, r * y.pitch + i, v);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -168,6 +168,53 @@ int rmsnorm(const RadArgs* a, RadStream st) {
|
|||||||
* decode norm's ~40); XE2_EXACT=1 keeps it (below). Rows up to 512 wide (the per-head q / k
|
* decode norm's ~40); XE2_EXACT=1 keeps it (below). Rows up to 512 wide (the per-head q / k
|
||||||
* norms) take a sub-group each, wider ones a work-group. */
|
* norms) take a sub-group each, wider ones a work-group. */
|
||||||
if (!exact_only()) {
|
if (!exact_only()) {
|
||||||
|
/* bf16 head rows (n = 128 .. 512, a multiple of 128): a lane its n / 16 contiguous
|
||||||
|
* elements by 16-byte loads, a sub-group a row (the generic form below issues a dependent
|
||||||
|
* load an element: ~16 us for Qwen3.8's q / k norms at a verify step). */
|
||||||
|
if (n <= 512 && n % 128 == 0 && x.dtype == RAD_BF16 && y.dtype == RAD_BF16 && x.pitch % 8 == 0 &&
|
||||||
|
y.pitch % 8 == 0 && rad_ptr_aligned(x.base, 16) && rad_ptr_aligned(y.base, 16) &&
|
||||||
|
(!g.base || ((g.dtype == RAD_F32 || g.dtype == RAD_BF16) && g.stride == 1))) {
|
||||||
|
using V8 = sycl::vec<uint16_t, 8>;
|
||||||
|
constexpr int64_t kRowsWg = (int64_t)kWg / kSub;
|
||||||
|
const int nv = (int)(n / 128); /* 16-byte vectors a lane: 1 .. 4 */
|
||||||
|
return submit([&] {
|
||||||
|
syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)((rows + kRowsWg - 1) / kRowsWg) * kWg, kWg},
|
||||||
|
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
|
||||||
|
const sycl::sub_group sg = it.get_sub_group();
|
||||||
|
const int64_t r = (int64_t)it.get_group(0) * kRowsWg + (int64_t)sg.get_group_linear_id();
|
||||||
|
if (r >= rows) return; /* uniform over the sub-group */
|
||||||
|
const int64_t l = (int64_t)sg.get_local_linear_id();
|
||||||
|
float v[32];
|
||||||
|
float ss = 0.0f;
|
||||||
|
#pragma unroll
|
||||||
|
for (int u = 0; u < 4; ++u) {
|
||||||
|
if (u >= nv) break;
|
||||||
|
const V8 w = *(const V8*)((const uint16_t*)x.base + r * x.pitch + (l * nv + u) * 8);
|
||||||
|
#pragma unroll
|
||||||
|
for (int j = 0; j < 8; ++j) {
|
||||||
|
v[u * 8 + j] = sycl::bit_cast<float>((uint32_t)w[j] << 16);
|
||||||
|
ss += v[u * 8 + j] * v[u * 8 + j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ss = sycl::reduce_over_group(sg, ss, sycl::plus<float>());
|
||||||
|
const float s = 1.0f / sycl::sqrt(ss / (float)n + eps);
|
||||||
|
#pragma unroll
|
||||||
|
for (int u = 0; u < 4; ++u) {
|
||||||
|
if (u >= nv) break;
|
||||||
|
V8 o;
|
||||||
|
#pragma unroll
|
||||||
|
for (int j = 0; j < 8; ++j) {
|
||||||
|
const int64_t i = (l * nv + u) * 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[u * 8 + j] * s * gw * mult);
|
||||||
|
}
|
||||||
|
*(V8*)((uint16_t*)y.base + r * y.pitch + (l * nv + u) * 8) = o;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
if (n <= 512) {
|
if (n <= 512) {
|
||||||
constexpr int64_t kRowsWg = (int64_t)kWg / kSub;
|
constexpr int64_t kRowsWg = (int64_t)kWg / kSub;
|
||||||
return submit([&] {
|
return submit([&] {
|
||||||
|
|||||||
Reference in new issue
Block a user