SciMLBase.jl icon indicating copy to clipboard operation
SciMLBase.jl copied to clipboard

Leaner serialization for solution types

Open ChrisRackauckas opened this issue 1 year ago • 11 comments

The solution types contain functions, and things like JLD2 don't like functions. So it would be nice to have a "strip_solution` function that generates a much leaner form of the solution for simply doing easier serialization. Here's how that should look:

  1. [ ] we should make a new trait for has_lazy_interpolation in https://github.com/SciML/SciMLBase.jl/blob/master/src/alg_traits.jl which is then set to true on BS5 and VernX solvers. strip_solution should then give an informative error message that these problems are not supported by this function. Just some proper error handling, and with that out of the way the other solvers should be free to go.
  2. [ ] https://github.com/SciML/SciMLBase.jl/blob/master/src/solutions/ode_solutions.jl#L102-L118 we can strip out any of the function information from this object. I.e. make a new problem where prob and alg are nothing.
  3. [ ] Downstream, sol.interp holds some extra information as well. So in SciMLBase we need strip_interp(::AbstractInterpolation). The SciMLBase ones are just the identity function, so that's easy https://github.com/SciML/SciMLBase.jl/blob/master/src/interpolation.jl .
  4. [ ] At this point any non-OrdinaryDiffEq algorithm will strip well. The only downstream interp to handle is the OrdinaryDiffEq one. https://github.com/SciML/OrdinaryDiffEq.jl/blob/master/src/interp_func.jl#L4-L15 . f is only in there for the lazy interpolations, so set it to nothing. You need to keep the caches because that's used for dispatch, but at this point you're done with explicit methods.
  5. [ ] For implicit methods, you need to strip https://github.com/SciML/OrdinaryDiffEq.jl/blob/master/src/caches/rosenbrock_caches.jl#L30-L33 jac_config and grad_config, as those contain function information. As a simple thing, you can just create a new cache with everything nothing though since this is only used for dispatch (and addsteps!, but since you know these caches are non-lazy the interpolation addsteps! post solution is trivial so it won't use anything in the cache). We need to make good functionality for this anyways for better default solvers (CC @oscardssmith), so it might be a good time to do this now.

With that done strip_solution(sol) should return a lean solution with no function information in it (or rather, any function information in it is in SciMLBase, which also defines the structure so that's a required import) and so it should work just fine with any BSON, JLD2, etc. package.

ChrisRackauckas avatar Feb 22 '24 12:02 ChrisRackauckas

For the first item, should it be has_lazy_interpolation(alg::AbstractDEAlgorithm) = false,? Then e.g. has_lazy_interpolation(alg::Vern6) = true, would that go somewhere in here : https://github.com/SciML/OrdinaryDiffEq.jl/tree/07470f95ab5d76864e2d1565468305459bf39b00/lib/OrdinaryDiffEqVerner/src ?

jClugstor avatar Aug 07 '24 13:08 jClugstor

yes, and traits go in alg_utils.jl, you'll see one in that folder.

ChrisRackauckas avatar Aug 07 '24 13:08 ChrisRackauckas

It's just Verner and BS5 (low order RK)

ChrisRackauckas avatar Aug 07 '24 13:08 ChrisRackauckas

Where should the strip functions go? SciMLBase utils.jl ?

jClugstor avatar Aug 07 '24 14:08 jClugstor

BTW I created the trait https://github.com/SciML/OrdinaryDiffEq.jl/pull/2313/commits/9734f4948c7ff465fedd428a09dde7179574d791 since I ended up needing it. It needs to get raised to the SciMLBase level.

Where should the strip functions go? SciMLBase utils.jl ?

yes that's fine.

ChrisRackauckas avatar Aug 07 '24 15:08 ChrisRackauckas

This adds a has_lazy_interpolation trait to SciMLBase, and sets strip_interpolation for the SciMLBase interpolations to identity, since they don't have functions in them. https://github.com/SciML/SciMLBase.jl/pull/761

This adds strip_interpolation and strip_solution functions for the stuff in OrdinaryDiffEq. They basically copy the objects, just setting the function field to nothing. https://github.com/SciML/OrdinaryDiffEq.jl/pull/2316

Now I need to make a strip_solution for SciMLSolution, and work on the implicit method stuff.

jClugstor avatar Aug 07 '24 19:08 jClugstor

https://github.com/SciML/OrdinaryDiffEq.jl/blob/master/src/caches/rosenbrock_caches.jl doesn't exist it seems Did you mean the caches here https://github.com/SciML/OrdinaryDiffEq.jl/blob/master/src/caches/basic_caches.jl?

Or these caches for each implicit solver, e.g. https://github.com/SciML/OrdinaryDiffEq.jl/blob/master/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl ?

jClugstor avatar Aug 08 '24 19:08 jClugstor

The latter

ChrisRackauckas avatar Aug 09 '24 00:08 ChrisRackauckas

It looks like https://github.com/SciML/OrdinaryDiffEq.jl/pull/2320 will combine the cache types, so I'll only need to deal with RosenbrockConstantCache and RosenbrockCache, right?

jClugstor avatar Aug 13 '24 19:08 jClugstor

Also, is there any way to tell whether an algorithm is implicit/has caches, other than looking at the docs? Would that be worth having as a trait?

jClugstor avatar Aug 13 '24 19:08 jClugstor

It looks like https://github.com/SciML/OrdinaryDiffEq.jl/pull/2320 will combine the cache types, so I'll only need to deal with RosenbrockConstantCache and RosenbrockCache, right?

right

oscardssmith avatar Aug 13 '24 21:08 oscardssmith

@jClugstor what's the next step here?

ChrisRackauckas avatar Aug 18 '24 01:08 ChrisRackauckas

Figuring out how to handle the explicit and implicit solvers seperately, since the Rosenbrock caches have some function information. It looks like there are imminent changes to the Rosenbrock caches so I was planning on waiting on those before trying to implement the stripping.

jClugstor avatar Aug 18 '24 02:08 jClugstor

Alright, but do the other methods first?

ChrisRackauckas avatar Aug 18 '24 03:08 ChrisRackauckas

@jClugstor are you adding integration tests today?

ChrisRackauckas avatar Aug 27 '24 14:08 ChrisRackauckas

I was just going to ask about this.

  • Should strip_solution be exported ?
  • What kind of integration tests do I need to add? A test to check that serialization with JLD2 and BSON works after strip_solution ?

jClugstor avatar Aug 27 '24 14:08 jClugstor

  1. Probably not.
  2. Downstream, just show that when you strip the interpolation with OrdinaryDiffEq that you have no functions inside of there. Can be in OrdinaryDiffEq.

ChrisRackauckas avatar Aug 27 '24 15:08 ChrisRackauckas

Just needs docs. @jClugstor add to the solution interface page in DiffEqDocs

ChrisRackauckas avatar Aug 28 '24 15:08 ChrisRackauckas