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

Mixed getindex with AbstractZeros #716

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion src/rulesets/Base/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ and its element type is wide enough to allow `setindex!(dx, dy, inds...)`, which
It's unfortunate to close over `x`, but `similar(typeof(x), axes(x))` doesn't
allow `eltype(dy)`, nor does it work for many structured matrices.
"""
_setindex_zero(x::AbstractArray{<:Number}, dy, inds::Integer...) = fill!(similar(x, typeof(dy), axes(x)), false)
_setindex_zero(x::AbstractArray{<:Number}, dy, inds::Integer...) = fill!(similar(x, eltype(dy), axes(x)), false)
_setindex_zero(x::AbstractArray{<:Number}, dy, inds...) = fill!(similar(x, eltype(dy), axes(x)), false)
function _setindex_zero(x::AbstractArray, dy, inds::Integer...)
# This allows for types which don't define zero (like Vector) and types whose zero special (like Tangent),
Expand Down
20 changes: 20 additions & 0 deletions test/rulesets/Base/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,26 @@
@test Array(y3) == Array(x_23_gpu)[1, [1,1,2]]
@test unthunk(bk3(jl(ones(3)))[2]) == jl([2 1 0; 0 0 0])
end

# https://github.com/JuliaDiff/ChainRules.jl/issues/697
@testset "pulling back mixes AbstractZero and co" begin
_, back = rrule(getindex, [1], 1)
_, gs = back(@not_implemented("test"))
@test unthunk(gs[2]) isa NotImplemented

_, back2 = rrule(getindex, [1], 1)
gs2 = back2([NoTangent()])
@test unthunk(gs2[2]) isa NoTangent

# Above are not realistic since they should be solved by the AD not calling with that
# but more realistic is ended up with a tangent that has a mixture of things
_, back3 = rrule(getindex, [10, 0, -1], :)
gs3 = back3([2.0, NoTangent(), (@not_implemented "test2")])
num, notan, not_imp = unthunk(gs3[2])
@test num isa Real
@test iszero(notan) # We don't care if this gets converted to a 0.0
@test not_imp isa NotImplemented
end
end

@testset "first & tail" begin
Expand Down