Skip to content

Commit d3ac030

Browse files
mtmd : fix LightOnOCR image preprocessing (ggml-org#20877)
1 parent 49bfdde commit d3ac030

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

tools/mtmd/clip.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,7 +1161,6 @@ struct clip_model_loader {
11611161
hparams.set_warmup_n_tokens(16*16);
11621162
} break;
11631163
case PROJECTOR_TYPE_PIXTRAL:
1164-
case PROJECTOR_TYPE_LIGHTONOCR:
11651164
{
11661165
// ref: https://huggingface.co/mistral-community/pixtral-12b/blob/main/preprocessor_config.json
11671166
// TODO: verify the image_min_tokens
@@ -1171,6 +1170,15 @@ struct clip_model_loader {
11711170
hparams.set_limit_image_tokens(8, 1024);
11721171
hparams.set_warmup_n_tokens(256); // avoid OOM on warmup
11731172
} break;
1173+
case PROJECTOR_TYPE_LIGHTONOCR:
1174+
{
1175+
hparams.n_merge = 1;
1176+
hparams.rope_theta = 10000.0f;
1177+
get_u32(KEY_SPATIAL_MERGE_SIZE, hparams.n_merge, false);
1178+
hparams.image_longest_edge = hparams.image_size;
1179+
get_u32(KEY_PREPROC_IMAGE_SIZE, hparams.image_longest_edge, false);
1180+
hparams.set_warmup_n_tokens(256); // avoid OOM on warmup
1181+
} break;
11741182
case PROJECTOR_TYPE_KIMIVL:
11751183
{
11761184
hparams.rope_theta = 10000.0f;
@@ -3180,7 +3188,6 @@ bool clip_image_preprocess(struct clip_ctx * ctx, const clip_image_u8 * img, str
31803188

31813189
case PROJECTOR_TYPE_PHI4:
31823190
case PROJECTOR_TYPE_PIXTRAL:
3183-
case PROJECTOR_TYPE_LIGHTONOCR:
31843191
{
31853192
GGML_ASSERT(params.image_min_pixels > 0 && params.image_max_pixels > 0);
31863193
clip_image_u8 resized_image;
@@ -3196,6 +3203,19 @@ bool clip_image_preprocess(struct clip_ctx * ctx, const clip_image_u8 * img, str
31963203
normalize_image_u8_to_f32(resized_image, *img_f32, params.image_mean, params.image_std);
31973204
res_imgs->entries.push_back(std::move(img_f32));
31983205
} break;
3206+
case PROJECTOR_TYPE_LIGHTONOCR:
3207+
{
3208+
GGML_ASSERT(params.image_longest_edge > 0);
3209+
clip_image_u8 resized_image;
3210+
const clip_image_size target_size = img_tool::calc_size_preserved_ratio(
3211+
original_size,
3212+
params.patch_size * params.n_merge,
3213+
params.image_longest_edge);
3214+
img_tool::resize(*img, resized_image, target_size, img_tool::RESIZE_ALGO_BICUBIC);
3215+
clip_image_f32_ptr img_f32(clip_image_f32_init());
3216+
normalize_image_u8_to_f32(resized_image, *img_f32, params.image_mean, params.image_std);
3217+
res_imgs->entries.push_back(std::move(img_f32));
3218+
} break;
31993219

32003220
case PROJECTOR_TYPE_LLAMA4:
32013221
{

0 commit comments

Comments
 (0)