Skip to content

value_and_gradient with Enzyme forward-mode returns a different type #754

Closed as not planned
@penelopeysm

Description

@penelopeysm

Forward-mode Enzyme returns its own array type for the gradient, compared to other backends which return regular Vector{Float64}:

julia> f(x) = x[1] + x[2]
f (generic function with 1 method)

julia> import DifferentiationInterface as DI

julia> using Enzyme

julia> typeof(DI.value_and_gradient(f, AutoEnzyme(mode=Enzyme.Forward), [0.5, 0.2])[2])
Enzyme.TupleArray{Float64, (2,), 2, 1}

julia> typeof(DI.value_and_gradient(f, AutoEnzyme(mode=Enzyme.Reverse), [0.5, 0.2])[2])
Vector{Float64} (alias for Array{Float64, 1})

It's easy enough to handle with a collect on my side and/or using value_and_gradient! (the copyto! gives back the Vector{Float64}). Enzyme officially doesn't promise what type of AbstractArray it returns, but wondered if this was something worth looking at and/or documenting in DI?

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