Uh oh!
There was an error while loading. Please reload this page.
Fix unwrap hooks when the model is wrapped - #10730
Conversation
HuggingFaceDocBuilderDev
commented
Feb 5, 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. |
@SunMarc@sayakpaul@yiyixuxu this seems to be a possibility now with the fix: |
SunMarc
commented
Feb 6, 2025
Could you share a reproducer @eppaneamd ? |
@SunMarc it seems that the issue becomes present when one attempts to do repeated inference (e.g. for benchmarking purposes). You can reproduce this by modifying the flux repro as follows: |
| for _, model in self.components.items(): | ||
| if isinstance(model, torch.nn.Module) and hasattr(model, "_hf_hook"): | ||
| accelerate.hooks.remove_hook_from_module(model, recurse=True) | ||
| accelerate.hooks.remove_hook_from_module(_unwrap_model(model), recurse=True) |
There was a problem hiding this comment.
Do we wanna add a test case to see where this is helpful?
There was a problem hiding this comment.
Yeah I'll add a test case if this solves the issue
sayakpaul
commented
Feb 6, 2025
What is the use case here? Do fromdiffusersimportDiffusionPipelineimporttorchpipeline=DiffusionPipeline.from_pretrained(
"black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16,
)
pipeline.enable_model_cpu_offload()
pipeline.transformer.compile()
image=pipeline(
prompt="a cat sitting by the sea waiting for its companion to come", guidance_scale=3.5, num_inference_steps=28, max_sequence_length=512,
generator=torch.manual_seed(0)
).images[0] |
eppaneamd
commented
Feb 6, 2025
@sayakpaul thank you for that example, the use case is to apply VAE tiling & Model cpu offload & compile together. That does seem to work also when calling pipe repeatedly, at least for Flux. So we should let diffusers handle the model compilation after all. 🙏 Are you able to reproduce this without issues for HunyuanVideo as well? When running: I am facing issues like: Output video looks allright though. 👍 |
sayakpaul
commented
Feb 7, 2025
There are recompilations it seems which could be because of a number of reasons. I would suggest a different issue thread for this as your original issue seems to have been solved by the code snippet I had posted? |
@sayakpaul sure thing, I can do that! But perhaps still a follow-up question related to the original issue: there are guides/tutorials where the model compilation is done using As per the hf_hook error, they don't seem to be equivalent, at least currently. |
sayakpaul
commented
Feb 7, 2025
So
Sorry, I still don't get your response. My snippet does achieve what you originally intended in #10729, no? |
yiyixuxu
commented
Mar 4, 2025
@SunMarc@sayakpaul |
sayakpaul
commented
Mar 4, 2025
@yiyixuxu thanks for the ping. I think https://github.com/huggingface/diffusers/pull/10730/files#r1944720217 is pending. Over to @SunMarc |
SunMarc
commented
Mar 13, 2025
I'm getting the same issue as @eppaneamd so this fix is not complete. I'll leave it as a draft until someone or I find the solution. [rank0]: File"/var/lib/jenkins/pytorch/torch/autograd/grad_mode.py", line185, in__init__
[rank0]: self.prev=torch.is_grad_enabled()
[rank0]: RecursionError: maximumrecursiondepthexceededwhilecallingaPythonobject |
sayakpaul
commented
Apr 10, 2025
@SunMarc a gentle ping |
sayakpaul
commented
Dec 2, 2025
Any reason why this is closed? |
SunMarc
commented
Dec 2, 2025
In the issue thread, it seems that it was fixed on torch.compile side ! |

What does this PR do ?
Fixes#10729.
This PR make sure that we are removing the hooks from the unwrapped model. Otherwise, we will get an error when trying to remove them.