-
Notifications
You must be signed in to change notification settings - Fork 30
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 for autodiff, allow ADTypes #153
Draft
gdalle
wants to merge
10
commits into
JuliaNLSolvers:master
Choose a base branch
from
gdalle:gd/di
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5d007f3
Start DI integration
gdalle 8be2259
Fix bug
gdalle 7783026
Handle constraints
gdalle dbf30ab
Bump version to 7.9.0
gdalle 250fd89
Merge remote-tracking branch 'upstream/master' into gd/di
gdalle ebaa4bd
Merge branch 'master' into gd/di
gdalle 5a7d1c4
Bump Julia compat to 1.10
gdalle 30ec326
Min dif
gdalle 914c6c5
Improve coverage
gdalle 1374805
Add docs
gdalle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -139,27 +139,13 @@ function OnceDifferentiableConstraints(c!, lx::AbstractVector, ux::AbstractVecto | |
xcache = zeros(T, sizex) | ||
ccache = zeros(T, sizec) | ||
|
||
if is_finitediff(autodiff) | ||
ccache2 = similar(ccache) | ||
fdtype = finitediff_fdtype(autodiff) | ||
jacobian_cache = FiniteDiff.JacobianCache(xcache, ccache,ccache2,fdtype) | ||
function jfinite!(J, x) | ||
FiniteDiff.finite_difference_jacobian!(J, c!, x, jacobian_cache) | ||
J | ||
end | ||
return OnceDifferentiableConstraints(c!, jfinite!, bounds) | ||
elseif is_forwarddiff(autodiff) | ||
jac_cfg = ForwardDiff.JacobianConfig(c!, ccache, xcache, chunk) | ||
ForwardDiff.checktag(jac_cfg, c!, xcache) | ||
|
||
function jforward!(J, x) | ||
ForwardDiff.jacobian!(J, c!, ccache, x, jac_cfg, Val{false}()) | ||
J | ||
end | ||
return OnceDifferentiableConstraints(c!, jforward!, bounds) | ||
else | ||
error("The autodiff value $autodiff is not support. Use :finite or :forward.") | ||
backend = get_adtype(autodiff, chunk) | ||
jac_prep = DI.prepare_jacobian(c!, ccache, backend, xcache) | ||
function j!(_j, _x) | ||
DI.jacobian!(c!, ccache, _j, jac_prep, backend, _x) | ||
return _j | ||
end | ||
return OnceDifferentiableConstraints(c!, j!, bounds) | ||
end | ||
|
||
|
||
|
@@ -179,153 +165,55 @@ function TwiceDifferentiableConstraints(c!, lx::AbstractVector, ux::AbstractVect | |
lc::AbstractVector, uc::AbstractVector, | ||
autodiff::Symbol = :central, | ||
chunk::ForwardDiff.Chunk = checked_chunk(lx)) | ||
if is_finitediff(autodiff) | ||
fdtype = finitediff_fdtype(autodiff) | ||
return twicediff_constraints_finite(c!,lx,ux,lc,uc,fdtype,nothing) | ||
elseif is_forwarddiff(autodiff) | ||
return twicediff_constraints_forward(c!,lx,ux,lc,uc,chunk,nothing) | ||
else | ||
error("The autodiff value $autodiff is not support. Use :finite or :forward.") | ||
end | ||
end | ||
|
||
function TwiceDifferentiableConstraints(c!, con_jac!,lx::AbstractVector, ux::AbstractVector, | ||
lc::AbstractVector, uc::AbstractVector, | ||
autodiff::Symbol = :central, | ||
chunk::ForwardDiff.Chunk = checked_chunk(lx)) | ||
if is_finitediff(autodiff) | ||
fdtype = finitediff_fdtype(autodiff) | ||
return twicediff_constraints_finite(c!,lx,ux,lc,uc,fdtype,con_jac!) | ||
elseif is_forwarddiff(autodiff) | ||
return twicediff_constraints_forward(c!,lx,ux,lc,uc,chunk,con_jac!) | ||
else | ||
error("The autodiff value $autodiff is not support. Use :finite or :forward.") | ||
end | ||
end | ||
|
||
|
||
|
||
function TwiceDifferentiableConstraints(lx::AbstractArray, ux::AbstractArray) | ||
bounds = ConstraintBounds(lx, ux, [], []) | ||
TwiceDifferentiableConstraints(bounds) | ||
end | ||
|
||
|
||
function twicediff_constraints_forward(c!, lx, ux, lc, uc,chunk,con_jac! = nothing) | ||
bounds = ConstraintBounds(lx, ux, lc, uc) | ||
T = eltype(bounds) | ||
nc = length(lc) | ||
nx = length(lx) | ||
x_example = zeros(T, nx) | ||
λ_example = zeros(T, nc) | ||
ccache = zeros(T, nc) | ||
xcache = zeros(T, nx) | ||
cache_check = Ref{DataType}(Missing) #the datatype Missing, not the singleton | ||
ref_f= Ref{Any}() #cache for intermediate jacobian used in the hessian | ||
cxxcache = zeros(T, nx * nc, nx) #output cache for hessian | ||
h = reshape(cxxcache, (nc, nx, nx)) #reshaped output | ||
hi = [@view h[i,:,:] for i in 1:nc] | ||
#ref_f caches the closure function with its caches. other aproaches include using a Dict, but the | ||
#cost of switching happens just once per optimize call. | ||
|
||
if isnothing(con_jac!) #if the jacobian is not provided, generate one | ||
jac_cfg = ForwardDiff.JacobianConfig(c!, ccache, xcache, chunk) | ||
ForwardDiff.checktag(jac_cfg, c!, xcache) | ||
|
||
jac! = (J, x) -> begin | ||
ForwardDiff.jacobian!(J, c!, ccache, x, jac_cfg, Val{false}()) | ||
J | ||
end | ||
|
||
function sum_constraints(_x, _λ) | ||
# TODO: get rid of this allocation with DI.Cache | ||
ccache_righttype = zeros(promote_type(T, eltype(_x)), nc) | ||
c!(ccache_righttype, _x) | ||
return sum(_λ[i] * ccache[i] for i in eachindex(_λ, ccache)) | ||
end | ||
|
||
con_jac_cached = x -> begin | ||
exists_cache = (cache_check[] == eltype(x)) | ||
if exists_cache | ||
f = ref_f[] | ||
return f(x) | ||
else | ||
jcache = zeros(eltype(x), nc) | ||
out_cache = zeros(eltype(x), nc, nx) | ||
cfg_cache = ForwardDiff.JacobianConfig(c!,jcache,x) | ||
f = z->ForwardDiff.jacobian!(out_cache, c!, jcache, z,cfg_cache,Val{false}()) | ||
ref_f[] = f | ||
cache_check[]= eltype(x) | ||
return f(x) | ||
end | ||
end | ||
backend = get_adtype(autodiff, chunk) | ||
|
||
else | ||
jac! = (J,x) -> con_jac!(J,x) | ||
|
||
#here, the cache should also include a JacobianConfig | ||
con_jac_cached = x -> begin | ||
exists_cache = (cache_check[] == eltype(x)) | ||
if exists_cache | ||
f = ref_f[] | ||
return f(x) | ||
else | ||
out_cache = zeros(eltype(x), nc, nx) | ||
f = z->jac!(out_cache,x) | ||
ref_f[] = f | ||
cache_check[]= eltype(x) | ||
return f(x) | ||
end | ||
end | ||
|
||
jac_prep = DI.prepare_jacobian(c!, ccache, backend, x_example) | ||
function con_jac!(_j, _x) | ||
DI.jacobian!(c!, ccache, _j, jac_prep, backend, _x) | ||
return _j | ||
end | ||
|
||
hess_config_cache = ForwardDiff.JacobianConfig(typeof(con_jac_cached),lx) | ||
function con_hess!(hess, x, λ) | ||
ForwardDiff.jacobian!(cxxcache, con_jac_cached, x,hess_config_cache,Val{false}()) | ||
for i = 1:nc #hot hessian loop | ||
hess+=λ[i].*hi[i] | ||
end | ||
return hess | ||
|
||
hess_prep = DI.prepare_hessian(sum_constraints, backend, x_example, DI.Constant(λ_example)) | ||
function con_hess!(_h, _x, _λ) | ||
DI.hessian!(sum_constraints, _h, hess_prep, backend, _x, DI.Constant(_λ)) | ||
return _h | ||
end | ||
|
||
return TwiceDifferentiableConstraints(c!, jac!, con_hess!, bounds) | ||
return TwiceDifferentiableConstraints(c!, con_jac!, con_hess!, bounds) | ||
end | ||
|
||
|
||
function twicediff_constraints_finite(c!,lx,ux,lc,uc,fdtype,con_jac! = nothing) | ||
bounds = ConstraintBounds(lx, ux, lc, uc) | ||
T = eltype(bounds) | ||
nx = length(lx) | ||
nc = length(lc) | ||
xcache = zeros(T, nx) | ||
ccache = zeros(T, nc) | ||
function TwiceDifferentiableConstraints(c!, con_jac!,lx::AbstractVector, ux::AbstractVector, | ||
lc::AbstractVector, uc::AbstractVector, | ||
autodiff::Symbol = :central, | ||
chunk::ForwardDiff.Chunk = checked_chunk(lx)) | ||
# TODO: is con_jac! still useful? we ignore it here | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What should we do about this? The new version of the code directly computes the Hessian of the sum of constraints |
||
|
||
if isnothing(con_jac!) | ||
jac_ccache = similar(ccache) | ||
jacobian_cache = FiniteDiff.JacobianCache(xcache, ccache,jac_ccache,fdtype) | ||
function jac!(J, x) | ||
FiniteDiff.finite_difference_jacobian!(J, c!, x, jacobian_cache) | ||
J | ||
end | ||
else | ||
jac! = (J,x) -> con_jac!(J,x) | ||
end | ||
cxxcache = zeros(T,nc*nx,nx) # to create cached jacobian | ||
h = reshape(cxxcache, (nc, nx, nx)) #reshaped output | ||
hi = [@view h[i,:,:] for i in 1:nc] | ||
|
||
function jac_vec!(J,x) #to evaluate the jacobian of a jacobian, FiniteDiff needs a vector version of that | ||
j_mat = reshape(J,nc,nx) | ||
return jac!(j_mat,x) | ||
return J | ||
end | ||
hess_xcache =similar(xcache) | ||
hess_cxcache =zeros(T,nc*nx) #output of jacobian, as a vector | ||
hess_cxxcache =similar(hess_cxcache) | ||
hess_config_cache = FiniteDiff.JacobianCache(hess_xcache,hess_cxcache,hess_cxxcache,fdtype) | ||
function con_hess!(hess, x, λ) | ||
FiniteDiff.finite_difference_jacobian!(cxxcache, jac_vec!, x,hess_config_cache) | ||
for i = 1:nc | ||
hi = @view h[i,:,:] | ||
hess+=λ[i].*hi | ||
end | ||
return hess | ||
end | ||
return TwiceDifferentiableConstraints(c!, jac!, con_hess!, bounds) | ||
return TwiceDifferentiableConstraints(c!, lx, ux, lc, uc, autodiff, chunk) | ||
end | ||
|
||
|
||
|
||
function TwiceDifferentiableConstraints(lx::AbstractArray, ux::AbstractArray) | ||
bounds = ConstraintBounds(lx, ux, [], []) | ||
TwiceDifferentiableConstraints(bounds) | ||
end | ||
|
||
function TwiceDifferentiableConstraints(bounds::ConstraintBounds) | ||
c! = (x, c)->nothing | ||
J! = (x, J)->nothing | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Maybe these could be made callable structs to avoid closing over
c!
,ccache
,jac_prep
andbackend
?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.
These were already closures in the existing code, I thought I'd go for the "minimum diff" changes and then we could improve later on
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.
Yeah, I saw that. It seemed you're closing over more variables in the DI version though, so it might be even more valuable to change the design.
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.
I seem to recall that closures are not an issue if the variable we close over does not get assigned to more than once? So we might be good here?
In any case, to hunt down this kind of type inference barriers we would also need to improve annotation of functions in the various structs of the package.
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.
I don't know. Even if it's currently the case, personally I wouldn't rely on it since in my experience the exact behaviour of the compiler is inherently unstable.
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.
See for instance the explanations in https://discourse.julialang.org/t/can-someone-explain-closures-to-me/105605