Repository navigation
added: ExplicitMPC now support AD with Mooncake.jl + warn_cond kwarg - #529
Conversation
Matrix inversion is avoided what all the `KalmanEstimator` except `MovingHorizonEstimator`
It can me manually disabled with `warn_cond=Inf`, so the `try` block is no longer necessary. The block also needs to be removed for `Mooncake.jl` autodiff.
The `cond` call must not appear in the call stack at all, not even in `if` branch that is never executed.
Making it a separate allow creating in a exception for Mooncake, with the `@mooncake_overlay` mechanism.
This is necessary for `ForwardDiff` AD.
ExplicitMPC now support AD with Mooncake.jlExplicitMPC now support AD with Mooncake.jl and warn_cond keyword argument
ExplicitMPC now support AD with Mooncake.jl and warn_cond keyword argumentExplicitMPC now support AD with Mooncake.jl + warn_cond kw arg
|
Now Here's a MRE with using LinearAlgebra
using Logging
using ModelPredictiveControl
using ReverseDiff
using Mooncake
using ForwardDiff
const ERROR_ONLY = ConsoleLogger(stderr, Logging.Error)
A = [0.9 0.08; -0.15 0.75]
Bu = [0.005, 0.09]
C = [1.0 0.0]
# needed for Mooncake, a `@warn` call make it crashes, even in a branch never reached:
Mooncake.@mooncake_overlay ModelPredictiveControl.verify_delays(model, Hp) = nothing
function first_move(Nwt)
T = eltype(Nwt)
model = LinModel{T}(A, Bu, C, 0, 0, 0.1)
estim = KalmanFilter(model, nint_ym=0, verify_obsv=false)
mpc = ExplicitMPC(estim; Hp=20, Hc=2, Nwt=Nwt, warn_cond=Inf)
preparestate!(mpc, [0.0])
return moveinput!(mpc, [1.0])[1]
end
@show first_move([0.5])
∇f = with_logger(ERROR_ONLY) do # suppress the `verify_delays` warning:
ForwardDiff.gradient(first_move, [0.5])
end
@show ∇f
∇f = with_logger(ERROR_ONLY) do # suppress the `verify_delays` warning:
ReverseDiff.gradient(first_move, [0.5])
end
@show ∇f
cache = Mooncake.prepare_gradient_cache(first_move, [0.5])
f, ∇f = Mooncake.value_and_gradient!!(cache, first_move, [0.5])
@show ∇f[2]printing: |
|
I will close the issue, I won't put too much time on Enzyme, a bit too finicky and I was not able to make it work. Closes #523 |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #529 +/- ##
==========================================
+ Coverage 97.30% 97.50% +0.19%
==========================================
Files 28 28
Lines 7099 7094 -5
==========================================
+ Hits 6908 6917 +9
+ Misses 191 177 -14
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Thank you @franckgaga - this is very exciting ! Thank you very much ! I'll get back to you :) With gratitude and excitwment, |
They are useless, on a second thought. The exception thrown in `ModelPredictiveControl.inv!` and the associated stacktrace is already clear enough.
Testing covariance matrices with non-zero off-diagonal coefficients.
ExplicitMPC now support AD with Mooncake.jl + warn_cond kw argExplicitMPC now support AD with Mooncake.jl + warn_cond kwarg
This PR introduces necessary modifications to support
ExplicitMPCdifferentiation withMooncake.jl. It also introduces various improvements in the codebase:warn_cond=1e6keyword argument inLinMPCandExplicitMPCconstructor, to change the warning threshold for the condition number of the QP Hessian. Set towarn_cond=Infto entirely skip this checkup.invP̄,invQ̂_HeandinvR̂_Heare only required for theMovingHorizonEstimator. TheKalmanCovariancesconstructor hence no longer computes them for all the otherStateEstimatortypes.empty_similarhelper that returns an empty matrix of the same type as the one in argument, and assert the return type. This helper is super useful on Julia v1.10 (LTS), since it does not support direct conversion fromMatrixtoDiagonal, even if the originalMatrixhas non-zero values only on the main diagonal. In any case, it should never happen in this package, so the assertion is an additional safety net for this.