-
Notifications
You must be signed in to change notification settings - Fork 37
Batch API #521
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
Draft
klamike
wants to merge
18
commits into
JuliaSmoothOptimizers:main
Choose a base branch
from
klamike:mk/batch
base: main
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
Batch API #521
Changes from 16 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
6866464
batch_* API & dumb VectorBatchNLPModel
klamike 1fa62a8
add missing SimpleNLPModel methods
klamike 80aa9e5
counters
klamike 79dce52
cleanup
klamike 71a2424
update
klamike e4ef7db
stubs
klamike 326d9bb
rm comment
klamike 8ea4b90
inplace
klamike 5a4cff8
reduce lambdas
klamike ef4540c
no inplace ops, test with parametric simple model
klamike a76ca78
Vararg{Any} -> Vararg{T}
klamike 9153826
simplify inplace syntax
klamike 17ee181
add todo
klamike 1331f06
1.12 gi
klamike 9598598
revert
klamike 7864819
try `@eval`
klamike 40fbb0c
simplify inputs/outputs
klamike 1d77abf
back to old API
klamike 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 hidden or 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 |
|---|---|---|
|
|
@@ -2,4 +2,4 @@ | |
| *.jl.mem | ||
| docs/build | ||
| docs/site | ||
| Manifest.toml | ||
| Manifest*.toml | ||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| export AbstractBatchNLPModel | ||
|
|
||
| abstract type AbstractBatchNLPModel end | ||
|
|
||
| function NLPModels.increment!(bnlp::AbstractBatchNLPModel, fun::Symbol) | ||
| NLPModels.increment!(bnlp, Val(fun)) | ||
| end | ||
|
|
||
| for fun in ( | ||
| :batch_obj, | ||
| :batch_grad, | ||
| :batch_grad!, | ||
| :batch_objgrad, | ||
| :batch_objgrad!, | ||
| :batch_objcons, | ||
| :batch_objcons!, | ||
| :batch_cons, | ||
| :batch_cons!, | ||
| :batch_cons_lin, | ||
| :batch_cons_lin!, | ||
| :batch_cons_nln, | ||
| :batch_cons_nln!, | ||
| :batch_jth_con, | ||
| :batch_jth_congrad, | ||
| :batch_jth_congrad!, | ||
| :batch_jth_sparse_congrad, | ||
| :batch_jac_structure!, | ||
| :batch_jac_structure, | ||
| :batch_jac_coord!, | ||
| :batch_jac_coord, | ||
| :batch_jac, | ||
| :batch_jprod, | ||
| :batch_jprod!, | ||
| :batch_jtprod, | ||
| :batch_jtprod!, | ||
| :batch_jac_op, | ||
| :batch_jac_op!, | ||
| :batch_jac_lin_structure!, | ||
| :batch_jac_lin_structure, | ||
| :batch_jac_lin_coord!, | ||
| :batch_jac_lin_coord, | ||
| :batch_jac_lin, | ||
| :batch_jprod_lin, | ||
| :batch_jprod_lin!, | ||
| :batch_jtprod_lin, | ||
| :batch_jtprod_lin!, | ||
| :batch_jac_lin_op, | ||
| :batch_jac_lin_op!, | ||
| :batch_jac_nln_structure!, | ||
| :batch_jac_nln_structure, | ||
| :batch_jac_nln_coord!, | ||
| :batch_jac_nln_coord, | ||
| :batch_jac_nln, | ||
| :batch_jprod_nln, | ||
| :batch_jprod_nln!, | ||
| :batch_jtprod_nln, | ||
| :batch_jtprod_nln!, | ||
| :batch_jac_nln_op, | ||
| :batch_jac_nln_op!, | ||
| :batch_jth_hess_coord, | ||
| :batch_jth_hess_coord!, | ||
| :batch_jth_hess, | ||
| :batch_jth_hprod, | ||
| :batch_jth_hprod!, | ||
| :batch_ghjvprod, | ||
| :batch_ghjvprod!, | ||
| :batch_hess_structure!, | ||
| :batch_hess_structure, | ||
| :batch_hess_coord!, | ||
| :batch_hess_coord, | ||
| :batch_hess, | ||
| :batch_hprod, | ||
| :batch_hprod!, | ||
| :batch_hess_op, | ||
| :batch_hess_op!, | ||
| :batch_varscale, | ||
| :batch_lagscale, | ||
| :batch_conscale, | ||
| ) | ||
| @eval export $fun | ||
| @eval function $fun end | ||
| end |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,259 @@ | ||
| export ForEachBatchNLPModel | ||
| struct ForEachBatchNLPModel{M} <: AbstractBatchNLPModel | ||
| models::M | ||
| counters::Counters | ||
| batch_size::Int | ||
| end | ||
| function ForEachBatchNLPModel(models::M) where {M} | ||
| isempty(models) && error("Cannot create ForEachBatchNLPModel from empty collection.") | ||
| ForEachBatchNLPModel{M}(models, Counters(), length(models)) | ||
| end | ||
| Base.length(vnlp::ForEachBatchNLPModel) = vnlp.batch_size | ||
| Base.getindex(vnlp::ForEachBatchNLPModel, i::Integer) = vnlp.models[i] | ||
| Base.iterate(vnlp::ForEachBatchNLPModel, state::Integer = 1) = iterate(vnlp.models, state) | ||
|
|
||
|
|
||
| function _batch_map(f::F, bnlp::ForEachBatchNLPModel, xs::Vararg{T,N}) where {F,T,N} | ||
| n = bnlp.batch_size | ||
| @lencheck_tup n xs | ||
| results = [] | ||
| resize!(results, n) | ||
| for i = 1:n | ||
| args_i = (x[i] for x in xs) | ||
| results[i] = f(bnlp[i], args_i...) | ||
| end | ||
| return results | ||
| end | ||
|
|
||
| function _batch_map!(f::F, bnlp::ForEachBatchNLPModel, xs::Vararg{T,N}) where {F,T,N} | ||
| n = bnlp.batch_size | ||
| length(xs) == 0 && error("Cannot call _batch_map! without providing arguments.") | ||
| @lencheck_tup n xs | ||
| outputs = xs[end] | ||
| inputs = length(xs) == 1 ? () : Base.ntuple(i -> xs[i], length(xs) - 1) | ||
klamike marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| @lencheck n outputs | ||
| for i = 1:n | ||
| args_i = (x[i] for x in inputs) | ||
| f(bnlp[i], args_i..., outputs[i]) | ||
| end | ||
| return outputs | ||
| end | ||
|
|
||
| function _batch_map_weight(f::F, bnlp::ForEachBatchNLPModel, obj_weights, xs::Vararg{T,N}) where {F,T,N} | ||
| n = bnlp.batch_size | ||
| @lencheck_tup n xs | ||
| @lencheck n obj_weights | ||
| results = [] | ||
| resize!(results, n) | ||
| for i = 1:n | ||
| args_i = (x[i] for x in xs) | ||
| results[i] = f(bnlp[i], args_i...; obj_weight = obj_weights[i]) | ||
| end | ||
| return results | ||
| end | ||
|
|
||
| function _batch_map_weight!(f::F, bnlp::ForEachBatchNLPModel, obj_weights, xs::Vararg{T,N}) where {F,T,N} | ||
| n = bnlp.batch_size | ||
| length(xs) == 0 && error("Cannot call _batch_map_weight! without providing arguments.") | ||
| @lencheck_tup n xs | ||
| @lencheck n obj_weights | ||
| outputs = xs[end] | ||
| inputs = length(xs) == 1 ? () : Base.ntuple(i -> xs[i], length(xs) - 1) | ||
| @lencheck n outputs | ||
| for i = 1:n | ||
| args_i = (x[i] for x in inputs) | ||
| f(bnlp[i], args_i..., outputs[i]; obj_weight = obj_weights[i]) | ||
| end | ||
| return outputs | ||
| end | ||
|
|
||
| function _batch_map_tuple(f::F, bnlp::ForEachBatchNLPModel, xs::Vararg{T,N}) where {F,T,N} | ||
| n = bnlp.batch_size | ||
| @lencheck_tup n xs | ||
| results = _batch_map(f, bnlp, xs...) | ||
|
|
||
| first_result = first(results) | ||
| T1, T2 = typeof(first_result[1]), typeof(first_result[2]) | ||
| vec1, vec2 = Vector{T1}(undef, n), Vector{T2}(undef, n) | ||
| for i = 1:n | ||
| vec1[i], vec2[i] = results[i] | ||
| end | ||
| return vec1, vec2 | ||
| end | ||
|
|
||
| function _batch_map_tuple!(f::F, bnlp::ForEachBatchNLPModel, outputs, xs::Vararg{T,N}) where {F,T,N} | ||
| n = bnlp.batch_size | ||
| @lencheck_tup n xs | ||
| @lencheck n outputs | ||
| firsts = [] | ||
| resize!(firsts, n) | ||
| for i = 1:n | ||
| args_i = (x[i] for x in xs) | ||
| firsts[i], _ = f(bnlp[i], args_i..., outputs[i]) | ||
| end | ||
| return firsts, outputs | ||
| end | ||
|
|
||
| for fun in fieldnames(Counters) | ||
| @eval function NLPModels.increment!(bnlp::ForEachBatchNLPModel, ::Val{$(Meta.quot(fun))}) | ||
| # sub-model counters are already incremented since we call their methods | ||
| bnlp.counters.$fun += 1 | ||
| end | ||
| end | ||
| for (batch_fun, fun) in ( | ||
| (:batch_jac_structure, :jac_structure), | ||
| (:batch_jac_lin_structure, :jac_lin_structure), | ||
| (:batch_jac_nln_structure, :jac_nln_structure), | ||
| (:batch_hess_structure, :hess_structure), | ||
| (:batch_varscale, :varscale), | ||
| (:batch_lagscale, :lagscale), | ||
| (:batch_conscale, :conscale), | ||
| ) | ||
| @eval $batch_fun(bnlp::ForEachBatchNLPModel) = _batch_map($fun, bnlp) | ||
| end | ||
| for (batch_fun, fun) in ( | ||
| (:batch_obj, :obj), | ||
| (:batch_grad, :grad), | ||
| (:batch_cons, :cons), | ||
| (:batch_cons_lin, :cons_lin), | ||
| (:batch_cons_nln, :cons_nln), | ||
| (:batch_jac, :jac), | ||
| (:batch_jac_lin, :jac_lin), | ||
| (:batch_jac_nln, :jac_nln), | ||
| (:batch_jac_lin_coord, :jac_lin_coord), | ||
| (:batch_jac_coord, :jac_coord), | ||
| (:batch_jac_nln_coord, :jac_nln_coord), | ||
| ) | ||
| @eval $batch_fun(bnlp::ForEachBatchNLPModel, x) = _batch_map($fun, bnlp, x) | ||
| end | ||
| for (batch_fun, fun) in ( | ||
| (:batch_jprod, :jprod), | ||
| (:batch_jtprod, :jtprod), | ||
| (:batch_jprod_nln, :jprod_nln), | ||
| (:batch_jtprod_nln, :jtprod_nln), | ||
| (:batch_jprod_lin, :jprod_lin), | ||
| (:batch_jtprod_lin, :jtprod_lin), | ||
| ) | ||
| @eval $batch_fun(bnlp::ForEachBatchNLPModel, x, y) = _batch_map($fun, bnlp, x, y) | ||
| end | ||
| for (batch_fun, fun) in ( | ||
| (:batch_ghjvprod, :ghjvprod), | ||
| ) | ||
| @eval $batch_fun(bnlp::ForEachBatchNLPModel, x, y, z) = _batch_map($fun, bnlp, x, y, z) | ||
| end | ||
|
|
||
| batch_jac_structure!(bnlp::ForEachBatchNLPModel, rowss, colss) = | ||
| _batch_map!(jac_structure!, bnlp, rowss, colss) | ||
| batch_jac_lin_structure!(bnlp::ForEachBatchNLPModel, rowss, colss) = | ||
| _batch_map!(jac_lin_structure!, bnlp, rowss, colss) | ||
| batch_jac_nln_structure!(bnlp::ForEachBatchNLPModel, rowss, colss) = | ||
| _batch_map!(jac_nln_structure!, bnlp, rowss, colss) | ||
| batch_hess_structure!(bnlp::ForEachBatchNLPModel, rowss, colss) = | ||
| _batch_map!(hess_structure!, bnlp, rowss, colss) | ||
| batch_jac_lin_coord!(bnlp::ForEachBatchNLPModel, xs, valss) = | ||
| _batch_map!(jac_lin_coord!, bnlp, xs, valss) | ||
| batch_grad!(bnlp::ForEachBatchNLPModel, xs, gs) = | ||
| _batch_map!(grad!, bnlp, xs, gs) | ||
| batch_cons!(bnlp::ForEachBatchNLPModel, xs, cs) = | ||
| _batch_map!(cons!, bnlp, xs, cs) | ||
| batch_cons_lin!(bnlp::ForEachBatchNLPModel, xs, cs) = | ||
| _batch_map!(cons_lin!, bnlp, xs, cs) | ||
| batch_cons_nln!(bnlp::ForEachBatchNLPModel, xs, cs) = | ||
| _batch_map!(cons_nln!, bnlp, xs, cs) | ||
| batch_jac_coord!(bnlp::ForEachBatchNLPModel, xs, valss) = | ||
| _batch_map!(jac_coord!, bnlp, xs, valss) | ||
| batch_jac_nln_coord!(bnlp::ForEachBatchNLPModel, xs, valss) = | ||
| _batch_map!(jac_nln_coord!, bnlp, xs, valss) | ||
| batch_jprod!(bnlp::ForEachBatchNLPModel, xs, vs, Jvs) = | ||
| _batch_map!(jprod!, bnlp, xs, vs, Jvs) | ||
| batch_jtprod!(bnlp::ForEachBatchNLPModel, xs, vs, Jtvs) = | ||
| _batch_map!(jtprod!, bnlp, xs, vs, Jtvs) | ||
| batch_jprod_nln!(bnlp::ForEachBatchNLPModel, xs, vs, Jvs) = | ||
| _batch_map!(jprod_nln!, bnlp, xs, vs, Jvs) | ||
| batch_jtprod_nln!(bnlp::ForEachBatchNLPModel, xs, vs, Jtvs) = | ||
| _batch_map!(jtprod_nln!, bnlp, xs, vs, Jtvs) | ||
| batch_jprod_lin!(bnlp::ForEachBatchNLPModel, xs, vs, Jvs) = | ||
| _batch_map!(jprod_lin!, bnlp, xs, vs, Jvs) | ||
| batch_jtprod_lin!(bnlp::ForEachBatchNLPModel, xs, vs, Jtvs) = | ||
| _batch_map!(jtprod_lin!, bnlp, xs, vs, Jtvs) | ||
| batch_ghjvprod!(bnlp::ForEachBatchNLPModel, xs, gs, vs, gHvs) = | ||
| _batch_map!(ghjvprod!, bnlp, xs, gs, vs, gHvs) | ||
|
|
||
| ## jth | ||
| batch_jth_con(bnlp::ForEachBatchNLPModel, xs, j::Integer) = | ||
| _batch_map((m, x) -> jth_con(m, x, j), bnlp, xs) | ||
| batch_jth_congrad(bnlp::ForEachBatchNLPModel, xs, j::Integer) = | ||
| _batch_map((m, x) -> jth_congrad(m, x, j), bnlp, xs) | ||
| batch_jth_sparse_congrad(bnlp::ForEachBatchNLPModel, xs, j::Integer) = | ||
| _batch_map((m, x) -> jth_sparse_congrad(m, x, j), bnlp, xs) | ||
| batch_jth_hess_coord(bnlp::ForEachBatchNLPModel, xs, j::Integer) = | ||
| _batch_map((m, x) -> jth_hess_coord(m, x, j), bnlp, xs) | ||
| batch_jth_hess(bnlp::ForEachBatchNLPModel, xs, j::Integer) = | ||
| _batch_map((m, x) -> jth_hess(m, x, j), bnlp, xs) | ||
| batch_jth_hprod(bnlp::ForEachBatchNLPModel, xs, vs, j::Integer) = | ||
| _batch_map((m, x, v) -> jth_hprod(m, x, v, j), bnlp, xs, vs) | ||
|
|
||
| batch_jth_congrad!(bnlp::ForEachBatchNLPModel, xs, j::Integer, outputs) = | ||
| _batch_map!((m, x, out) -> jth_congrad!(m, x, j, out), bnlp, xs, outputs) | ||
| batch_jth_hess_coord!(bnlp::ForEachBatchNLPModel, xs, j::Integer, outputs) = | ||
| _batch_map!((m, x, out) -> jth_hess_coord!(m, x, j, out), bnlp, xs, outputs) | ||
| batch_jth_hprod!(bnlp::ForEachBatchNLPModel, xs, vs, j::Integer, outputs) = | ||
| _batch_map!((m, x, v, out) -> jth_hprod!(m, x, v, j, out), bnlp, xs, vs, outputs) | ||
|
|
||
| # hess (need to treat obj_weight) FIXME: obj_weights is required in batch API | ||
| batch_hprod(bnlp::ForEachBatchNLPModel, xs, vs; obj_weights) = | ||
| _batch_map_weight(hprod, bnlp, obj_weights, xs, vs) | ||
| batch_hprod(bnlp::ForEachBatchNLPModel, xs, ys, vs; obj_weights) = | ||
| _batch_map_weight(hprod, bnlp, obj_weights, xs, ys, vs) | ||
| batch_hess_coord(bnlp::ForEachBatchNLPModel, xs; obj_weights) = | ||
| _batch_map_weight(hess_coord, bnlp, obj_weights, xs) | ||
| batch_hess_coord(bnlp::ForEachBatchNLPModel, xs, ys; obj_weights) = | ||
| _batch_map_weight(hess_coord, bnlp, obj_weights, xs, ys) | ||
| batch_hess_op(bnlp::ForEachBatchNLPModel, xs; obj_weights) = | ||
| _batch_map_weight(hess_op, bnlp, obj_weights, xs) | ||
| batch_hess_op(bnlp::ForEachBatchNLPModel, xs, ys; obj_weights) = | ||
| _batch_map_weight(hess_op, bnlp, obj_weights, xs, ys) | ||
|
|
||
| batch_hprod!(bnlp::ForEachBatchNLPModel, xs, vs, outputs; obj_weights) = | ||
| _batch_map_weight!(hprod!, bnlp, obj_weights, xs, vs, outputs) | ||
| batch_hprod!(bnlp::ForEachBatchNLPModel, xs, ys, vs, outputs; obj_weights) = | ||
| _batch_map_weight!(hprod!, bnlp, obj_weights, xs, ys, vs, outputs) | ||
| batch_hess_coord!(bnlp::ForEachBatchNLPModel, xs, outputs; obj_weights) = | ||
| _batch_map_weight!(hess_coord!, bnlp, obj_weights, xs, outputs) | ||
| batch_hess_coord!(bnlp::ForEachBatchNLPModel, xs, ys, outputs; obj_weights) = | ||
| _batch_map_weight!(hess_coord!, bnlp, obj_weights, xs, ys, outputs) | ||
| batch_hess_op!(bnlp::ForEachBatchNLPModel, xs, Hvs; obj_weights) = | ||
| _batch_map_weight(hess_op!, bnlp, obj_weights, xs, Hvs) | ||
| batch_hess_op!(bnlp::ForEachBatchNLPModel, xs, ys, Hvs; obj_weights) = | ||
| _batch_map_weight(hess_op!, bnlp, obj_weights, xs, ys, Hvs) | ||
|
|
||
| batch_hess(bnlp::ForEachBatchNLPModel, xs; obj_weights) = | ||
| _batch_map_weight(hess, bnlp, obj_weights, xs) | ||
| batch_hess(bnlp::ForEachBatchNLPModel, xs, ys; obj_weights) = | ||
| _batch_map_weight(hess, bnlp, obj_weights, xs, ys) | ||
|
|
||
| ## operators | ||
| batch_jac_op(bnlp::ForEachBatchNLPModel, xs) = | ||
| _batch_map(jac_op, bnlp, xs) | ||
| batch_jac_lin_op(bnlp::ForEachBatchNLPModel, xs) = | ||
| _batch_map(jac_lin_op, bnlp, xs) | ||
| batch_jac_nln_op(bnlp::ForEachBatchNLPModel, xs) = | ||
| _batch_map(jac_nln_op, bnlp, xs) | ||
|
|
||
| batch_jac_op!(bnlp::ForEachBatchNLPModel, xs, Jvs, Jtvs) = | ||
| _batch_map(jac_op!, bnlp, xs, Jvs, Jtvs) | ||
| batch_jac_lin_op!(bnlp::ForEachBatchNLPModel, xs, Jvs, Jtvs) = | ||
| _batch_map(jac_lin_op!, bnlp, xs, Jvs, Jtvs) | ||
| batch_jac_nln_op!(bnlp::ForEachBatchNLPModel, xs, Jvs, Jtvs) = | ||
| _batch_map(jac_nln_op!, bnlp, xs, Jvs, Jtvs) | ||
|
|
||
| ## tuple functions | ||
| batch_objgrad(bnlp::ForEachBatchNLPModel, xs) = | ||
| _batch_map_tuple(objgrad, bnlp, xs) | ||
| batch_objcons(bnlp::ForEachBatchNLPModel, xs) = | ||
| _batch_map_tuple(objcons, bnlp, xs) | ||
|
|
||
| batch_objgrad!(bnlp::ForEachBatchNLPModel, xs, gs) = | ||
| _batch_map_tuple!(objgrad!, bnlp, gs, xs) | ||
| batch_objcons!(bnlp::ForEachBatchNLPModel, xs, cs) = | ||
| _batch_map_tuple!(objcons!, bnlp, cs, xs) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.