Fix cache reuse safety in non-allocating finite_difference_jacobian#214
Merged
ChrisRackauckas merged 2 commits intoJuliaDiff:masterfrom Apr 30, 2026
Merged
Conversation
The non-allocating cached `finite_difference_jacobian` was reading the unperturbed components of x from `cache.x1` instead of the input `x`. When a caller (e.g. DifferentiationInterface) builds the cache via `similar(x)` (uninitialized memory) or reuses it at a different x than it was constructed for, the `:central` mode produced wildly wrong Jacobians, while `:forward` and `:complex` were unaffected because they already read from the input directly. The in-place `finite_difference_jacobian!` was already safe due to its upfront `copyto!(x1, x)`. Other caches (`GradientCache`, `HessianCache`, `JVPCache`, `DerivativeCache`) were also already safe and now have regression tests. Fixes JuliaDiff#213. Closes the upstream report at JuliaDiff/DifferentiationInterface.jl#983. Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
Author
|
CI status:
The Downstream failure is |
Two upstream breaking changes had silently broken the Tridiagonal solve test on master since (at least) 2026-04-16: - `Rodas4P` is no longer reexported by the OrdinaryDiffEq meta-package; it now lives in OrdinaryDiffEqRosenbrock. - `autodiff::Bool` is no longer accepted; you must pass an ADTypes specifier such as `AutoFiniteDiff()`. Switch to those new APIs (and update the Project.toml deps) so the downstream test exercises the FiniteDiff path again. The expected gradient value drifts slightly with solver internals between releases, so use a loose `atol=1e-2` — this test is a smoke test that differentiation through a Rosenbrock solver with a Tridiagonal jacobian prototype works, not a tight numeric regression. Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
finite_difference_jacobianwas reading the unperturbed components ofxfromcache.x1(which can be uninitialized when the cache is built viasimilar(x), or stale when the cache is reused at a differentx) instead of from the inputx. This produced junk Jacobians in:centralmode — most visibly viaDifferentiationInterface.jl(seeFiniteDiffwith:centralincludes junk DifferentiationInterface.jl#983).:centraldense and sparse paths perturb aroundvecx(the input) directly. The in-place!version was already safe viacopyto!(x1, x). A defensivecopyto!(cache.x1, x)is added to the non-allocating entry point too (guarded for immutability), so the cache is observably consistent withxafter the call.GradientCache,HessianCache,JVPCachealready update their scratch fields fromxcorrectly.DerivativeCachehas noxcache. New tests pin all of those down to prevent regressions.Reproducer (from JuliaDiff/DifferentiationInterface.jl#983)
Test plan
test/cache_reuse_tests.jl(27 tests) covers:JacobianCachereuse, out-of-place + in-place,:forward/:central/:complex, dense + sparse, including: fresh cache reused at a newx, explicitly-poisoned (fill!(.., 1e10)) cache, and a direct DI-stylesimilar(x)reproduction of #983.GradientCache,JVPCache,HessianCachereuse with poisoned scratch buffers (these were already safe but lacked regression coverage).:centralsparse path: verifiesxis not mutated across the call.Coretest group intest/runtests.jl.@test_broken).🤖 Generated with Claude Code