Пример #1
0
        self.check_warns((2, 3), ddof=3)
        self.check_warns((2, 3), ddof=4)

    def test_cov_raises(self):
        self.check_raises((2, 3), ddof=1.2)
        self.check_raises((3, 4, 2))
        self.check_raises((2, 3), (3, 4, 2))

    def test_cov_empty(self):
        self.check((0, 1))


@testing.gpu
@testing.parameterize(*testing.product({
    'mode': ['valid', 'same', 'full'],
    'shape1': [(5, ), (6, ), (20, ), (21, )],
    'shape2': [(5, ), (6, ), (20, ), (21, )],
}))
class TestCorrelateShapeCombination(unittest.TestCase):
    @testing.for_all_dtypes(no_float16=True)
    @testing.numpy_cupy_allclose(rtol=1e-4)
    def test_correlate(self, xp, dtype):
        a = testing.shaped_arange(self.shape1, xp, dtype)
        b = testing.shaped_arange(self.shape2, xp, dtype)
        return xp.correlate(a, b, mode=self.mode)


@testing.gpu
@testing.parameterize(*testing.product({'mode': ['valid', 'full', 'same']}))
class TestCorrelate(unittest.TestCase):
    @testing.for_all_dtypes()
Пример #2
0
    #         src = testing.shaped_arange((2, 3, 4), cupy, dtype)
    #         src = src.swapaxes(0, 1)
    #     with cuda.Device(1):
    #         dst = cupy.empty_like(src)
    #         cupy.copyto(dst, src)
    #
    #     expected = testing.shaped_arange((2, 3, 4), numpy, dtype)
    #     expected = expected.swapaxes(0, 1)
    #
    #     testing.assert_array_equal(expected, src.get())
    #     testing.assert_array_equal(expected, dst.get())


@testing.parameterize(*testing.product({
    'src': [float(3.2),
            int(0), int(4),
            int(-4), True, False, 1 + 1j],
    'dst_shape': [(), (0, ), (1, ), (1, 1), (2, 2)]
}))
@testing.gpu
class TestCopytoFromScalar(unittest.TestCase):
    @testing.for_all_dtypes()
    @testing.numpy_cupy_allclose(accept_error=TypeError)
    def test_copyto(self, xp, dtype):
        dst = xp.ones(self.dst_shape, dtype=dtype)
        xp.copyto(dst, self.src)
        return dst

    @testing.for_all_dtypes()
    @testing.numpy_cupy_allclose(accept_error=TypeError)
    def test_copyto_where(self, xp, dtype):
        dst = xp.ones(self.dst_shape, dtype=dtype)
Пример #3
0
@testing.parameterize(*testing.product({
    'shape_pair': [
        # dot test
        ((3, 2), (2, 4)),
        ((3, 0), (0, 4)),
        ((0, 2), (2, 4)),
        ((3, 2), (2, 0)),
        ((2, ), (2, 4)),
        ((0, ), (0, 4)),
        ((3, 2), (2, )),
        ((3, 0), (0, )),
        ((2, ), (2, )),
        ((0, ), (0, )),
        # matmul test
        ((5, 3, 2), (5, 2, 4)),
        # ((0, 3, 2), (0, 2, 4)),
        # ((5, 3, 2), (2, 4)),
        # ((0, 3, 2), (2, 4)),
        # ((3, 2), (5, 2, 4)),
        # ((3, 2), (0, 2, 4)),
        # ((5, 3, 2), (1, 2, 4)),
        # ((0, 3, 2), (1, 2, 4)),
        # ((1, 3, 2), (5, 2, 4)),
        # ((1, 3, 2), (0, 2, 4)),
        # ((5, 3, 2), (2,)),
        # ((5, 3, 0), (0,)),
        # ((2,), (5, 2, 4)),
        # ((0,), (5, 0, 4)),
        # ((2, 2, 3, 2), (2, 2, 2, 4)),
        # ((5, 0, 3, 2), (5, 0, 2, 4)),
        # ((6, 5, 3, 2), (2, 4)),
        # ((5, 0, 3, 2), (2, 4)),
        # ((3, 2), (6, 5, 2, 4)),
        # ((3, 2), (5, 0, 2, 4)),
        # ((1, 5, 3, 2), (6, 1, 2, 4)),
        # ((1, 0, 3, 2), (6, 1, 2, 4)),
        # ((6, 1, 3, 2), (1, 5, 2, 4)),
        # ((6, 1, 3, 2), (1, 0, 2, 4)),
        # ((6, 5, 3, 2), (2,)),
        # ((6, 5, 3, 0), (0,)),
        # ((2,), (6, 5, 2, 4)),
        # ((0,), (6, 5, 0, 4)),
        ((1, 3, 3), (10, 1, 3, 1)),
    ],
}))
Пример #4
0
# if self.include_strides is True:
# desc['strides'] = self.a.strides
# elif self.include_strides is None:
# desc['strides'] = None
# else:  # self.include_strides is False
# pass
# else:  # F contiguous or neither
# desc['strides'] = self.a.strides
# if self.mask is not None:
# desc['mask'] = self.mask
# return desc


@testing.parameterize(*testing.product({
    'ndmin': [0, 1, 2, 3],
    'copy': [True, False],
    'xp': [numpy, cupy]
}))
class TestArrayPreservationOfShape(unittest.TestCase):
    @testing.for_all_dtypes()
    def test_cupy_array(self, dtype):
        shape = 2, 3
        a = testing.shaped_arange(shape, self.xp, dtype)
        cupy.array(a, copy=self.copy, ndmin=self.ndmin)

        # Check if cupy.ndarray does not alter
        # the shape of the original array.
        assert a.shape == shape


@testing.parameterize(*testing.product({
Пример #5
0
class RandomDistributionsTestCase(unittest.TestCase):
    def check_distribution(self, dist_name, params, dtype):
        cp_params = {k: cupy.asarray(params[k]) for k in params}
        np_out = numpy.asarray(
            getattr(numpy.random, dist_name)(size=self.shape, **params),
            dtype)
        cp_out = getattr(_distributions, dist_name)(
            size=self.shape, dtype=dtype, **cp_params)
        self.assertEqual(cp_out.shape, np_out.shape)
        self.assertEqual(cp_out.dtype, np_out.dtype)


@testing.parameterize(*testing.product({
    'shape': [(4, 3, 2), (3, 2)],
    'a_shape': [(), (3, 2)],
    'b_shape': [(), (3, 2)],
    'dtype': _float_dtypes,  # to escape timeout
})
)
@testing.gpu
class TestDistributionsBeta(RandomDistributionsTestCase):

    @helper.for_dtypes_combination(
        _float_dtypes, names=['a_dtype', 'b_dtype'])
    def test_beta(self, a_dtype, b_dtype):
        a = numpy.full(self.a_shape, 3, dtype=a_dtype)
        b = numpy.full(self.b_shape, 3, dtype=b_dtype)
        self.check_distribution('beta',
                                {'a': a, 'b': b}, self.dtype)

Пример #6
0
import unittest

import numpy
import pytest

import dpnp as cupy
from tests.third_party.cupy import testing


@testing.parameterize(*testing.product({
    'shape': [(7, ), (2, 3), (4, 3, 2)],
    'n_vals': [0, 1, 3, 15],
}))
@testing.gpu
class TestPlace(unittest.TestCase):

    # NumPy 1.9 don't wraps values.
    # https://github.com/numpy/numpy/pull/5821
    @testing.for_all_dtypes()
    @testing.numpy_cupy_array_equal()
    def test_place(self, xp, dtype):
        a = testing.shaped_arange(self.shape, xp, dtype)
        if self.n_vals == 0:
            mask = xp.zeros(self.shape, dtype=numpy.bool_)
        else:
            mask = testing.shaped_random(self.shape, xp, numpy.bool_)
        vals = testing.shaped_random((self.n_vals, ), xp, dtype)
        xp.place(a, mask, vals)
        return a

Пример #7
0
    # memory constraint
    {
        'subscript': 'a,b,c->abc',
        'opt': ('greedy', 0)
    },
    {
        'subscript': 'acdf,jbje,gihb,hfac',
        'opt': ('greedy', 0)
    },
] + testing.product({
    'subscript': [
        # long paths
        'acdf,jbje,gihb,hfac,gfac,gifabc,hfac',
        'chd,bde,agbc,hiad,bdi,cgh,agdb',
        # edge cases
        'eb,cb,fb->cef',
        'dd,fb,be,cdb->cef',
        'bca,cdb,dbf,afc->',
        'dcc,fce,ea,dbf->ab',
        'a,ac,ab,ad,cd,bd,bc->',
    ],
    'opt': ['greedy', 'optimal'],
})))
class TestEinSumLarge(unittest.TestCase):
    def setUp(self):
        chars = 'abcdefghij'
        sizes = numpy.array([2, 3, 4, 5, 4, 3, 2, 6, 5, 4, 3])
        size_dict = {}
        for size, char in zip(sizes, chars):
            size_dict[char] = size

        # Builds views based off initial operands
Пример #8
0
# @testing.numpy_cupy_array_equal()
# def test_histogram_range_float(self, xp, dtype):
# a = testing.shaped_arange((10,), xp, dtype)
# h, b = xp.histogram(a, testing.shaped_arange((10,), xp, numpy.float64))
# assert int(h.sum()) == 10
# return h, b


@testing.gpu
@testing.parameterize(*testing.product({
    'bins': [
        # Test monotonically increasing with in-bounds values
        [1.5, 2.5, 4.0, 6.0],
        # Explicit out-of-bounds for x values
        [-1.0, 1.0, 2.5, 4.0, 20.0],
        # Repeated values should yield right-most or left-most indexes
        [0.0, 1.0, 1.0, 4.0, 4.0, 10.0],
    ],
    'increasing': [True, False],
    'right': [True, False],
    'shape': [(), (10, ), (6, 3, 3)]
}))
class TestDigitize(unittest.TestCase):
    @testing.for_all_dtypes(no_bool=True, no_complex=True)
    @testing.numpy_cupy_array_equal()
    def test_digitize(self, xp, dtype):
        x = testing.shaped_arange(self.shape, xp, dtype)
        bins = self.bins
        if not self.increasing:
            bins = bins[::-1]
        bins = xp.array(bins)
Пример #9
0
                return xp.median(a, -a.ndim - 1, keepdims=False)

            with pytest.raises(numpy.AxisError):
                return xp.median(a, a.ndim, keepdims=False)

            with pytest.raises(numpy.AxisError):
                return xp.median(a, (-a.ndim - 1, 1), keepdims=False)

            with pytest.raises(numpy.AxisError):
                return xp.median(a, (0, a.ndim,), keepdims=False)


@testing.parameterize(
    *testing.product({
        'shape': [(3, 4, 5)],
        'axis': [(0, 1), (0, -1), (1, 2), (1,)],
        'keepdims': [True, False]
    })
)
@testing.gpu
class TestMedianAxis(unittest.TestCase):

    @testing.for_all_dtypes()
    @testing.numpy_cupy_allclose()
    def test_median_axis_sequence(self, xp, dtype):
        a = testing.shaped_random(self.shape, xp, dtype)
        return xp.median(a, self.axis, keepdims=self.keepdims)


@testing.gpu
class TestAverage(unittest.TestCase):
Пример #10
0
        a = testing.shaped_arange((2, 3, 4), xp, dtype)
        return xp.prod(a, axis=1)

    @testing.for_all_dtypes_combination(names=['src_dtype', 'dst_dtype'])
    @testing.numpy_cupy_allclose()
    def test_prod_dtype(self, xp, src_dtype, dst_dtype):
        if not xp.can_cast(src_dtype, dst_dtype):
            return xp.array([])  # skip
        a = testing.shaped_arange((2, 3), xp, src_dtype)
        return a.prod(dtype=dst_dtype)


@testing.parameterize(*testing.product({
    'shape': [(2, 3, 4), (20, 30, 40)],
    'axis': [0, 1],
    'transpose_axes': [True, False],
    'keepdims': [True, False],
    'func': ['nansum', 'nanprod']
}))
@testing.gpu
class TestNansumNanprodLong(unittest.TestCase):
    def _do_transposed_axis_test(self):
        return not self.transpose_axes and self.axis != 1

    def _numpy_nanprod_implemented(self):
        return (self.func == 'nanprod'
                and numpy.__version__ >= numpy.lib.NumpyVersion('1.10.0'))

    def _test(self, xp, dtype):
        a = testing.shaped_arange(self.shape, xp, dtype)
        if self.transpose_axes:
Пример #11
0
    def test_fix(self):
        self.check_unary('fix')
        self.check_unary_complex_unsupported('fix')

    def test_around(self):
        self.check_unary('around')
        self.check_unary_complex('around')

    def test_round_(self):
        self.check_unary('round_')
        self.check_unary_complex('around')


@testing.parameterize(*testing.product({
    'decimals': [-2, -1, 0, 1, 2],
}))
class TestRound(unittest.TestCase):

    shape = (20, )

    @testing.for_all_dtypes()
    @testing.numpy_cupy_allclose(atol=1e-5)
    def test_round(self, xp, dtype):
        if dtype == numpy.bool_:
            # avoid cast problem
            a = testing.shaped_random(self.shape, xp, scale=10, dtype=dtype)
            return xp.around(a, 0)
        if dtype == numpy.float16:
            # avoid accuracy problem
            a = testing.shaped_random(self.shape, xp, scale=10, dtype=dtype)
Пример #12
0
# if xp is numpy:
# return a.argmax()

# # xp is cupy, first ensure we really use CUB
# ret = cupy.empty(())  # Cython checks return type, need to fool it
# func = 'cupy.core._routines_statistics.cub.device_reduce'
# with testing.AssertFunctionIsCalled(func, return_value=ret):
# a.argmax()
# # ...then perform the actual computation
# return a.argmax()


@testing.gpu
@testing.parameterize(*testing.product({
    'func': ['argmin', 'argmax'],
    'is_module': [True, False],
    'shape': [(3, 4), ()],
}))
class TestArgMinMaxDtype(unittest.TestCase):
    @testing.for_dtypes(
        dtypes=[numpy.int8, numpy.int16, numpy.int32, numpy.int64],
        name='result_dtype')
    @testing.for_all_dtypes(name='in_dtype')
    def test_argminmax_dtype(self, in_dtype, result_dtype):
        a = testing.shaped_random(self.shape, cupy, in_dtype)
        if self.is_module:
            func = getattr(cupy, self.func)
            y = func(a, dtype=result_dtype)
        else:
            func = getattr(a, self.func)
            y = func(dtype=result_dtype)
Пример #13
0
from tests.third_party.cupy import testing

float_types = [numpy.float32, numpy.float64]
complex_types = []
signed_int_types = [numpy.int32, numpy.int64]
unsigned_int_types = []
int_types = signed_int_types + unsigned_int_types
all_types = float_types + int_types + complex_types
negative_types = (float_types + signed_int_types + complex_types)
negative_no_complex_types = float_types + signed_int_types
no_complex_types = float_types + int_types


@testing.gpu
@testing.parameterize(*(testing.product({
    'nargs': [1],
    'name': ['reciprocal', 'angle'],
}) + testing.product({
    'nargs': [2],
    'name': [
        'add', 'multiply', 'divide', 'power', 'subtract', 'true_divide',
        'floor_divide', 'fmod', 'remainder', 'mod'
    ],
})))
class TestArithmeticRaisesWithNumpyInput(unittest.TestCase):
    def test_raises_with_numpy_input(self):
        nargs = self.nargs
        name = self.name

        # Check TypeError is raised if numpy.ndarray is given as input
        func = getattr(cupy, name)
        for input_xp_list in itertools.product(*[[numpy, cupy]] * nargs):
Пример #14
0
import unittest

import numpy
import pytest

import dpnp as cupy
from tests.third_party.cupy import testing


@testing.parameterize(*testing.product({
    'shape': [(2, 3), (), (4, )],
}))
@testing.gpu
class TestShape(unittest.TestCase):
    def test_shape(self):
        shape = self.shape
        for xp in (numpy, cupy):
            a = testing.shaped_arange(shape, xp)
            assert cupy.shape(a) == shape

    def test_shape_list(self):
        shape = self.shape
        a = testing.shaped_arange(shape, numpy)
        a = a.tolist()
        assert cupy.shape(a) == shape


@testing.gpu
class TestReshape(unittest.TestCase):
    def test_reshape_strides(self):
        def func(xp):
Пример #15
0
        return xp.logspace(0.1, 2.1, 11, dtype=int)

    def test_logspace_neg_num(self):
        for xp in (numpy, cupy):
            with pytest.raises(ValueError):
                xp.logspace(0, 10, -1)

    @testing.for_all_dtypes(no_bool=True)
    @testing.numpy_cupy_allclose()
    def test_logspace_base(self, xp, dtype):
        return xp.logspace(0, 2, 5, base=2.0, dtype=dtype)


@testing.parameterize(*testing.product({
    'indexing': ['xy', 'ij'],
    'sparse': [False, True],
    'copy': [False, True],
}))
@testing.gpu
class TestMeshgrid(unittest.TestCase):
    @testing.for_all_dtypes()
    def test_meshgrid0(self, dtype):
        out = cupy.meshgrid(indexing=self.indexing,
                            sparse=self.sparse,
                            copy=self.copy)
        assert (out == [])

    @testing.for_all_dtypes()
    @testing.numpy_cupy_array_equal()
    def test_meshgrid1(self, xp, dtype):
        x = xp.arange(2).astype(dtype)
Пример #16
0
import unittest

import numpy

import dpnp as cupy
from tests.third_party.cupy import testing


@testing.parameterize(*testing.product({
    'decimals': [-2, -1, 0, 1, 2],
}))
class TestRound(unittest.TestCase):

    shape = (20, )

    @testing.for_all_dtypes()
    @testing.numpy_cupy_allclose(atol=1e-5)
    def test_round(self, xp, dtype):
        if dtype == numpy.bool_:
            # avoid cast problem
            a = testing.shaped_random(self.shape, xp, scale=10, dtype=dtype)
            return a.round(0)
        if dtype == numpy.float16:
            # avoid accuracy problem
            a = testing.shaped_random(self.shape, xp, scale=10, dtype=dtype)
            return a.round(0)
        a = testing.shaped_random(self.shape, xp, scale=100, dtype=dtype)
        return a.round(self.decimals)

    @testing.numpy_cupy_array_equal()
    def test_round_out(self, xp):
Пример #17
0
    @testing.for_complex_dtypes()
    @testing.numpy_cupy_allclose()
    def test_ptp_nan_real(self, xp, dtype):
        a = xp.array([float('nan'), 1, -1], dtype)
        return a.ptp()

    @testing.for_complex_dtypes()
    @testing.numpy_cupy_allclose()
    def test_ptp_nan_imag(self, xp, dtype):
        a = xp.array([float('nan') * 1.j, 1.j, -1.j], dtype)
        return a.ptp()


# This class compares CUB results against NumPy's
@testing.parameterize(*testing.product({
    'shape': [(10,), (10, 20), (10, 20, 30), (10, 20, 30, 40)],
    'order': ('C', 'F'),
}))
@testing.gpu
# @unittest.skipUnless(cupy.cuda.cub.available, 'The CUB routine is not enabled')
class TestCubReduction(unittest.TestCase):

    # def setUp(self):
    #     self.old_accelerators = _accelerator.get_routine_accelerators()
    #     _accelerator.set_routine_accelerators(['cub'])
    #
    # def tearDown(self):
    #     _accelerator.set_routine_accelerators(self.old_accelerators)

    # @testing.for_contiguous_axes()
    @testing.for_all_dtypes(no_bool=True, no_float16=True)
    @testing.numpy_cupy_allclose(rtol=1E-5)
Пример #18
0
import functools
import pytest
import string
import unittest

import numpy as np

import dpnp as cupy
from tests.third_party.cupy import testing


@testing.parameterize(*testing.product({
    'n': [None, 0, 5, 10, 15],
    'shape': [(0, ), (10, 0), (10, ), (10, 10)],
    'norm': [None, 'ortho', ''],
}))
@testing.gpu
class TestFft(unittest.TestCase):
    @testing.for_all_dtypes()
    @testing.numpy_cupy_allclose(rtol=1e-4,
                                 atol=1e-7,
                                 accept_error=ValueError,
                                 contiguous_check=False)
    def test_fft(self, xp, dtype):
        a = testing.shaped_random(self.shape, xp, dtype)
        out = xp.fft.fft(a, n=self.n, norm=self.norm)

        return out

    @testing.for_all_dtypes()
    @testing.numpy_cupy_allclose(rtol=1e-4,
Пример #19
0
import dpnp as cupy
from tests.third_party.cupy import testing


@testing.parameterize(*testing.product({
    'shape': [
        ((2, 3, 4), (3, 4, 2)),
        ((1, 1), (1, 1)),
        ((1, 1), (1, 2)),
        ((1, 2), (2, 1)),
        ((2, 1), (1, 1)),
        ((1, 2), (2, 3)),
        ((2, 1), (1, 3)),
        ((2, 3), (3, 1)),
        ((2, 3), (3, 4)),
        ((0, 3), (3, 4)),
        ((2, 3), (3, 0)),
        ((0, 3), (3, 0)),
        ((3, 0), (0, 4)),
        ((2, 3, 0), (3, 0, 2)),
        ((0, 0), (0, 0)),
        ((3, ), (3, )),
        ((2, ), (2, 4)),
        ((4, 2), (2, )),
    ],
    'trans_a': [True, False],
    'trans_b': [True, False],
}))
@testing.gpu
class TestDot(unittest.TestCase):
    @testing.for_all_dtypes_combination(['dtype_a', 'dtype_b'])
    @testing.numpy_cupy_allclose()
Пример #20
0
    @testing.for_orders('C')
    @testing.for_all_dtypes()
    @testing.numpy_cupy_array_equal()
    def test_full_like(self, xp, dtype, order):
        a = xp.ndarray((2, 3, 4), dtype=dtype)
        return xp.full_like(a, 1, order=order)

    def test_full_like_subok(self):
        a = cupy.ndarray((2, 3, 4))
        with pytest.raises(TypeError):
            cupy.full_like(a, 1, subok=True)


@testing.parameterize(*testing.product({
    'shape': [4, (4, ), (4, 2), (4, 2, 3), (5, 4, 2, 3)],
}))
@testing.gpu
class TestBasicReshape(unittest.TestCase):
    @testing.with_requires('numpy>=1.17.0')
    @testing.for_orders('C')
    @testing.for_all_dtypes()
    @testing.numpy_cupy_array_equal()
    def test_empty_like_reshape(self, xp, dtype, order):
        a = testing.shaped_arange((2, 3, 4), xp, dtype)
        b = xp.empty_like(a, order=order, shape=self.shape)
        b.fill(0)
        return b

    @testing.for_CF_orders()
    @testing.for_all_dtypes()