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

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!

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.

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

did you ever reach an acceptable solution for this?

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

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