r"""Upsample2D a batch of 2D images with the given filter. Accepts a batch of 2D images of the shape `[N, C, H, W]` or `[N, H, W, C]` and upsamples each image with the given filter. The filter is normalized so that if the input pixels are constant, they will be scaled by the specified `g
(
hidden_states: torch.FloatTensor,
kernel: Optional[torch.FloatTensor] = None,
factor: int = 2,
gain: float = 1,
)
| 408 | |
| 409 | |
| 410 | def upsample_2d( |
| 411 | hidden_states: torch.FloatTensor, |
| 412 | kernel: Optional[torch.FloatTensor] = None, |
| 413 | factor: int = 2, |
| 414 | gain: float = 1, |
| 415 | ) -> torch.FloatTensor: |
| 416 | r"""Upsample2D a batch of 2D images with the given filter. |
| 417 | Accepts a batch of 2D images of the shape `[N, C, H, W]` or `[N, H, W, C]` and upsamples each image with the given |
| 418 | filter. The filter is normalized so that if the input pixels are constant, they will be scaled by the specified |
| 419 | `gain`. Pixels outside the image are assumed to be zero, and the filter is padded with zeros so that its shape is |
| 420 | a: multiple of the upsampling factor. |
| 421 | |
| 422 | Args: |
| 423 | hidden_states (`torch.FloatTensor`): |
| 424 | Input tensor of the shape `[N, C, H, W]` or `[N, H, W, C]`. |
| 425 | kernel (`torch.FloatTensor`, *optional*): |
| 426 | FIR filter of the shape `[firH, firW]` or `[firN]` (separable). The default is `[1] * factor`, which |
| 427 | corresponds to nearest-neighbor upsampling. |
| 428 | factor (`int`, *optional*, default to `2`): |
| 429 | Integer upsampling factor. |
| 430 | gain (`float`, *optional*, default to `1.0`): |
| 431 | Scaling factor for signal magnitude (default: 1.0). |
| 432 | |
| 433 | Returns: |
| 434 | output (`torch.FloatTensor`): |
| 435 | Tensor of the shape `[N, C, H * factor, W * factor]` |
| 436 | """ |
| 437 | assert isinstance(factor, int) and factor >= 1 |
| 438 | if kernel is None: |
| 439 | kernel = [1] * factor |
| 440 | |
| 441 | kernel = torch.tensor(kernel, dtype=torch.float32) |
| 442 | if kernel.ndim == 1: |
| 443 | kernel = torch.outer(kernel, kernel) |
| 444 | kernel /= torch.sum(kernel) |
| 445 | |
| 446 | kernel = kernel * (gain * (factor**2)) |
| 447 | pad_value = kernel.shape[0] - factor |
| 448 | output = upfirdn2d_native( |
| 449 | hidden_states, |
| 450 | kernel.to(device=hidden_states.device), |
| 451 | up=factor, |
| 452 | pad=((pad_value + 1) // 2 + factor - 1, pad_value // 2), |
| 453 | ) |
| 454 | return output |
no test coverage detected