diff --git a/libxe2/xe2_draft.cpp b/libxe2/xe2_draft.cpp index 1a88b8b3..65b154a6 100644 --- a/libxe2/xe2_draft.cpp +++ b/libxe2/xe2_draft.cpp @@ -6,19 +6,25 @@ * dflash_select the block drafter's greedy walk over its candidate lattice (ref_quant.cpp): * score[l, c] = unary[l, c] + sum_r pred[pid][r] * hp[l][r] * succ[cand[l, c]][r], * the best candidate (first strict maximum) becoming the next step's predecessor. - * Serial in l by nature, so one work-item a sequence, summing over r in libref's - * order; (pred * hp)[r] is recomputed per term rather than held, the same floats. + * Serial in l by nature, so one sub-group a sequence, a lane a candidate summing + * over r in libref's order (rank <= 1024; else one work-item a sequence). */ #include "xe2_common.h" #include "xe2_ops.h" #include +#include #include namespace xe2 { namespace { constexpr size_t kRowWg = 1024; +constexpr int64_t kSelMaxR = 1024; +constexpr int64_t kTopChunks = 128; /* row_topk's first stage: work-groups a row */ +constexpr size_t kTopWg = 256; +constexpr int64_t kTopMaxR = 64; +constexpr int64_t kTopMaxChunk = 4096; /* keys a chunk in local memory (32 KiB) */ /* dflash_select's sub-group form: R floats a row in local memory */ } /* ------------------------------------------------------------------ row_topk */ @@ -41,6 +47,78 @@ int row_topk(const RadArgs* a, RadStream st) { float* pairs = PAIRS ? (float*)PAIRS->data : nullptr; sycl::queue* q = queue_of(st); if (!q) return RAD_E_DEVICE; + /* A long row in two stages: each of kTopChunks work-groups takes its chunk's top R by the + * same passes, and one work-group a row then takes the top R of those chunk winners. The keys + * are distinct (they carry the column), so the row's top R are among the winners and both + * stages pick exactly what one pass over the row would. One work-group walking 248320 + * columns 32 times was 3.2 ms. */ + const int64_t chunk = (N + kTopChunks - 1) / kTopChunks; + if (N >= 16 * (int64_t)kRowWg && R <= kTopMaxR && chunk <= kTopMaxChunk && + !std::getenv("XE2_TOPK_SERIAL")) { + uint64_t* win = (uint64_t*)workspace(*q, (size_t)(M * kTopChunks * R * 2), 8); + if (!win) return RAD_E_DEVICE; + int rc = submit([&] { + q->submit([&](sycl::handler& h) { + sycl::local_accessor keys(sycl::range<1>((size_t)chunk), h); + h.parallel_for(sycl::nd_range<1>{(size_t)(M * kTopChunks) * kTopWg, kTopWg}, + [=](sycl::nd_item<1> it) { + const int64_t m = (int64_t)it.get_group(0) / kTopChunks, ch = (int64_t)it.get_group(0) % kTopChunks; + const int lane = (int)it.get_local_id(0); + const int64_t i0 = ch * chunk, n = sycl::min(chunk, N - i0); + for (int64_t i = lane; i < n; i += (int64_t)kTopWg) + keys[i] = sel_key(rad_load_f32(x.base, x.dtype, m * x.pitch + i0 + i), i0 + i); + sycl::group_barrier(it.get_group()); + uint64_t prev = ~(uint64_t)0; + for (int64_t r = 0; r < R; ++r) { + uint64_t best = 0; + if (prev != 0) { /* uniform */ + for (int64_t i = lane; i < n; i += (int64_t)kTopWg) + if (keys[i] < prev) best = sycl::max(best, keys[i]); + best = sycl::reduce_over_group(it.get_group(), best, sycl::maximum()); + } + if (lane == 0) win[(m * kTopChunks + ch) * R + r] = best; + prev = best; + } + }); + }); + }); + if (rc < 0) return rc; + const int64_t nw = kTopChunks * R; + return submit([&] { + syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)M * kRowWg, kRowWg}, [=](sycl::nd_item<1> it) { + const int64_t m = (int64_t)it.get_group(0); + const int lane = (int)it.get_local_id(0); + const uint64_t* wk = win + m * nw; + uint64_t prev = ~(uint64_t)0; + bool more = true; + for (int64_t r = 0; r < R; ++r) { + uint64_t best = 0; + if (more) { + for (int64_t i = lane; i < nw; i += (int64_t)kRowWg) + if (wk[i] < prev) best = sycl::max(best, wk[i]); + best = sycl::reduce_over_group(it.get_group(), best, sycl::maximum()); + } + if (lane == 0) { + const bool ok = best != 0; + const int64_t id = ok ? key_index(best) + off : -1; + const float v = ok ? key_value(best) : -INFINITY; + const int64_t io = m * iv.pitch + r; + if (iv.dtype == RAD_I64) ((int64_t*)iv.base)[io] = id; + else ((int32_t*)iv.base)[io] = (int32_t)id; + rad_store_f32(vv.base, vv.dtype, m * vv.pitch + r, v); + if (pairs) { + float* pq = pairs + (m * R + r) * 2; + const int32_t i32 = (int32_t)id; + std::memcpy(pq, &i32, sizeof i32); + pq[1] = v; + } + } + if (best == 0) more = false; + else prev = best; + } + }); + }); + } return submit([&] { syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)M * kRowWg, kRowWg}, [=](sycl::nd_item<1> it) { const int64_t m = (int64_t)it.get_group(0); @@ -114,6 +192,60 @@ int dflash_select(const RadArgs* a, RadStream st) { const Lin anc = Lin::of(ANC); sycl::queue* q = queue_of(st); if (!q) return RAD_E_DEVICE; + if (R <= kSelMaxR && !std::getenv("XE2_SELECT_SERIAL")) { + /* A sub-group a sequence: (pred * hp)[r] and the 16 candidates' succ rows staged in + * local memory by all lanes, lane c then sums candidate c's score over r in order (the + * same chain as below), and lane 0 takes the first strict maximum over the lanes' scores + * in candidate order. The serial form walks 16 rows of 256 one after another. */ + const int64_t RP = R + 1; /* a padded row: lane c reads row c without bank conflicts */ + return submit([&] { + q->submit([&](sycl::handler& h) { + sycl::local_accessor axl(sycl::range<1>((size_t)R), h); + sycl::local_accessor rows(sycl::range<1>((size_t)(kSub * RP)), h); + h.parallel_for(sycl::nd_range<1>{(size_t)M * kSub, (size_t)kSub}, + [=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] { + const sycl::sub_group sg = it.get_sub_group(); + const int64_t m = (int64_t)it.get_group(0); + const int lane = (int)sg.get_local_linear_id(); + int64_t pid = ld_index(anc.dtype, anc.base, anc.off(m * astr)); + for (int64_t l = 0; l < steps; ++l) { + const int64_t r = m * steps + l; + const bool ok = pid >= 0 && pid < nv; + for (int64_t i = lane; i < R; i += kSub) + axl[i] = (ok ? rad_load_f32(pred.base, pred.dtype, pid * pred.pitch + i) : 0.0f) * + (has_hp ? rad_load_f32(hp.base, hp.dtype, r * hp.pitch + i) : 1.0f); + int64_t best_c = 0; + float best_s = -INFINITY; + for (int64_t c0 = 0; c0 < K; c0 += kSub) { + const int64_t c = c0 + lane; + const int64_t tok = c < K ? ld_index(cand.dtype, cand.base, r * cand.pitch + c) : -1; + for (int j = 0; j < kSub; ++j) { + const int64_t tj = sycl::select_from_group(sg, tok, j); + if (tj >= 0 && tj < nv) /* uniform */ + for (int64_t i = lane; i < R; i += kSub) + rows[j * RP + i] = rad_load_f32(succ.base, succ.dtype, tj * succ.pitch + i); + } + sycl::group_barrier(sg); + float sc = c < K ? rad_load_f32(un.base, un.dtype, r * un.pitch + c) : -INFINITY; + if (c < K && tok >= 0 && tok < nv) + for (int64_t i = 0; i < R; ++i) sc += axl[i] * rows[lane * RP + i]; + for (int j = 0; j < kSub; ++j) { + const float sj = sycl::select_from_group(sg, sc, j); + if (c0 + j < K && sj > best_s) { best_s = sj; best_c = c0 + j; } + } + sycl::group_barrier(sg); + } + pid = ld_index(cand.dtype, cand.base, r * cand.pitch + best_c); + if (lane == 0) { + const int64_t oo = m * out.pitch + l; + if (out.dtype == RAD_I64) ((int64_t*)out.base)[oo] = pid; + else ((int32_t*)out.base)[oo] = (int32_t)pid; + } + } + }); + }); + }); + } const size_t wg = 16; const size_t n = (size_t)((M + (int64_t)wg - 1) / (int64_t)wg) * wg; return submit([&] { diff --git a/libxe2/xe2_gemm_zp.cpp b/libxe2/xe2_gemm_zp.cpp index 6b43297a..8946a92f 100644 --- a/libxe2/xe2_gemm_zp.cpp +++ b/libxe2/xe2_gemm_zp.cpp @@ -19,6 +19,7 @@ #include "rad_planes.h" +#include #include namespace xe2 { @@ -287,6 +288,111 @@ int zp_gemv(sycl::queue& q, const int8_t* ab, int64_t ars, const char* sab, uint }); } +/* The same group terms with the integer sums on XMX: an int8 DPAS (8 x 32 by 32 x 16) forms each + * group's sum of a * code exactly in int32 for up to 8 rows and 16 columns at once. The codes + * unpack straight into the packed B operand: four codes along k are one dword, and the step + * order's swap of bits 2 and 3 of k moves whole dwords, so the activation is used as stored. + * A sub-group owns 16 columns and walks the groups in order, each lane adding its column's terms + * as the per-output form does -- libref's chain, at one read of the weight. */ +constexpr int kZpDpasSgs = 16; + +template +inline uint32_t zp_dword(const uint8_t* crow, int64_t d) { /* the 4 codes of logical dword d */ + if constexpr (CBITS == 2) { + const uint32_t b = crow[d]; + return (b & 3u) | ((b >> 2) & 3u) << 8 | ((b >> 4) & 3u) << 16 | ((b >> 6) & 3u) << 24; + } else { + const uint32_t h = (uint32_t)crow[2 * d] | (uint32_t)crow[2 * d + 1] << 8; + return (h & 15u) | ((h >> 4) & 15u) << 8 | ((h >> 8) & 15u) << 16 | ((h >> 12) & 15u) << 24; + } +} + +template +int zp_dpas(sycl::queue& q, const int8_t* ab, int64_t ars, const char* sab, uint32_t sadt, int64_t sa_rs, + const char* sumb, uint32_t sumdt, int64_t asum_s, int64_t mpad, const uint8_t* bb, int64_t brow, + const char* bsb, char* yb, uint32_t ydt, int64_t ys_r, int64_t ys_c, int64_t N, int64_t K, + int64_t group, int mm) { + namespace jm = sycl::ext::oneapi::experimental::matrix; + const int64_t G = K / group; + const size_t wgs = (size_t)((N + 16 * kZpDpasSgs - 1) / (16 * kZpDpasSgs)); + return submit([&] { + q.submit([&](sycl::handler& h) { + sycl::local_accessor As(sycl::range<1>((size_t)(8 * K)), h); + sycl::local_accessor Bs(sycl::range<1>((size_t)kZpDpasSgs * 8 * 16), h); + sycl::local_accessor Cs(sycl::range<1>((size_t)kZpDpasSgs * 8 * 16), h); + h.parallel_for(sycl::nd_range<1>{wgs * kZpDpasSgs * kSub, (size_t)kZpDpasSgs * kSub}, + [=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] { + const sycl::sub_group sg = it.get_sub_group(); + const int s = (int)sg.get_group_linear_id(), lane = (int)sg.get_local_linear_id(); + const int t = (int)it.get_local_id(0); + for (int64_t i = t; i < 2 * K; i += kZpDpasSgs * kSub) { /* 8 rows, 4 bytes at a time */ + const int64_t m = i / (K / 4), c = (i % (K / 4)) * 4; + uint32_t v = 0u; + if (m < mm) v = *(const uint32_t*)(ab + m * ars + c); + *(uint32_t*)&As[m * K + c] = v; + } + sycl::group_barrier(it.get_group()); + const int64_t n0 = ((int64_t)it.get_group(0) * kZpDpasSgs + s) * 16; + if (n0 >= N) return; + const DPlane sc = find_plane(bsb, "scale"), zp = find_plane(bsb, "zero"); + if (!sc.p || !zp.p) return; + const int64_t n = n0 + lane; + const bool live = n < N; + const uint8_t* crow = bb + (live ? n : n0) * brow; + float sa[8], acc[8]; + for (int m = 0; m < 8; ++m) { + sa[m] = m < mm ? rad_load_f32(sab, sadt, m * sa_rs) : 0.0f; + acc[m] = 0.0f; + } + uint32_t* bt = &Bs[s * 8 * 16]; + int32_t* ct = &Cs[s * 8 * 16]; + auto lb = sycl::address_space_cast((int8_t*)bt); + auto lc = sycl::address_space_cast(ct); + jm::joint_matrix ma; + jm::joint_matrix mb; + jm::joint_matrix mc; + for (int64_t g = 0; g < G; ++g) { + jm::joint_matrix_fill(sg, mc, 0); + for (int64_t k0 = g * group; k0 < (g + 1) * group; k0 += 32) { + /* stored dword r of this step holds logical dword (r & ~3) | swap(r & 3) */ + for (int r = 0; r < 8; ++r) { + const int ld = (r & ~3) | ((r & 1) << 1) | ((r & 2) >> 1); + bt[r * 16 + lane] = zp_dword(crow, k0 / 4 + ld); + } + sycl::group_barrier(sg); + auto la = sycl::address_space_cast(&As[k0]); + jm::joint_matrix_load(sg, ma, la, (size_t)K); + jm::joint_matrix_load(sg, mb, lb, 64); + jm::joint_matrix_mad(sg, mc, ma, mb, mc); + sycl::group_barrier(sg); + } + jm::joint_matrix_store(sg, mc, lc, 16, jm::layout::row_major); + sycl::group_barrier(sg); + if (live) { + const float sb = sc.at(n, g); + const float z = (float)(int32_t)zp.at(n, g); + for (int m = 0; m < 8; ++m) { + if (m >= mm) break; + const float sub = (float)ct[m * 16 + lane] - z * rad_load_f32(sumb, sumdt, (g * mpad + m) * asum_s); + const float term = sub * sa[m] * sb; + acc[m] += term; + } + } + sycl::group_barrier(sg); + } + if (live) + for (int m = 0; m < 8; ++m) { + if (m >= mm) break; + rad_store_f32(yb, ydt, m * ys_r + n * ys_c, acc[m]); + } + }); + }); + }); +} + } // namespace int gemm_nt_q_zp_gemv(const RadArgs* a, RadStream st) { @@ -321,6 +427,14 @@ int gemm_nt_q_zp_gemv(const RadArgs* a, RadStream st) { const int64_t sa_rs = row_stride(AS, 1); const int64_t asum_s = ASUM->rank ? ASUM->stride[ASUM->rank - 1] : 1; const int64_t ys_r = row_stride(Y, N), ys_c = Y->rank ? Y->stride[Y->rank - 1] : 1; + if (M <= 8 && (cbits == 2 || cbits == 4) && group % 32 == 0 && K * 8 <= 64 * 1024 && ars % 4 == 0 && + !std::getenv("XE2_ZP_GEMV")) { + auto dp = cbits == 2 ? zp_dpas<2> : zp_dpas<4>; + int rc = dp(*q, (const int8_t*)A->data, ars, (const char*)AS->data, AS->dtype, sa_rs, + (const char*)ASUM->data, ASUM->dtype, asum_s, mpad, (const uint8_t*)B->data, brow, + (const char*)BS->data, (char*)Y->data, Y->dtype, ys_r, ys_c, N, K, group, (int)M); + return rc; + } auto go = [&](auto tag) { constexpr int MM = decltype(tag)::value; return zp_gemv(*q, (const int8_t*)A->data, ars, (const char*)AS->data, AS->dtype, sa_rs,