New models. Thinking Machines' Inkling 2 Mini by gostar (#584-#586): its text backbone and loop, the release's NVFP4 and MXFP8 checkpoint read by the converter, an int8 trunk, the vision tower, tensor parallelism, tool calls (with Warzuponus) and an image processor that matches transformers bit for bit (millaguie). Inkling's routed experts are served in IQR-V4's four-bit trellis (moe_gemm_iqr), fitted by GPTQ against Hessians the plugin now records (`--calib-dir`), its token embedding in int8 (read in place by embed_lookup), and incoai's DFlash2 drafter in four bits in place of the release's DSpark. That container, 14.4 GiB with the vision tower, decodes 190 / 427 / 658 tokens a second at 1 / 4 / 8 streams at tp 2 on two R9700 with `draft-window: adaptive` (code 240 / 550 / 790, step-by-step arithmetic 358 / 811 / 1116): 19-43% ahead of the same target with the release's DSpark drafter and 45-83% ahead of the all-int8 container (26.1 GiB, 124 / 263 / 386), at a mean KL of 0.0066 against the release's weights (int8: 0.0042). DeepSeek-V4.1-Flash on 2.x by baKon (#583), with its vision tower, its IQ1_S and IQ2_XXS containers and linears and experts served in llama.cpp's own blocks; parts ported from mikoskinen/r9700-exact-serving by Dad_Vibes' fork. GLM-5.3-Flash on 2.x with its IQR / IQ8R container, an int8 latent cache and four-card serving by AnimusNull (#566). Gemma 4 dense on EXL3-V3 by keptn42 (#537), on the EXL3 dense rows and qwen35_exl3 tensor parallelism by Hero422. NVIDIA's 2-bit Nemotron-3.5-Bolt-74B-A9B on one card by keptn42 (#477). Wan 2.2 TI2V-5B text-to-video and first-frame video by rkcth (#535). LFM3-1.7B on RDNA3 iGPUs by DokterSmol (#578). Qwen3.8-27B in bf16 with the DFlash2 drafter, and bf16x12, bf16 weights packed losslessly in 12.25 bits, by eddie168 (#577). Int8 experts for drama-3 by morevgalex (#598). Speculative decoding. Draft trees for Qwen3.8-27B's DFlash2 drafts by Kezi (#450, opt-in): one card, +17% at one stream and +14% at two. --draft-max-lanes and --draft-min-accept, two gates on a block drafter, and --draft-window-ckpt, its window KV kept in the checkpoint slots, by rkcth (#600-#602). `draft-window: adaptive` verifies each sequence's drafts up to the first one unlikely to be kept. A verify's rows share each K and V tile in Inkling's attention by gostar (#608): 93 to 118 tokens a second at a 70k-token prompt on one card. Generation. Krea 2 instruction edits with one or two references and registered outfill, LoRA on the image generators with bias deltas and row-mixer adapters applied per request, and MiniMax-H3's model-declared sparse attention profiles, by rkcth (#534, #542, #543, #605-#609). MiniMax-H3's audio reference guide pinned from a frame by 1beb (#595). LTX-2's chunk ceiling raised to 40 by Itoash (#570). Fleets and memory. memory.queue-reserve, the hardware queues a card keeps ready, by kevinjd (#589). memory.pinned-numa-node by Foundry (#580). memory.card-headroom-mib on single-model tensor-parallel budgets by zihaomu (#624). Parked weights rebuilt on the CPU, memory ownership kept across a failed handoff and quiescent callbacks serialised, by rkcth (#557-#561). A warm load may unload a parked model (#592); the first card listed chooses the architecture (#458). Kernels. libr3d's packed dense IQR GEMM and routed IQR expert rows on gfx1100, and libhalo's GEMV decode for Qwen3.8-Flash-Next's MoE down, by zihaomu (#562, #620, #622). GGML IQ2_XXS and IQ3_XXS packed on the card by baKon. Dashboard. KV cache in tokens (in use, capacity now and with all flexible space), live figures through long prefill steps, step / decode / prefill / acceptance lines without holes, and expert hit rates only on tiered models. Seven tile and history defects fixed by Noblistic (#597), vision encoder and per-card hardware figures by davetha (#574), per-LoRA attribution by dowjames (#552). Fixed. return_tokens / return_token_ids on the OpenAI endpoints (#571). reasoning_effort "none" and "minimal" reach a template that takes them as levels, by millaguie (#608). Misaligned loads in the windowed paged attention over an int8 cache (#540). A retired 27B head refused by name (#541). rad-convert's GPU trellis jobs kept fed, and --reuse-only, by gostar (#575). jinja's tojson takes sort_keys by Warzuponus (#582). hfget measures file sizes with stat, by kepler_62f (#623). The tested C++ compiler is stated in the build guide by lowell80 (#551). The codebase was cleaned up: shared helpers instead of repeated blocks, and the int8 linear, activation and GEMM code no longer named after the retired fp8 formats. Inkling 2 Mini is served in one form: IQR-V4 experts with the DFlash2 drafter (StillDeadcode/inkling-2-mini-iq4r-v4 on Hugging Face, or data/recipes/inkling2-mini-iq4r-v4.recipe). A container with the release's DSpark drafter, or with NVFP4, int8 or 4-bit experts, is refused by name and needs converting again. Plugin ABI 44: out-of-tree plugins need to be rebuilt. Thanks to @gostar, @baKon, @Dad_Vibes, @AnimusNull, @keptn42, @Hero422, @rkcth, @DokterSmol, @eddie168, @morevgalex, @Kezi, @1beb, @Itoash, @kevinjd, @Foundry, @zihaomu, @Noblistic, @davetha, @dowjames, @Warzuponus, @millaguie, @kepler_62f, @lowell80 and @Code000.
4910 lines
252 KiB
C++
4910 lines
252 KiB
C++
#include "rad_unipc_math.h"
|
|
#include "rad_wan_dit.h"
|
|
#include "rad_wan_vae.h"
|
|
/* ref_shapes.cpp -- how big is operand i of this op at this geometry, and what may legally be in
|
|
* it. libref's implementation of `RadKernelInfo::opd_shape` (rad_abi.h), for every op this
|
|
* plugin registers.
|
|
*
|
|
* ============================== Why this lives here ==============================
|
|
*
|
|
* A tool that calls an op cold (a correctness check, a benchmark) must size its buffers, and a
|
|
* schema names operands and roles but not extents. A hand-written table in `tools/` (opshapes.h)
|
|
* could only describe ops its author knew and could not be extended without patching the engine.
|
|
* The component that reads an operand knows its extent; this file answers for the whole
|
|
* conventional vocabulary, so any op in docs/OPS.md is describable even when the kernel under test
|
|
* is not.
|
|
*
|
|
* ============================== One function per row, named after the launch ==============
|
|
*
|
|
* `RadShapeFn` gets only the resolved parameters and an operand index, not the op name. So every
|
|
* entry point `ref_X` has a `ref_X_shape`, which ref_registry.cpp's ROW() macro pastes as
|
|
* `fn ## _shape`; a new op without a description fails to link.
|
|
*
|
|
* ============================== The count is checked ==============================
|
|
*
|
|
* A description must emit exactly as many operands as the schema declares; that catches a
|
|
* description that drifted from its schema (which would otherwise leave the op silently skipped).
|
|
* The hook is called per operand, so each function fills a local array with the whole list and
|
|
* hands out the requested entry, and `Opd::finish` compares the length with
|
|
* `RadOpSchema::n_operands` from this plugin's schema table. A mismatch returns RAD_E_SHAPE, a bug
|
|
* here or in the schema; RAD_E_UNSUPPORTED means the geometry lacks what the op needs, which a
|
|
* caller skips and counts.
|
|
*
|
|
* ============================== What is in an operand ==============================
|
|
*
|
|
* The fill domain matters as much as the extent (see rad_abi.h):
|
|
*
|
|
* * A gated-delta-net `g` is an intra-chunk cumulative sum of log-decays: non-increasing and
|
|
* never positive. The scan's e^{g_i - g_j}, j <= i, is <= 1 only because g is monotone; normal
|
|
* draws overflow the oracle to 2e38.
|
|
* * `beta` is a sigmoid output in (0, 1). `A` is a chunk's triangular inverse, zero above the
|
|
* diagonal within a chunk; a dense random A exposes a convention (whether the upper part is
|
|
* read) that never matters on real data.
|
|
* * An index operand's range is part of its meaning: a KV slot indexes n_blocks * block_size
|
|
* positions and a block-table entry n_blocks, neither being any operand's dim 0. Drawing from
|
|
* the wrong range tests out-of-range handling instead.
|
|
* * A scatter destination must be unique: two tokens on one slot are a race that ref (OpenMP over
|
|
* tokens) and a device kernel resolve differently. Read-side indices (gather rows, block-table
|
|
* entries) may repeat.
|
|
* * `seqused` is a constant, not a draw: a context shorter than the query makes causal attention
|
|
* read a nonexistent prefix.
|
|
* * A sampler's `params` rows are requests: a stage gated by a flag, a history window or a mask
|
|
* row does work only where those integer fields are what a request sets. Its token history
|
|
* must repeat itself, or DRY never finds a repeat to penalise.
|
|
* * An optional operand deliberately not passed still needs an entry for the count check; a null
|
|
* RadTensor.data marks absence, as in the ABI.
|
|
*
|
|
* ============================== What this file may use ==============================
|
|
*
|
|
* A kernel plugin links only rad_abi (libref/CMakeLists.txt), so core symbols outside headers
|
|
* (e.g. RAD_DEBUG) are unreachable. Header-only ABI helpers are available: `rad_dtype_parse` and
|
|
* `rad_dtype_bytes` (rad_types.h) and `rad_param_*` (rad_plugin.h). Use the ABI's version first;
|
|
* `gi` below is the one local variant, and says why.
|
|
*
|
|
* `rad_kernel_schema_count` / `rad_kernel_schema_at` are this plugin's own exports
|
|
* (ref_registry.cpp), so the count check adds no coupling.
|
|
*/
|
|
#include "ref_ops.h"
|
|
#include "rad_umt5_attention.h"
|
|
|
|
/* rad_param_find / rad_param_getdim / rad_param_gets / rad_dtype_parse. */
|
|
#include "rad_builder.h" /* rad_kv_row_elems */
|
|
#include "rad_kv_fp8.h"
|
|
#include "rad_plugin.h"
|
|
#include "rad_runtime.h" /* RAD_VGEOM_N */
|
|
#include "rad_sample.h"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <cstring>
|
|
#include <cstdlib>
|
|
#include <initializer_list>
|
|
|
|
namespace {
|
|
|
|
/* ================================================================== parameters
|
|
* Not ref_common.h's p_int(), which takes a RadArgs; this hook gets a bare RadParam list before
|
|
* anything is allocated. rad_param_getdim gives the same int-or-range reading (an uncollapsed range
|
|
* describes the band's upper bound, the extent the arena is sized for).
|
|
*
|
|
* The one local leniency: a string spelling of an integer is accepted, so the oracle checks rather
|
|
* than skips such a geometry. libr4d's shape hooks deliberately refuse it, since its launches read
|
|
* RAD_P_INT only; the difference is intended. */
|
|
static int64_t gi(const RadParam* p, int n_p, const char* k, int64_t dflt) {
|
|
const RadParam* q = rad_param_find(p, n_p, k);
|
|
if (q && q->kind == RAD_P_STR && q->sval) return (int64_t)std::strtoll(q->sval, nullptr, 10);
|
|
return (int64_t)rad_param_getdim(p, n_p, k, (long long)dflt);
|
|
}
|
|
|
|
static const char* gs(const RadParam* p, int n_p, const char* k) {
|
|
return rad_param_gets(p, n_p, k, nullptr);
|
|
}
|
|
|
|
/* ================================================================== dtype names
|
|
* The ABI's parser, so a dtype added to the ABI is understood here too. */
|
|
static uint32_t dt_parse(const char* n) { return rad_dtype_parse(n); }
|
|
|
|
/* The activation dtype: `dtype`, else attention's `q_dtype` / `kv_dtype`, else bf16. Some ops
|
|
* (e.g. `rope`) carry no dtype parameter, and an f32 fallback would make a bf16-only kernel fail on
|
|
* dtype. A quantisation-scheme string ("w4a8", "fp8a8") also lands on bf16. */
|
|
static 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");
|
|
if (!s) s = gs(p, n_p, "kv_dtype");
|
|
const uint32_t dt = dt_parse(s);
|
|
return dt == (uint32_t)RAD_DT_INVALID ? (uint32_t)RAD_BF16 : dt;
|
|
}
|
|
|
|
/* ================================================================== the operand list
|
|
* Each function builds the whole list and hands out the requested entry, so the count can be
|
|
* checked against the schema (see the header). */
|
|
enum { REF_MAX_OPD = 24 }; /* the widest schema here is gdn_conv_prep's sixteen */
|
|
|
|
struct Opd {
|
|
RadOpdDesc v[REF_MAX_OPD];
|
|
int n = 0;
|
|
bool over = false;
|
|
RadOpdDesc sink{}; /* somewhere for an overflowing push to land; `over` is the answer */
|
|
|
|
RadOpdDesc* push() {
|
|
if (n >= REF_MAX_OPD) { over = true; return &sink; }
|
|
RadOpdDesc& d = v[n++];
|
|
std::memset(&d, 0, sizeof d);
|
|
d.fill = (uint32_t)RAD_FILL_NORMAL;
|
|
/* -1 is the ABI's "draw"; set explicitly because a zeroed field means "the constant 0". */
|
|
d.idx_const = -1;
|
|
return &d;
|
|
}
|
|
|
|
static void shape(RadOpdDesc* d, uint32_t dt, std::initializer_list<int64_t> s) {
|
|
d->dtype = dt;
|
|
d->rank = 0;
|
|
for (int64_t e : s) {
|
|
if (d->rank >= (uint32_t)RAD_MAX_RANK) break;
|
|
d->shape[d->rank++] = e;
|
|
}
|
|
}
|
|
|
|
/* An ordinary float operand: standard normals. */
|
|
void t(uint32_t dt, std::initializer_list<int64_t> s) { shape(push(), dt, s); }
|
|
|
|
/* A float operand whose domain is narrower than a normal draw (a sigmoid gate, a monotone
|
|
* log-decay cumsum, a chunk-triangular factor). `chunk` is the reset width. */
|
|
void t_fill(uint32_t dt, std::initializer_list<int64_t> s, uint32_t fill, int64_t chunk) {
|
|
RadOpdDesc* d = push();
|
|
shape(d, dt, s);
|
|
d->fill = fill;
|
|
d->fill_chunk = chunk;
|
|
}
|
|
|
|
/* An index operand of dtype `dt`. `max` is the exclusive upper bound of the draw; 0 means the
|
|
* previous operand's leading extent. `konst` >= 0 makes every element that value. A dtype
|
|
* other than i32 is for weights whose values are sizes or offsets (those are i64 in every
|
|
* container and plugins refuse any other dtype) and for a byte plane of marks. */
|
|
void t_idx_dt(uint32_t dt, std::initializer_list<int64_t> s, int64_t max, int64_t konst,
|
|
uint32_t flags) {
|
|
RadOpdDesc* d = push();
|
|
shape(d, dt, s);
|
|
d->fill = (uint32_t)RAD_FILL_INDEX;
|
|
d->idx_max = max;
|
|
d->idx_const = konst;
|
|
d->flags = flags;
|
|
}
|
|
void t_idx(std::initializer_list<int64_t> s, int64_t max, int64_t konst, uint32_t flags) {
|
|
t_idx_dt((uint32_t)RAD_I32, s, max, konst, flags);
|
|
}
|
|
|
|
/* An optional operand this description deliberately does not pass. */
|
|
void skip() {
|
|
RadOpdDesc* d = push();
|
|
d->flags = (uint32_t)RAD_OPD_F_ABSENT;
|
|
}
|
|
|
|
int finish(const char* op, int operand, RadOpdDesc* out) const;
|
|
};
|
|
|
|
/* The operand count this plugin's schema declares for `op`, or -1 if the op is not ours. */
|
|
static int schema_operands(const char* op) {
|
|
const int n = rad_kernel_schema_count();
|
|
for (int i = 0; i < n; ++i) {
|
|
const RadOpSchema* s = rad_kernel_schema_at(i);
|
|
if (s && s->op && std::strcmp(s->op, op) == 0) return s->n_operands;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int Opd::finish(const char* op, int operand, RadOpdDesc* out) const {
|
|
if (over) return RAD_E_SHAPE;
|
|
const int want = schema_operands(op);
|
|
/* Description and schema disagree: a bug in one of them. */
|
|
if (want != n) return RAD_E_SHAPE;
|
|
if (operand < 0 || operand >= n || !out) return RAD_E_SHAPE;
|
|
/* A non-positive extent means the geometry lacks a parameter this description needs: a
|
|
* decline (the caller skips and counts), not a bug. */
|
|
for (int i = 0; i < n; ++i) {
|
|
if (v[i].flags & RAD_OPD_F_ABSENT) continue;
|
|
if (v[i].rank == 0) return RAD_E_UNSUPPORTED;
|
|
for (uint32_t d = 0; d < v[i].rank; ++d)
|
|
if (v[i].shape[d] <= 0) return RAD_E_UNSUPPORTED;
|
|
}
|
|
*out = v[operand];
|
|
return RAD_OK;
|
|
}
|
|
|
|
/* ================================================================== shared readings */
|
|
|
|
/* The scale-stream group count shared by `quant_act_i8g`, `quant_act_fp8` and `dequant`, matching
|
|
* their launches (ref_quant.cpp): group <= 0 or wider than the row is one scale per row, otherwise
|
|
* ceil(n / group). Not the `had_*` reading, where `group` is the rotation width and the scale is
|
|
* per row. */
|
|
static int64_t scale_groups(int64_t n, int64_t group) {
|
|
int64_t g = group;
|
|
if (g <= 0 || g > n) g = n;
|
|
return g > 0 ? (n + g - 1) / g : 0;
|
|
}
|
|
|
|
/* The gated-delta-net head counts. No gdn op has them as parameters (kernels read them off the
|
|
* operands), so the family declines unless the caller passes `n_head_k` / `n_head_v`, taken from
|
|
* the container's `linear_num_key_heads` / `linear_num_value_heads`. */
|
|
static bool gdn_heads(const RadParam* p, int n_p, int64_t* Hk, int64_t* Hv) {
|
|
*Hk = gi(p, n_p, "n_head_k", 0);
|
|
*Hv = gi(p, n_p, "n_head_v", 0);
|
|
return *Hk > 0 && *Hv > 0;
|
|
}
|
|
|
|
/* Sequences in the described batch: 1 by default; a caller may pass `n_seq` for a ragged batch,
|
|
* which the gdn and attention descriptions follow. */
|
|
static int64_t n_seq_of(const RadParam* p, int n_p) {
|
|
const int64_t s = gi(p, n_p, "n_seq", 1);
|
|
return s > 0 ? s : 1;
|
|
}
|
|
|
|
/* The sampler's per-row parameter plane, one RadSampleParams per sampled position
|
|
* (abi/rad_sample.h): a byte blob viewed through a 32-bit dtype; this plugin and libr4d accept u32,
|
|
* i32 or f32.
|
|
*
|
|
* Drawn with RAD_FILL_SAMPLE_PARAMS: each row is a request's settings, its flags agreeing with its
|
|
* fields, its history window inside the history plane and its `mask_row` a row of the bitmask
|
|
* plane, which the description passes as the fill's `fill_chunk` and `idx_max`. Row 0 switches
|
|
* every stage on, so a one-row case does work in each; every other row switches each stage on or
|
|
* leaves it at its identity at random, and a minority is greedy (rad_abi.h has the law). The
|
|
* history repeats itself (RAD_OPD_F_IDX_HISTORY), so the frequency penalty counts tokens more than
|
|
* once and DRY finds repeated suffixes to penalise.
|
|
*
|
|
* Described as f32: a tool that does not know the law draws normals, under which the float fields
|
|
* land in plausible ranges and the narrowing stages still work on some rows. A word-typed normal
|
|
* draw would make them denormals or NaN and `1 / temp` infinite.
|
|
*
|
|
* Word-typed operands here are i32, never u32: opshapes.cpp's rad_widen has no RAD_U32 case, so a
|
|
* u32 operand would compare as zeroes and pass vacuously. */
|
|
static int64_t sample_params_words() {
|
|
return (int64_t)(sizeof(RadSampleParams) / 4); /* 96 bytes, 24 words */
|
|
}
|
|
|
|
} /* namespace */
|
|
|
|
/* ================================================================== elementwise and norms */
|
|
|
|
int ref_rmsnorm_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);
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x */
|
|
o.t(D, { n }); /* w */
|
|
o.t(D, { M, n }); /* y */
|
|
return o.finish("rmsnorm", operand, out);
|
|
}
|
|
|
|
int ref_rmsnorm_p_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);
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x */
|
|
o.t(D, { M, n }); /* y */
|
|
return o.finish("rmsnorm_p", operand, out);
|
|
}
|
|
|
|
int ref_rmsnorm_add_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);
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x */
|
|
o.t(D, { M, n }); /* residual */
|
|
o.t(D, { n }); /* w */
|
|
o.t(D, { M, n }); /* y */
|
|
o.t(D, { M, n }); /* residual_out */
|
|
return o.finish("rmsnorm_add", operand, out);
|
|
}
|
|
|
|
/* The gated residual. `inject` and `group` default on (every trunk connection); 0 describes the
|
|
* final mixer or a bf16-consumer read (see the schema in ref_registry.cpp). */
|
|
int ref_hc_read_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);
|
|
const int64_t hc = gi(p, n_p, "hc", 0), lr = gi(p, n_p, "lowrank", 0);
|
|
const int64_t hn = hc * n;
|
|
const int64_t inject = gi(p, n_p, "inject", 1);
|
|
const int64_t group = gi(p, n_p, "group", 0);
|
|
const int64_t rotate = gi(p, n_p, "rotate", 0);
|
|
Opd o;
|
|
o.t(D, { M, hn }); /* h */
|
|
/* The gain is f32 in the container, not the activation dtype. */
|
|
o.t((uint32_t)RAD_F32, { hn }); /* w */
|
|
o.t(D, { lr, hn }); /* mix_down */
|
|
o.t(D, { hn, lr }); /* mix_up */
|
|
if (inject) o.t(D, { hc, hn }); else o.skip(); /* inject_w */
|
|
o.t(D, { M, n }); /* x */
|
|
if (inject) o.t(D, { M, hc }); else o.skip(); /* inj */
|
|
if (group > 0) {
|
|
o.t((uint32_t)RAD_F8E4M3, { M, n }); /* q */
|
|
o.t((uint32_t)RAD_F32, { M, scale_groups(n, group) }); /* scale */
|
|
} else {
|
|
o.skip();
|
|
o.skip();
|
|
}
|
|
if (group > 0 && rotate) {
|
|
o.t((uint32_t)RAD_F8E4M3, { M, n }); /* rq */
|
|
o.t((uint32_t)RAD_F32, { M, scale_groups(n, group) }); /* rscale */
|
|
} else {
|
|
o.skip();
|
|
o.skip();
|
|
}
|
|
return o.finish("hc_read", operand, out);
|
|
}
|
|
|
|
int ref_hc_enter_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);
|
|
const int64_t hc = gi(p, n_p, "hc", 0);
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x */
|
|
o.t(D, { M, hc * n }); /* h */
|
|
return o.finish("hc_enter", operand, out);
|
|
}
|
|
|
|
int ref_kda_update_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const char* form = gs(p, n_p, "state_form");
|
|
// Replay records and aliased engine index columns require a hand-built fixture.
|
|
if (form && !std::strcmp(form,"anchor")) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
const int64_t M = gi(p, n_p, "M", 0), H = gi(p, n_p, "heads", 0);
|
|
const int64_t K = gi(p, n_p, "head_k", 0), V = gi(p, n_p, "head_v", 0);
|
|
if (M < 1 || H < 1 || K < 1 || K > 128 || V < 1 || V > 128) return RAD_E_UNSUPPORTED;
|
|
const char* mode = gs(p, n_p, "mode");
|
|
const int64_t history = mode && !std::strcmp(mode, "verify") ? M : 1;
|
|
Opd o;
|
|
o.t(D, {M, H, K}); o.t(D, {M, H, K}); o.t(D, {M, H, V});
|
|
o.t(D, {M, H, K}); o.t(D, {M, H});
|
|
o.t((uint32_t)RAD_F32, {H}); o.t((uint32_t)RAD_F32, {H * K});
|
|
o.t((uint32_t)RAD_F32, {history + 1, H, V, K});
|
|
o.t_idx({1, history}, history + 1, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE);
|
|
o.t_idx({2}, M, -1, (uint32_t)RAD_OPD_F_IDX_CU);
|
|
o.t_idx({1}, 0, 1, 0); o.t_idx({1}, 0, 1, 0);
|
|
o.t(D, {M, H, V});
|
|
return o.finish("kda_update", operand, out);
|
|
}
|
|
|
|
int ref_ssd_update_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const char* form = gs(p, n_p, "state_form");
|
|
if (form && !std::strcmp(form, "anchor")) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
const int64_t M = gi(p, n_p, "M", 0), H = gi(p, n_p, "heads", 0);
|
|
const int64_t P = gi(p, n_p, "head_dim", 0), N = gi(p, n_p, "d_state", 0);
|
|
const int64_t G = gi(p, n_p, "groups", 0);
|
|
if (M < 1 || H < 1 || P < 1 || N < 1 || G < 1 || H % G) return RAD_E_UNSUPPORTED;
|
|
const char* mode = gs(p, n_p, "mode");
|
|
const int64_t history = mode && !std::strcmp(mode, "verify") ? M : 1;
|
|
Opd o;
|
|
o.t(D, {M, H, P}); o.t(D, {M, G, N}); o.t(D, {M, G, N}); o.t(D, {M, H});
|
|
o.t((uint32_t)RAD_F32, {H}); o.t((uint32_t)RAD_F32, {H}); o.t((uint32_t)RAD_F32, {H});
|
|
o.t((uint32_t)RAD_F32, {history + 1, H, P, N});
|
|
o.t_idx({1, history}, history + 1, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE);
|
|
o.t_idx({2}, M, -1, (uint32_t)RAD_OPD_F_IDX_CU);
|
|
o.t_idx({1}, 0, 1, 0); o.t_idx({1}, 0, 1, 0);
|
|
o.t(D, {M, H, P});
|
|
return o.finish("ssd_update", operand, out);
|
|
}
|
|
|
|
int ref_mhc_pre_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), hc = gi(p, n_p, "hc", 0);
|
|
if (M <= 0 || n <= 0 || hc < 2 || hc > 8) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(D, {M, hc * n});
|
|
o.t((uint32_t)RAD_F32, {hc * (hc + 2), hc * n});
|
|
o.t((uint32_t)RAD_F32, {3});
|
|
o.t((uint32_t)RAD_F32, {hc * (hc + 2)});
|
|
o.t(D, {M, n});
|
|
o.t((uint32_t)RAD_F32, {M, hc});
|
|
o.t((uint32_t)RAD_F32, {M, hc * hc});
|
|
return o.finish("mhc_pre", operand, out);
|
|
}
|
|
|
|
static int64_t mhc_part_stride(int64_t hc) { return (hc * (hc + 2) + 1 + 3) / 4 * 4; }
|
|
|
|
int ref_mhc_pre_partial_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), hc = gi(p, n_p, "hc", 0);
|
|
const int64_t chunk = gi(p, n_p, "chunk", 0), chunks = gi(p, n_p, "chunks", 0);
|
|
if (M <= 0 || n <= 0 || hc < 2 || hc > 8 || chunk <= 0 || (hc * n) % chunk || chunks <= 0)
|
|
return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(D, {M, hc * n});
|
|
o.t((uint32_t)RAD_F32, {hc * (hc + 2), hc * n});
|
|
o.t((uint32_t)RAD_F32, {M, chunks * mhc_part_stride(hc)});
|
|
return o.finish("mhc_pre_partial", operand, out);
|
|
}
|
|
|
|
int ref_mhc_pre_finish_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), hc = gi(p, n_p, "hc", 0);
|
|
const int64_t chunk = gi(p, n_p, "chunk", 0);
|
|
if (M <= 0 || n <= 0 || hc < 2 || hc > 8 || chunk <= 0 || (hc * n) % chunk)
|
|
return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t((uint32_t)RAD_F32, {M, hc * n / chunk * mhc_part_stride(hc)});
|
|
o.t(D, {M, hc * n});
|
|
o.t((uint32_t)RAD_F32, {3});
|
|
o.t((uint32_t)RAD_F32, {hc * (hc + 2)});
|
|
o.t(D, {M, n});
|
|
o.t((uint32_t)RAD_F32, {M, hc});
|
|
o.t((uint32_t)RAD_F32, {M, hc * hc});
|
|
return o.finish("mhc_pre_finish", operand, out);
|
|
}
|
|
|
|
int ref_mhc_post_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), hc = gi(p, n_p, "hc", 0);
|
|
if (M <= 0 || n <= 0 || hc < 2 || hc > 8) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(D, {M, n}); o.t(D, {M, hc * n});
|
|
o.t((uint32_t)RAD_F32, {M, hc}); o.t((uint32_t)RAD_F32, {M, hc * hc});
|
|
o.t(D, {M, hc * n});
|
|
return o.finish("mhc_post", operand, out);
|
|
}
|
|
|
|
int ref_mhc_contract_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), hc = gi(p, n_p, "hc", 0);
|
|
if (M <= 0 || n <= 0 || hc < 2 || hc > 8) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(D, {M, hc * n}); o.t(D, {M, n});
|
|
return o.finish("mhc_contract", operand, out);
|
|
}
|
|
|
|
int ref_mhc_mix_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), hc = gi(p, n_p, "hc", 0);
|
|
if (M <= 0 || n <= 0 || hc < 2 || hc > 8) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(D, {M, hc * n});
|
|
o.t((uint32_t)RAD_F32, {hc * (hc + 2), hc * n});
|
|
o.t((uint32_t)RAD_F32, {3});
|
|
o.t((uint32_t)RAD_F32, {hc * (hc + 2)});
|
|
o.t((uint32_t)RAD_F32, {M, hc});
|
|
o.t((uint32_t)RAD_F32, {M, hc});
|
|
o.t((uint32_t)RAD_F32, {M, hc * hc});
|
|
return o.finish("mhc_mix", operand, out);
|
|
}
|
|
|
|
int ref_mhc_collapse_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), hc = gi(p, n_p, "hc", 0);
|
|
if (M <= 0 || n <= 0 || hc < 2 || hc > 8) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(D, {M, hc * n}); o.t((uint32_t)RAD_F32, {M, hc}); o.t(D, {M, n});
|
|
return o.finish("mhc_collapse", operand, out);
|
|
}
|
|
|
|
int ref_hc_write_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);
|
|
const int64_t hc = gi(p, n_p, "hc", 0);
|
|
Opd o;
|
|
o.t(D, { M, n }); /* y */
|
|
o.t(D, { M, hc }); /* inj */
|
|
o.t(D, { M, hc * n }); /* h, in place */
|
|
return o.finish("hc_write", operand, out);
|
|
}
|
|
|
|
int ref_hc_project_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);
|
|
const int64_t hc = gi(p, n_p, "hc", 0);
|
|
Opd o;
|
|
o.t(D, { M, hc * n }); /* h, in place */
|
|
o.t((uint32_t) RAD_F32, { n }); /* v: one layer's unit direction */
|
|
o.t((uint32_t) RAD_I32, { M }); /* on: 1 = this row takes the projection */
|
|
return o.finish("hc_project", operand, out);
|
|
}
|
|
|
|
int ref_mtp_enter_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);
|
|
const int64_t hc = gi(p, n_p, "hc", 0);
|
|
Opd o;
|
|
o.t(D, { M, hc * n }); /* h, the wide stream */
|
|
o.t(D, { M, n }); /* e, the next token's embedding */
|
|
/* Both gains are f32 in the container, as hc_read's. */
|
|
o.t((uint32_t)RAD_F32, { hc * n }); /* w_h */
|
|
o.t((uint32_t)RAD_F32, { n }); /* w_e */
|
|
o.t(D, { n, n }); /* fc_h */
|
|
o.t(D, { n, n }); /* fc_e */
|
|
o.t(D, { M, hc * n }); /* x */
|
|
return o.finish("mtp_enter", operand, out);
|
|
}
|
|
|
|
int ref_grid_embed_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), side = gi(p, n_p, "side", 0);
|
|
if (M <= 0 || n <= 0 || side <= 0) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x, INOUT */
|
|
o.t(D, { side * side, n }); /* table */
|
|
/* row, column, height, width, drawn over the table's side so some rows exercise the clamped
|
|
* taps. */
|
|
o.t_idx({ 4, M }, side, -1, 0);
|
|
return o.finish("grid_embed", operand, out);
|
|
}
|
|
|
|
/* grid_embed's operands; the coordinates are drawn over the table's side, as there. */
|
|
int ref_grid_embed_aa_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), side = gi(p, n_p, "side", 0);
|
|
if (M <= 0 || n <= 0 || side <= 0) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x, INOUT */
|
|
o.t(D, { side * side, n }); /* table */
|
|
o.t_idx({ 4, M }, side, -1, 0);
|
|
return o.finish("grid_embed_aa", operand, out);
|
|
}
|
|
|
|
/* grid_embed_pad's: the same operands and drawing as grid_embed's. */
|
|
int ref_grid_embed_pad_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), side = gi(p, n_p, "side", 0);
|
|
if (M <= 0 || n <= 0 || side <= 0) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x, INOUT */
|
|
o.t(D, { side * side, n }); /* table */
|
|
o.t_idx({ 4, M }, side, -1, 0);
|
|
return o.finish("grid_embed_pad", operand, out);
|
|
}
|
|
|
|
/* posemb_resample: the source table [S_h * S_w, n] and the grid's table [T_h * T_w, n]. A grid or
|
|
* source the op cannot resample is refused here, as its launch refuses it. */
|
|
int ref_posemb_resample_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const uint32_t D = act_dt(p, n_p);
|
|
const int64_t Sh = gi(p, n_p, "S_h", 0), Sw = gi(p, n_p, "S_w", 0);
|
|
const int64_t Th = gi(p, n_p, "T_h", 0), Tw = gi(p, n_p, "T_w", 0), n = gi(p, n_p, "n", 0);
|
|
if (Sh <= 0 || Sw <= 0 || Th < 1 || Tw < 1 || n <= 0) return RAD_E_SHAPE;
|
|
Opd o;
|
|
o.t(D, { Sh * Sw, n }); /* table */
|
|
o.t(D, { Th * Tw, n }); /* y */
|
|
return o.finish("posemb_resample", operand, out);
|
|
}
|
|
|
|
/* window_order declines: its `crd` rows, segment grids and `cu` boundaries must describe whole
|
|
* raster grids, and an index draw cannot; avx_check_hand.cpp and the unit tests build them. */
|
|
int ref_window_order_shape(const RadParam*, int, int, RadOpdDesc*) { return RAD_E_UNSUPPORTED; }
|
|
|
|
/* grid_embed's operands plus the prefix rows; coordinates drawn over the table's side. */
|
|
int ref_grid_embed_cpe_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), side = gi(p, n_p, "side", 0);
|
|
const int64_t npre = gi(p, n_p, "n_prefix", 0);
|
|
if (M <= 0 || n <= 0 || side <= 0) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x, INOUT */
|
|
o.t(D, { side * side, n }); /* table */
|
|
o.t_idx({ 4, M }, side, -1, 0);
|
|
o.t(D, { npre > 0 ? npre : 1, n }); /* prefix */
|
|
return o.finish("grid_embed_cpe", operand, out);
|
|
}
|
|
|
|
/* grid_embed_hw: grid_embed_cpe's operands; only the resample's targets differ. */
|
|
int ref_grid_embed_hw_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), side = gi(p, n_p, "side", 0);
|
|
const int64_t npre = gi(p, n_p, "n_prefix", 0);
|
|
if (M <= 0 || n <= 0 || side <= 0) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x, INOUT */
|
|
o.t(D, { side * side, n }); /* table */
|
|
o.t_idx({ 4, M }, side, -1, 0);
|
|
o.t(D, { npre > 0 ? npre : 1, n }); /* prefix */
|
|
return o.finish("grid_embed_hw", operand, out);
|
|
}
|
|
|
|
/* attn_res_mix: the planes of `states` are the first n_state of a [max_blocks, max_tok, n] buffer,
|
|
* drawn dense here; a hand case builds the pitched form. */
|
|
int ref_attn_res_mix_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);
|
|
const int64_t nS = gi(p, n_p, "n_state", 0), nQ = gi(p, n_p, "n_query", 0);
|
|
if (M <= 0 || n <= 0 || nS < 0 || nQ <= 0) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
if (nS > 0) o.t(D, { nS, M, n }); /* states */
|
|
else o.skip();
|
|
o.t(D, { M, n }); /* cur */
|
|
o.t(D, { M, n }); /* addend */
|
|
o.t((uint32_t)RAD_F32, { nQ, n }); /* query */
|
|
o.t((uint32_t)RAD_F32, { nQ, n }); /* w */
|
|
o.t(D, { nQ, M, n }); /* y */
|
|
return o.finish("attn_res_mix", operand, out);
|
|
}
|
|
|
|
/* LiLiCorr's ops. lili_cand declines: its stats rows (sample_logprobs' sorted pairs and positive
|
|
* sums) are no draw; avx_check_hand.cpp builds them. The others draw freely. */
|
|
int ref_lili_cand_shape(const RadParam*, int, int, RadOpdDesc*) { return RAD_E_UNSUPPORTED; }
|
|
|
|
int ref_lili_attn_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), S = gi(p, n_p, "steps", 0), k = gi(p, n_p, "k", 0);
|
|
const int64_t nh = gi(p, n_p, "n_head", 0), hd = gi(p, n_p, "head_dim", 0);
|
|
if (M <= 0 || S <= 0 || k <= 0 || nh <= 0 || hd <= 0) return RAD_E_UNSUPPORTED;
|
|
const int64_t rows = (M + S * k - 1) / (S * k) * (S * k); /* whole lattices */
|
|
Opd o;
|
|
o.t(D, { rows, 3 * nh * hd }); /* qkv */
|
|
o.t((uint32_t)RAD_F32, { nh, 2 * S + 1 }); /* rel */
|
|
o.t((uint32_t)RAD_F32, { nh }); /* same */
|
|
o.t(D, { rows, nh * hd }); /* out */
|
|
return o.finish("lili_attn", operand, out);
|
|
}
|
|
|
|
int ref_lili_factor_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), L = gi(p, n_p, "L", 0), n = gi(p, n_p, "n", 0);
|
|
if (M <= 0 || L <= 0 || n <= 0) return RAD_E_UNSUPPORTED;
|
|
const int64_t rows = (M + L - 1) / L * L;
|
|
Opd o;
|
|
o.t(D, { rows, n }); /* h */
|
|
o.t(D, { rows / L, n }); /* a */
|
|
o.t(D, { rows, 3 * n }); /* y */
|
|
return o.finish("lili_factor", operand, out);
|
|
}
|
|
|
|
int ref_lili_walk_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), S = gi(p, n_p, "steps", 0), k = gi(p, n_p, "k", 0);
|
|
const int64_t n = gi(p, n_p, "n", 0);
|
|
if (M <= 0 || S <= 0 || k <= 0 || n <= 0) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(D, { M * S * k, 2 * n }); /* edges */
|
|
o.t(D, { M, n }); /* aout */
|
|
o.t_idx({ M * S, k }, 1000, -1, 0); /* cand: ids, copied */
|
|
o.t((uint32_t)RAD_I32, { M, S }); /* tokens */
|
|
o.t((uint32_t)RAD_F32, { M, S, k, k }); /* scores */
|
|
return o.finish("lili_walk", operand, out);
|
|
}
|
|
|
|
int ref_layernorm_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);
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x */
|
|
o.t(D, { n }); /* w */
|
|
/* `b` is optional but passed: the biased form covers strictly more code. */
|
|
o.t(D, { n }); /* b */
|
|
o.t(D, { M, n }); /* y */
|
|
return o.finish("layernorm", operand, out);
|
|
}
|
|
|
|
/* add / mul share a schema and a description. `b` is described at full extent; the row-broadcast
|
|
* form is not described, since its buffers cannot be told from a shape bug. */
|
|
static int binary_shape(const RadParam* p, int n_p, int operand, const char* op, 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);
|
|
Opd o;
|
|
o.t(D, { M, n }); /* a */
|
|
o.t(D, { M, n }); /* b */
|
|
o.t(D, { M, n }); /* y */
|
|
return o.finish(op, operand, out);
|
|
}
|
|
|
|
int ref_add_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
return binary_shape(p, n_p, operand, "add", out);
|
|
}
|
|
int ref_mul_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
return binary_shape(p, n_p, operand, "mul", out);
|
|
}
|
|
|
|
int ref_mul_weight_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);
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x */
|
|
if (gi(p, n_p, "scalar", 0)) o.t(D, { 1 }); /* w -- the scalar form */
|
|
else o.t(D, { n }); /* w -- one row */
|
|
o.t(D, { M, n }); /* y */
|
|
return o.finish("mul_weight", operand, out);
|
|
}
|
|
|
|
int ref_act_mul_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
return binary_shape(p, n_p, operand, "act_mul", out);
|
|
}
|
|
|
|
int ref_position_scale_shape(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);
|
|
if (M <= 0 || n <= 0) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(act_dt(p, n_p), {M, n});
|
|
o.t_idx({M}, 32768, -1, 0);
|
|
return o.finish("position_scale", operand, out);
|
|
}
|
|
|
|
int ref_embed_attn_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), D = gi(p, n_p, "head_dim", 0);
|
|
const int64_t G = gi(p, n_p, "gqa", 1), S = n_seq_of(p, n_p);
|
|
Opd o;
|
|
o.t(act_dt(p, n_p), {M, G * D});
|
|
o.t(act_dt(p, n_p), {M, D});
|
|
o.t(act_dt(p, n_p), {M, D});
|
|
o.t_idx({S + 1}, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU);
|
|
o.t(act_dt(p, n_p), {M, G * D});
|
|
return o.finish("embed_attn", operand, out);
|
|
}
|
|
|
|
int ref_ministral_rope_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), D = gi(p, n_p, "head_dim", 0);
|
|
const int64_t H = gi(p, n_p, "n_head", 0), K = gi(p, n_p, "n_head_kv", 0);
|
|
Opd o;
|
|
o.t(act_dt(p, n_p), {M, (H + 2 * K) * D});
|
|
o.t_idx({M}, 32768, -1, 0);
|
|
return o.finish("ministral_rope", operand, out);
|
|
}
|
|
|
|
int ref_pixtral_rope_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), D = gi(p, n_p, "head_dim", 0),
|
|
H = gi(p, n_p, "n_head", 0);
|
|
if (M <= 0 || D <= 0 || D % 4 || H <= 0) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(act_dt(p, n_p), {M, 3 * H * D});
|
|
o.t_idx({2, M}, 80, -1, 0);
|
|
return o.finish("pixtral_rope", operand, out);
|
|
}
|
|
|
|
int ref_pixtral_merge_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
/* The coordinates describe complete raster grids. Random independent indices cannot make
|
|
* a legal fixture: callers provide the grid and its patch data together. */
|
|
return RAD_E_UNSUPPORTED;
|
|
}
|
|
|
|
int ref_seg_mean_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);
|
|
if (M <= 0 || n <= 0) return RAD_E_UNSUPPORTED;
|
|
const int64_t S = n_seq_of(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x */
|
|
o.t_idx({ S + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu_seqlens */
|
|
o.t(D, { S, n }); /* y */
|
|
return o.finish("seg_mean", operand, out);
|
|
}
|
|
|
|
int ref_seg_softmax_pool_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);
|
|
if (M <= 0 || n <= 0) return RAD_E_UNSUPPORTED;
|
|
const int64_t S = n_seq_of(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M }); /* score, one a row of v */
|
|
o.t(D, { M, n }); /* v */
|
|
o.t_idx({ S + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu_seqlens */
|
|
o.t(D, { S, n }); /* y */
|
|
return o.finish("seg_softmax_pool", operand, out);
|
|
}
|
|
|
|
int ref_seg_mean_bf16_sum_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
if (act_dt(p, n_p) != RAD_BF16)
|
|
return RAD_E_UNSUPPORTED;
|
|
return ref_seg_mean_shape(p, n_p, operand, out);
|
|
}
|
|
|
|
int ref_pool_rows_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const uint32_t D = act_dt(p, n_p);
|
|
const int64_t n = gi(p, n_p, "n", 0), k = gi(p, n_p, "k", 0);
|
|
/* A band's bound need not be whole windows, but issued rows always are; round down. */
|
|
int64_t M = gi(p, n_p, "M", 0);
|
|
if (k > 0) M -= M % k;
|
|
if (M <= 0 || n <= 0 || k <= 0) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x */
|
|
o.t(D, { M / k, n }); /* y */
|
|
return o.finish("pool_rows", operand, out);
|
|
}
|
|
|
|
int ref_pool_cols_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const uint32_t D = act_dt(p, n_p);
|
|
const int64_t n = gi(p, n_p, "n", 0), k = gi(p, n_p, "k", 0), M = gi(p, n_p, "M", 0);
|
|
if (M <= 0 || n <= 0 || k <= 0 || n % k) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x */
|
|
o.t(D, { M, n / k }); /* y */
|
|
return o.finish("pool_cols", operand, out);
|
|
}
|
|
|
|
int ref_point_fourier_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), F = gi(p, n_p, "freqs", 0);
|
|
if (M <= 0 || n <= 0 || F <= 0 || F > 30) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(RAD_F32, { M, n }); /* x */
|
|
o.t(D, { M, n * (2 * F + 1) }); /* y */
|
|
return o.finish("point_fourier", operand, out);
|
|
}
|
|
|
|
int ref_relu_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);
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x */
|
|
o.t(D, { M, n }); /* y */
|
|
return o.finish("relu", operand, out);
|
|
}
|
|
|
|
int ref_clamp_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);
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x */
|
|
o.t(D, { 1 }); /* lo */
|
|
o.t(D, { 1 }); /* hi */
|
|
o.t(D, { M, n }); /* y */
|
|
return o.finish("clamp", operand, out);
|
|
}
|
|
|
|
/* The audio tower's segmented ops are described as one segment. `cu_seqlens` counts in units
|
|
* `cu_div` times finer than the operand's rows (a subsampled layer reads the pass's frame
|
|
* boundaries), so the rows are M / cu_div; a band that does not divide (into whole output rows, for
|
|
* a strided convolution) is declined. */
|
|
int ref_conv2d_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), W = gi(p, n_p, "width", 0);
|
|
const int64_t cin = gi(p, n_p, "cin", 0), cout = gi(p, n_p, "cout", 0);
|
|
const int64_t kh = gi(p, n_p, "kh", 0), kw = gi(p, n_p, "kw", 0);
|
|
const int64_t st = gi(p, n_p, "stride", 1), pad = gi(p, n_p, "pad", 0);
|
|
const int64_t cdiv = gi(p, n_p, "cu_div", 1), vst = gi(p, n_p, "valid_stride", 1);
|
|
if (M <= 0 || W <= 0 || st <= 0 || cdiv <= 0 || vst <= 0 || M % (cdiv * st))
|
|
return RAD_E_UNSUPPORTED;
|
|
const int64_t T = M / cdiv;
|
|
const int64_t To = (T + 2 * pad - kh) / st + 1, Wo = (W + 2 * pad - kw) / st + 1;
|
|
Opd o;
|
|
o.t(D, { T, W * cin }); /* x */
|
|
if (gi(p, n_p, "t_last", 0)) o.t(D, { cout, cin, kw, kh }); /* w, time last */
|
|
else o.t(D, { cout, cin, kh, kw }); /* w */
|
|
o.t_idx({ 2 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu_seqlens */
|
|
o.t_idx({ T * vst }, 2, -1, 0); /* valid: 0 or 1 a row */
|
|
o.t(D, { To, Wo * cout }); /* y */
|
|
o.t(D, { cout }); /* b */
|
|
return o.finish("conv2d", operand, out);
|
|
}
|
|
|
|
int ref_conv2d_unfold_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), W = gi(p, n_p, "width", 0), cin = gi(p, n_p, "cin", 0);
|
|
const int64_t kh = gi(p, n_p, "kh", 0), kw = gi(p, n_p, "kw", 0);
|
|
const int64_t st = gi(p, n_p, "stride", 1), pad = gi(p, n_p, "pad", 0);
|
|
const int64_t cdiv = gi(p, n_p, "cu_div", 1), vst = gi(p, n_p, "valid_stride", 1);
|
|
if (M <= 0 || W <= 0 || st <= 0 || cdiv <= 0 || vst <= 0 || M % (cdiv * st))
|
|
return RAD_E_UNSUPPORTED;
|
|
const int64_t T = M / cdiv;
|
|
const int64_t To = (T + 2 * pad - kh) / st + 1, Wo = (W + 2 * pad - kw) / st + 1;
|
|
Opd o;
|
|
o.t(D, { T, W * cin }); /* x */
|
|
o.t_idx({ 2 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu_seqlens */
|
|
o.t_idx({ T * vst }, 2, -1, 0); /* valid */
|
|
o.t(D, { To * Wo, cin * kh * kw }); /* y */
|
|
return o.finish("conv2d_unfold", operand, out);
|
|
}
|
|
|
|
int ref_conv1d_dw_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), C = gi(p, n_p, "n", 0), K = gi(p, n_p, "k", 0);
|
|
const int64_t cdiv = gi(p, n_p, "cu_div", 1);
|
|
if (M <= 0 || cdiv <= 0 || M % cdiv) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(D, { M / cdiv, C }); /* x */
|
|
o.t(D, { C, 1, K }); /* w */
|
|
o.t_idx({ 2 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu_seqlens */
|
|
o.t(D, { M / cdiv, C }); /* y */
|
|
o.skip(); /* b */
|
|
return o.finish("conv1d_dw", operand, out);
|
|
}
|
|
|
|
int ref_rel_sinusoid_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);
|
|
Opd o;
|
|
o.t(D, { M, n }); /* y */
|
|
return o.finish("rel_sinusoid", operand, out);
|
|
}
|
|
|
|
int ref_seg_sinusoid_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);
|
|
if (gi(p, n_p, "cu_div", 1) != 1) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t_idx({ 2 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu_seqlens */
|
|
o.t(D, { M, n }); /* y */
|
|
return o.finish("seg_sinusoid", operand, out);
|
|
}
|
|
|
|
int ref_attn_local_rel_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), hd = gi(p, n_p, "head_dim", 0);
|
|
const int64_t H = gi(p, n_p, "n_head", 0), L = gi(p, n_p, "left", 0);
|
|
const int64_t cdiv = gi(p, n_p, "cu_div", 1);
|
|
if (M <= 0 || cdiv <= 0 || M % cdiv || L < 0) return RAD_E_UNSUPPORTED;
|
|
const int64_t T = M / cdiv;
|
|
Opd o;
|
|
o.t(D, { T, H * hd }); /* q */
|
|
o.t(D, { T, H * hd }); /* k */
|
|
o.t(D, { T, H * hd }); /* v */
|
|
o.t(D, { L + 1, H * hd }); /* relk */
|
|
o.t(D, { hd }); /* pds */
|
|
o.t_idx({ 2 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu_seqlens */
|
|
o.t(D, { T, H * hd }); /* out */
|
|
return o.finish("attn_local_rel", operand, out);
|
|
}
|
|
|
|
int ref_swiglu_clamped_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
return ref_silu_mul_shape(p, n_p, operand, out);
|
|
}
|
|
int ref_router_sigmoid_topk_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), ne = gi(p, n_p, "n_expert", 0),
|
|
k = gi(p, n_p, "top_k", 0);
|
|
if (M < 1 || ne < 1 || ne > 512 || k < 1 || k > ne) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M, ne });
|
|
o.t(RAD_F32, { ne });
|
|
o.t_idx({ M, k }, ne, -1, 0);
|
|
o.t(D, { M, k });
|
|
return o.finish("router_sigmoid_topk", operand, out);
|
|
}
|
|
int ref_swiglu_step_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
return ref_silu_mul_shape(p, n_p, operand, out);
|
|
}
|
|
int ref_router_sigmoid_group_topk_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), ne = gi(p, n_p, "n_expert", 0),
|
|
k = gi(p, n_p, "top_k", 0), ng = gi(p, n_p, "n_group", 0),
|
|
kg = gi(p, n_p, "topk_group", 0);
|
|
if (M < 1 || ne < 1 || ne > 512 || k < 1 || ng < 1 || ne % ng || ne / ng < 2 || kg < 1 ||
|
|
kg > ng || k > kg * (ne / ng))
|
|
return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M, ne });
|
|
o.t(RAD_F32, { ne });
|
|
o.t_idx({ M, k }, ne, -1, 0);
|
|
o.t(D, { M, k });
|
|
return o.finish("router_sigmoid_group_topk", operand, out);
|
|
}
|
|
int ref_router_scored_topk_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), ne = gi(p, n_p, "n_expert", 0),
|
|
k = gi(p, n_p, "top_k", 0);
|
|
if (M < 1 || ne < 1 || ne > 512 || k < 1 || k > ne) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M, ne });
|
|
o.t(RAD_F32, { ne });
|
|
o.t_idx({ M, k }, ne, -1, 0);
|
|
o.t(D, { M, k });
|
|
return o.finish("router_scored_topk", operand, out);
|
|
}
|
|
int ref_mla_project_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), h = gi(p, n_p, "heads", 0), d = gi(p, n_p, "head_dim", 0),
|
|
l = gi(p, n_p, "latent_dim", 0);
|
|
const char* mode = gs(p, n_p, "mode");
|
|
const bool absorb = mode && !std::strcmp(mode, "absorb"),
|
|
expand = mode && !std::strcmp(mode, "expand");
|
|
if (M < 1 || h < 1 || d < 1 || l < 1 || (!absorb && !expand)) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(RAD_BF16, { M, h, absorb ? d : l });
|
|
o.t(RAD_BF16, { h * 2 * d, l });
|
|
o.t(RAD_BF16, { M, h, absorb ? l : d });
|
|
return o.finish("mla_project", operand, out);
|
|
}
|
|
int ref_dsa_query_shape(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);
|
|
if (M < 1 || n < 1 || n > 256 || (n & (n - 1))) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(RAD_BF16, { M, n });
|
|
o.t(RAD_F8E4M3, { M, n });
|
|
o.t(RAD_F32, { M, 1 });
|
|
return o.finish("dsa_query", operand, out);
|
|
}
|
|
int ref_dsa_pool_shape(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), ratio = gi(p, n_p, "ratio", 0);
|
|
if (M < 1 || n < 1 || n > 256 || (n & (n - 1)) || ratio < 2 || ratio > 16)
|
|
return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(RAD_BF16, { M * ratio, 2 * n });
|
|
o.t(RAD_F32, { ratio, n });
|
|
o.t(RAD_F8E4M3, { M, n });
|
|
o.t(RAD_F32, { M, 1 });
|
|
o.skip();
|
|
o.skip(); // Optional paged cache writes use a hand-built unique page list.
|
|
return o.finish("dsa_pool", operand, out);
|
|
}
|
|
int ref_dsa_score_shape(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), H = gi(p, n_p, "heads", 0);
|
|
const int64_t B = gi(p, n_p, "blocks", 0), S = gi(p, n_p, "seqs", 0), pages = S * B;
|
|
if (M < 1 || n < 1 || H < 1 || H > 64 || B < 1 || S < 1 || S > M) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(RAD_F8E4M3, { M, H, n });
|
|
o.t_fill(RAD_F32, { M, H }, RAD_FILL_SIGMOID, 0);
|
|
o.t(RAD_F32, { M, H });
|
|
o.t(RAD_F8E4M3, { pages, n });
|
|
o.t_fill(RAD_F32, { pages }, RAD_FILL_SIGMOID, 0);
|
|
o.t_idx({ S, B }, pages, -1, 0);
|
|
o.t_idx({ S + 1 }, M, -1, RAD_OPD_F_IDX_CU);
|
|
o.t_idx({ S }, 0, B, 0);
|
|
o.t(RAD_F32, { M, B });
|
|
o.skip(); // cu_part: a part's rebased bounds, exercised by the split's own tests.
|
|
return o.finish("dsa_score", operand, out);
|
|
}
|
|
int ref_mla_attn_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), D = gi(p, n_p, "latent_dim", 0),
|
|
H = gi(p, n_p, "heads", 0);
|
|
const int64_t B = gi(p, n_p, "blocks", 0), bs = gi(p, n_p, "block_size", 0),
|
|
pages = std::max((int64_t)2, B * M);
|
|
if (M < 1 || D < 1 || D > 2048 || H < 1 || B < 1 || bs < 1) return RAD_E_UNSUPPORTED;
|
|
const uint32_t dtype = act_dt(p, n_p);
|
|
/* An i8 latent has D codes followed by the two halves' f16 scales (four bytes). */
|
|
const char* kvs = gs(p, n_p, "kv_dtype");
|
|
const uint32_t kv = kvs && dt_parse(kvs) != (uint32_t)RAD_DT_INVALID ? dt_parse(kvs) : dtype;
|
|
if (kv == (uint32_t)RAD_I8 && D % 2) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(dtype, { M, H, D });
|
|
o.t(kv, { pages, 1, bs, D + (kv == (uint32_t)RAD_I8 ? 4 : 0) });
|
|
o.t_idx({ M, B }, pages, -1, 0);
|
|
o.t_idx({ M }, 0, B * bs, 0);
|
|
o.t(dtype, { M, H, D });
|
|
return o.finish("mla_attn", operand, out);
|
|
}
|
|
/* One sequence whose context is its query (a prefill chunk), as attn_paged describes itself; a
|
|
* harness passes `ctx` for a deeper history. */
|
|
int ref_mla_attn_paged_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), L = gi(p, n_p, "latent_dim", 0),
|
|
R = gi(p, n_p, "rope_dim", 0), H = gi(p, n_p, "heads", 0),
|
|
bs = gi(p, n_p, "block_size", 0);
|
|
if (M < 1 || L < 1 || L > 2048 || R < 1 || H < 1 || bs < 1) return RAD_E_UNSUPPORTED;
|
|
int64_t ctx = gi(p, n_p, "ctx", 0);
|
|
if (ctx < M) ctx = M;
|
|
const int64_t blocks = (ctx + bs - 1) / bs;
|
|
const uint32_t dtype = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(dtype, { M, H, L }); /* q */
|
|
o.t(dtype, { M, H, R }); /* q_rope */
|
|
o.t(dtype, { blocks, 1, bs, L + R }); /* kv */
|
|
o.t_idx({ 1, blocks }, blocks, -1, 0); /* block_table */
|
|
o.t_idx({ 1 }, 0, ctx, 0); /* seqused */
|
|
o.t_idx({ 2 }, M, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu_seqlens: [0, M], one sequence */
|
|
o.skip(); /* gate */
|
|
o.t(dtype, { M, H, L }); /* out */
|
|
return o.finish("mla_attn_paged", operand, out);
|
|
}
|
|
int ref_silu_mul_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);
|
|
Opd o;
|
|
o.t(D, { M, 2 * n }); /* gate_up -- the gate is the FIRST half */
|
|
o.t(D, { M, n }); /* y */
|
|
return o.finish("silu_mul", operand, out);
|
|
}
|
|
|
|
int ref_situ_mul_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);
|
|
Opd o;
|
|
o.t(D, { M, 2 * n }); /* gate_up -- the gate is the FIRST half */
|
|
o.t(D, { M, n }); /* y */
|
|
return o.finish("situ_mul", operand, out);
|
|
}
|
|
|
|
int ref_gelu_mul_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);
|
|
Opd o;
|
|
o.t(D, { M, 2 * n }); /* gate_up -- the gate is the FIRST half */
|
|
o.t(D, { M, n }); /* y */
|
|
return o.finish("gelu_mul", operand, out);
|
|
}
|
|
|
|
static int unary_shape(const RadParam* p, int n_p, int operand, const char* op, 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);
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x */
|
|
o.t(D, { M, n }); /* y */
|
|
return o.finish(op, operand, out);
|
|
}
|
|
|
|
int ref_gelu_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
return unary_shape(p, n_p, operand, "gelu", out);
|
|
}
|
|
int ref_silu_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
return unary_shape(p, n_p, operand, "silu", out);
|
|
}
|
|
int ref_sigmoid_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
return unary_shape(p, n_p, operand, "sigmoid", out);
|
|
}
|
|
|
|
int ref_fill_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);
|
|
Opd o;
|
|
o.t(D, { M, n }); /* y */
|
|
return o.finish("fill", operand, out);
|
|
}
|
|
|
|
int ref_logit_softcap_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
return unary_shape(p, n_p, operand, "logit_softcap", out);
|
|
}
|
|
int ref_scale_shape(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);
|
|
if (M <= 0 || n <= 0) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x */
|
|
o.t(D, { 1 }); /* s -- one scalar, broadcast over everything */
|
|
o.t(D, { M, n }); /* y */
|
|
return o.finish("scale", operand, out);
|
|
}
|
|
int ref_softmax_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
return unary_shape(p, n_p, operand, "softmax", out);
|
|
}
|
|
|
|
int ref_gather_rows_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);
|
|
Opd o;
|
|
/* 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. */
|
|
o.t(D, { 4 * M, n }); /* x */
|
|
o.t_idx({ M }, 0, -1, 0); /* idx */
|
|
o.t(D, { M, n }); /* y */
|
|
return o.finish("gather_rows", operand, out);
|
|
}
|
|
|
|
|
|
/* scatter_rows. The destination is 4x the scattered height: onto exactly M rows a scatter is a
|
|
* permutation, which a kernel ignoring `idx` could pass. Unnamed rows must come back unchanged. */
|
|
int ref_scatter_rows_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);
|
|
Opd o;
|
|
o.t(D, { M, n }); /* v */
|
|
o.t_idx({ M }, 4 * M, -1, RAD_OPD_F_IDX_UNIQUE); /* idx, distinct destinations */
|
|
o.t(D, { 4 * M, n }); /* x, inout */
|
|
return o.finish("scatter_rows", operand, out);
|
|
}
|
|
/* PLE's gate. `v` is the one narrow operand (n wide against hc*n): a single value row gated
|
|
* differently into each of the hc streams. */
|
|
int ref_ple_gate_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), hc = gi(p, n_p, "hc", 0);
|
|
const int64_t hn = hc * n;
|
|
Opd o;
|
|
o.t(D, { M, hn }); /* k */
|
|
o.t(D, { M, hn }); /* q */
|
|
o.t(D, { M, n }); /* v */
|
|
/* The three gains are f32 in the container, as hc_read's. */
|
|
o.t((uint32_t)RAD_F32, { hn }); /* w_key */
|
|
o.t((uint32_t)RAD_F32, { hn }); /* w_query */
|
|
o.t((uint32_t)RAD_F32, { hn }); /* w_conv */
|
|
o.t(D, { M, hn }); /* gv */
|
|
o.t(D, { M, hn }); /* gvn */
|
|
return o.finish("ple_gate", operand, out);
|
|
}
|
|
|
|
/* PLE's dilated convolution, described as a decode step: `num_accepted` present, so the state
|
|
* must be (width-1)*dilation - 1 + M deep for the shifted window (ref_ple.cpp), as the manager's
|
|
* (width-1)*dilation + n_spec is for a 1 + n_spec step.
|
|
*
|
|
* Warm and read at the deepest offset (`num_accepted` = M), so the offset arithmetic and shifted
|
|
* history are exercised. One sequence. */
|
|
int ref_ple_conv_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);
|
|
const int64_t wid = gi(p, n_p, "width", 0), dil = gi(p, n_p, "dilation", 1);
|
|
if (M <= 0 || n <= 0 || wid <= 0 || dil <= 0) return RAD_E_UNSUPPORTED;
|
|
const int64_t S = 1, hist = (wid - 1) * dil, depth = hist - 1 + M;
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x -- gvn */
|
|
o.t(D, { n, wid }); /* w */
|
|
o.t(D, { S, n, depth }); /* conv_state, INOUT */
|
|
o.t_idx({ S + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu */
|
|
o.t(D, { M, n }); /* resid -- gv */
|
|
o.t_idx({ S }, S, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE); /* cache_idx */
|
|
/* Warm, stated explicitly rather than relying on a null `has_init` default. */
|
|
o.t_idx({ S }, 0, 1, 0); /* has_init */
|
|
o.t_idx({ S }, 0, M, 0); /* num_accepted: read at M - 1 */
|
|
o.t(D, { M, n }); /* y */
|
|
return o.finish("ple_conv", operand, out);
|
|
}
|
|
|
|
/* ple_conv's description with `b` before `y`. */
|
|
int ref_ple_conv_bias_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);
|
|
const int64_t wid = gi(p, n_p, "width", 0), dil = gi(p, n_p, "dilation", 1);
|
|
if (M <= 0 || n <= 0 || wid <= 0 || dil <= 0) return RAD_E_UNSUPPORTED;
|
|
const int64_t S = 1, hist = (wid - 1) * dil, depth = hist - 1 + M;
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x */
|
|
o.t(D, { n, wid }); /* w */
|
|
o.t(D, { S, n, depth }); /* conv_state, INOUT */
|
|
o.t_idx({ S + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu */
|
|
o.t(D, { M, n }); /* resid */
|
|
o.t_idx({ S }, S, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE); /* cache_idx */
|
|
o.t_idx({ S }, 0, 1, 0); /* has_init: warm */
|
|
o.t_idx({ S }, 0, M, 0); /* num_accepted: read at M - 1 */
|
|
o.t(D, { n }); /* b */
|
|
o.t(D, { M, n }); /* y */
|
|
return o.finish("ple_conv_bias", operand, out);
|
|
}
|
|
|
|
/* ple_conv's description without `resid` and at dilation one. */
|
|
int ref_causal_conv1d_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), wid = gi(p, n_p, "width", 0);
|
|
if (M <= 0 || n <= 0 || wid <= 0) return RAD_E_UNSUPPORTED;
|
|
const int64_t S = 1, depth = wid - 2 + M;
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x */
|
|
o.t(D, { n, wid }); /* w */
|
|
o.t(D, { S, n, depth }); /* conv_state, INOUT */
|
|
o.t_idx({ S + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu */
|
|
o.t_idx({ S }, S, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE); /* cache_idx */
|
|
o.t_idx({ S }, 0, 1, 0); /* has_init: warm */
|
|
o.t_idx({ S }, 0, M, 0); /* num_accepted: read at M - 1 */
|
|
o.t(D, { M, n }); /* y */
|
|
return o.finish("causal_conv1d", operand, out);
|
|
}
|
|
|
|
/* causal_conv1d's description with `dyn` [M, heads*width] where `resid` would sit (sconv_res).
|
|
* Without dyn (heads 0) the operand is described at one head's width, which nothing reads. */
|
|
int ref_sconv_res_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), wid = gi(p, n_p, "width", 0);
|
|
const int64_t heads = gi(p, n_p, "heads", 0);
|
|
if (M <= 0 || n <= 0 || wid <= 0 || heads < 0) return RAD_E_UNSUPPORTED;
|
|
const int64_t S = 1, depth = wid - 2 + M;
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x */
|
|
o.t(D, { n, wid }); /* w */
|
|
o.t(D, { S, n, depth }); /* conv_state, INOUT */
|
|
o.t_idx({ S + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu */
|
|
o.t(D, { M, (heads > 0 ? heads : 1) * wid }); /* dyn */
|
|
o.t_idx({ S }, S, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE); /* cache_idx */
|
|
o.t_idx({ S }, 0, 1, 0); /* has_init: warm */
|
|
o.t_idx({ S }, 0, M, 0); /* num_accepted: read at M - 1 */
|
|
o.t(D, { M, n }); /* y */
|
|
return o.finish("sconv_res", operand, out);
|
|
}
|
|
|
|
/* PLE's n-gram ids. The three i64 weights are not index-like: `mult` is ~10^13, `vocab_sizes` a
|
|
* prime near 2*10^7 and `offsets` its prefix sum. Index-range draws would give a modulo by a small
|
|
* number, landing every id in the first rows of the table.
|
|
*
|
|
* `tok` and `state` are unbounded index operands (a token id is bounded by the vocabulary, which
|
|
* this op never sees); drawing over the whole i32 range also exercises the wrapping multiply. */
|
|
int ref_ngram_ids_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0);
|
|
const int64_t heads = gi(p, n_p, "heads", 0);
|
|
const int64_t ngram = gi(p, n_p, "ngram", 0);
|
|
const int64_t eos = gi(p, n_p, "eos", 0);
|
|
const int64_t ctx = ngram > 0 ? ngram - 1 : 0;
|
|
/* One sequence, a decode step as for ple_conv: `num_accepted` present, a warm window read at
|
|
* the deepest offset, and a state `ctx - 1 + M` deep for the shifted window. */
|
|
const int64_t S = 1, depth = ctx - 1 + M;
|
|
Opd o;
|
|
o.t_idx({ M }, 0, -1, 0); /* tok */
|
|
o.t_idx({ S, depth }, 0, -1, 0); /* state, INOUT */
|
|
o.t_idx({ S + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu */
|
|
o.t((uint32_t)RAD_I64, { ngram }); /* mult */
|
|
/* Not normal draws: the launch refuses `v <= 0 || off[h] < 0`, which a normal cast to i64
|
|
* fails on most heads, and the refusal would look like an unserved geometry.
|
|
*
|
|
* The vocabulary size is a constant eos + 1 (positive, and the vocabulary must contain eos).
|
|
* The offsets are drawn non-negative over [0, heads); they give the heads of one n-gram block,
|
|
* which share `mixed`, different answers. */
|
|
o.t_idx_dt((uint32_t)RAD_I64, { heads }, 0, eos + 1, 0); /* vocab_sizes */
|
|
o.t_idx_dt((uint32_t)RAD_I64, { heads }, 0, -1, 0); /* offsets */
|
|
o.t_idx({ S }, S, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE); /* cache_idx */
|
|
o.t_idx({ S }, 0, 1, 0); /* has_init: warm */
|
|
o.t_idx({ S }, 0, M, 0); /* num_accepted: at M - 1 */
|
|
o.t((uint32_t)RAD_I32, { M, heads }); /* ids */
|
|
o.skip(); /* ahead_ids */
|
|
return o.finish("ngram_ids", operand, out);
|
|
}
|
|
|
|
int ref_cast_shape(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);
|
|
/* Operand dtypes come from `from` and `to`; an unparseable spelling falls back to f32, the
|
|
* widest it could mean. */
|
|
uint32_t from = dt_parse(gs(p, n_p, "from"));
|
|
uint32_t to = dt_parse(gs(p, n_p, "to"));
|
|
if (from == (uint32_t)RAD_DT_INVALID) from = (uint32_t)RAD_F32;
|
|
if (to == (uint32_t)RAD_DT_INVALID) to = (uint32_t)RAD_F32;
|
|
Opd o;
|
|
o.t(from, { M, n }); /* x */
|
|
o.t(to, { M, n }); /* y */
|
|
return o.finish("cast", operand, out);
|
|
}
|
|
|
|
/* ================================================================== quantisation and rotation */
|
|
|
|
/* `asum` is described whenever `group` divides n, so a case checks the row sums as well. */
|
|
int ref_quant_act_i8_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), g = gi(p, n_p, "group", 0);
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x */
|
|
o.t((uint32_t)RAD_I8, { M, n }); /* q, in the step order */
|
|
o.t((uint32_t)RAD_F32, { M, 1 }); /* scale */
|
|
if (g > 0 && n % g == 0)
|
|
o.t((uint32_t)RAD_F32, { n / g, (M + 15) / 16 * 16 }); /* asum, group-major */
|
|
else
|
|
o.skip();
|
|
return o.finish("quant_act_i8", operand, out);
|
|
}
|
|
|
|
int ref_quant_act_i8g_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);
|
|
const int64_t ng = scale_groups(n, gi(p, n_p, "group", 0));
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x */
|
|
o.t((uint32_t)RAD_I8, { M, n }); /* q */
|
|
o.t((uint32_t)RAD_F32, { M, ng }); /* scale */
|
|
return o.finish("quant_act_i8g", operand, out);
|
|
}
|
|
|
|
int ref_quant_act_fp8_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);
|
|
const int64_t ng = scale_groups(n, gi(p, n_p, "group", 0));
|
|
Opd o;
|
|
/* `dtype` names the input; the output dtype is in the op name. */
|
|
o.t(D, { M, n }); /* x */
|
|
o.t((uint32_t)RAD_F8E4M3, { M, n }); /* q */
|
|
o.t((uint32_t)RAD_F32, { M, ng }); /* scale, a dequant multiplier (amax / 448) */
|
|
return o.finish("quant_act_fp8", operand, out);
|
|
}
|
|
|
|
int ref_quant_cols_fp8_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);
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x */
|
|
o.t((uint32_t)RAD_F8E4M3, { M, n }); /* y */
|
|
o.t((uint32_t)RAD_F32, { n }); /* scale, one a column (amax / 448) */
|
|
return o.finish("quant_cols_fp8", operand, out);
|
|
}
|
|
|
|
/* `n` is K on both axes of the accumulator: h is [n, n], not [M, n]. */
|
|
int ref_gram_accum_shape(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 char* dt = gs(p, n_p, "dtype");
|
|
Opd o;
|
|
if (dt && std::strcmp(dt, "bf16") == 0) {
|
|
o.t((uint32_t)RAD_BF16, { M, n }); /* a, read as it stands */
|
|
o.skip(); /* a_scale: a plain activation has none */
|
|
} else {
|
|
const int64_t ng = scale_groups(n, gi(p, n_p, "group", 0));
|
|
o.t((uint32_t)RAD_F8E4M3, { M, n }); /* a */
|
|
o.t((uint32_t)RAD_F32, { M, ng }); /* a_scale, a dequant multiplier (amax / 448) */
|
|
}
|
|
o.t((uint32_t)RAD_F32, { n, n }); /* h, read and added to */
|
|
return o.finish("gram_accum", operand, out);
|
|
}
|
|
|
|
/* quant_act_i8g's extents: the rotation changes the values, not the shapes. */
|
|
int ref_had_quant_act_i8g_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);
|
|
const int64_t ng = scale_groups(n, gi(p, n_p, "group", 0));
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x */
|
|
o.t((uint32_t)RAD_I8, { M, n }); /* q */
|
|
o.t((uint32_t)RAD_F32, { M, ng }); /* scale (amax / 127) */
|
|
return o.finish("had_quant_act_i8g", operand, out);
|
|
}
|
|
|
|
/* The int8 rotated forms: `group` is the Walsh-Hadamard rotation width (libr4d's `had`) and the
|
|
* scale is one per row, carrying the 1/sqrt(group) normalisation (ref_quant.cpp). Describing it as
|
|
* [M, n/group] would leave the tail of every scale buffer unwritten by both sides, comparing two
|
|
* zero fills. */
|
|
|
|
int ref_had_quant_act_i8_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);
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x */
|
|
o.t((uint32_t)RAD_I8, { M, n }); /* q */
|
|
o.t((uint32_t)RAD_F32, { M, 1 }); /* scale -- one per row */
|
|
return o.finish("had_quant_act_i8", operand, out);
|
|
}
|
|
|
|
int ref_rmsnorm_had_quant_i8_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);
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x */
|
|
/* `residual` passed, so the fused_add_rms_norm rounding (the norm reads the narrowed sum
|
|
* written back through the INOUT operand) is exercised. */
|
|
o.t(D, { M, n }); /* residual, INOUT */
|
|
o.t(D, { n }); /* w */
|
|
o.t((uint32_t)RAD_I8, { M, n }); /* q */
|
|
o.t((uint32_t)RAD_F32, { M, 1 }); /* scale -- one per row */
|
|
/* `out_bf16` (post-norm, pre-rotation) is written by both plugins when present, so
|
|
* described. */
|
|
o.t(D, { M, n }); /* out_bf16 */
|
|
return o.finish("rmsnorm_had_quant_i8", operand, out);
|
|
}
|
|
|
|
int ref_gated_had_quant_i8_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);
|
|
Opd o;
|
|
/* The packed form the MLP issues: `b` absent, `a` [M, 2n] with the gate first. */
|
|
o.t(D, { M, 2 * n }); /* a */
|
|
o.skip(); /* b */
|
|
o.t((uint32_t)RAD_I8, { M, n }); /* q */
|
|
o.t((uint32_t)RAD_F32, { M, 1 }); /* scale -- one per row */
|
|
return o.finish("gated_had_quant_i8", operand, out);
|
|
}
|
|
|
|
int ref_dequant_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);
|
|
const int64_t ng = scale_groups(n, gi(p, n_p, "group", 0));
|
|
Opd o;
|
|
o.t((uint32_t)RAD_I8, { M, n }); /* q */
|
|
o.t((uint32_t)RAD_F32, { M, ng }); /* scale */
|
|
o.t(D, { M, n }); /* y */
|
|
return o.finish("dequant", operand, out);
|
|
}
|
|
|
|
int ref_dequant_w4_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t N = gi(p, n_p, "N", 0), K = gi(p, n_p, "K", 0);
|
|
const int64_t group = gi(p, n_p, "group", 128);
|
|
if (N < 1 || K < 1 || N % 16 || K % 64 || group < 1 || K % group) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(RAD_I4, { N * K }); /* wq -- packed int4: N*K nibbles = N*K/2 bytes = N*K/8 dwords */
|
|
o.t(RAD_F32, { N * K / group }); /* ws -- scale (f16 in a dword, but declared f32) */
|
|
o.t(RAD_BF16, { N, K }); /* y */
|
|
return o.finish("dequant_w4", operand, out);
|
|
}
|
|
|
|
/* ================================================================== gemm */
|
|
|
|
int ref_gemm_nt_f32_shape(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 < 1 || N < 1 || K < 1) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(RAD_BF16, { M, K });
|
|
o.t(RAD_F32, { N, K });
|
|
o.t(RAD_F32, { M, N });
|
|
return o.finish("gemm_nt_f32", operand, out);
|
|
}
|
|
int ref_gemm_nt_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), K = gi(p, n_p, "K", 0);
|
|
Opd o;
|
|
o.t(D, { M, K }); /* a */
|
|
o.t(D, { N, K }); /* b -- stored transposed, so both operands are k-contiguous */
|
|
o.t(D, { M, N }); /* y */
|
|
return o.finish("gemm_nt", operand, out);
|
|
}
|
|
|
|
int ref_gemm_nt_bias_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), K = gi(p, n_p, "K", 0);
|
|
Opd o;
|
|
o.t(D, { M, K }); /* a */
|
|
o.t(D, { N, K }); /* b */
|
|
o.t(D, { N }); /* bias */
|
|
/* `res` is optional but passed, to cover the residual epilogue. */
|
|
o.t(D, { M, N }); /* res */
|
|
o.t(D, { M, N }); /* y */
|
|
return o.finish("gemm_nt_bias", operand, out);
|
|
}
|
|
|
|
/* Block-scaled fp8, the symmetric int4 and int8 weights against quant_act_i8's activation, the
|
|
* int8 trunk (`i8a8`) and OCP-MXFP4 (`mxfp4a8`). The zero-point grids (`w2a8`, `w4a8_asym`,
|
|
* `w4a16_asym`, `w5a16`, `w4a8i8_asym`, `w5a8i8`) read their scale and zero joined in one operand
|
|
* (ref_planes.h), which a description cannot state; they and any other format are declined rather
|
|
* than guessed.
|
|
*
|
|
* The fp8 grid is stated by the checkpoint: E4M3 [N][K] against a bf16 [ceil(N/128)][ceil(K/128)]
|
|
* plane, `group` carrying the 128. w4a8 and w8a8 are the affine encoding's two planes, codes and
|
|
* an f16 scale per (row, `group` of K), against int8 codes in the step order with one f32 scale a
|
|
* row. `a_sum` (asymmetric grids) is absent, and so is `b_ref` except at mxfp4a8. Activation
|
|
* scales are drawn in (0, 1): a quantiser's are never negative, and an MXFP4 kernel treats a
|
|
* block whose scale is not positive as empty (docs/OPS.md). */
|
|
static int gemm_q_shape(const RadParam* p, int n_p, int operand, bool bias, 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);
|
|
const int64_t group = gi(p, n_p, "group", 0);
|
|
const char* q = gs(p, n_p, "dtype");
|
|
auto is = [&](const char* s) { return q && std::strcmp(q, s) == 0; };
|
|
const bool f32s = is("fp8a8f32");
|
|
const bool a8 = is("fp8a8") || f32s, a16 = is("fp8a16"), w4 = is("w4a8"), w8 = is("w8a8");
|
|
/* i8a8h is i8a8's operands; its rotation is in the values. */
|
|
const bool i8 = is("i8a8") || is("i8a8h"), mx = is("mxfp4a8");
|
|
if ((!a8 && !a16 && !w4 && !w8 && !i8 && !mx) || N <= 0 || K <= 0 || group <= 0)
|
|
return RAD_E_UNSUPPORTED;
|
|
/* Neither has a bias form in any architecture. */
|
|
if (bias && (i8 || mx)) return RAD_E_UNSUPPORTED;
|
|
/* The bias form requires `a_scale`, so the fp8a16 reading (bf16 activation, no scale) cannot
|
|
* be expressed there. */
|
|
if (bias && a16) return RAD_E_UNSUPPORTED;
|
|
|
|
const int64_t kb = (K + group - 1) / group;
|
|
const int64_t nb = (N + group - 1) / group;
|
|
Opd o;
|
|
if (i8 || mx) {
|
|
/* i8a8: quant_act_i8g's row-major codes and f32 scale per (row, `group` of K) against int8
|
|
* codes with a bf16 scale per (row, `group` of K). mxfp4a8: quant_act_fp8's pair at 128
|
|
* against e2m1 codes with an E8M0 exponent per (row, `group` = 32 of K); `b_ref` is the
|
|
* same exponent plane, which an mxfp4 kernel reduces to a per-row reference at load. */
|
|
const int64_t akb = mx ? (K + 127) / 128 : kb;
|
|
o.t((uint32_t)(mx ? RAD_F8E4M3 : RAD_I8), { M, K }); /* a */
|
|
o.t_fill((uint32_t)RAD_F32, { M, akb }, (uint32_t)RAD_FILL_SIGMOID, 0); /* a_scale */
|
|
o.t((uint32_t)(mx ? RAD_FP4E2M1 : RAD_I8), { N, K }); /* b */
|
|
o.t((uint32_t)(mx ? RAD_E8M0 : RAD_BF16), { N, kb }); /* b_scale */
|
|
o.t((uint32_t)RAD_BF16, { M, N }); /* y */
|
|
o.skip(); /* a_sum */
|
|
if (mx) o.t((uint32_t)RAD_E8M0, { N, kb }); /* b_ref */
|
|
else o.skip();
|
|
return o.finish("gemm_nt_q", operand, out);
|
|
}
|
|
if (w4 || w8) {
|
|
o.t((uint32_t)RAD_I8, { M, K }); /* a, in the step order */
|
|
o.t_fill((uint32_t)RAD_F32, { M, 1 }, (uint32_t)RAD_FILL_SIGMOID, 0); /* a_scale */
|
|
o.t((uint32_t)(w4 ? RAD_I4 : RAD_I8), { N, K }); /* b */
|
|
o.t((uint32_t)RAD_F16, { N, kb }); /* b_scale */
|
|
} else {
|
|
if (a8) {
|
|
o.t((uint32_t)RAD_F8E4M3, { M, K }); /* a */
|
|
o.t_fill((uint32_t)RAD_F32, { M, kb }, (uint32_t)RAD_FILL_SIGMOID, 0); /* a_scale */
|
|
} else {
|
|
o.t((uint32_t)RAD_BF16, { M, K }); /* a */
|
|
o.skip(); /* a_scale */
|
|
}
|
|
o.t((uint32_t)RAD_F8E4M3, { N, K }); /* b -- the checkpoint's own bytes */
|
|
/* b_scale -- weight_scale_inv, per 128x128; f32 at fp8a8f32 */
|
|
o.t(f32s ? (uint32_t)RAD_F32 : (uint32_t)RAD_BF16, { nb, kb });
|
|
}
|
|
if (bias) {
|
|
o.t((uint32_t)RAD_BF16, { N }); /* bias */
|
|
o.t((uint32_t)RAD_BF16, { M, N }); /* y */
|
|
return o.finish("gemm_nt_q_bias", operand, out);
|
|
}
|
|
o.t((uint32_t)RAD_BF16, { M, N }); /* y */
|
|
o.skip(); /* a_sum */
|
|
o.skip(); /* b_ref */
|
|
return o.finish("gemm_nt_q", operand, out);
|
|
}
|
|
|
|
int ref_gemm_nt_q_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
return gemm_q_shape(p, n_p, operand, false, out);
|
|
}
|
|
int ref_gemm_nt_q_bias_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
return gemm_q_shape(p, n_p, operand, true, out);
|
|
}
|
|
|
|
/* Runtime LoRA. `slot` is drawn in [0, S], S itself being a base row one past the pool, so a cold
|
|
* draw covers the rows neither op may touch as well as the adapter rows. A draw never yields -1,
|
|
* the other spelling of a base row, which is a hand case (tests/lora_ops_test.cpp, ref_test). A
|
|
* single row is drawn in [0, S): a one-row call whose row drew S would write nothing, which
|
|
* rad-avx-check refuses as vacuous. The scale stream exists only beside a coded activation; a float
|
|
* `a` is read as it stands, as gemm_nt_q's fp8a16 form reads it. `group` defaults to 128, the
|
|
* block every block-scaled checkpoint and quantiser here uses, when the geometry states none. */
|
|
int ref_lora_shrink_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), K = gi(p, n_p, "K", 0);
|
|
const int64_t R = gi(p, n_p, "R", 0), S = gi(p, n_p, "S", 0);
|
|
int64_t group = gi(p, n_p, "group", 0);
|
|
const bool coded = D == (uint32_t)RAD_F8E4M3 || D == (uint32_t)RAD_F8E5M2 ||
|
|
D == (uint32_t)RAD_I8;
|
|
if (coded && group <= 0) group = 128;
|
|
Opd o;
|
|
o.t(D, { M, K }); /* a */
|
|
if (coded) o.t((uint32_t)RAD_F32, { M, (K + group - 1) / group }); /* a_scale */
|
|
else o.skip(); /* a_scale */
|
|
o.t_idx({ M }, M > 1 ? S + 1 : S, -1, 0); /* slot, S = base */
|
|
o.t((uint32_t)RAD_BF16, { S, R, K }); /* lora_a */
|
|
o.t((uint32_t)RAD_F32, { M, R }); /* t */
|
|
return o.finish("lora_shrink", operand, out);
|
|
}
|
|
|
|
int ref_lora_expand_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);
|
|
const int64_t R = gi(p, n_p, "R", 0), S = gi(p, n_p, "S", 0);
|
|
Opd o;
|
|
o.t((uint32_t)RAD_F32, { M, R }); /* t */
|
|
o.t_idx({ M }, M > 1 ? S + 1 : S, -1, 0); /* slot, S = base */
|
|
o.t((uint32_t)RAD_BF16, { S, N, R }); /* lora_b */
|
|
o.t((uint32_t)RAD_F32, { S, N }); /* scale */
|
|
o.t(D, { M, N }); /* y */
|
|
return o.finish("lora_expand", operand, out);
|
|
}
|
|
|
|
int ref_lora_expand_mm_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
return ref_lora_expand_shape(p, n_p, operand, out);
|
|
}
|
|
|
|
/* ================================================================== attention
|
|
*
|
|
* The paged layout (ref_ops.h):
|
|
*
|
|
* kv_cache[num_blocks, kv_heads, block_size, 2 * head_dim] K first, V second in a slot
|
|
*
|
|
* The block count and context length are this description's choice; any cache large enough for
|
|
* the indices will do. */
|
|
|
|
static int ref_attn_paged_shape_impl(const RadParam* p, int n_p, int operand, RadOpdDesc* out,
|
|
const char* op_name) {
|
|
/* `q_len` is the ranged parameter, a query length (one row at decode, thousands in a prefill
|
|
* chunk; different kernels, docs/OPS.md). */
|
|
const int64_t M = gi(p, n_p, "q_len", 0);
|
|
const int64_t hd = gi(p, n_p, "head_dim", 0);
|
|
const int64_t bs = gi(p, n_p, "block_size", 16);
|
|
const int64_t gqa = gi(p, n_p, "gqa", 1);
|
|
const int64_t nh = gi(p, n_p, "n_head", gqa);
|
|
if (M <= 0 || hd <= 0 || bs <= 0 || gqa <= 0 || nh <= 0 || nh % gqa) return RAD_E_UNSUPPORTED;
|
|
const int64_t kvh = nh / gqa;
|
|
|
|
/* Context depth, not the query length. By default one sequence whose context is exactly its
|
|
* query (a prefill chunk's case), which never exercises deep paging, KV splits or sliding
|
|
* windows. A harness sweeping depth passes `ctx` (no op declares it; kernels read the context
|
|
* from `seqused`). It is at least the query length, or causal attention would read a
|
|
* nonexistent prefix. */
|
|
const int64_t S = 1;
|
|
int64_t ctx = gi(p, n_p, "ctx", 0);
|
|
if (ctx < M) ctx = M;
|
|
const int64_t max_blocks = (ctx + bs - 1) / bs;
|
|
|
|
/* `q_dtype` and `kv_dtype` are read separately: the engine issues int8 caches beside bf16
|
|
* queries. An int8 cache is not described: a row's two f16 scales cannot be drawn at random
|
|
* (the hand cases build one). */
|
|
const uint32_t Q = act_dt(p, n_p);
|
|
const uint32_t kv = dt_parse(gs(p, n_p, "kv_dtype"));
|
|
const uint32_t KV = kv == (uint32_t)RAD_DT_INVALID ? Q : kv;
|
|
if (KV == (uint32_t)RAD_I8) return RAD_E_UNSUPPORTED;
|
|
|
|
Opd o;
|
|
o.t(Q, { S * M, nh, hd }); /* q */
|
|
o.t(KV, { max_blocks, kvh, bs, 2 * hd }); /* kv_cache */
|
|
o.t_idx({ S, max_blocks }, max_blocks, -1, 0); /* block_table -- indexes blocks, may repeat */
|
|
/* A constant, not a draw (see the file header). */
|
|
o.t_idx({ S }, 0, ctx, 0); /* seqused */
|
|
o.skip(); /* k_descale -- no per-(seq,head) descales */
|
|
o.skip(); /* v_descale */
|
|
/* `cu_seqlens` absent: the rectangular reading, the only one at one sequence. Present, it
|
|
* would have to agree with a tool's `M` rather than this description's. */
|
|
o.skip(); /* cu_seqlens */
|
|
o.t(Q, { S * M, nh, hd }); /* out */
|
|
return o.finish(op_name, operand, out);
|
|
}
|
|
|
|
int ref_attn_paged_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
return ref_attn_paged_shape_impl(p, n_p, operand, out, "attn_paged");
|
|
}
|
|
|
|
int ref_attn_paged_anchor_window_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
return ref_attn_paged_shape_impl(p, n_p, operand, out, "attn_paged_anchor_window");
|
|
}
|
|
|
|
/* attn_paged's operands and its tree's parents, each row an ordinary causal one (-2): a drawn
|
|
* plane would name parents that are not a tree. */
|
|
int ref_attn_paged_tree_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
if (operand < 8) return ref_attn_paged_shape(p, n_p, operand, out);
|
|
const int64_t M = gi(p, n_p, "q_len", 0);
|
|
if (M <= 0) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
for (int i = 0; i < 8; ++i) o.skip();
|
|
o.t_idx({ M }, 0, -2, 0); /* spec_parent */
|
|
return o.finish("attn_paged_tree", operand, out);
|
|
}
|
|
|
|
/* attn_paged's operands with the sinks before `out`: a learned logit a query head. */
|
|
int ref_attn_paged_sink_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int nbase = 8;
|
|
if (operand < 7) return ref_attn_paged_shape(p, n_p, operand, out);
|
|
if (operand == 8) return ref_attn_paged_shape(p, n_p, nbase - 1, out);
|
|
if (operand != 7 || !out) return RAD_E_SHAPE;
|
|
const int64_t gqa = gi(p, n_p, "gqa", 1);
|
|
const int64_t nh = gi(p, n_p, "n_head", gqa);
|
|
if (nh <= 0) return RAD_E_UNSUPPORTED;
|
|
std::memset(out, 0, sizeof *out);
|
|
out->fill = (uint32_t)RAD_FILL_NORMAL;
|
|
out->idx_const = -1;
|
|
out->dtype = (uint32_t)RAD_F32;
|
|
out->rank = 1;
|
|
out->shape[0] = nh;
|
|
return RAD_OK;
|
|
}
|
|
|
|
/* attn_paged's operands, then r [M, n_head, d_rel] at q's dtype, rel_proj [d_rel, extent] f32 and
|
|
* sinks [M, n_head] f32 (one per query row and head), then out. A single sequence, as
|
|
* attn_paged. */
|
|
int ref_attn_paged_rel_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
if (operand < 7) return ref_attn_paged_shape(p, n_p, operand, out);
|
|
if (operand == 10) return ref_attn_paged_shape(p, n_p, 7, out);
|
|
if (operand > 9 || !out) return RAD_E_SHAPE;
|
|
const int64_t M = gi(p, n_p, "q_len", 0);
|
|
const int64_t gqa = gi(p, n_p, "gqa", 1);
|
|
const int64_t nh = gi(p, n_p, "n_head", gqa);
|
|
const int64_t dr = gi(p, n_p, "d_rel", 0);
|
|
const int64_t ext = gi(p, n_p, "extent", 0);
|
|
if (M <= 0 || nh <= 0 || dr <= 0 || ext <= 0) return RAD_E_UNSUPPORTED;
|
|
RadOpdDesc qd;
|
|
const int rc = ref_attn_paged_shape(p, n_p, 0, &qd);
|
|
if (rc != RAD_OK) return rc;
|
|
std::memset(out, 0, sizeof *out);
|
|
out->fill = (uint32_t)RAD_FILL_NORMAL;
|
|
out->idx_const = -1;
|
|
if (operand == 7) {
|
|
out->dtype = qd.dtype;
|
|
out->rank = 3;
|
|
out->shape[0] = M; out->shape[1] = nh; out->shape[2] = dr;
|
|
} else if (operand == 8) {
|
|
out->dtype = (uint32_t)RAD_F32;
|
|
out->rank = 2;
|
|
out->shape[0] = dr; out->shape[1] = ext;
|
|
} else {
|
|
out->dtype = (uint32_t)RAD_F32;
|
|
out->rank = 2;
|
|
out->shape[0] = M; out->shape[1] = nh;
|
|
}
|
|
return RAD_OK;
|
|
}
|
|
|
|
static int attn_dense_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out,
|
|
bool reference) {
|
|
const int64_t M = gi(p, n_p, "M", 0);
|
|
const int64_t hd = gi(p, n_p, "head_dim", 0);
|
|
const int64_t gqa = gi(p, n_p, "gqa", 1);
|
|
if (M <= 0 || hd <= 0 || gqa <= 0) return RAD_E_UNSUPPORTED;
|
|
/* A segment read in units finer than the rows has no [0, ..., T] description. */
|
|
if (gi(p, n_p, "cu_div", 1) != 1) return RAD_E_UNSUPPORTED;
|
|
/* The head count is not in the schema (`gqa` is a ratio; the count is read off `q`), so this
|
|
* picks `gqa` query heads over one KV head, the smallest geometry exercising the grouping. */
|
|
const int64_t nh = gqa, kvh = 1;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
const int64_t S = n_seq_of(p, n_p);
|
|
|
|
Opd o;
|
|
o.t(D, { M, nh, hd }); /* q */
|
|
o.t(D, { M, kvh, hd }); /* k */
|
|
o.t(D, { M, kvh, hd }); /* v */
|
|
/* [0, ..., T], the batch's shape, not a draw. */
|
|
o.t_idx({ S + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu_seqlens */
|
|
o.t(D, { M, nh, hd }); /* out */
|
|
if (reference) {
|
|
o.t_idx({3 * S + 1}, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU);
|
|
o.t(RAD_F32, {S});
|
|
}
|
|
return o.finish(reference ? "attn_dense_ref" : "attn_dense", operand, out);
|
|
}
|
|
|
|
int ref_attn_dense_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
return attn_dense_shape(p, n_p, operand, out, false);
|
|
}
|
|
|
|
int ref_attn_dense_ref_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
return attn_dense_shape(p, n_p, operand, out, true);
|
|
}
|
|
|
|
int ref_attn_cross_shape(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);
|
|
const int64_t gqa = gi(p, n_p, "gqa", 1);
|
|
if (M <= 0 || hd <= 0 || gqa <= 0) return RAD_E_UNSUPPORTED;
|
|
const int64_t nh = gqa, kvh = 1;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
const int64_t S = n_seq_of(p, n_p);
|
|
|
|
/* As many key rows as query rows, each side bounded by its own [0, ..., T]. */
|
|
Opd o;
|
|
o.t(D, { M, nh, hd }); /* q */
|
|
o.t(D, { M, kvh, hd }); /* k */
|
|
o.t(D, { M, kvh, hd }); /* v */
|
|
o.t_idx({ S + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu_seqlens */
|
|
o.t_idx({ S + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu_k */
|
|
o.t(D, { M, nh, hd }); /* out */
|
|
return o.finish("attn_cross", operand, out);
|
|
}
|
|
|
|
int ref_table_add_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), L = gi(p, n_p, "L", 0);
|
|
if (M <= 0 || n <= 0 || L <= 0) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x */
|
|
o.t(D, { L, n }); /* t */
|
|
o.t(D, { M, L * n }); /* y */
|
|
return o.finish("table_add", operand, out);
|
|
}
|
|
|
|
int ref_mix_rows_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), k = gi(p, n_p, "k", 0);
|
|
if (M <= 0 || n <= 0 || k <= 0) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(D, { M * k, n }); /* x */
|
|
o.t(D, { k }); /* w */
|
|
o.t(D, { M, n }); /* y */
|
|
return o.finish("mix_rows", operand, out);
|
|
}
|
|
|
|
int ref_chan_stats_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);
|
|
if (M <= 0 || n <= 0) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x */
|
|
o.t(D, { n }); /* mean */
|
|
o.t_fill(D, { n }, (uint32_t)RAD_FILL_SIGMOID, 0); /* s -- a deviation or variance, > 0 */
|
|
o.t(D, { M, n }); /* y */
|
|
return o.finish("chan_stats", operand, out);
|
|
}
|
|
|
|
/* The SAM tower's attention (ref_sam_attn.cpp): a row of `n_head * head_dim`, one raster
|
|
* grid_h x grid_w segment a cu entry, and the two rel_pos tables. Their length follows the contract
|
|
* in arch/common/rad_block_sam.h: 2*maxdist-1 rows with maxdist the window, or the longer grid axis
|
|
* for a global layer (which is the length a resampled table needs; the op resamples along dim 0
|
|
* either way). */
|
|
int ref_sam_attn_shape(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);
|
|
const int64_t nh = gi(p, n_p, "n_head", 0);
|
|
const int64_t gh = gi(p, n_p, "grid_h", 0);
|
|
const int64_t gw = gi(p, n_p, "grid_w", 0);
|
|
const int64_t win = gi(p, n_p, "window", 0);
|
|
if (M <= 0 || hd <= 0 || nh <= 0 || gh <= 0 || gw <= 0 || win < 0) return RAD_E_UNSUPPORTED;
|
|
/* A shape hook answers "describe this geometry", so an unbuildable one spells E_UNSUPPORTED
|
|
* (the harness convention); the kernels' own launch checks report E_SHAPE for the same
|
|
* inputs. */
|
|
const int64_t W = nh * hd;
|
|
const int64_t S = n_seq_of(p, n_p);
|
|
const int64_t maxd = win > 0 ? win : (gh > gw ? gh : gw);
|
|
const int64_t tab = 2 * maxd - 1;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
|
|
Opd o;
|
|
o.t(D, { M, W }); /* q */
|
|
o.t(D, { M, W }); /* k */
|
|
o.t(D, { M, W }); /* v */
|
|
/* [0, ..., M], the batch's shape, not a draw. */
|
|
o.t_idx({ S + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu_seqlens */
|
|
o.t(D, { M, W }); /* out */
|
|
o.t(D, { tab, hd }); /* rel_pos_h */
|
|
o.t(D, { tab, hd }); /* rel_pos_w */
|
|
return o.finish("sam_attn", operand, out);
|
|
}
|
|
|
|
int ref_kv_store_shape(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);
|
|
const int64_t kvh = gi(p, n_p, "n_head_kv", 0);
|
|
const int64_t bs = gi(p, n_p, "block_size", 0);
|
|
if (M <= 0 || hd <= 0 || kvh <= 0 || bs <= 0) return RAD_E_UNSUPPORTED;
|
|
/* Two blocks of slack so the slot mapping is not a dense prefix and the block index matters. */
|
|
const int64_t nb = (M + bs - 1) / bs + 2;
|
|
/* Only the cache takes `kv_dtype`: k and v are bf16 activations whatever the cache stores
|
|
* (act_dt would fall back on kv_dtype and describe an int8 k). */
|
|
const uint32_t D = (uint32_t)RAD_BF16;
|
|
const uint32_t kv = dt_parse(gs(p, n_p, "kv_dtype"));
|
|
const uint32_t KV = kv == (uint32_t)RAD_DT_INVALID ? D : kv;
|
|
|
|
Opd o;
|
|
o.t(D, { M, kvh, hd }); /* k */
|
|
o.t(D, { M, kvh, hd }); /* v */
|
|
/* A scatter destination: distinct (see the file header), over the whole cache in slots. */
|
|
o.t_idx({ M }, nb * bs, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE); /* slot_mapping */
|
|
o.t(KV, { nb, kvh, bs, rad_kv_row_elems(KV, hd) }); /* kv_cache, INOUT */
|
|
return o.finish("kv_store", operand, out);
|
|
}
|
|
|
|
/* The streamed store: kv_store's geometry plus the mirror (one block a row at the ring's pitch)
|
|
* and the residency map. The described call touches one block, resident in slot 0: the draw
|
|
* cannot express an injective map, and two blocks drawing one slot would race on a ring byte.
|
|
* Partly resident maps and mirror-only writes are built by hand in tests/ref_test.cpp and in
|
|
* libavx's hand cases. `M <= block_size` keeps every token on its own position of that block. */
|
|
int ref_kv_store_streamed_shape(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);
|
|
const int64_t kvh = gi(p, n_p, "n_head_kv", 0);
|
|
const int64_t bs = gi(p, n_p, "block_size", 0);
|
|
if (M <= 0 || hd <= 0 || kvh <= 0 || bs <= 0 || M > bs) return RAD_E_UNSUPPORTED;
|
|
const uint32_t kv = dt_parse(gs(p, n_p, "kv_dtype"));
|
|
const uint32_t KV = kv == (uint32_t)RAD_DT_INVALID ? (uint32_t)RAD_I8 : kv;
|
|
const int64_t rowe = rad_kv_row_elems(KV, hd);
|
|
const int64_t row = kvh * bs * rowe * (KV == (uint32_t)RAD_BF16 ? 2 : 1);
|
|
|
|
Opd o;
|
|
o.t((uint32_t)RAD_BF16, { M, kvh, hd }); /* k */
|
|
o.t((uint32_t)RAD_BF16, { M, kvh, hd }); /* v */
|
|
o.t_idx({ M }, bs, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE); /* slot_mapping, one block */
|
|
o.t(KV, { 1, kvh, bs, rowe }); /* kv_cache, INOUT */
|
|
o.t_idx({ 1 }, 1, -1, 0); /* res -- resident in slot 0 */
|
|
o.t((uint32_t)RAD_U8, { 1, row }); /* host, INOUT */
|
|
return o.finish("kv_store_streamed", operand, out);
|
|
}
|
|
|
|
int ref_rope_shape(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);
|
|
const int64_t nh = gi(p, n_p, "n_head", 0);
|
|
const int64_t nkv = gi(p, n_p, "n_head_kv", nh);
|
|
if (M <= 0 || hd <= 0 || nh <= 0) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p); /* no `dtype` on this op: bf16, see act_dt */
|
|
Opd o;
|
|
/* The fused row: n_head q heads, then n_head_kv each of k and v, head_dim wide. */
|
|
o.t(D, { M, (nh + 2 * nkv) * hd }); /* qkv, INOUT */
|
|
/* A multi-component mode with sections takes one position per component, component-major:
|
|
* the section count, or three for imrope. */
|
|
const char* mode = gs(p, n_p, "mode");
|
|
if (!mode) mode = "neox";
|
|
const char* sec = gs(p, n_p, "sections");
|
|
int64_t nc = 0;
|
|
for (const char* q = sec; q && *q; ) {
|
|
char* e = nullptr;
|
|
(void)std::strtoll(q, &e, 10);
|
|
if (e == q) break;
|
|
++nc;
|
|
q = e;
|
|
}
|
|
const bool multi = nc > 0 && (!std::strcmp(mode, "mrope") || !std::strcmp(mode, "imrope") ||
|
|
!std::strcmp(mode, "axial"));
|
|
if (multi) o.t_idx({ !std::strcmp(mode, "imrope") ? 3 : nc, M }, M, -1, 0);
|
|
else o.t_idx({ M }, 0, -1, 0); /* positions */
|
|
return o.finish("rope", operand, out);
|
|
}
|
|
|
|
int ref_qk_norm_rope_shape(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);
|
|
const int64_t nh = gi(p, n_p, "n_head", 0);
|
|
const int64_t nkv = gi(p, n_p, "n_head_kv", nh);
|
|
if (M <= 0 || hd <= 0 || nh <= 0) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
/* The contiguous fused [q|k|v] row, as for `rope`. (libr4d's `qk_norm_rope_gate` reads an
|
|
* interleaved q/gate view that parameters cannot describe; opshapes.h declines it.) */
|
|
o.t(D, { M, (nh + 2 * nkv) * hd }); /* qkv, INOUT */
|
|
/* `blocks` takes positions [C, M], component-major, C one past the largest `comps` entry (the
|
|
* section count without `comps`); `axial` one component a section. Without a rotation
|
|
* (`rotary_dim` 0) the positions are not passed. */
|
|
const char* mode = gs(p, n_p, "mode");
|
|
const bool blocks = mode && !std::strcmp(mode, "blocks");
|
|
const bool axial = mode && !std::strcmp(mode, "axial");
|
|
if (gi(p, n_p, "rotary_dim", hd) == 0) {
|
|
o.skip(); /* positions */
|
|
} else if (blocks || axial) {
|
|
int64_t nc = 0;
|
|
for (const char* q = gs(p, n_p, "sections"); q && *q; ) {
|
|
char* e = nullptr;
|
|
(void)std::strtoll(q, &e, 10);
|
|
if (e == q) break;
|
|
++nc;
|
|
q = e;
|
|
}
|
|
if (const char* cs = blocks ? gs(p, n_p, "comps") : nullptr) {
|
|
int64_t mx = -1;
|
|
for (const char* q = cs; *q; ) {
|
|
char* e = nullptr;
|
|
const long long x = std::strtoll(q, &e, 10);
|
|
if (e == q) break;
|
|
mx = x > mx ? x : mx;
|
|
q = e;
|
|
}
|
|
nc = mx + 1;
|
|
}
|
|
if (nc <= 0) return RAD_E_UNSUPPORTED;
|
|
o.t_idx({ nc, M }, M, -1, 0); /* positions */
|
|
} else {
|
|
o.t_idx({ M }, 0, -1, 0); /* positions */
|
|
}
|
|
o.t(D, { hd }); /* q_w -- one gain per head channel */
|
|
o.t(D, { hd }); /* k_w */
|
|
return o.finish("qk_norm_rope", operand, out);
|
|
}
|
|
|
|
int ref_rope_table_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0);
|
|
const int64_t rot = gi(p, n_p, "rot", 0);
|
|
if (M <= 0 || rot < 2 || (rot & 1)) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
/* Each position writes its own row of a [max_ctx, rot] plane and max_ctx is not a parameter,
|
|
* so the table is M rows with positions drawn from [0, M). The bound must be explicit:
|
|
* `positions` is the first operand, so the default (previous operand's extent) does not
|
|
* apply. */
|
|
o.t_idx({ M }, M, -1, 0); /* positions */
|
|
o.t(D, { M, rot }); /* cos_sin -- cos in the first half, sin in the second */
|
|
return o.finish("rope_table", operand, out);
|
|
}
|
|
|
|
int ref_rope_rows_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), nh = gi(p, n_p, "n_head", 0),
|
|
hd = gi(p, n_p, "head_dim", 0);
|
|
const int64_t rot = gi(p, n_p, "rot", 0), off = gi(p, n_p, "off", -1);
|
|
if (M <= 0 || nh < 1 || rot < 2 || (rot & 1) || off < 0 || off + rot > hd)
|
|
return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M, nh * hd }); /* x, rotated in place */
|
|
o.t((uint32_t)RAD_F32, { M, rot }); /* cos_sin: a rope_table plane of M rows */
|
|
o.t_idx({ M }, M, -1, 0); /* positions */
|
|
return o.finish("ds41_rope_rows", operand, out);
|
|
}
|
|
|
|
int ref_quant_roundtrip_shape(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), block = gi(p, n_p, "block", 0);
|
|
if (M <= 0 || n < 1 || block < 1 || n % block) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(act_dt(p, n_p), { M, n });
|
|
return o.finish("quant_roundtrip", operand, out);
|
|
}
|
|
|
|
int ref_softmax_pool_shape(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), ratio = gi(p, n_p, "ratio", 0);
|
|
if (M <= 0 || n < 1 || ratio < 1 || ratio > 64) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t((uint32_t)RAD_F32, { M * ratio, 2 * n }); /* stage: values then scores */
|
|
o.t(act_dt(p, n_p), { M, n });
|
|
o.t_idx({ M }, M, -1, 0); /* page, optional: negative skips the entry */
|
|
return o.finish("softmax_pool", operand, out);
|
|
}
|
|
|
|
int ref_attn_sparse_sink_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), H = gi(p, n_p, "heads", 0), Dm = gi(p, n_p, "dim", 0);
|
|
const int64_t win = gi(p, n_p, "window", 0), ratio = gi(p, n_p, "ratio", 0);
|
|
const int64_t topk = gi(p, n_p, "topk", 0);
|
|
if (M <= 0 || H < 1 || Dm < 2 || win < 1 || ratio < 0 ||
|
|
(ratio > 0 && (topk < 1 || Dm % ratio)))
|
|
return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
/* latent_format: an E4M3 entry is its rad_kv_fp8.h bytes */
|
|
const RadParam* lfp = rad_param_find(p, n_p, "latent_format");
|
|
const int lf = rad_kvf_parse(lfp && lfp->kind == RAD_P_STR ? lfp->sval : nullptr);
|
|
const int64_t cslots = lf > 0 ? rad_kvf_bytes(lf, Dm) : Dm;
|
|
if (lf < 0 || cslots < 1 || (ratio > 0 && cslots % ratio)) return RAD_E_UNSUPPORTED;
|
|
/* One sequence; window blocks of 4 slots covering every position a row may hold. */
|
|
const int64_t bs = 4, nb = (M + bs - 1) / bs + 1, ncb = M + 1;
|
|
Opd o;
|
|
o.t(D, { M, H * Dm }); /* q */
|
|
o.t(D, { nb, 1, bs, Dm }); /* window cache */
|
|
o.t_idx({ 1, nb }, nb, -1, 0); /* window block table */
|
|
o.t_idx({ 1 }, M + 1, -1, 0); /* window tokens held from the table's start */
|
|
if (ratio > 0) {
|
|
o.t(lf > 0 ? (uint32_t)RAD_F8E4M3 : D,
|
|
{ ncb, 1, ratio, cslots / ratio }); /* compressed cache: an entry a block */
|
|
o.t_idx({ 1, ncb }, ncb, -1, 0); /* compressed block table */
|
|
o.t_idx({ M, topk }, ncb, -1, 0); /* selected compressed positions */
|
|
} else {
|
|
o.skip(); o.skip(); o.skip();
|
|
}
|
|
o.t_idx({ M }, M, -1, 0); /* positions */
|
|
o.t_idx({ 2 }, M, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu_seqlens */
|
|
o.t((uint32_t)RAD_F32, { H }); /* sinks */
|
|
o.t(D, { M, H * Dm }); /* out */
|
|
return o.finish("attn_sparse_sink", operand, out);
|
|
}
|
|
|
|
int ref_index_score_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), H = gi(p, n_p, "heads", 0), Dm = gi(p, n_p, "dim", 0);
|
|
const int64_t ratio = gi(p, n_p, "ratio", 0), C = gi(p, n_p, "width", 0);
|
|
if (M <= 0 || H < 1 || Dm < 1 || ratio < 1 || Dm % ratio || C < 1) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
const int64_t ncb = C + 1;
|
|
/* key_format: an E4M3 entry is its rad_kv_fp8.h bytes */
|
|
const RadParam* kfp = rad_param_find(p, n_p, "key_format");
|
|
const int kf = rad_kvf_parse(kfp && kfp->kind == RAD_P_STR ? kfp->sval : nullptr);
|
|
const int64_t kslots = kf > 0 ? rad_kvf_bytes(kf, Dm) : Dm;
|
|
if (kf < 0 || kslots < 1 || kslots % ratio) return RAD_E_UNSUPPORTED;
|
|
/* A run (row_part p) scores rows [p M, p M + M) of a step that holds them. */
|
|
const int64_t part = gi(p, n_p, "row_part", 0), Mq = part > 0 ? (part + 1) * M : M;
|
|
Opd o;
|
|
o.t(D, { Mq, H * Dm }); /* q */
|
|
o.t(D, { Mq, H }); /* head weights */
|
|
o.t(kf > 0 ? (uint32_t)RAD_F8E4M3 : D,
|
|
{ ncb, 1, ratio, kslots / ratio }); /* index cache: an entry a block */
|
|
o.t_idx({ 1, ncb }, ncb, -1, 0); /* block table */
|
|
o.t_idx({ Mq }, Mq, -1, 0); /* positions */
|
|
o.t_idx({ 2 }, Mq, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu_seqlens */
|
|
o.t((uint32_t)RAD_F32, { M, C }); /* score */
|
|
return o.finish("index_score", operand, out);
|
|
}
|
|
|
|
int ref_index_candidates_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), C = gi(p, n_p, "width", 0),
|
|
block = gi(p, n_p, "block", 0);
|
|
if (M <= 0 || C < 1 || block < 1 || gi(p, n_p, "topk_blocks", 0) < 1 ||
|
|
gi(p, n_p, "ratio", 0) < 1)
|
|
return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t((uint32_t)RAD_F32, { M, C }); /* score */
|
|
o.t_idx({ M }, M, -1, 0); /* positions */
|
|
o.t((uint32_t)RAD_I32, { M, ref_keep_words(C, block) }); /* keep: a bit a block */
|
|
return o.finish("index_candidates", operand, out);
|
|
}
|
|
|
|
int ref_index_topk_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), C = gi(p, n_p, "width", 0), k = gi(p, n_p, "topk", 0);
|
|
const int64_t block = gi(p, n_p, "block", 0);
|
|
if (M <= 0 || C < 1 || k < 1 || gi(p, n_p, "ratio", 0) < 1) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t((uint32_t)RAD_F32, { M, C }); /* score */
|
|
/* keep, optional: with `block`, index_candidates' bits (any word is a valid mask) */
|
|
if (block > 0) o.t_idx({ M, ref_keep_words(C, block) }, 0x7fffffff, -1, 0);
|
|
else o.skip();
|
|
o.t_idx({ M }, M, -1, 0); /* positions */
|
|
o.t((uint32_t)RAD_I32, { M, k }); /* sel */
|
|
return o.finish("index_topk", operand, out);
|
|
}
|
|
|
|
int ref_entry_store_shape(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), bs = gi(p, n_p, "block_size", 0);
|
|
if (M <= 0 || n < 1 || bs < 1 || n % bs) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
/* format: an E4M3 entry is its rad_kv_fp8.h bytes */
|
|
const RadParam* fp = rad_param_find(p, n_p, "format");
|
|
const int f = rad_kvf_parse(fp && fp->kind == RAD_P_STR ? fp->sval : nullptr);
|
|
const int64_t ne = f > 0 ? rad_kvf_bytes(f, n) : n;
|
|
if (f < 0 || ne < 1 || ne % bs) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x */
|
|
o.t_idx({ M }, M, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE); /* page: distinct blocks */
|
|
o.t(f > 0 ? (uint32_t)RAD_F8E4M3 : D, { M, 1, bs, ne / bs }); /* cache */
|
|
return o.finish("entry_store", operand, out);
|
|
}
|
|
|
|
#ifdef REF_HAVE_GGML
|
|
extern "C" int64_t ref_ggml_block(const char* type);
|
|
int ref_gemm_nt_ggml_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const RadParam* t = rad_param_find(p, n_p, "type");
|
|
const int64_t blk = ref_ggml_block(t && t->kind == RAD_P_STR ? t->sval : nullptr);
|
|
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 (blk <= 0 || M <= 0 || N <= 0 || K <= 0 || K % blk) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M, K }); /* a */
|
|
o.t((uint32_t)RAD_F32, { N, K }); /* w, the logical form */
|
|
o.t(D, { M, N }); /* y */
|
|
return o.finish("gemm_nt_ggml", operand, out);
|
|
}
|
|
#endif
|
|
|
|
int ref_token_map_shape(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);
|
|
if (M <= 0 || nv < 1) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t_idx({ M }, nv, -1, 0); /* ids */
|
|
o.t_idx({ nv }, nv, -1, 0); /* table */
|
|
o.t((uint32_t)RAD_I32, { M }); /* mapped */
|
|
return o.finish("token_map", operand, out);
|
|
}
|
|
|
|
int ref_stream_gate_add_shape(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), hc = gi(p, n_p, "hc", 0);
|
|
if (M <= 0 || n < 1 || hc < 1 || hc > 16) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M, hc * n }); /* h */
|
|
o.t(D, { M, hc * n }); /* k */
|
|
o.t(D, { M, n }); /* v */
|
|
o.t((uint32_t)RAD_F32, { hc * n }); /* w */
|
|
o.t(D, { M, hc * n }); /* out */
|
|
o.t((uint32_t)RAD_I32, { M }); /* mask, absent for a text batch */
|
|
return o.finish("stream_gate_add", operand, out);
|
|
}
|
|
|
|
int ref_qsa_block_key_shape(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);
|
|
const int64_t r = gi(p, n_p, "ratio", 0);
|
|
if (M <= 0 || n <= 0 || r <= 0) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M * r, n }); /* k -- `ratio` rows a block */
|
|
o.t((uint32_t)RAD_F32, { n }); /* w -- the norm gain */
|
|
/* page: absent, so the write is contiguous; a drawn page list could repeat and race. */
|
|
o.skip();
|
|
o.t_idx({ M }, M * r, -1, 0); /* pos -- each block's first-token position */
|
|
o.t(D, { M, n }); /* out */
|
|
return o.finish("qsa_block_key", operand, out);
|
|
}
|
|
|
|
/* The indexer's other four ops. The fill laws have no per-sequence position run, so the
|
|
* descriptions pick batches whose answers are defined and race-free on every implementation:
|
|
* `cu` splits the M rows evenly over S sequences; the tail store gives every row a sequence and a
|
|
* distinct slot of its own (two rows of one sequence could share a ring row); the work list puts
|
|
* every row of a sequence at position ratio - 1, so each sequence completes block 0 from one key
|
|
* of the batch and ratio - 1 of the tail. */
|
|
static int64_t qsa_seqs(const RadParam* p, int n_p, int64_t M) {
|
|
const int64_t s = gi(p, n_p, "seqs", 1);
|
|
return std::max<int64_t>(1, std::min(s, M));
|
|
}
|
|
|
|
int ref_qsa_score_shape(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 heads = gi(p, n_p, "heads", 0), blocks = gi(p, n_p, "blocks", 0);
|
|
if (M <= 0 || n <= 0 || heads <= 0 || blocks <= 0) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
const int64_t S = qsa_seqs(p, n_p, M), pages = S * blocks;
|
|
Opd o;
|
|
o.t(D, { M, heads * n }); /* q */
|
|
o.t(D, { pages, n }); /* bk -- one block key a page */
|
|
o.t_idx({ S, blocks }, pages, -1, 0); /* bt -- page ids */
|
|
o.t_idx({ S + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu */
|
|
o.t_idx({ S }, blocks + 1, -1, 0); /* nc -- 0 .. blocks */
|
|
o.t((uint32_t)RAD_F32, { M, blocks }); /* score */
|
|
return o.finish("qsa_score", operand, out);
|
|
}
|
|
|
|
int ref_qsa_select_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), blocks = gi(p, n_p, "blocks", 0);
|
|
const int64_t topk = gi(p, n_p, "topk", 0), ratio = gi(p, n_p, "ratio", 0);
|
|
if (M <= 0 || blocks <= 0 || topk <= 0 || ratio <= 0) return RAD_E_UNSUPPORTED;
|
|
const int64_t S = qsa_seqs(p, n_p, M);
|
|
Opd o;
|
|
o.t((uint32_t)RAD_F32, { M, blocks }); /* score */
|
|
o.t_idx({ S, blocks }, S * blocks, -1, 0); /* bt -- page ids */
|
|
o.t_idx({ S }, blocks + 1, -1, 0); /* nc -- 0 .. blocks */
|
|
o.t_idx({ M }, (blocks + 1) * ratio, -1, 0); /* pos */
|
|
o.t_idx({ S + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu */
|
|
o.t((uint32_t)RAD_I32, { M, topk + 1 }); /* sel */
|
|
o.t((uint32_t)RAD_I32, { M }); /* nsel */
|
|
o.t((uint32_t)RAD_I32, { M }); /* seqused */
|
|
return o.finish("qsa_select", operand, out);
|
|
}
|
|
|
|
int ref_qsa_tail_store_shape(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 ratio = gi(p, n_p, "ratio", 0), ring = gi(p, n_p, "ring", ratio);
|
|
if (M <= 0 || n <= 0 || ratio <= 0 || ring < ratio) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M, n }); /* k */
|
|
o.t_idx({ M }, 1 << 16, -1, 0); /* pos */
|
|
o.t_idx({ M + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu -- a row a sequence */
|
|
o.t(D, { M, 1, ring, n }); /* state */
|
|
o.t_idx({ M, 1 }, M, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE); /* sidx -- a slot each */
|
|
return o.finish("qsa_tail_store", operand, out);
|
|
}
|
|
|
|
int ref_qsa_work_shape(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 ratio = gi(p, n_p, "ratio", 0), ring = gi(p, n_p, "ring", ratio);
|
|
const int64_t work = gi(p, n_p, "work", 0);
|
|
if (M <= 0 || n <= 0 || ratio <= 0 || ring < ratio || work <= 0) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
const int64_t S = std::min(qsa_seqs(p, n_p, M), work);
|
|
Opd o;
|
|
o.t(D, { M, n }); /* k */
|
|
o.t_idx({ M }, 0, ratio - 1, 0); /* pos -- see above */
|
|
o.t_idx({ S + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu */
|
|
o.t(D, { S, 1, ring, n }); /* state -- the tail */
|
|
o.t_idx({ S, 1 }, S, -1, 0); /* sidx */
|
|
o.t_idx({ S, 2 }, 1 << 16, -1, 0); /* bt -- block-key pages */
|
|
o.skip(); /* rope */
|
|
o.t(D, { work * ratio, n }); /* stage */
|
|
o.t((uint32_t)RAD_I32, { work }); /* page */
|
|
o.t((uint32_t)RAD_I32, { work }); /* bpos */
|
|
o.t((uint32_t)RAD_I32, { S }); /* nc */
|
|
return o.finish("qsa_work", operand, out);
|
|
}
|
|
|
|
/* The residency map and the two byte planes. `sel` is block ids, -1 past `nsel`. A described map
|
|
* can only be all-resident (no draw expresses -1), so a tool's cold call exercises the epoch, the
|
|
* rewrite and the bookkeeping, never a miss, eviction or copy; those cases are built by hand in
|
|
* tests/ref_test.cpp and in libavx's hand cases. */
|
|
int ref_kv_resolve_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), blocks = gi(p, n_p, "blocks", 0);
|
|
const int64_t slots = gi(p, n_p, "slots", 0), pitch = gi(p, n_p, "sel_pitch", 0);
|
|
const int64_t bbytes = gi(p, n_p, "block_bytes", 0);
|
|
if (M <= 0 || blocks <= 0 || slots <= 0 || pitch <= 0 || bbytes <= 0) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t_idx({ M, pitch }, blocks, -1, 0); /* sel -- one block id an entry */
|
|
o.t_idx({ M }, pitch + 1, -1, 0); /* nsel -- 0 .. sel_pitch */
|
|
o.t_idx({ blocks }, slots, -1, 0); /* res -- every block resident, no -2 */
|
|
o.t_idx({ slots }, blocks, -1, 0); /* slot_block -- the reverse map */
|
|
o.t_idx({ slots, 2 }, 2, 0, 0); /* slot_state -- cold: epoch 0, no reference */
|
|
o.t_idx({ 16 }, 2, 0, 0); /* ctl -- cold: epoch 0, hand 0 */
|
|
o.t((uint32_t)RAD_U8, { blocks, bbytes }); /* host -- the layer's mirror, a block a row */
|
|
o.t((uint32_t)RAD_U8, { slots, bbytes }); /* kv_cache -- the layer's ring, a block a row */
|
|
return o.finish("kv_resolve", operand, out);
|
|
}
|
|
|
|
/* `delta` is described as the whole [T, 2 * taps * NG] coefficient projection, of which the op
|
|
* reads the first taps * NG columns at the row pitch: the form the architecture issues, and the
|
|
* pitch libr4d requires. */
|
|
int ref_dflash_conv_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t T = gi(p, n_p, "T", 0), H = gi(p, n_p, "hidden_size", 0);
|
|
const int64_t taps = gi(p, n_p, "taps", 0), NG = gi(p, n_p, "NG", 0);
|
|
if (T <= 0 || H <= 0 || taps <= 0 || NG <= 0) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { T, H }); /* x */
|
|
o.t(D, { T, 2 * taps * NG }); /* delta */
|
|
o.t(D, { taps, H }); /* base */
|
|
o.t(D, { T, H }); /* out */
|
|
return o.finish("dflash_conv", operand, out);
|
|
}
|
|
|
|
/* ================================================================== mixture of experts */
|
|
|
|
int ref_scale_rows_shape(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);
|
|
if (M <= 0 || n <= 0) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x */
|
|
o.t(D, { M }); /* s -- one scalar a row */
|
|
o.t(D, { M, n }); /* add -- optional, described to exercise the
|
|
* fused form */
|
|
o.t(D, { M, n }); /* y */
|
|
return o.finish("scale_rows", operand, out);
|
|
}
|
|
|
|
int ref_l2_clip_shape(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);
|
|
if (M <= 0 || n <= 0) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x */
|
|
o.t(D, { n }); /* w -- the optional scale, one per column */
|
|
o.t(RAD_I32, { M }); /* positions -- optional, one per row */
|
|
o.t(D, { M, n }); /* y */
|
|
return o.finish("l2_clip", operand, out);
|
|
}
|
|
|
|
int ref_router_topk_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0);
|
|
const int64_t ne = gi(p, n_p, "n_expert", 0);
|
|
const int64_t k = gi(p, n_p, "top_k", 0);
|
|
if (M <= 0 || ne <= 0 || k <= 0 || k > ne) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M, ne }); /* logits */
|
|
o.t((uint32_t)RAD_I32, { M, k }); /* expert_ids */
|
|
o.t(D, { M, k }); /* expert_w */
|
|
return o.finish("router_topk", operand, out);
|
|
}
|
|
|
|
int ref_moe_scatter_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0);
|
|
const int64_t ne = gi(p, n_p, "n_expert", 0);
|
|
const int64_t k = gi(p, n_p, "top_k", 0);
|
|
if (M <= 0 || ne <= 0 || k <= 0) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
/* Expert ids draw over [0, n_expert), exercising the sort rather than the dropping of
|
|
* out-of-range slots. */
|
|
o.t_idx({ M, k }, ne, -1, 0); /* expert_ids */
|
|
o.t((uint32_t)RAD_I32, { M * k }); /* sorted_tok -- token*top_k + slot, by expert */
|
|
o.t((uint32_t)RAD_I32, { ne + 1 }); /* expert_offset */
|
|
o.t((uint32_t)RAD_I32, { ne }); /* expert_count */
|
|
return o.finish("moe_scatter", operand, out);
|
|
}
|
|
|
|
/* The two shape lists concatenated, with `expert_ids` named ONCE as an output -- it is what the
|
|
* selection writes and what the sort reads. The sort's three outputs are sized by THIS RANK'S
|
|
* SLICE (`n_local`, absent means the slice is the router), exactly as in libr4d's shape. */
|
|
int ref_router_topk_scatter_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0);
|
|
const int64_t ne = gi(p, n_p, "n_expert", 0);
|
|
const int64_t k = gi(p, n_p, "top_k", 0);
|
|
const int64_t nl = gi(p, n_p, "n_local", ne);
|
|
if (M <= 0 || ne <= 0 || k <= 0 || k > ne || nl <= 0 || nl > ne) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M, ne }); /* logits */
|
|
o.t((uint32_t)RAD_I32, { M, k }); /* expert_ids */
|
|
o.t(D, { M, k }); /* expert_w */
|
|
o.t((uint32_t)RAD_I32, { M * k }); /* sorted_tok */
|
|
o.t((uint32_t)RAD_I32, { nl + 1 }); /* expert_offset */
|
|
o.t((uint32_t)RAD_I32, { nl }); /* expert_count */
|
|
return o.finish("router_topk_scatter", operand, out);
|
|
}
|
|
|
|
/* The router GEMM's list, the pair's list, and -- with the gate present -- the gate's one-column
|
|
* list, with `logits` named once as the GEMM's output. `gate_b`/`gate` come together or not: the
|
|
* operand count carries it, as in libr4d's shape. */
|
|
int ref_router_gemm_topk_scatter_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0);
|
|
const int64_t K = gi(p, n_p, "K", 0);
|
|
const int64_t ne = gi(p, n_p, "n_expert", 0);
|
|
const int64_t k = gi(p, n_p, "top_k", 0);
|
|
const int64_t nl = gi(p, n_p, "n_local", ne);
|
|
if (M <= 0 || K <= 0 || ne <= 0 || k <= 0 || k > ne || nl <= 0 || nl > ne)
|
|
return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M, K }); /* a */
|
|
o.t(D, { ne, K }); /* b: the router */
|
|
o.t(D, { 1, K }); /* gate_b: the shared expert's gate */
|
|
o.t(D, { M, ne }); /* logits */
|
|
o.t((uint32_t)RAD_I32, { M, k }); /* expert_ids */
|
|
o.t(D, { M, k }); /* expert_w */
|
|
o.t((uint32_t)RAD_I32, { M * k }); /* sorted_tok */
|
|
o.t((uint32_t)RAD_I32, { nl + 1 }); /* expert_offset */
|
|
o.t((uint32_t)RAD_I32, { nl }); /* expert_count */
|
|
o.t(D, { M, 1 }); /* gate */
|
|
return o.finish("router_gemm_topk_scatter", operand, out);
|
|
}
|
|
|
|
/* `a_order` decides the activation's row count: one per token, or one per (token, slot) already
|
|
* in the scatter's order. A described case that got this wrong would build the operand at the
|
|
* extent the WRONG reading wants, and the fault it names would go unseen. */
|
|
static inline bool sorted_a_order(const RadParam* p, int n_p) {
|
|
const char* s = gs(p, n_p, "a_order");
|
|
return s && std::strcmp(s, "sorted") == 0;
|
|
}
|
|
|
|
int ref_moe_gemm_shape(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), K = gi(p, n_p, "K", 0);
|
|
const int64_t ne = gi(p, n_p, "n_expert", 0);
|
|
const int64_t k = gi(p, n_p, "top_k", 0);
|
|
if (M <= 0 || N <= 0 || K <= 0 || ne <= 0 || k <= 0) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
const int64_t AR = sorted_a_order(p, n_p) ? M * k : M;
|
|
o.t(D, { AR, K }); /* a -- see `a_order` */
|
|
o.t(D, { ne, N, K }); /* w */
|
|
/* A valid routing, as moe_scatter produces it: `sorted_tok` a permutation of [0, M*top_k) and
|
|
* `expert_offset` a non-decreasing prefix array from 0 to M*top_k. A random offset array is a
|
|
* malformed histogram that implementations searching it differently (binary against linear)
|
|
* may read differently without either being wrong. */
|
|
o.t_idx({ M * k }, M * k, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE); /* sorted_tok */
|
|
o.t_idx({ ne + 1 }, M * k + 1, -1, (uint32_t)RAD_OPD_F_IDX_PREFIX); /* expert_offset */
|
|
o.t(D, { M * k, N }); /* y -- in sorted order */
|
|
return o.finish("moe_gemm", operand, out);
|
|
}
|
|
|
|
#ifdef REF_HAVE_GGML
|
|
/* moe_gemm's operands with each expert's weight in gemm_nt_ggml's logical form. */
|
|
int ref_moe_gemm_ggml_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const RadParam* t = rad_param_find(p, n_p, "type");
|
|
const int64_t blk = ref_ggml_block(t && t->kind == RAD_P_STR ? t->sval : nullptr);
|
|
const int64_t M = gi(p, n_p, "M", 0);
|
|
const int64_t N = gi(p, n_p, "N", 0), K = gi(p, n_p, "K", 0);
|
|
const int64_t ne = gi(p, n_p, "n_expert", 0);
|
|
const int64_t k = gi(p, n_p, "top_k", 0);
|
|
if (blk <= 0 || M <= 0 || N <= 0 || K <= 0 || K % blk || ne <= 0 || k <= 0)
|
|
return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
const int64_t AR = sorted_a_order(p, n_p) ? M * k : M;
|
|
o.t(D, { AR, K }); /* a -- see `a_order` */
|
|
o.t((uint32_t)RAD_F32, { ne, N, K }); /* w, the logical form */
|
|
o.t_idx({ M * k }, M * k, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE); /* sorted_tok */
|
|
o.t_idx({ ne + 1 }, M * k + 1, -1, (uint32_t)RAD_OPD_F_IDX_PREFIX); /* expert_offset */
|
|
o.t(D, { M * k, N }); /* y -- in sorted order */
|
|
return o.finish("moe_gemm_ggml", operand, out);
|
|
}
|
|
|
|
/* logits_gemm's and embed_lookup's operands with the table in gemm_nt_ggml's logical form. */
|
|
static int64_t ggml_blk_of(const RadParam* p, int n_p) {
|
|
const RadParam* t = rad_param_find(p, n_p, "type");
|
|
return ref_ggml_block(t && t->kind == RAD_P_STR ? t->sval : nullptr);
|
|
}
|
|
int ref_logits_gemm_ggml_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t blk = ggml_blk_of(p, n_p);
|
|
const int64_t M = gi(p, n_p, "M", 0);
|
|
const int64_t nv = gi(p, n_p, "n_vocab", 0), ne = gi(p, n_p, "n_embd", 0);
|
|
if (blk <= 0 || M <= 0 || nv <= 0 || ne <= 0 || ne % blk) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M, ne }); /* x */
|
|
o.t((uint32_t)RAD_F32, { nv, ne }); /* lm_head, the logical form */
|
|
o.t((uint32_t)RAD_F32, { M, nv }); /* logits */
|
|
return o.finish("logits_gemm_ggml", operand, out);
|
|
}
|
|
int ref_embed_lookup_ggml_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t blk = ggml_blk_of(p, n_p);
|
|
const int64_t M = gi(p, n_p, "M", 0);
|
|
const int64_t ne = gi(p, n_p, "n_embd", 0), nv = gi(p, n_p, "n_vocab", 0);
|
|
const int64_t vo = gi(p, n_p, "vocab_offset", 0);
|
|
if (blk <= 0 || M <= 0 || ne <= 0 || nv <= 0 || vo < 0 || ne % blk) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t_idx({ M }, vo + nv, -1, 0); /* tokens */
|
|
o.t((uint32_t)RAD_F32, { nv, ne }); /* wte, the logical form */
|
|
o.t(D, { M, ne }); /* x */
|
|
return o.finish("embed_lookup_ggml", operand, out);
|
|
}
|
|
#endif
|
|
|
|
/* The weight scale grid `dtype` names, as weight rows and K elements per scale. Block-fp8 experts
|
|
* (fp8a8) carry the checkpoint's one scale per 128x128 tile (`group` = 128). The 4-bit formats
|
|
* (docs/MOE-W4.md) carry one per row per K group: all of `group` for w4a8, w4a8h and w4nla8h, half
|
|
* for w4nl64a8h, w5nl64a8h and w6nl64a8h, a quarter for w4nl32a8h. The `a8i8` names are the
|
|
* four-bit formats against an int8 activation and `i8a8` / `i8a8h` int8 experts against one, a
|
|
* scale per row per `group`. These are the served format names (the architecture's MoE block,
|
|
* kServed and kServed8). Any other dtype returns { 0, 0 } and the description declines rather
|
|
* than guess a grid.
|
|
*
|
|
* The grid matters beyond extents: re-laid weights are inverted into the tensor described here
|
|
* before the oracle reads them (core/runtime/oracle.cpp), so the wrong grid leaves the scales
|
|
* nothing to invert into. The arithmetic (ref_gemm.h) derives the group from the extents. */
|
|
struct MoeScaleGrid { int64_t rows, k; };
|
|
static MoeScaleGrid moe_scale_grid(const RadParam* p, int n_p, int64_t g) {
|
|
const char* d = gs(p, n_p, "dtype");
|
|
auto is = [&](const char* s) { return d && std::strcmp(d, s) == 0; };
|
|
if (is("fp8a8") || is("fp8a8f32")) return { g, g };
|
|
if (is("mxfp4a8")) return { 1, 32 };
|
|
if (is("w4a8") || is("w4a8h") || is("w4nla8h")) return { 1, g };
|
|
if (is("w4nl64a8h") || is("w5nl64a8h") || is("w6nl64a8h")) return { 1, g / 2 };
|
|
if (is("w4nl32a8h")) return { 1, g / 4 };
|
|
if (is("i8a8") || is("i8a8h") || is("w4a8i8") || is("w4a8i8h")) return { 1, g };
|
|
if (is("w4nl64a8i8h")) return { 1, g / 2 };
|
|
if (is("w4nl32a8i8h")) return { 1, g / 4 };
|
|
return { 0, 0 };
|
|
}
|
|
/* The operands' code dtypes `dtype` names: an int8 activation for the `a8i8` and `i8a8` names and
|
|
* int8 experts for the latter, E4M3 otherwise. A four-bit expert is described at the E4M3 its
|
|
* logical values are exact in, whichever activation it meets. */
|
|
struct MoeCodes { uint32_t a, w; };
|
|
static MoeCodes moe_codes(const RadParam* p, int n_p) {
|
|
const char* d = gs(p, n_p, "dtype");
|
|
auto is = [&](const char* s) { return d && std::strcmp(d, s) == 0; };
|
|
const bool w8 = is("i8a8") || is("i8a8h");
|
|
const bool a8 = w8 || is("w4a8i8") || is("w4a8i8h") || is("w4nl64a8i8h") || is("w4nl32a8i8h");
|
|
return { a8 ? (uint32_t)RAD_I8 : (uint32_t)RAD_F8E4M3,
|
|
w8 ? (uint32_t)RAD_I8 : (uint32_t)RAD_F8E4M3 };
|
|
}
|
|
|
|
/* The quantised arm: the same planes plus two scale streams, the activation's per row per K group
|
|
* and the weight's one plane per expert on the grid above ([ceil(N/g), ceil(K/g)] for block fp8).
|
|
* `w` is described stacked [ne, N, K], since a fixture cannot hold a pointer table; expert_entry()
|
|
* reads both forms. */
|
|
int ref_moe_gemm_q_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
if (const char* dk = gs(p, n_p, "dtype")) {
|
|
if (!std::strcmp(dk, "w4a8")) return ref_moe_gemm_w4a8_shape(p, n_p, operand, out);
|
|
if (!std::strcmp(dk, "w4a4")) return ref_moe_gemm_w4a4_shape(p, n_p, operand, out);
|
|
}
|
|
const int64_t M = gi(p, n_p, "M", 0);
|
|
const int64_t N0 = gi(p, n_p, "N", 0), K0 = gi(p, n_p, "K", 0);
|
|
const int64_t N1 = gi(p, n_p, "N_odd", N0), K1 = gi(p, n_p, "K_odd", K0);
|
|
const int64_t ne = gi(p, n_p, "n_expert", 0);
|
|
const int64_t k = gi(p, n_p, "top_k", 0);
|
|
int64_t g = gi(p, n_p, "group", 0);
|
|
if (M <= 0 || N0 <= 0 || K0 <= 0 || N1 <= 0 || K1 <= 0 || ne <= 0 || k <= 0)
|
|
return RAD_E_UNSUPPORTED;
|
|
/* Output width and activation K are the larger class's; the tables hold even and odd
|
|
* experts. */
|
|
const int64_t N = N0 > N1 ? N0 : N1, K = K0 > K1 ? K0 : K1;
|
|
const int64_t ne0 = (ne + 1) / 2, ne1 = ne / 2;
|
|
if (g <= 0) g = K;
|
|
const MoeScaleGrid sg = moe_scale_grid(p, n_p, g);
|
|
if (sg.rows <= 0 || sg.k <= 0) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
const int64_t AR = sorted_a_order(p, n_p) ? M * k : M;
|
|
const char* kind = gs(p, n_p, "dtype");
|
|
const bool mx = kind && !std::strcmp(kind, "mxfp4a8");
|
|
const auto SD = kind && !std::strcmp(kind, "fp8a8f32") ? (uint32_t)RAD_F32 : D;
|
|
const MoeCodes cd = moe_codes(p, n_p);
|
|
/* The K one activation scale covers: `a_group` where stated, else `group`. */
|
|
const int64_t ag = gi(p, n_p, "a_group", g) > 0 ? gi(p, n_p, "a_group", g) : g;
|
|
o.t(cd.a, { AR, K }); /* a -- E4M3 or int8 codes, see `a_order` */
|
|
o.t((uint32_t)RAD_F32, { AR, (K + ag - 1) / ag }); /* a_scale */
|
|
/* w -- the even experts; mxfp4a8 carries e2m1 codes with an E8M0 byte per (row, 32 of K) */
|
|
o.t(mx ? (uint32_t)RAD_FP4E2M1 : cd.w, { ne0, N0, K0 });
|
|
if (mx) o.t_idx_dt(RAD_U8, { ne0, N0, (K0 + 31) / 32 }, 0, 127, 0); /* w_scale */
|
|
else o.t(SD, { ne0, (N0 + sg.rows - 1) / sg.rows, (K0 + sg.k - 1) / sg.k });
|
|
o.t_idx({ M * k }, M * k, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE); /* sorted_tok */
|
|
o.t_idx({ ne + 1 }, M * k + 1, -1, (uint32_t)RAD_OPD_F_IDX_PREFIX); /* expert_offset */
|
|
/* w_odd -- the odd experts */
|
|
o.t(mx ? (uint32_t)RAD_FP4E2M1 : cd.w, { ne1, N1, K1 });
|
|
if (mx) o.t_idx_dt(RAD_U8, { ne1, N1, (K1 + 31) / 32 }, 0, 127, 0); /* w_odd_scale */
|
|
else o.t(SD, { ne1, (N1 + sg.rows - 1) / sg.rows, (K1 + sg.k - 1) / sg.k });
|
|
o.t(D, { M * k, N }); /* y -- in sorted order */
|
|
o.skip(); /* split -- optional */
|
|
return o.finish("moe_gemm_q", operand, out);
|
|
}
|
|
|
|
/* The four-class form: class q at [N_q, K_q] (default class 0's) holds experts e % 4 == q,
|
|
* ceil((n - q) / 4) of them; `y` follows the last class. */
|
|
int ref_moe_gemm_q_mod4_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0);
|
|
const int64_t N0 = gi(p, n_p, "N", 0), K0 = gi(p, n_p, "K", 0);
|
|
const int64_t Nq[4] = { N0, gi(p, n_p, "N_1", N0), gi(p, n_p, "N_2", N0),
|
|
gi(p, n_p, "N_3", N0) };
|
|
const int64_t Kq[4] = { K0, gi(p, n_p, "K_1", K0), gi(p, n_p, "K_2", K0),
|
|
gi(p, n_p, "K_3", K0) };
|
|
const int64_t ne = gi(p, n_p, "n_expert", 0);
|
|
const int64_t k = gi(p, n_p, "top_k", 0);
|
|
int64_t g = gi(p, n_p, "group", 0);
|
|
if (M <= 0 || ne <= 0 || k <= 0) return RAD_E_UNSUPPORTED;
|
|
int64_t N = 0, K = 0;
|
|
for (int q = 0; q < 4; ++q) {
|
|
if (Nq[q] <= 0 || Kq[q] <= 0) return RAD_E_UNSUPPORTED;
|
|
if (Nq[q] > N) N = Nq[q];
|
|
if (Kq[q] > K) K = Kq[q];
|
|
}
|
|
if (g <= 0) g = K;
|
|
const MoeScaleGrid sg = moe_scale_grid(p, n_p, g);
|
|
if (sg.rows <= 0 || sg.k <= 0) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
const int64_t AR = sorted_a_order(p, n_p) ? M * k : M;
|
|
const char* kind = gs(p, n_p, "dtype");
|
|
const bool mx = kind && !std::strcmp(kind, "mxfp4a8");
|
|
const MoeCodes cd = moe_codes(p, n_p);
|
|
const int64_t ag = gi(p, n_p, "a_group", g) > 0 ? gi(p, n_p, "a_group", g) : g;
|
|
auto cls = [&](int q) {
|
|
const int64_t nq = (ne + 3 - q) / 4;
|
|
o.t(mx ? (uint32_t)RAD_FP4E2M1 : cd.w, { nq, Nq[q], Kq[q] });
|
|
if (mx) o.t_idx_dt(RAD_U8, {nq, Nq[q], (Kq[q] + 31) / 32}, 0, 127, 0);
|
|
else o.t(D, { nq, (Nq[q] + sg.rows - 1) / sg.rows, (Kq[q] + sg.k - 1) / sg.k });
|
|
};
|
|
o.t(cd.a, { AR, K }); /* a -- E4M3 or int8 codes, see `a_order` */
|
|
o.t((uint32_t)RAD_F32, { AR, (K + ag - 1) / ag }); /* a_scale */
|
|
cls(0); /* w, w_scale -- class 0 */
|
|
o.t_idx({ M * k }, M * k, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE); /* sorted_tok */
|
|
o.t_idx({ ne + 1 }, M * k + 1, -1, (uint32_t)RAD_OPD_F_IDX_PREFIX); /* expert_offset */
|
|
cls(1);
|
|
cls(2);
|
|
cls(3);
|
|
o.t(D, { M * k, N }); /* y -- in sorted order */
|
|
o.skip(); /* split -- optional */
|
|
return o.finish("moe_gemm_q_mod4", operand, out);
|
|
}
|
|
|
|
/* The grouped int4 GEMM: an int8 activation with an f32 scale per (row, 128 of K), two's-
|
|
* complement int4 experts (nibbles, -8..7) with an f32 scale per (row, 128 of K); one weight
|
|
* table, token-order a only. */
|
|
int ref_moe_gemm_w4a8_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0);
|
|
const int64_t N0 = gi(p, n_p, "N", 0), K0 = gi(p, n_p, "K", 0);
|
|
const int64_t N1 = gi(p, n_p, "N_odd", N0), K1 = gi(p, n_p, "K_odd", K0);
|
|
const int64_t ne = gi(p, n_p, "n_expert", 0);
|
|
const int64_t k = gi(p, n_p, "top_k", 0);
|
|
if (M <= 0 || N0 <= 0 || K0 <= 0 || N1 <= 0 || K1 <= 0 || K0 % 128 || K1 % 128 ||
|
|
ne <= 0 || k <= 0)
|
|
return RAD_E_UNSUPPORTED;
|
|
const int64_t N = N0 > N1 ? N0 : N1, K = K0 > K1 ? K0 : K1;
|
|
const int64_t ne0 = (ne + 1) / 2, ne1 = ne / 2;
|
|
const int64_t AR = sorted_a_order(p, n_p) ? M * k : M;
|
|
Opd o;
|
|
o.t((uint32_t)RAD_I8, { AR, K }); /* a -- int8 (the E4M3 arm is 8-bit too) */
|
|
o.t((uint32_t)RAD_F32, { AR, K / 128 }); /* a_scale */
|
|
o.t((uint32_t)RAD_I4, { ne0, N0, K0 }); /* w -- even experts, nibbles */
|
|
o.t((uint32_t)RAD_BF16, { ne0, N0, K0 / 128 }); /* w_scale */
|
|
o.t_idx({ M * k }, M * k, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE); /* sorted_tok */
|
|
o.t_idx({ ne + 1 }, M * k + 1, -1, (uint32_t)RAD_OPD_F_IDX_PREFIX); /* expert_offset */
|
|
o.t((uint32_t)RAD_I4, { ne1, N1, K1 }); /* w_odd -- odd experts */
|
|
o.t((uint32_t)RAD_BF16, { ne1, N1, K1 / 128 }); /* w_odd_scale */
|
|
o.t((uint32_t)RAD_BF16, { M * k, N }); /* y -- in sorted order */
|
|
o.skip(); /* split -- optional */
|
|
return o.finish("moe_gemm_q", operand, out);
|
|
}
|
|
|
|
/* The same over a two's-complement int4 activation (two nibbles per byte): the w4a8 grid with an
|
|
* I4 a, so the activation row is K/2 bytes. */
|
|
int ref_moe_gemm_w4a4_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0);
|
|
const int64_t N0 = gi(p, n_p, "N", 0), K0 = gi(p, n_p, "K", 0);
|
|
const int64_t N1 = gi(p, n_p, "N_odd", N0), K1 = gi(p, n_p, "K_odd", K0);
|
|
const int64_t ne = gi(p, n_p, "n_expert", 0);
|
|
const int64_t k = gi(p, n_p, "top_k", 0);
|
|
if (M <= 0 || N0 <= 0 || K0 <= 0 || N1 <= 0 || K1 <= 0 || K0 % 128 || K1 % 128 ||
|
|
ne <= 0 || k <= 0)
|
|
return RAD_E_UNSUPPORTED;
|
|
const int64_t N = N0 > N1 ? N0 : N1, K = K0 > K1 ? K0 : K1;
|
|
const int64_t ne0 = (ne + 1) / 2, ne1 = ne / 2;
|
|
const int64_t AR = sorted_a_order(p, n_p) ? M * k : M;
|
|
Opd o;
|
|
o.t((uint32_t)RAD_I4, { AR, K }); /* a -- int4, two nibbles per byte */
|
|
o.t((uint32_t)RAD_F32, { AR, K / 128 }); /* a_scale */
|
|
o.t((uint32_t)RAD_I4, { ne0, N0, K0 }); /* w -- even experts, nibbles */
|
|
o.t((uint32_t)RAD_BF16, { ne0, N0, K0 / 128 }); /* w_scale */
|
|
o.t_idx({ M * k }, M * k, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE); /* sorted_tok */
|
|
o.t_idx({ ne + 1 }, M * k + 1, -1, (uint32_t)RAD_OPD_F_IDX_PREFIX); /* expert_offset */
|
|
o.t((uint32_t)RAD_I4, { ne1, N1, K1 }); /* w_odd -- odd experts */
|
|
o.t((uint32_t)RAD_BF16, { ne1, N1, K1 / 128 }); /* w_odd_scale */
|
|
o.t((uint32_t)RAD_BF16, { M * k, N }); /* y -- in sorted order */
|
|
o.skip(); /* split -- optional */
|
|
return o.finish("moe_gemm_q", operand, out);
|
|
}
|
|
|
|
int ref_moe_gather_shape(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 k = gi(p, n_p, "top_k", 0);
|
|
if (M <= 0 || n <= 0 || k <= 0) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M * k, n }); /* y_expert -- one row per (token, slot) */
|
|
o.t(D, { M, k }); /* expert_w */
|
|
/* Distinct although read-side: gather rebuilds the inverse permutation, so a repeat would let
|
|
* two rows claim one (token, slot) with an implementation-defined winner. A full permutation of
|
|
* [0, M*top_k), as moe_scatter produces. */
|
|
o.t_idx({ M * k }, M * k, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE); /* sorted_tok */
|
|
o.t(D, { M, n }); /* shared -- optional, described anyway */
|
|
o.t(D, { M }); /* shared_gate */
|
|
o.t(D, { M, n }); /* y */
|
|
return o.finish("moe_gather", operand, out);
|
|
}
|
|
|
|
int ref_row_topk_shape(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;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M, N }); /* x */
|
|
o.t((uint32_t)RAD_I32, { M, R }); /* idx */
|
|
o.t((uint32_t)RAD_F32, { M, R }); /* val */
|
|
/* `pairs`, an output, is described to check the packing: an int32 id's bits in the first float
|
|
* slot beside its value, id -1 for an unfilled slot. It must be f32 to hold the value. */
|
|
o.t((uint32_t)RAD_F32, { M, R, 2 }); /* pairs */
|
|
return o.finish("row_topk", operand, out);
|
|
}
|
|
|
|
int ref_row_topk_merge_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), R = gi(p, n_p, "R", 0);
|
|
const int64_t W = gi(p, n_p, "world_size", 1);
|
|
if (M <= 0 || R <= 0 || W <= 0) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
/* A fillable packed pair plane (unlike sample_merge_topk's): as f32 normals the value lanes are
|
|
* proper floats, and the id lanes are float bit patterns read as int32, negative ones
|
|
* dropped as pads and positive ones legal ids that are only compared and copied. No ties
|
|
* occur. */
|
|
o.t((uint32_t)RAD_F32, { M, W * R, 2 }); /* gathered */
|
|
o.t((uint32_t)RAD_I32, { M, R }); /* idx */
|
|
o.t((uint32_t)RAD_F32, { M, R }); /* val */
|
|
return o.finish("row_topk_merge", operand, out);
|
|
}
|
|
|
|
/* The same pair plane, fillable for the same reason as row_topk_merge's. */
|
|
int ref_pair_argmax_shape(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);
|
|
if (M <= 0 || n <= 0) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t((uint32_t)RAD_F32, { M, n, 2 }); /* pairs */
|
|
o.t((uint32_t)RAD_I32, { M }); /* idx */
|
|
o.t((uint32_t)RAD_F32, { M }); /* val */
|
|
return o.finish("pair_argmax", operand, out);
|
|
}
|
|
|
|
|
|
|
|
/* The head is described in f32: an E4M3 code times its bf16 block scale needs more mantissa than
|
|
* bf16 has, and a reference reading rounded weights reorders candidates whose exact scores are
|
|
* close. At a full vocabulary that puts a case over rad-kbench's default --max-bytes, so it is
|
|
* checked only where the budget is raised; r4d_selftest checks the kernels over their stored head
|
|
* either way. Candidate ids are distinct, as row_topk's are: a repeated id is one entry of the
|
|
* kernel's top-R chain but two of a plain sort.
|
|
*
|
|
* The op oracle needs the description too: it un-relays the device weight through
|
|
* RadUnrelayoutFn and dequantises it into this logical form, giving the reference exactly what the
|
|
* kernel saw. ref_logits_gemm_shape describes the same weight. */
|
|
int ref_logit_rerank_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), R = gi(p, n_p, "R", 0);
|
|
const int64_t nv = gi(p, n_p, "n_vocab", 0), ne = gi(p, n_p, "n_embd", 0);
|
|
const int64_t vo = gi(p, n_p, "vocab_off", 0);
|
|
if (M <= 0 || R <= 0 || nv <= 0 || ne <= 0 || vo < 0) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M, ne }); /* x */
|
|
o.t((uint32_t)RAD_F32, { nv, ne }); /* head */
|
|
/* Candidate ids are global, drawn over [0, vo + nv); those below this rank's slice become pads
|
|
* (-1). */
|
|
o.t_idx({ M, R }, vo + nv, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE); /* idx_in */
|
|
o.t_idx({ M, R }, vo + nv, -1, 0); /* idx */
|
|
o.t((uint32_t)RAD_F32, { M, R }); /* val */
|
|
/* f32 [M, R, 2], as row_topk's. */
|
|
o.t((uint32_t)RAD_F32, { M, R, 2 }); /* pairs */
|
|
return o.finish("logit_rerank", operand, out);
|
|
}
|
|
/* ================================================================== the fp8 fusions
|
|
*
|
|
* The unfused pair's operands minus the bf16 intermediate, which `out_bf16` restores (the codes are
|
|
* taken over its value).
|
|
*/
|
|
int ref_rmsnorm_quant_fp8_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);
|
|
const int64_t ng = scale_groups(n, gi(p, n_p, "group", 0));
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x */
|
|
o.t(D, { M, n }); /* residual, inout -- the fused_add_rms_norm form */
|
|
o.t(D, { n }); /* w, the gain */
|
|
o.t((uint32_t)RAD_F8E4M3, { M, n }); /* q */
|
|
o.t((uint32_t)RAD_F32, { M, ng }); /* scale */
|
|
o.t((uint32_t)RAD_BF16, { M, n }); /* out_bf16 -- what the codes were taken over */
|
|
return o.finish("rmsnorm_quant_fp8", operand, out);
|
|
}
|
|
|
|
int ref_gated_quant_fp8_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);
|
|
const int64_t ng = scale_groups(n, gi(p, n_p, "group", 0));
|
|
Opd o;
|
|
o.t(D, { M, 2 * n }); /* gate_up, gate half first */
|
|
o.t((uint32_t)RAD_F8E4M3, { M, n }); /* q */
|
|
o.t((uint32_t)RAD_F32, { M, ng }); /* scale */
|
|
o.t((uint32_t)RAD_BF16, { M, n }); /* out_bf16 */
|
|
return o.finish("gated_quant_fp8", operand, out);
|
|
}
|
|
|
|
/* `N` is the weight's rows, twice the fused width: the quantised output is [M, N/2]. */
|
|
int ref_gemm_nt_q_gated_shape(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);
|
|
const int64_t group = gi(p, n_p, "group", 0);
|
|
const char* q = gs(p, n_p, "dtype");
|
|
const bool a8 = q && std::strcmp(q, "fp8a8") == 0;
|
|
if (!a8 || N <= 0 || K <= 0 || group <= 0 || (N & 1)) return RAD_E_UNSUPPORTED;
|
|
const int64_t n = N / 2;
|
|
const int64_t kb = (K + group - 1) / group;
|
|
const int64_t nb = (N + group - 1) / group;
|
|
const int64_t ng = scale_groups(n, group);
|
|
Opd o;
|
|
o.t((uint32_t)RAD_F8E4M3, { M, K }); /* a */
|
|
o.t((uint32_t)RAD_F32, { M, kb }); /* a_scale */
|
|
o.t((uint32_t)RAD_F8E4M3, { N, K }); /* b */
|
|
o.t((uint32_t)RAD_BF16, { nb, kb }); /* b_scale */
|
|
o.t((uint32_t)RAD_F8E4M3, { M, n }); /* q */
|
|
o.t((uint32_t)RAD_F32, { M, ng }); /* scale */
|
|
o.t((uint32_t)RAD_BF16, { M, n }); /* out_bf16 */
|
|
return o.finish("gemm_nt_q_gated", operand, out);
|
|
}
|
|
|
|
/* DFlash2's candidate walk. `cand` and `anchor` hold token ids, bounded by n_vocab so the codebook
|
|
* gather stays in the plane. `cand`/`unary`/`hp` have one row per (sequence, step), M * steps. */
|
|
int ref_dflash_select_shape(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);
|
|
const int64_t K = gi(p, n_p, "top_k", 0);
|
|
const int64_t R = gi(p, n_p, "rank", 0);
|
|
const int64_t nv = gi(p, n_p, "n_vocab", 0);
|
|
const int64_t astr = gi(p, n_p, "anchor_stride", 1);
|
|
if (M <= 0 || steps <= 0 || K <= 0 || R <= 0 || nv <= 0 || astr <= 0) return RAD_E_UNSUPPORTED;
|
|
const int64_t rows = M * steps;
|
|
Opd o;
|
|
o.t_idx_dt((uint32_t)RAD_I32, { rows, K }, nv, -1, 0); /* cand */
|
|
o.t((uint32_t)RAD_F32, { rows, K }); /* unary */
|
|
o.t((uint32_t)RAD_BF16, { rows, R }); /* hp, absent is a plane of ones */
|
|
o.t_idx_dt((uint32_t)RAD_I32, { M * astr }, nv, -1, 0); /* anchor */
|
|
o.t((uint32_t)RAD_BF16, { nv, R }); /* pred */
|
|
o.t((uint32_t)RAD_BF16, { nv, R }); /* succ */
|
|
o.t_idx_dt((uint32_t)RAD_I32, { M, steps }, nv, -1, 0); /* tokens */
|
|
return o.finish("dflash_select", operand, out);
|
|
}
|
|
|
|
/* The full-vocabulary DSpark Markov walk. Its tile planes hold one exact local max per 64 ids. */
|
|
static int ref_markov_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out,
|
|
uint32_t logits_dtype, const char* op) {
|
|
const int64_t M = gi(p, n_p, "M", 0);
|
|
const int64_t steps = gi(p, n_p, "steps", 0);
|
|
const int64_t R = gi(p, n_p, "rank", 0);
|
|
const int64_t nv = gi(p, n_p, "n_vocab", 0);
|
|
const int64_t astr = gi(p, n_p, "anchor_stride", 1);
|
|
if (M <= 0 || steps <= 0 || R <= 0 || nv <= 0 || astr <= 0)
|
|
return RAD_E_UNSUPPORTED;
|
|
const int64_t rows = M * steps;
|
|
const int64_t tiles = (nv + 63) / 64;
|
|
Opd o;
|
|
o.t(logits_dtype, { rows, nv }); /* logits */
|
|
o.t_idx_dt((uint32_t)RAD_I32, { M * astr }, nv, -1, 0); /* anchor */
|
|
o.t((uint32_t)RAD_BF16, { nv, R }); /* pred */
|
|
o.t((uint32_t)RAD_BF16, { nv, R }); /* succ */
|
|
o.t((uint32_t)RAD_F32, { M, tiles }); /* tile_scores */
|
|
o.t_idx_dt((uint32_t)RAD_I32, { M, tiles }, nv, -1, 0); /* tile_tokens */
|
|
o.t_idx_dt((uint32_t)RAD_I32, { M, steps }, nv, -1, 0); /* tokens */
|
|
return o.finish(op, operand, out);
|
|
}
|
|
|
|
int ref_dflash_markov_greedy_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
return ref_markov_shape(p, n_p, operand, out, RAD_BF16, "dflash_markov_greedy");
|
|
}
|
|
int ref_dflash_markov_greedy_f32_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
return ref_markov_shape(p, n_p, operand, out, RAD_F32, "dflash_markov_greedy_f32");
|
|
}
|
|
|
|
/* dflash_select's operands, then `nodes` tokens and parents a sequence. */
|
|
int ref_dflash_tree_shape(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);
|
|
const int64_t K = gi(p, n_p, "top_k", 0);
|
|
const int64_t R = gi(p, n_p, "rank", 0);
|
|
const int64_t nv = gi(p, n_p, "n_vocab", 0);
|
|
const int64_t astr = gi(p, n_p, "anchor_stride", 1);
|
|
const int64_t N = gi(p, n_p, "nodes", steps);
|
|
if (M <= 0 || steps <= 0 || K <= 0 || R <= 0 || nv <= 0 || astr <= 0 || N <= 0)
|
|
return RAD_E_UNSUPPORTED;
|
|
const int64_t rows = M * steps;
|
|
Opd o;
|
|
o.t_idx_dt((uint32_t)RAD_I32, { rows, K }, nv, -1, 0); /* cand */
|
|
o.t((uint32_t)RAD_F32, { rows, K }); /* unary */
|
|
o.t((uint32_t)RAD_BF16, { rows, R }); /* hp */
|
|
o.t_idx_dt((uint32_t)RAD_I32, { M * astr }, nv, -1, 0); /* anchor */
|
|
o.t((uint32_t)RAD_BF16, { nv, R }); /* pred */
|
|
o.t((uint32_t)RAD_BF16, { nv, R }); /* succ */
|
|
o.t_idx_dt((uint32_t)RAD_I32, { M, N }, nv, -1, 0); /* tokens */
|
|
o.t_idx_dt((uint32_t)RAD_I32, { M, N }, N + 1, -1, 0); /* parents */
|
|
return o.finish("dflash_tree", operand, out);
|
|
}
|
|
|
|
/* ================================================================== gated delta net
|
|
*
|
|
* The layouts are libref/ref_gdn.cpp's:
|
|
*
|
|
* q, k [T, Hk, head_k] v, o [T, Hv, head_v]
|
|
* g, beta [T, Hv] fp32 A [T, Hv, chunk]
|
|
* h0, ht [N, Hv, head_v, head_k] fp32 cu [N+1]
|
|
* conv w [conv_dim, conv_width] conv_state [n_slots, conv_dim, state_len]
|
|
*
|
|
* with conv_dim = 2 * Hk * head_k + Hv * head_v. The family declines unless the caller supplies
|
|
* the head counts (see gdn_heads). */
|
|
|
|
namespace {
|
|
/* What the gdn descriptions share, resolved once; gdn_geom returns false to decline. */
|
|
struct GdnGeom {
|
|
int64_t Hk, Hv, hk, hv, chunk, width, conv_dim, S, M;
|
|
uint32_t D;
|
|
};
|
|
|
|
static bool gdn_geom(const RadParam* p, int n_p, const char* mkey, GdnGeom* g) {
|
|
if (!gdn_heads(p, n_p, &g->Hk, &g->Hv)) return false;
|
|
g->hk = gi(p, n_p, "head_k", 0);
|
|
g->hv = gi(p, n_p, "head_v", g->hk);
|
|
g->chunk = gi(p, n_p, "chunk", 64);
|
|
g->width = gi(p, n_p, "conv_width", 4);
|
|
g->M = gi(p, n_p, mkey, 0);
|
|
g->S = n_seq_of(p, n_p);
|
|
g->D = act_dt(p, n_p);
|
|
if (g->hk <= 0 || g->hv <= 0 || g->chunk <= 0 || g->width <= 1 || g->M <= 0) return false;
|
|
g->conv_dim = 2 * g->Hk * g->hk + g->Hv * g->hv;
|
|
return true;
|
|
}
|
|
|
|
/* The recurrence's state, state_idx and num_accepted, by `state_form` (docs/OPS.md).
|
|
*
|
|
* per_candidate: a slot per candidate token, a [S, M] table of distinct slots, and every token
|
|
* accepted.
|
|
*
|
|
* anchor: the kernels read six columns a sequence (the anchor's three, then the factor block's
|
|
* three), so the table is at least six wide, every entry a distinct slot. The state is drawn
|
|
* whole, so the factor block holds random factors and a random stamp; every implementation reads
|
|
* a block the same way (each 32-row group of V from its own copy of k, g and beta, the anchor
|
|
* column from the rounded stamp), so that is a defined input. One accepted token is replayed: with
|
|
* random k the replay scales the state by about |k|^2, which one token keeps finite. A step
|
|
* longer than the factor block holds is refused by the op, so it is not described. */
|
|
bool gdn_state(const RadParam* p, int n_p, const GdnGeom& g, Opd& o) {
|
|
const char* form = gs(p, n_p, "state_form");
|
|
if (form && std::strcmp(form, "anchor") == 0) {
|
|
const int64_t groups = (g.hv + 31) / 32, fstr = groups * g.hk + g.hv + 2 * groups;
|
|
const int64_t tmax = std::min<int64_t>(16, (g.hv * g.hk - 1) / fstr);
|
|
if (tmax < 1 || g.M > tmax) return false;
|
|
const int64_t cols = std::max<int64_t>(g.M, 6), slots = g.S * cols;
|
|
o.t((uint32_t)RAD_F32, { slots, g.Hv, g.hv, g.hk }); /* state, INOUT */
|
|
o.t_idx({ g.S, cols }, slots, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE); /* state_idx */
|
|
o.t_idx({ g.S }, 0, 1, 0); /* num_accepted */
|
|
return true;
|
|
}
|
|
if (form && std::strcmp(form, "per_candidate") != 0) return false;
|
|
const int64_t T = g.S * g.M;
|
|
/* One state slot per candidate token (ref_ops.h's rollback contract). */
|
|
o.t((uint32_t)RAD_F32, { T, g.Hv, g.hv, g.hk }); /* state, INOUT */
|
|
o.t_idx({ g.S, g.M }, T, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE); /* state_idx */
|
|
o.t_idx({ g.S }, 0, 1, 0); /* num_accepted: none rejected */
|
|
return true;
|
|
}
|
|
|
|
} /* namespace */
|
|
|
|
int ref_gdn_conv_prep_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
GdnGeom g;
|
|
if (!gdn_geom(p, n_p, "M", &g)) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
/* T tokens over S sequences, one conv-state slot each, `state_len` conv_width - 1 (the
|
|
* manager's conv_width - 1 + n_spec with no speculation). */
|
|
o.t(g.D, { g.M, g.conv_dim }); /* x -- the fused projection row */
|
|
o.t(g.D, { g.conv_dim, g.width }); /* w */
|
|
o.skip(); /* b -- this checkpoint has no conv bias */
|
|
o.t((uint32_t)RAD_F32, { g.Hv }); /* A_log */
|
|
o.t((uint32_t)RAD_F32, { g.Hv }); /* dt_bias */
|
|
o.t(g.D, { g.S, g.conv_dim, g.width - 1 }); /* conv_state, INOUT */
|
|
o.t_idx({ g.S + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu */
|
|
/* `a` and `b_gate` passed: the separate-projection path every current caller takes. */
|
|
o.t(g.D, { g.M, g.Hv }); /* a */
|
|
o.t(g.D, { g.M, g.Hv }); /* b_gate */
|
|
o.t_idx({ g.S }, g.S, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE); /* cache_idx, one a sequence */
|
|
/* A cold prefill, stated explicitly: ref reads a null `has_init` as warm, libr4d as cold. */
|
|
o.t_idx({ g.S }, 0, 0, 0); /* has_init */
|
|
o.t(g.D, { g.M, g.Hk, g.hk }); /* q */
|
|
o.t_fill(g.D, { g.M, g.Hk, g.hk }, (uint32_t)RAD_FILL_UNIT_ROW, 0); /* k */
|
|
o.t(g.D, { g.M, g.Hv, g.hv }); /* v */
|
|
/* g and beta are outputs, but their domains are stated: they are the inputs of gdn_kkt_solve
|
|
* and gdn_chunk_scan. */
|
|
o.t_fill((uint32_t)RAD_F32, { g.M, g.Hv }, (uint32_t)RAD_FILL_GATE_CUMSUM, g.chunk); /* g */
|
|
o.t_fill((uint32_t)RAD_F32, { g.M, g.Hv }, (uint32_t)RAD_FILL_SIGMOID, 0); /* beta */
|
|
return o.finish("gdn_conv_prep", operand, out);
|
|
}
|
|
|
|
int ref_gdn_conv_update_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
GdnGeom g;
|
|
if (!gdn_geom(p, n_p, "q_len", &g)) return RAD_E_UNSUPPORTED;
|
|
/* Decode: `M` is q_len, one row per candidate token, and the window is conv_width - 2 + q_len
|
|
* deep (ref_ops.h's rollback contract). */
|
|
const int64_t sl = g.width - 2 + g.M;
|
|
Opd o;
|
|
o.t(g.D, { g.S * g.M, g.conv_dim }); /* x */
|
|
o.t(g.D, { g.conv_dim, g.width }); /* w */
|
|
o.skip(); /* b */
|
|
o.t(g.D, { g.S, g.conv_dim, sl }); /* conv_state, INOUT */
|
|
o.t_idx({ g.S, g.M }, g.S, -1, 0); /* state_idx */
|
|
/* num_accepted is 1-based; all ones is the non-speculative decode. A draw could move the read
|
|
* offset outside the window. */
|
|
o.t_idx({ g.S }, 0, 1, 0); /* num_accepted */
|
|
o.t_idx({ g.S + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu */
|
|
o.t(g.D, { g.S * g.M, g.Hk, g.hk }); /* q */
|
|
o.t(g.D, { g.S * g.M, g.Hk, g.hk }); /* k */
|
|
o.t(g.D, { g.S * g.M, g.Hv, g.hv }); /* v */
|
|
return o.finish("gdn_conv_update", operand, out);
|
|
}
|
|
|
|
/* The `_tree` forms: the chain op's operands, then the parents, each row an ordinary causal one
|
|
* (-2), as attn_paged_tree's: a drawn plane would name parents that are not a tree. `skip` is the
|
|
* chain op's operand count, its optional outputs included. */
|
|
static int gdn_tree_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out, int skip,
|
|
const char* name) {
|
|
GdnGeom g;
|
|
if (!gdn_geom(p, n_p, "q_len", &g)) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
for (int i = 0; i < skip; ++i) o.skip();
|
|
o.t_idx({ g.S * g.M }, 0, -2, 0); /* spec_parent */
|
|
return o.finish(name, operand, out);
|
|
}
|
|
|
|
int ref_gdn_conv_update_tree_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
if (operand < 10) return ref_gdn_conv_update_shape(p, n_p, operand, out);
|
|
return gdn_tree_shape(p, n_p, operand, out, 10, "gdn_conv_update_tree");
|
|
}
|
|
|
|
int ref_gdn_kkt_solve_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
GdnGeom g;
|
|
if (!gdn_geom(p, n_p, "M", &g)) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t_fill(g.D, { g.M, g.Hk, g.hk }, (uint32_t)RAD_FILL_UNIT_ROW, 0); /* k */
|
|
o.t_fill((uint32_t)RAD_F32, { g.M, g.Hv }, (uint32_t)RAD_FILL_SIGMOID, 0); /* beta */
|
|
/* Non-increasing, never positive, reset at each chunk boundary (see the file header). */
|
|
o.t_fill((uint32_t)RAD_F32, { g.M, g.Hv }, (uint32_t)RAD_FILL_GATE_CUMSUM, g.chunk); /* g */
|
|
o.t_idx({ g.S + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu */
|
|
o.t_fill(g.D, { g.M, g.Hv, g.chunk }, (uint32_t)RAD_FILL_TRI_LOWER, g.chunk); /* A */
|
|
return o.finish("gdn_kkt_solve", operand, out);
|
|
}
|
|
|
|
int ref_gdn_chunk_scan_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
GdnGeom g;
|
|
if (!gdn_geom(p, n_p, "M", &g)) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(g.D, { g.M, g.Hk, g.hk }); /* q */
|
|
o.t_fill(g.D, { g.M, g.Hk, g.hk }, (uint32_t)RAD_FILL_UNIT_ROW, 0); /* k */
|
|
o.t(g.D, { g.M, g.Hv, g.hv }); /* v */
|
|
/* Zero above the diagonal within a chunk, as kkt_solve produces (see the file header). */
|
|
o.t_fill(g.D, { g.M, g.Hv, g.chunk }, (uint32_t)RAD_FILL_TRI_LOWER, g.chunk); /* A */
|
|
o.t_fill((uint32_t)RAD_F32, { g.M, g.Hv }, (uint32_t)RAD_FILL_GATE_CUMSUM, g.chunk); /* g */
|
|
o.t_fill((uint32_t)RAD_F32, { g.M, g.Hv }, (uint32_t)RAD_FILL_SIGMOID, 0); /* beta */
|
|
o.t((uint32_t)RAD_F32, { g.S, g.Hv, g.hv, g.hk }); /* h0 */
|
|
o.t_idx({ g.S + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu */
|
|
o.t(g.D, { g.M, g.Hv, g.hv }); /* o */
|
|
o.t((uint32_t)RAD_F32, { g.S, g.Hv, g.hv, g.hk }); /* ht */
|
|
o.t_idx({ g.S }, g.S, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE); /* state_idx */
|
|
return o.finish("gdn_chunk_scan", operand, out);
|
|
}
|
|
|
|
int ref_gdn_recurrent_update_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
GdnGeom g;
|
|
if (!gdn_geom(p, n_p, "q_len", &g)) return RAD_E_UNSUPPORTED;
|
|
const int64_t T = g.S * g.M;
|
|
Opd o;
|
|
o.t(g.D, { T, g.Hk, g.hk }); /* q */
|
|
o.t(g.D, { T, g.Hk, g.hk }); /* k */
|
|
o.t(g.D, { T, g.Hv, g.hv }); /* v */
|
|
/* `a` and `b` are the raw gate and beta projections (the op applies softplus and sigmoid
|
|
* itself), so normals are the right domain. */
|
|
o.t(g.D, { T, g.Hv }); /* a */
|
|
o.t(g.D, { T, g.Hv }); /* b */
|
|
o.t((uint32_t)RAD_F32, { g.Hv }); /* A_log */
|
|
o.t((uint32_t)RAD_F32, { g.Hv }); /* dt_bias */
|
|
if (!gdn_state(p, n_p, g, o)) return RAD_E_UNSUPPORTED; /* state, state_idx, num_accepted */
|
|
o.t_idx({ g.S + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu */
|
|
o.t(g.D, { T, g.Hv, g.hv }); /* z -- the output gate */
|
|
o.t((uint32_t)RAD_F32, { g.hv }); /* norm_w */
|
|
o.t(g.D, { T, g.Hv, g.hv }); /* o */
|
|
/* The folded fp8 out-projection quantiser (all-or-nothing) is skipped; the unfused pair (this
|
|
* op, then quant_act_fp8) covers the same arithmetic. */
|
|
o.skip(); /* o_q */
|
|
o.skip(); /* o_scale */
|
|
return o.finish("gdn_recurrent_update", operand, out);
|
|
}
|
|
|
|
int ref_gdn_conv_recurrent_update_shape(const RadParam* p, int n_p, int operand,
|
|
RadOpdDesc* out) {
|
|
GdnGeom g;
|
|
if (!gdn_geom(p, n_p, "q_len", &g)) return RAD_E_UNSUPPORTED;
|
|
/* gdn_conv_update's inputs, then gdn_recurrent_update's minus q, k and v. `conv_idx` is one
|
|
* slot per sequence. */
|
|
const int64_t T = g.S * g.M, sl = g.width - 2 + g.M;
|
|
Opd o;
|
|
o.t(g.D, { T, g.conv_dim }); /* x */
|
|
o.t(g.D, { g.conv_dim, g.width }); /* conv_w */
|
|
o.skip(); /* conv_b */
|
|
o.t(g.D, { g.S, g.conv_dim, sl }); /* conv_state, INOUT */
|
|
o.t_idx({ g.S }, g.S, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE); /* conv_idx */
|
|
o.t(g.D, { T, g.Hv }); /* a */
|
|
o.t(g.D, { T, g.Hv }); /* b */
|
|
o.t((uint32_t)RAD_F32, { g.Hv }); /* A_log */
|
|
o.t((uint32_t)RAD_F32, { g.Hv }); /* dt_bias */
|
|
if (!gdn_state(p, n_p, g, o)) return RAD_E_UNSUPPORTED; /* state, state_idx, num_accepted */
|
|
o.t_idx({ g.S + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu */
|
|
o.t(g.D, { T, g.Hv, g.hv }); /* z */
|
|
o.t((uint32_t)RAD_F32, { g.hv }); /* norm_w */
|
|
o.t(g.D, { T, g.Hv, g.hv }); /* o */
|
|
o.skip(); /* o_q -- see gdn_recurrent_update */
|
|
o.skip(); /* o_scale */
|
|
return o.finish("gdn_conv_recurrent_update", operand, out);
|
|
}
|
|
|
|
int ref_gdn_recurrent_update_tree_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
if (operand < 16) return ref_gdn_recurrent_update_shape(p, n_p, operand, out);
|
|
return gdn_tree_shape(p, n_p, operand, out, 16, "gdn_recurrent_update_tree");
|
|
}
|
|
|
|
int ref_gdn_conv_recurrent_update_tree_shape(const RadParam* p, int n_p, int operand,
|
|
RadOpdDesc* out) {
|
|
if (operand < 18) return ref_gdn_conv_recurrent_update_shape(p, n_p, operand, out);
|
|
return gdn_tree_shape(p, n_p, operand, out, 18, "gdn_conv_recurrent_update_tree");
|
|
}
|
|
|
|
int ref_gdn_gated_rmsnorm_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
/* One row per (token, head): `M` is tokens x heads and `channels` is head_v, so no head count
|
|
* is needed. */
|
|
const int64_t M = gi(p, n_p, "M", 0);
|
|
const int64_t ch = gi(p, n_p, "channels", 0);
|
|
if (M <= 0 || ch <= 0) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M, ch }); /* x */
|
|
o.t(D, { M, ch }); /* z -- the gate */
|
|
o.t((uint32_t)RAD_F32, { ch }); /* w */
|
|
o.t(D, { M, ch }); /* o */
|
|
return o.finish("gdn_gated_rmsnorm", operand, out);
|
|
}
|
|
|
|
/* ================================================================== collectives */
|
|
|
|
int ref_all_reduce_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t numel = gi(p, n_p, "numel", 0);
|
|
if (numel <= 0) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { numel }); /* x, INOUT */
|
|
/* `y`, the optional separate destination, is passed: libr4d's two-shot rows are not safe in
|
|
* place and need it (see ref_all_reduce). */
|
|
o.t(D, { numel }); /* y */
|
|
return o.finish("all_reduce", operand, out);
|
|
}
|
|
|
|
int ref_all_gather_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t numel = gi(p, n_p, "numel", 0);
|
|
const int64_t W = gi(p, n_p, "world_size", 1);
|
|
if (numel <= 0 || W <= 0) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { numel }); /* x */
|
|
o.t(D, { numel * W }); /* y -- rank r's contribution at r * numel */
|
|
return o.finish("all_gather", operand, out);
|
|
}
|
|
|
|
/* ================================================================== vocabulary edges */
|
|
|
|
int ref_embed_lookup_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0);
|
|
const int64_t ne = gi(p, n_p, "n_embd", 0);
|
|
const int64_t nv = gi(p, n_p, "n_vocab", 0);
|
|
const int64_t vo = gi(p, n_p, "vocab_offset", 0);
|
|
if (M <= 0 || ne <= 0 || nv <= 0 || vo < 0) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
/* The range is explicit since `tokens` is operand 0. Ids are global, so the bound is
|
|
* `vocab_offset + nv`: under a shard the draw crosses the slice boundary and exercises the
|
|
* zero-row path. */
|
|
o.t_idx({ M }, vo + nv, -1, 0); /* tokens */
|
|
o.t(D, { nv, ne }); /* wte */
|
|
o.t(D, { M, ne }); /* x */
|
|
return o.finish("embed_lookup", operand, out);
|
|
}
|
|
|
|
/* The quantised gather: the table is E4M3 and the output the activation dtype (`dtype` names the
|
|
* combination, e.g. "w8a16"). `scale` is one element; a normal draw is fine, since the op only
|
|
* multiplies by it. */
|
|
int ref_embed_lookup_q_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0);
|
|
const int64_t ne = gi(p, n_p, "n_embd", 0);
|
|
const int64_t nv = gi(p, n_p, "n_vocab", 0);
|
|
const int64_t vo = gi(p, n_p, "vocab_offset", 0);
|
|
if (M <= 0 || ne <= 0 || nv <= 0 || vo < 0) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t_idx({ M }, vo + nv, -1, 0); /* tokens */
|
|
o.t((uint32_t)RAD_F8E4M3, { nv, ne }); /* wte */
|
|
o.t(D, { 1 }); /* scale */
|
|
o.t(D, { M, ne }); /* x */
|
|
o.skip(); /* ahead: a later call's ids, optional */
|
|
return o.finish("embed_lookup_q", operand, out);
|
|
}
|
|
|
|
/* The planes as a host tool draws them: i8 codes (no table), a scale per `group` of a row. */
|
|
int ref_embed_lookup_planes_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0);
|
|
const int64_t ne = gi(p, n_p, "n_embd", 0), nv = gi(p, n_p, "n_vocab", 0);
|
|
const int64_t G = gi(p, n_p, "group", 0), vo = gi(p, n_p, "vocab_offset", 0);
|
|
if (M <= 0 || ne <= 0 || nv <= 0 || G <= 0 || ne % G || vo < 0) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t_idx({ M }, vo + nv, -1, 0); /* tokens */
|
|
o.t((uint32_t)RAD_I8, { nv, ne }); /* codes */
|
|
o.t(D, { nv, ne / G }); /* scale */
|
|
o.skip(); /* table: optional */
|
|
o.t(D, { M, ne }); /* x */
|
|
return o.finish("embed_lookup_planes", operand, out);
|
|
}
|
|
|
|
int ref_logits_gemm_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0);
|
|
const int64_t nv = gi(p, n_p, "n_vocab", 0);
|
|
const int64_t ne = gi(p, n_p, "n_embd", 0);
|
|
if (M <= 0 || nv <= 0 || ne <= 0) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M, ne }); /* x */
|
|
o.t(D, { nv, ne }); /* lm_head */
|
|
/* f32 logits, as every sampler stage reads them. */
|
|
o.t((uint32_t)RAD_F32, { M, nv }); /* logits */
|
|
return o.finish("logits_gemm", operand, out);
|
|
}
|
|
|
|
/* ================================================================== sampling
|
|
*
|
|
* Per-row scalars arrive in the `params` operand (see sample_params_words() for its draw). The
|
|
* other operands:
|
|
*
|
|
* logits f32 [M, n_vocab] cand_idx i32 [M, n_cand] token i32 [M]
|
|
* history i32 [M, max_hist] cand_val f32 [M, n_cand] bitmask i32 [M, ceil(nv/32)]
|
|
* breakers u8 [M, max_hist]
|
|
*
|
|
* libr4d's shim requires exactly these dtypes (libr4d/r4d_sample_stages_f32.hip). Word-typed
|
|
* operands are i32, never u32 (see sample_params_words()). */
|
|
|
|
/* The history window width: the deployment sizes the history plane, so this picks a plausible
|
|
* 64. */
|
|
enum { REF_SAMPLE_HIST = 64 };
|
|
/* The candidate width for `sample_topk`, which reads it off the operand (its parameters carry only
|
|
* `n_vocab`): narrower than the vocabulary, wide enough for the narrowing stages. */
|
|
enum { REF_SAMPLE_CAND = 64 };
|
|
|
|
static 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);
|
|
return (*M > 0 && *nv > 0) ? RAD_OK : RAD_E_UNSUPPORTED;
|
|
}
|
|
|
|
/* The `params` plane, drawn as requests (see sample_params_words()). The law places each row's
|
|
* history window inside a plane `hist` wide and its `mask_row` among `mask_rows` bitmask rows; 0
|
|
* for an op without that plane. */
|
|
static void sample_params(Opd& o, int64_t M, int64_t hist, int64_t mask_rows) {
|
|
RadOpdDesc* d = o.push();
|
|
Opd::shape(d, (uint32_t)RAD_F32, { M, sample_params_words() });
|
|
d->fill = (uint32_t)RAD_FILL_SAMPLE_PARAMS;
|
|
d->fill_chunk = hist;
|
|
d->idx_max = mask_rows;
|
|
}
|
|
|
|
/* sample_penalties and sample_dry share logits, params and history. The history is real token ids
|
|
* in [0, n_vocab) that repeat as text does (RAD_OPD_F_IDX_HISTORY). DRY's optional breaker plane is
|
|
* passed with about half its positions marked, so the exemption runs as well as the penalty. */
|
|
static int penalty_shape(const RadParam* p, int n_p, int operand, const char* op,
|
|
bool breakers, RadOpdDesc* out) {
|
|
int64_t M = 0, nv = 0;
|
|
if (sample_vocab(p, n_p, &M, &nv) < 0) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t((uint32_t)RAD_F32, { M, nv }); /* logits, INOUT */
|
|
sample_params(o, M, REF_SAMPLE_HIST, 0); /* params */
|
|
o.t_idx({ M, (int64_t)REF_SAMPLE_HIST }, nv, -1,
|
|
(uint32_t)RAD_OPD_F_IDX_HISTORY); /* history */
|
|
if (breakers) /* breakers, optional */
|
|
o.t_idx_dt((uint32_t)RAD_U8, { M, (int64_t)REF_SAMPLE_HIST }, 2, -1, 0);
|
|
return o.finish(op, operand, out);
|
|
}
|
|
|
|
int ref_sample_penalties_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
return penalty_shape(p, n_p, operand, "sample_penalties", false, out);
|
|
}
|
|
int ref_sample_dry_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
return penalty_shape(p, n_p, operand, "sample_dry", true, out);
|
|
}
|
|
|
|
/* The n-gram ban reads the same history the two penalty stages do and writes the logits in place,
|
|
* and adds one small plane beside them: `ngram`, an i32 {n-gram length, window} pair a row. It is
|
|
* a plane of its own rather than fields in `params` because the params row is a fixed 24-word wire
|
|
* format shared by every sampler op (and recorded in the kbench fixtures). Drawn in 0..63 so both
|
|
* a length inside the 64-token history and the 0 (= off) and whole-window forms occur.
|
|
*
|
|
* The optional `whitelist` bitmask is sample_mask's plane shape (word w bit b covers column
|
|
* 32w + b, a SET bit marking an exempt token), addressed by the sampled row itself; a random draw
|
|
* fills it with random bits, so a sweep exercises exemptions as often as plain bans. */
|
|
int ref_sample_ngram_ban_shape(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;
|
|
Opd o;
|
|
o.t((uint32_t)RAD_F32, { M, nv }); /* logits, INOUT */
|
|
sample_params(o, M, REF_SAMPLE_HIST, 0); /* params */
|
|
o.t_idx({ M, (int64_t)REF_SAMPLE_HIST }, nv, -1,
|
|
(uint32_t)RAD_OPD_F_IDX_HISTORY); /* history */
|
|
o.t_idx({ M, 2 }, 64, -1, 0); /* ngram: {n, window} */
|
|
o.t((uint32_t)RAD_I32, { M, (nv + 31) / 32 }); /* whitelist, optional */
|
|
return o.finish("sample_ngram_ban", operand, out);
|
|
}
|
|
|
|
int ref_sample_temp_shape(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;
|
|
Opd o;
|
|
o.t((uint32_t)RAD_F32, { M, nv }); /* logits, INOUT */
|
|
sample_params(o, M, 0, 0); /* params */
|
|
return o.finish("sample_temp", operand, out);
|
|
}
|
|
|
|
int ref_sample_mask_shape(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;
|
|
Opd o;
|
|
o.t((uint32_t)RAD_F32, { M, nv }); /* logits, INOUT */
|
|
sample_params(o, M, 0, M); /* params */
|
|
/* A set bit means allowed, word w bit b covering token 32w + b. One row per sequence is the
|
|
* widest the plane gets (unconstrained rows share it with `mask_row` -1). */
|
|
o.t((uint32_t)RAD_I32, { M, (nv + 31) / 32 }); /* bitmask */
|
|
return o.finish("sample_mask", operand, out);
|
|
}
|
|
|
|
int ref_sample_argmax_shape(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;
|
|
Opd o;
|
|
o.t((uint32_t)RAD_F32, { M, nv }); /* logits */
|
|
sample_params(o, M, 0, 0); /* params -- unread by argmax */
|
|
o.t((uint32_t)RAD_I32, { M }); /* token */
|
|
return o.finish("sample_argmax", operand, out);
|
|
}
|
|
|
|
int ref_sample_logprobs_shape(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 int64_t k = gi(p, n_p, "k", 0);
|
|
if (k <= 0) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t((uint32_t)RAD_F32, { M, nv }); /* logits */
|
|
sample_params(o, M, 0, 0); /* params */
|
|
o.t((uint32_t)RAD_F32, { M, 2 + 2 * k }); /* stats */
|
|
return o.finish("sample_logprobs", operand, out);
|
|
}
|
|
|
|
int ref_sample_topk_shape(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 int64_t nc = nv < (int64_t)REF_SAMPLE_CAND ? nv : (int64_t)REF_SAMPLE_CAND;
|
|
Opd o;
|
|
o.t((uint32_t)RAD_F32, { M, nv }); /* logits */
|
|
sample_params(o, M, 0, 0); /* params */
|
|
o.t((uint32_t)RAD_I32, { M, nc }); /* cand_idx */
|
|
o.t((uint32_t)RAD_F32, { M, nc }); /* cand_val -- logits, not probs */
|
|
/* The vocab-parallel pair plane, an output, described to check the packing: (u32 global id,
|
|
* f32 logit), 0xffffffff for a hole; `vocab_off` is 0 here. */
|
|
o.t((uint32_t)RAD_I32, { M, 2 * nc }); /* pairs */
|
|
return o.finish("sample_topk", operand, out);
|
|
}
|
|
|
|
/* The four narrowing stages share a schema (cand_idx, cand_val, params, in place) and one
|
|
* description.
|
|
*
|
|
* The candidate set is drawn, not sorted: the ABI has no descending fill domain. min-p, typical and
|
|
* XTC are per-candidate predicates. 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. */
|
|
static int narrow_shape(const RadParam* p, int n_p, int operand, const char* op, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0);
|
|
const int64_t nc = gi(p, n_p, "n_cand", 0);
|
|
if (M <= 0 || nc <= 0) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
/* Drawn indices are >= 0, so every candidate is live. The range is the candidate width; ids
|
|
* are only copied and compared here. */
|
|
o.t_idx({ M, nc }, nc, -1, 0); /* cand_idx, INOUT */
|
|
o.t((uint32_t)RAD_F32, { M, nc }); /* cand_val, INOUT */
|
|
sample_params(o, M, 0, 0); /* params */
|
|
return o.finish(op, operand, out);
|
|
}
|
|
|
|
int ref_sample_topp_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
return narrow_shape(p, n_p, operand, "sample_topp", out);
|
|
}
|
|
int ref_sample_minp_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
return narrow_shape(p, n_p, operand, "sample_minp", out);
|
|
}
|
|
int ref_sample_typical_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
return narrow_shape(p, n_p, operand, "sample_typical", out);
|
|
}
|
|
int ref_sample_xtc_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
return narrow_shape(p, n_p, operand, "sample_xtc", out);
|
|
}
|
|
|
|
int ref_sample_pick_shape(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);
|
|
if (M <= 0 || nc <= 0) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t_idx({ M, nc }, nc, -1, 0); /* cand_idx */
|
|
o.t((uint32_t)RAD_F32, { M, nc }); /* cand_val */
|
|
sample_params(o, M, 0, 0); /* params -- seed and pos */
|
|
o.t((uint32_t)RAD_I32, { M }); /* token */
|
|
return o.finish("sample_pick", operand, out);
|
|
}
|
|
|
|
/* sample_merge_topk is declined although its extents are known. `gathered` packs (u32 global id,
|
|
* f32 logit) pairs in a word-typed buffer, so logits are read by bit pattern and no fill domain
|
|
* yields sensible floats: an integer draw makes every logit a denormal, which a flushing device
|
|
* and a non-flushing host order differently (the failure r4d_selftest's two-rank all-gather
|
|
* checks). Also, `world_size` is not a parameter, so the gathered width would be invented.
|
|
*
|
|
* row_topk_merge is not declined: its plane is f32, so the value lane is a real float. */
|
|
int ref_sample_merge_topk_shape(const RadParam*, int, int, RadOpdDesc*) {
|
|
return RAD_E_UNSUPPORTED;
|
|
}
|
|
|
|
/* ================================================================== image generation (ref_gen.cpp)
|
|
*
|
|
* Lane ops are described over REF_LANES lanes and image ops at a chosen picture shape (below);
|
|
* gather_rows2 declines. */
|
|
int ref_ln_mod_shape(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 uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x */
|
|
o.t(D, { 1, n }); /* s: one sequence */
|
|
o.t_idx({ 2 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu_seqlens */
|
|
o.t(D, { M, n }); /* y */
|
|
return o.finish("ln_mod", operand, out);
|
|
}
|
|
/* rms_mod's branch and gate are passed when `gated`, and absent otherwise. */
|
|
int ref_rms_mod_shape(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 uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
if (gi(p, n_p, "gated", 0)) {
|
|
o.t(D, { M, n }); /* y */
|
|
o.t(D, { 1, n }); /* g: one sequence */
|
|
} else {
|
|
o.skip();
|
|
o.skip();
|
|
}
|
|
o.t(D, { 1, 2 * n }); /* s: [shift | scale] */
|
|
/* A LayerNorm without a gain passes none, and no norm takes none. */
|
|
const char* nk = gs(p, n_p, "norm");
|
|
if (nk && (std::strcmp(nk, "layer") == 0 || std::strcmp(nk, "none") == 0)) o.skip(); /* w */
|
|
else o.t(D, { n }); /* w */
|
|
o.t_idx({ 2 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu_seqlens */
|
|
o.t(D, { M, n }); /* x, INOUT */
|
|
o.t(D, { M, n }); /* z */
|
|
o.skip(); /* colmap: the sweep passes none */
|
|
return o.finish("rms_mod", operand, out);
|
|
}
|
|
/* The sampler's three scalars in (0, 1), the state f32. */
|
|
int ref_flow_x0_step_shape(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 uint32_t F = (uint32_t)RAD_F32;
|
|
Opd o;
|
|
o.t(act_dt(p, n_p), { M, n }); /* v */
|
|
o.t_fill(F, { 1 }, (uint32_t)RAD_FILL_SIGMOID, 0); /* t */
|
|
o.t_fill(F, { 1 }, (uint32_t)RAD_FILL_SIGMOID, 0); /* sigma */
|
|
o.t_fill(F, { 1 }, (uint32_t)RAD_FILL_SIGMOID, 0); /* sigma_next */
|
|
o.t(F, { M, n }); /* x, INOUT */
|
|
return o.finish("flow_x0_step", operand, out);
|
|
}
|
|
int ref_flow_res_multistep_shape(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 uint32_t F = (uint32_t)RAD_F32;
|
|
Opd o;
|
|
o.t(act_dt(p, n_p), { M, n }); /* v */
|
|
o.t(F, { M, n }); /* x0, INOUT */
|
|
o.t_fill(F, { 1 }, (uint32_t)RAD_FILL_SIGMOID, 0); /* sigma */
|
|
o.t_fill(F, { 1 }, (uint32_t)RAD_FILL_SIGMOID, 0); /* r */
|
|
o.t_fill(F, { 1 }, (uint32_t)RAD_FILL_SIGMOID, 0); /* hb1 */
|
|
o.t_fill(F, { 1 }, (uint32_t)RAD_FILL_SIGMOID, 0); /* hb2 */
|
|
o.t_fill(F, { 1 }, (uint32_t)RAD_FILL_SIGMOID, 0); /* order */
|
|
o.t(F, { M, n }); /* x, INOUT */
|
|
return o.finish("flow_res_multistep", operand, out);
|
|
}
|
|
int ref_gate_add_shape(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 uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M, n }); /* y */
|
|
o.t(D, { 1, n }); /* g */
|
|
o.t_idx({ 2 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu_seqlens */
|
|
o.t(D, { M, n }); /* x */
|
|
return o.finish("gate_add", operand, out);
|
|
}
|
|
int ref_timestep_embed_shape(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), G = gi(p, n_p, "curve", 0);
|
|
Opd o;
|
|
o.t_fill((uint32_t)RAD_F32, { M }, (uint32_t)RAD_FILL_SIGMOID, 0); /* t: a sigma in (0, 1) */
|
|
o.t(act_dt(p, n_p), { M, n }); /* y */
|
|
if (G > 0) o.t((uint32_t)RAD_F32, { G, n }); else o.skip(); /* table */
|
|
return o.finish("timestep_embed", operand, out);
|
|
}
|
|
int ref_gather_rows2_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
(void)p; (void)n_p; (void)operand; (void)out;
|
|
return RAD_E_UNSUPPORTED; /* a signed index: half its draw would have to be negative */
|
|
}
|
|
/* ---- the per-lane ops. A lane is one image's slot of a state buffer, `stride` rows apart; each
|
|
* entry reads or writes its own lane. Four lanes leaves some untouched, so a write into the wrong
|
|
* lane shows up. Entries are as many as M needs at `stride` rows each, split by cu_seqlens. */
|
|
enum { REF_LANES = 4 };
|
|
static int64_t lane_entries(const RadParam* p, int n_p, int64_t M, int64_t stride) {
|
|
const int64_t s = n_seq_of(p, n_p), need = (M + stride - 1) / stride;
|
|
return std::min<int64_t>(std::max(s, need), REF_LANES);
|
|
}
|
|
|
|
int ref_lane_gather_shape(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), st = gi(p, n_p, "stride", 0);
|
|
if (M <= 0 || n <= 0 || st <= 0) return RAD_E_UNSUPPORTED;
|
|
const int64_t S = lane_entries(p, n_p, M, st);
|
|
if (M > S * st) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { REF_LANES * st, n }); /* src: every lane */
|
|
o.t_idx({ S }, REF_LANES, -1, 0); /* lane: may repeat */
|
|
o.t_idx({ S + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu_seqlens */
|
|
o.t(D, { M, n }); /* y */
|
|
return o.finish("lane_gather", operand, out);
|
|
}
|
|
|
|
int ref_lane_store_shape(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), st = gi(p, n_p, "stride", 0);
|
|
if (M <= 0 || n <= 0 || st <= 0 || M > st) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M, n }); /* src */
|
|
/* Distinct, two of the four: one image stored to the lanes of its guided pair. */
|
|
o.t_idx({ 2 }, REF_LANES, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE); /* lane */
|
|
o.t(D, { REF_LANES * st, n }); /* dst, INOUT */
|
|
return o.finish("lane_store", operand, out);
|
|
}
|
|
|
|
int ref_randn_lanes_shape(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), st = gi(p, n_p, "stride", 0);
|
|
if (M <= 0 || n <= 0 || st <= 0) return RAD_E_UNSUPPORTED;
|
|
const int64_t S = lane_entries(p, n_p, M, st);
|
|
if (M > S * st) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
/* Flags drawn over all three bits, so only some entries are first steps. */
|
|
o.t_idx({ S }, 8, -1, 0); /* flags */
|
|
o.t_idx({ 2 * S }, (int64_t)1 << 30, -1, 0); /* seed: two words an entry */
|
|
/* Distinct: two first steps in one lane would be a race between their seeds. */
|
|
o.t_idx({ S }, REF_LANES, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE); /* lane */
|
|
o.t_idx({ S + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu_seqlens */
|
|
o.t((uint32_t)RAD_BF16, { REF_LANES * st, n }); /* dst, INOUT */
|
|
return o.finish("randn_lanes", operand, out);
|
|
}
|
|
|
|
int ref_flow_euler_shape(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), st = gi(p, n_p, "stride", 0);
|
|
if (M <= 0 || n <= 0 || st <= 0) return RAD_E_UNSUPPORTED;
|
|
/* Two equal-length entries where M allows, so a pair drawn from {0, 1} is always legal. -1
|
|
* (unguided) and -2 (parked) are not drawable and are left to the engine's tests. */
|
|
const int64_t S = (M % 2 == 0 && M / 2 <= st) ? 2 : 1;
|
|
if (M > S * st) return RAD_E_UNSUPPORTED;
|
|
const uint32_t F = (uint32_t)RAD_F32, B = (uint32_t)RAD_BF16;
|
|
Opd o;
|
|
o.t(B, { M, n }); /* v */
|
|
o.t(F, { S }); /* sigma */
|
|
o.t(F, { S }); /* sigma_next */
|
|
o.t(F, { S }); /* cfg */
|
|
o.t_idx({ S }, S, -1, 0); /* pair */
|
|
o.t_idx({ S }, REF_LANES, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE); /* lane */
|
|
o.t_idx({ S }, 8, -1, 0); /* flags: preview and repaint drawn */
|
|
o.t_idx({ 2 * S }, (int64_t)1 << 30, -1, 0); /* seed */
|
|
o.t_idx({ S + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu_seqlens */
|
|
o.t(B, { REF_LANES * st, n }); /* inp_lat */
|
|
o.t(F, { REF_LANES * st }); /* inp_mask */
|
|
o.t(B, { REF_LANES * st, n }); /* state, INOUT */
|
|
o.t(B, { REF_LANES * st, n }); /* preview, INOUT */
|
|
return o.finish("flow_euler", operand, out);
|
|
}
|
|
|
|
/* The k-diffusion sampler's two ops over flow_euler's entries: two equal-length entries where M
|
|
* allows, the lanes f32, sigmas drawn in (0, 1) so every logarithm and root is of a positive
|
|
* value. Flags draw only the first step (bit 1): the preview needs its optional operand and
|
|
* neither op repaints; the hand-written cases take the rest. */
|
|
int ref_kd_input_shape(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), st = gi(p, n_p, "stride", 0);
|
|
if (M <= 0 || n <= 0 || st <= 0) return RAD_E_UNSUPPORTED;
|
|
const int64_t S = (M % 2 == 0 && M / 2 <= st) ? 2 : 1;
|
|
if (M > S * st) return RAD_E_UNSUPPORTED;
|
|
const uint32_t F = (uint32_t)RAD_F32;
|
|
Opd o;
|
|
o.t_fill(F, { S }, (uint32_t)RAD_FILL_SIGMOID, 0); /* sigma */
|
|
o.t_idx({ S }, S, -1, 0); /* pair */
|
|
o.t_idx({ S }, REF_LANES, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE); /* lane */
|
|
o.t_idx({ S }, 2, -1, 0); /* flags */
|
|
o.t_idx({ 2 * S }, (int64_t)1 << 30, -1, 0); /* seed */
|
|
o.t_idx({ S + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu_seqlens */
|
|
o.t(F, { REF_LANES * st, n }); /* state, INOUT */
|
|
o.t(act_dt(p, n_p), { M, n }); /* y */
|
|
return o.finish("kd_input", operand, out);
|
|
}
|
|
|
|
int ref_kd_step_shape(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), st = gi(p, n_p, "stride", 0);
|
|
if (M <= 0 || n <= 0 || st <= 0) return RAD_E_UNSUPPORTED;
|
|
const int64_t S = (M % 2 == 0 && M / 2 <= st) ? 2 : 1;
|
|
if (M > S * st) return RAD_E_UNSUPPORTED;
|
|
const uint32_t F = (uint32_t)RAD_F32;
|
|
Opd o;
|
|
o.t(act_dt(p, n_p), { M, n }); /* out */
|
|
o.t_fill(F, { S }, (uint32_t)RAD_FILL_SIGMOID, 0); /* sigma */
|
|
o.t_fill(F, { S }, (uint32_t)RAD_FILL_SIGMOID, 0); /* sigma_next */
|
|
o.t_fill(F, { S }, (uint32_t)RAD_FILL_SIGMOID, 0); /* sigma_prev */
|
|
o.t(F, { S }); /* cfg */
|
|
o.t_idx({ S }, S, -1, 0); /* pair */
|
|
o.t_idx({ S }, REF_LANES, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE); /* lane */
|
|
o.t_idx({ S }, 2, -1, 0); /* flags */
|
|
o.t_idx({ 2 * S }, (int64_t)1 << 30, -1, 0); /* seed */
|
|
o.t_idx({ S }, 3, -1, 0); /* sampler */
|
|
o.t_idx({ S }, 64, -1, 0); /* step */
|
|
o.t_idx({ S + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu_seqlens */
|
|
o.t(F, { REF_LANES * st, n }); /* state, INOUT */
|
|
o.t(F, { REF_LANES * st, n }); /* hist, INOUT */
|
|
o.t(F, { REF_LANES * st, n }); /* preview, INOUT */
|
|
return o.finish("kd_step", operand, out);
|
|
}
|
|
|
|
/* ---- an image decoder's convolutions and shortcuts. Sizes are read off the operands and M counts
|
|
* output pixels, so this picks eight columns where M allows (a row with both edges and an interior)
|
|
* and the input that produces that output; otherwise it declines. */
|
|
static int64_t cols_for(int64_t M, int64_t mult) {
|
|
for (int64_t w = 8; w >= 1; w /= 2)
|
|
if (w % mult == 0 && M % w == 0 && (M / w) % mult == 0) return w;
|
|
return 0;
|
|
}
|
|
|
|
int ref_conv2d_nhwc_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), cin = gi(p, n_p, "cin", 0), cout = gi(p, n_p, "cout", 0);
|
|
const int64_t k = gi(p, n_p, "k", 0), stride = gi(p, n_p, "stride", 1);
|
|
const int64_t up = gi(p, n_p, "up", 1);
|
|
const char* pad = gs(p, n_p, "pad");
|
|
if (M <= 0 || cin <= 0 || cout <= 0 || k <= 0 || stride <= 0 || up <= 0)
|
|
return RAD_E_UNSUPPORTED;
|
|
const bool down = pad && !std::strcmp(pad, "down");
|
|
const int64_t pads = down ? 1 : 2 * (k / 2);
|
|
const int64_t Wo = cols_for(M, 1), Ho = Wo ? M / Wo : 0;
|
|
if (!Wo) return RAD_E_UNSUPPORTED;
|
|
/* The smallest upsampled input the output needs, which must be a whole input at `up`. */
|
|
const int64_t Hv = (Ho - 1) * stride + k - pads, Wv = (Wo - 1) * stride + k - pads;
|
|
if (Hv <= 0 || Wv <= 0 || Hv % up || Wv % up) return RAD_E_UNSUPPORTED;
|
|
if ((Hv + pads - k) / stride + 1 != Ho || (Wv + pads - k) / stride + 1 != Wo)
|
|
return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { Hv / up, (Wv / up) * cin }); /* x */
|
|
o.t(D, { cout, cin, k, k }); /* w */
|
|
o.t(D, { cout }); /* b */
|
|
o.t(D, { Ho, Wo * cout }); /* y */
|
|
return o.finish("conv2d_nhwc", operand, out);
|
|
}
|
|
|
|
/* conv2d_nhwc's geometry with the receptive fields as y's rows: M output pixels of cin k k
|
|
* values. */
|
|
int ref_im2col_nhwc_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), cin = gi(p, n_p, "cin", 0), k = gi(p, n_p, "k", 0);
|
|
const int64_t stride = gi(p, n_p, "stride", 1), up = gi(p, n_p, "up", 1);
|
|
const char* pad = gs(p, n_p, "pad");
|
|
if (M <= 0 || cin <= 0 || k <= 0 || stride <= 0 || up <= 0) return RAD_E_UNSUPPORTED;
|
|
const bool down = pad && !std::strcmp(pad, "down");
|
|
const int64_t pads = down ? 1 : 2 * (k / 2);
|
|
const int64_t Wo = cols_for(M, 1), Ho = Wo ? M / Wo : 0;
|
|
if (!Wo) return RAD_E_UNSUPPORTED;
|
|
const int64_t Hv = (Ho - 1) * stride + k - pads, Wv = (Wo - 1) * stride + k - pads;
|
|
if (Hv <= 0 || Wv <= 0 || Hv % up || Wv % up) return RAD_E_UNSUPPORTED;
|
|
if ((Hv + pads - k) / stride + 1 != Ho || (Wv + pads - k) / stride + 1 != Wo)
|
|
return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { Hv / up, (Wv / up) * cin }); /* x */
|
|
o.t(D, { Ho * Wo, cin * k * k }); /* y */
|
|
return o.finish("im2col_nhwc", operand, out);
|
|
}
|
|
|
|
int ref_d2s_add_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), cin = gi(p, n_p, "cin", 0), cout = gi(p, n_p, "cout", 0);
|
|
if (M <= 0 || cin <= 0 || cout <= 0) return RAD_E_UNSUPPORTED;
|
|
const int64_t Wo = cols_for(M, 2);
|
|
if (!Wo) return RAD_E_UNSUPPORTED;
|
|
const int64_t Ho = M / Wo;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { Ho / 2, (Wo / 2) * cin }); /* x */
|
|
o.t(D, { Ho, Wo * cout }); /* y, INOUT */
|
|
return o.finish("d2s_add", operand, out);
|
|
}
|
|
|
|
int ref_s2d_avg_add_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), cin = gi(p, n_p, "cin", 0), cout = gi(p, n_p, "cout", 0);
|
|
if (M <= 0 || cin <= 0 || cout <= 0) return RAD_E_UNSUPPORTED;
|
|
const int64_t Wo = cols_for(M, 1), Ho = Wo ? M / Wo : 0;
|
|
if (!Wo) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { 2 * Ho, 2 * Wo * cin }); /* x */
|
|
o.t(D, { Ho, Wo * cout }); /* y, INOUT */
|
|
return o.finish("s2d_avg_add", operand, out);
|
|
}
|
|
|
|
/* ---- attention over a paged prefix and the step's own rows. The prefix is `ctx` deep, else 37
|
|
* (two whole blocks and part of a third at block size 16), exercising the block walk and the
|
|
* partial block. */
|
|
int ref_attn_prefix_self_shape(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);
|
|
const int64_t bs = gi(p, n_p, "block_size", 16);
|
|
const int64_t gqa = gi(p, n_p, "gqa", 1);
|
|
const int64_t nh = gi(p, n_p, "n_head", gqa);
|
|
if (M <= 0 || hd <= 0 || bs <= 0 || gqa <= 0 || nh <= 0 || nh % gqa) return RAD_E_UNSUPPORTED;
|
|
const int64_t kvh = nh / gqa, S = n_seq_of(p, n_p);
|
|
int64_t P = gi(p, n_p, "ctx", 0);
|
|
if (P <= 0) P = 37;
|
|
const int64_t max_blocks = (P + bs - 1) / bs;
|
|
const uint32_t Q = act_dt(p, n_p);
|
|
const uint32_t kv = dt_parse(gs(p, n_p, "kv_dtype"));
|
|
const uint32_t KV = kv == (uint32_t)RAD_DT_INVALID ? Q : kv;
|
|
/* `max_ctx` 0 declares the prefix-free form: no cache, block table or prefix lengths. */
|
|
const bool pre = gi(p, n_p, "max_ctx", -1) != 0;
|
|
const char* pv = gs(p, n_p, "pv");
|
|
const bool pv8 = pv && !std::strcmp(pv, "fp8");
|
|
if (pv8 && pre) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(Q, { M, nh, hd }); /* q */
|
|
o.t(Q, { M, kvh, hd }); /* k */
|
|
o.t(pv8 ? (uint32_t)RAD_F8E4M3 : Q, { M, kvh, hd }); /* v */
|
|
if (pre) {
|
|
o.t(KV, { S * max_blocks, kvh, bs, 2 * hd }); /* kv_cache */
|
|
o.t_idx({ S, max_blocks }, S * max_blocks, -1, 0); /* block_table: may repeat */
|
|
o.t_idx({ S }, 0, P, 0); /* seqused: the prefix depth */
|
|
} else {
|
|
o.skip();
|
|
o.skip();
|
|
o.skip();
|
|
}
|
|
o.t_idx({ S + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu_seqlens */
|
|
o.skip(); /* k_descale */
|
|
o.skip(); /* v_descale */
|
|
const char* qk = gs(p, n_p, "qk");
|
|
if (qk && !std::strcmp(qk, "i8")) {
|
|
o.t((uint32_t)RAD_I8, { M, kvh, hd }); /* k_i8 */
|
|
o.t((uint32_t)RAD_F32, { M, kvh }); /* k_scale */
|
|
} else {
|
|
o.skip();
|
|
o.skip();
|
|
}
|
|
o.t(Q, { M, nh, hd }); /* out */
|
|
if (pv8) o.t((uint32_t)RAD_F32, { kvh * hd }); /* v_scale: one a column */
|
|
else o.skip();
|
|
return o.finish("attn_prefix_self", operand, out);
|
|
}
|
|
|
|
int ref_chan_affine_shape(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 uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M, n });
|
|
o.t(D, { M, n });
|
|
return o.finish("chan_affine", operand, out);
|
|
}
|
|
int ref_l2norm_chan_shape(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 uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x */
|
|
o.t(D, { n }); /* gamma */
|
|
o.t(D, { M, n }); /* y */
|
|
return o.finish("l2norm_chan", operand, out);
|
|
}
|
|
int ref_to_rgba8_shape(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);
|
|
Opd o;
|
|
o.t(act_dt(p, n_p), { M, n });
|
|
o.t((uint32_t)RAD_U8, { M, n });
|
|
return o.finish("to_rgba8", operand, out);
|
|
}
|
|
int ref_gelu_tanh_shape(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 uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M, n });
|
|
o.t(D, { M, n });
|
|
return o.finish("gelu_tanh", operand, out);
|
|
}
|
|
int ref_gelu_quick_shape(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 uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M, n });
|
|
o.t(D, { M, n });
|
|
return o.finish("gelu_quick", operand, out);
|
|
}
|
|
int ref_scatter_add_rows_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);
|
|
Opd o;
|
|
o.t(D, { M, n }); /* v */
|
|
o.t_idx({ M }, 4 * M, -1, RAD_OPD_F_IDX_UNIQUE); /* idx, distinct destinations */
|
|
o.t(D, { 4 * M, n }); /* x, inout */
|
|
return o.finish("scatter_add_rows", operand, out);
|
|
}
|
|
|
|
/* ================================================================== video generation
|
|
*
|
|
* The request's geometry is a device operand, and a description can only draw an index operand
|
|
* as one value throughout, so every axis of `geom` is one extent c: a c x c x c grid (and, for
|
|
* vtile_stitch, a chunk that is its video's first and last). The ranged M is the output's rows;
|
|
* an M below what the grid writes declines, and rows past it are left as drawn. */
|
|
|
|
/* The tiles split_tiles (ref_video.cpp) lays over `len` latent units, 0 where none fit. */
|
|
static int64_t tile_count(int64_t len, int64_t tile, int64_t overlap) {
|
|
if (len <= 0 || tile <= 0 || overlap < 0 || overlap >= tile) return 0;
|
|
if (tile >= len) return len == tile ? 1 : 0;
|
|
int64_t nt = (len + tile - 1) / tile;
|
|
while (tile * nt - overlap * (nt - 1) - len < 0) ++nt;
|
|
return nt;
|
|
}
|
|
|
|
/* The encoder's split (ref_video.cpp split_tiles_axis) over one axis: the tile count and the
|
|
* tiles' extent sum along it, a lone tile short of `tile` if the axis is. -1 where the split
|
|
* refuses. The drawn encode planes are `axis_len` long, so the shape fns can size the tile
|
|
* operands exactly. */
|
|
static int64_t axis_split(int64_t len, int64_t tile, int64_t overlap, int64_t* ext) {
|
|
if (len <= 0 || tile <= 0 || overlap < 0 || overlap >= tile) return -1;
|
|
if (tile >= len) {
|
|
*ext = len;
|
|
return 1;
|
|
}
|
|
const int64_t nt = tile_count(len, tile, overlap);
|
|
/* The extents telescope: sum of the tiles = len + sum of the overlaps = `tile` nt. */
|
|
*ext = tile * nt;
|
|
return nt;
|
|
}
|
|
|
|
/* A 2 x 2 x 2 grid of patches. */
|
|
int ref_patch_unfold_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), C = gi(p, n_p, "C", 0);
|
|
const int64_t pt = gi(p, n_p, "pt", 0), ph = gi(p, n_p, "ph", 0), pw = gi(p, n_p, "pw", 0);
|
|
const int64_t c = 2, vol = pt * ph * pw;
|
|
if (C <= 0 || pt <= 0 || ph <= 0 || pw <= 0 || M < c * c * c * vol) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { c * c * c, C * vol }); /* x: patch rows */
|
|
o.t_idx({ 3 }, 0, c, 0); /* geom: (F, Hp, Wp) */
|
|
o.t(D, { M, C }); /* y: voxel rows */
|
|
return o.finish("patch_unfold", operand, out);
|
|
}
|
|
|
|
/* A latent of `frames` frames, as tall and as wide as it is long. */
|
|
int ref_vtile_gather_shape(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), tile = gi(p, n_p, "tile", 0);
|
|
const int64_t ov = gi(p, n_p, "overlap", 0), c = gi(p, n_p, "frames", 0);
|
|
const int64_t period = gi(p, n_p, "period", 0), n_reg = gi(p, n_p, "n_reg", 0);
|
|
const int64_t nt = tile_count(c, tile, ov);
|
|
if (n <= 0 || nt <= 0 || n_reg < 0 || period < c * tile * tile + n_reg ||
|
|
M < nt * nt * period)
|
|
return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { c * c * c, n }); /* x: voxel rows */
|
|
o.t(D, { n_reg > 0 ? n_reg : 1, n }); /* reg */
|
|
o.t_idx({ 3 }, 0, c, 0); /* geom: (frames, H, W) */
|
|
o.t(D, { M, n }); /* y */
|
|
return o.finish("vtile_gather", operand, out);
|
|
}
|
|
|
|
/* The smallest grid the chunk's frames and the tile fit. */
|
|
int ref_vtile_stitch_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), tile = gi(p, n_p, "tile", 0);
|
|
const int64_t ov = gi(p, n_p, "overlap", 0), S = gi(p, n_p, "scale", 0);
|
|
const int64_t pt = gi(p, n_p, "pt", 0), skip = gi(p, n_p, "skip", 0);
|
|
const int64_t keep = gi(p, n_p, "keep", 0), blend = gi(p, n_p, "blend", 0);
|
|
const int64_t period = gi(p, n_p, "period", 0);
|
|
if (tile <= 0 || S <= 0 || pt <= 0 || skip < 0 || keep <= 0 || blend < 0 || blend > keep)
|
|
return RAD_E_UNSUPPORTED;
|
|
const int64_t c = std::max(tile, (2 * skip + keep + blend + pt - 1) / pt);
|
|
const int64_t nt = tile_count(c, tile, ov), npx = c * S * c * S;
|
|
if (nt <= 0 || period < c * tile * tile || M < (keep + blend) * npx) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { nt * nt * period, 3 * pt * S * S }); /* x: the decoded tiles */
|
|
o.t_idx({ 5 }, 0, c, 0); /* geom: first and last set */
|
|
o.t(D, { blend > 0 ? blend * npx : 1, 3 }); /* prev, INOUT */
|
|
o.t((uint32_t)RAD_U8, { M, 3 }); /* y */
|
|
return o.finish("vtile_stitch", operand, out);
|
|
}
|
|
|
|
int ref_grid_pos3_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0);
|
|
if (M <= 0) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t((uint32_t)RAD_F32, { 3, M }); /* y: one row a component */
|
|
return o.finish("grid_pos3", operand, out);
|
|
}
|
|
|
|
/* ---- the latent upscaler. A volume is c x c x c voxels, c the largest whose cube M holds (the
|
|
* ranged M is the output's rows), so a 3 x 3 x 3 volume and up has an interior voxel. Every geom
|
|
* entry being c, resize_bilinear's description is the identity resize; its hand cases resize. */
|
|
static int64_t cube_side(int64_t M) {
|
|
int64_t c = 0;
|
|
while ((c + 1) * (c + 1) * (c + 1) <= M) ++c;
|
|
return c;
|
|
}
|
|
|
|
int ref_conv3d_ndhwc_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), cin = gi(p, n_p, "cin", 0), cout = gi(p, n_p, "cout", 0);
|
|
const int64_t k = gi(p, n_p, "k", 0), kt = gi(p, n_p, "kt", 0), c = cube_side(M);
|
|
if (c <= 0 || cin <= 0 || cout <= 0 || k <= 0 || kt <= 0 || !(k & 1) || !(kt & 1))
|
|
return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { c * c * c, cin }); /* x: voxel rows */
|
|
o.t(D, { cout, cin, kt, k, k }); /* w */
|
|
o.t(D, { cout }); /* b */
|
|
o.t_idx({ 3 }, 0, c, 0); /* geom: (T, H, W) */
|
|
o.t(D, { M, cout }); /* res */
|
|
o.t(D, { M, cout }); /* y */
|
|
return o.finish("conv3d_ndhwc", operand, out);
|
|
}
|
|
|
|
/* The modulated form; `stats` at the size the op requires. */
|
|
int ref_group_norm_shape(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), G = gi(p, n_p, "groups", 0);
|
|
if (M <= 0 || n <= 0 || G <= 0 || n % G) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x */
|
|
o.t(D, { n }); /* w */
|
|
o.t(D, { n }); /* b */
|
|
o.t(D, { 2 * n }); /* mod: [scale | shift] */
|
|
o.t((uint32_t)RAD_F32, { G * (2 + 3 * 1024) }); /* stats, INOUT */
|
|
o.t(D, { M, n }); /* y */
|
|
return o.finish("group_norm", operand, out);
|
|
}
|
|
|
|
int ref_tconv_dw_shape(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);
|
|
const int64_t c = cube_side(M);
|
|
if (c <= 0 || n <= 0 || k <= 0 || !(k & 1)) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { c * c * c, n }); /* x */
|
|
o.t(D, { n, 1, k, 1, 1 }); /* w */
|
|
o.t(D, { n }); /* b */
|
|
o.t_idx({ 3 }, 0, c, 0); /* geom: (T, H, W) */
|
|
o.t(D, { M, n }); /* y */
|
|
return o.finish("tconv_dw", operand, out);
|
|
}
|
|
|
|
int ref_resize_bilinear_shape(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), c = cube_side(M);
|
|
if (c <= 0 || n <= 0) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { c * c * c, n }); /* x */
|
|
o.t_idx({ 5 }, 0, c, 0); /* geom: (T, h, w, H, W) */
|
|
o.t(D, { M, n }); /* y */
|
|
return o.finish("resize_bilinear", operand, out);
|
|
}
|
|
|
|
/* patch_unfold's 2 x 2 x 2 grid of patches, folded. */
|
|
int ref_patch_fold_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), C = gi(p, n_p, "C", 0);
|
|
const int64_t pt = gi(p, n_p, "pt", 0), ph = gi(p, n_p, "ph", 0), pw = gi(p, n_p, "pw", 0);
|
|
const int64_t c = 2, vol = pt * ph * pw;
|
|
if (C <= 0 || pt <= 0 || ph <= 0 || pw <= 0 || M < c * c * c) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { c * c * c * vol, C }); /* x: voxel rows */
|
|
o.t_idx({ 3 }, 0, c, 0); /* geom: (F, Hp, Wp) */
|
|
o.t((uint32_t)RAD_F32, { M, C * vol }); /* y: patch rows */
|
|
return o.finish("patch_fold", operand, out);
|
|
}
|
|
|
|
/* sigma in (0, 1); the latent f32. */
|
|
int ref_flow_renoise_shape(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);
|
|
if (M <= 0 || n <= 0) return RAD_E_UNSUPPORTED;
|
|
const uint32_t F = (uint32_t)RAD_F32;
|
|
Opd o;
|
|
o.t_fill(F, { 1 }, (uint32_t)RAD_FILL_SIGMOID, 0); /* sigma */
|
|
o.t_idx({ 2 }, (int64_t)1 << 30, -1, 0); /* seed: two words */
|
|
o.t(F, { M, n }); /* x, INOUT */
|
|
return o.finish("flow_renoise", operand, out);
|
|
}
|
|
|
|
/* aug in [0, 1); the condition rows f32. */
|
|
int ref_cond_aug_shape(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 double aug = rad_param_getf(p, n_p, "aug", -1.0);
|
|
if (M <= 0 || n <= 0 || !(aug >= 0.0 && aug < 1.0)) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t_idx({ 2 }, (int64_t)1 << 30, -1, 0); /* seed: two words */
|
|
o.t((uint32_t)RAD_F32, { M, n }); /* x, INOUT */
|
|
return o.finish("cond_aug", operand, out);
|
|
}
|
|
|
|
/* ---- the tiled encoder. The drawn frame is 20 latent units a side; with the sweep's tile of
|
|
* 16 and overlap of 4 the split lays 2 x 2 full tiles over it, so the crops, the plan's level
|
|
* triples and the blends all draw non-trivial. The kernels size by the geom; these only draw a
|
|
* plane that one gives them. */
|
|
int ref_vtile_plan_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), tile = gi(p, n_p, "tile", 0);
|
|
const int64_t ov = gi(p, n_p, "overlap", 0), ratio = gi(p, n_p, "ratio", 0);
|
|
int64_t e = 0;
|
|
const int64_t nt = axis_split(20, tile, ov, &e);
|
|
if (M <= 0 || nt < 0 || ratio <= 0) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t_idx({ 3 }, 0, 20 * ratio, 0); /* geom: (_, H, W) pixels */
|
|
o.t((uint32_t)RAD_I32, { M, 22 }); /* y: one row a tile */
|
|
return o.finish("vtile_plan", operand, out);
|
|
}
|
|
|
|
int ref_vtile_crop_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), tile = gi(p, n_p, "tile", 0);
|
|
const int64_t ov = gi(p, n_p, "overlap", 0), ratio = gi(p, n_p, "ratio", 0);
|
|
int64_t e = 0;
|
|
const int64_t nt = axis_split(20, tile, ov, &e);
|
|
if (M <= 0 || nt < 0 || ratio <= 0) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { 20 * ratio * (20 * ratio), 3 }); /* x: the pixel plane */
|
|
o.t_idx({ 3 }, 0, 20 * ratio, 0); /* geom: (_, H, W) pixels */
|
|
o.t(D, { M, 3 }); /* y: the tiles' pixels */
|
|
return o.finish("vtile_crop", operand, out);
|
|
}
|
|
|
|
int ref_vtile_join_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), tile = gi(p, n_p, "tile", 0);
|
|
const int64_t ov = gi(p, n_p, "overlap", 0), n = gi(p, n_p, "n", 0);
|
|
int64_t e = 0;
|
|
const int64_t nt = axis_split(20, tile, ov, &e);
|
|
if (M <= 0 || nt < 0 || n <= 0) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { e * e, n }); /* x: the tiles' moments */
|
|
o.t_idx({ 3 }, 0, 20, 0); /* geom: (_, lat_h, lat_w) */
|
|
o.t(D, { M, n }); /* y: the canvas */
|
|
return o.finish("vtile_join", operand, out);
|
|
}
|
|
|
|
/* ================================================================== video sparse attention
|
|
*
|
|
* The tiles come from the values of `segs` and `geom`, which a description draws only as
|
|
* cumulative lengths and as one value c in every entry. So the grid is a cube of c frames and
|
|
* c / patch rows and columns, c the smallest that gives every axis an edge box (more than one box,
|
|
* the last short), and `segs` cuts M into M / (F Hp Wp) segments of the grid's rows each, the last
|
|
* the video; an M not a multiple of the grid declines. `n_head` (default 2) and `dtype` (default
|
|
* bf16) are the description's, as is `coarse`: 0 leaves the optional operands out.
|
|
*
|
|
* attn_vsa's `sel` is drawn as one value, the first video tile, which is a selection only at
|
|
* k = 1; any other sparsity declines (the hand-built cases build selections,
|
|
* libavx/avx_check_hand.cpp). */
|
|
namespace {
|
|
|
|
struct VsaGeo {
|
|
int64_t M, hd, nh, c, n_seg, P, V, N, k;
|
|
uint32_t D;
|
|
bool coarse;
|
|
};
|
|
|
|
/* An axis of `len` units over boxes of `t` has more than one box, the last short; a box of one
|
|
* unit needs only a second unit. */
|
|
bool vsa_edge(int64_t len, int64_t t) { return t > 1 ? len > t && len % t != 0 : len > 1; }
|
|
|
|
int vsa_geo(const RadParam* p, int n_p, VsaGeo* g) {
|
|
g->M = gi(p, n_p, "M", 0);
|
|
g->hd = gi(p, n_p, "head_dim", 0);
|
|
g->nh = gi(p, n_p, "n_head", 2);
|
|
const int64_t tt = gi(p, n_p, "tile_t", 0), th = gi(p, n_p, "tile_h", 0);
|
|
const int64_t tw = gi(p, n_p, "tile_w", 0), patch = gi(p, n_p, "patch", 0);
|
|
const double sparsity = rad_param_getf(p, n_p, "sparsity", -1.0);
|
|
if (g->M <= 0 || g->hd <= 0 || g->nh <= 0 || tt <= 0 || th <= 0 || tw <= 0 || patch <= 0 ||
|
|
!(sparsity >= 0.0 && sparsity < 1.0))
|
|
return RAD_E_UNSUPPORTED;
|
|
int64_t c = 1;
|
|
while (c <= 4096 && !(vsa_edge(c, tt) && vsa_edge(c / patch, th) && vsa_edge(c / patch, tw)))
|
|
++c;
|
|
const int64_t hp = c / patch, rows = c * hp * hp, TT = tt * th * tw;
|
|
if (c > 4096 || g->M % rows != 0) return RAD_E_UNSUPPORTED;
|
|
g->c = c;
|
|
g->n_seg = g->M / rows;
|
|
g->P = (g->n_seg - 1) * ((rows + TT - 1) / TT);
|
|
g->V = ((c + tt - 1) / tt) * ((hp + th - 1) / th) * ((hp + tw - 1) / tw);
|
|
g->N = g->P + g->V;
|
|
const double keep = (1.0 - sparsity) * (double)g->V;
|
|
g->k = std::max<int64_t>(1, std::min<int64_t>((int64_t)std::ceil(keep), g->V));
|
|
g->D = act_dt(p, n_p);
|
|
g->coarse = gi(p, n_p, "coarse", 1) != 0;
|
|
return RAD_OK;
|
|
}
|
|
|
|
} /* namespace */
|
|
|
|
int ref_vsa_select_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
VsaGeo g{};
|
|
if (vsa_geo(p, n_p, &g) != RAD_OK) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(g.D, { g.M, g.nh, g.hd }); /* q */
|
|
o.t(g.D, { g.M, g.nh, g.hd }); /* k */
|
|
if (g.coarse) o.t(g.D, { g.M, g.nh, g.hd }); else o.skip(); /* v */
|
|
o.t_idx({ g.n_seg + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* segs */
|
|
o.t_idx({ RAD_VGEOM_N }, 0, g.c, 0); /* geom: c everywhere */
|
|
/* A tile row and a slot past the selection's, which nothing writes. */
|
|
o.t((uint32_t)RAD_I32, { g.nh, g.N + 1, g.k + 1 }); /* sel */
|
|
if (g.coarse) o.t(g.D, { g.nh, g.N + 1, g.hd }); else o.skip(); /* coarse */
|
|
return o.finish("vsa_select", operand, out);
|
|
}
|
|
|
|
int ref_attn_vsa_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
VsaGeo g{};
|
|
if (vsa_geo(p, n_p, &g) != RAD_OK || g.k != 1) return RAD_E_UNSUPPORTED;
|
|
const char* pv = gs(p, n_p, "pv");
|
|
const bool pv8 = pv && !std::strcmp(pv, "fp8");
|
|
Opd o;
|
|
o.t(g.D, { g.M, g.nh, g.hd }); /* q */
|
|
o.t(g.D, { g.M, g.nh, g.hd }); /* k */
|
|
o.t(pv8 ? (uint32_t)RAD_F8E4M3 : g.D, { g.M, g.nh, g.hd }); /* v */
|
|
o.t_idx({ g.n_seg + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* segs */
|
|
o.t_idx({ RAD_VGEOM_N }, 0, g.c, 0); /* geom: c everywhere */
|
|
o.t_idx({ g.nh, g.N, 1 }, g.N, g.P, 0); /* sel: the first video tile */
|
|
if (g.coarse) {
|
|
o.t(g.D, { g.nh, g.N, g.hd }); /* coarse */
|
|
o.t(g.D, { g.M, g.nh, g.hd }); /* gate */
|
|
} else {
|
|
o.skip();
|
|
o.skip();
|
|
}
|
|
o.t(g.D, { g.M, g.nh, g.hd }); /* out */
|
|
const char* qk = gs(p, n_p, "qk");
|
|
if (qk && !std::strcmp(qk, "i8")) {
|
|
o.t((uint32_t)RAD_I8, { g.M, g.nh, g.hd }); /* k_i8 */
|
|
o.t((uint32_t)RAD_F32, { g.M, g.nh }); /* k_scale */
|
|
} else {
|
|
o.skip();
|
|
o.skip();
|
|
}
|
|
if (pv8) o.t((uint32_t)RAD_F32, { g.nh * g.hd }); /* v_scale: one a column */
|
|
else o.skip();
|
|
return o.finish("attn_vsa", operand, out);
|
|
}
|
|
|
|
/* ================================================================== audio generation
|
|
* Sequenced ops (the convolutions) are described over two sequences where M allows; lane ops over
|
|
* REF_LANES lanes, as the image generator's are. */
|
|
static int64_t two_seqs(int64_t M) { return M >= 4 ? 2 : 1; }
|
|
|
|
/* `M` is the input rows; with `stride` > 1 (bf16, one sequence, explicit `pad`) the output is
|
|
* PyTorch's strided length, `M` being the rows of the one sequence `cu_seqlens` must cover. */
|
|
int ref_conv1d_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), cin = gi(p, n_p, "cin", 0), cout = gi(p, n_p, "cout", 0);
|
|
const int64_t k = gi(p, n_p, "k", 0), causal = gi(p, n_p, "causal", 0);
|
|
const int64_t st = gi(p, n_p, "stride", 1), pp = gi(p, n_p, "pad", -1);
|
|
const int64_t dil = gi(p, n_p, "dil", 1);
|
|
const bool padded = pp >= 0 && st > 1; /* the zero-padded strided form (explicit `pad`) */
|
|
if (M <= 0 || cin <= 0 || cout <= 0 || k <= 0 || (!causal && st <= 1 && (k & 1) == 0) ||
|
|
st < 1 || (pp >= 0 && (st <= 1 || causal)) ||
|
|
(st > 1 && pp < 0 && (!causal || dil * (k - 1) + 1 < st || M % st)) ||
|
|
(!padded && gi(p, n_p, "cu_div", 1) != 1))
|
|
return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
if (padded && D != (uint32_t)RAD_BF16) return RAD_E_UNSUPPORTED;
|
|
/* A strided convolution is described over one sequence: two would each need a whole number
|
|
* of strides. Bounds in coarser units (cu_div) are not drawn; ref_test checks them. */
|
|
const int64_t S = st > 1 ? 1 : two_seqs(M);
|
|
const int64_t O = padded ? (M + 2 * pp - dil * (k - 1) - 1) / st + 1 : M / st;
|
|
if (O <= 0) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(D, { M, cin }); /* x */
|
|
o.t(D, { cout, cin, k }); /* w */
|
|
o.t_idx({ S + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu_seqlens */
|
|
o.t(D, { O, cout }); /* res */
|
|
o.t(D, { O, cout }); /* y */
|
|
o.t(D, { cout }); /* b */
|
|
return o.finish("conv1d", operand, out);
|
|
}
|
|
|
|
int ref_conv_t1d_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), cin = gi(p, n_p, "cin", 0), cout = gi(p, n_p, "cout", 0);
|
|
const int64_t s = gi(p, n_p, "stride", 0), k = gi(p, n_p, "k", 2 * s);
|
|
const int64_t pad = gi(p, n_p, "pad", -1);
|
|
if (M <= 0 || cin <= 0 || cout <= 0 || s <= 0 || k < s || (pad < 0 && ((k - s) & 1)))
|
|
return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M, cin }); /* x */
|
|
o.t(D, { cin, cout, k }); /* w */
|
|
o.t(D, { cout }); /* b */
|
|
o.t_idx({ two_seqs(M) + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu_seqlens */
|
|
o.t(D, { M * s, cout }); /* y */
|
|
return o.finish("conv_t1d", operand, out);
|
|
}
|
|
|
|
int ref_snake_shape(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 uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x */
|
|
/* alpha in (0, 1): a trained alpha is positive, and 1 / alpha near zero is no test. */
|
|
o.t_fill((uint32_t)RAD_F32, { n }, (uint32_t)RAD_FILL_SIGMOID, 0); /* alpha */
|
|
o.t(D, { M, n }); /* y */
|
|
o.skip(); /* beta */
|
|
return o.finish("snake", operand, out);
|
|
}
|
|
|
|
int ref_snake_aa_shape(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 R = gi(p, n_p, "ratio", 0), k = gi(p, n_p, "k", 0);
|
|
if (M <= 0 || n <= 0 || R <= 0 || k < R) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p), F = (uint32_t)RAD_F32;
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x */
|
|
o.t_fill(F, { n }, (uint32_t)RAD_FILL_SIGMOID, 0); /* alpha */
|
|
o.t_fill(F, { n }, (uint32_t)RAD_FILL_SIGMOID, 0); /* beta */
|
|
o.t(F, { 1, 1, k }); /* f_up */
|
|
o.t(F, { 1, 1, k }); /* f_down */
|
|
o.t_idx({ two_seqs(M) + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu_seqlens */
|
|
o.t(D, { M, n }); /* y */
|
|
return o.finish("snake_aa", operand, out);
|
|
}
|
|
|
|
int ref_cfg_topk_sample_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), V = gi(p, n_p, "n_vocab", 0), rep = gi(p, n_p, "rep", 1);
|
|
if (M <= 0 || V <= 0 || rep < 1 || M % rep) return RAD_E_UNSUPPORTED;
|
|
const int64_t n = M / rep;
|
|
Opd o;
|
|
o.t((uint32_t)RAD_F32, { M, V }); /* logits */
|
|
o.t_idx({ 2 * n }, (int64_t)1 << 30, -1, 0); /* seed */
|
|
o.t_idx({ n }, 9000, -1, 0); /* frame */
|
|
o.t_idx({ M }, V, -1, 0); /* ids */
|
|
o.skip(); /* cfg */
|
|
o.skip(); /* lane */
|
|
o.skip(); /* hist */
|
|
return o.finish("cfg_topk_sample", operand, out);
|
|
}
|
|
|
|
int ref_rvq_embed_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), D = gi(p, n_p, "n_embd", 0);
|
|
const int64_t K = gi(p, n_p, "n_codes", 0), voc = gi(p, n_p, "vocab", 0);
|
|
const int64_t off0 = gi(p, n_p, "off0", 0);
|
|
if (M <= 0 || D <= 0 || K < 1 || voc <= 0 || off0 < 0) return RAD_E_UNSUPPORTED;
|
|
const uint32_t B = (uint32_t)RAD_BF16;
|
|
Opd o;
|
|
o.t_idx({ K, M }, voc, -1, 0); /* codes */
|
|
o.t(B, { off0 + voc, D }); /* t0 */
|
|
o.t(B, { (K > 1 ? K - 1 : 1) * voc, D }); /* t1 */
|
|
o.t(act_dt(p, n_p), { M, D }); /* y */
|
|
return o.finish("rvq_embed", operand, out);
|
|
}
|
|
|
|
int ref_vq_encode_shape(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 dim = gi(p, n_p, "dim", 0), bins = gi(p, n_p, "bins", 0);
|
|
if (M <= 0 || n <= 0 || dim <= 0 || bins <= 0) return RAD_E_UNSUPPORTED;
|
|
const uint32_t F = (uint32_t)RAD_F32;
|
|
Opd o;
|
|
o.t(act_dt(p, n_p), { M, n }); /* x, INOUT */
|
|
o.t(F, { dim, n }); /* in_w */
|
|
o.t(F, { dim }); /* in_b */
|
|
o.t(F, { bins, dim }); /* cb */
|
|
o.t(F, { n, dim }); /* out_w */
|
|
o.t(F, { n }); /* out_b */
|
|
o.t_idx({ M }, bins, -1, 0); /* codes, OUT */
|
|
return o.finish("vq_encode", operand, out);
|
|
}
|
|
|
|
int ref_frame_mix_store_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), D = gi(p, n_p, "n_embd", 0), L = gi(p, n_p, "L", 0);
|
|
const int64_t st = gi(p, n_p, "stride", 0);
|
|
if (M <= 0 || D <= 0 || L <= 0 || st <= 0 || M > REF_LANES) return RAD_E_UNSUPPORTED;
|
|
const uint32_t A = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(A, { L * 2 * M, D }); /* hid: two branch rows a lane */
|
|
o.t((uint32_t)RAD_F32, { L }); /* logits */
|
|
o.t_fill((uint32_t)RAD_F32, { 1 }, (uint32_t)RAD_FILL_SIGMOID, 0); /* scale */
|
|
o.t_idx({ M }, REF_LANES, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE); /* lane */
|
|
o.t_idx({ M }, st + 1, -1, 0); /* frame, 0 included */
|
|
o.t_idx({ REF_LANES }, 8, -1, 0); /* status */
|
|
o.t(A, { REF_LANES * st, D }); /* dst, INOUT */
|
|
return o.finish("frame_mix_store", operand, out);
|
|
}
|
|
|
|
int ref_end_status_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), rep = gi(p, n_p, "rep", 1), end = gi(p, n_p, "end", 0);
|
|
if (M <= 0 || rep < 1 || M > REF_LANES) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
/* Ids over [0, 2 end): about half the lanes end. */
|
|
o.t_idx({ rep * M }, end > 0 ? 2 * end : 2, -1, 0); /* ids */
|
|
o.t_idx({ M }, REF_LANES, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE); /* lane */
|
|
o.t_idx({ REF_LANES }, 8, -1, 0); /* status, INOUT */
|
|
return o.finish("end_status", operand, out);
|
|
}
|
|
|
|
int ref_ras_pick_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), rep = gi(p, n_p, "rep", 1), W = gi(p, n_p, "win", 0);
|
|
const int64_t hi = gi(p, n_p, "hi", 0);
|
|
if (M <= 0 || rep < 1 || W <= 0 || hi <= 0 || M > REF_LANES) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
/* Picks over a small band so the windows repeat them often enough to exercise the redraw. */
|
|
const int64_t band = std::min<int64_t>(hi + 2, 12);
|
|
o.t_idx({ rep * M }, band, -1, 0); /* first */
|
|
o.t_idx({ rep * M }, band, -1, 0); /* alt */
|
|
o.t_idx({ M }, REF_LANES, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE); /* lane */
|
|
o.t_idx({ REF_LANES * W }, band, -1, 0); /* hist, INOUT */
|
|
o.t_idx({ rep * M }, band, -1, 0); /* ids, OUT */
|
|
return o.finish("ras_pick", operand, out);
|
|
}
|
|
|
|
int ref_frame_store_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), K = gi(p, n_p, "n_codes", 0);
|
|
const int64_t st = gi(p, n_p, "stride", 0), rep = gi(p, n_p, "rep", 1);
|
|
if (M <= 0 || K <= 0 || st <= 0 || rep < 1 || M > REF_LANES) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t_idx({ K, rep * M }, 2048, -1, 0); /* codes */
|
|
o.t_idx({ M }, REF_LANES, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE); /* lane */
|
|
o.t_idx({ M }, st, -1, 0); /* frame */
|
|
o.t_idx({ REF_LANES }, 8, -1, 0); /* status */
|
|
o.t_idx({ REF_LANES * K, st }, 2048, -1, 0); /* dst, INOUT */
|
|
return o.finish("frame_store", operand, out);
|
|
}
|
|
|
|
int ref_resample_nearest_shape(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);
|
|
if (M <= 0 || n <= 0) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M / 3 + 1, n }); /* x: fewer rows than y */
|
|
o.t(D, { M, n }); /* y */
|
|
return o.finish("resample_nearest", operand, out);
|
|
}
|
|
|
|
int ref_elu_shape(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);
|
|
if (M <= 0 || n <= 0) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(act_dt(p, n_p), {M, n});
|
|
o.t(act_dt(p, n_p), {M, n});
|
|
return o.finish("elu", operand, out);
|
|
}
|
|
|
|
int ref_vq_euclidean_shape(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), bins = gi(p, n_p, "bins", 0);
|
|
if (M <= 0 || n <= 0 || bins <= 0) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(act_dt(p, n_p), {M, n});
|
|
o.t(act_dt(p, n_p), {bins, n});
|
|
o.t_idx({M}, bins, -1, 0);
|
|
return o.finish("vq_euclidean", operand, out);
|
|
}
|
|
|
|
int ref_fourier_embed_shape(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);
|
|
if (M <= 0 || n <= 0 || (n & 1)) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t_fill((uint32_t)RAD_F32, { M }, (uint32_t)RAD_FILL_SIGMOID, 0); /* t in (0, 1) */
|
|
o.t((uint32_t)RAD_F32, { n / 2 }); /* w */
|
|
o.t(act_dt(p, n_p), { M, n }); /* y */
|
|
return o.finish("fourier_embed", operand, out);
|
|
}
|
|
|
|
int ref_win_input_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), C = gi(p, n_p, "n_lat", 0), Z = gi(p, n_p, "n_zero", 0);
|
|
const int64_t Dc = gi(p, n_p, "n_cond", 0), st = gi(p, n_p, "stride", 0);
|
|
if (M <= 0 || C <= 0 || Z < 0 || Dc <= 0 || st <= 0) return RAD_E_UNSUPPORTED;
|
|
const int64_t S = lane_entries(p, n_p, M, st);
|
|
if (M > S * st) return RAD_E_UNSUPPORTED;
|
|
const uint32_t A = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(A, { REF_LANES * st, C }); /* lat */
|
|
o.t(A, { REF_LANES * st, Dc }); /* cond */
|
|
o.t_idx({ S + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* lat_cu */
|
|
o.t_idx({ S }, REF_LANES, -1, 0); /* win_lane */
|
|
o.t_idx({ S }, S, -1, 0); /* pair */
|
|
o.t(A, { M, C + Z + Dc }); /* y */
|
|
return o.finish("win_input", operand, out);
|
|
}
|
|
|
|
int ref_win_prep_shape(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), st = gi(p, n_p, "stride", 0);
|
|
const int64_t ov = gi(p, n_p, "ov", 0);
|
|
if (M <= 0 || n <= 0 || st <= 0 || ov <= 0 || ov > st) return RAD_E_UNSUPPORTED;
|
|
const int64_t S = lane_entries(p, n_p, M, st);
|
|
if (M > S * st) return RAD_E_UNSUPPORTED;
|
|
const uint32_t B = (uint32_t)RAD_BF16;
|
|
Opd o;
|
|
o.t_idx({ S }, 32, -1, 0); /* flags: every bit drawn */
|
|
o.t_fill((uint32_t)RAD_F32, { S }, (uint32_t)RAD_FILL_SIGMOID, 0); /* t */
|
|
o.t_idx({ 2 * S }, (int64_t)1 << 30, -1, 0); /* seed */
|
|
o.t_idx({ S }, 64, -1, 0); /* win_index */
|
|
/* Distinct: two entries moving one lane would race. */
|
|
o.t_idx({ S }, REF_LANES, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE); /* win_lane */
|
|
o.t_idx({ S }, S, -1, 0); /* pair */
|
|
o.t_idx({ S + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* lat_cu */
|
|
o.t(B, { REF_LANES * ov, n }); /* prev */
|
|
o.t(B, { REF_LANES * ov, n }); /* noise, INOUT */
|
|
o.t(B, { REF_LANES * st, n }); /* lat, INOUT */
|
|
return o.finish("win_prep", operand, out);
|
|
}
|
|
|
|
int ref_win_finish_shape(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), Dc = gi(p, n_p, "n_cond", 0);
|
|
const int64_t st = gi(p, n_p, "stride", 0), ov = gi(p, n_p, "ov", 0);
|
|
if (M <= 0 || n <= 0 || Dc <= 0 || st <= 0 || ov <= 0 || ov > st) return RAD_E_UNSUPPORTED;
|
|
const int64_t S = lane_entries(p, n_p, M, st);
|
|
if (M > S * st) return RAD_E_UNSUPPORTED;
|
|
const uint32_t B = (uint32_t)RAD_BF16;
|
|
Opd o;
|
|
o.t_idx({ S }, 32, -1, 0); /* flags */
|
|
o.t_idx({ S }, S, -1, 0); /* pair */
|
|
o.t_idx({ S }, REF_LANES, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE); /* win_lane */
|
|
o.t_idx({ S + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* lat_cu */
|
|
o.t(B, { REF_LANES * st, Dc }); /* cond */
|
|
o.t(B, { REF_LANES * st, n }); /* lat, INOUT */
|
|
o.t(B, { REF_LANES * ov, n }); /* prev, INOUT */
|
|
o.t(B, { REF_LANES * ov, Dc }); /* prev_cond, INOUT */
|
|
return o.finish("win_finish", operand, out);
|
|
}
|
|
|
|
int ref_chan_split_shape(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), P = gi(p, n_p, "parts", 0);
|
|
if (M <= 0 || n <= 0 || P <= 0) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M, P * n }); /* x */
|
|
o.t(D, { P * M, n }); /* y */
|
|
return o.finish("chan_split", operand, out);
|
|
}
|
|
|
|
int ref_interleave2_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0);
|
|
if (M <= 0) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M }); /* a */
|
|
o.t(D, { M }); /* b */
|
|
o.t((uint32_t)RAD_F32, { M, 2 }); /* y */
|
|
return o.finish("interleave2", operand, out);
|
|
}
|
|
|
|
int ref_iota_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0);
|
|
if (M <= 0) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t_idx({ M }, 1, -1, 0); /* y */
|
|
return o.finish("iota", operand, out);
|
|
}
|
|
|
|
int ref_cu_repeat_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0);
|
|
if (M <= 0) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t_idx({ 1 }, 1000, -1, 0); /* len */
|
|
o.t_idx({ M }, 1, -1, 0); /* y */
|
|
return o.finish("cu_repeat", operand, out);
|
|
}
|
|
|
|
int ref_int_affine_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0);
|
|
if (M <= 0) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t_idx({ 16 }, 1000, -1, 0); /* x */
|
|
o.t_idx({ M }, 1, -1, 0); /* y */
|
|
return o.finish("int_affine", operand, out);
|
|
}
|
|
|
|
int ref_sub_i32_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0);
|
|
if (M <= 0) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t_idx({ M }, 100000, -1, 0); /* x */
|
|
o.t_idx({ 1 }, 1000, -1, 0); /* s */
|
|
o.t_idx({ M }, 1, -1, 0); /* y */
|
|
return o.finish("sub_i32", operand, out);
|
|
}
|
|
|
|
/* ================================================================== LTX-2 (ref_ltx.cpp) */
|
|
int ref_rope_split_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), nh = gi(p, n_p, "n_head", 0);
|
|
const int64_t hd = gi(p, n_p, "head_dim", 0), nd = gi(p, n_p, "n_dims", 0);
|
|
if (M <= 0 || nh <= 0 || hd <= 0 || nd <= 0) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(act_dt(p, n_p), { M, nh * hd }); /* x, INOUT */
|
|
o.t((uint32_t)RAD_F32, { nd, M }); /* positions */
|
|
return o.finish("rope_split", operand, out);
|
|
}
|
|
|
|
int ref_head_gate_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), nh = gi(p, n_p, "n_head", 0);
|
|
const int64_t hd = gi(p, n_p, "head_dim", 0);
|
|
if (M <= 0 || nh <= 0 || hd <= 0) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M, nh }); /* g */
|
|
o.t(D, { M, nh * hd }); /* x, INOUT */
|
|
return o.finish("head_gate", operand, out);
|
|
}
|
|
|
|
/* The four guidance passes bound, scales and sigmas in (0, 1): every term taken. */
|
|
int ref_ltx_guide_shape(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);
|
|
if (M <= 0 || n <= 0) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p), F = (uint32_t)RAD_F32;
|
|
Opd o;
|
|
for (int i = 0; i < 4; ++i) o.t(D, { M, n }); /* vc, vu, vs, vm */
|
|
o.t_fill(F, { 4 }, (uint32_t)RAD_FILL_SIGMOID, 0); /* scales */
|
|
o.t_fill(F, { 2 }, (uint32_t)RAD_FILL_SIGMOID, 0); /* sched */
|
|
o.t(F, { kLtxGuideStats }); /* stats, INOUT */
|
|
o.t(F, { M, n }); /* x, INOUT */
|
|
return o.finish("ltx_guide", operand, out);
|
|
}
|
|
|
|
/* The rest of LTX-2's: its VAE's norm, resampling and patches, its vocoder's spectrogram and
|
|
* resampler. A volume op's `geom` is the cube of side cube_side(M), M its input's rows. */
|
|
int ref_pixel_norm_shape(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), L = gi(p, n_p, "ileave", 1);
|
|
if (M <= 0 || n <= 0 || L <= 0 || M % L) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M, n }); /* x */
|
|
o.t(D, { M / L, n * L }); /* y */
|
|
return o.finish("pixel_norm", operand, out);
|
|
}
|
|
|
|
int ref_d2s3d_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), C = gi(p, n_p, "C", 0), c = cube_side(M);
|
|
const int64_t st = gi(p, n_p, "st", 1), sh = gi(p, n_p, "sh", 1), sw = gi(p, n_p, "sw", 1);
|
|
if (c <= 0 || C <= 0 || st <= 0 || sh <= 0 || sw <= 0) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { c * c * c, C * st * sh * sw }); /* x */
|
|
o.t_idx({ 3 }, 0, c, 0); /* geom */
|
|
o.t(D, { (c * st - st + 1) * c * sh * c * sw, C }); /* y */
|
|
return o.finish("d2s3d", operand, out);
|
|
}
|
|
|
|
int ref_s2d3d_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), C = gi(p, n_p, "C", 0), c = cube_side(M);
|
|
const int64_t st = gi(p, n_p, "st", 1), sh = gi(p, n_p, "sh", 1), sw = gi(p, n_p, "sw", 1);
|
|
const int64_t tpre = gi(p, n_p, "tpre", 0), g = gi(p, n_p, "group", 1);
|
|
const char* mode = gs(p, n_p, "mode");
|
|
const bool mean = mode && !std::strcmp(mode, "mean");
|
|
const int64_t P = C * st * sh * sw;
|
|
if (c <= 0 || C <= 0 || st <= 0 || sh <= 0 || sw <= 0 || tpre < 0 || g <= 0 ||
|
|
(c + tpre) % st || c % sh || c % sw || (mean && P % g))
|
|
return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { c * c * c, C }); /* x */
|
|
o.t_idx({ 3 }, 0, c, 0); /* geom */
|
|
o.t(D, { ((c + tpre) / st) * (c / sh) * (c / sw), mean ? P / g : P }); /* y, INOUT */
|
|
return o.finish("s2d3d", operand, out);
|
|
}
|
|
|
|
int ref_pix_patch_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), pp = gi(p, n_p, "p", 0), C = gi(p, n_p, "C", 0);
|
|
const int64_t w = cols_for(M, 1), h = w ? M / w : 0;
|
|
if (!w || pp <= 0 || C <= 0) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { h * pp, w * pp * C }); /* x: the image */
|
|
o.t(D, { h, w * C * pp * pp }); /* y: patch rows */
|
|
return o.finish("pix_patch", operand, out);
|
|
}
|
|
|
|
/* attn_na3d over a c x c x c volume, two heads. */
|
|
int ref_attn_na3d_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), hd = gi(p, n_p, "head_dim", 0), c = cube_side(M);
|
|
if (c <= 0 || hd <= 0) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
const int64_t V = c * c * c;
|
|
Opd o;
|
|
o.t(D, { V, 2 * hd }); /* q */
|
|
o.t(D, { V, 2 * hd }); /* k */
|
|
o.t(D, { V, 2 * hd }); /* v */
|
|
o.t_idx({ 3 }, 0, c, 0); /* geom */
|
|
o.t(D, { V, 2 * hd }); /* out */
|
|
return o.finish("attn_na3d", operand, out);
|
|
}
|
|
|
|
int ref_vol_index_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), c = cube_side(M);
|
|
if (c <= 0) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t_idx({ 3 }, 0, c, 0); /* geom: c x c x c */
|
|
o.t_idx({ 3, c * c * c }, 1, -1, 0); /* y */
|
|
return o.finish("vol_index", operand, out);
|
|
}
|
|
|
|
/* The tiled decode's ops: a c x c x c volume (every geom entry c), tiles and boxes inside it. */
|
|
int ref_tile_grid_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), stride = gi(p, n_p, "stride", 0);
|
|
int64_t k = 0;
|
|
while ((k + 1) * (k + 1) <= M) ++k;
|
|
if (k <= 0 || stride <= 0) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t_idx({ 3 }, 0, k * stride, 0); /* geom: k x k tiles */
|
|
o.t_idx({ M, 8 }, 1, -1, 0); /* y */
|
|
return o.finish("tile_grid", operand, out);
|
|
}
|
|
|
|
int ref_tile_crop_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), C = gi(p, n_p, "C", 0);
|
|
int64_t k = 0;
|
|
while ((k + 1) * (k + 1) * (k + 1) <= M) ++k;
|
|
if (k <= 0 || C <= 0) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { 8 * k * k * k, C }); /* x: 2k x 2k x 2k */
|
|
o.t_idx({ 3 }, 0, 2 * k, 0); /* geom */
|
|
o.t_idx({ 6 }, 0, k, 0); /* box: k x k x k at (k, k, k) */
|
|
o.t(D, { k * k * k, C }); /* y */
|
|
return o.finish("tile_crop", operand, out);
|
|
}
|
|
|
|
int ref_tile_stitch_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), tile = gi(p, n_p, "tile", 0);
|
|
const int64_t stride = gi(p, n_p, "stride", 0), sc = gi(p, n_p, "scale", 0);
|
|
const int64_t pp = gi(p, n_p, "p", 0), C = gi(p, n_p, "C", 0);
|
|
if (tile <= 0 || stride <= 0 || stride > tile || sc <= 0 || pp <= 0 || C <= 0)
|
|
return RAD_E_UNSUPPORTED;
|
|
int64_t c = 0;
|
|
while ((c + 1) * (c + 1) * sc * pp <= M) ++c;
|
|
if (c <= 0) return RAD_E_UNSUPPORTED;
|
|
int64_t span = 0; /* the tiles' rows along an axis */
|
|
for (int64_t s0 = 0; s0 < c; s0 += stride) span += std::min(tile, c - s0);
|
|
Opd o;
|
|
o.t(act_dt(p, n_p), { c * sc * sc * span * span, C * pp * pp }); /* x: c frames of tiles */
|
|
o.t_idx({ 3 }, 0, c, 0); /* geom */
|
|
o.t_idx({ 1 }, 0, 0, 0); /* first: frame 0 */
|
|
o.t((uint32_t)RAD_U8, { c * c * sc * pp, c * sc * pp * C }); /* y: every frame */
|
|
return o.finish("tile_stitch", operand, out);
|
|
}
|
|
|
|
int ref_stft_logmel_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), hop = gi(p, n_p, "hop", 0), nfft = gi(p, n_p, "nfft", 0);
|
|
const int64_t nm = gi(p, n_p, "n_mel", 0), ch = gi(p, n_p, "ch", 0), F = nfft / 2 + 1;
|
|
if (M <= 0 || hop <= 0 || nfft < hop || nm <= 0 || ch <= 0) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M * hop, ch }); /* x: samples */
|
|
o.t(D, { 2 * F, 1, nfft }); /* basis */
|
|
o.t(D, { nm, F }); /* mel */
|
|
o.t(D, { M, ch * nm }); /* y */
|
|
return o.finish("stft_logmel", operand, out);
|
|
}
|
|
|
|
int ref_bwe_up_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), r = gi(p, n_p, "ratio", 0), ch = gi(p, n_p, "ch", 0);
|
|
if (M <= 0 || r <= 0 || ch <= 0 || M % r) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M / r, ch }); /* x: the low-rate waveform */
|
|
o.t(D, { M, ch }); /* res */
|
|
o.t(D, { M, ch }); /* y */
|
|
return o.finish("bwe_up", operand, out);
|
|
}
|
|
|
|
int ref_attn_umt5_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t m = gi(p, n_p, "M", 0), heads = gi(p, n_p, "heads", 0);
|
|
if (heads < 1 || heads > 128 || m < 1 || m > INT32_MAX) return RAD_E_SHAPE;
|
|
const int64_t width = heads * rad_umt5::head_width;
|
|
// Use as few balanced sequences as the per-sequence limit allows.
|
|
const int64_t segments = (m + rad_umt5::max_tokens - 1) / rad_umt5::max_tokens;
|
|
Opd o;
|
|
o.t(RAD_BF16, { m, width });
|
|
o.t(RAD_BF16, { m, width });
|
|
o.t(RAD_BF16, { m, width });
|
|
o.t_idx({ segments + 1 }, m, -1, RAD_OPD_F_IDX_CU);
|
|
o.t(RAD_BF16, { rad_umt5::buckets, heads });
|
|
o.t(RAD_BF16, { m, width });
|
|
return o.finish("attn_umt5", operand, out);
|
|
}
|
|
|
|
int ref_gelu_umt5_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t rows = gi(p, n_p, "M", 0), width = gi(p, n_p, "n", 0);
|
|
if (act_dt(p, n_p) != RAD_BF16) return RAD_E_DTYPE;
|
|
Opd o;
|
|
o.t(RAD_BF16, {rows, width});
|
|
o.t(RAD_BF16, {rows, width});
|
|
return o.finish("gelu_umt5", operand, out);
|
|
}
|
|
|
|
int ref_wan_rope_table_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t bound = gi(p, n_p, "M", 0);
|
|
if (bound < 2 || bound > (1ll << 30)) return RAD_E_UNSUPPORTED;
|
|
const int64_t side = std::min<int64_t>(cube_side(bound / 2), 512);
|
|
const int64_t rows = 2 * side * side * side;
|
|
Opd o;
|
|
o.t_idx({3}, 0, 2 * side, 0);
|
|
o.t(RAD_F32, {rows, rad_wan_dit::head_width});
|
|
return o.finish("wan_rope_table", operand, out);
|
|
}
|
|
|
|
int ref_wan_rope_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t rows = gi(p, n_p, "M", 0), heads = gi(p, n_p, "heads", 0);
|
|
if (rows < 1 || rows > INT32_MAX || heads < 1 || heads > 128) return RAD_E_SHAPE;
|
|
const int64_t width = heads * rad_wan_dit::head_width;
|
|
const uint32_t dtype = act_dt(p, n_p);
|
|
if (dtype != RAD_BF16 && dtype != RAD_F32) return RAD_E_DTYPE;
|
|
Opd o;
|
|
o.t(dtype, {rows, width});
|
|
o.t(dtype, {rows, width});
|
|
o.t(RAD_F32, {rows, rad_wan_dit::head_width});
|
|
return o.finish("wan_rope", operand, out);
|
|
}
|
|
|
|
int ref_wan_mod_norm_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t rows = gi(p, n_p, "M", 0), width = gi(p, n_p, "n", 0);
|
|
const bool head = gi(p, n_p, "group", 0) == -1;
|
|
if (rows < 1 || rows > INT32_MAX || width < 1 || width > 4096) return RAD_E_SHAPE;
|
|
const uint32_t dtype = act_dt(p, n_p);
|
|
if (dtype != RAD_BF16 && dtype != RAD_F32) return RAD_E_DTYPE;
|
|
Opd o;
|
|
o.t(RAD_F32, {rows, width});
|
|
o.t(RAD_F32, {1, head ? width : 6 * width});
|
|
o.t(RAD_F32, {1, head ? 2 : 6, width});
|
|
o.skip();
|
|
o.t(dtype, {rows, width});
|
|
return o.finish("wan_mod_norm", operand, out);
|
|
}
|
|
|
|
int ref_wan_gated_add_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t rows = gi(p, n_p, "M", 0), width = gi(p, n_p, "n", 0);
|
|
if (rows < 1 || rows > INT32_MAX || width < 1 || width > 4096) return RAD_E_SHAPE;
|
|
const bool gated = gi(p, n_p, "gate", -1) != -1;
|
|
Opd o;
|
|
o.t(RAD_F32, {rows, width});
|
|
o.t(RAD_BF16, {rows, width});
|
|
if (gated) {
|
|
o.t(RAD_F32, {1, 6 * width});
|
|
o.t(RAD_F32, {1, 6, width});
|
|
} else {
|
|
o.skip();
|
|
o.skip();
|
|
}
|
|
o.skip();
|
|
return o.finish("wan_gated_add", operand, out);
|
|
}
|
|
|
|
int ref_wan_unpatch_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t bound = gi(p, n_p, "M", 0), channels = gi(p, n_p, "n", 0);
|
|
if (bound < 2 || bound > (1ll << 30)) return RAD_E_UNSUPPORTED;
|
|
const int64_t side = std::min<int64_t>(cube_side(bound / 2), 512);
|
|
const int64_t patches = 2 * side * side * side;
|
|
if (channels < 1 || channels > 1024) return RAD_E_SHAPE;
|
|
const uint32_t dtype = act_dt(p, n_p);
|
|
if (dtype != RAD_F32 && dtype != RAD_BF16) return RAD_E_DTYPE;
|
|
Opd o;
|
|
o.t(dtype, {patches, 4 * channels});
|
|
o.t_idx({3}, 0, 2 * side, 0);
|
|
o.t(RAD_F32, {4 * patches, channels});
|
|
return o.finish("wan_unpatch", operand, out);
|
|
}
|
|
|
|
int ref_wan_vae_frame_cu_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t offsets = gi(p, n_p, "M", 0);
|
|
if (offsets < 2 || offsets > 9) return RAD_E_SHAPE;
|
|
Opd o;
|
|
o.t_idx({3}, 0, offsets - 1, 0);
|
|
o.t(RAD_I32, {offsets});
|
|
return o.finish("wan_vae_frame_cu", operand, out);
|
|
}
|
|
|
|
int ref_wan_vae_history_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t rows = gi(p, n_p, "M", 0);
|
|
const bool temporal = gi(p, n_p, "temporal", 0) != 0;
|
|
if (rows < 1 || rows > INT32_MAX) return RAD_E_SHAPE;
|
|
constexpr int64_t channels = 8;
|
|
const int64_t cached = rows % 2 == 0 ? 4 : 2;
|
|
Opd o;
|
|
o.t(RAD_BF16, {rows, channels});
|
|
o.t(RAD_BF16, {cached, channels});
|
|
o.t(RAD_BF16, {temporal ? rows : rows + cached, temporal ? 3 * channels : channels});
|
|
return o.finish("wan_vae_history", operand, out);
|
|
}
|
|
|
|
int ref_wan_vae_expand_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t bound = gi(p, n_p, "M", 0);
|
|
if (bound < 1 || bound > (1ll << 30)) return RAD_E_SHAPE;
|
|
const int64_t side = cube_side(bound);
|
|
const int64_t cin = gi(p, n_p, "cin", 0), cout = gi(p, n_p, "cout", 0);
|
|
const int64_t st = gi(p, n_p, "st", 1), s = gi(p, n_p, "s", 1), drop = gi(p, n_p, "drop", 0);
|
|
if (cin < 1 || cin > 4096 || cout < 1 || cout > 4096 || st < 1 || st > 2 ||
|
|
s < 1 || s > 2 || drop < 0 || drop >= st) return RAD_E_SHAPE;
|
|
Opd o;
|
|
o.t(RAD_BF16, {side * side * side, cin});
|
|
o.t_idx({3}, 0, side, 0);
|
|
o.t(RAD_BF16, {(side * st - drop) * side * side * s * s, cout});
|
|
return o.finish("wan_vae_expand", operand, out);
|
|
}
|
|
|
|
int ref_timestep_embed_f64_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t rows = gi(p, n_p, "M", 0), width = gi(p, n_p, "n", 0);
|
|
if (act_dt(p, n_p) != RAD_F32) return RAD_E_DTYPE;
|
|
if (rows < 1 || rows > INT32_MAX || width < 2 || width > INT32_MAX || (width & 1))
|
|
return RAD_E_SHAPE;
|
|
Opd o;
|
|
o.t(RAD_F32, {rows});
|
|
o.t(RAD_F32, {rows, width});
|
|
return o.finish("timestep_embed_f64", operand, out);
|
|
}
|
|
|
|
int ref_flow_unipc_step_shape(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);
|
|
if (M <= 0 || n <= 0 || n > INT32_MAX / 3) return RAD_E_SHAPE;
|
|
Opd o;
|
|
o.t(act_dt(p, n_p), { M, n });
|
|
o.t_fill(RAD_F32, { rad_unipc::COUNT }, RAD_FILL_SIGMOID, 0);
|
|
o.t(RAD_F32, { M, 3 * n });
|
|
o.t(RAD_F32, { M, n });
|
|
o.skip(); // Classifier-free guidance is covered by a separate correctness test.
|
|
o.skip();
|
|
return o.finish("flow_unipc_step", operand, out);
|
|
}
|
|
|
|
/* ---- DeepSeek V4.1's tower ops (ref_pixtral.cpp, ref_glm.cpp) ---- */
|
|
|
|
int ref_ds41_vit_rope2d_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), D = gi(p, n_p, "head_dim", 0),
|
|
H = gi(p, n_p, "n_head", 0);
|
|
if (M <= 0 || D <= 0 || D % 4 || H <= 0) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(act_dt(p, n_p), {M, 3 * H * D});
|
|
o.t_idx({2, M}, 80, -1, 0);
|
|
return o.finish("ds41_vit_rope2d", operand, out);
|
|
}
|
|
|
|
int ref_ds41_pack3x3_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), m = gi(p, n_p, "merge", 3);
|
|
if (M <= 0 || n <= 0 || m < 2 || m > 8) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(D, { M, n }); /* h: M patches of n */
|
|
o.t_idx({4, M}, 80, -1, 0); /* coord: origin, height, width */
|
|
o.t(D, { M, m * m * n }); /* packed: at most one row a patch */
|
|
return o.finish("ds41_pack3x3", operand, out);
|
|
}
|
|
|
|
int ref_ds41_span_rows_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const uint32_t D = act_dt(p, n_p);
|
|
const int64_t n = gi(p, n_p, "n", 0);
|
|
if (n <= 0) return RAD_E_UNSUPPORTED;
|
|
Opd o;
|
|
o.t(D, { n }); /* start */
|
|
o.t(D, { n }); /* nl */
|
|
o.t(D, { n }); /* end */
|
|
o.t(D, { 4, n }); /* span_rows */
|
|
return o.finish("ds41_span_rows", operand, out);
|
|
}
|
|
|
|
int ref_router_scored_topk_vl_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
|
const int64_t M = gi(p, n_p, "M", 0), ne = gi(p, n_p, "n_expert", 0),
|
|
k = gi(p, n_p, "top_k", 0);
|
|
if (M < 1 || ne < 1 || ne > 512 || k < 1 || k > ne) return RAD_E_UNSUPPORTED;
|
|
const uint32_t D = act_dt(p, n_p);
|
|
Opd o;
|
|
o.t(D, { M, ne }); /* logits */
|
|
o.t(RAD_F32, { ne }); /* bias */
|
|
o.t(RAD_F32, { ne }); /* bias_vl */
|
|
o.t((uint32_t)RAD_I32, { M }); /* mask: 1 inside an image span */
|
|
o.t_idx({ M, k }, ne, -1, 0); /* ids */
|
|
o.t(D, { M, k }); /* weights */
|
|
return o.finish("router_scored_topk_vl", operand, out);
|
|
}
|