From 4235a2df6a9f33554e672a1ad3ad504e25dc5fa6 Mon Sep 17 00:00:00 2001 From: bapijun Date: Mon, 26 Feb 2024 15:43:35 +0800 Subject: [PATCH 01/25] =?UTF-8?q?=E9=99=A4=E4=BA=86to=5Fdense=20bug?= =?UTF-8?q?=E6=B2=A1=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kernels/sparse/cpu/sparse_utils_kernel.cc | 27 ++++++++++++------- .../kernels/sparse/cpu/unary_grad_kernel.cc | 16 ++++++++++- paddle/phi/kernels/sparse/cpu/unary_kernel.cc | 21 ++++++++++++++- paddle/phi/kernels/sparse/empty_kernel.cc | 6 +++-- 4 files changed, 57 insertions(+), 13 deletions(-) diff --git a/paddle/phi/kernels/sparse/cpu/sparse_utils_kernel.cc b/paddle/phi/kernels/sparse/cpu/sparse_utils_kernel.cc index 6ec3f747dd381..aece8f059c7e0 100644 --- a/paddle/phi/kernels/sparse/cpu/sparse_utils_kernel.cc +++ b/paddle/phi/kernels/sparse/cpu/sparse_utils_kernel.cc @@ -328,7 +328,8 @@ PD_REGISTER_KERNEL(dense_to_coo, int8_t, int16_t, int, - int64_t) {} + int64_t,phi::dtype::complex, + phi::dtype::complex) {} PD_REGISTER_KERNEL(csr_to_coo, CPU, @@ -342,7 +343,8 @@ PD_REGISTER_KERNEL(csr_to_coo, int16_t, int, int64_t, - bool) {} + bool,phi::dtype::complex, + phi::dtype::complex) {} PD_REGISTER_KERNEL(coo_to_csr, CPU, @@ -356,7 +358,8 @@ PD_REGISTER_KERNEL(coo_to_csr, int16_t, int, int64_t, - bool) {} + bool,phi::dtype::complex, + phi::dtype::complex) {} PD_REGISTER_KERNEL(dense_to_csr, CPU, @@ -369,7 +372,8 @@ PD_REGISTER_KERNEL(dense_to_csr, int8_t, int16_t, int, - int64_t) {} + int64_t,phi::dtype::complex, + phi::dtype::complex) {} PD_REGISTER_KERNEL(coo_to_dense, CPU, @@ -383,7 +387,8 @@ PD_REGISTER_KERNEL(coo_to_dense, int16_t, int, int64_t, - bool) {} + bool,phi::dtype::complex, + phi::dtype::complex) {} PD_REGISTER_KERNEL(csr_to_dense, CPU, @@ -397,7 +402,8 @@ PD_REGISTER_KERNEL(csr_to_dense, int16_t, int, int64_t, - bool) {} + bool,phi::dtype::complex, + phi::dtype::complex) {} PD_REGISTER_KERNEL(values_coo, CPU, @@ -411,7 +417,8 @@ PD_REGISTER_KERNEL(values_coo, int16_t, int, int64_t, - bool) { + bool,phi::dtype::complex, + phi::dtype::complex) { kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); } @@ -442,7 +449,8 @@ PD_REGISTER_KERNEL(values_csr, int16_t, int, int64_t, - bool) { + bool,phi::dtype::complex, + phi::dtype::complex) { kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); } @@ -456,4 +464,5 @@ PD_REGISTER_KERNEL(sparse_coo_tensor, uint8_t, int16_t, int, - int64_t) {} + int64_t,phi::dtype::complex, + phi::dtype::complex) {} diff --git a/paddle/phi/kernels/sparse/cpu/unary_grad_kernel.cc b/paddle/phi/kernels/sparse/cpu/unary_grad_kernel.cc index 4c993e3a27425..861398b7151d6 100644 --- a/paddle/phi/kernels/sparse/cpu/unary_grad_kernel.cc +++ b/paddle/phi/kernels/sparse/cpu/unary_grad_kernel.cc @@ -49,12 +49,26 @@ PD_REGISTER_SPARSE_UNARY_CPU_GRAD_KERNEL(sqrt, Sqrt) PD_REGISTER_SPARSE_UNARY_CPU_GRAD_KERNEL(square, Square) PD_REGISTER_SPARSE_UNARY_CPU_GRAD_KERNEL(log1p, Log1p) PD_REGISTER_SPARSE_UNARY_CPU_GRAD_KERNEL(relu, Relu) -PD_REGISTER_SPARSE_UNARY_CPU_GRAD_KERNEL(abs, Abs) PD_REGISTER_SPARSE_UNARY_CPU_GRAD_KERNEL(pow, Pow) PD_REGISTER_SPARSE_UNARY_CPU_GRAD_KERNEL(expm1, Expm1) PD_REGISTER_SPARSE_UNARY_CPU_GRAD_KERNEL(relu6, Relu6) PD_REGISTER_SPARSE_UNARY_CPU_GRAD_KERNEL(leaky_relu, LeakyRelu) +PD_REGISTER_KERNEL(abs_coo_grad, + CPU, + ALL_LAYOUT, + phi::sparse::AbsCooGradKernel, + float, + double,phi::dtype::complex, + phi::dtype::complex) {} + +PD_REGISTER_KERNEL(abs_csr_grad, + CPU, + ALL_LAYOUT, + phi::sparse::AbsCsrGradKernel, + float, + double,phi::dtype::complex, + phi::dtype::complex) {} PD_REGISTER_KERNEL(cast_coo_grad, CPU, ALL_LAYOUT, diff --git a/paddle/phi/kernels/sparse/cpu/unary_kernel.cc b/paddle/phi/kernels/sparse/cpu/unary_kernel.cc index 53956174044fa..ec93d9e5dd152 100644 --- a/paddle/phi/kernels/sparse/cpu/unary_kernel.cc +++ b/paddle/phi/kernels/sparse/cpu/unary_kernel.cc @@ -90,13 +90,32 @@ PD_REGISTER_SPARSE_UNARY_CPU_KERNEL(sqrt, Sqrt) PD_REGISTER_SPARSE_UNARY_CPU_KERNEL(square, Square) PD_REGISTER_SPARSE_UNARY_CPU_KERNEL(log1p, Log1p) PD_REGISTER_SPARSE_UNARY_CPU_KERNEL(relu, Relu) -PD_REGISTER_SPARSE_UNARY_CPU_KERNEL(abs, Abs) PD_REGISTER_SPARSE_UNARY_CPU_KERNEL(pow, Pow) PD_REGISTER_SPARSE_UNARY_CPU_KERNEL(scale, Scale) PD_REGISTER_SPARSE_UNARY_CPU_KERNEL(expm1, Expm1) PD_REGISTER_SPARSE_UNARY_CPU_KERNEL(relu6, Relu6) PD_REGISTER_SPARSE_UNARY_CPU_KERNEL(leaky_relu, LeakyRelu) +PD_REGISTER_KERNEL(abs_coo, + CPU, + ALL_LAYOUT, + phi::sparse::AbsCooKernel, + float, + double,phi::dtype::complex, + phi::dtype::complex) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); +} + +PD_REGISTER_KERNEL(abs_csr, + CPU, + ALL_LAYOUT, + phi::sparse::AbsCsrKernel, + float, + double,phi::dtype::complex, + phi::dtype::complex) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); +} + PD_REGISTER_KERNEL(divide_scalar_coo, CPU, ALL_LAYOUT, diff --git a/paddle/phi/kernels/sparse/empty_kernel.cc b/paddle/phi/kernels/sparse/empty_kernel.cc index 49a377ca70f67..454c30090e4d4 100644 --- a/paddle/phi/kernels/sparse/empty_kernel.cc +++ b/paddle/phi/kernels/sparse/empty_kernel.cc @@ -63,7 +63,8 @@ PD_REGISTER_KERNEL(empty_like_coo, int16_t, int, int64_t, - bool) { + bool,phi::dtype::complex, + phi::dtype::complex) { kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); } @@ -78,7 +79,8 @@ PD_REGISTER_KERNEL(empty_like_csr, int16_t, int, int64_t, - bool) { + bool,phi::dtype::complex, + phi::dtype::complex) { kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); } From 406eaef59b8c32e3f794929b096d57da60053630 Mon Sep 17 00:00:00 2001 From: bapijun Date: Mon, 26 Feb 2024 16:58:43 +0800 Subject: [PATCH 02/25] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86to=5Fdense?= =?UTF-8?q?=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kernels/sparse/impl/unary_kernel_impl.h | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/paddle/phi/kernels/sparse/impl/unary_kernel_impl.h b/paddle/phi/kernels/sparse/impl/unary_kernel_impl.h index 32fe4ae07ab67..dcdd7022f7f2d 100644 --- a/paddle/phi/kernels/sparse/impl/unary_kernel_impl.h +++ b/paddle/phi/kernels/sparse/impl/unary_kernel_impl.h @@ -86,12 +86,40 @@ DEFINE_SPARSE_UNARY_KERNEL(Sqrt) DEFINE_SPARSE_UNARY_KERNEL(Square) DEFINE_SPARSE_UNARY_KERNEL(Log1p) DEFINE_SPARSE_UNARY_KERNEL(Relu) -DEFINE_SPARSE_UNARY_KERNEL(Abs) DEFINE_SPARSE_UNARY_KERNEL(Expm1) DEFINE_SPARSE_UNARY_KERNEL(Relu6) DEFINE_SPARSE_UNARY_KERNEL_WITH_ONE_ATTR(Pow, factor) DEFINE_SPARSE_UNARY_KERNEL_WITH_ONE_ATTR(LeakyRelu, alpha) +template +void AbsCooKernel(const Context& dev_ctx, + const SparseCooTensor& x, + SparseCooTensor* out) { + EmptyLikeCooKernel(dev_ctx, x, out); + phi::AbsKernel( + dev_ctx, x.non_zero_elements(), out->mutable_non_zero_elements()); + out->SetIndicesDict(x.GetIndicesDict()); + if (out->dtype() == DataType::COMPLEX64 || out->dtype() == DataType::COMPLEX64) { + DenseTensor* out_values = out->mutable_non_zero_elements(); + out->set_type(out_values->dtype()); + } + +} + +template +void AbsCsrKernel(const Context& dev_ctx, + const SparseCsrTensor& x, + SparseCsrTensor* out) { + EmptyLikeCsrKernel(dev_ctx, x, out); + phi::AbsKernel( + dev_ctx, x.non_zero_elements(), out->mutable_non_zero_elements()); + + if (out->dtype() == DataType::COMPLEX64 || out->dtype() == DataType::COMPLEX64) { + DenseTensor* out_values = out->mutable_non_zero_elements(); + out->set_type(out_values->dtype()); + } +} + template void ScaleCooKernel(const Context& dev_ctx, const SparseCooTensor& x, From 6ca3e9a8008b01bf4fbea2a78b2fad2fb0c8f0e2 Mon Sep 17 00:00:00 2001 From: bapijun Date: Mon, 26 Feb 2024 21:09:24 +0800 Subject: [PATCH 03/25] gpu --- .../kernels/sparse/cpu/unary_grad_kernel.cc | 8 ++++-- paddle/phi/kernels/sparse/empty_kernel.cc | 6 +++-- .../kernels/sparse/gpu/sparse_utils_kernel.cu | 27 ++++++++++++------- .../kernels/sparse/gpu/unary_grad_kernel.cu | 23 +++++++++++++++- paddle/phi/kernels/sparse/gpu/unary_kernel.cu | 23 +++++++++++++++- 5 files changed, 72 insertions(+), 15 deletions(-) diff --git a/paddle/phi/kernels/sparse/cpu/unary_grad_kernel.cc b/paddle/phi/kernels/sparse/cpu/unary_grad_kernel.cc index 861398b7151d6..71d63e58e9f22 100644 --- a/paddle/phi/kernels/sparse/cpu/unary_grad_kernel.cc +++ b/paddle/phi/kernels/sparse/cpu/unary_grad_kernel.cc @@ -60,7 +60,9 @@ PD_REGISTER_KERNEL(abs_coo_grad, phi::sparse::AbsCooGradKernel, float, double,phi::dtype::complex, - phi::dtype::complex) {} + phi::dtype::complex) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); + } PD_REGISTER_KERNEL(abs_csr_grad, CPU, @@ -68,7 +70,9 @@ PD_REGISTER_KERNEL(abs_csr_grad, phi::sparse::AbsCsrGradKernel, float, double,phi::dtype::complex, - phi::dtype::complex) {} + phi::dtype::complex) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); + } PD_REGISTER_KERNEL(cast_coo_grad, CPU, ALL_LAYOUT, diff --git a/paddle/phi/kernels/sparse/empty_kernel.cc b/paddle/phi/kernels/sparse/empty_kernel.cc index 454c30090e4d4..518fdfd3b3cf5 100644 --- a/paddle/phi/kernels/sparse/empty_kernel.cc +++ b/paddle/phi/kernels/sparse/empty_kernel.cc @@ -97,7 +97,8 @@ PD_REGISTER_KERNEL(empty_like_coo, int16_t, int, int64_t, - bool) { + bool,phi::dtype::complex, + phi::dtype::complex) { kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); } @@ -113,7 +114,8 @@ PD_REGISTER_KERNEL(empty_like_csr, int16_t, int, int64_t, - bool) { + bool,phi::dtype::complex, + phi::dtype::complex) { kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); } #endif diff --git a/paddle/phi/kernels/sparse/gpu/sparse_utils_kernel.cu b/paddle/phi/kernels/sparse/gpu/sparse_utils_kernel.cu index e9001d2c506b8..16f22e0b413aa 100644 --- a/paddle/phi/kernels/sparse/gpu/sparse_utils_kernel.cu +++ b/paddle/phi/kernels/sparse/gpu/sparse_utils_kernel.cu @@ -589,7 +589,8 @@ PD_REGISTER_KERNEL(dense_to_coo, int8_t, int16_t, int, - int64_t) {} + int64_t,phi::dtype::complex, + phi::dtype::complex) {} PD_REGISTER_KERNEL(csr_to_coo, GPU, @@ -603,7 +604,8 @@ PD_REGISTER_KERNEL(csr_to_coo, int16_t, int, int64_t, - bool) {} + bool,phi::dtype::complex, + phi::dtype::complex) {} PD_REGISTER_KERNEL(coo_to_csr, GPU, @@ -617,7 +619,8 @@ PD_REGISTER_KERNEL(coo_to_csr, int16_t, int, int64_t, - bool) {} + bool,phi::dtype::complex, + phi::dtype::complex) {} PD_REGISTER_KERNEL(dense_to_csr, GPU, @@ -630,7 +633,8 @@ PD_REGISTER_KERNEL(dense_to_csr, int8_t, int16_t, int, - int64_t) {} + int64_t,phi::dtype::complex, + phi::dtype::complex) {} PD_REGISTER_KERNEL(coo_to_dense, GPU, @@ -644,7 +648,8 @@ PD_REGISTER_KERNEL(coo_to_dense, int16_t, int, int64_t, - bool) {} + bool,phi::dtype::complex, + phi::dtype::complex) {} PD_REGISTER_KERNEL(csr_to_dense, GPU, @@ -658,7 +663,8 @@ PD_REGISTER_KERNEL(csr_to_dense, int16_t, int, int64_t, - bool) {} + bool,phi::dtype::complex, + phi::dtype::complex) {} PD_REGISTER_KERNEL(values_coo, GPU, @@ -672,7 +678,8 @@ PD_REGISTER_KERNEL(values_coo, int16_t, int, int64_t, - bool) { + bool,phi::dtype::complex, + phi::dtype::complex) { kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); } @@ -688,7 +695,8 @@ PD_REGISTER_KERNEL(values_csr, int16_t, int, int64_t, - bool) { + bool,phi::dtype::complex, + phi::dtype::complex) { kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); } @@ -717,4 +725,5 @@ PD_REGISTER_KERNEL(sparse_coo_tensor, uint8_t, int16_t, int, - int64_t) {} + int64_t,phi::dtype::complex, + phi::dtype::complex) {} diff --git a/paddle/phi/kernels/sparse/gpu/unary_grad_kernel.cu b/paddle/phi/kernels/sparse/gpu/unary_grad_kernel.cu index ef66e91364c02..9cfd7caa196c9 100644 --- a/paddle/phi/kernels/sparse/gpu/unary_grad_kernel.cu +++ b/paddle/phi/kernels/sparse/gpu/unary_grad_kernel.cu @@ -51,12 +51,33 @@ PD_REGISTER_SPARSE_UNARY_GPU_GRAD_KERNEL(sqrt, Sqrt) PD_REGISTER_SPARSE_UNARY_GPU_GRAD_KERNEL(square, Square) PD_REGISTER_SPARSE_UNARY_GPU_GRAD_KERNEL(log1p, Log1p) PD_REGISTER_SPARSE_UNARY_GPU_GRAD_KERNEL(relu, Relu) -PD_REGISTER_SPARSE_UNARY_GPU_GRAD_KERNEL(abs, Abs) PD_REGISTER_SPARSE_UNARY_GPU_GRAD_KERNEL(pow, Pow) PD_REGISTER_SPARSE_UNARY_GPU_GRAD_KERNEL(expm1, Expm1) PD_REGISTER_SPARSE_UNARY_GPU_GRAD_KERNEL(relu6, Relu6) PD_REGISTER_SPARSE_UNARY_GPU_GRAD_KERNEL(leaky_relu, LeakyRelu) +PD_REGISTER_KERNEL(abs_coo_grad, + GPU, + ALL_LAYOUT, + phi::sparse::AbsCooGradKernel, + phi::dtype::float16, + float, + double,phi::dtype::complex, + phi::dtype::complex) { +kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); +} + +PD_REGISTER_KERNEL(abs_csr_grad, + GPU, + ALL_LAYOUT, + phi::sparse::AbsCsrGradKernel, + phi::dtype::float16, + float, + double,phi::dtype::complex, + phi::dtype::complex) { +kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); +} + PD_REGISTER_KERNEL(cast_coo_grad, GPU, ALL_LAYOUT, diff --git a/paddle/phi/kernels/sparse/gpu/unary_kernel.cu b/paddle/phi/kernels/sparse/gpu/unary_kernel.cu index 22392a9fea5d5..590c97e7f13cd 100644 --- a/paddle/phi/kernels/sparse/gpu/unary_kernel.cu +++ b/paddle/phi/kernels/sparse/gpu/unary_kernel.cu @@ -81,13 +81,34 @@ PD_REGISTER_SPARSE_UNARY_GPU_KERNEL(sqrt, Sqrt) PD_REGISTER_SPARSE_UNARY_GPU_KERNEL(square, Square) PD_REGISTER_SPARSE_UNARY_GPU_KERNEL(log1p, Log1p) PD_REGISTER_SPARSE_UNARY_GPU_KERNEL(relu, Relu) -PD_REGISTER_SPARSE_UNARY_GPU_KERNEL(abs, Abs) PD_REGISTER_SPARSE_UNARY_GPU_KERNEL(pow, Pow) PD_REGISTER_SPARSE_UNARY_GPU_KERNEL(scale, Scale) PD_REGISTER_SPARSE_UNARY_GPU_KERNEL(expm1, Expm1) PD_REGISTER_SPARSE_UNARY_GPU_KERNEL(relu6, Relu6) PD_REGISTER_SPARSE_UNARY_GPU_KERNEL(leaky_relu, LeakyRelu) +PD_REGISTER_KERNEL(abs_coo, + GPU, + ALL_LAYOUT, + phi::sparse::AbsCooKernel, + phi::dtype::float16, + float, + double,phi::dtype::complex, + phi::dtype::complex) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); +} + +PD_REGISTER_KERNEL(abs_csr, + GPU, + ALL_LAYOUT, + phi::sparse::AbsCsrKernel, + phi::dtype::float16, + float, + double,phi::dtype::complex, + phi::dtype::complex) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); +} + PD_REGISTER_KERNEL(divide_scalar_coo, GPU, ALL_LAYOUT, From 21ca039a7411bd8b1bca53e8bd2d39e9516b5309 Mon Sep 17 00:00:00 2001 From: bapijun Date: Tue, 27 Feb 2024 11:35:03 +0800 Subject: [PATCH 04/25] grad --- .../sparse/impl/unary_grad_kernel_impl.h | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/paddle/phi/kernels/sparse/impl/unary_grad_kernel_impl.h b/paddle/phi/kernels/sparse/impl/unary_grad_kernel_impl.h index 6a71a92452673..4cf7c0c0aa329 100644 --- a/paddle/phi/kernels/sparse/impl/unary_grad_kernel_impl.h +++ b/paddle/phi/kernels/sparse/impl/unary_grad_kernel_impl.h @@ -92,12 +92,35 @@ DEFINE_SPARSE_UNARY_GRAD_KERNEL(Sqrt) DEFINE_SPARSE_UNARY_GRAD_KERNEL(Square) DEFINE_SPARSE_UNARY_GRAD_KERNEL(Log1p) DEFINE_SPARSE_UNARY_GRAD_KERNEL(Relu) -DEFINE_SPARSE_UNARY_GRAD_KERNEL(Abs) DEFINE_SPARSE_UNARY_GRAD_KERNEL(Expm1) DEFINE_SPARSE_UNARY_GRAD_KERNEL(Relu6) DEFINE_SPARSE_UNARY_GRAD_KERNEL_WITH_ONE_ATTR(Pow, factor) DEFINE_SPARSE_UNARY_GRAD_KERNEL_WITH_ONE_ATTR(LeakyRelu, alpha) + template + void AbsCooGradKernel(const Context& dev_ctx, + const SparseCooTensor& x_or_out, + const SparseCooTensor& dout, + SparseCooTensor* dx) { + EmptyLikeCooKernel(dev_ctx, x_or_out, dx); + phi::AbsGradKernel(dev_ctx, + x_or_out.non_zero_elements(), + dout.non_zero_elements(), + dx->mutable_non_zero_elements()); + } + + template + void AbsCsrGradKernel(const Context& dev_ctx, + const SparseCsrTensor& x_or_out, + const SparseCsrTensor& dout, + SparseCsrTensor* dx) { + EmptyLikeCsrKernel(dev_ctx, x_or_out, dx); + phi::AbsGradKernel(dev_ctx, + x_or_out.non_zero_elements(), + dout.non_zero_elements(), + dx->mutable_non_zero_elements()); + } + template void CastCooGradKernel(const Context& dev_ctx, const SparseCooTensor& x, From 76277579c70aeb3cec0b2c4e6d381272930f20b4 Mon Sep 17 00:00:00 2001 From: bapijun Date: Wed, 28 Feb 2024 21:31:56 +0800 Subject: [PATCH 05/25] =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sparse/impl/unary_grad_kernel_impl.h | 8 ++ test/legacy_test/test_sparse_abs_op.py | 90 +++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 test/legacy_test/test_sparse_abs_op.py diff --git a/paddle/phi/kernels/sparse/impl/unary_grad_kernel_impl.h b/paddle/phi/kernels/sparse/impl/unary_grad_kernel_impl.h index 4cf7c0c0aa329..2c0e674b3cff6 100644 --- a/paddle/phi/kernels/sparse/impl/unary_grad_kernel_impl.h +++ b/paddle/phi/kernels/sparse/impl/unary_grad_kernel_impl.h @@ -107,6 +107,10 @@ DEFINE_SPARSE_UNARY_GRAD_KERNEL_WITH_ONE_ATTR(LeakyRelu, alpha) x_or_out.non_zero_elements(), dout.non_zero_elements(), dx->mutable_non_zero_elements()); + if (dx->dtype() == DataType::COMPLEX64 || dx->dtype() == DataType::COMPLEX64) { + DenseTensor* out_values = dx->mutable_non_zero_elements(); + dx->set_type(out_values->dtype()); + } } template @@ -119,6 +123,10 @@ DEFINE_SPARSE_UNARY_GRAD_KERNEL_WITH_ONE_ATTR(LeakyRelu, alpha) x_or_out.non_zero_elements(), dout.non_zero_elements(), dx->mutable_non_zero_elements()); + if (dx->dtype() == DataType::COMPLEX64 || dx->dtype() == DataType::COMPLEX64) { + DenseTensor* out_values = dx->mutable_non_zero_elements(); + dx->set_type(out_values->dtype()); + } } template diff --git a/test/legacy_test/test_sparse_abs_op.py b/test/legacy_test/test_sparse_abs_op.py new file mode 100644 index 0000000000000..757e6d1a0e93a --- /dev/null +++ b/test/legacy_test/test_sparse_abs_op.py @@ -0,0 +1,90 @@ +# 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. + +import unittest + +import numpy as np + +import paddle +from paddle.base.framework import convert_np_dtype_to_dtype_ + + +class TestSparseUnary(unittest.TestCase): + def to_sparse(self, x, format): + if format == 'coo': + return x.detach().to_sparse_coo(sparse_dim=x.ndim) + elif format == 'csr': + return x.detach().to_sparse_csr() + + def check_result(self, dense_func, sparse_func, format, dtype): + if (dtype == 'float32' or dtype == 'float64'): + origin_x = paddle.rand([8, 16, 32], dtype) + mask = paddle.randint(0, 2, [8, 16, 32]).astype(dtype) + while paddle.sum(mask) == 0: + mask = paddle.randint(0, 2, [8, 16, 32]).astype(dtype) + if (dtype == 'complex64'): + origin_x_real = paddle.rand([8, 16, 32], 'float32') + origin_x_com = paddle.rand([8, 16, 32], 'float32') + origin_x = (origin_x_real + 1j * origin_x_com).astype('complex64') + mask = paddle.randint(0, 2, [8, 16, 32]).astype("float32") + while paddle.sum(mask) == 0: + mask = paddle.randint(0, 2, [8, 16, 32]).astype("float32") + if (dtype == 'complex128'): + origin_x_real = paddle.rand([8, 16, 32], 'float64') + origin_x_com = paddle.rand([8, 16, 32], 'float64') + origin_x = (origin_x_real + 1j * origin_x_com).astype('complex128') + mask = paddle.randint(0, 2, [8, 16, 32]).astype("float64") + while paddle.sum(mask) == 0: + mask = paddle.randint(0, 2, [8, 16, 32]).astype("float64") + + + # --- check sparse coo with dense --- # + dense_x = origin_x * mask + + sp_x = self.to_sparse(dense_x, format) + + sp_x.stop_gradient = False + sp_out = sparse_func(sp_x) + sp_out.backward() + + dense_x.stop_gradient = False + dense_out = dense_func(dense_x) + dense_out.backward() + + # compare forward + np.testing.assert_allclose( + sp_out.to_dense().numpy(), dense_out.numpy(), rtol=1e-05 + ) + + # compare backward + if dense_func == paddle.sqrt: + expect_grad = np.nan_to_num(dense_x.grad.numpy(), 0.0, 0.0, 0.0) + else: + expect_grad = (dense_x.grad * mask).numpy() + np.testing.assert_allclose( + sp_x.grad.to_dense().numpy(), expect_grad, rtol=1e-05 + ) + def compare_with_dense(self, dense_func, sparse_func, dtype): + self.check_result(dense_func, sparse_func, 'coo', dtype) + self.check_result(dense_func, sparse_func, 'csr', dtype) + + def test_sparse_abs(self): + self.compare_with_dense(paddle.abs, paddle.sparse.abs, 'float32') + self.compare_with_dense(paddle.abs, paddle.sparse.abs, 'float64') + self.compare_with_dense(paddle.abs, paddle.sparse.abs, 'complex64') + # self.compare_with_dense(paddle.abs, paddle.sparse.abs, 'complex128') + + +if __name__ == "__main__": + unittest.main() From 952630eec963aea9b4b42005739c0c5acbe28186 Mon Sep 17 00:00:00 2001 From: bapijun Date: Wed, 28 Feb 2024 21:34:42 +0800 Subject: [PATCH 06/25] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sparse/impl/unary_grad_kernel_impl.h | 48 +++++++++---------- test/legacy_test/test_sparse_abs_op.py | 17 ++++--- 2 files changed, 32 insertions(+), 33 deletions(-) diff --git a/paddle/phi/kernels/sparse/impl/unary_grad_kernel_impl.h b/paddle/phi/kernels/sparse/impl/unary_grad_kernel_impl.h index 2c0e674b3cff6..57c60420b143c 100644 --- a/paddle/phi/kernels/sparse/impl/unary_grad_kernel_impl.h +++ b/paddle/phi/kernels/sparse/impl/unary_grad_kernel_impl.h @@ -97,35 +97,35 @@ DEFINE_SPARSE_UNARY_GRAD_KERNEL(Relu6) DEFINE_SPARSE_UNARY_GRAD_KERNEL_WITH_ONE_ATTR(Pow, factor) DEFINE_SPARSE_UNARY_GRAD_KERNEL_WITH_ONE_ATTR(LeakyRelu, alpha) - template - void AbsCooGradKernel(const Context& dev_ctx, - const SparseCooTensor& x_or_out, - const SparseCooTensor& dout, - SparseCooTensor* dx) { - EmptyLikeCooKernel(dev_ctx, x_or_out, dx); - phi::AbsGradKernel(dev_ctx, - x_or_out.non_zero_elements(), - dout.non_zero_elements(), - dx->mutable_non_zero_elements()); + template + void AbsCooGradKernel(const Context& dev_ctx, + const SparseCooTensor& x_or_out, + const SparseCooTensor& dout, + SparseCooTensor* dx) { + EmptyLikeCooKernel(dev_ctx, x_or_out, dx); + phi::AbsGradKernel(dev_ctx, + x_or_out.non_zero_elements(), + dout.non_zero_elements(), + dx->mutable_non_zero_elements()); if (dx->dtype() == DataType::COMPLEX64 || dx->dtype() == DataType::COMPLEX64) { DenseTensor* out_values = dx->mutable_non_zero_elements(); - dx->set_type(out_values->dtype()); + dx->set_type(out_values->dtype()); } - } - - template - void AbsCsrGradKernel(const Context& dev_ctx, - const SparseCsrTensor& x_or_out, - const SparseCsrTensor& dout, - SparseCsrTensor* dx) { - EmptyLikeCsrKernel(dev_ctx, x_or_out, dx); - phi::AbsGradKernel(dev_ctx, - x_or_out.non_zero_elements(), - dout.non_zero_elements(), - dx->mutable_non_zero_elements()); + } + + template + void AbsCsrGradKernel(const Context& dev_ctx, + const SparseCsrTensor& x_or_out, + const SparseCsrTensor& dout, + SparseCsrTensor* dx) { + EmptyLikeCsrKernel(dev_ctx, x_or_out, dx); + phi::AbsGradKernel(dev_ctx, + x_or_out.non_zero_elements(), + dout.non_zero_elements(), + dx->mutable_non_zero_elements()); if (dx->dtype() == DataType::COMPLEX64 || dx->dtype() == DataType::COMPLEX64) { DenseTensor* out_values = dx->mutable_non_zero_elements(); - dx->set_type(out_values->dtype()); + dx->set_type(out_values->dtype()); } } diff --git a/test/legacy_test/test_sparse_abs_op.py b/test/legacy_test/test_sparse_abs_op.py index 757e6d1a0e93a..08b6470f107c3 100644 --- a/test/legacy_test/test_sparse_abs_op.py +++ b/test/legacy_test/test_sparse_abs_op.py @@ -17,7 +17,6 @@ import numpy as np import paddle -from paddle.base.framework import convert_np_dtype_to_dtype_ class TestSparseUnary(unittest.TestCase): @@ -28,30 +27,29 @@ def to_sparse(self, x, format): return x.detach().to_sparse_csr() def check_result(self, dense_func, sparse_func, format, dtype): - if (dtype == 'float32' or dtype == 'float64'): + if dtype == 'float32' or dtype == 'float64': origin_x = paddle.rand([8, 16, 32], dtype) mask = paddle.randint(0, 2, [8, 16, 32]).astype(dtype) while paddle.sum(mask) == 0: mask = paddle.randint(0, 2, [8, 16, 32]).astype(dtype) - if (dtype == 'complex64'): + if dtype == 'complex64': origin_x_real = paddle.rand([8, 16, 32], 'float32') origin_x_com = paddle.rand([8, 16, 32], 'float32') origin_x = (origin_x_real + 1j * origin_x_com).astype('complex64') mask = paddle.randint(0, 2, [8, 16, 32]).astype("float32") while paddle.sum(mask) == 0: mask = paddle.randint(0, 2, [8, 16, 32]).astype("float32") - if (dtype == 'complex128'): + if dtype == 'complex128': origin_x_real = paddle.rand([8, 16, 32], 'float64') origin_x_com = paddle.rand([8, 16, 32], 'float64') origin_x = (origin_x_real + 1j * origin_x_com).astype('complex128') mask = paddle.randint(0, 2, [8, 16, 32]).astype("float64") while paddle.sum(mask) == 0: mask = paddle.randint(0, 2, [8, 16, 32]).astype("float64") - - + # --- check sparse coo with dense --- # dense_x = origin_x * mask - + sp_x = self.to_sparse(dense_x, format) sp_x.stop_gradient = False @@ -75,15 +73,16 @@ def check_result(self, dense_func, sparse_func, format, dtype): np.testing.assert_allclose( sp_x.grad.to_dense().numpy(), expect_grad, rtol=1e-05 ) + def compare_with_dense(self, dense_func, sparse_func, dtype): self.check_result(dense_func, sparse_func, 'coo', dtype) self.check_result(dense_func, sparse_func, 'csr', dtype) - + def test_sparse_abs(self): self.compare_with_dense(paddle.abs, paddle.sparse.abs, 'float32') self.compare_with_dense(paddle.abs, paddle.sparse.abs, 'float64') self.compare_with_dense(paddle.abs, paddle.sparse.abs, 'complex64') - # self.compare_with_dense(paddle.abs, paddle.sparse.abs, 'complex128') + self.compare_with_dense(paddle.abs, paddle.sparse.abs, 'complex128') if __name__ == "__main__": From 669eb764edfb170e36fe94718b8e9d6b49b610b7 Mon Sep 17 00:00:00 2001 From: bapijun Date: Wed, 28 Feb 2024 22:23:48 +0800 Subject: [PATCH 07/25] =?UTF-8?q?bug=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- paddle/phi/kernels/sparse/impl/unary_grad_kernel_impl.h | 4 ++-- paddle/phi/kernels/sparse/impl/unary_kernel_impl.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/paddle/phi/kernels/sparse/impl/unary_grad_kernel_impl.h b/paddle/phi/kernels/sparse/impl/unary_grad_kernel_impl.h index 57c60420b143c..ea5c3dd897b99 100644 --- a/paddle/phi/kernels/sparse/impl/unary_grad_kernel_impl.h +++ b/paddle/phi/kernels/sparse/impl/unary_grad_kernel_impl.h @@ -107,7 +107,7 @@ DEFINE_SPARSE_UNARY_GRAD_KERNEL_WITH_ONE_ATTR(LeakyRelu, alpha) x_or_out.non_zero_elements(), dout.non_zero_elements(), dx->mutable_non_zero_elements()); - if (dx->dtype() == DataType::COMPLEX64 || dx->dtype() == DataType::COMPLEX64) { + if (dx->dtype() == DataType::COMPLEX64 || dx->dtype() == DataType::COMPLEX128) { DenseTensor* out_values = dx->mutable_non_zero_elements(); dx->set_type(out_values->dtype()); } @@ -123,7 +123,7 @@ DEFINE_SPARSE_UNARY_GRAD_KERNEL_WITH_ONE_ATTR(LeakyRelu, alpha) x_or_out.non_zero_elements(), dout.non_zero_elements(), dx->mutable_non_zero_elements()); - if (dx->dtype() == DataType::COMPLEX64 || dx->dtype() == DataType::COMPLEX64) { + if (dx->dtype() == DataType::COMPLEX64 || dx->dtype() == DataType::COMPLEX128) { DenseTensor* out_values = dx->mutable_non_zero_elements(); dx->set_type(out_values->dtype()); } diff --git a/paddle/phi/kernels/sparse/impl/unary_kernel_impl.h b/paddle/phi/kernels/sparse/impl/unary_kernel_impl.h index dcdd7022f7f2d..a4b941d9dc1ab 100644 --- a/paddle/phi/kernels/sparse/impl/unary_kernel_impl.h +++ b/paddle/phi/kernels/sparse/impl/unary_kernel_impl.h @@ -99,7 +99,7 @@ void AbsCooKernel(const Context& dev_ctx, phi::AbsKernel( dev_ctx, x.non_zero_elements(), out->mutable_non_zero_elements()); out->SetIndicesDict(x.GetIndicesDict()); - if (out->dtype() == DataType::COMPLEX64 || out->dtype() == DataType::COMPLEX64) { + if (out->dtype() == DataType::COMPLEX64 || out->dtype() == DataType::COMPLEX128) { DenseTensor* out_values = out->mutable_non_zero_elements(); out->set_type(out_values->dtype()); } @@ -114,7 +114,7 @@ void AbsCsrKernel(const Context& dev_ctx, phi::AbsKernel( dev_ctx, x.non_zero_elements(), out->mutable_non_zero_elements()); - if (out->dtype() == DataType::COMPLEX64 || out->dtype() == DataType::COMPLEX64) { + if (out->dtype() == DataType::COMPLEX64 || out->dtype() == DataType::COMPLEX128) { DenseTensor* out_values = out->mutable_non_zero_elements(); out->set_type(out_values->dtype()); } From f418c49e8bcbb79e75aa986e68f730ef1325693e Mon Sep 17 00:00:00 2001 From: bapijun Date: Wed, 28 Feb 2024 22:55:43 +0800 Subject: [PATCH 08/25] pre_commit --- .../kernels/sparse/cpu/sparse_utils_kernel.cc | 27 ++++++--- .../kernels/sparse/cpu/unary_grad_kernel.cc | 10 ++-- paddle/phi/kernels/sparse/cpu/unary_kernel.cc | 6 +- paddle/phi/kernels/sparse/empty_kernel.cc | 12 ++-- .../kernels/sparse/gpu/sparse_utils_kernel.cu | 27 ++++++--- .../kernels/sparse/gpu/unary_grad_kernel.cu | 42 +++++++------- paddle/phi/kernels/sparse/gpu/unary_kernel.cu | 42 +++++++------- .../sparse/impl/unary_grad_kernel_impl.h | 58 ++++++++++--------- .../kernels/sparse/impl/unary_kernel_impl.h | 47 +++++++-------- test/legacy_test/test_sparse_unary_op.py | 3 - 10 files changed, 152 insertions(+), 122 deletions(-) diff --git a/paddle/phi/kernels/sparse/cpu/sparse_utils_kernel.cc b/paddle/phi/kernels/sparse/cpu/sparse_utils_kernel.cc index aece8f059c7e0..0c5e6857de24c 100644 --- a/paddle/phi/kernels/sparse/cpu/sparse_utils_kernel.cc +++ b/paddle/phi/kernels/sparse/cpu/sparse_utils_kernel.cc @@ -328,7 +328,8 @@ PD_REGISTER_KERNEL(dense_to_coo, int8_t, int16_t, int, - int64_t,phi::dtype::complex, + int64_t, + phi::dtype::complex, phi::dtype::complex) {} PD_REGISTER_KERNEL(csr_to_coo, @@ -343,7 +344,8 @@ PD_REGISTER_KERNEL(csr_to_coo, int16_t, int, int64_t, - bool,phi::dtype::complex, + bool, + phi::dtype::complex, phi::dtype::complex) {} PD_REGISTER_KERNEL(coo_to_csr, @@ -358,7 +360,8 @@ PD_REGISTER_KERNEL(coo_to_csr, int16_t, int, int64_t, - bool,phi::dtype::complex, + bool, + phi::dtype::complex, phi::dtype::complex) {} PD_REGISTER_KERNEL(dense_to_csr, @@ -372,7 +375,8 @@ PD_REGISTER_KERNEL(dense_to_csr, int8_t, int16_t, int, - int64_t,phi::dtype::complex, + int64_t, + phi::dtype::complex, phi::dtype::complex) {} PD_REGISTER_KERNEL(coo_to_dense, @@ -387,7 +391,8 @@ PD_REGISTER_KERNEL(coo_to_dense, int16_t, int, int64_t, - bool,phi::dtype::complex, + bool, + phi::dtype::complex, phi::dtype::complex) {} PD_REGISTER_KERNEL(csr_to_dense, @@ -402,7 +407,8 @@ PD_REGISTER_KERNEL(csr_to_dense, int16_t, int, int64_t, - bool,phi::dtype::complex, + bool, + phi::dtype::complex, phi::dtype::complex) {} PD_REGISTER_KERNEL(values_coo, @@ -417,7 +423,8 @@ PD_REGISTER_KERNEL(values_coo, int16_t, int, int64_t, - bool,phi::dtype::complex, + bool, + phi::dtype::complex, phi::dtype::complex) { kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); } @@ -449,7 +456,8 @@ PD_REGISTER_KERNEL(values_csr, int16_t, int, int64_t, - bool,phi::dtype::complex, + bool, + phi::dtype::complex, phi::dtype::complex) { kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); } @@ -464,5 +472,6 @@ PD_REGISTER_KERNEL(sparse_coo_tensor, uint8_t, int16_t, int, - int64_t,phi::dtype::complex, + int64_t, + phi::dtype::complex, phi::dtype::complex) {} diff --git a/paddle/phi/kernels/sparse/cpu/unary_grad_kernel.cc b/paddle/phi/kernels/sparse/cpu/unary_grad_kernel.cc index 71d63e58e9f22..8fbae5a40f4e4 100644 --- a/paddle/phi/kernels/sparse/cpu/unary_grad_kernel.cc +++ b/paddle/phi/kernels/sparse/cpu/unary_grad_kernel.cc @@ -59,20 +59,22 @@ PD_REGISTER_KERNEL(abs_coo_grad, ALL_LAYOUT, phi::sparse::AbsCooGradKernel, float, - double,phi::dtype::complex, + double, + phi::dtype::complex, phi::dtype::complex) { kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); - } +} PD_REGISTER_KERNEL(abs_csr_grad, CPU, ALL_LAYOUT, phi::sparse::AbsCsrGradKernel, float, - double,phi::dtype::complex, + double, + phi::dtype::complex, phi::dtype::complex) { kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); - } +} PD_REGISTER_KERNEL(cast_coo_grad, CPU, ALL_LAYOUT, diff --git a/paddle/phi/kernels/sparse/cpu/unary_kernel.cc b/paddle/phi/kernels/sparse/cpu/unary_kernel.cc index ec93d9e5dd152..8c3dddd6a8d84 100644 --- a/paddle/phi/kernels/sparse/cpu/unary_kernel.cc +++ b/paddle/phi/kernels/sparse/cpu/unary_kernel.cc @@ -101,7 +101,8 @@ PD_REGISTER_KERNEL(abs_coo, ALL_LAYOUT, phi::sparse::AbsCooKernel, float, - double,phi::dtype::complex, + double, + phi::dtype::complex, phi::dtype::complex) { kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); } @@ -111,7 +112,8 @@ PD_REGISTER_KERNEL(abs_csr, ALL_LAYOUT, phi::sparse::AbsCsrKernel, float, - double,phi::dtype::complex, + double, + phi::dtype::complex, phi::dtype::complex) { kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); } diff --git a/paddle/phi/kernels/sparse/empty_kernel.cc b/paddle/phi/kernels/sparse/empty_kernel.cc index 518fdfd3b3cf5..5bfa419cd24df 100644 --- a/paddle/phi/kernels/sparse/empty_kernel.cc +++ b/paddle/phi/kernels/sparse/empty_kernel.cc @@ -63,7 +63,8 @@ PD_REGISTER_KERNEL(empty_like_coo, int16_t, int, int64_t, - bool,phi::dtype::complex, + bool, + phi::dtype::complex, phi::dtype::complex) { kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); } @@ -79,7 +80,8 @@ PD_REGISTER_KERNEL(empty_like_csr, int16_t, int, int64_t, - bool,phi::dtype::complex, + bool, + phi::dtype::complex, phi::dtype::complex) { kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); } @@ -97,7 +99,8 @@ PD_REGISTER_KERNEL(empty_like_coo, int16_t, int, int64_t, - bool,phi::dtype::complex, + bool, + phi::dtype::complex, phi::dtype::complex) { kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); } @@ -114,7 +117,8 @@ PD_REGISTER_KERNEL(empty_like_csr, int16_t, int, int64_t, - bool,phi::dtype::complex, + bool, + phi::dtype::complex, phi::dtype::complex) { kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); } diff --git a/paddle/phi/kernels/sparse/gpu/sparse_utils_kernel.cu b/paddle/phi/kernels/sparse/gpu/sparse_utils_kernel.cu index 16f22e0b413aa..6f080e4df90cd 100644 --- a/paddle/phi/kernels/sparse/gpu/sparse_utils_kernel.cu +++ b/paddle/phi/kernels/sparse/gpu/sparse_utils_kernel.cu @@ -589,7 +589,8 @@ PD_REGISTER_KERNEL(dense_to_coo, int8_t, int16_t, int, - int64_t,phi::dtype::complex, + int64_t, + phi::dtype::complex, phi::dtype::complex) {} PD_REGISTER_KERNEL(csr_to_coo, @@ -604,7 +605,8 @@ PD_REGISTER_KERNEL(csr_to_coo, int16_t, int, int64_t, - bool,phi::dtype::complex, + bool, + phi::dtype::complex, phi::dtype::complex) {} PD_REGISTER_KERNEL(coo_to_csr, @@ -619,7 +621,8 @@ PD_REGISTER_KERNEL(coo_to_csr, int16_t, int, int64_t, - bool,phi::dtype::complex, + bool, + phi::dtype::complex, phi::dtype::complex) {} PD_REGISTER_KERNEL(dense_to_csr, @@ -633,7 +636,8 @@ PD_REGISTER_KERNEL(dense_to_csr, int8_t, int16_t, int, - int64_t,phi::dtype::complex, + int64_t, + phi::dtype::complex, phi::dtype::complex) {} PD_REGISTER_KERNEL(coo_to_dense, @@ -648,7 +652,8 @@ PD_REGISTER_KERNEL(coo_to_dense, int16_t, int, int64_t, - bool,phi::dtype::complex, + bool, + phi::dtype::complex, phi::dtype::complex) {} PD_REGISTER_KERNEL(csr_to_dense, @@ -663,7 +668,8 @@ PD_REGISTER_KERNEL(csr_to_dense, int16_t, int, int64_t, - bool,phi::dtype::complex, + bool, + phi::dtype::complex, phi::dtype::complex) {} PD_REGISTER_KERNEL(values_coo, @@ -678,7 +684,8 @@ PD_REGISTER_KERNEL(values_coo, int16_t, int, int64_t, - bool,phi::dtype::complex, + bool, + phi::dtype::complex, phi::dtype::complex) { kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); } @@ -695,7 +702,8 @@ PD_REGISTER_KERNEL(values_csr, int16_t, int, int64_t, - bool,phi::dtype::complex, + bool, + phi::dtype::complex, phi::dtype::complex) { kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); } @@ -725,5 +733,6 @@ PD_REGISTER_KERNEL(sparse_coo_tensor, uint8_t, int16_t, int, - int64_t,phi::dtype::complex, + int64_t, + phi::dtype::complex, phi::dtype::complex) {} diff --git a/paddle/phi/kernels/sparse/gpu/unary_grad_kernel.cu b/paddle/phi/kernels/sparse/gpu/unary_grad_kernel.cu index 9cfd7caa196c9..5746103925d42 100644 --- a/paddle/phi/kernels/sparse/gpu/unary_grad_kernel.cu +++ b/paddle/phi/kernels/sparse/gpu/unary_grad_kernel.cu @@ -56,26 +56,28 @@ PD_REGISTER_SPARSE_UNARY_GPU_GRAD_KERNEL(expm1, Expm1) PD_REGISTER_SPARSE_UNARY_GPU_GRAD_KERNEL(relu6, Relu6) PD_REGISTER_SPARSE_UNARY_GPU_GRAD_KERNEL(leaky_relu, LeakyRelu) -PD_REGISTER_KERNEL(abs_coo_grad, - GPU, - ALL_LAYOUT, - phi::sparse::AbsCooGradKernel, - phi::dtype::float16, - float, - double,phi::dtype::complex, - phi::dtype::complex) { -kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); -} - -PD_REGISTER_KERNEL(abs_csr_grad, - GPU, - ALL_LAYOUT, - phi::sparse::AbsCsrGradKernel, - phi::dtype::float16, - float, - double,phi::dtype::complex, - phi::dtype::complex) { -kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); +PD_REGISTER_KERNEL(abs_coo_grad, + GPU, + ALL_LAYOUT, + phi::sparse::AbsCooGradKernel, + phi::dtype::float16, + float, + double, + phi::dtype::complex, + phi::dtype::complex) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); +} + +PD_REGISTER_KERNEL(abs_csr_grad, + GPU, + ALL_LAYOUT, + phi::sparse::AbsCsrGradKernel, + phi::dtype::float16, + float, + double, + phi::dtype::complex, + phi::dtype::complex) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); } PD_REGISTER_KERNEL(cast_coo_grad, diff --git a/paddle/phi/kernels/sparse/gpu/unary_kernel.cu b/paddle/phi/kernels/sparse/gpu/unary_kernel.cu index 590c97e7f13cd..251e7b7863e0b 100644 --- a/paddle/phi/kernels/sparse/gpu/unary_kernel.cu +++ b/paddle/phi/kernels/sparse/gpu/unary_kernel.cu @@ -87,26 +87,28 @@ PD_REGISTER_SPARSE_UNARY_GPU_KERNEL(expm1, Expm1) PD_REGISTER_SPARSE_UNARY_GPU_KERNEL(relu6, Relu6) PD_REGISTER_SPARSE_UNARY_GPU_KERNEL(leaky_relu, LeakyRelu) -PD_REGISTER_KERNEL(abs_coo, - GPU, - ALL_LAYOUT, - phi::sparse::AbsCooKernel, - phi::dtype::float16, - float, - double,phi::dtype::complex, - phi::dtype::complex) { - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); -} - -PD_REGISTER_KERNEL(abs_csr, - GPU, - ALL_LAYOUT, - phi::sparse::AbsCsrKernel, - phi::dtype::float16, - float, - double,phi::dtype::complex, - phi::dtype::complex) { - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); +PD_REGISTER_KERNEL(abs_coo, + GPU, + ALL_LAYOUT, + phi::sparse::AbsCooKernel, + phi::dtype::float16, + float, + double, + phi::dtype::complex, + phi::dtype::complex) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); +} + +PD_REGISTER_KERNEL(abs_csr, + GPU, + ALL_LAYOUT, + phi::sparse::AbsCsrKernel, + phi::dtype::float16, + float, + double, + phi::dtype::complex, + phi::dtype::complex) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); } PD_REGISTER_KERNEL(divide_scalar_coo, diff --git a/paddle/phi/kernels/sparse/impl/unary_grad_kernel_impl.h b/paddle/phi/kernels/sparse/impl/unary_grad_kernel_impl.h index ea5c3dd897b99..dbb2730d94f51 100644 --- a/paddle/phi/kernels/sparse/impl/unary_grad_kernel_impl.h +++ b/paddle/phi/kernels/sparse/impl/unary_grad_kernel_impl.h @@ -97,37 +97,39 @@ DEFINE_SPARSE_UNARY_GRAD_KERNEL(Relu6) DEFINE_SPARSE_UNARY_GRAD_KERNEL_WITH_ONE_ATTR(Pow, factor) DEFINE_SPARSE_UNARY_GRAD_KERNEL_WITH_ONE_ATTR(LeakyRelu, alpha) - template - void AbsCooGradKernel(const Context& dev_ctx, - const SparseCooTensor& x_or_out, - const SparseCooTensor& dout, - SparseCooTensor* dx) { - EmptyLikeCooKernel(dev_ctx, x_or_out, dx); - phi::AbsGradKernel(dev_ctx, - x_or_out.non_zero_elements(), - dout.non_zero_elements(), - dx->mutable_non_zero_elements()); - if (dx->dtype() == DataType::COMPLEX64 || dx->dtype() == DataType::COMPLEX128) { - DenseTensor* out_values = dx->mutable_non_zero_elements(); - dx->set_type(out_values->dtype()); - } +template +void AbsCooGradKernel(const Context& dev_ctx, + const SparseCooTensor& x_or_out, + const SparseCooTensor& dout, + SparseCooTensor* dx) { + EmptyLikeCooKernel(dev_ctx, x_or_out, dx); + phi::AbsGradKernel(dev_ctx, + x_or_out.non_zero_elements(), + dout.non_zero_elements(), + dx->mutable_non_zero_elements()); + if (dx->dtype() == DataType::COMPLEX64 || + dx->dtype() == DataType::COMPLEX128) { + DenseTensor* out_values = dx->mutable_non_zero_elements(); + dx->set_type(out_values->dtype()); } +} - template - void AbsCsrGradKernel(const Context& dev_ctx, - const SparseCsrTensor& x_or_out, - const SparseCsrTensor& dout, - SparseCsrTensor* dx) { - EmptyLikeCsrKernel(dev_ctx, x_or_out, dx); - phi::AbsGradKernel(dev_ctx, - x_or_out.non_zero_elements(), - dout.non_zero_elements(), - dx->mutable_non_zero_elements()); - if (dx->dtype() == DataType::COMPLEX64 || dx->dtype() == DataType::COMPLEX128) { - DenseTensor* out_values = dx->mutable_non_zero_elements(); - dx->set_type(out_values->dtype()); - } +template +void AbsCsrGradKernel(const Context& dev_ctx, + const SparseCsrTensor& x_or_out, + const SparseCsrTensor& dout, + SparseCsrTensor* dx) { + EmptyLikeCsrKernel(dev_ctx, x_or_out, dx); + phi::AbsGradKernel(dev_ctx, + x_or_out.non_zero_elements(), + dout.non_zero_elements(), + dx->mutable_non_zero_elements()); + if (dx->dtype() == DataType::COMPLEX64 || + dx->dtype() == DataType::COMPLEX128) { + DenseTensor* out_values = dx->mutable_non_zero_elements(); + dx->set_type(out_values->dtype()); } +} template void CastCooGradKernel(const Context& dev_ctx, diff --git a/paddle/phi/kernels/sparse/impl/unary_kernel_impl.h b/paddle/phi/kernels/sparse/impl/unary_kernel_impl.h index a4b941d9dc1ab..d8f09e3b4a6e1 100644 --- a/paddle/phi/kernels/sparse/impl/unary_kernel_impl.h +++ b/paddle/phi/kernels/sparse/impl/unary_kernel_impl.h @@ -92,32 +92,33 @@ DEFINE_SPARSE_UNARY_KERNEL_WITH_ONE_ATTR(Pow, factor) DEFINE_SPARSE_UNARY_KERNEL_WITH_ONE_ATTR(LeakyRelu, alpha) template -void AbsCooKernel(const Context& dev_ctx, - const SparseCooTensor& x, - SparseCooTensor* out) { - EmptyLikeCooKernel(dev_ctx, x, out); - phi::AbsKernel( - dev_ctx, x.non_zero_elements(), out->mutable_non_zero_elements()); - out->SetIndicesDict(x.GetIndicesDict()); - if (out->dtype() == DataType::COMPLEX64 || out->dtype() == DataType::COMPLEX128) { - DenseTensor* out_values = out->mutable_non_zero_elements(); - out->set_type(out_values->dtype()); - } - -} +void AbsCooKernel(const Context& dev_ctx, + const SparseCooTensor& x, + SparseCooTensor* out) { + EmptyLikeCooKernel(dev_ctx, x, out); + phi::AbsKernel( + dev_ctx, x.non_zero_elements(), out->mutable_non_zero_elements()); + out->SetIndicesDict(x.GetIndicesDict()); + if (out->dtype() == DataType::COMPLEX64 || + out->dtype() == DataType::COMPLEX128) { + DenseTensor* out_values = out->mutable_non_zero_elements(); + out->set_type(out_values->dtype()); + } +} template -void AbsCsrKernel(const Context& dev_ctx, - const SparseCsrTensor& x, - SparseCsrTensor* out) { - EmptyLikeCsrKernel(dev_ctx, x, out); - phi::AbsKernel( - dev_ctx, x.non_zero_elements(), out->mutable_non_zero_elements()); +void AbsCsrKernel(const Context& dev_ctx, + const SparseCsrTensor& x, + SparseCsrTensor* out) { + EmptyLikeCsrKernel(dev_ctx, x, out); + phi::AbsKernel( + dev_ctx, x.non_zero_elements(), out->mutable_non_zero_elements()); - if (out->dtype() == DataType::COMPLEX64 || out->dtype() == DataType::COMPLEX128) { - DenseTensor* out_values = out->mutable_non_zero_elements(); - out->set_type(out_values->dtype()); - } + if (out->dtype() == DataType::COMPLEX64 || + out->dtype() == DataType::COMPLEX128) { + DenseTensor* out_values = out->mutable_non_zero_elements(); + out->set_type(out_values->dtype()); + } } template diff --git a/test/legacy_test/test_sparse_unary_op.py b/test/legacy_test/test_sparse_unary_op.py index 1f04694747e71..ae8885e92ab39 100644 --- a/test/legacy_test/test_sparse_unary_op.py +++ b/test/legacy_test/test_sparse_unary_op.py @@ -137,9 +137,6 @@ def test_sparse_leaky_relu(self): paddle.nn.LeakyReLU(0.1), paddle.sparse.nn.LeakyReLU(0.1) ) - def test_sparse_abs(self): - self.compare_with_dense(paddle.abs, paddle.sparse.abs) - def test_sparse_expm1(self): self.compare_with_dense(paddle.expm1, paddle.sparse.expm1) From 4de8ffab81ee767ea8085cc993e111cbfa7c06b6 Mon Sep 17 00:00:00 2001 From: bapijun Date: Thu, 29 Feb 2024 17:12:30 +0800 Subject: [PATCH 09/25] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A?= =?UTF-8?q?=E7=9A=84=E6=9B=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/paddle/sparse/unary.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/sparse/unary.py b/python/paddle/sparse/unary.py index ddb8fc669e8f8..47f0f70c4c5d2 100644 --- a/python/paddle/sparse/unary.py +++ b/python/paddle/sparse/unary.py @@ -648,7 +648,7 @@ def abs(x, name=None): out = |x| Parameters: - x (Tensor): The input Sparse Tensor with data type float32, float64. + x (Tensor): The input Sparse Tensor with data type float32, float64, complex64, complex128. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. From 08e43fb1144d27b00edd21691c418f86fff164a5 Mon Sep 17 00:00:00 2001 From: bapijun Date: Wed, 13 Mar 2024 08:07:17 +0000 Subject: [PATCH 10/25] merge into test_sparse_uanry --- test/legacy_test/test_sparse_abs_op.py | 89 ------------------------ test/legacy_test/test_sparse_unary_op.py | 72 +++++++++++++++---- 2 files changed, 57 insertions(+), 104 deletions(-) delete mode 100644 test/legacy_test/test_sparse_abs_op.py diff --git a/test/legacy_test/test_sparse_abs_op.py b/test/legacy_test/test_sparse_abs_op.py deleted file mode 100644 index 08b6470f107c3..0000000000000 --- a/test/legacy_test/test_sparse_abs_op.py +++ /dev/null @@ -1,89 +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. - -import unittest - -import numpy as np - -import paddle - - -class TestSparseUnary(unittest.TestCase): - def to_sparse(self, x, format): - if format == 'coo': - return x.detach().to_sparse_coo(sparse_dim=x.ndim) - elif format == 'csr': - return x.detach().to_sparse_csr() - - def check_result(self, dense_func, sparse_func, format, dtype): - if dtype == 'float32' or dtype == 'float64': - origin_x = paddle.rand([8, 16, 32], dtype) - mask = paddle.randint(0, 2, [8, 16, 32]).astype(dtype) - while paddle.sum(mask) == 0: - mask = paddle.randint(0, 2, [8, 16, 32]).astype(dtype) - if dtype == 'complex64': - origin_x_real = paddle.rand([8, 16, 32], 'float32') - origin_x_com = paddle.rand([8, 16, 32], 'float32') - origin_x = (origin_x_real + 1j * origin_x_com).astype('complex64') - mask = paddle.randint(0, 2, [8, 16, 32]).astype("float32") - while paddle.sum(mask) == 0: - mask = paddle.randint(0, 2, [8, 16, 32]).astype("float32") - if dtype == 'complex128': - origin_x_real = paddle.rand([8, 16, 32], 'float64') - origin_x_com = paddle.rand([8, 16, 32], 'float64') - origin_x = (origin_x_real + 1j * origin_x_com).astype('complex128') - mask = paddle.randint(0, 2, [8, 16, 32]).astype("float64") - while paddle.sum(mask) == 0: - mask = paddle.randint(0, 2, [8, 16, 32]).astype("float64") - - # --- check sparse coo with dense --- # - dense_x = origin_x * mask - - sp_x = self.to_sparse(dense_x, format) - - sp_x.stop_gradient = False - sp_out = sparse_func(sp_x) - sp_out.backward() - - dense_x.stop_gradient = False - dense_out = dense_func(dense_x) - dense_out.backward() - - # compare forward - np.testing.assert_allclose( - sp_out.to_dense().numpy(), dense_out.numpy(), rtol=1e-05 - ) - - # compare backward - if dense_func == paddle.sqrt: - expect_grad = np.nan_to_num(dense_x.grad.numpy(), 0.0, 0.0, 0.0) - else: - expect_grad = (dense_x.grad * mask).numpy() - np.testing.assert_allclose( - sp_x.grad.to_dense().numpy(), expect_grad, rtol=1e-05 - ) - - def compare_with_dense(self, dense_func, sparse_func, dtype): - self.check_result(dense_func, sparse_func, 'coo', dtype) - self.check_result(dense_func, sparse_func, 'csr', dtype) - - def test_sparse_abs(self): - self.compare_with_dense(paddle.abs, paddle.sparse.abs, 'float32') - self.compare_with_dense(paddle.abs, paddle.sparse.abs, 'float64') - self.compare_with_dense(paddle.abs, paddle.sparse.abs, 'complex64') - self.compare_with_dense(paddle.abs, paddle.sparse.abs, 'complex128') - - -if __name__ == "__main__": - unittest.main() diff --git a/test/legacy_test/test_sparse_unary_op.py b/test/legacy_test/test_sparse_unary_op.py index ae8885e92ab39..bccf203d26e78 100644 --- a/test/legacy_test/test_sparse_unary_op.py +++ b/test/legacy_test/test_sparse_unary_op.py @@ -27,16 +27,32 @@ def to_sparse(self, x, format): elif format == 'csr': return x.detach().to_sparse_csr() - def check_result(self, dense_func, sparse_func, format, *args): - origin_x = paddle.rand([8, 16, 32], dtype='float32') - mask = paddle.randint(0, 2, [8, 16, 32]).astype('float32') - while paddle.sum(mask) == 0: + def check_result( + self, dense_func, sparse_func, format, dtype='float32', *args + ): + if dtype == 'float32' or dtype == 'float64': + origin_x = paddle.rand([8, 16, 32], dtype) + mask = paddle.randint(0, 2, [8, 16, 32]).astype(dtype) + while paddle.sum(mask) == 0: + mask = paddle.randint(0, 2, [8, 16, 32]).astype(dtype) + if dtype == 'complex64': + origin_x_real = paddle.rand([8, 16, 32], 'float32') + origin_x_com = paddle.rand([8, 16, 32], 'float32') + origin_x = (origin_x_real + 1j * origin_x_com).astype('complex64') mask = paddle.randint(0, 2, [8, 16, 32]).astype("float32") + while paddle.sum(mask) == 0: + mask = paddle.randint(0, 2, [8, 16, 32]).astype("float32") + if dtype == 'complex128': + origin_x_real = paddle.rand([8, 16, 32], 'float64') + origin_x_com = paddle.rand([8, 16, 32], 'float64') + origin_x = (origin_x_real + 1j * origin_x_com).astype('complex128') + mask = paddle.randint(0, 2, [8, 16, 32]).astype("float64") + while paddle.sum(mask) == 0: + mask = paddle.randint(0, 2, [8, 16, 32]).astype("float64") # --- check sparse coo with dense --- # dense_x = origin_x * mask sp_x = self.to_sparse(dense_x, format) - sp_x.stop_gradient = False if len(args) == 0: sp_out = sparse_func(sp_x) @@ -79,19 +95,45 @@ def check_result(self, dense_func, sparse_func, format, *args): sp_x.grad.to_dense().numpy(), expect_grad, rtol=1e-05 ) - def compare_with_dense(self, dense_func, sparse_func): - self.check_result(dense_func, sparse_func, 'coo') - self.check_result(dense_func, sparse_func, 'csr') + def compare_with_dense(self, dense_func, sparse_func, dtype='float32'): + paddle.set_device('cpu') + self.check_result(dense_func, sparse_func, 'coo', dtype) + self.check_result(dense_func, sparse_func, 'csr', dtype) + paddle.set_device('gpu') + self.check_result(dense_func, sparse_func, 'coo', dtype) + self.check_result(dense_func, sparse_func, 'csr', dtype) def compare_with_dense_one_attr(self, dense_func, sparse_func, attr1): - self.check_result(dense_func, sparse_func, 'coo', attr1) - self.check_result(dense_func, sparse_func, 'csr', attr1) + paddle.set_device('cpu') + self.check_result(dense_func, sparse_func, 'coo', 'float32', attr1) + self.check_result(dense_func, sparse_func, 'csr', 'float32', attr1) + paddle.set_device('gpu') + self.check_result(dense_func, sparse_func, 'coo', 'float32', attr1) + self.check_result(dense_func, sparse_func, 'csr', 'float32', attr1) def compare_with_dense_two_attr( self, dense_func, sparse_func, attr1, attr2 ): - self.check_result(dense_func, sparse_func, 'coo', attr1, attr2) - self.check_result(dense_func, sparse_func, 'csr', attr1, attr2) + paddle.set_device('cpu') + self.check_result( + dense_func, sparse_func, 'coo', 'float32', attr1, attr2 + ) + self.check_result( + dense_func, sparse_func, 'csr', 'float32', attr1, attr2 + ) + paddle.set_device('gpu') + self.check_result( + dense_func, sparse_func, 'coo', 'float32', attr1, attr2 + ) + self.check_result( + dense_func, sparse_func, 'csr', 'float32', attr1, attr2 + ) + + def test_sparse_abs(self): + self.compare_with_dense(paddle.abs, paddle.sparse.abs, 'float32') + self.compare_with_dense(paddle.abs, paddle.sparse.abs, 'float64') + self.compare_with_dense(paddle.abs, paddle.sparse.abs, 'complex64') + self.compare_with_dense(paddle.abs, paddle.sparse.abs, 'complex128') def test_sparse_sin(self): self.compare_with_dense(paddle.sin, paddle.sparse.sin) @@ -105,9 +147,6 @@ def test_sparse_asin(self): def test_sparse_atan(self): self.compare_with_dense(paddle.atan, paddle.sparse.atan) - def test_sparse_sinh(self): - self.compare_with_dense(paddle.sinh, paddle.sparse.sinh) - def test_sparse_tanh(self): self.compare_with_dense(paddle.tanh, paddle.sparse.tanh) @@ -137,6 +176,9 @@ def test_sparse_leaky_relu(self): paddle.nn.LeakyReLU(0.1), paddle.sparse.nn.LeakyReLU(0.1) ) + def test_sparse_sinh(self): + self.compare_with_dense(paddle.sinh, paddle.sparse.sinh) + def test_sparse_expm1(self): self.compare_with_dense(paddle.expm1, paddle.sparse.expm1) From 285e5c381c173f2073de1cdbdc2287ea5dc62af0 Mon Sep 17 00:00:00 2001 From: bapijun Date: Wed, 13 Mar 2024 08:48:04 +0000 Subject: [PATCH 11/25] add devices check --- test/legacy_test/test_sparse_unary_op.py | 54 ++++++++++++------------ 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/test/legacy_test/test_sparse_unary_op.py b/test/legacy_test/test_sparse_unary_op.py index bccf203d26e78..bef146a84adf6 100644 --- a/test/legacy_test/test_sparse_unary_op.py +++ b/test/legacy_test/test_sparse_unary_op.py @@ -19,6 +19,8 @@ import paddle from paddle.base.framework import convert_np_dtype_to_dtype_ +devices = ['cpu', 'gpu'] + class TestSparseUnary(unittest.TestCase): def to_sparse(self, x, format): @@ -96,38 +98,38 @@ def check_result( ) def compare_with_dense(self, dense_func, sparse_func, dtype='float32'): - paddle.set_device('cpu') - self.check_result(dense_func, sparse_func, 'coo', dtype) - self.check_result(dense_func, sparse_func, 'csr', dtype) - paddle.set_device('gpu') - self.check_result(dense_func, sparse_func, 'coo', dtype) - self.check_result(dense_func, sparse_func, 'csr', dtype) + for device in devices: + if device == 'cpu' or ( + device == 'gpu' and paddle.is_compiled_with_cuda() + ): + self.check_result(dense_func, sparse_func, 'coo', dtype) + self.check_result(dense_func, sparse_func, 'csr', dtype) def compare_with_dense_one_attr(self, dense_func, sparse_func, attr1): - paddle.set_device('cpu') - self.check_result(dense_func, sparse_func, 'coo', 'float32', attr1) - self.check_result(dense_func, sparse_func, 'csr', 'float32', attr1) - paddle.set_device('gpu') - self.check_result(dense_func, sparse_func, 'coo', 'float32', attr1) - self.check_result(dense_func, sparse_func, 'csr', 'float32', attr1) + for device in devices: + if device == 'cpu' or ( + device == 'gpu' and paddle.is_compiled_with_cuda() + ): + self.check_result( + dense_func, sparse_func, 'coo', 'float32', attr1 + ) + self.check_result( + dense_func, sparse_func, 'csr', 'float32', attr1 + ) def compare_with_dense_two_attr( self, dense_func, sparse_func, attr1, attr2 ): - paddle.set_device('cpu') - self.check_result( - dense_func, sparse_func, 'coo', 'float32', attr1, attr2 - ) - self.check_result( - dense_func, sparse_func, 'csr', 'float32', attr1, attr2 - ) - paddle.set_device('gpu') - self.check_result( - dense_func, sparse_func, 'coo', 'float32', attr1, attr2 - ) - self.check_result( - dense_func, sparse_func, 'csr', 'float32', attr1, attr2 - ) + for device in devices: + if device == 'cpu' or ( + device == 'gpu' and paddle.is_compiled_with_cuda() + ): + self.check_result( + dense_func, sparse_func, 'coo', 'float32', attr1, attr2 + ) + self.check_result( + dense_func, sparse_func, 'csr', 'float32', attr1, attr2 + ) def test_sparse_abs(self): self.compare_with_dense(paddle.abs, paddle.sparse.abs, 'float32') From 0eabfcd3adaa6b407cfb19df2602203402f833b2 Mon Sep 17 00:00:00 2001 From: bapijun Date: Thu, 14 Mar 2024 16:08:22 +0000 Subject: [PATCH 12/25] bug fix --- test/legacy_test/test_sparse_unary_op.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/legacy_test/test_sparse_unary_op.py b/test/legacy_test/test_sparse_unary_op.py index bef146a84adf6..7f0839ae29c4c 100644 --- a/test/legacy_test/test_sparse_unary_op.py +++ b/test/legacy_test/test_sparse_unary_op.py @@ -102,6 +102,7 @@ def compare_with_dense(self, dense_func, sparse_func, dtype='float32'): if device == 'cpu' or ( device == 'gpu' and paddle.is_compiled_with_cuda() ): + paddle.set_device(device) self.check_result(dense_func, sparse_func, 'coo', dtype) self.check_result(dense_func, sparse_func, 'csr', dtype) @@ -110,6 +111,7 @@ def compare_with_dense_one_attr(self, dense_func, sparse_func, attr1): if device == 'cpu' or ( device == 'gpu' and paddle.is_compiled_with_cuda() ): + paddle.set_device(device) self.check_result( dense_func, sparse_func, 'coo', 'float32', attr1 ) @@ -124,6 +126,7 @@ def compare_with_dense_two_attr( if device == 'cpu' or ( device == 'gpu' and paddle.is_compiled_with_cuda() ): + paddle.set_device(device) self.check_result( dense_func, sparse_func, 'coo', 'float32', attr1, attr2 ) From ff00fe7933bf6b6a16215874e36da960df79a069 Mon Sep 17 00:00:00 2001 From: bapijun Date: Sat, 16 Mar 2024 12:36:35 +0000 Subject: [PATCH 13/25] macro definition for abs --- .../kernels/sparse/cpu/unary_grad_kernel.cc | 44 +++++++++-------- paddle/phi/kernels/sparse/cpu/unary_kernel.cc | 45 +++++++++-------- .../kernels/sparse/gpu/unary_grad_kernel.cu | 49 ++++++++++--------- paddle/phi/kernels/sparse/gpu/unary_kernel.cu | 49 ++++++++++--------- 4 files changed, 100 insertions(+), 87 deletions(-) diff --git a/paddle/phi/kernels/sparse/cpu/unary_grad_kernel.cc b/paddle/phi/kernels/sparse/cpu/unary_grad_kernel.cc index 8fbae5a40f4e4..5d65412a5dc05 100644 --- a/paddle/phi/kernels/sparse/cpu/unary_grad_kernel.cc +++ b/paddle/phi/kernels/sparse/cpu/unary_grad_kernel.cc @@ -37,6 +37,29 @@ kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); \ } +#define PD_REGISTER_SPARSE_UNARY_CPU_GRAD_KERNEL_WITH_COMPLEX(name, prefix) \ + PD_REGISTER_KERNEL(name##_coo_grad, \ + CPU, \ + ALL_LAYOUT, \ + phi::sparse::prefix##CooGradKernel, \ + float, \ + double, \ + phi::dtype::complex, \ + phi::dtype::complex) { \ + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); \ + } \ + \ + PD_REGISTER_KERNEL(name##_csr_grad, \ + CPU, \ + ALL_LAYOUT, \ + phi::sparse::prefix##CsrGradKernel, \ + float, \ + double, \ + phi::dtype::complex, \ + phi::dtype::complex) { \ + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); \ + } + PD_REGISTER_SPARSE_UNARY_CPU_GRAD_KERNEL(sin, Sin) PD_REGISTER_SPARSE_UNARY_CPU_GRAD_KERNEL(tan, Tan) PD_REGISTER_SPARSE_UNARY_CPU_GRAD_KERNEL(asin, Asin) @@ -54,27 +77,8 @@ PD_REGISTER_SPARSE_UNARY_CPU_GRAD_KERNEL(expm1, Expm1) PD_REGISTER_SPARSE_UNARY_CPU_GRAD_KERNEL(relu6, Relu6) PD_REGISTER_SPARSE_UNARY_CPU_GRAD_KERNEL(leaky_relu, LeakyRelu) -PD_REGISTER_KERNEL(abs_coo_grad, - CPU, - ALL_LAYOUT, - phi::sparse::AbsCooGradKernel, - float, - double, - phi::dtype::complex, - phi::dtype::complex) { - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); -} +PD_REGISTER_SPARSE_UNARY_CPU_GRAD_KERNEL_WITH_COMPLEX(abs, Abs) -PD_REGISTER_KERNEL(abs_csr_grad, - CPU, - ALL_LAYOUT, - phi::sparse::AbsCsrGradKernel, - float, - double, - phi::dtype::complex, - phi::dtype::complex) { - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); -} PD_REGISTER_KERNEL(cast_coo_grad, CPU, ALL_LAYOUT, diff --git a/paddle/phi/kernels/sparse/cpu/unary_kernel.cc b/paddle/phi/kernels/sparse/cpu/unary_kernel.cc index 8c3dddd6a8d84..6066d79bc26be 100644 --- a/paddle/phi/kernels/sparse/cpu/unary_kernel.cc +++ b/paddle/phi/kernels/sparse/cpu/unary_kernel.cc @@ -78,6 +78,29 @@ void DivScalarCsrKernel(const Context& dev_ctx, kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); \ } +#define PD_REGISTER_SPARSE_UNARY_CPU_KERNEL_WITH_COMPLEX(name, prefix) \ + PD_REGISTER_KERNEL(name##_coo, \ + CPU, \ + ALL_LAYOUT, \ + phi::sparse::prefix##CooKernel, \ + float, \ + double, \ + phi::dtype::complex, \ + phi::dtype::complex) { \ + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); \ + } \ + \ + PD_REGISTER_KERNEL(name##_csr, \ + CPU, \ + ALL_LAYOUT, \ + phi::sparse::prefix##CsrKernel, \ + float, \ + double, \ + phi::dtype::complex, \ + phi::dtype::complex) { \ + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); \ + } + PD_REGISTER_SPARSE_UNARY_CPU_KERNEL(sin, Sin) PD_REGISTER_SPARSE_UNARY_CPU_KERNEL(tan, Tan) PD_REGISTER_SPARSE_UNARY_CPU_KERNEL(asin, Asin) @@ -96,27 +119,7 @@ PD_REGISTER_SPARSE_UNARY_CPU_KERNEL(expm1, Expm1) PD_REGISTER_SPARSE_UNARY_CPU_KERNEL(relu6, Relu6) PD_REGISTER_SPARSE_UNARY_CPU_KERNEL(leaky_relu, LeakyRelu) -PD_REGISTER_KERNEL(abs_coo, - CPU, - ALL_LAYOUT, - phi::sparse::AbsCooKernel, - float, - double, - phi::dtype::complex, - phi::dtype::complex) { - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); -} - -PD_REGISTER_KERNEL(abs_csr, - CPU, - ALL_LAYOUT, - phi::sparse::AbsCsrKernel, - float, - double, - phi::dtype::complex, - phi::dtype::complex) { - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); -} +PD_REGISTER_SPARSE_UNARY_CPU_KERNEL_WITH_COMPLEX(abs, Abs) PD_REGISTER_KERNEL(divide_scalar_coo, CPU, diff --git a/paddle/phi/kernels/sparse/gpu/unary_grad_kernel.cu b/paddle/phi/kernels/sparse/gpu/unary_grad_kernel.cu index 5746103925d42..248556c1d6b53 100644 --- a/paddle/phi/kernels/sparse/gpu/unary_grad_kernel.cu +++ b/paddle/phi/kernels/sparse/gpu/unary_grad_kernel.cu @@ -39,6 +39,31 @@ kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); \ } +#define PD_REGISTER_SPARSE_UNARY_GPU_GRAD_KERNEL_WITH_COMPLEX(name, prefix) \ + PD_REGISTER_KERNEL(name##_coo_grad, \ + GPU, \ + ALL_LAYOUT, \ + phi::sparse::prefix##CooGradKernel, \ + phi::dtype::float16, \ + float, \ + double, \ + phi::dtype::complex, \ + phi::dtype::complex) { \ + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); \ + } \ + \ + PD_REGISTER_KERNEL(name##_csr_grad, \ + GPU, \ + ALL_LAYOUT, \ + phi::sparse::prefix##CsrGradKernel, \ + phi::dtype::float16, \ + float, \ + double, \ + phi::dtype::complex, \ + phi::dtype::complex) { \ + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); \ + } + PD_REGISTER_SPARSE_UNARY_GPU_GRAD_KERNEL(sin, Sin) PD_REGISTER_SPARSE_UNARY_GPU_GRAD_KERNEL(tan, Tan) PD_REGISTER_SPARSE_UNARY_GPU_GRAD_KERNEL(asin, Asin) @@ -56,29 +81,7 @@ PD_REGISTER_SPARSE_UNARY_GPU_GRAD_KERNEL(expm1, Expm1) PD_REGISTER_SPARSE_UNARY_GPU_GRAD_KERNEL(relu6, Relu6) PD_REGISTER_SPARSE_UNARY_GPU_GRAD_KERNEL(leaky_relu, LeakyRelu) -PD_REGISTER_KERNEL(abs_coo_grad, - GPU, - ALL_LAYOUT, - phi::sparse::AbsCooGradKernel, - phi::dtype::float16, - float, - double, - phi::dtype::complex, - phi::dtype::complex) { - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); -} - -PD_REGISTER_KERNEL(abs_csr_grad, - GPU, - ALL_LAYOUT, - phi::sparse::AbsCsrGradKernel, - phi::dtype::float16, - float, - double, - phi::dtype::complex, - phi::dtype::complex) { - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); -} +PD_REGISTER_SPARSE_UNARY_GPU_GRAD_KERNEL_WITH_COMPLEX(abs, Abs) PD_REGISTER_KERNEL(cast_coo_grad, GPU, diff --git a/paddle/phi/kernels/sparse/gpu/unary_kernel.cu b/paddle/phi/kernels/sparse/gpu/unary_kernel.cu index 251e7b7863e0b..8d24b3011d9ba 100644 --- a/paddle/phi/kernels/sparse/gpu/unary_kernel.cu +++ b/paddle/phi/kernels/sparse/gpu/unary_kernel.cu @@ -69,6 +69,31 @@ void DivScalarCsrKernel(const Context& dev_ctx, kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); \ } +#define PD_REGISTER_SPARSE_UNARY_GPU_KERNEL_WITH_COMPLEX(name, prefix) \ + PD_REGISTER_KERNEL(name##_coo, \ + GPU, \ + ALL_LAYOUT, \ + phi::sparse::prefix##CooKernel, \ + phi::dtype::float16, \ + float, \ + double, \ + phi::dtype::complex, \ + phi::dtype::complex) { \ + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); \ + } \ + \ + PD_REGISTER_KERNEL(name##_csr, \ + GPU, \ + ALL_LAYOUT, \ + phi::sparse::prefix##CsrKernel, \ + phi::dtype::float16, \ + float, \ + double, \ + phi::dtype::complex, \ + phi::dtype::complex) { \ + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); \ + } + PD_REGISTER_SPARSE_UNARY_GPU_KERNEL(sin, Sin) PD_REGISTER_SPARSE_UNARY_GPU_KERNEL(tan, Tan) PD_REGISTER_SPARSE_UNARY_GPU_KERNEL(asin, Asin) @@ -87,29 +112,7 @@ PD_REGISTER_SPARSE_UNARY_GPU_KERNEL(expm1, Expm1) PD_REGISTER_SPARSE_UNARY_GPU_KERNEL(relu6, Relu6) PD_REGISTER_SPARSE_UNARY_GPU_KERNEL(leaky_relu, LeakyRelu) -PD_REGISTER_KERNEL(abs_coo, - GPU, - ALL_LAYOUT, - phi::sparse::AbsCooKernel, - phi::dtype::float16, - float, - double, - phi::dtype::complex, - phi::dtype::complex) { - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); -} - -PD_REGISTER_KERNEL(abs_csr, - GPU, - ALL_LAYOUT, - phi::sparse::AbsCsrKernel, - phi::dtype::float16, - float, - double, - phi::dtype::complex, - phi::dtype::complex) { - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); -} +PD_REGISTER_SPARSE_UNARY_GPU_KERNEL_WITH_COMPLEX(abs, Abs) PD_REGISTER_KERNEL(divide_scalar_coo, GPU, From 0f982fd58c0347314bb4d1ae11140d812998fe3c Mon Sep 17 00:00:00 2001 From: bapijun Date: Sat, 16 Mar 2024 13:13:46 +0000 Subject: [PATCH 14/25] Add tests for float16 --- test/legacy_test/test_sparse_unary_op.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/test/legacy_test/test_sparse_unary_op.py b/test/legacy_test/test_sparse_unary_op.py index 7f0839ae29c4c..77a0fa6eb8469 100644 --- a/test/legacy_test/test_sparse_unary_op.py +++ b/test/legacy_test/test_sparse_unary_op.py @@ -32,11 +32,6 @@ def to_sparse(self, x, format): def check_result( self, dense_func, sparse_func, format, dtype='float32', *args ): - if dtype == 'float32' or dtype == 'float64': - origin_x = paddle.rand([8, 16, 32], dtype) - mask = paddle.randint(0, 2, [8, 16, 32]).astype(dtype) - while paddle.sum(mask) == 0: - mask = paddle.randint(0, 2, [8, 16, 32]).astype(dtype) if dtype == 'complex64': origin_x_real = paddle.rand([8, 16, 32], 'float32') origin_x_com = paddle.rand([8, 16, 32], 'float32') @@ -44,13 +39,18 @@ def check_result( mask = paddle.randint(0, 2, [8, 16, 32]).astype("float32") while paddle.sum(mask) == 0: mask = paddle.randint(0, 2, [8, 16, 32]).astype("float32") - if dtype == 'complex128': + elif dtype == 'complex128': origin_x_real = paddle.rand([8, 16, 32], 'float64') origin_x_com = paddle.rand([8, 16, 32], 'float64') origin_x = (origin_x_real + 1j * origin_x_com).astype('complex128') mask = paddle.randint(0, 2, [8, 16, 32]).astype("float64") while paddle.sum(mask) == 0: mask = paddle.randint(0, 2, [8, 16, 32]).astype("float64") + else: + origin_x = paddle.rand([8, 16, 32], dtype) + mask = paddle.randint(0, 2, [8, 16, 32]).astype(dtype) + while paddle.sum(mask) == 0: + mask = paddle.randint(0, 2, [8, 16, 32]).astype(dtype) # --- check sparse coo with dense --- # dense_x = origin_x * mask @@ -99,7 +99,8 @@ def check_result( def compare_with_dense(self, dense_func, sparse_func, dtype='float32'): for device in devices: - if device == 'cpu' or ( + # The sparse unary op is only compatible with float16 on the CUDA. + if (device == 'cpu' and dtype != 'float16') or ( device == 'gpu' and paddle.is_compiled_with_cuda() ): paddle.set_device(device) @@ -137,6 +138,7 @@ def compare_with_dense_two_attr( def test_sparse_abs(self): self.compare_with_dense(paddle.abs, paddle.sparse.abs, 'float32') self.compare_with_dense(paddle.abs, paddle.sparse.abs, 'float64') + self.compare_with_dense(paddle.abs, paddle.sparse.abs, 'float16') self.compare_with_dense(paddle.abs, paddle.sparse.abs, 'complex64') self.compare_with_dense(paddle.abs, paddle.sparse.abs, 'complex128') From 6d99580a9e5666088f7d5153f9855f56a2f1fcf7 Mon Sep 17 00:00:00 2001 From: bapijun Date: Mon, 18 Mar 2024 14:02:38 +0000 Subject: [PATCH 15/25] rabit hole --- paddle/phi/api/yaml/sparse_ops.yaml | 2 +- paddle/phi/kernels/sparse/empty_kernel.cc | 5 +++++ .../phi/kernels/sparse/impl/unary_kernel_impl.h | 16 ++++++++++------ 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/paddle/phi/api/yaml/sparse_ops.yaml b/paddle/phi/api/yaml/sparse_ops.yaml index fdebffcc4f06c..fb67dc0cbc322 100644 --- a/paddle/phi/api/yaml/sparse_ops.yaml +++ b/paddle/phi/api/yaml/sparse_ops.yaml @@ -2,7 +2,7 @@ args : (Tensor x) output : Tensor(out) infer_meta : - func : UnchangedInferMeta + func : RealAndImagInferMeta kernel : func : abs_coo{sparse_coo -> sparse_coo}, abs_csr{sparse_csr -> sparse_csr} diff --git a/paddle/phi/kernels/sparse/empty_kernel.cc b/paddle/phi/kernels/sparse/empty_kernel.cc index 5bfa419cd24df..1f9b2cd9cede6 100644 --- a/paddle/phi/kernels/sparse/empty_kernel.cc +++ b/paddle/phi/kernels/sparse/empty_kernel.cc @@ -13,6 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. */ #include "paddle/phi/kernels/sparse/empty_kernel.h" +#include "glog/logging.h" #include "paddle/phi/backends/cpu/cpu_context.h" #include "paddle/phi/backends/gpu/gpu_context.h" @@ -26,13 +27,17 @@ template void EmptyLikeCooKernel(const Context& dev_ctx, const SparseCooTensor& x, SparseCooTensor* out) { + VLOG(8) << "rabit4" << out->dtype(); *(out->mutable_indices()) = x.indices(); const DenseTensor& x_values = x.values(); DenseTensor* out_values = out->mutable_values(); out_values->Resize(x_values.dims()); + VLOG(8) << "rabit5" << out->dtype(); out->set_meta(x.meta()); + VLOG(8) << "rabit6" << out->dtype(); dev_ctx.template Alloc(out_values); + VLOG(8) << "rabit7" << out->dtype(); } template diff --git a/paddle/phi/kernels/sparse/impl/unary_kernel_impl.h b/paddle/phi/kernels/sparse/impl/unary_kernel_impl.h index d8f09e3b4a6e1..ce5c5e5779c51 100644 --- a/paddle/phi/kernels/sparse/impl/unary_kernel_impl.h +++ b/paddle/phi/kernels/sparse/impl/unary_kernel_impl.h @@ -13,7 +13,7 @@ // limitations under the License. #pragma once - +#include "glog/logging.h" #include "paddle/phi/core/meta_tensor.h" #include "paddle/phi/core/sparse_coo_tensor.h" #include "paddle/phi/core/sparse_csr_tensor.h" @@ -74,6 +74,7 @@ namespace sparse { out->mutable_non_zero_elements()); \ } +// DEFINE_SPARSE_UNARY_KERNEL(Abs) DEFINE_SPARSE_UNARY_KERNEL(Sin) DEFINE_SPARSE_UNARY_KERNEL(Tan) DEFINE_SPARSE_UNARY_KERNEL(Asin) @@ -95,15 +96,18 @@ template void AbsCooKernel(const Context& dev_ctx, const SparseCooTensor& x, SparseCooTensor* out) { + VLOG(8) << "rabit3" << out->dtype(); EmptyLikeCooKernel(dev_ctx, x, out); + VLOG(8) << "rabit final" << out->dtype(); phi::AbsKernel( dev_ctx, x.non_zero_elements(), out->mutable_non_zero_elements()); + out->SetIndicesDict(x.GetIndicesDict()); - if (out->dtype() == DataType::COMPLEX64 || - out->dtype() == DataType::COMPLEX128) { - DenseTensor* out_values = out->mutable_non_zero_elements(); - out->set_type(out_values->dtype()); - } + // if (out->dtype() == DataType::COMPLEX64 || + // out->dtype() == DataType::COMPLEX128) { + // DenseTensor* out_values = out->mutable_non_zero_elements(); + // out->set_type(out_values->dtype()); + // } } template From 52d33d2c19e0ec529701c030e43381be13e223f6 Mon Sep 17 00:00:00 2001 From: bapijun Date: Wed, 20 Mar 2024 13:15:41 +0000 Subject: [PATCH 16/25] Add a new EmptyLikeCXXKernel function to the absKernel for sparse tensors. --- paddle/phi/kernels/sparse/empty_kernel.cc | 104 +++++++++++++++++- paddle/phi/kernels/sparse/empty_kernel.h | 10 ++ .../kernels/sparse/impl/unary_kernel_impl.h | 18 +-- test/legacy_test/test_sparse_unary_op.py | 2 +- 4 files changed, 111 insertions(+), 23 deletions(-) diff --git a/paddle/phi/kernels/sparse/empty_kernel.cc b/paddle/phi/kernels/sparse/empty_kernel.cc index 1f9b2cd9cede6..14fa222e7ec77 100644 --- a/paddle/phi/kernels/sparse/empty_kernel.cc +++ b/paddle/phi/kernels/sparse/empty_kernel.cc @@ -13,8 +13,6 @@ See the License for the specific language governing permissions and limitations under the License. */ #include "paddle/phi/kernels/sparse/empty_kernel.h" -#include "glog/logging.h" - #include "paddle/phi/backends/cpu/cpu_context.h" #include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/core/kernel_registry.h" @@ -27,17 +25,13 @@ template void EmptyLikeCooKernel(const Context& dev_ctx, const SparseCooTensor& x, SparseCooTensor* out) { - VLOG(8) << "rabit4" << out->dtype(); *(out->mutable_indices()) = x.indices(); const DenseTensor& x_values = x.values(); DenseTensor* out_values = out->mutable_values(); out_values->Resize(x_values.dims()); - VLOG(8) << "rabit5" << out->dtype(); out->set_meta(x.meta()); - VLOG(8) << "rabit6" << out->dtype(); dev_ctx.template Alloc(out_values); - VLOG(8) << "rabit7" << out->dtype(); } template @@ -54,6 +48,35 @@ void EmptyLikeCsrKernel(const Context& dev_ctx, dev_ctx.template Alloc(out_values); } +template +void EmptyLikeCooRealComplexKernel(const Context& dev_ctx, + const SparseCooTensor& x, + SparseCooTensor* out) { + *(out->mutable_indices()) = x.indices(); + + const DenseTensor& x_values = x.values(); + DenseTensor* out_values = out->mutable_values(); + out_values->Resize(x_values.dims()); + // For certain c2r or r2c op, the meta-information has already been processed + // in the infermeta function. + dev_ctx.template Alloc(out_values); +} + +template +void EmptyLikeCsrRealComplexKernel(const Context& dev_ctx, + const SparseCsrTensor& x, + SparseCsrTensor* out) { + *(out->mutable_crows()) = x.crows(); + *(out->mutable_cols()) = x.cols(); + + const DenseTensor& x_values = x.values(); + DenseTensor* out_values = out->mutable_values(); + out_values->Resize(x_values.dims()); + // For certain c2r or r2c op, the meta-information has already been processed + // in the infermeta function. + dev_ctx.template Alloc(out_values); +} + } // namespace sparse } // namespace phi @@ -91,6 +114,40 @@ PD_REGISTER_KERNEL(empty_like_csr, kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); } +PD_REGISTER_KERNEL(empty_like_coo_real_comlex, + CPU, + ALL_LAYOUT, + phi::sparse::EmptyLikeCooRealComplexKernel, + float, + double, + int8_t, + uint8_t, + int16_t, + int, + int64_t, + bool, + phi::dtype::complex, + phi::dtype::complex) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); +} + +PD_REGISTER_KERNEL(empty_like_csr_real_comlex, + CPU, + ALL_LAYOUT, + phi::sparse::EmptyLikeCsrRealComplexKernel, + float, + double, + int8_t, + uint8_t, + int16_t, + int, + int64_t, + bool, + phi::dtype::complex, + phi::dtype::complex) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); +} + #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) PD_REGISTER_KERNEL(empty_like_coo, GPU, @@ -127,4 +184,39 @@ PD_REGISTER_KERNEL(empty_like_csr, phi::dtype::complex) { kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); } +PD_REGISTER_KERNEL(empty_like_coo_real_comlex, + GPU, + ALL_LAYOUT, + phi::sparse::EmptyLikeCooRealComplexKernel, + phi::dtype::float16, + float, + double, + int8_t, + uint8_t, + int16_t, + int, + int64_t, + bool, + phi::dtype::complex, + phi::dtype::complex) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); +} + +PD_REGISTER_KERNEL(empty_like_csr_real_comlex, + GPU, + ALL_LAYOUT, + phi::sparse::EmptyLikeCsrRealComplexKernel, + phi::dtype::float16, + float, + double, + int8_t, + uint8_t, + int16_t, + int, + int64_t, + bool, + phi::dtype::complex, + phi::dtype::complex) { + kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); +} #endif diff --git a/paddle/phi/kernels/sparse/empty_kernel.h b/paddle/phi/kernels/sparse/empty_kernel.h index 29eb20af583da..3cf09de2a19e4 100644 --- a/paddle/phi/kernels/sparse/empty_kernel.h +++ b/paddle/phi/kernels/sparse/empty_kernel.h @@ -30,5 +30,15 @@ void EmptyLikeCsrKernel(const Context& dev_ctx, const SparseCsrTensor& x, SparseCsrTensor* out); +template +void EmptyLikeCooRealComplexKernel(const Context& dev_ctx, + const SparseCooTensor& x, + SparseCooTensor* out); + +template +void EmptyLikeCsrRealComplexKernel(const Context& dev_ctx, + const SparseCsrTensor& x, + SparseCsrTensor* out); + } // namespace sparse } // namespace phi diff --git a/paddle/phi/kernels/sparse/impl/unary_kernel_impl.h b/paddle/phi/kernels/sparse/impl/unary_kernel_impl.h index ce5c5e5779c51..feab1968d54a4 100644 --- a/paddle/phi/kernels/sparse/impl/unary_kernel_impl.h +++ b/paddle/phi/kernels/sparse/impl/unary_kernel_impl.h @@ -13,7 +13,6 @@ // limitations under the License. #pragma once -#include "glog/logging.h" #include "paddle/phi/core/meta_tensor.h" #include "paddle/phi/core/sparse_coo_tensor.h" #include "paddle/phi/core/sparse_csr_tensor.h" @@ -96,33 +95,20 @@ template void AbsCooKernel(const Context& dev_ctx, const SparseCooTensor& x, SparseCooTensor* out) { - VLOG(8) << "rabit3" << out->dtype(); - EmptyLikeCooKernel(dev_ctx, x, out); - VLOG(8) << "rabit final" << out->dtype(); + EmptyLikeCooRealComplexKernel(dev_ctx, x, out); phi::AbsKernel( dev_ctx, x.non_zero_elements(), out->mutable_non_zero_elements()); out->SetIndicesDict(x.GetIndicesDict()); - // if (out->dtype() == DataType::COMPLEX64 || - // out->dtype() == DataType::COMPLEX128) { - // DenseTensor* out_values = out->mutable_non_zero_elements(); - // out->set_type(out_values->dtype()); - // } } template void AbsCsrKernel(const Context& dev_ctx, const SparseCsrTensor& x, SparseCsrTensor* out) { - EmptyLikeCsrKernel(dev_ctx, x, out); + EmptyLikeCsrRealComplexKernel(dev_ctx, x, out); phi::AbsKernel( dev_ctx, x.non_zero_elements(), out->mutable_non_zero_elements()); - - if (out->dtype() == DataType::COMPLEX64 || - out->dtype() == DataType::COMPLEX128) { - DenseTensor* out_values = out->mutable_non_zero_elements(); - out->set_type(out_values->dtype()); - } } template diff --git a/test/legacy_test/test_sparse_unary_op.py b/test/legacy_test/test_sparse_unary_op.py index 77a0fa6eb8469..26e867c966e51 100644 --- a/test/legacy_test/test_sparse_unary_op.py +++ b/test/legacy_test/test_sparse_unary_op.py @@ -136,9 +136,9 @@ def compare_with_dense_two_attr( ) def test_sparse_abs(self): + self.compare_with_dense(paddle.abs, paddle.sparse.abs, 'float16') self.compare_with_dense(paddle.abs, paddle.sparse.abs, 'float32') self.compare_with_dense(paddle.abs, paddle.sparse.abs, 'float64') - self.compare_with_dense(paddle.abs, paddle.sparse.abs, 'float16') self.compare_with_dense(paddle.abs, paddle.sparse.abs, 'complex64') self.compare_with_dense(paddle.abs, paddle.sparse.abs, 'complex128') From 765251b75b70f3501d5faf07b82c2e46644e1b2d Mon Sep 17 00:00:00 2001 From: bapijun Date: Wed, 20 Mar 2024 13:17:48 +0000 Subject: [PATCH 17/25] Remove irrelevant comments. --- paddle/phi/kernels/sparse/impl/unary_kernel_impl.h | 1 - 1 file changed, 1 deletion(-) diff --git a/paddle/phi/kernels/sparse/impl/unary_kernel_impl.h b/paddle/phi/kernels/sparse/impl/unary_kernel_impl.h index feab1968d54a4..e361a2abd3c6d 100644 --- a/paddle/phi/kernels/sparse/impl/unary_kernel_impl.h +++ b/paddle/phi/kernels/sparse/impl/unary_kernel_impl.h @@ -73,7 +73,6 @@ namespace sparse { out->mutable_non_zero_elements()); \ } -// DEFINE_SPARSE_UNARY_KERNEL(Abs) DEFINE_SPARSE_UNARY_KERNEL(Sin) DEFINE_SPARSE_UNARY_KERNEL(Tan) DEFINE_SPARSE_UNARY_KERNEL(Asin) From bd029e2bad71ae85acf87b5c1dd679912d4f0cd2 Mon Sep 17 00:00:00 2001 From: bapijun Date: Fri, 22 Mar 2024 07:59:52 +0000 Subject: [PATCH 18/25] add new infermeta --- paddle/phi/api/yaml/sparse_ops.yaml | 2 +- paddle/phi/infermeta/sparse/unary.cc | 7 +++++++ paddle/phi/infermeta/sparse/unary.h | 1 + 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/paddle/phi/api/yaml/sparse_ops.yaml b/paddle/phi/api/yaml/sparse_ops.yaml index fb67dc0cbc322..ae3e0c6371f10 100644 --- a/paddle/phi/api/yaml/sparse_ops.yaml +++ b/paddle/phi/api/yaml/sparse_ops.yaml @@ -2,7 +2,7 @@ args : (Tensor x) output : Tensor(out) infer_meta : - func : RealAndImagInferMeta + func : sparse::SparseC2RInferMeta kernel : func : abs_coo{sparse_coo -> sparse_coo}, abs_csr{sparse_csr -> sparse_csr} diff --git a/paddle/phi/infermeta/sparse/unary.cc b/paddle/phi/infermeta/sparse/unary.cc index f80f18bbba857..14898c17e02dd 100644 --- a/paddle/phi/infermeta/sparse/unary.cc +++ b/paddle/phi/infermeta/sparse/unary.cc @@ -14,6 +14,7 @@ limitations under the License. */ #include "paddle/phi/infermeta/sparse/unary.h" +#include "paddle/phi/common/type_traits.h" #include "paddle/phi/core/infermeta_utils.h" namespace phi { @@ -36,5 +37,11 @@ void ValuesInferMeta(const MetaTensor& x, MetaTensor* out) { out->set_layout(x.layout()); } +void SparseC2RInferMeta(const MetaTensor& x, MetaTensor* out) { + out->set_dims(x.dims()); + out->set_dtype(dtype::ToReal(x.dtype())); + out->set_layout(x.layout()); +} + } // namespace sparse } // namespace phi diff --git a/paddle/phi/infermeta/sparse/unary.h b/paddle/phi/infermeta/sparse/unary.h index 880e90b7ae697..b04fada93e289 100644 --- a/paddle/phi/infermeta/sparse/unary.h +++ b/paddle/phi/infermeta/sparse/unary.h @@ -24,5 +24,6 @@ void IndicesInferMeta(const MetaTensor& x, MetaTensor* out); void ValuesInferMeta(const MetaTensor& x, MetaTensor* out); +void SparseC2RInferMeta(const MetaTensor& x, MetaTensor* out); } // namespace sparse } // namespace phi From 2868f59540b041bc150404a511650436b56ca622 Mon Sep 17 00:00:00 2001 From: bapijun Date: Sun, 24 Mar 2024 08:12:30 +0000 Subject: [PATCH 19/25] Temporary changes --- paddle/phi/infermeta/sparse/unary.cc | 9 +++++++-- paddle/phi/infermeta/sparse/unary.h | 2 ++ .../phi/kernels/sparse/impl/unary_kernel_impl.h | 17 +++++++++++++++-- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/paddle/phi/infermeta/sparse/unary.cc b/paddle/phi/infermeta/sparse/unary.cc index 14898c17e02dd..647101f955f83 100644 --- a/paddle/phi/infermeta/sparse/unary.cc +++ b/paddle/phi/infermeta/sparse/unary.cc @@ -37,11 +37,16 @@ void ValuesInferMeta(const MetaTensor& x, MetaTensor* out) { out->set_layout(x.layout()); } -void SparseC2RInferMeta(const MetaTensor& x, MetaTensor* out) { +void SparseUnchangedInferMeta(const MetaTensor& x, MetaTensor* out) { out->set_dims(x.dims()); - out->set_dtype(dtype::ToReal(x.dtype())); + out->set_dtype(x.dtype()); out->set_layout(x.layout()); } +void SparseC2RInferMeta(const MetaTensor& x, MetaTensor* out) { + SparseUnchangedInferMeta(x, out); + out->set_dtype(dtype::ToReal(x.dtype())); +} + } // namespace sparse } // namespace phi diff --git a/paddle/phi/infermeta/sparse/unary.h b/paddle/phi/infermeta/sparse/unary.h index b04fada93e289..56ba60495e3b5 100644 --- a/paddle/phi/infermeta/sparse/unary.h +++ b/paddle/phi/infermeta/sparse/unary.h @@ -24,6 +24,8 @@ void IndicesInferMeta(const MetaTensor& x, MetaTensor* out); void ValuesInferMeta(const MetaTensor& x, MetaTensor* out); +void SparseUnchangedInferMeta(const MetaTensor& x, MetaTensor* out); + void SparseC2RInferMeta(const MetaTensor& x, MetaTensor* out); } // namespace sparse } // namespace phi diff --git a/paddle/phi/kernels/sparse/impl/unary_kernel_impl.h b/paddle/phi/kernels/sparse/impl/unary_kernel_impl.h index e361a2abd3c6d..2e83b84839bad 100644 --- a/paddle/phi/kernels/sparse/impl/unary_kernel_impl.h +++ b/paddle/phi/kernels/sparse/impl/unary_kernel_impl.h @@ -94,7 +94,13 @@ template void AbsCooKernel(const Context& dev_ctx, const SparseCooTensor& x, SparseCooTensor* out) { - EmptyLikeCooRealComplexKernel(dev_ctx, x, out); + *(out->mutable_indices()) = x.indices(); + + DenseTensor* out_values = out->mutable_values(); + const DenseTensor& x_values = x.values(); + out_values->Resize(x_values.dims()); + dev_ctx.template Alloc(out_values); + phi::AbsKernel( dev_ctx, x.non_zero_elements(), out->mutable_non_zero_elements()); @@ -105,7 +111,14 @@ template void AbsCsrKernel(const Context& dev_ctx, const SparseCsrTensor& x, SparseCsrTensor* out) { - EmptyLikeCsrRealComplexKernel(dev_ctx, x, out); + *(out->mutable_crows()) = x.crows(); + *(out->mutable_cols()) = x.cols(); + + DenseTensor* out_values = out->mutable_values(); + const DenseTensor& x_values = x.values(); + out_values->Resize(x_values.dims()); + dev_ctx.template Alloc(out_values); + phi::AbsKernel( dev_ctx, x.non_zero_elements(), out->mutable_non_zero_elements()); } From cc62200b14bf874fa6bfc22c3ef32191b4b72d8e Mon Sep 17 00:00:00 2001 From: bapijun Date: Tue, 26 Mar 2024 09:00:47 +0000 Subject: [PATCH 20/25] remove EmptyLikeCXXRealComplexKernel --- paddle/phi/api/yaml/sparse_ops.yaml | 2 +- paddle/phi/infermeta/sparse/unary.cc | 11 --- paddle/phi/infermeta/sparse/unary.h | 3 - paddle/phi/kernels/sparse/empty_kernel.cc | 98 ----------------------- paddle/phi/kernels/sparse/empty_kernel.h | 10 --- 5 files changed, 1 insertion(+), 123 deletions(-) diff --git a/paddle/phi/api/yaml/sparse_ops.yaml b/paddle/phi/api/yaml/sparse_ops.yaml index ae3e0c6371f10..fb67dc0cbc322 100644 --- a/paddle/phi/api/yaml/sparse_ops.yaml +++ b/paddle/phi/api/yaml/sparse_ops.yaml @@ -2,7 +2,7 @@ args : (Tensor x) output : Tensor(out) infer_meta : - func : sparse::SparseC2RInferMeta + func : RealAndImagInferMeta kernel : func : abs_coo{sparse_coo -> sparse_coo}, abs_csr{sparse_csr -> sparse_csr} diff --git a/paddle/phi/infermeta/sparse/unary.cc b/paddle/phi/infermeta/sparse/unary.cc index 647101f955f83..cb9157c228bba 100644 --- a/paddle/phi/infermeta/sparse/unary.cc +++ b/paddle/phi/infermeta/sparse/unary.cc @@ -37,16 +37,5 @@ void ValuesInferMeta(const MetaTensor& x, MetaTensor* out) { out->set_layout(x.layout()); } -void SparseUnchangedInferMeta(const MetaTensor& x, MetaTensor* out) { - out->set_dims(x.dims()); - out->set_dtype(x.dtype()); - out->set_layout(x.layout()); -} - -void SparseC2RInferMeta(const MetaTensor& x, MetaTensor* out) { - SparseUnchangedInferMeta(x, out); - out->set_dtype(dtype::ToReal(x.dtype())); -} - } // namespace sparse } // namespace phi diff --git a/paddle/phi/infermeta/sparse/unary.h b/paddle/phi/infermeta/sparse/unary.h index 56ba60495e3b5..880e90b7ae697 100644 --- a/paddle/phi/infermeta/sparse/unary.h +++ b/paddle/phi/infermeta/sparse/unary.h @@ -24,8 +24,5 @@ void IndicesInferMeta(const MetaTensor& x, MetaTensor* out); void ValuesInferMeta(const MetaTensor& x, MetaTensor* out); -void SparseUnchangedInferMeta(const MetaTensor& x, MetaTensor* out); - -void SparseC2RInferMeta(const MetaTensor& x, MetaTensor* out); } // namespace sparse } // namespace phi diff --git a/paddle/phi/kernels/sparse/empty_kernel.cc b/paddle/phi/kernels/sparse/empty_kernel.cc index 14fa222e7ec77..2fb11e7a66f2e 100644 --- a/paddle/phi/kernels/sparse/empty_kernel.cc +++ b/paddle/phi/kernels/sparse/empty_kernel.cc @@ -47,36 +47,6 @@ void EmptyLikeCsrKernel(const Context& dev_ctx, out->set_meta(x.meta()); dev_ctx.template Alloc(out_values); } - -template -void EmptyLikeCooRealComplexKernel(const Context& dev_ctx, - const SparseCooTensor& x, - SparseCooTensor* out) { - *(out->mutable_indices()) = x.indices(); - - const DenseTensor& x_values = x.values(); - DenseTensor* out_values = out->mutable_values(); - out_values->Resize(x_values.dims()); - // For certain c2r or r2c op, the meta-information has already been processed - // in the infermeta function. - dev_ctx.template Alloc(out_values); -} - -template -void EmptyLikeCsrRealComplexKernel(const Context& dev_ctx, - const SparseCsrTensor& x, - SparseCsrTensor* out) { - *(out->mutable_crows()) = x.crows(); - *(out->mutable_cols()) = x.cols(); - - const DenseTensor& x_values = x.values(); - DenseTensor* out_values = out->mutable_values(); - out_values->Resize(x_values.dims()); - // For certain c2r or r2c op, the meta-information has already been processed - // in the infermeta function. - dev_ctx.template Alloc(out_values); -} - } // namespace sparse } // namespace phi @@ -114,40 +84,6 @@ PD_REGISTER_KERNEL(empty_like_csr, kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); } -PD_REGISTER_KERNEL(empty_like_coo_real_comlex, - CPU, - ALL_LAYOUT, - phi::sparse::EmptyLikeCooRealComplexKernel, - float, - double, - int8_t, - uint8_t, - int16_t, - int, - int64_t, - bool, - phi::dtype::complex, - phi::dtype::complex) { - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); -} - -PD_REGISTER_KERNEL(empty_like_csr_real_comlex, - CPU, - ALL_LAYOUT, - phi::sparse::EmptyLikeCsrRealComplexKernel, - float, - double, - int8_t, - uint8_t, - int16_t, - int, - int64_t, - bool, - phi::dtype::complex, - phi::dtype::complex) { - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); -} - #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) PD_REGISTER_KERNEL(empty_like_coo, GPU, @@ -184,39 +120,5 @@ PD_REGISTER_KERNEL(empty_like_csr, phi::dtype::complex) { kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); } -PD_REGISTER_KERNEL(empty_like_coo_real_comlex, - GPU, - ALL_LAYOUT, - phi::sparse::EmptyLikeCooRealComplexKernel, - phi::dtype::float16, - float, - double, - int8_t, - uint8_t, - int16_t, - int, - int64_t, - bool, - phi::dtype::complex, - phi::dtype::complex) { - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); -} -PD_REGISTER_KERNEL(empty_like_csr_real_comlex, - GPU, - ALL_LAYOUT, - phi::sparse::EmptyLikeCsrRealComplexKernel, - phi::dtype::float16, - float, - double, - int8_t, - uint8_t, - int16_t, - int, - int64_t, - bool, - phi::dtype::complex, - phi::dtype::complex) { - kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); -} #endif diff --git a/paddle/phi/kernels/sparse/empty_kernel.h b/paddle/phi/kernels/sparse/empty_kernel.h index 3cf09de2a19e4..29eb20af583da 100644 --- a/paddle/phi/kernels/sparse/empty_kernel.h +++ b/paddle/phi/kernels/sparse/empty_kernel.h @@ -30,15 +30,5 @@ void EmptyLikeCsrKernel(const Context& dev_ctx, const SparseCsrTensor& x, SparseCsrTensor* out); -template -void EmptyLikeCooRealComplexKernel(const Context& dev_ctx, - const SparseCooTensor& x, - SparseCooTensor* out); - -template -void EmptyLikeCsrRealComplexKernel(const Context& dev_ctx, - const SparseCsrTensor& x, - SparseCsrTensor* out); - } // namespace sparse } // namespace phi From d11cb5b8d1beac025bba6557db074ca3308e94dd Mon Sep 17 00:00:00 2001 From: bapijun Date: Tue, 26 Mar 2024 09:03:50 +0000 Subject: [PATCH 21/25] Remove unrelated header files --- paddle/phi/infermeta/sparse/unary.cc | 2 -- 1 file changed, 2 deletions(-) diff --git a/paddle/phi/infermeta/sparse/unary.cc b/paddle/phi/infermeta/sparse/unary.cc index cb9157c228bba..e8381bc79403e 100644 --- a/paddle/phi/infermeta/sparse/unary.cc +++ b/paddle/phi/infermeta/sparse/unary.cc @@ -13,8 +13,6 @@ See the License for the specific language governing permissions and limitations under the License. */ #include "paddle/phi/infermeta/sparse/unary.h" - -#include "paddle/phi/common/type_traits.h" #include "paddle/phi/core/infermeta_utils.h" namespace phi { From e589c98f8ef58deb8f443364d6e5a3ef0ae30132 Mon Sep 17 00:00:00 2001 From: bapijun Date: Fri, 29 Mar 2024 08:17:05 +0000 Subject: [PATCH 22/25] change the device setting in test op --- .../sparse/impl/unary_grad_kernel_impl.h | 35 +----------------- test/legacy_test/test_sparse_unary_op.py | 36 +++++++++++++------ 2 files changed, 27 insertions(+), 44 deletions(-) diff --git a/paddle/phi/kernels/sparse/impl/unary_grad_kernel_impl.h b/paddle/phi/kernels/sparse/impl/unary_grad_kernel_impl.h index dbb2730d94f51..763293de836e5 100644 --- a/paddle/phi/kernels/sparse/impl/unary_grad_kernel_impl.h +++ b/paddle/phi/kernels/sparse/impl/unary_grad_kernel_impl.h @@ -91,46 +91,13 @@ DEFINE_SPARSE_UNARY_GRAD_KERNEL(Atanh) DEFINE_SPARSE_UNARY_GRAD_KERNEL(Sqrt) DEFINE_SPARSE_UNARY_GRAD_KERNEL(Square) DEFINE_SPARSE_UNARY_GRAD_KERNEL(Log1p) +DEFINE_SPARSE_UNARY_GRAD_KERNEL(Abs) DEFINE_SPARSE_UNARY_GRAD_KERNEL(Relu) DEFINE_SPARSE_UNARY_GRAD_KERNEL(Expm1) DEFINE_SPARSE_UNARY_GRAD_KERNEL(Relu6) DEFINE_SPARSE_UNARY_GRAD_KERNEL_WITH_ONE_ATTR(Pow, factor) DEFINE_SPARSE_UNARY_GRAD_KERNEL_WITH_ONE_ATTR(LeakyRelu, alpha) -template -void AbsCooGradKernel(const Context& dev_ctx, - const SparseCooTensor& x_or_out, - const SparseCooTensor& dout, - SparseCooTensor* dx) { - EmptyLikeCooKernel(dev_ctx, x_or_out, dx); - phi::AbsGradKernel(dev_ctx, - x_or_out.non_zero_elements(), - dout.non_zero_elements(), - dx->mutable_non_zero_elements()); - if (dx->dtype() == DataType::COMPLEX64 || - dx->dtype() == DataType::COMPLEX128) { - DenseTensor* out_values = dx->mutable_non_zero_elements(); - dx->set_type(out_values->dtype()); - } -} - -template -void AbsCsrGradKernel(const Context& dev_ctx, - const SparseCsrTensor& x_or_out, - const SparseCsrTensor& dout, - SparseCsrTensor* dx) { - EmptyLikeCsrKernel(dev_ctx, x_or_out, dx); - phi::AbsGradKernel(dev_ctx, - x_or_out.non_zero_elements(), - dout.non_zero_elements(), - dx->mutable_non_zero_elements()); - if (dx->dtype() == DataType::COMPLEX64 || - dx->dtype() == DataType::COMPLEX128) { - DenseTensor* out_values = dx->mutable_non_zero_elements(); - dx->set_type(out_values->dtype()); - } -} - template void CastCooGradKernel(const Context& dev_ctx, const SparseCooTensor& x, diff --git a/test/legacy_test/test_sparse_unary_op.py b/test/legacy_test/test_sparse_unary_op.py index 26e867c966e51..70a29ce0c62e3 100644 --- a/test/legacy_test/test_sparse_unary_op.py +++ b/test/legacy_test/test_sparse_unary_op.py @@ -30,7 +30,13 @@ def to_sparse(self, x, format): return x.detach().to_sparse_csr() def check_result( - self, dense_func, sparse_func, format, dtype='float32', *args + self, + dense_func, + sparse_func, + format, + device='cpu', + dtype='float32', + *args ): if dtype == 'complex64': origin_x_real = paddle.rand([8, 16, 32], 'float32') @@ -54,6 +60,7 @@ def check_result( # --- check sparse coo with dense --- # dense_x = origin_x * mask + dense_x.to(device) sp_x = self.to_sparse(dense_x, format) sp_x.stop_gradient = False if len(args) == 0: @@ -103,21 +110,19 @@ def compare_with_dense(self, dense_func, sparse_func, dtype='float32'): if (device == 'cpu' and dtype != 'float16') or ( device == 'gpu' and paddle.is_compiled_with_cuda() ): - paddle.set_device(device) - self.check_result(dense_func, sparse_func, 'coo', dtype) - self.check_result(dense_func, sparse_func, 'csr', dtype) + self.check_result(dense_func, sparse_func, 'coo', device, dtype) + self.check_result(dense_func, sparse_func, 'csr', device, dtype) def compare_with_dense_one_attr(self, dense_func, sparse_func, attr1): for device in devices: if device == 'cpu' or ( device == 'gpu' and paddle.is_compiled_with_cuda() ): - paddle.set_device(device) self.check_result( - dense_func, sparse_func, 'coo', 'float32', attr1 + dense_func, sparse_func, 'coo', device, 'float32', attr1 ) self.check_result( - dense_func, sparse_func, 'csr', 'float32', attr1 + dense_func, sparse_func, 'csr', device, 'float32', attr1 ) def compare_with_dense_two_attr( @@ -127,12 +132,23 @@ def compare_with_dense_two_attr( if device == 'cpu' or ( device == 'gpu' and paddle.is_compiled_with_cuda() ): - paddle.set_device(device) self.check_result( - dense_func, sparse_func, 'coo', 'float32', attr1, attr2 + dense_func, + sparse_func, + 'coo', + device, + 'float32', + attr1, + attr2, ) self.check_result( - dense_func, sparse_func, 'csr', 'float32', attr1, attr2 + dense_func, + sparse_func, + 'csr', + device, + 'float32', + attr1, + attr2, ) def test_sparse_abs(self): From de638a6a4569096f41c0969f4ff542e3ff9cca3b Mon Sep 17 00:00:00 2001 From: bapijun Date: Sun, 7 Apr 2024 09:18:22 +0000 Subject: [PATCH 23/25] sparse_utiles_grad for complex --- paddle/fluid/eager/grad_node_info.cc | 26 +++++++++++++++++++ paddle/phi/kernels/sparse/cpu/mask_kernel.cc | 11 +++++--- paddle/phi/kernels/sparse/gpu/mask_kernel.cu | 8 ++++-- .../sparse/sparse_utils_grad_kernel.cc | 24 ++++++++++++----- 4 files changed, 58 insertions(+), 11 deletions(-) diff --git a/paddle/fluid/eager/grad_node_info.cc b/paddle/fluid/eager/grad_node_info.cc index 78e3dd32fd40e..4fa535102a49a 100644 --- a/paddle/fluid/eager/grad_node_info.cc +++ b/paddle/fluid/eager/grad_node_info.cc @@ -335,6 +335,32 @@ void GradNodeBase::SetGradOutMeta(const paddle::Tensor& fwd_in, meta.SetDistAttr(dist_attr); meta.SetDistTensorGlobalDims(dist_tensor->dims()); SetIsRunAutoParallel(true); + } else if (phi::SparseCsrTensor::classof(fwd_in.impl().get())) { + phi::SparseCsrTensor* sparse_tensor = + static_cast(fwd_in.impl().get()); + const phi::DenseTensor dense_tensor = + static_cast(sparse_tensor->values()); + PADDLE_ENFORCE_NE( + dense_tensor.dtype(), + phi::DataType::UNDEFINED, + paddle::platform::errors::Fatal("Attempting to copy DenseTensorMeta " + "with phi::DataType::UNDEFINED," + "which is illegal.")); + meta.SetTensorMeta(dense_tensor.meta()); + meta.SetPlace(fwd_in.place()); + } else if (phi::SparseCooTensor::classof(fwd_in.impl().get())) { + phi::SparseCooTensor* sparse_tensor = + static_cast(fwd_in.impl().get()); + const phi::DenseTensor dense_tensor = + static_cast(sparse_tensor->values()); + PADDLE_ENFORCE_NE( + dense_tensor.dtype(), + phi::DataType::UNDEFINED, + paddle::platform::errors::Fatal("Attempting to copy DenseTensorMeta " + "with phi::DataType::UNDEFINED," + "which is illegal.")); + meta.SetTensorMeta(dense_tensor.meta()); + meta.SetPlace(fwd_in.place()); } else { VLOG(7) << "Unable to initialize the DenseTensorMeta of GradSlotMeta with " diff --git a/paddle/phi/kernels/sparse/cpu/mask_kernel.cc b/paddle/phi/kernels/sparse/cpu/mask_kernel.cc index b92ebccbefbc8..ce4f20f3ab3b5 100644 --- a/paddle/phi/kernels/sparse/cpu/mask_kernel.cc +++ b/paddle/phi/kernels/sparse/cpu/mask_kernel.cc @@ -122,8 +122,9 @@ void MaskHelperCooCPUKernel(const CPUContext& dev_ctx, x_indexs_map[x_indexs[i]] = i; } *out = phi::EmptyLike(dev_ctx, x.values()); + phi::funcs::SetConstant set_zero; + set_zero(dev_ctx, out, static_cast(0)); T* out_ptr = out->data(); - memset(out_ptr, static_cast(0), out->numel() * sizeof(T)); const int64_t stride = x.dims().size() == sparse_dim ? 1 : x.values().dims()[1]; const T* in_ptr = x.values().data(); @@ -166,7 +167,9 @@ PD_REGISTER_KERNEL(mask_coo, int16_t, int, int64_t, - bool) { + bool, + phi::dtype::complex, + phi::dtype::complex) { kernel->InputAt(1).SetDataLayout(phi::DataLayout::SPARSE_COO); } @@ -179,6 +182,8 @@ PD_REGISTER_KERNEL(mask_helper_coo, uint8_t, int16_t, int, - int64_t) { + int64_t, + phi::dtype::complex, + phi::dtype::complex) { kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); } diff --git a/paddle/phi/kernels/sparse/gpu/mask_kernel.cu b/paddle/phi/kernels/sparse/gpu/mask_kernel.cu index ab367efb11fd6..0941ad69b0dd2 100644 --- a/paddle/phi/kernels/sparse/gpu/mask_kernel.cu +++ b/paddle/phi/kernels/sparse/gpu/mask_kernel.cu @@ -308,7 +308,9 @@ PD_REGISTER_KERNEL(mask_coo, int16_t, int, int64_t, - bool) { + bool, + phi::dtype::complex, + phi::dtype::complex) { kernel->InputAt(1).SetDataLayout(phi::DataLayout::SPARSE_COO); } @@ -322,6 +324,8 @@ PD_REGISTER_KERNEL(mask_helper_coo, uint8_t, int16_t, int, - int64_t) { + int64_t, + phi::dtype::complex, + phi::dtype::complex) { kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); } diff --git a/paddle/phi/kernels/sparse/sparse_utils_grad_kernel.cc b/paddle/phi/kernels/sparse/sparse_utils_grad_kernel.cc index 064867610d719..fa18f6460fd8a 100644 --- a/paddle/phi/kernels/sparse/sparse_utils_grad_kernel.cc +++ b/paddle/phi/kernels/sparse/sparse_utils_grad_kernel.cc @@ -49,7 +49,9 @@ PD_REGISTER_KERNEL(values_coo_grad, int16_t, int, int64_t, - bool) { + bool, + phi::dtype::complex, + phi::dtype::complex) { kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); } @@ -64,7 +66,9 @@ PD_REGISTER_KERNEL(coo_to_dense_grad, int16_t, int, int64_t, - bool) { + bool, + phi::dtype::complex, + phi::dtype::complex) { kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); } @@ -77,7 +81,9 @@ PD_REGISTER_KERNEL(sparse_coo_tensor_grad, uint8_t, int16_t, int, - int64_t) { + int64_t, + phi::dtype::complex, + phi::dtype::complex) { kernel->InputAt(1).SetDataLayout(phi::DataLayout::SPARSE_COO); } @@ -94,7 +100,9 @@ PD_REGISTER_KERNEL(values_coo_grad, int16_t, int, int64_t, - bool) { + bool, + phi::dtype::complex, + phi::dtype::complex) { kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); } PD_REGISTER_KERNEL(coo_to_dense_grad, @@ -109,7 +117,9 @@ PD_REGISTER_KERNEL(coo_to_dense_grad, int16_t, int, int64_t, - bool) { + bool, + phi::dtype::complex, + phi::dtype::complex) { kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); } PD_REGISTER_KERNEL(sparse_coo_tensor_grad, @@ -121,7 +131,9 @@ PD_REGISTER_KERNEL(sparse_coo_tensor_grad, uint8_t, int16_t, int, - int64_t) { + int64_t, + phi::dtype::complex, + phi::dtype::complex) { kernel->InputAt(1).SetDataLayout(phi::DataLayout::SPARSE_COO); } #endif From 602d5a96aeb2a7ce85182ceb79df90d5bd64b585 Mon Sep 17 00:00:00 2001 From: bapijun Date: Fri, 17 May 2024 08:05:05 +0000 Subject: [PATCH 24/25] bug fix --- paddle/phi/kernels/sparse/cpu/mask_kernel.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/paddle/phi/kernels/sparse/cpu/mask_kernel.cc b/paddle/phi/kernels/sparse/cpu/mask_kernel.cc index ce4f20f3ab3b5..f187941ffd2fc 100644 --- a/paddle/phi/kernels/sparse/cpu/mask_kernel.cc +++ b/paddle/phi/kernels/sparse/cpu/mask_kernel.cc @@ -121,6 +121,7 @@ void MaskHelperCooCPUKernel(const CPUContext& dev_ctx, for (uint64_t i = 0; i < x_indexs.size(); i++) { x_indexs_map[x_indexs[i]] = i; } + *out = phi::EmptyLike(dev_ctx, x.values()); phi::funcs::SetConstant set_zero; set_zero(dev_ctx, out, static_cast(0)); From 9f2ddda603e7f5e2aab68ce1bbe82abe362e92db Mon Sep 17 00:00:00 2001 From: bapijun Date: Mon, 20 May 2024 15:52:31 +0000 Subject: [PATCH 25/25] add support for float16 in sparse_coo_grad --- paddle/phi/kernels/sparse/cpu/mask_kernel.cc | 1 + paddle/phi/kernels/sparse/sparse_utils_grad_kernel.cc | 1 + 2 files changed, 2 insertions(+) diff --git a/paddle/phi/kernels/sparse/cpu/mask_kernel.cc b/paddle/phi/kernels/sparse/cpu/mask_kernel.cc index f187941ffd2fc..5213dd44a4c07 100644 --- a/paddle/phi/kernels/sparse/cpu/mask_kernel.cc +++ b/paddle/phi/kernels/sparse/cpu/mask_kernel.cc @@ -180,6 +180,7 @@ PD_REGISTER_KERNEL(mask_helper_coo, phi::sparse::MaskHelperCooKernel, float, double, + phi::dtype::float16, uint8_t, int16_t, int, diff --git a/paddle/phi/kernels/sparse/sparse_utils_grad_kernel.cc b/paddle/phi/kernels/sparse/sparse_utils_grad_kernel.cc index fa18f6460fd8a..f5915c7acb84c 100644 --- a/paddle/phi/kernels/sparse/sparse_utils_grad_kernel.cc +++ b/paddle/phi/kernels/sparse/sparse_utils_grad_kernel.cc @@ -78,6 +78,7 @@ PD_REGISTER_KERNEL(sparse_coo_tensor_grad, phi::sparse::SparseCooTensorGradKernel, float, double, + paddle::float16, uint8_t, int16_t, int,