-
-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use DifferentiationInterface #148
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #148 +/- ##
==========================================
- Coverage 90.60% 90.34% -0.26%
==========================================
Files 23 21 -2
Lines 1299 1202 -97
==========================================
- Hits 1177 1086 -91
+ Misses 122 116 -6 ☔ View full report in Codecov by Sentry. |
f111bd3
to
5ef7667
Compare
Any issues with DI integration @avik-pal ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So cool that you managed to merge this! Here are a few remarks on DI use, feel free to take them into account or not
mul!(reshape(du, 1, :), vec(DiffResults.value(res))', | ||
DiffResults.jacobian(res), 2, false) | ||
resid = __similar(du, length(sol.resid)) | ||
v, J = DI.value_and_jacobian(_f, resid, AutoForwardDiff(), u) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you may want to use preparation
2 .* vec(DiffResults.value(res))' * DiffResults.jacobian(res), | ||
size(u)) | ||
_f = Base.Fix2(prob.f, p) | ||
v, J = DI.value_and_jacobian(_f, AutoForwardDiff(), u) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
preparation?
__f = (du, u) -> f(du, u, p) | ||
ForwardDiff.jacobian(__f, du, u) | ||
__f = @closure (du, u) -> f(du, u, p) | ||
return ForwardDiff.jacobian(__f, __similar(u), u) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not use DI?
return ForwardDiff.jacobian(__f, u) | ||
end | ||
u isa Number && return ForwardDiff.derivative(__f, u) | ||
return ForwardDiff.jacobian(__f, u) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DI?
value_and_jacobian(ad, f, y, x, p, cache; J = nothing) | ||
function value_and_jacobian( | ||
ad, prob::AbstractNonlinearProblem, f::F, y, x, cache; J = nothing) where {F} | ||
x isa Number && return DI.value_and_derivative(f, ad, x, cache) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
was the cache
obtained with DI.prepare_derivative(f, ad, x)
?
H = DI.second_derivative(f, ad, x) | ||
v, J = DI.value_and_derivative(f, ad, x) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
preparation?
return fx, dfx, d2fx | ||
df = @closure x -> begin | ||
res = __similar(y, promote_type(eltype(y), eltype(x))) | ||
return DI.jacobian(f, res, ad, x) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
preparation?
res = __similar(y, promote_type(eltype(y), eltype(x))) | ||
return DI.jacobian(f, res, ad, x) | ||
end | ||
J, H = DI.value_and_jacobian(df, ad, x) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
preparation?
dfx = J_fn(x) | ||
d2fx = ForwardDiff.jacobian(J_fn, x) | ||
return fx, dfx, d2fx | ||
df = @closure x -> begin |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can use DI with functions f!(y, x)
without the closure
@inline function __get_concrete_autodiff(prob, ad::AutoForwardDiff{nothing}; kwargs...) | ||
return AutoForwardDiff(; chunksize = ForwardDiff.pickchunksize(length(prob.u0)), ad.tag) | ||
end | ||
@inline function __get_concrete_autodiff( | ||
prob, ad::AutoPolyesterForwardDiff{nothing}; kwargs...) | ||
return AutoPolyesterForwardDiff(; | ||
chunksize = ForwardDiff.pickchunksize(length(prob.u0)), ad.tag) | ||
end | ||
@inline function __get_concrete_autodiff(prob, ::Nothing; kwargs...) | ||
return ifelse( | ||
ForwardDiff.can_dual(eltype(prob.u0)), AutoForwardDiff(), AutoFiniteDiff()) | ||
return ifelse(ForwardDiff.can_dual(eltype(prob.u0)), | ||
AutoForwardDiff(; chunksize = ForwardDiff.pickchunksize(length(prob.u0))), | ||
AutoFiniteDiff()) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this necessary with DI?
SimpleHalley
now supports in place problemsTODOs