forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
1676 lines (1500 loc) · 69.2 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# ---[ Generate and install header and cpp files
include(../cmake/Codegen.cmake)
# ---[ Vulkan code gen
if(USE_VULKAN)
include(../cmake/VulkanCodegen.cmake)
endif()
# ---[ MSVC OpenMP modification
if(MSVC)
include(../cmake/public/utils.cmake)
endif()
# ATen parallelism settings
# OMP - OpenMP for intra-op, native thread pool for inter-op parallelism
# NATIVE - using native thread pool for intra- and inter-op parallelism
# TBB - using TBB for intra- and native thread pool for inter-op parallelism
if(INTERN_BUILD_MOBILE AND NOT BUILD_CAFFE2_MOBILE)
set(ATEN_THREADING "NATIVE" CACHE STRING "ATen parallel backend")
else()
if(USE_OPENMP)
set(ATEN_THREADING "OMP" CACHE STRING "ATen parallel backend")
elseif(USE_TBB)
set(ATEN_THREADING "TBB" CACHE STRING "ATen parallel backend")
else()
set(ATEN_THREADING "NATIVE" CACHE STRING "ATen parallel backend")
endif()
endif()
set(AT_PARALLEL_OPENMP 0)
set(AT_PARALLEL_NATIVE 0)
set(AT_PARALLEL_NATIVE_TBB 0)
message(STATUS "Using ATen parallel backend: ${ATEN_THREADING}")
if("${ATEN_THREADING}" STREQUAL "OMP")
set(AT_PARALLEL_OPENMP 1)
elseif("${ATEN_THREADING}" STREQUAL "NATIVE")
set(AT_PARALLEL_NATIVE 1)
elseif("${ATEN_THREADING}" STREQUAL "TBB")
if(NOT USE_TBB)
message(FATAL_ERROR "Using TBB backend but USE_TBB is off")
endif()
set(AT_PARALLEL_NATIVE_TBB 1)
else()
message(FATAL_ERROR "Unknown ATen parallel backend: ${ATEN_THREADING}")
endif()
# ---[ Declare source file lists
# ---[ ATen build
if(INTERN_BUILD_ATEN_OPS)
set(__caffe2_CMAKE_POSITION_INDEPENDENT_CODE ${CMAKE_POSITION_INDEPENDENT_CODE})
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
add_subdirectory(../aten aten)
set(CMAKE_POSITION_INDEPENDENT_CODE ${__caffe2_CMAKE_POSITION_INDEPENDENT_CODE})
# Generate the headers wrapped by our operator
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/contrib/aten/aten_op.h
COMMAND
"${PYTHON_EXECUTABLE}" ${CMAKE_CURRENT_SOURCE_DIR}/contrib/aten/gen_op.py
--aten_root=${CMAKE_CURRENT_SOURCE_DIR}/../aten
--template_dir=${CMAKE_CURRENT_SOURCE_DIR}/contrib/aten
--yaml_dir=${CMAKE_CURRENT_BINARY_DIR}/../aten/src/ATen
--install_dir=${CMAKE_CURRENT_BINARY_DIR}/contrib/aten
DEPENDS
ATEN_CPU_FILES_GEN_TARGET
${CMAKE_BINARY_DIR}/aten/src/ATen/Declarations.yaml
${CMAKE_CURRENT_SOURCE_DIR}/contrib/aten/gen_op.py
${CMAKE_CURRENT_SOURCE_DIR}/contrib/aten/aten_op_template.h)
add_custom_target(__aten_op_header_gen
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/contrib/aten/aten_op.h)
add_library(aten_op_header_gen INTERFACE)
add_dependencies(aten_op_header_gen __aten_op_header_gen)
# Add source, includes, and libs to lists
list(APPEND Caffe2_CPU_SRCS ${ATen_CPU_SRCS})
list(APPEND Caffe2_GPU_SRCS ${ATen_CUDA_SRCS})
list(APPEND Caffe2_GPU_SRCS_W_SORT_BY_KEY ${ATen_CUDA_SRCS_W_SORT_BY_KEY})
list(APPEND Caffe2_HIP_SRCS ${ATen_HIP_SRCS})
list(APPEND Caffe2_HIP_SRCS ${ATen_HIP_SRCS_W_SORT_BY_KEY})
list(APPEND Caffe2_CPU_TEST_SRCS ${ATen_CPU_TEST_SRCS})
list(APPEND Caffe2_GPU_TEST_SRCS ${ATen_CUDA_TEST_SRCS})
list(APPEND Caffe2_HIP_TEST_SRCS ${ATen_HIP_TEST_SRCS})
list(APPEND Caffe2_CPU_TEST_SRCS ${ATen_CORE_TEST_SRCS})
list(APPEND Caffe2_VULKAN_TEST_SRCS ${ATen_VULKAN_TEST_SRCS})
list(APPEND Caffe2_CPU_INCLUDE ${ATen_CPU_INCLUDE})
list(APPEND Caffe2_GPU_INCLUDE ${ATen_CUDA_INCLUDE})
list(APPEND Caffe2_HIP_INCLUDE ${ATen_HIP_INCLUDE})
list(APPEND Caffe2_VULKAN_INCLUDE ${ATen_VULKAN_INCLUDE})
list(APPEND Caffe2_DEPENDENCY_LIBS ${ATen_CPU_DEPENDENCY_LIBS})
list(APPEND Caffe2_CUDA_DEPENDENCY_LIBS ${ATen_CUDA_DEPENDENCY_LIBS})
list(APPEND Caffe2_HIP_DEPENDENCY_LIBS ${ATen_HIP_DEPENDENCY_LIBS})
list(APPEND Caffe2_DEPENDENCY_INCLUDE ${ATen_THIRD_PARTY_INCLUDE})
endif()
# ---[ Caffe2 build
# Note: the folders that are being commented out have not been properly
# addressed yet.
if(NOT MSVC AND USE_XNNPACK)
if(NOT TARGET fxdiv)
set(FXDIV_BUILD_TESTS OFF CACHE BOOL "")
set(FXDIV_BUILD_BENCHMARKS OFF CACHE BOOL "")
add_subdirectory(
"${FXDIV_SOURCE_DIR}"
"${CMAKE_BINARY_DIR}/FXdiv")
endif()
endif()
add_subdirectory(core)
add_subdirectory(serialize)
add_subdirectory(utils)
if(BUILD_CAFFE2 OR (NOT USE_FBGEMM))
add_subdirectory(perfkernels)
endif()
# Skip modules that are not used by libtorch mobile yet.
if(BUILD_CAFFE2 AND (NOT INTERN_BUILD_MOBILE OR BUILD_CAFFE2_MOBILE))
add_subdirectory(contrib)
add_subdirectory(predictor)
add_subdirectory(predictor/emulator)
add_subdirectory(core/nomnigraph)
if(USE_NVRTC)
add_subdirectory(cuda_rtc)
endif()
add_subdirectory(db)
add_subdirectory(distributed)
# add_subdirectory(experiments) # note, we may remove this folder at some point
add_subdirectory(ideep)
add_subdirectory(image)
add_subdirectory(video)
add_subdirectory(mobile)
add_subdirectory(mpi)
add_subdirectory(observers)
add_subdirectory(onnx)
if(BUILD_CAFFE2_OPS)
add_subdirectory(operators)
add_subdirectory(operators/rnn)
if(USE_FBGEMM)
add_subdirectory(quantization)
add_subdirectory(quantization/server)
endif()
if(USE_QNNPACK)
add_subdirectory(operators/quantized)
endif()
endif()
add_subdirectory(opt)
add_subdirectory(proto)
add_subdirectory(python)
add_subdirectory(queue)
add_subdirectory(sgd)
add_subdirectory(share)
# add_subdirectory(test) # todo: use caffe2_gtest_main instead of gtest_main because we will need to call GlobalInit
add_subdirectory(transforms)
endif()
if(NOT BUILD_CAFFE2)
add_subdirectory(proto)
endif()
# Advanced: if we have allow list specified, we will do intersections for all
# main lib srcs.
if(CAFFE2_ALLOWLISTED_FILES)
caffe2_do_allowlist(Caffe2_CPU_SRCS CAFFE2_ALLOWLISTED_FILES)
caffe2_do_allowlist(Caffe2_GPU_SRCS CAFFE2_ALLOWLISTED_FILES)
caffe2_do_allowlist(Caffe2_HIP_SRCS CAFFE2_ALLOWLISTED_FILES)
endif()
# Debug messages - if you want to get a list of source files, enable the
# following.
if(FALSE)
message(STATUS "CPU sources: ")
foreach(tmp ${Caffe2_CPU_SRCS})
message(STATUS " " ${tmp})
endforeach()
message(STATUS "GPU sources: ")
foreach(tmp ${Caffe2_GPU_SRCS})
message(STATUS " " ${tmp})
endforeach()
message(STATUS "CPU include: ")
foreach(tmp ${Caffe2_CPU_INCLUDE})
message(STATUS " " ${tmp})
endforeach()
message(STATUS "GPU include: ")
foreach(tmp ${Caffe2_GPU_INCLUDE})
message(STATUS " " ${tmp})
endforeach()
message(STATUS "CPU test sources: ")
foreach(tmp ${Caffe2_CPU_TEST_SRCS})
message(STATUS " " ${tmp})
endforeach()
message(STATUS "GPU test sources: ")
foreach(tmp ${Caffe2_GPU_TEST_SRCS})
message(STATUS " " ${tmp})
endforeach()
message(STATUS "HIP sources: ")
foreach(tmp ${Caffe2_HIP_SRCS})
message(STATUS " " ${tmp})
endforeach()
message(STATUS "HIP test sources: ")
foreach(tmp ${Caffe2_HIP_TEST_SRCS})
message(STATUS " " ${tmp})
endforeach()
message(STATUS "ATen CPU test sources: ")
foreach(tmp ${ATen_CPU_TEST_SRCS})
message(STATUS " " ${tmp})
endforeach()
message(STATUS "ATen CUDA test sources: ")
foreach(tmp ${ATen_CUDA_TEST_SRCS})
message(STATUS " " ${tmp})
endforeach()
message(STATUS "ATen HIP test sources: ")
foreach(tmp ${ATen_HIP_TEST_SRCS})
message(STATUS " " ${tmp})
endforeach()
message(STATUS "ATen Vulkan test sources: ")
foreach(tmp ${ATen_VULKAN_TEST_SRCS})
message(STATUS " " ${tmp})
endforeach()
endif()
if(NOT INTERN_BUILD_MOBILE OR BUILD_CAFFE2_MOBILE)
# ---[ List of libraries to link with
add_library(caffe2_protos STATIC $<TARGET_OBJECTS:Caffe2_PROTO>)
add_dependencies(caffe2_protos Caffe2_PROTO)
# If we are going to link protobuf locally inside caffe2 libraries, what we will do is
# to create a helper static library that always contains libprotobuf source files, and
# link the caffe2 related dependent libraries to it.
target_include_directories(caffe2_protos INTERFACE $<INSTALL_INTERFACE:include>)
# Reason for this public dependency is as follows:
# (1) Strictly speaking, we should not expose any Protobuf related functions. We should
# only use function interfaces wrapped with our own public API, and link protobuf
# locally.
# (2) However, currently across the Caffe2 codebase, we have extensive use of protobuf
# functionalities. For example, not only libcaffe2.so uses it, but also other
# binaries such as python extensions etc. As a result, we will have to have a
# transitive dependency to libprotobuf.
#
# Good thing is that, if we specify CAFFE2_LINK_LOCAL_PROTOBUF, then we do not need to
# separately deploy protobuf binaries - libcaffe2.so will contain all functionalities
# one needs. One can verify this via ldd.
#
# TODO item in the future includes:
# (1) Enable using lite protobuf
# (2) Properly define public API that do not directly depend on protobuf itself.
# (3) Expose the libprotobuf.a file for dependent libraries to link to.
#
# What it means for users/developers?
# (1) Users: nothing affecting the users, other than the fact that CAFFE2_LINK_LOCAL_PROTOBUF
# avoids the need to deploy protobuf.
# (2) Developers: if one simply uses core caffe2 functionality without using protobuf,
# nothing changes. If one has a dependent library that uses protobuf, then one needs to
# have the right protobuf version as well as linking to libprotobuf.a.
target_link_libraries(caffe2_protos PUBLIC protobuf::libprotobuf)
if(NOT BUILD_SHARED_LIBS)
install(TARGETS caffe2_protos ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}")
endif()
endif()
# ==========================================================
# formerly-libtorch
# ==========================================================
set(TORCH_SRC_DIR "${PROJECT_SOURCE_DIR}/torch")
set(TORCH_ROOT "${PROJECT_SOURCE_DIR}")
if(NOT TORCH_INSTALL_BIN_DIR)
set(TORCH_INSTALL_BIN_DIR bin)
endif()
if(NOT TORCH_INSTALL_INCLUDE_DIR)
set(TORCH_INSTALL_INCLUDE_DIR include)
endif()
if(NOT TORCH_INSTALL_LIB_DIR)
set(TORCH_INSTALL_LIB_DIR lib)
endif()
if(NOT INTERN_BUILD_MOBILE OR NOT BUILD_CAFFE2_MOBILE)
if(USE_DISTRIBUTED)
# Define this target even if we're building without TensorPipe, to make life
# easier to other targets that depend on this. However, in that case, by not
# setting the USE_TENSORPIPE compile definition, this target will just end
# up being empty. Downstream targets should also add a #ifdef guard.
if(NOT WIN32)
add_library(process_group_agent "${TORCH_SRC_DIR}/csrc/distributed/rpc/process_group_agent.cpp" "${TORCH_SRC_DIR}/csrc/distributed/rpc/process_group_agent.h")
target_link_libraries(process_group_agent PRIVATE torch c10d fmt::fmt-header-only)
add_dependencies(process_group_agent torch c10d)
add_library(tensorpipe_agent
"${TORCH_SRC_DIR}/csrc/distributed/rpc/tensorpipe_agent.cpp"
"${TORCH_SRC_DIR}/csrc/distributed/rpc/tensorpipe_agent.h"
"${TORCH_SRC_DIR}/csrc/distributed/rpc/tensorpipe_utils.cpp"
"${TORCH_SRC_DIR}/csrc/distributed/rpc/tensorpipe_utils.h"
)
target_link_libraries(tensorpipe_agent PRIVATE torch c10d tensorpipe fmt::fmt-header-only)
add_dependencies(tensorpipe_agent torch c10d)
if(USE_TENSORPIPE)
if(USE_CUDA)
target_compile_definitions(tensorpipe_agent PUBLIC USE_CUDA)
endif()
if(USE_ROCM)
target_compile_definitions(tensorpipe_agent PRIVATE
USE_ROCM
__HIP_PLATFORM_HCC__
)
endif()
target_compile_definitions(tensorpipe_agent PUBLIC USE_TENSORPIPE)
target_link_libraries(tensorpipe_agent PRIVATE tensorpipe)
add_dependencies(tensorpipe_agent tensorpipe)
endif()
endif()
endif()
set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
# Generate files
set(TOOLS_PATH "${TORCH_ROOT}/tools")
configure_file("${TORCH_SRC_DIR}/_utils_internal.py"
"${TOOLS_PATH}/shared/_utils_internal.py"
COPYONLY)
set(GENERATED_CXX_TORCH
"${TORCH_SRC_DIR}/csrc/autograd/generated/Functions.cpp"
"${TORCH_SRC_DIR}/csrc/jit/generated/generated_unboxing_wrappers_0.cpp"
"${TORCH_SRC_DIR}/csrc/jit/generated/generated_unboxing_wrappers_1.cpp"
"${TORCH_SRC_DIR}/csrc/jit/generated/generated_unboxing_wrappers_2.cpp"
)
if(NOT INTERN_DISABLE_AUTOGRAD)
list(APPEND GENERATED_CXX_TORCH
"${TORCH_SRC_DIR}/csrc/autograd/generated/VariableType_0.cpp"
"${TORCH_SRC_DIR}/csrc/autograd/generated/VariableType_1.cpp"
"${TORCH_SRC_DIR}/csrc/autograd/generated/VariableType_2.cpp"
"${TORCH_SRC_DIR}/csrc/autograd/generated/VariableType_3.cpp"
"${TORCH_SRC_DIR}/csrc/autograd/generated/VariableType_4.cpp"
"${TORCH_SRC_DIR}/csrc/autograd/generated/TraceType_0.cpp"
"${TORCH_SRC_DIR}/csrc/autograd/generated/TraceType_1.cpp"
"${TORCH_SRC_DIR}/csrc/autograd/generated/TraceType_2.cpp"
"${TORCH_SRC_DIR}/csrc/autograd/generated/TraceType_3.cpp"
"${TORCH_SRC_DIR}/csrc/autograd/generated/TraceType_4.cpp"
)
endif()
set(GENERATED_H_TORCH
"${TORCH_SRC_DIR}/csrc/autograd/generated/Functions.h"
"${TORCH_SRC_DIR}/csrc/autograd/generated/variable_factories.h"
)
if(NOT INTERN_DISABLE_AUTOGRAD)
list(APPEND GENERATED_H_TORCH
"${TORCH_SRC_DIR}/csrc/autograd/generated/VariableType.h"
)
endif()
set(GENERATED_CXX_PYTHON
"${TORCH_SRC_DIR}/csrc/autograd/generated/python_functions.cpp"
"${TORCH_SRC_DIR}/csrc/autograd/generated/python_variable_methods.cpp"
"${TORCH_SRC_DIR}/csrc/autograd/generated/python_torch_functions.cpp"
"${TORCH_SRC_DIR}/csrc/autograd/generated/python_nn_functions.cpp"
"${TORCH_SRC_DIR}/csrc/autograd/generated/python_fft_functions.cpp"
"${TORCH_SRC_DIR}/csrc/autograd/generated/python_linalg_functions.cpp"
)
set(GENERATED_H_PYTHON
"${TORCH_SRC_DIR}/csrc/autograd/generated/python_functions.h"
)
set(GENERATED_TESTING_PYTHON
"${TORCH_SRC_DIR}/testing/_internal/generated/annotated_fn_args.py"
)
set(TORCH_GENERATED_CODE
${GENERATED_CXX_TORCH}
${GENERATED_H_TORCH}
${GENERATED_CXX_PYTHON}
${GENERATED_H_PYTHON}
${GENERATED_TESTING_PYTHON}
)
add_custom_command(
OUTPUT
${TORCH_GENERATED_CODE}
COMMAND
"${PYTHON_EXECUTABLE}" tools/setup_helpers/generate_code.py
--declarations-path "${CMAKE_BINARY_DIR}/aten/src/ATen/Declarations.yaml"
--native-functions-path "aten/src/ATen/native/native_functions.yaml"
--nn-path "aten/src"
$<$<BOOL:${INTERN_DISABLE_AUTOGRAD}>:--disable-autograd>
$<$<BOOL:${SELECTED_OP_LIST}>:--selected-op-list-path="${SELECTED_OP_LIST}">
--force_schema_registration
DEPENDS
"${TORCH_ROOT}/aten/src/ATen/native/native_functions.yaml"
"${CMAKE_BINARY_DIR}/aten/src/ATen/Declarations.yaml"
"${TOOLS_PATH}/autograd/templates/VariableType.h"
"${TOOLS_PATH}/autograd/templates/VariableType.cpp"
"${TOOLS_PATH}/autograd/templates/TraceType.cpp"
"${TOOLS_PATH}/autograd/templates/Functions.h"
"${TOOLS_PATH}/autograd/templates/Functions.cpp"
"${TOOLS_PATH}/autograd/templates/python_functions.h"
"${TOOLS_PATH}/autograd/templates/python_functions.cpp"
"${TOOLS_PATH}/autograd/templates/python_variable_methods.cpp"
"${TOOLS_PATH}/autograd/templates/python_torch_functions.cpp"
"${TOOLS_PATH}/autograd/templates/python_nn_functions.cpp"
"${TOOLS_PATH}/autograd/templates/python_fft_functions.cpp"
"${TOOLS_PATH}/autograd/templates/python_linalg_functions.cpp"
"${TOOLS_PATH}/autograd/templates/variable_factories.h"
"${TOOLS_PATH}/autograd/templates/annotated_fn_args.py"
"${TOOLS_PATH}/autograd/deprecated.yaml"
"${TOOLS_PATH}/autograd/derivatives.yaml"
"${TOOLS_PATH}/autograd/gen_autograd_functions.py"
"${TOOLS_PATH}/autograd/gen_autograd.py"
"${TOOLS_PATH}/autograd/gen_python_functions.py"
"${TOOLS_PATH}/autograd/gen_variable_factories.py"
"${TOOLS_PATH}/autograd/gen_variable_type.py"
"${TOOLS_PATH}/autograd/load_derivatives.py"
"${TOOLS_PATH}/autograd/nested_dict.py"
"${TOOLS_PATH}/autograd/utils.py"
"${TOOLS_PATH}/jit/gen_unboxing_wrappers.py"
"${TOOLS_PATH}/jit/templates/generated_unboxing_wrappers.cpp"
WORKING_DIRECTORY "${TORCH_ROOT}")
# Required workaround for libtorch_python.so build
# see https://samthursfield.wordpress.com/2015/11/21/cmake-dependencies-between-targets-and-files-and-custom-commands/#custom-commands-in-different-directories
add_custom_target(
generate-torch-sources
DEPENDS ${TORCH_GENERATED_CODE}
)
set(TORCH_SRCS ${GENERATED_CXX_TORCH})
list(APPEND TORCH_SRCS ${GENERATED_H_TORCH})
append_filelist("libtorch_cmake_sources" TORCH_SRCS)
# Required workaround for LLVM 9 includes.
if(NOT MSVC)
set_source_files_properties(${TORCH_SRC_DIR}/csrc/jit/tensorexpr/llvm_jit.cpp PROPERTIES COMPILE_FLAGS -Wno-noexcept-type)
endif()
# Disable certain warnings for GCC-9.X
if(CMAKE_COMPILER_IS_GNUCXX AND (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 9.0.0))
# See https://github.com/pytorch/pytorch/issues/38856
set_source_files_properties(${TORCH_SRC_DIR}/csrc/jit/tensorexpr/llvm_jit.cpp PROPERTIES COMPILE_FLAGS "-Wno-redundant-move -Wno-noexcept-type")
set_source_files_properties(${TORCH_SRC_DIR}/csrc/jit/tensorexpr/llvm_codegen.cpp PROPERTIES COMPILE_FLAGS -Wno-init-list-lifetime)
endif()
if(NOT INTERN_DISABLE_MOBILE_INTERP)
set(MOBILE_SRCS
${TORCH_SRC_DIR}/csrc/jit/mobile/function.cpp
${TORCH_SRC_DIR}/csrc/jit/mobile/import.cpp
${TORCH_SRC_DIR}/csrc/jit/mobile/import_data.cpp
${TORCH_SRC_DIR}/csrc/jit/mobile/module.cpp
${TORCH_SRC_DIR}/csrc/jit/mobile/observer.cpp
${TORCH_SRC_DIR}/csrc/jit/mobile/interpreter.cpp
${TORCH_SRC_DIR}/csrc/jit/mobile/export_data.cpp
${TORCH_SRC_DIR}/csrc/jit/mobile/optim/sgd.cpp
${TORCH_SRC_DIR}/csrc/jit/mobile/sequential.cpp
)
list(APPEND TORCH_SRCS ${MOBILE_SRCS})
endif()
# This one needs to be unconditionally added as Functions.cpp is also unconditionally added
list(APPEND TORCH_SRCS
${TORCH_SRC_DIR}/csrc/autograd/FunctionsManual.cpp
)
if(NOT INTERN_DISABLE_AUTOGRAD)
list(APPEND TORCH_SRCS
${TORCH_SRC_DIR}/csrc/autograd/TraceTypeManual.cpp
${TORCH_SRC_DIR}/csrc/autograd/VariableTypeManual.cpp
)
endif()
if(NOT INTERN_BUILD_MOBILE)
list(APPEND TORCH_SRCS
${TORCH_SRC_DIR}/csrc/api/src/jit.cpp
${TORCH_SRC_DIR}/csrc/jit/serialization/onnx.cpp
${TORCH_SRC_DIR}/csrc/jit/serialization/export.cpp
${TORCH_SRC_DIR}/csrc/jit/serialization/export_module.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/fuser/cpu/fused_kernel.cpp
${TORCH_SRC_DIR}/csrc/jit/api/module_save.cpp
${TORCH_SRC_DIR}/csrc/utils/byte_order.cpp
)
# Disable legacy import of building without Caffe2 support
if(BUILD_CAFFE2)
list(APPEND TORCH_SRCS
${TORCH_SRC_DIR}/csrc/jit/serialization/import_legacy.cpp
)
else()
set_source_files_properties(
${TORCH_SRC_DIR}/csrc/jit/serialization/import.cpp
PROPERTIES COMPILE_FLAGS "-DC10_DISABLE_LEGACY_IMPORT"
)
endif()
if(USE_DISTRIBUTED AND NOT WIN32)
append_filelist("libtorch_distributed_sources" TORCH_SRCS)
endif()
endif()
if(USE_CUDA OR USE_ROCM)
list(APPEND Caffe2_GPU_HIP_JIT_FUSERS_SRCS
${TORCH_SRC_DIR}/csrc/jit/codegen/fuser/cuda/fused_kernel.cpp
${TORCH_SRC_DIR}/csrc/autograd/profiler_cuda.cpp
${TORCH_SRC_DIR}/csrc/autograd/functions/comm.cpp
${TORCH_SRC_DIR}/csrc/cuda/comm.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/arith.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/compute_at.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/codegen.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/dispatch.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/expr_evaluator.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/executor.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/executor_kernel_arg.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/executor_launch_params.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/executor_utils.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/fusion.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/graph_fuser.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/index_compute.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/instrumentation.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/ir_base_nodes.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/ir_cloner.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/ir_graphviz.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/ir_nodes.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/ir_iostream.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/iter_visitor.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/kernel.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/kernel_cache.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/kernel_ir.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/kernel_ir_builder.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/kernel_ir_printer.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/lower_index.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/lower_alias_memory.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/lower_insert_syncs.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/lower_loops.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/lower_thread_predicate.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/lower_unroll.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/lower_utils.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/lower_validation.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/lower2device.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/manager.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/mutator.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/parser.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/partition.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/predicate_compute.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/register_interface.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/scheduler.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/shape_inference.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/tensor_view.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/transform_iter.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/transform_replay.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/transform_rfactor.cpp
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/type.cpp
${TORCH_SRC_DIR}/csrc/jit/tensorexpr/cuda_codegen.cpp
)
endif()
if(USE_CUDA)
list(APPEND Caffe2_GPU_SRCS ${Caffe2_GPU_HIP_JIT_FUSERS_SRCS})
add_library(caffe2_nvrtc SHARED ${ATen_NVRTC_STUB_SRCS})
if(MSVC)
# Delay load nvcuda.dll so we can import torch compiled with cuda on a CPU-only machine
set(DELAY_LOAD_FLAGS "-DELAYLOAD:nvcuda.dll;delayimp.lib")
else()
set(DELAY_LOAD_FLAGS "")
endif()
target_link_libraries(caffe2_nvrtc ${CUDA_NVRTC} ${CUDA_CUDA_LIB} ${CUDA_NVRTC_LIB} ${DELAY_LOAD_FLAGS})
target_include_directories(caffe2_nvrtc PRIVATE ${CUDA_INCLUDE_DIRS})
install(TARGETS caffe2_nvrtc DESTINATION "${TORCH_INSTALL_LIB_DIR}")
if(USE_NCCL)
list(APPEND Caffe2_GPU_SRCS
${TORCH_SRC_DIR}/csrc/cuda/nccl.cpp)
endif()
endif()
if(USE_ROCM)
list(APPEND Caffe2_HIP_SRCS ${Caffe2_GPU_HIP_JIT_FUSERS_SRCS})
if(USE_NCCL)
list(APPEND Caffe2_HIP_SRCS
${TORCH_SRC_DIR}/csrc/cuda/nccl.cpp)
endif()
# caffe2_nvrtc's stubs to driver APIs are useful for HIP.
# See NOTE [ ATen NVRTC Stub and HIP ]
add_library(caffe2_nvrtc SHARED ${ATen_NVRTC_STUB_SRCS})
target_link_libraries(caffe2_nvrtc ${PYTORCH_HIP_HCC_LIBRARIES} ${ROCM_HIPRTC_LIB})
target_compile_definitions(caffe2_nvrtc PRIVATE USE_ROCM __HIP_PLATFORM_HCC__)
install(TARGETS caffe2_nvrtc DESTINATION "${TORCH_INSTALL_LIB_DIR}")
endif()
if(NOT NO_API)
list(APPEND TORCH_SRCS
${TORCH_SRC_DIR}/csrc/api/src/cuda.cpp
${TORCH_SRC_DIR}/csrc/api/src/data/datasets/mnist.cpp
${TORCH_SRC_DIR}/csrc/api/src/data/samplers/distributed.cpp
${TORCH_SRC_DIR}/csrc/api/src/data/samplers/random.cpp
${TORCH_SRC_DIR}/csrc/api/src/data/samplers/sequential.cpp
${TORCH_SRC_DIR}/csrc/api/src/data/samplers/stream.cpp
${TORCH_SRC_DIR}/csrc/api/src/enum.cpp
${TORCH_SRC_DIR}/csrc/api/src/serialize.cpp
${TORCH_SRC_DIR}/csrc/api/src/jit.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/init.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/module.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/modules/_functions.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/modules/activation.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/modules/adaptive.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/modules/batchnorm.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/modules/normalization.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/modules/instancenorm.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/modules/conv.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/modules/dropout.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/modules/distance.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/modules/embedding.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/modules/fold.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/modules/linear.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/modules/loss.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/modules/padding.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/modules/pixelshuffle.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/modules/pooling.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/modules/rnn.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/modules/upsampling.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/modules/transformer.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/modules/container/functional.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/options/activation.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/options/adaptive.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/options/batchnorm.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/options/embedding.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/options/instancenorm.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/options/normalization.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/options/conv.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/options/dropout.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/options/linear.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/options/padding.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/options/pooling.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/options/rnn.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/options/vision.cpp
${TORCH_SRC_DIR}/csrc/api/src/nn/options/transformer.cpp
${TORCH_SRC_DIR}/csrc/api/src/optim/adagrad.cpp
${TORCH_SRC_DIR}/csrc/api/src/optim/adam.cpp
${TORCH_SRC_DIR}/csrc/api/src/optim/adamw.cpp
${TORCH_SRC_DIR}/csrc/api/src/optim/lbfgs.cpp
${TORCH_SRC_DIR}/csrc/api/src/optim/optimizer.cpp
${TORCH_SRC_DIR}/csrc/api/src/optim/rmsprop.cpp
${TORCH_SRC_DIR}/csrc/api/src/optim/serialize.cpp
${TORCH_SRC_DIR}/csrc/api/src/optim/sgd.cpp
${TORCH_SRC_DIR}/csrc/api/src/serialize/input-archive.cpp
${TORCH_SRC_DIR}/csrc/api/src/serialize/output-archive.cpp
)
endif()
list(APPEND Caffe2_CPU_SRCS ${TORCH_SRCS})
endif()
# NOTE [ Linking AVX and non-AVX files ]
#
# Regardless of the CPU capabilities, we build some files with AVX and AVX2
# instruction set. If the host CPU doesn't support those, we simply ignore their
# functions at runtime during dispatch.
#
# We must make sure that those files are at the end of the input list when
# linking the torch_cpu library. Otherwise, the following error scenario might
# occur:
# 1. A non-AVX and an AVX file both call a function defined with the `inline`
# keyword
# 2. The compiler decides not to inline this function
# 3. Two different versions of the machine code are generated for this function:
# one without AVX instructions and one with AVX.
# 4. When linking, the AVX version is found earlier in the input object files,
# so the linker makes the entire library use it, even in code not guarded by
# the dispatcher.
# 5. A CPU without AVX support executes this function, encounters an AVX
# instruction and crashes.
#
# Thus we organize the input files in the following order:
# 1. All files with no AVX support
# 2. All files with AVX support (conveniently, they all have names ending with
# 'AVX.cpp')
# 3. All files with AVX2 support ('*AVX2.cpp')
set(Caffe2_CPU_SRCS_NON_AVX)
set(Caffe2_CPU_SRCS_AVX)
set(Caffe2_CPU_SRCS_AVX2)
foreach(input_filename ${Caffe2_CPU_SRCS})
if(${input_filename} MATCHES "AVX\\.cpp")
list(APPEND Caffe2_CPU_SRCS_AVX ${input_filename})
elseif(${input_filename} MATCHES "AVX2\\.cpp")
list(APPEND Caffe2_CPU_SRCS_AVX2 ${input_filename})
else()
list(APPEND Caffe2_CPU_SRCS_NON_AVX ${input_filename})
endif()
endforeach(input_filename)
set(Caffe2_CPU_SRCS ${Caffe2_CPU_SRCS_NON_AVX} ${Caffe2_CPU_SRCS_AVX} ${Caffe2_CPU_SRCS_AVX2})
# ==========================================================
# END formerly-libtorch sources
# ==========================================================
add_library(torch_cpu ${Caffe2_CPU_SRCS})
if(HAVE_SOVERSION)
set_target_properties(torch_cpu PROPERTIES
VERSION ${TORCH_VERSION} SOVERSION ${TORCH_SOVERSION})
endif()
torch_compile_options(torch_cpu) # see cmake/public/utils.cmake
if(USE_LLVM AND LLVM_FOUND)
llvm_map_components_to_libnames(LLVM_LINK_LIBS
support core analysis executionengine instcombine
scalaropts transformutils native orcjit)
target_link_libraries(torch_cpu PRIVATE ${LLVM_LINK_LIBS})
endif(USE_LLVM AND LLVM_FOUND)
# This is required for older versions of CMake, which don't allow
# specifying add_library() without a list of source files
set(DUMMY_EMPTY_FILE ${CMAKE_BINARY_DIR}/empty.cpp)
if(MSVC)
set(DUMMY_FILE_CONTENT "__declspec(dllexport) int ignore_this_library_placeholder(){return 0\\;}")
else()
set(DUMMY_FILE_CONTENT "")
endif()
file(WRITE ${DUMMY_EMPTY_FILE} ${DUMMY_FILE_CONTENT})
# Wrapper library for people who link against torch and expect both CPU and CUDA support
# Contains "torch_cpu" and "torch_cuda"
add_library(torch ${DUMMY_EMPTY_FILE})
if(HAVE_SOVERSION)
set_target_properties(torch PROPERTIES
VERSION ${TORCH_VERSION} SOVERSION ${TORCH_SOVERSION})
endif()
if(USE_ROCM)
filter_list(__caffe2_hip_srcs_cpp Caffe2_HIP_SRCS "\\.(cu|hip)$")
set_source_files_properties(${__caffe2_hip_srcs_cpp} PROPERTIES HIP_SOURCE_PROPERTY_FORMAT 1)
endif()
# Compile exposed libraries.
if(USE_ROCM)
set(CUDA_LINK_LIBRARIES_KEYWORD PRIVATE)
hip_add_library(torch_hip ${Caffe2_HIP_SRCS})
set(CUDA_LINK_LIBRARIES_KEYWORD)
torch_compile_options(torch_hip) # see cmake/public/utils.cmake
# TODO: Not totally sure if this is live or not
if(USE_NCCL)
target_link_libraries(torch_hip PRIVATE __caffe2_nccl)
target_compile_definitions(torch_hip PRIVATE USE_NCCL)
endif()
elseif(USE_CUDA)
set(CUDA_LINK_LIBRARIES_KEYWORD PRIVATE)
if(CUDA_SEPARABLE_COMPILATION)
# Separate compilation fails when kernels using `thrust::sort_by_key`
# are linked with the rest of CUDA code. Workaround by linking the separateley
set(_generated_name "torch_cuda_w_sort_by_key_intermediate_link${CMAKE_C_OUTPUT_EXTENSION}")
set(torch_cuda_w_sort_by_key_link_file "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/torch_cuda.dir/${CMAKE_CFG_INTDIR}/${_generated_name}")
cuda_wrap_srcs(torch_cuda OBJ Caffe2_GPU_W_SORT_BY_KEY_OBJ ${Caffe2_GPU_SRCS_W_SORT_BY_KEY})
CUDA_LINK_SEPARABLE_COMPILATION_OBJECTS("${torch_cuda_w_sort_by_key_link_file}" torch_cpu "${_options}" "${torch_cuda_SEPARABLE_COMPILATION_OBJECTS}")
set( torch_cuda_SEPARABLE_COMPILATION_OBJECTS )
# Pass compiled sort-by-key object + device-linked fatbin as extra dependencies of torch_cuda
cuda_add_library(torch_cuda ${Caffe2_GPU_SRCS} ${torch_cuda_w_sort_by_key_link_file} ${Caffe2_GPU_W_SORT_BY_KEY_OBJ})
else()
cuda_add_library(torch_cuda ${Caffe2_GPU_SRCS} ${Caffe2_GPU_SRCS_W_SORT_BY_KEY})
endif()
set(CUDA_LINK_LIBRARIES_KEYWORD)
torch_compile_options(torch_cuda) # see cmake/public/utils.cmake
if(USE_NCCL)
target_link_libraries(torch_cuda PRIVATE __caffe2_nccl)
target_compile_definitions(torch_cuda PRIVATE USE_NCCL)
endif()
endif()
if(USE_CUDA OR USE_ROCM)
if(USE_CUDA)
set(TORCHLIB_FLAVOR torch_cuda)
elseif(USE_ROCM)
set(TORCHLIB_FLAVOR torch_hip)
endif()
# The list of NVFUSER runtime files
list(APPEND NVFUSER_RUNTIME_FILES
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/runtime/block_reduction.cu
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/runtime/broadcast.cu
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/runtime/fp16_support.cu
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/runtime/grid_reduction.cu
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/runtime/helpers.cu
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/runtime/random_numbers.cu
${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/runtime/tensor.cu
)
file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/include/nvfuser_resources")
# "stringify" NVFUSER runtime sources
# (generate C++ header files embedding the original input as a string literal)
set(NVFUSER_STRINGIFY_TOOL "${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/tools/stringify_file.py")
foreach(src ${NVFUSER_RUNTIME_FILES})
get_filename_component(filename ${src} NAME_WE)
set(dst "${CMAKE_BINARY_DIR}/include/nvfuser_resources/${filename}.h")
add_custom_command(
COMMENT "Stringify NVFUSER runtime source file"
OUTPUT ${dst}
DEPENDS ${src}
COMMAND ${PYTHON_EXECUTABLE} ${NVFUSER_STRINGIFY_TOOL} -i ${src} -o ${dst}
)
add_custom_target(nvfuser_rt_${filename} DEPENDS ${dst})
add_dependencies(${TORCHLIB_FLAVOR} nvfuser_rt_${filename})
# also generate the resource headers during the configuration step
# (so tools like clang-tidy can run w/o requiring a real build)
execute_process(COMMAND
${PYTHON_EXECUTABLE} ${NVFUSER_STRINGIFY_TOOL} -i ${src} -o ${dst})
endforeach()
target_include_directories(${TORCHLIB_FLAVOR} PRIVATE "${CMAKE_BINARY_DIR}/include")
endif()
if(NOT MSVC AND USE_XNNPACK)
TARGET_LINK_LIBRARIES(torch_cpu PRIVATE fxdiv)
endif()
# ==========================================================
# formerly-libtorch flags
# ==========================================================
if(NOT INTERN_BUILD_MOBILE)
# Forces caffe2.pb.h to be generated before its dependents are compiled.
# Adding the generated header file to the ${TORCH_SRCS} list is not sufficient
# to establish the dependency, since the generation procedure is declared in a different CMake file.
# See https://samthursfield.wordpress.com/2015/11/21/cmake-dependencies-between-targets-and-files-and-custom-commands/#custom-commands-in-different-directories
add_dependencies(torch_cpu Caffe2_PROTO)
endif()
if(NOT INTERN_BUILD_MOBILE OR NOT BUILD_CAFFE2_MOBILE)
if(NOT NO_API)
target_include_directories(torch_cpu PRIVATE
${TORCH_SRC_DIR}/csrc/api
${TORCH_SRC_DIR}/csrc/api/include)
endif()
if(USE_CUDA AND MSVC)
# -INCLUDE is used to ensure torch_cuda is linked against in a project that relies on it.
# Related issue: https://github.com/pytorch/pytorch/issues/31611
target_link_libraries(torch_cuda INTERFACE "-INCLUDE:?warp_size@cuda@at@@YAHXZ")
endif()
set(TH_CPU_INCLUDE
# dense
aten/src/TH
${CMAKE_CURRENT_BINARY_DIR}/aten/src/TH
${TORCH_ROOT}/aten/src
${CMAKE_CURRENT_BINARY_DIR}/aten/src
${CMAKE_BINARY_DIR}/aten/src)
target_include_directories(torch_cpu PRIVATE ${TH_CPU_INCLUDE})
set(ATen_CPU_INCLUDE
${TORCH_ROOT}/aten/src
${CMAKE_CURRENT_BINARY_DIR}/../aten/src
${CMAKE_CURRENT_BINARY_DIR}/../aten/src/ATen
${CMAKE_BINARY_DIR}/aten/src)
if(USE_TBB)
list(APPEND ATen_CPU_INCLUDE ${TBB_ROOT_DIR}/include)
target_link_libraries(torch_cpu PUBLIC tbb)
endif()
target_include_directories(torch_cpu PRIVATE ${ATen_CPU_INCLUDE})
target_include_directories(torch_cpu PRIVATE
${TORCH_SRC_DIR}/csrc)
target_include_directories(torch_cpu PRIVATE
${TORCH_ROOT}/third_party/miniz-2.0.8)
if(USE_KINETO)
target_include_directories(torch_cpu PRIVATE
${TORCH_ROOT}/third_party/kineto/libkineto/include
${TORCH_ROOT}/third_party/kineto/libkineto/src)
endif()
install(DIRECTORY "${TORCH_SRC_DIR}/csrc"
DESTINATION ${TORCH_INSTALL_INCLUDE_DIR}/torch
FILES_MATCHING PATTERN "*.h")
install(FILES
"${TORCH_SRC_DIR}/script.h"
"${TORCH_SRC_DIR}/extension.h"
"${TORCH_SRC_DIR}/custom_class.h"
"${TORCH_SRC_DIR}/library.h"
"${TORCH_SRC_DIR}/custom_class_detail.h"
DESTINATION ${TORCH_INSTALL_INCLUDE_DIR}/torch)
if(BUILD_TEST)
add_subdirectory(${TORCH_ROOT}/test/cpp/jit ${CMAKE_BINARY_DIR}/test_jit)
add_subdirectory(${TORCH_ROOT}/test/cpp/tensorexpr ${CMAKE_BINARY_DIR}/test_tensorexpr)
if(USE_DISTRIBUTED AND NOT WIN32)
add_subdirectory(${TORCH_ROOT}/test/cpp/rpc ${CMAKE_BINARY_DIR}/test_cpp_rpc)
endif()
endif()
if(BUILD_TEST AND NOT NO_API)
add_subdirectory(${TORCH_ROOT}/test/cpp/api ${CMAKE_BINARY_DIR}/test_api)
add_subdirectory(${TORCH_ROOT}/test/cpp/dist_autograd ${CMAKE_BINARY_DIR}/dist_autograd)
endif()
# XXX This ABI check cannot be run with arm-linux-androideabi-g++
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
if(DEFINED GLIBCXX_USE_CXX11_ABI)
message(STATUS "_GLIBCXX_USE_CXX11_ABI is already defined as a cmake variable")
else()
message(STATUS "${CMAKE_CXX_COMPILER} ${TORCH_SRC_DIR}/abi-check.cpp -o ${CMAKE_BINARY_DIR}/abi-check")
execute_process(
COMMAND
"${CMAKE_CXX_COMPILER}"
"${TORCH_SRC_DIR}/abi-check.cpp"
"-o"
"${CMAKE_BINARY_DIR}/abi-check"
RESULT_VARIABLE ABI_CHECK_COMPILE_RESULT)
if(ABI_CHECK_COMPILE_RESULT)
message(FATAL_ERROR "Could not compile ABI Check: ${ABI_CHECK_COMPILE_RESULT}")
endif()
execute_process(
COMMAND "${CMAKE_BINARY_DIR}/abi-check"
RESULT_VARIABLE ABI_CHECK_RESULT
OUTPUT_VARIABLE GLIBCXX_USE_CXX11_ABI)
if(ABI_CHECK_RESULT)
message(WARNING "Could not run ABI Check: ${ABI_CHECK_RESULT}")
endif()
endif()
message(STATUS "Determined _GLIBCXX_USE_CXX11_ABI=${GLIBCXX_USE_CXX11_ABI}")
endif()
# CMake config for external projects.
configure_file(
${PROJECT_SOURCE_DIR}/cmake/TorchConfigVersion.cmake.in
${PROJECT_BINARY_DIR}/TorchConfigVersion.cmake
@ONLY)
configure_file(
${TORCH_ROOT}/cmake/TorchConfig.cmake.in
${PROJECT_BINARY_DIR}/TorchConfig.cmake
@ONLY)
install(FILES
${PROJECT_BINARY_DIR}/TorchConfigVersion.cmake
${PROJECT_BINARY_DIR}/TorchConfig.cmake
DESTINATION share/cmake/Torch)
if(USE_DISTRIBUTED)
add_subdirectory(${TORCH_SRC_DIR}/lib/c10d lib_c10d)
endif()
# ---[ Torch python bindings build
add_subdirectory(../torch torch)
endif()
# ==========================================================
# END formerly-libtorch flags
# ==========================================================
if(NOT NO_API)
target_include_directories(torch_cpu PUBLIC
$<BUILD_INTERFACE:${TORCH_SRC_DIR}/csrc/api>
$<BUILD_INTERFACE:${TORCH_SRC_DIR}/csrc/api/include>)
endif()
if(USE_OPENMP)
find_package(OpenMP QUIET)
endif()
if(USE_OPENMP AND OPENMP_FOUND)
if(MSVC AND OpenMP_CXX_LIBRARIES MATCHES "libiomp5md\\.lib")
set(AT_MKL_MT 1)
else()
set(AT_MKL_MT 0)
endif()
message(STATUS "pytorch is compiling with OpenMP. \n"
"OpenMP CXX_FLAGS: ${OpenMP_CXX_FLAGS}. \n"
"OpenMP libraries: ${OpenMP_CXX_LIBRARIES}.")
target_compile_options(torch_cpu PRIVATE ${OpenMP_CXX_FLAGS})
target_link_libraries(torch_cpu PRIVATE ${OpenMP_CXX_LIBRARIES})
endif()