| 2265 | |
| 2266 | |
| 2267 | class AttnUpBlock2D(nn.Module): |
| 2268 | def __init__( |
| 2269 | self, |
| 2270 | in_channels: int, |
| 2271 | prev_output_channel: int, |
| 2272 | out_channels: int, |
| 2273 | temb_channels: int, |
| 2274 | resolution_idx: int = None, |
| 2275 | dropout: float = 0.0, |
| 2276 | num_layers: int = 1, |
| 2277 | resnet_eps: float = 1e-6, |
| 2278 | resnet_time_scale_shift: str = "default", |
| 2279 | resnet_act_fn: str = "swish", |
| 2280 | resnet_groups: int = 32, |
| 2281 | resnet_pre_norm: bool = True, |
| 2282 | attention_head_dim: int = 1, |
| 2283 | output_scale_factor: float = 1.0, |
| 2284 | upsample_type: str = "conv", |
| 2285 | ): |
| 2286 | super().__init__() |
| 2287 | resnets = [] |
| 2288 | attentions = [] |
| 2289 | |
| 2290 | self.upsample_type = upsample_type |
| 2291 | |
| 2292 | if attention_head_dim is None: |
| 2293 | logger.warning( |
| 2294 | f"It is not recommend to pass `attention_head_dim=None`. Defaulting `attention_head_dim` to `in_channels`: {out_channels}." |
| 2295 | ) |
| 2296 | attention_head_dim = out_channels |
| 2297 | |
| 2298 | for i in range(num_layers): |
| 2299 | res_skip_channels = in_channels if (i == num_layers - 1) else out_channels |
| 2300 | resnet_in_channels = prev_output_channel if i == 0 else out_channels |
| 2301 | |
| 2302 | resnets.append( |
| 2303 | ResnetBlock2D( |
| 2304 | in_channels=resnet_in_channels + res_skip_channels, |
| 2305 | out_channels=out_channels, |
| 2306 | temb_channels=temb_channels, |
| 2307 | eps=resnet_eps, |
| 2308 | groups=resnet_groups, |
| 2309 | dropout=dropout, |
| 2310 | time_embedding_norm=resnet_time_scale_shift, |
| 2311 | non_linearity=resnet_act_fn, |
| 2312 | output_scale_factor=output_scale_factor, |
| 2313 | pre_norm=resnet_pre_norm, |
| 2314 | ) |
| 2315 | ) |
| 2316 | attentions.append( |
| 2317 | Attention( |
| 2318 | out_channels, |
| 2319 | heads=out_channels // attention_head_dim, |
| 2320 | dim_head=attention_head_dim, |
| 2321 | rescale_output_factor=output_scale_factor, |
| 2322 | eps=resnet_eps, |
| 2323 | norm_num_groups=resnet_groups, |
| 2324 | residual_connection=True, |