Clip operation on csr returns row sparse

The Sparse NDArray documentation claims " clip(csr, a_min <= 0, a_max >= 0) = csr", but this returns a RowSparse NDArray.

Example:

a = nd.ones((2, 3)) * 2
a.asnumpy()
array([[ 2., 2., 2.],
[ 2., 2., 2.]], dtype=float32)
b = a.tostype(‘csr’)
b

<CSRNDArray 2x3 @cpu(0)>

b.asnumpy()
array([[ 2., 2., 2.],
[ 2., 2., 2.]], dtype=float32)
nd.sparse.clip(b, 0, 1)

<RowSparseNDArray 2x3 @cpu(0)>

Hi,

Do you have a pointer to the documentation you’re using? I’m looking at https://mxnet.incubator.apache.org/api/python/ndarray/sparse.html#mxnet.ndarray.sparse.clip which says that clip for CSRNDArray is a convenience method for Sparse.clip but doesn’t mention the return type.

You’re correct, unfortunately clip isn’t preserving the CSRNDArray. Feel free to create an issue https://github.com/apache/incubator-mxnet/issues if you’d like to see this functionality or if you’d like to contribute.

I’m looking at the same documentation at https://mxnet.incubator.apache.org/api/python/ndarray/sparse.html#mxnet.ndarray.sparse.clip
Just under the example, it says:

The storage type of clip output depends on storage types of inputs and the a_min, a_max parameter values:

  • clip(default) = default
  • clip(row_sparse, a_min <= 0, a_max >= 0) = row_sparse
  • clip(csr, a_min <= 0, a_max >= 0) = csr
  • clip(row_sparse, a_min < 0, a_max < 0) = default
  • clip(row_sparse, a_min > 0, a_max > 0) = default
  • clip(csr, a_min < 0, a_max < 0) = csr
  • clip(csr, a_min > 0, a_max > 0) = csr

I feel the documentation is generally more in line with the spirit of the data type (not sure about the row_sparse -> default case). I can’t think of a reason why clip should change the data type. In that sense it looks like a bug. I’ll create an issue for this.

Issue created here: https://github.com/apache/incubator-mxnet/issues/12787

1 Like

I agree. Thank you for creating the issue.