Caffe2 - Python API
A deep learning, cross platform ML framework
nccl.py
1 import os
2 import glob
3 import warnings
4 from itertools import chain
5 
6 from .env import IS_WINDOWS, IS_DARWIN, IS_CONDA, CONDA_DIR, check_negative_env_flag, \
7  gather_paths
8 
9 from .cuda import USE_CUDA, CUDA_HOME
10 
11 USE_NCCL = USE_CUDA and not check_negative_env_flag('USE_NCCL') and not IS_DARWIN and not IS_WINDOWS
12 USE_SYSTEM_NCCL = False
13 NCCL_LIB_DIR = None
14 NCCL_SYSTEM_LIB = None
15 NCCL_INCLUDE_DIR = None
16 NCCL_ROOT_DIR = None
17 USE_STATIC_NCCL = os.getenv("USE_STATIC_NCCL")
18 LIBNCCL_PREFIX = "libnccl"
19 if USE_STATIC_NCCL is not None:
20  LIBNCCL_PREFIX = "libnccl_static"
21 
22 if USE_CUDA and not check_negative_env_flag('USE_SYSTEM_NCCL'):
23  ENV_ROOT = os.getenv('NCCL_ROOT_DIR', None)
24  LIB_DIR = os.getenv('NCCL_LIB_DIR', None)
25  INCLUDE_DIR = os.getenv('NCCL_INCLUDE_DIR', None)
26 
27  lib_paths = list(filter(bool, [
28  LIB_DIR,
29  ENV_ROOT,
30  os.path.join(ENV_ROOT, 'lib') if ENV_ROOT is not None else None,
31  os.path.join(ENV_ROOT, 'lib', 'x86_64-linux-gnu') if ENV_ROOT is not None else None,
32  os.path.join(ENV_ROOT, 'lib64') if ENV_ROOT is not None else None,
33  os.path.join(CUDA_HOME, 'lib'),
34  os.path.join(CUDA_HOME, 'lib64'),
35  '/usr/local/lib',
36  '/usr/lib/x86_64-linux-gnu/',
37  '/usr/lib/powerpc64le-linux-gnu/',
38  '/usr/lib/aarch64-linux-gnu/',
39  '/usr/lib',
40  ] + gather_paths([
41  'LIBRARY_PATH',
42  ]) + gather_paths([
43  'LD_LIBRARY_PATH',
44  ])))
45  include_paths = list(filter(bool, [
46  INCLUDE_DIR,
47  ENV_ROOT,
48  os.path.join(ENV_ROOT, 'include') if ENV_ROOT is not None else None,
49  os.path.join(CUDA_HOME, 'include'),
50  '/usr/local/include',
51  '/usr/include',
52  ]))
53 
54  if IS_CONDA:
55  lib_paths.append(os.path.join(CONDA_DIR, 'lib'))
56  for path in lib_paths:
57  path = os.path.expanduser(path)
58  if path is None or not os.path.exists(path):
59  continue
60  if glob.glob(os.path.join(path, LIBNCCL_PREFIX + '*')):
61  NCCL_LIB_DIR = path
62  # try to find an exact versioned .so/.dylib, rather than libnccl.so
63  preferred_path = glob.glob(os.path.join(path, LIBNCCL_PREFIX + '*[0-9]*'))
64  if len(preferred_path) == 0:
65  NCCL_SYSTEM_LIB = glob.glob(os.path.join(path, LIBNCCL_PREFIX + '*'))[0]
66  else:
67  NCCL_SYSTEM_LIB = os.path.realpath(preferred_path[0])
68  break
69  for path in include_paths:
70  path = os.path.expanduser(path)
71  if path is None or not os.path.exists(path):
72  continue
73  if glob.glob(os.path.join(path, 'nccl.h')):
74  NCCL_INCLUDE_DIR = path
75  break
76  if NCCL_LIB_DIR is not None and NCCL_INCLUDE_DIR is not None:
77  USE_SYSTEM_NCCL = True
78  NCCL_ROOT_DIR = os.path.commonprefix((NCCL_LIB_DIR, NCCL_INCLUDE_DIR))