radiance 1.13.0

New models:
- Tencent Hy-Memory (HYV3) from its BF16, FP8 and GGUF checkpoints, with its native tool calls
- M2XFP4: metadata-augmented MXFP4 weights for Qwen3.8-27B and its DFlash2 drafter (top-1 89.1% ->
  92.4% against plain MXFP4), its quantiser and recipe, and ParoQuant's M2XFP4 checkpoints
- Gemma 4 E2B/E4B audio input; NVIDIA-Nemotron-3.5-Bolt-E4B in int8 and 4-bit, on two cards by
  default; Ornith-1.5-122B compose files

Speed:
- Weight reads beside the decode all-reduces (qwen35_fp8, gemma3; --weight-prefetch, on by default):
  the 27B MXFP4 with DFlash2 61.2 -> 67.9 tok/s at c1, TranslateGemma 27B +13%
- Head-256 split-KV decode attention at 256 workgroups and without bank conflicts (27B: +3-5% a
  step at 25-41K context), and the prefill KV split for small launches (a short turn over a long
  context decodes the other sequences 27% faster in that step)
- MXFP4 decode gate_up, activation and fp8 quantiser in one launch (27B, Gemma 3 up to 64 rows:
  TranslateGemma +2-5% at 2-8 streams)
- ParoQuant int4/int5 tiled GEMMs 6-9% faster
- Speculation: a step's tokens are sent before the drafter's passes end, no step waits behind one
  that ends its requests, and the MTP head's first draft no longer collapses after short chats in
  the prefix cache

Features:
- --kv-resident N: KV streaming for a context bigger than VRAM (qwen4exp_fp8, GLM-5.3)
- MiniMax-H3: reference media and keyframe conditioning with the VAE encoders
- Krea 2 rebalance presets balanced, detail and subtle
- The dashboard's request history; a stall watch that reports a frozen step's thread and stack
- rad-tune at the served configuration, collectives measured as the exchange; Gemma 4 E2B/E4B's
  per-layer embeddings read ahead off the step path
- A GPTQ calibration tap and recipe for Gemma 4 31B's w4a8 container

Fixes:
- The two-rank all-reduce checks that a peer's push has landed before reducing it
- z-lab's DFlash drafter on Gemma 4 31B keeps drafting after a prefix-cache hit
- MiniMax-H3's modulation rows

Changes: Plugin ABI 31: out-of-tree plugins must be rebuilt. RADIANCE_PREFETCH_BESIDE is
--weight-prefetch now; RADIANCE_NO_HUGE_PAGES, RADIANCE_IDS_EARLY, RADIANCE_ATTN_BAND_SPLIT,
R4D_PREFILL_SPLITS, RAD_PLE_TIMING and RAD_PLE_CACHE_ROWS are gone.
This commit is contained in:
Deadcode committed 2026-10-27 16:47:35 +01:00
198 files changed
+20904 -3240

No files matched your search

+474 -27
View File
@@ -15,6 +15,11 @@
* (`w:h`) instead lays the model's own canvas over that ratio: the short edge (the model's, or
* `short_edge`), the area capped at the model's largest, both sides rounded to the multiple.
* A video's length is a whole number of the model's chunks: `seconds` is rounded up to the next.
* A model with the VAEs' encoders also takes keyframes, form file parts `first_frame` and
* `last_frame` (tasks i2va, l2va, fl2va), or reference media (task ref2va): `ref_image_N` (at
* most 9), `ref_video_N` (3) with a soundtrack `ref_video_audio_N` and `ref_audio_N` (3), N from 1
* without gaps within each kind. The parts carry the bytes: `input_reference` (but for i2va on a
* model that starts from a picture) and non-empty `conditions` are refused.
* A model with a length head (LTX-2.5's duration head) chooses the length of a request that names
* none, or says `"seconds": "auto"`; the video object reports it once chosen. A model with more
* than one decoder takes `decoder`, one of their names (LTX-2.5: `vae`, the default, or
@@ -53,10 +58,12 @@
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <random>
#include <thread>
#include <vector>
namespace rad {
namespace server {
@@ -65,16 +72,25 @@ struct Server::VideoJob {
std::string id, model, prompt, seconds, size;
int64_t created_at = 0;
uint64_t seed = 0;
int32_t sampler = 0; /* the request's RAD_SAMPLER_* */
double shift_video = 0, shift_audio = 0; /* 0: the model's */
std::shared_ptr<gen::VideoTicket> ticket;
std::thread worker;
/* Under Server::videos_m_ from here on. */
std::string status = "queued";
int progress = 0;
/* The steps done of the total (0 before the first), and the first one's steady-clock time in
* ns, which the per-step log line measures its rate from. */
int steps_done = 0, steps_total = 0;
int64_t step_ns0 = 0;
int64_t completed_at = 0, expires_at = 0;
bool failed = false;
std::string err_code, err_message;
std::string mp4;
std::vector<uint8_t> first; /* the first frame, RGB, for the thumbnail */
/* A job with reference media or keyframes: its task and its parts in conditioning order. */
std::string task;
std::vector<std::string> refs;
/* Latent upscaling, as the generator resolved it: the factor, the evaluations after it and the
* small size; refine 0 when the video is not upscaled. */
double upscale = 0.0;
@@ -144,8 +160,110 @@ void canvas_for(double rw, double rh, int64_t short_edge, int64_t max_area, int6
*h = std::max<int64_t>(mult, (int64_t)std::nearbyint(H / mult) * mult);
}
/* The most reference media of each kind a request carries, ComfyUI's ref2va workflow's; a model
* may take fewer. */
constexpr int32_t kMaxRefImages = 9, kMaxRefVideos = 3, kMaxRefAudios = 3;
/* A reference part's kind and number from its name; false for any other name. `ref_video_audio_`
* is tried before `ref_video_`, which it starts with. */
bool parse_ref_name(const std::string& name, gen::VideoRefMedia::Kind* kind, int32_t* index) {
static const struct { const char* pfx; gen::VideoRefMedia::Kind kind; } kKinds[] = {
{ "ref_image_", gen::VideoRefMedia::IMAGE },
{ "ref_video_audio_", gen::VideoRefMedia::VIDEO_AUDIO },
{ "ref_video_", gen::VideoRefMedia::VIDEO },
{ "ref_audio_", gen::VideoRefMedia::AUDIO },
};
for (const auto& k : kKinds) {
const size_t n = std::strlen(k.pfx);
if (name.compare(0, n, k.pfx) != 0 || name.size() == n) continue;
char* e = nullptr;
const long v = std::strtol(name.c_str() + n, &e, 10);
if (*e == '\0' && v >= 1 && v <= 99) { *kind = k.kind; *index = (int32_t)v; return true; }
}
return false;
}
/* A ref2va request's parts, refused naming the part when one is empty, over its kind's count,
* numbered with a gap or twice, or a soundtrack with no video of its number. On return `refs` is
* sorted by (kind, number), the order of the conditioning. */
int validate_refs(std::vector<gen::VideoRefMedia>& refs, const VideoGenInfo& info, ApiError* err) {
if (refs.empty()) {
*err = err_bad("task", "ref2va takes at least one reference part: ref_image_N, "
"ref_video_N, ref_video_audio_N or ref_audio_N");
return -1;
}
const struct {
const char* pfx;
gen::VideoRefMedia::Kind kind;
int32_t cap;
} kinds[] = {
{ "ref_image", gen::VideoRefMedia::IMAGE,
std::min(kMaxRefImages, (int32_t)info.max_ref_images) },
{ "ref_video", gen::VideoRefMedia::VIDEO,
std::min(kMaxRefVideos, (int32_t)info.max_ref_videos) },
{ "ref_video_audio", gen::VideoRefMedia::VIDEO_AUDIO,
std::min(kMaxRefVideos, (int32_t)info.max_ref_videos) },
{ "ref_audio", gen::VideoRefMedia::AUDIO,
std::min(kMaxRefAudios, (int32_t)info.max_ref_audios) },
};
for (const auto& k : kinds) {
std::vector<bool> got(k.cap + 1, false);
int n = 0;
for (const gen::VideoRefMedia& m : refs) {
if (m.kind != k.kind) continue;
++n;
if (m.data.empty())
return *err = err_bad(m.name, "'" + m.name + "' carries an empty file"), -1;
if (m.index > k.cap)
return *err = err_bad(m.name, k.cap == 0
? "this model takes no parts named " +
std::string(k.pfx) + "_N"
: "this model takes at most " +
std::to_string(k.cap) + " parts named " +
std::string(k.pfx) + "_N"), -1;
if (got[m.index])
return *err = err_bad(m.name, "two parts are numbered " + std::string(k.pfx) +
"_" + std::to_string(m.index)), -1;
got[m.index] = true;
}
for (int i = 1; i <= n; ++i)
if (!got[i])
return *err = err_bad(std::string(k.pfx) + "_" + std::to_string(i),
"the " + std::string(k.pfx) + "_N parts must be numbered 1 "
"to " + std::to_string(n) + " without gaps or repeats"), -1;
}
for (const gen::VideoRefMedia& m : refs) {
if (m.kind != gen::VideoRefMedia::VIDEO_AUDIO) continue;
bool paired = false;
for (const gen::VideoRefMedia& v : refs)
paired = paired || (v.kind == gen::VideoRefMedia::VIDEO && v.index == m.index);
if (!paired)
return *err = err_bad(m.name, "'" + m.name + "' is the soundtrack of a reference "
"video; give ref_video_" + std::to_string(m.index) +
" with it"), -1;
}
std::stable_sort(refs.begin(), refs.end(),
[](const gen::VideoRefMedia& a, const gen::VideoRefMedia& b) {
return a.kind != b.kind ? a.kind < b.kind : a.index < b.index;
});
return 0;
}
} /* namespace */
/* A video model's capabilities on /v1/models and /server_info: the counts validate_refs applies,
* the samplers and the model's shifts. */
void Server::video_caps(json& caps, const IVideoGen* gen) {
const VideoGenInfo& info = gen->info();
caps["video_max_text"] = info.max_text;
caps["video_max_ref_images"] = std::min(kMaxRefImages, (int32_t)info.max_ref_images);
caps["video_max_ref_videos"] = std::min(kMaxRefVideos, (int32_t)info.max_ref_videos);
caps["video_max_ref_audios"] = std::min(kMaxRefAudios, (int32_t)info.max_ref_audios);
caps["video_samplers"] = json::array({ "euler", "res_multistep" });
caps["video_shift"] = info.shift_video;
caps["video_audio_shift"] = info.shift_audio;
}
HttpResponse Server::handle_video_create(const HttpRequest& req) {
if (!deps_.videos || !deps_.tok)
return error_response(err_not_implemented(
@@ -153,8 +271,14 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
req.peer);
const VideoGenInfo& info = deps_.videos->info();
const bool takes_refs = info.max_ref_images > 0 || info.max_ref_videos > 0 ||
info.max_ref_audios > 0;
/* JSON, or a form whose fields are strings (OpenAI's clients send the create call as a form,
* since it may carry a file). */
* since it may carry a file). Reference media and keyframes are file parts, collected here and
* checked once the task is known. */
std::vector<gen::VideoRefMedia> refs;
/* The keyframes, `first_frame` and `last_frame`. */
std::vector<gen::VideoRefMedia> keys;
json body = json::object();
std::vector<uint8_t> ref_bytes;
bool has_ref = false;
@@ -165,9 +289,32 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
has_ref = true;
continue;
}
if (!p.filename.empty())
return error_response(err_bad(p.name, "'" + p.name + "' takes text, not a file"),
req.peer);
if (p.name == "first_frame" || p.name == "last_frame") {
if (p.filename.empty())
return error_response(err_bad(p.name, "'" + p.name + "' must carry an image "
"file"), req.peer);
gen::VideoRefMedia m;
m.kind = p.name == "first_frame" ? gen::VideoRefMedia::FIRST_FRAME
: gen::VideoRefMedia::LAST_FRAME;
m.name = p.name;
m.filename = p.filename;
m.content_type = p.content_type;
m.data = p.data;
keys.push_back(std::move(m));
continue;
}
if (!p.filename.empty()) {
gen::VideoRefMedia m;
if (!parse_ref_name(p.name, &m.kind, &m.index))
return error_response(err_bad(p.name, "'" + p.name + "' takes text, not a "
"file"), req.peer);
m.name = p.name;
m.filename = p.filename;
m.content_type = p.content_type;
m.data = p.data;
refs.push_back(std::move(m));
continue;
}
json v = p.data;
if (p.name == "target" || p.name == "conditions" ||
((p.name == "loras" || p.name == "lora") && !p.data.empty() &&
@@ -186,6 +333,7 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
static const char* kKnown[] = {
"model", "prompt", "seconds", "size", "input_reference", "seed", "num_inference_steps",
"task", "target", "conditions", "user", "latent_upscale", "upscale_refine_steps",
"sampler", "shift", "audio_shift", "first_frame_mode",
"negative_prompt", "decoder", "loras", "lora",
/* RadVideoGenBatch::guidance_h's order */
"guidance_scale", "stg_scale", "modality_scale", "guidance_rescale",
@@ -243,19 +391,104 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
has_ref = true;
}
if (has_ref && !info.image_cond)
return error_response(err_unsupported("input_reference", "this model makes video from "
"text only"), req.peer);
const std::string task_want = has_ref ? "i2va" : "t2va";
if (has(body, "task") && (!body["task"].is_string() ||
body["task"].get<std::string>() != task_want)) {
ApiError e = err_unsupported("task", has_ref ? "a video from a picture is i2va"
: "this request makes video from text: t2va");
e.supported = { task_want };
return error_response(err_unsupported("input_reference", takes_refs
? "this server reads reference media as ref_image_N "
"file parts"
: "this model makes video from text only"), req.peer);
/* With `input_reference` the task is i2va. */
if (has_ref && has(body, "task") && (!body["task"].is_string() ||
body["task"].get<std::string>() != "i2va")) {
ApiError e = err_unsupported("task", "a video from a picture is i2va");
e.supported = { "i2va" };
return error_response(e, req.peer);
}
/* Otherwise t2va, or what the model takes: ref2va with reference media, the keyframe tasks
* with a video encoder. */
bool ref2va = false;
std::string kf_task; /* a keyframe task named */
if (!has_ref && has(body, "task")) {
const std::string task = body["task"].is_string() ? body["task"].get<std::string>() : "";
if (task == "ref2va" && takes_refs) ref2va = true;
/* The parts say which frames there are; a named task is checked against them below. */
else if (info.max_enc_side > 0 && (task == "i2va" || task == "l2va" || task == "fl2va"))
kf_task = task;
else if (task != "t2va") {
std::string what = "this server makes video from text";
std::vector<std::string> sup{ "t2va" };
if (info.image_cond) { what += ", from a picture"; sup.push_back("i2va"); }
if (info.max_enc_side > 0) {
what += ", or between keyframes";
for (const char* k : { "i2va", "l2va", "fl2va" })
if (std::find(sup.begin(), sup.end(), std::string(k)) == sup.end())
sup.push_back(k);
}
if (takes_refs) { what += ", from reference media"; sup.push_back("ref2va"); }
what += ": ";
for (size_t i = 0; i < sup.size(); ++i) what += (i ? ", " : "") + sup[i];
ApiError e = err_unsupported("task", what);
e.supported = sup;
return error_response(e, req.peer);
}
}
if (has(body, "conditions") && (!body["conditions"].is_array() || !body["conditions"].empty()))
return error_response(err_unsupported("conditions", "this server makes video from text "
"only; conditions must be empty"), req.peer);
return error_response(err_unsupported("conditions", takes_refs
? "reference media arrive as ref_* file parts; "
"conditions must be empty"
: "this server makes video from text only; "
"conditions must be empty"), req.peer);
/* Reference parts without task ref2va are a mistake about the task. */
if (!refs.empty() && !ref2va)
return bad("task", "reference files are given: send them with task ref2va, or send "
"none");
if (ref2va) {
ApiError e;
if (validate_refs(refs, info, &e) < 0) return error_response(e, req.peer);
}
/* At most one keyframe of each, not with reference media, and only with a video encoder. */
{
int n_first = 0, n_last = 0;
for (const gen::VideoRefMedia& m : keys)
if (m.kind == gen::VideoRefMedia::FIRST_FRAME) ++n_first; else ++n_last;
if (n_first > 1) return bad("first_frame", "at most one first_frame part");
if (n_last > 1) return bad("last_frame", "at most one last_frame part");
/* A keyframe's <Picture N> counts the pictures present, as ComfyUI numbers them: a last
* frame alone is <Picture 1>. */
for (gen::VideoRefMedia& m : keys)
m.index = m.kind == gen::VideoRefMedia::LAST_FRAME && n_first ? 2 : 1;
if (!keys.empty()) {
if (info.max_enc_side <= 0)
return bad("first_frame", "this model has no video encoder, so it cannot pin a "
"keyframe");
if (!refs.empty())
return bad("first_frame", "a keyframe completion conditions on its pinned frames, "
"not on reference media: give first_frame/last_frame or "
"the ref_* parts, not both");
}
/* A named keyframe task carries its frames. */
if (!kf_task.empty()) {
if ((kf_task == "i2va" || kf_task == "fl2va") && n_first < 1)
return bad("first_frame", "task '" + kf_task + "' pins a first frame: give a "
"first_frame part");
if ((kf_task == "l2va" || kf_task == "fl2va") && n_last < 1)
return bad("last_frame", "task '" + kf_task + "' pins a last frame: give a "
"last_frame part");
}
}
/* How the first frame is fitted to the video: stretched, as ComfyUI does, or `center`, scaled
* to cover it and cropped. The last frame is always cropped, as ComfyUI does. */
bool first_center = false;
if (has(body, "first_frame_mode")) {
const std::string fm = body["first_frame_mode"].is_string()
? body["first_frame_mode"].get<std::string>() : "";
if (fm.empty() || fm == "stretch")
;
else if (fm == "center")
first_center = true;
else
return bad("first_frame_mode", "expected \"stretch\" (the reference's own resize) "
"or \"center\" (keep the anchor's aspect, crop the "
"canvas)");
}
json target = json::object();
if (has(body, "target")) {
if (!body["target"].is_object()) return bad("target", "expected an object");
@@ -384,6 +617,34 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
(unit + 1) + ")");
r->steps = (int32_t)v;
}
if (has(body, "sampler")) {
const std::string sm = body["sampler"].is_string() ? body["sampler"].get<std::string>() : "";
if (sm.empty() || sm == "euler") r->sampler = RAD_SAMPLER_EULER;
else if (sm == "res_multistep") r->sampler = RAD_SAMPLER_RES_MULTISTEP;
else {
ApiError e = err_unsupported("sampler", "the sampler is euler (first order) or "
"res_multistep (second order on the clean "
"estimate)");
e.supported = { "euler", "res_multistep" };
return error_response(e, req.peer);
}
}
/* Each stream's schedule shift; any positive shift is safe, and (0, 100] refuses typos. */
if (has(body, "shift")) {
double v = 0;
if (!number(body["shift"], &v) || !(v > 0.0) || v > 100.0)
return bad("shift", "the video schedule shift: a number in (0, 100], or omit it to run "
"the model's trained shift " + rad::fmt("%g", info.shift_video));
r->shift_video = v;
}
if (has(body, "audio_shift")) {
double v = 0;
if (!number(body["audio_shift"], &v) || !(v > 0.0) || v > 100.0)
return bad("audio_shift", "the soundtrack schedule shift: a number in (0, 100], or "
"omit it to run the model's trained shift " +
rad::fmt("%g", info.shift_audio));
r->shift_audio = v;
}
/* ---- guidance */
static const char* kGuide[8] = {
"guidance_scale", "stg_scale", "modality_scale", "guidance_rescale",
@@ -442,25 +703,139 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
std::random_device rd;
r->seed = (((uint64_t)rd() << 32) ^ rd()) & (((uint64_t)1 << 53) - 1);
}
r->refs = std::move(refs);
/* The keyframes after the references, the first before the last. */
std::stable_sort(keys.begin(), keys.end(), [](const gen::VideoRefMedia& a,
const gen::VideoRefMedia& b) {
return a.kind < b.kind;
});
r->refs.insert(r->refs.end(), keys.begin(), keys.end());
const bool has_keys = !keys.empty();
/* ---- the prompt: the template's text with special tokens, the prompt without */
/* Pixels as the VAE encoders take them: RGB in 0..255, frame after frame. */
auto pixels = [](gen::VideoRefMedia& m, const std::vector<mm::Frame>& fr) {
m.enc_frames = (int32_t)fr.size();
m.enc_h = fr.front().h;
m.enc_w = fr.front().w;
m.enc_pixels.clear();
for (const mm::Frame& f : fr) m.enc_pixels.insert(m.enc_pixels.end(), f.rgb.begin(),
f.rgb.end());
};
/* A keyframe is decoded once, onto the output canvas, and that frame serves both the VAE pin
* and the vision tower, as in ComfyUI: the tower sees the pixels that are pinned, not the file
* at its own size. The first frame is stretched unless first_frame_mode=center, the last is
* centre-cropped. `canvas` parallels r->refs. */
std::vector<mm::Frame> canvas;
if (has_keys && deps_.mm) {
canvas.resize(r->refs.size());
for (size_t i = 0; i < r->refs.size(); ++i) {
gen::VideoRefMedia& m = r->refs[i];
if (m.kind != gen::VideoRefMedia::FIRST_FRAME &&
m.kind != gen::VideoRefMedia::LAST_FRAME)
continue;
std::string why;
if (deps_.mm->encoder_image_canvas((const uint8_t*)m.data.data(), m.data.size(),
(int32_t)W, (int32_t)H,
m.kind == gen::VideoRefMedia::LAST_FRAME ||
first_center,
&canvas[i], &why) < 0)
return bad(m.name.c_str(), "'" + m.name + "' could not be decoded: " + why);
pixels(m, { canvas[i] });
}
}
/* ---- the prompt: the template's text with special tokens, the media in ComfyUI's order, the
* prompt without. A picture is its `<Picture N>: ` label, encoded on its own, then its
* tokens; a video its soundtrack's `<Audio N>: ` label if it has one, then its `<Video N>: `;
* a soundtrack its `<Audio N>: `, numbered with the videos' soundtracks. Only the pictures are
* shown to the text encoder: videos and soundtracks reach the model through the VAEs. */
{
static const std::string kSlot = "{prompt}";
const std::string& t = info.tmpl;
const size_t p = t.find(kSlot);
auto render = [&](const std::string& text, std::vector<int32_t>* out) {
auto put = [&](const std::string& s, bool special) {
if (s.empty()) return;
const auto ids = deps_.tok->encode(s, false, special);
out->insert(out->end(), ids.begin(), ids.end());
};
put(t.substr(0, p), true);
put(text, false);
put(t.substr(p + kSlot.size()), true);
auto put = [&](std::vector<int32_t>* out, const std::string& s, bool special) {
if (s.empty()) return;
const auto ids = deps_.tok->encode(s, false, special);
out->insert(out->end(), ids.begin(), ids.end());
};
render(prompt, &r->tokens);
auto render = [&](const std::string& text, std::vector<int32_t>* out) {
put(out, t.substr(0, p), true);
put(out, text, false);
put(out, t.substr(p + kSlot.size()), true);
};
if ((ref2va || has_keys) && deps_.mm && deps_.mm->accepts(MediaKind::Image)) {
/* Every picture is processed before any token is laid down. ComfyUI also shows the text
* encoder a video's frames at 2 fps, which this does not. */
std::vector<std::shared_ptr<mm::Item>> pics;
MediaOpts mo;
mo.max_pixels = (int64_t)W * (int64_t)H; /* scaled to the video's area */
/* A keyframe is shown to the tower as well as pinned, as its canvas frame. */
auto anchor = [](const gen::VideoRefMedia& m) {
return m.kind == gen::VideoRefMedia::IMAGE ||
m.kind == gen::VideoRefMedia::FIRST_FRAME ||
m.kind == gen::VideoRefMedia::LAST_FRAME;
};
auto keyframe = [](const gen::VideoRefMedia& m) {
return m.kind == gen::VideoRefMedia::FIRST_FRAME ||
m.kind == gen::VideoRefMedia::LAST_FRAME;
};
for (size_t i = 0; i < r->refs.size(); ++i) {
const gen::VideoRefMedia& m = r->refs[i];
if (!anchor(m)) continue;
std::shared_ptr<mm::Item> it;
std::string why;
int rc;
if (keyframe(m) && !canvas.empty() && canvas[i].w > 0)
rc = deps_.mm->prepare_frame(canvas[i], &it, &why);
else {
const std::vector<uint8_t> bytes(m.data.begin(), m.data.end());
rc = deps_.mm->prepare(MediaKind::Image, bytes, mo, &it, &why);
}
if (rc < 0 || !it)
return bad(m.name.c_str(), "'" + m.name + "' could not be used: " + why);
pics.push_back(std::move(it));
}
auto has_soundtrack = [&](int32_t idx) {
for (const gen::VideoRefMedia& m : r->refs)
if (m.kind == gen::VideoRefMedia::VIDEO_AUDIO && m.index == idx) return true;
return false;
};
put(&r->tokens, t.substr(0, p), true);
std::shared_ptr<mm::PromptMedia> media;
if (!pics.empty()) media = std::make_shared<mm::PromptMedia>();
size_t i = 0;
for (const gen::VideoRefMedia& m : r->refs) {
if (!anchor(m)) continue;
put(&r->tokens, "<Picture " + std::to_string(m.index) + ">: ", false);
mm::Placed pl;
pl.item = pics[i++];
pl.tok = (int32_t)r->tokens.size();
r->tokens.insert(r->tokens.end(), pl.item->expand.begin(),
pl.item->expand.end());
media->items.push_back(std::move(pl));
}
int audio_ord = 0;
for (const gen::VideoRefMedia& m : r->refs) {
if (m.kind != gen::VideoRefMedia::VIDEO) continue;
if (has_soundtrack(m.index))
put(&r->tokens, "<Audio " + std::to_string(++audio_ord) + ">: ", false);
put(&r->tokens, "<Video " + std::to_string(m.index) + ">: ", false);
}
for (const gen::VideoRefMedia& m : r->refs) {
if (m.kind != gen::VideoRefMedia::AUDIO) continue;
put(&r->tokens, "<Audio " + std::to_string(++audio_ord) + ">: ", false);
}
if (media) { media->layout(); r->media = std::move(media); }
put(&r->tokens, prompt, false);
put(&r->tokens, t.substr(p + kSlot.size()), true);
} else {
render(prompt, &r->tokens);
}
if ((int64_t)r->tokens.size() > info.max_text)
return bad("prompt", "the prompt is " + std::to_string(r->tokens.size()) +
return bad("prompt", "the prompt" + std::string((ref2va || has_keys)
? " with its media" : "") +
" is " + std::to_string(r->tokens.size()) +
" tokens; this model takes at most " +
std::to_string(info.max_text));
if (info.guidance) {
@@ -509,6 +884,43 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
std::memcpy(&r->image[3 * i], &sized.rgba[4 * i], 3);
}
/* ---- every reference as its VAE encoder takes it (the keyframes are done above): pictures and
* video frames fitted to the encoder's bounds, soundtracks as mono samples at the model's rate,
* one copy a channel. The driver pads both to the encoders' clips and hops. */
if ((ref2va || !keys.empty()) && deps_.mm) {
for (gen::VideoRefMedia& m : r->refs) {
std::string why;
const uint8_t* p = (const uint8_t*)m.data.data();
const size_t n = m.data.size();
std::vector<mm::Frame> fr(1);
int st = 0;
if (m.kind == gen::VideoRefMedia::FIRST_FRAME || m.kind == gen::VideoRefMedia::LAST_FRAME)
continue;
if (m.kind == gen::VideoRefMedia::IMAGE)
st = deps_.mm->encoder_image(p, n, (int32_t)info.enc_ratio,
(int32_t)info.max_enc_side, &fr[0], &why);
else if (m.kind == gen::VideoRefMedia::VIDEO)
st = deps_.mm->encoder_video(p, n, (int32_t)info.enc_ratio,
(int32_t)info.max_enc_side,
(int32_t)info.max_enc_frames, &fr, &why);
else {
std::vector<float> mono;
if (deps_.mm->encoder_audio(p, n, (int32_t)info.sample_rate, info.max_enc_samples,
&mono, &why) < 0 || mono.empty())
return bad(m.name.c_str(), "'" + m.name + "' could not be decoded: " + why);
m.enc_channels = (int32_t)info.channels;
m.enc_n = (int64_t)mono.size();
m.enc_samples.clear();
for (int32_t c = 0; c < m.enc_channels; ++c)
m.enc_samples.insert(m.enc_samples.end(), mono.begin(), mono.end());
continue;
}
if (st < 0 || fr.empty())
return bad(m.name.c_str(), "'" + m.name + "' could not be decoded: " + why);
pixels(m, fr);
}
}
/* ---- admitted and queued: the job holds its place until its file is written */
if (!loras.empty()) {
std::string why, note;
@@ -529,6 +941,14 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
j->size = std::to_string(W) + "x" + std::to_string(H);
j->created_at = unix_now();
j->seed = r->seed;
j->sampler = r->sampler;
j->shift_video = r->shift_video;
j->shift_audio = r->shift_audio;
for (const gen::VideoRefMedia& m : r->refs) j->refs.push_back(m.name);
if (ref2va) j->task = "ref2va";
else if (keys.size() == 2) j->task = "fl2va";
else if (!keys.empty())
j->task = keys[0].kind == gen::VideoRefMedia::FIRST_FRAME ? "i2va" : "l2va";
j->ticket = std::make_shared<gen::VideoTicket>();
r->ticket = j->ticket;
const int64_t n_tok = (int64_t)r->tokens.size();
@@ -557,7 +977,7 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
j->refine = tk.refine;
j->upscale_from = std::to_string(tk.small_width) + "x" + std::to_string(tk.small_height);
}
RAD_INFO("video %s: %s, %s, %lld prompt tokens, %d steps%s, seed=%llu",
RAD_INFO("video %s: %s, %s, %lld prompt tokens, %d steps%s%s, seed=%llu",
j->id.c_str(), j->size.c_str(),
auto_len ? "the length the model chooses"
: rad::fmt("%lld frames (%s s)", (long long)frames, j->seconds.c_str()).c_str(),
@@ -566,6 +986,7 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
(int)(tk.steps - tk.refine), (int)tk.small_width,
(int)tk.small_height, (int)tk.refine).c_str()
: "",
j->refs.empty() ? "" : rad::fmt(", %zu reference parts", j->refs.size()).c_str(),
(unsigned long long)j->seed);
std::lock_guard<std::mutex> lk(videos_m_);
sweep_videos_locked(unix_now());
@@ -597,9 +1018,24 @@ void Server::video_work(std::shared_ptr<VideoJob> j) {
continue;
}
if (e.kind == gen::VideoEvent::STEP) {
const int64_t now = std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::steady_clock::now().time_since_epoch()).count();
std::lock_guard<std::mutex> lk(videos_m_);
j->status = "in_progress";
j->progress = t.steps > 0 ? (int)(90LL * e.steps_done / t.steps) : 0;
j->steps_done = e.steps_done;
j->steps_total = t.steps;
if (!j->step_ns0) j->step_ns0 = now;
/* A line a step, its rate the mean since the first step, so the eta of an upscaled
* video, whose small steps are faster, is an estimate. */
const double el = (double)(now - j->step_ns0) / 1e9;
const double per = e.steps_done > 1 ? el / (e.steps_done - 1) : 0.0;
if (per > 0)
RAD_INFO("video %s: step %d/%d, %.2f s/step, eta %.0f s",
j->id.c_str(), e.steps_done, t.steps, per,
per * (double)(t.steps - e.steps_done));
else
RAD_INFO("video %s: step %d/%d", j->id.c_str(), e.steps_done, t.steps);
} else if (e.kind == gen::VideoEvent::LENGTH) {
std::lock_guard<std::mutex> lk(videos_m_);
j->seconds = rad::fmt("%.2f", (double)e.n * info.fps_den / info.fps_num);
@@ -715,6 +1151,10 @@ json Server::video_json(const VideoJob& j) {
o["model"] = j.model;
o["status"] = j.status;
o["progress"] = j.progress;
if (j.steps_total > 0) {
o["steps_done"] = j.steps_done;
o["steps_total"] = j.steps_total;
}
o["created_at"] = j.created_at;
o["completed_at"] = j.completed_at ? json(j.completed_at) : json(nullptr);
o["expires_at"] = j.expires_at ? json(j.expires_at) : json(nullptr);
@@ -722,11 +1162,18 @@ json Server::video_json(const VideoJob& j) {
o["seconds"] = j.seconds;
o["size"] = j.size;
o["seed"] = j.seed;
o["sampler"] = j.sampler == RAD_SAMPLER_RES_MULTISTEP ? "res_multistep" : "euler";
if (j.shift_video > 0.0) o["shift"] = j.shift_video;
if (j.shift_audio > 0.0) o["audio_shift"] = j.shift_audio;
if (j.refine > 0) {
o["latent_upscale"] = j.upscale;
o["upscale_refine_steps"] = j.refine;
o["upscale_from"] = j.upscale_from;
}
if (!j.refs.empty()) {
o["task"] = j.task;
o["refs"] = j.refs;
}
if (j.guided) {
static const char* kGuide[8] = {
"guidance_scale", "stg_scale", "modality_scale", "guidance_rescale",