Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion paddle/phi/kernels/sparse/cpu/unary_grad_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ 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(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_SPARSE_UNARY_CPU_GRAD_KERNEL_WITH_COMPLEX(pow, Pow)
PD_REGISTER_SPARSE_UNARY_CPU_GRAD_KERNEL_WITH_COMPLEX(sinh, Sinh)
PD_REGISTER_SPARSE_UNARY_CPU_GRAD_KERNEL_WITH_COMPLEX(tan, Tan)
PD_REGISTER_SPARSE_UNARY_CPU_GRAD_KERNEL_WITH_COMPLEX(sin, Sin)
Expand Down
2 changes: 1 addition & 1 deletion paddle/phi/kernels/sparse/cpu/unary_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ 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(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_SPARSE_UNARY_CPU_KERNEL_WITH_COMPLEX(pow, Pow)
PD_REGISTER_SPARSE_UNARY_CPU_KERNEL_WITH_COMPLEX(sinh, Sinh)
PD_REGISTER_SPARSE_UNARY_CPU_KERNEL_WITH_COMPLEX(tan, Tan)
PD_REGISTER_SPARSE_UNARY_CPU_KERNEL_WITH_COMPLEX(sin, Sin)
Expand Down
2 changes: 1 addition & 1 deletion paddle/phi/kernels/sparse/gpu/unary_grad_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ 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(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_SPARSE_UNARY_GPU_GRAD_KERNEL_WITH_COMPLEX(pow, Pow)
PD_REGISTER_SPARSE_UNARY_GPU_GRAD_KERNEL_WITH_COMPLEX(sinh, Sinh)
PD_REGISTER_SPARSE_UNARY_GPU_GRAD_KERNEL_WITH_COMPLEX(sin, Sin)
PD_REGISTER_SPARSE_UNARY_GPU_GRAD_KERNEL_WITH_COMPLEX(abs, Abs)
Expand Down
2 changes: 1 addition & 1 deletion paddle/phi/kernels/sparse/gpu/unary_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ 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(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_SPARSE_UNARY_GPU_KERNEL_WITH_COMPLEX(pow, Pow)
PD_REGISTER_SPARSE_UNARY_GPU_KERNEL_WITH_COMPLEX(sinh, Sinh)
PD_REGISTER_SPARSE_UNARY_GPU_KERNEL_WITH_COMPLEX(tan, Tan)
PD_REGISTER_SPARSE_UNARY_GPU_KERNEL_WITH_COMPLEX(sin, Sin)
Expand Down
37 changes: 36 additions & 1 deletion paddle/phi/kernels/sparse/impl/unary_kernel_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,41 @@ namespace sparse {
dev_ctx, x.non_zero_elements(), out->mutable_non_zero_elements()); \
}

#define DEFINE_SPARSE_UNARY_KERNEL_WITH_ONE_ATTR_COMPLEX(prefix, attr) \
template <typename T, typename Context> \
void prefix##CooKernel(const Context& dev_ctx, \
const SparseCooTensor& x, \
float attr, \
SparseCooTensor* 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<T>(out_values); \
phi::prefix##Kernel<T, Context>(dev_ctx, \
x.non_zero_elements(), \
attr, \
out->mutable_non_zero_elements()); \
out->SetIndicesDict(x.GetIndicesDict()); \
} \
\
template <typename T, typename Context> \
void prefix##CsrKernel(const Context& dev_ctx, \
const SparseCsrTensor& x, \
float attr, \
SparseCsrTensor* 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<T>(out_values); \
phi::prefix##Kernel<T, Context>(dev_ctx, \
x.non_zero_elements(), \
attr, \
out->mutable_non_zero_elements()); \
}

DEFINE_SPARSE_UNARY_KERNEL(Asin)
DEFINE_SPARSE_UNARY_KERNEL(Atan)
DEFINE_SPARSE_UNARY_KERNEL(Tanh)
Expand All @@ -114,7 +149,7 @@ DEFINE_SPARSE_UNARY_KERNEL(Log1p)
DEFINE_SPARSE_UNARY_KERNEL(Relu)
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_COMPLEX(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)
Expand Down
2 changes: 1 addition & 1 deletion python/paddle/sparse/unary.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ def pow(x: Tensor, factor: float, name: str | None = None) -> Tensor:
out = x^{factor}

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.
factor (float|int): factor of pow.
name (str|None, optional): Name for the operation (optional, default is None).
For more information, please refer to :ref:`api_guide_Name`.
Expand Down
34 changes: 27 additions & 7 deletions test/legacy_test/test_sparse_unary_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,18 @@ def compare_with_dense(self, dense_func, sparse_func, dtype='float32'):
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):
def compare_with_dense_one_attr(
self, dense_func, sparse_func, attr1, dtype='float32'
):
for device in devices:
if device == 'cpu' or (
device == 'gpu' and paddle.is_compiled_with_cuda()
):
self.check_result(
dense_func, sparse_func, 'coo', device, 'float32', attr1
dense_func, sparse_func, 'coo', device, dtype, attr1
)
self.check_result(
dense_func, sparse_func, 'csr', device, 'float32', attr1
dense_func, sparse_func, 'csr', device, dtype, attr1
)

def compare_with_dense_two_attr(
Expand Down Expand Up @@ -227,7 +229,15 @@ def test_sparse_neg(self):
self.compare_with_dense(paddle.neg, paddle.sparse.neg)

def test_sparse_pow(self):
self.compare_with_dense_one_attr(paddle.pow, paddle.sparse.pow, 3)
self.compare_with_dense_one_attr(
paddle.pow, paddle.sparse.pow, 3, 'float32'
)
self.compare_with_dense_one_attr(
paddle.pow, paddle.sparse.pow, 3, 'complex64'
)
self.compare_with_dense_one_attr(
paddle.pow, paddle.sparse.pow, 3, 'complex128'
)

def test_sparse_mul_scalar(self):
self.compare_with_dense_one_attr(
Expand Down Expand Up @@ -366,14 +376,16 @@ def compare_with_dense(self, dense_func, sparse_func, dtype='float32'):
dense_func, sparse_func, device, dtype
)

def compare_with_dense_one_attr(self, dense_func, sparse_func, attr1):
def compare_with_dense_one_attr(
self, dense_func, sparse_func, attr1, dtype='float32'
):
if in_pir_mode():
for device in devices:
if device == 'cpu' or (
device == 'gpu' and paddle.is_compiled_with_cuda()
):
self.check_result_coo(
dense_func, sparse_func, device, 'float32', attr1
dense_func, sparse_func, device, dtype, attr1
)

def compare_with_dense_two_attr(
Expand Down Expand Up @@ -469,7 +481,15 @@ def test_sparse_neg(self):
self.compare_with_dense(paddle.neg, paddle.sparse.neg)

def test_sparse_pow(self):
self.compare_with_dense_one_attr(paddle.pow, paddle.sparse.pow, 3)
self.compare_with_dense_one_attr(
paddle.pow, paddle.sparse.pow, 3, 'float32'
)
self.compare_with_dense_one_attr(
paddle.pow, paddle.sparse.pow, 3, 'complex64'
)
self.compare_with_dense_one_attr(
paddle.pow, paddle.sparse.pow, 3, 'complex128'
)

def test_sparse_mul_scalar(self):
self.compare_with_dense_one_attr(
Expand Down