alexander | f4e2c47 | 2021-05-14 13:14:21 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 2 | # Copyright (c) 2021-2022 Arm Limited. All rights reserved. |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 16 | import errno |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 17 | import fnmatch |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 18 | import json |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 19 | import logging |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 20 | import os |
| 21 | import re |
| 22 | import shutil |
| 23 | import subprocess |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 24 | import sys |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 25 | import urllib.request |
| 26 | from argparse import ArgumentParser |
| 27 | from argparse import ArgumentTypeError |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 28 | from collections import namedtuple |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 29 | from urllib.error import URLError |
Kshitij Sisodia | 6a2ac46 | 2022-03-01 17:36:06 +0000 | [diff] [blame] | 30 | from scripts.py.check_update_resources_downloaded import get_md5sum_for_file |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 31 | |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 32 | |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 33 | json_uc_res = [ |
| 34 | { |
| 35 | "use_case_name": "ad", |
| 36 | "url_prefix": [ |
| 37 | "https://github.com/ARM-software/ML-zoo/raw/7c32b097f7d94aae2cd0b98a8ed5a3ba81e66b18/models/anomaly_detection/micronet_medium/tflite_int8/" |
| 38 | ], |
| 39 | "resources": [ |
| 40 | { |
| 41 | "name": "ad_medium_int8.tflite", |
| 42 | "url": "{url_prefix:0}ad_medium_int8.tflite", |
| 43 | }, |
| 44 | {"name": "ifm0.npy", "url": "{url_prefix:0}testing_input/input/0.npy"}, |
| 45 | {"name": "ofm0.npy", "url": "{url_prefix:0}testing_output/Identity/0.npy"}, |
| 46 | ], |
| 47 | }, |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 48 | { |
| 49 | "use_case_name": "asr", |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 50 | "url_prefix": [ |
| 51 | "https://github.com/ARM-software/ML-zoo/raw/1a92aa08c0de49a7304e0a7f3f59df6f4fd33ac8/models/speech_recognition/wav2letter/tflite_pruned_int8/" |
| 52 | ], |
| 53 | "resources": [ |
| 54 | { |
| 55 | "name": "wav2letter_pruned_int8.tflite", |
| 56 | "url": "{url_prefix:0}wav2letter_pruned_int8.tflite", |
| 57 | }, |
| 58 | { |
| 59 | "name": "ifm0.npy", |
| 60 | "url": "{url_prefix:0}testing_input/input_2_int8/0.npy", |
| 61 | }, |
| 62 | { |
| 63 | "name": "ofm0.npy", |
| 64 | "url": "{url_prefix:0}testing_output/Identity_int8/0.npy", |
| 65 | }, |
| 66 | ], |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 67 | }, |
| 68 | { |
| 69 | "use_case_name": "img_class", |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 70 | "url_prefix": [ |
| 71 | "https://github.com/ARM-software/ML-zoo/raw/e0aa361b03c738047b9147d1a50e3f2dcb13dbcb/models/image_classification/mobilenet_v2_1.0_224/tflite_int8/" |
| 72 | ], |
| 73 | "resources": [ |
| 74 | { |
| 75 | "name": "mobilenet_v2_1.0_224_INT8.tflite", |
| 76 | "url": "{url_prefix:0}mobilenet_v2_1.0_224_INT8.tflite", |
| 77 | }, |
| 78 | { |
| 79 | "name": "ifm0.npy", |
| 80 | "url": "{url_prefix:0}testing_input/tfl.quantize/0.npy", |
| 81 | }, |
| 82 | { |
| 83 | "name": "ofm0.npy", |
| 84 | "url": "{url_prefix:0}testing_output/MobilenetV2/Predictions/Reshape_11/0.npy", |
| 85 | }, |
| 86 | ], |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 87 | }, |
| 88 | { |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 89 | "use_case_name": "object_detection", |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 90 | "url_prefix": [ |
| 91 | "https://github.com/emza-vs/ModelZoo/blob/v1.0/object_detection/" |
| 92 | ], |
| 93 | "resources": [ |
| 94 | { |
| 95 | "name": "yolo-fastest_192_face_v4.tflite", |
| 96 | "url": "{url_prefix:0}yolo-fastest_192_face_v4.tflite?raw=true", |
| 97 | } |
| 98 | ], |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 99 | }, |
| 100 | { |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 101 | "use_case_name": "kws", |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 102 | "url_prefix": [ |
| 103 | "https://github.com/ARM-software/ML-zoo/raw/9f506fe52b39df545f0e6c5ff9223f671bc5ae00/models/keyword_spotting/micronet_medium/tflite_int8/" |
| 104 | ], |
| 105 | "resources": [ |
| 106 | {"name": "ifm0.npy", "url": "{url_prefix:0}testing_input/input/0.npy"}, |
| 107 | {"name": "ofm0.npy", "url": "{url_prefix:0}testing_output/Identity/0.npy"}, |
| 108 | { |
| 109 | "name": "kws_micronet_m.tflite", |
| 110 | "url": "{url_prefix:0}kws_micronet_m.tflite", |
| 111 | }, |
| 112 | ], |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 113 | }, |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 114 | { |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 115 | "use_case_name": "vww", |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 116 | "url_prefix": [ |
| 117 | "https://github.com/ARM-software/ML-zoo/raw/7dd3b16bb84007daf88be8648983c07f3eb21140/models/visual_wake_words/micronet_vww4/tflite_int8/" |
| 118 | ], |
| 119 | "resources": [ |
| 120 | { |
| 121 | "name": "vww4_128_128_INT8.tflite", |
| 122 | "url": "{url_prefix:0}vww4_128_128_INT8.tflite", |
| 123 | }, |
| 124 | {"name": "ifm0.npy", "url": "{url_prefix:0}testing_input/input/0.npy"}, |
| 125 | {"name": "ofm0.npy", "url": "{url_prefix:0}testing_output/Identity/0.npy"}, |
| 126 | ], |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 127 | }, |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 128 | { |
| 129 | "use_case_name": "kws_asr", |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 130 | "url_prefix": [ |
| 131 | "https://github.com/ARM-software/ML-zoo/raw/1a92aa08c0de49a7304e0a7f3f59df6f4fd33ac8/models/speech_recognition/wav2letter/tflite_pruned_int8/", |
| 132 | "https://github.com/ARM-software/ML-zoo/raw/9f506fe52b39df545f0e6c5ff9223f671bc5ae00/models/keyword_spotting/micronet_medium/tflite_int8/", |
| 133 | ], |
| 134 | "resources": [ |
| 135 | { |
| 136 | "name": "wav2letter_pruned_int8.tflite", |
| 137 | "url": "{url_prefix:0}wav2letter_pruned_int8.tflite", |
| 138 | }, |
| 139 | { |
| 140 | "sub_folder": "asr", |
| 141 | "name": "ifm0.npy", |
| 142 | "url": "{url_prefix:0}testing_input/input_2_int8/0.npy", |
| 143 | }, |
| 144 | { |
| 145 | "sub_folder": "asr", |
| 146 | "name": "ofm0.npy", |
| 147 | "url": "{url_prefix:0}testing_output/Identity_int8/0.npy", |
| 148 | }, |
| 149 | { |
| 150 | "sub_folder": "kws", |
| 151 | "name": "ifm0.npy", |
| 152 | "url": "{url_prefix:1}testing_input/input/0.npy", |
| 153 | }, |
| 154 | { |
| 155 | "sub_folder": "kws", |
| 156 | "name": "ofm0.npy", |
| 157 | "url": "{url_prefix:1}testing_output/Identity/0.npy", |
| 158 | }, |
| 159 | { |
| 160 | "name": "kws_micronet_m.tflite", |
| 161 | "url": "{url_prefix:1}kws_micronet_m.tflite", |
| 162 | }, |
| 163 | ], |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 164 | }, |
| 165 | { |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 166 | "use_case_name": "noise_reduction", |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 167 | "url_prefix": [ |
| 168 | "https://github.com/ARM-software/ML-zoo/raw/a061600058097a2785d6f1f7785e5a2d2a142955/models/noise_suppression/RNNoise/tflite_int8/" |
| 169 | ], |
| 170 | "resources": [ |
| 171 | {"name": "rnnoise_INT8.tflite", "url": "{url_prefix:0}rnnoise_INT8.tflite"}, |
| 172 | { |
| 173 | "name": "ifm0.npy", |
| 174 | "url": "{url_prefix:0}testing_input/main_input_int8/0.npy", |
| 175 | }, |
| 176 | { |
| 177 | "name": "ifm1.npy", |
| 178 | "url": "{url_prefix:0}testing_input/vad_gru_prev_state_int8/0.npy", |
| 179 | }, |
| 180 | { |
| 181 | "name": "ifm2.npy", |
| 182 | "url": "{url_prefix:0}testing_input/noise_gru_prev_state_int8/0.npy", |
| 183 | }, |
| 184 | { |
| 185 | "name": "ifm3.npy", |
| 186 | "url": "{url_prefix:0}testing_input/denoise_gru_prev_state_int8/0.npy", |
| 187 | }, |
| 188 | { |
| 189 | "name": "ofm0.npy", |
| 190 | "url": "{url_prefix:0}testing_output/Identity_int8/0.npy", |
| 191 | }, |
| 192 | { |
| 193 | "name": "ofm1.npy", |
| 194 | "url": "{url_prefix:0}testing_output/Identity_1_int8/0.npy", |
| 195 | }, |
| 196 | { |
| 197 | "name": "ofm2.npy", |
| 198 | "url": "{url_prefix:0}testing_output/Identity_2_int8/0.npy", |
| 199 | }, |
| 200 | { |
| 201 | "name": "ofm3.npy", |
| 202 | "url": "{url_prefix:0}testing_output/Identity_3_int8/0.npy", |
| 203 | }, |
| 204 | { |
| 205 | "name": "ofm4.npy", |
| 206 | "url": "{url_prefix:0}testing_output/Identity_4_int8/0.npy", |
| 207 | }, |
| 208 | ], |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 209 | }, |
| 210 | { |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 211 | "use_case_name": "inference_runner", |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 212 | "url_prefix": [ |
| 213 | "https://github.com/ARM-software/ML-zoo/raw/68b5fbc77ed28e67b2efc915997ea4477c1d9d5b/models/keyword_spotting/dnn_small/tflite_int8/" |
| 214 | ], |
| 215 | "resources": [ |
| 216 | { |
| 217 | "name": "dnn_s_quantized.tflite", |
| 218 | "url": "{url_prefix:0}dnn_s_quantized.tflite", |
| 219 | } |
| 220 | ], |
| 221 | }, |
| 222 | ] |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 223 | |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 224 | # Valid NPU configurations: |
| 225 | valid_npu_config_names = [ |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 226 | "ethos-u55-32", |
| 227 | "ethos-u55-64", |
| 228 | "ethos-u55-128", |
| 229 | "ethos-u55-256", |
| 230 | "ethos-u65-256", |
| 231 | "ethos-u65-512", |
| 232 | ] |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 233 | |
| 234 | # Default NPU configurations (these are always run when the models are optimised) |
| 235 | default_npu_config_names = [valid_npu_config_names[2], valid_npu_config_names[4]] |
| 236 | |
| 237 | # NPU config named tuple |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 238 | NPUConfig = namedtuple( |
| 239 | "NPUConfig", |
| 240 | [ |
| 241 | "config_name", |
| 242 | "memory_mode", |
| 243 | "system_config", |
| 244 | "ethos_u_npu_id", |
| 245 | "ethos_u_config_id", |
| 246 | "arena_cache_size", |
| 247 | ], |
| 248 | ) |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 249 | |
Kshitij Sisodia | 661959c | 2021-11-24 10:39:52 +0000 | [diff] [blame] | 250 | # The internal SRAM size for Corstone-300 implementation on MPS3 specified by AN552 |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 251 | mps3_max_sram_sz = 2 * 1024 * 1024 # 2 MiB (2 banks of 1 MiB each) |
Liam Barry | b52b585 | 2021-11-15 11:41:40 +0000 | [diff] [blame] | 252 | |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 253 | |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 254 | def call_command(command: str, verbose: bool = True) -> str: |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 255 | """ |
| 256 | Helpers function that call subprocess and return the output. |
| 257 | |
| 258 | Parameters: |
| 259 | ---------- |
| 260 | command (string): Specifies the command to run. |
| 261 | """ |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 262 | if verbose: |
| 263 | logging.info(command) |
| 264 | proc = subprocess.run( |
| 265 | command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True |
| 266 | ) |
alexander | 50a0650 | 2021-05-12 19:06:02 +0100 | [diff] [blame] | 267 | log = proc.stdout.decode("utf-8") |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 268 | if proc.returncode == 0 and verbose: |
alexander | 50a0650 | 2021-05-12 19:06:02 +0100 | [diff] [blame] | 269 | logging.info(log) |
| 270 | else: |
| 271 | logging.error(log) |
| 272 | proc.check_returncode() |
| 273 | return log |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 274 | |
| 275 | |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 276 | def get_default_npu_config_from_name( |
| 277 | config_name: str, arena_cache_size: int = 0 |
| 278 | ) -> NPUConfig: |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 279 | """ |
| 280 | Gets the file suffix for the tflite file from the |
| 281 | `accelerator_config` string. |
| 282 | |
| 283 | Parameters: |
| 284 | ---------- |
Isabella Gottardi | 3acaaee | 2021-11-30 12:33:27 +0000 | [diff] [blame] | 285 | config_name (str): Ethos-U NPU configuration from valid_npu_config_names |
| 286 | |
| 287 | arena_cache_size (int): Specifies arena cache size in bytes. If a value |
| 288 | greater than 0 is provided, this will be taken |
| 289 | as the cache size. If 0, the default values, as per |
| 290 | the NPU config requirements, are used. |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 291 | |
| 292 | Returns: |
| 293 | ------- |
| 294 | NPUConfig: An NPU config named tuple populated with defaults for the given |
| 295 | config name |
| 296 | """ |
| 297 | if config_name not in valid_npu_config_names: |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 298 | raise ValueError( |
| 299 | f""" |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 300 | Invalid Ethos-U NPU configuration. |
| 301 | Select one from {valid_npu_config_names}. |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 302 | """ |
| 303 | ) |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 304 | |
| 305 | strings_ids = ["ethos-u55-", "ethos-u65-"] |
| 306 | processor_ids = ["U55", "U65"] |
| 307 | prefix_ids = ["H", "Y"] |
| 308 | memory_modes = ["Shared_Sram", "Dedicated_Sram"] |
| 309 | system_configs = ["Ethos_U55_High_End_Embedded", "Ethos_U65_High_End"] |
Isabella Gottardi | 3acaaee | 2021-11-30 12:33:27 +0000 | [diff] [blame] | 310 | memory_modes_arena = { |
| 311 | # For shared SRAM memory mode, we use the MPS3 SRAM size by default |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 312 | "Shared_Sram": mps3_max_sram_sz if arena_cache_size <= 0 else arena_cache_size, |
Isabella Gottardi | 3acaaee | 2021-11-30 12:33:27 +0000 | [diff] [blame] | 313 | # For dedicated SRAM memory mode, we do no override the arena size. This is expected to |
| 314 | # be defined in the vela configuration file instead. |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 315 | "Dedicated_Sram": None if arena_cache_size <= 0 else arena_cache_size, |
Isabella Gottardi | 3acaaee | 2021-11-30 12:33:27 +0000 | [diff] [blame] | 316 | } |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 317 | |
| 318 | for i in range(len(strings_ids)): |
| 319 | if config_name.startswith(strings_ids[i]): |
| 320 | npu_config_id = config_name.replace(strings_ids[i], prefix_ids[i]) |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 321 | return NPUConfig( |
| 322 | config_name=config_name, |
| 323 | memory_mode=memory_modes[i], |
| 324 | system_config=system_configs[i], |
| 325 | ethos_u_npu_id=processor_ids[i], |
| 326 | ethos_u_config_id=npu_config_id, |
| 327 | arena_cache_size=memory_modes_arena[memory_modes[i]], |
| 328 | ) |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 329 | |
| 330 | return None |
| 331 | |
| 332 | |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 333 | def remove_tree_dir(dir_path): |
| 334 | try: |
| 335 | # Remove the full directory |
| 336 | shutil.rmtree(dir_path) |
| 337 | # Re-create an empty one |
| 338 | os.mkdir(dir_path) |
| 339 | except Exception as e: |
| 340 | logging.error(f"Failed to delete {dir_path}.") |
| 341 | |
| 342 | |
| 343 | def set_up_resources( |
| 344 | run_vela_on_models: bool = False, |
| 345 | additional_npu_config_names: list = (), |
| 346 | arena_cache_size: int = 0, |
| 347 | check_clean_folder: bool = False, |
| 348 | ): |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 349 | """ |
| 350 | Helpers function that retrieve the output from a command. |
| 351 | |
| 352 | Parameters: |
| 353 | ---------- |
Isabella Gottardi | 3acaaee | 2021-11-30 12:33:27 +0000 | [diff] [blame] | 354 | run_vela_on_models (bool): Specifies if run vela on downloaded models. |
| 355 | additional_npu_config_names(list): list of strings of Ethos-U NPU configs. |
| 356 | arena_cache_size (int): Specifies arena cache size in bytes. If a value |
| 357 | greater than 0 is provided, this will be taken |
| 358 | as the cache size. If 0, the default values, as per |
| 359 | the NPU config requirements, are used. |
Kshitij Sisodia | 6a2ac46 | 2022-03-01 17:36:06 +0000 | [diff] [blame] | 360 | check_clean_folder (bool): Indicates whether the resources folder needs to |
| 361 | be checked for updates and cleaned. |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 362 | """ |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 363 | # Paths |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 364 | current_file_dir = os.path.dirname(os.path.abspath(__file__)) |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 365 | download_dir = os.path.abspath( |
| 366 | os.path.join(current_file_dir, "resources_downloaded") |
| 367 | ) |
| 368 | metadata_file_path = os.path.join( |
| 369 | download_dir, "resources_downloaded_metadata.json" |
| 370 | ) |
| 371 | |
| 372 | metadata_dict = dict() |
Richard Burton | b4123fd | 2022-03-04 09:19:09 +0000 | [diff] [blame] | 373 | vela_version = "3.3.0" |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 374 | |
Kshitij Sisodia | 6a2ac46 | 2022-03-01 17:36:06 +0000 | [diff] [blame] | 375 | setup_script_hash_verified = False |
| 376 | setup_script_hash = get_md5sum_for_file(os.path.abspath(__file__)) |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 377 | |
| 378 | try: |
| 379 | # 1.1 Does the download dir exist? |
| 380 | os.mkdir(download_dir) |
| 381 | except OSError as e: |
| 382 | if e.errno == errno.EEXIST: |
| 383 | logging.info("'resources_downloaded' directory exists.") |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 384 | # Check and clean? |
| 385 | if check_clean_folder and os.path.isfile(metadata_file_path): |
| 386 | with open(metadata_file_path) as (metadata_file): |
| 387 | metadata_dict = json.load(metadata_file) |
| 388 | vela_in_metadata = metadata_dict["ethosu_vela_version"] |
| 389 | if vela_in_metadata != vela_version: |
| 390 | # Check if all the resources needs to be removed and regenerated. |
| 391 | # This can happen when the Vela version has changed. |
| 392 | logging.info( |
| 393 | f"Vela version in metadata is {vela_in_metadata}, current {vela_version}. Removing the resources and re-download them." |
| 394 | ) |
| 395 | remove_tree_dir(download_dir) |
| 396 | metadata_dict = dict() |
| 397 | else: |
Kshitij Sisodia | 6a2ac46 | 2022-03-01 17:36:06 +0000 | [diff] [blame] | 398 | # Check if the set_up_default_resorces.py has changed from last setup |
| 399 | setup_script_hash_verified = ( |
| 400 | metadata_dict.get('set_up_script_md5sum') == setup_script_hash) |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 401 | else: |
| 402 | raise |
| 403 | |
| 404 | # 1.2 Does the virtual environment exist? |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 405 | env_python = str( |
| 406 | os.path.abspath(os.path.join(download_dir, "env", "bin", "python3")) |
| 407 | ) |
| 408 | env_activate = str( |
| 409 | os.path.abspath(os.path.join(download_dir, "env", "bin", "activate")) |
| 410 | ) |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 411 | if not os.path.isdir(os.path.join(download_dir, "env")): |
| 412 | os.chdir(download_dir) |
| 413 | # Create the virtual environment |
| 414 | command = "python3 -m venv env" |
| 415 | call_command(command) |
| 416 | commands = ["pip install --upgrade pip", "pip install --upgrade setuptools"] |
| 417 | for c in commands: |
| 418 | command = f"{env_python} -m {c}" |
| 419 | call_command(command) |
| 420 | os.chdir(current_file_dir) |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 421 | |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 422 | # 1.3 Make sure to have all the requirement |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 423 | requirements = [f"ethos-u-vela=={vela_version}"] |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 424 | command = f"{env_python} -m pip freeze" |
| 425 | packages = call_command(command) |
| 426 | for req in requirements: |
| 427 | if req not in packages: |
| 428 | command = f"{env_python} -m pip install {req}" |
| 429 | call_command(command) |
| 430 | |
| 431 | # 2. Download models |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 432 | logging.info("Downloading resources.") |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 433 | for uc in json_uc_res: |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 434 | use_case_name = uc["use_case_name"] |
| 435 | res_url_prefix = uc["url_prefix"] |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 436 | try: |
| 437 | # Does the usecase_name download dir exist? |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 438 | os.mkdir(os.path.join(download_dir, use_case_name)) |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 439 | except OSError as e: |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 440 | if e.errno == errno.EEXIST: |
| 441 | # The usecase_name download dir exist |
Kshitij Sisodia | 6a2ac46 | 2022-03-01 17:36:06 +0000 | [diff] [blame] | 442 | if check_clean_folder and not setup_script_hash_verified: |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 443 | for idx, metadata_uc_url_prefix in enumerate( |
| 444 | [ |
| 445 | f |
| 446 | for f in metadata_dict["resources_info"] |
| 447 | if f["use_case_name"] == use_case_name |
| 448 | ][0]["url_prefix"] |
| 449 | ): |
| 450 | if metadata_uc_url_prefix != res_url_prefix[idx]: |
| 451 | logging.info(f"Removing {use_case_name} resources.") |
| 452 | remove_tree_dir(os.path.join(download_dir, use_case_name)) |
| 453 | break |
| 454 | elif e.errno != errno.EEXIST: |
| 455 | logging.error(f"Error creating {use_case_name} directory.") |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 456 | raise |
| 457 | |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 458 | reg_expr_str = r"{url_prefix:(.*\d)}" |
| 459 | reg_expr_pattern = re.compile(reg_expr_str) |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 460 | for res in uc["resources"]: |
| 461 | res_name = res["name"] |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 462 | url_prefix_idx = int(reg_expr_pattern.search(res["url"]).group(1)) |
| 463 | res_url = res_url_prefix[url_prefix_idx] + re.sub( |
| 464 | reg_expr_str, "", res["url"] |
| 465 | ) |
| 466 | |
| 467 | sub_folder = "" |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 468 | if "sub_folder" in res: |
| 469 | try: |
| 470 | # Does the usecase_name/sub_folder download dir exist? |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 471 | os.mkdir( |
| 472 | os.path.join(download_dir, use_case_name, res["sub_folder"]) |
| 473 | ) |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 474 | except OSError as e: |
| 475 | if e.errno != errno.EEXIST: |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 476 | logging.error( |
| 477 | f"Error creating {use_case_name} / {res['sub_folder']} directory." |
| 478 | ) |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 479 | raise |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 480 | sub_folder = res["sub_folder"] |
| 481 | |
| 482 | res_dst = os.path.join(download_dir, use_case_name, sub_folder, res_name) |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 483 | |
| 484 | if os.path.isfile(res_dst): |
| 485 | logging.info(f"File {res_dst} exists, skipping download.") |
| 486 | else: |
| 487 | try: |
| 488 | g = urllib.request.urlopen(res_url) |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 489 | with open(res_dst, "b+w") as f: |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 490 | f.write(g.read()) |
| 491 | logging.info(f"- Downloaded {res_url} to {res_dst}.") |
| 492 | except URLError: |
| 493 | logging.error(f"URLError while downloading {res_url}.") |
| 494 | raise |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 495 | |
| 496 | # 3. Run vela on models in resources_downloaded |
| 497 | # New models will have same name with '_vela' appended. |
| 498 | # For example: |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 499 | # original model: kws_micronet_m.tflite |
| 500 | # after vela model: kws_micronet_m_vela_H128.tflite |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 501 | # |
| 502 | # Note: To avoid to run vela twice on the same model, it's supposed that |
| 503 | # downloaded model names don't contain the 'vela' word. |
| 504 | if run_vela_on_models is True: |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 505 | config_file = os.path.join( |
| 506 | current_file_dir, "scripts", "vela", "default_vela.ini" |
| 507 | ) |
| 508 | models = [ |
| 509 | os.path.join(dirpath, f) |
| 510 | for dirpath, dirnames, files in os.walk(download_dir) |
| 511 | for f in fnmatch.filter(files, "*.tflite") |
| 512 | if "vela" not in f |
| 513 | ] |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 514 | |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 515 | # Consolidate all config names while discarding duplicates: |
| 516 | config_names = list(set(default_npu_config_names + additional_npu_config_names)) |
| 517 | |
| 518 | # Get npu config tuple for each config name in a list: |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 519 | npu_configs = [ |
| 520 | get_default_npu_config_from_name(name, arena_cache_size) |
| 521 | for name in config_names |
| 522 | ] |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 523 | |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 524 | logging.info(f"All models will be optimised for these configs:") |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 525 | for config in npu_configs: |
| 526 | logging.info(config) |
| 527 | |
Isabella Gottardi | 3acaaee | 2021-11-30 12:33:27 +0000 | [diff] [blame] | 528 | optimisation_skipped = False |
| 529 | |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 530 | for model in models: |
| 531 | output_dir = os.path.dirname(model) |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 532 | # model name after compiling with vela is an initial model name + _vela suffix |
| 533 | vela_optimised_model_path = str(model).replace(".tflite", "_vela.tflite") |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 534 | |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 535 | for config in npu_configs: |
Isabella Gottardi | 3acaaee | 2021-11-30 12:33:27 +0000 | [diff] [blame] | 536 | vela_command_arena_cache_size = "" |
| 537 | |
| 538 | if config.arena_cache_size: |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 539 | vela_command_arena_cache_size = ( |
| 540 | f"--arena-cache-size={config.arena_cache_size}" |
| 541 | ) |
Isabella Gottardi | 3acaaee | 2021-11-30 12:33:27 +0000 | [diff] [blame] | 542 | |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 543 | vela_command = ( |
| 544 | f". {env_activate} && vela {model} " |
| 545 | + f"--accelerator-config={config.config_name} " |
| 546 | + "--optimise Performance " |
| 547 | + f"--config {config_file} " |
| 548 | + f"--memory-mode={config.memory_mode} " |
| 549 | + f"--system-config={config.system_config} " |
| 550 | + f"--output-dir={output_dir} " |
| 551 | + f"{vela_command_arena_cache_size}" |
| 552 | ) |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 553 | |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 554 | # we want the name to include the configuration suffix. For example: vela_H128, |
| 555 | # vela_Y512 etc. |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 556 | new_suffix = "_vela_" + config.ethos_u_config_id + ".tflite" |
| 557 | new_vela_optimised_model_path = vela_optimised_model_path.replace( |
| 558 | "_vela.tflite", new_suffix |
| 559 | ) |
Isabella Gottardi | 118f73e | 2021-09-16 17:54:35 +0100 | [diff] [blame] | 560 | |
| 561 | if os.path.isfile(new_vela_optimised_model_path): |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 562 | logging.info( |
| 563 | f"File {new_vela_optimised_model_path} exists, skipping optimisation." |
| 564 | ) |
Isabella Gottardi | 3acaaee | 2021-11-30 12:33:27 +0000 | [diff] [blame] | 565 | optimisation_skipped = True |
Isabella Gottardi | 118f73e | 2021-09-16 17:54:35 +0100 | [diff] [blame] | 566 | continue |
| 567 | |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 568 | call_command(vela_command) |
Isabella Gottardi | 118f73e | 2021-09-16 17:54:35 +0100 | [diff] [blame] | 569 | |
| 570 | # rename default vela model |
| 571 | os.rename(vela_optimised_model_path, new_vela_optimised_model_path) |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 572 | logging.info( |
| 573 | f"Renaming {vela_optimised_model_path} to {new_vela_optimised_model_path}." |
| 574 | ) |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 575 | |
Isabella Gottardi | 3acaaee | 2021-11-30 12:33:27 +0000 | [diff] [blame] | 576 | # If any optimisation was skipped, show how to regenerate: |
| 577 | if optimisation_skipped: |
| 578 | logging.warning("One or more optimisations were skipped.") |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 579 | logging.warning( |
| 580 | f"To optimise all the models, please remove the directory {download_dir}." |
| 581 | ) |
| 582 | |
| 583 | # 4. Collect and write metadata |
| 584 | logging.info("Collecting and write metadata.") |
| 585 | metadata_dict["ethosu_vela_version"] = vela_version |
Kshitij Sisodia | 6a2ac46 | 2022-03-01 17:36:06 +0000 | [diff] [blame] | 586 | metadata_dict["set_up_script_md5sum"] = setup_script_hash.strip("\n") |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 587 | metadata_dict["resources_info"] = json_uc_res |
| 588 | |
| 589 | with open(metadata_file_path, "w") as metadata_file: |
| 590 | json.dump(metadata_dict, metadata_file, indent=4) |
Isabella Gottardi | 3acaaee | 2021-11-30 12:33:27 +0000 | [diff] [blame] | 591 | |
| 592 | |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 593 | if __name__ == "__main__": |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 594 | parser = ArgumentParser() |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 595 | parser.add_argument( |
| 596 | "--skip-vela", |
| 597 | help="Do not run Vela optimizer on downloaded models.", |
| 598 | action="store_true", |
| 599 | ) |
| 600 | parser.add_argument( |
| 601 | "--additional-ethos-u-config-name", |
| 602 | help=f"""Additional (non-default) configurations for Vela: |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 603 | {valid_npu_config_names}""", |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 604 | default=[], |
| 605 | action="append", |
| 606 | ) |
| 607 | parser.add_argument( |
| 608 | "--arena-cache-size", |
| 609 | help="Arena cache size in bytes (if overriding the defaults)", |
| 610 | type=int, |
| 611 | default=0, |
| 612 | ) |
| 613 | parser.add_argument( |
| 614 | "--clean", |
| 615 | help="Clean the disctory and optimize the downloaded resources", |
| 616 | action="store_true", |
| 617 | ) |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 618 | args = parser.parse_args() |
Kshitij Sisodia | b9e9c89 | 2021-05-27 13:57:35 +0100 | [diff] [blame] | 619 | |
Isabella Gottardi | 3acaaee | 2021-11-30 12:33:27 +0000 | [diff] [blame] | 620 | if args.arena_cache_size < 0: |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 621 | raise ArgumentTypeError("Arena cache size cannot not be less than 0") |
Isabella Gottardi | 3acaaee | 2021-11-30 12:33:27 +0000 | [diff] [blame] | 622 | |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 623 | logging.basicConfig(filename="log_build_default.log", level=logging.DEBUG) |
Kshitij Sisodia | b9e9c89 | 2021-05-27 13:57:35 +0100 | [diff] [blame] | 624 | logging.getLogger().addHandler(logging.StreamHandler(sys.stdout)) |
| 625 | |
Isabella Gottardi | ef2b9dd | 2022-02-16 14:24:03 +0000 | [diff] [blame] | 626 | set_up_resources( |
| 627 | not args.skip_vela, |
| 628 | args.additional_ethos_u_config_name, |
| 629 | args.arena_cache_size, |
| 630 | args.clean, |
| 631 | ) |