1313#include < executorch/runtime/platform/log.h>
1414#include < executorch/runtime/platform/runtime.h>
1515
16+ // Define a fixed-size memory pool for the method allocator (4 MB)
1617static uint8_t method_allocator_pool[4 * 1024U * 1024U ]; // 4 MB
1718
19+ // Define command-line flags for model path and the number of iterations
1820DEFINE_string (
1921 model_path,
20- " /home/icx-6338/ynimmaga/delegate.pte" , // "model.pte",
21- " Model serialized in flatbuffer format." );
22- DEFINE_int32 (iteration, 1 , " Iterations of inference." );
22+ " " ,
23+ " Path to the model serialized in flatbuffer format (required)." );
24+ DEFINE_int32 (
25+ num_iter,
26+ 1 ,
27+ " Number of inference iterations (default is 1)." );
2328
2429using executorch::extension::FileDataLoader;
2530using executorch::extension::prepare_input_tensors;
@@ -35,32 +40,42 @@ using executorch::runtime::Result;
3540using executorch::runtime::Span;
3641
3742int main (int argc, char ** argv) {
43+ // Initialize the runtime environment
3844 executorch::runtime::runtime_init ();
3945
46+ // Parse command-line arguments and flags
4047 gflags::ParseCommandLineFlags (&argc, &argv, true );
41- if (argc != 1 ) {
42- std::string msg = " Extra commandline args: " ;
43- for ( int i = 1 ; i < argc; i++ ) {
44- msg += " " + std::string (argv[i]) ;
45- }
46- ET_LOG (Error, " %s " , msg. c_str ()) ;
48+
49+ // Check if the model path is provided
50+ if (FLAGS_model_path. empty () ) {
51+ std::cerr << " Error: --model_path is required. " << std::endl ;
52+ std::cerr << " Usage: " << argv[ 0 ]
53+ << " --model_path=<path_to_model> --num_iter=<iterations> " << std::endl ;
4754 return 1 ;
4855 }
4956
57+ // Retrieve the model path and number of iterations
5058 const char * model_path = FLAGS_model_path.c_str ();
59+ int num_iterations = FLAGS_num_iter;
60+ std::cout << " Model path: " << model_path << std::endl;
61+ std::cout << " Number of iterations: " << num_iterations << std::endl;
62+
63+ // Load the model using FileDataLoader
5164 Result<FileDataLoader> loader = FileDataLoader::from (model_path);
5265 ET_CHECK_MSG (
5366 loader.ok (),
5467 " FileDataLoader::from() failed: 0x%" PRIx32,
5568 static_cast <uint32_t >(loader.error ()));
5669
70+ // Load the program from the loaded model
5771 Result<Program> program = Program::load (&loader.get ());
5872 if (!program.ok ()) {
5973 ET_LOG (Error, " Failed to parse model file %s" , model_path);
6074 return 1 ;
6175 }
6276 ET_LOG (Info, " Model file %s is loaded." , model_path);
6377
78+ // Retrieve the method name from the program (assumes the first method is used)
6479 const char * method_name = nullptr ;
6580 {
6681 const auto method_name_result = program->get_method_name (0 );
@@ -69,16 +84,19 @@ int main(int argc, char** argv) {
6984 }
7085 ET_LOG (Info, " Using method %s" , method_name);
7186
87+ // Retrieve metadata about the method
7288 Result<MethodMeta> method_meta = program->method_meta (method_name);
7389 ET_CHECK_MSG (
7490 method_meta.ok (),
7591 " Failed to get method_meta for %s: 0x%" PRIx32,
7692 method_name,
7793 static_cast <uint32_t >(method_meta.error ()));
7894
95+ // Set up a memory allocator for the method
7996 MemoryAllocator method_allocator{
8097 MemoryAllocator (sizeof (method_allocator_pool), method_allocator_pool)};
8198
99+ // Prepare planned buffers for memory planning
82100 std::vector<std::unique_ptr<uint8_t []>> planned_buffers;
83101 std::vector<Span<uint8_t >> planned_spans;
84102 size_t num_memory_planned_buffers = method_meta->num_memory_planned_buffers ();
@@ -92,8 +110,10 @@ int main(int argc, char** argv) {
92110 HierarchicalAllocator planned_memory (
93111 {planned_spans.data (), planned_spans.size ()});
94112
113+ // Set up a memory manager using the method allocator and planned memory
95114 MemoryManager memory_manager (&method_allocator, &planned_memory);
96115
116+ // Load the method into the program
97117 Result<Method> method = program->load_method (method_name, &memory_manager);
98118 ET_CHECK_MSG (
99119 method.ok (),
@@ -102,44 +122,45 @@ int main(int argc, char** argv) {
102122 static_cast <uint32_t >(method.error ()));
103123 ET_LOG (Info, " Method loaded." );
104124
125+ // Prepare the input tensors for the method
105126 auto inputs = prepare_input_tensors (*method);
106127 ET_CHECK_MSG (
107128 inputs.ok (),
108129 " Could not prepare inputs: 0x%" PRIx32,
109130 static_cast <uint32_t >(inputs.error ()));
110131 ET_LOG (Info, " Inputs prepared." );
111132
133+ // Measure execution time for inference
112134 auto before_exec = std::chrono::high_resolution_clock::now ();
113135 Error status = Error::Ok;
114- for (int i = 0 ; i < FLAGS_iteration ; ++i) {
136+ for (int i = 0 ; i < num_iterations ; ++i) {
115137 status = method->execute ();
116138 }
117139 auto after_exec = std::chrono::high_resolution_clock::now ();
118140 double elapsed_time = std::chrono::duration_cast<std::chrono::microseconds>(
119141 after_exec - before_exec)
120142 .count () / 1000.0 ;
121143
144+ // Log execution time and average time per iteration
122145 ET_LOG (
123146 Info,
124147 " %d inference took %f ms, avg %f ms" ,
125- FLAGS_iteration ,
148+ num_iterations ,
126149 elapsed_time,
127- elapsed_time / static_cast <float >(FLAGS_iteration ));
150+ elapsed_time / static_cast <float >(num_iterations ));
128151 ET_CHECK_MSG (
129152 status == Error::Ok,
130153 " Execution of method %s failed with status 0x%" PRIx32,
131154 method_name,
132155 static_cast <uint32_t >(status));
133156 ET_LOG (Info, " Model executed successfully." );
134157
158+ // Retrieve and print the method outputs
135159 std::vector<EValue> outputs (method->outputs_size ());
136- ET_LOG (Info, " %zu outputs: " , outputs.size ());
160+ ET_LOG (Info, " %zu Number of outputs: " , outputs.size ());
137161 status = method->get_outputs (outputs.data (), outputs.size ());
138162 ET_CHECK (status == Error::Ok);
139- // std::cout << executorch::extension::evalue_edge_items(100);
140- // for (int i = 0; i < outputs.size(); ++i) {
141- // std::cout << "Output " << i << ": " << outputs[i] << std::endl;
142- // }
143163
144164 return 0 ;
145165}
166+
0 commit comments