piazzi 0.1.0a1__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 (45) hide show
  1. piazzi-0.1.0a1/.gitignore +16 -0
  2. piazzi-0.1.0a1/LICENSE +201 -0
  3. piazzi-0.1.0a1/PKG-INFO +102 -0
  4. piazzi-0.1.0a1/README.md +74 -0
  5. piazzi-0.1.0a1/conftest.py +24 -0
  6. piazzi-0.1.0a1/pyproject.toml +125 -0
  7. piazzi-0.1.0a1/src/piazzi/__init__.py +56 -0
  8. piazzi-0.1.0a1/src/piazzi/_common.py +188 -0
  9. piazzi-0.1.0a1/src/piazzi/_finite_difference.py +238 -0
  10. piazzi-0.1.0a1/src/piazzi/_gauss_newton.py +446 -0
  11. piazzi-0.1.0a1/src/piazzi/_lbfgs.py +350 -0
  12. piazzi-0.1.0a1/src/piazzi/_least_squares.py +204 -0
  13. piazzi-0.1.0a1/src/piazzi/_linear_solvers.py +149 -0
  14. piazzi-0.1.0a1/src/piazzi/_losses.py +122 -0
  15. piazzi-0.1.0a1/src/piazzi/_manifolds.py +615 -0
  16. piazzi-0.1.0a1/src/piazzi/_minimize.py +126 -0
  17. piazzi-0.1.0a1/src/piazzi/_progress.py +97 -0
  18. piazzi-0.1.0a1/src/piazzi/_torch_optim.py +129 -0
  19. piazzi-0.1.0a1/src/piazzi/_version.py +24 -0
  20. piazzi-0.1.0a1/src/piazzi/py.typed +0 -0
  21. piazzi-0.1.0a1/src/piazzi/pypose/__init__.py +37 -0
  22. piazzi-0.1.0a1/src/piazzi/pypose/_least_squares.py +328 -0
  23. piazzi-0.1.0a1/src/piazzi/scipy/__init__.py +26 -0
  24. piazzi-0.1.0a1/src/piazzi/scipy/_convert.py +117 -0
  25. piazzi-0.1.0a1/src/piazzi/scipy/_least_squares.py +184 -0
  26. piazzi-0.1.0a1/src/piazzi/scipy/_minimize.py +247 -0
  27. piazzi-0.1.0a1/src/piazzi/torchmin/__init__.py +50 -0
  28. piazzi-0.1.0a1/src/piazzi/torchmin/_minimize.py +242 -0
  29. piazzi-0.1.0a1/tests/problems.py +135 -0
  30. piazzi-0.1.0a1/tests/test_ceres_manifolds.py +485 -0
  31. piazzi-0.1.0a1/tests/test_devices.py +223 -0
  32. piazzi-0.1.0a1/tests/test_finite_difference.py +239 -0
  33. piazzi-0.1.0a1/tests/test_iteration_counts.py +102 -0
  34. piazzi-0.1.0a1/tests/test_lie_derivatives.py +128 -0
  35. piazzi-0.1.0a1/tests/test_linear_solvers.py +165 -0
  36. piazzi-0.1.0a1/tests/test_losses.py +228 -0
  37. piazzi-0.1.0a1/tests/test_manifolds.py +385 -0
  38. piazzi-0.1.0a1/tests/test_ported_scipy.py +301 -0
  39. piazzi-0.1.0a1/tests/test_progress.py +181 -0
  40. piazzi-0.1.0a1/tests/test_pypose_backend.py +298 -0
  41. piazzi-0.1.0a1/tests/test_scipy_backend.py +217 -0
  42. piazzi-0.1.0a1/tests/test_scipy_minimize.py +238 -0
  43. piazzi-0.1.0a1/tests/test_solvers.py +469 -0
  44. piazzi-0.1.0a1/tests/test_torch_optim.py +176 -0
  45. piazzi-0.1.0a1/tests/test_torchmin_backend.py +237 -0
@@ -0,0 +1,16 @@
1
+ .venv/
2
+ __pycache__/
3
+ *.egg-info/
4
+ build/
5
+ dist/
6
+ .pytest_cache/
7
+ .ruff_cache/
8
+ .ipynb_checkpoints/
9
+ .DS_Store
10
+ src/piazzi/_version.py
11
+ # Data downloaded by the notebooks
12
+ notebooks/*.bz2
13
+ # Copied from notebooks/ by docs/conf.py at build time
14
+ docs/examples/*.ipynb
15
+ docs/_build/
16
+ docs/jupyter_execute/
piazzi-0.1.0a1/LICENSE ADDED
@@ -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,102 @@
1
+ Metadata-Version: 2.5
2
+ Name: piazzi
3
+ Version: 0.1.0a1
4
+ Summary: Deterministic PyTorch solvers for nonlinear fitting problems: L-BFGS with Armijo line search, Gauss-Newton, Levenberg-Marquardt.
5
+ Project-URL: Documentation, https://cai4cai.github.io/torch-piazzi/
6
+ Project-URL: Changelog, https://cai4cai.github.io/torch-piazzi/changelog.html
7
+ Project-URL: Repository, https://github.com/cai4cai/torch-piazzi
8
+ Project-URL: Issues, https://github.com/cai4cai/torch-piazzi/issues
9
+ Author: CAI4CAI
10
+ License-Expression: Apache-2.0
11
+ License-File: LICENSE
12
+ Keywords: autodiff,bundle-adjustment,gauss-newton,image-registration,lbfgs,levenberg-marquardt,lie-groups,nonlinear-least-squares,optimization,pose-estimation,python,pytorch,quasi-newton,scipy,se3
13
+ Classifier: Development Status :: 3 - Alpha
14
+ Classifier: Intended Audience :: Science/Research
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Topic :: Scientific/Engineering :: Mathematics
17
+ Requires-Python: >=3.11
18
+ Requires-Dist: torch>=2.15
19
+ Provides-Extra: pypose
20
+ Requires-Dist: pypose>=0.9; extra == 'pypose'
21
+ Provides-Extra: scipy
22
+ Requires-Dist: scipy>=1.11; extra == 'scipy'
23
+ Provides-Extra: sparse
24
+ Requires-Dist: torchsparsegradutils>=0.2.7; extra == 'sparse'
25
+ Provides-Extra: torchmin
26
+ Requires-Dist: pytorch-minimize>=0.1.0; extra == 'torchmin'
27
+ Description-Content-Type: text/markdown
28
+
29
+ # torch-piazzi
30
+
31
+ [![tests](https://github.com/cai4cai/torch-piazzi/actions/workflows/tests.yml/badge.svg)](https://github.com/cai4cai/torch-piazzi/actions/workflows/tests.yml)
32
+ [![docs](https://github.com/cai4cai/torch-piazzi/actions/workflows/docs.yml/badge.svg)](https://cai4cai.github.io/torch-piazzi/)
33
+
34
+ `piazzi` provides deterministic PyTorch solvers for nonlinear fitting problems, such as pose or camera registration, curve fitting and bundle adjustment. It is built for problems where every objective evaluation is expensive (rendering, for example) and you want each evaluation to count. `torch.optim` targets stochastic training instead; `piazzi` targets full-batch, curvature-aware fitting.
35
+
36
+ > Named after Giuseppe Piazzi, who discovered the dwarf planet Ceres in 1801. Gauss recovered Ceres from Piazzi's observations by inventing least squares.
37
+
38
+ **Status:** pre-alpha, and the API may change. The package is not on PyPI yet.
39
+
40
+ **Documentation:** <https://cai4cai.github.io/torch-piazzi/>
41
+
42
+ ## Installation
43
+
44
+ ```bash
45
+ pip install "piazzi @ git+https://github.com/cai4cai/torch-piazzi"
46
+ pip install "piazzi[scipy,pypose,torchmin,sparse] @ git+https://github.com/cai4cai/torch-piazzi" # optional backends and sparse CG
47
+ ```
48
+
49
+ ## Example
50
+
51
+ ```python
52
+ import torch
53
+ import piazzi
54
+
55
+ t = torch.linspace(0, 4, 40)
56
+ y = 2.5 * torch.exp(-1.3 * t)
57
+
58
+ y[::10] += 2.0 # outliers
59
+
60
+ res = piazzi.least_squares(
61
+ lambda th: th[0] * torch.exp(-th[1] * t) - y, torch.tensor([1.0, 0.5]), loss="cauchy", f_scale=0.1
62
+ )
63
+ print(res.x, res.message) # tensor([2.5026, 1.3005]) step below step_tol; loss="linear" gives [3.1053, 1.2741]
64
+ ```
65
+
66
+ ## Features
67
+
68
+ - **[Solvers](https://cai4cai.github.io/torch-piazzi/guide/solvers.html):** L-BFGS with an Armijo line search that counts every evaluation, Gauss-Newton and Levenberg-Marquardt, and SciPy-style `least_squares` and `minimize`.
69
+ - **[Robust losses](https://cai4cai.github.io/torch-piazzi/guide/least-squares.html):** SciPy's losses or any PyTorch `rho(z)`, with IRLS or SciPy curvature.
70
+ - **[Black-box objectives](https://cai4cai.github.io/torch-piazzi/guide/least-squares.html):** `finite_difference` makes a function that autograd cannot see through usable by every solver.
71
+ - **[Manifolds](https://cai4cai.github.io/torch-piazzi/guide/manifolds.html):** keep rotations and poses on SO(3) and SE(3), as Ceres's `Manifold` does, with batched exp/log maps.
72
+ - **[Large sparse problems](https://cai4cai.github.io/torch-piazzi/guide/sparse.html):** sparse normal equations, conjugate gradients and custom linear solvers such as a Schur complement.
73
+ - **[Devices and precision](https://cai4cai.github.io/torch-piazzi/guide/devices-precision.html):** run the objective in float32 on CUDA or Apple MPS, and solve the normal equations in float64 on the CPU or on the GPU.
74
+ - **[`torch.optim` optimisers](https://cai4cai.github.io/torch-piazzi/guide/torch-optim.html)** and **[SciPy, PyPose and pytorch-minimize backends](https://cai4cai.github.io/torch-piazzi/guide/backends.html)** behind the same interface, to compare methods on the same problem.
75
+
76
+ ## Notebooks
77
+
78
+ The notebooks in [`notebooks/`](https://github.com/cai4cai/torch-piazzi/tree/main/notebooks) run in Google Colab, or locally after `uv sync --group notebooks`. They are also rendered in the [documentation](https://cai4cai.github.io/torch-piazzi/examples/index.html).
79
+
80
+ | Notebook | |
81
+ |---|---|
82
+ | [Robust nonlinear regression](https://github.com/cai4cai/torch-piazzi/blob/main/notebooks/robust_regression.ipynb): robust losses, checked against `scipy.optimize.least_squares` | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/cai4cai/torch-piazzi/blob/main/notebooks/robust_regression.ipynb) |
83
+ | [The Rosenbrock function](https://github.com/cai4cai/torch-piazzi/blob/main/notebooks/rosenbrock.ipynb): L-BFGS variants versus Gauss-Newton and Levenberg-Marquardt, and SciPy's methods | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/cai4cai/torch-piazzi/blob/main/notebooks/rosenbrock.ipynb) |
84
+ | [Bundle adjustment](https://github.com/cai4cai/torch-piazzi/blob/main/notebooks/bundle_adjustment.ipynb): 49 cameras on SE(3), sparse normal equations, CG and Schur solvers, on the CPU and a GPU | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/cai4cai/torch-piazzi/blob/main/notebooks/bundle_adjustment.ipynb) |
85
+
86
+ ## Related work
87
+
88
+ Ceres Solver, Theseus, PyPose, `torch.optim.LBFGS`, nlesc-dirac/pytorch, pytorch-minimize and `scipy.optimize` solve related problems; the [related work page](https://cai4cai.github.io/torch-piazzi/related-work.html) compares them.
89
+
90
+ ## Development
91
+
92
+ ```bash
93
+ uv sync --group dev
94
+ uv run pytest
95
+ uv run ruff check .
96
+ ```
97
+
98
+ See the [contributing page](https://cai4cai.github.io/torch-piazzi/contributing.html) and [AGENTS.md](https://github.com/cai4cai/torch-piazzi/blob/main/AGENTS.md) for the full checks and conventions. Planned work is tracked in the [issues](https://github.com/cai4cai/torch-piazzi/issues).
99
+
100
+ ## Licence
101
+
102
+ Apache-2.0
@@ -0,0 +1,74 @@
1
+ # torch-piazzi
2
+
3
+ [![tests](https://github.com/cai4cai/torch-piazzi/actions/workflows/tests.yml/badge.svg)](https://github.com/cai4cai/torch-piazzi/actions/workflows/tests.yml)
4
+ [![docs](https://github.com/cai4cai/torch-piazzi/actions/workflows/docs.yml/badge.svg)](https://cai4cai.github.io/torch-piazzi/)
5
+
6
+ `piazzi` provides deterministic PyTorch solvers for nonlinear fitting problems, such as pose or camera registration, curve fitting and bundle adjustment. It is built for problems where every objective evaluation is expensive (rendering, for example) and you want each evaluation to count. `torch.optim` targets stochastic training instead; `piazzi` targets full-batch, curvature-aware fitting.
7
+
8
+ > Named after Giuseppe Piazzi, who discovered the dwarf planet Ceres in 1801. Gauss recovered Ceres from Piazzi's observations by inventing least squares.
9
+
10
+ **Status:** pre-alpha, and the API may change. The package is not on PyPI yet.
11
+
12
+ **Documentation:** <https://cai4cai.github.io/torch-piazzi/>
13
+
14
+ ## Installation
15
+
16
+ ```bash
17
+ pip install "piazzi @ git+https://github.com/cai4cai/torch-piazzi"
18
+ pip install "piazzi[scipy,pypose,torchmin,sparse] @ git+https://github.com/cai4cai/torch-piazzi" # optional backends and sparse CG
19
+ ```
20
+
21
+ ## Example
22
+
23
+ ```python
24
+ import torch
25
+ import piazzi
26
+
27
+ t = torch.linspace(0, 4, 40)
28
+ y = 2.5 * torch.exp(-1.3 * t)
29
+
30
+ y[::10] += 2.0 # outliers
31
+
32
+ res = piazzi.least_squares(
33
+ lambda th: th[0] * torch.exp(-th[1] * t) - y, torch.tensor([1.0, 0.5]), loss="cauchy", f_scale=0.1
34
+ )
35
+ print(res.x, res.message) # tensor([2.5026, 1.3005]) step below step_tol; loss="linear" gives [3.1053, 1.2741]
36
+ ```
37
+
38
+ ## Features
39
+
40
+ - **[Solvers](https://cai4cai.github.io/torch-piazzi/guide/solvers.html):** L-BFGS with an Armijo line search that counts every evaluation, Gauss-Newton and Levenberg-Marquardt, and SciPy-style `least_squares` and `minimize`.
41
+ - **[Robust losses](https://cai4cai.github.io/torch-piazzi/guide/least-squares.html):** SciPy's losses or any PyTorch `rho(z)`, with IRLS or SciPy curvature.
42
+ - **[Black-box objectives](https://cai4cai.github.io/torch-piazzi/guide/least-squares.html):** `finite_difference` makes a function that autograd cannot see through usable by every solver.
43
+ - **[Manifolds](https://cai4cai.github.io/torch-piazzi/guide/manifolds.html):** keep rotations and poses on SO(3) and SE(3), as Ceres's `Manifold` does, with batched exp/log maps.
44
+ - **[Large sparse problems](https://cai4cai.github.io/torch-piazzi/guide/sparse.html):** sparse normal equations, conjugate gradients and custom linear solvers such as a Schur complement.
45
+ - **[Devices and precision](https://cai4cai.github.io/torch-piazzi/guide/devices-precision.html):** run the objective in float32 on CUDA or Apple MPS, and solve the normal equations in float64 on the CPU or on the GPU.
46
+ - **[`torch.optim` optimisers](https://cai4cai.github.io/torch-piazzi/guide/torch-optim.html)** and **[SciPy, PyPose and pytorch-minimize backends](https://cai4cai.github.io/torch-piazzi/guide/backends.html)** behind the same interface, to compare methods on the same problem.
47
+
48
+ ## Notebooks
49
+
50
+ The notebooks in [`notebooks/`](https://github.com/cai4cai/torch-piazzi/tree/main/notebooks) run in Google Colab, or locally after `uv sync --group notebooks`. They are also rendered in the [documentation](https://cai4cai.github.io/torch-piazzi/examples/index.html).
51
+
52
+ | Notebook | |
53
+ |---|---|
54
+ | [Robust nonlinear regression](https://github.com/cai4cai/torch-piazzi/blob/main/notebooks/robust_regression.ipynb): robust losses, checked against `scipy.optimize.least_squares` | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/cai4cai/torch-piazzi/blob/main/notebooks/robust_regression.ipynb) |
55
+ | [The Rosenbrock function](https://github.com/cai4cai/torch-piazzi/blob/main/notebooks/rosenbrock.ipynb): L-BFGS variants versus Gauss-Newton and Levenberg-Marquardt, and SciPy's methods | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/cai4cai/torch-piazzi/blob/main/notebooks/rosenbrock.ipynb) |
56
+ | [Bundle adjustment](https://github.com/cai4cai/torch-piazzi/blob/main/notebooks/bundle_adjustment.ipynb): 49 cameras on SE(3), sparse normal equations, CG and Schur solvers, on the CPU and a GPU | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/cai4cai/torch-piazzi/blob/main/notebooks/bundle_adjustment.ipynb) |
57
+
58
+ ## Related work
59
+
60
+ Ceres Solver, Theseus, PyPose, `torch.optim.LBFGS`, nlesc-dirac/pytorch, pytorch-minimize and `scipy.optimize` solve related problems; the [related work page](https://cai4cai.github.io/torch-piazzi/related-work.html) compares them.
61
+
62
+ ## Development
63
+
64
+ ```bash
65
+ uv sync --group dev
66
+ uv run pytest
67
+ uv run ruff check .
68
+ ```
69
+
70
+ See the [contributing page](https://cai4cai.github.io/torch-piazzi/contributing.html) and [AGENTS.md](https://github.com/cai4cai/torch-piazzi/blob/main/AGENTS.md) for the full checks and conventions. Planned work is tracked in the [issues](https://github.com/cai4cai/torch-piazzi/issues).
71
+
72
+ ## Licence
73
+
74
+ Apache-2.0
@@ -0,0 +1,24 @@
1
+ """Pytest setup shared by the test suite and the docstring examples (``--doctest-modules`` on ``src/``)."""
2
+
3
+ import importlib.util
4
+
5
+ import pytest
6
+ import torch
7
+
8
+ import piazzi
9
+
10
+ # The SciPy wrappers import SciPy at module level: skip their docstrings when the extra is missing.
11
+ collect_ignore_glob = [] if importlib.util.find_spec("scipy") else ["src/piazzi/scipy/*"]
12
+ # Likewise for pytorch-minimize.
13
+ if not importlib.util.find_spec("torchmin"):
14
+ collect_ignore_glob.append("src/piazzi/torchmin/*")
15
+ # Likewise for PyPose (the modules raise ImportError without it).
16
+ if not importlib.util.find_spec("pypose"):
17
+ collect_ignore_glob.append("src/piazzi/pypose/*")
18
+
19
+
20
+ @pytest.fixture(autouse=True)
21
+ def _doctest_namespace(doctest_namespace):
22
+ """Docstring examples use ``torch`` and ``piazzi`` without importing them, as PyTorch's do."""
23
+ doctest_namespace["torch"] = torch
24
+ doctest_namespace["piazzi"] = piazzi
@@ -0,0 +1,125 @@
1
+ [project]
2
+ name = "piazzi"
3
+ dynamic = ["version"]
4
+ description = "Deterministic PyTorch solvers for nonlinear fitting problems: L-BFGS with Armijo line search, Gauss-Newton, Levenberg-Marquardt."
5
+ readme = "README.md"
6
+ license = "Apache-2.0"
7
+ license-files = ["LICENSE"]
8
+ authors = [{ name = "CAI4CAI" }]
9
+ requires-python = ">=3.11"
10
+ dependencies = ["torch>=2.15"]
11
+ # Kept in sync with the GitHub repository topics.
12
+ keywords = [
13
+ "autodiff",
14
+ "bundle-adjustment",
15
+ "gauss-newton",
16
+ "image-registration",
17
+ "lbfgs",
18
+ "levenberg-marquardt",
19
+ "lie-groups",
20
+ "nonlinear-least-squares",
21
+ "optimization",
22
+ "pose-estimation",
23
+ "python",
24
+ "pytorch",
25
+ "quasi-newton",
26
+ "scipy",
27
+ "se3",
28
+ ]
29
+ classifiers = [
30
+ "Development Status :: 3 - Alpha",
31
+ "Intended Audience :: Science/Research",
32
+ "Programming Language :: Python :: 3",
33
+ "Topic :: Scientific/Engineering :: Mathematics",
34
+ ]
35
+
36
+ [project.optional-dependencies]
37
+ # Third-party backends, each importable as a piazzi subpackage (e.g. piazzi.scipy).
38
+ scipy = ["scipy>=1.11"]
39
+ torchmin = ["pytorch-minimize>=0.1.0"]
40
+ # Gauss-Newton and Levenberg-Marquardt of PyPose (piazzi.pypose).
41
+ pypose = ["pypose>=0.9"]
42
+ # Iterative sparse solvers (piazzi.cg_solver, the default for a sparse Hessian).
43
+ sparse = ["torchsparsegradutils>=0.2.7"]
44
+
45
+ [project.urls]
46
+ Documentation = "https://cai4cai.github.io/torch-piazzi/"
47
+ Changelog = "https://cai4cai.github.io/torch-piazzi/changelog.html"
48
+ Repository = "https://github.com/cai4cai/torch-piazzi"
49
+ Issues = "https://github.com/cai4cai/torch-piazzi/issues"
50
+
51
+ [dependency-groups]
52
+ dev = ["pyright[nodejs]==1.1.414", "pypose>=0.9", "pytest>=8", "pytorch-minimize>=0.1.0", "ruff==0.17.6", "scipy>=1.11", "torchsparsegradutils>=0.2.7"]
53
+ notebooks = [
54
+ "jupyterlab>=4",
55
+ "matplotlib>=3.7",
56
+ "nbclient>=0.10",
57
+ "numpy>=1.24",
58
+ "pytorch-minimize>=0.1.0",
59
+ "pypose>=0.9",
60
+ "scipy>=1.11",
61
+ "torchsparsegradutils>=0.2.7",
62
+ ]
63
+ docs = [
64
+ "furo>=2024.8.6",
65
+ "myst-nb>=1.1",
66
+ "myst-parser>=3",
67
+ "pytorch-minimize>=0.1.0",
68
+ "pypose>=0.9",
69
+ "scipy>=1.11",
70
+ "sphinx>=7.4",
71
+ "sphinx-copybutton>=0.5.2",
72
+ "sphinx-design>=0.6",
73
+ "torchsparsegradutils>=0.2.7",
74
+ ]
75
+
76
+ [build-system]
77
+ requires = ["hatchling>=1.26", "hatch-vcs"]
78
+ build-backend = "hatchling.build"
79
+
80
+ [tool.hatch.version]
81
+ source = "vcs"
82
+
83
+ [tool.hatch.build.hooks.vcs]
84
+ version-file = "src/piazzi/_version.py"
85
+
86
+ [tool.hatch.build.targets.wheel]
87
+ packages = ["src/piazzi"]
88
+
89
+ [tool.hatch.build.targets.sdist]
90
+ only-include = [
91
+ "LICENSE",
92
+ "README.md",
93
+ "conftest.py",
94
+ "pyproject.toml",
95
+ "src",
96
+ "tests",
97
+ ]
98
+
99
+ [tool.pytest.ini_options]
100
+ # src/ is collected for the examples in docstrings (doctests).
101
+ testpaths = ["tests", "src"]
102
+ addopts = "--doctest-modules"
103
+ filterwarnings = ["error"]
104
+
105
+ [tool.ruff]
106
+ line-length = 120
107
+ target-version = "py311"
108
+
109
+ [tool.ruff.lint]
110
+ select = ["E", "F", "W", "I", "B", "UP", "D", "RUF", "SIM", "C4", "PT", "NPY"]
111
+ # PT018: splitting `assert a and b` in the tests would be churn for little gain.
112
+ ignore = ["E702", "PT018"]
113
+
114
+ [tool.ruff.lint.pydocstyle]
115
+ # PyTorch-style docstrings: Google sections with types, ``Default: ...`` (see AGENTS.md).
116
+ convention = "google"
117
+
118
+ [tool.ruff.lint.per-file-ignores]
119
+ # Docstring rules apply to the library; tests, scripts, notebooks and the docs config are exempt.
120
+ "!src/**" = ["D"]
121
+
122
+ [tool.pyright]
123
+ # Type-check the library loosely (not strict mode, not the tests): see AGENTS.md.
124
+ include = ["src"]
125
+ typeCheckingMode = "standard"
@@ -0,0 +1,56 @@
1
+ """piazzi: deterministic PyTorch solvers for nonlinear fitting problems.
2
+
3
+ Named after Giuseppe Piazzi, who discovered Ceres in 1801; Gauss recovered it from
4
+ Piazzi's observations with the method of least squares.
5
+ """
6
+
7
+ from ._common import IterationInfo, OptimizeResult
8
+ from ._finite_difference import finite_difference
9
+ from ._gauss_newton import gauss_newton, levenberg_marquardt
10
+ from ._lbfgs import lbfgs_armijo
11
+ from ._least_squares import least_squares, least_squares_system
12
+ from ._linear_solvers import LinearSolver, cg_solver, dense_solver
13
+ from ._losses import LOSSES, robust_cost
14
+ from ._manifolds import SE3, SO3, Euclidean, Manifold, Product, Subset, se3_exp, se3_log, so3_exp, so3_hat, so3_log
15
+ from ._minimize import MINIMIZE_METHODS, minimize
16
+ from ._progress import print_progress
17
+
18
+ try:
19
+ from ._version import __version__
20
+ except ImportError:
21
+ from importlib.metadata import PackageNotFoundError, version
22
+
23
+ try:
24
+ __version__ = version("piazzi")
25
+ except PackageNotFoundError:
26
+ __version__ = "0.0.0.dev0+unknown"
27
+
28
+ __all__ = [
29
+ "LOSSES",
30
+ "MINIMIZE_METHODS",
31
+ "SE3",
32
+ "SO3",
33
+ "Euclidean",
34
+ "IterationInfo",
35
+ "LinearSolver",
36
+ "Manifold",
37
+ "OptimizeResult",
38
+ "Product",
39
+ "Subset",
40
+ "cg_solver",
41
+ "dense_solver",
42
+ "finite_difference",
43
+ "gauss_newton",
44
+ "lbfgs_armijo",
45
+ "least_squares",
46
+ "least_squares_system",
47
+ "levenberg_marquardt",
48
+ "minimize",
49
+ "print_progress",
50
+ "robust_cost",
51
+ "se3_exp",
52
+ "se3_log",
53
+ "so3_exp",
54
+ "so3_hat",
55
+ "so3_log",
56
+ ]