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

Use similar in creation of DiffResults buffer #95

Merged
merged 5 commits into from
Nov 22, 2022
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "LogDensityProblems"
uuid = "6fdf6af0-433a-55f7-b3ed-c6c6e0b8df7c"
authors = ["Tamas K. Papp <[email protected]>"]
version = "1.0.2"
version = "1.0.3"

[deps]
ArgCheck = "dce04be8-c92d-5529-be00-80e4d2c0e197"
Expand Down
2 changes: 1 addition & 1 deletion src/AD_ForwardDiff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ end

function logdensity_and_gradient(fℓ::ForwardDiffLogDensity, x::AbstractVector)
@unpack ℓ, gradientconfig = fℓ
buffer = _diffresults_buffer(ℓ, x)
buffer = _diffresults_buffer(x)
result = ForwardDiff.gradient!(buffer, Base.Fix1(logdensity, ℓ), x, gradientconfig)
_diffresults_extract(result)
end
2 changes: 1 addition & 1 deletion src/AD_ReverseDiff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ end

function logdensity_and_gradient(∇ℓ::ReverseDiffLogDensity, x::AbstractVector)
@unpack ℓ, compiledtape = ∇ℓ
buffer = _diffresults_buffer(ℓ, x)
buffer = _diffresults_buffer(x)
if compiledtape === nothing
result = ReverseDiff.gradient!(buffer, Base.Fix1(logdensity, ℓ), x)
else
Expand Down
6 changes: 3 additions & 3 deletions src/DiffResults_helpers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ $(SIGNATURES)
Allocate a DiffResults buffer for a gradient, taking the element type of `x` into account
(heuristically).
"""
function _diffresults_buffer(ℓ, x)
function _diffresults_buffer(x)
T = eltype(x)
S = T <: Real ? float(Real) : Float64 # heuristic
DiffResults.MutableDiffResult(zero(S), (Vector{S}(undef, dimension(ℓ)), ))
DiffResults.MutableDiffResult(zero(S), (similar(x, S), ))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should this still take into account dimension? I'm not sure but I assume there was a reason why it was used instead of x?

Suggested change
DiffResults.MutableDiffResult(zero(S), (similar(x, S), ))
DiffResults.MutableDiffResult(zero(S), (similar(x, S, dimension(ℓ)), ))

Copy link
Owner

Choose a reason for hiding this comment

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

Currently all it uses from xis its element type, and it takes the dimension from the first argument.

I think it could be done differently, using the dimension from x, and then maybe would not need to be an argument at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

using the dimension from x

So similar basically does this. And AFAIK this is only called from the logdensity_and_gradient method, meaning that x should always have the correct dimensions.

Using dimension from instead will sometimes result in loss of structure of the original type for x, e.g. ComponentArrays.

Copy link
Owner

Choose a reason for hiding this comment

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

I would recommend something like

function _diffresults_buffer(x)
    T = eltype(x)
    S = T <: Real ? float(Real) : Float64 # heuristic
    DiffResults.MutableDiffResult(zero(S), (similar(x, S), ))
end

with the caller modified accordingly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is what I've impleented, right? Or are my eyes deceiving me?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think @tpapp's point is that the first argument of the function should be dropped if we do not use it anymore.

end

"""
Expand All @@ -25,5 +25,5 @@ constructed with [`diffresults_buffer`](@ref). Gradient is not copied as caller
vector.
"""
function _diffresults_extract(diffresult::DiffResults.DiffResult)
DiffResults.value(diffresult)::Real, DiffResults.gradient(diffresult)
DiffResults.value(diffresult), DiffResults.gradient(diffresult)
end