Skip to content

Automate use of deferred in Higher order derivatives #1005

@AtilaSaraiva

Description

@AtilaSaraiva

Hello folks,

I tried to make calculate a second order derivative using Enzyme and wasn't able to. Is it possible?

Here is the code

using Enzyme


# Impure implementation of the f(x) = x^3 function
function f!(x::Vector{<:Number}, y::Vector{<:Number})
    @assert length(x) == length(y)
    for i=1:length(x)
        y[i] = x[i]^3
    end
    return nothing
end

x = Float64[1, 2, 3, 4] # input values
y = zeros(4)            # output values
dx = zeros(4)           # ∂f/∂x
dy = ones(4)            # dummy accumulator for ∂f/∂y

f!(x,y)

@show y - x.^3

autodiff(Reverse, f!, Const, Duplicated(x,dx), Duplicated(y, dy))

@show dx - 3*x.^2

function ∂f_∂x!(x::Vector{<:Number}, dx::Vector{<:Number})
    y = zeros(4)            # output values
    dy = ones(4)            # dummy accumulator for ∂f/∂y
    autodiff(Reverse, f!, Const, Duplicated(x,dx), Duplicated(y, dy))
    return nothing
end

dx = zeros(4)

∂f_∂x!(x, dx)

@show dx - 3*x.^2

dx2 = zeros(4)

# Trying to calculate a second order derivative with Enzyme
autodiff(Reverse, ∂f_∂x!, Const, Duplicated(x,dx2), Duplicated(y, dy))

@show dx2

When I try to run the second derivative the Julia REPL crashes and shows an allocation error. I can try to paste it here but it exceeds the maximum number of characters, so I'd have to upload a file.

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