Skip to content
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

Add abstract AD types #103

Merged
merged 2 commits into from
Mar 9, 2023
Merged
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
15 changes: 9 additions & 6 deletions src/ADNLPModels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ using ForwardDiff, ReverseDiff, Symbolics
using NLPModels
using Requires

abstract type AbstractADNLPModel{T, S} <: AbstractNLPModel{T, S} end
abstract type AbstractADNLPModel{T, S} <: AbstractNLPModel{T, S} end
abstract type AbstractADNLSModel{T, S} <: AbstractNLSModel{T, S} end

const ADModel{T, S} = Union{AbstractADNLPModel{T, S}, AbstractADNLSModel{T, S}}

include("ad.jl")
include("sparse_derivatives.jl")
Expand All @@ -26,22 +29,22 @@ export get_adbackend, set_adbackend!

Return the out-of-place version of `nlp.c!`.
"""
function get_c(nlp::Union{ADNLPModel, ADNLSModel})
function get_c(nlp::ADModel)
function c(x; nnln = nlp.meta.nnln)
c = similar(x, nnln)
nlp.c!(c, x)
return c
end
return c
end
get_c(nlp::Union{ADNLPModel, ADNLSModel}, ::ADBackend) = get_c(nlp)
get_c(nlp::ADModel, ::ADBackend) = get_c(nlp)

"""
get_adbackend(nlp)

Returns the value `adbackend` from nlp.
"""
get_adbackend(nlp::Union{ADNLPModel, ADNLSModel}) = nlp.adbackend
get_adbackend(nlp::ADModel) = nlp.adbackend

"""
set_adbackend!(nlp, new_adbackend)
Expand All @@ -50,11 +53,11 @@ get_adbackend(nlp::Union{ADNLPModel, ADNLSModel}) = nlp.adbackend
Replace the current `adbackend` value of nlp by `new_adbackend` or instantiate a new one with `kwargs`, see `ADModelBackend`.
By default, the setter with kwargs will reuse existing backends.
"""
function set_adbackend!(nlp::Union{ADNLPModel, ADNLSModel}, new_adbackend::ADModelBackend)
function set_adbackend!(nlp::ADModel, new_adbackend::ADModelBackend)
nlp.adbackend = new_adbackend
return nlp
end
function set_adbackend!(nlp::Union{ADNLPModel, ADNLSModel}; kwargs...)
function set_adbackend!(nlp::ADModel; kwargs...)
args = []
for field in fieldnames(ADNLPModels.ADModelBackend)
push!(args, if field in keys(kwargs) && typeof(kwargs[field]) <: ADBackend
Expand Down
16 changes: 8 additions & 8 deletions src/ad.jl
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ Hvprod(b::ADBackend, ::Any, ::Any, ::Any) = throw_error(b)
directional_second_derivative(::ADBackend, ::Any, ::Any, ::Any, ::Any) = throw_error(b)
function hess_structure!(
b::ADBackend,
nlp,
nlp::ADModel,
rows::AbstractVector{<:Integer},
cols::AbstractVector{<:Integer},
)
Expand All @@ -191,7 +191,7 @@ end

function hess_coord!(
b::ADBackend,
nlp::AbstractNLPModel,
nlp::AbstractADNLPModel,
x::AbstractVector,
y::AbstractVector,
obj_weight::Real,
Expand All @@ -207,7 +207,7 @@ function hess_coord!(
end
function hess_coord!(
b::ADBackend,
nlp::AbstractNLPModel,
nlp::AbstractADNLPModel,
x::AbstractVector,
obj_weight::Real,
vals::AbstractVector,
Expand All @@ -217,7 +217,7 @@ function hess_coord!(
end
function hess_coord!(
b::ADBackend,
nls::AbstractNLSModel,
nls::AbstractADNLSModel,
x::AbstractVector,
y::AbstractVector,
obj_weight::Real,
Expand All @@ -233,15 +233,15 @@ function hess_coord!(
end
function hess_coord!(
b::ADBackend,
nls::AbstractNLSModel,
nls::AbstractADNLSModel,
x::AbstractVector,
obj_weight::Real,
vals::AbstractVector,
)
ℓ(x) = obj_weight * sum(nls.F(x) .^ 2) / 2
return hess_coord!(b, nls, x, ℓ, vals)
end
function hess_coord!(b::ADBackend, nlp, x::AbstractVector, ℓ::Function, vals::AbstractVector)
function hess_coord!(b::ADBackend, nlp::ADModel, x::AbstractVector, ℓ::Function, vals::AbstractVector)
Hx = hessian(b, ℓ, x)
k = 1
for j = 1:(nlp.meta.nvar)
Expand All @@ -255,7 +255,7 @@ end

function jac_structure!(
b::ADBackend,
nlp,
nlp::ADModel,
rows::AbstractVector{<:Integer},
cols::AbstractVector{<:Integer},
)
Expand All @@ -265,7 +265,7 @@ function jac_structure!(
cols .= getindex.(I, 2)[:]
return rows, cols
end
function jac_coord!(b::ADBackend, nlp, x::AbstractVector, vals::AbstractVector)
function jac_coord!(b::ADBackend, nlp::ADModel, x::AbstractVector, vals::AbstractVector)
c = get_c(nlp, b)
Jx = jacobian(b, c, x)
vals .= Jx[:]
Expand Down
2 changes: 1 addition & 1 deletion src/nlp.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export ADNLPModel, ADNLPModel!

mutable struct ADNLPModel{T, S, Si} <: AbstractNLPModel{T, S}
mutable struct ADNLPModel{T, S, Si} <:AbstractADNLPModel{T, S}
meta::NLPModelMeta{T, S}
counters::Counters
adbackend::ADModelBackend
Expand Down
2 changes: 1 addition & 1 deletion src/nls.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export ADNLSModel

mutable struct ADNLSModel{T, S, Si} <: AbstractNLSModel{T, S}
mutable struct ADNLSModel{T, S, Si} <: AbstractADNLSModel{T, S}
meta::NLPModelMeta{T, S}
nls_meta::NLSMeta{T, S}
counters::NLSCounters
Expand Down
4 changes: 2 additions & 2 deletions src/sparse_derivatives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ end

function jac_structure!(
b::SparseADJacobian,
nlp,
::ADModel,
rows::AbstractVector{<:Integer},
cols::AbstractVector{<:Integer},
)
rows .= b.rows
cols .= b.cols
return rows, cols
end
function jac_coord!(b::SparseADJacobian, nlp, x::AbstractVector, vals::AbstractVector)
function jac_coord!(b::SparseADJacobian, ::ADModel, x::AbstractVector, vals::AbstractVector)
_fun = eval(b.cfJ[2])
Base.invokelatest(_fun, vals, x)
return vals
Expand Down
4 changes: 2 additions & 2 deletions src/zygote.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ struct ZygoteADJtprod <: ImmutableADbackend end
@init begin
@require Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f" begin
# See https://fluxml.ai/Zygote.jl/latest/limitations/
function get_immutable_c(nlp::Union{ADNLPModel, ADNLSModel})
function get_immutable_c(nlp::ADModel)
function c(x; nnln = nlp.meta.nnln)
c = Zygote.Buffer(x, nnln)
nlp.c!(c, x)
return copy(c)
end
return c
end
get_c(nlp::Union{ADNLPModel, ADNLSModel}, ::ImmutableADbackend) = get_immutable_c(nlp)
get_c(nlp::ADModel, ::ImmutableADbackend) = get_immutable_c(nlp)

function ZygoteADGradient(
nvar::Integer,
Expand Down