Esempio n. 1
0
def test_round():
    for lib, call in helpers.calls:
        assert np.array_equal(call(ivy_gen.round, ivy_gen.array([0.3], f=lib)),
                              np.round(np.array([0.3])))
        assert np.array_equal(
            call(ivy_gen.round, ivy_gen.array([[0.51]], f=lib)),
            np.array([[1.]]))
Esempio n. 2
0
def test_linear():

    fname = os.path.join(this_file_dir, 'runtime_analysis/{}/layers/linear.txt'.format(DIM))
    if os.path.exists(fname):
        os.remove(fname)
    for lib, call in [(l, c) for l, c in helpers.calls if c not in [helpers.tf_graph_call, helpers.mx_graph_call]]:

        time_lib = LIB_DICT[lib]

        append_to_file(fname, '{}'.format(lib))

        x0 = ivy_gen.array([[random.uniform(0, 1) for _ in range(DIM)]], f=lib)
        weight = ivy_gen.array([[random.uniform(0, 1) for _ in range(DIM)],
                                [random.uniform(0, 1) for _ in range(DIM)]], f=lib)
        bias = ivy_gen.array([random.uniform(0, 1), random.uniform(0, 1)], f=lib)

        ivy_layers.linear(x0, weight, bias, num_hidden=2, f=lib)
        ivy_layers_w_time.linear(x0, weight, bias, num_hidden=2, f=time_lib)
        TIMES_DICT.clear()

        for _ in range(100):

            log_time(fname, 'tb0')
            ivy_layers_w_time.linear(x0, weight, bias, num_hidden=2, f=time_lib)
            log_time(fname, 'tb4', time_at_start=True)

            log_time(fname, 'tt0')
            ivy_layers.linear(x0, weight, bias, num_hidden=2, f=lib)
            log_time(fname, 'tt1', time_at_start=True)

        write_times()

    append_to_file(fname, 'end of analysis')
Esempio n. 3
0
def test_floor():
    for lib, call in helpers.calls:
        assert np.array_equal(call(ivy_gen.floor, ivy_gen.array([0.3], f=lib)),
                              np.floor(np.array([0.3])))
        assert np.array_equal(
            call(ivy_gen.floor, ivy_gen.array([[0.7]], f=lib)),
            np.floor(np.array([[0.7]])))
Esempio n. 4
0
def test_ceil():
    for lib, call in helpers.calls:
        assert np.array_equal(call(ivy_gen.ceil, ivy_gen.array([0.3], f=lib)),
                              np.ceil(np.array([0.3])))
        assert np.array_equal(
            call(ivy_gen.ceil, ivy_gen.array([[0.7]], f=lib)),
            np.ceil(np.array([[0.7]])))
Esempio n. 5
0
def test_unstack():
    for lib, call in helpers.calls:
        if call is helpers.mx_graph_call:
            # mxsymbolic split returns either list or tensor depending on number of splits
            continue
        x = np.swapaxes(np.array([[0.]]), 0, 0)
        true = [np.array(item) for item in x.tolist()]
        pred = call(ivy_gen.unstack,
                    ivy_gen.array([[0.]], f=lib),
                    0,
                    num_outputs=1)
        assert _reduce(
            _mul,
            [np.array_equal(pred_, true_)
             for pred_, true_ in zip(pred, true)], 1) == 1
        x = np.swapaxes(np.array([[[0.]]]), 0, 0)
        true = [np.array(item) for item in x.tolist()]
        pred = call(ivy_gen.unstack,
                    ivy_gen.array([[[0.]]], f=lib),
                    0,
                    num_outputs=1)
        assert _reduce(
            _mul,
            [np.array_equal(pred_, true_)
             for pred_, true_ in zip(pred, true)], 1) == 1
Esempio n. 6
0
def test_tile():
    for lib, call in helpers.calls:
        assert np.array_equal(
            call(ivy_gen.tile, ivy_gen.array([[0.]], f=lib), [1, 2]),
            np.tile(np.array([[0.]]), [1, 2]))
        assert np.array_equal(
            call(ivy_gen.tile, ivy_gen.array([[[0.]]], f=lib), [1, 2, 3]),
            np.tile(np.array([[[0.]]]), [1, 2, 3]))
Esempio n. 7
0
def test_split():
    for lib, call in helpers.calls:
        assert np.array_equal(
            call(ivy_gen.split, ivy_gen.array([[0., 1.]], f=lib), 2, -1),
            np.split(np.array([[0., 1.]]), 2, -1))
        assert np.array_equal(
            call(ivy_gen.split, ivy_gen.array([[[0., 1.]]], f=lib), 2, -1),
            np.split(np.array([[[0., 1.]]]), 2, -1))
Esempio n. 8
0
def test_cast():
    for lib, call in helpers.calls:
        assert np.array_equal(
            call(ivy_gen.cast, ivy_gen.array([0], f=lib), 'float32'),
            np.array([0]).astype(np.float32))
        assert np.array_equal(
            call(ivy_gen.cast, ivy_gen.array([[0]], f=lib), 'float32'),
            np.array([[0]]).astype(np.float32))
Esempio n. 9
0
def test_abs():
    for lib, call in helpers.calls:
        assert np.allclose(call(ivy_gen.abs, ivy_gen.array([-0.3], f=lib)),
                           np.array([0.3]),
                           atol=1e-6)
        assert np.allclose(call(ivy_gen.abs, ivy_gen.array([[-0.7]], f=lib)),
                           np.array([[0.7]]),
                           atol=1e-6)
Esempio n. 10
0
def test_logical_not():
    for lib, call in helpers.calls:
        assert np.array_equal(
            call(ivy_logic.logical_not, ivy_gen.array([True, True], f=lib)),
            np.logical_not(np.array([True, True])))
        assert np.array_equal(
            call(ivy_logic.logical_not, ivy_gen.array([[0.]], f=lib)),
            np.logical_not(np.array([[0.]])))
Esempio n. 11
0
def test_clip():
    for lib, call in helpers.calls:
        assert np.array_equal(
            call(ivy_gen.clip, ivy_gen.array([0.], f=lib), 0, 1),
            np.clip(np.array([0.]), 0, 1))
        assert np.array_equal(
            call(ivy_gen.clip, ivy_gen.array([[0.]], f=lib), 0, 1),
            np.clip(np.array([[0.]]), 0, 1))
Esempio n. 12
0
def test_reduce_mean():
    for lib, call in helpers.calls:
        assert np.array_equal(call(ivy_red.reduce_mean, ivy_gen.array([1., 2., 3.], f=lib), 0, True),
                              np.mean(np.array([1., 2., 3.]), keepdims=True))
        assert np.array_equal(call(ivy_red.reduce_mean, ivy_gen.array([1., 2., 3.], f=lib), (0,), True),
                              np.mean(np.array([1., 2., 3.]), keepdims=True))
        assert np.array_equal(call(ivy_red.reduce_mean, ivy_gen.array([[1., 2., 3.]], f=lib), (0, 1), True),
                              np.mean(np.array([[1., 2., 3.]]), keepdims=True))
Esempio n. 13
0
def test_pinv():
    for lib, call in helpers.calls:
        if call is helpers.mx_graph_call:
            # mxnet symbolic does not support pinv
            continue
        assert np.allclose(call(ivy_linalg.pinv, ivy_gen.array([[1., 0.], [0., 1.], [1., 0.]], f=lib)),
                              np.linalg.pinv(np.array([[1., 0.], [0., 1.], [1., 0.]])), atol=1e-6)
        assert np.allclose(call(ivy_linalg.pinv, ivy_gen.array([[[1., 0.], [0., 1.], [1., 0.]]], f=lib)),
                              np.linalg.pinv(np.array([[[1., 0.], [0., 1.], [1., 0.]]])), atol=1e-6)
Esempio n. 14
0
def test_random_shuffle():
    for lib, call in helpers.calls:
        call(ivy_rand.seed, 0, f=lib)
        first_shuffle = call(ivy_rand.shuffle, ivy_gen.array([1, 2, 3], f=lib),
                             lib)
        call(ivy_rand.seed, 0, f=lib)
        second_shuffle = call(ivy_rand.shuffle, ivy_gen.array([1, 2, 3],
                                                              f=lib), lib)
        assert np.array(first_shuffle == second_shuffle).all()
Esempio n. 15
0
def test_concatenate():
    for lib, call in helpers.calls:
        assert np.array_equal(
            call(ivy_gen.concatenate,
                 (ivy_gen.array([0.], f=lib), ivy_gen.array([0.], f=lib)), 0),
            np.concatenate((np.array([0.]), np.array([0.])), 0))
        assert np.array_equal(
            call(ivy_gen.concatenate,
                 (ivy_gen.array([[0.]], f=lib), ivy_gen.array([[0.]], f=lib)),
                 0), np.concatenate((np.array([[0.]]), np.array([[0.]])), 0))
Esempio n. 16
0
def test_argmin():
    for lib, call in helpers.calls:
        assert np.allclose(call(ivy_gen.argmin,
                                ivy_gen.array([-0.3, 0.1], f=lib)),
                           np.array([0]),
                           atol=1e-6)
        assert np.allclose(call(
            ivy_gen.argmin, ivy_gen.array([[1.3, -0.7], [0.1, 2.5]], f=lib)),
                           np.array([1, 0]),
                           atol=1e-6)
Esempio n. 17
0
def test_norm():
    for lib, call in helpers.calls:
        assert np.array_equal(call(ivy_linalg.norm, ivy_gen.array([[1., 0.], [0., 1.]], f=lib), 1),
                              np.linalg.norm(np.array([[1., 0.], [0., 1.]]), 1, -1))
        assert np.array_equal(call(ivy_linalg.norm, ivy_gen.array([[1., 0.], [0., 1.]], f=lib), 1, 1),
                              np.linalg.norm(np.array([[1., 0.], [0., 1.]]), 1, 1))
        assert np.array_equal(call(ivy_linalg.norm, ivy_gen.array([[1., 0.], [0., 1.]], f=lib), 1, 1, True),
                              np.linalg.norm(np.array([[1., 0.], [0., 1.]]), 1, 1, True))
        assert np.array_equal(call(ivy_linalg.norm, ivy_gen.array([[[1., 0.], [0., 1.]]], f=lib), 2),
                              np.linalg.norm(np.array([[[1., 0.], [0., 1.]]]), 2, -1))