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

Add arg compile_unet #17

Merged
merged 3 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/diffusers/models/unet_blocks_oneflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,8 @@ def set_attention_slice(self, slice_size):

def forward(self, hidden_states, temb=None, encoder_hidden_states=None):
hidden_states = self.resnets[0](hidden_states, temb)
for attn, resnet in zip(self.attentions, self.resnets[1:]):
resnets_list = [m for m in self.resnets]
for attn, resnet in zip(self.attentions, resnets_list[1:]):
hidden_states = attn(hidden_states, encoder_hidden_states)
hidden_states = resnet(hidden_states, temb)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ def __call__(
latents: Optional[torch.FloatTensor] = None,
output_type: Optional[str] = "pil",
return_dict: bool = True,
compile_unet: bool = True,
**kwargs,
):
r"""
Expand Down Expand Up @@ -179,6 +180,8 @@ def __call__(
return_dict (`bool`, *optional*, defaults to `True`):
Whether or not to return a [`~pipelines.stable_diffusion.StableDiffusionPipelineOutput`] instead of a
plain tuple.
compile_unet (`bool`, *optional*, defaults to `True`):
Whether or not to compile unet as nn.graph

Returns:
[`~pipelines.stable_diffusion.StableDiffusionPipelineOutput`] or `tuple`:
Expand Down Expand Up @@ -278,15 +281,16 @@ def __call__(

compilation_start = timer()
compilation_time = 0
if self.unet_compiled == False:
print("[oneflow]", "compiling unet beforehand to make sure the progress bar is more accurate")
i, t = list(enumerate(self.scheduler.timesteps))[0]
latent_model_input = torch.cat([latents] * 2) if do_classifier_free_guidance else latents
self.unet_graph._compile(latent_model_input, t, text_embeddings)
self.unet_compiled = True
self.unet_graph(latent_model_input, t, text_embeddings) # warmup
compilation_time = timer() - compilation_start
print("[oneflow]", "[elapsed(s)]", "[unet compilation]", compilation_time)
if compile_unet:
if self.unet_compiled == False:
print("[oneflow]", "compiling unet beforehand to make sure the progress bar is more accurate")
i, t = list(enumerate(self.scheduler.timesteps))[0]
latent_model_input = torch.cat([latents] * 2) if do_classifier_free_guidance else latents
self.unet_graph._compile(latent_model_input, t, text_embeddings)
self.unet_compiled = True
self.unet_graph(latent_model_input, t, text_embeddings) # warmup
compilation_time = timer() - compilation_start
print("[oneflow]", "[elapsed(s)]", "[unet compilation]", compilation_time)

for i, t in enumerate(self.progress_bar(self.scheduler.timesteps)):
torch._oneflow_internal.profiler.RangePush(f"denoise-{i}")
Expand All @@ -298,9 +302,12 @@ def __call__(
latent_model_input = latent_model_input / ((sigma**2 + 1) ** 0.5)

# predict the noise residual
torch._oneflow_internal.profiler.RangePush(f"denoise-{i}-unet-graph")
noise_pred = self.unet_graph(latent_model_input, t, text_embeddings)
torch._oneflow_internal.profiler.RangePop()
if compile_unet:
torch._oneflow_internal.profiler.RangePush(f"denoise-{i}-unet-graph")
noise_pred = self.unet_graph(latent_model_input, t, text_embeddings)
torch._oneflow_internal.profiler.RangePop()
else:
noise_pred = self.unet(latent_model_input, t, encoder_hidden_states=text_embeddings).sample

# perform guidance
if do_classifier_free_guidance:
Expand Down