mtmd: fix LFM2 image tiling threshold - #27057
Merged
Merged
Conversation
Collaborator
|
CC @tdakhran , seems like there was a fix similar to this that was merged recently. Could you confirm if this is still valid? |
Contributor
|
Thanks for the fix. We will evaluate it internally, and I'll report back. |
Contributor
|
@ngxson, does this look good to merge, or do we need anything else? |
Collaborator
|
this PR adds a new test infra structure, I need to clean it up first |
tdakhran
suggested changes
Aug 17, 2026
tdakhran
reviewed
Aug 17, 2026
tdakhran
approved these changes
Aug 17, 2026
ngxson
force-pushed
the
test/lfm2-area-tiling
branch
from
August 17, 2026 17:46
f99ed87 to
7dfec7b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.