CMakeLists.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. cmake_minimum_required(VERSION 3.21)
  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")
  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.3.1")
  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. #
  57. # Forward the non-CUDA device extensions to external CMake scripts.
  58. #
  59. if (NOT APHRODITE_TARGET_DEVICE STREQUAL "cuda" AND
  60. NOT APHRODITE_TARGET_DEVICE STREQUAL "rocm")
  61. if (APHRODITE_TARGET_DEVICE STREQUAL "cpu")
  62. include(${CMAKE_CURRENT_LIST_DIR}/cmake/cpu_extension.cmake)
  63. else()
  64. message(FATAL_ERROR "Unsupported Aphrodite target device: ${APHRODITE_TARGET_DEVICE}")
  65. endif()
  66. return()
  67. endif()
  68. #
  69. # Set up GPU language and check the torch version and warn if it isn't
  70. # what is expected.
  71. #
  72. if (NOT HIP_FOUND AND CUDA_FOUND)
  73. set(APHRODITE_GPU_LANG "CUDA")
  74. if (NOT Torch_VERSION VERSION_EQUAL ${TORCH_SUPPORTED_VERSION_CUDA})
  75. message(WARNING "Pytorch version ${TORCH_SUPPORTED_VERSION_CUDA} "
  76. "expected for CUDA build, saw ${Torch_VERSION} instead.")
  77. endif()
  78. elseif(HIP_FOUND)
  79. set(APHRODITE_GPU_LANG "HIP")
  80. # Importing torch recognizes and sets up some HIP/ROCm configuration but does
  81. # not let cmake recognize .hip files. In order to get cmake to understand the
  82. # .hip extension automatically, HIP must be enabled explicitly.
  83. enable_language(HIP)
  84. # ROCm 5.X and 6.X
  85. if (ROCM_VERSION_DEV_MAJOR GREATER_EQUAL 5 AND
  86. NOT Torch_VERSION VERSION_EQUAL ${TORCH_SUPPORTED_VERSION_ROCM})
  87. message(WARNING "Pytorch version >= ${TORCH_SUPPORTED_VERSION_ROCM} "
  88. "expected for ROCm build, saw ${Torch_VERSION} instead.")
  89. endif()
  90. else()
  91. message(FATAL_ERROR "Can't find CUDA or HIP installation.")
  92. endif()
  93. #
  94. # Override the GPU architectures detected by cmake/torch and filter them by
  95. # the supported versions for the current language.
  96. # The final set of arches is stored in `APHRODITE_GPU_ARCHES`.
  97. #
  98. override_gpu_arches(APHRODITE_GPU_ARCHES
  99. ${APHRODITE_GPU_LANG}
  100. "${${APHRODITE_GPU_LANG}_SUPPORTED_ARCHS}")
  101. #
  102. # Query torch for additional GPU compilation flags for the given
  103. # `APHRODITE_GPU_LANG`.
  104. # The final set of arches is stored in `APHRODITE_GPU_FLAGS`.
  105. #
  106. get_torch_gpu_compiler_flags(APHRODITE_GPU_FLAGS ${APHRODITE_GPU_LANG})
  107. #
  108. # Set nvcc parallelism.
  109. #
  110. if(NVCC_THREADS AND APHRODITE_GPU_LANG STREQUAL "CUDA")
  111. list(APPEND APHRODITE_GPU_FLAGS "--threads=${NVCC_THREADS}")
  112. endif()
  113. #
  114. # Define extension targets
  115. #
  116. #
  117. # _C extension
  118. #
  119. set(APHRODITE_EXT_SRC
  120. "kernels/cache_kernels.cu"
  121. "kernels/attention/attention_kernels.cu"
  122. "kernels/pos_encoding_kernels.cu"
  123. "kernels/activation_kernels.cu"
  124. "kernels/layernorm_kernels.cu"
  125. "kernels/quantization/squeezellm/quant_cuda_kernel.cu"
  126. "kernels/quantization/gptq/q_gemm.cu"
  127. "kernels/quantization/compressed_tensors/int8_quant_kernels.cu"
  128. "kernels/quantization/fp8/common.cu"
  129. "kernels/cuda_utils_kernels.cu"
  130. "kernels/moe/align_block_size_kernel.cu"
  131. "kernels/prepare_inputs/advance_step.cu"
  132. "kernels/torch_bindings.cpp")
  133. if(APHRODITE_GPU_LANG STREQUAL "CUDA")
  134. include(FetchContent)
  135. SET(CUTLASS_ENABLE_HEADERS_ONLY=ON)
  136. FetchContent_Declare(
  137. cutlass
  138. GIT_REPOSITORY https://github.com/nvidia/cutlass.git
  139. # CUTLASS 3.5.0
  140. GIT_TAG 7d49e6c7e2f8896c47f586706e67e1fb215529dc
  141. )
  142. FetchContent_MakeAvailable(cutlass)
  143. list(APPEND APHRODITE_EXT_SRC
  144. "kernels/mamba/mamba_ssm/selective_scan_fwd.cu"
  145. "kernels/mamba/causal_conv1d/causal_conv1d.cu"
  146. "kernels/quantization/aqlm/gemm_kernels.cu"
  147. "kernels/quantization/awq/gemm_kernels.cu"
  148. "kernels/quantization/quip/origin_order.cu"
  149. "kernels/quantization/marlin/dense/marlin_cuda_kernel.cu"
  150. "kernels/quantization/marlin/sparse/marlin_24_cuda_kernel.cu"
  151. "kernels/quantization/gptq_marlin/gptq_marlin.cu"
  152. "kernels/quantization/gptq_marlin/gptq_marlin_repack.cu"
  153. "kernels/quantization/gptq_marlin/awq_marlin_repack.cu"
  154. "kernels/quantization/fp8/fp8_marlin.cu"
  155. "kernels/all_reduce/custom_all_reduce.cu"
  156. "kernels/sampling/sampling.cu"
  157. "kernels/quantization/cutlass_w8a8/scaled_mm_entry.cu"
  158. "kernels/quantization/cutlass_w8a8/scaled_mm_c2x.cu"
  159. "kernels/quantization/cutlass_w8a8/scaled_mm_c3x.cu")
  160. #
  161. # The CUTLASS kernels for Hopper require sm90a to be enabled.
  162. # This is done via the below gencode option, BUT that creates kernels for both sm90 and sm90a.
  163. # That adds an extra 17MB to compiled binary, so instead we selectively enable it.
  164. if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER 12.0)
  165. set_source_files_properties(
  166. "kernels/quantization/cutlass_w8a8/scaled_mm_c3x.cu"
  167. PROPERTIES
  168. COMPILE_FLAGS
  169. "-gencode arch=compute_90a,code=sm_90a")
  170. endif()
  171. endif()
  172. define_gpu_extension_target(
  173. _C
  174. DESTINATION aphrodite
  175. LANGUAGE ${APHRODITE_GPU_LANG}
  176. SOURCES ${APHRODITE_EXT_SRC}
  177. COMPILE_FLAGS ${APHRODITE_GPU_FLAGS}
  178. ARCHITECTURES ${APHRODITE_GPU_ARCHES}
  179. INCLUDE_DIRECTORIES ${CUTLASS_INCLUDE_DIR};${CUTLASS_TOOLS_UTIL_INCLUDE_DIR}
  180. USE_SABI 3
  181. WITH_SOABI)
  182. #
  183. # _moe_C extension
  184. #
  185. set(APHRODITE_MOE_EXT_SRC
  186. "kernels/moe/torch_bindings.cpp"
  187. "kernels/moe/softmax.cu")
  188. define_gpu_extension_target(
  189. _moe_C
  190. DESTINATION aphrodite
  191. LANGUAGE ${APHRODITE_GPU_LANG}
  192. SOURCES ${APHRODITE_MOE_EXT_SRC}
  193. COMPILE_FLAGS ${APHRODITE_GPU_FLAGS}
  194. ARCHITECTURES ${APHRODITE_GPU_ARCHES}
  195. USE_SABI 3
  196. WITH_SOABI)
  197. #
  198. # _punica_C extension
  199. #
  200. set(APHRODITE_PUNICA_EXT_SRC
  201. "kernels/punica/bgmv/bgmv_bf16_bf16_bf16.cu"
  202. "kernels/punica/bgmv/bgmv_bf16_bf16_fp16.cu"
  203. "kernels/punica/bgmv/bgmv_bf16_fp16_bf16.cu"
  204. "kernels/punica/bgmv/bgmv_bf16_fp16_fp16.cu"
  205. "kernels/punica/bgmv/bgmv_bf16_fp32_bf16.cu"
  206. "kernels/punica/bgmv/bgmv_bf16_fp32_fp16.cu"
  207. "kernels/punica/bgmv/bgmv_fp16_bf16_bf16.cu"
  208. "kernels/punica/bgmv/bgmv_fp16_bf16_fp16.cu"
  209. "kernels/punica/bgmv/bgmv_fp16_fp16_bf16.cu"
  210. "kernels/punica/bgmv/bgmv_fp16_fp16_fp16.cu"
  211. "kernels/punica/bgmv/bgmv_fp16_fp32_bf16.cu"
  212. "kernels/punica/bgmv/bgmv_fp16_fp32_fp16.cu"
  213. "kernels/punica/bgmv/bgmv_fp32_bf16_bf16.cu"
  214. "kernels/punica/bgmv/bgmv_fp32_bf16_fp16.cu"
  215. "kernels/punica/bgmv/bgmv_fp32_fp16_bf16.cu"
  216. "kernels/punica/bgmv/bgmv_fp32_fp16_fp16.cu"
  217. "kernels/punica/bgmv/bgmv_fp32_fp32_bf16.cu"
  218. "kernels/punica/bgmv/bgmv_fp32_fp32_fp16.cu"
  219. "kernels/punica/punica_ops.cu"
  220. "kernels/punica/torch_bindings.cpp")
  221. #
  222. # Copy GPU compilation flags+update for punica
  223. #
  224. set(APHRODITE_PUNICA_GPU_FLAGS ${APHRODITE_GPU_FLAGS})
  225. list(REMOVE_ITEM APHRODITE_PUNICA_GPU_FLAGS
  226. "-D__CUDA_NO_HALF_OPERATORS__"
  227. "-D__CUDA_NO_HALF_CONVERSIONS__"
  228. "-D__CUDA_NO_BFLOAT16_CONVERSIONS__"
  229. "-D__CUDA_NO_HALF2_OPERATORS__")
  230. #
  231. # Filter out CUDA architectures < 8.0 for punica.
  232. #
  233. if (${APHRODITE_GPU_LANG} STREQUAL "CUDA")
  234. set(APHRODITE_PUNICA_GPU_ARCHES)
  235. foreach(ARCH ${APHRODITE_GPU_ARCHES})
  236. string_to_ver(CODE_VER ${ARCH})
  237. if (CODE_VER GREATER_EQUAL 8.0)
  238. list(APPEND APHRODITE_PUNICA_GPU_ARCHES ${ARCH})
  239. endif()
  240. endforeach()
  241. message(STATUS "Punica target arches: ${APHRODITE_PUNICA_GPU_ARCHES}")
  242. elseif(${APHRODITE_GPU_LANG} STREQUAL "HIP")
  243. set(APHRODITE_PUNICA_GPU_ARCHES ${APHRODITE_GPU_ARCHES})
  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. USE_SABI 3
  255. WITH_SOABI)
  256. else()
  257. message(WARNING "Unable to create _punica_C target because none of the "
  258. "requested architectures (${APHRODITE_GPU_ARCHES}) are supported, i.e. >= 8.0")
  259. endif()
  260. # #
  261. # # _hadamard_C extension
  262. # #
  263. # set(APHRODITE_HADAMARD_EXT_SRC
  264. # "kernels/hadamard/fast_hadamard_transform.cpp"
  265. # "kernels/hadamard/fast_hadamard_transform_cuda.cu")
  266. # #
  267. # # Copy GPU compilation flags+update for hadamard
  268. # #
  269. # set(APHRODITE_HADAMARD_GPU_FLAGS ${APHRODITE_GPU_FLAGS})
  270. # list(APPEND APHRODITE_HADAMARD_GPU_FLAGS
  271. # "-U__CUDA_NO_HALF_OPERATORS__"
  272. # "-U__CUDA_NO_HALF_CONVERSIONS__"
  273. # "-U__CUDA_NO_BFLOAT16_OPERATORS__"
  274. # "-U__CUDA_NO_BFLOAT16_CONVERSIONS__"
  275. # "-U__CUDA_NO_BFLOAT162_OPERATORS__"
  276. # "-U__CUDA_NO_BFLOAT162_CONVERSIONS__"
  277. # "--expt-relaxed-constexpr"
  278. # "--expt-extended-lambda"
  279. # "--use_fast_math"
  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. message(STATUS "Enabling moe extension.")
  325. add_dependencies(default _moe_C)
  326. # Enable punica if -DAPHRODITE_INSTALL_PUNICA_KERNELS=ON or
  327. # APHRODITE_INSTALL_PUNICA_KERNELS is set in the environment and
  328. # there are supported target arches.
  329. if (APHRODITE_PUNICA_GPU_ARCHES AND
  330. (ENV{APHRODITE_INSTALL_PUNICA_KERNELS} OR APHRODITE_INSTALL_PUNICA_KERNELS))
  331. message(STATUS "Enabling punica extension.")
  332. add_dependencies(default _punica_C)
  333. endif()
  334. # if (APHRODITE_HADAMARD_GPU_ARCHES AND
  335. # (ENV{APHRODITE_INSTALL_HADAMARD_KERNELS} OR APHRODITE_INSTALL_HADAMARD_KERNELS))
  336. # message(STATUS "Enabling hadamard extension.")
  337. # add_dependencies(default _hadamard_C)
  338. # endif()
  339. endif()