Skip to content

Commit ace1371

Browse files
ccli8jhedberg
authored andcommitted
samples: tflite_ethosu: support numaker_m55m1
This supports nuvoton numaker_m55m1 board, including. Besides, re-organize for new target and Ethos-U support, including: 1. Keep only one model originally for MPS3 target 2. Add guide to regenerate Vela-compiled model for targets supporting Ethos-U 3. Support running non-Vela-compiled model for targets supporting no Ethos-U Signed-off-by: Chun-Chieh Li <[email protected]>
1 parent 9f58227 commit ace1371

File tree

9 files changed

+5025
-4936
lines changed

9 files changed

+5025
-4936
lines changed

samples/modules/tflite-micro/tflm_ethosu/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
88

99
project(tflm_ethosu_app)
1010

11+
# Choose model
1112
target_include_directories(app PRIVATE src/models/keyword_spotting_cnn_small_int8)
1213

1314
target_sources(app PRIVATE src/main.cpp src/inference_process.cpp)
1415

15-
zephyr_linker_sources(SECTIONS linker.ld)
16+
# Add platform specific linker source snippet
17+
zephyr_linker_sources_ifdef(CONFIG_SOC_SERIES_MPS3 SECTIONS linker.ld)
18+
zephyr_linker_sources_ifdef(CONFIG_SOC_SERIES_MPS4 SECTIONS linker.ld)
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
# Copyright 2022 Arm Limited and/or its affiliates <[email protected]>
22
# SPDX-License-Identifier: Apache-2.0
33

4-
config TFLM_ETHOSU_TAINT_BLOBS
4+
config TAINT_BLOBS_TFLM
55
bool
66
default y
77
select TAINT_BLOBS
88

9+
config TAINT_BLOBS_TFLM_ETHOSU
10+
bool "Choose Vela-compiled model targeting Ethos-U"
11+
default y
12+
depends on ETHOS_U_ARM || ETHOS_U_NUMAKER
13+
select TAINT_BLOBS_TFLM
14+
915
source "Kconfig.zephyr"

samples/modules/tflite-micro/tflm_ethosu/README.rst

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,67 @@ where the operators supported by Ethos-U have been replaced by an Ethos-U custom
1919
operator. In an ideal case the complete network would be replaced by a single
2020
Ethos-U custom operator.
2121

22+
Generating Vela-compiled model
23+
******************************
24+
25+
Follow the steps below to generate Vela-compiled model and test input/output data.
26+
Use `keyword_spotting_cnn_small_int8`_ model in this sample:
27+
28+
.. _keyword_spotting_cnn_small_int8: https://github.com/Arm-Examples/ML-zoo/tree/master/models/keyword_spotting/cnn_small/model_package_tf/model_archive/TFLite/tflite_int8
29+
30+
.. note:: The default Vela-compiled model is to target Ethos-U55 and 128 MAC
31+
on MPS3 target. Because one model can add up to hundreds of KB, don't
32+
attempt to add more models into code base for other targets.
33+
34+
1. Downloading the files below from `keyword_spotting_cnn_small_int8`_:
35+
36+
- cnn_s_quantized.tflite
37+
- testing_input/input/0.npy
38+
- testing_output/identity/0.npy
39+
40+
2. Optimizing the model for Ethos-U using Vela
41+
42+
Assuming target Ethos-U is U55 and 128 MAC:
43+
44+
.. code-block:: console
45+
46+
$ vela cnn_s_quantized.tflite \
47+
--output-dir . \
48+
--accelerator-config ethos-u55-128 \
49+
--system-config Ethos_U55_High_End_Embedded \
50+
--memory-mode Shared_Sram
51+
52+
3. Removing unnecessary header
53+
54+
``testing_input/input/0.npy`` and ``testing_output/0.npy`` have 128-byte header.
55+
They must be removed for integration with this sample.
56+
57+
.. code-block:: console
58+
59+
$ dd if=testing_input/input/0.npy of=testing_input/input/0_no-header.npy bs=1 skip=128
60+
$ dd if=testing_output/identity/0.npy of=testing_output/identity/0_no-header.npy bs=1 skip=128
61+
62+
4. Converting to C array
63+
64+
.. code-block:: console
65+
66+
$ xxd -c 16 -i cnn_s_quantized.tflite cnn_s_quantized.tflite.h
67+
$ xxd -c 16 -i cnn_s_quantized_vela.tflite cnn_s_quantized_vela.tflite.h
68+
$ xxd -c 16 -i testing_input/input/0_no-header.npy testing_input/input/0_no-header.npy.h
69+
$ xxd -c 16 -i testing_output/identity/0_no-header.npy testing_output/identity/0_no-header.npy.h
70+
71+
5. Synchronizing to this sample
72+
73+
Synchronize the files below to ``keyword_spotting_cnn_small_int8`` directory
74+
in this sample:
75+
76+
- cnn_s_quantized_vela.tflite.h > model.h
77+
- testing_input/input/0_no-header.npy.h > input.h
78+
- testing_output/identity/0_no-header.npy.h > output.h
79+
80+
.. note:: To run non-Vela-compiled model (``CONFIG_TAINT_BLOBS_TFLM_ETHOSU=n``),
81+
synchronize ``cnn_s_quantized.tflite.h`` instead.
82+
2283
Building and running
2384
********************
2485

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* SPDX-License-Identifier: Apache-2.0 */
2+
3+
&npu0 {
4+
status = "okay";
5+
};

samples/modules/tflite-micro/tflm_ethosu/src/inference_process.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,16 @@ bool InferenceProcess::runJob(InferenceJob &job)
118118
}
119119

120120
/* Create the TFL micro interpreter */
121+
#ifdef CONFIG_TAINT_BLOBS_TFLM_ETHOSU
121122
tflite::MicroMutableOpResolver <1> resolver;
122123
resolver.AddEthosU();
123-
124+
#else
125+
tflite::MicroMutableOpResolver <4> resolver;
126+
resolver.AddReshape();
127+
resolver.AddConv2D();
128+
resolver.AddFullyConnected();
129+
resolver.AddSoftmax();
130+
#endif
124131
tflite::MicroInterpreter interpreter(model, resolver, tensorArena, tensorArenaSize);
125132

126133
/* Allocate tensors */

samples/modules/tflite-micro/tflm_ethosu/src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ volatile int totalCompletedJobs = 0;
9898
/* TensorArena static initialisation */
9999
const size_t arenaSize = TENSOR_ARENA_SIZE_PER_INFERENCE;
100100

101-
__attribute__((section("tflm_arena"), aligned(16)))
101+
TENSOR_ARENA_ATTR
102102
uint8_t inferenceProcessTensorArena[NUM_INFERENCE_TASKS][arenaSize];
103103

104104
/* Allocate and initialize heap */

samples/modules/tflite-micro/tflm_ethosu/src/models/keyword_spotting_cnn_small_int8/input.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
__aligned(4) __attribute__((section("tflm_input"))) uint8_t inputData[] = {
7+
#if (defined(CONFIG_SOC_SERIES_MPS3) || defined(CONFIG_SOC_SERIES_MPS4)) && \
8+
DT_NODE_HAS_STATUS(DT_NODELABEL(ddr4), okay)
9+
#define INPUT_ATTR __aligned(4) __attribute__((section("tflm_input")))
10+
#else
11+
#define INPUT_ATTR __aligned(4) __attribute__((section(".rodata.tflm_input")))
12+
#endif
13+
14+
INPUT_ATTR uint8_t inputData[] = {
815
0x2c, 0x8a, 0xff, 0x0c, 0xaf, 0x2a, 0x44, 0x17, 0xf5, 0x26, 0x96, 0x37, 0x40, 0x4c, 0xa1,
916
0x58, 0xc3, 0x33, 0xce, 0x1a, 0x7b, 0xd2, 0x22, 0x5b, 0x43, 0xf6, 0xfd, 0x0b, 0xe7, 0xfd,
1017
0x65, 0x58, 0x89, 0x24, 0xf4, 0xec, 0x53, 0x5e, 0x21, 0x1f, 0x95, 0xd1, 0xd9, 0x25, 0x72,

0 commit comments

Comments
 (0)