Skip to content

Commit

Permalink
fix nothing in mutable struct getindex/getfield
Browse files Browse the repository at this point in the history
  • Loading branch information
mcabbott committed Jul 30, 2022
1 parent 5ffbd43 commit 927ee27
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/lib/lib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ using Base: tail
@adjoint function literal_getindex(xs::NTuple{N,Any}, ::Val{i}) where {N,i}
val = xs[i]
function back(Δ)
accum_param(__context__, val, Δ) === nothing && return
accum_param(__context__, val, Δ)
return ntuple(j -> i == j ? Δ : nothing, Val(N)), nothing
end
val, back
Expand All @@ -116,7 +116,7 @@ end
@adjoint function getindex(xs::NTuple{N,Any}, i::Integer) where N
val = xs[i]
function back(Δ)
accum_param(__context__, val, Δ) === nothing && return
accum_param(__context__, val, Δ)
return ntuple(j -> i == j ? Δ : nothing, Val(N)), nothing
end
return val, back
Expand Down Expand Up @@ -228,8 +228,8 @@ end

@adjoint function literal_getfield(x, ::Val{f}) where f
val = getfield(x, f)
function back(Δ)
accum_param(__context__, val, Δ) === nothing && return
function literal_getfield_back(Δ)
accum_param(__context__, val, Δ)
if isimmutable(x)
dx = (; nt_nothing(x)..., pair(Val(f), Δ, x)...)
(_project(x, dx), nothing)
Expand All @@ -239,7 +239,7 @@ end
return (dx,nothing)
end
end
unwrap(val), back
unwrap(val), literal_getfield_back
end

_pullback(cx::AContext, ::typeof(getfield), x, field_name::Symbol) =
Expand Down
57 changes: 56 additions & 1 deletion test/features.jl
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ end
@test_broken gradient(x -> abs2(x[1].x) + 7 * x[1].x.re, [Ref(1+im)]) == ([(x = 9.0 + 2.0im,)],)
@test_broken gradient(x -> abs2(x[1].x) + 7 * real(x[1].x), [Ref(1+im)]) == ([(x = 9.0 + 2.0im,)],) # worked on 0.6.0, 0.6.20

@test_broken gradient(x -> abs2(x[].x) + 7 * real(x[].x), Ref(Ref(1+im))) == ((x = 9.0 + 2.0im,),) # gives nothing, same in 0.6.0
@test gradient(x -> abs2(x[].x) + 7 * real(x[].x), Ref(Ref(1+im))) == ((x = (x = 9.0 + 2.0im,),),) # gave `nothing` from 0.6.0 to 0.6.41

# Array of mutables:
@test gradient(x -> sum(getindex.(x).^2), Ref.(1:3))[1] == [(;x=2i) for i in 1:3]
Expand All @@ -490,6 +490,61 @@ end
@test gradient(x -> sum(sum, Ref(x) .* [1,2,3]), [4,5]) == ([6.0, 6.0],)
end

@testset "mutable accum_param bugs" begin

mutable struct Mut{T}; x::T; end
struct Imm{T}; x::T; end

# Indexing a tuple containing a mutable struct gave `nothing`
x1 = (Mut(3.0),)
x2 = (Imm(3.0),)
x3 = (Ref(3.0),)
@test gradient(x -> x[1].x^2, x1)[1] == ((x = 6.0,),) # fails on v0.6.0 v0.6.41
@test gradient(x -> x[1].x^2, x2)[1] == ((x = 6.0,),)
@test gradient(x -> x[1].x^2, x3)[1] == ((x = 6.0,),) # fails on v0.6.0 v0.6.41
i1 = 1
@test gradient(x -> x[i1].x^2, x1)[1] == ((x = 6.0,),) # fails on v0.6.0 v0.6.41
@test gradient(x -> x[i1].x^2, x2)[1] == ((x = 6.0,),)
@test gradient(x -> x[i1].x^2, x3)[1] == ((x = 6.0,),) # fails on v0.6.0 v0.6.41

@test gradient(x -> x[1][1].x^2, [x1])[1] == [((x = 6.0,),)] # fails on v0.6.0 v0.6.41
@test gradient(x -> x[1][1].x^2, [x2])[1] == [((x = 6.0,),)]
@test gradient(x -> x[1][1].x^2, [x3])[1] == [((x = 6.0,),)] # fails on v0.6.0 v0.6.41

# When `getfield` returns a mutable struct, it gave `nothing`:
x4 = Imm(Mut(4.0))
x5 = Mut(Mut(4.0))
x6 = Imm(Imm(4.0))
@test gradient(x -> x.x.x^3, x4)[1] == (x = (x = 48.0,),) # fails on v0.6.0 v0.6.41
@test gradient(x -> x.x.x^3, x5)[1] == (x = (x = 48.0,),) # fails on v0.6.0
@test gradient(x -> x.x.x^3, x6)[1] == (x = (x = 48.0,),) # fails on v0.6.41

@test gradient(x -> x[2].x.x^3, [x4, x4])[1] == [nothing, (x = (x = 48.0,),)] # fails on v0.6.0 v0.6.41
@test gradient(x -> x[2].x.x^3, [x4, x5])[1] == [nothing, (x = (x = 48.0,),)] # fails on v0.6.0
@test gradient(x -> x[2].x.x^3, [x4, x6])[1] == [nothing, (x = (x = 48.0,),)] # fails on v0.6.41

# Check when using implicit parameters, Params cases used to pass:
y1 = [3.0]
y2 = (Mut(y1),)
y3 = (Imm(y1),)
@test gradient(x -> sum(x[1].x)^2, y2)[1] == ((x = [6.0],),) # fails on v0.6.0 v0.6.41
@test gradient(() -> sum(y2[1].x)^2, Params([y1]))[y1] == [6.0]
@test gradient(x -> sum(x[1].x)^2, y3)[1] == ((x = [6.0],),)
@test gradient(() -> sum(y3[1].x)^2, Params([y1]))[y1] == [6.0]

@test gradient(x -> sum(x[1].x .+ x[1].x)^3, y2)[1] == ((x = [216.0],),) # fails on v0.6.0 v0.6.41
@test gradient(() -> sum(y2[1].x .+ y2[1].x)^3, Params([y1]))[y1] == [216.0]
@test gradient(x -> sum(x[1].x .+ x[1].x)^3, y3)[1] == ((x = [216.0],),)
@test gradient(() -> sum(y3[1].x .+ y3[1].x)^3, Params([y1]))[y1] == [216.0]

i1 = 1
@test gradient(x -> sum(x[i1].x .+ x[1].x)^3, y2)[1] == ((x = [216.0],),) # fails on v0.6.0 v0.6.41
@test gradient(() -> sum(y2[i1].x .+ y2[1].x)^3, Params([y1]))[y1] == [216.0]
@test gradient(x -> sum(x[i1].x .+ x[1].x)^3, y3)[1] == ((x = [216.0],),)
@test gradient(() -> sum(y3[i1].x .+ y3[1].x)^3, Params([y1]))[y1] == [216.0]

end

@testset "NamedTuples" begin
@test gradient(x -> x.a, (a=1, b=2)) == ((a = 1, b = nothing),)
@test gradient(x -> x[1].a, [(a=1, b=2)]) == ([(a = 1, b = nothing)],)
Expand Down

0 comments on commit 927ee27

Please sign in to comment.