JuliaDiff / ForwardDiff.jl

Forward Mode Automatic Differentiation for Julia
Other
892 stars 148 forks source link

`ForwardDiff.jacobian` throws error for `fft` #670

Open ziyiyin97 opened 1 year ago

ziyiyin97 commented 1 year ago

reproducing script is below

(@v1.8) pkg> activate --temp
  Activating new project at `/var/folders/29/j2y0t06d1918t1tymbv45wgr0000gn/T/jl_T8WfIS`

(jl_T8WfIS) pkg> add ForwardDiff, FFTW
    Updating registry at `~/.julia/registries/General.toml`
   Resolving package versions...
   Installed ChainRulesCore ─ v1.18.0
    Updating `/private/var/folders/29/j2y0t06d1918t1tymbv45wgr0000gn/T/jl_T8WfIS/Project.toml`
  [7a1cc6ca] + FFTW v1.7.1
  [f6369f11] + ForwardDiff v0.10.36
    Updating `/private/var/folders/29/j2y0t06d1918t1tymbv45wgr0000gn/T/jl_T8WfIS/Manifest.toml`
  [621f4979] + AbstractFFTs v1.5.0
  [d360d2e6] + ChainRulesCore v1.18.0
  [9e997f8a] + ChangesOfVariables v0.1.8
  [bbf7d656] + CommonSubexpressions v0.3.0
  [34da2185] + Compat v4.10.0
  [163ba53b] + DiffResults v1.1.0
  [b552c78f] + DiffRules v1.15.1
  [ffbed154] + DocStringExtensions v0.9.3
  [7a1cc6ca] + FFTW v1.7.1
  [f6369f11] + ForwardDiff v0.10.36
  [3587e190] + InverseFunctions v0.1.12
  [92d709cd] + IrrationalConstants v0.2.2
  [692b3bcd] + JLLWrappers v1.5.0
  [2ab3a3ac] + LogExpFunctions v0.3.26
  [1914dd2f] + MacroTools v0.5.11
  [77ba4419] + NaNMath v1.0.2
  [21216c6a] + Preferences v1.4.1
  [189a3867] + Reexport v1.2.2
  [276daf66] + SpecialFunctions v2.3.1
  [90137ffa] + StaticArrays v1.6.5
  [1e83bf80] + StaticArraysCore v1.4.2
  [f5851436] + FFTW_jll v3.3.10+0
  [1d5cc7b8] + IntelOpenMP_jll v2023.2.0+0
  [856f044c] + MKL_jll v2023.2.0+0
  [efe28fd5] + OpenSpecFun_jll v0.5.5+0
  [0dad84c5] + ArgTools v1.1.1
  [56f22d72] + Artifacts
  [2a0f44e3] + Base64
  [ade2ca70] + Dates
  [f43a241f] + Downloads v1.6.0
  [7b1f6079] + FileWatching
  [b77e0a4c] + InteractiveUtils
  [4af54fe1] + LazyArtifacts
  [b27032c2] + LibCURL v0.6.3
  [76f85450] + LibGit2
  [8f399da3] + Libdl
  [37e2e46d] + LinearAlgebra
  [56ddb016] + Logging
  [d6f4376e] + Markdown
  [ca575930] + NetworkOptions v1.2.0
  [44cfe95a] + Pkg v1.8.0
  [de0858da] + Printf
  [3fa0cd96] + REPL
  [9a3f8284] + Random
  [ea8e919c] + SHA v0.7.0
  [9e88b42a] + Serialization
  [6462fe0b] + Sockets
  [2f01184e] + SparseArrays
  [10745b16] + Statistics
  [fa267f1f] + TOML v1.0.0
  [a4e569a6] + Tar v1.10.1
  [8dfed614] + Test
  [cf7118a7] + UUIDs
  [4ec0a83e] + Unicode
  [e66e0078] + CompilerSupportLibraries_jll v0.5.2+0
  [deac9b47] + LibCURL_jll v7.84.0+0
  [29816b5a] + LibSSH2_jll v1.10.2+0
  [c8ffd9c3] + MbedTLS_jll v2.28.0+0
  [14a3606d] + MozillaCACerts_jll v2022.2.1
  [4536629a] + OpenBLAS_jll v0.3.20+0
  [05823500] + OpenLibm_jll v0.8.1+0
  [83775a58] + Zlib_jll v1.2.12+3
  [8e850b90] + libblastrampoline_jll v5.1.1+0
  [8e850ede] + nghttp2_jll v1.48.0+0
  [3f19e933] + p7zip_jll v17.4.0+0
Precompiling project...
  7 dependencies successfully precompiled in 6 seconds. 25 already precompiled.

julia> x = randn(2)
2-element Vector{Float64}:
 -0.04005888157665624
  1.7357331187899907

julia> using ForwardDiff, FFTW

julia> ForwardDiff.jacobian(fft, x)
ERROR: type ForwardDiff.Dual{ForwardDiff.Tag{typeof(fft), Float64}, Float64, 2} not supported
Stacktrace:
  [1] error(s::String)
    @ Base ./error.jl:35
  [2] _fftfloat(#unused#::Type{ForwardDiff.Dual{ForwardDiff.Tag{typeof(fft), Float64}, Float64, 2}})
    @ AbstractFFTs ~/.julia/packages/AbstractFFTs/4iQz5/src/definitions.jl:38
  [3] _fftfloat(x::ForwardDiff.Dual{ForwardDiff.Tag{typeof(fft), Float64}, Float64, 2})
    @ AbstractFFTs ~/.julia/packages/AbstractFFTs/4iQz5/src/definitions.jl:39
  [4] fftfloat(x::ForwardDiff.Dual{ForwardDiff.Tag{typeof(fft), Float64}, Float64, 2})
    @ AbstractFFTs ~/.julia/packages/AbstractFFTs/4iQz5/src/definitions.jl:34
  [5] complexfloat(x::Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(fft), Float64}, Float64, 2}})
    @ AbstractFFTs ~/.julia/packages/AbstractFFTs/4iQz5/src/definitions.jl:47
  [6] fft(x::Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(fft), Float64}, Float64, 2}}, region::UnitRange{Int64})
    @ AbstractFFTs ~/.julia/packages/AbstractFFTs/4iQz5/src/definitions.jl:214
  [7] fft(x::Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(fft), Float64}, Float64, 2}})
    @ AbstractFFTs ~/.julia/packages/AbstractFFTs/4iQz5/src/definitions.jl:66
  [8] vector_mode_dual_eval!(f::typeof(fft), cfg::ForwardDiff.JacobianConfig{ForwardDiff.Tag{typeof(fft), Float64}, Float64, 2, Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(fft), Float64}, Float64, 2}}}, x::Vector{Float64})
    @ ForwardDiff ~/.julia/packages/ForwardDiff/PcZ48/src/apiutils.jl:24
  [9] vector_mode_jacobian(f::typeof(fft), x::Vector{Float64}, cfg::ForwardDiff.JacobianConfig{ForwardDiff.Tag{typeof(fft), Float64}, Float64, 2, Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(fft), Float64}, Float64, 2}}})
    @ ForwardDiff ~/.julia/packages/ForwardDiff/PcZ48/src/jacobian.jl:125
 [10] jacobian(f::Function, x::Vector{Float64}, cfg::ForwardDiff.JacobianConfig{ForwardDiff.Tag{typeof(fft), Float64}, Float64, 2, Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(fft), Float64}, Float64, 2}}}, ::Val{true})
    @ ForwardDiff ~/.julia/packages/ForwardDiff/PcZ48/src/jacobian.jl:0
 [11] jacobian(f::Function, x::Vector{Float64}, cfg::ForwardDiff.JacobianConfig{ForwardDiff.Tag{typeof(fft), Float64}, Float64, 2, Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(fft), Float64}, Float64, 2}}}) (repeats 2 times)
    @ ForwardDiff ~/.julia/packages/ForwardDiff/PcZ48/src/jacobian.jl:19
 [12] top-level scope
    @ REPL[5]:1
mcabbott commented 12 months ago

As it says, FFTW does not support dual numbers. This would need something like #668 to define special methods.