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