fix sparse segfault in pynative bprop

This commit is contained in:
yanglf1121 2022-03-10 15:36:11 +08:00
parent 3037c08862
commit 108fcd750e
4 changed files with 52 additions and 11 deletions

View File

@ -298,7 +298,11 @@ py::object BaseRefToPyData(const BaseRef &value, const AbstractBasePtr &output)
if (utils::isa<VectorRef>(value)) {
MS_LOG(DEBUG) << "BaseRefToPyData, value is tuple: " << value.ToString();
auto vec_ref = utils::cast<VectorRef>(value);
ret = VectorRefToPyData(vec_ref, output);
if (output != nullptr) {
ret = VectorRefToPyData(vec_ref, output);
} else {
ret = VectorRefToPyData(vec_ref);
}
} else {
ret = BaseRefToPyData(value);
}

View File

@ -2532,7 +2532,7 @@ class COOTensor(COOTensor_):
Tensor.
Supported Platforms:
``GPU`` ``CPU``
``GPU``
"""
zeros_tensor = tensor_operator_registry.get("zeros")(self.shape, self.values.dtype)
return tensor_operator_registry.get("tensor_scatter_add")(
@ -2574,7 +2574,7 @@ class COOTensor(COOTensor_):
Examples:
>>> import mindspore as ms
>>> from mindspore import Tensor, COOTensor
>>> indices = Tensor([[0, 1], [1, 2]])
>>> indices = Tensor([[0, 1], [1, 2]], dtype=ms.int32)
>>> values = Tensor([1, 2], dtype=ms.float32)
>>> shape = (3, 4)
>>> coo_tensor = COOTensor(indices, values, shape)
@ -2713,7 +2713,15 @@ class CSRTensor(CSRTensor_):
return self.indptr, self.indices, self.values, self.shape
def to_coo(self):
"""Return a COOTensor."""
"""
Converts CSRTensor to COOTensor.
Returns:
COOTensor.
Supported Platforms:
``GPU`` ``CPU``
"""
row_indices = tensor_operator_registry.get("csr2coo")(self.indptr, self.values.shape[0])
coo_indices = tensor_operator_registry.get("stack")(1)((row_indices, self.indices))
return COOTensor(coo_indices, self.values, self.shape)
@ -2726,7 +2734,7 @@ class CSRTensor(CSRTensor_):
Tensor.
Supported Platforms:
``GPU`` ``CPU``
``GPU``
"""
coo_tensor = self.to_coo()
return coo_tensor.to_dense()
@ -2747,8 +2755,8 @@ class CSRTensor(CSRTensor_):
Examples:
>>> import mindspore as ms
>>> from mindspore import Tensor, CSRTensor
>>> indptr = Tensor([0, 1, 2])
>>> indices = Tensor([0, 1])
>>> indptr = Tensor([0, 1, 2], dtype=ms.int32)
>>> indices = Tensor([0, 1], dtype=ms.int32)
>>> values = Tensor([1, 2], dtype=ms.float32)
>>> shape = (2, 4)
>>> csr_tensor = CSRTensor(indptr, indices, values, shape)
@ -2774,8 +2782,8 @@ class CSRTensor(CSRTensor_):
Examples:
>>> from mindspore import Tensor, CSRTensor
>>> from mindspore import dtype as mstype
>>> indptr = Tensor([0, 1, 2])
>>> indices = Tensor([0, 1])
>>> indptr = Tensor([0, 1, 2], dtype=ms.int32)
>>> indices = Tensor([0, 1], dtype=ms.int32)
>>> values = Tensor([2, 1], dtype=mstype.float32)
>>> dense_shape = (2, 4)
>>> csr_tensor = CSRTensor(indptr, indices, values, dense_shape)
@ -2802,8 +2810,8 @@ class CSRTensor(CSRTensor_):
Examples:
>>> from mindspore import Tensor, CSRTensor
>>> from mindspore import dtype as mstype
>>> indptr = Tensor([0, 1, 2])
>>> indices = Tensor([0, 1])
>>> indptr = Tensor([0, 1, 2], dtype=ms.int32)
>>> indices = Tensor([0, 1], dtype=ms.int32)
>>> values = Tensor([2, 1], dtype=mstype.float32)
>>> dense_shape = (2, 4)
>>> csr_tensor = CSRTensor(indptr, indices, values, dense_shape)

View File

@ -14,6 +14,7 @@
# ============================================================================
"""smoke tests for COO operations"""
import platform
import pytest
import numpy as np
@ -24,6 +25,8 @@ from mindspore.ops import functional as F
context.set_context(mode=context.GRAPH_MODE)
def get_platform():
return platform.system().lower()
def compare_coo(coo1, coo2):
assert isinstance(coo1, COOTensor)
@ -105,6 +108,8 @@ def test_coo_method():
Description: Test coo_tensor.to_csr(), coo_tensor.to_dense().
Expectation: Success.
"""
if get_platform() != "linux":
return
class COOToCSRNet(nn.Cell):
def construct(self, coo_tensor):
return coo_tensor.to_csr()
@ -134,6 +139,7 @@ def test_coo_method():
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_x86_cpu
@pytest.mark.env_onecard
def test_dtype_coo_tensor():
"""
@ -141,6 +147,8 @@ def test_dtype_coo_tensor():
Description: Test: F.dtype(x), x.dtype.
Expectation: Success.
"""
if get_platform() != "linux":
return
indices = Tensor([[0, 1], [1, 2]])
values = Tensor([1, 2], dtype=mstype.float32)
shape = (3, 4)
@ -170,6 +178,8 @@ def test_coo_attr():
Description: Test COOTensor.indices, COOTensor.values, COOTensor.shape.
Expectation: Success.
"""
if get_platform() != "linux":
return
indices = Tensor([[0, 1], [1, 2]])
values = Tensor([1, 2], dtype=mstype.float32)
shape = (3, 4)

View File

@ -15,6 +15,7 @@
"""smoke tests for CSR operations"""
import os
import platform
import pytest
import numpy as np
@ -35,6 +36,8 @@ def compare_csr(csr1, csr2):
assert (csr1.values.asnumpy() == csr2.values.asnumpy()).all()
assert csr1.shape == csr2.shape
def get_platform():
return platform.system().lower()
@pytest.mark.level0
@pytest.mark.platform_arm_ascend_training
@ -48,6 +51,8 @@ def test_make_csr():
Description: Test CSRTensor(indptr, indices, values, shape) and CSRTensor(CSRTensor)
Expectation: Success.
"""
if get_platform() != "linux":
return
indptr = Tensor([0, 1, 2])
indices = Tensor([0, 1])
values = Tensor([1, 2], dtype=mstype.float32)
@ -75,6 +80,8 @@ def test_csr_attr():
Description: Test CSRTensor.indptr, CSRTensor.indices, CSRTensor.values, CSRTensor.shape.
Expectation: Success.
"""
if get_platform() != "linux":
return
indptr = Tensor([0, 1, 2])
indices = Tensor([0, 1])
values = Tensor([1, 2], dtype=mstype.float32)
@ -261,6 +268,8 @@ def test_csr_ops():
Description: Test CSRReduceSum, CSRMul, CSRMV.
Expectation: Success.
"""
if get_platform() != "linux":
return
csr_reducesum = _csr_ops.CSRReduceSum()
csrmv = _csr_ops.CSRMV()
@ -319,6 +328,8 @@ def test_csrtensor_export_and_import_mindir():
Description: Test export and load.
Expectation: Success.
"""
if get_platform() != "linux":
return
class TestCSRTensor(nn.Cell):
def __init__(self, shape):
super(TestCSRTensor, self).__init__()
@ -421,6 +432,8 @@ def test_isinstance_csr_tensor():
Description: Test: isinstance(x, CSRTensor).
Expectation: Success.
"""
if get_platform() != "linux":
return
indptr = Tensor([0, 1, 2])
indices = Tensor([0, 1])
values = Tensor([2, 1], dtype=mstype.float32)
@ -457,6 +470,8 @@ def test_dtype_csr_tensor():
Description: Test: F.dtype(x).
Expectation: Success.
"""
if get_platform() != "linux":
return
indptr = Tensor([0, 1, 2])
indices = Tensor([0, 1])
values = Tensor([2, 1], dtype=mstype.float32)
@ -485,6 +500,8 @@ def test_csr_bprop():
Description: Test CSRReduceSum, CSRMul, CSRMV, CSRTensor.to_coo(), CSRTensor.to_dense().
Expectation: Success.
"""
if get_platform() != "linux":
return
csr_reduce_sum = _csr_ops.CSRReduceSum()
csrmv = _csr_ops.CSRMV()
grad_op = ops.GradOperation(get_all=True)
@ -558,6 +575,8 @@ def test_csr_method():
Description: Test csr_tensor.to_coo(), csr_tensor.to_dense().
Expectation: Success.
"""
if get_platform() != "linux":
return
class CSRToCOONet(nn.Cell):
def construct(self, csr_tensor):
return csr_tensor.to_coo()