Skip to content

Commit

Permalink
【complex op No.46】sin_coo/sin_csr(sparse) (#62663)
Browse files Browse the repository at this point in the history
* 添加sparce 的复数

* 复数修改2

* empty for sparse

* 添加了abs进行测试

* 修改为define

* 修正了反向的bug

* 删除了比较用的abs

* 修改了单元测试 删除了测试单元测试

* 格式修改

* 格式修改

* change for switch

* bug fix for sparse sin

* add macro

* 把abs添加到宏里面去

* bug fix

* Remove extraneous code

* python doc string
  • Loading branch information
bapijun authored Jun 24, 2024
1 parent 9b97101 commit 320d8c3
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 41 deletions.
1 change: 0 additions & 1 deletion paddle/fluid/eager/grad_node_info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,6 @@ void GradNodeBase::HandleComplexGradToRealGrad(
for (size_t rank_id = 0; rank_id < slot_out_grads.size(); rank_id++) {
if (bwd_out_meta_[slot_id].size() == 0) continue;
const GradSlotMeta& slot_meta = bwd_out_meta_[slot_id][rank_id];

PADDLE_ENFORCE(
slot_meta.HasTensorMeta() > 0,
paddle::platform::errors::Fatal(
Expand Down
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 @@ -60,7 +60,6 @@
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)
PD_REGISTER_SPARSE_UNARY_CPU_GRAD_KERNEL(atan, Atan)
Expand All @@ -77,6 +76,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(sin, Sin)
PD_REGISTER_SPARSE_UNARY_CPU_GRAD_KERNEL_WITH_COMPLEX(abs, Abs)

PD_REGISTER_KERNEL(cast_coo_grad,
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 @@ -101,7 +101,6 @@ void DivScalarCsrKernel(const Context& dev_ctx,
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)
PD_REGISTER_SPARSE_UNARY_CPU_KERNEL(atan, Atan)
Expand All @@ -119,6 +118,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(sin, Sin)
PD_REGISTER_SPARSE_UNARY_CPU_KERNEL_WITH_COMPLEX(abs, Abs)

PD_REGISTER_KERNEL(divide_scalar_coo,
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 @@ -64,7 +64,6 @@
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)
PD_REGISTER_SPARSE_UNARY_GPU_GRAD_KERNEL(atan, Atan)
Expand All @@ -81,6 +80,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_SPARSE_UNARY_GPU_GRAD_KERNEL_WITH_COMPLEX(sin, Sin)
PD_REGISTER_SPARSE_UNARY_GPU_GRAD_KERNEL_WITH_COMPLEX(abs, Abs)

PD_REGISTER_KERNEL(cast_coo_grad,
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 @@ -94,7 +94,6 @@ void DivScalarCsrKernel(const Context& dev_ctx,
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)
PD_REGISTER_SPARSE_UNARY_GPU_KERNEL(atan, Atan)
Expand All @@ -112,6 +111,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(sin, Sin)
PD_REGISTER_SPARSE_UNARY_GPU_KERNEL_WITH_COMPLEX(abs, Abs)

PD_REGISTER_KERNEL(divide_scalar_coo,
Expand Down
65 changes: 31 additions & 34 deletions paddle/phi/kernels/sparse/impl/unary_kernel_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,35 @@ namespace sparse {
out->mutable_non_zero_elements()); \
}

DEFINE_SPARSE_UNARY_KERNEL(Sin)
#define DEFINE_SPARSE_UNARY_KERNEL_WITH_COMPLEX(prefix) \
template <typename T, typename Context> \
void prefix##CooKernel(const Context& dev_ctx, \
const SparseCooTensor& x, \
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(), out->mutable_non_zero_elements()); \
out->SetIndicesDict(x.GetIndicesDict()); \
} \
\
template <typename T, typename Context> \
void prefix##CsrKernel(const Context& dev_ctx, \
const SparseCsrTensor& x, \
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(), out->mutable_non_zero_elements()); \
}

DEFINE_SPARSE_UNARY_KERNEL(Tan)
DEFINE_SPARSE_UNARY_KERNEL(Asin)
DEFINE_SPARSE_UNARY_KERNEL(Atan)
Expand All @@ -90,39 +118,8 @@ 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 <typename T, typename Context>
void AbsCooKernel(const Context& dev_ctx,
const SparseCooTensor& x,
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::AbsKernel<T, Context>(
dev_ctx, x.non_zero_elements(), out->mutable_non_zero_elements());

out->SetIndicesDict(x.GetIndicesDict());
}

template <typename T, typename Context>
void AbsCsrKernel(const Context& dev_ctx,
const SparseCsrTensor& x,
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::AbsKernel<T, Context>(
dev_ctx, x.non_zero_elements(), out->mutable_non_zero_elements());
}
DEFINE_SPARSE_UNARY_KERNEL_WITH_COMPLEX(Abs)
DEFINE_SPARSE_UNARY_KERNEL_WITH_COMPLEX(Sin)

template <typename T, typename Context>
void ScaleCooKernel(const Context& dev_ctx,
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 @@ -48,7 +48,7 @@ def sin(x, name=None):
out = sin(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`.
Expand Down
6 changes: 5 additions & 1 deletion test/legacy_test/test_sparse_unary_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,11 @@ def test_sparse_abs(self):
self.compare_with_dense(paddle.abs, paddle.sparse.abs, 'complex128')

def test_sparse_sin(self):
self.compare_with_dense(paddle.sin, paddle.sparse.sin)
self.compare_with_dense(paddle.sin, paddle.sparse.sin, 'float16')
self.compare_with_dense(paddle.sin, paddle.sparse.sin, 'float32')
self.compare_with_dense(paddle.sin, paddle.sparse.sin, 'float64')
self.compare_with_dense(paddle.sin, paddle.sparse.sin, 'complex64')
self.compare_with_dense(paddle.sin, paddle.sparse.sin, 'complex128')

def test_sparse_tan(self):
self.compare_with_dense(paddle.tan, paddle.sparse.tan)
Expand Down

0 comments on commit 320d8c3

Please sign in to comment.