From c0493723f7851c4a5cc05ef55d3320bd9ab7718d Mon Sep 17 00:00:00 2001 From: Yih-Dar <2521628+ydshieh@users.noreply.github.com> Date: Fri, 16 Sep 2022 15:15:05 +0200 Subject: [PATCH] Remove the usage of numpy in up/down sample_2d (#503) * Fix PT up/down sample_2d * empty commit * style * style Co-authored-by: ydshieh --- src/diffusers/models/resnet.py | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/src/diffusers/models/resnet.py b/src/diffusers/models/resnet.py index 507ca863..de0d255f 100644 --- a/src/diffusers/models/resnet.py +++ b/src/diffusers/models/resnet.py @@ -1,6 +1,5 @@ from functools import partial -import numpy as np import torch import torch.nn as nn import torch.nn.functional as F @@ -134,10 +133,10 @@ class FirUpsample2D(nn.Module): kernel = [1] * factor # setup kernel - kernel = np.asarray(kernel, dtype=np.float32) + kernel = torch.tensor(kernel, dtype=torch.float32) if kernel.ndim == 1: - kernel = np.outer(kernel, kernel) - kernel /= np.sum(kernel) + kernel = torch.outer(kernel, kernel) + kernel /= torch.sum(kernel) kernel = kernel * (gain * (factor**2)) @@ -219,10 +218,10 @@ class FirDownsample2D(nn.Module): kernel = [1] * factor # setup kernel - kernel = np.asarray(kernel, dtype=np.float32) + kernel = torch.tensor(kernel, dtype=torch.float32) if kernel.ndim == 1: - kernel = np.outer(kernel, kernel) - kernel /= np.sum(kernel) + kernel = torch.outer(kernel, kernel) + kernel /= torch.sum(kernel) kernel = kernel * gain @@ -391,16 +390,14 @@ def upsample_2d(x, kernel=None, factor=2, gain=1): if kernel is None: kernel = [1] * factor - kernel = np.asarray(kernel, dtype=np.float32) + kernel = torch.tensor(kernel, dtype=torch.float32) if kernel.ndim == 1: - kernel = np.outer(kernel, kernel) - kernel /= np.sum(kernel) + kernel = torch.outer(kernel, kernel) + kernel /= torch.sum(kernel) kernel = kernel * (gain * (factor**2)) p = kernel.shape[0] - factor - return upfirdn2d_native( - x, torch.tensor(kernel, device=x.device), up=factor, pad=((p + 1) // 2 + factor - 1, p // 2) - ) + return upfirdn2d_native(x, kernel.to(device=x.device), up=factor, pad=((p + 1) // 2 + factor - 1, p // 2)) def downsample_2d(x, kernel=None, factor=2, gain=1): @@ -425,14 +422,14 @@ def downsample_2d(x, kernel=None, factor=2, gain=1): if kernel is None: kernel = [1] * factor - kernel = np.asarray(kernel, dtype=np.float32) + kernel = torch.tensor(kernel, dtype=torch.float32) if kernel.ndim == 1: - kernel = np.outer(kernel, kernel) - kernel /= np.sum(kernel) + kernel = torch.outer(kernel, kernel) + kernel /= torch.sum(kernel) kernel = kernel * gain p = kernel.shape[0] - factor - return upfirdn2d_native(x, torch.tensor(kernel, device=x.device), down=factor, pad=((p + 1) // 2, p // 2)) + return upfirdn2d_native(x, kernel.to(device=x.device), down=factor, pad=((p + 1) // 2, p // 2)) def upfirdn2d_native(input, kernel, up=1, down=1, pad=(0, 0)):