Skip to content

Commit

Permalink
fix: revert catch of nothing pullbacks with Zygote (#714)
Browse files Browse the repository at this point in the history
  • Loading branch information
gdalle authored Feb 1, 2025
1 parent 19803d1 commit dccd07e
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 34 deletions.
2 changes: 1 addition & 1 deletion DifferentiationInterface/Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "DifferentiationInterface"
uuid = "a0c0ee7d-e4b9-4e03-894e-1c5f64a51d63"
authors = ["Guillaume Dalle", "Adrian Hill"]
version = "0.6.38"
version = "0.6.39"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,6 @@ using Zygote:
withgradient,
withjacobian

struct ZygoteNothingError <: Exception
f
x
contexts
end

function Base.showerror(io::IO, e::ZygoteNothingError)
(; f, x, contexts) = e
sig = (typeof(x), map(typeof DI.unwrap, contexts)...)
return print(
io,
"Zygote failed to differentiate function `$f` with argument types `$sig` (the pullback returned `nothing`).",
)
end

check_nothing(::Nothing, f, x, contexts) = throw(ZygoteNothingError(f, x, contexts))
check_nothing(::Any, f, x, contexts) = nothing

DI.check_available(::AutoZygote) = true
DI.inplace_support(::AutoZygote) = DI.InPlaceNotSupported()

Expand Down Expand Up @@ -64,7 +46,6 @@ function DI.value_and_pullback(
tx = map(ty) do dy
first(pb(dy))
end
check_nothing(first(tx), f, x, contexts)
return y, tx
end

Expand All @@ -80,7 +61,6 @@ function DI.value_and_pullback(
tx = map(ty) do dy
first(pb(dy))
end
check_nothing(first(tx), f, x, contexts)
return copy(y), tx
end

Expand All @@ -96,7 +76,6 @@ function DI.pullback(
tx = map(ty) do dy
first(pb(dy))
end
check_nothing(first(tx), f, x, contexts)
return tx
end

Expand All @@ -110,15 +89,13 @@ function DI.value_and_gradient(
f, ::DI.NoGradientPrep, ::AutoZygote, x, contexts::Vararg{DI.Context,C}
) where {C}
(; val, grad) = withgradient(f, x, map(translate, contexts)...)
check_nothing(first(grad), f, x, contexts)
return val, first(grad)
end

function DI.gradient(
f, ::DI.NoGradientPrep, ::AutoZygote, x, contexts::Vararg{DI.Context,C}
) where {C}
grad = gradient(f, x, map(translate, contexts)...)
check_nothing(first(grad), f, x, contexts)
return first(grad)
end

Expand Down Expand Up @@ -147,15 +124,13 @@ function DI.value_and_jacobian(
y = f(x, map(translate, contexts)...)
# https://github.com/FluxML/Zygote.jl/issues/1506
jac = jacobian(f, x, map(translate, contexts)...)
check_nothing(first(jac), f, x, contexts)
return y, first(jac)
end

function DI.jacobian(
f, ::DI.NoJacobianPrep, ::AutoZygote, x, contexts::Vararg{DI.Context,C}
) where {C}
jac = jacobian(f, x, map(translate, contexts)...)
check_nothing(first(jac), f, x, contexts)
return first(jac)
end

Expand Down Expand Up @@ -242,7 +217,6 @@ function DI.hessian(
) where {C}
fc = DI.with_contexts(f, contexts...)
hess = hessian(fc, x)
check_nothing(hess, f, x, contexts)
return hess
end

Expand Down
7 changes: 0 additions & 7 deletions DifferentiationInterface/test/Back/Zygote/test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,3 @@ test_differentiation(
logging=LOGGING,
)
end

## Errors

@testset "Errors" begin
safe_log(x) = x > zero(x) ? log(x) : convert(typeof(x), NaN)
@test_throws "Zygote failed to differentiate" derivative(safe_log, AutoZygote(), 0.0)
end

2 comments on commit dccd07e

@gdalle
Copy link
Member Author

@gdalle gdalle commented on dccd07e Feb 1, 2025

Choose a reason for hiding this comment

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

@JuliaRegistrator register subdir=DifferentiationInterface

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/124177

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a DifferentiationInterface-v0.6.39 -m "<description of version>" dccd07ecc240f515262ea3c0ced13a1e4006fb4b
git push origin DifferentiationInterface-v0.6.39

Please sign in to comment.