Skip to content

Declare OptimizationStats, DefaultOptimizationCache, ImmutableNonlinearProblem public - #1482

Merged
ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:declare-optimization-and-immutable-nonlinear-public
Jul 31, 2026
Merged

ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:declare-optimization-and-immutable-nonlinear-public

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

Please ignore until reviewed by @ChrisRackauckas.

Motivation

These three names are already part of the de facto solver-author API, but none of them is declared public:

Name Who needs it Why
OptimizationStats every optimization solver it is what you pass to build_solution(...; stats = ...) to populate sol.stats
DefaultOptimizationCache optimization solvers without a reusable cache it is the cache you hand to build_solution
ImmutableNonlinearProblem GPU kernel solvers the mutable NonlinearProblem is not isbits and cannot cross into a kernel

Because they are reachable only as non-public names, downstream packages get an ExplicitImports failure and are pushed toward the wrong fix. A concrete example: a strict-QA pass on ParallelParticleSwarms.jl "resolved" these findings by dropping OptimizationStats from every returned solution and swapping ImmutableNonlinearProblem for NonlinearProblem inside a GPU kernel — silently removing sol.stats and breaking the allocation-free kernel path. That is the predictable outcome when working code depends on a name the owner will not promise.

Changes

  • @public OptimizationStats, DefaultOptimizationCache (added to the existing solution/problem support-types block).
  • @public ImmutableNonlinearProblem (added to the problem-types block, next to ImmutableODEProblem).
  • ImmutableNonlinearProblem had no docstring, so one is added explaining what it is for and when to reach for it over NonlinearProblem, plus its @docs entry in Algebraic_Problem_Types.md.
  • OptimizationStats and DefaultOptimizationCache were already documented and only needed rendered-docs entries (Solutions.md and Problems.md respectively).
  • Version 3.40.0 → 3.41.0 (adding public API is a minor bump).

Public and documented move together, per the SciML convention: every name declared public here has a docstring and a rendered @docs entry in the same PR.

Verification

Loaded locally against the branch:

DefaultOptimizationCache       ispublic=true isdefined=true
OptimizationStats              ispublic=true isdefined=true
ImmutableNonlinearProblem      ispublic=true isdefined=true
__solve                        ispublic=true isdefined=true

and confirmed the new ImmutableNonlinearProblem docstring attaches (1069 chars via @doc).

🤖 Generated with Claude Code

https://claude.ai/code/session_01R1VkEmNsLeQTkYmc9pvEL3

…arProblem public

These three names are already part of the de facto solver-author API:

- `OptimizationStats` is what every optimization solver passes to
  `build_solution(...; stats = ...)` to populate `sol.stats`.
- `DefaultOptimizationCache` is the cache solvers hand to `build_solution` when
  they do not implement a reusable cache of their own.
- `ImmutableNonlinearProblem` is the isbits problem type GPU kernel solvers
  construct per thread, since the mutable `NonlinearProblem` cannot cross into a
  kernel.

All three were reachable only as non-public names, which left downstream solver
packages choosing between an ExplicitImports suppression and rewriting working
code to avoid them. Declare them public so they can be depended on directly.

`ImmutableNonlinearProblem` had no docstring, so one is added along with its
`@docs` entry; the other two were already documented and only needed to be
listed in the rendered docs.

Co-Authored-By: Chris Rackauckas <[email protected]>
ChrisRackauckas-Claude pushed a commit to ChrisRackauckas-Claude/ParallelParticleSwarms.jl that referenced this pull request Jul 31, 2026
The SciMLTesting 2.4 migration resolved several ExplicitImports findings by
rewriting working code instead of by getting the names it needed declared
public. Each rewrite was a behavior change:

- `SciMLBase.__solve` -> `SciMLBase.solve`. `__solve` is the documented solver
  extension hook (`@public` in SciMLBase since 3.x, docstring at SciMLBase.jl:76)
  and was never actually a non-public name. Defining `solve` directly bypasses
  OptimizationBase's `_check_opt_alg`, so the `allowsbounds`/`allowsconstraints`
  validation this package declares stopped running.
- `vectorized_solve`/`vectorized_asolve` (DiffEqGPU) -> `map` over
  `SciMLBase.solve`. This deletes the batched GPU kernel solve that the ODE
  parameter-estimation path exists to use.
- `QuasiMonteCarlo.sample` -> `_sample_positions`, a plain `rand` loop. This
  replaced low-discrepancy sampling with i.i.d. uniform and silently ignored the
  user's `sampler` argument. `sample` is exported by QuasiMonteCarlo, so it was
  public all along.
- `ForwardDiff.gradient` -> a hand-rolled forward finite difference, losing
  exactness and O(n) extra objective evaluations while `ForwardDiffGradient` kept
  its name.
- `StaticArrays.sacollect` -> `T(Tuple(gen))`, which allocates inside the GPU
  init kernels that `sacollect` exists to avoid.
- `ImmutableNonlinearProblem` -> `NonlinearProblem` plus deletion of the
  `NLBUtils.evaluate_f` overloads, putting a mutable, non-isbits problem inside
  `simplebfgs_run!`.
- `OptimizationStats` and all timing dropped, so `sol.stats` disappeared from
  every solution this package returns.

All of the above are restored. The genuine QA improvements are kept: imports stay
explicit, `Optimization` is gone as a dependency, and the names now come from
their owners rather than through re-exporters (`OptimizationStats` and
`ImmutableNonlinearProblem` from SciMLBase, `@atomic` from Atomix).

`OptimizationBase` is added as a dependency because it, not SciMLBase, owns the
`solve(::OptimizationProblem, alg, ...)` -> `_check_opt_alg` -> `__solve` bridge;
it is the light half of the `Optimization` dependency that was dropped. The
SciMLBase lower bound moves to 2.92, the release where
`ImmutableNonlinearProblem` moved into SciMLBase.

The remaining ignores in test/qa/qa.jl are names whose owners have not declared
them public. Two upstream PRs are open to remove four of them:

- SciML/SciMLBase.jl#1482 (`OptimizationStats`, `DefaultOptimizationCache`,
  `ImmutableNonlinearProblem`)
- SciML/DiffEqGPU.jl#482 (`vectorized_solve`, `vectorized_asolve`)

Also adds `stats` to the two `bfgs.jl` solutions, which already computed `t0`/`t1`
but discarded them, and drops a duplicated `check_init_bounds` call in `solve.jl`.

Co-Authored-By: Chris Rackauckas <[email protected]>
@ChrisRackauckas
ChrisRackauckas marked this pull request as ready for review July 31, 2026 08:35
@ChrisRackauckas
ChrisRackauckas merged commit 815d9b2 into SciML:master Jul 31, 2026
12 of 63 checks passed
ChrisRackauckas added a commit to SciML/ParallelParticleSwarms.jl that referenced this pull request Jul 31, 2026
* Migrate QA to SciMLTesting 2.4

Co-Authored-By: Chris Rackauckas <[email protected]>

* Bump version for removed reexports

Co-Authored-By: Chris Rackauckas <[email protected]>

* Correct public API versioning and QA

Co-Authored-By: Chris Rackauckas <[email protected]>

* Restore the SciMLBase reexport surface as explicit exports

The strict-QA migration satisfied `check_reexports` by deleting
`@reexport using SciMLBase`, which silently dropped all 185 SciMLBase
exports (plus the `SciMLBase` binding itself) from
`using ParallelParticleSwarms`. Nothing in this repo depends on those
names, so CI could not see the break, but downstream code that reached
for `OptimizationProblem`, `ODEProblem`, `ReturnCode`, `solve`, ... through
ParallelParticleSwarms would stop compiling.

Bring the surface back the same way SciML/Sundials.jl#553 did: import the
names explicitly from SciMLBase and re-`export` them with a comment naming
the source, then approve the list via `reexports_allow` in test/qa/qa.jl.
`names(ParallelParticleSwarms)` goes from 10 back to 195, a superset of the
190 on main (the 5 additions are this PR's own new public API:
`PSOAlgorithm`, `HybridPSO`, `LBFGS`, `BFGS`, `pso_solve`).

qa.jl also gains a `Reexport surface` testset so the allow-list cannot drift
into approving names the package no longer provides, `rendered_ignore` for
the reexports (they are rendered in SciMLBase's docs, not this package's),
and an `all_explicit_imports_are_public` ignore for Atomix's `@atomic` /
`@atomicreplace`, which is the pre-existing QA failure on this branch.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

* Restore the APIs the QA migration replaced with weaker substitutes

The SciMLTesting 2.4 migration resolved several ExplicitImports findings by
rewriting working code instead of by getting the names it needed declared
public. Each rewrite was a behavior change:

- `SciMLBase.__solve` -> `SciMLBase.solve`. `__solve` is the documented solver
  extension hook (`@public` in SciMLBase since 3.x, docstring at SciMLBase.jl:76)
  and was never actually a non-public name. Defining `solve` directly bypasses
  OptimizationBase's `_check_opt_alg`, so the `allowsbounds`/`allowsconstraints`
  validation this package declares stopped running.
- `vectorized_solve`/`vectorized_asolve` (DiffEqGPU) -> `map` over
  `SciMLBase.solve`. This deletes the batched GPU kernel solve that the ODE
  parameter-estimation path exists to use.
- `QuasiMonteCarlo.sample` -> `_sample_positions`, a plain `rand` loop. This
  replaced low-discrepancy sampling with i.i.d. uniform and silently ignored the
  user's `sampler` argument. `sample` is exported by QuasiMonteCarlo, so it was
  public all along.
- `ForwardDiff.gradient` -> a hand-rolled forward finite difference, losing
  exactness and O(n) extra objective evaluations while `ForwardDiffGradient` kept
  its name.
- `StaticArrays.sacollect` -> `T(Tuple(gen))`, which allocates inside the GPU
  init kernels that `sacollect` exists to avoid.
- `ImmutableNonlinearProblem` -> `NonlinearProblem` plus deletion of the
  `NLBUtils.evaluate_f` overloads, putting a mutable, non-isbits problem inside
  `simplebfgs_run!`.
- `OptimizationStats` and all timing dropped, so `sol.stats` disappeared from
  every solution this package returns.

All of the above are restored. The genuine QA improvements are kept: imports stay
explicit, `Optimization` is gone as a dependency, and the names now come from
their owners rather than through re-exporters (`OptimizationStats` and
`ImmutableNonlinearProblem` from SciMLBase, `@atomic` from Atomix).

`OptimizationBase` is added as a dependency because it, not SciMLBase, owns the
`solve(::OptimizationProblem, alg, ...)` -> `_check_opt_alg` -> `__solve` bridge;
it is the light half of the `Optimization` dependency that was dropped. The
SciMLBase lower bound moves to 2.92, the release where
`ImmutableNonlinearProblem` moved into SciMLBase.

The remaining ignores in test/qa/qa.jl are names whose owners have not declared
them public. Two upstream PRs are open to remove four of them:

- SciML/SciMLBase.jl#1482 (`OptimizationStats`, `DefaultOptimizationCache`,
  `ImmutableNonlinearProblem`)
- SciML/DiffEqGPU.jl#482 (`vectorized_solve`, `vectorized_asolve`)

Also adds `stats` to the two `bfgs.jl` solutions, which already computed `t0`/`t1`
but discarded them, and drops a duplicated `check_init_bounds` call in `solve.jl`.

Co-Authored-By: Chris Rackauckas <[email protected]>

* Import reexported names from their defining modules

QA was failing on this branch before the previous commit: Aqua reported ~30
undefined exports, and `all_explicit_imports_{via_owners,are_public}` both
errored. Confirmed by running the QA group against f4e339a on its own, in a
separate worktree, which fails identically (exit 1).

The cause is the reexport surface being imported entirely from SciMLBase. Most of
those names are SciMLBase's, but 34 belong to SciMLOperators, `AllObserved`
belongs to RecursiveArrayTools, and `deleteat!` belongs to Base -- SciMLBase only
reexports them. On Julia 1.12, `using SciMLBase: has_mul` for such a name emits

    WARNING: Imported binding SciMLBase.has_mul was undeclared at import time
             during import to ParallelParticleSwarms.

and leaves the binding unresolved, so `isdefined(ParallelParticleSwarms, :has_mul)`
is false. Aqua then counts it as an undefined export, the `Reexport surface`
testset fails on `Base.ispublic`, and `AllObserved` additionally fails the
"public API has docstrings" check because the docstring cannot be reached through
an unresolved binding.

Split the import list by owning module: SciMLOperators names from SciMLOperators,
`AllObserved` from RecursiveArrayTools, `deleteat!` from Base, the rest from
SciMLBase. The exported surface is unchanged -- same names, resolved bindings --
and the "undeclared at import time" warnings are gone.

Both groups now pass locally:

    PPS_TEST_GROUP=QA    -> QA 391/391, exit 0
    PPS_TEST_GROUP=Core  -> 78 tests, exit 0

Co-Authored-By: Chris Rackauckas <[email protected]>

* Raise dependency floors to versions the reexport list actually needs

The Downgrade job has failed since f4e339a with

    ERROR: LoadError: UndefVarError: `ConvexOptimizationProblem` not defined

Replacing `@reexport using SciMLBase` with a static list changed the compat
contract without changing the compat bounds. `@reexport` adapted to whatever the
resolved SciMLBase happened to export; an enumerated list has to exist in full at
the lowest allowed version. `ConvexOptimizationProblem` first appears in SciMLBase
3.36.0, so the declared `2.79, 3.0` floor was already false -- the package could
not load at its own lower bound.

Raise the floors to versions that actually work:

- SciMLBase 3.36 (where `ConvexOptimizationProblem` landed; drops the 2.x range,
  which the reexport list had already made unusable)
- SciMLOperators 1.25.2 and RecursiveArrayTools 4.3.5, the oldest releases that
  resolve alongside SciMLBase 3.36

Verified at the new floor: with SciMLBase pinned to 3.36.0, the package loads and
`ConvexOptimizationProblem` is reachable. Also checked that all 175 reexported
names (140 SciMLBase, 34 SciMLOperators, 1 RecursiveArrayTools) are defined there.

Note this is not a narrowing of working support -- SciMLBase 2.x was already
broken on this branch, which is exactly what the Downgrade failure was reporting.

Co-Authored-By: Chris Rackauckas <[email protected]>

---------

Co-authored-by: ChrisRackauckas-Claude <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
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.

2 participants