-
Notifications
You must be signed in to change notification settings - Fork 43
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
Improve chol_lower
and chol_upper
#149
Conversation
ecabc58
to
44cdb40
Compare
Is the type instability a concern? |
If you mean if it caused me problems in any downstream application, then no, it didn't. I noticed it when I played around with FluxML/Zygote.jl#1117 and this PR seemed like a simple improvement. But I'm not too invested in the PR and therefore didn't push it, I just remembered it when I went through my list of open PRs yesterday. |
I was considering the type instability in the new version |
Ah, sorry, I misunderstood. The PR doesn't change this type instability, the return types are exactly the same as on the master branch. For completeness, I benchmarked the version on the master branch, the version in this PR, and the version in this PR + julia> using BenchmarkTools, LinearAlgebra
julia> chol_lower(a::Cholesky) = a.uplo === 'L' ? a.L : a.U'
chol_lower (generic function with 1 method)
julia> chol_upper(a::Cholesky) = a.uplo === 'U' ? a.U : a.L'
chol_upper (generic function with 1 method)
julia> function chol_lower_pr(a::Cholesky)
return a.uplo === 'L' ? LowerTriangular(a.factors) : LowerTriangular(copy(a.factors'))
end
chol_lower_pr (generic function with 1 method)
julia> function chol_lower_pr(a::Cholesky)
return a.uplo === 'L' ? LowerTriangular(a.factors) : LowerTriangular(a.factors')
end
chol_lower_pr (generic function with 1 method)
julia> function chol_lower_pr_copy(a::Cholesky)
return a.uplo === 'L' ? LowerTriangular(a.factors) : LowerTriangular(copy(a.factors'))
end
chol_lower_pr_copy (generic function with 1 method)
julia> function chol_upper_pr_copy(a::Cholesky)
return a.uplo === 'U' ? UpperTriangular(a.factors) : UpperTriangular(copy(a.factors'))
end
chol_upper_pr_copy (generic function with 1 method)
julia> C = cholesky(Symmetric(Matrix(I, 250, 250), :L));
julia> typeof(chol_lower(C))
LowerTriangular{Float64, Matrix{Float64}}
julia> typeof(chol_lower_pr(C))
LowerTriangular{Float64, Matrix{Float64}}
julia> typeof(chol_lower_pr_copy(C))
LowerTriangular{Float64, Matrix{Float64}}
julia> @btime chol_lower($C);
12.179 ns (1 allocation: 16 bytes)
julia> @btime chol_lower_pr($C);
8.839 ns (1 allocation: 16 bytes)
julia> @btime chol_lower_pr_copy($C);
4.598 ns (0 allocations: 0 bytes)
julia> typeof(chol_upper(C))
UpperTriangular{Float64, Adjoint{Float64, Matrix{Float64}}}
julia> typeof(chol_upper_pr(C))
UpperTriangular{Float64, Adjoint{Float64, Matrix{Float64}}}
julia> typeof(chol_upper_pr_copy(C))
UpperTriangular{Float64, Matrix{Float64}}
julia> @btime chol_upper($C);
16.554 ns (2 allocations: 32 bytes)
julia> @btime chol_upper_pr($C);
8.933 ns (1 allocation: 16 bytes)
julia> @btime chol_upper_pr_copy($C);
69.869 μs (2 allocations: 488.36 KiB)
julia> C = cholesky(Symmetric(Matrix(I, 250, 250), :U));
julia> typeof(chol_lower(C))
LowerTriangular{Float64, Adjoint{Float64, Matrix{Float64}}}
julia> typeof(chol_lower_pr(C))
LowerTriangular{Float64, Adjoint{Float64, Matrix{Float64}}}
julia> typeof(chol_lower_pr_copy(C))
LowerTriangular{Float64, Matrix{Float64}}
julia> @btime chol_lower($C);
16.602 ns (2 allocations: 32 bytes)
julia> @btime chol_lower_pr($C);
8.655 ns (1 allocation: 16 bytes)
julia> @btime chol_lower_pr_copy($C);
69.314 μs (2 allocations: 488.36 KiB)
julia> typeof(chol_upper(C))
UpperTriangular{Float64, Matrix{Float64}}
julia> typeof(chol_upper_pr(C))
UpperTriangular{Float64, Matrix{Float64}}
julia> typeof(chol_upper_pr_copy(C))
UpperTriangular{Float64, Matrix{Float64}}
julia> @btime chol_upper($C);
12.153 ns (1 allocation: 16 bytes)
julia> @btime chol_upper_pr($C);
8.617 ns (1 allocation: 16 bytes)
julia> @btime chol_upper_pr_copy($C);
4.588 ns (0 allocations: 0 bytes) As expected the version with So I don't think it's worth fixing the type instability. |
Codecov Report
@@ Coverage Diff @@
## master #149 +/- ##
==========================================
+ Coverage 89.67% 90.14% +0.47%
==========================================
Files 8 8
Lines 426 406 -20
==========================================
- Hits 382 366 -16
+ Misses 44 40 -4
Continue to review full report at Codecov.
|
This PR improves
chol_lower
andchol_upper
by using the fieldfactors
directly: