From 4fbcf6f4c52adccbc6ea0786b302485f14e5a951 Mon Sep 17 00:00:00 2001 From: zyfncg Date: Tue, 1 Mar 2022 11:51:22 +0800 Subject: [PATCH] [PHI] Remove reseting dtype, layout and allocation by arg_def for outputs in executor (#39781) * remove SetAllocationForOutputTenosr * add place param for copy kernel * recover SetAllocationForOutputTenosr * polish code * fix empty_dev api bug * remove reseting dtype and layout for output in executor * fix merge bug * [Phi] Add ClearHolder when re-alloc on new place in DeviceContext * fix hostAlloc * remove setting output allocation * remove full_kernel_impl.h * fix bug of xpu full_like Co-authored-by: Aurelius84 --- paddle/fluid/framework/operator.cc | 6 ------ paddle/fluid/framework/phi_utils.cc | 21 --------------------- paddle/fluid/framework/phi_utils.h | 3 --- paddle/fluid/imperative/prepared_operator.h | 6 ------ paddle/phi/api/lib/utils/tensor_utils.cc | 21 --------------------- paddle/phi/api/lib/utils/tensor_utils.h | 3 --- paddle/phi/core/CMakeLists.txt | 2 +- paddle/phi/core/dense_tensor_impl.cc | 5 ----- paddle/phi/kernels/xpu/full_kernel.cc | 3 ++- 9 files changed, 3 insertions(+), 67 deletions(-) diff --git a/paddle/fluid/framework/operator.cc b/paddle/fluid/framework/operator.cc index 36208c41ed..b12ad552ab 100644 --- a/paddle/fluid/framework/operator.cc +++ b/paddle/fluid/framework/operator.cc @@ -2108,12 +2108,6 @@ void OperatorWithKernel::BuildPhiKernelContext( "Unsupported output `%s` type when call pt kernel.", framework::ToTypeName(var->Type()))); } - - experimental::ResetTensorDtypeAndLayoutByArgDef(tensor_out, - output_defs.at(i)); - SetAllocationForOutputTenosr( - tensor_out, phi::TransToPhiPlace(output_defs.at(i).backend)); - pt_kernel_context->EmplaceBackOutputWithoutSetRange(tensor_out); } diff --git a/paddle/fluid/framework/phi_utils.cc b/paddle/fluid/framework/phi_utils.cc index 1a39a87fb9..93bc2c02d5 100644 --- a/paddle/fluid/framework/phi_utils.cc +++ b/paddle/fluid/framework/phi_utils.cc @@ -233,26 +233,5 @@ static void SetAllocationForUninitializedDenseTensor( dense_tensor->ResetHolder(shared_allocation); } -void SetAllocationForOutputTenosr(phi::TensorBase* tensor, - const platform::Place& place) { - if (phi::DenseTensor::classof(tensor)) { - auto* dense_tensor = static_cast(tensor); - if (!dense_tensor->IsInitialized() || !(dense_tensor->place() == place)) { - SetAllocationForUninitializedDenseTensor(dense_tensor, place); - } - } else if (phi::SelectedRows::classof(tensor)) { - auto* selected_rows = static_cast(tensor); - if (!selected_rows->value().IsInitialized() || - !(selected_rows->place() == place)) { - SetAllocationForUninitializedDenseTensor(selected_rows->mutable_value(), - place); - } - } else { - PADDLE_THROW(platform::errors::Unimplemented( - "Unsupported tensor type is received when setting allocation for " - "output tensor.")); - } -} - } // namespace framework } // namespace paddle diff --git a/paddle/fluid/framework/phi_utils.h b/paddle/fluid/framework/phi_utils.h index 1a1f79d827..a175788169 100644 --- a/paddle/fluid/framework/phi_utils.h +++ b/paddle/fluid/framework/phi_utils.h @@ -62,9 +62,6 @@ class KernelArgsNameMaker { void InitDefaultKernelSignatureMap(); -void SetAllocationForOutputTenosr(phi::TensorBase* tensor, - const platform::Place& place); - // TODO(Wilber): support others device context. template struct ConvertToPhiContext { diff --git a/paddle/fluid/imperative/prepared_operator.h b/paddle/fluid/imperative/prepared_operator.h index 8e1e2fbe9a..3b5762720e 100644 --- a/paddle/fluid/imperative/prepared_operator.h +++ b/paddle/fluid/imperative/prepared_operator.h @@ -323,12 +323,6 @@ void BuildDygraphPhiKernelContext( "Unsupported output `%s` type when call pt kernel.", framework::ToTypeName(var->Type()))); } - - experimental::ResetTensorDtypeAndLayoutByArgDef(tensor_out, - output_defs.at(i)); - framework::SetAllocationForOutputTenosr( - tensor_out, phi::TransToPhiPlace(output_defs.at(i).backend)); - kernel_ctx->EmplaceBackOutputWithoutSetRange(tensor_out); } kernel_ctx->AssignOutputRange(std::make_pair(start_idx, end_idx), i); diff --git a/paddle/phi/api/lib/utils/tensor_utils.cc b/paddle/phi/api/lib/utils/tensor_utils.cc index 31325e22af..1c9f7c3a86 100644 --- a/paddle/phi/api/lib/utils/tensor_utils.cc +++ b/paddle/phi/api/lib/utils/tensor_utils.cc @@ -136,26 +136,5 @@ phi::ScalarArray MakePhiScalarArrayFromVarList( return result; } -void ResetTensorDtypeAndLayoutByArgDef(phi::TensorBase* dst, - const phi::TensorArgDef& arg_def) { - VLOG(5) << "ResetTensor by TensorArgDef."; - if (phi::DenseTensor::classof(dst)) { - auto* dense_t = static_cast(dst); - auto* meta = phi::DenseTensorUtils::GetMutableMeta(dense_t); - meta->dtype = arg_def.dtype; - meta->layout = arg_def.layout; - } else if (phi::SelectedRows::classof(dst)) { - auto* selected_rows = static_cast(dst); - auto* meta = - phi::DenseTensorUtils::GetMutableMeta(selected_rows->mutable_value()); - meta->dtype = arg_def.dtype; - meta->layout = arg_def.layout; - } else { - PADDLE_THROW(phi::errors::Unimplemented( - "Unsupported tensor type is received when reseting tensor dtype and " - "layout by argument definition.")); - } -} - } // namespace experimental } // namespace paddle diff --git a/paddle/phi/api/lib/utils/tensor_utils.h b/paddle/phi/api/lib/utils/tensor_utils.h index 8b30d5421a..64df59c1a2 100644 --- a/paddle/phi/api/lib/utils/tensor_utils.h +++ b/paddle/phi/api/lib/utils/tensor_utils.h @@ -42,8 +42,5 @@ phi::ScalarArray MakePhiScalarArrayFromVar(const framework::Variable& variable); phi::ScalarArray MakePhiScalarArrayFromVarList( const std::vector& variable_list); -void ResetTensorDtypeAndLayoutByArgDef(phi::TensorBase* dst, - const phi::TensorArgDef& arg_def); - } // namespace experimental } // namespace paddle diff --git a/paddle/phi/core/CMakeLists.txt b/paddle/phi/core/CMakeLists.txt index f4f57a0acb..8ffacbb39b 100644 --- a/paddle/phi/core/CMakeLists.txt +++ b/paddle/phi/core/CMakeLists.txt @@ -22,8 +22,8 @@ cc_library(sparse_csr_tensor SRCS sparse_csr_tensor.cc DEPS dense_tensor tensor_ cc_library(meta_tensor SRCS meta_tensor.cc DEPS tensor_base tensor_meta dense_tensor) cc_library(infermeta_utils SRCS infermeta_utils.cc DEPS meta_tensor) -cc_library(phi_device_context SRCS device_context.cc DEPS dense_tensor selected_rows) cc_library(selected_rows SRCS selected_rows_impl.cc DEPS dense_tensor phi_enforce ddim memcpy) +cc_library(phi_device_context SRCS device_context.cc DEPS dense_tensor selected_rows) cc_library(phi_custom_kernel SRCS custom_kernel.cc DEPS kernel_factory convert_utils) diff --git a/paddle/phi/core/dense_tensor_impl.cc b/paddle/phi/core/dense_tensor_impl.cc index 29e7dc01f3..5ee8308958 100644 --- a/paddle/phi/core/dense_tensor_impl.cc +++ b/paddle/phi/core/dense_tensor_impl.cc @@ -73,11 +73,6 @@ void DenseTensor::set_layout(const paddle::framework::DataLayout layout) { // Note: When you reset holder, you need to ensure the offset is correct void DenseTensor::ResetHolder(const std::shared_ptr& holder) { if (holder_) { - // TODO(zyfncg): The change of static_cast<> in check will recover back - // when SetAllocationForOutputTenosr is deleted. - // Now the numel() may return -1, and will cast to a very large number when - // compare with a data with unsigned long type, this will make checking - // failed, so it's a temporary solution to deal with this problem. PADDLE_ENFORCE_LE( numel() * static_cast(SizeOf(dtype())) + static_cast(meta_.offset), diff --git a/paddle/phi/kernels/xpu/full_kernel.cc b/paddle/phi/kernels/xpu/full_kernel.cc index 574f4e991a..d43126d56e 100644 --- a/paddle/phi/kernels/xpu/full_kernel.cc +++ b/paddle/phi/kernels/xpu/full_kernel.cc @@ -59,7 +59,7 @@ void FullKernel(const Context& dev_ctx, const Scalar& val, DataType dtype, DenseTensor* out) { - out->ResizeAndAllocate(phi::make_ddim(shape.GetData())); + out->Resize(phi::make_ddim(shape.GetData())); FullValueXPU(dev_ctx, out, val.to()); } @@ -69,6 +69,7 @@ void FullLikeKernel(const Context& dev_ctx, const Scalar& val, DataType dtype, DenseTensor* out) { + dev_ctx.template Alloc(out); auto value = val.to(); using XPUInTDType = typename XPUTypeTrait::Type; using CommonType = typename std::common_type< -- GitLab