Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/gather.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,33 @@ function gather_kernel!(dst, src, idx::CUDA.CuDeviceArray{<:CartesianIndex}, max
return nothing
end

function checkbounds_src(src, dims::Int, ::Type{<:Any})
return i -> checkbounds(Bool, src, ntuple(x -> Colon(), dims)..., i...)
end

function checkbounds_src(src, dims::Int, ::Type{<:CartesianIndex})
return i -> checkbounds(Bool, src, ntuple(x -> Colon(), dims)..., i)
end

function NNlib.gather!(dst::AnyCuArray, src::AnyCuArray, idx::AnyCuArray)
# check dims
dims = gather_check_dims(src, dst, idx)
dims_size = size(src)[1:dims]
max_dims_idx = prod(dims_size)
max_idx = max_dims_idx * length(idx)
args = dst, src, idx, max_idx, max_dims_idx, dims_size

# check bounds
chk = checkbounds_src(src, dims, eltype(idx))
isempty(src) && return dst
in_bnd = map(chk, collect(idx))
if !all(in_bnd)
Copy link

@YichengDWu YichengDWu Jun 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line slows down the code to a great portion

j = findfirst(i -> !i, in_bnd)
k = CUDA.@allowscalar idx[j]
throw(BoundsError(src, k))
end

# cuda kernel
args = dst, src, idx, max_idx, max_dims_idx, dims_size
kernel = @cuda launch=false gather_kernel!(args...)
config = launch_configuration(kernel.fun; max_threads=256)
threads = min(max_idx, config.threads)
Expand Down
12 changes: 12 additions & 0 deletions test/gather.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
gputest(src -> NNlib.gather(src, index), src, checkgrad=true)
@test NNlib.gather!(CUDA.zeros(T, size(index)...), src, index) == output
@test_throws ArgumentError NNlib.gather!(zeros(T, 3, 5), src, index)
index[1,:] .= 6
@test_throws BoundsError NNlib.gather(src, index)

## 1d src, 2d index of tuples -> 2d output
src = CT([3, 4, 5, 6, 7])
Expand Down Expand Up @@ -89,4 +91,14 @@
@test y isa CuArray{Float32,3}
@test size(y) == (size(src)[1:Nsrc-M]..., size(index)...)
gputest(src -> NNlib.gather(src, index), src, checkgrad=true)

## empty 2d src, 2d index of ints -> 3d output
src = CT(zeros(Int, 0, 3))
index = cu([1 2 3;
2 2 1;
3 1 3])

y = NNlib.gather(src, index)
@test y isa CuArray{Float32,3}
@test size(y) == (0, size(index)...)
end