alexander | f4e2c47 | 2021-05-14 13:14:21 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +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 | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 16 | import logging |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame^] | 17 | import multiprocessing |
| 18 | import os |
| 19 | import shutil |
| 20 | import subprocess |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 21 | import sys |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame^] | 22 | import threading |
| 23 | from argparse import ArgumentDefaultsHelpFormatter |
| 24 | from argparse import ArgumentParser |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 25 | |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame^] | 26 | from set_up_default_resources import default_npu_config_names |
| 27 | from set_up_default_resources import get_default_npu_config_from_name |
| 28 | from set_up_default_resources import set_up_resources |
| 29 | from set_up_default_resources import valid_npu_config_names |
| 30 | |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 31 | |
Cisco Cervellera | 6ef76bd | 2021-11-25 18:32:27 +0000 | [diff] [blame] | 32 | class PipeLogging(threading.Thread): |
Cisco Cervellera | 6ef76bd | 2021-11-25 18:32:27 +0000 | [diff] [blame] | 33 | def __init__(self, log_level): |
| 34 | threading.Thread.__init__(self) |
| 35 | self.logLevel = log_level |
| 36 | self.fileRead, self.fileWrite = os.pipe() |
| 37 | self.pipeIn = os.fdopen(self.fileRead) |
| 38 | self.daemon = False |
| 39 | self.start() |
| 40 | |
| 41 | def fileno(self): |
| 42 | return self.fileWrite |
| 43 | |
| 44 | def run(self): |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame^] | 45 | for line in iter(self.pipeIn.readline, ""): |
| 46 | logging.log(self.logLevel, line.strip("\n")) |
Cisco Cervellera | 6ef76bd | 2021-11-25 18:32:27 +0000 | [diff] [blame] | 47 | |
| 48 | self.pipeIn.close() |
| 49 | |
| 50 | def close(self): |
| 51 | os.close(self.fileWrite) |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 52 | |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame^] | 53 | |
| 54 | def run( |
| 55 | toolchain: str, |
| 56 | download_resources: bool, |
| 57 | run_vela_on_models: bool, |
| 58 | npu_config_name: str, |
| 59 | make_jobs: int, |
| 60 | make_verbose: bool, |
| 61 | ): |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 62 | """ |
| 63 | Run the helpers scripts. |
| 64 | |
| 65 | Parameters: |
| 66 | ---------- |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 67 | toolchain (str) : Specifies if 'gnu' or 'arm' toolchain needs to be used. |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 68 | download_resources (bool): Specifies if 'Download resources' step is performed. |
| 69 | run_vela_on_models (bool): Only if `download_resources` is True, specifies if run vela on downloaded models. |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 70 | npu_config_name(str) : Ethos-U NPU configuration name. See "valid_npu_config_names" |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 71 | """ |
| 72 | |
| 73 | current_file_dir = os.path.dirname(os.path.abspath(__file__)) |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 74 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 75 | # 1. Make sure the toolchain is supported, and set the right one here |
| 76 | supported_toolchain_ids = ["gnu", "arm"] |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame^] | 77 | assert ( |
| 78 | toolchain in supported_toolchain_ids |
| 79 | ), f"Toolchain must be from {supported_toolchain_ids}" |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 80 | if toolchain == "arm": |
| 81 | toolchain_file_name = "bare-metal-armclang.cmake" |
| 82 | elif toolchain == "gnu": |
| 83 | toolchain_file_name = "bare-metal-gcc.cmake" |
| 84 | |
| 85 | # 2. Download models if specified |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 86 | if download_resources is True: |
| 87 | logging.info("Downloading resources.") |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame^] | 88 | set_up_resources( |
| 89 | run_vela_on_models=run_vela_on_models, |
| 90 | additional_npu_config_names=[npu_config_name], |
| 91 | ) |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 92 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 93 | # 3. Build default configuration |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 94 | logging.info("Building default configuration.") |
| 95 | target_platform = "mps3" |
| 96 | target_subsystem = "sse-300" |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 97 | ethos_u_cfg = get_default_npu_config_from_name(npu_config_name) |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame^] | 98 | build_dir = os.path.join( |
| 99 | current_file_dir, |
| 100 | f"cmake-build-{target_platform}-{target_subsystem}-{npu_config_name}-{toolchain}", |
| 101 | ) |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 102 | try: |
| 103 | os.mkdir(build_dir) |
| 104 | except FileExistsError: |
| 105 | # Directory already exists, clean it |
| 106 | for filename in os.listdir(build_dir): |
| 107 | filepath = os.path.join(build_dir, filename) |
| 108 | try: |
| 109 | if os.path.isfile(filepath) or os.path.islink(filepath): |
| 110 | os.unlink(filepath) |
| 111 | elif os.path.isdir(filepath): |
| 112 | shutil.rmtree(filepath) |
| 113 | except Exception as e: |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame^] | 114 | logging.error(f"Failed to delete {filepath}. Reason: {e}") |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 115 | |
Cisco Cervellera | 6ef76bd | 2021-11-25 18:32:27 +0000 | [diff] [blame] | 116 | logpipe = PipeLogging(logging.INFO) |
| 117 | |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 118 | os.chdir(build_dir) |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame^] | 119 | cmake_toolchain_file = os.path.join( |
| 120 | current_file_dir, "scripts", "cmake", "toolchains", toolchain_file_name |
| 121 | ) |
| 122 | cmake_command = ( |
| 123 | f"cmake .. -DTARGET_PLATFORM={target_platform}" |
| 124 | + f" -DTARGET_SUBSYSTEM={target_subsystem}" |
| 125 | + f" -DCMAKE_TOOLCHAIN_FILE={cmake_toolchain_file}" |
| 126 | + f" -DETHOS_U_NPU_ID={ethos_u_cfg.ethos_u_npu_id}" |
| 127 | + f" -DETHOS_U_NPU_CONFIG_ID={ethos_u_cfg.ethos_u_config_id}" |
| 128 | ) |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 129 | |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 130 | logging.info(cmake_command) |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame^] | 131 | state = subprocess.run( |
| 132 | cmake_command, shell=True, stdout=logpipe, stderr=subprocess.STDOUT |
| 133 | ) |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 134 | |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame^] | 135 | make_command = f"make -j{make_jobs}" |
| 136 | if make_verbose: |
Cisco Cervellera | 6ef76bd | 2021-11-25 18:32:27 +0000 | [diff] [blame] | 137 | make_command += " VERBOSE=1" |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 138 | logging.info(make_command) |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame^] | 139 | state = subprocess.run( |
| 140 | make_command, shell=True, stdout=logpipe, stderr=subprocess.STDOUT |
| 141 | ) |
Cisco Cervellera | 6ef76bd | 2021-11-25 18:32:27 +0000 | [diff] [blame] | 142 | |
| 143 | logpipe.close() |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 144 | |
| 145 | |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame^] | 146 | if __name__ == "__main__": |
Cisco Cervellera | 6ef76bd | 2021-11-25 18:32:27 +0000 | [diff] [blame] | 147 | parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter) |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame^] | 148 | parser.add_argument( |
| 149 | "--toolchain", |
| 150 | default="gnu", |
| 151 | help=""" |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 152 | Specify the toolchain to use (Arm or GNU). |
| 153 | Options are [gnu, arm]; default is gnu. |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame^] | 154 | """, |
| 155 | ) |
| 156 | parser.add_argument( |
| 157 | "--skip-download", |
| 158 | help="Do not download resources: models and test vectors", |
| 159 | action="store_true", |
| 160 | ) |
| 161 | parser.add_argument( |
| 162 | "--skip-vela", |
| 163 | help="Do not run Vela optimizer on downloaded models.", |
| 164 | action="store_true", |
| 165 | ) |
| 166 | parser.add_argument( |
| 167 | "--npu-config-name", |
| 168 | help=f"""Arm Ethos-U configuration to build for. Choose from: |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 169 | {valid_npu_config_names}""", |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame^] | 170 | default=default_npu_config_names[0], |
| 171 | ) |
| 172 | parser.add_argument( |
| 173 | "--make-jobs", |
| 174 | help="Number of jobs to run with make", |
| 175 | default=multiprocessing.cpu_count(), |
| 176 | ) |
| 177 | parser.add_argument( |
| 178 | "--make-verbose", help="Make runs with VERBOSE=1", action="store_true" |
| 179 | ) |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 180 | args = parser.parse_args() |
Kshitij Sisodia | b9e9c89 | 2021-05-27 13:57:35 +0100 | [diff] [blame] | 181 | |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame^] | 182 | logging.basicConfig( |
| 183 | filename="log_build_default.log", level=logging.DEBUG, filemode="w" |
| 184 | ) |
Kshitij Sisodia | b9e9c89 | 2021-05-27 13:57:35 +0100 | [diff] [blame] | 185 | logging.getLogger().addHandler(logging.StreamHandler(sys.stdout)) |
| 186 | |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame^] | 187 | run( |
| 188 | args.toolchain.lower(), |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 189 | not args.skip_download, |
| 190 | not args.skip_vela, |
Cisco Cervellera | 6ef76bd | 2021-11-25 18:32:27 +0000 | [diff] [blame] | 191 | args.npu_config_name, |
| 192 | args.make_jobs, |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame^] | 193 | args.make_verbose, |
| 194 | ) |