JuliaDiff / JuliaDiff/ForwardDiff.jl

#481 possibly broke `ishermitian`

Open
#606 16 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
1k
Forks
160
PR merge metrics
No merged PRs in 30d

Description

After #481 , ishermitian is now broken (maybe other methods too?) when working with ForwardDiff.Dual.

julia> Pkg.status()
Status `/tmp/jl_gs3tnK/Project.toml`
  [f6369f11] ForwardDiff v0.10.33
  [37e2e46d] LinearAlgebra

julia> using LinearAlgebra, ForwardDiff

julia> x = [1 0.5; 0.5 1]
2×2 Matrix{Float64}:
 1.0  0.5
 0.5  1.0

julia> ishermitian(x)
true

julia> f(x) = sum(cholesky(reshape(x, 2, 2)))
f (generic function with 1 method)

julia> ForwardDiff.gradient(vec(x))
ERROR: MethodError: no method matching gradient(::Vector{Float64})
Closest candidates are:
  gradient(::Any, ::StaticArraysCore.StaticArray) at ~/.julia/packages/ForwardDiff/eqMFf/src/gradient.jl:44
  gradient(::Any, ::StaticArraysCore.StaticArray, ::ForwardDiff.GradientConfig) at ~/.julia/packages/ForwardDiff/eqMFf/src/gradient.jl:45
  gradient(::Any, ::StaticArraysCore.StaticArray, ::ForwardDiff.GradientConfig, ::Val) at ~/.julia/packages/ForwardDiff/eqMFf/src/gradient.jl:46
  ...
Stacktrace:
 [1] top-level scope
   @ REPL[10]:1

julia> ForwardDiff.gradient(f, vec(x))
ERROR: PosDefException: matrix is not Hermitian; Cholesky factorization failed.
Stacktrace:
  [1] checkpositivedefinite(info::Int64)
    @ LinearAlgebra /opt/julia-1.8.2/share/julia/stdlib/v1.8/LinearAlgebra/src/factorization.jl:18
  [2] cholesky!(A::Matrix{ForwardDiff.Dual{ForwardDiff.Tag{typeof(f), Float64}, Float64, 4}}, ::NoPivot; check::Bool)
    @ LinearAlgebra /opt/julia-1.8.2/share/julia/stdlib/v1.8/LinearAlgebra/src/cholesky.jl:299
  [3] #cholesky#162
    @ /opt/julia-1.8.2/share/julia/stdlib/v1.8/LinearAlgebra/src/cholesky.jl:402 [inlined]
  [4] cholesky (repeats 2 times)
    @ /opt/julia-1.8.2/share/julia/stdlib/v1.8/LinearAlgebra/src/cholesky.jl:402 [inlined]
  [5] f(x::Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(f), Float64}, Float64, 4}})
    @ Main ./REPL[9]:1
  [6] vector_mode_dual_eval!
    @ ~/.julia/packages/ForwardDiff/eqMFf/src/apiutils.jl:37 [inlined]
  [7] vector_mode_gradient(f::typeof(f), x::Vector{Float64}, cfg::ForwardDiff.GradientConfig{ForwardDiff.Tag{typeof(f), Float64}, Float64, 4, Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(f), Float64}, Float64, 4}}})
    @ ForwardDiff ~/.julia/packages/ForwardDiff/eqMFf/src/gradient.jl:106
  [8] gradient(f::Function, x::Vector{Float64}, cfg::ForwardDiff.GradientConfig{ForwardDiff.Tag{typeof(f), Float64}, Float64, 4, Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(f), Float64}, Float64, 4}}}, ::Val{true})
    @ ForwardDiff ~/.julia/packages/ForwardDiff/eqMFf/src/gradient.jl:19
  [9] gradient(f::Function, x::Vector{Float64}, cfg::ForwardDiff.GradientConfig{ForwardDiff.Tag{typeof(f), Float64}, Float64, 4, Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(f), Float64}, Float64, 4}}}) (repeats 2 times)
    @ ForwardDiff ~/.julia/packages/ForwardDiff/eqMFf/src/gradient.jl:17
 [10] top-level scope
    @ REPL[11]:1

This is because now the A[i,j] != adjoint(A[j,i]) check performed in ishermitian returns true.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure from the issue, then read the ishermitian-related logic implicated by the A[i,j] != adjoint(A[j,i]) check and the LinearAlgebra cholesky.jl stack frame. Compare behavior for ordinary values and ForwardDiff.Dual values, including whether other affected methods reproduce it. Done means the reported cholesky path no longer rejects the Hermitian Dual matrix and regression coverage protects the behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.