Archived
radiance 1.7.0
- every model serves without a GPU: libavx covers every op, so `--kernels libavx` alone serves every shipped container - Qwen3-ASR (zivius): speech to text on /v1/audio/transcriptions and live over /v1/realtime - /v1/messages works with Claude Code as it ships (gearwave00001) - a long agent conversation keeps its cached history when the client rewrites its last turn (manueloverride) - a stream's first event goes out with the first token, so llama-benchy measures prefill right - opt-in --prefill-interleave (Smellyyyy, baKon) and --prefill-span (keptn42) - four-rank TP releases the link early; four-rank selftests and docs/TP4.md (goodman-b) - NVFP4 and FP8 compressed-tensors imports (devmilan); tools/rad-export for finetunes and LoRA adapters (baKon) - many fixes, among them lost tokens on stop strings under concurrency, keep-alive, MiniCPM5 tool arguments and the CPU's vision and audio encoders - plugin ABI 17 and changed op schemas: out-of-tree plugins must be rebuilt
This commit is contained in:
1 parent
b068190326
commit
1e968f6e1d
739 files changed
+128447
-84632
No files matched your search
@@ -1,10 +1,9 @@
|
||||
// DFlash2's candidate selector: the edge scores and the path walk, one workgroup a sequence.
|
||||
// DFlash2's candidate selector: edge scores and the path walk, one workgroup per sequence.
|
||||
//
|
||||
// A block-diffusion drafter proposes every position of a block in ONE backbone pass, so it has no
|
||||
// way to condition position l+1 on what it chose at position l -- the two were computed together.
|
||||
// The selector is what puts that dependence back. Each position keeps the draft head's top K
|
||||
// candidates and a low-rank bilinear form scores every (predecessor, candidate) EDGE between
|
||||
// consecutive positions; a greedy walk over those edges picks one token per position.
|
||||
// A block-diffusion drafter proposes every position of a block in one backbone pass, so position
|
||||
// l+1 cannot condition on the choice at l. The selector restores that dependence: each position
|
||||
// keeps the draft head's top K candidates, a low-rank bilinear form scores each (predecessor,
|
||||
// candidate) edge between consecutive positions, and a greedy walk picks one token per position.
|
||||
//
|
||||
// score[s, l, c] = unary[s, l, c]
|
||||
// + sum_r pred[pid(s,l)][r] * hp[s,l][r] * succ[cand[s,l,c]][r]
|
||||
@@ -12,26 +11,20 @@
|
||||
// pid(s, 0) = anchor[s * anchor_stride] the token the target just committed
|
||||
// pid(s, l) = cand[s, l-1, chosen(s, l-1)] the walk's own previous pick
|
||||
//
|
||||
// `hp` IS OPTIONAL, AND ABSENT IT IS ONES -- which drops the trilinear form to a plain BIGRAM one,
|
||||
// score = unary + <pred[pid], succ[cand]>, with no dependence on the hidden state at all. That is
|
||||
// not a degenerate case: it is exactly DSpark's Markov head, a rank-256 learned
|
||||
// bigram bias `markov_w2 @ markov_w1[prev]` added to the draft logits, whose previous token is the
|
||||
// anchor at position 0 and the drafter's own last pick after it. The head is a full-vocabulary
|
||||
// [rank, n_vocab] GEMM per position in the reference and it is SERIAL in exactly the way this walk
|
||||
// already is, so running it here costs the candidate rows of two codebooks instead of seven passes
|
||||
// over 66 MiB -- and the greedy argmax over the base head's top K is what makes that exact enough
|
||||
// to matter only to acceptance (a draft is verified; see the 2-bit head's argument).
|
||||
// `hp` is optional; absent means ones, giving the bigram form score = unary + <pred[pid],
|
||||
// succ[cand]>. That is DSpark's Markov head (a rank-256 learned bigram bias
|
||||
// markov_w2 @ markov_w1[prev] on the draft logits, prev = anchor at position 0, then the last
|
||||
// pick). The reference runs it as a full-vocabulary [rank, n_vocab] GEMM per position, serially;
|
||||
// here it costs only the candidate rows of two codebooks. Restricting to the base head's top K
|
||||
// affects only acceptance, since drafts are verified.
|
||||
//
|
||||
// THE WALK IS SERIAL IN l and that is the whole reason this is a kernel and not a batched GEMM
|
||||
// followed by an argmax. The reference materialises the full [steps, K, K] score tensor -- vLLM's
|
||||
// `_score_edges` einsum -- of which exactly one row per step is ever read, because the predecessor
|
||||
// index is not known until the previous step has been decided. Scoring K*K edges to use K of them
|
||||
// is 16x the work and a round trip per step; here the predecessor's codebook row is fetched after
|
||||
// the previous step's argmax, inside one workgroup, with no launch between.
|
||||
// The walk is serial in l, which is why this is a kernel rather than a batched GEMM plus argmax.
|
||||
// The reference (vLLM's _score_edges einsum) materialises [steps, K, K] scores and reads one row
|
||||
// per step, since the predecessor is only known after the previous step. Here the predecessor's
|
||||
// codebook row is fetched after the previous argmax, inside one workgroup.
|
||||
//
|
||||
// The arithmetic is tiny -- steps * K * R multiply-adds a sequence, 28K at the shipped 7/16/256 --
|
||||
// so this is a latency kernel, not a throughput one. It is written for one workgroup a sequence
|
||||
// with no global synchronisation at all.
|
||||
// The arithmetic is tiny (steps * K * R multiply-adds per sequence, 28K at 7/16/256), so this is a
|
||||
// latency kernel: one workgroup per sequence, no global synchronisation.
|
||||
#include <hip/hip_runtime.h>
|
||||
#include "r4d_gdn_args.h"
|
||||
#include "r4d_dflash_conv_body.h" // r4d_bf2f, r4d_u16
|
||||
@@ -51,11 +44,9 @@
|
||||
// succ [vocab, R] bf16 successor codebook
|
||||
// out [M, out_ld] i32 the chosen token per step
|
||||
//
|
||||
// AN ID OUTSIDE [0, V) IS NOT A CODEBOOK ROW, and one reaches this kernel: row_topk_merge reports a
|
||||
// slot no rank could fill as id -1, and step 0's predecessor is whatever the anchor plane holds. So
|
||||
// the codebook reads are guarded exactly as ref_dflash_select guards them -- a predecessor out of
|
||||
// range contributes nothing, and a candidate out of range scores its unary term alone -- rather
|
||||
// than gathering a row from before or past the codebook.
|
||||
// Ids outside [0, V) do occur (row_topk_merge reports an unfilled slot as -1, and step 0's
|
||||
// predecessor is whatever the anchor plane holds). As in ref_dflash_select, an out-of-range
|
||||
// predecessor contributes nothing and an out-of-range candidate scores its unary term alone.
|
||||
__global__ __launch_bounds__(DSL_THREADS) void r4d_dflash_select_kernel(
|
||||
const int* __restrict__ cand, const float* __restrict__ unary,
|
||||
const r4d_u16* __restrict__ hp, const int* __restrict__ anchor,
|
||||
@@ -68,21 +59,19 @@ __global__ __launch_bounds__(DSL_THREADS) void r4d_dflash_select_kernel(
|
||||
const int lane = tid & (DSL_WAVE - 1);
|
||||
const int wave = tid / DSL_WAVE;
|
||||
|
||||
// a[r] = pred[pid][r] * hp[r] -- the half of the bilinear form that does not depend on the
|
||||
// candidate, so it is computed once per step and read K times out of LDS.
|
||||
// a[r] = pred[pid][r] * hp[r]: the candidate-independent half, computed once per step.
|
||||
__shared__ float sa[DSL_MAX_R];
|
||||
__shared__ float ssc[DSL_MAX_K];
|
||||
__shared__ int spid; // the token the previous step chose: this step's predecessor
|
||||
|
||||
// THE CANDIDATES A WAVE SCORES, at most this many: c = wave, wave + DSL_WAVES, ...
|
||||
// Candidates per wave, at most: c = wave, wave + DSL_WAVES, ...
|
||||
constexpr int CPW = (DSL_MAX_K + DSL_WAVES - 1) / DSL_WAVES;
|
||||
|
||||
for (int l = 0; l < steps; ++l) {
|
||||
const long row = (long)s * steps + l;
|
||||
// THE STEP'S OWN READS GO FIRST. The candidates' ids and unary scores do not depend on the
|
||||
// predecessor, so they are in flight with its codebook row instead of behind the barrier, each
|
||||
// a round trip of its own in a walk that is nothing but round trips. A wave past K re-reads
|
||||
// the last candidate and scores nothing.
|
||||
// Candidate ids and unary scores do not depend on the predecessor, so load them first,
|
||||
// overlapping the codebook row fetch. A wave past K re-reads the last candidate and scores
|
||||
// nothing.
|
||||
int cid[CPW];
|
||||
float un[CPW];
|
||||
#pragma unroll
|
||||
@@ -92,15 +81,13 @@ __global__ __launch_bounds__(DSL_THREADS) void r4d_dflash_select_kernel(
|
||||
cid[i] = cand[at];
|
||||
un[i] = unary[at];
|
||||
}
|
||||
// Step 0's predecessor is the committed anchor; every later step's is the token this walk
|
||||
// picked last time round, which thread 0 left in LDS before the barrier that ended that step.
|
||||
// Step 0's predecessor is the committed anchor; later steps use the previous pick, left in
|
||||
// LDS by thread 0 before the barrier that ended that step.
|
||||
const int pid = (l == 0) ? anchor[(long)s * anchor_stride] : spid;
|
||||
const bool pid_ok = pid >= 0 && pid < V;
|
||||
|
||||
const r4d_u16* __restrict__ prow = pred + (long)(pid_ok ? pid : 0) * R;
|
||||
/* hp absent is hp == 1: the predecessor's codebook row goes in untouched. Branching outside
|
||||
* the loop rather than multiplying by a materialised plane of ones keeps the bigram case from
|
||||
* reading R bf16 a step for a number it already knows. */
|
||||
/* hp absent means ones: the predecessor's row is used as is, without reading a plane. */
|
||||
if (!pid_ok) {
|
||||
for (int r = tid; r < R; r += DSL_THREADS) sa[r] = 0.0f;
|
||||
} else if (hp) {
|
||||
@@ -113,10 +100,9 @@ __global__ __launch_bounds__(DSL_THREADS) void r4d_dflash_select_kernel(
|
||||
}
|
||||
__syncthreads();
|
||||
|
||||
// One wave a candidate. R is a multiple of the wave, so the strided loop is balanced and the
|
||||
// reduction is five xor-butterfly steps with no barrier inside it. A candidate out of range
|
||||
// reads codebook row 0 and drops the sum, so the row's loads are never in a branch of their
|
||||
// own; its score is its unary term alone either way.
|
||||
// One wave per candidate. R is a multiple of the wave size, so the reduction is a barrier-free
|
||||
// xor butterfly. An out-of-range candidate reads row 0 and drops the sum, keeping the loads
|
||||
// out of a branch.
|
||||
#pragma unroll
|
||||
for (int i = 0; i < CPW; ++i) {
|
||||
const int c = wave + i * DSL_WAVES;
|
||||
@@ -132,9 +118,9 @@ __global__ __launch_bounds__(DSL_THREADS) void r4d_dflash_select_kernel(
|
||||
}
|
||||
__syncthreads();
|
||||
|
||||
// K is at most 32 and the walk is serial anyway, so the argmax is one thread. Ties go to the
|
||||
// lower index, which is `row_topk`'s own tie rule and therefore the more likely candidate.
|
||||
// Seeded below every score, as ref seeds it, so a NaN in the first slot cannot win by position.
|
||||
// K <= 32 and the walk is serial, so one thread does the argmax. Ties go to the lower index
|
||||
// (row_topk's rule, the more likely candidate). Seeded at -inf, as in ref, so a NaN in the
|
||||
// first slot cannot win by position.
|
||||
if (tid == 0) {
|
||||
int best = 0;
|
||||
float bv = -INFINITY;
|
||||
@@ -144,8 +130,7 @@ __global__ __launch_bounds__(DSL_THREADS) void r4d_dflash_select_kernel(
|
||||
spid = tok;
|
||||
out[(long)s * out_ld + l] = tok;
|
||||
}
|
||||
// Both directions: the next step reads `spid` and overwrites `sa`, and every other thread
|
||||
// is still reading this step's.
|
||||
// The next step reads spid and overwrites sa, which other threads may still be reading.
|
||||
__syncthreads();
|
||||
}
|
||||
}
|
||||
@@ -161,7 +146,7 @@ extern "C" int r4d_dflash_select_bf16(
|
||||
if (R < DSL_WAVE || R > DSL_MAX_R || (R % DSL_WAVE)) return -1;
|
||||
if (V < 1) return -1;
|
||||
if (cand_ld < K || out_ld < steps || anchor_stride < 1) return -1;
|
||||
/* `hp` may be null -- the bigram form -- and then there is no plane to have a pitch. */
|
||||
/* hp may be null (the bigram form); then hp_ld is unused. */
|
||||
if (hp && hp_ld < R) return -1;
|
||||
|
||||
hipLaunchKernelGGL(r4d_dflash_select_kernel, dim3(M), dim3(DSL_THREADS), 0, (hipStream_t)stream,
|
||||
@@ -172,16 +157,14 @@ extern "C" int r4d_dflash_select_bf16(
|
||||
}
|
||||
|
||||
// ============================================================== the plugin ABI entry points
|
||||
//
|
||||
// The shared operand helpers are r4d_gdn_args.h. See libr4d/r4d_args.h for why there is no
|
||||
// adapter directory between the core and these launches.
|
||||
// Shared operand helpers: r4d_gdn_args.h.
|
||||
/* ------------------------------------------------------------------ dflash select */
|
||||
|
||||
enum { DS_CAND = 0, DS_UNARY, DS_HP, DS_ANCHOR, DS_PRED, DS_SUCC, DS_OUT };
|
||||
|
||||
/* A rank-2 [rows, width] operand whose row pitch libr4d takes explicitly. `width` is checked
|
||||
* rather than derived: this op reads three planes at the SAME row index and a plane whose width is
|
||||
* not what the parameters say would still have a plausible number of rows. */
|
||||
/* A rank-2 [rows, width] operand with an explicit row pitch. `width` is checked, not derived:
|
||||
* three planes are read at the same row index, and a wrong-width plane could still have a
|
||||
* plausible row count. */
|
||||
static long long ds_plane(const RadTensor* t, uint32_t dtype, long long width, long long rows) {
|
||||
if (!t || !t->data || t->rank != 2) return -1;
|
||||
if (t->dtype != dtype) return -1;
|
||||
@@ -198,57 +181,49 @@ extern "C" int r4d_rad_dflash_select(const RadArgs* a, RadStream s) {
|
||||
const RadTensor* pr = r4d_opt(a, DS_PRED);
|
||||
const RadTensor* sc = r4d_opt(a, DS_SUCC);
|
||||
const RadTensor* out = r4d_opt(a, DS_OUT);
|
||||
/* `hp` is the one that may be absent; every other operand is load-bearing. */
|
||||
/* Only hp may be absent. */
|
||||
if (!cand || !un || !anc || !pr || !sc || !out) return R4D_NO(RAD_E_INVAL);
|
||||
|
||||
const long long steps = r4d_geti_or(a, "steps", 0);
|
||||
const long long K = r4d_geti_or(a, "top_k", 0);
|
||||
const long long R = r4d_geti_or(a, "rank", 0);
|
||||
const long long V = r4d_geti_or(a, "n_vocab", 0);
|
||||
/* M IS THE OPERAND'S ROW COUNT AND NOT THE PARAMETER'S. RadArgs carries the geometry with its
|
||||
* RANGES COLLAPSED -- `M` here is the band's bound, max_seqs, and not the sequence count this
|
||||
* step issued (the same trap fill_paged spells out for q_len). Reading the parameter would
|
||||
* make the selector demand max_seqs * steps rows of candidates on a one-sequence step and
|
||||
* refuse every launch. The output plane is one row a sequence, so it is the count. */
|
||||
/* M comes from the output plane (one row per sequence), not the parameter: RadArgs carries the
|
||||
* band's bound (max_seqs) for M, as for q_len in fill_paged. The parameter would demand
|
||||
* max_seqs * steps candidate rows on a one-sequence step. */
|
||||
if (out->rank != 2) return R4D_NO(RAD_E_SHAPE);
|
||||
const long long M = out->shape[0];
|
||||
if (M <= 0 || steps <= 0 || K <= 0 || R <= 0 || V <= 0) return R4D_NO(RAD_E_INVAL);
|
||||
|
||||
/* The three input planes are read at row s*steps+l, so between them they must hold M*steps
|
||||
* rows -- a short one is a walk that reads another sequence's candidates and proposes a
|
||||
* perfectly plausible draft belonging to the wrong request. */
|
||||
/* The input planes are read at row s*steps+l and must hold M*steps rows; a short plane would
|
||||
* silently draft from another request's candidates. */
|
||||
const long long rows = M * steps;
|
||||
const long long cand_ld = ds_plane(cand, RAD_I32, K, rows);
|
||||
const long long un_ld = ds_plane(un, RAD_F32, K, rows);
|
||||
const long long out_ld = ds_plane(out, RAD_I32, steps, M);
|
||||
/* ABSENT IS A NULL `data`, NOT A MISSING OPERAND: operands are positional, so a graph that
|
||||
* does not use the hidden projection still passes it, empty. Both spellings arrive here and
|
||||
* both mean the bigram form. */
|
||||
/* Absent hp may arrive as a missing operand or as an empty one with null data (operands are
|
||||
* positional); both mean the bigram form. */
|
||||
const bool has_hp = hp && hp->data;
|
||||
const long long hp_ld = has_hp ? ds_plane(hp, RAD_BF16, R, rows) : 0;
|
||||
if (cand_ld < 0 || un_ld < 0 || hp_ld < 0 || out_ld < 0) return R4D_NO(RAD_E_SHAPE);
|
||||
/* One pitch for both, because the kernel indexes them with the same expression. */
|
||||
/* The kernel indexes cand and unary with one pitch. */
|
||||
if (cand_ld != un_ld) return R4D_NO(RAD_E_STRIDE);
|
||||
|
||||
/* The codebooks are gathered by GLOBAL token id, so a row-sharded one would let a rank score
|
||||
* only the candidates that happen to land in its own vocabulary slice -- and the candidates
|
||||
* arrive already merged across ranks. Both are declared replicated at the whole vocabulary;
|
||||
* this is where that would otherwise fail silently. */
|
||||
/* The codebooks are gathered by global token id, and candidates arrive merged across ranks, so
|
||||
* both must be replicated over the whole vocabulary; a row-sharded one would fail silently. */
|
||||
if (pr->dtype != RAD_BF16 || sc->dtype != RAD_BF16) return R4D_NO(RAD_E_DTYPE);
|
||||
if (!r4d_contig(pr) || !r4d_contig(sc)) return R4D_NO(RAD_E_STRIDE);
|
||||
if (r4d_numel(pr) < V * R || r4d_numel(sc) < V * R) return R4D_NO(RAD_E_SHAPE);
|
||||
|
||||
/* `anchor` is usually the step's own token_ids, which carries `block` ids a sequence with the
|
||||
* anchor first -- so the stride is a parameter and a packed [M] array is the stride-1 case. */
|
||||
/* anchor is usually the step's token_ids (`block` ids per sequence, anchor first), hence the
|
||||
* stride; a packed [M] array is stride 1. */
|
||||
const long long astr = r4d_geti_or(a, "anchor_stride", 1);
|
||||
if (anc->dtype != RAD_I32 && anc->dtype != RAD_U32) return R4D_NO(RAD_E_DTYPE);
|
||||
if (astr < 1 || !r4d_contig(anc)) return R4D_NO(RAD_E_SHAPE);
|
||||
if (r4d_numel(anc) < (M - 1) * astr + 1) return R4D_NO(RAD_E_SHAPE);
|
||||
|
||||
|
||||
return r4d_dflash_select_bf16(cand->data, un->data, has_hp ? hp->data : nullptr,
|
||||
anc->data, pr->data, sc->data,
|
||||
out->data, (int)M, (int)steps, (int)K, (int)R, (int)V,
|
||||
(int)cand_ld, (int)hp_ld, (int)out_ld, (int)astr, s) == 0
|
||||
? RAD_OK : RAD_E_DEVICE;
|
||||
return R4D_RAW(r4d_dflash_select_bf16(cand->data, un->data, has_hp ? hp->data : nullptr,
|
||||
anc->data, pr->data, sc->data,
|
||||
out->data, (int)M, (int)steps, (int)K, (int)R, (int)V,
|
||||
(int)cand_ld, (int)hp_ld, (int)out_ld, (int)astr, s));
|
||||
}
|
||||
Reference in new issue
Block a user