-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_eval.py
More file actions
186 lines (148 loc) · 5.39 KB
/
main_eval.py
File metadata and controls
186 lines (148 loc) · 5.39 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
import os
import pickle
import time
from functools import partial
import hydra
import jax
import jax.numpy as jnp
import pandas as pd
import wandb
from jax.sharding import Mesh, NamedSharding, PartitionSpec
from omegaconf import DictConfig, OmegaConf
from learned_qd.evo.evolution import Evolution
from learned_qd.evo.genetic_algorithm import GeneticAlgorithm
from learned_qd.evo.metrics import metrics_agg_fn, metrics_fn
from learned_qd.utils.helpers import get_config_and_model_path
from learned_qd.utils.plot import plot_evolution
@hydra.main(config_path="configs", config_name="eval", version_base=None)
def main(config: DictConfig) -> None:
wandb.init(
project="Learned-QD",
name=f"eval-{os.getcwd().split('/')[-1]}",
tags=["eval"] + config.tags,
config=OmegaConf.to_container(config, resolve=True),
mode="online" if config.wandb else "disabled",
)
key = jax.random.key(config.seed)
num_devices = config.num_devices or jax.device_count()
assert num_devices <= jax.device_count(), (
f"Requested {num_devices} devices, but only {jax.device_count()} available."
)
devices = jax.devices()[:num_devices]
mesh = Mesh(devices, ("devices",))
# Define sharding specifications
parallel_sharding = NamedSharding(mesh, PartitionSpec("devices"))
# Task
key, subkey = jax.random.split(key)
task = hydra.utils.instantiate(config.task)
x = task.sample_x(subkey)
genotype_count = sum(x.size for x in jax.tree.leaves(x))
wandb.log({"genotype_count": genotype_count})
# Population
if config.evo.name == "lqd":
config_run, model_path = get_config_and_model_path(config.evo.run_path)
config.evo.population.learned_fitness = config_run.evo.population.learned_fitness
wandb.config.update(OmegaConf.to_container(config, resolve=True), allow_val_change=True)
key, subkey = jax.random.split(key)
population = hydra.utils.instantiate(config.evo.population)(
x,
subkey,
descriptor_size=task.descriptor_size,
)
# Load params
with open(os.path.join(model_path, "params.pickle"), "rb") as f:
params = pickle.load(f)
population = population.replace(params=params)
params_count = sum(x.size for x in jax.tree.leaves(params))
else:
key, subkey = jax.random.split(key)
population = hydra.utils.instantiate(config.evo.population)(
x,
subkey,
descriptor_size=task.descriptor_size,
)
params_count = 0
wandb.log({"params_count": params_count})
# Reproduction
emitter = hydra.utils.instantiate(config.evo.reproduction)(
minval=task.x_range[0],
maxval=task.x_range[1],
)
# Genetic Algorithm
ga = GeneticAlgorithm(
emitter=emitter,
metrics_fn=metrics_fn,
)
# Evolution
evo = Evolution(ga, task)
# Run
key, subkey = jax.random.split(key)
keys = jax.random.split(subkey, config.num_evaluations)
keys = jax.device_put(keys, parallel_sharding)
evo_state, metrics_init = jax.vmap(evo.init, in_axes=(None, 0))(population, keys)
# Log
fitness_all = [jnp.expand_dims(metrics_init["fitness"][0], axis=0)]
descriptor_all = [jnp.expand_dims(metrics_init["descriptor"][0], axis=0)]
current_metrics_df = pd.DataFrame(
{
"batch_id": range(config.num_evaluations),
"generation": [0] * config.num_evaluations,
**metrics_agg_fn(metrics_init),
"time": [0.0] * config.num_evaluations,
"iterations_per_second": [0.0] * config.num_evaluations,
}
)
metrics_df = current_metrics_df.copy()
metrics_agg = (
current_metrics_df.drop(columns="batch_id").groupby("generation").mean().reset_index()
)
metrics_agg.to_csv("./metrics.csv", index=False)
wandb.log(metrics_agg.to_dict("records")[0])
for i in range(config.num_generations // config.log_every):
start_time = time.time()
evo_state, metrics = jax.vmap(partial(evo.evolve, num_generations=config.log_every))(
evo_state, metrics_init
)
time_elapsed = time.time() - start_time
# Log
fitness_all.append(metrics["fitness"][0])
descriptor_all.append(metrics["descriptor"][0])
current_metrics_df = pd.DataFrame(
{
"batch_id": [
i for i in range(config.num_evaluations) for _ in range(config.log_every)
],
"generation": [
gen
for _ in range(config.num_evaluations)
for gen in range(1 + i * config.log_every, 1 + (i + 1) * config.log_every)
],
**{k: v.reshape(-1) for k, v in metrics_agg_fn(metrics).items()},
"time": [time_elapsed] * (config.num_evaluations * config.log_every),
"iterations_per_second": [config.log_every / time_elapsed]
* (config.num_evaluations * config.log_every),
}
)
metrics_df = pd.concat([metrics_df, current_metrics_df])
metrics_agg = (
current_metrics_df.drop(columns="batch_id").groupby("generation").mean().reset_index()
)
metrics_agg.to_csv("./metrics.csv", mode="a", header=False, index=False)
wandb.log(metrics_agg.to_dict("records")[-1])
with open("./metrics.pickle", "wb") as f:
pickle.dump(metrics_df, f)
with open("./population.pickle", "wb") as f:
pickle.dump(evo_state.population, f)
fitness_all = jnp.concatenate(fitness_all)
descriptor_all = jnp.concatenate(descriptor_all)
if config.log_evolution and task.descriptor_size == 2:
anim = plot_evolution(fitness_all, descriptor_all)
anim.save("./evolution.mp4", writer="ffmpeg")
wandb.log({"evolution": wandb.Video("./evolution.mp4")})
if config.log_fitness_descriptor:
with open("./fitness_all.pickle", "wb") as f:
pickle.dump(fitness_all, f)
with open("./descriptor_all.pickle", "wb") as f:
pickle.dump(descriptor_all, f)
if __name__ == "__main__":
main()