Skip to content

Commit

Permalink
migrate from ScatterNNlib
Browse files Browse the repository at this point in the history
refactor to current scatter API

refactor test cases
  • Loading branch information
yuehhua committed Mar 16, 2021
1 parent ca82fb2 commit 9873a8b
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/NNlibCUDA/src/NNlibCUDA.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ using Random, Statistics
include("upsample.jl")
include("activations.jl")
include("batchedmul.jl")
include("scatter.jl")
include("cudnn/cudnn.jl")
include("cudnn/conv.jl")
include("cudnn/pooling.jl")
Expand Down
24 changes: 24 additions & 0 deletions lib/NNlibCUDA/src/scatter.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
ATM_OPS = Dict((+) => CUDA.atomic_add!, (-) => CUDA.atomic_sub!, (max) => CUDA.atomic_max!, (min) => CUDA.atomic_min!,
(*) => CUDA.atomic_mul!, (/) => CUDA.atomic_div!, (&) => CUDA.atomic_and!, (|) => CUDA.atomic_or!)

function scatter!(op, dst::CuArray, src::CuArray, idx::CuArray{IntOrIntTuple})
function kernel!(atm_op, dst, src, idx)
li = threadIdx().y + (blockIdx().y - 1) * blockDim().y
i = threadIdx().x + (blockIdx().x - 1) * blockDim().x

@inbounds if li <= length(idx) && i <= size(dst, 1)
ind = CartesianIndices(idx)[li]
j = Base._to_linear_index(dst, i, idx[li]...)
atm_op(pointer(dst, j), src[i, ind])
end

return
end

thread_x = min(MAX_THREADS, size(dst, 1))
thread_y = min(MAX_THREADS ÷ thread_x, length(idx))
threads = (thread_x, thread_y)
blocks = ceil.(Int, (size(dst, 1), length(idx)) ./ threads)
@cuda blocks=blocks threads=threads kernel!(ATM_OPS[op], dst, src, idx)
return dst
end
1 change: 1 addition & 0 deletions lib/NNlibCUDA/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ if CUDA.has_cuda()
include("pooling.jl")
include("softmax.jl")
include("batchnorm.jl")
include("scatter.jl")
end
147 changes: 147 additions & 0 deletions lib/NNlibCUDA/test/scatter.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
dsts = Dict(
0 => cu([3, 4, 5, 6, 7]),
1 => cu([3 3 4 4 5;
5 5 6 6 7]),
)
srcs = Dict(
(0, true) => cu(ones(Int, 3, 4)),
(0, false) => cu(ones(Int, 3) * collect(1:4)'),
(1, true) => cu(ones(Int, 2, 3, 4)),
(1, false) => cu([1, 2] .* reshape(ones(Int, 3) * collect(1:4)', 1,3,4)),
)
idxs = Dict(
:int => cu([1 2 3 4;
4 2 1 3;
3 5 5 3]),
:tup => cu([(1,) (2,) (3,) (4,);
(4,) (2,) (1,) (3,);
(3,) (5,) (5,) (3,)]),
)
res = Dict(
(+, 0, true) => cu([5, 6, 9, 8, 9]),
(+, 1, true) => cu([5 5 8 6 7;
7 7 10 8 9]),
(+, 0, false) => cu([4, 4, 12, 5, 5]),
(+, 1, false) => cu([4 4 12 5 5;
8 8 24 10 10]),
(-, 0, true) => cu([1, 2, 1, 4, 5]),
(-, 1, true) => cu([1 1 0 2 3;
3 3 2 4 5]),
(-, 0, false) => cu([-4, -4, -12, -5, -5]),
(-, 1, false) => cu([-4 -4 -12 -5 -5;
-8 -8 -24 -10 -10]),
(max, 0, true) => cu([3, 4, 5, 6, 7]),
(max, 1, true) => cu([3 3 4 4 5;
5 5 6 6 7]),
(max, 0, false) => cu([3, 2, 4, 4, 3]),
(max, 1, false) => cu([3 2 4 4 3;
6 4 8 8 6]),
(min, 0, true) => cu([1, 1, 1, 1, 1]),
(min, 1, true) => cu([1 1 1 1 1;
1 1 1 1 1]),
(min, 0, false) => cu([1, 2, 1, 1, 2]),
(min, 1, false) => cu([1 2 1 1 2;
2 4 2 2 4]),
(*, 0, true) => cu([3, 4, 5, 6, 7]),
(*, 1, true) => cu([3 3 4 4 5;
5 5 6 6 7]),
(*, 0, false) => cu([3, 4, 48, 4, 6]),
(*, 1, false) => cu([3 4 48 4 6;
12 16 768 16 24]),
(/, 0, true) => cu([0.75, 1., 0.3125, 1.5, 1.75]),
(/, 1, true) => cu([0.75 0.75 0.25 1. 1.25;
1.25 1.25 0.375 1.5 1.75]),
(/, 0, false) => cu([1//3, 1//4, 1//48, 1//4, 1//6]),
(/, 1, false) => cu([1//3 1//4 1//48 1//4 1//6;
1//12 1//16 1//768 1//16 1//24]),
(mean, 0, true) => cu([4., 5., 6., 7., 8.]),
(mean, 1, true) => cu([4. 4. 5. 5. 6.;
6. 6. 7. 7. 8.]),
(mean, 0, false) => cu([2, 2, 3, 2.5, 2.5]),
(mean, 1, false) => cu([2. 2. 3. 2.5 2.5;
4. 4. 6. 5. 5.]),
)

types = [UInt32, UInt64, Int32, Int64, Float32, Float64]


@testset "scatter" begin
for T = types
@testset "$(T)" begin
@testset "+" begin
for idx = values(idxs), dims = [0, 1]
mutated = true
@test scatter!(+, T.(dsts[dims]), srcs[(dims, mutated)], idx) == T.(res[(+, dims, mutated)])

mutated = false
# @test scatter(+, srcs[(dims, mutated)], idx) == T.(res[(+, dims, mutated)])
end
end

@testset "-" begin
for idx = values(idxs), dims = [0, 1]
mutated = true
@test scatter!(-, T.(dsts[dims]), srcs[(dims, mutated)], idx) == T.(res[(-, dims, mutated)])

mutated = false
# @test scatter(-, srcs[(dims, mutated)], idx) == T.(res[(-, dims, mutated)])
end
end

@testset "max" begin
for idx = values(idxs), dims = [0, 1]
mutated = true
@test scatter!(max, T.(dsts[dims]), srcs[(dims, mutated)], idx) == T.(res[(max, dims, mutated)])

mutated = false
# @test scatter(max, srcs[(dims, mutated)], idx) == T.(res[(max, dims, mutated)])
end
end

@testset "min" begin
for idx = values(idxs), dims = [0, 1]
mutated = true
@test scatter!(min, T.(dsts[dims]), srcs[(dims, mutated)], idx) == T.(res[(min, dims, mutated)])

mutated = false
# @test scatter(min, srcs[(dims, mutated)], idx) == T.(res[(min, dims, mutated)])
end
end
end
end


for T = [Float32, Float64]
@testset "$(T)" begin
@testset "*" begin
for idx = values(idxs), dims = [0, 1]
mutated = true
@test scatter!(*, T.(dsts[dims]), srcs[(dims, mutated)], idx) == T.(res[(*, dims, mutated)])

mutated = false
# @test scatter(*, srcs[(dims, mutated)], idx) == T.(res[(*, dims, mutated)])
end
end

@testset "/" begin
for idx = values(idxs), dims = [0, 1]
mutated = true
@test scatter!(/, T.(dsts[dims]), srcs[(dims, mutated)].*2, idx) == T.(res[(/, dims, mutated)])

mutated = false
# @test scatter(/, srcs[(dims, mutated)], idx) == T.(res[(/, dims, mutated)])
end
end

@testset "mean" begin
for idx = values(idxs), dims = [0, 1]
mutated = true
@test scatter!(mean, T.(dsts[dims]), srcs[(dims, mutated)], idx) == T.(res[(mean, dims, mutated)])

mutated = false
# @test scatter(mean, srcs[(dims, mutated)], idx) == T.(res[(mean, dims, mutated)])
end
end
end
end
end

0 comments on commit 9873a8b

Please sign in to comment.