-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_sweep_temp.py
More file actions
362 lines (305 loc) · 14.1 KB
/
main_sweep_temp.py
File metadata and controls
362 lines (305 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import os
import csv
os.environ['XLA_PYTHON_CLIENT_PREALLOCATE'] = 'false'
import argparse
from functools import partial
import pickle
import csv
import jax
import jax.numpy as jnp
from jax.random import split
import numpy as np
import evosax
from tqdm.auto import tqdm
import imageio
import asal.substrates as substrates
import asal.foundation_models as foundation_models
from asal.rollout import rollout_simulation
import asal.asal_metrics as asal_metrics
import asal.util as util
# from asal.wandb_util import WandbLogger
# Gemma 3
from asal_pytorch.foundation_models.gemma3 import Gemma3Chat
parser = argparse.ArgumentParser()
group = parser.add_argument_group("meta")
group.add_argument("--seed", type=int, default=0, help="the random seed")
group.add_argument("--save_dir", type=str, default=None, help="path to save results to")
group.add_argument("--wandb", action="store_true", help="log to wandb; default false unless flag is given")
group = parser.add_argument_group("substrate")
group.add_argument("--substrate", type=str, default='boids', help="name of the substrate")
group.add_argument("--rollout_steps", type=int, default=None, help="number of rollout timesteps")
group = parser.add_argument_group("evaluation")
group.add_argument("--foundation_model", type=str, default="clip", help="foundation model to use")
group.add_argument("--time_sampling", type=int, default=1,
help="images to render during one simulation rollout")
group.add_argument("--prompts", type=str, default="a biological cell;two biological cells",
help="the initial prompts (we only use the first as the 'original' prompt #1)")
group.add_argument("--coef_prompt", type=float, default=1., help="coefficient for ASAL prompt loss")
group.add_argument("--coef_softmax", type=float, default=0.,
help="coefficient for softmax loss (used for multiple temporal prompts)")
group.add_argument("--coef_oe", type=float, default=0., help="coefficient for open-endedness loss")
group = parser.add_argument_group("optimization")
group.add_argument("--bs", type=int, default=1, help="number of init states to average simulation over")
group.add_argument("--pop_size", type=int, default=16, help="population size for Sep-CMA-ES")
group.add_argument("--n_iters", type=int, default=1000, help="CMA-ES steps per iteration")
group.add_argument("--sigma", type=float, default=0.1, help="mutation rate")
group.add_argument("--N", type=int, default=3, help="total number of Gemma loops")
group.add_argument("--max_images", type=int, default=10, help="Number of image to give to Foundation Model")
def parse_args(*args, **kwargs):
args = parser.parse_args(*args, **kwargs)
for k, v in vars(args).items():
if isinstance(v, str) and v.lower() == "none":
setattr(args, k, None)
return args
def load_best_params(save_dir, iteration_idx=None):
"""Load best member from best.pkl."""
if iteration_idx is not None:
best_path = os.path.join(save_dir, f"best_{iteration_idx}.pkl")
else:
best_path = os.path.join(save_dir, "best.pkl")
with open(best_path, "rb") as f:
data = pickle.load(f)
# data[0] => best_member, data[1] => best_fitness
return data[0]
def run_for_iteration(
args,
rng,
iteration_idx,
prompt_list, # all prompts for iteration i
save_dir=None,
init_params=None,
wandb_logger=None
):
"""
We do time_sampling = len(prompt_list).
That means each time chunk in the same rollout is matched to a different prompt.
Returns best_params, video_frames, updated rng.
"""
print(f"\n=== Iteration {iteration_idx} with prompts:", prompt_list, "===")
# 1) Prepare foundation model & substrate
fm = foundation_models.create_foundation_model(args.foundation_model)
substrate = substrates.create_substrate(args.substrate)
substrate = substrates.FlattenSubstrateParameters(substrate)
if args.rollout_steps is None:
args.rollout_steps = substrate.rollout_steps
# Initialise wandb logging
if args.wandb and iteration_idx==0:
wandb_logger.initialise_rollout(substrate=substrate)
# 2) We'll have as many prompts as we have time chunks
n_prompts = len(prompt_list)
# We'll override the user's time_sampling for the rollout,
# so that each chunk can be matched to one prompt.
rollout_fn = partial(
rollout_simulation,
s0=None,
substrate=substrate,
fm=fm,
rollout_steps=args.rollout_steps,
time_sampling=(n_prompts, True), # e.g. (2, True) if 2 prompts
img_size=224,
return_state=False
)
# embed all prompts at once => shape (P, D), where P = n_prompts
z_txt = fm.embed_txt(prompt_list)
# 3) CMA-ES setup
if rng is None:
rng = jax.random.PRNGKey(args.seed)
rng, init_rng = split(rng)
strategy = evosax.Sep_CMA_ES(
popsize=args.pop_size,
num_dims=substrate.n_params,
sigma_init=args.sigma
)
es_params = strategy.default_params
es_state = strategy.initialize(init_rng, es_params)
# If continuing from previous iteration, set the state's mean to init_params
if init_params is not None:
es_state = es_state.replace(mean=init_params)
# define the CMA-ES loss
def calc_loss(rng_in, candidate_params):
rollout_data = rollout_fn(rng_in, candidate_params)
z = rollout_data['z'] # shape: (n_prompts, embedding_dim)
# For multiple prompts, we measure how each time chunk's embedding matches
# its corresponding prompt in z_txt. Inside asal, we can do e.g.:
loss_prompt = asal_metrics.calc_supervised_target_score(z, z_txt)
loss_softmax = asal_metrics.calc_supervised_target_softmax_score(z, z_txt)
loss_oe = asal_metrics.calc_open_endedness_score(z)
# Weighted sum
loss_val = (loss_prompt * args.coef_prompt
+ loss_softmax * args.coef_softmax
+ loss_oe * args.coef_oe)
return loss_val, dict(
loss=loss_val,
loss_prompt=loss_prompt,
loss_softmax=loss_softmax,
loss_oe=loss_oe
)
@jax.jit
def do_iter(es_state_inner, rng_inner):
rng_inner, ask_rng = split(rng_inner)
params, next_es_state = strategy.ask(ask_rng, es_state_inner, es_params)
calc_loss_vv = jax.vmap(
jax.vmap(calc_loss, in_axes=(0, None)),
in_axes=(None, 0)
)
rng_inner, batch_rng = split(rng_inner)
losses, loss_dicts = calc_loss_vv(split(batch_rng, args.bs), params)
# Average across the 'bs' dimension
losses, loss_dicts = jax.tree_map(lambda x: x.mean(axis=1), (losses, loss_dicts))
next_es_state = strategy.tell(params, losses, next_es_state, es_params)
data_out = {
'best_loss': next_es_state.best_fitness,
'loss_dict': loss_dicts
}
return next_es_state, data_out, rng_inner
# 4) CMA-ES main loop
data_log = []
pbar = tqdm(range(args.n_iters), desc=f"Iteration {iteration_idx}")
for i in pbar:
es_state, di, rng = do_iter(es_state, rng)
data_log.append(di)
pbar.set_postfix(best_loss=es_state.best_fitness.item())
# occasionally save
if save_dir is not None and (i == args.n_iters - 1 or i % max(1, args.n_iters // 10) == 0):
data_save = jax.tree_map(lambda *x: np.array(jnp.stack(x, axis=0)), *data_log)
util.save_pkl(save_dir, f"data_{iteration_idx}", data_save)
best_blob = jax.tree_map(lambda x: np.array(x), (es_state.best_member, es_state.best_fitness))
util.save_pkl(save_dir, f"best_{iteration_idx}", best_blob)
# after done with n_iters, load the best params from disk
if save_dir:
best_params = load_best_params(save_dir, iteration_idx)
else:
best_params = es_state.best_member
# produce final frames from best params
rollout_fn_video = partial(
rollout_simulation,
s0=None,
substrate=substrate,
fm=None, # no need to embed again
rollout_steps=args.rollout_steps,
time_sampling='video',
img_size=224,
return_state=False
)
rollout_data = rollout_fn_video(rng, best_params)
rgb = np.array(rollout_data['rgb'])
video_frames = (rgb * 255).clip(0, 255).astype(np.uint8)
return best_params, video_frames, rng, data_log
def save_final_prompts_csv(all_prompts, folder):
"""Saves the list of prompts to 'final_prompts.csv' in the given folder."""
csv_path = os.path.join(folder, "final_prompts.csv")
with open(csv_path, "w", newline="", encoding="utf-8") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["prompt"])
for p in all_prompts:
writer.writerow([p])
print(f"Final prompts saved at: {csv_path}")
# Show final video to Gemma => get new prompt
# EVOLVE_INSTRUCTION ="""You just saw the video for iteration {i}, which used prompts so far: {all_prompts}. "
# "Suggest a NEW single prompt (no extra text) to expand upon this evolution next time."""
def main(args):
"""
1) We keep a dynamic list of prompts: all_prompts
2) On iteration i, we pass the *first i prompts* to the CMA run, so time_sampling=i.
That means the single rollout is chunked into i segments, each matched to one prompt.
3) We produce a final video for iteration i, then feed it to Gemma for the next prompt.
4) Over N iterations, we have i=1..N => 1 + 2 + ... + N total prompts appended across iterations.
"""
# Add evolve instruction to args for logging
# args.evolve_instruction = EVOLVE_INSTRUCTION.format(current_prompt="current_prompt")
gemma = Gemma3Chat()
splitted = args.prompts.split(";")
if len(splitted) < 1:
splitted = ["a default prompt"]
# Start with just the first prompt
all_prompts = [splitted[0]]
TEMP_SWEEP = [0.01, 0.1, 0.3, 0.5, 0.7, 1.0, 2.0, 5.0]
for temp in TEMP_SWEEP:
rng = None
current_params = None
final_video_paths = []
save_dir = os.path.join(args.save_dir, f"temp_{temp}")
os.makedirs(save_dir, exist_ok=True)
# Initialise wandb logging
# if args.wandb:
# wandb_logger = WandbLogger(project="alife-project", group="evolutionary-prompting", entity="ucl-asal", config=vars(args))
# wandb_logger.initialise_prompt_logging()
# wandb_logger.log_prompt(args.prompts)
# else:
# wandb_logger = None
for i in range(1, args.N + 1):
print(f"\n=== Starting iteration {i} ===")
# We have i prompts so far: all_prompts[:i]
# We'll run 1 CMA-ES loop with time_sampling = i
# i.e. each chunk in the same rollout matches one prompt
prompts_for_i = all_prompts[:i]
best_params, video_frames, rng, data_log = run_for_iteration(
args,
rng=rng,
iteration_idx=i,
prompt_list=prompts_for_i,
init_params=current_params,
save_dir=save_dir,
# wandb_logger=wandb_logger
)
# Save final iteration video
if args.save_dir:
video_path_i = os.path.join(save_dir, f"video_iteration_{i}.mp4")
imageio.mimsave(video_path_i, video_frames, fps=30, codec="libx264")
print(f"[Iteration {i}] final video saved at: {video_path_i}")
final_video_paths.append(video_path_i)
# First pass to collect all flat keys
flat_keys = []
for key in data_log[0]["loss_dict"].keys():
val = data_log[0]["loss_dict"][key]
if isinstance(val, np.ndarray) and val.ndim > 0:
flat_keys.extend([f"{key}_{i}" for i in range(val.size)])
else:
flat_keys.append(key)
# Write the header
header = ["iteration", "best_loss"] + flat_keys
with open(os.path.join(save_dir, f"losses_{i}.csv"), "w") as f:
writer = csv.writer(f)
writer.writerow(header)
for idx, d in enumerate(data_log):
row = [idx, d["best_loss"]]
for key in d["loss_dict"]:
val = d["loss_dict"][key]
if isinstance(val, np.ndarray):
val = val.flatten()
row.extend(val.tolist())
else:
row.append(val)
writer.writerow(row)
# # Show final video to Gemma => get new prompt
instruction =(f"You just saw the video for iteration {i}, which used prompts so far: {all_prompts}. "
"Suggest a NEW single prompt (no extra text) to expand upon this evolution next time."
)
instruction = instruction.format(i=i, all_prompts=all_prompts)
new_prompt = gemma.describe_video(
video_frames,
extract_prompt=instruction,
max_tokens=20,
temperature=temp,
max_images=args.max_images,
)
print(f"[Iteration {i}] Gemma suggested => '{new_prompt}'")
# Log the prompt file and text to wandb
# if args.wandb:
# wandb_logger.log_prompt(new_prompt)
# Append new prompt to our list => next iteration will have i+1 total prompts
all_prompts.append(new_prompt)
# Our final best_params becomes the init for next iteration
current_params = best_params
if save_dir:
save_final_prompts_csv(all_prompts, save_dir)
# Save the given prompt, generated prompt and similarity score
with open(os.path.join(save_dir, "results.txt"), "w") as f:
# Write all args
f.write("Arguments:\n")
for arg, value in vars(args).items():
f.write(f"{arg}: {value}\n")
# Reset the prompts for the next temperature
all_prompts = [splitted[0]]
if __name__ == '__main__':
main(parse_args())