From 29d840062a289df87101631f06e5e22cf7ab223b Mon Sep 17 00:00:00 2001 From: ynimmaga Date: Wed, 29 Jan 2025 17:03:43 -0800 Subject: [PATCH] Added arguments for model path and num iters to openvino example --- .../openvino_executor_runner.cpp | 55 +++++++++++++------ 1 file changed, 38 insertions(+), 17 deletions(-) diff --git a/examples/openvino/executor_runner/openvino_executor_runner.cpp b/examples/openvino/executor_runner/openvino_executor_runner.cpp index 86b975fe007..67bb35d9701 100644 --- a/examples/openvino/executor_runner/openvino_executor_runner.cpp +++ b/examples/openvino/executor_runner/openvino_executor_runner.cpp @@ -13,13 +13,18 @@ #include #include +// Define a fixed-size memory pool for the method allocator (4 MB) static uint8_t method_allocator_pool[4 * 1024U * 1024U]; // 4 MB +// Define command-line flags for model path and the number of iterations DEFINE_string( model_path, - "/home/icx-6338/ynimmaga/delegate.pte", //"model.pte", - "Model serialized in flatbuffer format."); -DEFINE_int32(iteration, 1, "Iterations of inference."); + "", + "Path to the model serialized in flatbuffer format (required)."); +DEFINE_int32( + num_iter, + 1, + "Number of inference iterations (default is 1)."); using executorch::extension::FileDataLoader; using executorch::extension::prepare_input_tensors; @@ -35,25 +40,34 @@ using executorch::runtime::Result; using executorch::runtime::Span; int main(int argc, char** argv) { + // Initialize the runtime environment executorch::runtime::runtime_init(); + // Parse command-line arguments and flags gflags::ParseCommandLineFlags(&argc, &argv, true); - if (argc != 1) { - std::string msg = "Extra commandline args:"; - for (int i = 1; i < argc; i++) { - msg += " " + std::string(argv[i]); - } - ET_LOG(Error, "%s", msg.c_str()); + + // Check if the model path is provided + if (FLAGS_model_path.empty()) { + std::cerr << "Error: --model_path is required." << std::endl; + std::cerr << "Usage: " << argv[0] + << " --model_path= --num_iter=" << std::endl; return 1; } + // Retrieve the model path and number of iterations const char* model_path = FLAGS_model_path.c_str(); + int num_iterations = FLAGS_num_iter; + std::cout << "Model path: " << model_path << std::endl; + std::cout << "Number of iterations: " << num_iterations << std::endl; + + // Load the model using FileDataLoader Result loader = FileDataLoader::from(model_path); ET_CHECK_MSG( loader.ok(), "FileDataLoader::from() failed: 0x%" PRIx32, static_cast(loader.error())); + // Load the program from the loaded model Result program = Program::load(&loader.get()); if (!program.ok()) { ET_LOG(Error, "Failed to parse model file %s", model_path); @@ -61,6 +75,7 @@ int main(int argc, char** argv) { } ET_LOG(Info, "Model file %s is loaded.", model_path); + // Retrieve the method name from the program (assumes the first method is used) const char* method_name = nullptr; { const auto method_name_result = program->get_method_name(0); @@ -69,6 +84,7 @@ int main(int argc, char** argv) { } ET_LOG(Info, "Using method %s", method_name); + // Retrieve metadata about the method Result method_meta = program->method_meta(method_name); ET_CHECK_MSG( method_meta.ok(), @@ -76,9 +92,11 @@ int main(int argc, char** argv) { method_name, static_cast(method_meta.error())); + // Set up a memory allocator for the method MemoryAllocator method_allocator{ MemoryAllocator(sizeof(method_allocator_pool), method_allocator_pool)}; + // Prepare planned buffers for memory planning std::vector> planned_buffers; std::vector> planned_spans; size_t num_memory_planned_buffers = method_meta->num_memory_planned_buffers(); @@ -92,8 +110,10 @@ int main(int argc, char** argv) { HierarchicalAllocator planned_memory( {planned_spans.data(), planned_spans.size()}); + // Set up a memory manager using the method allocator and planned memory MemoryManager memory_manager(&method_allocator, &planned_memory); + // Load the method into the program Result method = program->load_method(method_name, &memory_manager); ET_CHECK_MSG( method.ok(), @@ -102,6 +122,7 @@ int main(int argc, char** argv) { static_cast(method.error())); ET_LOG(Info, "Method loaded."); + // Prepare the input tensors for the method auto inputs = prepare_input_tensors(*method); ET_CHECK_MSG( inputs.ok(), @@ -109,9 +130,10 @@ int main(int argc, char** argv) { static_cast(inputs.error())); ET_LOG(Info, "Inputs prepared."); + // Measure execution time for inference auto before_exec = std::chrono::high_resolution_clock::now(); Error status = Error::Ok; - for (int i = 0; i < FLAGS_iteration; ++i) { + for (int i = 0; i < num_iterations; ++i) { status = method->execute(); } auto after_exec = std::chrono::high_resolution_clock::now(); @@ -119,12 +141,13 @@ int main(int argc, char** argv) { after_exec - before_exec) .count() / 1000.0; + // Log execution time and average time per iteration ET_LOG( Info, "%d inference took %f ms, avg %f ms", - FLAGS_iteration, + num_iterations, elapsed_time, - elapsed_time / static_cast(FLAGS_iteration)); + elapsed_time / static_cast(num_iterations)); ET_CHECK_MSG( status == Error::Ok, "Execution of method %s failed with status 0x%" PRIx32, @@ -132,14 +155,12 @@ int main(int argc, char** argv) { static_cast(status)); ET_LOG(Info, "Model executed successfully."); + // Retrieve and print the method outputs std::vector outputs(method->outputs_size()); - ET_LOG(Info, "%zu outputs: ", outputs.size()); + ET_LOG(Info, "%zu Number of outputs: ", outputs.size()); status = method->get_outputs(outputs.data(), outputs.size()); ET_CHECK(status == Error::Ok); - //std::cout << executorch::extension::evalue_edge_items(100); - //for (int i = 0; i < outputs.size(); ++i) { - // std::cout << "Output " << i << ": " << outputs[i] << std::endl; - //} return 0; } +