-
Notifications
You must be signed in to change notification settings - Fork 1
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
added: linearize!
is allocation-free once again
#165
Conversation
Something that I still don't like
NonLinModel{Float64, ModelPredictiveControl.var"#rk4_solver!#21"{DataType, RungeKutta, typeof(f3!), PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, Float64}, typeof(h3!), @NamedTuple{A::Matrix{Float64}, Bu::Vector{Float64}, Bd::Vector{Float64}, C::Matrix{Float64}, Dd::Vector{Int64}}, RungeKutta, ModelPredictiveControl.LinearizationBuffer{Float64, ModelPredictiveControl.JacobianBuffer{ModelPredictiveControl.var"#f_at_u_d!#192"{ModelPredictiveControl.var"#rk4_solver!#21"{DataType, RungeKutta, typeof(f3!), PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, Float64}, @NamedTuple{A::Matrix{Float64}, Bu::Vector{Float64}, Bd::Vector{Float64}, C::Matrix{Float64}, Dd::Vector{Int64}}, Vector{Float64}, Vector{Float64}}, ForwardDiff.JacobianConfig{ForwardDiff.Tag{ModelPredictiveControl.var"#f_at_u_d!#192"{ModelPredictiveControl.var"#rk4_solver!#21"{DataType, RungeKutta, typeof(f3!), PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, Float64}, @NamedTuple{A::Matrix{Float64}, Bu::Vector{Float64}, Bd::Vector{Float64}, C::Matrix{Float64}, Dd::Vector{Int64}}, Vector{Float64}, Vector{Float64}}, Float64}, Float64, 2, Tuple{Vector{ForwardDiff.Dual{ForwardDiff.Tag{ModelPredictiveControl.var"#f_at_u_d!#192"{ModelPredictiveControl.var"#rk4_solver!#21"{DataType, RungeKutta, typeof(f3!), PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, Float64}, @NamedTuple{A::Matrix{Float64}, Bu::Vector{Float64}, Bd::Vector{Float64}, C::Matrix{Float64}, Dd::Vector{Int64}}, Vector{Float64}, Vector{Float64}}, Float64}, Float64, 2}}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{ModelPredictiveControl.var"#f_at_u_d!#192"{ModelPredictiveControl.var"#rk4_solver!#21"{DataType, RungeKutta, typeof(f3!), PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, Float64}, @NamedTuple{A::Matrix{Float64}, Bu::Vector{Float64}, Bd::Vector{Float64}, C::Matrix{Float64}, Dd::Vector{Int64}}, Vector{Float64}, Vector{Float64}}, Float64}, Float64, 2}}}}}, ModelPredictiveControl.JacobianBuffer{ModelPredictiveControl.var"#f_at_x_d!#193"{ModelPredictiveControl.var"#rk4_solver!#21"{DataType, RungeKutta, typeof(f3!), PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, Float64}, @NamedTuple{A::Matrix{Float64}, Bu::Vector{Float64}, Bd::Vector{Float64}, C::Matrix{Float64}, Dd::Vector{Int64}}, Vector{Float64}, Vector{Float64}}, ForwardDiff.JacobianConfig{ForwardDiff.Tag{ModelPredictiveControl.var"#f_at_x_d!#193"{ModelPredictiveControl.var"#rk4_solver!#21"{DataType, RungeKutta, typeof(f3!), PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, Float64}, @NamedTuple{A::Matrix{Float64}, Bu::Vector{Float64}, Bd::Vector{Float64}, C::Matrix{Float64}, Dd::Vector{Int64}}, Vector{Float64}, Vector{Float64}}, Float64}, Float64, 1, Tuple{Vector{ForwardDiff.Dual{ForwardDiff.Tag{ModelPredictiveControl.var"#f_at_x_d!#193"{ModelPredictiveControl.var"#rk4_solver!#21"{DataType, RungeKutta, typeof(f3!), PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, Float64}, @NamedTuple{A::Matrix{Float64}, Bu::Vector{Float64}, Bd::Vector{Float64}, C::Matrix{Float64}, Dd::Vector{Int64}}, Vector{Float64}, Vector{Float64}}, Float64}, Float64, 1}}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{ModelPredictiveControl.var"#f_at_x_d!#193"{ModelPredictiveControl.var"#rk4_solver!#21"{DataType, RungeKutta, typeof(f3!), PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, Float64}, @NamedTuple{A::Matrix{Float64}, Bu::Vector{Float64}, Bd::Vector{Float64}, C::Matrix{Float64}, Dd::Vector{Int64}}, Vector{Float64}, Vector{Float64}}, Float64}, Float64, 1}}}}}, ModelPredictiveControl.JacobianBuffer{ModelPredictiveControl.var"#f_at_x_u!#194"{ModelPredictiveControl.var"#rk4_solver!#21"{DataType, RungeKutta, typeof(f3!), PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, Float64}, @NamedTuple{A::Matrix{Float64}, Bu::Vector{Float64}, Bd::Vector{Float64}, C::Matrix{Float64}, Dd::Vector{Int64}}, Vector{Float64}, Vector{Float64}}, ForwardDiff.JacobianConfig{ForwardDiff.Tag{ModelPredictiveControl.var"#f_at_x_u!#194"{ModelPredictiveControl.var"#rk4_solver!#21"{DataType, RungeKutta, typeof(f3!), PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, Float64}, @NamedTuple{A::Matrix{Float64}, Bu::Vector{Float64}, Bd::Vector{Float64}, C::Matrix{Float64}, Dd::Vector{Int64}}, Vector{Float64}, Vector{Float64}}, Float64}, Float64, 1, Tuple{Vector{ForwardDiff.Dual{ForwardDiff.Tag{ModelPredictiveControl.var"#f_at_x_u!#194"{ModelPredictiveControl.var"#rk4_solver!#21"{DataType, RungeKutta, typeof(f3!), PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, Float64}, @NamedTuple{A::Matrix{Float64}, Bu::Vector{Float64}, Bd::Vector{Float64}, C::Matrix{Float64}, Dd::Vector{Int64}}, Vector{Float64}, Vector{Float64}}, Float64}, Float64, 1}}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{ModelPredictiveControl.var"#f_at_x_u!#194"{ModelPredictiveControl.var"#rk4_solver!#21"{DataType, RungeKutta, typeof(f3!), PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, PreallocationTools.DiffCache{Vector{Float64}, Vector{Float64}}, Float64}, @NamedTuple{A::Matrix{Float64}, Bu::Vector{Float64}, Bd::Vector{Float64}, C::Matrix{Float64}, Dd::Vector{Int64}}, Vector{Float64}, Vector{Float64}}, Float64}, Float64, 1}}}}}, ModelPredictiveControl.JacobianBuffer{ModelPredictiveControl.var"#h_at_d!#195"{typeof(h3!), @NamedTuple{A::Matrix{Float64}, Bu::Vector{Float64}, Bd::Vector{Float64}, C::Matrix{Float64}, Dd::Vector{Int64}}, Vector{Float64}}, ForwardDiff.JacobianConfig{ForwardDiff.Tag{ModelPredictiveControl.var"#h_at_d!#195"{typeof(h3!), @NamedTuple{A::Matrix{Float64}, Bu::Vector{Float64}, Bd::Vector{Float64}, C::Matrix{Float64}, Dd::Vector{Int64}}, Vector{Float64}}, Float64}, Float64, 2, Tuple{Vector{ForwardDiff.Dual{ForwardDiff.Tag{ModelPredictiveControl.var"#h_at_d!#195"{typeof(h3!), @NamedTuple{A::Matrix{Float64}, Bu::Vector{Float64}, Bd::Vector{Float64}, C::Matrix{Float64}, Dd::Vector{Int64}}, Vector{Float64}}, Float64}, Float64, 2}}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{ModelPredictiveControl.var"#h_at_d!#195"{typeof(h3!), @NamedTuple{A::Matrix{Float64}, Bu::Vector{Float64}, Bd::Vector{Float64}, C::Matrix{Float64}, Dd::Vector{Int64}}, Vector{Float64}}, Float64}, Float64, 2}}}}}, ModelPredictiveControl.JacobianBuffer{ModelPredictiveControl.var"#h_at_x!#196"{typeof(h3!), @NamedTuple{A::Matrix{Float64}, Bu::Vector{Float64}, Bd::Vector{Float64}, C::Matrix{Float64}, Dd::Vector{Int64}}, Vector{Float64}}, ForwardDiff.JacobianConfig{ForwardDiff.Tag{ModelPredictiveControl.var"#h_at_x!#196"{typeof(h3!), @NamedTuple{A::Matrix{Float64}, Bu::Vector{Float64}, Bd::Vector{Float64}, C::Matrix{Float64}, Dd::Vector{Int64}}, Vector{Float64}}, Float64}, Float64, 1, Tuple{Vector{ForwardDiff.Dual{ForwardDiff.Tag{ModelPredictiveControl.var"#h_at_x!#196"{typeof(h3!), @NamedTuple{A::Matrix{Float64}, Bu::Vector{Float64}, Bd::Vector{Float64}, C::Matrix{Float64}, Dd::Vector{Int64}}, Vector{Float64}}, Float64}, Float64, 1}}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{ModelPredictiveControl.var"#h_at_x!#196"{typeof(h3!), @NamedTuple{A::Matrix{Float64}, Bu::Vector{Float64}, Bd::Vector{Float64}, C::Matrix{Float64}, Dd::Vector{Int64}}, Vector{Float64}}, Float64}, Float64, 1}}}}}}} I'm pretty sure it cannot be avoided when pre-allocating the work buffer of any AD tools. Normally the user should not see this extremely verbose type. @gdalle I see that there is a similar "problem" in the type of the object returned by |
Why would you need to hide them? If the user prints the preparation result, which is an internal, that's on them. |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #165 +/- ##
=======================================
Coverage 99.01% 99.02%
=======================================
Files 24 24
Lines 3770 3790 +20
=======================================
+ Hits 3733 3753 +20
Misses 37 37 ☔ View full report in Codecov by Sentry. |
Yep, that's true if the user explicitly print them. The real problem in my opinion is when the target function is not differentiable for some reasons. Oh well, I think I will have to live with this. |
Also, overriding printing on types seems dangerously close to piracy |
There are options to abbreviate stack traces using third party packages |
Alright thanks @gdalle, I'll take a look at the third party packages. |
The one I had in mind is https://github.com/BioTurboNick/AbbreviatedStackTraces.jl. Note that it shouldn't become a dependency of your package, it's better suited as a dev dependency installed in your global environment (like Revise). |
Re-designed the Jacobian and linearization buffers for an implementation that is a bit simpler, cleaner and also modular (will also work with gradients and hessians, normally)
Also prepare the ground for migration to
DifferentiationInterface.jl
.There is no type-instability at runtime, tested on my side with
@code_warntype
.Related to #162