CMakeLists.txt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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/common.cu"
  167. "kernels/quantization/compressed_tensors/int8_quant_kernels.cu"
  168. "kernels/quantization/quant_ops.cpp")
  169. if(APHRODITE_GPU_LANG STREQUAL "CUDA")
  170. include(FetchContent)
  171. SET(CUTLASS_ENABLE_HEADERS_ONLY=ON)
  172. FetchContent_Declare(
  173. cutlass
  174. GIT_REPOSITORY https://github.com/nvidia/cutlass.git
  175. # CUTLASS 3.5.0
  176. GIT_TAG 7d49e6c7e2f8896c47f586706e67e1fb215529dc
  177. )
  178. FetchContent_MakeAvailable(cutlass)
  179. list(APPEND APHRODITE_QUANT_EXT_SRC
  180. "kernels/quantization/aqlm/gemm_kernels.cu"
  181. "kernels/quantization/awq/gemm_kernels.cu"
  182. "kernels/quantization/bitsandbytes/int4_fp16_gemm_kernels.cu"
  183. "kernels/quantization/bitsandbytes/format.cu"
  184. "kernels/quantization/bitsandbytes/gemm_s4_f16.cu"
  185. "kernels/quantization/gguf/gguf_kernel.cu"
  186. "kernels/quantization/marlin/dense/marlin_cuda_kernel.cu"
  187. "kernels/quantization/marlin/sparse/marlin_24_cuda_kernel.cu"
  188. "kernels/quantization/gptq_marlin/gptq_marlin.cu"
  189. "kernels/quantization/gptq_marlin/gptq_marlin_repack.cu"
  190. "kernels/quantization/quip/origin_order.cu"
  191. "kernels/quantization/cutlass_w8a8/scaled_mm_dq_entry.cu"
  192. "kernels/quantization/cutlass_w8a8/scaled_mm_dq_c2x.cu"
  193. "kernels/quantization/cutlass_w8a8/scaled_mm_dq_c3x.cu")
  194. #
  195. # The CUTLASS kernels for Hopper require sm90a to be enabled.
  196. # This is done via the below gencode option, BUT that creates kernels for both sm90 and sm90a.
  197. # That adds an extra 17MB to compiled binary, so instead we selectively enable it.
  198. set_source_files_properties(
  199. "csrc/quantization/cutlass_w8a8/scaled_mm_dq_c3x.cu"
  200. PROPERTIES
  201. COMPILE_FLAGS
  202. "-gencode arch=compute_90a,code=sm_90a")
  203. endif()
  204. define_gpu_extension_target(
  205. _quant_C
  206. DESTINATION aphrodite
  207. LANGUAGE ${APHRODITE_GPU_LANG}
  208. SOURCES ${APHRODITE_QUANT_EXT_SRC}
  209. COMPILE_FLAGS ${APHRODITE_GPU_FLAGS}
  210. ARCHITECTURES ${APHRODITE_GPU_ARCHES}
  211. INCLUDE_DIRECTORIES ${CUTLASS_INCLUDE_DIR};${CUTLASS_TOOLS_UTIL_INCLUDE_DIR}
  212. WITH_SOABI)
  213. #
  214. # _moe_C extension
  215. #
  216. set(APHRODITE_MOE_EXT_SRC
  217. "kernels/moe/moe_ops.cpp"
  218. "kernels/moe/softmax.cu")
  219. define_gpu_extension_target(
  220. _moe_C
  221. DESTINATION aphrodite
  222. LANGUAGE ${APHRODITE_GPU_LANG}
  223. SOURCES ${APHRODITE_MOE_EXT_SRC}
  224. COMPILE_FLAGS ${APHRODITE_GPU_FLAGS}
  225. ARCHITECTURES ${APHRODITE_GPU_ARCHES}
  226. WITH_SOABI)
  227. #
  228. # _punica_C extension
  229. #
  230. set(APHRODITE_PUNICA_EXT_SRC
  231. "kernels/punica/bgmv/bgmv_bf16_bf16_bf16.cu"
  232. "kernels/punica/bgmv/bgmv_bf16_bf16_fp16.cu"
  233. "kernels/punica/bgmv/bgmv_bf16_fp16_bf16.cu"
  234. "kernels/punica/bgmv/bgmv_bf16_fp16_fp16.cu"
  235. "kernels/punica/bgmv/bgmv_bf16_fp32_bf16.cu"
  236. "kernels/punica/bgmv/bgmv_bf16_fp32_fp16.cu"
  237. "kernels/punica/bgmv/bgmv_fp16_bf16_bf16.cu"
  238. "kernels/punica/bgmv/bgmv_fp16_bf16_fp16.cu"
  239. "kernels/punica/bgmv/bgmv_fp16_fp16_bf16.cu"
  240. "kernels/punica/bgmv/bgmv_fp16_fp16_fp16.cu"
  241. "kernels/punica/bgmv/bgmv_fp16_fp32_bf16.cu"
  242. "kernels/punica/bgmv/bgmv_fp16_fp32_fp16.cu"
  243. "kernels/punica/bgmv/bgmv_fp32_bf16_bf16.cu"
  244. "kernels/punica/bgmv/bgmv_fp32_bf16_fp16.cu"
  245. "kernels/punica/bgmv/bgmv_fp32_fp16_bf16.cu"
  246. "kernels/punica/bgmv/bgmv_fp32_fp16_fp16.cu"
  247. "kernels/punica/bgmv/bgmv_fp32_fp32_bf16.cu"
  248. "kernels/punica/bgmv/bgmv_fp32_fp32_fp16.cu"
  249. "kernels/punica/punica_ops.cu"
  250. "kernels/punica/punica_pybind.cpp")
  251. #
  252. # Copy GPU compilation flags+update for punica
  253. #
  254. set(APHRODITE_PUNICA_GPU_FLAGS ${APHRODITE_GPU_FLAGS})
  255. list(REMOVE_ITEM APHRODITE_PUNICA_GPU_FLAGS
  256. "-D__CUDA_NO_HALF_OPERATORS__"
  257. "-D__CUDA_NO_HALF_CONVERSIONS__"
  258. "-D__CUDA_NO_BFLOAT16_CONVERSIONS__"
  259. "-D__CUDA_NO_HALF2_OPERATORS__")
  260. #
  261. # Filter out CUDA architectures < 8.0 for punica.
  262. #
  263. if (${APHRODITE_GPU_LANG} STREQUAL "CUDA")
  264. set(APHRODITE_PUNICA_GPU_ARCHES)
  265. foreach(ARCH ${APHRODITE_GPU_ARCHES})
  266. string_to_ver(CODE_VER ${ARCH})
  267. if (CODE_VER GREATER_EQUAL 8.0)
  268. list(APPEND APHRODITE_PUNICA_GPU_ARCHES ${ARCH})
  269. endif()
  270. endforeach()
  271. message(STATUS "Punica target arches: ${APHRODITE_PUNICA_GPU_ARCHES}")
  272. elseif(${APHRODITE_GPU_LANG} STREQUAL "HIP")
  273. set(APHRODITE_PUNICA_GPU_ARCHES ${APHRODITE_GPU_ARCHES})
  274. message(STATUS "Punica target arches: ${APHRODITE_PUNICA_GPU_ARCHES}")
  275. endif()
  276. if (APHRODITE_PUNICA_GPU_ARCHES)
  277. define_gpu_extension_target(
  278. _punica_C
  279. DESTINATION aphrodite
  280. LANGUAGE ${APHRODITE_GPU_LANG}
  281. SOURCES ${APHRODITE_PUNICA_EXT_SRC}
  282. COMPILE_FLAGS ${APHRODITE_PUNICA_GPU_FLAGS}
  283. ARCHITECTURES ${APHRODITE_PUNICA_GPU_ARCHES}
  284. WITH_SOABI)
  285. else()
  286. message(WARNING "Unable to create _punica_C target because none of the "
  287. "requested architectures (${APHRODITE_GPU_ARCHES}) are supported, i.e. >= 8.0")
  288. endif()
  289. #
  290. # _hadamard_C extension
  291. #
  292. set(APHRODITE_HADAMARD_EXT_SRC
  293. "kernels/hadamard/fast_hadamard_transform.cpp"
  294. "kernels/hadamard/fast_hadamard_transform_cuda.cu")
  295. #
  296. # Copy GPU compilation flags+update for hadamard
  297. #
  298. set(APHRODITE_HADAMARD_GPU_FLAGS ${APHRODITE_GPU_FLAGS})
  299. list(APPEND APHRODITE_HADAMARD_GPU_FLAGS
  300. "-U__CUDA_NO_HALF_OPERATORS__"
  301. "-U__CUDA_NO_HALF_CONVERSIONS__"
  302. "-U__CUDA_NO_BFLOAT16_OPERATORS__"
  303. "-U__CUDA_NO_BFLOAT16_CONVERSIONS__"
  304. "-U__CUDA_NO_BFLOAT162_OPERATORS__"
  305. "-U__CUDA_NO_BFLOAT162_CONVERSIONS__"
  306. "--expt-relaxed-constexpr"
  307. "--expt-extended-lambda"
  308. "--use_fast_math"
  309. "-lineinfo")
  310. #
  311. # Filter out CUDA architectures < 7.0 for hadamard.
  312. #
  313. if (${APHRODITE_GPU_LANG} STREQUAL "CUDA")
  314. set(APHRODITE_HADAMARD_GPU_ARCHES)
  315. foreach(ARCH ${APHRODITE_GPU_ARCHES})
  316. string_to_ver(CODE_VER ${ARCH})
  317. if (CODE_VER GREATER_EQUAL 6.0)
  318. list(APPEND APHRODITE_HADAMARD_GPU_ARCHES ${ARCH})
  319. endif()
  320. endforeach()
  321. message(STATUS "Hadamard target arches: ${APHRODITE_HADAMARD_GPU_ARCHES}")
  322. endif()
  323. if (APHRODITE_HADAMARD_GPU_ARCHES)
  324. define_gpu_extension_target(
  325. _hadamard_C
  326. DESTINATION aphrodite
  327. LANGUAGE ${APHRODITE_GPU_LANG}
  328. SOURCES ${APHRODITE_HADAMARD_EXT_SRC}
  329. COMPILE_FLAGS ${APHRODITE_HADAMARD_GPU_FLAGS}
  330. ARCHITECTURES ${APHRODITE_HADAMARD_GPU_ARCHES}
  331. WITH_SOABI)
  332. else()
  333. message(WARNING "Unable to create _hadamard_C target because none of the "
  334. "requested architectures (${APHRODITE_GPU_ARCHES}) are supported, i.e. >= 6.0")
  335. endif()
  336. #
  337. # Add the `default` target which detects which extensions should be
  338. # built based on platform/architecture. This is the same logic that
  339. # setup.py uses to select which extensions should be built and should
  340. # be kept in sync.
  341. #
  342. # The `default` target makes direct use of cmake easier since knowledge
  343. # of which extensions are supported has been factored in, e.g.
  344. #
  345. # mkdir build && cd build
  346. # cmake -G Ninja -DAPHRODITE_PYTHON_EXECUTABLE=`which python3` -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=../aphrodite ..
  347. # cmake --build . --target default
  348. #
  349. add_custom_target(default)
  350. if(APHRODITE_GPU_LANG STREQUAL "CUDA" OR APHRODITE_GPU_LANG STREQUAL "HIP")
  351. message(STATUS "Enabling C extension.")
  352. add_dependencies(default _C)
  353. # Enable punica if -DAPHRODITE_INSTALL_PUNICA_KERNELS=ON or
  354. # APHRODITE_INSTALL_PUNICA_KERNELS is set in the environment and
  355. # there are supported target arches.
  356. if (APHRODITE_QUANT_EXT_SRC AND
  357. (ENV{APHRODITE_INSTALL_QUANT_KERNELS} OR APHRODITE_INSTALL_QUANT_KERNELS))
  358. message(STATUS "Enabling quant extension.")
  359. add_dependencies(default _quant_C)
  360. endif()
  361. if (APHRODITE_PUNICA_GPU_ARCHES AND
  362. (ENV{APHRODITE_INSTALL_PUNICA_KERNELS} OR APHRODITE_INSTALL_PUNICA_KERNELS))
  363. message(STATUS "Enabling punica extension.")
  364. add_dependencies(default _punica_C)
  365. endif()
  366. if (APHRODITE_HADAMARD_GPU_ARCHES AND
  367. (ENV{APHRODITE_INSTALL_HADAMARD_KERNELS} OR APHRODITE_INSTALL_HADAMARD_KERNELS))
  368. message(STATUS "Enabling hadamard extension.")
  369. add_dependencies(default _hadamard_C)
  370. endif()
  371. endif()
  372. if(APHRODITE_GPU_LANG STREQUAL "CUDA")
  373. message(STATUS "Enabling moe extension.")
  374. add_dependencies(default _moe_C)
  375. endif()