x-evolution 0.2.2__tar.gz → 0.2.3__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.
- {x_evolution-0.2.2 → x_evolution-0.2.3}/PKG-INFO +47 -1
- {x_evolution-0.2.2 → x_evolution-0.2.3}/README.md +46 -0
- {x_evolution-0.2.2 → x_evolution-0.2.3}/pyproject.toml +1 -1
- {x_evolution-0.2.2 → x_evolution-0.2.3}/tests/test_evolution.py +127 -0
- {x_evolution-0.2.2 → x_evolution-0.2.3}/x_evolution/cma_es.py +68 -5
- {x_evolution-0.2.2 → x_evolution-0.2.3}/x_evolution/x_evolution.py +81 -2
- {x_evolution-0.2.2 → x_evolution-0.2.3}/.gitignore +0 -0
- {x_evolution-0.2.2 → x_evolution-0.2.3}/LICENSE +0 -0
- {x_evolution-0.2.2 → x_evolution-0.2.3}/x_evolution/__init__.py +0 -0
- {x_evolution-0.2.2 → x_evolution-0.2.3}/x_evolution/noisable.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: x-evolution
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.3
|
|
4
4
|
Summary: x-evolution
|
|
5
5
|
Project-URL: Homepage, https://pypi.org/project/x-evolution/
|
|
6
6
|
Project-URL: Repository, https://github.com/lucidrains/x-evolution
|
|
@@ -102,6 +102,31 @@ evo_strat()
|
|
|
102
102
|
# can also specify checkpoint_every at init and select the one with your favored fitness score for continued policy gradient learning etc
|
|
103
103
|
```
|
|
104
104
|
|
|
105
|
+
## Preventing Catastrophic Forgetting (Anchored Weight Decay)
|
|
106
|
+
|
|
107
|
+
Continuing from a pretrained checkpoint, ES random-walks in directions the new task barely constrains, which can erode prior capabilities. [Anchored Weight Decay (AWD)](https://arxiv.org/abs/2605.30148) from Schweighofer et al. (2026) pulls weights toward their init snapshot without changing the optimizer. This is the L2 form, equivalent to L2 Init (Kumar et al., 2023). Weights are snapshotted at construction:
|
|
108
|
+
|
|
109
|
+
```python
|
|
110
|
+
evo_strat = EvoStrategy(
|
|
111
|
+
pretrained_model,
|
|
112
|
+
environment = environment,
|
|
113
|
+
num_generations = 100,
|
|
114
|
+
noise_population_size = 32,
|
|
115
|
+
learning_rate = 3e-3,
|
|
116
|
+
anchored_weight_decay_rate = 10. # L2 AWD penalty factor (paper's lambda); lr * rate is pulled toward the anchor each generation
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
evo_strat()
|
|
120
|
+
|
|
121
|
+
# after interleaving with gradient-based training (e.g. PPO -> ES -> PPO -> ES), re-anchor to the latest weights
|
|
122
|
+
evo_strat.reanchor_()
|
|
123
|
+
|
|
124
|
+
# or anchor to an explicit checkpoint / module
|
|
125
|
+
evo_strat.set_init_anchor_(torch.load('earlier.pt'))
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
`anchored_weight_decay_rate` is optimizer agnostic and also works with `use_optimizer = False` and custom optimizers. In `EvoStrategy` the pull per step is `learning_rate * anchored_weight_decay_rate`; `CMAEvoStrategy` applies the rate directly once per base update, so it requires `freeze_base = False` or `merge_every`. When using the default SGD optimizer, a positive rate turns off zero-targeting weight decay.
|
|
129
|
+
|
|
105
130
|
## CMA-ES
|
|
106
131
|
|
|
107
132
|
`CMAEvoStrategy` implements full-covariance CMA-ES over a low-rank adapter, so an `(m, n)` weight costs `r * (m + n)` dimensions instead of `m * n`:
|
|
@@ -282,6 +307,27 @@ Special thanks to [Pollen Robotics 🤗](https://www.pollen-robotics.com/) for t
|
|
|
282
307
|
}
|
|
283
308
|
```
|
|
284
309
|
|
|
310
|
+
```bibtex
|
|
311
|
+
@article{schweighofer2026overcoming,
|
|
312
|
+
title = {Overcoming Forgetting in LLM Fine-Tuning with Evolution Strategies},
|
|
313
|
+
author = {Kajetan Schweighofer and Conor F. Hayes and Roberto Dailey and Risto Miikkulainen and Xin Qiu},
|
|
314
|
+
journal = {ArXiv},
|
|
315
|
+
year = {2026},
|
|
316
|
+
volume = {abs/2605.30148},
|
|
317
|
+
url = {https://arxiv.org/abs/2605.30148}
|
|
318
|
+
}
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
```bibtex
|
|
322
|
+
@article{kumar2023maintaining,
|
|
323
|
+
title = {Maintaining Plasticity in Continual Learning via Regenerative Regularization},
|
|
324
|
+
author = {Saurabh Kumar and Henrik Marklund and Benjamin Van Roy},
|
|
325
|
+
journal = {ArXiv},
|
|
326
|
+
year = {2023},
|
|
327
|
+
url = {https://arxiv.org/abs/2308.11958}
|
|
328
|
+
}
|
|
329
|
+
```
|
|
330
|
+
|
|
285
331
|
```bibtex
|
|
286
332
|
@inproceedings{anonymous2026continual,
|
|
287
333
|
title = {Continual Reinforcement Learning with Neuroevolution},
|
|
@@ -46,6 +46,31 @@ evo_strat()
|
|
|
46
46
|
# can also specify checkpoint_every at init and select the one with your favored fitness score for continued policy gradient learning etc
|
|
47
47
|
```
|
|
48
48
|
|
|
49
|
+
## Preventing Catastrophic Forgetting (Anchored Weight Decay)
|
|
50
|
+
|
|
51
|
+
Continuing from a pretrained checkpoint, ES random-walks in directions the new task barely constrains, which can erode prior capabilities. [Anchored Weight Decay (AWD)](https://arxiv.org/abs/2605.30148) from Schweighofer et al. (2026) pulls weights toward their init snapshot without changing the optimizer. This is the L2 form, equivalent to L2 Init (Kumar et al., 2023). Weights are snapshotted at construction:
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
evo_strat = EvoStrategy(
|
|
55
|
+
pretrained_model,
|
|
56
|
+
environment = environment,
|
|
57
|
+
num_generations = 100,
|
|
58
|
+
noise_population_size = 32,
|
|
59
|
+
learning_rate = 3e-3,
|
|
60
|
+
anchored_weight_decay_rate = 10. # L2 AWD penalty factor (paper's lambda); lr * rate is pulled toward the anchor each generation
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
evo_strat()
|
|
64
|
+
|
|
65
|
+
# after interleaving with gradient-based training (e.g. PPO -> ES -> PPO -> ES), re-anchor to the latest weights
|
|
66
|
+
evo_strat.reanchor_()
|
|
67
|
+
|
|
68
|
+
# or anchor to an explicit checkpoint / module
|
|
69
|
+
evo_strat.set_init_anchor_(torch.load('earlier.pt'))
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
`anchored_weight_decay_rate` is optimizer agnostic and also works with `use_optimizer = False` and custom optimizers. In `EvoStrategy` the pull per step is `learning_rate * anchored_weight_decay_rate`; `CMAEvoStrategy` applies the rate directly once per base update, so it requires `freeze_base = False` or `merge_every`. When using the default SGD optimizer, a positive rate turns off zero-targeting weight decay.
|
|
73
|
+
|
|
49
74
|
## CMA-ES
|
|
50
75
|
|
|
51
76
|
`CMAEvoStrategy` implements full-covariance CMA-ES over a low-rank adapter, so an `(m, n)` weight costs `r * (m + n)` dimensions instead of `m * n`:
|
|
@@ -226,6 +251,27 @@ Special thanks to [Pollen Robotics 🤗](https://www.pollen-robotics.com/) for t
|
|
|
226
251
|
}
|
|
227
252
|
```
|
|
228
253
|
|
|
254
|
+
```bibtex
|
|
255
|
+
@article{schweighofer2026overcoming,
|
|
256
|
+
title = {Overcoming Forgetting in LLM Fine-Tuning with Evolution Strategies},
|
|
257
|
+
author = {Kajetan Schweighofer and Conor F. Hayes and Roberto Dailey and Risto Miikkulainen and Xin Qiu},
|
|
258
|
+
journal = {ArXiv},
|
|
259
|
+
year = {2026},
|
|
260
|
+
volume = {abs/2605.30148},
|
|
261
|
+
url = {https://arxiv.org/abs/2605.30148}
|
|
262
|
+
}
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
```bibtex
|
|
266
|
+
@article{kumar2023maintaining,
|
|
267
|
+
title = {Maintaining Plasticity in Continual Learning via Regenerative Regularization},
|
|
268
|
+
author = {Saurabh Kumar and Henrik Marklund and Benjamin Van Roy},
|
|
269
|
+
journal = {ArXiv},
|
|
270
|
+
year = {2023},
|
|
271
|
+
url = {https://arxiv.org/abs/2308.11958}
|
|
272
|
+
}
|
|
273
|
+
```
|
|
274
|
+
|
|
229
275
|
```bibtex
|
|
230
276
|
@inproceedings{anonymous2026continual,
|
|
231
277
|
title = {Continual Reinforcement Learning with Neuroevolution},
|
|
@@ -809,3 +809,130 @@ def test_cma_scopes():
|
|
|
809
809
|
|
|
810
810
|
fitnesses = cma(num_generations = 1)
|
|
811
811
|
assert fitnesses.shape[-1] == 2
|
|
812
|
+
|
|
813
|
+
|
|
814
|
+
@param('use_optimizer', (True, False))
|
|
815
|
+
def test_evo_strat_awd_prevents_nullspace_drift(use_optimizer, tmp_path):
|
|
816
|
+
from functools import partial
|
|
817
|
+
|
|
818
|
+
from torch.optim import SGD
|
|
819
|
+
|
|
820
|
+
from x_evolution.x_evolution import EvoStrategy
|
|
821
|
+
|
|
822
|
+
# w_null is unused by the reward, so any drift is pure forgetting
|
|
823
|
+
|
|
824
|
+
class PretrainedNullspaceModel(nn.Module):
|
|
825
|
+
def __init__(self):
|
|
826
|
+
super().__init__()
|
|
827
|
+
self.w_signal = nn.Parameter(torch.zeros(4))
|
|
828
|
+
self.w_null = nn.Parameter(torch.full((32,), 5.0))
|
|
829
|
+
|
|
830
|
+
def forward(self):
|
|
831
|
+
return -((self.w_signal - 1.0) ** 2).mean()
|
|
832
|
+
|
|
833
|
+
def train(awd_rate, name):
|
|
834
|
+
torch.manual_seed(42)
|
|
835
|
+
model = PretrainedNullspaceModel()
|
|
836
|
+
|
|
837
|
+
evo_strat = EvoStrategy(
|
|
838
|
+
model,
|
|
839
|
+
environment = lambda m: m(),
|
|
840
|
+
num_generations = 40,
|
|
841
|
+
noise_population_size = 16,
|
|
842
|
+
learning_rate = 5e-3,
|
|
843
|
+
noise_scale = 5e-2,
|
|
844
|
+
use_optimizer = use_optimizer,
|
|
845
|
+
anchored_weight_decay_rate = awd_rate,
|
|
846
|
+
optimizer_klass = partial(SGD, nesterov = True, momentum = 0.1),
|
|
847
|
+
cpu = True,
|
|
848
|
+
verbose = False,
|
|
849
|
+
checkpoint_path = str(tmp_path / name)
|
|
850
|
+
)
|
|
851
|
+
evo_strat()
|
|
852
|
+
return model, evo_strat
|
|
853
|
+
|
|
854
|
+
unanchored, _ = train(0., 'unanchored')
|
|
855
|
+
anchored, evo_strat = train(5., 'anchored')
|
|
856
|
+
|
|
857
|
+
assert set(evo_strat.anchor_params) == {'w_signal', 'w_null'}
|
|
858
|
+
assert torch.allclose(evo_strat.anchor_params['w_null'], torch.full((32,), 5.0))
|
|
859
|
+
|
|
860
|
+
# signal improves while the anchored nullspace stays near the checkpoint
|
|
861
|
+
|
|
862
|
+
assert (anchored.w_signal - 1.0).norm().item() < 0.85
|
|
863
|
+
assert (anchored.w_null - 5.0).norm().item() < (unanchored.w_null - 5.0).norm().item() * 0.7
|
|
864
|
+
|
|
865
|
+
# after an external update, reanchor adopts the new weights
|
|
866
|
+
|
|
867
|
+
with torch.no_grad():
|
|
868
|
+
anchored.w_null.fill_(-3.0)
|
|
869
|
+
|
|
870
|
+
evo_strat.reanchor_()
|
|
871
|
+
evo_strat(num_generations = 10)
|
|
872
|
+
assert (anchored.w_null - (-3.0)).norm().item() < 1.0
|
|
873
|
+
|
|
874
|
+
# set_init_anchor_ accepts an explicit state dict or module
|
|
875
|
+
|
|
876
|
+
evo_strat.set_init_anchor_({'w_signal': torch.zeros(4), 'w_null': torch.full((32,), 1.0)})
|
|
877
|
+
assert torch.allclose(evo_strat.anchor_params['w_null'], torch.full((32,), 1.0))
|
|
878
|
+
|
|
879
|
+
anchor_model = PretrainedNullspaceModel()
|
|
880
|
+
|
|
881
|
+
with torch.no_grad():
|
|
882
|
+
anchor_model.w_null.fill_(2.0)
|
|
883
|
+
|
|
884
|
+
evo_strat.set_init_anchor_(anchor_model)
|
|
885
|
+
assert torch.allclose(evo_strat.anchor_params['w_null'], torch.full((32,), 2.0))
|
|
886
|
+
|
|
887
|
+
|
|
888
|
+
def test_cma_awd_and_reanchor(tmp_path):
|
|
889
|
+
torch.manual_seed(0)
|
|
890
|
+
|
|
891
|
+
class TwoMatrixModel(nn.Module):
|
|
892
|
+
def __init__(self):
|
|
893
|
+
super().__init__()
|
|
894
|
+
self.w_signal = nn.Parameter(torch.zeros(3, 3))
|
|
895
|
+
self.w_null = nn.Parameter(torch.full((4, 4), 3.0))
|
|
896
|
+
|
|
897
|
+
def train(awd_rate, name):
|
|
898
|
+
model = TwoMatrixModel()
|
|
899
|
+
|
|
900
|
+
cma = CMAEvoStrategy(
|
|
901
|
+
model,
|
|
902
|
+
environment = lambda m: -((m.w_signal - 0.5) ** 2).mean(),
|
|
903
|
+
num_generations = 25,
|
|
904
|
+
population_size = 12,
|
|
905
|
+
noise_scale = 1e-1,
|
|
906
|
+
lora_rank = None,
|
|
907
|
+
freeze_base = False,
|
|
908
|
+
mirror_sampling = False,
|
|
909
|
+
anchored_weight_decay_rate = awd_rate,
|
|
910
|
+
cpu = True,
|
|
911
|
+
verbose = False,
|
|
912
|
+
checkpoint_path = str(tmp_path / name)
|
|
913
|
+
)
|
|
914
|
+
cma()
|
|
915
|
+
return model, cma
|
|
916
|
+
|
|
917
|
+
model_unanchored, _ = train(0., 'unanchored')
|
|
918
|
+
model_anchored, cma = train(0.25, 'anchored')
|
|
919
|
+
|
|
920
|
+
assert torch.allclose(cma.default_state.anchor_params['w_null'], torch.full((4, 4), 3.0))
|
|
921
|
+
assert (model_anchored.w_signal - 0.5).norm().item() < (torch.zeros(3, 3) - 0.5).norm().item() * 0.5
|
|
922
|
+
assert (model_anchored.w_null - 3.0).norm().item() < (model_unanchored.w_null - 3.0).norm().item()
|
|
923
|
+
|
|
924
|
+
# after an external update, reanchor adopts the new weights
|
|
925
|
+
|
|
926
|
+
with torch.no_grad():
|
|
927
|
+
model_anchored.w_null.fill_(-2.0)
|
|
928
|
+
|
|
929
|
+
cma.reanchor_()
|
|
930
|
+
assert torch.allclose(cma.default_state.anchor_params['w_null'], torch.full((4, 4), -2.0))
|
|
931
|
+
|
|
932
|
+
cma(num_generations = 5)
|
|
933
|
+
assert (model_anchored.w_null - (-2.0)).norm().item() < 1.0
|
|
934
|
+
|
|
935
|
+
# set a new init anchor explicitly from a state dict
|
|
936
|
+
|
|
937
|
+
cma.set_init_anchor_({'w_signal': torch.zeros(3, 3), 'w_null': torch.full((4, 4), 1.0)})
|
|
938
|
+
assert torch.allclose(cma.default_state.anchor_params['w_null'], torch.full((4, 4), 1.0))
|
|
@@ -50,7 +50,8 @@ class _CMAState:
|
|
|
50
50
|
population_size,
|
|
51
51
|
noise_scale,
|
|
52
52
|
noise_dtype,
|
|
53
|
-
sigma_clamp_range
|
|
53
|
+
sigma_clamp_range,
|
|
54
|
+
anchored_weight_decay_rate = 0.
|
|
54
55
|
):
|
|
55
56
|
self.strategy = strategy
|
|
56
57
|
self.param_names = sorted(param_names)
|
|
@@ -61,6 +62,7 @@ class _CMAState:
|
|
|
61
62
|
self.sep_cma = sep_cma
|
|
62
63
|
self.noise_dtype = noise_dtype
|
|
63
64
|
self.sigma_clamp_range = sigma_clamp_range
|
|
65
|
+
self.anchored_weight_decay_rate = anchored_weight_decay_rate
|
|
64
66
|
|
|
65
67
|
self.dim = 0
|
|
66
68
|
self.param_slices = dict()
|
|
@@ -78,6 +80,14 @@ class _CMAState:
|
|
|
78
80
|
for name in self.param_names
|
|
79
81
|
}
|
|
80
82
|
|
|
83
|
+
self.anchor_params = None
|
|
84
|
+
|
|
85
|
+
if anchored_weight_decay_rate > 0.:
|
|
86
|
+
self.anchor_params = {
|
|
87
|
+
name: param.clone()
|
|
88
|
+
for name, param in self.base_params.items()
|
|
89
|
+
}
|
|
90
|
+
|
|
81
91
|
self.mean = torch.zeros(self.dim, device = device)
|
|
82
92
|
self.C = torch.ones(self.dim, device = device) if sep_cma else torch.eye(self.dim, device = device)
|
|
83
93
|
self.pc = torch.zeros(self.dim, device = device)
|
|
@@ -282,13 +292,24 @@ class _CMAState:
|
|
|
282
292
|
|
|
283
293
|
self.generation = generation
|
|
284
294
|
|
|
295
|
+
@torch.no_grad()
|
|
296
|
+
def add_to_base_(self, name, delta):
|
|
297
|
+
base = self.base_params[name]
|
|
298
|
+
|
|
299
|
+
# AWD toward the init snapshot (Schweighofer et al. 2026)
|
|
300
|
+
|
|
301
|
+
if self.anchored_weight_decay_rate > 0.:
|
|
302
|
+
base.lerp_(self.anchor_params[name].to(base.device), self.anchored_weight_decay_rate)
|
|
303
|
+
|
|
304
|
+
base.add_(delta.to(base.device))
|
|
305
|
+
|
|
285
306
|
@torch.no_grad()
|
|
286
307
|
def commit_(self):
|
|
287
308
|
if not exists(self._update):
|
|
288
309
|
return
|
|
289
310
|
|
|
290
311
|
for name, delta in self._update.items():
|
|
291
|
-
self.
|
|
312
|
+
self.add_to_base_(name, delta)
|
|
292
313
|
|
|
293
314
|
self._update = None
|
|
294
315
|
self.mean.zero_()
|
|
@@ -299,12 +320,36 @@ class _CMAState:
|
|
|
299
320
|
deltas = self.decode_(self.mean)
|
|
300
321
|
|
|
301
322
|
for name, delta in deltas.items():
|
|
302
|
-
|
|
303
|
-
base.add_(delta.to(base.device))
|
|
323
|
+
self.add_to_base_(name, delta)
|
|
304
324
|
|
|
305
325
|
self.mean.zero_()
|
|
306
326
|
self.set_model_to_(self.mean)
|
|
307
327
|
|
|
328
|
+
@torch.no_grad()
|
|
329
|
+
def set_init_anchor_(self, model_or_state = None):
|
|
330
|
+
# anchor to a module, state dict, or the current weights
|
|
331
|
+
|
|
332
|
+
if not exists(model_or_state):
|
|
333
|
+
state = self.strategy._named_parameters
|
|
334
|
+
elif isinstance(model_or_state, Module):
|
|
335
|
+
state = model_or_state.state_dict()
|
|
336
|
+
else:
|
|
337
|
+
state = model_or_state
|
|
338
|
+
|
|
339
|
+
self.anchor_params = {
|
|
340
|
+
name: state[name].detach().clone().to(self.base_params[name].device)
|
|
341
|
+
for name in self.param_names
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
@torch.no_grad()
|
|
345
|
+
def reanchor_(self):
|
|
346
|
+
self.set_init_anchor_()
|
|
347
|
+
|
|
348
|
+
for name in self.param_names:
|
|
349
|
+
self.base_params[name] = self.anchor_params[name].clone()
|
|
350
|
+
|
|
351
|
+
self.mean.zero_()
|
|
352
|
+
|
|
308
353
|
class CMAEvoStrategy(EvolutionStrategy):
|
|
309
354
|
|
|
310
355
|
@beartype
|
|
@@ -320,6 +365,7 @@ class CMAEvoStrategy(EvolutionStrategy):
|
|
|
320
365
|
lora_rank: int | None = 1,
|
|
321
366
|
freeze_base = True,
|
|
322
367
|
merge_every: int | None = None, # fold mean into base every n generations
|
|
368
|
+
anchored_weight_decay_rate = 0., # Anchored Weight Decay (AWD) - pulls the base toward its init snapshot
|
|
323
369
|
mirror_sampling = True,
|
|
324
370
|
sep_cma = False, # diagonal covariance: O(dim) memory, no eigendecomposition
|
|
325
371
|
params_to_optimize: dict[str, ParametersToOptimize] | ParametersToOptimize | None = None,
|
|
@@ -444,6 +490,9 @@ class CMAEvoStrategy(EvolutionStrategy):
|
|
|
444
490
|
assert freeze_base, 'merge_every requires freeze_base=True'
|
|
445
491
|
assert merge_every >= 1, 'merge_every must be >= 1'
|
|
446
492
|
|
|
493
|
+
if anchored_weight_decay_rate > 0.:
|
|
494
|
+
assert not freeze_base or exists(merge_every), 'AWD requires base updates: set freeze_base=False or merge_every'
|
|
495
|
+
|
|
447
496
|
assert not (mirror_sampling and not freeze_base and exists(lora_rank)), 'mirror sampling is incompatible with freeze_base=False and a low-rank adapter; pass mirror_sampling=False or freeze_base=True'
|
|
448
497
|
|
|
449
498
|
self._mirror_sampling = mirror_sampling
|
|
@@ -452,6 +501,9 @@ class CMAEvoStrategy(EvolutionStrategy):
|
|
|
452
501
|
self.cma_device = torch.device(cma_device)
|
|
453
502
|
|
|
454
503
|
assert 0. < sigma_clamp_range[0] < sigma_clamp_range[1]
|
|
504
|
+
assert anchored_weight_decay_rate >= 0.
|
|
505
|
+
|
|
506
|
+
self.anchored_weight_decay_rate = anchored_weight_decay_rate
|
|
455
507
|
|
|
456
508
|
# states are created lazily per parameter subset / scope
|
|
457
509
|
|
|
@@ -527,11 +579,22 @@ class CMAEvoStrategy(EvolutionStrategy):
|
|
|
527
579
|
population_size = self.population_size,
|
|
528
580
|
noise_scale = self.noise_scale,
|
|
529
581
|
noise_dtype = self.noise_dtype,
|
|
530
|
-
sigma_clamp_range = self.sigma_clamp_range
|
|
582
|
+
sigma_clamp_range = self.sigma_clamp_range,
|
|
583
|
+
anchored_weight_decay_rate = self.anchored_weight_decay_rate
|
|
531
584
|
)
|
|
532
585
|
|
|
533
586
|
return self._states[names]
|
|
534
587
|
|
|
588
|
+
@torch.no_grad()
|
|
589
|
+
def set_init_anchor_(self, model_or_state = None):
|
|
590
|
+
for state in self._states.values():
|
|
591
|
+
state.set_init_anchor_(model_or_state)
|
|
592
|
+
|
|
593
|
+
@torch.no_grad()
|
|
594
|
+
def reanchor_(self):
|
|
595
|
+
for state in self._states.values():
|
|
596
|
+
state.reanchor_()
|
|
597
|
+
|
|
535
598
|
def to_cma_device_(self):
|
|
536
599
|
for state in self._states.values():
|
|
537
600
|
state.to_device_()
|
|
@@ -181,7 +181,8 @@ class EvoStrategy(EvolutionStrategy):
|
|
|
181
181
|
noise_scale_learning_rate = 1e-5,
|
|
182
182
|
noise_scale_clamp_range: tuple[float, float] = (1e-3, 1e-1),
|
|
183
183
|
use_optimizer = True,
|
|
184
|
-
|
|
184
|
+
anchored_weight_decay_rate = 0., # Anchored Weight Decay (AWD) - pulls parameters toward their init snapshot
|
|
185
|
+
optimizer_klass: type[Optimizer] | Callable | None = None,
|
|
185
186
|
optimizer_kwargs: dict = dict(),
|
|
186
187
|
use_sigma_optimizer = True,
|
|
187
188
|
sigma_optimizer_klass: type[Optimizer] | Callable = partial(SGD, nesterov = True, momentum = 0.1),
|
|
@@ -343,12 +344,33 @@ class EvoStrategy(EvolutionStrategy):
|
|
|
343
344
|
|
|
344
345
|
self.rollout_fixed_seed = rollout_fixed_seed
|
|
345
346
|
|
|
347
|
+
# anchored weight decay (AWD, Schweighofer et al. 2026)
|
|
348
|
+
|
|
349
|
+
assert anchored_weight_decay_rate >= 0.
|
|
350
|
+
|
|
351
|
+
self.anchored_weight_decay_rate = anchored_weight_decay_rate
|
|
352
|
+
|
|
353
|
+
self.anchor_params = None
|
|
354
|
+
|
|
355
|
+
if anchored_weight_decay_rate > 0.:
|
|
356
|
+
self.anchor_params = {
|
|
357
|
+
name: named_parameters_dict[name].detach().clone()
|
|
358
|
+
for name in self.param_names_to_optimize
|
|
359
|
+
}
|
|
360
|
+
|
|
346
361
|
# maybe use optimizer to update, allow for Adam
|
|
347
362
|
|
|
348
363
|
self.use_optimizer = use_optimizer
|
|
364
|
+
self.optimizer = None
|
|
365
|
+
|
|
366
|
+
if not exists(optimizer_klass):
|
|
367
|
+
# AWD replaces weight decay toward zero, which erodes the checkpoint
|
|
368
|
+
|
|
369
|
+
weight_decay = 0. if anchored_weight_decay_rate > 0. else 1e-2
|
|
370
|
+
optimizer_klass = partial(SGD, nesterov = True, momentum = 0.1, weight_decay = weight_decay)
|
|
349
371
|
|
|
350
372
|
if use_optimizer:
|
|
351
|
-
optim_params = [named_parameters_dict[name] for name in
|
|
373
|
+
optim_params = [named_parameters_dict[name] for name in self.param_names_to_optimize]
|
|
352
374
|
self.optimizer = optimizer_klass(optim_params, lr = learning_rate, **optimizer_kwargs)
|
|
353
375
|
|
|
354
376
|
self.use_sigma_optimizer = use_sigma_optimizer
|
|
@@ -384,6 +406,54 @@ class EvoStrategy(EvolutionStrategy):
|
|
|
384
406
|
sigma_index = self.param_name_to_sigma_index[param_name]
|
|
385
407
|
return self.sigmas[sigma_index]
|
|
386
408
|
|
|
409
|
+
@torch.no_grad()
|
|
410
|
+
def anchored_weight_decay_(self, param_names):
|
|
411
|
+
if not exists(self.anchor_params) or self.anchored_weight_decay_rate <= 0.:
|
|
412
|
+
return
|
|
413
|
+
|
|
414
|
+
named_params = dict(self.model.named_parameters())
|
|
415
|
+
lr = self.optimizer.param_groups[0]['lr'] if exists(self.optimizer) else self.learning_rate
|
|
416
|
+
decay_rate = lr * self.anchored_weight_decay_rate
|
|
417
|
+
|
|
418
|
+
for name in param_names:
|
|
419
|
+
param = named_params[name]
|
|
420
|
+
anchor = self.anchor_params.get(name)
|
|
421
|
+
|
|
422
|
+
if not exists(anchor):
|
|
423
|
+
self.anchor_params[name] = param.detach().clone()
|
|
424
|
+
continue
|
|
425
|
+
|
|
426
|
+
if anchor.device != param.device:
|
|
427
|
+
anchor = anchor.to(param.device)
|
|
428
|
+
self.anchor_params[name] = anchor
|
|
429
|
+
|
|
430
|
+
param.lerp_(anchor, decay_rate)
|
|
431
|
+
|
|
432
|
+
@torch.no_grad()
|
|
433
|
+
def set_init_anchor_(self, model_or_state = None):
|
|
434
|
+
# anchor to a module, state dict, or the current weights
|
|
435
|
+
|
|
436
|
+
named_params = dict(self.model.named_parameters())
|
|
437
|
+
|
|
438
|
+
if not exists(model_or_state):
|
|
439
|
+
state = named_params
|
|
440
|
+
elif isinstance(model_or_state, Module):
|
|
441
|
+
state = model_or_state.state_dict()
|
|
442
|
+
else:
|
|
443
|
+
state = model_or_state
|
|
444
|
+
|
|
445
|
+
names = self.param_names_to_optimize if not exists(self.anchor_params) else self.anchor_params
|
|
446
|
+
|
|
447
|
+
self.anchor_params = {
|
|
448
|
+
name: state[name].detach().clone().to(named_params[name].device)
|
|
449
|
+
for name in names
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
def reanchor_(self):
|
|
453
|
+
# adopt current weights as anchor after interleaving with policy gradient training
|
|
454
|
+
|
|
455
|
+
self.set_init_anchor_()
|
|
456
|
+
|
|
387
457
|
@torch.inference_mode()
|
|
388
458
|
def evolve_(
|
|
389
459
|
self,
|
|
@@ -420,6 +490,15 @@ class EvoStrategy(EvolutionStrategy):
|
|
|
420
490
|
|
|
421
491
|
weights /= self.noise_population_size * (2. if self.mirror_sampling else 1.)
|
|
422
492
|
|
|
493
|
+
# anchored weight decay, before the optimizer / weight update
|
|
494
|
+
|
|
495
|
+
active_names = self.param_names_to_optimize
|
|
496
|
+
|
|
497
|
+
if exists(scoped_names):
|
|
498
|
+
active_names = [name for name in active_names if name in scoped_names]
|
|
499
|
+
|
|
500
|
+
self.anchored_weight_decay_(active_names)
|
|
501
|
+
|
|
423
502
|
# update one seed at a time for enabling evolutionary strategy for large models
|
|
424
503
|
|
|
425
504
|
for individual_seed, weight in zip(seeds_for_population.tolist(), weights.tolist()):
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|