diff --git a/libxe2/xe2_gemv.cpp b/libxe2/xe2_gemv.cpp index bce8e74b..4f994944 100644 --- a/libxe2/xe2_gemv.cpp +++ b/libxe2/xe2_gemv.cpp @@ -1,10 +1,12 @@ /* xe2_gemv.cpp -- Phase 4's first fast kernel: the skinny bf16 GEMM a decode step is made of * (gemm_nt and logits_gemm at M <= 16), bandwidth-bound by construction. * - * One 16-lane sub-group an output column: each lane reads 8 bf16 of the weight row at a time (one - * 16-byte load, the sub-group 256 contiguous bytes), multiplies them against the same 8 columns of - * every activation row, and keeps one partial sum a row; the sub-group then reduces the partials and - * lane 0 stores. A weight byte is read once per step whatever M is, so the cost is the weight's + * At decode (M = 1) one 16-lane sub-group takes two output columns: each lane reads 8 bf16 of each + * weight row at a time (16-byte loads, the sub-group 256 contiguous bytes a row), multiplies them + * against the same 8 columns of the activation, and keeps a partial sum a column; the sub-group then + * reduces and lane 0 stores. Measured on the B65 at a 1.8k context: one column 9.1 ms a step of + * gemm_nt, two 8.5 (~465 GB/s of the card's ~600), four 10.9 (fewer sub-groups, more registers). + * For 2 <= M <= 16, one column a sub-group with a partial sum a row. A weight byte is read once per step whatever M is, so the cost is the weight's * bytes over the card's bandwidth. * * Not libref's order: each lane sums its own strided slice and the slices are reduced as a tree, so @@ -34,7 +36,8 @@ bool exact_only() { namespace { constexpr int kMaxM = 16; -constexpr size_t kWg = 256; /* 16 sub-groups, 16 output columns a work-group */ +constexpr int kCols = 2; /* output columns a sub-group at decode: two weight rows in flight */ +constexpr size_t kWg = 256; /* 16 sub-groups */ int64_t row_stride(const RadTensor* t, int64_t cols) { if (!t || t->rank < 2) return cols; @@ -61,6 +64,55 @@ bool gemv_ok(const RadTensor* A, const RadTensor* B, const RadTensor* Y, int64_t return rad_ptr_aligned(A->data, 16) && rad_ptr_aligned(B->data, 16); } +/* The kernel: NC output columns a sub-group, up to MM activation rows. */ +template +int launch(sycl::queue& q, const uint16_t* ab, const uint16_t* bb, char* yb, uint32_t ydt, int64_t N, + int64_t K, int64_t ars, int64_t brs, int64_t yrs, int64_t ycs, int mm) { + const int64_t per_wg = 16 * NC; + const size_t groups = (size_t)((N + per_wg - 1) / per_wg); + return submit([&] { + syclex::nd_launch(q, sycl::nd_range<1>{groups * 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 n0 = ((int64_t)it.get_group(0) * 16 + (int64_t)sg.get_group_linear_id()) * NC; + if (n0 >= N) return; /* uniform across the sub-group */ + const int nc = (int)sycl::min((int64_t)NC, N - n0); + const int lane = (int)sg.get_local_linear_id(); + float acc[NC][MM]; + for (int c = 0; c < NC; ++c) + for (int r = 0; r < MM; ++r) acc[c][r] = 0.0f; + for (int64_t k = (int64_t)lane * 8; k < K; k += 8 * kSub) { + float wf[NC][8]; + for (int c = 0; c < NC; ++c) { + if (c >= nc) break; + const sycl::vec w = *(const sycl::vec*)(bb + (n0 + c) * brs + k); + for (int j = 0; j < 8; ++j) wf[c][j] = bf(w[j]); + } + for (int r = 0; r < MM; ++r) { + if (r >= mm) break; + const sycl::vec x = *(const sycl::vec*)(ab + r * ars + k); + float xf[8]; + for (int j = 0; j < 8; ++j) xf[j] = bf(x[j]); + for (int c = 0; c < NC; ++c) { + if (c >= nc) break; + float s = 0.0f; + for (int j = 0; j < 8; ++j) s += xf[j] * wf[c][j]; + acc[c][r] += s; + } + } + } + for (int c = 0; c < NC; ++c) { + if (c >= nc) break; + for (int r = 0; r < MM; ++r) { + if (r >= mm) break; + const float v = sycl::reduce_over_group(sg, acc[c][r], sycl::plus()); + if (lane == 0) rad_store_f32(yb, ydt, r * yrs + (n0 + c) * ycs, v); + } + } + }); + }); +} + int gemv(const RadArgs* a, RadStream st, const char* nkey, const char* kkey, int (*exact)(const RadArgs*, RadStream)) { if (exact_only()) return exact(a, st); if (!a || a->n_t < 3 || !a->t[0].data || !a->t[1].data || !a->t[2].data) return RAD_E_INVAL; @@ -80,36 +132,11 @@ int gemv(const RadArgs* a, RadStream st, const char* nkey, const char* kkey, int const int mm = (int)M; sycl::queue* q = queue_of(st); if (!q) return RAD_E_DEVICE; - const size_t groups = (size_t)((N + 15) / 16); - return submit([&] { - syclex::nd_launch(*q, sycl::nd_range<1>{groups * 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 n = (int64_t)it.get_group(0) * 16 + (int64_t)sg.get_group_linear_id(); - if (n >= N) return; /* uniform across the sub-group */ - const int lane = (int)sg.get_local_linear_id(); - float acc[kMaxM]; - for (int r = 0; r < kMaxM; ++r) acc[r] = 0.0f; - const uint16_t* wrow = bb + n * brs; - for (int64_t k = (int64_t)lane * 8; k < K; k += 8 * kSub) { - const sycl::vec w = *(const sycl::vec*)(wrow + k); - float wf[8]; - for (int j = 0; j < 8; ++j) wf[j] = bf(w[j]); - for (int r = 0; r < kMaxM; ++r) { - if (r >= mm) break; - const sycl::vec x = *(const sycl::vec*)(ab + r * ars + k); - float s = 0.0f; - for (int j = 0; j < 8; ++j) s += bf(x[j]) * wf[j]; - acc[r] += s; - } - } - for (int r = 0; r < kMaxM; ++r) { - if (r >= mm) break; - const float v = sycl::reduce_over_group(sg, acc[r], sycl::plus()); - if (lane == 0) rad_store_f32(yb, ydt, r * yrs + n * ycs, v); - } - }); - }); + /* Decode (M = 1) takes the lean form with kCols columns a sub-group; M up to 16 one column a + * sub-group with a partial sum a row. Sizing the accumulators by the band keeps them in + * registers (four columns at sixteen rows spilled and ran slower than one). */ + if (mm == 1) return launch(*q, ab, bb, yb, ydt, N, K, ars, brs, yrs, ycs, 1); + return launch<1, kMaxM>(*q, ab, bb, yb, ydt, N, K, ars, brs, yrs, ycs, mm); } } // namespace