Skip to content

Add batch/vectorized finite difference Jacobian evaluation#211

Open
ChrisRackauckas-Claude wants to merge 1 commit into
JuliaDiff:masterfrom
ChrisRackauckas-Claude:batch-jacobian-evaluation
Open

Add batch/vectorized finite difference Jacobian evaluation#211
ChrisRackauckas-Claude wants to merge 1 commit into
JuliaDiff:masterfrom
ChrisRackauckas-Claude:batch-jacobian-evaluation

Conversation

@ChrisRackauckas-Claude
Copy link
Copy Markdown

Summary

Closes #210.

  • Adds batch=true keyword argument to finite_difference_jacobian and finite_difference_jacobian!
  • When batch=true, the function f receives a matrix where each column is an input point, and should return a matrix where each column is the corresponding output
  • For the in-place variant, f(FX, X) receives output matrix FX and input matrix X
  • All perturbations are evaluated in a single call to f, enabling GPU-parallelized or otherwise vectorized evaluation
  • Supports forward, central, and complex step methods

Implementation details

Out-of-place (finite_difference_jacobian):

  • Forward: builds n×(n+1) matrix (base + n perturbations) or n×n if f_in provided → 1 call to f
  • Central: builds n×2n matrix (forward + backward perturbations) → 1 call to f
  • Complex: builds complex n×n matrix → 1 call to f

In-place (finite_difference_jacobian!):

  • Same matrix construction, but f(FX, X) fills the pre-allocated output matrix

Example usage

# Out-of-place: f accepts matrix, returns matrix (columns = eval points)
function f_batch(X::AbstractMatrix)
    hcat([f_scalar(X[:, j]) for j in 1:size(X, 2)]...)
end

J = finite_difference_jacobian(f_batch, x; batch=true)

# In-place: f(FX, X) fills FX
function f_batch!(FX::AbstractMatrix, X::AbstractMatrix)
    for j in 1:size(X, 2)
        FX[:, j] .= f_scalar(X[:, j])
    end
end

finite_difference_jacobian!(J, f_batch!, x; batch=true)

Test plan

  • Out-of-place batch Jacobian matches reference for forward, central, complex step
  • In-place batch Jacobian matches reference for forward, central, complex step
  • Batch results match non-batch results on a 3→3 function
  • All existing tests still pass (core + coloring + out-of-place)
  • CI passes

🤖 Generated with Claude Code

@gdalle gdalle requested a review from ChrisRackauckas May 10, 2026 09:05
@gdalle
Copy link
Copy Markdown
Member

gdalle commented May 10, 2026

Adding a note to draw inspiration from BatchSolve.jl by @mattsignorelli. In particular, the file batch-ad/finitediff.jl seems relevant

Implements feature requested in JuliaDiff#210: allows computing the full Jacobian
in a single batched function call instead of N sequential calls. This is
useful for GPU-parallelized functions that can evaluate multiple inputs
simultaneously.

Adds `batch=true` keyword to `finite_difference_jacobian` and
`finite_difference_jacobian!`. When enabled, `f` receives a matrix where
each column is an input point and returns a matrix of outputs. Supports
forward, central, and complex step methods.

Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@ChrisRackauckas-Claude ChrisRackauckas-Claude force-pushed the batch-jacobian-evaluation branch from 689b74a to 8cfcfd2 Compare May 12, 2026 11:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: vectorized finite difference evaluation

3 participants