1use crate::audiovae::AudioVae;
20use crate::mmh3::{Layout, MiniMaxH3, time_shift_sigma};
21use crate::qwen3te::{ImageSpan, Qwen3Encoder};
22use crate::qwen3vis::{self, VisionTower};
23use crate::sampler::SplitMix64;
24use crate::tokenizer::Tokenizer;
25use crate::vae3d::VideoVae;
26use crate::vae3d::VideoVaeEncoder;
27use std::path::Path;
28use std::sync::Arc;
29
30pub const FPS: usize = 24;
31pub const AUDIO_LATENT_FPS: usize = 40;
32
33pub struct AnimParams {
34 pub width: usize,
35 pub height: usize,
36 pub frames: usize,
38 pub steps: usize,
39 pub seed: u64,
40 pub stock_sampler: bool,
43 pub max_tokens: usize,
44 pub first_frame: Option<(Vec<f32>, usize, usize)>,
47 pub last_frame: Option<(Vec<f32>, usize, usize)>,
48 pub mid_frames: Vec<((Vec<f32>, usize, usize), usize)>,
53 pub lora: Option<String>,
55 pub lora_strength: f32,
56 pub upscale: Option<String>,
61 pub upscale_by: f32,
62 pub stream_chunk: usize,
69 pub stream_sink: usize,
70 pub stream_window: usize,
71}
72
73const VISION_START: u32 = 151_652;
75const VISION_END: u32 = 151_653;
76
77pub fn fit_to_canvas(
81 rgb: &[f32],
82 h: usize,
83 w: usize,
84 out_h: usize,
85 out_w: usize,
86 crop: bool,
87) -> Vec<f32> {
88 let (sx0, sy0, sw, sh) = if crop {
91 let (tw, th) = (out_w as f64, out_h as f64);
92 let scale = (w as f64 / tw).min(h as f64 / th);
93 let (cw, ch) = ((tw * scale).round() as usize, (th * scale).round() as usize);
94 ((w - cw) / 2, (h - ch) / 2, cw.max(1), ch.max(1))
95 } else {
96 (0, 0, w, h)
97 };
98 let mut out = vec![0f32; 3 * out_h * out_w];
99 for c in 0..3 {
100 for y in 0..out_h {
101 let sy = ((y as f64 + 0.5) * sh as f64 / out_h as f64 - 0.5).max(0.0);
102 let y0 = sy.floor() as usize;
103 let y1 = (y0 + 1).min(sh - 1);
104 let fy = (sy - y0 as f64) as f32;
105 for x in 0..out_w {
106 let sx = ((x as f64 + 0.5) * sw as f64 / out_w as f64 - 0.5).max(0.0);
107 let x0 = sx.floor() as usize;
108 let x1 = (x0 + 1).min(sw - 1);
109 let fx = (sx - x0 as f64) as f32;
110 let p = |yy: usize, xx: usize| rgb[(c * h + sy0 + yy) * w + sx0 + xx];
111 let top = p(y0, x0) * (1.0 - fx) + p(y0, x1) * fx;
112 let bot = p(y1, x0) * (1.0 - fx) + p(y1, x1) * fx;
113 out[(c * out_h + y) * out_w + x] = top * (1.0 - fy) + bot * fy;
114 }
115 }
116 }
117 out
118}
119
120impl Default for AnimParams {
121 fn default() -> Self {
122 Self {
123 width: 512,
124 height: 288,
125 frames: 39,
126 steps: 4,
127 seed: 42,
128 stock_sampler: false,
129 max_tokens: 512,
130 first_frame: None,
131 last_frame: None,
132 mid_frames: Vec::new(),
133 lora: None,
134 lora_strength: 1.0,
135 upscale: None,
136 upscale_by: 2.0,
137 stream_chunk: 0,
138 stream_sink: 2,
139 stream_window: 2,
140 }
141 }
142}
143
144pub struct Anim {
147 pub rgb: Vec<f32>,
148 pub frames: usize,
149 pub height: usize,
150 pub width: usize,
151 pub audio: Vec<f32>,
152 pub samples: usize,
153 pub sample_rate: usize,
154}
155
156pub fn align_frames(n: usize) -> usize {
159 let mut n = n.max(5);
160 while n % 17 != 5 {
161 n += 1;
162 }
163 n
164}
165
166pub fn video_latent_t(frames: usize) -> usize {
167 if frames <= 5 {
168 2
169 } else {
170 (frames - 5) / 17 * 5 + 2
171 }
172}
173
174pub fn temporal_shape(len: usize) -> (usize, usize, usize) {
176 let frames = align_frames(len);
177 let audio_t = ((frames as f64 / FPS as f64) * AUDIO_LATENT_FPS as f64).round() as usize;
178 (frames, video_latent_t(frames), audio_t)
179}
180
181pub fn sigmas(steps: usize, shift: f64) -> Vec<f64> {
184 let table = 1000usize;
185 let mut out: Vec<f64> = (0..steps)
186 .map(|x| {
187 let idx = table - 1 - x * table / steps;
188 let t = (idx + 1) as f64 / table as f64;
189 shift * t / (1.0 + (shift - 1.0) * t)
190 })
191 .collect();
192 out.push(0.0);
193 out
194}
195
196pub fn gauss_pub(n: usize, seed: u64) -> Vec<f32> {
198 gauss(n, seed)
199}
200
201fn gauss(n: usize, seed: u64) -> Vec<f32> {
202 let mut rng = SplitMix64::new(seed);
203 let mut u = || (rng.next_u64() >> 11) as f64 / (1u64 << 53) as f64;
204 let mut out = Vec::with_capacity(n);
205 while out.len() < n {
206 let (a, b) = (u().max(1e-300), u());
207 let r = (-2.0 * a.ln()).sqrt();
208 let ang = 2.0 * std::f64::consts::PI * b;
209 out.push((r * ang.cos()) as f32);
210 if out.len() < n {
211 out.push((r * ang.sin()) as f32);
212 }
213 }
214 out
215}
216
217pub fn generate(
219 path: &Path,
220 prompt: &str,
221 p: &AnimParams,
222 mut progress: impl FnMut(&str, usize, usize),
223) -> Result<Anim, String> {
224 if p.width % 32 != 0 || p.height % 32 != 0 {
225 return Err("width/height must be multiples of 32".into());
226 }
227 let use_gpu = match std::env::var("CMF_MMH3_GPU").ok().as_deref() {
235 Some("1") => true,
236 Some("0") => false,
237 _ => mmh3_gpu_parity_probe(path).unwrap_or(false),
238 };
239 if use_gpu {
240 generate_inner(path, prompt, p, &mut progress)
241 } else {
242 crate::gpu::cpu_scope(|| generate_inner(path, prompt, p, &mut progress))
243 }
244}
245
246fn mmh3_gpu_parity_probe(path: &Path) -> Result<bool, String> {
253 let model = Arc::new(
254 cortiq_core::CmfModel::open(path).map_err(|e| format!("{}: {e}", path.display()))?,
255 );
256 let Some(idx) = model.tensors.iter().position(|t| {
261 t.name.starts_with("dit.") && t.name.ends_with("attn.qkv_proj.weight")
262 }) else {
263 tracing::info!("mmh3 GPU parity probe: no qkv weight — host path");
264 return Ok(false);
265 };
266 let entry = &model.tensors[idx];
267 let (rows, cols) = (entry.shape[0], entry.shape[1]);
268 let b = 64usize;
269 let mut xs = vec![0f32; b * cols];
270 for (i, v) in xs.iter_mut().enumerate() {
271 let base = ((i * 37 + 11) % 1009) as f32 / 1009.0 - 0.5;
272 *v = base * if i % 7 == 0 { 2000.0 } else { 2.0 };
273 }
274 let mut gpu = vec![0f32; b * rows];
275 if std::env::var("CMF_GPU_DEBUG").is_ok() {
276 eprintln!(
277 "mmh3 probe env: CMF_GPU={:?} enabled={} avail={}",
278 std::env::var("CMF_GPU").ok(),
279 crate::gpu::enabled(),
280 crate::gpu::backend_available(),
281 );
282 }
283 let qt = crate::qtensor::QTensor::from_model(&model, &entry.name.clone())?;
284 if !qt.device_matmat(&xs, b, &mut gpu) {
285 tracing::info!(
286 "mmh3 GPU parity probe: {:?} device GEMM refused ({rows}x{cols}) — host path",
287 entry.dtype
288 );
289 if std::env::var("CMF_GPU_DEBUG").is_ok() {
290 eprintln!("mmh3 probe: device GEMM refused {rows}x{cols} for {:?}", entry.dtype);
291 }
292 return Ok(false);
293 }
294 let host = {
295 let name = entry.name.clone();
296 let proj = crate::dit::Proj::from_model(&model, &name)?;
297 let mut out = vec![0f32; b * rows];
298 crate::gpu::cpu_scope(|| proj.matmat(&xs, b, &mut out, None));
299 out
300 };
301 let mut num = 0f64;
302 let mut den = 0f64;
303 for (g, h) in gpu.iter().zip(&host) {
304 num += ((g - h) as f64).powi(2);
305 den += (*h as f64).powi(2);
306 }
307 let rel = (num / den.max(1e-30)).sqrt();
308 let ok = rel < 1e-2;
309 tracing::info!(
310 "mmh3 GPU parity probe: rel rms {rel:.2e} → {}",
311 if ok { "device" } else { "host" }
312 );
313 if std::env::var("CMF_GPU_DEBUG").is_ok() {
314 eprintln!("mmh3 GPU parity probe: rel rms {rel:.2e}");
315 }
316 Ok(ok)
317}
318
319fn generate_inner(
320 path: &Path,
321 prompt: &str,
322 p: &AnimParams,
323 progress: &mut dyn FnMut(&str, usize, usize),
324) -> Result<Anim, String> {
325 let model = Arc::new(
326 cortiq_core::CmfModel::open(path).map_err(|e| format!("{}: {e}", path.display()))?,
327 );
328 let (frames_total, latent_t, audio_t) = temporal_shape(p.frames);
329 let (lat_h, lat_w) = (p.height / 16, p.width / 16);
330
331 let t_stage = std::time::Instant::now();
338 let mut marks: Vec<(&str, f32)> = Vec::new();
339 let lap = |marks: &mut Vec<(&'static str, f32)>, name: &'static str| {
340 let prev: f32 = marks.iter().map(|(_, v)| v).sum();
341 marks.push((name, t_stage.elapsed().as_secs_f32() - prev));
342 };
343 let vocab = model
344 .vocab
345 .as_deref()
346 .ok_or("packaged .cmf has no embedded tokenizer")?;
347 let tok = Tokenizer::from_bytes(vocab).map_err(|e| format!("tokenizer: {e}"))?;
348 let mut keyframes: Vec<(&(Vec<f32>, usize, usize), usize)> = p
352 .first_frame
353 .iter()
354 .map(|f| (f, 0usize))
355 .chain(p.mid_frames.iter().map(|(f, i)| (f, (*i).min(frames_total - 1))))
356 .chain(p.last_frame.iter().map(|f| (f, frames_total - 1)))
357 .collect();
358 keyframes.sort_by_key(|(_, i)| *i);
362 keyframes.dedup_by_key(|(_, i)| *i);
363 let mut ids: Vec<u32> = Vec::new();
364 let mut spans: Vec<ImageSpan> = Vec::new();
365 let mut embeds: Vec<Vec<f32>> = Vec::new();
366 let mut deepstack: Vec<Vec<f32>> = Vec::new();
367 let mut cond: Vec<Vec<f32>> = Vec::new();
368 let mut tags: Vec<u8> = Vec::new();
369
370 if !keyframes.is_empty() {
371 let tower = VisionTower::from_cmf(&model)?;
372 let te_only = std::env::var("CMF_TE_ONLY").as_deref() == Ok("1");
376 let venc = if te_only {
377 None
378 } else {
379 Some(VideoVaeEncoder::from_cmf(&model)?)
380 };
381 for (i, (frame, _)) in keyframes.iter().enumerate() {
382 let (src, sh, sw) = *frame;
383 let fitted = fit_to_canvas(src, *sh, *sw, p.height, p.width, i > 0);
386 if let Some(venc) = &venc {
387 let (z, _, _) = venc.encode_frame(
388 &fitted.iter().map(|&v| v * 2.0 - 1.0).collect::<Vec<_>>(),
389 p.height,
390 p.width,
391 );
392 cond.push(z);
393 }
394
395 for t in tok.encode(&format!("<Picture {}>: ", i + 1)) {
396 ids.push(t);
397 tags.push(1);
398 }
399 let (patches, gh, gw) = qwen3vis::preprocess(
400 &fitted,
401 p.height,
402 p.width,
403 tower.patch_size,
404 tower.temporal_patch,
405 tower.merge,
406 );
407 let (merged, deep) = tower.forward(&patches, gh, gw);
408 let n_img = merged.len() / tower.out_hidden;
409 ids.push(VISION_START);
412 tags.push(0);
413 let start = ids.len();
414 for _ in 0..n_img {
415 ids.push(VISION_START); tags.push(0);
417 }
418 ids.push(VISION_END);
419 tags.push(0);
420 spans.push(ImageSpan {
421 start,
422 len: n_img,
423 merged_h: gh / tower.merge,
424 merged_w: gw / tower.merge,
425 });
426 embeds.push(merged);
427 if deepstack.is_empty() {
428 deepstack = deep;
429 } else {
430 for (a, b) in deepstack.iter_mut().zip(deep) {
431 a.extend_from_slice(&b);
432 }
433 }
434 }
435 }
436 for t in tok.encode(prompt) {
437 ids.push(t);
438 tags.push(1);
439 }
440 if ids.is_empty() {
441 ids.push(151643); tags.push(1);
443 }
444 ids.truncate(p.max_tokens);
445 tags.truncate(ids.len());
446 lap(&mut marks, "prepare");
447 crate::gpu::mm_kill_arm(false);
453 progress("encode", 0, 1);
454 let states = {
455 let enc = Qwen3Encoder::from_cmf(&model)?;
456 enc.encode_with_images(&ids, &spans, &embeds, &deepstack)
457 };
458 if let Ok(p) = std::env::var("CMF_TE_DUMP") {
463 let w = states.len() / ids.len().max(1);
464 let mut b = Vec::with_capacity(16 + states.len() * 4);
465 b.extend_from_slice(&(ids.len() as u64).to_le_bytes());
466 b.extend_from_slice(&(w as u64).to_le_bytes());
467 for v in &states {
468 b.extend_from_slice(&v.to_le_bytes());
469 }
470 std::fs::write(&p, &b).map_err(|e| format!("CMF_TE_DUMP {p}: {e}"))?;
471 eprintln!("te dump: {} tokens x {w} -> {p}", ids.len());
472 }
473 progress("encode", 1, 1);
474 lap(&mut marks, "text encode");
475 crate::gpu::mm_kill_arm(true);
476 if std::env::var("CMF_TE_ONLY").as_deref() == Ok("1") {
479 return Err("CMF_TE_ONLY: encode dumped, render skipped".into());
480 }
481 {
487 let dropped = model.advise_done(|n| n.starts_with("model.") || n.starts_with("vis."));
488 if dropped > 0 {
489 tracing::info!(
490 "encoder pages released after prompt encode: {} MB",
491 dropped / (1024 * 1024)
492 );
493 }
494 }
495
496 let (mut video, audio) = {
498 let bank = match p.lora.as_deref() {
503 None => None,
504 Some(path) => {
505 let k = crate::ltxlora::LoraBank::load(std::path::Path::new(path), p.lora_strength)?;
506 Some(k)
507 }
508 };
509 let dit = MiniMaxH3::from_cmf_lora(&model, bank.as_ref())?;
510 if let Some(k) = &bank {
511 let bound = dit.lora_bound();
512 let mut skipped: std::collections::BTreeMap<String, usize> = Default::default();
516 for name in k.keys() {
517 if !dit.lora_binds(name) {
518 let fam = name
519 .rsplit_once('.')
520 .map(|(_, t)| {
521 let head = name.split('.').next().unwrap_or("");
522 format!("{head}…{t}")
523 })
524 .unwrap_or_else(|| name.to_string());
525 *skipped.entry(fam).or_default() += 1;
526 }
527 }
528 let tail = if skipped.is_empty() {
529 String::new()
530 } else {
531 let parts: Vec<String> =
532 skipped.iter().map(|(k, v)| format!("{k} ×{v}")).collect();
533 format!("; not applied: {}", parts.join(", "))
534 };
535 tracing::info!(
536 "lora: rank {}, {} branches, {} bound at strength {}{}",
537 k.rank(),
538 k.len(),
539 bound,
540 p.lora_strength,
541 tail
542 );
543 println!(
544 "lora: rank {}, {}/{} branches bound{}",
545 k.rank(),
546 bound,
547 k.len(),
548 tail
549 );
550 }
551 let kf: Vec<(usize, usize)> = keyframes
552 .iter()
553 .map(|&(_, idx)| (idx, frames_total))
554 .collect();
555 let layout = if kf.is_empty() {
556 Layout::t2va(ids.len(), latent_t, lat_h, lat_w, audio_t)
557 } else {
558 Layout::fl2va(ids.len(), latent_t, lat_h, lat_w, audio_t, &kf, &tags)
559 };
560 let text = dit.refine_text(&states, ids.len());
561 let mut v = gauss(dit.latents_dim * latent_t * lat_h * lat_w, p.seed);
562 let mut a = gauss(dit.audio_dim * 2 * audio_t, p.seed ^ 0x9E37_79B9_7F4A_7C15);
563 let sg = sigmas(p.steps, dit.shift_video);
564 let mut return_streaming: Option<(Vec<f32>, Vec<f32>)> = None;
565 let prof = std::env::var_os("CMF_ANIM_PROF").is_some();
569 let rms = |x: &[f32]| {
570 (x.iter().map(|&v| (v as f64) * (v as f64)).sum::<f64>() / x.len() as f64).sqrt()
571 };
572 if prof {
573 eprintln!(
574 " text {} tok, refined rms {:.4}, sigmas {:?}",
575 ids.len(),
576 rms(&text),
577 sg.iter()
578 .map(|v| (v * 1e4).round() / 1e4)
579 .collect::<Vec<_>>()
580 );
581 }
582 if p.stream_chunk > 0 && cond.is_empty() {
592 let hw = lat_h * lat_w / 4 * 4; let _ = hw;
594 let vc = dit.latents_dim;
595 let ac = dit.audio_dim;
596 let vframe = lat_h * lat_w; let mut chunks: Vec<std::ops::Range<usize>> = (0..latent_t)
598 .step_by(p.stream_chunk)
599 .map(|s| s..(s + p.stream_chunk).min(latent_t))
600 .collect();
601 if chunks.len() > 1 {
607 let tail = chunks[chunks.len() - 1].clone();
608 if tail.len() < p.stream_chunk.div_ceil(2) {
609 chunks.pop();
610 let last = chunks.len() - 1;
611 chunks[last].end = tail.end;
612 }
613 }
614 let a_bound = |k: usize| (k * audio_t).div_ceil(latent_t.max(1));
617 let mut v_out = vec![0f32; v.len()];
618 let mut a_out = vec![0f32; a.len()];
619 let gather_v = |src: &[f32], idx: &[usize]| -> Vec<f32> {
620 let mut out = vec![0f32; vc * idx.len() * vframe];
621 for ci in 0..vc {
622 for (n, &k) in idx.iter().enumerate() {
623 let s = (ci * latent_t + k) * vframe;
624 let d = (ci * idx.len() + n) * vframe;
625 out[d..d + vframe].copy_from_slice(&src[s..s + vframe]);
626 }
627 }
628 out
629 };
630 let gather_a = |src: &[f32], idx: &[usize]| -> Vec<f32> {
631 let mut out = vec![0f32; ac * 2 * idx.len()];
632 for ci in 0..ac {
633 for ch in 0..2 {
634 for (n, &i) in idx.iter().enumerate() {
635 out[(ci * 2 + ch) * idx.len() + n] = src[(ci * 2 + ch) * audio_t + i];
636 }
637 }
638 }
639 out
640 };
641 for (ci_, cur) in chunks.iter().enumerate() {
642 let by_chunk = std::env::var("CMF_STREAM_UNIT").as_deref() == Ok("chunks");
651 let ctx_v: Vec<usize> = if by_chunk {
652 let mut vis: Vec<usize> = (0..p.stream_sink.min(ci_)).collect();
653 for j in ci_.saturating_sub(p.stream_window)..ci_ {
654 if !vis.contains(&j) {
655 vis.push(j);
656 }
657 }
658 vis.sort_unstable();
659 vis.iter().flat_map(|&j| chunks[j].clone()).collect()
660 } else {
661 let done = cur.start; let mut f: Vec<usize> = (0..p.stream_sink.min(done)).collect();
663 for k in done.saturating_sub(p.stream_window)..done {
664 if !f.contains(&k) {
665 f.push(k);
666 }
667 }
668 f.sort_unstable();
669 f
670 };
671 let cur_v: Vec<usize> = cur.clone().collect();
672 let mut ctx_a: Vec<usize> = Vec::new();
674 for &k in &ctx_v {
675 for i in a_bound(k)..a_bound(k + 1) {
676 if !ctx_a.contains(&i) {
677 ctx_a.push(i);
678 }
679 }
680 }
681 ctx_a.sort_unstable();
682 let cur_a: Vec<usize> = (a_bound(cur.start)..a_bound(cur.end)).collect();
683 let clay = Layout::streaming(
684 ids.len(),
685 &tags,
686 lat_h,
687 lat_w,
688 &ctx_v,
689 &cur_v,
690 &ctx_a,
691 &cur_a,
692 );
693 let ctx_vx = gather_v(&v_out, &ctx_v);
694 let ctx_ax = gather_a(&a_out, &ctx_a);
695 let mut xv = gather_v(&v, &cur_v);
696 let mut xa = gather_a(&a, &cur_a);
697 for i in 0..p.steps {
698 let (sv, sv_n) = (sg[i], sg[i + 1]);
699 let mut vin = vec![0f32; vc * (ctx_v.len() + cur_v.len()) * vframe];
702 let mut ain = vec![0f32; ac * 2 * (ctx_a.len() + cur_a.len())];
703 let (nc, nk) = (ctx_v.len(), cur_v.len());
704 for c in 0..vc {
705 let d = c * (nc + nk) * vframe;
706 vin[d..d + nc * vframe]
707 .copy_from_slice(&ctx_vx[c * nc * vframe..(c + 1) * nc * vframe]);
708 vin[d + nc * vframe..d + (nc + nk) * vframe]
709 .copy_from_slice(&xv[c * nk * vframe..(c + 1) * nk * vframe]);
710 }
711 let (mc, mk) = (ctx_a.len(), cur_a.len());
712 for c in 0..ac {
713 for ch in 0..2 {
714 let d = (c * 2 + ch) * (mc + mk);
715 ain[d..d + mc]
716 .copy_from_slice(&ctx_ax[(c * 2 + ch) * mc..(c * 2 + ch + 1) * mc]);
717 ain[d + mc..d + mc + mk]
718 .copy_from_slice(&xa[(c * 2 + ch) * mk..(c * 2 + ch + 1) * mk]);
719 }
720 }
721 let (dv, da) = dit.forward(&clay, &text, &vin, &ain, sv, &[]);
722 let step_v = (sv_n - sv) as f32;
723 for (x, &d) in xv.iter_mut().zip(&dv) {
724 *x += step_v * d;
725 }
726 let step_a = if p.stock_sampler {
727 step_v
728 } else {
729 (time_shift_sigma(sv_n, dit.shift_video, dit.shift_audio)
730 - time_shift_sigma(sv.max(1e-6), dit.shift_video, dit.shift_audio))
731 as f32
732 };
733 for (x, &d) in xa.iter_mut().zip(&da) {
734 *x += step_a * d;
735 }
736 }
737 for c in 0..vc {
738 for (n, &k) in cur_v.iter().enumerate() {
739 let d = (c * latent_t + k) * vframe;
740 let s = (c * cur_v.len() + n) * vframe;
741 v_out[d..d + vframe].copy_from_slice(&xv[s..s + vframe]);
742 }
743 }
744 for c in 0..ac {
745 for ch in 0..2 {
746 for (n, &i) in cur_a.iter().enumerate() {
747 a_out[(c * 2 + ch) * audio_t + i] =
748 xa[(c * 2 + ch) * cur_a.len() + n];
749 }
750 }
751 }
752 progress("stream", ci_ + 1, chunks.len());
753 }
754 if let Some(rep) = dit.lora_report() {
755 eprint!("{rep}");
756 }
757 return_streaming = Some((v_out, a_out));
758 }
759
760 for i in 0..p.steps {
761 if return_streaming.is_some() {
762 break;
763 }
764 let (sv, sv_n) = (sg[i], sg[i + 1]);
765 let (dv, da) = dit.forward(&layout, &text, &v, &a, sv, &cond);
766 let step_v = (sv_n - sv) as f32;
767 for (x, &d) in v.iter_mut().zip(&dv) {
768 *x += step_v * d;
769 }
770 let step_a = if p.stock_sampler {
771 step_v
772 } else {
773 (time_shift_sigma(sv_n, dit.shift_video, dit.shift_audio)
774 - time_shift_sigma(sv.max(1e-6), dit.shift_video, dit.shift_audio))
775 as f32
776 };
777 for (x, &d) in a.iter_mut().zip(&da) {
778 *x += step_a * d;
779 }
780 if prof {
781 eprintln!(
782 " step {i}: sv {sv:.4}->{sv_n:.4} v_vel {:.4} a_vel {:.4} | video {:.4} audio {:.4}",
783 rms(&dv),
784 rms(&da),
785 rms(&v),
786 rms(&a)
787 );
788 }
789 progress("denoise", i + 1, p.steps);
790 }
791 if let Some(rep) = dit.lora_report() {
792 eprint!("{rep}");
793 }
794 match return_streaming {
795 Some(pair) => pair,
796 None => (v, a),
797 }
798 };
799
800 lap(&mut marks, "denoise");
801
802 let (mut out_h, mut out_w) = (p.height, p.width);
804 let (mut lat_h, mut lat_w) = (lat_h, lat_w);
805 if let Some(path) = p.upscale.as_deref() {
806 progress("upscale", 0, 1);
807 let t = std::time::Instant::now();
808 let ups = crate::mmh3ups::LatentUpscaler::load(std::path::Path::new(path))?;
809 let z = crate::mmh3ups::Vol {
810 c: video.len() / (latent_t * lat_h * lat_w),
811 t: latent_t,
812 h: lat_h,
813 w: lat_w,
814 data: video,
815 };
816 let f = p.upscale_by.max(1.0);
819 let nh = ((lat_h as f32 * f).round() as usize).max(lat_h);
820 let nw = ((lat_w as f32 * f).round() as usize).max(lat_w);
821 let big = ups.upscale(&z, nh, nw, None);
822 tracing::info!(
823 "latent upscale {}x{} -> {}x{} in {:.1}s",
824 lat_h,
825 lat_w,
826 nh,
827 nw,
828 t.elapsed().as_secs_f64()
829 );
830 out_h = out_h * nh / lat_h;
831 out_w = out_w * nw / lat_w;
832 lat_h = nh;
833 lat_w = nw;
834 video = big.data;
835 progress("upscale", 1, 1);
836 lap(&mut marks, "upscale");
837 }
838
839 progress("video vae", 0, 1);
841 let (rgb, out_frames) = {
842 let vae = VideoVae::from_cmf(&model)?;
843 vae.decode(&video, latent_t, lat_h, lat_w)
844 };
845 progress("video vae", 1, 1);
846 lap(&mut marks, "video vae");
847 progress("audio vae", 0, 1);
848 let (wave, samples, sr) = {
849 let vae = AudioVae::from_cmf(&model)?;
850 let c = audio.len() / (2 * audio_t);
851 let (w, n) = vae.decode(&audio, c, audio_t);
852 (w, n, vae.sample_rate)
853 };
854 progress("audio vae", 1, 1);
855 lap(&mut marks, "audio vae");
856 tracing::info!(
857 "stages: {}",
858 marks
859 .iter()
860 .map(|(n, v)| format!("{n} {v:.1}s"))
861 .collect::<Vec<_>>()
862 .join(" · ")
863 );
864 #[cfg(target_os = "macos")]
868 if std::env::var("CMF_METAL_MMPROF").is_ok() {
869 use std::sync::atomic::Ordering::Relaxed;
870 let n = crate::gpu_metal::MM_N.load(Relaxed);
871 eprintln!(
872 " q4tp mm x{n}: upload {:.1}s · submit+wait {:.1}s · readback {:.1}s",
873 crate::gpu_metal::MM_UP.load(Relaxed) as f64 / 1e6,
874 crate::gpu_metal::MM_GPU.load(Relaxed) as f64 / 1e6,
875 crate::gpu_metal::MM_DN.load(Relaxed) as f64 / 1e6,
876 );
877 }
878
879 let keep = out_frames.min(frames_total);
883 Ok(Anim {
884 rgb: trim_frames(&rgb, out_frames, keep, out_h, out_w),
885 frames: keep,
886 height: out_h,
887 width: out_w,
888 audio: wave,
889 samples,
890 sample_rate: sr,
891 })
892}
893
894fn trim_frames(rgb: &[f32], have: usize, keep: usize, h: usize, w: usize) -> Vec<f32> {
895 if keep == have {
896 return rgb.to_vec();
897 }
898 let mut out = vec![0f32; 3 * keep * h * w];
899 for c in 0..3 {
900 let s = c * have * h * w;
901 let d = c * keep * h * w;
902 out[d..d + keep * h * w].copy_from_slice(&rgb[s..s + keep * h * w]);
903 }
904 out
905}
906
907#[cfg(test)]
908mod tests {
909 use super::*;
910
911 #[test]
912 fn the_four_step_schedule_is_the_references() {
913 let s = sigmas(4, 12.0);
914 let want = [1.0, 0.972_973, 0.923_077, 0.8, 0.0];
915 assert_eq!(s.len(), want.len());
916 for (g, w) in s.iter().zip(&want) {
917 assert!((g - w).abs() < 1e-6, "{s:?}");
918 }
919 }
920
921 #[test]
922 fn a_stretch_keeps_the_corners_and_a_crop_takes_the_middle() {
923 let (h, w) = (2usize, 4usize);
926 let mut rgb = vec![0f32; 3 * h * w];
927 for c in 0..3 {
928 for y in 0..h {
929 for x in 0..w {
930 rgb[(c * h + y) * w + x] = x as f32 / (w - 1) as f32;
931 }
932 }
933 }
934 let s = fit_to_canvas(&rgb, h, w, 4, 4, false);
936 assert!((s[0] - 0.0).abs() < 1e-6, "left edge");
937 assert!((s[3] - 1.0).abs() < 1e-6, "right edge");
938 let c = fit_to_canvas(&rgb, h, w, 4, 4, true);
941 let (lo, hi) = c[..16]
942 .iter()
943 .fold((f32::MAX, f32::MIN), |(a, b), &v| (a.min(v), b.max(v)));
944 assert!(lo > 0.05, "crop kept the left edge: {lo}");
945 assert!(hi < 0.95, "crop kept the right edge: {hi}");
946 }
947
948 #[test]
949 fn frame_counts_snap_to_the_models_grid() {
950 assert_eq!(align_frames(1), 5);
952 assert_eq!(align_frames(39), 39);
953 assert_eq!(align_frames(41), 56);
954 assert_eq!(align_frames(124), 124);
955 assert_eq!(video_latent_t(124), 37);
956 assert_eq!(video_latent_t(39), 12);
957 let (f, lt, at) = temporal_shape(124);
958 assert_eq!((f, lt, at), (124, 37, 207));
959 }
960}