diff --git a/Project.toml b/Project.toml index 8ca9b5c1d..ff8195039 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "Zygote" uuid = "e88e6eb3-aa80-5325-afca-941959d7151f" -version = "0.6.69" +version = "0.7.0" [deps] AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c" diff --git a/src/compiler/interface.jl b/src/compiler/interface.jl index 80fd9b477..4708c0cc9 100644 --- a/src/compiler/interface.jl +++ b/src/compiler/interface.jl @@ -114,8 +114,14 @@ sensitivity(y::AbstractArray) = error("Output is an array, so the gradient is no sensitivity(y) = error("Output should be scalar; gradients are not defined for output $(repr(y))") # Preserves output as tuple when gradients are collapsed -_project_all(::NTuple{N}, ::Nothing) where {N} = ntuple(_ -> nothing, N) -_project_all(x::Tuple, dx::Tuple) = map(_project, x, dx) +_project_grad(::NTuple{N}, ::Nothing) where {N} = ntuple(_ -> nothing, N) +_project_grad(x::Tuple, dx::Tuple) = map(_project_grad, x, dx) +_project_grad(::Any, ::NoTangent) = nothing +_project_grad(::Any, ::ZeroTangent) = nothing +_project_grad(::Any, ::Nothing) = nothing +_project_grad(::Any, dx::Any) = dx +_project_grad(x::AbstractArray, dx::Tuple) = _project(x, dx) +_project_grad(x::Any, dx::Base.RefValue) = _project(x, dx) """ gradient(f, args...) @@ -146,7 +152,7 @@ julia> gradient([7, 11], 0, 1) do x, y, d function gradient(f, args...) y, back = pullback(f, args...) grad = back(sensitivity(y)) - return _project_all(args, grad) + return _project_grad(args, grad) end # Base.adjoint(f::Function) = x -> gradient(f, x)[1] # piracy! @@ -212,7 +218,7 @@ function withgradient(f, args...) else back(sensitivity(y)) end - results = _project_all(args, grad) + results = _project_grad(args, grad) (val=y, grad=results) end @@ -473,7 +479,7 @@ function pullback(f, ps::Params) end # No conversion required here -_project_all(_, dx::Grads) = dx +_project_grad(_, dx::Grads) = dx # Code Reflection diff --git a/test/gradcheck.jl b/test/gradcheck.jl index 8cb7e6e1a..5be70b1c6 100644 --- a/test/gradcheck.jl +++ b/test/gradcheck.jl @@ -2125,3 +2125,9 @@ end @test gradient(x -> @.(x * x * x), 2.0) == gradient(x -> x * (x * x), 2.0) @test gradient(x -> @.(3.0*x*2.0*x), 2.0) == gradient(x -> 6(x^2), 2.0) end + +@testset "Sparse input" begin + g1 = Zygote.gradient(sum, zeros(1,1))[1] + g2 = Zygote.gradient(sum, spzeros(1,1))[1] + @test g1 == g2 +end