1# Unit tests for CMSIS-NN
2
3Unit test CMSIS-NN functions on any [Arm Mbed OS](https://os.mbed.com/mbed-os/) supported HW or using a fixed virtual platform (FVP) based on [Arm Corstone-300 software](https://developer.arm.com/ip-products/subsystem/corstone/corstone-300).
4
5The [Unity test framework](http://www.throwtheswitch.org/unity) is used for running the actual unit tests.
6
7## Requirements
8
9Python3 is required.
10It has been tested with Python 3.6 and it has been tested on Ubuntu 16, 18 and 20.
11
12Make sure to use the latest pip version before starting.
13If in a virtual environment just start by upgrading pip.
14
15```
16pip install --upgrade pip
17```
18
19See below for what pip packages are needed.
20
21### Executing unit tests
22
23If using the script unittest_targets.py for executing unit tests, the following packages are needed.
24
25```
26pip install pyserial mbed-ls mbed-cli termcolor mercurial pyelftools==0.29 pyyaml jsonschema jinja2 mbed_host_tests mbed_greentea pycryptodome pyusb cmsis_pack_manager psutil cryptography click cbor
27```
28
29Python packages mbed-cli and and mbed-ls are command line tools so it should not matter if those are installed under Python2 or Python3. These packages have been tested for Python2, with the following versions: mbed-ls(1.7.9) and mbed-cli(1.10.1). They have also been tested for Python3, with the following versions: mbed-ls(1.7.12) and mbed-cli(1.10.5). Package mercurial is needed for package mbed-cli.
30
31### Generating new test data
32
33For generating new test data, the following packages are needed.
34
35```
36pip install numpy packaging tensorflow tf-keras~=2.16
37```
38
39
40For generating new data, the python3 packages tensorflow, numpy and packaging are required. Most unit tests use a Keras generated model for reference. The SVDF unit test use a json template as input for generating a model. To do so flatc compiler is needed and it requires a schema file.
41
42#### Get flatc and schema
43
44Note this is only needed for generating SVDF unit tests.
45
46For flatc compiler clone this [repo](https://github.com/google/flatbuffers) and build:
47```
48cd flatbuffers
49cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release
50make
51```
52Remember to add the built flatc binary to the path.
53
54For schema file download [schema.fbs](https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/lite/schema/schema.fbs).
55
56#### Interpreter for generating reference output
57Python package tensorflow is always needed however the script has the option to use other interpreters than the tensorflow default, which will generate the actual reference output.
58
59##### tflite_runtime
60Python package tflite_runtime can be installed with pip and it can also be built locally. Check this [link](https://www.tensorflow.org/lite/guide/build_cmake_pip) on how to do that.
61Use the -h flag to get more info on supported interpreters.
62
63##### tflite_micro
64Python package tflite_micro can be installed with pip and it can also be built locally. See this comment for more info: https://github.com/tensorflow/tflite-micro/issues/1484#issuecomment-1677842603. This interpreter is only partially supported, see *Tests depending on TFLM interpreter*.
65
66## Getting started
67
68### Using Arm Mbed OS supported hardware
69
70Connect any HW (e.g. NUCLEO_F746ZG) that is supported by Arm Mbed OS. Multiple boards are supported. Note that board must be mounted, which may or may not be done automatically depending on OS. If all requirements are satisfied you can just run:
71
72```
73./unittest_targets.py
74```
75
76Use the -h flag to get more info.
77
78### Using FVP based on Arm Corstone-300 software
79
80The build for unit tests differs from the build of CMSIS-NN as a [standalone library](https://github.com/ARM-software/CMSIS-NN/blob/main/README.md#building-cmsis-nn-as-a-library) in that, there is a dependency to [CMSIS](https://github.com/ARM-software/CMSIS_5) project for the startup files from CMSIS-Core. This is specified by the mandatory CMSIS_PATH CMake argument.
81
82
83Here is an example for testing on Arm Cortex-M55:
84
85```
86cd </path/to/CMSIS_NN/Tests/Unittest>
87mkdir build
88cd build
89cmake .. -DCMAKE_TOOLCHAIN_FILE=</path/to/ethos-u-core-platform>/cmake/toolchain/arm-none-eabi-gcc.cmake -DTARGET_CPU=cortex-m55 -DCMSIS_PATH=</path/to/CMSIS>
90make
91```
92
93This will build all unit tests. You can also just build a specific unit test only, for example:
94
95```
96make test_arm_depthwise_conv_s8_opt
97```
98
99Then you need to download and install the FVP based Arm Corstone-300 software, for example:
100
101```
102mkdir -p /home/$user/FVP
103wget https://developer.arm.com/-/media/Arm%20Developer%20Community/Downloads/OSS/FVP/Corstone-300/FVP_Corstone_SSE-300_Ethos-U55_11.12_57.tgz
104tar -xvzf FVP_Corstone_SSE-300_Ethos-U55_11.12_57.tgz
105./FVP_Corstone_SSE-300_Ethos-U55.sh --i-agree-to-the-contained-eula --no-interactive -d /home/$user/FVP
106export PATH="/home/$user/FVP/models/Linux64_GCC-6.4:$PATH"
107```
108
109Finally you can run the unit tests. For example:
110
111```
112FVP_Corstone_SSE-300_Ethos-U55 --cpulimit 2 -C mps3_board.visualisation.disable-visualisation=1 -C mps3_board.telnetterminal0.start_telnet=0 -C mps3_board.uart0.out_file="-" -C mps3_board.uart0.unbuffered_output=1 ./TestCases/test_arm_depthwise_conv_s8_opt/test_arm_depthwise_conv_s8_opt.elf
113```
114
115## Generating new test data
116**NOTE:** The data generation scrips are being reworked, see *Refactoring of generate_test_data*
117
118Generating new test data is done with the following script. Use the -h flag to get more info.
119
120```
121./generate_test_data.py -h
122
123```
124
125The script use a concept of test data sets, i.e. it need a test set data name as input. It will then generate files with that name as prefix. Multiple header files of different test sets can then be included in the actual unit test files.
126When adding a new test data set, new c files should be added or existing c files should be updated to use the new data set. See overview of the folders on how/where to add new c files.
127
128The steps to add a new unit test are as follows. Add a new test test in the load_all_testdatasets() function. Run the generate script with that new test set as input. Add the new generated header files to an existing or new unit test.
129
130### Tests depending on TFLM interpreter
131
132If TFL and TFLM reference kernels differ, CMSIS-NN aims to be bit-exact to TFLM reference kernels. Hence those operators depends on tflite_micro interpreter.
133
134Operator bit-exactness compability:
135
136| Operator        |  TFL bit-exact  | TFLM bit-exact |  Notes
137| ---             | ---             | ---       | ---
138| convolution     |   x             |  x        |
139| fully_connected |   x             |  x        |
140| lstm            |                 |  x        |
141| svdf            |                 |  x        | Operator is only fully supported by TFLM.
142| softmax         |   x             |  x        |
143| avgpool         |   x             |  x        |
144| maxpool         |   x             |  x        |
145| add             |   x             |  x        |
146| mul             |   x             |  x        |
147
148### Refactoring of generate_test_data.py
149Test data generation is in progress of incrementally moving over to the cleaned up scripts placed in `RefactoredTestGen`.
150
151To try out the new scripts, use
152```
153./RefactoredTestGen/generate_test_data.py --help
154```
155
156The previous generate_test_data will remain as the main data generator until all functionality is replicated with the new scripts.
157
158Current progress:
159
160| Operator        | Old  | New | Notes
161| ---             | ---  | --- | ---
162| convolution     |  x   |  x  | New version only supports 16x8
163| fully_connected |  x   |  x  | New version only supports int4 packed weights
164| lstm            |  x   |  x  | Only new version supporting 16x8
165| svdf            |  x   |     |
166| softmax         |  x   |     |
167| avgpool         |  x   |     |
168| maxpool         |  x   |     |
169| add             |  x   |     |
170| mul             |  x   |     |
171
172
173## Overview of the Folders
174
175- `Corstone-300` - These are dependencies, like linker files etc, needed when building binaries targeting the FVP based on Arm Corstone-300 software. This is mostly taken from Arm Ethos-U Core Platform project.
176- `Mbed` - These are the Arm Mbed OS settings that are used. See Mbed/README.md.
177- `Output` - This will be created when building.
178- `PregeneratedData` - Host local(Not part of GitHub) test data for model creation using Keras in unit tests. It can be used for debug purposes when
179                       adding new operators or debugging existing ones.
180- `TestCases` - Here are the actual unit tests. For each function under test there is a folder under here.
181- `TestCases/<cmsis-nn function name>` - For each function under test there is a folder with the same name with test_ prepended to the name and it contains a c-file with the actual unit tests. For example for arm_convolve_s8() the file is called test_arm_convolve_s8.c
182- `TestCases/<cmsis-nn function name>/Unity` - This folder contains a Unity file that calls the actual unit tests. For example for arm_convolve_s8() the file is called unity_test_arm_convolve_s8.c.
183- `TestCases/<cmsis-nn function name>/Unity/TestRunner` - This folder will contain the autogenerated Unity test runner.
184- `TestCases/TestData` - This is auto generated test data in .h files that the unit tests are using. The data in PregenrateData folder has fp32 data of a network whereas  here it is the quantized equivalent of the same. They are not the same. All data can regenerated or only parts of it (e.g. only bias data). Of course even the config can be regenerated. This partial/full regeneration is useful during debugging.
185- `TestCases/Common` - Common files used in test data generation is placed here.
186- `RefactoredTestGen` - Temporary location for new test generation scripts
187
188## Formatting
189
190The python test scripts should be formatted with yapf.
191
192```
193pip install --upgrade yapf
194```
195
196The following settings are used.
197
198```
199python -m yapf --in-place --style='{based_on_style:pep8,column_limit:120,indent_width:4}' *.py
200```
201