-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a8b8ef1
commit 6226676
Showing
3 changed files
with
73 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,65 @@ | ||
using AMDGPU | ||
using CUDA | ||
using DINCAE: interpnd, interp_adjn | ||
using Flux | ||
using Test | ||
using LinearAlgebra | ||
using Test | ||
|
||
f(pos,B) = sum(interpnd(pos,B)) | ||
|
||
device_list = Any[identity] # CPU | ||
|
||
if CUDA.functional() | ||
push!(device_list,cu) | ||
end | ||
|
||
if AMDGPU.functional() | ||
push!(device_list,roc) | ||
end | ||
|
||
pos = [(1.5f0,2.f0),(5.f0,2.3f0)] | ||
values = [3.f0,4.f0] | ||
sz = (6,7) | ||
B = ones(Float32,sz) | ||
|
||
A = interp_adjn(pos,values,sz) | ||
A_ref = zeros(Float32,sz) | ||
A_ref[1,2] = A_ref[2,2] = 0.5 * 3 | ||
A_ref[5,2] = 0.7 * 4 | ||
A_ref[5,3] = 0.3 * 4 | ||
|
||
@test A[1,2] ≈ 1.5 | ||
@test A[2,2] ≈ 1.5 | ||
dA_ref = zeros(Float32,sz) | ||
dA_ref[1,2] = dA_ref[2,2] = 0.5 | ||
dA_ref[5,2] = 0.7 | ||
dA_ref[5,3] = 0.3 | ||
|
||
dA = gradient(f,pos,B)[2] | ||
|
||
@test dA[1,2] ≈ 0.5 | ||
@test dA[2,2] ≈ 0.5 | ||
|
||
if CUDA.functional() | ||
pos_d = cu(pos) | ||
values_d = cu(values) | ||
B_d = cu(B) | ||
for device in device_list | ||
pos_d = device(pos) | ||
values_d = device(values) | ||
B_d = device(B) | ||
|
||
A_d = interp_adjn(pos_d,values_d,sz) | ||
|
||
CUDA.@allowscalar begin | ||
@test A_d[1,2] ≈ 1.5 | ||
@test A_d[2,2] ≈ 1.5 | ||
end | ||
@test Array(A_d) ≈ A_ref | ||
|
||
dA_d = gradient(f,pos_d,B_d)[2] | ||
|
||
|
||
CUDA.@allowscalar begin | ||
@test dA_d[1,2] ≈ 0.5 | ||
@test dA_d[2,2] ≈ 0.5 | ||
end | ||
@test Array(dA_d) ≈ dA_ref | ||
end | ||
|
||
# check adjoint relationship | ||
|
||
A = randn(Float32,size(A,1),size(A,2)) | ||
Ai = randn(Float32,length(pos)) | ||
A = randn(Float32,sz) | ||
dAi = randn(Float32,length(pos)) | ||
|
||
@test Ai ⋅ interpnd(pos,A) ≈ A ⋅ interp_adjn(pos,Ai,size(A)) | ||
@test dAi ⋅ interpnd(pos,A) ≈ A ⋅ interp_adjn(pos,dAi,size(A)) | ||
|
||
for device in device_list[2:end] | ||
A_d = device(A) | ||
dAi_d = device(dAi) | ||
pos_d = device(pos) | ||
|
||
@test interpnd(pos,A) ≈ Array(interpnd(pos_d,A_d)) | ||
@test interp_adjn(pos,dAi,size(A)) ≈ Array(interp_adjn(pos_d,dAi_d,size(A))) | ||
|
||
@test dAi_d ⋅ interpnd(pos_d,A_d) ≈ A_d ⋅ interp_adjn(pos_d,dAi_d,size(A)) | ||
end |