Skip to content

Commit d6fcb2a

Browse files
committed
[executorch] Migrate runner-like targets to use the new HierarchicalAllocator span ctor
Stop using the deprecated HierarchicalAllocator ctor. Differential Revision: [D49344926](https://our.internmc.facebook.com/intern/diff/D49344926/) ghstack-source-id: 200970940 Pull Request resolved: #388
1 parent 7d00cdc commit d6fcb2a

File tree

6 files changed

+55
-66
lines changed

6 files changed

+55
-66
lines changed

examples/bundled_executor_runner/bundled_executor_runner.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -154,26 +154,27 @@ int main(int argc, char** argv) {
154154
MemoryAllocator(kRuntimeMemorySize, runtime_pool)};
155155
runtime_allocator.enable_profiling("runtime allocator");
156156

157-
// The non-const allocator is used to provide the memory-planned buffers that
158-
// back mutable tensors. Since it was planned ahead of time, the Program knows
159-
// how big each of the allocators needs to be.
157+
// The non-const buffers will back the mutable tensors used by the method. The
158+
// sizes of these buffers were determined ahead of time during the
159+
// memory-planning pasees.
160160
//
161-
// These buffers correspond to different hardware memory banks. Most mobile
162-
// environments will only have a single buffer. Some embedded environments may
163-
// have more than one for, e.g., slow/large DRAM and fast/small SRAM.
161+
// Each buffer typically corresponds to a different hardware memory bank. Most
162+
// mobile environments will only have a single buffer. Some embedded
163+
// environments may have more than one for, e.g., slow/large DRAM and
164+
// fast/small SRAM, or for memory associated with particular cores.
164165
std::vector<std::unique_ptr<uint8_t[]>> non_const_buffers;
165-
std::vector<MemoryAllocator> non_const_allocators;
166+
std::vector<Span<uint8_t>> non_const_spans;
166167
size_t num_non_const_buffers = method_meta->num_non_const_buffers();
167168
for (size_t id = 0; id < num_non_const_buffers; ++id) {
168-
size_t buffer_size = method_meta->non_const_buffer_size(id).get();
169+
// .get() will always succeed because id < num_non_const_buffers.
170+
size_t buffer_size =
171+
static_cast<size_t>(method_meta->non_const_buffer_size(id).get());
169172
ET_LOG(Info, "Setting up non-const buffer %zu, size %zu.", id, buffer_size);
170173
non_const_buffers.push_back(std::make_unique<uint8_t[]>(buffer_size));
171-
non_const_allocators.push_back(
172-
MemoryAllocator(buffer_size, non_const_buffers.back().get()));
173-
non_const_allocators.back().enable_profiling("non_const_allocators");
174+
non_const_spans.push_back({non_const_buffers.back().get(), buffer_size});
174175
}
175176
HierarchicalAllocator non_const_allocator(
176-
non_const_allocators.size(), non_const_allocators.data());
177+
{non_const_spans.data(), non_const_spans.size()});
177178

178179
// Allocator for bundled input.
179180
MemoryAllocator bundled_input_allocator{

examples/executor_runner/executor_runner.cpp

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -114,33 +114,27 @@ int main(int argc, char** argv) {
114114
MemoryAllocator(kRuntimeMemorySize, runtime_pool)};
115115
runtime_allocator.enable_profiling("runtime allocator");
116116

117-
// The non-const allocator is used to provide the memory-planned buffers that
118-
// back mutable tensors. Since it was planned ahead of time, the Program knows
119-
// how big each of the allocators needs to be.
117+
// The non-const buffers will back the mutable tensors used by the method. The
118+
// sizes of these buffers were determined ahead of time during the
119+
// memory-planning pasees.
120120
//
121-
// These buffers correspond to different hardware memory banks. Most mobile
122-
// environments will only have a single buffer. Some embedded environments may
123-
// have more than one for, e.g., slow/large DRAM and fast/small SRAM.
121+
// Each buffer typically corresponds to a different hardware memory bank. Most
122+
// mobile environments will only have a single buffer. Some embedded
123+
// environments may have more than one for, e.g., slow/large DRAM and
124+
// fast/small SRAM, or for memory associated with particular cores.
124125
std::vector<std::unique_ptr<uint8_t[]>> non_const_buffers;
125-
std::vector<MemoryAllocator> non_const_allocators;
126+
std::vector<Span<uint8_t>> non_const_spans;
126127
size_t num_non_const_buffers = method_meta->num_non_const_buffers();
127128
for (size_t id = 0; id < num_non_const_buffers; ++id) {
128-
auto buffer_size = method_meta->non_const_buffer_size(id);
129-
ET_CHECK_MSG(
130-
buffer_size.ok(),
131-
"Failed to get size of non-const buffer %zu for method %s: 0x%x",
132-
id,
133-
method_name,
134-
(unsigned int)buffer_size.error());
135-
ET_LOG(
136-
Info, "Setting up non-const buffer %zu, size %zu.", id, *buffer_size);
137-
non_const_buffers.push_back(std::make_unique<uint8_t[]>(*buffer_size));
138-
non_const_allocators.push_back(
139-
MemoryAllocator(*buffer_size, non_const_buffers.back().get()));
140-
non_const_allocators.back().enable_profiling("non_const_allocators");
129+
// .get() will always succeed because id < num_non_const_buffers.
130+
size_t buffer_size =
131+
static_cast<size_t>(method_meta->non_const_buffer_size(id).get());
132+
ET_LOG(Info, "Setting up non-const buffer %zu, size %zu.", id, buffer_size);
133+
non_const_buffers.push_back(std::make_unique<uint8_t[]>(buffer_size));
134+
non_const_spans.push_back({non_const_buffers.back().get(), buffer_size});
141135
}
142136
HierarchicalAllocator non_const_allocator(
143-
non_const_allocators.size(), non_const_allocators.data());
137+
{non_const_spans.data(), non_const_spans.size()});
144138

145139
// The constant allocator is not currently used. Please initialize with a
146140
// zero-sized allocator.

exir/backend/test/demos/rpc/ExecutorBackend.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,24 +79,21 @@ class ExecutorBackend final : public PyTorchBackendInterface {
7979

8080
auto num_non_const_buffers = method_meta->num_non_const_buffers();
8181

82-
uint8_t** non_const_buffers = ET_ALLOCATE_LIST_OR_RETURN_ERROR(
83-
runtime_allocator, uint8_t*, num_non_const_buffers);
84-
MemoryAllocator* non_const_allocators = ET_ALLOCATE_LIST_OR_RETURN_ERROR(
85-
runtime_allocator, MemoryAllocator, num_non_const_buffers);
82+
Span<uint8_t>* non_const_buffers = ET_ALLOCATE_LIST_OR_RETURN_ERROR(
83+
runtime_allocator, Span<uint8_t>, num_non_const_buffers);
8684

8785
for (size_t id = 0; id < num_non_const_buffers; ++id) {
88-
auto buffer_size = method_meta->non_const_buffer_size(id);
86+
size_t buffer_size =
87+
static_cast<size_t>(method_meta->non_const_buffer_size(id).get());
8988
uint8_t* buffer_i = ET_ALLOCATE_LIST_OR_RETURN_ERROR(
90-
runtime_allocator, uint8_t, buffer_size.get());
91-
non_const_buffers[id] = buffer_i;
92-
new (&non_const_allocators[id])
93-
MemoryAllocator(static_cast<uint32_t>(buffer_size.get()), buffer_i);
89+
runtime_allocator, uint8_t, buffer_size);
90+
non_const_buffers[id] = {buffer_i, buffer_size};
9491
}
9592

9693
auto client_non_const_allocator = ET_ALLOCATE_INSTANCE_OR_RETURN_ERROR(
9794
runtime_allocator, HierarchicalAllocator);
9895
new (client_non_const_allocator)
99-
HierarchicalAllocator(num_non_const_buffers, non_const_allocators);
96+
HierarchicalAllocator({non_const_buffers, num_non_const_buffers});
10097

10198
// Allocate some memory from runtime allocator for the client executor, in
10299
// real case, like if it's an executor in dsp, it should allocate memory

sdk/runners/executor_runner.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -264,26 +264,27 @@ int main(int argc, char** argv) {
264264
MemoryAllocator(kRuntimeMemorySize, runtime_pool)};
265265
runtime_allocator.enable_profiling("runtime allocator");
266266

267-
// The non-const allocator is used to provide the memory-planned buffers that
268-
// back mutable tensors. Since it was planned ahead of time, the Program knows
269-
// how big each of the allocators needs to be.
267+
// The non-const buffers will back the mutable tensors used by the method. The
268+
// sizes of these buffers were determined ahead of time during the
269+
// memory-planning pasees.
270270
//
271-
// These buffers correspond to different hardware memory banks. Most mobile
272-
// environments will only have a single buffer. Some embedded environments may
273-
// have more than one for, e.g., slow/large DRAM and fast/small SRAM.
271+
// Each buffer typically corresponds to a different hardware memory bank. Most
272+
// mobile environments will only have a single buffer. Some embedded
273+
// environments may have more than one for, e.g., slow/large DRAM and
274+
// fast/small SRAM, or for memory associated with particular cores.
274275
std::vector<std::unique_ptr<uint8_t[]>> non_const_buffers;
275-
std::vector<MemoryAllocator> non_const_allocators;
276+
std::vector<Span<uint8_t>> non_const_spans;
276277
size_t num_non_const_buffers = method_meta->num_non_const_buffers();
277278
for (size_t id = 0; id < num_non_const_buffers; ++id) {
278-
size_t buffer_size = method_meta->non_const_buffer_size(id).get();
279+
// .get() will always succeed because id < num_non_const_buffers.
280+
size_t buffer_size =
281+
static_cast<size_t>(method_meta->non_const_buffer_size(id).get());
279282
ET_LOG(Info, "Setting up non-const buffer %zu, size %zu.", id, buffer_size);
280283
non_const_buffers.push_back(std::make_unique<uint8_t[]>(buffer_size));
281-
non_const_allocators.push_back(
282-
MemoryAllocator(buffer_size, non_const_buffers.back().get()));
283-
non_const_allocators.back().enable_profiling("non_const_allocators");
284+
non_const_spans.push_back({non_const_buffers.back().get(), buffer_size});
284285
}
285286
HierarchicalAllocator non_const_allocator(
286-
non_const_allocators.size(), non_const_allocators.data());
287+
{non_const_spans.data(), non_const_spans.size()});
287288

288289
// The constant allocator is not currently used. Please initialize with a
289290
// zero-sized allocator.

test/relocatable_runner.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,16 @@ MemoryManager* create_memory_manager(
8989

9090
// Create the non-const allocator and the buffers it points to.
9191
size_t num_non_const_buffers = method_meta->num_non_const_buffers();
92-
MemoryAllocator* non_const_allocators =
93-
worker_allocator.allocateList<MemoryAllocator>(num_non_const_buffers);
92+
Span<uint8_t>* non_const_buffers =
93+
worker_allocator.allocateList<Span<uint8_t>>(num_non_const_buffers);
94+
ET_CHECK(non_const_buffers != nullptr);
9495
for (size_t id = 0; id < num_non_const_buffers; ++id) {
9596
const size_t buffer_size = method_meta->non_const_buffer_size(id).get();
9697
ET_LOG(
9798
Info, "Setting up non-const buffer id %zu, size %zu.", id, buffer_size);
9899
void* buffer = worker_allocator.allocate(buffer_size);
99100
ET_CHECK(buffer != nullptr);
100-
new (&non_const_allocators[id])
101-
MemoryAllocator(buffer_size, (uint8_t*)buffer);
101+
non_const_buffers[id] = {(uint8_t*)buffer, buffer_size};
102102
ET_LOG(
103103
Info,
104104
"Created non_const_allocators with size %zu and addr %p",
@@ -109,7 +109,7 @@ MemoryManager* create_memory_manager(
109109
worker_allocator.allocateInstance<HierarchicalAllocator>();
110110
ET_CHECK(non_const_allocator != nullptr);
111111
new (non_const_allocator)
112-
HierarchicalAllocator(num_non_const_buffers, non_const_allocators);
112+
HierarchicalAllocator({non_const_buffers, num_non_const_buffers});
113113

114114
// The constant allocator is not currently used, but must be provided.
115115
auto* const_allocator = worker_allocator.allocateInstance<MemoryAllocator>();

test/size_test.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,8 @@ int main(int argc, char** argv) {
3939
MemoryAllocator temp_allocator{MemoryAllocator(0, nullptr)};
4040
temp_allocator.enable_profiling("temp allocator");
4141

42-
MemoryAllocator non_const_allocators[1]{
43-
MemoryAllocator(kMemoryAmount, activation_pool)};
44-
non_const_allocators[0].enable_profiling("non_const_allocators");
45-
46-
HierarchicalAllocator non_const_allocator{
47-
HierarchicalAllocator(1, non_const_allocators)};
42+
Span<uint8_t> non_const_buffers[1]{{activation_pool, kMemoryAmount}};
43+
HierarchicalAllocator non_const_allocator({non_const_buffers, 1});
4844

4945
MemoryManager memory_manager{MemoryManager(
5046
&const_allocator,

0 commit comments

Comments
 (0)