From 97ac270680ae64c77aecc07f2396d889c359e485 Mon Sep 17 00:00:00 2001 From: tiancaishaonvjituizi <452565578@qq.com> Date: Sun, 3 Apr 2022 00:07:40 +0800 Subject: [PATCH 01/31] refactor unary sparse ops and add relu --- paddle/phi/kernels/activation_grad_kernel.h | 1 + .../kernels/sparse/activation_grad_kernel.cc | 57 +----- .../kernels/sparse/activation_grad_kernel.h | 23 ++- .../phi/kernels/sparse/activation_kernel.cc | 52 +----- paddle/phi/kernels/sparse/activation_kernel.h | 28 ++- paddle/phi/kernels/sparse/utils.h | 170 ++++++++++++++++++ python/paddle/utils/code_gen/sparse_api.yaml | 30 +++- .../paddle/utils/code_gen/sparse_bw_api.yaml | 27 ++- 8 files changed, 270 insertions(+), 118 deletions(-) create mode 100644 paddle/phi/kernels/sparse/utils.h diff --git a/paddle/phi/kernels/activation_grad_kernel.h b/paddle/phi/kernels/activation_grad_kernel.h index be6f97ad7c96e..9801725a3ebb3 100644 --- a/paddle/phi/kernels/activation_grad_kernel.h +++ b/paddle/phi/kernels/activation_grad_kernel.h @@ -187,6 +187,7 @@ DECLARE_ACTIVATION_GRAD_KERNEL_DEPX(Log1p); DECLARE_ACTIVATION_GRAD_KERNEL_DEPOUT(Relu); DECLARE_ACTIVATION_GRAD_KERNEL_DEPOUT(Tanh); DECLARE_ACTIVATION_GRAD_KERNEL_DEPOUT(Sigmoid); +DECLARE_ACTIVATION_GRAD_KERNEL_DEPOUT(Sqrt); DECLARE_ACTIVATION_GRAD_KERNEL_NODEP(Round); DECLARE_ACTIVATION_GRAD_KERNEL_NODEP(Floor); diff --git a/paddle/phi/kernels/sparse/activation_grad_kernel.cc b/paddle/phi/kernels/sparse/activation_grad_kernel.cc index 9eca14e660939..628db10532da2 100644 --- a/paddle/phi/kernels/sparse/activation_grad_kernel.cc +++ b/paddle/phi/kernels/sparse/activation_grad_kernel.cc @@ -13,58 +13,9 @@ See the License for the specific language governing permissions and limitations under the License. */ #include "paddle/phi/kernels/sparse/activation_grad_kernel.h" -#include "paddle/phi/kernels/activation_grad_kernel.h" -#include "paddle/phi/kernels/copy_kernel.h" -#include "paddle/phi/kernels/empty_kernel.h" - -#include "paddle/phi/backends/cpu/cpu_context.h" -#include "paddle/phi/backends/gpu/gpu_context.h" -#include "paddle/phi/core/kernel_registry.h" - -namespace phi { -namespace sparse { -template -void SparseReluGradKernel(const Context& dev_ctx, - const SparseCooTensor& x, - const SparseCooTensor& out_grad, - SparseCooTensor* x_grad) { - DenseTensor non_zero_indices = - phi::EmptyLike(dev_ctx, x.non_zero_indices()); - DenseTensor non_zero_elements = - phi::EmptyLike(dev_ctx, x.non_zero_elements()); - phi::Copy(dev_ctx, - x.non_zero_indices(), - dev_ctx.GetPlace(), - false, - &non_zero_indices); - phi::ReluGradKernel(dev_ctx, - x.non_zero_elements(), - out_grad.non_zero_elements(), - &non_zero_elements); - x_grad->SetMember(non_zero_indices, non_zero_elements, x.dims(), true); -} - -} // namespace sparse -} // namespace phi - -PD_REGISTER_KERNEL(sparse_relu_grad, - CPU, - ALL_LAYOUT, - phi::sparse::SparseReluGradKernel, - float, - double) { - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); -} +#include "paddle/phi/kernels/activation_grad_kernel.h" +#include "paddle/phi/kernels/sparse/utils.h" -#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) -PD_REGISTER_KERNEL(sparse_relu_grad, - GPU, - ALL_LAYOUT, - phi::sparse::SparseReluGradKernel, - float, - double, - phi::dtype::float16) { - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); -} -#endif +DEFINE_AND_REGISTER_SPARSE_UNARY_GRAD_KERNEL(sparse_relu_grad, ReluGradKernel) +DEFINE_AND_REGISTER_SPARSE_UNARY_GRAD_KERNEL(sparse_sqrt_grad, SqrtGradKernel) diff --git a/paddle/phi/kernels/sparse/activation_grad_kernel.h b/paddle/phi/kernels/sparse/activation_grad_kernel.h index aab4a3e5a590b..0c7c448bf8fb9 100644 --- a/paddle/phi/kernels/sparse/activation_grad_kernel.h +++ b/paddle/phi/kernels/sparse/activation_grad_kernel.h @@ -15,15 +15,28 @@ limitations under the License. */ #pragma once #include "paddle/phi/core/sparse_coo_tensor.h" +#include "paddle/phi/core/sparse_csr_tensor.h" namespace phi { namespace sparse { -template -void SparseReluGradKernel(const Context& dev_ctx, - const SparseCooTensor& x, - const SparseCooTensor& out_grad, - SparseCooTensor* x_grad); +#define DECLARE_SPARSE_ACTIVATION_GRAD_KERNEL(name) \ + template \ + void SparseCoo##name##GradKernel(const Context& dev_ctx, \ + const SparseCooTensor& x, \ + const SparseCooTensor& out_grad, \ + SparseCooTensor* x_grad); \ + \ + template \ + void SparseCsr##name##GradKernel(const Context& dev_ctx, \ + const SparseCsrTensor& x, \ + const SparseCsrTensor& out_grad, \ + SparseCsrTensor* x_grad); + +DECLARE_SPARSE_ACTIVATION_GRAD_KERNEL(Relu) +DECLARE_SPARSE_ACTIVATION_GRAD_KERNEL(Sqrt) + +#undef DECLARE_SPARSE_ACTIVATION_GRAD_KERNEL } // namespace sparse } // namespace phi diff --git a/paddle/phi/kernels/sparse/activation_kernel.cc b/paddle/phi/kernels/sparse/activation_kernel.cc index a1a00897d33cf..c541b14d5ddba 100644 --- a/paddle/phi/kernels/sparse/activation_kernel.cc +++ b/paddle/phi/kernels/sparse/activation_kernel.cc @@ -13,54 +13,8 @@ See the License for the specific language governing permissions and limitations under the License. */ #include "paddle/phi/kernels/sparse/activation_kernel.h" -#include "paddle/phi/kernels/copy_kernel.h" -#include "paddle/phi/kernels/empty_kernel.h" -#include "paddle/phi/backends/cpu/cpu_context.h" -#include "paddle/phi/backends/gpu/gpu_context.h" -#include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/kernels/sparse/utils.h" -namespace phi { -namespace sparse { - -template -void SparseReluKernel(const Context& dev_ctx, - const SparseCooTensor& x, - SparseCooTensor* out) { - DenseTensor non_zero_indices = - phi::EmptyLike(dev_ctx, x.non_zero_indices()); - DenseTensor non_zero_elements = - phi::EmptyLike(dev_ctx, x.non_zero_elements()); - phi::Copy(dev_ctx, - x.non_zero_indices(), - dev_ctx.GetPlace(), - false, - &non_zero_indices); - phi::ReluKernel( - dev_ctx, x.non_zero_elements(), &non_zero_elements); - out->SetMember(non_zero_indices, non_zero_elements, x.dims(), true); -} - -} // namespace sparse -} // namespace phi - -PD_REGISTER_KERNEL(sparse_relu, - CPU, - ALL_LAYOUT, - phi::sparse::SparseReluKernel, - float, - double) { - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); -} - -#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) -PD_REGISTER_KERNEL(sparse_relu, - GPU, - ALL_LAYOUT, - phi::sparse::SparseReluKernel, - float, - double, - phi::dtype::float16) { - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); -} -#endif +DEFINE_AND_REGISTER_SPARSE_UNARY_KERNEL(sparse_relu, ReluKernel) +DEFINE_AND_REGISTER_SPARSE_UNARY_KERNEL(sparse_sqrt, SqrtKernel) diff --git a/paddle/phi/kernels/sparse/activation_kernel.h b/paddle/phi/kernels/sparse/activation_kernel.h index 568c0aa8b2ecb..034b84c6f2186 100644 --- a/paddle/phi/kernels/sparse/activation_kernel.h +++ b/paddle/phi/kernels/sparse/activation_kernel.h @@ -16,22 +16,40 @@ limitations under the License. */ #include "paddle/phi/core/dense_tensor.h" #include "paddle/phi/core/sparse_coo_tensor.h" +#include "paddle/phi/core/sparse_csr_tensor.h" #include "paddle/phi/kernels/activation_kernel.h" #include "paddle/phi/kernels/empty_kernel.h" namespace phi { namespace sparse { -template -void SparseReluKernel(const Context& dev_ctx, - const SparseCooTensor& x, - SparseCooTensor* out); +#define DECLARE_SPARSE_ACTIVATION_KERNEL(name) \ + template \ + void SparseCoo##name##Kernel( \ + const Context& dev_ctx, const SparseCooTensor& x, SparseCooTensor* out); \ + \ + template \ + void SparseCsr##name##Kernel( \ + const Context& dev_ctx, const SparseCsrTensor& x, SparseCsrTensor* out); + +DECLARE_SPARSE_ACTIVATION_KERNEL(Relu) +DECLARE_SPARSE_ACTIVATION_KERNEL(Sqrt) + +#undef DECLARE_SPARSE_ACTIVATION_KERNEL template SparseCooTensor SparseRelu(const Context& dev_ctx, const SparseCooTensor& x) { DenseTensor indices, values; SparseCooTensor coo(indices, values, x.dims()); - SparseReluKernel(dev_ctx, x, &coo); + SparseCooReluKernel(dev_ctx, x, &coo); + return coo; +} + +template +SparseCooTensor SparseSqrt(const Context& dev_ctx, const SparseCooTensor& x) { + DenseTensor indices, values; + SparseCooTensor coo(indices, values, x.dims()); + SparseCooSqrtKernel(dev_ctx, x, &coo); return coo; } diff --git a/paddle/phi/kernels/sparse/utils.h b/paddle/phi/kernels/sparse/utils.h new file mode 100644 index 0000000000000..fc04f5487a790 --- /dev/null +++ b/paddle/phi/kernels/sparse/utils.h @@ -0,0 +1,170 @@ +#include "paddle/phi/backends/cpu/cpu_context.h" +#include "paddle/phi/backends/gpu/gpu_context.h" +#include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/kernels/copy_kernel.h" +#include "paddle/phi/kernels/empty_kernel.h" + +#define DEFINE_SPARSE_UNARY_KERNEL(dense_kernel_func) \ + namespace phi { \ + namespace sparse { \ + \ + template \ + void SparseCoo##dense_kernel_func(const Context& dev_ctx, \ + const SparseCooTensor& x, \ + SparseCooTensor* out) { \ + DenseTensor non_zero_indices = \ + phi::EmptyLike(dev_ctx, x.non_zero_indices()); \ + DenseTensor non_zero_elements = \ + phi::EmptyLike(dev_ctx, x.non_zero_elements()); \ + phi::Copy(dev_ctx, \ + x.non_zero_indices(), \ + dev_ctx.GetPlace(), \ + false, \ + &non_zero_indices); \ + phi::dense_kernel_func( \ + dev_ctx, x.non_zero_elements(), &non_zero_elements); \ + out->SetMember(non_zero_indices, non_zero_elements, x.dims(), true); \ + } \ + \ + template \ + void SparseCsr##dense_kernel_func(const Context& dev_ctx, \ + const SparseCsrTensor& x, \ + SparseCsrTensor* out) { \ + DenseTensor non_zero_crows = \ + phi::EmptyLike(dev_ctx, x.non_zero_crows()); \ + DenseTensor non_zero_cols = \ + phi::EmptyLike(dev_ctx, x.non_zero_cols()); \ + DenseTensor non_zero_elements = \ + phi::EmptyLike(dev_ctx, x.non_zero_elements()); \ + phi::Copy(dev_ctx, \ + x.non_zero_crows(), \ + dev_ctx.GetPlace(), \ + false, \ + &non_zero_crows); \ + phi::Copy(dev_ctx, \ + x.non_zero_cols(), \ + dev_ctx.GetPlace(), \ + false, \ + &non_zero_cols); \ + phi::dense_kernel_func( \ + dev_ctx, x.non_zero_elements(), &non_zero_elements); \ + out->SetMember( \ + non_zero_crows, non_zero_cols, non_zero_elements, x.dims()); \ + } \ + } \ + } + +#define DEFINE_SPARSE_UNARY_GRAD_KERNEL(dense_kernel_func) \ + namespace phi { \ + namespace sparse { \ + \ + template \ + void SparseCoo##dense_kernel_func(const Context& dev_ctx, \ + const SparseCooTensor& x, \ + const SparseCooTensor& out_grad, \ + SparseCooTensor* x_grad) { \ + DenseTensor non_zero_indices = \ + phi::EmptyLike(dev_ctx, x.non_zero_indices()); \ + DenseTensor non_zero_elements = \ + phi::EmptyLike(dev_ctx, x.non_zero_elements()); \ + phi::Copy(dev_ctx, \ + x.non_zero_indices(), \ + dev_ctx.GetPlace(), \ + false, \ + &non_zero_indices); \ + phi::dense_kernel_func(dev_ctx, \ + x.non_zero_elements(), \ + out_grad.non_zero_elements(), \ + &non_zero_elements); \ + x_grad->SetMember(non_zero_indices, non_zero_elements, x.dims(), true); \ + } \ + \ + template \ + void SparseCsr##dense_kernel_func(const Context& dev_ctx, \ + const SparseCsrTensor& x, \ + const SparseCsrTensor& out_grad, \ + SparseCsrTensor* out) { \ + DenseTensor non_zero_crows = \ + phi::EmptyLike(dev_ctx, x.non_zero_crows()); \ + DenseTensor non_zero_cols = \ + phi::EmptyLike(dev_ctx, x.non_zero_cols()); \ + DenseTensor non_zero_elements = \ + phi::EmptyLike(dev_ctx, x.non_zero_elements()); \ + phi::Copy(dev_ctx, \ + x.non_zero_crows(), \ + dev_ctx.GetPlace(), \ + false, \ + &non_zero_crows); \ + phi::Copy(dev_ctx, \ + x.non_zero_cols(), \ + dev_ctx.GetPlace(), \ + false, \ + &non_zero_cols); \ + phi::dense_kernel_func(dev_ctx, \ + x.non_zero_elements(), \ + out_grad.non_zero_elements(), \ + &non_zero_elements); \ + out->SetMember( \ + non_zero_crows, non_zero_cols, non_zero_elements, x.dims()); \ + } \ + } \ + } + +#define REGISTER_CPU_SPARSE_UNARY_KERNEL(kernel_name, dense_kernel_func) \ + PD_REGISTER_KERNEL(sparse_coo_##kernel_name, \ + CPU, \ + ALL_LAYOUT, \ + phi::sparse::SparseCoo##dense_kernel_func, \ + float, \ + double) { \ + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); \ + } \ + PD_REGISTER_KERNEL(sparse_csr_##kernel_name, \ + CPU, \ + ALL_LAYOUT, \ + phi::sparse::SparseCsr##dense_kernel_func, \ + float, \ + double) { \ + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); \ + } + +#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) +#define REGISTER_GPU_SPARSE_UNARY_KERNEL(kernel_name, dense_kernel_func) \ + PD_REGISTER_KERNEL(sparse_coo_##kernel_name, \ + GPU, \ + ALL_LAYOUT, \ + phi::sparse::SparseCoo##dense_kernel_func, \ + float, \ + double, \ + phi::dtype::float16) { \ + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); \ + } \ + \ + PD_REGISTER_KERNEL(sparse_csr_##kernel_name, \ + GPU, \ + ALL_LAYOUT, \ + phi::sparse::SparseCsr##dense_kernel_func, \ + float, \ + double, \ + phi::dtype::float16) { \ + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); \ + } +#else +// This macro definition is empty when GPU is disabled +#define REGISTER_GPU_SPARSE_UNARY_KERNEL(sparse_kernel_name, dense_kernel_func) +#endif + +#define REGISTER_SPARSE_UNARY_KERNEL(kernel_name, dense_kernel_func) \ + REGISTER_CPU_SPARSE_UNARY_KERNEL(kernel_name, dense_kernel_func) \ + REGISTER_GPU_SPARSE_UNARY_KERNEL(kernel_name, dense_kernel_func) + +#define DEFINE_AND_REGISTER_SPARSE_UNARY_KERNEL(kernel_name, \ + dense_kernel_func) \ + DEFINE_SPARSE_UNARY_KERNEL(dense_kernel_func) \ + REGISTER_SPARSE_UNARY_KERNEL(kernel_name, dense_kernel_func) + +#define DEFINE_AND_REGISTER_SPARSE_UNARY_GRAD_KERNEL(kernel_name, \ + dense_kernel_func) \ + DEFINE_SPARSE_UNARY_GRAD_KERNEL(dense_kernel_func) \ + REGISTER_SPARSE_UNARY_KERNEL(kernel_name, dense_kernel_func) + diff --git a/python/paddle/utils/code_gen/sparse_api.yaml b/python/paddle/utils/code_gen/sparse_api.yaml index 56b253159fa72..a8f2dd1a4d69f 100644 --- a/python/paddle/utils/code_gen/sparse_api.yaml +++ b/python/paddle/utils/code_gen/sparse_api.yaml @@ -7,13 +7,37 @@ intermediate : rulebook backward : conv3d_grad -- api : relu +- api : coo_relu args : (Tensor x) output : Tensor(out@SparseCooTensor) kernel : - func : sparse_relu + func : sparse_coo_relu layout : x - backward : sparse_relu_grad + backward : sparse_coo_relu_grad + +- api : csr_relu + args : (Tensor x) + output : Tensor(out@SparseCsrTensor) + kernel : + func : sparse_csr_relu + layout : x + backward : sparse_csr_relu_grad + +- api : coo_sqrt + args : (Tensor x) + output : Tensor(out@SparseCooTensor) + kernel : + func : sparse_coo_sqrt + layout : x + backward : sparse_coo_sqrt_grad + +- api : csr_sqrt + args : (Tensor x) + output : Tensor(out@SparseCsrTensor) + kernel : + func : sparse_csr_sqrt + layout : x + backward : sparse_csr_sqrt_grad - api : to_dense args : (Tensor x) diff --git a/python/paddle/utils/code_gen/sparse_bw_api.yaml b/python/paddle/utils/code_gen/sparse_bw_api.yaml index 7ffc906b22084..b0dfc231070db 100644 --- a/python/paddle/utils/code_gen/sparse_bw_api.yaml +++ b/python/paddle/utils/code_gen/sparse_bw_api.yaml @@ -5,9 +5,30 @@ kernel : func : sparse_conv3d_grad -- backward_api : sparse_relu_grad - forward : sparse_relu(Tensor x) -> Tensor(out@SparseCooTensor) +- backward_api : sparse_coo_relu_grad + forward : sparse_coo_relu(Tensor x) -> Tensor(out@SparseCooTensor) args : (Tensor x, Tensor out_grad) output : Tensor(x_grad@SparseCooTensor) kernel : - func : sparse_relu_grad + func : sparse_coo_relu_grad + +- backward_api : sparse_csr_relu_grad + forward : sparse_csr_relu(Tensor x) -> Tensor(out@SparseCsrTensor) + args : (Tensor x, Tensor out_grad) + output : Tensor(x_grad@SparseCsrTensor) + kernel : + func : sparse_csr_relu_grad + +- backward_api : sparse_coo_sqrt_grad + forward : sparse_coo_sqrt(Tensor x) -> Tensor(out@SparseCooTensor) + args : (Tensor x, Tensor out_grad) + output : Tensor(x_grad@SparseCooTensor) + kernel : + func : sparse_coo_sqrt_grad + +- backward_api : sparse_csr_sqrt_grad + forward : sparse_csr_sqrt(Tensor x) -> Tensor(out@SparseCsrTensor) + args : (Tensor x, Tensor out_grad) + output : Tensor(x_grad@SparseCsrTensor) + kernel : + func : sparse_csr_sqrt_grad From 26f466229888d517cafda8776556bacb290f171c Mon Sep 17 00:00:00 2001 From: tiancaishaonvjituizi <452565578@qq.com> Date: Sun, 3 Apr 2022 00:12:09 +0800 Subject: [PATCH 02/31] add test --- .../unittests/test_sparse_activation_op.py | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/python/paddle/fluid/tests/unittests/test_sparse_activation_op.py b/python/paddle/fluid/tests/unittests/test_sparse_activation_op.py index df13ae4e4b7ff..fb82b76057d39 100644 --- a/python/paddle/fluid/tests/unittests/test_sparse_activation_op.py +++ b/python/paddle/fluid/tests/unittests/test_sparse_activation_op.py @@ -30,11 +30,33 @@ def test_sparse_relu(self): sparse_dim = 2 sparse_coo_x = dense_x.to_sparse_coo(sparse_dim) #TODO(zhangkaihuo): change to test the corresponding API: paddle.sparse.relu(sparse_coo_x) - sparse_act_out = _C_ops.final_state_sparse_relu(sparse_coo_x) + sparse_act_out = _C_ops.final_state_sparse_coo_relu(sparse_coo_x) correct_result = [0, 2, 0, 4, 5] actual_result = sparse_act_out.non_zero_elements().numpy() assert np.array_equal(correct_result, actual_result) + def test_sparse_coo_sqrt(self): + with _test_eager_guard(): + x = [[0, 4, 0, 2], [0, 0, 16, 0]] + dense_x = paddle.to_tensor(x, dtype='float32') + sparse_dim = 2 + sparse_coo_x = dense_x.to_sparse_coo(sparse_dim) + sparse_act_out = _C_ops.final_state_sparse_coo_sqrt(sparse_coo_x) + correct_result = [2, np.sqrt(2), 4] + actual_result = sparse_act_out.non_zero_elements().numpy() + assert np.array_equal(correct_result, actual_result) + + def test_sparse_csr_sqrt(self): + with _test_eager_guard(): + x = [[0, 4, 0, 2], [0, 0, 0, 0], [0, 0, 16, 0]] + dense_x = paddle.to_tensor(x, dtype='float32') + sparse_dim = 2 + sparse_coo_x = dense_x.to_sparse_csr(sparse_dim) + sparse_act_out = _C_ops.final_state_sparse_csr_sqrt(sparse_coo_x) + correct_result = [2, np.sqrt(2), 4] + actual_result = sparse_act_out.non_zero_elements().numpy() + assert np.array_equal(correct_result, actual_result) + if __name__ == "__main__": unittest.main() From a7f34103bfc38f5db6123bd79812aec9a62e1de3 Mon Sep 17 00:00:00 2001 From: tiancaishaonvjituizi <452565578@qq.com> Date: Mon, 4 Apr 2022 16:58:39 +0800 Subject: [PATCH 03/31] fix the bug in generated api code, tests are passed now --- paddle/phi/api/lib/api_gen_utils.cc | 2 +- paddle/phi/core/sparse_csr_tensor.cc | 8 +++++--- .../kernels/sparse/activation_grad_kernel.cc | 20 +++++++++++++++++-- .../phi/kernels/sparse/activation_kernel.cc | 20 +++++++++++++++++-- .../unittests/test_sparse_activation_op.py | 7 +++---- 5 files changed, 45 insertions(+), 12 deletions(-) diff --git a/paddle/phi/api/lib/api_gen_utils.cc b/paddle/phi/api/lib/api_gen_utils.cc index 7cbb4344e81d7..50989933088a1 100644 --- a/paddle/phi/api/lib/api_gen_utils.cc +++ b/paddle/phi/api/lib/api_gen_utils.cc @@ -144,7 +144,7 @@ phi::TensorBase* SetSparseKernelOutput(Tensor* out, TensorType type) { std::make_shared(phi::DenseTensor(), phi::DenseTensor(), phi::DenseTensor(), - phi::DDim{-1}); + phi::DDim{-1, -1}); out->set_impl(sparse_tensor); return sparse_tensor.get(); } else { diff --git a/paddle/phi/core/sparse_csr_tensor.cc b/paddle/phi/core/sparse_csr_tensor.cc index ab9717a564eb5..447fab0e33c5b 100644 --- a/paddle/phi/core/sparse_csr_tensor.cc +++ b/paddle/phi/core/sparse_csr_tensor.cc @@ -27,9 +27,11 @@ SparseCsrTensor::SparseCsrTensor() { inline void check_shape(const DDim& dims) { bool valid = dims.size() == 2 || dims.size() == 3; - PADDLE_ENFORCE(valid, - phi::errors::InvalidArgument( - "the SparseCsrTensor only support 2-D Tensor.")); + PADDLE_ENFORCE( + valid, + phi::errors::InvalidArgument("the SparseCsrTensor only support 2-D or " + "3-D Tensor, but get %d-D Tensor", + dims.size())); } #define Check(non_zero_crows, non_zero_cols, non_zero_elements, dims) \ { \ diff --git a/paddle/phi/kernels/sparse/activation_grad_kernel.cc b/paddle/phi/kernels/sparse/activation_grad_kernel.cc index 628db10532da2..8962f8639244d 100644 --- a/paddle/phi/kernels/sparse/activation_grad_kernel.cc +++ b/paddle/phi/kernels/sparse/activation_grad_kernel.cc @@ -17,5 +17,21 @@ limitations under the License. */ #include "paddle/phi/kernels/activation_grad_kernel.h" #include "paddle/phi/kernels/sparse/utils.h" -DEFINE_AND_REGISTER_SPARSE_UNARY_GRAD_KERNEL(sparse_relu_grad, ReluGradKernel) -DEFINE_AND_REGISTER_SPARSE_UNARY_GRAD_KERNEL(sparse_sqrt_grad, SqrtGradKernel) +DEFINE_AND_REGISTER_SPARSE_UNARY_GRAD_KERNEL(relu_grad, ReluGradKernel) +DEFINE_AND_REGISTER_SPARSE_UNARY_GRAD_KERNEL(sqrt_grad, SqrtGradKernel) + +namespace phi { +namespace sparse { + +template +void SparseCooXXGrad(const Context& dev_ctx, + const SparseCooTensor& x, + SparseCooTensor* out) { +} +} // namespace sparse +} // namespace phi + +PD_REGISTER_KERNEL( + sparse_coo_xx_grad, CPU, ALL_LAYOUT, phi::sparse::SparseCooXXGrad, float, double) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); +} diff --git a/paddle/phi/kernels/sparse/activation_kernel.cc b/paddle/phi/kernels/sparse/activation_kernel.cc index c541b14d5ddba..7c8ca2eb959fa 100644 --- a/paddle/phi/kernels/sparse/activation_kernel.cc +++ b/paddle/phi/kernels/sparse/activation_kernel.cc @@ -16,5 +16,21 @@ limitations under the License. */ #include "paddle/phi/kernels/sparse/utils.h" -DEFINE_AND_REGISTER_SPARSE_UNARY_KERNEL(sparse_relu, ReluKernel) -DEFINE_AND_REGISTER_SPARSE_UNARY_KERNEL(sparse_sqrt, SqrtKernel) +DEFINE_AND_REGISTER_SPARSE_UNARY_KERNEL(relu, ReluKernel) +DEFINE_AND_REGISTER_SPARSE_UNARY_KERNEL(sqrt, SqrtKernel) + +namespace phi { +namespace sparse { + +template +void SparseCooXX(const Context& dev_ctx, + const SparseCooTensor& x, + SparseCooTensor* out) { +} +} // namespace sparse +} // namespace phi + +PD_REGISTER_KERNEL( + sparse_coo_xx, CPU, ALL_LAYOUT, phi::sparse::SparseCooXX, float, double) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); +} diff --git a/python/paddle/fluid/tests/unittests/test_sparse_activation_op.py b/python/paddle/fluid/tests/unittests/test_sparse_activation_op.py index fb82b76057d39..575475c4fd19e 100644 --- a/python/paddle/fluid/tests/unittests/test_sparse_activation_op.py +++ b/python/paddle/fluid/tests/unittests/test_sparse_activation_op.py @@ -44,18 +44,17 @@ def test_sparse_coo_sqrt(self): sparse_act_out = _C_ops.final_state_sparse_coo_sqrt(sparse_coo_x) correct_result = [2, np.sqrt(2), 4] actual_result = sparse_act_out.non_zero_elements().numpy() - assert np.array_equal(correct_result, actual_result) + assert np.allclose(correct_result, actual_result) def test_sparse_csr_sqrt(self): with _test_eager_guard(): x = [[0, 4, 0, 2], [0, 0, 0, 0], [0, 0, 16, 0]] dense_x = paddle.to_tensor(x, dtype='float32') - sparse_dim = 2 - sparse_coo_x = dense_x.to_sparse_csr(sparse_dim) + sparse_coo_x = dense_x.to_sparse_csr() sparse_act_out = _C_ops.final_state_sparse_csr_sqrt(sparse_coo_x) correct_result = [2, np.sqrt(2), 4] actual_result = sparse_act_out.non_zero_elements().numpy() - assert np.array_equal(correct_result, actual_result) + assert np.allclose(correct_result, actual_result) if __name__ == "__main__": From 7e5f10296525784223264d0762a86e16384ec6f8 Mon Sep 17 00:00:00 2001 From: tiancaishaonvjituizi <452565578@qq.com> Date: Wed, 20 Apr 2022 22:40:43 +0800 Subject: [PATCH 04/31] update relu for new sparse api --- python/paddle/sparse/functional/activation.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/python/paddle/sparse/functional/activation.py b/python/paddle/sparse/functional/activation.py index c0109bc4e2429..040b3b30ae0c2 100644 --- a/python/paddle/sparse/functional/activation.py +++ b/python/paddle/sparse/functional/activation.py @@ -47,7 +47,11 @@ def relu(x, name=None): """ assert in_dynamic_mode(), "Currently, Sparse API only support dynamic mode" - assert x.is_sparse_coo( - ), "Currently, sparse.relu only support the input of SparseCooTensor" - return _C_ops.final_state_sparse_relu(x) + if x.is_sparse_coo(): + return _C_ops.final_state_sparse_coo_relu(x) + elif x.is_sparse_csr(): + return _C_ops.final_state_sparse_csr_relu(x) + else: + raise ValueError("Currently, sparse.relu only support the input of SparseCooTensor or SparseCsrTensor") + From 71864fd5a9e1092229d61c45596fe7e36574f46a Mon Sep 17 00:00:00 2001 From: tiancaishaonvjituizi <452565578@qq.com> Date: Thu, 21 Apr 2022 13:39:22 +0800 Subject: [PATCH 05/31] update test, implement api, fix sqrt grad --- paddle/phi/kernels/sparse/utils.h | 30 +++---- .../unittests/test_sparse_activation_op.py | 82 +++++++++---------- python/paddle/sparse/functional/__init__.py | 3 +- python/paddle/sparse/functional/activation.py | 39 +++++++++ .../paddle/utils/code_gen/sparse_bw_api.yaml | 8 +- 5 files changed, 100 insertions(+), 62 deletions(-) diff --git a/paddle/phi/kernels/sparse/utils.h b/paddle/phi/kernels/sparse/utils.h index fc04f5487a790..a6b12d49beb91 100644 --- a/paddle/phi/kernels/sparse/utils.h +++ b/paddle/phi/kernels/sparse/utils.h @@ -60,52 +60,53 @@ \ template \ void SparseCoo##dense_kernel_func(const Context& dev_ctx, \ - const SparseCooTensor& x, \ + const SparseCooTensor& x_or_out, \ const SparseCooTensor& out_grad, \ SparseCooTensor* x_grad) { \ DenseTensor non_zero_indices = \ - phi::EmptyLike(dev_ctx, x.non_zero_indices()); \ + phi::EmptyLike(dev_ctx, x_or_out.non_zero_indices()); \ DenseTensor non_zero_elements = \ - phi::EmptyLike(dev_ctx, x.non_zero_elements()); \ + phi::EmptyLike(dev_ctx, x_or_out.non_zero_elements()); \ phi::Copy(dev_ctx, \ - x.non_zero_indices(), \ + x_or_out.non_zero_indices(), \ dev_ctx.GetPlace(), \ false, \ &non_zero_indices); \ phi::dense_kernel_func(dev_ctx, \ - x.non_zero_elements(), \ + x_or_out.non_zero_elements(), \ out_grad.non_zero_elements(), \ &non_zero_elements); \ - x_grad->SetMember(non_zero_indices, non_zero_elements, x.dims(), true); \ + x_grad->SetMember( \ + non_zero_indices, non_zero_elements, x_or_out.dims(), true); \ } \ \ template \ void SparseCsr##dense_kernel_func(const Context& dev_ctx, \ - const SparseCsrTensor& x, \ + const SparseCsrTensor& x_or_out, \ const SparseCsrTensor& out_grad, \ SparseCsrTensor* out) { \ DenseTensor non_zero_crows = \ - phi::EmptyLike(dev_ctx, x.non_zero_crows()); \ + phi::EmptyLike(dev_ctx, x_or_out.non_zero_crows()); \ DenseTensor non_zero_cols = \ - phi::EmptyLike(dev_ctx, x.non_zero_cols()); \ + phi::EmptyLike(dev_ctx, x_or_out.non_zero_cols()); \ DenseTensor non_zero_elements = \ - phi::EmptyLike(dev_ctx, x.non_zero_elements()); \ + phi::EmptyLike(dev_ctx, x_or_out.non_zero_elements()); \ phi::Copy(dev_ctx, \ - x.non_zero_crows(), \ + x_or_out.non_zero_crows(), \ dev_ctx.GetPlace(), \ false, \ &non_zero_crows); \ phi::Copy(dev_ctx, \ - x.non_zero_cols(), \ + x_or_out.non_zero_cols(), \ dev_ctx.GetPlace(), \ false, \ &non_zero_cols); \ phi::dense_kernel_func(dev_ctx, \ - x.non_zero_elements(), \ + x_or_out.non_zero_elements(), \ out_grad.non_zero_elements(), \ &non_zero_elements); \ out->SetMember( \ - non_zero_crows, non_zero_cols, non_zero_elements, x.dims()); \ + non_zero_crows, non_zero_cols, non_zero_elements, x_or_out.dims()); \ } \ } \ } @@ -167,4 +168,3 @@ dense_kernel_func) \ DEFINE_SPARSE_UNARY_GRAD_KERNEL(dense_kernel_func) \ REGISTER_SPARSE_UNARY_KERNEL(kernel_name, dense_kernel_func) - diff --git a/python/paddle/fluid/tests/unittests/test_sparse_activation_op.py b/python/paddle/fluid/tests/unittests/test_sparse_activation_op.py index 944d96f0d1e52..6dbeac1809acb 100644 --- a/python/paddle/fluid/tests/unittests/test_sparse_activation_op.py +++ b/python/paddle/fluid/tests/unittests/test_sparse_activation_op.py @@ -1,11 +1,11 @@ # Copyright (c) 2022 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. @@ -14,57 +14,55 @@ from __future__ import print_function import unittest +from typing import Union, Callable import numpy as np import paddle from paddle.fluid.framework import _test_eager_guard +from paddle import _C_ops class TestSparseActivation(unittest.TestCase): - def test_sparse_relu(self): + def compare_with_dense( + self, + x, + to_sparse: Callable[[paddle.Tensor], paddle.Tensor], + dense_func: Callable[[paddle.Tensor], paddle.Tensor], + sparse_func: Callable[[paddle.Tensor], paddle.Tensor], + test_gradient: bool, + ): + def tensor_equal(dense_tensor: paddle.Tensor, sparse_tensor: paddle.Tensor): + mask = ~np.isnan(dense_tensor.numpy()) + return np.array_equal(dense_tensor.numpy()[mask], sparse_tensor.to_dense().numpy()[mask]) + with _test_eager_guard(): - x = [[0, -1, 0, 2], [0, 0, -3, 0], [4, 5, 0, 0]] + dense_x = paddle.to_tensor(x, dtype="float32", stop_gradient=not test_gradient) - def dense_relu(x): - dense_x = paddle.to_tensor( - x, dtype='float32', stop_gradient=False) - dense_relu = paddle.nn.ReLU() - dense_out = dense_relu(dense_x) - dense_out.backward(dense_out) - return dense_out, dense_x.grad + sparse_x = to_sparse(dense_x) + sparse_out = sparse_func(sparse_x) - dense_x = paddle.to_tensor(x, dtype='float32', stop_gradient=False) - sparse_dim = 2 - sparse_x = dense_x.to_sparse_coo(sparse_dim) - sparse_relu = paddle.sparse.ReLU() - sparse_out = sparse_relu(sparse_x) - sparse_out.backward(sparse_out) + dense_x = paddle.to_tensor(x, dtype="float32", stop_gradient=not test_gradient) + dense_out = dense_func(dense_x) - dense_out, dense_x_grad = dense_relu(x) - assert np.array_equal(dense_out.numpy(), - sparse_out.to_dense().numpy()) - assert np.array_equal(dense_x_grad.numpy(), - sparse_x.grad.to_dense().numpy()) + assert tensor_equal(dense_out, sparse_out) - def test_sparse_coo_sqrt(self): - with _test_eager_guard(): - x = [[0, 4, 0, 2], [0, 0, 16, 0]] - dense_x = paddle.to_tensor(x, dtype='float32') - sparse_dim = 2 - sparse_coo_x = dense_x.to_sparse_coo(sparse_dim) - sparse_act_out = _C_ops.final_state_sparse_coo_sqrt(sparse_coo_x) - correct_result = [2, np.sqrt(2), 4] - actual_result = sparse_act_out.non_zero_elements().numpy() - assert np.allclose(correct_result, actual_result) + if test_gradient: + dense_out.backward(dense_out) + sparse_out.backward(sparse_out) + assert tensor_equal( + dense_x.grad, sparse_x.grad + ) - def test_sparse_csr_sqrt(self): - with _test_eager_guard(): - x = [[0, 4, 0, 2], [0, 0, 0, 0], [0, 0, 16, 0]] - dense_x = paddle.to_tensor(x, dtype='float32') - sparse_coo_x = dense_x.to_sparse_csr() - sparse_act_out = _C_ops.final_state_sparse_csr_sqrt(sparse_coo_x) - correct_result = [2, np.sqrt(2), 4] - actual_result = sparse_act_out.non_zero_elements().numpy() - assert np.allclose(correct_result, actual_result) + def test_sparse_relu(self): + x = [[0, -1, 0, 2], [0, 0, -3, 0], [4, 5, 0, 0]] + sparse_dim = 2 + self.compare_with_dense(x, lambda x: x.to_sparse_coo(sparse_dim), lambda x: paddle.nn.ReLU()(x), lambda x: paddle.sparse.ReLU()(x), True) + self.compare_with_dense(x, lambda x: x.to_sparse_csr(), lambda x: paddle.nn.ReLU()(x), lambda x: paddle.sparse.ReLU()(x), False) + + def test_sparse_sqrt(self): + x = [[0, 16, 0, 0], [0, 0, 0, 0], [0, 4, 2, 0]] + sparse_dim = 2 + self.compare_with_dense(x, lambda x: x.to_sparse_coo(sparse_dim), paddle.sqrt, paddle.sparse.functional.sqrt, True) + self.compare_with_dense(x, lambda x: x.to_sparse_csr(), paddle.sqrt, paddle.sparse.functional.sqrt, False) if __name__ == "__main__": diff --git a/python/paddle/sparse/functional/__init__.py b/python/paddle/sparse/functional/__init__.py index 93c3ccda4a613..a970c0acaeb2e 100644 --- a/python/paddle/sparse/functional/__init__.py +++ b/python/paddle/sparse/functional/__init__.py @@ -13,7 +13,8 @@ # limitations under the License. from .activation import relu # noqa: F401 +from .activation import sqrt # noqa: F401 from .conv import conv3d # noqa: F401 from .conv import subm_conv3d # noqa: F401 -__all__ = ['relu', 'conv3d', 'subm_conv3d'] +__all__ = ['relu', 'conv3d', 'subm_conv3d', 'sqrt'] diff --git a/python/paddle/sparse/functional/activation.py b/python/paddle/sparse/functional/activation.py index 040b3b30ae0c2..069d4f6aa5a95 100644 --- a/python/paddle/sparse/functional/activation.py +++ b/python/paddle/sparse/functional/activation.py @@ -55,3 +55,42 @@ def relu(x, name=None): else: raise ValueError("Currently, sparse.relu only support the input of SparseCooTensor or SparseCsrTensor") + +def sqrt(x, name=None): + """ + sparse sqrt activation. + + .. math:: + + out = sqrt(x) + + Parameters: + x (Tensor): The input Sparse Tensor with data type float32, float64. + name (str, optional): Name for the operation (optional, default is None). + For more information, please refer to :ref:`api_guide_Name`. + + Returns: + A Sparse Tensor with the same data type and shape as ``x`` . + + Examples: + .. code-block:: python + + import paddle + import numpy as np + from paddle.fluid.framework import _test_eager_guard + + with _test_eager_guard(): + dense_x = paddle.to_tensor(np.array([-2, 0, 1]).astype('float32')) + sparse_x = dense_x.to_sparse_coo(1) + out = paddle.sparse.functional.sqrt(sparse_x) + """ + + assert in_dynamic_mode(), "Currently, Sparse API only support dynamic mode" + + if x.is_sparse_coo(): + return _C_ops.final_state_sparse_coo_sqrt(x) + elif x.is_sparse_csr(): + return _C_ops.final_state_sparse_csr_sqrt(x) + else: + raise ValueError("Currently, sparse.sqrt only support the input of SparseCooTensor or SparseCsrTensor") + diff --git a/python/paddle/utils/code_gen/sparse_bw_api.yaml b/python/paddle/utils/code_gen/sparse_bw_api.yaml index 64af586cb8265..5868256359652 100644 --- a/python/paddle/utils/code_gen/sparse_bw_api.yaml +++ b/python/paddle/utils/code_gen/sparse_bw_api.yaml @@ -34,28 +34,28 @@ - backward_api : sparse_coo_relu_grad forward : sparse_coo_relu(Tensor x) -> Tensor(out@SparseCooTensor) - args : (Tensor x, Tensor out_grad) + args : (Tensor out, Tensor out_grad) output : Tensor(x_grad@SparseCooTensor) kernel : func : sparse_coo_relu_grad - backward_api : sparse_csr_relu_grad forward : sparse_csr_relu(Tensor x) -> Tensor(out@SparseCsrTensor) - args : (Tensor x, Tensor out_grad) + args : (Tensor out, Tensor out_grad) output : Tensor(x_grad@SparseCsrTensor) kernel : func : sparse_csr_relu_grad - backward_api : sparse_coo_sqrt_grad forward : sparse_coo_sqrt(Tensor x) -> Tensor(out@SparseCooTensor) - args : (Tensor x, Tensor out_grad) + args : (Tensor out, Tensor out_grad) output : Tensor(x_grad@SparseCooTensor) kernel : func : sparse_coo_sqrt_grad - backward_api : sparse_csr_sqrt_grad forward : sparse_csr_sqrt(Tensor x) -> Tensor(out@SparseCsrTensor) - args : (Tensor x, Tensor out_grad) + args : (Tensor out, Tensor out_grad) output : Tensor(x_grad@SparseCsrTensor) kernel : func : sparse_csr_sqrt_grad From a99a5baf55cedb05e6d633af34d1a3b00410271a Mon Sep 17 00:00:00 2001 From: tiancaishaonvjituizi <452565578@qq.com> Date: Thu, 21 Apr 2022 14:23:50 +0800 Subject: [PATCH 06/31] manually register relu and relu_grad kernel to bypass the restriction --- .../kernels/sparse/activation_grad_kernel.cc | 54 ++++++++++++++----- .../phi/kernels/sparse/activation_kernel.cc | 51 ++++++++++++++---- 2 files changed, 81 insertions(+), 24 deletions(-) diff --git a/paddle/phi/kernels/sparse/activation_grad_kernel.cc b/paddle/phi/kernels/sparse/activation_grad_kernel.cc index 8962f8639244d..f02ad36ccde3d 100644 --- a/paddle/phi/kernels/sparse/activation_grad_kernel.cc +++ b/paddle/phi/kernels/sparse/activation_grad_kernel.cc @@ -17,21 +17,49 @@ limitations under the License. */ #include "paddle/phi/kernels/activation_grad_kernel.h" #include "paddle/phi/kernels/sparse/utils.h" -DEFINE_AND_REGISTER_SPARSE_UNARY_GRAD_KERNEL(relu_grad, ReluGradKernel) DEFINE_AND_REGISTER_SPARSE_UNARY_GRAD_KERNEL(sqrt_grad, SqrtGradKernel) -namespace phi { -namespace sparse { - -template -void SparseCooXXGrad(const Context& dev_ctx, - const SparseCooTensor& x, - SparseCooTensor* out) { +// NOTE: the following code is to bypass the restriction of Paddle +// kernel registration mechanism. Do NOT refactor them unless you +// know what you are doing. +// If you want to implement any new kernel, please follow `sqrt_grad` above +// instead of `relu_grad` following +DEFINE_SPARSE_UNARY_GRAD_KERNEL(ReluGradKernel) +PD_REGISTER_KERNEL(sparse_coo_relu_grad, + CPU, + ALL_LAYOUT, + phi::sparse::SparseCooReluGradKernel, + float, + double) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); } -} // namespace sparse -} // namespace phi - -PD_REGISTER_KERNEL( - sparse_coo_xx_grad, CPU, ALL_LAYOUT, phi::sparse::SparseCooXXGrad, float, double) { +PD_REGISTER_KERNEL(sparse_csr_relu_grad, + CPU, + ALL_LAYOUT, + phi::sparse::SparseCsrReluGradKernel, + float, + double) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); +} +#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) +PD_REGISTER_KERNEL(sparse_coo_relu_grad, + GPU, + ALL_LAYOUT, + phi::sparse::SparseCooReluGradKernel, + float, + double, + phi::dtype::float16) { kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); } + +PD_REGISTER_KERNEL(sparse_csr_relu_grad, + GPU, + ALL_LAYOUT, + phi::sparse::SparseCsrReluGradKernel, + float, + double, + phi::dtype::float16) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); +} +#endif + diff --git a/paddle/phi/kernels/sparse/activation_kernel.cc b/paddle/phi/kernels/sparse/activation_kernel.cc index 7c8ca2eb959fa..1862a846035d1 100644 --- a/paddle/phi/kernels/sparse/activation_kernel.cc +++ b/paddle/phi/kernels/sparse/activation_kernel.cc @@ -16,21 +16,50 @@ limitations under the License. */ #include "paddle/phi/kernels/sparse/utils.h" -DEFINE_AND_REGISTER_SPARSE_UNARY_KERNEL(relu, ReluKernel) DEFINE_AND_REGISTER_SPARSE_UNARY_KERNEL(sqrt, SqrtKernel) -namespace phi { -namespace sparse { +// NOTE: the following code is to bypass the restriction of Paddle +// kernel registration mechanism. Do NOT refactor them unless you +// know what you are doing. +// If you want to implement any new kernel, please follow `sqrt` above +// instead of `relu` following +DEFINE_SPARSE_UNARY_KERNEL(ReluKernel) -template -void SparseCooXX(const Context& dev_ctx, - const SparseCooTensor& x, - SparseCooTensor* out) { +PD_REGISTER_KERNEL(sparse_coo_relu, + CPU, + ALL_LAYOUT, + phi::sparse::SparseCooReluKernel, + float, + double) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); +} +PD_REGISTER_KERNEL(sparse_csr_relu, + CPU, + ALL_LAYOUT, + phi::sparse::SparseCsrReluKernel, + float, + double) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); } -} // namespace sparse -} // namespace phi -PD_REGISTER_KERNEL( - sparse_coo_xx, CPU, ALL_LAYOUT, phi::sparse::SparseCooXX, float, double) { +#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) +PD_REGISTER_KERNEL(sparse_coo_relu, + GPU, + ALL_LAYOUT, + phi::sparse::SparseCooReluKernel, + float, + double, + phi::dtype::float16) { kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); } + +PD_REGISTER_KERNEL(sparse_csr_relu, + GPU, + ALL_LAYOUT, + phi::sparse::SparseCsrReluKernel, + float, + double, + phi::dtype::float16) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); +} +#endif From 95aa0b3ce2061efe38810421b6f8f473487bd4d7 Mon Sep 17 00:00:00 2001 From: tiancaishaonvjituizi <452565578@qq.com> Date: Thu, 21 Apr 2022 15:06:29 +0800 Subject: [PATCH 07/31] polish sqrt docs --- python/paddle/sparse/functional/activation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/sparse/functional/activation.py b/python/paddle/sparse/functional/activation.py index 069d4f6aa5a95..a5a81371e845a 100644 --- a/python/paddle/sparse/functional/activation.py +++ b/python/paddle/sparse/functional/activation.py @@ -80,7 +80,7 @@ def sqrt(x, name=None): from paddle.fluid.framework import _test_eager_guard with _test_eager_guard(): - dense_x = paddle.to_tensor(np.array([-2, 0, 1]).astype('float32')) + dense_x = paddle.to_tensor(np.array([4, 0, 1]).astype('float32')) sparse_x = dense_x.to_sparse_coo(1) out = paddle.sparse.functional.sqrt(sparse_x) """ From f706dea2ac317872846b2f3f612261df617d885d Mon Sep 17 00:00:00 2001 From: tiancaishaonvjituizi <452565578@qq.com> Date: Thu, 21 Apr 2022 15:12:08 +0800 Subject: [PATCH 08/31] reformat --- .../kernels/sparse/activation_grad_kernel.cc | 1 - .../unittests/test_sparse_activation_op.py | 50 +++++++++++++++---- python/paddle/sparse/functional/activation.py | 8 ++- 3 files changed, 45 insertions(+), 14 deletions(-) diff --git a/paddle/phi/kernels/sparse/activation_grad_kernel.cc b/paddle/phi/kernels/sparse/activation_grad_kernel.cc index f02ad36ccde3d..dbcb2718ab98c 100644 --- a/paddle/phi/kernels/sparse/activation_grad_kernel.cc +++ b/paddle/phi/kernels/sparse/activation_grad_kernel.cc @@ -62,4 +62,3 @@ PD_REGISTER_KERNEL(sparse_csr_relu_grad, kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); } #endif - diff --git a/python/paddle/fluid/tests/unittests/test_sparse_activation_op.py b/python/paddle/fluid/tests/unittests/test_sparse_activation_op.py index 6dbeac1809acb..40d254d9e2fe4 100644 --- a/python/paddle/fluid/tests/unittests/test_sparse_activation_op.py +++ b/python/paddle/fluid/tests/unittests/test_sparse_activation_op.py @@ -32,15 +32,21 @@ def compare_with_dense( ): def tensor_equal(dense_tensor: paddle.Tensor, sparse_tensor: paddle.Tensor): mask = ~np.isnan(dense_tensor.numpy()) - return np.array_equal(dense_tensor.numpy()[mask], sparse_tensor.to_dense().numpy()[mask]) + return np.array_equal( + dense_tensor.numpy()[mask], sparse_tensor.to_dense().numpy()[mask] + ) with _test_eager_guard(): - dense_x = paddle.to_tensor(x, dtype="float32", stop_gradient=not test_gradient) + dense_x = paddle.to_tensor( + x, dtype="float32", stop_gradient=not test_gradient + ) - sparse_x = to_sparse(dense_x) + sparse_x = to_sparse(dense_x) sparse_out = sparse_func(sparse_x) - dense_x = paddle.to_tensor(x, dtype="float32", stop_gradient=not test_gradient) + dense_x = paddle.to_tensor( + x, dtype="float32", stop_gradient=not test_gradient + ) dense_out = dense_func(dense_x) assert tensor_equal(dense_out, sparse_out) @@ -48,21 +54,43 @@ def tensor_equal(dense_tensor: paddle.Tensor, sparse_tensor: paddle.Tensor): if test_gradient: dense_out.backward(dense_out) sparse_out.backward(sparse_out) - assert tensor_equal( - dense_x.grad, sparse_x.grad - ) + assert tensor_equal(dense_x.grad, sparse_x.grad) def test_sparse_relu(self): x = [[0, -1, 0, 2], [0, 0, -3, 0], [4, 5, 0, 0]] sparse_dim = 2 - self.compare_with_dense(x, lambda x: x.to_sparse_coo(sparse_dim), lambda x: paddle.nn.ReLU()(x), lambda x: paddle.sparse.ReLU()(x), True) - self.compare_with_dense(x, lambda x: x.to_sparse_csr(), lambda x: paddle.nn.ReLU()(x), lambda x: paddle.sparse.ReLU()(x), False) + self.compare_with_dense( + x, + lambda x: x.to_sparse_coo(sparse_dim), + lambda x: paddle.nn.ReLU()(x), + lambda x: paddle.sparse.ReLU()(x), + True, + ) + self.compare_with_dense( + x, + lambda x: x.to_sparse_csr(), + lambda x: paddle.nn.ReLU()(x), + lambda x: paddle.sparse.ReLU()(x), + False, + ) def test_sparse_sqrt(self): x = [[0, 16, 0, 0], [0, 0, 0, 0], [0, 4, 2, 0]] sparse_dim = 2 - self.compare_with_dense(x, lambda x: x.to_sparse_coo(sparse_dim), paddle.sqrt, paddle.sparse.functional.sqrt, True) - self.compare_with_dense(x, lambda x: x.to_sparse_csr(), paddle.sqrt, paddle.sparse.functional.sqrt, False) + self.compare_with_dense( + x, + lambda x: x.to_sparse_coo(sparse_dim), + paddle.sqrt, + paddle.sparse.functional.sqrt, + True, + ) + self.compare_with_dense( + x, + lambda x: x.to_sparse_csr(), + paddle.sqrt, + paddle.sparse.functional.sqrt, + False, + ) if __name__ == "__main__": diff --git a/python/paddle/sparse/functional/activation.py b/python/paddle/sparse/functional/activation.py index a5a81371e845a..5d37040367785 100644 --- a/python/paddle/sparse/functional/activation.py +++ b/python/paddle/sparse/functional/activation.py @@ -53,7 +53,9 @@ def relu(x, name=None): elif x.is_sparse_csr(): return _C_ops.final_state_sparse_csr_relu(x) else: - raise ValueError("Currently, sparse.relu only support the input of SparseCooTensor or SparseCsrTensor") + raise ValueError( + "Currently, sparse.relu only support the input of SparseCooTensor or SparseCsrTensor" + ) def sqrt(x, name=None): @@ -92,5 +94,7 @@ def sqrt(x, name=None): elif x.is_sparse_csr(): return _C_ops.final_state_sparse_csr_sqrt(x) else: - raise ValueError("Currently, sparse.sqrt only support the input of SparseCooTensor or SparseCsrTensor") + raise ValueError( + "Currently, sparse.sqrt only support the input of SparseCooTensor or SparseCsrTensor" + ) From d898df7c64b1fc6ff12282b9dfe8db2e2149a7b1 Mon Sep 17 00:00:00 2001 From: tiancaishaonvjituizi <452565578@qq.com> Date: Thu, 21 Apr 2022 15:19:00 +0800 Subject: [PATCH 09/31] polish docs --- paddle/phi/kernels/sparse/activation_grad_kernel.cc | 5 +++-- paddle/phi/kernels/sparse/activation_kernel.cc | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/paddle/phi/kernels/sparse/activation_grad_kernel.cc b/paddle/phi/kernels/sparse/activation_grad_kernel.cc index dbcb2718ab98c..1767211e7b65d 100644 --- a/paddle/phi/kernels/sparse/activation_grad_kernel.cc +++ b/paddle/phi/kernels/sparse/activation_grad_kernel.cc @@ -22,9 +22,10 @@ DEFINE_AND_REGISTER_SPARSE_UNARY_GRAD_KERNEL(sqrt_grad, SqrtGradKernel) // NOTE: the following code is to bypass the restriction of Paddle // kernel registration mechanism. Do NOT refactor them unless you // know what you are doing. -// If you want to implement any new kernel, please follow `sqrt_grad` above -// instead of `relu_grad` following +// If you want to implement any new kernel, please follow the above `sqrt_grad`, +// do NOT follow the following `relu_grad`. DEFINE_SPARSE_UNARY_GRAD_KERNEL(ReluGradKernel) + PD_REGISTER_KERNEL(sparse_coo_relu_grad, CPU, ALL_LAYOUT, diff --git a/paddle/phi/kernels/sparse/activation_kernel.cc b/paddle/phi/kernels/sparse/activation_kernel.cc index 1862a846035d1..42ae23544933b 100644 --- a/paddle/phi/kernels/sparse/activation_kernel.cc +++ b/paddle/phi/kernels/sparse/activation_kernel.cc @@ -21,8 +21,8 @@ DEFINE_AND_REGISTER_SPARSE_UNARY_KERNEL(sqrt, SqrtKernel) // NOTE: the following code is to bypass the restriction of Paddle // kernel registration mechanism. Do NOT refactor them unless you // know what you are doing. -// If you want to implement any new kernel, please follow `sqrt` above -// instead of `relu` following +// If you want to implement any new kernel, please follow the above `sqrt`, +// do NOT follow the following `relu`. DEFINE_SPARSE_UNARY_KERNEL(ReluKernel) PD_REGISTER_KERNEL(sparse_coo_relu, From b770f418009679d8c9dc779cffa53fa7a4375cc6 Mon Sep 17 00:00:00 2001 From: tiancaishaonvjituizi <452565578@qq.com> Date: Thu, 21 Apr 2022 15:26:37 +0800 Subject: [PATCH 10/31] remove csr backward api --- python/paddle/utils/code_gen/sparse_api.yaml | 2 -- python/paddle/utils/code_gen/sparse_bw_api.yaml | 14 -------------- 2 files changed, 16 deletions(-) diff --git a/python/paddle/utils/code_gen/sparse_api.yaml b/python/paddle/utils/code_gen/sparse_api.yaml index 26db3c6b61216..8bf148ddd452e 100644 --- a/python/paddle/utils/code_gen/sparse_api.yaml +++ b/python/paddle/utils/code_gen/sparse_api.yaml @@ -57,7 +57,6 @@ kernel : func : sparse_csr_relu layout : x - backward : sparse_csr_relu_grad - api : coo_sqrt args : (Tensor x) @@ -73,7 +72,6 @@ kernel : func : sparse_csr_sqrt layout : x - backward : sparse_csr_sqrt_grad - api : to_dense args : (Tensor x) diff --git a/python/paddle/utils/code_gen/sparse_bw_api.yaml b/python/paddle/utils/code_gen/sparse_bw_api.yaml index 5868256359652..ff9d5d458491d 100644 --- a/python/paddle/utils/code_gen/sparse_bw_api.yaml +++ b/python/paddle/utils/code_gen/sparse_bw_api.yaml @@ -39,23 +39,9 @@ kernel : func : sparse_coo_relu_grad -- backward_api : sparse_csr_relu_grad - forward : sparse_csr_relu(Tensor x) -> Tensor(out@SparseCsrTensor) - args : (Tensor out, Tensor out_grad) - output : Tensor(x_grad@SparseCsrTensor) - kernel : - func : sparse_csr_relu_grad - - backward_api : sparse_coo_sqrt_grad forward : sparse_coo_sqrt(Tensor x) -> Tensor(out@SparseCooTensor) args : (Tensor out, Tensor out_grad) output : Tensor(x_grad@SparseCooTensor) kernel : func : sparse_coo_sqrt_grad - -- backward_api : sparse_csr_sqrt_grad - forward : sparse_csr_sqrt(Tensor x) -> Tensor(out@SparseCsrTensor) - args : (Tensor out, Tensor out_grad) - output : Tensor(x_grad@SparseCsrTensor) - kernel : - func : sparse_csr_sqrt_grad From f92e8cd8c35be6ae9d209c3181f2b8e084d2a5c8 Mon Sep 17 00:00:00 2001 From: tiancaishaonvjituizi <452565578@qq.com> Date: Thu, 21 Apr 2022 17:47:00 +0800 Subject: [PATCH 11/31] fix test compile error Signed-off-by: tiancaishaonvjituizi <452565578@qq.com> --- paddle/phi/tests/kernels/test_sparse_activation_dev_api.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paddle/phi/tests/kernels/test_sparse_activation_dev_api.cc b/paddle/phi/tests/kernels/test_sparse_activation_dev_api.cc index 43640da270aad..f86170980ffbb 100644 --- a/paddle/phi/tests/kernels/test_sparse_activation_dev_api.cc +++ b/paddle/phi/tests/kernels/test_sparse_activation_dev_api.cc @@ -70,7 +70,7 @@ TEST(DEV_API, sparse_relu) { SparseCooTensor sparse_out_grad( sparse_coo.non_zero_indices(), dense_out, {3, 4}); - sparse::SparseReluGradKernel( + sparse::SparseCooReluGradKernel( dev_ctx_cpu, sparse_coo, sparse_out_grad, &sparse_grad_x); cmp = memcmp(dense_grad_x.data(), From 394ce5e1eee21494af7f5aa2cf0b0544fbf09f68 Mon Sep 17 00:00:00 2001 From: tiancaishaonvjituizi <452565578@qq.com> Date: Thu, 21 Apr 2022 20:21:59 +0800 Subject: [PATCH 12/31] use allclose instead of array_equal --- .../fluid/tests/unittests/test_sparse_activation_op.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_sparse_activation_op.py b/python/paddle/fluid/tests/unittests/test_sparse_activation_op.py index 40d254d9e2fe4..c3312199fff17 100644 --- a/python/paddle/fluid/tests/unittests/test_sparse_activation_op.py +++ b/python/paddle/fluid/tests/unittests/test_sparse_activation_op.py @@ -30,9 +30,9 @@ def compare_with_dense( sparse_func: Callable[[paddle.Tensor], paddle.Tensor], test_gradient: bool, ): - def tensor_equal(dense_tensor: paddle.Tensor, sparse_tensor: paddle.Tensor): + def tensor_allclose(dense_tensor: paddle.Tensor, sparse_tensor: paddle.Tensor): mask = ~np.isnan(dense_tensor.numpy()) - return np.array_equal( + return np.allclose( dense_tensor.numpy()[mask], sparse_tensor.to_dense().numpy()[mask] ) @@ -49,12 +49,12 @@ def tensor_equal(dense_tensor: paddle.Tensor, sparse_tensor: paddle.Tensor): ) dense_out = dense_func(dense_x) - assert tensor_equal(dense_out, sparse_out) + assert tensor_allclose(dense_out, sparse_out) if test_gradient: dense_out.backward(dense_out) sparse_out.backward(sparse_out) - assert tensor_equal(dense_x.grad, sparse_x.grad) + assert tensor_allclose(dense_x.grad, sparse_x.grad) def test_sparse_relu(self): x = [[0, -1, 0, 2], [0, 0, -3, 0], [4, 5, 0, 0]] From c577f461f2b5e3ab6fe61a9d6b0395e3b1e490b8 Mon Sep 17 00:00:00 2001 From: tiancaishaonvjituizi <452565578@qq.com> Date: Thu, 21 Apr 2022 22:23:37 +0800 Subject: [PATCH 13/31] move sqrt to math_kernel.cc, implement sin and tanh --- .../kernels/sparse/activation_grad_kernel.cc | 6 +- .../kernels/sparse/activation_grad_kernel.h | 21 +---- .../phi/kernels/sparse/activation_kernel.cc | 6 +- paddle/phi/kernels/sparse/activation_kernel.h | 23 +---- paddle/phi/kernels/sparse/math_grad_kernel.cc | 51 +++++++++++ paddle/phi/kernels/sparse/math_grad_kernel.h | 12 +++ paddle/phi/kernels/sparse/math_kernel.cc | 50 +++++++++++ paddle/phi/kernels/sparse/math_kernel.h | 25 ++++++ paddle/phi/kernels/sparse/utils.h | 24 +++++ ...tivation_op.py => test_sparse_unary_op.py} | 40 ++++++++- python/paddle/sparse/functional/__init__.py | 6 +- python/paddle/sparse/functional/activation.py | 40 +++++++++ python/paddle/sparse/functional/math.py | 87 +++++++++++++++++++ python/paddle/utils/code_gen/sparse_api.yaml | 30 +++++++ .../paddle/utils/code_gen/sparse_bw_api.yaml | 14 +++ 15 files changed, 385 insertions(+), 50 deletions(-) create mode 100644 paddle/phi/kernels/sparse/math_grad_kernel.cc create mode 100644 paddle/phi/kernels/sparse/math_grad_kernel.h create mode 100644 paddle/phi/kernels/sparse/math_kernel.cc create mode 100644 paddle/phi/kernels/sparse/math_kernel.h rename python/paddle/fluid/tests/unittests/{test_sparse_activation_op.py => test_sparse_unary_op.py} (72%) create mode 100644 python/paddle/sparse/functional/math.py diff --git a/paddle/phi/kernels/sparse/activation_grad_kernel.cc b/paddle/phi/kernels/sparse/activation_grad_kernel.cc index 1767211e7b65d..3c5aebb284ab2 100644 --- a/paddle/phi/kernels/sparse/activation_grad_kernel.cc +++ b/paddle/phi/kernels/sparse/activation_grad_kernel.cc @@ -17,13 +17,13 @@ limitations under the License. */ #include "paddle/phi/kernels/activation_grad_kernel.h" #include "paddle/phi/kernels/sparse/utils.h" -DEFINE_AND_REGISTER_SPARSE_UNARY_GRAD_KERNEL(sqrt_grad, SqrtGradKernel) +DEFINE_AND_REGISTER_SPARSE_UNARY_GRAD_KERNEL(tanh_grad, TanhGradKernel) // NOTE: the following code is to bypass the restriction of Paddle // kernel registration mechanism. Do NOT refactor them unless you // know what you are doing. -// If you want to implement any new kernel, please follow the above `sqrt_grad`, -// do NOT follow the following `relu_grad`. +// If you want to implement any new kernel, please follow the above +// `sigmoid_grad`, do NOT follow the following `relu_grad`. DEFINE_SPARSE_UNARY_GRAD_KERNEL(ReluGradKernel) PD_REGISTER_KERNEL(sparse_coo_relu_grad, diff --git a/paddle/phi/kernels/sparse/activation_grad_kernel.h b/paddle/phi/kernels/sparse/activation_grad_kernel.h index 0c7c448bf8fb9..f9e5bc3c37fc5 100644 --- a/paddle/phi/kernels/sparse/activation_grad_kernel.h +++ b/paddle/phi/kernels/sparse/activation_grad_kernel.h @@ -14,29 +14,12 @@ limitations under the License. */ #pragma once -#include "paddle/phi/core/sparse_coo_tensor.h" -#include "paddle/phi/core/sparse_csr_tensor.h" +#include "paddle/phi/kernels/sparse/utils.h" namespace phi { namespace sparse { -#define DECLARE_SPARSE_ACTIVATION_GRAD_KERNEL(name) \ - template \ - void SparseCoo##name##GradKernel(const Context& dev_ctx, \ - const SparseCooTensor& x, \ - const SparseCooTensor& out_grad, \ - SparseCooTensor* x_grad); \ - \ - template \ - void SparseCsr##name##GradKernel(const Context& dev_ctx, \ - const SparseCsrTensor& x, \ - const SparseCsrTensor& out_grad, \ - SparseCsrTensor* x_grad); - -DECLARE_SPARSE_ACTIVATION_GRAD_KERNEL(Relu) -DECLARE_SPARSE_ACTIVATION_GRAD_KERNEL(Sqrt) - -#undef DECLARE_SPARSE_ACTIVATION_GRAD_KERNEL +DECLARE_SPARSE_UNARY_GRAD_KERNEL(Relu) } // namespace sparse } // namespace phi diff --git a/paddle/phi/kernels/sparse/activation_kernel.cc b/paddle/phi/kernels/sparse/activation_kernel.cc index 42ae23544933b..f67d6533adc20 100644 --- a/paddle/phi/kernels/sparse/activation_kernel.cc +++ b/paddle/phi/kernels/sparse/activation_kernel.cc @@ -16,13 +16,13 @@ limitations under the License. */ #include "paddle/phi/kernels/sparse/utils.h" -DEFINE_AND_REGISTER_SPARSE_UNARY_KERNEL(sqrt, SqrtKernel) +DEFINE_AND_REGISTER_SPARSE_UNARY_KERNEL(tanh, TanhKernel) // NOTE: the following code is to bypass the restriction of Paddle // kernel registration mechanism. Do NOT refactor them unless you // know what you are doing. -// If you want to implement any new kernel, please follow the above `sqrt`, -// do NOT follow the following `relu`. +// If you want to implement any new kernel, please follow the above +// `tanh`, do NOT follow the following `relu`. DEFINE_SPARSE_UNARY_KERNEL(ReluKernel) PD_REGISTER_KERNEL(sparse_coo_relu, diff --git a/paddle/phi/kernels/sparse/activation_kernel.h b/paddle/phi/kernels/sparse/activation_kernel.h index 034b84c6f2186..03622f5c9f77d 100644 --- a/paddle/phi/kernels/sparse/activation_kernel.h +++ b/paddle/phi/kernels/sparse/activation_kernel.h @@ -19,23 +19,12 @@ limitations under the License. */ #include "paddle/phi/core/sparse_csr_tensor.h" #include "paddle/phi/kernels/activation_kernel.h" #include "paddle/phi/kernels/empty_kernel.h" +#include "paddle/phi/kernels/sparse/utils.h" namespace phi { namespace sparse { -#define DECLARE_SPARSE_ACTIVATION_KERNEL(name) \ - template \ - void SparseCoo##name##Kernel( \ - const Context& dev_ctx, const SparseCooTensor& x, SparseCooTensor* out); \ - \ - template \ - void SparseCsr##name##Kernel( \ - const Context& dev_ctx, const SparseCsrTensor& x, SparseCsrTensor* out); - -DECLARE_SPARSE_ACTIVATION_KERNEL(Relu) -DECLARE_SPARSE_ACTIVATION_KERNEL(Sqrt) - -#undef DECLARE_SPARSE_ACTIVATION_KERNEL +DECLARE_SPARSE_UNARY_KERNEL(Relu) template SparseCooTensor SparseRelu(const Context& dev_ctx, const SparseCooTensor& x) { @@ -45,13 +34,5 @@ SparseCooTensor SparseRelu(const Context& dev_ctx, const SparseCooTensor& x) { return coo; } -template -SparseCooTensor SparseSqrt(const Context& dev_ctx, const SparseCooTensor& x) { - DenseTensor indices, values; - SparseCooTensor coo(indices, values, x.dims()); - SparseCooSqrtKernel(dev_ctx, x, &coo); - return coo; -} - } // namespace sparse } // namespace phi diff --git a/paddle/phi/kernels/sparse/math_grad_kernel.cc b/paddle/phi/kernels/sparse/math_grad_kernel.cc new file mode 100644 index 0000000000000..f0ff5aa5eced6 --- /dev/null +++ b/paddle/phi/kernels/sparse/math_grad_kernel.cc @@ -0,0 +1,51 @@ +#include "paddle/phi/kernels/sparse/math_grad_kernel.h" + +#include "paddle/phi/kernels/activation_grad_kernel.h" +#include "paddle/phi/kernels/sparse/utils.h" + +DEFINE_AND_REGISTER_SPARSE_UNARY_GRAD_KERNEL(sin_grad, SinGradKernel) + +// NOTE: the following code is to bypass the restriction of Paddle +// kernel registration mechanism. Do NOT refactor them unless you +// know what you are doing. +// If you want to implement any new kernel, please follow the above +// `log_grad`, do NOT follow the following `sqrt_grad`. +DEFINE_SPARSE_UNARY_GRAD_KERNEL(SqrtGradKernel) + +PD_REGISTER_KERNEL(sparse_coo_sqrt_grad, + CPU, + ALL_LAYOUT, + phi::sparse::SparseCooSqrtGradKernel, + float, + double) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); +} +PD_REGISTER_KERNEL(sparse_csr_sqrt_grad, + CPU, + ALL_LAYOUT, + phi::sparse::SparseCsrSqrtGradKernel, + float, + double) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); +} +#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) +PD_REGISTER_KERNEL(sparse_coo_sqrt_grad, + GPU, + ALL_LAYOUT, + phi::sparse::SparseCooSqrtGradKernel, + float, + double, + phi::dtype::float16) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); +} + +PD_REGISTER_KERNEL(sparse_csr_sqrt_grad, + GPU, + ALL_LAYOUT, + phi::sparse::SparseCsrSqrtGradKernel, + float, + double, + phi::dtype::float16) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); +} +#endif diff --git a/paddle/phi/kernels/sparse/math_grad_kernel.h b/paddle/phi/kernels/sparse/math_grad_kernel.h new file mode 100644 index 0000000000000..52c0cf993ed15 --- /dev/null +++ b/paddle/phi/kernels/sparse/math_grad_kernel.h @@ -0,0 +1,12 @@ +#pragma once + +#include "paddle/phi/kernels/sparse/utils.h" + +namespace phi { +namespace sparse { + +DECLARE_SPARSE_UNARY_GRAD_KERNEL(Sqrt) + +} // namespace sparse +} // namespace phi + diff --git a/paddle/phi/kernels/sparse/math_kernel.cc b/paddle/phi/kernels/sparse/math_kernel.cc new file mode 100644 index 0000000000000..150496ee0a045 --- /dev/null +++ b/paddle/phi/kernels/sparse/math_kernel.cc @@ -0,0 +1,50 @@ +#include "paddle/phi/kernels/activation_kernel.h" +#include "paddle/phi/kernels/sparse/utils.h" + +DEFINE_AND_REGISTER_SPARSE_UNARY_KERNEL(sin, SinKernel) + +// NOTE: the following code is to bypass the restriction of Paddle +// kernel registration mechanism. Do NOT refactor them unless you +// know what you are doing. +// If you want to implement any new kernel, please follow the above +// `log`, do NOT follow the following `sqrt`. +DEFINE_SPARSE_UNARY_KERNEL(SqrtKernel) + +PD_REGISTER_KERNEL(sparse_coo_sqrt, + CPU, + ALL_LAYOUT, + phi::sparse::SparseCooSqrtKernel, + float, + double) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); +} +PD_REGISTER_KERNEL(sparse_csr_sqrt, + CPU, + ALL_LAYOUT, + phi::sparse::SparseCsrSqrtKernel, + float, + double) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); +} + +#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) +PD_REGISTER_KERNEL(sparse_coo_sqrt, + GPU, + ALL_LAYOUT, + phi::sparse::SparseCooSqrtKernel, + float, + double, + phi::dtype::float16) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); +} + +PD_REGISTER_KERNEL(sparse_csr_sqrt, + GPU, + ALL_LAYOUT, + phi::sparse::SparseCsrSqrtKernel, + float, + double, + phi::dtype::float16) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); +} +#endif diff --git a/paddle/phi/kernels/sparse/math_kernel.h b/paddle/phi/kernels/sparse/math_kernel.h new file mode 100644 index 0000000000000..1738743b03e6d --- /dev/null +++ b/paddle/phi/kernels/sparse/math_kernel.h @@ -0,0 +1,25 @@ +#pragma once + +#include "paddle/phi/core/dense_tensor.h" +#include "paddle/phi/core/sparse_coo_tensor.h" +#include "paddle/phi/core/sparse_csr_tensor.h" +#include "paddle/phi/kernels/activation_kernel.h" +#include "paddle/phi/kernels/empty_kernel.h" +#include "paddle/phi/kernels/sparse/utils.h" + +namespace phi { +namespace sparse { + +DECLARE_SPARSE_UNARY_KERNEL(Sqrt) + +template +SparseCooTensor SparseSqrt(const Context& dev_ctx, const SparseCooTensor& x) { + DenseTensor indices, values; + SparseCooTensor coo(indices, values, x.dims()); + SparseCooSqrtKernel(dev_ctx, x, &coo); + return coo; +} + +} // namespace sparse +} // namespace phi + diff --git a/paddle/phi/kernels/sparse/utils.h b/paddle/phi/kernels/sparse/utils.h index a6b12d49beb91..5134bf8feb037 100644 --- a/paddle/phi/kernels/sparse/utils.h +++ b/paddle/phi/kernels/sparse/utils.h @@ -1,9 +1,33 @@ #include "paddle/phi/backends/cpu/cpu_context.h" #include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/core/sparse_coo_tensor.h" +#include "paddle/phi/core/sparse_csr_tensor.h" #include "paddle/phi/kernels/copy_kernel.h" #include "paddle/phi/kernels/empty_kernel.h" +#define DECLARE_SPARSE_UNARY_KERNEL(name) \ + template \ + void SparseCoo##name##Kernel( \ + const Context& dev_ctx, const SparseCooTensor& x, SparseCooTensor* out); \ + \ + template \ + void SparseCsr##name##Kernel( \ + const Context& dev_ctx, const SparseCsrTensor& x, SparseCsrTensor* out); + +#define DECLARE_SPARSE_UNARY_GRAD_KERNEL(name) \ + template \ + void SparseCoo##name##GradKernel(const Context& dev_ctx, \ + const SparseCooTensor& x, \ + const SparseCooTensor& out_grad, \ + SparseCooTensor* x_grad); \ + \ + template \ + void SparseCsr##name##GradKernel(const Context& dev_ctx, \ + const SparseCsrTensor& x, \ + const SparseCsrTensor& out_grad, \ + SparseCsrTensor* x_grad); + #define DEFINE_SPARSE_UNARY_KERNEL(dense_kernel_func) \ namespace phi { \ namespace sparse { \ diff --git a/python/paddle/fluid/tests/unittests/test_sparse_activation_op.py b/python/paddle/fluid/tests/unittests/test_sparse_unary_op.py similarity index 72% rename from python/paddle/fluid/tests/unittests/test_sparse_activation_op.py rename to python/paddle/fluid/tests/unittests/test_sparse_unary_op.py index c3312199fff17..fa4c1a70e51eb 100644 --- a/python/paddle/fluid/tests/unittests/test_sparse_activation_op.py +++ b/python/paddle/fluid/tests/unittests/test_sparse_unary_op.py @@ -31,9 +31,10 @@ def compare_with_dense( test_gradient: bool, ): def tensor_allclose(dense_tensor: paddle.Tensor, sparse_tensor: paddle.Tensor): - mask = ~np.isnan(dense_tensor.numpy()) + dense_numpy = dense_tensor.numpy() + mask = ~(np.isnan(dense_numpy) | np.isinf(dense_numpy)) return np.allclose( - dense_tensor.numpy()[mask], sparse_tensor.to_dense().numpy()[mask] + dense_numpy[mask], sparse_tensor.to_dense().numpy()[mask] ) with _test_eager_guard(): @@ -92,6 +93,41 @@ def test_sparse_sqrt(self): False, ) + def test_sparse_sin(self): + x = [[0, 16, 0, 0], [0, 0, 0, 0], [0, 4, 2, 0]] + sparse_dim = 2 + self.compare_with_dense( + x, + lambda x: x.to_sparse_coo(sparse_dim), + paddle.sin, + paddle.sparse.functional.sin, + True, + ) + self.compare_with_dense( + x, + lambda x: x.to_sparse_csr(), + paddle.sin, + paddle.sparse.functional.sin, + False, + ) + + def test_sparse_tanh(self): + x = [[0, 16, 0, 0], [0, 0, 0, 0], [0, -4, 2, 0]] + sparse_dim = 2 + self.compare_with_dense( + x, + lambda x: x.to_sparse_coo(sparse_dim), + paddle.tanh, + paddle.sparse.functional.tanh, + True, + ) + self.compare_with_dense( + x, + lambda x: x.to_sparse_csr(), + paddle.tanh, + paddle.sparse.functional.tanh, + False, + ) if __name__ == "__main__": unittest.main() diff --git a/python/paddle/sparse/functional/__init__.py b/python/paddle/sparse/functional/__init__.py index a970c0acaeb2e..9c1e3f8034b81 100644 --- a/python/paddle/sparse/functional/__init__.py +++ b/python/paddle/sparse/functional/__init__.py @@ -13,8 +13,10 @@ # limitations under the License. from .activation import relu # noqa: F401 -from .activation import sqrt # noqa: F401 +from .activation import tanh # noqa: F401 +from .math import sqrt # noqa: F401 +from .math import sin # noqa: F401 from .conv import conv3d # noqa: F401 from .conv import subm_conv3d # noqa: F401 -__all__ = ['relu', 'conv3d', 'subm_conv3d', 'sqrt'] +__all__ = ['relu', 'tanh', 'conv3d', 'subm_conv3d', 'sqrt', 'sin'] diff --git a/python/paddle/sparse/functional/activation.py b/python/paddle/sparse/functional/activation.py index 5d37040367785..4710821d7e101 100644 --- a/python/paddle/sparse/functional/activation.py +++ b/python/paddle/sparse/functional/activation.py @@ -98,3 +98,43 @@ def sqrt(x, name=None): "Currently, sparse.sqrt only support the input of SparseCooTensor or SparseCsrTensor" ) + +def tanh(x, name=None): + """ + sparse tanh activation. + + .. math:: + + out = tanh(x) + + Parameters: + x (Tensor): The input Sparse Tensor with data type float32, float64. + name (str, optional): Name for the operation (optional, default is None). + For more information, please refer to :ref:`api_guide_Name`. + + Returns: + A Sparse Tensor with the same data type and shape as ``x`` . + + Examples: + .. code-block:: python + + import paddle + import numpy as np + from paddle.fluid.framework import _test_eager_guard + + with _test_eager_guard(): + dense_x = paddle.to_tensor(np.array([-2, 0, 1]).astype('float32')) + sparse_x = dense_x.to_sparse_coo(1) + out = paddle.sparse.functional.tanh(sparse_x) + """ + + assert in_dynamic_mode(), "Currently, Sparse API only support dynamic mode" + + if x.is_sparse_coo(): + return _C_ops.final_state_sparse_coo_tanh(x) + elif x.is_sparse_csr(): + return _C_ops.final_state_sparse_csr_tanh(x) + else: + raise ValueError( + "Currently, sparse.tanh only support the input of SparseCooTensor or SparseCsrTensor" + ) diff --git a/python/paddle/sparse/functional/math.py b/python/paddle/sparse/functional/math.py new file mode 100644 index 0000000000000..d5ce3629f157e --- /dev/null +++ b/python/paddle/sparse/functional/math.py @@ -0,0 +1,87 @@ +__all__ = [] + +from paddle import _C_ops, in_dynamic_mode + + +def sqrt(x, name=None): + """ + sparse sqrt. + + .. math:: + + out = sqrt(x) + + Parameters: + x (Tensor): The input Sparse Tensor with data type float32, float64. + name (str, optional): Name for the operation (optional, default is None). + For more information, please refer to :ref:`api_guide_Name`. + + Returns: + A Sparse Tensor with the same data type and shape as ``x`` . + + Examples: + .. code-block:: python + + import paddle + import numpy as np + from paddle.fluid.framework import _test_eager_guard + + with _test_eager_guard(): + dense_x = paddle.to_tensor(np.array([4, 0, 1]).astype('float32')) + sparse_x = dense_x.to_sparse_coo(1) + out = paddle.sparse.functional.sqrt(sparse_x) + """ + + assert in_dynamic_mode(), "Currently, Sparse API only support dynamic mode" + + if x.is_sparse_coo(): + return _C_ops.final_state_sparse_coo_sqrt(x) + elif x.is_sparse_csr(): + return _C_ops.final_state_sparse_csr_sqrt(x) + else: + raise ValueError( + "Currently, sparse.sqrt only support the input of SparseCooTensor or SparseCsrTensor" + ) + + +def sin(x, name=None): + """ + sparse sin. + + .. math:: + + out = sin(x) + + Parameters: + x (Tensor): The input Sparse Tensor with data type float32, float64. + name (str, optional): Name for the operation (optional, default is None). + For more information, please refer to :ref:`api_guide_Name`. + + Returns: + A Sparse Tensor with the same data type and shape as ``x`` . + + Examples: + .. code-block:: python + + import paddle + import numpy as np + from paddle.fluid.framework import _test_eager_guard + + with _test_eager_guard(): + dense_x = paddle.to_tensor(np.array([-2, 0, 3]).astype('float32')) + sparse_x = dense_x.to_sparse_coo(1) + out = paddle.sparse.functional.sin(sparse_x) + """ + + assert in_dynamic_mode(), "Currently, Sparse API only support dynamic mode" + + if x.is_sparse_coo(): + return _C_ops.final_state_sparse_coo_sin(x) + elif x.is_sparse_csr(): + return _C_ops.final_state_sparse_csr_sin(x) + else: + raise ValueError( + "Currently, sparse.sin only support the input of SparseCooTensor or SparseCsrTensor" + ) + + diff --git a/python/paddle/utils/code_gen/sparse_api.yaml b/python/paddle/utils/code_gen/sparse_api.yaml index 8bf148ddd452e..0bbc5e50f7a67 100644 --- a/python/paddle/utils/code_gen/sparse_api.yaml +++ b/python/paddle/utils/code_gen/sparse_api.yaml @@ -73,6 +73,36 @@ func : sparse_csr_sqrt layout : x +- api : coo_tanh + args : (Tensor x) + output : Tensor(out@SparseCooTensor) + kernel : + func : sparse_coo_tanh + layout : x + backward : sparse_coo_tanh_grad + +- api : csr_tanh + args : (Tensor x) + output : Tensor(out@SparseCsrTensor) + kernel : + func : sparse_csr_tanh + layout : x + +- api : coo_sin + args : (Tensor x) + output : Tensor(out@SparseCooTensor) + kernel : + func : sparse_coo_sin + layout : x + backward : sparse_coo_sin_grad + +- api : csr_sin + args : (Tensor x) + output : Tensor(out@SparseCsrTensor) + kernel : + func : sparse_csr_sin + layout : x + - api : to_dense args : (Tensor x) output : Tensor(out@DenseTensor) diff --git a/python/paddle/utils/code_gen/sparse_bw_api.yaml b/python/paddle/utils/code_gen/sparse_bw_api.yaml index ff9d5d458491d..fc37f2d05b79a 100644 --- a/python/paddle/utils/code_gen/sparse_bw_api.yaml +++ b/python/paddle/utils/code_gen/sparse_bw_api.yaml @@ -45,3 +45,17 @@ output : Tensor(x_grad@SparseCooTensor) kernel : func : sparse_coo_sqrt_grad + +- backward_api : sparse_coo_tanh_grad + forward : sparse_coo_tanh(Tensor x) -> Tensor(out@SparseCooTensor) + args : (Tensor out, Tensor out_grad) + output : Tensor(x_grad@SparseCooTensor) + kernel : + func : sparse_coo_tanh_grad + +- backward_api : sparse_coo_sin_grad + forward : sparse_coo_sin(Tensor x) -> Tensor(out@SparseCooTensor) + args : (Tensor x, Tensor out_grad) + output : Tensor(x_grad@SparseCooTensor) + kernel : + func : sparse_coo_sin_grad From 3ad6fba20e13492ab0c9807c6515dab94f611d88 Mon Sep 17 00:00:00 2001 From: tiancaishaonvjituizi <452565578@qq.com> Date: Thu, 21 Apr 2022 22:46:19 +0800 Subject: [PATCH 14/31] polish header file --- paddle/phi/kernels/sparse/math_grad_kernel.h | 1 + paddle/phi/kernels/sparse/math_kernel.h | 9 +-------- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/paddle/phi/kernels/sparse/math_grad_kernel.h b/paddle/phi/kernels/sparse/math_grad_kernel.h index 52c0cf993ed15..9d83269a263e2 100644 --- a/paddle/phi/kernels/sparse/math_grad_kernel.h +++ b/paddle/phi/kernels/sparse/math_grad_kernel.h @@ -6,6 +6,7 @@ namespace phi { namespace sparse { DECLARE_SPARSE_UNARY_GRAD_KERNEL(Sqrt) +DECLARE_SPARSE_UNARY_GRAD_KERNEL(Sin) } // namespace sparse } // namespace phi diff --git a/paddle/phi/kernels/sparse/math_kernel.h b/paddle/phi/kernels/sparse/math_kernel.h index 1738743b03e6d..290baa50edfca 100644 --- a/paddle/phi/kernels/sparse/math_kernel.h +++ b/paddle/phi/kernels/sparse/math_kernel.h @@ -11,14 +11,7 @@ namespace phi { namespace sparse { DECLARE_SPARSE_UNARY_KERNEL(Sqrt) - -template -SparseCooTensor SparseSqrt(const Context& dev_ctx, const SparseCooTensor& x) { - DenseTensor indices, values; - SparseCooTensor coo(indices, values, x.dims()); - SparseCooSqrtKernel(dev_ctx, x, &coo); - return coo; -} +DECLARE_SPARSE_UNARY_KERNEL(Sin) } // namespace sparse } // namespace phi From 56fc5da1b1d23e93605d80bf407dc7ea43eabbd9 Mon Sep 17 00:00:00 2001 From: tiancaishaonvjituizi <452565578@qq.com> Date: Thu, 21 Apr 2022 22:50:43 +0800 Subject: [PATCH 15/31] reformat --- paddle/phi/kernels/sparse/math_grad_kernel.h | 1 - 1 file changed, 1 deletion(-) diff --git a/paddle/phi/kernels/sparse/math_grad_kernel.h b/paddle/phi/kernels/sparse/math_grad_kernel.h index 9d83269a263e2..febb7fbbf88e8 100644 --- a/paddle/phi/kernels/sparse/math_grad_kernel.h +++ b/paddle/phi/kernels/sparse/math_grad_kernel.h @@ -10,4 +10,3 @@ DECLARE_SPARSE_UNARY_GRAD_KERNEL(Sin) } // namespace sparse } // namespace phi - From 1f18c59fcb081069ddf35eace5fd7aa3e0c2f7aa Mon Sep 17 00:00:00 2001 From: tiancaishaonvjituizi <452565578@qq.com> Date: Thu, 21 Apr 2022 22:55:37 +0800 Subject: [PATCH 16/31] refine --- .../tests/unittests/test_sparse_unary_op.py | 10 ++--- python/paddle/sparse/functional/activation.py | 41 ------------------- 2 files changed, 5 insertions(+), 46 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_sparse_unary_op.py b/python/paddle/fluid/tests/unittests/test_sparse_unary_op.py index fa4c1a70e51eb..9e3a07fd0f246 100644 --- a/python/paddle/fluid/tests/unittests/test_sparse_unary_op.py +++ b/python/paddle/fluid/tests/unittests/test_sparse_unary_op.py @@ -32,7 +32,7 @@ def compare_with_dense( ): def tensor_allclose(dense_tensor: paddle.Tensor, sparse_tensor: paddle.Tensor): dense_numpy = dense_tensor.numpy() - mask = ~(np.isnan(dense_numpy) | np.isinf(dense_numpy)) + mask = ~np.isnan(dense_numpy) return np.allclose( dense_numpy[mask], sparse_tensor.to_dense().numpy()[mask] ) @@ -63,15 +63,15 @@ def test_sparse_relu(self): self.compare_with_dense( x, lambda x: x.to_sparse_coo(sparse_dim), - lambda x: paddle.nn.ReLU()(x), - lambda x: paddle.sparse.ReLU()(x), + paddle.nn.ReLU(), + paddle.sparse.ReLU(), True, ) self.compare_with_dense( x, lambda x: x.to_sparse_csr(), - lambda x: paddle.nn.ReLU()(x), - lambda x: paddle.sparse.ReLU()(x), + paddle.nn.ReLU(), + paddle.sparse.ReLU(), False, ) diff --git a/python/paddle/sparse/functional/activation.py b/python/paddle/sparse/functional/activation.py index 4710821d7e101..8569abdefa274 100644 --- a/python/paddle/sparse/functional/activation.py +++ b/python/paddle/sparse/functional/activation.py @@ -58,47 +58,6 @@ def relu(x, name=None): ) -def sqrt(x, name=None): - """ - sparse sqrt activation. - - .. math:: - - out = sqrt(x) - - Parameters: - x (Tensor): The input Sparse Tensor with data type float32, float64. - name (str, optional): Name for the operation (optional, default is None). - For more information, please refer to :ref:`api_guide_Name`. - - Returns: - A Sparse Tensor with the same data type and shape as ``x`` . - - Examples: - .. code-block:: python - - import paddle - import numpy as np - from paddle.fluid.framework import _test_eager_guard - - with _test_eager_guard(): - dense_x = paddle.to_tensor(np.array([4, 0, 1]).astype('float32')) - sparse_x = dense_x.to_sparse_coo(1) - out = paddle.sparse.functional.sqrt(sparse_x) - """ - - assert in_dynamic_mode(), "Currently, Sparse API only support dynamic mode" - - if x.is_sparse_coo(): - return _C_ops.final_state_sparse_coo_sqrt(x) - elif x.is_sparse_csr(): - return _C_ops.final_state_sparse_csr_sqrt(x) - else: - raise ValueError( - "Currently, sparse.sqrt only support the input of SparseCooTensor or SparseCsrTensor" - ) - - def tanh(x, name=None): """ sparse tanh activation. From c606825bfe1b5e3810b35c55e441710a8faa8e38 Mon Sep 17 00:00:00 2001 From: tiancaishaonvjituizi <452565578@qq.com> Date: Fri, 22 Apr 2022 10:46:01 +0800 Subject: [PATCH 17/31] fix typo --- paddle/phi/kernels/sparse/activation_grad_kernel.cc | 2 +- paddle/phi/kernels/sparse/math_grad_kernel.cc | 2 +- paddle/phi/kernels/sparse/math_kernel.cc | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/paddle/phi/kernels/sparse/activation_grad_kernel.cc b/paddle/phi/kernels/sparse/activation_grad_kernel.cc index 3c5aebb284ab2..b16897cd76b6c 100644 --- a/paddle/phi/kernels/sparse/activation_grad_kernel.cc +++ b/paddle/phi/kernels/sparse/activation_grad_kernel.cc @@ -23,7 +23,7 @@ DEFINE_AND_REGISTER_SPARSE_UNARY_GRAD_KERNEL(tanh_grad, TanhGradKernel) // kernel registration mechanism. Do NOT refactor them unless you // know what you are doing. // If you want to implement any new kernel, please follow the above -// `sigmoid_grad`, do NOT follow the following `relu_grad`. +// `tanh_grad`, do NOT follow the following `relu_grad`. DEFINE_SPARSE_UNARY_GRAD_KERNEL(ReluGradKernel) PD_REGISTER_KERNEL(sparse_coo_relu_grad, diff --git a/paddle/phi/kernels/sparse/math_grad_kernel.cc b/paddle/phi/kernels/sparse/math_grad_kernel.cc index f0ff5aa5eced6..96040d229c628 100644 --- a/paddle/phi/kernels/sparse/math_grad_kernel.cc +++ b/paddle/phi/kernels/sparse/math_grad_kernel.cc @@ -9,7 +9,7 @@ DEFINE_AND_REGISTER_SPARSE_UNARY_GRAD_KERNEL(sin_grad, SinGradKernel) // kernel registration mechanism. Do NOT refactor them unless you // know what you are doing. // If you want to implement any new kernel, please follow the above -// `log_grad`, do NOT follow the following `sqrt_grad`. +// `sin_grad`, do NOT follow the following `sqrt_grad`. DEFINE_SPARSE_UNARY_GRAD_KERNEL(SqrtGradKernel) PD_REGISTER_KERNEL(sparse_coo_sqrt_grad, diff --git a/paddle/phi/kernels/sparse/math_kernel.cc b/paddle/phi/kernels/sparse/math_kernel.cc index 150496ee0a045..1edc08d46b799 100644 --- a/paddle/phi/kernels/sparse/math_kernel.cc +++ b/paddle/phi/kernels/sparse/math_kernel.cc @@ -7,7 +7,7 @@ DEFINE_AND_REGISTER_SPARSE_UNARY_KERNEL(sin, SinKernel) // kernel registration mechanism. Do NOT refactor them unless you // know what you are doing. // If you want to implement any new kernel, please follow the above -// `log`, do NOT follow the following `sqrt`. +// `sin`, do NOT follow the following `sqrt`. DEFINE_SPARSE_UNARY_KERNEL(SqrtKernel) PD_REGISTER_KERNEL(sparse_coo_sqrt, From 5dd450739d2aed667b8a141327205690229d7f8b Mon Sep 17 00:00:00 2001 From: tiancaishaonvjituizi <452565578@qq.com> Date: Fri, 22 Apr 2022 10:47:15 +0800 Subject: [PATCH 18/31] fix typo --- python/paddle/fluid/tests/unittests/test_sparse_unary_op.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/fluid/tests/unittests/test_sparse_unary_op.py b/python/paddle/fluid/tests/unittests/test_sparse_unary_op.py index 9e3a07fd0f246..2610607eeb84d 100644 --- a/python/paddle/fluid/tests/unittests/test_sparse_unary_op.py +++ b/python/paddle/fluid/tests/unittests/test_sparse_unary_op.py @@ -21,7 +21,7 @@ from paddle import _C_ops -class TestSparseActivation(unittest.TestCase): +class TestSparseUnary(unittest.TestCase): def compare_with_dense( self, x, From f59fa26ea11278305819ece6f4c2aa1aea69450b Mon Sep 17 00:00:00 2001 From: tiancaishaonvjituizi <452565578@qq.com> Date: Sat, 23 Apr 2022 16:34:11 +0800 Subject: [PATCH 19/31] add test about error, reformat --- paddle/phi/kernels/sparse/math_grad_kernel.cc | 14 +++ paddle/phi/kernels/sparse/math_grad_kernel.h | 14 +++ paddle/phi/kernels/sparse/math_kernel.cc | 16 ++++ paddle/phi/kernels/sparse/math_kernel.h | 15 +++- .../tests/unittests/test_sparse_unary_op.py | 61 +++++++------ python/paddle/utils/code_gen/sparse_api.yaml | 86 +++++++++---------- .../paddle/utils/code_gen/sparse_bw_api.yaml | 14 +-- 7 files changed, 138 insertions(+), 82 deletions(-) diff --git a/paddle/phi/kernels/sparse/math_grad_kernel.cc b/paddle/phi/kernels/sparse/math_grad_kernel.cc index 96040d229c628..5bc39673d0f44 100644 --- a/paddle/phi/kernels/sparse/math_grad_kernel.cc +++ b/paddle/phi/kernels/sparse/math_grad_kernel.cc @@ -1,3 +1,17 @@ +// Copyright (c) 2022 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. + #include "paddle/phi/kernels/sparse/math_grad_kernel.h" #include "paddle/phi/kernels/activation_grad_kernel.h" diff --git a/paddle/phi/kernels/sparse/math_grad_kernel.h b/paddle/phi/kernels/sparse/math_grad_kernel.h index febb7fbbf88e8..0b6a9ce26af76 100644 --- a/paddle/phi/kernels/sparse/math_grad_kernel.h +++ b/paddle/phi/kernels/sparse/math_grad_kernel.h @@ -1,3 +1,17 @@ +// Copyright (c) 2022 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. + #pragma once #include "paddle/phi/kernels/sparse/utils.h" diff --git a/paddle/phi/kernels/sparse/math_kernel.cc b/paddle/phi/kernels/sparse/math_kernel.cc index 1edc08d46b799..9706730e0119d 100644 --- a/paddle/phi/kernels/sparse/math_kernel.cc +++ b/paddle/phi/kernels/sparse/math_kernel.cc @@ -1,3 +1,19 @@ +// Copyright (c) 2022 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. + +#include "paddle/phi/kernels/sparse/math_kernel.h" + #include "paddle/phi/kernels/activation_kernel.h" #include "paddle/phi/kernels/sparse/utils.h" diff --git a/paddle/phi/kernels/sparse/math_kernel.h b/paddle/phi/kernels/sparse/math_kernel.h index 290baa50edfca..b48f04f7a0f0f 100644 --- a/paddle/phi/kernels/sparse/math_kernel.h +++ b/paddle/phi/kernels/sparse/math_kernel.h @@ -1,3 +1,17 @@ +// Copyright (c) 2022 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. + #pragma once #include "paddle/phi/core/dense_tensor.h" @@ -15,4 +29,3 @@ DECLARE_SPARSE_UNARY_KERNEL(Sin) } // namespace sparse } // namespace phi - diff --git a/python/paddle/fluid/tests/unittests/test_sparse_unary_op.py b/python/paddle/fluid/tests/unittests/test_sparse_unary_op.py index 2610607eeb84d..a972270f8fad0 100644 --- a/python/paddle/fluid/tests/unittests/test_sparse_unary_op.py +++ b/python/paddle/fluid/tests/unittests/test_sparse_unary_op.py @@ -22,32 +22,34 @@ class TestSparseUnary(unittest.TestCase): + def assert_raises_on_dense_tensor(self, sparse_func): + dense_x = paddle.ones((2, 3)) + with self.assertRaises(ValueError): + sparse_func(dense_x) + def compare_with_dense( - self, - x, - to_sparse: Callable[[paddle.Tensor], paddle.Tensor], - dense_func: Callable[[paddle.Tensor], paddle.Tensor], - sparse_func: Callable[[paddle.Tensor], paddle.Tensor], - test_gradient: bool, - ): - def tensor_allclose(dense_tensor: paddle.Tensor, sparse_tensor: paddle.Tensor): + self, + x, + to_sparse: Callable[[paddle.Tensor], paddle.Tensor], + dense_func: Callable[[paddle.Tensor], paddle.Tensor], + sparse_func: Callable[[paddle.Tensor], paddle.Tensor], + test_gradient: bool, ): + def tensor_allclose(dense_tensor: paddle.Tensor, + sparse_tensor: paddle.Tensor): dense_numpy = dense_tensor.numpy() mask = ~np.isnan(dense_numpy) - return np.allclose( - dense_numpy[mask], sparse_tensor.to_dense().numpy()[mask] - ) + return np.allclose(dense_numpy[mask], + sparse_tensor.to_dense().numpy()[mask]) with _test_eager_guard(): dense_x = paddle.to_tensor( - x, dtype="float32", stop_gradient=not test_gradient - ) + x, dtype="float32", stop_gradient=not test_gradient) sparse_x = to_sparse(dense_x) sparse_out = sparse_func(sparse_x) dense_x = paddle.to_tensor( - x, dtype="float32", stop_gradient=not test_gradient - ) + x, dtype="float32", stop_gradient=not test_gradient) dense_out = dense_func(dense_x) assert tensor_allclose(dense_out, sparse_out) @@ -65,15 +67,14 @@ def test_sparse_relu(self): lambda x: x.to_sparse_coo(sparse_dim), paddle.nn.ReLU(), paddle.sparse.ReLU(), - True, - ) + True, ) self.compare_with_dense( x, lambda x: x.to_sparse_csr(), paddle.nn.ReLU(), paddle.sparse.ReLU(), - False, - ) + False, ) + self.assert_raises_on_dense_tensor(paddle.sparse.ReLU()) def test_sparse_sqrt(self): x = [[0, 16, 0, 0], [0, 0, 0, 0], [0, 4, 2, 0]] @@ -83,15 +84,14 @@ def test_sparse_sqrt(self): lambda x: x.to_sparse_coo(sparse_dim), paddle.sqrt, paddle.sparse.functional.sqrt, - True, - ) + True, ) self.compare_with_dense( x, lambda x: x.to_sparse_csr(), paddle.sqrt, paddle.sparse.functional.sqrt, - False, - ) + False, ) + self.assert_raises_on_dense_tensor(paddle.sparse.functional.sqrt) def test_sparse_sin(self): x = [[0, 16, 0, 0], [0, 0, 0, 0], [0, 4, 2, 0]] @@ -101,15 +101,14 @@ def test_sparse_sin(self): lambda x: x.to_sparse_coo(sparse_dim), paddle.sin, paddle.sparse.functional.sin, - True, - ) + True, ) self.compare_with_dense( x, lambda x: x.to_sparse_csr(), paddle.sin, paddle.sparse.functional.sin, - False, - ) + False, ) + self.assert_raises_on_dense_tensor(paddle.sparse.functional.sin) def test_sparse_tanh(self): x = [[0, 16, 0, 0], [0, 0, 0, 0], [0, -4, 2, 0]] @@ -119,15 +118,15 @@ def test_sparse_tanh(self): lambda x: x.to_sparse_coo(sparse_dim), paddle.tanh, paddle.sparse.functional.tanh, - True, - ) + True, ) self.compare_with_dense( x, lambda x: x.to_sparse_csr(), paddle.tanh, paddle.sparse.functional.tanh, - False, - ) + False, ) + self.assert_raises_on_dense_tensor(paddle.sparse.functional.tanh) + if __name__ == "__main__": unittest.main() diff --git a/python/paddle/utils/code_gen/sparse_api.yaml b/python/paddle/utils/code_gen/sparse_api.yaml index 0bbc5e50f7a67..1ff2d3e31bcc4 100644 --- a/python/paddle/utils/code_gen/sparse_api.yaml +++ b/python/paddle/utils/code_gen/sparse_api.yaml @@ -7,6 +7,38 @@ intermediate : rulebook backward : conv3d_grad +- api : coo_relu + args : (Tensor x) + output : Tensor(out@SparseCooTensor) + kernel : + func : sparse_coo_relu + layout : x + backward : sparse_coo_relu_grad + +- api : coo_sin + args : (Tensor x) + output : Tensor(out@SparseCooTensor) + kernel : + func : sparse_coo_sin + layout : x + backward : sparse_coo_sin_grad + +- api : coo_sqrt + args : (Tensor x) + output : Tensor(out@SparseCooTensor) + kernel : + func : sparse_coo_sqrt + layout : x + backward : sparse_coo_sqrt_grad + +- api : coo_tanh + args : (Tensor x) + output : Tensor(out@SparseCooTensor) + kernel : + func : sparse_coo_tanh + layout : x + backward : sparse_coo_tanh_grad + - api : coo_to_dense args : (Tensor x) output : Tensor(out@DenseTensor) @@ -30,27 +62,6 @@ data_type : values backward : create_sparse_coo_tensor_grad -- api : csr_values - args : (Tensor x) - output : Tensor(out@DenseTensor) - kernel : - func : csr_values - layout : x - -- api : dense_to_coo - args : (Tensor x, int64_t sparse_dim) - output : Tensor(out@SparseCooTensor) - invoke : to_sparse_coo_impl(x, sparse_dim) - backward : dense_to_coo_grad - -- api : coo_relu - args : (Tensor x) - output : Tensor(out@SparseCooTensor) - kernel : - func : sparse_coo_relu - layout : x - backward : sparse_coo_relu_grad - - api : csr_relu args : (Tensor x) output : Tensor(out@SparseCsrTensor) @@ -58,13 +69,12 @@ func : sparse_csr_relu layout : x -- api : coo_sqrt +- api : csr_sin args : (Tensor x) - output : Tensor(out@SparseCooTensor) + output : Tensor(out@SparseCsrTensor) kernel : - func : sparse_coo_sqrt + func : sparse_csr_sin layout : x - backward : sparse_coo_sqrt_grad - api : csr_sqrt args : (Tensor x) @@ -73,14 +83,6 @@ func : sparse_csr_sqrt layout : x -- api : coo_tanh - args : (Tensor x) - output : Tensor(out@SparseCooTensor) - kernel : - func : sparse_coo_tanh - layout : x - backward : sparse_coo_tanh_grad - - api : csr_tanh args : (Tensor x) output : Tensor(out@SparseCsrTensor) @@ -88,20 +90,18 @@ func : sparse_csr_tanh layout : x -- api : coo_sin +- api : csr_values args : (Tensor x) - output : Tensor(out@SparseCooTensor) + output : Tensor(out@DenseTensor) kernel : - func : sparse_coo_sin + func : csr_values layout : x - backward : sparse_coo_sin_grad -- api : csr_sin - args : (Tensor x) - output : Tensor(out@SparseCsrTensor) - kernel : - func : sparse_csr_sin - layout : x +- api : dense_to_coo + args : (Tensor x, int64_t sparse_dim) + output : Tensor(out@SparseCooTensor) + invoke : to_sparse_coo_impl(x, sparse_dim) + backward : dense_to_coo_grad - api : to_dense args : (Tensor x) diff --git a/python/paddle/utils/code_gen/sparse_bw_api.yaml b/python/paddle/utils/code_gen/sparse_bw_api.yaml index fc37f2d05b79a..f272fff1efb94 100644 --- a/python/paddle/utils/code_gen/sparse_bw_api.yaml +++ b/python/paddle/utils/code_gen/sparse_bw_api.yaml @@ -39,6 +39,13 @@ kernel : func : sparse_coo_relu_grad +- backward_api : sparse_coo_sin_grad + forward : sparse_coo_sin(Tensor x) -> Tensor(out@SparseCooTensor) + args : (Tensor x, Tensor out_grad) + output : Tensor(x_grad@SparseCooTensor) + kernel : + func : sparse_coo_sin_grad + - backward_api : sparse_coo_sqrt_grad forward : sparse_coo_sqrt(Tensor x) -> Tensor(out@SparseCooTensor) args : (Tensor out, Tensor out_grad) @@ -52,10 +59,3 @@ output : Tensor(x_grad@SparseCooTensor) kernel : func : sparse_coo_tanh_grad - -- backward_api : sparse_coo_sin_grad - forward : sparse_coo_sin(Tensor x) -> Tensor(out@SparseCooTensor) - args : (Tensor x, Tensor out_grad) - output : Tensor(x_grad@SparseCooTensor) - kernel : - func : sparse_coo_sin_grad From dea61c7528be79de4371e94475e0ec33f7843765 Mon Sep 17 00:00:00 2001 From: tiancaishaonvjituizi <452565578@qq.com> Date: Sat, 23 Apr 2022 17:20:15 +0800 Subject: [PATCH 20/31] fix test error --- .../paddle/fluid/tests/unittests/test_sparse_unary_op.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_sparse_unary_op.py b/python/paddle/fluid/tests/unittests/test_sparse_unary_op.py index a972270f8fad0..70df9b1322678 100644 --- a/python/paddle/fluid/tests/unittests/test_sparse_unary_op.py +++ b/python/paddle/fluid/tests/unittests/test_sparse_unary_op.py @@ -23,9 +23,10 @@ class TestSparseUnary(unittest.TestCase): def assert_raises_on_dense_tensor(self, sparse_func): - dense_x = paddle.ones((2, 3)) - with self.assertRaises(ValueError): - sparse_func(dense_x) + with _test_eager_guard(): + dense_x = paddle.ones((2, 3)) + with self.assertRaises(ValueError): + sparse_func(dense_x) def compare_with_dense( self, From 60c73591a58b86a3ce9353040fcdadec5b79126e Mon Sep 17 00:00:00 2001 From: tiancaishaonvjituizi <452565578@qq.com> Date: Sat, 23 Apr 2022 19:39:56 +0800 Subject: [PATCH 21/31] fix format --- paddle/phi/kernels/sparse/utils.h | 14 ++++++++++++++ python/paddle/sparse/functional/math.py | 16 ++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/paddle/phi/kernels/sparse/utils.h b/paddle/phi/kernels/sparse/utils.h index 5134bf8feb037..9c19219e8518e 100644 --- a/paddle/phi/kernels/sparse/utils.h +++ b/paddle/phi/kernels/sparse/utils.h @@ -1,3 +1,17 @@ +// Copyright (c) 2022 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. + #include "paddle/phi/backends/cpu/cpu_context.h" #include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/core/kernel_registry.h" diff --git a/python/paddle/sparse/functional/math.py b/python/paddle/sparse/functional/math.py index d5ce3629f157e..2f86f323b413e 100644 --- a/python/paddle/sparse/functional/math.py +++ b/python/paddle/sparse/functional/math.py @@ -1,3 +1,17 @@ +# Copyright (c) 2022 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. + __all__ = [] from paddle import _C_ops, in_dynamic_mode @@ -83,5 +97,3 @@ def sin(x, name=None): raise ValueError( "Currently, sparse.sin only support the input of SparseCooTensor or SparseCsrTensor" ) - - From ad8ceda26ed078cf310bddcc86cfd40fd920d4d0 Mon Sep 17 00:00:00 2001 From: tiancaishaonvjituizi <452565578@qq.com> Date: Tue, 26 Apr 2022 14:20:57 +0800 Subject: [PATCH 22/31] fix false positive warning in gcc>=9 --- paddle/utils/variant.h | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/paddle/utils/variant.h b/paddle/utils/variant.h index a7546d094c2ff..ed579edf18772 100644 --- a/paddle/utils/variant.h +++ b/paddle/utils/variant.h @@ -1823,6 +1823,20 @@ struct dtor { template class destructor; +// gcc >= 9 has a bug that creates a false positive warning +// Reference: +// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92145 +// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89381 +#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 9 +#define USING_SUPER_ASSIGNMENT_OPERATOR \ +_Pragma("GCC diagnostic push") \ +_Pragma("GCC diagnostic ignored \"-Wdeprecated-copy\"") \ +using super::operator=; \ +_Pragma("GCC diagnostic pop") +#else +#define USING_SUPER_ASSIGNMENT_OPERATOR using super::operator=; +#endif + #define MPARK_VARIANT_DESTRUCTOR(destructible_trait, definition, destroy) \ template \ class destructor, destructible_trait> \ @@ -1831,7 +1845,7 @@ class destructor; \ public: \ MPARK_INHERITING_CTOR(destructor, super) \ - using super::operator=; \ + USING_SUPER_ASSIGNMENT_OPERATOR \ \ destructor(const destructor &) = default; \ destructor(destructor &&) = default; \ @@ -1867,7 +1881,7 @@ class constructor : public destructor { public: MPARK_INHERITING_CTOR(constructor, super) - using super::operator=; + USING_SUPER_ASSIGNMENT_OPERATOR protected: #ifndef MPARK_GENERIC_LAMBDAS @@ -1919,7 +1933,7 @@ class move_constructor; \ public: \ MPARK_INHERITING_CTOR(move_constructor, super) \ - using super::operator=; \ + USING_SUPER_ASSIGNMENT_OPERATOR \ \ move_constructor(const move_constructor &) = default; \ definition ~move_constructor() = default; \ @@ -1955,7 +1969,7 @@ class copy_constructor; \ public: \ MPARK_INHERITING_CTOR(copy_constructor, super) \ - using super::operator=; \ + USING_SUPER_ASSIGNMENT_OPERATOR \ \ definition copy_constructor(copy_constructor &&) = default; \ ~copy_constructor() = default; \ @@ -1984,7 +1998,7 @@ class assignment : public copy_constructor { public: MPARK_INHERITING_CTOR(assignment, super) - using super::operator=; + USING_SUPER_ASSIGNMENT_OPERATOR template inline /* auto & */ auto emplace(Args &&... args) @@ -2071,7 +2085,7 @@ class move_assignment; \ public: \ MPARK_INHERITING_CTOR(move_assignment, super) \ - using super::operator=; \ + USING_SUPER_ASSIGNMENT_OPERATOR \ \ move_assignment(const move_assignment &) = default; \ move_assignment(move_assignment &&) = default; \ @@ -2111,7 +2125,7 @@ class copy_assignment; \ public: \ MPARK_INHERITING_CTOR(copy_assignment, super) \ - using super::operator=; \ + USING_SUPER_ASSIGNMENT_OPERATOR \ \ copy_assignment(const copy_assignment &) = default; \ copy_assignment(copy_assignment &&) = default; \ @@ -2141,7 +2155,7 @@ class impl : public copy_assignment> { public: MPARK_INHERITING_CTOR(impl, super) - using super::operator=; + USING_SUPER_ASSIGNMENT_OPERATOR template inline void assign(Arg &&arg) { From 178dd270cfc0599bd0df5dab286c4915903325aa Mon Sep 17 00:00:00 2001 From: tiancaishaonvjituizi <452565578@qq.com> Date: Tue, 26 Apr 2022 15:00:38 +0800 Subject: [PATCH 23/31] use more aggressive way --- paddle/utils/variant.h | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/paddle/utils/variant.h b/paddle/utils/variant.h index ed579edf18772..7b11ae1bee88c 100644 --- a/paddle/utils/variant.h +++ b/paddle/utils/variant.h @@ -13,6 +13,11 @@ #pragma once +#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 9 +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-copy" +#endif + /* variant synopsis @@ -1823,20 +1828,6 @@ struct dtor { template class destructor; -// gcc >= 9 has a bug that creates a false positive warning -// Reference: -// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92145 -// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89381 -#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 9 -#define USING_SUPER_ASSIGNMENT_OPERATOR \ -_Pragma("GCC diagnostic push") \ -_Pragma("GCC diagnostic ignored \"-Wdeprecated-copy\"") \ -using super::operator=; \ -_Pragma("GCC diagnostic pop") -#else -#define USING_SUPER_ASSIGNMENT_OPERATOR using super::operator=; -#endif - #define MPARK_VARIANT_DESTRUCTOR(destructible_trait, definition, destroy) \ template \ class destructor, destructible_trait> \ @@ -1845,7 +1836,7 @@ _Pragma("GCC diagnostic pop") \ public: \ MPARK_INHERITING_CTOR(destructor, super) \ - USING_SUPER_ASSIGNMENT_OPERATOR \ + using super::operator=; \ \ destructor(const destructor &) = default; \ destructor(destructor &&) = default; \ @@ -1881,7 +1872,7 @@ class constructor : public destructor { public: MPARK_INHERITING_CTOR(constructor, super) - USING_SUPER_ASSIGNMENT_OPERATOR + using super::operator=; protected: #ifndef MPARK_GENERIC_LAMBDAS @@ -1933,7 +1924,7 @@ class move_constructor; \ public: \ MPARK_INHERITING_CTOR(move_constructor, super) \ - USING_SUPER_ASSIGNMENT_OPERATOR \ + using super::operator=; \ \ move_constructor(const move_constructor &) = default; \ definition ~move_constructor() = default; \ @@ -1969,7 +1960,7 @@ class copy_constructor; \ public: \ MPARK_INHERITING_CTOR(copy_constructor, super) \ - USING_SUPER_ASSIGNMENT_OPERATOR \ + using super::operator=; \ \ definition copy_constructor(copy_constructor &&) = default; \ ~copy_constructor() = default; \ @@ -1998,7 +1989,7 @@ class assignment : public copy_constructor { public: MPARK_INHERITING_CTOR(assignment, super) - USING_SUPER_ASSIGNMENT_OPERATOR + using super::operator=; template inline /* auto & */ auto emplace(Args &&... args) @@ -2085,7 +2076,7 @@ class move_assignment; \ public: \ MPARK_INHERITING_CTOR(move_assignment, super) \ - USING_SUPER_ASSIGNMENT_OPERATOR \ + using super::operator=; \ \ move_assignment(const move_assignment &) = default; \ move_assignment(move_assignment &&) = default; \ @@ -2125,7 +2116,7 @@ class copy_assignment; \ public: \ MPARK_INHERITING_CTOR(copy_assignment, super) \ - USING_SUPER_ASSIGNMENT_OPERATOR \ + using super::operator=; \ \ copy_assignment(const copy_assignment &) = default; \ copy_assignment(copy_assignment &&) = default; \ @@ -2155,7 +2146,7 @@ class impl : public copy_assignment> { public: MPARK_INHERITING_CTOR(impl, super) - USING_SUPER_ASSIGNMENT_OPERATOR + using super::operator=; template inline void assign(Arg &&arg) { @@ -2842,3 +2833,7 @@ struct hash { }; } // namespace std + +#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 9 +#pragma GCC diagnostic pop +#endif From 7bb41d7150cd419237e133cc071e05d3b8f3a8a4 Mon Sep 17 00:00:00 2001 From: tiancaishaonvjituizi <452565578@qq.com> Date: Tue, 26 Apr 2022 22:00:26 +0800 Subject: [PATCH 24/31] add api in paddle.sparse namespace --- .../tests/unittests/test_sparse_unary_op.py | 18 +++++++++--------- python/paddle/sparse/functional/activation.py | 2 +- python/paddle/sparse/functional/math.py | 4 ++-- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_sparse_unary_op.py b/python/paddle/fluid/tests/unittests/test_sparse_unary_op.py index 70df9b1322678..573cc5ba8cf5d 100644 --- a/python/paddle/fluid/tests/unittests/test_sparse_unary_op.py +++ b/python/paddle/fluid/tests/unittests/test_sparse_unary_op.py @@ -84,15 +84,15 @@ def test_sparse_sqrt(self): x, lambda x: x.to_sparse_coo(sparse_dim), paddle.sqrt, - paddle.sparse.functional.sqrt, + paddle.sparse.sqrt, True, ) self.compare_with_dense( x, lambda x: x.to_sparse_csr(), paddle.sqrt, - paddle.sparse.functional.sqrt, + paddle.sparse.sqrt, False, ) - self.assert_raises_on_dense_tensor(paddle.sparse.functional.sqrt) + self.assert_raises_on_dense_tensor(paddle.sparse.sqrt) def test_sparse_sin(self): x = [[0, 16, 0, 0], [0, 0, 0, 0], [0, 4, 2, 0]] @@ -101,15 +101,15 @@ def test_sparse_sin(self): x, lambda x: x.to_sparse_coo(sparse_dim), paddle.sin, - paddle.sparse.functional.sin, + paddle.sparse.sin, True, ) self.compare_with_dense( x, lambda x: x.to_sparse_csr(), paddle.sin, - paddle.sparse.functional.sin, + paddle.sparse.sin, False, ) - self.assert_raises_on_dense_tensor(paddle.sparse.functional.sin) + self.assert_raises_on_dense_tensor(paddle.sparse.sin) def test_sparse_tanh(self): x = [[0, 16, 0, 0], [0, 0, 0, 0], [0, -4, 2, 0]] @@ -118,15 +118,15 @@ def test_sparse_tanh(self): x, lambda x: x.to_sparse_coo(sparse_dim), paddle.tanh, - paddle.sparse.functional.tanh, + paddle.sparse.tanh, True, ) self.compare_with_dense( x, lambda x: x.to_sparse_csr(), paddle.tanh, - paddle.sparse.functional.tanh, + paddle.sparse.tanh, False, ) - self.assert_raises_on_dense_tensor(paddle.sparse.functional.tanh) + self.assert_raises_on_dense_tensor(paddle.sparse.tanh) if __name__ == "__main__": diff --git a/python/paddle/sparse/functional/activation.py b/python/paddle/sparse/functional/activation.py index 8569abdefa274..6a167406654f6 100644 --- a/python/paddle/sparse/functional/activation.py +++ b/python/paddle/sparse/functional/activation.py @@ -84,7 +84,7 @@ def tanh(x, name=None): with _test_eager_guard(): dense_x = paddle.to_tensor(np.array([-2, 0, 1]).astype('float32')) sparse_x = dense_x.to_sparse_coo(1) - out = paddle.sparse.functional.tanh(sparse_x) + out = paddle.sparse.tanh(sparse_x) """ assert in_dynamic_mode(), "Currently, Sparse API only support dynamic mode" diff --git a/python/paddle/sparse/functional/math.py b/python/paddle/sparse/functional/math.py index 2f86f323b413e..3f18f9ed67c83 100644 --- a/python/paddle/sparse/functional/math.py +++ b/python/paddle/sparse/functional/math.py @@ -43,7 +43,7 @@ def sqrt(x, name=None): with _test_eager_guard(): dense_x = paddle.to_tensor(np.array([4, 0, 1]).astype('float32')) sparse_x = dense_x.to_sparse_coo(1) - out = paddle.sparse.functional.sqrt(sparse_x) + out = paddle.sparse.sqrt(sparse_x) """ assert in_dynamic_mode(), "Currently, Sparse API only support dynamic mode" @@ -84,7 +84,7 @@ def sin(x, name=None): with _test_eager_guard(): dense_x = paddle.to_tensor(np.array([-2, 0, 3]).astype('float32')) sparse_x = dense_x.to_sparse_coo(1) - out = paddle.sparse.functional.sin(sparse_x) + out = paddle.sparse.sin(sparse_x) """ assert in_dynamic_mode(), "Currently, Sparse API only support dynamic mode" From c44ac741d92f8b8e2c633ba913150a730fec53db Mon Sep 17 00:00:00 2001 From: tiancaishaonvjituizi <452565578@qq.com> Date: Wed, 27 Apr 2022 15:19:48 +0800 Subject: [PATCH 25/31] address reviews --- python/paddle/sparse/__init__.py | 6 +++++- python/paddle/sparse/functional/activation.py | 10 ++++------ python/paddle/sparse/functional/math.py | 10 ++++------ 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/python/paddle/sparse/__init__.py b/python/paddle/sparse/__init__.py index 93653e09c9019..17eab93dab18e 100644 --- a/python/paddle/sparse/__init__.py +++ b/python/paddle/sparse/__init__.py @@ -22,7 +22,11 @@ from .layer.pooling import MaxPool3D +from .functional import sqrt +from .functional import sin +from .functional import tanh + __all__ = [ 'sparse_coo_tensor', 'sparse_csr_tensor', 'ReLU', 'Conv3D', 'SubmConv3D', - 'BatchNorm', 'MaxPool3D' + 'BatchNorm', 'MaxPool3D', 'sqrt', 'sin', 'tanh' ] diff --git a/python/paddle/sparse/functional/activation.py b/python/paddle/sparse/functional/activation.py index 6a167406654f6..7eebbde73e1bc 100644 --- a/python/paddle/sparse/functional/activation.py +++ b/python/paddle/sparse/functional/activation.py @@ -19,7 +19,7 @@ def relu(x, name=None): """ - sparse relu activation. + sparse relu activation, requiring x to be a sparse coo or sparse csr tensor. .. math:: @@ -37,11 +37,10 @@ def relu(x, name=None): .. code-block:: python import paddle - import numpy as np from paddle.fluid.framework import _test_eager_guard with _test_eager_guard(): - dense_x = paddle.to_tensor(np.array([-2, 0, 1]).astype('float32')) + dense_x = paddle.to_tensor([-2, 0, 1]) sparse_x = dense_x.to_sparse_coo(1) out = paddle.sparse.functional.relu(sparse_x) """ @@ -60,7 +59,7 @@ def relu(x, name=None): def tanh(x, name=None): """ - sparse tanh activation. + sparse tanh activation, requiring x to be a sparse coo or sparse csr tensor. .. math:: @@ -78,11 +77,10 @@ def tanh(x, name=None): .. code-block:: python import paddle - import numpy as np from paddle.fluid.framework import _test_eager_guard with _test_eager_guard(): - dense_x = paddle.to_tensor(np.array([-2, 0, 1]).astype('float32')) + dense_x = paddle.to_tensor([-2, 0, 1]) sparse_x = dense_x.to_sparse_coo(1) out = paddle.sparse.tanh(sparse_x) """ diff --git a/python/paddle/sparse/functional/math.py b/python/paddle/sparse/functional/math.py index 3f18f9ed67c83..f8e1fa64d4c5a 100644 --- a/python/paddle/sparse/functional/math.py +++ b/python/paddle/sparse/functional/math.py @@ -19,7 +19,7 @@ def sqrt(x, name=None): """ - sparse sqrt. + Calculate square root of x, requiring x to be a sparse coo or sparse csr tensor. .. math:: @@ -37,11 +37,10 @@ def sqrt(x, name=None): .. code-block:: python import paddle - import numpy as np from paddle.fluid.framework import _test_eager_guard with _test_eager_guard(): - dense_x = paddle.to_tensor(np.array([4, 0, 1]).astype('float32')) + dense_x = paddle.to_tensor([4, 0, 1]) sparse_x = dense_x.to_sparse_coo(1) out = paddle.sparse.sqrt(sparse_x) """ @@ -60,7 +59,7 @@ def sqrt(x, name=None): def sin(x, name=None): """ - sparse sin. + Calculate sin of x, requiring x to be a sparse coo or sparse csr tensor. .. math:: @@ -78,11 +77,10 @@ def sin(x, name=None): .. code-block:: python import paddle - import numpy as np from paddle.fluid.framework import _test_eager_guard with _test_eager_guard(): - dense_x = paddle.to_tensor(np.array([-2, 0, 3]).astype('float32')) + dense_x = paddle.to_tensor([-2, 0, 3]) sparse_x = dense_x.to_sparse_coo(1) out = paddle.sparse.sin(sparse_x) """ From b04ab6c376f5e876bcc4385146035884e7f6d168 Mon Sep 17 00:00:00 2001 From: tiancaishaonvjituizi <452565578@qq.com> Date: Fri, 29 Apr 2022 18:54:01 +0800 Subject: [PATCH 26/31] fix ci error --- python/paddle/sparse/functional/activation.py | 4 ++-- python/paddle/sparse/functional/math.py | 4 ++-- python/paddle/utils/code_gen/sparse_bw_api.yaml | 1 - 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/python/paddle/sparse/functional/activation.py b/python/paddle/sparse/functional/activation.py index 7eebbde73e1bc..97879677416cd 100644 --- a/python/paddle/sparse/functional/activation.py +++ b/python/paddle/sparse/functional/activation.py @@ -40,7 +40,7 @@ def relu(x, name=None): from paddle.fluid.framework import _test_eager_guard with _test_eager_guard(): - dense_x = paddle.to_tensor([-2, 0, 1]) + dense_x = paddle.to_tensor([-2, 0, 1], dtype='float32') sparse_x = dense_x.to_sparse_coo(1) out = paddle.sparse.functional.relu(sparse_x) """ @@ -80,7 +80,7 @@ def tanh(x, name=None): from paddle.fluid.framework import _test_eager_guard with _test_eager_guard(): - dense_x = paddle.to_tensor([-2, 0, 1]) + dense_x = paddle.to_tensor([-2, 0, 1], dtype='float32') sparse_x = dense_x.to_sparse_coo(1) out = paddle.sparse.tanh(sparse_x) """ diff --git a/python/paddle/sparse/functional/math.py b/python/paddle/sparse/functional/math.py index f8e1fa64d4c5a..3e964c98bc860 100644 --- a/python/paddle/sparse/functional/math.py +++ b/python/paddle/sparse/functional/math.py @@ -40,7 +40,7 @@ def sqrt(x, name=None): from paddle.fluid.framework import _test_eager_guard with _test_eager_guard(): - dense_x = paddle.to_tensor([4, 0, 1]) + dense_x = paddle.to_tensor([4, 0, 1], dtype='float32') sparse_x = dense_x.to_sparse_coo(1) out = paddle.sparse.sqrt(sparse_x) """ @@ -80,7 +80,7 @@ def sin(x, name=None): from paddle.fluid.framework import _test_eager_guard with _test_eager_guard(): - dense_x = paddle.to_tensor([-2, 0, 3]) + dense_x = paddle.to_tensor([-2, 0, 3], dtype='float32') sparse_x = dense_x.to_sparse_coo(1) out = paddle.sparse.sin(sparse_x) """ diff --git a/python/paddle/utils/code_gen/sparse_bw_api.yaml b/python/paddle/utils/code_gen/sparse_bw_api.yaml index 64483e79d54b2..d8e8aad8f98b2 100644 --- a/python/paddle/utils/code_gen/sparse_bw_api.yaml +++ b/python/paddle/utils/code_gen/sparse_bw_api.yaml @@ -66,4 +66,3 @@ output : Tensor(x_grad@SparseCooTensor) kernel : func : sparse_maxpool_grad - From fa93d7d927c59373e2c393674341a85bcea29df0 Mon Sep 17 00:00:00 2001 From: tiancaishaonvjituizi <452565578@qq.com> Date: Fri, 6 May 2022 15:29:04 +0800 Subject: [PATCH 27/31] rename to unary_kernel, update name --- .../kernels/sparse/activation_grad_kernel.h | 25 --- .../phi/kernels/sparse/activation_kernel.cc | 65 ------ paddle/phi/kernels/sparse/activation_kernel.h | 38 ---- paddle/phi/kernels/sparse/math_grad_kernel.cc | 65 ------ paddle/phi/kernels/sparse/math_grad_kernel.h | 26 --- paddle/phi/kernels/sparse/math_kernel.cc | 66 ------- .../phi/kernels/sparse/unary_grad_kernel.cc | 183 +++++++++++++++++ paddle/phi/kernels/sparse/unary_grad_kernel.h | 41 ++++ paddle/phi/kernels/sparse/unary_kernel.cc | 171 ++++++++++++++++ .../sparse/{math_kernel.h => unary_kernel.h} | 20 +- paddle/phi/kernels/sparse/utils.h | 186 ------------------ .../kernels/test_sparse_activation_dev_api.cc | 4 +- 12 files changed, 416 insertions(+), 474 deletions(-) delete mode 100644 paddle/phi/kernels/sparse/activation_grad_kernel.h delete mode 100644 paddle/phi/kernels/sparse/activation_kernel.cc delete mode 100644 paddle/phi/kernels/sparse/activation_kernel.h delete mode 100644 paddle/phi/kernels/sparse/math_grad_kernel.cc delete mode 100644 paddle/phi/kernels/sparse/math_grad_kernel.h delete mode 100644 paddle/phi/kernels/sparse/math_kernel.cc create mode 100644 paddle/phi/kernels/sparse/unary_grad_kernel.cc create mode 100644 paddle/phi/kernels/sparse/unary_grad_kernel.h create mode 100644 paddle/phi/kernels/sparse/unary_kernel.cc rename paddle/phi/kernels/sparse/{math_kernel.h => unary_kernel.h} (51%) diff --git a/paddle/phi/kernels/sparse/activation_grad_kernel.h b/paddle/phi/kernels/sparse/activation_grad_kernel.h deleted file mode 100644 index f9e5bc3c37fc5..0000000000000 --- a/paddle/phi/kernels/sparse/activation_grad_kernel.h +++ /dev/null @@ -1,25 +0,0 @@ -/* Copyright (c) 2022 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. */ - -#pragma once - -#include "paddle/phi/kernels/sparse/utils.h" - -namespace phi { -namespace sparse { - -DECLARE_SPARSE_UNARY_GRAD_KERNEL(Relu) - -} // namespace sparse -} // namespace phi diff --git a/paddle/phi/kernels/sparse/activation_kernel.cc b/paddle/phi/kernels/sparse/activation_kernel.cc deleted file mode 100644 index f67d6533adc20..0000000000000 --- a/paddle/phi/kernels/sparse/activation_kernel.cc +++ /dev/null @@ -1,65 +0,0 @@ -/* Copyright (c) 2022 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. */ - -#include "paddle/phi/kernels/sparse/activation_kernel.h" - -#include "paddle/phi/kernels/sparse/utils.h" - -DEFINE_AND_REGISTER_SPARSE_UNARY_KERNEL(tanh, TanhKernel) - -// NOTE: the following code is to bypass the restriction of Paddle -// kernel registration mechanism. Do NOT refactor them unless you -// know what you are doing. -// If you want to implement any new kernel, please follow the above -// `tanh`, do NOT follow the following `relu`. -DEFINE_SPARSE_UNARY_KERNEL(ReluKernel) - -PD_REGISTER_KERNEL(sparse_coo_relu, - CPU, - ALL_LAYOUT, - phi::sparse::SparseCooReluKernel, - float, - double) { - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); -} -PD_REGISTER_KERNEL(sparse_csr_relu, - CPU, - ALL_LAYOUT, - phi::sparse::SparseCsrReluKernel, - float, - double) { - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); -} - -#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) -PD_REGISTER_KERNEL(sparse_coo_relu, - GPU, - ALL_LAYOUT, - phi::sparse::SparseCooReluKernel, - float, - double, - phi::dtype::float16) { - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); -} - -PD_REGISTER_KERNEL(sparse_csr_relu, - GPU, - ALL_LAYOUT, - phi::sparse::SparseCsrReluKernel, - float, - double, - phi::dtype::float16) { - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); -} -#endif diff --git a/paddle/phi/kernels/sparse/activation_kernel.h b/paddle/phi/kernels/sparse/activation_kernel.h deleted file mode 100644 index 03622f5c9f77d..0000000000000 --- a/paddle/phi/kernels/sparse/activation_kernel.h +++ /dev/null @@ -1,38 +0,0 @@ -/* Copyright (c) 2022 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. */ - -#pragma once - -#include "paddle/phi/core/dense_tensor.h" -#include "paddle/phi/core/sparse_coo_tensor.h" -#include "paddle/phi/core/sparse_csr_tensor.h" -#include "paddle/phi/kernels/activation_kernel.h" -#include "paddle/phi/kernels/empty_kernel.h" -#include "paddle/phi/kernels/sparse/utils.h" - -namespace phi { -namespace sparse { - -DECLARE_SPARSE_UNARY_KERNEL(Relu) - -template -SparseCooTensor SparseRelu(const Context& dev_ctx, const SparseCooTensor& x) { - DenseTensor indices, values; - SparseCooTensor coo(indices, values, x.dims()); - SparseCooReluKernel(dev_ctx, x, &coo); - return coo; -} - -} // namespace sparse -} // namespace phi diff --git a/paddle/phi/kernels/sparse/math_grad_kernel.cc b/paddle/phi/kernels/sparse/math_grad_kernel.cc deleted file mode 100644 index 5bc39673d0f44..0000000000000 --- a/paddle/phi/kernels/sparse/math_grad_kernel.cc +++ /dev/null @@ -1,65 +0,0 @@ -// Copyright (c) 2022 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. - -#include "paddle/phi/kernels/sparse/math_grad_kernel.h" - -#include "paddle/phi/kernels/activation_grad_kernel.h" -#include "paddle/phi/kernels/sparse/utils.h" - -DEFINE_AND_REGISTER_SPARSE_UNARY_GRAD_KERNEL(sin_grad, SinGradKernel) - -// NOTE: the following code is to bypass the restriction of Paddle -// kernel registration mechanism. Do NOT refactor them unless you -// know what you are doing. -// If you want to implement any new kernel, please follow the above -// `sin_grad`, do NOT follow the following `sqrt_grad`. -DEFINE_SPARSE_UNARY_GRAD_KERNEL(SqrtGradKernel) - -PD_REGISTER_KERNEL(sparse_coo_sqrt_grad, - CPU, - ALL_LAYOUT, - phi::sparse::SparseCooSqrtGradKernel, - float, - double) { - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); -} -PD_REGISTER_KERNEL(sparse_csr_sqrt_grad, - CPU, - ALL_LAYOUT, - phi::sparse::SparseCsrSqrtGradKernel, - float, - double) { - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); -} -#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) -PD_REGISTER_KERNEL(sparse_coo_sqrt_grad, - GPU, - ALL_LAYOUT, - phi::sparse::SparseCooSqrtGradKernel, - float, - double, - phi::dtype::float16) { - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); -} - -PD_REGISTER_KERNEL(sparse_csr_sqrt_grad, - GPU, - ALL_LAYOUT, - phi::sparse::SparseCsrSqrtGradKernel, - float, - double, - phi::dtype::float16) { - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); -} -#endif diff --git a/paddle/phi/kernels/sparse/math_grad_kernel.h b/paddle/phi/kernels/sparse/math_grad_kernel.h deleted file mode 100644 index 0b6a9ce26af76..0000000000000 --- a/paddle/phi/kernels/sparse/math_grad_kernel.h +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright (c) 2022 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. - -#pragma once - -#include "paddle/phi/kernels/sparse/utils.h" - -namespace phi { -namespace sparse { - -DECLARE_SPARSE_UNARY_GRAD_KERNEL(Sqrt) -DECLARE_SPARSE_UNARY_GRAD_KERNEL(Sin) - -} // namespace sparse -} // namespace phi diff --git a/paddle/phi/kernels/sparse/math_kernel.cc b/paddle/phi/kernels/sparse/math_kernel.cc deleted file mode 100644 index 9706730e0119d..0000000000000 --- a/paddle/phi/kernels/sparse/math_kernel.cc +++ /dev/null @@ -1,66 +0,0 @@ -// Copyright (c) 2022 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. - -#include "paddle/phi/kernels/sparse/math_kernel.h" - -#include "paddle/phi/kernels/activation_kernel.h" -#include "paddle/phi/kernels/sparse/utils.h" - -DEFINE_AND_REGISTER_SPARSE_UNARY_KERNEL(sin, SinKernel) - -// NOTE: the following code is to bypass the restriction of Paddle -// kernel registration mechanism. Do NOT refactor them unless you -// know what you are doing. -// If you want to implement any new kernel, please follow the above -// `sin`, do NOT follow the following `sqrt`. -DEFINE_SPARSE_UNARY_KERNEL(SqrtKernel) - -PD_REGISTER_KERNEL(sparse_coo_sqrt, - CPU, - ALL_LAYOUT, - phi::sparse::SparseCooSqrtKernel, - float, - double) { - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); -} -PD_REGISTER_KERNEL(sparse_csr_sqrt, - CPU, - ALL_LAYOUT, - phi::sparse::SparseCsrSqrtKernel, - float, - double) { - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); -} - -#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) -PD_REGISTER_KERNEL(sparse_coo_sqrt, - GPU, - ALL_LAYOUT, - phi::sparse::SparseCooSqrtKernel, - float, - double, - phi::dtype::float16) { - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); -} - -PD_REGISTER_KERNEL(sparse_csr_sqrt, - GPU, - ALL_LAYOUT, - phi::sparse::SparseCsrSqrtKernel, - float, - double, - phi::dtype::float16) { - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); -} -#endif diff --git a/paddle/phi/kernels/sparse/unary_grad_kernel.cc b/paddle/phi/kernels/sparse/unary_grad_kernel.cc new file mode 100644 index 0000000000000..1fd3ef2711299 --- /dev/null +++ b/paddle/phi/kernels/sparse/unary_grad_kernel.cc @@ -0,0 +1,183 @@ +// Copyright (c) 2022 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. + +#include "paddle/phi/kernels/sparse/unary_grad_kernel.h" + +#include "paddle/phi/backends/cpu/cpu_context.h" +#include "paddle/phi/backends/gpu/gpu_context.h" +#include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/core/sparse_coo_tensor.h" +#include "paddle/phi/core/sparse_csr_tensor.h" +#include "paddle/phi/kernels/activation_grad_kernel.h" +#include "paddle/phi/kernels/copy_kernel.h" +#include "paddle/phi/kernels/empty_kernel.h" + +#define DEFINE_SPARSE_UNARY_GRAD_KERNEL(DenseKernelFunc) \ + namespace phi { \ + namespace sparse { \ + \ + template \ + void SparseCoo##DenseKernelFunc(const Context& dev_ctx, \ + const SparseCooTensor& x_or_out, \ + const SparseCooTensor& out_grad, \ + SparseCooTensor* x_grad) { \ + DenseTensor non_zero_indices = \ + phi::EmptyLike(dev_ctx, x_or_out.non_zero_indices()); \ + DenseTensor non_zero_elements = \ + phi::EmptyLike(dev_ctx, x_or_out.non_zero_elements()); \ + phi::Copy(dev_ctx, \ + x_or_out.non_zero_indices(), \ + dev_ctx.GetPlace(), \ + false, \ + &non_zero_indices); \ + phi::DenseKernelFunc(dev_ctx, \ + x_or_out.non_zero_elements(), \ + out_grad.non_zero_elements(), \ + &non_zero_elements); \ + x_grad->SetMember( \ + non_zero_indices, non_zero_elements, x_or_out.dims(), true); \ + } \ + \ + template \ + void SparseCsr##DenseKernelFunc(const Context& dev_ctx, \ + const SparseCsrTensor& x_or_out, \ + const SparseCsrTensor& out_grad, \ + SparseCsrTensor* out) { \ + DenseTensor non_zero_crows = \ + phi::EmptyLike(dev_ctx, x_or_out.non_zero_crows()); \ + DenseTensor non_zero_cols = \ + phi::EmptyLike(dev_ctx, x_or_out.non_zero_cols()); \ + DenseTensor non_zero_elements = \ + phi::EmptyLike(dev_ctx, x_or_out.non_zero_elements()); \ + phi::Copy(dev_ctx, \ + x_or_out.non_zero_crows(), \ + dev_ctx.GetPlace(), \ + false, \ + &non_zero_crows); \ + phi::Copy(dev_ctx, \ + x_or_out.non_zero_cols(), \ + dev_ctx.GetPlace(), \ + false, \ + &non_zero_cols); \ + phi::DenseKernelFunc(dev_ctx, \ + x_or_out.non_zero_elements(), \ + out_grad.non_zero_elements(), \ + &non_zero_elements); \ + out->SetMember( \ + non_zero_crows, non_zero_cols, non_zero_elements, x_or_out.dims()); \ + } \ + } \ + } + +#define REGISTER_CPU_SPARSE_UNARY_KERNEL(kernel_name, DenseKernelFunc) \ + PD_REGISTER_KERNEL(sparse_coo_##kernel_name, \ + CPU, \ + ALL_LAYOUT, \ + phi::sparse::SparseCoo##DenseKernelFunc, \ + float, \ + double) { \ + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); \ + } \ + PD_REGISTER_KERNEL(sparse_csr_##kernel_name, \ + CPU, \ + ALL_LAYOUT, \ + phi::sparse::SparseCsr##DenseKernelFunc, \ + float, \ + double) { \ + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); \ + } + +#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) +#define REGISTER_GPU_SPARSE_UNARY_KERNEL(kernel_name, DenseKernelFunc) \ + PD_REGISTER_KERNEL(sparse_coo_##kernel_name, \ + GPU, \ + ALL_LAYOUT, \ + phi::sparse::SparseCoo##DenseKernelFunc, \ + float, \ + double, \ + phi::dtype::float16) { \ + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); \ + } \ + \ + PD_REGISTER_KERNEL(sparse_csr_##kernel_name, \ + GPU, \ + ALL_LAYOUT, \ + phi::sparse::SparseCsr##DenseKernelFunc, \ + float, \ + double, \ + phi::dtype::float16) { \ + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); \ + } +#else +// This macro definition is empty when GPU is disabled +#define REGISTER_GPU_SPARSE_UNARY_KERNEL(sparse_kernel_name, DenseKernelFunc) +#endif + +#define REGISTER_SPARSE_UNARY_KERNEL(kernel_name, DenseKernelFunc) \ + REGISTER_CPU_SPARSE_UNARY_KERNEL(kernel_name, DenseKernelFunc) \ + REGISTER_GPU_SPARSE_UNARY_KERNEL(kernel_name, DenseKernelFunc) + +#define DEFINE_AND_REGISTER_SPARSE_UNARY_GRAD_KERNEL(kernel_name, \ + DenseKernelFunc) \ + DEFINE_SPARSE_UNARY_GRAD_KERNEL(DenseKernelFunc) \ + REGISTER_SPARSE_UNARY_KERNEL(kernel_name, DenseKernelFunc) + +// NOTE: the following code is to bypass the restriction of Paddle +// kernel registration mechanism. Do NOT refactor them unless you +// know what you are doing. +// If you want to implement any new kernel, please follow `sin_grad`, +// `tanh_grad` etc, do NOT follow the following `relu_grad`. +DEFINE_SPARSE_UNARY_GRAD_KERNEL(ReluGradKernel) + +PD_REGISTER_KERNEL(sparse_coo_relu_grad, + CPU, + ALL_LAYOUT, + phi::sparse::SparseCooReluGradKernel, + float, + double) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); +} +PD_REGISTER_KERNEL(sparse_csr_relu_grad, + CPU, + ALL_LAYOUT, + phi::sparse::SparseCsrReluGradKernel, + float, + double) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); +} +#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) +PD_REGISTER_KERNEL(sparse_coo_relu_grad, + GPU, + ALL_LAYOUT, + phi::sparse::SparseCooReluGradKernel, + float, + double, + phi::dtype::float16) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); +} + +PD_REGISTER_KERNEL(sparse_csr_relu_grad, + GPU, + ALL_LAYOUT, + phi::sparse::SparseCsrReluGradKernel, + float, + double, + phi::dtype::float16) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); +} +#endif + +DEFINE_AND_REGISTER_SPARSE_UNARY_GRAD_KERNEL(sin_grad, SinGradKernel) +DEFINE_AND_REGISTER_SPARSE_UNARY_GRAD_KERNEL(sqrt_grad, SqrtGradKernel) +DEFINE_AND_REGISTER_SPARSE_UNARY_GRAD_KERNEL(tanh_grad, TanhGradKernel) diff --git a/paddle/phi/kernels/sparse/unary_grad_kernel.h b/paddle/phi/kernels/sparse/unary_grad_kernel.h new file mode 100644 index 0000000000000..24ea4fee1a4fd --- /dev/null +++ b/paddle/phi/kernels/sparse/unary_grad_kernel.h @@ -0,0 +1,41 @@ +// Copyright (c) 2022 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. + +#pragma once + +#include "paddle/phi/core/sparse_coo_tensor.h" +#include "paddle/phi/core/sparse_csr_tensor.h" + +#define DECLARE_SPARSE_UNARY_GRAD_KERNEL(name) \ + template \ + void SparseCoo##name##GradKernel(const Context& dev_ctx, \ + const SparseCooTensor& x, \ + const SparseCooTensor& out_grad, \ + SparseCooTensor* x_grad); \ + \ + template \ + void SparseCsr##name##GradKernel(const Context& dev_ctx, \ + const SparseCsrTensor& x, \ + const SparseCsrTensor& out_grad, \ + SparseCsrTensor* x_grad); + +namespace phi { +namespace sparse { + +DECLARE_SPARSE_UNARY_GRAD_KERNEL(Relu) +DECLARE_SPARSE_UNARY_GRAD_KERNEL(Sqrt) +DECLARE_SPARSE_UNARY_GRAD_KERNEL(Sin) + +} // namespace sparse +} // namespace phi diff --git a/paddle/phi/kernels/sparse/unary_kernel.cc b/paddle/phi/kernels/sparse/unary_kernel.cc new file mode 100644 index 0000000000000..97dd8f0d67c16 --- /dev/null +++ b/paddle/phi/kernels/sparse/unary_kernel.cc @@ -0,0 +1,171 @@ +// Copyright (c) 2022 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. + +#include "paddle/phi/kernels/sparse/unary_kernel.h" + +#include "paddle/phi/kernels/activation_kernel.h" +#include "paddle/phi/kernels/sparse/utils.h" + +#define DEFINE_SPARSE_UNARY_KERNEL(DenseKernelFunc) \ + namespace phi { \ + namespace sparse { \ + \ + template \ + void SparseCoo##DenseKernelFunc(const Context& dev_ctx, \ + const SparseCooTensor& x, \ + SparseCooTensor* out) { \ + DenseTensor non_zero_indices = \ + phi::EmptyLike(dev_ctx, x.non_zero_indices()); \ + DenseTensor non_zero_elements = \ + phi::EmptyLike(dev_ctx, x.non_zero_elements()); \ + phi::Copy(dev_ctx, \ + x.non_zero_indices(), \ + dev_ctx.GetPlace(), \ + false, \ + &non_zero_indices); \ + phi::DenseKernelFunc( \ + dev_ctx, x.non_zero_elements(), &non_zero_elements); \ + out->SetMember(non_zero_indices, non_zero_elements, x.dims(), true); \ + } \ + \ + template \ + void SparseCsr##DenseKernelFunc(const Context& dev_ctx, \ + const SparseCsrTensor& x, \ + SparseCsrTensor* out) { \ + DenseTensor non_zero_crows = \ + phi::EmptyLike(dev_ctx, x.non_zero_crows()); \ + DenseTensor non_zero_cols = \ + phi::EmptyLike(dev_ctx, x.non_zero_cols()); \ + DenseTensor non_zero_elements = \ + phi::EmptyLike(dev_ctx, x.non_zero_elements()); \ + phi::Copy(dev_ctx, \ + x.non_zero_crows(), \ + dev_ctx.GetPlace(), \ + false, \ + &non_zero_crows); \ + phi::Copy(dev_ctx, \ + x.non_zero_cols(), \ + dev_ctx.GetPlace(), \ + false, \ + &non_zero_cols); \ + phi::DenseKernelFunc( \ + dev_ctx, x.non_zero_elements(), &non_zero_elements); \ + out->SetMember( \ + non_zero_crows, non_zero_cols, non_zero_elements, x.dims()); \ + } \ + } \ + } + +#define REGISTER_CPU_SPARSE_UNARY_KERNEL(kernel_name, DenseKernelFunc) \ + PD_REGISTER_KERNEL(sparse_coo_##kernel_name, \ + CPU, \ + ALL_LAYOUT, \ + phi::sparse::SparseCoo##DenseKernelFunc, \ + float, \ + double) { \ + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); \ + } \ + PD_REGISTER_KERNEL(sparse_csr_##kernel_name, \ + CPU, \ + ALL_LAYOUT, \ + phi::sparse::SparseCsr##DenseKernelFunc, \ + float, \ + double) { \ + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); \ + } + +#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) +#define REGISTER_GPU_SPARSE_UNARY_KERNEL(kernel_name, DenseKernelFunc) \ + PD_REGISTER_KERNEL(sparse_coo_##kernel_name, \ + GPU, \ + ALL_LAYOUT, \ + phi::sparse::SparseCoo##DenseKernelFunc, \ + float, \ + double, \ + phi::dtype::float16) { \ + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); \ + } \ + \ + PD_REGISTER_KERNEL(sparse_csr_##kernel_name, \ + GPU, \ + ALL_LAYOUT, \ + phi::sparse::SparseCsr##DenseKernelFunc, \ + float, \ + double, \ + phi::dtype::float16) { \ + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); \ + } +#else +// This macro definition is empty when GPU is disabled +#define REGISTER_GPU_SPARSE_UNARY_KERNEL(sparse_kernel_name, DenseKernelFunc) +#endif + +#define REGISTER_SPARSE_UNARY_KERNEL(kernel_name, DenseKernelFunc) \ + REGISTER_CPU_SPARSE_UNARY_KERNEL(kernel_name, DenseKernelFunc) \ + REGISTER_GPU_SPARSE_UNARY_KERNEL(kernel_name, DenseKernelFunc) + +#define DEFINE_AND_REGISTER_SPARSE_UNARY_KERNEL(kernel_name, DenseKernelFunc) \ + DEFINE_SPARSE_UNARY_KERNEL(DenseKernelFunc) \ + REGISTER_SPARSE_UNARY_KERNEL(kernel_name, DenseKernelFunc) + +// NOTE: the following code is to bypass the restriction of Paddle +// kernel registration mechanism. Do NOT refactor them unless you +// know what you are doing. +// If you want to implement any new kernel, please follow `sin`, +// `tanh` etc, do NOT follow `sqrt`. +DEFINE_SPARSE_UNARY_KERNEL(SqrtKernel) + +PD_REGISTER_KERNEL(sparse_coo_sqrt, + CPU, + ALL_LAYOUT, + phi::sparse::SparseCooSqrtKernel, + float, + double) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); +} +PD_REGISTER_KERNEL(sparse_csr_sqrt, + CPU, + ALL_LAYOUT, + phi::sparse::SparseCsrSqrtKernel, + float, + double) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); +} + +#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) +PD_REGISTER_KERNEL(sparse_coo_sqrt, + GPU, + ALL_LAYOUT, + phi::sparse::SparseCooSqrtKernel, + float, + double, + phi::dtype::float16) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); +} + +PD_REGISTER_KERNEL(sparse_csr_sqrt, + GPU, + ALL_LAYOUT, + phi::sparse::SparseCsrSqrtKernel, + float, + double, + phi::dtype::float16) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); +} + +#endif + +DEFINE_AND_REGISTER_SPARSE_UNARY_KERNEL(sin, SinKernel) +DEFINE_AND_REGISTER_SPARSE_UNARY_KERNEL(tanh, TanhKernel) +DEFINE_AND_REGISTER_SPARSE_UNARY_KERNEL(relu, ReluKernel) diff --git a/paddle/phi/kernels/sparse/math_kernel.h b/paddle/phi/kernels/sparse/unary_kernel.h similarity index 51% rename from paddle/phi/kernels/sparse/math_kernel.h rename to paddle/phi/kernels/sparse/unary_kernel.h index b48f04f7a0f0f..2aa056eec4360 100644 --- a/paddle/phi/kernels/sparse/math_kernel.h +++ b/paddle/phi/kernels/sparse/unary_kernel.h @@ -19,13 +19,31 @@ #include "paddle/phi/core/sparse_csr_tensor.h" #include "paddle/phi/kernels/activation_kernel.h" #include "paddle/phi/kernels/empty_kernel.h" -#include "paddle/phi/kernels/sparse/utils.h" + +#define DECLARE_SPARSE_UNARY_KERNEL(name) \ + template \ + void SparseCoo##name##Kernel( \ + const Context& dev_ctx, const SparseCooTensor& x, SparseCooTensor* out); \ + \ + template \ + void SparseCsr##name##Kernel( \ + const Context& dev_ctx, const SparseCsrTensor& x, SparseCsrTensor* out); namespace phi { namespace sparse { +DECLARE_SPARSE_UNARY_KERNEL(Relu) DECLARE_SPARSE_UNARY_KERNEL(Sqrt) DECLARE_SPARSE_UNARY_KERNEL(Sin) +template +SparseCooTensor SparseRelu(const Context& dev_ctx, const SparseCooTensor& x) { + DenseTensor indices, values; + SparseCooTensor coo(indices, values, x.dims()); + SparseCooReluKernel(dev_ctx, x, &coo); + return coo; +} + + } // namespace sparse } // namespace phi diff --git a/paddle/phi/kernels/sparse/utils.h b/paddle/phi/kernels/sparse/utils.h index 9c19219e8518e..9b8d9459561bf 100644 --- a/paddle/phi/kernels/sparse/utils.h +++ b/paddle/phi/kernels/sparse/utils.h @@ -20,189 +20,3 @@ #include "paddle/phi/kernels/copy_kernel.h" #include "paddle/phi/kernels/empty_kernel.h" -#define DECLARE_SPARSE_UNARY_KERNEL(name) \ - template \ - void SparseCoo##name##Kernel( \ - const Context& dev_ctx, const SparseCooTensor& x, SparseCooTensor* out); \ - \ - template \ - void SparseCsr##name##Kernel( \ - const Context& dev_ctx, const SparseCsrTensor& x, SparseCsrTensor* out); - -#define DECLARE_SPARSE_UNARY_GRAD_KERNEL(name) \ - template \ - void SparseCoo##name##GradKernel(const Context& dev_ctx, \ - const SparseCooTensor& x, \ - const SparseCooTensor& out_grad, \ - SparseCooTensor* x_grad); \ - \ - template \ - void SparseCsr##name##GradKernel(const Context& dev_ctx, \ - const SparseCsrTensor& x, \ - const SparseCsrTensor& out_grad, \ - SparseCsrTensor* x_grad); - -#define DEFINE_SPARSE_UNARY_KERNEL(dense_kernel_func) \ - namespace phi { \ - namespace sparse { \ - \ - template \ - void SparseCoo##dense_kernel_func(const Context& dev_ctx, \ - const SparseCooTensor& x, \ - SparseCooTensor* out) { \ - DenseTensor non_zero_indices = \ - phi::EmptyLike(dev_ctx, x.non_zero_indices()); \ - DenseTensor non_zero_elements = \ - phi::EmptyLike(dev_ctx, x.non_zero_elements()); \ - phi::Copy(dev_ctx, \ - x.non_zero_indices(), \ - dev_ctx.GetPlace(), \ - false, \ - &non_zero_indices); \ - phi::dense_kernel_func( \ - dev_ctx, x.non_zero_elements(), &non_zero_elements); \ - out->SetMember(non_zero_indices, non_zero_elements, x.dims(), true); \ - } \ - \ - template \ - void SparseCsr##dense_kernel_func(const Context& dev_ctx, \ - const SparseCsrTensor& x, \ - SparseCsrTensor* out) { \ - DenseTensor non_zero_crows = \ - phi::EmptyLike(dev_ctx, x.non_zero_crows()); \ - DenseTensor non_zero_cols = \ - phi::EmptyLike(dev_ctx, x.non_zero_cols()); \ - DenseTensor non_zero_elements = \ - phi::EmptyLike(dev_ctx, x.non_zero_elements()); \ - phi::Copy(dev_ctx, \ - x.non_zero_crows(), \ - dev_ctx.GetPlace(), \ - false, \ - &non_zero_crows); \ - phi::Copy(dev_ctx, \ - x.non_zero_cols(), \ - dev_ctx.GetPlace(), \ - false, \ - &non_zero_cols); \ - phi::dense_kernel_func( \ - dev_ctx, x.non_zero_elements(), &non_zero_elements); \ - out->SetMember( \ - non_zero_crows, non_zero_cols, non_zero_elements, x.dims()); \ - } \ - } \ - } - -#define DEFINE_SPARSE_UNARY_GRAD_KERNEL(dense_kernel_func) \ - namespace phi { \ - namespace sparse { \ - \ - template \ - void SparseCoo##dense_kernel_func(const Context& dev_ctx, \ - const SparseCooTensor& x_or_out, \ - const SparseCooTensor& out_grad, \ - SparseCooTensor* x_grad) { \ - DenseTensor non_zero_indices = \ - phi::EmptyLike(dev_ctx, x_or_out.non_zero_indices()); \ - DenseTensor non_zero_elements = \ - phi::EmptyLike(dev_ctx, x_or_out.non_zero_elements()); \ - phi::Copy(dev_ctx, \ - x_or_out.non_zero_indices(), \ - dev_ctx.GetPlace(), \ - false, \ - &non_zero_indices); \ - phi::dense_kernel_func(dev_ctx, \ - x_or_out.non_zero_elements(), \ - out_grad.non_zero_elements(), \ - &non_zero_elements); \ - x_grad->SetMember( \ - non_zero_indices, non_zero_elements, x_or_out.dims(), true); \ - } \ - \ - template \ - void SparseCsr##dense_kernel_func(const Context& dev_ctx, \ - const SparseCsrTensor& x_or_out, \ - const SparseCsrTensor& out_grad, \ - SparseCsrTensor* out) { \ - DenseTensor non_zero_crows = \ - phi::EmptyLike(dev_ctx, x_or_out.non_zero_crows()); \ - DenseTensor non_zero_cols = \ - phi::EmptyLike(dev_ctx, x_or_out.non_zero_cols()); \ - DenseTensor non_zero_elements = \ - phi::EmptyLike(dev_ctx, x_or_out.non_zero_elements()); \ - phi::Copy(dev_ctx, \ - x_or_out.non_zero_crows(), \ - dev_ctx.GetPlace(), \ - false, \ - &non_zero_crows); \ - phi::Copy(dev_ctx, \ - x_or_out.non_zero_cols(), \ - dev_ctx.GetPlace(), \ - false, \ - &non_zero_cols); \ - phi::dense_kernel_func(dev_ctx, \ - x_or_out.non_zero_elements(), \ - out_grad.non_zero_elements(), \ - &non_zero_elements); \ - out->SetMember( \ - non_zero_crows, non_zero_cols, non_zero_elements, x_or_out.dims()); \ - } \ - } \ - } - -#define REGISTER_CPU_SPARSE_UNARY_KERNEL(kernel_name, dense_kernel_func) \ - PD_REGISTER_KERNEL(sparse_coo_##kernel_name, \ - CPU, \ - ALL_LAYOUT, \ - phi::sparse::SparseCoo##dense_kernel_func, \ - float, \ - double) { \ - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); \ - } \ - PD_REGISTER_KERNEL(sparse_csr_##kernel_name, \ - CPU, \ - ALL_LAYOUT, \ - phi::sparse::SparseCsr##dense_kernel_func, \ - float, \ - double) { \ - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); \ - } - -#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) -#define REGISTER_GPU_SPARSE_UNARY_KERNEL(kernel_name, dense_kernel_func) \ - PD_REGISTER_KERNEL(sparse_coo_##kernel_name, \ - GPU, \ - ALL_LAYOUT, \ - phi::sparse::SparseCoo##dense_kernel_func, \ - float, \ - double, \ - phi::dtype::float16) { \ - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); \ - } \ - \ - PD_REGISTER_KERNEL(sparse_csr_##kernel_name, \ - GPU, \ - ALL_LAYOUT, \ - phi::sparse::SparseCsr##dense_kernel_func, \ - float, \ - double, \ - phi::dtype::float16) { \ - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); \ - } -#else -// This macro definition is empty when GPU is disabled -#define REGISTER_GPU_SPARSE_UNARY_KERNEL(sparse_kernel_name, dense_kernel_func) -#endif - -#define REGISTER_SPARSE_UNARY_KERNEL(kernel_name, dense_kernel_func) \ - REGISTER_CPU_SPARSE_UNARY_KERNEL(kernel_name, dense_kernel_func) \ - REGISTER_GPU_SPARSE_UNARY_KERNEL(kernel_name, dense_kernel_func) - -#define DEFINE_AND_REGISTER_SPARSE_UNARY_KERNEL(kernel_name, \ - dense_kernel_func) \ - DEFINE_SPARSE_UNARY_KERNEL(dense_kernel_func) \ - REGISTER_SPARSE_UNARY_KERNEL(kernel_name, dense_kernel_func) - -#define DEFINE_AND_REGISTER_SPARSE_UNARY_GRAD_KERNEL(kernel_name, \ - dense_kernel_func) \ - DEFINE_SPARSE_UNARY_GRAD_KERNEL(dense_kernel_func) \ - REGISTER_SPARSE_UNARY_KERNEL(kernel_name, dense_kernel_func) diff --git a/paddle/phi/tests/kernels/test_sparse_activation_dev_api.cc b/paddle/phi/tests/kernels/test_sparse_activation_dev_api.cc index f86170980ffbb..6de51fc4b77a2 100644 --- a/paddle/phi/tests/kernels/test_sparse_activation_dev_api.cc +++ b/paddle/phi/tests/kernels/test_sparse_activation_dev_api.cc @@ -24,8 +24,8 @@ limitations under the License. */ #include "paddle/phi/kernels/activation_grad_kernel.h" #include "paddle/phi/kernels/activation_kernel.h" #include "paddle/phi/kernels/empty_kernel.h" -#include "paddle/phi/kernels/sparse/activation_grad_kernel.h" -#include "paddle/phi/kernels/sparse/activation_kernel.h" +#include "paddle/phi/kernels/sparse/unary_grad_kernel.h" +#include "paddle/phi/kernels/sparse/unary_kernel.h" #include "paddle/phi/kernels/sparse/sparse_utils_kernel.h" namespace phi { From 67d14b4adb6fee52dfb1714357f7ea1f828ec030 Mon Sep 17 00:00:00 2001 From: tiancaishaonvjituizi <452565578@qq.com> Date: Fri, 6 May 2022 15:34:30 +0800 Subject: [PATCH 28/31] remove unused files --- .../kernels/sparse/activation_grad_kernel.cc | 65 ------------------- paddle/phi/kernels/sparse/unary_kernel.cc | 8 ++- paddle/phi/kernels/sparse/utils.h | 22 ------- 3 files changed, 7 insertions(+), 88 deletions(-) delete mode 100644 paddle/phi/kernels/sparse/activation_grad_kernel.cc delete mode 100644 paddle/phi/kernels/sparse/utils.h diff --git a/paddle/phi/kernels/sparse/activation_grad_kernel.cc b/paddle/phi/kernels/sparse/activation_grad_kernel.cc deleted file mode 100644 index b16897cd76b6c..0000000000000 --- a/paddle/phi/kernels/sparse/activation_grad_kernel.cc +++ /dev/null @@ -1,65 +0,0 @@ -/* Copyright (c) 2022 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. */ - -#include "paddle/phi/kernels/sparse/activation_grad_kernel.h" - -#include "paddle/phi/kernels/activation_grad_kernel.h" -#include "paddle/phi/kernels/sparse/utils.h" - -DEFINE_AND_REGISTER_SPARSE_UNARY_GRAD_KERNEL(tanh_grad, TanhGradKernel) - -// NOTE: the following code is to bypass the restriction of Paddle -// kernel registration mechanism. Do NOT refactor them unless you -// know what you are doing. -// If you want to implement any new kernel, please follow the above -// `tanh_grad`, do NOT follow the following `relu_grad`. -DEFINE_SPARSE_UNARY_GRAD_KERNEL(ReluGradKernel) - -PD_REGISTER_KERNEL(sparse_coo_relu_grad, - CPU, - ALL_LAYOUT, - phi::sparse::SparseCooReluGradKernel, - float, - double) { - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); -} -PD_REGISTER_KERNEL(sparse_csr_relu_grad, - CPU, - ALL_LAYOUT, - phi::sparse::SparseCsrReluGradKernel, - float, - double) { - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); -} -#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) -PD_REGISTER_KERNEL(sparse_coo_relu_grad, - GPU, - ALL_LAYOUT, - phi::sparse::SparseCooReluGradKernel, - float, - double, - phi::dtype::float16) { - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); -} - -PD_REGISTER_KERNEL(sparse_csr_relu_grad, - GPU, - ALL_LAYOUT, - phi::sparse::SparseCsrReluGradKernel, - float, - double, - phi::dtype::float16) { - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); -} -#endif diff --git a/paddle/phi/kernels/sparse/unary_kernel.cc b/paddle/phi/kernels/sparse/unary_kernel.cc index 97dd8f0d67c16..e02d7757664fa 100644 --- a/paddle/phi/kernels/sparse/unary_kernel.cc +++ b/paddle/phi/kernels/sparse/unary_kernel.cc @@ -14,8 +14,14 @@ #include "paddle/phi/kernels/sparse/unary_kernel.h" +#include "paddle/phi/backends/cpu/cpu_context.h" +#include "paddle/phi/backends/gpu/gpu_context.h" +#include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/core/sparse_coo_tensor.h" +#include "paddle/phi/core/sparse_csr_tensor.h" #include "paddle/phi/kernels/activation_kernel.h" -#include "paddle/phi/kernels/sparse/utils.h" +#include "paddle/phi/kernels/copy_kernel.h" +#include "paddle/phi/kernels/empty_kernel.h" #define DEFINE_SPARSE_UNARY_KERNEL(DenseKernelFunc) \ namespace phi { \ diff --git a/paddle/phi/kernels/sparse/utils.h b/paddle/phi/kernels/sparse/utils.h deleted file mode 100644 index 9b8d9459561bf..0000000000000 --- a/paddle/phi/kernels/sparse/utils.h +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright (c) 2022 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. - -#include "paddle/phi/backends/cpu/cpu_context.h" -#include "paddle/phi/backends/gpu/gpu_context.h" -#include "paddle/phi/core/kernel_registry.h" -#include "paddle/phi/core/sparse_coo_tensor.h" -#include "paddle/phi/core/sparse_csr_tensor.h" -#include "paddle/phi/kernels/copy_kernel.h" -#include "paddle/phi/kernels/empty_kernel.h" - From 268ac349150c64e17cb18ba57725869670624a9b Mon Sep 17 00:00:00 2001 From: tiancaishaonvjituizi <452565578@qq.com> Date: Fri, 6 May 2022 17:39:41 +0800 Subject: [PATCH 29/31] rename python files --- python/paddle/sparse/functional/__init__.py | 8 +- python/paddle/sparse/functional/math.py | 97 ------------------- .../functional/{activation.py => unary.py} | 80 +++++++++++++++ python/paddle/sparse/layer/__init__.py | 2 +- .../sparse/layer/{activation.py => unary.py} | 0 5 files changed, 85 insertions(+), 102 deletions(-) delete mode 100644 python/paddle/sparse/functional/math.py rename python/paddle/sparse/functional/{activation.py => unary.py} (56%) rename python/paddle/sparse/layer/{activation.py => unary.py} (100%) diff --git a/python/paddle/sparse/functional/__init__.py b/python/paddle/sparse/functional/__init__.py index 67b31cad0ab52..cfefa3ff4ff76 100644 --- a/python/paddle/sparse/functional/__init__.py +++ b/python/paddle/sparse/functional/__init__.py @@ -12,10 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -from .activation import relu # noqa: F401 -from .activation import tanh # noqa: F401 -from .math import sqrt # noqa: F401 -from .math import sin # noqa: F401 +from .unary import relu # noqa: F401 +from .unary import tanh # noqa: F401 +from .unary import sqrt # noqa: F401 +from .unary import sin # noqa: F401 from .conv import conv3d # noqa: F401 from .conv import subm_conv3d # noqa: F401 from .pooling import max_pool3d # noqa: F401 diff --git a/python/paddle/sparse/functional/math.py b/python/paddle/sparse/functional/math.py deleted file mode 100644 index 3e964c98bc860..0000000000000 --- a/python/paddle/sparse/functional/math.py +++ /dev/null @@ -1,97 +0,0 @@ -# Copyright (c) 2022 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. - -__all__ = [] - -from paddle import _C_ops, in_dynamic_mode - - -def sqrt(x, name=None): - """ - Calculate square root of x, requiring x to be a sparse coo or sparse csr tensor. - - .. math:: - - out = sqrt(x) - - Parameters: - x (Tensor): The input Sparse Tensor with data type float32, float64. - name (str, optional): Name for the operation (optional, default is None). - For more information, please refer to :ref:`api_guide_Name`. - - Returns: - A Sparse Tensor with the same data type and shape as ``x`` . - - Examples: - .. code-block:: python - - import paddle - from paddle.fluid.framework import _test_eager_guard - - with _test_eager_guard(): - dense_x = paddle.to_tensor([4, 0, 1], dtype='float32') - sparse_x = dense_x.to_sparse_coo(1) - out = paddle.sparse.sqrt(sparse_x) - """ - - assert in_dynamic_mode(), "Currently, Sparse API only support dynamic mode" - - if x.is_sparse_coo(): - return _C_ops.final_state_sparse_coo_sqrt(x) - elif x.is_sparse_csr(): - return _C_ops.final_state_sparse_csr_sqrt(x) - else: - raise ValueError( - "Currently, sparse.sqrt only support the input of SparseCooTensor or SparseCsrTensor" - ) - - -def sin(x, name=None): - """ - Calculate sin of x, requiring x to be a sparse coo or sparse csr tensor. - - .. math:: - - out = sin(x) - - Parameters: - x (Tensor): The input Sparse Tensor with data type float32, float64. - name (str, optional): Name for the operation (optional, default is None). - For more information, please refer to :ref:`api_guide_Name`. - - Returns: - A Sparse Tensor with the same data type and shape as ``x`` . - - Examples: - .. code-block:: python - - import paddle - from paddle.fluid.framework import _test_eager_guard - - with _test_eager_guard(): - dense_x = paddle.to_tensor([-2, 0, 3], dtype='float32') - sparse_x = dense_x.to_sparse_coo(1) - out = paddle.sparse.sin(sparse_x) - """ - - assert in_dynamic_mode(), "Currently, Sparse API only support dynamic mode" - - if x.is_sparse_coo(): - return _C_ops.final_state_sparse_coo_sin(x) - elif x.is_sparse_csr(): - return _C_ops.final_state_sparse_csr_sin(x) - else: - raise ValueError( - "Currently, sparse.sin only support the input of SparseCooTensor or SparseCsrTensor" - ) diff --git a/python/paddle/sparse/functional/activation.py b/python/paddle/sparse/functional/unary.py similarity index 56% rename from python/paddle/sparse/functional/activation.py rename to python/paddle/sparse/functional/unary.py index 97879677416cd..860b4025d89e0 100644 --- a/python/paddle/sparse/functional/activation.py +++ b/python/paddle/sparse/functional/unary.py @@ -95,3 +95,83 @@ def tanh(x, name=None): raise ValueError( "Currently, sparse.tanh only support the input of SparseCooTensor or SparseCsrTensor" ) + + +def sqrt(x, name=None): + """ + Calculate square root of x, requiring x to be a sparse coo or sparse csr tensor. + + .. math:: + + out = sqrt(x) + + Parameters: + x (Tensor): The input Sparse Tensor with data type float32, float64. + name (str, optional): Name for the operation (optional, default is None). + For more information, please refer to :ref:`api_guide_Name`. + + Returns: + A Sparse Tensor with the same data type and shape as ``x`` . + + Examples: + .. code-block:: python + + import paddle + from paddle.fluid.framework import _test_eager_guard + + with _test_eager_guard(): + dense_x = paddle.to_tensor([4, 0, 1], dtype='float32') + sparse_x = dense_x.to_sparse_coo(1) + out = paddle.sparse.sqrt(sparse_x) + """ + + assert in_dynamic_mode(), "Currently, Sparse API only support dynamic mode" + + if x.is_sparse_coo(): + return _C_ops.final_state_sparse_coo_sqrt(x) + elif x.is_sparse_csr(): + return _C_ops.final_state_sparse_csr_sqrt(x) + else: + raise ValueError( + "Currently, sparse.sqrt only support the input of SparseCooTensor or SparseCsrTensor" + ) + + +def sin(x, name=None): + """ + Calculate sin of x, requiring x to be a sparse coo or sparse csr tensor. + + .. math:: + + out = sin(x) + + Parameters: + x (Tensor): The input Sparse Tensor with data type float32, float64. + name (str, optional): Name for the operation (optional, default is None). + For more information, please refer to :ref:`api_guide_Name`. + + Returns: + A Sparse Tensor with the same data type and shape as ``x`` . + + Examples: + .. code-block:: python + + import paddle + from paddle.fluid.framework import _test_eager_guard + + with _test_eager_guard(): + dense_x = paddle.to_tensor([-2, 0, 3], dtype='float32') + sparse_x = dense_x.to_sparse_coo(1) + out = paddle.sparse.sin(sparse_x) + """ + + assert in_dynamic_mode(), "Currently, Sparse API only support dynamic mode" + + if x.is_sparse_coo(): + return _C_ops.final_state_sparse_coo_sin(x) + elif x.is_sparse_csr(): + return _C_ops.final_state_sparse_csr_sin(x) + else: + raise ValueError( + "Currently, sparse.sin only support the input of SparseCooTensor or SparseCsrTensor" + ) diff --git a/python/paddle/sparse/layer/__init__.py b/python/paddle/sparse/layer/__init__.py index 3a6d99392e4e8..8a814b514276f 100644 --- a/python/paddle/sparse/layer/__init__.py +++ b/python/paddle/sparse/layer/__init__.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from .activation import ReLU +from .unary import ReLU from .norm import BatchNorm from .conv import Conv3D from .conv import SubmConv3D diff --git a/python/paddle/sparse/layer/activation.py b/python/paddle/sparse/layer/unary.py similarity index 100% rename from python/paddle/sparse/layer/activation.py rename to python/paddle/sparse/layer/unary.py From 39c9750e4169f6f6ebdff7bbd209b0c647f23c55 Mon Sep 17 00:00:00 2001 From: tiancaishaonvjituizi <452565578@qq.com> Date: Sat, 7 May 2022 14:29:22 +0800 Subject: [PATCH 30/31] fix import path --- python/paddle/sparse/__init__.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/python/paddle/sparse/__init__.py b/python/paddle/sparse/__init__.py index 17eab93dab18e..26a2f0cfadbe7 100644 --- a/python/paddle/sparse/__init__.py +++ b/python/paddle/sparse/__init__.py @@ -14,13 +14,13 @@ from .creation import sparse_coo_tensor from .creation import sparse_csr_tensor -from .layer.activation import ReLU -from .layer.norm import BatchNorm +from .layer import ReLU +from .layer import BatchNorm -from .layer.conv import Conv3D -from .layer.conv import SubmConv3D +from .layer import Conv3D +from .layer import SubmConv3D -from .layer.pooling import MaxPool3D +from .layer import MaxPool3D from .functional import sqrt from .functional import sin From 06787c0edffdfff0ebd5a01664d0404711c685dd Mon Sep 17 00:00:00 2001 From: tiancaishaonvjituizi <452565578@qq.com> Date: Mon, 9 May 2022 17:58:34 +0800 Subject: [PATCH 31/31] reformat --- paddle/phi/kernels/sparse/unary_kernel.h | 1 - paddle/phi/tests/kernels/test_sparse_activation_dev_api.cc | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/paddle/phi/kernels/sparse/unary_kernel.h b/paddle/phi/kernels/sparse/unary_kernel.h index 2aa056eec4360..4470173c143db 100644 --- a/paddle/phi/kernels/sparse/unary_kernel.h +++ b/paddle/phi/kernels/sparse/unary_kernel.h @@ -44,6 +44,5 @@ SparseCooTensor SparseRelu(const Context& dev_ctx, const SparseCooTensor& x) { return coo; } - } // namespace sparse } // namespace phi diff --git a/paddle/phi/tests/kernels/test_sparse_activation_dev_api.cc b/paddle/phi/tests/kernels/test_sparse_activation_dev_api.cc index 6de51fc4b77a2..05781156cd1d6 100644 --- a/paddle/phi/tests/kernels/test_sparse_activation_dev_api.cc +++ b/paddle/phi/tests/kernels/test_sparse_activation_dev_api.cc @@ -24,9 +24,9 @@ limitations under the License. */ #include "paddle/phi/kernels/activation_grad_kernel.h" #include "paddle/phi/kernels/activation_kernel.h" #include "paddle/phi/kernels/empty_kernel.h" +#include "paddle/phi/kernels/sparse/sparse_utils_kernel.h" #include "paddle/phi/kernels/sparse/unary_grad_kernel.h" #include "paddle/phi/kernels/sparse/unary_kernel.h" -#include "paddle/phi/kernels/sparse/sparse_utils_kernel.h" namespace phi { namespace tests {