Wrap Jacobian with both dense and sparse FunctionWrapper signatures#1288
Merged
ChrisRackauckas merged 1 commit intoSciML:masterfrom Feb 23, 2026
Merged
Conversation
39667c5 to
11589a2
Compare
3 tasks
When an ODEFunction has a sparse sparsity pattern but no jac_prototype,
the solver's build_J_W may create a SparseMatrixCSC J from the sparsity
pattern (when using AutoSparse). The previous code only wrapped the
Jacobian with a dense Matrix{Float64} signature, causing "No matching
function wrapper was found!" at solve time.
Now detects non-dense sparsity patterns and creates a FunctionWrappersWrapper
with both dense and sparse matrix signatures so the Jacobian call works
regardless of which matrix type the solver allocates.
All type computations use Base.promote_op to avoid allocating prototype
matrices just for their types.
Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
11589a2 to
dea4704
Compare
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
ODEFunctionhas a sparse sparsity pattern (f.sparsity isa AbstractMatrixwith non-dense type) but nojac_prototype, the solver'sbuild_J_Wmay create aSparseMatrixCSCJ from the sparsity pattern (when usingAutoSparse).promote_fonly wrapped the Jacobian with a denseMatrix{Float64}signature in theFunctionWrappersWrapper, causing "No matching function wrapper was found!" at solve time.FunctionWrappersWrapperwith both dense and sparse matrix signatures, so the Jacobian call works regardless of which matrix type the solver allocates.Context
This fixes a failure in
ModelingToolkit.jltests when usingAutoSpecialize(the new default) withjac=true, sparsity=trueand an implicit solver likeRodas5(). See: SciML/ModelingToolkit.jl#4335The root cause is that
OrdinaryDiffEqDifferentiation'sbuild_J_Wcreates a sparse J from the sparsity pattern whenalg_autodiff(alg) isa AutoSparse, even whenjac_prototypeisnothing. The FunctionWrapper was only registered for the dense(Matrix{Float64}, Vector{Float64}, Vector{Float64}, Float64)signature, so the call withSparseMatrixCSCfailed.Test plan
jac=true, sparsity=true, Rodas5()on AutoSpecialize ODEProblem@inferred remakeand@inferred solvestill passprob1(no jac) andprob2(jac, no sparsity) still work🤖 Generated with Claude Code