Core graph grammar
neps.search_spaces.architecture.core_graph_grammar
#
CoreGraphGrammar
#
CoreGraphGrammar(
grammars: list[Grammar] | Grammar,
terminal_to_op_names: dict,
terminal_to_graph_edges: dict = None,
edge_attr: bool = True,
edge_label: str = "op_name",
zero_op: list = None,
identity_op: list = None,
name: str = None,
scope: str = None,
return_all_subgraphs: bool = False,
return_graph_per_hierarchy: bool = False,
)
Bases: Graph
Source code in neps/search_spaces/architecture/core_graph_grammar.py
OPTIMIZER_SCOPE
class-attribute
instance-attribute
#
Whether the search space has an interface to one of the tabular benchmarks which can then be used to query architecture performances.
If this is set to true then query()
should be implemented.
__hash__
#
As it is very complicated to compare graphs (i.e. check all edge attributes, do the have shared attributes, ...) use just the name for comparison.
This is used when determining whether two instances are copies.
Source code in neps/search_spaces/architecture/graph.py
add_edges_densly
#
add_node
#
Adds a node to the graph.
Note that adding a node using an index that has been used already will override its attributes.
PARAMETER | DESCRIPTION |
---|---|
node_index |
The index for the node. Expect to be >= 1.
TYPE:
|
**attr |
The attributes which can be added in a dict like form.
DEFAULT:
|
Source code in neps/search_spaces/architecture/graph.py
assemble_trees
#
assemble_trees(
base_tree: str | DiGraph,
motif_trees: list[str] | list[DiGraph],
terminal_to_sublanguage_map: dict = None,
node_label: str = "op_name",
) -> str | DiGraph
Assembles the base parse tree with the motif parse trees
PARAMETER | DESCRIPTION |
---|---|
base_tree |
Base parse tree
TYPE:
|
motif_trees |
List of motif parse trees
TYPE:
|
node_label |
node label key. Defaults to "op_name".
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
str | DiGraph
|
nx.DiGraph: Assembled parse tree |
Source code in neps/search_spaces/architecture/core_graph_grammar.py
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 |
|
build_graph_from_tree
#
build_graph_from_tree(
tree: DiGraph,
terminal_to_torch_map: dict,
node_label: str = "op_name",
flatten_graph: bool = True,
return_cell: bool = False,
) -> None | Graph
Builds the computational graph from a parse tree.
PARAMETER | DESCRIPTION |
---|---|
tree |
parse tree.
TYPE:
|
terminal_to_torch_map |
Mapping from terminal symbols to primitives or topologies.
TYPE:
|
node_label |
Key to access terminal symbol. Defaults to "op_name".
TYPE:
|
return_cell |
Whether to return a cell. Is only needed if cell is repeated multiple times.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None | Graph
|
Tuple[Union[None, Graph]]: computational graph (self) or cell. |
Source code in neps/search_spaces/architecture/core_graph_grammar.py
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 |
|
clone
#
compile
#
Instanciates the ops at the edges using the arguments specified at the edges
Source code in neps/search_spaces/architecture/graph.py
copy
#
Copy as defined in networkx, i.e. a shallow copy.
Just handling recursively nested graphs seperately.
Source code in neps/search_spaces/architecture/graph.py
forward
#
Forward some data through the graph. This is done recursively in case there are graphs defined on nodes or as 'op' on edges.
PARAMETER | DESCRIPTION |
---|---|
x |
The input. If the graph sits on a node the input can be a dict with {source_idx: Tensor} to be routed to the defined input nodes. If the graph sits on an edge, x is the feature tensor.
TYPE:
|
args |
This is only required to handle cases where the graph sits on an edge and receives an EdgeData object which will be ignored
DEFAULT:
|
Source code in neps/search_spaces/architecture/graph.py
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 |
|
from_nxTree_to_stringTree
#
Transforms parse tree represented as NetworkX DAG to string representation.
PARAMETER | DESCRIPTION |
---|---|
nxTree |
parse tree.
TYPE:
|
node_label |
key to access operation names. Defaults to "op_name".
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
str
|
parse tree represented as string.
TYPE:
|
Source code in neps/search_spaces/architecture/core_graph_grammar.py
from_stringTree_to_graph_repr
#
from_stringTree_to_graph_repr(
string_tree: str,
grammar: Grammar,
valid_terminals: KeysView,
edge_attr: bool = True,
sym_name: str = "op_name",
prune: bool = True,
add_subtree_map: bool = False,
return_all_subgraphs: bool = None,
return_graph_per_hierarchy: bool = None,
) -> DiGraph | tuple[DiGraph, OrderedDict]
Generates graph from parse tree in string representation. Note that we ignore primitive HPs!
PARAMETER | DESCRIPTION |
---|---|
string_tree |
parse tree.
TYPE:
|
grammar |
underlying grammar.
TYPE:
|
valid_terminals |
list of keys.
TYPE:
|
edge_attr |
Shoud graph be edge attributed (True) or node attributed (False). Defaults to True.
TYPE:
|
sym_name |
Attribute name of operation. Defaults to "op_name".
TYPE:
|
prune |
Prune graph, e.g., None operations etc. Defaults to True.
TYPE:
|
add_subtree_map |
Add attribute indicating to which subtrees of the parse tree the specific part belongs to. Can only be true if you set prune=False! TODO: Check if we really need this constraint or can also allow pruning. Defaults to False.
TYPE:
|
return_all_subgraphs |
Additionally returns an hierarchical dictionary containing all subgraphs. Defaults to False. TODO: check if edge attr also works.
TYPE:
|
return_graph_per_hierarchy |
Additionally returns a graph from each each hierarchy.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
DiGraph | tuple[DiGraph, OrderedDict]
|
nx.DiGraph: [description] |
Source code in neps/search_spaces/architecture/core_graph_grammar.py
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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 |
|
from_stringTree_to_nxTree
staticmethod
#
from_stringTree_to_nxTree(
string_tree: str,
grammar: Grammar,
sym_name: str = "op_name",
) -> DiGraph
Transforms a parse tree from string representation to NetworkX representation.
PARAMETER | DESCRIPTION |
---|---|
string_tree |
parse tree.
TYPE:
|
grammar |
context-free grammar which generated the parse tree in string represenation.
TYPE:
|
sym_name |
Key to save the terminal symbols. Defaults to "op_name".
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
DiGraph
|
nx.DiGraph: parse tree as NetworkX representation. |
Source code in neps/search_spaces/architecture/core_graph_grammar.py
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 |
|
get_all_edge_data
#
Get edge attributes of this graph and all child graphs in one go.
PARAMETER | DESCRIPTION |
---|---|
key |
The key of the attribute
TYPE:
|
scope |
The scope to be applied
TYPE:
|
private_edge_data |
Whether to return data from graph copies as well.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
list
|
All data in a list.
TYPE:
|
Source code in neps/search_spaces/architecture/graph.py
get_dense_edges
#
Returns the edge indices (i, j) that would make a fully connected DAG without circles such that i < j and i != j. Assumes nodes are already created.
RETURNS | DESCRIPTION |
---|---|
list
|
list of edge indices. |
Source code in neps/search_spaces/architecture/graph.py
get_graph_representation
#
This functions takes an identifier and constructs the (multi-variate) composition of the functions it describes. Args: identifier (str): identifier grammar (Grammar): grammar flatten_graph (bool, optional): Whether to flatten the graph. Defaults to True. Returns: nx.DiGraph: (multi-variate) composition of functions
Source code in neps/search_spaces/architecture/core_graph_grammar.py
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 |
|
graph_to_self
#
graph_to_self(graph: DiGraph, clear_self: bool = True)
Copies graph to self
PARAMETER | DESCRIPTION |
---|---|
graph |
graph
TYPE:
|
Source code in neps/search_spaces/architecture/core_graph_grammar.py
modules_str
#
Once the graph has been parsed, prints the modules as they appear in pytorch.
Source code in neps/search_spaces/architecture/graph.py
num_input_nodes
#
num_input_nodes() -> int
The number of input nodes, i.e. the nodes without an incoming edge.
RETURNS | DESCRIPTION |
---|---|
int
|
Number of input nodes.
TYPE:
|
parse
#
Convert the graph into a neural network which can then be optimized by pytorch.
Source code in neps/search_spaces/architecture/graph.py
prepare_discretization
#
In some cases the search space is manipulated before the final discretization is happening, e.g. DARTS. In such chases this should be defined in the search space, so all optimizers can call it.
Source code in neps/search_spaces/architecture/graph.py
prepare_evaluation
#
In some cases the evaluation architecture does not match the searched one. An example is where the makro_model is extended to increase the parameters. This is done here.
prune_tree
#
prune_tree(
tree: DiGraph,
terminal_to_torch_map_keys: KeysView,
node_label: str = "op_name",
) -> DiGraph
Prunes unnecessary parts of parse tree, i.e., only one child
PARAMETER | DESCRIPTION |
---|---|
tree |
Parse tree
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
DiGraph
|
nx.DiGraph: Pruned parse tree |
Source code in neps/search_spaces/architecture/core_graph_grammar.py
reset_weights
#
reset_weights(inplace: bool = False)
Resets the weights for the 'op' at all edges.
PARAMETER | DESCRIPTION |
---|---|
inplace |
Do the operation in place or return a modified copy.
TYPE:
|
Returns: Graph: Returns the modified version of the graph.
Source code in neps/search_spaces/architecture/graph.py
set_at_edges
#
Sets the attribute for all edges in this and any child graph
Source code in neps/search_spaces/architecture/graph.py
set_input
#
set_input(node_idxs: list)
Route the input from specific parent edges to the input nodes of this subgraph. Inputs are assigned in lexicographical order.
Example:
- Parent node (i.e. node where self
is located on) has two
incoming edges from nodes 3 and 5.
- self
has two input nodes 1 and 2 (i.e. nodes without
an incoming edge)
- node_idxs = [5, 3]
Then input of node 5 is routed to node 1 and input of node 3
is routed to node 2.
Similarly, if node_idxs = [5, 5]
then input of node 5 is routed
to both node 1 and 2. Warning: In this case the output of another
incoming edge is ignored!
Should be used in a builder-like pattern: 'subgraph'=Graph().set_input([5, 3])
PARAMETER | DESCRIPTION |
---|---|
node_idx |
The index of the nodes where the data is coming from.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Graph
|
self with input node indices set. |
Source code in neps/search_spaces/architecture/graph.py
set_scope
#
set_scope(scope: str, recursively=True)
Sets the scope of this instance of the graph.
The function should be used in a builder-like pattern
'subgraph'=Graph().set_scope("scope")
.
PARAMETER | DESCRIPTION |
---|---|
scope |
the scope
TYPE:
|
recursively |
Also set the scope for all child graphs. default True
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Graph
|
self with the setted scope. |
Source code in neps/search_spaces/architecture/graph.py
to_graph_repr
#
Transforms NASLib-esque graph to NetworkX graph.
PARAMETER | DESCRIPTION |
---|---|
graph |
NASLib-esque graph.
TYPE:
|
edge_attr |
Transform to edge attribution or node attribution.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
DiGraph
|
nx.DiGraph: edge- or node-attributed representation of computational graph. |
Source code in neps/search_spaces/architecture/core_graph_grammar.py
unparse
#
Undo the pytorch parsing by reconstructing the graph uusing the networkx data structures.
This is done recursively also for child graphs.
RETURNS | DESCRIPTION |
---|---|
Graph
|
An unparsed shallow copy of the graph. |
Source code in neps/search_spaces/architecture/graph.py
update_edges
#
This updates the edge data of this graph and all child graphs.
This is the preferred way to manipulate the edges after the definition
of the graph, e.g. by optimizers who want to insert their own op.
update_func(current_edge_data)
. This way optimizers
can initialize and store necessary information at edges.
Note that edges marked as 'final' will not be updated here.
PARAMETER | DESCRIPTION |
---|---|
update_func |
Function which accepts one argument called
TYPE:
|
scope |
Can be "all" or list of scopes to be updated.
TYPE:
|
private_edge_data |
If set to true, this means update_func will be applied to all edges. THIS IS NOT RECOMMENDED FOR SHARED ATTRIBUTES. Shared attributes should be set only once, we take care it is syncronized across all copies of this graph. The only usecase for setting it to true is when actually changing
TYPE:
|
Source code in neps/search_spaces/architecture/graph.py
update_nodes
#
Update the nodes of the graph and its incoming and outgoing edges by iterating over the
graph and applying update_func
to each of it. This is the
preferred way to change the search space once it has been defined.
Note that edges marked as 'final' will not be updated here.
PARAMETER | DESCRIPTION |
---|---|
update_func |
Function that accepts three incoming parameters named
TYPE:
|
scope |
Can be "all" or list of scopes to be updated. Only graphs and child graphs with the specified scope are considered
TYPE:
|
single_instance |
If set to false, this means update_func will be applied to nodes of all copies of a graphs. THIS IS NOT RECOMMENDED FOR SHARED ATTRIBUTES, i.e. when manipulating the shared data of incoming or outgoing edges. Shared attributes should be set only once, we take care it is syncronized across all copies of this graph. The only usecase for setting it to true is when actually changing
TYPE:
|