2024-02-06 03:56:32 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from contextlib import contextmanager
|
2024-02-18 06:27:42 +00:00
|
|
|
from typing import Callable, List, Union
|
2024-02-06 03:56:32 +00:00
|
|
|
|
|
|
|
import torch.nn as nn
|
2024-02-18 06:27:42 +00:00
|
|
|
from diffusers.models.autoencoders.autoencoder_kl import AutoencoderKL
|
2024-03-12 16:00:24 +00:00
|
|
|
from diffusers.models.autoencoders.autoencoder_tiny import AutoencoderTiny
|
2024-02-18 06:27:42 +00:00
|
|
|
from diffusers.models.unets.unet_2d_condition import UNet2DConditionModel
|
2024-02-06 03:56:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _conv_forward_asymmetric(self, input, weight, bias):
|
|
|
|
"""
|
|
|
|
Patch for Conv2d._conv_forward that supports asymmetric padding
|
|
|
|
"""
|
|
|
|
working = nn.functional.pad(input, self.asymmetric_padding["x"], mode=self.asymmetric_padding_mode["x"])
|
|
|
|
working = nn.functional.pad(working, self.asymmetric_padding["y"], mode=self.asymmetric_padding_mode["y"])
|
|
|
|
return nn.functional.conv2d(
|
|
|
|
working,
|
|
|
|
weight,
|
|
|
|
bias,
|
|
|
|
self.stride,
|
|
|
|
nn.modules.utils._pair(0),
|
|
|
|
self.dilation,
|
|
|
|
self.groups,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@contextmanager
|
2024-03-12 16:00:24 +00:00
|
|
|
def set_seamless(model: Union[UNet2DConditionModel, AutoencoderKL, AutoencoderTiny], seamless_axes: List[str]):
|
2024-02-18 06:27:42 +00:00
|
|
|
# Callable: (input: Tensor, weight: Tensor, bias: Optional[Tensor]) -> Tensor
|
|
|
|
to_restore: list[tuple[nn.Conv2d | nn.ConvTranspose2d, Callable]] = []
|
2024-02-06 03:56:32 +00:00
|
|
|
try:
|
2024-02-18 06:27:42 +00:00
|
|
|
# Hard coded to skip down block layers, allowing for seamless tiling at the expense of prompt adherence
|
|
|
|
skipped_layers = 1
|
2024-02-06 03:56:32 +00:00
|
|
|
for m_name, m in model.named_modules():
|
2024-02-18 06:27:42 +00:00
|
|
|
if not isinstance(m, (nn.Conv2d, nn.ConvTranspose2d)):
|
|
|
|
continue
|
2024-02-06 03:56:32 +00:00
|
|
|
|
2024-02-18 06:27:42 +00:00
|
|
|
if isinstance(model, UNet2DConditionModel) and m_name.startswith("down_blocks.") and ".resnets." in m_name:
|
|
|
|
# down_blocks.1.resnets.1.conv1
|
|
|
|
_, block_num, _, resnet_num, submodule_name = m_name.split(".")
|
|
|
|
block_num = int(block_num)
|
|
|
|
resnet_num = int(resnet_num)
|
2024-02-06 03:56:32 +00:00
|
|
|
|
2024-02-18 06:27:42 +00:00
|
|
|
if block_num >= len(model.down_blocks) - skipped_layers:
|
2024-02-06 03:56:32 +00:00
|
|
|
continue
|
|
|
|
|
2024-02-18 06:27:42 +00:00
|
|
|
# Skip the second resnet (could be configurable)
|
|
|
|
if resnet_num > 0:
|
2024-02-06 03:56:32 +00:00
|
|
|
continue
|
|
|
|
|
2024-02-18 06:27:42 +00:00
|
|
|
# Skip Conv2d layers (could be configurable)
|
|
|
|
if submodule_name == "conv2":
|
2024-02-06 03:56:32 +00:00
|
|
|
continue
|
|
|
|
|
2024-02-18 06:27:42 +00:00
|
|
|
m.asymmetric_padding_mode = {}
|
|
|
|
m.asymmetric_padding = {}
|
|
|
|
m.asymmetric_padding_mode["x"] = "circular" if ("x" in seamless_axes) else "constant"
|
|
|
|
m.asymmetric_padding["x"] = (
|
|
|
|
m._reversed_padding_repeated_twice[0],
|
|
|
|
m._reversed_padding_repeated_twice[1],
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
)
|
|
|
|
m.asymmetric_padding_mode["y"] = "circular" if ("y" in seamless_axes) else "constant"
|
|
|
|
m.asymmetric_padding["y"] = (
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
m._reversed_padding_repeated_twice[2],
|
|
|
|
m._reversed_padding_repeated_twice[3],
|
|
|
|
)
|
|
|
|
|
|
|
|
to_restore.append((m, m._conv_forward))
|
|
|
|
m._conv_forward = _conv_forward_asymmetric.__get__(m, nn.Conv2d)
|
2024-02-06 03:56:32 +00:00
|
|
|
|
|
|
|
yield
|
|
|
|
|
|
|
|
finally:
|
|
|
|
for module, orig_conv_forward in to_restore:
|
|
|
|
module._conv_forward = orig_conv_forward
|
|
|
|
if hasattr(module, "asymmetric_padding_mode"):
|
|
|
|
del module.asymmetric_padding_mode
|
|
|
|
if hasattr(module, "asymmetric_padding"):
|
|
|
|
del module.asymmetric_padding
|