diff --git a/arch/common/rad_block_mlp_fp8.h b/arch/common/rad_block_mlp_fp8.h index 82ad42a9..d0dce54d 100644 --- a/arch/common/rad_block_mlp_fp8.h +++ b/arch/common/rad_block_mlp_fp8.h @@ -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); RAD_ARCH_TRY(down.probe(b, nm, g, down_base, { src.down })); 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, down.reads_x && !(w.ffn.q8_fed && down.probed_i8), false, swiglu_limit)); diff --git a/libxe2/xe2_attn_flash_esimd.cpp b/libxe2/xe2_attn_flash_esimd.cpp index 980cf1be..1e3280b2 100644 --- a/libxe2/xe2_attn_flash_esimd.cpp +++ b/libxe2/xe2_attn_flash_esimd.cpp @@ -41,7 +41,9 @@ inline esimd::simd bf16_of(esimd::simd v) SYCL_ESIMD_ * (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, * [row][head][P] of (m, l) in `ml` and of HD floats in `op`, and merge_parts adds them. */ -template +/* 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 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 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, [=](sycl::nd_item<1> it) SYCL_ESIMD_KERNEL { using namespace esimd; - slm_init(); + slm_init(); const int t = (int)it.get_local_id(0); const int64_t wg0 = (int64_t)it.get_group(0); 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; float* op = (float*)scratch; 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); if (rc != RAD_OK) return rc; 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, - scale, jcap, ob, o_rs, o_hs); + 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, 1, nullptr, nullptr); } } // namespace xe2 diff --git a/libxe2/xe2_gemm_q.cpp b/libxe2/xe2_gemm_q.cpp index 8a94d951..4eb6c1d3 100644 --- a/libxe2/xe2_gemm_q.cpp +++ b/libxe2/xe2_gemm_q.cpp @@ -289,6 +289,7 @@ int silu_mul(const RadArgs* a, RadStream st) { sycl::queue* q = queue_of(st); if (!q) return RAD_E_DEVICE; 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); return submit([&] { 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 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 sg = 1.0f / (1.0f + (float)sycl::exp((double)-x)); - rad_store_f32(y.base, y.dtype, r * y.pitch + i, x * sg * up); + /* a bf16 result from a float exp, libref's double chain again only near a rounding + * 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); }); }); } diff --git a/libxe2/xe2_norm.cpp b/libxe2/xe2_norm.cpp index 036ebe8b..e60d7a43 100644 --- a/libxe2/xe2_norm.cpp +++ b/libxe2/xe2_norm.cpp @@ -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 * norms) take a sub-group each, wider ones a work-group. */ 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; + 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((uint32_t)w[j] << 16); + ss += v[u * 8 + j] * v[u * 8 + j]; + } + } + ss = sycl::reduce_over_group(sg, ss, sycl::plus()); + 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((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) { constexpr int64_t kRowsWg = (int64_t)kWg / kSub; return submit([&] {