未验证 提交 d763a91a 编写于 作者: C Chen Weihang 提交者: GitHub

[PTen] Remove pten core's dependency on fluid xxx_info.h (#39401)

* ermove xxx_info include

* fix namespace error

* resolve conflict

* skip xpu context in registry

* fix macro error

* resolve conflict

* resolve conflict

* revert xpu convert

* remove trans to fluid place

* remove useless headers
上级 2ea15fc9
......@@ -230,7 +230,7 @@ TEST(CustomKernel, custom_kernel_dot) {
pten::dtype::float16 fake_attr_f16 = pten::dtype::float16(5);
pten::DataType fake_attr_dtype = pten::DataType::UINT32;
paddle::framework::LoDTensor tmp_tensor;
tmp_tensor.mutable_data<uint8_t>({1}, pten::TransToFluidPlace(backend));
tmp_tensor.mutable_data<uint8_t>({1}, pten::TransToPtenPlace(backend));
pten::Scalar fake_attr_scalar =
paddle::experimental::MakePtenScalar(tmp_tensor);
pten::ScalarArray fake_attr_scalar_array;
......@@ -251,7 +251,7 @@ TEST(CustomKernel, custom_kernel_dot) {
auto dense_out = std::make_shared<pten::DenseTensor>(
pten::make_intrusive<paddle::experimental::SharedStorage>(
pten::TransToFluidPlace(backend)),
pten::TransToPtenPlace(backend)),
pten::DenseTensorMeta());
pten::MetaTensor meta_out(dense_out.get());
......
......@@ -605,7 +605,7 @@ bool OpSupportGPU(const std::string& op_type) {
kernel_factory.SelectKernelMap(pten::TransToPtenKernelName(op_type));
for (auto& kernel : kernel_key_map) {
if (platform::is_gpu_place(
pten::TransToFluidPlace(kernel.first.backend()))) {
pten::TransToPtenPlace(kernel.first.backend()))) {
return true;
}
}
......@@ -1949,7 +1949,7 @@ Scope* OperatorWithKernel::PreparePtenData(
continue;
}
auto expected_place = pten::TransToFluidPlace(in_def.backend);
auto expected_place = pten::TransToPtenPlace(in_def.backend);
if (platform::is_same_place(tensor_in->place(), expected_place)) {
continue;
}
......@@ -2079,7 +2079,7 @@ void OperatorWithKernel::BuildPtenKernelContext(
experimental::ResetTensorDtypeAndLayoutByArgDef(tensor_out,
output_defs.at(i));
SetAllocationForOutputTenosr(
tensor_out, pten::TransToFluidPlace(output_defs.at(i).backend));
tensor_out, pten::TransToPtenPlace(output_defs.at(i).backend));
pt_kernel_context->EmplaceBackOutputWithoutSetRange(tensor_out);
}
......
......@@ -15,15 +15,15 @@ limitations under the License. */
#include <sstream>
#include "paddle/fluid/framework/pten_utils.h"
#include "paddle/pten/core/compat/convert_utils.h"
#include "paddle/pten/core/compat/op_utils.h"
#include "paddle/pten/core/kernel_factory.h"
#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/framework/op_info.h"
#include "paddle/fluid/framework/selected_rows_utils.h"
#include "paddle/fluid/framework/variable.h"
#include "paddle/fluid/string/string_helper.h"
#include "paddle/pten/core/compat/convert_utils.h"
#include "paddle/pten/core/compat/op_utils.h"
#include "paddle/pten/core/kernel_factory.h"
namespace paddle {
namespace framework {
......@@ -61,7 +61,7 @@ OpKernelType TransPtenKernelKeyToOpKernelType(
proto::VarType::Type data_type =
pten::TransToProtoVarType(kernel_key.dtype());
// no need to set current device id here
platform::Place place = pten::TransToFluidPlace(kernel_key.backend(), false);
platform::Place place = pten::TransToPtenPlace(kernel_key.backend(), false);
DataLayout data_layout = kernel_key.layout();
LibraryType library_type = LibraryType::kPlain;
if (kernel_key.backend() == pten::Backend::MKLDNN) {
......
......@@ -27,6 +27,7 @@ limitations under the License. */
#include "paddle/fluid/framework/operator.h"
#include "paddle/pten/api/lib/utils/tensor_utils.h"
#include "paddle/pten/common/backend.h"
#include "paddle/pten/core/compat/arg_map_context.h"
#include "paddle/pten/core/kernel_factory.h"
#include "paddle/utils/flat_hash_map.h"
......
......@@ -326,7 +326,7 @@ void BuildDygraphPtenKernelContext(
experimental::ResetTensorDtypeAndLayoutByArgDef(tensor_out,
output_defs.at(i));
framework::SetAllocationForOutputTenosr(
tensor_out, pten::TransToFluidPlace(output_defs.at(i).backend));
tensor_out, pten::TransToPtenPlace(output_defs.at(i).backend));
kernel_ctx->EmplaceBackOutputWithoutSetRange(tensor_out);
}
......@@ -467,7 +467,7 @@ void PreparePtenData(const pten::Kernel& pt_kernel,
auto var = ins_vector[offset];
const auto* tensor_in = GetTensorFromVar(var->Var());
if (tensor_in && tensor_in->IsInitialized()) {
auto expected_place = pten::TransToFluidPlace(in_def.backend);
auto expected_place = pten::TransToPtenPlace(in_def.backend);
if (platform::is_same_place(tensor_in->place(), expected_place)) {
continue;
}
......
......@@ -63,7 +63,7 @@ inline std::vector<pten::MetaTensor> MakeMetaTensor(
inline pten::DenseTensor* SetKernelOutput(Backend backend, Tensor* out) {
auto dense_tensor = std::make_shared<pten::DenseTensor>(
pten::make_intrusive<SharedStorage>(pten::TransToFluidPlace(backend)),
pten::make_intrusive<SharedStorage>(pten::TransToPtenPlace(backend)),
pten::DenseTensorMeta());
out->set_impl(dense_tensor);
return dense_tensor.get();
......@@ -75,7 +75,7 @@ inline std::vector<pten::DenseTensor*> SetKernelOutput(
std::vector<pten::DenseTensor*> results(out_size);
for (size_t i = 0; i < out_size; ++i) {
auto tensor_ptr = std::make_shared<pten::DenseTensor>(
pten::make_intrusive<SharedStorage>(pten::TransToFluidPlace(backend)),
pten::make_intrusive<SharedStorage>(pten::TransToPtenPlace(backend)),
pten::DenseTensorMeta());
results[i] = tensor_ptr.get();
out->emplace_back();
......
......@@ -38,7 +38,7 @@ inline bool NeedTransformPlace(const paddle::platform::Place& input,
const TransformFlag& transform_flag) {
bool ret = transform_flag.need_trans_backend() &&
target != Backend::ALL_BACKEND &&
!platform::is_same_place(input, pten::TransToFluidPlace(target));
!platform::is_same_place(input, pten::TransToPtenPlace(target));
return ret;
}
......@@ -168,10 +168,10 @@ pten::DenseTensor TransformData(const pten::DenseTensor& tensor,
out.place(), target_args_def.backend, transform_flag)) {
pten::DenseTensor result(
pten::make_intrusive<paddle::experimental::SharedStorage>(
pten::TransToFluidPlace(target_args_def.backend)),
pten::TransToPtenPlace(target_args_def.backend)),
{out.dtype(), out.dims(), out.layout()});
framework::TransDataDevice(
out, pten::TransToFluidPlace(target_args_def.backend), &result);
out, pten::TransToPtenPlace(target_args_def.backend), &result);
out = result;
}
return out;
......
......@@ -53,7 +53,7 @@ std::size_t CountLeadingZeros(uint64_t val) {
pten::DeviceContext* GetDeviceContextByBackend(pten::Backend backend) {
auto& pool = paddle::platform::DeviceContextPool::Instance();
return pool.Get(pten::TransToFluidPlace(backend));
return pool.Get(pten::TransToPtenPlace(backend));
}
DataType ParseDataType(DataType dtype) { return dtype; }
......
......@@ -60,11 +60,11 @@ PADDLE_API Tensor copy_to(const Tensor& x, Backend backend, bool blocking) {
// 4. Prepare outputs & InferMeta
auto dense_out = std::make_shared<pten::DenseTensor>(
pten::make_intrusive<paddle::experimental::SharedStorage>(
pten::TransToFluidPlace(backend)),
pten::TransToPtenPlace(backend)),
pten::DenseTensorMeta());
pten::MetaTensor meta_out(dense_out.get());
pten::UnchangedInferMeta(*dense_x, &meta_out);
dense_out->mutable_data(pten::TransToFluidPlace(backend));
dense_out->mutable_data(pten::TransToPtenPlace(backend));
kernel_context.EmplaceBackOutput(dense_out.get());
Tensor out;
out.set_impl(dense_out);
......
......@@ -86,11 +86,11 @@ PADDLE_API Tensor to_sparse_coo(const Tensor& x,
// create empty SparseCooTensor
pten::DenseTensor non_zero_indices(
pten::make_intrusive<paddle::experimental::SharedStorage>(
pten::TransToFluidPlace(backend)),
pten::TransToPtenPlace(backend)),
std::move(indices_meta));
pten::DenseTensor non_zero_elements(
pten::make_intrusive<paddle::experimental::SharedStorage>(
pten::TransToFluidPlace(backend)),
pten::TransToPtenPlace(backend)),
std::move(elements_meta));
auto coo = std::make_shared<pten::SparseCooTensor>(
non_zero_indices, non_zero_elements, x.dims());
......@@ -148,15 +148,15 @@ PADDLE_API Tensor to_sparse_csr(const Tensor& x, Backend backend) {
// create empty SparseCooTensor
pten::DenseTensor non_zero_crows(
pten::make_intrusive<paddle::experimental::SharedStorage>(
pten::TransToFluidPlace(backend)),
pten::TransToPtenPlace(backend)),
std::move(crows_meta));
pten::DenseTensor non_zero_cols(
pten::make_intrusive<paddle::experimental::SharedStorage>(
pten::TransToFluidPlace(backend)),
pten::TransToPtenPlace(backend)),
std::move(cols_meta));
pten::DenseTensor non_zero_elements(
pten::make_intrusive<paddle::experimental::SharedStorage>(
pten::TransToFluidPlace(backend)),
pten::TransToPtenPlace(backend)),
std::move(elements_meta));
auto csr = std::make_shared<pten::SparseCsrTensor>(
non_zero_crows, non_zero_cols, non_zero_elements, x.dims());
......@@ -211,7 +211,7 @@ PADDLE_API Tensor to_dense(const Tensor& x, Backend backend) {
// create empty SparseCooTensor
auto dense_out = std::make_shared<pten::DenseTensor>(
pten::make_intrusive<paddle::experimental::SharedStorage>(
pten::TransToFluidPlace(backend)),
pten::TransToPtenPlace(backend)),
std::move(dense_meta));
kernel_context.EmplaceBackOutput(dense_out.get());
......
......@@ -76,7 +76,7 @@ pten::Scalar MakePtenScalar(const paddle::framework::Tensor& src) {
}
pten::Scalar MakePtenScalarFromVar(const framework::Variable& variable) {
auto expected_place = pten::TransToFluidPlace(pten::Backend::CPU);
auto expected_place = pten::TransToPtenPlace(pten::Backend::CPU);
if (variable.IsType<framework::LoDTensor>()) {
const auto& tensor = variable.Get<framework::LoDTensor>();
if (!platform::is_same_place(tensor.place(), expected_place)) {
......@@ -110,7 +110,7 @@ pten::ScalarArray MakePtenScalarArray(const paddle::framework::Tensor& src) {
pten::ScalarArray MakePtenScalarArrayFromVar(
const framework::Variable& variable) {
auto expected_place = pten::TransToFluidPlace(pten::Backend::CPU);
auto expected_place = pten::TransToPtenPlace(pten::Backend::CPU);
if (variable.IsType<framework::LoDTensor>()) {
const auto& tensor = variable.Get<framework::LoDTensor>();
if (!platform::is_same_place(tensor.place(), expected_place)) {
......@@ -133,7 +133,7 @@ pten::ScalarArray MakePtenScalarArrayFromVarList(
if (variable_list.size() == 0) {
return pten::ScalarArray();
}
auto expected_place = pten::TransToFluidPlace(pten::Backend::CPU);
auto expected_place = pten::TransToPtenPlace(pten::Backend::CPU);
paddle::framework::proto::VarType::Type data_type;
auto* first_var = variable_list.front();
......
cc_library(arg_map_context SRCS arg_map_context.cc DEPS pten_enforce)
cc_library(op_utils SRCS op_utils.cc DEPS arg_map_context enforce)
if(WITH_GPU)
cc_library(convert_utils SRCS convert_utils.cc DEPS data_type place op_utils gpu_info)
cc_library(convert_utils SRCS convert_utils.cc DEPS data_type place op_utils pten_gpu_info)
elseif(WITH_ROCM)
cc_library(convert_utils SRCS convert_utils.cc DEPS data_type place op_utils gpu_info)
cc_library(convert_utils SRCS convert_utils.cc DEPS data_type place op_utils pten_gpu_info)
elseif(WITH_XPU)
cc_library(convert_utils SRCS convert_utils.cc DEPS data_type place op_utils xpu_info)
elseif(WITH_ASCEND_CL)
cc_library(convert_utils SRCS convert_utils.cc DEPS data_type place op_utils npu_info)
cc_library(convert_utils SRCS convert_utils.cc DEPS data_type place op_utils pten_xpu_info)
else()
cc_library(convert_utils SRCS convert_utils.cc DEPS data_type place op_utils)
endif()
......@@ -13,26 +13,14 @@ See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/pten/core/compat/convert_utils.h"
#include "paddle/pten/core/compat/op_utils.h"
// See Note [ Why still include the fluid headers? ]
#include "paddle/fluid/platform/device/gpu/gpu_info.h"
#include "paddle/fluid/platform/device/npu/npu_info.h"
#include "paddle/fluid/platform/device/xpu/xpu_info.h"
#include "paddle/pten/backends/gpu/gpu_info.h"
#include "paddle/pten/backends/xpu/xpu_info.h"
#include "paddle/pten/common/place.h"
#include "paddle/pten/core/compat/op_utils.h"
namespace pten {
// TODO(chenweihang): Add other place trans cases later
Backend TransToPtenBackend(const paddle::platform::Place& place) {
if (paddle::platform::is_cpu_place(place)) {
return Backend::CPU;
} else if (paddle::platform::is_gpu_place(place)) {
return Backend::GPU;
} else {
return Backend::UNDEFINED;
}
}
paddle::experimental::DataType TransToPtenDataType(
const paddle::framework::proto::VarType::Type& dtype) {
// Set the order of case branches according to the frequency with
......@@ -67,45 +55,6 @@ paddle::experimental::DataType TransToPtenDataType(
}
}
paddle::platform::Place TransToFluidPlace(const Backend& backend,
bool set_device_id) {
// NOTE(zhiqiu): GetCurrentDeviceId not always success, and device id is not
// always needed.
// So, add set_device_id parameter here.
switch (backend) {
case pten::Backend::CPU:
return paddle::platform::CPUPlace();
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
case pten::Backend::GPU:
return paddle::platform::CUDAPlace(
set_device_id ? paddle::platform::GetCurrentDeviceId() : 0);
#endif
#ifdef PADDLE_WITH_MKLDNN
case pten::Backend::MKLDNN:
return paddle::platform::CPUPlace();
#endif
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
case pten::Backend::CUDNN:
return paddle::platform::CUDAPlace(
set_device_id ? paddle::platform::GetCurrentDeviceId() : 0);
#endif
#if defined(PADDLE_WITH_XPU)
case pten::Backend::XPU:
return paddle::platform::XPUPlace(
set_device_id ? paddle::platform::GetXPUCurrentDeviceId() : 0);
#endif
#if defined(PADDLE_WITH_ASCEND_CL)
case pten::Backend::NPU:
return paddle::platform::NPUPlace(
set_device_id ? paddle::platform::GetCurrentNPUDeviceId() : 0);
#endif
default:
PADDLE_THROW(paddle::platform::errors::Unimplemented(
"Unsupported backend `%s` when casting it to paddle place type.",
backend));
}
}
paddle::framework::proto::VarType::Type TransToProtoVarType(
const paddle::experimental::DataType& dtype) {
// Set the order of case branches according to the frequency with
......@@ -136,13 +85,56 @@ paddle::framework::proto::VarType::Type TransToProtoVarType(
case DataType::BOOL:
return paddle::framework::proto::VarType::BOOL;
default:
PADDLE_THROW(paddle::platform::errors::Unimplemented(
PADDLE_THROW(pten::errors::Unimplemented(
"Unsupported data type `%s` when casting it into "
"paddle data type.",
dtype));
}
}
Backend TransToPtenBackend(const pten::Place& place) {
if (place.GetType() == pten::AllocationType::CPU) {
return Backend::CPU;
} else if (place.GetType() == pten::AllocationType::GPU) {
return Backend::GPU;
} else {
return Backend::UNDEFINED;
}
}
pten::Place TransToPtenPlace(const Backend& backend, bool set_device_id) {
// NOTE(zhiqiu): GetCurrentDeviceId not always success, and device id is not
// always needed.
// So, add set_device_id parameter here.
switch (backend) {
case pten::Backend::CPU:
return pten::CPUPlace();
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
case pten::Backend::GPU:
return pten::GPUPlace(
set_device_id ? pten::backends::gpu::GetCurrentDeviceId() : 0);
#endif
#ifdef PADDLE_WITH_MKLDNN
case pten::Backend::MKLDNN:
return pten::CPUPlace();
#endif
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
case pten::Backend::CUDNN:
return pten::GPUPlace(
set_device_id ? pten::backends::gpu::GetCurrentDeviceId() : 0);
#endif
#if defined(PADDLE_WITH_XPU)
case pten::Backend::XPU:
return pten::XPUPlace(
set_device_id ? pten::backends::xpu::GetXPUCurrentDeviceId() : 0);
#endif
default:
PADDLE_THROW(pten::errors::Unimplemented(
"Unsupported backend `%s` when casting it to paddle place type.",
backend));
}
}
size_t DataTypeSize(DataType dtype) {
switch (dtype) {
case DataType::UNDEFINED:
......@@ -160,15 +152,15 @@ size_t DataTypeSize(DataType dtype) {
case DataType::INT64:
return sizeof(int64_t);
case DataType::FLOAT16:
return sizeof(paddle::platform::float16);
return sizeof(pten::dtype::float16);
case DataType::FLOAT32:
return sizeof(float);
case DataType::FLOAT64:
return sizeof(double);
case DataType::COMPLEX64:
return sizeof(paddle::platform::complex<float>);
return sizeof(pten::dtype::complex<float>);
case DataType::COMPLEX128:
return sizeof(paddle::platform::complex<double>);
return sizeof(pten::dtype::complex<double>);
default:
return 0;
}
......@@ -227,7 +219,7 @@ std::string DataType2String(DataType dtype) {
case DataType::COMPLEX128:
return "complex128";
default:
PADDLE_THROW(paddle::platform::errors::InvalidArgument(
PADDLE_THROW(pten::errors::InvalidArgument(
"Unknow pten::DataType, the int value = %d.",
static_cast<int>(dtype)));
return "";
......
......@@ -27,18 +27,18 @@ limitations under the License. */
namespace pten {
std::string TransToPtenKernelName(const std::string& fluid_op_name);
const std::string& TransToFluidOpName(const std::string& pten_kernel_name);
Backend TransToPtenBackend(const pten::Place& place);
// dtype convert function still relay on fluid proto
DataType TransToPtenDataType(
const paddle::framework::proto::VarType::Type& dtype);
paddle::platform::Place TransToFluidPlace(const Backend& backend,
bool set_device_id = true);
paddle::framework::proto::VarType::Type TransToProtoVarType(
const DataType& dtype);
std::string TransToPtenKernelName(const std::string& fluid_op_name);
const std::string& TransToFluidOpName(const std::string& pten_kernel_name);
Backend TransToPtenBackend(const pten::Place& place);
pten::Place TransToPtenPlace(const Backend& backend, bool set_device_id = true);
size_t DataTypeSize(DataType dtype);
DataType String2DataType(const std::string& str);
std::string DataType2String(DataType dtype);
......
......@@ -59,6 +59,9 @@ struct KernelArgsParseFunctor<Return_ (*)(Args_...)> {
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
||
arg_type == std::type_index(typeid(const GPUContext&))) {
#elif defined(PADDLE_WITH_XPU)
||
arg_type == std::type_index(typeid(const XPUContext&))) {
#else
) {
#endif
......
......@@ -71,7 +71,7 @@ PADDLE_API Tensor scale_kernel_context(const Tensor& x,
auto dense_out = std::make_shared<pten::DenseTensor>(
pten::make_intrusive<paddle::experimental::SharedStorage>(
pten::TransToFluidPlace(kernel_backend)),
pten::TransToPtenPlace(kernel_backend)),
pten::DenseTensorMeta());
pten::MetaTensor meta_out(dense_out.get());
pten::UnchangedInferMeta(*dense_x, &meta_out);
......@@ -238,7 +238,7 @@ Tensor scale_switch_case(const Tensor& x,
auto dense_out = std::make_shared<pten::DenseTensor>(
pten::make_intrusive<paddle::experimental::SharedStorage>(
pten::TransToFluidPlace(kernel_backend)),
pten::TransToPtenPlace(kernel_backend)),
pten::DenseTensorMeta());
pten::MetaTensor meta_out(dense_out.get());
pten::UnchangedInferMeta(*dense_x, &meta_out);
......
......@@ -84,7 +84,7 @@ TEST(Tensor, data_transform_diff_place) {
ASSERT_EQ(out.layout(), pten::DataLayout::NCHW);
ASSERT_EQ(out.initialized(), true);
ASSERT_EQ(out.impl()->place(),
pten::TransToFluidPlace(experimental::Backend::GPU));
pten::TransToPtenPlace(experimental::Backend::GPU));
auto ref_out = experimental::copy_to(out, experimental::Backend::CPU, true);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册