diff --git a/paddle/fluid/pybind/CMakeLists.txt b/paddle/fluid/pybind/CMakeLists.txt index 83092e93c7781a4e3461aaf16090536f742e3407..3cc43cdfe643c59beab110dbd9797b8c1b1e4a71 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 1888d0b52809fbf5e50ee4c7b29b3f678590f06f..0c5e0c9e6ec538dbd4fad590fe74f46ff2d4013f 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 69c83c74712667d6e933270a577ba5dd729de22f..d685d262ebc1c9262e3f176254d87f2af94eb331 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 bd890faf47156dc11a15c67779e94eaa79c2deae..f45d511602d71a175c5f917cd955e5be93a8f431 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 2bb063999f472601e7b2974fc2df83e715e12e31..0000000000000000000000000000000000000000 --- 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 a9fb2118a9eeaaa5edb763e52f31a61d0cb6a3bd..b608e18f3da529fc4f672223c80e93c4be8ab88e 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 444c52e87f559d32018195ca3e7fd17ddc477835..0000000000000000000000000000000000000000 --- 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 72e8fbd017490a1629b1784ebf506a93c29a14ad..0000000000000000000000000000000000000000 --- 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 200b1101956b499de7ece4e6ba9dd3cfb7e27cf6..9188d0b21ed250eac117ef1524c739b0c4d0722e 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 0000000000000000000000000000000000000000..27d18c4a9ab215997669b188a0281b782d19f2db --- /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 0000000000000000000000000000000000000000..7db9a6b181d29740f131c9629eb4a7ed39c968e4 --- /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 0000000000000000000000000000000000000000..32e48f12f906c1148eed1c79ec795e345d3b4d1a --- /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 75d99c0794c40a748116612175161acd4ac699b0..f8abb9436679b19260ee28423d706019b7bfd764 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 80e310847e1865627bcd3e349837acbc07f2c867..0000000000000000000000000000000000000000 --- 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