|
| 1 | +# Configure TFLite Micro Development Environment |
| 2 | + |
| 3 | +Before developing TensorFlow Lite for Microcontrollers (TFLite Micro) applications on the openvela platform, the compilation environment and dependent libraries must be configured correctly. This section guides developers through source code confirmation, library dependency configuration, and memory strategy formulation. |
| 4 | + |
| 5 | +## I. Prerequisites |
| 6 | + |
| 7 | +Before starting, please ensure that the following preparations have been completed: |
| 8 | + |
| 9 | +- **Basic Environment**: Refer to the [Official Documentation](../quickstart/openvela_ubuntu_quick_start.md) to complete the deployment of the openvela basic development environment. |
| 10 | + |
| 11 | +- **Source Code Confirmation**: The TFLite Micro source code has been integrated into the openvela code repository at the following path: |
| 12 | + |
| 13 | + - `apps/mlearning/tflite-micro/` |
| 14 | + |
| 15 | +## II. Component and Dependency Library Support |
| 16 | + |
| 17 | +TFLite Micro relies on specific mathematical and utility libraries to implement model parsing and operator acceleration. The openvela repository has pre-configured the following key components: |
| 18 | + |
| 19 | +| **Component Name** | **Functional Description** | **Source Path** | |
| 20 | +| :----------------- | :--------------------------------------------------------------------------------------------------------------- | :------------------------- | |
| 21 | +| **FlatBuffers** | Library supporting the TFLite model serialization format; provides necessary headers. | `apps/system/flatbuffers/` | |
| 22 | +| **Gemmlowp** | Google's low-precision general matrix multiplication library, used for quantized operations. | `apps/math/gemmlowp/` | |
| 23 | +| **Ruy** | TensorFlow's high-performance matrix multiplication backend, mainly optimizing fully connected layer operations. | `apps/math/ruy/` | |
| 24 | +| **KissFFT** | Lightweight Fast Fourier Transform library, supporting fixed-point and floating-point operations. | `apps/math/kissfft/` | |
| 25 | +| **CMSIS-NN** | Neural network kernel optimization library dedicated to ARM Cortex-M (optional). | `apps/mlearning/cmsis-nn/` | |
| 26 | + |
| 27 | +## III. Compilation Configuration (Kconfig) |
| 28 | + |
| 29 | +Enable necessary library support through the `menuconfig` graphical interface to ensure successful compilation and optimize code size. |
| 30 | + |
| 31 | +Launch the configuration menu: |
| 32 | + |
| 33 | +```Bash |
| 34 | +cmake --build cmake_out/goldfish-arm64-v8a-ap -t menuconfig |
| 35 | +``` |
| 36 | + |
| 37 | +Please complete the configuration of the following four core modules in order: |
| 38 | + |
| 39 | +### 1. Enable C++ Runtime Support |
| 40 | + |
| 41 | +TFLite Micro is written based on C++11/14 standards; therefore, LLVM libc++ support must be enabled. |
| 42 | + |
| 43 | +- **Configuration Path**: `Library Routines` -> `C++ Library` |
| 44 | +- **Action**: Select `LLVM libc++ C++ Standard Library` |
| 45 | + |
| 46 | +```Plain |
| 47 | +(Top) → Library Routines → C++ Library |
| 48 | +
|
| 49 | +( ) Toolchain C++ support |
| 50 | +( ) Basic C++ support |
| 51 | +(X) LLVM libc++ C++ Standard Library |
| 52 | +``` |
| 53 | + |
| 54 | +### 2. Enable Math Acceleration Libraries |
| 55 | + |
| 56 | +Enable matrix operation and signal processing libraries based on model requirements. |
| 57 | + |
| 58 | +- **Configuration Path**: `Application Configuration` -> `Math Library Support` |
| 59 | +- **Action**: Select `Gemmlowp`, `kissfft`, and `Ruy` |
| 60 | + |
| 61 | +```Plain |
| 62 | +(Top) → Application Configuration → Math Library Support |
| 63 | +
|
| 64 | +[*] Gemmlowp |
| 65 | +[*] kissfft |
| 66 | +[ ] LibTomMath MPI Math Library |
| 67 | +[*] Ruy |
| 68 | +``` |
| 69 | + |
| 70 | +### 3. Enable FlatBuffers Support |
| 71 | + |
| 72 | +Enable the system-level FlatBuffers library to support model parsing. |
| 73 | + |
| 74 | +- **Configuration Path**: `Application Configuration` -> `System Libraries and NSH Add-Ons` |
| 75 | +- **Action**: Select `flatbuffers` |
| 76 | + |
| 77 | +```Plain |
| 78 | +(Top) → Application Configuration → System Libraries and NSH Add-Ons |
| 79 | +
|
| 80 | +[*] flatbuffers |
| 81 | +``` |
| 82 | + |
| 83 | +### 4. Enable TFLite Micro Core |
| 84 | + |
| 85 | +- **Configuration Path**: `Application Configuration` -> `Machine Learning Support` |
| 86 | +- **Action**: Select `TFLiteMicro`. If ARM hardware acceleration is required, it is recommended to also select `CMSIS_NN Library`. |
| 87 | + |
| 88 | +```Plain |
| 89 | +(Top) → Application Configuration → Machine Learning Support |
| 90 | +
|
| 91 | +[ ] CMSIS_NN Library |
| 92 | +[*] TFLiteMicro |
| 93 | +[ ] Print tflite-micro's debug message |
| 94 | +``` |
| 95 | + |
| 96 | +## IV. Memory Allocation Strategy |
| 97 | + |
| 98 | +Embedded systems have limited memory resources. TFLite Micro requires a continuous memory area (Tensor Arena) to store input/output tensors and intermediate calculation results. |
| 99 | + |
| 100 | +### 1. Static Allocation (Recommended) |
| 101 | + |
| 102 | +For production environments, static array allocation is recommended. This method eliminates the risk of memory fragmentation, and memory usage is known at compile time. |
| 103 | + |
| 104 | +**Implementation Example**: |
| 105 | + |
| 106 | +```C++ |
| 107 | +// Define in the global area of the application code |
| 108 | +// Note: Memory must be aligned to 16 bytes to meet SIMD instruction requirements |
| 109 | +#define TENSOR_ARENA_SIZE (100 * 1024) |
| 110 | +static uint8_t tensor_arena[TENSOR_ARENA_SIZE] __attribute__((aligned(16))); |
| 111 | +``` |
| 112 | +
|
| 113 | +### 2. Determine Arena Size |
| 114 | +
|
| 115 | +To precisely set `TENSOR_ARENA_SIZE` and avoid waste or overflow, you can use `RecordingMicroInterpreter` to capture actual memory usage at runtime. |
| 116 | +
|
| 117 | +**Debugging Steps**: |
| 118 | +
|
| 119 | +1. Include the recorder header file. |
| 120 | +2. Use `RecordingMicroInterpreter` to replace the standard `MicroInterpreter`. |
| 121 | +3. Run model inference once (Invoke). |
| 122 | +4. Read the actual usage and add a safety margin (suggest adding +1KB). |
| 123 | +
|
| 124 | +```C++ |
| 125 | +#include "tensorflow/lite/micro/recording_micro_interpreter.h" |
| 126 | +
|
| 127 | +// 1. Create recording allocator |
| 128 | +auto* allocator = tflite::RecordingMicroAllocator::Create(tensor_arena, arena_size); |
| 129 | +
|
| 130 | +// 2. Instantiate recording interpreter |
| 131 | +tflite::RecordingMicroInterpreter interpreter(model, resolver, allocator); |
| 132 | +
|
| 133 | +// 3. Allocate tensors and execute inference |
| 134 | +interpreter.AllocateTensors(); |
| 135 | +interpreter.Invoke(); |
| 136 | +
|
| 137 | +// 4. Get memory statistics |
| 138 | +size_t used = interpreter.arena_used_bytes(); // Actual usage |
| 139 | +interpreter.GetMicroAllocator().PrintAllocations(); // Itemized details |
| 140 | +size_t recommended = used + 1024; // Reserve at least ~1KB extra space |
| 141 | +``` |
0 commit comments