Skip to content

Commit

Permalink
Make setadbackend more consistent + use ZygoteRules (#1401)
Browse files Browse the repository at this point in the history
  • Loading branch information
devmotion authored Sep 13, 2020
1 parent 309eeb1 commit 18cdd35
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 53 deletions.
6 changes: 4 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Turing"
uuid = "fce5fe82-541a-59a6-adf8-730c64b5f9a0"
version = "0.14.1"
version = "0.14.2"

[deps]
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
Expand Down Expand Up @@ -28,6 +28,7 @@ Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
StatsFuns = "4c63d2b9-4356-54db-8cca-17b64c39e42c"
Tracker = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c"
ZygoteRules = "700de1a5-db45-46bc-99cf-38207098b444"

[compat]
AbstractMCMC = "1"
Expand All @@ -38,7 +39,7 @@ Bijectors = "0.8"
Distributions = "0.23.3"
DistributionsAD = "0.6"
DocStringExtensions = "0.8"
DynamicPPL = "0.9"
DynamicPPL = "0.9.1"
EllipticalSliceSampling = "0.2, 0.3"
ForwardDiff = "0.10.3"
Libtask = "0.4"
Expand All @@ -51,4 +52,5 @@ SpecialFunctions = "0.7.2, 0.8, 0.9, 0.10"
StatsBase = "0.32, 0.33"
StatsFuns = "0.8, 0.9"
Tracker = "0.2.3"
ZygoteRules = "0.2"
julia = "1.3"
9 changes: 5 additions & 4 deletions src/core/Core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ using StatsFuns: logsumexp, softmax
@reexport using DynamicPPL
using Requires

import ZygoteRules

include("container.jl")
include("ad.jl")
include("deprecations.jl")

function __init__()
@require Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f" begin
include("compat/zygote.jl")
export ZygoteAD
end
@require ReverseDiff = "37e2e3b7-166d-5795-8a7a-e32c996b4267" begin
include("compat/reversediff.jl")
export ReverseDiffAD, getrdcache, setrdcache, emptyrdcache
Expand Down Expand Up @@ -51,6 +51,7 @@ export @model,
setadsafe,
ForwardDiffAD,
TrackerAD,
ZygoteAD,
value,
gradient_logp,
CHUNKSIZE,
Expand Down
52 changes: 37 additions & 15 deletions src/core/ad.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,23 @@
# Global variables/constants #
##############################
const ADBACKEND = Ref(:forwarddiff)
function setadbackend(backend_sym::Symbol)
setadbackend(Val(backend_sym))
AdvancedVI.setadbackend(Val(backend_sym))
Bijectors.setadbackend(Val(backend_sym))
setadbackend(backend_sym::Symbol) = setadbackend(Val(backend_sym))
function setadbackend(backend::Val)
_setadbackend(backend)
AdvancedVI.setadbackend(backend)
Bijectors.setadbackend(backend)
end
function setadbackend(::Val{:forward_diff})
Base.depwarn("`Turing.setadbackend(:forward_diff)` is deprecated. Please use `Turing.setadbackend(:forwarddiff)` to use `ForwardDiff`.", :setadbackend)
setadbackend(Val(:forwarddiff))
end
function setadbackend(::Val{:forwarddiff})

function _setadbackend(::Val{:forwarddiff})
CHUNKSIZE[] == 0 && setchunksize(40)
ADBACKEND[] = :forwarddiff
end

function setadbackend(::Val{:reverse_diff})
Base.depwarn("`Turing.setadbackend(:reverse_diff)` is deprecated. Please use `Turing.setadbackend(:tracker)` to use `Tracker` or `Turing.setadbackend(:reversediff)` to use `ReverseDiff`. To use `ReverseDiff`, please make sure it is loaded separately with `using ReverseDiff`.", :setadbackend)
setadbackend(Val(:tracker))
end
function setadbackend(::Val{:tracker})
function _setadbackend(::Val{:tracker})
ADBACKEND[] = :tracker
end
function _setadbackend(::Val{:zygote})
ADBACKEND[] = :zygote
end

const ADSAFE = Ref(false)
function setadsafe(switch::Bool)
Expand All @@ -46,12 +42,14 @@ getchunksize(::Type{<:Sampler{Talg}}) where Talg = getchunksize(Talg)
getchunksize(::Type{SampleFromPrior}) = CHUNKSIZE[]

struct TrackerAD <: ADBackend end
struct ZygoteAD <: ADBackend end

ADBackend() = ADBackend(ADBACKEND[])
ADBackend(T::Symbol) = ADBackend(Val(T))

ADBackend(::Val{:forwarddiff}) = ForwardDiffAD{CHUNKSIZE[]}
ADBackend(::Val{:tracker}) = TrackerAD
ADBackend(::Val{:zygote}) = ZygoteAD
ADBackend(::Val) = error("The requested AD backend is not available. Make sure to load all required packages.")

"""
Expand Down Expand Up @@ -149,6 +147,30 @@ function gradient_logp(
return l, ∂l∂θ
end

function gradient_logp(
backend::ZygoteAD,
θ::AbstractVector{<:Real},
vi::VarInfo,
model::Model,
sampler::AbstractSampler = SampleFromPrior(),
context::DynamicPPL.AbstractContext = DynamicPPL.DefaultContext()
)
T = typeof(getlogp(vi))

# Specify objective function.
function f(θ)
new_vi = VarInfo(vi, sampler, θ)
model(new_vi, sampler)
return getlogp(new_vi)
end

# Compute forward and reverse passes.
l::T, ȳ = ZygoteRules.pullback(f, θ)
∂l∂θ::typeof(θ) = (1)[1]

return l, ∂l∂θ
end

function verifygrad(grad::AbstractVector{<:Real})
if any(isnan, grad) || any(isinf, grad)
@warn("Numerical error in gradients. Rejecting current proposal...")
Expand Down
2 changes: 1 addition & 1 deletion src/core/compat/reversediff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function emptyrdcache end

getrdcache() = RDCache[]
ADBackend(::Val{:reversediff}) = ReverseDiffAD{getrdcache()}
function setadbackend(::Val{:reversediff})
function _setadbackend(::Val{:reversediff})
ADBACKEND[] = :reversediff
end

Expand Down
31 changes: 0 additions & 31 deletions src/core/compat/zygote.jl

This file was deleted.

9 changes: 9 additions & 0 deletions src/core/deprecations.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function setadbackend(::Val{:forward_diff})
Base.depwarn("`Turing.setadbackend(:forward_diff)` is deprecated. Please use `Turing.setadbackend(:forwarddiff)` to use `ForwardDiff`.", :setadbackend)
setadbackend(Val(:forwarddiff))
end

function setadbackend(::Val{:reverse_diff})
Base.depwarn("`Turing.setadbackend(:reverse_diff)` is deprecated. Please use `Turing.setadbackend(:tracker)` to use `Tracker` or `Turing.setadbackend(:reversediff)` to use `ReverseDiff`. To use `ReverseDiff`, please make sure it is loaded separately with `using ReverseDiff`.", :setadbackend)
setadbackend(Val(:tracker))
end

2 comments on commit 18cdd35

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

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.14.2 -m "<description of version>" 18cdd35caaccf03e53cd8143829f1c7767621019
git push origin v0.14.2

Please sign in to comment.