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

Extra fixes for ChainRulesCore @1.0 #1039

Merged
merged 7 commits into from
Jul 23, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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 Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ ZygoteRules = "700de1a5-db45-46bc-99cf-38207098b444"
[compat]
AbstractFFTs = "0.5, 1.0"
ChainRules = "0.8.12"
ChainRulesCore = "0.10.4"
ChainRulesCore = "1"
ChainRulesTestUtils = "0.7.1"
DiffRules = "1.0"
FillArrays = "0.8, 0.9, 0.10, 0.11, 0.12"
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/chainrules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ for T_outer in (:Tuple, :NamedTuple)
# than happy.
@eval @inline function wrap_chainrules_output(x::ChainRules.Tangent{P, T}) where {P, T<:$T_outer}
xp = map(wrap_chainrules_output, canonicalize(x))
convert($T_outer, xp)
ChainRulesCore.backing(xp)
oxinabox marked this conversation as resolved.
Show resolved Hide resolved
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ using Zygote, Test, LinearAlgebra
@test gradient(x -> real(logabsdet(x)[1]), [1 2im; 3im 4])[1] ≈ [4 3im; 2im 1]/10

# https://github.com/FluxML/Zygote.jl/issues/705
@test gradient(x -> imag(sum(exp, x)), [1,2,3])[1] ≈ im .* exp.(1:3)
@test gradient(x -> imag(sum(exp, x)), [1,2,3])[1] ≈ real(im .* exp.(1:3))
Copy link
Member

Choose a reason for hiding this comment

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

I love it

@test gradient(x -> imag(sum(exp, x)), [1+0im,2,3])[1] ≈ im .* exp.(1:3)

fs_C_to_R = (real,
Expand Down
4 changes: 2 additions & 2 deletions test/features.jl
Original file line number Diff line number Diff line change
Expand Up @@ -449,12 +449,12 @@ end
@test pullback(type_test)[1] == Complex{<:Real}

@testset "Pairs" begin
@test (x->10*pairs((a=x, b=2))[1])'(100) === 10
@test (x->10*pairs((a=x, b=2))[1])'(100) === 10.0
Copy link
Member

Choose a reason for hiding this comment

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

I am pretty sure I love it.

@test (x->10*pairs((a=x, b=2))[2])'(100) === 0
foo(;kw...) = 1
@test gradient(() -> foo(a=1,b=2.0)) === ()

@test (x->10*(x => 2)[1])'(100) === 10
@test (x->10*(x => 2)[1])'(100) === 10.0
@test (x->10*(x => 2)[2])'(100) === 0
end

Expand Down
4 changes: 2 additions & 2 deletions test/gradcheck.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ end
@test gradient(xs ->sum(xs .^ _pow), [4, -1]) == ([_pow*4^9, -10],)

@test gradient(x -> real((1+3im) * x^2), 5+7im) == (-32 - 44im,)
@test gradient(p -> real((1+3im) * (5+7im)^p), 2)[1] ≈ (-234 + 2im)*log(5 - 7im)
@test gradient(p -> real((1+3im) * (5+7im)^p), 2)[1] ≈ real((-234 + 2im)*log(5 - 7im))
# D[(1+3I)x^p, p] /. {x->5+7I, p->2} // Conjugate
end

Expand Down Expand Up @@ -160,7 +160,7 @@ end

# https://github.com/FluxML/Zygote.jl/issues/376
_, back = Zygote._pullback(x->x[1]*im, randn(2))
@test back(1.0)[2] == [-im, 0]
@test back(1.0)[2] == real([-im, 0])
oxinabox marked this conversation as resolved.
Show resolved Hide resolved

# _droplike
@test gradient(x -> sum(inv, x[1, :]'), ones(2, 2)) == ([-1 -1; 0 0],)
Expand Down
13 changes: 9 additions & 4 deletions test/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ end

xs, y = randn(2,3), rand()
f34(xs, y) = xs[1] * (sum(xs .^ (1:3)') + y^4) # non-diagonal Hessian, two arguments
dx, dy = diaghessian(f34, xs, y)
@test size(dx) == size(xs)
@test vec(dx) ≈ diag(hessian(x -> f34(x,y), xs))
@test dy ≈ hessian(y -> f34(xs,y), y)

function broken()
oxinabox marked this conversation as resolved.
Show resolved Hide resolved
dx, dy = diaghessian(f34, xs, y) # This fails becase ProjectTo can't project a Dual onto a Float
c1 = size(dx) == size(xs)
c2 = vec(dx) ≈ diag(hessian(x -> f34(x,y), xs))
c3 = dy ≈ hessian(y -> f34(xs,y), y)
return all([c1, c2, c3])
end
@test_broken broken()

zs = randn(7,13) # test chunk mode
@test length(zs) > ForwardDiff.DEFAULT_CHUNK_THRESHOLD
Expand Down