Skip to content

Commit

Permalink
Revert D16281714: Add sanity checks for NCCL detection.
Browse files Browse the repository at this point in the history
Differential Revision:
D16281714

Original commit changeset: 396bcbf099bd

fbshipit-source-id: a22cc112d1b6a62d689f9d8a7f93e8be3abe2a44
  • Loading branch information
ezyang authored and facebook-github-bot committed Jul 16, 2019
1 parent 7586ffd commit 798d5d9
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 39 deletions.
11 changes: 2 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,8 @@ option(USE_LITE_PROTO "Use lite protobuf instead of full." OFF)
option(USE_LMDB "Use LMDB" OFF)
option(USE_METAL "Use Metal for iOS build" ON)
option(USE_NATIVE_ARCH "Use -march=native" OFF)
cmake_dependent_option(
USE_NCCL "Use NCCL" ON
"USE_CUDA;UNIX;NOT APPLE" OFF)
cmake_dependent_option(
USE_STATIC_NCCL "Use static NCCL" OFF
"USE_NCCL" OFF)
cmake_dependent_option(
USE_SYSTEM_NCCL "Use system-wide NCCL" ON
"USE_NCCL" OFF)
option(USE_NCCL "Use NCCL" ON)
option(USE_SYSTEM_NCCL "Use system-wide NCCL" OFF)
option(USE_NNAPI "Use NNAPI" OFF)
option(USE_NNPACK "Use NNPACK" ON)
option(USE_NUMA "Use NUMA (only available on Linux)" ON)
Expand Down
17 changes: 14 additions & 3 deletions cmake/External/nccl.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@ if (NOT __NCCL_INCLUDED)
set(__NCCL_INCLUDED TRUE)

if (USE_SYSTEM_NCCL)
# NCCL_ROOT, NCCL_LIB_DIR, NCCL_INCLUDE_DIR will be accounted in the following line.
find_package(NCCL REQUIRED)
if (NCCL_FOUND)
# if we have explicit paths passed from setup.py, use those
if (NCCL_INCLUDE_DIR)
# used by gloo cmake among others
SET(NCCL_INCLUDE_DIRS ${NCCL_INCLUDE_DIR})
SET(NCCL_LIBRARIES ${NCCL_SYSTEM_LIB})
set(NCCL_FOUND TRUE)
add_library(__caffe2_nccl INTERFACE)
target_link_libraries(__caffe2_nccl INTERFACE ${NCCL_LIBRARIES})
target_include_directories(__caffe2_nccl INTERFACE ${NCCL_INCLUDE_DIRS})
else()
find_package(NCCL)
if (NCCL_FOUND)
add_library(__caffe2_nccl INTERFACE)
target_link_libraries(__caffe2_nccl INTERFACE ${NCCL_LIBRARIES})
target_include_directories(__caffe2_nccl INTERFACE ${NCCL_INCLUDE_DIRS})
endif()
endif()
else()
torch_cuda_get_nvcc_gencode_flag(NVCC_GENCODE)
Expand Down Expand Up @@ -47,4 +57,5 @@ if (NOT __NCCL_INCLUDED)
target_link_libraries(__caffe2_nccl INTERFACE ${NCCL_LIBRARIES})
target_include_directories(__caffe2_nccl INTERFACE ${NCCL_INCLUDE_DIRS})
endif()

endif()
43 changes: 19 additions & 24 deletions cmake/Modules/FindNCCL.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Find the nccl libraries
#
# The following variables are optionally searched for defaults
# NCCL_ROOT: Base directory where all NCCL components are found
# NCCL_ROOT_DIR: Base directory where all NCCL components are found
# NCCL_INCLUDE_DIR: Directory where NCCL header is found
# NCCL_LIB_DIR: Directory where NCCL library is found
#
Expand All @@ -14,37 +14,32 @@
# install NCCL in the same location as the CUDA toolkit.
# See https://github.com/caffe2/caffe2/issues/1601

set(NCCL_INCLUDE_DIR $ENV{NCCL_INCLUDE_DIR} CACHE PATH "Folder contains NVIDIA NCCL headers")
set(NCCL_LIB_DIR $ENV{NCCL_LIB_DIR} CACHE PATH "Folder contains NVIDIA NCCL libraries")
set(NCCL_VERSION $ENV{NCCL_VERSION} CACHE STRING "Version of NCCL to build with")

if ($ENV{NCCL_ROOT_DIR})
message(WARNING "NCCL_ROOT_DIR is deprecated. Please set NCCL_ROOT instead.")
endif()
list(APPEND NCCL_ROOT $ENV{NCCL_ROOT_DIR} ${CUDA_TOOLKIT_ROOT_DIR})
# Compatible layer for CMake <3.12. NCCL_ROOT will be accounted in for searching paths and libraries for CMake >=3.12.
list(APPEND CMAKE_PREFIX_PATH ${NCCL_ROOT})
set(NCCL_ROOT_DIR $ENV{NCCL_ROOT_DIR} CACHE PATH "Folder contains NVIDIA NCCL")

find_path(NCCL_INCLUDE_DIRS
NAMES nccl.h
HINTS ${NCCL_INCLUDE_DIR})
HINTS
${NCCL_INCLUDE_DIR}
${NCCL_ROOT_DIR}
${NCCL_ROOT_DIR}/include
${CUDA_TOOLKIT_ROOT_DIR}/include)

if (USE_STATIC_NCCL)
MESSAGE(STATUS "USE_STATIC_NCCL is set. Linking with static NCCL library.")
SET(NCCL_LIBNAME "nccl_static")
if (NCCL_VERSION) # Prefer the versioned library if a specific NCCL version is specified
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a.${NCCL_VERSION}" ${CMAKE_FIND_LIBRARY_SUFFIXES})
endif()
else()
IF ($ENV{USE_STATIC_NCCL})
MESSAGE(STATUS "USE_STATIC_NCCL detected. Linking against static NCCL library")
SET(NCCL_LIBNAME "libnccl_static.a")
ELSE()
SET(NCCL_LIBNAME "nccl")
if (NCCL_VERSION) # Prefer the versioned library if a specific NCCL version is specified
set(CMAKE_FIND_LIBRARY_SUFFIXES ".so.${NCCL_VERSION}" ${CMAKE_FIND_LIBRARY_SUFFIXES})
endif()
endif()
ENDIF()

find_library(NCCL_LIBRARIES
NAMES ${NCCL_LIBNAME}
HINTS ${NCCL_LIB_DIR})
HINTS
${NCCL_LIB_DIR}
${NCCL_ROOT_DIR}
${NCCL_ROOT_DIR}/lib
${NCCL_ROOT_DIR}/lib/x86_64-linux-gnu
${NCCL_ROOT_DIR}/lib64
${CUDA_TOOLKIT_ROOT_DIR}/lib64)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(NCCL DEFAULT_MSG NCCL_INCLUDE_DIRS NCCL_LIBRARIES)
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
# MIOPEN_LIBRARY
# specify where MIOpen is installed
#
# NCCL_ROOT
# NCCL_ROOT_DIR
# NCCL_LIB_DIR
# NCCL_INCLUDE_DIR
# specify where nccl is installed
Expand Down Expand Up @@ -187,6 +187,7 @@
from tools.setup_helpers.cmake import CMake
from tools.setup_helpers.cuda import CUDA_HOME, CUDA_VERSION
from tools.setup_helpers.cudnn import CUDNN_LIBRARY, CUDNN_INCLUDE_DIR
from tools.setup_helpers.nccl import NCCL_SYSTEM_LIB, NCCL_INCLUDE_DIR

try:
FileNotFoundError
Expand Down Expand Up @@ -386,8 +387,7 @@ def run(self):
else:
report('-- Not using MKLDNN')
if cmake_cache_vars['USE_NCCL'] and cmake_cache_vars['USE_SYSTEM_NCCL']:
report('-- Using system provided NCCL library at {}, {}'.format(cmake_cache_vars['NCCL_LIBRARIES'],
cmake_cache_vars['NCCL_INCLUDE_DIRS']))
report('-- Using system provided NCCL library at ' + NCCL_SYSTEM_LIB + ', ' + NCCL_INCLUDE_DIR)
elif cmake_cache_vars['USE_NCCL']:
report('-- Building NCCL library')
else:
Expand Down
7 changes: 7 additions & 0 deletions tools/setup_helpers/cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
check_env_flag, check_negative_env_flag)
from .cuda import USE_CUDA
from .dist_check import USE_DISTRIBUTED, USE_GLOO_IBVERBS
from .nccl import (USE_SYSTEM_NCCL, NCCL_INCLUDE_DIR, NCCL_ROOT_DIR,
NCCL_SYSTEM_LIB, USE_NCCL)
from .numpy_ import USE_NUMPY, NUMPY_INCLUDE_DIR


Expand Down Expand Up @@ -270,6 +272,8 @@ def generate(self, version, cmake_python_library, build_python, build_test, my_e
'USE_DISTRIBUTED': USE_DISTRIBUTED,
'USE_FBGEMM': not (check_env_flag('NO_FBGEMM') or
check_negative_env_flag('USE_FBGEMM')),
'USE_NCCL': USE_NCCL,
'USE_SYSTEM_NCCL': USE_SYSTEM_NCCL,
'USE_NUMPY': USE_NUMPY,
'USE_SYSTEM_EIGEN_INSTALL': 'OFF'
})
Expand All @@ -282,6 +286,9 @@ def generate(self, version, cmake_python_library, build_python, build_test, my_e
CMAKE_BUILD_TYPE=self._build_type,
INSTALL_TEST=build_test,
NUMPY_INCLUDE_DIR=escape_path(NUMPY_INCLUDE_DIR),
NCCL_INCLUDE_DIR=NCCL_INCLUDE_DIR,
NCCL_ROOT_DIR=NCCL_ROOT_DIR,
NCCL_SYSTEM_LIB=NCCL_SYSTEM_LIB,
CMAKE_INSTALL_PREFIX=install_dir,
CMAKE_C_FLAGS=cflags,
CMAKE_CXX_FLAGS=cflags,
Expand Down
76 changes: 76 additions & 0 deletions tools/setup_helpers/nccl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import os
import glob

from .env import IS_WINDOWS, IS_DARWIN, IS_CONDA, CONDA_DIR, check_negative_env_flag, \
gather_paths

from .cuda import USE_CUDA, CUDA_HOME

USE_NCCL = USE_CUDA and not check_negative_env_flag('USE_NCCL') and not IS_DARWIN and not IS_WINDOWS
USE_SYSTEM_NCCL = False
NCCL_LIB_DIR = None
NCCL_SYSTEM_LIB = None
NCCL_INCLUDE_DIR = None
NCCL_ROOT_DIR = None
USE_STATIC_NCCL = os.getenv("USE_STATIC_NCCL")
LIBNCCL_PREFIX = "libnccl"
if USE_STATIC_NCCL is not None:
LIBNCCL_PREFIX = "libnccl_static"

if USE_CUDA and not check_negative_env_flag('USE_SYSTEM_NCCL'):
ENV_ROOT = os.getenv('NCCL_ROOT_DIR', None)
LIB_DIR = os.getenv('NCCL_LIB_DIR', None)
INCLUDE_DIR = os.getenv('NCCL_INCLUDE_DIR', None)

lib_paths = list(filter(bool, [
LIB_DIR,
ENV_ROOT,
os.path.join(ENV_ROOT, 'lib') if ENV_ROOT is not None else None,
os.path.join(ENV_ROOT, 'lib', 'x86_64-linux-gnu') if ENV_ROOT is not None else None,
os.path.join(ENV_ROOT, 'lib64') if ENV_ROOT is not None else None,
os.path.join(CUDA_HOME, 'lib'),
os.path.join(CUDA_HOME, 'lib64'),
'/usr/local/lib',
'/usr/lib/x86_64-linux-gnu/',
'/usr/lib/powerpc64le-linux-gnu/',
'/usr/lib/aarch64-linux-gnu/',
'/usr/lib',
] + gather_paths([
'LIBRARY_PATH',
]) + gather_paths([
'LD_LIBRARY_PATH',
])))
include_paths = list(filter(bool, [
INCLUDE_DIR,
ENV_ROOT,
os.path.join(ENV_ROOT, 'include') if ENV_ROOT is not None else None,
os.path.join(CUDA_HOME, 'include'),
'/usr/local/include',
'/usr/include',
]))

if IS_CONDA:
lib_paths.append(os.path.join(CONDA_DIR, 'lib'))
for path in lib_paths:
path = os.path.expanduser(path)
if path is None or not os.path.exists(path):
continue
if glob.glob(os.path.join(path, LIBNCCL_PREFIX + '*')):
NCCL_LIB_DIR = path
# try to find an exact versioned .so/.dylib, rather than libnccl.so
preferred_path = glob.glob(os.path.join(path, LIBNCCL_PREFIX + '*[0-9]*'))
if len(preferred_path) == 0:
NCCL_SYSTEM_LIB = glob.glob(os.path.join(path, LIBNCCL_PREFIX + '*'))[0]
else:
NCCL_SYSTEM_LIB = os.path.realpath(preferred_path[0])
break
for path in include_paths:
path = os.path.expanduser(path)
if path is None or not os.path.exists(path):
continue
if glob.glob(os.path.join(path, 'nccl.h')):
NCCL_INCLUDE_DIR = path
break
if NCCL_LIB_DIR is not None and NCCL_INCLUDE_DIR is not None:
USE_SYSTEM_NCCL = True
NCCL_ROOT_DIR = os.path.commonprefix((NCCL_LIB_DIR, NCCL_INCLUDE_DIR))

0 comments on commit 798d5d9

Please sign in to comment.