Skip to content

Allow default_counters macro to skip functions#560

Merged
dpo merged 1 commit into
mainfrom
default-counters-skip
Mar 12, 2026
Merged

Allow default_counters macro to skip functions#560
dpo merged 1 commit into
mainfrom
default-counters-skip

Conversation

@dpo

@dpo dpo commented Mar 12, 2026

Copy link
Copy Markdown
Member

Skipping certain functions is useful to avoid overwriting methods. If, e.g., neval_hprod is forwarded by

@default_counters Model inner

and the user redefines

NLPModels.neval_hprod(m::Model) = ...

then, neval_hprod is being overwritten, and that prevents precompilation. That is the case in quasi-Newton models.

Copilot AI review requested due to automatic review settings March 12, 2026 19:57

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates how NLP model wrappers can forward evaluation-counter accessors by enhancing @default_counters to support excluding selected forwarded counter functions, and adjusts module include order + tests accordingly.

Changes:

  • Extend @default_counters to accept an optional exclusion list to avoid generating forwarding methods for specific counters.
  • Reorder include("nlp/*.jl") so counters is loaded before utils/api.
  • Expand test coverage around excluded counter forwarding behavior.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/NLPModels.jl Reorders include sequence so counters loads before utils/api.
src/nlp/utils.jl Extends @default_counters with an excluded parameter and updates its docstring.
test/nlp/utils.jl Updates tests to exercise the new excluded argument path.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/nlp/utils.jl Outdated
@dpo

dpo commented Mar 12, 2026

Copy link
Copy Markdown
Member Author

@tmigot @MaxenceGollier The macro @default_counters forwards both the neval_* methods and the counters attribute. Because we have the definition neval_*(nlp) = nlp.counters.neval_* (in counters.jl), forwarding the counters attribute again forwards all the methods. So the part

  push!(
    ex.args,
    :(
      Base.getproperty(nlp::$(esc(Model)), s::Symbol) =
        (s == :counters ? nlp.$inner.counters : getfield(nlp, s))
    ),
  )

is redundant.

In addition, some models do not have a counters attribute (example: quasi-Newton models). So this is all a bit messy. For starters, it would surely be better to define a get_counters() accessor.

Admittedly, this is fairly old code.

@dpo dpo force-pushed the default-counters-skip branch from 4abce4b to 3208a49 Compare March 12, 2026 20:11
Skipping certain functions is useful to avoid overwriting methods.
If, e.g., neval_hprod is forwarded by

    @default_counters Model inner

and the user redefines

    NLPModels.neval_hprod(m::Model) = ...

then, neval_hprod is being overwritten, and that prevents
precompilation. That is the case in quasi-Newton models.
@github-actions

github-actions Bot commented Mar 12, 2026

Copy link
Copy Markdown
Contributor
Package name latest stable
ADNLPModels
AdaptiveRegularization
AmplNLReader
BundleAdjustmentModels
CUTEst
CaNNOLeS
DCISolver
FletcherPenaltySolver
FluxNLPModels
JSOSolvers
JSOSuite
LLSModels
ManualNLPModels
NLPModelsIpopt
NLPModelsJuMP
NLPModelsKnitro
NLPModelsModifiers
NLPModelsTest
NLSProblems
PDENLPModels
PartiallySeparableNLPModels
PartiallySeparableSolvers
Percival
QuadraticModels
RegularizedOptimization
RegularizedProblems
SolverBenchmark
SolverTest
SolverTools

@dpo dpo merged commit b057f72 into main Mar 12, 2026
74 checks passed
@dpo dpo deleted the default-counters-skip branch March 12, 2026 23:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants