Feature request: add differentiation rule for jax.scipy.linalg.schur (NotImplementedError on jax.grad)
Description
jax.scipy.linalg.schur raises NotImplementedError: Differentiation rule for 'schur' not implemented when used inside jax.grad, making it impossible to differentiate through functions that use Schur decomposition.
Minimal reproducer
import jax
import jax.numpy as jnp
x = jnp.eye(3, dtype=jnp.float32)
f = lambda a: jax.scipy.linalg.schur(a)[0].sum()
jax.grad(f)(x)
# NotImplementedError: Differentiation rule for 'schur' not implemented
Expected behavior
Either a correct gradient via implicit differentiation (as implemented in other frameworks), or a clear error message indicating that differentiation through schur is not supported and suggesting alternatives.
Notes
This also affects any function that internally uses schur, such as jax.scipy.linalg.sqrtm and jax.scipy.linalg.funm.
System info
- jax: 0.10.1
- jaxlib: 0.10.1
- python: 3.12.13
- platform: CPU
Would a PR implementing reverse-mode autodiff for schur be welcome? I’d like to try contributing.
Hi maintainers! I'd love to help tackle this issue to improve the user experience when differentiating through Schur decomposition.
Proposed Approach:
As a first step to resolve the generic NotImplementedError, I can implement an explicit, user-friendly error handler inside jax/scipy/linalg.py that intercepts gradient passes through the schur primitive. This will clearly explain the support limitation and suggest alternative differentiable matrix operations (such as eigh for symmetric structures or svd).
Set up the foundational primitives or structural hooks required to eventually register a custom VJP/JVP rule using implicit differentiation once the mathematical stability limits are defined.
If saikat709 hasn't already begun work on the full reverse-mode math implementation, could you please assign this to me so I can get started on this phased approach?
This is actually a pretty important problem, and it actually comes down to how schur is implemented in practice. It comes straight from LAPACK, which means two things: 1, it quietly lowers to CPU, 2, it cannot go through JAX's autodiff. Efficient Schur decomposition in the general case is an open problem in GPU. If it's a small matrix, you can actually get around this by implementing LAPACK's routines in something like Pallas or CuTe DSL by hand, but this is still very ugly. For large matrices, things get very ugly and the same cannot be said. It's just one of those problems that refuse to parallelize.
I have a working JVP rule for lax.linalg.schur locally and wanted to check the design with you before opening a PR, since it fixes a behavior JAX would have to keep.
Approach. With Ω = Qᴴ dQ (skew-Hermitian), Qᴴ dA Q = dT + ΩT − TΩ. Requiring dT to stay (quasi-)upper-triangular gives a triangular Sylvester-type equation for the strictly lower part of Ω, which I solve row by row with one triangular solve per row (O(n³), checkpointed so reverse mode keeps O(n²) memory). Real 2x2 blocks are reduced to the complex case with a closed-form block-diagonal unitary, and the in-block rotation is fixed so LAPACK's equal-diagonal standardization is preserved. Reverse mode comes from transposition.
Coverage. Real and complex Schur, compute_schur_vectors=False, batching, and second order. Against finite differences the max error is about 5e-9 (real, including 2x2 blocks) and 3e-8 (complex), and the JVP costs 1.6x to 3.3x the primal for n up to 400. The diff is about 100 lines in jax/_src/lax/linalg.py plus five tests in tests/linalg_test.py.
The design question is the gauge. Q is only defined up to column phases (and signs and 2x2 block orientation in the real case). I fix it with diag(Qᴴ dQ) = 0, the same convention as eigh. One thing I didn't expect is that LAPACK's own sign and block orientation choice can flip over 100 times along a 2e-6-wide line in input space, so finite differences of raw schur outputs are unreliable and the tests compare against a gauge-normalized Schur. Is "the derivative of a canonical gauge-fixed branch" an acceptable contract here?
Limitations. Repeated eigenvalues give NaN/inf, like eig and eigh, so the reproducer at jnp.eye(3) still returns NaN (Schur isn't differentiable there). Real normal 2x2 blocks (exact rotations) also give NaN since the in-block rotation becomes a continuous gauge. I could set that rotation's derivative to zero instead, matching the complex phase choice. Functions that are smooth at degenerate points, like sqrtm at 4I, would need their own rules.
@jakevdp would you be open to a PR along these lines? Happy to adjust the gauge handling to whatever you prefer.
@mattjj what do you think?