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_plugin.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

285 lines
17 KiB
C

/* r3d_plugin.h -- what libr3d's translation units share.
*
* The row table (r3d_rows.cpp) and the operand descriptions (r3d_shapes.cpp) are pure host C++ and
* include only this file and the ABI; the launch shims include r3d.h and so <hip/hip_runtime.h>.
* The split lets the constraint table compile and be checked without ROCm, which is why every shim
* is declared here rather than beside its definition.
*
* Nothing in this header may include r3d.h.
*/
#ifndef RAD_R3D_PLUGIN_H
#define RAD_R3D_PLUGIN_H
#include "r3d_args.h"
/* ================================================================== the priority ladder
*
* The selector ranks candidate rows by an integer priority (spec §2.1); where two rows of one op
* match a geometry, it decides which serves. Each group states the reason for its order. Values are
* spaced by ten so a row can be inserted between two without renumbering.
*
* Most ops have a single row: the matrix work runs on the dot2 path, whose lane map takes the dtype
* as a template argument, so the kernel that serves bf16 also serves f16. Where one op has two
* rows, the reason is geometry and is stated next to the pair.
*/
enum {
/* One row for the op; nothing competes with it. */
R3D_PRIO_ONLY = 50,
/* `attn_paged` is banded on q_len, and the decode row's bound (q_len <= 16) overlaps the
* prefill row: a speculative verify at 8 query tokens matches both, and the decode kernel
* (split-KV, one head group per workgroup, no query tile) is faster there. Decode outranks
* prefill, as in libr4d. */
R3D_PRIO_ATTN_DECODE = 70,
R3D_PRIO_ATTN_PREFILL = 60,
/* The skinny bf16 GEMM's band is M <= 16 and the tiled one's starts at M >= 16. At 16 rows the
* skinny kernel wins: it keeps the whole weight stream in flight with one wave per row and has
* no LDS staging. The overlap is deliberate; a caller that wants the tiled kernel at 16 rows
* asks with M=17, as at libr4d's m64/tiled boundary. */
R3D_PRIO_GEMM_M16 = 60,
R3D_PRIO_GEMM_TILED = 40,
/* The vocab GEMM (N = the vocabulary): one row, M <= 16, split-K across the whole card. The
* tiled GEMM has no row for these N, so this priority only matters against a future tiled
* vocab row. */
R3D_PRIO_VOCAB_M1 = 60,
/* An fp8 weight row and an int8 weight row never compete: no dtype string reaches both, and
* the container's encoding picks which is legal. */
R3D_PRIO_GEMM_FP8 = 60,
/* One row per sampler stage, on f32 logits, so there is nothing to rank yet. A second row for
* a stage (an i8 logits form, say) would rank against this value. */
R3D_PRIO_SAMPLE = 50
};
/* ================================================================== the dtype codes a C
* launcher takes
*
* The launchers in r3d.h take a dtype as an int: it is a runtime value for the ops that are one
* kernel over several dtypes (cast, fill, dequant) and a template argument everywhere else. The
* codes are libr3d's own, not rad_types.h's RAD_DT_*, because the launcher signature is private to
* a shim and its kernel and a dense 0..n switches more cleanly than the ABI's sparse enum. Every
* shim translates. */
enum {
R3D_DT_BF16 = 0,
R3D_DT_F16 = 1,
R3D_DT_F32 = 2,
R3D_DT_I32 = 3,
R3D_DT_I64 = 4,
R3D_DT_U8 = 5, /* byte codes: fp8 e4m3, read with the codec */
R3D_DT_I8 = 6,
R3D_DT_E5M2 = 7,
R3D_DT_N = 8
};
/* A RadTensor's dtype to the code above, or -1 for one libr3d has no kernel for. */
static inline int r3d_dt_code(uint32_t dt) {
switch (dt) {
case RAD_BF16: return R3D_DT_BF16;
case RAD_F16: return R3D_DT_F16;
case RAD_F32: return R3D_DT_F32;
case RAD_I32:
case RAD_U32: return R3D_DT_I32;
case RAD_I64: return R3D_DT_I64;
case RAD_F8E4M3: return R3D_DT_U8;
case RAD_F8E5M2: return R3D_DT_E5M2;
case RAD_I8: return R3D_DT_I8;
case RAD_U8: return R3D_DT_U8;
default: return -1;
}
}
/* The bytes an element of that code occupies. */
static inline int r3d_dt_size(int code) {
static const int kSize[R3D_DT_N] = { 2, 2, 4, 4, 8, 1, 1, 1 };
return (code >= 0 && code < R3D_DT_N) ? kSize[code] : 0;
}
/* The activation an `act` parameter names, as the kernels' template argument. */
enum { R3D_ACT_SILU = 0, R3D_ACT_SIGMOID = 1, R3D_ACT_GELU = 2 };
/* rope's `mode`, numbered as libref's kModes. Only neox and gptj are implemented, and the rows'
* constraints say so, so a model that needs mrope is not routed here. */
enum { R3D_ROPE_NEOX = 0, R3D_ROPE_GPTJ = 1 };
/* ================================================================== the row table's entry points
*
* Declared here so r3d_rows.cpp, host code built without ROCm, can name them. Each is defined and
* documented beside its kernel, in the unit named below. A shim is a `RadLaunchFn`: operands read
* with r3d_args.h, dtype from the tensor, row count from the operand. A shape hook is a
* `RadShapeFn`: the extent and content domain of one operand (r3d_shapes.cpp).
*/
extern "C" {
/* --- r3d_model_bf16.hip --- */
int r3d_rad_rmsnorm (const RadArgs* a, RadStream s);
int r3d_rad_layernorm (const RadArgs* a, RadStream s);
int r3d_rad_add (const RadArgs* a, RadStream s);
int r3d_rad_mul (const RadArgs* a, RadStream s);
int r3d_rad_cast (const RadArgs* a, RadStream s);
int r3d_rad_silu (const RadArgs* a, RadStream s);
int r3d_rad_gelu (const RadArgs* a, RadStream s);
int r3d_rad_sigmoid (const RadArgs* a, RadStream s);
int r3d_rad_act_mul (const RadArgs* a, RadStream s);
int r3d_rad_softmax (const RadArgs* a, RadStream s);
int r3d_rad_scale (const RadArgs* a, RadStream s);
int r3d_rad_fill (const RadArgs* a, RadStream s);
int r3d_rad_rope (const RadArgs* a, RadStream s);
int r3d_rad_kv_store (const RadArgs* a, RadStream s);
int r3d_rad_embed_lookup (const RadArgs* a, RadStream s);
int r3d_rad_gather_rows (const RadArgs* a, RadStream s);
int r3d_rad_scatter_rows (const RadArgs* a, RadStream s);
/* --- r3d_quant_fp8.hip --- */
int r3d_rad_quant_act_fp8 (const RadArgs* a, RadStream s);
int r3d_rad_quant_act_i8 (const RadArgs* a, RadStream s);
int r3d_rad_quant_act_i8g (const RadArgs* a, RadStream s);
int r3d_rad_gated_quant_fp8 (const RadArgs* a, RadStream s);
int r3d_rad_dequant (const RadArgs* a, RadStream s);
/* --- r3d_gemm_bf16_nt_m16.hip, r3d_gemm_fp8a16_nt_m1.hip --- */
int r3d_rad_gemm_nt_bf16_m16 (const RadArgs* a, RadStream s);
int r3d_rad_gemm_nt_q_fp8 (const RadArgs* a, RadStream s);
/* --- r3d_shapes.cpp --- */
int r3d_shape_rmsnorm (const RadParam* p, int n_p, int operand, RadOpdDesc* out);
int r3d_shape_layernorm (const RadParam* p, int n_p, int operand, RadOpdDesc* out);
int r3d_shape_add (const RadParam* p, int n_p, int operand, RadOpdDesc* out);
int r3d_shape_mul (const RadParam* p, int n_p, int operand, RadOpdDesc* out);
int r3d_shape_act_mul (const RadParam* p, int n_p, int operand, RadOpdDesc* out);
int r3d_shape_cast (const RadParam* p, int n_p, int operand, RadOpdDesc* out);
int r3d_shape_silu (const RadParam* p, int n_p, int operand, RadOpdDesc* out);
int r3d_shape_gelu (const RadParam* p, int n_p, int operand, RadOpdDesc* out);
int r3d_shape_sigmoid (const RadParam* p, int n_p, int operand, RadOpdDesc* out);
int r3d_shape_softmax (const RadParam* p, int n_p, int operand, RadOpdDesc* out);
int r3d_shape_scale (const RadParam* p, int n_p, int operand, RadOpdDesc* out);
int r3d_shape_fill (const RadParam* p, int n_p, int operand, RadOpdDesc* out);
int r3d_shape_rope (const RadParam* p, int n_p, int operand, RadOpdDesc* out);
int r3d_shape_kv_store (const RadParam* p, int n_p, int operand, RadOpdDesc* out);
int r3d_shape_embed_lookup (const RadParam* p, int n_p, int operand, RadOpdDesc* out);
int r3d_shape_gather_rows (const RadParam* p, int n_p, int operand, RadOpdDesc* out);
int r3d_shape_scatter_rows (const RadParam* p, int n_p, int operand, RadOpdDesc* out);
int r3d_shape_quant_act_fp8 (const RadParam* p, int n_p, int operand, RadOpdDesc* out);
int r3d_shape_quant_act_i8 (const RadParam* p, int n_p, int operand, RadOpdDesc* out);
int r3d_shape_quant_act_i8g (const RadParam* p, int n_p, int operand, RadOpdDesc* out);
int r3d_shape_gated_quant_fp8 (const RadParam* p, int n_p, int operand, RadOpdDesc* out);
int r3d_shape_dequant (const RadParam* p, int n_p, int operand, RadOpdDesc* out);
int r3d_shape_gemm_nt (const RadParam* p, int n_p, int operand, RadOpdDesc* out);
int r3d_shape_gemm_nt_q (const RadParam* p, int n_p, int operand, RadOpdDesc* out);
int r3d_shape_gemm_nt_q_mxfp4a8(const RadParam* p, int n_p, int operand, RadOpdDesc* out);
/* --- r3d_shapes.cpp: the fused-op, attention and sampler shape hooks --- */
extern "C" int r3d_shape_silu_mul (const RadParam*, int, int, RadOpdDesc*);
extern "C" int r3d_shape_attn_paged (const RadParam*, int, int, RadOpdDesc*);
extern "C" int r3d_shape_gemm_nt_q_gated (const RadParam*, int, int, RadOpdDesc*);
/* r3d_gemm_nt_q_w2a8.hip: the w2a8 vocab projection's layout hook, one of the library's three
* (with the fp8 weight's and the logits head's, below). It repacks nothing: it presents the
* container's `scale` and `zero` planes as the single joined `b_scale` operand the way rad_planes
* stores them, transcribed from ref_layout_gemm_q. Without it the plan would have a placement with
* no reader. */
extern "C" int r3d_layout_gemm_nt_q_w2a8(const RadParam*, int, int, const RadEncoding*,
const int* sel, const RadTensor* planes, int n_planes,
RadLayout*);
extern "C" int r3d_shape_dflash_select (const RadParam*, int, int, RadOpdDesc*);
extern "C" int r3d_shape_row_topk (const RadParam*, int, int, RadOpdDesc*);
extern "C" int r3d_shape_sample_argmax (const RadParam*, int, int, RadOpdDesc*);
extern "C" int r3d_shape_sample_temp (const RadParam*, int, int, RadOpdDesc*);
extern "C" int r3d_shape_sample_topk (const RadParam*, int, int, RadOpdDesc*);
extern "C" int r3d_shape_sample_topp (const RadParam*, int, int, RadOpdDesc*);
extern "C" int r3d_shape_sample_minp (const RadParam*, int, int, RadOpdDesc*);
extern "C" int r3d_shape_sample_typical (const RadParam*, int, int, RadOpdDesc*);
extern "C" int r3d_shape_sample_xtc (const RadParam*, int, int, RadOpdDesc*);
extern "C" int r3d_shape_sample_pick (const RadParam*, int, int, RadOpdDesc*);
extern "C" int r3d_shape_sample_penalties (const RadParam*, int, int, RadOpdDesc*);
extern "C" int r3d_shape_sample_dry (const RadParam*, int, int, RadOpdDesc*);
extern "C" int r3d_shape_sample_ngram_ban (const RadParam*, int, int, RadOpdDesc*);
extern "C" int r3d_shape_sample_mask (const RadParam*, int, int, RadOpdDesc*);
extern "C" int r3d_shape_sample_logprobs (const RadParam*, int, int, RadOpdDesc*);
/* --- r3d_gemm_nt_q_w2a8.hip, r3d_gemm_fp8a8_gated.hip, r3d_dflash_f32.hip; r3d_sample_f32.hip's
draw spine (row_topk, argmax, temp, topk, pick, the four narrowings) and
r3d_sample_pre_f32.hip's pre-filters (penalties, dry, ngram_ban, mask, logprobs) --- */
int r3d_rad_gemm_nt_q_w2a8 (const RadArgs* a, RadStream s);
int r3d_rad_gemm_nt_q_gated (const RadArgs* a, RadStream s);
int r3d_rad_dflash_select (const RadArgs* a, RadStream s);
int r3d_rad_row_topk (const RadArgs* a, RadStream s);
int r3d_rad_sample_argmax (const RadArgs* a, RadStream s);
int r3d_rad_sample_temp (const RadArgs* a, RadStream s);
int r3d_rad_sample_topk (const RadArgs* a, RadStream s);
int r3d_rad_sample_pick (const RadArgs* a, RadStream s);
int r3d_rad_sample_topp (const RadArgs* a, RadStream s);
int r3d_rad_sample_minp (const RadArgs* a, RadStream s);
int r3d_rad_sample_typical (const RadArgs* a, RadStream s);
int r3d_rad_sample_xtc (const RadArgs* a, RadStream s);
int r3d_rad_sample_penalties (const RadArgs* a, RadStream s);
int r3d_rad_sample_dry (const RadArgs* a, RadStream s);
int r3d_rad_sample_ngram_ban (const RadArgs* a, RadStream s);
int r3d_rad_sample_mask (const RadArgs* a, RadStream s);
int r3d_rad_sample_logprobs (const RadArgs* a, RadStream s);
/* --- r3d_attn_paged_h128.hip: the paged attention, banded on q_len. The decode kernel is
* split-KV over the cache with one head group per workgroup; the prefill kernel stages a 16-token
* query tile. */
int r3d_rad_attn_decode_h128 (const RadArgs* a, RadStream s);
int r3d_rad_attn_prefill_h128 (const RadArgs* a, RadStream s);
/* --- r3d_gemm_fp8a8_tiled.hip: the quantised GEMM above the decode heights, and the reader for
* the container's fp8 weight: codes and per-128x128 scale as two planes behind `b_scale`. The row
* carries its own layout hook because the reference's gemm_nt_q hook answers nothing for this
* dtype (ref_layout_gemm_q reads zp_grid, which has no fp8a8 entry). */
int r3d_rad_gemm_nt_q_fp8_tiled (const RadArgs* a, RadStream s);
int r3d_rad_gemm_nt_q_w2a8_tiled (const RadArgs* a, RadStream s);
extern "C" int r3d_layout_gemm_q_fp8_weight(const RadParam*, int, int, const RadEncoding*,
const int*, const RadTensor*, int, RadLayout*);
/* --- r3d_gemm_mxfp4a8.hip: the OCP-MXFP4 weight against the fp8 activation, both heights. The
* weight is two plain u8 planes (e2m1 codes [N, K/2], e8m0 scale [N, K/32]) read in place, so the
* row has no layout hook. Its shape hook is separate because these planes are not the fp8 grid's
* E4M3 codes and block scale. */
int r3d_rad_gemm_nt_q_mxfp4a8_m16 (const RadArgs* a, RadStream s);
int r3d_rad_gemm_nt_q_mxfp4a8_tiled(const RadArgs* a, RadStream s);
/* --- r3d_gemm_inta8.hip: the symmetric int4 and int8 weight grids at decode heights. The weight
* is signed codes (I4 two per byte, or I8) with a plain f16 scale per (row, 128 of K). No zero
* point and no a_sum, so both planes are read in place and the rows have no layout hook. */
int r3d_rad_gemm_nt_q_w4a8_m16(const RadArgs* a, RadStream s);
int r3d_rad_gemm_nt_q_w8a8_m16(const RadArgs* a, RadStream s);
int r3d_shape_gemm_nt_q_w4a8(const RadParam* p, int n_p, int operand, RadOpdDesc* out);
int r3d_shape_gemm_nt_q_w8a8(const RadParam* p, int n_p, int operand, RadOpdDesc* out);
/* The relayout writers for the three layout hooks, each delegating to rad_planes_relayout. The
* loader converts a quantised weight into the joined operand the plan names, and only the row that
* reads it can say how. See abi/rad_abi.h. */
extern "C" int r3d_relayout_logits_head(const RadParam*, int, int, const RadEncoding*, const int*,
const RadTensor*, int, void*, int64_t);
extern "C" int r3d_relayout_gemm_q_fp8_weight(const RadParam*, int, int, const RadEncoding*,
const int*, const RadTensor*, int, void*, int64_t);
extern "C" int r3d_relayout_gemm_nt_q_w2a8(const RadParam*, int, int, const RadEncoding*,
const int*, const RadTensor*, int, void*, int64_t);
/* --- r3d_vocab_bf16.hip: the vocabulary projection, both heights. Its shape hook
* (r3d_shapes.cpp) transcribes ref_logits_gemm_shape, three plain tensors with nothing joined, so
* the band the engine declares is the band kbench fills. */
int r3d_rad_logits_gemm_bf16 (const RadArgs* a, RadStream s);
/* The head's layout hook: `output.weight` at a llama_dense_fp8 checkpoint is E4M3 codes and a bf16
* scale per 128x128 block, stored as two container planes behind the one `lm_head` operand, and the
* mapped tier does not place a weight a loaded library cannot read in place. Transcribed from
* ref_layout_head, with the statics it uses inlined. It moves no bytes: a round-trip through its
* relayout writer (above) reproduces the planes exactly, and the writer exists for that
* round-trip. */
extern "C" int r3d_layout_logits_head(const RadParam*, int, int, const RadEncoding*,
const int*, const RadTensor*, int, RadLayout*);
int r3d_rad_gemm_nt_bf16_tiled (const RadArgs* a, RadStream s);
int r3d_shape_logits_gemm_bf16 (const RadParam*, int, int, RadOpdDesc*);
/* --- r3d_model_bf16.hip: the fused MLP activation and the fused norm-quantiser --- */
int r3d_rad_silu_mul (const RadArgs* a, RadStream s);
int r3d_rad_rmsnorm_quant_fp8 (const RadArgs* a, RadStream s);
int r3d_shape_rmsnorm_quant_fp8 (const RadParam*, int, int, RadOpdDesc*);
} /* extern "C" */
#endif /* RAD_R3D_PLUGIN_H */