xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/g3doc/microcontrollers/library.md (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1# Understand the C++ library
2
3The TensorFlow Lite for Microcontrollers C++ library is part of the
4[TensorFlow repository](https://github.com/tensorflow/tflite-micro/blob/main/tensorflow/lite/micro).
5It is designed to be readable, easy to modify, well-tested, easy to integrate,
6and compatible with regular TensorFlow Lite.
7
8The following document outlines the basic structure of the C++ library and
9provides information about creating your own project.
10
11## File structure
12
13The
14[`micro`](https://github.com/tensorflow/tflite-micro/blob/main/tensorflow/lite/micro)
15root directory has a relatively simple structure. However, since it is located
16inside of the extensive TensorFlow repository, we have created scripts and
17pre-generated project files that provide the relevant source files in isolation
18within various embedded development environments.
19
20### Key files
21
22The most important files for using the TensorFlow Lite for Microcontrollers
23interpreter are located in the root of the project, accompanied by tests:
24
25-   [`all_ops_resolver.h`](https://github.com/tensorflow/tflite-micro/blob/main/tensorflow/lite/micro/all_ops_resolver.h)
26    or
27    [`micro_mutable_op_resolver.h`](https://github.com/tensorflow/tflite-micro/blob/main/tensorflow/lite/micro/micro_mutable_op_resolver.h)
28    can be used to provide the operations used by the interpreter to run the
29    model. Since `all_ops_resolver.h` pulls in every available operation, it
30    uses a lot of memory. In production applications, you should use
31    `micro_mutable_op_resolver.h` to pull in only the operations your model
32    needs.
33-   [`micro_error_reporter.h`](https://github.com/tensorflow/tflite-micro/blob/main/tensorflow/lite/micro/micro_error_reporter.h)
34    outputs debug information.
35-   [`micro_interpreter.h`](https://github.com/tensorflow/tflite-micro/blob/main/tensorflow/lite/micro/micro_interpreter.h)
36    contains code to handle and run models.
37
38See [Get started with microcontrollers](get_started_low_level.md) for a
39walkthrough of typical usage.
40
41The build system provides for platform-specific implementations of certain
42files. These are located in a directory with the platform name, for example
43[`sparkfun_edge`](https://github.com/tensorflow/tflite-micro/blob/main/tensorflow/lite/micro/sparkfun_edge).
44
45Several other directories exist, including:
46
47-   [`kernel`](https://github.com/tensorflow/tflite-micro/blob/main/tensorflow/lite/micro/kernels),
48    which contains operation implementations and the associated code.
49-   [`tools`](https://github.com/tensorflow/tflite-micro/blob/main/tensorflow/lite/micro/tools),
50    which contains build tools and their output.
51-   [`examples`](https://github.com/tensorflow/tflite-micro/blob/main/tensorflow/lite/micro/examples),
52    which contains sample code.
53
54## Start a new project
55
56We recommend using the *Hello World* example as a template for new projects. You
57can obtain a version of it for your platform of choice by following the
58instructions in this section.
59
60### Use the Arduino library
61
62If you are using Arduino, the *Hello World* example is included in the
63`Arduino_TensorFlowLite` Arduino library, which you can download from the
64Arduino IDE and in [Arduino Create](https://create.arduino.cc/).
65
66Once the library has been added, go to `File -> Examples`. You should see an
67example near the bottom of the list named `TensorFlowLite:hello_world`. Select
68it and click `hello_world` to load the example. You can then save a copy of the
69example and use it as the basis of your own project.
70
71### Generate projects for other platforms
72
73TensorFlow Lite for Microcontrollers is able to generate standalone projects
74that contain all of the necessary source files, using a `Makefile`. The current
75supported environments are Keil, Make, and Mbed.
76
77To generate these projects with Make, clone the
78[TensorFlow repository](http://github.com/tensorflow/tensorflow) and run the
79following command:
80
81```bash
82make -f tensorflow/lite/micro/tools/make/Makefile generate_projects
83```
84
85This will take a few minutes, since it has to download some large toolchains for
86the dependencies. Once it has finished, you should see some folders created
87inside a path like `tensorflow/lite/micro/tools/make/gen/linux_x86_64/prj/` (the
88exact path depends on your host operating system). These folders contain the
89generated project and source files.
90
91After running the command, you'll be able to find the *Hello World* projects in
92`tensorflow/lite/micro/tools/make/gen/linux_x86_64/prj/hello_world`. For
93example, `hello_world/keil` will contain the Keil project.
94
95## Run the tests
96
97To build the library and run all of its unit tests, use the following command:
98
99```bash
100make -f tensorflow/lite/micro/tools/make/Makefile test
101```
102
103To run an individual test, use the following command, replacing `<test_name>`
104with the name of the test:
105
106```bash
107make -f tensorflow/lite/micro/tools/make/Makefile test_<test_name>
108```
109
110You can find the test names in the project's Makefiles. For example,
111`examples/hello_world/Makefile.inc` specifies the test names for the *Hello
112World* example.
113
114## Build binaries
115
116To build a runnable binary for a given project (such as an example application),
117use the following command, replacing `<project_name>` with the project you wish
118to build:
119
120```bash
121make -f tensorflow/lite/micro/tools/make/Makefile <project_name>_bin
122```
123
124For example, the following command will build a binary for the *Hello World*
125application:
126
127```bash
128make -f tensorflow/lite/micro/tools/make/Makefile hello_world_bin
129```
130
131By default, the project will be compiled for the host operating system. To
132specify a different target architecture, use `TARGET=`. The following example
133shows how to build the *Hello World* example for the SparkFun Edge:
134
135```bash
136make -f tensorflow/lite/micro/tools/make/Makefile TARGET=sparkfun_edge hello_world_bin
137```
138
139When a target is specified, any available target-specific source files will be
140used in place of the original code. For example, the subdirectory
141`examples/hello_world/sparkfun_edge` contains SparkFun Edge implementations of
142the files `constants.cc` and `output_handler.cc`, which will be used when the
143target `sparkfun_edge` is specified.
144
145You can find the project names in the project's Makefiles. For example,
146`examples/hello_world/Makefile.inc` specifies the binary names for the *Hello
147World* example.
148
149## Optimized kernels
150
151The reference kernels in the root of `tensorflow/lite/micro/kernels` are
152implemented in pure C/C++, and do not include platform-specific hardware
153optimizations.
154
155Optimized versions of kernels are provided in subdirectories. For example,
156`kernels/cmsis-nn` contains several optimized kernels that make use of Arm's
157CMSIS-NN library.
158
159To generate projects using optimized kernels, use the following command,
160replacing `<subdirectory_name>` with the name of the subdirectory containing the
161optimizations:
162
163```bash
164make -f tensorflow/lite/micro/tools/make/Makefile TAGS=<subdirectory_name> generate_projects
165```
166
167You can add your own optimizations by creating a new subfolder for them. We
168encourage pull requests for new optimized implementations.
169
170## Generate the Arduino library
171
172A nightly build of the Arduino library is available via the Arduino IDE's
173library manager.
174
175If you need to generate a new build of the library, you can run the following
176script from the TensorFlow repository:
177
178```bash
179./tensorflow/lite/micro/tools/ci_build/test_arduino.sh
180```
181
182The resulting library can be found in
183`tensorflow/lite/micro/tools/make/gen/arduino_x86_64/prj/tensorflow_lite.zip`.
184
185## Port to new devices
186
187Guidance on porting TensorFlow Lite for Microcontrollers to new platforms and
188devices can be found in
189[`micro/docs/new_platform_support.md`](https://github.com/tensorflow/tflite-micro/blob/main/tensorflow/lite/micro/docs/new_platform_support.md).
190