Spatially conditioned normalization as defined in https://arxiv.org/abs/2209.09002. Args: f_channels (`int`): The number of channels for input to group normalization layer, and output of the spatial norm layer. zq_channels (`int`): The number of chan
| 4808 | |
| 4809 | |
| 4810 | class SpatialNorm(nn.Module): |
| 4811 | """ |
| 4812 | Spatially conditioned normalization as defined in https://arxiv.org/abs/2209.09002. |
| 4813 | |
| 4814 | Args: |
| 4815 | f_channels (`int`): |
| 4816 | The number of channels for input to group normalization layer, and output of the spatial norm layer. |
| 4817 | zq_channels (`int`): |
| 4818 | The number of channels for the quantized vector as described in the paper. |
| 4819 | """ |
| 4820 | |
| 4821 | def __init__( |
| 4822 | self, |
| 4823 | f_channels: int, |
| 4824 | zq_channels: int, |
| 4825 | ): |
| 4826 | super().__init__() |
| 4827 | self.norm_layer = nn.GroupNorm(num_channels=f_channels, num_groups=32, eps=1e-6, affine=True) |
| 4828 | self.conv_y = nn.Conv2d(zq_channels, f_channels, kernel_size=1, stride=1, padding=0) |
| 4829 | self.conv_b = nn.Conv2d(zq_channels, f_channels, kernel_size=1, stride=1, padding=0) |
| 4830 | |
| 4831 | def forward(self, f: torch.Tensor, zq: torch.Tensor) -> torch.Tensor: |
| 4832 | f_size = f.shape[-2:] |
| 4833 | zq = F.interpolate(zq, size=f_size, mode="nearest") |
| 4834 | norm_f = self.norm_layer(f) |
| 4835 | new_f = norm_f * self.conv_y(zq) + self.conv_b(zq) |
| 4836 | return new_f |
| 4837 | |
| 4838 | |
| 4839 | class IPAdapterAttnProcessor(nn.Module): |