No pre-allocation of outputs for logpdf
and pdf
#1263
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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).