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

No pre-allocation of outputs for logpdf and pdf #1263

Merged
merged 1 commit into from
Jan 21, 2021

Conversation

devmotion
Copy link
Member

This PR is a rebased version of #1257 without the deprecations and refactoring that were suggested by @andreasnoack in the previous PR but might require some additional discussion since it implies that some special implementations for evaluating batches of samples (e.g. for MvNormal) are removed.

I hope that the subset of changes in this PR are potentially less controversial and hence can be merged faster.

Copied from the original PR:
This PR removes the pre-allocation of output arrays for logpdf and pdf.

There seems little reason to fall back to the in-place methods since it does neither reduce allocations nor lines of code. Instead, I'd argue that the pre-allocation causes problems since the computation of the output type is only a heuristic and it seems easy to come up with examples where it fails since typically logpdf and pdf do not ensure that their output is of this specific type. Additionally, the pre-allocations lead to calls of mutating functions which break AD with e.g. Zygote. Moreover, it is consistent with logpdf and pdf implementations for univariate distributions that do not fall back to the mutating functions but use broadcasting (one could use the same implementation with map and Base.Fix1 there as well).

@@ -230,15 +230,13 @@ end
function logpdf(d::MultivariateDistribution, X::AbstractMatrix)
size(X, 1) == length(d) ||
throw(DimensionMismatch("Inconsistent array dimensions."))
T = promote_type(partype(d), eltype(X))
_logpdf!(Vector{T}(undef, size(X,2)), d, X)
map(i -> _logpdf(d, view(X, :, i)), axes(X, 2))
Copy link
Member Author

Choose a reason for hiding this comment

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

As mentioned in the original PR,

Suggested change
map(i -> _logpdf(d, view(X, :, i)), axes(X, 2))
map(Base.Fix1(_logpdf, d), eachcol(X))

would requires Julia >= 1.1 or Compat.

@andreasnoack
Copy link
Member

This should be uncontroversial

@andreasnoack andreasnoack merged commit 01ba56b into JuliaStats:master Jan 21, 2021
@devmotion devmotion deleted the logpdf_pdf_nodeprecations branch January 21, 2021 19:58
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.

2 participants