1
0

CMakeLists.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. "kernels/permute_cols.cu"
  191. "kernels/sampling/sampling.cu")
  192. # Add CUTLASS and GPTQ Marlin kernels if not MSVC
  193. if(NOT MSVC)
  194. # Include CUTLASS only when needed
  195. include(FetchContent)
  196. SET(CUTLASS_ENABLE_HEADERS_ONLY=ON)
  197. FetchContent_Declare(
  198. cutlass
  199. GIT_REPOSITORY https://github.com/nvidia/cutlass.git
  200. # CUTLASS 3.5.1
  201. GIT_TAG 06b21349bcf6ddf6a1686a47a137ad1446579db9
  202. )
  203. FetchContent_MakeAvailable(cutlass)
  204. list(APPEND APHRODITE_EXT_SRC
  205. "kernels/quantization/cutlass_w8a8/scaled_mm_entry.cu"
  206. "kernels/quantization/cutlass_w8a8/scaled_mm_c2x.cu"
  207. "kernels/quantization/cutlass_w8a8/scaled_mm_c3x.cu")
  208. # Enable sm90a for Hopper CUTLASS kernels when using newer CUDA
  209. if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER 12.0)
  210. set_source_files_properties(
  211. "kernels/quantization/cutlass_w8a8/scaled_mm_c2x.cu"
  212. "kernels/quantization/cutlass_w8a8/scaled_mm_c3x.cu"
  213. PROPERTIES
  214. COMPILE_FLAGS
  215. "-gencode arch=compute_90a,code=sm_90a -Wno-psabi")
  216. endif()
  217. endif()
  218. #
  219. # For the Machete kernels we automatically generate sources for various
  220. # preselected input type pairs and schedules.
  221. # Generate sources:
  222. execute_process(
  223. COMMAND ${CMAKE_COMMAND} -E env
  224. PYTHONPATH=${CMAKE_CURRENT_SOURCE_DIR}/kernels/cutlass_extensions/:${CUTLASS_DIR}/python/:$PYTHONPATH
  225. ${Python_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/kernels/quantization/machete/generate.py
  226. RESULT_VARIABLE machete_generation_result
  227. OUTPUT_VARIABLE machete_generation_output
  228. OUTPUT_FILE ${CMAKE_CURRENT_BINARY_DIR}/machete_generation.log
  229. ERROR_FILE ${CMAKE_CURRENT_BINARY_DIR}/machete_generation.log
  230. )
  231. if (NOT machete_generation_result EQUAL 0)
  232. message(FATAL_ERROR "Machete generation failed."
  233. " Result: \"${machete_generation_result}\""
  234. "\nCheck the log for details: "
  235. "${CMAKE_CURRENT_BINARY_DIR}/machete_generation.log")
  236. else()
  237. message(STATUS "Machete generation completed successfully.")
  238. endif()
  239. # Add machete generated sources
  240. file(GLOB MACHETE_GEN_SOURCES "kernels/quantization/machete/generated/*.cu")
  241. list(APPEND APHRODITE_EXT_SRC ${MACHETE_GEN_SOURCES})
  242. message(STATUS "Machete generated sources: ${MACHETE_GEN_SOURCES}")
  243. # See comment above for scaled_mm_c3x (same if condition)
  244. if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER 12.0)
  245. set_source_files_properties(
  246. ${MACHETE_GEN_SOURCES}
  247. PROPERTIES
  248. COMPILE_FLAGS
  249. "-gencode arch=compute_90a,code=sm_90a")
  250. endif()
  251. # Add pytorch binding
  252. list(APPEND APHRODITE_EXT_SRC
  253. kernels/quantization/machete/machete_pytorch.cu)
  254. endif()
  255. define_gpu_extension_target(
  256. _C
  257. DESTINATION aphrodite
  258. LANGUAGE ${APHRODITE_GPU_LANG}
  259. SOURCES ${APHRODITE_EXT_SRC}
  260. COMPILE_FLAGS ${APHRODITE_GPU_FLAGS}
  261. ARCHITECTURES ${APHRODITE_GPU_ARCHES}
  262. INCLUDE_DIRECTORIES ${CUTLASS_INCLUDE_DIR}
  263. LIBRARIES ${LIBS}
  264. USE_SABI 3
  265. WITH_SOABI)
  266. #
  267. # _moe_C extension
  268. #
  269. set(APHRODITE_MOE_EXT_SRC
  270. "kernels/moe/torch_bindings.cpp"
  271. "kernels/moe/softmax.cu")
  272. define_gpu_extension_target(
  273. _moe_C
  274. DESTINATION aphrodite
  275. LANGUAGE ${APHRODITE_GPU_LANG}
  276. SOURCES ${APHRODITE_MOE_EXT_SRC}
  277. COMPILE_FLAGS ${APHRODITE_GPU_FLAGS}
  278. ARCHITECTURES ${APHRODITE_GPU_ARCHES}
  279. USE_SABI 3
  280. WITH_SOABI)
  281. if(APHRODITE_GPU_LANG STREQUAL "CUDA" OR APHRODITE_GPU_LANG STREQUAL "HIP")
  282. message(STATUS "Enabling C extension.")
  283. add_dependencies(default _C)
  284. message(STATUS "Enabling moe extension.")
  285. add_dependencies(default _moe_C)
  286. endif()