mocleiri / tensorflow-micropython-examples

A custom micropython firmware integrating tensorflow lite for microcontrollers and ulab to implement the tensorflow micro examples.
MIT License
179 stars 86 forks source link

Add support for customizing which tflite micro operators are included in the firmware #28

Open mocleiri opened 3 years ago

mocleiri commented 3 years ago

Both tensorflow and the micropython firmware are built where unused code can be dropped at link time. This allows for a smaller firmware because not all operators need to be included.

We can copy the ulab approach of having many user controllable #defines that can be used to reduce the size of the firmware.

As the unused code detection is done at linking time we may need to add some extra objects to what is linked as libtensorflow-microlite.a so that it knows which op resolver we want to use and how many ops to build.

I think we should be able to use the COUNTER preproccessor extension to know how many ops are being built at compile time and then to set a matching value for a MutableOpResolver.

First we should try to use the tensorflow makefile: https://github.com/tensorflow/tflite-micro/blob/main/tensorflow/lite/micro/tools/make/Makefile

If that doesn't work we can pick up the Zeyphr cmake work and change our build so that we build tensorflow within the micropython build instead of just linking to the result of the tensorflow build.

https://github.com/tensorflow/tensorflow/issues/47241

mocleiri commented 3 years ago

As a test I turned off all of the 84 ops and the firmware size was shrunk by 200 kb from: 1.8 MB to 1.6 MB.

mocleiri commented 3 years ago

I checked the size of the reference build and it is 1.5 MB

mocleiri commented 3 years ago

I need to assess the experience when a user tries to use a model that uses an op that is not built into the firmware. I should be able to flash the minimal build and then install maybe the hello world example and then see what happens when trying to run it.

mocleiri commented 2 years ago

While we don't seem to have any space limitations at the moment. I think the research work on this topic can be merged down so that others have the ability to choose which ops they want in their custom firmware build.

Probably should be merged down after #36 has been completed.