Support immutable output types in out-of-place scalar→vector gradient#212
Merged
ChrisRackauckas merged 1 commit intoJuliaDiff:masterfrom Apr 16, 2026
Conversation
`finite_difference_gradient` (out-of-place, cached) for scalar `x` previously
delegated to `finite_difference_gradient!` which uses `@. df = result / epsilon`.
This in-place broadcast fails when the output buffer contains immutable array
types (e.g. `ArrayPartition{SVector}` from `SecondOrderODEProblem` in
OrdinaryDiffEq.jl) because `setindex!` is not defined for `SVector`.
Extract the scalar→vector case into `_scalar_gradient_oop` which computes the
finite difference result purely out-of-place using `@. (a - b) / h` (which
allocates a new array via `copy` rather than mutating via `copyto!`).
Fixes SciML/OrdinaryDiffEq.jl#3444
Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.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_gradient(out-of-place, cached) for scalarxpreviously delegated tofinite_difference_gradient!, which uses@. df = result / epsilon. This in-place broadcast callscopyto!→setindex!on the output buffer, which fails for immutable array types likeSVectororArrayPartition{SVector}._scalar_gradient_oopwhich computes the finite difference purely out-of-place using@. (a - b) / h(allocating a new array viacopyrather than mutating viacopyto!).ReadOnlyVecwrapper that blockssetindex!to simulate immutable array types.Motivation
When solving a
SecondOrderODEProblemwithSVectorstate in OrdinaryDiffEq.jl, the solver creates anArrayPartition(SVector, SVector)state. If the solver switches to Rosenbrock23 with finite differences,calc_tderivativecallsDI.derivative→finite_difference_gradient, which allocates a buffer viazero(cache.c1). Sincesimilar(::ArrayPartition{SVector})preserves the immutableSVectorinner parts (unlikesimilar(::SVector)which returnsMVector), the in-place@. df = ...fails withsetindex!(::SVector{N,T}, ...) is not defined.The out-of-place
finite_difference_gradientAPI should never require mutation of the result — it already allocates a return value. This PR makes it compute directly without mutation for the scalar input case.Fixes SciML/OrdinaryDiffEq.jl#3444
Test plan
finite_difference_gradientworks with immutable output arrays (forward and central)SecondOrderODEProblemwithSVectorstate +Rosenbrock23(autodiff=false)now solves successfully🤖 Generated with Claude Code