blob: 3a308a985a2e2dcdaada53f331dbd82a3fccafaf [file] [log] [blame]
alexanderf4e2c472021-05-14 13:14:21 +01001#!/usr/bin/env python3
Isabella Gottardiee4920b2022-02-25 14:29:32 +00002# Copyright (c) 2021-2022 Arm Limited. All rights reserved.
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.
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010016import logging
Isabella Gottardiee4920b2022-02-25 14:29:32 +000017import multiprocessing
18import os
19import shutil
20import subprocess
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010021import sys
Isabella Gottardiee4920b2022-02-25 14:29:32 +000022import threading
23from argparse import ArgumentDefaultsHelpFormatter
24from argparse import ArgumentParser
Richard Burton17069622022-03-17 10:54:26 +000025from pathlib import Path
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010026
Isabella Gottardiee4920b2022-02-25 14:29:32 +000027from set_up_default_resources import default_npu_config_names
28from set_up_default_resources import get_default_npu_config_from_name
29from set_up_default_resources import set_up_resources
30from set_up_default_resources import valid_npu_config_names
31
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010032
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +000033class PipeLogging(threading.Thread):
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +000034 def __init__(self, log_level):
35 threading.Thread.__init__(self)
36 self.logLevel = log_level
37 self.fileRead, self.fileWrite = os.pipe()
38 self.pipeIn = os.fdopen(self.fileRead)
39 self.daemon = False
40 self.start()
41
42 def fileno(self):
43 return self.fileWrite
44
45 def run(self):
Isabella Gottardiee4920b2022-02-25 14:29:32 +000046 for line in iter(self.pipeIn.readline, ""):
47 logging.log(self.logLevel, line.strip("\n"))
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +000048
49 self.pipeIn.close()
50
51 def close(self):
52 os.close(self.fileWrite)
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010053
Isabella Gottardiee4920b2022-02-25 14:29:32 +000054
55def run(
56 toolchain: str,
57 download_resources: bool,
58 run_vela_on_models: bool,
59 npu_config_name: str,
60 make_jobs: int,
61 make_verbose: bool,
62):
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010063 """
64 Run the helpers scripts.
65
66 Parameters:
67 ----------
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010068 toolchain (str) : Specifies if 'gnu' or 'arm' toolchain needs to be used.
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010069 download_resources (bool): Specifies if 'Download resources' step is performed.
70 run_vela_on_models (bool): Only if `download_resources` is True, specifies if run vela on downloaded models.
Kshitij Sisodia3be26232021-10-29 12:29:06 +010071 npu_config_name(str) : Ethos-U NPU configuration name. See "valid_npu_config_names"
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010072 """
73
Richard Burton17069622022-03-17 10:54:26 +000074 current_file_dir = Path(__file__).parent.resolve()
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010075
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010076 # 1. Make sure the toolchain is supported, and set the right one here
77 supported_toolchain_ids = ["gnu", "arm"]
Isabella Gottardiee4920b2022-02-25 14:29:32 +000078 assert (
79 toolchain in supported_toolchain_ids
80 ), f"Toolchain must be from {supported_toolchain_ids}"
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010081 if toolchain == "arm":
82 toolchain_file_name = "bare-metal-armclang.cmake"
83 elif toolchain == "gnu":
84 toolchain_file_name = "bare-metal-gcc.cmake"
85
86 # 2. Download models if specified
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010087 if download_resources is True:
88 logging.info("Downloading resources.")
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +010089 (download_dir, env_path) = set_up_resources(
Isabella Gottardiee4920b2022-02-25 14:29:32 +000090 run_vela_on_models=run_vela_on_models,
91 additional_npu_config_names=[npu_config_name],
Richard Burton17069622022-03-17 10:54:26 +000092 additional_requirements_file=current_file_dir / "scripts" / "py" / "requirements.txt"
Isabella Gottardiee4920b2022-02-25 14:29:32 +000093 )
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010094
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010095 # 3. Build default configuration
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010096 logging.info("Building default configuration.")
97 target_platform = "mps3"
98 target_subsystem = "sse-300"
Kshitij Sisodia3be26232021-10-29 12:29:06 +010099 ethos_u_cfg = get_default_npu_config_from_name(npu_config_name)
Richard Burton17069622022-03-17 10:54:26 +0000100 build_dir = current_file_dir / f"cmake-build-{target_platform}-{target_subsystem}-{npu_config_name}-{toolchain}"
101
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100102 try:
Richard Burton17069622022-03-17 10:54:26 +0000103 build_dir.mkdir()
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100104 except FileExistsError:
Richard Burton17069622022-03-17 10:54:26 +0000105 # Directory already exists, clean it.
106 for filepath in build_dir.iterdir():
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100107 try:
Richard Burton17069622022-03-17 10:54:26 +0000108 if filepath.is_file() or filepath.is_symlink():
109 filepath.unlink()
110 elif filepath.is_dir():
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100111 shutil.rmtree(filepath)
112 except Exception as e:
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000113 logging.error(f"Failed to delete {filepath}. Reason: {e}")
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100114
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +0000115 logpipe = PipeLogging(logging.INFO)
116
Richard Burton17069622022-03-17 10:54:26 +0000117 cmake_toolchain_file = current_file_dir / "scripts" / "cmake" / "toolchains" / toolchain_file_name
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +0100118 cmake_path = env_path / "bin" / "cmake"
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000119 cmake_command = (
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +0100120 f"{cmake_path} -B {build_dir} -DTARGET_PLATFORM={target_platform}"
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000121 + f" -DTARGET_SUBSYSTEM={target_subsystem}"
122 + f" -DCMAKE_TOOLCHAIN_FILE={cmake_toolchain_file}"
123 + f" -DETHOS_U_NPU_ID={ethos_u_cfg.ethos_u_npu_id}"
124 + f" -DETHOS_U_NPU_CONFIG_ID={ethos_u_cfg.ethos_u_config_id}"
125 )
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100126
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +0100127 logging.info(f"\n\n\n{cmake_command}\n\n\n")
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000128 state = subprocess.run(
129 cmake_command, shell=True, stdout=logpipe, stderr=subprocess.STDOUT
130 )
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100131
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +0100132 make_command = f"{cmake_path} --build {build_dir} -j{make_jobs}"
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000133 if make_verbose:
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +0100134 make_command += "--verbose"
135 logging.info(f"\n\n\n{make_command}\n\n\n")
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000136 state = subprocess.run(
137 make_command, shell=True, stdout=logpipe, stderr=subprocess.STDOUT
138 )
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +0000139
140 logpipe.close()
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100141
142
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000143if __name__ == "__main__":
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +0000144 parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000145 parser.add_argument(
146 "--toolchain",
147 default="gnu",
148 help="""
Richard Burton17069622022-03-17 10:54:26 +0000149 Specify the toolchain to use (Arm or GNU).
150 Options are [gnu, arm]; default is gnu.
151 """,
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000152 )
153 parser.add_argument(
154 "--skip-download",
155 help="Do not download resources: models and test vectors",
156 action="store_true",
157 )
158 parser.add_argument(
159 "--skip-vela",
160 help="Do not run Vela optimizer on downloaded models.",
161 action="store_true",
162 )
163 parser.add_argument(
164 "--npu-config-name",
165 help=f"""Arm Ethos-U configuration to build for. Choose from:
Richard Burton17069622022-03-17 10:54:26 +0000166 {valid_npu_config_names}""",
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000167 default=default_npu_config_names[0],
168 )
169 parser.add_argument(
170 "--make-jobs",
171 help="Number of jobs to run with make",
172 default=multiprocessing.cpu_count(),
173 )
174 parser.add_argument(
175 "--make-verbose", help="Make runs with VERBOSE=1", action="store_true"
176 )
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100177 args = parser.parse_args()
Kshitij Sisodiab9e9c892021-05-27 13:57:35 +0100178
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000179 logging.basicConfig(
180 filename="log_build_default.log", level=logging.DEBUG, filemode="w"
181 )
Kshitij Sisodiab9e9c892021-05-27 13:57:35 +0100182 logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
183
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000184 run(
185 args.toolchain.lower(),
Kshitij Sisodia3be26232021-10-29 12:29:06 +0100186 not args.skip_download,
187 not args.skip_vela,
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +0000188 args.npu_config_name,
189 args.make_jobs,
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000190 args.make_verbose,
191 )