diff --git a/paddle/fluid/framework/data_transform.cc b/paddle/fluid/framework/data_transform.cc index 16c1923ce1815..d8c372becf1b4 100644 --- a/paddle/fluid/framework/data_transform.cc +++ b/paddle/fluid/framework/data_transform.cc @@ -45,7 +45,6 @@ void TransformData(const OpKernelType &expected_kernel_type, Tensor out; const DataLayout lin = kernel_type_for_var.data_layout_; const DataLayout lout = expected_kernel_type.data_layout_; - // do layout transform if (NeedTransformLayout(lout, lin)) { #ifdef PADDLE_WITH_MKLDNN diff --git a/paddle/fluid/framework/lod_tensor.h b/paddle/fluid/framework/lod_tensor.h index 7dee0f44e384d..22f2027998137 100644 --- a/paddle/fluid/framework/lod_tensor.h +++ b/paddle/fluid/framework/lod_tensor.h @@ -108,54 +108,7 @@ bool CheckAbsLoD(const LoD& in, int tensor_height = -1); */ class LoDTensor : public Tensor { public: - LoDTensor() : Tensor() {} - - explicit LoDTensor(const LoD& lod) : lod_(lod) {} - - void set_lod(const LoD& lod) { lod_ = lod; } - - const LoD& lod() const { return lod_; } - - LoD* mutable_lod() { return &lod_; } - - /* - * Get the start offset and end offset of an element from LoD. - */ - std::pair lod_element(size_t level, size_t elem) const { - PADDLE_ENFORCE_LT( - level, NumLevels(), - platform::errors::InvalidArgument( - "The input level of LoD is invalid, it should be less than LoD " - "size. The input level is %zu, the LoD size is %zu.", - level, NumLevels())); - PADDLE_ENFORCE_LT(elem, NumElements(level), - platform::errors::InvalidArgument( - "The input element of LoD is invalid, it should be " - "less than the number of elements in its level." - "The input element is %zu, the number of elements in " - "its level is %zu.", - elem, NumElements(level))); - return std::make_pair((lod_)[level][elem], (lod_)[level][elem + 1]); - } - - /* - * Number of LoDTensor's levels, each level has units of data, for example, - * in the sentence's view, article, paragraph, sentence are 3 levels. - */ - size_t NumLevels() const { return lod_.size(); } - /* - * Number of elements in a level. - */ - size_t NumElements(size_t level = 0) const { - PADDLE_ENFORCE_LT( - level, NumLevels(), - platform::errors::InvalidArgument( - "The input level of LoD is invalid, it should be less than LoD " - "size. The input level is %zu, the LoD size is %zu.", - level, NumLevels())); - // the last offset is the end of last element - return (lod_)[level].size() - 1; - } + using Tensor::Tensor; // Split LoDTensor and copy to each place specified in places. std::vector SplitLoDTensor( @@ -163,9 +116,6 @@ class LoDTensor : public Tensor { void MergeLoDTensor(const std::vector& lod_tensors, platform::Place place); - - private: - LoD lod_; }; /* diff --git a/paddle/fluid/framework/tensor_util.cc b/paddle/fluid/framework/tensor_util.cc index 9d05cc07a30a7..f2323f6e2c6ee 100644 --- a/paddle/fluid/framework/tensor_util.cc +++ b/paddle/fluid/framework/tensor_util.cc @@ -441,6 +441,7 @@ void TensorCopySync(const Tensor& src, const platform::Place& dst_place, auto src_place = src.place(); auto src_ptr = src.data(); auto dst_ptr = dst->mutable_data(dst_place, src.type()); + VLOG(4) << "src:" << src_ptr << ", dst:" << dst_ptr; if (src_ptr == dst_ptr && src_place == dst_place) { VLOG(3) << "Skip copy the same data from " << src_place << " to " diff --git a/paddle/pten/api/lib/utils/storage.h b/paddle/pten/api/lib/utils/storage.h index 7fb0c1e8fa23b..e102ecbc5de7d 100644 --- a/paddle/pten/api/lib/utils/storage.h +++ b/paddle/pten/api/lib/utils/storage.h @@ -83,6 +83,15 @@ class SharedStorage : public pten::Storage { size_ = 0; } + void set_data_shared( + const std::shared_ptr& holder) override { + data_ = holder; + if (holder) { + size_ = holder->size(); + place_ = holder->place(); + } + } + size_t size() const noexcept override { return data_ ? data_->size() : size_; } diff --git a/paddle/pten/core/dense_tensor.cc b/paddle/pten/core/dense_tensor.cc index 7513ff8930897..52c936b2fe12a 100644 --- a/paddle/pten/core/dense_tensor.cc +++ b/paddle/pten/core/dense_tensor.cc @@ -41,14 +41,32 @@ DenseTensor::DenseTensor(intrusive_ptr storage, DenseTensor::DenseTensor(intrusive_ptr storage, DenseTensorMeta&& meta) : meta_(std::move(meta)), storage_(std::move(storage)) {} -DenseTensor::DenseTensor(const DenseTensor& other) - : meta_(other.meta()), storage_(copy_intrusive(other.storage_)) {} +DenseTensor::DenseTensor(const DenseTensor& other) : meta_(other.meta()) { + if (storage_ == nullptr) { + storage_ = make_intrusive( + paddle::platform::CPUPlace()); + } + if (other.storage_ != nullptr && other.storage_->data_shared()) { + storage_->set_data_shared(other.storage_->data_shared()); + } + +#ifdef PADDLE_WITH_MKLDNN + format_ = other.format_; +#endif +} DenseTensor& DenseTensor::operator=(const DenseTensor& other) { meta_ = other.meta(); - if (other.storage_ != nullptr) + if (storage_ == nullptr) { storage_ = make_intrusive( - other.storage_->data_shared()); + paddle::platform::CPUPlace()); + } + if (other.storage_ != nullptr && other.storage_->data_shared()) { + storage_->set_data_shared(other.storage_->data_shared()); + } +#ifdef PADDLE_WITH_MKLDNN + format_ = other.format_; +#endif return *this; } @@ -212,6 +230,8 @@ DATA_MEMBER_FUNC_INSTANTIATION(::paddle::experimental::complex128); /* From framework::Tensor */ /* --------------------------- */ DenseTensor::DenseTensor() { + storage_ = make_intrusive( + paddle::platform::CPUPlace()); inplace_version_counter_ = std::make_shared(0); meta_ = DenseTensorMeta(); meta_.dtype = paddle::experimental::DataType::FLOAT32; @@ -219,6 +239,8 @@ DenseTensor::DenseTensor() { } DenseTensor::DenseTensor(const paddle::framework::proto::VarType::Type& dtype) { + storage_ = make_intrusive( + paddle::platform::CPUPlace()); inplace_version_counter_ = std::make_shared(0); meta_ = DenseTensorMeta(); meta_.dtype = TransToPtenDataType(dtype); @@ -288,7 +310,12 @@ void DenseTensor::ResetHolder( paddle::platform::errors::Fatal( "Only the offset is supported to zero when the holder is reset.")); - if (storage_ != nullptr && storage_->data_shared()) { + PADDLE_ENFORCE_NOT_NULL( + storage_, + paddle::platform::errors::PreconditionNotMet( + "The storage must be valid when call the mutable data function.")); + + if (storage_->data_shared()) { PADDLE_ENFORCE_LE( numel() * SizeOf(dtype()) + meta_.offset, holder->size(), @@ -296,7 +323,7 @@ void DenseTensor::ResetHolder( "The size of Holder is not enough to store the Tensor.")); } - storage_ = make_intrusive(holder); + storage_->set_data_shared(holder); } void DenseTensor::ResetHolderWithType( @@ -328,16 +355,17 @@ void* DenseTensor::mutable_data(const paddle::platform::Place& place, size = requested_size; } + if (storage_ == nullptr) { + storage_ = make_intrusive(place); + } + /* some versions of boost::variant don't have operator!= */ - if (storage_ == nullptr || storage_->data_shared() == nullptr || + if (storage_->data_shared() == nullptr || !(storage_->data_shared()->place() == place) || storage_->data_shared()->size() < size + meta_.offset) { - auto shared_storage = - std::move(make_intrusive(place)); - shared_storage->ResetAllocationPlace(place); - shared_storage->Realloc(size); + storage_->Clear(); + storage_->set_data_shared(paddle::memory::AllocShared(place, size)); meta_.offset = 0; - storage_ = std::move(shared_storage); } return reinterpret_cast(reinterpret_cast(storage_->data()) + meta_.offset); @@ -345,11 +373,6 @@ void* DenseTensor::mutable_data(const paddle::platform::Place& place, void* DenseTensor::mutable_data(const paddle::platform::Place& place, size_t requested_size) { - if (storage_ == nullptr) { - PADDLE_THROW(paddle::platform::errors::PreconditionNotMet( - "The tensor is not initialized.")); - } - return mutable_data(place, type(), requested_size); } @@ -367,20 +390,19 @@ void* DenseTensor::mutable_data(const paddle::platform::Place& place, "] now")); size_t size = numel() * SizeOf(dtype()); + if (storage_ == nullptr) { + storage_ = make_intrusive(place); + } + /* some versions of boost::variant don't have operator!= */ - if (storage_ == nullptr || storage_->data_shared() == nullptr || + if (storage_->data_shared() == nullptr || !(storage_->data_shared()->place() == place) || storage_->data_shared()->size() < size + meta_.offset || !(paddle::platform::is_gpu_place(place) && paddle::memory::InSameStream(storage_->data_shared(), stream))) { - auto shared_storage = - make_intrusive(place); - shared_storage->Clear(); - shared_storage->ResetAllocationPlace(place); - shared_storage->set_data_shared( - paddle::memory::AllocShared(place, size, stream)); + storage_->Clear(); + storage_->set_data_shared(paddle::memory::AllocShared(place, size, stream)); meta_.offset = 0; - storage_ = std::move(shared_storage); } return reinterpret_cast(reinterpret_cast(storage_->data()) + meta_.offset); @@ -396,7 +418,7 @@ inline T* DenseTensor::mutable_data(const DDim& dims, const paddle::platform::Place& place, size_t requested_size) { static_assert(std::is_pod::value, "T must be POD"); - Resize(dims); + meta_.dims = dims; return mutable_data(place, requested_size); } @@ -409,9 +431,20 @@ inline T* DenseTensor::mutable_data(const paddle::platform::Place& place, } void DenseTensor::ShareBufferWith(const DenseTensor& tensor) { - if (tensor.storage_ != nullptr) - storage_ = make_intrusive( - tensor.storage_->data_shared()); + PADDLE_ENFORCE_NOT_NULL( + storage_, + paddle::platform::errors::PreconditionNotMet( + "The storage must be valid when call the mutable data function.")); + PADDLE_ENFORCE_NOT_NULL( + tensor.storage_, + paddle::platform::errors::PreconditionNotMet( + "The storage must be valid when call the mutable data function.")); + PADDLE_ENFORCE_NOT_NULL( + tensor.storage_->data_shared(), + paddle::platform::errors::PreconditionNotMet( + "The storage must be valid when call the mutable data function.")); + + storage_->set_data_shared(tensor.storage_->data_shared()); meta_.offset = tensor.meta().offset; } diff --git a/paddle/pten/core/storage.h b/paddle/pten/core/storage.h index 74c303697755a..fc56935eeaf19 100644 --- a/paddle/pten/core/storage.h +++ b/paddle/pten/core/storage.h @@ -60,7 +60,7 @@ class Storage : public intrusive_ref_counter { return data_; } - void set_data_shared( + virtual void set_data_shared( const std::shared_ptr& holder) { data_ = holder; }