| 1995 | |
| 1996 | |
| 1997 | class KDownBlock2D(nn.Module): |
| 1998 | def __init__( |
| 1999 | self, |
| 2000 | in_channels: int, |
| 2001 | out_channels: int, |
| 2002 | temb_channels: int, |
| 2003 | dropout: float = 0.0, |
| 2004 | num_layers: int = 4, |
| 2005 | resnet_eps: float = 1e-5, |
| 2006 | resnet_act_fn: str = "gelu", |
| 2007 | resnet_group_size: int = 32, |
| 2008 | add_downsample: bool = False, |
| 2009 | ): |
| 2010 | super().__init__() |
| 2011 | resnets = [] |
| 2012 | |
| 2013 | for i in range(num_layers): |
| 2014 | in_channels = in_channels if i == 0 else out_channels |
| 2015 | groups = in_channels // resnet_group_size |
| 2016 | groups_out = out_channels // resnet_group_size |
| 2017 | |
| 2018 | resnets.append( |
| 2019 | ResnetBlockCondNorm2D( |
| 2020 | in_channels=in_channels, |
| 2021 | out_channels=out_channels, |
| 2022 | dropout=dropout, |
| 2023 | temb_channels=temb_channels, |
| 2024 | groups=groups, |
| 2025 | groups_out=groups_out, |
| 2026 | eps=resnet_eps, |
| 2027 | non_linearity=resnet_act_fn, |
| 2028 | time_embedding_norm="ada_group", |
| 2029 | conv_shortcut_bias=False, |
| 2030 | ) |
| 2031 | ) |
| 2032 | |
| 2033 | self.resnets = nn.ModuleList(resnets) |
| 2034 | |
| 2035 | if add_downsample: |
| 2036 | # YiYi's comments- might be able to use FirDownsample2D, look into details later |
| 2037 | self.downsamplers = nn.ModuleList([KDownsample2D()]) |
| 2038 | else: |
| 2039 | self.downsamplers = None |
| 2040 | |
| 2041 | self.gradient_checkpointing = False |
| 2042 | |
| 2043 | def forward( |
| 2044 | self, hidden_states: torch.Tensor, temb: torch.Tensor | None = None, *args, **kwargs |
| 2045 | ) -> tuple[torch.Tensor, tuple[torch.Tensor, ...]]: |
| 2046 | if len(args) > 0 or kwargs.get("scale", None) is not None: |
| 2047 | 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`." |
| 2048 | deprecate("scale", "1.0.0", deprecation_message) |
| 2049 | |
| 2050 | output_states = () |
| 2051 | |
| 2052 | for resnet in self.resnets: |
| 2053 | if torch.is_grad_enabled() and self.gradient_checkpointing: |
| 2054 | hidden_states = self._gradient_checkpointing_func(resnet, hidden_states, temb) |
no outgoing calls
no test coverage detected
searching dependent graphs…