Debian Codegraph @

docs/teflon.rst

raw
1 TensorFlow Lite delegate
2 ========================
3
4 Mesa contains a TensorFlow Lite delegate that can make use of NPUs to accelerate ML inference. It is implemented in the form of a *external delegate*, a shared library that the TensorFlow Lite runtime can load at startup. See https://www.tensorflow.org/api_docs/python/tf/lite/experimental/load_delegate.
5
6 .. list-table:: Supported acceleration hardware
7 :header-rows: 1
8
9 * - Gallium driver
10 - NPU supported
11 - Hardware tested
12 * - Etnaviv
13 - ``VeriSilicon VIPNano-QI.7120``
14 - ``Amlogic A311D on Libre Computer AML-A311D-CC Alta and Khadas VIM3``
15 * - Etnaviv
16 - ``VeriSilicon VIPNano-SI+.8002``
17 - ``NXP iMX8M Plus on Toradex Verdin SoM``
18 * - Rocket
19 - ``RK3588 NPU``
20 - ``PINE64 QuartzPro64``
21
22 .. list-table:: Tested models
23 :header-rows: 1
24
25 * - Model name
26 - Data type
27 - Link (may be outdated)
28 - Status
29 - Inference speed on AML-A311D-CC Alta
30 - Inference speed on Verdin iMX8M Plus
31 - Inference speed on QuartzPro64
32 * - MobileNet V1
33 - UINT8
34 - http://download.tensorflow.org/models/mobilenet_v1_2018_08_02/mobilenet_v1_1.0_224_quant.tgz
35 - Fully supported
36 - ~6.6 ms
37 - ~7.9 ms
38 - ~18 ms
39 * - MobileNet V2
40 - UINT8
41 - https://storage.googleapis.com/mobilenet_v2/checkpoints/quantized_v2_224_100.tgz
42 - Fully supported
43 - ~6.9 ms
44 - ~8.0 ms
45 - ~21 ms
46 * - SSDLite MobileDet
47 - UINT8
48 - https://raw.githubusercontent.com/google-coral/test_data/master/ssdlite_mobiledet_coco_qat_postprocess.tflite
49 - Fully supported
50 - ~24.8 ms
51 - ~24.4 ms
52 - ~48 ms
53
54 Build
55 -----
56
57 Build Mesa as usual, with the -Dteflon=true argument. Make sure at least one of etnaviv or rocket gallium drivers is enabled, as Teflon only works with these drivers.
58
59 Example instructions:
60
61 .. code-block:: console
62
63 # Install build dependencies
64 ~ # apt-get -y build-dep mesa
65 ~ # apt-get -y install git cmake
66
67 # Download sources
68 ~ $ git clone https://gitlab.freedesktop.org/mesa/mesa.git
69
70 # Build Mesa
71 ~ $ cd mesa
72 mesa $ meson setup build -Dgallium-drivers=etnaviv,rocket -Dvulkan-drivers= -Dteflon=true
73 mesa $ meson compile -C build
74
75 Install runtime dependencies
76 ----------------------------
77
78 Your board should have booted into a mainline 6.7 (6.8 for the i.MX8MP) or greater kernel.
79
80 .. code-block:: console
81
82 # Install Python 3.10 and dependencies (as root)
83 ~ # echo deb-src http://deb.debian.org/debian testing main >> /etc/apt/sources.list
84 ~ # echo deb http://deb.debian.org/debian unstable main >> /etc/apt/sources.list
85 ~ # echo 'APT::Default-Release "testing";' >> /etc/apt/apt.conf
86 ~ # apt-get update
87 ~ # apt-get -y install python3.10 python3-pytest python3-exceptiongroup
88
89 # Install TensorFlow Lite Python package (as non-root)
90 ~ $ python3.10 -m pip install --break-system-packages tflite-runtime==2.13.0
91
92 # For the classification.py script mentioned below, you will need PIL
93 ~ $ python3.10 -m pip install --break-system-packages pillow
94
95 Do some inference with MobileNetV1
96 ----------------------------------
97
98 Run the above for a quick way of checking that the setup is correct and the NPU is accelerating the inference. It assumes you have followed the steps above so Python 3.10 and dependencies have been installed, and assumes that Mesa was built to the ``./build`` directory.
99
100 You can use any image that prominently features one of the objects in the ``src/gallium/frontends/teflon/tests/labels_mobilenet_quant_v1_224.txt`` file. You can use an [image of Grace Hopper](https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/lite/examples/label_image/testdata/grace_hopper.bmp) as a running example.
101
102 This example script has been based from the code in https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/examples/python.
103
104 .. code-block:: console
105
106 ~ $ cd mesa/
107 mesa $ TEFLON_DEBUG=verbose ETNA_MESA_DEBUG=ml_msgs python3.10 src/gallium/frontends/teflon/tests/classification.py \
108 -i ~/tensorflow/assets/grace_hopper.bmp \
109 -m src/gallium/targets/teflon/tests/models/mobilenetv1/mobilenet_v1_1_224_quant.tflite \
110 -l src/gallium/frontends/teflon/tests/labels_mobilenet_quant_v1_224.txt \
111 -e build/src/gallium/targets/teflon/libteflon.so
112
113 Loading external delegate from build/src/gallium/targets/teflon/libteflon.so with args: {}
114 Teflon delegate: loaded etnaviv driver
115 idx type ver support inputs
116 ================================================================================================
117 0 CONV v1 supported in: 88(u8) 8(u8) 6(i32) out: 7(u8)
118 1 DWCONV v1 supported in: 7(u8) 35(u8) 34(i32) out: 33(u8)
119 2 CONV v1 supported in: 33(u8) 38(u8) 36(i32) out: 37(u8)
120 3 DWCONV v1 supported in: 37(u8) 41(u8) 40(i32) out: 39(u8)
121 4 CONV v1 supported in: 39(u8) 44(u8) 42(i32) out: 43(u8)
122 5 DWCONV v1 supported in: 43(u8) 47(u8) 46(i32) out: 45(u8)
123 6 CONV v1 supported in: 45(u8) 50(u8) 48(i32) out: 49(u8)
124 7 DWCONV v1 supported in: 49(u8) 53(u8) 52(i32) out: 51(u8)
125 8 CONV v1 supported in: 51(u8) 56(u8) 54(i32) out: 55(u8)
126 9 DWCONV v1 supported in: 55(u8) 59(u8) 58(i32) out: 57(u8)
127 ...
128
129 teflon: compiling graph: 89 tensors 27 operations
130 idx scale zp has_data size
131 =======================================
132 0 0.023528 0 no 1x1x1x1024
133 1 0.166099 42 no 1x1x1x1001
134 2 0.000117 0 yes 1x1x1x1001
135 3 0.004987 4a yes 1001x1x1x1024
136 4 0.166099 42 no 1x1x1x1001
137 5 0.000000 0 yes 1x1x1x2
138 6 0.000171 0 yes 1x1x1x32
139 7 0.023528 0 no 1x112x112x32
140 8 0.021827 97 yes 32x3x3x3
141 9 0.023528 0 no 1x14x14x512
142 ...
143
144 idx type inputs outputs
145 ==========================================================================
146 0 CONV 88,8,6 7
147 1 DWCONV 7,35,34 33
148 2 CONV 33,38,36 37
149 3 DWCONV 37,41,40 39
150 4 CONV 39,44,42 43
151 5 DWCONV 43,47,46 45
152 6 CONV 45,50,48 49
153 7 DWCONV 49,53,52 51
154 8 CONV 51,56,54 55
155 9 DWCONV 55,59,58 57
156 10 CONV 57,62,60 61
157 11 DWCONV 61,65,64 63
158 12 CONV 63,68,66 67
159 13 DWCONV 67,71,70 69
160 14 CONV 69,74,72 73
161 15 DWCONV 73,77,76 75
162 16 CONV 75,80,78 79
163 17 DWCONV 79,83,82 81
164 18 CONV 81,86,84 85
165 19 DWCONV 85,11,10 9
166 20 CONV 9,14,12 13
167 21 DWCONV 13,17,16 15
168 22 CONV 15,20,18 19
169 23 DWCONV 19,23,22 21
170 24 CONV 21,26,24 25
171 25 DWCONV 25,29,28 27
172 26 CONV 27,32,30 31
173
174 teflon: compiled graph, took 1911 ms
175
176 ...
177 teflon: compiled graph, took 602 ms
178 teflon: invoked graph, took 7 ms
179 teflon: invoked graph, took 1 ms
180 teflon: invoked graph, took 7 ms
181 teflon: invoked graph, took 0 ms
182 teflon: invoked graph, took 7 ms
183 teflon: invoked graph, took 1 ms
184 teflon: invoked graph, took 7 ms
185 teflon: invoked graph, took 0 ms
186 teflon: invoked graph, took 7 ms
187 teflon: invoked graph, took 0 ms
188 0.870588: military uniform
189 0.031373: Windsor tie
190 0.011765: mortarboard
191 0.007843: bow tie
192 0.007843: bulletproof vest
193 time: 7.549ms