Repository navigation
Description
Dear ModelPredictiveControl,
Thank you for the incredible work, and incredibly useful software!
I am fitting control theory models to observed behaviour (happy to give more context for why this is useful if you'd like), and in that context, I need to differentiate through the construction of the model. This breaks with ForwardDiff, ReverseDiff, Mooncake and Enzyme, for various reasons. Below is a Claude-reduced and Claude-reported MWE with the different issues, and suggestions for making differentiation possible. I hope its useful - let me know if I can do anything! And if this is a known issue, or something you don't want to support, I apologise for taking your time.
--- Claude ---
Versions
- Julia 1.13.3, macOS 26.6.2, arm64 (Apple Silicon)
- ModelPredictiveControl 2.16.3, ControlSystemsBase 1.25.4, ForwardDiff 1.4.8, Mooncake 0.5.70
Context
I fit a model whose parameters include MPC weights (here Nwt) by MCMC, so the controller is built inside the function being differentiated. LinModel{NT}, KalmanFilter and ExplicitMPC are generic in the number type, and the control law itself differentiates fine. What stops it are three checks in the constructors that compute eigenvalues with LAPACK.
Reproducer
using ModelPredictiveControl, ForwardDiff
A = [0.9 0.08; -0.15 0.75]
Bu = reshape([0.005, 0.09], 2, 1)
C = [1.0 0.0]
# The first move of an ExplicitMPC as a function of its input weight
function first_move(Nwt)
T = typeof(Nwt)
model = LinModel{T}(A, Bu, C, 0, 0, 0.1)
estim = KalmanFilter(model; nint_ym=0)
mpc = ExplicitMPC(estim; Hp=20, Hc=2, Mwt=[one(T)], Nwt=[Nwt], Lwt=[zero(T)])
preparestate!(mpc, [0.0])
return moveinput!(mpc, [1.0])[1]
end
first_move(0.5)
ForwardDiff.derivative(first_move, 0.5)With the three checks bypassed for dual numbers (type piracy, only to show that the rest differentiates), ForwardDiff matches a central difference:
using ForwardDiff: Dual, value
const MPC = ModelPredictiveControl
MPC.estimate_delays(::LinModel{<:Dual}) = 0
MPC.ControlSystemsBase.observability(A::AbstractMatrix{<:Dual}, C::AbstractMatrix; kwargs...) =
MPC.ControlSystemsBase.observability(value.(A), value.(C); kwargs...)
MPC.verify_cond(::MPC.SingleShooting, ::AbstractMatrix{<:Dual}, warn_cond) = nothing
ForwardDiff.derivative(first_move, 0.5)
(first_move(0.5 + 1e-6) - first_move(0.5 - 1e-6)) / 2e-6Mooncake in reverse mode stops earlier, at the try/catch blocks of the covariance constructor:
using Mooncake
Mooncake.prepare_gradient_cache(first_move, 0.5)Observed
ERROR: MethodError: no method matching eigvals!(::Matrix{ForwardDiff.Dual{ForwardDiff.Tag{typeof(first_move), Float64}, Float64, 1}})
from ControlSystemsBase.observability in augment_model, called by the KalmanFilter constructor. With the checks bypassed, the derivative agrees with the central difference. Mooncake:
ERROR: Mooncake failed to differentiate the following method:
│ (ModelPredictiveControl.KalmanCovariances{NT, Q̂C, R̂C} where ...)(Q̂::Q̂C, R̂::R̂C, P̂_0, He)
│ Caused by:
│ try/catch/finally blocks are not supported by Mooncake.jl in reverse mode.
Expected
ForwardDiff.derivative(first_move, 0.5) returns the derivative, since the computation it differentiates is generic.
Likely cause
Line numbers are for v2.16.3:
estimate_delayscallseigvals(model.A)(src/controller/construct.jl:586). It is called by theExplicitMPC,LinMPCandNonLinMPCconstructors (explicitmpc.jl:170,linmpc.jl:306,nonlinmpc.jl:452), even whenHpis given, where its only use is a warning.augment_modelchecks observability withControlSystemsBase.observability(Â, Ĉ)(src/estimator/construct.jl:330), which callseigvalsandsvdvals.verify_condcomputescond(H̃)for the condition-number warning (src/controller/construct.jl:857), frominit_quadprog.KalmanCovariances{NT}wraps eachinv!intry/catchto turn aPosDefExceptioninto a clearer error (src/estimator/construct.jl:104–131).
The first one reached is point 2, since the KalmanFilter is built first. ReverseDiff fails the same way, with no eigvals! for tracked numbers. With the checks bypassed, ReverseDiff then hit a StackOverflowError, which I didn't investigate.
Suggested fix
- Run these checks on plain values when the element type isn't a float, e.g. on
ForwardDiff.value.(A), which the package already depends on. Or allow skipping them:augment_modelalready hasverify_obsv, andinit_quadprogalready takeswarn_cond = Inf, but neither reaches the public constructors. And don't callestimate_delayswhenHpis given. - Check positive definiteness with
isposdeforissuccess(cholesky(X; check=false))instead oftry/catch. - Related, for Enzyme: in
init_quadprog(src/controller/construct.jl:842),P̃Δu'*Ñ_Hc*P̃Δuwith a sparseP̃Δuand aHermitianweight uses the three-argument*, which returnsUnion{SparseMatrixCSC, Matrix}. Enzyme then throwsIllegalTypeAnalysisExceptionin every mode. In a minimal example,(P'*N)*Pis type stable and Enzyme differentiates it.
Activity
franckgaga commentedon Nov 18, 2026
Hi thanks for the report. I think it should be supported for
ExplicitMPC. I will take a look at your MRE.PTWaade commentedon Nov 18, 2026
Thank you @franckgaga !
Yes, this allows me to differentiate through ExplicitMPC with ForwardDiff - amazing. Gradients look correct! I need to wrap the construction of it in a with_logger(ERROR_ONLY), but that is doable!
I am using your models for a software package where they are part of a larger model code (alongside other possible models) within a PPL (such as Turing.jl), to reconstruct parameters of the controller from observed behaviour (essentially doing system identification on the controller itself). That means that it is really useful to be able to use different autodiff libraries - I test my package against the main four (ForwardDiff, ReverseDiff, Mooncake and Enzyme).
This works for for ForwardDiff, but I would be excited to be able to differentiate the constructor with the other libraries too. I've made an issue with Enzyme already for an error on their side, but to allow ReverseDiff and Mooncake its necessary to make changes here.
ReverseDiff seems to break in the ControllerWeights constructor. Claude's summary:
ReverseDiff stops at
NT.(M_Hp)in the outerControllerWeightsconstructor. ReverseDiff differentiates a broadcast over its tracked numbers by passing dual numbers through the broadcast function, which here is the tracked number type itself. Converting only the matrices whose element type isn'tNTalready,eltype(M_Hp) == NT ? M_Hp : NT.(M_Hp), makes ReverseDiff's gradient match finite differences too.And for Mooncake, Claude suggests:
Check positive definiteness with isposdef or issuccess(cholesky(X; check=false)) instead of try/catch.
Obviously, if one or both of these is not something you want to implement, that is completely fair (I will mark in my documentation that using your models requires ForwardDiff); but if either of these are easy to implement, I would be very happy!
In any case - thank you again. Let me know if I can do anything!
franckgaga commentedon Nov 18, 2026
Nice application!
I will take a look are the
ReverseDiffsuggestion!MoonCake.jland particularlyEnzyme.jlare very finicky in my experience, and the subset of the Julia language that is supported is quite narrow (e.g. mutation is limited). I won't put too much time on this but if it can be easily solved I will try.franckgaga commentedon Nov 18, 2026
Also, I suppose that constraints are not require for your application, since it is not supported by
ModelPredictiveControl.ExplicitMPC. Did you consider an even simpler LQR controller withControlSystemsBase.lqr?PTWaade commentedon Nov 18, 2026
Thank you - it is very appreciated (and not urgent if you are busy!). And yes, Mooncake and Enzyme can be finicky - I will do what I can to handle issues with them myself :)
Claude reports that if the try-catch is bypassed, Mooncake works. Enzyme has an error on their side that I don't think you have to worry about, I reported that already!
Let me know if I can do anything! When this works, and when I am done with my documentation, I can ping you if you'd like when I've made a tutorial for using your models with CognitiveModeling.jl
PTWaade commentedon Nov 18, 2026
Yeah, my hope is to be able to use as many of your models as I can, depending on the application. Sometimes a simpler model will be enough, and I'm also looking at ControlSystemsBase and DiscretePID. I will have to have a thorough look through the ecosystem; and also find some clear examples where the different models are relevant; but I expect there to be plenty of examples where full model predictive control will be very valuable (which has always not been so easily available to people in my field of cognitive neuroscience before, so its quite exciting to be able to rely on your work for this!)