1919 NCCL_INCLUDE_DIR , NCCL_ROOT_DIR , NCCL_SYSTEM_LIB
2020from tools .setup_helpers .nnpack import WITH_NNPACK , NNPACK_LIB_PATHS , \
2121 NNPACK_INCLUDE_DIRS
22+ from tools .setup_helpers .nvtoolext import NVTOOLEXT_HOME
2223from tools .setup_helpers .split_types import split_types
2324
2425DEBUG = check_env_flag ('DEBUG' )
25- WITH_DISTRIBUTED = not check_env_flag ('NO_DISTRIBUTED' )
26+
27+ IS_WINDOWS = (platform .system () == 'Windows' )
28+ IS_DARWIN = (platform .system () == 'Darwin' )
29+ IS_LINUX = (platform .system () == 'Linux' )
30+
31+ WITH_DISTRIBUTED = not check_env_flag ('NO_DISTRIBUTED' ) and not IS_WINDOWS
2632WITH_DISTRIBUTED_MW = WITH_DISTRIBUTED and check_env_flag ('WITH_DISTRIBUTED_MW' )
2733
2834
@@ -82,14 +88,17 @@ def patched_link(self, *args, **kwargs):
8288
8389dep_libs = [
8490 'nccl' , 'ATen' ,
85- 'libshm' , 'gloo' , 'THD' , 'nanopb' ,
91+ 'libshm' , 'libshm_windows' , ' gloo' , 'THD' , 'nanopb' ,
8692]
8793
8894
8995def build_libs (libs ):
9096 for lib in libs :
9197 assert lib in dep_libs , 'invalid lib: {}' .format (lib )
92- build_libs_cmd = ['bash' , 'torch/lib/build_libs.sh' ]
98+ if IS_WINDOWS :
99+ build_libs_cmd = ['torch\\ lib\\ build_libs.bat' ]
100+ else :
101+ build_libs_cmd = ['bash' , 'torch/lib/build_libs.sh' ]
93102 my_env = os .environ .copy ()
94103 my_env ["PYTORCH_PYTHON" ] = sys .executable
95104 if WITH_SYSTEM_NCCL :
@@ -119,7 +128,11 @@ def run(self):
119128 libs = []
120129 if WITH_NCCL and not WITH_SYSTEM_NCCL :
121130 libs += ['nccl' ]
122- libs += ['ATen' , 'libshm' , 'nanopb' ]
131+ libs += ['ATen' , 'nanopb' ]
132+ if IS_WINDOWS :
133+ libs += ['libshm_windows' ]
134+ else :
135+ libs += ['libshm' ]
123136 if WITH_DISTRIBUTED :
124137 if sys .platform .startswith ('linux' ):
125138 libs += ['gloo' ]
@@ -201,6 +214,7 @@ def monkey_patch_THD_link_flags():
201214class build_ext (setuptools .command .build_ext .build_ext ):
202215
203216 def run (self ):
217+
204218 # Print build options
205219 if WITH_NUMPY :
206220 print ('-- Building with NumPy bindings' )
@@ -273,6 +287,25 @@ def run(self):
273287 'torch/lib/tmp_install/share/ATen/Declarations.yaml' ,
274288 jit_gen_dir )
275289
290+ if IS_WINDOWS :
291+ build_temp = self .build_temp
292+ build_dir = 'torch/csrc'
293+
294+ ext_filename = self .get_ext_filename ('_C' )
295+ lib_filename = '.' .join (ext_filename .split ('.' )[:- 1 ]) + '.lib'
296+
297+ _C_LIB = os .path .join (build_temp , build_dir , lib_filename ).replace ('\\ ' , '/' )
298+
299+ THNN .extra_link_args += [_C_LIB ]
300+ if WITH_CUDA :
301+ THCUNN .extra_link_args += [_C_LIB ]
302+ else :
303+ # To generate .obj files for AutoGPU for the export class
304+ # a header file cannot build, so it has to be copied to someplace as a source file
305+ if os .path .exists ("torch/csrc/generated/AutoGPU_cpu_win.cpp" ):
306+ os .remove ("torch/csrc/generated/AutoGPU_cpu_win.cpp" )
307+ shutil .copyfile ("torch/csrc/cuda/AutoGPU.h" , "torch/csrc/generated/AutoGPU_cpu_win.cpp" )
308+
276309 # It's an old-style class in Python 2.7...
277310 setuptools .command .build_ext .build_ext .run (self )
278311
@@ -315,14 +348,23 @@ def run(self):
315348include_dirs = []
316349library_dirs = []
317350extra_link_args = []
318- extra_compile_args = ['-std=c++11' , '-Wno-write-strings' ,
319- # Python 2.6 requires -fno-strict-aliasing, see
320- # http://legacy.python.org/dev/peps/pep-3123/
321- '-fno-strict-aliasing' ,
322- # Clang has an unfixed bug leading to spurious missing
323- # braces warnings, see
324- # https://bugs.llvm.org/show_bug.cgi?id=21629
325- '-Wno-missing-braces' ]
351+
352+ if IS_WINDOWS :
353+ extra_compile_args = ['/Z7' , '/EHa' , '/DNOMINMAX'
354+ # /Z7 turns on symbolic debugging information in .obj files
355+ # /EHa is about native C++ catch support for asynchronous
356+ # structured exception handling (SEH)
357+ # /DNOMINMAX removes builtin min/max functions
358+ ]
359+ else :
360+ extra_compile_args = ['-std=c++11' , '-Wno-write-strings' ,
361+ # Python 2.6 requires -fno-strict-aliasing, see
362+ # http://legacy.python.org/dev/peps/pep-3123/
363+ '-fno-strict-aliasing' ,
364+ # Clang has an unfixed bug leading to spurious missing
365+ # braces warnings, see
366+ # https://bugs.llvm.org/show_bug.cgi?id=21629
367+ '-Wno-missing-braces' ]
326368
327369cwd = os .path .dirname (os .path .abspath (__file__ ))
328370lib_path = os .path .join (cwd , "torch" , "lib" )
@@ -355,13 +397,18 @@ def check_file(f):
355397ATEN_LIB = os .path .join (lib_path , 'libATen.so.1' )
356398THD_LIB = os .path .join (lib_path , 'libTHD.a' )
357399NCCL_LIB = os .path .join (lib_path , 'libnccl.so.1' )
358- if platform .system () == 'Darwin' :
359- ATEN_LIB = os .path .join (lib_path , 'libATen.1.dylib' )
360- NCCL_LIB = os .path .join (lib_path , 'libnccl.1.dylib' )
361400
362401# static library only
363402NANOPB_STATIC_LIB = os .path .join (lib_path , 'libprotobuf-nanopb.a' )
364403
404+ if IS_DARWIN :
405+ ATEN_LIB = os .path .join (lib_path , 'libATen.1.dylib' )
406+ NCCL_LIB = os .path .join (lib_path , 'libnccl.1.dylib' )
407+
408+ if IS_WINDOWS :
409+ ATEN_LIB = os .path .join (lib_path , 'ATen.lib' )
410+ NANOPB_STATIC_LIB = os .path .join (lib_path , 'protobuf-nanopb.lib' )
411+
365412main_compile_args = ['-D_THP_CORE' ]
366413main_libraries = ['shm' ]
367414main_link_args = [ATEN_LIB , NANOPB_STATIC_LIB ]
@@ -457,20 +504,41 @@ def check_file(f):
457504 include_dirs += [tmp_install_path + "/include/THD" ]
458505 main_link_args += [THD_LIB ]
459506
507+ if IS_WINDOWS and not WITH_CUDA :
508+ main_sources += ["torch/csrc/generated/AutoGPU_cpu_win.cpp" ]
509+
460510if WITH_CUDA :
461- cuda_lib_dirs = ['lib64' , 'lib' ]
511+ nvtoolext_lib_name = None
512+ if IS_WINDOWS :
513+ cuda_lib_path = CUDA_HOME + '/lib/x64/'
514+ nvtoolext_lib_path = NVTOOLEXT_HOME + '/lib/x64/'
515+ nvtoolext_include_path = os .path .join (NVTOOLEXT_HOME , 'include' )
516+
517+ library_dirs .append (nvtoolext_lib_path )
518+ include_dirs .append (nvtoolext_include_path )
519+
520+ nvtoolext_lib_name = 'nvToolsExt64_1'
521+
522+ # MSVC doesn't support runtime symbol resolving, `nvrtc` and `cuda` should be linked
523+ main_libraries += ['nvrtc' , 'cuda' ]
524+ else :
525+ cuda_lib_dirs = ['lib64' , 'lib' ]
526+
527+ for lib_dir in cuda_lib_dirs :
528+ cuda_lib_path = os .path .join (CUDA_HOME , lib_dir )
529+ if os .path .exists (cuda_lib_path ):
530+ break
531+ extra_link_args .append ('-Wl,-rpath,' + cuda_lib_path )
532+
533+ nvtoolext_lib_name = 'nvToolsExt'
534+
535+ library_dirs .append (cuda_lib_path )
462536 cuda_include_path = os .path .join (CUDA_HOME , 'include' )
463- for lib_dir in cuda_lib_dirs :
464- cuda_lib_path = os .path .join (CUDA_HOME , lib_dir )
465- if os .path .exists (cuda_lib_path ):
466- break
467537 include_dirs .append (cuda_include_path )
468538 include_dirs .append (tmp_install_path + "/include/THCUNN" )
469- library_dirs .append (cuda_lib_path )
470- extra_link_args .append ('-Wl,-rpath,' + cuda_lib_path )
471539 extra_compile_args += ['-DWITH_CUDA' ]
472540 extra_compile_args += ['-DCUDA_LIB_PATH=' + cuda_lib_path ]
473- main_libraries += ['cudart' , 'nvToolsExt' ]
541+ main_libraries += ['cudart' , nvtoolext_lib_name ]
474542 main_sources += [
475543 "torch/csrc/cuda/Module.cpp" ,
476544 "torch/csrc/cuda/Storage.cpp" ,
@@ -498,7 +566,8 @@ def check_file(f):
498566 library_dirs .append (CUDNN_LIB_DIR )
499567 # NOTE: these are at the front, in case there's another cuDNN in CUDA path
500568 include_dirs .insert (0 , CUDNN_INCLUDE_DIR )
501- extra_link_args .insert (0 , '-Wl,-rpath,' + CUDNN_LIB_DIR )
569+ if not IS_WINDOWS :
570+ extra_link_args .insert (0 , '-Wl,-rpath,' + CUDNN_LIB_DIR )
502571 main_sources += [
503572 "torch/csrc/cudnn/BatchNorm.cpp" ,
504573 "torch/csrc/cudnn/Conv.cpp" ,
@@ -519,8 +588,11 @@ def check_file(f):
519588 extra_compile_args += ['-DWITH_NNPACK' ]
520589
521590if DEBUG :
522- extra_compile_args += ['-O0' , '-g' ]
523- extra_link_args += ['-O0' , '-g' ]
591+ if IS_WINDOWS :
592+ extra_link_args .append ('/DEBUG:FULL' )
593+ else :
594+ extra_compile_args += ['-O0' , '-g' ]
595+ extra_link_args += ['-O0' , '-g' ]
524596
525597if os .getenv ('PYTORCH_BINARY_BUILD' ) and platform .system () == 'Linux' :
526598 print ('PYTORCH_BINARY_BUILD found. Static linking libstdc++ on Linux' )
@@ -537,8 +609,10 @@ def check_file(f):
537609
538610
539611def make_relative_rpath (path ):
540- if platform . system () == 'Darwin' :
612+ if IS_DARWIN :
541613 return '-Wl,-rpath,@loader_path/' + path
614+ elif IS_WINDOWS :
615+ return ''
542616 else :
543617 return '-Wl,-rpath,$ORIGIN/' + path
544618
@@ -559,11 +633,12 @@ def make_relative_rpath(path):
559633 )
560634extensions .append (C )
561635
562- DL = Extension ("torch._dl" ,
563- sources = ["torch/csrc/dl.c" ],
564- language = 'c' ,
565- )
566- extensions .append (DL )
636+ if not IS_WINDOWS :
637+ DL = Extension ("torch._dl" ,
638+ sources = ["torch/csrc/dl.c" ],
639+ language = 'c' ,
640+ )
641+ extensions .append (DL )
567642
568643THNN = Extension ("torch._thnn._THNN" ,
569644 sources = ['torch/csrc/nn/THNN.cpp' ],
@@ -579,7 +654,7 @@ def make_relative_rpath(path):
579654
580655if WITH_CUDA :
581656 thnvrtc_link_flags = extra_link_args + [make_relative_rpath ('lib' )]
582- if platform . system () == 'Linux' :
657+ if IS_LINUX :
583658 thnvrtc_link_flags = ['-Wl,--no-as-needed' ] + thnvrtc_link_flags
584659 THNVRTC = Extension ("torch._nvrtc" ,
585660 libraries = ['nvrtc' , 'cuda' ],
@@ -634,7 +709,7 @@ def make_relative_rpath(path):
634709 cmdclass = cmdclass ,
635710 packages = packages ,
636711 package_data = {'torch' : [
637- 'lib/*.so*' , 'lib/*.dylib*' ,
712+ 'lib/*.so*' , 'lib/*.dylib*' , 'lib/*.dll' ,
638713 'lib/torch_shm_manager' ,
639714 'lib/*.h' ,
640715 'lib/include/TH/*.h' , 'lib/include/TH/generic/*.h' ,
0 commit comments