From bb38b6aa4b2a0c4d3ca333f991580773b2ab585e Mon Sep 17 00:00:00 2001 From: Chen Weihang Date: Wed, 22 Dec 2021 22:02:38 -0600 Subject: [PATCH] move sign kernel impl (#38363) --- paddle/fluid/operators/sign_op.h | 3 +- paddle/pten/api/lib/kernel_declare.h | 4 +-- paddle/pten/kernels/cpu/math.cc | 7 ---- paddle/pten/kernels/cpu/math.h | 3 -- paddle/pten/kernels/cpu/sign_kernel.cc | 24 ++++++++++++++ paddle/pten/kernels/gpu/math.cu | 7 ---- paddle/pten/kernels/gpu/math.h | 3 -- paddle/pten/kernels/gpu/sign_kernel.cu | 27 ++++++++++++++++ paddle/pten/kernels/impl/sign_kernel_impl.h | 36 +++++++++++++++++++++ paddle/pten/kernels/sign_kernel.h | 24 ++++++++++++++ 10 files changed, 114 insertions(+), 24 deletions(-) create mode 100644 paddle/pten/kernels/cpu/sign_kernel.cc create mode 100644 paddle/pten/kernels/gpu/sign_kernel.cu create mode 100644 paddle/pten/kernels/impl/sign_kernel_impl.h create mode 100644 paddle/pten/kernels/sign_kernel.h diff --git a/paddle/fluid/operators/sign_op.h b/paddle/fluid/operators/sign_op.h index b7a46cc546..b93c062cda 100644 --- a/paddle/fluid/operators/sign_op.h +++ b/paddle/fluid/operators/sign_op.h @@ -19,9 +19,8 @@ limitations under the License. */ #include "paddle/fluid/framework/pten_utils.h" #include "paddle/fluid/operators/eigen/eigen_function.h" -// only can include the headers in paddle/pten/api dirs #include "paddle/pten/include/core.h" -#include "paddle/pten/include/math.h" +#include "paddle/pten/kernels/sign_kernel.h" namespace paddle { namespace operators { diff --git a/paddle/pten/api/lib/kernel_declare.h b/paddle/pten/api/lib/kernel_declare.h index 6ca6495e2c..9757290999 100644 --- a/paddle/pten/api/lib/kernel_declare.h +++ b/paddle/pten/api/lib/kernel_declare.h @@ -22,13 +22,13 @@ limitations under the License. */ PT_DECLARE_KERNEL(matmul, CPU, ALL_LAYOUT); PT_DECLARE_KERNEL(cast, CPU, ALL_LAYOUT); -PT_DECLARE_KERNEL(sign, CPU, ALL_LAYOUT); +PT_DECLARE_KERNEL(mean, CPU, ALL_LAYOUT); PT_DECLARE_KERNEL(conj, CPU, ALL_LAYOUT); #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) PT_DECLARE_KERNEL(matmul, GPU, ALL_LAYOUT); PT_DECLARE_KERNEL(cast, GPU, ALL_LAYOUT); -PT_DECLARE_KERNEL(sign, GPU, ALL_LAYOUT); +PT_DECLARE_KERNEL(mean, GPU, ALL_LAYOUT); PT_DECLARE_KERNEL(conj, GPU, ALL_LAYOUT); #endif diff --git a/paddle/pten/kernels/cpu/math.cc b/paddle/pten/kernels/cpu/math.cc index 861ecf2829..ee01cf65d0 100644 --- a/paddle/pten/kernels/cpu/math.cc +++ b/paddle/pten/kernels/cpu/math.cc @@ -17,7 +17,6 @@ #include "paddle/pten/api/ext/dispatch.h" #include "paddle/pten/kernels/hybird/cpu/elementwise.h" #include "paddle/pten/kernels/hybird/eigen/reduce.h" -#include "paddle/pten/kernels/hybird/eigen/sign.h" #include "paddle/pten/kernels/hybird/general/elementwise_functor.h" #include "paddle/pten/kernels/hybird/general/reduce_impl.h" @@ -28,11 +27,6 @@ namespace pten { -template -void Sign(const CPUContext& dev_ctx, const DenseTensor& x, DenseTensor* out) { - eigen::Sign(dev_ctx, x, out); -} - template void Mean(const CPUContext& dev_ctx, const DenseTensor& x, @@ -97,7 +91,6 @@ using complex128 = ::paddle::platform::complex; // NOTE(chenweihang): using bfloat16 will cause redefine with xpu bfloat16 // using bfloat16 = ::paddle::platform::bfloat16; -PT_REGISTER_KERNEL(sign, CPU, ALL_LAYOUT, pten::Sign, float, double) {} PT_REGISTER_KERNEL(mean, CPU, ALL_LAYOUT, pten::Mean, float, double, bool) {} PT_REGISTER_KERNEL(add, CPU, diff --git a/paddle/pten/kernels/cpu/math.h b/paddle/pten/kernels/cpu/math.h index 61e361d37a..1a179218b4 100644 --- a/paddle/pten/kernels/cpu/math.h +++ b/paddle/pten/kernels/cpu/math.h @@ -21,9 +21,6 @@ limitations under the License. */ namespace pten { -template -void Sign(const CPUContext& dev_ctx, const DenseTensor& x, DenseTensor* out); - template void Mean(const CPUContext& dev_ctx, const DenseTensor& x, diff --git a/paddle/pten/kernels/cpu/sign_kernel.cc b/paddle/pten/kernels/cpu/sign_kernel.cc new file mode 100644 index 0000000000..c6e352f7da --- /dev/null +++ b/paddle/pten/kernels/cpu/sign_kernel.cc @@ -0,0 +1,24 @@ +/* Copyright (c) 2021 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. */ + +#include "paddle/pten/kernels/sign_kernel.h" +#include "paddle/pten/kernels/impl/sign_kernel_impl.h" + +#include "paddle/pten/backends/cpu/cpu_context.h" +#include "paddle/pten/core/kernel_registry.h" + +// See Note [ Why still include the fluid headers? ] +#include "paddle/fluid/platform/bfloat16.h" + +PT_REGISTER_CTX_KERNEL(sign, CPU, ALL_LAYOUT, pten::Sign, float, double) {} diff --git a/paddle/pten/kernels/gpu/math.cu b/paddle/pten/kernels/gpu/math.cu index 59d816d237..4fc89a18da 100644 --- a/paddle/pten/kernels/gpu/math.cu +++ b/paddle/pten/kernels/gpu/math.cu @@ -17,7 +17,6 @@ limitations under the License. */ #include "paddle/fluid/operators/reduce_ops/reduce_functor_op.h" #include "paddle/pten/kernels/hybird/cuda/elementwise/elementwise.h" #include "paddle/pten/kernels/hybird/cuda/reduce/reduce.h" -#include "paddle/pten/kernels/hybird/eigen/sign.h" #include "paddle/pten/kernels/hybird/general/elementwise_functor.h" #include "paddle/pten/kernels/hybird/general/reduce_impl.h" @@ -57,11 +56,6 @@ struct DivideFunctor { * Kernels */ -template -void Sign(const GPUContext& dev_ctx, const DenseTensor& x, DenseTensor* out) { - eigen::Sign(dev_ctx, x, out); -} - template void Mean(const GPUContext& dev_ctx, const DenseTensor& x, @@ -101,7 +95,6 @@ using float16 = paddle::platform::float16; using complex64 = ::paddle::platform::complex; using complex128 = ::paddle::platform::complex; -PT_REGISTER_KERNEL(sign, GPU, ALL_LAYOUT, pten::Sign, float, double, float16) {} PT_REGISTER_KERNEL(mean, GPU, ALL_LAYOUT, pten::Mean, float, double, bool) {} PT_REGISTER_KERNEL(add, GPU, diff --git a/paddle/pten/kernels/gpu/math.h b/paddle/pten/kernels/gpu/math.h index 5a872542fb..c1d33a0fcd 100644 --- a/paddle/pten/kernels/gpu/math.h +++ b/paddle/pten/kernels/gpu/math.h @@ -23,9 +23,6 @@ limitations under the License. */ namespace pten { -template -void Sign(const GPUContext& dev_ctx, const DenseTensor& x, DenseTensor* out); - template void Mean(const GPUContext& dev_ctx, const DenseTensor& x, diff --git a/paddle/pten/kernels/gpu/sign_kernel.cu b/paddle/pten/kernels/gpu/sign_kernel.cu new file mode 100644 index 0000000000..42b3914146 --- /dev/null +++ b/paddle/pten/kernels/gpu/sign_kernel.cu @@ -0,0 +1,27 @@ +/* Copyright (c) 2021 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. */ + +#include "paddle/pten/kernels/impl/sign_kernel_impl.h" +#include "paddle/pten/kernels/sign_kernel.h" + +#include "paddle/pten/backends/gpu/gpu_context.h" +#include "paddle/pten/core/kernel_registry.h" + +// See Note [ Why still include the fluid headers? ] +#include "paddle/fluid/platform/float16.h" + +using float16 = paddle::platform::float16; + +PT_REGISTER_CTX_KERNEL( + sign, GPU, ALL_LAYOUT, pten::Sign, float, double, float16) {} diff --git a/paddle/pten/kernels/impl/sign_kernel_impl.h b/paddle/pten/kernels/impl/sign_kernel_impl.h new file mode 100644 index 0000000000..088690ec64 --- /dev/null +++ b/paddle/pten/kernels/impl/sign_kernel_impl.h @@ -0,0 +1,36 @@ +/* Copyright (c) 2021 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. */ + +#pragma once + +#include "paddle/pten/core/dense_tensor.h" +#include "paddle/pten/kernels/hybird/eigen/common.h" + +// See Note [ Why still include the fluid headers? ] +#include "paddle/fluid/operators/eigen/eigen_function.h" + +namespace pten { + +template +void Sign(const ContextT& dev_ctx, const DenseTensor& x, DenseTensor* out) { + out->mutable_data(); + auto eigen_out = pten::EigenVector::Flatten(*out); + auto eigen_x = pten::EigenVector::Flatten(x); + + auto& dev = *dev_ctx.eigen_device(); + paddle::operators::EigenSign, T>::Eval( + dev, eigen_out, eigen_x); +} + +} // namespace pten diff --git a/paddle/pten/kernels/sign_kernel.h b/paddle/pten/kernels/sign_kernel.h new file mode 100644 index 0000000000..bbb3f45c9a --- /dev/null +++ b/paddle/pten/kernels/sign_kernel.h @@ -0,0 +1,24 @@ +/* Copyright (c) 2021 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. */ + +#pragma once + +#include "paddle/pten/core/dense_tensor.h" + +namespace pten { + +template +void Sign(const ContextT& dev_ctx, const DenseTensor& x, DenseTensor* out); + +} // namespace pten -- GitLab