def test_nested():
    notimpl = NotImplementedOp()
    ifelseifelseif = IfElseIfElseIf()

    x1 = tt.scalar("x1")
    x2 = tt.scalar("x2")
    c1 = tt.scalar("c1")
    c2 = tt.scalar("c2")
    t1 = ifelse(c1, x1, notimpl(x2))
    t1.name = "t1"
    t2 = t1 * 10
    t2.name = "t2"
    t3 = ifelse(c2, t2, x1 + t1)
    t3.name = "t3"
    t4 = ifelseifelseif(tt.eq(x1, x2), x1, tt.eq(x1, 5), x2, c2, t3, t3 + 0.5)
    t4.name = "t4"

    linker = aesara.gof.vm.VM_Linker(lazy=False)
    f = function([c1, c2, x1, x2],
                 t4,
                 mode=Mode(linker=linker, optimizer="fast_run"))
    with pytest.raises(NotImplementedOpException):
        f(1, 0, np.array(10, dtype=x1.dtype), 0)

    linker = aesara.gof.vm.VM_Linker(lazy=True)
    f = function([c1, c2, x1, x2],
                 t4,
                 mode=Mode(linker=linker, optimizer="fast_run"))
    assert f(1, 0, np.array(10, dtype=x1.dtype), 0) == 20.5
    def test_ifelse(self):
        config1 = aesara.config.profile
        config2 = aesara.config.profile_memory

        try:
            aesara.config.profile = True
            aesara.config.profile_memory = True

            a, b = tt.scalars("a", "b")
            x, y = tt.scalars("x", "y")

            z = ifelse(tt.lt(a, b), x * 2, y * 2)

            p = aesara.ProfileStats(False, gpu_checks=False)

            if aesara.config.mode in ["DebugMode", "DEBUG_MODE", "FAST_COMPILE"]:
                m = "FAST_RUN"
            else:
                m = None

            f_ifelse = aesara.function(
                [a, b, x, y], z, profile=p, name="test_ifelse", mode=m
            )

            val1 = 0.0
            val2 = 1.0
            big_mat1 = 10
            big_mat2 = 11

            f_ifelse(val1, val2, big_mat1, big_mat2)

        finally:
            aesara.config.profile = config1
            aesara.config.profile_memory = config2
Beispiel #3
0
def test_jax_ifelse():

    true_vals = np.r_[1, 2, 3]
    false_vals = np.r_[-1, -2, -3]

    x = ifelse(np.array(True), true_vals, false_vals)
    x_fg = FunctionGraph([], [x])

    compare_jax_and_py(x_fg, [])

    a = dscalar("a")
    a.tag.test_value = np.array(0.2, dtype=config.floatX)
    x = ifelse(a < 0.5, true_vals, false_vals)
    x_fg = FunctionGraph([a], [x])  # I.e. False

    compare_jax_and_py(x_fg, [get_test_value(i) for i in x_fg.inputs])
def test_ifelse():
    a = tt.scalar()
    b = generic()
    c = generic()

    notimpl = NotImplementedOp()

    lazys = [True]
    # We need lazy to end up being True for this test.
    if aesara.config.vm.lazy in [True, None]:
        lazys = [True, None]

    cloops = [True, False]

    if aesara.config.cxx == "":
        cloops = [False]

    for cloop in cloops:
        for lazy in lazys:
            linker = aesara.gof.vm.VM_Linker(use_cloop=cloop, lazy=lazy)
            f = function(
                [a, b, c],
                ifelse(a, notimpl(b), c),
                mode=Mode(linker=linker, optimizer="fast_run"),
            )

            with pytest.raises(NotImplementedOpException):
                f(1, "a", "b")

            assert f(0, "a", "b") == "b"
Beispiel #5
0
    def test_callback_with_ifelse(self):
        a, b, c = scalars("abc")
        f = function(
            [a, b, c],
            ifelse(a, 2 * b, 2 * c),
            mode=Mode(optimizer=None, linker=VMLinker(callback=self.callback)),
        )

        f(1, 2, 3)
        assert self.n_callbacks["IfElse"] == 2
Beispiel #6
0
def test_c_thunks():
    a = tensor.scalars("a")
    b, c = tensor.vectors("bc")
    cases = [False]
    if aesara.config.cxx:
        cases.append(True)
    for c_thunks in cases:
        f = function(
            [a, b, c],
            ifelse(a, a * b, b * c),
            mode=Mode(optimizer=None,
                      linker=vm.VM_Linker(c_thunks=c_thunks, use_cloop=False)),
        )
        f(1, [2], [3, 2])
        with pytest.raises(ValueError):
            f(0, [2], [3, 4])
        assert any([hasattr(t, "cthunk") for t in f.fn.thunks]) == c_thunks
Beispiel #7
0
 def build_graph(x, depth=5):
     z = x
     for d in range(depth):
         z = ifelse(z.mean() > 0.5, -z, z)
     return z
Beispiel #8
0
 def build_graph(x, depth=5):
     z = x
     for d in range(depth):
         z = ifelse(z[0] > 0, -z, z)
     return z
Beispiel #9
0
import time

import numpy as np

import aesara
from aesara import tensor as tt
from aesara.ifelse import ifelse


a, b = tt.scalars("a", "b")
x, y = tt.matrices("x", "y")

z_switch = tt.switch(tt.lt(a, b), tt.mean(x), tt.mean(y))
z_lazy = ifelse(tt.lt(a, b), tt.mean(x), tt.mean(y))

f_switch = aesara.function([a, b, x, y], z_switch)
f_lazyifelse = aesara.function([a, b, x, y], z_lazy)

val1 = 0.0
val2 = 1.0
big_mat1 = np.ones((10000, 1000))
big_mat2 = np.ones((10000, 1000))

n_times = 10

tic = time.clock()
for i in range(n_times):
    f_switch(val1, val2, big_mat1, big_mat2)
print("time spent evaluating both values %f sec" % (time.clock() - tic))

tic = time.clock()