Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eigvals! bug #15

Open
baggepinnen opened this issue May 17, 2023 · 0 comments
Open

eigvals! bug #15

baggepinnen opened this issue May 17, 2023 · 0 comments

Comments

@baggepinnen
Copy link
Contributor

The following example tries to differentiate through eigvals! with both real and complex input arguments. Both fail, but with different errors. ChainRules has rules that should cover both cases

using LinearAlgebra, ForwardDiff, ForwardDiffChainRules, ChainRules
function test_ev(x)
    X = copy(reshape(x, 4, 4))
    X2 = LinearAlgebra.eigvals!(X)
    sum(X2)
end

@ForwardDiff_frule LinearAlgebra.eigvals!(x1::AbstractMatrix{<:ForwardDiff.Dual}; kwargs...)
xr = randn(16)
ForwardDiff.gradient(test_ev, xr)


function test_ev_complex(x)
    X = copy(reshape(x, 4, 4))
    X = X + im*X
    X2 = LinearAlgebra.eigvals!(X)
    sum(X2)
end

@ForwardDiff_frule LinearAlgebra.eigvals!(x1::AbstractMatrix{<:Complex{<:ForwardDiff.Dual}}; kwargs...)
ForwardDiff.gradient(test_ev_complex, xr)
julia> ForwardDiff.gradient(test_ev, xr)
ERROR: MethodError: no method matching Float64(::ForwardDiff.Dual{ForwardDiff.Tag{typeof(test_ev), Float64}, Float64, 8})

Closest candidates are:
  (::Type{T})(::Real, ::RoundingMode) where T<:AbstractFloat
   @ Base rounding.jl:207
  (::Type{T})(::T) where T<:Number
   @ Core boot.jl:792
  (::Type{T})(::AbstractChar) where T<:Union{AbstractChar, Number}
   @ Base char.jl:50
  ...

Stacktrace:
  [1] convert(#unused#::Type{Float64}, x::ForwardDiff.Dual{ForwardDiff.Tag{typeof(test_ev), Float64}, Float64, 8})
    @ Base ./number.jl:7
  [2] ComplexF64(re::ForwardDiff.Dual{ForwardDiff.Tag{typeof(test_ev), Float64}, Float64, 8}, im::ForwardDiff.Dual{ForwardDiff.Tag{typeof(test_ev), Float64}, Float64, 8})
    @ Base ./complex.jl:14
  [3] structfromnt(#unused#::Type{ComplexF64}, x::NamedTuple{(:re, :im), Tuple{ForwardDiff.Dual{ForwardDiff.Tag{typeof(test_ev), Float64}, Float64, 8}, ForwardDiff.Dual{ForwardDiff.Tag{typeof(test_ev), Float64}, Float64, 8}}})
    @ NamedTupleTools ~/.julia/packages/NamedTupleTools/7MQH4/src/NamedTupleTools.jl:118
  [4] (::DifferentiableFlatten.var"#22#24"{ComplexF64})(y::Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(test_ev), Float64}, Float64, 8}})
    @ DifferentiableFlatten ~/.julia/packages/DifferentiableFlatten/ro7xH/src/DifferentiableFlatten.jl:114
  [5] Unflatten
    @ ~/.julia/packages/DifferentiableFlatten/ro7xH/src/DifferentiableFlatten.jl:222 [inlined]
  [6] (::DifferentiableFlatten.var"#3#6"{Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(test_ev), Float64}, Float64, 8}}, Vector{Int64}, Vector{DifferentiableFlatten.Unflatten{ComplexF64, DifferentiableFlatten.var"#22#24"{ComplexF64}}}, Vector{Vector{Float64}}})(n::Int64)
    @ DifferentiableFlatten ./none:0
  [7] iterate
    @ ./generator.jl:47 [inlined]
  [8] collect(itr::Base.Generator{Base.OneTo{Int64}, DifferentiableFlatten.var"#3#6"{Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(test_ev), Float64}, Float64, 8}}, Vector{Int64}, Vector{DifferentiableFlatten.Unflatten{ComplexF64, DifferentiableFlatten.var"#22#24"{ComplexF64}}}, Vector{Vector{Float64}}}})
    @ Base ./array.jl:782
  [9] Vector_from_vec
    @ ~/.julia/packages/DifferentiableFlatten/ro7xH/src/DifferentiableFlatten.jl:57 [inlined]
 [10] eigvals!(x1::Matrix{ForwardDiff.Dual{ForwardDiff.Tag{typeof(test_ev), Float64}, Float64, 8}}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ Main ~/.julia/packages/ForwardDiffChainRules/2Xt9G/src/ForwardDiffChainRules.jl:96
 [11] eigvals!
    @ ~/.julia/packages/ForwardDiffChainRules/2Xt9G/src/ForwardDiffChainRules.jl:70 [inlined]
 [12] test_ev(x::Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(test_ev), Float64}, Float64, 8}})
    @ Main ~/.julia/dev/ControlSystems/docs/src/examples/optimization_based_tuning.jl:275
 [13] chunk_mode_gradient(f::typeof(test_ev), x::Vector{Float64}, cfg::ForwardDiff.GradientConfig{ForwardDiff.Tag{typeof(test_ev), Float64}, Float64, 8, Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(test_ev), Float64}, Float64, 8}}})
    @ ForwardDiff ~/.julia/packages/ForwardDiff/vXysl/src/gradient.jl:123
 [14] gradient(f::Function, x::Vector{Float64}, cfg::ForwardDiff.GradientConfig{ForwardDiff.Tag{typeof(test_ev), Float64}, Float64, 8, Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(test_ev), Float64}, Float64, 8}}}, ::Val{true})
    @ ForwardDiff ~/.julia/packages/ForwardDiff/vXysl/src/gradient.jl:21
 [15] gradient(f::Function, x::Vector{Float64}, cfg::ForwardDiff.GradientConfig{ForwardDiff.Tag{typeof(test_ev), Float64}, Float64, 8, Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(test_ev), Float64}, Float64, 8}}})
    @ ForwardDiff ~/.julia/packages/ForwardDiff/vXysl/src/gradient.jl:17
 [16] gradient(f::Function, x::Vector{Float64})
    @ ForwardDiff ~/.julia/packages/ForwardDiff/vXysl/src/gradient.jl:17
 [17] top-level scope
    @ REPL[16]:1

julia> ForwardDiff.gradient(test_ev_complex, xr)
ERROR: MethodError: no method matching iterate(::Nothing)

Closest candidates are:
  iterate(::Union{LinRange, StepRangeLen})
   @ Base range.jl:880
  iterate(::Union{LinRange, StepRangeLen}, ::Integer)
   @ Base range.jl:880
  iterate(::T) where T<:Union{Base.KeySet{<:Any, <:Dict}, Base.ValueIterator{<:Dict}}
   @ Base dict.jl:698
  ...

Stacktrace:
 [1] indexed_iterate(I::Nothing, i::Int64)
   @ Base ./tuple.jl:91
 [2] eigvals!(x1::Matrix{Complex{ForwardDiff.Dual{ForwardDiff.Tag{typeof(test_ev_complex), Float64}, Float64, 8}}}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ Main ~/.julia/packages/ForwardDiffChainRules/2Xt9G/src/ForwardDiffChainRules.jl:81
 [3] eigvals!
   @ ~/.julia/packages/ForwardDiffChainRules/2Xt9G/src/ForwardDiffChainRules.jl:70 [inlined]
 [4] test_ev_complex(x::Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(test_ev_complex), Float64}, Float64, 8}})
   @ Main ~/.julia/dev/ControlSystems/docs/src/examples/optimization_based_tuning.jl:287
 [5] chunk_mode_gradient(f::typeof(test_ev_complex), x::Vector{Float64}, cfg::ForwardDiff.GradientConfig{ForwardDiff.Tag{typeof(test_ev_complex), Float64}, Float64, 8, Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(test_ev_complex), Float64}, Float64, 8}}})
   @ ForwardDiff ~/.julia/packages/ForwardDiff/vXysl/src/gradient.jl:123
 [6] gradient(f::Function, x::Vector{Float64}, cfg::ForwardDiff.GradientConfig{ForwardDiff.Tag{typeof(test_ev_complex), Float64}, Float64, 8, Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(test_ev_complex), Float64}, Float64, 8}}}, ::Val{true})
   @ ForwardDiff ~/.julia/packages/ForwardDiff/vXysl/src/gradient.jl:21
 [7] gradient(f::Function, x::Vector{Float64}, cfg::ForwardDiff.GradientConfig{ForwardDiff.Tag{typeof(test_ev_complex), Float64}, Float64, 8, Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(test_ev_complex), Float64}, Float64, 8}}})
   @ ForwardDiff ~/.julia/packages/ForwardDiff/vXysl/src/gradient.jl:17
 [8] gradient(f::Function, x::Vector{Float64})
   @ ForwardDiff ~/.julia/packages/ForwardDiff/vXysl/src/gradient.jl:17
 [9] top-level scope
   @ REPL[17]:1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant