Skip to content

Missing _shadow method in Enzyme extension #942

@timholy

Description

@timholy
using LinearAlgebra
using DifferentiationInterface
using Enzyme: Enzyme

function lagrangian(A, b, Bdiag, y, x, s, z, q, λ, η, xₖ)
    r1 = A*x - y
    r2 = s - x
    return sum(abs2, b - y) / 2 + λ * dot(x - xₖ, Diagonal(Bdiag), x - xₖ) / 2 - η * sum(log, s) + dot(z, r1) + dot(q, r2)
end
function lagrangian(A, b, Bdiag, top, bottom, λ, η, xₖ)
    m, n = size(A)
    y, x, s = view(top, 1:m), view(top, m+1:m+n), view(top, m+n+1:m+2n)
    z, q = view(bottom, 1:m), view(bottom, m+1:m+n)
    return lagrangian(A, b, Bdiag, y, x, s, z, q, λ, η, xₖ)
end
function lagrangian(A, b, Bdiag, topbottom, λ, η, xₖ)
    m, n = size(A)
    top, bottom = view(topbottom, 1:m+2n), view(topbottom, m+2n+1:2m+3n)
    return lagrangian(A, b, Bdiag, top, bottom, λ, η, xₖ)
end

A = [ -0.24  -0.82   1.35   0.36   0.35
        -0.53  -0.20  -0.76   0.98  -0.54
        0.22   1.25  -1.60  -1.37  -1.94
        -0.51  -0.56  -0.08   0.96   0.46
        0.48  -2.25   0.38   0.06  -1.29 ]
m, n = size(A)
b = [-1.6, 0.19, 0.17, 0.31, -1.27]
Bdiag = diag(A'*A)
η = 0.01
λ = 1e-4
top0, bottom0 = vcat(zeros(m+n), ones(n)), ones(m+n)
xₖ = top0[m+1:m+n]
tb0 = vcat(top0, bottom0)
function makeL(A, b, Bdiag, λ, η, xₖ)
    L(tb) = lagrangian(A, b, Bdiag, tb, λ, η, xₖ)
    return L
end
L = makeL(A, b, Bdiag, λ, η, xₖ)

@assert isfinite(L(tb0))

backend = AutoEnzyme(; mode=nothing, function_annotation=Enzyme.Duplicated)
gradient(L, backend, tb0)  # fine
hessian(L, backend, tb0)   # errors

The error is

julia> hessian(L, backend, tb0)   # errors
ERROR: MethodError: no method matching _shadow(::AutoEnzyme{Nothing, EnzymeCore.Duplicated}, ::EnzymeCore.ForwardMode{true, EnzymeCore.FFIABI, false, false, false}, ::Val{13}, ::DifferentiationInterface.FunctionContext{var"#L#1"{Matrix{Float64}, Vector{Float64}, Vector{Float64}, Float64, Float64, Vector{Float64}}})

Closest candidates are:
  _shadow(::AutoEnzyme{M, <:Union{Nothing, EnzymeCore.Const}}, ::EnzymeCore.Mode, ::Val{B}, ::DifferentiationInterface.FunctionContext) where {M, B}
   @ DifferentiationInterfaceEnzymeExt C:\Users\timho\.julia\dev\DifferentiationInterface\DifferentiationInterface\ext\DifferentiationInterfaceEnzymeExt\utils.jl:89
  _shadow(::AutoEnzyme, ::EnzymeCore.Mode, ::Val{B}, ::ConstantOrCache) where B
   @ DifferentiationInterfaceEnzymeExt C:\Users\timho\.julia\dev\DifferentiationInterface\DifferentiationInterface\ext\DifferentiationInterfaceEnzymeExt\utils.jl:73
  _shadow(::AutoEnzyme, ::EnzymeCore.Mode, ::Val{B}, ::Cache) where B
   @ DifferentiationInterfaceEnzymeExt C:\Users\timho\.julia\dev\DifferentiationInterface\DifferentiationInterface\ext\DifferentiationInterfaceEnzymeExt\utils.jl:64
  ...

Stacktrace:
  [1] (::DifferentiationInterfaceEnzymeExt.var"#7#8"{13, AutoEnzyme{…}, EnzymeCore.ForwardMode{…}})(c_wrapped::DifferentiationInterface.FunctionContext{var"#L#1"{…}})
    @ DifferentiationInterfaceEnzymeExt C:\Users\timho\.julia\dev\DifferentiationInterface\DifferentiationInterface\ext\DifferentiationInterfaceEnzymeExt\utils.jl:103
  [2] map
    @ .\tuple.jl:293 [inlined]
  [3] make_context_shadows(::AutoEnzyme{…}, ::EnzymeCore.ForwardMode{…}, ::Val{…}, ::DifferentiationInterface.FunctionContext{…}, ::Constant{…}, ::Constant{…})
    @ DifferentiationInterfaceEnzymeExt C:\Users\timho\.julia\dev\DifferentiationInterface\DifferentiationInterface\ext\DifferentiationInterfaceEnzymeExt\utils.jl:102
  [4] prepare_pushforward_nokwarg(::Val{…}, ::typeof(DifferentiationInterface.shuffled_gradient), ::AutoEnzyme{…}, ::Vector{…}, ::NTuple{…}, ::DifferentiationInterface.FunctionContext{…}, ::Constant{…}, ::Constant{…})
    @ DifferentiationInterfaceEnzymeExt C:\Users\timho\.julia\dev\DifferentiationInterface\DifferentiationInterface\ext\DifferentiationInterfaceEnzymeExt\forward_onearg.jl:20
  [5] _prepare_hvp_aux(::Val{…}, ::DifferentiationInterface.ForwardOverReverse, ::DifferentiationInterface.DontPrepareInner, ::var"#L#1"{…}, ::AutoEnzyme{…}, ::Vector{…}, ::NTuple{…})
    @ DifferentiationInterface C:\Users\timho\.julia\dev\DifferentiationInterface\DifferentiationInterface\src\second_order\hvp.jl:166
  [6] prepare_hvp_nokwarg(::Val{true}, ::var"#L#1"{Matrix{Float64}, Vector{Float64}, Vector{Float64}, Float64, Float64, Vector{Float64}}, ::AutoEnzyme{Nothing, EnzymeCore.Duplicated}, ::Vector{Float64}, ::NTuple{13, Vector{Float64}})
    @ DifferentiationInterface C:\Users\timho\.julia\dev\DifferentiationInterface\DifferentiationInterface\src\second_order\hvp.jl:125
  [7] _prepare_hessian_aux(::Val{true}, ::DifferentiationInterface.BatchSizeSettings{13, false, false}, ::var"#L#1"{Matrix{Float64}, Vector{Float64}, Vector{Float64}, Float64, Float64, Vector{Float64}}, ::AutoEnzyme{Nothing, EnzymeCore.Duplicated}, ::Vector{Float64})
    @ DifferentiationInterface C:\Users\timho\.julia\dev\DifferentiationInterface\DifferentiationInterface\src\second_order\hessian.jl:125
  [8] prepare_hessian_nokwarg(::Val{true}, ::var"#L#1"{Matrix{Float64}, Vector{Float64}, Vector{Float64}, Float64, Float64, Vector{Float64}}, ::AutoEnzyme{Nothing, EnzymeCore.Duplicated}, ::Vector{Float64})
    @ DifferentiationInterface C:\Users\timho\.julia\dev\DifferentiationInterface\DifferentiationInterface\src\second_order\hessian.jl:106
  [9] hessian(::var"#L#1"{Matrix{Float64}, Vector{Float64}, Vector{Float64}, Float64, Float64, Vector{Float64}}, ::AutoEnzyme{Nothing, EnzymeCore.Duplicated}, ::Vector{Float64})
    @ DifferentiationInterface C:\Users\timho\.julia\dev\DifferentiationInterface\DifferentiationInterface\src\second_order\hessian.jl:34
 [10] top-level scope
    @ REPL[3]:1

Metadata

Metadata

Assignees

No one assigned

    Labels

    backendRelated to one or more autodiff backends

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions