Fix OOM in dynamic_preprocess for extreme aspect ratios#1256
Open
Mr-Neutr0n wants to merge 1 commit intoOpenGVLab:mainfrom
Open
Fix OOM in dynamic_preprocess for extreme aspect ratios#1256Mr-Neutr0n wants to merge 1 commit intoOpenGVLab:mainfrom
Mr-Neutr0n wants to merge 1 commit intoOpenGVLab:mainfrom
Conversation
…spect ratios Images with extreme aspect ratios (e.g., very wide panoramas or tall screenshots) can cause excessive memory allocation in dynamic_preprocess because the function generates patch grid ratios without any aspect ratio filtering. This can lead to OOM errors, especially when max_num is set to high values. Changes: - Cap max_num at MAX_PATCHES_LIMIT (24) to prevent runaway patch counts - Filter out target ratios where either dimension ratio exceeds MAX_ASPECT_RATIO_THRESHOLD (200) to avoid degenerate patch grids - Add a safety fallback to (1,1) if all ratios are filtered out - Apply the fix consistently across all three copies of the function: internvl_chat, internvl_chat_gpt_oss, and streamlit_demo
Author
|
Friendly bump! Let me know if there's anything I should update or improve to help move this forward. |
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.
Summary
dynamic_preprocessto prevent out-of-memory (OOM) errors when processing images with extreme aspect ratios or whenmax_numis set to very high values.max_numatMAX_PATCHES_LIMIT(24) to prevent runaway patch counts that lead to excessive memory allocation.MAX_ASPECT_RATIO_THRESHOLDof 200) to avoid degenerate patch grids like(12, 1)that produce large intermediate images without useful visual information.(1, 1)grid if all candidate ratios are filtered out.internvl_chat,internvl_chat_gpt_oss, andstreamlit_demo.Closes #1221
Details
When
dynamic_preprocessreceives an image with extreme proportions (e.g., a very wide panorama or a very tall screenshot), the generated target ratios can include highly elongated grids. Combined with largemax_numvalues that can be configured via training metadata (max_dynamic_patch), this causes:5376x448pixels for a(12, 1)grid withimage_size=448)The fix introduces two configurable module-level constants:
MAX_PATCHES_LIMIT = 24: Hard upper bound on patch count regardless ofmax_numparameterMAX_ASPECT_RATIO_THRESHOLD = 200: Maximum allowed ratio between grid dimensionsThese values are intentionally generous to avoid affecting normal usage while preventing pathological cases.
Test plan
max_numvalues > 24 to confirm capping works correctly(1, 1)works when all ratios are filtered