Archived
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:
commit
a1a75da7e9
415 files changed
+78837
-5310
No files matched your search
+522
-298
@@ -2,6 +2,7 @@
|
||||
* waits on, and a request's shape and schedule (video.h). */
|
||||
#include "video_driver.h"
|
||||
#include "rad_internal.h"
|
||||
#include "rad_plugin.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
@@ -78,7 +79,7 @@ VideoShape video_shape(const RadVideoGenDecl& d, int32_t width, int32_t height,
|
||||
if (r2 > den || (r2 == den && (q & 1))) ++q;
|
||||
s.n_audio = (int32_t)q;
|
||||
s.video_rows = (int64_t)s.lat_frames * (s.lat_h / d.patch) * (s.lat_w / d.patch);
|
||||
s.audio_rows = d.audio_channels * (int64_t)s.n_audio;
|
||||
s.audio_rows = (d.audio_streams > 0 ? d.audio_streams : d.audio_channels) * (int64_t)s.n_audio;
|
||||
return s;
|
||||
}
|
||||
|
||||
@@ -123,12 +124,51 @@ static std::vector<float> rung_sigmas(double shift, const double* rungs, int32_t
|
||||
return s;
|
||||
}
|
||||
|
||||
VideoSchedule video_schedule(const RadVideoGenDecl& d, int32_t steps, double shift_video,
|
||||
double shift_audio) {
|
||||
/* A request's shift overrides the declaration's per stream; 0 keeps the trained value. */
|
||||
/* Form 1 (RadVideoGenDecl::sched_kind): FlowMatchEulerDiscreteScheduler.set_timesteps with
|
||||
* LTX-2's sigmas. numpy's linspace in f64 (i * step + start, the last exactly the stop) taken to
|
||||
* f32; the shift and the terminal stretch in f32, as numpy computes on an f32 array with Python
|
||||
* scalars. */
|
||||
static std::vector<float> flow_sigmas(const RadVideoGenDecl& d, int32_t n, int64_t tokens) {
|
||||
std::vector<float> s((size_t)n);
|
||||
const double stop = 1.0 / n, step = n > 1 ? (stop - 1.0) / (n - 1) : 0.0;
|
||||
for (int32_t i = 0; i < n; ++i) s[(size_t)i] = (float)(i == n - 1 && n > 1 ? stop : i * step + 1.0);
|
||||
if (d.dyn_shift) {
|
||||
const double m = (d.max_shift - d.base_shift) / (double)(d.max_seq - d.base_seq);
|
||||
const double mu = (double)tokens * m + (d.base_shift - m * (double)d.base_seq);
|
||||
const float e = (float)std::exp(mu);
|
||||
for (float& v : s) v = e / (e + (1.0f / v - 1.0f));
|
||||
}
|
||||
if (d.shift_terminal > 0.0) {
|
||||
const float sf = (1.0f - s.back()) / (float)(1.0 - d.shift_terminal);
|
||||
for (float& v : s) v = 1.0f - (1.0f - v) / sf;
|
||||
}
|
||||
s.push_back(0.0f);
|
||||
return s;
|
||||
}
|
||||
|
||||
VideoSchedule video_schedule(const RadVideoGenDecl& d, int32_t steps, int64_t video_tokens,
|
||||
bool refine, double shift_video, double shift_audio) {
|
||||
/* A request's shift overrides the declaration's per stream; 0 keeps the trained value. Form 1
|
||||
* ignores both: its shift is the declaration's, dynamic or terminal. */
|
||||
const double sv = shift_video > 0.0 ? shift_video : d.shift_video;
|
||||
const double sa = shift_audio > 0.0 ? shift_audio : d.shift_audio;
|
||||
VideoSchedule v;
|
||||
if (d.sched_kind == 1) {
|
||||
if (d.n_rungs > 0) {
|
||||
if (steps != d.n_rungs) return v;
|
||||
for (int32_t i = 0; i < d.n_rungs; ++i) v.sig_v.push_back((float)d.rungs[i]);
|
||||
if (refine)
|
||||
for (int32_t i = 0; i < d.n_refine_rungs; ++i)
|
||||
v.sig_v.push_back((float)d.refine_rungs[i]);
|
||||
v.sig_v.push_back(0.0f);
|
||||
} else {
|
||||
if (steps < 1) return v;
|
||||
v.sig_v = flow_sigmas(d, steps, video_tokens);
|
||||
}
|
||||
v.sig_a = v.sig_v;
|
||||
v.steps = (int32_t)v.sig_v.size() - 1;
|
||||
return v;
|
||||
}
|
||||
if (d.n_rungs > 0) {
|
||||
if (steps != d.n_rungs + 1) return v;
|
||||
v.sig_v = rung_sigmas(sv, d.rungs, d.n_rungs);
|
||||
@@ -163,10 +203,12 @@ int64_t align_up(int64_t v) { return (v + kAlign - 1) / kAlign * kAlign; }
|
||||
* vision encoder: the media regions then hold nothing. */
|
||||
int64_t carve(const RadVideoGenDecl& d, int64_t max_tok, int64_t ep, int64_t pd,
|
||||
int64_t (*bytes)[2], int n) {
|
||||
/* A guided model's VCOND carries the negative prompt after the positive. */
|
||||
const int64_t prompts = d.guidance ? 2 : 1;
|
||||
const int64_t want[] = {
|
||||
d.max_text * 4, /* R_TOK */
|
||||
d.max_text * 4, /* R_POS */
|
||||
2 * 4, /* R_CU */
|
||||
prompts * d.max_text * 4, /* R_TOK */
|
||||
prompts * d.max_text * 4, /* R_POS */
|
||||
3 * 4, /* R_CU */
|
||||
3 * max_tok * 4, /* R_ROPE: a step's rotary positions, a conditioning's [3,T] M-RoPE */
|
||||
RAD_VGEOM_N * 4, /* R_GEOM */
|
||||
16 * 4, /* R_SCHED: the Euler scalars [0..5], then the res_multistep
|
||||
@@ -190,6 +232,8 @@ int64_t carve(const RadVideoGenDecl& d, int64_t max_tok, int64_t ep, int64_t pd,
|
||||
d.max_enc_samples * d.audio_channels * 4, /* R_ENCSM: [ch,n] f32 */
|
||||
25 * 4, /* R_ENCG: rad_arch_encode_geom's 25 i32 (the video geom) */
|
||||
kSegCap * 4, /* R_MOD: the merged modulation-group boundaries (<= R_SEG) */
|
||||
d.image_cond ? d.max_area * 3 * 2 : 0, /* R_IMG: a first frame, bf16 */
|
||||
8 * 4, /* R_GUIDE */
|
||||
};
|
||||
int64_t off = 0;
|
||||
for (int r = 0; r < n; ++r) {
|
||||
@@ -252,9 +296,17 @@ int VideoDriver::init(const Program& prog, PositionsFn positions,
|
||||
"video generator must", name_.c_str());
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
if (max_tok_ < decl_.max_text) {
|
||||
RAD_ERR("video generator '%s': a pass carries %lld rows and a prompt may be %lld tokens",
|
||||
name_.c_str(), (long long)max_tok_, (long long)decl_.max_text);
|
||||
decoder_names_ = prog.vgen_str.decoders;
|
||||
decoder_area_ = prog.vgen_str.decoder_max_area;
|
||||
refine_rungs_ = prog.vgen_str.refine_rungs;
|
||||
decl_.refine_rungs = refine_rungs_.empty() ? nullptr : refine_rungs_.data();
|
||||
default_negative_ = prog.vgen_str.default_negative;
|
||||
decl_.default_negative = default_negative_.c_str();
|
||||
const int64_t prompts = decl_.guidance ? 2 : 1;
|
||||
if (max_tok_ < prompts * decl_.max_text) {
|
||||
RAD_ERR("video generator '%s': a pass carries %lld rows and %s may be %lld tokens",
|
||||
name_.c_str(), (long long)max_tok_, prompts == 2 ? "a prompt and its negative" :
|
||||
"a prompt", (long long)(prompts * decl_.max_text));
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
int64_t b[R_N][2];
|
||||
@@ -268,7 +320,8 @@ int VideoDriver::init(const Program& prog, PositionsFn positions,
|
||||
host_ = static_cast<char*>(rad_dev_alloc(bytes_, RAD_MEM_HOST_PINNED));
|
||||
frames_host_ = static_cast<uint8_t*>(rad_dev_alloc(frames_bytes_, RAD_MEM_HOST_PINNED));
|
||||
audio_host_ = static_cast<float*>(rad_dev_alloc(audio_bytes_, RAD_MEM_HOST_PINNED));
|
||||
if (!host_ || !frames_host_ || !audio_host_) {
|
||||
duration_host_ = static_cast<float*>(rad_dev_alloc(64, RAD_MEM_HOST_PINNED));
|
||||
if (!host_ || !frames_host_ || !audio_host_ || !duration_host_) {
|
||||
RAD_ERR("video generator: no pinned host memory for the pass staging and outputs (%lld + "
|
||||
"%lld + %lld bytes)", (long long)bytes_, (long long)frames_bytes_,
|
||||
(long long)audio_bytes_);
|
||||
@@ -362,6 +415,8 @@ void VideoDriver::fini() {
|
||||
if (audio_host_) rad_dev_free(audio_host_, RAD_MEM_HOST_PINNED);
|
||||
if (enc_dbg_host_) rad_dev_free(enc_dbg_host_, RAD_MEM_HOST_PINNED);
|
||||
if (dit_host_) rad_dev_free(dit_host_, RAD_MEM_HOST_PINNED);
|
||||
if (duration_host_) rad_dev_free(duration_host_, RAD_MEM_HOST_PINNED);
|
||||
duration_host_ = nullptr;
|
||||
host_ = nullptr;
|
||||
frames_host_ = nullptr;
|
||||
audio_host_ = nullptr;
|
||||
@@ -370,6 +425,358 @@ void VideoDriver::fini() {
|
||||
}
|
||||
|
||||
/* ================================================================== requests */
|
||||
/* A length head's prediction (RadVideoGenDecl::duration_out) in chunks: diffusers'
|
||||
* LTX2DurationHead.predict_num_frames on the chunk grid -- the seconds clamped to the declared
|
||||
* bounds, rounded to frames, floored to the grid and lifted one chunk when that falls under the
|
||||
* minimum -- then held to min_chunks..max_chunks. `note` says what was clamped. */
|
||||
int32_t VideoDriver::predicted_chunks(double log_seconds, std::string* note) const {
|
||||
const RadVideoGenDecl& d = decl_;
|
||||
const double fps = (double)d.fps_num / (double)d.fps_den;
|
||||
float e = std::exp((float)log_seconds);
|
||||
if (d.duration_dtype == RAD_BF16) e = rad_bf16_to_f32(rad_f32_to_bf16(e));
|
||||
const double sec = e;
|
||||
auto round_even = [](double v) { return (int64_t)std::nearbyint(v); };
|
||||
const int64_t lo = std::max<int64_t>(1, round_even(d.duration_min * fps));
|
||||
const int64_t hi = round_even(d.duration_max * fps);
|
||||
const int64_t want = std::isfinite(sec) ? round_even(sec * fps) : hi;
|
||||
const int64_t frames = std::max(lo, std::min(want, hi));
|
||||
int64_t n = std::max<int64_t>(0, (frames - d.frames_tail) / d.frames_per_chunk);
|
||||
if (d.frames_per_chunk * n + d.frames_tail < lo &&
|
||||
d.frames_per_chunk * (n + 1) + d.frames_tail <= hi)
|
||||
++n;
|
||||
std::string what = fmt(": %.3f s predicted", sec);
|
||||
if (!(sec >= d.duration_min && sec <= d.duration_max))
|
||||
what += fmt(", held to %g..%g s", d.duration_min, d.duration_max);
|
||||
if (n < d.min_chunks || n > d.max_chunks) {
|
||||
n = std::max<int64_t>(d.min_chunks, std::min<int64_t>(n, d.max_chunks));
|
||||
what += fmt(", within the %lld..%lld frames this model makes",
|
||||
(long long)(d.frames_per_chunk * d.min_chunks + d.frames_tail),
|
||||
(long long)(d.frames_per_chunk * d.max_chunks + d.frames_tail));
|
||||
}
|
||||
if (note) *note = what;
|
||||
return (int32_t)n;
|
||||
}
|
||||
|
||||
/* The request's shape at `chunks` long: its geometry, rows and positions, its schedule, the
|
||||
* upscale's split and what its ticket says it makes. Run at submit, and again for a request whose
|
||||
* length the model predicts once its VCOND pass has written the prediction. */
|
||||
int VideoDriver::shape_job(Job& j, const VideoRequest& r, int32_t chunks, int32_t steps,
|
||||
std::string* why) {
|
||||
const RadVideoGenDecl& d = decl_;
|
||||
auto no = [&](const std::string& w) {
|
||||
if (why) *why = w;
|
||||
return RAD_E_INVAL;
|
||||
};
|
||||
const int64_t T = (int64_t)r.tokens.size();
|
||||
const VideoShape s = video_shape(d, r.width, r.height, chunks);
|
||||
j.geom.n_text = (int32_t)(d.step_text_rows > 0 ? d.step_text_rows : T);
|
||||
j.geom.frames = s.frames;
|
||||
j.geom.lat_frames = s.lat_frames;
|
||||
j.geom.lat_h = s.lat_h;
|
||||
j.geom.lat_w = s.lat_w;
|
||||
j.geom.n_audio = s.n_audio;
|
||||
/* The step's row count and its positions. A reference-conditioned step widens them; the
|
||||
* encode queue and the pack's table are built first because the layout depends on them. */
|
||||
j.ref_tab.clear();
|
||||
j.enc_q.clear();
|
||||
/* The pack's reference table (RadVideoGenBatch::ref_tab): every reference the DiT conditions
|
||||
* on, in presentation order, in ITS OWN latent grid -- {kind, slot, h, w, frames, audio} six
|
||||
* words after the reference and keyframe counts. `kind` is a pack Ref2VARefKind (picture 0,
|
||||
* standalone audio 1, video 2), the server's VIDEO_AUDIO being folded into its video's `audio`
|
||||
* word rather than a row of its own. Each reference's slot is the one its encode pass folds its
|
||||
* latents into, and its latent counts come from the encoder's own geometry, not hand arithmetic:
|
||||
* a picture is one latent frame (its grid the encoder's sides over the VAE ratio); a video's
|
||||
* frames and its soundtrack's are the Vencode/Aencode latent counts rad_arch_encode_geom gives
|
||||
* (a video's are the same before and after the encode pass clip-pads its short final clip). The
|
||||
* table must exist before the step layout is decided, because when the denoiser consumes it the
|
||||
* reference rows are part of that layout. Endpoint keyframes (a first and/or last frame pinned
|
||||
* at the output's ends) are appended after the reference rows as {frame, video_t, audio_t}
|
||||
* triples; a request carries either references or keyframes, never both. */
|
||||
if (d.encodes) {
|
||||
bool need_geom = false;
|
||||
for (const VideoRefMedia& m : r.refs)
|
||||
if (m.kind != VideoRefMedia::IMAGE && m.kind != VideoRefMedia::FIRST_FRAME &&
|
||||
m.kind != VideoRefMedia::LAST_FRAME) need_geom = true;
|
||||
if (need_geom && !enc_geom_)
|
||||
return no("the request carries a reference video or audio, but this plugin exports no "
|
||||
"rad_arch_encode_geom, so their encoder geometry cannot be sized");
|
||||
int32_t eg[25];
|
||||
/* A reference's latent frame counts, straight from the encoder's geometry. */
|
||||
auto video_lat = [&](const VideoRefMedia& m) -> int64_t {
|
||||
int64_t in[4] = { m.enc_frames, m.enc_h, m.enc_w, 3 };
|
||||
return enc_geom_(RAD_GEN_PASS_VENCODE, in, eg, 25) < 0 ? -1 : (int64_t)eg[24];
|
||||
};
|
||||
auto audio_lat = [&](const VideoRefMedia& m) -> int64_t {
|
||||
int64_t in[4] = { m.enc_n, 0, 0, m.enc_channels };
|
||||
return enc_geom_(RAD_GEN_PASS_AENCODE, in, eg, 25) < 0 ? -1 : (int64_t)eg[12];
|
||||
};
|
||||
/* Pictures and videos share a side-grid check against the VAE ratio. */
|
||||
auto bad_side = [&](const VideoRefMedia& m) {
|
||||
return d.enc_ratio < 1 || m.enc_h < d.enc_ratio || m.enc_w < d.enc_ratio ||
|
||||
m.enc_h % d.enc_ratio || m.enc_w % d.enc_ratio;
|
||||
};
|
||||
for (size_t i = 0; i < r.refs.size(); ++i) {
|
||||
const VideoRefMedia& m = r.refs[i];
|
||||
if (m.kind == VideoRefMedia::IMAGE) {
|
||||
if (m.enc_pixels.empty())
|
||||
return no("'" + m.name + "' was not prepared for the video encoder");
|
||||
if (m.index < 1)
|
||||
return no("'" + m.name + "' has no slot in the conditioning");
|
||||
if (bad_side(m))
|
||||
return no("'" + m.name + "' has encoder sides (" + std::to_string(m.enc_h) +
|
||||
"x" + std::to_string(m.enc_w) + ") the VAE grid of " +
|
||||
std::to_string(d.enc_ratio) + " does not divide");
|
||||
j.enc_q.push_back({RAD_ENC_IMAGE, m.index, (int32_t)i});
|
||||
if (j.ref_tab.empty()) j.ref_tab.assign(2, 0); /* [n_refs, n_keys] */
|
||||
++j.ref_tab[0];
|
||||
j.ref_tab.insert(j.ref_tab.end(),
|
||||
{0 /*a picture*/, m.index, (int32_t)(m.enc_h / d.enc_ratio),
|
||||
(int32_t)(m.enc_w / d.enc_ratio), 1 /*one latent frame*/,
|
||||
0 /*no soundtrack*/});
|
||||
} else if (m.kind == VideoRefMedia::VIDEO) {
|
||||
if (m.enc_pixels.empty())
|
||||
return no("'" + m.name + "' was not prepared for the video encoder");
|
||||
if (m.index < 1 || m.index > 3)
|
||||
return no("'" + m.name + "' has no video slot (1..3)");
|
||||
if (bad_side(m))
|
||||
return no("'" + m.name + "' has encoder sides (" + std::to_string(m.enc_h) +
|
||||
"x" + std::to_string(m.enc_w) + ") the VAE grid of " +
|
||||
std::to_string(d.enc_ratio) + " does not divide");
|
||||
/* A soundtrack given for this video (ref_video_audio_N) is folded into THIS row's
|
||||
* `audio` word, not a row of its own; the pack lays its block right before the
|
||||
* video's and the scatter reads the two back as the pair they are. It is encoded at
|
||||
* the VIDEO's slot, since aenc_slot_row numbers a soundtrack by its video. */
|
||||
int64_t a_t = 0;
|
||||
for (size_t k = 0; k < r.refs.size(); ++k) {
|
||||
const VideoRefMedia& a = r.refs[k];
|
||||
if (a.kind != VideoRefMedia::VIDEO_AUDIO || a.index != m.index) continue;
|
||||
if (a.enc_samples.empty() || a.enc_n < 1 || a.enc_channels < 1)
|
||||
return no("'" + a.name + "' (the soundtrack of '" + m.name +
|
||||
"') was not prepared for the audio encoder");
|
||||
a_t = audio_lat(a);
|
||||
if (a_t < 1)
|
||||
return no("the audio encoder refuses the soundtrack '" + a.name + "'");
|
||||
j.enc_q.push_back({RAD_ENC_VAUDIO, m.index, (int32_t)k});
|
||||
}
|
||||
const int64_t v_t = video_lat(m);
|
||||
if (v_t < 1) return no("the video encoder refuses '" + m.name + "'");
|
||||
j.enc_q.push_back({RAD_ENC_VIDEO, m.index, (int32_t)i});
|
||||
if (j.ref_tab.empty()) j.ref_tab.assign(2, 0);
|
||||
++j.ref_tab[0];
|
||||
j.ref_tab.insert(j.ref_tab.end(),
|
||||
{2 /*a video*/, m.index, (int32_t)(m.enc_h / d.enc_ratio),
|
||||
(int32_t)(m.enc_w / d.enc_ratio), (int32_t)v_t, (int32_t)a_t});
|
||||
} else if (m.kind == VideoRefMedia::AUDIO) {
|
||||
if (m.enc_samples.empty() || m.enc_n < 1 || m.enc_channels < 1)
|
||||
return no("'" + m.name + "' was not prepared for the audio encoder");
|
||||
if (m.index < 1 || m.index > 3)
|
||||
return no("'" + m.name + "' has no audio slot (1..3)");
|
||||
const int64_t a_t = audio_lat(m);
|
||||
if (a_t < 1) return no("the audio encoder refuses '" + m.name + "'");
|
||||
j.enc_q.push_back({RAD_ENC_AUDIO, m.index, (int32_t)i});
|
||||
if (j.ref_tab.empty()) j.ref_tab.assign(2, 0);
|
||||
++j.ref_tab[0];
|
||||
j.ref_tab.insert(j.ref_tab.end(),
|
||||
{1 /*a standalone audio*/, m.index, 0, 0, 0, (int32_t)a_t});
|
||||
}
|
||||
/* VIDEO_AUDIO rows are folded into their video above, so they take no row here. */
|
||||
}
|
||||
/* Endpoint keyframes: each FIRST_FRAME/LAST_FRAME the server resized to the target canvas
|
||||
* and staged in enc_pixels folds into the pack's KEYFRAME table -- not a reference row. The
|
||||
* DiT reads its latent rows PINNED at that frame's moment on the target timeline (denoised
|
||||
* never): the first pins output frame 0, the last the final frame, and the gap between is
|
||||
* diffused. The key triple is {pixel frame, video_t, audio_t}; a picture anchor is video-only
|
||||
* (video_t = one latent frame, audio_t = 0), and its pixel frame is 0 or frames-1 -- the same
|
||||
* resolved frame index the reference's MiniMaxH3ImageToVideo assigns. Its latents ride the
|
||||
* image slot its keyframe ORDER names (1, then 2): a keyframe request carries no references,
|
||||
* so those slots are free, and ref_parts -- and the scatter -- read them back by the same
|
||||
* order. Keyframe rows are appended after every reference row, which is where ref_pack_of
|
||||
* reads the table's key block. */
|
||||
for (size_t i = 0; i < r.refs.size(); ++i) {
|
||||
const VideoRefMedia& m = r.refs[i];
|
||||
const bool first = m.kind == VideoRefMedia::FIRST_FRAME;
|
||||
if (!first && m.kind != VideoRefMedia::LAST_FRAME) continue;
|
||||
if (m.enc_pixels.empty())
|
||||
return no("'" + m.name + "' was not prepared for the video encoder");
|
||||
if (bad_side(m))
|
||||
return no("'" + m.name + "' has encoder sides (" + std::to_string(m.enc_h) +
|
||||
"x" + std::to_string(m.enc_w) + ") the VAE grid of " +
|
||||
std::to_string(d.enc_ratio) + " does not divide");
|
||||
if (j.ref_tab.empty()) j.ref_tab.assign(2, 0); /* [n_refs, n_keys] */
|
||||
const int32_t slot = ++j.ref_tab[1]; /* keyframe order -> image slot 1, 2 */
|
||||
j.enc_q.push_back({RAD_ENC_IMAGE, slot, (int32_t)i});
|
||||
j.ref_tab.insert(j.ref_tab.end(),
|
||||
{first ? 0 : s.frames - 1, 1 /*one latent frame*/, 0 /*no soundtrack*/});
|
||||
}
|
||||
}
|
||||
/* The step's packed sequence. When the denoiser consumes the reference table the pack is the
|
||||
* only source of its rows, segment boundaries and positions -- the reference rows sit between
|
||||
* the text and the target streams, so the plain `text + audio + video` arithmetic and the plain
|
||||
* position layout no longer describe it. rad_arch_video_pack returns the total rows and, when
|
||||
* roomy, the boundaries and the full [3, rows] planar RoPE in one call. Otherwise the plain
|
||||
* layout is computed exactly as before, and a staged reference table (if any) rides inert. */
|
||||
const int64_t nd = positions_(RAD_VPOS_DECODE, &j.geom, nullptr, 0);
|
||||
if (nd <= 0 || nd > max_tok_)
|
||||
return no("the model lays this video's decoder in " + std::to_string(nd) + " rows, which "
|
||||
"this server cannot run");
|
||||
j.dec_rows = nd;
|
||||
if (d.ref_rows && !j.ref_tab.empty()) {
|
||||
if (r.upscale > 1.0)
|
||||
return no("reference conditioning does not yet combine with latent upscale; ask for "
|
||||
"the size you want without latent_upscale");
|
||||
if (!pack_)
|
||||
return no("the generator says it conditions on references but exports no "
|
||||
"rad_arch_video_pack");
|
||||
const int64_t pr = pack_(&j.geom, j.ref_tab.data(), nullptr, 0, nullptr, 0);
|
||||
if (pr <= 0)
|
||||
return no("the model refused the reference conditioning layout");
|
||||
if (pr > max_tok_)
|
||||
return no("a conditioned step is " + std::to_string(pr) + " rows and a pass carries " +
|
||||
std::to_string(max_tok_) + "; ask for a shorter or smaller video, or fewer "
|
||||
"references");
|
||||
j.seg.assign((size_t)kSegCap, 0);
|
||||
j.pos.assign((size_t)(3 * pr), 0.0f);
|
||||
if (pack_(&j.geom, j.ref_tab.data(), j.seg.data(), kSegCap, j.pos.data(), 3 * pr) != pr)
|
||||
return no("the model could not lay the reference conditioning out");
|
||||
int nseg = -1;
|
||||
for (int k = 0; k < kSegCap; ++k)
|
||||
if (j.seg[(size_t)k] == (int32_t)pr) { nseg = k + 1; break; } /* video grid ends here */
|
||||
if (nseg < 0 || nseg > kSegCap)
|
||||
return no("the reference conditioning layout has no closing segment boundary");
|
||||
j.seg.resize((size_t)nseg);
|
||||
j.nseg = nseg;
|
||||
/* The modulation groups. A modulation cell is fixed by a segment's kind, so the denoiser
|
||||
* merges runs of consecutive same-cell segments and pitches its fixed-width table by the
|
||||
* GROUP count, not the reference count -- which is how nine reference photos (four groups:
|
||||
* prompt, all-photos, soundtrack, video) ride a table the fine layout (twelve segments)
|
||||
* would overrun. The plugin merges for us (rad_arch_video_mod_segs); one that exports none
|
||||
* merges nothing, so the group table is the fine one and the older per-segment ceiling
|
||||
* applies. Refuse over-wide merges here, at submit, where a clean answer is still possible. */
|
||||
int nmod = -1;
|
||||
if (mod_segs_) {
|
||||
j.mod_seg.assign((size_t)kSegCap, 0);
|
||||
nmod = (int)mod_segs_(&j.geom, j.ref_tab.data(), j.mod_seg.data(), kSegCap);
|
||||
if (nmod < 0 || nmod > kSegCap)
|
||||
return no("the model could not merge the reference conditioning into its "
|
||||
"modulation groups");
|
||||
j.mod_seg.resize((size_t)nmod);
|
||||
} else {
|
||||
nmod = nseg; /* nothing merges: the groups are the fine segments */
|
||||
j.mod_seg = j.seg;
|
||||
}
|
||||
j.nmod = nmod;
|
||||
const int ngroups = nmod - 1; /* `mod_seg` is boundaries: one more than the groups */
|
||||
if (d.max_ref_segs > 0 && ngroups > d.max_ref_segs)
|
||||
return no("these references change the conditioning's modulation cell " +
|
||||
std::to_string(ngroups) + " times, but this deployment's modulation table holds " +
|
||||
std::to_string(d.max_ref_segs) + " cells -- ask for references that share their "
|
||||
"modalities and target, or fewer of them");
|
||||
/* `rows` stays the plain text+audio+video count: every non-step pass (encode, decode,
|
||||
* audio, upscale) stages its own segment table and target counts from it and must see the
|
||||
* bytes it sees today. Only build_step runs at the widened `step_rows`. */
|
||||
j.rows = j.geom.n_text + s.audio_rows + s.video_rows;
|
||||
j.step_rows = pr;
|
||||
/* The last segment (the video target) runs [seg[nseg-2], seg[nseg-1]); seg[nseg-1] is the
|
||||
* total rows, so the video start is the boundary before it -- the same one the VSA takes as
|
||||
* its video base (it works in segments, one fewer than the boundaries it is handed). */
|
||||
j.lat_v = pr - j.seg[(size_t)(nseg - 2)];
|
||||
j.lat_a = decl_.audio_channels * (int64_t)j.geom.n_audio;
|
||||
j.ref_wide = true;
|
||||
} else {
|
||||
j.rows = j.geom.n_text + s.audio_rows + s.video_rows;
|
||||
if (j.rows > max_tok_)
|
||||
return no("a step of this video is " + std::to_string(j.rows) + " rows and a pass "
|
||||
"carries " + std::to_string(max_tok_) + "; ask for a shorter or smaller video");
|
||||
const int64_t nr = positions_(RAD_VPOS_STEP, &j.geom, nullptr, 0);
|
||||
if (nr != j.rows)
|
||||
return no("the model lays this video out in " + std::to_string(nr) + " step rows, "
|
||||
"which this server cannot run");
|
||||
j.pos.assign((size_t)(3 * nr), 0.0f);
|
||||
if (positions_(RAD_VPOS_STEP, &j.geom, j.pos.data(), nr) != nr)
|
||||
return no("the model could not lay this video out");
|
||||
j.step_rows = j.rows;
|
||||
}
|
||||
|
||||
/* The schedule follows the upscale's split, which needs the small size first. */
|
||||
const bool upscaling = r.upscale > 1.0;
|
||||
const bool own_refine = upscaling && d.n_refine_rungs > 0;
|
||||
int64_t first_tokens = s.video_rows;
|
||||
if (upscaling) {
|
||||
int32_t sw = 0, sh = 0;
|
||||
video_small_size(d, r.width, r.height, r.upscale, &sw, &sh);
|
||||
first_tokens = video_shape(d, sw, sh, chunks).video_rows;
|
||||
}
|
||||
j.sched = video_schedule(d, steps, first_tokens, own_refine, r.shift_video, r.shift_audio);
|
||||
if (j.sched.steps < 1) return no("the schedule has no steps");
|
||||
int32_t first_w = r.width, first_h = r.height;
|
||||
if (upscaling) {
|
||||
if (!(d.upscale_max > 1.0)) return no("this model has no latent upscaler");
|
||||
if (d.upscale_exact) {
|
||||
const int64_t m = (int64_t)std::llround(d.size_multiple * d.upscale_max);
|
||||
if (r.upscale != d.upscale_max || r.width % m || r.height % m)
|
||||
return no("this model's latent upscaler enlarges by exactly " +
|
||||
fmt("%g", d.upscale_max) + ": latent_upscale must be that, and both "
|
||||
"sides multiples of " + std::to_string(m));
|
||||
}
|
||||
if (r.upscale > d.upscale_max)
|
||||
return no("latent_upscale is at most " + fmt("%g", d.upscale_max) + " for this model");
|
||||
int32_t sw = 0, sh = 0;
|
||||
video_small_size(d, r.width, r.height, r.upscale, &sw, &sh);
|
||||
if (sw == r.width && sh == r.height)
|
||||
return no("latent_upscale " + fmt("%g", r.upscale) + " leaves " +
|
||||
std::to_string(r.width) + "x" + std::to_string(r.height) + " as it is: "
|
||||
"the small size is each side over the factor, rounded to a multiple of " +
|
||||
std::to_string(d.size_multiple));
|
||||
const int32_t evals = j.sched.steps;
|
||||
int32_t refine = r.refine;
|
||||
if (own_refine) {
|
||||
if (refine != 0 && refine != d.n_refine_rungs)
|
||||
return no("upscale_refine_steps must be " + std::to_string(d.n_refine_rungs) +
|
||||
": this model refines the upscaled video over the schedule it was "
|
||||
"trained on");
|
||||
refine = d.n_refine_rungs;
|
||||
}
|
||||
if (refine == 0) refine = std::min<int32_t>(d.upscale_refine, evals - 1);
|
||||
if (refine < 1 || refine >= evals)
|
||||
return no("upscale_refine_steps must be 1.." + std::to_string(evals - 1) + ": the "
|
||||
"schedule takes " + std::to_string(evals) + " evaluations and the small "
|
||||
"size at least one of them");
|
||||
const VideoShape ss = video_shape(d, sw, sh, chunks);
|
||||
j.geom_small = j.geom;
|
||||
j.geom_small.lat_h = ss.lat_h;
|
||||
j.geom_small.lat_w = ss.lat_w;
|
||||
j.rows_small = j.geom.n_text + ss.audio_rows + ss.video_rows;
|
||||
const int64_t ns = positions_(RAD_VPOS_STEP, &j.geom_small, nullptr, 0);
|
||||
if (ns != j.rows_small)
|
||||
return no("the model lays the small size " + std::to_string(sw) + "x" +
|
||||
std::to_string(sh) + " out in " + std::to_string(ns) + " step rows, which "
|
||||
"this server cannot run");
|
||||
j.pos_small.assign((size_t)(3 * ns), 0.0f);
|
||||
if (positions_(RAD_VPOS_STEP, &j.geom_small, j.pos_small.data(), ns) != ns)
|
||||
return no("the model could not lay the small size out");
|
||||
j.split = evals - refine;
|
||||
j.factor = (float)(((double)r.width / sw + (double)r.height / sh) / 2.0);
|
||||
r.ticket->small_width = sw;
|
||||
r.ticket->small_height = sh;
|
||||
r.ticket->refine = refine;
|
||||
first_w = sw;
|
||||
first_h = sh;
|
||||
}
|
||||
if (!r.image.empty()) {
|
||||
if (!d.image_cond) return no("this model cannot start from a picture");
|
||||
if (r.image.size() != (size_t)first_w * first_h * 3)
|
||||
return no("the first frame is not " + std::to_string(first_w) + "x" +
|
||||
std::to_string(first_h) + " RGB");
|
||||
}
|
||||
j.n_chunks = chunks;
|
||||
j.n_samples = (int64_t)s.n_audio * (d.sample_rate / d.audio_rate);
|
||||
VideoTicket& t = *r.ticket;
|
||||
t.frames = s.frames;
|
||||
t.steps = j.sched.steps;
|
||||
t.rows = j.step_rows; /* the conditioned step's real length, references included */
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
int VideoDriver::submit(std::unique_ptr<VideoRequest> r, std::string* why) {
|
||||
if (!r || !r->ticket) return RAD_E_INVAL;
|
||||
const RadVideoGenDecl& d = decl_;
|
||||
@@ -436,7 +843,7 @@ int VideoDriver::submit(std::unique_ptr<VideoRequest> r, std::string* why) {
|
||||
return no("the references are " + std::to_string(n_patch) + " patches; this model "
|
||||
"encodes at most " + std::to_string(max_enc_) + " in one pass");
|
||||
}
|
||||
if (r->chunks < d.min_chunks || r->chunks > d.max_chunks)
|
||||
if (r->chunks != 0 && (r->chunks < d.min_chunks || r->chunks > d.max_chunks))
|
||||
return no("the length must be " + std::to_string(d.min_chunks) + ".." +
|
||||
std::to_string(d.max_chunks) + " chunks");
|
||||
const int64_t W = r->width, H = r->height;
|
||||
@@ -448,287 +855,46 @@ int VideoDriver::submit(std::unique_ptr<VideoRequest> r, std::string* why) {
|
||||
" pixels, the long side at most " + std::to_string(d.max_aspect) +
|
||||
" times the short");
|
||||
const int32_t steps = r->steps > 0 ? r->steps : (int32_t)d.default_steps;
|
||||
if (d.n_rungs > 0 && steps != d.n_rungs + 1)
|
||||
return no("num_inference_steps must be " + std::to_string(d.n_rungs + 1) + ": this model "
|
||||
const bool evals = d.sched_kind == 1; /* form 1 counts evaluations, form 0 grid points */
|
||||
const int32_t rung_steps = evals ? d.n_rungs : d.n_rungs + 1;
|
||||
if (d.n_rungs > 0 && steps != rung_steps)
|
||||
return no("num_inference_steps must be " + std::to_string(rung_steps) + ": this model "
|
||||
"runs the schedule it was trained on, " + std::to_string(d.n_rungs) +
|
||||
" evaluations");
|
||||
if (steps < 2 || steps > d.max_steps)
|
||||
return no("num_inference_steps must be 2.." + std::to_string(d.max_steps) + " (sigma grid "
|
||||
"points, the last one 0)");
|
||||
if (steps < (evals ? 1 : 2) || steps > d.max_steps)
|
||||
return no("num_inference_steps must be " + std::to_string(evals ? 1 : 2) + ".." +
|
||||
std::to_string(d.max_steps) + (evals ? " (model evaluations)" :
|
||||
" (sigma grid points, the last one 0)"));
|
||||
|
||||
auto j = std::make_unique<Job>();
|
||||
const VideoShape s = video_shape(d, r->width, r->height, r->chunks);
|
||||
j->geom.n_text = (int32_t)T;
|
||||
j->geom.frames = s.frames;
|
||||
j->geom.lat_frames = s.lat_frames;
|
||||
j->geom.lat_h = s.lat_h;
|
||||
j->geom.lat_w = s.lat_w;
|
||||
j->geom.n_audio = s.n_audio;
|
||||
/* The pack's reference table (RadVideoGenBatch::ref_tab): every reference the DiT conditions
|
||||
* on, in presentation order, in ITS OWN latent grid -- {kind, slot, h, w, frames, audio} six
|
||||
* words after the reference and keyframe counts. `kind` is a pack Ref2VARefKind (picture 0,
|
||||
* standalone audio 1, video 2), the server's VIDEO_AUDIO being folded into its video's `audio`
|
||||
* word rather than a row of its own. Each reference's slot is the one its encode pass folds its
|
||||
* latents into, and its latent counts come from the encoder's own geometry, not hand arithmetic:
|
||||
* a picture is one latent frame (its grid the encoder's sides over the VAE ratio); a video's
|
||||
* frames and its soundtrack's are the Vencode/Aencode latent counts rad_arch_encode_geom gives
|
||||
* (a video's are the same before and after the encode pass clip-pads its short final clip). The
|
||||
* table must exist before the step layout is decided, because when the denoiser consumes it the
|
||||
* reference rows are part of that layout. Endpoint keyframes (a first and/or last frame pinned
|
||||
* at the output's ends) are appended after the reference rows as {frame, video_t, audio_t}
|
||||
* triples; a request carries either references or keyframes, never both. */
|
||||
if (d.encodes) {
|
||||
bool need_geom = false;
|
||||
for (const VideoRefMedia& m : r->refs)
|
||||
if (m.kind != VideoRefMedia::IMAGE && m.kind != VideoRefMedia::FIRST_FRAME &&
|
||||
m.kind != VideoRefMedia::LAST_FRAME) need_geom = true;
|
||||
if (need_geom && !enc_geom_)
|
||||
return no("the request carries a reference video or audio, but this plugin exports no "
|
||||
"rad_arch_encode_geom, so their encoder geometry cannot be sized");
|
||||
int32_t eg[25];
|
||||
/* A reference's latent frame counts, straight from the encoder's geometry. */
|
||||
auto video_lat = [&](const VideoRefMedia& m) -> int64_t {
|
||||
int64_t in[4] = { m.enc_frames, m.enc_h, m.enc_w, 3 };
|
||||
return enc_geom_(RAD_GEN_PASS_VENCODE, in, eg, 25) < 0 ? -1 : (int64_t)eg[24];
|
||||
};
|
||||
auto audio_lat = [&](const VideoRefMedia& m) -> int64_t {
|
||||
int64_t in[4] = { m.enc_n, 0, 0, m.enc_channels };
|
||||
return enc_geom_(RAD_GEN_PASS_AENCODE, in, eg, 25) < 0 ? -1 : (int64_t)eg[12];
|
||||
};
|
||||
/* Pictures and videos share a side-grid check against the VAE ratio. */
|
||||
auto bad_side = [&](const VideoRefMedia& m) {
|
||||
return d.enc_ratio < 1 || m.enc_h < d.enc_ratio || m.enc_w < d.enc_ratio ||
|
||||
m.enc_h % d.enc_ratio || m.enc_w % d.enc_ratio;
|
||||
};
|
||||
for (size_t i = 0; i < r->refs.size(); ++i) {
|
||||
const VideoRefMedia& m = r->refs[i];
|
||||
if (m.kind == VideoRefMedia::IMAGE) {
|
||||
if (m.enc_pixels.empty())
|
||||
return no("'" + m.name + "' was not prepared for the video encoder");
|
||||
if (m.index < 1)
|
||||
return no("'" + m.name + "' has no slot in the conditioning");
|
||||
if (bad_side(m))
|
||||
return no("'" + m.name + "' has encoder sides (" + std::to_string(m.enc_h) +
|
||||
"x" + std::to_string(m.enc_w) + ") the VAE grid of " +
|
||||
std::to_string(d.enc_ratio) + " does not divide");
|
||||
j->enc_q.push_back({RAD_ENC_IMAGE, m.index, (int32_t)i});
|
||||
if (j->ref_tab.empty()) j->ref_tab.assign(2, 0); /* [n_refs, n_keys] */
|
||||
++j->ref_tab[0];
|
||||
j->ref_tab.insert(j->ref_tab.end(),
|
||||
{0 /*a picture*/, m.index, (int32_t)(m.enc_h / d.enc_ratio),
|
||||
(int32_t)(m.enc_w / d.enc_ratio), 1 /*one latent frame*/,
|
||||
0 /*no soundtrack*/});
|
||||
} else if (m.kind == VideoRefMedia::VIDEO) {
|
||||
if (m.enc_pixels.empty())
|
||||
return no("'" + m.name + "' was not prepared for the video encoder");
|
||||
if (m.index < 1 || m.index > 3)
|
||||
return no("'" + m.name + "' has no video slot (1..3)");
|
||||
if (bad_side(m))
|
||||
return no("'" + m.name + "' has encoder sides (" + std::to_string(m.enc_h) +
|
||||
"x" + std::to_string(m.enc_w) + ") the VAE grid of " +
|
||||
std::to_string(d.enc_ratio) + " does not divide");
|
||||
/* A soundtrack given for this video (ref_video_audio_N) is folded into THIS row's
|
||||
* `audio` word, not a row of its own; the pack lays its block right before the
|
||||
* video's and the scatter reads the two back as the pair they are. It is encoded at
|
||||
* the VIDEO's slot, since aenc_slot_row numbers a soundtrack by its video. */
|
||||
int64_t a_t = 0;
|
||||
for (size_t k = 0; k < r->refs.size(); ++k) {
|
||||
const VideoRefMedia& a = r->refs[k];
|
||||
if (a.kind != VideoRefMedia::VIDEO_AUDIO || a.index != m.index) continue;
|
||||
if (a.enc_samples.empty() || a.enc_n < 1 || a.enc_channels < 1)
|
||||
return no("'" + a.name + "' (the soundtrack of '" + m.name +
|
||||
"') was not prepared for the audio encoder");
|
||||
a_t = audio_lat(a);
|
||||
if (a_t < 1)
|
||||
return no("the audio encoder refuses the soundtrack '" + a.name + "'");
|
||||
j->enc_q.push_back({RAD_ENC_VAUDIO, m.index, (int32_t)k});
|
||||
}
|
||||
const int64_t v_t = video_lat(m);
|
||||
if (v_t < 1) return no("the video encoder refuses '" + m.name + "'");
|
||||
j->enc_q.push_back({RAD_ENC_VIDEO, m.index, (int32_t)i});
|
||||
if (j->ref_tab.empty()) j->ref_tab.assign(2, 0);
|
||||
++j->ref_tab[0];
|
||||
j->ref_tab.insert(j->ref_tab.end(),
|
||||
{2 /*a video*/, m.index, (int32_t)(m.enc_h / d.enc_ratio),
|
||||
(int32_t)(m.enc_w / d.enc_ratio), (int32_t)v_t, (int32_t)a_t});
|
||||
} else if (m.kind == VideoRefMedia::AUDIO) {
|
||||
if (m.enc_samples.empty() || m.enc_n < 1 || m.enc_channels < 1)
|
||||
return no("'" + m.name + "' was not prepared for the audio encoder");
|
||||
if (m.index < 1 || m.index > 3)
|
||||
return no("'" + m.name + "' has no audio slot (1..3)");
|
||||
const int64_t a_t = audio_lat(m);
|
||||
if (a_t < 1) return no("the audio encoder refuses '" + m.name + "'");
|
||||
j->enc_q.push_back({RAD_ENC_AUDIO, m.index, (int32_t)i});
|
||||
if (j->ref_tab.empty()) j->ref_tab.assign(2, 0);
|
||||
++j->ref_tab[0];
|
||||
j->ref_tab.insert(j->ref_tab.end(),
|
||||
{1 /*a standalone audio*/, m.index, 0, 0, 0, (int32_t)a_t});
|
||||
}
|
||||
/* VIDEO_AUDIO rows are folded into their video above, so they take no row here. */
|
||||
}
|
||||
/* Endpoint keyframes: each FIRST_FRAME/LAST_FRAME the server resized to the target canvas
|
||||
* and staged in enc_pixels folds into the pack's KEYFRAME table -- not a reference row. The
|
||||
* DiT reads its latent rows PINNED at that frame's moment on the target timeline (denoised
|
||||
* never): the first pins output frame 0, the last the final frame, and the gap between is
|
||||
* diffused. The key triple is {pixel frame, video_t, audio_t}; a picture anchor is video-only
|
||||
* (video_t = one latent frame, audio_t = 0), and its pixel frame is 0 or frames-1 -- the same
|
||||
* resolved frame index the reference's MiniMaxH3ImageToVideo assigns. Its latents ride the
|
||||
* image slot its keyframe ORDER names (1, then 2): a keyframe request carries no references,
|
||||
* so those slots are free, and ref_parts -- and the scatter -- read them back by the same
|
||||
* order. Keyframe rows are appended after every reference row, which is where ref_pack_of
|
||||
* reads the table's key block. */
|
||||
for (size_t i = 0; i < r->refs.size(); ++i) {
|
||||
const VideoRefMedia& m = r->refs[i];
|
||||
const bool first = m.kind == VideoRefMedia::FIRST_FRAME;
|
||||
if (!first && m.kind != VideoRefMedia::LAST_FRAME) continue;
|
||||
if (m.enc_pixels.empty())
|
||||
return no("'" + m.name + "' was not prepared for the video encoder");
|
||||
if (bad_side(m))
|
||||
return no("'" + m.name + "' has encoder sides (" + std::to_string(m.enc_h) +
|
||||
"x" + std::to_string(m.enc_w) + ") the VAE grid of " +
|
||||
std::to_string(d.enc_ratio) + " does not divide");
|
||||
if (j->ref_tab.empty()) j->ref_tab.assign(2, 0); /* [n_refs, n_keys] */
|
||||
const int32_t slot = ++j->ref_tab[1]; /* keyframe order -> image slot 1, 2 */
|
||||
j->enc_q.push_back({RAD_ENC_IMAGE, slot, (int32_t)i});
|
||||
j->ref_tab.insert(j->ref_tab.end(),
|
||||
{first ? 0 : s.frames - 1, 1 /*one latent frame*/, 0 /*no soundtrack*/});
|
||||
}
|
||||
/* Guidance: the request's terms over the model's defaults; a negative prompt only when a
|
||||
* scale asks for the unconditional branch. */
|
||||
bool named = r->has_neg;
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
named = named || !std::isnan(r->guidance[i]);
|
||||
j->guidance[i] = (float)(std::isnan(r->guidance[i]) ? d.guidance_default[i] : r->guidance[i]);
|
||||
if (!(j->guidance[i] >= 0.0f) || !std::isfinite(j->guidance[i]))
|
||||
return no("guidance terms must be finite and not negative");
|
||||
}
|
||||
/* The step's packed sequence. When the denoiser consumes the reference table the pack is the
|
||||
* only source of its rows, segment boundaries and positions -- the reference rows sit between
|
||||
* the text and the target streams, so the plain `text + audio + video` arithmetic and the plain
|
||||
* position layout no longer describe it. rad_arch_video_pack returns the total rows and, when
|
||||
* roomy, the boundaries and the full [3, rows] planar RoPE in one call. Otherwise the plain
|
||||
* layout is computed exactly as before, and a staged reference table (if any) rides inert. */
|
||||
const int64_t nd = positions_(RAD_VPOS_DECODE, &j->geom, nullptr, 0);
|
||||
if (nd <= 0 || nd > max_tok_)
|
||||
return no("the model lays this video's decoder in " + std::to_string(nd) + " rows, which "
|
||||
"this server cannot run");
|
||||
j->dec_rows = nd;
|
||||
if (d.ref_rows && !j->ref_tab.empty()) {
|
||||
if (r->upscale > 1.0)
|
||||
return no("reference conditioning does not yet combine with latent upscale; ask for "
|
||||
"the size you want without latent_upscale");
|
||||
if (!pack_)
|
||||
return no("the generator says it conditions on references but exports no "
|
||||
"rad_arch_video_pack");
|
||||
const int64_t pr = pack_(&j->geom, j->ref_tab.data(), nullptr, 0, nullptr, 0);
|
||||
if (pr <= 0)
|
||||
return no("the model refused the reference conditioning layout");
|
||||
if (pr > max_tok_)
|
||||
return no("a conditioned step is " + std::to_string(pr) + " rows and a pass carries " +
|
||||
std::to_string(max_tok_) + "; ask for a shorter or smaller video, or fewer "
|
||||
"references");
|
||||
j->seg.assign((size_t)kSegCap, 0);
|
||||
j->pos.assign((size_t)(3 * pr), 0.0f);
|
||||
if (pack_(&j->geom, j->ref_tab.data(), j->seg.data(), kSegCap, j->pos.data(), 3 * pr) != pr)
|
||||
return no("the model could not lay the reference conditioning out");
|
||||
int nseg = -1;
|
||||
for (int k = 0; k < kSegCap; ++k)
|
||||
if (j->seg[(size_t)k] == (int32_t)pr) { nseg = k + 1; break; } /* video grid ends here */
|
||||
if (nseg < 0 || nseg > kSegCap)
|
||||
return no("the reference conditioning layout has no closing segment boundary");
|
||||
j->seg.resize((size_t)nseg);
|
||||
j->nseg = nseg;
|
||||
/* The modulation groups. A modulation cell is fixed by a segment's kind, so the denoiser
|
||||
* merges runs of consecutive same-cell segments and pitches its fixed-width table by the
|
||||
* GROUP count, not the reference count -- which is how nine reference photos (four groups:
|
||||
* prompt, all-photos, soundtrack, video) ride a table the fine layout (twelve segments)
|
||||
* would overrun. The plugin merges for us (rad_arch_video_mod_segs); one that exports none
|
||||
* merges nothing, so the group table is the fine one and the older per-segment ceiling
|
||||
* applies. Refuse over-wide merges here, at submit, where a clean answer is still possible. */
|
||||
int nmod = -1;
|
||||
if (mod_segs_) {
|
||||
j->mod_seg.assign((size_t)kSegCap, 0);
|
||||
nmod = (int)mod_segs_(&j->geom, j->ref_tab.data(), j->mod_seg.data(), kSegCap);
|
||||
if (nmod < 0 || nmod > kSegCap)
|
||||
return no("the model could not merge the reference conditioning into its "
|
||||
"modulation groups");
|
||||
j->mod_seg.resize((size_t)nmod);
|
||||
} else {
|
||||
nmod = nseg; /* nothing merges: the groups are the fine segments */
|
||||
j->mod_seg = j->seg;
|
||||
}
|
||||
j->nmod = nmod;
|
||||
const int ngroups = nmod - 1; /* `mod_seg` is boundaries: one more than the groups */
|
||||
if (d.max_ref_segs > 0 && ngroups > d.max_ref_segs)
|
||||
return no("these references change the conditioning's modulation cell " +
|
||||
std::to_string(ngroups) + " times, but this deployment's modulation table holds " +
|
||||
std::to_string(d.max_ref_segs) + " cells -- ask for references that share their "
|
||||
"modalities and target, or fewer of them");
|
||||
/* `rows` stays the plain text+audio+video count: every non-step pass (encode, decode,
|
||||
* audio, upscale) stages its own segment table and target counts from it and must see the
|
||||
* bytes it sees today. Only build_step runs at the widened `step_rows`. */
|
||||
j->rows = T + s.audio_rows + s.video_rows;
|
||||
j->step_rows = pr;
|
||||
/* The last segment (the video target) runs [seg[nseg-2], seg[nseg-1]); seg[nseg-1] is the
|
||||
* total rows, so the video start is the boundary before it -- the same one the VSA takes as
|
||||
* its video base (it works in segments, one fewer than the boundaries it is handed). */
|
||||
j->lat_v = pr - j->seg[(size_t)(nseg - 2)];
|
||||
j->lat_a = decl_.audio_channels * (int64_t)j->geom.n_audio;
|
||||
j->ref_wide = true;
|
||||
} else {
|
||||
j->rows = T + s.audio_rows + s.video_rows;
|
||||
if (j->rows > max_tok_)
|
||||
return no("a step of this video is " + std::to_string(j->rows) + " rows and a pass "
|
||||
"carries " + std::to_string(max_tok_) + "; ask for a shorter or smaller video");
|
||||
const int64_t nr = positions_(RAD_VPOS_STEP, &j->geom, nullptr, 0);
|
||||
if (nr != j->rows)
|
||||
return no("the model lays this video out in " + std::to_string(nr) + " step rows, "
|
||||
"which this server cannot run");
|
||||
j->pos.assign((size_t)(3 * nr), 0.0f);
|
||||
if (positions_(RAD_VPOS_STEP, &j->geom, j->pos.data(), nr) != nr)
|
||||
return no("the model could not lay this video out");
|
||||
j->step_rows = j->rows;
|
||||
if (!d.guidance && named)
|
||||
return no("this model takes no guidance or negative prompt");
|
||||
j->n_neg = 0;
|
||||
if (d.guidance && (j->guidance[0] != 1.0f || j->guidance[4] != 1.0f)) {
|
||||
const int64_t N = (int64_t)r->neg_tokens.size();
|
||||
if (N < 1 || N > d.max_text)
|
||||
return no("the negative prompt is " + std::to_string(N) + " tokens; this model takes "
|
||||
"1.." + std::to_string(d.max_text));
|
||||
j->n_neg = (int32_t)N;
|
||||
}
|
||||
j->sched = video_schedule(d, steps, r->shift_video, r->shift_audio);
|
||||
if (j->sched.steps < 1) return no("the schedule has no steps");
|
||||
if (r->upscale > 1.0) {
|
||||
if (!(d.upscale_max > 1.0)) return no("this model has no latent upscaler");
|
||||
if (r->upscale > d.upscale_max)
|
||||
return no("latent_upscale is at most " + fmt("%g", d.upscale_max) + " for this model");
|
||||
int32_t sw = 0, sh = 0;
|
||||
video_small_size(d, r->width, r->height, r->upscale, &sw, &sh);
|
||||
if (sw == r->width && sh == r->height)
|
||||
return no("latent_upscale " + fmt("%g", r->upscale) + " leaves " +
|
||||
std::to_string(r->width) + "x" + std::to_string(r->height) + " as it is: "
|
||||
"the small size is each side over the factor, rounded to a multiple of " +
|
||||
std::to_string(d.size_multiple));
|
||||
const int32_t evals = j->sched.steps;
|
||||
int32_t refine = r->refine;
|
||||
if (refine == 0) refine = std::min<int32_t>(d.upscale_refine, evals - 1);
|
||||
if (refine < 1 || refine >= evals)
|
||||
return no("upscale_refine_steps must be 1.." + std::to_string(evals - 1) + ": the "
|
||||
"schedule takes " + std::to_string(evals) + " evaluations and the small "
|
||||
"size at least one of them");
|
||||
const VideoShape ss = video_shape(d, sw, sh, r->chunks);
|
||||
j->geom_small = j->geom;
|
||||
j->geom_small.lat_h = ss.lat_h;
|
||||
j->geom_small.lat_w = ss.lat_w;
|
||||
j->rows_small = T + ss.audio_rows + ss.video_rows;
|
||||
const int64_t ns = positions_(RAD_VPOS_STEP, &j->geom_small, nullptr, 0);
|
||||
if (ns != j->rows_small)
|
||||
return no("the model lays the small size " + std::to_string(sw) + "x" +
|
||||
std::to_string(sh) + " out in " + std::to_string(ns) + " step rows, which "
|
||||
"this server cannot run");
|
||||
j->pos_small.assign((size_t)(3 * ns), 0.0f);
|
||||
if (positions_(RAD_VPOS_STEP, &j->geom_small, j->pos_small.data(), ns) != ns)
|
||||
return no("the model could not lay the small size out");
|
||||
j->split = evals - refine;
|
||||
j->factor = (float)(((double)r->width / sw + (double)r->height / sh) / 2.0);
|
||||
r->ticket->small_width = sw;
|
||||
r->ticket->small_height = sh;
|
||||
r->ticket->refine = refine;
|
||||
}
|
||||
j->n_chunks = r->chunks;
|
||||
j->n_samples = (int64_t)s.n_audio * (d.sample_rate / d.audio_rate);
|
||||
/* The reference table and the VAE encode queue were built above, before the step layout was
|
||||
* chosen. Each reference the server decoded into encoder form rides one encode pass at the slot
|
||||
* its index numbers within its kind -- a picture or video a Vencode pass, a standalone audio and
|
||||
* every video's soundtrack an Aencode pass (a soundtrack at its video's slot, since the encoder
|
||||
* and the scatter both number it by its video). Endpoint keyframes ride the same queue as
|
||||
* picture anchors -- a Vencode pass at the image slot their keyframe order names. */
|
||||
/* The soundtrack's length, exactly the samples the decode will hand the encoder — the
|
||||
if (r->decoder < 0 || (r->decoder > 0 && (size_t)r->decoder >= decoder_area_.size()))
|
||||
return no("this model has no decoder " + std::to_string(r->decoder));
|
||||
if (r->decoder > 0 && W * H > decoder_area_[(size_t)r->decoder])
|
||||
return no("the " + decoder_names_[(size_t)r->decoder] + " decoder makes frames of " +
|
||||
std::to_string(decoder_area_[(size_t)r->decoder]) + " pixels at most");
|
||||
j->auto_len = r->chunks == 0 && d.duration_out != RAD_NULL_HANDLE;
|
||||
const int32_t chunks = r->chunks == 0 ? (int32_t)d.default_chunks : r->chunks;
|
||||
if (shape_job(*j, *r, chunks, steps, why) < 0) return RAD_E_INVAL;
|
||||
/* The soundtrack's length, exactly the samples the decode will hand the encoder -- the
|
||||
* number a caller that chains clips against an audio floor checks. A build without
|
||||
* libavcodec never gets the muxed file to read it back from, so it is logged here. */
|
||||
RAD_INFO("video: soundtrack %.2f s, %lld samples at %lld Hz x %lld ch",
|
||||
@@ -736,15 +902,12 @@ int VideoDriver::submit(std::unique_ptr<VideoRequest> r, std::string* why) {
|
||||
(long long)d.sample_rate, (long long)d.audio_channels);
|
||||
|
||||
VideoTicket& t = *r->ticket;
|
||||
t.frames = s.frames;
|
||||
t.width = r->width;
|
||||
t.height = r->height;
|
||||
t.steps = j->sched.steps;
|
||||
t.sample_rate = d.sample_rate;
|
||||
t.channels = d.audio_channels;
|
||||
t.fps = (double)d.fps_num / (double)d.fps_den;
|
||||
t.in_tokens = T;
|
||||
t.rows = j->step_rows; /* the conditioned step's real length, references included */
|
||||
t.t_submit_ns = (int64_t)rad_mono_ns();
|
||||
std::lock_guard<std::mutex> lk(mu_);
|
||||
if (closed_) {
|
||||
@@ -850,9 +1013,17 @@ void VideoDriver::stage_geom(const Job& j, const RadVideoGeom& g0, int64_t rows,
|
||||
b.lat_frames, b.lat_h / p, b.lat_w / p };
|
||||
std::memcpy(g + RAD_VGEOM_UP_SRC, up, sizeof up);
|
||||
}
|
||||
g[RAD_VGEOM_AUDIO_VOL] = g0.n_audio;
|
||||
g[RAD_VGEOM_AUDIO_VOL + 1] = 1;
|
||||
g[RAD_VGEOM_AUDIO_VOL + 2] = 1;
|
||||
const int32_t ppf = (int32_t)((g0.lat_h / decl_.patch) * (g0.lat_w / decl_.patch));
|
||||
g[RAD_VGEOM_FIRST_CU] = 0;
|
||||
g[RAD_VGEOM_FIRST_CU + 1] = ppf;
|
||||
g[RAD_VGEOM_FIRST_CU + 2] = ppf * g0.lat_frames;
|
||||
reg_[R_GEOM].used = RAD_VGEOM_N * 4;
|
||||
const bool wide = j.ref_wide && ref_step;
|
||||
const int64_t audio_rows = decl_.audio_channels * (int64_t)g0.n_audio;
|
||||
const int64_t audio_rows =
|
||||
(decl_.audio_streams > 0 ? decl_.audio_streams : decl_.audio_channels) * (int64_t)g0.n_audio;
|
||||
/* With references riding, the sampler owns only the target video rows -- not the reference
|
||||
* rows the pack placed ahead of them -- so the target count is the pack's, not the plain
|
||||
* rows-minus-text-minus-audio. The audio target is the whole soundtrack either way. */
|
||||
@@ -909,6 +1080,8 @@ void VideoDriver::stage_geom(const Job& j, const RadVideoGeom& g0, int64_t rows,
|
||||
lat[1] = (int32_t)video_rows;
|
||||
lat[2] = 0;
|
||||
lat[3] = (int32_t)audio_rows;
|
||||
std::memcpy(hptr<float>(R_GUIDE), j.guidance, sizeof j.guidance);
|
||||
reg_[R_GUIDE].used = sizeof j.guidance;
|
||||
reg_[R_LANE].used = 4;
|
||||
reg_[R_FIRST].used = 4;
|
||||
reg_[R_LAT].used = 4 * 4;
|
||||
@@ -953,6 +1126,10 @@ void VideoDriver::base_batches(const Job& j, const RadVideoGeom& g, int64_t n_to
|
||||
V.n_samples = j.n_samples;
|
||||
V.upscale = dptr<float>(ri, R_UP);
|
||||
V.ref_tab = j.ref_tab.empty() ? nullptr : j.ref_tab.data();
|
||||
V.guidance_h = j.guidance;
|
||||
V.guidance = dptr<float>(ri, R_GUIDE);
|
||||
V.has_image = !j.req->image.empty();
|
||||
V.decoder = j.req->decoder;
|
||||
B.vgen_b = &V;
|
||||
}
|
||||
}
|
||||
@@ -1013,23 +1190,39 @@ const RadBatch* VideoDriver::batch_for(int rank) const {
|
||||
|
||||
const RadBatch* VideoDriver::build_cond(Job& j, int step) {
|
||||
for (int g = 0; g < R_N; ++g) reg_[g].used = 0;
|
||||
const int64_t T = j.geom.n_text;
|
||||
/* The prompt, then the negative prompt when the request is guided against one, each its own
|
||||
* sequence from position 0. */
|
||||
const int64_t T = (int64_t)j.req->tokens.size(), N = j.n_neg;
|
||||
int32_t* tok = hptr<int32_t>(R_TOK);
|
||||
int32_t* pos = hptr<int32_t>(R_POS);
|
||||
std::memcpy(tok, j.req->tokens.data(), (size_t)T * 4);
|
||||
for (int64_t i = 0; i < T; ++i) pos[i] = (int32_t)i;
|
||||
if (N > 0) std::memcpy(tok + T, j.req->neg_tokens.data(), (size_t)N * 4);
|
||||
/* A left-padded encoder's prompt ends at text_left_pad - 1. */
|
||||
const int64_t lp = decl_.text_left_pad;
|
||||
for (int64_t i = 0; i < T; ++i) pos[i] = (int32_t)(lp > 0 ? lp - T + i : i);
|
||||
for (int64_t i = 0; i < N; ++i) pos[T + i] = (int32_t)(lp > 0 ? lp - N + i : i);
|
||||
int32_t* cu = hptr<int32_t>(R_CU);
|
||||
cu[0] = 0;
|
||||
cu[1] = (int32_t)T;
|
||||
cu[2] = (int32_t)(T + N);
|
||||
/* The first frame as the reference's image processor hands it to the VAE: x / 255 * 2 - 1 in
|
||||
* f32, then bf16. */
|
||||
const std::vector<uint8_t>& img = j.req->image;
|
||||
if (!img.empty()) {
|
||||
uint16_t* px = hptr<uint16_t>(R_IMG);
|
||||
for (size_t i = 0; i < img.size(); ++i)
|
||||
px[i] = rad_f32_to_bf16(2.0f * ((float)img[i] / 255.0f) - 1.0f);
|
||||
reg_[R_IMG].used = (int64_t)img.size() * 2;
|
||||
}
|
||||
float* tv = hptr<float>(R_TV);
|
||||
float* ta = hptr<float>(R_TA);
|
||||
for (int32_t i = 0; i < j.sched.steps; ++i) {
|
||||
tv[i] = 1.0f - j.sched.sig_v[(size_t)i];
|
||||
ta[i] = 1.0f - j.sched.sig_a[(size_t)i];
|
||||
}
|
||||
reg_[R_TOK].used = T * 4;
|
||||
reg_[R_POS].used = T * 4;
|
||||
reg_[R_CU].used = 2 * 4;
|
||||
reg_[R_TOK].used = (T + N) * 4;
|
||||
reg_[R_POS].used = (T + N) * 4;
|
||||
reg_[R_CU].used = (N > 0 ? 3 : 2) * 4;
|
||||
reg_[R_TV].used = j.sched.steps * 4;
|
||||
reg_[R_TA].used = j.sched.steps * 4;
|
||||
if (dit_dbg_)
|
||||
@@ -1090,7 +1283,7 @@ const RadBatch* VideoDriver::build_cond(Job& j, int step) {
|
||||
}
|
||||
|
||||
if (upload() < 0) return nullptr;
|
||||
base_batches(j, g, T, step);
|
||||
base_batches(j, g, T + N, step);
|
||||
if (j.req->media) {
|
||||
for (size_t r = 0; r < ranks_.size(); ++r) {
|
||||
RadBatch& B = out_[r];
|
||||
@@ -1108,9 +1301,18 @@ const RadBatch* VideoDriver::build_cond(Job& j, int step) {
|
||||
B.enc_max_seg = max_seg;
|
||||
}
|
||||
}
|
||||
for (auto& B : out_) B.gen = RAD_GEN_PASS_VCOND;
|
||||
for (size_t r = 0; r < out_.size(); ++r) {
|
||||
RadBatch& B = out_[r];
|
||||
B.gen = RAD_GEN_PASS_VCOND;
|
||||
B.n_seq = N > 0 ? 2 : 1;
|
||||
B.max_q_len = B.max_q_len_prefill = (int32_t)std::max(T, N);
|
||||
vout_[r].n_neg = (int32_t)N;
|
||||
if (!img.empty()) vout_[r].image = dptr<uint16_t>((int)r, R_IMG);
|
||||
}
|
||||
if (j.auto_len) rb_.assign(1, Readback{ decl_.duration_out, 4, duration_host_ });
|
||||
pass_.kind = RAD_GEN_PASS_VCOND;
|
||||
pass_.job = j.id;
|
||||
pass_.lora_slot = j.req->lora_slot;
|
||||
return &out_[0];
|
||||
}
|
||||
|
||||
@@ -1241,6 +1443,7 @@ const RadBatch* VideoDriver::build_step(Job& j, int step) {
|
||||
}
|
||||
pass_.kind = RAD_GEN_PASS_VSTEP;
|
||||
pass_.job = j.id;
|
||||
pass_.lora_slot = j.req->lora_slot;
|
||||
return &out_[0];
|
||||
}
|
||||
|
||||
@@ -1276,6 +1479,7 @@ const RadBatch* VideoDriver::build_audio(Job& j, int step) {
|
||||
const RadBatch* VideoDriver::build_chunk(Job& j, int step) {
|
||||
for (int g = 0; g < R_N; ++g) reg_[g].used = 0;
|
||||
stage_geom(j, j.geom, j.rows, j.chunk);
|
||||
stage_seed(j); /* a decoder that denoises draws from the request's seed */
|
||||
if (upload() < 0) return nullptr;
|
||||
base_batches(j, j.geom, j.dec_rows, step);
|
||||
const bool last = j.chunk == j.n_chunks - 1;
|
||||
@@ -1495,6 +1699,26 @@ int VideoDriver::done() {
|
||||
case RAD_GEN_PASS_VCOND:
|
||||
j->cond_done = true;
|
||||
j->req->ticket->t_cond_ns = now;
|
||||
if (j->auto_len) {
|
||||
j->auto_len = false;
|
||||
std::string note, why;
|
||||
int32_t n = predicted_chunks((double)duration_host_[0], ¬e);
|
||||
/* Shaped as at submit, which passed at default_chunks; a longer video than a pass
|
||||
* can carry is shortened until it fits. */
|
||||
const int32_t steps = j->req->steps > 0 ? j->req->steps : (int32_t)decl_.default_steps;
|
||||
const int32_t at_submit = j->n_chunks;
|
||||
while (n > at_submit && shape_job(*j, *j->req, n, steps, &why) < 0) --n;
|
||||
if (n <= at_submit && shape_job(*j, *j->req, n, steps, &why) < 0) {
|
||||
fail_locked(*j, RAD_E_INVAL, why);
|
||||
break;
|
||||
}
|
||||
RAD_INFO("video: the model gives the prompt %d frames (%.2f s)%s", j->geom.frames,
|
||||
(double)j->geom.frames * decl_.fps_den / decl_.fps_num, note.c_str());
|
||||
VideoEvent e;
|
||||
e.kind = VideoEvent::LENGTH;
|
||||
e.n = j->geom.frames;
|
||||
j->req->ticket->push(std::move(e));
|
||||
}
|
||||
break;
|
||||
case RAD_GEN_PASS_VENCODE:
|
||||
case RAD_GEN_PASS_AENCODE:
|
||||
|
||||
Reference in new issue
Block a user