Skip to content

mtmd: fix LFM2 image tiling threshold - #27057

Merged
ngxson merged 6 commits into
ggml-org:masterfrom
BlackFoil:test/lfm2-area-tiling
Aug 18, 2026
Merged

mtmd: fix LFM2 image tiling threshold#27057
ngxson merged 6 commits into
ggml-org:masterfrom
BlackFoil:test/lfm2-area-tiling

Conversation

@BlackFoil

Copy link
Copy Markdown
Contributor

Overview

This PR fixes a discrepancy in the LFM2-VL tiling logic by aligning its tiling predicate with the Hugging Face Transformers reference implementation, addressing significant OCR degradation observed at specific image resolutions.

Details of Changes
Fixed Tiling Condition: The previous tiling logic for LFM2-VL was based on edge length (width/height), which differed from the Transformers reference implementation. This has been updated to use a pixel-area-based condition to align with the reference tiling criterion.

Mitigated Tiling-Induced OCR Degradation: The previous edge-based logic caused the processing path to diverge incorrectly for certain image resolutions (e.g., 1024x977). Ensuring the correct tiling path mitigates the severe degradation caused by the wrong path, and we observed substantial OCR performance recovery specifically for the tested 1024x977 case.

Added Regression Test: Added a regression test to verify the tiling decision behavior and prevent future regressions.

Additional information

Empirical Encoding Observations:
Under our specific validation setup (testing OCR on Japanese forms with the same model), we observed the following behavior:

For 1024x977: The old logic resulted in 1 encoding, whereas the new logic produces 5 encodings, leading to the substantial OCR recovery mentioned above.

For 1025x978: The behavior remained consistent at 5 encodings in both implementations.

Relation to Issue #17290:
This PR shares highly similar symptoms with Issue #17290 (severe output degradation around 1024px) where tiling is also discussed. However, the investigations in #17290 encompass multiple preprocessing differences (such as resize/padding, position embeddings, and antialiasing). Therefore, while related, this PR specifically corrects the area-based tiling predicate and may not encompass all the regressions discussed in #17290.

Testing Details:

Added a regression test (test-mtmd-image.cpp) that explicitly verifies the tiling decision across specific resolutions (e.g., 704x704, 736x736, 1024x977, 1056x384).

Confirmed that this test FAILS when applying the old edge-based logic to the predicate, and PASSES with the new area-based logic.

Executed the full local CI suite (ci/run.sh) and confirmed all tests passed.

Ran llama-perplexity and llama-bench (tested on Qwen3 0.6B Q4_0). Observed no regressions in this specific setup (PPL matched exactly at 12.1282, and benchmark throughput differences in tokens/sec were within measurement noise).

Requirements

I have read and agree with the contributing guidelines

AI usage disclosure: YES - I identified the bug and used an AI assistant as a copilot to investigate the root cause, analyze the logic, and help draft the code fix and regression test. I have reviewed and tested the changes myself, and I take responsibility for all submitted code.

@BlackFoil
BlackFoil requested review from a team and ggerganov as code owners August 14, 2026 09:56
@github-actions github-actions Bot added testing Everything test related mtmd Related to multimodal functionality (video/image/audio) labels Aug 14, 2026
@ngxson

ngxson commented Aug 14, 2026

Copy link
Copy Markdown
Collaborator

CC @tdakhran , seems like there was a fix similar to this that was merged recently. Could you confirm if this is still valid?

@tdakhran

Copy link
Copy Markdown
Contributor

Thanks for the fix. We will evaluate it internally, and I'll report back.

@tdakhran tdakhran left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tested, looks good

@tdakhran

Copy link
Copy Markdown
Contributor

@ngxson, does this look good to merge, or do we need anything else?

@ngxson

ngxson commented Aug 17, 2026

Copy link
Copy Markdown
Collaborator

this PR adds a new test infra structure, I need to clean it up first

Comment thread tools/mtmd/mtmd-image.cpp Outdated
Comment thread tools/mtmd/clip-impl.h
@ngxson
ngxson force-pushed the test/lfm2-area-tiling branch from f99ed87 to 7dfec7b Compare August 17, 2026 17:46
@ngxson
ngxson merged commit 7acdbb1 into ggml-org:master Aug 18, 2026
23 of 27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mtmd Related to multimodal functionality (video/image/audio) testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants