Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion ext/SciMLBaseForwardDiffExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import SciMLBase:
AbstractTimeseriesSolution, NonlinearProblem, NonlinearLeastSquaresProblem,
ODEProblem, SDEProblem, RODEProblem, DDEProblem, PDEProblem, DAEProblem,
RecursiveArrayTools, totallength, sse, anyeltypedual, reduce_tup,
unitfulvalue
unitfulvalue, _promote_jac_p!

eltypedual(x) = eltype(x) <: ForwardDiff.Dual
isdualtype(::Type{<:ForwardDiff.Dual}) = true
Expand Down Expand Up @@ -460,4 +460,16 @@ function SciMLBase.totallength(x::ForwardDiff.Dual)
sum(SciMLBase.totallength, ForwardDiff.partials(x))
end

function _promote_jac_p!(ff::SciMLBase.UJacobianWrapper, u::AbstractArray{<:ForwardDiff.Dual})
p = ff.p
p isa AbstractArray{<:ForwardDiff.Dual} || return p
hasfield(typeof(ff.f), :f) && getfield(ff.f, :f) isa SciMLBase.FunctionWrappersWrappers.FunctionWrappersWrapper && return p
DualU = eltype(u)
eltype(p) <: DualU && return p
cached = ff._promoted_p
cached isa AbstractArray{DualU} && return cached
ff._promoted_p = DualU.(p)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still allocates

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This converts p once on the first f! call and caches it in _promoted_p all subsequent calls hit cached isa AbstractArray{DualU} && return cached.
Am I missing something?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't the right place to do it. DiffEqBase solve has a promotion path, why not do it there?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened one SciML/OrdinaryDiffEq.jl#3406 here but have made the changes in the ForwardDiff extension file.

return ff._promoted_p
end

end
8 changes: 5 additions & 3 deletions src/function_wrappers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,16 @@ mutable struct UJacobianWrapper{iip, fType, tType, P} <: AbstractWrappedFunction
f::fType
t::tType
p::P
_promoted_p::Any
end

function UJacobianWrapper{iip}(f::F, t, p) where {F, iip}
return UJacobianWrapper{iip, F, typeof(t), typeof(p)}(f, t, p)
return UJacobianWrapper{iip, F, typeof(t), typeof(p)}(f, t, p, nothing)
end
UJacobianWrapper(f::F, t, p) where {F} = UJacobianWrapper{isinplace(f, 4)}(f, t, p)

(ff::UJacobianWrapper{true})(du1, uprev) = ff.f(du1, uprev, ff.p, ff.t)
(ff::UJacobianWrapper{true})(du1, uprev) = ff.f(du1, uprev, _promote_jac_p!(ff, uprev), ff.t)
_promote_jac_p!(ff, u) = ff.p
function (ff::UJacobianWrapper{true})(uprev)
(du1 = similar(uprev); ff.f(du1, uprev, ff.p, ff.t); du1)
end
Expand Down Expand Up @@ -236,4 +238,4 @@ JacobianWrapper(f::F, p) where {F} = JacobianWrapper{isinplace(f, 3)}(f, p)

(uf::JacobianWrapper{false})(u) = uf.f(u, uf.p)
(uf::JacobianWrapper{false})(res, u) = (vec(res) .= vec(uf.f(u, uf.p)))
(uf::JacobianWrapper{true})(res, u) = uf.f(res, u, uf.p)
(uf::JacobianWrapper{true})(res, u) = uf.f(res, u, uf.p)
Loading