Пример #1
0
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)
Пример #2
0
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)
Пример #3
0
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
}
""")
Пример #4
0
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
Пример #5
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)
Пример #6
0
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"
Пример #7
0
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) {
Пример #8
0
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"
Пример #9
0
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)
Пример #10
0
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
}
""")
Пример #11
0
def _graph_for_inline():
    target = Graph()
    target.debug.name = 'target'
    target.output = THREE
    return target
Пример #12
0
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', [
    p1,
    c,
    mid,
])
def test_anfnode(node):
    node2 = dumpload(node)
    assert type(node) is type(node2)
    assert len(node.inputs) == len(node2.inputs)
    # more inputs assert?