blob: c0cf8d1b52df96fa6fcf9a73e4fe673444333445 [file] [log] [blame]
alexanderf4e2c472021-05-14 13:14:21 +01001#!/usr/bin/env python3
Isabella Gottardi2181d0a2021-04-07 09:27:38 +01002
3# Copyright (c) 2021 Arm Limited. All rights reserved.
4# SPDX-License-Identifier: Apache-2.0
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17
18import os
19import subprocess
20import shutil
21import multiprocessing
22import logging
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +000023import threading
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010024import sys
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +000025from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010026
Kshitij Sisodia3be26232021-10-29 12:29:06 +010027from set_up_default_resources import set_up_resources, \
28 get_default_npu_config_from_name, \
29 valid_npu_config_names, \
30 default_npu_config_names
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010031
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +000032class PipeLogging(threading.Thread):
33
34 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):
46 for line in iter(self.pipeIn.readline, ''):
47 logging.log(self.logLevel, line.strip('\n'))
48
49 self.pipeIn.close()
50
51 def close(self):
52 os.close(self.fileWrite)
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010053
Kshitij Sisodia3be26232021-10-29 12:29:06 +010054def run(toolchain: str,
55 download_resources: bool,
56 run_vela_on_models: bool,
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +000057 npu_config_name: str,
58 make_jobs: int,
59 make_verbose: bool):
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010060 """
61 Run the helpers scripts.
62
63 Parameters:
64 ----------
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010065 toolchain (str) : Specifies if 'gnu' or 'arm' toolchain needs to be used.
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010066 download_resources (bool): Specifies if 'Download resources' step is performed.
67 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 +010068 npu_config_name(str) : Ethos-U NPU configuration name. See "valid_npu_config_names"
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010069 """
70
71 current_file_dir = os.path.dirname(os.path.abspath(__file__))
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010072
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010073 # 1. Make sure the toolchain is supported, and set the right one here
74 supported_toolchain_ids = ["gnu", "arm"]
Nina Drozd59169522022-02-10 13:33:20 +000075 assert toolchain in supported_toolchain_ids, f"Toolchain must be from {supported_toolchain_ids}"
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010076 if toolchain == "arm":
77 toolchain_file_name = "bare-metal-armclang.cmake"
78 elif toolchain == "gnu":
79 toolchain_file_name = "bare-metal-gcc.cmake"
80
81 # 2. Download models if specified
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010082 if download_resources is True:
83 logging.info("Downloading resources.")
Kshitij Sisodia3be26232021-10-29 12:29:06 +010084 set_up_resources(run_vela_on_models=run_vela_on_models,
85 additional_npu_config_names=[npu_config_name])
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010086
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010087 # 3. Build default configuration
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010088 logging.info("Building default configuration.")
89 target_platform = "mps3"
90 target_subsystem = "sse-300"
Kshitij Sisodia3be26232021-10-29 12:29:06 +010091 ethos_u_cfg = get_default_npu_config_from_name(npu_config_name)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010092 build_dir = os.path.join(current_file_dir,
Kshitij Sisodia3be26232021-10-29 12:29:06 +010093 f"cmake-build-{target_platform}-{target_subsystem}-{npu_config_name}-{toolchain}")
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010094 try:
95 os.mkdir(build_dir)
96 except FileExistsError:
97 # Directory already exists, clean it
98 for filename in os.listdir(build_dir):
99 filepath = os.path.join(build_dir, filename)
100 try:
101 if os.path.isfile(filepath) or os.path.islink(filepath):
102 os.unlink(filepath)
103 elif os.path.isdir(filepath):
104 shutil.rmtree(filepath)
105 except Exception as e:
106 logging.error('Failed to delete %s. Reason: %s' % (filepath, e))
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100107
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +0000108 logpipe = PipeLogging(logging.INFO)
109
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100110 os.chdir(build_dir)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100111 cmake_toolchain_file = os.path.join(current_file_dir, "scripts", "cmake",
112 "toolchains", toolchain_file_name)
Kshitij Sisodia3be26232021-10-29 12:29:06 +0100113 cmake_command = (f"cmake .. -DTARGET_PLATFORM={target_platform}" +
114 f" -DTARGET_SUBSYSTEM={target_subsystem}" +
115 f" -DCMAKE_TOOLCHAIN_FILE={cmake_toolchain_file}" +
116 f" -DETHOS_U_NPU_ID={ethos_u_cfg.ethos_u_npu_id}" +
117 f" -DETHOS_U_NPU_CONFIG_ID={ethos_u_cfg.ethos_u_config_id}")
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100118
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100119 logging.info(cmake_command)
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +0000120 state = subprocess.run(cmake_command, shell=True, stdout=logpipe,
Kshitij Sisodia3be26232021-10-29 12:29:06 +0100121 stderr=subprocess.STDOUT)
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100122
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +0000123 make_command = f"make -j{make_jobs}"
124 if make_verbose :
125 make_command += " VERBOSE=1"
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100126 logging.info(make_command)
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +0000127 state = subprocess.run(make_command, shell=True, stdout=logpipe,
Kshitij Sisodia3be26232021-10-29 12:29:06 +0100128 stderr=subprocess.STDOUT)
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +0000129
130 logpipe.close()
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100131
132
133if __name__ == '__main__':
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +0000134 parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100135 parser.add_argument("--toolchain", default="gnu",
136 help="""
137 Specify the toolchain to use (Arm or GNU).
138 Options are [gnu, arm]; default is gnu.
139 """)
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100140 parser.add_argument("--skip-download",
141 help="Do not download resources: models and test vectors",
142 action="store_true")
143 parser.add_argument("--skip-vela",
144 help="Do not run Vela optimizer on downloaded models.",
145 action="store_true")
Kshitij Sisodia3be26232021-10-29 12:29:06 +0100146 parser.add_argument("--npu-config-name",
147 help=f"""Arm Ethos-U configuration to build for. Choose from:
148 {valid_npu_config_names}""",
149 default=default_npu_config_names[0])
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +0000150 parser.add_argument("--make-jobs",
151 help="Number of jobs to run with make",
152 default=multiprocessing.cpu_count())
153 parser.add_argument("--make-verbose",
154 help="Make runs with VERBOSE=1",
155 action='store_true')
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100156 args = parser.parse_args()
Kshitij Sisodiab9e9c892021-05-27 13:57:35 +0100157
Kshitij Sisodia3be26232021-10-29 12:29:06 +0100158 logging.basicConfig(filename='log_build_default.log', level=logging.DEBUG,
159 filemode='w')
Kshitij Sisodiab9e9c892021-05-27 13:57:35 +0100160 logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
161
Kshitij Sisodia3be26232021-10-29 12:29:06 +0100162 run(args.toolchain.lower(),
163 not args.skip_download,
164 not args.skip_vela,
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +0000165 args.npu_config_name,
166 args.make_jobs,
167 args.make_verbose)