Caffe2 - Python API
A deep learning, cross platform ML framework
dist_check.py
1 import os
2 import subprocess
3 import glob
4 
5 from .env import IS_CONDA, IS_LINUX, IS_WINDOWS, CONDA_DIR, check_env_flag, check_negative_env_flag, gather_paths
6 from .cuda import USE_CUDA
7 
8 # On ROCm, RCCL development isn't complete. https://github.com/ROCmSoftwarePlatform/rccl
9 USE_DISTRIBUTED = not check_negative_env_flag("USE_DISTRIBUTED") and not IS_WINDOWS and not check_env_flag("USE_ROCM")
10 USE_GLOO_IBVERBS = False
11 
12 IB_DEVINFO_CMD = "ibv_devinfo"
13 
14 
15 def get_command_path(command):
16  """
17  Helper function that checks if the command exists in the path and gets the
18  full path of a given linux command if it exists.
19  """
20  def excutable(command_path):
21  return os.path.isfile(command_path) and os.access(command_path, os.X_OK)
22 
23  for path in os.environ["PATH"].split(os.pathsep):
24  command_path = os.path.join(path, command)
25  if excutable(command_path):
26  return command_path
27 
28  return None
29 
30 
31 def should_build_ib():
32  """
33  Helper function that detects the system's IB support and returns if we
34  should build with IB support.
35  """
36  ib_util_found = False
37  ib_lib_found = False
38  ib_header_found = False
39 
40  try:
41  # If the command doesn't exist, we can directly return instead of
42  # making a subprocess call
43  full_cmd_path = get_command_path(IB_DEVINFO_CMD)
44  if not full_cmd_path:
45  ib_util_found = False
46  subprocess.check_output([full_cmd_path, "--list"])
47  # Here we just would like to simply run the command to test if IB
48  # related tools / lib are installed without parsing the output. We
49  # will enable IB build as long as the command runs successfully.
50  #
51  # The output should look like either:
52  #
53  # > ibv_devinfo --list
54  # 0 HCAs founds:
55  #
56  # or
57  #
58  # > ibv_devinfo --list
59  # 4 HCAs found:
60  # mlx5_3
61  # mlx5_2
62  # mlx5_1
63  # mlx5_0
64  ib_util_found = True
65  except Exception:
66  # We just take all the exceptions here without affecting the build
67  ib_util_found = False
68 
69  lib_paths = list(filter(bool, [
70  "/usr/lib/",
71  "/usr/lib/x86_64-linux-gnu/",
72  "/usr/lib/powerpc64le-linux-gnu/",
73  "/usr/lib/aarch64-linux-gnu/",
74  ] + gather_paths([
75  "LIBRARY_PATH",
76  ]) + gather_paths([
77  "LD_LIBRARY_PATH",
78  ])))
79 
80  include_paths = [
81  "/usr/include/",
82  ]
83 
84  if IS_CONDA:
85  lib_paths.append(os.path.join(CONDA_DIR, "lib"))
86  include_paths.append(os.path.join(CONDA_DIR, "include"))
87 
88  for path in lib_paths:
89  if path is None or not os.path.exists(path):
90  continue
91  ib_libraries = sorted(glob.glob(os.path.join(path, "libibverbs*")))
92  if ib_libraries:
93  ib_lib_found = True
94  break
95 
96  for path in include_paths:
97  if path is None or not os.path.exists(path):
98  continue
99  if os.path.exists(os.path.join(path, "infiniband/verbs.h")):
100  ib_header_found = True
101  break
102 
103  return ib_util_found and ib_lib_found and ib_lib_found
104 
105 if USE_DISTRIBUTED:
106  # If the env variable is specified, use the value,
107  # otherwise only build with IB when IB support is detected on the system
108  if "USE_GLOO_IBVERBS" in os.environ:
109  USE_GLOO_IBVERBS = check_env_flag("USE_GLOO_IBVERBS")
110  else:
111  USE_GLOO_IBVERBS = should_build_ib()
Module caffe2.python.layers.split.