(
tensor: torch.Tensor,
kernel: torch.Tensor,
up: int = 1,
down: int = 1,
pad: tuple[int, int] = (0, 0),
)
| 419 | |
| 420 | |
| 421 | def upfirdn2d_native( |
| 422 | tensor: torch.Tensor, |
| 423 | kernel: torch.Tensor, |
| 424 | up: int = 1, |
| 425 | down: int = 1, |
| 426 | pad: tuple[int, int] = (0, 0), |
| 427 | ) -> torch.Tensor: |
| 428 | up_x = up_y = up |
| 429 | down_x = down_y = down |
| 430 | pad_x0 = pad_y0 = pad[0] |
| 431 | pad_x1 = pad_y1 = pad[1] |
| 432 | |
| 433 | _, channel, in_h, in_w = tensor.shape |
| 434 | tensor = tensor.reshape(-1, in_h, in_w, 1) |
| 435 | |
| 436 | _, in_h, in_w, minor = tensor.shape |
| 437 | kernel_h, kernel_w = kernel.shape |
| 438 | |
| 439 | out = tensor.view(-1, in_h, 1, in_w, 1, minor) |
| 440 | out = F.pad(out, [0, 0, 0, up_x - 1, 0, 0, 0, up_y - 1]) |
| 441 | out = out.view(-1, in_h * up_y, in_w * up_x, minor) |
| 442 | |
| 443 | out = F.pad(out, [0, 0, max(pad_x0, 0), max(pad_x1, 0), max(pad_y0, 0), max(pad_y1, 0)]) |
| 444 | out = out.to(tensor.device) # Move back to mps if necessary |
| 445 | out = out[ |
| 446 | :, |
| 447 | max(-pad_y0, 0) : out.shape[1] - max(-pad_y1, 0), |
| 448 | max(-pad_x0, 0) : out.shape[2] - max(-pad_x1, 0), |
| 449 | :, |
| 450 | ] |
| 451 | |
| 452 | out = out.permute(0, 3, 1, 2) |
| 453 | out = out.reshape([-1, 1, in_h * up_y + pad_y0 + pad_y1, in_w * up_x + pad_x0 + pad_x1]) |
| 454 | w = torch.flip(kernel, [0, 1]).view(1, 1, kernel_h, kernel_w) |
| 455 | out = F.conv2d(out, w) |
| 456 | out = out.reshape( |
| 457 | -1, |
| 458 | minor, |
| 459 | in_h * up_y + pad_y0 + pad_y1 - kernel_h + 1, |
| 460 | in_w * up_x + pad_x0 + pad_x1 - kernel_w + 1, |
| 461 | ) |
| 462 | out = out.permute(0, 2, 3, 1) |
| 463 | out = out[:, ::down_y, ::down_x, :] |
| 464 | |
| 465 | out_h = (in_h * up_y + pad_y0 + pad_y1 - kernel_h) // down_y + 1 |
| 466 | out_w = (in_w * up_x + pad_x0 + pad_x1 - kernel_w) // down_x + 1 |
| 467 | |
| 468 | return out.view(-1, channel, out_h, out_w) |
| 469 | |
| 470 | |
| 471 | def upsample_2d( |
no test coverage detected
searching dependent graphs…