blob: 5adff22b555ee7ed148e1627b3f47aa194a3aa02 [file] [log] [blame]
alexanderf4e2c472021-05-14 13:14:21 +01001#!/usr/bin/env python3
Hugues Kamba-Mpianafea932f2024-03-04 16:01:55 +00002# SPDX-FileCopyrightText: Copyright 2021-2024 Arm Limited and/or its affiliates <open-source-office@arm.com>
Isabella Gottardi2181d0a2021-04-07 09:27:38 +01003# 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.
Alex Tawsedaba3cf2023-09-29 15:55:38 +010016"""
17Script to build the ML Embedded Evaluation kit using default configuration
18"""
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010019import logging
Isabella Gottardiee4920b2022-02-25 14:29:32 +000020import multiprocessing
21import os
22import shutil
23import subprocess
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010024import sys
Isabella Gottardiee4920b2022-02-25 14:29:32 +000025import threading
26from argparse import ArgumentDefaultsHelpFormatter
27from argparse import ArgumentParser
Hugues Kamba-Mpianafea932f2024-03-04 16:01:55 +000028from dataclasses import dataclass
Richard Burton17069622022-03-17 10:54:26 +000029from pathlib import Path
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010030
Hugues Kamba-Mpianafea932f2024-03-04 16:01:55 +000031from set_up_default_resources import SetupArgs
Isabella Gottardiee4920b2022-02-25 14:29:32 +000032from set_up_default_resources import default_npu_config_names
33from set_up_default_resources import get_default_npu_config_from_name
34from set_up_default_resources import set_up_resources
35from set_up_default_resources import valid_npu_config_names
36
Hugues Kamba-Mpianafea932f2024-03-04 16:01:55 +000037
38@dataclass(frozen=True)
39class BuildArgs:
40 """
41 Args used to build the project.
42
43 Attributes:
44 toolchain (str) : Specifies if 'gnu' or 'arm' toolchain needs to be used.
45 download_resources (bool) : Specifies if 'Download resources' step is performed.
46 run_vela_on_models (bool) : Only if `download_resources` is True, specifies whether to
47 run Vela on downloaded models.
48 npu_config_name (str) : Ethos-U NPU configuration name. See "valid_npu_config_names"
49 make_jobs (int) : The number of make jobs to use (`-j` flag).
50 make_verbose (bool) : Runs make with VERBOSE=1.
51 """
52 toolchain: str
53 download_resources: bool
54 run_vela_on_models: bool
55 npu_config_name: str
56 make_jobs: int
57 make_verbose: bool
Alex Tawsedaba3cf2023-09-29 15:55:38 +010058
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010059
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +000060class PipeLogging(threading.Thread):
Alex Tawsedaba3cf2023-09-29 15:55:38 +010061 """
62 Class used to log stdout from subprocesses
63 """
64
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +000065 def __init__(self, log_level):
66 threading.Thread.__init__(self)
Alex Tawsedaba3cf2023-09-29 15:55:38 +010067 self.log_level = log_level
68 self.file_read, self.file_write = os.pipe()
69 self.pipe_in = os.fdopen(self.file_read)
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +000070 self.daemon = False
71 self.start()
72
73 def fileno(self):
Alex Tawsedaba3cf2023-09-29 15:55:38 +010074 """
75 Get self.file_write
76
77 Returns
78 -------
79 self.file_write
80 """
81 return self.file_write
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +000082
83 def run(self):
Alex Tawsedaba3cf2023-09-29 15:55:38 +010084 """
85 Log the contents of self.pipe_in
86 """
87 for line in iter(self.pipe_in.readline, ""):
88 logging.log(self.log_level, line.strip("\n"))
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +000089
Alex Tawsedaba3cf2023-09-29 15:55:38 +010090 self.pipe_in.close()
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +000091
92 def close(self):
Alex Tawsedaba3cf2023-09-29 15:55:38 +010093 """
94 Close the pipe
95 """
96 os.close(self.file_write)
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010097
Isabella Gottardiee4920b2022-02-25 14:29:32 +000098
Alex Tawsedaba3cf2023-09-29 15:55:38 +010099def get_toolchain_file_name(toolchain: str) -> str:
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100100 """
Alex Tawsedaba3cf2023-09-29 15:55:38 +0100101 Get the name of the toolchain file for the toolchain.
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100102
Alex Tawsedaba3cf2023-09-29 15:55:38 +0100103 Parameters
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100104 ----------
Alex Tawsedaba3cf2023-09-29 15:55:38 +0100105 toolchain : name of the specified toolchain
106
107 Returns
108 -------
109 name of the toolchain file corresponding to the specified
110 toolchain
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100111 """
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100112 if toolchain == "arm":
Alex Tawsedaba3cf2023-09-29 15:55:38 +0100113 return "bare-metal-armclang.cmake"
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100114
Alex Tawsedaba3cf2023-09-29 15:55:38 +0100115 if toolchain == "gnu":
116 return "bare-metal-gcc.cmake"
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100117
Alex Tawsedaba3cf2023-09-29 15:55:38 +0100118 raise ValueError("Toolchain must be one of: gnu, arm")
119
120
121def prep_build_dir(
122 current_file_dir: Path,
123 target_platform: str,
124 target_subsystem: str,
125 npu_config_name: str,
126 toolchain: str
127) -> Path:
128 """
129 Create or clean the build directory for this project.
130
131 Parameters
132 ----------
133 current_file_dir : The current directory of the running script
134 target_platform : The name of the target platform, e.g. "mps3"
135 target_subsystem: : The name of the target subsystem, e.g. "sse-300"
136 npu_config_name : The NPU config name, e.g. "ethos-u55-32"
137 toolchain : The name of the specified toolchain, e.g."arm"
138
139 Returns
140 -------
141 The path to the build directory
142 """
143 build_dir = (
144 current_file_dir /
145 f"cmake-build-{target_platform}-{target_subsystem}-{npu_config_name}-{toolchain}"
146 )
Richard Burton17069622022-03-17 10:54:26 +0000147
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100148 try:
Richard Burton17069622022-03-17 10:54:26 +0000149 build_dir.mkdir()
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100150 except FileExistsError:
Richard Burton17069622022-03-17 10:54:26 +0000151 # Directory already exists, clean it.
152 for filepath in build_dir.iterdir():
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100153 try:
Richard Burton17069622022-03-17 10:54:26 +0000154 if filepath.is_file() or filepath.is_symlink():
155 filepath.unlink()
156 elif filepath.is_dir():
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100157 shutil.rmtree(filepath)
Alex Tawsedaba3cf2023-09-29 15:55:38 +0100158 except OSError as err:
159 logging.error("Failed to delete %s. Reason: %s", filepath, err)
160
161 return build_dir
162
163
164def run_command(
165 command: str,
166 logpipe: PipeLogging,
167 fail_message: str
168):
169 """
170 Run a command and exit upon failure.
171
172 Parameters
173 ----------
174 command : The command to run
175 logpipe : The PipeLogging object to capture stdout
176 fail_message : The message to log upon a non-zero exit code
177 """
178 logging.info("\n\n\n%s\n\n\n", command)
179
180 try:
181 subprocess.run(
182 command, check=True, shell=True, stdout=logpipe, stderr=subprocess.STDOUT
183 )
184 except subprocess.CalledProcessError as err:
185 logging.error(fail_message)
186 logpipe.close()
187 sys.exit(err.returncode)
188
189
190def run(args: BuildArgs):
191 """
192 Run the helpers scripts.
193
194 Parameters:
195 ----------
Hugues Kamba-Mpianafea932f2024-03-04 16:01:55 +0000196 args (BuildArgs) : Arguments used to build the project
Alex Tawsedaba3cf2023-09-29 15:55:38 +0100197 """
198
199 current_file_dir = Path(__file__).parent.resolve()
200
201 # 1. Make sure the toolchain is supported, and set the right one here
202 toolchain_file_name = get_toolchain_file_name(args.toolchain)
203
204 # 2. Download models if specified
205 if args.download_resources is True:
206 logging.info("Downloading resources.")
Hugues Kamba-Mpianafea932f2024-03-04 16:01:55 +0000207 setup_args = SetupArgs(
Alex Tawsedaba3cf2023-09-29 15:55:38 +0100208 run_vela_on_models=args.run_vela_on_models,
Hugues Kamba-Mpianafea932f2024-03-04 16:01:55 +0000209 additional_npu_config_names=[args.npu_config_name],
210 additional_requirements_file=current_file_dir / "scripts" / "py" / "requirements.txt",
211 use_case_resources_file=current_file_dir / "scripts" / "py" / "use_case_resources.json",
Alex Tawsedaba3cf2023-09-29 15:55:38 +0100212 )
Hugues Kamba-Mpianafea932f2024-03-04 16:01:55 +0000213 env_path = set_up_resources(setup_args)
Alex Tawsedaba3cf2023-09-29 15:55:38 +0100214
215 # 3. Build default configuration
216 logging.info("Building default configuration.")
217 target_platform = "mps3"
218 target_subsystem = "sse-300"
219
220 build_dir = prep_build_dir(
221 current_file_dir,
222 target_platform,
223 target_subsystem,
224 args.npu_config_name,
225 args.toolchain
226 )
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100227
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +0000228 logpipe = PipeLogging(logging.INFO)
229
Alex Tawsedaba3cf2023-09-29 15:55:38 +0100230 cmake_toolchain_file = (
231 current_file_dir /
232 "scripts" /
233 "cmake" /
234 "toolchains" /
235 toolchain_file_name
236 )
237 ethos_u_cfg = get_default_npu_config_from_name(args.npu_config_name)
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +0100238 cmake_path = env_path / "bin" / "cmake"
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000239 cmake_command = (
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +0100240 f"{cmake_path} -B {build_dir} -DTARGET_PLATFORM={target_platform}"
Alex Tawsedaba3cf2023-09-29 15:55:38 +0100241 f" -DTARGET_SUBSYSTEM={target_subsystem}"
242 f" -DCMAKE_TOOLCHAIN_FILE={cmake_toolchain_file}"
243 f" -DETHOS_U_NPU_ID={ethos_u_cfg.ethos_u_npu_id}"
244 f" -DETHOS_U_NPU_CONFIG_ID={ethos_u_cfg.ethos_u_config_id}"
245 " -DTENSORFLOW_LITE_MICRO_CLEAN_DOWNLOADS=ON"
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000246 )
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100247
Alex Tawsedaba3cf2023-09-29 15:55:38 +0100248 run_command(cmake_command, logpipe, fail_message="Failed to configure the project.")
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100249
Alex Tawsedaba3cf2023-09-29 15:55:38 +0100250 make_command = f"{cmake_path} --build {build_dir} -j{args.make_jobs}"
251 if args.make_verbose:
Richard Burton2bf6a702023-06-19 14:51:36 +0100252 make_command += " --verbose"
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +0000253
Alex Tawsedaba3cf2023-09-29 15:55:38 +0100254 run_command(make_command, logpipe, fail_message="Failed to build project.")
Kshitij Sisodia1bd434e2023-03-17 10:40:45 +0000255
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +0000256 logpipe.close()
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100257
258
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000259if __name__ == "__main__":
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +0000260 parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000261 parser.add_argument(
262 "--toolchain",
263 default="gnu",
264 help="""
Richard Burton17069622022-03-17 10:54:26 +0000265 Specify the toolchain to use (Arm or GNU).
266 Options are [gnu, arm]; default is gnu.
267 """,
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000268 )
269 parser.add_argument(
270 "--skip-download",
271 help="Do not download resources: models and test vectors",
272 action="store_true",
273 )
274 parser.add_argument(
275 "--skip-vela",
276 help="Do not run Vela optimizer on downloaded models.",
277 action="store_true",
278 )
279 parser.add_argument(
280 "--npu-config-name",
281 help=f"""Arm Ethos-U configuration to build for. Choose from:
Richard Burton17069622022-03-17 10:54:26 +0000282 {valid_npu_config_names}""",
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000283 default=default_npu_config_names[0],
284 )
285 parser.add_argument(
286 "--make-jobs",
287 help="Number of jobs to run with make",
288 default=multiprocessing.cpu_count(),
289 )
290 parser.add_argument(
291 "--make-verbose", help="Make runs with VERBOSE=1", action="store_true"
292 )
Alex Tawsedaba3cf2023-09-29 15:55:38 +0100293 parsed_args = parser.parse_args()
Kshitij Sisodiab9e9c892021-05-27 13:57:35 +0100294
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000295 logging.basicConfig(
296 filename="log_build_default.log", level=logging.DEBUG, filemode="w"
297 )
Kshitij Sisodiab9e9c892021-05-27 13:57:35 +0100298 logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
299
Alex Tawsedaba3cf2023-09-29 15:55:38 +0100300 build_args = BuildArgs(
301 toolchain=parsed_args.toolchain.lower(),
302 download_resources=not parsed_args.skip_download,
303 run_vela_on_models=not parsed_args.skip_vela,
304 npu_config_name=parsed_args.npu_config_name,
305 make_jobs=parsed_args.make_jobs,
306 make_verbose=parsed_args.make_verbose
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000307 )
Alex Tawsedaba3cf2023-09-29 15:55:38 +0100308
309 run(build_args)