Archived
radiance 1.14.0
New models and formats: - libr3d: a kernel library for RDNA3 (gfx11) cards - nomic-embed-text-v2-moe (the nomicbert plugin) - Gemma 4 E2B with its per-layer MLP width, and Google's unmodified E2B/E4B directories in bf16 - DeepSeek-V4.1-Flash converted straight from its release: the Engram tables, the fused imatrix and rad-convert --chat-template for a release that ships no template - GGUF Q8_0, Q4_K and Q6_K tensors import as their planes; rad-convert writes them without a recipe - Qwen3.8-Flash-Next-FP8 converts and serves through qwen4exp_fp8 Speed: - hc_read's decode kernels as one grid (34.7 -> 25.2 us at 4 rows) and its tiled up-mix pipelined - gemm_fp8a8: the narrow form's scales staged through LDS (Flash-Next int8 at 32 rows 83.6 -> 68.3 us), the gated three-tile form at 8 waves (gate_up at 48 rows 272.9 -> 180.6 us) - Qwen3.8-27B: the delta net's out-projection read beside its decode recurrent update (x1 19.40 -> 19.10 ms a step), and the verify's split-KV attention sized by context depth (x3 at 180K 42.4 -> 40.3 ms a step) - Hy-Memory (HYV3) decode: fused QK norm, RoPE and KV store, residual adds folded into the norms, coalesced one-token expert GEMMs - spechub: the coupled verify walks the live candidates only (151 -> 31 us a launch) Speculation: - DFlash2: lossless coupled acceptance at T > 0 (--draft-coupling on) - A block drafter's proposals verified as a tree, on by default for the Gemma 4 31B's DFlash2, with a block-16 tree that runs its short block under load Features: - Decode-hot residency: --draft-expert-placement host, --expert-profile, --expert-pin-top, and --expert-stats FILE for the routed experts each layer selects - --ngram-placement ram=DIR: one n-gram table shared by engines on separate cards - Prefix cache: --prefix-resume exact (default) resumes a hit on the cold run's own chunk grid, so it answers bit-identically; snapshots at a head other prompts share, from 528-token prompts, and for tip-only one drafter window below the tip; hits on models whose KV groups have different block sizes; the snapshot store keeps a floor of 8 ahead of --host-headroom-mib - Typed tool-call argument tags; rad-chatparse --request renders and reads back one request as the server does - MiniMax-H3: keyframes ride a latent upscale - Gemma 3's fused all-reduce junction on the lossy wire Fixes: - A speculative step shorter than its band (an MTP step of 4 rows at gqa 8) no longer stops the engine with a scratch refusal at issue (#293) - --p2p off: prefill back to 1.12.0's rate; 1.13.0's push check read a host-memory slot over the link twice (#323) - A chat template that cannot render a conversation is refused at load, not mid-request - imatrix entries found under llama.cpp's spelling; the W4A8 prefill's tail scale read; the GDN conv kernels on an f32 weight; dense integer rows at the group they compute (a Q8_0 weight at group 32) - --expert-profile refuses a profile recorded on another model - hc_read's one-grid wait traps past its deadline instead of reading a stale value Changes: Plugin ABI 31, unchanged. RADIANCE_DRAFT_TREE, DFLASH2_TREE_NODES, DFLASH2_TREE_TEMP and DFLASH_Q_TEMP are gone (trees run where the probe says they pay; temperatures 1.25 / 0.6).
This commit is contained in:
1 parent
481fcd5df0
commit
4a61d7296b
160 files changed
+8790
-6819
No files matched your search
+113
-160
@@ -1,45 +1,37 @@
|
||||
/* r3d_shapes.cpp -- the extent and content domain of each operand of an op at a geometry.
|
||||
*
|
||||
* This is libr3d's side of the `RadShapeFn` hook: the component that reads an operand is the one
|
||||
* that knows its extent. The hand-written table in `tools/` describes the ops it was written
|
||||
* against; a row that exists only here is invisible to it, and a tool that cannot size an operand
|
||||
* cannot call the op cold. So correctness checks, benchmarks and the coverage census reach these
|
||||
* rows through this hook.
|
||||
* libr3d's side of the `RadShapeFn` hook. The component that reads an operand knows its extent,
|
||||
* and a row that exists only here is invisible to the hand-written table in `tools/`. Correctness
|
||||
* checks, benchmarks and the coverage census size these rows through this hook.
|
||||
*
|
||||
* The authority for every extent is the shim, not the prose or the row table. Each `r3d_rad_*`
|
||||
* entry point unpacks `RadArgs.t[i]` into extents and pitches; if a shim passes `x->shape[1]` as
|
||||
* `n`, that is what the operand is. Each group names its unit, and a change to a shim's contract
|
||||
* must be mirrored here.
|
||||
* The shim is the authority for every extent: each `r3d_rad_*` entry point unpacks `RadArgs.t[i]`
|
||||
* into extents and pitches, and a change to a shim's contract must be mirrored here.
|
||||
*
|
||||
* Content matters as much as size. A token id gathers a row of a codebook, a KV slot indexes
|
||||
* Content matters as much as size. A token id gathers a codebook row, a KV slot indexes
|
||||
* n_blocks * block_size positions, a block-table entry indexes n_blocks. Drawn from the wrong
|
||||
* range, such an operand tests the kernel's out-of-range handling instead of its arithmetic, and a
|
||||
* scatter whose indices collide is a race the parallel rerun rightly reports as a failure.
|
||||
* `RAD_FILL_INDEX` with an explicit `idx_max` states each range, and `RAD_OPD_F_IDX_UNIQUE` marks
|
||||
* the scatters that need distinct destinations.
|
||||
* range, such an operand tests out-of-range handling instead of arithmetic, and colliding scatter
|
||||
* indices are a race. `RAD_FILL_INDEX` with an explicit `idx_max` states each range, and
|
||||
* `RAD_OPD_F_IDX_UNIQUE` marks scatters that need distinct destinations.
|
||||
*
|
||||
* ============================================================ rows with no description
|
||||
*
|
||||
* A decline is a real answer: the caller skips and counts the case, where an invented extent
|
||||
* yields a meaningless pass. These rows carry a null `opd_shape`:
|
||||
* yields a meaningless pass.
|
||||
*
|
||||
* gemm_nt_q at a quantised `a` whose scale plane the row does not name
|
||||
* Not present in libr3d: both quantised rows state their activation dtype in `dtype`, so
|
||||
* `a` is bf16 against an fp8 weight (fp8a16) or i8 against an i8 weight (i8a8), and both are
|
||||
* described below. The case is listed here because it is the one libr4d declines, and a
|
||||
* reader comparing the two files will look for the difference.
|
||||
* Does not arise here (libr4d declines it): both quantised rows state their activation
|
||||
* dtype in `dtype`, so `a` is bf16 against fp8 (fp8a16) or i8 against i8 (i8a8).
|
||||
*
|
||||
* rope at a multi-component mode
|
||||
* mrope, imrope and axial take one position per component, component-major, and the
|
||||
* component count comes from the `sections` string. libr3d's kernel implements neox and gptj
|
||||
* and its row's constraints say so, so a geometry with `sections` set is never routed here.
|
||||
* mrope, imrope and axial take one position per component, component-major, with the count
|
||||
* from `sections`. libr3d's kernel implements neox and gptj only and its row's constraints
|
||||
* say so, so a geometry with `sections` set is never routed here.
|
||||
*
|
||||
* No tuned axis is read here: this plugin's axes (split counts, wave counts, the streaming mode)
|
||||
* change how a kernel walks its operands, not their extents. An axis that did would be read from
|
||||
* `p`, where the core appends the chosen point.
|
||||
* No tuned axis is read: this plugin's axes (split counts, wave counts, the streaming mode) change
|
||||
* how a kernel walks its operands, not their extents. An axis that did would be read from `p`,
|
||||
* where the core appends the chosen point.
|
||||
*
|
||||
* Pure host code (no r3d.h, no HIP), like r3d_rows.cpp, so the tables compile and can be read
|
||||
* without ROCm.
|
||||
* Pure host code (no r3d.h, no HIP), like r3d_rows.cpp, so it compiles without ROCm.
|
||||
*/
|
||||
|
||||
#include <cstdint>
|
||||
@@ -52,12 +44,12 @@ namespace {
|
||||
/* ==================================================================== building a description
|
||||
*
|
||||
* Each op builds its whole operand list in schema order and hands out the one asked for, so the
|
||||
* count can be checked by eye against the schema in r3d_rows.cpp (the tool also checks it against
|
||||
* count can be checked against the schema in r3d_rows.cpp (the tool checks
|
||||
* `RadOpSchema::n_operands`).
|
||||
*/
|
||||
|
||||
/* A plain operand of up to four extents. A trailing 0 ends the shape; zero is never a legal
|
||||
* extent (`pick` refuses one), so it cannot be mistaken for a real one. */
|
||||
* extent (`pick` refuses one). */
|
||||
RadOpdDesc opd(uint32_t dt, int64_t d0, int64_t d1 = 0, int64_t d2 = 0, int64_t d3 = 0) {
|
||||
RadOpdDesc d{};
|
||||
d.dtype = dt;
|
||||
@@ -83,8 +75,7 @@ RadOpdDesc opd_idx(int64_t d0, int64_t idx_max, int64_t konst = -1, uint32_t fla
|
||||
return d;
|
||||
}
|
||||
|
||||
/* An optional operand this description does not pass. One entry per schema operand is still
|
||||
* emitted, so absence needs a spelling; to the ABI it means a null RadTensor. */
|
||||
/* An optional operand this description does not pass; to the ABI it means a null RadTensor. */
|
||||
RadOpdDesc opd_none() {
|
||||
RadOpdDesc d{};
|
||||
d.idx_const = -1;
|
||||
@@ -92,16 +83,13 @@ RadOpdDesc opd_none() {
|
||||
return d;
|
||||
}
|
||||
|
||||
/* Restricts the fill to (0, 1); `RAD_FILL_SIGMOID` is the ABI's only such draw. A scale plane drawn
|
||||
* from a normal is as likely to be negative as positive, and a negative descale inverts the sign of
|
||||
* an entire quantised block. */
|
||||
/* Restricts the fill to (0, 1); `RAD_FILL_SIGMOID` is the ABI's only such draw. A scale drawn from
|
||||
* a normal is negative half the time, and a negative descale inverts a whole quantised block. */
|
||||
RadOpdDesc in_unit(RadOpdDesc d) { d.fill = RAD_FILL_SIGMOID; return d; }
|
||||
|
||||
/* Hands out operand `operand` of a list after checking the whole list, so a geometry missing a
|
||||
* parameter declines every operand rather than describing a few and declining the rest. A
|
||||
* non-positive extent means a needed parameter was absent or zero: RAD_E_UNSUPPORTED, not
|
||||
* RAD_E_SHAPE, which would read as a tool bug. An index outside the list is a bug here or in the
|
||||
* schema, reported as RAD_E_SHAPE. */
|
||||
/* Hands out operand `operand` after checking the whole list, so a geometry missing a parameter
|
||||
* declines every operand. A non-positive extent means a needed parameter was absent or zero
|
||||
* (RAD_E_UNSUPPORTED); an index outside the list is a bug here or in the schema (RAD_E_SHAPE). */
|
||||
int pick(RadOpdDesc* out, int operand, const RadOpdDesc* list, int n) {
|
||||
if (!out || !list || n <= 0) return RAD_E_INVAL;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
@@ -119,12 +107,11 @@ int pick(RadOpdDesc* out, int operand, const RadOpdDesc* list, int n) {
|
||||
|
||||
/* ==================================================================== parameters
|
||||
*
|
||||
* `M` and the other ranged parameters read as the band's bound, not any one step's row count (as
|
||||
* in every shim here): buffers must hold the whole band the kernel was selected for.
|
||||
* `M` and the other ranged parameters read as the band's bound, not one step's row count: buffers
|
||||
* must hold the whole band the kernel was selected for.
|
||||
*
|
||||
* Not r3d_pgeti_or(): that reads RAD_P_INT only, which suits a launch (every range has collapsed
|
||||
* by then), but this hook can see ranges still open. rad_param_getdim (ABI, shared with libr4d)
|
||||
* reads a RAD_P_RANGE as its high bound.
|
||||
* Not r3d_pgeti_or(), which reads RAD_P_INT only; this hook can see ranges still open, and
|
||||
* rad_param_getdim reads a RAD_P_RANGE as its high bound.
|
||||
*/
|
||||
int64_t gi(const RadParam* p, int n_p, const char* k, int64_t dflt) {
|
||||
return rad_param_getdim(p, n_p, k, dflt);
|
||||
@@ -134,10 +121,9 @@ const char* gs(const RadParam* p, int n_p, const char* k) {
|
||||
return rad_param_gets(p, n_p, k, nullptr);
|
||||
}
|
||||
|
||||
/* The activation dtype, from `dtype` and its attention spellings; bf16 when the geometry names
|
||||
* none. A quantisation-scheme string ("fp8a16", "w8a16") is not a dtype and lands on bf16, which
|
||||
* is what the quantised rows use it to mean: the scheme picks the weight's dtype, the row states
|
||||
* the activation's. */
|
||||
/* The activation dtype, from `dtype` or `q_dtype`; bf16 when the geometry names none. A
|
||||
* quantisation-scheme string ("fp8a16", "w8a16") is not a dtype and lands on bf16, which is what
|
||||
* the quantised rows mean by it: the scheme picks the weight's dtype. */
|
||||
uint32_t act_dt(const RadParam* p, int n_p) {
|
||||
const char* s = gs(p, n_p, "dtype");
|
||||
if (!s) s = gs(p, n_p, "q_dtype");
|
||||
@@ -161,8 +147,8 @@ int64_t scale_groups(int64_t n, int64_t group) {
|
||||
|
||||
extern "C" {
|
||||
|
||||
/* y = x * rsqrt(mean(x^2) + eps) * (w + wadd) * mult. The gain is described at its full width; the
|
||||
* form with no gain at all is a hand case, since a null optional operand is not a fill domain. */
|
||||
/* y = x * rsqrt(mean(x^2) + eps) * (w + wadd) * mult. The gain is described at full width; the
|
||||
* form with no gain is a hand case, since a null optional operand is not a fill domain. */
|
||||
int r3d_shape_rmsnorm(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const uint32_t D = act_dt(p, n_p);
|
||||
const int64_t M = gi(p, n_p, "M", 0), n = gi(p, n_p, "n", 0);
|
||||
@@ -170,8 +156,7 @@ int r3d_shape_rmsnorm(const RadParam* p, int n_p, int operand, RadOpdDesc* out)
|
||||
R3D_PICK(list);
|
||||
}
|
||||
|
||||
/* The two-pass form. `b` is optional and passed: the biased form covers strictly more code, and a
|
||||
* null optional operand is not a fill domain a tool can draw. */
|
||||
/* The two-pass form. `b` is optional and passed: the biased form covers strictly more code. */
|
||||
int r3d_shape_layernorm(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const uint32_t D = act_dt(p, n_p);
|
||||
const int64_t M = gi(p, n_p, "M", 0), n = gi(p, n_p, "n", 0);
|
||||
@@ -180,8 +165,7 @@ int r3d_shape_layernorm(const RadParam* p, int n_p, int operand, RadOpdDesc* out
|
||||
}
|
||||
|
||||
/* add / mul share a schema and a description. `b` is described at full extent; the row-broadcast
|
||||
* form is not described, since a buffer of n elements against M rows cannot be told from a shape
|
||||
* bug by a tool that sizes by shape alone. */
|
||||
* form is not, since a tool that sizes by shape alone cannot tell it from a shape bug. */
|
||||
static int binary_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const uint32_t D = act_dt(p, n_p);
|
||||
const int64_t M = gi(p, n_p, "M", 0), n = gi(p, n_p, "n", 0);
|
||||
@@ -196,9 +180,8 @@ int r3d_shape_mul(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
return binary_shape(p, n_p, operand, out);
|
||||
}
|
||||
|
||||
/* y = act(gate) * up over two [M, n] operands. The fused [M, 2n] form is the one the model
|
||||
* issues; describing it here would be a second spelling of a geometry the row's `n` is about, so
|
||||
* the two halves are described as the kernel sees them. */
|
||||
/* y = act(gate) * up over two [M, n] operands, the halves as the kernel sees them (the model
|
||||
* issues the fused [M, 2n] form). */
|
||||
int r3d_shape_act_mul(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
return binary_shape(p, n_p, operand, out);
|
||||
}
|
||||
@@ -236,7 +219,7 @@ int r3d_shape_softmax(const RadParam* p, int n_p, int operand, RadOpdDesc* out)
|
||||
return unary_shape(p, n_p, operand, out);
|
||||
}
|
||||
|
||||
/* y = x * s. `s` is one element, broadcast over everything; a row of them would be a different op. */
|
||||
/* y = x * s with `s` a single broadcast element. */
|
||||
int r3d_shape_scale(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const uint32_t D = act_dt(p, n_p);
|
||||
const int64_t M = gi(p, n_p, "M", 0), n = gi(p, n_p, "n", 0);
|
||||
@@ -244,7 +227,7 @@ int r3d_shape_scale(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
R3D_PICK(list);
|
||||
}
|
||||
|
||||
/* y = value. The only operand is the output, so a case's whole content is the kernel's. */
|
||||
/* y = value. The only operand is the output. */
|
||||
int r3d_shape_fill(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const uint32_t D = act_dt(p, n_p);
|
||||
const int64_t M = gi(p, n_p, "M", 0), n = gi(p, n_p, "n", 0);
|
||||
@@ -256,10 +239,9 @@ int r3d_shape_fill(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
* r3d_model_bf16.hip
|
||||
*/
|
||||
|
||||
/* In-place rotary over a fused qkv row: n_head q heads, then n_head_kv each of k and v. The
|
||||
* positions are drawn over the row count, which for a single step is its own positions; a step
|
||||
* crossing a context boundary is a hand case, since a fill cannot spell "contiguous from an
|
||||
* arbitrary start". */
|
||||
/* In-place rotary over a fused qkv row: n_head q heads, then n_head_kv each of k and v. Positions
|
||||
* are drawn over the row count; positions contiguous from an arbitrary start are a hand case,
|
||||
* since no fill domain spells them. */
|
||||
int r3d_shape_rope(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const int64_t M = gi(p, n_p, "M", 0);
|
||||
const int64_t hd = gi(p, n_p, "head_dim", 0);
|
||||
@@ -274,8 +256,7 @@ int r3d_shape_rope(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
}
|
||||
|
||||
/* K and V into a paged cache. Two blocks of slack over the tokens, so the slot mapping is not a
|
||||
* dense prefix and the block index matters. Only the cache takes `kv_dtype`: k and v are bf16
|
||||
* activations whatever the cache stores. */
|
||||
* dense prefix. Only the cache takes `kv_dtype`; k and v are always bf16. */
|
||||
int r3d_shape_kv_store(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const int64_t M = gi(p, n_p, "M", 0);
|
||||
const int64_t hd = gi(p, n_p, "head_dim", 0);
|
||||
@@ -311,8 +292,8 @@ int r3d_shape_embed_lookup(const RadParam* p, int n_p, int operand, RadOpdDesc*
|
||||
R3D_PICK(list);
|
||||
}
|
||||
|
||||
/* The source is 4x the gathered height so the gather is not the identity, which would pass while
|
||||
* broken; `idx` draws over those 4M rows. */
|
||||
/* The source is 4x the gathered height so the gather is not the identity; `idx` draws over those
|
||||
* 4M rows. */
|
||||
int r3d_shape_gather_rows(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const uint32_t D = act_dt(p, n_p);
|
||||
const int64_t M = gi(p, n_p, "M", 0), n = gi(p, n_p, "n", 0);
|
||||
@@ -320,9 +301,9 @@ int r3d_shape_gather_rows(const RadParam* p, int n_p, int operand, RadOpdDesc* o
|
||||
R3D_PICK(list);
|
||||
}
|
||||
|
||||
/* Onto exactly M rows a scatter is a permutation, which a kernel ignoring `idx` could pass, so the
|
||||
* destination is 4x the scattered height and unnamed rows must come back unchanged. The indices are
|
||||
* distinct: two rows drawing one destination is a race. */
|
||||
/* The destination is 4x the scattered height, since onto exactly M rows a kernel ignoring `idx`
|
||||
* could pass; unnamed rows must come back unchanged. Indices are distinct, as a shared destination
|
||||
* is a race. */
|
||||
int r3d_shape_scatter_rows(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const uint32_t D = act_dt(p, n_p);
|
||||
const int64_t M = gi(p, n_p, "M", 0), n = gi(p, n_p, "n", 0);
|
||||
@@ -348,14 +329,10 @@ int r3d_shape_quant_act_fp8(const RadParam* p, int n_p, int operand, RadOpdDesc*
|
||||
R3D_PICK(list);
|
||||
}
|
||||
|
||||
/* The int8 twin, with the optional row-sum plane beside it. `asum` is described whenever `group`
|
||||
* divides n, so a case checks the row sums as well; it is group-major, the layout libref's
|
||||
* quant_act_i8 writes. */
|
||||
/* The per-row int8: one scale per row, in a [M, 1] plane (the reference writes it at pitch 1, so a
|
||||
* plane with more room is legal and this states the tightest one), and the optional `asum` described
|
||||
* whenever `group` divides n, so a case exercises the row sums too. `q` is in the step order --
|
||||
* described by its extent, because a description states a plane's size and content domain, not a
|
||||
* permutation; the row's `shape` prose names the order. */
|
||||
/* The per-row int8: one scale per row in the tightest [M, 1] plane (the reference writes it at
|
||||
* pitch 1). The optional `asum`, group-major as libref's quant_act_i8 writes it, is described
|
||||
* whenever `group` divides n so a case checks the row sums too. `q` is in the step order; a
|
||||
* description states size and content domain, not a permutation, so it is described by extent. */
|
||||
int r3d_shape_quant_act_i8(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const uint32_t D = act_dt(p, n_p);
|
||||
const int64_t M = gi(p, n_p, "M", 0), n = gi(p, n_p, "n", 0), g = gi(p, n_p, "group", 0);
|
||||
@@ -368,8 +345,7 @@ int r3d_shape_quant_act_i8(const RadParam* p, int n_p, int operand, RadOpdDesc*
|
||||
R3D_PICK(list);
|
||||
}
|
||||
|
||||
/* The per-group int8, the activation the i8a8 GEMM reads: the fp8 quantiser's extents at int8 codes
|
||||
* and a 127 denominator. */
|
||||
/* The per-group int8 the i8a8 GEMM reads: the fp8 quantiser's extents with int8 codes. */
|
||||
int r3d_shape_quant_act_i8g(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const uint32_t D = act_dt(p, n_p);
|
||||
const int64_t M = gi(p, n_p, "M", 0), n = gi(p, n_p, "n", 0);
|
||||
@@ -395,9 +371,8 @@ int r3d_shape_gated_quant_fp8(const RadParam* p, int n_p, int operand, RadOpdDes
|
||||
R3D_PICK(list);
|
||||
}
|
||||
|
||||
/* y = code * scale. `dtype` names the code: E4M3 and E5M2 are the checkpoint's fp8, i8 the int8
|
||||
* twin. The scale is a dequant multiplier, so its draw is restricted to (0, 1) -- a normal draw is
|
||||
* as likely to be negative, and a negative scale inverts a whole block. */
|
||||
/* y = code * scale. `dtype` names the code: E4M3 or E5M2 (checkpoint fp8) or i8. The scale is
|
||||
* drawn in (0, 1), see in_unit(). */
|
||||
int r3d_shape_dequant(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const int64_t M = gi(p, n_p, "M", 0), n = gi(p, n_p, "n", 0);
|
||||
const int64_t g = gi(p, n_p, "group", 0);
|
||||
@@ -414,8 +389,7 @@ int r3d_shape_dequant(const RadParam* p, int n_p, int operand, RadOpdDesc* out)
|
||||
* r3d_gemm_bf16_nt_m16.hip, r3d_gemm_bf16_nt_tiled.hip, r3d_gemm_fp8a16_nt_m1.hip
|
||||
*/
|
||||
|
||||
/* y[M,N] = a[M,K] @ b[N,K]^T. Both operands are k-contiguous, which is what the transposed spelling
|
||||
* in the op's name means and why neither is described as [K, N]. */
|
||||
/* y[M,N] = a[M,K] @ b[N,K]^T. Both operands are k-contiguous (the "nt" in the name). */
|
||||
int r3d_shape_gemm_nt(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const uint32_t D = act_dt(p, n_p);
|
||||
const int64_t M = gi(p, n_p, "M", 0), N = gi(p, n_p, "N", 0), K = gi(p, n_p, "K", 0);
|
||||
@@ -424,15 +398,10 @@ int r3d_shape_gemm_nt(const RadParam* p, int n_p, int operand, RadOpdDesc* out)
|
||||
}
|
||||
|
||||
/* The vocabulary projection, transcribed from ref_logits_gemm_shape: x [M, n_embd] and the head
|
||||
* [n_vocab, n_embd] in the activation's dtype, the logits in f32 -- the dtype the schema states, not
|
||||
* the one a GEMM would narrow to, because every sampler stage downstream reads f32 and a prefill
|
||||
* band's plane is 1.07 GB of it at M=2048. The reference's own logits row answers a placement
|
||||
* question (`ref_layout_head`: where the head lives) and so carries a layout hook; this row has
|
||||
* nothing to place, it reads the head where it is stored, which is why the hook it declares is the
|
||||
* shape and nothing else. A hook is the right thing to write *because* all three operands are
|
||||
* describable: the extents the hook names are the ones that get filled, which is the opposite of
|
||||
* the joined w2a8 plane, where a describable byte count is a container header waiting to be filled
|
||||
* with random bytes. */
|
||||
* [n_vocab, n_embd] in the activation's dtype, logits in f32 because every sampler stage reads
|
||||
* f32 (a prefill band's plane is 1.07 GB at M=2048). Unlike the reference row, which also places
|
||||
* the head (`ref_layout_head`), this row reads the head where it is stored, so it declares only a
|
||||
* shape hook. */
|
||||
int r3d_shape_logits_gemm_bf16(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const int64_t M = gi(p, n_p, "M", 0), nv = gi(p, n_p, "n_vocab", 0), ne = gi(p, n_p, "n_embd", 0);
|
||||
if (M <= 0 || nv <= 0 || ne <= 0) return RAD_E_UNSUPPORTED;
|
||||
@@ -441,17 +410,17 @@ int r3d_shape_logits_gemm_bf16(const RadParam* p, int n_p, int operand, RadOpdDe
|
||||
R3D_PICK(list); /* x, lm_head, logits */
|
||||
}
|
||||
|
||||
/* The quantised GEMM. `dtype` names the scheme, and the scheme fixes which operands are coded:
|
||||
/* The quantised GEMM. `dtype` names the scheme, which fixes which operands are coded:
|
||||
*
|
||||
* fp8a16 a bf16, b the checkpoint's E4M3 [N][K], b_scale one f32 per 128x128 block (the
|
||||
* container's `weight_scale_inv`, whose dtype the container states: f32 at fp8a8f32,
|
||||
* bf16 as DeepSeek's own scales are).
|
||||
* fp8a8 a E4M3 against a_scale, one f32 per (row, `group` of K) -- quant_act_fp8's output
|
||||
* feeding the GEMM, which is the reason the two rows state the same group.
|
||||
* fp8a8 a E4M3 against a_scale, one f32 per (row, `group` of K): quant_act_fp8's output, so
|
||||
* the two rows state the same group.
|
||||
* i8a8 i8 codes on both axes with an f32 scale per (row, `group`).
|
||||
*
|
||||
* `b_scale`'s plane is block-major [ceil(N/pb), ceil(K/pb)] for fp8 and row-major [N, ceil(K/group)]
|
||||
* for i8: the two checkpoints spell it differently and both are the bytes on disk. */
|
||||
* `b_scale` is block-major [ceil(N/pb), ceil(K/pb)] for fp8 and row-major [N, ceil(K/group)] for
|
||||
* i8, as each checkpoint stores it. */
|
||||
int r3d_shape_gemm_nt_q(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const int64_t M = gi(p, n_p, "M", 0), N = gi(p, n_p, "N", 0), K = gi(p, n_p, "K", 0);
|
||||
if (M <= 0 || N <= 0 || K <= 0) return RAD_E_UNSUPPORTED;
|
||||
@@ -474,11 +443,10 @@ int r3d_shape_gemm_nt_q(const RadParam* p, int n_p, int operand, RadOpdDesc* out
|
||||
return pick(out, operand, list, R3D_GEMM_Q_NOPERAND);
|
||||
}
|
||||
|
||||
/* The OCP-MXFP4 grid, libref's description for the dtype: the weight is e2m1, two a byte, with
|
||||
* one e8m0 exponent per 32 of K, so the weight's planes are [N, K] of fp4_e2m1 and [N, K/32] of
|
||||
* e8m0; `b_ref` is the same exponent plane, accepted beside an e2m1 weight and ignored. The
|
||||
* activation is the fp8a8 row's -- E4M3 against one f32 per (row, 128 of K), drawn in (0, 1) as
|
||||
* a quantiser's is. Both zero-point operands are absent: the scale is symmetric. */
|
||||
/* The OCP-MXFP4 grid, as libref describes it: an e2m1 weight [N, K], two a byte, with one e8m0
|
||||
* exponent per 32 of K ([N, K/32]); `b_ref` is the same exponent plane, accepted and ignored. The
|
||||
* activation is the fp8a8 row's: E4M3 against one f32 per (row, 128 of K), drawn in (0, 1). Both
|
||||
* zero-point operands are absent since the scale is symmetric. */
|
||||
int r3d_shape_gemm_nt_q_mxfp4a8(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const int64_t M = gi(p, n_p, "M", 0), N = gi(p, n_p, "N", 0), K = gi(p, n_p, "K", 0);
|
||||
if (M <= 0 || N <= 0 || K <= 0) return RAD_E_UNSUPPORTED;
|
||||
@@ -492,9 +460,9 @@ int r3d_shape_gemm_nt_q_mxfp4a8(const RadParam* p, int n_p, int operand, RadOpdD
|
||||
as.fill = RAD_FILL_SIGMOID; /* a quantiser's (0, 1) */
|
||||
list[1] = as;
|
||||
list[2] = opd((uint32_t)RAD_FP4E2M1, N, K); /* b: e2m1, two a byte */
|
||||
/* The e8m0 scale, filled with 127: the factor 2^0. A normal draw of the u8 grid is an exponent
|
||||
* as high as 227, a factor 2^100, and the plane would scale the output past the finiteness
|
||||
* bound the run checks; 127 is the checkpoint's own cluster and no reserved value. */
|
||||
/* The e8m0 scale is filled with 127, the factor 2^0. A normal draw of the u8 grid reaches
|
||||
* exponents near 227 (2^100) and overflows the output's finiteness check; 127 is where real
|
||||
* checkpoints cluster and is not a reserved value. */
|
||||
RadOpdDesc e8 = opd((uint32_t)RAD_E8M0, N, K / 32); /* b_scale: e8m0 */
|
||||
e8.fill = RAD_FILL_INDEX;
|
||||
e8.idx_const = 127;
|
||||
@@ -505,10 +473,10 @@ int r3d_shape_gemm_nt_q_mxfp4a8(const RadParam* p, int n_p, int operand, RadOpdD
|
||||
return pick(out, operand, list, R3D_GEMM_Q_NOPERAND);
|
||||
}
|
||||
|
||||
/* The symmetric int4 / int8 weight grids, libref's descriptions: int8 activations in the step
|
||||
* order against signed codes (I4, two a byte, or I8, one a byte) with one f16 scale per (row,
|
||||
* 128 of K) and one f32 activation scale per row, drawn in (0, 1) as a quantiser's is. There is
|
||||
* no zero point and no a_sum: the dequant is code times scale. */
|
||||
/* The symmetric int4 / int8 weight grids, as libref describes them: int8 activations in the step
|
||||
* order against signed codes (I4 two a byte, or I8) with one f16 scale per (row, 128 of K) and one
|
||||
* f32 activation scale per row, drawn in (0, 1). No zero point and no a_sum: the dequant is code
|
||||
* times scale. */
|
||||
static int shape_inta8(const RadParam* p, int n_p, int operand, RadOpdDesc* out, const char* q,
|
||||
uint32_t bdt)
|
||||
{
|
||||
@@ -546,18 +514,14 @@ int r3d_shape_gemm_nt_q_w8a8(const RadParam* p, int n_p, int operand, RadOpdDesc
|
||||
|
||||
/* ==================================================================== r3d_sample_f32.hip, r3d_attn_paged_h128.hip
|
||||
*
|
||||
* The vocabulary and the sampler. The schemas in r3d_rows.cpp are transcribed from libref and
|
||||
* checked against it, and these are the other half of that transcription. Every extent, dtype,
|
||||
* fill domain and flag below is libref's: a description that differs from libref's is a case
|
||||
* kbench skips, with one line saying so, and a row that is never exercised is a row that is not
|
||||
* tested.
|
||||
* The vocabulary and the sampler. The schemas in r3d_rows.cpp are transcribed from libref, and
|
||||
* every extent, dtype, fill domain and flag below is libref's: a description that differs is a
|
||||
* case kbench skips with a one-line notice.
|
||||
*
|
||||
* The sampler's `params` operand is one plane with one fill domain, `RAD_FILL_SAMPLE_PARAMS`: the
|
||||
* ABI's `RadSampleParams`, 96 bytes, 24 f32 words a row, whose fields the stages read by name. The
|
||||
* history window and the mask-row count are placed *inside* that plane by `fill_chunk` and
|
||||
* `idx_max`, so the draw produces a window that fits the plane and a `mask_row` that is a row of
|
||||
* the bitmask plane (or -1, the unconstrained row sharing a short plane). Both are the stage's
|
||||
* parameters to obey, not this file's invention.
|
||||
* The sampler's `params` operand is one plane with fill domain `RAD_FILL_SAMPLE_PARAMS`: the ABI's
|
||||
* `RadSampleParams`, 96 bytes (24 f32 words) a row. `fill_chunk` and `idx_max` place the history
|
||||
* window and the mask-row count inside it, so the draw yields a window that fits and a `mask_row`
|
||||
* that is a row of the bitmask plane (or -1, an unconstrained row).
|
||||
*/
|
||||
|
||||
/* The sampler's parameter plane: [M, 24] f32, one RadSampleParams a row. */
|
||||
@@ -569,13 +533,11 @@ RadOpdDesc opd_params(int64_t M, int64_t hist, int64_t mask_rows) {
|
||||
return d;
|
||||
}
|
||||
|
||||
/* The widths libref's shapes choose, and the reasons it gives for them: the history window is what
|
||||
* a deployment sizes the plane to, 64 is plausible; the candidate width is narrower than the
|
||||
* vocabulary and wide enough for the narrowing stages. */
|
||||
/* libref's widths: a plausible deployment history window, and a candidate width narrower than the
|
||||
* vocabulary but wide enough for the narrowing stages. */
|
||||
enum { kHist = 64, kCand = 64 };
|
||||
|
||||
/* `n_vocab` rows of f32 logits, and the parameter plane, the pair every vocabulary-width stage
|
||||
* opens with. */
|
||||
/* Reads M and n_vocab, the extents every vocabulary-width stage opens with. */
|
||||
int sample_vocab(const RadParam* p, int n_p, int64_t* M, int64_t* nv) {
|
||||
*M = gi(p, n_p, "M", 0);
|
||||
*nv = gi(p, n_p, "n_vocab", 0);
|
||||
@@ -590,10 +552,8 @@ int r3d_shape_silu_mul(const RadParam* p, int n_p, int operand, RadOpdDesc* out)
|
||||
R3D_PICK(list);
|
||||
}
|
||||
|
||||
/* The fused add + norm + quantiser. `residual` is INOUT and the sum it holds is the bf16 one the
|
||||
* variance reads, so x and residual are the same shape by the op's meaning, not by convenience;
|
||||
* `out_bf16`, optional, is what the codes were taken over, and it exists so a test can read the
|
||||
* value the quantiser saw. */
|
||||
/* The fused add + norm + quantiser. `residual` is INOUT and holds the bf16 sum the variance reads,
|
||||
* so it has x's shape. The optional `out_bf16` is the value the codes were taken over, for tests. */
|
||||
int r3d_shape_rmsnorm_quant_fp8(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const uint32_t D = act_dt(p, n_p);
|
||||
const int64_t M = gi(p, n_p, "M", 0), n = gi(p, n_p, "n", 0);
|
||||
@@ -604,11 +564,9 @@ int r3d_shape_rmsnorm_quant_fp8(const RadParam* p, int n_p, int operand, RadOpdD
|
||||
R3D_PICK(list); /* x, residual, w, q, scale, out_bf16 */
|
||||
}
|
||||
|
||||
/* Paged attention. The context is deeper than the query: one sequence whose context is its query
|
||||
* length is the smallest geometry, and the one that exercises nothing -- no deep paging, no KV
|
||||
* split, no window -- so `ctx`, when a harness passes it, wins, and is never below the query.
|
||||
* `q_len` is the ranged parameter; a decode row and a 2048-token prefill chunk are different
|
||||
* kernels (docs/OPS.md). */
|
||||
/* Paged attention over one sequence. A context equal to the query length exercises no deep paging,
|
||||
* KV split or window, so `ctx` wins when given and is never below the query. `q_len` is the ranged
|
||||
* parameter; decode and prefill chunks are different kernels (docs/OPS.md). */
|
||||
int r3d_shape_attn_paged(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const int64_t M = gi(p, n_p, "q_len", 0);
|
||||
const int64_t hd = gi(p, n_p, "head_dim", 0);
|
||||
@@ -618,9 +576,8 @@ int r3d_shape_attn_paged(const RadParam* p, int n_p, int operand, RadOpdDesc* ou
|
||||
if (M <= 0 || hd <= 0 || bs <= 0 || gqa <= 0 || nh <= 0 || nh % gqa) return RAD_E_UNSUPPORTED;
|
||||
const int64_t kvh = nh / gqa;
|
||||
const int64_t S = 1;
|
||||
/* `ctx`, as the reference spells it -- and it is NOT the band's `max_ctx`, which is why a
|
||||
* geometry that wants a context deeper than its query chunk has to name `ctx`. When no case
|
||||
* names it, the context is the query chunk. */
|
||||
/* `ctx` as the reference spells it, not the band's `max_ctx`; absent, the context is the
|
||||
* query chunk. */
|
||||
int64_t ctx = gi(p, n_p, "ctx", 0);
|
||||
if (ctx < M) ctx = M;
|
||||
const int64_t max_blocks = (ctx + bs - 1) / bs;
|
||||
@@ -644,8 +601,8 @@ int r3d_shape_attn_paged(const RadParam* p, int n_p, int operand, RadOpdDesc* ou
|
||||
R3D_PICK(list);
|
||||
}
|
||||
|
||||
/* Exact per-row top-R. `pairs` is described because its packing is checked: an int32 id's bits in
|
||||
* the first slot beside its value, so the plane is f32 to hold the value. */
|
||||
/* Exact per-row top-R. `pairs` packs an int32 id's bits in the first slot beside its value, in an
|
||||
* f32 plane. */
|
||||
int r3d_shape_row_topk(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const int64_t M = gi(p, n_p, "M", 0), N = gi(p, n_p, "N", 0), R = gi(p, n_p, "R", 0);
|
||||
if (M <= 0 || N <= 0 || R <= 0) return RAD_E_UNSUPPORTED;
|
||||
@@ -673,15 +630,11 @@ int r3d_shape_sample_temp(const RadParam* p, int n_p, int operand, RadOpdDesc* o
|
||||
R3D_PICK(list); /* logits INOUT, params */
|
||||
}
|
||||
|
||||
/* DFlash2's candidate walk, slot for slot from libref's shape hook. `cand` and `anchor` hold token
|
||||
* ids bounded by n_vocab so the codebook gather stays inside the plane, and cand/unary/hp carry one
|
||||
* row per (sequence, step), M * steps. */
|
||||
/* The gated MLP band, slot for slot from libref's shape hook: n = N/2, the weight's scales per
|
||||
* (ceil(N/group), ceil(K/group)) in **bf16** (the container's `weight_scale_inv`, which writes
|
||||
* `o.t(RAD_BF16, {nb, kb})`), the output's codes per (M, n) and its scales per (M, ceil(n/group)).
|
||||
* The row's set is fp8a8 only, group a power of two in [32, 256], no wider than n, and K a whole
|
||||
* number of groups -- a hook that describes a shape the kernel refuses is the bug class this file
|
||||
* exists to prevent. */
|
||||
* (ceil(N/group), ceil(K/group)) in bf16 (the container's `weight_scale_inv`), the output's codes
|
||||
* per (M, n) and its scales per (M, ceil(n/group)). The checks mirror the kernel's: fp8a8 only,
|
||||
* group a power of two in [32, 256] no wider than n, and K a whole number of groups, so the hook
|
||||
* never describes a shape the kernel refuses. */
|
||||
int r3d_shape_gemm_nt_q_gated(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const int64_t M = gi(p, n_p, "M", 0);
|
||||
const int64_t N = gi(p, n_p, "N", 0);
|
||||
@@ -707,6 +660,9 @@ int r3d_shape_gemm_nt_q_gated(const RadParam* p, int n_p, int operand, RadOpdDes
|
||||
R3D_PICK(list);
|
||||
}
|
||||
|
||||
/* DFlash2's candidate walk, slot for slot from libref's shape hook. `cand` and `anchor` hold token
|
||||
* ids bounded by n_vocab so the codebook gather stays inside the plane; cand/unary/hp carry one
|
||||
* row per (sequence, step), M * steps. */
|
||||
int r3d_shape_dflash_select(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const int64_t M = gi(p, n_p, "M", 0);
|
||||
const int64_t steps = gi(p, n_p, "steps", 0);
|
||||
@@ -760,10 +716,9 @@ int r3d_shape_sample_topk(const RadParam* p, int n_p, int operand, RadOpdDesc* o
|
||||
R3D_PICK(list);
|
||||
}
|
||||
|
||||
/* The four narrowing stages -- top-p, min-p, typical, XTC -- in place over the candidate set.
|
||||
* The set is drawn, not sorted: the ABI has no descending fill domain, and top-p is a prefix rule
|
||||
* in candidate order, which applies equally to a random order. An implementation that sorted
|
||||
* internally would genuinely disagree with the schema. */
|
||||
/* The four narrowing stages (top-p, min-p, typical, XTC), in place over the candidate set. The set
|
||||
* is drawn, not sorted: the ABI has no descending fill domain, and top-p is a prefix rule in
|
||||
* candidate order, which applies to any order. */
|
||||
int r3d_shape_narrow(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const int64_t M = gi(p, n_p, "M", 0);
|
||||
const int64_t nc = gi(p, n_p, "n_cand", 0);
|
||||
@@ -782,9 +737,8 @@ int r3d_shape_sample_pick(const RadParam* p, int n_p, int operand, RadOpdDesc* o
|
||||
R3D_PICK(list); /* cand_idx, cand_val, params (seed and pos), token */
|
||||
}
|
||||
|
||||
/* Penalties and DRY: logits, params with a history window inside it, and the history itself -- real
|
||||
* token ids that repeat as text repeats. DRY's breaker plane marks about half its positions, so the
|
||||
* exemption runs as well as the penalty. */
|
||||
/* Penalties and DRY: logits, params with a history window, and the history of token ids. DRY's
|
||||
* breaker plane marks about half its positions, so the exemption runs as well as the penalty. */
|
||||
int r3d_shape_sample_penalties(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
int64_t M = 0, nv = 0;
|
||||
if (sample_vocab(p, n_p, &M, &nv) < 0) return RAD_E_UNSUPPORTED;
|
||||
@@ -798,8 +752,8 @@ int r3d_shape_sample_penalties(const RadParam* p, int n_p, int operand, RadOpdDe
|
||||
int r3d_shape_sample_dry(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
int64_t M = 0, nv = 0;
|
||||
if (sample_vocab(p, n_p, &M, &nv) < 0) return RAD_E_UNSUPPORTED;
|
||||
const RadOpdDesc brk = opd_idx(M, 2, -1, 0, kHist);
|
||||
const_cast<RadOpdDesc&>(brk).dtype = (uint32_t)RAD_U8;
|
||||
RadOpdDesc brk = opd_idx(M, 2, -1, 0, kHist);
|
||||
brk.dtype = (uint32_t)RAD_U8;
|
||||
const RadOpdDesc list[] = {
|
||||
opd((uint32_t)RAD_F32, M, nv), opd_params(M, kHist, 0),
|
||||
opd_idx(M, nv, -1, RAD_OPD_F_IDX_HISTORY, kHist), brk
|
||||
@@ -820,8 +774,7 @@ int r3d_shape_sample_ngram_ban(const RadParam* p, int n_p, int operand, RadOpdDe
|
||||
R3D_PICK(list);
|
||||
}
|
||||
|
||||
/* The four narrowing stages share the description above; the op names which predicate the kernel
|
||||
* applies, and the schema they share says the operands are the same. */
|
||||
/* The four narrowing stages share one description; the op names the predicate. */
|
||||
int r3d_shape_sample_topp(const RadParam* p, int n_p, int o, RadOpdDesc* out) {
|
||||
return r3d_shape_narrow(p, n_p, o, out);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user