#198017·pytorch

☂️ Forward-mode and higher-order AD return wrong derivatives

Author: ezyangCreated Sep 21, 2026Updated Sep 25, 2026
Labelsmodule: autogradtriagedmodule: derivativesmodule: correctness (silent)module: forward ad

Umbrella tracking issue for a class of bugs: torch.func.jvp / torch.autograd.forward_ad, nested (second-order) forward AD, jacfwd, and hessian return numerically wrong derivatives for ops whose first-order backward is correct. No error is raised; the wrong number flows into an optimizer, an implicit-layer solve or a physics model and surfaces as a training problem rather than a stack trace.

This is the forward-mode sibling of the eager-vs-compiled correctness umbrellas (#195547, #197554). If the repro only misbehaves under torch.compile, it belongs on oncall: pt2, not here.

The shared root cause

Every member is a per-op formula bug in a hand-written derivative rule (tools/autograd/derivatives.yaml / torch/csrc/autograd/FunctionsManual.cpp), not an infrastructure bug in the forward-AD machinery:

  • a JVP written with the factors in the wrong order for a non-commutative product (cholesky_inverse, cholesky_solve);
  • a second-order term dropped when the JVP rule is itself differentiated (linalg.solve, det, slogdet, householder_product, batch_norm, layer_norm);
  • a rule that is only right on the generic branch and wrong at a boundary the sample inputs never hit (cumprod at a zero factor, scatter_reduce(prod) with a zero, triangular_solve(unitriangular=True), logcumsumexp with an underflowing prefix, sinc / logaddexp at a removable singularity);
  • a custom autograd.Function whose jvp is not re-entered for higher derivatives (#197867).

Each one needs its own derivation and a float64 / complex128 finite-difference check. They are cheap to fix individually, but that is exactly why they keep coming back: the class is under-tested, so each fix only covers the op somebody happened to sweep.

Why CI did not catch these

All of the affected ops declare supports_forward_ad=True and supports_fwgrad_bwgrad=True in their OpInfo, and none of the second-order ones carry a skip. The gap is in what the harness checks:

Order What is tested today Reference
first-order JVP test_forward_mode_AD (gradcheck(check_forward_ad=True)) finite differences
reverse-over-forward test_fn_fwgrad_bwgrad finite differences
reverse-over-reverse test_fn_gradgrad finite differences
forward-over-forward (nested JVP) functorch test_jvpjvpvmap only vmap-vs-loop consistency, no numerical reference, and only over autograd_function_db

So a wrong second-order JVP passes CI as long as it is wrong the same way with and without vmap. The first-order members slipped through a different hole: sample_inputs never exercise the failing regime (zero factors, unitriangular=True, underflowing prefixes), and cholesky_solve has test_forward_mode_AD skipped outright.

What would close the class

Not one PR per op. Per-op fixes are welcome (two are already up) and each member stays open as the record for its reproducer, but the ask that actually closes the class is test infrastructure:

  • an OpInfo test that checks forward-over-forward against a finite-difference reference (gradcheck already has check_forward_ad; add a nested variant or extend gradgradcheck with a check_fwd_over_fwd mode) and turn it on for every op with supports_forward_ad=True;
  • audit ops whose forward-AD tests are skipped or whose sample_inputs avoid the singular / boundary regime, and add error_inputs-style boundary samples for derivative checks;
  • for autograd.Function, a test that jvp composes with itself (jacfwd(jacfwd(f))) over autograd_function_db.

Members

Batch of 2026-09-11 (all forward-AD formula bugs, float64 finite-difference reproducers in each):

  • #196682 — cholesky_inverse JVP places the inverse on the wrong side of the tangent (fix: #197010)
  • #196694 — cholesky_solve propagates an incorrect tangent through the Cholesky factor
  • #196695 — linalg.solve loses a second-order term under nested forward AD
  • #196696 — det second derivative wrong under nested JVP
  • #196697 — linalg.slogdet returns zero instead of a nonzero second-order JVP
  • #196698 — linalg.householder_product nested JVP wrong
  • #196699 — nn.functional.batch_norm second-order JVP wrong
  • #196700 — nn.functional.layer_norm second-order JVP wrong
  • #196701 — cumprod nested JVP wrong at a zero factor
  • #196702 — scatter_reduce(reduce="prod") returns a zero JVP for a nonconstant polynomial
  • #196703 — sinc second derivative is NaN at its removable singularity
  • #196704 — logaddexp second-order AD is NaN for finite logits
  • #196705 — logcumsumexp drops an early-prefix first-order JVP contribution (fix: #196741)
  • #196707 — triangular_solve(unitriangular=True) differentiates the ignored diagonal

Other reports of the same class:

  • #197867 — nested jacfwd through a custom autograd.Function returns 0 for higher derivatives

Already fixed, listed as a prior instance: #196680 (ldexp dropped the derivative contribution of a negative exponent).

The list is seeded from the 2026-09 triage sweep; comment here with a short float64 reproducer and it will be added.

Out of scope

  • Forward AD raising NotImplementedError / "not supported" for an op (a missing rule, not a wrong one), e.g. #182641, #182661, #182842, #191787.
  • Low-precision inf/nan in a derivative where the float64 formula is right (dynamic range, not a formula bug).
  • Wrong derivatives that appear only under torch.compile (e.g. #190855) — oncall: pt2.
  • First-order backward formula bugs; those go on their own tracker.

cc @albanD @gqchen @nikitaved @soulitzer @Varal7 @bobrenjc93