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:
claudeandClaude Opus 5.5 committed 2026-11-02 07:31:15 +01:00
1 parent b5f116e797
commit 8f84939aa2
2 files changed
+147 -40

No files matched your search

+23 -3
View File
@@ -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;
}