torchzero 0.3.13__py3-none-any.whl → 0.3.15__py3-none-any.whl
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.
- tests/test_opts.py +4 -10
- torchzero/core/__init__.py +4 -1
- torchzero/core/chain.py +50 -0
- torchzero/core/functional.py +37 -0
- torchzero/core/modular.py +237 -0
- torchzero/core/module.py +12 -599
- torchzero/core/reformulation.py +3 -1
- torchzero/core/transform.py +7 -5
- torchzero/core/var.py +376 -0
- torchzero/modules/__init__.py +0 -1
- torchzero/modules/adaptive/adahessian.py +2 -2
- torchzero/modules/adaptive/esgd.py +2 -2
- torchzero/modules/adaptive/matrix_momentum.py +1 -1
- torchzero/modules/adaptive/sophia_h.py +2 -2
- torchzero/modules/conjugate_gradient/cg.py +16 -16
- torchzero/modules/experimental/__init__.py +1 -0
- torchzero/modules/experimental/newtonnewton.py +5 -5
- torchzero/modules/experimental/spsa1.py +93 -0
- torchzero/modules/functional.py +7 -0
- torchzero/modules/grad_approximation/__init__.py +1 -1
- torchzero/modules/grad_approximation/forward_gradient.py +2 -5
- torchzero/modules/grad_approximation/rfdm.py +27 -110
- torchzero/modules/line_search/__init__.py +1 -1
- torchzero/modules/line_search/_polyinterp.py +3 -1
- torchzero/modules/line_search/adaptive.py +3 -3
- torchzero/modules/line_search/backtracking.py +1 -1
- torchzero/modules/line_search/interpolation.py +160 -0
- torchzero/modules/line_search/line_search.py +11 -20
- torchzero/modules/line_search/scipy.py +15 -3
- torchzero/modules/line_search/strong_wolfe.py +3 -5
- torchzero/modules/misc/misc.py +2 -2
- torchzero/modules/misc/multistep.py +13 -13
- torchzero/modules/quasi_newton/__init__.py +2 -0
- torchzero/modules/quasi_newton/quasi_newton.py +15 -6
- torchzero/modules/quasi_newton/sg2.py +292 -0
- torchzero/modules/restarts/restars.py +5 -4
- torchzero/modules/second_order/__init__.py +6 -3
- torchzero/modules/second_order/ifn.py +89 -0
- torchzero/modules/second_order/inm.py +105 -0
- torchzero/modules/second_order/newton.py +103 -193
- torchzero/modules/second_order/newton_cg.py +86 -110
- torchzero/modules/second_order/nystrom.py +1 -1
- torchzero/modules/second_order/rsn.py +227 -0
- torchzero/modules/trust_region/levenberg_marquardt.py +2 -2
- torchzero/modules/trust_region/trust_cg.py +6 -4
- torchzero/modules/wrappers/optim_wrapper.py +49 -42
- torchzero/modules/zeroth_order/__init__.py +1 -1
- torchzero/modules/zeroth_order/cd.py +1 -238
- torchzero/utils/derivatives.py +19 -19
- torchzero/utils/linalg/linear_operator.py +50 -2
- torchzero/utils/optimizer.py +2 -2
- torchzero/utils/python_tools.py +1 -0
- {torchzero-0.3.13.dist-info → torchzero-0.3.15.dist-info}/METADATA +1 -1
- {torchzero-0.3.13.dist-info → torchzero-0.3.15.dist-info}/RECORD +57 -48
- torchzero/modules/higher_order/__init__.py +0 -1
- /torchzero/modules/{higher_order → experimental}/higher_order_newton.py +0 -0
- {torchzero-0.3.13.dist-info → torchzero-0.3.15.dist-info}/WHEEL +0 -0
- {torchzero-0.3.13.dist-info → torchzero-0.3.15.dist-info}/top_level.txt +0 -0
torchzero/core/transform.py
CHANGED
|
@@ -5,7 +5,9 @@ from typing import Any, Literal, final
|
|
|
5
5
|
import torch
|
|
6
6
|
|
|
7
7
|
from ..utils import TensorList, set_storage_, vec_to_tensors
|
|
8
|
-
from .
|
|
8
|
+
from .chain import Chain
|
|
9
|
+
from .module import Chainable, Module
|
|
10
|
+
from .var import Var
|
|
9
11
|
|
|
10
12
|
Target = Literal['grad', 'update', 'closure', 'params_direct', 'params_difference', 'update_difference']
|
|
11
13
|
|
|
@@ -86,7 +88,7 @@ class Transform(Module, ABC):
|
|
|
86
88
|
|
|
87
89
|
@final
|
|
88
90
|
@torch.no_grad
|
|
89
|
-
def
|
|
91
|
+
def update_transform(
|
|
90
92
|
self,
|
|
91
93
|
tensors: list[torch.Tensor],
|
|
92
94
|
params: list[torch.Tensor],
|
|
@@ -123,7 +125,7 @@ class Transform(Module, ABC):
|
|
|
123
125
|
|
|
124
126
|
@final
|
|
125
127
|
@torch.no_grad
|
|
126
|
-
def
|
|
128
|
+
def apply_transform(
|
|
127
129
|
self,
|
|
128
130
|
tensors: list[torch.Tensor],
|
|
129
131
|
params: list[torch.Tensor],
|
|
@@ -190,7 +192,7 @@ class Transform(Module, ABC):
|
|
|
190
192
|
):
|
|
191
193
|
"""`params` will be used as keys and need to always point to same tensor objects.`"""
|
|
192
194
|
states, settings = self._get_keyed_states_settings(params)
|
|
193
|
-
self.
|
|
195
|
+
self.update_transform(tensors=tensors, params=params, grads=grads, loss=loss, states=states, settings=settings)
|
|
194
196
|
|
|
195
197
|
|
|
196
198
|
@final
|
|
@@ -204,7 +206,7 @@ class Transform(Module, ABC):
|
|
|
204
206
|
):
|
|
205
207
|
"""`params` will be used as keys and need to always point to same tensor objects.`"""
|
|
206
208
|
states, settings = self._get_keyed_states_settings(params)
|
|
207
|
-
return self.
|
|
209
|
+
return self.apply_transform(tensors=tensors, params=params, grads=grads, loss=loss, states=states, settings=settings)
|
|
208
210
|
|
|
209
211
|
|
|
210
212
|
def pre_step(self, var: Var) -> None:
|
torchzero/core/var.py
ADDED
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
|
|
2
|
+
import warnings
|
|
3
|
+
from abc import ABC, abstractmethod
|
|
4
|
+
from collections import ChainMap, defaultdict
|
|
5
|
+
from collections.abc import Callable, Iterable, MutableMapping, Sequence
|
|
6
|
+
from operator import itemgetter
|
|
7
|
+
from typing import Any, final, overload, Literal, cast, TYPE_CHECKING
|
|
8
|
+
|
|
9
|
+
import torch
|
|
10
|
+
|
|
11
|
+
from ..utils import (
|
|
12
|
+
Init,
|
|
13
|
+
ListLike,
|
|
14
|
+
Params,
|
|
15
|
+
_make_param_groups,
|
|
16
|
+
get_state_vals,
|
|
17
|
+
vec_to_tensors
|
|
18
|
+
)
|
|
19
|
+
from ..utils.derivatives import hvp, hvp_fd_central, hvp_fd_forward, flatten_jacobian
|
|
20
|
+
from ..utils.python_tools import flatten
|
|
21
|
+
from ..utils.linalg.linear_operator import LinearOperator
|
|
22
|
+
|
|
23
|
+
if TYPE_CHECKING:
|
|
24
|
+
from .modular import Modular
|
|
25
|
+
|
|
26
|
+
def _closure_backward(closure, params, retain_graph, create_graph):
|
|
27
|
+
with torch.enable_grad():
|
|
28
|
+
if not (retain_graph or create_graph):
|
|
29
|
+
return closure()
|
|
30
|
+
|
|
31
|
+
for p in params: p.grad = None
|
|
32
|
+
loss = closure(False)
|
|
33
|
+
grad = torch.autograd.grad(loss, params, retain_graph=retain_graph, create_graph=create_graph)
|
|
34
|
+
for p,g in zip(params,grad): p.grad = g
|
|
35
|
+
return loss
|
|
36
|
+
|
|
37
|
+
# region Vars
|
|
38
|
+
# ----------------------------------- var ----------------------------------- #
|
|
39
|
+
class Var:
|
|
40
|
+
"""
|
|
41
|
+
Holds parameters, gradient, update, objective function (closure) if supplied, loss, and some other info.
|
|
42
|
+
Modules take in a ``Var`` object, modify and it is passed to the next module.
|
|
43
|
+
|
|
44
|
+
"""
|
|
45
|
+
def __init__(
|
|
46
|
+
self,
|
|
47
|
+
params: list[torch.Tensor],
|
|
48
|
+
closure: Callable | None,
|
|
49
|
+
model: torch.nn.Module | None,
|
|
50
|
+
current_step: int,
|
|
51
|
+
parent: "Var | None" = None,
|
|
52
|
+
modular: "Modular | None" = None,
|
|
53
|
+
loss: torch.Tensor | None = None,
|
|
54
|
+
storage: dict | None = None,
|
|
55
|
+
):
|
|
56
|
+
self.params: list[torch.Tensor] = params
|
|
57
|
+
"""List of all parameters with requires_grad = True."""
|
|
58
|
+
|
|
59
|
+
self.closure = closure
|
|
60
|
+
"""A closure that reevaluates the model and returns the loss, None if it wasn't specified"""
|
|
61
|
+
|
|
62
|
+
self.model = model
|
|
63
|
+
"""torch.nn.Module object of the model, None if it wasn't specified."""
|
|
64
|
+
|
|
65
|
+
self.current_step: int = current_step
|
|
66
|
+
"""global current step, starts at 0. This may not correspond to module current step,
|
|
67
|
+
for example a module may step every 10 global steps."""
|
|
68
|
+
|
|
69
|
+
self.parent: "Var | None" = parent
|
|
70
|
+
"""parent ``Var`` object. When ``self.get_grad()`` is called, it will also set ``parent.grad``.
|
|
71
|
+
Same with ``self.get_loss()``. This is useful when ``self.params`` are different from ``parent.params``,
|
|
72
|
+
e.g. when projecting."""
|
|
73
|
+
|
|
74
|
+
self.modular: "Modular | None" = modular
|
|
75
|
+
"""Modular optimizer object that created this ``Var``."""
|
|
76
|
+
|
|
77
|
+
self.update: list[torch.Tensor] | None = None
|
|
78
|
+
"""
|
|
79
|
+
current update. Update is assumed to be a transformed gradient, therefore it is subtracted.
|
|
80
|
+
|
|
81
|
+
If closure is None, this is initially set to cloned gradient. Otherwise this is set to None.
|
|
82
|
+
|
|
83
|
+
At the end ``var.get_update()`` is subtracted from parameters. Therefore if ``var.update`` is ``None``,
|
|
84
|
+
gradient will be used and calculated if needed.
|
|
85
|
+
"""
|
|
86
|
+
|
|
87
|
+
self.grad: list[torch.Tensor] | None = None
|
|
88
|
+
"""gradient with current parameters. If closure is not ``None``, this is set to ``None`` and can be calculated if needed."""
|
|
89
|
+
|
|
90
|
+
self.loss: torch.Tensor | Any | None = loss
|
|
91
|
+
"""loss with current parameters."""
|
|
92
|
+
|
|
93
|
+
self.loss_approx: torch.Tensor | Any | None = None
|
|
94
|
+
"""loss at a point near current point. This can be useful as some modules only calculate loss at perturbed points,
|
|
95
|
+
whereas some other modules require loss strictly at current point."""
|
|
96
|
+
|
|
97
|
+
self.post_step_hooks: list[Callable[[Modular, Var]]] = []
|
|
98
|
+
"""list of functions to be called after optimizer step.
|
|
99
|
+
|
|
100
|
+
This attribute should always be modified in-place (using ``append`` or ``extend``).
|
|
101
|
+
|
|
102
|
+
The signature is:
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
def hook(optimizer: Modular, var: Vars): ...
|
|
106
|
+
```
|
|
107
|
+
"""
|
|
108
|
+
|
|
109
|
+
self.stop: bool = False
|
|
110
|
+
"""if True, all following modules will be skipped.
|
|
111
|
+
If this module is a child, it only affects modules at the same level (in the same Chain)."""
|
|
112
|
+
|
|
113
|
+
self.skip_update: bool = False
|
|
114
|
+
"""if True, the parameters will not be updated."""
|
|
115
|
+
|
|
116
|
+
# self.storage: dict = {}
|
|
117
|
+
# """Storage for any other data, such as hessian estimates, etc."""
|
|
118
|
+
|
|
119
|
+
self.attrs: dict = {}
|
|
120
|
+
"""attributes, Modular.attrs is updated with this after each step. This attribute should always be modified in-place"""
|
|
121
|
+
|
|
122
|
+
if storage is None: storage = {}
|
|
123
|
+
self.storage: dict = storage
|
|
124
|
+
"""additional kwargs passed to closure will end up in this dict. This attribute should always be modified in-place"""
|
|
125
|
+
|
|
126
|
+
self.should_terminate: bool | None = None
|
|
127
|
+
"""termination criteria, Modular.should_terminate is set to this after each step if not None"""
|
|
128
|
+
|
|
129
|
+
def get_loss(self, backward: bool, retain_graph = None, create_graph: bool = False) -> torch.Tensor | float:
|
|
130
|
+
"""Returns the loss at current parameters, computing it if it hasn't been computed already and assigning ``var.loss``.
|
|
131
|
+
Do not call this at perturbed parameters. Backward always sets grads to None before recomputing."""
|
|
132
|
+
if self.loss is None:
|
|
133
|
+
|
|
134
|
+
if self.closure is None: raise RuntimeError("closure is None")
|
|
135
|
+
if backward:
|
|
136
|
+
with torch.enable_grad():
|
|
137
|
+
self.loss = self.loss_approx = _closure_backward(
|
|
138
|
+
closure=self.closure, params=self.params, retain_graph=retain_graph, create_graph=create_graph
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
# initializing to zeros_like is equivalent to using zero_grad with set_to_none = False.
|
|
142
|
+
# it is technically a more correct approach for when some parameters conditionally receive gradients
|
|
143
|
+
# and in this case it shouldn't be slower.
|
|
144
|
+
|
|
145
|
+
# next time closure() is called, it will set grad to None.
|
|
146
|
+
# zero_grad(set_to_none=False) shouldn't be used (I should add a warning)
|
|
147
|
+
self.grad = [p.grad if p.grad is not None else torch.zeros_like(p) for p in self.params]
|
|
148
|
+
else:
|
|
149
|
+
self.loss = self.loss_approx = self.closure(False)
|
|
150
|
+
|
|
151
|
+
# if self.loss was not None, above branch wasn't executed because loss has already been evaluated, but without backward since self.grad is None.
|
|
152
|
+
# and now it is requested to be evaluated with backward.
|
|
153
|
+
if backward and self.grad is None:
|
|
154
|
+
warnings.warn('get_loss was called with backward=False, and then with backward=True so it had to be re-evaluated, so the closure was evaluated twice where it could have been evaluated once.')
|
|
155
|
+
if self.closure is None: raise RuntimeError("closure is None")
|
|
156
|
+
|
|
157
|
+
with torch.enable_grad():
|
|
158
|
+
self.loss = self.loss_approx = _closure_backward(
|
|
159
|
+
closure=self.closure, params=self.params, retain_graph=retain_graph, create_graph=create_graph
|
|
160
|
+
)
|
|
161
|
+
self.grad = [p.grad if p.grad is not None else torch.zeros_like(p) for p in self.params]
|
|
162
|
+
|
|
163
|
+
# set parent grad
|
|
164
|
+
if self.parent is not None:
|
|
165
|
+
# the way projections/split work, they make a new closure which evaluates original
|
|
166
|
+
# closure and projects the gradient, and set it as their var.closure.
|
|
167
|
+
# then on `get_loss(backward=True)` it is called, so it also sets original parameters gradient.
|
|
168
|
+
# and we set it to parent var here.
|
|
169
|
+
if self.parent.loss is None: self.parent.loss = self.loss
|
|
170
|
+
if self.parent.grad is None and backward:
|
|
171
|
+
if all(p.grad is None for p in self.parent.params):
|
|
172
|
+
warnings.warn("Parent grad is None after backward.")
|
|
173
|
+
self.parent.grad = [p.grad if p.grad is not None else torch.zeros_like(p) for p in self.parent.params]
|
|
174
|
+
|
|
175
|
+
return self.loss # type:ignore
|
|
176
|
+
|
|
177
|
+
def get_grad(self, retain_graph: bool | None = None, create_graph: bool = False) -> list[torch.Tensor]:
|
|
178
|
+
"""Returns the gradient at initial parameters, computing it if it hasn't been computed already and assigning
|
|
179
|
+
``var.grad`` and potentially ``var.loss``. Do not call this at perturbed parameters."""
|
|
180
|
+
if self.grad is None:
|
|
181
|
+
if self.closure is None: raise RuntimeError("closure is None")
|
|
182
|
+
self.get_loss(backward=True, retain_graph=retain_graph, create_graph=create_graph) # evaluate and set self.loss and self.grad
|
|
183
|
+
|
|
184
|
+
assert self.grad is not None
|
|
185
|
+
return self.grad
|
|
186
|
+
|
|
187
|
+
def get_update(self) -> list[torch.Tensor]:
|
|
188
|
+
"""Returns the update. If update is None, it is initialized by cloning the gradients and assigning to ``var.update``.
|
|
189
|
+
Computing the gradients may assign ``var.grad`` and ``var.loss`` if they haven't been computed.
|
|
190
|
+
Do not call this at perturbed parameters."""
|
|
191
|
+
if self.update is None: self.update = [g.clone() for g in self.get_grad()]
|
|
192
|
+
return self.update
|
|
193
|
+
|
|
194
|
+
def clone(self, clone_update: bool, parent: "Var | None" = None):
|
|
195
|
+
"""Creates a shallow copy of the Vars object, update can optionally be deep-copied (via ``torch.clone``).
|
|
196
|
+
|
|
197
|
+
Setting ``parent`` is only if clone's parameters are something different,
|
|
198
|
+
while clone's closure referes to the same objective but with a "view" on parameters.
|
|
199
|
+
"""
|
|
200
|
+
copy = Var(params = self.params, closure=self.closure, model=self.model, current_step=self.current_step, parent=parent)
|
|
201
|
+
|
|
202
|
+
if clone_update and self.update is not None:
|
|
203
|
+
copy.update = [u.clone() for u in self.update]
|
|
204
|
+
else:
|
|
205
|
+
copy.update = self.update
|
|
206
|
+
|
|
207
|
+
copy.grad = self.grad
|
|
208
|
+
copy.loss = self.loss
|
|
209
|
+
copy.loss_approx = self.loss_approx
|
|
210
|
+
copy.closure = self.closure
|
|
211
|
+
copy.post_step_hooks = self.post_step_hooks
|
|
212
|
+
copy.stop = self.stop
|
|
213
|
+
copy.skip_update = self.skip_update
|
|
214
|
+
|
|
215
|
+
copy.modular = self.modular
|
|
216
|
+
copy.attrs = self.attrs
|
|
217
|
+
copy.storage = self.storage
|
|
218
|
+
copy.should_terminate = self.should_terminate
|
|
219
|
+
|
|
220
|
+
return copy
|
|
221
|
+
|
|
222
|
+
def update_attrs_from_clone_(self, var: "Var"):
|
|
223
|
+
"""Updates attributes of this `Vars` instance from a cloned instance.
|
|
224
|
+
Typically called after a child module has processed a cloned `Vars`
|
|
225
|
+
object. This propagates any newly computed loss or gradient values
|
|
226
|
+
from the child's context back to the parent `Vars` if the parent
|
|
227
|
+
didn't have them computed already.
|
|
228
|
+
|
|
229
|
+
Also, as long as ``post_step_hooks`` and ``attrs`` are modified in-place,
|
|
230
|
+
if the child updates them, the update will affect the parent too.
|
|
231
|
+
"""
|
|
232
|
+
if self.loss is None: self.loss = var.loss
|
|
233
|
+
if self.loss_approx is None: self.loss_approx = var.loss_approx
|
|
234
|
+
if self.grad is None: self.grad = var.grad
|
|
235
|
+
|
|
236
|
+
if var.should_terminate is not None: self.should_terminate = var.should_terminate
|
|
237
|
+
|
|
238
|
+
def zero_grad(self, set_to_none=True):
|
|
239
|
+
if set_to_none:
|
|
240
|
+
for p in self.params: p.grad = None
|
|
241
|
+
else:
|
|
242
|
+
grads = [p.grad for p in self.params if p.grad is not None]
|
|
243
|
+
if len(grads) != 0: torch._foreach_zero_(grads)
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
# ------------------------------ HELPER METHODS ------------------------------ #
|
|
247
|
+
@torch.no_grad
|
|
248
|
+
def hessian_vector_product(
|
|
249
|
+
self,
|
|
250
|
+
v: Sequence[torch.Tensor],
|
|
251
|
+
at_x0: bool,
|
|
252
|
+
rgrad: Sequence[torch.Tensor] | None,
|
|
253
|
+
hvp_method: Literal['autograd', 'forward', 'central'],
|
|
254
|
+
h: float,
|
|
255
|
+
normalize: bool,
|
|
256
|
+
retain_graph: bool,
|
|
257
|
+
) -> tuple[list[torch.Tensor], Sequence[torch.Tensor] | None]:
|
|
258
|
+
"""
|
|
259
|
+
Returns ``(Hvp, rgrad)``, where ``rgrad`` is gradient at current parameters,
|
|
260
|
+
possibly with ``create_graph=True``, or it may be None with ``hvp_method="central"``.
|
|
261
|
+
Gradient is set to vars automatically if ``at_x0``, you can always access it with ``vars.get_grad()``
|
|
262
|
+
|
|
263
|
+
Single sample example:
|
|
264
|
+
|
|
265
|
+
```python
|
|
266
|
+
Hvp, _ = self.hessian_vector_product(v, at_x0=True, rgrad=None, ..., retain_graph=False)
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
Multiple samples example:
|
|
270
|
+
|
|
271
|
+
```python
|
|
272
|
+
D = None
|
|
273
|
+
rgrad = None
|
|
274
|
+
for i in range(n_samples):
|
|
275
|
+
v = [torch.randn_like(p) for p in params]
|
|
276
|
+
Hvp, rgrad = self.hessian_vector_product(v, at_x0=True, rgrad=rgrad, ..., retain_graph=i < n_samples-1)
|
|
277
|
+
|
|
278
|
+
if D is None: D = Hvp
|
|
279
|
+
else: torch._foreach_add_(D, Hvp)
|
|
280
|
+
|
|
281
|
+
if n_samples > 1: torch._foreach_div_(D, n_samples)
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
Args:
|
|
285
|
+
v (Sequence[torch.Tensor]): vector in hessian-vector product
|
|
286
|
+
at_x0 (bool): whether this is being called at original or perturbed parameters.
|
|
287
|
+
var (Var): Var
|
|
288
|
+
rgrad (Sequence[torch.Tensor] | None): pass None initially, then pass what this returns.
|
|
289
|
+
hvp_method (str): hvp method.
|
|
290
|
+
h (float): finite difference step size
|
|
291
|
+
normalize (bool): whether to normalize v for finite difference
|
|
292
|
+
retain_grad (bool): retain grad
|
|
293
|
+
"""
|
|
294
|
+
# get grad
|
|
295
|
+
if rgrad is None and hvp_method in ('autograd', 'forward'):
|
|
296
|
+
if at_x0: rgrad = self.get_grad(create_graph = hvp_method=='autograd')
|
|
297
|
+
else:
|
|
298
|
+
if self.closure is None: raise RuntimeError("Closure is required to calculate HVp")
|
|
299
|
+
with torch.enable_grad():
|
|
300
|
+
loss = self.closure()
|
|
301
|
+
rgrad = torch.autograd.grad(loss, self.params, create_graph = hvp_method=='autograd')
|
|
302
|
+
|
|
303
|
+
if hvp_method == 'autograd':
|
|
304
|
+
assert rgrad is not None
|
|
305
|
+
Hvp = hvp(self.params, rgrad, v, retain_graph=retain_graph)
|
|
306
|
+
|
|
307
|
+
elif hvp_method == 'forward':
|
|
308
|
+
assert rgrad is not None
|
|
309
|
+
loss, Hvp = hvp_fd_forward(self.closure, self.params, v, h=h, g_0=rgrad, normalize=normalize)
|
|
310
|
+
|
|
311
|
+
elif hvp_method == 'central':
|
|
312
|
+
loss, Hvp = hvp_fd_central(self.closure, self.params, v, h=h, normalize=normalize)
|
|
313
|
+
|
|
314
|
+
else:
|
|
315
|
+
raise ValueError(hvp_method)
|
|
316
|
+
|
|
317
|
+
return list(Hvp), rgrad
|
|
318
|
+
|
|
319
|
+
@torch.no_grad
|
|
320
|
+
def hessian_matrix_product(
|
|
321
|
+
self,
|
|
322
|
+
M: torch.Tensor,
|
|
323
|
+
at_x0: bool,
|
|
324
|
+
rgrad: Sequence[torch.Tensor] | None,
|
|
325
|
+
hvp_method: Literal["batched", 'autograd', 'forward', 'central'],
|
|
326
|
+
h: float,
|
|
327
|
+
normalize: bool,
|
|
328
|
+
retain_graph: bool,
|
|
329
|
+
) -> tuple[torch.Tensor, Sequence[torch.Tensor] | None]:
|
|
330
|
+
"""M is (n_dim, n_hvps), computes H @ M - (n_dim, n_hvps)."""
|
|
331
|
+
|
|
332
|
+
# get grad
|
|
333
|
+
if rgrad is None and hvp_method in ('autograd', 'forward', "batched"):
|
|
334
|
+
if at_x0: rgrad = self.get_grad(create_graph = hvp_method in ('autograd', "batched"))
|
|
335
|
+
else:
|
|
336
|
+
if self.closure is None: raise RuntimeError("Closure is required to calculate HVp")
|
|
337
|
+
with torch.enable_grad():
|
|
338
|
+
loss = self.closure()
|
|
339
|
+
create_graph = hvp_method in ('autograd', "batched")
|
|
340
|
+
rgrad = torch.autograd.grad(loss, self.params, create_graph=create_graph)
|
|
341
|
+
|
|
342
|
+
if hvp_method == "batched":
|
|
343
|
+
assert rgrad is not None
|
|
344
|
+
with torch.enable_grad():
|
|
345
|
+
flat_inputs = torch.cat([g.ravel() for g in rgrad])
|
|
346
|
+
HM_list = torch.autograd.grad(flat_inputs, self.params, grad_outputs=M.T, is_grads_batched=True, retain_graph=retain_graph)
|
|
347
|
+
HM = flatten_jacobian(HM_list).T
|
|
348
|
+
|
|
349
|
+
elif hvp_method == 'autograd':
|
|
350
|
+
assert rgrad is not None
|
|
351
|
+
with torch.enable_grad():
|
|
352
|
+
flat_inputs = torch.cat([g.ravel() for g in rgrad])
|
|
353
|
+
HV_tensors = [torch.autograd.grad(
|
|
354
|
+
flat_inputs, self.params, grad_outputs=col,
|
|
355
|
+
retain_graph = retain_graph or (i < M.size(1) - 1)
|
|
356
|
+
) for i,col in enumerate(M.unbind(1))]
|
|
357
|
+
HM_list = [torch.cat([t.ravel() for t in tensors]) for tensors in HV_tensors]
|
|
358
|
+
HM = torch.stack(HM_list, 1)
|
|
359
|
+
|
|
360
|
+
elif hvp_method == 'forward':
|
|
361
|
+
assert rgrad is not None
|
|
362
|
+
HV_tensors = [hvp_fd_forward(self.closure, self.params, vec_to_tensors(col, self.params), h=h, g_0=rgrad, normalize=normalize)[1] for col in M.unbind(1)]
|
|
363
|
+
HM_list = [torch.cat([t.ravel() for t in tensors]) for tensors in HV_tensors]
|
|
364
|
+
HM = flatten_jacobian(HM_list)
|
|
365
|
+
|
|
366
|
+
elif hvp_method == 'central':
|
|
367
|
+
HV_tensors = [hvp_fd_central(self.closure, self.params, vec_to_tensors(col, self.params), h=h, normalize=normalize)[1] for col in M.unbind(1)]
|
|
368
|
+
HM_list = [torch.cat([t.ravel() for t in tensors]) for tensors in HV_tensors]
|
|
369
|
+
HM = flatten_jacobian(HM_list)
|
|
370
|
+
|
|
371
|
+
else:
|
|
372
|
+
raise ValueError(hvp_method)
|
|
373
|
+
|
|
374
|
+
return HM, rgrad
|
|
375
|
+
|
|
376
|
+
# endregion
|
torchzero/modules/__init__.py
CHANGED
|
@@ -193,8 +193,8 @@ class AdaHessian(Module):
|
|
|
193
193
|
for i in range(n_samples):
|
|
194
194
|
u = [_rademacher_like(p, generator=generator) for p in params]
|
|
195
195
|
|
|
196
|
-
Hvp, rgrad =
|
|
197
|
-
h=fd_h, normalize=True,
|
|
196
|
+
Hvp, rgrad = var.hessian_vector_product(u, at_x0=True, rgrad=rgrad, hvp_method=hvp_method,
|
|
197
|
+
h=fd_h, normalize=True, retain_graph=i < n_samples-1)
|
|
198
198
|
Hvp = tuple(Hvp)
|
|
199
199
|
|
|
200
200
|
if D is None: D = Hvp
|
|
@@ -144,8 +144,8 @@ class ESGD(Module):
|
|
|
144
144
|
for j in range(n_samples):
|
|
145
145
|
u = [torch.randn(p.size(), generator=generator, device=p.device, dtype=p.dtype) for p in params]
|
|
146
146
|
|
|
147
|
-
Hvp, rgrad =
|
|
148
|
-
h=fd_h, normalize=True,
|
|
147
|
+
Hvp, rgrad = var.hessian_vector_product(u, at_x0=True, rgrad=rgrad, hvp_method=hvp_method,
|
|
148
|
+
h=fd_h, normalize=True, retain_graph=j < n_samples-1)
|
|
149
149
|
|
|
150
150
|
if D is None: D = Hvp
|
|
151
151
|
else: torch._foreach_add_(D, Hvp)
|
|
@@ -74,7 +74,7 @@ class MatrixMomentum(Module):
|
|
|
74
74
|
if step > 0:
|
|
75
75
|
s = p - p_prev
|
|
76
76
|
|
|
77
|
-
Hs, _ =
|
|
77
|
+
Hs, _ = var.hessian_vector_product(s, at_x0=True, rgrad=None, hvp_method=hvp_method, h=h, normalize=True, retain_graph=False)
|
|
78
78
|
Hs = [t.detach() for t in Hs]
|
|
79
79
|
|
|
80
80
|
if 'hvp_tfm' in self.children:
|
|
@@ -155,8 +155,8 @@ class SophiaH(Module):
|
|
|
155
155
|
for i in range(n_samples):
|
|
156
156
|
u = [torch.randn(p.shape, device=p.device, dtype=p.dtype, generator=generator) for p in params]
|
|
157
157
|
|
|
158
|
-
Hvp, rgrad =
|
|
159
|
-
h=fd_h, normalize=True,
|
|
158
|
+
Hvp, rgrad = var.hessian_vector_product(u, at_x0=True, rgrad=rgrad, hvp_method=hvp_method,
|
|
159
|
+
h=fd_h, normalize=True, retain_graph=i < n_samples-1)
|
|
160
160
|
Hvp = tuple(Hvp)
|
|
161
161
|
|
|
162
162
|
if h is None: h = Hvp
|
|
@@ -50,7 +50,7 @@ class ConguateGradientBase(Transform, ABC):
|
|
|
50
50
|
```
|
|
51
51
|
|
|
52
52
|
"""
|
|
53
|
-
def __init__(self, defaults
|
|
53
|
+
def __init__(self, defaults, clip_beta: bool, restart_interval: int | None | Literal['auto'], inner: Chainable | None = None):
|
|
54
54
|
if defaults is None: defaults = {}
|
|
55
55
|
defaults['restart_interval'] = restart_interval
|
|
56
56
|
defaults['clip_beta'] = clip_beta
|
|
@@ -140,8 +140,8 @@ class PolakRibiere(ConguateGradientBase):
|
|
|
140
140
|
Note:
|
|
141
141
|
This requires step size to be determined via a line search, so put a line search like ``tz.m.StrongWolfe(c2=0.1, a_init="first-order")`` after this.
|
|
142
142
|
"""
|
|
143
|
-
def __init__(self, clip_beta=True, restart_interval: int | None =
|
|
144
|
-
super().__init__(clip_beta=clip_beta, restart_interval=restart_interval, inner=inner)
|
|
143
|
+
def __init__(self, clip_beta=True, restart_interval: int | None | Literal['auto'] = 'auto', inner: Chainable | None = None):
|
|
144
|
+
super().__init__({}, clip_beta=clip_beta, restart_interval=restart_interval, inner=inner)
|
|
145
145
|
|
|
146
146
|
def get_beta(self, p, g, prev_g, prev_d):
|
|
147
147
|
return polak_ribiere_beta(g, prev_g)
|
|
@@ -158,7 +158,7 @@ class FletcherReeves(ConguateGradientBase):
|
|
|
158
158
|
This requires step size to be determined via a line search, so put a line search like ``tz.m.StrongWolfe(c2=0.1, a_init="first-order")`` after this.
|
|
159
159
|
"""
|
|
160
160
|
def __init__(self, restart_interval: int | None | Literal['auto'] = 'auto', clip_beta=False, inner: Chainable | None = None):
|
|
161
|
-
super().__init__(clip_beta=clip_beta, restart_interval=restart_interval, inner=inner)
|
|
161
|
+
super().__init__({}, clip_beta=clip_beta, restart_interval=restart_interval, inner=inner)
|
|
162
162
|
|
|
163
163
|
def initialize(self, p, g):
|
|
164
164
|
self.global_state['prev_gg'] = g.dot(g)
|
|
@@ -183,8 +183,8 @@ class HestenesStiefel(ConguateGradientBase):
|
|
|
183
183
|
Note:
|
|
184
184
|
This requires step size to be determined via a line search, so put a line search like ``tz.m.StrongWolfe(c2=0.1, a_init="first-order")`` after this.
|
|
185
185
|
"""
|
|
186
|
-
def __init__(self, restart_interval: int | None | Literal['auto'] =
|
|
187
|
-
super().__init__(clip_beta=clip_beta, restart_interval=restart_interval, inner=inner)
|
|
186
|
+
def __init__(self, restart_interval: int | None | Literal['auto'] = 'auto', clip_beta=False, inner: Chainable | None = None):
|
|
187
|
+
super().__init__({}, clip_beta=clip_beta, restart_interval=restart_interval, inner=inner)
|
|
188
188
|
|
|
189
189
|
def get_beta(self, p, g, prev_g, prev_d):
|
|
190
190
|
return hestenes_stiefel_beta(g, prev_d, prev_g)
|
|
@@ -202,8 +202,8 @@ class DaiYuan(ConguateGradientBase):
|
|
|
202
202
|
Note:
|
|
203
203
|
This requires step size to be determined via a line search, so put a line search like ``tz.m.StrongWolfe(c2=0.1)`` after this.
|
|
204
204
|
"""
|
|
205
|
-
def __init__(self, restart_interval: int | None | Literal['auto'] =
|
|
206
|
-
super().__init__(clip_beta=clip_beta, restart_interval=restart_interval, inner=inner)
|
|
205
|
+
def __init__(self, restart_interval: int | None | Literal['auto'] = 'auto', clip_beta=False, inner: Chainable | None = None):
|
|
206
|
+
super().__init__({}, clip_beta=clip_beta, restart_interval=restart_interval, inner=inner)
|
|
207
207
|
|
|
208
208
|
def get_beta(self, p, g, prev_g, prev_d):
|
|
209
209
|
return dai_yuan_beta(g, prev_d, prev_g)
|
|
@@ -221,8 +221,8 @@ class LiuStorey(ConguateGradientBase):
|
|
|
221
221
|
Note:
|
|
222
222
|
This requires step size to be determined via a line search, so put a line search like ``tz.m.StrongWolfe(c2=0.1, a_init="first-order")`` after this.
|
|
223
223
|
"""
|
|
224
|
-
def __init__(self, restart_interval: int | None | Literal['auto'] =
|
|
225
|
-
super().__init__(clip_beta=clip_beta, restart_interval=restart_interval, inner=inner)
|
|
224
|
+
def __init__(self, restart_interval: int | None | Literal['auto'] = 'auto', clip_beta=False, inner: Chainable | None = None):
|
|
225
|
+
super().__init__({}, clip_beta=clip_beta, restart_interval=restart_interval, inner=inner)
|
|
226
226
|
|
|
227
227
|
def get_beta(self, p, g, prev_g, prev_d):
|
|
228
228
|
return liu_storey_beta(g, prev_d, prev_g)
|
|
@@ -239,8 +239,8 @@ class ConjugateDescent(ConguateGradientBase):
|
|
|
239
239
|
Note:
|
|
240
240
|
This requires step size to be determined via a line search, so put a line search like ``tz.m.StrongWolfe(c2=0.1, a_init="first-order")`` after this.
|
|
241
241
|
"""
|
|
242
|
-
def __init__(self, restart_interval: int | None | Literal['auto'] =
|
|
243
|
-
super().__init__(clip_beta=clip_beta, restart_interval=restart_interval, inner=inner)
|
|
242
|
+
def __init__(self, restart_interval: int | None | Literal['auto'] = 'auto', clip_beta=False, inner: Chainable | None = None):
|
|
243
|
+
super().__init__({}, clip_beta=clip_beta, restart_interval=restart_interval, inner=inner)
|
|
244
244
|
|
|
245
245
|
def get_beta(self, p, g, prev_g, prev_d):
|
|
246
246
|
return conjugate_descent_beta(g, prev_d, prev_g)
|
|
@@ -264,8 +264,8 @@ class HagerZhang(ConguateGradientBase):
|
|
|
264
264
|
Note:
|
|
265
265
|
This requires step size to be determined via a line search, so put a line search like ``tz.m.StrongWolfe(c2=0.1, a_init="first-order")`` after this.
|
|
266
266
|
"""
|
|
267
|
-
def __init__(self, restart_interval: int | None | Literal['auto'] =
|
|
268
|
-
super().__init__(clip_beta=clip_beta, restart_interval=restart_interval, inner=inner)
|
|
267
|
+
def __init__(self, restart_interval: int | None | Literal['auto'] = 'auto', clip_beta=False, inner: Chainable | None = None):
|
|
268
|
+
super().__init__({}, clip_beta=clip_beta, restart_interval=restart_interval, inner=inner)
|
|
269
269
|
|
|
270
270
|
def get_beta(self, p, g, prev_g, prev_d):
|
|
271
271
|
return hager_zhang_beta(g, prev_d, prev_g)
|
|
@@ -291,8 +291,8 @@ class DYHS(ConguateGradientBase):
|
|
|
291
291
|
Note:
|
|
292
292
|
This requires step size to be determined via a line search, so put a line search like ``tz.m.StrongWolfe(c2=0.1, a_init="first-order")`` after this.
|
|
293
293
|
"""
|
|
294
|
-
def __init__(self, restart_interval: int | None | Literal['auto'] =
|
|
295
|
-
super().__init__(clip_beta=clip_beta, restart_interval=restart_interval, inner=inner)
|
|
294
|
+
def __init__(self, restart_interval: int | None | Literal['auto'] = 'auto', clip_beta=False, inner: Chainable | None = None):
|
|
295
|
+
super().__init__({}, clip_beta=clip_beta, restart_interval=restart_interval, inner=inner)
|
|
296
296
|
|
|
297
297
|
def get_beta(self, p, g, prev_g, prev_d):
|
|
298
298
|
return dyhs_beta(g, prev_d, prev_g)
|
|
@@ -4,6 +4,7 @@ from .curveball import CurveBall
|
|
|
4
4
|
# from dct import DCTProjection
|
|
5
5
|
from .fft import FFTProjection
|
|
6
6
|
from .gradmin import GradMin
|
|
7
|
+
from .higher_order_newton import HigherOrderNewton
|
|
7
8
|
from .l_infinity import InfinityNormTrustRegion
|
|
8
9
|
from .momentum import (
|
|
9
10
|
CoordinateMomentum,
|
|
@@ -45,9 +45,9 @@ class NewtonNewton(Module):
|
|
|
45
45
|
order: int = 3,
|
|
46
46
|
search_negative: bool = False,
|
|
47
47
|
vectorize: bool = True,
|
|
48
|
-
|
|
48
|
+
eigval_fn: Callable[[torch.Tensor], torch.Tensor] | None = None,
|
|
49
49
|
):
|
|
50
|
-
defaults = dict(order=order, reg=reg, vectorize=vectorize,
|
|
50
|
+
defaults = dict(order=order, reg=reg, vectorize=vectorize, eigval_fn=eigval_fn, search_negative=search_negative)
|
|
51
51
|
super().__init__(defaults)
|
|
52
52
|
|
|
53
53
|
@torch.no_grad
|
|
@@ -61,7 +61,7 @@ class NewtonNewton(Module):
|
|
|
61
61
|
vectorize = settings['vectorize']
|
|
62
62
|
order = settings['order']
|
|
63
63
|
search_negative = settings['search_negative']
|
|
64
|
-
|
|
64
|
+
eigval_fn = settings['eigval_fn']
|
|
65
65
|
|
|
66
66
|
# ------------------------ calculate grad and hessian ------------------------ #
|
|
67
67
|
Hs = []
|
|
@@ -82,8 +82,8 @@ class NewtonNewton(Module):
|
|
|
82
82
|
Hs.append(H)
|
|
83
83
|
|
|
84
84
|
x = None
|
|
85
|
-
if search_negative or (is_last and
|
|
86
|
-
x = _eigh_solve(H, xp,
|
|
85
|
+
if search_negative or (is_last and eigval_fn is not None):
|
|
86
|
+
x = _eigh_solve(H, xp, eigval_fn, search_negative=search_negative)
|
|
87
87
|
if x is None: x = _cholesky_solve(H, xp)
|
|
88
88
|
if x is None: x = _lu_solve(H, xp)
|
|
89
89
|
if x is None: x = _least_squares_solve(H, xp)
|