adv-optm 1.2.dev14__tar.gz → 2.dev1__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.
Potentially problematic release.
This version of adv-optm might be problematic. Click here for more details.
- {adv_optm-1.2.dev14 → adv_optm-2.dev1}/PKG-INFO +20 -20
- {adv_optm-1.2.dev14 → adv_optm-2.dev1}/README.md +19 -19
- {adv_optm-1.2.dev14 → adv_optm-2.dev1}/adv_optm/__init__.py +1 -1
- {adv_optm-1.2.dev14 → adv_optm-2.dev1}/adv_optm/optim/AdamW_adv.py +85 -64
- {adv_optm-1.2.dev14 → adv_optm-2.dev1}/adv_optm/optim/Adopt_adv.py +114 -69
- {adv_optm-1.2.dev14 → adv_optm-2.dev1}/adv_optm/optim/Lion_Prodigy_adv.py +79 -81
- {adv_optm-1.2.dev14 → adv_optm-2.dev1}/adv_optm/optim/Lion_adv.py +37 -42
- {adv_optm-1.2.dev14 → adv_optm-2.dev1}/adv_optm/optim/Prodigy_adv.py +105 -85
- {adv_optm-1.2.dev14 → adv_optm-2.dev1}/adv_optm/optim/Simplified_AdEMAMix.py +92 -51
- {adv_optm-1.2.dev14 → adv_optm-2.dev1}/adv_optm/optim/__init__.py +1 -1
- {adv_optm-1.2.dev14 → adv_optm-2.dev1}/adv_optm/util/BF16_Stochastic_Rounding.py +1 -1
- {adv_optm-1.2.dev14 → adv_optm-2.dev1}/adv_optm/util/Effective_Shape.py +1 -1
- {adv_optm-1.2.dev14 → adv_optm-2.dev1}/adv_optm/util/Kourkoutas.py +10 -11
- {adv_optm-1.2.dev14 → adv_optm-2.dev1}/adv_optm/util/NNMF.py +7 -2
- {adv_optm-1.2.dev14 → adv_optm-2.dev1}/adv_optm/util/Newton_Schulz.py +1 -2
- {adv_optm-1.2.dev14 → adv_optm-2.dev1}/adv_optm/util/One_Bit_Boolean.py +1 -1
- {adv_optm-1.2.dev14 → adv_optm-2.dev1}/adv_optm/util/OrthoGrad.py +4 -3
- {adv_optm-1.2.dev14 → adv_optm-2.dev1}/adv_optm/util/__init__.py +1 -1
- {adv_optm-1.2.dev14 → adv_optm-2.dev1}/adv_optm.egg-info/PKG-INFO +20 -20
- {adv_optm-1.2.dev14 → adv_optm-2.dev1}/setup.py +1 -1
- {adv_optm-1.2.dev14 → adv_optm-2.dev1}/LICENSE +0 -0
- {adv_optm-1.2.dev14 → adv_optm-2.dev1}/adv_optm/optim/AdaMuon_adv.py +0 -0
- {adv_optm-1.2.dev14 → adv_optm-2.dev1}/adv_optm/optim/Muon_adv.py +0 -0
- {adv_optm-1.2.dev14 → adv_optm-2.dev1}/adv_optm.egg-info/SOURCES.txt +0 -0
- {adv_optm-1.2.dev14 → adv_optm-2.dev1}/adv_optm.egg-info/dependency_links.txt +0 -0
- {adv_optm-1.2.dev14 → adv_optm-2.dev1}/adv_optm.egg-info/requires.txt +0 -0
- {adv_optm-1.2.dev14 → adv_optm-2.dev1}/adv_optm.egg-info/top_level.txt +0 -0
- {adv_optm-1.2.dev14 → adv_optm-2.dev1}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: adv_optm
|
|
3
|
-
Version:
|
|
3
|
+
Version: 2.dev1
|
|
4
4
|
Summary: A family of highly efficient, lightweight yet powerful optimizers.
|
|
5
5
|
Home-page: https://github.com/Koratahiu/Advanced_Optimizers
|
|
6
6
|
Author: Koratahiu
|
|
@@ -52,7 +52,7 @@ This library integrates multiple state-of-the-art optimization techniques valida
|
|
|
52
52
|
### **Memory-Efficient Optimization (SMMF-inspired)**
|
|
53
53
|
- **Paper**: [SMMF: Square-Matricized Momentum Factorization](https://arxiv.org/abs/2412.08894)
|
|
54
54
|
- **Approach**: Uses rank-1 non-negative matrix factorization with reconstruction cycle (factor → reconstruct → update → factor)
|
|
55
|
-
- **Innovation**:
|
|
55
|
+
- **Innovation**:
|
|
56
56
|
- First moment split into **1-bit sign + absolute value**
|
|
57
57
|
- Final storage: **four factored vectors + one 1-bit sign state**
|
|
58
58
|
- Preserves Adam-like update quality with drastically reduced memory
|
|
@@ -110,7 +110,7 @@ This library integrates multiple state-of-the-art optimization techniques valida
|
|
|
110
110
|
|
|
111
111
|
## 🛠️ Comprehensive Feature Guide
|
|
112
112
|
|
|
113
|
-
### A. Universal Safe Features
|
|
113
|
+
### A. Universal Safe Features
|
|
114
114
|
*These features work with all optimizers and are generally safe to enable.*
|
|
115
115
|
|
|
116
116
|
| Feature | Description | Recommended Usage | Performance Impact | Theoretical Basis | Compatibility |
|
|
@@ -165,7 +165,7 @@ This library integrates multiple state-of-the-art optimization techniques valida
|
|
|
165
165
|
| `beta1` | 0.99 | Controls accumulator memory length:<br>• Small BS: **0.99–0.9999**<br>• Large BS: **0.9** |
|
|
166
166
|
| `Grad α` | 100 | Most critical parameter:<br>• Inversely scales with batch size<br>• **100–10** for small BS (≤32)<br>• **1–0.1** for large BS (≥512) |
|
|
167
167
|
|
|
168
|
-
> ⚠️ **Critical**: Requires **~100x smaller learning rate** than AdamW (e.g., 1e-6 vs 1e-4).
|
|
168
|
+
> ⚠️ **Critical**: Requires **~100x smaller learning rate** than AdamW (e.g., 1e-6 vs 1e-4).
|
|
169
169
|
> For `Prodigy_Adv`, set `initial_d` to:
|
|
170
170
|
> - **LoRA**: `1e-8`
|
|
171
171
|
> - **Full FT**: `1e-10`
|
|
@@ -175,10 +175,10 @@ This library integrates multiple state-of-the-art optimization techniques valida
|
|
|
175
175
|
|
|
176
176
|
#### Performance Validation
|
|
177
177
|
|
|
178
|
-
**Small Batch Training (SDXL, BS=2, 1.8K steps)**
|
|
178
|
+
**Small Batch Training (SDXL, BS=2, 1.8K steps)**
|
|
179
179
|

|
|
180
180
|
|
|
181
|
-
- **🟢 Prodigy_Adv** (beta1=0.9, d0=1e-5): Final LR = 2.9e-4
|
|
181
|
+
- **🟢 Prodigy_Adv** (beta1=0.9, d0=1e-5): Final LR = 2.9e-4
|
|
182
182
|
- **🔵 Prodigy_Adv + Simplified_AdEMAMix** (beta1=0.99, α=100, d0=1e-7): Final LR = 5.8e-6
|
|
183
183
|
|
|
184
184
|
**Results**:
|
|
@@ -202,8 +202,8 @@ This library integrates multiple state-of-the-art optimization techniques valida
|
|
|
202
202
|
|
|
203
203
|
Instead of using a fixed β₂ (e.g., 0.999 or 0.95), it **dynamically modulates β₂ per layer** based on a bounded *sunspike ratio*:
|
|
204
204
|
|
|
205
|
-
- **During gradient bursts** → β₂ ↓ toward `Lower β₂` → faster reaction
|
|
206
|
-
- **During calm phases** → β₂ ↑ toward `The Selected β₂` → stronger smoothing
|
|
205
|
+
- **During gradient bursts** → β₂ ↓ toward `Lower β₂` → faster reaction
|
|
206
|
+
- **During calm phases** → β₂ ↑ toward `The Selected β₂` → stronger smoothing
|
|
207
207
|
|
|
208
208
|
This is especially effective for **noisy training, small batch sizes, and high learning rates**, where gradient norms shift abruptly due to noise or aggressive LR schedules.
|
|
209
209
|
|
|
@@ -220,17 +220,17 @@ This is especially effective for **noisy training, small batch sizes, and high l
|
|
|
220
220
|
|
|
221
221
|
#### 📊 Performance Validation
|
|
222
222
|
|
|
223
|
-
**ADAMW_ADV - full SDXL finetuning (aggressive LR: 3e-5) (BS=4, 2.5K steps)**
|
|
223
|
+
**ADAMW_ADV - full SDXL finetuning (aggressive LR: 3e-5) (BS=4, 2.5K steps)**
|
|
224
224
|
<img width="1460" height="382" alt="image" src="https://github.com/user-attachments/assets/007f278a-fbac-4f3d-9cc7-274c3b959cdd" />
|
|
225
225
|
|
|
226
|
-
- 🟣 Fixed `beta2=0.999`
|
|
227
|
-
- 🟠 Auto K-beta
|
|
226
|
+
- 🟣 Fixed `beta2=0.999`
|
|
227
|
+
- 🟠 Auto K-beta
|
|
228
228
|
|
|
229
|
-
**Observations:**
|
|
229
|
+
**Observations:**
|
|
230
230
|
- K-beta is clearly better and more robust/stable for high LRs.
|
|
231
231
|
|
|
232
|
-
> 📚 **Reference**:
|
|
233
|
-
> - Paper: [Kourkoutas-β: A Sunspike-Driven Adam Optimizer with Desert Flair](https://arxiv.org/abs/2508.12996)
|
|
232
|
+
> 📚 **Reference**:
|
|
233
|
+
> - Paper: [Kourkoutas-β: A Sunspike-Driven Adam Optimizer with Desert Flair](https://arxiv.org/abs/2508.12996)
|
|
234
234
|
> - Code: [kbeta](https://github.com/sck-at-ucy/kbeta)
|
|
235
235
|
|
|
236
236
|
---
|
|
@@ -258,7 +258,7 @@ settings:
|
|
|
258
258
|
- factored: False # Can be true or false, quality should not degrade due to Simplified_AdEMAMix’s high tolerance to 1-bit factorization.
|
|
259
259
|
```
|
|
260
260
|
|
|
261
|
-
> ✅ **Why it works**:
|
|
261
|
+
> ✅ **Why it works**:
|
|
262
262
|
> - `Kourkoutas-β` handles beta2 values
|
|
263
263
|
> - `Simplified_AdEMAMix` ensures responsiveness in small-batch noise
|
|
264
264
|
> - `OrthoGrad` prevents overfitting without weight decay
|
|
@@ -267,9 +267,9 @@ settings:
|
|
|
267
267
|
|
|
268
268
|
## 📚 References
|
|
269
269
|
|
|
270
|
-
1. [Revisiting BFloat16 Training](https://arxiv.org/abs/2010.06192)
|
|
271
|
-
2. [SMMF: Square-Matricized Momentum Factorization](https://arxiv.org/abs/2412.08894)
|
|
272
|
-
3. [The AdEMAMix Optimizer](https://arxiv.org/abs/2409.03137)
|
|
273
|
-
4. [Connections between Schedule-Free Optimizers, AdEMAMix, and Accelerated SGD](https://arxiv.org/abs/2502.02431)
|
|
274
|
-
5. [AdaMeM: Memory Efficient Momentum for Adafactor](https://openreview.net/forum?id=fZqMVTz7K5)
|
|
270
|
+
1. [Revisiting BFloat16 Training](https://arxiv.org/abs/2010.06192)
|
|
271
|
+
2. [SMMF: Square-Matricized Momentum Factorization](https://arxiv.org/abs/2412.08894)
|
|
272
|
+
3. [The AdEMAMix Optimizer](https://arxiv.org/abs/2409.03137)
|
|
273
|
+
4. [Connections between Schedule-Free Optimizers, AdEMAMix, and Accelerated SGD](https://arxiv.org/abs/2502.02431)
|
|
274
|
+
5. [AdaMeM: Memory Efficient Momentum for Adafactor](https://openreview.net/forum?id=fZqMVTz7K5)
|
|
275
275
|
6. [Kourkoutas-β: A Sunspike-Driven Adam Optimizer with Desert Flair](https://arxiv.org/abs/2508.12996)
|
|
@@ -21,7 +21,7 @@ This library integrates multiple state-of-the-art optimization techniques valida
|
|
|
21
21
|
### **Memory-Efficient Optimization (SMMF-inspired)**
|
|
22
22
|
- **Paper**: [SMMF: Square-Matricized Momentum Factorization](https://arxiv.org/abs/2412.08894)
|
|
23
23
|
- **Approach**: Uses rank-1 non-negative matrix factorization with reconstruction cycle (factor → reconstruct → update → factor)
|
|
24
|
-
- **Innovation**:
|
|
24
|
+
- **Innovation**:
|
|
25
25
|
- First moment split into **1-bit sign + absolute value**
|
|
26
26
|
- Final storage: **four factored vectors + one 1-bit sign state**
|
|
27
27
|
- Preserves Adam-like update quality with drastically reduced memory
|
|
@@ -79,7 +79,7 @@ This library integrates multiple state-of-the-art optimization techniques valida
|
|
|
79
79
|
|
|
80
80
|
## 🛠️ Comprehensive Feature Guide
|
|
81
81
|
|
|
82
|
-
### A. Universal Safe Features
|
|
82
|
+
### A. Universal Safe Features
|
|
83
83
|
*These features work with all optimizers and are generally safe to enable.*
|
|
84
84
|
|
|
85
85
|
| Feature | Description | Recommended Usage | Performance Impact | Theoretical Basis | Compatibility |
|
|
@@ -134,7 +134,7 @@ This library integrates multiple state-of-the-art optimization techniques valida
|
|
|
134
134
|
| `beta1` | 0.99 | Controls accumulator memory length:<br>• Small BS: **0.99–0.9999**<br>• Large BS: **0.9** |
|
|
135
135
|
| `Grad α` | 100 | Most critical parameter:<br>• Inversely scales with batch size<br>• **100–10** for small BS (≤32)<br>• **1–0.1** for large BS (≥512) |
|
|
136
136
|
|
|
137
|
-
> ⚠️ **Critical**: Requires **~100x smaller learning rate** than AdamW (e.g., 1e-6 vs 1e-4).
|
|
137
|
+
> ⚠️ **Critical**: Requires **~100x smaller learning rate** than AdamW (e.g., 1e-6 vs 1e-4).
|
|
138
138
|
> For `Prodigy_Adv`, set `initial_d` to:
|
|
139
139
|
> - **LoRA**: `1e-8`
|
|
140
140
|
> - **Full FT**: `1e-10`
|
|
@@ -144,10 +144,10 @@ This library integrates multiple state-of-the-art optimization techniques valida
|
|
|
144
144
|
|
|
145
145
|
#### Performance Validation
|
|
146
146
|
|
|
147
|
-
**Small Batch Training (SDXL, BS=2, 1.8K steps)**
|
|
147
|
+
**Small Batch Training (SDXL, BS=2, 1.8K steps)**
|
|
148
148
|

|
|
149
149
|
|
|
150
|
-
- **🟢 Prodigy_Adv** (beta1=0.9, d0=1e-5): Final LR = 2.9e-4
|
|
150
|
+
- **🟢 Prodigy_Adv** (beta1=0.9, d0=1e-5): Final LR = 2.9e-4
|
|
151
151
|
- **🔵 Prodigy_Adv + Simplified_AdEMAMix** (beta1=0.99, α=100, d0=1e-7): Final LR = 5.8e-6
|
|
152
152
|
|
|
153
153
|
**Results**:
|
|
@@ -171,8 +171,8 @@ This library integrates multiple state-of-the-art optimization techniques valida
|
|
|
171
171
|
|
|
172
172
|
Instead of using a fixed β₂ (e.g., 0.999 or 0.95), it **dynamically modulates β₂ per layer** based on a bounded *sunspike ratio*:
|
|
173
173
|
|
|
174
|
-
- **During gradient bursts** → β₂ ↓ toward `Lower β₂` → faster reaction
|
|
175
|
-
- **During calm phases** → β₂ ↑ toward `The Selected β₂` → stronger smoothing
|
|
174
|
+
- **During gradient bursts** → β₂ ↓ toward `Lower β₂` → faster reaction
|
|
175
|
+
- **During calm phases** → β₂ ↑ toward `The Selected β₂` → stronger smoothing
|
|
176
176
|
|
|
177
177
|
This is especially effective for **noisy training, small batch sizes, and high learning rates**, where gradient norms shift abruptly due to noise or aggressive LR schedules.
|
|
178
178
|
|
|
@@ -189,17 +189,17 @@ This is especially effective for **noisy training, small batch sizes, and high l
|
|
|
189
189
|
|
|
190
190
|
#### 📊 Performance Validation
|
|
191
191
|
|
|
192
|
-
**ADAMW_ADV - full SDXL finetuning (aggressive LR: 3e-5) (BS=4, 2.5K steps)**
|
|
192
|
+
**ADAMW_ADV - full SDXL finetuning (aggressive LR: 3e-5) (BS=4, 2.5K steps)**
|
|
193
193
|
<img width="1460" height="382" alt="image" src="https://github.com/user-attachments/assets/007f278a-fbac-4f3d-9cc7-274c3b959cdd" />
|
|
194
194
|
|
|
195
|
-
- 🟣 Fixed `beta2=0.999`
|
|
196
|
-
- 🟠 Auto K-beta
|
|
195
|
+
- 🟣 Fixed `beta2=0.999`
|
|
196
|
+
- 🟠 Auto K-beta
|
|
197
197
|
|
|
198
|
-
**Observations:**
|
|
198
|
+
**Observations:**
|
|
199
199
|
- K-beta is clearly better and more robust/stable for high LRs.
|
|
200
200
|
|
|
201
|
-
> 📚 **Reference**:
|
|
202
|
-
> - Paper: [Kourkoutas-β: A Sunspike-Driven Adam Optimizer with Desert Flair](https://arxiv.org/abs/2508.12996)
|
|
201
|
+
> 📚 **Reference**:
|
|
202
|
+
> - Paper: [Kourkoutas-β: A Sunspike-Driven Adam Optimizer with Desert Flair](https://arxiv.org/abs/2508.12996)
|
|
203
203
|
> - Code: [kbeta](https://github.com/sck-at-ucy/kbeta)
|
|
204
204
|
|
|
205
205
|
---
|
|
@@ -227,7 +227,7 @@ settings:
|
|
|
227
227
|
- factored: False # Can be true or false, quality should not degrade due to Simplified_AdEMAMix’s high tolerance to 1-bit factorization.
|
|
228
228
|
```
|
|
229
229
|
|
|
230
|
-
> ✅ **Why it works**:
|
|
230
|
+
> ✅ **Why it works**:
|
|
231
231
|
> - `Kourkoutas-β` handles beta2 values
|
|
232
232
|
> - `Simplified_AdEMAMix` ensures responsiveness in small-batch noise
|
|
233
233
|
> - `OrthoGrad` prevents overfitting without weight decay
|
|
@@ -236,9 +236,9 @@ settings:
|
|
|
236
236
|
|
|
237
237
|
## 📚 References
|
|
238
238
|
|
|
239
|
-
1. [Revisiting BFloat16 Training](https://arxiv.org/abs/2010.06192)
|
|
240
|
-
2. [SMMF: Square-Matricized Momentum Factorization](https://arxiv.org/abs/2412.08894)
|
|
241
|
-
3. [The AdEMAMix Optimizer](https://arxiv.org/abs/2409.03137)
|
|
242
|
-
4. [Connections between Schedule-Free Optimizers, AdEMAMix, and Accelerated SGD](https://arxiv.org/abs/2502.02431)
|
|
243
|
-
5. [AdaMeM: Memory Efficient Momentum for Adafactor](https://openreview.net/forum?id=fZqMVTz7K5)
|
|
239
|
+
1. [Revisiting BFloat16 Training](https://arxiv.org/abs/2010.06192)
|
|
240
|
+
2. [SMMF: Square-Matricized Momentum Factorization](https://arxiv.org/abs/2412.08894)
|
|
241
|
+
3. [The AdEMAMix Optimizer](https://arxiv.org/abs/2409.03137)
|
|
242
|
+
4. [Connections between Schedule-Free Optimizers, AdEMAMix, and Accelerated SGD](https://arxiv.org/abs/2502.02431)
|
|
243
|
+
5. [AdaMeM: Memory Efficient Momentum for Adafactor](https://openreview.net/forum?id=fZqMVTz7K5)
|
|
244
244
|
6. [Kourkoutas-β: A Sunspike-Driven Adam Optimizer with Desert Flair](https://arxiv.org/abs/2508.12996)
|
|
@@ -49,12 +49,6 @@ class AdamW_adv(torch.optim.Optimizer):
|
|
|
49
49
|
before it is added to the fast momentum term (`update = mt + alpha * mt_slow`).
|
|
50
50
|
A higher value increases the stabilizing influence of the slow
|
|
51
51
|
momentum. (default: 5.0)
|
|
52
|
-
t_alpha (Optional[int]): The number of steps for a linear warmup of the
|
|
53
|
-
`alpha` parameter (only used when `use_AdEMAMix` is `True`). This is
|
|
54
|
-
highly recommended to prevent instability at the beginning of training,
|
|
55
|
-
as it gradually introduces the stabilizing slow momentum term. During
|
|
56
|
-
the warmup, `alpha` ramps from 0 to its target value. If `None`,
|
|
57
|
-
the scheduler is disabled. (default: None)
|
|
58
52
|
kourkoutas_beta (bool): whether to enable the layer-wise dynamic β₂ logic.
|
|
59
53
|
If `False`, the optimizer behaves as standard AdamW. (default: False)
|
|
60
54
|
beta2_min (float): The minimum value for dynamic β₂, used during periods of
|
|
@@ -72,11 +66,7 @@ class AdamW_adv(torch.optim.Optimizer):
|
|
|
72
66
|
k_logging (int): if > 0 and kourkoutas_beta=True, enables periodic console
|
|
73
67
|
logging of Kourkoutas-β statistics (min, max, mean of `β₂` across layers)
|
|
74
68
|
every logging steps. Useful for debugging and tuning. Set to 0 to disable
|
|
75
|
-
logging (default: 0).
|
|
76
|
-
layer_key_fn (Optional[Callable]): A function that takes a parameter `p`
|
|
77
|
-
and returns a unique, hashable key representing its "layer" or "bucket".
|
|
78
|
-
If `None`, parameters are bucketed by their memory ID (tensor-wise).
|
|
79
|
-
(default: None)
|
|
69
|
+
logging (default: 0).
|
|
80
70
|
nnmf_factor (bool): whether to use the factorization or disable it to use
|
|
81
71
|
the uncompressed optimizer. (default: False)
|
|
82
72
|
"""
|
|
@@ -89,7 +79,7 @@ class AdamW_adv(torch.optim.Optimizer):
|
|
|
89
79
|
eps: float = 1e-8,
|
|
90
80
|
weight_decay: float = 0.0,
|
|
91
81
|
use_bias_correction: bool = True,
|
|
92
|
-
vector_reshape: bool =
|
|
82
|
+
vector_reshape: bool = False,
|
|
93
83
|
stochastic_rounding: bool = True,
|
|
94
84
|
use_atan2: bool = False,
|
|
95
85
|
cautious_mask: bool = False,
|
|
@@ -98,15 +88,15 @@ class AdamW_adv(torch.optim.Optimizer):
|
|
|
98
88
|
use_AdEMAMix: bool = False,
|
|
99
89
|
beta3_ema: float = 0.9999,
|
|
100
90
|
alpha: float = 5.0,
|
|
101
|
-
t_alpha: int | None = None,
|
|
102
91
|
kourkoutas_beta: bool = False,
|
|
103
92
|
beta2_min: float = 0.9,
|
|
104
93
|
ema_alpha: float = 0.95,
|
|
105
94
|
tiny_spike: float = 1e-9,
|
|
106
95
|
k_warmup_steps: int = 0,
|
|
107
96
|
k_logging: int = 0,
|
|
108
|
-
layer_key_fn: Optional[Callable] = None,
|
|
109
97
|
nnmf_factor: bool = False,
|
|
98
|
+
# Compiled
|
|
99
|
+
compiled_optimizer: bool = False,
|
|
110
100
|
):
|
|
111
101
|
if not (lr >= 0.0):
|
|
112
102
|
raise ValueError(f"Learning-rate should be >= 0.0. Got {lr}")
|
|
@@ -116,7 +106,8 @@ class AdamW_adv(torch.optim.Optimizer):
|
|
|
116
106
|
raise ValueError(f"Epsilon should be >= 0.0. Got {eps}")
|
|
117
107
|
if not (weight_decay >= 0.0):
|
|
118
108
|
raise ValueError(f"Weight-decay should be >= 0.0. Got {weight_decay}")
|
|
119
|
-
if kourkoutas_beta and not (betas[1] > beta2_min):
|
|
109
|
+
if kourkoutas_beta and not (betas[1] > beta2_min):
|
|
110
|
+
raise ValueError(f"For Kourkoutas-β, betas[1] (as beta2_max) must be > beta2_min. Got {betas[1]} and {beta2_min}")
|
|
120
111
|
|
|
121
112
|
if cautious_mask and grams_moment:
|
|
122
113
|
print("Warning: cautious is incompatible with grams, Disabling cautious.")
|
|
@@ -126,9 +117,10 @@ class AdamW_adv(torch.optim.Optimizer):
|
|
|
126
117
|
"lr": lr, "betas": betas, "eps": eps, "weight_decay": weight_decay,
|
|
127
118
|
"vector_reshape": vector_reshape, "use_atan2": use_atan2,
|
|
128
119
|
"orthogonal_gradient": orthogonal_gradient, "use_bias_correction": use_bias_correction,
|
|
129
|
-
"beta3_ema": beta3_ema, "alpha": alpha,
|
|
120
|
+
"beta3_ema": beta3_ema, "alpha": alpha,
|
|
130
121
|
"kourkoutas_beta": kourkoutas_beta, "beta2_min": beta2_min, "ema_alpha": ema_alpha,
|
|
131
122
|
"tiny_spike": tiny_spike, "k_warmup_steps": k_warmup_steps, "k_logging": k_logging,
|
|
123
|
+
"compiled_optimizer": compiled_optimizer,
|
|
132
124
|
}
|
|
133
125
|
self.stochastic_rounding = stochastic_rounding
|
|
134
126
|
self.cautious_mask = cautious_mask
|
|
@@ -136,12 +128,20 @@ class AdamW_adv(torch.optim.Optimizer):
|
|
|
136
128
|
self.use_AdEMAMix = use_AdEMAMix
|
|
137
129
|
self.factored = nnmf_factor
|
|
138
130
|
self.kourkoutas_beta = kourkoutas_beta
|
|
139
|
-
|
|
131
|
+
|
|
140
132
|
super().__init__(params, defaults)
|
|
141
133
|
|
|
134
|
+
self.init_step()
|
|
135
|
+
|
|
142
136
|
if self.kourkoutas_beta:
|
|
143
137
|
self.kourkoutas_helper = KourkoutasHelper(self)
|
|
144
138
|
|
|
139
|
+
self.global_step = 0
|
|
140
|
+
|
|
141
|
+
if compiled_optimizer:
|
|
142
|
+
torch._dynamo.config.cache_size_limit = 8192
|
|
143
|
+
self.compile(fullgraph=True)
|
|
144
|
+
|
|
145
145
|
@property
|
|
146
146
|
def supports_fused_back_pass(self):
|
|
147
147
|
return True
|
|
@@ -154,29 +154,22 @@ class AdamW_adv(torch.optim.Optimizer):
|
|
|
154
154
|
def supports_flat_params(self):
|
|
155
155
|
return False
|
|
156
156
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
157
|
+
def init_step(self):
|
|
158
|
+
for group in self.param_groups:
|
|
159
|
+
for p in group['params']:
|
|
160
|
+
self.__init_state(p, group)
|
|
161
161
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
grad = grad.float()
|
|
165
|
-
if group["orthogonal_gradient"]:
|
|
166
|
-
grad = _orthogonalize_gradient(p, grad)
|
|
162
|
+
@torch.no_grad()
|
|
163
|
+
def __init_state(self, p, group):
|
|
167
164
|
state = self.state[p]
|
|
168
165
|
|
|
169
|
-
|
|
170
|
-
if 'step' not in state:
|
|
171
|
-
state['step'] = 0
|
|
166
|
+
if len(state) == 0:
|
|
172
167
|
|
|
173
|
-
|
|
168
|
+
state['factored'] = (
|
|
174
169
|
self.factored and
|
|
175
170
|
not (len(p.shape) == 1 and not group['vector_reshape'])
|
|
176
171
|
)
|
|
177
172
|
|
|
178
|
-
state['factored'] = should_factor
|
|
179
|
-
|
|
180
173
|
dtype = torch.float32 if self.factored else p.dtype
|
|
181
174
|
device = p.device
|
|
182
175
|
|
|
@@ -186,18 +179,18 @@ class AdamW_adv(torch.optim.Optimizer):
|
|
|
186
179
|
|
|
187
180
|
# First moment (m)
|
|
188
181
|
if group['betas'][0] > 0:
|
|
189
|
-
state['mu_m_nmf'] = torch.zeros(d1, device=device, dtype=dtype)
|
|
182
|
+
state['mu_m_nmf'] = torch.zeros(d1, device=device, dtype=dtype)
|
|
190
183
|
state['mv_m_nmf'] = torch.zeros(d2, device=device, dtype=dtype)
|
|
191
184
|
if not self.grams_moment:
|
|
192
185
|
packed_d2 = (d2 + 7) // 8
|
|
193
186
|
state['sign'] = torch.zeros((d1, packed_d2), dtype=torch.uint8, device=device)
|
|
194
187
|
if self.use_AdEMAMix:
|
|
195
|
-
state['mu_m_slow_nmf'] = torch.zeros(d1, device=p.device, dtype=dtype)
|
|
188
|
+
state['mu_m_slow_nmf'] = torch.zeros(d1, device=p.device, dtype=dtype)
|
|
196
189
|
state['mv_m_slow_nmf'] = torch.zeros(d2, device=p.device, dtype=dtype)
|
|
197
190
|
packed_d2 = (d2 + 7) // 8
|
|
198
191
|
state['sign_slow'] = torch.zeros((d1, packed_d2), dtype=torch.uint8, device=p.device)
|
|
199
192
|
# Second moment (v)
|
|
200
|
-
state['mu_v_nmf'] = torch.zeros(d1, device=device, dtype=dtype)
|
|
193
|
+
state['mu_v_nmf'] = torch.zeros(d1, device=device, dtype=dtype)
|
|
201
194
|
state['mv_v_nmf'] = torch.zeros(d2, device=device, dtype=dtype)
|
|
202
195
|
else: # Fallback to standard AdamW for non-factored tensors
|
|
203
196
|
if group['betas'][0] > 0:
|
|
@@ -206,37 +199,32 @@ class AdamW_adv(torch.optim.Optimizer):
|
|
|
206
199
|
state['exp_avg_slow'] = torch.zeros_like(p, device=device, dtype=dtype)
|
|
207
200
|
state['exp_avg_sq'] = torch.zeros_like(p, device=device, dtype=dtype)
|
|
208
201
|
|
|
202
|
+
@torch.no_grad()
|
|
203
|
+
def __step_parameter(self, p: torch.Tensor, group: dict, lr: torch.Tensor | float, bias_correction1: torch.Tensor | float, bias_correction2: torch.Tensor | float):
|
|
204
|
+
if p.grad is None:
|
|
205
|
+
return
|
|
206
|
+
|
|
207
|
+
grad = p.grad
|
|
208
|
+
if grad.dtype != torch.float32 and self.factored:
|
|
209
|
+
grad = grad.float()
|
|
210
|
+
if group["orthogonal_gradient"]:
|
|
211
|
+
grad = _orthogonalize_gradient(p, grad)
|
|
212
|
+
state = self.state[p]
|
|
213
|
+
|
|
214
|
+
|
|
209
215
|
beta1, beta2 = group['betas']
|
|
210
216
|
|
|
211
|
-
current_step = state['step']
|
|
212
217
|
if group.get('kourkoutas_beta', False):
|
|
213
|
-
# Call prepare_step() once at the beginning of the step for all params
|
|
214
|
-
self.kourkoutas_helper.maybe_prepare_step(current_step)
|
|
215
218
|
# Accumulate current grad's norm for the *next* step
|
|
216
219
|
self.kourkoutas_helper.accumulate_gradient_sq_norm(p, grad)
|
|
217
220
|
# Get the dynamic beta2 calculated in prepare_step()
|
|
218
|
-
beta2 = self.kourkoutas_helper.get_beta2(p, group
|
|
221
|
+
beta2 = self.kourkoutas_helper.get_beta2(p, group)
|
|
219
222
|
|
|
220
|
-
|
|
221
|
-
if group['use_bias_correction']:
|
|
222
|
-
bias_correction1 = 1.0 - beta1 ** step
|
|
223
|
-
if group.get('kourkoutas_beta', False):
|
|
224
|
-
bias_correction2 = 1.0 - group['betas'][1] ** step
|
|
225
|
-
# Use beta2_max for bias correction
|
|
226
|
-
else:
|
|
227
|
-
bias_correction2 = 1.0 - beta2 ** step
|
|
228
|
-
else:
|
|
229
|
-
bias_correction1 = 1
|
|
230
|
-
bias_correction2 = 1
|
|
231
|
-
step_size = group['lr'] / bias_correction1
|
|
223
|
+
step_size = lr / bias_correction1
|
|
232
224
|
|
|
233
225
|
if self.use_AdEMAMix:
|
|
234
226
|
beta3_ema = group['beta3_ema']
|
|
235
227
|
alpha = group['alpha']
|
|
236
|
-
t_alpha = group['t_alpha']
|
|
237
|
-
alpha_t = alpha
|
|
238
|
-
if t_alpha is not None and t_alpha > 0 and step < t_alpha:
|
|
239
|
-
alpha_t = min(step * alpha / t_alpha, alpha)
|
|
240
228
|
|
|
241
229
|
if state['factored']:
|
|
242
230
|
d1, d2 = state['effective_shape']
|
|
@@ -252,7 +240,7 @@ class AdamW_adv(torch.optim.Optimizer):
|
|
|
252
240
|
# Update momentum in full-size
|
|
253
241
|
mt.mul_(beta1).add_(grad_reshaped, alpha=1.0 - beta1)
|
|
254
242
|
if self.grams_moment:
|
|
255
|
-
mt
|
|
243
|
+
mt = (grad_reshaped.sign().mul_(mt.abs()))
|
|
256
244
|
elif self.cautious_mask:
|
|
257
245
|
mask = (mt * grad_reshaped > 0).to(grad_reshaped.dtype)
|
|
258
246
|
mask.div_(mask.mean().clamp_(min=1e-3))
|
|
@@ -272,9 +260,9 @@ class AdamW_adv(torch.optim.Optimizer):
|
|
|
272
260
|
|
|
273
261
|
mt_slow.mul_(beta3_ema).add_(grad_reshaped, alpha=1.0 - beta3_ema)
|
|
274
262
|
if beta1 > 0:
|
|
275
|
-
update = torch.add(mt, mt_slow, alpha=
|
|
263
|
+
update = torch.add(mt, mt_slow, alpha=alpha)
|
|
276
264
|
else:
|
|
277
|
-
update = torch.add(grad_reshaped, mt_slow, alpha=
|
|
265
|
+
update = torch.add(grad_reshaped, mt_slow, alpha=alpha)
|
|
278
266
|
else:
|
|
279
267
|
update = mt.clone() if beta1 > 0 else grad_reshaped.clone()
|
|
280
268
|
del grad_reshaped
|
|
@@ -310,7 +298,7 @@ class AdamW_adv(torch.optim.Optimizer):
|
|
|
310
298
|
exp_avg = state['exp_avg']
|
|
311
299
|
exp_avg.mul_(beta1).add_(grad, alpha=1 - beta1)
|
|
312
300
|
if self.grams_moment:
|
|
313
|
-
exp_avg = grad.sign()
|
|
301
|
+
exp_avg = grad.sign().mul_(exp_avg.abs())
|
|
314
302
|
elif self.cautious_mask:
|
|
315
303
|
mask = (exp_avg * grad > 0).to(grad.dtype)
|
|
316
304
|
mask.div_(mask.mean().clamp_(min=1e-3))
|
|
@@ -321,9 +309,9 @@ class AdamW_adv(torch.optim.Optimizer):
|
|
|
321
309
|
exp_avg_slow = state['exp_avg_slow']
|
|
322
310
|
exp_avg_slow.mul_(beta3_ema).add_(grad, alpha=1 - beta3_ema)
|
|
323
311
|
if beta1 > 0:
|
|
324
|
-
update = torch.add(exp_avg, exp_avg_slow, alpha=
|
|
312
|
+
update = torch.add(exp_avg, exp_avg_slow, alpha=alpha)
|
|
325
313
|
else:
|
|
326
|
-
update = torch.add(grad, exp_avg_slow, alpha=
|
|
314
|
+
update = torch.add(grad, exp_avg_slow, alpha=alpha)
|
|
327
315
|
else:
|
|
328
316
|
update = exp_avg.clone() if beta1 > 0 else grad.clone()
|
|
329
317
|
|
|
@@ -343,9 +331,9 @@ class AdamW_adv(torch.optim.Optimizer):
|
|
|
343
331
|
# Decoupled weight decay
|
|
344
332
|
if group["weight_decay"] != 0:
|
|
345
333
|
if p.dtype == torch.bfloat16 and self.stochastic_rounding:
|
|
346
|
-
add_stochastic_(p.data, p.data, alpha=-group["weight_decay"] *
|
|
334
|
+
add_stochastic_(p.data, p.data, alpha=-group["weight_decay"] * lr)
|
|
347
335
|
else:
|
|
348
|
-
p.data.add_(p.data, alpha=-group["weight_decay"] *
|
|
336
|
+
p.data.add_(p.data, alpha=-group["weight_decay"] * lr)
|
|
349
337
|
|
|
350
338
|
if p.dtype == torch.bfloat16 and self.stochastic_rounding:
|
|
351
339
|
add_stochastic_(p.data, -update)
|
|
@@ -353,7 +341,38 @@ class AdamW_adv(torch.optim.Optimizer):
|
|
|
353
341
|
p.data.add_(-update)
|
|
354
342
|
del update
|
|
355
343
|
|
|
356
|
-
|
|
344
|
+
@torch.no_grad()
|
|
345
|
+
def step_parameter(self, p: torch.Tensor, group: dict, i: int | None = None):
|
|
346
|
+
# if 'exp_avg_sq' not in self.state[p] and 'mu_v_nmf' not in self.state[p]:
|
|
347
|
+
# return
|
|
348
|
+
|
|
349
|
+
if self.global_step is None and 'step' in self.state[p]:
|
|
350
|
+
# For backward compatibility
|
|
351
|
+
self.global_step = self.state[p]['step']
|
|
352
|
+
|
|
353
|
+
if group['use_bias_correction']:
|
|
354
|
+
current_step = self.global_step + 1
|
|
355
|
+
beta1, beta2 = group['betas']
|
|
356
|
+
bias_correction1 = 1.0 - beta1 ** current_step
|
|
357
|
+
bias_correction2 = 1.0 - beta2 ** current_step
|
|
358
|
+
else:
|
|
359
|
+
bias_correction1 = 1.0
|
|
360
|
+
bias_correction2 = 1.0
|
|
361
|
+
|
|
362
|
+
if group.get('kourkoutas_beta', False):
|
|
363
|
+
# Prepare Kourkoutas-β once per step using the global step counter.
|
|
364
|
+
self.kourkoutas_helper.maybe_prepare_step(self.global_step)
|
|
365
|
+
|
|
366
|
+
if not group.get('compiled_optimizer', False):
|
|
367
|
+
self.__step_parameter(p, group, group['lr'], bias_correction1, bias_correction2)
|
|
368
|
+
else:
|
|
369
|
+
lr_tensor = torch.tensor(group['lr'], device=p.device)
|
|
370
|
+
bias_correction1_tensor = torch.tensor(bias_correction1, device=p.device)
|
|
371
|
+
bias_correction2_tensor = torch.tensor(bias_correction2, device=p.device)
|
|
372
|
+
self._compiled_step_parameter(p, group, lr_tensor, bias_correction1_tensor, bias_correction2_tensor)
|
|
373
|
+
|
|
374
|
+
def compile(self, *args, **kwargs):
|
|
375
|
+
self._compiled_step_parameter = torch.compile(self.__step_parameter, *args, **kwargs)
|
|
357
376
|
|
|
358
377
|
@torch.no_grad()
|
|
359
378
|
def step(self, closure=None):
|
|
@@ -367,4 +386,6 @@ class AdamW_adv(torch.optim.Optimizer):
|
|
|
367
386
|
for i, p in enumerate(group['params']):
|
|
368
387
|
self.step_parameter(p, group, i)
|
|
369
388
|
|
|
389
|
+
self.global_step += 1
|
|
390
|
+
|
|
370
391
|
return loss
|