-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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 unwrap hooks when the model is wrapped #10730
base: main
Are you sure you want to change the base?
Conversation
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. |
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!
@SunMarc @sayakpaul @yiyixuxu this seems to be a possibility now with the fix:
|
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:
|
@@ -1042,7 +1042,7 @@ def remove_all_hooks(self): | |||
""" | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we wanna add a test case to see where this is helpful?
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.
Yeah I'll add a test case if this solves the issue
What is the use case here? Do from diffusers import DiffusionPipeline
import torch
pipeline = 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] |
@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. 👍 |
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. |
So
Sorry, I still don't get your response. My snippet does achieve what you originally intended in #10729, no? |
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.