Skip to content
Open
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
18 changes: 18 additions & 0 deletions lib/DiffEqBase/ext/DiffEqBaseForwardDiffExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,24 @@ if !hasmethod(nextfloat, Tuple{ForwardDiff.Dual})
end
end

struct WidenedDualArray{T,A} <: AbstractVector{T}
parent::A
end
Base.size(a::WidenedDualArray) = size(a.parent)
Base.@propagate_inbounds Base.getindex(a::WidenedDualArray{T}, i::Int) where {T} = convert(T, a.parent[i])

function (ff::SciMLBase.UJacobianWrapper{true})(du1, uprev::AbstractArray{<:ForwardDiff.Dual})
p = ff.p
if p isa AbstractArray && eltype(p) <: ForwardDiff.Dual
DualU = eltype(uprev)
if !(eltype(p) <: DualU)
hasfield(typeof(ff.f), :f) && getfield(ff.f, :f) isa FunctionWrappersWrappers.FunctionWrappersWrapper && return ff.f(du1, uprev, p, ff.t)
Comment on lines +213 to +218
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.

No need to do it like this, it should just be in the solve dispatch?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I tried it but the DiffEqBase solve path can't promote p because the nested dual type doesn't exist yet at that point , it's created later by Rosenbrock's JacobianConfig. Promoting early caused triple nesting.

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.

It's incorrect to just unwrap the functionwrapper though, that means the wrong call is getting compiled, so it needs to be handled at the source of the issue.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Makes sense. I'll move the fix to jacobian! in derivative_wrappers.jl , widen p once before DI.jacobian! runs, same approach as #3389. or you could reopen that.

return ff.f(du1, uprev, WidenedDualArray{DualU, typeof(p)}(p), ff.t)
end
end
ff.f(du1, uprev, p, ff.t)
end

import PrecompileTools
PrecompileTools.@compile_workload begin
# Scalar operations on Dual numbers (arithmetic, math functions, comparisons)
Expand Down