/* xe2_draft.cpp -- the speculative drafters' selection ops. * * row_topk exact per-row top-R, descending, ties to the lower column (ref_moe.cpp): libref's * topk_next R times, each a work-group reduction of the selection key * (xe2_common.h), one work-group a row * 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. */ #include "xe2_common.h" #include "xe2_ops.h" #include #include namespace xe2 { namespace { constexpr size_t kRowWg = 1024; } /* ------------------------------------------------------------------ row_topk */ int row_topk(const RadArgs* a, RadStream st) { if (!rad_args_have(a, 3)) return RAD_E_INVAL; const RadTensor *X = &a->t[0], *IDX = &a->t[1], *VAL = &a->t[2]; const RadTensor* PAIRS = rad_arg_in(a, 3); const int64_t off = rad_args_geti_or(a, "vocab_off", 0); const int64_t N = rad_args_geti_or(a, "N", X->rank >= 2 ? X->shape[X->rank - 1] : 0); const int64_t R = rad_args_geti_or(a, "R", 1); if (N <= 0 || R <= 0) return RAD_E_SHAPE; Rows x, iv, vv; if (int rc = rows_view(X, N, &x, "row_topk x"); rc < 0) return rc; const int64_t M = x.rows; if (M <= 0 || rad_tensor_numel(IDX) < M * R || rad_tensor_numel(VAL) < M * R) return RAD_E_SHAPE; if (PAIRS && (PAIRS->dtype != RAD_F32 || rad_tensor_numel(PAIRS) < M * R * 2)) return RAD_E_SHAPE; if (PAIRS && !rad_tensor_is_contiguous(PAIRS)) return refuse("row_topk: pairs not contiguous"); if (int rc = rows_view(IDX, R, &iv, "row_topk idx"); rc < 0) return rc; if (int rc = rows_view(VAL, R, &vv, "row_topk val"); rc < 0) return rc; float* pairs = PAIRS ? (float*)PAIRS->data : nullptr; sycl::queue* q = queue_of(st); if (!q) return RAD_E_DEVICE; 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); uint64_t prev = ~(uint64_t)0; bool more = true; /* uniform across the group */ 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 = sel_key(rad_load_f32(x.base, x.dtype, m * x.pitch + i), i); if (key < prev) best = sycl::max(best, key); } 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; } }); }); } /* ------------------------------------------------------------------ dflash_select */ int dflash_select(const RadArgs* a, RadStream st) { if (!a || a->n_t < 7) return RAD_E_INVAL; const RadTensor *CAND = &a->t[0], *UN = &a->t[1], *ANC = &a->t[3]; const RadTensor *PRED = &a->t[4], *SUCC = &a->t[5], *OUT = &a->t[6]; const RadTensor* HP = rad_arg_in(a, 2); if (!CAND->data || !UN->data || !ANC->data || !PRED->data || !SUCC->data || !OUT->data) return RAD_E_INVAL; const int64_t steps = rad_args_geti_or(a, "steps", 0); const int64_t K = rad_args_geti_or(a, "top_k", 0); /* libref's band_rows: the rows `cand` holds, unless that exceeds the band. */ int64_t M = 0; if (steps > 0 && K > 0) { const int64_t from = rad_tensor_numel(CAND) / (steps * K), band = rad_args_geti_or(a, "M", 0); M = from <= 0 ? 0 : (band > 0 && from > band) ? 0 : from; } const int64_t R = rad_args_geti_or(a, "rank", PRED->rank >= 2 ? PRED->shape[PRED->rank - 1] : 0); const int64_t nv = rad_args_geti_or(a, "n_vocab", PRED->rank >= 2 ? PRED->shape[PRED->rank - 2] : 0); const int64_t astr = rad_args_geti_or(a, "anchor_stride", 1); if (M <= 0 || steps <= 0 || K <= 0 || R <= 0 || nv <= 0 || astr <= 0) return RAD_E_SHAPE; if (rad_tensor_numel(UN) < M * steps * K) return RAD_E_SHAPE; if (rad_tensor_numel(ANC) < (M - 1) * astr + 1 || rad_tensor_numel(OUT) < M * steps) return RAD_E_SHAPE; if (rad_tensor_numel(PRED) < nv * R || rad_tensor_numel(SUCC) < nv * R) return RAD_E_SHAPE; if (HP && rad_tensor_numel(HP) < M * steps * R) return RAD_E_SHAPE; Rows cand, un, pred, succ, out, hp; if (int rc = rows_view(CAND, K, &cand, "dflash_select cand"); rc < 0) return rc; if (int rc = rows_view(UN, K, &un, "dflash_select unary"); rc < 0) return rc; if (int rc = rows_view(PRED, R, &pred, "dflash_select pred"); rc < 0) return rc; if (int rc = rows_view(SUCC, R, &succ, "dflash_select succ"); rc < 0) return rc; if (int rc = rows_view(OUT, steps, &out, "dflash_select tokens"); rc < 0) return rc; const bool has_hp = HP != nullptr; if (has_hp) if (int rc = rows_view(HP, R, &hp, "dflash_select hp"); rc < 0) return rc; const Lin anc = Lin::of(ANC); sycl::queue* q = queue_of(st); if (!q) return RAD_E_DEVICE; const size_t wg = 16; const size_t n = (size_t)((M + (int64_t)wg - 1) / (int64_t)wg) * wg; return submit([&] { syclex::nd_launch(*q, sycl::nd_range<1>{n, wg}, [=](sycl::nd_item<1> it) { const int64_t m = (int64_t)it.get_global_id(0); if (m >= M) return; 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; auto ax = [&](int64_t i) { return (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 c = 0; c < K; ++c) { const int64_t tok = ld_index(cand.dtype, cand.base, r * cand.pitch + c); float sc = rad_load_f32(un.base, un.dtype, r * un.pitch + c); if (tok >= 0 && tok < nv) for (int64_t i = 0; i < R; ++i) sc += ax(i) * rad_load_f32(succ.base, succ.dtype, tok * succ.pitch + i); if (sc > best_s) { best_s = sc; best_c = c; } } pid = ld_index(cand.dtype, cand.base, r * cand.pitch + best_c); 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; } }); }); } } // namespace xe2