-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisland.py
464 lines (403 loc) · 14.9 KB
/
island.py
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
import dataclasses
import json
import logging
import multiprocessing
import pickle
import queue
from datetime import datetime
from typing import Dict, Optional, Text, Tuple
import numpy as np
from mpi4py import MPI
import configuration
import corpora
import genetic_algorithm
import network
import utils
_MPI_COMMUNICATOR = MPI.COMM_WORLD
_MPI_RANK = _MPI_COMMUNICATOR.Get_rank()
_MPI_LOGGING_BUFFER_SIZE = 5_000_000
def _island_process(
island_num: int,
config: configuration.SimulationConfig,
corpus: corpora.Corpus,
result_queue: multiprocessing.Queue,
logging_queue: multiprocessing.Queue,
process_locks: Tuple[multiprocessing.Lock, ...],
):
try:
result_network = genetic_algorithm.run(
island_num=island_num,
corpus=corpus,
config=config,
logging_queue=logging_queue,
process_locks=process_locks,
)
result_queue.put((island_num, result_network))
except:
logging.exception(f"Exception in island {island_num}:")
class _DummyProcess:
# Used for profiling without multiprocessing.
def __init__(self, target, kwargs):
self._target = target
self._kwargs = kwargs
self.__setattr__ = lambda x: x
def start(self):
self._target(**self._kwargs)
def _log_net_stats(
stats: Dict,
config: configuration.SimulationConfig,
train_corpus: corpora.Corpus,
test_corpus: Optional[corpora.Corpus],
best_net: network.Network,
status: Text,
):
stats.update(
{
"simulation": config.simulation_id,
"simulation id": config.simulation_id,
"configuration": json.dumps(config.__dict__),
"params": config.comment,
"status": status,
"best island": stats["island"],
"last update": datetime.now().isoformat(),
}
)
log_text = (
f"Current best net, island {stats['island']}, generation {stats['generation']}: "
f"MDL = {stats['mdl']:,.2f}, |G| = {stats['|g|']}, |D:G| = {stats['|d:g|']:,.2f}, units: {stats['units']}, connections: {stats['connections']}."
)
train_num_chars = corpora.get_num_chars_in_corpus(train_corpus)
average_train_dg_per_char = best_net.fitness.data_encoding_length / train_num_chars
stats.update(
{
"average train d:g per character": f"{average_train_dg_per_char:.2f}",
"training set num. chars": f"{train_num_chars}",
}
)
if train_corpus.deterministic_steps_mask is not None:
train_deterministic_accuracy = network.calculate_deterministic_accuracy(
best_net, train_corpus, config
)
stats.update({"deterministic accuracy": train_deterministic_accuracy})
log_text += (
f" Training set deterministic accuracy: {train_deterministic_accuracy:,.2f}"
)
if test_corpus is not None:
test_net = network.invalidate_fitness(best_net)
test_net = network.calculate_fitness(
test_net, corpus=test_corpus, config=config
)
test_num_chars = corpora.get_num_chars_in_corpus(test_corpus)
average_test_dg_per_char = (
test_net.fitness.data_encoding_length / test_num_chars
)
stats.update(
{
"test set d:g": f"{test_net.fitness.data_encoding_length:.2f}",
"test set accuracy": f"{test_net.fitness.accuracy}",
"average test d:g per character": f"{average_test_dg_per_char:.2f}",
}
)
log_text += f" Test set |D:G|: {test_net.fitness.data_encoding_length:,.2f}."
if test_corpus.deterministic_steps_mask is not None:
test_deterministic_accuracy = network.calculate_deterministic_accuracy(
test_net, test_corpus, config
)
stats.update(
{"test set deterministic accuracy": test_deterministic_accuracy}
)
log_text += (
f" Test set deterministic accuracy: {test_deterministic_accuracy:,.2f}"
)
logging.info(log_text)
current_best_net_filename = f"{config.simulation_id}__current_best"
network.save(best_net, current_best_net_filename)
network.visualize(
best_net,
current_best_net_filename,
class_to_label=test_corpus.vocabulary if test_corpus else None,
)
def _mpi_logging_worker(
config: configuration.SimulationConfig,
train_corpus: corpora.Corpus,
test_corpus: Optional[corpora.Corpus],
):
best_mdl = float("inf")
buffer = np.empty(7)
logging.info(f"Started MPI logging worker, rank {_MPI_RANK}")
while True:
_MPI_COMMUNICATOR.Recv(buffer, tag=utils.MPI_LOGGING_TAG)
(
island_num,
generation,
mdl,
grammar_encoding_length,
data_encoding_length,
num_units,
connections,
) = buffer
island_num = int(island_num)
if mdl < best_mdl:
best_mdl = mdl
with open(
f"./networks/{config.simulation_id}__island_{island_num}__best_network.pickle",
"rb",
) as f:
best_net = pickle.load(f)
best_island_stats = {
"island": island_num,
"generation": int(generation),
"mdl": mdl,
"|g|": grammar_encoding_length,
"|d:g|": data_encoding_length,
"units": int(num_units),
"connections": int(connections),
"accuracy": best_net.fitness.accuracy,
}
_log_net_stats(
stats=best_island_stats,
config=config,
train_corpus=train_corpus,
test_corpus=test_corpus,
best_net=best_net,
status="Running",
)
def _queue_logging_worker(
logging_queue: multiprocessing.Queue,
config: configuration.SimulationConfig,
train_corpus: corpora.Corpus,
test_corpus: Optional[corpora.Corpus],
):
best_net_fitness = float("inf")
while True:
island_num_to_data = {}
start_time = datetime.now()
while (datetime.now() - start_time).total_seconds() < 60:
try:
data = logging_queue.get(timeout=5)
island_num = data["stats"]["island"]
island_num_to_data[island_num] = data
except queue.Empty:
pass
best_island_data = None
for data in island_num_to_data.values():
stats = data["stats"]
if stats["mdl"] <= best_net_fitness:
best_net_fitness = stats["mdl"]
best_island_data = data
if best_island_data is None:
continue
_log_net_stats(
stats=best_island_data["stats"],
config=config,
train_corpus=train_corpus,
test_corpus=test_corpus,
best_net=best_island_data["best_net"],
status="Running",
)
def _create_csv_and_spreadsheet_entry(
config: configuration.SimulationConfig, train_corpus, test_corpus
):
data = {
"simulation": config.simulation_id,
"simulation id": config.simulation_id,
"params": config.comment,
"configuration": json.dumps(config.__dict__),
"status": "Running",
"started": datetime.now().isoformat(),
"seed": config.seed,
}
train_num_chars = corpora.get_num_chars_in_corpus(train_corpus)
data.update(
{"training set num. chars": f"{train_num_chars}",}
)
if train_corpus.optimal_d_given_g is not None:
data.update(
{
"training set optimal d:g": f"{train_corpus.optimal_d_given_g:.2f}",
"optimal average train d:g per character": f"{train_corpus.optimal_d_given_g / train_num_chars:.2f}",
}
)
if test_corpus is not None:
test_num_chars = corpora.get_num_chars_in_corpus(test_corpus)
data.update(
{
"test set params": f"Input shape: {test_corpus.input_sequence.shape}. Output shape: {test_corpus.target_sequence.shape}",
"test set num. chars": f"{test_num_chars}",
}
)
if test_corpus.optimal_d_given_g is not None:
data.update(
{
"test set optimal d:g": f"{test_corpus.optimal_d_given_g:.2f}",
"optimal average test d:g per character": f"{test_corpus.optimal_d_given_g / test_num_chars:.2f}",
}
)
def _init_islands(
first_island: int,
last_island: int,
config: configuration.SimulationConfig,
train_corpus,
result_queue: multiprocessing.Queue,
logging_queue: multiprocessing.Queue,
process_locks: Tuple[multiprocessing.Lock, ...],
) -> Tuple[multiprocessing.Process, ...]:
processes = []
for i in range(first_island, last_island + 1):
if config.parallelize:
process_class = multiprocessing.Process
else:
process_class = _DummyProcess
p = process_class(
target=_island_process,
kwargs={
"island_num": i,
"config": config,
"corpus": train_corpus,
"result_queue": result_queue,
"logging_queue": logging_queue,
"process_locks": process_locks,
},
)
p.daemon = True
processes.append(p)
return tuple(processes)
def _get_island_results_from_queue_or_mpi(result_queue: multiprocessing.Queue):
if result_queue is not None:
return result_queue.get()
return _MPI_COMMUNICATOR.recv(tag=utils.MPI_RESULTS_TAG)
def _collect_results(
num_islands_to_collect: int,
result_queue: multiprocessing.Queue,
config: configuration.SimulationConfig,
train_corpus: corpora.Corpus,
test_corpus: Optional[corpora.Corpus],
):
result_network_per_island = {}
while len(result_network_per_island) < num_islands_to_collect:
island_num, result_net = _get_island_results_from_queue_or_mpi(
result_queue=result_queue
)
result_network_per_island[island_num] = result_net
logging.info(f"Island {island_num} best network:\n{str(result_net)}")
logging.info(
f"{len(result_network_per_island)}/{num_islands_to_collect} (global {config.num_islands}) islands done."
)
best_island, best_net = min(
result_network_per_island.items(), key=lambda x: x[1].fitness.mdl
)
network_filename = f"{config.simulation_id}__best_network"
network.visualize(
best_net, network_filename, class_to_label=train_corpus.vocabulary
)
network.save(best_net, network_filename)
logging.info(f"Best network of all islands:\n{str(best_net)}")
csv_data = {
"simulation id": config.simulation_id,
"island": best_island,
"mdl": best_net.fitness.mdl,
"|g|": best_net.fitness.grammar_encoding_length,
"|d:g|": best_net.fitness.data_encoding_length,
"units": network.get_num_units(best_net),
"connections": network.get_total_connections(best_net, include_biases=True),
"accuracy": best_net.fitness.accuracy,
"generation": config.num_generations,
"finished": datetime.now().isoformat(),
}
_log_net_stats(
stats=csv_data,
config=config,
train_corpus=train_corpus,
test_corpus=test_corpus,
best_net=best_net,
status="Done",
)
def _run_mpi_island(island_num, corpus, config):
result_network = genetic_algorithm.run(
island_num=island_num,
corpus=corpus,
config=config,
logging_queue=None,
process_locks=None,
)
_MPI_COMMUNICATOR.send(
(island_num, result_network),
dest=config.num_islands + 1,
tag=utils.MPI_RESULTS_TAG,
)
while True:
# TODO: need to keep running this so buffer won't fill. need to find a better solution.
genetic_algorithm.get_migrants_through_mpi()
def run(
corpus: corpora.Corpus,
config: configuration.SimulationConfig,
first_island: int,
last_island: int,
):
train_corpus = corpora.optimize_for_feeding(
dataclasses.replace(corpus, test_corpus=None)
)
test_corpus = (
corpora.optimize_for_feeding(corpus.test_corpus) if corpus.test_corpus else None
)
genetic_algorithm.verify_existing_simulation_override(
simulation_id=config.simulation_id,
)
logging.info(f"Starting simulation {config.simulation_id}\n")
logging.info(f"Config: {config}\n")
logging.info(
f"Running islands {first_island}-{last_island} ({last_island-first_island+1}/{config.num_islands})"
)
if config.migration_channel == "file":
genetic_algorithm.get_migration_path(config.simulation_id).mkdir(
parents=True, exist_ok=True
)
if first_island == 0:
_create_csv_and_spreadsheet_entry(config, train_corpus, test_corpus)
result_queue = multiprocessing.Queue()
logging_queue = multiprocessing.Queue()
process_locks = tuple(multiprocessing.Lock() for _ in range(config.num_islands))
island_processes = _init_islands(
first_island=first_island,
last_island=last_island,
config=config,
train_corpus=train_corpus,
result_queue=result_queue,
logging_queue=logging_queue,
process_locks=process_locks,
)
for p in island_processes:
p.start()
logging_process = multiprocessing.Process(
target=_queue_logging_worker,
args=(logging_queue, config, train_corpus, test_corpus),
)
logging_process.daemon = True
logging_process.start()
_collect_results(
num_islands_to_collect=len(island_processes),
result_queue=result_queue,
config=config,
train_corpus=train_corpus,
test_corpus=test_corpus,
)
elif config.migration_channel == "mpi":
if _MPI_RANK < config.num_islands:
# Ranks [0, num_islands - 1] = islands
_run_mpi_island(
island_num=_MPI_RANK, corpus=train_corpus, config=config,
)
elif _MPI_RANK == config.num_islands:
# Rank num_islands = logger
_create_csv_and_spreadsheet_entry(config, train_corpus, test_corpus)
_mpi_logging_worker(config, train_corpus, test_corpus)
elif _MPI_RANK == config.num_islands + 1:
# Rank num_island + 1 = results collector
logging.info(f"Starting results collection, rank {_MPI_RANK}")
_collect_results(
num_islands_to_collect=config.num_islands,
result_queue=None,
config=config,
train_corpus=train_corpus,
test_corpus=test_corpus,
)