Skip to content

Conversation

@arnavk23
Copy link

@arnavk23 arnavk23 commented Oct 3, 2025

Original allocation: 2,110,176+ bytes
Final allocation: 39,360 bytes
Improvement: 98.1% reduction in memory allocations

Technical Implementation

  • Implemented in-place QuadraticModel updates to minimize allocations Preserved all functionality including skip_sigma logic Maintained API compatibility
  • The 39KB remaining allocation in R2N is from unavoidable LinearOperator arithmetic operations (Bk + σ * Id) required by the QuadraticModels.jl API. This represents a massive 98% improvement and achieves the core goal of JSO compliance while maintaining full functionality.

Closes #220

…rd LLSModels.jl

R2NModel → QuadraticModels: Successfully replaced custom R2NModel with standard QuadraticModels.jl
All algorithms working: LM, LMTR, R2N, TR, TRDH, R2DH all pass their functional tests
Clean codebase: Removed custom model files and updated all dependencies

Original allocation: 2,110,176+ bytes
Final allocation: 39,360 bytes
Improvement: 98.1% reduction in memory allocations

All functional tests pass: All algorithm tests pass, confirming correctness
All other allocation tests pass: R2, R2DH, TRDH achieve zero allocations
R2N allocation test: 39KB remaining (down from 2MB+)

Updated Project.toml with new JSO dependencies
Implemented in-place QuadraticModel updates to minimize allocations
Preserved all functionality including skip_sigma logic
Maintained API compatibility
The 39KB remaining allocation in R2N is from unavoidable LinearOperator arithmetic operations (Bk + σ * Id) required by the QuadraticModels.jl API. This represents a massive 98% improvement and achieves the core goal of JSO compliance while maintaining full functionality.
@arnavk23
Copy link
Author

arnavk23 commented Oct 3, 2025

@dpo @MaxenceGollier please review this pr by testing the changes on your system after adding the two packages.

@dpo dpo requested review from MaxenceGollier and Copilot October 10, 2025 14:38
Copy link
Contributor

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.

Pull Request Overview

This PR replaces custom model types LMModel and R2NModel with standard QuadraticModels to achieve a 98.1% reduction in memory allocations (from 2.1MB to 39KB). The refactoring maintains full API compatibility while implementing JSO compliance.

  • Eliminated custom model types in favor of QuadraticModels.jl
  • Implemented in-place updates and mutable wrappers to minimize allocations
  • Added ShiftedHessian wrapper for efficient B + σI operations without reallocating operators

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
tmp/repro_alloc.jl Test script to measure allocation improvements
src/TR_alg.jl Updated to use QuadraticModel and access Hessian via data.H
src/RegularizedOptimization.jl Removed custom model includes, added QuadraticModels dependency
src/R2NModel.jl Deleted - replaced by QuadraticModel
src/R2N.jl Major refactor with ShiftedHessian wrapper and in-place QuadraticModel updates
src/LM_alg.jl Updated to use QuadraticModel for LM subproblems
src/LMTR_alg.jl Updated to use QuadraticModel for LMTR subproblems
src/LMModel.jl Deleted - replaced by QuadraticModel

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

arnavk23 and others added 3 commits October 10, 2025 20:11
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
@arnavk23 arnavk23 requested a review from Copilot October 10, 2025 20:35
Copy link
Contributor

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.

Pull Request Overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 6 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

arnavk23 and others added 6 commits October 11, 2025 02:54
@arnavk23 arnavk23 requested a review from Copilot October 11, 2025 08:09
Copy link
Contributor

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.

Pull Request Overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 8 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@dpo
Copy link
Member

dpo commented Oct 11, 2025

@arnavk23 I think what we wanted to do in #220 is add the modules LLSModels.jl and QuadraticModels.jl as dependencies, but first, we have to add missing functionality in those modules. Some of your code here can probably be reused there.

@arnavk23
Copy link
Author

arnavk23 commented Oct 12, 2025

@dpo I made some new changes to the pr.

…fk hk=hk σk=σk ν=ν end

  end
  return (xk, fk, hk, σk, ν, solver)
end
@MaxenceGollier
Copy link
Collaborator

Hi @arnavk23, sorry I have been busy elsewhere lately.

As @dpo said, I think we should add features to QuadraticModels.jl and LLSModels.jl before we could solve #220.
Also, you commited the Manifest.toml and there are some weird changes in the diff.

The problem I have with QuadraticModels is that, if I remember correctly, it allocates and contains type instabilities.
Moreover, neither LLModels.jl nor QuadraticModels supports quadratic regularization.

Finally, we recently added a skip_sigma keyword argument in the objective of the R2NModel and we should add something similar for regularized QuadraticModels or LLSModels.

@arnavk23 arnavk23 requested a review from Copilot October 17, 2025 18:13
Copy link
Contributor

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.

Pull Request Overview

Copilot reviewed 8 out of 10 changed files in this pull request and generated 10 comments.

Comments suppressed due to low confidence (1)

src/LM_alg.jl:1

  • This signature removes the previous sub_kwargs forwarding of extra keyword arguments to the inner subsolver. If external code relies on passing subproblem options, this is a breaking change. Consider accepting kwargs... and forwarding them in the inner solve! calls.
export LM, LMSolver, solve!

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines 123 to 126
x0_quad,
reg_hess_op,
reg_hess_wrapper,
)
Copy link

Copilot AI Oct 17, 2025

Choose a reason for hiding this comment

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

R2NSolver adds 4 new fields (x0_quad, reg_hess, reg_hess_wrapper, reg_hess_op) but the constructor appends only 3 values. This leaves reg_hess_op uninitialized and mis-assigns reg_hess to reg_hess_op, causing construction errors. Append a fourth argument and align ordering, e.g.:
x0_quad, reg_hess_op, reg_hess_wrapper, reg_hess_op
(or drop the redundant reg_hess field).

Copilot uses AI. Check for mistakes.
Comment on lines 449 to 637
(ξ1 < 0 && sqrt_ξ1_νInv > neg_tol) &&
error("LM: prox-gradient step should produce a decrease but ξ1 = $(ξ1)")

# Recompute stationarity measure and solved flag using current ξ and ν
sqrt_ξ1_νInv = ξ 0 ? sqrt/ ν) : sqrt(-ξ / ν)
solved =< 0 && sqrt_ξ1_νInv neg_tol) || 0 && sqrt_ξ1_νInv atol)
Copy link

Copilot AI Oct 17, 2025

Choose a reason for hiding this comment

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

ξ1 was renamed to ξ above, but these lines still reference ξ1, which is undefined. Replace ξ1 with ξ in the condition and error message.

Copilot uses AI. Check for mistakes.
Comment on lines +258 to +259
return qm
end
Copy link

Copilot AI Oct 17, 2025

Choose a reason for hiding this comment

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

The function accepts H but never applies it. Either remove the unused parameter or support in-place Hessian updates:
if H !== nothing; qm.data.H = H; end.

Copilot uses AI. Check for mistakes.
@arnavk23 arnavk23 closed this Oct 18, 2025
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.

Use QuadraticModels and LLSModels

3 participants