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.Tensor,
kernel: torch.Tensor | None = None,
factor: int = 2,
gain: float = 1,
)
| 469 | |
| 470 | |
| 471 | def upsample_2d( |
| 472 | hidden_states: torch.Tensor, |
| 473 | kernel: torch.Tensor | None = None, |
| 474 | factor: int = 2, |
| 475 | gain: float = 1, |
| 476 | ) -> torch.Tensor: |
| 477 | r"""Upsample2D a batch of 2D images with the given filter. |
| 478 | 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 |
| 479 | filter. The filter is normalized so that if the input pixels are constant, they will be scaled by the specified |
| 480 | `gain`. Pixels outside the image are assumed to be zero, and the filter is padded with zeros so that its shape is |
| 481 | a: multiple of the upsampling factor. |
| 482 | |
| 483 | Args: |
| 484 | hidden_states (`torch.Tensor`): |
| 485 | Input tensor of the shape `[N, C, H, W]` or `[N, H, W, C]`. |
| 486 | kernel (`torch.Tensor`, *optional*): |
| 487 | FIR filter of the shape `[firH, firW]` or `[firN]` (separable). The default is `[1] * factor`, which |
| 488 | corresponds to nearest-neighbor upsampling. |
| 489 | factor (`int`, *optional*, default to `2`): |
| 490 | Integer upsampling factor. |
| 491 | gain (`float`, *optional*, default to `1.0`): |
| 492 | Scaling factor for signal magnitude (default: 1.0). |
| 493 | |
| 494 | Returns: |
| 495 | output (`torch.Tensor`): |
| 496 | Tensor of the shape `[N, C, H * factor, W * factor]` |
| 497 | """ |
| 498 | assert isinstance(factor, int) and factor >= 1 |
| 499 | if kernel is None: |
| 500 | kernel = [1] * factor |
| 501 | |
| 502 | kernel = torch.tensor(kernel, dtype=torch.float32) |
| 503 | if kernel.ndim == 1: |
| 504 | kernel = torch.outer(kernel, kernel) |
| 505 | kernel /= torch.sum(kernel) |
| 506 | |
| 507 | kernel = kernel * (gain * (factor**2)) |
| 508 | pad_value = kernel.shape[0] - factor |
| 509 | output = upfirdn2d_native( |
| 510 | hidden_states, |
| 511 | kernel.to(device=hidden_states.device), |
| 512 | up=factor, |
| 513 | pad=((pad_value + 1) // 2 + factor - 1, pad_value // 2), |
| 514 | ) |
| 515 | return output |
no test coverage detected
searching dependent graphs…