Last active
August 14, 2024 21:30
-
-
Save sayakpaul/508d89d7aad4f454900813da5d42ca97 to your computer and use it in GitHub Desktop.
The script shows how to run SD3 with `torch.compile()`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import torch | |
| torch.set_float32_matmul_precision("high") | |
| from diffusers import StableDiffusion3Pipeline | |
| import time | |
| id = "stabilityai/stable-diffusion-3-medium-diffusers" | |
| pipeline = StableDiffusion3Pipeline.from_pretrained( | |
| id, | |
| torch_dtype=torch.float16 | |
| ).to("cuda") | |
| pipeline.set_progress_bar_config(disable=True) | |
| torch._inductor.config.conv_1x1_as_mm = True | |
| torch._inductor.config.coordinate_descent_tuning = True | |
| torch._inductor.config.epilogue_fusion = False | |
| torch._inductor.config.coordinate_descent_check_all_directions = True | |
| pipeline.transformer.to(memory_format=torch.channels_last) | |
| pipeline.vae.to(memory_format=torch.channels_last) | |
| pipeline.transformer = torch.compile(pipeline.transformer, mode="max-autotune", fullgraph=True) | |
| pipeline.vae.decode = torch.compile(pipeline.vae.decode, mode="max-autotune", fullgraph=True) | |
| prompt = "a photo of a cat" | |
| for _ in range(3): | |
| _ = pipeline( | |
| prompt=prompt, | |
| num_inference_steps=50, | |
| guidance_scale=5.0, | |
| generator=torch.manual_seed(1), | |
| ) | |
| start = time.time() | |
| for _ in range(10): | |
| _ = pipeline( | |
| prompt=prompt, | |
| num_inference_steps=50, | |
| guidance_scale=5.0, | |
| generator=torch.manual_seed(1), | |
| ) | |
| end = time.time() | |
| avg_inference_time = (end - start) / 10 | |
| print(f"Average inference time: {avg_inference_time:.3f} seconds.") | |
| image = pipeline( | |
| prompt=prompt, | |
| num_inference_steps=50, | |
| guidance_scale=5.0, | |
| generator=torch.manual_seed(1), | |
| ).images[0] | |
| filename = "_".join(prompt.split(" ")) | |
| image.save(f"diffusers_{filename}.png") |
hello,I run this program in docker(torch=2.1) and meet a problem that:
Traceback (most recent call last):
File "/workspace/std3/a.py", line 18, in
torch._inductor.config.coordinate_descent_check_all_directions = True
File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/config_utils.py", line 71, in setattr
raise AttributeError(f"{self.name}.{name} does not exist")
AttributeError: torch._inductor.config.coordinate_descent_check_all_directions does not exist
Author
You should use PyTorch 2.3
You should use PyTorch 2.3
I solved this problem with pytorch2.3,thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I solved the issue by installing peft:
pip install peftI'm not sure what the main problem was exactly, but the error was caused here:
That logger caused the error when
torch.compilewas applied to the transformers.It makes 19.8% faster in 1024x1024 resolution
baseline : 12.2532 sec
compile : 9.82578 sec