-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
189 lines (153 loc) · 5.12 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import torch
from fftNd import *
import numpy as np
import time
torch.manual_seed(1230)
device = "cpu"
# Create input complex tensor
x = torch.rand(1,1,5,5,2)
# Test wrappers with 2D
y = rfftNd(x,2)
y = ifftNd(y,2)
y = rfftNd(x[:,:,:,:,0],2)
y = irfftNd(y,2)
############# test nD FFT
nDims = 6
dim_sizes = [1,1]
dim_sizes.extend(nDims*[5])
dim_sizes.extend([2])
# Create complex input tensor
x = torch.rand(*dim_sizes).to(device)
with torch.no_grad():
######### fftNd
# Run proposed implementation
torch.cuda.synchronize()
start = time.time()
y = fftNd(x,nDims)
torch.cuda.synchronize()
end = time.time()
print("time: " + str(end-start))
# Create Numpy tensor
xNumpy = x[0,0,:,...,0].cpu().numpy() + x[0,0,:,...,1].cpu().numpy()* 1j
# Run numpy implementation
start = time.time()
yNumpy = np.fft.fftn(xNumpy)
end = time.time()
print("time: " + str(end-start))
# Copy result to tensor
Y = torch.zeros_like(x)
Y[0,0,:,...,0] = torch.from_numpy(np.real(yNumpy))
Y[0,0,:,...,1] = torch.from_numpy(np.imag(yNumpy))
# Compute error
diff = abs(y-Y).sum().item()
print('\tfftNd diff: ' + str(diff))
########## ifftNd
torch.cuda.synchronize()
start = time.time()
y = ifftNd(x,nDims)
torch.cuda.synchronize()
end = time.time()
print("time: " + str(end-start))
# Create Numpy tensor
xNumpy = x[0,0,:,...,0].cpu().numpy() + x[0,0,:,...,1].cpu().numpy()* 1j
# Run numpy implementation
start = time.time()
yNumpy = np.fft.ifftn(xNumpy)
end = time.time()
print("time: " + str(end-start))
# Copy result to tensor
Y = torch.zeros_like(x)
Y[0,0,:,...,0] = torch.from_numpy(np.real(yNumpy))
Y[0,0,:,...,1] = torch.from_numpy(np.imag(yNumpy))
# Compute error
diff = abs(y-Y).sum().item()
print('\tifftNd diff: ' + str(diff))
########## rfftNd
torch.cuda.synchronize()
start = time.time()
y = rfftNd(x[...,0],nDims,onesided=True)
torch.cuda.synchronize()
end = time.time()
print("time: " + str(end-start))
# Create Numpy tensor
xNumpy = x[0,0,:,...,0].cpu().numpy()
# Run numpy implementation
start = time.time()
yNumpy = np.fft.rfftn(xNumpy)
end = time.time()
print("time: " + str(end-start))
# Copy result to tensor
Y = torch.zeros_like(y)
Y[0,0,:,...,0] = torch.from_numpy(np.real(yNumpy))
Y[0,0,:,...,1] = torch.from_numpy(np.imag(yNumpy))
# Compute error
diff = abs(y-Y).sum().item()
print('\trfftNd diff: ' + str(diff))
########## irfftNd
torch.cuda.synchronize()
start = time.time()
y = irfftNd(x,nDims, onesided=False)
torch.cuda.synchronize()
end = time.time()
print("time: " + str(end-start))
# Create Numpy tensor
xNumpy = x[0,0,:,...,0].cpu().numpy() + x[0,0,:,...,1].cpu().numpy()* 1j
# Run numpy implementation
start = time.time()
yNumpy = np.fft.irfftn(xNumpy, xNumpy.shape)
end = time.time()
print("time: " + str(end-start))
# Copy result to tensor
Y = torch.zeros_like(y)
Y[0,0,:,...] = torch.from_numpy(np.real(yNumpy))
# Compute error
diff = abs(y-Y).sum().item()
print('\tirfftNd diff: ' + str(diff))
######### convNd with fourier
def mulComplex(x, other):
out = torch.zeros_like(x)
out[...,0] = torch.sub(torch.mul(x[...,0], other[...,0]), torch.mul(x[...,1], other[...,1]))
out[...,1] = torch.add(torch.mul(x[...,0], other[...,1]), torch.mul(x[...,1], other[...,0]))
return out
def fft_conv(A,B):
nDims = A.ndim-2
padSize = (torch.tensor(A.shape[2:])-torch.tensor(B.shape[2:]))
padSizes = torch.zeros(2*nDims,dtype=int)
padSizes[0::2] = torch.floor(padSize/2.0)
padSizes[1::2] = torch.ceil(padSize/2.0)
padSizes = list(padSizes.numpy()[::-1])
B = F.pad(B,padSizes)
return ifftNd( mulComplex(rfftNd(A, nDims,onesided=False),rfftNd(B, nDims,onesided=False)), nDims)[...,0]
def np_fftconvolve(A, B):
nDims = A.ndim-2
padSize = (torch.tensor(A.shape[2:])-torch.tensor(B.shape[2:]))
padSizes = torch.zeros(2*nDims,dtype=int)
padSizes[0::2] = torch.floor(padSize/2.0)
padSizes[1::2] = torch.ceil(padSize/2.0)
padSizes = list(padSizes.numpy()[::-1])
B = F.pad(B,padSizes)
A = A[0,0,...].numpy()
B = B[0,0,...].numpy()
return np.real(np.fft.ifftn(np.fft.fftn(A)*np.fft.fftn(B, s=A.shape)))
# Create image and kernel
A = torch.rand(1,1,5,6,7,8)
B = torch.rand(1,1,2,3,4,5)
# Run proposed implementation
start = time.time()
C = fft_conv(A,B)
end = time.time()
print("time: " + str(end-start))
# Create Numpy tensors
Anumpy = A[0,0,...].numpy()
Bnumpy = B[0,0,...].numpy()
# Run numpy implementation
torch.cuda.synchronize()
start = time.time()
Cnumpy = np_fftconvolve(A,B)
torch.cuda.synchronize()
end = time.time()
print("time: " + str(end-start))
Cnumpy = torch.from_numpy(Cnumpy).unsqueeze(0).unsqueeze(0)
# Compute error
diff = abs(C-Cnumpy).sum().item()
print('\t Fourier Conv. diff: ' + str(diff))