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 2 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: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "LogDensityProblemsAD"
uuid = "996a588d-648d-4e1f-a8f0-a84b347e47b1"
authors = ["Tamás K. Papp <[email protected]>"]
version = "1.9.0"
sunxd3 marked this conversation as resolved.
Show resolved Hide resolved
version = "2.0.0"

[deps]
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
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
12 changes: 11 additions & 1 deletion src/LogDensityProblemsAD.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Automatic differentiation backends for LogDensityProblems.
"""
module LogDensityProblemsAD

export ADgradient
export ADgradient, replace_ℓ
sunxd3 marked this conversation as resolved.
Show resolved Hide resolved

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

Replace the log density in an `ADGradientWrapper`.
"""
function replace_ℓ(∇ℓ::ADGradientWrapper, new_ℓ)
@info "$(typeof(∇ℓ)) not supported for replacement"
throw(MethodError(replace_ℓ, (∇ℓ, 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