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 4th No.24】为 Paddle 新增 paddle.sparse.is_nan 稀疏 API #51513

Merged
merged 31 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
c8ae296
untracked files
thunder95 Feb 20, 2023
6aa02f0
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
thunder95 Feb 23, 2023
d599110
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
thunder95 Feb 23, 2023
264894d
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
thunder95 Feb 25, 2023
98d1e1c
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
thunder95 Feb 25, 2023
b958122
Merge branch 'develop' of https://github.com/thunder95/Paddle into de…
thunder95 Feb 25, 2023
760e099
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
thunder95 Feb 26, 2023
e16076d
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
thunder95 Feb 26, 2023
085c7a6
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
thunder95 Mar 2, 2023
b1edf68
Merge branch 'develop' of https://github.com/thunder95/Paddle into de…
thunder95 Mar 2, 2023
366305e
to_dense error
thunder95 Mar 11, 2023
06abd01
remove unused files
thunder95 Mar 11, 2023
7399da9
add static
thunder95 Mar 16, 2023
daaa4d5
bool_dtype
thunder95 Mar 21, 2023
de93190
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
thunder95 Mar 21, 2023
5e5c86b
restart ci
thunder95 Mar 21, 2023
929963a
fix datatype bool
thunder95 Mar 21, 2023
0f13d4e
fix sample code
thunder95 Mar 22, 2023
fb34f98
values_coo_grad bool
thunder95 Mar 22, 2023
7a70698
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
thunder95 Mar 22, 2023
3fb3fa5
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
thunder95 Mar 26, 2023
2284e65
restart ci
thunder95 Mar 26, 2023
94f9bc5
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
thunder95 Mar 28, 2023
0075dcf
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
thunder95 Mar 28, 2023
f323b0a
remove duplicate def
thunder95 Mar 28, 2023
9282ed7
Merge branch 'is_nan_sparse' of https://github.com/thunder95/Paddle i…
thunder95 Mar 29, 2023
33a5ec8
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
thunder95 Mar 29, 2023
fe595a5
fix out type error
thunder95 Mar 29, 2023
cdeeacd
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
thunder95 Mar 29, 2023
015f94e
optimize doc
thunder95 Mar 30, 2023
987d523
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
thunder95 Mar 30, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions paddle/phi/api/yaml/sparse_ops.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,17 @@
layout : x
backward : expm1_grad

- op : isnan
args : (Tensor x)
output : Tensor(out)
infer_meta :
func : IsfiniteInferMeta
Copy link
Contributor

Choose a reason for hiding this comment

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

这个名字不太对?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

IsfiniteInferMeta吗?最新提交已修改成unchanged

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@zhouwei25 如果改成unchanged,还是会报错,最新提交又修改回IsfiniteInferMeta
InvalidArgumentError: The type of data we are trying to retrieve (float32) does not match the type of data (bool) currently contained in the container.

Copy link
Contributor

Choose a reason for hiding this comment

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

@zhouwei25 如果改成unchanged,还是会报错,最新提交又修改回IsfiniteInferMeta InvalidArgumentError: The type of data we are trying to retrieve (float32) does not match the type of data (bool) currently contained in the container.

就是命名风格有点问题,这个不是IsNanInferMeta吗

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@zhouwei25 IsfiniteInferMeta是参考dense tensor那里设计的IsfiniteInferMeta, 考虑到代码可能会冗余就这么直接复用了,老师建议这个地方是需要单独写一个IsNanInferMeta吗

Copy link
Contributor Author

Choose a reason for hiding this comment

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

PD_REGISTER_INFER_META_FN(isnan, phi::IsfiniteInferMeta);

Copy link
Contributor

Choose a reason for hiding this comment

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

好复用也可以

param: [x]
kernel :
func : isnan_coo{sparse_coo -> sparse_coo},
isnan_csr{sparse_csr -> sparse_csr}
layout : x

- op : leaky_relu
args : (Tensor x, float alpha)
output : Tensor(out)
Expand Down
3 changes: 2 additions & 1 deletion paddle/phi/kernels/sparse/cpu/mask_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ PD_REGISTER_KERNEL(mask_coo,
int8_t,
int16_t,
int,
int64_t) {
int64_t,
bool) {
kernel->InputAt(1).SetDataLayout(phi::DataLayout::SPARSE_COO);
}

Expand Down
12 changes: 8 additions & 4 deletions paddle/phi/kernels/sparse/cpu/sparse_utils_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,8 @@ PD_REGISTER_KERNEL(coo_to_dense,
int8_t,
int16_t,
int,
int64_t) {}
int64_t,
bool) {}

PD_REGISTER_KERNEL(csr_to_dense,
CPU,
Expand All @@ -383,7 +384,8 @@ PD_REGISTER_KERNEL(csr_to_dense,
int8_t,
int16_t,
int,
int64_t) {}
int64_t,
bool) {}

PD_REGISTER_KERNEL(values_coo,
CPU,
Expand All @@ -396,7 +398,8 @@ PD_REGISTER_KERNEL(values_coo,
int8_t,
int16_t,
int,
int64_t) {
int64_t,
bool) {
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO);
}

Expand Down Expand Up @@ -426,7 +429,8 @@ PD_REGISTER_KERNEL(values_csr,
int8_t,
int16_t,
int,
int64_t) {
int64_t,
bool) {
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR);
}

Expand Down
24 changes: 24 additions & 0 deletions paddle/phi/kernels/sparse/cpu/unary_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,27 @@ PD_REGISTER_KERNEL(cast_csr,
int,
int64_t,
bool) {}

PD_REGISTER_KERNEL(isnan_coo,
CPU,
ALL_LAYOUT,
phi::sparse::IsnanCooKernel,
float,
double,
phi::dtype::float16,
int,
int64_t) {
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO);
}

PD_REGISTER_KERNEL(isnan_csr,
CPU,
ALL_LAYOUT,
phi::sparse::IsnanCsrKernel,
float,
double,
phi::dtype::float16,
int,
int64_t) {
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR);
}
3 changes: 2 additions & 1 deletion paddle/phi/kernels/sparse/gpu/mask_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,8 @@ PD_REGISTER_KERNEL(mask_coo,
int8_t,
int16_t,
int,
int64_t) {
int64_t,
bool) {
kernel->InputAt(1).SetDataLayout(phi::DataLayout::SPARSE_COO);
}

Expand Down
12 changes: 8 additions & 4 deletions paddle/phi/kernels/sparse/gpu/sparse_utils_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,8 @@ PD_REGISTER_KERNEL(coo_to_dense,
int8_t,
int16_t,
int,
int64_t) {}
int64_t,
bool) {}

PD_REGISTER_KERNEL(csr_to_dense,
GPU,
Expand All @@ -593,7 +594,8 @@ PD_REGISTER_KERNEL(csr_to_dense,
int8_t,
int16_t,
int,
int64_t) {}
int64_t,
bool) {}

PD_REGISTER_KERNEL(values_coo,
GPU,
Expand All @@ -606,7 +608,8 @@ PD_REGISTER_KERNEL(values_coo,
int8_t,
int16_t,
int,
int64_t) {
int64_t,
bool) {
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO);
}

Expand All @@ -621,7 +624,8 @@ PD_REGISTER_KERNEL(values_csr,
int8_t,
int16_t,
int,
int64_t) {
int64_t,
bool) {
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR);
}

Expand Down
24 changes: 24 additions & 0 deletions paddle/phi/kernels/sparse/gpu/unary_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,27 @@ PD_REGISTER_KERNEL(cast_csr,
int,
int64_t,
bool) {}

PD_REGISTER_KERNEL(isnan_coo,
GPU,
ALL_LAYOUT,
phi::sparse::IsnanCooKernel,
float,
double,
phi::dtype::float16,
int,
int64_t) {
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO);
}

PD_REGISTER_KERNEL(isnan_csr,
GPU,
ALL_LAYOUT,
phi::sparse::IsnanCsrKernel,
float,
double,
phi::dtype::float16,
int,
int64_t) {
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR);
}
40 changes: 40 additions & 0 deletions paddle/phi/kernels/sparse/impl/unary_kernel_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "paddle/phi/kernels/abs_kernel.h"
#include "paddle/phi/kernels/activation_kernel.h"
#include "paddle/phi/kernels/cast_kernel.h"
#include "paddle/phi/kernels/isfinite_kernel.h"
#include "paddle/phi/kernels/scale_kernel.h"
#include "paddle/phi/kernels/sparse/empty_kernel.h"
#include "paddle/phi/kernels/trunc_kernel.h"
Expand Down Expand Up @@ -219,5 +220,44 @@ void CastCsrKernel(const Context& dev_ctx,
}
}

template <typename T, typename Context>
void IsnanCooKernel(const Context& dev_ctx,
Copy link
Contributor

Choose a reason for hiding this comment

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

这个用目前的公共组件,宏函数来注册kernel。可以复用减少代码

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@zhouwei25 公共组件不能满足这个算子,因为emptylike会创建一个相同类型的输出tensor,而这个算子输出是bool型的,所以这里单独写了个kernel。

Copy link
Contributor

Choose a reason for hiding this comment

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

@zhouwei25 公共组件不能满足这个算子,因为emptylike会创建一个相同类型的输出tensor,而这个算子输出是bool型的,所以这里单独写了个kernel。

OK

const SparseCooTensor& x,
SparseCooTensor* out) {
*(out->mutable_indices()) = x.indices();
const DenseTensor& x_values = x.non_zero_elements();
DenseTensor* out_values = out->mutable_non_zero_elements();

phi::MetaTensor meta(out_values);
meta.set_dims(x_values.dims());
meta.set_dtype(DataType::BOOL);

phi::IsnanKernel<T, Context>(
dev_ctx, x.non_zero_elements(), out->mutable_non_zero_elements());
out->SetIndicesDict(x.GetIndicesDict());
}

template <typename T, typename Context>
void IsnanCsrKernel(const Context& dev_ctx,
const SparseCsrTensor& x,
SparseCsrTensor* out) {
const DenseTensor& x_crows = x.crows();
const DenseTensor& x_cols = x.cols();
const DenseTensor& x_values = x.non_zero_elements();
DenseTensor* out_crows = out->mutable_crows();
DenseTensor* out_cols = out->mutable_cols();
DenseTensor* out_values = out->mutable_non_zero_elements();

*out_crows = x_crows;
*out_cols = x_cols;

phi::MetaTensor meta(out_values);
meta.set_dims(x_values.dims());
meta.set_dtype(DataType::BOOL);

phi::IsnanKernel<T, Context>(
dev_ctx, x.non_zero_elements(), out->mutable_non_zero_elements());
}

} // namespace sparse
} // namespace phi
12 changes: 8 additions & 4 deletions paddle/phi/kernels/sparse/sparse_utils_grad_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ PD_REGISTER_KERNEL(values_coo_grad,
int8_t,
int16_t,
int,
int64_t) {
int64_t,
bool) {
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO);
}

Expand All @@ -62,7 +63,8 @@ PD_REGISTER_KERNEL(coo_to_dense_grad,
int8_t,
int16_t,
int,
int64_t) {
int64_t,
bool) {
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO);
}

Expand Down Expand Up @@ -91,7 +93,8 @@ PD_REGISTER_KERNEL(values_coo_grad,
int8_t,
int16_t,
int,
int64_t) {
int64_t,
bool) {
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO);
}
PD_REGISTER_KERNEL(coo_to_dense_grad,
Expand All @@ -105,7 +108,8 @@ PD_REGISTER_KERNEL(coo_to_dense_grad,
int8_t,
int16_t,
int,
int64_t) {
int64_t,
bool) {
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO);
}
PD_REGISTER_KERNEL(sparse_coo_tensor_grad,
Expand Down
1 change: 1 addition & 0 deletions paddle/phi/kernels/sparse/unary_kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ DECLARE_SPARSE_UNARY_KERNEL(Sinh)
DECLARE_SPARSE_UNARY_KERNEL(Asinh)
DECLARE_SPARSE_UNARY_KERNEL(Atanh)
DECLARE_SPARSE_UNARY_KERNEL(Relu)
DECLARE_SPARSE_UNARY_KERNEL(Isnan)
DECLARE_SPARSE_UNARY_KERNEL(Tanh)
DECLARE_SPARSE_UNARY_KERNEL(Square)
DECLARE_SPARSE_UNARY_KERNEL(Sqrt)
Expand Down
97 changes: 97 additions & 0 deletions python/paddle/fluid/tests/unittests/test_sparse_isnan_op.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest

import numpy as np

import paddle


class TestSparseIsnan(unittest.TestCase):
"""
Test the API paddle.sparse.isnan on some sparse tensors.
x: sparse tensor, out: sparse tensor
"""

def to_sparse(self, x, format):
if format == 'coo':
return x.detach().to_sparse_coo(sparse_dim=x.ndim)
elif format == 'csr':
return x.detach().to_sparse_csr()

def check_result(self, x_shape, format, data_type="float32"):
raw_inp = np.random.randint(-100, 100, x_shape)
mask = np.random.randint(0, 2, x_shape)
inp_x = (raw_inp * mask).astype(data_type)
inp_x[inp_x > 0] = np.nan
np_out = np.isnan(inp_x[inp_x != 0])

dense_x = paddle.to_tensor(inp_x)
sp_x = self.to_sparse(dense_x, format)
sp_out = paddle.sparse.isnan(sp_x)
sp_out_values = sp_out.values().numpy()

np.testing.assert_allclose(np_out, sp_out_values, rtol=1e-05)

def test_isnan_shape(self):
self.check_result([20], 'coo')

self.check_result([4, 5], 'coo')
self.check_result([4, 5], 'csr')

self.check_result([8, 16, 32], 'coo')
self.check_result([8, 16, 32], 'csr')

def test_isnan_dtype(self):
self.check_result([4, 5], 'coo', "float32")
self.check_result([4, 5], 'csr', "float32")

self.check_result([8, 16, 32], 'coo', "float64")
self.check_result([8, 16, 32], 'csr', "float64")


class TestStatic(unittest.TestCase):
def test(self):
paddle.enable_static()

indices = paddle.static.data(
name='indices', shape=[2, 3], dtype='int32'
)
values = paddle.static.data(name='values', shape=[3], dtype='float32')

dense_shape = [3, 3]
sp_x = paddle.sparse.sparse_coo_tensor(indices, values, dense_shape)
sp_y = paddle.sparse.isnan(sp_x)
out = sp_y.to_dense()

exe = paddle.static.Executor()
indices_data = [[0, 1, 2], [1, 2, 0]]
values_data = np.array([1.0, float("nan"), 3.0]).astype('float32')

fetch = exe.run(
feed={'indices': indices_data, 'values': values_data},
fetch_list=[out],
return_numpy=True,
)

correct_out = np.array(
[[False, False, False], [False, False, True], [False, False, False]]
).astype('float32')
np.testing.assert_allclose(correct_out, fetch[0], rtol=1e-5)
paddle.disable_static()


if __name__ == "__main__":
unittest.main()
2 changes: 2 additions & 0 deletions python/paddle/sparse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from .unary import expm1
from .unary import transpose
from .unary import reshape
from .unary import isnan

from .binary import mv
from .binary import matmul
Expand Down Expand Up @@ -83,4 +84,5 @@
'coalesce',
'is_same_shape',
'reshape',
'isnan',
]
Loading