# Zygote Type error when using Rodas5() solver but only when manually setting sensealg

**URL:** <https://discourse.julialang.org/t/zygote-type-error-when-using-rodas5-solver-but-only-when-manually-setting-sensealg/84434>\
**Category:** New to Julia\
**Tags:** flux, zygote, diffeqflux\
**Created:** [July 19, 2022, 12:29am UTC](https://discourse.julialang.org/t/zygote-type-error-when-using-rodas5-solver-but-only-when-manually-setting-sensealg/84434 "2022-07-19T00:29:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![infiniteFun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/infinitefun/32/36979_2.png) [@infiniteFun](https://discourse.julialang.org/u/infiniteFun)\
**Post date:** [July 19, 2022, 12:29am UTC](https://discourse.julialang.org/t/zygote-type-error-when-using-rodas5-solver-but-only-when-manually-setting-sensealg/84434/1 "2022-07-19T00:29:02Z")

</div>

I’m dealing with an error here that I’m unable to understand. It would be great if someone more experienced could help me understand what’s going on here.

It happens when I try to do pullback on a loss function with:

1. Rodas5() solver

2. Lux neural network specifying the ODE. When I specify the ODE without Lux the gradients are computed no problem.

3. Setting sensealg manually to InterpolatingAdjoint(; autojacvec=ZygoteVJP()). If I let the solver decide it automatically, even when using a neural network I get no error.

I found something similar in the FAQ ([Frequently Asked Questions · DifferentialEquations.jl](https://diffeq.sciml.ai/stable/basics/faq/)) but I’m not sure where the tmp cache is sneaking in and why the error is so specific.

I’ve attached an MWE below:

```julia
using DifferentialEquations, SciMLSensitivity, Zygote, PreallocationTools, Lux, Random, ComponentArrays

rng = Random.default_rng()
Random.seed!(rng, 0)

basic_tgrad(u, p, t) = zero(u)

############################ Lux NN #################################################

m = Lux.Dense(2, 1, tanh)
ps, st = Lux.setup(rng, m)
ps = ComponentArray(ps)

m(Float32.([1.f0, 0.f0]), ps, st)

############################### Creating ODE Function ###################################

function f(u, p, t)
    du_1 = m(u, p, st)[1]
    du_2 = u[2]
    return [du_1 ; du_2]
end

mass_matrix = Float32.([1.0 0.0; 0.0 0.0])

f_ = ODEFunction{false}(f, mass_matrix=mass_matrix, tgrad=basic_tgrad)

function g(u, p, t)
    du_1 = p * u[1]
    du_2 = u[2]
    return [du_1; du_2]
end

g_ = ODEFunction{false}(g, mass_matrix=mass_matrix, tgrad=basic_tgrad)

################################# Solve/loss Function ###########################################

function solve_de(p; sense=true, func=f_)
    prob = ODEProblem{false}(func, [1.f0, 1.f0], (0.f0, 1.f0), p)
    if sense
        sensealg=InterpolatingAdjoint(; autojacvec=ZygoteVJP())
        return solve(prob, Rodas5(); saveat=0.1, sensealg=sensealg)
    else
        return solve(prob, Rodas5(); saveat=0.1)
    end
end

solve_de(ps)

function loss(p; sense=true, func=f_)
    pred = Array(solve_de(p; sense=sense, func=func))
    return sum(abs2, pred)
end

loss(ps)

##################################### Autograd ###################################

#Lux NN. Sensealg is manual
(l_1,), back_1 = pullback(p -> loss(p), ps)
back_1(one(l_1)) #Error: Expected Float32, Got ForwardDiff.Dual

# Lux NN. Sensealg is auto
(l_2,), back_2 = pullback(p -> loss(p; sense=false), ps)
back_2(one(l_2)) #This works

# No neural net. Sensealg is manual
(l_3,), back_3 = pullback(p -> loss(p; sense=true, func=g_), [2.f0])
back_3(one(l_3)) #This works

# No neural net. Sensealg is auto
(l_4,), back_4 = pullback(p -> loss(p; sense=false, func=g_), [2.f0])
back_4(one(l_4)) #This works

```

I saw that in the tutorial here ([MNIST Classification using NeuralODE - Lux.jl](http://lux.csail.mit.edu/stable/examples/generated/intermediate/NeuralODE/main/)), sensealg was set automatically. I’m assuming since it used the Tsit5() solver there was no error.

What advantages does setting sensealg manually confer? Speed?

Any directions in helping me understand this are appreciated greatly. Thank you very much!

---

<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:** [July 19, 2022, 3:15am UTC](https://discourse.julialang.org/t/zygote-type-error-when-using-rodas5-solver-but-only-when-manually-setting-sensealg/84434/2 "2022-07-19T03:15:25Z")

</div>

Can you open an issue on SciMLSensitivity.jl and share the full stack trace? I think this might be related to Enzyme stuff I need to handle.

---

<div class="post-metadata">

**Author:** ![infiniteFun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/infinitefun/32/36979_2.png) [@infiniteFun](https://discourse.julialang.org/u/infiniteFun)\
**Post date:** [July 20, 2022, 12:23am UTC](https://discourse.julialang.org/t/zygote-type-error-when-using-rodas5-solver-but-only-when-manually-setting-sensealg/84434/3 "2022-07-20T00:23:43Z")

</div>

Hi! Sorry for the slow response. And yes, of course. I’ll do it ASAP.

---

<div class="post-metadata">

**Author:** ![infiniteFun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/infinitefun/32/36979_2.png) [@infiniteFun](https://discourse.julialang.org/u/infiniteFun)\
**Post date:** [July 23, 2022, 1:48am UTC](https://discourse.julialang.org/t/zygote-type-error-when-using-rodas5-solver-but-only-when-manually-setting-sensealg/84434/4 "2022-07-23T01:48:15Z")

</div>

Hi. I’ve created the issue here: [https://github.com/SciML/SciMLSensitivity.jl/issues/694](https://github.com/SciML/SciMLSensitivity.jl/issues/694)
