|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Copyright 2023 The IREE Authors |
| 4 | +# |
| 5 | +# Licensed under the Apache License v2.0 with LLVM Exceptions. |
| 6 | +# See https://llvm.org/LICENSE.txt for license information. |
| 7 | +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 8 | +"""Configure script to setup VSCode DevContainer Environment.""" |
| 9 | + |
| 10 | +import json |
| 11 | +import os |
| 12 | +import pathlib |
| 13 | +import shlex |
| 14 | +import subprocess |
| 15 | +import sys |
| 16 | + |
| 17 | +from importlib.machinery import SourceFileLoader |
| 18 | +from shutil import which |
| 19 | + |
| 20 | +CURRENT_DIR = pathlib.Path(__file__).resolve().parent |
| 21 | + |
| 22 | +DOCKER_IMAGE_SHORTNAME_DICT = { |
| 23 | + "base": "base-bleeding-edge", |
| 24 | + "nvidia": "nvidia-bleeding-edge", |
| 25 | +} |
| 26 | + |
| 27 | + |
| 28 | +def run_shell(cmd): |
| 29 | + cmd = shlex.split(cmd) |
| 30 | + return subprocess.run(cmd, check=True, text=True, |
| 31 | + stdout=subprocess.PIPE).stdout |
| 32 | + |
| 33 | + |
| 34 | +def is_nvidia_gpu_available(): |
| 35 | + """This function verifies NVIDIA Docker runtime is installed and |
| 36 | + available. It also checks NVIDIA GPU are available and the driver |
| 37 | + is installed.""" |
| 38 | + |
| 39 | + command_str = f"'{which('docker')}' info --format '{{{{json .}}}}'" |
| 40 | + data = json.loads(run_shell(command_str)) |
| 41 | + |
| 42 | + if "nvidia" not in data["Runtimes"].keys(): |
| 43 | + return ( |
| 44 | + False, "NVIDIA Docker Runtime is not available. Please see: " |
| 45 | + "https://developer.nvidia.com/nvidia-container-runtime for additional " |
| 46 | + "information.") |
| 47 | + |
| 48 | + nvidia_smi_executable = which('nvidia-smi') |
| 49 | + if nvidia_smi_executable is None: |
| 50 | + return False, "NVIDIA Driver is not installed or not in the user path." |
| 51 | + |
| 52 | + command_str = f"{nvidia_smi_executable} --query-gpu=gpu_name --format=csv" |
| 53 | + |
| 54 | + if run_shell(command_str).splitlines()[1:]: # Skip header |
| 55 | + return True, None |
| 56 | + else: |
| 57 | + return False, "No NVIDIA GPU was found on this system." |
| 58 | + |
| 59 | + |
| 60 | +def _get_input(question, default_answer): |
| 61 | + try: |
| 62 | + answer = input(f"\n- {question} ") |
| 63 | + except EOFError: |
| 64 | + answer = default_answer |
| 65 | + |
| 66 | + return (answer or default_answer).strip().lower() |
| 67 | + |
| 68 | + |
| 69 | +def get_input(question, default_answer='', accepted_answers=None): |
| 70 | + if accepted_answers is None: |
| 71 | + raise RuntimeError("Argument `accepted_answers` is None.") |
| 72 | + |
| 73 | + accepted_answers = [x.strip().lower() for x in accepted_answers] |
| 74 | + |
| 75 | + while True: |
| 76 | + answer = _get_input(question, default_answer) |
| 77 | + if answer not in accepted_answers: |
| 78 | + print(f"\tERROR: Unsupported answer received: {answer}." |
| 79 | + f"Expected: {accepted_answers}") |
| 80 | + continue |
| 81 | + break |
| 82 | + |
| 83 | + return answer |
| 84 | + |
| 85 | + |
| 86 | +def get_input_path(question): |
| 87 | + |
| 88 | + while True: |
| 89 | + answer = _get_input(question, default_answer="") |
| 90 | + if answer != "" and not os.path.isdir(answer): |
| 91 | + print(f"\tERROR: Received path does not exist: `{answer}`") |
| 92 | + continue |
| 93 | + break |
| 94 | + |
| 95 | + return answer |
| 96 | + |
| 97 | + |
| 98 | +if __name__ == "__main__": |
| 99 | + |
| 100 | + # ------------------------------------------------------------------------- # |
| 101 | + # Environment Verifications # |
| 102 | + # ------------------------------------------------------------------------- # |
| 103 | + |
| 104 | + docker_executable = which('docker') |
| 105 | + if docker_executable is None: |
| 106 | + raise RuntimeError( |
| 107 | + "Docker is not installed or in the user path. Please refer to: " |
| 108 | + "https://docs.docker.com/desktop/") |
| 109 | + |
| 110 | + try: |
| 111 | + run_shell(f"'{docker_executable}' compose version") |
| 112 | + except subprocess.CalledProcessError as e: |
| 113 | + raise RuntimeError( |
| 114 | + "Docker Compose must be installed in order to use IREE VS Code " |
| 115 | + "Development Container. Please refer to: " |
| 116 | + "https://docs.docker.com/compose/") from e |
| 117 | + |
| 118 | + docker_image_key = "base" |
| 119 | + |
| 120 | + # ------------------------------------------------------------------------- # |
| 121 | + # Mandatory Steps # |
| 122 | + # ------------------------------------------------------------------------- # |
| 123 | + |
| 124 | + # STEP 1: Verify the user doesn't have a pre-existing `docker-compose.yml`. |
| 125 | + # If yes, confirm they want to overwrite it. |
| 126 | + if os.path.isfile(CURRENT_DIR / "docker-compose.yml"): |
| 127 | + if get_input( |
| 128 | + "A `docker-compose.yml` already exists. Are you certain you want to overwrite it [y/N]?", |
| 129 | + default_answer="n", |
| 130 | + accepted_answers=["y", "n"]) == "n": |
| 131 | + sys.exit(1) |
| 132 | + |
| 133 | + # STEP 2: Read the template configuration file |
| 134 | + with open(CURRENT_DIR / "docker-compose.base.yml") as f: |
| 135 | + docker_config = json.load(f) |
| 136 | + |
| 137 | + # STEP 3: Prebuilt vs Locally Built Containers |
| 138 | + use_official_img = get_input( |
| 139 | + "Do you wish to use the official prebuild development containers [Y/n]?", |
| 140 | + default_answer="y", |
| 141 | + accepted_answers=["y", "n"]) == "y" |
| 142 | + |
| 143 | + # ------------------------------------------------------------------------- # |
| 144 | + # Optional Mounting Points - Build & Cache # |
| 145 | + # ------------------------------------------------------------------------- # |
| 146 | + |
| 147 | + # STEP 4 [OPTIONAL]: Does the user want to mount a directory for CCACHE ? |
| 148 | + ccache_mount_cache = get_input_path( |
| 149 | + "[OPTIONAL] Enter the the directory path to mount as CCache Cache.\n" |
| 150 | + " It will be mounted in the container under: `/home/iree/.cache/ccache` [Default: None]:" |
| 151 | + ) |
| 152 | + if ccache_mount_cache: |
| 153 | + docker_config["services"]["iree-dev"]["volumes"].append( |
| 154 | + f"{ccache_mount_cache}:/home/iree/.cache/ccache:cached") |
| 155 | + |
| 156 | + # STEP 5 [OPTIONAL]: Does the user want to mount a directory for BUILD ? |
| 157 | + build_mount_cache = get_input_path( |
| 158 | + "[OPTIONAL] Enter the the directory path to mount as Build Directory.\n" |
| 159 | + " It will mounted in the container under: `/home/iree/build` [Default: None]:" |
| 160 | + ) |
| 161 | + if build_mount_cache: |
| 162 | + docker_config["services"]["iree-dev"]["volumes"].append( |
| 163 | + f"{build_mount_cache}:/home/iree/build:cached") |
| 164 | + |
| 165 | + # ------------------------------------------------------------------------- # |
| 166 | + # Optional Deep Learning Accelerator Support # |
| 167 | + # ------------------------------------------------------------------------- # |
| 168 | + |
| 169 | + # STEP 6 [OPTIONAL]: Does the user want to use NVIDIA GPUs ? |
| 170 | + nvidia_gpu_available, err_msg = is_nvidia_gpu_available() |
| 171 | + |
| 172 | + if nvidia_gpu_available: |
| 173 | + if get_input("[OPTIONAL] Do you wish to use NVIDIA GPU [y/N]?", |
| 174 | + default_answer="n", |
| 175 | + accepted_answers=["y", "n"]) == "y": |
| 176 | + docker_image_key = "nvidia" |
| 177 | + |
| 178 | + else: |
| 179 | + print(f"[INFO] NVIDIA GPUs are not available for use: {err_msg}") |
| 180 | + |
| 181 | + if docker_image_key != "nvidia": |
| 182 | + del docker_config["services"]["iree-dev"]["deploy"] |
| 183 | + |
| 184 | + # ------------------------------------------------------------------------- # |
| 185 | + # Setting the right docker image / container to build # |
| 186 | + # ------------------------------------------------------------------------- # |
| 187 | + |
| 188 | + docker_img_shortname = DOCKER_IMAGE_SHORTNAME_DICT[docker_image_key] |
| 189 | + |
| 190 | + if use_official_img: |
| 191 | + del docker_config["services"]["iree-dev"]["build"] |
| 192 | + |
| 193 | + docker_iree_registry = SourceFileLoader( |
| 194 | + "docker_iree_registry", |
| 195 | + str(CURRENT_DIR / |
| 196 | + "../build_tools/docker/get_image_name.py")).load_module() |
| 197 | + |
| 198 | + image_name = docker_iree_registry.find_image_by_name(docker_img_shortname) |
| 199 | + docker_config["services"]["iree-dev"]["image"] = image_name |
| 200 | + |
| 201 | + else: |
| 202 | + del docker_config["services"]["iree-dev"]["image"] |
| 203 | + |
| 204 | + dockerfile = f"build_tools/docker/dockerfiles/{docker_img_shortname}.Dockerfile" |
| 205 | + docker_config["services"]["iree-dev"]["build"]["dockerfile"] = dockerfile |
| 206 | + |
| 207 | + if (not os.path.isfile(CURRENT_DIR / ".." / dockerfile)): |
| 208 | + raise FileNotFoundError(f"The file `{dockerfile}` does not exist.") |
| 209 | + |
| 210 | + docker_compose_filepath = os.path.join(CURRENT_DIR, "docker-compose.yml") |
| 211 | + with open(docker_compose_filepath, "w") as f: |
| 212 | + json.dump(docker_config, f, indent=2) |
| 213 | + |
| 214 | + # ------------------------------------------------------------------------- # |
| 215 | + # SUCCESS # |
| 216 | + # ------------------------------------------------------------------------- # |
| 217 | + |
| 218 | + print("\n" + "=" * 80) |
| 219 | + print(f"\nSuccess! Wrote Docker Compose file to `{docker_compose_filepath}`.") |
0 commit comments