cupy.testing.for_all_dtypes#

cupy.testing.for_all_dtypes(name='dtype', no_float16=False, no_bool=False, no_complex=False)[源代码][源代码]#

检查所有数据类型的装置的装饰器。

参数:
  • name (str) – 传递指定数据类型的参数名称。

  • no_float16 (bool) – 如果 True,候选数据类型中将省略 numpy.float16

  • no_bool (bool) – 如果 Truenumpy.bool_ 将从候选数据类型中省略。

  • no_complex (bool) – 如果 True,则 numpy.complex64numpy.complex128 将从候选数据类型中省略。

要测试的数据类型:numpy.complex64``(可选),``numpy.complex128``(可选),``numpy.float16``(可选),``numpy.float32numpy.float64numpy.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_``(可选)。

用法如下。此测试夹具检查 cPickle 是否成功为各种 dtypes 重建 cupy.ndarraydtype 是由装饰器插入的参数。

>>> 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()