# Breakpoint/infiltrate inside a gradient calculation (Zygote or ReverseDiff)?

**URL:** <https://discourse.julialang.org/t/breakpoint-infiltrate-inside-a-gradient-calculation-zygote-or-reversediff/104568>\
**Category:** General Usage\
**Tags:** debug, debugging, zygote, reversediff\
**Created:** [October 4, 2023, 10:56am UTC](https://discourse.julialang.org/t/breakpoint-infiltrate-inside-a-gradient-calculation-zygote-or-reversediff/104568 "2023-10-04T10:56:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Qfl3x](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qfl3x/32/16227_2.png) [@Qfl3x](https://discourse.julialang.org/u/Qfl3x)\
**Post date:** [October 4, 2023, 10:56am UTC](https://discourse.julialang.org/t/breakpoint-infiltrate-inside-a-gradient-calculation-zygote-or-reversediff/104568/1 "2023-10-04T10:56:12Z")

</div>

I’m trying to debug why Zygote and ReverseDiff don’t work with MethodOfLines, however, I’m struggling to break inside the gradient calculation. Zygote and ReverseDiff are changing the types of some inputs. Breakpoints in the Debugger aren’t working, and `@infiltrate` will cause precompilation to fail. Simple example:

```julia
using DifferentialEquations, ModelingToolkit, MethodOfLines, DomainSets

using PDEBase: add_metadata!
using ModelingToolkit: get_metadata

using Zygote
using ReverseDiff
import AbstractDifferentiation as AD
using SciMLSensitivity
# Method of Manufactured Solutions: exact solution
u_exact = (x,t) -> exp.(-t) * cos.(x)

# Parameters, variables, and derivatives
@parameters x t
@variables u(..)
@parameters α β
Dt = Differential(t)
Dxx = Differential(x)^2

# 1D PDE and boundary conditions
eq = Dt(u(t, x)) ~(α + β) * Dxx(u(t, x))
bcs = [u(0, x) ~ cos(x),
        u(t, 0) ~ exp(-t),
        u(t, 1) ~ exp(-t) * cos(1)]

# Space and time domains
domains = [t ∈ Interval(0.0, 1.0),
           x ∈ Interval(0.0, 1.0)]

# Parameters
ps = [α => 1.2, β => 2.1]
# PDE system
@named pdesys = PDESystem(eq, bcs, domains, [t, x], [u(t, x)], ps)

# Method of lines discretization
dx = 0.1
order = 2
discretization = MOLFiniteDifference([x => dx], t)

# Convert the PDE problem into an ODE problem
sys,tspan = symbolic_discretize(pdesys,discretization)
simpsys = structural_simplify(sys)
add_metadata!(get_metadata(simpsys), sys)
prob = ODEProblem{true, SciMLBase.FullSpecialize}(simpsys, Pair[], tspan; discretization.kwargs...)

function remake_p(prob::ODEProblem, p; simpsys=simpsys)
    tspan = prob.tspan
    u0 = prob.u0

    _f = prob.f
    ODEProblem{true, SciMLBase.FullSpecialize}(simpsys, u0, tspan, p)#, prob.problem_type )
end

param_vars = [α, β]
idxs = ModelingToolkit.varmap_to_vars([param_vars[1] => 1, param_vars[2] => 2], param_vars)

test_p = [1.2, 1.4]
test_p[Int.(idxs)]
function pde_solution2(ps)
    #_prob = remake_p(prob, ps)
    ps = ps[Int.(idxs)]
    sol = solve(prob, Tsit5(), saveat=0.1, p=ps, wrap=Val(false))
    return sum(Array(sol)[end, :])
end

using Zygote

pde_solution2([1.2,.3]);
ADzyg = AD.ZygoteBackend()
function grad(ps)
    AD.gradient(ADzyg, pde_solution2, ps)
end

grad(rand(2))

```

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [October 4, 2023, 11:50am UTC](https://discourse.julialang.org/t/breakpoint-infiltrate-inside-a-gradient-calculation-zygote-or-reversediff/104568/2 "2023-10-04T11:50:11Z")

</div>

> [@Qfl3x](#):
>
> and `@infiltrate` will cause precompilation to fail

What error are you running into?

---

<div class="post-metadata">

**Author:** ![Qfl3x](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qfl3x/32/16227_2.png) [@Qfl3x](https://discourse.julialang.org/u/Qfl3x)\
**Post date:** [October 4, 2023, 12:05pm UTC](https://discourse.julialang.org/t/breakpoint-infiltrate-inside-a-gradient-calculation-zygote-or-reversediff/104568/3 "2023-10-04T12:05:50Z")

</div>

Revise is throwing `@infiltrate not defined` error when I try to load a package with `@infiltrate` in it, don’t know why I’m thinking precompilation error…

Yes, Infiltrator is in that package’s deps (In Project.toml)

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [October 4, 2023, 12:09pm UTC](https://discourse.julialang.org/t/breakpoint-infiltrate-inside-a-gradient-calculation-zygote-or-reversediff/104568/4 "2023-10-04T12:09:12Z")

</div>

Did you do `using Infiltrate` in addition to adding it as a dependency?

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [October 4, 2023, 12:20pm UTC](https://discourse.julialang.org/t/breakpoint-infiltrate-inside-a-gradient-calculation-zygote-or-reversediff/104568/5 "2023-10-04T12:20:23Z")

</div>

If you use Revise, then it’s not necessary to add Infiltrator to your packages dependencies. Instead I’d recommend putting `Main.@infiltrate` statements into your function.

Also check out the other possibilities for using Infiltrator [here](https://juliadebug.github.io/Infiltrator.jl/stable/#@infiltrate).
