def test_graph_properties(): @clone @parse def test(x): def f(y): def g(z): def h(): return y + z return h return g(x) return f(x + 1) with pytest.raises(Exception): test.manager mng = GraphManager(test) for g in mng.graphs: assert g.manager is mng assert g.nodes is mng.nodes[g] assert g.constants is mng.constants[g] assert g.free_variables_direct is mng.free_variables_direct[g] assert g.free_variables_total is mng.free_variables_total[g] assert g.graphs_used is mng.graphs_used[g] assert g.graph_users is mng.graph_users[g] assert g.graphs_reachable is mng.graphs_reachable[g] assert g.graph_dependencies_direct is mng.graph_dependencies_direct[g] assert g.graph_dependencies_total is mng.graph_dependencies_total[g] assert g.parent is mng.parents[g] assert g.children is mng.children[g] assert g.scope is mng.scopes[g] assert g.recursive is mng.recursive[g]
def test_clone_without_forcing_manager(): @clone @parse def f(x, y): return x * y clone(f) clone(f) GraphManager(f)
def test_manager_exclusivity(): @parse def f(x): return x * x mng = manage(f) assert f._manager is mng with pytest.raises(ManagerError): GraphManager(f)
def test(): def _replace(tr, uses, value): for node, key in uses: tr.set_edge(node, key, value) gfn = clone(parse(fn)) todo = [[] for stage in stages] mng = GraphManager(gfn) with mng.transact() as tr: for node in mng.all_nodes: if node.is_constant() and node.value in swaps: j = swaps.index(node.value) for swap_node, i in mng.uses[node]: assert i == 0 uses = set(mng.uses[swap_node]) _, first, second = swap_node.inputs todo[j + 1].append((uses, second)) tr.replace(swap_node, first) mng.reset() for stage, operations in zip(stages, todo): with mng.transact() as tr: for uses, new_node in operations: _replace(tr, uses, new_node) _check_uses(mng) stage.check(mng)
def test_weak_manager(): @clone @parse def f(x, y): return x * y mng1 = GraphManager(f, manage=False) assert f._manager is None with pytest.raises(ManagerError): with mng1.transact(): pass with pytest.raises(ManagerError): mng1.set_parameters(f, f.parameters) with pytest.raises(ManagerError): mng1.replace(f.output, f.output) with pytest.raises(ManagerError): mng1.set_edge(f.return_, 1, f.output) mng2 = GraphManager(f, manage=True) assert f._manager is mng2 GraphManager(f, manage=False) assert f._manager is mng2