Arulkumar03/Fox_Sheep_Detector_Computer_Vision_model
0
1# Copyright (c) Facebook, Inc. and its affiliates.2import importlib3import numpy as np4import os5import re6import subprocess7import sys8from collections import defaultdict9import PIL10import torch11import torchvision12from tabulate import tabulate13 14__all__ = ["collect_env_info"]15 16 17def collect_torch_env():18 try:19 import torch.__config__20 21 return torch.__config__.show()22 except ImportError:23 # compatible with older versions of pytorch24 from torch.utils.collect_env import get_pretty_env_info25 26 return get_pretty_env_info()27 28 29def get_env_module():30 var_name = "DETECTRON2_ENV_MODULE"31 return var_name, os.environ.get(var_name, "<not set>")32 33 34def detect_compute_compatibility(CUDA_HOME, so_file):35 try:36 cuobjdump = os.path.join(CUDA_HOME, "bin", "cuobjdump")37 if os.path.isfile(cuobjdump):38 output = subprocess.check_output(39 "'{}' --list-elf '{}'".format(cuobjdump, so_file), shell=True40 )41 output = output.decode("utf-8").strip().split("\n")42 arch = []43 for line in output:44 line = re.findall(r"\.sm_([0-9]*)\.", line)[0]45 arch.append(".".join(line))46 arch = sorted(set(arch))47 return ", ".join(arch)48 else:49 return so_file + "; cannot find cuobjdump"50 except Exception:51 # unhandled failure52 return so_file53 54 55def collect_env_info():56 has_gpu = torch.cuda.is_available() # true for both CUDA & ROCM57 torch_version = torch.__version__58 59 # NOTE that CUDA_HOME/ROCM_HOME could be None even when CUDA runtime libs are functional60 from torch.utils.cpp_extension import CUDA_HOME, ROCM_HOME61 62 has_rocm = False63 if (getattr(torch.version, "hip", None) is not None) and (ROCM_HOME is not None):64 has_rocm = True65 has_cuda = has_gpu and (not has_rocm)66 67 data = []68 data.append(("sys.platform", sys.platform)) # check-template.yml depends on it69 data.append(("Python", sys.version.replace("\n", "")))70 data.append(("numpy", np.__version__))71 72 try:73 import detectron2 # noqa74 75 data.append(76 ("detectron2", detectron2.__version__ + " @" + os.path.dirname(detectron2.__file__))77 )78 except ImportError:79 data.append(("detectron2", "failed to import"))80 except AttributeError:81 data.append(("detectron2", "imported a wrong installation"))82 83 try:84 import detectron2._C as _C85 except ImportError as e:86 data.append(("detectron2._C", f"not built correctly: {e}"))87 88 # print system compilers when extension fails to build89 if sys.platform != "win32": # don't know what to do for windows90 try:91 # this is how torch/utils/cpp_extensions.py choose compiler92 cxx = os.environ.get("CXX", "c++")93 cxx = subprocess.check_output("'{}' --version".format(cxx), shell=True)94 cxx = cxx.decode("utf-8").strip().split("\n")[0]95 except subprocess.SubprocessError:96 cxx = "Not found"97 data.append(("Compiler ($CXX)", cxx))98 99 if has_cuda and CUDA_HOME is not None:100 try:101 nvcc = os.path.join(CUDA_HOME, "bin", "nvcc")102 nvcc = subprocess.check_output("'{}' -V".format(nvcc), shell=True)103 nvcc = nvcc.decode("utf-8").strip().split("\n")[-1]104 except subprocess.SubprocessError:105 nvcc = "Not found"106 data.append(("CUDA compiler", nvcc))107 if has_cuda and sys.platform != "win32":108 try:109 so_file = importlib.util.find_spec("detectron2._C").origin110 except (ImportError, AttributeError):111 pass112 else:113 data.append(114 ("detectron2 arch flags", detect_compute_compatibility(CUDA_HOME, so_file))115 )116 else:117 # print compilers that are used to build extension118 data.append(("Compiler", _C.get_compiler_version()))119 data.append(("CUDA compiler", _C.get_cuda_version())) # cuda or hip120 if has_cuda and getattr(_C, "has_cuda", lambda: True)():121 data.append(122 ("detectron2 arch flags", detect_compute_compatibility(CUDA_HOME, _C.__file__))123 )124 125 data.append(get_env_module())126 data.append(("PyTorch", torch_version + " @" + os.path.dirname(torch.__file__)))127 data.append(("PyTorch debug build", torch.version.debug))128 try:129 data.append(("torch._C._GLIBCXX_USE_CXX11_ABI", torch._C._GLIBCXX_USE_CXX11_ABI))130 except Exception:131 pass132 133 if not has_gpu:134 has_gpu_text = "No: torch.cuda.is_available() == False"135 else:136 has_gpu_text = "Yes"137 data.append(("GPU available", has_gpu_text))138 if has_gpu:139 devices = defaultdict(list)140 for k in range(torch.cuda.device_count()):141 cap = ".".join((str(x) for x in torch.cuda.get_device_capability(k)))142 name = torch.cuda.get_device_name(k) + f" (arch={cap})"143 devices[name].append(str(k))144 for name, devids in devices.items():145 data.append(("GPU " + ",".join(devids), name))146 147 if has_rocm:148 msg = " - invalid!" if not (ROCM_HOME and os.path.isdir(ROCM_HOME)) else ""149 data.append(("ROCM_HOME", str(ROCM_HOME) + msg))150 else:151 try:152 from torch.utils.collect_env import get_nvidia_driver_version, run as _run153 154 data.append(("Driver version", get_nvidia_driver_version(_run)))155 except Exception:156 pass157 msg = " - invalid!" if not (CUDA_HOME and os.path.isdir(CUDA_HOME)) else ""158 data.append(("CUDA_HOME", str(CUDA_HOME) + msg))159 160 cuda_arch_list = os.environ.get("TORCH_CUDA_ARCH_LIST", None)161 if cuda_arch_list:162 data.append(("TORCH_CUDA_ARCH_LIST", cuda_arch_list))163 data.append(("Pillow", PIL.__version__))164 165 try:166 data.append(167 (168 "torchvision",169 str(torchvision.__version__) + " @" + os.path.dirname(torchvision.__file__),170 )171 )172 if has_cuda:173 try:174 torchvision_C = importlib.util.find_spec("torchvision._C").origin175 msg = detect_compute_compatibility(CUDA_HOME, torchvision_C)176 data.append(("torchvision arch flags", msg))177 except (ImportError, AttributeError):178 data.append(("torchvision._C", "Not found"))179 except AttributeError:180 data.append(("torchvision", "unknown"))181 182 try:183 import fvcore184 185 data.append(("fvcore", fvcore.__version__))186 except (ImportError, AttributeError):187 pass188 189 try:190 import iopath191 192 data.append(("iopath", iopath.__version__))193 except (ImportError, AttributeError):194 pass195 196 try:197 import cv2198 199 data.append(("cv2", cv2.__version__))200 except (ImportError, AttributeError):201 data.append(("cv2", "Not found"))202 env_str = tabulate(data) + "\n"203 env_str += collect_torch_env()204 return env_str205 206 207def test_nccl_ops():208 num_gpu = torch.cuda.device_count()209 if os.access("/tmp", os.W_OK):210 import torch.multiprocessing as mp211 212 dist_url = "file:///tmp/nccl_tmp_file"213 print("Testing NCCL connectivity ... this should not hang.")214 mp.spawn(_test_nccl_worker, nprocs=num_gpu, args=(num_gpu, dist_url), daemon=False)215 print("NCCL succeeded.")216 217 218def _test_nccl_worker(rank, num_gpu, dist_url):219 import torch.distributed as dist220 221 dist.init_process_group(backend="NCCL", init_method=dist_url, rank=rank, world_size=num_gpu)222 dist.barrier(device_ids=[rank])223 224 225if __name__ == "__main__":226 try:227 from detectron2.utils.collect_env import collect_env_info as f228 229 print(f())230 except ImportError:231 print(collect_env_info())232 233 if torch.cuda.is_available():234 num_gpu = torch.cuda.device_count()235 for k in range(num_gpu):236 device = f"cuda:{k}"237 try:238 x = torch.tensor([1, 2.0], dtype=torch.float32)239 x = x.to(device)240 except Exception as e:241 print(242 f"Unable to copy tensor to device={device}: {e}. "243 "Your CUDA environment is broken."244 )245 if num_gpu > 1:246 test_nccl_ops()247 