heavyball 1.4.1__tar.gz → 1.4.4__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 (32) hide show
  1. heavyball-1.4.4/PKG-INFO +934 -0
  2. heavyball-1.4.4/README.md +910 -0
  3. {heavyball-1.4.1 → heavyball-1.4.4}/heavyball/utils.py +23 -28
  4. heavyball-1.4.4/heavyball.egg-info/PKG-INFO +934 -0
  5. {heavyball-1.4.1 → heavyball-1.4.4}/setup.py +1 -1
  6. heavyball-1.4.1/PKG-INFO +0 -136
  7. heavyball-1.4.1/README.md +0 -112
  8. heavyball-1.4.1/heavyball.egg-info/PKG-INFO +0 -136
  9. {heavyball-1.4.1 → heavyball-1.4.4}/LICENSE +0 -0
  10. {heavyball-1.4.1 → heavyball-1.4.4}/heavyball/__init__.py +0 -0
  11. {heavyball-1.4.1 → heavyball-1.4.4}/heavyball/chainable.py +0 -0
  12. {heavyball-1.4.1 → heavyball-1.4.4}/heavyball.egg-info/SOURCES.txt +0 -0
  13. {heavyball-1.4.1 → heavyball-1.4.4}/heavyball.egg-info/dependency_links.txt +0 -0
  14. {heavyball-1.4.1 → heavyball-1.4.4}/heavyball.egg-info/requires.txt +0 -0
  15. {heavyball-1.4.1 → heavyball-1.4.4}/heavyball.egg-info/top_level.txt +0 -0
  16. {heavyball-1.4.1 → heavyball-1.4.4}/setup.cfg +0 -0
  17. {heavyball-1.4.1 → heavyball-1.4.4}/test/test_bf16_params.py +0 -0
  18. {heavyball-1.4.1 → heavyball-1.4.4}/test/test_bf16_q.py +0 -0
  19. {heavyball-1.4.1 → heavyball-1.4.4}/test/test_bf16_storage.py +0 -0
  20. {heavyball-1.4.1 → heavyball-1.4.4}/test/test_caution.py +0 -0
  21. {heavyball-1.4.1 → heavyball-1.4.4}/test/test_channels_last.py +0 -0
  22. {heavyball-1.4.1 → heavyball-1.4.4}/test/test_closure.py +0 -0
  23. {heavyball-1.4.1 → heavyball-1.4.4}/test/test_ema.py +0 -0
  24. {heavyball-1.4.1 → heavyball-1.4.4}/test/test_foreach.py +0 -0
  25. {heavyball-1.4.1 → heavyball-1.4.4}/test/test_hook.py +0 -0
  26. {heavyball-1.4.1 → heavyball-1.4.4}/test/test_mars.py +0 -0
  27. {heavyball-1.4.1 → heavyball-1.4.4}/test/test_memory.py +0 -0
  28. {heavyball-1.4.1 → heavyball-1.4.4}/test/test_merge.py +0 -0
  29. {heavyball-1.4.1 → heavyball-1.4.4}/test/test_no_grad.py +0 -0
  30. {heavyball-1.4.1 → heavyball-1.4.4}/test/test_psgd.py +0 -0
  31. {heavyball-1.4.1 → heavyball-1.4.4}/test/test_soap.py +0 -0
  32. {heavyball-1.4.1 → heavyball-1.4.4}/test/test_stochastic_updates.py +0 -0
@@ -0,0 +1,934 @@
1
+ Metadata-Version: 2.1
2
+ Name: heavyball
3
+ Version: 1.4.4
4
+ Summary: Efficient optimizers
5
+ Home-page: https://github.com/clashluke/heavyball
6
+ Author: Lucas Nestler
7
+ Author-email: github.heavyball@nestler.sh
8
+ License: BSD
9
+ Classifier: Development Status :: 5 - Production/Stable
10
+ Classifier: License :: OSI Approved :: BSD License
11
+ Classifier: Programming Language :: Python
12
+ Classifier: Programming Language :: Python :: 3.7
13
+ Classifier: Programming Language :: Python :: 3.8
14
+ Classifier: Programming Language :: Python :: 3.9
15
+ Classifier: Topic :: Software Development :: Libraries
16
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
17
+ Classifier: Intended Audience :: Developers
18
+ Requires-Python: >=3.7
19
+ Description-Content-Type: text/markdown
20
+ License-File: LICENSE
21
+ Requires-Dist: opt-einsum
22
+ Requires-Dist: torch
23
+ Requires-Dist: numpy
24
+
25
+ # `heavyball`: Efficient Optimizers
26
+
27
+ * [Public API](#Public-API)
28
+ - [Foreach Optimizers](#Foreach-Optimizers)
29
+ - [`heavyball.utils`](#heavyball.utils)
30
+ - [Example Usage](#Example-Usage)
31
+
32
+ * [`heavyball.chainable`](##heavyball.chainable)
33
+ - [Core Concept](#Core-Concept)
34
+ - [`FunctionTransform` and Guards](#FunctionTransform-and-Guards)
35
+ - [Chaining Transformations](#Chaining-Transformations)
36
+ - [Building Optimizers](#Building-Optimizers)
37
+ - [Creating New Transformations](#Creating-New-Transformations)
38
+
39
+ * [Optimizer Recommendations](#Optimizer-Recommendations)
40
+ - [Choosing the Right Optimizer](#Choosing-the-Right-Optimizer)
41
+
42
+ ---
43
+
44
+ The `heavyball` library provides a collection of efficient optimizers designed for deep learning. It leverages
45
+ techniques like preconditioning, momentum, and adaptive learning rates to accelerate training and improve convergence.
46
+ The library's core strength lies in its `chainable` API, which allows for flexible composition of optimizers, enabling
47
+ users to build custom optimization strategies.
48
+
49
+ ## Public API
50
+
51
+ The `heavyball` library exposes the following optimizers through its main namespace:
52
+
53
+ ### Foreach Optimizers
54
+
55
+ These optimizers are designed to be efficient by operating on batches of parameters simultaneously using `foreach`
56
+ operations whenever possible.
57
+
58
+ #### `ForeachAdamW`
59
+
60
+ ```python
61
+ class ForeachAdamW(C.BaseOpt):
62
+ def __init__(self, params, lr=0.0025, betas=(0.9, 0.99), eps=1e-8, weight_decay=0, warmup_steps=0,
63
+ foreach: bool = True, storage_dtype: str = 'float32', mars: bool = False, caution: bool = False,
64
+ mars_gamma: float = 0.0025, gradient_clipping: C.str_or_fn = C.use_default,
65
+ update_clipping: C.str_or_fn = C.use_default, palm: bool = C.use_default, beta2_scale: float = 0.8):
66
+ # ...
67
+ ```
68
+
69
+ A foreach implementation of the AdamW optimizer. It incorporates weight decay into the update rule and uses adaptive
70
+ learning rates based on the first and second moments of the gradients.
71
+
72
+ **Key Parameters:**
73
+
74
+ * **`lr`**: Learning rate.
75
+ * **`betas`**: Coefficients used for computing running averages of the gradient and its square.
76
+ * **`eps`**: A small constant for numerical stability.
77
+ * **`weight_decay`**: Weight decay coefficient.
78
+ * **`warmup_steps`**: Number of steps for linear learning rate warmup.
79
+ * **`foreach`**: Enables/disables the use of `foreach` operations.
80
+ * **`storage_dtype`**: The floating-point type to be used for internal state. `"float32"` or `"bfloat16"`.
81
+ * **`mars`**: Enables/disables Mars correction.
82
+ * **`caution`**: Enables/disables the use of a cautious update rule, avoiding updates that point in the opposite
83
+ direction to the gradients.
84
+ * **`mars_gamma`**: Mars correction coefficient.
85
+ * **`gradient_clipping`**: Gradient clipping function or method. See `heavyball.utils` for available options.
86
+ * **`update_clipping`**: Update clipping function or method. See `heavyball.utils` for available options.
87
+ * **`palm`**: Enables/disables PaLM's beta2 schedule.
88
+ * **`beta2_scale`**: if we're using the PaLM schedule, `beta2 = step ** -beta2_scale`
89
+
90
+ #### `ForeachRMSprop`
91
+
92
+ ```python
93
+ class ForeachRMSprop(C.BaseOpt):
94
+ def __init__(self, params, lr=0.0025, betas=(0.9, 0.99), eps=1e-6, weight_decay=0, warmup_steps=0, r=0.0,
95
+ weight_lr_power=2.0, foreach: bool = True, storage_dtype: str = 'float32', mars: bool = False,
96
+ caution: bool = False, mars_gamma: float = 0.0025, gradient_clipping: C.str_or_fn = C.use_default,
97
+ update_clipping: C.str_or_fn = C.use_default, palm: bool = C.use_default, beta2_scale: float = 0.8):
98
+ # ...
99
+ ```
100
+
101
+ A foreach implementation of a debiased RMSprop optimizer (Note: this is different from `torch.optim.RMSprop`). It uses
102
+ adaptive learning rates based on the second moment of the gradients.
103
+
104
+ **Key Parameters:**
105
+
106
+ * **`lr`**: Learning rate.
107
+ * **`betas`**: Coefficients used for computing running averages of the squared gradient.
108
+ * **`eps`**: A small constant for numerical stability.
109
+ * **`weight_decay`**: Weight decay coefficient.
110
+ * **`warmup_steps`**: Number of steps for linear learning rate warmup.
111
+ * **`r`**: Schedule-Free coefficient that controls dependence of the learning rate on step count.
112
+ * **`weight_lr_power`**: Schedule-Free coefficient that controls the sensitivity of `r` to the learning rate.
113
+ * **`foreach`**: Enables/disables the use of `foreach` operations.
114
+ * **`storage_dtype`**: The floating-point type to be used for internal state. `"float32"` or `"bfloat16"`.
115
+ * **`mars`**: Enables/disables Mars correction.
116
+ * **`caution`**: Enables/disables the use of a cautious update rule, avoiding updates that point in the opposite
117
+ direction to the gradients.
118
+ * **`mars_gamma`**: Mars correction coefficient.
119
+ * **`gradient_clipping`**: Gradient clipping function or method. See `heavyball.utils` for available options.
120
+ * **`update_clipping`**: Update clipping function or method. See `heavyball.utils` for available options.
121
+ * **`palm`**: Enables/disables PaLM's beta2 schedule.
122
+ * **`beta2_scale`**: if we're using the PaLM schedule, `beta2 = step ** -beta2_scale`
123
+
124
+ #### `ForeachSFAdamW`
125
+
126
+ ```python
127
+ class ForeachSFAdamW(C.ScheduleFree):
128
+ def __init__(self, params, lr=0.0025, betas=(0.9, 0.99), eps=1e-6, weight_decay=0, warmup_steps=0, r=0.0,
129
+ weight_lr_power=2.0, foreach: bool = True, storage_dtype: str = 'float32', mars: bool = False,
130
+ caution: bool = False, mars_gamma: float = 0.0025, gradient_clipping: C.str_or_fn = C.use_default,
131
+ update_clipping: C.str_or_fn = C.use_default, palm: bool = C.use_default, beta2_scale: float = 0.8):
132
+ # ...
133
+ ```
134
+
135
+ A foreach implementation of the Schedule-Free AdamW optimizer. It combines the benefits of AdamW with the Schedule-Free
136
+ approach, which dynamically adjusts the learning rate based on the current state of optimization.
137
+
138
+ **Key Parameters:**
139
+
140
+ * **`lr`**: Base learning rate. The effective learning rate at each step depends on `lr`, `r`, and `weight_lr_power`.
141
+ * **`betas`**: Coefficients used for computing running averages of the gradient and its square.
142
+ * **`eps`**: A small constant for numerical stability.
143
+ * **`weight_decay`**: Weight decay coefficient.
144
+ * **`warmup_steps`**: Number of steps for linear learning rate warmup.
145
+ * **`r`**: Schedule-Free coefficient that controls dependence of the learning rate on step count.
146
+ * **`weight_lr_power`**: Schedule-Free coefficient that controls the sensitivity of `r` to the learning rate.
147
+ * **`foreach`**: Enables/disables the use of `foreach` operations.
148
+ * **`storage_dtype`**: The floating-point type to be used for internal state. `"float32"` or `"bfloat16"`.
149
+ * **`mars`**: Enables/disables Mars correction.
150
+ * **`caution`**: Enables/disables the use of a cautious update rule, avoiding updates that point in the opposite
151
+ direction to the gradients.
152
+ * **`mars_gamma`**: Mars correction coefficient.
153
+ * **`gradient_clipping`**: Gradient clipping function or method. See `heavyball.utils` for available options.
154
+ * **`update_clipping`**: Update clipping function or method. See `heavyball.utils` for available options.
155
+ * **`palm`**: Enables/disables PaLM's beta2 schedule.
156
+ * **`beta2_scale`**: if we're using the PaLM schedule, `beta2 = step ** -beta2_scale`
157
+
158
+ #### `PaLMForeachSFAdamW`
159
+
160
+ ```python
161
+ class PaLMForeachSFAdamW(ForeachSFAdamW):
162
+ palm: bool = True
163
+ ```
164
+
165
+ A specialized version of `ForeachSFAdamW` with PaLM's beta2 schedule enabled by default.
166
+
167
+ #### `ForeachADOPT`
168
+
169
+ ```python
170
+ class ForeachADOPT(C.BaseOpt):
171
+ def __init__(self, params, lr=0.0025, betas=(0.9, 0.99), eps=1e-8, weight_decay=0, warmup_steps=0,
172
+ foreach: bool = True, storage_dtype: str = 'float32', mars: bool = False, caution: bool = False,
173
+ mars_gamma: float = 0.0025, gradient_clipping: C.str_or_fn = C.use_default,
174
+ update_clipping: C.str_or_fn = C.use_default, palm: bool = C.use_default, beta2_scale: float = 0.8):
175
+ # ...
176
+ ```
177
+
178
+ A foreach implementation of the ADOPT optimizer, which uses a debiased estimate of the second moment of the gradients.
179
+
180
+ **Key Parameters:**
181
+
182
+ * **`lr`**: Learning rate.
183
+ * **`betas`**: Coefficients used for computing running averages of the gradient and its square.
184
+ * **`eps`**: A small constant for numerical stability.
185
+ * **`weight_decay`**: Weight decay coefficient.
186
+ * **`warmup_steps`**: Number of steps for linear learning rate warmup.
187
+ * **`foreach`**: Enables/disables the use of `foreach` operations.
188
+ * **`storage_dtype`**: The floating-point type to be used for internal state. `"float32"` or `"bfloat16"`.
189
+ * **`mars`**: Enables/disables Mars correction.
190
+ * **`caution`**: Enables/disables the use of a cautious update rule, avoiding updates that point in the opposite
191
+ direction to the gradients.
192
+ * **`mars_gamma`**: Mars correction coefficient.
193
+ * **`gradient_clipping`**: Gradient clipping function or method. See `heavyball.utils` for available options.
194
+ * **`update_clipping`**: Update clipping function or method. See `heavyball.utils` for available options.
195
+ * **`palm`**: Enables/disables PaLM's beta2 schedule.
196
+ * **`beta2_scale`**: if we're using the PaLM schedule, `beta2 = step ** -beta2_scale`
197
+
198
+ #### `ForeachMuon`
199
+
200
+ ```python
201
+ class ForeachMuon(C.BaseOpt):
202
+ def __init__(self, params, lr=0.0025, betas=(0.9, 0.99), eps=1e-8, weight_decay=0, warmup_steps=0,
203
+ foreach: bool = True, storage_dtype: str = 'float32', mars: bool = False, caution: bool = False,
204
+ mars_gamma: float = 0.0025, gradient_clipping: C.str_or_fn = C.use_default,
205
+ update_clipping: C.str_or_fn = C.use_default, palm: bool = C.use_default, beta2_scale: float = 0.8,
206
+ nesterov: bool = True):
207
+ # ...
208
+ ```
209
+
210
+ A foreach implementation of the Muon optimizer, incorporating orthogonal updates via the `orthogonalize_update`
211
+ transformation.
212
+
213
+ **Key Parameters:**
214
+
215
+ * **`lr`**: Learning rate.
216
+ * **`betas`**: Coefficients used for computing running averages of the gradient and its square.
217
+ * **`eps`**: A small constant for numerical stability.
218
+ * **`weight_decay`**: Weight decay coefficient.
219
+ * **`warmup_steps`**: Number of steps for linear learning rate warmup.
220
+ * **`foreach`**: Enables/disables the use of `foreach` operations.
221
+ * **`storage_dtype`**: The floating-point type to be used for internal state. `"float32"` or `"bfloat16"`.
222
+ * **`mars`**: Enables/disables Mars correction.
223
+ * **`caution`**: Enables/disables the use of a cautious update rule, avoiding updates that point in the opposite
224
+ direction to the gradients.
225
+ * **`mars_gamma`**: Mars correction coefficient.
226
+ * **`gradient_clipping`**: Gradient clipping function or method. See `heavyball.utils` for available options.
227
+ * **`update_clipping`**: Update clipping function or method. See `heavyball.utils` for available options.
228
+ * **`palm`**: Enables/disables PaLM's beta2 schedule.
229
+ * **`beta2_scale`**: if we're using the PaLM schedule, `beta2 = step ** -beta2_scale`
230
+ * **`nesterov`**: Enables/disables Nesterov momentum.
231
+
232
+ #### `ForeachLaProp`
233
+
234
+ ```python
235
+ class ForeachLaProp(C.BaseOpt):
236
+ def __init__(self, params, lr=0.0025, betas=(0.9, 0.99), eps=1e-8, weight_decay=0, warmup_steps=0,
237
+ foreach: bool = True, storage_dtype: str = 'float32', mars: bool = False, caution: bool = False,
238
+ mars_gamma: float = 0.0025, gradient_clipping: C.str_or_fn = C.use_default,
239
+ update_clipping: C.str_or_fn = C.use_default, palm: bool = C.use_default, beta2_scale: float = 0.8):
240
+ # ...
241
+ ```
242
+
243
+ A foreach implementation of the LaProp optimizer.
244
+
245
+ **Key Parameters:**
246
+
247
+ * **`lr`**: Learning rate.
248
+ * **`betas`**: Coefficients used for computing running averages of the gradient and its square.
249
+ * **`eps`**: A small constant for numerical stability.
250
+ * **`weight_decay`**: Weight decay coefficient.
251
+ * **`warmup_steps`**: Number of steps for linear learning rate warmup.
252
+ * **`foreach`**: Enables/disables the use of `foreach` operations.
253
+ * **`storage_dtype`**: The floating-point type to be used for internal state. `"float32"` or `"bfloat16"`.
254
+ * **`mars`**: Enables/disables Mars correction.
255
+ * **`caution`**: Enables/disables the use of a cautious update rule, avoiding updates that point in the opposite
256
+ direction to the gradients.
257
+ * **`mars_gamma`**: Mars correction coefficient.
258
+ * **`gradient_clipping`**: Gradient clipping function or method. See `heavyball.utils` for available options.
259
+ * **`update_clipping`**: Update clipping function or method. See `heavyball.utils` for available options.
260
+ * **`palm`**: Enables/disables PaLM's beta2 schedule.
261
+ * **`beta2_scale`**: if we're using the PaLM schedule, `beta2 = step ** -beta2_scale`
262
+
263
+ #### `MuonLaProp`
264
+
265
+ ```python
266
+ class MuonLaProp(C.BaseOpt):
267
+ def __init__(self, params, lr=0.0025, betas=(0.9, 0.99), eps=1e-8, weight_decay=0, warmup_steps=0,
268
+ foreach: bool = True, storage_dtype: str = 'float32', mars: bool = False, caution: bool = False,
269
+ mars_gamma: float = 0.0025, gradient_clipping: C.str_or_fn = C.use_default,
270
+ update_clipping: C.str_or_fn = C.use_default, palm: bool = C.use_default, beta2_scale: float = 0.8):
271
+ # ...
272
+ ```
273
+
274
+ A variant of LaProp that incorporates orthogonal updates via the `orthogonalize_update` transformation.
275
+
276
+ **Key Parameters:**
277
+
278
+ * **`lr`**: Learning rate.
279
+ * **`betas`**: Coefficients used for computing running averages of the gradient and its square.
280
+ * **`eps`**: A small constant for numerical stability.
281
+ * **`weight_decay`**: Weight decay coefficient.
282
+ * **`warmup_steps`**: Number of steps for linear learning rate warmup.
283
+ * **`foreach`**: Enables/disables the use of `foreach` operations.
284
+ * **`storage_dtype`**: The floating-point type to be used for internal state. `"float32"` or `"bfloat16"`.
285
+ * **`mars`**: Enables/disables Mars correction.
286
+ * **`caution`**: Enables/disables the use of a cautious update rule, avoiding updates that point in the opposite
287
+ direction to the gradients.
288
+ * **`mars_gamma`**: Mars correction coefficient.
289
+ * **`gradient_clipping`**: Gradient clipping function or method. See `heavyball.utils` for available options.
290
+ * **`update_clipping`**: Update clipping function or method. See `heavyball.utils` for available options.
291
+ * **`palm`**: Enables/disables PaLM's beta2 schedule.
292
+ * **`beta2_scale`**: if we're using the PaLM schedule, `beta2 = step ** -beta2_scale`
293
+
294
+ #### `ForeachSOAP`
295
+
296
+ ```python
297
+ class ForeachSOAP(C.BaseOpt):
298
+ use_precond_schedule: bool = False
299
+
300
+ def __init__(self, params, lr: float = 3e-3, betas=(0.9, 0.95), shampoo_beta: float = 0.95, eps: float = 1e-8,
301
+ weight_decay: float = 0.01, precondition_frequency: int = 2, max_precond_dim: int = 2048, #
302
+ merge_dims: bool = True, precondition_1d: bool = False, normalize_grads: bool = False,
303
+ data_format: str = "channels_first", correct_bias: bool = True, warmup_steps: int = 1,
304
+ split: bool = False, foreach: bool = True, mars: bool = False, caution: bool = False,
305
+ mars_gamma: float = 0.0025, palm: bool = C.use_default, precond_scheduler=(1 / 3, 9),
306
+ beta2_scale: float = 0.8, use_precond_schedule: bool = C.use_default,
307
+ gradient_clipping: C.str_or_fn = C.use_default, update_clipping: C.str_or_fn = C.use_default):
308
+ # ...
309
+ ```
310
+
311
+ A foreach implementation of the SOAP (Second-Order Adaptive Preconditioner) optimizer. It uses a preconditioner based on
312
+ the second-order statistics of the gradients to accelerate convergence.
313
+
314
+ **Key Parameters:**
315
+
316
+ * **`lr`**: Learning rate.
317
+ * **`betas`**: Coefficients used for computing running averages of the gradient.
318
+ * **`shampoo_beta`**: Coefficient used for computing running average of the preconditioner.
319
+ * **`eps`**: A small constant for numerical stability.
320
+ * **`weight_decay`**: Weight decay coefficient.
321
+ * **`precondition_frequency`**: Frequency of preconditioner updates. If using `use_precond_schedule`, this parameter is
322
+ ignored.
323
+ * **`max_precond_dim`**: Maximum dimension of the preconditioner.
324
+ * **`merge_dims`**: Whether to merge dimensions when forming the preconditioner.
325
+ * **`precondition_1d`**: Whether to use a 1D preconditioner for 1D parameters.
326
+ * **`normalize_grads`**: Whether to normalize gradients before applying SOAP.
327
+ * **`data_format`**: `"channels_first"` or `"channels_last"`. Specifies the data format of the input tensors.
328
+ * **`correct_bias`**: Enables/disables bias correction for the running averages.
329
+ * **`warmup_steps`**: Number of steps for linear learning rate warmup.
330
+ * **`split`**: Whether to split large dimensions when forming the preconditioner.
331
+ * **`foreach`**: Enables/disables the use of `foreach` operations.
332
+ * **`mars`**: Enables/disables Mars correction.
333
+ * **`caution`**: Enables/disables the use of a cautious update rule, avoiding updates that point in the opposite
334
+ direction to the gradients.
335
+ * **`mars_gamma`**: Mars correction coefficient.
336
+ * **`palm`**: Enables/disables PaLM's beta2 schedule.
337
+ * **`precond_scheduler`**: A tuple `(power, log_base)` specifying the preconditioner update schedule, where the update
338
+ probability is `1 / (step ** power * log_base)`. This parameter is only used if `use_precond_schedule` is `True`.
339
+ * **`beta2_scale`**: if we're using the PaLM schedule, `beta2 = step ** -beta2_scale`
340
+ * **`use_precond_schedule`**: Whether to use a dynamic preconditioner update schedule instead of a fixed frequency.
341
+ * **`gradient_clipping`**: Gradient clipping function or method. See `heavyball.utils` for available options.
342
+ * **`update_clipping`**: Update clipping function or method. See `heavyball.utils` for available options.
343
+
344
+ #### `PaLMForeachSOAP`
345
+
346
+ ```python
347
+ class PaLMForeachSOAP(ForeachSOAP):
348
+ use_precond_schedule: bool = False
349
+ palm: bool = True
350
+ ```
351
+
352
+ A specialized version of `ForeachSOAP` with PaLM's beta2 schedule enabled by default.
353
+
354
+ #### `PrecondScheduleForeachSOAP`
355
+
356
+ ```python
357
+ class PrecondScheduleForeachSOAP(ForeachSOAP):
358
+ use_precond_schedule: bool = True
359
+ ```
360
+
361
+ A specialized version of `ForeachSOAP` that uses a dynamic preconditioner update schedule.
362
+
363
+ #### `PrecondSchedulePaLMForeachSOAP`
364
+
365
+ ```python
366
+ class PrecondSchedulePaLMForeachSOAP(ForeachSOAP):
367
+ use_precond_schedule: bool = True
368
+ palm: bool = True
369
+ ```
370
+
371
+ A specialized version of `ForeachSOAP` with both PaLM-specific modifications and a dynamic preconditioner update
372
+ schedule enabled by default.
373
+
374
+ #### `ForeachPSGDKron`
375
+
376
+ ```python
377
+ class ForeachPSGDKron(C.BaseOpt):
378
+ delayed: bool = False
379
+ cached: bool = False
380
+ exp_avg_input: bool = True
381
+
382
+ def __init__(self, params, lr=0.001, beta=0.9, weight_decay=0.0, preconditioner_update_probability=None,
383
+ max_size_triangular=2048, min_ndim_triangular=2, memory_save_mode=None,
384
+ momentum_into_precond_update=True, warmup_steps: int = 1, merge_dims: bool = False,
385
+ split: bool = False, store_triu_as_line: bool = True, foreach: bool = True, q_dtype='float32',
386
+ stochastic_schedule: bool = True, storage_dtype: str = 'float32', mars: bool = False,
387
+ caution: bool = False, mars_gamma: float = 0.0025, delayed: Optional[bool] = C.use_default,
388
+ cached: Optional[bool] = C.use_default, exp_avg_input: Optional[bool] = C.use_default,
389
+ gradient_clipping: C.str_or_fn = C.use_default, update_clipping: C.str_or_fn = C.use_default, #
390
+ # expert parameters
391
+ precond_init_scale=1.0, precond_lr=0.1):
392
+ # ...
393
+ ```
394
+
395
+ A foreach implementation of the PSGD (Preconditioned Stochastic Gradient Descent) optimizer with Kronecker-factored
396
+ preconditioners.
397
+
398
+ **Key Parameters:**
399
+
400
+ * **`lr`**: Learning rate.
401
+ * **`beta`**: Coefficient used for computing running average of the gradient.
402
+ * **`weight_decay`**: Weight decay coefficient.
403
+ * **`preconditioner_update_probability`**: Probability of updating the preconditioner at each step. If `None`, a default
404
+ schedule is used.
405
+ * **`max_size_triangular`**: Maximum size of triangular matrices used in the preconditioner.
406
+ * **`min_ndim_triangular`**: Minimum number of dimensions for a tensor to be considered for triangular preconditioner.
407
+ * **`memory_save_mode`**: Memory saving mode for the preconditioner. Can be `None`, `"one_diag"`, or `"all_diag"`.
408
+ * **`momentum_into_precond_update`**: Whether to use momentum in the preconditioner update.
409
+ * **`warmup_steps`**: Number of steps for linear learning rate warmup.
410
+ * **`merge_dims`**: Whether to merge dimensions when forming the preconditioner.
411
+ * **`split`**: Whether to split large dimensions when forming the preconditioner.
412
+ * **`store_triu_as_line`**: Whether to store the upper triangular part of the preconditioner as a 1D vector.
413
+ * **`foreach`**: Enables/disables the use of `foreach` operations.
414
+ * **`q_dtype`**: The floating-point type to be used for the preconditioner. `"float32"` or `"bfloat16"`.
415
+ * **`stochastic_schedule`**: Whether to use a stochastic schedule for updating the preconditioner.
416
+ * **`storage_dtype`**: The floating-point type to be used for internal state. `"float32"` or `"bfloat16"`.
417
+ * **`mars`**: Enables/disables Mars correction.
418
+ * **`caution`**: Enables/disables the use of a cautious update rule, avoiding updates that point in the opposite
419
+ direction to the gradients.
420
+ * **`mars_gamma`**: Mars correction coefficient.
421
+ * **`delayed`**: Enables/disables delayed preconditioner updates.
422
+ * **`cached`**: Enables/disables caching of preconditioner-related computations.
423
+ * **`exp_avg_input`**: Whether to apply `exp_avg` to the input before calculating the preconditioner.
424
+ * **`gradient_clipping`**: Gradient clipping function or method. See `heavyball.utils` for available options.
425
+ * **`update_clipping`**: Update clipping function or method. See `heavyball.utils` for available options.
426
+ * **`precond_init_scale`**: Initial scale of the preconditioner.
427
+ * **`precond_lr`**: Learning rate for preconditioner updates.
428
+
429
+ #### `ForeachPurePSGD`
430
+
431
+ ```python
432
+ class ForeachPurePSGD(ForeachPSGDKron):
433
+ exp_avg_input: bool = False
434
+ ```
435
+
436
+ A specialized version of `ForeachPSGDKron` that does not apply `exp_avg` to the input before calculating the
437
+ preconditioner.
438
+
439
+ #### `ForeachCachedDelayedPSGDKron`
440
+
441
+ ```python
442
+ class ForeachCachedDelayedPSGDKron(ForeachPSGDKron):
443
+ delayed: bool = True
444
+ cached: bool = True
445
+ ```
446
+
447
+ A specialized version of `ForeachPSGDKron` with both delayed preconditioner updates and caching enabled by default.
448
+
449
+ #### `ForeachCachedPSGDKron`
450
+
451
+ ```python
452
+ class ForeachCachedPSGDKron(ForeachPSGDKron):
453
+ cached: bool = True
454
+ ```
455
+
456
+ A specialized version of `ForeachPSGDKron` with caching enabled by default.
457
+
458
+ #### `ForeachDelayedPSGD`
459
+
460
+ ```python
461
+ class ForeachDelayedPSGD(ForeachPSGDKron):
462
+ delayed: bool = True
463
+ ```
464
+
465
+ A specialized version of `ForeachPSGDKron` with delayed preconditioner updates enabled by default.
466
+
467
+ ## `heavyball.utils`
468
+
469
+ The `heavyball.utils` module provides several important functions and settings that users may find useful:
470
+
471
+ ### Settings
472
+
473
+ * **`compile_mode`**: (defaults to `"max-autotune-no-cudagraphs"`) Controls the compilation mode used by
474
+ `torch.compile`. Setting this to `"default"` or `"max-autotune-no-cudagraphs"` improves performance at the cost of
475
+ increasd compile time. Setting it to `None` disables compilation.
476
+ * **`dynamic`**: (defaults to `False`) Enables/disables dynamic shapes during compilation. Enabling this reduces
477
+ compilation time but may lead to slower execution.
478
+ * **`zeroth_power_mode`**: (defaults to `"qr"`) Controls the method used for computing the zeroth power of a matrix (
479
+ orthogonalization) in certain preconditioners. Options include:
480
+ * `"qr"`: Uses QR decomposition.
481
+ * `"svd"`: Uses singular value decomposition.
482
+ * `"newtonschulz"`: Uses Newton-Schulz iteration.
483
+
484
+ ### Gradient/Update Clipping
485
+
486
+ The following functions are used for gradient and update clipping. They can be passed to the `gradient_clipping` or
487
+ `update_clipping` arguments of the optimizers:
488
+
489
+ * **`l2_clip_`**: Clips the gradient/update by its L2 norm.
490
+ * **`rmsnorm_clip_`**: Clips the gradient/update by its RMS norm.
491
+ * **`trust_region_clip_`**: Clips the gradient/update using a trust region method.
492
+ * **`mu_law_compress`**: Compresses the gradient/update using the µ-law algorithm.
493
+ * **`a_law_compress`**: Compresses the gradient/update using the A-law algorithm.
494
+ * **`identity`**: Does not modify the gradient/update (no clipping).
495
+
496
+ ### Other Utilities
497
+
498
+ * **`set_torch`**: Sets recommended PyTorch settings for performance, including enabling cuDNN benchmark mode, disabling
499
+ deterministic algorithms, setting the precision of float32 matrix multiplications, and enabling opt-einsum with the "
500
+ auto-hq" strategy.
501
+ * **`clean`**: Clears the CUDA cache.
502
+ * **`hook_optimizer_into_model`**: Hooks an optimizer into a model's `post_accumulate_grad_hook`.
503
+ * **`fused_hook`**: Hooks an optimizer into a model's `post_accumulate_grad_hook`, fusing multiple parameter updates
504
+ into a single step.
505
+ * **`disable_caution_scaling`**: Disables the scaling factor applied when `caution` is enabled in optimizers.
506
+
507
+ ## Example Usage
508
+
509
+ ```python
510
+ import torch
511
+ from torch import nn
512
+ import heavyball
513
+
514
+ # Define a simple model
515
+ model = nn.Linear(10, 2)
516
+
517
+ # Create an optimizer
518
+ optimizer = heavyball.ForeachAdamW(model.parameters(), lr=1e-3, weight_decay=1e-2)
519
+ # alternative:
520
+ optimizer = heavyball.AdamW(model.parameters(), lr=1e-3, weight_decay=1e-2)
521
+
522
+ # Generate some dummy data
523
+ input = torch.randn(1, 10)
524
+ target = torch.randn(1, 2)
525
+
526
+ # Training loop
527
+ for _ in range(100):
528
+ # Forward pass
529
+ output = model(input)
530
+ loss = (output - target).sum()
531
+
532
+ # Backward pass
533
+ loss.backward()
534
+
535
+ # Optimizer step
536
+ optimizer.step()
537
+
538
+ # optional: zero gradients; optimizer.step() already does this, which is different from torch.optim
539
+ optimizer.zero_grad()
540
+ ```
541
+
542
+ This example demonstrates how to create an `AdamW` optimizer and use it to train a simple linear model. You can easily
543
+ replace `AdamW` with any other optimizer from the `heavyball` library and customize its behavior using the various
544
+ available parameters and settings.
545
+
546
+ By using `heavyball`'s optimizers and understanding the options in `heavyball.utils`, users can achieve better
547
+ performance, control over training, and easier experimentation with advanced optimization techniques.
548
+
549
+
550
+ ---
551
+
552
+ # `heavyball.chainable`: A Composable Optimizer API
553
+
554
+ The `heavyball.chainable` module provides a powerful and flexible way to build optimizers through function composition,
555
+ similar to Optax. It allows you to chain together a sequence of transformations to create custom optimization algorithms
556
+ tailored to your specific needs. This modular approach makes it easy to experiment with different optimization
557
+ strategies and build complex optimizers from simple, reusable components.
558
+
559
+ ## Core Concept
560
+
561
+ At the heart of `heavyball.chainable` lies the concept of gradient transformations. A gradient transformation is simply
562
+ a function that takes a state dictionary, a group dictionary, an update tensor, a gradient tensor, and a parameter
563
+ tensor as input, and returns a new (or modified) update tensor. These transformations can be chained together to form an
564
+ optimization algorithm.
565
+
566
+ The state dictionary stores any persistent state needed by the transformation, such as momentum buffers or
567
+ preconditioners. The group dictionary contains hyperparameters specific to a group of parameters. The update tensor is
568
+ the current update being processed, the gradient tensor is the gradient of the loss with respect to the parameter, and
569
+ the parameter tensor is the parameter itself.
570
+
571
+ ### Function Signature
572
+
573
+ A typical gradient transformation function has the following signature:
574
+
575
+ ```python
576
+
577
+ def my_transformation(state: dict, group: dict, update: List[torch.Tensor], grad: List[torch.Tensor],
578
+ param: List[torch.Tensor]) -> torch.Tensor:
579
+ # ... transformation logic ...
580
+ return update
581
+ ```
582
+
583
+ or
584
+
585
+ ```python
586
+ @C.no_state_no_foreach
587
+ def my_transformation(group: dict, update: torch.Tensor, grad: torch.Tensor, param: torch.Tensor, *args,
588
+ **kwargs) -> torch.Tensor:
589
+ # ... transformation logic ...
590
+ return update
591
+ ```
592
+
593
+ Note that the second version has no state and processes updates one by one, while the first version processes updates
594
+ in parallel.
595
+
596
+ These functions modify the `update` in place or return a new tensor.
597
+
598
+ ### Example: Scaling by the learning rate
599
+
600
+ ```python
601
+ from heavyball import chainable as C
602
+
603
+
604
+ @C.no_state_no_foreach
605
+ def scale_by_learning_rate(group: dict, update: torch.Tensor, grad: torch.Tensor, param: torch.Tensor) -> torch.Tensor:
606
+ return update * group["lr"]
607
+ ```
608
+
609
+ ## `FunctionTransform` and Guards
610
+
611
+ To make it easier to create gradient transformations, `heavyball.chainable` provides the `FunctionTransform` class and a
612
+ set of "guard" decorators.
613
+
614
+ ### `FunctionTransform`
615
+
616
+ `FunctionTransform` is a base class for gradient transformations that provides a common interface and helper methods. It
617
+ takes a function `fn` as input and stores it along with its name.
618
+
619
+ ```python
620
+ class FunctionTransform:
621
+ def __init__(self, fn):
622
+ self.fn = fn
623
+ self.fn_name = self.get_fn().__name__
624
+
625
+ def __call__(self, state, group, update, grad, param, *args, **kwargs):
626
+ raise NotImplementedError
627
+
628
+ def get_fn(self):
629
+ if hasattr(self.fn, 'get_fn'):
630
+ return self.fn.get_fn()
631
+ return self.fn
632
+
633
+ def val_name(self, name):
634
+ return f"{self.fn_name}_{name}"
635
+ ```
636
+
637
+ ### Guards
638
+
639
+ Guards are decorators that help manage the state dictionary and ensure that transformations are applied correctly. They
640
+ handle common tasks like initializing state variables and preventing redundant computations.
641
+
642
+ #### `zero_guard`
643
+
644
+ The `zero_guard` decorator ensures that a specific variable in the state dictionary is initialized to zero if it doesn't
645
+ exist.
646
+
647
+ ```python
648
+ @C.zero_guard("momentum")
649
+ def my_transformation(state, group, update, grad, param, momentum):
650
+ # ... momentum will be initialized to zero if it doesn't exist in state ...
651
+ return update
652
+ ```
653
+
654
+ #### `copy_guard`
655
+
656
+ The `copy_guard` decorator creates a copy of a specified input (update, grad, or param) and stores it in the state
657
+ dictionary.
658
+
659
+ ```python
660
+ @C.copy_guard(0, "update_copy") # 0 refers to the 'update' argument
661
+ def my_transformation(state, group, update, grad, param, update_copy):
662
+ # ... update_copy will be a copy of the update tensor ...
663
+ return update
664
+ ```
665
+
666
+ #### `general_guard`
667
+
668
+ The `general_guard` decorator provides a more flexible way to manage state. It allows you to specify a custom
669
+ initialization function that is called if a specific variable is not found in the state.
670
+
671
+ ```python
672
+ def init_preconditioner(state, group, update, grad, param, **kwargs):
673
+
674
+
675
+ # ... initialize preconditioner ...
676
+
677
+ @C.general_guard("precond", init_fn=init_preconditioner)
678
+ def my_transformation(state, group, update, grad, param, precond):
679
+ # ... precond will be initialized using init_preconditioner if it doesn't exist ...
680
+ return update
681
+ ```
682
+
683
+ #### `no_state`
684
+
685
+ The `no_state` decorator indicates that a transformation does not use or modify any state.
686
+
687
+ #### `no_state_no_foreach`
688
+
689
+ The `no_state_no_foreach` decorator indicates that a transformation does not use or modify any state and also does not
690
+ support `foreach` implementations.
691
+
692
+ ## Chaining Transformations
693
+
694
+ The power of `heavyball.chainable` comes from its ability to chain transformations together. This is achieved through
695
+ the `chain` function.
696
+
697
+ ```python
698
+ def chain(state: Union[callable, dict], group, grad, param, *fns):
699
+ update = [torch.clone(g, memory_format=torch.preserve_format) for g in grad]
700
+ skip_update = False
701
+ for fn in fns:
702
+ try:
703
+ update = fn(state, group, update, grad, param)
704
+ except SkipUpdate:
705
+ skip_update = True
706
+ continue
707
+ if update is None:
708
+ break
709
+ if not skip_update and update is not None:
710
+ utils.update_param_(param, update, group['lr'], group['weight_decay'], caution=group['caution'], grad=grad)
711
+ ```
712
+
713
+ The `chain` function takes a state dictionary, a group dictionary, a gradient tensor, a parameter tensor, and a sequence
714
+ of gradient transformations as input. It applies each transformation in order, passing the output of one transformation
715
+ as the input to the next.
716
+
717
+ ## Building Optimizers
718
+
719
+ The `ChainOpt` class provides a convenient way to build optimizers from chained transformations.
720
+
721
+ ```python
722
+ class ChainOpt(utils.StatefulOptimizer):
723
+ # ...
724
+ def __init__(self, params, defaults, foreach: bool, *fns):
725
+ # ...
726
+ self.fns = tuple(fns)
727
+
728
+ def _step(self, group):
729
+ # ...
730
+ if not group['foreach'] or len(p) == 1:
731
+ for param, grad in zip(p, g):
732
+ chain(self.state_, group, [grad], [param], *self.fns)
733
+ else:
734
+ chain(self.state_, group, g, p, *self.fns)
735
+ # ...
736
+ ```
737
+
738
+ ### BaseOpt
739
+
740
+ The `BaseOpt` class extends `ChainOpt` and provides additional features like gradient clipping, update clipping, and
741
+ optional PaLM beta2 schedule.
742
+
743
+ ```python
744
+ class BaseOpt(ChainOpt):
745
+ # ...
746
+ def __init__(self, params, defaults, foreach: bool, gradient_clipping: str_or_fn, update_clipping: str_or_fn,
747
+ palm: bool = use_default, *fns, compile_step: bool = use_default, promote: bool = use_default):
748
+ # ...
749
+ ```
750
+
751
+ ### `ScheduleFree`
752
+
753
+ The `ScheduleFree` class provides a convenient interface for using the `update_by_schedule_free` transformation.
754
+
755
+ ### Predefined Transformations
756
+
757
+ `heavyball.chainable` provides a number of predefined gradient transformations, including:
758
+
759
+ * `exp_avg`: Calculates the exponential moving average of the gradients.
760
+ * `scale_by_exp_avg_sq`: Scales the updates by the inverse square root of the exponential moving average of squared
761
+ gradients.
762
+ * `scale_by_adam`: Scales the updates using the Adam algorithm.
763
+ * `update_by_adam`: Updates the parameters using the Adam algorithm.
764
+ * `scale_by_laprop`: Scales the updates using the LaProp algorithm.
765
+ * `update_by_laprop`: Updates the parameters using the LaProp algorithm.
766
+ * `update_by_schedule_free`: Updates the parameters using the Schedule-Free algorithm.
767
+ * `update_by_adopt`: Updates the parameters using the ADOPT algorithm.
768
+ * `scale_by_adopt`: Scales the updates using the ADOPT algorithm.
769
+ * `orthogonalize_update`: Orthogonalizes the update tensor.
770
+ * `nesterov_momentum`: Applies Nesterov momentum to the updates.
771
+ * `heavyball_momentum`: Applies heavy-ball momentum to the updates.
772
+ * `scale_by_soap`: Scales the updates using the SOAP preconditioner.
773
+ * `scale_by_psgd`: Scales the updates using the PSGD preconditioner.
774
+ * `scale_by_delayed_psgd`: Scales the updates using the delayed PSGD preconditioner.
775
+ * `update_by_psgd`: Updates the parameters using the PSGD preconditioner.
776
+ * `update_by_delayed_psgd`: Updates the parameters using the delayed PSGD preconditioner.
777
+ * `palm_beta2`: Modifies the beta2 parameter for PaLM optimizers.
778
+
779
+ ## Creating New Transformations
780
+
781
+ You can easily create new gradient transformations by following the function signature and using the provided guards and
782
+ `FunctionTransform` class.
783
+
784
+ ### Example: Clipping gradients by norm
785
+
786
+ ```python
787
+ from heavyball import chainable as C
788
+ from heavyball import utils
789
+
790
+
791
+ @C.no_state
792
+ def clip_by_global_norm(group: dict, update: torch.Tensor, grad: torch.Tensor, param: torch.Tensor,
793
+ max_norm: float) -> torch.Tensor:
794
+ """Clips the gradient by its global norm."""
795
+ total_norm = torch.norm(torch.stack([torch.norm(g) for g in grad]))
796
+ clip_coef = max_norm / (total_norm + 1e-6)
797
+ if clip_coef < 1:
798
+ return [u * clip_coef for u in update]
799
+ return update
800
+ ```
801
+
802
+ ### Example: L2-Normalization of updates
803
+
804
+ ```python
805
+ from heavyball import chainable as C
806
+ from heavyball import utils
807
+
808
+
809
+ @C.no_state_no_foreach
810
+ def l2_normalize_updates(group: dict, update: torch.Tensor, grad: torch.Tensor, param: torch.Tensor) -> torch.Tensor:
811
+ """L2-normalizes the updates."""
812
+ norm = update.norm()
813
+ if norm > 0:
814
+ return update / norm
815
+ return update
816
+ ```
817
+
818
+ ---
819
+
820
+ ## Optimizer Recommendations
821
+
822
+ This hierarchy ranks optimizers from most recommended (top) to least recommended (bottom) for general deep learning
823
+ tasks. However, the best choice always depends on your specific model, dataset, and computational resources.
824
+
825
+ **1. Preconditioned Optimizers (SOAP and PSGD):**
826
+
827
+ - **Recommendation:** **Start here.** These are generally the most powerful and efficient optimizers in `heavyball`.
828
+ - **`ForeachSOAP`** (and its variants: `PaLMForeachSOAP`, `PrecondScheduleForeachSOAP`,
829
+ `PrecondSchedulePaLMForeachSOAP`):
830
+ - **Strengths:**
831
+ - **Adaptive Preconditioning:** SOAP dynamically adapts to the curvature of the loss landscape using
832
+ second-order information, leading to faster convergence, especially in ill-conditioned problems.
833
+ - **Robustness:** Less sensitive to hyperparameter choices compared to Adam.
834
+ - **Strong Empirical Performance:** Often outperforms other optimizers across various tasks and architectures.
835
+ - **Weaknesses:**
836
+ - **Computational Cost:** Higher per-step cost due to preconditioner computation and updates.
837
+ - **Memory Usage:** Can use more memory than simpler optimizers, particularly for large models.
838
+ - **`precondition_frequency` or `precond_scheduler`:** Needs to be tuned, though the default schedule usually
839
+ works well.
840
+ - **When to use:**
841
+ - **Complex models and datasets:** Where optimization is challenging.
842
+ - **When training stability is crucial.**
843
+ - **When you can't retune hyperparameters.**
844
+ - **Variants:**
845
+ - `PaLMForeachSOAP`: Enables PaLM's beta2 schedule by default.
846
+ - `PrecondScheduleForeachSOAP`: Uses a dynamic schedule for preconditioner updates.
847
+ - `PrecondSchedulePaLMForeachSOAP`: Combines the PaLM schedule with a dynamic preconditioner schedule.
848
+
849
+ - **`ForeachPSGDKron`** (and its variants: `ForeachPurePSGD`, `ForeachCachedDelayedPSGDKron`, `ForeachCachedPSGDKron`,
850
+ `ForeachDelayedPSGD`):
851
+ - **Strengths:**
852
+ - **Preconditioning:** Uses Kronecker-factored approximations to capture curvature information, providing many
853
+ of the benefits of second-order methods at a lower cost than full curvature methods.
854
+ - **Efficiency:** Relatively efficient in terms of computation.
855
+ - **Tunability:** Offers many options for customization.
856
+ - **Convergence:** Tends to converge faster than SOAP.
857
+ - **Weaknesses:**
858
+ - **No baseline:** SOAP can copy Adam's hyperparameters - PSGD requires more tuning.
859
+ - **Complexity:** Has many hyperparameters to tune.
860
+ - **When to use:**
861
+ - **Large models:** Where memory is a constraint.
862
+ - **When `ForeachSOAP` is too computationally expensive.**
863
+ - **When you want potentially the best performance regardless of computational cost.**
864
+ - **Variants:**
865
+ - `ForeachPurePSGD`: Disables exponential averaging of the input when calculating the preconditioner.
866
+ - `ForeachCachedDelayedPSGDKron`: Caches preconditioner-related computations and uses delayed preconditioner
867
+ updates.
868
+ - `ForeachCachedPSGDKron`: Caches preconditioner-related computations.
869
+ - `ForeachDelayedPSGD`: Uses delayed preconditioner updates.
870
+
871
+ **2. Muon:**
872
+
873
+ - **`ForeachMuon`** (and `MuonLaProp`):
874
+ - **Strengths:**
875
+ - **Momentum with Orthogonal Updates:** Combines momentum with orthogonalized updates, which can
876
+ improve stability and exploration.
877
+ - **Good Generalization:** Often leads to better generalization performance compared to Adam.
878
+ - **Weaknesses:**
879
+ - **Performance:** Generally outperformed by SOAP and PSGD.
880
+ - **Computational Cost:** Higher overheads than SOAP and PSGD.
881
+ - **When to use:**
882
+ - **When generalization is a primary concern.**
883
+ - **When you want an optimizer less prone to finding sharp minima.**
884
+
885
+ **3. Adam-Based Optimizers:**
886
+
887
+ - **`ForeachLaProp`**:
888
+ - **Strengths:**
889
+ - **Backward Compatibility:** Can use Adam's hyperparameters, but allows a larger range of betas.
890
+ - **Stability:** More stable than Adam.
891
+ - **Weaknesses:**
892
+ - **Performance:** Generally outperformed by SOAP, PSGD, and Muon.
893
+ - **When to use:**
894
+ - **When you want less risk or better losses than Adam, but can't run advanced methods.**
895
+
896
+ - **`ForeachAdamW`** (and `ForeachSFAdamW`, `PaLMForeachSFAdamW`):
897
+ - **Strengths:**
898
+ - **Widely Used:** A popular and well-established optimizer.
899
+ - **Weaknesses:**
900
+ - **Performance:** Often outperformed by preconditioned optimizers (SOAP, PSGD) and Muon.
901
+ - **Sensitivity to Hyperparameters:** Can be sensitive to the choice of learning rate and beta parameters.
902
+ - **When to use:**
903
+ - **As a strong baseline.**
904
+ - **When you are familiar with Adam and want a robust starting point.**
905
+ - **When computational cost is a major concern (compared to second-order methods).**
906
+ - **Variants:**
907
+ - `ForeachSFAdamW`: A Schedule-Free version of AdamW that dynamically adjusts the learning rate.
908
+ - `PaLMForeachSFAdamW`: A PaLM version of Schedule-Free AdamW.
909
+
910
+ ## Choosing the Right Optimizer
911
+
912
+ 1. **Start with Preconditioning:** Begin with either `ForeachSOAP` or `ForeachPSGDKron`. If computational resources are
913
+ a major constraint, lean towards `ForeachPSGDKron`. If performance is paramount, try `ForeachSOAP` first.
914
+
915
+ 2. **Consider Muon:** If preconditioned optimizers are not feasible or if you want to explore alternatives that
916
+ incorporate momentum and orthogonal updates, try `ForeachMuon`.
917
+
918
+ 3. **Use LaProp or Adam as Baselines:** `ForeachLaProp` can serve as a simple adaptive baseline. `ForeachAdamW` is a
919
+ strong and widely used baseline that you should always compare against.
920
+
921
+ 4. **Experiment and Tune:** The best optimizer ultimately depends on your specific problem. It's crucial to experiment
922
+ with different optimizers and carefully tune their hyperparameters (especially the learning rate).
923
+
924
+ ## Important Notes
925
+
926
+ * **Learning Rate:** The learning rate is the most important hyperparameter. You'll likely need to adjust it when
927
+ switching between optimizers.
928
+ * **Warmup:** Consider using a learning rate warmup, especially for more complex optimizers like SOAP and PSGD.
929
+ * **Weight Decay:** Weight decay can improve generalization for many optimizers, especially AdamW.
930
+ * **`foreach`:** Use `foreach` versions of the optimizers when possible for better performance.
931
+ * **`heavyball.utils`:** Remember to utilize the settings and functions in `heavyball.utils` (e.g., `set_torch`,
932
+ `compile_mode`, `zeroth_power_mode`, clipping functions) to optimize performance and experiment with different
933
+ configurations.
934
+