Skip to content

Gather throw error on cpu and gpu array inputs #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
14 changes: 14 additions & 0 deletions src/gather.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,17 @@ function NNlib.gather!(dst::AnyCuArray, src::AnyCuArray, idx::AnyCuArray)
kernel(args...; threads=threads, blocks=blocks)
return dst
end

function NNlib.gather(src::AnyCuArray{Tsrc, Nsrc},
idx::AnyCuArray{Tidx, Nidx}) where
{Tsrc, Nsrc, Nidx, Tidx}
M = NNlib.typelength(Tidx)
dstsize = (size(src)[1:Nsrc-M]..., size(idx)...)
dst = similar(src, Tsrc, dstsize)
return NNlib.gather!(dst, src, idx)
end
Copy link
Member

Choose a reason for hiding this comment

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

Why we need this method? The one in Nlib is not enough?

Copy link
Member Author

Choose a reason for hiding this comment

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

I take it from NNlib. The dispatching rule needs to distinguish (::AnyCuArray, ::AnyCuArray) from (::AnyCuArray, ::AbstractArray).

Copy link
Member

Choose a reason for hiding this comment

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

I still don't see why we need this since we have (::AbstractArray, ::AbstractArray) in NNlib


function NNlib.gather(src::AnyCuArray, idx::AbstractArray)
err_msg = "src and idx both must be on GPU, but received $(typeof(src)) and $(typeof(idx)), respectively."
throw(ArgumentError(err_msg))
end
1 change: 1 addition & 0 deletions test/gather.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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)
@test_throws ArgumentError NNlib.gather(src, collect(index))

## 1d src, 2d index of tuples -> 2d output
src = CT([3, 4, 5, 6, 7])
Expand Down