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

logstatsexp #77

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open

logstatsexp #77

wants to merge 9 commits into from

Conversation

cossio
Copy link
Contributor

@cossio cossio commented Nov 11, 2023

Adds the functions logmeanexp, logvarexp, logstdexp

@cossio
Copy link
Contributor Author

cossio commented Nov 11, 2023

These functions are usually useful for me, thought it'd be nice to have them here.

Not sure why CI is failing on Julia 1.0 ?

src/logstatsexp.jl Outdated Show resolved Hide resolved
src/logstatsexp.jl Outdated Show resolved Hide resolved
function logmeanexp(A::AbstractArray; dims=:)
R = logsumexp(A; dims)
N = length(A) ÷ length(R)
return R .- log(N)
Copy link
Member

Choose a reason for hiding this comment

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

This will cause undesired promotions and allocations.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it's better now.

Copy link
Member

@devmotion devmotion Nov 11, 2023

Choose a reason for hiding this comment

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

It seems you adressed my comment about promotions but not the allocations.

Copy link

Choose a reason for hiding this comment

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

@devmotion you might argue that unless there's a way to modify logsumexp to directly take log(N) into account, an in-place operation might create issues with AD systems which do not support it?

src/logstatsexp.jl Outdated Show resolved Hide resolved
R = logsumexp(2logsubexp.(A, logmean); dims)
N = length(A) ÷ length(R)
if corrected
return R .- log(N - 1)
Copy link
Member

Choose a reason for hiding this comment

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

Again this causes undesired promotions and allocations.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I think it's better now.

Copy link
Member

Choose a reason for hiding this comment

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

The unnecessary allocations are still present.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Then I'm not sure what you are referring to?

Copy link
Member

Choose a reason for hiding this comment

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

R .- log(N - 1)

creates a new array. But if R is e.g. of type Array this causes unnecessary allocations.

test/logstatsexp.jl Outdated Show resolved Hide resolved
@longemen3000
Copy link
Contributor

longemen3000 commented Nov 11, 2023

I don't have an strong opinion on this, but Is it possible to implement this as an extension? This package is a really low level one, and adding a new dependency could have effects on downstream packages?
The same thing applies to LinearAlgebra, (but they should be it's own issue)

@cossio
Copy link
Contributor Author

cossio commented Nov 11, 2023

@longemen3000 The PR is not introducing any dependencies.

@longemen3000
Copy link
Contributor

Statistics, while a Stdlib, it is a new dependency

src/logstatsexp.jl Outdated Show resolved Hide resolved
@longemen3000
Copy link
Contributor

longemen3000 commented Nov 11, 2023

Ohhh, nvm, my error, in that case, no comment at all

@cossio
Copy link
Contributor Author

cossio commented Nov 11, 2023

@longemen3000 Yes but Statistics is only used in tests

@cossio
Copy link
Contributor Author

cossio commented Nov 11, 2023

CI passing now.

@devmotion I agree the implementation is not optimal but I don't have time to tune it now. Could we merge as it is now?

@ParadaCarleton
Copy link

I think we can merge now and work on optimization later.

Honestly, it's just a huge pain trying to optimize for allocations without having Transducers.jl, laziness, or good functional primitives in Julia.

@devmotion
Copy link
Member

devmotion commented Nov 11, 2023

@ParadaCarleton as far as I know you've never contributed to this package, so I really think that you should let the maintainers of this package decide when a PR is ready and can be merged. In my opinion, being a member of JuliaStats doesn't mean that you are able and should merge PRs in repos that you haven't contributed to and/or are not familiar with. For instance, there are many JuliaStats packages where I don't think it's appropriate for me to merge PRs.

@devmotion
Copy link
Member

Clearly, we should not over-optimize this PR. But at the same time we should hold off merging PRs if reviewer comments that can be addressed without too much effort are not resolved. For instance, I think currently the following things are missing in this PR:

  • Definitions for non-AbstractArray inputs (it seems the same algorithm could be applied?)
  • Avoiding allocating R .- log(N) etc. when R is an Array and broadcasting when R is a scalar
  • Tests with non-Float64 inputs (these would have uncovered the promotion issues)

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.

5 participants