Warwick Researcher Fixes Silent Gradient Failures in Square-Root Kalman Filters
A new closed-form gradient rule fixes broken autodiff in square-root Kalman filters when covariance matrices are wide or rank-deficient.
ProTakeaways
- New autodiff rule fixes broken gradients in square-root Kalman filters with wide or rank-deficient matrices.
- Insight: filter outputs depend on input only through Gramian MM^T, so loss is smooth.
- Closed-form JVP splits into column-space term via pseudoinverse plus null-space correction.
- Recovers classical QR-based formula exactly when the Cholesky factor is invertible.
- Reverse-mode VJP follows automatically by transposition, no extra derivation needed.
- Open-source JAX implementation available in the cuthbert library.
Anyone who has tried to fit a linear Gaussian state-space model with gradient descent on top of a numerically stable Kalman filter has probably hit a wall: the gradients return NaNs, or worse, silently wrong values. A short paper from Adrien Corenflos at the University of Warwick pinpoints exactly why this happens and offers a closed-form fix that plugs into JAX or PyTorch without additional derivation work.
The paper targets square-root Kalman filters, the numerically stable variant used in long-horizon and parallel-in-time filtering. Its central move is to differentiate the triangularization step through the Gramian identity LL^T = MM^T rather than through the QR decomposition that computes it in practice.
Why square-root filters break autodiff
Standard Kalman filters propagate a state covariance matrix P, but rounding errors over long trajectories can make P lose positive semi-definiteness. Square-root filters avoid this by propagating a lower-triangular factor S with P = S S transpose, and are increasingly used in parallel-in-time implementations. Every predict-update cycle boils down to one primitive: triangularization, which takes a wide matrix M and returns a square lower-triangular L such that LL^T = MM^T. In practice this is done via a thin QR decomposition.
Now the trouble. When M is square and full-rank, differentiating through the QR decomposition is well-defined and has been used in this context. In general, however, two failure modes arise.
- Non-unique Q. When m is greater than n, the map has no unique differential: Q admits a continuous family of valid choices, so there is no canonical Jacobian-vector product for L or M; this occurs in the predict step whenever rank(U) is positive and in the update step whenever d_y is positive.
- Rank-deficient inverses. The standard JVP formula for R involves R inverse, which is singular whenever M is rank-deficient; this arises when U or V is singular, or when the covariance degenerates during filtering.
In plain terms, the moment your process or observation noise covariance has a zero eigenvalue, or your augmented matrix is wider than tall (which it always is in the predict and update steps), the textbook autodiff rule for QR either produces one of infinitely many valid answers or blows up entirely.
The Gramian shortcut
The key observation is deceptively simple. The filter's marginal likelihood, filtered mean, and filtered covariance all depend on L only through the Gramian LL transpose equals MM transpose, so the loss composed with the triangularization factors through the smooth polynomial map from M to MM transpose even though the triangularization itself is not everywhere differentiable.
In other words, even though the QR-based route is non-differentiable, the actual quantity the optimizer cares about (log-likelihood, gain, posterior covariance) is a smooth function of M. You just need a surrogate tangent dL that satisfies the differential of the Gramian identity:
dL L^T + L dL^T = dM M^T + M dM^T
Any dL satisfying this equation yields exact gradients for the log-marginal likelihood and filtered moments. That gives freedom to construct a well-behaved surrogate, and the paper does so by splitting perturbations into two orthogonal pieces.
Column-space plus null-space, in closed form
The construction uses the orthogonal projector P = LL^† onto the column space of L, where L^† is the Moore-Penrose pseudoinverse. The right-hand side of the Gramian differential splits cleanly:
- Column-space component. Absorbed by
dL_col = L [tril(K + K^T) - diag(K)], whereK = L^† dM Q. This is the piece that recovers the classical QR formula whenLis invertible. - Null-space correction.
dL_null = (I - LL^†) dM Qaccounts for perturbations ofMwhose image lies outside the column space ofL. This term vanishes at full rank but is essential otherwise.
When L is invertible, P equals I and the null-space term is zero, recovering the classical QR-based formula. So the new rule is a strict generalization, not a replacement.
Reverse-mode comes for free
One nice side effect is that the reverse-mode gradient (VJP) needs no separate derivation. The map from dM to dL is linear in dM, with the primal quantities held fixed. Because it is a composition of matrix multiplications, tril, and diag, AD frameworks can transpose it mechanically using the "you only linearize once" trick from recent JAX work. That means writing the forward rule is enough; PyTorch and JAX handle backprop automatically.
Does it actually work?
The empirical section is small but pointed. Corenflos uses a 4-dimensional LGSSM with A = 0.9 I_4, a rank-two observation matrix, and a parameterized observation noise V(alpha) = diag([1, alpha^2]) that continuously interpolates from full-rank to rank-one as alpha goes to zero. The naive QR approach diverges and is therefore not reported, while the approach presented here is correct and provides consistent gradients to the log-likelihood. Tangent arrows match the log-likelihood curve across the entire range of alpha, including at the singular endpoint.
Where this matters
If you fit state-space models by maximum likelihood or variational inference and use a square-root filter for stability, this rule quietly removes a class of silent-failure bugs. Concrete situations where the fix pays off:
- Learning parameters when process or observation noise is structurally singular, for example when part of the state is deterministic or observations are noise-free in some dimensions.
- Long-horizon filtering where
P_{t|t}may numerically degenerate during training. - Parallel-in-time square-root filters, where triangularization sits inside a scan that gets differentiated end-to-end.
What it does not do: it does not speed anything up, does not extend to nonlinear filters directly, and does not help if your loss actually depends on the specific Cholesky factor rather than the covariance (a rare case in practice). The result is exact only because Kalman quantities are Gramian-dependent.
Code and availability
The method is implemented in cuthbert, an open-source JAX library for state-space models. Because the JVP is linear and built from standard primitives, dropping the same rule into PyTorch or another framework is straightforward. For the underlying theory, the full HTML version of the paper walks through the proofs in a few pages.