Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【complex op No.33】abs_coo/abs_csr(sparse) #62237

Merged
merged 26 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 21 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/api/yaml/sparse_ops.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
1 change: 0 additions & 1 deletion paddle/phi/infermeta/sparse/unary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +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/core/infermeta_utils.h"

namespace phi {
Expand Down
36 changes: 27 additions & 9 deletions paddle/phi/kernels/sparse/cpu/sparse_utils_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,9 @@ PD_REGISTER_KERNEL(dense_to_coo,
int8_t,
int16_t,
int,
int64_t) {}
int64_t,
phi::dtype::complex<float>,
phi::dtype::complex<double>) {}

PD_REGISTER_KERNEL(csr_to_coo,
CPU,
Expand All @@ -342,7 +344,9 @@ PD_REGISTER_KERNEL(csr_to_coo,
int16_t,
int,
int64_t,
bool) {}
bool,
phi::dtype::complex<float>,
phi::dtype::complex<double>) {}

PD_REGISTER_KERNEL(coo_to_csr,
CPU,
Expand All @@ -356,7 +360,9 @@ PD_REGISTER_KERNEL(coo_to_csr,
int16_t,
int,
int64_t,
bool) {}
bool,
phi::dtype::complex<float>,
phi::dtype::complex<double>) {}

PD_REGISTER_KERNEL(dense_to_csr,
CPU,
Expand All @@ -369,7 +375,9 @@ PD_REGISTER_KERNEL(dense_to_csr,
int8_t,
int16_t,
int,
int64_t) {}
int64_t,
phi::dtype::complex<float>,
phi::dtype::complex<double>) {}

PD_REGISTER_KERNEL(coo_to_dense,
CPU,
Expand All @@ -383,7 +391,9 @@ PD_REGISTER_KERNEL(coo_to_dense,
int16_t,
int,
int64_t,
bool) {}
bool,
phi::dtype::complex<float>,
phi::dtype::complex<double>) {}

PD_REGISTER_KERNEL(csr_to_dense,
CPU,
Expand All @@ -397,7 +407,9 @@ PD_REGISTER_KERNEL(csr_to_dense,
int16_t,
int,
int64_t,
bool) {}
bool,
phi::dtype::complex<float>,
phi::dtype::complex<double>) {}

PD_REGISTER_KERNEL(values_coo,
CPU,
Expand All @@ -411,7 +423,9 @@ PD_REGISTER_KERNEL(values_coo,
int16_t,
int,
int64_t,
bool) {
bool,
phi::dtype::complex<float>,
phi::dtype::complex<double>) {
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO);
}

Expand Down Expand Up @@ -442,7 +456,9 @@ PD_REGISTER_KERNEL(values_csr,
int16_t,
int,
int64_t,
bool) {
bool,
phi::dtype::complex<float>,
phi::dtype::complex<double>) {
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR);
}

Expand All @@ -456,4 +472,6 @@ PD_REGISTER_KERNEL(sparse_coo_tensor,
uint8_t,
int16_t,
int,
int64_t) {}
int64_t,
phi::dtype::complex<float>,
phi::dtype::complex<double>) {}
26 changes: 25 additions & 1 deletion paddle/phi/kernels/sparse/cpu/unary_grad_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<float>, \
phi::dtype::complex<double>) { \
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<float>, \
phi::dtype::complex<double>) { \
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)
Expand All @@ -49,12 +72,13 @@ 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_SPARSE_UNARY_CPU_GRAD_KERNEL_WITH_COMPLEX(abs, Abs)

PD_REGISTER_KERNEL(cast_coo_grad,
CPU,
ALL_LAYOUT,
Expand Down
26 changes: 25 additions & 1 deletion paddle/phi/kernels/sparse/cpu/unary_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<float>, \
phi::dtype::complex<double>) { \
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<float>, \
phi::dtype::complex<double>) { \
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)
Expand All @@ -90,13 +113,14 @@ 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_SPARSE_UNARY_CPU_KERNEL_WITH_COMPLEX(abs, Abs)

PD_REGISTER_KERNEL(divide_scalar_coo,
CPU,
ALL_LAYOUT,
Expand Down
19 changes: 13 additions & 6 deletions paddle/phi/kernels/sparse/empty_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +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 "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
Expand Down Expand Up @@ -48,7 +47,6 @@ void EmptyLikeCsrKernel(const Context& dev_ctx,
out->set_meta(x.meta());
dev_ctx.template Alloc<T>(out_values);
}

} // namespace sparse
} // namespace phi

Expand All @@ -63,7 +61,9 @@ PD_REGISTER_KERNEL(empty_like_coo,
int16_t,
int,
int64_t,
bool) {
bool,
phi::dtype::complex<float>,
phi::dtype::complex<double>) {
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO);
}

Expand All @@ -78,7 +78,9 @@ PD_REGISTER_KERNEL(empty_like_csr,
int16_t,
int,
int64_t,
bool) {
bool,
phi::dtype::complex<float>,
phi::dtype::complex<double>) {
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR);
}

Expand All @@ -95,7 +97,9 @@ PD_REGISTER_KERNEL(empty_like_coo,
int16_t,
int,
int64_t,
bool) {
bool,
phi::dtype::complex<float>,
phi::dtype::complex<double>) {
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO);
}

Expand All @@ -111,7 +115,10 @@ PD_REGISTER_KERNEL(empty_like_csr,
int16_t,
int,
int64_t,
bool) {
bool,
phi::dtype::complex<float>,
phi::dtype::complex<double>) {
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR);
}

#endif
36 changes: 27 additions & 9 deletions paddle/phi/kernels/sparse/gpu/sparse_utils_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,9 @@ PD_REGISTER_KERNEL(dense_to_coo,
int8_t,
int16_t,
int,
int64_t) {}
int64_t,
phi::dtype::complex<float>,
phi::dtype::complex<double>) {}

PD_REGISTER_KERNEL(csr_to_coo,
GPU,
Expand All @@ -603,7 +605,9 @@ PD_REGISTER_KERNEL(csr_to_coo,
int16_t,
int,
int64_t,
bool) {}
bool,
phi::dtype::complex<float>,
phi::dtype::complex<double>) {}

PD_REGISTER_KERNEL(coo_to_csr,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这些kernel 对应的grad kernel 也注册上复数吧

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这些kernel 对应的grad kernel 也注册上复数吧

这一次提交里面,在对应的sparse_backward.yaml下面能找到的后向我改掉了,但是对应的to_dense和value的梯度,涉及到
mask_kernel下面的两个kernel
另外还有之前提到的,稀疏格式在backward的时候涉及到的meta丢失的问题,在修改to_dense的梯度的时候也出现了,这一次也改掉了

GPU,
Expand All @@ -617,7 +621,9 @@ PD_REGISTER_KERNEL(coo_to_csr,
int16_t,
int,
int64_t,
bool) {}
bool,
phi::dtype::complex<float>,
phi::dtype::complex<double>) {}

PD_REGISTER_KERNEL(dense_to_csr,
GPU,
Expand All @@ -630,7 +636,9 @@ PD_REGISTER_KERNEL(dense_to_csr,
int8_t,
int16_t,
int,
int64_t) {}
int64_t,
phi::dtype::complex<float>,
phi::dtype::complex<double>) {}

PD_REGISTER_KERNEL(coo_to_dense,
GPU,
Expand All @@ -644,7 +652,9 @@ PD_REGISTER_KERNEL(coo_to_dense,
int16_t,
int,
int64_t,
bool) {}
bool,
phi::dtype::complex<float>,
phi::dtype::complex<double>) {}

PD_REGISTER_KERNEL(csr_to_dense,
GPU,
Expand All @@ -658,7 +668,9 @@ PD_REGISTER_KERNEL(csr_to_dense,
int16_t,
int,
int64_t,
bool) {}
bool,
phi::dtype::complex<float>,
phi::dtype::complex<double>) {}

PD_REGISTER_KERNEL(values_coo,
GPU,
Expand All @@ -672,7 +684,9 @@ PD_REGISTER_KERNEL(values_coo,
int16_t,
int,
int64_t,
bool) {
bool,
phi::dtype::complex<float>,
phi::dtype::complex<double>) {
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO);
}

Expand All @@ -688,7 +702,9 @@ PD_REGISTER_KERNEL(values_csr,
int16_t,
int,
int64_t,
bool) {
bool,
phi::dtype::complex<float>,
phi::dtype::complex<double>) {
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR);
}

Expand Down Expand Up @@ -717,4 +733,6 @@ PD_REGISTER_KERNEL(sparse_coo_tensor,
uint8_t,
int16_t,
int,
int64_t) {}
int64_t,
phi::dtype::complex<float>,
phi::dtype::complex<double>) {}
Loading