Archived
libxe2: sample_topk in chunks and a merge, the candidate walks staged in local memory, rerank's K split
sample_topk walked the whole vocabulary top_k times with one work-group a row (2.3 ms at 8 rows); each 4096-logit chunk now takes its own top k from registers and a row's merge takes the top k of those -- the same keys, the order being total. narrow and sample_pick stage a row's 1024 candidates (mostly pads) in local memory and compute the exps a lane each before the serial walk; logit_rerank spreads a candidate over up to 16 sub-groups (290 -> 72 us). Outputs unchanged (kbench; the same draft trajectory). Coupled MTP decode of a seeded code prompt 90.5 -> 98.3 tok/s. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b5f116e797
commit
8f84939aa2
2 files changed
+147
-40
No files matched your search
+23
-3
@@ -14,6 +14,7 @@
|
||||
|
||||
#include "rad_planes.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
|
||||
@@ -86,15 +87,21 @@ int rerank(const RadArgs* a, RadStream st, bool joined) {
|
||||
* so the score is libref's unless its exact value sits within ~2^-24 ulp of an f32
|
||||
* rounding boundary -- a fast kernel, held to the tolerance (and the drafter's acceptance
|
||||
* changes only through such ties). The selection is libref's, one work-item a row. */
|
||||
/* SP sub-groups a candidate, each a strided share of K, their pairs added in sub-group
|
||||
* order: one sub-group walking K alone was 320 dependent steps at K = 5120 (290 us a call,
|
||||
* one work-group on the whole card) */
|
||||
const int64_t SP = std::max<int64_t>(1, std::min<int64_t>(16, 1024 / (R * kSub)));
|
||||
return submit([&] {
|
||||
q->submit([&](sycl::handler& h) {
|
||||
sycl::local_accessor<float, 1> scl(sycl::range<1>((size_t)R), h);
|
||||
sycl::local_accessor<int32_t, 1> idl(sycl::range<1>((size_t)R), h);
|
||||
h.parallel_for(sycl::nd_range<1>{(size_t)M * (size_t)R * kSub, (size_t)R * kSub},
|
||||
sycl::local_accessor<float, 1> part(sycl::range<1>((size_t)(R * SP * 2)), h);
|
||||
h.parallel_for(sycl::nd_range<1>{(size_t)M * (size_t)(R * SP) * kSub, (size_t)(R * SP) * kSub},
|
||||
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
|
||||
const int64_t m = (int64_t)it.get_group(0);
|
||||
const sycl::sub_group sg = it.get_sub_group();
|
||||
const int64_t r = (int64_t)sg.get_group_linear_id();
|
||||
const int64_t sgi = (int64_t)sg.get_group_linear_id();
|
||||
const int64_t r = sgi / SP, sp_ = sgi % SP;
|
||||
const int lane = (int)sg.get_local_linear_id();
|
||||
HPlane cp, sp;
|
||||
if (joined) {
|
||||
@@ -112,7 +119,7 @@ int rerank(const RadArgs* a, RadStream st, bool joined) {
|
||||
};
|
||||
float hi = 0.0f, lo = 0.0f;
|
||||
if (!pad)
|
||||
for (int64_t k = lane; k < K; k += kSub) {
|
||||
for (int64_t k = sp_ * kSub + lane; k < K; k += kSub * SP) {
|
||||
const float xv = rad_load_f32(x.base, x.dtype, m * x.pitch + k);
|
||||
const float wv = joined ? cp.at(local, k) * sp.cover(local, k)
|
||||
: rad_load_f32(w.base, w.dtype, local * w.pitch + k);
|
||||
@@ -132,6 +139,19 @@ int rerank(const RadArgs* a, RadStream st, bool joined) {
|
||||
lo = lo + l2 + se;
|
||||
}
|
||||
if (lane == 0) {
|
||||
part[(r * SP + sp_) * 2] = hi;
|
||||
part[(r * SP + sp_) * 2 + 1] = lo;
|
||||
}
|
||||
sycl::group_barrier(it.get_group());
|
||||
if (sp_ == 0 && lane == 0) {
|
||||
hi = part[r * SP * 2];
|
||||
lo = part[r * SP * 2 + 1];
|
||||
for (int64_t u = 1; u < SP; ++u) {
|
||||
float sv, se;
|
||||
two_sum(hi, part[(r * SP + u) * 2], sv, se);
|
||||
hi = sv;
|
||||
lo = lo + part[(r * SP + u) * 2 + 1] + se;
|
||||
}
|
||||
idl[r] = pad ? -1 : (int32_t)g;
|
||||
scl[r] = pad ? -INFINITY : hi + lo;
|
||||
}
|
||||
|
||||
+124
-37
@@ -109,6 +109,48 @@ int per_row(sycl::queue* q, int64_t M, F&& body) {
|
||||
});
|
||||
}
|
||||
|
||||
/* One work-item a row over M rows, a row's candidates first staged in local memory by its whole
|
||||
* work-group: the serial walks over n_cand (1024, mostly pads behind top_k's 20) were a dependent
|
||||
* global load a step, ~480 us a call. The body sees the staged ids (int32, -1 a pad) and values;
|
||||
* what it writes back to the planes it writes itself. */
|
||||
constexpr int64_t kStageMax = 4096;
|
||||
template <class F>
|
||||
int per_row_staged(sycl::queue* q, int64_t M, const Cands& c, F&& body) {
|
||||
const int64_t n = c.n_cand;
|
||||
if (n > kStageMax) return refuse("candidate rows of %lld over %lld", (long long)n, (long long)kStageMax);
|
||||
constexpr size_t wg = 256;
|
||||
return submit([&] {
|
||||
q->submit([&](sycl::handler& h) {
|
||||
sycl::local_accessor<int32_t, 1> sid(sycl::range<1>((size_t)n), h);
|
||||
sycl::local_accessor<float, 1> sv(sycl::range<1>((size_t)n), h);
|
||||
sycl::local_accessor<float, 1> se(sycl::range<1>((size_t)n), h);
|
||||
h.parallel_for(sycl::nd_range<1>{(size_t)M * wg, wg}, [=](sycl::nd_item<1> it) {
|
||||
const int64_t m = (int64_t)it.get_group(0);
|
||||
const int64_t lane = (int64_t)it.get_local_id(0);
|
||||
float lmx = -INFINITY;
|
||||
for (int64_t i = lane; i < n; i += (int64_t)wg) {
|
||||
const int32_t id = (int32_t)ld_index(c.idx.dtype, c.idx.base, m * c.idx.pitch + i);
|
||||
const float v = rad_load_f32(c.val.base, c.val.dtype, m * c.val.pitch + i);
|
||||
sid[i] = id;
|
||||
sv[i] = v;
|
||||
if (id >= 0 && v > lmx) lmx = v;
|
||||
}
|
||||
/* the live maximum (the bodies' own serial scan finds the same value, up to the
|
||||
* sign of a zero, which no exp(v - mx) below sees), then every live candidate's
|
||||
* exp(v - mx) a lane each: the double exp is the bodies' cost, ~6 us apiece */
|
||||
const float mx = sycl::reduce_over_group(it.get_group(), lmx, sycl::maximum<float>());
|
||||
for (int64_t i = lane; i < n; i += (int64_t)wg)
|
||||
se[i] = sid[i] >= 0 && mx != -INFINITY ? ref_exp(sv[i] - mx) : 0.0f;
|
||||
sycl::group_barrier(it.get_group());
|
||||
if (lane == 0)
|
||||
body(m, sid.template get_multi_ptr<sycl::access::decorated::no>().get(),
|
||||
sv.template get_multi_ptr<sycl::access::decorated::no>().get(),
|
||||
se.template get_multi_ptr<sycl::access::decorated::no>().get());
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
/* ------------------------------------------------------------------ sample_penalties */
|
||||
@@ -401,45 +443,89 @@ int sample_topk(const RadArgs* a, RadStream st) {
|
||||
const int64_t voff = rad_args_geti_or(a, "vocab_off", 0);
|
||||
sycl::queue* q = queue_of(st);
|
||||
if (!q) return RAD_E_DEVICE;
|
||||
/* Two passes: each chunk of kChunk logits (a work-group, its keys held in registers) takes its
|
||||
* own top k, and a row's merge takes the top k of the chunks' -- the same keys, since a key's
|
||||
* rank is a total order (value, then index). One work-group a row walking the whole vocabulary
|
||||
* k times was 2.3 ms a call at 8 rows (k = 20). */
|
||||
constexpr int kCw = 256, kEpi = 16;
|
||||
constexpr int64_t kChunk = (int64_t)kCw * kEpi;
|
||||
const int64_t nch = (n_vocab + kChunk - 1) / kChunk;
|
||||
uint64_t* ck = (uint64_t*)workspace(*q, (size_t)(M * nch * n_cand * 2), 10);
|
||||
if (!ck) return RAD_E_DEVICE;
|
||||
if (int rc = submit([&] {
|
||||
syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)(M * nch) * kCw, kCw}, [=](sycl::nd_item<1> it) {
|
||||
const int64_t m = (int64_t)it.get_group(0) / nch, ch = (int64_t)it.get_group(0) % nch;
|
||||
const int lane = (int)it.get_local_id(0);
|
||||
int64_t k = P[m].top_k;
|
||||
if (k <= 0 || k > n_cand) k = n_cand;
|
||||
if (P[m].flags & RAD_SP_GREEDY) k = 0;
|
||||
uint64_t key[kEpi];
|
||||
#pragma unroll
|
||||
for (int e = 0; e < kEpi; ++e) {
|
||||
const int64_t i = ch * kChunk + e * kCw + lane;
|
||||
key[e] = i < n_vocab ? sel_key(rad_load_f32(lg.base, lg.dtype, m * lg.pitch + i), i) : 0;
|
||||
}
|
||||
uint64_t* out = ck + (m * nch + ch) * n_cand;
|
||||
uint64_t prev = ~(uint64_t)0;
|
||||
for (int64_t r = 0; r < k; ++r) { /* a chunk's first k keys (0: none left) */
|
||||
uint64_t best = 0;
|
||||
if (prev != 0) {
|
||||
#pragma unroll
|
||||
for (int e = 0; e < kEpi; ++e)
|
||||
if (key[e] < prev) best = sycl::max(best, key[e]);
|
||||
best = sycl::reduce_over_group(it.get_group(), best, sycl::maximum<uint64_t>());
|
||||
}
|
||||
if (lane == 0) out[r] = best;
|
||||
prev = best;
|
||||
}
|
||||
});
|
||||
});
|
||||
rc < 0)
|
||||
return rc;
|
||||
return submit([&] {
|
||||
syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)M * kRowWg, kRowWg}, [=](sycl::nd_item<1> it) {
|
||||
syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)M * kCw, kCw}, [=](sycl::nd_item<1> it) {
|
||||
const int64_t m = (int64_t)it.get_group(0);
|
||||
const int lane = (int)it.get_local_id(0);
|
||||
int64_t k = P[m].top_k;
|
||||
if (k <= 0 || k > n_cand) k = n_cand;
|
||||
if (P[m].flags & RAD_SP_GREEDY) k = 0;
|
||||
const uint64_t* rk = ck + m * nch * n_cand;
|
||||
const int64_t nk = nch * k;
|
||||
/* prev starts above every key, so the first round takes the maximum; `more` is uniform
|
||||
* across the group, so every work-item reaches the same reductions. */
|
||||
* across the group, so every work-item reaches the same reductions. Rounds k and on
|
||||
* only write the pads, a lane each. */
|
||||
uint64_t prev = ~(uint64_t)0;
|
||||
bool more = true;
|
||||
for (int64_t r = 0; r < n_cand; ++r) {
|
||||
auto emit = [&](int64_t r, uint64_t best) {
|
||||
const bool ok = best != 0;
|
||||
const int64_t pi = ok ? key_index(best) : -1;
|
||||
const float pv = ok ? key_value(best) : -INFINITY;
|
||||
const int64_t io = m * c.idx.pitch + r;
|
||||
if (c.idx.dtype == RAD_I64) ((int64_t*)c.idx.base)[io] = pi;
|
||||
else ((int32_t*)c.idx.base)[io] = (int32_t)pi;
|
||||
rad_store_f32(c.val.base, c.val.dtype, m * c.val.pitch + r, pv);
|
||||
if (has_pp) {
|
||||
uint32_t bits;
|
||||
std::memcpy(&bits, &pv, 4);
|
||||
uint32_t* pr = (uint32_t*)pp.base + m * pp.pitch + 2 * r;
|
||||
pr[0] = ok ? (uint32_t)(pi + voff) : 0xffffffffu;
|
||||
pr[1] = bits;
|
||||
}
|
||||
};
|
||||
for (int64_t r = 0; r < k; ++r) {
|
||||
uint64_t best = 0;
|
||||
if (r < k && more) {
|
||||
for (int64_t i = lane; i < n_vocab; i += (int64_t)kRowWg) {
|
||||
const uint64_t key = sel_key(rad_load_f32(lg.base, lg.dtype, m * lg.pitch + i), i);
|
||||
if (more) {
|
||||
for (int64_t i = lane; i < nk; i += (int64_t)kCw) {
|
||||
const uint64_t key = rk[(i / k) * n_cand + i % k];
|
||||
if (key < prev) best = sycl::max(best, key);
|
||||
}
|
||||
best = sycl::reduce_over_group(it.get_group(), best, sycl::maximum<uint64_t>());
|
||||
}
|
||||
if (lane == 0) {
|
||||
const bool ok = best != 0;
|
||||
const int64_t pi = ok ? key_index(best) : -1;
|
||||
const float pv = ok ? key_value(best) : -INFINITY;
|
||||
const int64_t io = m * c.idx.pitch + r;
|
||||
if (c.idx.dtype == RAD_I64) ((int64_t*)c.idx.base)[io] = pi;
|
||||
else ((int32_t*)c.idx.base)[io] = (int32_t)pi;
|
||||
rad_store_f32(c.val.base, c.val.dtype, m * c.val.pitch + r, pv);
|
||||
if (has_pp) {
|
||||
uint32_t bits;
|
||||
std::memcpy(&bits, &pv, 4);
|
||||
uint32_t* pr = (uint32_t*)pp.base + m * pp.pitch + 2 * r;
|
||||
pr[0] = ok ? (uint32_t)(pi + voff) : 0xffffffffu;
|
||||
pr[1] = bits;
|
||||
}
|
||||
}
|
||||
if (lane == 0) emit(r, best);
|
||||
if (best == 0) more = false;
|
||||
else prev = best;
|
||||
}
|
||||
for (int64_t r = k + lane; r < n_cand; r += (int64_t)kCw) emit(r, 0);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -462,7 +548,7 @@ int narrow(const RadArgs* a, RadStream st, Narrow which, const char* op) {
|
||||
if (int rc = param_rows(&a->t[2], M, &P); rc < 0) return rc;
|
||||
sycl::queue* q = queue_of(st);
|
||||
if (!q) return RAD_E_DEVICE;
|
||||
return per_row(q, M, [=](int64_t m) {
|
||||
return per_row_staged(q, M, c, [=](int64_t m, int32_t* sid, float* sv, const float* se) {
|
||||
const RadSampleParams& sp = P[m];
|
||||
if (sp.flags & RAD_SP_GREEDY) return;
|
||||
const int64_t ib = m * c.idx.pitch, vb = m * c.val.pitch;
|
||||
@@ -470,9 +556,11 @@ int narrow(const RadArgs* a, RadStream st, Narrow which, const char* op) {
|
||||
if (c.idx.dtype == RAD_I64) ((int64_t*)c.idx.base)[ib + i] = -1;
|
||||
else ((int32_t*)c.idx.base)[ib + i] = -1;
|
||||
rad_store_f32(c.val.base, c.val.dtype, vb + i, -INFINITY);
|
||||
sid[i] = -1;
|
||||
sv[i] = -INFINITY;
|
||||
};
|
||||
auto live = [&](int64_t i) { return ld_index(c.idx.dtype, c.idx.base, ib + i) >= 0; };
|
||||
auto val = [&](int64_t i) { return rad_load_f32(c.val.base, c.val.dtype, vb + i); };
|
||||
auto live = [&](int64_t i) { return sid[i] >= 0; };
|
||||
auto val = [&](int64_t i) { return sv[i]; };
|
||||
float mx = -INFINITY;
|
||||
for (int64_t i = 0; i < n_cand; ++i)
|
||||
if (live(i)) { const float v = val(i); if (v > mx) mx = v; }
|
||||
@@ -484,13 +572,13 @@ int narrow(const RadArgs* a, RadStream st, Narrow which, const char* op) {
|
||||
if (!live(i)) continue;
|
||||
const float v = val(i);
|
||||
if (v == mx) continue;
|
||||
if (ref_exp(v - mx) < p) drop(i);
|
||||
if (se[i] < p) drop(i);
|
||||
}
|
||||
return;
|
||||
}
|
||||
float sum = 0.0f;
|
||||
for (int64_t i = 0; i < n_cand; ++i)
|
||||
if (live(i)) sum += ref_exp(val(i) - mx);
|
||||
if (live(i)) sum += se[i];
|
||||
const float inv = sum > 0.0f ? 1.0f / sum : 0.0f;
|
||||
if (which == NR_TOPP) {
|
||||
const float p = sp.top_p;
|
||||
@@ -500,7 +588,7 @@ int narrow(const RadArgs* a, RadStream st, Narrow which, const char* op) {
|
||||
for (int64_t i = 0; i < n_cand; ++i) {
|
||||
if (!live(i)) continue;
|
||||
if (done) { drop(i); continue; }
|
||||
cum += ref_exp(val(i) - mx) * inv;
|
||||
cum += se[i] * inv;
|
||||
if (cum >= p) done = true;
|
||||
}
|
||||
return;
|
||||
@@ -510,7 +598,7 @@ int narrow(const RadArgs* a, RadStream st, Narrow which, const char* op) {
|
||||
if (!(p < 1.0f) || !(p > 0.0f)) return;
|
||||
/* libref keeps q[] and d[]; here they are recomputed from the candidates, which give
|
||||
* the same floats, since the plane is not written until the drops at the end. */
|
||||
auto qp = [&](int64_t i) { return live(i) ? ref_exp(val(i) - mx) * inv : 0.0f; };
|
||||
auto qp = [&](int64_t i) { return live(i) ? se[i] * inv : 0.0f; };
|
||||
float H = 0.0f;
|
||||
for (int64_t i = 0; i < n_cand; ++i) {
|
||||
const float qi = qp(i);
|
||||
@@ -549,7 +637,7 @@ int narrow(const RadArgs* a, RadStream st, Narrow which, const char* op) {
|
||||
int64_t last_above = -1, n_above = 0;
|
||||
for (int64_t i = 0; i < n_cand; ++i) {
|
||||
if (!live(i)) continue;
|
||||
if (ref_exp(val(i) - mx) * inv >= sp.xtc_threshold) {
|
||||
if (se[i] * inv >= sp.xtc_threshold) {
|
||||
last_above = i;
|
||||
++n_above;
|
||||
}
|
||||
@@ -557,7 +645,7 @@ int narrow(const RadArgs* a, RadStream st, Narrow which, const char* op) {
|
||||
if (n_above < 2) return;
|
||||
for (int64_t i = 0; i < n_cand; ++i) {
|
||||
if (!live(i) || i == last_above) continue;
|
||||
if (ref_exp(val(i) - mx) * inv >= sp.xtc_threshold) drop(i);
|
||||
if (se[i] * inv >= sp.xtc_threshold) drop(i);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -586,11 +674,10 @@ int sample_pick(const RadArgs* a, RadStream st) {
|
||||
const uint32_t tdt = TOK->dtype;
|
||||
sycl::queue* q = queue_of(st);
|
||||
if (!q) return RAD_E_DEVICE;
|
||||
return per_row(q, M, [=](int64_t m) {
|
||||
return per_row_staged(q, M, c, [=](int64_t m, const int32_t* sid, const float* sv, const float* se) {
|
||||
if (P[m].flags & RAD_SP_GREEDY) return;
|
||||
const int64_t ib = m * c.idx.pitch, vb = m * c.val.pitch;
|
||||
auto id_at = [&](int64_t i) { return ld_index(c.idx.dtype, c.idx.base, ib + i); };
|
||||
auto val = [&](int64_t i) { return rad_load_f32(c.val.base, c.val.dtype, vb + i); };
|
||||
auto id_at = [&](int64_t i) { return (int64_t)sid[i]; };
|
||||
auto val = [&](int64_t i) { return sv[i]; };
|
||||
auto put = [&](int64_t v) {
|
||||
if (tdt == RAD_I64) ((int64_t*)tb)[m] = v;
|
||||
else ((int32_t*)tb)[m] = (int32_t)v;
|
||||
@@ -604,7 +691,7 @@ int sample_pick(const RadArgs* a, RadStream st) {
|
||||
if (mx == -INFINITY) { put(-1); return; }
|
||||
float sum = 0.0f;
|
||||
for (int64_t i = 0; i < n_cand; ++i)
|
||||
if (id_at(i) >= 0) sum += ref_exp(val(i) - mx);
|
||||
if (id_at(i) >= 0) sum += se[i];
|
||||
const float inv = sum > 0.0f ? 1.0f / sum : 0.0f;
|
||||
const float u = rad_sample_uniform01(P[m].seed ^ RAD_SAMPLE_KEY_PICK, P[m].pos);
|
||||
float cum = 0.0f;
|
||||
@@ -614,7 +701,7 @@ int sample_pick(const RadArgs* a, RadStream st) {
|
||||
if (id < 0) continue;
|
||||
const float v = val(i);
|
||||
if (v > -INFINITY) last = id;
|
||||
cum += ref_exp(v - mx) * inv;
|
||||
cum += se[i] * inv;
|
||||
if (u < cum) { chosen = id; break; }
|
||||
}
|
||||
put(chosen >= 0 ? chosen : last);
|
||||
|
||||
Reference in new issue
Block a user