From 953638e00d3cb246a26dce584c05165126a9031e Mon Sep 17 00:00:00 2001 From: Zhanlue Yang Date: Mon, 10 Jan 2022 16:30:37 +0800 Subject: [PATCH] [Unify Tensors PR #6] Removed interfaces & members from lod_tensor,test=allcases (#38811) * Added shared_ptr member & corresponding interfaces to Storage * Removed original pten::Allocation from Storage and adjusted the interfaces accordingly * Fixed issues with storage offset * Used place to malloc allocation for TensorStorage * [Unify Tensors PR #3]Ported framework::Tensor interfaces to pten::DenseTensor * Fixed issues with place * Added comments * Moved mutable_data with stream argument to DenseTensor * Added set_offset interface * Fixed CI issues,test=allcases * [Unify Tensors PR #4] Port LoDTensor interfaces to DenseTensor * Removed friend class EigenTensor/EigenMatrix/EigenVector from Tensor * Modified framework::Tensor to inherit from DenseTensor * Reverted changes too pten_layout() interface * Removed friend classes * Rearranged cfunction calls from tensor.data() to tensor.data() * Fixed CI issues * Fixed lite issues * Fixed data() interface issues,test=allcases * Resolved IsInitialized() issues * Fixed ResetHolder() issues * Fixed MKLDNN & Storage issues * Resolved ShareBufferWith() issues * Fixed LoD issues * Removed interfaces & members from lod_tensor,test=allcases --- paddle/fluid/framework/lod_tensor.h | 52 +---------------------------- paddle/fluid/framework/tensor.cc | 3 ++ 2 files changed, 4 insertions(+), 51 deletions(-) diff --git a/paddle/fluid/framework/lod_tensor.h b/paddle/fluid/framework/lod_tensor.h index dff6d0e018..22f2027998 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.cc b/paddle/fluid/framework/tensor.cc index e5dfe28be7..f11b37825d 100644 --- a/paddle/fluid/framework/tensor.cc +++ b/paddle/fluid/framework/tensor.cc @@ -110,7 +110,10 @@ std::vector Tensor::Chunk(int64_t chunks, int64_t axis) const { Tensor& Tensor::ShareDataWith(const Tensor& src) { src.check_memory_size(); + // Preserve LoD + auto lod = meta_.lod; *this = src; + meta_.lod = lod; return *this; } Tensor& Tensor::ShareInplaceVersionCounterWith(const Tensor& src) { -- GitLab