blob: e37a9adcc999b9d9f04502082ea696b73ed4d57a [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.")
Isabella Gottardiee4920b2022-02-25 14:29:32 +000089 set_up_resources(
90 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
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100117 os.chdir(build_dir)
Richard Burton17069622022-03-17 10:54:26 +0000118 cmake_toolchain_file = current_file_dir / "scripts" / "cmake" / "toolchains" / toolchain_file_name
119
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000120 cmake_command = (
121 f"cmake .. -DTARGET_PLATFORM={target_platform}"
122 + f" -DTARGET_SUBSYSTEM={target_subsystem}"
123 + f" -DCMAKE_TOOLCHAIN_FILE={cmake_toolchain_file}"
124 + f" -DETHOS_U_NPU_ID={ethos_u_cfg.ethos_u_npu_id}"
125 + f" -DETHOS_U_NPU_CONFIG_ID={ethos_u_cfg.ethos_u_config_id}"
126 )
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100127
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100128 logging.info(cmake_command)
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000129 state = subprocess.run(
130 cmake_command, shell=True, stdout=logpipe, stderr=subprocess.STDOUT
131 )
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100132
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000133 make_command = f"make -j{make_jobs}"
134 if make_verbose:
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +0000135 make_command += " VERBOSE=1"
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100136 logging.info(make_command)
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000137 state = subprocess.run(
138 make_command, shell=True, stdout=logpipe, stderr=subprocess.STDOUT
139 )
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +0000140
141 logpipe.close()
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100142
143
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000144if __name__ == "__main__":
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +0000145 parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000146 parser.add_argument(
147 "--toolchain",
148 default="gnu",
149 help="""
Richard Burton17069622022-03-17 10:54:26 +0000150 Specify the toolchain to use (Arm or GNU).
151 Options are [gnu, arm]; default is gnu.
152 """,
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000153 )
154 parser.add_argument(
155 "--skip-download",
156 help="Do not download resources: models and test vectors",
157 action="store_true",
158 )
159 parser.add_argument(
160 "--skip-vela",
161 help="Do not run Vela optimizer on downloaded models.",
162 action="store_true",
163 )
164 parser.add_argument(
165 "--npu-config-name",
166 help=f"""Arm Ethos-U configuration to build for. Choose from:
Richard Burton17069622022-03-17 10:54:26 +0000167 {valid_npu_config_names}""",
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000168 default=default_npu_config_names[0],
169 )
170 parser.add_argument(
171 "--make-jobs",
172 help="Number of jobs to run with make",
173 default=multiprocessing.cpu_count(),
174 )
175 parser.add_argument(
176 "--make-verbose", help="Make runs with VERBOSE=1", action="store_true"
177 )
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100178 args = parser.parse_args()
Kshitij Sisodiab9e9c892021-05-27 13:57:35 +0100179
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000180 logging.basicConfig(
181 filename="log_build_default.log", level=logging.DEBUG, filemode="w"
182 )
Kshitij Sisodiab9e9c892021-05-27 13:57:35 +0100183 logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
184
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000185 run(
186 args.toolchain.lower(),
Kshitij Sisodia3be26232021-10-29 12:29:06 +0100187 not args.skip_download,
188 not args.skip_vela,
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +0000189 args.npu_config_name,
190 args.make_jobs,
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000191 args.make_verbose,
192 )