diff --git a/paddle/pten/CMakeLists.txt b/paddle/pten/CMakeLists.txt index 7f6a64a1bb2e7..4538016333e5e 100644 --- a/paddle/pten/CMakeLists.txt +++ b/paddle/pten/CMakeLists.txt @@ -23,7 +23,7 @@ add_subdirectory(ops) add_subdirectory(tests) # make an unity target for compile deps -set(PTEN_DEPS convert_utils dense_tensor kernel_factory kernel_context) +set(PTEN_DEPS convert_utils dense_tensor pten_context kernel_factory kernel_context) set(PTEN_DEPS ${PTEN_DEPS} math_cpu linalg_cpu creation_cpu manipulation_cpu) set(PTEN_DEPS ${PTEN_DEPS} nary unary binary) if(WITH_GPU OR WITH_ROCM) diff --git a/paddle/pten/api/lib/CMakeLists.txt b/paddle/pten/api/lib/CMakeLists.txt index d1e60c4505d6b..1c2b3823920d6 100644 --- a/paddle/pten/api/lib/CMakeLists.txt +++ b/paddle/pten/api/lib/CMakeLists.txt @@ -10,7 +10,7 @@ else() cc_library(pten_tensor SRCS tensor.cc DEPS tensor_base dense_tensor pten_api_utils ext_compat_utils enforce) endif() -cc_library(kernel_dispatch SRCS kernel_dispatch.cc DEPS pten_tensor device_context kernel_factory) +cc_library(kernel_dispatch SRCS kernel_dispatch.cc DEPS pten_tensor pten_context kernel_factory) cc_library(op_meta_info SRCS op_meta_info.cc DEPS pten_tensor) diff --git a/paddle/pten/api/lib/kernel_dispatch.cc b/paddle/pten/api/lib/kernel_dispatch.cc index 97b3bf281fc8e..7930869632a66 100644 --- a/paddle/pten/api/lib/kernel_dispatch.cc +++ b/paddle/pten/api/lib/kernel_dispatch.cc @@ -51,8 +51,7 @@ std::size_t CountLeadingZeros(uint64_t val) { } // namespace detail -paddle::platform::DeviceContext* GetDeviceContextByBackend( - pten::Backend backend) { +pten::DeviceContext* GetDeviceContextByBackend(pten::Backend backend) { auto& pool = paddle::platform::DeviceContextPool::Instance(); return pool.Get(pten::TransToFluidPlace(backend)); } diff --git a/paddle/pten/api/lib/kernel_dispatch.h b/paddle/pten/api/lib/kernel_dispatch.h index 1bba16d107d48..9c83e0d3d4e9e 100644 --- a/paddle/pten/api/lib/kernel_dispatch.h +++ b/paddle/pten/api/lib/kernel_dispatch.h @@ -21,31 +21,22 @@ limitations under the License. */ #include "paddle/pten/api/include/tensor.h" #include "paddle/pten/api/lib/backend_set.h" #include "paddle/pten/api/lib/data_type_set.h" +#include "paddle/pten/backends/all_context.h" #include "paddle/pten/common/data_type.h" #include "paddle/pten/common/layout.h" // TODO(chenweihang): split Key, Kernel, Factory into diff files #include "paddle/pten/core/kernel_factory.h" -// See Note [ Why still include the fluid headers? ] -#include "paddle/fluid/platform/device_context.h" - namespace paddle { namespace experimental { -// TODO(shixiaowei): replaced by new DeviceContext later -using CPUContext = paddle::platform::CPUDeviceContext; -#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) -using CUDAContext = paddle::platform::CUDADeviceContext; -#endif - namespace detail { BackendSet GetTensorBackendSet(const Tensor& t); std::size_t CountLeadingZeros(uint64_t val); } // namespace detail -paddle::platform::DeviceContext* GetDeviceContextByBackend( - pten::Backend backend); +pten::DeviceContext* GetDeviceContextByBackend(pten::Backend backend); // TODO(chenweihang): support DataLayout and DataType selected struct KernelKeySet { diff --git a/paddle/pten/backends/CMakeLists.txt b/paddle/pten/backends/CMakeLists.txt index e69de29bb2d1d..af49ad61a48f5 100644 --- a/paddle/pten/backends/CMakeLists.txt +++ b/paddle/pten/backends/CMakeLists.txt @@ -0,0 +1 @@ +cc_library(pten_context SRCS all_context.cc DEPS device_context) diff --git a/paddle/pten/backends/all_context.cc b/paddle/pten/backends/all_context.cc new file mode 100644 index 0000000000000..eb73e41a95508 --- /dev/null +++ b/paddle/pten/backends/all_context.cc @@ -0,0 +1,17 @@ +/* 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/backends/all_context.h" + +namespace pten {} // namespace pten diff --git a/paddle/pten/backends/all_context.h b/paddle/pten/backends/all_context.h new file mode 100644 index 0000000000000..d056af1b3a0d9 --- /dev/null +++ b/paddle/pten/backends/all_context.h @@ -0,0 +1,31 @@ +/* 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 + +// Note: Some scenarios need to include all types of Context declarations. +// In order to avoid including the header files of each backend in turn, +// add this header file +// Note: Limit the entry of DeviceContext to backends to avoid multiple include +// path replacement after implementing pten DeviceContext + +#include "paddle/pten/backends/cpu/cpu_context.h" +#include "paddle/pten/backends/cuda/cuda_context.h" +#include "paddle/pten/backends/npu/npu_context.h" +#include "paddle/pten/backends/xpu/xpu_context.h" + +namespace pten { +using DeviceContext = paddle::platform::DeviceContext; +using DeviceContextPool = paddle::platform::DeviceContextPool; +} // namespace pten diff --git a/paddle/pten/backends/cpu/cpu_context.h b/paddle/pten/backends/cpu/cpu_context.h new file mode 100644 index 0000000000000..b161e98d63617 --- /dev/null +++ b/paddle/pten/backends/cpu/cpu_context.h @@ -0,0 +1,22 @@ +/* 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 [ Why still include the fluid headers? ] +#include "paddle/fluid/platform/device_context.h" + +namespace pten { +using CPUContext = paddle::platform::CPUDeviceContext; +} // namespace pten diff --git a/paddle/pten/backends/cuda/cuda_context.h b/paddle/pten/backends/cuda/cuda_context.h new file mode 100644 index 0000000000000..332fdd2fdaf43 --- /dev/null +++ b/paddle/pten/backends/cuda/cuda_context.h @@ -0,0 +1,26 @@ +/* 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 + +#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) + +// See Note [ Why still include the fluid headers? ] +#include "paddle/fluid/platform/device_context.h" + +namespace pten { +using CUDAContext = paddle::platform::CUDADeviceContext; +} // namespace pten + +#endif diff --git a/paddle/pten/backends/npu/npu_context.h b/paddle/pten/backends/npu/npu_context.h new file mode 100644 index 0000000000000..bb17a1bea6df3 --- /dev/null +++ b/paddle/pten/backends/npu/npu_context.h @@ -0,0 +1,26 @@ +/* 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_ASCEND_CL + +// See Note [ Why still include the fluid headers? ] +#include "paddle/fluid/platform/device_context.h" + +namespace pten { +using NPUContext = paddle::platform::NPUDeviceContext; +} // namespace pten + +#endif // PADDLE_WITH_ASCEND_CL diff --git a/paddle/pten/backends/xpu/xpu_context.h b/paddle/pten/backends/xpu/xpu_context.h new file mode 100644 index 0000000000000..94d2a1532f636 --- /dev/null +++ b/paddle/pten/backends/xpu/xpu_context.h @@ -0,0 +1,26 @@ +/* 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 + +// See Note [ Why still include the fluid headers? ] +#include "paddle/fluid/platform/device_context.h" + +namespace pten { +using XPUContext = paddle::platform::XPUDeviceContext; +} // namespace pten + +#endif // PADDLE_WITH_XPU diff --git a/paddle/pten/core/CMakeLists.txt b/paddle/pten/core/CMakeLists.txt index f02678538cb8e..63cae6cc70867 100644 --- a/paddle/pten/core/CMakeLists.txt +++ b/paddle/pten/core/CMakeLists.txt @@ -7,7 +7,7 @@ else() endif() cc_library(kernel_factory SRCS kernel_factory.cc DEPS enforce convert_utils) -cc_library(kernel_context SRCS kernel_context.cc DEPS enforce device_context) +cc_library(kernel_context SRCS kernel_context.cc DEPS enforce pten_context) cc_library(tensor_base SRCS tensor_base.cc allocator.cc storage.cc DEPS enforce) cc_library(tensor_meta SRCS tensor_meta.cc DEPS enforce) diff --git a/paddle/pten/core/kernel_utils.h b/paddle/pten/core/kernel_utils.h index ad7387e15295f..82ffa573870df 100644 --- a/paddle/pten/core/kernel_utils.h +++ b/paddle/pten/core/kernel_utils.h @@ -14,6 +14,7 @@ #pragma once +#include "paddle/pten/backends/all_context.h" #include "paddle/pten/common/scalar.h" #include "paddle/pten/common/scalar_array.h" #include "paddle/pten/core/dense_tensor.h" @@ -21,26 +22,10 @@ #include "paddle/pten/core/kernel_def.h" // See Note [ Why still include the fluid headers? ] -#include "paddle/fluid/platform/device_context.h" #include "paddle/fluid/platform/enforce.h" namespace pten { -// TODO(shixiaowei): replaced by new DeviceContext later -using CPUContext = paddle::platform::CPUDeviceContext; -#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) -using CUDAContext = paddle::platform::CUDADeviceContext; -#endif -#ifdef PADDLE_WITH_MKLDNN -using MKLDNNContext = paddle::platform::MKLDNNDeviceContext; -#endif -#ifdef PADDLE_WITH_ASCEND_CL -using NPUContext = paddle::platform::NPUDeviceContext; -#endif -#ifdef PADDLE_WITH_XPU -using XPUContext = paddle::platform::XPUDeviceContext; -#endif - #define PT_KERNEL(...) \ ::pten::KernelImpl::Compute