Beispiel #1
0
def mnp_clip(x):
    a = mnp.clip(x, mnp.asarray(10.0), mnp.asarray([
        2,
    ]))
    b = mnp.clip(x, 0, 1)
    c = mnp.clip(x, mnp.asarray(0), mnp.asarray(10), dtype=mnp.float32)
    return a, b, c
def test_asarray():
    test_case = Cases()
    for array in test_case.array_sets:
        # Check for dtype matching
        actual = onp.asarray(array)
        expected = mnp.asarray(array).asnumpy()
        # Since we set float32/int32 as the default dtype in mindspore, we need
        # to make a conversion between numpy.asarray and mindspore.numpy.asarray
        if actual.dtype is onp.dtype('float64'):
            assert expected.dtype == onp.dtype('float32')
        elif actual.dtype is onp.dtype('int64'):
            assert expected.dtype == onp.dtype('int32')
        else:
            assert actual.dtype == expected.dtype
        match_array(actual, expected, error=7)

        for i in range(len(test_case.onp_dtypes)):
            actual = onp.asarray(array, test_case.onp_dtypes[i])
            expected = mnp.asarray(array, test_case.mnp_dtypes[i]).asnumpy()
            match_array(actual, expected, error=7)

    # Additional tests for nested tensor mixture
    mnp_input = [(mnp.ones(3, ), mnp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
    onp_input = [(onp.ones(3, ), onp.ones(3)), [[1, 1, 1], (1, 1, 1)]]

    actual = onp.asarray(onp_input)
    expected = mnp.asarray(mnp_input).asnumpy()
    match_array(actual, expected, error=7)
def test_append():
    onp_array = onp.random.random((4, 3, 2)).astype('float32')
    onp_value = onp.random.random((4, 3, 2)).astype('float32')
    mnp_array = mnp.asarray(onp_array)
    mnp_value = mnp.asarray(onp_value)
    onp_res = onp_append(onp_array, onp_value)
    mnp_res = mnp_append(mnp_array, mnp_value)
    check_all_results(onp_res, mnp_res)
Beispiel #4
0
def test_type_promotion():
    arr = rand_int(2, 3)
    onp_sum = onp_add(arr, arr)

    a = mnp.asarray(arr, dtype='float16')
    b = mnp.asarray(arr, dtype='float32')
    c = mnp.asarray(arr, dtype='int32')

    match_array(mnp_add(a, b).asnumpy(), onp_sum)
    match_array(mnp_add(b, c).asnumpy(), onp_sum)
Beispiel #5
0
def test_squeeze():
    onp_array = onp.random.random((1, 3, 1, 4, 2)).astype('float32')
    mnp_array = mnp.asarray(onp_array)
    o_squeezed = onp_squeeze(onp_array)
    m_squeezed = mnp_squeeze(mnp_array)
    check_all_results(o_squeezed, m_squeezed)

    onp_array = onp.random.random((1, 1, 1, 1, 1)).astype('float32')
    mnp_array = mnp.asarray(onp_array)
    o_squeezed = onp_squeeze(onp_array)
    m_squeezed = mnp_squeeze(mnp_array)
    check_all_results(o_squeezed, m_squeezed)
def test_array():
    # array's function is very similar to asarray, so we mainly test the
    # `copy` argument.
    test_case = Cases()
    for array in test_case.array_sets:
        arr1 = mnp.asarray(array)
        arr2 = mnp.array(arr1, copy=False)
        arr3 = mnp.array(arr1)
        arr4 = mnp.asarray(array, dtype='int32')
        arr5 = mnp.asarray(arr4, dtype=mnp.int32)
        assert arr1 is arr2
        assert arr1 is not arr3
        assert arr4 is arr5
Beispiel #7
0
def test_cumsum():
    x = mnp.ones((16, 16), dtype="bool")
    match_array(mnp.cumsum(x).asnumpy(), onp.cumsum(x.asnumpy()))
    match_array(
        mnp.cumsum(x, axis=0).asnumpy(), onp.cumsum(x.asnumpy(), axis=0))
    match_meta(mnp.cumsum(x).asnumpy(), onp.cumsum(x.asnumpy()))

    x = rand_int(3, 4, 5)
    match_array(
        mnp.cumsum(mnp.asarray(x), dtype="bool").asnumpy(),
        onp.cumsum(x, dtype="bool"))
    match_array(
        mnp.cumsum(mnp.asarray(x), axis=-1).asnumpy(), onp.cumsum(x, axis=-1))
Beispiel #8
0
def test_ndarray_astype():
    onp_array = onp.random.random((3, 4, 5)).astype('float32')
    mnp_array = mnp.asarray(onp_array)
    o_astype = onp_ndarray_astype(onp_array)
    m_astype = mnp_ndarray_astype(mnp_array)
    for arr1, arr2 in zip(o_astype, m_astype):
        assert arr1.dtype == arr2.asnumpy().dtype
Beispiel #9
0
def run_single_test(mnp_fn, onp_fn, arr, error=0):
    mnp_arr = mnp.asarray(arr)
    for actual, expected in zip(mnp_fn(mnp_arr), onp_fn(arr)):
        if isinstance(expected, tuple):
            for actual_arr, expected_arr in zip(actual, expected):
                match_array(actual_arr.asnumpy(), expected_arr, error)
        match_array(actual.asnumpy(), expected, error)
Beispiel #10
0
def test_tensor_flatten():
    lst = [[1.0, 2.0], [3.0, 4.0]]
    tensor_list = mnp.asarray(lst)
    assert tensor_list.flatten().asnumpy().tolist() == [1.0, 2.0, 3.0, 4.0]
    assert tensor_list.flatten(order='F').asnumpy().tolist() == [
        1.0, 3.0, 2.0, 4.0
    ]
Beispiel #11
0
def test_tensor_ravel():
    lst = [[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0]]
    tensor_list = mnp.asarray(lst)
    assert tensor_list.ravel().shape == (8, )
    assert tensor_list.ravel().asnumpy().tolist() == [
        1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0
    ]
Beispiel #12
0
def test_negative():
    arr = onp.arange(-6, 6).reshape((2, 2, 3)).astype('float32')
    out_lst = [
        onp.ones((2, 2, 3)).astype('float32'),
        onp.ones((5, 2, 2, 3)).astype('float32')
    ]
    where_lst = [
        onp.full((2, 2, 3), [True, False, True]),
        onp.full((2, 3), False)
    ]
    for out in out_lst:
        for where in where_lst:
            onp_neg = onp_negative(arr, out=out, where=where)
            mnp_neg = mnp_negative(mnp.asarray(arr), mnp.asarray(out),
                                   mnp.asarray(where))
            match_array(mnp_neg.asnumpy(), onp_neg, 1e-5)
def test_tril_indices_from():
    m = int(rand_int().tolist())
    n = int(rand_int().tolist())
    t = mnp.asarray(rand_int(m, n).tolist())
    k = rand_int().tolist()
    mnp_res = mnp.tril_indices_from(t, k)
    onp_res = onp.tril_indices_from(t.asnumpy(), k)
    match_all_arrays(mnp_res, onp_res)
Beispiel #14
0
def test_tensor_swapaxes():
    lst = [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]
    tensor_list = mnp.asarray(lst)
    with pytest.raises(TypeError):
        tensor_list = tensor_list.swapaxes(0, (1,))
    with pytest.raises(ValueError):
        tensor_list = tensor_list.swapaxes(0, 3)
    assert tensor_list.swapaxes(0, 1).shape == (3, 2)
Beispiel #15
0
def test_tensor_squeeze():
    lst = [[[1.0], [2.0], [3.0]]]
    tensor_list = mnp.asarray(lst)
    with pytest.raises(TypeError):
        tensor_list = tensor_list.squeeze(1.2)
    with pytest.raises(ValueError):
        tensor_list = tensor_list.squeeze(4)
    assert tensor_list.squeeze().shape == (3,)
    assert tensor_list.squeeze(axis=2).shape == (1, 3)
Beispiel #16
0
def test_tensor_reshape():
    lst = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]
    tensor_list = mnp.asarray(lst)
    with pytest.raises(TypeError):
        tensor_list = tensor_list.reshape({0, 1, 2})
    with pytest.raises(ValueError):
        tensor_list = tensor_list.reshape(1, 2, 3)
    assert tensor_list.reshape([-1, 4]).shape == (2, 4)
    assert tensor_list.reshape(1, -1, 4).shape == (1, 2, 4)
Beispiel #17
0
def test_dsplit():
    onp_arrs = [onp.random.randint(1, 5, size=(5, 4, 9)).astype('float32')]
    mnp_arrs = [mnp.asarray(arr) for arr in onp_arrs]
    for onp_arr, mnp_arr in zip(onp_arrs, mnp_arrs):
        o_dsplit = onp_dsplit(onp_arr)
        m_dsplit = mnp_dsplit(mnp_arr)
        for expect_lst, actual_lst in zip(o_dsplit, m_dsplit):
            for expect, actual in zip(expect_lst, actual_lst):
                match_array(expect, actual.asnumpy())
Beispiel #18
0
def construct_arrays(n=1, ndim=1, axis=None, low=1, high=5):
    onp_array_lst = []
    mnp_array_lst = []
    shape = onp.random.randint(low=low, high=high, size=ndim)
    new_shape = [sh for sh in shape]
    while n > 0:
        n -= 1
        onp_array1 = onp.random.randint(
            low=low, high=high, size=shape).astype(onp.float32)
        onp_array_lst.append(onp_array1)
        mnp_array_lst.append(mnp.asarray(onp_array1))
        if axis is not None and axis < ndim:
            new_shape[axis] += onp.random.randint(2)
            onp_array2 = onp.random.randint(
                low=low, high=high, size=new_shape).astype(onp.float32)
            onp_array_lst.append(onp_array2)
            mnp_array_lst.append(mnp.asarray(onp_array2))
    return onp_array_lst, mnp_array_lst
def test_pad_gpu():
    x_np = onp.random.random([2, 1, 4, 3]).astype("float32")
    x_ms = mnp.asarray(x_np.tolist())

    # pad symmetric odd
    mnp_res = mnp.pad(x_ms, ((10, 3), (5, 2), (3, 0), (2, 6)),
                      mode='symmetric',
                      reflect_type='odd')
    onp_res = onp.pad(x_np, ((10, 3), (5, 2), (3, 0), (2, 6)),
                      mode='symmetric',
                      reflect_type='odd')
    match_all_arrays(mnp_res, onp_res, error=1e-5)

    # pad symmetric even
    mnp_res = mnp.pad(x_ms, ((10, 13), (5, 12), (3, 0), (2, 6)),
                      mode='symmetric',
                      reflect_type='even')
    onp_res = onp.pad(x_np, ((10, 13), (5, 12), (3, 0), (2, 6)),
                      mode='symmetric',
                      reflect_type='even')
    match_all_arrays(mnp_res, onp_res, error=1e-5)

    # pad reflect odd
    mnp_res = mnp.pad(x_ms, ((10, 3), (5, 2), (3, 0), (2, 6)),
                      mode='reflect',
                      reflect_type='odd')
    onp_res = onp.pad(x_np, ((10, 3), (5, 2), (3, 0), (2, 6)),
                      mode='reflect',
                      reflect_type='odd')
    match_all_arrays(mnp_res, onp_res, error=1e-5)

    # pad reflect even
    mnp_res = mnp.pad(x_ms, ((10, 13)), mode='reflect', reflect_type='even')
    onp_res = onp.pad(x_np, ((10, 13)), mode='reflect', reflect_type='even')
    match_all_arrays(mnp_res, onp_res, error=1e-5)

    # pad func
    x_np = onp.random.random([2, 4]).astype("float32")
    x_ms = mnp.asarray(x_np.tolist())
    mnp_res = mnp.pad(x_ms, ((5, 5)), mode=pad_with_msfunc, padder=99)
    onp_res = onp.pad(x_np, ((5, 5)), mode=pad_with_npfunc, padder=99)
    match_all_arrays(mnp_res, onp_res, error=1e-5)
Beispiel #20
0
def test_asarray():
    test_case = Cases()
    for array in test_case.array_sets:
        # Check for dtype matching
        actual = onp.asarray(array)
        expected = mnp.asarray(array).asnumpy()
        # Since we set float32/int32 as the default dtype in mindspore, we need
        # to make a conversion between numpy.asarray and mindspore.numpy.asarray
        if actual.dtype is onp.dtype('float64'):
            assert expected.dtype == onp.dtype('float32')
        elif actual.dtype is onp.dtype('int64'):
            assert expected.dtype == onp.dtype('int32')
        else:
            assert actual.dtype == expected.dtype
        match_array(actual, expected, error=7)

        for i in range(len(test_case.onp_dtypes)):
            actual = onp.asarray(array, test_case.onp_dtypes[i])
            expected = mnp.asarray(array, test_case.mnp_dtypes[i]).asnumpy()
            match_array(actual, expected, error=7)
def test_array():
    # array's function is very similar to asarray, so we mainly test the
    # `copy` argument.
    test_case = Cases()
    for array in test_case.array_sets:
        arr1 = mnp.asarray(array)
        arr2 = mnp.array(arr1, copy=False)
        arr3 = mnp.array(arr1)
        arr4 = mnp.asarray(array, dtype='int32')
        arr5 = mnp.asarray(arr4, dtype=mnp.int32)
        assert arr1 is arr2
        assert arr1 is not arr3
        assert arr4 is arr5

    # Additional tests for nested tensor/numpy_array mixture
    mnp_input = [(mnp.ones(3, ), mnp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
    onp_input = [(onp.ones(3, ), onp.ones(3)), [[1, 1, 1], (1, 1, 1)]]

    actual = onp.array(onp_input)
    expected = mnp.array(mnp_input).asnumpy()
    match_array(actual, expected, error=7)
Beispiel #22
0
def test_concatenate_type_promotion():
    onp_array = onp.random.random((5, 1)).astype('float32')
    mnp_array = mnp.asarray(onp_array)
    onp_array1 = onp_array.astype(onp.float16)
    onp_array2 = onp_array.astype(onp.bool_)
    onp_array3 = onp_array.astype(onp.float32)
    onp_array4 = onp_array.astype(onp.int32)

    mnp_array1 = mnp_array.astype(onp.float16)
    mnp_array2 = mnp_array.astype(onp.bool_)
    mnp_array3 = mnp_array.astype(onp.float32)
    mnp_array4 = mnp_array.astype(onp.int32)
    o_concatenate = onp_concatenate_type_promotion(
        onp_array1, onp_array2, onp_array3, onp_array4).astype('float32')
    m_concatenate = mnp_concatenate_type_promotion(
        mnp_array1, mnp_array2, mnp_array3, mnp_array4)
    check_all_results(o_concatenate, m_concatenate, error=1e-7)
def test_pad():
    x_np = onp.random.random([2, 3, 4]).astype("float32")
    x_ms = mnp.asarray(x_np.tolist())

    # pad constant
    mnp_res = mnp.pad(x_ms, ((1, 1), (2, 2), (3, 4)))
    onp_res = onp.pad(x_np, ((1, 1), (2, 2), (3, 4)))
    match_all_arrays(mnp_res, onp_res, error=1e-5)
    mnp_res = mnp.pad(x_ms, ((1, 1), (2, 3), (4, 5)),
                      constant_values=((3, 4), (5, 6), (7, 8)))
    onp_res = onp.pad(x_np, ((1, 1), (2, 3), (4, 5)),
                      constant_values=((3, 4), (5, 6), (7, 8)))
    match_all_arrays(mnp_res, onp_res, error=1e-5)

    # pad statistic
    mnp_res = mnp.pad(x_ms, ((1, 1), (2, 2), (3, 4)),
                      mode="mean",
                      stat_length=((1, 2), (2, 10), (3, 4)))
    onp_res = onp.pad(x_np, ((1, 1), (2, 2), (3, 4)),
                      mode="mean",
                      stat_length=((1, 2), (2, 10), (3, 4)))
    match_all_arrays(mnp_res, onp_res, error=1e-5)

    # pad edge
    mnp_res = mnp.pad(x_ms, ((1, 1), (2, 2), (3, 4)), mode="edge")
    onp_res = onp.pad(x_np, ((1, 1), (2, 2), (3, 4)), mode="edge")
    match_all_arrays(mnp_res, onp_res, error=1e-5)

    # pad wrap
    mnp_res = mnp.pad(x_ms, ((1, 1), (2, 2), (3, 4)), mode="wrap")
    onp_res = onp.pad(x_np, ((1, 1), (2, 2), (3, 4)), mode="wrap")
    match_all_arrays(mnp_res, onp_res, error=1e-5)

    # pad linear_ramp
    mnp_res = mnp.pad(x_ms, ((1, 3), (5, 2), (3, 0)),
                      mode="linear_ramp",
                      end_values=((0, 10), (9, 1), (-10, 99)))
    onp_res = onp.pad(x_np, ((1, 3), (5, 2), (3, 0)),
                      mode="linear_ramp",
                      end_values=((0, 10), (9, 1), (-10, 99)))
    match_all_arrays(mnp_res, onp_res, error=1e-5)
Beispiel #24
0
def test_linspace():
    actual = onp.linspace(2.0, 3.0, dtype=onp.float32)
    expected = mnp.linspace(2.0, 3.0).asnumpy()
    match_array(actual, expected, error=6)

    actual = onp.linspace(2.0, 3.0, num=5, dtype=onp.float32)
    expected = mnp.linspace(2.0, 3.0, num=5).asnumpy()
    match_array(actual, expected, error=6)

    actual = onp.linspace(
        2.0, 3.0, num=5, endpoint=False, dtype=onp.float32)
    expected = mnp.linspace(2.0, 3.0, num=5, endpoint=False).asnumpy()
    match_array(actual, expected, error=6)

    actual = onp.linspace(2.0, 3.0, num=5, retstep=True, dtype=onp.float32)
    expected = mnp.linspace(2.0, 3.0, num=5, retstep=True)
    match_array(actual[0], expected[0].asnumpy())
    assert actual[1] == expected[1].asnumpy()

    actual = onp.linspace(2.0, [3, 4, 5], num=5,
                          endpoint=False, dtype=onp.float32)
    expected = mnp.linspace(
        2.0, [3, 4, 5], num=5, endpoint=False).asnumpy()
    match_array(actual, expected, error=6)

    start = onp.random.random([2, 1, 4]).astype("float32")
    stop = onp.random.random([1, 5, 1]).astype("float32")
    actual = onp.linspace(start, stop, num=20, retstep=True,
                          endpoint=False, dtype=onp.float32)
    expected = mnp.linspace(mnp.asarray(start), mnp.asarray(stop), num=20,
                            retstep=True, endpoint=False)
    match_array(actual[0], expected[0].asnumpy(), error=6)
    match_array(actual[1], expected[1].asnumpy(), error=6)

    actual = onp.linspace(start, stop, num=20, retstep=True,
                          endpoint=False, dtype=onp.int16)
    expected = mnp.linspace(mnp.asarray(start), mnp.asarray(stop), num=20,
                            retstep=True, endpoint=False, dtype=mnp.int16)
    match_array(actual[0], expected[0].asnumpy(), error=6)
    match_array(actual[1], expected[1].asnumpy(), error=6)

    for axis in range(2):
        actual = onp.linspace(start, stop, num=20, retstep=False,
                              endpoint=False, dtype=onp.float32, axis=axis)
        expected = mnp.linspace(mnp.asarray(start), mnp.asarray(stop), num=20,
                                retstep=False, endpoint=False, dtype=mnp.float32, axis=axis)
        match_array(actual, expected.asnumpy(), error=6)
Beispiel #25
0
def test_absolute():
    arr = rand_int(2, 3)

    a = mnp.asarray(arr, dtype='float16')
    b = mnp.asarray(arr, dtype='float32')
    c = mnp.asarray(arr, dtype='uint8')
    d = mnp.asarray(arr, dtype='bool')

    match_array(mnp_absolute(a).asnumpy(), onp_absolute(a.asnumpy()))
    match_array(mnp_absolute(b).asnumpy(), onp_absolute(b.asnumpy()))
    match_array(mnp_absolute(c).asnumpy(), onp_absolute(c.asnumpy()))
    match_array(mnp_absolute(d).asnumpy(), onp_absolute(d.asnumpy()))

    where = rand_int(2, 3).astype('bool')
    out = rand_int(2, 3)
    match_array(mnp.absolute(a, out=mnp.asarray(out), where=mnp.asarray(where)).asnumpy(),
                onp.absolute(a.asnumpy(), out=out, where=where))
Beispiel #26
0
def test_ndarray_transpose():
    onp_array = onp.random.random((3, 4, 5)).astype('float32')
    mnp_array = mnp.asarray(onp_array)
    o_transposed = onp_ndarray_transpose(onp_array)
    m_transposed = mnp_ndarray_transpose(mnp_array)
    check_all_results(o_transposed, m_transposed)
Beispiel #27
0
def test_ndarray_flatten():
    onp_array = onp.random.random((3, 4, 5)).astype('float32')
    mnp_array = mnp.asarray(onp_array)
    o_flatten = onp_ndarray_flatten(onp_array)
    m_flatten = mnp_ndarray_flatten(mnp_array)
    check_all_results(o_flatten, m_flatten)
Beispiel #28
0
def test_concatenate():
    onp_array = onp.random.random((5, 4, 3, 2)).astype('float32')
    mnp_array = mnp.asarray(onp_array)
    o_concatenate = onp_concatenate(onp_array)
    m_concatenate = mnp_concatenate(mnp_array)
    check_all_results(o_concatenate, m_concatenate)
Beispiel #29
0
def test_ravel():
    onp_array = onp.random.random((2, 3, 4)).astype('float32')
    mnp_array = mnp.asarray(onp_array)
    o_ravel = onp_ravel(onp_array)
    m_ravel = mnp_ravel(mnp_array).asnumpy()
    match_array(o_ravel, m_ravel)
Beispiel #30
0
def test_reshape():
    onp_array = onp.random.random((2, 3, 4)).astype('float32')
    mnp_array = mnp.asarray(onp_array)
    o_reshaped = onp_reshape(onp_array)
    m_reshaped = mnp_reshape(mnp_array)
    check_all_results(o_reshaped, m_reshaped)