Commit e4b620af1 for llama.cpp

commit e4b620af19f02960dea7a3a81de7c5805aa3a333
Author: Xuan-Son Nguyen <son@huggingface.co>
Date:   Thu Nov 19 02:39:05 2026 +0100

    mtmd: support voxcpm2 (#32443)

    * mtmd: support voxcpm

    * speed up a bit

    * various fixes

diff --git a/conversion/__init__.py b/conversion/__init__.py
index 634cafa24..1182c06be 100644
--- a/conversion/__init__.py
+++ b/conversion/__init__.py
@@ -287,6 +287,7 @@ TEXT_MODEL_MAP: dict[str, str] = {
     "TinyVocaModel": "tiny_voca",
     "Zonos3Model": "zonos3",
     "YuE2ForCausalLM": "yue2",
+    "VoxCPM2Model": "voxcpm",
     "MiniMaxMusic3ForConditionalGeneration": "minimax_music3",
     "Qwen3TTSForConditionalGeneration": "qwen3tts",
     "Qwen3VLForConditionalGeneration": "qwen3vl",
@@ -367,6 +368,7 @@ MMPROJ_MODEL_MAP: dict[str, str] = {
     "FishOmniModel": "drama3",
     "Zonos3Model": "zonos3",
     "YuE2ForCausalLM": "yue2",
+    "VoxCPM2Model": "voxcpm",
     "MiniMaxMusic3ForConditionalGeneration": "minimax_music3",
     "Gemma3ForConditionalGeneration": "gemma",
     "Gemma3nForConditionalGeneration": "gemma",
diff --git a/conversion/voxcpm.py b/conversion/voxcpm.py
new file mode 100644
index 000000000..8fc855830
--- /dev/null
+++ b/conversion/voxcpm.py
@@ -0,0 +1,314 @@
+from __future__ import annotations
+
+import json
+import math
+import re
+from pathlib import Path
+from typing import Any, Callable, Iterable, TYPE_CHECKING
+
+import torch
+
+if TYPE_CHECKING:
+    from torch import Tensor
+
+from .base import ModelBase, MmprojModel, TextModel, LazyTorchTensor, gguf, logger
+from .drama3 import fold_snake
+from .minicpm import MiniCPMModel
+
+# sample rate of the AudioVAE decoder output, picks the row of the sample-rate conditioning
+VAE_OUT_SAMPLE_RATE = 48000
+
+
+def _load_config(dir_model: Path) -> dict[str, Any] | None:
+    path = dir_model / "config.json"
+    if not path.is_file():
+        return None
+    with open(path, encoding="utf-8") as f:
+        config = json.load(f)
+    return config if config.get("architecture") == "voxcpm2" else None
+
+
+def _is_voxcpm2_checkpoint(dir_model: Path) -> bool:
+    return _load_config(dir_model) is not None
+
+
+@ModelBase.register_hparams_loader(_is_voxcpm2_checkpoint)
+def _load_voxcpm2_hparams(dir_model: Path) -> dict[str, Any]:
+    logger.info("gguf: detected VoxCPM2 checkpoint")
+    config = _load_config(dir_model)
+    assert config is not None
+    if config["lm_config"].get("use_mup"):
+        raise NotImplementedError("VoxCPM2: use_mup = true is not supported")
+    if config["dit_config"].get("mean_mode"):
+        raise NotImplementedError("VoxCPM2: dit mean_mode = true is not supported")
+    if not config.get("residual_lm_no_rope"):
+        raise NotImplementedError("VoxCPM2: residual LM with RoPE is not supported")
+    for key in ("hidden_dim", "ffn_dim", "num_heads", "num_layers", "kv_channels"):
+        if config["encoder_config"][key] != config["dit_config"][key]:
+            raise NotImplementedError(f"VoxCPM2: LocEnc and LocDiT differ in {key}")
+    return {**config, "architectures": ["VoxCPM2Model"], "text_config": config["lm_config"], "audio_config": config["dit_config"]}
+
+
+@ModelBase.register("VoxCPM2Model")
+@ModelBase.example("openbmb/VoxCPM2")
+class VoxCPM2TextModel(MiniCPMModel):
+    model_arch = gguf.MODEL_ARCH.MINICPM
+
+    @classmethod
+    def filter_tensors(cls, item: tuple[str, Callable[[], Tensor]]) -> tuple[str, Callable[[], Tensor]] | None:
+        name, gen = item
+        # the text-semantic LM is a plain MiniCPM, the rest goes to the mmproj
+        if not name.startswith("base_lm."):
+            return None
+        return super().filter_tensors(("model." + name.removeprefix("base_lm."), gen))
+
+    def set_gguf_parameters(self):
+        TextModel.set_gguf_parameters(self)
+        # use_mup = false: no embedding, residual or logit scaling
+        self.gguf_writer.add_embedding_scale(1.0)
+        self.gguf_writer.add_residual_scale(1.0)
+        self.gguf_writer.add_logit_scale(1.0)
+
+    def set_vocab(self):
+        # the checkpoint has no tokenizer.model and its tokenizer class is remote code, so read tokenizer.json
+        # SPM-style BPE: a merged token scores minus its merge rank, so the SPM tokenizer merges in the BPE order
+        with open(self.dir_model / "tokenizer.json", encoding="utf-8") as f:
+            tok = json.load(f)
+        with open(self.dir_model / "tokenizer_config.json", encoding="utf-8") as f:
+            tok_cfg = json.load(f)
+        model = tok["model"]
+        if model["type"] != "BPE" or not model.get("byte_fallback"):
+            raise NotImplementedError("VoxCPM2: unexpected tokenizer type")
+
+        vocab: dict[str, int] = model["vocab"]
+        vocab_size = self.hparams["vocab_size"]
+        tokens = [f"[PAD{i}]" for i in range(vocab_size)]
+        scores = [-1e9] * vocab_size
+        toktypes = [gguf.TokenType.UNUSED] * vocab_size
+        for text, i in vocab.items():
+            tokens[i] = text
+            toktypes[i] = gguf.TokenType.BYTE if re.fullmatch(r"<0x[0-9A-Fa-f]{2}>", text) else gguf.TokenType.NORMAL
+        for rank, merge in enumerate(model["merges"]):
+            a, b = merge.split(" ") if isinstance(merge, str) else merge
+            i = vocab[a + b]
+            scores[i] = max(scores[i], -float(rank))
+
+        # the special tokens (e.g. <|audio_start|>) are only listed in tokenizer_config.json
+        specials = {int(i): t["content"] for i, t in tok_cfg.get("added_tokens_decoder", {}).items() if t.get("special")}
+        specials.update({t["id"]: t["content"] for t in tok["added_tokens"] if t.get("special")})
+        for i, text in specials.items():
+            tokens[i] = text
+            toktypes[i] = gguf.TokenType.CONTROL
+
+        self.gguf_writer.add_tokenizer_model("llama")
+        self.gguf_writer.add_tokenizer_pre("default")
+        self.gguf_writer.add_token_list(tokens)
+        self.gguf_writer.add_token_scores(scores)
+        self.gguf_writer.add_token_types(toktypes)
+        special_vocab = gguf.SpecialVocab(self.dir_model, n_vocab=len(tokens))
+        special_vocab.add_to_gguf(self.gguf_writer)
+
+
+@ModelBase.register("VoxCPM2Model")
+@ModelBase.example("openbmb/VoxCPM2")
+class VoxCPM2MmprojModel(MmprojModel):
+    has_vision_encoder = False
+    has_audio_encoder = True  # AudioVAE encoder, for voice cloning
+
+    _LAYER_MAP = {
+        "input_layernorm.weight":          "ln1",
+        "self_attn.q_proj.weight":         "attn_q",
+        "self_attn.k_proj.weight":         "attn_k",
+        "self_attn.v_proj.weight":         "attn_v",
+        "self_attn.o_proj.weight":         "attn_out",
+        "post_attention_layernorm.weight": "ln2",
+        "mlp.gate_proj.weight":            "ffn_gate",
+        "mlp.up_proj.weight":              "ffn_up",
+        "mlp.down_proj.weight":            "ffn_down",
+    }
+
+    # libllama names, mtmd loads the residual LM as a text model
+    _RES_LAYER_MAP = {
+        "input_layernorm.weight":          "attn_norm",
+        "self_attn.q_proj.weight":         "attn_q",
+        "self_attn.k_proj.weight":         "attn_k",
+        "self_attn.v_proj.weight":         "attn_v",
+        "self_attn.o_proj.weight":         "attn_output",
+        "post_attention_layernorm.weight": "ffn_norm",
+        "mlp.gate_proj.weight":            "ffn_gate",
+        "mlp.up_proj.weight":              "ffn_up",
+        "mlp.down_proj.weight":            "ffn_down",
+    }
+
+    _PROJ_MAP = {
+        "fsq_layer.in_proj":  "a.gen.fsq.in",
+        "fsq_layer.out_proj": "a.gen.fsq.out",
+        "enc_to_lm_proj":     "a.gen.enc_to_lm",
+        "lm_to_dit_proj":     "a.gen.lm_to_dit",
+        "res_to_dit_proj":    "a.gen.res_to_dit",
+        "fusion_concat_proj": "a.gen.fusion",
+        "stop_proj":          "a.gen.stop_proj",
+        "stop_head":          "a.gen.stop_head",
+        "feat_encoder.in_proj":             "a.gen.enc.in_proj",
+        "feat_decoder.estimator.in_proj":   "a.gen.dit.in_proj",
+        "feat_decoder.estimator.cond_proj": "a.gen.dit.cond_proj",
+        "feat_decoder.estimator.out_proj":  "a.gen.dit.out_proj",
+        "feat_decoder.estimator.time_mlp.linear_1": "a.gen.dit.time_mlp.0",
+        "feat_decoder.estimator.time_mlp.linear_2": "a.gen.dit.time_mlp.1",
+    }
+
+    def set_gguf_parameters(self):
+        lm  = self.global_config["lm_config"]
+        dit = self.global_config["dit_config"]
+        vae = self.global_config["audio_vae_config"]
+        self.gguf_writer.add_file_type(self.ftype)
+
+        # LocEnc + LocDiT + residual LM + AudioVAE decoder, LocEnc has the hparams of LocDiT
+        self.gguf_writer.add_clip_has_gen_audio_encoder(True)
+        self.gguf_writer.add_clip_gen_audio_projector_type(gguf.VisionProjectorType.VOXCPM2_GEN)
+        self.gguf_writer.add_gen_audio_projection_dim(self.n_embd_text)
+        self.gguf_writer.add_gen_audio_embedding_length(dit["hidden_dim"])
+        self.gguf_writer.add_gen_audio_feed_forward_length(dit["ffn_dim"])
+        self.gguf_writer.add_gen_audio_block_count(dit["num_layers"])
+        self.gguf_writer.add_gen_audio_head_count(dit["num_heads"])
+        self.gguf_writer.add_gen_audio_head_count_kv(lm["num_key_value_heads"])
+        self.gguf_writer.add_gen_audio_attention_layernorm_eps(lm["rms_norm_eps"])
+
+        # AudioVAE encoder: raw waveform -> latents, LocEnc in the gen model then makes the backbone rows
+        self.gguf_writer.add_clip_has_audio_encoder(True)
+        self.gguf_writer.add_clip_audio_projector_type(gguf.VisionProjectorType.VOXCPM2_SPKENC)
+        self.gguf_writer.add_audio_projection_dim(self.n_embd_text)
+        self.gguf_writer.add_audio_num_mel_bins(1)
+        self.gguf_writer.add_audio_block_count(0)
+        self.gguf_writer.add_audio_embedding_length(vae["encoder_dim"])
+        self.gguf_writer.add_audio_feed_forward_length(vae["encoder_dim"])
+        self.gguf_writer.add_audio_head_count(1)
+        self.gguf_writer.add_audio_attention_layernorm_eps(lm["rms_norm_eps"])
+        # the ODE solver, the RoPE factors and the VAE layout are hard-coded in tools/mtmd
+
+    def tensor_force_quant(self, name, new_name, bid, n_dims):
+        # the AudioVAE convs and the 1D tensors stay F32
+        if new_name.startswith(("a.vae.", "a.gen.vae.")) or n_dims < 2:
+            return gguf.GGMLQuantizationType.F32
+        return super().tensor_force_quant(name, new_name, bid, n_dims)
+
+    @classmethod
+    def filter_tensors(cls, item: tuple[str, Callable[[], Tensor]]) -> tuple[str, Callable[[], Tensor]] | None:
+        if item[0].startswith("base_lm."):
+            return None
+        return item
+
+    def modify_tensors(self, data_torch: Tensor, name: str, bid: int | None) -> Iterable[tuple[str, Tensor]]:
+        # already named by generate_extra_tensors()
+        if name.startswith("a."):
+            yield (name, data_torch)
+            return
+
+        # dt is always 0 (mean_mode = false), delta_time_mlp gives a constant that is folded into the time_mlp bias
+        if name.startswith("feat_decoder.estimator.delta_time_mlp."):
+            return
+        if name == "feat_decoder.estimator.time_mlp.linear_2.bias":
+            data_torch = LazyTorchTensor.to_eager(data_torch).float() + self._dtime_const()
+
+        if name == "feat_encoder.special_token":
+            yield ("a.gen.enc.special_token", data_torch.reshape(-1))
+            return
+
+        for src, dst in (("residual_lm.", "a.gen.res"), ("feat_encoder.encoder.", "a.gen.enc"),
+                         ("feat_decoder.estimator.decoder.", "a.gen.code")):
+            if not name.startswith(src):
+                continue
+            key = name.removeprefix(src)
+            if key == "norm.weight":
+                yield (f"{dst}.output_norm.weight" if dst == "a.gen.res" else f"{dst}.post_ln.weight", data_torch)
+                return
+            assert bid is not None, name
+            key = key.removeprefix(f"layers.{bid}.")
+            layer_map = self._RES_LAYER_MAP if dst == "a.gen.res" else self._LAYER_MAP
+            yield (f"{dst}.blk.{bid}.{layer_map[key]}.weight", data_torch)
+            return
+
+        base, _, suffix = name.rpartition(".")
+        yield (f"{self._PROJ_MAP[base]}.{suffix}", data_torch)
+
+    # delta_time_mlp(SinusoidalPosEmb(0)), see VoxCPMLocDiTV2.forward()
+    def _dtime_const(self) -> Tensor:
+        d = {k: LazyTorchTensor.to_eager(self.model_tensors[f"feat_decoder.estimator.delta_time_mlp.{k}"]()).float()
+             for k in ("linear_1.weight", "linear_1.bias", "linear_2.weight", "linear_2.bias")}
+        dim = d["linear_1.weight"].shape[1]
+        emb = torch.cat([torch.zeros(dim // 2), torch.ones(dim // 2)])  # sin(0), cos(0)
+        h = torch.nn.functional.silu(d["linear_1.weight"] @ emb + d["linear_1.bias"])
+        return d["linear_2.weight"] @ h + d["linear_2.bias"]
+
+    @staticmethod
+    def _wn(sd: dict[str, Tensor], prefix: str) -> Tensor:
+        # torch weight norm, over every dim but 0
+        g, v = sd[prefix + ".weight_g"].float(), sd[prefix + ".weight_v"].float()
+        return g * v / v.flatten(1).norm(dim=1).view(-1, *[1] * (v.dim() - 1))
+
+    def generate_extra_tensors(self) -> Iterable[tuple[str, Tensor]]:
+        # LongRoPE factors of LocEnc and LocDiT, picked as in MiniCPMLongRoPE._set_cos_sin_cache()
+        lm = self.global_config["lm_config"]
+        rope = lm["rope_scaling"]
+        if rope.get("type") != "longrope":
+            raise NotImplementedError(f"VoxCPM2: unexpected rope_scaling {rope.get('type')}")
+        if lm["max_position_embeddings"] > rope["original_max_position_embeddings"]:
+            raise NotImplementedError("VoxCPM2: the long RoPE factors also scale cos/sin, not supported")
+        yield ("a.gen.rope_factors", torch.tensor(rope["short_factor"], dtype=torch.float32))
+
+        path = self.dir_model / "audiovae.pth"
+        logger.info(f"gguf: loading AudioVAE from '{path}'")
+        sd = torch.load(str(path), map_location="cpu", mmap=True, weights_only=True)
+        sd = sd.get("state_dict", sd)
+        vae = self.global_config["audio_vae_config"]
+
+        def conv(dst: str, src: str) -> Iterable[tuple[str, Tensor]]:
+            yield (f"{dst}.weight", self._wn(sd, src))
+            yield (f"{dst}.bias", sd[src + ".bias"].float())
+
+        def snake(dst: str, src: str) -> Iterable[tuple[str, Tensor]]:
+            alpha, beta = fold_snake(sd[src + ".alpha"])
+            yield (f"{dst}.alpha", alpha)
+            yield (f"{dst}.beta", beta)
+
+        def res_units(dst: str, src: str, first: int) -> Iterable[tuple[str, Tensor]]:
+            # dilation 1, 3, 9: Snake, depthwise conv k = 7, Snake, conv k = 1
+            for r in range(3):
+                s = f"{src}.{first + r}.block"
+                d = f"{dst}.res.{r}"
+                yield from snake(f"{d}.snake1", f"{s}.0")
+                yield from conv(f"{d}.conv1", f"{s}.1")
+                yield from snake(f"{d}.snake2", f"{s}.2")
+                yield from conv(f"{d}.conv2", f"{s}.3")
+
+        # encoder: conv, then per stride (3 residual units, Snake, strided conv), then fc_mu
+        p = "a.vae.enc"
+        yield from conv(f"{p}.conv_in", "encoder.block.0")
+        for b in range(len(vae["encoder_rates"])):
+            s = f"encoder.block.{b + 1}.block"
+            yield from res_units(f"{p}.blk.{b}", s, 0)
+            yield from snake(f"{p}.blk.{b}.snake", f"{s}.3")
+            yield from conv(f"{p}.blk.{b}.down", f"{s}.4")
+        yield from conv(f"{p}.fc_mu", "encoder.fc_mu")
+
+        # decoder: depthwise conv, conv k = 1, then per stride (sample-rate scale and bias, Snake, transposed conv, 3 residual units), then Snake, conv
+        bounds = vae["sr_bin_boundaries"]
+        sr_idx = sum(1 for b in bounds if b < VAE_OUT_SAMPLE_RATE)  # torch.bucketize
+        if vae["out_sample_rate"] != VAE_OUT_SAMPLE_RATE:
+            raise NotImplementedError(f"VoxCPM2: unexpected out_sample_rate {vae['out_sample_rate']}")
+        p = "a.gen.vae"
+        yield from conv(f"{p}.conv_in_dw", "decoder.model.0")
+        yield from conv(f"{p}.conv_in", "decoder.model.1")
+        n_up = len(vae["decoder_rates"])
+        for b in range(n_up):
+            c = f"decoder.sr_cond_model.{b + 2}"
+            yield (f"{p}.blk.{b}.sr_scale", sd[c + ".scale_embed.weight"][sr_idx].float())
+            yield (f"{p}.blk.{b}.sr_bias",  sd[c + ".bias_embed.weight"][sr_idx].float())
+            s = f"decoder.model.{b + 2}.block"
+            yield from snake(f"{p}.blk.{b}.snake", f"{s}.0")
+            yield from conv(f"{p}.blk.{b}.up", f"{s}.1")
+            yield from res_units(f"{p}.blk.{b}", s, 2)
+        yield from snake(f"{p}.post_snake", f"decoder.model.{n_up + 2}")
+        yield from conv(f"{p}.conv_out", f"decoder.model.{n_up + 3}")
+
+        assert math.prod(vae["encoder_rates"]) * VAE_OUT_SAMPLE_RATE == math.prod(vae["decoder_rates"]) * vae["sample_rate"]
diff --git a/docs/multimodal/tts.md b/docs/multimodal/tts.md
index 5d0cc6fdb..48c6d72e5 100644
--- a/docs/multimodal/tts.md
+++ b/docs/multimodal/tts.md
@@ -17,6 +17,7 @@ Supported models:
 - [MiniMax Music 3](#minimax-music-3) (songs)
 - [Kokoro](#kokoro)
 - [tiny-voca](#tiny-voca)
+- [VoxCPM2](#voxcpm2)

 The sections below list the params supported by each model, with `llama-tts` examples. In `llama-server`, the same options are set with `--tts-lang` and `--tts-voice`, or per request (see the [`/tts` endpoint](../../tools/server/README.md#post-tts-generate-speech-audio-from-text)).

@@ -467,3 +468,45 @@ pip install git+https://github.com/n4ze3m/dizione
 python convert_hf_to_gguf.py path/to/tiny-voca --outfile tiny-voca.gguf
 python convert_hf_to_gguf.py path/to/tiny-voca --mmproj --outfile mmproj-tiny-voca.gguf
 ```
+
+## VoxCPM2
+
+[VoxCPM2](https://huggingface.co/openbmb/VoxCPM2): a MiniCPM4 backbone (2B) makes one hidden state per frame, without an audio token. The mmproj turns it into a patch of 4 continuous latents: FSQ, an 8-layer residual LM (run with libllama, one context per sequence), then the LocDiT flow matching. The causal AudioVAE decodes the latents to 48 kHz audio.
+
+Available params:
+- The language is the one of the text, `--tts-lang` is not used. A voice description in parentheses at the start of the text designs the voice, for example `(A young woman, gentle and warm)Hello!`
+- Voice cloning: `--tts-speaker-file` with the reference clip (resampled to 16 kHz, truncated to the first 30 s). `--tts-speaker-text` is not used, the transcript of the clip is not needed
+- `--temp` scales the noise of the flow matching (default 1.0), `--top-k` and `--top-p` are not used
+- The flow matching uses 10 Euler steps and classifier-free guidance with scale 2.0, like the reference. `llama-server` takes `flow_steps` (2 to 32) and `cfg_scale` to change them
+- Each frame is 4 latents, 7680 samples (160 ms). Generation stops on the stop head of the model, at least 4 frames, at most 6 frames per text token + 10 (the limit of the reference)
+- Not supported yet: the continuation mode of the reference (prompt audio with its transcript)
+- Not supported yet: the bad case retry of the reference (`retry_badcase`, a new seed when the audio reaches 6 frames per text token). Short prompts may then run long and repeat some words, try another seed
+
+Example usage:
+
+```sh
+llama-tts -m VoxCPM2.gguf \
+    -mm mmproj-VoxCPM2.gguf \
+    -p "(A calm male narrator)The quick brown fox jumps over the lazy dog." \
+    --output out.wav
+```
+
+Voice cloning:
+
+```sh
+llama-tts -m VoxCPM2.gguf \
+    -mm mmproj-VoxCPM2.gguf \
+    --tts-speaker-file reference.wav \
+    -p "Hello, this is a cloned voice." \
+    --output out.wav
+```
+
+**Note for GGUF conversion:**
+
+The mmproj holds LocEnc, LocDiT and the residual LM layers (loaded with libllama as a Granite text model with RoPE off) in the `--outtype` type, and the AudioVAE (`audiovae.pth`, encoder for voice cloning, decoder for the audio) always in F32:
+
+```sh
+hf download openbmb/VoxCPM2 --local-dir VoxCPM2
+python convert_hf_to_gguf.py VoxCPM2 --outfile VoxCPM2.gguf
+python convert_hf_to_gguf.py VoxCPM2 --mmproj --outfile mmproj-VoxCPM2.gguf
+```
diff --git a/gguf-py/gguf/constants.py b/gguf-py/gguf/constants.py
index e9e1866e4..e07ea2f4b 100644
--- a/gguf-py/gguf/constants.py
+++ b/gguf-py/gguf/constants.py
@@ -7419,6 +7419,8 @@ class VisionProjectorType:
     ZONOS3_GEN = "zonos3_gen" # audio generation: 10 codebook heads + ZAC decoder
     ZONOS3_SPKENC = "zonos3_spkenc" # audio: MECT-B2 speaker encoder
     YUE2_GEN = "yue2_gen" # audio generation: flow-matching NAR + Oobleck VAE decoder
+    VOXCPM2_GEN = "voxcpm2_gen" # audio generation: residual LM + LocDiT flow matching + AudioVAE decoder
+    VOXCPM2_SPKENC = "voxcpm2_spkenc" # audio: AudioVAE encoder, reference clip -> latents
     MINIMAX_MUSIC3_GEN = "minimax_music3_gen" # audio generation: RVQ depth decoder + flow-matching DiT + DAC decoder
     FISH_S2_GEN = "fish_s2_gen" # audio generation: fast AR code predictor + Fish DAC decoder
     FISH_S2_SPKENC = "fish_s2_spkenc" # audio: Fish DAC encoder + RVQ, reference clip -> prompt rows
diff --git a/tools/mtmd/CMakeLists.txt b/tools/mtmd/CMakeLists.txt
index 25a5549c4..9c37d60ed 100644
--- a/tools/mtmd/CMakeLists.txt
+++ b/tools/mtmd/CMakeLists.txt
@@ -88,6 +88,8 @@ add_library(mtmd
             models/fish-s2-spkenc.cpp
             models/zonos3-gen.cpp
             models/yue2-gen.cpp
+            models/voxcpm2-gen.cpp
+            models/voxcpm2-spkenc.cpp
             models/snake-vae.cpp
             models/music3-gen.cpp
             models/zonos3-spkenc.cpp
diff --git a/tools/mtmd/README-dev.md b/tools/mtmd/README-dev.md
index 55ad1c5b3..c7fec16fa 100644
--- a/tools/mtmd/README-dev.md
+++ b/tools/mtmd/README-dev.md
@@ -86,15 +86,16 @@ Due to wide variety of audio generation pipelines, the `mtmd_gen_audio` system i

 ### Running a transformer stage on libllama (mtmd-owned `llama_context`)

-Some stages are a full LLM-sized transformer that attends to the keys/values of the backbone, for example the flow-matching NAR of YuE2 (same layer shapes as the backbone, its own weights, bidirectional over its rows, and it reads the backbone KV). Do not re-implement it as an mtmd graph. Instead, mtmd can own a second `llama_model` + `llama_context` (see `mtmd_yue2_nar_load()` and `mtmd_gen_flow_yue2()` in `mtmd.cpp`):
+Some stages are a full LLM-sized transformer that attends to the keys/values of the backbone, for example the flow-matching NAR of YuE2 (same layer shapes as the backbone, its own weights, bidirectional over its rows, and it reads the backbone KV). Do not re-implement it as an mtmd graph. Instead, mtmd can own a second `llama_model` + `llama_context` (see `mtmd_yue2_nar_load()` and `mtmd_gen_flow_yue2()`, or `mtmd_voxcpm2_res_load()` and `mtmd_gen_code_voxcpm2()` in `mtmd.cpp`):

 - Conversion: store the layers in the mmproj under `a.gen.<stage>.` with the libllama tensor names of an existing arch (e.g. `a.gen.nar.blk.0.attn_q.weight`)
-- Loading: build a synthetic `gguf_context` with the arch hparams (`general.architecture`, `tokenizer.ggml.model = none`, `vocab_size = 1`, ...) and one tensor info per mmproj tensor (only the type is used, the shapes come from the hparams), then call `llama_model_init_from_user()`. The `set_tensor_data` callback reads the data from the mmproj file, so no copy is kept in the mtmd model
-    - libllama also creates every optional tensor of the arch (biases, `*.scale`, `*.input_scale`, `dense_2`, ...) for a model built from metadata. The callback must fill them with values that leave the graph unchanged: 0 for biases, 1 for scales, identity for `dense_2`. Zeros everywhere silently make every matmul output 0
+- Loading: build a synthetic `gguf_context` with the arch hparams (`general.architecture`, `tokenizer.ggml.model = none`, `vocab_size = 1`, ...) and one tensor info per mmproj tensor (only the type is used, the shapes come from the hparams), then call `llama_model_init_from_user()` (`mtmd_owned_llama_load()` does this, the caller only writes the hparams). The `set_tensor_data` callback reads the data from the mmproj file, so no copy is kept in the mtmd model
+    - libllama also creates every optional tensor of the arch (biases, `*.scale`, `*.input_scale`, `dense_2`, ...) for a model built from metadata. The callback must fill them with values that leave the graph unchanged: 0 for biases, 1 for scales and `rope_freqs`, identity for `dense_2`. Zeros everywhere silently make every matmul output 0
 - Backbone keys/values: the helper passes the backbone sequence state (`llama_state_seq_get_data()`) through `mtmd_gen_inp`, mtmd loads it into its context with `llama_state_seq_set_data()`. This only checks the layer count and the K/V types and row sizes, so it works across models with the same KV layout. The two contexts must agree on the KV type and on flash attention (`v_trans`)
 - Inputs and outputs are embedding rows (`llama_batch::embd`, `llama_get_embeddings()` with `LLAMA_POOLING_TYPE_NONE`), the projections around them stay small mtmd graphs
 - Non-causal rows: `LLAMA_ATTENTION_TYPE_NON_CAUSAL`, all rows in one ubatch (`n_ubatch` >= rows), they see every cached position of the sequence. Remove them with `llama_memory_seq_rm()` after each pass
 - Size the context per call (`n_ctx`, `n_ubatch`) instead of keeping one for the whole session
+- Causal stage with its own KV cache across calls (the residual LM of VoxCPM2): the `mtmd_gen_state` owns the `llama_context`, `mtmd_gen_state_reset()` clears its memory. Options of the arch may switch parts off, e.g. RoPE with `rope.scaling.finetuned = false` on granite

 ### Text preprocessing (the `tts` chat template)

diff --git a/tools/mtmd/models/models.h b/tools/mtmd/models/models.h
index e5e568b1b..64f7bf70d 100644
--- a/tools/mtmd/models/models.h
+++ b/tools/mtmd/models/models.h
@@ -507,7 +507,7 @@ struct mtmd_graph_qwen3tts_gen : mtmd_graph {

         // stateful conv ops: read/update their state via state_in/state_out[state_name]
         ggml_tensor * causal_conv1d(ggml_tensor * x, ggml_tensor * w, ggml_tensor * b, int dilation, const std::string & state_name) const;
-        ggml_tensor * causal_conv1d_dw(ggml_tensor * x, ggml_tensor * w, ggml_tensor * b, const std::string & state_name) const;
+        ggml_tensor * causal_conv1d_dw(ggml_tensor * x, ggml_tensor * w, ggml_tensor * b, const std::string & state_name, int dilation = 1) const;
         ggml_tensor * causal_conv_transpose1d(ggml_tensor * x, ggml_tensor * w, ggml_tensor * b, int stride, const std::string & state_name) const;
         ggml_tensor * snake(ggml_tensor * x, ggml_tensor * alpha, ggml_tensor * beta) const;

@@ -780,6 +780,66 @@ struct mtmd_graph_music3_gen : mtmd_graph_snake_vae {
     ggml_cgraph * build_vocoder();
 };

+//
+// VoxCPM2 generation, the residual LM runs on a llama_context of the gen state, see mtmd_gen_code_voxcpm2():
+// RES_INP   = backbone row -> FSQ'd hidden state, residual LM input row, stop score
+// GEN_CODE  = LocDiT flow matching with CFG-Zero* -> next patch, then LocEnc -> next backbone input row
+// LATENT_IN = LocEnc, latent patches -> backbone input rows (voice reference)
+// GEN_WAV   = causal AudioVAE decoder, latent patches -> PCM
+//
+struct mtmd_graph_voxcpm2_gen : mtmd_graph {
+    mtmd_graph_voxcpm2_gen(mtmd_subcontext * ctx, const mtmd_image_f32 & img, mtmd_subctx_gen_process gen_process,
+                           int n_rows, bool audio_row, int n_steps, float cfg_scale)
+        : mtmd_graph(ctx, img), gen_process(gen_process), n_rows(n_rows), audio_row(audio_row), n_steps(n_steps), cfg_scale(cfg_scale) {}
+    ggml_cgraph * build() override;
+
+    static constexpr int   n_latent        = 64;     // feat_dim
+    static constexpr int   n_patch         = 4;      // latents per backbone row
+    static constexpr int   n_dit_tok       = 3 + 2 * n_patch; // lm, res, time, cond patch, noisy patch
+    static constexpr int   head_dim        = 128;    // kv_channels
+    static constexpr float rope_theta      = 10000.0f;
+    static constexpr int   rope_n_ctx_orig = 32768;
+    static constexpr float fsq_scale       = 9.0f;
+    static constexpr int   n_steps_default = 10;     // inference_timesteps
+    static constexpr int   n_steps_max     = 32;
+    static constexpr float cfg_default     = 2.0f;   // cfg_value
+    static constexpr int   n_ctx_res       = 8192;   // residual LM positions of one sequence, max_length of the reference
+    static constexpr int   dec_ratio       = 1920;   // decoder samples per latent
+    static constexpr int   res_dilations[3] = { 1, 3, 9 };
+
+    // t of the Euler steps with sway sampling, n_steps + 1 values from 1 to 0
+    static std::vector<float> t_span(int n_steps);
+    // SinusoidalPosEmb(t * 1000) of the steps, [n_embd, n_steps]
+    static std::vector<float> time_embd(int n_steps, int n_embd);
+
+    mtmd_subctx_gen_process gen_process;
+    int   n_rows;
+    bool  audio_row;
+    int   n_steps;
+    float cfg_scale;
+
+    // bidirectional MiniCPM layers, x: [n_embd, n_tok, n_seq]
+    ggml_tensor * tfm(ggml_tensor * x, const std::vector<mtmd_layer> & layers, ggml_tensor * norm_w, ggml_tensor * pos) const;
+    // [n_latent, n_patch * n] -> [n_text_embd, n]
+    ggml_tensor * loc_enc(ggml_tensor * latents, int n) const;
+    ggml_tensor * inp_pos(const char * name, int n) const;
+    void build_res_inp();
+    void build_flow();
+    void build_latent_in();
+    void build_dec();
+};
+
+std::vector<c2w_state_slot> list_voxcpm2_state_slots(const mtmd_hparams & hparams, const mtmd_model & model);
+
+// AudioVAE encoder: raw waveform -> latent patches, LocEnc then runs on the gen model (LATENT_IN)
+struct mtmd_graph_voxcpm2_spkenc : mtmd_graph {
+    mtmd_graph_voxcpm2_spkenc(mtmd_subcontext * ctx, const mtmd_image_f32 & img) : mtmd_graph(ctx, img) {}
+    ggml_cgraph * build() override;
+
+    // x: [T, IC], causal: pad zeros on the left
+    ggml_tensor * conv(ggml_tensor * x, ggml_tensor * w, ggml_tensor * b, int stride, int dilation, int pad, bool depthwise) const;
+};
+
 // tiny-voca: small graphs around the stacks, the stacks run on libllama (see mtmd_gen_wav_tiny_voca())
 // LATENT_IN:  token states [n_embd, n_rows] -> duration outputs [2, n_rows]
 // LATENT_OUT: acoustic states [n_embd, n_rows] -> source-filter params [n_voc, n_rows]
diff --git a/tools/mtmd/models/qwen3tts-gen.cpp b/tools/mtmd/models/qwen3tts-gen.cpp
index 386e22db7..a453b09f5 100644
--- a/tools/mtmd/models/qwen3tts-gen.cpp
+++ b/tools/mtmd/models/qwen3tts-gen.cpp
@@ -315,18 +315,18 @@ ggml_tensor * mtmd_graph_qwen3tts_gen::code2wav::causal_conv1d(ggml_tensor * x,
     return y;
 }

-// causal depthwise conv1d, stride 1, dilation 1, kernel from w's shape.
+// causal depthwise conv1d, stride 1, kernel from w's shape.
 // x: [T, C]. w: [K, 1, C]. returns [T, C]. see causal_conv1d for the state contract.
-ggml_tensor * mtmd_graph_qwen3tts_gen::code2wav::causal_conv1d_dw(ggml_tensor * x, ggml_tensor * w, ggml_tensor * b, const std::string & state_name) const {
+ggml_tensor * mtmd_graph_qwen3tts_gen::code2wav::causal_conv1d_dw(ggml_tensor * x, ggml_tensor * w, ggml_tensor * b, const std::string & state_name, int dilation) const {
     const int K   = (int) w->ne[0];
-    const int pad = K - 1;
+    const int pad = (K - 1) * dilation;

     ggml_tensor * x_full = x;
     if (pad > 0) {
         ggml_tensor * left = state_in.at(state_name); // [pad, C]
         x_full = ggml_concat(ctx0, left, x, 0);
     }
-    ggml_tensor * y = ggml_conv_1d_dw(ctx0, w, x_full, 1, 0, 1); // [T, C, 1]
+    ggml_tensor * y = ggml_conv_1d_dw(ctx0, w, x_full, 1, 0, dilation); // [T, C, 1]
     y = ggml_reshape_2d(ctx0, y, y->ne[0], y->ne[1]);
     if (b) {
         y = ggml_add(ctx0, y, ggml_reshape_2d(ctx0, b, 1, b->ne[0]));
diff --git a/tools/mtmd/models/voxcpm2-gen.cpp b/tools/mtmd/models/voxcpm2-gen.cpp
new file mode 100644
index 000000000..649dfb07e
--- /dev/null
+++ b/tools/mtmd/models/voxcpm2-gen.cpp
@@ -0,0 +1,273 @@
+#include "models.h"
+
+#include <cmath>
+
+// linspace(1, 0) with sway sampling (coef 1), see UnifiedCFM.forward()
+std::vector<float> mtmd_graph_voxcpm2_gen::t_span(int n_steps) {
+    std::vector<float> t(n_steps + 1);
+    for (int i = 0; i <= n_steps; i++) {
+        const double x = 1.0 - (double) i / n_steps;
+        t[i] = (float) (x + std::cos(M_PI / 2 * x) - 1.0 + x);
+    }
+    return t;
+}
+
+// SinusoidalPosEmb with scale 1000: [sin, cos], the frequency step divides by half - 1
+std::vector<float> mtmd_graph_voxcpm2_gen::time_embd(int n_steps, int n_embd) {
+    const std::vector<float> t = t_span(n_steps);
+    const int half = n_embd / 2;
+    std::vector<float> out((size_t) n_embd * n_steps);
+    for (int s = 0; s < n_steps; s++) {
+        for (int j = 0; j < half; j++) {
+            const double a = 1000.0 * t[s] * std::exp(-std::log(10000.0) * j / (half - 1));
+            out[(size_t) s * n_embd + j]        = (float) std::sin(a);
+            out[(size_t) s * n_embd + half + j] = (float) std::cos(a);
+        }
+    }
+    return out;
+}
+
+ggml_tensor * mtmd_graph_voxcpm2_gen::inp_pos(const char * name, int n) const {
+    ggml_tensor * pos = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, n);
+    ggml_set_name(pos, name);
+    ggml_set_input(pos);
+    return pos;
+}
+
+ggml_tensor * mtmd_graph_voxcpm2_gen::tfm(ggml_tensor * x, const std::vector<mtmd_layer> & layers, ggml_tensor * norm_w, ggml_tensor * pos) const {
+    const int64_t n_tok     = x->ne[1];
+    const int64_t n_seq     = x->ne[2];
+    const int     n_head    = hparams.n_head;
+    const int     n_head_kv = hparams.n_head_kv;
+    const float   kq_scale  = 1.0f / sqrtf((float) head_dim);
+    ggml_tensor * factors   = model.voxcpm2.rope_factors;
+
+    for (size_t il = 0; il < layers.size(); il++) {
+        const auto & layer = layers[il];
+        ggml_tensor * cur = build_norm(x, layer.ln_1_w, nullptr, NORM_TYPE_RMS, hparams.eps, (int) il);
+
+        ggml_tensor * q = ggml_reshape_4d(ctx0, build_mm(layer.q_w, cur), head_dim, n_head,    n_tok, n_seq);
+        ggml_tensor * k = ggml_reshape_4d(ctx0, build_mm(layer.k_w, cur), head_dim, n_head_kv, n_tok, n_seq);
+        ggml_tensor * v = ggml_reshape_4d(ctx0, build_mm(layer.v_w, cur), head_dim, n_head_kv, n_tok, n_seq);
+        q = ggml_rope_ext(ctx0, q, pos, factors, head_dim, GGML_ROPE_TYPE_NEOX, rope_n_ctx_orig, rope_theta, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f);
+        k = ggml_rope_ext(ctx0, k, pos, factors, head_dim, GGML_ROPE_TYPE_NEOX, rope_n_ctx_orig, rope_theta, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f);
+
+        cur = build_attn(layer.o_w, nullptr, q, k, v, nullptr, kq_scale, (int) il);
+        x = ggml_add(ctx0, x, ggml_reshape_3d(ctx0, cur, cur->ne[0], n_tok, n_seq));
+
+        cur = build_norm(x, layer.ln_2_w, nullptr, NORM_TYPE_RMS, hparams.eps, (int) il);
+        cur = build_ffn(cur, layer.ff_up_w, nullptr, layer.ff_gate_w, nullptr, layer.ff_down_w, nullptr, FFN_SILU, (int) il);
+        x = ggml_add(ctx0, x, cur);
+    }
+    return build_norm(x, norm_w, nullptr, NORM_TYPE_RMS, hparams.eps, -1);
+}
+
+// each patch gets the special token in front, its output is the summary of the patch
+ggml_tensor * mtmd_graph_voxcpm2_gen::loc_enc(ggml_tensor * latents, int n) const {
+    const auto &  m      = model.voxcpm2;
+    const int64_t n_embd = hparams.n_embd;
+
+    ggml_tensor * x = ggml_add(ctx0, build_mm(m.enc_in_w, latents), m.enc_in_b);
+    x = ggml_reshape_3d(ctx0, x, n_embd, n_patch, n);
+    ggml_tensor * special = ggml_repeat(ctx0, ggml_reshape_3d(ctx0, m.enc_special, n_embd, 1, 1),
+                                        ggml_view_3d(ctx0, x, n_embd, 1, n, x->nb[1], x->nb[2], 0));
+    x = ggml_concat(ctx0, special, x, 1); // [n_embd, 1 + n_patch, n]
+    x = tfm(x, m.enc_layers, m.enc_norm_w, inp_pos("inp_pos_enc", 1 + n_patch));
+    x = ggml_cont(ctx0, ggml_view_2d(ctx0, x, n_embd, n, x->nb[2], 0));
+    cb(x, "loc_enc_out", -1);
+    return ggml_add(ctx0, build_mm(m.enc_to_lm_w, x), m.enc_to_lm_b);
+}
+
+void mtmd_graph_voxcpm2_gen::build_res_inp() {
+    const auto &  m      = model.voxcpm2;
+    const int64_t n_text = m.fusion_b->ne[0];
+
+    ggml_tensor * h = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_text, n_rows);
+    ggml_set_name(h, "inp_raw");
+    ggml_set_input(h);
+    ggml_tensor * x = nullptr;
+    if (audio_row) {
+        x = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_text, n_rows);
+        ggml_set_name(x, "inp_feats");
+        ggml_set_input(x);
+        // FSQ: tanh, then round to the grid of 1/9
+        ggml_tensor * q = ggml_tanh(ctx0, ggml_add(ctx0, build_mm(m.fsq_in_w, h), m.fsq_in_b));
+        q = ggml_scale(ctx0, ggml_round(ctx0, ggml_scale(ctx0, q, fsq_scale)), 1.0f / fsq_scale);
+        h = ggml_add(ctx0, build_mm(m.fsq_out_w, q), m.fsq_out_b);
+    } else {
+        x = ggml_scale(ctx0, h, 0.0f); // a text row has no input row
+    }
+
+    // logit(stop) - logit(continue), only the row of the next frame needs it
+    if (n_rows == 1) {
+        ggml_tensor * s = ggml_silu(ctx0, ggml_add(ctx0, build_mm(m.stop_proj_w, h), m.stop_proj_b));
+        s = build_mm(m.stop_head_w, s);
+        s = ggml_sub(ctx0, ggml_view_1d(ctx0, s, 1, ggml_element_size(s)), ggml_view_1d(ctx0, s, 1, 0));
+        ggml_set_name(s, "out_eos_score");
+        ggml_set_output(s);
+        ggml_build_forward_expand(gf, s);
+    }
+
+    // residual LM input rows, then the (FSQ'd) hidden states
+    ggml_tensor * r = ggml_add(ctx0, build_mm(m.fusion_w, ggml_concat(ctx0, h, x, 0)), m.fusion_b);
+    ggml_tensor * out = ggml_concat(ctx0, r, h, 1);
+    ggml_set_name(out, "out_feats");
+    ggml_set_output(out);
+    ggml_build_forward_expand(gf, out);
+}
+
+// Euler solver from noise (t = 1) to the patch (t = 0), see UnifiedCFM.solve_euler()
+void mtmd_graph_voxcpm2_gen::build_flow() {
+    const auto &  m      = model.voxcpm2;
+    const int64_t n_text = m.fusion_b->ne[0];
+    const int64_t n_embd = hparams.n_embd;
+
+    // FSQ'd hidden state, then the residual LM output
+    ggml_tensor * inp = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_text, 2);
+    ggml_set_name(inp, "inp_raw");
+    ggml_set_input(inp);
+    ggml_tensor * cond = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_latent, n_patch);
+    ggml_set_name(cond, "inp_cond");
+    ggml_set_input(cond);
+    ggml_tensor * noise = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_latent, n_patch);
+    ggml_set_name(noise, "inp_noise");
+    ggml_set_input(noise);
+    ggml_tensor * temb = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_embd, n_steps);
+    ggml_set_name(temb, "inp_temb");
+    ggml_set_input(temb);
+    ggml_tensor * pos = inp_pos("inp_pos_dit", n_dit_tok);
+
+    ggml_tensor * mu_lm  = ggml_add(ctx0, build_mm(m.lm_to_dit_w,  ggml_view_1d(ctx0, inp, n_text, 0)),          m.lm_to_dit_b);
+    ggml_tensor * mu_res = ggml_add(ctx0, build_mm(m.res_to_dit_w, ggml_view_1d(ctx0, inp, n_text, inp->nb[1])), m.res_to_dit_b);
+    ggml_tensor * mu   = ggml_concat(ctx0, ggml_reshape_2d(ctx0, mu_lm, n_embd, 1), ggml_reshape_2d(ctx0, mu_res, n_embd, 1), 1);
+    ggml_tensor * mu_0 = ggml_scale(ctx0, mu, 0.0f); // the unconditional branch has mu = 0
+
+    temb = ggml_silu(ctx0, ggml_add(ctx0, build_mm(m.time0_w, temb), m.time0_b));
+    temb = ggml_add(ctx0, build_mm(m.time1_w, temb), m.time1_b);
+    ggml_tensor * cond_tok = ggml_add(ctx0, build_mm(m.dit_cond_w, cond), m.dit_cond_b);
+
+    const std::vector<float> t = t_span(n_steps);
+    // CFG-Zero*: the first steps keep x as is
+    const int n_zero = std::max(1, (int) ((n_steps + 1) * 0.04));
+
+    ggml_tensor * x = noise;
+    for (int s = n_zero; s < n_steps; s++) {
+        ggml_tensor * t_tok = ggml_view_2d(ctx0, temb, n_embd, 1, temb->nb[1], (size_t) s * temb->nb[1]);
+        ggml_tensor * x_tok = ggml_add(ctx0, build_mm(m.dit_in_w, x), m.dit_in_b);
+        ggml_tensor * tail  = ggml_concat(ctx0, ggml_concat(ctx0, t_tok, cond_tok, 1), x_tok, 1);
+        ggml_tensor * h = ggml_concat(ctx0,
+            ggml_reshape_3d(ctx0, ggml_concat(ctx0, mu,   tail, 1), n_embd, n_dit_tok, 1),
+            ggml_reshape_3d(ctx0, ggml_concat(ctx0, mu_0, tail, 1), n_embd, n_dit_tok, 1), 2);
+        h = tfm(h, model.layers, model.post_ln_w, pos);
+        h = ggml_cont(ctx0, ggml_view_3d(ctx0, h, n_embd, n_patch, 2, h->nb[1], h->nb[2], (size_t) (n_dit_tok - n_patch) * h->nb[1]));
+        h = ggml_add(ctx0, build_mm(m.dit_out_w, h), m.dit_out_b); // [n_latent, n_patch, 2]
+
+        // guided velocity, the unconditional one is scaled by its projection on the conditional one
+        ggml_tensor * v_c = ggml_view_2d(ctx0, h, n_latent, n_patch, h->nb[1], 0);
+        ggml_tensor * v_u = ggml_view_2d(ctx0, h, n_latent, n_patch, h->nb[1], h->nb[2]);
+        ggml_tensor * st  = ggml_div(ctx0, ggml_sum(ctx0, ggml_mul(ctx0, v_c, v_u)),
+                                           ggml_scale_bias(ctx0, ggml_sum(ctx0, ggml_sqr(ctx0, v_u)), 1.0f, 1e-8f));
+        ggml_tensor * v_s = ggml_mul(ctx0, v_u, st);
+        ggml_tensor * v   = ggml_add(ctx0, v_s, ggml_scale(ctx0, ggml_sub(ctx0, v_c, v_s), cfg_scale));
+        x = ggml_add(ctx0, x, ggml_scale(ctx0, v, t[s + 1] - t[s]));
+    }
+    GGML_ASSERT(x != noise && "voxcpm2: not enough flow steps");
+    ggml_set_name(x, "out_feats");
+    ggml_set_output(x);
+    ggml_build_forward_expand(gf, x);
+
+    // next backbone input row, the last node
+    ggml_build_forward_expand(gf, loc_enc(x, 1));
+}
+
+void mtmd_graph_voxcpm2_gen::build_latent_in() {
+    ggml_tensor * inp = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_latent, n_patch * n_rows);
+    ggml_set_name(inp, "inp_feats");
+    ggml_set_input(inp);
+    ggml_tensor * out = loc_enc(inp, n_rows);
+    ggml_set_name(out, "out_feats");
+    ggml_set_output(out);
+    ggml_build_forward_expand(gf, out);
+}
+
+std::vector<c2w_state_slot> list_voxcpm2_state_slots(const mtmd_hparams & /*hparams*/, const mtmd_model & model) {
+    const auto & m = model.voxcpm2;
+    std::vector<c2w_state_slot> slots;
+    slots.push_back({"in_dw", m.dec_in_dw_w->ne[0] - 1, m.dec_in_dw_w->ne[2]});
+    for (size_t ib = 0; ib < m.dec.size(); ib++) {
+        const auto & blk = m.dec[ib];
+        const std::string name = "up" + std::to_string(ib);
+        slots.push_back({name, blk.conv_w->ne[0] / 2, blk.conv_w->ne[1]});
+        for (size_t ir = 0; ir < blk.res.size(); ir++) {
+            const auto & ru = blk.res[ir];
+            slots.push_back({name + "_res" + std::to_string(ir),
+                             (ru.conv1_w->ne[0] - 1) * mtmd_graph_voxcpm2_gen::res_dilations[ir], ru.conv1_w->ne[2]});
+        }
+    }
+    slots.push_back({"out", m.dec_out_w->ne[0] - 1, m.dec_out_w->ne[1]});
+    return slots;
+}
+
+// causal AudioVAE decoder, the conv states carry the left context between calls
+void mtmd_graph_voxcpm2_gen::build_dec() {
+    const auto & m = model.voxcpm2;
+
+    ggml_tensor * inp = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_latent, n_patch * n_rows);
+    ggml_set_name(inp, "inp_feats");
+    ggml_set_input(inp);
+
+    mtmd_graph_qwen3tts_gen::code2wav dec(*this);
+    for (const auto & slot : list_voxcpm2_state_slots(hparams, model)) {
+        ggml_tensor * t = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, slot.ne0, slot.ne1);
+        ggml_set_name(t, ("state_in_" + slot.name).c_str());
+        ggml_set_input(t);
+        dec.state_in[slot.name] = t;
+    }
+
+    ggml_tensor * x = ggml_cont(ctx0, ggml_transpose(ctx0, inp)); // [T, n_latent]
+    x = dec.causal_conv1d_dw(x, m.dec_in_dw_w, m.dec_in_dw_b, "in_dw");
+    x = dec.causal_conv1d(x, m.dec_in_w, m.dec_in_b, 1, "");
+    for (size_t ib = 0; ib < m.dec.size(); ib++) {
+        const auto & blk  = m.dec[ib];
+        const std::string name = "up" + std::to_string(ib);
+        const int64_t n_ch = m.dec_sr_scale[ib]->ne[0];
+
+        // sample-rate conditioning, the row of the output rate is picked at conversion
+        x = ggml_add(ctx0, ggml_mul(ctx0, x, ggml_reshape_2d(ctx0, m.dec_sr_scale[ib], 1, n_ch)),
+                           ggml_reshape_2d(ctx0, m.dec_sr_bias[ib], 1, n_ch));
+        x = dec.snake(x, blk.snake_alpha, blk.snake_beta);
+        x = dec.causal_conv_transpose1d(x, blk.conv_w, blk.conv_b, (int) (blk.conv_w->ne[0] / 2), name);
+        for (size_t ir = 0; ir < blk.res.size(); ir++) {
+            const auto & ru = blk.res[ir];
+            ggml_tensor * h = dec.snake(x, ru.act1_alpha, ru.act1_beta);
+            h = dec.causal_conv1d_dw(h, ru.conv1_w, ru.conv1_b, name + "_res" + std::to_string(ir), res_dilations[ir]);
+            h = dec.snake(h, ru.act2_alpha, ru.act2_beta);
+            h = dec.causal_conv1d(h, ru.conv2_w, ru.conv2_b, 1, "");
+            x = ggml_add(ctx0, x, h);
+        }
+        cb(x, "vae_block", (int) ib);
+    }
+    x = dec.snake(x, m.dec_post_alpha, m.dec_post_beta);
+    x = dec.causal_conv1d(x, m.dec_out_w, m.dec_out_b, 1, "out");
+    x = ggml_tanh(ctx0, ggml_reshape_1d(ctx0, x, x->ne[0]));
+    ggml_set_name(x, "out_audio");
+    ggml_set_output(x);
+    ggml_build_forward_expand(gf, x);
+
+    for (auto & slot : dec.state_out) {
+        ggml_set_name(slot.second, ("state_out_" + slot.first).c_str());
+        ggml_set_output(slot.second);
+        ggml_build_forward_expand(gf, slot.second);
+    }
+}
+
+ggml_cgraph * mtmd_graph_voxcpm2_gen::build() {
+    switch (gen_process) {
+        case MTMD_SUBCTX_GEN_PROCESS_RES_INP:   build_res_inp();   break;
+        case MTMD_SUBCTX_GEN_PROCESS_GEN_CODE:  build_flow();      break;
+        case MTMD_SUBCTX_GEN_PROCESS_LATENT_IN: build_latent_in(); break;
+        case MTMD_SUBCTX_GEN_PROCESS_GEN_WAV:   build_dec();       break;
+        default: GGML_ABORT("voxcpm2: unknown gen_process");
+    }
+    return gf;
+}
diff --git a/tools/mtmd/models/voxcpm2-spkenc.cpp b/tools/mtmd/models/voxcpm2-spkenc.cpp
new file mode 100644
index 000000000..ea5d7efaa
--- /dev/null
+++ b/tools/mtmd/models/voxcpm2-spkenc.cpp
@@ -0,0 +1,59 @@
+#include "models.h"
+
+ggml_tensor * mtmd_graph_voxcpm2_spkenc::conv(ggml_tensor * x, ggml_tensor * w, ggml_tensor * b, int stride, int dilation, int pad, bool depthwise) const {
+    if (pad > 0) {
+        x = ggml_pad_ext(ctx0, x, pad, 0, 0, 0, 0, 0, 0, 0);
+    }
+    // im2col in F32: the reference AudioVAE runs in FP32
+    ggml_tensor * y = nullptr;
+    if (depthwise) {
+        ggml_tensor * col = ggml_im2col(ctx0, w, ggml_reshape_4d(ctx0, x, x->ne[0], 1, x->ne[1], 1), stride, 0, 0, 0, dilation, 0, false, GGML_TYPE_F32);
+        y = ggml_mul_mat(ctx0, col, w); // [T, 1, C]
+        y = ggml_reshape_2d(ctx0, y, y->ne[0], y->ne[2]);
+    } else {
+        ggml_tensor * col = ggml_im2col(ctx0, w, x, stride, 0, 0, 0, dilation, 0, false, GGML_TYPE_F32); // [IC * K, T]
+        y = ggml_mul_mat(ctx0,
+                ggml_reshape_2d(ctx0, col, col->ne[0], col->ne[1] * col->ne[2]),
+                ggml_reshape_2d(ctx0, w, w->ne[0] * w->ne[1], w->ne[2])); // [T, OC]
+    }
+    if (b) {
+        y = ggml_add(ctx0, y, ggml_reshape_2d(ctx0, b, 1, b->ne[0]));
+    }
+    return y;
+}
+
+// causal AudioVAE encoder: waveform -> mu of the latents, 4 latents per output row
+ggml_cgraph * mtmd_graph_voxcpm2_spkenc::build() {
+    using g = mtmd_graph_voxcpm2_gen;
+    const auto & m = model.voxcpm2;
+    mtmd_graph_qwen3tts_gen::code2wav act(*this);
+
+    // the preprocessor hands over the waveform as a single-row "mel"
+    ggml_tensor * inp_raw = build_inp_raw(1);
+    ggml_tensor * x = ggml_reshape_2d(ctx0, inp_raw, inp_raw->ne[0], 1); // [T, 1]
+
+    x = conv(x, m.enc_conv_in_w, m.enc_conv_in_b, 1, 1, (int) m.enc_conv_in_w->ne[0] - 1, false);
+    for (size_t ib = 0; ib < m.enc.size(); ib++) {
+        const auto & blk = m.enc[ib];
+        for (size_t ir = 0; ir < blk.res.size(); ir++) {
+            const auto & ru  = blk.res[ir];
+            const int    dil = g::res_dilations[ir];
+            ggml_tensor * h = act.snake(x, ru.act1_alpha, ru.act1_beta);
+            h = conv(h, ru.conv1_w, ru.conv1_b, 1, dil, (int) (ru.conv1_w->ne[0] - 1) * dil, true);
+            h = act.snake(h, ru.act2_alpha, ru.act2_beta);
+            h = conv(h, ru.conv2_w, ru.conv2_b, 1, 1, 0, false);
+            x = ggml_add(ctx0, x, h);
+        }
+        x = act.snake(x, blk.snake_alpha, blk.snake_beta);
+        // kernel 2 * stride, the causal padding is one stride
+        const int stride = (int) (blk.conv_w->ne[0] / 2);
+        x = conv(x, blk.conv_w, blk.conv_b, stride, 1, stride, false);
+        cb(x, "vae_enc_block", (int) ib);
+    }
+    x = conv(x, m.enc_mu_w, m.enc_mu_b, 1, 1, (int) m.enc_mu_w->ne[0] - 1, false); // [T / hop, n_latent]
+
+    x = ggml_cont(ctx0, ggml_transpose(ctx0, x));
+    x = ggml_reshape_2d(ctx0, x, g::n_latent * g::n_patch, x->ne[1] / g::n_patch);
+    ggml_build_forward_expand(gf, x);
+    return gf;
+}
diff --git a/tools/mtmd/mtmd-audio.cpp b/tools/mtmd/mtmd-audio.cpp
index 638648ab9..53c2cbb27 100644
--- a/tools/mtmd/mtmd-audio.cpp
+++ b/tools/mtmd/mtmd-audio.cpp
@@ -2250,11 +2250,11 @@ bool mtmd_audio_preprocessor_pockettts::process(const float *                 sa
     return true;
 }

-bool mtmd_audio_preprocessor_fish_s2::process(const float * samples, size_t n_samples, std::vector<mtmd_audio_mel> & output, bool shape_only) const {
-    const size_t max_samples = (size_t) mtmd_hparams::fish_s2_max_spk_seconds * hparams.audio_sample_rate;
+bool mtmd_audio_preprocessor_spk_ref::process(const float * samples, size_t n_samples, std::vector<mtmd_audio_mel> & output, bool shape_only) const {
+    const size_t max_samples = (size_t) max_seconds * hparams.audio_sample_rate;
     if (n_samples > max_samples) {
         LOG_WRN("%s: speaker reference is %.1f s, truncating to the first %d s\n", __func__,
-                (double) n_samples / hparams.audio_sample_rate, mtmd_hparams::fish_s2_max_spk_seconds);
+                (double) n_samples / hparams.audio_sample_rate, max_seconds);
         n_samples = max_samples;
     }
     return mtmd_audio_preprocessor_muse_spark_audio::process(samples, n_samples, output, shape_only);
diff --git a/tools/mtmd/mtmd-audio.h b/tools/mtmd/mtmd-audio.h
index 117d2bb77..6d282721d 100644
--- a/tools/mtmd/mtmd-audio.h
+++ b/tools/mtmd/mtmd-audio.h
@@ -134,10 +134,13 @@ struct mtmd_audio_preprocessor_muse_spark_audio : mtmd_audio_preprocessor {
     bool process(const float * samples, size_t n_samples, std::vector<mtmd_audio_mel> & output, bool shape_only) const override;
 };

-// fish_s2 speaker reference: same as muse spark (raw waveform, whole frames), truncated to fish_s2_max_spk_seconds
-struct mtmd_audio_preprocessor_fish_s2 : mtmd_audio_preprocessor_muse_spark_audio {
-    using mtmd_audio_preprocessor_muse_spark_audio::mtmd_audio_preprocessor_muse_spark_audio;
+// speaker reference (fish_s2, voxcpm2): same as muse spark (raw waveform, whole frames), truncated to max_seconds
+struct mtmd_audio_preprocessor_spk_ref : mtmd_audio_preprocessor_muse_spark_audio {
+    mtmd_audio_preprocessor_spk_ref(const mtmd_model * model, int32_t max_seconds)
+        : mtmd_audio_preprocessor_muse_spark_audio(model), max_seconds(max_seconds) {}
     bool process(const float * samples, size_t n_samples, std::vector<mtmd_audio_mel> & output, bool shape_only) const override;
+
+    const int32_t max_seconds;
 };

 // vibevoice: gain to -25 dBFS RMS, without clipping the peak
diff --git a/tools/mtmd/mtmd-graph.cpp b/tools/mtmd/mtmd-graph.cpp
index 24469c14e..81ae155ac 100644
--- a/tools/mtmd/mtmd-graph.cpp
+++ b/tools/mtmd/mtmd-graph.cpp
@@ -977,6 +977,10 @@ std::unique_ptr<mtmd_graph> mtmd_get_graph_builder(mtmd_subcontext * ctx, const
             {
                 builder = std::make_unique<mtmd_graph_fish_s2_spkenc>(ctx, img);
             } break;
+        case PROJECTOR_TYPE_VOXCPM2_SPKENC:
+            {
+                builder = std::make_unique<mtmd_graph_voxcpm2_spkenc>(ctx, img);
+            } break;
         case PROJECTOR_TYPE_POCKETTTS_GEN:
             {
                 const auto gen_process = params ? params->gen_process : MTMD_SUBCTX_GEN_PROCESS_GEN_CODE;
@@ -1038,6 +1042,24 @@ std::unique_ptr<mtmd_graph> mtmd_get_graph_builder(mtmd_subcontext * ctx, const
                                      ? (int) (params->codes->size() / n_cb) : 1;
                 builder = std::make_unique<mtmd_graph_zonos3_gen>(ctx, img, gen_process, all_known, n_frames);
             } break;
+        case PROJECTOR_TYPE_VOXCPM2_GEN:
+            {
+                using g = mtmd_graph_voxcpm2_gen;
+                // the default (warmup) graph is one frame of the flow
+                const auto gen_process = params ? params->gen_process : MTMD_SUBCTX_GEN_PROCESS_GEN_CODE;
+                int n_rows = 1;
+                if (gen_process == MTMD_SUBCTX_GEN_PROCESS_RES_INP) {
+                    // hidden states in img, the input rows of audio rows in feats
+                    n_rows = img.ny();
+                } else if (params && params->feats && gen_process != MTMD_SUBCTX_GEN_PROCESS_GEN_CODE) {
+                    GGML_ASSERT(!params->feats->empty() && params->feats->size() % (g::n_latent * g::n_patch) == 0);
+                    n_rows = (int) (params->feats->size() / (g::n_latent * g::n_patch));
+                }
+                const bool  audio_row = gen_process == MTMD_SUBCTX_GEN_PROCESS_RES_INP && params->feats != nullptr;
+                const int   n_steps   = params && params->n_steps > 0 ? params->n_steps : g::n_steps_default;
+                const float cfg_scale = params && params->cfg_scale > 0.0f ? params->cfg_scale : g::cfg_default;
+                builder = std::make_unique<mtmd_graph_voxcpm2_gen>(ctx, img, gen_process, n_rows, audio_row, n_steps, cfg_scale);
+            } break;
         case PROJECTOR_TYPE_YUE2_GEN:
             {
                 const auto gen_process = params ? params->gen_process : MTMD_SUBCTX_GEN_PROCESS_LATENT_IN;
diff --git a/tools/mtmd/mtmd-helper-gen.cpp b/tools/mtmd/mtmd-helper-gen.cpp
index ef9cfeefb..32f1d39d4 100644
--- a/tools/mtmd/mtmd-helper-gen.cpp
+++ b/tools/mtmd/mtmd-helper-gen.cpp
@@ -4914,6 +4914,433 @@ private:
     bool   wav_header_sent = false;
 };

+// VoxCPM2: the backbone emits no token, mtmd turns each hidden state into a patch of AudioVAE latents
+// every backbone row also goes through the residual LM inside mtmd, see mtmd_gen_code_voxcpm2()
+class voxcpm2_gen_audio_pipeline : public mtmd_gen_audio_pipeline {
+public:
+    using mtmd_gen_audio_pipeline::mtmd_gen_audio_pipeline;
+
+    // step_gen() stops at the limit of the reference, which depends on the text
+    int32_t max_frames() const override { return 0; }
+    bool supports_cfg() const override { return true; }
+    bool supports_flow_steps() const override { return true; }
+
+    void reset() override {
+        seq_id = 0;
+        pos = 0;
+        rows.clear();
+        voice.clear();
+        prompt_pos = 0;
+        n_frames = 0;
+        max_len = 0;
+        h_state_buf.clear();
+        embd_in.clear();
+        latents.clear();
+        n_decoded = 0;
+        gen_state.reset();
+        audio_pcm.clear();
+        out_buf.clear();
+        pcm_sent = 0;
+        wav_header_sent = false;
+    }
+
+    int32_t set_input(const mtmd_helper_gen_audio_inp * inp) override {
+        reset();
+        seq_id     = inp->seq_id;
+        out_type   = inp->out_type;
+        stream     = inp->stream;
+        temp       = inp->temp;
+        cfg_scale  = inp->cfg_scale;
+        flow_steps = inp->flow_steps;
+        if (flow_steps != 0 && (flow_steps < 2 || flow_steps > 32)) {
+            LOG_ERR("mtmd_helper_gen_audio: voxcpm2 needs flow_steps in [2, 32] (0 = model default)\n");
+            return 2;
+        }
+        if (inp->speaker_ref_text) {
+            LOG_WRN("mtmd_helper_gen_audio: voxcpm2 clones the voice without its transcript, the speaker text is ignored\n");
+        }
+        if (!ensure_cache()) {
+            return 1;
+        }
+
+        // re.sub(r"\s+", " ", text) as in VoxCPM._generate() of core.py, a leading or trailing space is kept
+        std::string text;
+        bool space = false;
+        for (char c : std::string(inp->prompt, inp->prompt_len)) {
+            const bool is_space = std::isspace((unsigned char) c);
+            if (!is_space || !space) {
+                text += is_space ? ' ' : c;
+            }
+            space = is_space;
+        }
+        std::vector<llama_token> ids = tokenize(text);
+        if (ids.empty()) {
+            LOG_ERR("mtmd_helper_gen_audio: empty prompt\n");
+            return 1;
+        }
+        // retry_badcase_ratio_threshold * text length + 10, capped by max_len of the reference
+        max_len = std::min((int) ids.size() * 6 + 10, 4096);
+
+        // [<|audio_prompt_start|>, voice rows, <|audio_prompt_end|>,] text, <|audio_start|>
+        if (inp->speaker_ref) {
+            if (!encode_speaker(inp->speaker_ref, voice)) {
+                return 1;
+            }
+            rows.push_back({ tok_ref_start, -1 });
+            for (int i = 0; i < (int) (voice.size() / n_embd); i++) {
+                rows.push_back({ LLAMA_TOKEN_NULL, i });
+            }
+            rows.push_back({ tok_ref_end, -1 });
+        }
+        for (llama_token t : ids) {
+            rows.push_back({ t, -1 });
+        }
+        rows.push_back({ tok_audio_start, -1 });
+
+        // the residual LM reads the prompt rows, then one row per frame after the first
+        max_len = std::min(max_len, n_ctx_res + 1 - (int) rows.size());
+        if (max_len <= 0) {
+            LOG_ERR("mtmd_helper_gen_audio: the prompt is too long for the residual LM (%d rows)\n", n_ctx_res);
+            return 1;
+        }
+        if (rows.size() + (size_t) max_len >= llama_n_ctx_seq(lctx)) {
+            LOG_WRN("mtmd_helper_gen_audio: the context may be too small for the prompt and %d frames\n", max_len);
+        }
+        if (!llama_memory_seq_rm(llama_get_memory(lctx), seq_id, 0, -1)) {
+            LOG_ERR("mtmd_helper_gen_audio: cannot reset the sequence\n");
+            return 1;
+        }
+        return init_gen_state(inp->seed) ? 0 : 1;
+    }
+
+    // the residual LM reads every prompt row but the last, which makes the first frame
+    int32_t step_prompt(int32_t n_batch) override {
+        GGML_ASSERT(n_batch > 0);
+        const int n_prompt = (int) rows.size();
+        if (prompt_pos >= n_prompt) {
+            return 0;
+        }
+        // a batch is either text tokens or voice rows; small batches, every row outputs its hidden state
+        const bool is_voice = rows[prompt_pos].voice >= 0;
+        int n = 0;
+        while (n < std::min(n_batch, 64) && prompt_pos + n < n_prompt && (rows[prompt_pos + n].voice >= 0) == is_voice) {
+            n++;
+        }
+
+        if (is_voice) {
+            decode_embd_batch batch(voice.data() + (size_t) rows[prompt_pos].voice * n_embd, n, 1, n_embd);
+            batch.set_position_normal(pos, seq_id);
+            std::fill(batch.logits.begin(), batch.logits.end(), 1);
+            if (llama_process(lctx, LLAMA_PROCESS_TYPE_DECODE, batch.render(lctx, 0, n)) != 0) {
+                LOG_ERR("mtmd_helper_gen_audio: prompt decode failed\n");
+                return -1;
+            }
+        } else {
+            std::vector<llama_token> toks(n);
+            for (int i = 0; i < n; i++) {
+                toks[i] = rows[prompt_pos + i].tok;
+            }
+            llama_batch_ext_ptr batch(llama_batch_ext_init(lctx));
+            if (!batch_set_tokens(batch.get(), toks.data(), n, pos, seq_id)) {
+                return -1;
+            }
+            for (int i = 0; i < n; i++) {
+                llama_batch_ext_set_output_logits(batch.get(), i, true);
+            }
+            if (llama_process(lctx, LLAMA_PROCESS_TYPE_DECODE, batch.get()) != 0) {
+                LOG_ERR("mtmd_helper_gen_audio: prompt decode failed\n");
+                return -1;
+            }
+        }
+
+        // the rows go to the residual LM together, the first frame comes from the last prompt row
+        const bool last = prompt_pos + n == n_prompt;
+        const int  n_res = last ? n - 1 : n;
+        std::vector<float> h_rows((size_t) n_res * n_embd);
+        for (int i = 0; i < n_res; i++) {
+            const float * h = llama_get_embeddings_ith(lctx, i);
+            std::copy(h, h + n_embd, h_rows.begin() + (size_t) i * n_embd);
+        }
+        if (last) {
+            const float * h = llama_get_embeddings_ith(lctx, n - 1);
+            h_state_buf.assign(h, h + n_embd);
+        }
+        const float * x = is_voice ? voice.data() + (size_t) rows[prompt_pos].voice * n_embd : nullptr;
+        if (n_res > 0 && !run_row(h_rows.data(), x, -1, nullptr, n_res)) {
+            return -1;
+        }
+        pos        += n;
+        prompt_pos += n;
+        return n_prompt - prompt_pos;
+    }
+
+    // the hidden state of the caller is not read, the server may decode other sequences in between
+    int32_t step_gen(llama_token sampled, const float * h_state_in, const float ** h_state_out, bool * out_stop) override {
+        GGML_UNUSED(sampled);
+        GGML_UNUSED(h_state_in);
+        *h_state_out = nullptr;
+        *out_stop    = false;
+        if (n_frames >= max_len || (uint32_t) pos >= llama_n_ctx_seq(lctx)) {
+            *out_stop = true;
+            return 0;
+        }
+
+        mtmd_gen_out out{};
+        if (!run_row(h_state_buf.data(), embd_in.empty() ? nullptr : embd_in.data(), 0, &out)) {
+            return 1;
+        }
+        latents.insert(latents.end(), out.feats, out.feats + out.n_feats);
+        n_frames++;
+        *h_state_out = h_state_buf.data();
+
+        // the frame of the stopping step is kept, see the loop of VoxCPM2Model._inference()
+        if ((out.is_eos && n_frames > min_len + 1) || n_frames >= max_len) {
+            *out_stop = true;
+            return 0;
+        }
+
+        embd_in.assign(out.embd, out.embd + n_embd);
+        decode_embd_batch batch(embd_in.data(), 1, 1, n_embd);
+        batch.set_position_normal(pos, seq_id);
+        batch.logits[0] = 1;
+        if (llama_process(lctx, LLAMA_PROCESS_TYPE_DECODE, batch.render(lctx, 0, 1)) != 0) {
+            LOG_ERR("mtmd_helper_gen_audio: decode failed\n");
+            return 1;
+        }
+        pos++;
+        const float * h = llama_get_embeddings_ith(lctx, -1);
+        h_state_buf.assign(h, h + n_embd);
+
+        if (stream && latents.size() / n_frame - n_decoded >= stream_frames) {
+            return decode_wav() ? 0 : 1;
+        }
+        return 0;
+    }
+
+    int32_t get_output(int32_t * out_sample_rate, const char ** out_data, size_t * out_data_len, int64_t * out_n_samples) override {
+        *out_sample_rate = info.sample_rate;
+
+        if (!stream) {
+            if (flush() != 0) {
+                return 1;
+            }
+            if (out_n_samples) {
+                *out_n_samples = (int64_t) audio_pcm.size();
+            }
+            if (out_type == MTMD_HELPER_GEN_AUDIO_OUTTYPE_PCM) {
+                *out_data     = (const char *) audio_pcm.data();
+                *out_data_len = audio_pcm.size() * sizeof(float);
+                return 0;
+            }
+            out_buf.clear();
+            if (!write_wav16(out_buf, audio_pcm, info.sample_rate)) {
+                LOG_ERR("mtmd_helper_gen_audio: output too large for WAV\n");
+                return 1;
+            }
+            *out_data     = out_buf.data();
+            *out_data_len = out_buf.size();
+            return 0;
+        }
+
+        // streaming: only the audio since the previous call
+        const size_t n_new = audio_pcm.size() - pcm_sent;
+        if (out_n_samples) {
+            *out_n_samples = (int64_t) n_new;
+        }
+        if (out_type == MTMD_HELPER_GEN_AUDIO_OUTTYPE_PCM) {
+            *out_data     = (const char *) (audio_pcm.data() + pcm_sent);
+            *out_data_len = n_new * sizeof(float);
+            pcm_sent = audio_pcm.size();
+            return 0;
+        }
+        out_buf.clear();
+        if (!wav_header_sent) {
+            write_wav16_header(out_buf, UINT32_MAX, info.sample_rate);
+            wav_header_sent = true;
+        }
+        append_wav16_pcm(out_buf, audio_pcm.data() + pcm_sent, n_new);
+        pcm_sent = audio_pcm.size();
+        *out_data     = out_buf.data();
+        *out_data_len = out_buf.size();
+        return 0;
+    }
+
+    int32_t flush() override {
+        return decode_wav() ? 0 : 1;
+    }
+
+private:
+    struct prompt_row {
+        llama_token tok;   // text row
+        int         voice; // row of the voice reference, -1 for a text row
+    };
+
+    bool ensure_cache() {
+        if (cache_ready) {
+            return true;
+        }
+        tok_audio_start = find_special_token(vocab, "<|audio_start|>");
+        tok_ref_start   = find_special_token(vocab, "<|audio_prompt_start|>");
+        tok_ref_end     = find_special_token(vocab, "<|audio_prompt_end|>");
+        if (tok_audio_start == LLAMA_TOKEN_NULL || tok_ref_start == LLAMA_TOKEN_NULL || tok_ref_end == LLAMA_TOKEN_NULL) {
+            LOG_ERR("mtmd_helper_gen_audio: missing voxcpm2 audio tokens in vocab\n");
+            return false;
+        }
+
+        // multi-character CJK tokens are split into single characters, see mask_multichar_chinese_tokens()
+        // a token is split when its text without the SPM space (U+2581) is a CJK token of the vocab, each char maps to its plain token
+        const std::string spm_space = "\xe2\x96\x81";
+        const int32_t n_vocab = llama_vocab_n_tokens(vocab);
+        std::unordered_map<std::string, llama_token> pieces;
+        for (llama_token id = 0; id < n_vocab; id++) {
+            pieces.emplace(llama_vocab_get_text(vocab, id), id);
+        }
+        for (llama_token id = 0; id < n_vocab; id++) {
+            std::string clean = llama_vocab_get_text(vocab, id);
+            for (size_t i; (i = clean.find(spm_space)) != std::string::npos;) {
+                clean.erase(i, spm_space.size());
+            }
+            if (clean.size() < 6 || !is_cjk(clean) || pieces.find(clean) == pieces.end()) {
+                continue;
+            }
+            std::vector<llama_token> ids;
+            for (size_t i = 0; i < clean.size(); i += 3) {
+                auto it = pieces.find(clean.substr(i, 3));
+                if (it == pieces.end()) {
+                    break;
+                }
+                ids.push_back(it->second);
+            }
+            if (ids.size() * 3 == clean.size()) {
+                split_cjk.emplace(id, std::move(ids));
+            }
+        }
+        cache_ready = true;
+        return true;
+    }
+
+    // chars of U+4E00..U+9FFF only, each is 3 bytes in UTF-8
+    static bool is_cjk(const std::string & s) {
+        if (s.empty() || s.size() % 3 != 0) {
+            return false;
+        }
+        for (size_t i = 0; i < s.size(); i += 3) {
+            const auto b0 = (unsigned char) s[i];
+            const auto b1 = (unsigned char) s[i + 1];
+            const auto b2 = (unsigned char) s[i + 2];
+            if ((b0 & 0xF0) != 0xE0 || (b1 & 0xC0) != 0x80 || (b2 & 0xC0) != 0x80) {
+                return false;
+            }
+            const uint32_t cp = ((b0 & 0x0F) << 12) | ((b1 & 0x3F) << 6) | (b2 & 0x3F);
+            if (cp < 0x4E00 || cp > 0x9FFF) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    // no BOS, special tokens in the text are plain text, like tokenize() of the reference
+    std::vector<llama_token> tokenize(const std::string & text) const {
+        std::vector<llama_token> ids(text.size() + 16);
+        const int32_t n = llama_tokenize(vocab, text.c_str(), (int32_t) text.size(), ids.data(), (int32_t) ids.size(), false, false);
+        if (n < 0) {
+            LOG_ERR("mtmd_helper_gen_audio: tokenization failed\n");
+            return {};
+        }
+        std::vector<llama_token> out;
+        for (int32_t i = 0; i < n; i++) {
+            auto it = split_cjk.find(ids[i]);
+            if (it != split_cjk.end()) {
+                out.insert(out.end(), it->second.begin(), it->second.end());
+            } else {
+                out.push_back(ids[i]);
+            }
+        }
+        return out;
+    }
+
+    // backbone rows through mtmd: code0 < 0 for n_rows prompt rows, else one row and the next frame goes to out
+    bool run_row(const float * h, const float * x, int32_t code0, mtmd_gen_out * out = nullptr, int n_rows = 1) {
+        mtmd_gen_inp inp = mtmd_gen_inp_default(mctx);
+        inp.type    = MTMD_GEN_PROCESS_TYPE_GEN_CODE;
+        inp.code0   = code0;
+        inp.embd    = const_cast<float *>(h);
+        inp.feats   = x;
+        inp.n_feats = x ? (size_t) n_rows * n_embd : 0;
+        inp.n_codes = (size_t) n_rows;
+        if (temp >= 0.0f) {
+            inp.temp = temp;
+        }
+        if (flow_steps > 0) {
+            inp.n_steps = flow_steps;
+        }
+        if (cfg_scale > 0.0f) {
+            inp.cfg_scale = cfg_scale;
+        }
+        mtmd_gen_out tmp{};
+        if (mtmd_gen_audio_process(mctx, gen_state.get(), &inp, out ? out : &tmp) != 0) {
+            LOG_ERR("mtmd_helper_gen_audio: voxcpm2 gen_code failed\n");
+            return false;
+        }
+        return true;
+    }
+
+    // the AudioVAE decoder is causal and keeps its state, so the latents can be decoded in pieces
+    bool decode_wav() {
+        const size_t n_total = latents.size() / n_frame;
+        while (n_decoded < n_total) {
+            const size_t n = std::min(n_total - n_decoded, (size_t) 16);
+            mtmd_gen_inp inp = mtmd_gen_inp_default(mctx);
+            inp.type    = MTMD_GEN_PROCESS_TYPE_GEN_WAV;
+            inp.feats   = latents.data() + n_decoded * n_frame;
+            inp.n_feats = n * n_frame;
+            mtmd_gen_out out{};
+            if (mtmd_gen_audio_process(mctx, gen_state.get(), &inp, &out) != 0) {
+                LOG_ERR("mtmd_helper_gen_audio: AudioVAE decode failed\n");
+                return false;
+            }
+            audio_pcm.insert(audio_pcm.end(), out.audio, out.audio + out.n_samples);
+            n_decoded += n;
+        }
+        return true;
+    }
+
+    static constexpr int    min_len       = 2;      // min_len of the reference
+    static constexpr size_t n_frame       = 64 * 4; // latents of one frame, n_latent * n_patch
+    static constexpr size_t stream_frames = 2;      // frames per decoder call when streaming (0.32 s)
+    static constexpr int    n_ctx_res     = 8192;   // rows of the residual LM, see mtmd_graph_voxcpm2_gen::n_ctx_res
+
+    llama_token tok_audio_start = LLAMA_TOKEN_NULL;
+    llama_token tok_ref_start   = LLAMA_TOKEN_NULL;
+    llama_token tok_ref_end     = LLAMA_TOKEN_NULL;
+    std::unordered_map<llama_token, std::vector<llama_token>> split_cjk;
+    bool cache_ready = false;
+
+    float   temp       = -1.0f;
+    float   cfg_scale  = 0.0f;
+    int32_t flow_steps = 0;
+
+    llama_seq_id seq_id = 0;
+    llama_pos    pos    = 0;
+    std::vector<prompt_row> rows;
+    std::vector<float>      voice;      // backbone rows of the voice reference
+    int prompt_pos = 0;
+    int n_frames   = 0;
+    int max_len    = 0;
+
+    std::vector<float> h_state_buf; // hidden state of the last backbone row
+    std::vector<float> embd_in;     // input row of the last backbone row, empty for the text row
+    std::vector<float> latents;     // frame-major
+    size_t n_decoded = 0;           // frames already through the decoder
+
+    std::vector<float> audio_pcm;
+    mtmd_helper_gen_audio_outtype out_type = MTMD_HELPER_GEN_AUDIO_OUTTYPE_WAV;
+    std::vector<char> out_buf;
+    bool   stream          = false;
+    size_t pcm_sent        = 0; // samples already returned by get_output()
+    bool   wav_header_sent = false;
+};
+
 static std::unique_ptr<mtmd_gen_audio_pipeline> make_pipeline(llama_context * lctx, mtmd_context * mctx) {
     switch (mtmd_gen_audio_get_info(mctx).type) {
         case MTMD_GEN_AUDIO_TYPE_QWEN3TTS:
@@ -4940,6 +5367,8 @@ static std::unique_ptr<mtmd_gen_audio_pipeline> make_pipeline(llama_context * lc
             return std::unique_ptr<mtmd_gen_audio_pipeline>(new music3_gen_audio_pipeline(lctx, mctx));
         case MTMD_GEN_AUDIO_TYPE_FISH_S2:
             return std::unique_ptr<mtmd_gen_audio_pipeline>(new fish_s2_gen_audio_pipeline(lctx, mctx));
+        case MTMD_GEN_AUDIO_TYPE_VOXCPM2:
+            return std::unique_ptr<mtmd_gen_audio_pipeline>(new voxcpm2_gen_audio_pipeline(lctx, mctx));
         default:
             return nullptr;
     }
diff --git a/tools/mtmd/mtmd-impl.h b/tools/mtmd/mtmd-impl.h
index c28a13d00..582b11365 100644
--- a/tools/mtmd/mtmd-impl.h
+++ b/tools/mtmd/mtmd-impl.h
@@ -622,6 +622,8 @@ enum projector_type {
     PROJECTOR_TYPE_ZONOS3_GEN,
     PROJECTOR_TYPE_ZONOS3_SPKENC,
     PROJECTOR_TYPE_YUE2_GEN,
+    PROJECTOR_TYPE_VOXCPM2_GEN,
+    PROJECTOR_TYPE_VOXCPM2_SPKENC,
     PROJECTOR_TYPE_MINIMAX_MUSIC3_GEN,
     PROJECTOR_TYPE_FISH_S2_GEN,
     PROJECTOR_TYPE_FISH_S2_SPKENC,
@@ -720,6 +722,8 @@ static std::map<projector_type, std::string> PROJECTOR_TYPE_NAMES = {
     { PROJECTOR_TYPE_ZONOS3_GEN,        "zonos3_gen"},
     { PROJECTOR_TYPE_ZONOS3_SPKENC,     "zonos3_spkenc"},
     { PROJECTOR_TYPE_YUE2_GEN,          "yue2_gen"},
+    { PROJECTOR_TYPE_VOXCPM2_GEN,       "voxcpm2_gen"},
+    { PROJECTOR_TYPE_VOXCPM2_SPKENC,    "voxcpm2_spkenc"},
     { PROJECTOR_TYPE_MINIMAX_MUSIC3_GEN, "minimax_music3_gen"},
     { PROJECTOR_TYPE_FISH_S2_GEN,       "fish_s2_gen"},
     { PROJECTOR_TYPE_FISH_S2_SPKENC,    "fish_s2_spkenc"},
diff --git a/tools/mtmd/mtmd-model.cpp b/tools/mtmd/mtmd-model.cpp
index 35f9d1c2d..e26824d0f 100644
--- a/tools/mtmd/mtmd-model.cpp
+++ b/tools/mtmd/mtmd-model.cpp
@@ -1105,6 +1105,12 @@ struct mtmd_model_loader {
                     {
                         hparams.audio_sample_rate = 48000;
                     } break;
+                case PROJECTOR_TYPE_VOXCPM2_GEN:
+                    {
+                        hparams.audio_sample_rate = 48000;
+                        // the flow graph unrolls up to n_steps_max LocDiT passes
+                        model.max_nodes = 65536;
+                    } break;
                 case PROJECTOR_TYPE_MINIMAX_MUSIC3_GEN:
                     {
                         hparams.audio_sample_rate = 44100;
@@ -1145,6 +1151,14 @@ struct mtmd_model_loader {
                         hparams.flow_n_step       = 1;
                         hparams.gen_eos_threshold = -4.0f;
                     } break;
+                case PROJECTOR_TYPE_VOXCPM2_SPKENC:
+                    {
+                        // AudioVAE encoder, one backbone row per patch of latents (checked against the strides at load)
+                        hparams.audio_sample_rate = 16000;
+                        hparams.audio_patch_sizes = { 2, 5, 8, 8, mtmd_graph_voxcpm2_gen::n_patch };
+                        // whole patches, about 10 s like a typical reference clip
+                        hparams.warmup_audio_size = hparams.audio_samples_per_token() * 63;
+                    } break;
                 case PROJECTOR_TYPE_FISH_S2_SPKENC:
                     {
                         // Fish DAC encoder, codec.pth has no config
@@ -1411,7 +1425,8 @@ struct mtmd_model_loader {
                                        model.proj_type != PROJECTOR_TYPE_MUSE_SPARK_AUDIO &&
                                        model.proj_type != PROJECTOR_TYPE_VIBEVOICE_ASR &&
                                        model.proj_type != PROJECTOR_TYPE_POCKETTTS_SPKENC &&
-                                       model.proj_type != PROJECTOR_TYPE_FISH_S2_SPKENC;
+                                       model.proj_type != PROJECTOR_TYPE_FISH_S2_SPKENC &&
+                                       model.proj_type != PROJECTOR_TYPE_VOXCPM2_SPKENC;

                 // Validate audio hparams loaded from GGUF metadata
                 if (hparams.n_mel_bins <= 0 || (fft_based && hparams.n_mel_bins > 256)) {
@@ -1691,6 +1706,20 @@ struct mtmd_model_loader {
             }
         }

+        // DAC-style residual unit: Snake, dilated conv, Snake, conv k = 1 (voxcpm2 AudioVAE)
+        auto load_dac_res = [&](const std::string & q) {
+            mtmd_code2wav::dac_res ru;
+            ru.act1_alpha = get_tensor(q + ".snake1.alpha");
+            ru.act1_beta  = get_tensor(q + ".snake1.beta");
+            ru.conv1_w    = get_tensor(q + ".conv1.weight");
+            ru.conv1_b    = get_tensor(q + ".conv1.bias");
+            ru.act2_alpha = get_tensor(q + ".snake2.alpha");
+            ru.act2_beta  = get_tensor(q + ".snake2.beta");
+            ru.conv2_w    = get_tensor(q + ".conv2.weight");
+            ru.conv2_b    = get_tensor(q + ".conv2.bias");
+            return ru;
+        };
+
         // code2wav: RVQ codes -> raw PCM, drama3 has no quant/pre_conv/tfm projections
         auto load_code2wav = [&](bool with_proj) {
             auto & c2w = model.c2w;
@@ -3032,6 +3061,119 @@ struct mtmd_model_loader {
                         throw std::runtime_error(mtmd_format("%s: yue2: unexpected VAE decoder layout\n", __func__));
                     }
                 } break;
+            case PROJECTOR_TYPE_VOXCPM2_GEN:
+                {
+                    // generic a.gen.code.blk.* tensors are the LocDiT layers, LocEnc has the same hparams
+                    using g = mtmd_graph_voxcpm2_gen;
+                    auto & v = model.voxcpm2;
+                    v.rope_factors = get_tensor("a.gen.rope_factors");
+
+                    v.enc_layers.resize(hparams.n_layer);
+                    for (int il = 0; il < hparams.n_layer; il++) {
+                        auto & layer = v.enc_layers[il];
+                        const char * p = "a.gen.enc";
+                        layer.ln_1_w    = get_tensor(mtmd_format(TN_LN_1,          p, il, "weight"));
+                        layer.q_w       = get_tensor(mtmd_format(TN_ATTN_Q,        p, il, "weight"));
+                        layer.k_w       = get_tensor(mtmd_format(TN_ATTN_K,        p, il, "weight"));
+                        layer.v_w       = get_tensor(mtmd_format(TN_ATTN_V,        p, il, "weight"));
+                        layer.o_w       = get_tensor(mtmd_format(TN_ATTN_OUTPUT,   p, il, "weight"));
+                        layer.ln_2_w    = get_tensor(mtmd_format(TN_LN_2,          p, il, "weight"));
+                        layer.ff_gate_w = get_tensor(mtmd_format(TN_FFN_GATE,      p, il, "weight"));
+                        layer.ff_up_w   = get_tensor(mtmd_format(TN_FFN_UP,        p, il, "weight"));
+                        layer.ff_down_w = get_tensor(mtmd_format(TN_FFN_DOWN,      p, il, "weight"));
+                    }
+                    v.enc_special = get_tensor("a.gen.enc.special_token");
+                    v.enc_in_w    = get_tensor("a.gen.enc.in_proj.weight");
+                    v.enc_in_b    = get_tensor("a.gen.enc.in_proj.bias");
+                    v.enc_norm_w  = get_tensor("a.gen.enc.post_ln.weight");
+                    v.enc_to_lm_w = get_tensor("a.gen.enc_to_lm.weight");
+                    v.enc_to_lm_b = get_tensor("a.gen.enc_to_lm.bias");
+
+                    v.dit_in_w     = get_tensor("a.gen.dit.in_proj.weight");
+                    v.dit_in_b     = get_tensor("a.gen.dit.in_proj.bias");
+                    v.dit_cond_w   = get_tensor("a.gen.dit.cond_proj.weight");
+                    v.dit_cond_b   = get_tensor("a.gen.dit.cond_proj.bias");
+                    v.dit_out_w    = get_tensor("a.gen.dit.out_proj.weight");
+                    v.dit_out_b    = get_tensor("a.gen.dit.out_proj.bias");
+                    v.time0_w      = get_tensor("a.gen.dit.time_mlp.0.weight");
+                    v.time0_b      = get_tensor("a.gen.dit.time_mlp.0.bias");
+                    v.time1_w      = get_tensor("a.gen.dit.time_mlp.1.weight");
+                    v.time1_b      = get_tensor("a.gen.dit.time_mlp.1.bias");
+                    v.lm_to_dit_w  = get_tensor("a.gen.lm_to_dit.weight");
+                    v.lm_to_dit_b  = get_tensor("a.gen.lm_to_dit.bias");
+                    v.res_to_dit_w = get_tensor("a.gen.res_to_dit.weight");
+                    v.res_to_dit_b = get_tensor("a.gen.res_to_dit.bias");
+
+                    v.fsq_in_w    = get_tensor("a.gen.fsq.in.weight");
+                    v.fsq_in_b    = get_tensor("a.gen.fsq.in.bias");
+                    v.fsq_out_w   = get_tensor("a.gen.fsq.out.weight");
+                    v.fsq_out_b   = get_tensor("a.gen.fsq.out.bias");
+                    v.fusion_w    = get_tensor("a.gen.fusion.weight");
+                    v.fusion_b    = get_tensor("a.gen.fusion.bias");
+                    v.stop_proj_w = get_tensor("a.gen.stop_proj.weight");
+                    v.stop_proj_b = get_tensor("a.gen.stop_proj.bias");
+                    v.stop_head_w = get_tensor("a.gen.stop_head.weight");
+
+                    const std::string p = "a.gen.vae";
+                    v.dec_in_dw_w = get_tensor(p + ".conv_in_dw.weight");
+                    v.dec_in_dw_b = get_tensor(p + ".conv_in_dw.bias");
+                    v.dec_in_w    = get_tensor(p + ".conv_in.weight");
+                    v.dec_in_b    = get_tensor(p + ".conv_in.bias");
+                    int64_t ratio = 1;
+                    for (int b = 0; gguf_find_tensor(ctx_gguf.get(), mtmd_format("%s.blk.%d.up.weight", p.c_str(), b).c_str()) >= 0; b++) {
+                        const std::string q = p + ".blk." + std::to_string(b);
+                        mtmd_code2wav::dac_block blk;
+                        blk.snake_alpha = get_tensor(q + ".snake.alpha");
+                        blk.snake_beta  = get_tensor(q + ".snake.beta");
+                        blk.conv_w      = get_tensor(q + ".up.weight");
+                        blk.conv_b      = get_tensor(q + ".up.bias");
+                        for (int r = 0; r < 3; r++) {
+                            blk.res.push_back(load_dac_res(q + ".res." + std::to_string(r)));
+                        }
+                        ratio *= blk.conv_w->ne[0] / 2;
+                        v.dec_sr_scale.push_back(get_tensor(q + ".sr_scale"));
+                        v.dec_sr_bias.push_back(get_tensor(q + ".sr_bias"));
+                        v.dec.push_back(std::move(blk));
+                    }
+                    v.dec_post_alpha = get_tensor(p + ".post_snake.alpha");
+                    v.dec_post_beta  = get_tensor(p + ".post_snake.beta");
+                    v.dec_out_w      = get_tensor(p + ".conv_out.weight");
+                    v.dec_out_b      = get_tensor(p + ".conv_out.bias");
+
+                    // the graphs hard-code these shapes
+                    if (v.rope_factors->ne[0] != g::head_dim / 2 || v.dit_in_w->ne[0] != g::n_latent ||
+                            v.dec_in_dw_w->ne[2] != g::n_latent || ratio != g::dec_ratio || v.dec_out_w->ne[2] != 1 ||
+                            model.layers.empty() || model.layers[0].q_w->ne[1] != (int64_t) g::head_dim * hparams.n_head) {
+                        throw std::runtime_error(mtmd_format("%s: voxcpm2: unexpected tensor shapes\n", __func__));
+                    }
+                } break;
+            case PROJECTOR_TYPE_VOXCPM2_SPKENC:
+                {
+                    using g = mtmd_graph_voxcpm2_gen;
+                    auto & v = model.voxcpm2;
+                    const std::string p = "a.vae.enc";
+                    v.enc_conv_in_w = get_tensor(p + ".conv_in.weight");
+                    v.enc_conv_in_b = get_tensor(p + ".conv_in.bias");
+                    int64_t hop = 1;
+                    for (int b = 0; gguf_find_tensor(ctx_gguf.get(), mtmd_format("%s.blk.%d.down.weight", p.c_str(), b).c_str()) >= 0; b++) {
+                        const std::string q = p + ".blk." + std::to_string(b);
+                        mtmd_code2wav::dac_block blk;
+                        for (int r = 0; r < 3; r++) {
+                            blk.res.push_back(load_dac_res(q + ".res." + std::to_string(r)));
+                        }
+                        blk.snake_alpha = get_tensor(q + ".snake.alpha");
+                        blk.snake_beta  = get_tensor(q + ".snake.beta");
+                        blk.conv_w      = get_tensor(q + ".down.weight");
+                        blk.conv_b      = get_tensor(q + ".down.bias");
+                        hop *= blk.conv_w->ne[0] / 2;
+                        v.enc.push_back(std::move(blk));
+                    }
+                    v.enc_mu_w = get_tensor(p + ".fc_mu.weight");
+                    v.enc_mu_b = get_tensor(p + ".fc_mu.bias");
+                    if (hop * g::n_patch != hparams.audio_samples_per_token() || v.enc_mu_w->ne[2] != g::n_latent) {
+                        throw std::runtime_error(mtmd_format("%s: voxcpm2: unexpected AudioVAE encoder layout\n", __func__));
+                    }
+                } break;
             case PROJECTOR_TYPE_MINIMAX_MUSIC3_GEN:
                 {
                     // generic a.gen.code.* tensors (depth decoder layers, position_embd, post_ln) are loaded above
@@ -4855,11 +4997,13 @@ int mtmd_model::n_output_tokens(const mtmd_image_f32 * img) const {
         case PROJECTOR_TYPE_YUE2_GEN:
         case PROJECTOR_TYPE_MINIMAX_MUSIC3_GEN:
         case PROJECTOR_TYPE_FISH_S2_GEN:
+        case PROJECTOR_TYPE_VOXCPM2_GEN:
             {
                 // one hidden-state vector fed back to the talker per call
                 n_patches = 1;
             } break;
         case PROJECTOR_TYPE_FISH_S2_SPKENC:
+        case PROJECTOR_TYPE_VOXCPM2_SPKENC:
             {
                 // one backbone row per codec frame, the preprocessor pads to whole frames
                 n_patches = (int) (img->nx() / hparams.audio_samples_per_token());
@@ -5055,6 +5199,11 @@ int mtmd_model::n_mmproj_embd() const {
             return gen_code_out_embd_w->ne[0];
         case PROJECTOR_TYPE_YUE2_GEN:
             return yue2.inp_w->ne[1];
+        case PROJECTOR_TYPE_VOXCPM2_GEN:
+            return voxcpm2.enc_to_lm_w->ne[1];
+        case PROJECTOR_TYPE_VOXCPM2_SPKENC:
+            // the rows come from LocEnc of the gen model, see voxcpm2_ref_encode()
+            return hparams.projection_dim;
         case PROJECTOR_TYPE_MINIMAX_MUSIC3_GEN:
             return gen_code_out_embd_w->ne[0];
         case PROJECTOR_TYPE_POCKETTTS_SPKENC:
diff --git a/tools/mtmd/mtmd-model.h b/tools/mtmd/mtmd-model.h
index 2dcf19021..893578b03 100644
--- a/tools/mtmd/mtmd-model.h
+++ b/tools/mtmd/mtmd-model.h
@@ -217,6 +217,8 @@ struct mtmd_hparams {
     static constexpr int32_t qwen3tts_max_ref_seconds = 30;
     // fish_s2: the codec encoder runs at 44.1 kHz, memory grows with the clip
     static constexpr int32_t fish_s2_max_spk_seconds = 30;
+    // voxcpm2: the AudioVAE encoder runs at 16 kHz, memory grows with the clip
+    static constexpr int32_t voxcpm2_max_spk_seconds = 30;
     int32_t seanet_n_stage    = 0;
     std::vector<int32_t> seanet_ratios; // encoder order (reversed compared to the config)
     int32_t mimi_downsample   = 0;      // encoder frame rate / model frame rate
@@ -1067,6 +1069,68 @@ struct mtmd_music3 {
     std::vector<mtmd_layer> dit;
 };

+// voxcpm2: LocEnc, LocDiT projections, FSQ, residual LM fusion, stop head and the causal AudioVAE
+// the LocDiT layers are model.layers, the residual LM layers are loaded by libllama, see mtmd_voxcpm2_res_load()
+struct mtmd_voxcpm2 {
+    ggml_tensor * rope_factors = nullptr; // LongRoPE factors of LocEnc and LocDiT
+
+    // LocEnc: [special token, patch latents] -> output of the special token -> backbone input row
+    std::vector<mtmd_layer> enc_layers;
+    ggml_tensor * enc_special = nullptr;
+    ggml_tensor * enc_in_w    = nullptr;
+    ggml_tensor * enc_in_b    = nullptr;
+    ggml_tensor * enc_norm_w  = nullptr;
+    ggml_tensor * enc_to_lm_w = nullptr;
+    ggml_tensor * enc_to_lm_b = nullptr;
+
+    // LocDiT: [lm, res, time, cond patch, noisy patch] -> velocity of the noisy patch
+    ggml_tensor * dit_in_w     = nullptr;
+    ggml_tensor * dit_in_b     = nullptr;
+    ggml_tensor * dit_cond_w   = nullptr;
+    ggml_tensor * dit_cond_b   = nullptr;
+    ggml_tensor * dit_out_w    = nullptr;
+    ggml_tensor * dit_out_b    = nullptr;
+    ggml_tensor * time0_w      = nullptr; // time_mlp, the constant dt term is folded into time1_b
+    ggml_tensor * time0_b      = nullptr;
+    ggml_tensor * time1_w      = nullptr;
+    ggml_tensor * time1_b      = nullptr;
+    ggml_tensor * lm_to_dit_w  = nullptr;
+    ggml_tensor * lm_to_dit_b  = nullptr;
+    ggml_tensor * res_to_dit_w = nullptr;
+    ggml_tensor * res_to_dit_b = nullptr;
+
+    // backbone hidden state: FSQ (audio rows), residual LM input, stop head
+    ggml_tensor * fsq_in_w    = nullptr;
+    ggml_tensor * fsq_in_b    = nullptr;
+    ggml_tensor * fsq_out_w   = nullptr;
+    ggml_tensor * fsq_out_b   = nullptr;
+    ggml_tensor * fusion_w    = nullptr;
+    ggml_tensor * fusion_b    = nullptr;
+    ggml_tensor * stop_proj_w = nullptr;
+    ggml_tensor * stop_proj_b = nullptr;
+    ggml_tensor * stop_head_w = nullptr;
+
+    // AudioVAE decoder (gen): depthwise conv, conv k = 1, upsample blocks with a sample-rate scale and bias, Snake, conv
+    ggml_tensor * dec_in_dw_w = nullptr;
+    ggml_tensor * dec_in_dw_b = nullptr;
+    ggml_tensor * dec_in_w    = nullptr;
+    ggml_tensor * dec_in_b    = nullptr;
+    std::vector<ggml_tensor *> dec_sr_scale;
+    std::vector<ggml_tensor *> dec_sr_bias;
+    std::vector<mtmd_code2wav::dac_block> dec;
+    ggml_tensor * dec_post_alpha = nullptr;
+    ggml_tensor * dec_post_beta  = nullptr;
+    ggml_tensor * dec_out_w      = nullptr;
+    ggml_tensor * dec_out_b      = nullptr;
+
+    // AudioVAE encoder (spkenc): conv, downsample blocks (residual units, Snake, strided conv), fc_mu
+    ggml_tensor * enc_conv_in_w = nullptr;
+    ggml_tensor * enc_conv_in_b = nullptr;
+    std::vector<mtmd_code2wav::dac_block> enc;
+    ggml_tensor * enc_mu_w = nullptr;
+    ggml_tensor * enc_mu_b = nullptr;
+};
+
 struct mtmd_vocos {
     ggml_tensor * input_w        = nullptr;
     ggml_tensor * input_b        = nullptr;
@@ -1362,6 +1426,7 @@ struct mtmd_model {
     std::vector<mtmd_drama3_layer> drama3_layers;
     mtmd_zonos3_wav zonos3;
     mtmd_yue2 yue2;
+    mtmd_voxcpm2 voxcpm2;
     mtmd_music3 music3;
     mtmd_snake_vae snake_vae; // yue2, minimax_music3
     mtmd_tiny_voca tiny_voca;
diff --git a/tools/mtmd/mtmd-subcontext.cpp b/tools/mtmd/mtmd-subcontext.cpp
index 4f3a24037..f754a913b 100644
--- a/tools/mtmd/mtmd-subcontext.cpp
+++ b/tools/mtmd/mtmd-subcontext.cpp
@@ -394,6 +394,7 @@ static std::vector<c2w_state_slot> list_gen_state_slots(const mtmd_hparams & hpa
         case PROJECTOR_TYPE_POCKETTTS_GEN: return list_pockettts_state_slots(hparams, model);
         case PROJECTOR_TYPE_LFM3A_GEN:     return list_lfm3a_gen_state_slots(hparams, model);
         case PROJECTOR_TYPE_ZONOS3_GEN:    return list_zonos3_gen_state_slots(hparams, model);
+        case PROJECTOR_TYPE_VOXCPM2_GEN:   return list_voxcpm2_state_slots(hparams, model);
         default:                           return {};
     }
 }
@@ -1600,6 +1601,44 @@ bool mtmd_subcontext::encode(mtmd_subctx_encode_params * params) {
             {
                 set_input_f32("inp_feats", *params->feats);
             } break;
+        case PROJECTOR_TYPE_VOXCPM2_GEN:
+            {
+                auto set_positions = [&](const char * name) {
+                    std::vector<int32_t> pos(ggml_nelements(get_inp_tensor(name)));
+                    for (size_t i = 0; i < pos.size(); i++) {
+                        pos[i] = (int32_t) i;
+                    }
+                    set_input_i32(name, pos);
+                };
+                if (params->gen_process == MTMD_SUBCTX_GEN_PROCESS_GEN_CODE) {
+                    // feats: the patch of the previous frame
+                    set_input_f32("inp_cond", *params->feats);
+                    // flow matching starts from gaussian noise, std = temp
+                    ggml_tensor * t = get_inp_tensor("inp_noise");
+                    std::vector<float> noise(ggml_nelements(t), 0.0f);
+                    if (params->temp > 0.0f) {
+                        std::normal_distribution<float> dist(0.0f, params->temp);
+                        for (auto & v : noise) {
+                            v = dist(params->gen_state->rng);
+                        }
+                    }
+                    set_input_f32("inp_noise", noise);
+                    const int n_steps = (int) (ggml_nelements(get_inp_tensor("inp_temb")) / hparams.n_embd);
+                    set_input_f32("inp_temb", mtmd_graph_voxcpm2_gen::time_embd(n_steps, hparams.n_embd));
+                    set_positions("inp_pos_dit");
+                    set_positions("inp_pos_enc");
+                } else if (params->gen_process == MTMD_SUBCTX_GEN_PROCESS_RES_INP) {
+                    if (params->feats) {
+                        set_input_f32("inp_feats", *params->feats);
+                    }
+                } else if (params->gen_process == MTMD_SUBCTX_GEN_PROCESS_LATENT_IN) {
+                    set_input_f32("inp_feats", *params->feats);
+                    set_positions("inp_pos_enc");
+                } else if (params->gen_process == MTMD_SUBCTX_GEN_PROCESS_GEN_WAV) {
+                    set_input_f32("inp_feats", *params->feats);
+                    set_gen_state_in();
+                }
+            } break;
         case PROJECTOR_TYPE_KOKORO:
             {
                 const auto * io = params->kokoro;
@@ -1830,6 +1869,7 @@ bool mtmd_subcontext::encode(mtmd_subctx_encode_params * params) {
         case PROJECTOR_TYPE_QWEN3TTS_SPKENC:
         case PROJECTOR_TYPE_ZONOS3_SPKENC:
         case PROJECTOR_TYPE_FISH_S2_SPKENC:
+        case PROJECTOR_TYPE_VOXCPM2_SPKENC:
             {
                 // do nothing
             } break;
diff --git a/tools/mtmd/mtmd-subcontext.h b/tools/mtmd/mtmd-subcontext.h
index 0dfceddd8..853268bc8 100644
--- a/tools/mtmd/mtmd-subcontext.h
+++ b/tools/mtmd/mtmd-subcontext.h
@@ -71,6 +71,7 @@ enum mtmd_subctx_gen_process {
     MTMD_SUBCTX_GEN_PROCESS_TOKENIZE,   // raw PCM audio to semantic speech tokens
     MTMD_SUBCTX_GEN_PROCESS_LATENT_IN,  // internal: flow latent rows to backbone embd rows (tiny-voca: token states to durations)
     MTMD_SUBCTX_GEN_PROCESS_LATENT_OUT, // internal: backbone hidden rows to flow velocity rows (tiny-voca: to source-filter params)
+    MTMD_SUBCTX_GEN_PROCESS_RES_INP,    // internal: backbone row to residual LM input row (voxcpm2)
     MTMD_SUBCTX_GEN_PROCESS_FLOW_COND,  // internal: frame rows to flow condition rows
     MTMD_SUBCTX_GEN_PROCESS_FLOW_STEP,  // internal: one velocity pass of the flow transformer
 };
@@ -147,6 +148,7 @@ struct mtmd_subctx_encode_params {
     float   temp = 0.0f;                      // sampling temperature, noise scale for flow-matching decoders
     float   cfg_scale = 1.0f;                 // classifier-free guidance, 1.0 = off
     float   flow_t = 0.0f;                    // FLOW_STEP: flow-matching time, 0 = noise
+    int     n_steps   = 0;                    // steps of the flow-matching ODE solver (voxcpm2)
     bool * out_is_eos = nullptr;

     // TOKENIZE: speech embedding rows of the produced codes
diff --git a/tools/mtmd/mtmd.cpp b/tools/mtmd/mtmd.cpp
index a4fee15f4..1101d267a 100644
--- a/tools/mtmd/mtmd.cpp
+++ b/tools/mtmd/mtmd.cpp
@@ -644,12 +644,13 @@ static void mtmd_owned_llama_set_tensor_data(ggml_tensor * t, void * user_data)
             return name.size() >= n && name.compare(name.size() - n, n, suffix) == 0;
         };
         const bool optional = ends_with("scale") || ends_with(".bias") || name == "dense_2.weight" ||
+                              name == "rope_freqs.weight" || // granite, voxcpm2 residual LM
                               name == "token_embd.weight" || name == "output.weight"; // inputs are embedding rows, outputs are hidden states
         if (!optional || t->type != GGML_TYPE_F32) {
             throw std::runtime_error(mtmd_format("tensor %s is missing from the mmproj", t->name));
         }
         float * w = (float *) buf.data();
-        if (name.size() >= 5 && name.compare(name.size() - 5, 5, "scale") == 0) {
+        if (ends_with("scale") || name == "rope_freqs.weight") {
             std::fill(w, w + ggml_nelements(t), 1.0f);
         } else if (name == "dense_2.weight") {
             GGML_ASSERT(t->ne[0] == t->ne[1]);
@@ -758,6 +759,62 @@ static llama_model * mtmd_tiny_voca_stack_load(const char * fname, ggml_backend_
     return model;
 }

+// voxcpm2: the residual LM is a granite text model with RoPE off, its sizes come from the tensor shapes
+static llama_model * mtmd_voxcpm2_res_load(const char * fname, ggml_backend_dev_t device, const mtmd_hparams & hparams) {
+    using g = mtmd_graph_voxcpm2_gen;
+    ggml_context * meta = nullptr;
+    gguf_init_params gparams = { /*no_alloc =*/ true, /*ctx =*/ &meta };
+    gguf_context_ptr gsrc(gguf_init_from_file(fname, gparams));
+    ggml_context_ptr meta_ptr(meta);
+    if (!gsrc) {
+        throw std::runtime_error(mtmd_format("failed to read %s", fname));
+    }
+    auto shape = [&](const char * name) {
+        const ggml_tensor * t = ggml_get_tensor(meta, name);
+        if (!t) {
+            throw std::runtime_error(mtmd_format("voxcpm2: missing tensor %s", name));
+        }
+        return t;
+    };
+    uint32_t n_layer = 0;
+    while (ggml_get_tensor(meta, mtmd_format("a.gen.res.blk.%u.attn_norm.weight", n_layer).c_str())) {
+        n_layer++;
+    }
+    const ggml_tensor * q    = shape("a.gen.res.blk.0.attn_q.weight");
+    const ggml_tensor * k    = shape("a.gen.res.blk.0.attn_k.weight");
+    const ggml_tensor * gate = shape("a.gen.res.blk.0.ffn_gate.weight");
+    if (q->ne[1] % g::head_dim != 0 || k->ne[1] % g::head_dim != 0) {
+        throw std::runtime_error(mtmd_format("voxcpm2: the residual LM heads are not of size %d", g::head_dim));
+    }
+
+    const llm_arch arch = LLM_ARCH_GRANITE;
+    gguf_context_ptr gdst(gguf_init_empty());
+    gguf_context * dst = gdst.get();
+    auto key = [&](llm_kv kv) { return llama_arch_kv_name(arch, kv); };
+    gguf_set_val_str (dst, key(LLM_KV_GENERAL_ARCHITECTURE).c_str(), llama_arch_name(arch));
+    gguf_set_val_str (dst, key(LLM_KV_TOKENIZER_MODEL).c_str(),       "none");
+    gguf_set_val_u32 (dst, key(LLM_KV_VOCAB_SIZE).c_str(),                    1);
+    gguf_set_val_u32 (dst, key(LLM_KV_CONTEXT_LENGTH).c_str(),                g::n_ctx_res);
+    gguf_set_val_u32 (dst, key(LLM_KV_EMBEDDING_LENGTH).c_str(),              (uint32_t) q->ne[0]);
+    gguf_set_val_u32 (dst, key(LLM_KV_FEED_FORWARD_LENGTH).c_str(),           (uint32_t) gate->ne[1]);
+    gguf_set_val_u32 (dst, key(LLM_KV_BLOCK_COUNT).c_str(),                   n_layer);
+    gguf_set_val_u32 (dst, key(LLM_KV_ATTENTION_HEAD_COUNT).c_str(),          (uint32_t) (q->ne[1] / g::head_dim));
+    gguf_set_val_u32 (dst, key(LLM_KV_ATTENTION_HEAD_COUNT_KV).c_str(),       (uint32_t) (k->ne[1] / g::head_dim));
+    gguf_set_val_u32 (dst, key(LLM_KV_ATTENTION_KEY_LENGTH).c_str(),          g::head_dim); // kv_channels, may differ from n_embd / n_head
+    gguf_set_val_u32 (dst, key(LLM_KV_ATTENTION_VALUE_LENGTH).c_str(),        g::head_dim);
+    gguf_set_val_f32 (dst, key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS).c_str(),   hparams.eps);
+    gguf_set_val_f32 (dst, key(LLM_KV_LOGIT_SCALE).c_str(),                   1.0f);
+    gguf_set_val_bool(dst, key(LLM_KV_ROPE_SCALING_FINETUNED).c_str(),        false);
+
+    size_t n_tensors = 0;
+    llama_model * model = mtmd_owned_llama_load(fname, device, dst, "a.gen.res.", n_tensors);
+    if (n_tensors != (size_t) n_layer * 9 + 1) {
+        llama_model_free(model);
+        throw std::runtime_error("voxcpm2: unexpected residual LM tensors in the mmproj");
+    }
+    return model;
+}
+
 struct mtmd_context {
     // must outlive the subcontexts below
     mtmd_models models;
@@ -777,6 +834,10 @@ struct mtmd_context {
     llama_model_ptr       yue2_nar;
     llama_flash_attn_type yue2_flash_attn = LLAMA_FLASH_ATTN_TYPE_AUTO;

+    // voxcpm2: the residual LM, each gen state has a llama_context of it, see mtmd_gen_code_voxcpm2()
+    llama_model_ptr       voxcpm2_res;
+    llama_flash_attn_type voxcpm2_flash_attn = LLAMA_FLASH_ATTN_TYPE_AUTO;
+
     // tiny-voca: the frame stacks, run on llama_contexts of their own, see mtmd_gen_wav_tiny_voca()
     llama_model_ptr       tiny_voca_prosody;
     llama_model_ptr       tiny_voca_acoustic;
@@ -919,6 +980,10 @@ struct mtmd_context {
             yue2_nar.reset(mtmd_yue2_nar_load(mmproj_fname, device, models.gen_a->hparams));
             yue2_flash_attn = ctx_params.flash_attn_type;
         }
+        if (models.gen_a && models.gen_a->proj_type == PROJECTOR_TYPE_VOXCPM2_GEN && !no_alloc) {
+            voxcpm2_res.reset(mtmd_voxcpm2_res_load(mmproj_fname, device, models.gen_a->hparams));
+            voxcpm2_flash_attn = ctx_params.flash_attn_type;
+        }
         if (models.gen_a && models.gen_a->proj_type == PROJECTOR_TYPE_TINY_VOCA && !no_alloc) {
             using g = mtmd_graph_tiny_voca;
             const auto & hp = models.gen_a->hparams;
@@ -1481,7 +1546,11 @@ struct mtmd_context {
                 } break;
             case PROJECTOR_TYPE_FISH_S2_SPKENC:
                 {
-                    audio_preproc = std::make_unique<mtmd_audio_preprocessor_fish_s2>(models.a.get());
+                    audio_preproc = std::make_unique<mtmd_audio_preprocessor_spk_ref>(models.a.get(), mtmd_hparams::fish_s2_max_spk_seconds);
+                } break;
+            case PROJECTOR_TYPE_VOXCPM2_SPKENC:
+                {
+                    audio_preproc = std::make_unique<mtmd_audio_preprocessor_spk_ref>(models.a.get(), mtmd_hparams::voxcpm2_max_spk_seconds);
                 } break;
             case PROJECTOR_TYPE_CHATTERBOX_SPKENC:
                 {
@@ -2545,6 +2614,7 @@ static int32_t mtmd_encode_impl(mtmd_context * ctx, const mtmd_image_tokens * im
 }

 static int32_t cbx_ref_encode(mtmd_context * ctx, const mtmd_audio_tokens * audio_tokens, std::vector<float> & out_embd);
+static int32_t voxcpm2_ref_encode(mtmd_context * ctx, const mtmd_audio_tokens * audio_tokens, std::vector<float> & out_embd);

 static bool mtmd_audio_batch_encode(mtmd_context * ctx, const mtmd_image_f32_batch & batch_f32, mtmd_audio_preproc_out * source, uint32_t idx, std::vector<float> & out_embd) {
     mtmd_image_f32_batch batch = batch_f32.clone();
@@ -2658,6 +2728,9 @@ static int32_t mtmd_encode_chunk_impl(mtmd_context * ctx, const mtmd_input_chunk
         if (ctx->ctx_a->model.proj_type == PROJECTOR_TYPE_CHATTERBOX_SPKENC) {
             return cbx_ref_encode(ctx, chunk->tokens_audio.get(), out_embd);
         }
+        if (ctx->ctx_a->model.proj_type == PROJECTOR_TYPE_VOXCPM2_SPKENC) {
+            return voxcpm2_ref_encode(ctx, chunk->tokens_audio.get(), out_embd);
+        }
         if (ctx->ctx_a->model.proj_type == PROJECTOR_TYPE_QWEN3TTS_SPKENC && chunk->tokens_audio->batch_f32.entries.size() == 2) {
             return mtmd_qwen3tts_ref_encode(ctx, chunk->tokens_audio.get(), out_embd);
         }
@@ -2754,6 +2827,10 @@ mtmd_gen_audio_info mtmd_gen_audio_get_info(const mtmd_context * ctx) {
             info.type = MTMD_GEN_AUDIO_TYPE_FISH_S2;
             info.sample_rate = 44100;
             break;
+        case PROJECTOR_TYPE_VOXCPM2_GEN:
+            info.type = MTMD_GEN_AUDIO_TYPE_VOXCPM2;
+            info.sample_rate = 48000;
+            break;
         case PROJECTOR_TYPE_KOKORO:
             info.type = MTMD_GEN_AUDIO_TYPE_KOKORO;
             info.sample_rate = 24000;
@@ -3118,6 +3195,39 @@ static int32_t cbx_ref_encode(mtmd_context * ctx, const mtmd_audio_tokens * audi
     return 0;
 }

+// the AudioVAE encoder of the audio model gives the latent patches, LocEnc of the gen model turns them into backbone rows
+static int32_t voxcpm2_ref_encode(mtmd_context * ctx, const mtmd_audio_tokens * audio_tokens, std::vector<float> & out_embd) {
+    using g = mtmd_graph_voxcpm2_gen;
+    if (!ctx->ctx_gen_a) {
+        LOG_ERR("%s: reference encoding needs the audio generation context\n", __func__);
+        return 1;
+    }
+    std::vector<float> latents((size_t) audio_tokens->n_tokens * g::n_latent * g::n_patch);
+    if (!mtmd_audio_batch_encode(ctx, audio_tokens->batch_f32, audio_tokens->source.get(), audio_tokens->source_idx, latents)) {
+        return 1;
+    }
+
+    // the batch entry is unused, present to satisfy the encode interface
+    mtmd_image_f32 dummy;
+    dummy.set_size({1, 1}, false, true);
+    dummy.cpy_buf(std::vector<float>(1, 0.0f));
+    mtmd_image_f32_batch batch;
+    batch.is_audio = true;
+    batch.entries.push_back(std::move(dummy));
+
+    mtmd_subctx_encode_params params;
+    params.imgs        = &batch;
+    params.n_threads   = ctx->n_threads;
+    params.gen_process = MTMD_SUBCTX_GEN_PROCESS_LATENT_IN;
+    params.feats       = &latents;
+    params.out_feats   = &out_embd;
+    if (!ctx->ctx_gen_a->encode(&params)) {
+        LOG_ERR("%s: encode failed (LocEnc)\n", __func__);
+        return 1;
+    }
+    return 0;
+}
+
 int32_t mtmd_gen_audio_g2p(const mtmd_context * ctx, const char * word, char * buf, size_t buf_size) {
     if (!ctx || !ctx->ctx_gen_a || !word || !buf || buf_size == 0) {
         return 0;
@@ -3198,6 +3308,12 @@ mtmd_gen_inp mtmd_gen_inp_default(const mtmd_context * ctx) {
         case PROJECTOR_TYPE_TINY_VOCA:
             inp.type = MTMD_GEN_PROCESS_TYPE_GEN_WAV;
             break;
+        case PROJECTOR_TYPE_VOXCPM2_GEN:
+            // UnifiedCFM.forward(): unit gaussian noise, see VoxCPM2Model._inference() for the steps and the guidance
+            inp.temp      = 1.0f;
+            inp.n_steps   = mtmd_graph_voxcpm2_gen::n_steps_default;
+            inp.cfg_scale = mtmd_graph_voxcpm2_gen::cfg_default;
+            break;
         case PROJECTOR_TYPE_MINIMAX_MUSIC3_GEN:
             // top-k 50 is fixed in the graph, like the reference
             inp.top_k = 50;
@@ -3254,6 +3370,10 @@ struct mtmd_gen_state {
     // zonos3: the iSTFT head runs on the host
     std::unique_ptr<mtmd_audio_zonos3_synth> zonos3_synth;

+    // voxcpm2: residual LM context of the sequence (its KV keeps the previous rows), and the patch of the last frame
+    llama_context_ptr  voxcpm2_res;
+    llama_pos          voxcpm2_n_past = 0;
+    std::vector<float> voxcpm2_cond;
     // minimax_music3: tail of the previous window, the next one starts from it, see mtmd_gen_wav_music3()
     std::vector<float> flow_prev_latent;
     std::vector<float> flow_prev_cond;
@@ -3284,6 +3404,26 @@ mtmd_gen_state * mtmd_gen_state_init(mtmd_context * ctx, mtmd_gen_state_params p
         const auto * hparams = &ctx->ctx_gen_a->model.hparams;
         st->istft = std::make_unique<mtmd_audio_streaming_istft>(hparams->audio_n_fft, hparams->audio_hop_len);
     }
+    if (ctx->ctx_gen_a->model.proj_type == PROJECTOR_TYPE_VOXCPM2_GEN) {
+        using g = mtmd_graph_voxcpm2_gen;
+        llama_context_params cparams = llama_context_default_params();
+        cparams.n_ctx           = g::n_ctx_res;
+        cparams.n_seq_max       = 1;
+        cparams.n_threads       = ctx->n_threads;
+        cparams.n_threads_batch = ctx->n_threads;
+        cparams.embeddings      = true;
+        cparams.pooling_type    = LLAMA_POOLING_TYPE_NONE;
+        cparams.flash_attn_type = ctx->voxcpm2_flash_attn;
+        cparams.no_perf         = true;
+        if (ctx->voxcpm2_res) {
+            st->voxcpm2_res.reset(llama_init_from_model(ctx->voxcpm2_res.get(), cparams));
+        }
+        if (!st->voxcpm2_res) {
+            LOG_ERR("%s: cannot create the residual LM context\n", __func__);
+            return nullptr;
+        }
+        st->voxcpm2_cond.assign(g::n_latent * g::n_patch, 0.0f);
+    }
     return st.release();
 }

@@ -3299,6 +3439,11 @@ void mtmd_gen_state_reset(mtmd_gen_state * state) {
         state->istft = std::make_unique<mtmd_audio_streaming_istft>(hparams->audio_n_fft, hparams->audio_hop_len);
     }
     state->zonos3_synth.reset();
+    if (state->voxcpm2_res) {
+        llama_memory_clear(llama_get_memory(state->voxcpm2_res.get()), true);
+        state->voxcpm2_n_past = 0;
+        std::fill(state->voxcpm2_cond.begin(), state->voxcpm2_cond.end(), 0.0f);
+    }
     state->flow_prev_latent.clear();
     state->flow_prev_cond.clear();
 }
@@ -3785,6 +3930,126 @@ static int32_t mtmd_gen_wav_yue2(mtmd_context * ctx, mtmd_gen_state * state, con
     return 0;
 }

+// voxcpm2 GEN_CODE: the backbone rows go through the residual LM, then code0 >= 0 makes the next frame
+// see VoxCPM2Model._inference(): the prompt rows are read in batches, then each frame is one call
+static int32_t mtmd_gen_code_voxcpm2(mtmd_context * ctx, mtmd_gen_state * state, const mtmd_gen_inp * inp, mtmd_gen_out * out) {
+    using g = mtmd_graph_voxcpm2_gen;
+    mtmd_subcontext * sctx = ctx->ctx_gen_a.get();
+    const size_t n_text = (size_t) sctx->model.n_mmproj_embd();
+    const bool   prompt = inp->code0 < 0;
+    const int    n_rows = prompt && inp->n_codes > 1 ? (int) inp->n_codes : 1;
+
+    if (!inp->embd || (inp->feats && inp->n_feats != n_text * n_rows)) {
+        LOG_ERR("%s: voxcpm2 needs the hidden states, and the input rows for audio rows\n", __func__);
+        return 1;
+    }
+    const int n_steps = inp->n_steps > 0 ? inp->n_steps : g::n_steps_default;
+    if (!prompt && (n_steps < 2 || n_steps > g::n_steps_max)) {
+        LOG_ERR("%s: flow steps must be in [2, %d]\n", __func__, g::n_steps_max);
+        return 1;
+    }
+    if (state->voxcpm2_n_past + n_rows > g::n_ctx_res) {
+        LOG_ERR("%s: the residual LM holds at most %d rows\n", __func__, g::n_ctx_res);
+        return 1;
+    }
+
+    auto make_batch = [&](const float * rows, int n) {
+        mtmd_image_f32 img;
+        img.set_size({(int) n_text, n}, false, true);
+        img.cpy_buf(std::vector<float>(rows, rows + n_text * n));
+        mtmd_image_f32_batch batch;
+        batch.is_audio = true;
+        batch.entries.push_back(std::move(img));
+        return batch;
+    };
+
+    // FSQ (audio rows), residual LM inputs, and the stop score of a single row
+    mtmd_image_f32_batch batch = make_batch(inp->embd, n_rows);
+    std::vector<float> x_rows;
+    if (inp->feats) {
+        x_rows.assign(inp->feats, inp->feats + n_text * n_rows);
+    }
+    std::vector<float> res_out; // residual LM input rows, then the (FSQ'd) hidden states
+    bool is_eos = false;
+    {
+        mtmd_subctx_encode_params params;
+        params.imgs        = &batch;
+        params.n_threads   = ctx->n_threads;
+        params.gen_process = MTMD_SUBCTX_GEN_PROCESS_RES_INP;
+        params.feats       = inp->feats ? &x_rows : nullptr;
+        params.out_feats   = &res_out;
+        params.out_is_eos  = &is_eos;
+        if (!sctx->encode(&params)) {
+            LOG_ERR("%s: encode failed (res_inp)\n", __func__);
+            return 1;
+        }
+    }
+
+    // residual LM, causal over all the rows of the sequence
+    llama_context * lctx = state->voxcpm2_res.get();
+    {
+        llama_batch_ext_ptr lbatch(llama_batch_ext_init(lctx));
+        for (int i = 0; i < n_rows; i++) {
+            const llama_embd row = { res_out.data() + (size_t) i * n_text, 1, n_text };
+            const int32_t    idx = llama_batch_ext_add_embd(lbatch.get(), 0, row);
+            if (idx < 0) {
+                LOG_ERR("%s: failed to add a row to the batch (error %d)\n", __func__, idx);
+                return 1;
+            }
+            const llama_pos p = state->voxcpm2_n_past + i;
+            llama_batch_ext_set_pos(lbatch.get(), idx, &p);
+            llama_batch_ext_set_output_embd(lbatch.get(), idx, true); // required with embeddings on, only the last is read
+        }
+        if (llama_process(lctx, LLAMA_PROCESS_TYPE_DECODE, lbatch.get()) != 0) {
+            LOG_ERR("%s: residual LM decode failed\n", __func__);
+            return 1;
+        }
+        state->voxcpm2_n_past += n_rows;
+    }
+
+    state->out_embd.clear();
+    state->out_feats.clear();
+    if (prompt) {
+        return 0;
+    }
+
+    // flow matching from [FSQ'd hidden state, residual LM output], then LocEnc of the new patch
+    const float * r = llama_get_embeddings_ith(lctx, -1);
+    std::vector<float> rows(res_out.begin() + n_text, res_out.end());
+    rows.insert(rows.end(), r, r + n_text);
+    batch = make_batch(rows.data(), 2);
+    std::vector<float> next_embd(n_text);
+    std::vector<float> patch;
+    {
+        mtmd_subctx_encode_params params;
+        params.imgs        = &batch;
+        params.n_threads   = ctx->n_threads;
+        params.gen_process = MTMD_SUBCTX_GEN_PROCESS_GEN_CODE;
+        params.feats       = &state->voxcpm2_cond;
+        params.temp        = inp->temp;
+        params.n_steps     = n_steps;
+        params.cfg_scale   = inp->cfg_scale > 0.0f ? inp->cfg_scale : g::cfg_default;
+        params.out_embd    = &next_embd;
+        params.out_feats   = &patch;
+        params.gen_state   = state->gen_state;
+        if (!sctx->encode(&params)) {
+            LOG_ERR("%s: encode failed (flow)\n", __func__);
+            // drop the row of this frame, so the state stays as before the call
+            state->voxcpm2_n_past -= n_rows;
+            llama_memory_seq_rm(llama_get_memory(lctx), 0, (llama_pos) state->voxcpm2_n_past, -1);
+            return 1;
+        }
+    }
+    state->voxcpm2_cond = patch;
+    state->out_embd     = std::move(next_embd);
+    state->out_feats    = std::move(patch);
+
+    out->embd    = state->out_embd.data();
+    out->feats   = state->out_feats.data();
+    out->n_feats = state->out_feats.size();
+    out->is_eos  = is_eos;
+    return 0;
+}

 // minimax_music3: one pass of an mtmd graph on rows of floats
 static bool music3_encode(mtmd_context * ctx, mtmd_gen_state * state, mtmd_subctx_encode_params & params, const std::vector<float> & in) {
@@ -3970,6 +4235,17 @@ static int32_t mtmd_gen_audio_process_impl(mtmd_context * ctx, mtmd_gen_state *
                                                            : mtmd_gen_wav_music3(ctx, state, inp, out);
     }

+    if (sctx->model.proj_type == PROJECTOR_TYPE_VOXCPM2_GEN) {
+        if (inp->type == MTMD_GEN_PROCESS_TYPE_GEN_CODE) {
+            return mtmd_gen_code_voxcpm2(ctx, state, inp, out);
+        }
+        const size_t n_frame = mtmd_graph_voxcpm2_gen::n_latent * mtmd_graph_voxcpm2_gen::n_patch;
+        if (!inp->feats || inp->n_feats == 0 || inp->n_feats % n_frame != 0 || inp->codes) {
+            LOG_ERR("%s: voxcpm2 gen_wav needs whole patches of latents\n", __func__);
+            return 1;
+        }
+    }
+
     if (sctx->model.proj_type == PROJECTOR_TYPE_TINY_VOCA) {
         return mtmd_gen_wav_tiny_voca(ctx, state, inp, out);
     }
diff --git a/tools/mtmd/mtmd.h b/tools/mtmd/mtmd.h
index 7a15f470c..49fc8136e 100644
--- a/tools/mtmd/mtmd.h
+++ b/tools/mtmd/mtmd.h
@@ -480,6 +480,7 @@ enum mtmd_gen_audio_type {
     MTMD_GEN_AUDIO_TYPE_FISH_S2,
     MTMD_GEN_AUDIO_TYPE_KOKORO,
     MTMD_GEN_AUDIO_TYPE_TINY_VOCA,
+    MTMD_GEN_AUDIO_TYPE_VOXCPM2,
 };

 struct mtmd_gen_audio_info {
@@ -541,17 +542,20 @@ struct mtmd_gen_inp {
     size_t        n_feats;
     float speed; // kokoro: speaking speed, > 0 (default: 1.0)

+    // voxcpm2 GEN_CODE: embd is the hidden state of a backbone row and feats its input row (n_text_embd elements, null for a text row)
+    //                   code0 >= 0 makes the next frame; code0 < 0 for prompt rows, the residual LM only reads them,
+    //                   n_codes of them (1 if 0) in embd and feats, all text rows or all audio rows
     // yue2 GEN_CODE: n_codes is the frame count, codes is not read (the backbone state holds them)
     // backbone_state: llama_state_seq_get_data() of the backbone_n_past positions (prompt, codes, end token)
     const uint8_t * backbone_state;
     size_t          backbone_state_size;
     int32_t         backbone_n_past;

-    // flow matching: steps of the ODE solver, <= 0 = model default (yue2 GEN_CODE: 32, minimax_music3 GEN_WAV: 30)
+    // flow matching: steps of the ODE solver, <= 0 = model default (yue2 GEN_CODE: 32, minimax_music3 GEN_WAV: 30, voxcpm2 GEN_CODE: 10)
     int32_t n_steps;

     // minimax_music3 GEN_CODE: feats holds the backbone hidden rows, cond then uncond with guidance
-    // classifier-free guidance scale of the residual codes, <= 0 = model default (1.5)
+    // classifier-free guidance scale, <= 0 = model default (minimax_music3 residual codes: 1.5, voxcpm2 GEN_CODE: 2.0)
     float cfg_scale;
 };