llama.cpp/tools/mtmd
Pascal c943331339
model : add hy_v4_flash on the DSA iSWA cache (#32960)
* model : add hy_v4_flash on the DSA iSWA cache

Tencent Hy4 flash is a hy_v4 trunk (iHC, gated MLA, sinks) with
sparse and sliding-window MLA layers, sandwich norms, latent routed
experts and deepseek41 Engram tables.

The sparse layers select their context with a pooled indexer: the
keys of 4 consecutive tokens are pooled by a per-channel softmax of a
gate plus a position bias, normed, and rotated at the position of the
first token. A token attends to the best scored pools of its sequence
plus its own pool.

The model runs on llama_kv_cache_dsa_iswa as dots3note does. The
indexer cache holds the projected value and gate of each token, and
the pools are rebuilt from the cells for every ubatch by
llama_kv_cache::set_input_kpool(), so there is no pooled state to
keep in sync with sequence edits, checkpoints or state save/restore.
The selection goes through ggml_top_k_mask with the pool members as
blocks and the token's own pool as extra cells.

Not covered yet: the MTP block (its tensors are skipped), the vision
tower, multiple KV streams, and backends without GGML_OP_TOP_K_MASK.

* convert : add hy_v4_flash

The converter of the hy_v4_flash model, without the vision parts.

The four Engram tables are written as one per_layer_token_embd
tensor of about 127 GB in BF16. Concatenating them with torch.cat
materializes the whole tensor at write time, so they are streamed one
table at a time in row chunks through the _StreamedBytes path of
deepseek41, which keeps the conversion within 100 GB of RAM.

* tests : add hy_v4_flash to test-llama-archs

A 4 layer fixture that alternates sparse and sliding-window layers,
with hyper-connections, one Engram module, an indexer on the sparse
layers and a latent width distinct from the expert width, so that a
transposed routed_down or routed_up is caught.

* kv-cache : do not shift the pooled indexer cache

The pooled indexer of hy_v4_flash caches the value and the gate of
each token, without a position: the pooled key is rotated in the
graph. A K-shift rotated these rows as if they were roped keys, which
broke the pooled keys after a context shift.

* tests : add the hy_v4_flash Metal fusion baseline

* model, mtmd : add hy_v4_flash vision

The vision tower of hy_v4_flash, with its projector in mtmd and its
converter.

The published files carry M-RoPE sections, so the text model runs the
interleaved M-RoPE path: the rope rows keep their reference order and
are reordered to NeoX halves in the graph. The tokens of an image share
their position, so the indexer pools now group consecutive tokens in
sequence order instead of consecutive positions, and a pooled key is
rotated at the four positions of its first member.

Without a backend that runs GGML_OP_TOP_K_MASK, the picked pools are
expanded to their cells and go through build_top_k_mask.

* CUDA: lightning indexer for 16 heads

hy_v4_flash scores its pools with 16 indexer heads, which the CUDA
backend did not support, so the scheduler ran the 12 sparse layers'
indexers on the CPU at every step. Both kernels already handle 16
heads: the mma kernel needs a multiple of 16 and the vector kernel
splits the heads in 4 passes.

* model : move the Hy4 flash Engram gathers before the token embeddings

With the gathers after build_inp_embd, the mixed embedding DUP and SET_ROWS
sat between two host nodes and stayed on the CPU. Those ops use every thread,
so all the CPU workers woke up on each decoded token for a 12K copy.
Gathering first lets the scheduler put them on the GPU, as in qwen4exp, and
leaves only small single threaded lookups on the host.

* llama : speed up the Hy4 flash pool inputs on long contexts

set_input_kpool deduplicated the pools through a map keyed by vectors even
with a single sequence, sorted cells that are already in order and scanned
every pool owner for each token. Dedup only when the ubatch has several
sequences, skip the sort when the cells are ordered and mark the earlier
pools of the token's sequence directly. At 129k cells the host cost of a
decode step drops from 4.0 ms to 0.5 ms, the logits are unchanged.

* llama : use a unified KV cache for Hy4 flash

The pools gather their members from a single KV stream, so a split cache
with several sequences hit an assert in the graph. Switch to the unified
cache with a warning, the sequences then share n_ctx.

* rm redundant code in mtmd

* clean up comments

---------

Co-authored-by: Xuan Son Nguyen <son@huggingface.co>
2026-11-25 18:39:50 +01:00
..
debug mtmd: refactoring: remove clip.h, consolidate code into mtmd (#31865) 2026-11-08 15:49:06 +01:00
legacy-models chore : correct typos [no ci] (#20041) 2026-03-05 08:50:21 +01:00
models model : add hy_v4_flash on the DSA iSWA cache (#32960) 2026-11-25 18:39:50 +01:00
tests sampler : remove "full-context windows" from history-based samplers (#26524) 2026-08-04 21:28:55 +03:00
CMakeLists.txt model : add hy_v4_flash on the DSA iSWA cache (#32960) 2026-11-25 18:39:50 +01:00
deprecation-warning.cpp llama : add missing headers (#28566) 2026-09-08 12:59:53 +02:00
mtmd-audio.cpp mtmd: speed up shared audio FFT (#32815) 2026-11-22 22:04:24 +01:00
mtmd-audio.h mtmd: speed up shared audio FFT (#32815) 2026-11-22 22:04:24 +01:00
mtmd-cli.cpp mtmd: support kyutai/hibiki realtime full-duplex (#32491) 2026-11-21 14:25:19 +01:00
mtmd-graph.cpp model : add hy_v4_flash on the DSA iSWA cache (#32960) 2026-11-25 18:39:50 +01:00
mtmd-graph.h mtmd: refactoring: remove clip.h, consolidate code into mtmd (#31865) 2026-11-08 15:49:06 +01:00
mtmd-helper-common.h mtmd : allow custom video decoder (#32188) 2026-11-14 14:12:07 +01:00
mtmd-helper-gen.cpp mtmd: support kyutai/hibiki realtime full-duplex (#32491) 2026-11-21 14:25:19 +01:00
mtmd-helper-video-ffmpeg.cpp mtmd: fix video pixel aspect ratio and video rotation (#32267) 2026-11-14 14:57:21 +01:00
mtmd-helper.cpp mtmd: support kyutai/hibiki realtime full-duplex (#32491) 2026-11-21 14:25:19 +01:00
mtmd-helper.h mtmd: support kyutai/hibiki realtime full-duplex (#32491) 2026-11-21 14:25:19 +01:00
mtmd-image.cpp feat: add Inkling 2 support (text + vision) (#32217) 2026-11-15 00:51:31 +01:00
mtmd-image.h feat: add Inkling 2 support (text + vision) (#32217) 2026-11-15 00:51:31 +01:00
mtmd-impl.h model : add hy_v4_flash on the DSA iSWA cache (#32960) 2026-11-25 18:39:50 +01:00
mtmd-internal.h mtmd: accurate support for qwen3-vl video prompt format (#31619) 2026-11-03 23:57:26 +01:00
mtmd-model.cpp model : add hy_v4_flash on the DSA iSWA cache (#32960) 2026-11-25 18:39:50 +01:00
mtmd-model.h mtmd: drama3 voice cloning (#32586) 2026-11-20 18:34:52 +01:00
mtmd-subcontext.cpp model : add hy_v4_flash on the DSA iSWA cache (#32960) 2026-11-25 18:39:50 +01:00
mtmd-subcontext.h mtmd: support voxcpm2 (#32443) 2026-11-19 02:39:05 +01:00
mtmd.cpp model : add hy_v4_flash on the DSA iSWA cache (#32960) 2026-11-25 18:39:50 +01:00
mtmd.h mtmd: support kyutai/hibiki realtime full-duplex (#32491) 2026-11-21 14:25:19 +01:00
README-dev.md mtmd: support voxcpm2 (#32443) 2026-11-19 02:39:05 +01:00
README.md mtmd: refactoring: remove clip.h, consolidate code into mtmd (#31865) 2026-11-08 15:49:06 +01:00
requirements.txt requirements, Fix CI: support Python 3.15 (#32199) 2026-11-13 17:50:03 +01:00
test-1.jpeg mtmd : rename llava directory to mtmd (#13311) 2025-05-05 16:02:55 +02:00
test-2.mp3 mtmd : support Qwen 2.5 Omni (input audio+vision, no audio output) (#13784) 2025-05-27 14:06:10 +02:00
test-3.mp4 mtmd : add video input support (#24269) 2026-06-08 14:40:12 +03:00
tests.sh mtmd: add batching for mtmd-cli, add video tests (#24778) 2026-06-18 21:55:04 +02:00

Multimodal Support in llama.cpp

This directory provides multimodal capabilities for llama.cpp. Initially intended as a showcase for running LLaVA models, its scope has expanded significantly over time to include various other vision-capable models. As a result, LLaVA is no longer the only multimodal architecture supported.

Important

Multimodal support can be viewed as a sub-project within llama.cpp. It is under very heavy development, and breaking changes are expected.

The naming and structure related to multimodal support have evolved, which might cause some confusion. Here's a brief timeline to clarify:

  • #3436: Initial support for LLaVA 1.5 was added, introducing llava.cpp and clip.cpp. The llava-cli binary was created for model interaction.
  • #4954: Support for MobileVLM was added, becoming the second vision model supported. This built upon the existing llava.cpp, clip.cpp, and llava-cli infrastructure.
  • Expansion & Fragmentation: Many new models were subsequently added (e.g., #7599, #10361, #12344, and others). However, llava-cli lacked support for the increasingly complex chat templates required by these models. This led to the creation of model-specific binaries like qwen2vl-cli, minicpmv-cli, and gemma3-cli. While functional, this proliferation of command-line tools became confusing for users.
  • #12849: libmtmd was introduced as a replacement for llava.cpp. Its goals include providing a single, unified command-line interface, improving the user/developer experience (UX/DX), and supporting both audio and image inputs.
  • #13012: mtmd-cli was added, consolidating the various model-specific CLIs into a single tool powered by libmtmd.

Pre-quantized models

See the list of pre-quantized model here

How it works and what is mmproj?

Multimodal support in llama.cpp works by encoding images into embeddings using a separate model component, and then feeding these embeddings into the language model.

This approach keeps the multimodal components distinct from the core libllama library. Separating these allows for faster, independent development cycles. While many modern vision models are based on Vision Transformers (ViTs), their specific pre-processing and projection steps can vary significantly. Integrating this diverse complexity directly into libllama is currently challenging.

Consequently, running a multimodal model typically requires two GGUF files:

  1. The standard language model file.
  2. A corresponding multimodal projector (mmproj) file, which handles the image encoding and projection.

What is libmtmd?

As outlined in the history, libmtmd is the modern library designed to replace the original llava.cpp implementation for handling multimodal inputs.

Originally built upon clip.cpp (similar to llava.cpp), libmtmd offers several advantages:

  • Unified Interface: Aims to consolidate interaction for various multimodal models.
  • Improved UX/DX: Features a more intuitive API, inspired by the Processor class in the Hugging Face transformers library.
  • Flexibility: Designed to support multiple input types (text, audio, images) while respecting the wide variety of chat templates used by different models.

How to obtain mmproj

Multimodal projector (mmproj) files are specific to each model architecture.

For the following models, you can use convert_hf_to_gguf.py with --mmproj flag to get the mmproj file:

  • Gemma 3 ; See the guide here - Note: 1B variant does not have vision support
  • SmolVLM (from HuggingFaceTB)
  • SmolVLM2 (from HuggingFaceTB)
  • Pixtral 12B - only works with transformers-compatible checkpoint
  • Qwen 2 VL and Qwen 2.5 VL (from Qwen)
  • Mistral Small 3.1 24B
  • InternVL 2.5 and InternVL 3 from OpenGVLab (note: we don't support conversion of InternVL3-*-hf model, only non-HF version is supported ; InternLM2Model text model is not supported)
  • MiniCPM-V 4.6 ; See the guide here - requires the standard transformers v5.7.0+ checkpoint

For older models, please refer to the relevant guide for instructions on how to obtain or create them:

NOTE: conversion scripts are located under tools/mtmd/legacy-models