Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transformers.image_transforms.normalize wrong types #35773

Merged
merged 1 commit into from
Jan 20, 2025

Conversation

CalOmnie
Copy link
Contributor

What does this PR do?

transformers.image_transforms.normalize types std and mean as Iterable, and checks for that, but calls len on them. The proper type for a sized iterable is Sequence, this PR adds that.

Fixes #35772

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?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

Copy link
Member

@Rocketknight1 Rocketknight1 left a comment

Choose a reason for hiding this comment

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

Yes, this is correct. Thanks for the fix!

@Rocketknight1 Rocketknight1 merged commit a142f16 into huggingface:main Jan 20, 2025
23 checks passed
bursteratom pushed a commit to bursteratom/transformers that referenced this pull request Jan 31, 2025
transformers.image_transforms.normalize documents and checks for the wrong type for std and mean arguments

Co-authored-by: Louis Groux <louis.cal.groux@gmail.com>
elvircrn pushed a commit to elvircrn/transformers that referenced this pull request Feb 13, 2025
transformers.image_transforms.normalize documents and checks for the wrong type for std and mean arguments

Co-authored-by: Louis Groux <louis.cal.groux@gmail.com>
@tomasruizt
Copy link

tomasruizt commented Feb 20, 2025

This PR was probably supposed to be a refactor, and should not change the execution paths of the code. However, it turns out that numpy arrays are Iterables but not Sequences, and therefore the execution paths changed. Client code that used to pass numpy arrays for mean and std fails now. We found this because it broke our MiniCPM video-chat pipeline. Case here: https://huggingface.co/openbmb/MiniCPM-V-2_6/discussions/53

from typing import Iterable, Sequence
import numpy as np

x = np.ones(3)
print(isinstance(x, Iterable))
#> True
print(isinstance(x, Sequence))
#> False

I can see that the type hint does not include numpy arrays, but passing a numpy array (which I guess is the intention of the type hint Sequence[float] / Iterable[float]) is reasonable, too. Would you say that client code is responsible for adjusting? Or could we revert? Or what would guys you suggest?

Best :)

Edit: I found that some client libs like vLLM are adjusting accordingly vllm-project/vllm@e8616d7

@CalOmnie
Copy link
Contributor Author

CalOmnie commented Feb 20, 2025

Oh, you're right, I went one level too high in the typing, the type needed was Collection not Sequence, see here

image

I will have a PR out tonight unless someone beats me to it.

>>> from typing import Collection
>>> import numpy as np
>>> a = np.ones(10)
>>> isinstance(a, Collection)
True

@CalOmnie
Copy link
Contributor Author

Alright I created a fix and modified some tests to make sure it worked here

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.

transformers.image_transforms.normalize documents and checks for the wrong type for std and mean arguments
3 participants