Skip to content

Commit 42cf2be

Browse files
authored
[Unify Tensors PR #1] Replaced pten::Allocation with shared_ptr<memory::Allocation> for Storage (PaddlePaddle#38301)
* Added shared_ptr<Allocation> 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
1 parent 52329f6 commit 42cf2be

File tree

7 files changed

+91
-53
lines changed

7 files changed

+91
-53
lines changed

paddle/pten/api/lib/utils/allocator.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class DefaultAllocator : public pten::Allocator {
3838
return Allocation(ptr, a.release(), &Delete, place_);
3939
}
4040

41+
const paddle::platform::Place& place() override { return place_; }
42+
4143
private:
4244
paddle::platform::Place place_;
4345
static paddle::memory::Allocator::AllocationDeleter deleter_;

paddle/pten/api/lib/utils/storage.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ namespace experimental {
2020
ExternalStorage::ExternalStorage(void* ptr,
2121
size_t size,
2222
const paddle::platform::Place& place)
23-
: pten::Storage(pten::Allocation(ptr, place)), size_(size) {}
23+
: pten::Storage(
24+
std::make_shared<paddle::memory::Allocation>(ptr, size, place)),
25+
size_(size) {}
2426

2527
ExternalStorage::ExternalStorage(const pten::intrusive_ptr<pten::Storage>& root,
2628
size_t delta,
2729
size_t size)
28-
: Storage(pten::Allocation(static_cast<uint8_t*>(root->data()) + delta,
29-
root->place())),
30+
: Storage(std::make_shared<paddle::memory::Allocation>(
31+
static_cast<uint8_t*>(root->data()) + delta, size, root->place())),
3032
size_(size) {
3133
PADDLE_ENFORCE_LE(static_cast<size_t>(delta + size),
3234
root->size(),

paddle/pten/api/lib/utils/storage.h

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,18 @@ class ExternalStorage : public pten::Storage {
3535
}
3636

3737
void Clear() override {
38-
data_.Clear();
38+
data_ = nullptr;
3939
size_ = 0;
40+
offset_ = 0;
4041
}
4142

4243
size_t size() const noexcept override { return size_; }
4344
const paddle::platform::Place& place() const override {
44-
return data_.place();
45+
PADDLE_ENFORCE_NOT_NULL(
46+
data_,
47+
paddle::platform::errors::Unavailable(
48+
"Unable to visit place as data_ has not been initialized yet."));
49+
return data_->place();
4550
}
4651
bool OwnsMemory() const noexcept override { return false; }
4752

@@ -54,74 +59,61 @@ class SharedStorage : public pten::Storage {
5459
explicit SharedStorage(
5560
const std::shared_ptr<paddle::memory::Allocation>& allocation,
5661
size_t offset)
57-
: allocation_(allocation) {
62+
: Storage(allocation) {
5863
CHECK(allocation);
59-
data_ = pten::Allocation(
60-
reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(allocation->ptr()) +
61-
offset),
62-
allocation->place());
64+
place_ = allocation->place();
6365
size_ = allocation->size();
66+
offset_ = offset;
6467
}
6568

6669
// In order to be compatible with the original Tensor design and execution
6770
// system, we need to allow the uninitialized SharedStorage to exist,
6871
// and it can be removed after the compatibility phase is over in the future
6972
explicit SharedStorage(const paddle::platform::Place& place) {
70-
data_ = pten::Allocation(nullptr, place);
73+
place_ = place;
7174
}
7275

73-
static const char* name() { return "SharedStorage"; }
74-
75-
// In order to be compatible with the original Tensor design and execution
76-
// system, we need to allow the SharedStorage realloc,
77-
// and it can be removed after the compatibility phase is over in the future
7876
void Realloc(size_t n) override {
79-
ResetAllocation(paddle::memory::AllocShared(place(), n), 0);
77+
this->Clear();
78+
data_ = paddle::memory::AllocShared(place(), n);
79+
size_ = n;
8080
}
8181

82+
static const char* name() { return "SharedStorage"; }
83+
8284
void Clear() override {
83-
data_.Clear();
85+
data_ = nullptr;
8486
size_ = 0;
8587
}
8688

8789
size_t size() const noexcept override { return size_; }
88-
const paddle::platform::Place& place() const override {
89-
return data_.place();
90-
}
90+
const paddle::platform::Place& place() const override { return place_; }
9191
bool OwnsMemory() const noexcept override { return false; }
9292

9393
const std::shared_ptr<paddle::memory::Allocation>& GetAllocation() {
94-
return allocation_;
94+
return data_;
9595
}
9696

9797
// Temporary method: For compatible with fluid Tensor and improve performance
9898
void ResetAllocation(std::shared_ptr<paddle::memory::Allocation> allocation,
9999
size_t offset) {
100-
allocation_ = allocation;
101-
data_ = pten::Allocation(
102-
reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(allocation->ptr()) +
103-
offset),
104-
allocation->place());
100+
data_ = allocation;
105101
size_ = allocation->size();
102+
place_ = allocation->place();
103+
offset_ = offset;
106104
}
107105

108106
// Temporary method: For compatible with fluid Tensor and improve performance
109107
void ResetAllocationPlace(const paddle::platform::Place& place) {
110-
data_ = pten::Allocation(nullptr, place);
108+
place_ = place;
111109
}
112110

113111
// Temporary method: For compatible with fluid Tensor and improve performance
114-
void Reset() {
115-
if (allocation_ != nullptr) {
116-
allocation_.reset();
117-
}
118-
data_.Clear();
119-
size_ = 0;
120-
}
112+
void Reset() { this->Clear(); }
121113

122114
private:
115+
Place place_;
123116
int64_t size_{0};
124-
std::shared_ptr<paddle::memory::Allocation> allocation_;
125117
};
126118

127119
class TensorStorage : public paddle::memory::allocation::Allocation {

paddle/pten/core/allocator.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,12 @@ inline void swap(Allocation& a, Allocation& b) noexcept {
134134
/// mainly used for general data structures such as Tensor. The raw
135135
/// allocator is more universal and efficient.
136136
class Allocator {
137+
using Place = paddle::platform::Place;
138+
137139
public:
138140
virtual ~Allocator() = default;
139141
virtual Allocation Allocate(size_t bytes_size) = 0;
142+
virtual const Place& place() = 0;
140143
};
141144

142145
inline Allocation Allocate(const std::shared_ptr<Allocator>& a, size_t n) {

paddle/pten/core/storage.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ limitations under the License. */
1717
namespace pten {
1818

1919
void TensorStorage::Realloc(size_t size) {
20-
data_.Clear();
21-
data_ = Allocate(alloc_, size);
20+
this->Clear();
21+
data_ = paddle::memory::AllocShared(alloc_->place(), size);
2222
size_ = size;
2323
}
2424

paddle/pten/core/storage.h

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ limitations under the License. */
2121
#include "paddle/pten/core/utils/intrusive_ref_counter.h"
2222
#include "paddle/pten/core/utils/type_info.h"
2323

24+
#include "paddle/fluid/memory/memory.h"
2425
#include "paddle/fluid/platform/place.h"
2526
#include "paddle/pten/core/allocator.h"
2627

@@ -35,14 +36,32 @@ class Storage : public intrusive_ref_counter<Storage> {
3536
Storage() = default;
3637
Storage(const Storage&) = delete;
3738

38-
explicit Storage(Allocation&& data) : data_(std::move(data)) {}
39+
/* --------- shared_ptr<Allocation> -------- */
40+
// Initialize a Storage with unique Allocation
41+
explicit Storage(std::shared_ptr<paddle::memory::Allocation>&& data)
42+
: data_(std::move(data)) {}
3943

40-
virtual ~Storage() = default;
44+
// Initialize a Storage shareing Allocation with another storage
45+
explicit Storage(const std::shared_ptr<paddle::memory::Allocation>& data)
46+
: data_(data) {}
47+
48+
void* data() const {
49+
return data_ ? reinterpret_cast<void*>(
50+
reinterpret_cast<uintptr_t>(data_->ptr()) + offset_)
51+
: nullptr;
52+
}
53+
54+
const std::shared_ptr<paddle::memory::Allocation> data_shared() const {
55+
return data_;
56+
}
4157

42-
/// \brief Get the mutable data pointer of the storage.
43-
/// This function is set to inline to improve performance.
44-
/// \return The mutable data pointer of the storage.
45-
void* data() const noexcept { return data_.operator->(); }
58+
virtual void ReallocShared(size_t n) {
59+
PADDLE_THROW(paddle::platform::errors::Unimplemented(
60+
"ReallocShared has not been overrided by the current Storage"));
61+
}
62+
/* --------- shared_ptr<Allocation> -------- */
63+
64+
virtual ~Storage() = default;
4665

4766
virtual void Clear() = 0;
4867

@@ -52,31 +71,47 @@ class Storage : public intrusive_ref_counter<Storage> {
5271
virtual void Realloc(size_t n) = 0;
5372

5473
protected:
55-
Allocation data_;
74+
size_t offset_{0};
75+
std::shared_ptr<paddle::memory::Allocation> data_;
5676
};
5777

5878
class TensorStorage : public Storage {
5979
public:
6080
using Place = paddle::platform::Place;
6181

6282
explicit TensorStorage(const std::shared_ptr<Allocator>& a) : alloc_(a) {}
83+
6384
TensorStorage(const std::shared_ptr<Allocator>& a, size_t size)
64-
: Storage(Allocate(a, size)), alloc_(a), size_(size) {}
85+
: Storage(paddle::memory::AllocShared(a->place(), size)), alloc_(a) {
86+
size_ = data_->size();
87+
}
88+
89+
void Clear() override {
90+
data_ = nullptr;
91+
size_ = 0;
92+
offset_ = 0;
93+
}
94+
95+
void Realloc(size_t size) override;
6596

6697
~TensorStorage() = default;
6798

6899
static const char* name() { return "TensorStorage"; }
69100

70-
void Realloc(size_t size) override;
71-
72101
size_t size() const noexcept override { return size_; }
73102

74-
void Clear() override {
75-
data_.Clear();
76-
size_ = 0;
103+
const Place& place() const override {
104+
if (!data_ && !alloc_) {
105+
PADDLE_THROW(paddle::platform::errors::Unimplemented(
106+
"Unable to visit place: either data_ or alloc_ has to be initialized "
107+
"first."));
108+
}
109+
if (data_) {
110+
return data_->place();
111+
}
112+
return alloc_->place();
77113
}
78114

79-
const Place& place() const override { return data_.place(); }
80115
bool OwnsMemory() const noexcept override { return true; }
81116
const std::shared_ptr<Allocator>& allocator() const noexcept {
82117
return alloc_;

paddle/pten/tests/core/allocator.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,12 @@ class FancyAllocator : public pten::Allocator {
4444

4545
Allocation Allocate(size_t bytes_size) override {
4646
void* data = ::operator new(bytes_size);
47-
return Allocation(data, data, &Delete, paddle::platform::CPUPlace());
47+
return Allocation(data, data, &Delete, place());
4848
}
49+
50+
const paddle::platform::Place& place() override { return place_; }
51+
52+
paddle::platform::Place place_ = paddle::platform::CPUPlace();
4953
};
5054

5155
template <typename T>

0 commit comments

Comments
 (0)