alexander | f4e2c47 | 2021-05-14 13:14:21 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 2 | |
| 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 | |
| 18 | import os |
| 19 | import subprocess |
| 20 | import shutil |
| 21 | import multiprocessing |
| 22 | import logging |
Cisco Cervellera | 6ef76bd | 2021-11-25 18:32:27 +0000 | [diff] [blame] | 23 | import threading |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 24 | import sys |
Cisco Cervellera | 6ef76bd | 2021-11-25 18:32:27 +0000 | [diff] [blame] | 25 | from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 26 | |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 27 | from 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 Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 31 | |
Cisco Cervellera | 6ef76bd | 2021-11-25 18:32:27 +0000 | [diff] [blame] | 32 | class 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 Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 53 | |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 54 | def run(toolchain: str, |
| 55 | download_resources: bool, |
| 56 | run_vela_on_models: bool, |
Cisco Cervellera | 6ef76bd | 2021-11-25 18:32:27 +0000 | [diff] [blame] | 57 | npu_config_name: str, |
| 58 | make_jobs: int, |
| 59 | make_verbose: bool): |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 60 | """ |
| 61 | Run the helpers scripts. |
| 62 | |
| 63 | Parameters: |
| 64 | ---------- |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 65 | toolchain (str) : Specifies if 'gnu' or 'arm' toolchain needs to be used. |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 66 | 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 Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 68 | npu_config_name(str) : Ethos-U NPU configuration name. See "valid_npu_config_names" |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 69 | """ |
| 70 | |
| 71 | current_file_dir = os.path.dirname(os.path.abspath(__file__)) |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 72 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 73 | # 1. Make sure the toolchain is supported, and set the right one here |
| 74 | supported_toolchain_ids = ["gnu", "arm"] |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 75 | assert (toolchain in supported_toolchain_ids, |
| 76 | f"Toolchain must be from {supported_toolchain_ids}") |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 77 | if toolchain == "arm": |
| 78 | toolchain_file_name = "bare-metal-armclang.cmake" |
| 79 | elif toolchain == "gnu": |
| 80 | toolchain_file_name = "bare-metal-gcc.cmake" |
| 81 | |
| 82 | # 2. Download models if specified |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 83 | if download_resources is True: |
| 84 | logging.info("Downloading resources.") |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 85 | set_up_resources(run_vela_on_models=run_vela_on_models, |
| 86 | additional_npu_config_names=[npu_config_name]) |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 87 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 88 | # 3. Build default configuration |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 89 | logging.info("Building default configuration.") |
| 90 | target_platform = "mps3" |
| 91 | target_subsystem = "sse-300" |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 92 | ethos_u_cfg = get_default_npu_config_from_name(npu_config_name) |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 93 | build_dir = os.path.join(current_file_dir, |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 94 | f"cmake-build-{target_platform}-{target_subsystem}-{npu_config_name}-{toolchain}") |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 95 | try: |
| 96 | os.mkdir(build_dir) |
| 97 | except FileExistsError: |
| 98 | # Directory already exists, clean it |
| 99 | for filename in os.listdir(build_dir): |
| 100 | filepath = os.path.join(build_dir, filename) |
| 101 | try: |
| 102 | if os.path.isfile(filepath) or os.path.islink(filepath): |
| 103 | os.unlink(filepath) |
| 104 | elif os.path.isdir(filepath): |
| 105 | shutil.rmtree(filepath) |
| 106 | except Exception as e: |
| 107 | logging.error('Failed to delete %s. Reason: %s' % (filepath, e)) |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 108 | |
Cisco Cervellera | 6ef76bd | 2021-11-25 18:32:27 +0000 | [diff] [blame] | 109 | logpipe = PipeLogging(logging.INFO) |
| 110 | |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 111 | os.chdir(build_dir) |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 112 | cmake_toolchain_file = os.path.join(current_file_dir, "scripts", "cmake", |
| 113 | "toolchains", toolchain_file_name) |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 114 | cmake_command = (f"cmake .. -DTARGET_PLATFORM={target_platform}" + |
| 115 | f" -DTARGET_SUBSYSTEM={target_subsystem}" + |
| 116 | f" -DCMAKE_TOOLCHAIN_FILE={cmake_toolchain_file}" + |
| 117 | f" -DETHOS_U_NPU_ID={ethos_u_cfg.ethos_u_npu_id}" + |
| 118 | f" -DETHOS_U_NPU_CONFIG_ID={ethos_u_cfg.ethos_u_config_id}") |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 119 | |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 120 | logging.info(cmake_command) |
Cisco Cervellera | 6ef76bd | 2021-11-25 18:32:27 +0000 | [diff] [blame] | 121 | state = subprocess.run(cmake_command, shell=True, stdout=logpipe, |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 122 | stderr=subprocess.STDOUT) |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 123 | |
Cisco Cervellera | 6ef76bd | 2021-11-25 18:32:27 +0000 | [diff] [blame] | 124 | make_command = f"make -j{make_jobs}" |
| 125 | if make_verbose : |
| 126 | make_command += " VERBOSE=1" |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 127 | logging.info(make_command) |
Cisco Cervellera | 6ef76bd | 2021-11-25 18:32:27 +0000 | [diff] [blame] | 128 | state = subprocess.run(make_command, shell=True, stdout=logpipe, |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 129 | stderr=subprocess.STDOUT) |
Cisco Cervellera | 6ef76bd | 2021-11-25 18:32:27 +0000 | [diff] [blame] | 130 | |
| 131 | logpipe.close() |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 132 | |
| 133 | |
| 134 | if __name__ == '__main__': |
Cisco Cervellera | 6ef76bd | 2021-11-25 18:32:27 +0000 | [diff] [blame] | 135 | parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter) |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 136 | parser.add_argument("--toolchain", default="gnu", |
| 137 | help=""" |
| 138 | Specify the toolchain to use (Arm or GNU). |
| 139 | Options are [gnu, arm]; default is gnu. |
| 140 | """) |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 141 | parser.add_argument("--skip-download", |
| 142 | help="Do not download resources: models and test vectors", |
| 143 | action="store_true") |
| 144 | parser.add_argument("--skip-vela", |
| 145 | help="Do not run Vela optimizer on downloaded models.", |
| 146 | action="store_true") |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 147 | parser.add_argument("--npu-config-name", |
| 148 | help=f"""Arm Ethos-U configuration to build for. Choose from: |
| 149 | {valid_npu_config_names}""", |
| 150 | default=default_npu_config_names[0]) |
Cisco Cervellera | 6ef76bd | 2021-11-25 18:32:27 +0000 | [diff] [blame] | 151 | parser.add_argument("--make-jobs", |
| 152 | help="Number of jobs to run with make", |
| 153 | default=multiprocessing.cpu_count()) |
| 154 | parser.add_argument("--make-verbose", |
| 155 | help="Make runs with VERBOSE=1", |
| 156 | action='store_true') |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 157 | args = parser.parse_args() |
Kshitij Sisodia | b9e9c89 | 2021-05-27 13:57:35 +0100 | [diff] [blame] | 158 | |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 159 | logging.basicConfig(filename='log_build_default.log', level=logging.DEBUG, |
| 160 | filemode='w') |
Kshitij Sisodia | b9e9c89 | 2021-05-27 13:57:35 +0100 | [diff] [blame] | 161 | logging.getLogger().addHandler(logging.StreamHandler(sys.stdout)) |
| 162 | |
Kshitij Sisodia | 3be2623 | 2021-10-29 12:29:06 +0100 | [diff] [blame] | 163 | run(args.toolchain.lower(), |
| 164 | not args.skip_download, |
| 165 | not args.skip_vela, |
Cisco Cervellera | 6ef76bd | 2021-11-25 18:32:27 +0000 | [diff] [blame] | 166 | args.npu_config_name, |
| 167 | args.make_jobs, |
| 168 | args.make_verbose) |