-
Notifications
You must be signed in to change notification settings - Fork 42
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
median(x, dims=n) is type unstable #39
Comments
I did a little digging and realized that the type instability comes from |
Possibly this should avoid mapslices, something like this? Statistics._median(v::AbstractVector, dims::Integer) = dims==1 ? [median!(collect(v))] : collect(v)
function Statistics._median(A::AbstractArray{T,N}, dims) where {T,N}
iter = Iterators.product(ntuple(d -> (d in dims) ? axes(A,d) : (:,), Val(N))...)
[median!(A[i...]) for i in iter]::Array{T,N}
end
@code_warntype median(rand(10), dims=1)
@code_warntype median(rand(10,10), dims=1) This is quicker for very small arrays, but doesn't matter for large ones: julia> @btime median(r, dims=1) setup=(r=rand(10,10)); # Julia 1.4
10.725 μs (119 allocations: 4.31 KiB)
julia> @btime median(r, dims=1) setup=(r=rand(10,10)); # with this change
1.475 μs (15 allocations: 1.80 KiB) |
Even if performance is similar, type stability matters as it can improve performance down the line. |
Agreed. But should be the fix for this to make |
Yes but we could maybe find a simpler fix for |
|
Another quick fix would be to at least type-assert that the output of This would of course not work for all |
Agree the output shouldn't change, but you could easily reshape afterwards. But still not viable until |
This appears to be fixed since Julia v1.9:
|
In both Julia 1.3.1 and Julia 1.4.1 the
median
function is type unstable if called with thedims
keyword argument.For example
The text was updated successfully, but these errors were encountered: