-
Notifications
You must be signed in to change notification settings - Fork 17
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
Fast concatentation #283
Fast concatentation #283
Conversation
Looking at the actual |
5c95b94
to
c97f0d2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@odunbar I have added explanations to all my changes as review comments. Feel free to reply directly to each one you may have questions about.
For every change I made, I tested that the refactored function is faster and allocates less than the baseline. In many cases, the speedup is very large. In other cases, it's minimal.
Note: I've only tested this using structs I found in the unit/integration tests, which are typically very small. So it's conceivable, although unlikely, that the improvement for "real world" examples (i.e. very large distributions) may be different.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for this! It looks greatly improved. I do not think any of these changes will haunt us computationally compared with the use of cat(...)
.
I've left a couple of small comments regarding using the ndims
for function distributions. An once resolved I'm happy!
@odunbar made the requested changes (I kept the relevant discussions "unresolved"). Let me know what you think. Once approved, I will squash before bors. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @haakon-e these changes look good! I see a test failed though.
Could this be resolved - then I'm happy for merge.
@odunbar It appears the test only fails on v1.6. |
It turns out that on # julia v1.8.4
julia> xx = zeros(4,5);
julia> mapslices(x -> reshape(x, 4, 1), xx; dims=1)
ERROR: DimensionMismatch: tried to assign 2 elements to 1 destinations
Stacktrace:
[1] throw_setindex_mismatch(X::Vector{Base.OneTo{Int64}}, I::Tuple{Int64})
@ Base ./indices.jl:191
[2] setindex_shape_check
@ ./indices.jl:245 [inlined]
[3] setindex!(A::Vector{Base.OneTo{Int64}}, X::Vector{Base.OneTo{Int64}}, I::Vector{Int64})
@ Base ./array.jl:973
[4] mapslices(f::var"#5#6", A::Matrix{Float64}; dims::Int64)
@ Base ./abstractarray.jl:2873
[5] top-level scope
@ REPL[5]:1 In contrast, this works in julia v1.9: # julia v1.9.2
julia> xx = zeros(4,5);
julia> mapslices(x -> reshape(x, 4, 1), xx; dims=1)
4×5 Matrix{Float64}:
0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 I have updated my implementation of
All of this being said, as far as I can see the use of x_real_constrained1 = mapslices(x -> transform_unconstrained_to_constrained(u1, x), x_unbd[1:4, :]; dims = 1)
x_real_constrained1 == transform_unconstrained_to_constrained(u1, x_unbd[1:4, :])
true but this could be changed in a different PR |
replace various instances of "cat" + "..." with faster (and often less allocating) alternatives such as reduce(vcat, [iterable]). Sometimes, the size and type of the output array can be predicted. this information is now exploited. For `transform_(un)cons_to_(un)cons`-methods, optimizations meant Vectors and Matrices could be handled by the same function without the need for multiple dispatch.
bors r+ |
Build succeeded! The publicly hosted instance of bors-ng is deprecated and will go away soon. If you want to self-host your own instance, instructions are here. If you want to switch to GitHub's built-in merge queue, visit their help page. |
closes #274