diff --git a/libxe2/xe2_draft.cpp b/libxe2/xe2_draft.cpp index 65b154a6..3b229889 100644 --- a/libxe2/xe2_draft.cpp +++ b/libxe2/xe2_draft.cpp @@ -20,11 +20,12 @@ namespace xe2 { namespace { constexpr size_t kRowWg = 1024; -constexpr int64_t kSelMaxR = 1024; +constexpr int64_t kSelMaxR = 1024; /* dflash_select's sub-group form: R floats a row in local memory */ 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 */ +constexpr int64_t kTopMaxChunk = 4096; /* keys a chunk in local memory (32 KiB) */ +constexpr int64_t kSurvMax = 4096; /* row_topk's filtered form: survivors in local memory */ } /* ------------------------------------------------------------------ row_topk */ @@ -52,6 +53,147 @@ int row_topk(const RadArgs* a, RadStream st) { * 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. */ + auto store_out = [=](int64_t m, int64_t r, uint64_t best) { + 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; + } + }; + /* A long row filtered, one work-group a row: the 32nd (R-th) largest of the work-group's 64 + * sub-group maxima is a lower bound on the row's R-th largest key (those R maxima are R + * distinct keys at or above it), so the row's top R are among the keys at or above it -- a + * few dozen on real logits. A second read of the row gathers them into local memory, and each + * one's place is the count of survivors above it: the keys are distinct, so that is exactly + * the order the passes below produce. More than kSurvMax survivors (a row of equal values) + * takes those passes over the row instead. The two-stage form was 0.87 ms at M = 15, + * N = 248320, R = 32 (R work-group reductions over every chunk). */ + if (N >= 16 * (int64_t)kRowWg && R <= kTopMaxR && N < ((int64_t)1 << 31) && !std::getenv("XE2_TOPK_CHUNKS") && + !std::getenv("XE2_TOPK_SERIAL")) { + constexpr int kSgs = (int)(kRowWg / kSub); + return submit([&] { + q->submit([&](sycl::handler& h) { + sycl::local_accessor smax(sycl::range<1>((size_t)kSgs + 1), h); + sycl::local_accessor surv(sycl::range<1>((size_t)kSurvMax), h); + sycl::local_accessor outk(sycl::range<1>((size_t)kTopMaxR), h); + sycl::local_accessor cnt(sycl::range<1>(1), h); + h.parallel_for(sycl::nd_range<1>{(size_t)M * kRowWg, kRowWg}, + [=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] { + const int64_t m = (int64_t)it.get_group(0); + const int lane = (int)it.get_local_id(0); + auto grp = it.get_group(); + const sycl::sub_group sg = it.get_sub_group(); + const int sgi = (int)sg.get_group_linear_id(); + const char* xb = x.base; + const uint32_t xdt = x.dtype; + auto key_at = [&](int64_t i) -> uint64_t { + const int64_t o = m * x.pitch + i; + float v; + if (xdt == RAD_BF16) v = rad_bf16_to_f32(((const uint16_t*)xb)[o]); + else if (xdt == RAD_F32) v = ((const float*)xb)[o]; + else v = rad_load_f32(xb, xdt, o); + return sel_key(v, i); + }; + /* both reads by value (NaN compares false, -0 equals 0, as the keys treat them), + * 8 bf16 values a 16-byte load where the row allows; keys only for survivors */ + const bool v8 = xdt == RAD_BF16 && N % 8 == 0 && x.pitch % 8 == 0 && + ((uintptr_t)xb & 15) == 0; /* uniform */ + const uint16_t* xr = (const uint16_t*)xb + m * x.pitch; + float mx = -INFINITY; + if (v8) { + for (int64_t i = (int64_t)lane * 8; i < N; i += (int64_t)kRowWg * 8) { + const sycl::vec w = *(const sycl::vec*)(xr + i); + for (int u = 0; u < 8; ++u) mx = sycl::fmax(mx, rad_bf16_to_f32(w[u])); + } + } else { + for (int64_t i = lane; i < N; i += (int64_t)kRowWg) mx = sycl::fmax(mx, key_value(key_at(i))); + } + mx = sycl::reduce_over_group(sg, mx, sycl::maximum()); + if (sg.get_local_linear_id() == 0) smax[sgi] = sel_key(mx, 0) & ~(uint64_t)0xffffffffu; + if (lane < kTopMaxR) outk[lane] = 0; + if (lane == 0) { cnt[0] = 0; smax[kSgs] = 0; } + sycl::group_barrier(grp); + /* the bound, by value: the largest v with at least R sub-group maxima >= v (those + * are R distinct elements), or none (every key) with fewer than R non-NaN */ + if (lane < kSgs) { + const uint64_t mine = smax[lane]; + int above = 0, atleast = 0; + for (int j = 0; j < kSgs; ++j) { + above += smax[j] > mine ? 1 : 0; + atleast += smax[j] >= mine ? 1 : 0; + } + if (mine != 0 && above < (int)R && atleast >= (int)R) smax[kSgs] = mine; /* ties: one value */ + } + sycl::group_barrier(grp); + const uint64_t bound = sycl::max(smax[kSgs], (uint64_t)1); /* a value's key, column bits 0 */ + sycl::atomic_ref nsurv(cnt[0]); + auto offer = [&](float v, int64_t i) { + const uint64_t k = sel_key(v, i); + if (k >= bound) { + const uint32_t slot = nsurv.fetch_add(1u); + if (slot < (uint32_t)kSurvMax) surv[slot] = k; + } + }; + const float bv = smax[kSgs] != 0 ? key_value(bound) : -INFINITY; + if (v8) { + for (int64_t i = (int64_t)lane * 8; i < N; i += (int64_t)kRowWg * 8) { + const sycl::vec w = *(const sycl::vec*)(xr + i); + for (int u = 0; u < 8; ++u) { + const float v = rad_bf16_to_f32(w[u]); + if (v >= bv) offer(v, i + u); + } + } + } else { + for (int64_t i = lane; i < N; i += (int64_t)kRowWg) { + const uint64_t k = key_at(i); + if (k >= bound) { + const uint32_t slot = nsurv.fetch_add(1u); + if (slot < (uint32_t)kSurvMax) surv[slot] = k; + } + } + } + sycl::group_barrier(grp); + const uint32_t ns = cnt[0]; + if (ns <= (uint32_t)kSurvMax) { /* uniform */ + for (uint32_t s0 = (uint32_t)lane; s0 < ns; s0 += (uint32_t)kRowWg) { + const uint64_t k = surv[s0]; + uint32_t above = 0; + for (uint32_t j = 0; j < ns; ++j) above += surv[j] > k ? 1u : 0u; + if (above < (uint32_t)R) outk[above] = k; + } + sycl::group_barrier(grp); + if (lane < R) store_out(m, lane, outk[lane]); + return; + } + /* too many survivors: the serial passes over the row */ + 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 < N; i += (int64_t)kRowWg) { + const uint64_t key = key_at(i); + if (key < prev) best = sycl::max(best, key); + } + best = sycl::reduce_over_group(grp, best, sycl::maximum()); + } + if (lane == 0) store_out(m, r, best); + if (best == 0) more = false; + else prev = best; + } + }); + }); + }); + } const int64_t chunk = (N + kTopChunks - 1) / kTopChunks; if (N >= 16 * (int64_t)kRowWg && R <= kTopMaxR && chunk <= kTopMaxChunk && !std::getenv("XE2_TOPK_SERIAL")) {