Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build extensions in parallel #1882

Merged
merged 4 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ pip install -v --disable-pip-version-check --no-cache-dir --no-build-isolation -
pip install -v --disable-pip-version-check --no-cache-dir --no-build-isolation --global-option="--cpp_ext" --global-option="--cuda_ext" ./
```

To reduce the build time of APEX, parallel building can be enhanced via
```bash
export NVCC_APPEND_FLAGS="--threads 4"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest not exporting this env, it affects nvcc globally.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved it to Temporary Environment Scope

pip install -v --disable-pip-version-check --no-cache-dir --no-build-isolation --config-settings "--build-option=--cpp_ext --cuda_ext --parallel 8" ./
```
When CPU cores or memory are limited, the `--parallel` option is generally preferred over `--threads`. See [pull#1882](https://github.com/NVIDIA/apex/pull/1882) for more details.

APEX also supports a Python-only build via
```bash
pip install -v --disable-pip-version-check --no-build-isolation --no-cache-dir ./
Expand Down
41 changes: 40 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys
import warnings
import os
import threading
import glob
from packaging.version import parse, Version

Expand Down Expand Up @@ -859,6 +860,44 @@ def check_cudnn_version_and_warn(global_option: str, required_cudnn_version: int
)


# Patch because `setup.py bdist_wheel` and `setup.py develop` do not support the `parallel` option
parallel = None
if "--parallel" in sys.argv:
idx = sys.argv.index("--parallel")
parallel = int(sys.argv[idx + 1])
sys.argv.pop(idx + 1)
sys.argv.pop(idx)


# Prevent file conflicts when multiple extensions are compiled simultaneously
class BuildExtensionSeparateDir(BuildExtension):
build_extension_patch_lock = threading.Lock()
thread_ext_name_map = {}

def finalize_options(self):
if parallel is not None:
self.parallel = parallel
super().finalize_options()

def build_extension(self, ext):
with self.build_extension_patch_lock:
if not getattr(self.compiler, "_compile_separate_output_dir", False):
compile_orig = self.compiler.compile

def compile_new(*args, **kwargs):
return compile_orig(*args, **{
**kwargs,
"output_dir": os.path.join(
kwargs["output_dir"],
self.thread_ext_name_map[threading.current_thread().ident]),
})
self.compiler.compile = compile_new
self.compiler._compile_separate_output_dir = True
self.thread_ext_name_map[threading.current_thread().ident] = ext.name
objects = super().build_extension(ext)
return objects


setup(
name="apex",
version="0.1",
Expand All @@ -868,6 +907,6 @@ def check_cudnn_version_and_warn(global_option: str, required_cudnn_version: int
install_requires=["packaging>20.6"],
description="PyTorch Extensions written by NVIDIA",
ext_modules=ext_modules,
cmdclass={"build_ext": BuildExtension} if ext_modules else {},
cmdclass={"build_ext": BuildExtensionSeparateDir} if ext_modules else {},
extras_require=extras,
)