This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/libr3d/r3d_args.h
T
Deadcode 4a61d7296b 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).
2026-10-29 12:04:24 +01:00

299 lines
15 KiB
C++

/* r3d_args.h -- how a libr3d entry point reads its arguments.
*
* An entry point takes `(const RadArgs*, RadStream)` and returns a status, the RadLaunchFn
* contract every plugin implements; there is no adapter layer. This header holds the shared
* helpers for reading parameters, finding operands and testing contiguity, mostly one line over
* rad_abi.h.
*
* Nothing here includes r3d.h or HIP, so host translation units (the row table, the shape hooks)
* can include it and compile without ROCm.
*/
#ifndef R3D_ARGS_H
#define R3D_ARGS_H
#include "rad_abi.h"
#include "rad_plugin.h"
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
/* ================================================================== parameter access
*
* Forwards to rad_abi.h's static inline helpers under libr3d's names. */
static inline int r3d_geti(const RadArgs* a, const char* k, long long* out) {
return rad_args_geti(a, k, out);
}
static inline long long r3d_geti_or(const RadArgs* a, const char* k, long long dflt) {
return rad_args_geti_or(a, k, dflt);
}
static inline const char* r3d_gets(const RadArgs* a, const char* k) {
return rad_args_gets(a, k);
}
/* A float parameter is RAD_P_F64 (rad_types.h, docs/OPS.md "The four rules"), so the step path
* reads `dval` without strtod. Deliberately laxer than rad_args_getf_or: string and integer forms
* are accepted because a tool or selftest may call a launch directly, with no declare to enforce
* the type. */
static inline double r3d_getf(const RadArgs* a, const char* k, double dflt) {
if (!a || !k) return dflt;
for (int i = 0; i < a->n_p; ++i) {
if (!a->p[i].key || std::strcmp(a->p[i].key, k) != 0) continue;
if (a->p[i].kind == RAD_P_F64) return a->p[i].dval;
if (a->p[i].kind == RAD_P_INT) return (double)a->p[i].ival;
if (a->p[i].kind == RAD_P_STR && a->p[i].sval && a->p[i].sval[0]) {
char* end = nullptr;
const double v = std::strtod(a->p[i].sval, &end);
return (end && end != a->p[i].sval) ? v : dflt;
}
return dflt;
}
return dflt;
}
/* An enumerated parameter: `act` is "silu" or "sigmoid" rather than 0 or 1, so a --debug-graph dump
* names what runs. Returns -1 for an unknown name, which callers turn into RAD_E_INVAL. `alt` is an
* integer-valued key for the same choice, read only when the named key is absent. */
static inline int r3d_act_code(const RadArgs* a, const char* key, const char* alt, int dflt) {
if (const char* s = r3d_gets(a, key)) {
if (!std::strcmp(s, "silu") || !std::strcmp(s, "swish")) return 0;
if (!std::strcmp(s, "sigmoid")) return 1;
return -1;
}
long long v = 0;
if (alt && r3d_geti(a, alt, &v)) return (v == 0 || v == 1) ? (int)v : -1;
return dflt;
}
/* Same, off a bare RadParam list (init() and the layout hooks); forwards to rad_plugin.h's
* rad_param_*. */
static inline long long r3d_pgeti_or(const RadParam* p, int n_p, const char* k, long long dflt) {
return rad_param_geti(p, n_p, k, dflt);
}
static inline const char* r3d_pgets(const RadParam* p, int n_p, const char* k) {
return rad_param_gets(p, n_p, k, nullptr);
}
/* ================================================================== operand access
*
* An absent optional operand has a null RadTensor.data (rad_abi.h). For a required operand the
* caller turns null into RAD_E_INVAL: it is an issue bug, not a shape the kernel declines.
*/
static inline const RadTensor* r3d_opt(const RadArgs* a, int i) { return rad_arg_in(a, i); }
/* Whether an optional operand is present. The engine passes an unbound optional operand as a
* descriptor with null `data`, so a bare `if (t)` is wrong: a shim that dtype-checks such a
* descriptor refuses a call that never reads it (e.g. an empty `out_bf16` slot of
* `rmsnorm_quant_fp8`). Every test of an optional operand goes through here. */
static inline bool r3d_has(const RadTensor* t) { return t && t->data; }
static inline const void* r3d_p(const RadTensor* t) { return t ? t->data : nullptr; }
/* The same for an operand a kernel writes. */
static inline void* r3d_wp(const RadTensor* t) { return t ? t->data : nullptr; }
static inline long r3d_addr(const RadTensor* t) { return t ? (long)(uintptr_t)t->data : 0L; }
static inline long long r3d_dim(const RadTensor* t, int i) {
if (!t || i < 0 || (uint32_t)i >= t->rank) return 0;
return t->shape[i];
}
static inline long long r3d_stride(const RadTensor* t, int i) {
if (!t || i < 0 || (uint32_t)i >= t->rank) return 0;
return t->stride[i];
}
static inline long long r3d_numel(const RadTensor* t) { return rad_tensor_numel(t); }
/* [rows..., K] flattened to (M, K). Every per-row kernel owns whole rows (one workgroup per row),
* so the leading axes are only a row count. */
static inline int r3d_rows_of(const RadTensor* t, long long* M, long long* K) {
if (!t || t->rank < 1) return RAD_E_SHAPE;
*K = t->shape[t->rank - 1];
*M = 1;
for (uint32_t i = 0; i + 1 < t->rank; ++i) *M *= t->shape[i];
return (*M >= 1 && *K >= 1) ? RAD_OK : RAD_E_SHAPE;
}
/* Dense row-major over the whole tensor. Entry points take at most one pitch, so everything else
* must be tight, and the shim checks it: per spec §2.1 the constraint table settles geometry while
* contiguity is a property of the call. */
static inline int r3d_contig(const RadTensor* t) { return rad_tensor_is_contiguous(t); }
/* Contiguous in every axis but the leading one, whose stride is the row pitch libr3d takes as an
* explicit argument (`x_ld`, `a_stride`, ...). Returns the pitch in elements, or -1. */
static inline long long r3d_row_pitch(const RadTensor* t) {
if (!t || t->rank == 0) return -1;
if (t->rank == 1) return 1;
long long acc = 1;
for (int i = (int)t->rank - 1; i >= 1; --i) {
if (t->shape[i] != 1 && t->stride[i] != acc) return -1;
acc *= t->shape[i];
}
return t->stride[0];
}
/* [rows..., n] read as (rows, n): the leading axes are a row count and only the last is
* structural. Returns the pitch in elements, or -1.
*
* A column slice is refused. k out of a fused [tok, 2*kv_dim] projection, read as
* [tok*n_head_kv, head_dim] for a per-head norm, has rows `n` apart within a token and a buffer row
* apart between tokens; no single pitch describes that, and using the leading stride would read
* most heads from the wrong place. So the trailing axes must cover the whole row. */
static inline long long r3d_rows_pitch(const RadTensor* t, long long n, long long* rows) {
if (!t || t->rank < 1 || n <= 0) return -1;
const long long nel = rad_tensor_numel(t);
if (nel % n) return -1;
*rows = nel / n;
if (t->rank == 1) return n;
if (t->shape[t->rank - 1] == n) {
if (t->stride[t->rank - 1] != 1) return -1;
return t->rank >= 2 ? t->stride[t->rank - 2] : n;
}
if (rad_tensor_is_contiguous(t)) return n;
long long trail = 1;
for (int i = 1; i < (int)t->rank; ++i) trail *= t->shape[i];
return trail == n ? r3d_row_pitch(t) : -1;
}
/* An attention operand, [tokens, heads * head_dim] over its trailing axes: the token count, head
* count and row pitch in elements. False when the trailing axes are not whole heads or no single
* pitch describes the rows. */
static inline bool r3d_head_rows(const RadTensor* t, long long head_dim, long long* n_tok,
long long* n_head, long long* ld) {
long long width = 1;
for (uint32_t i = 1; i < t->rank; ++i) width *= t->shape[i];
if (t->rank < 2 || width % head_dim) return false;
*n_head = width / head_dim;
*ld = r3d_rows_pitch(t, width, n_tok);
return *ld >= 0;
}
/* 16-byte alignment, needed by every kernel that loads a dword pair per lane. The kernels do not
* check it, so the shim does. */
static inline int r3d_aligned(const void* p, unsigned bytes) {
return p ? (((uintptr_t)p & (uintptr_t)(bytes - 1)) == 0) : 1;
}
/* ================================================================== which check refused
*
* A shim refuses in many places with the same few codes, and the engine's report names the kernel
* and geometry but not the failing condition. R3D_NO() wraps a refusal and, when RAD_R3D_TRACE is
* set to a value other than "0", prints "[r3d] file:line refused -> code" to stderr. The variable
* is read once and cached; unset, the cost is one flag load on a refusal path. */
static inline int r3d_trace_on(void) {
static int on = -1;
if (on < 0) { const char* e = std::getenv("RAD_R3D_TRACE"); on = (e && *e && *e != '0'); }
return on;
}
static inline int r3d_refused(int code, const char* file, int line) {
if (r3d_trace_on()) std::fprintf(stderr, "[r3d] %s:%d refused -> %d\n", file, line, code);
return code;
}
#define R3D_NO(code) r3d_refused((code), __FILE__, __LINE__)
/* Under RAD_R3D_TRACE, prints a refused descriptor's dtype, rank, shape, strides and data pointer.
* The engine binds shape and strides in elements, and the shape is the step's, not the band's.
* R3D_STR refuses with RAD_E_STRIDE and, under the same flag, prints the reason and two named
* values. */
static inline void r3d_trace_desc(const char* what, const RadTensor* t) {
if (!r3d_trace_on()) return;
if (!t) { std::fprintf(stderr, "[r3d] %s: (absent)\n", what); return; }
std::fprintf(stderr, "[r3d] %s: dtype %u rank %u shape [", what, t->dtype, t->rank);
for (int i = 0; i < (int)t->rank; ++i) std::fprintf(stderr, "%s%lld", i ? "," : "", (long long)t->shape[i]);
std::fprintf(stderr, "] stride [");
for (int i = 0; i < (int)t->rank; ++i) std::fprintf(stderr, "%s%lld", i ? "," : "", (long long)t->stride[i]);
std::fprintf(stderr, "] data %p\n", t->data);
}
#define R3D_STR(why, a1, v1, a2, v2) \
do { \
if (r3d_trace_on()) \
std::fprintf(stderr, "[r3d] %s:%d stride refusal: %s, %s %lld, %s %lld\n",\
__FILE__, __LINE__, (why), (a1), (long long)(v1), (a2), \
(long long)(v2)); \
return R3D_NO(RAD_E_STRIDE); \
} while (0)
/* ================================================================== dtype helpers
*
* A row's `dtype` constraint is a necessary condition, not a promise about the tensors, so the
* shim tests each operand's dtype with these. */
static inline int r3d_is_bf16(const RadTensor* t) { return t && t->dtype == RAD_BF16; }
static inline int r3d_is_f16(const RadTensor* t) { return t && t->dtype == RAD_F16; }
static inline int r3d_is_f32(const RadTensor* t) { return t && t->dtype == RAD_F32; }
static inline int r3d_is_i32(const RadTensor* t) { return t && t->dtype == RAD_I32; }
static inline int r3d_is_i64(const RadTensor* t) { return t && t->dtype == RAD_I64; }
static inline int r3d_is_fp8(const RadTensor* t) {
return t && (t->dtype == RAD_F8E4M3 || t->dtype == RAD_F8E5M2);
}
static inline int r3d_is_e4m3(const RadTensor* t) { return t && t->dtype == RAD_F8E4M3; }
static inline int r3d_is_i8(const RadTensor* t) { return t && t->dtype == RAD_I8; }
static inline int r3d_is_u8(const RadTensor* t) { return t && t->dtype == RAD_U8; }
/* The dtype name a row's constraint table carries, for a shim that must match a string parameter
* against an operand. */
static inline int r3d_dt_named(uint32_t dt, const char* name) {
if (!name) return 0;
if (!std::strcmp(name, "bf16")) return dt == RAD_BF16;
if (!std::strcmp(name, "f16") || !std::strcmp(name, "fp16")) return dt == RAD_F16;
if (!std::strcmp(name, "f32") || !std::strcmp(name, "fp32")) return dt == RAD_F32;
if (!std::strcmp(name, "fp8_e4m3") || !std::strcmp(name, "e4m3")) return dt == RAD_F8E4M3;
if (!std::strcmp(name, "fp8_e5m2") || !std::strcmp(name, "e5m2")) return dt == RAD_F8E5M2;
if (!std::strcmp(name, "i8") || !std::strcmp(name, "int8")) return dt == RAD_I8;
return 0;
}
/* ================================================================== libr3d's constants
*
* Defined here because r3d_rows.cpp and r3d_shapes.cpp are host code built without ROCm and cannot
* include r3d.h, and because a .rad container records them in its layout tag. */
enum {
R3D_WAVE = 32,
/* The activation quantiser's group: the width a scale covers along n. It equals the weight
* scale's block width, a property of the checkpoint, not a knob. */
R3D_ACT_GROUP = 128,
/* Block-scaled fp8, on both axes: the checkpoint's `quantization_config.weight_block_size`,
* which the bytes on disk follow. The entry points reject a mismatch. */
R3D_FP8_BLOCK = 128,
/* The int8 weight group, the same 128 the checkpoint's scales use. */
R3D_W8_GROUP = 128,
/* The largest N a dot2 GEMM's per-lane accumulator grid spans in one wave. */
R3D_NTILE = 16,
/* The operand count of `gemm_nt_q` (a, a_scale, b, b_scale, y, a_sum, b_ref), used by the shape
* hook. The schema in r3d_rows.cpp is authoritative; rad-kbench checks the two agree. */
R3D_GEMM_Q_NOPERAND = 7
};
/* ================================================================== libr3d's tunables
*
* The knobs the row table declares, so `rad-tune` can search them and `--debug-graph` can name the
* choice (spec §15). The chosen point is appended to RadArgs::p; these structs read several knobs
* at once, with the row's defaults where a caller supplied none.
*
* sk == 0 means "the largest legal split count at issue". Split-K requires K % (sk * group) == 0,
* so no fixed default is legal at every shape, and an illegal default would make an untuned install
* fail. The GEMM unit resolves the sentinel.
*/
struct R3dGemmCfg { int wv, sk, kb, nt; }; /* waves a block, split-K, K slab, non-temporal loads */
struct R3dSplitCfg { int splits; }; /* split-KV segments; 0 is the kernel's own law */
struct R3dTopkCfg { int rc, nch; }; /* candidates a chunk keeps, chunk width in columns */
extern "C" {
/* -- the tuned configuration (r3d_rows.cpp) --
*
* Read the point the core appended to RadArgs::p, falling back to each axis's default (defined in
* the RadTunable table) when none was supplied, so a caller without a tuned key runs what an
* untuned install runs. They cannot fail: the loader refuses a plugin whose default is not one of
* the axis's values. */
void r3d_cfg_gemm (const RadArgs*, struct R3dGemmCfg*);
void r3d_cfg_split(const RadArgs*, struct R3dSplitCfg*);
void r3d_cfg_topk (const RadArgs*, struct R3dTopkCfg*);
} /* extern "C" */
#endif /* R3D_ARGS_H */