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

Enzyme Hessian bug #345

Closed
gdalle opened this issue Jul 11, 2024 · 1 comment
Closed

Enzyme Hessian bug #345

gdalle opened this issue Jul 11, 2024 · 1 comment
Labels
backend Related to one or more autodiff backends bug Something isn't working

Comments

@gdalle
Copy link
Member

gdalle commented Jul 11, 2024

See EnzymeAD/Enzyme.jl#1628

@gdalle gdalle added bug Something isn't working backend Related to one or more autodiff backends labels Jul 11, 2024
@gdalle
Copy link
Member Author

gdalle commented Oct 11, 2024

@dleather the issue you noticed is fixed on the latest version of everything, presumably thanks to the introduction of Constant arguments in DI.

Setup code
import DifferentiationInterface as AD
using Enzyme: Enzyme
using ForwardDiff: ForwardDiff

function compute_EI1uv_aug(z, t0, T, θz, θy)
    wu = compute_wu(t0, T, θz, θy)
    wv = compute_wv(t0, T, θz, θy)

    return wu * z[2] + wv * z[1] + 2 * z[1]^2
end

function compute_wu(t0, T, θz, θy)
    if θz  θy
        numerator =
            -θy - θz * coth((T - t0) * θz) + exp((T - t0) * θy) * θz * csch((T - t0) * θz)
        denominator = (θy - θz) * (θy + θz)
        return numerator / denominator
    else
        numerator =
            exp((T + t0) * θz) *
            (exp(2 * t0 * θz) + exp(2 * T * θz) * (-1 + 2 * T * θz - 2 * t0 * θz)) *
            csch((T - t0) * θz)
        denominator = 4 * θz
        return numerator / denominator
    end
end

function compute_wv(t0, T, θz, θy)
    if θz  θy
        numerator =
            exp((T - t0) * θy) * θy - exp((T - t0) * θy) * θz * coth((T - t0) * θz) +
            θz * csch((T - t0) * θz)
        denominator = (θy - θz) * (θy + θz)
        return numerator / denominator
    else
        numerator =
            exp(-(T + t0) * θz) *
            (exp((3 * T - t0) * θz) - exp((T + t0) * θz) * (1 + 2 * T * θz - 2 * t0 * θz)) *
            csch((T - t0) * θz)
        denominator = 4 * θz
        return numerator / denominator
    end
end

t0 = 0.0
T = 0.25
θz = 0.5
θy = 2.0
u = 0.1
v = 0.4
z = [v, u, v^2, u * v, u * v, u^2]
julia> compute_EI1uv_aug(z, t0, T, θz, θy)
0.4050218388870948

julia> AD.gradient(
           compute_EI1uv_aug,
           AD.AutoEnzyme(),
           z,
           AD.Constant(t0),
           AD.Constant(T),
           AD.Constant(θz),
           AD.Constant(θy),
       )
6-element Vector{Float64}:
 1.7754258975969854
 0.14851479848300594
 0.0
 0.0
 0.0
 0.0

julia> enzyme_hess = AD.hessian(
           compute_EI1uv_aug,
           AD.AutoEnzyme(),
           z,
           AD.Constant(t0),
           AD.Constant(T),
           AD.Constant(θz),
           AD.Constant(θy),
       )
6×6 Matrix{Float64}:
 4.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0

julia> R(z) = compute_EI1uv_aug(z, t0, T, θz, θy)
R (generic function with 1 method)

julia> ForwardDiff.hessian(R, z)
6×6 Matrix{Float64}:
 4.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend Related to one or more autodiff backends bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant