Ejemplo n.º 1
0
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}
Ejemplo n.º 2
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)
Ejemplo n.º 3
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)
Ejemplo n.º 4
0
def opt_newg(optimizer, node, equiv):
    from myia.ir import clone

    @parse
    def newf(a, b):
        return a - (a + b)
    return Constant(clone(newf))
Ejemplo n.º 5
0
def _opt_fancy_resolve(optimizer, node, equiv):
    ns = equiv[V1]
    name = equiv[V2]
    with About(node.debug, 'cosmetic'):
        lbl = f'{ns.value.label}.{name.value}'
        ct = Constant(GraphCosmeticPrimitive(lbl))
        return ct
Ejemplo n.º 6
0
def opt_newg_bad(optimizer, node, equiv):
    from myia.ir import clone

    @parse
    def newf(a):
        return a + a
    return Constant(clone(newf))
Ejemplo n.º 7
0
def _opt_fancy_transpose(optimizer, node, equiv):
    if equiv[V].value == (1, 0):
        x = equiv[X]
        ct = Constant(GraphCosmeticPrimitive(f'T', on_edge=True))
        with About(node.debug, 'cosmetic'):
            return Apply([ct, x], node.graph)
    else:
        return node
Ejemplo n.º 8
0
def test_sexp_conversion():
    def f():
        return 10 * (5 + 4)

    sexp = (prim.scalar_mul, 10, (prim.scalar_add, 5, Constant(4)))

    g = sexp_to_graph(sexp)

    assert isomorphic(g, parse(f))
Ejemplo n.º 9
0
def _opt_fancy_array_map(optimizer, node, equiv):
    xs = equiv[Xs]
    v = equiv[V]
    if v.is_constant_graph():
        return node
    name = short_labeler.label(v)
    ct = Constant(GraphCosmeticPrimitive(f'[{name}]'))
    with About(node.debug, 'cosmetic'):
        return Apply([ct, *xs], node.graph)
Ejemplo n.º 10
0
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)
Ejemplo n.º 11
0
Archivo: gprint.py Proyecto: tor4z/myia
def _opt_fancy_scalar_to_array(optimizer, node, equiv):
    x = equiv[X]
    ct = Constant(GraphCosmeticPrimitive(f"to_array", on_edge=True))
    with About(node.debug, "cosmetic"):
        return Apply([ct, x], node.graph)
Ejemplo n.º 12
0
def _opt_fancy_array_to_scalar(optimizer, node, equiv):
    x = equiv[X]
    ct = Constant(GraphCosmeticPrimitive(f'to_scalar', on_edge=True))
    with About(node.debug, 'cosmetic'):
        return Apply([ct, x], node.graph)
Ejemplo n.º 13
0
def _opt_fancy_distribute(optimizer, node, equiv):
    x = equiv[X]
    v = equiv[V]
    ct = Constant(GraphCosmeticPrimitive(f'shape→{v.value}', on_edge=True))
    with About(node.debug, 'cosmetic'):
        return Apply([ct, x], node.graph)
Ejemplo n.º 14
0
def _opt_fancy_unsafe_static_cast(optimizer, node, equiv):
    x = equiv[X]
    ct = Constant(GraphCosmeticPrimitive(f'cast', on_edge=True))
    with About(node.debug, 'cosmetic'):
        return Apply([ct, x], node.graph)
Ejemplo n.º 15
0
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}
Ejemplo n.º 16
0
def _opt_fancy_make_tuple(optimizer, node, equiv):
    xs = equiv[Xs]
    ct = Constant(GraphCosmeticPrimitive('(...)'))
    with About(node.debug, 'cosmetic'):
        return Apply([ct, *xs], node.graph)
Ejemplo n.º 17
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)
Ejemplo n.º 18
0
def _opt_fancy_sum(optimizer, node, equiv):
    x = equiv[X]
    shp = equiv[V].value
    ct = Constant(GraphCosmeticPrimitive(f'sum {"x".join(map(str, shp))}'))
    with About(node.debug, 'cosmetic'):
        return Apply([ct, x], node.graph)
Ejemplo n.º 19
0
Archivo: gprint.py Proyecto: tor4z/myia
def _opt_fancy_tagged(optimizer, node, equiv):
    x = equiv[X]
    v = equiv[V]
    ct = Constant(GraphCosmeticPrimitive(f"@{v.value}", on_edge=True))
    with About(node.debug, "cosmetic"):
        return Apply([ct, x], node.graph)
Ejemplo n.º 20
0
def test_get_resolved():
    eng = standard_pipeline.make().resources.inferrer.engine
    ref = eng.ref(Constant(123), Context.empty())
    with pytest.raises(InternalInferenceError):
        ref.get_resolved()
Ejemplo n.º 21
0
def test_to_abstract_skey():
    inst = SymbolicKeyInstance(Constant(123), 456)
    expected = AbstractScalar({VALUE: inst, TYPE: ty.SymbolicKeyType})
    assert to_abstract(inst) == expected
Ejemplo n.º 22
0
    g = parse(f2)
    idx0 = GraphIndex(g)

    cl1 = GraphCloner(g, clone_constants=True, total=True)
    idx1 = GraphIndex(cl1[g])
    assert idx1['f2'] is not idx0['f2']
    assert idx1['f1'] is not idx0['f1']

    cl2 = GraphCloner(g, clone_constants=True, total=False)
    idx2 = GraphIndex(cl2[g])
    assert idx2['f2'] is not idx0['f2']
    assert idx2['f1'] is idx0['f1']


ONE = Constant(1)
TWO = Constant(2)
THREE = Constant(3)


def _graph_for_inline():
    target = Graph()
    target.debug.name = 'target'
    target.output = THREE
    return target


def _successful_inlining(cl, orig, new_params, target):
    assert cl[orig] is not target
    assert cl[orig] is orig
Ejemplo n.º 23
0
def test_to_abstract():
    inst = SymbolicKeyInstance(Constant(123), 456)
    assert to_abstract(inst) == _S({VALUE: inst, TYPE: ty.SymbolicKeyType})