blob: 2e95528ed715a5efbfe67b283890db93046af8c6 [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
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010025
Isabella Gottardiee4920b2022-02-25 14:29:32 +000026from set_up_default_resources import default_npu_config_names
27from set_up_default_resources import get_default_npu_config_from_name
28from set_up_default_resources import set_up_resources
29from set_up_default_resources import valid_npu_config_names
30
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010031
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +000032class PipeLogging(threading.Thread):
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +000033 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 Gottardiee4920b2022-02-25 14:29:32 +000045 for line in iter(self.pipeIn.readline, ""):
46 logging.log(self.logLevel, line.strip("\n"))
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +000047
48 self.pipeIn.close()
49
50 def close(self):
51 os.close(self.fileWrite)
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010052
Isabella Gottardiee4920b2022-02-25 14:29:32 +000053
54def 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 Gottardi2181d0a2021-04-07 09:27:38 +010062 """
63 Run the helpers scripts.
64
65 Parameters:
66 ----------
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010067 toolchain (str) : Specifies if 'gnu' or 'arm' toolchain needs to be used.
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010068 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 Sisodia3be26232021-10-29 12:29:06 +010070 npu_config_name(str) : Ethos-U NPU configuration name. See "valid_npu_config_names"
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010071 """
72
73 current_file_dir = os.path.dirname(os.path.abspath(__file__))
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010074
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010075 # 1. Make sure the toolchain is supported, and set the right one here
76 supported_toolchain_ids = ["gnu", "arm"]
Isabella Gottardiee4920b2022-02-25 14:29:32 +000077 assert (
78 toolchain in supported_toolchain_ids
79 ), f"Toolchain must be from {supported_toolchain_ids}"
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010080 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 Gottardi2181d0a2021-04-07 09:27:38 +010086 if download_resources is True:
87 logging.info("Downloading resources.")
Isabella Gottardiee4920b2022-02-25 14:29:32 +000088 set_up_resources(
89 run_vela_on_models=run_vela_on_models,
90 additional_npu_config_names=[npu_config_name],
91 )
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010092
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010093 # 3. Build default configuration
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010094 logging.info("Building default configuration.")
95 target_platform = "mps3"
96 target_subsystem = "sse-300"
Kshitij Sisodia3be26232021-10-29 12:29:06 +010097 ethos_u_cfg = get_default_npu_config_from_name(npu_config_name)
Isabella Gottardiee4920b2022-02-25 14:29:32 +000098 build_dir = os.path.join(
99 current_file_dir,
100 f"cmake-build-{target_platform}-{target_subsystem}-{npu_config_name}-{toolchain}",
101 )
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100102 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 Gottardiee4920b2022-02-25 14:29:32 +0000114 logging.error(f"Failed to delete {filepath}. Reason: {e}")
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100115
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +0000116 logpipe = PipeLogging(logging.INFO)
117
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100118 os.chdir(build_dir)
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000119 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 Sisodiaf9c19ea2021-05-07 16:08:14 +0100129
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100130 logging.info(cmake_command)
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000131 state = subprocess.run(
132 cmake_command, shell=True, stdout=logpipe, stderr=subprocess.STDOUT
133 )
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100134
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000135 make_command = f"make -j{make_jobs}"
136 if make_verbose:
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +0000137 make_command += " VERBOSE=1"
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100138 logging.info(make_command)
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000139 state = subprocess.run(
140 make_command, shell=True, stdout=logpipe, stderr=subprocess.STDOUT
141 )
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +0000142
143 logpipe.close()
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100144
145
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000146if __name__ == "__main__":
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +0000147 parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000148 parser.add_argument(
149 "--toolchain",
150 default="gnu",
151 help="""
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100152 Specify the toolchain to use (Arm or GNU).
153 Options are [gnu, arm]; default is gnu.
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000154 """,
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 Sisodia3be26232021-10-29 12:29:06 +0100169 {valid_npu_config_names}""",
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000170 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 Gottardi2181d0a2021-04-07 09:27:38 +0100180 args = parser.parse_args()
Kshitij Sisodiab9e9c892021-05-27 13:57:35 +0100181
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000182 logging.basicConfig(
183 filename="log_build_default.log", level=logging.DEBUG, filemode="w"
184 )
Kshitij Sisodiab9e9c892021-05-27 13:57:35 +0100185 logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
186
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000187 run(
188 args.toolchain.lower(),
Kshitij Sisodia3be26232021-10-29 12:29:06 +0100189 not args.skip_download,
190 not args.skip_vela,
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +0000191 args.npu_config_name,
192 args.make_jobs,
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000193 args.make_verbose,
194 )