onnxruntime
307 строк · 15.9 Кб
1# Copyright (c) Microsoft Corporation. All rights reserved.
2# Licensed under the MIT License.
3
4
5if (onnxruntime_CUDA_MINIMAL)
6file(GLOB onnxruntime_providers_cuda_cc_srcs CONFIGURE_DEPENDS
7"${ONNXRUNTIME_ROOT}/core/providers/cuda/*.h"
8"${ONNXRUNTIME_ROOT}/core/providers/cuda/*.cc"
9"${ONNXRUNTIME_ROOT}/core/providers/cuda/tunable/*.h"
10"${ONNXRUNTIME_ROOT}/core/providers/cuda/tunable/*.cc"
11)
12# Remove pch files
13list(REMOVE_ITEM onnxruntime_providers_cuda_cc_srcs
14"${ONNXRUNTIME_ROOT}/core/providers/cuda/integer_gemm.cc"
15"${ONNXRUNTIME_ROOT}/core/providers/cuda/triton_kernel.h"
16)
17else()
18file(GLOB_RECURSE onnxruntime_providers_cuda_cc_srcs CONFIGURE_DEPENDS
19"${ONNXRUNTIME_ROOT}/core/providers/cuda/*.h"
20"${ONNXRUNTIME_ROOT}/core/providers/cuda/*.cc"
21)
22endif()
23# Remove pch files
24list(REMOVE_ITEM onnxruntime_providers_cuda_cc_srcs
25"${ONNXRUNTIME_ROOT}/core/providers/cuda/cuda_pch.h"
26"${ONNXRUNTIME_ROOT}/core/providers/cuda/cuda_pch.cc"
27)
28
29# The shared_library files are in a separate list since they use precompiled headers, and the above files have them disabled.
30file(GLOB_RECURSE onnxruntime_providers_cuda_shared_srcs CONFIGURE_DEPENDS
31"${ONNXRUNTIME_ROOT}/core/providers/shared_library/*.h"
32"${ONNXRUNTIME_ROOT}/core/providers/shared_library/*.cc"
33)
34
35
36if (NOT onnxruntime_CUDA_MINIMAL)
37file(GLOB_RECURSE onnxruntime_providers_cuda_cu_srcs CONFIGURE_DEPENDS
38"${ONNXRUNTIME_ROOT}/core/providers/cuda/*.cu"
39"${ONNXRUNTIME_ROOT}/core/providers/cuda/*.cuh"
40)
41else()
42set(onnxruntime_providers_cuda_cu_srcs
43"${ONNXRUNTIME_ROOT}/core/providers/cuda/math/unary_elementwise_ops_impl.cu"
44)
45endif()
46source_group(TREE ${ONNXRUNTIME_ROOT}/core FILES ${onnxruntime_providers_cuda_cc_srcs} ${onnxruntime_providers_cuda_shared_srcs} ${onnxruntime_providers_cuda_cu_srcs})
47set(onnxruntime_providers_cuda_src ${onnxruntime_providers_cuda_cc_srcs} ${onnxruntime_providers_cuda_shared_srcs} ${onnxruntime_providers_cuda_cu_srcs})
48
49# disable contrib ops conditionally
50if(NOT onnxruntime_DISABLE_CONTRIB_OPS AND NOT onnxruntime_CUDA_MINIMAL)
51if (NOT onnxruntime_ENABLE_ATEN)
52list(REMOVE_ITEM onnxruntime_cuda_contrib_ops_cc_srcs
53"${ONNXRUNTIME_ROOT}/contrib_ops/cuda/aten_ops/aten_op.cc"
54)
55endif()
56if (NOT onnxruntime_USE_NCCL)
57list(REMOVE_ITEM onnxruntime_cuda_contrib_ops_cc_srcs
58"${ONNXRUNTIME_ROOT}/contrib_ops/cuda/collective/nccl_kernels.cc"
59"${ONNXRUNTIME_ROOT}/contrib_ops/cuda/collective/sharded_moe.h"
60"${ONNXRUNTIME_ROOT}/contrib_ops/cuda/collective/sharded_moe.cc"
61"${ONNXRUNTIME_ROOT}/contrib_ops/cuda/collective/sharding_spec.cc"
62"${ONNXRUNTIME_ROOT}/contrib_ops/cuda/collective/sharding.cc"
63"${ONNXRUNTIME_ROOT}/contrib_ops/cuda/collective/distributed_matmul.cc"
64"${ONNXRUNTIME_ROOT}/contrib_ops/cuda/collective/distributed_slice.cc"
65"${ONNXRUNTIME_ROOT}/contrib_ops/cuda/collective/distributed_reshape.cc"
66"${ONNXRUNTIME_ROOT}/contrib_ops/cuda/collective/distributed_expand.cc"
67"${ONNXRUNTIME_ROOT}/contrib_ops/cuda/collective/distributed_reduce.cc"
68"${ONNXRUNTIME_ROOT}/contrib_ops/cuda/collective/distributed_unsqueeze.cc"
69"${ONNXRUNTIME_ROOT}/contrib_ops/cuda/collective/distributed_squeeze.cc"
70)
71endif()
72# add using ONNXRUNTIME_ROOT so they show up under the 'contrib_ops' folder in Visual Studio
73source_group(TREE ${ONNXRUNTIME_ROOT} FILES ${onnxruntime_cuda_contrib_ops_cc_srcs} ${onnxruntime_cuda_contrib_ops_cu_srcs})
74list(APPEND onnxruntime_providers_cuda_src ${onnxruntime_cuda_contrib_ops_cc_srcs} ${onnxruntime_cuda_contrib_ops_cu_srcs})
75endif()
76
77if (onnxruntime_ENABLE_TRAINING_OPS)
78file(GLOB_RECURSE onnxruntime_cuda_training_ops_cc_srcs CONFIGURE_DEPENDS
79"${ORTTRAINING_SOURCE_DIR}/training_ops/cuda/*.h"
80"${ORTTRAINING_SOURCE_DIR}/training_ops/cuda/*.cc"
81)
82
83file(GLOB_RECURSE onnxruntime_cuda_training_ops_cu_srcs CONFIGURE_DEPENDS
84"${ORTTRAINING_SOURCE_DIR}/training_ops/cuda/*.cu"
85"${ORTTRAINING_SOURCE_DIR}/training_ops/cuda/*.cuh"
86)
87
88source_group(TREE ${ORTTRAINING_ROOT} FILES ${onnxruntime_cuda_training_ops_cc_srcs} ${onnxruntime_cuda_training_ops_cu_srcs})
89list(APPEND onnxruntime_providers_cuda_src ${onnxruntime_cuda_training_ops_cc_srcs} ${onnxruntime_cuda_training_ops_cu_srcs})
90
91if(NOT onnxruntime_ENABLE_TRAINING)
92file(GLOB_RECURSE onnxruntime_cuda_full_training_only_srcs
93"${ORTTRAINING_SOURCE_DIR}/training_ops/cuda/collective/*.cc"
94"${ORTTRAINING_SOURCE_DIR}/training_ops/cuda/collective/*.h"
95"${ORTTRAINING_SOURCE_DIR}/training_ops/cuda/communication/*.cc"
96"${ORTTRAINING_SOURCE_DIR}/training_ops/cuda/communication/*.h"
97"${ORTTRAINING_SOURCE_DIR}/training_ops/cuda/controlflow/record.cc"
98"${ORTTRAINING_SOURCE_DIR}/training_ops/cuda/controlflow/record.h"
99"${ORTTRAINING_SOURCE_DIR}/training_ops/cuda/controlflow/wait.cc"
100"${ORTTRAINING_SOURCE_DIR}/training_ops/cuda/controlflow/wait.h"
101"${ORTTRAINING_SOURCE_DIR}/training_ops/cuda/controlflow/yield.cc"
102"${ORTTRAINING_SOURCE_DIR}/training_ops/cuda/gist/*.cc"
103"${ORTTRAINING_SOURCE_DIR}/training_ops/cuda/gist/*.h"
104"${ORTTRAINING_SOURCE_DIR}/training_ops/cuda/gist/*.cu"
105"${ORTTRAINING_SOURCE_DIR}/training_ops/cuda/torch/*.cc"
106"${ORTTRAINING_SOURCE_DIR}/training_ops/cuda/torch/*.h"
107"${ORTTRAINING_SOURCE_DIR}/training_ops/cuda/triton/triton_op.cc"
108)
109
110list(REMOVE_ITEM onnxruntime_providers_cuda_src ${onnxruntime_cuda_full_training_only_srcs})
111elseif(WIN32 OR NOT onnxruntime_USE_NCCL)
112# NCCL is not support in Windows build
113file(GLOB_RECURSE onnxruntime_cuda_nccl_op_srcs
114"${ORTTRAINING_SOURCE_DIR}/training_ops/cuda/collective/nccl_common.cc"
115"${ORTTRAINING_SOURCE_DIR}/training_ops/cuda/collective/nccl_kernels.cc"
116"${ORTTRAINING_SOURCE_DIR}/training_ops/cuda/collective/megatron.cc"
117)
118list(REMOVE_ITEM onnxruntime_providers_cuda_src ${onnxruntime_cuda_nccl_op_srcs})
119endif()
120endif()
121
122if (onnxruntime_REDUCED_OPS_BUILD)
123substitute_op_reduction_srcs(onnxruntime_providers_cuda_src)
124endif()
125if(onnxruntime_ENABLE_CUDA_EP_INTERNAL_TESTS)
126# cuda_provider_interface.cc is removed from the object target: onnxruntime_providers_cuda_obj and
127# added to the lib onnxruntime_providers_cuda separately.
128# onnxruntime_providers_cuda_ut can share all the object files with onnxruntime_providers_cuda except cuda_provider_interface.cc.
129set(cuda_provider_interface_src ${ONNXRUNTIME_ROOT}/core/providers/cuda/cuda_provider_interface.cc)
130list(REMOVE_ITEM onnxruntime_providers_cuda_src ${cuda_provider_interface_src})
131onnxruntime_add_object_library(onnxruntime_providers_cuda_obj ${onnxruntime_providers_cuda_src})
132onnxruntime_add_shared_library_module(onnxruntime_providers_cuda ${cuda_provider_interface_src} $<TARGET_OBJECTS:onnxruntime_providers_cuda_obj>)
133else()
134onnxruntime_add_shared_library_module(onnxruntime_providers_cuda ${onnxruntime_providers_cuda_src})
135endif()
136# config_cuda_provider_shared_module can be used to config onnxruntime_providers_cuda_obj, onnxruntime_providers_cuda & onnxruntime_providers_cuda_ut.
137# This function guarantees that all 3 targets have the same configurations.
138function(config_cuda_provider_shared_module target)
139if (onnxruntime_REDUCED_OPS_BUILD)
140add_op_reduction_include_dirs(${target})
141endif()
142
143if (HAS_GUARD_CF)
144target_compile_options(${target} PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:-Xcompiler /guard:cf>")
145endif()
146
147if (HAS_QSPECTRE)
148target_compile_options(${target} PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:-Xcompiler /Qspectre>")
149endif()
150
151foreach(ORT_FLAG ${ORT_WARNING_FLAGS})
152target_compile_options(${target} PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:-Xcompiler \"${ORT_FLAG}\">")
153endforeach()
154
155# CUDA 11.3+ supports parallel compilation
156# https://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html#options-for-guiding-compiler-driver-threads
157if (CMAKE_CUDA_COMPILER_VERSION VERSION_GREATER_EQUAL 11.3)
158set(onnxruntime_NVCC_THREADS "1" CACHE STRING "Number of threads that NVCC can use for compilation.")
159target_compile_options(${target} PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--threads \"${onnxruntime_NVCC_THREADS}\">")
160endif()
161
162if (UNIX)
163target_compile_options(${target} PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:-Xcompiler -Wno-reorder>"
164"$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:-Wno-reorder>")
165target_compile_options(${target} PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:-Xcompiler -Wno-error=sign-compare>"
166"$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:-Wno-error=sign-compare>")
167else()
168#mutex.cuh(91): warning C4834: discarding return value of function with 'nodiscard' attribute
169target_compile_options(${target} PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:-Xcompiler /wd4834>")
170target_compile_options(${target} PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:-Xcompiler /wd4127>")
171if (MSVC)
172# the VS warnings for 'Conditional Expression is Constant' are spurious as they don't handle multiple conditions
173# e.g. `if (std::is_same_v<T, float> && not_a_const)` will generate the warning even though constexpr cannot
174# be used due to `&& not_a_const`. This affects too many places for it to be reasonable to disable at a finer
175# granularity.
176target_compile_options(${target} PRIVATE "$<$<COMPILE_LANGUAGE:CXX>:/wd4127>")
177endif()
178endif()
179
180if(MSVC)
181target_compile_options(${target} PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:-Xcompiler /Zc:__cplusplus>")
182endif()
183
184onnxruntime_add_include_to_target(${target} onnxruntime_common onnxruntime_framework onnx onnx_proto ${PROTOBUF_LIB} flatbuffers::flatbuffers)
185if (onnxruntime_ENABLE_TRAINING_OPS)
186onnxruntime_add_include_to_target(${target} onnxruntime_training)
187if (onnxruntime_ENABLE_TRAINING)
188target_link_libraries(${target} PRIVATE onnxruntime_training)
189endif()
190if (onnxruntime_ENABLE_TRAINING_TORCH_INTEROP OR onnxruntime_ENABLE_TRITON)
191onnxruntime_add_include_to_target(${target} Python::Module)
192endif()
193endif()
194
195add_dependencies(${target} onnxruntime_providers_shared ${onnxruntime_EXTERNAL_DEPENDENCIES})
196if(onnxruntime_CUDA_MINIMAL)
197target_compile_definitions(${target} PRIVATE USE_CUDA_MINIMAL)
198target_link_libraries(${target} PRIVATE ${ABSEIL_LIBS} ${ONNXRUNTIME_PROVIDERS_SHARED} Boost::mp11 safeint_interface CUDA::cudart)
199else()
200include(cudnn_frontend) # also defines CUDNN::*
201if (onnxruntime_USE_CUDA_NHWC_OPS)
202if(CUDNN_MAJOR_VERSION GREATER 8)
203add_compile_definitions(ENABLE_CUDA_NHWC_OPS)
204else()
205message( WARNING "To compile with NHWC ops enabled please compile against cuDNN 9 or newer." )
206endif()
207endif()
208target_link_libraries(${target} PRIVATE CUDA::cublasLt CUDA::cublas CUDNN::cudnn_all cudnn_frontend CUDA::curand CUDA::cufft CUDA::cudart
209${ABSEIL_LIBS} ${ONNXRUNTIME_PROVIDERS_SHARED} Boost::mp11 safeint_interface)
210endif()
211
212if (onnxruntime_USE_TRITON_KERNEL)
213# compile triton kernel, generate .a and .h files
214include(onnxruntime_compile_triton_kernel.cmake)
215compile_triton_kernel(triton_kernel_obj_file triton_kernel_header_dir)
216add_dependencies(${target} onnxruntime_triton_kernel)
217target_compile_definitions(${target} PRIVATE USE_TRITON_KERNEL)
218target_include_directories(${target} PRIVATE ${triton_kernel_header_dir})
219target_link_libraries(${target} PUBLIC -Wl,--whole-archive ${triton_kernel_obj_file} -Wl,--no-whole-archive)
220# lib cuda needed by cuLaunchKernel
221target_link_libraries(${target} PRIVATE CUDA::cuda_driver)
222endif()
223
224include(cutlass)
225target_include_directories(${target} PRIVATE ${cutlass_SOURCE_DIR}/include ${cutlass_SOURCE_DIR}/examples ${cutlass_SOURCE_DIR}/tools/util/include)
226
227target_include_directories(${target} PRIVATE ${ONNXRUNTIME_ROOT} ${CMAKE_CURRENT_BINARY_DIR} ${eigen_INCLUDE_DIRS} ${TVM_INCLUDES}
228PUBLIC ${CUDAToolkit_INCLUDE_DIRS})
229# ${CMAKE_CURRENT_BINARY_DIR} is so that #include "onnxruntime_config.h" inside tensor_shape.h is found
230set_target_properties(${target} PROPERTIES LINKER_LANGUAGE CUDA)
231set_target_properties(${target} PROPERTIES FOLDER "ONNXRuntime")
232
233if (onnxruntime_ENABLE_CUDA_PROFILING) # configure cupti for cuda profiling
234target_link_libraries(${target} PRIVATE CUDA::cupti)
235endif()
236
237if (onnxruntime_ENABLE_NVTX_PROFILE)
238target_link_libraries(${target} PRIVATE CUDA::nvtx3)
239endif()
240
241if (onnxruntime_ENABLE_TRAINING_OPS)
242target_include_directories(${target} PRIVATE ${ORTTRAINING_ROOT} ${MPI_CXX_INCLUDE_DIRS})
243endif()
244
245if(onnxruntime_USE_MPI)
246target_link_libraries(${target} PRIVATE ${MPI_LIBRARIES} ${MPI_CXX_LINK_FLAGS})
247endif()
248
249if (onnxruntime_USE_NCCL)
250target_include_directories(${target} PRIVATE ${NCCL_INCLUDE_DIRS})
251target_link_libraries(${target} PRIVATE ${NCCL_LIBRARIES})
252endif()
253
254if (WIN32)
255# *.cu cannot use PCH
256if (NOT onnxruntime_BUILD_CACHE)
257target_precompile_headers(${target} PUBLIC
258"${ONNXRUNTIME_ROOT}/core/providers/cuda/cuda_pch.h"
259"${ONNXRUNTIME_ROOT}/core/providers/cuda/cuda_pch.cc"
260)
261endif()
262
263# minimize the Windows includes.
264# this avoids an issue with CUDA 11.6 where 'small' is defined in the windows and cuda headers.
265target_compile_definitions(${target} PRIVATE "WIN32_LEAN_AND_MEAN")
266
267# disable a warning from the CUDA headers about unreferenced local functions
268#target_compile_options(${target} PRIVATE /wd4505)
269set(onnxruntime_providers_cuda_static_library_flags
270-IGNORE:4221 # LNK4221: This object file does not define any previously undefined public symbols, so it will not be used by any link operation that consumes this library
271)
272set_target_properties(${target} PROPERTIES
273STATIC_LIBRARY_FLAGS "${onnxruntime_providers_cuda_static_library_flags}")
274endif()
275
276if(APPLE)
277set_property(TARGET ${target} APPEND_STRING PROPERTY LINK_FLAGS "-Xlinker -exported_symbols_list ${ONNXRUNTIME_ROOT}/core/providers/cuda/exported_symbols.lst")
278target_link_libraries(${target} PRIVATE nsync::nsync_cpp)
279elseif(UNIX)
280set_property(TARGET ${target} APPEND_STRING PROPERTY LINK_FLAGS "-Xlinker --version-script=${ONNXRUNTIME_ROOT}/core/providers/cuda/version_script.lds -Xlinker --gc-sections")
281target_link_libraries(${target} PRIVATE nsync::nsync_cpp)
282elseif(WIN32)
283set_property(TARGET ${target} APPEND_STRING PROPERTY LINK_FLAGS "-DEF:${ONNXRUNTIME_ROOT}/core/providers/cuda/symbols.def")
284else()
285message(FATAL_ERROR "${target} unknown platform, need to specify shared library exports for it")
286endif()
287
288if (onnxruntime_ENABLE_ATEN)
289target_compile_definitions(${target} PRIVATE ENABLE_ATEN)
290endif()
291endfunction()
292if(onnxruntime_ENABLE_CUDA_EP_INTERNAL_TESTS)
293config_cuda_provider_shared_module(onnxruntime_providers_cuda_obj)
294endif()
295config_cuda_provider_shared_module(onnxruntime_providers_cuda)
296# Cannot use glob because the file cuda_provider_options.h should not be exposed out.
297set(ONNXRUNTIME_CUDA_PROVIDER_PUBLIC_HEADERS
298"${REPO_ROOT}/include/onnxruntime/core/providers/cuda/cuda_context.h"
299"${REPO_ROOT}/include/onnxruntime/core/providers/cuda/cuda_resource.h"
300)
301set_target_properties(onnxruntime_providers_cuda PROPERTIES
302PUBLIC_HEADER "${ONNXRUNTIME_CUDA_PROVIDER_PUBLIC_HEADERS}")
303install(TARGETS onnxruntime_providers_cuda
304PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/onnxruntime/core/providers/cuda
305ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
306LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
307RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
308