From 3e22b11f1fa6bb470232b09a146a82705c226af1 Mon Sep 17 00:00:00 2001 From: Aoraki-Dream Date: Thu, 29 Aug 2024 16:02:04 +0000 Subject: [PATCH 1/2] add complex for tan_coo/tan_csr --- paddle/phi/kernels/sparse/cpu/unary_grad_kernel.cc | 2 +- paddle/phi/kernels/sparse/cpu/unary_kernel.cc | 2 +- paddle/phi/kernels/sparse/gpu/unary_kernel.cu | 2 +- paddle/phi/kernels/sparse/impl/unary_kernel_impl.h | 2 +- python/paddle/sparse/unary.py | 2 +- test/legacy_test/test_sparse_unary_op.py | 6 +++++- 6 files changed, 10 insertions(+), 6 deletions(-) diff --git a/paddle/phi/kernels/sparse/cpu/unary_grad_kernel.cc b/paddle/phi/kernels/sparse/cpu/unary_grad_kernel.cc index cf7e5dc158b26f..50630409c293a1 100644 --- a/paddle/phi/kernels/sparse/cpu/unary_grad_kernel.cc +++ b/paddle/phi/kernels/sparse/cpu/unary_grad_kernel.cc @@ -60,7 +60,6 @@ kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); \ } -PD_REGISTER_SPARSE_UNARY_CPU_GRAD_KERNEL(tan, Tan) PD_REGISTER_SPARSE_UNARY_CPU_GRAD_KERNEL(asin, Asin) PD_REGISTER_SPARSE_UNARY_CPU_GRAD_KERNEL(atan, Atan) PD_REGISTER_SPARSE_UNARY_CPU_GRAD_KERNEL(sinh, Sinh) @@ -76,6 +75,7 @@ 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_SPARSE_UNARY_CPU_GRAD_KERNEL_WITH_COMPLEX(tan, Tan) PD_REGISTER_SPARSE_UNARY_CPU_GRAD_KERNEL_WITH_COMPLEX(sin, Sin) PD_REGISTER_SPARSE_UNARY_CPU_GRAD_KERNEL_WITH_COMPLEX(abs, Abs) diff --git a/paddle/phi/kernels/sparse/cpu/unary_kernel.cc b/paddle/phi/kernels/sparse/cpu/unary_kernel.cc index 4e4394af45a01b..d567fe9f379163 100644 --- a/paddle/phi/kernels/sparse/cpu/unary_kernel.cc +++ b/paddle/phi/kernels/sparse/cpu/unary_kernel.cc @@ -101,7 +101,6 @@ void DivScalarCsrKernel(const Context& dev_ctx, kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); \ } -PD_REGISTER_SPARSE_UNARY_CPU_KERNEL(tan, Tan) PD_REGISTER_SPARSE_UNARY_CPU_KERNEL(asin, Asin) PD_REGISTER_SPARSE_UNARY_CPU_KERNEL(atan, Atan) PD_REGISTER_SPARSE_UNARY_CPU_KERNEL(sinh, Sinh) @@ -118,6 +117,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_SPARSE_UNARY_CPU_KERNEL_WITH_COMPLEX(tan, Tan) PD_REGISTER_SPARSE_UNARY_CPU_KERNEL_WITH_COMPLEX(sin, Sin) PD_REGISTER_SPARSE_UNARY_CPU_KERNEL_WITH_COMPLEX(abs, Abs) diff --git a/paddle/phi/kernels/sparse/gpu/unary_kernel.cu b/paddle/phi/kernels/sparse/gpu/unary_kernel.cu index 136485c53b1eee..5300a486539338 100644 --- a/paddle/phi/kernels/sparse/gpu/unary_kernel.cu +++ b/paddle/phi/kernels/sparse/gpu/unary_kernel.cu @@ -94,7 +94,6 @@ void DivScalarCsrKernel(const Context& dev_ctx, kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); \ } -PD_REGISTER_SPARSE_UNARY_GPU_KERNEL(tan, Tan) PD_REGISTER_SPARSE_UNARY_GPU_KERNEL(asin, Asin) PD_REGISTER_SPARSE_UNARY_GPU_KERNEL(atan, Atan) PD_REGISTER_SPARSE_UNARY_GPU_KERNEL(sinh, Sinh) @@ -111,6 +110,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_SPARSE_UNARY_GPU_KERNEL_WITH_COMPLEX(tan, Tan) PD_REGISTER_SPARSE_UNARY_GPU_KERNEL_WITH_COMPLEX(sin, Sin) PD_REGISTER_SPARSE_UNARY_GPU_KERNEL_WITH_COMPLEX(abs, Abs) diff --git a/paddle/phi/kernels/sparse/impl/unary_kernel_impl.h b/paddle/phi/kernels/sparse/impl/unary_kernel_impl.h index d6f649b79bb9b6..e4df2645ef2b08 100644 --- a/paddle/phi/kernels/sparse/impl/unary_kernel_impl.h +++ b/paddle/phi/kernels/sparse/impl/unary_kernel_impl.h @@ -103,7 +103,6 @@ namespace sparse { dev_ctx, x.non_zero_elements(), out->mutable_non_zero_elements()); \ } -DEFINE_SPARSE_UNARY_KERNEL(Tan) DEFINE_SPARSE_UNARY_KERNEL(Asin) DEFINE_SPARSE_UNARY_KERNEL(Atan) DEFINE_SPARSE_UNARY_KERNEL(Sinh) @@ -120,6 +119,7 @@ DEFINE_SPARSE_UNARY_KERNEL_WITH_ONE_ATTR(Pow, factor) DEFINE_SPARSE_UNARY_KERNEL_WITH_ONE_ATTR(LeakyRelu, alpha) DEFINE_SPARSE_UNARY_KERNEL_WITH_COMPLEX(Abs) DEFINE_SPARSE_UNARY_KERNEL_WITH_COMPLEX(Sin) +DEFINE_SPARSE_UNARY_KERNEL_WITH_COMPLEX(Tan) template void ScaleCooKernel(const Context& dev_ctx, diff --git a/python/paddle/sparse/unary.py b/python/paddle/sparse/unary.py index 2c44dd824d197f..02c48e1979fd48 100644 --- a/python/paddle/sparse/unary.py +++ b/python/paddle/sparse/unary.py @@ -91,7 +91,7 @@ def tan(x: Tensor, name: str | None = None) -> Tensor: out = tan(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|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. diff --git a/test/legacy_test/test_sparse_unary_op.py b/test/legacy_test/test_sparse_unary_op.py index 365bcf8a481050..474eff10e40d7a 100644 --- a/test/legacy_test/test_sparse_unary_op.py +++ b/test/legacy_test/test_sparse_unary_op.py @@ -166,7 +166,11 @@ def test_sparse_sin(self): self.compare_with_dense(paddle.sin, paddle.sparse.sin, 'complex128') def test_sparse_tan(self): - self.compare_with_dense(paddle.tan, paddle.sparse.tan) + self.compare_with_dense(paddle.tan, paddle.sparse.tan, 'float16') + self.compare_with_dense(paddle.tan, paddle.sparse.tan, 'float32') + self.compare_with_dense(paddle.tan, paddle.sparse.tan, 'float64') + self.compare_with_dense(paddle.tan, paddle.sparse.tan, 'complex64') + self.compare_with_dense(paddle.tan, paddle.sparse.tan, 'complex128') def test_sparse_asin(self): self.compare_with_dense(paddle.asin, paddle.sparse.asin) From a2d0ff3b887f46653a61dadf8596c1eb39185b45 Mon Sep 17 00:00:00 2001 From: Aoraki-Dream Date: Sat, 31 Aug 2024 03:34:49 +0000 Subject: [PATCH 2/2] add two missed changes --- paddle/phi/kernels/sparse/gpu/unary_grad_kernel.cu | 2 +- test/legacy_test/test_sparse_unary_op.py | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/paddle/phi/kernels/sparse/gpu/unary_grad_kernel.cu b/paddle/phi/kernels/sparse/gpu/unary_grad_kernel.cu index 364b4dfc0ba3a3..f310383899cb40 100644 --- a/paddle/phi/kernels/sparse/gpu/unary_grad_kernel.cu +++ b/paddle/phi/kernels/sparse/gpu/unary_grad_kernel.cu @@ -64,7 +64,6 @@ kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); \ } -PD_REGISTER_SPARSE_UNARY_GPU_GRAD_KERNEL(tan, Tan) PD_REGISTER_SPARSE_UNARY_GPU_GRAD_KERNEL(asin, Asin) PD_REGISTER_SPARSE_UNARY_GPU_GRAD_KERNEL(atan, Atan) PD_REGISTER_SPARSE_UNARY_GPU_GRAD_KERNEL(sinh, Sinh) @@ -82,6 +81,7 @@ PD_REGISTER_SPARSE_UNARY_GPU_GRAD_KERNEL(leaky_relu, LeakyRelu) PD_REGISTER_SPARSE_UNARY_GPU_GRAD_KERNEL_WITH_COMPLEX(sin, Sin) PD_REGISTER_SPARSE_UNARY_GPU_GRAD_KERNEL_WITH_COMPLEX(abs, Abs) +PD_REGISTER_SPARSE_UNARY_GPU_GRAD_KERNEL_WITH_COMPLEX(tan, Tan) PD_REGISTER_KERNEL(cast_coo_grad, GPU, diff --git a/test/legacy_test/test_sparse_unary_op.py b/test/legacy_test/test_sparse_unary_op.py index 474eff10e40d7a..eae3e1b1ae519a 100644 --- a/test/legacy_test/test_sparse_unary_op.py +++ b/test/legacy_test/test_sparse_unary_op.py @@ -404,7 +404,11 @@ def test_sparse_sin(self): self.compare_with_dense(paddle.sin, paddle.sparse.sin, 'complex128') def test_sparse_tan(self): - self.compare_with_dense(paddle.tan, paddle.sparse.tan) + self.compare_with_dense(paddle.tan, paddle.sparse.tan, 'float16') + self.compare_with_dense(paddle.tan, paddle.sparse.tan, 'float32') + self.compare_with_dense(paddle.tan, paddle.sparse.tan, 'float64') + self.compare_with_dense(paddle.tan, paddle.sparse.tan, 'complex64') + self.compare_with_dense(paddle.tan, paddle.sparse.tan, 'complex128') def test_sparse_asin(self): self.compare_with_dense(paddle.asin, paddle.sparse.asin)