A 2D FIR downsampling layer with an optional convolution. Parameters: channels (`int`): number of channels in the inputs and outputs. use_conv (`bool`, default `False`): option to use a convolution. out_channels (`int`, optional): numb
| 155 | |
| 156 | |
| 157 | class FirDownsample2D(nn.Module): |
| 158 | """A 2D FIR downsampling layer with an optional convolution. |
| 159 | |
| 160 | Parameters: |
| 161 | channels (`int`): |
| 162 | number of channels in the inputs and outputs. |
| 163 | use_conv (`bool`, default `False`): |
| 164 | option to use a convolution. |
| 165 | out_channels (`int`, optional): |
| 166 | number of output channels. Defaults to `channels`. |
| 167 | fir_kernel (`tuple`, default `(1, 3, 3, 1)`): |
| 168 | kernel for the FIR filter. |
| 169 | """ |
| 170 | |
| 171 | def __init__( |
| 172 | self, |
| 173 | channels: Optional[int] = None, |
| 174 | out_channels: Optional[int] = None, |
| 175 | use_conv: bool = False, |
| 176 | fir_kernel: Tuple[int, int, int, int] = (1, 3, 3, 1), |
| 177 | ): |
| 178 | super().__init__() |
| 179 | out_channels = out_channels if out_channels else channels |
| 180 | if use_conv: |
| 181 | self.Conv2d_0 = nn.Conv2d(channels, out_channels, kernel_size=3, stride=1, padding=1) |
| 182 | self.fir_kernel = fir_kernel |
| 183 | self.use_conv = use_conv |
| 184 | self.out_channels = out_channels |
| 185 | |
| 186 | def _downsample_2d( |
| 187 | self, |
| 188 | hidden_states: torch.FloatTensor, |
| 189 | weight: Optional[torch.FloatTensor] = None, |
| 190 | kernel: Optional[torch.FloatTensor] = None, |
| 191 | factor: int = 2, |
| 192 | gain: float = 1, |
| 193 | ) -> torch.FloatTensor: |
| 194 | """Fused `Conv2d()` followed by `downsample_2d()`. |
| 195 | Padding is performed only once at the beginning, not between the operations. The fused op is considerably more |
| 196 | efficient than performing the same calculation using standard TensorFlow ops. It supports gradients of |
| 197 | arbitrary order. |
| 198 | |
| 199 | Args: |
| 200 | hidden_states (`torch.FloatTensor`): |
| 201 | Input tensor of the shape `[N, C, H, W]` or `[N, H, W, C]`. |
| 202 | weight (`torch.FloatTensor`, *optional*): |
| 203 | Weight tensor of the shape `[filterH, filterW, inChannels, outChannels]`. Grouped convolution can be |
| 204 | performed by `inChannels = x.shape[0] // numGroups`. |
| 205 | kernel (`torch.FloatTensor`, *optional*): |
| 206 | FIR filter of the shape `[firH, firW]` or `[firN]` (separable). The default is `[1] * factor`, which |
| 207 | corresponds to average pooling. |
| 208 | factor (`int`, *optional*, default to `2`): |
| 209 | Integer downsampling factor. |
| 210 | gain (`float`, *optional*, default to `1.0`): |
| 211 | Scaling factor for signal magnitude. |
| 212 | |
| 213 | Returns: |
| 214 | output (`torch.FloatTensor`): |