コード例 #1
0
 def test_constant(self):
     x = Constant(MyType(), 2, name="x")
     y = MyVariable("y")
     z = Constant(MyType(), 2, name="z")
     e = op1(op1(x, y), y)
     g = FunctionGraph([y], [e])
     PatternOptimizer((op1, z, "1"), (op2, "1", z)).optimize(g)
     assert str(g) == "FunctionGraph(Op1(Op2(y, z), y))"
コード例 #2
0
ファイル: test_opt.py プロジェクト: ricardoV94/aesara
 def test_identical_constant_args(self):
     x = MyVariable("x")
     y = Constant(MyType(), 2, name="y")
     z = Constant(MyType(), 2, name="z")
     with config.change_flags(compute_test_value="off"):
         e1 = op1(y, z)
     g = FunctionGraph([x, y, z], [e1])
     MergeOptimizer().optimize(g)
     strg = str(g)
     assert strg == "FunctionGraph(Op1(y, y))" or strg == "FunctionGraph(Op1(z, z))"
コード例 #3
0
ファイル: test_opt.py プロジェクト: ricardoV94/aesara
 def test_constant_merging(self):
     x = MyVariable("x")
     y = Constant(MyType(), 2, name="y")
     z = Constant(MyType(), 2, name="z")
     e = op1(op2(x, y), op2(x, y), op2(x, z))
     g = FunctionGraph([x, y, z], [e])
     MergeOptimizer().optimize(g)
     strg = str(g)
     assert (strg == "FunctionGraph(Op1(*1 -> Op2(x, y), *1, *1))"
             or strg == "FunctionGraph(Op1(*1 -> Op2(x, z), *1, *1))")
コード例 #4
0
    def test_identical_constant_args(self):
        x = MyVariable("x")
        y = Constant(MyType(), 2, name="y")
        z = Constant(MyType(), 2, name="z")
        e1 = op1(y, z)
        g = FunctionGraph([x, y, z], [e1], clone=False)
        MergeOptimizer().optimize(g)

        assert g.outputs[0].owner.op == op1
        input_1 = g.outputs[0].owner.inputs[0]
        assert input_1 is g.outputs[0].owner.inputs[1]
コード例 #5
0
 def test_constant_merging(self):
     x = MyVariable("x")
     y = Constant(MyType(), 2, name="y")
     z = Constant(MyType(), 2, name="z")
     e = op1(op2(x, y), op2(x, y), op2(x, z))
     g = FunctionGraph([x, y, z], [e], clone=False)
     MergeOptimizer().optimize(g)
     out_var = g.outputs[0]
     var_1, var_2, var_3 = out_var.owner.inputs
     assert var_1 is var_2
     assert var_2 is var_3
コード例 #6
0
def test_pre_greedy_local_optimizer():

    empty_fgraph = FunctionGraph([], [])

    x = MyVariable("x")
    y = MyVariable("y")
    c1 = Constant(MyType(), 1, "c1")
    c2 = Constant(MyType(), 2, "c2")
    o1 = op2(c1, c2)
    o3 = op1(c1, y)
    o2 = op1(o1, c2, x, o3, o1)

    assert o2.owner.inputs[0].owner is not None
    assert o2.owner.inputs[4].owner is not None

    # This should fold `o1`, because it has only `Constant` arguments, and
    # replace it with the `Constant` result
    cst = pre_greedy_local_optimizer(empty_fgraph, [constant_folding], o2)

    assert cst.owner.inputs[0].owner is None
    assert cst.owner.inputs[1] is c2
    assert cst.owner.inputs[2] is x
    assert cst.owner.inputs[3] is o3
    assert cst.owner.inputs[4] is cst.owner.inputs[0]

    # We're going to do it again, except this time `o1` is
    # in the `fgraph`, so it shouldn't be folded
    fg = FunctionGraph([], [o1], clone=False)
    o2 = op1(o1, c2, x, o3, o1)

    cst = pre_greedy_local_optimizer(fg, [constant_folding], o2)

    assert cst.owner.inputs[0] is o1
    assert cst.owner.inputs[4] is cst.owner.inputs[0]

    # What exactly is this supposed to test?
    ms = MakeSlice()(1)
    cst = pre_greedy_local_optimizer(empty_fgraph, [constant_folding], ms)

    assert isinstance(cst, SliceConstant)

    # Make sure constant of slice signature is hashable.
    assert isinstance(hash(cst.signature()), int)
コード例 #7
0
def test_pre_constant_merge():

    empty_fgraph = FunctionGraph([], [])

    x = MyVariable("x")
    y = MyVariable("y")
    c1 = Constant(MyType(), 1, "c1")
    c2 = Constant(MyType(), 1, "c1")
    o1 = op2(c1, x)
    o2 = op1(o1, y, c2)

    assert c1 is not c2

    res = pre_constant_merge(empty_fgraph, [o2])

    assert [o2] == res
    assert o2.owner.inputs[2] is c1

    o2 = op1(o1, y, c2)
    fg = FunctionGraph([x, y], [o2], clone=False)

    assert o2.owner in fg.apply_nodes

    res = pre_constant_merge(fg, [o2])

    assert res == [o2]
    assert o2.owner.inputs[2] is c2

    # What is this supposed to test?
    ms = MakeSlice()(1)
    res = pre_constant_merge(empty_fgraph, [ms])

    assert res == [ms]

    const_slice = SliceConstant(type=slicetype, data=slice(1, None, 2))

    assert isinstance(const_slice, Constant)

    adv = AdvancedSubtensor()(matrix(), [2, 3], const_slice)

    res = pre_constant_merge(empty_fgraph, adv)
    assert res == [adv]
コード例 #8
0
    def test_nominals(self):
        t1 = MyType()

        nm = NominalVariable(1, t1)
        nm2 = NominalVariable(2, t1)

        v1 = op1(nm, nm2)

        fg = FunctionGraph(outputs=[v1], clone=False)

        assert nm not in fg.inputs
        assert nm2 not in fg.inputs
        assert nm in fg.variables
        assert nm2 in fg.variables
コード例 #9
0
 def make_node(self):
     return Apply(self, [], [MyType()()])
コード例 #10
0
def test_unify_Variable():
    x_at = at.vector("x")
    y_at = at.vector("y")

    z_at = x_at + y_at

    # `Variable`, `Variable`
    s = unify(z_at, z_at)
    assert s == {}

    # These `Variable`s have no owners
    v1 = MyType()()
    v2 = MyType()()

    assert v1 != v2

    s = unify(v1, v2)
    assert s is False

    op_lv = var()
    z_pat_et = etuple(op_lv, x_at, y_at)

    # `Variable`, `ExpressionTuple`
    s = unify(z_at, z_pat_et, {})

    assert op_lv in s
    assert s[op_lv] == z_at.owner.op

    res = reify(z_pat_et, s)

    assert isinstance(res, ExpressionTuple)
    assert equal_computations([res.evaled_obj], [z_at])

    z_et = etuple(at.add, x_at, y_at)

    # `ExpressionTuple`, `ExpressionTuple`
    s = unify(z_et, z_pat_et, {})

    assert op_lv in s
    assert s[op_lv] == z_et[0]

    res = reify(z_pat_et, s)

    assert isinstance(res, ExpressionTuple)
    assert equal_computations([res.evaled_obj], [z_et.evaled_obj])

    # `ExpressionTuple`, `Variable`
    s = unify(z_et, x_at, {})
    assert s is False

    # This `Op` doesn't expand into an `ExpressionTuple`
    op1_np = CustomOpNoProps(1)

    q_at = op1_np(x_at, y_at)

    a_lv = var()
    b_lv = var()
    # `Variable`, `ExpressionTuple`
    s = unify(q_at, etuple(op1_np, a_lv, b_lv))

    assert s[a_lv] == x_at
    assert s[b_lv] == y_at
コード例 #11
0
 def make_node(self, *inputs):
     outputs = [MyType()(), MyType()()]
     return Apply(self, list(inputs), outputs)