merge origin/main: the upstream LoRA-runtime refactor beneath the ref2va port

Merges upstream 1.10.0 (152 commits: the LoRA acquire/runtime refactor, the
form-1 LTX-2 scheduler family, guidance terms + negative prompts, decoder
selection, input_reference, new architectures) into the ref2va port branch.
Resolution rule throughout: keep BOTH feature sets; ABI numbering follows
upstream (RAD_ABI_VERSION 26, RAD_FORMAT_VER 2), with the port's video fields
unioned into RadVideoGenBatch/RadVideoGenDecl and abi_layout_table.h
regenerated from the live table.

Notable reconciliations:
  * videos.cpp: the task grammars are disjoint by capability -- keyframe tasks
    (i2va/l2va/flash-attn l2va/fl2va) are recognized only when decl.encodes,
    so upstream models keep refusing them as unknown tasks; input_reference,
    the LoRA tag/field pipeline, and the guidance/negative_prompt rendering
    coexist with refs/ref2va; the prompt put() helper hoisted to block scope
    for the media branch.
  * video_schedule(): upstream's (steps, video_tokens, refine) keeps its
    positional slots; the port's shift_video/shift_audio overrides move to the
    tail so upstream's schedule tests call unchanged.
  * r4d_selftest conv3d cases: the port's sp/st/causal/down/refl geometry and
    upstream's tpad/tpre fold into one reference (ft = causal ? 2 : tpad==2 ?
    2+tpre : 1); rms_mod rows keep the port's i8/fp8 quant modes alongside
    upstream's wadd/norm_once/norm parameters.
  * tests/oot_plugins.sh: the per-plugin cmake --install gains a --prefix
    staging dir -- plugin .so files still land in the absolute
    RAD_PLUGIN_INSTALL_HOME, but libr4d now also installs r4d_selftest to a
    relative bin/, which needs no root.

Host ctest: 112/113 (mm_test's webp gap pre-exists this merge); r4d_selftest,
engine_e2e, oot_plugins, abi_layout, all LoRA tests green.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
gearwave00001andClaude committed 2026-10-22 18:38:04 -05:00
commit a1a75da7e9
415 files changed
+78837 -5310

No files matched your search

+26 -2
View File
@@ -17,7 +17,7 @@ extern "C" {
* kind of plugin reports it, and a plugin that does not report exactly this version is refused at
* load with both versions printed. A new optional export (e.g. rad_plugin_reference) is not a
* change of shape: a plugin without it gets the default. */
#define RAD_ABI_VERSION 24u
#define RAD_ABI_VERSION 26u
/* ================================================================== plugin identity */
/* One value per plugin, not flags (QUANT is 3). RAD_PLUGIN_QUANT is a quantiser plugin:
@@ -135,7 +135,13 @@ typedef struct RadLayout {
* RAD_E_DTYPE the kernel does not read this encoding (an fp8 GEMM handed int4, a rotated
* weight to a kernel that does not rotate its activation). A declare-time
* error naming the weight, the encoding and the kernel.
* RAD_E_SHAPE the selected planes, or their extents, are not what the kernel reads. */
* RAD_E_SHAPE the selected planes, or their extents, are not what the kernel reads.
* RAD_LAYOUT_ANY the kernel reads the stored bytes without interpreting them (a prefetch
* into the cache), so it takes whatever form another reader chose and claims
* none: the builder does not count it when settling the stored form, and the
* runtime's layout check accepts it. A weight only such kernels read is
* stored as it is. */
#define RAD_LAYOUT_ANY 1
typedef int (*RadLayoutFn)(const RadParam* p, int n_p, int operand, const RadEncoding* enc,
const int* sel, const RadTensor* planes, int n_planes,
RadLayout* out);
@@ -498,6 +504,24 @@ typedef void (*RadPluginCloseFn)(void);
* it, which is always correct. Symbol name: `rad_kernel_concurrent`. */
typedef int (*RadKernelConcurrentFn)(int i);
/* Optional promise about kernel `i` of rad_kernel_at: nonzero means it can chain. Chaining replaces
* the packet processor's ordering between dependent launches (the AQL barrier bit) with a wait in
* the kernel. Where chaining is on the engine resolves the row with the parameter `chain` = 1, and
* the launch may then emit chained forms of its kernels, recognised by "r4dchain" in the kernel's
* name; only those get a record and lose the bit. A chained form, before its first read of an
* operand, one lane a block waits until the 64-bit word at the record's `ctr` reaches `target`,
* then the block barriers and acquires at agent scope; after its last write the block releases at
* agent scope, barriers, and one lane adds 1 to `ctr`. Every block, including one with nothing to
* do, adds exactly once. The record is RAD_CHAIN_REC_BYTES at byte RAD_CHAIN_KARG_OFF of the
* kernel's argument segment: { magic, target, ctr, check }, with check = magic ^ target ^ ctr ^
* RAD_CHAIN_CHECK. A record whose magic or check does not match means: run without waiting and do
* not add. Without the symbol, or with 0, the engine never asks. Symbol name: `rad_kernel_chain`. */
typedef int (*RadKernelChainFn)(int i);
#define RAD_CHAIN_KARG_OFF 992
#define RAD_CHAIN_REC_BYTES 32
#define RAD_CHAIN_MAGIC 0x3130434e48434452ull /* "RDCHCN01" */
#define RAD_CHAIN_CHECK 0x9e3779b97f4a7c15ull
/* Optional promise about kernel `i` of rad_kernel_at: nonzero means it is a collective. Its
* launches exchange data with the other ranks' cards, so the interconnect carries their bytes
* while they run and the step waits on it. The engine keeps its own bulk traffic on those links
+332 -9
View File
@@ -56,6 +56,30 @@ 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 */
/* RadBuildCtx::kv_quant. A layout groups each head's dims by 32 with an f16 scale per group, the
* group's absmax over the largest code. A token's row in a head, at head_dim HD (a multiple of 32):
*
* RAD_KVQ_V4 (fp8_v4), 25 * HD / 16 bytes:
* [0, HD) K, E4M3 as the fp8 cache stores it
* [HD, 3HD/2) V's int4 codes, dim d in the nibble at bit 4 * (d % 2) of byte d / 2
* [3HD/2, 25HD/16) V's scales (absmax / 7)
* RAD_KVQ_K6V4 (k6_v4), 22 * HD / 16 bytes:
* [0, HD/2) K's low four bits, as V's codes above
* [HD/2, 3HD/4) K's high two bits, a 4-byte word per 16 dims whose byte j holds dims 2j
* and 2j+1 in bits 0-3 and dims 8+2j and 9+2j in bits 4-7
* [3HD/4, 5HD/4) V's int4 codes
* [5HD/4, 21HD/16) K's scales (absmax / 31)
* [21HD/16, 22HD/16) V's scales (absmax / 7)
*
* Codes are two's complement (int4 -7..7, int6 -31..31). The group is declared at RAD_F8E4M3 with
* head_dim the row's bytes / 2 so the core sizes pages from it; only the layout's kernels read
* it. */
enum {
RAD_KVQ_NONE = 0,
RAD_KVQ_V4 = 1,
RAD_KVQ_K6V4 = 2
};
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) */
@@ -86,7 +110,7 @@ typedef struct RadBuildCtx {
* 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.
* head) descales. `kv_quant` below narrows the trunk's attention cache further.
*
* 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. */
@@ -140,8 +164,58 @@ typedef struct RadBuildCtx {
*
* Trailing and zero-defaulted, so a plugin built against the older struct is unchanged. */
int64_t max_mixed_tok;
/* A narrower layout of the trunk's attention cache under kv_dtype RAD_F8E4M3, from
* --kv-cache-dtype fp8_v4 or k6_v4: a RAD_KVQ_* (below). The engine sets it only for a plugin
* whose probe has the layout's bit in RadArchProbe::kv_quant_ok and refuses the flag
* otherwise, so no plugin sees it that would store plain E4M3 instead. A drafter's cache stays
* E4M3.
*
* Trailing and zero-defaulted, so a plugin built against the older struct is unchanged. */
int kv_quant;
/* ---- appended in ABI 26 (control vectors) */
/* A control vector loaded from a GGUF `controlvector` file found for this model, or null.
*
* The engine loads and validates it once, at bringup, and the plugin only reads it: the
* direction table lives in device memory on every rank, in the activations' own float form,
* and a per-request switch reaches the step through a derived buffer the plugin declares
* itself (a row mask, `projection_on_rows`). Nothing is reloaded to turn the feature on or
* off, and with the mask zero the model computes what it computes without a vector.
*
* The plugin decides where in its own graph the projection applies; for the gated-residual
* architectures that is after each block's write into the wide stream, which is the point
* llama.cpp's control-vector hook uses for `qwen4exp`. A plugin that does not declare an op
* reading this table ignores it.
*
* Trailing and zero-defaulted, so a plugin built against the older struct is unchanged. */
const struct RadControlVector* cvec;
} RadBuildCtx;
/* ================================================================== control vectors */
/* A direction per layer, in the shape llama.cpp's `--cvec-mode project` applies it:
*
* h <- h - s_l (h . v_l) v_l v_l = d_l / |d_l|, s_l = |d_l| (times the user scale)
*
* with `v_l` unit and `s_l` the norm the file stored (so a unit vector's scale is 1). The engine
* normalises the rows when it uploads them, so a kernel reads `dir` as the unit direction and
* takes `s` from `scales`; a zero scale means "this layer is not steered".
*
* `dir` is [n_layers, n_embd] f32, row `l` that layer's direction, row 0 unused (layer 0 has no
* direction in the format). Each rank has its own copy: the pointer a plugin receives is that
* rank's, and the residual stream is replicated, so no collective is involved.
*
* `n_layers` is the model's layer count, which may be longer than the file's (the published
* Flash-Next vector carries 47 of 48 rows). The steered range is not a separate field: a layer is
* steered exactly when its `scales` entry is nonzero, so a plugin asks that and cannot disagree
* with the loader. */
typedef struct RadControlVector {
const float* dir; /* device, [n_layers, n_embd] f32, unit rows */
const float* scales; /* host, [n_layers] f32, |d_l| * user scale; 0 = not steered */
int64_t n_embd;
int64_t n_layers;
} RadControlVector;
/* ================================================================== 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). */
@@ -377,6 +451,16 @@ int rad_weight_shard_span_parts(RadBuilder* b, rad_weight w, const int64_t* lo,
* at issue. Additive export. */
int rad_kv_group_zero(RadBuilder* b, rad_kvgroup g);
/* Every rank holds the same bytes for this paged group, so a tier stores it once. Under tensor
* parallelism a host slot for a cached entry is otherwise every rank's shard end to end; a group
* declared replicated takes one window at the front of the slot, written by rank 0 and read by
* every rank on a restore. The plugin asserts the replication (an MLA latent cache that every
* rank computes whole, an indexer's key cache every rank pools into); the engine moves bytes and
* cannot know. RADIANCE_DEBUG_TIER_VERIFY checks the claim: each follower hashes what it would
* have written and the leader compares. Paged groups only, and never a group this rank declared
* zero. No effect at one rank. Additive export; RAD_ABI_VERSION does not move. */
int rad_kv_group_replicated(RadBuilder* b, rad_kvgroup g);
#define RAD_BUFS(...) ((const rad_buf[]){ __VA_ARGS__ }), \
(int)(sizeof((const rad_buf[]){ __VA_ARGS__ }) / sizeof(rad_buf))
@@ -396,7 +480,7 @@ int rad_op_declared(RadBuilder* b, const char* op);
* 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 };
RAD_MAP_TRANSPOSE = 4, RAD_MAP_EMA = 5, RAD_MAP_TAP = 6, RAD_MAP_COLS = 7 };
typedef struct RadNameMap {
const char* declared; /* the name given to rad_decl_weight */
@@ -412,7 +496,8 @@ typedef struct RadNameMap {
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. */
* higher, rows along dim 0) tensor src[i]; the pieces join along dim 0. RAD_MAP_COLS: columns
* [src_index[0], src_index[0] + src_rows[0]) of src[0]'s last dimension. */
int64_t src_rows[8];
} RadNameMap;
@@ -430,7 +515,13 @@ typedef struct RadNameMap {
*
* 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.
* fraction rather than the whole table. Successive ROWS maps for one name extend it in call order,
* as CONCAT maps do, so a weight may take more than eight pieces (an output projection whose rows
* are permuted, one piece a row).
*
* RAD_MAP_COLS keeps one column range of src[0] (see src_rows): a linear whose input carries
* channels the runtime never feeds (Cosmos's padding-mask channel, always zero) is read over the
* columns it is fed.
*
* 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).
@@ -440,7 +531,13 @@ typedef struct RadNameMap {
* 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. */
* RAD_MAP_TAP keeps one index of one dimension of src[0], dropping that dimension: index
* src_index[0] (negative counts from the end) of dimension `concat_dim`. A causal 3-D convolution
* run on a single frame reads only its last temporal tap, so [cout, cin, kt, kh, kw] read at tap -1
* of dimension 2 is the 2-D convolution it then is.
*
* TRANSPOSE, EMA, TAP and COLS are computed at convert in f32, like WNORM, and stored as the recipe
* says. */
int rad_decl_name_map(RadBuilder* b, const RadNameMap* m);
@@ -556,6 +653,15 @@ int rad_declare_drafter(RadBuilder* b, const RadDrafterDecl* d);
* checkpoint was trained with is the plugin's to know. */
int rad_drafter_shared_kv(RadBuilder* b, int pos_offset);
/* Whether this engine's launches chain (RadKernelChainFn, abi/rad_abi.h): the core's decision, on
* the AQL backend only, which keeps a completion counter per queue -- by default where the model's
* plugin measured a gain with its speculative drafter (RadArchProbe::chain_pays) and that drafter
* decodes speculatively, or as RADIANCE_CHAIN says. A plugin that places work by whether a kernel
* waits in its prologue behind the one before it -- a weight read issued on lane 0 right after its
* GEMM -- asks this instead of reading the environment, so it follows that decision. Additive
* export; RAD_ABI_VERSION does not move. */
int rad_chain_on(void);
/* ================================================================== 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
@@ -621,8 +727,21 @@ enum { RAD_GEN_PASS_COND = 1, RAD_GEN_PASS_DENOISE = 2, RAD_GEN_PASS_DECODE = 3
* 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 };
* each other both ways, every row to every row before it.
*
* JOINT_STEP the conditioning is not cached as keys and values but rides in every denoising
* step beside the image, since the model updates it as it goes. The conditioning
* is the prompt's rows after `drop_prefix`, then `pad_token` rows up to a multiple
* of `pad_multiple`, then each reference picture's latent tokens in raster order
* (the pictures are not in the prompt; `tmpl_image` is unused). No KV group is
* declared: the COND pass leaves the conditioning's rows in the plugin's own state,
* once for every lane the request's images run in, and each DENOISE entry is that
* conditioning followed by the image (RadGenBatch, "the joint layout"). Rotary
* positions are four f32 components (t, h, w, l): a text or padding row is (0, 0,
* 0, l), l its index with `rope_text_l` and 0 without; a picture's token at row h,
* column w (from 0) is (0, s h, s w, l) with s `rope_hw_scale`, l -(1 + k) for
* the k-th reference picture and 0 for the image being made. */
enum { RAD_GEN_LAYOUT_JOINT_GRID = 1, RAD_GEN_LAYOUT_JOINT_STEP = 2 };
typedef struct RadGeneratorDecl {
const char* name; /* "qwen-image" -- for messages */
@@ -662,7 +781,8 @@ typedef struct RadGeneratorDecl {
/* 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:
* step count. Otherwise `default_steps` sigmas from 1 to 1/steps (or off `sigma_grid`'s
* table), 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 + ...)
@@ -684,10 +804,122 @@ typedef struct RadGeneratorDecl {
* pixels of the image a decode pass made. */
int64_t max_lanes;
rad_buf image_out;
/* ---- the JOINT_STEP layout (zero for JOINT_GRID) */
/* The most rows one conditioning lays out (text, padding and reference latents together):
* the plugin keeps `cond_rows` rows a lane a branch, and a longer prompt is refused. */
int64_t cond_rows;
double rope_hw_scale;
int32_t rope_text_l;
int32_t pad_multiple;
int32_t pad_token;
/* Guidance as the model's card states its scale s: 1 when s means cond + s (cond - uncond),
* which the core runs as s + 1, 0 when it means uncond + s (cond - uncond). `default_cfg` is
* in the card's terms. */
int32_t cfg_original;
/* The negative prompt guidance uses when a request names none ("" is a prompt); null when a
* guided request must name its own. */
const char* default_negative;
/* 1: a reference picture is shrunk to `ref_area` only when larger and cut to `size_multiple`
* by a centred crop; 0: resized to `ref_area` at its own aspect. */
int32_t ref_shrink_only;
/* ---- appended in ABI 25 (Anima, SDXL) */
/* `aux_tokens` nonzero: the conditioning also reads the user's prompt through the container's
* second tokenizer (rad-convert --tokenizer2), as that tokenizer encodes it alone, special
* tokens included, and the COND pass carries the ids (RadGenBatch::aux_ids). A prompt whose
* second tokenisation is longer than `aux_max_tokens` is refused. */
int64_t aux_max_tokens;
/* The unshifted sigmas of a schedule that is not fixed: 0 is diffusers' linspace(1, 1 / steps,
* steps); N > 0 picks them off a table of N timesteps as ComfyUI's `simple` scheduler does,
* step i at (N - floor(i N / steps)) / N, each an f32 quotient of the table's own. */
int32_t sigma_grid;
/* A prompt shorter than this many tokens after `drop_prefix` is padded with `pad_token` up to
* it, the padding read as prompt (a reference tokenizer's minimum length: an empty prompt is
* one padding token, and stays empty rather than becoming " "). 0: no minimum. */
int32_t min_tokens;
int32_t aux_tokens;
/* ---- a discrete-time denoiser (train_steps 0: the flow-matching sampler above)
* A model trained on `train_steps` timesteps of a variance-preserving schedule, betas from
* `beta_start` to `beta_end` (RAD_GEN_BETAS_*), sampled as k-diffusion samples it (Stable
* Diffusion's family). Its schedule is in sigmas, sigma_t = sqrt((1 - abar_t) / abar_t) for
* the cumulative product abar of 1 - beta; `zero_snr` rescales it to a zero terminal SNR
* (the last abar 4.8973451890853435e-08, as ComfyUI does). A request names a sampler
* (RAD_GEN_SAMPLER_*, one of those `samplers` holds as 1 << id, which the plugin's step
* serves) and a scheduler (RAD_GEN_SCHED_*), which places the request's steps between the
* table's largest and smallest sigma, 0 last. A DENOISE pass carries each entry's sigma,
* next and previous sigma in these units, the timestep the denoiser is conditioned on, the
* sampler and the step (RadGenBatch). n_sigmas, shift and the dynamic shift are unused. */
double beta_start, beta_end;
int32_t train_steps;
int32_t beta_schedule;
int32_t zero_snr;
uint32_t samplers;
int32_t default_sampler;
int32_t default_scheduler;
/* ---- the prompt in windows (a CLIP text encoder's fixed context)
* Nonzero: the prompt's tokens after `drop_prefix` are cut into runs of at most
* `chunk_tokens`, each laid out as `chunk_bos`, the run, `chunk_eos`, then `pad_token` up to
* chunk_tokens + 2 rows, the windows end to end; a prompt with no tokens is one window of
* the two markers and padding. The JOINT_STEP layout only, with `pad_multiple` 0, and
* `cond_rows` a whole number of windows. */
int32_t chunk_tokens;
int32_t chunk_bos, chunk_eos;
} RadGeneratorDecl;
/* RadGeneratorDecl::beta_schedule: betas linear in sqrt(beta) (Stable Diffusion's
* "scaled_linear"), or linear in beta. */
enum { RAD_GEN_BETAS_SCALED_LINEAR = 0, RAD_GEN_BETAS_LINEAR = 1 };
/* A discrete-time model's samplers, as k-diffusion (and ComfyUI) name and define them. */
enum {
RAD_GEN_SAMPLER_EULER = 0, /* "euler" */
RAD_GEN_SAMPLER_EULER_ANCESTRAL = 1, /* "euler_ancestral", eta 1 */
RAD_GEN_SAMPLER_DPMPP_2M = 2, /* "dpmpp_2m" */
RAD_GEN_SAMPLER_COUNT = 3
};
/* ... and its schedulers, ComfyUI's: "normal" (timesteps evenly spaced, the last at the smallest
* sigma), "karras" (rho 7), "exponential", "sgm_uniform" (normal without its last point) and
* "simple" (the table read at even strides). */
enum {
RAD_GEN_SCHED_NORMAL = 0,
RAD_GEN_SCHED_KARRAS = 1,
RAD_GEN_SCHED_EXPONENTIAL = 2,
RAD_GEN_SCHED_SGM_UNIFORM = 3,
RAD_GEN_SCHED_SIMPLE = 4,
RAD_GEN_SCHED_COUNT = 5
};
int rad_declare_generator(RadBuilder* b, const RadGeneratorDecl* d);
/* ================================================================== adapter key names */
/* How LoRA files written for this model name its linears, for an image or video generator served
* with runtime adapters (--lora, --lora-dir, --max-loras; core/lora/). Adapter files from training
* tools and UIs name a module by the model's original code, not by the checkpoint the container
* was converted from, and wrap it in a prefix of their own: `diffusion_model.blocks.3.attn.wq`,
* kohya's `lora_unet_blocks_3_attn_wq`, a bare `blocks.3.attn.wq`. Both calls are optional,
* made before the GEMMs they cover are declared, and only a program that declares a root carries
* adapters at all. Additive exports; RAD_ABI_VERSION does not move.
*
* rad_lora_root declares one network the adapters may reach (the denoiser, a text encoder): every
* LoRA-capable GEMM over a checkpoint tensor whose name starts with `native` gets an adapter path.
* `spellings` lists, space separated, the prefixes adapter keys of this network start with in
* place of `native`; one ending in '_' is kohya's form, in which the rest of the key is the module
* path with every '.' written '_'. `native` itself is always accepted, and "-" stands for a key
* with no prefix at all. A key is tried against the longest matching spelling first.
*
* rad_lora_alias maps an adapter module that the checkpoint names differently. `from` and `to`
* are module paths relative to the root's prefixes (no ".weight"), '#' standing for a decimal
* index that `to` repeats in the same order. Rows [from_lo, from_hi) of the adapter module's
* output (its lora_B rows; from_hi 0 = all of them) land at rows from `to_lo` of the checkpoint
* module `to`. Several aliases with one `from` split a fused adapter module over separate
* checkpoint tensors (a `qkv` over `to_q`, `to_k`, `to_v`); several `from` into one `to` at
* different `to_lo` place separate adapter modules into one fused tensor. A module path that is
* the checkpoint's own needs no alias. */
int rad_lora_root(RadBuilder* b, const char* native, const char* spellings);
int rad_lora_alias(RadBuilder* b, const char* native, const char* from, const char* to,
int64_t from_lo, int64_t from_hi, int64_t to_lo);
/* ================================================================== 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
@@ -1080,6 +1312,76 @@ typedef struct RadVideoGenDecl {
int64_t dbg_vd_n_bytes;
rad_buf dbg_vd_pix; /* [1797, 3072] bf16: proj_out pixels of the first tile */
int64_t dbg_vd_pix_bytes;
/* ---- appended in ABI 24 (LTX-2.5) */
/* The sampler's form. 0: the grid points above. 1: diffusers' FlowMatchEulerDiscrete as
* LTX-2 runs it -- a request's `steps` are its evaluations n, between 1 and max_steps; sigmas
* linspace(1, 1/n, n), computed in f64 and taken to f32, and with `dyn_shift` 1 shifted by mu
* as exp(mu) / (exp(mu) + 1/s - 1) in f32, mu linear in the video's latent tokens from
* `base_seq` (`base_shift`) to `max_seq` (`max_shift`); then, with `shift_terminal` > 0,
* stretched so the last is shift_terminal; and a closing 0. A trained schedule (n_rungs > 0)
* is the rungs taken to f32 and a closing 0, with default_steps and max_steps both n_rungs.
* Both modalities step the same sigmas; shift_video and shift_audio are unused. */
int32_t sched_kind;
int32_t dyn_shift;
double base_shift, max_shift;
int64_t base_seq, max_seq;
double shift_terminal;
/* With latent upscaling under a trained schedule of form 1: the sigmas the evaluations after
* the upscale step through (a second stage's own schedule, its first the sigma the upscaled
* latents are re-noised to), instead of the schedule's tail. The small size then runs every
* rung, its last to the clean estimate, and a request's refinement is n_refine_rungs. */
int32_t n_refine_rungs;
const double* refine_rungs;
/* The text rows of every VSTEP pass, whatever the prompt's length (0: the prompt's tokens):
* a model whose text encoder writes a fixed number of rows. */
int64_t step_text_rows;
/* Guidance terms (RadVideoGenBatch::guidance_h) when `guidance` is 1: the defaults a request
* that names none gets, the video's scale, spatio-temporal scale, modality scale and rescale,
* then the soundtrack's four. A request whose video or soundtrack scale is not 1 is guided
* against a negative prompt: its own, else `default_negative` (null: ""). The VCOND pass then
* carries both prompts, the positive first (two cu_seqlens entries). */
int32_t guidance;
double guidance_default[8];
const char* default_negative;
/* Image to video: 1 when the model can start from a first frame (OpenAI's
* `input_reference`), handed to the VCOND pass resized to the size the first evaluations run
* at (RadVideoGenBatch `image`), after an H.264 round trip at `image_crf` when that is > 0
* (the compression the model was trained against). */
int32_t image_cond;
int32_t image_crf;
/* A left-padded text encoder: the VCOND pass's positions count so that each prompt ends at
* `text_left_pad` - 1, the place it holds in a sequence padded on the left to that length (0:
* from 0). */
int64_t text_left_pad;
/* The soundtrack's latent rows a step carries for each latent (0: audio_channels, a stream of
* rows a channel); a model whose latent holds every channel at once carries 1. */
int32_t audio_streams;
/* A latent upscaler of one fixed factor, upscale_max: a request's sides must then be
* multiples of size_multiple x upscale_max, so the small size is exactly the full size over
* the factor. */
int32_t upscale_exact;
/* A length head: a model that predicts from the prompt how long its video should be. With
* `duration_out` set, every VCOND pass writes to it (f32 [1], device) the natural log of that
* length in seconds. For a request that names no length the core takes its exponent, clamps it
* to [duration_min, duration_max] seconds, rounds it to frames at the frame rate and floors
* that to the chunk grid (frames_per_chunk n + frames_tail; one chunk more when the floor falls
* under duration_min and duration_max allows it), then holds it to min_chunks..max_chunks and
* to what a pass carries. Until that VCOND pass is done such a request is shaped at
* `default_chunks`, so the pass may not depend on the length: its geometry carries the
* provisional frame count, and `t_video` / `t_audio` the provisional schedule. 0: a request
* that names no length is made at default_chunks. `duration_dtype` is the precision the model
* takes the exponent in: RAD_F32, or RAD_BF16 for a head run in bf16, whose length is
* exp(x) in f32 rounded to bf16. Frames round half to even, as Python's round(). */
rad_buf duration_out;
double duration_min, duration_max;
uint32_t duration_dtype;
/* Decoders: a model with more than one way to turn its latents into frames names them,
* space-separated, its default first (LTX-2.5: "vae diffusion"). A request may name one; every
* VDECODE pass carries its index (RadVideoGenBatch::decoder). `decoder_max_area`, when set,
* holds each decoder's largest frame in pixels (0: max_area), and a request over it is refused.
* Null: one decoder. */
const char* decoders;
const int64_t* decoder_max_area;
} RadVideoGenDecl;
int rad_declare_video_generator(RadBuilder* b, const RadVideoGenDecl* d);
@@ -1104,6 +1406,14 @@ const char* rad_arch_id(void);
* served at: (unquantised)". */
const char* rad_arch_quant(void);
/* Whether this plugin may serve a checkpoint whose quantisation was imported weight by weight
* (Quark, ParoQuant, GPTQ, NVFP4, ...). Such a descriptor names the producing tool, which no plugin
* claims, so the family's one quantised plugin serves it, and two are refused (startup.h). A
* family's second quantised plugin that reads only its own containers returns 0 and leaves imports
* to the first. Optional: absent or non-zero is yes. Additive export; RAD_ABI_VERSION does not
* move. */
int rad_arch_serves_imports(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.
*
@@ -1163,6 +1473,16 @@ typedef struct RadArchProbe {
* `gen_rows` (its largest pass: a step's packed sequence at the largest size and length). It
* keeps no KV. */
int32_t gen_video;
/* The RadBuildCtx::kv_quant layouts the plugin serves for this model, bit 1 << RAD_KVQ_*:
* libr4d has their rows for its attention geometry. A layout whose bit is clear is refused at
* startup with the plugin named. */
int32_t kv_quant_ok;
/* Nonzero when this plugin measured its speculative step faster with chained launches
* (RadKernelChainFn: a dependent kernel waits in its own prologue rather than behind the
* queue's barrier bit). With a drafter decoding speculatively on the AQL backend, the core
* then takes them by default; RADIANCE_CHAIN overrides either way. Zero keeps them off: plain
* decode measured slower chained, and a plugin that never measured them asks for nothing. */
int32_t chain_pays;
} RadArchProbe;
int rad_arch_probe(const RadModelMeta* meta, RadArchProbe* out);
@@ -1244,10 +1564,13 @@ typedef struct RadChatFormat {
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 */
/* "" when nothing of its own ends the name: then the object's `{` does, or, for tagged
* arguments in an unwrapped call, the first arg_prefix or call_end, which begin the body */
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 */
/* "" when arg_value_prefix alone ends the name; both set are written back to back */
const char* arg_name_suffix;
const char* arg_value_prefix;
const char* arg_value_suffix;
+15
View File
@@ -56,9 +56,16 @@ typedef struct RadFileHeader {
uint64_t prof_off, prof_count; /* RadFileProfile[] -- expert popularity */
uint64_t data_off, data_bytes; /* the blob; RAD_ALIGN_POOL-aligned */
/* reserved[RAD_HDR_AUX_VOCAB_OFF] and reserved[RAD_HDR_AUX_VOCAB_BYTES] place a second vocab
* section, laid out as the first (RadVocabHeader + its payload, its strings in the one string
* blob): a tokenizer a generator reads its prompt through beside the model's own
* (RadGeneratorDecl::aux_tokens). Both zero when there is none, as in a file written before
* the section existed; the rest are zero. */
uint64_t reserved[8];
} RadFileHeader;
enum { RAD_HDR_AUX_VOCAB_OFF = 0, RAD_HDR_AUX_VOCAB_BYTES = 1 };
/* ------------------------------------------------------------------ metadata */
typedef struct RadFileKV {
rad_stroff key;
@@ -124,9 +131,17 @@ enum { RAD_TOK_BPE = 1, RAD_TOK_UNIGRAM = 2, RAD_TOK_WORDPIECE = 3, RAD_TOK_RWKV
enum {
RAD_NORM_NFC = 1, RAD_NORM_NFKC = 2, RAD_NORM_NFD = 3, RAD_NORM_NFKD = 4,
RAD_NORM_LOWERCASE = 5, RAD_NORM_STRIP = 6, RAD_NORM_REPLACE = 7, RAD_NORM_PREPEND = 8,
RAD_NORM_PRECOMPILED = 9,
RAD_PRE_BYTELEVEL = 32, RAD_PRE_SPLIT = 33, RAD_PRE_METASPACE = 34,
RAD_PRE_WHITESPACE = 35, RAD_PRE_PUNCTUATION = 36, RAD_PRE_DIGITS = 37,
RAD_PRE_BYTE_FALLBACK = 38,
/* BPE's end-of-word suffix (arg0, CLIP's "</w>"): a word's last symbol carries it, so the
* merges and the vocabulary tell a word's end from its middle. */
RAD_PRE_END_OF_WORD = 39,
/* An added token matched in the normalised text rather than the raw one (tokenizers'
* `normalized`): arg0 its spelling, iarg its id. Cut out after the normalisers and before
* the other pre-tokenisers. */
RAD_PRE_ADDED_NORMALIZED = 40,
RAD_DEC_BYTELEVEL = 64, RAD_DEC_METASPACE = 65, RAD_DEC_REPLACE = 66,
RAD_DEC_STRIP = 67, RAD_DEC_FUSE = 68, RAD_DEC_BYTE_FALLBACK = 69
};
+95 -2
View File
@@ -315,8 +315,79 @@ typedef struct RadGenBatch {
int32_t dec_lane;
int32_t dec_height, dec_width; /* pixels */
int32_t dec_preview; /* the lane's preview latent, not its state */
/* ---- the joint layout (RAD_GEN_LAYOUT_JOINT_STEP)
* COND: `branch` is 0 for the prompt and 1 for the negative prompt, and `n_lane` / `lane_h` /
* `lane` name every lane the conditioning's images run in; the plugin keeps the
* conditioning's row r for lane l at its own row (2 l + branch) cond_rows + r. The prompt's
* last `n_pad` tokens are the padding, and `valid_cu` is {0, the tokens before it}. `rope_f`
* is the conditioning's positions, [4, rows] f32 component-major.
* DENOISE: each entry's rows are its conditioning and then its image's latent tokens, so
* `cu_seqlens` bounds both together. `src` [n_tok] names a row's source: >= 0 the plugin's
* conditioning row (as above), < 0 the image row -(1 + k) of the compact image order, in
* which `img_cu` [n_seq + 1] bounds each entry's image and `img_rows` [n_img] is each image
* row's place in the pass. `mode_cu` [3 n_seq + 1] cuts each entry into its text (padding
* included), its reference pictures and its image: entry e's text is rows [mode_cu[3e],
* mode_cu[3e + 1]), its pictures [mode_cu[3e + 1], mode_cu[3e + 2]), its image up to
* mode_cu[3e + 3]. `rope_f` is [4, n_tok]. Everything sigma, cfg, pair, lane, flags and seed
* say is per entry as for JOINT_GRID, and the lane ops read `img_cu`. */
int32_t branch;
int32_t n_pad;
const int32_t* valid_cu;
int64_t n_img;
const int32_t* img_cu;
const int32_t* img_rows;
const int32_t* mode_cu;
const float* rope_f;
/* DENOISE: the rows again in mode-major order -- every entry's text, then every entry's
* pictures, then every entry's image (the compact image order) -- for a model whose early
* blocks run each mode on its own. `mode_rows` [n_tok] is each one's place in the pass,
* `mode_src` [n_tok] its source as `src` names it, and `mode_rope_f` holds the positions mode
* by mode, mode m's a dense [4, n_m] block at 4 times its start;
* `n_mode_h` [3] counts each mode's rows, so mode m starts at the sum of the counts before
* it, and `mode_ccu` [3, n_seq + 1] bounds each entry's rows of each mode from that start
* (mode m's cu_seqlens at mode_ccu + m (n_seq + 1), each beginning at 0). */
const int32_t* mode_rows;
const int32_t* mode_src;
const int32_t* mode_ccu;
const int64_t* n_mode_h;
const float* mode_rope_f;
/* ---- appended in ABI 25 (Anima, SDXL) */
/* COND, for a generator that reads its prompt through a second tokenizer
* (RadGeneratorDecl::aux_tokens): the user's prompt as that tokenizer encodes it, `n_aux` ids
* on the device, and `aux_cu` {0, n_aux} on the device. 0 and null otherwise. */
int64_t n_aux;
const int32_t* aux_ids;
const int32_t* aux_cu;
/* ---- a discrete-time denoiser (RadGeneratorDecl::train_steps), DENOISE, one value per
* entry: the schedule's sigma before `sigma` (0 on an image's first step), `sigma` on the
* model's trained clock (the nearest of its timesteps, as ComfyUI conditions it), the
* request's sampler (RAD_GEN_SAMPLER_*) and the image's step, from 0. */
const float* sigma_prev;
const float* timestep;
const int32_t* sampler;
const int32_t* step;
/* DENOISE: [n_seq, 2] each entry's image height and width in pixels, f32 (a model
* conditioned on its size); `entry_h` [n_seq, 4] the same entries on the host: the
* conditioning's rows (JOINT_STEP), the height and width in pixels, and the branch (0 the
* prompt, 1 the negative prompt), for a plugin whose ops take an image's geometry from their
* operands and so issue each entry on its own. */
const float* size_px;
const int32_t* entry_h;
/* COND: the pass's token ids on the host ([n_tok]), for a plugin whose issue depends on
* where a window's end-of-text marker sits. */
const int32_t* tokens_h;
/* ---- appended in ABI 26 (Krea 2's rebalance) */
/* COND: the request's RAD_GEN_COND_* options, 0 for none. */
int32_t cond_opts;
} RadGenBatch;
/* A request's conditioning options on a COND pass (RadGenBatch::cond_opts). */
enum {
RAD_GEN_COND_REBALANCE = 1 /* Krea 2: the text taps at the plugin's fixed rebalance gains */
};
/* An entry's flags on a DENOISE pass. */
enum {
RAD_GEN_F_FIRST = 1, /* the image's first step: its latent is the noise its seed draws */
@@ -461,7 +532,8 @@ typedef struct RadVideoGenBatch {
/* f32 [6]: the video's timestep, sigma and next sigma, then the soundtrack's. */
const float* sched;
const float* pos; /* f32 [3, n_tok]: RAD_VPOS_STEP */
/* u32 [2, 2]: the video's noise seed and the soundtrack's, low word first. */
/* u32 [2, 2]: the video's noise seed and the soundtrack's, low word first (VSTEP, VUPSCALE and
* VDECODE passes; a decoder that denoises draws from it). */
const uint32_t* seed;
/* ---- VDECODE */
@@ -530,6 +602,23 @@ typedef struct RadVideoGenBatch {
/* ---- The sampler that advances the latents: RAD_SAMPLER_* above. Trailing since ABI 24. */
int32_t sampler;
/* ---- appended in ABI 24 */
/* VCOND with a negative prompt: `n_neg` of the pass's tokens, after the prompt's. */
int32_t n_neg;
/* The request's guidance, f32 [8] host: the video's scale, its spatio-temporal scale, its
* modality scale and its rescale, then the soundtrack's four; a model's own defaults where
* the request named none. */
const float* guidance_h;
/* VCOND with a first frame (RadVideoGenDecl::image_cond): `image` [height, width, 3] bf16 in
* [-1, 1], device; null without one. Every later pass carries `has_image` 1 for such a
* request. */
const uint16_t* image;
int32_t has_image;
/* guidance_h on the device, f32 [8], for ops that read the terms. */
const float* guidance;
/* VDECODE: the decoder the request named (RadVideoGenDecl::decoders), its index; 0 the
* default. */
int32_t decoder;
} RadVideoGenBatch;
/* RadVideoGenBatch::enc_kind: which reference media an encode pass carries. A plugin's latent
@@ -556,7 +645,11 @@ enum {
* first three are also the source volume; (frames, H, W) the target volume; then both in
* patches, (frames, h / patch, w / patch) and (frames, H / patch, W / patch). */
RAD_VGEOM_UP_SRC = 16, RAD_VGEOM_UP_DST = 21, RAD_VGEOM_UP_PSRC = 24, RAD_VGEOM_UP_PDST = 27,
RAD_VGEOM_N = 32
/* The soundtrack's latents as a volume of single latents, (n_audio, 1, 1); and the video's
* rows cut at its first latent frame, {0, that frame's patches, every patch} (a first frame
* a model conditions on at its own timestep). */
RAD_VGEOM_AUDIO_VOL = 32, RAD_VGEOM_FIRST_CU = 35,
RAD_VGEOM_N = 40
};
/* ================================================================== operands */