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

Add interface functions to allow replacing the log density function and replacing AD wrapper type #33

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ authors = ["Tamás K. Papp <[email protected]>"]
version = "1.9.0"
sunxd3 marked this conversation as resolved.
Show resolved Hide resolved

[deps]
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
LogDensityProblems = "6fdf6af0-433a-55f7-b3ed-c6c6e0b8df7c"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
Expand Down Expand Up @@ -31,6 +32,7 @@ LogDensityProblemsADZygoteExt = "Zygote"

[compat]
ADTypes = "0.1.7, 0.2, 1"
Compat = "4.15.0"
DocStringExtensions = "0.8, 0.9"
Enzyme = "0.11, 0.12"
FiniteDifferences = "0.12"
Expand Down
12 changes: 12 additions & 0 deletions ext/LogDensityProblemsADReverseDiffExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ function ADgradient(::Val{:ReverseDiff}, ℓ;
x::Union{Nothing,AbstractVector}=nothing)
ReverseDiffLogDensity(ℓ, _compiledtape(ℓ, compile, x))
end
function ADgradient(::Val{:ReverseDiff}, ∇ℓ::ADGradientWrapper;
compile::Union{Val{true},Val{false}}=Val(false),
x::Union{Nothing,AbstractVector}=nothing)
ADgradient(Val{:ReverseDiff}, ∇ℓ.ℓ; compile=compile, x=x)
end

function LogDensityProblemsAD.replace_ℓ(∇ℓ::ReverseDiffLogDensity{L,C}, new_ℓ)
ReverseDiffLogDensity(new_ℓ, _compiledtape(new_ℓ, Val(true), nothing))
end
function LogDensityProblemsAD.replace_ℓ(∇ℓ::ReverseDiffLogDensity{L,Nothing}, new_ℓ)
ReverseDiffLogDensity(new_ℓ, _compiledtape(new_ℓ, Val(false), nothing))
end

_compiledtape(ℓ, compile, x) = nothing
_compiledtape(ℓ, ::Val{true}, ::Nothing) = _compiledtape(ℓ, Val(true), zeros(dimension(ℓ)))
Expand Down
8 changes: 8 additions & 0 deletions src/LogDensityProblemsAD.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Automatic differentiation backends for LogDensityProblems.
module LogDensityProblemsAD

export ADgradient
@compat public replace_ℓ

using DocStringExtensions: SIGNATURES
import LogDensityProblems: logdensity, logdensity_and_gradient, capabilities, dimension
Expand Down Expand Up @@ -38,6 +39,13 @@ Base.copy(x::ADGradientWrapper) = x # no-op, except for ForwardDiff
"""
$(SIGNATURES)

Replace the log density in an `ADGradientWrapper`.
"""
function replace_ℓ(∇ℓ::ADGradientWrapper, new_ℓ) end

sunxd3 marked this conversation as resolved.
Show resolved Hide resolved
"""
$(SIGNATURES)

Wrap `P` using automatic differentiation to obtain a gradient.

`kind` is usually a `Val` type with a symbol that refers to a package, for example
Expand Down
Loading