computation-graph 52__tar.gz → 54__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {computation-graph-52 → computation-graph-54}/PKG-INFO +1 -1
- {computation-graph-52 → computation-graph-54}/computation_graph/base_types.py +0 -2
- {computation-graph-52 → computation-graph-54}/computation_graph/composers/__init__.py +31 -33
- {computation-graph-52 → computation-graph-54}/computation_graph/composers/duplication.py +39 -2
- {computation-graph-52 → computation-graph-54}/computation_graph/graph.py +41 -19
- {computation-graph-52 → computation-graph-54}/computation_graph/graph_test.py +103 -5
- {computation-graph-52 → computation-graph-54}/computation_graph/legacy.py +3 -0
- {computation-graph-52 → computation-graph-54}/computation_graph/run.py +35 -6
- {computation-graph-52 → computation-graph-54}/computation_graph/signature.py +7 -10
- {computation-graph-52 → computation-graph-54}/computation_graph.egg-info/PKG-INFO +1 -1
- {computation-graph-52 → computation-graph-54}/setup.py +1 -1
- {computation-graph-52 → computation-graph-54}/LICENSE.md +0 -0
- {computation-graph-52 → computation-graph-54}/README.md +0 -0
- {computation-graph-52 → computation-graph-54}/computation_graph/__init__.py +0 -0
- {computation-graph-52 → computation-graph-54}/computation_graph/composers/composers_test.py +0 -0
- {computation-graph-52 → computation-graph-54}/computation_graph/composers/condition.py +0 -0
- {computation-graph-52 → computation-graph-54}/computation_graph/composers/condition_test.py +0 -0
- {computation-graph-52 → computation-graph-54}/computation_graph/composers/debug.py +0 -0
- {computation-graph-52 → computation-graph-54}/computation_graph/composers/lift.py +0 -0
- {computation-graph-52 → computation-graph-54}/computation_graph/composers/logic.py +0 -0
- {computation-graph-52 → computation-graph-54}/computation_graph/composers/memory.py +0 -0
- {computation-graph-52 → computation-graph-54}/computation_graph/composers/memory_test.py +0 -0
- {computation-graph-52 → computation-graph-54}/computation_graph/graph_runners.py +0 -0
- {computation-graph-52 → computation-graph-54}/computation_graph/trace/__init__.py +0 -0
- {computation-graph-52 → computation-graph-54}/computation_graph/trace/ascii.py +0 -0
- {computation-graph-52 → computation-graph-54}/computation_graph/trace/graphviz.py +0 -0
- {computation-graph-52 → computation-graph-54}/computation_graph/trace/graphviz_test.py +0 -0
- {computation-graph-52 → computation-graph-54}/computation_graph/trace/mermaid.py +0 -0
- {computation-graph-52 → computation-graph-54}/computation_graph/trace/trace_utils.py +0 -0
- {computation-graph-52 → computation-graph-54}/computation_graph.egg-info/SOURCES.txt +0 -0
- {computation-graph-52 → computation-graph-54}/computation_graph.egg-info/dependency_links.txt +0 -0
- {computation-graph-52 → computation-graph-54}/computation_graph.egg-info/requires.txt +0 -0
- {computation-graph-52 → computation-graph-54}/computation_graph.egg-info/top_level.txt +0 -0
- {computation-graph-52 → computation-graph-54}/pyproject.toml +0 -0
- {computation-graph-52 → computation-graph-54}/setup.cfg +0 -0
|
@@ -48,14 +48,14 @@ def make_and(
|
|
|
48
48
|
|
|
49
49
|
merge_node = graph.make_computation_node(args_to_tuple)
|
|
50
50
|
|
|
51
|
-
return gamla.pipe(
|
|
51
|
+
return gamla.sync.pipe(
|
|
52
52
|
funcs,
|
|
53
|
-
gamla.map(_callable_or_graph_type_to_node_or_graph_type),
|
|
53
|
+
gamla.sync.map(_callable_or_graph_type_to_node_or_graph_type),
|
|
54
54
|
tuple,
|
|
55
|
-
gamla.juxtcat(
|
|
56
|
-
gamla.map(_get_edges_from_node_or_graph),
|
|
57
|
-
gamla.compose_left(
|
|
58
|
-
gamla.map(_infer_sink),
|
|
55
|
+
gamla.sync.juxtcat(
|
|
56
|
+
gamla.sync.map(_get_edges_from_node_or_graph),
|
|
57
|
+
gamla.sync.compose_left(
|
|
58
|
+
gamla.sync.map(_infer_sink),
|
|
59
59
|
tuple,
|
|
60
60
|
lambda nodes: (
|
|
61
61
|
(
|
|
@@ -72,7 +72,7 @@ def make_and(
|
|
|
72
72
|
),
|
|
73
73
|
),
|
|
74
74
|
),
|
|
75
|
-
gamla.star(base_types.merge_graphs),
|
|
75
|
+
gamla.sync.star(base_types.merge_graphs),
|
|
76
76
|
)
|
|
77
77
|
|
|
78
78
|
|
|
@@ -92,13 +92,13 @@ def make_or(
|
|
|
92
92
|
|
|
93
93
|
filter_node = graph.make_computation_node(filter_computation_errors)
|
|
94
94
|
|
|
95
|
-
return gamla.pipe(
|
|
95
|
+
return gamla.sync.pipe(
|
|
96
96
|
funcs,
|
|
97
|
-
gamla.map(make_optional(default_value=_ComputationError())),
|
|
97
|
+
gamla.sync.map(make_optional(default_value=_ComputationError())),
|
|
98
98
|
tuple,
|
|
99
|
-
gamla.
|
|
100
|
-
gamla.compose_left(
|
|
101
|
-
gamla.map(_infer_sink),
|
|
99
|
+
gamla.sync.pair_right(
|
|
100
|
+
gamla.sync.compose_left(
|
|
101
|
+
gamla.sync.map(_infer_sink),
|
|
102
102
|
tuple,
|
|
103
103
|
lambda sinks: (
|
|
104
104
|
(
|
|
@@ -116,7 +116,7 @@ def make_or(
|
|
|
116
116
|
)
|
|
117
117
|
),
|
|
118
118
|
gamla.concat,
|
|
119
|
-
gamla.star(base_types.merge_graphs),
|
|
119
|
+
gamla.sync.star(base_types.merge_graphs),
|
|
120
120
|
)
|
|
121
121
|
|
|
122
122
|
|
|
@@ -126,18 +126,16 @@ _destinations = gamla.compose(set, gamla.map(base_types.edge_destination))
|
|
|
126
126
|
def _infer_sink(graph_or_node: base_types.NodeOrGraph) -> base_types.ComputationNode:
|
|
127
127
|
if isinstance(graph_or_node, base_types.ComputationNode):
|
|
128
128
|
return graph_or_node
|
|
129
|
-
graph_without_future_edges =
|
|
129
|
+
graph_without_future_edges = graph.remove_future_edges(graph_or_node)
|
|
130
130
|
if graph_without_future_edges:
|
|
131
131
|
try:
|
|
132
|
-
return
|
|
133
|
-
graph_without_future_edges, graph.sink_excluding_terminals
|
|
134
|
-
)
|
|
132
|
+
return graph.sink_excluding_terminals(graph_without_future_edges)
|
|
135
133
|
except AssertionError:
|
|
136
134
|
# If we reached here we can try again without sources of future edges.
|
|
137
|
-
sources_of_future_edges = gamla.pipe(
|
|
135
|
+
sources_of_future_edges = gamla.sync.pipe(
|
|
138
136
|
graph_or_node,
|
|
139
|
-
gamla.filter(base_types.edge_is_future),
|
|
140
|
-
gamla.map(base_types.edge_source),
|
|
137
|
+
gamla.sync.filter(base_types.edge_is_future),
|
|
138
|
+
gamla.sync.map(base_types.edge_source),
|
|
141
139
|
frozenset,
|
|
142
140
|
)
|
|
143
141
|
result = (
|
|
@@ -234,30 +232,30 @@ def _infer_composition_edges(
|
|
|
234
232
|
graph.get_incoming_edges_for_node(destination)
|
|
235
233
|
)
|
|
236
234
|
return base_types.merge_graphs(
|
|
237
|
-
gamla.pipe(
|
|
235
|
+
gamla.sync.pipe(
|
|
238
236
|
destination,
|
|
239
|
-
gamla.mapcat(graph.get_edge_nodes),
|
|
237
|
+
gamla.sync.mapcat(graph.get_edge_nodes),
|
|
240
238
|
gamla.unique,
|
|
241
|
-
gamla.filter(
|
|
242
|
-
gamla.compose_left(
|
|
239
|
+
gamla.sync.filter(
|
|
240
|
+
gamla.sync.compose_left(
|
|
243
241
|
unbound_signature,
|
|
244
242
|
lambda sig: key in sig.kwargs
|
|
245
243
|
or (key is None and signature.is_unary(sig)),
|
|
246
244
|
)
|
|
247
245
|
),
|
|
248
246
|
# Do not add edges to nodes from source that are already present in destination (cycle).
|
|
249
|
-
gamla.filter(
|
|
247
|
+
gamla.sync.filter(
|
|
250
248
|
lambda node: isinstance(source, base_types.ComputationNode)
|
|
251
249
|
or node not in graph.get_all_nodes(source)
|
|
252
250
|
),
|
|
253
|
-
gamla.map(
|
|
251
|
+
gamla.sync.map(
|
|
254
252
|
lambda destination: try_connect(
|
|
255
253
|
destination=destination,
|
|
256
254
|
unbound_destination_signature=unbound_signature(destination),
|
|
257
255
|
)
|
|
258
256
|
),
|
|
259
257
|
tuple,
|
|
260
|
-
gamla.check(
|
|
258
|
+
gamla.sync.check(
|
|
261
259
|
gamla.identity,
|
|
262
260
|
AssertionError(
|
|
263
261
|
f"Cannot compose, destination signature does not contain key '{key}'"
|
|
@@ -278,13 +276,13 @@ def _make_compose_inner(
|
|
|
278
276
|
assert (
|
|
279
277
|
len(funcs) > 1
|
|
280
278
|
), f"Only {len(funcs)} function passed to compose, need at least 2, funcs={funcs}"
|
|
281
|
-
return gamla.pipe(
|
|
279
|
+
return gamla.sync.pipe(
|
|
282
280
|
funcs,
|
|
283
281
|
reversed,
|
|
284
|
-
gamla.map(_callable_or_graph_type_to_node_or_graph_type),
|
|
282
|
+
gamla.sync.map(_callable_or_graph_type_to_node_or_graph_type),
|
|
285
283
|
gamla.sliding_window(2),
|
|
286
|
-
gamla.map(gamla.star(_infer_composition_edges(priority, key, is_future))),
|
|
287
|
-
gamla.star(base_types.merge_graphs),
|
|
284
|
+
gamla.sync.map(gamla.star(_infer_composition_edges(priority, key, is_future))),
|
|
285
|
+
gamla.sync.star(base_types.merge_graphs),
|
|
288
286
|
)
|
|
289
287
|
|
|
290
288
|
|
|
@@ -389,8 +387,8 @@ def compose_dict(
|
|
|
389
387
|
return gamla.pipe(
|
|
390
388
|
d,
|
|
391
389
|
dict.items,
|
|
392
|
-
gamla.map(gamla.star(lambda key, fn: make_compose(f, fn, key=key))),
|
|
393
|
-
gamla.star(base_types.merge_graphs),
|
|
390
|
+
gamla.sync.map(gamla.star(lambda key, fn: make_compose(f, fn, key=key))),
|
|
391
|
+
gamla.sync.star(base_types.merge_graphs),
|
|
394
392
|
) or compose_left_unary(f, lambda x: x)
|
|
395
393
|
|
|
396
394
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import asyncio
|
|
2
2
|
import functools
|
|
3
3
|
import inspect
|
|
4
|
+
from typing import Dict
|
|
4
5
|
|
|
5
6
|
import gamla
|
|
6
7
|
|
|
@@ -20,6 +21,7 @@ def duplicate_function(func):
|
|
|
20
21
|
def inner(*args, **kwargs):
|
|
21
22
|
return func(*args, **kwargs)
|
|
22
23
|
|
|
24
|
+
inner.__name__ = f"duplicate of {inner.__name__}"
|
|
23
25
|
return inner
|
|
24
26
|
|
|
25
27
|
|
|
@@ -42,8 +44,8 @@ _duplicate_node = gamla.compose_left(
|
|
|
42
44
|
graph.make_computation_node,
|
|
43
45
|
)
|
|
44
46
|
|
|
47
|
+
|
|
45
48
|
_node_to_duplicated_node = gamla.compose_left(
|
|
46
|
-
graph.get_all_nodes,
|
|
47
49
|
gamla.remove(base_types.node_is_terminal),
|
|
48
50
|
gamla.map(gamla.pair_right(_duplicate_node)),
|
|
49
51
|
dict,
|
|
@@ -51,7 +53,7 @@ _node_to_duplicated_node = gamla.compose_left(
|
|
|
51
53
|
)
|
|
52
54
|
|
|
53
55
|
duplicate_graph = gamla.compose_left(
|
|
54
|
-
gamla.pair_with(_node_to_duplicated_node),
|
|
56
|
+
gamla.pair_with(gamla.compose_left(graph.get_all_nodes, _node_to_duplicated_node)),
|
|
55
57
|
gamla.star(
|
|
56
58
|
lambda get_duplicated_node, graph: gamla.pipe(
|
|
57
59
|
graph,
|
|
@@ -68,3 +70,38 @@ duplicate_graph = gamla.compose_left(
|
|
|
68
70
|
duplicate_function_or_graph = gamla.ternary(
|
|
69
71
|
gamla.is_instance(tuple), duplicate_graph, duplicate_function
|
|
70
72
|
)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def safe_replace_sources(
|
|
76
|
+
source_to_replacement_dict: Dict[
|
|
77
|
+
base_types.CallableOrNode, base_types.CallableOrNodeOrGraph
|
|
78
|
+
],
|
|
79
|
+
cg: base_types.GraphType,
|
|
80
|
+
) -> base_types.GraphType:
|
|
81
|
+
source_to_replacement_dict = gamla.keymap(graph.make_computation_node)(
|
|
82
|
+
source_to_replacement_dict
|
|
83
|
+
)
|
|
84
|
+
reachable_to_duplicate_map = gamla.pipe(
|
|
85
|
+
gamla.graph_traverse_many(
|
|
86
|
+
source_to_replacement_dict.keys(), graph.traverse_forward(cg)
|
|
87
|
+
),
|
|
88
|
+
gamla.remove(gamla.contains(source_to_replacement_dict.keys())),
|
|
89
|
+
_node_to_duplicated_node,
|
|
90
|
+
)
|
|
91
|
+
get_node_replacement = gamla.compose_left(
|
|
92
|
+
gamla.lazyjuxt(
|
|
93
|
+
gamla.dict_to_getter_with_default(None)(source_to_replacement_dict),
|
|
94
|
+
reachable_to_duplicate_map,
|
|
95
|
+
gamla.identity,
|
|
96
|
+
),
|
|
97
|
+
gamla.find(gamla.identity),
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
def update_edge(e: base_types.ComputationEdge) -> base_types.GraphType:
|
|
101
|
+
dest = base_types.edge_destination(e)
|
|
102
|
+
g = graph.replace_node(dest, get_node_replacement(dest))((e,))
|
|
103
|
+
for source in base_types.edge_sources(e):
|
|
104
|
+
g = graph.replace_source(source, get_node_replacement(source), g)
|
|
105
|
+
return g
|
|
106
|
+
|
|
107
|
+
return gamla.reduce(lambda updated_cg, e: updated_cg + update_edge(e), (), cg)
|
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
import dataclasses
|
|
2
|
-
from typing import Callable, FrozenSet
|
|
2
|
+
from typing import Callable, FrozenSet, Tuple
|
|
3
3
|
|
|
4
4
|
import gamla
|
|
5
5
|
from gamla.optimized import sync as opt_gamla
|
|
6
6
|
|
|
7
7
|
from computation_graph import base_types, signature
|
|
8
8
|
|
|
9
|
-
get_edge_nodes =
|
|
9
|
+
get_edge_nodes = opt_gamla.ternary(
|
|
10
10
|
base_types.edge_args,
|
|
11
11
|
lambda edge: edge.args + (edge.destination,),
|
|
12
12
|
lambda edge: (edge.source, edge.destination),
|
|
13
13
|
)
|
|
14
14
|
|
|
15
|
-
get_all_nodes =
|
|
15
|
+
get_all_nodes = opt_gamla.compose_left(opt_gamla.mapcat(get_edge_nodes), frozenset)
|
|
16
16
|
|
|
17
17
|
|
|
18
|
-
edges_to_node_id_map =
|
|
18
|
+
edges_to_node_id_map = opt_gamla.compose_left(
|
|
19
19
|
gamla.mapcat(get_edge_nodes), gamla.unique, enumerate, gamla.map(reversed), dict
|
|
20
20
|
)
|
|
21
21
|
|
|
@@ -41,13 +41,13 @@ def make_computation_node(
|
|
|
41
41
|
|
|
42
42
|
|
|
43
43
|
def get_leaves(edges: base_types.GraphType) -> FrozenSet[base_types.ComputationNode]:
|
|
44
|
-
return
|
|
44
|
+
return opt_gamla.pipe(
|
|
45
45
|
edges,
|
|
46
46
|
get_all_nodes,
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
opt_gamla.remove(
|
|
48
|
+
opt_gamla.pipe(
|
|
49
49
|
edges,
|
|
50
|
-
|
|
50
|
+
opt_gamla.mapcat(lambda edge: (edge.source, *edge.args)),
|
|
51
51
|
frozenset,
|
|
52
52
|
gamla.contains,
|
|
53
53
|
)
|
|
@@ -56,27 +56,29 @@ def get_leaves(edges: base_types.GraphType) -> FrozenSet[base_types.ComputationN
|
|
|
56
56
|
)
|
|
57
57
|
|
|
58
58
|
|
|
59
|
-
sink_excluding_terminals =
|
|
59
|
+
sink_excluding_terminals = opt_gamla.compose_left(
|
|
60
60
|
get_leaves,
|
|
61
|
-
|
|
61
|
+
opt_gamla.remove(base_types.node_is_terminal),
|
|
62
62
|
tuple,
|
|
63
63
|
gamla.assert_that(gamla.len_equals(1)),
|
|
64
64
|
gamla.head,
|
|
65
65
|
)
|
|
66
66
|
|
|
67
|
-
get_incoming_edges_for_node =
|
|
68
|
-
|
|
69
|
-
|
|
67
|
+
get_incoming_edges_for_node = opt_gamla.compose_left(
|
|
68
|
+
opt_gamla.groupby(base_types.edge_destination),
|
|
69
|
+
opt_gamla.valmap(frozenset),
|
|
70
70
|
gamla.dict_to_getter_with_default(frozenset()),
|
|
71
71
|
)
|
|
72
72
|
|
|
73
73
|
|
|
74
|
-
get_terminals =
|
|
75
|
-
get_all_nodes,
|
|
74
|
+
get_terminals = opt_gamla.compose_left(
|
|
75
|
+
get_all_nodes, opt_gamla.filter(base_types.node_is_terminal), tuple
|
|
76
76
|
)
|
|
77
77
|
|
|
78
78
|
|
|
79
|
-
remove_future_edges =
|
|
79
|
+
remove_future_edges = opt_gamla.compose(
|
|
80
|
+
tuple, opt_gamla.remove(base_types.edge_is_future)
|
|
81
|
+
)
|
|
80
82
|
|
|
81
83
|
|
|
82
84
|
def make_source():
|
|
@@ -98,7 +100,7 @@ def make_terminal(name: str, func: Callable) -> base_types.ComputationNode:
|
|
|
98
100
|
)
|
|
99
101
|
|
|
100
102
|
|
|
101
|
-
_keep_not_in_bound_kwargs =
|
|
103
|
+
_keep_not_in_bound_kwargs = opt_gamla.compose_left(
|
|
102
104
|
opt_gamla.map(base_types.edge_key),
|
|
103
105
|
gamla.filter(gamla.identity),
|
|
104
106
|
frozenset,
|
|
@@ -148,6 +150,25 @@ def _replace_source_in_edges(
|
|
|
148
150
|
)
|
|
149
151
|
|
|
150
152
|
|
|
153
|
+
traverse_forward: Callable[
|
|
154
|
+
[base_types.GraphType],
|
|
155
|
+
Callable[[base_types.CallableOrNode], Tuple[base_types.ComputationNode, ...]],
|
|
156
|
+
] = opt_gamla.compose_left(
|
|
157
|
+
gamla.mapcat(
|
|
158
|
+
opt_gamla.compose_left(
|
|
159
|
+
gamla.juxt(base_types.edge_sources, base_types.edge_destination),
|
|
160
|
+
gamla.explode(0),
|
|
161
|
+
)
|
|
162
|
+
),
|
|
163
|
+
gamla.groupby_many_reduce(
|
|
164
|
+
opt_gamla.compose_left(gamla.head, gamla.wrap_tuple),
|
|
165
|
+
lambda destinations, e: (*(destinations if destinations else ()), e[1]),
|
|
166
|
+
),
|
|
167
|
+
gamla.dict_to_getter_with_default(()),
|
|
168
|
+
gamla.before(make_computation_node),
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
|
|
151
172
|
@gamla.curry
|
|
152
173
|
def replace_source(
|
|
153
174
|
original: base_types.CallableOrNode,
|
|
@@ -193,7 +214,8 @@ def transform_edges(
|
|
|
193
214
|
edge_mapper: Callable[[base_types.ComputationEdge], base_types.ComputationEdge],
|
|
194
215
|
):
|
|
195
216
|
return _operate_on_subgraph(
|
|
196
|
-
_split_by_condition(query),
|
|
217
|
+
_split_by_condition(query),
|
|
218
|
+
opt_gamla.compose_left(gamla.map(edge_mapper), tuple),
|
|
197
219
|
)
|
|
198
220
|
|
|
199
221
|
|
|
@@ -264,7 +286,7 @@ def _operate_on_subgraph(selector, transformation):
|
|
|
264
286
|
|
|
265
287
|
|
|
266
288
|
def _split_by_condition(condition):
|
|
267
|
-
return
|
|
289
|
+
return opt_gamla.compose_left(
|
|
268
290
|
gamla.bifurcate(gamla.filter(condition), gamla.remove(condition)),
|
|
269
291
|
gamla.map(tuple),
|
|
270
292
|
tuple,
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import asyncio
|
|
2
|
+
from typing import Dict
|
|
2
3
|
|
|
3
4
|
import gamla
|
|
4
5
|
import pytest
|
|
@@ -375,7 +376,7 @@ def test_or_with_sink_that_raises():
|
|
|
375
376
|
|
|
376
377
|
|
|
377
378
|
def test_unambiguous_composition_using_terminal():
|
|
378
|
-
terminal = graph.make_terminal("1", lambda x: x)
|
|
379
|
+
terminal = graph.make_terminal("1", lambda x: x[0])
|
|
379
380
|
|
|
380
381
|
def source():
|
|
381
382
|
return 1
|
|
@@ -402,8 +403,8 @@ def test_unambiguous_composition_using_terminal():
|
|
|
402
403
|
|
|
403
404
|
|
|
404
405
|
def test_two_terminals():
|
|
405
|
-
terminal1 = graph.make_terminal("1",
|
|
406
|
-
terminal2 = graph.make_terminal("2",
|
|
406
|
+
terminal1 = graph.make_terminal("1", lambda x: x)
|
|
407
|
+
terminal2 = graph.make_terminal("2", lambda x: x)
|
|
407
408
|
result = graph_runners.unary_bare(
|
|
408
409
|
base_types.merge_graphs(
|
|
409
410
|
composers.compose_unary(terminal1, composers.make_compose(_node2, _node1)),
|
|
@@ -417,8 +418,8 @@ def test_two_terminals():
|
|
|
417
418
|
|
|
418
419
|
def test_two_paths_succeed():
|
|
419
420
|
source = graph.make_source()
|
|
420
|
-
terminal1 = graph.make_terminal("1",
|
|
421
|
-
terminal2 = graph.make_terminal("2",
|
|
421
|
+
terminal1 = graph.make_terminal("1", lambda x: x)
|
|
422
|
+
terminal2 = graph.make_terminal("2", lambda x: x)
|
|
422
423
|
result = graph_runners.variadic_bare(
|
|
423
424
|
base_types.merge_graphs(
|
|
424
425
|
composers.make_first(
|
|
@@ -802,3 +803,100 @@ def test_ambig_edges_assertion_in_merge_graphs_active_only_when_env_var_is_activ
|
|
|
802
803
|
composers.compose_left_unary(lambda: 1, a),
|
|
803
804
|
composers.compose_left_unary(lambda: 1, a),
|
|
804
805
|
)
|
|
806
|
+
|
|
807
|
+
|
|
808
|
+
def a():
|
|
809
|
+
pass
|
|
810
|
+
|
|
811
|
+
|
|
812
|
+
def b(x):
|
|
813
|
+
pass
|
|
814
|
+
|
|
815
|
+
|
|
816
|
+
def c(x):
|
|
817
|
+
pass
|
|
818
|
+
|
|
819
|
+
|
|
820
|
+
def d(x, y):
|
|
821
|
+
pass
|
|
822
|
+
|
|
823
|
+
|
|
824
|
+
def kuky():
|
|
825
|
+
pass
|
|
826
|
+
|
|
827
|
+
|
|
828
|
+
def kuku():
|
|
829
|
+
pass
|
|
830
|
+
|
|
831
|
+
|
|
832
|
+
t = graph.make_terminal("t", lambda x: x)
|
|
833
|
+
|
|
834
|
+
g = (
|
|
835
|
+
composers.compose_left(a, c)
|
|
836
|
+
+ composers.compose_left(c, d, key="x")
|
|
837
|
+
+ composers.compose_left(b, d, key="y")
|
|
838
|
+
+ composers.compose_left_future(d, b, "x", "bla")
|
|
839
|
+
+ composers.compose_left(a, t)
|
|
840
|
+
)
|
|
841
|
+
|
|
842
|
+
|
|
843
|
+
@pytest.mark.parametrize(
|
|
844
|
+
"to_replace,expected_edges_strs",
|
|
845
|
+
[
|
|
846
|
+
pytest.param(
|
|
847
|
+
{a: kuky},
|
|
848
|
+
{
|
|
849
|
+
"kuky----x---->duplicate of c",
|
|
850
|
+
"kuky----x---->t",
|
|
851
|
+
"duplicate of c----x---->duplicate of d",
|
|
852
|
+
"when_memory_unavailable----x---->duplicate of b",
|
|
853
|
+
"duplicate of d....x....>duplicate of b",
|
|
854
|
+
"duplicate of b----y---->duplicate of d",
|
|
855
|
+
},
|
|
856
|
+
id="replace source node",
|
|
857
|
+
),
|
|
858
|
+
pytest.param(
|
|
859
|
+
{c: kuky},
|
|
860
|
+
{
|
|
861
|
+
"a----x---->kuky",
|
|
862
|
+
"a----x---->t",
|
|
863
|
+
"kuky----x---->duplicate of d",
|
|
864
|
+
"when_memory_unavailable----x---->duplicate of b",
|
|
865
|
+
"duplicate of d....x....>duplicate of b",
|
|
866
|
+
"duplicate of b----y---->duplicate of d",
|
|
867
|
+
},
|
|
868
|
+
id="replace node not in cycle",
|
|
869
|
+
),
|
|
870
|
+
pytest.param(
|
|
871
|
+
{b: kuky},
|
|
872
|
+
{
|
|
873
|
+
"a----x---->c",
|
|
874
|
+
"a----x---->t",
|
|
875
|
+
"c----x---->duplicate of d",
|
|
876
|
+
"when_memory_unavailable----x---->kuky",
|
|
877
|
+
"duplicate of d....x....>kuky",
|
|
878
|
+
"kuky----y---->duplicate of d",
|
|
879
|
+
},
|
|
880
|
+
id="replace node in cycle",
|
|
881
|
+
),
|
|
882
|
+
pytest.param(
|
|
883
|
+
{a: kuku, b: kuky},
|
|
884
|
+
{
|
|
885
|
+
"duplicate of c----x---->duplicate of d",
|
|
886
|
+
"duplicate of d....x....>kuky",
|
|
887
|
+
"kuku----x---->duplicate of c",
|
|
888
|
+
"kuku----x---->t",
|
|
889
|
+
"kuky----y---->duplicate of d",
|
|
890
|
+
"when_memory_unavailable----x---->kuky",
|
|
891
|
+
},
|
|
892
|
+
id="replace multiple nodes - duplicate reachables once",
|
|
893
|
+
),
|
|
894
|
+
],
|
|
895
|
+
)
|
|
896
|
+
def test_safe_replace_node(
|
|
897
|
+
to_replace: Dict[base_types.CallableOrNode, base_types.CallableOrNodeOrGraph],
|
|
898
|
+
expected_edges_strs: str,
|
|
899
|
+
):
|
|
900
|
+
assert expected_edges_strs == {
|
|
901
|
+
str(e) for e in duplication.safe_replace_sources(to_replace, g)
|
|
902
|
+
}
|
|
@@ -16,7 +16,7 @@ import typeguard
|
|
|
16
16
|
from gamla.optimized import async_functions as opt_async_gamla
|
|
17
17
|
from gamla.optimized import sync as opt_gamla
|
|
18
18
|
|
|
19
|
-
from computation_graph import base_types, graph, signature
|
|
19
|
+
from computation_graph import base_types, composers, graph, signature
|
|
20
20
|
from computation_graph.composers import debug
|
|
21
21
|
|
|
22
22
|
|
|
@@ -28,7 +28,7 @@ _NodeToResults = Dict[base_types.ComputationNode, base_types.Result]
|
|
|
28
28
|
_ComputationInput = Tuple[Tuple[base_types.Result, ...], Dict[str, base_types.Result]]
|
|
29
29
|
_SingleNodeSideEffect = Callable[[base_types.ComputationNode, Any], None]
|
|
30
30
|
_NodeToComputationInput = Callable[[base_types.ComputationNode], _ComputationInput]
|
|
31
|
-
|
|
31
|
+
_ComputationInputSpec = Tuple[
|
|
32
32
|
Tuple[base_types.ComputationNode, ...], Dict[str, base_types.ComputationNode]
|
|
33
33
|
]
|
|
34
34
|
|
|
@@ -143,12 +143,41 @@ _graph_to_future_sources = opt_gamla.compose_left(
|
|
|
143
143
|
)
|
|
144
144
|
|
|
145
145
|
|
|
146
|
+
"""Replace multiple edges pointing to a terminal with one edge that has multiple args"""
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def _merge_edges_pointing_to_terminals(g: base_types.GraphType) -> base_types.GraphType:
|
|
150
|
+
return gamla.compose_left(
|
|
151
|
+
gamla.groupby(gamla.attrgetter("destination")),
|
|
152
|
+
gamla.itemmap(
|
|
153
|
+
gamla.star(
|
|
154
|
+
lambda dest, edges_for_dest: (
|
|
155
|
+
dest,
|
|
156
|
+
base_types.merge_graphs(
|
|
157
|
+
composers.make_or(
|
|
158
|
+
opt_gamla.maptuple(base_types.edge_source)(edges_for_dest),
|
|
159
|
+
merge_fn=(aggregate := lambda args: args),
|
|
160
|
+
),
|
|
161
|
+
composers.compose_left_unary(aggregate, dest),
|
|
162
|
+
)
|
|
163
|
+
if dest.is_terminal
|
|
164
|
+
else edges_for_dest,
|
|
165
|
+
)
|
|
166
|
+
)
|
|
167
|
+
),
|
|
168
|
+
dict.values,
|
|
169
|
+
gamla.concat,
|
|
170
|
+
tuple,
|
|
171
|
+
)(g)
|
|
172
|
+
|
|
173
|
+
|
|
146
174
|
def _to_callable_with_side_effect_for_single_and_multiple(
|
|
147
175
|
single_node_side_effect: _SingleNodeSideEffect,
|
|
148
176
|
all_nodes_side_effect: Callable,
|
|
149
177
|
edges: base_types.GraphType,
|
|
150
178
|
handled_exceptions: Tuple[Type[Exception], ...],
|
|
151
179
|
) -> Callable[[_NodeToResults, _NodeToResults], _NodeToResults]:
|
|
180
|
+
edges = _merge_edges_pointing_to_terminals(edges)
|
|
152
181
|
single_node_side_effect = (
|
|
153
182
|
(lambda node, result: result)
|
|
154
183
|
if os.getenv(base_types.COMPUTATION_GRAPH_DEBUG_ENV_KEY) is None
|
|
@@ -238,7 +267,7 @@ _get_kwargs_nodes = opt_gamla.compose_left(
|
|
|
238
267
|
|
|
239
268
|
def _node_incoming_edges_to_input_spec(
|
|
240
269
|
node_incoming_edges: Tuple[base_types.ComputationEdge],
|
|
241
|
-
) ->
|
|
270
|
+
) -> _ComputationInputSpec:
|
|
242
271
|
if not len(node_incoming_edges):
|
|
243
272
|
return (), {}
|
|
244
273
|
first_incoming_edge = gamla.head(node_incoming_edges)
|
|
@@ -256,7 +285,7 @@ def _edges_to_accumulated_results_to_node_to_first_possible_input(
|
|
|
256
285
|
) -> Callable[[_NodeToResults], _NodeToComputationInput]:
|
|
257
286
|
node_to_incoming_edges = graph.get_incoming_edges_for_node(edges)
|
|
258
287
|
node_to_computation_input_spec_options: Callable[
|
|
259
|
-
[base_types.ComputationNode], Tuple[
|
|
288
|
+
[base_types.ComputationNode], Tuple[_ComputationInputSpec]
|
|
260
289
|
] = functools.cache(
|
|
261
290
|
gamla.compose_left(
|
|
262
291
|
node_to_incoming_edges,
|
|
@@ -304,7 +333,7 @@ def _make_reduce_layers(
|
|
|
304
333
|
get_node_input_and_apply = _make_get_node_input_and_apply(
|
|
305
334
|
is_async, single_node_side_effect
|
|
306
335
|
)
|
|
307
|
-
|
|
336
|
+
accumulated_results_to_node_to_input = (
|
|
308
337
|
_edges_to_accumulated_results_to_node_to_first_possible_input(edges)
|
|
309
338
|
)
|
|
310
339
|
single_layer_reducer = debug.name_callable(
|
|
@@ -314,7 +343,7 @@ def _make_reduce_layers(
|
|
|
314
343
|
gamla.head,
|
|
315
344
|
gamla.compose_left(
|
|
316
345
|
opt_gamla.packstack(
|
|
317
|
-
|
|
346
|
+
accumulated_results_to_node_to_input, gamla.identity
|
|
318
347
|
),
|
|
319
348
|
gamla.explode(1),
|
|
320
349
|
gamla.map_filter_empty(
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import functools
|
|
2
2
|
import inspect
|
|
3
3
|
from types import MappingProxyType
|
|
4
|
-
from typing import Callable, FrozenSet
|
|
4
|
+
from typing import Callable, FrozenSet
|
|
5
5
|
|
|
6
6
|
import gamla
|
|
7
7
|
|
|
@@ -34,16 +34,13 @@ def _is_default(parameter):
|
|
|
34
34
|
|
|
35
35
|
_parameter_name = gamla.attrgetter("name")
|
|
36
36
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
gamla.compose_left(
|
|
40
|
-
inspect.signature,
|
|
41
|
-
gamla.attrgetter("parameters"),
|
|
42
|
-
MappingProxyType.values,
|
|
43
|
-
tuple,
|
|
44
|
-
)
|
|
37
|
+
_func_parameters = gamla.compose_left(
|
|
38
|
+
inspect.signature, gamla.attrgetter("parameters"), MappingProxyType.values, tuple
|
|
45
39
|
)
|
|
46
|
-
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def from_callable(func: Callable) -> base_types.NodeSignature:
|
|
43
|
+
function_parameters = _func_parameters(func)
|
|
47
44
|
return base_types.NodeSignature(
|
|
48
45
|
is_args=gamla.anymap(parameter_is_star)(function_parameters),
|
|
49
46
|
is_kwargs=gamla.anymap(parameter_is_double_star)(function_parameters),
|
|
@@ -7,7 +7,7 @@ with open("README.md", "r") as fh:
|
|
|
7
7
|
setuptools.setup(
|
|
8
8
|
name="computation-graph",
|
|
9
9
|
python_requires=">=3",
|
|
10
|
-
version="
|
|
10
|
+
version="54",
|
|
11
11
|
long_description=_LONG_DESCRIPTION,
|
|
12
12
|
long_description_content_type="text/markdown",
|
|
13
13
|
packages=setuptools.find_namespace_packages(),
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{computation-graph-52 → computation-graph-54}/computation_graph.egg-info/dependency_links.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|