Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions docs/_tutorials/advanced-install.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ just-in-time (JIT) using [torch's JIT C++ extension loader that relies on
ninja](https://pytorch.org/docs/stable/cpp_extension.html) to build and
dynamically link them at runtime.

**Note:** [PyTorch](https://pytorch.org/) must be installed _before_ installing
DeepSpeed.
{: .notice--info}

```bash
pip install deepspeed
```
Expand All @@ -30,6 +26,9 @@ ds_report

## Pre-install DeepSpeed Ops

**Note:** [PyTorch](https://pytorch.org/) must be installed _before_ pre-compiling any DeepSpeed c++/cuda ops. However, this is not required if using the default mode of JIT compilition of ops.
{: .notice--info}

Sometimes we have found it useful to pre-install either some or all DeepSpeed
C++/CUDA ops instead of using the JIT compiled path. In order to support
pre-installation we introduce build environment flags to turn on/off building
Expand Down
8 changes: 7 additions & 1 deletion op_builder/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import os
import sys
import time
import torch
import importlib
from pathlib import Path
import subprocess
Expand All @@ -17,6 +16,13 @@
DEFAULT_TORCH_EXTENSION_PATH = "/tmp/torch_extensions"
DEFAULT_COMPUTE_CAPABILITIES = "6.0;6.1;7.0"

try:
import torch
except ImportError:
print(
f"{WARNING} unable to import torch, please install it if you want to pre-compile any deepspeed ops."
)


def installed_cuda_version():
import torch.utils.cpp_extension
Expand Down
3 changes: 2 additions & 1 deletion op_builder/cpu_adam.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""
import os
import sys
import torch
import subprocess
from .builder import CUDAOpBuilder

Expand All @@ -26,6 +25,7 @@ def sources(self):
return ['csrc/adam/cpu_adam.cpp', 'csrc/adam/custom_cuda_kernel.cu']

def include_paths(self):
import torch
CUDA_INCLUDE = os.path.join(torch.utils.cpp_extension.CUDA_HOME, "include")
return ['csrc/includes', CUDA_INCLUDE]

Expand All @@ -47,6 +47,7 @@ def simd_width(self):
return '-D__SCALAR__'

def cxx_args(self):
import torch
CUDA_LIB64 = os.path.join(torch.utils.cpp_extension.CUDA_HOME, "lib64")
SIMD_WIDTH = self.simd_width()

Expand Down
1 change: 0 additions & 1 deletion op_builder/fused_adam.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""
Copyright 2020 The Microsoft DeepSpeed Team
"""
import torch
from .builder import CUDAOpBuilder


Expand Down
1 change: 0 additions & 1 deletion op_builder/fused_lamb.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""
Copyright 2020 The Microsoft DeepSpeed Team
"""
import torch
from .builder import CUDAOpBuilder


Expand Down
1 change: 0 additions & 1 deletion op_builder/quantizer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import torch
from .builder import CUDAOpBuilder


Expand Down
7 changes: 6 additions & 1 deletion op_builder/sparse_attn.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""
Copyright 2020 The Microsoft DeepSpeed Team
"""
import torch
import warnings
from .builder import OpBuilder

Expand All @@ -28,6 +27,12 @@ def is_compatible(self):
#command_status = list(map(self.command_exists, required_commands))
#deps_compatible = all(command_status)

try:
import torch
except ImportError:
self.warning(f"unable to import torch, please install it first")
return False

# torch-cpu will not have a cuda version
if torch.version.cuda is None:
cuda_compatible = False
Expand Down
1 change: 0 additions & 1 deletion op_builder/stochastic_transformer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""
Copyright 2020 The Microsoft DeepSpeed Team
"""
import torch
from .transformer import TransformerBuilder


Expand Down
1 change: 0 additions & 1 deletion op_builder/transformer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""
Copyright 2020 The Microsoft DeepSpeed Team
"""
import torch
from .builder import CUDAOpBuilder


Expand Down
1 change: 0 additions & 1 deletion op_builder/transformer_inference.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import torch
from .builder import CUDAOpBuilder


Expand Down
27 changes: 19 additions & 8 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
from setuptools import setup, find_packages
import time

torch_available = True
try:
import torch
from torch.utils.cpp_extension import BuildExtension
except ImportError:
raise ImportError('Unable to import torch, please visit https://pytorch.org/ '
'to see how to properly install torch on your system.')
torch_available = False
print('[WARNING] Unable to import torch, pre-compiling ops will be disabled. ' \
'Please visit https://pytorch.org/ to see how to properly install torch on your system.')

from op_builder import ALL_OPS, get_default_compute_capatabilities

Expand All @@ -45,7 +47,7 @@ def fetch_requirements(path):
}

# If MPI is available add 1bit-adam requirements
if torch.cuda.is_available():
if torch_available and torch.cuda.is_available():
if shutil.which('ompi_info') or shutil.which('mpiname'):
cupy = f"cupy-cuda{torch.version.cuda.replace('.','')[:3]}"
extras_require['1bit_adam'].append(cupy)
Expand All @@ -60,12 +62,17 @@ def fetch_requirements(path):
cmdclass = {}

# For any pre-installed ops force disable ninja
cmdclass['build_ext'] = BuildExtension.with_options(use_ninja=False)
if torch_available:
cmdclass['build_ext'] = BuildExtension.with_options(use_ninja=False)

TORCH_MAJOR = torch.__version__.split('.')[0]
TORCH_MINOR = torch.__version__.split('.')[1]
if torch_available:
TORCH_MAJOR = torch.__version__.split('.')[0]
TORCH_MINOR = torch.__version__.split('.')[1]
else:
TORCH_MAJOR = "0"
TORCH_MINOR = "0"

if not torch.cuda.is_available():
if torch_available and not torch.cuda.is_available():
# Fix to allow docker builds, similar to https://github.com/NVIDIA/apex/issues/486
print(
"[WARNING] Torch did not find cuda available, if cross-compiling or running with cpu only "
Expand All @@ -81,6 +88,9 @@ def fetch_requirements(path):
BUILD_OP_DEFAULT = int(os.environ.get('DS_BUILD_OPS', BUILD_OP_PLATFORM))
print(f"DS_BUILD_OPS={BUILD_OP_DEFAULT}")

if BUILD_OP_DEFAULT:
assert torch_available, "Unable to pre-compile ops without torch installed. Please install torch before attempting to pre-compile ops."


def command_exists(cmd):
if sys.platform == "win32":
Expand Down Expand Up @@ -109,6 +119,7 @@ def op_enabled(op_name):

# If op install enabled, add builder to extensions
if op_enabled(op_name) and op_compatible:
assert torch_available, f"Unable to pre-compile {op_name}, please first install torch"
install_ops[op_name] = op_enabled(op_name)
ext_modules.append(builder.builder())

Expand Down Expand Up @@ -170,7 +181,7 @@ def create_dir_symlink(src, dest):
torch_version = ".".join([TORCH_MAJOR, TORCH_MINOR])
# Set cuda_version to 0.0 if cpu-only
cuda_version = "0.0"
if torch.version.cuda is not None:
if torch_available and torch.version.cuda is not None:
cuda_version = ".".join(torch.version.cuda.split('.')[:2])
torch_info = {"version": torch_version, "cuda_version": cuda_version}

Expand Down