Skip to content

src: add AutoSparseDI option for jacobian based on DI - #22

Open
rveltz wants to merge 4 commits into
mainfrom
21-provide-inplace-sparse-jacobian-by-ad
Open

src: add AutoSparseDI option for jacobian based on DI#22
rveltz wants to merge 4 commits into
mainfrom
21-provide-inplace-sparse-jacobian-by-ad

Conversation

@rveltz

@rveltz rveltz commented Mar 21, 2026

Copy link
Copy Markdown
Member

No description provided.

@rveltz rveltz linked an issue Mar 21, 2026 that may be closed by this pull request
Comment thread ext/SparseDIExt.jl
Comment on lines +18 to +21
# we use the following instead of TracerSparsityDetector
# because of searchsortedfirst in POSolution
# sparsity_detector = TracerLocalSparsityDetector(),
sparsity_detector = DI.DenseSparsityDetector(backend, atol = 1e-8),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this makes the sparsity pattern dependent on the actual values inside the vector, which may not be a good idea?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am mostly trying to get this to work for now. Nothing of the above works...
The corresponding test is in test_DI.jl

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you paste the error messages you get?

@rveltz rveltz Mar 23, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When using TracerLocalSparsityDetector in SparseDIExt, I get

ERROR: PreparationMismatchError (inconsistent types between preparation and execution):
  - f!: ✅
  - y: ✅
  - backend: ✅
  - x: ✅
  - contexts: ❌
    - prep: Tuple{Constant{PeriodicOrbitOCollProblem{ConstantDDEBifProblem{BifFunction{typeof(Hutchinson), Nothing, Nothing, Nothing, typeof(JacHutchinson), Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Float64, BifurcationKit.Jet{Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}}, typeof(delaysF), Vector{Float64}, Vector{Float64}, @NamedTuple{a::Float64, d::Int64, τ::Float64, Δ::SparseMatrixCSC{Float64, Int64}, N::Int64}, PropertyLens{:a}, typeof(BifurcationKit.plot_default), typeof(BifurcationKit.record_sol_default), typeof(BifurcationKit.save_solution_default), Float64}, DDEBifurcationKit.AutoSparseDI, Float64, Vector{Float64}, Matrix{Float64}, Nothing}}, Constant{@NamedTuple{a::Float64, d::Int64, τ::Float64, Δ::SparseMatrixCSC{Float64, Int64}, N::Int64}}}
    - exec: Tuple{Constant{@NamedTuple{a::Float64, d::Int64, τ::Float64, Δ::SparseMatrixCSC{Float64, Int64}, N::Int64}}}
If you are confident that this check is superfluous, you can disable it by running preparation with the keyword argument `strict=Val(false)` inside DifferentiationInterface.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like you're trying to detect sparsity through another differentiation operation?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the function is not.

And for TracerSparsityDetector, I get:

ERROR: TypeError: non-boolean (SparseConnectivityTracer.GradientTracer{Int64, BitSet}) used in boolean context
Stacktrace:
  [1] searchsortedfirst
    @ ./sort.jl:177 [inlined]
  [2] searchsortedfirst
    @ ./sort.jl:290 [inlined]
  [3] searchsortedfirst
    @ ./sort.jl:292 [inlined]
  [4] __interpolate_posolution(coll::PeriodicOrbitOCollProblem{…}, t0::SparseConnectivityTracer.GradientTracer{…}, x::Vector{…}, period::Int64)
    @ BifurcationKit ~/work/prog_gd/julia/dev/dev1/bkorg/BifurcationKitGITHUB/src/periodicorbit/PeriodicOrbitCollocation.jl:1173
  [5] functional_coll!(coll::PeriodicOrbitOCollProblem{…}, outc::Base.ReshapedArray{…}, uc::Base.ReshapedArray{…}, period::SparseConnectivityTracer.GradientTracer{…}, ::Tuple{…}, pars::@NamedTuple{}, u::Vector{…})
    @ DDEBifurcationKit ~/work/prog_gd/julia/dev/dev1/bkorg/DDEBifurcationKit.jl/src/periodicorbit/PeriodicOrbitCollocation.jl:70
  [6] residual!(coll::PeriodicOrbitOCollProblem{…}, result::Vector{…}, u::Vector{…}, pars::@NamedTuple{})
    @ DDEBifurcationKit ~/work/prog_gd/julia/dev/dev1/bkorg/DDEBifurcationKit.jl/src/periodicorbit/PeriodicOrbitCollocation.jl:10
  [7] coll_residual_for_di!
    @ ~/work/prog_gd/julia/dev/dev1/bkorg/DDEBifurcationKit.jl/ext/SparseDIExt.jl:8 [inlined]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adrhill this is for you

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +100 to +102
function BK._generate_jacobian(coll::PeriodicOrbitOCollProblem{Tprob}, ::AutoSparseDI, orbitguess, pars; k...) where {Tprob <: AbstractDDEBifurcationProblem}
error("You need to import `DifferentiationInterface, SparseConnectivityTracer, SparseMatrixColorings` in order to use this jacobian")
end

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find it more idiomatic to throw a MethodError instead, perhaps with an error hint like this one https://github.com/JuliaDiff/DifferentiationInterface.jl/blob/main/DifferentiationInterface/src/init.jl

Comment thread Project.toml
Comment on lines +18 to +20
DifferentiationInterface = "a0c0ee7d-e4b9-4e03-894e-1c5f64a51d63"
SparseConnectivityTracer = "9f842d2f-2579-4b1d-911e-f412cf18a3f5"
SparseMatrixColorings = "0a514795-09f3-496d-8182-132a7b665d35"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compat bounds are missing

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.

provide inplace sparse jacobian by AD

3 participants