Separate default implementations from api.jl#513
Separate default implementations from api.jl#513arnavk23 wants to merge 15 commits intoJuliaSmoothOptimizers:mainfrom
Conversation
- Create src/nlp/defaults.jl for default implementations - Update src/NLPModels.jl to include defaults.jl after api.jl - Move default implementations from api.jl to defaults.jl - Replace implementations with function stubs in api.jl - Resolves method overwriting warnings during precompilation - Fixes GitHub issue #385: Separate the default implementations from the api file Functions refactored: - grad(), cons(), cons!(), cons_lin(), cons_nln() - jth_congrad(), objcons(), objcons!(), objgrad(), objgrad!() - jac_structure(), jac_structure!(), jac_lin_structure(), jac_nln_structure() All tests pass and module loads without warnings.
|
@tmigot @amontoison please review this pr. |
Co-authored-by: Tangi Migot <tangi.migot@gmail.com>
Co-authored-by: Tangi Migot <tangi.migot@gmail.com>
@arnavk23 Could you please explain what you refactored exactly? |
Files changed / added
Concrete functions moved (representative list)NLP (moved into
NLS (moved into
Why this change fixes the problem
|
Co-authored-by: Tangi Migot <tangi.migot@gmail.com>
Co-authored-by: Tangi Migot <tangi.migot@gmail.com>
Co-authored-by: Tangi Migot <tangi.migot@gmail.com>
Co-authored-by: Tangi Migot <tangi.migot@gmail.com>
There was a problem hiding this comment.
Pull request overview
This PR refactors the NLPModels codebase by separating default function implementations from API definitions. It creates new defaults.jl files for both NLP and NLS models, moves default implementations from api.jl to these new files, and replaces the moved implementations with function stubs in api.jl. This restructuring aims to resolve method overwriting warnings during precompilation.
Key Changes:
- Created
src/nlp/defaults.jlandsrc/nls/defaults.jlto house default implementations - Modified
src/NLPModels.jlto includedefaults.jlfiles after correspondingapi.jlfiles - Converted select function implementations in
api.jlfiles to stubs (bare function declarations)
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
| src/NLPModels.jl | Updated file inclusion list to add "defaults" between "api" and "counters" |
| src/nlp/api.jl | Replaced selected function implementations with stubs; added NLS-specific documentation for obj() and objcons!() |
| src/nlp/defaults.jl | New file containing default implementations for grad, cons, cons!, cons_lin, cons_nln, jth_congrad, objcons, objcons!, objgrad, objgrad!, jac_structure, jac_structure!, jac_lin_structure, jac_nln_structure, and various jacobian/hessian operator functions |
| src/nls/api.jl | Replaced function implementations with stubs for residual, jacobian, and hessian functions specific to NLS models |
| src/nls/defaults.jl | New file containing default implementations for NLS-specific functions including residual, jac_residual, jac_op_residual, hess_residual, obj, grad!, objcons!, and objgrad! |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| function jtprod_lin(nlp::AbstractNLPModel{T, S}, v::AbstractVector) where {T, S} | ||
| @lencheck nlp.meta.nlin v | ||
| Jtv = S(undef, nlp.meta.nvar) | ||
| return jtprod_lin!(nlp, v, Jtv) | ||
| end |
There was a problem hiding this comment.
Critical signature mismatch: This function is defined without the x parameter, but code in api.jl (lines 509-514 and within implementations at lines 469, 473, 480, 688) expects signatures that include x like jtprod_lin(nlp, x, v) and jtprod_lin!(nlp, x, v, Jtv). Since linear constraints don't depend on x, the signature without x is theoretically correct, but this creates a breaking change where existing API calls will fail with "MethodError: no method matching" errors. Either defaults.jl needs to add x parameter to match existing API, or api.jl needs to be updated to remove x from all linear constraint function calls.
| function jac_lin_op(nlp::AbstractNLPModel{T, S}) where {T, S} | ||
| Jv = S(undef, nlp.meta.nlin) | ||
| Jtv = S(undef, nlp.meta.nvar) | ||
| return jac_lin_op!(nlp, Jv, Jtv) | ||
| end | ||
|
|
||
| function jac_lin_op!( | ||
| nlp::AbstractNLPModel{T, S}, | ||
| Jv::AbstractVector, | ||
| Jtv::AbstractVector, | ||
| ) where {T, S} | ||
| @lencheck nlp.meta.nlin Jv | ||
| @lencheck nlp.meta.nvar Jtv | ||
| prod! = @closure (res, v, α, β) -> begin # res = α * J * v + β * res | ||
| jprod_lin!(nlp, v, Jv) | ||
| if β == 0 | ||
| res .= α .* Jv | ||
| else | ||
| res .= α .* Jv .+ β .* res | ||
| end | ||
| return res | ||
| end | ||
| ctprod! = @closure (res, v, α, β) -> begin | ||
| jtprod_lin!(nlp, v, Jtv) | ||
| if β == 0 | ||
| res .= α .* Jtv | ||
| else | ||
| res .= α .* Jtv .+ β .* res | ||
| end | ||
| return res | ||
| end | ||
| return LinearOperator{T}(nlp.meta.nlin, nlp.meta.nvar, false, false, prod!, ctprod!, ctprod!) | ||
| end |
There was a problem hiding this comment.
Critical signature mismatch: This function is defined without the x parameter (line 489: jac_lin_op(nlp) and line 495: jac_lin_op!(nlp, Jv, Jtv)), but code in api.jl (lines 655-660, 670-697) expects signatures that include x like jac_lin_op(nlp, x) and jac_lin_op!(nlp, x, Jv, Jtv). This creates a signature mismatch where the api.jl wrappers with x parameter won't correctly delegate to these implementations. Either add the x parameter here to match the API, or update api.jl to remove x from linear Jacobian operator functions.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Functions refactored:
Closes #385