Skip to content

Commit

Permalink
New Zygote context in every call to AD.pullback_function (#77)
Browse files Browse the repository at this point in the history
* New Zygote context in every call to `AD.pullback_function`

* Make fix more modular
  • Loading branch information
devmotion authored Mar 3, 2023
1 parent 19ce815 commit 3218c08
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "AbstractDifferentiation"
uuid = "c29ec348-61ec-40c8-8164-b8c60e9d9f3d"
authors = ["Mohamed Tarek <mohamed82008@gmail.com> and contributors"]
version = "0.5.0"
version = "0.5.1"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
2 changes: 1 addition & 1 deletion ext/AbstractDifferentiationChainRulesCoreExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import AbstractDifferentiation as AD
using ChainRulesCore: ChainRulesCore

AD.@primitive function pullback_function(ba::AD.ReverseRuleConfigBackend, f, xs...)
_, back = ChainRulesCore.rrule_via_ad(ba.ruleconfig, f, xs...)
_, back = ChainRulesCore.rrule_via_ad(AD.ruleconfig(ba), f, xs...)
pullback(vs) = Base.tail(back(vs))
pullback(vs::Tuple{Any}) = Base.tail(back(first(vs)))
return pullback
Expand Down
5 changes: 5 additions & 0 deletions ext/AbstractDifferentiationZygoteExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ end

AD.ZygoteBackend() = AD.ReverseRuleConfigBackend(Zygote.ZygoteRuleConfig())

# Context should not persist between different AD calls: fixes #69
function AD.ruleconfig(::AD.ReverseRuleConfigBackend{<:Zygote.ZygoteRuleConfig})
return Zygote.ZygoteRuleConfig()
end

end # module
5 changes: 5 additions & 0 deletions src/backends.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ struct ReverseRuleConfigBackend{RC} <: AbstractReverseMode
ruleconfig::RC
end

# internal function for extracting the rule config
# falls back to returning the wrapped `ruleconfig` but can be specialized
# e.g., for Zygote to fix #69
ruleconfig(ba::ReverseRuleConfigBackend) = ba.ruleconfig

"""
ZygoteBackend()
Expand Down
22 changes: 22 additions & 0 deletions test/ruleconfig.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,26 @@ using Zygote
test_lazy_jacobians(backend)
end
end

# issue #69
@testset "Zygote context" begin
ad = AD.ZygoteBackend()

# example in #69: context is not mutated
@test ad.ruleconfig.context.cache === nothing
@test AD.derivative(ad, exp, 1.0) === (exp(1.0),)
@test ad.ruleconfig.context.cache === nothing
@test AD.derivative(ad, exp, 1.0) === (exp(1.0),)
@test ad.ruleconfig.context.cache === nothing

# Jacobian computation still works
# https://github.com/JuliaDiff/AbstractDifferentiation.jl/pull/70#issuecomment-1449481724
function f(x, a)
r = Ref(x)
r[] = r[] + r[]
r[] = r[] * a
r[]
end
@test AD.jacobian(ad, f, [1, 2, 3], 3) == ([6.0 0.0 0.0; 0.0 6.0 0.0; 0.0 0.0 6.0], [2.0, 4.0, 6.0])
end
end

2 comments on commit 3218c08

@devmotion
Copy link
Member Author

Choose a reason for hiding this comment

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

@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/78913

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 v0.5.1 -m "<description of version>" 3218c0866d027fe795a84a20b51759eeeb6770ed
git push origin v0.5.1

Please sign in to comment.