Skip to content

Commit 6ed9282

Browse files
fix to_sparse_coo, to_sparse_csr (#76076)
* fix to_sparse_coo, to_sparse_csr * fix * fix * fix
1 parent fecfacd commit 6ed9282

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

paddle/fluid/pybind/eager_method.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2883,6 +2883,7 @@ PyDoc_STRVAR(tensor_to_sparse_csr__doc__, // NOLINT
28832883
Convert input Tensor to SparseCsrTensor.
28842884
28852885
When input is SparseCooTensor, will convert `COO` to `CSR` . When input is DenseTensor, will convert `Dense` to `CSR` .
2886+
When input is SparseCsrTensor, the function will directly return the input itself without performing any conversion.
28862887
28872888
Returns:
28882889
SparseCsrTensor
@@ -2909,6 +2910,10 @@ static PyObject* tensor_method_to_sparse_csr(TensorObject* self,
29092910
PyObject* args,
29102911
PyObject* kwargs) {
29112912
EAGER_TRY
2913+
if (self->tensor.is_sparse_csr_tensor()) {
2914+
Py_INCREF(self);
2915+
return reinterpret_cast<PyObject*>(self);
2916+
}
29122917
auto csr_tensor = self->tensor.to_sparse_csr();
29132918
egr::EagerUtils::autograd_meta(&csr_tensor)
29142919
->SetStopGradient(

python/paddle/base/dygraph/tensor_patch_methods.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1256,7 +1256,9 @@ def to_sparse_coo(self: Tensor, sparse_dim: int) -> Tensor:
12561256
**Notes**:
12571257
**This API is ONLY available in Dygraph mode**
12581258
1259-
Convert the current DenseTensor to SparseTensor in COO format.
1259+
Convert the current DenseTensor to SparseTensor in COO format. When the input is already a SparseCooTensor, this function will directly return
1260+
the input itself without performing any conversion.
1261+
12601262
12611263
Returns:
12621264
Tensor: A SparseCooTensor
@@ -1274,6 +1276,8 @@ def to_sparse_coo(self: Tensor, sparse_dim: int) -> Tensor:
12741276
[1, 3, 2, 3]],
12751277
values=[1., 2., 3., 4.])
12761278
"""
1279+
if self.is_sparse_coo():
1280+
return self
12771281

12781282
return _C_ops.sparse_to_sparse_coo(self, sparse_dim)
12791283

test/legacy_test/test_sparse_utils_op.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,29 @@ def test_to_sparse_coo(self):
180180
dense_x.grad.numpy(), out_grad.to_dense().numpy()
181181
)
182182

183+
def test_coo_to_coo(self):
184+
indices = [[0, 0, 1, 2, 2], [1, 3, 2, 0, 1]]
185+
values = [1.0, 2.0, 3.0, 4.0, 5.0]
186+
sparse_x = paddle.sparse.sparse_coo_tensor(
187+
paddle.to_tensor(indices),
188+
paddle.to_tensor(values),
189+
shape=[3, 4],
190+
stop_gradient=False,
191+
)
192+
sparse_x_ = sparse_x.to_sparse_coo(2)
193+
assert sparse_x is sparse_x_
194+
195+
def test_csr_to_csr(self):
196+
crows = [0, 2, 3, 5]
197+
cols = [1, 3, 2, 0, 1]
198+
values = [1.0, 2.0, 3.0, 4.0, 5.0]
199+
crows = paddle.to_tensor(crows, dtype='int32')
200+
cols = paddle.to_tensor(cols, dtype='int32')
201+
values = paddle.to_tensor(values, dtype='float32')
202+
sparse_x = paddle.sparse.sparse_csr_tensor(crows, cols, values)
203+
sparse_x_ = sparse_x.to_sparse_csr()
204+
assert sparse_x is sparse_x_
205+
183206
def test_coo_to_dense(self):
184207
indices = [[0, 0, 1, 2, 2], [1, 3, 2, 0, 1]]
185208
values = [1.0, 2.0, 3.0, 4.0, 5.0]

0 commit comments

Comments
 (0)