cupy.testing.for_all_dtypes#
- cupy.testing.for_all_dtypes(name='dtype', no_float16=False, no_bool=False, no_complex=False)[source]#
一个装饰器,用所有 dtype 检查 fixture。
- 参数:
要测试的 dtypes:
numpy.complex64
(可选),numpy.complex128
(可选),numpy.float16
(可选),numpy.float32
,numpy.float64
,numpy.dtype('b')
,numpy.dtype('h')
,numpy.dtype('i')
,numpy.dtype('l')
,numpy.dtype('q')
,numpy.dtype('B')
,numpy.dtype('H')
,numpy.dtype('I')
,numpy.dtype('L')
,numpy.dtype('Q')
,以及numpy.bool_
(可选)。用法如下所示。此测试 fixture 检查
cPickle
是否能成功重构各种 dtype 的cupy.ndarray
。dtype
是由装饰器插入的参数。>>> import unittest >>> from cupy import testing >>> class TestNpz(unittest.TestCase): ... ... @testing.for_all_dtypes() ... def test_pickle(self, dtype): ... a = testing.shaped_arange((2, 3, 4), dtype=dtype) ... s = pickle.dumps(a) ... b = pickle.loads(s) ... testing.assert_array_equal(a, b)
通常,我们将此装饰器与检查 NumPy 和 CuPy 之间一致性的装饰器(例如
cupy.testing.numpy_cupy_allclose()
)结合使用。下面就是一个这样的例子。>>> import unittest >>> from cupy import testing >>> class TestMean(unittest.TestCase): ... ... @testing.for_all_dtypes() ... @testing.numpy_cupy_allclose() ... def test_mean_all(self, xp, dtype): ... a = testing.shaped_arange((2, 3), xp, dtype) ... return a.mean()