MCPcopy Create free account
hub / github.com/YesianRohn/TextSSR / upfirdn2d_native

Function upfirdn2d_native

diffusers/src/diffusers/models/upsampling.py:415–462  ·  view source on GitHub ↗
(
    tensor: torch.Tensor,
    kernel: torch.Tensor,
    up: int = 1,
    down: int = 1,
    pad: Tuple[int, int] = (0, 0),
)

Source from the content-addressed store, hash-verified

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

Callers 4

_downsample_2dMethod · 0.85
downsample_2dFunction · 0.85
_upsample_2dMethod · 0.85
upsample_2dFunction · 0.85

Calls 2

padMethod · 0.80
toMethod · 0.45

Tested by

no test coverage detected