|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from contextlib import contextmanager |
| 4 | +from typing import Callable, Dict, List, Optional, Tuple |
| 5 | + |
| 6 | +import torch |
| 7 | +import torch.nn as nn |
| 8 | +from diffusers import UNet2DConditionModel |
| 9 | +from diffusers.models.lora import LoRACompatibleConv |
| 10 | + |
| 11 | +from invokeai.backend.stable_diffusion.extensions.base import ExtensionBase |
| 12 | + |
| 13 | + |
| 14 | +class SeamlessExt(ExtensionBase): |
| 15 | + def __init__( |
| 16 | + self, |
| 17 | + seamless_axes: List[str], |
| 18 | + ): |
| 19 | + super().__init__() |
| 20 | + self._seamless_axes = seamless_axes |
| 21 | + |
| 22 | + @contextmanager |
| 23 | + def patch_unet(self, unet: UNet2DConditionModel, cached_weights: Optional[Dict[str, torch.Tensor]] = None): |
| 24 | + with self.static_patch_model( |
| 25 | + model=unet, |
| 26 | + seamless_axes=self._seamless_axes, |
| 27 | + ): |
| 28 | + yield |
| 29 | + |
| 30 | + @staticmethod |
| 31 | + @contextmanager |
| 32 | + def static_patch_model( |
| 33 | + model: torch.nn.Module, |
| 34 | + seamless_axes: List[str], |
| 35 | + ): |
| 36 | + if not seamless_axes: |
| 37 | + yield |
| 38 | + return |
| 39 | + |
| 40 | + # override conv_forward |
| 41 | + # https://github.com/huggingface/diffusers/issues/556#issuecomment-1993287019 |
| 42 | + def _conv_forward_asymmetric( |
| 43 | + self, input: torch.Tensor, weight: torch.Tensor, bias: Optional[torch.Tensor] = None |
| 44 | + ): |
| 45 | + self.paddingX = (self._reversed_padding_repeated_twice[0], self._reversed_padding_repeated_twice[1], 0, 0) |
| 46 | + self.paddingY = (0, 0, self._reversed_padding_repeated_twice[2], self._reversed_padding_repeated_twice[3]) |
| 47 | + working = torch.nn.functional.pad(input, self.paddingX, mode=x_mode) |
| 48 | + working = torch.nn.functional.pad(working, self.paddingY, mode=y_mode) |
| 49 | + return torch.nn.functional.conv2d( |
| 50 | + working, weight, bias, self.stride, torch.nn.modules.utils._pair(0), self.dilation, self.groups |
| 51 | + ) |
| 52 | + |
| 53 | + original_layers: List[Tuple[nn.Conv2d, Callable]] = [] |
| 54 | + |
| 55 | + try: |
| 56 | + x_mode = "circular" if "x" in seamless_axes else "constant" |
| 57 | + y_mode = "circular" if "y" in seamless_axes else "constant" |
| 58 | + |
| 59 | + conv_layers: List[torch.nn.Conv2d] = [] |
| 60 | + |
| 61 | + for module in model.modules(): |
| 62 | + if isinstance(module, torch.nn.Conv2d): |
| 63 | + conv_layers.append(module) |
| 64 | + |
| 65 | + for layer in conv_layers: |
| 66 | + if isinstance(layer, LoRACompatibleConv) and layer.lora_layer is None: |
| 67 | + layer.lora_layer = lambda *x: 0 |
| 68 | + original_layers.append((layer, layer._conv_forward)) |
| 69 | + layer._conv_forward = _conv_forward_asymmetric.__get__(layer, torch.nn.Conv2d) |
| 70 | + |
| 71 | + yield |
| 72 | + |
| 73 | + finally: |
| 74 | + for layer, orig_conv_forward in original_layers: |
| 75 | + layer._conv_forward = orig_conv_forward |
0 commit comments