cupy
cupy copied to clipboard
cupy.scipy.sparse.csrmatrix gives unhelpful error message when np.ndarray is passed
Problem description
cupyx.scipy.sparse.csr_matrix((
np.ones([4], np.int32),
np.array([1, 2, 3, 4], np.int32),
np.array([0, 2, 3])
))
gives following error.
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-18-ab346d4e12f0> in <module>()
7 np.ones([4], np.int32),
8 np.array([1, 2, 3, 4], np.int32),
----> 9 np.array([0, 2, 3])
10 ))
/usr/local/lib/python3.6/dist-packages/cupyx/scipy/sparse/compressed.py in __init__(self, arg1, shape, dtype, copy)
81 base.isdense(indptr) and indptr.ndim == 1):
82 raise ValueError(
---> 83 'data, indices, and indptr should be 1-D')
84
85 if len(data) != len(indices):
Input tensors are indeed 1-D. The problem is that trigger for this ValueError is:
if not (base.isdense(data) and data.ndim == 1 and
base.isdense(indices) and indices.ndim == 1 and
base.isdense(indptr) and indptr.ndim == 1):
raise ValueError(
'data, indices, and indptr should be 1-D')
https://github.com/cupy/cupy/blob/v5/cupyx/scipy/sparse/compressed.py#L79-L83
where,
def isdense(x):
return isinstance(x, cupy.ndarray)
https://github.com/cupy/cupy/blob/v5/cupyx/scipy/sparse/sputils.py#L4-L5
Environment to reproduce
CuPy Version : 5.0.0
CUDA Root : /usr/local/cuda
CUDA Build Version : 9020
CUDA Driver Version : 9020
CUDA Runtime Version : 9020
cuDNN Build Version : 7201
cuDNN Version : 7201
NCCL Build Version : 2213
Suggestion
It should test input matrices first if they are cupy array or not, then check for sparse matrices or not. It should give separate error messages for different reasons of failures.
@kmaehashi
Suggestion: Add the following code to cupyx/scipy/sparse/_compressed.py into the _compressed_sparse_matrix.__init__() function, from line 247:
if not (isinstance(data,cupy.ndarray)):
raise ValueError(f'We are terribly sorry to inform you, that "data" is not a cupy.ndarray but has type {type(data)}.')
if not (isinstance(indices,cupy.ndarray)):
raise ValueError(f'We are terribly sorry to inform you, that "indices" is not a cupy.ndarray but has type {type(indices)}.')
if not (isinstance(indptr,cupy.ndarray)):
raise ValueError(f'We are terribly sorry to inform you, that "indptr" is not a cupy.ndarray but has type {type(indptr)}.')
if not (_base.isdense(data) and data.ndim == 1 and
_base.isdense(indices) and indices.ndim == 1 and
_base.isdense(indptr) and indptr.ndim == 1):
raise ValueError(
'data, indices, and indptr should be 1-D. Check that all arrays are cupy arrays and not numpy!')
if len(data) != len(indices):
raise ValueError(f'indices and data should have the same size but they have sizes {len(data)} and {len(indices)}. The order of the input arguments is : data, indices, indptr ')