|
| 1 | +/* Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + * All rights reserved. |
| 3 | + * Copyright 2023-2025 Arm Limited and/or its affiliates. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#include "simple_addmodule_pte.h" |
| 10 | + |
| 11 | +#include "pico/stdio_usb.h" |
| 12 | +#include "pico/stdlib.h" |
| 13 | +#include <executorch/extension/data_loader/buffer_data_loader.h> |
| 14 | +#include <executorch/runtime/core/portable_type/scalar_type.h> |
| 15 | +#include <executorch/runtime/executor/method.h> |
| 16 | +#include <executorch/runtime/executor/program.h> |
| 17 | +#include <executorch/runtime/platform/runtime.h> |
| 18 | + |
| 19 | +void wait_for_usb() { |
| 20 | + const int kMaxRetryCount = 10; |
| 21 | + int retry_usb_count = 0; |
| 22 | + while (!stdio_usb_connected() && retry_usb_count++ < kMaxRetryCount) { |
| 23 | + printf("Retry again! USB not connected \n"); |
| 24 | + sleep_ms(100); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +bool load_and_prepare_model(Program& program, Method& method, MemoryManager& memory_manager) { |
| 29 | + executorch::extension::BufferDataLoader loader(model_pte, model_pte_len); |
| 30 | + auto program_result = Program::load(&loader); |
| 31 | + if (!program_result.ok()) { |
| 32 | + printf("Failed to load model: %d\n", (int)program_result.error()); |
| 33 | + blink_indicator(INDICATOR_PIN_1, 10); |
| 34 | + return false; |
| 35 | + } |
| 36 | + program = std::move(*program_result); |
| 37 | + |
| 38 | + auto method_name_result = program.get_method_name(0); |
| 39 | + if (!method_name_result.ok()) { |
| 40 | + printf("Failed to get method name: %d\n", (int)method_name_result.error()); |
| 41 | + blink_indicator(INDICATOR_PIN_1, 10); |
| 42 | + return false; |
| 43 | + } |
| 44 | + auto method_result = program.load_method(*method_name_result, &memory_manager); |
| 45 | + if (!method_result.ok()) { |
| 46 | + printf("Failed to load method: %d\n", (int)method_result.error()); |
| 47 | + blink_indicator(INDICATOR_PIN_1, 10); |
| 48 | + return false; |
| 49 | + } |
| 50 | + method = std::move(*method_result); |
| 51 | + printf("Method loaded [%s]\n", *method_name_result); |
| 52 | + return true; |
| 53 | +} |
| 54 | + |
| 55 | +bool run_inference(Method& method) { |
| 56 | + float input_data_0[4] = {4.0, 109.0, 13.0, 123.0}; |
| 57 | + float input_data_1[4] = {9.0, 27.0, 11.0, 8.0}; |
| 58 | + TensorImpl::SizesType sizes[1] = {4}; |
| 59 | + TensorImpl::DimOrderType dim_order[] = {0}; |
| 60 | + TensorImpl impl0(ScalarType::Float, 1, sizes, input_data_0, dim_order); |
| 61 | + TensorImpl impl1(ScalarType::Float, 1, sizes, input_data_1, dim_order); |
| 62 | + Tensor input0(&impl0); |
| 63 | + Tensor input1(&impl1); |
| 64 | + |
| 65 | + if (method.set_input(input0, 0) != Error::Ok || method.set_input(input1, 1) != Error::Ok) { |
| 66 | + printf("Failed to set input(s)\n"); |
| 67 | + blink_indicator(INDICATOR_PIN_1, 10); |
| 68 | + return false; |
| 69 | + } |
| 70 | + if (method.execute() != Error::Ok) { |
| 71 | + printf("Failed to execute\n"); |
| 72 | + blink_indicator(INDICATOR_PIN_1, 10); |
| 73 | + return false; |
| 74 | + } |
| 75 | + const EValue& output = method.get_output(0); |
| 76 | + if (output.isTensor()) { |
| 77 | + const float* out_data = output.toTensor().const_data_ptr<float>(); |
| 78 | + printf("Output: %f, %f, %f, %f\n", out_data[0], out_data[1], out_data[2], out_data[3]); |
| 79 | + } else { |
| 80 | + printf("Output is not a tensor!\n"); |
| 81 | + blink_indicator(INDICATOR_PIN_1, 10); |
| 82 | + return false; |
| 83 | + } |
| 84 | + return true; |
| 85 | +} |
| 86 | + |
| 87 | +int executor_runner() { |
| 88 | + using namespace executorch::extension; |
| 89 | + using namespace executorch::runtime; |
| 90 | + stdio_init_all(); |
| 91 | + sleep_ms(1000); |
| 92 | + |
| 93 | + wait_for_usb(); |
| 94 | + runtime_init(); |
| 95 | + |
| 96 | + static uint8_t method_allocator_pool[1024]; |
| 97 | + static uint8_t activation_pool[512]; |
| 98 | + MemoryAllocator method_allocator(sizeof(method_allocator_pool), method_allocator_pool); |
| 99 | + method_allocator.enable_profiling("method allocator"); |
| 100 | + Span<uint8_t> memory_planned_buffers[1]{{activation_pool, sizeof(activation_pool)}}; |
| 101 | + HierarchicalAllocator planned_memory({memory_planned_buffers, 1}); |
| 102 | + MemoryManager memory_manager(&method_allocator, &planned_memory); |
| 103 | + |
| 104 | + Program program; |
| 105 | + Method method; |
| 106 | + if (!load_and_prepare_model(program, method, memory_manager)) { |
| 107 | + printf("Failed to load and prepare model\n"); |
| 108 | + return 1; |
| 109 | + } |
| 110 | + if (!run_inference(method)) { |
| 111 | + printf("Failed to run inference\n"); |
| 112 | + return 1; |
| 113 | + } |
| 114 | + |
| 115 | + blink_indicator(INDICATOR_PIN_1, 100, 500); |
| 116 | + return 0; |
| 117 | +} |
| 118 | + |
| 119 | +int main() { |
| 120 | + return executor_runner(); |
| 121 | +} |
0 commit comments