qqn-torch 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. qqn_torch-0.1.0/.github/workflows/python-publish.yml +80 -0
  2. qqn_torch-0.1.0/.gitignore +24 -0
  3. qqn_torch-0.1.0/.llmignore +11 -0
  4. qqn_torch-0.1.0/LICENSE +201 -0
  5. qqn_torch-0.1.0/PKG-INFO +246 -0
  6. qqn_torch-0.1.0/README.md +220 -0
  7. qqn_torch-0.1.0/algorithm.md +443 -0
  8. qqn_torch-0.1.0/build.sh +14 -0
  9. qqn_torch-0.1.0/experiments/__init__.py +24 -0
  10. qqn_torch-0.1.0/experiments/config.py +114 -0
  11. qqn_torch-0.1.0/experiments/data/__init__.py +11 -0
  12. qqn_torch-0.1.0/experiments/data/loaders.py +128 -0
  13. qqn_torch-0.1.0/experiments/data/subset.py +73 -0
  14. qqn_torch-0.1.0/experiments/driver.py +186 -0
  15. qqn_torch-0.1.0/experiments/env.py +80 -0
  16. qqn_torch-0.1.0/experiments/fashion_mnist_mlp_comparison.py +44 -0
  17. qqn_torch-0.1.0/experiments/metrics/__init__.py +12 -0
  18. qqn_torch-0.1.0/experiments/metrics/milestones.py +31 -0
  19. qqn_torch-0.1.0/experiments/metrics/pareto.py +35 -0
  20. qqn_torch-0.1.0/experiments/metrics/result.py +54 -0
  21. qqn_torch-0.1.0/experiments/models/__init__.py +19 -0
  22. qqn_torch-0.1.0/experiments/models/activations.py +100 -0
  23. qqn_torch-0.1.0/experiments/models/mlp.py +153 -0
  24. qqn_torch-0.1.0/experiments/models/pytree_mlp.py +123 -0
  25. qqn_torch-0.1.0/experiments/models/topology.py +49 -0
  26. qqn_torch-0.1.0/experiments/optimizers/__init__.py +17 -0
  27. qqn_torch-0.1.0/experiments/optimizers/eval_counting.py +34 -0
  28. qqn_torch-0.1.0/experiments/optimizers/profiles.py +218 -0
  29. qqn_torch-0.1.0/experiments/optimizers/runners.py +229 -0
  30. qqn_torch-0.1.0/experiments/reporting/__init__.py +17 -0
  31. qqn_torch-0.1.0/experiments/reporting/plots.py +65 -0
  32. qqn_torch-0.1.0/experiments/reporting/sparse_plots.py +148 -0
  33. qqn_torch-0.1.0/experiments/reporting/tables.py +246 -0
  34. qqn_torch-0.1.0/experiments/sparse_config.py +86 -0
  35. qqn_torch-0.1.0/experiments/sparse_driver.py +418 -0
  36. qqn_torch-0.1.0/experiments/tests/__init__.py +1 -0
  37. qqn_torch-0.1.0/experiments/tests/test_fairness.py +95 -0
  38. qqn_torch-0.1.0/py.typed +0 -0
  39. qqn_torch-0.1.0/pyproject.toml +50 -0
  40. qqn_torch-0.1.0/qqn-torch.iml +9 -0
  41. qqn_torch-0.1.0/qqn_torch/__init__.py +10 -0
  42. qqn_torch-0.1.0/qqn_torch/_flatten.py +53 -0
  43. qqn_torch-0.1.0/qqn_torch/_vendor/__init__.py +1 -0
  44. qqn_torch-0.1.0/qqn_torch/_vendor/strong_wolfe.py +159 -0
  45. qqn_torch-0.1.0/qqn_torch/line_search/__init__.py +35 -0
  46. qqn_torch-0.1.0/qqn_torch/line_search/armijo.py +121 -0
  47. qqn_torch-0.1.0/qqn_torch/line_search/backtracking.py +48 -0
  48. qqn_torch-0.1.0/qqn_torch/line_search/base.py +38 -0
  49. qqn_torch-0.1.0/qqn_torch/line_search/fixed.py +24 -0
  50. qqn_torch-0.1.0/qqn_torch/line_search/strong_wolfe.py +74 -0
  51. qqn_torch-0.1.0/qqn_torch/optimizer.py +205 -0
  52. qqn_torch-0.1.0/qqn_torch/oracles/__init__.py +30 -0
  53. qqn_torch-0.1.0/qqn_torch/oracles/base.py +26 -0
  54. qqn_torch-0.1.0/qqn_torch/oracles/lbfgs.py +79 -0
  55. qqn_torch-0.1.0/qqn_torch/oracles/momentum.py +31 -0
  56. qqn_torch-0.1.0/qqn_torch/oracles/secant.py +30 -0
  57. qqn_torch-0.1.0/qqn_torch/path.py +25 -0
  58. qqn_torch-0.1.0/qqn_torch/regions/__init__.py +37 -0
  59. qqn_torch-0.1.0/qqn_torch/regions/base.py +20 -0
  60. qqn_torch-0.1.0/qqn_torch/regions/box.py +27 -0
  61. qqn_torch-0.1.0/qqn_torch/regions/identity.py +11 -0
  62. qqn_torch-0.1.0/qqn_torch/regions/sequential.py +21 -0
  63. qqn_torch-0.1.0/qqn_torch/regions/trust.py +45 -0
  64. qqn_torch-0.1.0/run_reports.js +460 -0
  65. qqn_torch-0.1.0/tests/test_flatten.py +55 -0
  66. qqn_torch-0.1.0/tests/test_line_search.py +150 -0
  67. qqn_torch-0.1.0/tests/test_optimizer_options.py +87 -0
  68. qqn_torch-0.1.0/tests/test_oracles.py +100 -0
  69. qqn_torch-0.1.0/tests/test_performance.py +176 -0
  70. qqn_torch-0.1.0/tests/test_quadratic.py +83 -0
  71. qqn_torch-0.1.0/tests/test_regions.py +98 -0
@@ -0,0 +1,80 @@
1
+ # This workflow will upload a Python Package to PyPI when a release is created
2
+ # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python#publishing-to-package-registries
3
+
4
+ # This workflow uses actions that are not certified by GitHub.
5
+ # They are provided by a third-party and are governed by
6
+ # separate terms of service, privacy policy, and support
7
+ # documentation.
8
+
9
+ name: Upload Python Package
10
+
11
+ on:
12
+ release:
13
+ types: [published]
14
+
15
+ permissions:
16
+ contents: read
17
+
18
+ # Avoid concurrent publish runs for the same release.
19
+ concurrency:
20
+ group: publish-${{ github.event.release.tag_name }}
21
+ cancel-in-progress: false
22
+
23
+ jobs:
24
+ release-build:
25
+ runs-on: ubuntu-latest
26
+
27
+ steps:
28
+ - uses: actions/checkout@v4
29
+
30
+ - uses: actions/setup-python@v5
31
+ with:
32
+ python-version: "3.x"
33
+ cache: "pip"
34
+
35
+ - name: Build release distributions
36
+ run: |
37
+ python -m pip install --upgrade pip
38
+ python -m pip install build
39
+ python -m build
40
+
41
+ - name: Check distributions with twine
42
+ run: |
43
+ python -m pip install twine
44
+ python -m twine check dist/*
45
+
46
+ - name: Upload distributions
47
+ uses: actions/upload-artifact@v4
48
+ with:
49
+ name: release-dists
50
+ path: dist/
51
+ if-no-files-found: error
52
+
53
+ pypi-publish:
54
+ runs-on: ubuntu-latest
55
+ needs:
56
+ - release-build
57
+ permissions:
58
+ # IMPORTANT: this permission is mandatory for trusted publishing
59
+ id-token: write
60
+
61
+ # Dedicated environments with protections for publishing are strongly recommended.
62
+ # For more information, see: https://docs.github.com/en/actions/deployment/targeting-different-environments/using-environments-for-deployment#deployment-protection-rules
63
+ environment:
64
+ name: pypi
65
+ url: https://pypi.org/p/qqn-torch
66
+
67
+ steps:
68
+ - name: Retrieve release distributions
69
+ uses: actions/download-artifact@v4
70
+ with:
71
+ name: release-dists
72
+ path: dist/
73
+
74
+ - name: Publish release distributions to PyPI
75
+ # Trusted publishing (OIDC) is used via the id-token permission above;
76
+ # no username/password or API token is required.
77
+ uses: pypa/gh-action-pypi-publish@release/v1
78
+ with:
79
+ packages-dir: dist/
80
+ print-hash: true
@@ -0,0 +1,24 @@
1
+ # Virtual environment
2
+ .venv/
3
+ venv/
4
+
5
+ # Python junk
6
+ __pycache__/
7
+ *.py[cod]
8
+ *.egg-info/
9
+ .eggs/
10
+ dist/
11
+ build/
12
+ .idea/
13
+
14
+ # Testing / coverage
15
+ .pytest_cache/
16
+ .coverage
17
+ htmlcov/
18
+
19
+ # OS junk
20
+ .DS_Store
21
+ _mnist_data
22
+ profiles/
23
+ #results/
24
+ _fashion_mnist_data
@@ -0,0 +1,11 @@
1
+ .idea
2
+ .venv
3
+ *.iml
4
+ *.png
5
+ #tests/
6
+ examples/
7
+ experiments/
8
+ *.md
9
+ LICENSE
10
+ results/
11
+ *.js
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,246 @@
1
+ Metadata-Version: 2.4
2
+ Name: qqn-torch
3
+ Version: 0.1.0
4
+ Summary: Quadratic Quasi-Newton optimizer for torch
5
+ Project-URL: Homepage, https://github.com/SimiaCryptus/qqn-torch
6
+ Project-URL: Repository, https://github.com/SimiaCryptus/qqn-torch
7
+ Author: QQN-torch Contributors
8
+ License: Apache License 2.0
9
+ License-File: LICENSE
10
+ Keywords: lbfgs,optimization,qqn,quasi-newton,torch
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Intended Audience :: Science/Research
13
+ Classifier: License :: OSI Approved :: Apache Software License
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Topic :: Scientific/Engineering :: Mathematics
16
+ Requires-Python: >=3.9
17
+ Requires-Dist: torch>=1.12
18
+ Provides-Extra: bench
19
+ Requires-Dist: matplotlib>=3.5; extra == 'bench'
20
+ Requires-Dist: torchvision>=0.13; extra == 'bench'
21
+ Provides-Extra: dev
22
+ Requires-Dist: numpy>=1.22; extra == 'dev'
23
+ Requires-Dist: pytest-cov>=4.0; extra == 'dev'
24
+ Requires-Dist: pytest>=7.0; extra == 'dev'
25
+ Description-Content-Type: text/markdown
26
+
27
+ # qqn-torch
28
+
29
+ **QQN (Quadratic Quasi-Newton)** — a drop-in replacement for
30
+ `torch.optim.LBFGS` that searches a *quadratic path* blending the
31
+ steepest-descent and quasi-Newton directions.
32
+
33
+ ## What is QQN?
34
+
35
+ Classic quasi-Newton methods (like L-BFGS) take a single direction `-H∇f`
36
+ and line-search along it. This works well near a minimum but can be fragile
37
+ far from it, where the curvature approximation is unreliable.
38
+
39
+ QQN instead constructs a **quadratic path** that interpolates between the
40
+ steepest-descent direction and the quasi-Newton direction:
41
+
42
+ ```
43
+ d(t) = t(1-t)·(-∇f) + t²·(-H∇f)
44
+ ```
45
+
46
+ with `t ∈ [0, 1]`. The key properties are:
47
+
48
+ | `t` | Behavior |
49
+ |---------------|------------------------------------------|
50
+ | `d(0) = 0` | The path starts at the current iterate. |
51
+ | `d'(0) = -∇f` | The initial tangent is steepest descent. |
52
+ | `d(1) = -H∇f` | The endpoint is the L-BFGS direction. |
53
+
54
+ Because the path *starts* tangent to `-∇f`, the beginning of the path always
55
+ decreases `f` (when `∇f ≠ 0`). This anchors **global convergence**, while the
56
+ `t = 1` endpoint recovers **L-BFGS superlinear behavior** near the optimum.
57
+ The line search walks `t` directly and *discovers* the right blend — no manual
58
+ tuning of a mixing coefficient.
59
+
60
+ ## Installation
61
+
62
+ ```bash
63
+ pip install qqn-torch
64
+ ```
65
+
66
+ Or from source:
67
+
68
+ ```bash
69
+ git clone https://github.com/your-org/qqn-torch
70
+ cd qqn-torch
71
+ pip install -e .
72
+ ```
73
+
74
+ Requires PyTorch.
75
+
76
+ ## Quick Start
77
+
78
+ QQN follows the same `closure`-based API as `torch.optim.LBFGS`:
79
+
80
+ ```python
81
+ import torch
82
+ from qqn_torch import QQN
83
+
84
+ # A simple quadratic objective.
85
+ x = torch.tensor([1.5, -2.0], requires_grad=True)
86
+
87
+ optimizer = QQN([x], max_iter=20)
88
+
89
+
90
+ def closure():
91
+ optimizer.zero_grad()
92
+ loss = (x[0] - 3.0) ** 2 + (x[1] + 1.0) ** 2
93
+ loss.backward()
94
+ return loss
95
+
96
+
97
+ for _ in range(10):
98
+ loss = optimizer.step(closure)
99
+ print(f"loss = {loss:.6e}, x = {x.detach().tolist()}")
100
+ ```
101
+
102
+ The `closure` must:
103
+
104
+ 1. Clear gradients (`zero_grad()`),
105
+ 2. Compute the loss,
106
+ 3. Call `loss.backward()`,
107
+ 4. Return the loss.
108
+
109
+ This is **identical to the `torch.optim.LBFGS` contract**, so existing LBFGS
110
+ training loops work unchanged.
111
+
112
+ ## Configuration
113
+
114
+ ```python
115
+ QQN(
116
+ params,
117
+ history_size=10, # L-BFGS curvature-pair history
118
+ line_search="armijo", # "armijo" | "backtracking" | "strong_wolfe" | "fixed"
119
+ oracle="lbfgs", # "lbfgs" | "momentum" | "secant" | Oracle instance
120
+ region=None, # None | "box" | "trust" | Region instance
121
+ max_iter=20, # inner iterations per .step()
122
+ tol_grad=1e-7, # gradient-norm stopping tolerance
123
+ tol_change=1e-9, # step/objective change tolerance
124
+ line_search_options=None # dict forwarded to the line search (c1, c2, ...)
125
+ )
126
+ ```
127
+
128
+ ### Four orthogonal, swappable components
129
+
130
+ QQN is built as a **combiner** of four independent pieces. Each can be
131
+ changed without touching the rest.
132
+
133
+ #### 1. Oracle — the `t = 1` endpoint (`-H∇f`)
134
+
135
+ | Name | Description |
136
+ |------------|----------------------------------------------------|
137
+ | `lbfgs` | (default) Two-loop recursion over curvature pairs. |
138
+ | `momentum` | Heavy-ball direction `-(β·v + (1-β)·∇f)`. |
139
+ | `secant` | Barzilai–Borwein scalar step (`O(n)` memory). |
140
+
141
+ Because the steepest-descent contribution anchors convergence, the oracle is
142
+ free to be aggressive — it need not guarantee descent on its own.
143
+
144
+ #### 2. Line search — walks the path and picks `t`
145
+
146
+ | Name | Conditions | Notes |
147
+ |----------------|----------------------------|----------------------------------|
148
+ | `armijo` | Armijo sufficient decrease | (default) Backtracking. |
149
+ | `backtracking` | Armijo sufficient decrease | Aggressive contraction from t=1. |
150
+ | `strong_wolfe` | Armijo + strong curvature | Can over-restrict the path step. |
151
+ | `fixed` | None | Debug/baseline; constant `t`. |
152
+
153
+ The line search is **not** an implementation detail — it is the glue that
154
+ makes the gradient and oracle work together. Convergence quality is bounded
155
+ by line-search quality.
156
+
157
+ #### 3. Region — optional projection of candidate points
158
+
159
+ | Name | Description |
160
+ |---------|-------------------------------------------|
161
+ | `None` | (default) Identity — zero overhead. |
162
+ | `box` | Elementwise clip to `[lo, hi]`. |
163
+ | `trust` | Trust-region sphere with adaptive radius. |
164
+
165
+ When a region is active, the line search navigates the **projected path**
166
+ `d_R(t) = project_R(x, x + d(t)) - x`, so descent guarantees hold on the
167
+ feasible set. Custom regions can be composed with `SequentialRegion`.
168
+
169
+ #### 4. Gradient
170
+
171
+ The raw `-∇f` signal, the path's tangent at the origin.
172
+
173
+ ### Custom components
174
+
175
+ You can pass instances instead of string shortcuts for full control:
176
+
177
+ ```python
178
+ from qqn_torch import QQN
179
+ from qqn_torch.regions import TrustRegion
180
+ from qqn_torch.oracles import SecantOracle
181
+
182
+ optimizer = QQN(
183
+ params,
184
+ oracle=SecantOracle(alpha_init=0.5),
185
+ region=TrustRegion(radius=2.0, max_radius=1e3),
186
+ line_search="strong_wolfe",
187
+ line_search_options={"c1": 1e-4, "c2": 0.9},
188
+ )
189
+ ```
190
+
191
+ ## How it works (per inner iteration)
192
+
193
+ ```
194
+ g = flat_grad(closure) # autograd on the closure
195
+ qn_dir = oracle.direction(g, state) # the t=1 endpoint, -H g
196
+ grad_dir = -g # the path tangent at t=0
197
+ d(t) = t(1-t)·grad_dir + t²·qn_dir # quadratic path
198
+ t* = line_search(...) # picks the blend AND the step
199
+ x += project(d(t*)) # apply (optionally projected) step
200
+ oracle.update(s, y) # s = Δx, y = Δg
201
+ region.update(...) # e.g. adapt trust radius
202
+ ```
203
+
204
+ ## Advantages
205
+
206
+ - **Adaptive**: automatically balances conservative vs. aggressive steps.
207
+ - **Robust**: `d'(0) = -∇f` plus line-search fallbacks ensure progress even
208
+ when the oracle is poor.
209
+ - **Efficient**: L-BFGS acceleration when curvature is reliable.
210
+ - **Modular**: gradient, oracle, search, and region are independently
211
+ swappable.
212
+
213
+ ## Limitations
214
+
215
+ - **Memory**: stores L-BFGS history (`O(m·n)`).
216
+ - **Overhead**: walking the curved path adds modest per-iteration cost.
217
+ - **Tuning**: sensitive to history size, line-search constants, region radii.
218
+ - **Line-search sensitivity**: a poor line search undermines convergence and
219
+ the quality of the curvature updates.
220
+
221
+ ## Theoretical guarantees
222
+
223
+ Under standard assumptions (smooth objective, bounded gradients):
224
+
225
+ - **Global convergence** — anchored by the steepest-descent tangent.
226
+ - **Superlinear convergence** — inherited from L-BFGS when `t → 1` near the
227
+ optimum.
228
+ - **Descent property** — every accepted step decreases `f`, enforced by the
229
+ line search's sufficient-decrease test.
230
+
231
+ All guarantees are contingent on the line search satisfying sufficient
232
+ decrease. When a region is active, they hold on the projected path `d_R(t)`.
233
+
234
+ ## Documentation
235
+
236
+ - [`algorithm.md`](algorithm.md) — comprehensive algorithm reference.
237
+
238
+ ## Acknowledgements
239
+
240
+ The strong-Wolfe line search is adapted from PyTorch's `_strong_wolfe`
241
+ helper (`torch/optim/lbfgs.py`, BSD-licensed). See
242
+ `qqn_torch/_vendor/strong_wolfe.py` for attribution.
243
+
244
+ ## License
245
+
246
+ See the `LICENSE` file. Vendored code retains its original PyTorch BSD license.