Skip to content

Segfault with nested differentiation triggered by @warn #2063

@michel2323

Description

@michel2323

out.log
The code below segfaults on Julia 1.10 and Enzyme v0.13.13

using Enzyme
import .EnzymeRules: augmented_primal, reverse, Annotation, has_rrule_from_sig
using .EnzymeRules

mutable struct Model
end

function bar!()
    @warn "segfaulting soon"
    # Doesn't sefault with a println
    # println("segfaulting soon")
    return nothing
end

function loop(body::Function, model)
    body(model)
    return model
end

function augmented_primal(config, func::Const{typeof(loop)}, ret, body, model,
)
    func.val(body.val, model.val)
    return AugmentedReturn(nothing, nothing, nothing)
end

function reverse(config, ::Const{typeof(loop)}, dret::Type{<:Const}, tape, body, model::Duplicated,)
    rev_loop(body.val, model.val, model.dval)
    return (nothing, nothing)
end

function rev_loop(
    body::Function,
    model::MT,
    shadowmodel::MT,
) where {MT}
    body(model)
    Enzyme.autodiff(Reverse, Const(body), Duplicated(model, shadowmodel))
end

function foo(model)
    model = loop(model) do model
            bar!()
    end
    return nothing
end

model = Model()
dmodel = Enzyme.make_zero(model)
foo(model)
autodiff(Enzyme.Reverse, foo, Duplicated(model, dmodel))

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions