|
| 1 | +#! /usr/bin/env python |
| 2 | + |
| 3 | +# Copyright (c) MONAI Consortium |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | + |
| 14 | +""" |
| 15 | +Script for checking various elements of the runtime environment and printing a large amount of diagnostic information. |
| 16 | +
|
| 17 | +This is meant to be used for debugging environments used with MONAI, but doesn't directly need MONAI itself. It will |
| 18 | +print information about the environment, including trying to get installed packages, test PyTorch with CUDA, and then |
| 19 | +have MONAI print its debugging information if no errors encountered. If MONAI is not installed this script should still |
| 20 | +work and produce useful information. Only standard libraries are needed in case a bare environment is being used. |
| 21 | +
|
| 22 | +This can be run as a program with the following options to see all outputs: |
| 23 | +
|
| 24 | + python check_env.py --env --monai |
| 25 | +
|
| 26 | +With no options at all this can be used to test that PyTorch is installed and can move a tensor to a device. This is |
| 27 | +useful when creating a fresh test environment and MONAI isn't present yet but it's good practice to valid PyTorch. |
| 28 | +Environment variables can be printed by using `--envars` with `--env`, however the output from this is not checked for |
| 29 | +secrets so users |
| 30 | +
|
| 31 | +This can also be used remotely with only Python installed to get current environment diagnostic info: |
| 32 | +
|
| 33 | + curl https://raw.githubusercontent.com/Project-MONAI/MONAI/refs/heads/dev/monai/config/check_env.py | python |
| 34 | +""" |
| 35 | + |
| 36 | +from __future__ import annotations |
| 37 | + |
| 38 | +import argparse |
| 39 | +import getpass |
| 40 | +import multiprocessing |
| 41 | +import os |
| 42 | +import platform |
| 43 | +import shutil |
| 44 | +import subprocess |
| 45 | +import sys |
| 46 | +from functools import partial |
| 47 | + |
| 48 | +DESC = """ |
| 49 | +Script for checking various elements of the runtime environment and printing a large amount of diagnostic information. |
| 50 | +This is used for debugging your environment by printing out various system statistics and diagnostic information. It |
| 51 | +checks PyTorch and MONAI are installed and functioning. A typical use case is with the `--env` and `--monai` options. |
| 52 | +""" |
| 53 | + |
| 54 | +USER = getpass.getuser() |
| 55 | +HOST = platform.node() |
| 56 | +efprint = partial(print, flush=True, file=sys.stderr) |
| 57 | + |
| 58 | + |
| 59 | +def fprint(*args, **kwargs): |
| 60 | + """ |
| 61 | + Print with flushing, replacing the username and hostname values with placeholders for better anonymization. |
| 62 | + """ |
| 63 | + kwargs["flush"] = True |
| 64 | + content = " ".join(map(str, args)) |
| 65 | + content = content.replace(USER, "<user>").replace(HOST, "<host>") |
| 66 | + print(content, **kwargs) |
| 67 | + |
| 68 | + |
| 69 | +def print_platform(): |
| 70 | + """ |
| 71 | + Print basic platform information. |
| 72 | + """ |
| 73 | + fprint(platform.platform()) |
| 74 | + fprint("uname:", list(platform.uname())) |
| 75 | + fprint("CPU:", platform.processor(), "Count:", multiprocessing.cpu_count()) |
| 76 | + fprint("Python:", sys.executable, platform.python_implementation(), platform.python_version()) |
| 77 | + |
| 78 | + |
| 79 | +def print_environment_vars(): |
| 80 | + """ |
| 81 | + Print all environment variables other than a few known pointless ones. |
| 82 | + """ |
| 83 | + fprint("Environment:") |
| 84 | + for k, v in os.environ.items(): |
| 85 | + if k not in ("LS_COLORS", "PS1", "PS2"): |
| 86 | + fprint(f" {k}:", v) |
| 87 | + |
| 88 | + |
| 89 | +def print_environment(): |
| 90 | + """ |
| 91 | + Print the installed environment using `conda` or `pip`, fail if neither are present. |
| 92 | + """ |
| 93 | + try: |
| 94 | + cmd = ("conda", "env", "export") if shutil.which("conda") else ("pip", "list") |
| 95 | + |
| 96 | + result = subprocess.check_output(cmd, stderr=subprocess.STDOUT) |
| 97 | + fprint(result.decode()) |
| 98 | + return True |
| 99 | + except Exception as e: |
| 100 | + efprint(f"Exception encountered getting environment with conda/pip: {e}") |
| 101 | + return False |
| 102 | + |
| 103 | + |
| 104 | +def check_torch(): |
| 105 | + """ |
| 106 | + Check PyTorch is installed and a tensor can be created. |
| 107 | + """ |
| 108 | + try: |
| 109 | + import torch |
| 110 | + |
| 111 | + t = torch.rand(2, 3) * 5 |
| 112 | + fprint("PyTorch:", torch.__version__, torch.__path__) |
| 113 | + fprint("Test tensor:", t.flatten()) |
| 114 | + return True |
| 115 | + except ImportError: |
| 116 | + efprint("PyTorch not installed") |
| 117 | + return False |
| 118 | + |
| 119 | + |
| 120 | +def check_torch_cuda(): |
| 121 | + """ |
| 122 | + Check CUDA capability in PyTorch by moving a tensor to each available device. |
| 123 | + """ |
| 124 | + import torch |
| 125 | + |
| 126 | + fprint("CUDA version:", torch.version.cuda) |
| 127 | + |
| 128 | + try: |
| 129 | + dcount = torch.cuda.device_count() |
| 130 | + fprint("PyTorch GPU Count:", dcount) |
| 131 | + |
| 132 | + for d in range(dcount): |
| 133 | + fprint(f" {torch.cuda.get_device_properties(d)}") |
| 134 | + t = torch.rand(2, 3).to(torch.device(f"cuda:{d}")) * 5 |
| 135 | + fprint("Test tensor:", t.flatten()) |
| 136 | + return True |
| 137 | + except Exception as e: |
| 138 | + efprint(f"PyTorch encountered exception creating GPU tensor on device {d}: {e}") |
| 139 | + return False |
| 140 | + |
| 141 | + |
| 142 | +def check_monai(): |
| 143 | + """ |
| 144 | + Check MONAI by importing it then printing its debug info. |
| 145 | + """ |
| 146 | + try: |
| 147 | + import monai |
| 148 | + |
| 149 | + monai.config.print_debug_info() # type: ignore |
| 150 | + return True |
| 151 | + except ImportError: |
| 152 | + efprint("MONAI not installed") |
| 153 | + return False |
| 154 | + |
| 155 | + |
| 156 | +if __name__ == "__main__": |
| 157 | + parser = argparse.ArgumentParser(prog="check_env.py", description=DESC.strip()) |
| 158 | + parser.add_argument("--env", default=False, action="store_true", help="Print environment info") |
| 159 | + parser.add_argument("--envvars", default=False, action="store_true", help="Include environment variables") |
| 160 | + parser.add_argument("--monai", default=False, action="store_true", help="Print MONAI info") |
| 161 | + args = parser.parse_args() |
| 162 | + |
| 163 | + fprint("=" * 10, "Platform Info", "=" * 10) |
| 164 | + print_platform() |
| 165 | + |
| 166 | + if args.env: |
| 167 | + fprint("=" * 10, "Checking Environment", "=" * 10) |
| 168 | + if args.envvars: |
| 169 | + print_environment_vars() |
| 170 | + print_environment() |
| 171 | + |
| 172 | + fprint("=" * 10, "Checking PyTorch", "=" * 10) |
| 173 | + |
| 174 | + if not check_torch(): |
| 175 | + efprint("Exiting early, no valid PyTorch install found.") |
| 176 | + sys.exit(1) |
| 177 | + |
| 178 | + if not check_torch_cuda(): |
| 179 | + efprint("Exiting early, PyTorch encountered CUDA error.") |
| 180 | + sys.exit(1) |
| 181 | + |
| 182 | + if args.monai: |
| 183 | + fprint("=" * 10, "Checking MONAI", "=" * 10) |
| 184 | + check_monai() |
0 commit comments