def test_dfs_graphs(): g0 = Graph() in0 = Constant(g0) in1 = Constant(1) g0.return_ = in1 value = Apply([in0], Graph()) assert set(dfs(value)) == {value, in0} assert set(dfs(value, follow_graph=True)) == {value, in0, in1}
def test_clone_dangling_parameters(): g = Graph() g.output = Parameter(g) g2 = clone(g) assert isinstance(g2.output, Parameter) assert g2.output.graph == g2 assert len(g2.parameters) == 0
async def bad(info, x): badg = Graph() badg.debug.name = 'badg' p = badg.add_parameter() p.debug.name = 'parameter' badg.output = badg.apply(P.scalar_add, p, p) # The return value of the macro can't directly refer to badg.output # because that node can only be accessed from the scope of a call to # badg. The error raised by Myia should reflect this. return info.graph.apply(P.transpose, badg.output)
def test_print_graph(): g = Graph() g.debug.name = "testfn" p = g.add_parameter() p.debug.name = "a" p2 = g.add_parameter() p2.debug.name = "b" c = g.constant(1) g.output = g.apply(g, c, p2) g.output.debug.name = "_apply0" s = print_graph(g) assert (s == """graph testfn(%a, %b) { %_apply0 = @testfn(1, %b) return %_apply0 } """) # We use fake types here because we only care about the printing logic, # not the proper use of types. p.abstract = "int64" p2.abstract = "float32" g.output.abstract = "bool" g.return_.abstract = "bool" s = print_graph(g) assert (s == """graph testfn(%a : int64, %b : float32) -> bool { %_apply0 = @testfn(1, %b) ; type=bool return %_apply0 } """)
def test_print_node(): g = Graph() g.debug.name = "testfn" p = g.add_parameter() p.debug.name = "a" p2 = g.add_parameter() p2.debug.name = "b" c = g.constant(1) g.output = g.apply(g, c, p2) g.output.debug.name = "_apply0" s = print_node(g.output) assert s == "%_apply0 = @testfn(1, %b)\n"
def test_print_graph(): g = Graph() g.debug.name = 'testfn' p = g.add_parameter() p.debug.name = 'a' p2 = g.add_parameter() p2.debug.name = 'b' c = g.constant(1) g.output = g.apply(g, c, p2) g.output.debug.name = '_apply0' s = print_graph(g) assert s == """graph testfn(%a, %b) {
def test_print_closure(): g = Graph() g.debug.name = "testfn" p = g.add_parameter() p.debug.name = "a" n = g.apply("make_tuple", p, p) n.debug.name = "_apply0" g2 = Graph() g2.debug.name = "sub" n2 = g2.apply("tuple_getitem", n, 0) n2.debug.name = "_apply1" g2.output = n2 g.output = g.apply(g2) s = print_graph(g2) assert (s == """graph sub() { %_apply1 = tuple_getitem(%_apply0, 0) return %_apply1 } """) s = print_node(n2) assert s == "%_apply1 = tuple_getitem(%_apply0, 0)\n"
def test_toposort(): g0 = Graph() g0.output = Constant(1) g1 = Graph() in0 = Constant(g0) value = Apply([in0], g1) g1.output = value order = list(toposort(g1.return_)) _check_toposort(order, g1.return_, succ_incoming)
def test_toposort2(): g0 = Graph() g0.output = Constant(33) g1 = Graph() in0 = Constant(g0) in1 = Constant(1) v1 = Apply([in0, in1], g1) v2 = Apply([in0, v1, in1], g1) g1.output = v2 order = list(toposort(g1.return_)) _check_toposort(order, g1.return_, succ_incoming)
def test_print_cycle(): g = Graph() g.debug.name = "testfn" p = g.add_parameter() p.debug.name = "a" p2 = g.add_parameter() p2.debug.name = "b" node = g.apply("make_tuple", p) node2 = g.apply("make_tuple", p2, node) node.inputs.append(node2) g.output = node2 with pytest.raises(ValueError, match="cycle"): print_graph(g, allow_cycles=False) print_graph(g)
def test_print_graph(): g = Graph() g.debug.name = "testfn" p = g.add_parameter() p.debug.name = "a" p2 = g.add_parameter() p2.debug.name = "b" c = g.constant(1) g.output = g.apply(g, c, p2) g.output.debug.name = "_apply0" s = print_graph(g) assert (s == """graph testfn(%a, %b) { %_apply0 = @testfn(1, %b) return %_apply0 } """)
def test_helpers(): g = Graph() cg = Constant(g) assert cg.is_constant(Graph) assert cg.is_constant_graph() one = Constant(1) assert one.is_constant() assert one.is_constant(int) assert not one.is_constant(str) assert not one.is_constant_graph() a = Apply([cg, one], g) assert a.is_apply() p = Parameter(g) assert p.is_parameter() s = Special(1234, g) assert s.is_special() assert s.is_special(int) assert not s.is_special(str)
def test_contextless(): C = CONTEXTLESS assert Contextless.empty() is C assert C.filter(Graph()) is C assert C.add(Graph(), []) is C
def _graph_for_inline(): target = Graph() target.debug.name = 'target' target.output = THREE return target
v2 = dumpload(v) assert v is v2 def test_dump_undefined(): with pytest.raises(Exception): dumpload(object()) def test_exception(): e2 = dumpload(Exception("this is bad")) assert e2.message == "Exception: this is bad\n" assert repr(e2) == 'LoadedException' g = Graph() p1 = g.add_parameter() p1.abstract = to_abstract_test(2) p2 = g.add_parameter() p2.abstract = to_abstract_test(2) mid = g.apply(scalar_add, p1, p2) mid.abstract = to_abstract_test(4) c = Constant(1) c.abstract = to_abstract_test(1) g.output = g.apply(scalar_add, mid, c) g.output.abstract = to_abstract_test(5) g.return_.abstract = to_abstract_test(5) g.return_.inputs[0].abstract = None @parametrize('node', [
def test_dfs(): in0 = Constant(0) in1 = Constant(1) value = Apply([in0, in1], Graph()) assert next(dfs(value)) == value assert set(dfs(value)) == {value, in0, in1}