x-evolution 0.2.3__tar.gz → 0.2.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.
- {x_evolution-0.2.3 → x_evolution-0.2.4}/PKG-INFO +1 -1
- {x_evolution-0.2.3 → x_evolution-0.2.4}/pyproject.toml +1 -1
- {x_evolution-0.2.3 → x_evolution-0.2.4}/tests/test_evolution.py +1 -1
- {x_evolution-0.2.3 → x_evolution-0.2.4}/x_evolution/cma_es.py +41 -19
- {x_evolution-0.2.3 → x_evolution-0.2.4}/.gitignore +0 -0
- {x_evolution-0.2.3 → x_evolution-0.2.4}/LICENSE +0 -0
- {x_evolution-0.2.3 → x_evolution-0.2.4}/README.md +0 -0
- {x_evolution-0.2.3 → x_evolution-0.2.4}/x_evolution/__init__.py +0 -0
- {x_evolution-0.2.3 → x_evolution-0.2.4}/x_evolution/noisable.py +0 -0
- {x_evolution-0.2.3 → x_evolution-0.2.4}/x_evolution/x_evolution.py +0 -0
|
@@ -915,7 +915,7 @@ def test_cma_awd_and_reanchor(tmp_path):
|
|
|
915
915
|
return model, cma
|
|
916
916
|
|
|
917
917
|
model_unanchored, _ = train(0., 'unanchored')
|
|
918
|
-
model_anchored, cma = train(0.
|
|
918
|
+
model_anchored, cma = train(0.1, 'anchored')
|
|
919
919
|
|
|
920
920
|
assert torch.allclose(cma.default_state.anchor_params['w_null'], torch.full((4, 4), 3.0))
|
|
921
921
|
assert (model_anchored.w_signal - 0.5).norm().item() < (torch.zeros(3, 3) - 0.5).norm().item() * 0.5
|
|
@@ -9,13 +9,13 @@ from typing import Any
|
|
|
9
9
|
import torch
|
|
10
10
|
from accelerate import Accelerator
|
|
11
11
|
from beartype import beartype
|
|
12
|
+
from einops import einsum, rearrange
|
|
12
13
|
from torch import Tensor, arange, randint, tensor
|
|
13
14
|
from torch.nn import Module, ModuleList
|
|
14
15
|
from torch_einops_utils import (
|
|
15
16
|
batched_index_select,
|
|
16
17
|
cast_item,
|
|
17
18
|
dict_add,
|
|
18
|
-
safe_cat,
|
|
19
19
|
safe_stack,
|
|
20
20
|
tree_map_tensor_to_device,
|
|
21
21
|
)
|
|
@@ -90,6 +90,8 @@ class _CMAState:
|
|
|
90
90
|
|
|
91
91
|
self.mean = torch.zeros(self.dim, device = device)
|
|
92
92
|
self.C = torch.ones(self.dim, device = device) if sep_cma else torch.eye(self.dim, device = device)
|
|
93
|
+
self.B = None if sep_cma else torch.eye(self.dim, device = device)
|
|
94
|
+
self.D = None if sep_cma else torch.ones(self.dim, device = device)
|
|
93
95
|
self.pc = torch.zeros(self.dim, device = device)
|
|
94
96
|
self.ps = torch.zeros(self.dim, device = device)
|
|
95
97
|
|
|
@@ -125,6 +127,9 @@ class _CMAState:
|
|
|
125
127
|
self.sigma = sqrt(noise_scale) if exists(lora_rank) else noise_scale
|
|
126
128
|
|
|
127
129
|
self.generation = 0
|
|
130
|
+
self._eigen_generation = 0
|
|
131
|
+
c_cov = self.c_1 + self.c_mu
|
|
132
|
+
self.eigen_interval = max(1, int(1. / (10. * dim * c_cov))) if (not sep_cma and c_cov > 0.) else 1
|
|
128
133
|
self._update = None
|
|
129
134
|
|
|
130
135
|
@property
|
|
@@ -145,11 +150,24 @@ class _CMAState:
|
|
|
145
150
|
|
|
146
151
|
def to_device_(self):
|
|
147
152
|
state = dict(mean = self.mean, C = self.C, pc = self.pc, ps = self.ps, weights = self.weights)
|
|
153
|
+
|
|
154
|
+
if not self.sep_cma:
|
|
155
|
+
state['B'] = self.B
|
|
156
|
+
state['D'] = self.D
|
|
157
|
+
|
|
148
158
|
state = tree_map_tensor_to_device(state, self.strategy.cma_device)
|
|
149
159
|
|
|
150
160
|
for name, value in state.items():
|
|
151
161
|
setattr(self, name, value)
|
|
152
162
|
|
|
163
|
+
@torch.no_grad()
|
|
164
|
+
def _update_eigensystem_(self):
|
|
165
|
+
C = 0.5 * (self.C + self.C.T)
|
|
166
|
+
eigenvalues, eigenvectors = torch.linalg.eigh(C)
|
|
167
|
+
self.D.copy_(eigenvalues.clamp(min = 1e-14).sqrt())
|
|
168
|
+
self.B.copy_(eigenvectors)
|
|
169
|
+
self._eigen_generation = self.generation
|
|
170
|
+
|
|
153
171
|
@torch.no_grad()
|
|
154
172
|
def decode_(self, vec):
|
|
155
173
|
deltas = dict()
|
|
@@ -161,11 +179,11 @@ class _CMAState:
|
|
|
161
179
|
|
|
162
180
|
if exists(self.lora_rank):
|
|
163
181
|
r = self.lora_rank
|
|
164
|
-
b = param_vec[:m * r]
|
|
165
|
-
a = param_vec[m * r:]
|
|
166
|
-
delta = (b
|
|
182
|
+
b = rearrange(param_vec[:m * r], '(m r) -> m r', m = m, r = r)
|
|
183
|
+
a = rearrange(param_vec[m * r:], '(r n) -> r n', r = r, n = n)
|
|
184
|
+
delta = einsum(b, a, 'm r, r n -> m n') / sqrt(r)
|
|
167
185
|
else:
|
|
168
|
-
delta = param_vec
|
|
186
|
+
delta = rearrange(param_vec, '(m n) -> m n', m = m, n = n)
|
|
169
187
|
|
|
170
188
|
deltas[name] = delta
|
|
171
189
|
|
|
@@ -200,19 +218,16 @@ class _CMAState:
|
|
|
200
218
|
assert (num_samples % 2) == 0
|
|
201
219
|
half = num_samples // 2
|
|
202
220
|
z_half = self.sample_noise_((half, self.dim), generator)
|
|
203
|
-
z =
|
|
221
|
+
z = rearrange([z_half, -z_half], 'two h d -> (h two) d')
|
|
204
222
|
else:
|
|
205
223
|
z = self.sample_noise_((num_samples, self.dim), generator)
|
|
206
224
|
|
|
207
225
|
if self.sep_cma:
|
|
208
226
|
y = z * self.C.sqrt()
|
|
209
227
|
else:
|
|
210
|
-
#
|
|
228
|
+
# C = B diag(D ** 2) B^T, cached and refreshed every eigen_interval generations
|
|
211
229
|
|
|
212
|
-
|
|
213
|
-
eigenvalues, eigenvectors = torch.linalg.eigh(C)
|
|
214
|
-
eigenvalues = eigenvalues.clamp(min = 1e-14)
|
|
215
|
-
y = (z * eigenvalues.sqrt()) @ eigenvectors.T
|
|
230
|
+
y = einsum(z * self.D, self.B, 'n k, d k -> n d')
|
|
216
231
|
|
|
217
232
|
x = self.mean + self.sigma * y
|
|
218
233
|
|
|
@@ -232,16 +247,17 @@ class _CMAState:
|
|
|
232
247
|
y_selected = batched_index_select(y, selected, dim = 0)
|
|
233
248
|
z_selected = batched_index_select(z, selected, dim = 0)
|
|
234
249
|
|
|
235
|
-
y_mean = (weights
|
|
236
|
-
z_mean = (weights
|
|
250
|
+
y_mean = einsum(weights, y_selected, 'p, p d -> d')
|
|
251
|
+
z_mean = einsum(weights, z_selected.to(weights.dtype), 'p, p k -> k')
|
|
237
252
|
|
|
238
253
|
# mean shift in factor space
|
|
239
254
|
|
|
240
255
|
new_mean = self.mean + self.sigma * y_mean
|
|
241
256
|
|
|
242
|
-
# sigma path
|
|
257
|
+
# sigma path: C^{-1/2} y_w = B z_w (coordinate space, invariant to eigenvector sign flips)
|
|
243
258
|
|
|
244
|
-
|
|
259
|
+
z_step = z_mean if self.sep_cma else einsum(self.B, z_mean, 'd k, k -> d')
|
|
260
|
+
ps = (1. - self.c_sigma) * self.ps + sqrt(self.c_sigma * (2. - self.c_sigma) * self.mu_eff) * z_step
|
|
245
261
|
ps_norm = cast_item(ps.norm())
|
|
246
262
|
|
|
247
263
|
self.ps.copy_(ps)
|
|
@@ -258,18 +274,21 @@ class _CMAState:
|
|
|
258
274
|
self.pc.copy_(pc)
|
|
259
275
|
|
|
260
276
|
if self.sep_cma:
|
|
277
|
+
rank_mu = einsum(weights, y_selected.square(), 'p, p d -> d')
|
|
278
|
+
|
|
261
279
|
C = (
|
|
262
280
|
(1. - self.c_1 - self.c_mu) * self.C
|
|
263
281
|
+ self.c_1 * (pc.square() + (1. - h_sigma) * self.c_c * (2. - self.c_c) * self.C)
|
|
264
|
-
+ self.c_mu *
|
|
282
|
+
+ self.c_mu * rank_mu
|
|
265
283
|
)
|
|
266
|
-
self.C.copy_(C)
|
|
284
|
+
self.C.copy_(C.clamp(min = 1e-14))
|
|
267
285
|
else:
|
|
268
|
-
|
|
286
|
+
rank_one = einsum(pc, pc, 'i, j -> i j')
|
|
287
|
+
rank_mu = einsum(weights, y_selected, y_selected, 'p, p i, p j -> i j')
|
|
269
288
|
|
|
270
289
|
C = (
|
|
271
290
|
(1. - self.c_1 - self.c_mu) * self.C
|
|
272
|
-
+ self.c_1 * (
|
|
291
|
+
+ self.c_1 * (rank_one + (1. - h_sigma) * self.c_c * (2. - self.c_c) * self.C)
|
|
273
292
|
+ self.c_mu * rank_mu
|
|
274
293
|
)
|
|
275
294
|
self.C.copy_(0.5 * (C + C.T))
|
|
@@ -292,6 +311,9 @@ class _CMAState:
|
|
|
292
311
|
|
|
293
312
|
self.generation = generation
|
|
294
313
|
|
|
314
|
+
if not self.sep_cma and (self.generation - self._eigen_generation) >= self.eigen_interval:
|
|
315
|
+
self._update_eigensystem_()
|
|
316
|
+
|
|
295
317
|
@torch.no_grad()
|
|
296
318
|
def add_to_base_(self, name, delta):
|
|
297
319
|
base = self.base_params[name]
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|