Merge upstream main (v1.17.0 -> v2.1.0) into the SYCL port

102 upstream commits. Nine conflicts resolved. Fork side of the merge:
- libxe2: declare the givens_dense_quant schema (upstream 2.0 dropped it
  from libavx and libref; the device libraries now each carry it).
- plain_norm becomes an ABI 42 append to RadBuildCtx; abi layout table
  regenerated, kTableAbi = 42.

Known before the PR: arch_test (MTP lint and pinned graph hashes for
qwen35_fp8, qwen35moe_fp8, qwen4exp with MTP).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
aneinanderandClaude Opus 5.5 committed 2026-11-18 15:06:17 +01:00
commit 4b70a96378
850 files changed
+157819 -58519

No files matched your search

+2
View File
@@ -78,6 +78,7 @@ extern "C" const RadKernelInfo* rad_kernel_at(int i) {
rad_spechub_row(0, rad_mine_mtp_couple_propose_launch),
rad_spechub_row(1, rad_mine_mtp_couple_verify_launch),
rad_spechub_row(2, rad_mine_dflash_couple_launch),
rad_spechub_row(3, rad_mine_mtp_trav_verify_launch),
};
if (i >= 0 && i < RAD_SPECHUB_N_OPS) return &dev[i];
i -= RAD_SPECHUB_N_OPS;
@@ -86,6 +87,7 @@ extern "C" const RadKernelInfo* rad_kernel_at(int i) {
ref_row(0, rad_ref_mtp_couple_propose, "ref_mtp_couple_propose"),
ref_row(1, rad_ref_mtp_couple_verify, "ref_mtp_couple_verify"),
ref_row(2, rad_ref_dflash_couple, "ref_dflash_couple_propose"),
ref_row(3, rad_ref_mtp_trav_verify, "ref_mtp_trav_verify"),
};
if (i < 0 || i >= RAD_SPECHUB_N_OPS) return nullptr;
return &ref[i];
+9
View File
@@ -21,6 +21,15 @@ int rad_ref_mtp_couple_verify(const RadArgs* a, RadStream) {
return RAD_OK;
}
/* The traversal walk, one request at a time: the definition the device kernel runs per root. */
int rad_ref_mtp_trav_verify(const RadArgs* a, RadStream) {
RadMtpTravCall c{};
const int rc = rad_mtp_trav_verify_args(a, &c);
if (rc != RAD_OK) return rc;
for (int64_t row = 0; row < c.v.M; ++row) rad_mtp_trav_verify_root(c, row);
return RAD_OK;
}
/* One sequence at a time through rad_dflash_couple_walk, the definition the device kernel matches
* bit for bit. */
int rad_ref_dflash_couple(const RadArgs* a, RadStream) {
+70 -1
View File
@@ -42,6 +42,16 @@ const RadOperandSpec kVerifyOperands[] = {
{ "token", RAD_OPD_INOUT, 0 },
};
/* Traversal verification: the verify's five operands and the step's tree. */
const RadOperandSpec kTravOperands[] = {
{ "cand_idx", RAD_OPD_IN, 0 },
{ "cand_val", RAD_OPD_IN, 0 },
{ "params", RAD_OPD_IN, 0 },
{ "state", RAD_OPD_IN, 0 },
{ "token", RAD_OPD_INOUT, 0 },
{ "topo", RAD_OPD_IN, 0 },
};
/* DFlash2's coupled walk: dflash_select's operands, then the coupling's. `tokens` is INOUT because a
* greedy request's row keeps the selector's walk; `state` holds one row per (round, sequence). */
const RadOperandSpec kDflashOperands[] = {
@@ -66,7 +76,8 @@ const RadOpSchema kSchemas[] = {
{ "mtp_couple_verify", kVerifyParams, 3, kVerifyOperands, 5,
"Maximal coupling of a bound mtp_couple state after sample_pick over all surviving target "
"candidates, FP64 P and 53-bit ACCEPT/RESID uniforms at params.pos; residual (P-Q)+ with "
"r(x)=0. Rows whose state is not status 1, not bound to params seed/pos, malformed, or whose "
"r(x)=0; a state with `blk` >= 1 is the first row of a block, verified together with the rows after "
"it (Block Verification, arXiv 2403.10444). Rows whose state is not status 1, not bound to params seed/pos, malformed, or whose "
"(P,Q) admit no coupling keep the ordinary token." },
{ "dflash_couple_propose", kDflashParams, 8, kDflashOperands, 10,
"DFlash2's candidate walk, sampled: at each step Q = FP64 softmax of (unary + low-rank edge "
@@ -74,6 +85,17 @@ const RadOpSchema kSchemas[] = {
"draw's (seed, base, KEY_DRAFT + step + 1), x the next step's predecessor; tokens and "
"state[step][row] written. mode0 rows keep tokens (status 0); a Q that cannot be built or "
"seq_ids != draw.card takes the walk's argmax (status -1)." },
{ "mtp_trav_verify", kVerifyParams, 3, kTravOperands, 6,
"Traversal Verification (arXiv 2505.12398) of a bound draft after sample_pick: per "
"request (a row whose topo parent is -1), the bottom-up sequence-level walk over its rows' "
"bound mtp_couple states and target candidates (the target's law as integer masses of an "
"FP32 exponential, the walk in FP64): a leaf is accepted with the path's cumulative ratio, a "
"rejected leaf updates its parent's acceptance, target residual and draft law (Gumbel "
"siblings without replacement; a RheoSampling node's tail child first, then its top-q "
"children as point masses). The accepted path's rows get their path child's token and the "
"last row the final token; rows whose state is not eligible (the row-wise gate) are leaves "
"and keep the pick; an eligible row whose draw asks for it gets P exported over its state. "
"topo is [M,2] int32: parent row (-1 root), draft token. Any n_cand." },
};
} /* namespace */
@@ -266,6 +288,40 @@ int rad_mtp_couple_verify_args(const RadArgs* a, RadMtpCoupleVerifyCall* out) {
return RAD_OK;
}
int rad_mtp_trav_verify_shape(const RadParam* p, int n, int i, RadOpdDesc* out) {
if (!out || i < 0 || i >= 6) return RAD_E_INVAL;
if (i < 5) return rad_mtp_couple_verify_shape(p, n, i, out);
int64_t M, v, nc;
int rc = couple_geometry(p, n, 1, true, &M, &v, &nc);
if (rc != RAD_OK) return rc;
RadOpdDesc d{};
d.idx_const = -1;
d.dtype = RAD_I32;
d.rank = 2;
d.shape[0] = M;
d.shape[1] = 2;
*out = d;
return RAD_OK;
}
int rad_mtp_trav_verify_args(const RadArgs* a, RadMtpTravCall* out) {
if (!out || !a || !a->t || a->n_t != 6) return RAD_E_INVAL;
RadArgs five = *a;
five.n_t = 5;
RadMtpTravCall c{};
int rc = rad_mtp_couple_verify_args(&five, &c.v);
if (rc != RAD_OK) return rc;
const RadTensor& t = a->t[5];
if (!t.data || (uintptr_t)t.data % 4) return RAD_E_INVAL;
if (t.dtype != RAD_I32) return RAD_E_DTYPE;
if (t.rank != 2 || t.shape[0] < c.v.M || t.shape[1] != 2) return RAD_E_SHAPE;
if (t.stride[1] != 1 || t.stride[0] < 2) return RAD_E_STRIDE;
c.topo = (const int32_t*)t.data;
c.ld_topo = t.stride[0];
*out = c;
return RAD_OK;
}
/* ------------------------------------------------------------------ dflash_couple_propose */
namespace {
@@ -405,6 +461,19 @@ RadKernelInfo rad_spechub_row(int i, RadLaunchFn launch) {
r.opd_shape = rad_dflash_couple_shape;
return r;
}
if (i == 3) {
r.name = "spechub_mtp_trav_verify";
r.op = "mtp_trav_verify";
r.family = "draft";
r.computes = "Traversal Verification of a coupled draft: integer target masses, FP64 walk";
r.shape = "M,n_cand,n_vocab";
r.dtypes = "f32,i32,u32";
r.domain = RAD_DOMAIN_DEVICE;
r.priority = 100;
r.launch = launch;
r.opd_shape = rad_mtp_trav_verify_shape;
return r;
}
r.name = i ? "spechub_mtp_couple_verify" : "spechub_mtp_couple_propose";
r.op = i ? "mtp_couple_verify" : "mtp_couple_propose";
r.family = "draft";
+148 -1
View File
@@ -10,6 +10,9 @@
#include "rad_abi.h"
#include "rad_plugin.h" /* rad_param_geti/getdim: the pure readers, inline and total */
#include "rad_spechub.h"
#include "rad_mtp_rrs.h"
#include "rad_mtp_block.h"
#include "rad_mtp_traversal.h"
#include <cmath>
#include <cstdint>
@@ -45,6 +48,16 @@ struct RadMtpCoupleVerifyCall {
int64_t M, n_cand, vocab, ld[5];
};
/* The traversal verify's operands: the row-wise verify's, plus the step's tree. `topo` is
* [M, 2] int32 per sampled row: the parent's row in the step (-1 for a request's first row, its
* tree's root) and the row's own draft token. The rows of one request are contiguous, the root
* first, parents before children (core/sample/sampler.cpp stages it that way). */
struct RadMtpTravCall {
RadMtpCoupleVerifyCall v;
const int32_t* topo;
int64_t ld_topo;
};
/* DFlash2's coupled walk (`dflash_couple_propose`, abi/rad_spechub.h): one sequence a row. Its
* operands, in schema order: cand, unary, hp?, anchor, pred w, succ w, seq_ids, draw, tokens
* (INOUT: dflash_select's greedy walk, rewritten for a sampled row), state. */
@@ -69,6 +82,8 @@ int rad_mtp_couple_verify_shape(const RadParam*, int, int, RadOpdDesc*);
int rad_dflash_couple_shape(const RadParam*, int, int, RadOpdDesc*);
int rad_mtp_couple_propose_args(const RadArgs*, RadMtpCoupleProposeCall*);
int rad_mtp_couple_verify_args(const RadArgs*, RadMtpCoupleVerifyCall*);
int rad_mtp_trav_verify_shape(const RadParam*, int, int, RadOpdDesc*);
int rad_mtp_trav_verify_args(const RadArgs*, RadMtpTravCall*);
int rad_dflash_couple_args(const RadArgs*, RadDflashCoupleCall*);
/* The geometry/validation the shape hooks and the arg readers share. `op` 0 proposes, 1 verifies. */
@@ -76,7 +91,7 @@ int rad_couple_validate(const RadArgs*, int op, int64_t* M, int64_t* vocab, int6
/* The op set. Three schemas, three device rows and three host-reference rows; row/schema index 2 is
* dflash_couple_propose. */
#define RAD_SPECHUB_N_OPS 3
#define RAD_SPECHUB_N_OPS 4
int rad_spechub_schema_count();
const RadOpSchema* rad_spechub_schema_at(int i);
RadKernelInfo rad_spechub_row(int i, RadLaunchFn launch);
@@ -87,6 +102,8 @@ int rad_mine_mtp_couple_verify_launch(const RadArgs*, RadStream);
int rad_ref_mtp_couple_propose(const RadArgs*, RadStream);
int rad_ref_mtp_couple_verify(const RadArgs*, RadStream);
int rad_mine_dflash_couple_launch(const RadArgs*, RadStream);
int rad_mine_mtp_trav_verify_launch(const RadArgs*, RadStream);
int rad_ref_mtp_trav_verify(const RadArgs*, RadStream);
int rad_ref_dflash_couple(const RadArgs*, RadStream);
/* One propose row, host and device: x ~ Q over the final eight. Column 0 is what ADV_ROUND and
@@ -121,7 +138,56 @@ static inline RAD_SPECHUB_HD void rad_mtp_couple_propose_row(const RadMtpCoupleP
*reinterpret_cast<RadMtpCoupleState*>(c.state + (c.round - 1) * c.st_round + row * c.st_row) = s;
}
/* --draft-verify block (abi/rad_mtp_block.h). The sampler marks the chain rows of a request it
* verifies as one block by `blk` = the block's length g (the rows 0 .. g-1 bound to rounds 1 .. g,
* consecutive); the row of round 1 decides the whole block and writes the tokens of rows 0 .. tau,
* the other rows do nothing (their tokens are written by it). Row i of the block is admitted only
* if it is the state the sampler would have bound for it: same request, seed, round, position
* (pos0 + i) and a valid state; any other row ends the block's coupling (the ordinary picks
* stand, which is exact). */
static inline RAD_SPECHUB_HD bool rad_mtp_block_row_ok(const RadMtpCoupleState& s0,
const RadMtpCoupleState& s,
const RadSampleParams& p0,
const RadSampleParams& p, int i, int g,
int64_t vocab) {
return s.status == 1 && s.blk == g && s.round == i + 1 && s.nkids <= 1 &&
!(p.flags & RAD_SP_GREEDY) && s.draw.seed == p.seed && p.seed == p0.seed &&
s.draw.seq == s0.draw.seq && s.pos == p.pos && p.pos == p0.pos + (uint64_t)i &&
s.pos == s0.pos + (uint64_t)i && rad_mtp_couple_state_valid(s, vocab);
}
/* The block's tokens: rows 0 .. tau of the block take the drafts and then Y. */
static inline RAD_SPECHUB_HD void rad_mtp_block_write(const RadMtpCoupleVerifyCall& c, int64_t row,
const RadMtpCoupleState* st, int tau, int g,
int32_t y) {
for (int i = 0; i < tau; ++i) c.token[(row + i) * c.ld[4]] = st[i].x;
if (tau < g) c.token[(row + tau) * c.ld[4]] = y;
}
static inline RAD_SPECHUB_HD void rad_mtp_block_verify_row(const RadMtpCoupleVerifyCall& c,
int64_t row) {
const auto& s0 = *reinterpret_cast<const RadMtpCoupleState*>(c.state + row * c.ld[3]);
const int g = s0.blk;
if (s0.round != 1 || g < 1 || g > RAD_MTP_BLOCK_MAX || row + g > c.M) return;
const auto& p0 = *reinterpret_cast<const RadSampleParams*>(c.params + row * c.ld[2]);
RadMtpCoupleState st[RAD_MTP_BLOCK_MAX];
RadBlockRow rows[RAD_MTP_BLOCK_MAX];
for (int i = 0; i < g; ++i) {
st[i] = *reinterpret_cast<const RadMtpCoupleState*>(c.state + (row + i) * c.ld[3]);
const auto& p = *reinterpret_cast<const RadSampleParams*>(c.params + (row + i) * c.ld[2]);
if (!rad_mtp_block_row_ok(s0, st[i], p0, p, i, g, c.vocab)) return;
rows[i] = RadBlockRow{ c.idx + (row + i) * c.ld[0], c.val + (row + i) * c.ld[1],
c.n_cand, 0, 0.0f };
rad_mtp_block_scan(&rows[i], c.vocab);
}
int tau = 0;
int32_t y = -1;
if (!rad_mtp_block_decide(st, rows, g, p0.seed, p0.pos, &tau, &y)) return;
rad_mtp_block_write(c, row, st, tau, g, y);
}
/* One verify row, after sample_pick. A row without a valid bound state keeps the ordinary token. */
template <bool kBlock = true>
static inline RAD_SPECHUB_HD void rad_mtp_couple_verify_row(const RadMtpCoupleVerifyCall& c,
int64_t row) {
const auto& s = *reinterpret_cast<const RadMtpCoupleState*>(c.state + row * c.ld[3]);
@@ -130,12 +196,93 @@ static inline RAD_SPECHUB_HD void rad_mtp_couple_verify_row(const RadMtpCoupleVe
if ((p.flags & RAD_SP_GREEDY) || s.draw.seed != p.seed || s.pos != p.pos ||
!rad_mtp_couple_state_valid(s, c.vocab))
return;
if (s.blk >= 1) {
/* kBlock = false: the caller has decided block rows already; the block row's planes are
* big private arrays and would put the whole kernel's scratch request up. */
if constexpr (kBlock) rad_mtp_block_verify_row(c, row);
return;
}
int32_t out = -1;
/* A RheoSampling node (state.scheme, abi/rad_mtp_rrs.h): the greedy multi-draft verifier. */
if (s.scheme != 0) {
if (rad_mtp_rrs_decide_rheo(s, c.idx + row * c.ld[0], c.val + row * c.ld[1], c.n_cand, c.vocab,
p.seed, p.pos, &out))
c.token[row * c.ld[4]] = out;
return;
}
/* A tree node with several drafted children: recursive rejection sampling over them. */
if (s.nkids >= 2) {
if (rad_mtp_rrs_decide(s, c.idx + row * c.ld[0], c.val + row * c.ld[1], c.n_cand, c.vocab,
p.seed, p.pos, &out)) {
c.token[row * c.ld[4]] = out;
}
return;
}
if (rad_mtp_couple_decide(s, c.idx + row * c.ld[0], c.val + row * c.ld[1], c.n_cand, c.vocab,
rad_spechub_uniform(p.seed, p.pos, RAD_SAMPLE_KEY_ACCEPT),
rad_spechub_uniform(p.seed, p.pos, RAD_SAMPLE_KEY_RESID), &out))
c.token[row * c.ld[4]] = out;
}
/* The row-wise verify's gate, which the traversal walk shares: a row is a node the walk may use
* only when the row-wise verify would have accepted its state, so a row that is greedy, unbound or
* stale is a leaf of the walk and keeps the pick's token. */
static inline RAD_SPECHUB_HD bool rad_mtp_trav_eligible(const RadMtpCoupleState& s,
const RadSampleParams& p, int64_t vocab) {
return s.status == 1 && !(p.flags & RAD_SP_GREEDY) && s.draw.seed == p.seed && s.pos == p.pos &&
rad_mtp_couple_state_valid(s, vocab);
}
/* The rows of the request whose root is `row` that the walk reads: the root and the rows after it
* with a parent, at most RAD_MTP_TRAV_MAX_ROWS (a later row is a child no verify carries). */
static inline RAD_SPECHUB_HD int64_t rad_mtp_trav_rows(const RadMtpTravCall& c, int64_t row) {
int64_t n = 1;
while (row + n < c.v.M && n < RAD_MTP_TRAV_MAX_ROWS && c.topo[(row + n) * c.ld_topo] >= 0) ++n;
return n;
}
/* The traversal walk's view of one request's rows: node v is row `root + v`. */
struct RadMtpTravRows {
const RadMtpTravCall& c;
int64_t root;
int64_t n; /* rows of the request */
RAD_SPECHUB_HD const RadMtpCoupleState* state(int v) const {
const int64_t row = root + v;
const auto& s = *reinterpret_cast<const RadMtpCoupleState*>(c.v.state + row * c.v.ld[3]);
const auto& p = *reinterpret_cast<const RadSampleParams*>(c.v.params + row * c.v.ld[2]);
return rad_mtp_trav_eligible(s, p, c.v.vocab) ? &s : nullptr;
}
RAD_SPECHUB_HD bool cand(int v, const int32_t** ids, const float** val, int64_t* nc) const {
*ids = c.v.idx + (root + v) * c.v.ld[0];
*val = c.v.val + (root + v) * c.v.ld[1];
*nc = c.v.n_cand;
return true;
}
RAD_SPECHUB_HD int child(int v, int32_t token) const {
for (int64_t u = (int64_t)v + 1; u < n; ++u) {
const int32_t* t = c.topo + (root + u) * c.ld_topo;
if (t[0] == (int32_t)(root + v) && t[1] == token) return (int)u;
}
return -1;
}
RAD_SPECHUB_HD int64_t vocab() const { return c.v.vocab; }
};
/* One request's tree, if `row` is its root: walk it and write the path's tokens. The host
* reference; the device kernel (spechub.hip, trav_verify) prepares the same tree with a workgroup
* and runs the same walk. */
static inline void rad_mtp_trav_verify_root(const RadMtpTravCall& c, int64_t row) {
if (c.topo[row * c.ld_topo] >= 0) return;
const int64_t n = rad_mtp_trav_rows(c, row);
const RadMtpTravRows T{ c, row, n };
const auto& p = *reinterpret_cast<const RadSampleParams*>(c.v.params + row * c.v.ld[2]);
RadMtpTravOut o;
const bool walked = rad_mtp_trav_walk(T, 0, p.seed, p.pos, &o);
if (!walked) return;
for (int i = 0; i + 1 < o.n; ++i)
c.v.token[(row + o.node[i]) * c.v.ld[4]] = c.topo[(row + o.node[i + 1]) * c.ld_topo + 1];
if (o.has_y) c.v.token[(row + o.node[o.n - 1]) * c.v.ld[4]] = o.y;
}
#undef RAD_SPECHUB_HD
#endif /* RAD_PLUGIN_SPECHUB_H */
+578 -5
View File
@@ -85,6 +85,153 @@ __device__ void couple_live(const int32_t* ids, const float* val, int64_t n, int
__syncthreads();
}
/* The live candidates of a row as the shared decisions read them (rad_mtp_rrs_decide_over,
* rad_mtp_trav_*): the sampler's ids, the compaction (`lj`, ascending candidate order), each live
* candidate's FP64 weight exp(val - top) and its slot in the node's support (count when outside),
* all computed by the workgroup and stored in LDS. */
struct LiveCands {
const int32_t* ids; const short* lj; const double* w; const unsigned char* k; int L;
template <class F> __device__ void each(F&& f) const {
for (int i = 0; i < L; ++i)
if (!f(ids[lj[i]], w[i], (int)k[i])) return;
}
};
/* --draft-verify block (abi/rad_mtp_block.h). The workgroup of a block's round-1 row decides the
* whole block and writes the tokens of rows 0 .. tau; the block's other rows return. Every FP64 sum
* over a candidate plane is the header's fixed lane tree (slot j to lane j % 128, ascending within
* the lane, butterfly over each wave's 32 lanes, the four wave sums as ((0+1)+(2+3))): the
* workgroup computes all exps and lane partials in parallel and thread 0 only runs the 8-wide
* plan, so the result is the host reference's bit for bit (rad_mtp_block_decide). Called by every
* thread of the workgroup. */
static_assert(kThreads == RAD_MTP_BLOCK_LANES && RAD_MTP_BLOCK_WAVE == 32, "the tree's shape");
__device__ void block_verify(const RadMtpCoupleVerifyCall& c, int64_t row) {
constexpr int kW = kThreads / 32;
__shared__ RadMtpCoupleState sst[RAD_MTP_BLOCK_MAX];
__shared__ RadBlockStat sstat[RAD_MTP_BLOCK_MAX];
__shared__ RadBlockPlan plan;
__shared__ float wmax[RAD_MTP_BLOCK_MAX][kW];
__shared__ int wbad[RAD_MTP_BLOCK_MAX][kW];
__shared__ double wacc[RAD_MTP_BLOCK_MAX][kW][10];
__shared__ RadBlockLane lanes[kThreads];
__shared__ int g_s, ok_s, tau_s;
__shared__ int32_t y_s;
const int tid = threadIdx.x, lane = tid & 31, wave = tid >> 5;
if (tid == 0) {
const auto& s0 = *reinterpret_cast<const RadMtpCoupleState*>(c.state + row * c.ld[3]);
g_s = s0.blk;
ok_s = s0.round == 1 && g_s >= 1 && g_s <= RAD_MTP_BLOCK_MAX && row + g_s <= c.M;
if (ok_s) {
const auto& p0 = *reinterpret_cast<const RadSampleParams*>(c.params + row * c.ld[2]);
for (int i = 0; i < g_s && ok_s; ++i) {
sst[i] = *reinterpret_cast<const RadMtpCoupleState*>(c.state + (row + i) * c.ld[3]);
const auto& p = *reinterpret_cast<const RadSampleParams*>(c.params + (row + i) * c.ld[2]);
ok_s = rad_mtp_block_row_ok(sst[0], sst[i], p0, p, i, g_s, c.vocab);
}
}
}
__syncthreads();
if (!ok_s) return;
const int g = g_s;
const int64_t n = c.n_cand;
/* every row's validity and maximum */
for (int i = 0; i < g; ++i) {
const int32_t* ids = c.idx + (row + i) * c.ld[0];
const float* val = c.val + (row + i) * c.ld[1];
int bad = 0;
float mx = -INFINITY;
for (int64_t j = tid; j < n; j += kThreads) {
const int32_t id = ids[j];
const float v = val[j];
if (id >= 0) {
if (id >= c.vocab || v != v || v == INFINITY) bad = 1;
else if (v > mx) mx = v;
}
}
for (int d = 16; d > 0; d >>= 1) {
bad |= __shfl_xor(bad, d);
mx = fmaxf(mx, __shfl_xor(mx, d));
}
if (lane == 0) { wbad[i][wave] = bad; wmax[i][wave] = mx; }
}
__syncthreads();
/* the lane partials and the butterfly of every row */
for (int i = 0; i < g; ++i) {
const int32_t* ids = c.idx + (row + i) * c.ld[0];
const float* val = c.val + (row + i) * c.ld[1];
float top = wmax[i][0];
int bad = wbad[i][0];
for (int w = 1; w < kW; ++w) { top = fmaxf(top, wmax[i][w]); bad |= wbad[i][w]; }
if (bad || top == -INFINITY) return; /* uniform: the same LDS everywhere */
RadBlockAcc a;
for (int k = 0; k < 10; ++k) a.v[k] = 0;
for (int64_t j = tid; j < n; j += kThreads)
if (rad_mtp_block_live(ids[j], val[j])) rad_mtp_block_acc_add(&a, sst[i], ids[j], val[j], top);
for (int d = 16; d > 0; d >>= 1)
for (int k = 0; k < 10; ++k) a.v[k] = a.v[k] + __shfl_xor(a.v[k], d);
if (lane == 0)
for (int k = 0; k < 10; ++k) wacc[i][wave][k] = a.v[k];
}
__syncthreads();
if (tid == 0) {
for (int i = 0; i < g && ok_s; ++i) {
RadBlockAcc t;
for (int k = 0; k < 10; ++k)
t.v[k] = (wacc[i][0][k] + wacc[i][1][k]) + (wacc[i][2][k] + wacc[i][3][k]);
float top = wmax[i][0];
for (int w = 1; w < kW; ++w) top = fmaxf(top, wmax[i][w]);
if (!rad_mtp_block_stat_from(t, 1, top, sst[i], &sstat[i])) ok_s = 0;
}
if (ok_s) {
const auto& p0 = *reinterpret_cast<const RadSampleParams*>(c.params + row * c.ld[2]);
rad_mtp_block_plan(sst, sstat, g, &plan);
tau_s = rad_mtp_block_tau(plan, p0.seed, p0.pos);
}
y_s = -1;
}
__syncthreads();
if (!ok_s) return;
const int tau = tau_s;
const auto& p0 = *reinterpret_cast<const RadSampleParams*>(c.params + row * c.ld[2]);
if (tau < g) {
const int32_t* ids = c.idx + (row + tau) * c.ld[0];
const float* val = c.val + (row + tau) * c.ld[1];
float top = wmax[tau][0];
for (int w = 1; w < kW; ++w) top = fmaxf(top, wmax[tau][w]);
RadBlockLane a{ 0, 0, -1, -1 };
for (int64_t j = tid; j < n; j += kThreads)
if (rad_mtp_block_live(ids[j], val[j]))
rad_mtp_block_lane_add(&a, sst[tau], sstat[tau], plan.p[tau], ids[j], val[j], top);
lanes[tid] = a;
__syncthreads();
if (tid == 0) {
const RadBlockRow r{ ids, val, n, 1, top };
if (!rad_mtp_block_resid_walk(r, sst[tau], sstat[tau], plan.p[tau],
rad_spechub_uniform(p0.seed, p0.pos + (uint64_t)tau,
RAD_SAMPLE_KEY_RESID),
lanes, &y_s))
ok_s = 0;
}
__syncthreads();
if (!ok_s) return;
}
if (tid == 0) rad_mtp_block_write(c, row, sst, tau, g, y_s);
}
/* Above kMaxCand the LDS planes would overflow, so one thread runs the serial helper. The helper
* carries the block row's big private planes, which is why it is a kernel of its own: inside
* couple_verify it gave every launch of that kernel a 17.8 KB/lane scratch request (queue
* OUT_OF_RESOURCES on the first sampled tree). */
__global__ void couple_verify_wide(const int32_t* idx, const float* values, const uint32_t* params,
const uint32_t* state, int32_t* token, int64_t M, int64_t n_cand,
int64_t vocab, int64_t ld0, int64_t ld1, int64_t ld2, int64_t ld3,
int64_t ld4) {
RadMtpCoupleVerifyCall c{ idx, values, params, state, token, M, n_cand, vocab,
{ ld0, ld1, ld2, ld3, ld4 } };
if (blockIdx.x < M && threadIdx.x == 0) rad_mtp_couple_verify_row<true>(c, blockIdx.x);
}
__global__ void couple_verify(const int32_t* idx, const float* values, const uint32_t* params,
const uint32_t* state, int32_t* token, int64_t M, int64_t n_cand,
int64_t vocab, int64_t ld0, int64_t ld1, int64_t ld2, int64_t ld3,
@@ -93,11 +240,7 @@ __global__ void couple_verify(const int32_t* idx, const float* values, const uin
{ ld0, ld1, ld2, ld3, ld4 } };
const int64_t row = blockIdx.x;
if (row >= M) return;
/* Above kMaxCand the LDS planes would overflow, so one thread runs the serial helper. */
if (n_cand > kMaxCand) {
if (threadIdx.x == 0) rad_mtp_couple_verify_row(c, row);
return;
}
/* n_cand <= kMaxCand here: the launch sends wider planes to couple_verify_wide. */
/* `weight` is P's unnormalised FP64 mass per live candidate and `mass` its residual (P-Q)+,
* both indexed by the candidate's place in `lj`. */
__shared__ double weight[kMaxCand], mass[kMaxCand], pq[8], sum;
@@ -115,10 +258,50 @@ __global__ void couple_verify(const int32_t* idx, const float* values, const uin
s.pos == p.pos && rad_mtp_couple_state_valid(s, c.vocab);
__syncthreads();
if (!eligible) return;
/* A chain row of a block-verified draft (--draft-verify block). */
if (s.blk >= 1) {
#ifdef RAD_BLOCK_REPEAT
/* microbenchmark build: the block is decided RAD_BLOCK_REPEAT times back to back (idempotent) */
for (int rep = 0; rep < RAD_BLOCK_REPEAT; ++rep) { block_verify(c, row); __syncthreads(); }
#else
block_verify(c, row);
#endif
return;
}
/* A RheoSampling node (abi/rad_mtp_rrs.h): one thread runs the shared serial helper, which is
* the host reference itself. Only a node with a RheoSampling scheme takes it. */
if (s.scheme != 0) {
if (threadIdx.x == 0) rad_mtp_couple_verify_row<false>(c, row);
return;
}
couple_live(ids, val, c.n_cand, c.vocab, lj, &nl, &valid, &top);
if (!valid || top == -INFINITY) return;
const int L = nl;
for (int i = threadIdx.x; i < L; i += blockDim.x) weight[i] = ::exp((double)val[lj[i]] - top);
/* A tree node with several drafted children (abi/rad_mtp_rrs.h, recursive rejection sampling):
* the workgroup has compacted the live candidates and computed every weight and every
* candidate's slot in the node's support; one lane then runs the host reference's own decision
* (rad_mtp_rrs_decide_over) over those stored values, in the same order, so the token is the
* host's bit for bit. */
if (s.nkids >= 2) {
__shared__ unsigned char kslot[kMaxCand];
__shared__ double pq_x[8], sum_x;
for (int i = threadIdx.x; i < L; i += blockDim.x) {
const int32_t id = ids[lj[i]];
int k = 0;
while (k < s.count && id != s.ids[k]) ++k;
kslot[i] = (unsigned char)k;
}
__syncthreads();
if (threadIdx.x == 0) {
int32_t out = -1;
if (rad_mtp_rrs_decide_over(s, LiveCands{ ids, lj, weight, kslot, L }, p.seed, p.pos, &out,
pq_x, &sum_x)) {
c.token[row * c.ld[4]] = out;
}
}
return;
}
__syncthreads();
if (threadIdx.x == 0) {
sum = 0;
@@ -354,6 +537,8 @@ __global__ __launch_bounds__(kDfThreads) void dflash_couple(
s.round = (int)l + 1;
s.status = -1;
s.count = 0;
s.nkids = 0; /* the walk's position is one draw: the chain's wire */
s.scheme = 0;
for (int i = 0; i < 8; ++i) { s.ids[i] = -1; s.q[i] = 0; }
for (int i = 0; i < sn; ++i)
if (wq[i] > 0) { s.ids[s.count] = lid[i]; s.q[s.count++] = wq[i]; }
@@ -402,12 +587,400 @@ int rad_mine_mtp_couple_propose_launch(const RadArgs* a, RadStream stream) {
return R4D_EMIT(stream, propose, dim3((unsigned)((c.M + 63) / 64)), dim3(64), 0, c);
}
/* ------------------------------------------------------------------ traversal verification
*
* abi/rad_mtp_traversal.h, one workgroup a row; only a request's root row (topo parent -1) does
* work, and it decides the whole request. Its four waves each take every fourth row of the request
* and need nothing from each other until the tree is complete:
*
* per row, one wave: lane 0 reads the row's state (the gate, the canonical draft side) into LDS
* and hands the wave what it needs of it in registers; a lane a later row finds the node's
* child rows in the step's topology; the wave reads the row's candidate
* plane a pass of 32 slots at a time, one slot a lane (a top_k 20 plane is one live pass): the
* largest value and the validity, reduced over the wave in registers, then the integer masses
* (rad_mtp_trav_mass) of the passes that have a live slot, summed in all and on the node's
* support slots and reduced the same way -- integer sums, so the order is free and the totals
* are the host's serial ones bit for bit; lanes 0..7 turn them into the node's law, mass /
* total,
* one barrier, the kernel's only one;
* wave 0, lane 0: the walk itself (rad_mtp_trav_walk_tree, the host's code over
* the tree in LDS: one FP64 division to enter a node, two and a few dozen multiply-adds when a
* child is rejected), the path's
* tokens and the final token's slot or off-support unit; then the wave finds the off-support
* candidate holding that unit (a prefix over the lanes of each pass, in registers).
*
* Lanes talk to each other in two ways only: through LDS ACROSS the barrier, and through registers
* (v_readlane, DPP) inside a wave. A lane never reads from LDS what another lane wrote since the last
* barrier: the hardware would order the two accesses of one wave, but the compiler, which sees a
* thread at a time, may move the read before the write. No candidate plane is copied, no lane walks a
* plane alone at any width, and the kernel has no scratch segment (nothing private is indexed at run
* time). Tokens and paths are the host reference's bit for bit (rad_mtp_trav_verify_root). */
namespace {
template <int N>
__device__ __forceinline__ uint32_t tv_xor_lane(uint32_t v) {
/* lane ^ N's value on DPP (row_xmask; 16 crosses the wave's two rows): every lane must be active */
if constexpr (N < 16)
return (uint32_t)__builtin_amdgcn_update_dpp(0, (int)v, 0x160 | N, 0xf, 0xf, true);
else
return (uint32_t)__builtin_amdgcn_permlanex16((int)v, (int)v, 0x76543210u, 0xFEDCBA98u, false, false);
}
template <int N>
__device__ __forceinline__ uint64_t tv_xor_add(uint64_t v) {
return v + (((uint64_t)tv_xor_lane<N>((uint32_t)(v >> 32)) << 32) | tv_xor_lane<N>((uint32_t)v));
}
/* The wave's sum of a 64-bit integer, in every lane (integers: the order is free). */
__device__ __forceinline__ uint64_t tv_wave_sum(uint64_t v) {
v = tv_xor_add<16>(v); v = tv_xor_add<8>(v); v = tv_xor_add<4>(v);
v = tv_xor_add<2>(v); v = tv_xor_add<1>(v);
return v;
}
__device__ __forceinline__ uint32_t tv_wave_or(uint32_t v) {
v |= tv_xor_lane<16>(v); v |= tv_xor_lane<8>(v); v |= tv_xor_lane<4>(v);
v |= tv_xor_lane<2>(v); v |= tv_xor_lane<1>(v);
return v;
}
__device__ __forceinline__ float tv_fmax_lane(float a, uint32_t b) { return fmaxf(a, __uint_as_float(b)); }
__device__ __forceinline__ float tv_wave_max(float v) {
v = tv_fmax_lane(v, tv_xor_lane<16>(__float_as_uint(v)));
v = tv_fmax_lane(v, tv_xor_lane<8>(__float_as_uint(v)));
v = tv_fmax_lane(v, tv_xor_lane<4>(__float_as_uint(v)));
v = tv_fmax_lane(v, tv_xor_lane<2>(__float_as_uint(v)));
v = tv_fmax_lane(v, tv_xor_lane<1>(__float_as_uint(v)));
return v;
}
/* Lane 0's value in every lane of the wave (v_readlane): how one lane hands the wave a scalar
* without LDS. Every lane must be active. */
__device__ __forceinline__ int32_t tv_lane0(int32_t v) { return __builtin_amdgcn_readlane(v, 0); }
__device__ __forceinline__ float tv_lane0f(float v) {
return __uint_as_float((uint32_t)__builtin_amdgcn_readlane((int)__float_as_uint(v), 0));
}
/* A wave's inclusive prefix sum over its lanes (row_shr 1, 2, 4, 8 inside each row of sixteen, then
* the first row's total into the second). Every lane must be active; sums below 2^32. */
__device__ __forceinline__ uint32_t tv_wave_prefix(uint32_t x, int lane) {
x += (uint32_t)__builtin_amdgcn_update_dpp(0, (int)x, 0x111, 0xF, 0xF, true);
x += (uint32_t)__builtin_amdgcn_update_dpp(0, (int)x, 0x112, 0xF, 0xF, true);
x += (uint32_t)__builtin_amdgcn_update_dpp(0, (int)x, 0x114, 0xF, 0xF, true);
x += (uint32_t)__builtin_amdgcn_update_dpp(0, (int)x, 0x118, 0xF, 0xF, true);
const uint32_t lo = (uint32_t)__builtin_amdgcn_readlane((int)x, 15);
return lane >= 16 ? x + lo : x;
}
constexpr int kTravRows = RAD_MTP_TRAV_MAX_ROWS;
constexpr int kTravWaves = kThreads / 32;
constexpr int kTravPer = 8; /* passes a batch */
/* The op's operands as the kernel takes them: 32-bit geometry (the launch refuses anything wider),
* which is half the scalar registers of 64-bit strides. */
struct TravArgs {
const int32_t* idx;
const float* val;
const uint32_t* params;
const uint32_t* state;
int32_t* token;
const int32_t* topo;
uint32_t M, n_cand, vocab, ld0, ld1, ld2, ld3, ld4, ld_topo;
};
/* Eight slots of a row's plane, in registers. Lane l's slots are l, l + 32, l + 64, ... (pass p is
* slots [32 p, 32 p + 32), one a lane), so the plane's live head -- 20 slots at top_k 20 -- is one
* pass of the wave whatever its width; a batch is eight passes, sixteen loads issued together (a loop
* that loaded a pass at a time would wait out a memory latency per pass). */
struct TvBatch {
int32_t id[kTravPer];
float v[kTravPer];
};
/* Passes p0 .. p0 + 8 of lane `lane` of a row of n slots; holes past the row's end. `full`: the batch
* lies inside the row for every lane, so the loads are unconditional. The ids first: a batch of holes
* (every batch but the first at top_k 20) is known from them alone, and its values are not read. Returns
* whether any lane of the wave has an id in the batch. */
__device__ __forceinline__ bool tv_load(const int32_t* ids, const float* val, int lane, uint32_t p0,
uint32_t n, bool full, TvBatch* c) {
const uint32_t j = (uint32_t)lane + 32u * p0;
bool on = false;
if (full) {
#pragma unroll
for (int i = 0; i < kTravPer; ++i) { c->id[i] = ids[j + 32u * i]; on = on || c->id[i] >= 0; }
} else {
#pragma unroll
for (int i = 0; i < kTravPer; ++i) {
c->id[i] = -1;
if (j + 32u * i < n) c->id[i] = ids[j + 32u * i];
on = on || c->id[i] >= 0;
}
}
#pragma unroll
for (int i = 0; i < kTravPer; ++i) c->v[i] = -INFINITY;
if (!__ballot(on)) return false;
if (full) {
#pragma unroll
for (int i = 0; i < kTravPer; ++i) c->v[i] = val[j + 32u * i];
} else {
#pragma unroll
for (int i = 0; i < kTravPer; ++i)
if (j + 32u * i < n) c->v[i] = val[j + 32u * i];
}
return true;
}
/* One request, by its root row's workgroup. Called by every thread. */
__device__ __forceinline__ void trav_request(const TravArgs& c, uint32_t row) {
__shared__ RadTravTree T;
__shared__ int32_t s_tok[kTravRows];
__shared__ int s_path[RAD_MTP_TRAV_MAX_DEPTH + 1]; /* lane 0's alone */
const int tid = threadIdx.x, lane = tid & 31, wave = tid >> 5;
const uint32_t avail = c.M - row < (uint32_t)kTravRows ? c.M - row : (uint32_t)kTravRows;
const uint32_t nc = c.n_cand;
const uint32_t npass = (nc + 31u) / 32u;
const int qb = rad_mtp_trav_qbits(nc);
/* The rows of the request: the root and the rows after it that have a parent (a lane a row). */
uint64_t has = 1;
for (uint32_t u0 = 0; u0 < avail; u0 += 32) {
const uint32_t u = u0 + (uint32_t)lane;
bool in = false;
if (u >= 1 && u < avail) in = c.topo[(uint64_t)(row + u) * c.ld_topo] >= 0;
has |= (uint64_t)(uint32_t)__ballot(in) << u0;
}
const int n = ~has ? (int)__builtin_ctzll(~has) : 64;
/* This wave's rows: the node, the row's law. */
for (int v = wave; v < n; v += kTravWaves) {
const uint64_t rr = (uint64_t)row + (uint32_t)v;
int elig = 0, nkv = 0;
int32_t cid[8];
for (int k = 0; k < 8; ++k) cid[k] = -1;
if (lane == 0) {
s_tok[v] = c.topo[rr * c.ld_topo + 1];
/* the state by value: its words are loaded together and stay in registers (nothing below
* indexes it at run time), instead of one load a check */
const RadMtpCoupleState s = *reinterpret_cast<const RadMtpCoupleState*>(c.state + rr * c.ld3);
const RadSampleParams p = *reinterpret_cast<const RadSampleParams*>(c.params + rr * c.ld2);
double q[8];
int nk = 0, nt = -1, tail = 0, swap = 0;
elig = rad_mtp_trav_eligible(s, p, c.vocab) &&
rad_mtp_trav_canon(s, 0, cid, q, &nk, &nt, &tail, &swap);
for (int k = 0; k < 8; ++k) {
if (!elig) cid[k] = -1;
T.ids[v][k] = cid[k];
T.qr[v][k] = elig ? q[k] : 0.0;
}
nkv = elig ? nk : 0;
T.nk[v] = nkv;
T.nt[v] = nt;
T.tail[v] = tail;
T.ci[v] = 0;
}
/* lane 0's findings, to the wave in registers */
elig = tv_lane0(elig);
nkv = tv_lane0(nkv);
int32_t sid[8];
for (int k = 0; k < 8; ++k) sid[k] = tv_lane0(cid[k]);
/* the node's child rows: a lane a later row of the request, the first row under this node
* that carries the slot's token */
int chrow[8];
for (int k = 0; k < 8; ++k) chrow[k] = -1;
if (nkv > 0) {
for (int u0 = v + 1; u0 < n; u0 += 32) {
const int u = u0 + lane;
int32_t upar = -2, utok = -1;
if (u < n) {
upar = c.topo[((uint64_t)row + (uint32_t)u) * c.ld_topo];
utok = c.topo[((uint64_t)row + (uint32_t)u) * c.ld_topo + 1];
}
const bool under = u < n && upar == (int32_t)(row + (uint32_t)v);
#pragma unroll
for (int k = 0; k < 8; ++k) {
const uint32_t mk = (uint32_t)__ballot(under && utok == sid[k]);
if (mk && chrow[k] < 0 && k < nkv) chrow[k] = u0 + (int)__builtin_ctz(mk);
}
}
}
if (lane == 0)
for (int k = 0; k < 8; ++k) T.child[v][k] = chrow[k];
const int32_t* ids = c.idx + rr * c.ld0;
const float* val = c.val + rr * c.ld1;
float top = -INFINITY;
uint32_t bad = 0;
uint64_t sum = 0, pk[8];
for (int k = 0; k < 8; ++k) pk[k] = 0;
if (elig) {
/* the largest value and the validity */
for (uint32_t p0 = 0; p0 < npass; p0 += kTravPer) {
TvBatch x;
if (!tv_load(ids, val, lane, p0, nc, 32u * (p0 + kTravPer) <= nc, &x)) continue;
#pragma unroll
for (int i = 0; i < kTravPer; ++i) {
const bool on = x.id[i] >= 0;
const bool inval = on && ((uint32_t)x.id[i] >= c.vocab || x.v[i] != x.v[i] || x.v[i] == INFINITY);
bad |= inval ? 1u : 0u;
top = on && !inval && x.v[i] > top ? x.v[i] : top;
}
}
top = tv_wave_max(top);
bad = tv_wave_or(bad);
/* the masses: in all, and on the support slots */
if (!bad && top != -INFINITY) {
for (uint32_t p0 = 0; p0 < npass; p0 += kTravPer) {
TvBatch x;
/* a batch of holes (top_k 20: all but the first) adds nothing */
if (!tv_load(ids, val, lane, p0, nc, 32u * (p0 + kTravPer) <= nc, &x)) continue;
#pragma unroll
for (int i = 0; i < kTravPer; ++i) {
const bool live = x.id[i] >= 0 && x.v[i] != -INFINITY;
if (!__ballot(live)) continue; /* nor does a pass with none (top_k 20: all but one) */
const uint64_t m = live ? rad_mtp_trav_mass(x.v[i], top, qb) : 0;
sum += m;
/* the support's ids sit in a few slots of the plane: a pass none of them is in
* costs the eight compares */
bool eq[8], anyeq = false;
#pragma unroll
for (int k = 0; k < 8; ++k) { eq[k] = x.id[i] == sid[k]; anyeq = anyeq || eq[k]; }
if (__ballot(anyeq)) {
#pragma unroll
for (int k = 0; k < 8; ++k) pk[k] += eq[k] ? m : 0;
}
}
}
}
sum = tv_wave_sum(sum);
#pragma unroll
for (int k = 0; k < 8; ++k) pk[k] = tv_wave_sum(pk[k]);
}
/* the node's law (rad_mtp_trav_set_law's values): lane k the support slot k, lane 0 the
* scalars; every word of the tree is written once, by one lane */
if (lane < 8) {
const bool valid = elig && !bad && top != -INFINITY && sum > 0;
uint64_t on = 0, mine = 0;
for (int k = 0; k < 8; ++k) { on += pk[k]; mine = k == lane ? pk[k] : mine; }
const double dsum = valid ? rad_mtp_trav_u2d(sum) : 1.0;
const double rk = valid ? rad_mtp_trav_u2d(mine) / dsum : 0.0;
T.m[v][lane] = rk;
if (lane == 0) {
const uint64_t off = sum - on;
T.moff[v] = valid ? rad_mtp_trav_u2d(off) / dsum : 0.0;
T.off[v] = valid ? off : 0;
T.top[v] = top;
T.ok[v] = valid;
}
}
}
__syncthreads(); /* the tree is complete */
if (wave != 0) return;
/* Lane 0: the walk (the host's code over the tree in LDS), the path's tokens. */
int kind = 0, fnode = 0;
uint32_t want_lo = 0, want_hi = 0;
float ftop = 0.0f;
int32_t fid[8];
for (int k = 0; k < 8; ++k) fid[k] = -1;
if (lane == 0) {
const auto& p0 = *reinterpret_cast<const RadSampleParams*>(c.params + (uint64_t)row * c.ld2);
const uint64_t seed = p0.seed, pos0 = p0.pos;
int np = 0;
if (rad_mtp_trav_walk_tree(T, seed, pos0, s_path, &np)) {
const int f = s_path[np - 1];
for (int i = 0; i + 1 < np; ++i)
c.token[((uint64_t)row + (uint32_t)s_path[i]) * c.ld4] = s_tok[s_path[i + 1]];
if (T.ok[f]) {
const RadTravPick pk = rad_mtp_trav_final_pick(
T, f, rad_spechub_uniform(seed, pos0 + (uint64_t)(np - 1), RAD_SAMPLE_KEY_RESID));
if (pk.kind == 1) c.token[((uint64_t)row + (uint32_t)f) * c.ld4] = T.ids[f][pk.slot];
if (pk.kind == 2) {
kind = 2;
fnode = f;
want_lo = (uint32_t)pk.t;
want_hi = (uint32_t)(pk.t >> 32);
ftop = T.top[f];
for (int k = 0; k < 8; ++k) fid[k] = T.ids[f][k];
}
}
}
}
if (tv_lane0(kind) != 2) return;
/* The final token off the support: the candidate holding unit `want`, candidates in slot order
* (pass by pass, the lanes of a pass in order). Lane 0's finding goes to the wave in registers; a
* mass is split in two limbs so its prefix over the lanes is two 32-bit DPP scans. */
{
const int f = tv_lane0(fnode);
const uint64_t want = ((uint64_t)(uint32_t)tv_lane0((int32_t)want_hi) << 32) | (uint32_t)tv_lane0((int32_t)want_lo);
const float top = tv_lane0f(ftop);
int32_t sid[8];
for (int k = 0; k < 8; ++k) sid[k] = tv_lane0(fid[k]);
const uint64_t rr = (uint64_t)row + (uint32_t)f;
const int32_t* ids = c.idx + rr * c.ld0;
const float* val = c.val + rr * c.ld1;
uint64_t before = 0; /* the off-support mass of the passes already walked: the same in every lane */
bool done = false;
for (uint32_t p0 = 0; p0 < npass && !done; p0 += kTravPer) {
TvBatch x;
if (!tv_load(ids, val, lane, p0, nc, 32u * (p0 + kTravPer) <= nc, &x)) continue;
#pragma unroll
for (int i = 0; i < kTravPer; ++i) {
bool off = x.id[i] >= 0 && x.v[i] != -INFINITY;
#pragma unroll
for (int k = 0; k < 8; ++k) off = off && x.id[i] != sid[k];
if (done || !__ballot(off)) continue;
const uint64_t m = off ? rad_mtp_trav_mass(x.v[i], top, qb) : 0;
/* a mass is at most 2^41: limbs of 24 and 17 bits, whose prefixes over 32 lanes stay
* below 2^32 */
const uint32_t plo = tv_wave_prefix((uint32_t)m & 0xffffffu, lane);
const uint32_t phi = tv_wave_prefix((uint32_t)(m >> 24), lane);
const uint64_t incl = ((uint64_t)phi << 24) + plo;
const uint64_t all = ((uint64_t)(uint32_t)__builtin_amdgcn_readlane((int)phi, 31) << 24) +
(uint32_t)__builtin_amdgcn_readlane((int)plo, 31);
const uint64_t base = before + incl - m;
const bool mine = m > 0 && want >= base && want - base < m;
if (mine) c.token[rr * c.ld4] = x.id[i];
done = __ballot(mine) != 0;
before += all;
}
}
}
}
} /* namespace */
__global__ __launch_bounds__(kThreads) void trav_verify(
const int32_t* idx, const float* values, const uint32_t* params, const uint32_t* state,
int32_t* token, const int32_t* topo, uint32_t M, uint32_t n_cand, uint32_t vocab, uint32_t ld0,
uint32_t ld1, uint32_t ld2, uint32_t ld3, uint32_t ld4, uint32_t ld_topo) {
const uint32_t row = blockIdx.x;
if (row >= M) return;
if (topo[(uint64_t)row * ld_topo] >= 0) return; /* not a request's root */
const TravArgs c{ idx, values, params, state, token, topo, M, n_cand, vocab, ld0, ld1, ld2, ld3, ld4, ld_topo };
#ifdef RAD_TRAV_REPEAT
/* microbenchmark build: the request is decided RAD_TRAV_REPEAT times back to back (idempotent) */
for (int rep = 0; rep < RAD_TRAV_REPEAT; ++rep) { trav_request(c, row); __syncthreads(); }
#else
trav_request(c, row);
#endif
}
int rad_mine_mtp_trav_verify_launch(const RadArgs* a, RadStream stream) {
RadMtpTravCall c{};
const int rc = rad_mtp_trav_verify_args(a, &c);
if (rc != RAD_OK) return rc;
/* the kernel's geometry is 32-bit */
for (int i = 0; i < 5; ++i)
if (c.v.ld[i] < 0 || c.v.ld[i] > INT32_MAX) return RAD_E_UNSUPPORTED;
if (c.ld_topo < 0 || c.ld_topo > INT32_MAX) return RAD_E_UNSUPPORTED;
return R4D_EMIT(stream, trav_verify, dim3((unsigned)c.v.M), dim3(kThreads), 0,
c.v.idx, c.v.val, c.v.params, c.v.state, c.v.token, c.topo, c.v.M, c.v.n_cand,
c.v.vocab, c.v.ld[0], c.v.ld[1], c.v.ld[2], c.v.ld[3], c.v.ld[4], c.ld_topo);
}
int rad_mine_mtp_couple_verify_launch(const RadArgs* a, RadStream stream) {
RadMtpCoupleVerifyCall c{};
const int rc = rad_mtp_couple_verify_args(a, &c);
if (rc != RAD_OK) return rc;
/* Separate pointer arguments let graph replay rebind each operand address independently: a
* struct on the host would freeze the addresses it was built with. */
if (c.n_cand > kMaxCand)
return R4D_EMIT(stream, couple_verify_wide, dim3((unsigned)c.M), dim3(1), 0,
c.idx, c.val, c.params, c.state, c.token, c.M, c.n_cand, c.vocab,
c.ld[0], c.ld[1], c.ld[2], c.ld[3], c.ld[4]);
return R4D_EMIT(stream, couple_verify, dim3((unsigned)c.M), dim3(kThreads), 0,
c.idx, c.val, c.params, c.state, c.token, c.M, c.n_cand, c.vocab,
c.ld[0], c.ld[1], c.ld[2], c.ld[3], c.ld[4]);