interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / src / ggml-cuda / CMakeLists.txt

268 lines · 11145 bytes Raw Blame History
1cmake_minimum_required(VERSION 3.18) # for CMAKE_CUDA_ARCHITECTURES
2
3# ARM64EC uses the x64 ABI, so it needs the x64 import libraries. The arm64 ones
4# hold native ARM64 symbols and cannot satisfy EC references at link time.
5# FindCUDAToolkit picks the library dir from the host arch, not the target, so
6# anchor its search at lib/x64 here. It derives everything else from CUDA_CUDART.
7string(TOLOWER "${CMAKE_GENERATOR_PLATFORM}" GGML_CUDA_PLATFORM_LWR)
8if (MSVC AND GGML_CUDA_PLATFORM_LWR STREQUAL "arm64ec")
9 find_library(CUDA_CUDART
10 NAMES cudart
11 HINTS ${CUDAToolkit_ROOT} ENV CUDA_PATH
12 PATH_SUFFIXES lib/x64
13 )
14endif()
15
16find_package(CUDAToolkit)
17
18if (CUDAToolkit_FOUND)
19 message(STATUS "CUDA Toolkit found")
20
21 if (NOT DEFINED CMAKE_CUDA_ARCHITECTURES)
22 # native == GPUs available at build time
23 # 50 == Maxwell, lowest CUDA 12 standard
24 # 60 == P100, FP16 CUDA intrinsics
25 # 61 == Pascal, __dp4a instruction (per-byte integer dot product)
26 # 70 == V100, FP16 tensor cores
27 # 75 == Turing, int8 tensor cores
28 # 80 == Ampere, asynchronous data loading, faster tensor core instructions
29 # 86 == RTX 3000, needs CUDA v11.1
30 # 89 == RTX 4000, needs CUDA v11.8
31 # 90 == Hopper H100/200, needs CUDA v11.8
32 # 120 == Blackwell, needs CUDA v12.8, FP4 tensor cores
33 #
34 # XX-virtual == compile CUDA code as PTX, do JIT compilation to binary code on first run
35 # XX-real == compile CUDA code as device code for this specific architecture
36 # no suffix == compile as both PTX and device code
37 #
38 # The default behavior for a non-native is to build virtual architectures as needed to cover all features needed
39 # for best performance and to also build real architectures for the most commonly used GPUs.
40 if (GGML_NATIVE AND CUDAToolkit_VERSION VERSION_GREATER_EQUAL "11.6" AND CMAKE_VERSION VERSION_GREATER_EQUAL "3.24")
41 set(CMAKE_CUDA_ARCHITECTURES "native")
42 else()
43 if (CUDAToolkit_VERSION VERSION_LESS "13")
44 list(APPEND CMAKE_CUDA_ARCHITECTURES 50-virtual 61-virtual 70-virtual)
45 endif ()
46
47 list(APPEND CMAKE_CUDA_ARCHITECTURES 75-virtual 80-virtual 86-real)
48
49 if (CUDAToolkit_VERSION VERSION_GREATER_EQUAL "11.8")
50 list(APPEND CMAKE_CUDA_ARCHITECTURES 89-real 90-virtual)
51 endif()
52
53 if (CUDAToolkit_VERSION VERSION_GREATER_EQUAL "12.8")
54 # The CUDA architecture 120f-virtual would in principle work for Blackwell support
55 # but the newly added "f" suffix conflicted with a preexising regex for validating CUDA architectures in CMake.
56 # So either a recent CMake version or one with the backported fix is needed.
57 # The following versions should work:
58 # - CMake >= v3.31.8 && CMake < v4.0.0
59 # - CMake >= v4.0.2
60 # This is NOT documented in the CMake release notes,
61 # check Modules/Internal/CMakeCUDAArchitecturesValidate.cmake in the CMake git repository instead.
62 # However, the architectures 120a-real and 121a-real should work with basically any CMake version and
63 # until the release of e.g. Rubin there is no benefit to shipping virtual architectures for Blackwell.
64 list(APPEND CMAKE_CUDA_ARCHITECTURES 120a-real)
65 endif()
66 if (CUDAToolkit_VERSION VERSION_GREATER_EQUAL "12.9")
67 list(APPEND CMAKE_CUDA_ARCHITECTURES 121a-real)
68 endif()
69 endif()
70 endif()
71
72 enable_language(CUDA)
73
74 if (GGML_CUDA_CCCL_VERSION)
75 include(FetchContent)
76
77 FetchContent_Declare(
78 CCCL
79 GIT_REPOSITORY https://github.com/nvidia/cccl.git
80 GIT_TAG "${GGML_CUDA_CCCL_VERSION}"
81 GIT_SHALLOW TRUE
82 )
83
84 FetchContent_MakeAvailable(CCCL)
85 endif()
86
87 # Replace any plain 12X CUDA architectures with their "architecture-specific" equivalents 12Xa.
88 # 12X is forwards-compatible, 12Xa is not.
89 # Notably the Blackwell FP4 tensor core instructions are not forwards compatible and therefore need 12Xa.
90 # But while 12X vs. 12Xa can be checked in device code there is (to my knowledge) no easy way to do the same check in host code.
91 # So for now just replace all instances of 12X with 12Xa, this should be fine until Rubin is released.
92 foreach(ARCHS IN ITEMS CMAKE_CUDA_ARCHITECTURES CMAKE_CUDA_ARCHITECTURES_NATIVE)
93 set(FIXED_ARCHS "")
94 foreach(ARCH IN LISTS ${ARCHS})
95 if (ARCH MATCHES "^12[0-9](-real|-virtual)?$")
96 string(REGEX REPLACE "^(12[0-9])((-real|-virtual)?)$" "\\1a\\2" FIXED_ARCH ${ARCH})
97 message(STATUS "Replacing ${ARCH} in ${ARCHS} with ${FIXED_ARCH}")
98 list(APPEND FIXED_ARCHS "${FIXED_ARCH}")
99 else()
100 list(APPEND FIXED_ARCHS "${ARCH}")
101 endif()
102 endforeach()
103 set(${ARCHS} ${FIXED_ARCHS})
104 endforeach()
105
106 # If we try to compile a "native" build it will use the 12X architectures and fail.
107 # So we should instead use the native architectures as determined by CMake after replacing 12X with 12Xa.
108 # But if at the time of the build no GPUs are connected at all CMAKE_CUDA_ARCHITECTURES will contain garbage that we should not use.
109 if (CMAKE_CUDA_ARCHITECTURES STREQUAL "native" AND CMAKE_CUDA_ARCHITECTURES_NATIVE MATCHES "^[0-9]+(a|f)?(-real|-virtual)?(;[0-9]+(a|f)?(-real|-virtual)?|;)*$")
110 set(CMAKE_CUDA_ARCHITECTURES ${CMAKE_CUDA_ARCHITECTURES_NATIVE})
111 endif()
112 message(STATUS "Using CMAKE_CUDA_ARCHITECTURES=${CMAKE_CUDA_ARCHITECTURES} CMAKE_CUDA_ARCHITECTURES_NATIVE=${CMAKE_CUDA_ARCHITECTURES_NATIVE}")
113
114 file(GLOB GGML_HEADERS_CUDA "*.cuh")
115 list(APPEND GGML_HEADERS_CUDA "../../include/ggml-cuda.h")
116
117 file(GLOB GGML_SOURCES_CUDA "*.cu")
118 file(GLOB SRCS "template-instances/fattn-tile*.cu")
119 list(APPEND GGML_SOURCES_CUDA ${SRCS})
120 file(GLOB SRCS "template-instances/fattn-mma*.cu")
121 list(APPEND GGML_SOURCES_CUDA ${SRCS})
122 file(GLOB SRCS "template-instances/mmq*.cu")
123 list(APPEND GGML_SOURCES_CUDA ${SRCS})
124 file(GLOB SRCS "template-instances/mmf*.cu")
125 list(APPEND GGML_SOURCES_CUDA ${SRCS})
126
127 ggml_cuda_fattn_vec_instances(${CMAKE_CURRENT_SOURCE_DIR} SRCS)
128 list(APPEND GGML_SOURCES_CUDA ${SRCS})
129
130 ggml_add_backend_library(ggml-cuda
131 ${GGML_HEADERS_CUDA}
132 ${GGML_SOURCES_CUDA}
133 )
134
135 if (GGML_CUDA_GRAPHS)
136 add_compile_definitions(GGML_CUDA_USE_GRAPHS)
137 endif()
138
139 if (GGML_CUDA_FORCE_MMQ)
140 add_compile_definitions(GGML_CUDA_FORCE_MMQ)
141 endif()
142
143 if (GGML_CUDA_FORCE_CUBLAS)
144 add_compile_definitions(GGML_CUDA_FORCE_CUBLAS)
145 endif()
146
147 if (GGML_CUDA_NO_VMM)
148 add_compile_definitions(GGML_CUDA_NO_VMM)
149 endif()
150
151 if (NOT GGML_CUDA_FA)
152 add_compile_definitions(GGML_CUDA_NO_FA)
153 endif()
154
155 if (GGML_CUDA_NO_PEER_COPY)
156 add_compile_definitions(GGML_CUDA_NO_PEER_COPY)
157 endif()
158
159 if (GGML_CUDA_CCCL_VERSION)
160 target_link_libraries(ggml-cuda PRIVATE CCCL::CCCL)
161 endif()
162
163 if (GGML_STATIC)
164 if (WIN32)
165 # As of 12.3.1 CUDA Toolkit for Windows does not offer a static cublas library
166 target_link_libraries(ggml-cuda PRIVATE CUDA::cudart_static CUDA::cublas)
167 else ()
168 if (CUDAToolkit_VERSION VERSION_GREATER_EQUAL "10.1")
169 target_link_libraries(ggml-cuda PRIVATE CUDA::cudart_static CUDA::cublas_static CUDA::cublasLt_static)
170 else()
171 target_link_libraries(ggml-cuda PRIVATE CUDA::cudart_static CUDA::cublas_static)
172 endif()
173 endif()
174 else()
175 target_link_libraries(ggml-cuda PRIVATE CUDA::cudart CUDA::cublas)
176 endif()
177
178 if (GGML_CUDA_NO_VMM)
179 # No VMM requested, no need to link directly with the cuda driver lib (libcuda.so)
180 else()
181 target_link_libraries(ggml-cuda PRIVATE CUDA::cuda_driver)
182 endif()
183
184 if (GGML_CUDA_NCCL)
185 find_package(NCCL)
186 if (NCCL_FOUND)
187 add_compile_definitions(GGML_USE_NCCL)
188 target_link_libraries(ggml-cuda PRIVATE NCCL::NCCL)
189 else()
190 message(STATUS "Warning: NCCL not found, performance for multiple CUDA GPUs will be suboptimal")
191 endif()
192 endif()
193
194 set(CUDA_CXX_FLAGS "")
195
196 set(CUDA_FLAGS -use_fast_math -extended-lambda)
197
198 if (GGML_CUDA_DEBUG)
199 list(APPEND CUDA_FLAGS -lineinfo)
200 add_compile_definitions(GGML_CUDA_DEBUG)
201 endif()
202
203 if (CUDAToolkit_VERSION VERSION_GREATER_EQUAL "12.8")
204 # Options are:
205 # - none (not recommended)
206 # - speed (nvcc's default)
207 # - balance
208 # - size
209 list(APPEND CUDA_FLAGS -compress-mode=${GGML_CUDA_COMPRESSION_MODE})
210 endif()
211
212 if (GGML_FATAL_WARNINGS)
213 list(APPEND CUDA_FLAGS -Werror all-warnings)
214 endif()
215
216 if (GGML_ALL_WARNINGS AND NOT MSVC)
217 set(NVCC_CMD ${CMAKE_CUDA_COMPILER} .c)
218 if (NOT CMAKE_CUDA_HOST_COMPILER STREQUAL "")
219 list(APPEND NVCC_CMD -ccbin ${CMAKE_CUDA_HOST_COMPILER})
220 endif()
221
222 execute_process(
223 COMMAND ${NVCC_CMD} -Xcompiler --version
224 OUTPUT_VARIABLE CUDA_CCFULLVER
225 ERROR_QUIET
226 )
227
228 if (NOT CUDA_CCFULLVER MATCHES clang)
229 set(CUDA_CCID "GNU")
230 execute_process(
231 COMMAND ${NVCC_CMD} -Xcompiler "-dumpfullversion -dumpversion"
232 OUTPUT_VARIABLE CUDA_CCVER
233 ERROR_QUIET
234 OUTPUT_STRIP_TRAILING_WHITESPACE
235 )
236 else()
237 if (CUDA_CCFULLVER MATCHES Apple)
238 set(CUDA_CCID "AppleClang")
239 else()
240 set(CUDA_CCID "Clang")
241 endif()
242 string(REGEX REPLACE "^.* version ([0-9.]*).*$" "\\1" CUDA_CCVER ${CUDA_CCFULLVER})
243 endif()
244
245 message(STATUS "CUDA host compiler is ${CUDA_CCID} ${CUDA_CCVER}")
246
247 ggml_get_flags(${CUDA_CCID} ${CUDA_CCVER})
248 list(APPEND CUDA_CXX_FLAGS ${CXX_FLAGS} ${GF_CXX_FLAGS}) # This is passed to -Xcompiler later
249 endif()
250
251 if (NOT MSVC)
252 list(APPEND CUDA_CXX_FLAGS -Wno-pedantic)
253 else()
254 # CCCL 3.2 onwards will require a cpp-standard-compliant preprocessor for MSVC
255 # https://github.com/NVIDIA/cccl/pull/6827
256 list(APPEND CUDA_CXX_FLAGS /Zc:preprocessor)
257 endif()
258
259 list(JOIN CUDA_CXX_FLAGS " " CUDA_CXX_FLAGS_JOINED) # pass host compiler flags as a single argument
260
261 if (NOT CUDA_CXX_FLAGS_JOINED STREQUAL "")
262 list(APPEND CUDA_FLAGS -Xcompiler ${CUDA_CXX_FLAGS_JOINED})
263 endif()
264
265 target_compile_options(ggml-cuda PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:${CUDA_FLAGS}>")
266else()
267 message(FATAL_ERROR "CUDA Toolkit not found")
268endif()