Skip to content

Commit

Permalink
Updated to use ADTypes.jl (#17)
Browse files Browse the repository at this point in the history
* updated to use ADTypes

* compat entry for ADTypes

* fixed tests

* Zygote failing sometimes results in a Zygote.CompileError

* bumped Turing.jl bound in docs
  • Loading branch information
torfjelde committed Feb 27, 2024
1 parent 5a750bf commit 5c27074
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 34 deletions.
7 changes: 5 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
name = "TuringBenchmarking"
uuid = "0db1332d-5c25-4deb-809f-459bc696f94f"
authors = ["Tor Erlend Fjelde <tor.erlend95@gmail.com> and contributors"]
version = "0.3.4"
version = "0.4.0"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
DynamicPPL = "366bfd00-2699-11ea-058f-f148b4cae6d8"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
LogDensityProblems = "6fdf6af0-433a-55f7-b3ed-c6c6e0b8df7c"
LogDensityProblemsAD = "996a588d-648d-4e1f-a8f0-a84b347e47b1"
Expand All @@ -22,6 +24,7 @@ BridgeStan = "c88b6f0a-829e-4b0b-94b7-f06ab5908f5a"
TuringBenchmarkingBridgeStanExt = "BridgeStan"

[compat]
ADTypes = "0.2"
BenchmarkTools = "1"
BridgeStan = "2"
DynamicPPL = "0.23, 0.24"
Expand All @@ -30,7 +33,7 @@ LogDensityProblemsAD = "1"
PrettyTables = "2"
Requires = "1"
ReverseDiff = "1.14"
Turing = "0.29"
Turing = "0.30"
Zygote = "0.6"
julia = "1.6"

Expand Down
2 changes: 1 addition & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Turing = "fce5fe82-541a-59a6-adf8-730c64b5f9a0"

[compat]
Documenter = "1"
Turing = "0.29"
Turing = "0.30"
42 changes: 23 additions & 19 deletions src/TuringBenchmarking.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ using BenchmarkTools
using LogDensityProblems
using LogDensityProblemsAD

using Turing
using ADTypes

using PrettyTables: PrettyTables

using Turing
using Turing.Essential: ForwardDiffAD, TrackerAD, ReverseDiffAD, ZygoteAD
using DynamicPPL: DynamicPPL

# Load some the default backends to trigger conditional loading.
using ForwardDiff: ForwardDiff
using ReverseDiff: ReverseDiff
using Zygote: Zygote

Expand All @@ -23,28 +26,29 @@ export benchmark_model, make_turing_suite, BenchmarkTools, @tagged

# Don't include `TrackerAD` because it's never going to win.
const DEFAULT_ADBACKENDS = [
ForwardDiffAD{Turing.Essential.CHUNKSIZE[]}(), # chunksize=40
ReverseDiffAD{false}(), # rdcache=false
ReverseDiffAD{true}(), # rdcache=false
ZygoteAD(),
AutoForwardDiff(chunksize=0),
AutoReverseDiff(false),
AutoReverseDiff(true),
AutoZygote(),
]

backend_label(::ForwardDiffAD) = "ForwardDiff"
backend_label(::ReverseDiffAD{false}) = "ReverseDiff"
backend_label(::ReverseDiffAD{true}) = "ReverseDiff[compiled]"
backend_label(::ZygoteAD) = "Zygote"
backend_label(::TrackerAD) = "Tracker"
backend_label(::AutoForwardDiff) = "ForwardDiff"
function backend_label(ad::AutoReverseDiff)
"ReverseDiff" * (ad.compile ? " [compiled]" : "")
end
backend_label(::AutoZygote) = "Zygote"
backend_label(::AutoTracker) = "Tracker"

const SYMBOL_TO_BACKEND = Dict(
:forwarddiff => ForwardDiffAD{Turing.Essential.CHUNKSIZE[]}(),
:reversediff => ReverseDiffAD{false}(),
:reversediff_compiled => ReverseDiffAD{true}(),
:zygote => ZygoteAD(),
:tracker => TrackerAD(),
:forwarddiff => AutoForwardDiff(chunksize=0),
:reversediff => AutoReverseDiff(false),
:reversediff_compiled => AutoReverseDiff(true),
:zygote => AutoZygote(),
:tracker => AutoTracker(),
)

to_backend(x) = error("Unknown backend: $x")
to_backend(x::Turing.Essential.ADBackend) = x
to_backend(x::ADTypes.AbstractADType) = x
function to_backend(x::Union{AbstractString,Symbol})
k = Symbol(lowercase(string(x)))
haskey(SYMBOL_TO_BACKEND, k) || error("Unknown backend: $x")
Expand Down Expand Up @@ -106,8 +110,8 @@ end
Create default benchmark suite for `model`.
# Keyword arguments
- `adbackends`: a collection of adbackends to use, specified either as a
`Turing.Essential.ADBackend` or using a `Symbol`. Defaults to `$(DEFAULT_ADBACKENDS)`.
- `adbackends`: a collection of adbackends to use, specified either as a type from
ADTypes.jl or using a `Symbol`. Defaults to `$(DEFAULT_ADBACKENDS)`.
- `run_once=true`: if `true`, the body of each benchmark will be run once to avoid
compilation to be included in the timings (this may occur if compilation runs
longer than the allowed time limit).
Expand Down
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
ReverseDiff = "37e2e3b7-166d-5795-8a7a-e32c996b4267"
Expand Down
25 changes: 13 additions & 12 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ using TuringBenchmarking
using BenchmarkTools
using Turing
using Test
using ADTypes

using Zygote: Zygote
using ReverseDiff: ReverseDiff
Expand Down Expand Up @@ -67,12 +68,12 @@ ADBACKENDS = TuringBenchmarking.DEFAULT_ADBACKENDS
adbackend_string = "$(adbackend)"
results_backend = results[@tagged adbackend_string]
# Each AD backend should have two results.
@test length(leaves(results_backend)) == 2
@test length(BenchmarkTools.leaves(results_backend)) == 2
# It should be under the "gradient" section.
@test haskey(results_backend, "gradient")
# It should have one tagged "linked" and one "standard"
@test length(leaves(results_backend[@tagged "linked"])) == 1
@test length(leaves(results_backend[@tagged "standard"])) == 1
@test length(BenchmarkTools.leaves(results_backend[@tagged "linked"])) == 1
@test length(BenchmarkTools.leaves(results_backend[@tagged "standard"])) == 1
end
end

Expand All @@ -89,12 +90,12 @@ ADBACKENDS = TuringBenchmarking.DEFAULT_ADBACKENDS
adbackend_string = "$(adbackend)"
results_backend = results[@tagged adbackend_string]
# Each AD backend should have two results.
@test length(leaves(results_backend)) == 2
@test length(BenchmarkTools.leaves(results_backend)) == 2
# It should be under the "gradient" section.
@test haskey(results_backend, "gradient")
# It should have one tagged "linked" and one "standard"
@test length(leaves(results_backend[@tagged "linked"])) == 1
@test length(leaves(results_backend[@tagged "standard"])) == 1
@test length(BenchmarkTools.leaves(results_backend[@tagged "linked"])) == 1
@test length(BenchmarkTools.leaves(results_backend[@tagged "standard"])) == 1
end
end
end
Expand All @@ -116,7 +117,7 @@ ADBACKENDS = TuringBenchmarking.DEFAULT_ADBACKENDS
DynamicPPL.SimpleVarInfo(DynamicPPL.OrderedDict(@varname(x) => randn(2))),
]
# Zygote will fail.
@test_throws Union{MethodError,ErrorException} TuringBenchmarking.make_turing_suite(
@test_throws Union{MethodError,ErrorException,Zygote.CompileError} TuringBenchmarking.make_turing_suite(
model;
adbackends=ADBACKENDS,
varinfo=varinfo,
Expand All @@ -134,17 +135,17 @@ ADBACKENDS = TuringBenchmarking.DEFAULT_ADBACKENDS
@testset "$adbackend" for (i, adbackend) in enumerate(ADBACKENDS)
adbackend_string = "$(adbackend)"
results_backend = results[@tagged adbackend_string]
if adbackend isa TuringBenchmarking.ZygoteAD
if adbackend isa AutoZygote
# Zygote.jl should fail, i.e. return an empty suite.
@test length(leaves(results_backend)) == 0
@test length(BenchmarkTools.leaves(results_backend)) == 0
else
# Each AD backend should have two results.
@test length(leaves(results_backend)) == 2
@test length(BenchmarkTools.leaves(results_backend)) == 2
# It should be under the "gradient" section.
@test haskey(results_backend, "gradient")
# It should have one tagged "linked" and one "standard"
@test length(leaves(results_backend[@tagged "linked"])) == 1
@test length(leaves(results_backend[@tagged "standard"])) == 1
@test length(BenchmarkTools.leaves(results_backend[@tagged "linked"])) == 1
@test length(BenchmarkTools.leaves(results_backend[@tagged "standard"])) == 1
end
end
end
Expand Down

2 comments on commit 5c27074

@torfjelde
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 register

Release notes:

Breaking changes

  • Updated to be compatible with Turing.jl v0.30, which uses ADTypes.jl.

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

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 v0.4.0 -m "<description of version>" 5c27074b8ff2343416a3afa19e9b048922a01b02
git push origin v0.4.0

Please sign in to comment.