Skip to content

mtmd: add minicpmv46 downsample - #25993

Merged
ngxson merged 6 commits into
ggml-org:masterfrom
tc-mb:minicpmv46-downsample-mode
Aug 1, 2026
Merged

ngxson merged 6 commits into
ggml-org:masterfrom
tc-mb:minicpmv46-downsample-mode

Conversation

@tc-mb

@tc-mb tc-mb commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Overview

This branch adds configurable visual token downsampling for MiniCPM-V 4.6:

  • Supports 4x and 16x modes, defaulting to the model’s 16x mode.
  • Skips the intermediate ViT merger in 4x mode to retain more visual tokens.

@github-actions github-actions Bot added server mtmd Related to multimodal functionality (video/image/audio) labels Jul 22, 2026
@tc-mb
tc-mb marked this pull request as ready for review July 22, 2026 11:26
@tc-mb
tc-mb requested review from a team as code owners July 22, 2026 11:26
Comment thread common/arg.cpp Resolved
Comment thread tools/mtmd/mtmd-image.h Resolved
@tc-mb
tc-mb requested a review from CISC as a code owner July 24, 2026 07:09
@ngxson ngxson removed the server label Jul 24, 2026
@ngxson

ngxson commented Jul 24, 2026

Copy link
Copy Markdown
Collaborator

@ggml-gh-bot review

@ggml-gh-bot

ggml-gh-bot Bot commented Jul 24, 2026 •

Copy link
Copy Markdown
Automated code review

I have completed my static review. Here are my findings.


Review: PR #25993 - MiniCPM-V 4.6 4x downsample mode

Scope: 6 files in conversion/ and tools/mtmd/, adding a 4x (total) downsample variant to the existing PROJECTOR_TYPE_MINICPMV4_6 projector. No ggml/backend, public API, or server changes. Checklists run: scope gate, security, general, plus approach/design for the graph-build refactor and new preprocessor subclass.

The graph/encoder indexing and input-name parity check out for both modes: n_out = (pos_h/2)*(pos_w/2) for is_4x matches the encoder's merger_ds_idx sizes, clip_n_output_tokens (n_patches /= n_merge*n_merge) matches the graph output count, gather indices stay in [0, n_pos), and the encoder's if (!is_4x) guards line up exactly with the graph's conditional tensor creation. The build_vit_layers lambda is a faithful dedup of the two previously-unrolled layer loops. The GGML_ASSERT(hparams.n_merge == 2 || hparams.n_merge == 4) is a good bound on the GGUF-supplied value.

Blocking

(point 1) tools/mtmd/clip.cpp:2015-2031 - the loader still unconditionally requires the v.vit_merger.* weight tensors (get_tensor(...) with the default required = true) for every MINICPMV4_6 GGUF. The new is_4x (n_merge == 2) graph path in tools/mtmd/models/minicpmv.cpp skips the ViT merger entirely, so for a 4x checkpoint that does not ship those weights (which the "final merger only" architecture implies), clip_init throws unable to find tensor v.vit_merger.ln1.weight at load time. hparams.n_merge is already populated by load_hparams (clip.cpp:1313), which runs before load_tensors (clip.cpp:3150-3151), so gate these get_tensor calls (lines 2015-2031) on hparams.n_merge == 4 (the 16x path). This is strictly safer in all cases: it avoids the crash if the 4x checkpoint omits the tensors, and avoids loading unused weights if it happens to include them. Please verify against the actual 4x checkpoint either way.

Will slow the review

(point 2) No end-to-end validation is shown for the new 4x path. Per CONTRIBUTING.md, verify token counts and CLIP embeddings against the upstream MiniCPMV4_6ImageProcessorPil / HF reference for a real downsample_mode: "4x" checkpoint, and confirm the existing 16x path is unchanged (it should be: the new preprocessor falls through to the base for n_merge == 4, and the graph's !is_4x branch reproduces the old unrolled code).

(point 3) tools/mtmd/mtmd-image.cpp:939-953 - the small-image override (n_merge == 2 && ratio <= 1.0) returns an aspect-ratio-preserved overview via get_best_resize(..., allow_upscale=true), which differs from the base llava_uhd behavior (a fixed slice_size x slice_size square when !has_slices). Confirm this matches the upstream 4x processor. Note the condition is area-based (ratio <= 1.0) while the base uses dimension-based (width > slice_size || height > slice_size), so a wide-but-small-area image (e.g. 1000x200 at slice_size 448) now produces a single overview instead of being sliced; make sure that is intended for 4x. For ratio > 1.0 the override falls through to the base dynamic-grid slicing with patch_size*n_merge = patch_size*2 alignment, which is correct for 4x but should also be checked against upstream tile counts/sizes.

(point 4) docs/multimodal/minicpmv4.6.md describes only the 16x architecture ("window-attention vit_merger + DownsampleMLP merger"). Add a note about downsample_mode and the 4x variant (final merger only, no vit_merger) so users know both are supported.

Nits

(point 5) tools/mtmd/clip.cpp:1311-1312 - the comment "ViT merger 2x2 + final merger 2x2 = 4x spatial merge per dimension" above hparams.n_merge = 4 now only describes the 16x case and is misleading for n_merge == 2. Rephrase to explain both values: 4 = ViT merger + final merger (16x total); 2 = final merger only (4x total).

(point 6) The is_4x = (hparams.n_merge == 2) mapping is duplicated in three places (clip.cpp:3667, tools/mtmd/models/minicpmv.cpp:117, tools/mtmd/mtmd-image.cpp:940). The "n_merge == 2 means 4x-total / no ViT merger" rule is undocumented in one place; a single shared comment (or a small helper) would keep future modes from diverging.

(point 7) tools/mtmd/models/minicpmv.cpp:122 - in the 4x branch n_out = n_ds, but n_ds is named for the ViT-merger downsample count, which does not exist in 4x mode. A one-line comment clarifying that in 4x mode the final merger runs on the full grid would help readers.

(point 8) tools/mtmd/clip.cpp:1314 adds a good bound on n_merge; consider also bounding insert_layer_id (read at clip.cpp:1320 from wa_layer_indexes[0]) against n_layer. It is used to index model.layers[il] in the !is_4x path (minicpmv.cpp:243), and a crafted GGUF could set it >= n_layer. This is pre-existing, not introduced here, but the assert is the natural place to harden it.

This review was generated automatically by pi coding agent using zai-org/GLM-5.2. It may contain mistakes. Maintainers make the final call.

@ngxson

ngxson commented Jul 24, 2026

Copy link
Copy Markdown
Collaborator

@tc-mb ok seems like there is just one blocking change left: the merger tensor is always required, it may break existing ggufs

please confirm if this is expected, or is indeed a bug

@tc-mb

tc-mb commented Jul 25, 2026

Copy link
Copy Markdown
Contributor Author

@tc-mb ok seems like there is just one blocking change left: the merger tensor is always required, it may break existing ggufs

please confirm if this is expected, or is indeed a bug

minicpm-v4.6 contains two modes, and the down-sampling switch I added at the beginning is for this purpose. Then 16x compression uses vit merger, but 4x compression does not, as you asked here. So do you have any suggestions for this switchable vit?

  1. Still control it through different parameters in gguf, and then export two types of mproj.
  2. Or is it an mmproj and then adjusted by some parameter?

I will explain the situation and hope to get your suggestions.

@ngxson

ngxson commented Jul 25, 2026 •

Copy link
Copy Markdown
Collaborator

minicpm-v4.6 contains two modes, and the down-sampling switch I added at the beginning is for this purpose. Then 16x compression uses vit merger, but 4x compression does not, as you asked here. So do you have any suggestions for this switchable vit?

Please correct if I'm wrong, I understand this as follow: 4x model doesn't use vit_merger_ln tensor, but it's still marked as a "required" tensor here:

llama.cpp/tools/mtmd/clip.cpp

Lines 2031 to 2032 in 555881e

model.vit_merger_ln1_w = get_tensor(string_format(TN_VIT_MERGER_LN1, "weight"));
model.vit_merger_ln1_b = get_tensor(string_format(TN_VIT_MERGER_LN1, "bias"));

So this means 4x model will fail to load because it misses these vit_merger_ln* tensors.

Proposed solution: Conditionally switch the "required" flag based on n_merge, example:

bool is_merger_required = n_merge == 4;
get_tensor(string_format(TN_VIT_MERGER_LN1, "weight"), is_merger_required);

@ngxson

ngxson commented Jul 25, 2026

Copy link
Copy Markdown
Collaborator

A better suggestion:

model.vit_merger_ln1_w = get_tensor(string_format(TN_VIT_MERGER_LN1, "weight"), false);

bool is_merger_required = n_merge == 4;
if (is_merger_required && !model.vit_merger_ln1_w) {
  throw std::runtime_exception("merger tensor is missing");
}

@ngxson

ngxson commented Jul 27, 2026

Copy link
Copy Markdown
Collaborator

Gentle ping @tc-mb in case you missed the notification. It should be good to merge once you confirm the question above

@tc-mb

tc-mb commented Jul 27, 2026

Copy link
Copy Markdown
Contributor Author

Gentle ping @tc-mb in case you missed the notification. It should be good to merge once you confirm the question above

OK, I held a meetup event on Sunday, delayed for a few days. I will revise it as soon as possible.

@tc-mb

tc-mb commented Jul 27, 2026

Copy link
Copy Markdown
Contributor Author

@ngxson ok, I made a skipped revision, which should conform to your suggestion. I hope you can take a look when you have time.

@tc-mb

tc-mb commented Aug 1, 2026

Copy link
Copy Markdown
Contributor Author

Gentle ping @tc-mb in case you missed the notification. It should be good to merge once you confirm the question above

I have revised it. Please check it again when you have time.

@ngxson ngxson changed the title add minicpmv46 downsample mtmd: add minicpmv46 downsample Aug 1, 2026
@ngxson
ngxson merged commit de69995 into ggml-org:master Aug 1, 2026
23 of 28 checks passed
@tc-mb
tc-mb deleted the minicpmv46-downsample-mode branch August 1, 2026 13:55
huaxel pushed a commit to huaxel/strix-llama.cpp that referenced this pull request Aug 2, 2026
* add minicpmv46 downsample

Signed-off-by: tc-mb <[email protected]>

* put downsample mode inside gguf.

Signed-off-by: tc-mb <[email protected]>

* build mtmd_image_preprocessor_llava_uhd

Signed-off-by: tc-mb <[email protected]>

* fix code

Signed-off-by: tc-mb <[email protected]>

* add convert

Signed-off-by: tc-mb <[email protected]>

* add 4x ignore vit merger

Signed-off-by: tc-mb <[email protected]>

---------

Signed-off-by: tc-mb <[email protected]>
satindergrewal pushed a commit to satindergrewal/llama.cpp that referenced this pull request Aug 12, 2026
* add minicpmv46 downsample

Signed-off-by: tc-mb <[email protected]>

* put downsample mode inside gguf.

Signed-off-by: tc-mb <[email protected]>

* build mtmd_image_preprocessor_llava_uhd

Signed-off-by: tc-mb <[email protected]>

* fix code

Signed-off-by: tc-mb <[email protected]>

* add convert

Signed-off-by: tc-mb <[email protected]>

* add 4x ignore vit merger

Signed-off-by: tc-mb <[email protected]>

---------

Signed-off-by: tc-mb <[email protected]>
thecodacus pushed a commit to thecodacus/llama.cpp that referenced this pull request Sep 7, 2026
* add minicpmv46 downsample

Signed-off-by: tc-mb <[email protected]>

* put downsample mode inside gguf.

Signed-off-by: tc-mb <[email protected]>

* build mtmd_image_preprocessor_llava_uhd

Signed-off-by: tc-mb <[email protected]>

* fix code

Signed-off-by: tc-mb <[email protected]>

* add convert

Signed-off-by: tc-mb <[email protected]>

* add 4x ignore vit merger

Signed-off-by: tc-mb <[email protected]>

---------

Signed-off-by: tc-mb <[email protected]>
zbrad pushed a commit to zbrad/llama.cpp that referenced this pull request Sep 10, 2026
* add minicpmv46 downsample

Signed-off-by: tc-mb <[email protected]>

* put downsample mode inside gguf.

Signed-off-by: tc-mb <[email protected]>

* build mtmd_image_preprocessor_llava_uhd

Signed-off-by: tc-mb <[email protected]>

* fix code

Signed-off-by: tc-mb <[email protected]>

* add convert

Signed-off-by: tc-mb <[email protected]>

* add 4x ignore vit merger

Signed-off-by: tc-mb <[email protected]>

---------

Signed-off-by: tc-mb <[email protected]>
alex-spacemit pushed a commit to spacemit-com/llama.cpp that referenced this pull request Oct 15, 2026
Sync 1575 upstream commits since fork point 79bba02 (2026-07-16).

Highlights brought in from upstream:
- MiniCPM-V 4.7 support (ggml-org#29416) and minicpmv46 downsample (ggml-org#25993)
- 22 new model architectures (kimi-k3, glm5-next, qwen4exp, qwen3tts,
  minimax-01/m3, granite-switch, k2-horizon, ...)
- mtmd refactors: internal headers, helper split, new vision/audio models
- server: MCP support, decision contexts, batch_ext migration
- common: chat parsers split into common/parsers/, json split
- llama: load_mode AUTO + lazy_mode on-demand tensor loading
- ggml-cpu spacemit upstream updates (Q8_0 IME1 ggml-org#28479, Q5_0 dispatch fix)

Merge resolutions (keeping our side):
- ggml-spacemit standalone backend registration (CMake, ggml-cpu.c)
- GGML_BUILD_PROFILE in ggml/src/CMakeLists.txt
- SMT media backend: --media-backend/--smt-config-dir args, media chunk
  wrapper (server_media_chunk), SMT prefill in process_mtmd_chunk,
  media worker routing for encode, multi-ASR service init
- Kaldi fbank + LFR helpers in mtmd-audio
- TTS server mode entry in llama-server main
- llama-media link and server-media/server-tts sources in server CMake

Merge resolutions (taking upstream):
- load_mode/lazy_mode evolution (our cherry-picked older revision was
  superseded by upstream's final AUTO/mmap+mlock/lazy-mode design)
- qwen3 pooled-embeddings LM head skip (ggml-org#30445 supersedes our guard)
- upstream server_tokens serialize/deserialize, adapted to wrap
  server_media_chunk (SMT embd chunks throw on serialize for now)
- check_no_mtmd removed (upstream removed all call sites)

Co-Authored-By: Claude Code <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

conversion mtmd Related to multimodal functionality (video/image/audio)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants