Skip to content

Building an ExplicitMPC from dual numbers fails in eigenvalue checks, so its weights can't be differentiated #523

Open

Description

@PTWaade

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-6

Mooncake 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:

  1. estimate_delays calls eigvals(model.A) (src/controller/construct.jl:586). It is called by the ExplicitMPC, LinMPC and NonLinMPC constructors (explicitmpc.jl:170, linmpc.jl:306, nonlinmpc.jl:452), even when Hp is given, where its only use is a warning.
  2. augment_model checks observability with ControlSystemsBase.observability(Â, Ĉ) (src/estimator/construct.jl:330), which calls eigvals and svdvals.
  3. verify_cond computes cond(H̃) for the condition-number warning (src/controller/construct.jl:857), from init_quadprog.
  4. KalmanCovariances{NT} wraps each inv! in try/catch to turn a PosDefException into 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_model already has verify_obsv, and init_quadprog already takes warn_cond = Inf, but neither reaches the public constructors. And don't call estimate_delays when Hp is given.
  • Check positive definiteness with isposdef or issuccess(cholesky(X; check=false)) instead of try/catch.
  • Related, for Enzyme: in init_quadprog (src/controller/construct.jl:842), P̃Δu'*Ñ_Hc*P̃Δu with a sparse P̃Δu and a Hermitian weight uses the three-argument *, which returns Union{SparseMatrixCSC, Matrix}. Enzyme then throws IllegalTypeAnalysisException in every mode. In a minimal example, (P'*N)*P is type stable and Enzyme differentiates it.

Activity

  1. franckgaga commented on Nov 18, 2026

    @franckgaga
    Member

    Hi thanks for the report. I think it should be supported for ExplicitMPC. I will take a look at your MRE.

  2. PTWaade commented on Nov 18, 2026

    @PTWaade
    Author

    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 outer ControllerWeights constructor. 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't NT already, 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!

  3. franckgaga commented on Nov 18, 2026

    @franckgaga
    Member

    Nice application!

    I will take a look are the ReverseDiff suggestion! MoonCake.jl and particularly Enzyme.jl are 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.

  4. franckgaga commented on Nov 18, 2026

    @franckgaga
    Member

    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 with ControlSystemsBase.lqr?

  5. PTWaade commented on Nov 18, 2026

    @PTWaade
    Author

    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

  6. PTWaade commented on Nov 18, 2026

    @PTWaade
    Author

    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!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions