Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【Hackathon 7th No.25】为 Paddle 新增 is_coalesced -part #68334

Merged
merged 18 commits into from
Oct 28, 2024
59 changes: 59 additions & 0 deletions paddle/fluid/pybind/eager_method.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2735,6 +2735,61 @@ static PyObject* tensor_method_to_sparse_csr(TensorObject* self,
EAGER_CATCH_AND_THROW_RETURN_NULL
}

PyDoc_STRVAR(tensor_is_coalesced__doc__, // NOLINT
R"DOC(is_coalesced($self, /)
--

Check whether the Tensor is a coalesced SparseCooTensor. If not it will return False.
Tensor types other than SparseCooTensor are not supported.

Notes:
It will return always False for a newly created SparseCooTensor.

Args:
x (Tensor): The input tensor. It can only be SparseCooTensor.

Returns:
bool: True if the Tensor is a coalesced SparseCooTensor, and False otherwise.

Examples:

.. code-block:: python

>>> import paddle

>>> indices = [[0, 0, 1], [1, 1, 2]]
>>> values = [1.0, 2.0, 3.0]
>>> x = paddle.sparse.sparse_coo_tensor(indices, values)

>>> x.is_coalesced()
False
>>> x = x.coalesce()
>>> x.is_coalesced()
True

>>> indices = [[0, 1, 1], [1, 0, 2]]
>>> values = [1.0, 2.0, 3.0]
>>> x = paddle.sparse.sparse_coo_tensor(indices, values)
>>> x.is_coalesced()
False

)DOC"); // NOLINT

static PyObject* tensor_method_is_coalesced(TensorObject* self,
PyObject* args,
PyObject* kwargs) {
EAGER_TRY
if (self->tensor.is_sparse_coo_tensor()) {
auto sparse_coo_tensor =
std::dynamic_pointer_cast<phi::SparseCooTensor>(self->tensor.impl());
return ToPyObject(sparse_coo_tensor->coalesced());
} else {
PADDLE_THROW(common::errors::InvalidArgument(
NKNaN marked this conversation as resolved.
Show resolved Hide resolved
"Method is_coalesced only support sparse coo tensor."));
}
EAGER_CATCH_AND_THROW_RETURN_NULL
}

PyDoc_STRVAR(tensor_is_same_shape__doc__, // NOLINT
R"DOC(is_same_shape($self, y, /)
--
Expand Down Expand Up @@ -3503,6 +3558,10 @@ PyMethodDef variable_methods[] = { // NOLINT
(PyCFunction)(void (*)())tensor_method_to_sparse_csr,
METH_VARARGS | METH_KEYWORDS,
tensor_to_sparse_csr__doc__},
{"is_coalesced",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

需要在 python/paddle/tensor/tensor.prototype.pyi stub 中补充新 Tensor API 类型

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的,已补充

(PyCFunction)(void (*)())tensor_method_is_coalesced,
METH_VARARGS | METH_KEYWORDS,
tensor_is_coalesced__doc__},
/***the method of sparse tensor****/
{"element_size",
(PyCFunction)(void (*)())tensor_method_element_size,
Expand Down
11 changes: 11 additions & 0 deletions paddle/fluid/pybind/pir.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1379,6 +1379,17 @@ void BindValue(py::module *m) {
[](Value &self, TensorDistAttribute dist_attr) {
self.set_type(dialect::CvtToPirDistType(self.type(), dist_attr));
})
.def("is_coalesced",
[](Value self) {
auto sparse_coo_tensor_type =
self.type().dyn_cast<SparseCooTensorType>();
if (sparse_coo_tensor_type) {
return sparse_coo_tensor_type.coalesced();
} else {
PADDLE_THROW(common::errors::InvalidArgument(
NKNaN marked this conversation as resolved.
Show resolved Hide resolved
"Method is_coalesced only support sparse coo tensor."));
}
})
.def_property_readonly("process_mesh", [](Value &self) -> py::object {
auto type = self.type();
if (auto dist_type = type.dyn_cast<DistTypeInterface>()) {
Expand Down
4 changes: 3 additions & 1 deletion paddle/phi/kernels/sparse/cpu/coalesce_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ PD_REGISTER_KERNEL(coalesce_coo,
uint8_t,
int16_t,
int,
int64_t) {
int64_t,
phi::dtype::complex<float>,
phi::dtype::complex<double>) {
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO);
}
4 changes: 3 additions & 1 deletion paddle/phi/kernels/sparse/gpu/coalesce_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ PD_REGISTER_KERNEL(coalesce_coo,
uint8_t,
int16_t,
int,
int64_t) {
int64_t,
phi::dtype::complex<float>,
phi::dtype::complex<double>) {
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO);
}
1 change: 1 addition & 0 deletions python/paddle/tensor/tensor.prototype.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ class AbstractTensor:
@property
def grad_fn(self) -> Any: ...
def is_contiguous(self) -> bool: ...
def is_coalesced(self) -> bool: ...
def is_dense(self) -> bool: ...
def is_dist(self) -> bool: ...
@property
Expand Down
Loading