Skip to content

[llava] one pixel is missing from padding when length is odd#37819

Merged
zucchini-nlp merged 15 commits intohuggingface:mainfrom
cyr0930:fix/one_pixel_off
May 6, 2025
Merged

[llava] one pixel is missing from padding when length is odd#37819
zucchini-nlp merged 15 commits intohuggingface:mainfrom
cyr0930:fix/one_pixel_off

Conversation

@cyr0930
Copy link
Contributor

@cyr0930 cyr0930 commented Apr 28, 2025

One pixel is missing from padding when length is odd in llava family and aria model which is different from the logic in LLaVA-NeXT repo. In LLaVA-NeXT, this is automatically done as its logic resize image first and paste (or overwrite) original image to it (https://github.com/LLaVA-VL/LLaVA-NeXT/blob/main/llava/mm_utils.py#L186).

Also add vision_aspect_ratio argument to LlavaOnevisionImageProcessor to regulate max_num_patches during unpadding. It was hard-coded to "9" before. (https://github.com/LLaVA-VL/LLaVA-NeXT/blob/main/llava/model/llava_arch.py#L387)

And fix typo "processinf" to "processing" in documents.

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

@amyeroberts, @qubvel

@github-actions github-actions bot marked this pull request as draft April 28, 2025 03:13
@github-actions
Copy link
Contributor

Hi 👋, thank you for opening this pull request! The pull request is converted to draft by default. The CI will be paused while the PR is in draft mode. When it is ready for review, please click the Ready for review button (at the bottom of the PR page). This will assign reviewers and trigger CI.

@qubvel
Copy link
Contributor

qubvel commented Apr 28, 2025

cc @zucchini-nlp

Copy link
Member

@zucchini-nlp zucchini-nlp left a comment

Choose a reason for hiding this comment

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

Thanks for submitting PR, I agree it has to be padded in square. Can you update the model code as well, where we do unpadding and mark the PR as ready for review when finalized?

Comment on lines +270 to +266
max_num_patches = int(vision_aspect_ratio.strip("anyres_max_"))
ratio = math.sqrt(current_height * current_width / (max_num_patches * patches_height**2))
Copy link
Member

Choose a reason for hiding this comment

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

afaik we don't have hf-style checkpoints with different anyres patching. In that case there's no need to support since we try to not bloat code with unused features

Copy link
Contributor Author

@cyr0930 cyr0930 May 2, 2025

Choose a reason for hiding this comment

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

Oh I understood your intention.
However I think supporting "vision_aspect_ratio" argument is a good idea in the sense of consistency as the folloing code already allow variable vision_aspect_ratio.

https://github.com/huggingface/transformers/blob/v4.51.3/src/transformers/models/llava_onevision/modeling_llava_onevision.py#L450

Copy link
Member

@zucchini-nlp zucchini-nlp May 2, 2025

Choose a reason for hiding this comment

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

Ah oke, if code uses different anyres we can keep it :)

@cyr0930
Copy link
Contributor Author

cyr0930 commented May 2, 2025

Thanks for submitting PR, I agree it has to be padded in square. Can you update the model code as well, where we do unpadding and mark the PR as ready for review when finalized?

Okay I will. Thanks for the review tho.

@cyr0930 cyr0930 force-pushed the fix/one_pixel_off branch from ed31cb5 to cc581ab Compare May 2, 2025 07:04
@cyr0930 cyr0930 marked this pull request as ready for review May 2, 2025 07:15
@cyr0930
Copy link
Contributor Author

cyr0930 commented May 2, 2025

Maybe logic in LLaVA-NeXT project should be fixed too

Copy link
Member

@zucchini-nlp zucchini-nlp left a comment

Choose a reason for hiding this comment

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

Thanks a lot, looks good to me! Hm, just checked LLaVA NeXT repo and seems they unpad image by removing same pixels from height/width. We try to stay close to the original implementation, so would be great to flag it to authors yeah

Also, if you can add a small test for edge cases with odd shapes? As an example, a prev issue (#34522) on edge case image size where model fails to unpad correctly. After testing, we're good to merge :)

@cyr0930 cyr0930 force-pushed the fix/one_pixel_off branch from ea998a8 to 63bd86e Compare May 6, 2025 06:32
@cyr0930
Copy link
Contributor Author

cyr0930 commented May 6, 2025

Looks like "vision_aspect_ratio" is better to put in processor not in image_processor

@cyr0930 cyr0930 requested a review from zucchini-nlp May 6, 2025 09:31
Copy link
Member

@zucchini-nlp zucchini-nlp left a comment

Choose a reason for hiding this comment

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

Oke, sounds good to keep in processor. Thanks for the tests!

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@zucchini-nlp zucchini-nlp merged commit acded47 into huggingface:main May 6, 2025
20 checks passed
zucchini-nlp pushed a commit to zucchini-nlp/transformers that referenced this pull request May 14, 2025
…face#37819)

* [fix] one pixel should be added when length is odd

* [fix] add vision_aspect_ratio args & typo

* [fix] style

* [fix] do not fix fast file directly

* [fix] convert using modular

* remove duplicate codes

* match unpad logic with pad logic

* test odd-sized images for llava & aria

* test unpad odd-sized padding for llava family

* fix style

* add kwarg to onvision modular

* move vision_aspect_ratio from image_processor to processor
(llava_onevision)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants