Skip to content

Conversation

@torfjelde
Copy link
Contributor

@torfjelde torfjelde commented Nov 17, 2022

Using similar means that we get automatic compat with several other packages, e.g. ComponentArrays.jl (the demo from the docs):

julia> x = ComponentVector= 0.0, σ = 1.0);

julia>= ADgradient(:ForwardDiff, problem)
ForwardDiff AD wrapper for NormalPosterior{Float64}(100, -0.06359079034463508, 1.241856096172367), w/ chunk size 2

julia> LogDensityProblems.logdensity_and_gradient(ADgradient(:ForwardDiff, problem), x)[2]
ComponentVector{Float64}= -6.359079034463508, σ = 25.339988478902235)

julia> ForwardDiff.gradient(Base.Fix1(LogDensityProblems.logdensity, problem), x) 
ComponentVector{Float64}= -6.359079034463508, σ = 25.339988478902235)

Ref: TuringLang/AdvancedHMC.jl#301

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.

vector.
"""
function _diffresults_extract(diffresult::DiffResults.DiffResult)
# NOTE: Is this still needed?
Copy link
Collaborator

Choose a reason for hiding this comment

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

That's not affected by the change above, is it?

Copy link
Owner

Choose a reason for hiding this comment

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

I also don't understand why it would not be needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nah, it was a note by me so we I could ask why it's there:) As in, can we just remove it?

Copy link
Owner

Choose a reason for hiding this comment

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

What would you use instead?

The purpose was to collect everything related to extraction in a single function. I am open to suggestions, but simply removing it would break the package (the function is used).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I thought the ::Real was the reason for it, and was thinking maybe this is now redundant because type-inference is better / DiffResults might have fixed whatever issue required the ::Real in the first place. If that's not the case and it's just for convenience, then I'll remove the comment:)

Copy link
Owner

Choose a reason for hiding this comment

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

I no longer recall the case for ::Real, so I guess you can remove it. We can always put it back if things break and then document why it is there.

Please also remove the comment, and then I am happy to merge.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done:)

@torfjelde
Copy link
Contributor Author

torfjelde commented Nov 18, 2022 via email

Copy link
Owner

@tpapp tpapp left a comment

Choose a reason for hiding this comment

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

LGTM, just remove ::Real and the added comment.

vector.
"""
function _diffresults_extract(diffresult::DiffResults.DiffResult)
# NOTE: Is this still needed?
Copy link
Owner

Choose a reason for hiding this comment

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

I no longer recall the case for ::Real, so I guess you can remove it. We can always put it back if things break and then document why it is there.

Please also remove the comment, and then I am happy to merge.

Copy link
Owner

@tpapp tpapp left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@tpapp tpapp merged commit d4f03be into tpapp:master Nov 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants