Archived
The reference AttnProjection (comfy/ldm/minimax/audio_vae.py:267) computes proj(norm3(x)) + attn(norm1(x)), then + mlp(norm2(·)). The two 2048-wide LayerNorms are not interchangeable: norm3's affine |w|mean is 0.60, norm1's 0.27, so the projection branch carries most of the block's magnitude (std 6.6 via norm3 vs 0.4 for the attention via norm1). The device had them wired the other way -- it staged norm1 into the projection and norm3 into the attention (qkv). Feeding the small-affine norm1 to the projection shrank it 6.6 -> 1.8, collapsing the pre_block output and every audio latent downstream to ~0.25-0.27x the reference. Fix: stage norm1 for the attention (feeding p_op_qkv) and norm3 for the projection (feeding p_op_proj). Both LN ops are the same ln(...,2048,b_apb-> b_aln3) kernel, so only the consumer wiring changed -- no kernel touched, no re-convert (norm1.*/norm3.* weights are already loaded correctly). Also adds a gated read-only R4D_ENC_SEL=amb readback (dbg_ae_amb = b_amb, the f32 mean_proj input): the bf16 b_ax2 readback lags the arena and dumps garbage, so b_amb is the reliable pre_block output used to localise the gap. Inert when R4D_ENC_SEL is unset. Device-verified (seed=7 tone, minimax-h3-ref2va-fp8, warm TP2): pre_block out b_amb std 1.65 -> 6.43 (ratio 0.25 -> 0.97); final latent b_enca std 0.23 -> 0.546 vs reference 0.588 (ratio 0.39 -> 0.93), affine slope 0.385 -> 0.923, R^2 0.975 -> 0.989. Host tier green (abi_layout/arch/ref/videos). Co-Authored-By: Claude <noreply@anthropic.com>
1329 lines
79 KiB
C
1329 lines
79 KiB
C
/* rad_builder.h -- the declare phase. An architecture plugin states everything it will do before
|
|
* doing any of it: every weight, every buffer, every op, in order. See spec.md §3.1.
|
|
*
|
|
* After declare, the whole model is known statically (every op, the kernel serving it, the weights
|
|
* it touches, and the order), and the rest of the engine relies on that.
|
|
*/
|
|
#ifndef RAD_BUILDER_H
|
|
#define RAD_BUILDER_H
|
|
|
|
#include "rad_abi.h"
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
typedef struct RadBuilder RadBuilder;
|
|
|
|
/* ================================================================== model metadata */
|
|
/* What the loader read out of the container's header before any plugin ran. Dimensions come from
|
|
* here, so one plugin covers every size in a family: qwen3_dense_bf16.so serves 2B, 9B and 27B. */
|
|
typedef struct RadModelMeta {
|
|
const char* arch_id; /* the string that selected this plugin */
|
|
const char* name;
|
|
const char* quant; /* the quantisation descriptor, e.g. "w4a8_symz_g128" */
|
|
|
|
int64_t n_layers;
|
|
int64_t n_embd;
|
|
int64_t n_head;
|
|
int64_t n_head_kv;
|
|
int64_t head_dim;
|
|
int64_t n_ff;
|
|
int64_t n_vocab;
|
|
int64_t n_ctx_train;
|
|
|
|
int64_t n_expert; /* 0 for a dense model */
|
|
int64_t n_expert_used;
|
|
int64_t n_expert_shared;
|
|
|
|
float rms_eps;
|
|
float rope_theta;
|
|
/* Always 1. Rotary scaling is a type with parameters (linear, YaRN, llama3, longrope) carried
|
|
* in the free-form keys below (rope_parameters.* / rope_scaling.*, with text_config.* and
|
|
* draft.* forms); arch/common/rad_arch.h's yarn_from reads them and refuses unknown schemes by
|
|
* name. A bare factor here would read every scheme as a linear position divide. */
|
|
float rope_scale;
|
|
|
|
/* Free-form key/value from the container header, for anything the struct above does not
|
|
* name. A plugin reads what it needs; the core never interprets these. */
|
|
int n_kv;
|
|
const char* const* kv_key;
|
|
const char* const* kv_val;
|
|
} RadModelMeta;
|
|
|
|
long long rad_meta_geti(const RadModelMeta* m, const char* key, long long dflt);
|
|
double rad_meta_getf(const RadModelMeta* m, const char* key, double dflt);
|
|
const char* rad_meta_gets(const RadModelMeta* m, const char* key, const char* dflt);
|
|
|
|
/* ================================================================== build context */
|
|
typedef struct RadBuildCtx {
|
|
int rank; /* declare runs once per rank; the plugin declares dimensions */
|
|
int world_size; /* already divided -- the core inserts no collectives (spec §9) */
|
|
|
|
int64_t max_tok; /* the largest token count one step may carry */
|
|
int64_t max_seqs; /* the largest number of sequences one step may carry */
|
|
int64_t max_ctx; /* context bound this deployment was configured for */
|
|
int max_spec; /* speculative window bound, 0 if speculation is off */
|
|
|
|
/* The tensor-parallel wire. 0 (the zero-initialised default) is the exact all-reduce. Nonzero
|
|
* accepts a lossy wire: at two ranks libr4d serves a Walsh-Hadamard-rotated 6-bit payload (6
|
|
* bits an element plus a bf16 scale per group of 64, so a prefill chunk's message shrinks
|
|
* 2.4x), at three and four ranks a tiered int8 two-shot. The plugin inverts it into
|
|
* `all_reduce`'s `exact` parameter.
|
|
* The polarity makes a context that forgets the field exact, not lossy (spec §9). */
|
|
int tp_wire_lossy;
|
|
|
|
/* The all-reduce message size, in bytes, from which the lossy wire is used. Below it the
|
|
* plugin must use the exact kernel even when `tp_wire_lossy` is set: compression pays only
|
|
* once the transfer is bandwidth-bound, and a decode step's few residual rows cost more to
|
|
* rotate/pack/unpack than they save. 0 means the plugin's default, 128 KiB. */
|
|
int64_t tp_wire_min_bytes;
|
|
|
|
|
|
/* The KV cache dtype, a deployment choice rather than a property of the checkpoint: fp8
|
|
* weights may want a bf16 cache and vice versa.
|
|
*
|
|
* RAD_DT_INVALID (the zero-initialised default) means the plugin's default, bf16 for every
|
|
* architecture in this tree. RAD_F8E4M3 is the only other implemented value: the fp8kv
|
|
* attention family is band-gated on it, and the store side writes E4M3 with per-(sequence,
|
|
* head) descales.
|
|
*
|
|
* An ABI field rather than an environment variable because it changes what the model
|
|
* computes, so it must have one visible answer: --kv-cache-dtype. */
|
|
int kv_dtype; /* RAD_DT_INVALID = the plugin's default (bf16); or RAD_F8E4M3 */
|
|
int is_draft; /* this builder scope is a drafter (spec §10) */
|
|
const char* scope; /* "", "draft", "vision", "audio" -- prefixes declared names */
|
|
|
|
/* Nonzero: a declare that only sizes the activations, at a `max_tok` smaller than the
|
|
* deployment's. The core runs one per level of the activation arena it lends out; the result
|
|
* gives every buffer's size when no step carries more than `max_tok` tokens, and the arena
|
|
* above that is lent to the expert slab while steps are that small.
|
|
*
|
|
* Invariant: the plugin must not touch its own state while this is set. Its step function
|
|
* reads state the real declare wrote at the real `max_tok`; overwriting it would run every
|
|
* later step against the smaller configuration. Declare into scratch storage instead: nothing
|
|
* a sizing declare produces is issued, and its op handles are the real declare's.
|
|
*
|
|
* Only asked of a plugin that set RadArchProbe::shape_probe_ok. Trailing and zero-defaulted,
|
|
* so a plugin built against the older struct is unchanged. */
|
|
int shape_probe;
|
|
|
|
/* The most patches one encoder pass may carry; 0 means the deployment serves no media, the
|
|
* plugin declares no encoder, and a tower the container carries stays off the card. It is the
|
|
* encoder's `max_tok` (its activations are sized against it) and bounds the largest single
|
|
* image accepted, since one image is one attention segment and is never split across passes. */
|
|
int64_t max_enc_patches;
|
|
|
|
/* The most logits rows one step may name, when that exceeds the plugin's own rule of one per
|
|
* sequence (1 + max_spec at a speculative step); 0 is the rule. The engine's KL mode sets it to
|
|
* a step's tokens plus a row per sequence, since it reads a full-vocabulary row at every prompt
|
|
* position; the logits buffer and the lm_head's row range are sized from it. The engine checks
|
|
* the declared logits buffer and refuses the mode when it is shorter, so a plugin that ignores
|
|
* the field is refused rather than overrun.
|
|
*
|
|
* Trailing and zero-defaulted, so a plugin built against the older struct is unchanged. */
|
|
int64_t max_out_rows;
|
|
|
|
/* The most frames one audio encoder pass may carry; 0 means no audio and no audio encoder.
|
|
* It is the audio encoder's `max_tok`, as max_enc_patches is a vision tower's, and bounds the
|
|
* longest clip, since one clip is one segment. The engine takes it from
|
|
* RadArchProbe::audio_frames.
|
|
*
|
|
* Trailing and zero-defaulted, so a plugin built against the older struct is unchanged. */
|
|
int64_t max_audio_frames;
|
|
|
|
/* The most tokens a step of more than one sequence may carry, when that is less than
|
|
* max_tok; 0 means max_tok. Under --prefill-span a prefill alone in its step may take max_tok
|
|
* and every other step is planned at a shared budget, so a step wider than this is one
|
|
* sequence. A plugin may size a buffer holding a row per query of every sequence in a step at
|
|
* this, and serve a wider step a window at a time.
|
|
*
|
|
* Trailing and zero-defaulted, so a plugin built against the older struct is unchanged. */
|
|
int64_t max_mixed_tok;
|
|
} RadBuildCtx;
|
|
|
|
/* ================================================================== weights */
|
|
/* Access class, declared per weight because only the architecture plugin knows it. It tells the
|
|
* placement planner whether prefetch can be exact (spec §5.2). */
|
|
enum {
|
|
RAD_ACCESS_PER_TOKEN = 0, /* read every forward pass -- dense weights */
|
|
RAD_ACCESS_PER_REQUEST = 1, /* read once per request -- an encoder tower, with an image */
|
|
RAD_ACCESS_CONDITIONAL = 2, /* read only if selected -- routed experts */
|
|
RAD_ACCESS_RARE = 3, /* may not be read at all in a given deployment */
|
|
RAD_ACCESS_VOCAB = 4 /* lm_head and the embedding table: read every token, very
|
|
* large, and one is a gather rather than a GEMM. Neither
|
|
* PER_TOKEN's prefetch nor CONDITIONAL's prediction fits it
|
|
* (spec §5.2). */
|
|
};
|
|
|
|
/* How a weight is split across tensor-parallel ranks. The plugin says; the core does not infer. */
|
|
enum {
|
|
RAD_SHARD_NONE = 0, /* replicated on every rank */
|
|
RAD_SHARD_ROW = 1, /* split along dim 0 -- column-parallel in the usual naming */
|
|
RAD_SHARD_COL = 2 /* split along dim 1 -- row-parallel, needs a following all-reduce */
|
|
};
|
|
|
|
/* Grouping key: the unit of movement. An expert's gate/up/down share one, because the unit of
|
|
* transfer is an expert, not a tensor (spec §4.2). Layer grouping makes layer offload a contiguous
|
|
* run rather than a scatter. */
|
|
typedef struct RadWeightGroup {
|
|
int32_t layer; /* -1 for a model-level weight */
|
|
int32_t expert; /* -1 for a non-expert weight */
|
|
int32_t slot; /* free index within the group, e.g. gate=0 up=1 down=2 */
|
|
} RadWeightGroup;
|
|
|
|
/* Note: these are C compound literals, which C++ accepts only as a GNU extension. A C++ plugin
|
|
* should use the equivalent grp_layer()/grp_expert()/grp_model() inline functions in
|
|
* `<arch/rad_arch.h>`, as the in-tree plugins do. */
|
|
#define rad_group_layer(l) ((RadWeightGroup){ (int32_t)(l), -1, 0 })
|
|
#define rad_group_expert(l, e) ((RadWeightGroup){ (int32_t)(l), (int32_t)(e), 0 })
|
|
#define rad_group_model() ((RadWeightGroup){ -1, -1, 0 })
|
|
|
|
/* The most projections one stored tensor may stack along dim 0. The bf16 delta net needs five
|
|
* (q, k, v, a, b); eight leaves room without making the decl big. */
|
|
enum { RAD_MAX_ROW_PARTS = 8 };
|
|
|
|
typedef struct RadWeightDecl {
|
|
uint32_t dtype; /* what the kernel reads; the stored layout comes from the kernel */
|
|
uint32_t rank;
|
|
int64_t shape[RAD_MAX_RANK]; /* this rank's extent of what `planes` select (below) */
|
|
int access; /* RAD_ACCESS_* */
|
|
int shard; /* RAD_SHARD_* */
|
|
RadWeightGroup group;
|
|
int optional; /* absent in the container is not an error */
|
|
/* What is stacked in dim 0, for a RAD_SHARD_ROW weight whose stored tensor concatenates
|
|
* several projections ([gate|up], the delta net's [q|k|v]). Rows of this rank, in stored
|
|
* order, summing to shape[0]; 0 parts means dim 0 is one projection.
|
|
*
|
|
* A tensor-parallel rank owns a slice of each stacked projection, not a contiguous slice of
|
|
* the stack: rows [rank*D, (rank+1)*D) of a fused gate_up would give rank 0 the whole gate and
|
|
* rank 1 the whole up, and the model would produce fluent wrong text (spec §9). The parts
|
|
* cannot be inferred from the container, which holds only the fused result, and a checkpoint
|
|
* tensor may already be a fusion the name map does not mention (in_proj_qkv is three head
|
|
* groups in one [10240, 5120]). Only the plugin knows them. */
|
|
int n_row_parts;
|
|
int64_t row_parts[RAD_MAX_ROW_PARTS];
|
|
|
|
/* Which stored weight this is a view of, and which of its planes. spec.md §4.3.
|
|
*
|
|
* A container entry or checkpoint tensor is a logical weight: the planes of its encoding
|
|
* (rad_encoding.h). A block-fp8 linear's codes and scale plane are one weight, not two. A
|
|
* kernel takes them as whatever operands its schema has: gemm_nt_q takes codes and scales as
|
|
* two, the fp8 lm_head one operand holding both. So a declared weight names its `source` (null
|
|
* for the logical weight of its own name) and the `planes` it takes, by role, comma-separated:
|
|
* "codes", "scale", "codes,scale". Null takes every plane, which for a plain weight is one.
|
|
*
|
|
* One declared weight is one stored buffer. Its dtype and shape describe what is selected: a
|
|
* single plane's own dtype and extents, or for several, the codes' dtype and the logical
|
|
* extents. A dtype other than the plane's is legal only as an exact widening (a bf16 norm read
|
|
* as f32), which the loader performs; anything lossy is the quantiser's job, done before the
|
|
* container is written. */
|
|
const char* source;
|
|
const char* planes;
|
|
} RadWeightDecl;
|
|
|
|
rad_weight rad_decl_weight(RadBuilder* b, const char* name, const RadWeightDecl* d);
|
|
|
|
/* A stored weight's encoding, its logical extents in `shape` (RAD_MAX_RANK entries, may be null)
|
|
* and its rank in `rank` (may be null). RAD_OK, or RAD_E_NOTFOUND when the model has no such
|
|
* weight.
|
|
*
|
|
* The answer comes from whatever the model is loaded from (a container entry, a checkpoint tensor,
|
|
* or in rad-convert the recipe that will quantise it), so a plugin chooses its ops the same way at
|
|
* convert and at load: an fp8 GEMM for a block-fp8 weight, the rotated expert kernels for
|
|
* `i4*bf16[1x128]/fwht128`. Ask after the weight's name map entry (rad_decl_name_map), which is how
|
|
* a checkpoint is searched for it. A builder with no model behind it (a test, a synthetic tool)
|
|
* answers RAD_E_NOTFOUND, and a weight declared there is read as plain in its declared dtype. */
|
|
int rad_weight_encoding(RadBuilder* b, const char* source, RadEncoding* out, int64_t* shape,
|
|
uint32_t* rank);
|
|
|
|
/* ================================================================== buffers */
|
|
/* Activations are declared too, so the buffer plan can analyse them. Liveness is computed over the
|
|
* declared op list, so scratch and intermediates come from one arena sized once instead of an
|
|
* allocator called per step (spec §3.1). */
|
|
enum {
|
|
RAD_BUF_TRANSIENT = 0, /* liveness-analysed, may share storage with another transient */
|
|
RAD_BUF_PERSIST = 1, /* survives across steps: a KV-adjacent scratch, a routing histogram */
|
|
RAD_BUF_DERIVED = 2 /* computed once per step by the core from the batch (spec §8) */
|
|
};
|
|
|
|
typedef struct RadBufDecl {
|
|
uint32_t dtype;
|
|
uint32_t rank;
|
|
int64_t shape[RAD_MAX_RANK]; /* sized at max_tok, which is the worst step */
|
|
int kind; /* RAD_BUF_* */
|
|
int domain; /* RAD_DOMAIN_DEVICE or RAD_DOMAIN_HOST */
|
|
const char* derive; /* RAD_BUF_DERIVED: the derivation the core runs */
|
|
/* The KV group a derivation is about, or 0 for the only one of its kind.
|
|
*
|
|
* `conv_state_index` computes a read cursor inside a rolling window, so it needs that group's
|
|
* slot per sequence and window depth, and a model may declare several (Qwen4-Exp's PLE layer
|
|
* has two conv states of different depths and dtypes beside the gated delta net's). 0 with
|
|
* more than one group of the kind a derivation reads is refused: the wrong window depth gives
|
|
* a plausible cursor and a convolution that reads history it never wrote.
|
|
*
|
|
* Trailing and zero-defaulted, so a plugin built against the older struct is unchanged. */
|
|
rad_kvgroup kv;
|
|
} RadBufDecl;
|
|
|
|
rad_buf rad_decl_buffer(RadBuilder* b, const char* name, const RadBufDecl* d);
|
|
|
|
/* ================================================================== KV groups */
|
|
/* Each distinct state kind is a KV group with its own block geometry, pool and slot mapping in the
|
|
* step batch. Linear-attention state is a KV group too: not paged the same way, but allocated,
|
|
* mapped and freed by the same manager, so there is one record of what a sequence owns
|
|
* (spec §7.2). */
|
|
enum {
|
|
RAD_KV_FULL = 0, /* paged, per-token, growing -- full attention */
|
|
RAD_KV_WINDOW = 1, /* paged, per-token, bounded by the window */
|
|
RAD_KV_LINEAR = 2, /* per-sequence, fixed size -- GDN recurrent state */
|
|
RAD_KV_CONV = 3 /* per-sequence, a rolling window of width-1 + num_spec entries */
|
|
};
|
|
|
|
typedef struct RadKVGroupDecl {
|
|
int kind; /* RAD_KV_* */
|
|
uint32_t dtype;
|
|
int64_t window; /* RAD_KV_WINDOW: the window; 0 otherwise */
|
|
int64_t n_head_kv;
|
|
int64_t head_dim;
|
|
int64_t state_dim[2]; /* RAD_KV_LINEAR: the per-head state geometry, e.g. {128,128} */
|
|
int64_t conv_width; /* RAD_KV_CONV */
|
|
/* Block size is not declared. It is taken from the resolved attention kernel's constraints at
|
|
* declare, so the core needs no edit when a kernel changes (spec §7.2). Read it back with
|
|
* rad_kv_block_size() after declaring the attention op that consumes this group. */
|
|
} RadKVGroupDecl;
|
|
|
|
/* `rad_kvgroup` is in rad_types.h with the other handles -- RadBufDecl above carries one. */
|
|
rad_kvgroup rad_decl_kv_group(RadBuilder* b, const char* name, const RadKVGroupDecl* d);
|
|
/* Valid only after the op that reads this group has been declared. 0 before that. */
|
|
int64_t rad_kv_block_size(RadBuilder* b, rad_kvgroup g);
|
|
|
|
/* Bind a layer to a group, so the block manager knows how many state instances a sequence owns. */
|
|
int rad_bind_layer_kv(RadBuilder* b, int layer, rad_kvgroup g);
|
|
|
|
/* ================================================================== ops */
|
|
/* Declared with their parameters and weight operands. The core resolves each against the kernel
|
|
* hierarchy, per band, per domain. This phase always completes, and every op that failed to
|
|
* resolve is reported together at the end. */
|
|
rad_op rad_decl_op(RadBuilder* b, const char* op,
|
|
const RadParam* params, int n_params,
|
|
const rad_weight* weights, int n_weights);
|
|
|
|
/* Convenience wrappers so a declare function reads like a list of what the model does.
|
|
* rad_op h = RAD_OP(b, "gemm_w4a8_nt",
|
|
* RAD_PARAMS(RAD_RANGE("M",1,max_tok), RAD_INT("N",8192), RAD_INT("K",5120)),
|
|
* RAD_WEIGHTS(w));
|
|
*/
|
|
#define RAD_PARAMS(...) ((const RadParam[]){ __VA_ARGS__ }), \
|
|
(int)(sizeof((const RadParam[]){ __VA_ARGS__ }) / sizeof(RadParam))
|
|
#define RAD_NOPARAMS ((const RadParam*)0), 0
|
|
#define RAD_WEIGHTS(...) ((const rad_weight[]){ __VA_ARGS__ }), \
|
|
(int)(sizeof((const rad_weight[]){ __VA_ARGS__ }) / sizeof(rad_weight))
|
|
#define RAD_NOWEIGHTS ((const rad_weight*)0), 0
|
|
#define RAD_OP(b, op, ...) rad_decl_op((b), (op), __VA_ARGS__)
|
|
|
|
/* An op's buffer operands. rad_decl_op names only weights, which placement moves; the buffer
|
|
* planner needs liveness, i.e. which ops read and write which buffers. A transient with no
|
|
* declared use is assumed live for the whole program, which is sound (a plugin may issue any
|
|
* handle in any order) but wasteful; --debug-graph prints how many buffers fell into that case.
|
|
* Separate calls rather than four more parameters on every rad_decl_op. */
|
|
int rad_op_reads (RadBuilder* b, rad_op h, const rad_buf* bufs, int n);
|
|
int rad_op_writes(RadBuilder* b, rad_op h, const rad_buf* bufs, int n);
|
|
|
|
/* A buffer the architecture touches from the second lane, the one case where the op range a
|
|
* buffer's declarations imply does not bound when its bytes are in use.
|
|
*
|
|
* The planner packs transients from one linear order (spec §3.1): two whose declared lives do not
|
|
* overlap share arena bytes. `rad_lane` breaks that: lane 1's ops sit at op indices inside lane
|
|
* 0's, so a shared-arm scratch live over ops 41..42 can overlay a routed-arm scratch live over ops
|
|
* 37..38, and on two streams those runs overlap in time, corrupting data silently.
|
|
*
|
|
* A buffer named here is live for the whole program, like a buffer with no declared use. It is
|
|
* per-buffer rather than a mode because unsharing every buffer costs ~220 MiB on a large model,
|
|
* against a few MiB for one block's lane-1 transients. Call it at declare for every buffer the
|
|
* second lane reads or writes; the lane-0 buffers they could share with are covered, since
|
|
* sharing needs both sides.
|
|
*
|
|
* A no-op for a PERSIST or DERIVED buffer, which is live everywhere already. */
|
|
int rad_buf_concurrent(RadBuilder* b, rad_buf buf);
|
|
|
|
/* An uneven shard: this rank's slice of a RAD_SHARD_ROW or RAD_SHARD_COL weight's split dimension
|
|
* is [lo, hi) rather than an even 1/tp. Rows of each stacked row part for ROW, columns for COL, in
|
|
* the container's unsharded units. The declared extent must already be `hi - lo` (per row part,
|
|
* for ROW).
|
|
*
|
|
* For a width that does not divide into equal whole blocks across ranks: a 640-column projection
|
|
* with 128-column scale blocks is five blocks, split three and two. The stacked parts of a ROW
|
|
* weight must be equal in the container, since only the rank's own part sizes are declared.
|
|
* Refused for a container converted for more than one rank (its entries are already even shares)
|
|
* and for a stored layout whose row runs or column groups the span would cut. */
|
|
int rad_weight_shard_span(RadBuilder* b, rad_weight w, int64_t lo, int64_t hi);
|
|
|
|
/* A stacked weight's span, one [lo, hi) per row part, with each part's whole extent in the
|
|
* container, for parts of unequal width that rad_weight_shard_span's single span cannot express
|
|
* (the delta net's [q|k|v|z] is 2048, 2048, 6144, 6144 rows on Flash-Next). `n` is the declared
|
|
* part count; container part g is `total[g]` rows, of which this rank takes [lo[g], hi[g]) and
|
|
* declared `hi[g] - lo[g]`; the totals must sum to the entry's rows. An uneven share of the heads
|
|
* (6 of 16 K heads, with three ranks taking 6, 5 and 5) is declared this way, without padding.
|
|
* Additive export. */
|
|
int rad_weight_shard_span_parts(RadBuilder* b, rad_weight w, const int64_t* lo, const int64_t* hi,
|
|
const int64_t* total, int n);
|
|
|
|
/* This rank keeps no cache for the group. The group is still declared as on every other rank (the
|
|
* scheduler indexes groups by position and one block table serves every rank, so it must exist
|
|
* with the same block size and layers), but no pool is carved for it. For a rank the geometry
|
|
* gives no attention heads (rad_arch.h Geom::attn_zero), whose attention is a `fill` of zeros.
|
|
* After this, no op on this rank may name the group as a RAD_KV operand; the engine refuses one
|
|
* at issue. Additive export. */
|
|
int rad_kv_group_zero(RadBuilder* b, rad_kvgroup g);
|
|
|
|
#define RAD_BUFS(...) ((const rad_buf[]){ __VA_ARGS__ }), \
|
|
(int)(sizeof((const rad_buf[]){ __VA_ARGS__ }) / sizeof(rad_buf))
|
|
|
|
/* Did the last rad_decl_op resolve? A plugin asks for the fused op first and emits the unfused
|
|
* sequence when it does not, which makes fusion a selection rather than a compiler pass
|
|
* (spec §2.3). An unresolved handle is RAD_NULL_HANDLE and becomes an error only if issued. */
|
|
int rad_op_resolved(RadBuilder* b, rad_op h);
|
|
|
|
/* Does any loaded kernel library declare op `op`? Declaring an op outside the vocabulary is an
|
|
* error, since that is what a misspelt name looks like; an op that is declared but resolves to no
|
|
* kernel is not (rad_op_resolved). A plugin asks this first for a fused op only an optional
|
|
* library defines, and emits the chain when the answer is 0. Additive export. */
|
|
int rad_op_declared(RadBuilder* b, const char* op);
|
|
|
|
/* ================================================================== checkpoint name map */
|
|
/* The plugin owns the map from checkpoint tensor names to declared weight names, including
|
|
* convert-time fusions (q/k/v into one qkv, gate and up into gate_up). It is the only component
|
|
* that knows both sides, and rad-convert reads the map from it (spec §2.4). */
|
|
enum { RAD_MAP_COPY = 0, RAD_MAP_CONCAT = 1, RAD_MAP_WNORM = 2, RAD_MAP_ROWS = 3,
|
|
RAD_MAP_TRANSPOSE = 4, RAD_MAP_EMA = 5 };
|
|
|
|
typedef struct RadNameMap {
|
|
const char* declared; /* the name given to rad_decl_weight */
|
|
int mode; /* RAD_MAP_COPY | RAD_MAP_CONCAT */
|
|
int n_src;
|
|
const char* src[8]; /* checkpoint tensor names */
|
|
int concat_dim;
|
|
|
|
/* A sub-tensor index within src[i], or -1 for the whole tensor. GGUF stores a MoE layer's
|
|
* experts stacked as one ffn_gate_exps of shape [.., n_expert], while placement needs one
|
|
* declared weight per expert; this carries the expert index so rad-convert need not parse it
|
|
* out of the declared name. */
|
|
int src_index[8];
|
|
|
|
/* RAD_MAP_ROWS only: piece i is rows [src_index[i], src_index[i] + src_rows[i]) of the 2-D (or
|
|
* higher, rows along dim 0) tensor src[i]; the pieces join along dim 0. */
|
|
int64_t src_rows[8];
|
|
} RadNameMap;
|
|
|
|
/* RAD_MAP_COPY with n_src > 1: the first source present in the container wins. This expresses a
|
|
* tied lm_head (output.weight if present, else token_embd.weight), so one plugin covers family
|
|
* members that differ only in tying. RAD_MAP_CONCAT with n_src > 1 concatenates all sources along
|
|
* concat_dim and requires every one.
|
|
*
|
|
* RAD_MAP_WNORM folds a weight-normalised tensor (torch.nn.utils.weight_norm) into the plain
|
|
* weight it stands for: src[0] is the magnitude `weight_g` ([d0, 1, ...]), src[1] the direction
|
|
* `weight_v` ([d0, ...]), and the result is g * v / ||v||, the norm taken over every dimension but
|
|
* the first. Folded in f32 and stored as the recipe says. A model that applies the
|
|
* parametrisation at every forward computes the same weight each time; doing it once at convert
|
|
* leaves the plugin an ordinary weight.
|
|
*
|
|
* RAD_MAP_ROWS keeps only row ranges of its sources (see src_rows), joined: a head read over a
|
|
* fraction of a vocabulary (an audio model's codes, a few rows of a text model's 200K) stores that
|
|
* fraction rather than the whole table.
|
|
*
|
|
* RAD_MAP_TRANSPOSE swaps the last two dimensions of src[0] in each leading slice, [.., a, b] to
|
|
* [.., b, a]: a linear a checkpoint keeps as (in, out), where the GEMMs read (out, in).
|
|
*
|
|
* RAD_MAP_EMA is an exponential-moving-average codebook as EnCodec-style quantisers keep it: src[0]
|
|
* the embedding sums [bins, ...], src[1] the cluster usage [bins], and the codebook is each row
|
|
* divided by max(usage, 1e-5), in f32. Successive EMA maps for one name stack their codebooks
|
|
* along dim 0, as CONCAT maps extend (a residual quantiser's codebooks as one table).
|
|
*
|
|
* TRANSPOSE and EMA are computed at convert in f32, like WNORM, and stored as the recipe says. */
|
|
|
|
int rad_decl_name_map(RadBuilder* b, const RadNameMap* m);
|
|
|
|
|
|
/* ================================================================== the step's output */
|
|
/* Which buffer holds this step's logits. The core samples from it (the sampler chain is core logic
|
|
* whose ops resolve through the same hierarchy as the plugin's, spec §13), and only the plugin
|
|
* knows which buffer it is. A name match would be wrong: a drafter or encoder tower declares into
|
|
* a scope (RadBuildCtx::scope, spec §10, §11), so "the buffer called logits" can be two buffers in
|
|
* one Program.
|
|
*
|
|
* Its extent along dim 0 bounds RadBatch::n_out: one row per sampled position, not per token of
|
|
* the step. Declaring it [max_tok, n_vocab] is legal but, at a 248K vocabulary, 8 GiB.
|
|
*
|
|
* A Program with no logits buffer cannot serve, and the engine says so at startup. An
|
|
* embedding-only or encoder-only plugin declares none. */
|
|
int rad_declare_logits(RadBuilder* b, rad_buf logits);
|
|
|
|
/* Which buffer holds this step's pooled vectors, what an embedding model returns instead of
|
|
* logits. Row s is sequence s's whole input pooled into one vector, with whatever pooling the
|
|
* model was trained with (a mean, the last token, a CLS row). The core reads rows [0, n_seq) back
|
|
* after the step and hands each to its request. It does not normalise them; that is the API's
|
|
* choice, not the model's.
|
|
*
|
|
* Declaring it makes the Program an embedding program:
|
|
* - every sequence in a step is a whole input. The scheduler never splits one across steps,
|
|
* since a bidirectional encoder cannot be fed half its input, and refuses an input longer
|
|
* than one step rather than truncating it;
|
|
* - there is no sampler, drafter or prefix cache, and nothing is generated: a request ends at
|
|
* the step that carried it.
|
|
*
|
|
* f32, device, [>= max_seqs, dim]: `dim` is the vector width the core reads. A Program declares
|
|
* this or logits, never both. */
|
|
int rad_declare_pooled(RadBuilder* b, rad_buf pooled);
|
|
|
|
|
|
/* ================================================================== the drafter */
|
|
/* The plugin's draft head. The core drives the extra passes, and only the plugin knows what it
|
|
* is, so it is declared rather than detected from weight names (`mtp.fc.weight`) with the core
|
|
* re-reading the drafter's configuration keys the plugin already read.
|
|
*
|
|
* The kind is a drive shape, not a model. SERIAL runs `depth` dependent passes, each embedding the
|
|
* last one's proposal, so tokens arrive one at a time and a pass that declines ends the chain
|
|
* (MTP, EAGLE, Medusa-serial). BLOCK runs a context pass, then one pass that fills every drafted
|
|
* position at once (DFlash2 and other block-diffusion drafters).
|
|
*
|
|
* `proposal` is the buffer a pass writes its token ids into; 0 means the sampler's token buffer,
|
|
* which suits a head whose logits go through the ordinary sampler chain.
|
|
*
|
|
* Declaring no drafter is the normal case; the scheduler's prompt-lookup drafter still runs. */
|
|
typedef enum {
|
|
RAD_DRAFT_NONE = 0,
|
|
RAD_DRAFT_SERIAL = 1, /* `depth` dependent passes, one proposal each */
|
|
RAD_DRAFT_BLOCK = 2, /* a context pass, then one pass that fills the whole block */
|
|
} RadDraftKind;
|
|
|
|
typedef struct RadDrafterDecl {
|
|
const char* name; /* "mtp", "dflash2" -- for messages and the graph dump */
|
|
int kind; /* RAD_DRAFT_* */
|
|
int depth; /* tokens proposed a step; must equal RadBuildCtx::max_spec */
|
|
|
|
/* Where a pass leaves its proposal. 0 = the sampler's token buffer. */
|
|
rad_buf proposal;
|
|
int64_t proposal_pitch; /* elements a row in `proposal`; 0 means 1 */
|
|
|
|
/* BLOCK only. `mask_token` is the id every unfilled block position embeds; a block drafter
|
|
* without one cannot run. `window` is the drafter's own sliding window over its own cache, 0
|
|
* for unbounded. The core stages the block pass against it, so it must match the window the
|
|
* plugin gated its attention with. */
|
|
int32_t mask_token;
|
|
int64_t window;
|
|
|
|
/* BLOCK only: how many rows the query pass runs over, per sequence. 0 means `depth + 1`.
|
|
*
|
|
* The core stages the block as an anchor (the token the sampler just chose) followed by
|
|
* `block - 1` copies of `mask_token`, and the head runs over the last `depth` rows. That covers
|
|
* both training conventions:
|
|
*
|
|
* block = depth + 1 the anchor row predicts nothing; each mask row predicts the token at
|
|
* its own position. The head sees rows 1..depth. DFlash2.
|
|
* block = depth every row, the anchor included, predicts the token at the next
|
|
* position (the ordinary next-token relation). The head sees rows
|
|
* 0..depth-1. DSpark, whose reference takes `hidden[:, -block_size:, :]`.
|
|
*
|
|
* A wrong value is not visible in the output: both run, every draft is verified so the text
|
|
* stays correct, but the proposals shift by one position and acceptance drops to about chance.
|
|
* Hence it is declared by the plugin, which knows how the checkpoint was trained. */
|
|
int64_t block;
|
|
} RadDrafterDecl;
|
|
|
|
int rad_declare_drafter(RadBuilder* b, const RadDrafterDecl* d);
|
|
|
|
/* A serial head with no cache of its own: each of its attention layers reads one trunk layer's K
|
|
* and V and writes none. Gemma 4's assistant head, whose layers have only a query projection,
|
|
* reads the last sliding and last global trunk layer. Called once, after rad_declare_drafter.
|
|
* Additive export; RAD_ABI_VERSION does not move.
|
|
*
|
|
* Effect on the serial driver, all following from the head writing nothing:
|
|
*
|
|
* No history pass, since there is no K and V of its own to fill. The pass carries round 1
|
|
* alone: one row per drafting sequence, at the committed position, with the token the trunk
|
|
* chose after it and the trunk's hidden state there (out_ids). A sequence that does not draft
|
|
* gets no row, and a step with nothing to draft no pass.
|
|
*
|
|
* Every round runs at that same position over the same context. The trunk's cache past the
|
|
* committed position holds K and V of drafts the last verify rejected, or nothing, so every
|
|
* round's `seqused` ends at the committed position; the head's proposals travel forward in its
|
|
* hidden state and next input token instead.
|
|
*
|
|
* `pos_offset` is the rotary position of those rows relative to the committed position: 0 is the
|
|
* position of the trunk hidden state the head continues from, 1 that of the token it embeds. It
|
|
* moves nothing else (the context ends at the committed position either way), and which one a
|
|
* checkpoint was trained with is the plugin's to know. */
|
|
int rad_drafter_shared_kv(RadBuilder* b, int pos_offset);
|
|
|
|
/* ================================================================== the encoder */
|
|
/* A media encoder the plugin runs on an encoder pass (RadBatch::enc). The core drives the pass and
|
|
* reads its result, so the plugin declares it. A plugin that declares none serves text only, and
|
|
* the server refuses a media part by name.
|
|
*
|
|
* This is the shape of the interface, not the model. The core stages patches of `patch_dim`
|
|
* elements and reads back one `n_embd`-wide row per `merge` of them; the tower itself is the
|
|
* plugin's business. How an image or video becomes patches is the processor's (core/mm), which
|
|
* reads its geometry from the container; the core refuses a program whose encoder disagrees with
|
|
* the processor about `patch_dim` or `merge`. */
|
|
/* A program declares at most two: one serving images and video, one serving audio. An audio
|
|
* encoder's "patch" is one feature frame (a mel row), its `merge` is frames per output row, and
|
|
* its `max_patches` is RadBuildCtx::max_audio_frames. RadBatch::enc names which one a pass runs. */
|
|
enum {
|
|
RAD_MM_IMAGE = 1u << 0,
|
|
RAD_MM_VIDEO = 1u << 1,
|
|
RAD_MM_AUDIO = 1u << 2,
|
|
};
|
|
|
|
typedef struct RadEncoderDecl {
|
|
const char* name; /* "vision" -- for messages */
|
|
uint32_t modalities; /* RAD_MM_* this encoder serves: IMAGE|VIDEO, or AUDIO alone */
|
|
int64_t patch_dim; /* elements in one input patch row */
|
|
int64_t merge; /* input patches per output row */
|
|
int64_t max_patches; /* must equal RadBuildCtx::max_enc_patches (max_audio_frames) */
|
|
int64_t n_embd; /* the width of an output row, the model's embedding width */
|
|
/* [max_patches / merge, n_embd] bf16: where a pass leaves its rows. The core reads it after
|
|
* the pass and before any other pass runs, so the planner may share its bytes with buffers
|
|
* no encoder pass touches. */
|
|
rad_buf out;
|
|
} RadEncoderDecl;
|
|
|
|
int rad_declare_encoder(RadBuilder* b, const RadEncoderDecl* d);
|
|
|
|
/* ================================================================== the image generator */
|
|
/* A program that makes images: a conditioning network reads a prompt (and pictures), a denoiser
|
|
* turns a noise latent into an image latent over a number of steps, and a decoder turns the
|
|
* latent into pixels. The core drives the passes, so the plugin declares them.
|
|
*
|
|
* Declaring one makes the program a generator, driven by passes rather than token steps
|
|
* (RadBatch::gen):
|
|
*
|
|
* RAD_GEN_PASS_COND one conditioning sequence (the prompt and any reference pictures)
|
|
* encoded into the generator's KV group. Once per request, twice with
|
|
* guidance (the negative prompt is its own sequence).
|
|
* RAD_GEN_PASS_DENOISE one step of every image the pass carries: each image's latent rows,
|
|
* read from and written back to the plugin's state at the image's lane,
|
|
* against its conditioning's KV.
|
|
* RAD_GEN_PASS_DECODE one image's latent to pixels, in `image_out`, which the core reads back.
|
|
*
|
|
* A program declares exactly one of this, the other generators, logits or pooled vectors.
|
|
*
|
|
* The fields are the model's contract with a client: the prompt it was trained on, the sizes it
|
|
* makes, its sampler's schedule. The core turns a request into passes from them without naming
|
|
* any model. */
|
|
enum { RAD_GEN_PASS_COND = 1, RAD_GEN_PASS_DENOISE = 2, RAD_GEN_PASS_DECODE = 3 };
|
|
|
|
/* How the generator's sequence is laid out; the core builds it from the conditioning prompt.
|
|
*
|
|
* JOINT_GRID the prompt's rows after `drop_prefix`, each media slot standing for `slot_rows`
|
|
* rows that a reference picture's latent tokens take in raster order, then the
|
|
* image being made. Rotary positions are three components: a text row counts up
|
|
* in all three; a picture of h x w tokens holds the count in the first, and its row
|
|
* and column, centred on zero (-(n - n/2) .. n/2 - 1), in the other two, after which
|
|
* the count moves on by max(h, w). Text rows attend causally, a picture's rows to
|
|
* each other both ways, every row to every row before it. */
|
|
enum { RAD_GEN_LAYOUT_JOINT_GRID = 1 };
|
|
|
|
typedef struct RadGeneratorDecl {
|
|
const char* name; /* "qwen-image" -- for messages */
|
|
int32_t layout; /* RAD_GEN_LAYOUT_* */
|
|
|
|
/* The conditioning prompt, as text the core tokenises. `{prompt}` is replaced by the user's
|
|
* prompt; in `tmpl_edit`, `{images}` by one `tmpl_image` per reference picture, joined by
|
|
* `tmpl_image_sep`. In `tmpl_image`, `{n}` is the picture's 1-based number and `{image}` marks
|
|
* where the picture's own tokens go (the media processor's run for it, markers included). */
|
|
const char* tmpl_text;
|
|
const char* tmpl_edit;
|
|
const char* tmpl_image;
|
|
const char* tmpl_image_sep;
|
|
/* The tokens of this text at the start of the prompt are encoded but not handed to the
|
|
* generator; the conditioning starts after them (e.g. a fixed system turn). */
|
|
const char* drop_prefix;
|
|
/* Prompt for a transparent image, in the words the model was trained on; `{prompt}` is the
|
|
* user's description. Empty when the model makes no alpha. */
|
|
const char* tmpl_transparent;
|
|
|
|
/* Geometry. One latent token covers `latent_scale` x `latent_scale` pixels and holds
|
|
* `latent_channels` values; a side is a multiple of `size_multiple` pixels; an image has
|
|
* `pixel_channels` channels (4: RGBA). A media slot of the conditioning prompt (one encoder
|
|
* output row) stands for `slot_rows` rows of the generator's sequence. */
|
|
int64_t latent_channels;
|
|
int64_t latent_scale;
|
|
int64_t size_multiple;
|
|
int64_t pixel_channels;
|
|
int64_t slot_rows;
|
|
/* The largest image, in latent tokens; the plugin sizes its per-image buffers for it. */
|
|
int64_t max_tokens;
|
|
/* Reference pictures: at most `max_refs` a request, each resized to `ref_area` pixels. */
|
|
int64_t max_refs;
|
|
int64_t ref_area;
|
|
/* The size a request that names none is made at. */
|
|
int64_t default_width, default_height;
|
|
|
|
/* The sampler: flow matching, Euler steps from sigma 1 towards 0.
|
|
* A fixed schedule: `n_sigmas` values, the terminal 0 implied; a request may not change the
|
|
* step count. Otherwise `default_steps` sigmas from 1 to 1/steps, shifted:
|
|
* dyn_shift 0 sigma' = shift * s / (1 + (shift - 1) * s)
|
|
* dyn_shift 1 mu linear in the image's token count, 256 -> base_shift .. max_seq ->
|
|
* max_shift; shift_exp 1: exp(mu) / (exp(mu) + (1/s - 1)), 0: mu / (mu + ...)
|
|
* then, with shift_terminal > 0, stretched so the last sigma is shift_terminal. */
|
|
int64_t default_steps;
|
|
int32_t n_sigmas;
|
|
const double* sigmas;
|
|
double shift;
|
|
int32_t dyn_shift;
|
|
int32_t shift_exp;
|
|
double base_shift, max_shift;
|
|
int64_t base_seq, max_seq;
|
|
double shift_terminal;
|
|
/* Guidance a request gets when it names none; 1 is off. */
|
|
double default_cfg;
|
|
|
|
/* `max_lanes` images may be in flight at once, each in a lane of the plugin's own state.
|
|
* `image_out` is [max_tokens * latent_scale^2, pixel_channels] u8 on the device: row-major
|
|
* pixels of the image a decode pass made. */
|
|
int64_t max_lanes;
|
|
rad_buf image_out;
|
|
} RadGeneratorDecl;
|
|
|
|
int rad_declare_generator(RadBuilder* b, const RadGeneratorDecl* d);
|
|
|
|
/* ================================================================== the audio generator */
|
|
/* A program that makes audio from text: an autoregressive language model writes the piece frame by
|
|
* frame, with classifier-free guidance (each request runs a conditional and an unconditional
|
|
* branch in step), and either a denoiser turns the frames' hidden states into audio latents window
|
|
* by window, which a vocoder turns into samples, or (`voc_chunk` > 0) a vocoder plays the frames
|
|
* themselves a few at a time as they are made. The core drives the passes (RadBatch::gen,
|
|
* RadAudioGenBatch):
|
|
*
|
|
* RAD_GEN_PASS_PROMPT the prompt of one or more requests, both branches, as an ordinary causal
|
|
* prefill into the KV group: two sequences a request, the conditional one
|
|
* first. A long prompt takes several passes, each a chunk of both branches,
|
|
* unless the declaration asks for whole prompts (`prompt_whole`). The pass
|
|
* that ends a prompt leaves the branches' last hidden states in the
|
|
* request's lane.
|
|
* RAD_GEN_PASS_FRAME one frame of every request in its autoregressive stage: the plugin samples
|
|
* the frame from the lane's last hidden states, keeps what the denoiser
|
|
* conditions on, and runs the language model over the frame's embedding
|
|
* (one row a branch, appended to that branch's KV). It writes `status` for
|
|
* each lane: >= 0 a frame was made, -1 the model ended the piece instead.
|
|
* RAD_GEN_PASS_WINDOW one denoising step of one or more windows, each a run of a request's frames
|
|
* (two entries a window with guidance, conditional and unconditional).
|
|
* RAD_GEN_PASS_VOCODE one finished window's latents to samples, in `audio_out`, which the core
|
|
* reads back; with `voc_chunk`, the frames [voc_frame0, voc_frame0 +
|
|
* voc_latents) of one lane, of which the first `voc_left` are context and
|
|
* only the rest are played.
|
|
*
|
|
* Windows overlap: window k covers frames [k win_hop, k win_hop + win_frames) and is stepped once
|
|
* the language model has made all of them, or ended. A window joins its predecessor through the
|
|
* latents they share, so a request's windows run in order, and window k + 1 does not start before
|
|
* window k is vocoded. The last window ends at the last frame; there is a window k >= 1 only while
|
|
* frames remain past k win_hop + (win_frames - win_hop), so no window is all overlap.
|
|
*
|
|
* A program declares exactly one of this, the other generators, logits or pooled vectors. */
|
|
enum { RAD_GEN_PASS_PROMPT = 4, RAD_GEN_PASS_FRAME = 5, RAD_GEN_PASS_WINDOW = 6,
|
|
RAD_GEN_PASS_VOCODE = 7 };
|
|
|
|
typedef struct RadAudioGenDecl {
|
|
const char* name; /* "minimax-music3" -- the served model id and messages */
|
|
|
|
/* The prompt, as text the core tokenises: `{instructions}` is replaced by the request's
|
|
* instructions (a piece's description, a voice's) and `{input}` by its input text (lyrics, the
|
|
* words to speak), each first rewritten by the plugin's rad_arch_text_prepare when it exports
|
|
* one. Special tokens are spelled as text. */
|
|
const char* tmpl;
|
|
/* The unconditional branch is the conditional prompt with every token but the first
|
|
* `neg_keep_head` and the last `neg_keep_tail` replaced by `neg_token`, so the two branches
|
|
* are the same length and step together. */
|
|
int32_t neg_token;
|
|
int32_t neg_keep_head, neg_keep_tail;
|
|
/* The longest prompt in tokens, and the most frames a request makes. */
|
|
int64_t max_prompt;
|
|
int64_t max_frames;
|
|
/* frame_rate_num / frame_rate_den frames a second. */
|
|
int64_t frame_rate_num, frame_rate_den;
|
|
|
|
/* Windows: `win_frames` frames, a new one every `win_hop`. A window of n frames has
|
|
* floor(n * lat_num / lat_den) latents of `latent_channels`, and each latent is
|
|
* `samples_per_latent` samples of `channels` channels at `sample_rate`. Where two windows meet,
|
|
* the earlier one's last `win_crop_right` latents and the later one's first `win_crop_left` are
|
|
* not played; the first window keeps its start and the last its end. */
|
|
int64_t win_frames, win_hop;
|
|
/* Rows the denoiser puts ahead of a window's latents in each entry of a WINDOW pass (a
|
|
* timestep token); the core counts them into the pass's rows. */
|
|
int64_t win_lead;
|
|
int64_t lat_num, lat_den;
|
|
int64_t latent_channels;
|
|
int64_t win_crop_left, win_crop_right;
|
|
int64_t samples_per_latent;
|
|
int64_t sample_rate;
|
|
int64_t channels;
|
|
|
|
/* The denoiser's sampler: `default_steps` Euler steps of flow matching from t = 0 (noise) to
|
|
* t = 1 (data), t_i = i / steps; guidance `default_cfg` (1 is off), v = u + cfg (c - u). */
|
|
int64_t default_steps;
|
|
double default_cfg;
|
|
|
|
/* `max_lanes` requests may be in flight at once, each in a lane of the plugin's state.
|
|
* `status` is [>= max_lanes] i32 on the device, indexed by lane (see FRAME above); `audio_out`
|
|
* is [>= the samples of the longest window, channels] f32 on the device, interleaved, in
|
|
* [-1, 1]: what a VOCODE pass plays, its crops already applied. */
|
|
int64_t max_lanes;
|
|
rad_buf status;
|
|
rad_buf audio_out;
|
|
|
|
/* The prompt of a request without instructions, `{input}` its only slot; NULL makes the
|
|
* instructions required. */
|
|
const char* tmpl_plain;
|
|
/* The unconditional branch's own template, rendered from the request's texts as `tmpl` is;
|
|
* NULL derives it from the conditional prompt by `neg_token` as above. With it the branches
|
|
* may differ in length, and a request whose guidance is 1 runs the conditional prompt in both
|
|
* (the guided logits are then the conditional ones). */
|
|
const char* neg_tmpl;
|
|
/* Nonzero: a prompt is never split across PROMPT passes (an encoder that reads it at once);
|
|
* the pass rows must hold both branches of the longest. Required with `neg_tmpl`. */
|
|
int32_t prompt_whole;
|
|
/* The first frame a request keeps: frames before it only move the model past the prompt. */
|
|
int32_t first_frame;
|
|
/* Nonzero: no denoiser and no windows. The vocoder plays the kept frames as they are made, at
|
|
* most `voc_chunk` a VOCODE pass, reading `voc_context` frames before them again (and not
|
|
* playing them) so that a causal vocoder needs no state between passes. A frame is then one
|
|
* latent: `lat_num` = `lat_den` = 1, `samples_per_latent` samples a frame. */
|
|
int64_t voc_chunk;
|
|
int64_t voc_context;
|
|
} RadAudioGenDecl;
|
|
|
|
int rad_declare_audio_generator(RadBuilder* b, const RadAudioGenDecl* d);
|
|
|
|
/* ================================================================== the video generator */
|
|
/* A program that makes a video and its soundtrack from text: a text encoder reads the prompt, one
|
|
* denoiser steps a packed sequence holding the prompt's rows, the soundtrack's latents and the
|
|
* video's latents together (flow matching, one forward a step, no guidance), and two decoders turn
|
|
* the latents into frames and samples. The core drives the passes (RadBatch::gen,
|
|
* RadVideoGenBatch), one request a pass:
|
|
*
|
|
* RAD_GEN_PASS_VCOND the request's prompt, an ordinary non-paged pass over its tokens: the
|
|
* text encoder, and what the denoiser reads of the prompt at every step,
|
|
* into the request's lane. It carries the request's whole schedule, so
|
|
* what depends only on a step's timesteps can be computed once here.
|
|
* RAD_GEN_PASS_VSTEP one denoising step of one lane over the packed sequence
|
|
* [text | audio | video]: the prompt's rows, then the soundtrack's
|
|
* `audio_channels` blocks of latents (channel-major, one row a latent),
|
|
* then the video's patches (frame-major, then row-major). The first step
|
|
* draws the lane's noise.
|
|
* RAD_GEN_PASS_ADECODE the lane's soundtrack to samples in `audio_out`, which the core reads
|
|
* back. Runs once, after the last step.
|
|
* RAD_GEN_PASS_VDECODE one chunk of the lane's video latents to frames in `frames_out`, which
|
|
* the core reads back. Chunks run in order after the soundtrack; each joins
|
|
* its predecessor through state the plugin keeps in the lane.
|
|
*
|
|
* Geometry. A video of n chunks has `frames_per_chunk n + frames_tail` frames at
|
|
* `fps_num / fps_den` a second and `latents_per_chunk n + latents_tail` latent frames, each of
|
|
* (height / latent_scale) x (width / latent_scale) latents of `latent_channels`; the denoiser
|
|
* reads them in `patch` x `patch` patches. Chunk c of n is `frames_per_chunk` frames, the last
|
|
* `frames_per_chunk + frames_tail`. A side is a multiple of `size_multiple` pixels, at least
|
|
* `min_side`, the area at most `max_area`, and the long side over the short at most `max_aspect`.
|
|
* The soundtrack has round(frames / fps * audio_rate) latents a channel, each
|
|
* `sample_rate / audio_rate` samples.
|
|
*
|
|
* The rotary positions of a pass's rows are the plugin's (rad_arch_video_positions), which a video
|
|
* generator must export.
|
|
*
|
|
* The sampler: a request's `steps` are sigma grid points, the terminal 0 included, as diffusers'
|
|
* MiniMax-H3 counts them: sigma_i = shift s / (1 + (shift - 1) s) over s = linspace(1, 0, steps)
|
|
* in f32 (consecutive duplicates dropped), and a model evaluation at each timestep
|
|
* t_i = 1 - sigma_i but the last; one schedule for the video (`shift_video`) and one for the
|
|
* soundtrack (`shift_audio`), stepped together. A few-step (distilled) checkpoint instead declares
|
|
* the base times it was trained on (`rungs`, below): s = rungs then 0, each taken to f32, the same
|
|
* shifts applied, and the step count fixed at n_rungs + 1.
|
|
*
|
|
* Latent upscaling, for a plugin with a network that enlarges a video latent (`upscale_max` > 1):
|
|
* a request with a factor f > 1 is denoised at a smaller size first. Its video is W x H; each side
|
|
* divided by f and rounded to the nearest multiple of `size_multiple` (at least one multiple, at
|
|
* most the side) gives the small size W' x H'. The request's evaluations but its last `refine` run
|
|
* at W' x H', the last of them stepping to sigma 0 (its clean estimate) instead of the next grid
|
|
* point. Then one pass:
|
|
*
|
|
* RAD_GEN_PASS_VUPSCALE the lane's video latent from W' x H' to W x H through the plugin's
|
|
* network, told the factor (W / W' + H / H') / 2; then both latents
|
|
* re-noised to the sigmas of the first evaluation at W x H, each with a
|
|
* stream of its own seed other than the one its first step drew.
|
|
*
|
|
* The last `refine` evaluations run at W x H, and the decodes follow as for any request. Every
|
|
* pass carries the geometry its latent has at that point: a step before the VUPSCALE pass is at
|
|
* W' x H'. The schedule is the request's own, whatever size each evaluation runs at.
|
|
*
|
|
* Reference conditioning (ref2va), for a program whose container carries the VAE encoders
|
|
* (`encodes`, below): before the prompt pass the driver encodes the request's reference media,
|
|
* one media a pass. Nothing the passes make is read back — each latent lands in the lane, and the
|
|
* step's packed sequence carries it:
|
|
*
|
|
* RAD_GEN_PASS_VENCODE one reference video's frames, staged in `enc_pixels` (RadVideoGenBatch),
|
|
* to the lane's video latents.
|
|
* RAD_GEN_PASS_AENCODE one reference audio's samples, staged in `enc_samples`, to the lane's
|
|
* soundtrack latents.
|
|
*
|
|
* `enc_kind` names the media (RAD_ENC_*, rad_runtime.h) and `enc_slot` is its 1-based number
|
|
* within that kind; RAD_ENC_TAIL — slot 0 — is the request's continuity tail, the frames of the
|
|
* clip it continues, which rides Vencode. The media an encoder takes is bounded by the model:
|
|
* reference pictures are one frame, reference video and the tail up to 289 frames, every frame
|
|
* at most 256 x 256 with both sides multiples of 16, and a reference audio up to 500 000
|
|
* samples; `rad_arch_encode_geom` (below) is the authority and refuses what it cannot encode. A
|
|
* program without the encoders declares neither pass and `encodes` stays 0, so the server
|
|
* refuses a request that carries references.
|
|
*
|
|
* A program declares exactly one of this, the other generators, logits or pooled vectors. */
|
|
enum { RAD_GEN_PASS_VCOND = 8, RAD_GEN_PASS_VSTEP = 9, RAD_GEN_PASS_ADECODE = 10,
|
|
RAD_GEN_PASS_VDECODE = 11, RAD_GEN_PASS_VUPSCALE = 12,
|
|
RAD_GEN_PASS_VENCODE = 13, RAD_GEN_PASS_AENCODE = 14 };
|
|
|
|
typedef struct RadVideoGenDecl {
|
|
const char* name; /* "minimax-h3" -- the served model id and messages */
|
|
|
|
/* The prompt as text the core tokenises, `{prompt}` replaced by the request's; special tokens
|
|
* are spelled as text. No beginning-of-sequence token is added. */
|
|
const char* tmpl;
|
|
int64_t max_text; /* the longest prompt, in tokens */
|
|
|
|
/* Geometry (above). */
|
|
int64_t fps_num, fps_den;
|
|
int64_t frames_per_chunk, frames_tail;
|
|
int64_t latents_per_chunk, latents_tail, dec_latents;
|
|
int64_t min_chunks, max_chunks;
|
|
int64_t latent_scale, latent_channels, patch;
|
|
int64_t size_multiple, min_side, max_area, max_aspect;
|
|
/* The size and length a request that names none is made at. */
|
|
int64_t default_width, default_height, default_chunks;
|
|
|
|
/* The soundtrack. */
|
|
int64_t sample_rate, audio_rate, audio_channels, audio_latent_channels;
|
|
|
|
/* The sampler (above). */
|
|
int64_t default_steps, max_steps;
|
|
double shift_video, shift_audio;
|
|
|
|
/* `max_lanes` requests may hold state at once. `frames_out` is [>= the frames of the largest
|
|
* chunk, height, width, 3] u8 on the device, the frames a VDECODE pass made, row-major RGB;
|
|
* `audio_out` is [>= the samples of the longest soundtrack, audio_channels] f32, interleaved,
|
|
* what an ADECODE pass made. */
|
|
int64_t max_lanes;
|
|
rad_buf frames_out;
|
|
rad_buf audio_out;
|
|
|
|
/* A trained schedule when `n_rungs` > 0: base times strictly decreasing in (0, 1] (a
|
|
* trainer's timesteps over its 1000-step clock), each modality's sigmas their shifts as above.
|
|
* `default_steps` and `max_steps` are then both n_rungs + 1 and a request may not name another
|
|
* count. 0 is the linspace schedule. */
|
|
int32_t n_rungs;
|
|
const double* rungs;
|
|
|
|
/* Latent upscaling (above): the largest factor a request may ask for, 0 when the plugin has no
|
|
* upscaler; and the evaluations at the full size a request that names none runs, at least 1
|
|
* and fewer than the default schedule's. */
|
|
double upscale_max;
|
|
int32_t upscale_refine;
|
|
|
|
/* Reference media a request may carry (ref2va): the ceilings the server enforces and
|
|
* `/v1/models` reports. All 0 — the zero default — on a generator that conditions on no
|
|
* reference media, which is how the server refuses the task outright; a generator declares
|
|
* them only once it can actually consume the bytes. Trailing since ABI 22: an older plugin's
|
|
* declaration reads none and keeps the 0 default. */
|
|
int64_t max_ref_images, max_ref_videos, max_ref_audios;
|
|
|
|
/* Nonzero when the container carries the video and audio VAE encoders, so the driver may
|
|
* stage Vencode and Aencode passes (above). Trailing since ABI 23: an older plugin declares
|
|
* none and keeps the 0 default. */
|
|
int32_t encodes;
|
|
|
|
/* The ceilings one encode pass may carry, so the driver can size its staging slab: a video's
|
|
* clip-padded frame count, the encoder's side (square, a multiple of its downsample), and an
|
|
* audio's sample count. Only meaningful with `encodes`; 0 otherwise. The driver stages at most
|
|
* one media item per pass, so the slab is these bounds, not a per-request sum. Trailing since
|
|
* ABI 23 (the encoder's own pass enums and batch fields): an older plugin declares none. */
|
|
int64_t max_enc_frames, max_enc_side, max_enc_samples;
|
|
|
|
/* The encoder's spatial downsample, so the server can snap a reference's sides to a multiple
|
|
* of it before handing them over (the geom refuses any other). Distinct from `size_multiple`,
|
|
* which bounds the *generated* video's size and is a further multiple of this. Only meaningful
|
|
* with `encodes`; 0 otherwise. Trailing since ABI 23. */
|
|
int32_t enc_ratio;
|
|
|
|
/* Nonzero when this generator's denoiser *consumes* the reference table the driver stages in
|
|
* `RadVideoGenBatch::ref_tab` — i.e. its per-step packed sequence already interleaves the
|
|
* reference blocks between the text and the audio/video targets, so the driver must size and
|
|
* stage those rows (via `rad_arch_video_pack`) rather than the plain `[text|audio|video]`
|
|
* layout. Trailing since ABI 23: an older plugin declares none and keeps the 0 default, which
|
|
* is exactly the plain path — the driver then never widens rows or segment boundaries and the
|
|
* generated bytes are unchanged. It is the arch, not the driver, that knows whether its own
|
|
* denoiser reads `ref_tab`, so this flag is what makes staging and consumption flip together. */
|
|
int32_t ref_rows;
|
|
|
|
/* The largest MODULATION-GROUP count this generator's modulation table holds -- its denoiser's
|
|
* `kMaxSegs`. A modulation cell is a pure function of a segment's KIND (its modality and its
|
|
* timestep-embedding block), so runs of consecutive segments that share a cell collapse into one
|
|
* group and the table is sized by the GROUP count, not the reference count: nine reference photos
|
|
* pack into four groups (prompt, all-photos, soundtrack, video). The attention still takes the
|
|
* FINE per-segment table (which may hold far more segments than this), so this is not a limit on
|
|
* how many references ride -- only on how many DISTINCT cells the sequence changes through. The
|
|
* driver refuses a request whose merged group count exceeds it at submit (it lays the pack out and
|
|
* merges it itself, via `rad_arch_video_mod_segs`, so it knows before dispatch) rather than letting
|
|
* the arch abort mid-generation. 0 = no reference consumption, so no such limit. Trailing since
|
|
* ABI 23: an older plugin declares none and keeps the 0 default. */
|
|
int32_t max_ref_segs;
|
|
|
|
/* Debug introspection of the encoder outputs (diagnostics only, not part of the serving
|
|
* contract). A generator that carries the VAE encoders names its internal audio / video latent
|
|
* buffers (b_enca / b_evc) and their byte sizes here, purely so a driver built with the
|
|
* R4D_ENC_DEBUG environment can read them back through the ordinary pass readback and log a
|
|
* digest — the one way to see the encoder's numbers without a device→host copy the arch step
|
|
* itself cannot issue. With the environment unset the driver never touches these fields, so a
|
|
* normal request is byte-unchanged; a container that carries no encoders leaves them zero.
|
|
* Trailing since ABI 23: an older plugin declares none and keeps the 0 default. */
|
|
rad_buf dbg_enca;
|
|
int64_t dbg_enca_bytes;
|
|
rad_buf dbg_evc;
|
|
int64_t dbg_evc_bytes;
|
|
/* The audio mean_proj OUTPUT (b_amb2), i.e. the latent BEFORE the fixed (z-mean)/std norm —
|
|
* the mono, last-channel slice of it. Read only when R4D_ENC_MP names it in place of b_enca,
|
|
* to split whether a latent-scale gap is introduced by the body or by the norm op. Trailing
|
|
* since ABI 23 with the fields above: an older plugin declares none and keeps the 0 default. */
|
|
rad_buf dbg_ae_mp;
|
|
int64_t dbg_ae_mp_bytes;
|
|
/* The audio pre_block OUTPUT (b_ax2), one stage before mean_proj; bf16, the mono last channel.
|
|
* Read only when R4D_ENC_SEL=pre, to split a body-scale gap between pre_block and mean_proj.
|
|
* Trailing since ABI 23 with the fields above. */
|
|
rad_buf dbg_ae_pre;
|
|
int64_t dbg_ae_pre_bytes;
|
|
/* The audio mean_proj INPUT (b_amb), i.e. the pre_block output cast to f32. Read with
|
|
* R4D_ENC_SEL=amb: it is the reliable f32 twin of the arena-rotated bf16 b_ax2, and splits a
|
|
* body-scale gap between pre_block and mean_proj. Trailing since ABI 23 with the fields above. */
|
|
rad_buf dbg_ae_amb;
|
|
int64_t dbg_ae_amb_bytes;
|
|
} RadVideoGenDecl;
|
|
|
|
int rad_declare_video_generator(RadBuilder* b, const RadVideoGenDecl* d);
|
|
|
|
/* ================================================================== diagnostics */
|
|
/* A plugin may annotate the graph dump. Free-form, printed under --debug-graph. */
|
|
void rad_note(RadBuilder* b, const char* fmt, ...);
|
|
|
|
/* ================================================================== the arch plugin exports */
|
|
const char* rad_arch_id(void);
|
|
|
|
/* The quantisation this plugin serves: the second half of the key that selects it.
|
|
*
|
|
* spec §2.4 is one plugin per (architecture, quantisation): a w4a8 model wants
|
|
* rmsnorm_had_quant_i8 where a bf16 model wants a plain rmsnorm, and branching on a quant
|
|
* descriptor inside one plugin obscures what actually runs. So `qwen35_bf16` and `qwen35_fp8` both
|
|
* answer "qwen35" to rad_arch_id and are told apart here, by RadModelMeta::quant.
|
|
*
|
|
* The match is exact, with no fallback. Unquantised containers and plugins both say ""; the
|
|
* symbol is optional and a plugin without it reads as "". A fallback to a dense plugin would fail
|
|
* later at the name map with a worse message than "no plugin claims qwen35 at fp8_e4m3; qwen35 is
|
|
* served at: (unquantised)". */
|
|
const char* rad_arch_quant(void);
|
|
|
|
/* What the plugin can say before it declares anything. Optional: a plugin that does not export
|
|
* it answers zero to everything, each field's default.
|
|
*
|
|
* The main case is the draft depth, a model property that is an input to declaration:
|
|
* `RadBuildCtx::max_spec` sizes buffers, bands kernels and sets the scheduler's window, so it is
|
|
* fixed before rad_arch_declare runs, yet only the plugin reads the drafter's configuration. 0
|
|
* means the container carries no drafter.
|
|
*
|
|
* --num-speculative-tokens overrides the answer, and the plugin sees the result in
|
|
* RadBuildCtx::max_spec; a plugin whose checkpoint cannot serve that depth refuses in declare. */
|
|
typedef struct RadArchProbe {
|
|
int draft_depth;
|
|
|
|
/* Nonzero when `draft_depth` is the only depth the drafter serves. A serial head's depth is a
|
|
* throughput operating point (each extra pass costs one pass and acceptance decays, but it
|
|
* still runs). A block drafter's depth is the block length its checkpoint was trained at (its
|
|
* convolution masks the position within the block), so any other depth is refused.
|
|
*
|
|
* `--num-speculative-tokens auto` takes `draft_depth` either way. rad-convert does not: a
|
|
* container carries everything the model can serve, so it widens to `draft_depth` only when
|
|
* this says the drafter has no choice; otherwise a deployment wanting a deeper serial draft
|
|
* would have to convert again. */
|
|
int draft_depth_fixed;
|
|
|
|
/* The plugin honours RadBuildCtx::shape_probe: a sizing declare leaves the state its step
|
|
* function reads untouched. Zero means the core does not ask, and the activation arena is not
|
|
* lent. */
|
|
int shape_probe_ok;
|
|
|
|
/* The encoder's merge, input patches per output row (RadEncoderDecl::merge), when the plugin
|
|
* declares an encoder. A pass carries whole output rows, so the core rounds the pass size it
|
|
* picks itself (`--mm-max-patches auto`) down to a multiple of this; an operator-named size is
|
|
* kept, and the plugin refuses one that does not divide. 0 is no opinion. */
|
|
int64_t enc_merge;
|
|
|
|
/* The longest clip the audio encoder takes, in feature frames (a whole number of its merge);
|
|
* the engine sizes the audio pass with it (RadBuildCtx::max_audio_frames). 0 is no audio. */
|
|
int64_t audio_frames;
|
|
|
|
/* Nonzero for a generator (rad_declare_generator, rad_declare_audio_generator): the rows its
|
|
* largest pass carries. The engine sizes the step for it before declare (an image's denoise
|
|
* pass carries all of its latent rows at once, twice with guidance; an audio window's step all
|
|
* of the window's) and turns off what a generator does not use. 0 is a program that
|
|
* generates text or embeds. */
|
|
int64_t gen_rows;
|
|
/* The most patches the generator's conditioning pass encodes: every reference picture a
|
|
* request may carry, at the model's resize target. This is the encoder's pass size
|
|
* (max_enc_patches). */
|
|
int64_t gen_patches;
|
|
/* Nonzero for an audio generator, which the engine sizes before declare as it does an image
|
|
* generator, with these differences: two sequences a request (its branches), and `gen_ctx`
|
|
* the longest a sequence grows (the whole prompt and every frame), which is the context the
|
|
* KV group is sized for. */
|
|
int32_t gen_audio;
|
|
int64_t gen_ctx;
|
|
/* Nonzero for a video generator, which the engine sizes as it does an image generator from
|
|
* `gen_rows` (its largest pass: a step's packed sequence at the largest size and length). It
|
|
* keeps no KV. */
|
|
int32_t gen_video;
|
|
} RadArchProbe;
|
|
|
|
int rad_arch_probe(const RadModelMeta* meta, RadArchProbe* out);
|
|
|
|
/* How this model spells a turn, as data. Optional: without it the engine derives the format by
|
|
* analysing the chat template.
|
|
*
|
|
* One streaming parser driven by this description splits a reply into reasoning, content and tool
|
|
* calls, and the lazy grammar that constrains a call and its trigger words are generated from it,
|
|
* so a model whose format the template analysis misses or gets wrong is supported by stating it
|
|
* here. It describes the tagged family:
|
|
*
|
|
* [reasoning_open] reasoning [reasoning_close] content
|
|
* [call_open] name_prefix NAME name_suffix
|
|
* arg_prefix ARG arg_name_suffix arg_value_prefix VALUE arg_value_suffix ...
|
|
* call_end [call_close]
|
|
*
|
|
* Qwen3.8, for example, is "<think>\n" / "\n</think>\n\n", calls "<tool_call>\n" / "</tool_call>",
|
|
* "<function=" / ">\n" / "</function>\n", arguments "<parameter=" / ">\n" / "" / "</parameter>\n".
|
|
* Whitespace at the ends of a tag is optional when matching. Every string is NUL-terminated
|
|
* UTF-8; NULL reads as "". The engine copies what it needs while loading, so static storage in
|
|
* the plugin is enough.
|
|
*
|
|
* The other family is a call written as a name and one object holding its arguments
|
|
* (args_form = RAD_CHAT_ARGS_OBJECT):
|
|
*
|
|
* [call_open] name_prefix NAME name_suffix { key: value, ... } [call_end] [call_close]
|
|
*
|
|
* The object is JSON, or a dialect of it: strings between object_string_open and
|
|
* object_string_close, their text verbatim, and keys written bare (object_bare_keys). Gemma 4 is
|
|
* "<|tool_call>" / "<tool_call|>", "call:" / "" / "", strings "<|\"|>" / "<|\"|>", bare keys --
|
|
* `<|tool_call>call:get_weather{city:<|"|>Paris<|"|>}<tool_call|>` -- and the reply's JSON
|
|
* arguments are that object respelled as JSON. The arg_* fields describe tagged arguments only
|
|
* and are empty in this family.
|
|
*
|
|
* The struct only grows at its end. `struct_size` is sizeof(RadChatFormat) as the plugin was
|
|
* compiled; the engine reads only the fields that size covers and treats the rest as zero, so a
|
|
* plugin built against an older header keeps loading, and a new field must make zero mean "as
|
|
* before". So this export does not move RAD_ABI_VERSION. A field reordered, retyped or removed is
|
|
* a layout change and bumps RAD_ABI_VERSION like any other. */
|
|
enum {
|
|
RAD_CHAT_VALUE_RAW = 0, /* the text between the delimiters, verbatim */
|
|
RAD_CHAT_VALUE_CDATA = 1, /* verbatim, unless wrapped in <![CDATA[ ... ]]>, which is removed */
|
|
RAD_CHAT_VALUE_JSON = 2 /* a JSON value; text that does not parse as JSON is taken raw */
|
|
};
|
|
|
|
enum {
|
|
/* The generation prompt shows whether the reply starts inside a reasoning block: whatever
|
|
* follows the first `reasoning_open` in it is the start of the reply's own text. */
|
|
RAD_CHAT_THINK_FROM_PROMPT = 0,
|
|
RAD_CHAT_THINK_ALWAYS = 1, /* the reply is inside a reasoning block from its first byte */
|
|
RAD_CHAT_THINK_NEVER = 2 /* the model opens its own block, if any */
|
|
};
|
|
|
|
enum {
|
|
RAD_CHAT_ARGS_TAGGED = 0, /* each argument in its own tags: arg_prefix .. arg_value_suffix */
|
|
RAD_CHAT_ARGS_OBJECT = 1 /* one object after the name, in JSON or the object_* dialect */
|
|
};
|
|
|
|
typedef struct RadChatFormat {
|
|
uint32_t struct_size;
|
|
const char* name; /* for the startup log; NULL is the plugin's name */
|
|
|
|
const char* reasoning_open; /* "" when the model does not reason */
|
|
const char* reasoning_close;
|
|
/* Literals that end a reasoning block the model forgot to close, when a call starts there.
|
|
* Each must be the call opener: `call_open`, or `name_prefix` when there is no call_open. */
|
|
const char* const* reasoning_breaks;
|
|
uint32_t n_reasoning_breaks;
|
|
uint32_t reasoning_in_prompt; /* RAD_CHAT_THINK_* */
|
|
|
|
/* Around all of a turn's calls, and between two calls beyond whitespace. The parser does not
|
|
* read these yet, so a format that sets one is refused at startup by name; they are fields so
|
|
* supporting them later does not change the struct. */
|
|
const char* section_open;
|
|
const char* section_close;
|
|
const char* call_separator;
|
|
|
|
const char* call_open; /* around each call; "" when calls are not wrapped */
|
|
const char* call_close;
|
|
const char* name_prefix; /* "" when the model has no tool calls */
|
|
const char* name_suffix;
|
|
const char* call_end;
|
|
const char* arg_prefix;
|
|
const char* arg_name_suffix; /* "" when arg_value_prefix alone ends the name */
|
|
const char* arg_value_prefix;
|
|
const char* arg_value_suffix;
|
|
|
|
uint32_t string_value; /* RAD_CHAT_VALUE_RAW or _CDATA: a string-typed argument */
|
|
uint32_t other_value; /* RAD_CHAT_VALUE_JSON: any other type (or _RAW, as text) */
|
|
/* Non-zero: one newline at each end of a value is punctuation, not part of the value -- at
|
|
* its start only where the tag before it does not end with that newline itself. */
|
|
uint32_t value_newlines;
|
|
|
|
/* The literal words that start constrained decoding of a call. None: no grammar, and the
|
|
* reply is read unconstrained. */
|
|
const char* const* triggers;
|
|
uint32_t n_triggers;
|
|
|
|
/* RAD_CHAT_ARGS_*: how a call's arguments are written. Zero is the tagged family above. With
|
|
* RAD_CHAT_ARGS_OBJECT, name_suffix may be empty -- the name then ends where the object
|
|
* begins -- and so may call_end, when the object's own closing brace ends the call. */
|
|
uint32_t args_form;
|
|
/* RAD_CHAT_ARGS_OBJECT: a string delimiter of the object's dialect besides JSON's `"`. What
|
|
* stands between the two is the string, verbatim -- the dialect has no escapes. NULL: the
|
|
* object writes JSON strings only. */
|
|
const char* object_string_open;
|
|
const char* object_string_close;
|
|
/* RAD_CHAT_ARGS_OBJECT: non-zero when the dialect writes a key bare, `key:value`. */
|
|
uint32_t object_bare_keys;
|
|
|
|
/* Non-zero: a reasoning block may open again later in the reply (after its answer, between
|
|
* calls) and is read as reasoning there too, and a reasoning_close with no block open is not
|
|
* part of the answer. Zero: only a block the reply starts with is reasoning. */
|
|
uint32_t reasoning_anywhere;
|
|
} RadChatFormat;
|
|
|
|
/* Return the format, or NULL for "derive it from the chat template". Called once at startup. */
|
|
const RadChatFormat* rad_arch_chat_format(const RadModelMeta* meta);
|
|
|
|
/* How a speech model's prompt carries audio, as data. Optional: a plugin that does not export it
|
|
* is a model the speech routes do not serve.
|
|
*
|
|
* It states the tokens that open and close a clip, the turn a transcription is asked in, how a
|
|
* language is named and forced, and where the transcript starts in the reply, so the core's
|
|
* routes know nothing of any one model. Qwen3-ASR, for example:
|
|
*
|
|
* audio_open "<|audio_start|>" audio_close "<|audio_end|>"
|
|
* transcribe_prompt "<|im_start|>system\n{context}<|im_end|>\n<|im_start|>user\n{audio}"
|
|
* "<|im_end|>\n<|im_start|>assistant\n{language}"
|
|
* language_prompt "language {language}<asr_text>"
|
|
* transcript_open "<asr_text>" language_lead "language "
|
|
* languages { "en", "English", "zh", "Chinese", ... }
|
|
*
|
|
* Every string is NUL-terminated UTF-8 and NULL reads as "". The engine copies what it needs while
|
|
* loading, so static storage in the plugin is enough.
|
|
*
|
|
* The struct only grows at its end, as RadChatFormat does: `struct_size` is sizeof(RadAudioFormat)
|
|
* as the plugin was compiled, the engine reads only the fields that size covers, and a field added
|
|
* later reads as zero for an older plugin. So this export does not move RAD_ABI_VERSION. */
|
|
typedef struct RadAudioFormat {
|
|
uint32_t struct_size;
|
|
|
|
/* The literal tokens around a clip's placeholder run, as the vocabulary spells them, for a
|
|
* model whose config names no id for them. NULL: the config's own (boa/eoa_token_id). */
|
|
const char* audio_open;
|
|
const char* audio_close;
|
|
|
|
/* The transcription prompt (POST /v1/audio/transcriptions), with three slots: {context} is
|
|
* the request's `prompt` text, {audio} the clip, and {language} `language_prompt` with the
|
|
* request's language named in it, or "" when the request names none and the model is to say
|
|
* which it heard. Each slot appears once. NULL: this model does not transcribe. */
|
|
const char* transcribe_prompt;
|
|
const char* language_prompt;
|
|
|
|
/* A reply whose language was not forced names it first: `language_lead` and the language's
|
|
* name, then `transcript_open`, then the transcript. "" for a model whose reply is the
|
|
* transcript alone. */
|
|
const char* transcript_open;
|
|
const char* language_lead;
|
|
|
|
/* The languages the model names, as n_languages (ISO 639-1 code, the model's own name) pairs:
|
|
* a request's `language` matches either spelling, and the name is what {language} takes. */
|
|
const char* const* languages;
|
|
uint32_t n_languages;
|
|
} RadAudioFormat;
|
|
|
|
/* Return the format, or NULL when this model serves no speech route. Called once at startup. */
|
|
const RadAudioFormat* rad_arch_audio_format(const RadModelMeta* meta);
|
|
|
|
/* A request's free text rewritten into the form the model was trained on, before it goes into the
|
|
* audio generator's prompt template (RadAudioGenDecl::tmpl). Optional: absent, the text goes in
|
|
* as given. Training data has conventions a client does not know -- section tags on lines of
|
|
* their own and lower case, no markdown in a description -- and only the plugin knows them.
|
|
*
|
|
* `field` is RAD_TEXT_*, `in` NUL-terminated UTF-8. Writes at most `cap` bytes, the NUL included,
|
|
* and returns the length the whole result needs without its NUL (call again with more room when
|
|
* it is >= cap), or a negative status for text the model cannot take. Called on HTTP threads, so
|
|
* it keeps no state. */
|
|
enum { RAD_TEXT_INSTRUCTIONS = 1, RAD_TEXT_INPUT = 2 };
|
|
int64_t rad_arch_text_prepare(int field, const char* in, char* out, int64_t cap);
|
|
|
|
/* The layout of a video generator's passes (rad_declare_video_generator), which only the model
|
|
* knows. `which` names the pass: RAD_VPOS_STEP, the packed sequence of every VSTEP pass of a
|
|
* request of geometry `g` -- its rows, and their rotary positions written to `pos` (f32 [3, rows]
|
|
* component-major) when it is non-null and `cap` rows hold them; RAD_VPOS_DECODE, the rows of one
|
|
* of its VDECODE passes, whose positions the plugin makes itself (`pos` is not written). Returns
|
|
* the rows, or a negative status for a geometry the model cannot take. Called on any thread, so it
|
|
* keeps no state. Required of a video generator. */
|
|
typedef struct RadVideoGeom {
|
|
int32_t n_text; /* the prompt's tokens */
|
|
int32_t frames; /* the video's frames */
|
|
int32_t lat_frames; /* its latent frames */
|
|
int32_t lat_h, lat_w; /* latents a frame, down and across */
|
|
int32_t n_audio; /* the soundtrack's latents a channel */
|
|
} RadVideoGeom;
|
|
enum { RAD_VPOS_STEP = 1, RAD_VPOS_DECODE = 2 };
|
|
int64_t rad_arch_video_positions(int which, const RadVideoGeom* g, float* pos, int64_t cap);
|
|
|
|
/* The packed sequence's segment boundaries for a video generator: cu[k] is the first row of
|
|
* segment k (modulation, attention sinks -- whatever segmentation the model lays out), and
|
|
* cu[n-1] the sequence's total rows -- the same rows RAD_VPOS_STEP positions. With `cu` null
|
|
* it returns the number of int32s needed; with `cap` too small, a negative status. Additive
|
|
* export; RAD_ABI_VERSION does not move -- a driver without it stages its own segmentation,
|
|
* which for a plain [text | audio | video] request is this table. Called on any thread. */
|
|
int64_t rad_arch_video_segs(const RadVideoGeom* g, int32_t* cu, int64_t cap);
|
|
|
|
/* The geometry of a video generator's encode passes (RAD_GEN_PASS_VENCODE, RAD_GEN_PASS_AENCODE;
|
|
* rad_builder.h, "Reference conditioning"): the i32 vector the driver stages into the pass's
|
|
* `enc_geom_d` device buffer, derived from the media's own shape, because what the network's
|
|
* strided convolutions and causal attention run at — the padded sample length, the latent frame
|
|
* count — is only the model's to know. `kind` is the pass value; `in` is four i64s
|
|
* {frames_or_samples, height, width, channels}, each unused entry 0. With `out` null it returns
|
|
* the number of int32s needed, or a negative status for media the model cannot take; with `out`
|
|
* it fills `out[cap]`. The driver stages the samples right-padded with zeros to the length this
|
|
* reports (entry 0), and copies these bytes to the device verbatim: it interprets nothing.
|
|
* Additive export; RAD_ABI_VERSION does not move — a driver without it drives no encode pass and
|
|
* the server refuses references. Called on any thread. */
|
|
int64_t rad_arch_encode_geom(int32_t kind, const int64_t* in, int32_t* out, int64_t cap);
|
|
|
|
/* The whole packed-sequence layout of a reference-conditioned video request: the general form
|
|
* of rad_arch_video_segs. `g` is the target's geometry and `ref_tab` the request's keyframes
|
|
* and VAE references -- RadVideoGenBatch::ref_tab, its int32 table verbatim -- or null when the
|
|
* request conditions on no VAE media, which lays out the plain [text | soundtrack | video]
|
|
* sequence. Returns the packed sequence's total rows -- the rows RAD_VPOS_STEP positions -- or
|
|
* a negative status for counts the model cannot present or caps too small to write into. With
|
|
* `seg` non-null it writes the segment boundaries (seg[k] the first row of segment k, of the n
|
|
* it needs) when `cap_seg` int32s hold them; with `pos` non-null the rotary positions (f32
|
|
* [3, rows]) when `cap_pos` rows hold them. A null `seg` and `pos` answer with the rows alone,
|
|
* which is all a submit needs to size the request. Additive export; RAD_ABI_VERSION does not
|
|
* move -- a driver without it refuses reference requests. Called on any thread. */
|
|
int64_t rad_arch_video_pack(const RadVideoGeom* g, const int32_t* ref_tab, int32_t* seg,
|
|
int64_t cap_seg, float* pos, int64_t cap_pos);
|
|
|
|
int rad_arch_declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* ctx);
|
|
/* Forward declaration; the signature is in rad_runtime.h. */
|
|
struct RadCtx;
|
|
struct RadBatch;
|
|
void rad_arch_step(struct RadCtx* c, const struct RadBatch* batch);
|
|
|
|
#ifdef __cplusplus
|
|
} /* extern "C" */
|
|
#endif
|
|
#endif /* RAD_BUILDER_H */
|