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
This is due to Partials{N} being treated as a dynamically sized AbstractArray by extract_jacobian. There are two implementations of extract_jacobian:
@generated function extract_jacobian(::Type{T}, ydual::StaticArray, x::S) where {T,S<:StaticArray}
M, N = length(ydual), length(x)
result = Expr(:tuple, [:(partials(T, ydual[$i], $j)) for i in 1:M, j in 1:N]...)
return quote
$(Expr(:meta, :inline))
V = StaticArrays.similar_type(S, valtype(eltype($ydual)), Size($M, $N))
return V($result)
end
end
function extract_jacobian(::Type{T}, ydual::AbstractArray, x::StaticArray) where T
result = similar(ydual, valtype(eltype(ydual)), length(ydual), length(x))
return extract_jacobian!(T, result, ydual, length(x))
end
and Partials{N} will hit the last one, leading to allocation of a Matrix{T}.
I'm not sure if the correct solution is to add Partials to the argument list of the @generated function, or to change what similar does. As a small test I just defined a new method for extract_jacobian:
@generated function extract_jacobian(::Type{T}, ydual::Partials{M}, x::S) where {M, T, S<:StaticArray}
N = length(x)
result = Expr(:tuple, [:(partials(T, ydual[$i], $j)) for i in 1:M, j in 1:N]...)
return quote
$(Expr(:meta, :inline))
V = StaticArrays.similar_type(S, valtype(eltype($ydual)), Size($M, $N))
return V($result)
end
end
When calculating hessians with DiffResults and StaticArrays there are unexpected allocations:
This is due to
Partials{N}
being treated as a dynamically sizedAbstractArray
byextract_jacobian
. There are two implementations ofextract_jacobian
:and
Partials{N}
will hit the last one, leading to allocation of aMatrix{T}
.I'm not sure if the correct solution is to add
Partials
to the argument list of the@generated
function, or to change whatsimilar
does. As a small test I just defined a new method forextract_jacobian
:After this change, I get:
The text was updated successfully, but these errors were encountered: