Skip to content

EnzymeRules #172

@vchuravy

Description

@vchuravy

We need an easier way to register custom adjoints that are compatible with Enzyme. The tricky bit here is coming up with a good ABI.

The goal would be to somewhat mimic Diffractor,jl (cc: @simeonschaub), meaning that the rule selection would run before activity analysis and during Julia inference. Alternatively we could attempt to have a callback function that can create additional code after activity analysis, but I rather would like that kind of unlimited compiler recursion.

So taking Base.sin as an example. During type-inference we have Tuple{typeof(Base.sin), Float64} (and maybe the return type) and we somehow need to map that onto:

function tfunc_rrrule(::typeof(Base.sin), ::Type{Active}, ::Type{Active{T}}) where T
    function ∇sin(ret_grad, x::Active{T}, tape) where T
        return cos(x) * ret_grad
    end
end

function tfunc_rrrule(::typeof(Base.sin), ::Type{Const}, ::Type{Active{T}}) where T
    function ∇sin(ret_grad, x::Active{T}, tape) where T
        return zero(T)
    end
end

or:

function rrrule(::typeof(Base.sin), ret_grad::Active{T}, x::Active{T}, tape) where T
    return cos(x) * ret_grad
end

function rrrule(::typeof(Base.sin), ret_grad::Const{T}, x::Active{T}, tape) where T
    return zero(T)
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions