From 5fc8bbf79d3551654a83562d23d2cc8a76c60ba2 Mon Sep 17 00:00:00 2001 From: chentianyu03 Date: Wed, 12 Jan 2022 16:07:16 +0800 Subject: [PATCH] [pten]Move dot, conj, sign dev_api into kernel.h (#38862) * move dot_dev api into dot_kernel.h * add infermate header * modify to dotkerel in dot_op.h * mvoe conj dev api into complex_kernel.h * move sign dev api into sign_kernel.h --- paddle/fluid/operators/conj_op.h | 2 +- paddle/fluid/operators/dot_op.h | 3 +- paddle/fluid/operators/sign_op.h | 2 +- paddle/pten/all.h | 1 - paddle/pten/include/linalg.h | 37 ------------------- paddle/pten/include/math.h | 12 ------ paddle/pten/kernels/complex_kernel.h | 4 +- paddle/pten/kernels/cpu/sign_kernel.cc | 3 +- paddle/pten/kernels/dot_kernel.h | 12 +++++- paddle/pten/kernels/gpu/sign_kernel.cu | 2 +- .../pten/kernels/impl/complex_kernel_impl.h | 4 +- paddle/pten/kernels/impl/sign_kernel_impl.h | 4 +- paddle/pten/kernels/sign_kernel.h | 12 +++++- .../pten/tests/kernels/test_conj_dev_api.cc | 2 +- paddle/pten/tests/kernels/test_dot_dev_api.cc | 2 +- 15 files changed, 39 insertions(+), 63 deletions(-) delete mode 100644 paddle/pten/include/linalg.h diff --git a/paddle/fluid/operators/conj_op.h b/paddle/fluid/operators/conj_op.h index 381f4cb66b3..71115c2eba7 100644 --- a/paddle/fluid/operators/conj_op.h +++ b/paddle/fluid/operators/conj_op.h @@ -39,7 +39,7 @@ class ConjKernel : public framework::OpKernel { auto pt_out = paddle::experimental::MakePtenDenseTensor(*out); // call new kernel - pten::ConjKernel(dev_ctx, *pt_x.get(), pt_out.get()); + pten::ConjKernel(dev_ctx, *pt_x.get(), pt_out.get()); } }; diff --git a/paddle/fluid/operators/dot_op.h b/paddle/fluid/operators/dot_op.h index 02ba57ef8d4..8817e2f3ca7 100644 --- a/paddle/fluid/operators/dot_op.h +++ b/paddle/fluid/operators/dot_op.h @@ -46,7 +46,8 @@ class DotKernel : public framework::OpKernel { auto pt_out = paddle::experimental::MakePtenDenseTensor(*out); // call new kernel - pten::DotKernel(dev_ctx, *pt_x.get(), *pt_y.get(), pt_out.get()); + pten::DotKernel(dev_ctx, *pt_x.get(), *pt_y.get(), + pt_out.get()); } }; diff --git a/paddle/fluid/operators/sign_op.h b/paddle/fluid/operators/sign_op.h index b93c062cda2..b8dd44c01b0 100644 --- a/paddle/fluid/operators/sign_op.h +++ b/paddle/fluid/operators/sign_op.h @@ -39,7 +39,7 @@ class SignKernel : public framework::OpKernel { auto pt_out = paddle::experimental::MakePtenDenseTensor(*out); // call new kernel - pten::Sign(dev_ctx, *pt_x.get(), pt_out.get()); + pten::SignKernel(dev_ctx, *pt_x.get(), pt_out.get()); } }; diff --git a/paddle/pten/all.h b/paddle/pten/all.h index 7dd517e5e63..c8be629b10e 100644 --- a/paddle/pten/all.h +++ b/paddle/pten/all.h @@ -17,5 +17,4 @@ limitations under the License. */ // developer apis #include "paddle/pten/include/core.h" #include "paddle/pten/include/infermeta.h" -#include "paddle/pten/include/linalg.h" #include "paddle/pten/include/math.h" diff --git a/paddle/pten/include/linalg.h b/paddle/pten/include/linalg.h deleted file mode 100644 index 71bc518aa89..00000000000 --- a/paddle/pten/include/linalg.h +++ /dev/null @@ -1,37 +0,0 @@ -// 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 - -// See Note: [ How do we organize the kernel directory ] -#include "paddle/pten/api/lib/utils/storage.h" -#include "paddle/pten/include/infermeta.h" -#include "paddle/pten/kernels/dot_kernel.h" - -namespace pten { - -template -DenseTensor Dot(const ContextT& dev_ctx, - const DenseTensor& x, - const DenseTensor& y) { - auto out_meta = DotInferMeta(x.meta(), y.meta()); - pten::DenseTensor dense_out( - pten::make_intrusive( - dev_ctx.GetPlace()), - std::move(out_meta)); - DotKernel(dev_ctx, x, y, &dense_out); - return dense_out; -} - -} // namespace pten diff --git a/paddle/pten/include/math.h b/paddle/pten/include/math.h index 5070d0d4e0e..a4fb7f4d98f 100644 --- a/paddle/pten/include/math.h +++ b/paddle/pten/include/math.h @@ -17,22 +17,10 @@ limitations under the License. */ // See Note: [ How do we organize the kernel directory ] #include "paddle/pten/api/lib/utils/storage.h" #include "paddle/pten/include/infermeta.h" -#include "paddle/pten/kernels/complex_kernel.h" #include "paddle/pten/kernels/scale_kernel.h" namespace pten { -template -DenseTensor Sign(const ContextT& dev_ctx, const DenseTensor& x) { - auto out_meta = UnchangedInferMeta(x.meta()); - pten::DenseTensor dense_out( - pten::make_intrusive( - dev_ctx.GetPlace()), - std::move(out_meta)); - Sign(dev_ctx, x, &dense_out); - return dense_out; -} - template DenseTensor Scale(const ContextT& dev_ctx, const DenseTensor& x, diff --git a/paddle/pten/kernels/complex_kernel.h b/paddle/pten/kernels/complex_kernel.h index e9f717152a4..9dd3d457e4a 100644 --- a/paddle/pten/kernels/complex_kernel.h +++ b/paddle/pten/kernels/complex_kernel.h @@ -15,6 +15,8 @@ limitations under the License. */ #pragma once #include "paddle/pten/core/dense_tensor.h" +#include "paddle/pten/include/infermeta.h" +#include "paddle/pten/kernels/empty_kernel.h" #include "paddle/pten/infermeta/unary.h" #include "paddle/pten/kernels/empty_kernel.h" @@ -27,7 +29,7 @@ void ConjKernel(const Context& dev_ctx, const DenseTensor& x, DenseTensor* out); template DenseTensor Conj(const Context& dev_ctx, const DenseTensor& x) { auto out_meta = UnchangedInferMeta(x.meta()); - auto dense_out = Empty(dev_ctx, std::move(out_meta)); + auto dense_out = pten::Empty(dev_ctx, std::move(out_meta)); ConjKernel(dev_ctx, x, &dense_out); return dense_out; } diff --git a/paddle/pten/kernels/cpu/sign_kernel.cc b/paddle/pten/kernels/cpu/sign_kernel.cc index c6e352f7da4..a7b62822d6e 100644 --- a/paddle/pten/kernels/cpu/sign_kernel.cc +++ b/paddle/pten/kernels/cpu/sign_kernel.cc @@ -21,4 +21,5 @@ limitations under the License. */ // 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) {} +PT_REGISTER_CTX_KERNEL(sign, CPU, ALL_LAYOUT, pten::SignKernel, float, double) { +} diff --git a/paddle/pten/kernels/dot_kernel.h b/paddle/pten/kernels/dot_kernel.h index 5ef66026533..47f1c89109e 100644 --- a/paddle/pten/kernels/dot_kernel.h +++ b/paddle/pten/kernels/dot_kernel.h @@ -15,7 +15,8 @@ #pragma once #include "paddle/pten/core/dense_tensor.h" - +#include "paddle/pten/infermeta/binary.h" +#include "paddle/pten/kernels/empty_kernel.h" namespace pten { template @@ -24,4 +25,13 @@ void DotKernel(const Context& dev_ctx, const DenseTensor& y, DenseTensor* out); +template +DenseTensor Dot(const Context& dev_ctx, + const DenseTensor& x, + const DenseTensor& y) { + auto out_meta = DotInferMeta(x.meta(), y.meta()); + auto dense_out = pten::Empty(dev_ctx, std::move(out_meta)); + DotKernel(dev_ctx, x, y, &dense_out); + return dense_out; +} } // namespace pten diff --git a/paddle/pten/kernels/gpu/sign_kernel.cu b/paddle/pten/kernels/gpu/sign_kernel.cu index 42b39141460..e7eb7e46861 100644 --- a/paddle/pten/kernels/gpu/sign_kernel.cu +++ b/paddle/pten/kernels/gpu/sign_kernel.cu @@ -24,4 +24,4 @@ limitations under the License. */ using float16 = paddle::platform::float16; PT_REGISTER_CTX_KERNEL( - sign, GPU, ALL_LAYOUT, pten::Sign, float, double, float16) {} + sign, GPU, ALL_LAYOUT, pten::SignKernel, float, double, float16) {} diff --git a/paddle/pten/kernels/impl/complex_kernel_impl.h b/paddle/pten/kernels/impl/complex_kernel_impl.h index e0c6825a78a..d7132b05f7f 100644 --- a/paddle/pten/kernels/impl/complex_kernel_impl.h +++ b/paddle/pten/kernels/impl/complex_kernel_impl.h @@ -21,14 +21,14 @@ namespace pten { template -void ConjKernel(const Context& context, +void ConjKernel(const Context& dev_ctx, const DenseTensor& x, DenseTensor* out) { auto numel = x.numel(); auto* x_data = x.data(); auto* out_data = out->mutable_data(); - paddle::platform::ForRange for_range(context, numel); + paddle::platform::ForRange for_range(dev_ctx, numel); paddle::operators::math::ConjFunctor functor(x_data, numel, out_data); for_range(functor); } diff --git a/paddle/pten/kernels/impl/sign_kernel_impl.h b/paddle/pten/kernels/impl/sign_kernel_impl.h index d663808f037..306206f1fc3 100644 --- a/paddle/pten/kernels/impl/sign_kernel_impl.h +++ b/paddle/pten/kernels/impl/sign_kernel_impl.h @@ -23,7 +23,9 @@ limitations under the License. */ namespace pten { template -void Sign(const Context& dev_ctx, const DenseTensor& x, DenseTensor* out) { +void SignKernel(const Context& dev_ctx, + const DenseTensor& x, + DenseTensor* out) { out->mutable_data(); auto eigen_out = pten::EigenVector::Flatten(*out); auto eigen_x = pten::EigenVector::Flatten(x); diff --git a/paddle/pten/kernels/sign_kernel.h b/paddle/pten/kernels/sign_kernel.h index 2cf5ca973f0..ba205fc96a1 100644 --- a/paddle/pten/kernels/sign_kernel.h +++ b/paddle/pten/kernels/sign_kernel.h @@ -15,10 +15,20 @@ limitations under the License. */ #pragma once #include "paddle/pten/core/dense_tensor.h" +#include "paddle/pten/include/infermeta.h" +#include "paddle/pten/kernels/empty_kernel.h" namespace pten { template -void Sign(const Context& dev_ctx, const DenseTensor& x, DenseTensor* out); +void SignKernel(const Context& dev_ctx, const DenseTensor& x, DenseTensor* out); + +template +DenseTensor Sign(const Context& dev_ctx, const DenseTensor& x) { + auto out_meta = UnchangedInferMeta(x.meta()); + auto dense_out = pten::Empty(dev_ctx, std::move(out_meta)); + SignKernel(dev_ctx, x, &dense_out); + return dense_out; +} } // namespace pten diff --git a/paddle/pten/tests/kernels/test_conj_dev_api.cc b/paddle/pten/tests/kernels/test_conj_dev_api.cc index 0438a8f4f46..3392626dc2a 100644 --- a/paddle/pten/tests/kernels/test_conj_dev_api.cc +++ b/paddle/pten/tests/kernels/test_conj_dev_api.cc @@ -15,7 +15,7 @@ limitations under the License. */ #include #include -#include "paddle/pten/include/math.h" +#include "paddle/pten/kernels/complex_kernel.h" #include "paddle/pten/api/lib/utils/allocator.h" #include "paddle/pten/core/dense_tensor.h" diff --git a/paddle/pten/tests/kernels/test_dot_dev_api.cc b/paddle/pten/tests/kernels/test_dot_dev_api.cc index 3fda81d3b5e..6e2166cb673 100644 --- a/paddle/pten/tests/kernels/test_dot_dev_api.cc +++ b/paddle/pten/tests/kernels/test_dot_dev_api.cc @@ -15,7 +15,7 @@ limitations under the License. */ #include #include -#include "paddle/pten/include/linalg.h" +#include "paddle/pten/kernels/dot_kernel.h" #include "paddle/pten/api/lib/utils/allocator.h" #include "paddle/pten/core/dense_tensor.h" -- GitLab