# Using Enzyme with DE.jl

**URL:** <https://discourse.julialang.org/t/using-enzyme-with-de-jl/95302>\
**Category:** General Usage\
**Tags:** ad, differentialequation, enzyme\
**Created:** [February 28, 2023, 3:52am UTC](https://discourse.julialang.org/t/using-enzyme-with-de-jl/95302 "2023-02-28T03:52:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomkimpson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomkimpson/32/39168_2.png) [@tomkimpson](https://discourse.julialang.org/u/tomkimpson)\
**Post date:** [February 28, 2023, 3:52am UTC](https://discourse.julialang.org/t/using-enzyme-with-de-jl/95302/1 "2023-02-28T03:52:15Z")

</div>

I am playing with using Enzyme.jl to try to get the derivative of some numerical function with respect to the initial condition. The function is solved using DifferentialEquations.jl. For example:

```julia
using DifferentialEquations
using Enzyme 

"""
The differential equation 
"""
function lorenz!(du,u,p,t)
    du[1] = 10.0*(u[2]-u[1])
    du[2] = u[1]*(28.0-u[3]) - u[2]
    du[3] = u[1]*u[2] - (8/3)*u[3]
    nothing
  end

"""
Given initial conditions a,b,c, integrate the system lorenz!()
"""
function timestepping(a,b,c)

    u0 = [a,b,c] 
    tspan = (0.0,100.0)
   
    prob = ODEProblem(lorenz!,u0,tspan)
    
    ode_solution = solve(prob,Tsit5())

    return ode_solution

end 

"""
An arbitrary loss function that returns the final value of the `a` variable at the end of the integration
"""
function loss_function(a,b,c)

    #Do an integration 
    solution = timestepping(a,b,c)

    #Get just the last element of the first variable as an arbitrary loss 
    critical_value = last(solution[1,:])
    
    return critical_value

end 

a = 1.0
b = 2.0
c = 3.0

#Just run once, make sure everything works OK
first_attempt = loss_function(a,b,c)

#Now try to get a derivative
second_attempt = Enzyme.autodiff(Forward,loss_function,Duplicated,Duplicated(a,1.0),Const(b),Const(c))

```

i.e. I am trying to get the derivative with respect to the initial condition `a`, holding all other variables constant.

However this throws a `LoadError: Tuple{DataType} is not a dispatch tuple` which seems to be related to the `GPUCompiler.specialization_id(job)` call. Can anyone lend some guidance on where I am going wrong here? Thanks!

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [February 28, 2023, 10:29am UTC](https://discourse.julialang.org/t/using-enzyme-with-de-jl/95302/2 "2023-02-28T10:29:23Z")

</div>

You’d want to do Enzyme just on the inside, i.e. EnzymeVJP, unless you’re on Enzyme master (which I assume you’re probably not 😅)

---

<div class="post-metadata">

**Author:** ![tomkimpson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomkimpson/32/39168_2.png) [@tomkimpson](https://discourse.julialang.org/u/tomkimpson)\
**Post date:** [March 8, 2023, 1:55am UTC](https://discourse.julialang.org/t/using-enzyme-with-de-jl/95302/3 "2023-03-08T01:55:09Z")

</div>

Sorry Chris could you expand a little more? What exactly do you mean by “on the inside”?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [March 10, 2023, 1:30pm UTC](https://discourse.julialang.org/t/using-enzyme-with-de-jl/95302/4 "2023-03-10T13:30:11Z")

</div>

> [@tomkimpson](#):
>
> Chris could you expand a little more? What exactly do you mean by “on the inside”?

If you just use Zygote on that it would use Enzyme for the vjps internally. Details in [Sensitivity Algorithms for Differential Equations with Automatic Differentiation (AD) · SciMLSensitivity.jl](https://docs.sciml.ai/SciMLSensitivity/dev/manual/differential_equation_sensitivities/). Enzyme should do this soon but needs the new rules system release.
