Consider a function f:\mathbb{R}^n\to\mathbb{R}. Following along from the Enzyme AutoDiff API documentation example on computing the Hessian via FoR mixed mode AD, I have written the following julia function to compute Hessian-vector products:
function hvp(f::F, x::S, v::S) where {F, S<:AbstractVector{<:AbstractFloat}}
bx = similar(x)
dbx = similar(x)
autodiff(
Forward,
x -> autodiff_deferred(f, x),
Duplicated(Duplicated(x, bx), Duplicated(v, dbx))
)
return dbx
end
My question: it is useful in a performance sense to provide the return type Activity
, and if so how do I do this correctly? For example, it seems to me that I should pass the Activity
value for the outermost autodiff
as const
, but I can’t get this to work.