Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autodiff in default algorithm #1027

Open
ArnoStrouwen opened this issue Apr 21, 2024 · 5 comments
Open

autodiff in default algorithm #1027

ArnoStrouwen opened this issue Apr 21, 2024 · 5 comments

Comments

@ArnoStrouwen
Copy link
Member

Is it possible to do a try/catch on the evaluation of the Jac to turn on autodiff?
https://github.com/SciML/DifferentialEquations.jl/blob/master/src/ode_default_alg.jl#L49

@ChrisRackauckas
Copy link
Member

try/catch always has a ton of overhead so that's not good for the default method. It only works in the adjoints because the cost of adjoint differentiation is already so high when it makes sense, so the overhead is always negligible. For many default ODEs, you'd just hit a standard explicit RK method and in those cases the try/catch would be noticeable but never used.

Maybe the right thing to do would be Core.return_type and check if it's a Union{}. That would tell you if it's provable from the types to error? SciML/OrdinaryDiffEq.jl#2103 should merge this week, and with that we may want to reconsider how this could be done @oscardssmith

@oscardssmith
Copy link
Contributor

I'm really not sure why you think try catch has so much overhead. I measure it as ~10ns in the case where no exception is thrown.

julia> function f(n)
           try
               return n[]+1
           catch
           end
       end
f (generic function with 1 method)

julia> @btime f(Any[100])
  34.174 ns (1 allocation: 64 bytes)
101

julia> function g(n)
           return n[]+1
       end
g (generic function with 1 method)

julia> @btime g(Any[100])
  24.751 ns (1 allocation: 64 bytes)
101

@ChrisRackauckas
Copy link
Member

Oh okay, then maybe in the new form we can make the AD more dynamic? Though in future versions when using ADTypes with DI this might be an issue. @gdalle something to consider.

@oscardssmith
Copy link
Contributor

Also worth pointing out that catching an error is pretty expensive.

julia> @btime f(Any["hi"])
  20.599 μs (3 allocations: 128 bytes)

@gdalle
Copy link

gdalle commented Apr 29, 2024

Oh okay, then maybe in the new form we can make the AD more dynamic? Though in future versions when using ADTypes with DI this might be an issue.

Probably related to JuliaDiff/DifferentiationInterface.jl#164

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

No branches or pull requests

4 participants