From 06128b9f0c41375aa7e577c856a0e8563078dd3f Mon Sep 17 00:00:00 2001 From: zyfncg Date: Mon, 20 Dec 2021 10:08:31 +0800 Subject: [PATCH] move the directory of fill kernels in pten (#38219) --- paddle/fluid/pybind/CMakeLists.txt | 2 +- paddle/pten/CMakeLists.txt | 6 +- paddle/pten/include/creation.h | 5 +- paddle/pten/kernels/cpu/CMakeLists.txt | 1 - paddle/pten/kernels/cpu/creation.cc | 87 ------------------- paddle/pten/kernels/cuda/CMakeLists.txt | 2 - paddle/pten/kernels/cuda/creation.cu | 87 ------------------- paddle/pten/kernels/cuda/creation.h | 38 -------- paddle/pten/kernels/eigen/CMakeLists.txt | 3 + paddle/pten/kernels/eigen/full.h | 73 ++++++++++++++++ paddle/pten/kernels/eigen/full_kernel.cc | 47 ++++++++++ paddle/pten/kernels/eigen/full_kernel.cu | 46 ++++++++++ .../kernels/{cpu/creation.h => full_kernel.h} | 10 +-- paddle/pten/kernels/hybird/eigen/fill.h | 34 -------- 14 files changed, 180 insertions(+), 261 deletions(-) delete mode 100644 paddle/pten/kernels/cpu/creation.cc delete mode 100644 paddle/pten/kernels/cuda/creation.cu delete mode 100644 paddle/pten/kernels/cuda/creation.h create mode 100644 paddle/pten/kernels/eigen/full.h create mode 100644 paddle/pten/kernels/eigen/full_kernel.cc create mode 100644 paddle/pten/kernels/eigen/full_kernel.cu rename paddle/pten/kernels/{cpu/creation.h => full_kernel.h} (83%) delete mode 100644 paddle/pten/kernels/hybird/eigen/fill.h diff --git a/paddle/fluid/pybind/CMakeLists.txt b/paddle/fluid/pybind/CMakeLists.txt index 83092e93c77..3cc43cdfe64 100644 --- a/paddle/fluid/pybind/CMakeLists.txt +++ b/paddle/fluid/pybind/CMakeLists.txt @@ -275,7 +275,7 @@ if(WITH_PYTHON) if(NOT ON_INFER) cc_library(paddle_eager SRCS eager.cc eager_functions.cc eager_method.cc eager_properties.cc eager_utils.cc - DEPS eager_api autograd_meta backward grad_node_info pten op_function_common dygraph_function dygraph_node math_cpu linalg_cpu creation_cpu utils_cpu manipulation_cpu accumulation_node global_utils utils python) + DEPS eager_api autograd_meta backward grad_node_info pten op_function_common dygraph_function dygraph_node math_cpu linalg_cpu utils_cpu manipulation_cpu accumulation_node global_utils utils python) add_dependencies(paddle_eager eager_codegen) add_dependencies(paddle_eager eager_op_function_generator_cmd) list(APPEND PYBIND_DEPS paddle_eager) diff --git a/paddle/pten/CMakeLists.txt b/paddle/pten/CMakeLists.txt index 1888d0b5280..0c5e0c9e6ec 100644 --- a/paddle/pten/CMakeLists.txt +++ b/paddle/pten/CMakeLists.txt @@ -24,11 +24,11 @@ add_subdirectory(tests) # make an unity target for compile deps set(PTEN_DEPS convert_utils dense_tensor pten_context kernel_factory kernel_context) -set(PTEN_DEPS ${PTEN_DEPS} scale_kernel_eigen) -set(PTEN_DEPS ${PTEN_DEPS} math_cpu linalg_cpu creation_cpu manipulation_cpu) +set(PTEN_DEPS ${PTEN_DEPS} scale_kernel_eigen full_kernel_eigen) +set(PTEN_DEPS ${PTEN_DEPS} math_cpu linalg_cpu manipulation_cpu) set(PTEN_DEPS ${PTEN_DEPS} nary unary binary) if(WITH_GPU OR WITH_ROCM) - set(PTEN_DEPS ${PTEN_DEPS} math_cuda linalg_cuda creation_cuda manipulation_cuda) + set(PTEN_DEPS ${PTEN_DEPS} math_cuda linalg_cuda manipulation_cuda) endif() if(WITH_XPU) set(PTEN_DEPS ${PTEN_DEPS} manipulation_xpu) diff --git a/paddle/pten/include/creation.h b/paddle/pten/include/creation.h index 69c83c74712..d685d262ebc 100644 --- a/paddle/pten/include/creation.h +++ b/paddle/pten/include/creation.h @@ -16,8 +16,7 @@ #include "paddle/pten/api/lib/utils/storage.h" #include "paddle/pten/include/infermeta.h" -#include "paddle/pten/kernels/cpu/creation.h" -#include "paddle/pten/kernels/cuda/creation.h" +#include "paddle/pten/kernels/full_kernel.h" namespace pten { @@ -36,7 +35,7 @@ DenseTensor FullLike( pten::make_intrusive( dev_ctx.GetPlace()), std::move(out_meta)); - FullLike(dev_ctx, val, &dense_out); + FullLike(dev_ctx, val, &dense_out); return dense_out; } diff --git a/paddle/pten/kernels/cpu/CMakeLists.txt b/paddle/pten/kernels/cpu/CMakeLists.txt index bd890faf471..f45d511602d 100644 --- a/paddle/pten/kernels/cpu/CMakeLists.txt +++ b/paddle/pten/kernels/cpu/CMakeLists.txt @@ -1,5 +1,4 @@ cc_library(math_cpu SRCS math.cc DEPS dense_tensor kernel_context kernel_factory eigen_function blas pten_transpose_cpu) cc_library(linalg_cpu SRCS linalg.cc DEPS dense_tensor kernel_context kernel_factory) -cc_library(creation_cpu SRCS creation.cc DEPS dense_tensor kernel_context kernel_factory eigen_function) cc_library(utils_cpu SRCS utils.cc DEPS dense_tensor kernel_context kernel_factory memory convert_utils) cc_library(manipulation_cpu SRCS manipulation.cc DEPS dense_tensor kernel_context kernel_factory utils_cpu unary) diff --git a/paddle/pten/kernels/cpu/creation.cc b/paddle/pten/kernels/cpu/creation.cc deleted file mode 100644 index 2bb063999f4..00000000000 --- a/paddle/pten/kernels/cpu/creation.cc +++ /dev/null @@ -1,87 +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/creation.h" - -#include "paddle/pten/core/kernel_registry.h" -#include "paddle/pten/kernels/hybird/eigen/fill.h" - -namespace pten { - -template -void FullLike(const CPUContext& dev_ctx, const Scalar& val, DenseTensor* out) { - auto value = val.to(); - using CommonType = typename std::common_type< - float, - typename std::conditional< - std::is_same::value, - float, - T>::type>::type; - - auto common_type_value = static_cast(value); - - PADDLE_ENFORCE_EQ( - (common_type_value >= - static_cast(std::numeric_limits::lowest())) && - (common_type_value <= - static_cast(std::numeric_limits::max())), - true, - paddle::platform::errors::InvalidArgument( - "The filled value is out of range for target type, " - "current kernel type is %s, the range should between %f " - "and %f, but now value is %f.", - typeid(T).name(), - static_cast(std::numeric_limits::lowest()), - static_cast(std::numeric_limits::max()), - static_cast(value))); - eigen::fill(dev_ctx, out, value); -} - -template -void Full(const CPUContext& dev_ctx, - const ScalarArray& shape, - const Scalar& val, - DenseTensor* out) { - out->Resize(paddle::framework::make_ddim(shape.GetData())); - eigen::fill(dev_ctx, out, val.to()); -} - -} // namespace pten - -PT_REGISTER_KERNEL(full_like, - CPU, - ALL_LAYOUT, - pten::FullLike, - float, - double, - int, - int64_t, - bool, - paddle::platform::float16) {} - -PT_REGISTER_KERNEL(full, - CPU, - ALL_LAYOUT, - pten::Full, - float, - double, - uint8_t, - int16_t, - int, - int64_t, - bool, - paddle::platform::float16, - paddle::platform::bfloat16, - paddle::platform::complex, - paddle::platform::complex) {} diff --git a/paddle/pten/kernels/cuda/CMakeLists.txt b/paddle/pten/kernels/cuda/CMakeLists.txt index a9fb2118a9e..b608e18f3da 100644 --- a/paddle/pten/kernels/cuda/CMakeLists.txt +++ b/paddle/pten/kernels/cuda/CMakeLists.txt @@ -1,13 +1,11 @@ if(WITH_GPU) nv_library(math_cuda SRCS math.cu DEPS eigen_function dense_tensor convert_utils kernel_context kernel_factory pten_transpose_cuda) nv_library(linalg_cuda SRCS linalg.cu DEPS eigen_function dense_tensor kernel_context kernel_factory) - nv_library(creation_cuda SRCS creation.cu DEPS eigen_function dense_tensor kernel_context kernel_factory) nv_library(utils_cuda SRCS utils.cu DEPS dense_tensor kernel_context kernel_factory memory convert_utils) nv_library(manipulation_cuda SRCS manipulation.cu DEPS dense_tensor kernel_context kernel_factory utils_cuda unary) elseif(WITH_ROCM) hip_library(math_cuda SRCS math.cu DEPS eigen_function dense_tensor convert_utils kernel_context kernel_factory pten_transpose_cuda) hip_library(linalg_cuda SRCS linalg.cu DEPS eigen_function dense_tensor kernel_context kernel_factory) - hip_library(creation_cuda SRCS creation.cu DEPS eigen_function dense_tensor kernel_context kernel_factory) hip_library(utils_cuda SRCS utils.cu DEPS dense_tensor kernel_context kernel_factory memory convert_utils) hip_library(manipulation_cuda SRCS manipulation.cu DEPS dense_tensor kernel_context kernel_factory utils_cuda unary) endif() diff --git a/paddle/pten/kernels/cuda/creation.cu b/paddle/pten/kernels/cuda/creation.cu deleted file mode 100644 index 444c52e87f5..00000000000 --- a/paddle/pten/kernels/cuda/creation.cu +++ /dev/null @@ -1,87 +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/cuda/creation.h" - -#include "paddle/pten/core/kernel_registry.h" -#include "paddle/pten/kernels/hybird/eigen/fill.h" - -namespace pten { - -template -void FullLike(const CUDAContext& dev_ctx, const Scalar& val, DenseTensor* out) { - auto value = val.to(); - using CommonType = typename std::common_type< - float, - typename std::conditional< - std::is_same::value, - float, - T>::type>::type; - - auto common_type_value = static_cast(value); - - PADDLE_ENFORCE_EQ( - (common_type_value >= - static_cast(std::numeric_limits::lowest())) && - (common_type_value <= - static_cast(std::numeric_limits::max())), - true, - paddle::platform::errors::InvalidArgument( - "The filled value is out of range for target type, " - "current kernel type is %s, the range should between %f " - "and %f, but now value is %f.", - typeid(T).name(), - static_cast(std::numeric_limits::lowest()), - static_cast(std::numeric_limits::max()), - static_cast(value))); - - eigen::fill(dev_ctx, out, val.to()); -} - -template -void Full(const CUDAContext& dev_ctx, - const ScalarArray& shape, - const Scalar& val, - DenseTensor* out) { - out->Resize(paddle::framework::make_ddim(shape.GetData())); - eigen::fill(dev_ctx, out, val.to()); -} - -} // namespace pten - -PT_REGISTER_KERNEL(full_like, - CUDA, - ALL_LAYOUT, - pten::FullLike, - float, - double, - int, - int64_t, - bool, - paddle::platform::float16) {} - -PT_REGISTER_KERNEL(full, - CUDA, - ALL_LAYOUT, - pten::Full, - float, - double, - uint8_t, - int16_t, - int, - int64_t, - bool, - paddle::platform::float16, - paddle::platform::complex, - paddle::platform::complex) {} diff --git a/paddle/pten/kernels/cuda/creation.h b/paddle/pten/kernels/cuda/creation.h deleted file mode 100644 index 72e8fbd0174..00000000000 --- a/paddle/pten/kernels/cuda/creation.h +++ /dev/null @@ -1,38 +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/cuda/cuda_context.h" -#include "paddle/pten/common/scalar.h" -#include "paddle/pten/common/scalar_array.h" -#include "paddle/pten/core/dense_tensor.h" - -namespace pten { - -template -void FullLike(const CUDAContext& dev_ctx, const Scalar& val, DenseTensor* out); - -template -void Full(const CUDAContext& dev_ctx, - const ScalarArray& shape, - const Scalar& val, - DenseTensor* out); - -} // namespace pten - -#endif diff --git a/paddle/pten/kernels/eigen/CMakeLists.txt b/paddle/pten/kernels/eigen/CMakeLists.txt index 200b1101956..9188d0b21ed 100644 --- a/paddle/pten/kernels/eigen/CMakeLists.txt +++ b/paddle/pten/kernels/eigen/CMakeLists.txt @@ -1,7 +1,10 @@ if(WITH_GPU) nv_library(scale_kernel_eigen SRCS scale_kernel.cc DEPS dense_tensor kernel_context kernel_factory eigen_function) + nv_library(full_kernel_eigen SRCS full_kernel.cc full_kernel.cu DEPS kernel_context kernel_factory dense_tensor eigen_function) elseif(WITH_ROCM) hip_library(scale_kernel_eigen SRCS scale_kernel.cc DEPS dense_tensor kernel_context kernel_factory eigen_function) + hip_library(full_kernel_eigen SRCS full_kernel.cc full_kernel.cu DEPS kernel_context kernel_factory dense_tensor eigen_function) else() cc_library(scale_kernel_eigen SRCS scale_kernel.cc DEPS dense_tensor kernel_context kernel_factory eigen_function) + cc_library(full_kernel_eigen SRCS full_kernel.cc DEPS dense_tensor kernel_context kernel_factory eigen_function) endif() diff --git a/paddle/pten/kernels/eigen/full.h b/paddle/pten/kernels/eigen/full.h new file mode 100644 index 00000000000..27d18c4a9ab --- /dev/null +++ b/paddle/pten/kernels/eigen/full.h @@ -0,0 +1,73 @@ +/* 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/common/scalar.h" +#include "paddle/pten/common/scalar_array.h" +#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 fill_(const DeviceContext& context, DenseTensor* tensor, VType val) { + tensor->mutable_data(); + auto t = pten::EigenVector::Flatten(*tensor); + t.device(*context.eigen_device()) = t.constant(static_cast(val)); +} + +template +void Full(const ContextT& dev_ctx, + const ScalarArray& shape, + const Scalar& val, + DenseTensor* out) { + out->Resize(paddle::framework::make_ddim(shape.GetData())); + fill_(dev_ctx, out, val.to()); +} + +template +void FullLike(const ContextT& dev_ctx, const Scalar& val, DenseTensor* out) { + auto value = val.to(); + using CommonType = typename std::common_type< + float, + typename std::conditional< + std::is_same::value, + float, + T>::type>::type; + + auto common_type_value = static_cast(value); + + PADDLE_ENFORCE_EQ( + (common_type_value >= + static_cast(std::numeric_limits::lowest())) && + (common_type_value <= + static_cast(std::numeric_limits::max())), + true, + paddle::platform::errors::InvalidArgument( + "The filled value is out of range for target type, " + "current kernel type is %s, the range should between %f " + "and %f, but now value is %f.", + typeid(T).name(), + static_cast(std::numeric_limits::lowest()), + static_cast(std::numeric_limits::max()), + static_cast(value))); + fill_(dev_ctx, out, value); +} + +} // namespace pten diff --git a/paddle/pten/kernels/eigen/full_kernel.cc b/paddle/pten/kernels/eigen/full_kernel.cc new file mode 100644 index 00000000000..7db9a6b181d --- /dev/null +++ b/paddle/pten/kernels/eigen/full_kernel.cc @@ -0,0 +1,47 @@ +/* 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/full_kernel.h" +#include "paddle/pten/kernels/eigen/full.h" + +#include "paddle/pten/core/kernel_registry.h" + +#include "paddle/pten/backends/all_context.h" + +PT_REGISTER_CTX_KERNEL(full, + CPU, + ALL_LAYOUT, + pten::Full, + float, + double, + uint8_t, + int16_t, + int, + int64_t, + bool, + paddle::platform::float16, + paddle::platform::bfloat16, + paddle::platform::complex, + paddle::platform::complex) {} + +PT_REGISTER_CTX_KERNEL(full_like, + CPU, + ALL_LAYOUT, + pten::FullLike, + float, + double, + int, + int64_t, + bool, + paddle::platform::float16) {} diff --git a/paddle/pten/kernels/eigen/full_kernel.cu b/paddle/pten/kernels/eigen/full_kernel.cu new file mode 100644 index 00000000000..32e48f12f90 --- /dev/null +++ b/paddle/pten/kernels/eigen/full_kernel.cu @@ -0,0 +1,46 @@ +/* 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/eigen/full.h" +#include "paddle/pten/kernels/full_kernel.h" + +#include "paddle/pten/core/kernel_registry.h" + +#include "paddle/pten/backends/all_context.h" + +PT_REGISTER_CTX_KERNEL(full, + CUDA, + ALL_LAYOUT, + pten::Full, + float, + double, + uint8_t, + int16_t, + int, + int64_t, + bool, + paddle::platform::float16, + paddle::platform::complex, + paddle::platform::complex) {} + +PT_REGISTER_CTX_KERNEL(full_like, + CUDA, + ALL_LAYOUT, + pten::FullLike, + float, + double, + int, + int64_t, + bool, + paddle::platform::float16) {} diff --git a/paddle/pten/kernels/cpu/creation.h b/paddle/pten/kernels/full_kernel.h similarity index 83% rename from paddle/pten/kernels/cpu/creation.h rename to paddle/pten/kernels/full_kernel.h index 75d99c0794c..f8abb943667 100644 --- a/paddle/pten/kernels/cpu/creation.h +++ b/paddle/pten/kernels/full_kernel.h @@ -21,13 +21,13 @@ namespace pten { -template -void FullLike(const CPUContext& dev_ctx, const Scalar& val, DenseTensor* out); - -template -void Full(const CPUContext& dev_ctx, +template +void Full(const ContextT& dev_ctx, const ScalarArray& shape, const Scalar& val, DenseTensor* out); +template +void FullLike(const ContextT& dev_ctx, const Scalar& val, DenseTensor* out); + } // namespace pten diff --git a/paddle/pten/kernels/hybird/eigen/fill.h b/paddle/pten/kernels/hybird/eigen/fill.h deleted file mode 100644 index 80e310847e1..00000000000 --- a/paddle/pten/kernels/hybird/eigen/fill.h +++ /dev/null @@ -1,34 +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 - -#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 { -namespace eigen { - -template -void fill(const DeviceContext& context, DenseTensor* tensor, VType val) { - tensor->mutable_data(); - auto t = pten::EigenVector::Flatten(*tensor); - t.device(*context.eigen_device()) = t.constant(static_cast(val)); -} - -} // namespace eigen -} // namespace pten -- GitLab