# Flux: Error when computing gradient of broadcasted ifelse on GPU

**URL:** <https://discourse.julialang.org/t/flux-error-when-computing-gradient-of-broadcasted-ifelse-on-gpu/43907>\
**Category:** Machine Learning\
**Tags:** flux, zygote\
**Created:** [July 29, 2020, 5:52pm UTC](https://discourse.julialang.org/t/flux-error-when-computing-gradient-of-broadcasted-ifelse-on-gpu/43907 "2020-07-29T17:52:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![raff](https://avatars.discourse-cdn.com/v4/letter/r/5e9695/32.png) [@raff](https://discourse.julialang.org/u/raff)\
**Post date:** [July 29, 2020, 5:52pm UTC](https://discourse.julialang.org/t/flux-error-when-computing-gradient-of-broadcasted-ifelse-on-gpu/43907/1 "2020-07-29T17:52:06Z")

</div>

Here is a code example

```julia
X = rand(3,3) .- 0.4
X = X |> cu
f(X) = sum(ifelse.(X .> 0, X, 1f0))
gradient(f, X)

```

And here is the error message

```julia
ERROR: TypeError: non-boolean (ForwardDiff.Dual{Nothing,Bool,3}) used in boolean context

```

If `X` is a CPU array, then it works fine, but not when `X` is a `CuArray`.  
Any suggestion on fixing the issue?

Thanks

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [July 30, 2020, 4:30pm UTC](https://discourse.julialang.org/t/flux-error-when-computing-gradient-of-broadcasted-ifelse-on-gpu/43907/2 "2020-07-30T16:30:22Z")

</div>

That `ifelse` looks somewhat suspect, try extracting it as an element-wise function:

```julia
weirdrelu(x) = ifelse(x > 0, x, one(x))
f(X) = sum(weirdrelu.(X))
gradient(f, X)

```

Returns `([1, 1, 1],)` as expected on the latest CUDA.jl.
