llmman 0.1.689

Run any agent on any model, models stored as OCI images
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
//! The Cosmos3 omni transformer (diffusers `Cosmos3OmniTransformer`): two token
//! streams per layer that share nothing but attention. The understanding stream
//! (prompt tokens, causal decoder) never sees the generation stream (latents),
//! so it runs once per prompt ([`und_forward`]) and its keys/values feed every
//! denoising step ([`gen_forward`]).

use anyhow::{bail, Result};

use super::Hparams;
use crate::mediagen::backend::{Backend, Graph};
use crate::mediagen::ffi::{self, Tensor};
use crate::mediagen::ltx::rms;
use crate::mediagen::weights::Weights;

/// The causal mask is `n x n`; the checkpoints are trained on far shorter prompts.
pub const MAX_PROMPT_TOKENS: i64 = 4096;

/// Per-layer keys (normed, roped) and values of the understanding stream, `[head_dim * n_kv_heads, n_tokens]`.
pub struct UndCache {
    pub n_tokens: i64,
    pub k: Vec<Vec<f32>>,
    pub v: Vec<Vec<f32>>,
}

/// Interleaved 3-D mRoPE cos/sin tables `[n_tokens][head_dim/2]` for `(t, h, w)` positions.
pub fn mrope_tables(hp: &Hparams, positions: &[[f64; 3]]) -> (Vec<f32>, Vec<f32>) {
    let half = (hp.head_dim / 2) as usize;
    let sect = hp.mrope_section;
    let mut cos = Vec::with_capacity(positions.len() * half);
    let mut sin = Vec::with_capacity(positions.len() * half);
    for p in positions {
        for j in 0..half {
            let inv = 1.0 / hp.rope_theta.powf(2.0 * j as f64 / hp.head_dim as f64);
            // [T H W T H W ... T T]: H takes 1, 4, 7, ... and W 2, 5, 8, ... below 3*section
            let axis = if j < 3 * sect[1] as usize && j % 3 == 1 {
                1
            } else if j < 3 * sect[2] as usize && j % 3 == 2 {
                2
            } else {
                0
            };
            let angle = p[axis] * inv;
            cos.push(angle.cos() as f32);
            sin.push(angle.sin() as f32);
        }
    }
    (cos, sin)
}

/// Positions of the prompt tokens: the same index on all three axes.
pub fn text_positions(n: i64) -> Vec<[f64; 3]> {
    (0..n).map(|i| [i as f64; 3]).collect()
}

/// Vision token positions (`get_3d_mrope_ids_vae_tokens`): a `[t][h][w]` grid `temporal_margin` after the text.
pub fn vision_positions(hp: &Hparams, n_text: i64, grid: [i64; 3], fps: f32) -> Vec<[f64; 3]> {
    let offset = (n_text + hp.temporal_margin) as f64;
    let [gt, gh, gw] = grid;
    let modulated = hp.enable_fps_modulation && gt > 1;
    let mut out = Vec::with_capacity((gt * gh * gw) as usize);
    for t in 0..gt {
        let tp = if modulated {
            let tps = fps as f64 / hp.vae_scale_t as f64;
            let base_tps = hp.base_fps as f64 / hp.vae_scale_t as f64;
            t as f64 / tps * base_tps + offset
        } else {
            t as f64 + offset
        };
        for h in 0..gh {
            for w in 0..gw {
                out.push([tp, h as f64, w as f64]);
            }
        }
    }
    out
}

/// Sound token positions: one per latent frame at `sound_fps`, same temporal offset as vision.
pub fn sound_positions(hp: &Hparams, n_text: i64, n: i64, sound_fps: f32) -> Vec<[f64; 3]> {
    let offset = (n_text + hp.temporal_margin) as f64;
    (0..n)
        .map(|f| {
            let t = if hp.enable_fps_modulation && n > 1 {
                f as f64 / sound_fps as f64 * hp.base_fps as f64 + offset
            } else {
                f as f64 + offset
            };
            [t, 0.0, 0.0]
        })
        .collect()
}

/// Action token positions: transition `f` sits at video frame `f + 1`.
pub fn action_positions(hp: &Hparams, n_text: i64, n: i64, fps: f32) -> Vec<[f64; 3]> {
    let offset = (n_text + hp.temporal_margin) as f64;
    let base_tps = hp.base_fps as f64 / hp.vae_scale_t as f64;
    (0..n)
        .map(|f| {
            let t = if hp.enable_fps_modulation && n > 1 {
                (f + 1) as f64 / fps as f64 * base_tps + offset
            } else {
                (f + 1) as f64 + offset
            };
            [t, 0.0, 0.0]
        })
        .collect()
}

/// A (cos, sin) pair of mRoPE tables.
pub type Tables = (Vec<f32>, Vec<f32>);

/// diffusers' `Timesteps(256, flip_sin_to_cos=True)`: 128 cosines then 128 sines.
pub fn timestep_embedding(t: f32) -> Vec<f32> {
    let half = 128;
    let mut e = vec![0f32; 256];
    for j in 0..half {
        let freq = (-(10000f32).ln() * j as f32 / half as f32).exp();
        let arg = t * freq;
        e[j] = arg.cos();
        e[half + j] = arg.sin();
    }
    e
}

/// Rotate-half RoPE on `x: [n_heads * hd, T]` with a `[hd/2, T]` table shared by all heads.
pub fn rope<'g>(
    g: &'g Graph<'g>,
    x: Tensor<'g>,
    cos: Tensor<'g>,
    sin: Tensor<'g>,
    n_heads: i64,
) -> Tensor<'g> {
    let [dim, t, ..] = x.shape();
    let hd = dim / n_heads;
    let x4 = g.reshape_4d(g.cont(x), hd / 2, 2, n_heads, t);
    let nb = x4.strides();
    let x1 = g.cont(g.view_4d(x4, hd / 2, 1, n_heads, t, nb[1], nb[2], nb[3], 0));
    let x2 = g.cont(g.view_4d(x4, hd / 2, 1, n_heads, t, nb[1], nb[2], nb[3], nb[1]));
    let c = g.reshape_4d(cos, hd / 2, 1, 1, t);
    let s = g.reshape_4d(sin, hd / 2, 1, 1, t);
    let o1 = g.sub(g.mul(x1, c), g.mul(x2, s));
    let o2 = g.add(g.mul(x1, s), g.mul(x2, c));
    g.reshape_2d(g.concat(o1, o2, 1), dim, t)
}

/// RMSNorm over each head of `x: [n_heads * head_dim, T]` with a `[head_dim]` weight.
pub fn head_rms<'g>(
    g: &'g Graph<'g>,
    x: Tensor<'g>,
    hd: i64,
    eps: f32,
    w: Option<Tensor<'g>>,
) -> Tensor<'g> {
    let [dim, t, ..] = x.shape();
    let x3 = g.reshape_3d(g.cont(x), hd, dim / hd, t);
    let y = rms(g, x3, eps, w);
    g.reshape_2d(y, dim, t)
}

/// Grouped-query attention: q `[n_heads*hd, Tq]`, k/v `[n_kv*hd, Tk]` -> `[n_heads*hd, Tq]`.
pub fn sdpa_gqa<'g>(
    g: &'g Graph<'g>,
    q: Tensor<'g>,
    k: Tensor<'g>,
    v: Tensor<'g>,
    n_heads: i64,
    n_kv: i64,
    mask: Option<Tensor<'g>>,
    flash: bool,
) -> Tensor<'g> {
    let tq = q.shape()[1];
    let tk = k.shape()[1];
    let hd = q.shape()[0] / n_heads;
    let scale = 1.0 / (hd as f32).sqrt();
    let q3 = g.permute(g.reshape_3d(g.cont(q), hd, n_heads, tq), 0, 2, 1, 3);
    let k3 = g.permute(g.reshape_3d(g.cont(k), hd, n_kv, tk), 0, 2, 1, 3);
    if flash && mask.is_none() {
        let v3 = g.permute(g.reshape_3d(g.cont(v), hd, n_kv, tk), 0, 2, 1, 3);
        let k3 = g.cast(k3, ffi::ty::F16);
        let v3 = g.cast(v3, ffi::ty::F16);
        let cur = g.prec_f32(g.flash_attn_ext(q3, k3, v3, None, scale, 0.0, 0.0));
        g.reshape_2d(cur, hd * n_heads, tq)
    } else {
        let v3 = g.cont(g.permute(g.reshape_3d(g.cont(v), hd, n_kv, tk), 1, 2, 0, 3));
        let kq = g.mul_mat(k3, q3);
        let kq = g.soft_max_ext(kq, mask, scale, 0.0);
        let kqv = g.mul_mat(v3, kq);
        g.cont_2d(g.permute(kqv, 0, 2, 1, 3), hd * n_heads, tq)
    }
}

/// `Cosmos3VLTextMLP`: `down(silu(gate) * up)` or `down(relu(up)^2)`.
fn mlp<'g>(
    g: &'g Graph<'g>,
    w: &'g Weights,
    prefix: &str,
    x: Tensor<'g>,
    relu2: bool,
) -> Result<Tensor<'g>> {
    let up = g.linear(x, w.get(&format!("{prefix}.up_proj.weight"))?, None);
    let h = if relu2 {
        g.sqr(g.clamp(up, 0.0, f32::INFINITY))
    } else {
        let gate = g.linear(x, w.get(&format!("{prefix}.gate_proj.weight"))?, None);
        g.mul(g.silu(gate), up)
    };
    Ok(g.linear(h, w.get(&format!("{prefix}.down_proj.weight"))?, None))
}

/// The Qwen3-VL text decoder over `ids`, calling `on_layer(graph, k, v)` with
/// every layer's keys (normed for the generation stream, roped) and values;
/// returns the last layer's output, before the final norm.
fn text_decoder<'g>(
    hp: &Hparams,
    w: &'g Weights,
    gr: &'g Graph<'g>,
    ids: &[u32],
    mut on_layer: impl FnMut(&'g Graph<'g>, Tensor<'g>, Tensor<'g>),
) -> Result<Tensor<'g>> {
    let n = ids.len() as i64;
    if n == 0 || n > MAX_PROMPT_TOKENS {
        bail!("the prompt must be 1 to {MAX_PROMPT_TOKENS} tokens, got {n}");
    }
    let ids_i32: Vec<i32> = ids.iter().map(|&i| i as i32).collect();
    let ids_t = gr.input_i32(&[n], &ids_i32, "ids");
    let (cos, sin) = mrope_tables(hp, &text_positions(n));
    let half = hp.head_dim / 2;
    let cos_t = gr.input_f32(&[half, n], &cos, "cos");
    let sin_t = gr.input_f32(&[half, n], &sin, "sin");
    let mask_t = gr.input_f32(&[n, n], &causal_mask(n), "mask");

    let g = &gr;
    let eps = hp.rms_eps;
    let hd = hp.head_dim;
    let mut x = g.get_rows(w.get("embed_tokens.weight")?, ids_t);
    for l in 0..hp.n_layers {
        let p = format!("layers.{l}");
        let h = rms(
            g,
            x,
            eps,
            Some(w.get(&format!("{p}.input_layernorm.weight"))?),
        );
        let mut q = g.linear(h, w.get(&format!("{p}.self_attn.to_q.weight"))?, None);
        let mut k = g.linear(h, w.get(&format!("{p}.self_attn.to_k.weight"))?, None);
        let v = g.linear(h, w.get(&format!("{p}.self_attn.to_v.weight"))?, None);
        if hp.qk_norm_for_text {
            q = head_rms(
                g,
                q,
                hd,
                eps,
                Some(w.get(&format!("{p}.self_attn.norm_q.weight"))?),
            );
            k = head_rms(
                g,
                k,
                hd,
                eps,
                Some(w.get(&format!("{p}.self_attn.norm_k.weight"))?),
            );
        }
        let mut k_gen = match w.opt(&format!("{p}.self_attn.k_norm_und_for_gen.weight")) {
            Some(nw) => head_rms(g, k, hd, eps, Some(nw)),
            None => k,
        };
        q = rope(g, q, cos_t, sin_t, hp.n_heads);
        k = rope(g, k, cos_t, sin_t, hp.n_kv_heads);
        k_gen = rope(g, k_gen, cos_t, sin_t, hp.n_kv_heads);
        on_layer(gr, k_gen, v);
        let attn = sdpa_gqa(g, q, k, v, hp.n_heads, hp.n_kv_heads, Some(mask_t), false);
        x = g.add(
            x,
            g.linear(attn, w.get(&format!("{p}.self_attn.to_out.weight"))?, None),
        );
        let h2 = rms(
            g,
            x,
            eps,
            Some(w.get(&format!("{p}.post_attention_layernorm.weight"))?),
        );
        x = g.add(x, mlp(g, w, &format!("{p}.mlp"), h2, hp.relu2)?);
    }
    Ok(x)
}

/// `[Tk, Tq]`: -inf where the key follows the query.
pub fn causal_mask(n: i64) -> Vec<f32> {
    let n = n as usize;
    let mut mask = vec![0f32; n * n];
    for q in 0..n {
        mask[q * n + q + 1..(q + 1) * n].fill(f32::NEG_INFINITY);
    }
    mask
}

fn text_graph<'o>(be: &Backend, hp: &Hparams) -> Result<Graph<'o>> {
    Graph::new(be.api, 4 * 1024 + hp.n_layers as usize * 96)
}

/// The understanding stream over the prompt tokens.
pub fn und_forward(hp: &Hparams, w: &Weights, be: &Backend, ids: &[u32]) -> Result<UndCache> {
    let gr = text_graph(be, hp)?;
    let (mut ks, mut vs) = (Vec::new(), Vec::new());
    text_decoder(hp, w, &gr, ids, |gr, k, v| {
        let (k, v) = (gr.cont(k), gr.cont(v));
        gr.mark_output(k);
        gr.mark_output(v);
        ks.push(k);
        vs.push(v);
    })?;
    gr.compute(be)?;
    Ok(UndCache {
        n_tokens: ids.len() as i64,
        k: ks.iter().map(|&t| gr.output_f32(t)).collect(),
        v: vs.iter().map(|&t| gr.output_f32(t)).collect(),
    })
}

/// A Qwen3-VL text model's last hidden states `[n][hidden]`, before the final norm.
pub fn text_hidden(hp: &Hparams, w: &Weights, be: &Backend, ids: &[u32]) -> Result<Vec<f32>> {
    let gr = text_graph(be, hp)?;
    let x = text_decoder(hp, w, &gr, ids, |_, _, _| {})?;
    let x = gr.cont(x);
    gr.mark_output(x);
    gr.compute(be)?;
    Ok(gr.output_f32(x))
}

/// One modality's tokens of the generation stream for a denoising step.
pub struct Segment<'a> {
    /// `[n][dim]`: patchified latents (vision), sound latents, or action vectors.
    pub tokens: &'a [f32],
    pub n: i64,
    /// mRoPE tables `[n][head_dim/2]`.
    pub cos: &'a [f32],
    pub sin: &'a [f32],
    /// `1` for noisy tokens, `0` for conditioning tokens; `None` when all are noisy.
    pub noisy: Option<&'a [f32]>,
}

/// One denoising evaluation of the generation stream.
pub struct GenInputs<'a> {
    pub vision: Segment<'a>,
    pub sound: Option<Segment<'a>>,
    /// Action tokens and the embodiment domain selecting the projection weights.
    pub action: Option<(Segment<'a>, i64)>,
    /// Already scaled by `timestep_scale` (0.995 for t = 995).
    pub timestep: f32,
    pub und: &'a UndCache,
    pub flash: bool,
}

/// Velocity predictions per modality, `[n][dim]` each (empty when absent).
pub struct GenOutputs {
    pub vision: Vec<f32>,
    pub sound: Vec<f32>,
    pub action: Vec<f32>,
}

/// `DomainAwareLinear`: one domain's weight `[in, out]` and bias `[out]` from the embedding tables.
fn domain_linear<'g>(
    g: &'g Graph<'g>,
    w: &'g Weights,
    prefix: &str,
    domain: Tensor<'g>,
    n_in: i64,
    n_out: i64,
) -> Result<(Tensor<'g>, Tensor<'g>)> {
    let row = g.get_rows(w.get(&format!("{prefix}.fc.weight"))?, domain); // [in*out, 1]
                                                                          // the row is `[in][out]` (out fastest); mul_mat wants `[in, out]` in ggml order
    let wt = g.cont(g.transpose(g.reshape_2d(row, n_out, n_in)));
    let b = g.get_rows(w.get(&format!("{prefix}.bias.weight"))?, domain); // [out, 1]
    Ok((wt, g.reshape_2d(b, n_out, 1)))
}

/// The velocity predictions for every generation token.
pub fn gen_forward(hp: &Hparams, w: &Weights, be: &Backend, inp: &GenInputs) -> Result<GenOutputs> {
    let nt = inp.und.n_tokens;
    let nv = inp.vision.n;
    let ns = inp.sound.as_ref().map_or(0, |s| s.n);
    let na = inp.action.as_ref().map_or(0, |(s, _)| s.n);
    let n = nv + ns + na;
    let gr = Graph::new(be.api, 4 * 1024 + hp.n_layers as usize * 128)?;
    let half = hp.head_dim / 2;
    let v_tok = gr.input_f32(&[hp.patch_dim, nv], inp.vision.tokens, "vision");
    let s_tok = inp
        .sound
        .as_ref()
        .map(|s| gr.input_f32(&[hp.sound_dim, s.n], s.tokens, "sound"));
    let a_tok = inp
        .action
        .as_ref()
        .map(|(s, _)| gr.input_f32(&[hp.action_dim, s.n], s.tokens, "action"));
    let domain = inp
        .action
        .as_ref()
        .map(|(_, d)| gr.input_i32(&[1], &[*d as i32], "domain"));
    // rope tables and the noisy mask over the concatenated tokens
    let mut cos = inp.vision.cos.to_vec();
    let mut sin = inp.vision.sin.to_vec();
    let mut noisy: Vec<f32> = match inp.vision.noisy {
        Some(m) => m.to_vec(),
        None => vec![1.0; nv as usize],
    };
    let mut any_cond = inp.vision.noisy.is_some();
    for seg in inp.sound.iter().chain(inp.action.iter().map(|(s, _)| s)) {
        cos.extend_from_slice(seg.cos);
        sin.extend_from_slice(seg.sin);
        match seg.noisy {
            Some(m) => {
                noisy.extend_from_slice(m);
                any_cond = true;
            }
            None => noisy.extend(std::iter::repeat_n(1.0, seg.n as usize)),
        }
    }
    let cos_t = gr.input_f32(&[half, n], &cos, "cos");
    let sin_t = gr.input_f32(&[half, n], &sin, "sin");
    let noisy_t = any_cond.then(|| gr.input_f32(&[1, n], &noisy, "noisy"));
    let temb_in = gr.input_f32(&[256], &timestep_embedding(inp.timestep), "temb");
    let kv_dim = hp.head_dim * hp.n_kv_heads;
    let und_k: Vec<Tensor> = (0..hp.n_layers as usize)
        .map(|l| gr.input_f32(&[kv_dim, nt], &inp.und.k[l], &format!("und_k{l}")))
        .collect();
    let und_v: Vec<Tensor> = (0..hp.n_layers as usize)
        .map(|l| gr.input_f32(&[kv_dim, nt], &inp.und.v[l], &format!("und_v{l}")))
        .collect();

    let g = &gr;
    let eps = hp.rms_eps;
    let hd = hp.head_dim;
    // embed every modality, then pack them in order: vision, sound, action
    let mut x = g.linear(
        v_tok,
        w.get("proj_in.weight")?,
        Some(w.get("proj_in.bias")?),
    );
    if let Some(st) = s_tok {
        let e = g.linear(
            st,
            w.get("audio_proj_in.weight")?,
            Some(w.get("audio_proj_in.bias")?),
        );
        let e = g.add(
            e,
            g.reshape_2d(w.get("audio_modality_embed")?, hp.hidden, 1),
        );
        x = g.concat(x, e, 1);
    }
    let mut action_out_w = None;
    if let (Some(at), Some(d)) = (a_tok, domain) {
        let (wi, bi) = domain_linear(g, w, "action_proj_in", d, hp.action_dim, hp.hidden)?;
        let e = g.add(g.mul_mat(wi, at), bi);
        let e = g.add(
            e,
            g.reshape_2d(w.get("action_modality_embed")?, hp.hidden, 1),
        );
        x = g.concat(x, e, 1);
        action_out_w = Some(domain_linear(
            g,
            w,
            "action_proj_out",
            d,
            hp.hidden,
            hp.action_dim,
        )?);
    }
    // timestep embedding, added to the noisy tokens
    let te = g.linear(
        g.reshape_2d(temb_in, 256, 1),
        w.get("time_embedder.linear_1.weight")?,
        Some(w.get("time_embedder.linear_1.bias")?),
    );
    let te = g.linear(
        g.silu(te),
        w.get("time_embedder.linear_2.weight")?,
        Some(w.get("time_embedder.linear_2.bias")?),
    );
    x = match noisy_t {
        None => g.add(x, te),
        Some(m) => g.add(x, g.mul(g.repeat(te, x), m)),
    };

    for l in 0..hp.n_layers {
        let p = format!("layers.{l}");
        let h = rms(
            g,
            x,
            eps,
            Some(w.get(&format!("{p}.input_layernorm_moe_gen.weight"))?),
        );
        let q = g.linear(h, w.get(&format!("{p}.self_attn.add_q_proj.weight"))?, None);
        let k = g.linear(h, w.get(&format!("{p}.self_attn.add_k_proj.weight"))?, None);
        let v = g.linear(h, w.get(&format!("{p}.self_attn.add_v_proj.weight"))?, None);
        let q = head_rms(
            g,
            q,
            hd,
            eps,
            Some(w.get(&format!("{p}.self_attn.norm_added_q.weight"))?),
        );
        let k = head_rms(
            g,
            k,
            hd,
            eps,
            Some(w.get(&format!("{p}.self_attn.norm_added_k.weight"))?),
        );
        let q = rope(g, q, cos_t, sin_t, hp.n_heads);
        let k = rope(g, k, cos_t, sin_t, hp.n_kv_heads);
        let kk = g.concat(und_k[l as usize], g.cont(k), 1);
        let vv = g.concat(und_v[l as usize], g.cont(v), 1);
        let attn = sdpa_gqa(g, q, kk, vv, hp.n_heads, hp.n_kv_heads, None, inp.flash);
        x = g.add(
            x,
            g.linear(
                attn,
                w.get(&format!("{p}.self_attn.to_add_out.weight"))?,
                None,
            ),
        );
        let h2 = rms(
            g,
            x,
            eps,
            Some(w.get(&format!("{p}.post_attention_layernorm_moe_gen.weight"))?),
        );
        x = g.add(x, mlp(g, w, &format!("{p}.mlp_moe_gen"), h2, hp.relu2)?);
    }
    let out = g.cont(rms(g, x, eps, Some(w.get("norm_moe_gen.weight")?)));
    let nb = out.strides();
    let part = |off: i64, len: i64| g.view_2d(out, hp.hidden, len, nb[1], (off as usize) * nb[1]);
    let v_out = g.cont(g.linear(
        part(0, nv),
        w.get("proj_out.weight")?,
        Some(w.get("proj_out.bias")?),
    ));
    gr.mark_output(v_out);
    let s_out = (ns > 0)
        .then(|| -> Result<Tensor> {
            let t = g.cont(g.linear(
                part(nv, ns),
                w.get("audio_proj_out.weight")?,
                Some(w.get("audio_proj_out.bias")?),
            ));
            gr.mark_output(t);
            Ok(t)
        })
        .transpose()?;
    let a_out = match action_out_w {
        Some((wo, bo)) if na > 0 => {
            let t = g.cont(g.add(g.mul_mat(wo, part(nv + ns, na)), bo));
            gr.mark_output(t);
            Some(t)
        }
        _ => None,
    };
    gr.compute(be)?;
    Ok(GenOutputs {
        vision: gr.output_f32(v_out),
        sound: s_out.map(|t| gr.output_f32(t)).unwrap_or_default(),
        action: a_out.map(|t| gr.output_f32(t)).unwrap_or_default(),
    })
}

/// Latents `[C][T][H][W]` -> tokens `[t][h][w][(p*P + q)*C + c]` (`_patchify_and_pack_latents`).
pub fn patchify(hp: &Hparams, lat: &[f32], t: i64, h: i64, w: i64) -> Vec<f32> {
    let (c, p) = (hp.latent_channels, hp.patch);
    // odd latent sizes are zero-padded to whole patches
    let (hp_, wp) = ((h + p - 1) / p, (w + p - 1) / p);
    let mut out = vec![0f32; (t * hp_ * wp * hp.patch_dim) as usize];
    let idx = |ci: i64, ti: i64, hi: i64, wi: i64| (((ci * t + ti) * h + hi) * w + wi) as usize;
    let mut n = 0usize;
    for ti in 0..t {
        for hh in 0..hp_ {
            for ww in 0..wp {
                let base = n * hp.patch_dim as usize;
                for pp in 0..p {
                    for q in 0..p {
                        let (hi, wi) = (hh * p + pp, ww * p + q);
                        if hi >= h || wi >= w {
                            continue;
                        }
                        for ci in 0..c {
                            out[base + ((pp * p + q) * c + ci) as usize] = lat[idx(ci, ti, hi, wi)];
                        }
                    }
                }
                n += 1;
            }
        }
    }
    out
}

/// The inverse of [`patchify`].
pub fn unpatchify(hp: &Hparams, tokens: &[f32], t: i64, h: i64, w: i64) -> Vec<f32> {
    let (c, p) = (hp.latent_channels, hp.patch);
    let (hp_, wp) = ((h + p - 1) / p, (w + p - 1) / p);
    let mut out = vec![0f32; (c * t * h * w) as usize];
    let idx = |ci: i64, ti: i64, hi: i64, wi: i64| (((ci * t + ti) * h + hi) * w + wi) as usize;
    let mut n = 0usize;
    for ti in 0..t {
        for hh in 0..hp_ {
            for ww in 0..wp {
                let base = n * hp.patch_dim as usize;
                for pp in 0..p {
                    for q in 0..p {
                        let (hi, wi) = (hh * p + pp, ww * p + q);
                        if hi >= h || wi >= w {
                            continue;
                        }
                        for ci in 0..c {
                            out[idx(ci, ti, hi, wi)] =
                                tokens[base + ((pp * p + q) * c + ci) as usize];
                        }
                    }
                }
                n += 1;
            }
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn patchify_roundtrip() {
        let hp = Hparams::test_default();
        let (t, h, w) = (2, 4, 6);
        let lat: Vec<f32> = (0..(hp.latent_channels * t * h * w))
            .map(|i| i as f32)
            .collect();
        let tok = patchify(&hp, &lat, t, h, w);
        assert_eq!(tok.len(), (t * (h / 2) * (w / 2) * hp.patch_dim) as usize);
        // first token: (t=0,h=0..2,w=0..2), entries ordered p, q, c
        assert_eq!(tok[0], lat[0]); // c0, p0, q0
        assert_eq!(tok[hp.latent_channels as usize], lat[1]); // p0, q1 -> w=1
        assert_eq!(tok[2 * hp.latent_channels as usize], lat[w as usize]); // p1 -> h=1
        assert_eq!(unpatchify(&hp, &tok, t, h, w), lat);
        // odd sizes pad to whole patches and crop back
        let (h, w) = (3, 5);
        let lat: Vec<f32> = (0..(hp.latent_channels * t * h * w))
            .map(|i| i as f32)
            .collect();
        let tok = patchify(&hp, &lat, t, h, w);
        assert_eq!(tok.len(), (t * 2 * 3 * hp.patch_dim) as usize);
        assert_eq!(unpatchify(&hp, &tok, t, h, w), lat);
    }

    #[test]
    fn mrope_axes() {
        let hp = Hparams::test_default();
        // only the H axis differs: frequencies 1,4,7,..,58 change (sin: cos of a
        // tiny angle rounds to 1 in f32)
        let (_, s0) = mrope_tables(&hp, &[[3.0, 0.0, 0.0]]);
        let (_, s1) = mrope_tables(&hp, &[[3.0, 5.0, 0.0]]);
        for j in 0..64 {
            let h_axis = j < 60 && j % 3 == 1;
            assert_eq!(s0[j] != s1[j], h_axis, "j={j}");
        }
        let (_, s2) = mrope_tables(&hp, &[[3.0, 0.0, 5.0]]);
        for j in 0..64 {
            let w_axis = j < 60 && j % 3 == 2;
            assert_eq!(s0[j] != s2[j], w_axis, "j={j}");
        }
    }

    #[test]
    fn timestep_embedding_layout() {
        let e = timestep_embedding(0.995);
        assert_eq!(e.len(), 256);
        assert!((e[0] - 0.995f32.cos()).abs() < 1e-6);
        assert!((e[128] - 0.995f32.sin()).abs() < 1e-6);
        // last frequency 10000^(-127/128)
        let f = (-(10000f32).ln() * 127.0 / 128.0).exp();
        assert!((e[127] - (0.995 * f).cos()).abs() < 1e-6);
    }

    #[test]
    fn vision_positions_layout() {
        let hp = Hparams::test_default();
        let pos = vision_positions(&hp, 10, [3, 2, 2], 24.0);
        assert_eq!(pos.len(), 12);
        assert_eq!(pos[0], [15010.0, 0.0, 0.0]);
        assert_eq!(pos[3], [15010.0, 1.0, 1.0]);
        assert_eq!(pos[4], [15011.0, 0.0, 0.0]);
        // 12 fps: latent frames are 2 base-fps latent frames apart
        let pos12 = vision_positions(&hp, 10, [3, 2, 2], 12.0);
        assert_eq!(pos12[4][0], 15012.0);
        // a single frame is not fps-modulated
        assert_eq!(vision_positions(&hp, 10, [1, 2, 2], 12.0)[0][0], 15010.0);
    }
}