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
+34
-62
@@ -1,20 +1,15 @@
|
||||
/* r4d_attn_args.h -- the paged-attention operand list, read once for four kernel geometries.
|
||||
/* r4d_attn_args.h -- the paged-attention operand reader shared by every paged kernel geometry.
|
||||
*
|
||||
* The paged kernels -- h256/gqa6, h128/gqa4 and the gqa8/gqa12 instantiations, prefill and decode
|
||||
* -- take the SAME operands in the same order, because they implement the same op. The reader for
|
||||
* that list therefore belongs to the family rather than to any one of them, and this is where it
|
||||
* lives: a header beside the kernels, included by each unit's entry point.
|
||||
* The paged kernels (h256/gqa6, h128/gqa4, the gqa8/gqa12 instantiations; prefill and decode) take
|
||||
* the same operands in the same order, so one reader serves them all.
|
||||
*
|
||||
* It is NOT a translation layer. libr4d's R4DArgs carries the same convention RadTensor does --
|
||||
* strides in ELEMENTS (spec 2.1) -- so this is field extraction plus the validation a constraint
|
||||
* cannot do: a constraint settles whether a kernel EXISTS for a geometry and says nothing about a
|
||||
* particular call, so contiguity, alignment and buffer sizes are checked here and refused with the
|
||||
* code that names the class.
|
||||
* R4DArgs uses RadTensor's convention (strides in elements, spec 2.1), so this is field extraction
|
||||
* plus the per-call validation a constraint cannot express: contiguity, alignment and buffer
|
||||
* sizes, each refused with the code that names the class.
|
||||
*
|
||||
* The kernels already return a negative code for a shape they do not serve (-1 head_dim or
|
||||
* block_size, -2 gqa, -4 too many decode rows, -100-hipError for a launch failure) and those are
|
||||
* passed through unchanged: a caller that gets one has hit a selection bug, and the exact value is
|
||||
* what says which.
|
||||
* Kernel return codes for an unserved shape (-1 head_dim or block_size, -2 gqa, -4 too many decode
|
||||
* rows, -100-hipError for a launch failure) pass through unchanged. Getting one means a selection
|
||||
* bug, and the value says which.
|
||||
*/
|
||||
#ifndef R4D_ATTN_ARGS_H
|
||||
#define R4D_ATTN_ARGS_H
|
||||
@@ -23,10 +18,10 @@
|
||||
#include "r4d_args.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
|
||||
/* libr4d's compiled-in geometry, checked against the row table at build time rather than trusted.
|
||||
* A libr4d rebuilt for a different head size would otherwise change what the rows mean without
|
||||
* changing the rows. */
|
||||
/* Checks libr4d's compiled-in geometry against the row table, so a libr4d rebuilt for a
|
||||
* different head size cannot silently change what the rows mean. */
|
||||
static inline int check_dims_once() {
|
||||
int hd = 0, gqa = 0, bs = 0, rows = 0;
|
||||
r4d_attn_dims(&hd, &gqa, &bs, &rows);
|
||||
@@ -38,20 +33,12 @@ enum { AP_Q = 0, AP_KV, AP_BT, AP_SEQ, AP_KDS, AP_VDS, AP_CU, AP_OUT };
|
||||
|
||||
/* Fill R4DArgs from a RadArgs. Returns RAD_OK, or the negative code naming what is wrong.
|
||||
*
|
||||
* RAGGED BATCHES. libr4d's paged kernels take a scalar q_len AND an optional cu_seqlens, and which
|
||||
* one this call uses is decided here. Without cu_seqlens every sequence in the batch has to present
|
||||
* the same query length, which is a restriction the scheduler does not honour and cannot be asked
|
||||
* to: core/sched/scheduler.cpp builds RAD_PHASE_MIXED steps deliberately, because a decode row
|
||||
* costs nothing once another sequence's prefill chunk is already reading the weights. A step that
|
||||
* mixes them has total_q that need not divide n_seq, and refusing that with RAD_E_SHAPE would be
|
||||
* reported by the run phase as a kernel that does not serve the shape, which aborts the engine. So
|
||||
* the operand is passed whenever it is there and the uniform arithmetic is kept only for the case
|
||||
* where it is not.
|
||||
*
|
||||
* q_len then means the batch MAXIMUM, and it comes from the PARAMETER, which is exactly what the
|
||||
* arch block issued the op with (RAD_ISSUE_N(..., batch->max_q_len, ...)). Deriving it from the
|
||||
* tensors is only possible in the uniform case, and that is what the code below does there --
|
||||
* total_q / num_seqs -- because the parameter is a band bound there, not a count.
|
||||
* Ragged batches: the kernels take a scalar q_len and an optional cu_seqlens. Without cu_seqlens
|
||||
* every sequence must have the same query length, which RAD_PHASE_MIXED steps (decode rows riding
|
||||
* on another sequence's prefill chunk, core/sched/scheduler.cpp) do not satisfy; refusing them
|
||||
* would abort the engine. So cu_seqlens is used whenever present, and the uniform arithmetic
|
||||
* (q_len = total_q / num_seqs) only when it is not. In the ragged case q_len is the batch maximum,
|
||||
* taken from the parameter the arch block issued the op with (batch->max_q_len).
|
||||
*/
|
||||
static inline int fill_paged(const RadArgs* a, R4DArgs* r, int expect_head_dim) {
|
||||
const RadTensor* q = r4d_opt(a, AP_Q);
|
||||
@@ -65,13 +52,9 @@ static inline int fill_paged(const RadArgs* a, R4DArgs* r, int expect_head_dim)
|
||||
if (q->rank < 2 || o->rank < 2 || bt->rank != 2 || su->rank < 1) return R4D_NO(RAD_E_SHAPE);
|
||||
if (kv->rank != 4) return R4D_NO(RAD_E_SHAPE);
|
||||
|
||||
/* THE HEAD AXIS MAY BE SPELLED OR FLATTENED, and both are the same dense block. An architecture
|
||||
* plugin declares the query buffer [tokens, n_head, head_dim] when something downstream needs
|
||||
* the head axis and [tokens, n_head*head_dim] when nothing does -- a gated attention block does
|
||||
* the latter, because its query comes out of a de-interleave and goes straight here. The kernel
|
||||
* indexes one dense [total_q, q_heads, head_dim] block either way, so the shim reads the rank
|
||||
* it was given rather than demanding three. `head_dim` is the op's parameter, which is what
|
||||
* makes the flattened form readable at all. */
|
||||
/* The query may be [tokens, n_head, head_dim] or flattened to [tokens, n_head*head_dim]
|
||||
* (a gated attention block's de-interleaved query). Both are the same dense block, so any rank
|
||||
* is accepted; head_dim comes from the op parameter, which makes the flattened form readable. */
|
||||
const long long head_dim = expect_head_dim;
|
||||
const long long total_q = q->shape[0];
|
||||
const long long num_seqs = bt->shape[0];
|
||||
@@ -79,7 +62,7 @@ static inline int fill_paged(const RadArgs* a, R4DArgs* r, int expect_head_dim)
|
||||
if (r4d_numel(q) % (total_q * head_dim)) return R4D_NO(RAD_E_SHAPE);
|
||||
const long long q_heads = r4d_numel(q) / (total_q * head_dim);
|
||||
if (q_heads <= 0) return R4D_NO(RAD_E_SHAPE);
|
||||
/* Ragged unless cu_seqlens says otherwise; see the note above fill_paged. */
|
||||
/* Ragged exactly when cu_seqlens is present; see the note above fill_paged. */
|
||||
const bool ragged = (cu != nullptr) && cu->data;
|
||||
if (ragged) {
|
||||
if (cu->rank != 1 || cu->shape[0] != num_seqs + 1) return R4D_NO(RAD_E_SHAPE);
|
||||
@@ -96,15 +79,14 @@ static inline int fill_paged(const RadArgs* a, R4DArgs* r, int expect_head_dim)
|
||||
if (su->shape[0] != num_seqs) return R4D_NO(RAD_E_SHAPE);
|
||||
if (!r4d_contig(bt) || !r4d_contig(su)) return R4D_NO(RAD_E_STRIDE);
|
||||
|
||||
/* The KV cache is the one operand that IS pitched: kv_block_stride and kv_head_stride are
|
||||
* taken as arguments, so a padded cache is legal. Everything inside a (block, head) slot is
|
||||
* not, because the kernel walks it with a vector load. */
|
||||
/* The KV cache is pitched (kv_block_stride, kv_head_stride), so a padded cache is legal.
|
||||
* Each (block, head) slot must be dense, because the kernel walks it with vector loads. */
|
||||
const long long block_size = kv->shape[2];
|
||||
if (kv->stride[3] != 1) return R4D_NO(RAD_E_STRIDE);
|
||||
if (kv->stride[2] != 2 * head_dim) return R4D_NO(RAD_E_STRIDE);
|
||||
if (kv->shape[3] != 2 * head_dim) return R4D_NO(RAD_E_SHAPE);
|
||||
|
||||
/* 16-byte alignment: every one of these is read through a 128-bit load. */
|
||||
/* 16-byte alignment for 128-bit loads. */
|
||||
if (!r4d_aligned(q->data, 16) || !r4d_aligned(o->data, 16) || !r4d_aligned(kv->data, 16))
|
||||
return R4D_NO(RAD_E_ALIGN);
|
||||
|
||||
@@ -124,20 +106,12 @@ static inline int fill_paged(const RadArgs* a, R4DArgs* r, int expect_head_dim)
|
||||
r->num_seqs = (int)num_seqs;
|
||||
r->cu_q = ragged ? (const int*)cu->data : nullptr;
|
||||
r->total_q = (int)total_q;
|
||||
/* q_len is the exact count when the batch is uniform, and an UPPER BOUND on the per-sequence
|
||||
* lengths when it is ragged -- it sizes the prefill grid and the decode wave count, and the
|
||||
* kernels take the exact lengths from cu_seqlens.
|
||||
*
|
||||
* The bound is the smaller of two things that are both >= the true maximum and neither of
|
||||
* which is it. The `q_len` parameter is the resolved BAND (RadArgs carries the geometry with
|
||||
* ranges collapsed, not the value the op was issued with), and the band was selected by
|
||||
* max_q_len, so it is at least that. total_q is a sum of lengths, so it is at least the
|
||||
* largest of them. Taking the smaller keeps the decode band legal: its bound is 10 and
|
||||
* 10 * gqa is 60 rows, under the 64 a decode workgroup holds, while total_q alone would be
|
||||
* the whole batch and would be refused with -4.
|
||||
*
|
||||
* The cost of the bound not being tight is empty workgroups that exit on their first
|
||||
* comparison -- the prefill kernel's `qb * BLOCK_Q >= qlen` and the decode kernel's `live`. */
|
||||
/* q_len is exact for a uniform batch and an upper bound for a ragged one; it sizes the
|
||||
* prefill grid and the decode wave count, and the kernels read exact lengths from cu_seqlens.
|
||||
* The bound is min(band, total_q): the `q_len` parameter is the resolved band selected by
|
||||
* max_q_len, and total_q is a sum of lengths, so both are >= the true maximum. The min keeps
|
||||
* decode legal (band bound 10 * gqa 6 = 60 rows fits a 64-row workgroup; total_q alone would
|
||||
* be refused with -4). A loose bound only costs workgroups that exit on their first compare. */
|
||||
r->q_len = (int)(total_q / num_seqs);
|
||||
if (ragged) {
|
||||
const long long band = r4d_geti_or(a, "q_len", total_q);
|
||||
@@ -154,13 +128,11 @@ static inline int fill_paged(const RadArgs* a, R4DArgs* r, int expect_head_dim)
|
||||
r->causal = (int)r4d_geti_or(a, "causal", 1);
|
||||
r->causal_seq = nullptr;
|
||||
|
||||
/* The softmax scale. Absent, 1/sqrt(head_dim) -- right for every model in scope, and a
|
||||
* parameter rather than a hardcode for the ones that are not. */
|
||||
/* Softmax scale; defaults to 1/sqrt(head_dim). */
|
||||
r->scale = (float)r4d_getf(a, "scale", 1.0 / std::sqrt((double)head_dim));
|
||||
|
||||
/* max_ctx is the HOST-visible context bound; seqused_k is device-side and the split law
|
||||
* cannot read it. The block table's width times the block size is an exact upper bound on
|
||||
* what any sequence in this batch can have, so it is a safe default. */
|
||||
/* max_ctx is the host-visible context bound for the split law (seqused_k is device-side).
|
||||
* Block-table width times block size is a safe upper bound. */
|
||||
long long mc = r4d_geti_or(a, "max_ctx", 0);
|
||||
if (mc <= 0) mc = (long long)r->max_blocks * block_size;
|
||||
r->max_ctx = (int)mc;
|
||||
|
||||
Reference in new issue
Block a user