alexander | f4e2c47 | 2021-05-14 13:14:21 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 2 | |
| 3 | # Copyright (c) 2021 Arm Limited. All rights reserved. |
| 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | |
| 18 | import os, errno |
| 19 | import urllib.request |
| 20 | import subprocess |
| 21 | import fnmatch |
| 22 | import logging |
| 23 | import sys |
| 24 | |
Isabella Gottardi | 3acaaee | 2021-11-30 12:33:27 +0000 | [diff] [blame^] | 25 | from argparse import ArgumentParser, ArgumentTypeError |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 26 | from urllib.error import URLError |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 27 | from collections import namedtuple |
| 28 | |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 29 | |
| 30 | json_uc_res = [{ |
| 31 | "use_case_name": "ad", |
| 32 | "resources": [{"name": "ad_medium_int8.tflite", |
| 33 | "url": "https://github.com/ARM-software/ML-zoo/raw/7c32b097f7d94aae2cd0b98a8ed5a3ba81e66b18/models/anomaly_detection/micronet_medium/tflite_int8/ad_medium_int8.tflite"}, |
| 34 | {"name": "ifm0.npy", |
| 35 | "url": "https://github.com/ARM-software/ML-zoo/raw/7c32b097f7d94aae2cd0b98a8ed5a3ba81e66b18/models/anomaly_detection/micronet_medium/tflite_int8/testing_input/input/0.npy"}, |
| 36 | {"name": "ofm0.npy", |
| 37 | "url": "https://github.com/ARM-software/ML-zoo/raw/7c32b097f7d94aae2cd0b98a8ed5a3ba81e66b18/models/anomaly_detection/micronet_medium/tflite_int8/testing_output/Identity/0.npy"}] |
| 38 | }, |
| 39 | { |
| 40 | "use_case_name": "asr", |
Kshitij Sisodia | e12ac83 | 2021-05-20 11:18:53 +0100 | [diff] [blame] | 41 | "resources": [{"name": "wav2letter_pruned_int8.tflite", |
| 42 | "url": "https://github.com/ARM-software/ML-zoo/raw/1a92aa08c0de49a7304e0a7f3f59df6f4fd33ac8/models/speech_recognition/wav2letter/tflite_pruned_int8/wav2letter_pruned_int8.tflite"}, |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 43 | {"name": "ifm0.npy", |
Kshitij Sisodia | e12ac83 | 2021-05-20 11:18:53 +0100 | [diff] [blame] | 44 | "url": "https://github.com/ARM-software/ML-zoo/raw/1a92aa08c0de49a7304e0a7f3f59df6f4fd33ac8/models/speech_recognition/wav2letter/tflite_pruned_int8/testing_input/input_2_int8/0.npy"}, |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 45 | {"name": "ofm0.npy", |
Kshitij Sisodia | e12ac83 | 2021-05-20 11:18:53 +0100 | [diff] [blame] | 46 | "url": "https://github.com/ARM-software/ML-zoo/raw/1a92aa08c0de49a7304e0a7f3f59df6f4fd33ac8/models/speech_recognition/wav2letter/tflite_pruned_int8/testing_output/Identity_int8/0.npy"}] |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 47 | }, |
| 48 | { |
| 49 | "use_case_name": "img_class", |
Richard Burton | 0d11059 | 2021-08-12 17:26:30 +0100 | [diff] [blame] | 50 | "resources": [{"name": "mobilenet_v2_1.0_224_INT8.tflite", |
| 51 | "url": "https://github.com/ARM-software/ML-zoo/raw/e0aa361b03c738047b9147d1a50e3f2dcb13dbcb/models/image_classification/mobilenet_v2_1.0_224/tflite_int8/mobilenet_v2_1.0_224_INT8.tflite"}, |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 52 | {"name": "ifm0.npy", |
Richard Burton | 0d11059 | 2021-08-12 17:26:30 +0100 | [diff] [blame] | 53 | "url": "https://github.com/ARM-software/ML-zoo/raw/e0aa361b03c738047b9147d1a50e3f2dcb13dbcb/models/image_classification/mobilenet_v2_1.0_224/tflite_int8/testing_input/tfl.quantize/0.npy"}, |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 54 | {"name": "ofm0.npy", |
Richard Burton | 0d11059 | 2021-08-12 17:26:30 +0100 | [diff] [blame] | 55 | "url": "https://github.com/ARM-software/ML-zoo/raw/e0aa361b03c738047b9147d1a50e3f2dcb13dbcb/models/image_classification/mobilenet_v2_1.0_224/tflite_int8/testing_output/MobilenetV2/Predictions/Reshape_11/0.npy"}] |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 56 | }, |
| 57 | { |
| 58 | "use_case_name": "kws", |
| 59 | "resources": [{"name": "ds_cnn_clustered_int8.tflite", |
| 60 | "url": "https://github.com/ARM-software/ML-zoo/raw/68b5fbc77ed28e67b2efc915997ea4477c1d9d5b/models/keyword_spotting/ds_cnn_large/tflite_clustered_int8/ds_cnn_clustered_int8.tflite"}, |
| 61 | {"name": "ifm0.npy", |
| 62 | "url": "https://github.com/ARM-software/ML-zoo/raw/68b5fbc77ed28e67b2efc915997ea4477c1d9d5b/models/keyword_spotting/ds_cnn_large/tflite_clustered_int8/testing_input/input_2/0.npy"}, |
| 63 | {"name": "ofm0.npy", |
| 64 | "url": "https://github.com/ARM-software/ML-zoo/raw/68b5fbc77ed28e67b2efc915997ea4477c1d9d5b/models/keyword_spotting/ds_cnn_large/tflite_clustered_int8/testing_output/Identity/0.npy"}] |
| 65 | }, |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 66 | { |
| 67 | "use_case_name": "vww", |
| 68 | "resources": [{"name": "vww4_128_128_INT8.tflite", |
| 69 | "url": "https://github.com/ARM-software/ML-zoo/raw/7dd3b16bb84007daf88be8648983c07f3eb21140/models/visual_wake_words/micronet_vww4/tflite_int8/vww4_128_128_INT8.tflite"}, |
| 70 | {"name": "ifm0.npy", |
| 71 | "url": "https://github.com/ARM-software/ML-zoo/raw/7dd3b16bb84007daf88be8648983c07f3eb21140/models/visual_wake_words/micronet_vww4/tflite_int8/testing_input/input/0.npy"}, |
| 72 | {"name": "ofm0.npy", |
| 73 | "url": "https://github.com/ARM-software/ML-zoo/raw/7dd3b16bb84007daf88be8648983c07f3eb21140/models/visual_wake_words/micronet_vww4/tflite_int8/testing_output/Identity/0.npy"}] |
| 74 | }, |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 75 | { |
| 76 | "use_case_name": "kws_asr", |
Kshitij Sisodia | e12ac83 | 2021-05-20 11:18:53 +0100 | [diff] [blame] | 77 | "resources": [{"name": "wav2letter_pruned_int8.tflite", |
| 78 | "url": "https://github.com/ARM-software/ML-zoo/raw/1a92aa08c0de49a7304e0a7f3f59df6f4fd33ac8/models/speech_recognition/wav2letter/tflite_pruned_int8/wav2letter_pruned_int8.tflite"}, |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 79 | {"sub_folder": "asr", "name": "ifm0.npy", |
Kshitij Sisodia | e12ac83 | 2021-05-20 11:18:53 +0100 | [diff] [blame] | 80 | "url": "https://github.com/ARM-software/ML-zoo/raw/1a92aa08c0de49a7304e0a7f3f59df6f4fd33ac8/models/speech_recognition/wav2letter/tflite_pruned_int8/testing_input/input_2_int8/0.npy"}, |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 81 | {"sub_folder": "asr", "name": "ofm0.npy", |
Kshitij Sisodia | e12ac83 | 2021-05-20 11:18:53 +0100 | [diff] [blame] | 82 | "url": "https://github.com/ARM-software/ML-zoo/raw/1a92aa08c0de49a7304e0a7f3f59df6f4fd33ac8/models/speech_recognition/wav2letter/tflite_pruned_int8/testing_output/Identity_int8/0.npy"}, |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 83 | {"name": "ds_cnn_clustered_int8.tflite", |
| 84 | "url": "https://github.com/ARM-software/ML-zoo/raw/68b5fbc77ed28e67b2efc915997ea4477c1d9d5b/models/keyword_spotting/ds_cnn_large/tflite_clustered_int8/ds_cnn_clustered_int8.tflite"}, |
| 85 | {"sub_folder": "kws", "name": "ifm0.npy", |
| 86 | "url": "https://github.com/ARM-software/ML-zoo/raw/68b5fbc77ed28e67b2efc915997ea4477c1d9d5b/models/keyword_spotting/ds_cnn_large/tflite_clustered_int8/testing_input/input_2/0.npy"}, |
| 87 | {"sub_folder": "kws", "name": "ofm0.npy", |
| 88 | "url": "https://github.com/ARM-software/ML-zoo/raw/68b5fbc77ed28e67b2efc915997ea4477c1d9d5b/models/keyword_spotting/ds_cnn_large/tflite_clustered_int8/testing_output/Identity/0.npy"}] |
| 89 | }, |
| 90 | { |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 91 | "use_case_name": "noise_reduction", |
| 92 | "resources": [{"name": "rnnoise_INT8.tflite", |
| 93 | "url": "https://github.com/ARM-software/ML-zoo/raw/a061600058097a2785d6f1f7785e5a2d2a142955/models/noise_suppression/RNNoise/tflite_int8/rnnoise_INT8.tflite"}, |
| 94 | {"name": "ifm0.npy", |
| 95 | "url": "https://github.com/ARM-software/ML-zoo/raw/a061600058097a2785d6f1f7785e5a2d2a142955/models/noise_suppression/RNNoise/tflite_int8/testing_input/main_input_int8/0.npy"}, |
| 96 | {"name": "ifm1.npy", |
| 97 | "url": "https://github.com/ARM-software/ML-zoo/raw/a061600058097a2785d6f1f7785e5a2d2a142955/models/noise_suppression/RNNoise/tflite_int8/testing_input/vad_gru_prev_state_int8/0.npy"}, |
| 98 | {"name": "ifm2.npy", |
| 99 | "url": "https://github.com/ARM-software/ML-zoo/raw/a061600058097a2785d6f1f7785e5a2d2a142955/models/noise_suppression/RNNoise/tflite_int8/testing_input/noise_gru_prev_state_int8/0.npy"}, |
| 100 | {"name": "ifm3.npy", |
| 101 | "url": "https://github.com/ARM-software/ML-zoo/raw/a061600058097a2785d6f1f7785e5a2d2a142955/models/noise_suppression/RNNoise/tflite_int8/testing_input/denoise_gru_prev_state_int8/0.npy"}, |
| 102 | {"name": "ofm0.npy", |
| 103 | "url": "https://github.com/ARM-software/ML-zoo/raw/a061600058097a2785d6f1f7785e5a2d2a142955/models/noise_suppression/RNNoise/tflite_int8/testing_output/Identity_int8/0.npy"}, |
| 104 | {"name": "ofm1.npy", |
| 105 | "url": "https://github.com/ARM-software/ML-zoo/raw/a061600058097a2785d6f1f7785e5a2d2a142955/models/noise_suppression/RNNoise/tflite_int8/testing_output/Identity_1_int8/0.npy"}, |
| 106 | {"name": "ofm2.npy", |
| 107 | "url": "https://github.com/ARM-software/ML-zoo/raw/a061600058097a2785d6f1f7785e5a2d2a142955/models/noise_suppression/RNNoise/tflite_int8/testing_output/Identity_2_int8/0.npy"}, |
| 108 | {"name": "ofm3.npy", |
| 109 | "url": "https://github.com/ARM-software/ML-zoo/raw/a061600058097a2785d6f1f7785e5a2d2a142955/models/noise_suppression/RNNoise/tflite_int8/testing_output/Identity_3_int8/0.npy"}, |
| 110 | {"name": "ofm4.npy", |
| 111 | "url": "https://github.com/ARM-software/ML-zoo/raw/a061600058097a2785d6f1f7785e5a2d2a142955/models/noise_suppression/RNNoise/tflite_int8/testing_output/Identity_4_int8/0.npy"}, |
| 112 | ] |
| 113 | }, |
| 114 | { |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 115 | "use_case_name": "inference_runner", |
| 116 | "resources": [{"name": "dnn_s_quantized.tflite", |
| 117 | "url": "https://github.com/ARM-software/ML-zoo/raw/68b5fbc77ed28e67b2efc915997ea4477c1d9d5b/models/keyword_spotting/dnn_small/tflite_int8/dnn_s_quantized.tflite"} |
| 118 | ] |
| 119 | },] |
| 120 | |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 121 | # Valid NPU configurations: |
| 122 | valid_npu_config_names = [ |
| 123 | 'ethos-u55-32', 'ethos-u55-64', |
| 124 | 'ethos-u55-128', 'ethos-u55-256', |
| 125 | 'ethos-u65-256','ethos-u65-512'] |
| 126 | |
| 127 | # Default NPU configurations (these are always run when the models are optimised) |
| 128 | default_npu_config_names = [valid_npu_config_names[2], valid_npu_config_names[4]] |
| 129 | |
| 130 | # NPU config named tuple |
| 131 | NPUConfig = namedtuple('NPUConfig',['config_name', |
| 132 | 'memory_mode', |
| 133 | 'system_config', |
| 134 | 'ethos_u_npu_id', |
Isabella Gottardi | 3acaaee | 2021-11-30 12:33:27 +0000 | [diff] [blame^] | 135 | 'ethos_u_config_id', |
| 136 | 'arena_cache_size']) |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 137 | |
Kshitij Sisodia | 661959c | 2021-11-24 10:39:52 +0000 | [diff] [blame] | 138 | # The internal SRAM size for Corstone-300 implementation on MPS3 specified by AN552 |
| 139 | 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] | 140 | |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 141 | |
| 142 | def call_command(command: str) -> str: |
| 143 | """ |
| 144 | Helpers function that call subprocess and return the output. |
| 145 | |
| 146 | Parameters: |
| 147 | ---------- |
| 148 | command (string): Specifies the command to run. |
| 149 | """ |
| 150 | logging.info(command) |
alexander | 50a0650 | 2021-05-12 19:06:02 +0100 | [diff] [blame] | 151 | proc = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) |
| 152 | log = proc.stdout.decode("utf-8") |
| 153 | if proc.returncode == 0: |
| 154 | logging.info(log) |
| 155 | else: |
| 156 | logging.error(log) |
| 157 | proc.check_returncode() |
| 158 | return log |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 159 | |
| 160 | |
Isabella Gottardi | 3acaaee | 2021-11-30 12:33:27 +0000 | [diff] [blame^] | 161 | def get_default_npu_config_from_name(config_name: str, arena_cache_size: int = 0) -> NPUConfig: |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 162 | """ |
| 163 | Gets the file suffix for the tflite file from the |
| 164 | `accelerator_config` string. |
| 165 | |
| 166 | Parameters: |
| 167 | ---------- |
Isabella Gottardi | 3acaaee | 2021-11-30 12:33:27 +0000 | [diff] [blame^] | 168 | config_name (str): Ethos-U NPU configuration from valid_npu_config_names |
| 169 | |
| 170 | arena_cache_size (int): Specifies arena cache size in bytes. If a value |
| 171 | greater than 0 is provided, this will be taken |
| 172 | as the cache size. If 0, the default values, as per |
| 173 | the NPU config requirements, are used. |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 174 | |
| 175 | Returns: |
| 176 | ------- |
| 177 | NPUConfig: An NPU config named tuple populated with defaults for the given |
| 178 | config name |
| 179 | """ |
| 180 | if config_name not in valid_npu_config_names: |
| 181 | raise ValueError(f""" |
| 182 | Invalid Ethos-U NPU configuration. |
| 183 | Select one from {valid_npu_config_names}. |
| 184 | """) |
| 185 | |
| 186 | strings_ids = ["ethos-u55-", "ethos-u65-"] |
| 187 | processor_ids = ["U55", "U65"] |
| 188 | prefix_ids = ["H", "Y"] |
| 189 | memory_modes = ["Shared_Sram", "Dedicated_Sram"] |
| 190 | system_configs = ["Ethos_U55_High_End_Embedded", "Ethos_U65_High_End"] |
Isabella Gottardi | 3acaaee | 2021-11-30 12:33:27 +0000 | [diff] [blame^] | 191 | memory_modes_arena = { |
| 192 | # For shared SRAM memory mode, we use the MPS3 SRAM size by default |
| 193 | "Shared_Sram" : mps3_max_sram_sz if arena_cache_size <= 0 else arena_cache_size, |
| 194 | # For dedicated SRAM memory mode, we do no override the arena size. This is expected to |
| 195 | # be defined in the vela configuration file instead. |
| 196 | "Dedicated_Sram" : None if arena_cache_size <= 0 else arena_cache_size |
| 197 | } |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 198 | |
| 199 | for i in range(len(strings_ids)): |
| 200 | if config_name.startswith(strings_ids[i]): |
| 201 | npu_config_id = config_name.replace(strings_ids[i], prefix_ids[i]) |
| 202 | return NPUConfig(config_name=config_name, |
| 203 | memory_mode=memory_modes[i], |
| 204 | system_config=system_configs[i], |
| 205 | ethos_u_npu_id=processor_ids[i], |
Isabella Gottardi | 3acaaee | 2021-11-30 12:33:27 +0000 | [diff] [blame^] | 206 | ethos_u_config_id=npu_config_id, |
| 207 | arena_cache_size=memory_modes_arena[memory_modes[i]]) |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 208 | |
| 209 | return None |
| 210 | |
| 211 | |
Isabella Gottardi | 3acaaee | 2021-11-30 12:33:27 +0000 | [diff] [blame^] | 212 | def set_up_resources(run_vela_on_models: bool = False, |
| 213 | additional_npu_config_names: list = [], |
| 214 | arena_cache_size: int = 0): |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 215 | """ |
| 216 | Helpers function that retrieve the output from a command. |
| 217 | |
| 218 | Parameters: |
| 219 | ---------- |
Isabella Gottardi | 3acaaee | 2021-11-30 12:33:27 +0000 | [diff] [blame^] | 220 | run_vela_on_models (bool): Specifies if run vela on downloaded models. |
| 221 | additional_npu_config_names(list): list of strings of Ethos-U NPU configs. |
| 222 | arena_cache_size (int): Specifies arena cache size in bytes. If a value |
| 223 | greater than 0 is provided, this will be taken |
| 224 | as the cache size. If 0, the default values, as per |
| 225 | the NPU config requirements, are used. |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 226 | """ |
| 227 | current_file_dir = os.path.dirname(os.path.abspath(__file__)) |
| 228 | download_dir = os.path.abspath(os.path.join(current_file_dir, "resources_downloaded")) |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 229 | |
| 230 | try: |
| 231 | # 1.1 Does the download dir exist? |
| 232 | os.mkdir(download_dir) |
| 233 | except OSError as e: |
| 234 | if e.errno == errno.EEXIST: |
| 235 | logging.info("'resources_downloaded' directory exists.") |
| 236 | else: |
| 237 | raise |
| 238 | |
| 239 | # 1.2 Does the virtual environment exist? |
| 240 | env_python = str(os.path.abspath(os.path.join(download_dir, "env", "bin", "python3"))) |
| 241 | env_activate = str(os.path.abspath(os.path.join(download_dir, "env", "bin", "activate"))) |
| 242 | if not os.path.isdir(os.path.join(download_dir, "env")): |
| 243 | os.chdir(download_dir) |
| 244 | # Create the virtual environment |
| 245 | command = "python3 -m venv env" |
| 246 | call_command(command) |
| 247 | commands = ["pip install --upgrade pip", "pip install --upgrade setuptools"] |
| 248 | for c in commands: |
| 249 | command = f"{env_python} -m {c}" |
| 250 | call_command(command) |
| 251 | os.chdir(current_file_dir) |
| 252 | # 1.3 Make sure to have all the requirement |
Richard Burton | 2de7ba0 | 2021-11-30 15:51:15 +0000 | [diff] [blame] | 253 | requirements = ["ethos-u-vela==3.2.0"] |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 254 | command = f"{env_python} -m pip freeze" |
| 255 | packages = call_command(command) |
| 256 | for req in requirements: |
| 257 | if req not in packages: |
| 258 | command = f"{env_python} -m pip install {req}" |
| 259 | call_command(command) |
| 260 | |
| 261 | # 2. Download models |
| 262 | for uc in json_uc_res: |
| 263 | try: |
| 264 | # Does the usecase_name download dir exist? |
| 265 | os.mkdir(os.path.join(download_dir, uc["use_case_name"])) |
| 266 | except OSError as e: |
| 267 | if e.errno != errno.EEXIST: |
| 268 | logging.error(f"Error creating {uc['use_case_name']} directory.") |
| 269 | raise |
| 270 | |
| 271 | for res in uc["resources"]: |
| 272 | res_name = res["name"] |
| 273 | res_url = res["url"] |
| 274 | if "sub_folder" in res: |
| 275 | try: |
| 276 | # Does the usecase_name/sub_folder download dir exist? |
| 277 | os.mkdir(os.path.join(download_dir, uc["use_case_name"], res["sub_folder"])) |
| 278 | except OSError as e: |
| 279 | if e.errno != errno.EEXIST: |
| 280 | logging.error(f"Error creating {uc['use_case_name']} / {res['sub_folder']} directory.") |
| 281 | raise |
| 282 | res_dst = os.path.join(download_dir, |
| 283 | uc["use_case_name"], |
| 284 | res["sub_folder"], |
| 285 | res_name) |
| 286 | else: |
| 287 | res_dst = os.path.join(download_dir, |
| 288 | uc["use_case_name"], |
| 289 | res_name) |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 290 | |
| 291 | if os.path.isfile(res_dst): |
| 292 | logging.info(f"File {res_dst} exists, skipping download.") |
| 293 | else: |
| 294 | try: |
| 295 | g = urllib.request.urlopen(res_url) |
| 296 | with open(res_dst, 'b+w') as f: |
| 297 | f.write(g.read()) |
| 298 | logging.info(f"- Downloaded {res_url} to {res_dst}.") |
| 299 | except URLError: |
| 300 | logging.error(f"URLError while downloading {res_url}.") |
| 301 | raise |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 302 | |
| 303 | # 3. Run vela on models in resources_downloaded |
| 304 | # New models will have same name with '_vela' appended. |
| 305 | # For example: |
| 306 | # original model: ds_cnn_clustered_int8.tflite |
alexander | 50a0650 | 2021-05-12 19:06:02 +0100 | [diff] [blame] | 307 | # after vela model: ds_cnn_clustered_int8_vela_H128.tflite |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 308 | # |
| 309 | # Note: To avoid to run vela twice on the same model, it's supposed that |
| 310 | # downloaded model names don't contain the 'vela' word. |
| 311 | if run_vela_on_models is True: |
| 312 | config_file = os.path.join(current_file_dir, "scripts", "vela", "default_vela.ini") |
| 313 | models = [os.path.join(dirpath, f) |
| 314 | for dirpath, dirnames, files in os.walk(download_dir) |
| 315 | for f in fnmatch.filter(files, '*.tflite') if "vela" not in f] |
| 316 | |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 317 | # Consolidate all config names while discarding duplicates: |
| 318 | config_names = list(set(default_npu_config_names + additional_npu_config_names)) |
| 319 | |
| 320 | # Get npu config tuple for each config name in a list: |
Isabella Gottardi | 3acaaee | 2021-11-30 12:33:27 +0000 | [diff] [blame^] | 321 | npu_configs = [get_default_npu_config_from_name(name, arena_cache_size) for name in config_names] |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 322 | |
| 323 | logging.info(f'All models will be optimised for these configs:') |
| 324 | for config in npu_configs: |
| 325 | logging.info(config) |
| 326 | |
Isabella Gottardi | 3acaaee | 2021-11-30 12:33:27 +0000 | [diff] [blame^] | 327 | optimisation_skipped = False |
| 328 | |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 329 | for model in models: |
| 330 | output_dir = os.path.dirname(model) |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 331 | # model name after compiling with vela is an initial model name + _vela suffix |
| 332 | vela_optimised_model_path = str(model).replace(".tflite", "_vela.tflite") |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 333 | |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 334 | for config in npu_configs: |
Isabella Gottardi | 3acaaee | 2021-11-30 12:33:27 +0000 | [diff] [blame^] | 335 | vela_command_arena_cache_size = "" |
| 336 | |
| 337 | if config.arena_cache_size: |
| 338 | vela_command_arena_cache_size = f"--arena-cache-size={config.arena_cache_size}" |
| 339 | |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 340 | vela_command = (f". {env_activate} && vela {model} " + |
| 341 | f"--accelerator-config={config.config_name} " + |
alexander | d475f09 | 2021-06-24 15:36:49 +0100 | [diff] [blame] | 342 | "--optimise Performance " + |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 343 | f"--config {config_file} " + |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 344 | f"--memory-mode={config.memory_mode} " + |
| 345 | f"--system-config={config.system_config} " + |
Liam Barry | b52b585 | 2021-11-15 11:41:40 +0000 | [diff] [blame] | 346 | f"--output-dir={output_dir} " + |
Isabella Gottardi | 3acaaee | 2021-11-30 12:33:27 +0000 | [diff] [blame^] | 347 | f"{vela_command_arena_cache_size}") |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 348 | |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 349 | # we want the name to include the configuration suffix. For example: vela_H128, |
| 350 | # vela_Y512 etc. |
| 351 | new_suffix = "_vela_" + config.ethos_u_config_id + '.tflite' |
| 352 | new_vela_optimised_model_path = ( |
| 353 | vela_optimised_model_path.replace("_vela.tflite", new_suffix)) |
Isabella Gottardi | 118f73e | 2021-09-16 17:54:35 +0100 | [diff] [blame] | 354 | |
| 355 | if os.path.isfile(new_vela_optimised_model_path): |
| 356 | logging.info(f"File {new_vela_optimised_model_path} exists, skipping optimisation.") |
Isabella Gottardi | 3acaaee | 2021-11-30 12:33:27 +0000 | [diff] [blame^] | 357 | optimisation_skipped = True |
Isabella Gottardi | 118f73e | 2021-09-16 17:54:35 +0100 | [diff] [blame] | 358 | continue |
| 359 | |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 360 | call_command(vela_command) |
Isabella Gottardi | 118f73e | 2021-09-16 17:54:35 +0100 | [diff] [blame] | 361 | |
| 362 | # rename default vela model |
| 363 | os.rename(vela_optimised_model_path, new_vela_optimised_model_path) |
| 364 | logging.info(f"Renaming {vela_optimised_model_path} to {new_vela_optimised_model_path}.") |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 365 | |
| 366 | |
Isabella Gottardi | 3acaaee | 2021-11-30 12:33:27 +0000 | [diff] [blame^] | 367 | # If any optimisation was skipped, show how to regenerate: |
| 368 | if optimisation_skipped: |
| 369 | logging.warning("One or more optimisations were skipped.") |
| 370 | logging.warning(f"To optimise all the models, please remove the directory {download_dir}.") |
| 371 | |
| 372 | |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 373 | if __name__ == '__main__': |
| 374 | parser = ArgumentParser() |
| 375 | parser.add_argument("--skip-vela", |
| 376 | help="Do not run Vela optimizer on downloaded models.", |
| 377 | action="store_true") |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 378 | parser.add_argument("--additional-ethos-u-config-name", |
| 379 | help=f"""Additional (non-default) configurations for Vela: |
| 380 | {valid_npu_config_names}""", |
| 381 | default=[], action="append") |
Liam Barry | b52b585 | 2021-11-15 11:41:40 +0000 | [diff] [blame] | 382 | parser.add_argument("--arena-cache-size", |
Isabella Gottardi | 3acaaee | 2021-11-30 12:33:27 +0000 | [diff] [blame^] | 383 | help="Arena cache size in bytes (if overriding the defaults)", |
Liam Barry | b52b585 | 2021-11-15 11:41:40 +0000 | [diff] [blame] | 384 | type=int, |
Isabella Gottardi | 3acaaee | 2021-11-30 12:33:27 +0000 | [diff] [blame^] | 385 | default=0) |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 386 | args = parser.parse_args() |
Kshitij Sisodia | b9e9c89 | 2021-05-27 13:57:35 +0100 | [diff] [blame] | 387 | |
Isabella Gottardi | 3acaaee | 2021-11-30 12:33:27 +0000 | [diff] [blame^] | 388 | if args.arena_cache_size < 0: |
| 389 | raise ArgumentTypeError('Arena cache size cannot not be less than 0') |
| 390 | |
Kshitij Sisodia | b9e9c89 | 2021-05-27 13:57:35 +0100 | [diff] [blame] | 391 | logging.basicConfig(filename='log_build_default.log', level=logging.DEBUG) |
| 392 | logging.getLogger().addHandler(logging.StreamHandler(sys.stdout)) |
| 393 | |
Liam Barry | b52b585 | 2021-11-15 11:41:40 +0000 | [diff] [blame] | 394 | set_up_resources(not args.skip_vela, |
| 395 | args.additional_ethos_u_config_name, |
| 396 | args.arena_cache_size) |