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

Only project sentinels in gradient #1508

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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 Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
16 changes: 11 additions & 5 deletions src/compiler/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down Expand Up @@ -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!
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions test/gradcheck.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading