SciML / ModelingToolkitNeuralNets.jl

Symbolic-Numeric Universal Differential Equations for Automating Scientific Machine Learning (SciML)
MIT License
18 stars 1 forks source link

"The supplied DiffCache was too small and was enlarged" in DAE initialization #4

Closed SebastianM-C closed 2 months ago

SebastianM-C commented 3 months ago

Describe the bug 🐞

When we add a UDE component to an existing ODESystem we automatically transform this into a DAE system because we add n algebraic equations for the neural network inputs. This means that we'll always have to use a DAE solver, such as the Rodas family to solve the resulting ODESystem.

When calling

ForwardDiff.gradient(Base.Fix2(of, ps), x0)

We get the following warning

┌ Warning: The supplied DiffCache was too small and was enlarged. This incurs allocations
│     on the first call to `get_tmp`. If few calls to `get_tmp` occur and optimal performance is essential,
│     consider changing 'N'/chunk size of this DiffCache to 12.
└ @ PreallocationTools ~/.julia/packages/PreallocationTools/zJxgA/src/PreallocationTools.jl:191

This comes from the _initialize_dae! function in OrdinaryDiffEq, where a cache is created with

csize = count(algebraic_vars)

I don't yet understand why we get this warning or how we can solve this.

Expected behavior

We should not have any warnings on the calls to gradients.

Minimal Reproducible Example 👇

See the tests.

ChrisRackauckas commented 3 months ago

Where is this DiffCache defined?

SebastianM-C commented 3 months ago

https://github.com/SciML/OrdinaryDiffEq.jl/blob/258bbbbc7458b66c14fe4a03054252d176c9dea8/src/initialize_dae.jl#L528-L538

ChrisRackauckas commented 3 months ago

I think that length just needs to be specialized.