| 2852 | |
| 2853 | |
| 2854 | class AttnSkipUpBlock2D(nn.Module): |
| 2855 | def __init__( |
| 2856 | self, |
| 2857 | in_channels: int, |
| 2858 | prev_output_channel: int, |
| 2859 | out_channels: int, |
| 2860 | temb_channels: int, |
| 2861 | resolution_idx: Optional[int] = None, |
| 2862 | dropout: float = 0.0, |
| 2863 | num_layers: int = 1, |
| 2864 | resnet_eps: float = 1e-6, |
| 2865 | resnet_time_scale_shift: str = "default", |
| 2866 | resnet_act_fn: str = "swish", |
| 2867 | resnet_pre_norm: bool = True, |
| 2868 | attention_head_dim: int = 1, |
| 2869 | output_scale_factor: float = np.sqrt(2.0), |
| 2870 | add_upsample: bool = True, |
| 2871 | ): |
| 2872 | super().__init__() |
| 2873 | self.attentions = nn.ModuleList([]) |
| 2874 | self.resnets = nn.ModuleList([]) |
| 2875 | |
| 2876 | for i in range(num_layers): |
| 2877 | res_skip_channels = in_channels if (i == num_layers - 1) else out_channels |
| 2878 | resnet_in_channels = prev_output_channel if i == 0 else out_channels |
| 2879 | |
| 2880 | self.resnets.append( |
| 2881 | ResnetBlock2D( |
| 2882 | in_channels=resnet_in_channels + res_skip_channels, |
| 2883 | out_channels=out_channels, |
| 2884 | temb_channels=temb_channels, |
| 2885 | eps=resnet_eps, |
| 2886 | groups=min(resnet_in_channels + res_skip_channels // 4, 32), |
| 2887 | groups_out=min(out_channels // 4, 32), |
| 2888 | dropout=dropout, |
| 2889 | time_embedding_norm=resnet_time_scale_shift, |
| 2890 | non_linearity=resnet_act_fn, |
| 2891 | output_scale_factor=output_scale_factor, |
| 2892 | pre_norm=resnet_pre_norm, |
| 2893 | ) |
| 2894 | ) |
| 2895 | |
| 2896 | if attention_head_dim is None: |
| 2897 | logger.warning( |
| 2898 | f"It is not recommend to pass `attention_head_dim=None`. Defaulting `attention_head_dim` to `out_channels`: {out_channels}." |
| 2899 | ) |
| 2900 | attention_head_dim = out_channels |
| 2901 | |
| 2902 | self.attentions.append( |
| 2903 | Attention( |
| 2904 | out_channels, |
| 2905 | heads=out_channels // attention_head_dim, |
| 2906 | dim_head=attention_head_dim, |
| 2907 | rescale_output_factor=output_scale_factor, |
| 2908 | eps=resnet_eps, |
| 2909 | norm_num_groups=32, |
| 2910 | residual_connection=True, |
| 2911 | bias=True, |