Skip to content
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

getindex for CuArray to fix repeated indices error #1131

Closed
wants to merge 7 commits into from
Closed
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
7 changes: 7 additions & 0 deletions src/lib/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,10 @@ using GPUArraysCore # replaces @require CUDA block, weird indenting to preserve

pull_block_vert(sz, Δ::AbstractGPUArray, A::Number) = @allowscalar Δ[sz]

∇getindex(x::T, inds::Tuple{AbstractArray{<:Integer}}) where {T <: AbstractGPUArray} = dy -> begin
inds1_cpu = Array(inds[1])
dx = zeros(eltype(dy), length(x))
dxv = view(dx, inds1_cpu)
dxv .= accum.(dxv, _droplike(Array(dy), dxv))
return _project(x, T(dx)), nothing
end
6 changes: 6 additions & 0 deletions test/cuda.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,9 @@ end
@test_skip gradient((x,y) -> sum(vcat(x,y)), 1f0, r, 2f0, r)[2] isa CUDA.CuArray{Float32}
end

@testset "repeated indexing" begin
f(a) = sum(view(a, [1, 1, 2]))
a = CUDA.CuArray([1.0f0, 1.0f0, 1.0f0])
@test f(a) == 3.0f0
@test Array(gradient(f, a)[1]) == [2.0f0, 1.0f0, 0.0f0]
end