Uh oh!
There was an error while loading. Please reload this page.
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 Fixeshuggingface#12195
a-r-r-o-w
left a comment
There was a problem hiding this comment.
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
Aishwarya0811
commented
Aug 21, 2025
Hi @a-r-r-o-w Tested with PyTorch 2.3.1 and the import works correctly. |
a-r-r-o-w
left a comment
There was a problem hiding this comment.
Thanks for the updates! Just one more comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
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>
Aishwarya0811
commented
Aug 22, 2025
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. |
a-r-r-o-w
left a comment
There was a problem hiding this comment.
Thanks for looking into it @Aishwarya0811!
a-r-r-o-w
commented
Aug 23, 2025
@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.
#12206) * Fix PyTorch 2.3.1 compatibility: add version guard for torch.library.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#12195 * Use dummy decorators approach for PyTorch version compatibility - Replace hasattr check with version string comparison - Add no-op decorator functions for PyTorch < 2.4.0 - Follows pattern from #11941 as suggested by reviewer - Maintains cleaner code structure without indentation changes * Update src/diffusers/models/attention_dispatch.py Update all the decorator usages Co-authored-by: Aryan <contact.aryanvs@gmail.com> * Update src/diffusers/models/attention_dispatch.py Co-authored-by: Aryan <contact.aryanvs@gmail.com> * Update src/diffusers/models/attention_dispatch.py Co-authored-by: Aryan <contact.aryanvs@gmail.com> * Update src/diffusers/models/attention_dispatch.py Co-authored-by: Aryan <contact.aryanvs@gmail.com> * Move version check to top of file and use private naming as requested * Apply style fixes --------- Co-authored-by: Aryan <contact.aryanvs@gmail.com> Co-authored-by: Aryan <aryan@huggingface.co> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
What does this PR do?
Fixes#12195 by adding version guards for
torch.library.custom_opandtorch.library.register_fakewhich are not available in PyTorch 2.3.1.Problem
torch.library.custom_opandtorch.library.register_fakewere introduced in PyTorch 2.4AttributeError: module 'torch.library' has no attribute 'custom_op'from diffusers import AutoencoderKLand 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.