Skip to content

Commit f6f3f00

Browse files
dbortfacebook-github-bot
authored andcommitted
Migrate relocatable_runner to use MethodMeta (#316)
Summary: Pull Request resolved: #316 Make this use the new APIs. Note that MethodMeta::num_non_const_buffers is zero-based, so we can avoid the hacks that skip index zero. Reviewed By: JacobSzwejbka Differential Revision: D48762565 fbshipit-source-id: fcb70ca0d85eb6c37438c31278d348fc996decdc
1 parent 5762802 commit f6f3f00

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

test/relocatable_runner.cpp

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ using namespace torch::executor;
2929
* power down and then back up) in between two inference requests.
3030
*
3131
* For ExecuTorch to work efficiently in these environments, we want to
32-
* initialize the execution plan once once for the model and avoid
33-
* re-initializing it for every inference. This can be achieved by restricting
34-
* the runtime contexts (torch::executor::Program and torch::executor::Method)
35-
* to live in a pre-allocated, shared, and persistent memory.
32+
* initialize the Method once once for the model and avoid re-initializing it
33+
* for every inference. This can be achieved by restricting the runtime contexts
34+
* (torch::executor::Program and torch::executor::Method) to live in a
35+
* pre-allocated, shared, and persistent memory.
3636
*
3737
* This tool demonstrates that the memory can be managed this way.
3838
*/
@@ -79,8 +79,7 @@ Program* load_program(
7979
}
8080

8181
MemoryManager* create_memory_manager(
82-
Program* program,
83-
const char* method_name,
82+
MethodMeta* method_meta,
8483
MemoryAllocator& worker_allocator) {
8584
// Create the runtime allocator.
8685
auto* runtime_allocator =
@@ -89,18 +88,16 @@ MemoryManager* create_memory_manager(
8988
new (runtime_allocator) MemoryAllocator(sizeof(runtime_pool), runtime_pool);
9089

9190
// Create the non-const allocator and the buffers it points to.
92-
size_t num_non_const_buffers =
93-
program->num_non_const_buffers(method_name).get();
91+
size_t num_non_const_buffers = method_meta->num_non_const_buffers();
9492
MemoryAllocator* non_const_allocators =
95-
worker_allocator.allocateList<MemoryAllocator>(num_non_const_buffers - 1);
96-
for (size_t id = 1; id < num_non_const_buffers; ++id) {
97-
const size_t buffer_size =
98-
program->get_non_const_buffer_size(id, method_name).get();
93+
worker_allocator.allocateList<MemoryAllocator>(num_non_const_buffers);
94+
for (size_t id = 0; id < num_non_const_buffers; ++id) {
95+
const size_t buffer_size = method_meta->non_const_buffer_size(id).get();
9996
ET_LOG(
10097
Info, "Setting up non-const buffer id %zu, size %zu.", id, buffer_size);
10198
void* buffer = worker_allocator.allocate(buffer_size);
10299
ET_CHECK(buffer != nullptr);
103-
new (&non_const_allocators[id - 1])
100+
new (&non_const_allocators[id])
104101
MemoryAllocator(buffer_size, (uint8_t*)buffer);
105102
ET_LOG(
106103
Info,
@@ -112,7 +109,7 @@ MemoryManager* create_memory_manager(
112109
worker_allocator.allocateInstance<HierarchicalAllocator>();
113110
ET_CHECK(non_const_allocator != nullptr);
114111
new (non_const_allocator)
115-
HierarchicalAllocator(num_non_const_buffers - 1, non_const_allocators);
112+
HierarchicalAllocator(num_non_const_buffers, non_const_allocators);
116113

117114
// The constant allocator is not currently used, but must be provided.
118115
auto* const_allocator = worker_allocator.allocateInstance<MemoryAllocator>();
@@ -140,8 +137,11 @@ Method* init_method(
140137
MemoryAllocator& worker_allocator,
141138
std::vector<size_t>& input_sizes,
142139
std::vector<size_t>& output_sizes) {
140+
Result<MethodMeta> method_meta = program->method_meta(method_name);
141+
ET_CHECK(method_meta.ok());
142+
143143
MemoryManager* memory_manager =
144-
create_memory_manager(program, method_name, worker_allocator);
144+
create_memory_manager(&method_meta.get(), worker_allocator);
145145

146146
//
147147
// Create and load a method from the program, using the provided
@@ -227,7 +227,7 @@ void inference_loop(
227227
Error status = method->execute();
228228
ET_CHECK_MSG(
229229
status == Error::Ok,
230-
"plan->execute() failed with status 0x%" PRIx32,
230+
"method->execute() failed with status 0x%" PRIx32,
231231
status);
232232
ET_LOG(Info, "Model executed successfully.");
233233
}
@@ -285,8 +285,7 @@ int main(int argc, char** argv) {
285285
const char* method_name = nullptr;
286286
{
287287
// Use the first method in the program.
288-
const size_t plan_index = 0;
289-
const auto method_name_result = program->get_method_name(plan_index);
288+
const auto method_name_result = program->get_method_name(0);
290289
ET_CHECK_MSG(method_name_result.ok(), "Program has no methods");
291290
method_name = *method_name_result;
292291
}

0 commit comments

Comments
 (0)