# Taking zygote gradient on function with ! operator fails on gpu

**URL:** <https://discourse.julialang.org/t/taking-zygote-gradient-on-function-with-operator-fails-on-gpu/45481>\
**Category:** Machine Learning\
**Tags:** flux, zygote\
**Created:** [August 25, 2020, 12:59am UTC](https://discourse.julialang.org/t/taking-zygote-gradient-on-function-with-operator-fails-on-gpu/45481 "2020-08-25T00:59:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![reid](https://avatars.discourse-cdn.com/v4/letter/r/439d5e/32.png) [@reid](https://discourse.julialang.org/u/reid)\
**Post date:** [August 25, 2020, 12:59am UTC](https://discourse.julialang.org/t/taking-zygote-gradient-on-function-with-operator-fails-on-gpu/45481/1 "2020-08-25T00:59:17Z")

</div>

ERROR: LoadError: MethodError: no method matching !(::ForwardDiff.Dual{Nothing,Bool,1})

Only happens on gpu, not cpu, which is really confusing me.  
MWE  
using Flux  
masksum(x,y) = sum(.!x \* y)  
gradient(masksum, [true,false,true] |\> gpu, .3)

I would appreciate any assistance. I don’t understand why putting it on the gpu would make it fail, and the error message doesn’t really illuminate.

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [August 25, 2020, 1:07am UTC](https://discourse.julialang.org/t/taking-zygote-gradient-on-function-with-operator-fails-on-gpu/45481/2 "2020-08-25T01:07:57Z")

</div>

Differentiating through boolean does really make sense as your can’t change a bool a little bit. The below works.

```julia
using Flux
masksum(x,y) = sum(1 .- x * y)

gradient(masksum, [1,0,1], .3)

gradient(masksum, [1,0,1] |> gpu, .3)

```

---

<div class="post-metadata">

**Author:** ![reid](https://avatars.discourse-cdn.com/v4/letter/r/439d5e/32.png) [@reid](https://discourse.julialang.org/u/reid)\
**Post date:** [August 25, 2020, 1:39am UTC](https://discourse.julialang.org/t/taking-zygote-gradient-on-function-with-operator-fails-on-gpu/45481/3 "2020-08-25T01:39:15Z")

</div>

True, I’m not using the gradient with respect to the mask, but this was the simplest way to demonstrate.

Thanks! got too into debugging why it was working on cpu but not gpu and stopped looking for workarounds after not(x) = x ? false : true crashed cuda.

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [August 25, 2020, 1:40am UTC](https://discourse.julialang.org/t/taking-zygote-gradient-on-function-with-operator-fails-on-gpu/45481/4 "2020-08-25T01:40:09Z")

</div>

on cpu it returned nothing so… maybe gpu didn’t have logic to deal with nothing.
