Skip to content
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

set config from original module but set compiled module on class #3650

Merged

Conversation

williamberman
Copy link
Contributor

@williamberman williamberman commented Jun 2, 2023

If you passed a compiled module to the pipeline constructor, this would silently strip the compilation and set the uncompiled module on the pipeline

Before:

from diffusers import UNet2DConditionModel
import torch
from diffusers import DiffusionPipeline

unet = UNet2DConditionModel.from_pretrained("runwayml/stable-diffusion-v1-5", subfolder="unet")

print(unet.__class__)
# diffusers.models.unet_2d_condition.UNet2DConditionModel

unet = torch.compile(unet)

print(unet.__class__)
# torch._dynamo.eval_frame.OptimizedModule

pipe = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", unet=unet)

print(pipe.unet.__class__)
# diffusers.models.unet_2d_condition.UNet2DConditionModel

Now:

>>> from diffusers import UNet2DConditionModel
>>> import torch
>>> from diffusers import DiffusionPipeline
>>>
>>> unet = UNet2DConditionModel.from_pretrained("runwayml/stable-diffusion-v1-5", subfolder="unet")
>>>
>>> print(unet.__class__)
<class 'diffusers.models.unet_2d_condition.UNet2DConditionModel'>
>>> # diffusers.models.unet_2d_condition.UNet2DConditionModel
>>>
>>> unet = torch.compile(unet)
>>>
>>> print(unet.__class__)
<class 'torch._dynamo.eval_frame.OptimizedModule'>
>>> # torch._dynamo.eval_frame.OptimizedModule
>>>
>>> pipe = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", unet=unet)
`text_config_dict` is provided which will be used to initialize `CLIPTextConfig`. The value `text_config["id2label"]` will be overriden.
>>>
>>> print(pipe.unet.__class__)
<class 'torch._dynamo.eval_frame.OptimizedModule'>

@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Jun 2, 2023

The documentation is not available anymore as the PR was closed or merged.

Copy link
Contributor

@patrickvonplaten patrickvonplaten left a comment

Choose a reason for hiding this comment

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

Nice fix! Can you add one test here? No need to run a forward pass in the dummy model, think we can just reproduce what was done in the readme

Copy link
Member

@pcuenca pcuenca left a comment

Choose a reason for hiding this comment

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

Cool solution!

@@ -485,17 +485,19 @@ def register_modules(self, **kwargs):
if module is None:
register_dict = {name: (None, None)}
else:
# register the original module, not the dynamo compiled one
# register the config from the original module, not the dynamo compiled one
Copy link
Member

Choose a reason for hiding this comment

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

❤️

Comment on lines +103 to +107

# previous diffusers versions stripped compilation off
# compiled modules
assert is_compiled_module(ddpm.unet)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@patrickvonplaten just added to existing pipeline dynamo test. Confirmed fails on main and passes on branch

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good to me!

@patrickvonplaten
Copy link
Contributor

Feel free to merge :-)

@williamberman williamberman merged commit b7af946 into huggingface:main Jun 2, 2023
7 checks passed
@williamberman williamberman deleted the set_attr_torch_compile branch June 2, 2023 17:26
yoonseokjin pushed a commit to yoonseokjin/diffusers that referenced this pull request Dec 25, 2023
…gingface#3650)

* set config from original module but set compiled module on class

* add test
AmericanPresidentJimmyCarter pushed a commit to AmericanPresidentJimmyCarter/diffusers that referenced this pull request Apr 26, 2024
…gingface#3650)

* set config from original module but set compiled module on class

* add test
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.

None yet

4 participants