Skip to content

Commit

Permalink
Merge pull request #79 from tpapp/tp/benchmarktools-requires
Browse files Browse the repository at this point in the history
Load BenchmarkTools conditionally, fix deps versions.
  • Loading branch information
tpapp committed Jul 23, 2021
2 parents 531ecf4 + fd33424 commit 507d358
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 50 deletions.
10 changes: 5 additions & 5 deletions Project.toml
@@ -1,11 +1,10 @@
name = "LogDensityProblems"
uuid = "6fdf6af0-433a-55f7-b3ed-c6c6e0b8df7c"
authors = ["Tamas K. Papp <tkpapp@gmail.com>"]
version = "0.10.6"
version = "0.11.0"

[deps]
ArgCheck = "dce04be8-c92d-5529-be00-80e4d2c0e197"
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
DiffResults = "163ba53b-c6d8-5494-b064-1a9d43ac40c5"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Expand All @@ -15,7 +14,7 @@ UnPack = "3a884ed6-31ef-47d7-9d2a-63182c4928ed"

[compat]
ArgCheck = "1, 2"
BenchmarkTools = "0.4, 0.5, 0.6, 0.7"
BenchmarkTools = "1"
DiffResults = "0.0, 1"
DocStringExtensions = "0.8"
Requires = "0.5, 1"
Expand All @@ -24,15 +23,16 @@ UnPack = "0.1, 1"
julia = "1"

[extras]
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
ReverseDiff = "37e2e3b7-166d-5795-8a7a-e32c996b4267"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
StatsFuns = "4c63d2b9-4356-54db-8cca-17b64c39e42c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Tracker = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
ReverseDiff = "37e2e3b7-166d-5795-8a7a-e32c996b4267"

[targets]
test = ["Distributions", "Documenter", "ForwardDiff", "Test", "StatsBase", "StatsFuns", "Tracker", "Zygote", "ReverseDiff"]
test = ["BenchmarkTools", "Distributions", "Documenter", "ForwardDiff", "Test", "StatsBase", "StatsFuns", "Tracker", "Zygote", "ReverseDiff"]
1 change: 1 addition & 0 deletions docs/Project.toml
@@ -1,4 +1,5 @@
[deps]
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
Tracker = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c"
Expand Down
2 changes: 1 addition & 1 deletion docs/make.jl
@@ -1,4 +1,4 @@
using Documenter, LogDensityProblems, ForwardDiff, Tracker, Zygote
using Documenter, LogDensityProblems, ForwardDiff, Tracker, Zygote, BenchmarkTools

makedocs(
sitename = "LogDensityProblems.jl",
Expand Down
42 changes: 0 additions & 42 deletions src/AD_ForwardDiff.jl
Expand Up @@ -3,7 +3,6 @@
#####

import .ForwardDiff
using BenchmarkTools: @belapsed

struct ForwardDiffLogDensity{L, C} <: ADGradientWrapper
::L
Expand Down Expand Up @@ -51,44 +50,3 @@ function logdensity_and_gradient(fℓ::ForwardDiffLogDensity, x::AbstractVector)
result = ForwardDiff.gradient!(buffer, _logdensity_closure(ℓ), x, gradientconfig)
_diffresults_extract(result)
end

"""
$(SIGNATURES)
Default chunk sizes to try for benchmarking. Fewer than `M`, always contains `1` and `N`.
"""
function heuristic_chunks(N, M = 20)
step = max(N ÷ M, 1)
Ns = 1:step:N
if N Ns
Ns = vcat(Ns, N)
end
Ns
end

"""
$(SIGNATURES)
Benchmark a log density problem with various chunk sizes using ForwardDiff.
`chunks`, which defaults to all possible chunk sizes, determines the chunks that are tried.
The function returns `chunk => time` pairs, where `time` is the benchmarked runtime in
seconds, as determined by `BenchmarkTools.@belapsed`. The gradient is evaluated at `x`
(defaults to zeros).
*Runtime may be long* because of tuned benchmarks, so when `markprogress == true` (the
default), dots are printed to mark progress.
This function is not exported, but part of the API when `ForwardDiff` is imported.
"""
function benchmark_ForwardDiff_chunks(ℓ;
chunks = heuristic_chunks(dimension(ℓ), 20),
markprogress = true,
x = zeros(dimension(ℓ)))
map(chunks) do chunk
∇ℓ = ADgradient(Val(:ForwardDiff), ℓ; chunk = ForwardDiff.Chunk(chunk))
markprogress && print(".")
chunk => @belapsed logdensity_and_gradient($(∇ℓ), $(x))
end
end
45 changes: 45 additions & 0 deletions src/ForwardDiff_benchmarking.jl
@@ -0,0 +1,45 @@
using .BenchmarkTools: @belapsed
using .ForwardDiff

"""
$(SIGNATURES)
Default chunk sizes to try for benchmarking. Fewer than `M`, always contains `1` and `N`.
"""
function heuristic_chunks(N, M = 20)
step = max(N ÷ M, 1)
Ns = 1:step:N
if N Ns
Ns = vcat(Ns, N)
end
Ns
end

"""
$(SIGNATURES)
Benchmark a log density problem with various chunk sizes using ForwardDiff.
`chunks`, which defaults to all possible chunk sizes, determines the chunks that are tried.
The function returns `chunk => time` pairs, where `time` is the benchmarked runtime in
seconds, as determined by `BenchmarkTools.@belapsed`. The gradient is evaluated at `x`
(defaults to zeros).
*Runtime may be long* because of tuned benchmarks, so when `markprogress == true` (the
default), dots are printed to mark progress.
This function is not exported, but part of the API.
*It is loaded conditionally when both `ForwardDiff` and `BenchmarkTools` are loaded.*
"""
function benchmark_ForwardDiff_chunks(ℓ;
chunks = heuristic_chunks(dimension(ℓ), 20),
markprogress = true,
x = zeros(dimension(ℓ)))
map(chunks) do chunk
∇ℓ = ADgradient(Val(:ForwardDiff), ℓ; chunk = ForwardDiff.Chunk(chunk))
markprogress && print(".")
chunk => @belapsed logdensity_and_gradient($(∇ℓ), $(x))
end
end
8 changes: 7 additions & 1 deletion src/LogDensityProblems.jl
Expand Up @@ -231,7 +231,13 @@ end

function __init__()
@require DiffResults="163ba53b-c6d8-5494-b064-1a9d43ac40c5" include("DiffResults_helpers.jl")
@require ForwardDiff="f6369f11-7733-5829-9624-2563aa707210" include("AD_ForwardDiff.jl")
@require ForwardDiff="f6369f11-7733-5829-9624-2563aa707210" begin
include("AD_ForwardDiff.jl")
@require BenchmarkTools="6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" begin
include("ForwardDiff_benchmarking.jl")
end

end
@require Tracker="9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c" include("AD_Tracker.jl")
@require Zygote="e88e6eb3-aa80-5325-afca-941959d7151f" include("AD_Zygote.jl")
@require ReverseDiff="37e2e3b7-166d-5795-8a7a-e32c996b4267" include("AD_ReverseDiff.jl")
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
@@ -1,4 +1,4 @@
using LogDensityProblems, Test, Distributions, TransformVariables
using LogDensityProblems, Test, Distributions, TransformVariables, BenchmarkTools
import LogDensityProblems: capabilities, dimension, logdensity
using LogDensityProblems: logdensity_and_gradient, LogDensityOrder

Expand Down

2 comments on commit 507d358

@tpapp
Copy link
Owner Author

@tpapp tpapp commented on 507d358 Jul 23, 2021

Choose a reason for hiding this comment

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

@JuliaRegistrator register()

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

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.11.0 -m "<description of version>" 507d35892daef756f4e34e30b1a783c718601dec
git push origin v0.11.0

Please sign in to comment.