ForwardDiff.jl icon indicating copy to clipboard operation
ForwardDiff.jl copied to clipboard

performance regression in x1.x? with GenericLinearAlgebra/sqrt or eigen

Open daviehh opened this issue 3 months ago • 1 comments

Codes that calculates sqrt or eigen on Hermitian matrices works in the v0.10.39 branch, but hangs in the v1.x releases, example mwe:

using GenericLinearAlgebra
using ForwardDiff

const sx = Matrix{ComplexF64}([0 1; 1 0])
const sy = Matrix{ComplexF64}([0 -im; im 0])
const sz = Matrix{ComplexF64}([1 0; 0 -1])
const ms = [sx, sy, sz]

const hs = [kron(x, x) for x in ms]

function f(g; hs)
    m = sin(g) * hs[1] + cos(g^2) * hs[2] + sin(cos(g / 2)) * hs[3]
    mh = Hermitian(m)
    mhs = Hermitian(sqrt(mh))
    l = abs.(eigvals(mhs))
    sum(l) - maximum(l) / 2
end

g(x) = f(x; hs)
@time ForwardDiff.derivative(g, 0.1)

may be more related to sqrt since it works fine it's not there, test with mhs = mh.

daviehh avatar Oct 06 '25 20:10 daviehh

This might be a duplicate of https://github.com/JuliaDiff/ForwardDiff.jl/issues/756 and be fixed by #757.

Edit: Indeed, I can reproduce the problem on the master branch but with #757 I get

julia> @time ForwardDiff.derivative(g, 0.1)
  0.005034 seconds (92 allocations: 18.500 KiB)
0.207978299889861

julia> using FiniteDifferences

julia> FiniteDifferences.grad(FiniteDifferences.central_fdm(5, 1), g ∘ only, [0.1])
([0.2079782998901855],)

devmotion avatar Oct 06 '25 20:10 devmotion