Skip to content

Commit bfa536c

Browse files
committed
fix(mtmd/qwen3vl): treat image_max_tiles=0 as unset, not an override
Zero-initialized clip_context_params/mtmd_context_params pass 0, which was clamped to 1 and silently forced single-tile preprocessing. Only a positive value now overrides the GGUF/model default.
1 parent 29a3c9f commit bfa536c

3 files changed

Lines changed: 14 additions & 6 deletions

File tree

tools/mtmd/clip.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2894,18 +2894,24 @@ struct clip_init_result clip_init(const char * fname, struct clip_context_params
28942894
// Only the Qwen3VL preprocessor reads preproc_max_tiles live; Qwen2/2.5VL use dyn_size
28952895
// and InternVL uses a candidate list already frozen in load_hparams, so the override is
28962896
// inert for them — apply (and validate) it only where it takes effect.
2897-
if (ctx_params.image_max_tiles != -1) {
2897+
//
2898+
// Treat only a POSITIVE value as an explicit override. -1 is the documented unset
2899+
// sentinel, and 0 is what a zero-initialized clip_context_params / mtmd_context_params
2900+
// passes (bindings / direct C API callers that never set the field). Both are treated
2901+
// as unset here so those callers keep the GGUF/model default instead of silently
2902+
// forcing single-tile and losing multi-tile preprocessing on large/high-res images.
2903+
if (ctx_params.image_max_tiles > 0) {
28982904
if (ctx_vision->model.proj_type != PROJECTOR_TYPE_QWEN3VL) {
28992905
LOG_WRN("%s: --image-max-tiles is only supported for Qwen3VL; ignoring for this model\n", __func__);
29002906
} else {
29012907
int max_tiles = ctx_params.image_max_tiles;
29022908
// Re-validate at this library boundary: bindings populate clip_context_params
29032909
// directly and bypass the CLI's [1,256] check. An unbounded value reaches the
29042910
// grid-fitting reserve in mtmd-image.cpp and can OOM the process.
2905-
if (max_tiles < 1 || max_tiles > CLIP_PREPROC_MAX_TILES_LIMIT) {
2906-
LOG_WRN("%s: --image-max-tiles=%d out of range [1,%d]; clamping\n",
2911+
if (max_tiles > CLIP_PREPROC_MAX_TILES_LIMIT) {
2912+
LOG_WRN("%s: --image-max-tiles=%d exceeds [1,%d]; clamping\n",
29072913
__func__, max_tiles, CLIP_PREPROC_MAX_TILES_LIMIT);
2908-
max_tiles = std::min(std::max(max_tiles, 1), CLIP_PREPROC_MAX_TILES_LIMIT);
2914+
max_tiles = CLIP_PREPROC_MAX_TILES_LIMIT;
29092915
}
29102916
ctx_vision->model.hparams.preproc_max_tiles = max_tiles;
29112917
LOG_INF("%s: preproc_max_tiles: %d (custom value)\n", __func__, max_tiles);

tools/mtmd/clip.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ struct clip_context_params {
5959
void * cb_eval_user_data;
6060
const char * backend_device; // optional, if null will use env var or default GPU backend
6161
int image_tile_mode; // 0=batched, 1=sequential (default), 2=disabled. NOTE: 0 (batched) is the zero value but NOT the default — init via mtmd_context_params_default() or set explicitly.
62-
int image_max_tiles; // override preproc_max_tiles; -1 = use GGUF/model default
62+
int image_max_tiles; // override preproc_max_tiles; -1 or 0 = use GGUF/model default (only a positive value overrides)
6363
};
6464

6565
struct clip_init_result {

tools/mtmd/mtmd.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ struct mtmd_context_params {
107107
// sequential) or set image_tile_mode explicitly. Values are fixed API (consumers pass "0"/"1"/"2").
108108
int image_tile_mode;
109109

110-
// override preproc_max_tiles from GGUF; -1 = use model default (4 for Qwen3VL 2B/4B)
110+
// override preproc_max_tiles from GGUF; -1 or 0 = use model default (4 for Qwen3VL 2B/4B).
111+
// Only a positive value is treated as an explicit override, so a zero-initialized struct
112+
// keeps the model default instead of forcing single-tile.
111113
// needed for 8B+ models whose GGUFs may lack the clip.vision.preproc_max_tiles key
112114
int image_max_tiles;
113115
};

0 commit comments

Comments
 (0)