19
19
NCCL_INCLUDE_DIR , NCCL_ROOT_DIR , NCCL_SYSTEM_LIB
20
20
from tools .setup_helpers .nnpack import WITH_NNPACK , NNPACK_LIB_PATHS , \
21
21
NNPACK_INCLUDE_DIRS
22
+ from tools .setup_helpers .nvtoolext import NVTOOLEXT_HOME
22
23
from tools .setup_helpers .split_types import split_types
23
24
24
25
DEBUG = 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
26
32
WITH_DISTRIBUTED_MW = WITH_DISTRIBUTED and check_env_flag ('WITH_DISTRIBUTED_MW' )
27
33
28
34
@@ -82,14 +88,17 @@ def patched_link(self, *args, **kwargs):
82
88
83
89
dep_libs = [
84
90
'nccl' , 'ATen' ,
85
- 'libshm' , 'gloo' , 'THD' , 'nanopb' ,
91
+ 'libshm' , 'libshm_windows' , ' gloo' , 'THD' , 'nanopb' ,
86
92
]
87
93
88
94
89
95
def build_libs (libs ):
90
96
for lib in libs :
91
97
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' ]
93
102
my_env = os .environ .copy ()
94
103
my_env ["PYTORCH_PYTHON" ] = sys .executable
95
104
if WITH_SYSTEM_NCCL :
@@ -119,7 +128,11 @@ def run(self):
119
128
libs = []
120
129
if WITH_NCCL and not WITH_SYSTEM_NCCL :
121
130
libs += ['nccl' ]
122
- libs += ['ATen' , 'libshm' , 'nanopb' ]
131
+ libs += ['ATen' , 'nanopb' ]
132
+ if IS_WINDOWS :
133
+ libs += ['libshm_windows' ]
134
+ else :
135
+ libs += ['libshm' ]
123
136
if WITH_DISTRIBUTED :
124
137
if sys .platform .startswith ('linux' ):
125
138
libs += ['gloo' ]
@@ -201,6 +214,7 @@ def monkey_patch_THD_link_flags():
201
214
class build_ext (setuptools .command .build_ext .build_ext ):
202
215
203
216
def run (self ):
217
+
204
218
# Print build options
205
219
if WITH_NUMPY :
206
220
print ('-- Building with NumPy bindings' )
@@ -273,6 +287,25 @@ def run(self):
273
287
'torch/lib/tmp_install/share/ATen/Declarations.yaml' ,
274
288
jit_gen_dir )
275
289
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
+
276
309
# It's an old-style class in Python 2.7...
277
310
setuptools .command .build_ext .build_ext .run (self )
278
311
@@ -315,14 +348,23 @@ def run(self):
315
348
include_dirs = []
316
349
library_dirs = []
317
350
extra_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' ]
326
368
327
369
cwd = os .path .dirname (os .path .abspath (__file__ ))
328
370
lib_path = os .path .join (cwd , "torch" , "lib" )
@@ -355,13 +397,18 @@ def check_file(f):
355
397
ATEN_LIB = os .path .join (lib_path , 'libATen.so.1' )
356
398
THD_LIB = os .path .join (lib_path , 'libTHD.a' )
357
399
NCCL_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' )
361
400
362
401
# static library only
363
402
NANOPB_STATIC_LIB = os .path .join (lib_path , 'libprotobuf-nanopb.a' )
364
403
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
+
365
412
main_compile_args = ['-D_THP_CORE' ]
366
413
main_libraries = ['shm' ]
367
414
main_link_args = [ATEN_LIB , NANOPB_STATIC_LIB ]
@@ -457,20 +504,41 @@ def check_file(f):
457
504
include_dirs += [tmp_install_path + "/include/THD" ]
458
505
main_link_args += [THD_LIB ]
459
506
507
+ if IS_WINDOWS and not WITH_CUDA :
508
+ main_sources += ["torch/csrc/generated/AutoGPU_cpu_win.cpp" ]
509
+
460
510
if 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 )
462
536
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
467
537
include_dirs .append (cuda_include_path )
468
538
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 )
471
539
extra_compile_args += ['-DWITH_CUDA' ]
472
540
extra_compile_args += ['-DCUDA_LIB_PATH=' + cuda_lib_path ]
473
- main_libraries += ['cudart' , 'nvToolsExt' ]
541
+ main_libraries += ['cudart' , nvtoolext_lib_name ]
474
542
main_sources += [
475
543
"torch/csrc/cuda/Module.cpp" ,
476
544
"torch/csrc/cuda/Storage.cpp" ,
@@ -498,7 +566,8 @@ def check_file(f):
498
566
library_dirs .append (CUDNN_LIB_DIR )
499
567
# NOTE: these are at the front, in case there's another cuDNN in CUDA path
500
568
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 )
502
571
main_sources += [
503
572
"torch/csrc/cudnn/BatchNorm.cpp" ,
504
573
"torch/csrc/cudnn/Conv.cpp" ,
@@ -519,8 +588,11 @@ def check_file(f):
519
588
extra_compile_args += ['-DWITH_NNPACK' ]
520
589
521
590
if 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' ]
524
596
525
597
if os .getenv ('PYTORCH_BINARY_BUILD' ) and platform .system () == 'Linux' :
526
598
print ('PYTORCH_BINARY_BUILD found. Static linking libstdc++ on Linux' )
@@ -537,8 +609,10 @@ def check_file(f):
537
609
538
610
539
611
def make_relative_rpath (path ):
540
- if platform . system () == 'Darwin' :
612
+ if IS_DARWIN :
541
613
return '-Wl,-rpath,@loader_path/' + path
614
+ elif IS_WINDOWS :
615
+ return ''
542
616
else :
543
617
return '-Wl,-rpath,$ORIGIN/' + path
544
618
@@ -559,11 +633,12 @@ def make_relative_rpath(path):
559
633
)
560
634
extensions .append (C )
561
635
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 )
567
642
568
643
THNN = Extension ("torch._thnn._THNN" ,
569
644
sources = ['torch/csrc/nn/THNN.cpp' ],
@@ -579,7 +654,7 @@ def make_relative_rpath(path):
579
654
580
655
if WITH_CUDA :
581
656
thnvrtc_link_flags = extra_link_args + [make_relative_rpath ('lib' )]
582
- if platform . system () == 'Linux' :
657
+ if IS_LINUX :
583
658
thnvrtc_link_flags = ['-Wl,--no-as-needed' ] + thnvrtc_link_flags
584
659
THNVRTC = Extension ("torch._nvrtc" ,
585
660
libraries = ['nvrtc' , 'cuda' ],
@@ -634,7 +709,7 @@ def make_relative_rpath(path):
634
709
cmdclass = cmdclass ,
635
710
packages = packages ,
636
711
package_data = {'torch' : [
637
- 'lib/*.so*' , 'lib/*.dylib*' ,
712
+ 'lib/*.so*' , 'lib/*.dylib*' , 'lib/*.dll' ,
638
713
'lib/torch_shm_manager' ,
639
714
'lib/*.h' ,
640
715
'lib/include/TH/*.h' , 'lib/include/TH/generic/*.h' ,
0 commit comments