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
//! Neural-network architecture of VoxCPM2: the top-level [`VoxCpm2Model`] that
//! composes the base and residual MiniCPM-4 LMs, the local feature encoder, the
//! diffusion decoder, the scalar-quantization layer, the projection layers, the
//! stop-prediction head, and the AudioVAE.
use crate::audiovae::AudioVae;
use crate::config::VoxCpm2Config;
use crate::fsq::ScalarQuantizationLayer;
use crate::locdit::{UnifiedCfm, VoxCpmLocDiTV2};
use crate::locenc::VoxCpmLocEnc;
use crate::minicpm4::MiniCpmModel;
use burn::module::Ignored;
use burn::nn::{Linear, LinearConfig};
use burn::prelude::*;
/// Special audio tokens added to the text vocabulary.
pub const AUDIO_START_TOKEN: i64 = 101;
pub const AUDIO_END_TOKEN: i64 = 102;
pub const REF_AUDIO_START_TOKEN: i64 = 103;
pub const REF_AUDIO_END_TOKEN: i64 = 104;
#[derive(Module, Debug)]
pub struct VoxCpm2Model<B: Backend> {
pub base_lm: MiniCpmModel<B>,
pub residual_lm: MiniCpmModel<B>,
pub feat_encoder: VoxCpmLocEnc<B>,
pub feat_decoder: UnifiedCfm<B>,
pub fsq_layer: ScalarQuantizationLayer<B>,
pub enc_to_lm_proj: Linear<B>,
pub lm_to_dit_proj: Linear<B>,
pub res_to_dit_proj: Linear<B>,
pub fusion_concat_proj: Linear<B>,
pub stop_proj: Linear<B>,
pub stop_head: Linear<B>,
pub audio_vae: AudioVae<B>,
pub config: Ignored<VoxCpm2Config>,
}
impl<B: Backend> VoxCpm2Model<B> {
pub fn new(config: VoxCpm2Config, device: &B::Device) -> Self {
let lm_hidden = config.lm_config.hidden_size;
let enc_hidden = config.encoder_config.hidden_dim;
let dit_hidden = config.dit_config.hidden_dim;
let audio_vae_config = config
.audio_vae_config
.clone()
.unwrap_or_default();
let base_lm = MiniCpmModel::new(config.lm_config.clone(), device);
let residual_lm = MiniCpmModel::new(config.residual_lm_config(), device);
let feat_encoder = VoxCpmLocEnc::new(config.encoder_lm_config(), config.feat_dim, device);
let estimator = VoxCpmLocDiTV2::new(config.dit_lm_config(), config.feat_dim, device);
let feat_decoder = UnifiedCfm::new(
config.feat_dim,
estimator,
config.dit_config.cfm_config.sigma_min as f64,
config.dit_config.cfm_config.inference_cfg_rate as f64,
config.dit_config.dit_mean_mode,
);
let fsq_layer = ScalarQuantizationLayer::new(
lm_hidden,
lm_hidden,
config.scalar_quantization_latent_dim,
config.scalar_quantization_scale,
device,
);
let enc_to_lm_proj = LinearConfig::new(enc_hidden, lm_hidden).init(device);
let lm_to_dit_proj = LinearConfig::new(lm_hidden, dit_hidden).init(device);
let res_to_dit_proj = LinearConfig::new(lm_hidden, dit_hidden).init(device);
let fusion_concat_proj = LinearConfig::new(lm_hidden * 2, lm_hidden).init(device);
let stop_proj = LinearConfig::new(lm_hidden, lm_hidden).init(device);
let stop_head = LinearConfig::new(lm_hidden, 2).with_bias(false).init(device);
let audio_vae = AudioVae::new(audio_vae_config, device);
Self {
base_lm,
residual_lm,
feat_encoder,
feat_decoder,
fsq_layer,
enc_to_lm_proj,
lm_to_dit_proj,
res_to_dit_proj,
fusion_concat_proj,
stop_proj,
stop_head,
audio_vae,
config: Ignored(config),
}
}
pub fn sample_rate(&self) -> usize {
self.audio_vae.out_sample_rate()
}
pub fn patch_size(&self) -> usize {
self.config.0.patch_size
}
pub fn latent_dim(&self) -> usize {
self.config.0.audio_vae_config.as_ref().map(|c| c.latent_dim).unwrap_or(64)
}
fn scale_emb(&self) -> f64 {
if self.config.0.lm_config.use_mup {
self.config.0.lm_config.scale_emb as f64
} else {
1.0
}
}
/// Run text + prompt-feat prefill through the base + residual LMs and
/// build the per-call autoregressive state used by [`Self::dit_step`] /
/// [`Self::lm_step`].
///
/// `max_len` sizes the static KV caches so they cover the prefill plus
/// up to `max_len` AR steps.
pub fn prefill(
&self,
text_token: Tensor<B, 2, burn::tensor::Int>,
text_mask: Tensor<B, 2>,
feat: Tensor<B, 4>,
feat_mask: Tensor<B, 2>,
max_len: usize,
) -> InferenceState<B> {
self.prefill_with_lengths(text_token, text_mask, feat, feat_mask, max_len, None)
}
/// Same as [`Self::prefill`] but accepts an optional `prefill_lengths`
/// vector — the per-batch real (unpadded) prefill length. When `Some`,
/// the inputs are assumed to be right-padded to a common max-S, and:
/// - the last hidden state per batch element is extracted at index
/// `lengths[b] - 1` (instead of `S - 1`),
/// - a key-padding mask is built and stored in the resulting state
/// so subsequent [`Self::lm_step`] calls can mask out the
/// `[lengths[b]..S]` gap in the KV caches.
///
/// When `None` the path is identical to the unbatched/serial code.
pub fn prefill_with_lengths(
&self,
text_token: Tensor<B, 2, burn::tensor::Int>,
text_mask: Tensor<B, 2>,
feat: Tensor<B, 4>,
feat_mask: Tensor<B, 2>,
max_len: usize,
prefill_lengths: Option<Vec<usize>>,
) -> InferenceState<B> {
let device = feat.device();
let [_b, _s, _p, _d] = feat.dims();
// 1) Encode audio feature patches.
let feat_embed = self.feat_encoder.forward(feat.clone()); // [B, S, enc_h]
let feat_embed = self.enc_to_lm_proj.forward(feat_embed); // [B, S, lm_h]
// 2) Embed text tokens.
let scale = self.scale_emb();
let text_embed = self.base_lm.embed(text_token).mul_scalar(scale); // [B, S, lm_h]
// 3) Combine via masks.
let text_mask3: Tensor<B, 3> = text_mask.clone().unsqueeze_dim(2);
let feat_mask3: Tensor<B, 3> = feat_mask.clone().unsqueeze_dim(2);
let combined = text_embed * text_mask3.clone() + feat_embed.clone() * feat_mask3.clone();
// 4) Prefix feat cond (last patch).
let s = feat.dims()[1];
let prefix_feat: Tensor<B, 3> = feat.clone().narrow(1, s - 1, 1).squeeze_dim::<3>(1); // [B, P, D]
// 5) Base LM prefill.
let (enc_outputs, base_kv) = self.base_lm.forward(combined, true);
let enc_outputs = self.fsq_layer.forward(enc_outputs.clone()) * feat_mask3.clone()
+ enc_outputs * text_mask3;
let lm_hidden_prefill = enc_outputs.clone();
// 6) Residual LM prefill.
let residual_input = self.fusion_concat_proj.forward(Tensor::cat(
vec![enc_outputs, feat_embed.clone() * feat_mask3],
2,
));
let (residual_outputs, residual_kv) = self.residual_lm.forward(residual_input, true);
// Seed caches with the prefill K/V.
let s_ctx = lm_hidden_prefill.dims()[1];
let batch = lm_hidden_prefill.dims()[0];
let lm_config = self.config.0.lm_config.clone();
let max_ctx = self.config.0.max_length.max(s_ctx + max_len);
let mut base_cache = crate::minicpm4::StaticKvCache::new(
lm_config.num_hidden_layers,
lm_config.num_key_value_heads,
lm_config.head_dim(),
batch,
max_ctx,
&device,
);
base_cache.fill(base_kv);
let res_cfg = self.config.0.residual_lm_config();
let mut res_cache = crate::minicpm4::StaticKvCache::new(
res_cfg.num_hidden_layers,
res_cfg.num_key_value_heads,
res_cfg.head_dim(),
batch,
max_ctx,
&device,
);
res_cache.fill(residual_kv);
// Take the last position for autoregressive start. Shape [B, lm_h].
// For batched padded inputs (`prefill_lengths` set), each batch
// element's real last position is `lengths[b] - 1`; we extract
// per-element with a small narrow loop and re-cat. For B=1 / serial,
// this collapses to the original `narrow(s_ctx-1, 1)` path.
let lm_h = lm_hidden_prefill.dims()[2];
let (lm_hidden, residual_hidden) = match &prefill_lengths {
None => (
lm_hidden_prefill.clone().narrow(1, s_ctx - 1, 1).squeeze_dim::<2>(1),
residual_outputs.clone().narrow(1, s_ctx - 1, 1).squeeze_dim::<2>(1),
),
Some(lens) => {
assert_eq!(lens.len(), batch, "prefill_lengths.len() must equal batch size");
let mut lm_rows = Vec::with_capacity(batch);
let mut res_rows = Vec::with_capacity(batch);
for (b, &len) in lens.iter().enumerate() {
assert!(len >= 1 && len <= s_ctx, "prefill_lengths[{b}]={len} out of range (S_ctx={s_ctx})");
lm_rows.push(
lm_hidden_prefill.clone()
.slice([b..b + 1, len - 1..len, 0..lm_h])
.squeeze_dim::<2>(1),
);
res_rows.push(
residual_outputs.clone()
.slice([b..b + 1, len - 1..len, 0..lm_h])
.squeeze_dim::<2>(1),
);
}
(Tensor::cat(lm_rows, 0), Tensor::cat(res_rows, 0))
}
};
// Build the cache key-padding mask once: mask out `[lengths[b]..s_ctx]`
// for each batch element. New decode positions (>= s_ctx) are always
// unmasked. We size the mask to `max_ctx` (full cache length) so it
// can be sliced to current_len at each step.
let key_padding_mask: Option<Tensor<B, 2, burn::tensor::Bool>> = match &prefill_lengths {
None => None,
Some(lens) => {
if lens.iter().all(|&l| l == s_ctx) {
None // all rows full — no padding to mask
} else {
let mut data = vec![false; batch * max_ctx];
for (b, &len) in lens.iter().enumerate() {
for j in len..s_ctx {
data[b * max_ctx + j] = true;
}
// [s_ctx..max_ctx] are future decode slots: keep false (unmasked).
}
Some(Tensor::<B, 1, burn::tensor::Bool>::from_data(
burn::tensor::TensorData::new(data, [batch * max_ctx]),
&device,
)
.reshape([batch, max_ctx]))
}
}
};
InferenceState {
lm_hidden,
residual_hidden,
prefix_feat_cond: prefix_feat,
base_cache,
res_cache,
steps_taken: 0,
key_padding_mask,
}
}
/// Run one diffusion sample + stop-head check from the current state.
///
/// Updates `state.prefix_feat_cond` to the newly predicted patch (so the
/// next DiT step sees it as context) but does NOT advance the LM caches —
/// call [`Self::lm_step`] with the returned `pred_feat` to do that before
/// the next [`Self::dit_step`].
pub fn dit_step(
&self,
state: &mut InferenceState<B>,
inference_timesteps: usize,
cfg_value: f64,
) -> DitStep<B> {
let patch_size = self.patch_size();
// DiT inputs: concat(lm_to_dit(lm), res_to_dit(res))
let dit1 = self.lm_to_dit_proj.forward(state.lm_hidden.clone());
let dit2 = self.res_to_dit_proj.forward(state.residual_hidden.clone());
let dit_hidden = Tensor::cat(vec![dit1, dit2], 1); // [B, 2*dit_h]
// Diffusion sample: [B, D, P] -> [B, P, D]
let pred = self.feat_decoder.forward(
dit_hidden,
inference_timesteps,
patch_size,
state.prefix_feat_cond.clone().swap_dims(1, 2),
1.0,
cfg_value,
1.0,
true,
);
let pred_feat = pred.swap_dims(1, 2);
let pred4: Tensor<B, 4> = pred_feat.clone().unsqueeze_dim(1);
state.prefix_feat_cond = pred_feat;
// Stop check (cheap GPU→CPU sync via argmax). One bit per batch
// element — for B=1 this is a 1-element vec.
let stop_logits = self
.stop_head
.forward(crate::minicpm4::silu_stable(self.stop_proj.forward(state.lm_hidden.clone())));
let stops: Vec<bool> = stop_logits
.argmax(1)
.into_data()
.iter::<i64>()
.map(|v| v == 1)
.collect();
DitStep { pred_feat: pred4, stops }
}
/// Advance the base + residual LMs by one position using `pred_feat`
/// (`[1, 1, P, D]`, the patch returned by [`Self::dit_step`]). Caller
/// should only call this if it intends to take another DiT step.
pub fn lm_step(&self, state: &mut InferenceState<B>, pred_feat: Tensor<B, 4>) {
let curr_embed = self.feat_encoder.forward(pred_feat); // [B, 1, enc_h]
let curr_embed = self.enc_to_lm_proj.forward(curr_embed); // [B, 1, lm_h]
let curr_embed2: Tensor<B, 2> = curr_embed.squeeze_dim::<2>(1); // [B, lm_h]
let pos = state.base_cache.step();
let mut lm_hidden = self.base_lm.forward_step_masked(
curr_embed2.clone(),
pos,
&mut state.base_cache,
state.key_padding_mask.clone(),
);
lm_hidden = self.fsq_layer.forward(lm_hidden);
let res_input2 = self
.fusion_concat_proj
.forward(Tensor::cat(vec![lm_hidden.clone(), curr_embed2], 1));
let pos = state.res_cache.step();
let residual_hidden = self.residual_lm.forward_step_masked(
res_input2,
pos,
&mut state.res_cache,
state.key_padding_mask.clone(),
);
state.lm_hidden = lm_hidden;
state.residual_hidden = residual_hidden;
state.steps_taken += 1;
}
/// Stack a sequence of predicted latent patches `[1, 1, P, D]` into the
/// AudioVAE input shape `[1, D, T*P]`.
pub fn stack_pred_feats(pred_feats: &[Tensor<B, 4>]) -> Tensor<B, 3> {
let feats = Tensor::cat(pred_feats.to_vec(), 1);
let [b, t, p, d] = feats.dims();
feats.swap_dims(1, 3).swap_dims(2, 3).reshape([b, d, t * p])
}
/// Core inference loop.
///
/// Runs the text+audio-mask prefill through the base LM and residual LM,
/// then iteratively samples audio feature patches via the diffusion
/// decoder until the stop head fires (or `max_len` is reached).
///
/// * `text_token`: `[B, S]` int tokens.
/// * `text_mask`, `feat_mask`: `[B, S]` float masks (0/1) indicating which
/// positions are text and which are audio patches.
/// * `feat`: `[B, S, P, D]` audio latent patches (zeros at text positions).
///
/// Returns `(latent, stop_steps)` where `latent` is `[B, D, T_max*P]`
/// and `stop_steps[b]` is the number of valid latent patches for
/// batch element `b` (= the index of the patch where its stop fired,
/// inclusive). For `B=1` the loop short-circuits when stop fires so
/// `T_max == stop_steps[0]`. For `B>1` the loop continues until ALL
/// elements have stopped (or `max_len`); callers must slice each
/// element to its own valid prefix before decoding through the AudioVAE.
pub fn inference(
&self,
text_token: Tensor<B, 2, burn::tensor::Int>,
text_mask: Tensor<B, 2>,
feat: Tensor<B, 4>,
feat_mask: Tensor<B, 2>,
min_len: usize,
max_len: usize,
inference_timesteps: usize,
cfg_value: f64,
cancel: Option<&dyn Fn() -> bool>,
) -> crate::Result<(Tensor<B, 3>, Vec<usize>)> {
self.inference_with_lengths(
text_token, text_mask, feat, feat_mask,
min_len, max_len, inference_timesteps, cfg_value, cancel, None,
)
}
/// Like [`Self::inference`] but accepts an optional `prefill_lengths`
/// vector for batched/right-padded inputs. See
/// [`Self::prefill_with_lengths`].
pub fn inference_with_lengths(
&self,
text_token: Tensor<B, 2, burn::tensor::Int>,
text_mask: Tensor<B, 2>,
feat: Tensor<B, 4>,
feat_mask: Tensor<B, 2>,
min_len: usize,
max_len: usize,
inference_timesteps: usize,
cfg_value: f64,
cancel: Option<&dyn Fn() -> bool>,
prefill_lengths: Option<Vec<usize>>,
) -> crate::Result<(Tensor<B, 3>, Vec<usize>)> {
let batch = text_token.dims()[0];
let mut state = self.prefill_with_lengths(
text_token, text_mask, feat, feat_mask, max_len, prefill_lengths,
);
let mut pred_feats: Vec<Tensor<B, 4>> = Vec::new();
let mut stopped = vec![false; batch];
let mut stop_steps = vec![max_len; batch];
let profile = std::env::var("VOXCPM_PROFILE").is_ok();
let mut t_dit_ns: u128 = 0;
let mut t_lm_ns: u128 = 0;
let mut n_steps: usize = 0;
// Helper closure: force a GPU→CPU sync by reading a tiny scalar.
let sync_barrier = |t: Tensor<B, 2>| {
let _ = t.slice([0..1, 0..1]).into_data();
};
for i in 0..max_len {
// Cancellation check (cheap atomic load via callback). Bails
// before launching the next DiT sample so latency = at most
// one in-flight diffusion step (~200ms on wgpu).
if let Some(c) = cancel
&& c()
{
return Err(crate::Error::Cancelled);
}
let t0 = profile.then(std::time::Instant::now);
let DitStep { pred_feat, stops } =
self.dit_step(&mut state, inference_timesteps, cfg_value);
pred_feats.push(pred_feat.clone());
if profile {
sync_barrier(pred_feat.clone().squeeze_dim::<3>(1).narrow(2, 0, 1).squeeze_dim(2));
}
let t1 = profile.then(std::time::Instant::now);
let mut all_done = false;
if i > min_len {
for (b, &s) in stops.iter().enumerate() {
if s && !stopped[b] {
stopped[b] = true;
stop_steps[b] = i + 1; // include the stop-firing patch
}
}
all_done = stopped.iter().all(|s| *s);
}
if all_done {
if let (Some(t0), Some(t1)) = (t0, t1) {
t_dit_ns += t1.duration_since(t0).as_nanos();
n_steps += 1;
}
break;
}
self.lm_step(&mut state, pred_feat);
if let (Some(t0), Some(t1)) = (t0, t1) {
sync_barrier(state.residual_hidden.clone());
let t2 = std::time::Instant::now();
t_dit_ns += t1.duration_since(t0).as_nanos();
t_lm_ns += t2.duration_since(t1).as_nanos();
n_steps += 1;
}
}
// Any element that never stopped: treat its valid length as the
// full number of patches we produced.
let produced = pred_feats.len();
for (b, s) in stop_steps.iter_mut().enumerate() {
if !stopped[b] || *s > produced {
*s = produced;
}
}
if profile && n_steps > 0 {
let ms = |ns: u128| (ns as f64) / 1e6;
eprintln!(
"[profile] AR steps={} dit+stop={:.1}ms lm_tail={:.1}ms avg_per_step: dit+stop={:.2}ms lm={:.2}ms",
n_steps, ms(t_dit_ns), ms(t_lm_ns),
ms(t_dit_ns) / n_steps as f64,
ms(t_lm_ns) / n_steps as f64,
);
}
Ok((Self::stack_pred_feats(&pred_feats), stop_steps))
}
}
/// Per-call autoregressive state produced by [`VoxCpm2Model::prefill`] and
/// consumed by [`VoxCpm2Model::dit_step`] / [`VoxCpm2Model::lm_step`].
///
/// You only need to touch this directly if you're driving inference manually
/// (e.g. for streaming or custom early-exit logic). The high-level
/// [`crate::VoxCPM::generate`] / [`crate::VoxCPM::generate_stream`] APIs
/// manage it for you.
#[derive(Debug)]
pub struct InferenceState<B: Backend> {
/// `[B, lm_h]` — last hidden state of the base LM (input to DiT + stop).
pub lm_hidden: Tensor<B, 2>,
/// `[B, lm_h]` — last hidden state of the residual LM (input to DiT).
pub residual_hidden: Tensor<B, 2>,
/// `[B, P, D]` — last predicted patch, used as DiT prefix for the next step.
pub prefix_feat_cond: Tensor<B, 3>,
/// Static KV cache for the base LM. Sized for prefill + `max_len` steps.
pub base_cache: crate::minicpm4::StaticKvCache<B>,
/// Static KV cache for the residual LM.
pub res_cache: crate::minicpm4::StaticKvCache<B>,
/// Number of [`VoxCpm2Model::lm_step`] calls applied so far.
pub steps_taken: usize,
/// `[B, max_ctx]` bool mask: `true` = position is padding from a
/// batched prefill and must be excluded from attention. `None` for
/// the unbatched/serial path.
pub key_padding_mask: Option<Tensor<B, 2, burn::tensor::Bool>>,
}
/// Output of [`VoxCpm2Model::dit_step`].
#[derive(Debug)]
pub struct DitStep<B: Backend> {
/// `[B, 1, P, D]` — the patch the diffusion sampler produced this step,
/// one per batch element.
pub pred_feat: Tensor<B, 4>,
/// One bit per batch element: `true` if the stop head argmax fired this
/// step for that element. The caller decides whether to honor it (e.g.
/// ignore until `min_len` patches are out).
pub stops: Vec<bool>,
}