-
Notifications
You must be signed in to change notification settings - Fork 230
Define private dot
function to avoid type-piracy
#749
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
Conversation
Codecov Report
@@ Coverage Diff @@
## master #749 +/- ##
==========================================
+ Coverage 81.53% 81.53% +<.01%
==========================================
Files 43 43
Lines 2431 2432 +1
==========================================
+ Hits 1982 1983 +1
Misses 449 449
Continue to review full report at Codecov.
|
Thanks, I didn't notice there is another PR. I'd actually argue this PR is better because it fixes the root cause (= type-piracy). But I guess it's up to Optim devs to choose, in the end. |
Thanks. I understand your points, but I merged the other PR. Either way, you should be able to use master Optim on master now :) |
@pkofod I appreciate that the immediate problem is quickly resolved. But let me point out type-pirating Optim.jl/src/multivariate/precon.jl Line 48 in 64eb600
overwrites the definition in function dot(x::AbstractVector, D::Diagonal, y::AbstractVector)
mapreduce(t -> dot(t[1], t[2], t[3]), +, zip(x, D.diag, y))
end The definition in Optim allocates an intermediate array. Also, IIRC, julia> using LinearAlgebra
julia> x = rand(1000);
julia> y = rand(1000);
julia> D = Diagonal(rand(1000));
julia> dot(x, D, y)
127.29363992864297
julia> using Optim
julia> dot(x, D, y)
127.29363992864292 I think this can introduce very subtle bugs which are hard to debug. |
So do you know what version of LinearAlgebra introduced this? Same commit as the other one? I think the solution is just to wrap that definition as well. I get your point, we could just define our own dot, but long term (post 1.4) that's not what we want to do, we want to import and if necessary extend (no need for piracy as base/stdlib now has these) LinearAlgebra.dot. So I think wrapping definitions in if blocks is fine, the one you pointed out was just missing. Sorry I'm not actively fixing this myself, I'm just not ready for 1.4 yet :) |
Sorry if it sounds very nit-picky, but Optim also defines
I think so. I'll check it and open a PR. |
That's fine, happy to discuss. (though its late here :) ) |
By the way, I noticed that A better long-term option might be to use https://github.com/JuliaArrays/LazyArrays.jl as handling something like |
😑
Sounds reasonable! |
Yeah or LinearMaps, with a function call to apply the ldiv (which I think LinearMaps does not actually support easily now, though) |
I guess it's not hard to hook LinearMaps into LazyArrays API. The syntax would be a bit nicer (e.g., |
The problem is that Optim expects P to define ldiv!, which LinearMaps don't do. This whole thing would be so much simpler if people used the sane convention that |
I'm not sure if I follow. I'm just an "end-user" when it comes to the actual optimization algorithms. But do you mean that you prefer to directly define the linear map that does the multiplication by the inverse of the hessian? |
Yeah, that's usually what happens for large scale systems (where ldiv! could be, eg, a multigrid algorithm or something). Also you never have to apply P, just inv(P). The same problem pops up in iterative solvers for linear systems or for eigenvalue problems. That's not covered well by the usual packages (eg LinearMaps), so what I usually do is define a custom object that implements ldiv! (and maybe a few others, as needed by the algorithm). |
I see. Then LazyArray may be useful here. You can define a linear map |
I think it's better for Optim to remain agnostic to what P actually is, and not use LazyArrays. I'm fine with the current situation, it's just a bit unfortunate that the community has decided on P being an approximation to A instead of an approximation to A^-1. |
Well, I'm personally hoping LazyArrays to be as universal as broadcasting :) |
A reminder that Compat.jl is an officially-sanctioned mechanism for "backporting" features from newer Julia versions to older ones (even if it's piracy), and one that allows other packages to take advantage of the features. |
As of JuliaLang/julia#32739, Julia has 3-arg
dot
function. Using Optim master with Julia 1.4-DEV produces a warningThis PR fixes the warning by defining a private function
Optim.dot
.