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

Make LoRACompatibleConv padding_mode work. #6031

Merged
merged 9 commits into from
Feb 27, 2024
19 changes: 13 additions & 6 deletions src/diffusers/models/lora.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,13 +355,20 @@ def forward(self, hidden_states: torch.Tensor, scale: float = 1.0) -> torch.Tens
if self.lora_layer is None:
# make sure to the functional Conv2D function as otherwise torch.compile's graph will break
# see: https://github.com/huggingface/diffusers/pull/4315
return F.conv2d(
hidden_states, self.weight, self.bias, self.stride, self.padding, self.dilation, self.groups
)
if self.padding_mode != 'zeros':
return F.conv2d(F.pad(hidden_states, self._reversed_padding_repeated_twice, mode=self.padding_mode),
self.weight, self.bias, self.stride,
(0, 0), self.dilation, self.groups)
return F.conv2d(hidden_states, self.weight, self.bias, self.stride,
self.padding, self.dilation, self.groups)
Copy link
Member

Choose a reason for hiding this comment

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

Can we compute original_outputs one time and the reuse it? That way, I think the code will remain cleaner.

Copy link
Contributor Author

@jinghuan-Chen jinghuan-Chen Dec 4, 2023

Choose a reason for hiding this comment

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

Thanks for your guidance.
There is currently no better, cleaner code solution. If you have, please give me some guidance.

testing script

import torch
from diffusers import DiffusionPipeline


# Modify the padding mode of Conv2d
def set_pad_mode(network, mode="circular"):
    for _, module in network.named_children():
        if len(module._modules) > 0:
            set_pad_mode(module, mode)
        else:
            if isinstance(module, torch.nn.Conv2d):
                module.padding_mode = mode


base = DiffusionPipeline.from_pretrained(
    "stabilityai/stable-diffusion-xl-base-1.0",
    torch_dtype=torch.float16,
    variant="fp16",
    use_safetensors=True,
)
base.to("cuda")
n_steps = 30
prompt = "interior design, Equirectangular, Panoramic, Panorama and 360"

set_pad_mode(base.vae, "circular")
set_pad_mode(base.unet, "circular")


image = base(
    prompt=prompt,
    height=1024,
    width=2048,
    num_inference_steps=n_steps,
    output_type="pil",
).images[0]

Copy link
Member

Choose a reason for hiding this comment

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

I don't this comment was addressed:

#6031 (comment)

Also, by testing, I meant adding a test to our testing suite. In this case, you add one here and here.

Does that make sense?

else:
original_outputs = F.conv2d(
hidden_states, self.weight, self.bias, self.stride, self.padding, self.dilation, self.groups
)
if self.padding_mode != 'zeros':
original_outputs = F.conv2d(F.pad(hidden_states, self._reversed_padding_repeated_twice, mode=self.padding_mode),
self.weight, self.bias, self.stride,
(0, 0), self.dilation, self.groups)
else:
original_outputs = F.conv2d(hidden_states, self.weight, self.bias, self.stride,
self.padding, self.dilation, self.groups)
return original_outputs + (scale * self.lora_layer(hidden_states))


Expand Down
Loading