-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Fix PyTorch 2.3.1 compatibility: add version guard for torch.library.... #12206
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
Fix PyTorch 2.3.1 compatibility: add version guard for torch.library.... #12206
Conversation
...custom_op - Add hasattr() check for torch.library.custom_op and register_fake - These functions were added in PyTorch 2.4, causing import failures in 2.3.1 - Both decorators and functions are now properly guarded with version checks - Maintains backward compatibility while preserving functionality Fixes huggingface#12195
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, thanks for the fix! I think we can improve a bit here by using the approach followed in #11941. Specifically, the following lines:
diffusers/src/diffusers/models/attention_dispatch.py
Lines 121 to 139 in 0018b62
- Replace hasattr check with version string comparison - Add no-op decorator functions for PyTorch < 2.4.0 - Follows pattern from huggingface#11941 as suggested by reviewer - Maintains cleaner code structure without indentation changes
Hi @a-r-r-o-w
Thanks for the feedback! I've updated the PR to use the dummy decorator approach as suggested, following the pattern from #11941. The code is much cleaner now without the indentation changes.
Tested with PyTorch 2.3.1 and the import works correctly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the updates! Just one more comment
Update all the decorator usages Co-authored-by: Aryan <contact.aryanvs@gmail.com>
Co-authored-by: Aryan <contact.aryanvs@gmail.com>
Co-authored-by: Aryan <contact.aryanvs@gmail.com>
Co-authored-by: Aryan <contact.aryanvs@gmail.com>
Hi @a-r-r-o-w I've addressed all your feedback - moved the version check to the top of the file and used private naming with underscores.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for looking into it @Aishwarya0811!
@bot /style
Style bot fixed some files and pushed the changes.
HuggingFaceDocBuilderDev
commented
Aug 23, 2025
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.
Uh oh!
There was an error while loading. Please reload this page.
What does this PR do?
Fixes #12195 by adding version guards for
torch.library.custom_op
andtorch.library.register_fake
which are not available in PyTorch 2.3.1.Problem
torch.library.custom_op
andtorch.library.register_fake
were introduced in PyTorch 2.4AttributeError: module 'torch.library' has no attribute 'custom_op'
from diffusers import AutoencoderKL
and other importsSolution
hasattr()
checks before using these torch.library functionsTesting
Fixes #12195
Who can review?
@sayakpaul @yiyixuxu - This is a PyTorch compatibility fix for core library functionality.