11import sys
22import warnings
33import os
4+ import threading
45import glob
56from packaging .version import parse , Version
67
@@ -859,6 +860,39 @@ def check_cudnn_version_and_warn(global_option: str, required_cudnn_version: int
859860 )
860861
861862
863+ # Patch because `setup.py bdist_wheel` does not accept the `parallel` option
864+ parallel = None
865+ if "--parallel" in sys .argv :
866+ idx = sys .argv .index ("--parallel" )
867+ parallel = int (sys .argv [idx + 1 ])
868+ sys .argv .pop (idx + 1 )
869+ sys .argv .pop (idx )
870+
871+
872+ # Prevent file conflicts when multiple extensions are compiled simultaneously
873+ class BuildExtensionSeparateDir (BuildExtension ):
874+ build_extension_patch_lock = threading .Lock ()
875+ thread_ext_name_map = {}
876+
877+ def build_extension (self , ext ):
878+ with self .build_extension_patch_lock :
879+ if not getattr (self .compiler , "_compile_separate_output_dir" , False ):
880+ compile_orig = self .compiler .compile
881+
882+ def compile_new (* args , ** kwargs ):
883+ return compile_orig (* args , ** {
884+ ** kwargs ,
885+ "output_dir" : os .path .join (
886+ kwargs ["output_dir" ],
887+ self .thread_ext_name_map [threading .current_thread ().ident ]),
888+ })
889+ self .compiler .compile = compile_new
890+ self .compiler ._compile_separate_output_dir = True
891+ self .thread_ext_name_map [threading .current_thread ().ident ] = ext .name
892+ objects = super ().build_extension (ext )
893+ return objects
894+
895+
862896setup (
863897 name = "apex" ,
864898 version = "0.1" ,
@@ -868,6 +902,6 @@ def check_cudnn_version_and_warn(global_option: str, required_cudnn_version: int
868902 install_requires = ["packaging>20.6" ],
869903 description = "PyTorch Extensions written by NVIDIA" ,
870904 ext_modules = ext_modules ,
871- cmdclass = {"build_ext" : BuildExtension } if ext_modules else {},
905+ cmdclass = {"build_ext" : BuildExtensionSeparateDir . with_options ( parallel = parallel ) } if ext_modules else {},
872906 extras_require = extras ,
873907)
0 commit comments