| 2051 | |
| 2052 | |
| 2053 | class KDownBlock2D(nn.Module): |
| 2054 | def __init__( |
| 2055 | self, |
| 2056 | in_channels: int, |
| 2057 | out_channels: int, |
| 2058 | temb_channels: int, |
| 2059 | dropout: float = 0.0, |
| 2060 | num_layers: int = 4, |
| 2061 | resnet_eps: float = 1e-5, |
| 2062 | resnet_act_fn: str = "gelu", |
| 2063 | resnet_group_size: int = 32, |
| 2064 | add_downsample: bool = False, |
| 2065 | ): |
| 2066 | super().__init__() |
| 2067 | resnets = [] |
| 2068 | |
| 2069 | for i in range(num_layers): |
| 2070 | in_channels = in_channels if i == 0 else out_channels |
| 2071 | groups = in_channels // resnet_group_size |
| 2072 | groups_out = out_channels // resnet_group_size |
| 2073 | |
| 2074 | resnets.append( |
| 2075 | ResnetBlockCondNorm2D( |
| 2076 | in_channels=in_channels, |
| 2077 | out_channels=out_channels, |
| 2078 | dropout=dropout, |
| 2079 | temb_channels=temb_channels, |
| 2080 | groups=groups, |
| 2081 | groups_out=groups_out, |
| 2082 | eps=resnet_eps, |
| 2083 | non_linearity=resnet_act_fn, |
| 2084 | time_embedding_norm="ada_group", |
| 2085 | conv_shortcut_bias=False, |
| 2086 | ) |
| 2087 | ) |
| 2088 | |
| 2089 | self.resnets = nn.ModuleList(resnets) |
| 2090 | |
| 2091 | if add_downsample: |
| 2092 | # YiYi's comments- might be able to use FirDownsample2D, look into details later |
| 2093 | self.downsamplers = nn.ModuleList([KDownsample2D()]) |
| 2094 | else: |
| 2095 | self.downsamplers = None |
| 2096 | |
| 2097 | self.gradient_checkpointing = False |
| 2098 | |
| 2099 | def forward( |
| 2100 | self, hidden_states: torch.Tensor, temb: Optional[torch.Tensor] = None, *args, **kwargs |
| 2101 | ) -> Tuple[torch.Tensor, Tuple[torch.Tensor, ...]]: |
| 2102 | if len(args) > 0 or kwargs.get("scale", None) is not None: |
| 2103 | deprecation_message = "The `scale` argument is deprecated and will be ignored. Please remove it, as passing it will raise an error in the future. `scale` should directly be passed while calling the underlying pipeline component i.e., via `cross_attention_kwargs`." |
| 2104 | deprecate("scale", "1.0.0", deprecation_message) |
| 2105 | |
| 2106 | output_states = () |
| 2107 | |
| 2108 | for resnet in self.resnets: |
| 2109 | if self.training and self.gradient_checkpointing: |
| 2110 | |