Skip to content

Allow generic solver to be passed to Newton method#1176

Open
rsenne wants to merge 15 commits intoJuliaNLSolvers:masterfrom
rsenne:generalize_matrix_support_newton
Open

Allow generic solver to be passed to Newton method#1176
rsenne wants to merge 15 commits intoJuliaNLSolvers:masterfrom
rsenne:generalize_matrix_support_newton

Conversation

@rsenne
Copy link

@rsenne rsenne commented Jul 10, 2025

This PR is in reference to issue #1170. To solve this I have edited the interface to allow a user to pass a generic solver. This in theory would allow one to use any matrix type given the user has that package loaded and there exists a solver designed for that matrix type on board. This also allows one to write a custom solver (i.e., problem specific factorization, strategies for solving) to circumvent the default behavior. I have included some examples of this in testing including:

  1. Different factorizations (would allow users to circumvent the default PositiveFactorizations.jl implementation of cholesky)
  2. Usage of a non-originally supported matrix type (StaticArray)
  3. Test type preservation of Hessian with TwiceDifferentiable object using BlockArrays.jl
  4. A more realistic example of a non-trivial use case (block-tridiagonal hessian) this implementation is not totally optimized as it is a proof of principle

Let me know if there are any other tests you would like me to include or changes to the documentation.

@github-actions
Copy link
Contributor

github-actions bot commented Jul 10, 2025

Benchmark Results

master 69f9d02... master / 69f9d02...
multivariate/solvers/first_order/AdaMax 0.646 ± 0.0079 ms 0.646 ± 0.0077 ms 0.999 ± 0.017
multivariate/solvers/first_order/Adam 0.645 ± 0.0076 ms 0.647 ± 0.0076 ms 0.998 ± 0.017
multivariate/solvers/first_order/BFGS 0.224 ± 0.0043 ms 0.224 ± 0.0041 ms 0.999 ± 0.026
multivariate/solvers/first_order/ConjugateGradient 0.048 ± 0.00066 ms 0.0483 ± 0.00065 ms 0.994 ± 0.019
multivariate/solvers/first_order/GradientDescent 1.71 ± 0.016 ms 1.71 ± 0.013 ms 0.999 ± 0.012
multivariate/solvers/first_order/LBFGS 0.219 ± 0.0038 ms 0.22 ± 0.0036 ms 0.997 ± 0.024
multivariate/solvers/first_order/MomentumGradientDescent 2.51 ± 0.016 ms 2.51 ± 0.016 ms 1 ± 0.0092
multivariate/solvers/first_order/NGMRES 0.554 ± 0.011 ms 0.554 ± 0.01 ms 0.999 ± 0.027
time_to_load 0.505 ± 0.0083 s 0.514 ± 0.0049 s 0.983 ± 0.019

Benchmark Plots

A plot of the benchmark results have been uploaded as an artifact to the workflow run for this PR.
Go to "Actions"->"Benchmark a pull request"->[the most recent run]->"Artifacts" (at the bottom).

@pkofod
Copy link
Member

pkofod commented Jul 17, 2025

Thanks, I'll review :)

@codecov
Copy link

codecov bot commented Jul 17, 2025

Codecov Report

❌ Patch coverage is 90.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 85.75%. Comparing base (6850998) to head (69f9d02).
⚠️ Report is 4 commits behind head on master.

Files with missing lines Patch % Lines
src/multivariate/solvers/second_order/newton.jl 90.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1176      +/-   ##
==========================================
+ Coverage   85.70%   85.75%   +0.04%     
==========================================
  Files          46       46              
  Lines        3596     3601       +5     
==========================================
+ Hits         3082     3088       +6     
+ Misses        514      513       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

g = gradient(d)

# Clean and simple - just call the user's solve function
state.s .= method.solve(H, g)
Copy link
Contributor

Choose a reason for hiding this comment

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

For the default solver, this seems less efficient than the current implementation. AFAICT this would introduce a regression as currently for Arrays both the cholesky decomposition of the Hessian is computed in place and then also the in-place ldiv! solver is used.

@rsenne
Copy link
Author

rsenne commented Nov 6, 2025

Hi @devmotion. You're right that's my mistake. In honesty I somewhat forgot what I was doing on this PR so my apologies for the late reply. I think i have fixed the regression I introduced. Let me know if there are any other issues

@github-actions
Copy link
Contributor

github-actions bot commented Nov 6, 2025

Benchmark Results (Julia vlts)

Time benchmarks
master a9a9a0c... master / a9a9a0c...
multivariate/solvers/first_order/AdaMax 0.544 ± 0.0091 ms 0.547 ± 0.0094 ms 0.995 ± 0.024
multivariate/solvers/first_order/Adam 0.542 ± 0.0095 ms 0.545 ± 0.01 ms 0.995 ± 0.026
multivariate/solvers/first_order/BFGS 0.265 ± 0.0082 ms 0.265 ± 0.0082 ms 1 ± 0.044
multivariate/solvers/first_order/ConjugateGradient 0.177 ± 0.0033 ms 0.178 ± 0.0034 ms 0.997 ± 0.027
multivariate/solvers/first_order/GradientDescent 1.56 ± 0.014 ms 1.57 ± 0.011 ms 0.997 ± 0.011
multivariate/solvers/first_order/LBFGS 0.235 ± 0.0078 ms 0.235 ± 0.0086 ms 0.997 ± 0.049
multivariate/solvers/first_order/MomentumGradientDescent 2.19 ± 0.024 ms 2.19 ± 0.013 ms 1 ± 0.013
multivariate/solvers/first_order/NGMRES 0.439 ± 0.011 ms 0.439 ± 0.012 ms 0.998 ± 0.037
time_to_load 0.29 ± 0.0038 s 2.91 ± 0.031 s 0.0999 ± 0.0017
Memory benchmarks
master a9a9a0c... master / a9a9a0c...
multivariate/solvers/first_order/AdaMax 0.34 k allocs: 7.28 kB 0.34 k allocs: 7.28 kB 1
multivariate/solvers/first_order/Adam 0.34 k allocs: 7.28 kB 0.34 k allocs: 7.28 kB 1
multivariate/solvers/first_order/BFGS 0.281 k allocs: 13.3 kB 0.281 k allocs: 13.3 kB 1
multivariate/solvers/first_order/ConjugateGradient 0.294 k allocs: 14.7 kB 0.294 k allocs: 14.7 kB 1
multivariate/solvers/first_order/GradientDescent 1.73 k allocs: 0.0757 MB 1.73 k allocs: 0.0757 MB 1
multivariate/solvers/first_order/LBFGS 0.298 k allocs: 15 kB 0.298 k allocs: 15 kB 1
multivariate/solvers/first_order/MomentumGradientDescent 2.07 k allocs: 0.081 MB 2.07 k allocs: 0.081 MB 1
multivariate/solvers/first_order/NGMRES 1.39 k allocs: 0.114 MB 1.39 k allocs: 0.114 MB 1
time_to_load 0.153 k allocs: 14.5 kB 0.153 k allocs: 14.5 kB 1

A plot of the benchmark results has been uploaded as an artifact at .

rsenne and others added 6 commits November 7, 2025 10:34
Co-authored-by: David Müller-Widmann <devmotion@users.noreply.github.com>
Co-authored-by: David Müller-Widmann <devmotion@users.noreply.github.com>
Co-authored-by: David Müller-Widmann <devmotion@users.noreply.github.com>
Co-authored-by: David Müller-Widmann <devmotion@users.noreply.github.com>
Copilot AI review requested due to automatic review settings January 9, 2026 07:05
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@github-actions
Copy link
Contributor

github-actions bot commented Jan 9, 2026

Benchmark Results (Julia v1)

Time benchmarks
master a9a9a0c... master / a9a9a0c...
multivariate/solvers/first_order/AdaMax 0.549 ± 0.0082 ms 0.546 ± 0.0084 ms 1 ± 0.022
multivariate/solvers/first_order/Adam 0.546 ± 0.0083 ms 0.544 ± 0.0089 ms 1 ± 0.022
multivariate/solvers/first_order/BFGS 0.207 ± 0.0053 ms 0.205 ± 0.0055 ms 1.01 ± 0.038
multivariate/solvers/first_order/ConjugateGradient 0.0482 ± 0.00086 ms 0.0479 ± 0.00071 ms 1.01 ± 0.023
multivariate/solvers/first_order/GradientDescent 1.59 ± 0.016 ms 1.59 ± 0.018 ms 1 ± 0.015
multivariate/solvers/first_order/LBFGS 0.213 ± 0.0062 ms 0.214 ± 0.0055 ms 0.995 ± 0.039
multivariate/solvers/first_order/MomentumGradientDescent 2.3 ± 0.026 ms 2.31 ± 0.027 ms 0.997 ± 0.016
multivariate/solvers/first_order/NGMRES 0.526 ± 0.01 ms 0.524 ± 0.0093 ms 1 ± 0.026
time_to_load 0.296 ± 0.00066 s 5.13 ± 0.11 s 0.0577 ± 0.0013
Memory benchmarks
master a9a9a0c... master / a9a9a0c...
multivariate/solvers/first_order/AdaMax 0.354 k allocs: 7.19 kB 0.354 k allocs: 7.19 kB 1
multivariate/solvers/first_order/Adam 0.354 k allocs: 7.19 kB 0.354 k allocs: 7.19 kB 1
multivariate/solvers/first_order/BFGS 0.263 k allocs: 9.66 kB 0.263 k allocs: 9.66 kB 1
multivariate/solvers/first_order/ConjugateGradient 0.108 k allocs: 4.89 kB 0.108 k allocs: 4.89 kB 1
multivariate/solvers/first_order/GradientDescent 2 k allocs: 0.0708 MB 2 k allocs: 0.0708 MB 1
multivariate/solvers/first_order/LBFGS 0.336 k allocs: 13.7 kB 0.336 k allocs: 13.7 kB 1
multivariate/solvers/first_order/MomentumGradientDescent 2.39 k allocs: 0.0798 MB 2.39 k allocs: 0.0798 MB 1
multivariate/solvers/first_order/NGMRES 2.62 k allocs: 0.129 MB 2.62 k allocs: 0.129 MB 1
time_to_load 0.145 k allocs: 11 kB 0.159 k allocs: 11.6 kB 0.947

A plot of the benchmark results has been uploaded as an artifact at .

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.

4 participants