A 2D FIR upsampling layer with an optional convolution. Parameters: channels (`int`, optional): number of channels in the inputs and outputs. use_conv (`bool`, default `False`): option to use a convolution. out_channels (`int`, optional):
| 194 | |
| 195 | |
| 196 | class FirUpsample2D(nn.Module): |
| 197 | """A 2D FIR upsampling layer with an optional convolution. |
| 198 | |
| 199 | Parameters: |
| 200 | channels (`int`, optional): |
| 201 | number of channels in the inputs and outputs. |
| 202 | use_conv (`bool`, default `False`): |
| 203 | option to use a convolution. |
| 204 | out_channels (`int`, optional): |
| 205 | number of output channels. Defaults to `channels`. |
| 206 | fir_kernel (`tuple`, default `(1, 3, 3, 1)`): |
| 207 | kernel for the FIR filter. |
| 208 | """ |
| 209 | |
| 210 | def __init__( |
| 211 | self, |
| 212 | channels: Optional[int] = None, |
| 213 | out_channels: Optional[int] = None, |
| 214 | use_conv: bool = False, |
| 215 | fir_kernel: Tuple[int, int, int, int] = (1, 3, 3, 1), |
| 216 | ): |
| 217 | super().__init__() |
| 218 | out_channels = out_channels if out_channels else channels |
| 219 | if use_conv: |
| 220 | self.Conv2d_0 = nn.Conv2d(channels, out_channels, kernel_size=3, stride=1, padding=1) |
| 221 | self.use_conv = use_conv |
| 222 | self.fir_kernel = fir_kernel |
| 223 | self.out_channels = out_channels |
| 224 | |
| 225 | def _upsample_2d( |
| 226 | self, |
| 227 | hidden_states: torch.FloatTensor, |
| 228 | weight: Optional[torch.FloatTensor] = None, |
| 229 | kernel: Optional[torch.FloatTensor] = None, |
| 230 | factor: int = 2, |
| 231 | gain: float = 1, |
| 232 | ) -> torch.FloatTensor: |
| 233 | """Fused `upsample_2d()` followed by `Conv2d()`. |
| 234 | |
| 235 | Padding is performed only once at the beginning, not between the operations. The fused op is considerably more |
| 236 | efficient than performing the same calculation using standard TensorFlow ops. It supports gradients of |
| 237 | arbitrary order. |
| 238 | |
| 239 | Args: |
| 240 | hidden_states (`torch.FloatTensor`): |
| 241 | Input tensor of the shape `[N, C, H, W]` or `[N, H, W, C]`. |
| 242 | weight (`torch.FloatTensor`, *optional*): |
| 243 | Weight tensor of the shape `[filterH, filterW, inChannels, outChannels]`. Grouped convolution can be |
| 244 | performed by `inChannels = x.shape[0] // numGroups`. |
| 245 | kernel (`torch.FloatTensor`, *optional*): |
| 246 | FIR filter of the shape `[firH, firW]` or `[firN]` (separable). The default is `[1] * factor`, which |
| 247 | corresponds to nearest-neighbor upsampling. |
| 248 | factor (`int`, *optional*): Integer upsampling factor (default: 2). |
| 249 | gain (`float`, *optional*): Scaling factor for signal magnitude (default: 1.0). |
| 250 | |
| 251 | Returns: |
| 252 | output (`torch.FloatTensor`): |
| 253 | Tensor of the shape `[N, C, H * factor, W * factor]` or `[N, H * factor, W * factor, C]`, and same |