Skip to content

Fix overriding Fast Image/Video Processors instance attributes affect other instances #39363

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

Merged

Conversation

yonigozlan
Copy link
Member

What does this PR do?

Fix an issue raised here #39172 (comment)
After internal discussions with @ydshieh , we found that the issue is not due to class attributes, but to lists/dicts being passed around without being copied.
In fact the same issue is also present in slow image processors, but we don't fix them here, as they all have their own attributes assignment logic, and are to be deprecated.

To reproduce the issue:

>>> o = CLIPImageProcessorFast()
>>> o.image_mean[0]
0.48145466
>>> CLIPImageProcessorFast.image_mean
[0.48145466, 0.4578275, 0.40821073]
>>> o.image_mean[0] = 1
>>> CLIPImageProcessorFast.image_mean
[1, 0.4578275, 0.40821073]
>>>

@yonigozlan yonigozlan requested a review from ydshieh July 11, 2025 14:43
@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.

image_processor_2.size[key] = -1
image_processor_2.image_mean[0] = -1

# check that the modified attributes of the first instance are not affected by the second instance
Copy link
Collaborator

Choose a reason for hiding this comment

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

if I understand correctly the purpose here, maybe the above block should be

        for key in image_processor_2.size:
            image_processor_2.size[key] = -2
        image_processor_2.image_mean[0] = -2

(or anything that is not -1)??

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah yes of course 😅, thanks

Copy link
Collaborator

@ydshieh ydshieh left a comment

Choose a reason for hiding this comment

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

Hopefully no edge case we will find in tomorrow's CI 😅

Thank you a lot, especially adding the test

@yonigozlan yonigozlan enabled auto-merge (squash) July 12, 2025 23:26
@yonigozlan yonigozlan merged commit a1ad919 into huggingface:main Jul 12, 2025
25 checks passed
rjgleaton pushed a commit to rjgleaton/transformers that referenced this pull request Jul 17, 2025
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.

3 participants