@@ -29,10 +29,10 @@ using namespace torch::executor;
29
29
* power down and then back up) in between two inference requests.
30
30
*
31
31
* 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.
36
36
*
37
37
* This tool demonstrates that the memory can be managed this way.
38
38
*/
@@ -79,8 +79,7 @@ Program* load_program(
79
79
}
80
80
81
81
MemoryManager* create_memory_manager (
82
- Program* program,
83
- const char * method_name,
82
+ MethodMeta* method_meta,
84
83
MemoryAllocator& worker_allocator) {
85
84
// Create the runtime allocator.
86
85
auto * runtime_allocator =
@@ -89,18 +88,16 @@ MemoryManager* create_memory_manager(
89
88
new (runtime_allocator) MemoryAllocator (sizeof (runtime_pool), runtime_pool);
90
89
91
90
// 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 ();
94
92
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 ();
99
96
ET_LOG (
100
97
Info, " Setting up non-const buffer id %zu, size %zu." , id, buffer_size);
101
98
void * buffer = worker_allocator.allocate (buffer_size);
102
99
ET_CHECK (buffer != nullptr );
103
- new (&non_const_allocators[id - 1 ])
100
+ new (&non_const_allocators[id])
104
101
MemoryAllocator (buffer_size, (uint8_t *)buffer);
105
102
ET_LOG (
106
103
Info,
@@ -112,7 +109,7 @@ MemoryManager* create_memory_manager(
112
109
worker_allocator.allocateInstance <HierarchicalAllocator>();
113
110
ET_CHECK (non_const_allocator != nullptr );
114
111
new (non_const_allocator)
115
- HierarchicalAllocator (num_non_const_buffers - 1 , non_const_allocators);
112
+ HierarchicalAllocator (num_non_const_buffers, non_const_allocators);
116
113
117
114
// The constant allocator is not currently used, but must be provided.
118
115
auto * const_allocator = worker_allocator.allocateInstance <MemoryAllocator>();
@@ -140,8 +137,11 @@ Method* init_method(
140
137
MemoryAllocator& worker_allocator,
141
138
std::vector<size_t >& input_sizes,
142
139
std::vector<size_t >& output_sizes) {
140
+ Result<MethodMeta> method_meta = program->method_meta (method_name);
141
+ ET_CHECK (method_meta.ok ());
142
+
143
143
MemoryManager* memory_manager =
144
- create_memory_manager (program, method_name , worker_allocator);
144
+ create_memory_manager (&method_meta. get () , worker_allocator);
145
145
146
146
//
147
147
// Create and load a method from the program, using the provided
@@ -227,7 +227,7 @@ void inference_loop(
227
227
Error status = method->execute ();
228
228
ET_CHECK_MSG (
229
229
status == Error::Ok,
230
- " plan ->execute() failed with status 0x%" PRIx32,
230
+ " method ->execute() failed with status 0x%" PRIx32,
231
231
status);
232
232
ET_LOG (Info, " Model executed successfully." );
233
233
}
@@ -285,8 +285,7 @@ int main(int argc, char** argv) {
285
285
const char * method_name = nullptr ;
286
286
{
287
287
// 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 );
290
289
ET_CHECK_MSG (method_name_result.ok (), " Program has no methods" );
291
290
method_name = *method_name_result;
292
291
}
0 commit comments