diff --git a/paddle/pten/CMakeLists.txt b/paddle/pten/CMakeLists.txt index 5e961ce23dbaa144936a338af3beac41b8be0a20..5a06ef11add72fdfdf9c6f9396d04f816d154ba6 100644 --- a/paddle/pten/CMakeLists.txt +++ b/paddle/pten/CMakeLists.txt @@ -28,13 +28,10 @@ get_property(pten_kernels GLOBAL PROPERTY PTEN_KERNELS) # keep this message for debug, remove it later if needless message(STATUS "All standard pten kernels: ${pten_kernels}") set(PTEN_DEPS ${PTEN_DEPS} ${pten_kernels}) -set(PTEN_DEPS ${PTEN_DEPS} math_cpu linalg_cpu manipulation_cpu) +set(PTEN_DEPS ${PTEN_DEPS} math_cpu linalg_cpu) set(PTEN_DEPS ${PTEN_DEPS} nary unary binary) if(WITH_GPU OR WITH_ROCM) - set(PTEN_DEPS ${PTEN_DEPS} math_gpu linalg_gpu manipulation_gpu) -endif() -if(WITH_XPU) - set(PTEN_DEPS ${PTEN_DEPS} manipulation_xpu) + set(PTEN_DEPS ${PTEN_DEPS} math_gpu linalg_gpu) endif() cc_library(pten SRCS all.cc DEPS ${PTEN_DEPS}) diff --git a/paddle/pten/api/lib/kernel_declare.h b/paddle/pten/api/lib/kernel_declare.h index a5d9537097a97bbe007a44716446f36a1eac98eb..3b7d5ef157bbe30fb224986710688ea220a0a0b4 100644 --- a/paddle/pten/api/lib/kernel_declare.h +++ b/paddle/pten/api/lib/kernel_declare.h @@ -21,15 +21,9 @@ limitations under the License. */ // file name of the kernel, and this header file will be removed PT_DECLARE_KERNEL(matmul, CPU, ALL_LAYOUT); -PT_DECLARE_KERNEL(reshape, CPU, ALL_LAYOUT); PT_DECLARE_KERNEL(mean, CPU, ALL_LAYOUT); #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) PT_DECLARE_KERNEL(matmul, GPU, ALL_LAYOUT); -PT_DECLARE_KERNEL(reshape, GPU, ALL_LAYOUT); PT_DECLARE_KERNEL(mean, GPU, ALL_LAYOUT); #endif - -#ifdef PADDLE_WITH_XPU -PT_DECLARE_KERNEL(reshape, XPU, ALL_LAYOUT); -#endif diff --git a/paddle/pten/include/manipulation.h b/paddle/pten/include/manipulation.h index 2d429c060f4498fa492f3751ceeb8123bb9b48d6..7015dce95d02448ec16eee126071a476cc6b1d76 100644 --- a/paddle/pten/include/manipulation.h +++ b/paddle/pten/include/manipulation.h @@ -18,10 +18,8 @@ #include "paddle/pten/api/lib/utils/storage.h" #include "paddle/pten/include/infermeta.h" #include "paddle/pten/kernels/cast_kernel.h" -#include "paddle/pten/kernels/cpu/manipulation.h" #include "paddle/pten/kernels/flatten_kernel.h" -#include "paddle/pten/kernels/gpu/manipulation.h" -#include "paddle/pten/kernels/xpu/manipulation.h" +#include "paddle/pten/kernels/reshape_kernel.h" namespace pten { @@ -62,7 +60,7 @@ DenseTensor Reshape(const ContextT& dev_ctx, pten::make_intrusive( dev_ctx.GetPlace()), std::move(out_meta)); - Reshape(dev_ctx, x, ScalarArray(shape), &dense_out); + Reshape(dev_ctx, x, ScalarArray(shape), &dense_out); return dense_out; } diff --git a/paddle/pten/kernels/CMakeLists.txt b/paddle/pten/kernels/CMakeLists.txt index 4d72d00a40af1be71739ca52ef49274cbf336925..be4f0d5b191ff326a5e8aab416196a8608f4236e 100644 --- a/paddle/pten/kernels/CMakeLists.txt +++ b/paddle/pten/kernels/CMakeLists.txt @@ -28,8 +28,6 @@ set(COMMON_KERNEL_DEPS dense_tensor kernel_context kernel_factory) set(COMMON_KERNEL_DEPS ${COMMON_KERNEL_DEPS} eigen_function) # auto build kernel targets by cmake -register_kernels(EXCLUDES flatten_kernel DEPS ${COMMON_KERNEL_DEPS}) -# TODO(chenweihang): auto parse compile deps by include headers later -kernel_library(flatten_kernel DEPS ${COMMON_KERNEL_DEPS} copy_kernel unary) +register_kernels(DEPS ${COMMON_KERNEL_DEPS}) copy_if_different(${kernel_declare_file} ${kernel_declare_file_final}) diff --git a/paddle/pten/kernels/cpu/CMakeLists.txt b/paddle/pten/kernels/cpu/CMakeLists.txt index b67bb6296bc98b4f77857b2fa9984746b34c3a4f..24ce40c2451dff70f5fa6d23003473cb251711d7 100644 --- a/paddle/pten/kernels/cpu/CMakeLists.txt +++ b/paddle/pten/kernels/cpu/CMakeLists.txt @@ -1,3 +1,2 @@ cc_library(math_cpu SRCS math.cc DEPS dense_tensor kernel_context kernel_factory eigen_function blas pten_transpose_cpu cast_kernel) cc_library(linalg_cpu SRCS linalg.cc DEPS dense_tensor kernel_context kernel_factory) -cc_library(manipulation_cpu SRCS manipulation.cc DEPS dense_tensor kernel_context kernel_factory copy_kernel unary) diff --git a/paddle/pten/kernels/cpu/manipulation.cc b/paddle/pten/kernels/cpu/manipulation.cc deleted file mode 100644 index e55eb316325107db9142fcb36ae3e1935acf2e82..0000000000000000000000000000000000000000 --- a/paddle/pten/kernels/cpu/manipulation.cc +++ /dev/null @@ -1,51 +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. - -#include "paddle/pten/kernels/cpu/manipulation.h" -#include "paddle/pten/api/ext/dispatch.h" -#include "paddle/pten/infermeta/unary.h" -#include "paddle/pten/kernels/copy_kernel.h" -#include "paddle/pten/kernels/hybird/general/manipulation.h" - -namespace pten { - -void Reshape(const CPUContext& dev_ctx, - const DenseTensor& x, - const ScalarArray& shape, - DenseTensor* out) { - auto out_meta = InferMetaFromVecValue(x.meta(), shape.GetData()); - if (x.data() == out->data() && x.numel() == out->numel()) { - out->Resize(out_meta.dims); - return; - } - pten::Copy(dev_ctx, x, false, out); - out->Resize(out_meta.dims); - out->ResetLoD(x.lod()); -} - -void ReshapeWithXShape(const CPUContext& dev_ctx, - const DenseTensor& x, - const ScalarArray& shape, - DenseTensor* xshape, - DenseTensor* out) { - general::SetXShape(x, xshape); - Reshape(dev_ctx, x, shape, out); -} - -} // namespace pten - -PT_REGISTER_NO_TEMPLATE_KERNEL( - reshape, CPU, ALL_LAYOUT, pten::Reshape, ALL_DTYPE) {} -PT_REGISTER_NO_TEMPLATE_KERNEL( - reshape_with_xshape, CPU, ALL_LAYOUT, pten::ReshapeWithXShape, ALL_DTYPE) {} diff --git a/paddle/pten/kernels/flatten_kernel.cc b/paddle/pten/kernels/flatten_kernel.cc index 0dc593618637f5f2a9feaaf03aab6a11f8390c2c..94605741708dffe08936fdf54dc84fc84011e23e 100644 --- a/paddle/pten/kernels/flatten_kernel.cc +++ b/paddle/pten/kernels/flatten_kernel.cc @@ -43,7 +43,7 @@ void FlattenWithXShape(const ContextT& dev_ctx, DenseTensor* out, DenseTensor* xshape) { Flatten(dev_ctx, x, start_axis, stop_axis, out); - functions::SetXShape(x, xshape); + funcs::SetXShape(x, xshape); } } // namespace pten diff --git a/paddle/pten/kernels/funcs/common_shape.h b/paddle/pten/kernels/funcs/common_shape.h index 3fa129014ebc5626745d5a30faeb9ea7059411f2..f0678e4706e04aaa3a039c71d354532d3fe00792 100644 --- a/paddle/pten/kernels/funcs/common_shape.h +++ b/paddle/pten/kernels/funcs/common_shape.h @@ -17,7 +17,7 @@ limitations under the License. */ #include "paddle/pten/core/dense_tensor.h" namespace pten { -namespace functions { +namespace funcs { inline void SetXShape(const DenseTensor& x, DenseTensor* xshape) { const auto& in_dims = x.meta().dims; @@ -30,5 +30,5 @@ inline void SetXShape(const DenseTensor& x, DenseTensor* xshape) { xshape->ResetLoD(x.meta().lod); } -} // namespace functions +} // namespace funcs } // namespace pten diff --git a/paddle/pten/kernels/gpu/CMakeLists.txt b/paddle/pten/kernels/gpu/CMakeLists.txt index 99e3871044f2ff158d0c9df4f10a528852ba1b4e..b35dd3ce303d70fc7971dbf85d39bd8625898b23 100644 --- a/paddle/pten/kernels/gpu/CMakeLists.txt +++ b/paddle/pten/kernels/gpu/CMakeLists.txt @@ -1,9 +1,7 @@ if(WITH_GPU) - nv_library(math_gpu SRCS math.cu DEPS eigen_function dense_tensor convert_utils kernel_context kernel_factory pten_transpose_gpu cast_kernel) + nv_library(math_gpu SRCS math.cu DEPS eigen_function dense_tensor convert_utils kernel_context kernel_factory pten_transpose_gpu cast_kernel copy_kernel) nv_library(linalg_gpu SRCS linalg.cu DEPS eigen_function dense_tensor kernel_context kernel_factory) - nv_library(manipulation_gpu SRCS manipulation.cu DEPS dense_tensor kernel_context kernel_factory copy_kernel unary) elseif(WITH_ROCM) - hip_library(math_gpu SRCS math.cu DEPS eigen_function dense_tensor convert_utils kernel_context kernel_factory pten_transpose_gpu cast_kernel) + hip_library(math_gpu SRCS math.cu DEPS eigen_function dense_tensor convert_utils kernel_context kernel_factory pten_transpose_gpu cast_kernel copy_kernel) hip_library(linalg_gpu SRCS linalg.cu DEPS eigen_function dense_tensor kernel_context kernel_factory) - hip_library(manipulation_gpu SRCS manipulation.cu DEPS dense_tensor kernel_context kernel_factory copy_kernel unary) endif() diff --git a/paddle/pten/kernels/gpu/manipulation.cu b/paddle/pten/kernels/gpu/manipulation.cu deleted file mode 100644 index 7e99510c015d07395778e238c041890546a49796..0000000000000000000000000000000000000000 --- a/paddle/pten/kernels/gpu/manipulation.cu +++ /dev/null @@ -1,51 +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. - -#include "paddle/pten/kernels/gpu/manipulation.h" - -#include "paddle/pten/infermeta/unary.h" -#include "paddle/pten/kernels/copy_kernel.h" -#include "paddle/pten/kernels/hybird/general/manipulation.h" - -namespace pten { - -void Reshape(const GPUContext& dev_ctx, - const DenseTensor& x, - const ScalarArray& shape, - DenseTensor* out) { - auto out_meta = InferMetaFromVecValue(x.meta(), shape.GetData()); - if (x.data() == out->data() && x.numel() == out->numel()) { - out->Resize(out_meta.dims); - return; - } - pten::Copy(dev_ctx, x, false, out); - out->Resize(out_meta.dims); - out->ResetLoD(x.lod()); -} - -void ReshapeWithXShape(const GPUContext& dev_ctx, - const DenseTensor& x, - const ScalarArray& shape, - DenseTensor* xshape, - DenseTensor* out) { - general::SetXShape(x, xshape); - Reshape(dev_ctx, x, shape, out); -} - -} // namespace pten - -PT_REGISTER_NO_TEMPLATE_KERNEL( - reshape, GPU, ALL_LAYOUT, pten::Reshape, ALL_DTYPE) {} -PT_REGISTER_NO_TEMPLATE_KERNEL( - reshape_with_xshape, GPU, ALL_LAYOUT, pten::ReshapeWithXShape, ALL_DTYPE) {} diff --git a/paddle/pten/kernels/gpu/manipulation.h b/paddle/pten/kernels/gpu/manipulation.h deleted file mode 100644 index a099763706b2d081a54de0484c91321919310ee3..0000000000000000000000000000000000000000 --- a/paddle/pten/kernels/gpu/manipulation.h +++ /dev/null @@ -1,40 +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 - -// CUDA and HIP use same api -#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) - -#include "paddle/pten/backends/gpu/gpu_context.h" -#include "paddle/pten/common/scalar_array.h" -#include "paddle/pten/core/dense_tensor.h" -#include "paddle/pten/core/kernel_registry.h" - -namespace pten { - -void Reshape(const GPUContext& dev_ctx, - const DenseTensor& x, - const ScalarArray& shape, - DenseTensor* out); - -void ReshapeWithXShape(const GPUContext& dev_ctx, - const DenseTensor& x, - const ScalarArray& shape, - DenseTensor* xshape, - DenseTensor* out); - -} // namespace pten - -#endif diff --git a/paddle/pten/kernels/reshape_kernel.cc b/paddle/pten/kernels/reshape_kernel.cc new file mode 100644 index 0000000000000000000000000000000000000000..ea1f03a017ac91cb2caf7b91a57a8ea905258b99 --- /dev/null +++ b/paddle/pten/kernels/reshape_kernel.cc @@ -0,0 +1,77 @@ +// 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/reshape_kernel.h" +#include "paddle/pten/backends/all_context.h" +#include "paddle/pten/core/kernel_registry.h" +#include "paddle/pten/infermeta/unary.h" +#include "paddle/pten/kernels/copy_kernel.h" +#include "paddle/pten/kernels/funcs/common_shape.h" + +namespace pten { + +template +void Reshape(const ContextT& dev_ctx, + const DenseTensor& x, + const ScalarArray& shape, + DenseTensor* out) { + auto out_meta = InferMetaFromVecValue(x.meta(), shape.GetData()); + if (x.data() == out->data() && x.numel() == out->numel()) { + out->Resize(out_meta.dims); + return; + } + pten::Copy(dev_ctx, x, false, out); + out->Resize(out_meta.dims); + out->ResetLoD(x.lod()); +} + +template +void ReshapeWithXShape(const ContextT& dev_ctx, + const DenseTensor& x, + const ScalarArray& shape, + DenseTensor* xshape, + DenseTensor* out) { + funcs::SetXShape(x, xshape); + Reshape(dev_ctx, x, shape, out); +} + +} // namespace pten + +PT_REGISTER_GENERAL_KERNEL( + reshape, CPU, ALL_LAYOUT, pten::Reshape, ALL_DTYPE) {} +PT_REGISTER_GENERAL_KERNEL(reshape_with_xshape, + CPU, + ALL_LAYOUT, + pten::ReshapeWithXShape, + ALL_DTYPE) {} + +#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) +PT_REGISTER_GENERAL_KERNEL( + reshape, GPU, ALL_LAYOUT, pten::Reshape, ALL_DTYPE) {} +PT_REGISTER_GENERAL_KERNEL(reshape_with_xshape, + GPU, + ALL_LAYOUT, + pten::ReshapeWithXShape, + ALL_DTYPE) {} +#endif + +#ifdef PADDLE_WITH_XPU +PT_REGISTER_GENERAL_KERNEL( + reshape, XPU, ALL_LAYOUT, pten::Reshape, ALL_DTYPE) {} +PT_REGISTER_GENERAL_KERNEL(reshape_with_xshape, + XPU, + ALL_LAYOUT, + pten::ReshapeWithXShape, + ALL_DTYPE) {} +#endif diff --git a/paddle/pten/kernels/cpu/manipulation.h b/paddle/pten/kernels/reshape_kernel.h similarity index 84% rename from paddle/pten/kernels/cpu/manipulation.h rename to paddle/pten/kernels/reshape_kernel.h index 7eaa430f5f6652c309a4a246305fcb29a781ba6c..d9ccd0449bff0206d45a9a130c55e4c3e1b08597 100644 --- a/paddle/pten/kernels/cpu/manipulation.h +++ b/paddle/pten/kernels/reshape_kernel.h @@ -14,19 +14,19 @@ limitations under the License. */ #pragma once -#include "paddle/pten/backends/cpu/cpu_context.h" #include "paddle/pten/common/scalar_array.h" #include "paddle/pten/core/dense_tensor.h" -#include "paddle/pten/core/kernel_registry.h" namespace pten { -void Reshape(const CPUContext& dev_ctx, +template +void Reshape(const ContextT& dev_ctx, const DenseTensor& x, const ScalarArray& shape, DenseTensor* out); -void ReshapeWithXShape(const CPUContext& dev_ctx, +template +void ReshapeWithXShape(const ContextT& dev_ctx, const DenseTensor& x, const ScalarArray& shape, DenseTensor* xshape, diff --git a/paddle/pten/kernels/xpu/CMakeLists.txt b/paddle/pten/kernels/xpu/CMakeLists.txt index c6d66b16512785156d4ca846b537d1e9612a0e2f..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 100644 --- a/paddle/pten/kernels/xpu/CMakeLists.txt +++ b/paddle/pten/kernels/xpu/CMakeLists.txt @@ -1 +0,0 @@ -cc_library(manipulation_xpu SRCS manipulation.cc DEPS dense_tensor kernel_context kernel_factory copy_kernel unary) diff --git a/paddle/pten/kernels/xpu/manipulation.cc b/paddle/pten/kernels/xpu/manipulation.cc deleted file mode 100644 index 4d0ed7cb825811f302ee1be399c3f01d9b1f40e2..0000000000000000000000000000000000000000 --- a/paddle/pten/kernels/xpu/manipulation.cc +++ /dev/null @@ -1,48 +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. - -#include "paddle/pten/kernels/xpu/manipulation.h" -#include "paddle/pten/infermeta/unary.h" -#include "paddle/pten/kernels/copy_kernel.h" -#include "paddle/pten/kernels/hybird/general/manipulation.h" - -namespace pten { - -void Reshape(const XPUContext& dev_ctx, - const DenseTensor& x, - const ScalarArray& shape, - DenseTensor* out) { - auto out_meta = InferMetaFromVecValue(x.meta(), shape.GetData()); - if (x.data() == out->data() && x.numel() == out->numel()) { - out->Resize(out_meta.dims); - return; - } - pten::Copy(dev_ctx, x, false, out); - out->Resize(out_meta.dims); - out->ResetLoD(x.lod()); -} - -void ReshapeWithXShape(const XPUContext& dev_ctx, - const DenseTensor& x, - const ScalarArray& shape, - DenseTensor* xshape, - DenseTensor* out) { - general::SetXShape(x, xshape); - Reshape(dev_ctx, x, shape, out); -} - -} // namespace pten - -PT_REGISTER_NO_TEMPLATE_KERNEL( - reshape, XPU, ALL_LAYOUT, pten::Reshape, ALL_DTYPE) {} diff --git a/paddle/pten/kernels/xpu/manipulation.h b/paddle/pten/kernels/xpu/manipulation.h deleted file mode 100644 index b1557a279fafcc68546d49e9fb0a72049b6d8509..0000000000000000000000000000000000000000 --- a/paddle/pten/kernels/xpu/manipulation.h +++ /dev/null @@ -1,39 +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 - -#ifdef PADDLE_WITH_XPU - -#include "paddle/pten/backends/xpu/xpu_context.h" -#include "paddle/pten/common/scalar_array.h" -#include "paddle/pten/core/dense_tensor.h" -#include "paddle/pten/core/kernel_registry.h" - -namespace pten { - -void Reshape(const XPUContext& dev_ctx, - const DenseTensor& x, - const ScalarArray& shape, - DenseTensor* out); - -void ReshapeWithXShape(const XPUContext& dev_ctx, - const DenseTensor& x, - const ScalarArray& shape, - DenseTensor* xshape, - DenseTensor* out); - -} // namespace pten - -#endif