# ForwardDiff and Zygote cannot automatically differentiate (AD) function from C^n to R that uses FFT

**URL:** <https://discourse.julialang.org/t/forwarddiff-and-zygote-cannot-automatically-differentiate-ad-function-from-c-n-to-r-that-uses-fft/52440>\
**Category:** Numerics\
**Created:** [December 27, 2020, 2:42am UTC](https://discourse.julialang.org/t/forwarddiff-and-zygote-cannot-automatically-differentiate-ad-function-from-c-n-to-r-that-uses-fft/52440 "2020-12-27T02:42:04Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [January 7, 2021, 9:32pm UTC](https://discourse.julialang.org/t/forwarddiff-and-zygote-cannot-automatically-differentiate-ad-function-from-c-n-to-r-that-uses-fft/52440/21 "2021-01-07T21:32:28Z")

</div>

Sorry about the `complexfloat` (copy-paste?) mistake.

`extract(::Array{Complex{Float64},1})` doesn’t sound good, I think it should get an array with dual numbers. If these have gone missing along the way then something is wrong!

---

<div class="post-metadata">

**Author:** ![tholdem](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tholdem/32/20563_2.png) [@tholdem](https://discourse.julialang.org/u/tholdem)\
**Post date:** [January 8, 2021, 8:35pm UTC](https://discourse.julialang.org/t/forwarddiff-and-zygote-cannot-automatically-differentiate-ad-function-from-c-n-to-r-that-uses-fft/52440/22 "2021-01-08T20:35:44Z")

</div>

No worries at all! I’m trying to debug this and realized that right now `fft(x_plus_dx)` actually gives me complex numbers instead of a complex dual but `xtil_plus` is a complex dual yet `xtil_plus == fft(x_plus_dx)` still returns `true`. That shouldn’t happen, right? Is that not the case for you? I see that this code defines how to apply plan to complex duel, but I’m not sure why it’s not returning complex dual for me.

```julia
function Base.:*(p::AbstractFFTs.Plan, x::AbstractArray{<:Complex{<:ForwardDiff.Dual}})
    xtil = p * ForwardDiff.value.(x)
    ndxs = ForwardDiff.npartials(first(x))
    dxtils = ntuple(ndxs) do n
        p * ForwardDiff.partials.(x, n)
    end
    # dxtils = (dx1til, dx2til)
    ndxs == 2 || error("this won't yet work for npartials(x) != 2, sorry")
    dx1til, dx2til = dxtils
    @. Complex(
        ForwardDiff.Dual(real(xtil), tuple(real(dx1til), real(dx2til))),
        ForwardDiff.Dual(imag(xtil), tuple(imag(dx1til), imag(dx2til))),
        )
end

```

---

<div class="post-metadata">

**Author:** ![AshtonSBradley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ashtonsbradley/32/11140_2.png) [@AshtonSBradley](https://discourse.julialang.org/u/AshtonSBradley)\
**Post date:** [March 22, 2022, 3:13am UTC](https://discourse.julialang.org/t/forwarddiff-and-zygote-cannot-automatically-differentiate-ad-function-from-c-n-to-r-that-uses-fft/52440/23 "2022-03-22T03:13:46Z")

</div>

did you ever reach an acceptable solution for this?

---

<div class="post-metadata">

**Author:** ![tictaccat](https://avatars.discourse-cdn.com/v4/letter/t/9f8e36/32.png) [@tictaccat](https://discourse.julialang.org/u/tictaccat)\
**Post date:** [March 25, 2022, 6:28pm UTC](https://discourse.julialang.org/t/forwarddiff-and-zygote-cannot-automatically-differentiate-ad-function-from-c-n-to-r-that-uses-fft/52440/24 "2022-03-25T18:28:24Z")

</div>

Looks like you’re looking for ForwardDiff, not backward diff, so I don’t the below is super relevant, sorry (although it should still be the case, even for forward differentiation, that the plan type doesn’t give you enough information)

Haven’t looked super closely at this thread, but I was getting the same error as the original post and I think this should patch it up as long as you’re not doing rfft’s? [https://github.com/FluxML/Zygote.jl/pull/1173](https://github.com/FluxML/Zygote.jl/pull/1173)

To differentiate rfft plans properly though, I think that requires changes to the plan types in AbstractFFTs? [https://github.com/JuliaMath/AbstractFFTs.jl/issues/63](https://github.com/JuliaMath/AbstractFFTs.jl/issues/63)

[Previous page](https://discourse.julialang.org/t/forwarddiff-and-zygote-cannot-automatically-differentiate-ad-function-from-c-n-to-r-that-uses-fft/52440.md?page=1)
