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

Make setadbackend more consistent + use ZygoteRules #1401

Merged
merged 2 commits into from
Sep 13, 2020
Merged
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
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