1
1
function upsample_testsuite (Backend)
2
- cpu, backend = CPU (), Backend ()
2
+ cpu (x) = adapt (CPU (), x)
3
+ device (x) = adapt (Backend (), x)
4
+ gradtest_fn = KernelAbstractions. isgpu (Backend ()) ? gputest : gradtest
3
5
T = Float32 # TODO test against all supported eltypes for each backend.
4
6
atol = T == Float32 ? 1e-3 : 1e-6
5
- gradtest_fn = backend == CPU () ? gradtest : gputest
6
7
7
8
@testset " upsample_nearest, integer scale via reshape" begin
8
- x = adapt (backend, reshape (T[1 2 ; 3 4 ], (2 ,2 ,1 ,1 )))
9
- @test adapt ( cpu, upsample_nearest (x, (3 ,3 )))[1 ,:] == [1 ,1 ,1 , 2 ,2 ,2 ]
9
+ x = device ( reshape (T[1 2 ; 3 4 ], (2 ,2 ,1 ,1 )))
10
+ @test cpu ( upsample_nearest (x, (3 ,3 )))[1 ,:] == [1 ,1 ,1 , 2 ,2 ,2 ]
10
11
11
12
y = upsample_nearest (x, (2 ,3 ))
12
13
@test size (y) == (4 ,6 ,1 ,1 )
13
14
y2 = upsample_nearest (x, size= (4 ,6 ))
14
- @test adapt ( cpu, y) ≈ adapt ( cpu, y2)
15
+ @test cpu ( y) ≈ cpu ( y2)
15
16
16
- @test adapt ( cpu, ∇upsample_nearest (y, (2 ,3 )))[:, :, 1 , 1 ] == [6 12 ; 18 24 ]
17
+ @test cpu ( ∇upsample_nearest (y, (2 ,3 )))[:, :, 1 , 1 ] == [6 12 ; 18 24 ]
17
18
gradtest_fn (
18
19
x -> upsample_nearest (x, (2 ,3 )),
19
- adapt (backend, rand (T, 2 ,2 ,1 ,1 )); atol)
20
+ device ( rand (T, 2 ,2 ,1 ,1 )); atol)
20
21
gradtest_fn (
21
22
x -> upsample_nearest (x, size= (4 ,6 )),
22
- adapt (backend, rand (T, 2 ,2 ,1 ,1 )); atol)
23
+ device ( rand (T, 2 ,2 ,1 ,1 )); atol)
23
24
24
25
@test_throws ArgumentError ∇upsample_nearest (y, (2 ,4 ))
25
26
@test_throws ArgumentError upsample_nearest (x, (1 ,2 ,3 ,4 ,5 ))
@@ -33,9 +34,9 @@ function upsample_testsuite(Backend)
33
34
y = collect (1 : 1 // 3 : 4 )
34
35
y = hcat (y,y,y)[:,:,:]
35
36
36
- xd = adapt (backend, x)
37
- @test y ≈ adapt ( cpu, upsample_linear (xd, 2.5 ))
38
- @test y ≈ adapt ( cpu, upsample_linear (xd; size= 10 ))
37
+ xd = device ( x)
38
+ @test y ≈ cpu ( upsample_linear (xd, 2.5 ))
39
+ @test y ≈ cpu ( upsample_linear (xd; size= 10 ))
39
40
gradtest_fn (x -> upsample_linear (x, 2.5 ), xd; atol)
40
41
end
41
42
@@ -56,18 +57,18 @@ function upsample_testsuite(Backend)
56
57
y_true = cat (y_true, y_true; dims= 3 )
57
58
y_true = cat (y_true, y_true; dims= 4 )
58
59
59
- xd = adapt (backend, x)
60
+ xd = device ( x)
60
61
y = upsample_bilinear (xd, (3 , 2 ))
61
62
@test size (y) == size (y_true)
62
63
@test eltype (y) == Float32
63
- @test adapt ( cpu, y) ≈ y_true
64
+ @test cpu ( y) ≈ y_true
64
65
65
66
gradtest_fn (x -> upsample_bilinear (x, (3 , 2 )), xd; atol)
66
67
67
68
# additional grad check, also compliant with pytorch
68
69
o = ones (Float32,6 ,4 ,2 ,1 )
69
70
grad_true = 6 * ones (Float32,2 ,2 ,2 ,1 )
70
- @test adapt ( cpu, ∇upsample_bilinear (adapt (backend, o); size = (2 ,2 ))) ≈ grad_true
71
+ @test cpu ( ∇upsample_bilinear (device ( o); size = (2 ,2 ))) ≈ grad_true
71
72
72
73
# CPU only tests.
73
74
@@ -110,7 +111,7 @@ function upsample_testsuite(Backend)
110
111
y_true[:,:,4 ,:,:] .= 2.5
111
112
y_true[:,:,5 ,:,:] .= 3.
112
113
113
- xd = adapt (backend, x)
114
+ xd = device ( x)
114
115
y = upsample_trilinear (xd; size= (5 ,5 ,5 ))
115
116
116
117
@test size (y) == size (y_true)
@@ -122,9 +123,9 @@ function upsample_testsuite(Backend)
122
123
atol= (T == Float32) ? 1e-2 : 1e-5 )
123
124
124
125
# This test only works when `align_corners=false`.
125
- o = adapt (backend, ones (Float32,8 ,8 ,8 ,1 ,1 ))
126
+ o = device ( ones (Float32,8 ,8 ,8 ,1 ,1 ))
126
127
grad_true = 8 * ones (Float32,4 ,4 ,4 ,1 ,1 )
127
- @test adapt ( cpu, ∇upsample_trilinear (o; size= (4 ,4 ,4 ), align_corners= false )) ≈ grad_true
128
+ @test cpu ( ∇upsample_trilinear (o; size= (4 ,4 ,4 ), align_corners= false )) ≈ grad_true
128
129
end
129
130
130
131
@testset " pixel_shuffle" begin
@@ -147,9 +148,9 @@ function upsample_testsuite(Backend)
147
148
2 10 4 12
148
149
6 14 8 16 ][:,:,:,:]
149
150
150
- y = pixel_shuffle (adapt (backend, x), 2 )
151
+ y = pixel_shuffle (device ( x), 2 )
151
152
@test size (y) == size (y_true)
152
- @test y_true == adapt ( cpu, y)
153
+ @test y_true == cpu ( y)
153
154
154
155
x = reshape (1 : 32 , (2 , 2 , 8 , 1 ))
155
156
y_true = zeros (Int, 4 , 4 , 2 , 1 )
@@ -163,28 +164,28 @@ function upsample_testsuite(Backend)
163
164
18 26 20 28
164
165
22 30 24 32 ]
165
166
166
- y = pixel_shuffle (adapt (backend, x), 2 )
167
+ y = pixel_shuffle (device ( x), 2 )
167
168
@test size (y) == size (y_true)
168
- @test y_true == adapt ( cpu, y)
169
+ @test y_true == cpu ( y)
169
170
170
171
x = reshape (1 : 4 * 3 * 27 * 2 , (4 ,3 ,27 ,2 ))
171
- y = pixel_shuffle (adapt (backend, x), 3 )
172
+ y = pixel_shuffle (device ( x), 3 )
172
173
@test size (y) == (12 , 9 , 3 , 2 )
173
174
174
175
# batch dimension is preserved
175
176
x1 = x[:,:,:,[1 ]]
176
177
x2 = x[:,:,:,[2 ]]
177
- y1 = pixel_shuffle (adapt (backend, x1), 3 )
178
- y2 = pixel_shuffle (adapt (backend, x2), 3 )
179
- @test adapt ( cpu, cat (y1, y2, dims= 4 )) == adapt ( cpu, y)
178
+ y1 = pixel_shuffle (device ( x1), 3 )
179
+ y2 = pixel_shuffle (device ( x2), 3 )
180
+ @test cpu ( cat (y1, y2, dims= 4 )) == cpu ( y)
180
181
181
182
for d in [1 , 2 , 3 ]
182
183
r = rand (1 : 5 )
183
184
n = rand (1 : 5 )
184
185
c = rand (1 : 5 )
185
186
insize = rand (1 : 5 , d)
186
187
x = rand (insize... , r^ d* c, n)
187
- xd = adapt (backend, x)
188
+ xd = device ( x)
188
189
189
190
y = pixel_shuffle (xd, r)
190
191
@test size (y) == ((r .* insize). .. , c, n)
@@ -195,19 +196,19 @@ function upsample_testsuite(Backend)
195
196
@testset " Complex-valued upsample" begin
196
197
for (d, method) in zip ([1 , 2 , 3 ], [upsample_linear, upsample_bilinear, upsample_trilinear])
197
198
for (k, interp) in zip ((2 , ntuple (_ -> 2 , d)), [method, upsample_nearest])
198
- x = adapt (backend, randn (Complex{Float32}, (4 ,8 ,12 )[1 : d]. .. , 1 , 1 ))
199
+ x = device ( randn (Complex{Float32}, (4 ,8 ,12 )[1 : d]. .. , 1 , 1 ))
199
200
200
201
upsize = (8 , 16 , 24 )[1 : d]
201
202
xup = interp (x, k)
202
203
@test size (xup)[1 : d] == upsize
203
- @test adapt ( cpu, real (xup)) == adapt ( cpu, interp (real (x), k))
204
- @test adapt ( cpu, imag (xup)) == adapt ( cpu, interp (imag (x), k))
204
+ @test cpu ( real (xup)) == cpu ( interp (real (x), k))
205
+ @test cpu ( imag (xup)) == cpu ( interp (imag (x), k))
205
206
206
207
upsize = (8 ,24 ,48 )[1 : d]
207
208
xup = interp (x; size= upsize)
208
209
@test size (xup)[1 : d] == upsize
209
- @test adapt ( cpu, real (xup)) == adapt ( cpu, interp (real (x), size= upsize))
210
- @test adapt ( cpu, imag (xup)) == adapt ( cpu, interp (imag (x), size= upsize))
210
+ @test cpu ( real (xup)) == cpu ( interp (real (x), size= upsize))
211
+ @test cpu ( imag (xup)) == cpu ( interp (imag (x), size= upsize))
211
212
end
212
213
end
213
214
end
0 commit comments