diff --git a/paddle/fluid/distributed/collective/Common.cc b/paddle/fluid/distributed/collective/Common.cc index 02eab58478ccce3664bcbc3ab99173ceb3f01f66..75eab0896c4f2b1420b252175da15e60cad1a874 100644 --- a/paddle/fluid/distributed/collective/Common.cc +++ b/paddle/fluid/distributed/collective/Common.cc @@ -21,7 +21,7 @@ std::vector GetPlaceList(const std::vector& tensors) { std::vector places; places.reserve(tensors.size()); for (auto& tensor : tensors) { - places.push_back(tensor.inner_place()); + places.push_back(tensor.place()); } return places; } @@ -41,13 +41,14 @@ std::string GetKeyFromPlaces(const std::vector& places) { } static bool CheckTensorsInPlace(const std::vector& tensors, - const PlaceType type) { - return std::all_of(tensors.cbegin(), tensors.cend(), - [&](const Tensor& t) { return t.place() == type; }); + phi::AllocationType type) { + return std::all_of(tensors.cbegin(), tensors.cend(), [&](const Tensor& t) { + return t.place().GetType() == type; + }); } bool CheckTensorsInCudaPlace(const std::vector& tensors) { - return CheckTensorsInPlace(tensors, PlaceType::kGPU); + return CheckTensorsInPlace(tensors, phi::AllocationType::GPU); } } // namespace distributed diff --git a/paddle/fluid/distributed/collective/ProcessGroupNCCL.cc b/paddle/fluid/distributed/collective/ProcessGroupNCCL.cc index eeb5e3b397c10e268607113e10ad857b6de9e444..88f8279a41793fa307d4b13da7d1a880020d74e3 100644 --- a/paddle/fluid/distributed/collective/ProcessGroupNCCL.cc +++ b/paddle/fluid/distributed/collective/ProcessGroupNCCL.cc @@ -404,7 +404,7 @@ void CheckTensorsInDifferentDevices(const std::vector& tensors, platform::errors::InvalidArgument( "Tensors must be CUDA and dense tensor.")); - const auto inserted = used_devices.insert(t.inner_place()).second; + const auto inserted = used_devices.insert(t.place()).second; PADDLE_ENFORCE_EQ(inserted, true, platform::errors::InvalidArgument( "Tensors must be on distinct GPU devices.")); diff --git a/paddle/fluid/distributed/collective/reducer.cc b/paddle/fluid/distributed/collective/reducer.cc index 71741515c90d563c5ba8c6ee43b674fa39bb9c40..0588bb085ea174f115b52795bf0283bc4fcf4522 100644 --- a/paddle/fluid/distributed/collective/reducer.cc +++ b/paddle/fluid/distributed/collective/reducer.cc @@ -398,7 +398,7 @@ void EagerReducer::InitializeDenseGroups( "GRAD is SelectedRows", tensor_name)); - PADDLE_ENFORCE_EQ(tensor.is_initialized(), true, + PADDLE_ENFORCE_EQ(tensor.initialized(), true, platform::errors::PreconditionNotMet( "Tensor %s is not initialized.", tensor_name)); const auto size = tensor.numel(); @@ -414,20 +414,13 @@ void EagerReducer::InitializeDenseGroups( p_group->dense_tensors_.push_back(phi::DenseTensor()); const auto &dtype = tensor.dtype(); - const auto &place = tensor.place(); const auto &inner_place = tensor.impl()->place(); if (index > 0) { PADDLE_ENFORCE_EQ(dtype, p_group->dtype_, platform::errors::PreconditionNotMet( "Tensor %s has unexpected dtype.", tensor_name)); - PADDLE_ENFORCE_EQ(place, place_, - platform::errors::PreconditionNotMet( - "Tensor %s has different place. Expected place is " - "%s, but actual place is %s", - tensor_name, inner_place_, inner_place)); } else { p_group->dtype_ = dtype; - place_ = place; inner_place_ = inner_place; } } @@ -717,7 +710,7 @@ void EagerReducer::MarkGroupReady(size_t group_index) { bool EagerReducer::HasGrad(size_t var_index) { auto grad = egr::EagerUtils::mutable_grad(tensors_[var_index]); - if (grad && grad->is_initialized()) { + if (grad && grad->initialized()) { return true; } else { return false; diff --git a/paddle/fluid/distributed/collective/reducer.h b/paddle/fluid/distributed/collective/reducer.h index 12c02509884e9e143d9c3d31dff4a127b3aa3acc..424bae0e5acd19fb39184e6b7cf8b2409e946add 100644 --- a/paddle/fluid/distributed/collective/reducer.h +++ b/paddle/fluid/distributed/collective/reducer.h @@ -26,7 +26,6 @@ #include "paddle/fluid/platform/device/gpu/gpu_info.h" #include "paddle/phi/api/include/api.h" #include "paddle/phi/api/include/tensor.h" -#include "paddle/phi/api/lib/ext_compat_utils.h" #include "paddle/phi/common/data_type.h" #include "paddle/phi/kernels/funcs/math_function.h" #include "paddle/utils/string/string_helper.h" @@ -121,7 +120,6 @@ class EagerReducer { std::vector groups_; std::vector variable_locators_; - PlaceType place_; platform::Place inner_place_; size_t next_group_ = 0; int64_t nranks_ = -1; diff --git a/paddle/fluid/eager/amp_auto_cast.h b/paddle/fluid/eager/amp_auto_cast.h index 6d5758adbe526144d155b8f7dbd6a3e1356ae4e2..3a96b23dcebbbfccd205a936cf0b4d787c5d0151 100644 --- a/paddle/fluid/eager/amp_auto_cast.h +++ b/paddle/fluid/eager/amp_auto_cast.h @@ -21,7 +21,7 @@ namespace egr { static inline bool NeedCast(const paddle::experimental::Tensor& tensor, const paddle::experimental::DataType& dst_dtype) { - auto place = tensor.inner_place(); + auto place = tensor.place(); auto data_type = tensor.dtype(); if (paddle::platform::is_gpu_place(place) || paddle::platform::is_cuda_pinned_place(place) || diff --git a/paddle/fluid/eager/eager_amp_auto_cast.h b/paddle/fluid/eager/eager_amp_auto_cast.h index 9bd1ca1f6fe53218bc15685bf4d7669eb6b11045..ee9da41881b2da0c59b45df2679de509991d7be1 100644 --- a/paddle/fluid/eager/eager_amp_auto_cast.h +++ b/paddle/fluid/eager/eager_amp_auto_cast.h @@ -20,7 +20,7 @@ namespace egr { static inline bool NeedCast(const paddle::experimental::Tensor& tensor, const paddle::experimental::DataType& dst_dtype) { - auto place = tensor.inner_place(); + auto place = tensor.place(); auto data_type = tensor.dtype(); if (paddle::platform::is_gpu_place(place) || paddle::platform::is_cuda_pinned_place(place) || diff --git a/paddle/fluid/eager/grad_node_info.cc b/paddle/fluid/eager/grad_node_info.cc index 23c7ea7c5e9b4c36a33a62fac26b3b49951f5eaf..9b1743b4860267e20858c8191aec37826e70299f 100644 --- a/paddle/fluid/eager/grad_node_info.cc +++ b/paddle/fluid/eager/grad_node_info.cc @@ -119,7 +119,7 @@ void GradNodeBase::SetGradInMeta(const paddle::experimental::Tensor& fwd_out, auto& meta = metas[0]; meta.SetStopGradient(fwd_out_meta->StopGradient()); - if (!fwd_out.is_initialized()) { + if (!fwd_out.initialized()) { VLOG(6) << "Skip Configuring GradSlotMeta for uninitialized GradInput Tensor"; return; @@ -145,7 +145,7 @@ void GradNodeBase::SetGradInMeta(const paddle::experimental::Tensor& fwd_out, "which is illegal.")); meta.SetTensorMeta(dense_tensor->meta()); - meta.SetPlace(fwd_out.inner_place()); + meta.SetPlace(fwd_out.place()); if (paddle::framework::IsComplexType( paddle::framework::TransToProtoVarType(dense_tensor->type()))) { @@ -186,7 +186,7 @@ void GradNodeBase::SetGradInMeta( meta.SetStopGradient(fwd_out_meta->StopGradient()); } - if (!fwd_out_tensor.is_initialized()) { + if (!fwd_out_tensor.initialized()) { VLOG(6) << "Skip Configuring GradSlotMeta for uninitialized GradInput Tensor"; return; @@ -204,7 +204,7 @@ void GradNodeBase::SetGradInMeta( "with phi::DataType::UNDEFINED," "which is illegal.")); meta.SetTensorMeta(dense_tensor->meta()); - meta.SetPlace(fwd_out_tensor.inner_place()); + meta.SetPlace(fwd_out_tensor.place()); if (paddle::framework::IsComplexType( paddle::framework::TransToProtoVarType(dense_tensor->type()))) { @@ -250,7 +250,7 @@ void GradNodeBase::SetGradOutMeta(const paddle::experimental::Tensor& fwd_in, "with phi::DataType::UNDEFINED," "which is illegal.")); meta.SetTensorMeta(dense_tensor->meta()); - meta.SetPlace(fwd_in.inner_place()); + meta.SetPlace(fwd_in.place()); } } else { VLOG(6) << "Unable to initialize the DenseTensorMeta of GradSlotMeta with " @@ -295,7 +295,7 @@ void GradNodeBase::SetGradOutMeta( "phi::DataType::UNDEFINED," "which is illegal.")); meta.SetTensorMeta(dense_tensor->meta()); - meta.SetPlace(fwd_in_tensor.inner_place()); + meta.SetPlace(fwd_in_tensor.place()); } } else { VLOG(6) << "Unable to initialize the DenseTensorMeta of GradSlotMeta " diff --git a/paddle/fluid/eager/grad_node_info.h b/paddle/fluid/eager/grad_node_info.h index 6a70a16a2416fac288374df07fa10886e502e31b..decb682bf45179e53b2b79699330a0f92f566b70 100644 --- a/paddle/fluid/eager/grad_node_info.h +++ b/paddle/fluid/eager/grad_node_info.h @@ -317,11 +317,11 @@ inline void CheckTensor(const paddle::experimental::Tensor& pre, paddle::framework::DataType2String(pre.dtype()), paddle::framework::DataType2String(post.dtype()))); PADDLE_ENFORCE_EQ( - pre.inner_place(), post.inner_place(), + pre.place(), post.place(), paddle::platform::errors::PermissionDenied( "The place of tensor before(%s) and after(%s) " "hook are not consistent", - pre.inner_place().DebugString(), post.inner_place().DebugString())); + pre.place().DebugString(), post.place().DebugString())); } } diff --git a/paddle/fluid/eager/grad_tensor_holder.cc b/paddle/fluid/eager/grad_tensor_holder.cc index 2dacb588ff847e63f8ccebc33faa74f63fef59b9..167717fb43166e300770db83b23fe24f1c74c7ad 100644 --- a/paddle/fluid/eager/grad_tensor_holder.cc +++ b/paddle/fluid/eager/grad_tensor_holder.cc @@ -53,7 +53,7 @@ void GradTensorHolder::CopyValueFromTensor( paddle::experimental::Tensor& buffer_tensor = buffer_[slot_id][rank]; if ((!buffer_tensor.defined() || !buffer_tensor.initialized())) { // Perform deep copy here - buffer_tensor.copy_(t, t.inner_place(), false); + buffer_tensor.copy_(t, t.place(), false); buffer_tensor.set_autograd_meta(t.mutable_autograd_meta()); } else { @@ -66,7 +66,7 @@ void GradTensorHolder::CopyValueFromTensor( if (t.defined()) { // Fill 1.0, use full to support complex, one_like don't support it. buffer_[slot_id][rank] = - paddle::experimental::full(t.shape(), 1, t.dtype(), t.inner_place()); + paddle::experimental::full(t.shape(), 1, t.dtype(), t.place()); } } } diff --git a/paddle/fluid/eager/pylayer/py_layer_node.h b/paddle/fluid/eager/pylayer/py_layer_node.h index f2e50494467c7a29b9b274d8b3ce35455e5b6b4f..87e8acf88a6948c3048eee2d7f07ab0aa05b1e0a 100644 --- a/paddle/fluid/eager/pylayer/py_layer_node.h +++ b/paddle/fluid/eager/pylayer/py_layer_node.h @@ -62,7 +62,7 @@ class GradNodePyLayer : public GradNodeBase { } else { forward_outputs_meta_[i].emplace_back(); } - forward_outputs_place_[i].emplace_back(tensor->inner_place()); + forward_outputs_place_[i].emplace_back(tensor->place()); } } } diff --git a/paddle/fluid/eager/tests/data_structure_tests/eager_tensor_test.cc b/paddle/fluid/eager/tests/data_structure_tests/eager_tensor_test.cc index de9758b73d2503e76b913d6a40d87dd26f641018..9afe3962faa29bd600c280d00dcf94d38b12a8e8 100644 --- a/paddle/fluid/eager/tests/data_structure_tests/eager_tensor_test.cc +++ b/paddle/fluid/eager/tests/data_structure_tests/eager_tensor_test.cc @@ -96,7 +96,7 @@ TEST(Tensor, MemberFunction) { CHECK_EQ(et3.dims(), expected_dim); CHECK_EQ(et3.type(), paddle::experimental::DataType::FLOAT32); CHECK_EQ(et3.layout(), paddle::experimental::DataLayout::NCHW); - CHECK(paddle::platform::is_cpu_place(et3.inner_place())); + CHECK(paddle::platform::is_cpu_place(et3.place())); VLOG(6) << "Get impl"; auto* dt3_ptr = std::dynamic_pointer_cast(et3.impl())->data(); diff --git a/paddle/fluid/eager/to_static/run_program_op_node.h b/paddle/fluid/eager/to_static/run_program_op_node.h index 46f48778a9656dd54eacf0cac08c22c4755f5cb5..9347a76fd48f00d6734ab4fa6368396d9a527695 100644 --- a/paddle/fluid/eager/to_static/run_program_op_node.h +++ b/paddle/fluid/eager/to_static/run_program_op_node.h @@ -114,7 +114,7 @@ static void ShareTensorsIntoScope(const std::vector &tensors, paddle::framework::Scope *scope) { for (size_t i = 0; i < tensors.size(); ++i) { auto name = tensors[i].name(); - if (name == "Fake_var" || !tensors[i].is_initialized()) { + if (name == "Fake_var" || !tensors[i].initialized()) { continue; } auto *var = scope->Var(name); diff --git a/paddle/fluid/eager/utils.cc b/paddle/fluid/eager/utils.cc index bcf4a4627bb769a4786adf6e9eb24b0be6655cd3..0847f691e822c0681dd503ac93f72d6f555da846 100644 --- a/paddle/fluid/eager/utils.cc +++ b/paddle/fluid/eager/utils.cc @@ -447,7 +447,7 @@ void EagerUtils::FillZeroForEmptyGradInputs( for (size_t i = 0; i < in_grads->size(); i++) { for (size_t j = 0; j < (*in_grads)[i].size(); j++) { paddle::experimental::Tensor& grad = (*in_grads)[i][j]; - if (!grad.is_initialized()) { + if (!grad.initialized()) { const GradSlotMeta& grad_in_meta = grad_in_metas[i][j]; PADDLE_ENFORCE( grad_in_meta.HasTensorMeta(), diff --git a/paddle/fluid/framework/custom_operator.cc b/paddle/fluid/framework/custom_operator.cc index df4879735bb82bfa5757854ab0ed3111f6cb45ee..fbcd920905c9d7bc2b2946d6df32dafc703594f3 100644 --- a/paddle/fluid/framework/custom_operator.cc +++ b/paddle/fluid/framework/custom_operator.cc @@ -36,7 +36,6 @@ limitations under the License. */ #include "paddle/fluid/platform/dynload/dynamic_loader.h" #include "paddle/fluid/string/string_helper.h" #include "paddle/phi/api/all.h" -#include "paddle/phi/api/lib/ext_compat_utils.h" #include "paddle/phi/api/lib/utils/tensor_utils.h" #include "paddle/phi/core/compat/convert_utils.h" #include "paddle/utils/any.h" @@ -627,8 +626,8 @@ class CustomGradOpMaker static void RegisterOperatorKernelWithPlace( const std::string& name, const OperatorWithKernel::OpKernelFunc& op_kernel_func, - const proto::VarType::Type type, const PlaceType& place) { - OpKernelType key(type, experimental::ConvertExtPlaceToInnerPlace(place)); + const proto::VarType::Type type, const platform::Place& place) { + OpKernelType key(type, place); VLOG(3) << "Custom Operator: op kernel key: " << key; OperatorWithKernel::AllOpKernels()[name][key] = op_kernel_func; } @@ -666,10 +665,10 @@ static void RegisterOperatorKernel(const std::string& name, op_kernel_func = func; } RegisterOperatorKernelWithPlace(name, op_kernel_func, proto::VarType::RAW, - PlaceType::kCPU); + platform::CPUPlace()); #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) RegisterOperatorKernelWithPlace(name, op_kernel_func, proto::VarType::RAW, - PlaceType::kGPU); + platform::CUDAPlace()); #endif } diff --git a/paddle/fluid/pybind/eager.cc b/paddle/fluid/pybind/eager.cc index 74d15b6c0ca7957d2bff7a46358323686efaa556..c529d121f39450e528bcab5fadb4436fb117fab5 100644 --- a/paddle/fluid/pybind/eager.cc +++ b/paddle/fluid/pybind/eager.cc @@ -137,7 +137,7 @@ void InitTensorWithTensor(TensorObject* self, const paddle::platform::Place& place, const std::string& name) { self->tensor.set_name(name); - if (place == src.inner_place()) { + if (place == src.place()) { auto impl = std::static_pointer_cast(src.impl()); self->tensor.set_impl(impl); VLOG(4) << "Same place, do ShareDataWith"; diff --git a/paddle/fluid/pybind/eager_functions.cc b/paddle/fluid/pybind/eager_functions.cc index fb115455357ddad7f164a6bfbb0f6c862d2dc472..1073cdc83a428bd0a4159b2202e0e21dd84dedd2 100644 --- a/paddle/fluid/pybind/eager_functions.cc +++ b/paddle/fluid/pybind/eager_functions.cc @@ -554,32 +554,32 @@ static PyObject* eager_api_async_read(PyObject* self, PyObject* args, src.is_gpu_pinned(), true, platform::errors::InvalidArgument("Required `src` device should be " "CUDAPinnedPlace, but received %d.", - src.inner_place())); + src.place())); PADDLE_ENFORCE_EQ( dst.is_gpu(), true, platform::errors::InvalidArgument( "Required `dst` device should be CUDAPlace, but received %d.", - dst.inner_place())); + dst.place())); PADDLE_ENFORCE_EQ( index.is_cpu(), true, platform::errors::InvalidArgument( "Required `index` device should be CPUPlace, but received %d.", - index.inner_place())); + index.place())); PADDLE_ENFORCE_EQ(buffer.is_gpu_pinned(), true, platform::errors::InvalidArgument( "Required `buffer` device should be CUDAPinnedPlace, " "but received %d.", - buffer.inner_place())); + buffer.place())); PADDLE_ENFORCE_EQ( offset.is_cpu(), true, platform::errors::InvalidArgument( "Required `offset` device should be CPUPlace, but received %d.", - offset.inner_place())); + offset.place())); PADDLE_ENFORCE_EQ( count.is_cpu(), true, platform::errors::InvalidArgument( "Required `count` device should be CPUPlace, but received %d.", - count.inner_place())); + count.place())); auto& src_tensor = src; auto* dst_tensor = &dst; @@ -701,22 +701,22 @@ static PyObject* eager_api_async_write(PyObject* self, PyObject* args, src.is_gpu(), true, platform::errors::InvalidArgument( "Required `src` device should be CUDAPlace, but received %d. ", - src.inner_place())); + src.place())); PADDLE_ENFORCE_EQ(dst.is_gpu_pinned(), true, platform::errors::InvalidArgument( "Required `dst` device should be CUDAPinnedPlace, " "but received %d. ", - dst.inner_place())); + dst.place())); PADDLE_ENFORCE_EQ( offset.is_cpu(), true, platform::errors::InvalidArgument("Required `offset` device should " "be CPUPlace, but received %d. ", - offset.inner_place())); + offset.place())); PADDLE_ENFORCE_EQ( count.is_cpu(), true, platform::errors::InvalidArgument( "Required `count` device should be CPUPlace, but received %d. ", - count.inner_place())); + count.place())); // TODO(daisiming): In future, add index as arguments following // async_read. diff --git a/paddle/fluid/pybind/eager_method.cc b/paddle/fluid/pybind/eager_method.cc index 021899c5f3782f9938355c229f98b07764f89cb1..dbeb9a77a14a3c53588aea810cf2c00d1597cef2 100644 --- a/paddle/fluid/pybind/eager_method.cc +++ b/paddle/fluid/pybind/eager_method.cc @@ -342,11 +342,11 @@ static PyObject* tensor_method_copy_(TensorObject* self, PyObject* args, ->SetPersistable( egr::EagerUtils::autograd_meta(&(src_tensor))->Persistable()); if (src_tensor.initialized()) { - self->tensor.copy_(src_tensor, src_tensor.inner_place(), blocking); + self->tensor.copy_(src_tensor, src_tensor.place(), blocking); } } else { if (src_tensor.initialized()) { - self->tensor.copy_(src_tensor, self->tensor.inner_place(), blocking); + self->tensor.copy_(src_tensor, self->tensor.place(), blocking); } } @@ -617,7 +617,7 @@ static PyObject* tensor__getitem_index_not_tensor(TensorObject* self, // if index is a list, list_select_flag will be true bool list_select_flag = false; PADDLE_ENFORCE_EQ( - self->tensor.is_initialized(), true, + self->tensor.initialized(), true, platform::errors::InvalidArgument( "tensor %s has not been initialized, we can only slice initialized " "tensor please init it first with numpy or other tensor.", @@ -921,7 +921,7 @@ static PyObject* tensor_method__setitem_eager_tensor(TensorObject* self, "please check the type of tensor.")); } - if (value_tensor_tmp.place() == paddle::PlaceType::kUNK) { + if (!value_tensor_tmp.initialized()) { #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) SetTensorFromPyArray( static_cast(value_tensor_tmp.impl().get()), @@ -934,7 +934,7 @@ static PyObject* tensor_method__setitem_eager_tensor(TensorObject* self, } else { SetTensorFromPyArray( static_cast(value_tensor_tmp.impl().get()), - value, value_tensor_tmp.inner_place(), false); + value, value_tensor_tmp.place(), false); } value_tensor = value_tensor_tmp; @@ -1009,7 +1009,7 @@ static PyObject* tensor_method__setitem_eager_tensor(TensorObject* self, VLOG(4) << "index is not tensor"; self_numpy[_index] = py::object(py::handle(value_obj), true); } - if (self->tensor.place() == paddle::PlaceType::kUNK) { + if (!self->tensor.initialized()) { #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) SetTensorFromPyArray(self_tensor, self_numpy, platform::Place(platform::CUDAPlace(0)), false); @@ -1018,7 +1018,7 @@ static PyObject* tensor_method__setitem_eager_tensor(TensorObject* self, platform::Place(platform::CPUPlace()), false); #endif } else { - SetTensorFromPyArray(self_tensor, self_numpy, self->tensor.inner_place(), + SetTensorFromPyArray(self_tensor, self_numpy, self->tensor.place(), false); } } @@ -1146,7 +1146,7 @@ static PyObject* tensor__copy_gradient_from(TensorObject* self, PyObject* args, PyObject* kwargs) { EAGER_TRY auto src = CastPyArg2Tensor(PyTuple_GET_ITEM(args, 0), 0); - if (self->tensor.is_initialized()) { + if (self->tensor.initialized()) { PADDLE_ENFORCE_EQ(self->tensor.dtype(), src.dtype(), platform::errors::PreconditionNotMet( "Tensor %s has different data type with Tensor %s", @@ -1367,7 +1367,7 @@ static PyObject* tensor_method__share_memory(TensorObject* self, PyObject* args, PyObject* kwargs) { EAGER_TRY #ifndef _WIN32 - PADDLE_ENFORCE_EQ(platform::is_cpu_place(self->tensor.inner_place()), true, + PADDLE_ENFORCE_EQ(platform::is_cpu_place(self->tensor.place()), true, platform::errors::InvalidArgument( "Sharing memory only support CPU Tensor currently")); // 1. get LoDTensor @@ -1419,7 +1419,7 @@ static PyObject* tensor_method__uva(TensorObject* self, PyObject* args, platform::errors::InvalidArgument( "Unified virtual addressing only support " "DenseTensor currently.")); - PADDLE_ENFORCE_EQ(platform::is_cpu_place(self->tensor.inner_place()), true, + PADDLE_ENFORCE_EQ(platform::is_cpu_place(self->tensor.place()), true, platform::errors::InvalidArgument( "Unified virtual addressing only support " "CPU Tensor currently.")); diff --git a/paddle/fluid/pybind/eager_properties.cc b/paddle/fluid/pybind/eager_properties.cc index a72ea6c4b02e1595987806a7850f17d5ad4d39f7..797b68fcb36ea9d2e0896eccb49e6afc95ebe2e4 100644 --- a/paddle/fluid/pybind/eager_properties.cc +++ b/paddle/fluid/pybind/eager_properties.cc @@ -108,7 +108,7 @@ int tensor_properties_set_grad(TensorObject* self, PyObject* value, "Detected NULL grad" "Please check if you have manually cleared" "the grad inside autograd_meta")); - grad->copy_(src, self->tensor.inner_place(), true); + grad->copy_(src, self->tensor.place(), true); return 0; EAGER_CATCH_AND_THROW_RETURN_NEG } @@ -160,14 +160,14 @@ PyObject* tensor_properties_get_shape(TensorObject* self, void* closure) { PyObject* tensor_properties_get_place(TensorObject* self, void* closure) { EAGER_TRY - return ToPyObject(self->tensor.inner_place()); + return ToPyObject(self->tensor.place()); EAGER_CATCH_AND_THROW_RETURN_NULL } PyObject* tensor_properties_get_place_str(TensorObject* self, void* closure) { EAGER_TRY std::stringstream ostr; - ostr << self->tensor.inner_place(); + ostr << self->tensor.place(); return ToPyObject(ostr.str()); EAGER_CATCH_AND_THROW_RETURN_NULL } diff --git a/paddle/phi/api/all.h b/paddle/phi/api/all.h index ac8607597a4368331dab7e7a73f06be0e70c84c4..5838e7b2eaab7107db9897eabaf824f9b5da0994 100644 --- a/paddle/phi/api/all.h +++ b/paddle/phi/api/all.h @@ -41,5 +41,4 @@ limitations under the License. */ #include "paddle/phi/api/ext/dispatch.h" #include "paddle/phi/api/ext/exception.h" #include "paddle/phi/api/ext/op_meta_info.h" -#include "paddle/phi/api/ext/place.h" #include "paddle/phi/api/ext/tensor_compat.h" diff --git a/paddle/phi/api/ext/place.h b/paddle/phi/api/ext/place.h deleted file mode 100644 index 91d4f41c2135145062fb98b2df77b14ec4e1a32b..0000000000000000000000000000000000000000 --- a/paddle/phi/api/ext/place.h +++ /dev/null @@ -1,22 +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 - -namespace paddle { - -// TODO(yangjiabin): Add other place support in next PR -enum class PlaceType { kUNK = -1, kCPU, kGPU }; - -} // namespace paddle diff --git a/paddle/phi/api/ext/tensor_compat.h b/paddle/phi/api/ext/tensor_compat.h index 530275de50ec7e566598f7e65f5816bea52f77f4..e63390db06e82a938491d50bfa812f66498023e1 100644 --- a/paddle/phi/api/ext/tensor_compat.h +++ b/paddle/phi/api/ext/tensor_compat.h @@ -14,6 +14,7 @@ limitations under the License. */ #pragma once +#include "paddle/phi/api/include/api.h" #include "paddle/phi/api/include/tensor.h" // Note(chenweihang): In order to be compatible with the original custom @@ -21,5 +22,8 @@ limitations under the License. */ // cannot be includeed in paddle namespace paddle { -using Tensor = paddle::experimental::Tensor; +using Tensor = experimental::Tensor; +// using several Tensor initialize functions in paddle namespace +using experimental::empty; +using experimental::full; } // namespace paddle diff --git a/paddle/phi/api/include/tensor.h b/paddle/phi/api/include/tensor.h index ad3933e2b2b53ffd08b0d4560dd321783b6b83b9..3c3da4b749ed0787ec78f14df0f1e73e499988af 100644 --- a/paddle/phi/api/include/tensor.h +++ b/paddle/phi/api/include/tensor.h @@ -29,7 +29,6 @@ using gpuStream_t = cudaStream_t; using gpuStream_t = hipStream_t; #endif -#include "paddle/phi/api/ext/place.h" #include "paddle/phi/api/include/dll_decl.h" #include "paddle/phi/common/data_type.h" #include "paddle/phi/common/layout.h" @@ -109,21 +108,23 @@ class PADDLE_API Tensor final { /** * @brief Construct a new Tensor object on the target place. - * This is a deprecated method and may be removed in the future! + * + * This is a deprecated method and may be removed in the future!!! * * @param place */ - explicit Tensor(const PlaceType& place); + explicit Tensor(const Place& place); /** * @brief Construct a new Tensor object on the target place * with specified shape. - * This is a deprecated method and may be removed in the future! + * + * This is a deprecated method and may be removed in the future!!! * * @param place * @param shape */ - Tensor(const PlaceType& place, const std::vector& shape); + Tensor(const Place& place, const std::vector& shape); /** * @brief Construct a new Tensor object by a TensorBase pointer and name @@ -135,8 +136,9 @@ class PADDLE_API Tensor final { /** * @brief Construct a new Tensor object with name * - * @note Used to adapt original execution mechanism and debug analysis - * in the development of new dygraph. It may be removed in the future. + * @note Internal method, used to adapt original execution mechanism and + * debug analysis in the development of new dygraph. It may be removed in + * the future. * */ explicit Tensor(const std::string& name) : name_(name) {} @@ -151,6 +153,7 @@ class PADDLE_API Tensor final { /** * @brief Get the size of current tensor. + * * The compatible method of `Tensor::numel()`. * This is a deprecated method and may be removed in the future! * @@ -167,6 +170,7 @@ class PADDLE_API Tensor final { /** * @brief Return the shape (dimensions) of Tensor. + * * The compatible method of `Tensor::dims()`. * This is a deprecated method and may be removed in the future! * @@ -178,7 +182,7 @@ class PADDLE_API Tensor final { * @brief Reset the shape of the tensor. * @note: This method means Reset the shape of the tensor, * and must be called before calling mutable_data() or - * copy_to(const PlaceType& place), this is not a standard definition of + * copy_to(const Place& place), this is not a standard definition of * reshape behavior, so we will deprecated this feature in the future. * * @param shape @@ -194,6 +198,7 @@ class PADDLE_API Tensor final { /** * @brief Return the data type of Tensor. + * * The compatible method of `Tensor::dtype()`. * This is a deprecated method and may be removed in the future! * @@ -244,20 +249,10 @@ class PADDLE_API Tensor final { /** * @brief Return the place (device) of Tensor. - * This is a deprecated method and may be removed in the future! * - * @return PlaceType + * @return Place */ - PlaceType place() const; - - /** - * @brief Return the place (device) of Tensor. - * Because the `place` method already exists, so we need to use a new name, - * here we temporarily use `inner_place`. - * - * @return paddle::platform::Place - */ - phi::Place inner_place() const; + Place place() const; /** * @brief Determine whether the tensor device is CPU @@ -287,7 +282,7 @@ class PADDLE_API Tensor final { /** * @brief Get the memory pointer in CPU or GPU with specific data type. - * It's usually used to get the output data pointer. + * It's usually used to get the output data pointer, same as the T* data(). * * @tparam T * @return T* @@ -297,6 +292,7 @@ class PADDLE_API Tensor final { /** * @brief Get the memory pointer in CPU or GPU with specific data type. + * * It's usually used to get the output data pointer. * This is a deprecated method and may be removed in the future! * @@ -305,7 +301,7 @@ class PADDLE_API Tensor final { * @return T* */ template - T* mutable_data(const PlaceType& place); + T* mutable_data(const Place& place); /** * @brief Get the const memory pointer directly. @@ -319,8 +315,7 @@ class PADDLE_API Tensor final { /** * @brief Get the memory pointer directly. - * It's usually used to get the output data pointer. - * This is a deprecated method and may be removed in the future! + * It's usually used to get the mutable output data pointer. * * @tparam T * @return T* @@ -409,7 +404,7 @@ class PADDLE_API Tensor final { * @return Tensor */ template - Tensor copy_to(const PlaceType& target_place) const; + Tensor copy_to(const Place& target_place) const; /** * @brief Transfer the current Tensor to the specified device and return. @@ -427,7 +422,8 @@ class PADDLE_API Tensor final { * @param blocking, Should we copy this in sync way. * @return void */ - void copy_(const Tensor& src, const phi::Place& target_place, bool blocking); + void copy_(const Tensor& src, const Place& target_place, bool blocking); + /** * @brief Cast datatype from one to another * @@ -489,11 +485,17 @@ class PADDLE_API Tensor final { /* Part 8: Autograd methods */ /** - * @brief Get the autograd meta object + * @brief Get the autograd meta object pointer * * @return AbstractAutogradMeta* */ AbstractAutogradMeta* get_autograd_meta() const; + + /** + * @brief Get the shared pointer of autograd meta object + * + * @return std::shared_ptr& + */ const std::shared_ptr& mutable_autograd_meta() const; /** @@ -524,7 +526,7 @@ class PADDLE_API Tensor final { /* Part 10: Auto generated Tensor methods */ - /* Part 11: Methods of converting SparseTensor and DenseTensor to each other + /* Part 11: Methods of converting underlying TensorType to each other */ /** * @brief Convert DenseTensor or SparseCsrTensor to SparseCooTensor @@ -587,12 +589,6 @@ class PADDLE_API Tensor final { * in the development of new dygraph. It may be removed in the future. */ std::string name_{""}; - - /** - * Place type: Return the expected memory location if the Tensor is - * uninitialized. - */ - PlaceType place_{PlaceType::kUNK}; }; } // namespace experimental diff --git a/paddle/phi/api/lib/CMakeLists.txt b/paddle/phi/api/lib/CMakeLists.txt index 7dfe7d8cf4d209e98386b54d11465ed46dec3868..9cc5d620280bcdf2187b2e0e6c80f257f00f6466 100644 --- a/paddle/phi/api/lib/CMakeLists.txt +++ b/paddle/phi/api/lib/CMakeLists.txt @@ -1,13 +1,11 @@ add_subdirectory(utils) -cc_library(ext_compat_utils SRCS ext_compat_utils.cc DEPS place) - if (WITH_GPU) - nv_library(phi_tensor_raw SRCS tensor.cc DEPS tensor_base dense_tensor phi_api_utils ext_compat_utils phi_enforce) + nv_library(phi_tensor_raw SRCS tensor.cc DEPS tensor_base dense_tensor phi_api_utils phi_enforce) elseif (WITH_ROCM) - hip_library(phi_tensor_raw SRCS tensor.cc DEPS tensor_base dense_tensor phi_api_utils ext_compat_utils phi_enforce) + hip_library(phi_tensor_raw SRCS tensor.cc DEPS tensor_base dense_tensor phi_api_utils phi_enforce) else() - cc_library(phi_tensor_raw SRCS tensor.cc DEPS tensor_base dense_tensor phi_api_utils ext_compat_utils phi_enforce) + cc_library(phi_tensor_raw SRCS tensor.cc DEPS tensor_base dense_tensor phi_api_utils phi_enforce) endif() set(api_gen_base ${CMAKE_SOURCE_DIR}/python/paddle/utils/code_gen/api_base.py) diff --git a/paddle/phi/api/lib/ext_compat_utils.cc b/paddle/phi/api/lib/ext_compat_utils.cc deleted file mode 100644 index 1d0f52b5f0b65dac4b889248b5e99e16424203ef..0000000000000000000000000000000000000000 --- a/paddle/phi/api/lib/ext_compat_utils.cc +++ /dev/null @@ -1,70 +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/phi/api/lib/ext_compat_utils.h" -#include "paddle/fluid/platform/device/gpu/gpu_info.h" - -namespace paddle { -namespace experimental { - -platform::Place ConvertExtPlaceToInnerPlace(PlaceType p) { - if (p == PlaceType::kCPU) { - return platform::Place(platform::CPUPlace()); -#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) - } else if (p == PlaceType::kGPU) { - return platform::Place(platform::CUDAPlace(platform::GetCurrentDeviceId())); -#endif - } else { - PADDLE_THROW( - platform::errors::Unimplemented("Unsupported place type code(%d) when " - "casting enum place to paddle place.", - static_cast(p))); - } - return platform::Place(); -} - -PlaceType ConvertInnerPlaceToExtPlace(const platform::Place& p) { - if (platform::is_cpu_place(p)) { - return PlaceType::kCPU; - } else if (platform::is_gpu_place(p)) { -#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) - return PlaceType::kGPU; -#endif - } else { - PADDLE_THROW( - platform::errors::Unimplemented("Unsupported place type `%s` when " - "casting paddle place to enum place.", - p)); - } - return PlaceType::kUNK; -} - -Backend ConvertExtPlaceToBackend(PlaceType p) { - switch (p) { - case PlaceType::kCPU: - return Backend::CPU; -#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) - case PlaceType::kGPU: - return Backend::GPU; -#endif - default: - PADDLE_THROW( - platform::errors::Unimplemented("Unsupported place type `%s` when " - "casting enum place to backend.", - static_cast(p))); - } -} - -} // namespace experimental -} // namespace paddle diff --git a/paddle/phi/api/lib/ext_compat_utils.h b/paddle/phi/api/lib/ext_compat_utils.h deleted file mode 100644 index 89f6f15b70ff2057aa78b3e7abc5c3045e3e87f6..0000000000000000000000000000000000000000 --- a/paddle/phi/api/lib/ext_compat_utils.h +++ /dev/null @@ -1,31 +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/fluid/platform/place.h" -#include "paddle/phi/api/ext/place.h" -#include "paddle/phi/common/backend.h" - -namespace paddle { -namespace experimental { - -platform::Place ConvertExtPlaceToInnerPlace(PlaceType p); - -PlaceType ConvertInnerPlaceToExtPlace(const platform::Place& p); - -Backend ConvertExtPlaceToBackend(PlaceType p); - -} // namespace experimental -} // namespace paddle diff --git a/paddle/phi/api/lib/kernel_dispatch.cc b/paddle/phi/api/lib/kernel_dispatch.cc index 6d97dc7657f00616c8970e86b03f09f35eaa4a0f..a534f02663dffe3b8a9533d8cb76851be71f1f8c 100644 --- a/paddle/phi/api/lib/kernel_dispatch.cc +++ b/paddle/phi/api/lib/kernel_dispatch.cc @@ -126,7 +126,7 @@ Backend ParseBackend(const Place& place) { return phi::TransToPhiBackend(place); } Backend ParseBackend(const Tensor& tensor) { - return phi::TransToPhiBackend(tensor.inner_place()); + return phi::TransToPhiBackend(tensor.place()); } Backend ParseBackendWithInputOrder(const Place& place, const Tensor& tensor) { diff --git a/paddle/phi/api/lib/tensor.cc b/paddle/phi/api/lib/tensor.cc index ffc754feaed9822983811d0bc76d80490ca18554..1fb08033798943efc6de6c2bc49fe1466718cba2 100644 --- a/paddle/phi/api/lib/tensor.cc +++ b/paddle/phi/api/lib/tensor.cc @@ -19,46 +19,41 @@ limitations under the License. */ #include #include "glog/logging.h" -#include "paddle/phi/api/lib/ext_compat_utils.h" + #include "paddle/phi/api/lib/utils/allocator.h" -#include "paddle/phi/api/lib/utils/storage.h" -#include "paddle/phi/core/compat/convert_utils.h" +#include "paddle/phi/backends/gpu/gpu_info.h" +#include "paddle/phi/core/ddim.h" #include "paddle/phi/core/dense_tensor.h" +#include "paddle/phi/core/enforce.h" #include "paddle/phi/core/selected_rows.h" #include "paddle/phi/core/sparse_coo_tensor.h" #include "paddle/phi/core/sparse_csr_tensor.h" #include "paddle/phi/core/tensor_base.h" #include "paddle/phi/core/tensor_meta.h" #include "paddle/phi/core/tensor_utils.h" -/** - * [ Why still include the fluid headers? ] - * - * We hope to organize the basic implementation of Tensor and the logic related - * to Tensor computation into an independent library, which we call - * [Tensor Operation Library, phi], so we extract or rewrite the original - * Kernels. - * - * In the future, the training library, inference library and custom operators - * will link to this Tensor Operation library. - * - * However, if we directly split the link relation, we need to make too many - * changes, which will affect the stability of the framework, so here we still - * rely on the implementation of the framework, which is a intermediate state. - * - * In the future, the necessary components will be moved to the this library, - * or the corresponding components will be re-implemented. - */ - -#include "paddle/fluid/memory/memory.h" -#include "paddle/fluid/platform/place.h" + #include "paddle/fluid/platform/stream/cuda_stream.h" -#include "paddle/phi/common/complex.h" -#include "paddle/phi/common/float16.h" -#include "paddle/phi/core/ddim.h" -#include "paddle/phi/core/enforce.h" namespace paddle { namespace experimental { +namespace detail { +static Place GetCorrectPlaceByPlaceType(const Place &place_type) { + auto alloc_type = place_type.GetType(); + switch (alloc_type) { + case AllocationType::CPU: + return place_type; +#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) + case AllocationType::GPU: + return phi::Place(AllocationType::GPU, + phi::backends::gpu::GetCurrentDeviceId()); +#endif + default: + PADDLE_THROW(phi::errors::Unavailable( + "The PlaceType is a legacy design, only supports CPU and GPU, " + "and will not support other place types in the future.")); + } +} +} // namespace detail /////// Tensor Methods //////// @@ -71,27 +66,43 @@ Tensor::Tensor(std::shared_ptr tensor_impl) phi::errors::InvalidArgument("TensorImpl with nullptr is not supported")); } -Tensor::Tensor(const PlaceType &place) - : impl_(std::move(std::make_shared( - std::move(phi::make_intrusive( - ConvertExtPlaceToInnerPlace(place))), - std::move(phi::DenseTensorMeta(phi::DataType::UNDEFINED, - phi::make_ddim({}), - phi::DataLayout::NCHW))))), - place_{place} {} - -Tensor::Tensor(const PlaceType &place, const std::vector &shape) - : impl_(std::move(std::make_shared( - std::move(phi::make_intrusive( - ConvertExtPlaceToInnerPlace(place))), - std::move(phi::DenseTensorMeta(phi::DataType::UNDEFINED, - phi::make_ddim(shape), - phi::DataLayout::NCHW))))), - place_{place} {} +Tensor::Tensor(const Place &place) { + LOG_FIRST_N(WARNING, 1) + << "The Tensor(place) constructor is deprecated since version " + "2.3, and will be removed in version 2.4! Please use " + "`paddle::empty/full` method to create a new " + "Tensor instead. " + "Reason: A legal tensor cannot be constructed only based on " + "the `place`, and datatype, shape, layout, etc. is also " + "required."; + DefaultAllocator alloc(detail::GetCorrectPlaceByPlaceType(place)); + impl_ = std::move(std::make_shared( + &alloc, + std::move(phi::DenseTensorMeta( + phi::DataType::FLOAT32, phi::make_ddim({}), phi::DataLayout::NCHW)))); +} + +Tensor::Tensor(const Place &place, const std::vector &shape) { + LOG_FIRST_N(WARNING, 1) + << "The Tensor(place, shape) constructor is deprecated since " + "version 2.3, and will be removed in version 2.4! Please use " + "`paddle::empty/full` method to create a new " + "Tensor instead. " + "Reason: A legal tensor cannot be constructed only based on " + "the `place` and `shape`, and datatype, layout, etc. is also " + "required."; + DefaultAllocator alloc(detail::GetCorrectPlaceByPlaceType(place)); + impl_ = std::move(std::make_shared( + &alloc, + std::move(phi::DenseTensorMeta(phi::DataType::FLOAT32, + phi::make_ddim({shape}), + phi::DataLayout::NCHW)))); +} Tensor::Tensor(std::shared_ptr tensor_impl, const std::string &name) : impl_(std::move(tensor_impl)), name_(std::move(name)) {} + /* Part 2: Dimension, DataType and DataLayout methods */ int64_t Tensor::numel() const { return impl_->numel(); } @@ -109,17 +120,17 @@ std::vector Tensor::shape() const { } void Tensor::reshape(const std::vector &shape) { - LOG(WARNING) << "The function of resetting the shape of the uninitialized " - "Tensor of the `reshape` method is deprecated since version " - "2.3, and will be removed in version 2.4, please use " - "`paddle::experimental::full` method to create a new Tensor " - "instead. " - "reason: `reshape` means changing the tensor shape without " - "touching underlying data, this requires the total size of " - "the tensor to remain constant."; + LOG_FIRST_N(WARNING, 1) + << "The function of resetting the shape of the uninitialized " + "Tensor of the `reshape` method is deprecated since version " + "2.3, and will be removed in version 2.4, please use " + "`paddle::empty/full` method to create a new Tensor " + "instead. " + "reason: `reshape` means changing the tensor shape without " + "touching underlying data, this requires the total size of " + "the tensor to remain constant."; if (is_dense_tensor()) { - std::dynamic_pointer_cast(impl_)->Resize( - phi::make_ddim(shape)); + static_cast(impl_.get())->Resize(phi::make_ddim(shape)); } else { PADDLE_THROW(phi::errors::Unimplemented( "Only support reshape operation on DenseTensor now.")); @@ -146,42 +157,40 @@ bool Tensor::is_sparse_csr_tensor() const { } /* Part 3: Device and Backend methods */ -PlaceType Tensor::place() const { - if (!impl_->initialized()) { - return place_; - } else { - return ConvertInnerPlaceToExtPlace(impl_->place()); - } -} - -paddle::platform::Place Tensor::inner_place() const { +Place Tensor::place() const { PADDLE_ENFORCE_NOT_NULL( impl_, phi::errors::PermissionDenied( "Null pointer error, the impl_ of Tensor should not be " - "Null when calling Tensor::inner_place().")); + "Null when calling Tensor::place().")); return impl_->place(); } -bool Tensor::is_cpu() const { - return paddle::platform::is_cpu_place(inner_place()); -} +bool Tensor::is_cpu() const { return paddle::platform::is_cpu_place(place()); } -bool Tensor::is_gpu() const { - return paddle::platform::is_gpu_place(inner_place()); -} +bool Tensor::is_gpu() const { return paddle::platform::is_gpu_place(place()); } bool Tensor::is_gpu_pinned() const { - return paddle::platform::is_cuda_pinned_place(inner_place()); + return paddle::platform::is_cuda_pinned_place(place()); } /* Part 4: Data Access methods */ template T *Tensor::mutable_data() { + LOG_FIRST_N(WARNING, 1) + << "Allocating memory through `mutable_data` method is " + "deprecated since version 2.3, and `mutable_data` method " + "will be removed in version 2.4! Please use " + "`paddle::empty/full` method to create a new " + "Tensor with allocated memory, and use data() method " + "to get the memory pointer of tensor instead. " + "Reason: When calling `mutable_data` to allocate memory, " + "the place, datatype, and data layout of tensor may be in " + "an illegal state."; if (is_dense_tensor()) { - return std::dynamic_pointer_cast(impl_)->mutable_data( - ConvertExtPlaceToInnerPlace(place())); + return static_cast(impl_.get()) + ->mutable_data(place()); } return nullptr; } @@ -202,51 +211,44 @@ template PADDLE_API phi::dtype::float16 * Tensor::mutable_data(); template -T *Tensor::mutable_data(const PlaceType &place) { - auto inner_place = ConvertExtPlaceToInnerPlace(place); - if (impl_->initialized()) { - PADDLE_ENFORCE_EQ( - platform::is_same_place(inner_place, impl_->place()), - true, - phi::errors::Unimplemented("Modification of tensor place through " - "mutable_data is not supported now")); - } +T *Tensor::mutable_data(const Place &place) { + LOG_FIRST_N(WARNING, 1) + << "Allocating memory through `mutable_data` method is " + "deprecated since version 2.3, and `mutable_data` method " + "will be removed in version 2.4! Please use " + "`paddle::empty/full` method to create a new " + "Tensor with allocated memory, and use data() method " + "to get the memory pointer of tensor instead. " + "Reason: When calling `mutable_data` to allocate memory, " + "the datatype, and data layout of tensor may be in " + "an illegal state."; if (is_dense_tensor()) { - return std::dynamic_pointer_cast(impl_)->mutable_data( - inner_place); + return static_cast(impl_.get())->mutable_data(place); } return nullptr; } -template PADDLE_API float *Tensor::mutable_data(const PlaceType &place); -template PADDLE_API double *Tensor::mutable_data( - const PlaceType &place); -template PADDLE_API int64_t *Tensor::mutable_data( - const PlaceType &place); -template PADDLE_API int32_t *Tensor::mutable_data( - const PlaceType &place); -template PADDLE_API uint8_t *Tensor::mutable_data( - const PlaceType &place); -template PADDLE_API int8_t *Tensor::mutable_data( - const PlaceType &place); -template PADDLE_API int16_t *Tensor::mutable_data( - const PlaceType &place); -template PADDLE_API bool *Tensor::mutable_data(const PlaceType &place); +template PADDLE_API float *Tensor::mutable_data(const Place &place); +template PADDLE_API double *Tensor::mutable_data(const Place &place); +template PADDLE_API int64_t *Tensor::mutable_data(const Place &place); +template PADDLE_API int32_t *Tensor::mutable_data(const Place &place); +template PADDLE_API uint8_t *Tensor::mutable_data(const Place &place); +template PADDLE_API int8_t *Tensor::mutable_data(const Place &place); +template PADDLE_API int16_t *Tensor::mutable_data(const Place &place); +template PADDLE_API bool *Tensor::mutable_data(const Place &place); template PADDLE_API phi::dtype::complex - *Tensor::mutable_data>(const PlaceType &place); + *Tensor::mutable_data>(const Place &place); template PADDLE_API phi::dtype::complex - *Tensor::mutable_data>(const PlaceType &place); + *Tensor::mutable_data>(const Place &place); template PADDLE_API phi::dtype::float16 * -Tensor::mutable_data(const PlaceType &place); +Tensor::mutable_data(const Place &place); template const T *Tensor::data() const { if (is_dense_tensor()) { - return std::dynamic_pointer_cast(impl_)->data(); - } else if (phi::SelectedRows::classof(impl_.get())) { - return std::dynamic_pointer_cast(impl_) - ->value() - .data(); + return static_cast(impl_.get())->data(); + } else if (is_selected_rows()) { + return static_cast(impl_.get())->value().data(); } return nullptr; } @@ -271,9 +273,9 @@ Tensor::data() const; template T *Tensor::data() { if (is_dense_tensor()) { - return std::dynamic_pointer_cast(impl_)->data(); - } else if (phi::SelectedRows::classof(impl_.get())) { - return std::dynamic_pointer_cast(impl_) + return static_cast(impl_.get())->data(); + } else if (is_selected_rows()) { + return static_cast(impl_.get()) ->mutable_value() ->data(); } @@ -299,7 +301,7 @@ Tensor Tensor::slice(int64_t begin_idx, int64_t end_idx) const { if (is_dense_tensor()) { return Tensor(std::make_shared( std::move(phi::DenseTensorUtils::Slice( - *(std::dynamic_pointer_cast(impl_).get()), + *(static_cast(impl_.get())), begin_idx, end_idx)))); } else { @@ -331,6 +333,10 @@ bool Tensor::defined() const { return impl_ != nullptr; } bool Tensor::initialized() const { return defined() && impl_->initialized(); } bool Tensor::is_initialized() const { + LOG_FIRST_N(WARNING, 1) + << "The `is_initialized` method is deprecated since version " + "2.3, and will be removed in version 2.4! " + "Please use `initialized` method instead."; return defined() && impl_->initialized(); } @@ -342,7 +348,6 @@ Tensor &Tensor::operator=(const Tensor &x) & { impl_ = x.impl_; autograd_meta_ = x.autograd_meta_; name_ = x.name_; - place_ = x.place_; return *this; } @@ -350,7 +355,6 @@ Tensor &Tensor::operator=(Tensor &&x) & { impl_ = std::move(x.impl_); autograd_meta_ = std::move(x.autograd_meta_); name_ = std::move(x.name_); - place_ = std::move(x.place_); return *this; } @@ -371,8 +375,7 @@ void Tensor::set_autograd_meta( void Tensor::bump_inplace_version() { if (is_dense_tensor()) { auto &inplace_version_counter = - std::dynamic_pointer_cast(impl_) - ->InplaceVersionCounter(); + static_cast(impl_.get())->InplaceVersionCounter(); inplace_version_counter.Bump(); } else { PADDLE_THROW(phi::errors::Unimplemented( @@ -383,8 +386,7 @@ void Tensor::bump_inplace_version() { uint32_t Tensor::current_inplace_version() { if (is_dense_tensor()) { auto &inplace_version_counter = - std::dynamic_pointer_cast(impl_) - ->InplaceVersionCounter(); + static_cast(impl_.get())->InplaceVersionCounter(); return inplace_version_counter.CurrentVersion(); } else { PADDLE_THROW(phi::errors::Unimplemented( @@ -397,8 +399,7 @@ void Tensor::reset_inplace_version(bool set_to_zero) { if (set_to_zero) { if (is_dense_tensor()) { auto &inplace_version_counter = - std::dynamic_pointer_cast(impl_) - ->InplaceVersionCounter(); + static_cast(impl_.get())->InplaceVersionCounter(); inplace_version_counter.SetInplaceVersionToZero(); } } diff --git a/paddle/phi/api/lib/tensor_method.cc b/paddle/phi/api/lib/tensor_method.cc index c4c77ab93790d9ce419f9f29361ef816f00791f4..463b72d0dbf5b1013acc7662721c02b9bb6f5bf1 100644 --- a/paddle/phi/api/lib/tensor_method.cc +++ b/paddle/phi/api/lib/tensor_method.cc @@ -14,7 +14,6 @@ limitations under the License. */ #include "paddle/phi/api/include/tensor.h" -#include "paddle/phi/api/lib/ext_compat_utils.h" #include "paddle/phi/common/int_array.h" #include "paddle/phi/core/compat/convert_utils.h" #include "paddle/phi/core/tensor_base.h" @@ -39,42 +38,43 @@ Tensor Tensor::copy_to(Place place, bool blocking) const { } template -Tensor Tensor::copy_to(const PlaceType &target_place) const { - LOG(WARNING) << "The Tensor's `copy_to` method is deprecated since version " - "2.3, and will be removed in version 2.4, please use " - "`copy_to` method without template argument instead. " - "reason: copying a Tensor to another device does not need " - "to specify the data type template argument."; - return copy_to(ConvertExtPlaceToInnerPlace(target_place), /*blocking=*/false); +Tensor Tensor::copy_to(const Place &target_place) const { + LOG_FIRST_N(WARNING, 1) + << "The Tensor's `copy_to` method is deprecated since version " + "2.3, and will be removed in version 2.4, please use " + "`copy_to` method without template argument instead. " + "reason: copying a Tensor to another device does not need " + "to specify the data type template argument."; + return copy_to(target_place, /*blocking=*/false); } template PADDLE_API Tensor -Tensor::copy_to(const PlaceType &target_place) const; +Tensor::copy_to(const Place &target_place) const; template PADDLE_API Tensor -Tensor::copy_to(const PlaceType &target_place) const; +Tensor::copy_to(const Place &target_place) const; template PADDLE_API Tensor -Tensor::copy_to(const PlaceType &target_place) const; +Tensor::copy_to(const Place &target_place) const; template PADDLE_API Tensor -Tensor::copy_to(const PlaceType &target_place) const; +Tensor::copy_to(const Place &target_place) const; template PADDLE_API Tensor -Tensor::copy_to(const PlaceType &target_place) const; +Tensor::copy_to(const Place &target_place) const; template PADDLE_API Tensor -Tensor::copy_to(const PlaceType &target_place) const; +Tensor::copy_to(const Place &target_place) const; template PADDLE_API Tensor -Tensor::copy_to(const PlaceType &target_place) const; +Tensor::copy_to(const Place &target_place) const; template PADDLE_API Tensor -Tensor::copy_to(const PlaceType &target_place) const; -template PADDLE_API Tensor Tensor::copy_to>( - const PlaceType &target_place) const; -template PADDLE_API Tensor Tensor::copy_to>( - const PlaceType &target_place) const; +Tensor::copy_to(const Place &target_place) const; template PADDLE_API Tensor -Tensor::copy_to(const PlaceType &target_place) const; +Tensor::copy_to>(const Place &target_place) const; +template PADDLE_API Tensor +Tensor::copy_to>(const Place &target_place) const; +template PADDLE_API Tensor +Tensor::copy_to(const Place &target_place) const; void Tensor::copy_(const Tensor &src, const phi::Place &target_place, bool blocking) { - if (!src.is_initialized()) { + if (!src.initialized()) { VLOG(8) << "Src is empty, skip copy"; return; } @@ -82,7 +82,7 @@ void Tensor::copy_(const Tensor &src, auto kernel_key_set = ParseKernelKeyByInputArgs(src); KernelType kernel_type = ParseKernelTypeByInputArgs(src); VLOG(3) << "Deep copy Tensor from " << src.name() << " to " << name(); - if (is_initialized()) { + if (initialized()) { PADDLE_ENFORCE_EQ(dtype(), src.dtype(), phi::errors::PreconditionNotMet( @@ -98,16 +98,15 @@ void Tensor::copy_(const Tensor &src, name(), src.name())); PADDLE_ENFORCE_EQ(target_place, - inner_place(), + place(), phi::errors::PreconditionNotMet( "Place is different of dst tensor and args %s, which " "current tensor holds %s " "Copy cannot be performed!", target_place, - inner_place())); - kernel_key_set.backend_set = - kernel_key_set.backend_set | - BackendSet(phi::TransToPhiBackend(inner_place())); + place())); + kernel_key_set.backend_set = kernel_key_set.backend_set | + BackendSet(phi::TransToPhiBackend(place())); } else { // Deep Copy AutoGrad info from src to self. *autograd_meta_ = *(src.autograd_meta_); diff --git a/paddle/phi/common/place.cc b/paddle/phi/common/place.cc index 2b5254d3d5f142b5269a667a7a9bf6780c9bb932..667d0a32b93da3f6b82a21f93c14927cb7db81d0 100644 --- a/paddle/phi/common/place.cc +++ b/paddle/phi/common/place.cc @@ -18,6 +18,8 @@ limitations under the License. */ #include #include +#include "glog/logging.h" + #include "paddle/phi/api/ext/exception.h" namespace phi { @@ -108,4 +110,34 @@ uint32_t Place::Hash::operator()(const Place &place) const { return hash_value; } +Place::Place(paddle::PlaceType type) + : device(0), + alloc_type_(static_cast(type)), + device_type_id_(GetOrRegisterGlobalDeviceTypeId("")) { + LOG_FIRST_N(WARNING, 1) + << "The `paddle::PlaceType::kCPU/kGPU` is deprecated since version " + "2.3, and will be removed in version 2.4! Please use " + "`paddle::CPUPlace()/GPUPlace()` to represent the place type."; +} + } // namespace phi + +namespace paddle { + +bool operator==(const Place &place, PlaceType place_type) { + LOG_FIRST_N(WARNING, 1) + << "The `paddle::PlaceType::kCPU/kGPU` is deprecated since version " + "2.3, and will be removed in version 2.4! Please use " + "`Tensor::is_cpu()/is_gpu()` method to determine the type of place."; + return place.GetType() == static_cast(place_type); +} + +bool operator==(PlaceType place_type, const Place &place) { + LOG_FIRST_N(WARNING, 1) + << "The `paddle::PlaceType::kCPU/kGPU` is deprecated since version " + "2.3, and will be removed in version 2.4! Please use " + "`Tensor::is_cpu()/is_gpu()` method to determine the type of place."; + return static_cast(place_type) == place.GetType(); +} + +} // namespace paddle diff --git a/paddle/phi/common/place.h b/paddle/phi/common/place.h index 390684366db716cef0aac3b0f249586b8bedb7cb..ed9fb7876425d36281ef1ab1234442cb9fa0c5f8 100644 --- a/paddle/phi/common/place.h +++ b/paddle/phi/common/place.h @@ -18,6 +18,10 @@ limitations under the License. */ #include "paddle/phi/api/include/dll_decl.h" +namespace paddle { +enum class PlaceType; +} + namespace phi { enum class AllocationType : int8_t { @@ -57,6 +61,9 @@ class PADDLE_API Place { alloc_type_(type), device_type_id_(GetOrRegisterGlobalDeviceTypeId(dev_type)) {} + // See NOTE [ Why need to temporarily adapt to PlaceType? ] + Place(paddle::PlaceType type); // NOLINT + void Reset(AllocationType type, int8_t device_id = 0, const std::string& dev_type = "") noexcept { @@ -213,4 +220,43 @@ using GPUPinnedPlace = phi::GPUPinnedPlace; using XPUPlace = phi::XPUPlace; using NPUPlace = phi::NPUPlace; } // namespace experimental + +using AllocationType = phi::AllocationType; +using Place = phi::Place; +using CPUPlace = phi::CPUPlace; +using GPUPlace = phi::GPUPlace; + +/* NOTE [ Why need to temporarily adapt to PlaceType? ] + +`PlaceType` emum class is the place type used by custom operators since the +release of 2.0. Since 2.3, we have refactored the operator library and designed +a new external Place type. The original PlaceType is no longer suitable for use +as an internal type of the framework, but immediately delete the PlaceType, +it will cause the previous custom operators to be incompatible, so it cannot be +deleted in the short term. We'd better delete this abandoned data type in 2.4. + +Note: This type cannot add any new type!!! It is only used for compatibility +with +historical writing and we will remove this temporary type in the future. +This Type cannot be used in framework! only used for custom operator! + +The original PlaceType define: + +- enum class PlaceType { kUNK = -1, kCPU, kGPU }; + +The historical PlaceType using: + +- PD_CHECK(x.place() == paddle::PlaceType::kCPU) +- auto out = paddle::Tensor(paddle::PlaceType::kCPU, x.shape()); + +*/ +enum class PlaceType { + kUNK = static_cast(phi::AllocationType::UNDEFINED), + kCPU = static_cast(phi::AllocationType::CPU), + kGPU = static_cast(phi::AllocationType::GPU), +}; + +PADDLE_API bool operator==(const Place& place, PlaceType place_type); +PADDLE_API bool operator==(PlaceType place_type, const Place& place); + } // namespace paddle diff --git a/paddle/phi/core/dense_tensor.cc b/paddle/phi/core/dense_tensor.cc index 8acdd8b34f7d1dd099b848359140b3514cc03d93..1bfe29bc9d3ba211681e18b749eb3b801abc019f 100644 --- a/paddle/phi/core/dense_tensor.cc +++ b/paddle/phi/core/dense_tensor.cc @@ -19,7 +19,24 @@ limitations under the License. */ #include "paddle/phi/common/float16.h" #include "paddle/phi/core/compat/convert_utils.h" -// See Note [ Why still include the fluid headers? ] +/** + * [ Why still include the fluid headers? ] + * + * We hope to organize the basic implementation of Tensor and the logic related + * to Tensor computation into an independent library, which we call + * [Tensor Operation Library, phi], so we extract or rewrite the original + * Kernels. + * + * In the future, the training library, inference library and custom operators + * will link to this Tensor Operation library. + * + * However, if we directly split the link relation, we need to make too many + * changes, which will affect the stability of the framework, so here we still + * rely on the implementation of the framework, which is a intermediate state. + * + * In the future, the necessary components will be moved to the this library, + * or the corresponding components will be re-implemented. + */ #include "paddle/fluid/memory/malloc.h" namespace phi { diff --git a/paddle/phi/tests/api/CMakeLists.txt b/paddle/phi/tests/api/CMakeLists.txt index 94378aceff58cef2656a42a84b390ae3e7493183..e6fbab0aa18d022827e3179f6760bf234374bddc 100644 --- a/paddle/phi/tests/api/CMakeLists.txt +++ b/paddle/phi/tests/api/CMakeLists.txt @@ -1,4 +1,6 @@ -if(WITH_ROCM) +if(WITH_GPU) + nv_test(test_phi_tensor SRCS test_pten_tensor.cc DEPS phi_tensor glog) +elseif(WITH_ROCM) hip_test(test_phi_tensor SRCS test_pten_tensor.cc DEPS phi_tensor glog) else() cc_test(test_phi_tensor SRCS test_pten_tensor.cc DEPS phi_tensor glog) diff --git a/paddle/phi/tests/api/test_pten_tensor.cc b/paddle/phi/tests/api/test_pten_tensor.cc index 74ed648f3ee6ec4e2b763f4a57cc58ce79fb25b9..590717b8d7b77a1937ff897339e7ab7430a024f8 100644 --- a/paddle/phi/tests/api/test_pten_tensor.cc +++ b/paddle/phi/tests/api/test_pten_tensor.cc @@ -15,7 +15,6 @@ #include "glog/logging.h" #include "gtest/gtest.h" #include "paddle/phi/api/include/tensor.h" -#include "paddle/phi/api/lib/ext_compat_utils.h" #include "paddle/phi/core/kernel_registry.h" PD_DECLARE_KERNEL(copy, CPU, ALL_LAYOUT); @@ -201,7 +200,7 @@ void GroupTestDtype() { void TestInitilized() { experimental::Tensor test_tensor(paddle::PlaceType::kCPU, {1, 1}); - CHECK(test_tensor.is_initialized() == false); + CHECK(test_tensor.is_initialized() == true); test_tensor.mutable_data(paddle::PlaceType::kCPU); CHECK(test_tensor.is_initialized() == true); float* tensor_data = test_tensor.mutable_data(); diff --git a/python/paddle/fluid/tests/custom_op/custom_relu_op.cc b/python/paddle/fluid/tests/custom_op/custom_relu_op.cc index 4ff9adf4f8fecaf552938ac96e8fc13df9b37c44..121a855a18f842136bd709c066eaa9ddfa413e62 100644 --- a/python/paddle/fluid/tests/custom_op/custom_relu_op.cc +++ b/python/paddle/fluid/tests/custom_op/custom_relu_op.cc @@ -108,7 +108,6 @@ std::vector relu_cuda_double_backward( const paddle::Tensor& out, const paddle::Tensor& ddx); std::vector ReluForward(const paddle::Tensor& x) { - // TODO(chenweihang): Check Input if (x.place() == paddle::PlaceType::kCPU) { return relu_cpu_forward(x); } else if (x.place() == paddle::PlaceType::kGPU) { diff --git a/python/paddle/fluid/tests/custom_op/custom_relu_op.cu b/python/paddle/fluid/tests/custom_op/custom_relu_op.cu index 8b9693054d1c4e39c43c049c66000e0486aad98c..364a2216b9e8eaa6447123ce8e55736c0d902a62 100644 --- a/python/paddle/fluid/tests/custom_op/custom_relu_op.cu +++ b/python/paddle/fluid/tests/custom_op/custom_relu_op.cu @@ -53,6 +53,7 @@ __global__ void relu_cuda_double_backward_kernel(const data_t* out_data, } std::vector relu_cuda_forward(const paddle::Tensor& x) { + CHECK_GPU_INPUT(x); auto out = paddle::Tensor(paddle::PlaceType::kGPU, x.shape()); int numel = x.size(); @@ -70,6 +71,9 @@ std::vector relu_cuda_forward(const paddle::Tensor& x) { std::vector relu_cuda_backward(const paddle::Tensor& x, const paddle::Tensor& out, const paddle::Tensor& grad_out) { + CHECK_GPU_INPUT(x); + CHECK_GPU_INPUT(out); + CHECK_GPU_INPUT(grad_out); auto grad_x = paddle::Tensor(paddle::PlaceType::kGPU, x.shape()); int numel = out.size();