You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
hessian!(::ImmutableDiffResult, f, ::StaticArray) errors when f does not depend on its argument #846
hessian!(::ImmutableDiffResult, f, ::StaticArray) errors outright whenever f returns no Dual of its own tag.
using ForwardDiff, StaticArrays, DiffResults
sx =SVector(1.0, 2.0, 3.0)
# (a) constant `f`
ForwardDiff.hessian!(DiffResults.HessianResult(sx), z ->2.0, sx)
# ERROR: DimensionMismatch: No precise constructor for SMatrix{3, 3, Float64, 9} found.# Size of input was (0, 3).# (b) `f` constant in `z` but carrying an enclosing tag
ForwardDiff.derivative(1.0) do a
r = ForwardDiff.hessian!(DiffResults.HessianResult(sx), z -> a *2.0, sx)
DiffResults.value(r)
end# ERROR: MethodError: no method matching extract_jacobian(::Type{Tag{…}}, ::Dual{…}, ::SVector{3, Float64})
Every other path handles both fine — hessian(f, sx), hessian!(::Matrix, f, sx) and hessian(f, ::Vector) all return a 3×3 zero Hessian, and the nested forms of those return 0.0.
Cause
The method extracts with extract_jacobian(T, partials(T, fd2), x), and extract_jacobian(::Type{T}, ydual::Union{StaticArray,Partials}, x) assumes partials(T, fd2) has one entry per input. It does not when f returns no Dual{T}:
a constant gives Partials{0}, so the result is built at size (0, N) and the SMatrix constructor rejects it;
a Dual{S} with S ≺ T gives zero(d), a bare Dual, which matches no method at all.
Suggested fix
Dispatch on the result rather than on its partials, returning a zero length(x) × length(x) Hessian when there is no differentiated layer to read — a constant f, an empty x, and a result that carries only an enclosing tag all fall under the same rule.
Version: ForwardDiff master (v1.4.5), Julia 1.12.7, StaticArrays 1.9.19.
Reproducer (b) above conflates two independent failures, and the buffer half is my mistake. DiffResults.HessianResult(sx) allocates Float64 buffers, but a Hessian computed inside an enclosing derivative is legitimately Dual-valued — hessian(z -> a * sum(abs2, z), x) has entries 2a, and stripping that layer is precisely what would break the nesting — so no path can store it there. That part is caller error, on master and for every array type.
The actual bug needs no DiffResult at all:
using ForwardDiff, StaticArrays
sx =SVector(1.0, 2.0, 3.0)
x = [1.0, 2.0, 3.0]
ForwardDiff.derivative(a -> ForwardDiff.hessian(z -> a *2.0, sx)[1, 1], 1.0)
# ERROR: MethodError: no method matching extract_jacobian(::Type{Tag{…}}, ::Dual{…}, ::SVector{3, Float64})
ForwardDiff.derivative(a -> ForwardDiff.hessian(z -> a *2.0, x)[1, 1], 1.0)
# 0.0
Reproducer (a) is unaffected by the correction.
Where it comes from
extract_hessian extracts with extract_jacobian(T, partials(T, ydual), x), and the unindexed partials(::Type{T}, ·) does not return the same kind of thing in all three cases:
ydual
partials(T, ydual)
Dual{T,V,N}
Partials{N,V}
not a Dual
Partials{0,V}
Dual{S,V,N} with S ≺ T
zero(ydual) — a Dual, not a Partials
The third row is src/dual.jl#L110-L116, where a single method with an i... vararg serves both forms. Indexed, zero(d) is the right answer: the derivative of a T-independent value is a zero of that value's own type. Unindexed there is no right answer — the number of T-partials is not recoverable from a value that carries none — and returning the same zero(d) gives back something that is neither a Partials nor of length N.
So the fix is for callers to dispatch on the result rather than on its partials, which is what the suggestion above amounts to.
Activity
devmotion commentedon Sep 1, 2026
Reproducer (b) above conflates two independent failures, and the buffer half is my mistake.
DiffResults.HessianResult(sx)allocatesFloat64buffers, but a Hessian computed inside an enclosingderivativeis legitimatelyDual-valued —hessian(z -> a * sum(abs2, z), x)has entries2a, and stripping that layer is precisely what would break the nesting — so no path can store it there. That part is caller error, on master and for every array type.The actual bug needs no
DiffResultat all:Reproducer (a) is unaffected by the correction.
Where it comes from
extract_hessianextracts withextract_jacobian(T, partials(T, ydual), x), and the unindexedpartials(::Type{T}, ·)does not return the same kind of thing in all three cases:ydualpartials(T, ydual)Dual{T,V,N}Partials{N,V}DualPartials{0,V}Dual{S,V,N}withS ≺ Tzero(ydual)— aDual, not aPartialsThe third row is src/dual.jl#L110-L116, where a single method with an
i...vararg serves both forms. Indexed,zero(d)is the right answer: the derivative of aT-independent value is a zero of that value's own type. Unindexed there is no right answer — the number ofT-partials is not recoverable from a value that carries none — and returning the samezero(d)gives back something that is neither aPartialsnor of lengthN.So the fix is for callers to dispatch on the result rather than on its partials, which is what the suggestion above amounts to.
Give the Hessian's two dual layers distinct tags