CMakeLists.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. cmake_minimum_required(VERSION 3.21)
  2. project(aphrodite_extensions LANGUAGES CXX)
  3. option(APHRODITE_TARGET_DEVICE "Target device backend for Aphrodite" "cuda")
  4. message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
  5. message(STATUS "Target device: ${APHRODITE_TARGET_DEVICE}")
  6. include(${CMAKE_CURRENT_LIST_DIR}/cmake/utils.cmake)
  7. #
  8. # Supported python versions. These versions will be searched in order, the
  9. # first match will be selected. These should be kept in sync with setup.py.
  10. #
  11. set(PYTHON_SUPPORTED_VERSIONS "3.8" "3.9" "3.10" "3.11")
  12. # Supported NVIDIA architectures.
  13. set(CUDA_SUPPORTED_ARCHS "6.0;6.1;7.0;7.5;8.0;8.6;8.9;9.0")
  14. # Supported AMD GPU architectures.
  15. set(HIP_SUPPORTED_ARCHS "gfx906;gfx908;gfx90a;gfx940;gfx941;gfx942;gfx1030;gfx1100")
  16. #
  17. # Supported/expected torch versions for CUDA/ROCm.
  18. #
  19. # Currently, having an incorrect pytorch version results in a warning
  20. # rather than an error.
  21. #
  22. # Note: the CUDA torch version is derived from pyproject.toml and various
  23. # requirements.txt files and should be kept consistent. The ROCm torch
  24. # versions are derived from Dockerfile.rocm
  25. #
  26. set(TORCH_SUPPORTED_VERSION_CUDA "2.3.0")
  27. set(TORCH_SUPPORTED_VERSION_ROCM_5X "2.0.1")
  28. set(TORCH_SUPPORTED_VERSION_ROCM_6X "2.1.1")
  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. #
  57. # Normally `torch.utils.cpp_extension.CUDAExtension` would add
  58. # `libtorch_python.so` for linking against an extension. Torch's cmake
  59. # configuration does not include this library (presumably since the cmake
  60. # config is used for standalone C++ binaries that link against torch).
  61. # The `libtorch_python.so` library defines some of the glue code between
  62. # torch/python via pybind and is required by APHRODITE extensions for this
  63. # reason. So, add it by manually using `append_torchlib_if_found` from
  64. # torch's cmake setup.
  65. #
  66. find_library(torch_python_LIBRARY torch_python PATHS
  67. "${TORCH_INSTALL_PREFIX}/lib")
  68. #
  69. # Forward the non-CUDA device extensions to external CMake scripts.
  70. #
  71. if (NOT APHRODITE_TARGET_DEVICE STREQUAL "cuda" AND
  72. NOT APHRODITE_TARGET_DEVICE STREQUAL "rocm")
  73. if (APHRODITE_TARGET_DEVICE STREQUAL "cpu")
  74. include(${CMAKE_CURRENT_LIST_DIR}/cmake/cpu_extension.cmake)
  75. else()
  76. message(FATAL_ERROR "Unsupported Aphrodite target device: ${APHRODITE_TARGET_DEVICE}")
  77. endif()
  78. return()
  79. endif()
  80. #
  81. # Set up GPU language and check the torch version and warn if it isn't
  82. # what is expected.
  83. #
  84. if (NOT HIP_FOUND AND CUDA_FOUND)
  85. set(APHRODITE_GPU_LANG "CUDA")
  86. if (NOT Torch_VERSION VERSION_EQUAL ${TORCH_SUPPORTED_VERSION_CUDA})
  87. message(WARNING "Pytorch version ${TORCH_SUPPORTED_VERSION_CUDA} "
  88. "expected for CUDA build, saw ${Torch_VERSION} instead.")
  89. endif()
  90. elseif(HIP_FOUND)
  91. set(APHRODITE_GPU_LANG "HIP")
  92. # Importing torch recognizes and sets up some HIP/ROCm configuration but does
  93. # not let cmake recognize .hip files. In order to get cmake to understand the
  94. # .hip extension automatically, HIP must be enabled explicitly.
  95. enable_language(HIP)
  96. # ROCm 5.x
  97. if (ROCM_VERSION_DEV_MAJOR EQUAL 5 AND
  98. NOT Torch_VERSION VERSION_EQUAL ${TORCH_SUPPORTED_VERSION_ROCM_5X})
  99. message(WARNING "Pytorch version ${TORCH_SUPPORTED_VERSION_ROCM_5X} "
  100. "expected for ROCMm 5.x build, saw ${Torch_VERSION} instead.")
  101. endif()
  102. # ROCm 6.x
  103. if (ROCM_VERSION_DEV_MAJOR EQUAL 6 AND
  104. NOT Torch_VERSION VERSION_EQUAL ${TORCH_SUPPORTED_VERSION_ROCM_6X})
  105. message(WARNING "Pytorch version ${TORCH_SUPPORTED_VERSION_ROCM_6X} "
  106. "expected for ROCMm 6.x build, saw ${Torch_VERSION} instead.")
  107. endif()
  108. else()
  109. message(FATAL_ERROR "Can't find CUDA or HIP installation.")
  110. endif()
  111. #
  112. # Override the GPU architectures detected by cmake/torch and filter them by
  113. # the supported versions for the current language.
  114. # The final set of arches is stored in `APHRODITE_GPU_ARCHES`.
  115. #
  116. override_gpu_arches(APHRODITE_GPU_ARCHES
  117. ${APHRODITE_GPU_LANG}
  118. "${${APHRODITE_GPU_LANG}_SUPPORTED_ARCHS}")
  119. #
  120. # Query torch for additional GPU compilation flags for the given
  121. # `APHRODITE_GPU_LANG`.
  122. # The final set of arches is stored in `APHRODITE_GPU_FLAGS`.
  123. #
  124. get_torch_gpu_compiler_flags(APHRODITE_GPU_FLAGS ${APHRODITE_GPU_LANG})
  125. #
  126. # Set nvcc parallelism.
  127. #
  128. if(NVCC_THREADS AND APHRODITE_GPU_LANG STREQUAL "CUDA")
  129. list(APPEND APHRODITE_GPU_FLAGS "--threads=${NVCC_THREADS}")
  130. endif()
  131. #
  132. # Define extension targets
  133. #
  134. #
  135. # _C extension
  136. #
  137. set(APHRODITE_EXT_SRC
  138. "kernels/cache_kernels.cu"
  139. "kernels/attention/attention_kernels.cu"
  140. "kernels/pos_encoding_kernels.cu"
  141. "kernels/activation_kernels.cu"
  142. "kernels/layernorm_kernels.cu"
  143. "kernels/cuda_utils_kernels.cu"
  144. "kernels/moe/align_block_size_kernel.cu"
  145. "kernels/pybind.cpp")
  146. if(APHRODITE_GPU_LANG STREQUAL "CUDA")
  147. list(APPEND APHRODITE_EXT_SRC
  148. "kernels/all_reduce/custom_all_reduce.cu")
  149. endif()
  150. define_gpu_extension_target(
  151. _C
  152. DESTINATION aphrodite
  153. LANGUAGE ${APHRODITE_GPU_LANG}
  154. SOURCES ${APHRODITE_EXT_SRC}
  155. COMPILE_FLAGS ${APHRODITE_GPU_FLAGS}
  156. ARCHITECTURES ${APHRODITE_GPU_ARCHES}
  157. WITH_SOABI)
  158. #
  159. # _quant_C extension
  160. #
  161. set (APHRODITE_QUANT_EXT_SRC
  162. "kernels/quantization/gptq/q_gemm.cu"
  163. "kernels/quantization/squeezellm/quant_cuda_kernel.cu"
  164. "kernels/quantization/exl2/q_matrix.cu"
  165. "kernels/quantization/exl2/q_gemm_exl2.cu"
  166. "kernels/quantization/fp8/fp8_cuda_kernels.cu"
  167. "kernels/quantization/quant_ops.cpp")
  168. if(APHRODITE_GPU_LANG STREQUAL "CUDA")
  169. list(APPEND APHRODITE_QUANT_EXT_SRC
  170. "kernels/quantization/aqlm/gemm_kernels.cu"
  171. "kernels/quantization/awq/gemm_kernels.cu"
  172. "kernels/quantization/bitsandbytes/int4_fp16_gemm_kernels.cu"
  173. "kernels/quantization/bitsandbytes/format.cu"
  174. "kernels/quantization/bitsandbytes/gemm_s4_f16.cu"
  175. "kernels/quantization/gguf/gguf_kernel.cu"
  176. "kernels/quantization/marlin/marlin_cuda_kernel.cu"
  177. "kernels/quantization/quip/origin_order.cu")
  178. endif()
  179. define_gpu_extension_target(
  180. _quant_C
  181. DESTINATION aphrodite
  182. LANGUAGE ${APHRODITE_GPU_LANG}
  183. SOURCES ${APHRODITE_QUANT_EXT_SRC}
  184. COMPILE_FLAGS ${APHRODITE_GPU_FLAGS}
  185. ARCHITECTURES ${APHRODITE_GPU_ARCHES}
  186. WITH_SOABI)
  187. #
  188. # _moe_C extension
  189. #
  190. set(APHRODITE_MOE_EXT_SRC
  191. "kernels/moe/moe_ops.cpp"
  192. "kernels/moe/softmax.cu")
  193. define_gpu_extension_target(
  194. _moe_C
  195. DESTINATION aphrodite
  196. LANGUAGE ${APHRODITE_GPU_LANG}
  197. SOURCES ${APHRODITE_MOE_EXT_SRC}
  198. COMPILE_FLAGS ${APHRODITE_GPU_FLAGS}
  199. ARCHITECTURES ${APHRODITE_GPU_ARCHES}
  200. WITH_SOABI)
  201. #
  202. # _punica_C extension
  203. #
  204. set(APHRODITE_PUNICA_EXT_SRC
  205. "kernels/punica/bgmv/bgmv_bf16_bf16_bf16.cu"
  206. "kernels/punica/bgmv/bgmv_bf16_bf16_fp16.cu"
  207. "kernels/punica/bgmv/bgmv_bf16_fp16_bf16.cu"
  208. "kernels/punica/bgmv/bgmv_bf16_fp16_fp16.cu"
  209. "kernels/punica/bgmv/bgmv_bf16_fp32_bf16.cu"
  210. "kernels/punica/bgmv/bgmv_bf16_fp32_fp16.cu"
  211. "kernels/punica/bgmv/bgmv_fp16_bf16_bf16.cu"
  212. "kernels/punica/bgmv/bgmv_fp16_bf16_fp16.cu"
  213. "kernels/punica/bgmv/bgmv_fp16_fp16_bf16.cu"
  214. "kernels/punica/bgmv/bgmv_fp16_fp16_fp16.cu"
  215. "kernels/punica/bgmv/bgmv_fp16_fp32_bf16.cu"
  216. "kernels/punica/bgmv/bgmv_fp16_fp32_fp16.cu"
  217. "kernels/punica/bgmv/bgmv_fp32_bf16_bf16.cu"
  218. "kernels/punica/bgmv/bgmv_fp32_bf16_fp16.cu"
  219. "kernels/punica/bgmv/bgmv_fp32_fp16_bf16.cu"
  220. "kernels/punica/bgmv/bgmv_fp32_fp16_fp16.cu"
  221. "kernels/punica/bgmv/bgmv_fp32_fp32_bf16.cu"
  222. "kernels/punica/bgmv/bgmv_fp32_fp32_fp16.cu"
  223. "kernels/punica/punica_ops.cc")
  224. #
  225. # Copy GPU compilation flags+update for punica
  226. #
  227. set(APHRODITE_PUNICA_GPU_FLAGS ${APHRODITE_GPU_FLAGS})
  228. list(REMOVE_ITEM APHRODITE_PUNICA_GPU_FLAGS
  229. "-D__CUDA_NO_HALF_OPERATORS__"
  230. "-D__CUDA_NO_HALF_CONVERSIONS__"
  231. "-D__CUDA_NO_BFLOAT16_CONVERSIONS__"
  232. "-D__CUDA_NO_HALF2_OPERATORS__")
  233. #
  234. # Filter out CUDA architectures < 8.0 for punica.
  235. #
  236. if (${APHRODITE_GPU_LANG} STREQUAL "CUDA")
  237. set(APHRODITE_PUNICA_GPU_ARCHES)
  238. foreach(ARCH ${APHRODITE_GPU_ARCHES})
  239. string_to_ver(CODE_VER ${ARCH})
  240. if (CODE_VER GREATER_EQUAL 8.0)
  241. list(APPEND APHRODITE_PUNICA_GPU_ARCHES ${ARCH})
  242. endif()
  243. endforeach()
  244. message(STATUS "Punica target arches: ${APHRODITE_PUNICA_GPU_ARCHES}")
  245. endif()
  246. if (APHRODITE_PUNICA_GPU_ARCHES)
  247. define_gpu_extension_target(
  248. _punica_C
  249. DESTINATION aphrodite
  250. LANGUAGE ${APHRODITE_GPU_LANG}
  251. SOURCES ${APHRODITE_PUNICA_EXT_SRC}
  252. COMPILE_FLAGS ${APHRODITE_PUNICA_GPU_FLAGS}
  253. ARCHITECTURES ${APHRODITE_PUNICA_GPU_ARCHES}
  254. WITH_SOABI)
  255. else()
  256. message(WARNING "Unable to create _punica_C target because none of the "
  257. "requested architectures (${APHRODITE_GPU_ARCHES}) are supported, i.e. >= 8.0")
  258. endif()
  259. #
  260. # _hadamard_C extension
  261. #
  262. set(APHRODITE_HADAMARD_EXT_SRC
  263. "kernels/hadamard/fast_hadamard_transform.cpp"
  264. "kernels/hadamard/fast_hadamard_transform_cuda.cu")
  265. #
  266. # Copy GPU compilation flags+update for hadamard
  267. #
  268. set(APHRODITE_HADAMARD_GPU_FLAGS ${APHRODITE_GPU_FLAGS})
  269. list(APPEND APHRODITE_HADAMARD_GPU_FLAGS
  270. "-U__CUDA_NO_HALF_OPERATORS__"
  271. "-U__CUDA_NO_HALF_CONVERSIONS__"
  272. "-U__CUDA_NO_BFLOAT16_OPERATORS__"
  273. "-U__CUDA_NO_BFLOAT16_CONVERSIONS__"
  274. "-U__CUDA_NO_BFLOAT162_OPERATORS__"
  275. "-U__CUDA_NO_BFLOAT162_CONVERSIONS__"
  276. "--expt-relaxed-constexpr"
  277. "--expt-extended-lambda"
  278. "--use_fast_math"
  279. "--ptxas-options=-v"
  280. "-lineinfo")
  281. #
  282. # Filter out CUDA architectures < 7.0 for hadamard.
  283. #
  284. if (${APHRODITE_GPU_LANG} STREQUAL "CUDA")
  285. set(APHRODITE_HADAMARD_GPU_ARCHES)
  286. foreach(ARCH ${APHRODITE_GPU_ARCHES})
  287. string_to_ver(CODE_VER ${ARCH})
  288. if (CODE_VER GREATER_EQUAL 6.0)
  289. list(APPEND APHRODITE_HADAMARD_GPU_ARCHES ${ARCH})
  290. endif()
  291. endforeach()
  292. message(STATUS "Hadamard target arches: ${APHRODITE_HADAMARD_GPU_ARCHES}")
  293. endif()
  294. if (APHRODITE_HADAMARD_GPU_ARCHES)
  295. define_gpu_extension_target(
  296. _hadamard_C
  297. DESTINATION aphrodite
  298. LANGUAGE ${APHRODITE_GPU_LANG}
  299. SOURCES ${APHRODITE_HADAMARD_EXT_SRC}
  300. COMPILE_FLAGS ${APHRODITE_HADAMARD_GPU_FLAGS}
  301. ARCHITECTURES ${APHRODITE_HADAMARD_GPU_ARCHES}
  302. WITH_SOABI)
  303. else()
  304. message(WARNING "Unable to create _hadamard_C target because none of the "
  305. "requested architectures (${APHRODITE_GPU_ARCHES}) are supported, i.e. >= 6.0")
  306. endif()
  307. #
  308. # Add the `default` target which detects which extensions should be
  309. # built based on platform/architecture. This is the same logic that
  310. # setup.py uses to select which extensions should be built and should
  311. # be kept in sync.
  312. #
  313. # The `default` target makes direct use of cmake easier since knowledge
  314. # of which extensions are supported has been factored in, e.g.
  315. #
  316. # mkdir build && cd build
  317. # cmake -G Ninja -DAPHRODITE_PYTHON_EXECUTABLE=`which python3` -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=../aphrodite ..
  318. # cmake --build . --target default
  319. #
  320. add_custom_target(default)
  321. if(APHRODITE_GPU_LANG STREQUAL "CUDA" OR APHRODITE_GPU_LANG STREQUAL "HIP")
  322. message(STATUS "Enabling C extension.")
  323. add_dependencies(default _C)
  324. endif()
  325. if(APHRODITE_GPU_LANG STREQUAL "CUDA")
  326. message(STATUS "Enabling moe extension.")
  327. add_dependencies(default _moe_C)
  328. # Enable punica if -DAPHRODITE_INSTALL_PUNICA_KERNELS=ON or
  329. # APHRODITE_INSTALL_PUNICA_KERNELS is set in the environment and
  330. # there are supported target arches.
  331. if (APHRODITE_QUANT_EXT_SRC AND
  332. (ENV{APHRODITE_INSTALL_QUANT_KERNELS} OR APHRODITE_INSTALL_QUANT_KERNELS))
  333. message(STATUS "Enabling quant extension.")
  334. add_dependencies(default _quant_C)
  335. endif()
  336. if (APHRODITE_PUNICA_GPU_ARCHES AND
  337. (ENV{APHRODITE_INSTALL_PUNICA_KERNELS} OR APHRODITE_INSTALL_PUNICA_KERNELS))
  338. message(STATUS "Enabling punica extension.")
  339. add_dependencies(default _punica_C)
  340. endif()
  341. if (APHRODITE_HADAMARD_GPU_ARCHES AND
  342. (ENV{APHRODITE_INSTALL_HADAMARD_KERNELS} OR APHRODITE_INSTALL_HADAMARD_KERNELS))
  343. message(STATUS "Enabling hadamard extension.")
  344. add_dependencies(default _hadamard_C)
  345. endif()
  346. endif()