Skip to content

Commit de07109

Browse files
committed
fix incorrect getindex gradient
1 parent 9334ddf commit de07109

File tree

2 files changed

+4
-18
lines changed

2 files changed

+4
-18
lines changed

src/stage1/generated.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ function (::∂⃖{N})(::typeof(Base.getindex), a::Array{<:Number}, inds...) whe
320320
(@Base.constprop :aggressive Δ->begin
321321
Δ isa AbstractZero && return (NoTangent(), Δ, map(Returns(Δ), inds)...)
322322
BB = zero(a)
323-
BB[inds...] = unthunk(Δ)
323+
view(BB, inds...) .+= unthunk(Δ)
324324
(NoTangent(), BB, map(x->NoTangent(), inds)...)
325325
end),
326326
(@Base.constprop :aggressive (_, Δ, _)->begin

test/gradcheck.jl

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -144,29 +144,15 @@ end
144144

145145
imat = [1 2; 3 4]
146146
@test jacobicheck(x -> x[:, imat], (3, 4))
147-
148-
# incorrect gradient
149-
# julia> gradient(sum ∘ x->x[:,[1,2,2]], x)[1]
150-
# 3×4 Matrix{Float64}:
151-
# 1.0 1.0 0.0 0.0
152-
# 1.0 1.0 0.0 0.0
153-
# 1.0 1.0 0.0 0.0
154-
# while it should be
155-
# 3×4 Matrix{Float64}:
156-
# 1.0 2.0 0.0 0.0
157-
# 1.0 2.0 0.0 0.0
158-
# 1.0 2.0 0.0 0.0
159-
@test_broken jacobicheck(x -> x[:, [1, 2, 2]], (3, 4))
160-
# same here
147+
@test jacobicheck(x -> x[:, [1, 2, 2]], (3, 4))
161148
irep = [1 2; 2 2]
162-
@test_broken jacobicheck(x -> x[1, irep], (3, 4))
149+
@test jacobicheck(x -> x[1, irep], (3, 4))
163150

164151
# https://github.com/invenia/Nabla.jl/issues/139
165152
x = rand(3)
166153
z = [1, 2, 3, 3]
167154
y139(x, z) = dot(ones(4), x[z])
168-
# wrong gradient: Evaluated: ([1.0 0.0 0.0; 1.0 0.0 0.0; 2.0 0.0 0.0], NoTangent()) == ([1, 1, 2], NoTangent())
169-
@test_broken gradient(y139, x, z) == ([1, 1, 2], NoTangent())
155+
@test gradient(y139, x, z) == ([1, 1, 2], NoTangent())
170156

171157
# https://github.com/FluxML/Zygote.jl/issues/376
172158

0 commit comments

Comments
 (0)