Archived
radiance 1.8.0
- video: MiniMax-H3 and FastVideo's FastH3 8-Step V2, a video with its stereo soundtrack from text on OpenAI's /v1/videos, block fp8 on two cards - music: MiniMax-Music3, songs from lyrics and a description on /v1/audio/speech - speech: Breeze-TTS-2 on /v1/audio/speech and vLLM-Omni's realtime WebSocket - runtime LoRA adapters beside the base model, picked per request by `model` (baKon, #105) - several agent streams keep their cached history: --checkpoint-slots auto, the new default, sizes the snapshot store by --max-num-seqs (#108) - the Swift 1.5 27B W4 releases convert and serve: compressed-tensors AWQ and GPTQ int4, AutoRound, ModelOpt NVFP4 (#110) - the image's host tests pass where perf_event_open is refused, as in BuildKit's sandbox (#106) - plugin ABI 21: out-of-tree plugins must be rebuilt
This commit is contained in:
1 parent
91127395ee
commit
7a7707a955
179 files changed
+37542
-1892
No files matched your search
+1
-1
@@ -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 18u
|
||||
#define RAD_ABI_VERSION 21u
|
||||
|
||||
/* ================================================================== plugin identity */
|
||||
/* One value per plugin, not flags (QUANT is 3). RAD_PLUGIN_QUANT is a quantiser plugin:
|
||||
|
||||
+269
-7
@@ -395,7 +395,8 @@ int rad_op_declared(RadBuilder* b, const char* op);
|
||||
/* 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 };
|
||||
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 */
|
||||
@@ -409,12 +410,37 @@ typedef struct RadNameMap {
|
||||
* 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. */
|
||||
* 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);
|
||||
|
||||
@@ -580,7 +606,7 @@ int rad_declare_encoder(RadBuilder* b, const RadEncoderDecl* d);
|
||||
* 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, logits or pooled vectors.
|
||||
* 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
|
||||
@@ -662,6 +688,202 @@ typedef struct 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.
|
||||
*
|
||||
* 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 };
|
||||
|
||||
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;
|
||||
} 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, ...);
|
||||
@@ -721,15 +943,26 @@ typedef struct RadArchProbe {
|
||||
* the engine sizes the audio pass with it (RadBuildCtx::max_audio_frames). 0 is no audio. */
|
||||
int64_t audio_frames;
|
||||
|
||||
/* Nonzero for an image generator (rad_declare_generator): the rows its largest pass carries.
|
||||
* The engine sizes the step for it before declare (a denoise pass carries all of an image's
|
||||
* latent rows at once, twice with guidance) and turns off what a generator does not use. 0 is
|
||||
* a program that generates text or embeds. */
|
||||
/* 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);
|
||||
@@ -900,6 +1133,35 @@ typedef struct 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);
|
||||
|
||||
int rad_arch_declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* ctx);
|
||||
/* Forward declaration; the signature is in rad_runtime.h. */
|
||||
struct RadCtx;
|
||||
|
||||
@@ -46,4 +46,30 @@ RAD_PHILOX_HD float rad_philox_normal(uint64_t seed, uint64_t e) {
|
||||
return (j & 1) ? r * sinf(t) : r * cosf(t);
|
||||
}
|
||||
|
||||
/* Element `e` of the standard-normal stream (seed, stream): the counter's upper half is `stream`,
|
||||
* so stream 0 is rad_philox_normal and each other value an independent stream of the same seed
|
||||
* (an audio window's noise is stream 1 + its index). */
|
||||
RAD_PHILOX_HD float rad_philox_normal_s(uint64_t seed, uint64_t stream, uint64_t e) {
|
||||
const uint64_t g = e >> 2;
|
||||
uint32_t c[4] = { (uint32_t)g, (uint32_t)(g >> 32), (uint32_t)stream, (uint32_t)(stream >> 32) };
|
||||
rad_philox4x32_10(c, (uint32_t)seed, (uint32_t)(seed >> 32));
|
||||
const int j = (int)(e & 3u);
|
||||
const uint32_t wa = c[(j >> 1) * 2], wb = c[(j >> 1) * 2 + 1];
|
||||
const float ua = ((float)(wa >> 8) + 0.5f) * (1.0f / 16777216.0f);
|
||||
const float ub = ((float)(wb >> 8) + 0.5f) * (1.0f / 16777216.0f);
|
||||
const float r = sqrtf(-2.0f * logf(ua));
|
||||
const float t = 6.28318530717958647692f * ub;
|
||||
return (j & 1) ? r * sinf(t) : r * cosf(t);
|
||||
}
|
||||
|
||||
/* Element `e` of the uniform stream (seed, stream), in (0, 1): word e % 4 of the counter
|
||||
* (e / 4, stream) made uniform as the normal stream's words are. A sampler draws its pick from
|
||||
* it. */
|
||||
RAD_PHILOX_HD float rad_philox_uniform_s(uint64_t seed, uint64_t stream, uint64_t e) {
|
||||
const uint64_t g = e >> 2;
|
||||
uint32_t c[4] = { (uint32_t)g, (uint32_t)(g >> 32), (uint32_t)stream, (uint32_t)(stream >> 32) };
|
||||
rad_philox4x32_10(c, (uint32_t)seed, (uint32_t)(seed >> 32));
|
||||
return ((float)(c[e & 3u] >> 8) + 0.5f) * (1.0f / 16777216.0f);
|
||||
}
|
||||
|
||||
#endif /* RAD_PHILOX_H */
|
||||
+166
-4
@@ -55,10 +55,11 @@ typedef struct RadBatch {
|
||||
* row of a sequence carries its request's slot, fixed when the request was admitted, so a step
|
||||
* that mixes adapters and the base needs nothing else.
|
||||
*
|
||||
* Null when the engine serves no adapters (no --lora and no --max-loras). The core issues the
|
||||
* adapter ops itself and reads it there; a block with an adapter epilogue of its own reads it
|
||||
* too. Like every array above it sits at one address per rank for the engine's life and is
|
||||
* rewritten every step, so a recorded pass replays it. */
|
||||
* Null on a step with no adapter row, so always when the engine serves no adapters (no --lora
|
||||
* and no --max-loras). The core issues the adapter ops itself, only when this is set; a block
|
||||
* with an adapter epilogue of its own reads it too. Like every array above it sits at one
|
||||
* address per rank for the engine's life and is rewritten every step, so a recorded pass
|
||||
* replays it. */
|
||||
const int32_t* lora_slot;
|
||||
|
||||
/* The tokens the sampler wants logits for: indices into this step's tokens, [n_out].
|
||||
@@ -230,6 +231,18 @@ typedef struct RadBatch {
|
||||
* every step of a program that declared no generator. */
|
||||
int32_t gen;
|
||||
const struct RadGenBatch* gen_b;
|
||||
|
||||
/* ================================================ audio generation */
|
||||
|
||||
/* An audio generator's pass (rad_declare_audio_generator; `gen` is one of its
|
||||
* RAD_GEN_PASS_*): its payload. Null on every other step. */
|
||||
const struct RadAudioGenBatch* agen_b;
|
||||
|
||||
/* ================================================ video generation */
|
||||
|
||||
/* A video generator's pass (rad_declare_video_generator; `gen` is one of its
|
||||
* RAD_GEN_PASS_*): its payload. Null on every other step. */
|
||||
const struct RadVideoGenBatch* vgen_b;
|
||||
} RadBatch;
|
||||
|
||||
/* What a generator pass carries beside the step batch's own fields, which keep their meanings:
|
||||
@@ -312,6 +325,155 @@ enum {
|
||||
RAD_GEN_F_PREVIEW = 4 /* write the step's estimate of the clean latent to the lane's preview */
|
||||
};
|
||||
|
||||
/* What an audio generator's pass carries beside the step batch's own fields (rad_builder.h,
|
||||
* RadAudioGenDecl, describes the passes):
|
||||
*
|
||||
* PROMPT an ordinary causal prefill: `token_ids`, `positions`, `cu_seqlens` and the KV group's
|
||||
* slot mapping, block table and seqused, two sequences a lane (2i conditional, 2i + 1
|
||||
* unconditional). A chunk of a longer prompt is the same with positions that start
|
||||
* past zero.
|
||||
* FRAME one row a sequence, two sequences a lane, ordered as PROMPT's: the frame's embedding
|
||||
* is the plugin's (it samples the frame first), so `token_ids` are zero; `positions`,
|
||||
* the slot mapping and seqused place the row after its sequence's earlier ones.
|
||||
* WINDOW one entry per (window, branch): `cu_seqlens` bounds each entry's rows, `win_lead`
|
||||
* rows of the plugin's own (RadAudioGenDecl::win_lead) and then the window's latent
|
||||
* rows; positions count from 0 in each entry.
|
||||
* VOCODE one window, no rows.
|
||||
*
|
||||
* Every pointer is device memory unless the name ends in _h. */
|
||||
typedef struct RadAudioGenBatch {
|
||||
/* ---- PROMPT and FRAME: the lanes of the pass's requests, in entry order */
|
||||
int64_t n_lane;
|
||||
const int32_t* lane; /* [n_lane] */
|
||||
const int32_t* lane_h;
|
||||
/* PROMPT: 1 where this pass holds the end of the lane's prompt, so the last rows' hidden
|
||||
* states are the ones the first frame is sampled from. [n_lane] */
|
||||
const int32_t* last;
|
||||
const int32_t* last_h;
|
||||
/* FRAME: the frame each lane makes, from 0; frames before RadAudioGenDecl::first_frame only
|
||||
* move the model past the prompt. [n_lane] */
|
||||
const int32_t* frame;
|
||||
const int32_t* frame_h;
|
||||
/* PROMPT, FRAME and WINDOW: each lane's (WINDOW: each entry's) request seed, [n, 2] low word
|
||||
* first. */
|
||||
const uint32_t* seed;
|
||||
/* PROMPT and FRAME, one value per entry ([n_seq]): the entry's row in a state of two rows a
|
||||
* lane, 2 lane + branch, and the entry's last row in the pass (a prompt chunk's, or a frame's
|
||||
* one row). */
|
||||
const int32_t* slot;
|
||||
const int32_t* last_row;
|
||||
/* PROMPT: each entry's rows already in the KV before this pass ([n_seq]), the prefix a chunk's
|
||||
* rows attend to beside their own. */
|
||||
const int32_t* prefix;
|
||||
|
||||
/* ---- WINDOW, one value per entry ([n_seq]) */
|
||||
const float* t; /* where the entry's latent is, in [0, 1) */
|
||||
const float* t_next; /* where this step takes it */
|
||||
const float* cfg; /* v = u + cfg (c - u); 1 without a pair. FRAME: each lane's
|
||||
* guidance, [n_lane] */
|
||||
/* The entry holding this window's unconditional branch, or -1 for none; -2 marks an entry
|
||||
* that is one, whose rows serve another entry's step and which does not move its lane. */
|
||||
const int32_t* pair;
|
||||
const int32_t* win_lane; /* the lane the window's request runs in */
|
||||
const int32_t* win_lane_h;
|
||||
/* RAD_AGEN_F_*, device and host. */
|
||||
const int32_t* flags;
|
||||
const int32_t* flags_h;
|
||||
/* The window: its index in the request, its first frame and its frames, host and device. */
|
||||
const int32_t* win_index;
|
||||
const int32_t* win_index_h;
|
||||
const int32_t* win_frame0;
|
||||
const int32_t* win_frame0_h;
|
||||
const int32_t* win_frames;
|
||||
const int32_t* win_frames_h;
|
||||
/* The latent rows apart from the lead rows: [n_seq + 1] offsets over them, and for each the
|
||||
* row of the pass it is (`lat_rows` of them). */
|
||||
int64_t lat_rows;
|
||||
const int32_t* lat_cu;
|
||||
const int32_t* lat_cu_h;
|
||||
const int32_t* lat_src;
|
||||
|
||||
/* ---- VOCODE */
|
||||
int32_t voc_lane;
|
||||
int32_t voc_window;
|
||||
int32_t voc_latents; /* the window's latents */
|
||||
int32_t voc_left; /* latents at its start not played */
|
||||
int32_t voc_right; /* latents at its end not played */
|
||||
|
||||
/* VOCODE with RadAudioGenDecl::voc_chunk: the lane's first frame the pass reads, counted
|
||||
* from the first kept frame. 64 bits so that it starts past the struct's ABI 18 end. */
|
||||
int64_t voc_frame0;
|
||||
} RadAudioGenBatch;
|
||||
|
||||
/* A window entry's flags on a WINDOW pass. FIRST is RAD_GEN_F_FIRST's bit and the others are clear
|
||||
* of RAD_GEN_F_*'s, so one flags array serves the image generator's sampler ops as well. */
|
||||
enum {
|
||||
RAD_AGEN_F_FIRST = 1, /* the window's first step: its latent is the noise its seed draws and
|
||||
* its conditioning is built from the request's frames */
|
||||
RAD_AGEN_F_LAST = 8, /* the window's last step: after it the latent is final */
|
||||
RAD_AGEN_F_JOIN = 16 /* the window has a predecessor whose end it overlaps */
|
||||
};
|
||||
|
||||
/* What a video generator's pass carries beside the step batch's own fields (rad_builder.h,
|
||||
* RadVideoGenDecl, describes the passes). Every pass is one request's, in lane `lane`:
|
||||
*
|
||||
* VCOND `token_ids`, `positions` and `cu_seqlens` are the prompt, one sequence of `n_tok`
|
||||
* rows, attended causally, nothing paged.
|
||||
* VSTEP `n_tok` rows: the packed sequence, n_text + audio_channels n_audio + the video's
|
||||
* patches. No token ids.
|
||||
* ADECODE no rows of the pass's own.
|
||||
* VDECODE `dec_rows` rows of the decoder's own (rad_arch_video_positions, RAD_VPOS_DECODE).
|
||||
*
|
||||
* Every pointer is device memory unless the name ends in _h. */
|
||||
typedef struct RadVideoGenBatch {
|
||||
int32_t lane;
|
||||
RadVideoGeom geom; /* the request's geometry */
|
||||
/* The geometry again for ops that read it from an operand, i32 [RAD_VGEOM_N] (below). */
|
||||
const int32_t* geom_d;
|
||||
/* The same lane, i32 [1], and 1 on a request's first step (0 otherwise), i32 [1]. */
|
||||
const int32_t* lane_d;
|
||||
const int32_t* first_d;
|
||||
/* i32 [4]: the packed sequence's segments, {0, n_text, n_text + audio rows, n_tok} -- text,
|
||||
* soundtrack, video -- and i32 [4] {0, video rows, 0, audio rows}, the bounds of the lane's two
|
||||
* latents counted from each one's first row. */
|
||||
const int32_t* seg_cu;
|
||||
const int32_t* lat_cu;
|
||||
|
||||
/* ---- VCOND: every step the request takes, the video's timestep and the soundtrack's */
|
||||
int32_t n_steps;
|
||||
const float* t_video; /* [n_steps] */
|
||||
const float* t_audio; /* [n_steps] */
|
||||
|
||||
/* ---- VSTEP */
|
||||
int32_t step; /* from 0; the first draws the lane's noise */
|
||||
/* 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. */
|
||||
const uint32_t* seed;
|
||||
|
||||
/* ---- VDECODE */
|
||||
int32_t chunk; /* from 0, of n_chunks */
|
||||
int32_t n_chunks;
|
||||
int64_t dec_rows; /* RAD_VPOS_DECODE */
|
||||
int32_t n_frames_out; /* frames the pass writes to frames_out */
|
||||
|
||||
/* ---- ADECODE */
|
||||
int64_t n_samples; /* samples a channel the pass writes to audio_out */
|
||||
} RadVideoGenBatch;
|
||||
|
||||
/* RadVideoGenBatch::geom_d. The request's latent geometry, then a VDECODE pass's chunk: the latent
|
||||
* frames it reads (from latent frame `chunk_lat0`), its latent grid again so that (frames, h, w,
|
||||
* first, last) is one run, and the same chunk in patches. */
|
||||
enum {
|
||||
RAD_VGEOM_LAT_FRAMES = 0, RAD_VGEOM_LAT_H = 1, RAD_VGEOM_LAT_W = 2, RAD_VGEOM_N_TEXT = 3,
|
||||
RAD_VGEOM_N_AUDIO = 4, RAD_VGEOM_CHUNK_LAT0 = 5,
|
||||
RAD_VGEOM_CHUNK_FRAMES = 6, RAD_VGEOM_CHUNK_H = 7, RAD_VGEOM_CHUNK_W = 8,
|
||||
RAD_VGEOM_CHUNK_FIRST = 9, RAD_VGEOM_CHUNK_LAST = 10,
|
||||
RAD_VGEOM_PATCH_FRAMES = 11, RAD_VGEOM_PATCH_H = 12, RAD_VGEOM_PATCH_W = 13,
|
||||
RAD_VGEOM_N = 16
|
||||
};
|
||||
|
||||
/* ================================================================== operands */
|
||||
/* RAD_OPK_KV names a KV group's pool rather than a buffer: attention and kv_store take the cache
|
||||
* as an operand, and RadKVGroupBatch carries the slot mapping and block table but not the pool
|
||||
|
||||
@@ -51,6 +51,41 @@ inline bool rad_yarn_valid(const RadYarn& y) {
|
||||
y.high >= y.low && std::isfinite(y.magnitude) && y.magnitude > 0.0f));
|
||||
}
|
||||
|
||||
/* Llama 3's frequency scaling (transformers' rope_type "llama3"), the other static scaling a
|
||||
* checkpoint may name: a frequency whose wavelength 2 pi / inv is shorter than orig / high is
|
||||
* kept, one longer than orig / low is divided by `factor`, and those between are blended,
|
||||
* s = (orig / wavelength - low) / (high - low), (1 - s) inv / factor + s inv. f32 throughout, as
|
||||
* transformers computes it. `factor` zero is off. */
|
||||
struct RadLlama3 {
|
||||
float factor = 0.0f;
|
||||
float low = 0.0f; /* low_freq_factor */
|
||||
float high = 0.0f; /* high_freq_factor */
|
||||
float orig = 0.0f; /* original_max_position_embeddings */
|
||||
|
||||
RAD_YARN_HD float frequency(float inv) const {
|
||||
if (factor == 0.0f) return inv;
|
||||
const float wavelen = 6.28318530717958647692f / inv;
|
||||
if (wavelen < orig / high) return inv;
|
||||
if (wavelen > orig / low) return inv / factor;
|
||||
const float sm = (orig / wavelen - low) / (high - low);
|
||||
return (1.0f - sm) * inv / factor + sm * inv;
|
||||
}
|
||||
};
|
||||
|
||||
inline RadLlama3 rad_llama3_args(const RadArgs* a) {
|
||||
const float factor = (float)rad_args_getf_or(a, "llama3_factor", 0.0);
|
||||
if (factor == 0.0f) return {};
|
||||
return { factor, (float)rad_args_getf_or(a, "llama3_low", 0.0),
|
||||
(float)rad_args_getf_or(a, "llama3_high", 0.0),
|
||||
(float)rad_args_getf_or(a, "llama3_orig", 0.0) };
|
||||
}
|
||||
|
||||
inline bool rad_llama3_valid(const RadLlama3& l) {
|
||||
return std::isfinite(l.factor) &&
|
||||
(l.factor == 0.0f || (l.factor >= 1.0f && l.low > 0.0f && l.high > l.low &&
|
||||
std::isfinite(l.high) && l.orig > 0.0f && std::isfinite(l.orig)));
|
||||
}
|
||||
|
||||
#undef RAD_YARN_HD
|
||||
|
||||
#else /* C */
|
||||
@@ -82,6 +117,23 @@ static inline RadYarn rad_yarn_args(const RadArgs* a) {
|
||||
return y;
|
||||
}
|
||||
|
||||
typedef struct RadLlama3 {
|
||||
float factor; /* zero: off */
|
||||
float low;
|
||||
float high;
|
||||
float orig;
|
||||
} RadLlama3;
|
||||
|
||||
static inline float rad_llama3_frequency(const RadLlama3* l, float inv) {
|
||||
float wavelen, sm;
|
||||
if (l->factor == 0.0f) return inv;
|
||||
wavelen = 6.28318530717958647692f / inv;
|
||||
if (wavelen < l->orig / l->high) return inv;
|
||||
if (wavelen > l->orig / l->low) return inv / l->factor;
|
||||
sm = (l->orig / wavelen - l->low) / (l->high - l->low);
|
||||
return (1.0f - sm) * inv / l->factor + sm * inv;
|
||||
}
|
||||
|
||||
static inline int rad_yarn_valid(const RadYarn* y) {
|
||||
return isfinite(y->factor) && (y->factor == 0.0f ||
|
||||
(y->factor >= 1.0f && isfinite(y->low) && isfinite(y->high) && y->high >= y->low &&
|
||||
|
||||
Reference in new issue
Block a user