Skip to content

Commit ec4228c

Browse files
authored
Arm backend: Extract code to helpers in executor runner (#13039)
To improve readability, extract blocks of code from the main function to new helper functions. Co-authored-by: [email protected] Signed-off-by: Martin Lindström <[email protected]>
1 parent 6c71c5e commit ec4228c

File tree

1 file changed

+119
-107
lines changed

1 file changed

+119
-107
lines changed

examples/arm/executor_runner/arm_executor_runner.cpp

Lines changed: 119 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -433,9 +433,10 @@ struct RunnerContext {
433433
#if defined(ET_EVENT_TRACER_ENABLED)
434434
Box<torch::executor::ETDumpGen> etdump_gen;
435435
#endif
436-
437-
/// Runs the loaded method and returns the status
438-
Error run();
436+
#if defined(SEMIHOSTING)
437+
Box<ArmMemoryAllocator> input_file_allocator;
438+
const char* output_basename = nullptr;
439+
#endif
439440
};
440441

441442
void runner_init(
@@ -637,124 +638,37 @@ void runner_init(
637638
ET_LOG(Info, "Input prepared.");
638639
}
639640

640-
Error RunnerContext::run() {
641+
void run_model(RunnerContext& ctx) {
641642
ET_LOG(Info, "Starting the model execution...");
642643

643644
StartMeasurements();
644645
// Run the model.
645-
Error status = method.value()->execute();
646+
Error status = ctx.method.value()->execute();
646647
StopMeasurements();
647648

648-
return status;
649+
ET_CHECK_MSG(
650+
status == Error::Ok,
651+
"Execution of method %s failed with status 0x%" PRIx32,
652+
ctx.method_name,
653+
status);
649654
}
650655

651-
} // namespace
652-
653-
int main(int argc, const char* argv[]) {
654-
#if defined(SEMIHOSTING)
655-
ET_LOG(Info, "Running executor with parameter:");
656-
if (argc < 7) {
657-
ET_LOG(Fatal, "Not right number of parameters!");
658-
ET_LOG(
659-
Fatal,
660-
"app -m model.pte -i input.bin [-i input2.bin] -o output_basename");
661-
ET_LOG(Fatal, "Exiting!");
662-
_exit(1);
663-
}
664-
ET_LOG(Info, " %s", argv[0]);
665-
for (int i = 1; i < argc; i++) {
666-
ET_LOG(Info, " %s %s", argv[i], argv[++i]);
667-
}
668-
#else
669-
(void)argc;
670-
(void)argv;
671-
#endif
672-
673-
executorch::runtime::runtime_init();
674-
std::vector<std::pair<char*, size_t>> input_buffers;
675-
size_t pte_size = sizeof(model_pte);
676-
677-
#if defined(SEMIHOSTING)
678-
const char* output_basename = nullptr;
679-
ArmMemoryAllocator input_file_allocator(
680-
input_file_allocation_pool_size, input_file_allocation_pool);
681-
682-
/* parse input parameters */
683-
for (int i = 0; i < argc; i++) {
684-
size_t nbr_inputs = 0;
685-
if (std::strcmp(argv[i], "-i") == 0) {
686-
// input file, read the data into memory
687-
const char* input_tensor_filename = argv[++i];
688-
ET_LOG(
689-
Info,
690-
"Reading input tensor %d from file %s",
691-
++nbr_inputs,
692-
input_tensor_filename);
693-
auto [buffer, buffer_size] =
694-
read_binary_file(input_tensor_filename, input_file_allocator);
695-
if (buffer == nullptr) {
696-
ET_LOG(
697-
Error,
698-
"Reading input tensor %d from file %s ERROR Out of memory",
699-
nbr_inputs,
700-
input_tensor_filename);
701-
_exit(1);
702-
}
703-
input_buffers.push_back(std::make_pair(buffer, buffer_size));
704-
} else if (std::strcmp(argv[i], "-m") == 0) {
705-
const char* pte_filename = argv[++i];
706-
ET_LOG(Info, "Reading pte model from file %s", pte_filename);
707-
auto [buffer, buffer_size] =
708-
read_binary_file(pte_filename, input_file_allocator);
709-
if (buffer == nullptr) {
710-
ET_LOG(
711-
Error,
712-
"Reading pte model from file %s ERROR Out of memory",
713-
pte_filename);
714-
_exit(1);
715-
}
716-
717-
// Store the model data with the same variable as if it was loaded
718-
// from compiled in location.
719-
model_pte = buffer;
720-
pte_size = buffer_size;
721-
} else if (std::strcmp(argv[i], "-o") == 0) {
722-
// store the base filename to write output to.
723-
output_basename = argv[++i];
724-
}
725-
}
726-
#endif
727-
ET_LOG(
728-
Info, "PTE in %p %c Size: %lu bytes", model_pte, model_pte[0], pte_size);
729-
730-
RunnerContext ctx;
731-
runner_init(ctx, input_buffers, pte_size);
732-
733-
Error status = ctx.run();
734-
if (status != Error::Ok) {
735-
ET_LOG(
736-
Info,
737-
"Execution of method %s failed with status 0x%" PRIx32,
738-
ctx.method_name,
739-
status);
740-
} else {
741-
ET_LOG(Info, "Model executed successfully.");
742-
}
743-
656+
void log_mem_status(const RunnerContext& ctx) {
744657
size_t executor_memsize =
745658
ctx.method_allocator->used_size() - ctx.executor_membase;
746659

747660
ET_LOG(Info, "model_pte_program_size: %lu bytes.", ctx.program_data_len);
748661
ET_LOG(Info, "model_pte_loaded_size: %lu bytes.", ctx.pte_size);
749662
#if defined(SEMIHOSTING)
750-
if (input_file_allocator.size() > 0) {
663+
if (ctx.input_file_allocator->size() > 0) {
751664
ET_LOG(
752665
Info,
753666
"input_file_allocator_used: %zu / %zu free: %zu ( used: %zu %% ) ",
754-
input_file_allocator.used_size(),
755-
input_file_allocator.size(),
756-
input_file_allocator.free_size(),
757-
100 * input_file_allocator.used_size() / input_file_allocator.size());
667+
ctx.input_file_allocator->used_size(),
668+
ctx.input_file_allocator->size(),
669+
ctx.input_file_allocator->free_size(),
670+
100 * ctx.input_file_allocator->used_size() /
671+
ctx.input_file_allocator->size());
758672
}
759673
#endif
760674
if (ctx.method_allocator->size() != 0) {
@@ -786,10 +700,13 @@ int main(int argc, const char* argv[]) {
786700
ctx.temp_allocator->free_size(),
787701
100 * ctx.temp_allocator->peak_used() / ctx.temp_allocator->size());
788702
}
703+
}
789704

705+
void print_outputs(RunnerContext& ctx) {
790706
std::vector<EValue> outputs(ctx.method.value()->outputs_size());
791707
ET_LOG(Info, "%zu outputs: ", outputs.size());
792-
status = ctx.method.value()->get_outputs(outputs.data(), outputs.size());
708+
Error status =
709+
ctx.method.value()->get_outputs(outputs.data(), outputs.size());
793710
ET_CHECK(status == Error::Ok);
794711

795712
// Print the outputs.
@@ -831,7 +748,7 @@ int main(int argc, const char* argv[]) {
831748
#endif
832749
#else
833750
char out_filename[255];
834-
snprintf(out_filename, 255, "%s-%d.bin", output_basename, i);
751+
snprintf(out_filename, 255, "%s-%d.bin", ctx.output_basename, i);
835752
ET_LOG(Info, "Writing output to file: %s", out_filename);
836753
FILE* out_file = fopen(out_filename, "wb");
837754
auto written_size =
@@ -842,7 +759,9 @@ int main(int argc, const char* argv[]) {
842759
printf("Output[%d]: Not Tensor\n", i);
843760
}
844761
}
762+
}
845763

764+
void write_etdump(RunnerContext& ctx) {
846765
#if defined(ET_EVENT_TRACER_ENABLED)
847766
#if !defined(SEMIHOSTING)
848767
// Dump the etdump data containing profiling/debugging data to the serial line
@@ -887,7 +806,9 @@ int main(int argc, const char* argv[]) {
887806
}
888807
#endif
889808
#endif
809+
}
890810

811+
void verify_result(RunnerContext& ctx, const void* model_pte) {
891812
#if defined(ET_BUNDLE_IO)
892813
if (ctx.bundle_io) {
893814
// Check result
@@ -908,7 +829,7 @@ int main(int argc, const char* argv[]) {
908829
}
909830

910831
// Verify the result.
911-
status = verify_method_outputs(
832+
Error status = verify_method_outputs(
912833
*ctx.method.value(), model_pte, testset_idx, et_rtol, et_atol);
913834
if (status == Error::Ok) {
914835
ET_LOG(Info, "Model output match expected BundleIO bpte ref data.");
@@ -926,7 +847,98 @@ int main(int argc, const char* argv[]) {
926847
"Bundle verification failed with status 0x%" PRIx32,
927848
status);
928849
}
850+
#else
851+
(void)ctx;
852+
(void)model_pte;
929853
#endif
854+
}
855+
856+
} // namespace
857+
858+
int main(int argc, const char* argv[]) {
859+
#if defined(SEMIHOSTING)
860+
ET_LOG(Info, "Running executor with parameter:");
861+
if (argc < 7) {
862+
ET_LOG(Fatal, "Not right number of parameters!");
863+
ET_LOG(
864+
Fatal,
865+
"app -m model.pte -i input.bin [-i input2.bin] -o output_basename");
866+
ET_LOG(Fatal, "Exiting!");
867+
_exit(1);
868+
}
869+
ET_LOG(Info, " %s", argv[0]);
870+
for (int i = 1; i < argc; i++) {
871+
ET_LOG(Info, " %s %s", argv[i], argv[++i]);
872+
}
873+
#else
874+
(void)argc;
875+
(void)argv;
876+
#endif
877+
878+
executorch::runtime::runtime_init();
879+
std::vector<std::pair<char*, size_t>> input_buffers;
880+
size_t pte_size = sizeof(model_pte);
881+
882+
RunnerContext ctx;
883+
884+
#if defined(SEMIHOSTING)
885+
ctx.input_file_allocator.reset(
886+
input_file_allocation_pool_size, input_file_allocation_pool);
887+
888+
/* parse input parameters */
889+
for (int i = 0; i < argc; i++) {
890+
size_t nbr_inputs = 0;
891+
if (std::strcmp(argv[i], "-i") == 0) {
892+
// input file, read the data into memory
893+
const char* input_tensor_filename = argv[++i];
894+
ET_LOG(
895+
Info,
896+
"Reading input tensor %d from file %s",
897+
++nbr_inputs,
898+
input_tensor_filename);
899+
auto [buffer, buffer_size] = read_binary_file(
900+
input_tensor_filename, ctx.input_file_allocator.value());
901+
if (buffer == nullptr) {
902+
ET_LOG(
903+
Error,
904+
"Reading input tensor %d from file %s ERROR Out of memory",
905+
nbr_inputs,
906+
input_tensor_filename);
907+
_exit(1);
908+
}
909+
input_buffers.push_back(std::make_pair(buffer, buffer_size));
910+
} else if (std::strcmp(argv[i], "-m") == 0) {
911+
const char* pte_filename = argv[++i];
912+
ET_LOG(Info, "Reading pte model from file %s", pte_filename);
913+
auto [buffer, buffer_size] =
914+
read_binary_file(pte_filename, ctx.input_file_allocator.value());
915+
if (buffer == nullptr) {
916+
ET_LOG(
917+
Error,
918+
"Reading pte model from file %s ERROR Out of memory",
919+
pte_filename);
920+
_exit(1);
921+
}
922+
923+
// Store the model data with the same variable as if it was loaded
924+
// from compiled in location.
925+
model_pte = buffer;
926+
pte_size = buffer_size;
927+
} else if (std::strcmp(argv[i], "-o") == 0) {
928+
// store the base filename to write output to.
929+
ctx.output_basename = argv[++i];
930+
}
931+
}
932+
#endif
933+
ET_LOG(
934+
Info, "PTE in %p %c Size: %lu bytes", model_pte, model_pte[0], pte_size);
935+
936+
runner_init(ctx, input_buffers, pte_size);
937+
run_model(ctx);
938+
log_mem_status(ctx);
939+
print_outputs(ctx);
940+
write_etdump(ctx);
941+
verify_result(ctx, model_pte);
930942

931943
ET_LOG(Info, "Program complete, exiting.");
932944
#if defined(SEMIHOSTING)

0 commit comments

Comments
 (0)