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/arch/common/rad_block_ple.h
T
Deadcode 9ed3ef7a7b radiance 1.15.0
New models and formats:
- IQR for Qwen3.8-27B: weights on the int8 grid under GPTQ, a trellis down each GPTQ column
  (IQR-V2), and IQR-V3, a computed trellis code under one scale a row, kept at its codes' width in
  VRAM and decoded in the GEMM; 15 IQR-V3 recipes from ~2 to ~7 bits a weight
  (data/recipes/q38-27b-*-v3.recipe, docs/IQR.md)
- libhalo: a kernel library for Strix Halo (gfx1151), preferred there over libr3d
- Nemotron 3.5 Embed 1B and 8B (nemotron35_embed): text and image embeddings through a Pixtral
  tower, on the card and on the CPU
- Nemotron 3.5 Rerank 8B
- /v1/systemone: Cloudflare's clef and clef-flash, Convai Innovations' Laya and Mica beside
  LiquidAI d1; scored steps read the candidates' logits from the head table with no lm_head pass
- LFM2's 8-bit trunk: block-fp8 or int8 linears with the fused producers, and the d1-3B recipe
- DeepSeek-V4.1's reply format with DSML tool calls and typed argument tags

Weights on the drive:
- The file tier reads only the experts the pass routes rows to; the draft head's weights stay in
  the host pool
- A hot VRAM cache for routed experts read off the drive, reads deposited straight into free host
  pool slots, and copyless evictions; the file tier's counters are now ssd_*
- GLM-5 Next shards its routed experts by expert, not by width, so they can live on the drive
- The stall watch counts drive reads as progress, and the DMA lane's room check reads the lane's
  own completion fence

Quantisation tooling:
- --calib-corpus, --calib-dir and --calib-layers record each linear's input Hessian; beside
  --kld-record the reference's own run records them
- --kld-top N records a sparse KL reference (the N most likely tokens and the tail's mass)
- GPTQ in a third of the time with the same codes, trellis GPTQ on the card, vector codebooks
  with coordinate descent, and rad-convert --reuse repeated, each container supplying what the
  earlier ones lack

Speculation:
- Prompt lookup alone (--spec-ngram K, --spec-ngram-min/-max): each sequence keeps an index of its
  n-grams, and a proposal is verified only when its measured step cost says it pays;
  --draft-window adaptive follows it

Speed:
- IQR decode: the narrow tile splits K for four times the int8 tile's waves and folds each 128-K
  block once; at 8 tokens down and the output projections run in 0.61-0.82x and 0.70-0.92x FP8's
  time (2-6 bits); IQ6R-S-V3 with its DFlash2 drafter steps in 26.3 ms at TP2, against 29.4 ms
  before and FP8's 27.3
- Qwen MXFP4 feed-forward: the gated fold up to 16 rows, not 8
- libr4d: the gated prefill fold, the short-prompt split row, and the rows forms of rmsnorm and
  rope; KLATE again for the head-256 gqa6 paged prefill; gram_accum on the matrix cores

Features:
- /health, /v1/health and /ping answer 503 with "status": "stuck" once the device has advanced
  nothing for a minute or a queue has faulted, so a supervisor can restart a wedged server (#356)
- --image-gen-max-pixels and --image-gen-max-refs size an image generator's buffers (#344)
- The startup report lists only the options not at their default, with a count; -v lists all
  (#250)
- --preempt-wait-ms bounds how long a preempted prefill waits for its whole-recompute room
- --placement layer_offload spills dense layers so one --max-model-len sequence fits
- Startup refuses amdgpu with CWSR off and says how to fix it;
  --proceed-with-incompatible-configuration starts anyway under an UNSUPPORTED banner

Fixes:
- The n-gram read-ahead hands each row to the reader once and reads only as far ahead as the row
  cache holds, and pauses while decode reads: about one drive read a row instead of two (#355)
- A file-tier window reads each of its runs once, not once a run per run
- The decode split law's verify floor is bounded by the batch's workgroups: max_seqs 256 no longer
  declares 64x the scratch (#342)
- A prefetch DMA list's mark stays out of the signal pool until the engine has read it, and a
  prefill stage copies only the blocks its own tables name (#343)
- The split-KV row sum is read with fetch-inactive (#328)
- LFM2 on the host backend: libref and libavx serve the row-form ops it declares (#348)
- The preempted-prefill gate skips a KV group that holds no blocks (#352)
- Every request entry point bounded, gated and sanitised (JSON depth, invalid UTF-8, control bytes
  in the log, the dashboard's CSP, cross-origin browsers on the loopback-only routes); a prompt id
  outside the vocabulary refused at admission; --api-key "" refused at startup
- The telemetry the HTTP thread reads is atomic
- An RMS norm row no longer depends on how many rows its launch carries; prefill attention's
  rescale is decided by live rows only
- A --kld-record prefill chunk that finishes no sequence gets its logits on a tied-head model
- A kernel's lazily made counter block is zeroed outside the lock other ranks take (a TP2 first-step
  hang)

Changes: Plugin ABI 32. Every plugin exports a layout fingerprint of the ABI structs and the loader
refuses a plugin without it or with another (#302), so out-of-tree plugins must be rebuilt.
Appended: RadBuildCtx calib_* and gen_max_*, RadArchProbe gen_max_*, RadBatch score_only and
plugin_words, RadDeviceProps::unified, RadChatFormat arg_json_value_prefix.
2026-10-30 22:01:01 +01:00

490 lines
26 KiB
C++

/* rad_block_ple.h -- Qwen4-Exp's Per-Layer Embedding, declared and issued.
*
* ============================== WHAT IT IS ==============================
*
* One layer has a second embedding table addressed by a hash of the last three tokens.
* Qwen3.8-Flash-Next's is 320,001,536 rows of 160 (51 of the checkpoint's 125 billion
* parameters), read sixteen rows per token and mixed into the residual stream by a dot product
* against the stream:
*
* ids = ngram_ids(tokens) [T, 16] a hash of the last 3 ids, per head
* e = embed_lookup_q(ids, table, scale) [T, 2560] 16 x 160, flattened
* k = key_proj(e) [T, hc*n] v = value_proj(e) [T, n]
* gv,
* gvn = ple_gate(k, h, v, ...) the dot, the signed sqrt, the broadcast
* out = ple_conv(gvn, resid = gv, ...) dilated depthwise conv, silu, and the add
* h += out
*
* This runs before the attention hyper-connection reads the stream, so `h` is the wide, un-normed
* residual.
*
* ============================== THE TABLE ==============================
*
* The 51.2 GB table cannot be staged onto a card, so `embed_lookup_q` reads it where
* --ngram-placement put it: a host gather over the container's mmap or a locked RAM copy
* (libavx/avx_ngram.cpp), a device kernel reading a pinned copy over the link, or a card's own
* copy. The result reaches the card through the host arena, 5 KB per token at decode. Hence:
*
* The table is a weight operand: the planner places an op on the host only when all its weight
* operands are Site::Host, judged from the table's placement.
*
* `ids` and `e` are host-domain buffers, written and read by the host gather; a device gather
* accesses them over the link. Everything downstream of `e` is device work.
*
* It is 128 checkpoint tensors, mapped in pieces by map_concat_shards(); the order is the
* concatenation, and a permuted table silently looks up wrong rows.
*
* ============================== THE TWO ROLLING WINDOWS ==============================
*
* The n-gram hash needs the two committed ids before the step, and the convolution nine timesteps
* of its input. Both are RAD_KV_CONV windows read at `num_accepted - 1`. The token window's cold
* value is EOS, the convolution's zero (zero is a real token id); `has_init` marks cold slots.
*
* What a step leaves in the window depends on the step, signalled by whether `num_accepted` is
* passed (libref/ref_ple.cpp). A decode row may have drafts rejected, so its window uses
* gdn_conv_update's shifted layout, where the next read offset selects the history ending at the
* last kept token. A prefill chunk commits everything and leaves its tail at offset zero, read by
* the next step's `num_accepted` of 1. One layout cannot serve both, so step() issues both ops
* once per half of the batch, as rad_block_gdn_fp8.h does.
*/
#ifndef RAD_BLOCK_PLE_H
#define RAD_BLOCK_PLE_H
#include "rad_arch.h"
#include "rad_fp8.h"
namespace rad {
namespace arch {
struct PleLayer {
struct Config {
int64_t hc = 0; /* hyper-connection streams */
int64_t embed_dim = 0; /* ple_embed_dim: heads * head_dim */
int64_t heads = 0; /* (ngram - 1) * heads_per_ngram */
int64_t ngram = 0;
int64_t eos = -1;
int64_t rows = 0; /* the table's row count, padded */
int64_t shards = 0; /* how many checkpoint tensors it is stored as */
int64_t conv_width = 0;
int64_t dilation = 0; /* == ngram, in this model */
int64_t head_dim() const { return heads > 0 ? embed_dim / heads : 0; }
int64_t hist() const { return (conv_width - 1) * dilation; }
};
struct Wire {
rad_buf h = 0; /* [max_tok, hc * n_embd] the wide stream, read and added into */
};
struct Src {
/* The table's shards are named `<prefix><i><suffix>`; everything else is one tensor. */
const char* shard_prefix = nullptr;
const char* shard_suffix = nullptr;
const char* mult = nullptr;
const char* vocab_sizes = nullptr;
const char* offsets = nullptr;
const char* key_proj = nullptr;
const char* value_proj = nullptr;
const char* norm_key = nullptr;
const char* norm_query = nullptr;
const char* norm_conv = nullptr;
const char* conv1d = nullptr;
};
Geom g{};
Config c{};
Wire w{};
int layer_ = 0;
rad_weight w_table = 0, w_scale = 0, w_mult = 0, w_vsz = 0, w_off = 0;
/* The table is plain bf16 as shipped (no scale weight) rather than E4M3. */
bool table_bf16 = false;
rad_weight w_key = 0, w_value = 0, w_nk = 0, w_nq = 0, w_nc = 0, w_conv = 0;
rad_kvgroup kv_tok = 0, kv_conv = 0;
rad_buf b_ids = 0, b_emb = 0, b_embd = 0, b_k = 0, b_v = 0, b_gv = 0, b_gvn = 0, b_out = 0;
rad_op op_ids = 0, op_gather = 0, op_embd = 0, op_key = 0, op_value = 0;
rad_op op_gate = 0, op_conv = 0, op_add = 0;
int64_t hn() const { return c.hc * g.n_embd; }
/* The read-ahead queue's depth in steps: batch.cpp's k_ahead_chunks steps of the following
* prompt, whose rows the gather's reader thread prefetches. Clamped to what `b_ids` holds, so
* a larger offer only shortens the queue. */
static constexpr int64_t kAheadChunks = 2;
/* The prompt that follows the step, bounded by what `b_ids` holds after the step's rows. */
int64_t ahead_rows(const RadBatch* batch) const {
const int64_t a = batch->n_ahead;
if (a <= 0) return 0;
const int64_t cap = kAheadChunks * g.max_tok;
return a < cap ? a : cap;
}
int declare(RadBuilder* b, Names& nm, const Geom& geom, const Config& cfg, int layer,
const Wire& wire, const Src& src);
/* The hash, issued at the top of the step; the rest at the layer (see ids()). */
void ids(RadCtx* c_, const RadBatch* batch) const;
void step(RadCtx* c_, const RadBatch* batch) const;
};
inline int PleLayer::declare(RadBuilder* b, Names& nm, const Geom& geom, const Config& cfg,
int layer, const Wire& wire, const Src& src) {
g = geom; c = cfg; w = wire;
if (c.hc <= 1 || c.heads <= 0 || c.ngram < 2 || c.eos < 0 || c.rows <= 0 ||
c.shards <= 0 || c.conv_width <= 1 || c.dilation <= 0 || c.embed_dim <= 0) {
fprintf(stderr, "radiance: ple: geometry refused -- hc %lld heads %lld ngram %lld eos "
"%lld rows %lld shards %lld width %lld dilation %lld embed %lld\n",
(long long)c.hc, (long long)c.heads, (long long)c.ngram, (long long)c.eos,
(long long)c.rows, (long long)c.shards, (long long)c.conv_width,
(long long)c.dilation, (long long)c.embed_dim);
return RAD_E_INVAL;
}
/* `heads` is `ngram - 1` equal blocks, one per n-gram order, and `embed_dim` splits evenly
* across heads (the reference's constructor checks). */
if (c.heads % (c.ngram - 1) || c.embed_dim % c.heads) {
fprintf(stderr, "radiance: ple: %lld heads over %lld n-gram orders, %lld embed dims -- "
"neither divides\n", (long long)c.heads, (long long)(c.ngram - 1),
(long long)c.embed_dim);
return RAD_E_INVAL;
}
if (!w.h) return RAD_E_INVAL;
if (!src.shard_prefix || !src.shard_suffix || !src.mult || !src.vocab_sizes ||
!src.offsets || !src.key_proj || !src.value_proj || !src.norm_key || !src.norm_query ||
!src.norm_conv || !src.conv1d) return RAD_E_INVAL;
/* ============================== REPLICATED UNDER TP ==============================
*
* The whole layer is replicated and has no collective. The table is addressed by a token hash
* every rank computes identically, so a vocabulary shard is useless; and it is Tier::Mapped,
* so all ranks gather through one shared row cache over the same file.
*
* The rest is 63 MiB per rank (key_proj 50, value_proj 12.5, three norm gains, a conv weight).
* Column-sharding the projections would shard `gv` and `gvn` across the hyper-connection
* streams and need an all-gather per step to save 31 MiB per card. Every rank adds the same
* `out` into an identical copy of the stream, so the result stays bit-identical across ranks.
*
* The duplicated gather is `heads` rows per token: 16 at decode, 32768 at a 2048-token chunk,
* about 70 ms per rank through the mapping (7.6% of a prefill step). That cost is page-table
* population, not I/O: a prefill's distinct rows land about one per 4 KiB page (of 25), so the
* mapping pays a fault per row. At decode half of each step's rows are new and the page cache
* makes the waiting thread start each read. So the kernel keeps its own row cache and reads
* misses straight from the file (libavx/avx_ngram.cpp). Ranks share that cache: each claims
* and reads the misses it reaches first and waits only for rows another rank has claimed, so
* each miss is read once without a cross-rank barrier.
*/
const int64_t HN = hn();
const int64_t HD = c.head_dim();
/* ---- the table, its scale, and the three hash constants -------------------------------- */
/* E4M3 with one scale for all 320 million rows (Qwen3.8-Flash-Next-FP8's choice; E4M3 has its
* own exponent, a row's dynamic range is 2.3x, and the scale sits on a three-octave plateau).
* One stored weight, `fp8_e4m3*bf16[*x*]`: codes plus a one-element scale plane, made by the
* recipe (data/recipes/qwen4exp-w4.recipe) or read as that release ships it (its shards under
* one `weight_scale`); the gather takes them as two operands. The name map
* joins the shards along rows and is declared first, since the encoding is looked up through
* it. RAD_ACCESS_VOCAB: it is a vocabulary table, just not the token one. */
const char* tn = nm.f("blk.%d.ple_ngram.weight", layer);
RAD_ARCH_TRY(map_concat_shards(b, tn, src.shard_prefix, src.shard_suffix, (int)c.shards, 0));
/* Or the checkpoint's own bf16 table (102 GB) when the container holds it as shipped: the same
* op with no scale (an absent scale means 1), still a host gather over the mapping. */
RadEncoding te{};
table_bf16 = weight_enc(b, tn, &te) && rad_enc_is(&te, "plain") && te.n_planes == 1 &&
te.plane[0].dtype == RAD_BF16;
if (table_bf16) {
w_table = decl_w(b, tn, RAD_BF16, {c.rows, HD}, RAD_ACCESS_VOCAB, RAD_SHARD_NONE,
grp_model());
} else {
w_table = decl_view(b, tn, nullptr, "codes", RAD_F8E4M3, {c.rows, HD},
RAD_ACCESS_VOCAB, RAD_SHARD_NONE, grp_model());
w_scale = decl_view(b, nm.f("blk.%d.ple_ngram.scale", layer), tn, "scale", RAD_BF16, {1},
RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_model());
}
/* I64: the multipliers are ~2.4e13 (a token id times one fills 63 bits), so narrowing them
* would change every id. */
RAD_ARCH_TRY(map_copy(b, nm.f("blk.%d.ple_mult", layer), src.mult));
w_mult = decl_w(b, nm.f("blk.%d.ple_mult", layer), RAD_I64, {c.ngram},
RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_model());
RAD_ARCH_TRY(map_copy(b, nm.f("blk.%d.ple_vocab_sizes", layer), src.vocab_sizes));
w_vsz = decl_w(b, nm.f("blk.%d.ple_vocab_sizes", layer), RAD_I64, {c.heads},
RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_model());
RAD_ARCH_TRY(map_copy(b, nm.f("blk.%d.ple_offsets", layer), src.offsets));
w_off = decl_w(b, nm.f("blk.%d.ple_offsets", layer), RAD_I64, {c.heads},
RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_model());
/* ---- the projections and the three gains ------------------------------------------------ */
/* bf16: the FP8 checkpoint's `modules_to_not_convert` names both projections. */
RAD_ARCH_TRY(map_copy(b, nm.f("blk.%d.ple_key.weight", layer), src.key_proj));
w_key = decl_w(b, nm.f("blk.%d.ple_key.weight", layer), RAD_BF16, {HN, c.embed_dim},
RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_model());
RAD_ARCH_TRY(map_copy(b, nm.f("blk.%d.ple_value.weight", layer), src.value_proj));
w_value = decl_w(b, nm.f("blk.%d.ple_value.weight", layer), RAD_BF16,
{g.n_embd, c.embed_dim}, RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_model());
RAD_ARCH_TRY(map_copy(b, nm.f("blk.%d.ple_norm_key.weight", layer), src.norm_key));
w_nk = decl_w(b, nm.f("blk.%d.ple_norm_key.weight", layer), RAD_F32, {HN},
RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_model());
RAD_ARCH_TRY(map_copy(b, nm.f("blk.%d.ple_norm_query.weight", layer), src.norm_query));
w_nq = decl_w(b, nm.f("blk.%d.ple_norm_query.weight", layer), RAD_F32, {HN},
RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_model());
RAD_ARCH_TRY(map_copy(b, nm.f("blk.%d.ple_norm_conv.weight", layer), src.norm_conv));
w_nc = decl_w(b, nm.f("blk.%d.ple_norm_conv.weight", layer), RAD_F32, {HN},
RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_model());
/* The checkpoint stores it [hc*n, 1, width]; the op wants [hc*n, width], which is the same
* bytes with the depthwise group axis dropped. */
RAD_ARCH_TRY(map_copy(b, nm.f("blk.%d.ple_conv.weight", layer), src.conv1d));
w_conv = decl_w(b, nm.f("blk.%d.ple_conv.weight", layer), RAD_BF16, {HN, c.conv_width},
RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_model());
if (!w_table || (!table_bf16 && !w_scale) || !w_mult || !w_vsz || !w_off || !w_key || !w_value ||
!w_nk || !w_nq || !w_nc || !w_conv) return RAD_E_INVAL;
/* ---- the two rolling windows ------------------------------------------------------------ */
/* RAD_KV_CONV is a per-sequence rolling window of width-1 + num_spec entries. The token
* window's `conv_width` is ngram, so its window is ngram - 1 + n_spec.
*
* The channel count is n_head_kv * head_dim, not `state_dim` (RAD_KV_LINEAR's per-head
* geometry, ignored by the CONV kind). Set via state_dim, both groups would size to zero and
* the KV manager would refuse the model; rad-kbench would not catch it, since it synthesises
* the state operand from the op's shape function. */
{
RadKVGroupDecl td{};
td.kind = RAD_KV_CONV;
td.dtype = RAD_I32;
td.conv_width = c.ngram;
td.n_head_kv = 1;
td.head_dim = 1; /* one token id a slot */
kv_tok = rad_decl_kv_group(b, nm.f("kv_ple_tok"), &td);
RadKVGroupDecl cd{};
cd.kind = RAD_KV_CONV;
cd.dtype = g.act_dtype;
/* The conv's history is (width-1)*dilation deep, so the group's width (window =
* conv_width - 1 + n_spec) is that plus one; the raw conv_width would size an undilated
* window and the deepest tap would read unwritten data. */
cd.conv_width = c.hist() + 1;
cd.n_head_kv = 1;
cd.head_dim = HN; /* [n_states, hc*n_embd, hist + n_spec] */
kv_conv = rad_decl_kv_group(b, nm.f("kv_ple_conv"), &cd);
}
if (!kv_tok || !kv_conv) return RAD_E_INVAL;
RAD_ARCH_TRY(rad_bind_layer_kv(b, layer, kv_tok));
RAD_ARCH_TRY(rad_bind_layer_kv(b, layer, kv_conv));
/* ---- buffers ---------------------------------------------------------------------------- */
/* The two buffers the gather touches are host domain. `ids` is written by ngram_ids and read by
* the gather; `e` is written by the gather and copied onto the card (`ple_emb_dev`). The host
* arena is mapped, so the device reads it over the link (core/runtime/ctx.cpp), 5 KB per token
* at decode. A device gather (--ngram-placement ram or vram, with libr4d) reads `ids` and
* writes `e` over the link too; the domain is fixed here, before placement decides who gathers.
*
* `ids` is PERSIST: it is written at the top of the step and read at this layer, but liveness
* follows declared order, where the two ops are adjacent, so a transient could be overwritten
* in between. */
{
/* The step's rows, then kAheadChunks further steps of the following prompt
* (RadBatch::n_ahead) for the reader to prefetch. */
RadBufDecl d{};
d.dtype = RAD_I32; d.rank = 2;
d.shape[0] = (1 + kAheadChunks) * g.max_tok; d.shape[1] = c.heads;
d.kind = RAD_BUF_PERSIST; d.domain = RAD_DOMAIN_HOST;
b_ids = rad_decl_buffer(b, nm.f("ple_ids"), &d);
/* [max_tok, embed_dim], not [max_tok * heads, head_dim]: one shape must serve the gather
* (`heads` rows per token) and the projections (one `embed_dim` row per token). The gather
* derives its row count as numel / width (r4d_rows_pitch), so [T, 2560] read at 160 is
* T*16 rows. The GEMM takes M and K from the operand's shape (gemm_ops), so the tall shape
* would give it M = T*16, K = 160 and the step would be refused. */
RadBufDecl e{};
e.dtype = g.act_dtype; e.rank = 2;
e.shape[0] = g.max_tok; e.shape[1] = c.embed_dim;
e.kind = RAD_BUF_TRANSIENT; e.domain = RAD_DOMAIN_HOST;
b_emb = rad_decl_buffer(b, nm.f("ple_emb"), &e);
}
/* The device copy the projections read. A tiled GEMM reads each A row band once per column
* block (80 times for the key projection), so reading `e` from the host arena repeats link
* traffic: at a 2048-token chunk the projections take about 3.0 and 0.9 ms from the host arena
* versus 1.4 and 0.3 ms from VRAM. The copy is 10.5 MB per chunk, 5 KB per token at decode. */
b_embd = decl_b(b, nm.f("ple_emb_dev"), g.act_dtype, {g.max_tok, c.embed_dim});
b_k = decl_b(b, nm.f("ple_k"), g.act_dtype, {g.max_tok, HN});
b_v = decl_b(b, nm.f("ple_v"), g.act_dtype, {g.max_tok, g.n_embd});
b_gv = decl_b(b, nm.f("ple_gv"), g.act_dtype, {g.max_tok, HN});
b_gvn = decl_b(b, nm.f("ple_gvn"), g.act_dtype, {g.max_tok, HN});
b_out = decl_b(b, nm.f("ple_out"), g.act_dtype, {g.max_tok, HN});
if (!b_ids || !b_emb || !b_embd || !b_k || !b_v || !b_gv || !b_gvn || !b_out)
return RAD_E_INVAL;
/* State slots come from RadKVGroupBatch::state_index, filled per group by the step batch, as in
* rad_block_gdn_fp8.h; the core's `conv_state_index` is not used.
*
* ---- the ops, in the order step() issues them ------------------------------------------- */
layer_ = layer;
op_ids = rw(b, RAD_OP(b, "ngram_ids",
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("heads", c.heads),
RAD_INT("ngram", c.ngram), RAD_INT("eos", c.eos)),
RAD_WEIGHTS(w_mult, w_vsz, w_off)),
{}, {b_ids});
/* M is T * heads: the gather fetches one table row per head per token, and the band resolves
* on that count. */
{
const rad_weight wt[2] = { w_table, w_scale }; /* a bf16 table has no scale plane */
op_gather = rw(b, RAD_OP(b, "embed_lookup_q",
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok * c.heads), RAD_INT("n_embd", HD),
RAD_INT("n_vocab", c.rows), RAD_STR("dtype", g.dtype)),
wt, table_bf16 ? 1 : 2),
{b_ids}, {b_emb});
}
op_embd = rw(b, RAD_OP(b, "cast",
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", c.embed_dim),
RAD_STR("from", g.dtype), RAD_STR("to", g.dtype)),
RAD_NOWEIGHTS),
{b_emb}, {b_embd});
op_key = rw(b, RAD_OP(b, "gemm_nt",
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("N", HN),
RAD_INT("K", c.embed_dim), RAD_STR("dtype", g.dtype)),
RAD_WEIGHTS(w_key)),
{b_embd}, {b_k});
op_value = rw(b, RAD_OP(b, "gemm_nt",
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("N", g.n_embd),
RAD_INT("K", c.embed_dim), RAD_STR("dtype", g.dtype)),
RAD_WEIGHTS(w_value)),
{b_embd}, {b_v});
/* `q` is the incoming stream, un-normed; the op applies all three norms because the third is
* over bytes it writes. */
op_gate = rw(b, RAD_OP(b, "ple_gate",
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", g.n_embd),
RAD_INT("hc", c.hc), RAD_F64("eps", g.eps),
RAD_STR("dtype", g.dtype), RAD_F64("wadd", g.wadd)),
RAD_WEIGHTS(w_nk, w_nq, w_nc)),
{b_k, w.h, b_v}, {b_gv, b_gvn});
op_conv = rw(b, RAD_OP(b, "ple_conv",
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", HN),
RAD_INT("width", c.conv_width), RAD_INT("dilation", c.dilation),
RAD_STR("dtype", g.dtype)),
RAD_WEIGHTS(w_conv)),
{b_gvn, b_gv}, {b_out});
/* The add into the stream: `hidden_states + ple(...)`, where ple_conv's residual operand
* already produced `gv + silu(conv)`. */
op_add = rw(b, RAD_OP(b, "add",
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", HN),
RAD_STR("dtype", g.dtype)),
RAD_NOWEIGHTS),
{w.h, b_out}, {w.h});
if (!op_ids || !op_gather || !op_embd || !op_key || !op_value || !op_gate || !op_conv ||
!op_add)
return RAD_E_INVAL;
return RAD_OK;
}
/* Slots come from the group batch, and `has_init` is `ctx_lens` (nonzero means a history), as in
* rad_block_gdn_fp8.h.
*
* The two windowed ops are issued once per half of the step (batch_split: decode sequences
* [0, D) with tokens [0, DT) first, then prefill). The decode half passes `num_accepted`, so the
* op leaves its window readable after a partial rejection; the prefill half passes none and leaves
* each chunk's tail at offset zero. The prefill half gets per-sequence arrays at `+ D` with token
* operands at their base (kernels use the absolute offsets in `cu`); the decode half narrows to DT
* rows.
*
* ids() runs first in the step, before every layer: it reads only the token ids and its own
* window. The host gather that consumes it then waits for one small kernel, and the layers issued
* in between keep the device busy during the gather. */
inline void PleLayer::ids(RadCtx* c_, const RadBatch* batch) const {
const int64_t T = batch->n_tok;
const int64_t S = batch->n_seq;
const RadKVGroupBatch* tk = kv_batch(batch, kv_tok);
const int32_t* tk_idx = tk ? tk->state_index : nullptr;
int64_t D = 0, DT = 0;
batch_split(batch, &D, &DT);
const int64_t P = S - D;
if (D > 0)
RAD_ISSUE_N(c_, op_ids, DT,
praw(batch->token_ids, RAD_I32, DT), kv_cache(kv_tok, layer_),
praw(batch->cu_seqlens, RAD_I32, D + 1),
RAD_W(w_mult), RAD_W(w_vsz), RAD_W(w_off),
praw(tk_idx, RAD_I32, D),
praw(batch->ctx_lens, RAD_I32, D),
praw(batch->num_accepted, RAD_I32, D),
brows(b_ids, DT), RAD_NONE);
/* The prompt following the last prefill chunk is hashed in the same launch into the rows
* after the step's (`tok` reaches A tokens past the step); the window left behind is the
* step's alone. */
const int64_t A = P > 0 ? ahead_rows(batch) : 0;
if (P > 0)
RAD_ISSUE_N(c_, op_ids, T,
praw(batch->token_ids, RAD_I32, T + A), kv_cache(kv_tok, layer_),
praw(batch->cu_seqlens + D, RAD_I32, P + 1),
RAD_W(w_mult), RAD_W(w_vsz), RAD_W(w_off),
praw(tk_idx ? tk_idx + D : nullptr, RAD_I32, P),
praw(batch->ctx_lens + D, RAD_I32, P),
RAD_NONE,
brows(b_ids, T),
A > 0 ? brow_slice(b_ids, T, A, c.heads) : RAD_NONE);
}
inline void PleLayer::step(RadCtx* c_, const RadBatch* batch) const {
const int64_t T = batch->n_tok;
const int64_t S = batch->n_seq;
const RadKVGroupBatch* cv = kv_batch(batch, kv_conv);
const int32_t* cv_idx = cv ? cv->state_index : nullptr;
int64_t D = 0, DT = 0;
batch_split(batch, &D, &DT);
const int64_t P = S - D;
/* Operands are T rows (the gather derives its per-head rows from numel); the op's M band is
* T * heads. The trailing operand is the following prompt's ids, which the gather's reader
* prefetches for the next step. */
const int64_t A = P > 0 ? ahead_rows(batch) : 0;
RAD_ISSUE_N(c_, op_gather, T * c.heads,
brows(b_ids, T), RAD_W(w_table), table_bf16 ? RAD_NONE : RAD_W(w_scale),
brows(b_emb, T),
A > 0 ? brow_slice(b_ids, T, A, c.heads) : RAD_NONE);
RAD_ISSUE_N(c_, op_embd, T, brows(b_emb, T), brows(b_embd, T));
RAD_ISSUE_N(c_, op_key, T, brows(b_embd, T), RAD_W(w_key), brows(b_k, T));
RAD_ISSUE_N(c_, op_value, T, brows(b_embd, T), RAD_W(w_value), brows(b_v, T));
RAD_ISSUE_N(c_, op_gate, T,
brows(b_k, T), brows(w.h, T), brows(b_v, T),
RAD_W(w_nk), RAD_W(w_nq), RAD_W(w_nc),
brows(b_gv, T), brows(b_gvn, T));
if (D > 0)
RAD_ISSUE_N(c_, op_conv, DT,
brows(b_gvn, DT), RAD_W(w_conv), kv_cache(kv_conv, layer_),
praw(batch->cu_seqlens, RAD_I32, D + 1), brows(b_gv, DT),
praw(cv_idx, RAD_I32, D),
praw(batch->ctx_lens, RAD_I32, D),
praw(batch->num_accepted, RAD_I32, D),
brows(b_out, DT));
if (P > 0)
RAD_ISSUE_N(c_, op_conv, T,
brows(b_gvn, T), RAD_W(w_conv), kv_cache(kv_conv, layer_),
praw(batch->cu_seqlens + D, RAD_I32, P + 1), brows(b_gv, T),
praw(cv_idx ? cv_idx + D : nullptr, RAD_I32, P),
praw(batch->ctx_lens + D, RAD_I32, P),
RAD_NONE,
brows(b_out, T));
/* M is T rows of `n`, not the element count, matching the declared band 1..max_tok. */
RAD_ISSUE_N(c_, op_add, T, brows(w.h, T), brows(b_out, T), brows(w.h, T));
}
} /* namespace arch */
} /* namespace rad */
#endif /* RAD_BLOCK_PLE_H */