# Enzyme gives 0 derivative when I use temporary array

**URL:** https://discourse.julialang.org/t/enzyme-gives-0-derivative-when-i-use-temporary-array/108018
**Category:** New to Julia
**Tags:** question, enzyme
**Created:** [December 24, 2023, 8:37pm UTC](https://discourse.julialang.org/t/enzyme-gives-0-derivative-when-i-use-temporary-array/108018 "2023-12-24T20:37:43Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![reusens](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@reusens](https://discourse.julialang.org/u/reusens)
#### Post date: [December 24, 2023, 8:37pm UTC](https://discourse.julialang.org/t/enzyme-gives-0-derivative-when-i-use-temporary-array/108018/1 "2023-12-24T20:37:43Z")

</div>

I am trying to understand how to use Enzyme. I run the following code:

```julia
using Enzyme

abstract type Layer end

mutable struct parameter
    p::Array
end

mutable struct Final<:Layer
    A::parameter
end

v=Float32.(randn((5,5,5,5)))
input=Float32.(randn((5,5,5,5)))

para=parameter(v)

FL1=Final(para)

function Valuation1(z,Ls1::Array{Float32})
    Ls1[1]=sum(z.A.p.*input)
    Ls=Ls1[1]
    return Ls
end

FL2=deepcopy(FL1)
dzdp=deepcopy(FL1)
dzdp.A.p.=Float32(0)
Enzyme.autodiff(Reverse,Valuation1,Active,Duplicated(FL2,dzdp),Duplicated(Vector{Float32}(undef,1),Vector{Float32}(undef,1)))

println("dzdp",dzdp.A.p[1,1,1,1])

function Valuation2(z,Ls1::Array{Float32})
    return sum(z.A.p.*input)
end

FL2=deepcopy(FL1)
dzdp=deepcopy(FL1)
dzdp.A.p.=Float32(0)
Enzyme.autodiff(Reverse,Valuation2,Active,Duplicated(FL2,dzdp),Duplicated(Vector{Float32}(undef,1),Vector{Float32}(undef,1)))

println("dzdp",dzdp.A.p[1,1,1,1])

```

The first println gives 0. The second one does not. Why does the first println give 0?  
Thank you

Julia Version 1.9.2  
Commit e4ee485e909 (2023-07-05 09:39 UTC)  
Platform Info:  
OS: macOS (arm64-apple-darwin22.4.0)  
CPU: 12 × Apple M2 Max  
WORD\_SIZE: 64  
LIBM: libopenlibm  
LLVM: libLLVM-14.0.6 (ORCJIT, apple-m1)  
Threads: 1 on 8 virtual cores

Enzyme v0.11.11

---

<div class="post-metadata">

### Author: ![reusens](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@reusens](https://discourse.julialang.org/u/reusens)
#### Post date: [December 25, 2023, 9:56am UTC](https://discourse.julialang.org/t/enzyme-gives-0-derivative-when-i-use-temporary-array/108018/2 "2023-12-25T09:56:30Z")

</div>

Actually I saw a similar issue on the GitHub page for enzyme with a recommendation to use Julia version 1.10. I tried this and it works.

---

<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: [December 26, 2023, 9:49am UTC](https://discourse.julialang.org/t/enzyme-gives-0-derivative-when-i-use-temporary-array/108018/3 "2023-12-26T09:49:45Z")

</div>

Glad you were able to make it work! As a side note, your structs use abstract types for their fields (eg. `Array` instead of `Array{T,N}` with parametric `T` and `N`), which is a common caveat for performance. It may not be relevant for your use case, but if you go further, do check out the “performance tips” in the Julia manual to learn more!

---

<div class="post-metadata">

### Author: ![wsmoses](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsmoses/32/26497_2.png) [@wsmoses](https://discourse.julialang.org/u/wsmoses)
#### Post date: [December 27, 2023, 4:42am UTC](https://discourse.julialang.org/t/enzyme-gives-0-derivative-when-i-use-temporary-array/108018/4 "2023-12-27T04:42:01Z")

</div>

You should almost certainly use zeros for your shadow buffers not undef. What happens if you try that.

Also can you post an issue on GitHub?

---

<div class="post-metadata">

### Author: ![reusens](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@reusens](https://discourse.julialang.org/u/reusens)
#### Post date: [December 27, 2023, 7:07pm UTC](https://discourse.julialang.org/t/enzyme-gives-0-derivative-when-i-use-temporary-array/108018/5 "2023-12-27T19:07:31Z")

</div>

Thank you William and Guillaume for your help

William,

After setting the shadow buffers to zero I still get the 0 answer. I submitted an issue on Github. I hope I made it consize enough: I don’t have much experience with Julia or submitting GitHub issues.

If I understand you correctly by submitting the temporary array as an input array and making it duplicated enzyme will now treat this as an other output of the model. I would have to set the temporary array back to some arbitrary number at the end of the calc to cut this link but it probably is nicer to set the shadow buffer to 0.
