CMakeLists.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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;gfx1101")
  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. if(MSVC)
  57. find_package(CUDA REQUIRED)
  58. find_package(CUDAToolkit REQUIRED)
  59. # Add cuBLAS to the list of libraries to link against
  60. list(APPEND LIBS CUDA::cublas)
  61. set(CMAKE_CXX_STANDARD 17)
  62. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  63. set(CMAKE_CUDA_STANDARD 17)
  64. set(CMAKE_CUDA_STANDARD_REQUIRED ON)
  65. # Replace -std=c++20 with -std=c++17 in APHRODITE_GPU_FLAGS
  66. if(APHRODITE_GPU_LANG STREQUAL "CUDA")
  67. list(APPEND APHRODITE_GPU_FLAGS "--std=c++17" "-Xcompiler -Wno-return-type")
  68. endif()
  69. endif()
  70. #
  71. # Add the `default` target which detects which extensions should be
  72. # built based on platform/architecture. This is the same logic that
  73. # setup.py uses to select which extensions should be built and should
  74. # be kept in sync.
  75. #
  76. # The `default` target makes direct use of cmake easier since knowledge
  77. # of which extensions are supported has been factored in, e.g.
  78. #
  79. # mkdir build && cd build
  80. # cmake -G Ninja -DAPHRODITE_PYTHON_EXECUTABLE=`which python3` -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=../aphrodite ..
  81. # cmake --build . --target default
  82. #
  83. add_custom_target(default)
  84. message(STATUS "Enabling core extension.")
  85. # Define _core_C extension
  86. # built for (almost) every target platform, (excludes TPU and Neuron)
  87. set(APHRODITE_EXT_SRC
  88. "kernels/core/torch_bindings.cpp")
  89. define_gpu_extension_target(
  90. _core_C
  91. DESTINATION aphrodite
  92. LANGUAGE CXX
  93. SOURCES ${APHRODITE_EXT_SRC}
  94. COMPILE_FLAGS ${CXX_COMPILE_FLAGS}
  95. USE_SABI 3
  96. WITH_SOABI)
  97. add_dependencies(default _core_C)
  98. #
  99. # Forward the non-CUDA device extensions to external CMake scripts.
  100. #
  101. if (NOT APHRODITE_TARGET_DEVICE STREQUAL "cuda" AND
  102. NOT APHRODITE_TARGET_DEVICE STREQUAL "rocm")
  103. if (APHRODITE_TARGET_DEVICE STREQUAL "cpu")
  104. include(${CMAKE_CURRENT_LIST_DIR}/cmake/cpu_extension.cmake)
  105. else()
  106. return()
  107. endif()
  108. return()
  109. endif()
  110. #
  111. # Set up GPU language and check the torch version and warn if it isn't
  112. # what is expected.
  113. #
  114. if (NOT HIP_FOUND AND CUDA_FOUND)
  115. set(APHRODITE_GPU_LANG "CUDA")
  116. if (NOT Torch_VERSION VERSION_EQUAL ${TORCH_SUPPORTED_VERSION_CUDA})
  117. message(WARNING "Pytorch version ${TORCH_SUPPORTED_VERSION_CUDA} "
  118. "expected for CUDA build, saw ${Torch_VERSION} instead.")
  119. endif()
  120. elseif(HIP_FOUND)
  121. set(APHRODITE_GPU_LANG "HIP")
  122. # Importing torch recognizes and sets up some HIP/ROCm configuration but does
  123. # not let cmake recognize .hip files. In order to get cmake to understand the
  124. # .hip extension automatically, HIP must be enabled explicitly.
  125. enable_language(HIP)
  126. # ROCm 5.X and 6.X
  127. if (ROCM_VERSION_DEV_MAJOR GREATER_EQUAL 5 AND
  128. NOT Torch_VERSION VERSION_EQUAL ${TORCH_SUPPORTED_VERSION_ROCM})
  129. message(WARNING "Pytorch version >= ${TORCH_SUPPORTED_VERSION_ROCM} "
  130. "expected for ROCm build, saw ${Torch_VERSION} instead.")
  131. endif()
  132. else()
  133. message(FATAL_ERROR "Can't find CUDA or HIP installation.")
  134. endif()
  135. #
  136. # Override the GPU architectures detected by cmake/torch and filter them by
  137. # the supported versions for the current language.
  138. # The final set of arches is stored in `APHRODITE_GPU_ARCHES`.
  139. #
  140. override_gpu_arches(APHRODITE_GPU_ARCHES
  141. ${APHRODITE_GPU_LANG}
  142. "${${APHRODITE_GPU_LANG}_SUPPORTED_ARCHS}")
  143. #
  144. # Query torch for additional GPU compilation flags for the given
  145. # `APHRODITE_GPU_LANG`.
  146. # The final set of arches is stored in `APHRODITE_GPU_FLAGS`.
  147. #
  148. get_torch_gpu_compiler_flags(APHRODITE_GPU_FLAGS ${APHRODITE_GPU_LANG})
  149. #
  150. # Set nvcc parallelism.
  151. #
  152. if(NVCC_THREADS AND APHRODITE_GPU_LANG STREQUAL "CUDA")
  153. list(APPEND APHRODITE_GPU_FLAGS "--threads=${NVCC_THREADS}")
  154. endif()
  155. #
  156. # Define other extension targets
  157. #
  158. #
  159. # _C extension
  160. #
  161. set(APHRODITE_EXT_SRC
  162. "kernels/cache_kernels.cu"
  163. "kernels/attention/attention_kernels.cu"
  164. "kernels/pos_encoding_kernels.cu"
  165. "kernels/activation_kernels.cu"
  166. "kernels/layernorm_kernels.cu"
  167. "kernels/quantization/squeezellm/quant_cuda_kernel.cu"
  168. "kernels/quantization/gptq/q_gemm.cu"
  169. "kernels/quantization/compressed_tensors/int8_quant_kernels.cu"
  170. "kernels/quantization/fp8/common.cu"
  171. "kernels/cuda_utils_kernels.cu"
  172. "kernels/moe/align_block_size_kernel.cu"
  173. "kernels/prepare_inputs/advance_step.cu"
  174. "kernels/torch_bindings.cpp")
  175. if(APHRODITE_GPU_LANG STREQUAL "CUDA")
  176. list(APPEND APHRODITE_EXT_SRC
  177. "kernels/quantization/fp6/fp6_linear.cu"
  178. "kernels/mamba/mamba_ssm/selective_scan_fwd.cu"
  179. "kernels/mamba/causal_conv1d/causal_conv1d.cu"
  180. "kernels/quantization/aqlm/gemm_kernels.cu"
  181. "kernels/quantization/awq/gemm_kernels.cu"
  182. "kernels/quantization/quip/origin_order.cu"
  183. "kernels/quantization/gptq_marlin/gptq_marlin.cu"
  184. "kernels/quantization/gptq_marlin/gptq_marlin_repack.cu"
  185. "kernels/quantization/marlin/dense/marlin_cuda_kernel.cu"
  186. "kernels/quantization/marlin/sparse/marlin_24_cuda_kernel.cu"
  187. "kernels/quantization/marlin/qqq/marlin_qqq_gemm_kernel.cu"
  188. "kernels/quantization/gguf/gguf_kernel.cu"
  189. "kernels/quantization/gptq_marlin/awq_marlin_repack.cu"
  190. "kernels/quantization/fp8/fp8_marlin.cu"
  191. "kernels/all_reduce/custom_all_reduce.cu"
  192. "kernels/permute_cols.cu"
  193. "kernels/sampling/sampling.cu")
  194. if(NOT MSVC)
  195. # Include CUTLASS only when needed
  196. include(FetchContent)
  197. SET(CUTLASS_ENABLE_HEADERS_ONLY=ON)
  198. FetchContent_Declare(
  199. cutlass
  200. GIT_REPOSITORY https://github.com/nvidia/cutlass.git
  201. # CUTLASS 3.5.1
  202. GIT_TAG 06b21349bcf6ddf6a1686a47a137ad1446579db9
  203. )
  204. FetchContent_MakeAvailable(cutlass)
  205. list(APPEND APHRODITE_EXT_SRC
  206. "kernels/quantization/cutlass_w8a8/scaled_mm_entry.cu"
  207. "kernels/quantization/cutlass_w8a8/scaled_mm_c2x.cu"
  208. "kernels/quantization/cutlass_w8a8/scaled_mm_c3x.cu")
  209. # Enable sm90a for Hopper CUTLASS kernels when using newer CUDA
  210. if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER 12.0)
  211. set_source_files_properties(
  212. "kernels/quantization/cutlass_w8a8/scaled_mm_c2x.cu"
  213. "kernels/quantization/cutlass_w8a8/scaled_mm_c3x.cu"
  214. PROPERTIES
  215. COMPILE_FLAGS
  216. "-gencode arch=compute_90a,code=sm_90a -Wno-psabi")
  217. endif()
  218. endif()
  219. #
  220. # For the Machete kernels we automatically generate sources for various
  221. # preselected input type pairs and schedules.
  222. # Generate sources:
  223. execute_process(
  224. COMMAND ${CMAKE_COMMAND} -E env
  225. PYTHONPATH=${CMAKE_CURRENT_SOURCE_DIR}/kernels/cutlass_extensions/:${CUTLASS_DIR}/python/:${APHRODITE_PYTHON_PATH}:$PYTHONPATH
  226. ${Python_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/kernels/quantization/machete/generate.py
  227. RESULT_VARIABLE machete_generation_result
  228. OUTPUT_VARIABLE machete_generation_output
  229. OUTPUT_FILE ${CMAKE_CURRENT_BINARY_DIR}/machete_generation.log
  230. ERROR_FILE ${CMAKE_CURRENT_BINARY_DIR}/machete_generation.log
  231. )
  232. if (NOT machete_generation_result EQUAL 0)
  233. message(FATAL_ERROR "Machete generation failed."
  234. " Result: \"${machete_generation_result}\""
  235. "\nCheck the log for details: "
  236. "${CMAKE_CURRENT_BINARY_DIR}/machete_generation.log")
  237. else()
  238. message(STATUS "Machete generation completed successfully.")
  239. endif()
  240. # Add machete generated sources
  241. file(GLOB MACHETE_GEN_SOURCES "kernels/quantization/machete/generated/*.cu")
  242. list(APPEND APHRODITE_EXT_SRC ${MACHETE_GEN_SOURCES})
  243. message(STATUS "Machete generated sources: ${MACHETE_GEN_SOURCES}")
  244. # See comment above for scaled_mm_c3x (same if condition)
  245. if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER 12.0)
  246. set_source_files_properties(
  247. ${MACHETE_GEN_SOURCES}
  248. PROPERTIES
  249. COMPILE_FLAGS
  250. "-gencode arch=compute_90a,code=sm_90a")
  251. endif()
  252. # Add pytorch binding
  253. list(APPEND APHRODITE_EXT_SRC
  254. kernels/quantization/machete/machete_pytorch.cu)
  255. endif()
  256. define_gpu_extension_target(
  257. _C
  258. DESTINATION aphrodite
  259. LANGUAGE ${APHRODITE_GPU_LANG}
  260. SOURCES ${APHRODITE_EXT_SRC}
  261. COMPILE_FLAGS ${APHRODITE_GPU_FLAGS}
  262. ARCHITECTURES ${APHRODITE_GPU_ARCHES}
  263. INCLUDE_DIRECTORIES ${CUTLASS_INCLUDE_DIR}
  264. LIBRARIES ${LIBS}
  265. USE_SABI 3
  266. WITH_SOABI)
  267. #
  268. # _moe_C extension
  269. #
  270. set(APHRODITE_MOE_EXT_SRC
  271. "kernels/moe/torch_bindings.cpp"
  272. "kernels/moe/softmax.cu")
  273. define_gpu_extension_target(
  274. _moe_C
  275. DESTINATION aphrodite
  276. LANGUAGE ${APHRODITE_GPU_LANG}
  277. SOURCES ${APHRODITE_MOE_EXT_SRC}
  278. COMPILE_FLAGS ${APHRODITE_GPU_FLAGS}
  279. ARCHITECTURES ${APHRODITE_GPU_ARCHES}
  280. USE_SABI 3
  281. WITH_SOABI)
  282. if(APHRODITE_GPU_LANG STREQUAL "CUDA" OR APHRODITE_GPU_LANG STREQUAL "HIP")
  283. message(STATUS "Enabling C extension.")
  284. add_dependencies(default _C)
  285. message(STATUS "Enabling moe extension.")
  286. add_dependencies(default _moe_C)
  287. endif()