Skip to content

Commit

Permalink
Fixed MKLDNN & Storage issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jim19930609 committed Jan 6, 2022
1 parent 7c6b10f commit 1d38ca0
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 82 deletions.
1 change: 0 additions & 1 deletion paddle/fluid/framework/data_transform.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 1 addition & 51 deletions paddle/fluid/framework/lod_tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,64 +108,14 @@ 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<size_t, size_t> 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<LoDTensor> SplitLoDTensor(
const std::vector<platform::Place> places) const;

void MergeLoDTensor(const std::vector<const LoDTensor*>& lod_tensors,
platform::Place place);

private:
LoD lod_;
};

/*
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/framework/tensor_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand Down
9 changes: 9 additions & 0 deletions paddle/pten/api/lib/utils/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ class SharedStorage : public pten::Storage {
size_ = 0;
}

void set_data_shared(
const std::shared_ptr<paddle::memory::Allocation>& holder) override {
data_ = holder;
if (holder) {
size_ = holder->size();
place_ = holder->place();
}
}

size_t size() const noexcept override {
return data_ ? data_->size() : size_;
}
Expand Down
91 changes: 62 additions & 29 deletions paddle/pten/core/dense_tensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,32 @@ DenseTensor::DenseTensor(intrusive_ptr<Storage> storage,
DenseTensor::DenseTensor(intrusive_ptr<Storage> 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::experimental::SharedStorage>(
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<paddle::experimental::SharedStorage>(
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;
}

Expand Down Expand Up @@ -212,13 +230,17 @@ DATA_MEMBER_FUNC_INSTANTIATION(::paddle::experimental::complex128);
/* From framework::Tensor */
/* --------------------------- */
DenseTensor::DenseTensor() {
storage_ = make_intrusive<paddle::experimental::SharedStorage>(
paddle::platform::CPUPlace());
inplace_version_counter_ = std::make_shared<TensorInplaceVersion>(0);
meta_ = DenseTensorMeta();
meta_.dtype = paddle::experimental::DataType::FLOAT32;
meta_.offset = 0;
}

DenseTensor::DenseTensor(const paddle::framework::proto::VarType::Type& dtype) {
storage_ = make_intrusive<paddle::experimental::SharedStorage>(
paddle::platform::CPUPlace());
inplace_version_counter_ = std::make_shared<TensorInplaceVersion>(0);
meta_ = DenseTensorMeta();
meta_.dtype = TransToPtenDataType(dtype);
Expand Down Expand Up @@ -288,15 +310,20 @@ 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(),
paddle::platform::errors::InvalidArgument(
"The size of Holder is not enough to store the Tensor."));
}

storage_ = make_intrusive<paddle::experimental::SharedStorage>(holder);
storage_->set_data_shared(holder);
}

void DenseTensor::ResetHolderWithType(
Expand Down Expand Up @@ -328,28 +355,24 @@ void* DenseTensor::mutable_data(const paddle::platform::Place& place,
size = requested_size;
}

if (storage_ == nullptr) {
storage_ = make_intrusive<paddle::experimental::SharedStorage>(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<paddle::experimental::SharedStorage>(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<void*>(reinterpret_cast<uintptr_t>(storage_->data()) +
meta_.offset);
}

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);
}

Expand All @@ -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<paddle::experimental::SharedStorage>(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<paddle::experimental::SharedStorage>(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<void*>(reinterpret_cast<uintptr_t>(storage_->data()) +
meta_.offset);
Expand All @@ -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<T>::value, "T must be POD");
Resize(dims);
meta_.dims = dims;
return mutable_data<T>(place, requested_size);
}

Expand All @@ -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<paddle::experimental::SharedStorage>(
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;
}

Expand Down
2 changes: 1 addition & 1 deletion paddle/pten/core/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Storage : public intrusive_ref_counter<Storage> {
return data_;
}

void set_data_shared(
virtual void set_data_shared(
const std::shared_ptr<paddle::memory::Allocation>& holder) {
data_ = holder;
}
Expand Down

0 comments on commit 1d38ca0

Please sign in to comment.