-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
1469 lines (1353 loc) · 65.7 KB
/
main.py
File metadata and controls
1469 lines (1353 loc) · 65.7 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
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
import argparse
import configparser
import copy
import csv
import hashlib
import itertools
import json
import logging
import math
import os
import re
import statistics
import tempfile
from contextlib import nullcontext
from datetime import datetime
from pathlib import Path
from warnings import warn
import neat
import torch
import torch.nn as nn
from neat.reporting import BaseReporter
from computation_graphs.functions.activation import *
from computation_graphs.functions.aggregation import *
from genes import *
from graph_ir import export_script_module_to_graph_ir
from loop_blocks import (
DEFAULT_BLOCK_PAYLOAD_VALUE_DIM,
encode_block_payload,
register_graph_blocks,
snapshot_registry,
)
from population import *
from population import _INVALID_REASON_COUNTER
from relative_rank_stagnation import RelativeRankStagnation
from reproduction import *
from torchscript_utils import serialize_script_module
from utility import MemoryUsageTracker, log_timing
GUIDED_POPULATION_SECTION = "GuidedPopulation"
logger = logging.getLogger(__name__)
def _parse_bool(value: str) -> bool:
lowered = value.strip().lower()
if lowered in {"1", "true", "yes", "on"}:
return True
if lowered in {"0", "false", "no", "off"}:
return False
raise ValueError(f"Expected boolean value for GuidedPopulation override, got '{value}'")
GUIDED_POPULATION_FIELDS = {
"kl_partial_slice_ratio": float,
"kl_partial_slice_dims": int,
"kl_partial_slice_start": int,
"wl_kernel_loss_weight": float,
"wl_kernel_iterations": int,
"trainer_freeze_cycle": str,
"trainer_freeze_verbose": _parse_bool,
}
LOG_LEVEL_CHOICES = ("CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG")
def _configure_root_logger(log_level: str) -> None:
"""Initialize the root logger according to the CLI-provided level."""
level = logging.getLevelName(log_level.upper())
if not isinstance(level, int):
level = logging.INFO
logging.basicConfig(
level=level,
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
force=True,
)
def load_guided_population_overrides(config_path: str | os.PathLike[str]) -> dict[str, object]:
"""Parse optional GuidedPopulation overrides from the NEAT config file."""
parser = configparser.ConfigParser()
try:
read_files = parser.read(config_path)
except configparser.Error as exc: # pragma: no cover - configparse guard
raise ValueError(f"Failed to parse config file '{config_path}': {exc}") from exc
if not read_files or not parser.has_section(GUIDED_POPULATION_SECTION):
return {}
section = parser[GUIDED_POPULATION_SECTION]
overrides: dict[str, object] = {}
for key, caster in GUIDED_POPULATION_FIELDS.items():
if key not in section:
continue
raw_value = section.get(key, "").strip()
if raw_value == "":
continue
try:
overrides[key] = caster(raw_value)
except ValueError as exc:
raise ValueError(
f"Invalid value '{raw_value}' for {key} in [{GUIDED_POPULATION_SECTION}] of {config_path}"
) from exc
return overrides
def _encode_string_sequence(values):
tokens = [str(v) for v in values if v is not None]
if not tokens:
return None
hashed = []
for token in tokens:
digest = hashlib.sha256(token.encode("utf-8")).digest()
hashed.append(int.from_bytes(digest[:4], byteorder="little") / 0xFFFFFFFF)
return torch.tensor(hashed, dtype=torch.float32)
def _annotate_node_for_speciation(gene: NodeGene, node: torch._C.Node) -> None:
"""Attach deterministic metadata so speciation can distinguish identical graphs."""
if node is None:
return
attrs = gene.dynamic_attributes
attrs["__node_kind__"] = node.kind()
scope = node.scopeName()
if scope:
attrs["__scope__"] = scope
outputs = list(node.outputs())
attrs["__num_outputs__"] = len(outputs)
output_tensor = _encode_string_sequence(str(out.type()) for out in outputs)
if output_tensor is not None:
attrs["__output_types__"] = output_tensor
inputs = list(node.inputs())
attrs["__num_inputs__"] = len(inputs)
if inputs:
kind_tensor = _encode_string_sequence(inp.node().kind() for inp in inputs)
if kind_tensor is not None:
attrs["__input_kinds__"] = kind_tensor
type_tensor = _encode_string_sequence(str(inp.type()) for inp in inputs)
if type_tensor is not None:
attrs["__input_types__"] = type_tensor
getattr_types = []
for inp in inputs:
src_node = inp.node()
if src_node.kind() == "prim::GetAttr" and src_node.outputsSize() > 0:
try:
getattr_types.append(str(src_node.output().type()))
except RuntimeError:
continue
getattr_tensor = _encode_string_sequence(getattr_types)
if getattr_tensor is not None:
attrs["__getattr_output_types__"] = getattr_tensor
def create_initial_genome(config, optimizer):
"""
Creates an initial genome that mirrors the structure of the provided TorchScript optimizer computation graph.
"""
graph_ir, module_state = export_script_module_to_graph_ir(optimizer)
node_block_map = register_graph_blocks(graph_ir)
block_ids = sorted({block_id for ids in node_block_map.values() for block_id in ids})
block_registry = snapshot_registry(block_ids)
payload_cap = getattr(config, "attr_value_max_dim", DEFAULT_BLOCK_PAYLOAD_VALUE_DIM)
try:
payload_cap = int(payload_cap)
except (TypeError, ValueError):
payload_cap = DEFAULT_BLOCK_PAYLOAD_VALUE_DIM
payload_cap = max(DEFAULT_BLOCK_PAYLOAD_VALUE_DIM, payload_cap)
graph_nodes = list(optimizer.graph.nodes())
node_index_map = {node: idx for idx, node in enumerate(graph_nodes)}
def _register_node(node_key: int, ts_node: torch._C.Node | None) -> None:
new_node_gene = NodeGene(node_key, ts_node)
new_node_gene.init_attributes(config.genome_config)
if ts_node is not None:
_annotate_node_for_speciation(new_node_gene, ts_node)
node_idx = node_index_map.get(ts_node)
if node_idx is not None:
block_ids_for_node = node_block_map.get(node_idx, [])
graph_block_attrs: Dict[str, Any] | None = None
for block_pos, block_id in enumerate(block_ids_for_node):
if graph_block_attrs is None:
graph_block_attrs = _graph_attrs_for_node(node_key)
ref_name = f"{BLOCK_REF_ATTR_PREFIX}{block_pos}"
ref_tensor = torch.tensor([float(block_id)], dtype=torch.float32)
new_node_gene.dynamic_attributes[ref_name] = ref_tensor
graph_block_attrs[ref_name] = ref_tensor
payload = block_registry.get(block_id)
if payload:
payload_name = f"{BLOCK_PAYLOAD_ATTR_PREFIX}{block_pos}"
try:
encoded = encode_block_payload(payload, max_value_dim=payload_cap)
except ValueError as exc:
warn(f"Skipping block payload encoding for node {node_key}: {exc}")
else:
# stash the actual block body alongside the reference so the
# decoder/repair path can round-trip nested control flow
# without consulting external templates
new_node_gene.dynamic_attributes[payload_name] = encoded
graph_block_attrs[payload_name] = encoded
genome.nodes[node_key] = new_node_gene
genome = config.genome_type(0)
genome.serialized_module = serialize_script_module(optimizer)
module_type = optimizer._c._type().qualified_name() if hasattr(optimizer._c._type(), "qualified_name") else None
genome.graph_dict = {
"graph_ir": graph_ir,
"module_state": module_state,
"module_type": module_type,
"block_registry": block_registry,
}
node_mapping = {} # from TorchScript nodes to genome node keys
node_attributes: List[Dict[str, Any]] = genome.graph_dict.setdefault("node_attributes", [])
node_attr_bindings: Dict[int, Dict[str, Any]] = {}
configured_input_keys = list(getattr(config.genome_config, "input_keys", []))
configured_output_keys = list(getattr(config.genome_config, "output_keys", []))
slot_node_candidates: set[int] = set()
for key in itertools.chain(configured_input_keys, configured_output_keys):
try:
slot_node_candidates.add(int(key))
except (TypeError, ValueError):
continue
def _ensure_attr_slot(slot_index: int) -> Dict[str, Any]:
while len(node_attributes) <= slot_index:
node_attributes.append({})
attrs = node_attributes[slot_index]
if not isinstance(attrs, dict):
attrs = {}
node_attributes[slot_index] = attrs
return attrs
def _bind_slot_to_node(slot_index: int, node_key: int) -> Dict[str, Any]:
attrs = _ensure_attr_slot(slot_index)
existing = node_attr_bindings.get(node_key)
if existing is None:
node_attr_bindings[node_key] = attrs
return attrs
if existing is attrs:
return attrs
# Preserve any previously recorded metadata on both dicts.
for key, value in attrs.items():
if key not in existing:
existing[key] = value
node_attributes[slot_index] = existing
node_attr_bindings[node_key] = existing
return existing
def _graph_attrs_for_node(node_key: int) -> Dict[str, Any]:
attrs = node_attr_bindings.get(node_key)
if attrs is None:
attrs = {}
node_attr_bindings[node_key] = attrs
if node_key not in slot_node_candidates:
node_attributes.append(attrs)
return attrs
graph_input_values = list(optimizer.graph.inputs())
graph_inputs = {val.node() for val in graph_input_values}
user_arg_nodes: List[torch._C.Node] = []
for value in graph_input_values:
ts_node = value.node()
if ts_node is None:
continue
try:
type_repr = str(value.type())
except RuntimeError:
type_repr = ""
if type_repr.startswith("__torch__.") and value.debugName() == "self":
# Skip the implicit module "self" argument.
continue
user_arg_nodes.append(ts_node)
if len(user_arg_nodes) >= len(configured_input_keys):
break
if len(user_arg_nodes) < len(configured_input_keys):
warn(
"TorchScript graph exposes fewer inputs than configured; missing pin roles may persist "
f"({len(user_arg_nodes)} found vs {len(configured_input_keys)} expected)."
)
for slot_index, (pin_key, ts_node) in enumerate(zip(configured_input_keys, user_arg_nodes)):
try:
key = int(pin_key)
except (TypeError, ValueError):
continue
_register_node(key, ts_node)
node_mapping[ts_node] = key
attrs = _bind_slot_to_node(slot_index, key)
attrs["pin_role"] = "input"
attrs["pin_slot_index"] = slot_index
output_producers: List[torch._C.Node] = []
for value in optimizer.graph.outputs():
producer = value.node()
if producer is not None:
output_producers.append(producer)
if len(output_producers) >= len(configured_output_keys):
break
if len(output_producers) < len(configured_output_keys):
warn(
"TorchScript graph exposes fewer outputs than configured; missing output pin labels may persist "
f"({len(output_producers)} found vs {len(configured_output_keys)} expected)."
)
output_attr_offset = len(configured_input_keys)
for slot_offset, (pin_key, producer) in enumerate(zip(configured_output_keys, output_producers)):
try:
key = int(pin_key)
except (TypeError, ValueError):
continue
if producer in node_mapping:
# Already registered via inputs (unexpected) or duplicates; skip.
continue
_register_node(key, producer)
node_mapping[producer] = key
attrs = _bind_slot_to_node(output_attr_offset + slot_offset, key)
attrs["pin_role"] = "output"
attrs["pin_slot_index"] = key
next_node_id = 0
for node in graph_nodes:
if node in node_mapping:
continue
while next_node_id in genome.nodes:
next_node_id += 1
_register_node(next_node_id, node)
node_mapping[node] = next_node_id
next_node_id += 1
connections = {}
innovation = 0
for node in optimizer.graph.nodes():
current_key = node_mapping[node]
for inp in node.inputs():
producer = inp.node()
# only create a connection if the producer is part of our mapping
if producer in node_mapping:
in_key = node_mapping[producer]
key = (in_key, current_key)
conn = ConnectionGene(key)
conn.enabled = True
conn.innovation = innovation
innovation += 1
connections[key] = conn
elif producer not in graph_inputs or producer.kind() != "prim::Param":
logger.warning("Missing mapping for input node [%s]", producer)
genome.connections = connections
genome.optimizer = optimizer
return genome
def override_initial_population(population, config):
"""
Overrides the initial genomes in the population with copies of the exact initial genome.
"""
with log_timing(logger, "Loading TorchScript seed optimizers") as timing:
new_population = {}
unique_paths = []
seen_hashes = set()
for fname in os.listdir("computation_graphs/optimizers/"):
if not fname.endswith(".pt"):
continue
path = f"computation_graphs/optimizers/{fname}"
with open(path, "rb") as fh:
md5_hash = hashlib.md5(fh.read()).hexdigest()
if md5_hash in seen_hashes:
continue
seen_hashes.add(md5_hash)
unique_paths.append(path)
for key, path in zip(population.population.keys(), unique_paths):
optimizer = torch.jit.load(path)
new_genome = create_initial_genome(config, optimizer)
new_genome.key = key
new_genome.optimizer_path = path
new_population[key] = new_genome
population.population = new_population
population.shared_attr_vocab.add_names(ATTRIBUTE_NAMES)
_warn_seed_limit_violations(population, new_population)
timing.set_details(f"assigned={len(new_population)} unique_seeds={len(unique_paths)}")
with log_timing(logger, "Initial speciation pass") as timing:
population.species.speciate(config, population.population, population.generation)
timing.set_details(f"species={len(population.species.species)}")
tracker = getattr(population, "memory_tracker", None)
if tracker is not None:
tracker.snapshot("post_seed_population")
class MLflowRunManager:
def __init__(
self,
*,
tracking_uri: str | None,
experiment_name: str,
run_name: str | None,
tags: dict[str, str],
nested: bool = False,
) -> None:
try:
import mlflow
except ImportError as exc: # pragma: no cover - runtime dependency injection
raise RuntimeError(
"MLflow logging requested but the mlflow package is not installed. "
"Install mlflow or omit --enable-mlflow."
) from exc
self._mlflow = mlflow
self._tracking_uri = tracking_uri
self._experiment_name = experiment_name
self._run_name = run_name or f"evolution_{datetime.utcnow().isoformat(timespec='seconds')}"
self._tags = tags or {}
self._nested = nested
self._active = False
self._log_lines: list[str] = []
self._log_dirty = False
self._log_artifact = "logs/progress.log"
def __enter__(self):
if self._tracking_uri:
self._mlflow.set_tracking_uri(self._tracking_uri)
if self._experiment_name:
self._mlflow.set_experiment(self._experiment_name)
self._mlflow.start_run(run_name=self._run_name, nested=self._nested)
self._active = True
if self._tags:
self._mlflow.set_tags(self._tags)
return self
def __exit__(self, exc_type, exc, exc_tb):
status = "FINISHED" if exc_type is None else "FAILED"
self.finish(status=status)
return False
def finish(self, status: str = "FINISHED") -> None:
if not self._active:
return
self.flush_log()
self._mlflow.end_run(status)
self._active = False
def log_params(self, params: dict[str, object]) -> None:
if not self._active or not params:
return
serializable = {k: self._serialize_param(v) for k, v in params.items() if v is not None}
if serializable:
self._mlflow.log_params(serializable)
def log_metrics(self, metrics: dict[str, object], step: int | None = None) -> None:
if not self._active or not metrics:
return
numeric = {}
for key, value in metrics.items():
if value is None:
continue
if isinstance(value, bool):
numeric[key] = int(value)
continue
try:
num = float(value)
except (TypeError, ValueError):
continue
if math.isfinite(num):
numeric[key] = num
if numeric:
self._mlflow.log_metrics(numeric, step=step)
def log_text(self, text: str, artifact_file: str) -> None:
if self._active and text:
self._mlflow.log_text(text, artifact_file)
def log_dict(self, data: dict, artifact_file: str) -> None:
if self._active and data:
self._mlflow.log_dict(data, artifact_file)
def log_artifact(self, path: str) -> None:
if self._active and os.path.exists(path):
self._mlflow.log_artifact(path)
def append_log_line(self, line: str) -> None:
if not line:
return
self._log_lines.append(line)
self._log_dirty = True
def flush_log(self) -> None:
if not self._active or not self._log_dirty:
return
payload = "\n".join(self._log_lines)
self._mlflow.log_text(payload, self._log_artifact)
self._log_dirty = False
@staticmethod
def _serialize_param(value: object) -> str | float | int:
if isinstance(value, (str, int, float)):
return value
if isinstance(value, bool):
return int(value)
return str(value)
_COMPATIBILITY_RE = re.compile(r"compatibility threshold to ([0-9]+\.?[0-9]*)", re.IGNORECASE)
class MLflowLoggingReporter(BaseReporter):
def __init__(self, run_manager: MLflowRunManager) -> None:
self._run = run_manager
self._generation = 0
self._extinction_events = 0
def start_generation(self, generation: int):
self._generation = generation
def post_evaluate(self, config, population, species, best_genome):
fitness_values = [g.fitness for g in population.values() if getattr(g, "fitness", None) is not None]
if not fitness_values:
return
best_fitness = max(fitness_values)
worst_fitness = min(fitness_values)
num_nodes, num_connections = _genome_complexity(best_genome)
mean_distance, stdev_distance = _genetic_distance_stats(config, population)
metrics = {
"population_best_fitness": best_fitness,
"population_worst_fitness": worst_fitness,
"population_mean_fitness": statistics.fmean(fitness_values),
"population_stdev_fitness": statistics.pstdev(fitness_values) if len(fitness_values) > 1 else 0.0,
"species_count": len(getattr(species, "species", {})),
"best_genome_num_nodes": num_nodes or 0,
"best_genome_num_enabled_connections": num_connections or 0,
"population_size": len(population),
}
threshold = getattr(getattr(config, "species_set_config", None), "compatibility_threshold", None)
if threshold is not None:
metrics["compatibility_threshold"] = threshold
if mean_distance is not None:
metrics["mean_genetic_distance"] = mean_distance
metrics["std_genetic_distance"] = stdev_distance or 0.0
self._run.log_metrics(metrics, step=self._generation)
if mean_distance is not None:
self._run.append_log_line(
f"Mean genetic distance {mean_distance:.3f}, standard deviation {stdev_distance or 0.0:.3f}"
)
if threshold is not None:
self._run.append_log_line(f"Compatibility threshold currently {threshold:.3f}")
self._log_species_summary(population, species, threshold)
self._run.flush_log()
def complete_extinction(self):
self._extinction_events += 1
self._run.log_metrics({"event_complete_extinction": self._extinction_events}, step=self._generation)
self._run.append_log_line("Complete extinction event detected")
self._run.flush_log()
def found_solution(self, config, generation, best):
metrics = {"event_found_solution": 1, "solution_generation": generation}
self._run.log_metrics(metrics, step=generation)
self._run.append_log_line(f"Found solution in generation {generation}")
self._run.flush_log()
def end_generation(self, config, population, species):
self._run.flush_log()
def info(self, msg):
if not msg:
return
self._run.append_log_line(f"[gen {self._generation}] {msg}")
match = _COMPATIBILITY_RE.search(msg)
if match:
try:
value = float(match.group(1))
except ValueError:
return
self._run.log_metrics({"compatibility_threshold": value}, step=self._generation)
self._run.flush_log()
def _log_species_summary(self, population, species, threshold):
species_dict = getattr(species, "species", {}) or {}
if not isinstance(species_dict, dict):
species_dict = dict(species_dict)
summary = {
"generation": self._generation,
"population_size": len(population),
"species_count": len(species_dict),
"compatibility_threshold": threshold,
"total_extinctions": self._extinction_events,
"species": [],
}
header = [
f"Population of {len(population)} members in {len(species_dict)} species:",
" ID age size fitness adj fit stag",
" ==== === ==== ========= ======= ====",
]
for sid in sorted(species_dict):
sp = species_dict[sid]
members = getattr(sp, "members", {}) or {}
if not isinstance(members, dict):
try:
members = dict(members)
except Exception:
members = {m: None for m in members}
size = len(members)
age = getattr(sp, "age", None)
fitness = getattr(sp, "fitness", None)
adj_fitness = getattr(sp, "adjusted_fitness", None)
last_improved = getattr(sp, "last_improved", None)
stagnation = None
if last_improved is not None:
stagnation = max(0, self._generation - int(last_improved))
entry = {
"species_id": sid,
"age": age,
"size": size,
"fitness": fitness,
"adjusted_fitness": adj_fitness,
"stagnation": stagnation,
}
summary["species"].append(entry)
sid_display = sid if isinstance(sid, int) else str(sid)
age_display = age if age is not None else "-"
fit_display = f"{fitness:.3f}" if isinstance(fitness, (int, float)) else "-"
adj_display = f"{adj_fitness:.3f}" if isinstance(adj_fitness, (int, float)) else "-"
stag_display = stagnation if stagnation is not None else "-"
header.append(
f"{str(sid_display):>6}"
f"{str(age_display):>5}"
f"{size:6d}"
f"{fit_display:>11}"
f"{adj_display:>9}"
f"{str(stag_display):>6}"
)
header.append(f"Total extinctions: {self._extinction_events}")
self._run.append_log_line("\n".join(header))
self._run.log_dict(summary, f"species/generation_{self._generation:05d}.json")
def _genome_complexity(genome):
if genome is None:
return (None, None)
size_attr = getattr(genome, "size", None)
if callable(size_attr):
try:
size = size_attr()
except TypeError:
size = None
if isinstance(size, tuple):
if len(size) >= 2:
return size[0], size[1]
if size:
return size[0], None
if isinstance(size, (int, float)):
return int(size), None
nodes = getattr(genome, "nodes", None)
connections = getattr(genome, "connections", None)
if nodes is not None and connections is not None:
num_enabled = sum(1 for conn in connections.values() if getattr(conn, "enabled", False))
return len(nodes), num_enabled
return (None, None)
def _genetic_distance_stats(config, population):
genomes = list(population.values())
if len(genomes) < 2:
return (None, None)
genome_config = getattr(config, "genome_config", None)
if genome_config is None:
return (None, None)
distances = []
for i in range(len(genomes)):
for j in range(i + 1, len(genomes)):
g1 = genomes[i]
g2 = genomes[j]
dist_fn = getattr(g1, "distance", None)
if dist_fn is None:
continue
try:
distance = dist_fn(g2, genome_config)
except Exception:
continue
if distance is None or not math.isfinite(distance):
continue
distances.append(float(distance))
if not distances:
return (None, None)
mean = statistics.fmean(distances)
stdev = statistics.pstdev(distances) if len(distances) > 1 else 0.0
return (mean, stdev)
def _log_trainer_history_artifacts(run_manager, history):
if run_manager is None or not history:
return
history = sorted(history, key=lambda item: item.get("step", 0))
loss_names = sorted({name for entry in history for name in entry.get("loss_terms", {}).keys()})
metric_loss_names = sorted({name for entry in history for name in entry.get("per_metric_losses", {}).keys()})
include_attr_mismatch = any(entry.get("attr_type_mismatch_skips") is not None for entry in history)
include_node_type_loss = any(entry.get("node_type_loss") is not None for entry in history)
include_node_type_tokens = any(entry.get("node_type_tokens") is not None for entry in history)
with tempfile.TemporaryDirectory() as tmpdir:
tmp_path = Path(tmpdir)
csv_path = tmp_path / "trainer_losses.csv"
header = ["step", "generation", "epoch", "total_loss"]
header.extend([f"loss_{name}" for name in loss_names])
header.extend([f"metric_loss_{name}" for name in metric_loss_names])
if include_attr_mismatch:
header.append("attr_type_mismatch_skips")
if include_node_type_loss:
header.append("node_type_loss")
if include_node_type_tokens:
header.append("node_type_tokens")
with open(csv_path, "w", newline="", encoding="utf-8") as csv_file:
writer = csv.writer(csv_file)
writer.writerow(header)
for entry in history:
row = [
entry.get("step"),
entry.get("generation"),
entry.get("epoch"),
entry.get("total_loss"),
]
for name in loss_names:
row.append(entry.get("loss_terms", {}).get(name))
for name in metric_loss_names:
row.append(entry.get("per_metric_losses", {}).get(name))
if include_attr_mismatch:
row.append(entry.get("attr_type_mismatch_skips"))
if include_node_type_loss:
row.append(entry.get("node_type_loss"))
if include_node_type_tokens:
row.append(entry.get("node_type_tokens"))
writer.writerow(row)
run_manager.log_artifact(str(csv_path))
def _warn_seed_limit_violations(population, genomes: dict[int, OptimizerGenome]):
decoder = getattr(getattr(population, "guide", None), "decoder", None)
if decoder is None:
return
max_nodes = getattr(decoder, "max_nodes", None)
max_attrs = getattr(decoder, "max_attributes_per_node", None)
try:
max_nodes = int(max_nodes) if max_nodes is not None else None
except (TypeError, ValueError):
max_nodes = None
try:
max_attrs = int(max_attrs) if max_attrs is not None else None
except (TypeError, ValueError):
max_attrs = None
if (max_nodes is None or max_nodes <= 0) and (max_attrs is None or max_attrs <= 0):
return
for genome in genomes.values():
identifier = getattr(genome, "optimizer_path", None) or f"seed_genome_{genome.key}"
if max_nodes is not None and max_nodes > 0:
node_total = len(getattr(genome, "nodes", {}))
if node_total > max_nodes:
logger.warning(
"Seed optimizer %s defines %d nodes which exceeds decoder max_nodes=%d; guided decoder caps will truncate these graphs.",
identifier,
node_total,
max_nodes,
)
if max_attrs is not None and max_attrs > 0:
violating = [
ng.key
for ng in getattr(genome, "nodes", {}).values()
if len(getattr(ng, "dynamic_attributes", {})) > max_attrs
]
if violating:
sample = ", ".join(str(key) for key in violating[:5])
logger.warning(
"Seed optimizer %s has %d node attribute sets exceeding cap (%d); example node ids: %s",
identifier,
len(violating),
max_attrs,
sample,
)
if __name__ == "__main__":
from genome import OptimizerGenome
def _parse_mlflow_tags(tag_pairs):
tags = {}
for raw in tag_pairs or []:
if not raw:
continue
if "=" not in raw:
raise ValueError(f"Invalid MLflow tag '{raw}'. Expected KEY=VALUE format.")
key, value = raw.split("=", 1)
key = key.strip()
if not key:
raise ValueError("Tag keys cannot be empty.")
tags[key] = value.strip()
return tags
def _build_arg_parser():
parser = argparse.ArgumentParser(description="Evolve optimizer computation graphs")
parser.add_argument(
"--config-file",
default="neat-config",
help="Path to the NEAT configuration file (default: neat-config)",
)
parser.add_argument(
"--num-generations",
type=int,
default=1000,
help="Number of generations to evolve (default: 1000)",
)
parser.add_argument(
"--test",
dest="test_mode",
action="store_true",
help=("Reduce trainer epochs per generation for quick smoke tests or continuous integration runs."),
)
parser.add_argument(
"--enable-mlflow",
action="store_true",
help="Enable MLflow tracking for this run.",
)
parser.add_argument(
"--mlflow-tracking-uri",
type=str,
default="http://127.0.0.1:5000",
help="Optional MLflow tracking URI (faslls back to the default client configuration).",
)
parser.add_argument(
"--mlflow-experiment",
type=str,
default="testing",
help="MLflow experiment name to use when logging is enabled.",
)
parser.add_argument(
"--mlflow-run-name",
type=str,
default=None,
help="Optional MLflow run name; defaults to a timestamped identifier.",
)
parser.add_argument(
"--mlflow-tag",
dest="mlflow_tags",
action="append",
default=[],
metavar="KEY=VALUE",
help="Additional MLflow tag to attach to the run (repeatable).",
)
parser.add_argument(
"--mlflow-nested",
action="store_true",
help="Log the run as a nested MLflow run.",
)
parser.add_argument(
"--log-level",
type=str.upper,
choices=LOG_LEVEL_CHOICES,
default="INFO",
help="Python logging level for this run (default: INFO).",
)
parser.add_argument(
"--max-evaluation-steps",
type=int,
default=None,
metavar="N",
help="Cap the number of task training epochs (optimizer evaluation steps) per generation.",
)
parser.add_argument(
"--final-population-dir",
type=str,
default="artifacts/final_population",
help=(
"Directory where a TorchScript-friendly snapshot of the final population will be saved "
"(default: artifacts/final_population)."
),
)
parser.add_argument(
"--trainer-max-batch-size",
type=int,
default=256,
help="Upper bound for OnlineTrainer batch size.",
)
parser.add_argument(
"--memory-debug",
action="store_true",
help="Log RSS + tracemalloc summaries at key phases to diagnose memory spikes.",
)
return parser
def _parse_args():
parser = _build_arg_parser()
args = parser.parse_args()
args.mlflow_tags = _parse_mlflow_tags(args.mlflow_tags)
return args
args = _parse_args()
_configure_root_logger(args.log_level)
guided_population_overrides = load_guided_population_overrides(args.config_file)
with log_timing(logger, f"Loading NEAT config ({args.config_file})") as timing:
config = neat.Config(
OptimizerGenome, GuidedReproduction, neat.DefaultSpeciesSet, RelativeRankStagnation, args.config_file
)
override_keys = tuple(GUIDED_POPULATION_FIELDS.keys())
applied_override_keys: list[str] = []
for key in override_keys:
value = guided_population_overrides.get(key)
if value is None and hasattr(config, "reproduction_config"):
value = getattr(config.reproduction_config, key, None)
if value is not None:
setattr(config, key, value)
applied_override_keys.append(key)
test_mode_enabled = bool(getattr(args, "test_mode", False))
if test_mode_enabled:
setattr(config, "test_mode", True)
max_eval_epochs: int | None = None
if args.max_evaluation_steps is not None:
max_eval_epochs = max(1, int(args.max_evaluation_steps))
setattr(config, "max_evaluation_steps", max_eval_epochs)
details = [f"pop_size={config.pop_size}"]
if applied_override_keys:
details.append(f"overrides={len(applied_override_keys)}")
if test_mode_enabled:
details.append("test_mode=1")
if max_eval_epochs is not None:
details.append(f"max_eval_steps={max_eval_epochs}")
if args.trainer_max_batch_size is not None:
setattr(config, "trainer_max_batch_size", max(1, int(args.trainer_max_batch_size)))
details.append(f"trainer_batch_cap={args.trainer_max_batch_size}")
timing.set_details(", ".join(details))
memory_tracker = MemoryUsageTracker(logger, enabled=args.memory_debug)
with log_timing(logger, "Constructing GuidedPopulation") as timing:
population = GuidedPopulation(config, memory_tracker=memory_tracker)
timing.set_details(f"initial_pop={len(population.population)}")
override_initial_population(population, config)
population.add_reporter(neat.StdOutReporter(True))
stats = neat.StatisticsReporter()
population.add_reporter(stats)
run_context = (
MLflowRunManager(
tracking_uri=args.mlflow_tracking_uri,
experiment_name=args.mlflow_experiment,
run_name=args.mlflow_run_name,
tags=args.mlflow_tags,
nested=args.mlflow_nested,
)
if args.enable_mlflow
else nullcontext()
)
with run_context as mlflow_run:
try:
if mlflow_run:
mlflow_run.log_params(
{
"config_file": args.config_file,
"population_size": config.pop_size,
"num_generations": args.num_generations,
"fitness_threshold": getattr(config, "fitness_threshold", None),
}
)
kl_slice_params = {}
trainer = getattr(population, "trainer", None)
if trainer is not None:
ratio = getattr(trainer, "kl_partial_slice_ratio", None)
dims = getattr(trainer, "kl_partial_slice_dims", None)
start = getattr(trainer, "kl_partial_slice_start", None)
if ratio is not None:
kl_slice_params["kl_partial_slice_ratio"] = ratio
if dims is not None:
kl_slice_params["kl_partial_slice_dims"] = dims
if (ratio is not None or dims is not None) and start is not None:
kl_slice_params["kl_partial_slice_start"] = start
if kl_slice_params:
mlflow_run.log_params(kl_slice_params)
mlflow_run.log_artifact(args.config_file)
mlflow_reporter = MLflowLoggingReporter(mlflow_run)
population.add_reporter(mlflow_reporter)
trainer_step_counter = itertools.count(start=1)
trainer_last_step = {"value": 0}
trainer_metrics_history: list[dict[str, object]] = []
def _log_trainer_progress(**kwargs):
generation = kwargs.get("generation", 0) or 0
epoch = kwargs.get("epoch", 0) or 0
total_epochs = kwargs.get("total_epochs")
total_loss = kwargs.get("total_loss")
loss_terms = kwargs.get("loss_terms", {})