| 1696 | |
| 1697 | |
| 1698 | class SkipDownBlock2D(nn.Module): |
| 1699 | def __init__( |
| 1700 | self, |
| 1701 | in_channels: int, |
| 1702 | out_channels: int, |
| 1703 | temb_channels: int, |
| 1704 | dropout: float = 0.0, |
| 1705 | num_layers: int = 1, |
| 1706 | resnet_eps: float = 1e-6, |
| 1707 | resnet_time_scale_shift: str = "default", |
| 1708 | resnet_act_fn: str = "swish", |
| 1709 | resnet_pre_norm: bool = True, |
| 1710 | output_scale_factor: float = np.sqrt(2.0), |
| 1711 | add_downsample: bool = True, |
| 1712 | downsample_padding: int = 1, |
| 1713 | ): |
| 1714 | super().__init__() |
| 1715 | self.resnets = nn.ModuleList([]) |
| 1716 | |
| 1717 | for i in range(num_layers): |
| 1718 | in_channels = in_channels if i == 0 else out_channels |
| 1719 | self.resnets.append( |
| 1720 | ResnetBlock2D( |
| 1721 | in_channels=in_channels, |
| 1722 | out_channels=out_channels, |
| 1723 | temb_channels=temb_channels, |
| 1724 | eps=resnet_eps, |
| 1725 | groups=min(in_channels // 4, 32), |
| 1726 | groups_out=min(out_channels // 4, 32), |
| 1727 | dropout=dropout, |
| 1728 | time_embedding_norm=resnet_time_scale_shift, |
| 1729 | non_linearity=resnet_act_fn, |
| 1730 | output_scale_factor=output_scale_factor, |
| 1731 | pre_norm=resnet_pre_norm, |
| 1732 | ) |
| 1733 | ) |
| 1734 | |
| 1735 | if add_downsample: |
| 1736 | self.resnet_down = ResnetBlock2D( |
| 1737 | in_channels=out_channels, |
| 1738 | out_channels=out_channels, |
| 1739 | temb_channels=temb_channels, |
| 1740 | eps=resnet_eps, |
| 1741 | groups=min(out_channels // 4, 32), |
| 1742 | dropout=dropout, |
| 1743 | time_embedding_norm=resnet_time_scale_shift, |
| 1744 | non_linearity=resnet_act_fn, |
| 1745 | output_scale_factor=output_scale_factor, |
| 1746 | pre_norm=resnet_pre_norm, |
| 1747 | use_in_shortcut=True, |
| 1748 | down=True, |
| 1749 | kernel="fir", |
| 1750 | ) |
| 1751 | self.downsamplers = nn.ModuleList([FirDownsample2D(out_channels, out_channels=out_channels)]) |
| 1752 | self.skip_conv = nn.Conv2d(3, out_channels, kernel_size=(1, 1), stride=(1, 1)) |
| 1753 | else: |
| 1754 | self.resnet_down = None |
| 1755 | self.downsamplers = None |