From 358994846488dcb4d3a159ef3024530415de79ef Mon Sep 17 00:00:00 2001 From: Kristoffer Carlsson Date: Thu, 23 Apr 2026 15:52:45 +0200 Subject: [PATCH 1/2] add HyperHessian definition ref https://github.com/JuliaDiff/DifferentiationInterface.jl/pull/940 --- src/ADTypes.jl | 1 + src/dense.jl | 32 ++++++++++++++++++++++++++++++++ src/symbols.jl | 4 ++-- test/dense.jl | 14 ++++++++++++++ test/runtests.jl | 3 +++ test/symbols.jl | 1 + 6 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/ADTypes.jl b/src/ADTypes.jl index f53cee1..de5f756 100644 --- a/src/ADTypes.jl +++ b/src/ADTypes.jl @@ -36,6 +36,7 @@ export AutoChainRules, AutoFiniteDifferences, AutoForwardDiff, AutoGTPSA, + AutoHyperHessians, AutoModelingToolkit, AutoMooncake, AutoMooncakeForward, diff --git a/src/dense.jl b/src/dense.jl index 6f7944d..a7ff514 100644 --- a/src/dense.jl +++ b/src/dense.jl @@ -562,6 +562,38 @@ struct AutoZygote <: AbstractADType end mode(::AutoZygote) = ReverseMode() +""" + AutoHyperHessians{chunksize} + +Struct used to select the [HyperHessians.jl](https://github.com/KristofferC/HyperHessians.jl) backend for automatic differentiation. + +Defined by [ADTypes.jl](https://github.com/SciML/ADTypes.jl). + +# Constructors + + AutoHyperHessians(; chunksize=nothing) + +# Type parameters + + - `chunksize`: the preferred chunk size to evaluate several derivatives at once. If `nothing`, HyperHessians chooses automatically. +""" +struct AutoHyperHessians{chunksize} <: AbstractADType end + +function AutoHyperHessians(; chunksize::Union{Nothing, Int} = nothing) + if chunksize isa Int + chunksize > 0 || throw(ArgumentError("chunksize must be positive, got $chunksize")) + end + return AutoHyperHessians{chunksize}() +end + +mode(::AutoHyperHessians) = ForwardMode() + +function Base.show(io::IO, ::AutoHyperHessians{chunksize}) where {chunksize} + print(io, AutoHyperHessians, "(") + chunksize !== nothing && print(io, "chunksize=", repr(chunksize; context = io)) + return print(io, ")") +end + """ NoAutoDiff diff --git a/src/symbols.jl b/src/symbols.jl index 11d27ca..afa5a22 100644 --- a/src/symbols.jl +++ b/src/symbols.jl @@ -24,8 +24,8 @@ Auto(package::Symbol, args...; kws...) = Auto(Val(package), args...; kws...) for backend in ( :ChainRules, :Diffractor, :Enzyme, :Reactant, :FastDifferentiation, - :FiniteDiff, :FiniteDifferences, :ForwardDiff, :GTPSA, :Mooncake, :PolyesterForwardDiff, - :ReverseDiff, :Symbolics, :Tapir, :TaylorDiff, :Tracker, :Zygote, + :FiniteDiff, :FiniteDifferences, :ForwardDiff, :GTPSA, :HyperHessians, :Mooncake, + :PolyesterForwardDiff, :ReverseDiff, :Symbolics, :Tapir, :TaylorDiff, :Tracker, :Zygote, ) @eval Auto(::Val{$(QuoteNode(backend))}, args...; kws...) = $(Symbol(:Auto, backend))( args...; kws... diff --git a/test/dense.jl b/test/dense.jl index 932b8ef..1948b4d 100644 --- a/test/dense.jl +++ b/test/dense.jl @@ -160,6 +160,20 @@ end @test ad.descriptor == Val(:descriptor) end +@testset "AutoHyperHessians" begin + ad = AutoHyperHessians() + @test ad isa AbstractADType + @test ad isa AutoHyperHessians{nothing} + @test mode(ad) isa ForwardMode + + ad = AutoHyperHessians(; chunksize = 8) + @test ad isa AbstractADType + @test ad isa AutoHyperHessians{8} + @test mode(ad) isa ForwardMode + + @test_throws ArgumentError AutoHyperHessians(; chunksize = -1) +end + @testset "AutoMooncake" begin ad = AutoMooncake(; config = :config) @test ad isa AbstractADType diff --git a/test/runtests.jl b/test/runtests.jl index c04d2ac..c77d322 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -46,6 +46,7 @@ function every_ad() AutoFiniteDifferences(; fdm = :fdm), AutoForwardDiff(), AutoGTPSA(), + AutoHyperHessians(), AutoPolyesterForwardDiff(), AutoReverseDiff(), AutoSymbolics(), @@ -72,6 +73,8 @@ function every_ad_with_options() AutoForwardDiff(chunksize = 3, tag = :tag), AutoGTPSA(), AutoGTPSA(descriptor = Val(:descriptor)), + AutoHyperHessians(), + AutoHyperHessians(chunksize = 8), AutoMooncake(; config = :config), AutoMooncakeForward(; config = :config), AutoPolyesterForwardDiff(), diff --git a/test/symbols.jl b/test/symbols.jl index 10802cc..90d330c 100644 --- a/test/symbols.jl +++ b/test/symbols.jl @@ -8,6 +8,7 @@ using Test @test ADTypes.Auto(:FiniteDiff) isa AutoFiniteDiff @test ADTypes.Auto(:FiniteDifferences, 1.0) isa AutoFiniteDifferences{Float64} @test ADTypes.Auto(:ForwardDiff) isa AutoForwardDiff +@test ADTypes.Auto(:HyperHessians) isa AutoHyperHessians @test ADTypes.Auto(:Mooncake) isa AutoMooncake @test ADTypes.Auto(:PolyesterForwardDiff) isa AutoPolyesterForwardDiff @test ADTypes.Auto(:ReverseDiff) isa AutoReverseDiff From d1a196b29311dae64feb5a8ca6b669fba75f35a9 Mon Sep 17 00:00:00 2001 From: Kristoffer Carlsson Date: Fri, 24 Apr 2026 21:28:10 +0200 Subject: [PATCH 2/2] add to docs --- docs/src/index.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/src/index.md b/docs/src/index.md index 2bed335..85e7e55 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -36,6 +36,7 @@ Taylor mode: ```@docs AutoGTPSA AutoTaylorDiff +AutoHyperHessians ``` ### Reverse mode