| 2986 | |
| 2987 | |
| 2988 | class SkipUpBlock2D(nn.Module): |
| 2989 | def __init__( |
| 2990 | self, |
| 2991 | in_channels: int, |
| 2992 | prev_output_channel: int, |
| 2993 | out_channels: int, |
| 2994 | temb_channels: int, |
| 2995 | resolution_idx: Optional[int] = None, |
| 2996 | dropout: float = 0.0, |
| 2997 | num_layers: int = 1, |
| 2998 | resnet_eps: float = 1e-6, |
| 2999 | resnet_time_scale_shift: str = "default", |
| 3000 | resnet_act_fn: str = "swish", |
| 3001 | resnet_pre_norm: bool = True, |
| 3002 | output_scale_factor: float = np.sqrt(2.0), |
| 3003 | add_upsample: bool = True, |
| 3004 | upsample_padding: int = 1, |
| 3005 | ): |
| 3006 | super().__init__() |
| 3007 | self.resnets = nn.ModuleList([]) |
| 3008 | |
| 3009 | for i in range(num_layers): |
| 3010 | res_skip_channels = in_channels if (i == num_layers - 1) else out_channels |
| 3011 | resnet_in_channels = prev_output_channel if i == 0 else out_channels |
| 3012 | |
| 3013 | self.resnets.append( |
| 3014 | ResnetBlock2D( |
| 3015 | in_channels=resnet_in_channels + res_skip_channels, |
| 3016 | out_channels=out_channels, |
| 3017 | temb_channels=temb_channels, |
| 3018 | eps=resnet_eps, |
| 3019 | groups=min((resnet_in_channels + res_skip_channels) // 4, 32), |
| 3020 | groups_out=min(out_channels // 4, 32), |
| 3021 | dropout=dropout, |
| 3022 | time_embedding_norm=resnet_time_scale_shift, |
| 3023 | non_linearity=resnet_act_fn, |
| 3024 | output_scale_factor=output_scale_factor, |
| 3025 | pre_norm=resnet_pre_norm, |
| 3026 | ) |
| 3027 | ) |
| 3028 | |
| 3029 | self.upsampler = FirUpsample2D(in_channels, out_channels=out_channels) |
| 3030 | if add_upsample: |
| 3031 | self.resnet_up = ResnetBlock2D( |
| 3032 | in_channels=out_channels, |
| 3033 | out_channels=out_channels, |
| 3034 | temb_channels=temb_channels, |
| 3035 | eps=resnet_eps, |
| 3036 | groups=min(out_channels // 4, 32), |
| 3037 | groups_out=min(out_channels // 4, 32), |
| 3038 | dropout=dropout, |
| 3039 | time_embedding_norm=resnet_time_scale_shift, |
| 3040 | non_linearity=resnet_act_fn, |
| 3041 | output_scale_factor=output_scale_factor, |
| 3042 | pre_norm=resnet_pre_norm, |
| 3043 | use_in_shortcut=True, |
| 3044 | up=True, |
| 3045 | kernel="fir", |