diff --git a/libxe2/xe2_givens.cpp b/libxe2/xe2_givens.cpp index 2e241f20..a193f2b4 100644 --- a/libxe2/xe2_givens.cpp +++ b/libxe2/xe2_givens.cpp @@ -61,8 +61,12 @@ int givens_dense_quant(const RadArgs* a, RadStream st) { const uint32_t xdt = X->dtype, tdt = T->dtype, ddt = D->dtype, qdt = Q->dtype, sdt = S->dtype; sycl::queue* q = queue_of(st); if (!q) return RAD_E_DEVICE; + /* XE2_GIVENS_BATCHED=1: the prefill's three-pass batched form; XE2_GIVENS_PAIR=1: the float-pair + * form at decode sizes */ + static const bool pair_s = std::getenv("XE2_GIVENS_PAIR") != nullptr; + static const bool batched_s = std::getenv("XE2_GIVENS_BATCHED") != nullptr; if (tdt == RAD_F16 && rad_ptr_aligned(T->data, 16) && rows >= 17 && !exact_only() && - xcs == 1 && rad_tensor_is_contiguous(T)) { + xcs == 1 && rad_tensor_is_contiguous(T) && batched_s) { /* Prefill: the rotation as a batch of GEMMs on XMX, one a (part, group): * y[r][j] = sum_i T[p][gi][j][i] * fp16(x[r][gi*128 + i] / D[p][gi*128 + i]). The * activation rounded once to fp16 (2^-11) where libref sums in double: a fast kernel, held @@ -105,8 +109,7 @@ int givens_dense_quant(const RadArgs* a, RadStream st) { }); }); } - if (tdt == RAD_F16 && rad_ptr_aligned(T->data, 16) && rows <= 16 && !exact_only() && - !std::getenv("XE2_GIVENS_PAIR")) { + if (tdt == RAD_F16 && rad_ptr_aligned(T->data, 16) && !exact_only() && !pair_s && (rows <= 16 || !batched_s)) { /* Decode and the verify (rows <= 16): a (part, group)'s rotation as one small GEMM on * register DPAS (dpas_f16), y[r][j] = sum_i T[j][i] fp16(x[r][i] / D[i]) -- the prefill * form's arithmetic (activation rounded once to fp16, f32 sums), held to the same @@ -115,15 +118,21 @@ int givens_dense_quant(const RadArgs* a, RadStream st) { * output's row of T (32 contiguous bytes a K16 step), so T is read once for all rows; then * libref's quantiser a (row, part, group). The float-pair form (XE2_GIVENS_PAIR=1) walked * the rows in turn on 80 work-groups at n = 5120. */ + /* any row count: a work-group a (part, group, 16-row block), T re-read from the cache for + * each block (the prefill's fp16 copy, strided batch GEMM and quantiser passes, 1.4 ms a + * 941-row call at n = 5120, two parts) */ const int mt = rows <= 8 ? 1 : 2; const int64_t mp = mt * 8; + const int64_t rblk = (rows + 15) / 16; return submit([&] { q->submit([&](sycl::handler& h) { sycl::local_accessor at(sycl::range<1>((size_t)(kG * 16)), h); sycl::local_accessor red(sycl::range<1>((size_t)(8 * 16)), h); - h.parallel_for(sycl::nd_range<1>{(size_t)(P * ngrp) * kG, kG}, + h.parallel_for(sycl::nd_range<1>{(size_t)(P * ngrp * rblk) * kG, kG}, [=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] { - const int64_t gid = (int64_t)it.get_group(0); + const int64_t gid0 = (int64_t)it.get_group(0); + const int64_t rb = gid0 / (P * ngrp), gid = gid0 % (P * ngrp); + const int64_t r0 = rb * 16; const int64_t gi = gid % ngrp, p = gid / ngrp; const int t = (int)it.get_local_id(0); const sycl::sub_group sg = it.get_sub_group(); @@ -131,7 +140,7 @@ int givens_dense_quant(const RadArgs* a, RadStream st) { const int64_t c = gi * kG + t; const float dsc = rad_load_f32(db, ddt, p * n + c); for (int r = 0; r < mp; ++r) { /* at[i][r]: lane k's A column is 16 bytes */ - const float v = r < rows ? rad_load_f32(xb, xdt, r * xrs + c * xcs) / dsc : 0.0f; + const float v = r0 + r < rows ? rad_load_f32(xb, xdt, (r0 + r) * xrs + c * xcs) / dsc : 0.0f; at[t * mp + r] = sycl::bit_cast((sycl::half)v); } sycl::group_barrier(it.get_group()); @@ -161,7 +170,7 @@ int givens_dense_quant(const RadArgs* a, RadStream st) { sycl::group_barrier(it.get_group()); for (int i = 0; i < mt; ++i) for (int m = 0; m < 8; ++m) { - const int64_t r = i * 8 + m; + const int64_t r = r0 + i * 8 + m; if (r >= rows) break; float amax = 0.0f; for (int u = 0; u < 8; ++u) amax = sycl::fmax(amax, red[u * 16 + i * 8 + m]);