# Gradient of the einsum not working with CuArrays, when it's output is assigned to the field of a mutable struct

**URL:** <https://discourse.julialang.org/t/gradient-of-the-einsum-not-working-with-cuarrays-when-its-output-is-assigned-to-the-field-of-a-mutable-struct/34396>\
**Category:** GPU\
**Tags:** machine-learning\
**Created:** [February 10, 2020, 3:00am UTC](https://discourse.julialang.org/t/gradient-of-the-einsum-not-working-with-cuarrays-when-its-output-is-assigned-to-the-field-of-a-mutable-struct/34396 "2020-02-10T03:00:45Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Azamat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/azamat/32/6892_2.png) [@Azamat](https://discourse.julialang.org/u/Azamat)\
**Post date:** [February 10, 2020, 3:00am UTC](https://discourse.julialang.org/t/gradient-of-the-einsum-not-working-with-cuarrays-when-its-output-is-assigned-to-the-field-of-a-mutable-struct/34396/1 "2020-02-10T03:00:46Z")

</div>

I’m getting a strange error when using `OMEinsum`, `CuArrays` and `Zygote` together. Here is the MRE:

```nohighlight
using CuArrays
using Zygote
using OMEinsum

mutable struct S{T}
   c::T
end

function f(α, s, H)
   @ein s.c[d,b] := α[t,b] * H[d,t,b]
   sum(s.c)
end

α = rand(3, 2)
H = rand(5, 3, 2)
s = S(rand(5, 2))

julia> gradient(x -> f(x, s, H), α) # works
([1.8856141333787124 3.177915606920301; 1.7898891125983922 3.2721594554280564; 1.959869250310523 2.247906876515321],)

```

Now if I call `f` with `cu`-ified inputs

```nohighlight
julia> gradient(x -> f(x, S(cu(s.c)), cu(H)), cu(α)) # errors
ERROR: MethodError: no method matching +(::Base.RefValue{Any}, ::CuArray{Float32,2,Nothing})

```

Yet, the forward pass works

```nohighlight
julia> f(cu(α), S(cu(s.c)), cu(H))
8.397266f0

```

Any explanations for what is happening? What would be the workaround here?
