CMakeLists.txt 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. cmake_minimum_required(VERSION 3.26)
  2. project(aphrodite_extensions LANGUAGES CXX)
  3. # CUDA by default, can be overridden by using -DAPHRODITE_TARGET_DEVICE=... (used by setup.py)
  4. set(APHRODITE_TARGET_DEVICE "cuda" CACHE STRING "Target device backend for Aphrodite")
  5. message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
  6. message(STATUS "Target device: ${APHRODITE_TARGET_DEVICE}")
  7. include(${CMAKE_CURRENT_LIST_DIR}/cmake/utils.cmake)
  8. #
  9. # Supported python versions. These versions will be searched in order, the
  10. # first match will be selected. These should be kept in sync with setup.py.
  11. #
  12. set(PYTHON_SUPPORTED_VERSIONS "3.8" "3.9" "3.10" "3.11" "3.12")
  13. # Supported NVIDIA architectures.
  14. set(CUDA_SUPPORTED_ARCHS "6.0;6.1;7.0;7.5;8.0;8.6;8.9;9.0")
  15. # Supported AMD GPU architectures.
  16. set(HIP_SUPPORTED_ARCHS "gfx906;gfx908;gfx90a;gfx940;gfx941;gfx942;gfx1030;gfx1100")
  17. #
  18. # Supported/expected torch versions for CUDA/ROCm.
  19. #
  20. # Currently, having an incorrect pytorch version results in a warning
  21. # rather than an error.
  22. #
  23. # Note: the CUDA torch version is derived from pyproject.toml and various
  24. # requirements.txt files and should be kept consistent. The ROCm torch
  25. # versions are derived from Dockerfile.rocm
  26. #
  27. set(TORCH_SUPPORTED_VERSION_CUDA "2.4.0")
  28. set(TORCH_SUPPORTED_VERSION_ROCM "2.5.0")
  29. #
  30. # Try to find python package with an executable that exactly matches
  31. # `APHRODITE_PYTHON_EXECUTABLE` and is one of the supported versions.
  32. #
  33. if (APHRODITE_PYTHON_EXECUTABLE)
  34. find_python_from_executable(${APHRODITE_PYTHON_EXECUTABLE} "${PYTHON_SUPPORTED_VERSIONS}")
  35. else()
  36. message(FATAL_ERROR
  37. "Please set APHRODITE_PYTHON_EXECUTABLE to the path of the desired python version"
  38. " before running cmake configure.")
  39. endif()
  40. #
  41. # Update cmake's `CMAKE_PREFIX_PATH` with torch location.
  42. #
  43. append_cmake_prefix_path("torch" "torch.utils.cmake_prefix_path")
  44. # Ensure the 'nvcc' command is in the PATH
  45. find_program(NVCC_EXECUTABLE nvcc)
  46. if (CUDA_FOUND AND NOT NVCC_EXECUTABLE)
  47. message(FATAL_ERROR "nvcc not found")
  48. endif()
  49. #
  50. # Import torch cmake configuration.
  51. # Torch also imports CUDA (and partially HIP) languages with some customizations,
  52. # so there is no need to do this explicitly with check_language/enable_language,
  53. # etc.
  54. #
  55. find_package(Torch REQUIRED)
  56. find_package(CUDA REQUIRED)
  57. find_package(CUDAToolkit REQUIRED)
  58. # Add cuBLAS to the list of libraries to link against
  59. list(APPEND LIBS CUDA::cublas)
  60. set(CMAKE_CXX_STANDARD 17)
  61. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  62. set(CMAKE_CUDA_STANDARD 17)
  63. set(CMAKE_CUDA_STANDARD_REQUIRED ON)
  64. # Replace -std=c++20 with -std=c++17 in APHRODITE_GPU_FLAGS
  65. if(APHRODITE_GPU_LANG STREQUAL "CUDA")
  66. list(APPEND APHRODITE_GPU_FLAGS "--std=c++17" "-Xcompiler -Wno-return-type")
  67. endif()
  68. #
  69. # Add the `default` target which detects which extensions should be
  70. # built based on platform/architecture. This is the same logic that
  71. # setup.py uses to select which extensions should be built and should
  72. # be kept in sync.
  73. #
  74. # The `default` target makes direct use of cmake easier since knowledge
  75. # of which extensions are supported has been factored in, e.g.
  76. #
  77. # mkdir build && cd build
  78. # cmake -G Ninja -DAPHRODITE_PYTHON_EXECUTABLE=`which python3` -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=../aphrodite ..
  79. # cmake --build . --target default
  80. #
  81. add_custom_target(default)
  82. message(STATUS "Enabling core extension.")
  83. # Define _core_C extension
  84. # built for (almost) every target platform, (excludes TPU and Neuron)
  85. set(APHRODITE_EXT_SRC
  86. "kernels/core/torch_bindings.cpp")
  87. define_gpu_extension_target(
  88. _core_C
  89. DESTINATION aphrodite
  90. LANGUAGE CXX
  91. SOURCES ${APHRODITE_EXT_SRC}
  92. COMPILE_FLAGS ${CXX_COMPILE_FLAGS}
  93. USE_SABI 3
  94. WITH_SOABI)
  95. add_dependencies(default _core_C)
  96. #
  97. # Forward the non-CUDA device extensions to external CMake scripts.
  98. #
  99. if (NOT APHRODITE_TARGET_DEVICE STREQUAL "cuda" AND
  100. NOT APHRODITE_TARGET_DEVICE STREQUAL "rocm")
  101. if (APHRODITE_TARGET_DEVICE STREQUAL "cpu")
  102. include(${CMAKE_CURRENT_LIST_DIR}/cmake/cpu_extension.cmake)
  103. else()
  104. return()
  105. endif()
  106. return()
  107. endif()
  108. #
  109. # Set up GPU language and check the torch version and warn if it isn't
  110. # what is expected.
  111. #
  112. if (NOT HIP_FOUND AND CUDA_FOUND)
  113. set(APHRODITE_GPU_LANG "CUDA")
  114. if (NOT Torch_VERSION VERSION_EQUAL ${TORCH_SUPPORTED_VERSION_CUDA})
  115. message(WARNING "Pytorch version ${TORCH_SUPPORTED_VERSION_CUDA} "
  116. "expected for CUDA build, saw ${Torch_VERSION} instead.")
  117. endif()
  118. elseif(HIP_FOUND)
  119. set(APHRODITE_GPU_LANG "HIP")
  120. # Importing torch recognizes and sets up some HIP/ROCm configuration but does
  121. # not let cmake recognize .hip files. In order to get cmake to understand the
  122. # .hip extension automatically, HIP must be enabled explicitly.
  123. enable_language(HIP)
  124. # ROCm 5.X and 6.X
  125. if (ROCM_VERSION_DEV_MAJOR GREATER_EQUAL 5 AND
  126. NOT Torch_VERSION VERSION_EQUAL ${TORCH_SUPPORTED_VERSION_ROCM})
  127. message(WARNING "Pytorch version >= ${TORCH_SUPPORTED_VERSION_ROCM} "
  128. "expected for ROCm build, saw ${Torch_VERSION} instead.")
  129. endif()
  130. else()
  131. message(FATAL_ERROR "Can't find CUDA or HIP installation.")
  132. endif()
  133. #
  134. # Override the GPU architectures detected by cmake/torch and filter them by
  135. # the supported versions for the current language.
  136. # The final set of arches is stored in `APHRODITE_GPU_ARCHES`.
  137. #
  138. override_gpu_arches(APHRODITE_GPU_ARCHES
  139. ${APHRODITE_GPU_LANG}
  140. "${${APHRODITE_GPU_LANG}_SUPPORTED_ARCHS}")
  141. #
  142. # Query torch for additional GPU compilation flags for the given
  143. # `APHRODITE_GPU_LANG`.
  144. # The final set of arches is stored in `APHRODITE_GPU_FLAGS`.
  145. #
  146. get_torch_gpu_compiler_flags(APHRODITE_GPU_FLAGS ${APHRODITE_GPU_LANG})
  147. #
  148. # Set nvcc parallelism.
  149. #
  150. if(NVCC_THREADS AND APHRODITE_GPU_LANG STREQUAL "CUDA")
  151. list(APPEND APHRODITE_GPU_FLAGS "--threads=${NVCC_THREADS}")
  152. endif()
  153. #
  154. # Define other extension targets
  155. #
  156. #
  157. # _C extension
  158. #
  159. set(APHRODITE_EXT_SRC
  160. "kernels/cache_kernels.cu"
  161. "kernels/attention/attention_kernels.cu"
  162. "kernels/pos_encoding_kernels.cu"
  163. "kernels/activation_kernels.cu"
  164. "kernels/layernorm_kernels.cu"
  165. "kernels/quantization/squeezellm/quant_cuda_kernel.cu"
  166. "kernels/quantization/gptq/q_gemm.cu"
  167. "kernels/quantization/compressed_tensors/int8_quant_kernels.cu"
  168. "kernels/quantization/fp8/common.cu"
  169. "kernels/cuda_utils_kernels.cu"
  170. "kernels/moe/align_block_size_kernel.cu"
  171. "kernels/prepare_inputs/advance_step.cu"
  172. "kernels/torch_bindings.cpp")
  173. if(APHRODITE_GPU_LANG STREQUAL "CUDA")
  174. list(APPEND APHRODITE_EXT_SRC
  175. "kernels/quantization/fp6/fp6_linear.cu"
  176. "kernels/mamba/mamba_ssm/selective_scan_fwd.cu"
  177. "kernels/mamba/causal_conv1d/causal_conv1d.cu"
  178. "kernels/quantization/aqlm/gemm_kernels.cu"
  179. "kernels/quantization/awq/gemm_kernels.cu"
  180. "kernels/quantization/quip/origin_order.cu"
  181. "kernels/quantization/gptq_marlin/gptq_marlin.cu"
  182. "kernels/quantization/gptq_marlin/gptq_marlin_repack.cu"
  183. "kernels/quantization/marlin/dense/marlin_cuda_kernel.cu"
  184. "kernels/quantization/marlin/sparse/marlin_24_cuda_kernel.cu"
  185. "kernels/quantization/marlin/qqq/marlin_qqq_gemm_kernel.cu"
  186. "kernels/quantization/gguf/gguf_kernel.cu"
  187. "kernels/quantization/gptq_marlin/awq_marlin_repack.cu"
  188. "kernels/quantization/fp8/fp8_marlin.cu"
  189. "kernels/all_reduce/custom_all_reduce.cu")
  190. # Add CUTLASS and GPTQ Marlin kernels if not MSVC
  191. if(NOT MSVC)
  192. # Include CUTLASS only when needed
  193. include(FetchContent)
  194. SET(CUTLASS_ENABLE_HEADERS_ONLY=ON)
  195. FetchContent_Declare(
  196. cutlass
  197. GIT_REPOSITORY https://github.com/nvidia/cutlass.git
  198. # CUTLASS 3.5.1
  199. GIT_TAG 06b21349bcf6ddf6a1686a47a137ad1446579db9
  200. )
  201. FetchContent_MakeAvailable(cutlass)
  202. list(APPEND APHRODITE_EXT_SRC
  203. "kernels/quantization/cutlass_w8a8/scaled_mm_entry.cu"
  204. "kernels/quantization/cutlass_w8a8/scaled_mm_c2x.cu"
  205. "kernels/quantization/cutlass_w8a8/scaled_mm_c3x.cu")
  206. # Enable sm90a for Hopper CUTLASS kernels when using newer CUDA
  207. if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER 12.0)
  208. set_source_files_properties(
  209. "kernels/quantization/cutlass_w8a8/scaled_mm_c2x.cu"
  210. "kernels/quantization/cutlass_w8a8/scaled_mm_c3x.cu"
  211. PROPERTIES
  212. COMPILE_FLAGS
  213. "-gencode arch=compute_90a,code=sm_90a -Wno-psabi")
  214. endif()
  215. endif()
  216. endif()
  217. define_gpu_extension_target(
  218. _C
  219. DESTINATION aphrodite
  220. LANGUAGE ${APHRODITE_GPU_LANG}
  221. SOURCES ${APHRODITE_EXT_SRC}
  222. COMPILE_FLAGS ${APHRODITE_GPU_FLAGS}
  223. ARCHITECTURES ${APHRODITE_GPU_ARCHES}
  224. INCLUDE_DIRECTORIES ${CUTLASS_INCLUDE_DIR}
  225. LIBRARIES ${LIBS}
  226. USE_SABI 3
  227. WITH_SOABI)
  228. #
  229. # _moe_C extension
  230. #
  231. set(APHRODITE_MOE_EXT_SRC
  232. "kernels/moe/torch_bindings.cpp"
  233. "kernels/moe/softmax.cu")
  234. define_gpu_extension_target(
  235. _moe_C
  236. DESTINATION aphrodite
  237. LANGUAGE ${APHRODITE_GPU_LANG}
  238. SOURCES ${APHRODITE_MOE_EXT_SRC}
  239. COMPILE_FLAGS ${APHRODITE_GPU_FLAGS}
  240. ARCHITECTURES ${APHRODITE_GPU_ARCHES}
  241. USE_SABI 3
  242. WITH_SOABI)
  243. if(APHRODITE_GPU_LANG STREQUAL "CUDA" OR APHRODITE_GPU_LANG STREQUAL "HIP")
  244. message(STATUS "Enabling C extension.")
  245. add_dependencies(default _C)
  246. message(STATUS "Enabling moe extension.")
  247. add_dependencies(default _moe_C)
  248. endif()