# Help debugging an error with Zygote

**URL:** <https://discourse.julialang.org/t/help-debugging-an-error-with-zygote/50411>\
**Category:** General Usage\
**Tags:** question, differentiation, zygote\
**Created:** [November 19, 2020, 2:30am UTC](https://discourse.julialang.org/t/help-debugging-an-error-with-zygote/50411 "2020-11-19T02:30:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ElOceanografo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eloceanografo/32/624_2.png) [@ElOceanografo](https://discourse.julialang.org/u/ElOceanografo)\
**Post date:** [November 19, 2020, 2:30am UTC](https://discourse.julialang.org/t/help-debugging-an-error-with-zygote/50411/1 "2020-11-19T02:30:30Z")

</div>

Hoping someone who understands autodiff better than I do can help me figure out what’s going on here. I’ve got a function that calls `ldl` from LDLFactorizations.jl, and then does further calculations on a field of the `LDLFactorization` object that’s returned. Taking the gradient of this function works with ForwardDiff and ReverseDiff, but errors for Zygote. MWE below:

```julia
using LinearAlgebra, LDLFactorizations
using ForwardDiff, ReverseDiff, Zygote

function foo(x)
    A = diagm(0 => x)
    F = ldl(A)
    return sum(F.D)
end

x = [1.0, 2.0, 3.0]
ForwardDiff.gradient(foo, x) # works
ReverseDiff.gradient(foo, x) # works
Zygote.gradient(foo, x) # errors

```

The error seems to say there’s no field `D` (there is) and that the object is a `NamedTuple` (it isn’t):

```julia
ERROR: type NamedTuple has no field D
Stacktrace:
 [1] (::Zygote.var"#back#158"{:D,Zygote.Context,LDLFactorizations.LDLFactorization{Float64,Int64,Int64,Int64},Diagonal{Float64,Array{Float64,1}}})(::FillArrays.Fill{Float64,2,Tuple{Base.OneTo{Int64},Base.OneTo{Int64}}}) at /home/sam/.julia/packages/Zygote/c0awc/src/lib/lib.jl:220
 [2] (::Zygote.var"#1704#back#159"{Zygote.var"#back#158"{:D,Zygote.Context,LDLFactorizations.LDLFactorization{Float64,Int64,Int64,Int64},Diagonal{Float64,Array{Float64,1}}}})(::FillArrays.Fill{Float64,2,Tuple{Base.OneTo{Int64},Base.OneTo{Int64}}}) at /home/sam/.julia/packages/ZygoteRules/6nssF/src/adjoint.jl:49
 [3] foo at ./untitled-42379960c5e23fc7ce2e780a42d5b5f9:7 [inlined]
 [4] (::typeof(∂(foo)))(::Float64) at /home/sam/.julia/packages/Zygote/c0awc/src/compiler/interface2.jl:0
 [5] (::Zygote.var"#41#42"{typeof(∂(foo))})(::Float64) at /home/sam/.julia/packages/Zygote/c0awc/src/compiler/interface.jl:45
 [6] gradient(::Function, ::Array{Float64,1}) at /home/sam/.julia/packages/Zygote/c0awc/src/compiler/interface.jl:54
 [7] top-level scope at none:1

```

This isn’t super critical, since I can use one of the other autodiff libraries, but it did make me curious what’s going on. Thanks in advance if anyone has insights!

---

<div class="post-metadata">

**Author:** ![ElOceanografo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eloceanografo/32/624_2.png) [@ElOceanografo](https://discourse.julialang.org/u/ElOceanografo)\
**Post date:** [November 19, 2020, 5:37pm UTC](https://discourse.julialang.org/t/help-debugging-an-error-with-zygote/50411/2 "2020-11-19T17:37:54Z")

</div>

I think I may have figured it out, at least partially. The diagonal matrix `D` is not actually part of the `LDLFactorization` object; it gets created by lazily by a custom `getproperty` method [here](https://github.com/JuliaSmoothOptimizers/LDLFactorizations.jl/blob/bdb2a2885c668f605a83067134e8410d9bc3d400/src/LDLFactorizations.jl#L530). Still not clear why Zygote is the only AD that fails here, though…
