Skip to content

Vectorization run between levels of nested AD #748

@samakins

Description

@samakins

Maybe misusing this but for a simple scalar function when I try to take a second derivative I get some very strange results.

using Enzyme

function derivative(f::F, x::T) where {F, T<:Real}
    adf = @inline xarg -> Enzyme.autodiff(Enzyme.Forward, f, Duplicated(xarg, 1.0))
    first(adf(x))
end

function secondderivative(f::F, x::T) where {F, T<:Real}
    adf = @inline xarg -> Enzyme.autodiff_deferred(Enzyme.Reverse, f, Active(xarg))
    first(first(first(autodiff(Forward, adf, Duplicated(x, 1.0)))))
end

g1(x) = x*x*x*x + 0.0*x*x*x
g2(x) = x*x*x*x
g3(x) = x*x*x*x + 1e-9*x*x*x

println(derivative(g1, 1.0))
println(derivative(g2, 1.0))
println(derivative(g3, 1.0))
println(secondderivative(g1, 1.0))
println(secondderivative(g2, 1.0))
println(secondderivative(g3, 1.0))

This produces the output

4.0

4.0

4.000000003

15.0

12.0

12.000000006

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    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