Skip to content

Exception access violation with Enzyme ("Incorrect number of arguments passed to called function!") #819

@timholy

Description

@timholy

includeing the following script:

using DifferentiationInterface
using Enzyme: Enzyme, Const, Forward, Reverse, Active, Duplicated, autodiff_deferred
using Test

function f(x)
    @assert axes(x) == (Base.OneTo(2),)
    (x1, x2) = x
    objvar = ((((((4 * x1*x1 - 2.1 * x1^4) + (((((0.333333333333333x1) * x1) * x1) * x1) * x1) * x1) + x1 * x2) - 4 * x2*x2) + 4 * x2^4))
    return objvar
end
x0 = [1.0, 1.0]

backend = AutoEnzyme()

prepg = prepare_gradient(f, backend, x0)
preph = prepare_hessian(f, backend, x0)
∇f(x) = gradient(f, prepg, backend, x)
∇²f(x) = hessian(f, preph, backend, x)

# For Enzyme, it works if you comment out the previous 4 lines and use these instead:

# ∇f(x) = only(Enzyme.gradient(Reverse, f, x))
# function gradient_deferred(f, x)
#     dx = zeros(length(x))
#     autodiff_deferred(Reverse, Const(f), Active, Duplicated(x, dx))
#     dx
# end
# ∇²f(x0) = only(Enzyme.jacobian(Forward, x -> gradient_deferred(f, x), x0))

@test isreal(f(x0))
g, H = ∇f(x0), ∇²f(x0)
@test isa(g, AbstractVector{<:Real})
@test isa(H, AbstractMatrix{<:Real})
@test length(g) == 2
@test size(H) == (2, 2)

causes

julia> include("dibug.jl")
Incorrect number of arguments passed to called function!
  %106 = call fast fastcc double @julia___154(double %arrayref12.i.i, double (double)* sub (double (double)* @julia___154,
Please submit a bug report with steps to reproduce this fault, and any error messages that follow (in their entirety). Thanks.
Exception: EXCEPTION_ACCESS_VIOLATION at 0x6de7b473 -- .text$_ZN12_GLOBAL__N_112TypePrinting5printEPN4llvm4TypeERNS1_11raw_ostreamE at C:\Users\timho\.julia\juliaup\julia-1.10.10+0.x64.w64.mingw32\bin\libLLVM-15jl.dll (unknown line)
in expression starting at C:\Users\timho\OneDrive\Documents\publications\biquadraticoptim\src\testproblems\gams\dibug.jl:31
 

The issue is not the @assert, instead it's the x^4. (This example looks strange because it's a lightly-edited repro of the "camel1.gms" file parsed by GAMSFiles.) The script passes correctly if you instead use

using ForwardDiff: ForwardDiff

backend = AutoForwardDiff()

suggesting that it's not some problem with how the script is written.

Note that I've included in a commented block an implementation in terms of the Enzyme API that succeeds. The key is the use of autodiff_deferred and Duplicated in the reverse-mode evaluation of the gradient. I tried DI with backend = AutoEnzyme(; function_annotation=Enzyme.Duplicated) but that is not sufficient to fix it (though it's a different error).

Metadata

Metadata

Assignees

No one assigned

    Labels

    backendRelated to one or more autodiff backendswontfixThis will not be worked on

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions