Refactor helper.py
@@ -226,8 +226,19 @@ def _make_decorator(check_func, name, type_check, contiguous_check,
assert len(cupy_result) == len(numpy_result)
- for cupy_r, numpy_r in zip(cupy_result, numpy_result):
+ # Check types
+ cupy_numpy_result_ndarrays = [
+ _convert_output_to_ndarray(cupy_r, numpy_r, sp_name)
+ for cupy_r, numpy_r in zip(cupy_result, numpy_result)]
+
+ # Check dtypes
+ if type_check:
+ for cupy_r, numpy_r in cupy_numpy_result_ndarrays:
+ assert cupy_r.dtype == numpy_r.dtype
+
+ # Check contiguities
if contiguous_check:
+ for cupy_r, numpy_r in zip(cupy_result, numpy_result):
if isinstance(numpy_r, numpy.ndarray):
if (numpy_r.flags.c_contiguous
and not cupy_r.flags.c_contiguous):
@@ -244,14 +255,12 @@ def _make_decorator(check_func, name, type_check, contiguous_check,
cupy_r.flags.f_contiguous,
numpy_r.flags.f_contiguous))
- cupy_r, numpy_r = _convert_output_to_ndarray(
- cupy_r, numpy_r, sp_name)
-
- if type_check:
- assert cupy_r.dtype == numpy_r.dtype
-
+ # Check shapes
+ for cupy_r, numpy_r in cupy_numpy_result_ndarrays:
assert cupy_r.shape == numpy_r.shape
+ # Check item values
+ for cupy_r, numpy_r in cupy_numpy_result_ndarrays:
# Behavior of assigning a negative value to an unsigned integer
# variable is undefined.
# nVidia GPUs and Intel CPUs behave differently.
@@ -276,6 +285,20 @@ def _make_decorator(check_func, name, type_check, contiguous_check,
def _convert_output_to_ndarray(c_out, n_out, sp_name):
+ """Checks type of cupy/numpy results and returns cupy/numpy ndarrays.
+
+ Args:
+ c_out (cupy.ndarray, cupyx.scipy.sparse matrix, cupy.poly1d or scalar):
+ cupy result
+ n_out (numpy.ndarray, scipy.sparse matrix, numpy.poly1d or scalar):
+ numpy result
+ sp_name(str or None): Argument name whose value is either
+ ``scipy.sparse`` or ``cupyx.scipy.sparse`` module. If ``None``, no
+ argument is given for the modules.
+
+ Returns:
+ The tuple of cupy.ndarray and numpy.ndarray.
+ """
if sp_name is not None:
import scipy.sparse
if cupyx.scipy.sparse.issparse(c_out):
