| 1 | cmake_minimum_required(VERSION 3.26) |
| 2 | |
| 3 | # When building directly using CMake, make sure you run the install step |
| 4 | # (it places the .so files in the correct location). |
| 5 | # |
| 6 | # Example: |
| 7 | # mkdir build && cd build |
| 8 | # cmake -G Ninja -DVLLM_PYTHON_EXECUTABLE=`which python3` -DCMAKE_INSTALL_PREFIX=.. .. |
| 9 | # cmake --build . --target install |
| 10 | # |
| 11 | # If you want to only build one target, make sure to install it manually: |
| 12 | # cmake --build . --target _C |
| 13 | # cmake --install . --component _C |
| 14 | project(vllm_extensions LANGUAGES CXX) |
| 15 | |
| 16 | set(CMAKE_CXX_STANDARD 20) |
| 17 | set(CMAKE_CXX_STANDARD_REQUIRED ON) |
| 18 | set(CMAKE_CUDA_STANDARD 20) |
| 19 | set(CMAKE_CUDA_STANDARD_REQUIRED ON) |
| 20 | set(CMAKE_HIP_STANDARD 20) |
| 21 | set(CMAKE_HIP_STANDARD_REQUIRED ON) |
| 22 | |
| 23 | # PyTorch headers require C++20; GCC < 11.3 has incomplete C++20 support. |
| 24 | if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "11.3") |
| 25 | message(FATAL_ERROR |
| 26 | "GCC >= 11.3 is required to build vLLM (found ${CMAKE_CXX_COMPILER_VERSION}). " |
| 27 | "PyTorch's C++20 headers require a compiler with full C++20 support. " |
| 28 | "See: https://github.com/pytorch/pytorch/pull/167929") |
| 29 | endif() |
| 30 | |
| 31 | |
| 32 | # CUDA by default, can be overridden by using -DVLLM_TARGET_DEVICE=... (used by setup.py) |
| 33 | set(VLLM_TARGET_DEVICE "cuda" CACHE STRING "Target device backend for vLLM") |
| 34 | message(STATUS "Build type: ${CMAKE_BUILD_TYPE}") |
| 35 | message(STATUS "Target device: ${VLLM_TARGET_DEVICE}") |
| 36 | |
| 37 | include(${CMAKE_CURRENT_LIST_DIR}/cmake/utils.cmake) |
| 38 | |
| 39 | # Suppress potential warnings about unused manually-specified variables |
| 40 | set(ignoreMe "${VLLM_PYTHON_PATH}") |
| 41 | |
| 42 | # Prevent installation of dependencies (cutlass) by default. |
| 43 | install(CODE "set(CMAKE_INSTALL_LOCAL_ONLY TRUE)" ALL_COMPONENTS) |
| 44 | |
| 45 | # |
| 46 | # Supported python versions. These versions will be searched in order, the |
| 47 | # first match will be selected. These should be kept in sync with setup.py. |
| 48 | # |
| 49 | set(PYTHON_SUPPORTED_VERSIONS "3.10" "3.11" "3.12" "3.13" "3.14") |
| 50 | |
| 51 | # Supported AMD GPU architectures. |
| 52 | set(HIP_SUPPORTED_ARCHS "gfx906;gfx908;gfx90a;gfx942;gfx950;gfx1250;gfx1030;gfx1100;gfx1101;gfx1102;gfx1103;gfx1150;gfx1151;gfx1152;gfx1153;gfx1200;gfx1201") |
| 53 | |
| 54 | # ROCm installation prefix. Default to /opt/rocm but allow override via |
| 55 | # -DROCM_PATH=/your/rocm/path when invoking cmake. |
| 56 | if(NOT DEFINED ROCM_PATH) |
| 57 | set(ROCM_PATH "/opt/rocm" CACHE PATH "ROCm installation prefix") |
| 58 | else() |
| 59 | set(ROCM_PATH ${ROCM_PATH} CACHE PATH "ROCm installation prefix" FORCE) |
| 60 | endif() |
| 61 | # |
| 62 | # Supported/expected torch versions for CUDA/ROCm. |
| 63 | # |
| 64 | # Currently, having an incorrect pytorch version results in a warning |
| 65 | # rather than an error. |
| 66 | # |
| 67 | # Note: the CUDA torch version is derived from pyproject.toml and various |
| 68 | # requirements.txt files and should be kept consistent. The ROCm torch |
| 69 | # versions are derived from docker/Dockerfile.rocm |
| 70 | # |
| 71 | set(TORCH_SUPPORTED_VERSION_CUDA "2.13.0") |
| 72 | set(TORCH_SUPPORTED_VERSION_ROCM "2.13.0") |
| 73 | # TORCH_NIGHTLY=1 builds run against unpinned nightly wheels, so the supported- |
| 74 | # version check would always warn. Only treat it as a nightly build when the |
| 75 | # value is exactly "1" (the bootstrap exports TORCH_NIGHTLY=0 by default, which |
| 76 | # must NOT suppress the warning for normal builds). |
| 77 | if (DEFINED ENV{TORCH_NIGHTLY} AND "$ENV{TORCH_NIGHTLY}" STREQUAL "1") |
| 78 | set(TORCH_NIGHTLY_BUILD TRUE) |
| 79 | else() |
| 80 | set(TORCH_NIGHTLY_BUILD FALSE) |
| 81 | endif() |
| 82 | |
| 83 | # |
| 84 | # Try to find python package with an executable that exactly matches |
| 85 | # `VLLM_PYTHON_EXECUTABLE` and is one of the supported versions. |
| 86 | # |
| 87 | if (VLLM_PYTHON_EXECUTABLE) |
| 88 | find_python_from_executable(${VLLM_PYTHON_EXECUTABLE} "${PYTHON_SUPPORTED_VERSIONS}") |
| 89 | else() |
| 90 | message(FATAL_ERROR |
| 91 | "Please set VLLM_PYTHON_EXECUTABLE to the path of the desired python version" |
| 92 | " before running cmake configure.") |
| 93 | endif() |
| 94 | |
| 95 | # |
| 96 | # Update cmake's `CMAKE_PREFIX_PATH` with torch location. |
| 97 | # |
| 98 | append_cmake_prefix_path("torch" "torch.utils.cmake_prefix_path") |
| 99 | |
| 100 | # Ensure the 'nvcc' command is in the PATH |
| 101 | find_program(NVCC_EXECUTABLE nvcc) |
| 102 | if (CUDA_FOUND AND NOT NVCC_EXECUTABLE) |
| 103 | message(FATAL_ERROR "nvcc not found") |
| 104 | endif() |
| 105 | |
| 106 | # |
| 107 | # Import torch cmake configuration. |
| 108 | # Torch also imports CUDA (and partially HIP) languages with some customizations, |
| 109 | # so there is no need to do this explicitly with check_language/enable_language, |
| 110 | # etc. |
| 111 | # |
| 112 | find_package(Torch REQUIRED) |
| 113 | |
| 114 | # Supported NVIDIA architectures. |
| 115 | # This check must happen after find_package(Torch) because that's when CMAKE_CUDA_COMPILER_VERSION gets defined |
| 116 | if(DEFINED CMAKE_CUDA_COMPILER_VERSION AND |
| 117 | CMAKE_CUDA_COMPILER_VERSION VERSION_GREATER_EQUAL 13.4) |
| 118 | # Rubin (10.7) can run SM100 family code, but CUDA 13.4 also supports |
| 119 | # targeting it directly. |
| 120 | set(CUDA_SUPPORTED_ARCHS "7.5;8.0;8.6;8.7;8.9;9.0;10.0;10.7;11.0;12.0") |
| 121 | elseif(DEFINED CMAKE_CUDA_COMPILER_VERSION AND |
| 122 | CMAKE_CUDA_COMPILER_VERSION VERSION_GREATER_EQUAL 13.0) |
| 123 | # starting from CUDA 12.9 and Blackwell (10.0), we use family-specific targets (10.0f, 12.0f, etc) |
| 124 | # to support the whole generation without specifying all sub-architectures |
| 125 | # see: https://developer.nvidia.com/blog/nvidia-blackwell-and-nvidia-cuda-12-9-introduce-family-specific-architecture-features/ |
| 126 | set(CUDA_SUPPORTED_ARCHS "7.5;8.0;8.6;8.7;8.9;9.0;10.0;11.0;12.0") |
| 127 | elseif(DEFINED CMAKE_CUDA_COMPILER_VERSION AND |
| 128 | CMAKE_CUDA_COMPILER_VERSION VERSION_GREATER_EQUAL 12.8) |
| 129 | set(CUDA_SUPPORTED_ARCHS "7.5;8.0;8.6;8.7;8.9;9.0;10.0;10.1;10.3;12.0;12.1") |
| 130 | else() |
| 131 | set(CUDA_SUPPORTED_ARCHS "7.0;7.5;8.0;8.6;8.7;8.9;9.0") |
| 132 | endif() |
| 133 | |
| 134 | # |
| 135 | # spinloop extension (pure CXX; must stay above the non-CUDA device branch so |
| 136 | # CPU builds define the target before the early return) |
| 137 | # This extension requires SABI 3.11 since it relies on Py_buffer support. Loading |
| 138 | # failure is handled gracefully on vLLM side for lower Python versions. |
| 139 | # Skip the target entirely on Python < 3.11 so the build doesn't break. |
| 140 | # |
| 141 | if(Python_VERSION VERSION_GREATER_EQUAL "3.11") |
| 142 | set(VLLM_SPINLOOP_EXT_SRC "csrc/spinloop.cpp") |
| 143 | set(SPINLOOP_COMPILE_FLAGS "") |
| 144 | if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|amd64") |
| 145 | list(APPEND SPINLOOP_COMPILE_FLAGS "-mmwaitx") |
| 146 | endif() |
| 147 | define_extension_target( |
| 148 | spinloop |
| 149 | DESTINATION vllm |
| 150 | LANGUAGE CXX |
| 151 | SOURCES ${VLLM_SPINLOOP_EXT_SRC} |
| 152 | COMPILE_FLAGS ${SPINLOOP_COMPILE_FLAGS} |
| 153 | USE_SABI 3.11 |
| 154 | WITH_SOABI) |
| 155 | endif() |
| 156 | |
| 157 | # |
| 158 | # fs_io extension (pure CXX; must stay above the non-CUDA device branch |
| 159 | # so CPU builds define the target before the early return). |
| 160 | # GIL-releasing filesystem helpers for FileSystemTierManager. |
| 161 | # |
| 162 | if(Python_VERSION VERSION_GREATER_EQUAL "3.11") |
| 163 | define_extension_target( |
| 164 | fs_io_C |
| 165 | DESTINATION vllm |
| 166 | LANGUAGE CXX |
| 167 | SOURCES csrc/fs_io.cpp |
| 168 | USE_SABI 3.11 |
| 169 | WITH_SOABI) |
| 170 | endif() |
| 171 | |
| 172 | # |
| 173 | # Forward the non-CUDA device extensions to external CMake scripts. |
| 174 | # |
| 175 | if (NOT VLLM_TARGET_DEVICE STREQUAL "cuda" AND |
| 176 | NOT VLLM_TARGET_DEVICE STREQUAL "rocm") |
| 177 | if (VLLM_TARGET_DEVICE STREQUAL "cpu") |
| 178 | include(${CMAKE_CURRENT_LIST_DIR}/cmake/cpu_extension.cmake) |
| 179 | else() |
| 180 | return() |
| 181 | endif() |
| 182 | return() |
| 183 | endif() |
| 184 | |
| 185 | # |
| 186 | # Set up GPU language and check the torch version and warn if it isn't |
| 187 | # what is expected. |
| 188 | # |
| 189 | if (NOT HIP_FOUND AND NOT PYTORCH_FOUND_HIP AND CUDA_FOUND) |
| 190 | set(VLLM_GPU_LANG "CUDA") |
| 191 | |
| 192 | if (NOT TORCH_NIGHTLY_BUILD AND NOT Torch_VERSION VERSION_EQUAL ${TORCH_SUPPORTED_VERSION_CUDA}) |
| 193 | message(WARNING "Pytorch version ${TORCH_SUPPORTED_VERSION_CUDA} " |
| 194 | "expected for CUDA build, saw ${Torch_VERSION} instead.") |
| 195 | endif() |
| 196 | elseif(HIP_FOUND OR PYTORCH_FOUND_HIP) |
| 197 | set(VLLM_GPU_LANG "HIP") |
| 198 | |
| 199 | # Importing torch recognizes and sets up some HIP/ROCm configuration but does |
| 200 | # not let cmake recognize .hip files. In order to get cmake to understand the |
| 201 | # .hip extension automatically, HIP must be enabled explicitly. |
| 202 | enable_language(HIP) |
| 203 | |
| 204 | # ROCm 5.X and 6.X |
| 205 | if (NOT TORCH_NIGHTLY_BUILD AND ROCM_VERSION_DEV_MAJOR GREATER_EQUAL 5 AND |
| 206 | Torch_VERSION VERSION_LESS ${TORCH_SUPPORTED_VERSION_ROCM}) |
| 207 | message(WARNING "Pytorch version >= ${TORCH_SUPPORTED_VERSION_ROCM} " |
| 208 | "expected for ROCm build, saw ${Torch_VERSION} instead.") |
| 209 | endif() |
| 210 | else() |
| 211 | message(FATAL_ERROR "Can't find CUDA or HIP installation.") |
| 212 | endif() |
| 213 | |
| 214 | |
| 215 | if(VLLM_GPU_LANG STREQUAL "CUDA") |
| 216 | # |
| 217 | # For cuda we want to be able to control which architectures we compile for on |
| 218 | # a per-file basis in order to cut down on compile time. So here we extract |
| 219 | # the set of architectures we want to compile for and remove the from the |
| 220 | # CMAKE_CUDA_FLAGS so that they are not applied globally. |
| 221 | # |
| 222 | # `+PTX` in TORCH_CUDA_ARCH_LIST is not preserved here. If a kernel really |
| 223 | # needs PTX, add `+PTX` to that kernel's component-specific arch list below. |
| 224 | # |
| 225 | clear_cuda_gencode_flags(CUDA_ARCH_FLAGS) |
| 226 | warn_if_ptx_arch_requested("${CUDA_ARCH_FLAGS}") |
| 227 | extract_unique_cuda_archs_ascending(CUDA_ARCHS "${CUDA_ARCH_FLAGS}") |
| 228 | message(STATUS "CUDA target architectures: ${CUDA_ARCHS}") |
| 229 | # Filter the target architectures by the supported supported archs |
| 230 | # since for some files we will build for all CUDA_ARCHS. |
| 231 | cuda_archs_loose_intersection(CUDA_ARCHS |
| 232 | "${CUDA_SUPPORTED_ARCHS}" "${CUDA_ARCHS}") |
| 233 | message(STATUS "CUDA supported target architectures: ${CUDA_ARCHS}") |
| 234 | if(NOT CUDA_ARCHS) |
| 235 | message(FATAL_ERROR |
| 236 | "No supported CUDA architectures; the build would produce a binary " |
| 237 | "with no usable kernels. Detected gencode flags: ${CUDA_ARCH_FLAGS}; " |
| 238 | "supported: ${CUDA_SUPPORTED_ARCHS}. " |
| 239 | "Set TORCH_CUDA_ARCH_LIST for your GPU (e.g. 12.0).") |
| 240 | endif() |
| 241 | else() |
| 242 | # |
| 243 | # For other GPU targets override the GPU architectures detected by cmake/torch |
| 244 | # and filter them by the supported versions for the current language. |
| 245 | # The final set of arches is stored in `VLLM_GPU_ARCHES`. |
| 246 | # |
| 247 | override_gpu_arches(VLLM_GPU_ARCHES |
| 248 | ${VLLM_GPU_LANG} |
| 249 | "${${VLLM_GPU_LANG}_SUPPORTED_ARCHS}") |
| 250 | endif() |
| 251 | |
| 252 | # |
| 253 | # Query torch for additional GPU compilation flags for the given |
| 254 | # `VLLM_GPU_LANG`. |
| 255 | # The final set of arches is stored in `VLLM_GPU_FLAGS`. |
| 256 | # |
| 257 | get_torch_gpu_compiler_flags(VLLM_GPU_FLAGS ${VLLM_GPU_LANG}) |
| 258 | |
| 259 | # |
| 260 | # Set nvcc parallelism. |
| 261 | # |
| 262 | if(NVCC_THREADS AND VLLM_GPU_LANG STREQUAL "CUDA") |
| 263 | list(APPEND VLLM_GPU_FLAGS "--threads=${NVCC_THREADS}") |
| 264 | endif() |
| 265 | |
| 266 | # |
| 267 | # Set compression mode for CUDA >=13.x. |
| 268 | # |
| 269 | if(VLLM_GPU_LANG STREQUAL "CUDA" AND |
| 270 | DEFINED CMAKE_CUDA_COMPILER_VERSION AND |
| 271 | CMAKE_CUDA_COMPILER_VERSION VERSION_GREATER_EQUAL 13.0) |
| 272 | list(APPEND VLLM_GPU_FLAGS "--compress-mode=size") |
| 273 | endif() |
| 274 | |
| 275 | # |
| 276 | # Set CUDA include flags for CXX compiler. |
| 277 | # |
| 278 | if(VLLM_GPU_LANG STREQUAL "CUDA") |
| 279 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I${CUDA_TOOLKIT_ROOT_DIR}/include") |
| 280 | if(CUDA_VERSION VERSION_GREATER_EQUAL 13.0) |
| 281 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I${CUDA_TOOLKIT_ROOT_DIR}/include/cccl") |
| 282 | endif() |
| 283 | endif() |
| 284 | |
| 285 | # |
| 286 | # Use FetchContent for C++ dependencies that are compiled as part of vLLM's build process. |
| 287 | # setup.py will override FETCHCONTENT_BASE_DIR to play nicely with sccache. |
| 288 | # Each dependency that produces build artifacts should override its BINARY_DIR to avoid |
| 289 | # conflicts between build types. It should instead be set to ${CMAKE_BINARY_DIR}/<dependency>. |
| 290 | # |
| 291 | include(FetchContent) |
| 292 | file(MAKE_DIRECTORY ${FETCHCONTENT_BASE_DIR}) # Ensure the directory exists |
| 293 | message(STATUS "FetchContent base directory: ${FETCHCONTENT_BASE_DIR}") |
| 294 | |
| 295 | if(VLLM_GPU_LANG STREQUAL "HIP") |
| 296 | # |
| 297 | # Overriding the default -O set up by cmake, adding ggdb3 for the most verbose devug info |
| 298 | # |
| 299 | set(CMAKE_${VLLM_GPU_LANG}_FLAGS_DEBUG "${CMAKE_${VLLM_GPU_LANG}_FLAGS_DEBUG} -O0 -ggdb3") |
| 300 | set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -ggdb3") |
| 301 | |
| 302 | # |
| 303 | # Certain HIP functions are marked as [[nodiscard]], yet vllm ignores the result which generates |
| 304 | # a lot of warnings that always mask real issues. Suppressing until this is properly addressed. |
| 305 | # |
| 306 | set(CMAKE_${VLLM_GPU_LANG}_FLAGS "${CMAKE_${VLLM_GPU_LANG}_FLAGS} -Wno-unused-result -Wno-unused-value") |
| 307 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-result -Wno-unused-value") |
| 308 | |
| 309 | # When using LTO then *.cpp files must be compiled with same compiler as used linker |
| 310 | # So if HIP uses clang linker we also must use it |
| 311 | # Otherwise symbols will be missing from .so |
| 312 | if (CMAKE_CXX_FLAGS MATCHES "\-flto") |
| 313 | if(NOT CMAKE_CXX_COMPILER_ID STREQUAL CMAKE_HIP_COMPILER_ID) |
| 314 | message(FATAL_ERROR "LTO is enabled for ROCm build, but the C++ compiler (${CMAKE_CXX_COMPILER_ID}) and HIP compiler (${CMAKE_HIP_COMPILER_ID}) are different which is not supported. " |
| 315 | "Please ensure they are same by setting CXX=${CMAKE_HIP_COMPILER} environment variable. Or alternatively disable LTO.") |
| 316 | endif() |
| 317 | endif() |
| 318 | endif() |
| 319 | |
| 320 | # |
| 321 | # Define other extension targets |
| 322 | # |
| 323 | |
| 324 | # |
| 325 | # cumem_allocator extension |
| 326 | # |
| 327 | |
| 328 | set(VLLM_CUMEM_EXT_SRC |
| 329 | "csrc/cumem_allocator.cpp") |
| 330 | |
| 331 | set_gencode_flags_for_srcs( |
| 332 | SRCS "${VLLM_CUMEM_EXT_SRC}" |
| 333 | CUDA_ARCHS "${CUDA_ARCHS}") |
| 334 | |
| 335 | if(VLLM_GPU_LANG STREQUAL "CUDA" OR VLLM_GPU_LANG STREQUAL "HIP") |
| 336 | message(STATUS "Enabling cumem allocator extension.") |
| 337 | if(VLLM_GPU_LANG STREQUAL "CUDA") |
| 338 | # link against cuda driver library |
| 339 | list(APPEND CUMEM_LIBS CUDA::cuda_driver) |
| 340 | else() |
| 341 | # link against rocm driver library. Prefer an absolute path to |
| 342 | # libamdhip64.so inside ${ROCM_PATH}/lib if available, otherwise fall |
| 343 | # back to linking by name "amdhip64". |
| 344 | find_library(AMDHIP64_LIB |
| 345 | NAMES amdhip64 libamdhip64.so |
| 346 | PATHS ${ROCM_PATH}/lib |
| 347 | NO_DEFAULT_PATH) |
| 348 | if(AMDHIP64_LIB) |
| 349 | message(STATUS "Found libamdhip64 at ${AMDHIP64_LIB}") |
| 350 | list(APPEND CUMEM_LIBS ${AMDHIP64_LIB}) |
| 351 | else() |
| 352 | message(WARNING "libamdhip64 not found in ${ROCM_PATH}/lib; falling back to linking 'amdhip64' by name") |
| 353 | list(APPEND CUMEM_LIBS amdhip64) |
| 354 | endif() |
| 355 | endif() |
| 356 | define_extension_target( |
| 357 | cumem_allocator |
| 358 | DESTINATION vllm |
| 359 | LANGUAGE CXX |
| 360 | SOURCES ${VLLM_CUMEM_EXT_SRC} |
| 361 | LIBRARIES ${CUMEM_LIBS} |
| 362 | USE_SABI 3.8 |
| 363 | WITH_SOABI) |
| 364 | endif() |
| 365 | |
| 366 | # |
| 367 | # Legacy _C extension (ROCm only — CUDA ops migrated to _C_stable_libtorch) |
| 368 | # |
| 369 | |
| 370 | if(VLLM_GPU_LANG STREQUAL "HIP") |
| 371 | set(VLLM_EXT_SRC |
| 372 | "csrc/torch_bindings.cpp" |
| 373 | "csrc/custom_quickreduce.cu") |
| 374 | |
| 375 | message(STATUS "Enabling C extension.") |
| 376 | define_extension_target( |
| 377 | _C |
| 378 | DESTINATION vllm |
| 379 | LANGUAGE ${VLLM_GPU_LANG} |
| 380 | SOURCES ${VLLM_EXT_SRC} |
| 381 | COMPILE_FLAGS ${VLLM_GPU_FLAGS} |
| 382 | ARCHITECTURES ${VLLM_GPU_ARCHES} |
| 383 | INCLUDE_DIRECTORIES ${CUTLASS_INCLUDE_DIR} |
| 384 | INCLUDE_DIRECTORIES ${CUTLASS_TOOLS_UTIL_INCLUDE_DIR} |
| 385 | USE_SABI 3 |
| 386 | WITH_SOABI) |
| 387 | |
| 388 | # If CUTLASS is compiled on NVCC >= 12.5, it by default uses |
| 389 | # cudaGetDriverEntryPointByVersion as a wrapper to avoid directly calling the |
| 390 | # driver API. This causes problems when linking with earlier versions of CUDA. |
| 391 | # Setting this variable sidesteps the issue by calling the driver directly. |
| 392 | target_compile_definitions(_C PRIVATE CUTLASS_ENABLE_DIRECT_CUDA_DRIVER_CALL=1) |
| 393 | endif() # _C HIP endif |
| 394 | |
| 395 | if(VLLM_GPU_LANG STREQUAL "CUDA" OR VLLM_GPU_LANG STREQUAL "HIP") |
| 396 | # |
| 397 | # _C_stable_libtorch extension (ops registered via STABLE_TORCH_LIBRARY) |
| 398 | # |
| 399 | if(Torch_VERSION VERSION_GREATER_EQUAL "2.13" AND |
| 400 | Torch_VERSION VERSION_LESS "2.14") |
| 401 | # hotfix for https://github.com/pytorch/pytorch/pull/190493 |
| 402 | # delete after we bump to 2.14 |
| 403 | list(GET TORCH_INCLUDE_DIRS 0 TORCH_INCLUDE_DIR) |
| 404 | set(_stable_hdr_rel "torch/csrc/stable/stableivalue_conversions.h") |
| 405 | file(READ "${TORCH_INCLUDE_DIR}/${_stable_hdr_rel}" |
| 406 | TORCH_STABLE_STRING_CONTENTS) |
| 407 | |
| 408 | if(TORCH_STABLE_STRING_CONTENTS MATCHES "auto strptr = new std::string") |
| 409 | set(TORCH_SHADOW_INCLUDE_DIR |
| 410 | "${CMAKE_CURRENT_BINARY_DIR}/torch_patched_headers") |
| 411 | |
| 412 | file(MAKE_DIRECTORY "${TORCH_SHADOW_INCLUDE_DIR}/torch/csrc/stable") |
| 413 | file(COPY_FILE |
| 414 | "${TORCH_INCLUDE_DIR}/${_stable_hdr_rel}" |
| 415 | "${TORCH_SHADOW_INCLUDE_DIR}/${_stable_hdr_rel}") |
| 416 | |
| 417 | execute_process( |
| 418 | COMMAND patch --batch --forward -p1 |
| 419 | "--input=${CMAKE_CURRENT_LIST_DIR}/cmake/patches/pytorch_stable_string.patch" |
| 420 | WORKING_DIRECTORY "${TORCH_SHADOW_INCLUDE_DIR}" |
| 421 | COMMAND_ERROR_IS_FATAL ANY) |
| 422 | |
| 423 | include_directories(BEFORE "${TORCH_SHADOW_INCLUDE_DIR}") |
| 424 | endif() |
| 425 | endif() |
| 426 | |
| 427 | set(VLLM_STABLE_EXT_SRC |
| 428 | "csrc/libtorch_stable/torch_bindings.cpp" |
| 429 | "csrc/libtorch_stable/cuda_view.cu" |
| 430 | "csrc/libtorch_stable/cuda_utils_kernels.cu" |
| 431 | "csrc/libtorch_stable/activation_kernels.cu" |
| 432 | "csrc/libtorch_stable/ngram_embedding_kernels.cu" |
| 433 | "csrc/libtorch_stable/vocab_parallel_embedding_kernels.cu" |
| 434 | "csrc/libtorch_stable/quantization/activation_kernels.cu" |
| 435 | "csrc/libtorch_stable/quantization/w8a8/int8/scaled_quant.cu" |
| 436 | "csrc/libtorch_stable/quantization/w8a8/fp8/common.cu" |
| 437 | "csrc/libtorch_stable/quantization/w8a8/fp8/per_token_group_quant.cu" |
| 438 | "csrc/libtorch_stable/quantization/w8a8/int8/per_token_group_quant.cu" |
| 439 | "csrc/libtorch_stable/quantization/gptq/q_gemm.cu" |
| 440 | "csrc/libtorch_stable/pos_encoding_kernels.cu" |
| 441 | "csrc/libtorch_stable/fused_qknorm_rope_kernel.cu" |
| 442 | "csrc/libtorch_stable/fused_minimax_m3_qknorm_rope_kv_insert_kernel.cu" |
| 443 | "csrc/libtorch_stable/layernorm_kernels.cu" |
| 444 | "csrc/libtorch_stable/layernorm_quant_kernels.cu" |
| 445 | "csrc/libtorch_stable/quantization/fused_kernels/fused_layernorm_dynamic_per_token_quant.cu" |
| 446 | "csrc/libtorch_stable/quantization/fused_kernels/fused_silu_mul_block_quant.cu" |
| 447 | "csrc/libtorch_stable/attention/merge_attn_states.cu" |
| 448 | "csrc/libtorch_stable/sampler.cu" |
| 449 | "csrc/libtorch_stable/topk.cu" |
| 450 | "csrc/libtorch_stable/mamba/selective_scan_fwd.cu" |
| 451 | "csrc/libtorch_stable/cache_kernels.cu" |
| 452 | "csrc/libtorch_stable/cache_kernels_fused.cu" |
| 453 | "csrc/libtorch_stable/custom_all_gather_reduce_scatter.cu" |
| 454 | "csrc/libtorch_stable/custom_all_gather_reduce_scatter_ops.cpp" |
| 455 | "csrc/libtorch_stable/custom_all_reduce.cu" |
| 456 | "csrc/libtorch_stable/fused_deepseek_v4_qnorm_rope_kv_insert_kernel.cu" |
| 457 | "csrc/libtorch_stable/fused_kimi_k3_mla_key_concat_kv_cache_kernel.cu") |
| 458 | |
| 459 | if(VLLM_GPU_LANG STREQUAL "CUDA" AND |
| 460 | DEFINED CMAKE_CUDA_COMPILER_VERSION AND |
| 461 | CMAKE_CUDA_COMPILER_VERSION VERSION_GREATER_EQUAL 12.0) |
| 462 | |
| 463 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 13.0) |
| 464 | cuda_archs_loose_intersection(COOPERATIVE_TOPK_ARCHS |
| 465 | "9.0a;10.0f;10.1f;10.3f;10.7f;11.0f;12.0f;12.1f" "${CUDA_ARCHS}") |
| 466 | else() |
| 467 | cuda_archs_loose_intersection(COOPERATIVE_TOPK_ARCHS |
| 468 | "9.0a;10.0a;10.1a;10.3a;12.0a;12.1a" "${CUDA_ARCHS}") |
| 469 | endif() |
| 470 | |
| 471 | if(COOPERATIVE_TOPK_ARCHS) |
| 472 | list(APPEND VLLM_GPU_FLAGS "-DVLLM_ENABLE_COOPERATIVE_TOPK=1") |
| 473 | |
| 474 | endif() |
| 475 | endif() |
| 476 | |
| 477 | if(VLLM_GPU_LANG STREQUAL "CUDA") |
| 478 | list(APPEND VLLM_STABLE_EXT_SRC |
| 479 | "csrc/libtorch_stable/hisparse_kernels.cu" |
| 480 | "csrc/libtorch_stable/attention/dcp_utils/dcp_direct_a2a_lse_reduce.cu" |
| 481 | "csrc/libtorch_stable/attention/dcp_utils/dcp_direct_kv_gather.cu" |
| 482 | "csrc/libtorch_stable/attention/dcp_utils/dcp_direct_q_gather.cu") |
| 483 | |
| 484 | SET(CUTLASS_ENABLE_HEADERS_ONLY ON CACHE BOOL "Enable only the header library") |
| 485 | |
| 486 | # Set CUTLASS_REVISION. Used for FetchContent. Also fixes some bogus messages when building. |
| 487 | set(CUTLASS_REVISION "v4.7.1") |
| 488 | |
| 489 | # Use the specified CUTLASS source directory for compilation if VLLM_CUTLASS_SRC_DIR is provided |
| 490 | if (DEFINED ENV{VLLM_CUTLASS_SRC_DIR}) |
| 491 | set(VLLM_CUTLASS_SRC_DIR $ENV{VLLM_CUTLASS_SRC_DIR}) |
| 492 | endif() |
| 493 | |
| 494 | if(VLLM_CUTLASS_SRC_DIR) |
| 495 | if(NOT IS_ABSOLUTE VLLM_CUTLASS_SRC_DIR) |
| 496 | get_filename_component(VLLM_CUTLASS_SRC_DIR "${VLLM_CUTLASS_SRC_DIR}" ABSOLUTE) |
| 497 | endif() |
| 498 | message(STATUS "The VLLM_CUTLASS_SRC_DIR is set, using ${VLLM_CUTLASS_SRC_DIR} for compilation") |
| 499 | FetchContent_Declare(cutlass SOURCE_DIR ${VLLM_CUTLASS_SRC_DIR}) |
| 500 | else() |
| 501 | FetchContent_Declare( |
| 502 | cutlass |
| 503 | GIT_REPOSITORY https://github.com/nvidia/cutlass.git |
| 504 | # Please keep this in sync with CUTLASS_REVISION line above. |
| 505 | GIT_TAG ${CUTLASS_REVISION} |
| 506 | GIT_PROGRESS TRUE |
| 507 | |
| 508 | # Speed up CUTLASS download by retrieving only the specified GIT_TAG instead of the history. |
| 509 | # Important: If GIT_SHALLOW is enabled then GIT_TAG works only with branch names and tags. |
| 510 | # So if the GIT_TAG above is updated to a commit hash, GIT_SHALLOW must be set to FALSE |
| 511 | GIT_SHALLOW TRUE |
| 512 | ) |
| 513 | endif() |
| 514 | FetchContent_MakeAvailable(cutlass) |
| 515 | |
| 516 | list(APPEND VLLM_STABLE_EXT_SRC |
| 517 | "csrc/libtorch_stable/cutlass_extensions/common.cpp" |
| 518 | "csrc/libtorch_stable/quantization/w8a8/cutlass/scaled_mm_entry.cu" |
| 519 | "csrc/libtorch_stable/quantization/fp4/nvfp4_quant_entry.cu" |
| 520 | "csrc/libtorch_stable/quantization/fp4/nvfp4_scaled_mm_entry.cu" |
| 521 | "csrc/libtorch_stable/quantization/awq/gemm_kernels.cu" |
| 522 | "csrc/libtorch_stable/minimax_reduce_rms_kernel.cu") |
| 523 | |
| 524 | # |
| 525 | # Machete kernels |
| 526 | # |
| 527 | # The machete kernels only work on hopper and require CUDA 12.0 or later. |
| 528 | # Only build Machete kernels if we are building for something compatible with sm90a |
| 529 | cuda_archs_loose_intersection(MACHETE_ARCHS "9.0a" "${CUDA_ARCHS}") |
| 530 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 12.0 AND MACHETE_ARCHS) |
| 531 | # |
| 532 | # For the Machete kernels we automatically generate sources for various |
| 533 | # preselected input type pairs and schedules. |
| 534 | # Generate sources: |
| 535 | set(MACHETE_GEN_SCRIPT |
| 536 | ${CMAKE_CURRENT_SOURCE_DIR}/csrc/libtorch_stable/quantization/machete/generate.py) |
| 537 | file(MD5 ${MACHETE_GEN_SCRIPT} MACHETE_GEN_SCRIPT_HASH) |
| 538 | |
| 539 | message(STATUS "Machete generation script hash: ${MACHETE_GEN_SCRIPT_HASH}") |
| 540 | message(STATUS "Last run machete generate script hash: $CACHE{MACHETE_GEN_SCRIPT_HASH}") |
| 541 | |
| 542 | if (NOT DEFINED CACHE{MACHETE_GEN_SCRIPT_HASH} |
| 543 | OR NOT $CACHE{MACHETE_GEN_SCRIPT_HASH} STREQUAL ${MACHETE_GEN_SCRIPT_HASH}) |
| 544 | execute_process( |
| 545 | COMMAND ${CMAKE_COMMAND} -E env |
| 546 | PYTHONPATH=${CMAKE_CURRENT_SOURCE_DIR}/csrc/cutlass_extensions/:${CUTLASS_DIR}/python/:${VLLM_PYTHON_PATH}:$ENV{PYTHONPATH} |
| 547 | ${Python_EXECUTABLE} ${MACHETE_GEN_SCRIPT} |
| 548 | RESULT_VARIABLE machete_generation_result |
| 549 | OUTPUT_VARIABLE machete_generation_output |
| 550 | OUTPUT_FILE ${CMAKE_CURRENT_BINARY_DIR}/machete_generation.log |
| 551 | ERROR_FILE ${CMAKE_CURRENT_BINARY_DIR}/machete_generation.log |
| 552 | ) |
| 553 | |
| 554 | if (NOT machete_generation_result EQUAL 0) |
| 555 | message(FATAL_ERROR "Machete generation failed." |
| 556 | " Result: \"${machete_generation_result}\"" |
| 557 | "\nCheck the log for details: " |
| 558 | "${CMAKE_CURRENT_BINARY_DIR}/machete_generation.log") |
| 559 | else() |
| 560 | set(MACHETE_GEN_SCRIPT_HASH ${MACHETE_GEN_SCRIPT_HASH} |
| 561 | CACHE STRING "Last run machete generate script hash" FORCE) |
| 562 | message(STATUS "Machete generation completed successfully.") |
| 563 | endif() |
| 564 | else() |
| 565 | message(STATUS "Machete generation script has not changed, skipping generation.") |
| 566 | endif() |
| 567 | |
| 568 | # Add machete generated sources |
| 569 | file(GLOB MACHETE_GEN_SOURCES "csrc/libtorch_stable/quantization/machete/generated/*.cu") |
| 570 | list(APPEND VLLM_STABLE_EXT_SRC ${MACHETE_GEN_SOURCES}) |
| 571 | |
| 572 | # forward compatible |
| 573 | set_gencode_flags_for_srcs( |
| 574 | SRCS "${MACHETE_GEN_SOURCES}" |
| 575 | CUDA_ARCHS "${MACHETE_ARCHS}") |
| 576 | |
| 577 | list(APPEND VLLM_STABLE_EXT_SRC |
| 578 | csrc/libtorch_stable/quantization/machete/machete_pytorch.cu) |
| 579 | message(STATUS "Building Machete kernels for archs: ${MACHETE_ARCHS}") |
| 580 | else() |
| 581 | if (NOT ${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 12.0 |
| 582 | AND MACHETE_ARCHS) |
| 583 | message(STATUS "Not building Machete kernels as CUDA Compiler version is " |
| 584 | "not >= 12.0, we recommend upgrading to CUDA 12.0 or " |
| 585 | "later if you intend on running w4a16 quantized models on " |
| 586 | "Hopper.") |
| 587 | else() |
| 588 | message(STATUS "Not building Machete kernels as no compatible archs " |
| 589 | "found in CUDA target architectures") |
| 590 | endif() |
| 591 | endif() |
| 592 | |
| 593 | # The fused MiniMax-M3 KV insert quantizes NVFP4 with SM100-family-only |
| 594 | # cvt instructions, so its SM10x/11x code is built for the family targets. |
| 595 | set(MINIMAX_M3_FUSED_SRC |
| 596 | "csrc/libtorch_stable/fused_minimax_m3_qknorm_rope_kv_insert_kernel.cu") |
| 597 | list(REMOVE_ITEM VLLM_STABLE_EXT_SRC "${MINIMAX_M3_FUSED_SRC}") |
| 598 | set_gencode_flags_for_srcs( |
| 599 | SRCS "${VLLM_STABLE_EXT_SRC}" |
| 600 | CUDA_ARCHS "${CUDA_ARCHS}") |
| 601 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 13.0) |
| 602 | cuda_archs_loose_intersection(MINIMAX_M3_FUSED_ARCHS |
| 603 | "10.0f;10.7f;11.0f" "${CUDA_ARCHS}") |
| 604 | else() |
| 605 | cuda_archs_loose_intersection(MINIMAX_M3_FUSED_ARCHS |
| 606 | "10.0a;10.1a;10.3a" "${CUDA_ARCHS}") |
| 607 | endif() |
| 608 | foreach(_ARCH ${CUDA_ARCHS}) |
| 609 | if(_ARCH VERSION_LESS 10.0 OR _ARCH VERSION_GREATER_EQUAL 12.0) |
| 610 | list(APPEND MINIMAX_M3_FUSED_ARCHS "${_ARCH}") |
| 611 | endif() |
| 612 | endforeach() |
| 613 | set_gencode_flags_for_srcs( |
| 614 | SRCS "${MINIMAX_M3_FUSED_SRC}" |
| 615 | CUDA_ARCHS "${MINIMAX_M3_FUSED_ARCHS}") |
| 616 | list(APPEND VLLM_STABLE_EXT_SRC "${MINIMAX_M3_FUSED_SRC}") |
| 617 | |
| 618 | if(COOPERATIVE_TOPK_ARCHS) |
| 619 | list(APPEND VLLM_STABLE_EXT_SRC |
| 620 | "csrc/libtorch_stable/cooperative_topk.cu") |
| 621 | set_gencode_flags_for_srcs( |
| 622 | SRCS "csrc/libtorch_stable/cooperative_topk.cu" |
| 623 | CUDA_ARCHS "${COOPERATIVE_TOPK_ARCHS}") |
| 624 | endif() |
| 625 | |
| 626 | # Only build Marlin kernels if we are building for at least some compatible archs. |
| 627 | # Keep building Marlin for 9.0 as there are some group sizes and shapes that |
| 628 | # are not supported by Machete yet. |
| 629 | |
| 630 | # marlin arches for fp16 output |
| 631 | # Family-conditional 12.0f (one cubin for SM12x family) requires CUDA >= 13.0; |
| 632 | # fall back to architecture-specific 12.0a;12.1a on CUDA < 13.0 (e.g. 12.8). |
| 633 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 13.0) |
| 634 | cuda_archs_loose_intersection(MARLIN_ARCHS "8.0+PTX;12.0f" "${CUDA_ARCHS}") |
| 635 | else() |
| 636 | cuda_archs_loose_intersection(MARLIN_ARCHS "8.0+PTX;12.0a;12.1a" "${CUDA_ARCHS}") |
| 637 | endif() |
| 638 | # marlin has limited support for turing |
| 639 | cuda_archs_loose_intersection(MARLIN_SM75_ARCHS "7.5" "${CUDA_ARCHS}") |
| 640 | # marlin arches for bf16 output (we need 9.0 for bf16 atomicAdd PTX) |
| 641 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 13.0) |
| 642 | cuda_archs_loose_intersection(MARLIN_BF16_ARCHS "8.0+PTX;9.0+PTX;12.0f" "${CUDA_ARCHS}") |
| 643 | else() |
| 644 | cuda_archs_loose_intersection(MARLIN_BF16_ARCHS "8.0+PTX;9.0+PTX;12.0a;12.1a" "${CUDA_ARCHS}") |
| 645 | endif() |
| 646 | # marlin arches for fp8 input |
| 647 | # - sm80 doesn't support fp8 computation |
| 648 | # - sm90 and sm100 don't support QMMA.16832.F32.E4M3.E4M3 SAAS instruction |
| 649 | # so we only enable fp8 computation for SM89 (e.g. RTX 40x0) and 12.0 (e.g. RTX 50x0) |
| 650 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 13.0) |
| 651 | cuda_archs_loose_intersection(MARLIN_FP8_ARCHS "8.9;12.0f" "${CUDA_ARCHS}") |
| 652 | else() |
| 653 | cuda_archs_loose_intersection(MARLIN_FP8_ARCHS "8.9;12.0a;12.1a" "${CUDA_ARCHS}") |
| 654 | endif() |
| 655 | # marlin arches for other files |
| 656 | cuda_archs_loose_intersection(MARLIN_OTHER_ARCHS "7.5;8.0+PTX" "${CUDA_ARCHS}") |
| 657 | |
| 658 | if (MARLIN_OTHER_ARCHS) |
| 659 | |
| 660 | # |
| 661 | # For the Marlin kernels we automatically generate sources for various |
| 662 | # preselected input type pairs and schedules. |
| 663 | # Generate sources: |
| 664 | set(MARLIN_GEN_SCRIPT |
| 665 | ${CMAKE_CURRENT_SOURCE_DIR}/csrc/libtorch_stable/quantization/marlin/generate_kernels.py) |
| 666 | file(MD5 ${MARLIN_GEN_SCRIPT} MARLIN_GEN_SCRIPT_HASH) |
| 667 | list(JOIN CUDA_ARCHS "," CUDA_ARCHS_STR) |
| 668 | set(MARLIN_GEN_SCRIPT_HASH_AND_ARCH "${MARLIN_GEN_SCRIPT_HASH}(ARCH:${CUDA_ARCHS_STR})") |
| 669 | |
| 670 | message(STATUS "Marlin generation script hash: ${MARLIN_GEN_SCRIPT_HASH_AND_ARCH}") |
| 671 | message(STATUS "Last run Marlin generate script hash: $CACHE{MARLIN_GEN_SCRIPT_HASH_AND_ARCH}") |
| 672 | |
| 673 | if (NOT DEFINED CACHE{MARLIN_GEN_SCRIPT_HASH_AND_ARCH} |
| 674 | OR NOT $CACHE{MARLIN_GEN_SCRIPT_HASH_AND_ARCH} STREQUAL ${MARLIN_GEN_SCRIPT_HASH_AND_ARCH}) |
| 675 | execute_process( |
| 676 | COMMAND ${CMAKE_COMMAND} -E env |
| 677 | PYTHONPATH=$ENV{PYTHONPATH} |
| 678 | ${Python_EXECUTABLE} ${MARLIN_GEN_SCRIPT} ${CUDA_ARCHS_STR} |
| 679 | RESULT_VARIABLE marlin_generation_result |
| 680 | OUTPUT_VARIABLE marlin_generation_result |
| 681 | OUTPUT_FILE ${CMAKE_CURRENT_BINARY_DIR}/marlin_generation.log |
| 682 | ERROR_FILE ${CMAKE_CURRENT_BINARY_DIR}/marlin_generation.log |
| 683 | ) |
| 684 | |
| 685 | if (NOT marlin_generation_result EQUAL 0) |
| 686 | message(FATAL_ERROR "Marlin generation failed." |
| 687 | " Result: \"${marlin_generation_result}\"" |
| 688 | "\nCheck the log for details: " |
| 689 | "${CMAKE_CURRENT_BINARY_DIR}/marlin_generation.log") |
| 690 | else() |
| 691 | set(MARLIN_GEN_SCRIPT_HASH_AND_ARCH ${MARLIN_GEN_SCRIPT_HASH_AND_ARCH} |
| 692 | CACHE STRING "Last run Marlin generate script hash and arch" FORCE) |
| 693 | message(STATUS "Marlin generation completed successfully.") |
| 694 | endif() |
| 695 | else() |
| 696 | message(STATUS "Marlin generation script has not changed, skipping generation.") |
| 697 | endif() |
| 698 | |
| 699 | if (MARLIN_ARCHS) |
| 700 | file(GLOB MARLIN_TEMPLATE_KERNEL_SRC "csrc/libtorch_stable/quantization/marlin/sm80_kernel_*_float16.cu") |
| 701 | set_gencode_flags_for_srcs( |
| 702 | SRCS "${MARLIN_TEMPLATE_KERNEL_SRC}" |
| 703 | CUDA_ARCHS "${MARLIN_ARCHS}") |
| 704 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 12.8) |
| 705 | set_source_files_properties(${MARLIN_TEMPLATE_KERNEL_SRC} |
| 706 | PROPERTIES COMPILE_FLAGS "-static-global-template-stub=false") |
| 707 | endif() |
| 708 | list(APPEND VLLM_STABLE_EXT_SRC ${MARLIN_TEMPLATE_KERNEL_SRC}) |
| 709 | |
| 710 | file(GLOB MARLIN_TEMPLATE_BF16_KERNEL_SRC "csrc/libtorch_stable/quantization/marlin/sm80_kernel_*_bfloat16.cu") |
| 711 | set_gencode_flags_for_srcs( |
| 712 | SRCS "${MARLIN_TEMPLATE_BF16_KERNEL_SRC}" |
| 713 | CUDA_ARCHS "${MARLIN_BF16_ARCHS}") |
| 714 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 12.8) |
| 715 | set_source_files_properties(${MARLIN_TEMPLATE_BF16_KERNEL_SRC} |
| 716 | PROPERTIES COMPILE_FLAGS "-static-global-template-stub=false") |
| 717 | endif() |
| 718 | list(APPEND VLLM_STABLE_EXT_SRC ${MARLIN_TEMPLATE_BF16_KERNEL_SRC}) |
| 719 | endif() |
| 720 | |
| 721 | if (MARLIN_SM75_ARCHS) |
| 722 | file(GLOB MARLIN_TEMPLATE_SM75_KERNEL_SRC "csrc/libtorch_stable/quantization/marlin/sm75_kernel_*.cu") |
| 723 | set_gencode_flags_for_srcs( |
| 724 | SRCS "${MARLIN_TEMPLATE_SM75_KERNEL_SRC}" |
| 725 | CUDA_ARCHS "${MARLIN_SM75_ARCHS}") |
| 726 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 12.8) |
| 727 | set_source_files_properties(${MARLIN_TEMPLATE_SM75_KERNEL_SRC} |
| 728 | PROPERTIES COMPILE_FLAGS "-static-global-template-stub=false") |
| 729 | endif() |
| 730 | list(APPEND VLLM_STABLE_EXT_SRC ${MARLIN_TEMPLATE_SM75_KERNEL_SRC}) |
| 731 | endif() |
| 732 | |
| 733 | if (MARLIN_FP8_ARCHS) |
| 734 | file(GLOB MARLIN_TEMPLATE_FP8_KERNEL_SRC "csrc/libtorch_stable/quantization/marlin/sm89_kernel_*.cu") |
| 735 | set_gencode_flags_for_srcs( |
| 736 | SRCS "${MARLIN_TEMPLATE_FP8_KERNEL_SRC}" |
| 737 | CUDA_ARCHS "${MARLIN_FP8_ARCHS}") |
| 738 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 12.8) |
| 739 | set_source_files_properties(${MARLIN_TEMPLATE_FP8_KERNEL_SRC} |
| 740 | PROPERTIES COMPILE_FLAGS "-static-global-template-stub=false") |
| 741 | endif() |
| 742 | list(APPEND VLLM_STABLE_EXT_SRC ${MARLIN_TEMPLATE_FP8_KERNEL_SRC}) |
| 743 | endif() |
| 744 | |
| 745 | set(MARLIN_SRCS |
| 746 | "csrc/libtorch_stable/quantization/marlin/marlin.cu" |
| 747 | "csrc/libtorch_stable/quantization/marlin/marlin_int4_fp8_preprocess.cu" |
| 748 | "csrc/libtorch_stable/quantization/marlin/gptq_marlin_repack.cu" |
| 749 | "csrc/libtorch_stable/quantization/marlin/awq_marlin_repack.cu") |
| 750 | set_gencode_flags_for_srcs( |
| 751 | SRCS "${MARLIN_SRCS}" |
| 752 | CUDA_ARCHS "${MARLIN_OTHER_ARCHS}") |
| 753 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 12.8) |
| 754 | set_source_files_properties(${MARLIN_SRCS} |
| 755 | PROPERTIES COMPILE_FLAGS "-static-global-template-stub=false") |
| 756 | endif() |
| 757 | list(APPEND VLLM_STABLE_EXT_SRC "${MARLIN_SRCS}") |
| 758 | |
| 759 | message(STATUS "Building Marlin kernels for archs: ${MARLIN_OTHER_ARCHS}") |
| 760 | else() |
| 761 | message(STATUS "Not building Marlin kernels as no compatible archs found" |
| 762 | " in CUDA target architectures") |
| 763 | endif() |
| 764 | |
| 765 | # DeepSeek V3 fused A GEMM kernel (requires SM 9.0+, Hopper and later) |
| 766 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 13.0) |
| 767 | cuda_archs_loose_intersection(DSV3_FUSED_A_GEMM_ARCHS "9.0a;10.0f;10.7f;11.0f;12.0f" "${CUDA_ARCHS}") |
| 768 | else() |
| 769 | cuda_archs_loose_intersection(DSV3_FUSED_A_GEMM_ARCHS "9.0a;10.0a;10.1a;10.3a;12.0a;12.1a" "${CUDA_ARCHS}") |
| 770 | endif() |
| 771 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 12.0 AND DSV3_FUSED_A_GEMM_ARCHS) |
| 772 | set(DSV3_FUSED_A_GEMM_SRCS "csrc/libtorch_stable/dsv3_fused_a_gemm.cu") |
| 773 | set_gencode_flags_for_srcs( |
| 774 | SRCS "${DSV3_FUSED_A_GEMM_SRCS}" |
| 775 | CUDA_ARCHS "${DSV3_FUSED_A_GEMM_ARCHS}") |
| 776 | list(APPEND VLLM_STABLE_EXT_SRC "${DSV3_FUSED_A_GEMM_SRCS}") |
| 777 | message(STATUS "Building dsv3_fused_a_gemm for archs: ${DSV3_FUSED_A_GEMM_ARCHS}") |
| 778 | else() |
| 779 | message(STATUS "Not building dsv3_fused_a_gemm as no compatible archs found " |
| 780 | "in CUDA target architectures.") |
| 781 | endif() |
| 782 | |
| 783 | # FP32 router GEMM (H=3072, E=256, M<=32). Requires SM90+ and CUDA >= 12.0. |
| 784 | cuda_archs_sm90plus(FP32_ROUTER_GEMM_ARCHS "${CUDA_ARCHS}") |
| 785 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 12.0 AND FP32_ROUTER_GEMM_ARCHS) |
| 786 | set(FP32_ROUTER_GEMM_SRCS |
| 787 | "csrc/libtorch_stable/fp32_router_gemm_entry.cu" |
| 788 | "csrc/libtorch_stable/fp32_router_gemm.cu") |
| 789 | set_gencode_flags_for_srcs( |
| 790 | SRCS "${FP32_ROUTER_GEMM_SRCS}" |
| 791 | CUDA_ARCHS "${FP32_ROUTER_GEMM_ARCHS}") |
| 792 | list(APPEND VLLM_STABLE_EXT_SRC "${FP32_ROUTER_GEMM_SRCS}") |
| 793 | message(STATUS "Building fp32_router_gemm for archs: ${FP32_ROUTER_GEMM_ARCHS}") |
| 794 | else() |
| 795 | message(STATUS "Not building fp32_router_gemm as no compatible archs found " |
| 796 | "(requires SM90+ and CUDA >= 12.0).") |
| 797 | endif() |
| 798 | |
| 799 | # |
| 800 | # CUTLASS scaled_mm kernels (moved from _C to _C_stable_libtorch) |
| 801 | # |
| 802 | set(SCALED_MM_3X_ARCHS) |
| 803 | # The cutlass_scaled_mm kernels for Hopper (c3x, i.e. CUTLASS 3.x) require |
| 804 | # CUDA 12.0 or later |
| 805 | cuda_archs_loose_intersection(SCALED_MM_ARCHS "9.0a;" "${CUDA_ARCHS}") |
| 806 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 12.0 AND SCALED_MM_ARCHS) |
| 807 | set(SCALED_MM_SM90_SRCS |
| 808 | "csrc/libtorch_stable/quantization/w8a8/cutlass/scaled_mm_c3x_sm90.cu" |
| 809 | "csrc/libtorch_stable/quantization/w8a8/cutlass/c3x/scaled_mm_sm90_fp8.cu" |
| 810 | "csrc/libtorch_stable/quantization/w8a8/cutlass/c3x/scaled_mm_sm90_int8.cu" |
| 811 | "csrc/libtorch_stable/quantization/w8a8/cutlass/c3x/scaled_mm_azp_sm90_int8.cu" |
| 812 | "csrc/libtorch_stable/quantization/w8a8/cutlass/c3x/scaled_mm_blockwise_sm90_fp8.cu") |
| 813 | set_gencode_flags_for_srcs( |
| 814 | SRCS "${SCALED_MM_SM90_SRCS}" |
| 815 | CUDA_ARCHS "${SCALED_MM_ARCHS}") |
| 816 | list(APPEND VLLM_STABLE_EXT_SRC "${SCALED_MM_SM90_SRCS}") |
| 817 | list(APPEND VLLM_GPU_FLAGS "-DENABLE_SCALED_MM_SM90=1") |
| 818 | # Let scaled_mm_c2x know it doesn't need to build these arches |
| 819 | list(APPEND SCALED_MM_3X_ARCHS "${SCALED_MM_ARCHS}") |
| 820 | message(STATUS "Building scaled_mm_c3x_sm90 for archs: ${SCALED_MM_ARCHS}") |
| 821 | else() |
| 822 | if (NOT ${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 12.0 AND SCALED_MM_ARCHS) |
| 823 | message(STATUS "Not building scaled_mm_c3x_sm90 as CUDA Compiler version is " |
| 824 | "not >= 12.0, we recommend upgrading to CUDA 12.0 or " |
| 825 | "later if you intend on running FP8 quantized models on " |
| 826 | "Hopper.") |
| 827 | else() |
| 828 | message(STATUS "Not building scaled_mm_c3x_sm90 as no compatible archs found " |
| 829 | "in CUDA target architectures") |
| 830 | endif() |
| 831 | endif() |
| 832 | |
| 833 | |
| 834 | # The cutlass_scaled_mm kernels for Blackwell SM12x (c3x, i.e. CUTLASS 3.x) require |
| 835 | # CUDA 12.8 or later |
| 836 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 13.0) |
| 837 | cuda_archs_loose_intersection(SCALED_MM_ARCHS "12.0f" "${CUDA_ARCHS}") |
| 838 | else() |
| 839 | cuda_archs_loose_intersection(SCALED_MM_ARCHS "12.0a;12.1a" "${CUDA_ARCHS}") |
| 840 | endif() |
| 841 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 12.8 AND SCALED_MM_ARCHS) |
| 842 | set(SCALED_MM_SM120_SRCS |
| 843 | "csrc/libtorch_stable/quantization/w8a8/cutlass/scaled_mm_c3x_sm120.cu" |
| 844 | "csrc/libtorch_stable/quantization/w8a8/cutlass/c3x/scaled_mm_sm120_fp8.cu" |
| 845 | "csrc/libtorch_stable/quantization/w8a8/cutlass/c3x/scaled_mm_blockwise_sm120_fp8.cu" |
| 846 | ) |
| 847 | set_gencode_flags_for_srcs( |
| 848 | SRCS "${SCALED_MM_SM120_SRCS}" |
| 849 | CUDA_ARCHS "${SCALED_MM_ARCHS}") |
| 850 | list(APPEND VLLM_STABLE_EXT_SRC "${SCALED_MM_SM120_SRCS}") |
| 851 | list(APPEND VLLM_GPU_FLAGS "-DENABLE_SCALED_MM_SM120=1") |
| 852 | # Let scaled_mm_c2x know it doesn't need to build these arches |
| 853 | list(APPEND SCALED_MM_3X_ARCHS "${SCALED_MM_ARCHS}") |
| 854 | message(STATUS "Building scaled_mm_c3x_sm120 for archs: ${SCALED_MM_ARCHS}") |
| 855 | else() |
| 856 | if (NOT ${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 12.8 AND SCALED_MM_ARCHS) |
| 857 | message(STATUS "Not building scaled_mm_c3x_sm120 as CUDA Compiler version is " |
| 858 | "not >= 12.8, we recommend upgrading to CUDA 12.8 or " |
| 859 | "later if you intend on running FP8 quantized models on " |
| 860 | "Blackwell.") |
| 861 | else() |
| 862 | message(STATUS "Not building scaled_mm_c3x_120 as no compatible archs found " |
| 863 | "in CUDA target architectures") |
| 864 | endif() |
| 865 | endif() |
| 866 | |
| 867 | |
| 868 | # The cutlass_scaled_mm kernels for Blackwell SM100 (c3x, i.e. CUTLASS 3.x) |
| 869 | # require CUDA 12.8 or later |
| 870 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 13.0) |
| 871 | cuda_archs_loose_intersection(SCALED_MM_ARCHS "10.0f;10.7f;11.0f" "${CUDA_ARCHS}") |
| 872 | else() |
| 873 | cuda_archs_loose_intersection(SCALED_MM_ARCHS "10.0a;10.1a;10.3a" "${CUDA_ARCHS}") |
| 874 | endif() |
| 875 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 12.8 AND SCALED_MM_ARCHS) |
| 876 | set(SCALED_MM_SM100_SRCS |
| 877 | "csrc/libtorch_stable/quantization/w8a8/cutlass/scaled_mm_c3x_sm100.cu" |
| 878 | "csrc/libtorch_stable/quantization/w8a8/cutlass/c3x/scaled_mm_sm100_fp8.cu" |
| 879 | "csrc/libtorch_stable/quantization/w8a8/cutlass/c3x/scaled_mm_blockwise_sm100_fp8.cu" |
| 880 | ) |
| 881 | set_gencode_flags_for_srcs( |
| 882 | SRCS "${SCALED_MM_SM100_SRCS}" |
| 883 | CUDA_ARCHS "${SCALED_MM_ARCHS}") |
| 884 | list(APPEND VLLM_STABLE_EXT_SRC "${SCALED_MM_SM100_SRCS}") |
| 885 | list(APPEND VLLM_GPU_FLAGS "-DENABLE_SCALED_MM_SM100=1") |
| 886 | # Let scaled_mm_c2x know it doesn't need to build these arches |
| 887 | list(APPEND SCALED_MM_3X_ARCHS "${SCALED_MM_ARCHS}") |
| 888 | message(STATUS "Building scaled_mm_c3x_sm100 for archs: ${SCALED_MM_ARCHS}") |
| 889 | else() |
| 890 | if (NOT ${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 12.8 AND SCALED_MM_ARCHS) |
| 891 | message(STATUS "Not building scaled_mm_c3x_sm100 as CUDA Compiler version is " |
| 892 | "not >= 12.8, we recommend upgrading to CUDA 12.8 or " |
| 893 | "later if you intend on running FP8 quantized models on " |
| 894 | "Blackwell.") |
| 895 | else() |
| 896 | message(STATUS "Not building scaled_mm_c3x_100 as no compatible archs found " |
| 897 | "in CUDA target architectures") |
| 898 | endif() |
| 899 | endif() |
| 900 | |
| 901 | # |
| 902 | # For the cutlass_scaled_mm kernels we want to build the c2x (CUTLASS 2.x) |
| 903 | # kernels for the remaining archs that are not already built for 3x. |
| 904 | # (Build 8.9 for FP8) |
| 905 | cuda_archs_loose_intersection(SCALED_MM_2X_ARCHS |
| 906 | "7.5;8.0;8.7;8.9+PTX" "${CUDA_ARCHS}") |
| 907 | # subtract out the archs that are already built for 3x |
| 908 | list(REMOVE_ITEM SCALED_MM_2X_ARCHS ${SCALED_MM_3X_ARCHS}) |
| 909 | if (SCALED_MM_2X_ARCHS) |
| 910 | set(SCALED_MM_C2X_SRCS "csrc/libtorch_stable/quantization/w8a8/cutlass/scaled_mm_c2x.cu") |
| 911 | set_gencode_flags_for_srcs( |
| 912 | SRCS "${SCALED_MM_C2X_SRCS}" |
| 913 | CUDA_ARCHS "${SCALED_MM_2X_ARCHS}") |
| 914 | list(APPEND VLLM_STABLE_EXT_SRC "${SCALED_MM_C2X_SRCS}") |
| 915 | list(APPEND VLLM_GPU_FLAGS "-DENABLE_SCALED_MM_C2X=1") |
| 916 | message(STATUS "Building scaled_mm_c2x for archs: ${SCALED_MM_2X_ARCHS}") |
| 917 | else() |
| 918 | if (SCALED_MM_3X_ARCHS) |
| 919 | message(STATUS "Not building scaled_mm_c2x as all archs are already built" |
| 920 | " for and covered by scaled_mm_c3x") |
| 921 | else() |
| 922 | message(STATUS "Not building scaled_mm_c2x as no compatible archs found " |
| 923 | "in CUDA target architectures") |
| 924 | endif() |
| 925 | endif() |
| 926 | |
| 927 | # |
| 928 | # CUTLASS MoE kernels (moved from _C to _C_stable_libtorch) |
| 929 | # |
| 930 | |
| 931 | # The MoE kernel cutlass_moe_mm requires CUDA 12.3 or later (and ONLY works |
| 932 | # on Hopper). get_cutlass_(batched_)moe_mm_data should only be compiled |
| 933 | # if it's possible to compile MoE kernels that use its output. |
| 934 | cuda_archs_loose_intersection(SCALED_MM_ARCHS "9.0a" "${CUDA_ARCHS}") |
| 935 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 12.3 AND SCALED_MM_ARCHS) |
| 936 | set(CUTLASS_MOE_SM90_SRCS "csrc/libtorch_stable/quantization/w8a8/cutlass/moe/grouped_mm_c3x_sm90.cu") |
| 937 | set_gencode_flags_for_srcs( |
| 938 | SRCS "${CUTLASS_MOE_SM90_SRCS}" |
| 939 | CUDA_ARCHS "${SCALED_MM_ARCHS}") |
| 940 | list(APPEND VLLM_STABLE_EXT_SRC "${CUTLASS_MOE_SM90_SRCS}") |
| 941 | list(APPEND VLLM_GPU_FLAGS "-DENABLE_CUTLASS_MOE_SM90=1") |
| 942 | message(STATUS "Building grouped_mm_c3x for archs: ${SCALED_MM_ARCHS}") |
| 943 | else() |
| 944 | if (NOT ${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 12.3 AND SCALED_MM_ARCHS) |
| 945 | message(STATUS "Not building grouped_mm_c3x kernels as CUDA Compiler version is " |
| 946 | "not >= 12.3, we recommend upgrading to CUDA 12.3 or later " |
| 947 | "if you intend on running FP8 quantized MoE models on Hopper.") |
| 948 | else() |
| 949 | message(STATUS "Not building grouped_mm_c3x as no compatible archs found " |
| 950 | "in CUDA target architectures.") |
| 951 | endif() |
| 952 | endif() |
| 953 | |
| 954 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 13.0) |
| 955 | cuda_archs_loose_intersection(SCALED_MM_ARCHS "10.0f;10.7f;11.0f" "${CUDA_ARCHS}") |
| 956 | else() |
| 957 | cuda_archs_loose_intersection(SCALED_MM_ARCHS "10.0a;10.1a;10.3a" "${CUDA_ARCHS}") |
| 958 | endif() |
| 959 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 12.8 AND SCALED_MM_ARCHS) |
| 960 | set(CUTLASS_MOE_SM100_SRCS "csrc/libtorch_stable/quantization/w8a8/cutlass/moe/grouped_mm_c3x_sm100.cu") |
| 961 | set_gencode_flags_for_srcs( |
| 962 | SRCS "${CUTLASS_MOE_SM100_SRCS}" |
| 963 | CUDA_ARCHS "${SCALED_MM_ARCHS}") |
| 964 | list(APPEND VLLM_STABLE_EXT_SRC "${CUTLASS_MOE_SM100_SRCS}") |
| 965 | list(APPEND VLLM_GPU_FLAGS "-DENABLE_CUTLASS_MOE_SM100=1") |
| 966 | message(STATUS "Building grouped_mm_c3x for archs: ${SCALED_MM_ARCHS}") |
| 967 | else() |
| 968 | if (NOT ${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 12.8 AND SCALED_MM_ARCHS) |
| 969 | message(STATUS "Not building grouped_mm_c3x kernels as CUDA Compiler version is " |
| 970 | "not >= 12.8, we recommend upgrading to CUDA 12.8 or later " |
| 971 | "if you intend on running FP8 quantized MoE models on Blackwell.") |
| 972 | else() |
| 973 | message(STATUS "Not building grouped_mm_c3x as no compatible archs found " |
| 974 | "in CUDA target architectures.") |
| 975 | endif() |
| 976 | endif() |
| 977 | |
| 978 | # moe_data.cu is used by all CUTLASS MoE kernels. |
| 979 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 13.0) |
| 980 | cuda_archs_loose_intersection(CUTLASS_MOE_DATA_ARCHS "9.0a;10.0f;10.7f;11.0f;12.0f" "${CUDA_ARCHS}") |
| 981 | else() |
| 982 | cuda_archs_loose_intersection(CUTLASS_MOE_DATA_ARCHS "9.0a;10.0a;10.1a;10.3a;12.0a;12.1a" "${CUDA_ARCHS}") |
| 983 | endif() |
| 984 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 12.3 AND CUTLASS_MOE_DATA_ARCHS) |
| 985 | set(CUTLASS_MOE_DATA_SRCS "csrc/libtorch_stable/quantization/w8a8/cutlass/moe/moe_data.cu") |
| 986 | set_gencode_flags_for_srcs( |
| 987 | SRCS "${CUTLASS_MOE_DATA_SRCS}" |
| 988 | CUDA_ARCHS "${CUTLASS_MOE_DATA_ARCHS}") |
| 989 | list(APPEND VLLM_STABLE_EXT_SRC "${CUTLASS_MOE_DATA_SRCS}") |
| 990 | message(STATUS "Building moe_data for archs: ${CUTLASS_MOE_DATA_ARCHS}") |
| 991 | else() |
| 992 | if (NOT ${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 12.3 AND CUTLASS_MOE_DATA_ARCHS) |
| 993 | message(STATUS "Not building moe_data as CUDA Compiler version is " |
| 994 | "not >= 12.3, we recommend upgrading to CUDA 12.3 or later " |
| 995 | "if you intend on running FP8 quantized MoE models on Hopper or Blackwell.") |
| 996 | else() |
| 997 | message(STATUS "Not building moe_data as no compatible archs found " |
| 998 | "in CUDA target architectures.") |
| 999 | endif() |
| 1000 | endif() |
| 1001 | |
| 1002 | # |
| 1003 | # FP4/NVFP4 kernels (moved from _C to _C_stable_libtorch) |
| 1004 | # |
| 1005 | |
| 1006 | # SM12x FP4 kernels. These share some generic NVFP4 quantization entry |
| 1007 | # sources with the SM10x/11x block below; set_gencode_flags_for_srcs appends |
| 1008 | # per-source flags, so shared files accumulate both SM12x and SM10x/11x |
| 1009 | # gencodes when both families are requested. |
| 1010 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 13.0) |
| 1011 | cuda_archs_loose_intersection(FP4_SM120_ARCHS "12.0f" "${CUDA_ARCHS}") |
| 1012 | else() |
| 1013 | cuda_archs_loose_intersection(FP4_SM120_ARCHS "12.0a;12.1a" "${CUDA_ARCHS}") |
| 1014 | endif() |
| 1015 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 12.8 AND FP4_SM120_ARCHS) |
| 1016 | set(FP4_SM120_SRCS |
| 1017 | "csrc/libtorch_stable/quantization/fp4/nvfp4_quant_kernels.cu" |
| 1018 | "csrc/libtorch_stable/quantization/fp4/activation_nvfp4_quant_fusion_kernels.cu" |
| 1019 | "csrc/libtorch_stable/quantization/fp4/nvfp4_experts_quant.cu" |
| 1020 | "csrc/libtorch_stable/quantization/fp4/nvfp4_scaled_mm_sm120_kernels.cu" |
| 1021 | "csrc/libtorch_stable/quantization/fp4/nvfp4_blockwise_moe_kernel.cu" |
| 1022 | "csrc/libtorch_stable/nvfp4_kv_cache_kernels.cu") |
| 1023 | set_gencode_flags_for_srcs( |
| 1024 | SRCS "${FP4_SM120_SRCS}" |
| 1025 | CUDA_ARCHS "${FP4_SM120_ARCHS}") |
| 1026 | list(APPEND VLLM_STABLE_EXT_SRC "${FP4_SM120_SRCS}") |
| 1027 | list(APPEND VLLM_GPU_FLAGS "-DENABLE_NVFP4_SM120=1") |
| 1028 | list(APPEND VLLM_GPU_FLAGS "-DENABLE_CUTLASS_MOE_SM120=1") |
| 1029 | message(STATUS "Building SM12x NVFP4 for archs: ${FP4_SM120_ARCHS}") |
| 1030 | else() |
| 1031 | message(STATUS "Not building SM12x NVFP4 as no compatible archs were found.") |
| 1032 | endif() |
| 1033 | |
| 1034 | # SM10x/11x FP4 kernels. MXFP4 experts quantization is currently compiled |
| 1035 | # only in this block; SM12x has separate NVFP4 matmul/MoE kernels above. |
| 1036 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 13.0) |
| 1037 | cuda_archs_loose_intersection(FP4_SM100_ARCHS "10.0f;10.7f;11.0f" "${CUDA_ARCHS}") |
| 1038 | else() |
| 1039 | cuda_archs_loose_intersection(FP4_SM100_ARCHS "10.0a;10.1a;10.3a" "${CUDA_ARCHS}") |
| 1040 | endif() |
| 1041 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 12.8 AND FP4_SM100_ARCHS) |
| 1042 | set(FP4_SM100_SRCS |
| 1043 | "csrc/libtorch_stable/quantization/fp4/nvfp4_quant_kernels.cu" |
| 1044 | "csrc/libtorch_stable/quantization/fp4/activation_nvfp4_quant_fusion_kernels.cu" |
| 1045 | "csrc/libtorch_stable/quantization/fp4/nvfp4_experts_quant.cu" |
| 1046 | "csrc/libtorch_stable/quantization/fp4/nvfp4_scaled_mm_kernels.cu" |
| 1047 | "csrc/libtorch_stable/quantization/fp4/nvfp4_blockwise_moe_kernel.cu" |
| 1048 | "csrc/libtorch_stable/quantization/fp4/mxfp4_experts_quant.cu" |
| 1049 | "csrc/libtorch_stable/quantization/fp4/mxfp4_blockwise_moe_kernel.cu" |
| 1050 | "csrc/libtorch_stable/nvfp4_kv_cache_kernels.cu" |
| 1051 | "csrc/libtorch_stable/nvfp4_ds_mla_cache_kernels.cu") |
| 1052 | if(NOT ${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 12.9) |
| 1053 | message(STATUS |
| 1054 | "Building mxfp4_experts_quant unsupported stubs because CUDA compiler version is not >= 12.9 (found ${CMAKE_CUDA_COMPILER_VERSION}).") |
| 1055 | endif() |
| 1056 | set_gencode_flags_for_srcs( |
| 1057 | SRCS "${FP4_SM100_SRCS}" |
| 1058 | CUDA_ARCHS "${FP4_SM100_ARCHS}") |
| 1059 | list(APPEND VLLM_STABLE_EXT_SRC "${FP4_SM100_SRCS}") |
| 1060 | list(APPEND VLLM_GPU_FLAGS "-DENABLE_NVFP4_SM100=1") |
| 1061 | list(APPEND VLLM_GPU_FLAGS "-DENABLE_CUTLASS_MOE_SM100=1") |
| 1062 | message(STATUS "Building SM10x/11x NVFP4/MXFP4 for archs: ${FP4_SM100_ARCHS}") |
| 1063 | else() |
| 1064 | message(STATUS "Not building SM10x/11x NVFP4/MXFP4 as no compatible archs were found.") |
| 1065 | endif() |
| 1066 | |
| 1067 | # |
| 1068 | # W4A8 kernels (moved from _C to _C_stable_libtorch) |
| 1069 | # |
| 1070 | |
| 1071 | # Only build W4A8 kernels if we are building for something compatible with sm90a |
| 1072 | cuda_archs_loose_intersection(W4A8_ARCHS "9.0a" "${CUDA_ARCHS}") |
| 1073 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 12.0 AND W4A8_ARCHS) |
| 1074 | set(W4A8_SRCS |
| 1075 | "csrc/libtorch_stable/quantization/cutlass_w4a8/w4a8_mm_entry.cu" |
| 1076 | "csrc/libtorch_stable/quantization/cutlass_w4a8/w4a8_grouped_mm_entry.cu" |
| 1077 | "csrc/libtorch_stable/quantization/cutlass_w4a8/w4a8_utils.cu" |
| 1078 | ) |
| 1079 | |
| 1080 | set_gencode_flags_for_srcs( |
| 1081 | SRCS "${W4A8_SRCS}" |
| 1082 | CUDA_ARCHS "${W4A8_ARCHS}") |
| 1083 | |
| 1084 | list(APPEND VLLM_STABLE_EXT_SRC "${W4A8_SRCS}") |
| 1085 | |
| 1086 | message(STATUS "Building W4A8 kernels for archs: ${W4A8_ARCHS}") |
| 1087 | else() |
| 1088 | if (NOT ${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 12.0 |
| 1089 | AND W4A8_ARCHS) |
| 1090 | message(STATUS "Not building W4A8 kernels as CUDA Compiler version is " |
| 1091 | "not >= 12.0, we recommend upgrading to CUDA 12.0 or " |
| 1092 | "later if you intend on running w4a16 quantized models on " |
| 1093 | "Hopper.") |
| 1094 | else() |
| 1095 | message(STATUS "Not building W4A8 kernels as no compatible archs " |
| 1096 | "found in CUDA target architectures") |
| 1097 | endif() |
| 1098 | endif() |
| 1099 | |
| 1100 | # CUTLASS MLA Archs and flags. |
| 1101 | # Runtime dispatch is gated in |
| 1102 | # vllm/v1/attention/backends/mla/cutlass_mla.py. |
| 1103 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 13.0) |
| 1104 | cuda_archs_loose_intersection(MLA_ARCHS "10.0f;10.7f;11.0f" "${CUDA_ARCHS}") |
| 1105 | else() |
| 1106 | cuda_archs_loose_intersection(MLA_ARCHS "10.0a;10.1a;10.3a" "${CUDA_ARCHS}") |
| 1107 | endif() |
| 1108 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 12.8 AND MLA_ARCHS) |
| 1109 | set(CUTLASS_MLA_SRCS |
| 1110 | "csrc/libtorch_stable/attention/mla/sm100_cutlass_mla_kernel.cu") |
| 1111 | set_gencode_flags_for_srcs( |
| 1112 | SRCS "${CUTLASS_MLA_SRCS}" |
| 1113 | CUDA_ARCHS "${MLA_ARCHS}") |
| 1114 | list(APPEND VLLM_STABLE_EXT_SRC "${CUTLASS_MLA_SRCS}") |
| 1115 | list(APPEND VLLM_GPU_FLAGS "-DENABLE_CUTLASS_MLA=1") |
| 1116 | # Add MLA-specific include directories only to MLA source files |
| 1117 | set_source_files_properties(${CUTLASS_MLA_SRCS} |
| 1118 | PROPERTIES INCLUDE_DIRECTORIES "${CUTLASS_DIR}/examples/77_blackwell_fmha;${CUTLASS_DIR}/examples/common") |
| 1119 | message(STATUS "Building CUTLASS MLA for archs: ${MLA_ARCHS}") |
| 1120 | else() |
| 1121 | message(STATUS "Not building CUTLASS MLA as no compatible archs were found.") |
| 1122 | # clear MLA_ARCHS |
| 1123 | set(MLA_ARCHS) |
| 1124 | endif() |
| 1125 | |
| 1126 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 13.0) |
| 1127 | cuda_archs_loose_intersection(FUSED_KDA_DECODE_ARCHS |
| 1128 | "9.0a;10.0f;12.0f" "${CUDA_ARCHS}") |
| 1129 | cuda_archs_loose_intersection(FUSED_GDN_DECODE_ARCHS |
| 1130 | "8.0;8.6;8.9;9.0a;10.0f;11.0f;12.0f" "${CUDA_ARCHS}") |
| 1131 | endif() |
| 1132 | if(FUSED_KDA_DECODE_ARCHS) |
| 1133 | set(FUSED_KDA_DECODE_SRC |
| 1134 | "csrc/libtorch_stable/kimi_k3/fused_kda_decode_kernel.cu") |
| 1135 | set_gencode_flags_for_srcs( |
| 1136 | SRCS "${FUSED_KDA_DECODE_SRC}" |
| 1137 | CUDA_ARCHS "${FUSED_KDA_DECODE_ARCHS}") |
| 1138 | set_property(SOURCE ${FUSED_KDA_DECODE_SRC} APPEND PROPERTY |
| 1139 | COMPILE_OPTIONS "$<$<COMPILE_LANGUAGE:CUDA>:--use_fast_math>") |
| 1140 | list(APPEND VLLM_STABLE_EXT_SRC "${FUSED_KDA_DECODE_SRC}") |
| 1141 | message(STATUS |
| 1142 | "Building fused KDA decode for archs: ${FUSED_KDA_DECODE_ARCHS}") |
| 1143 | endif() |
| 1144 | if(FUSED_GDN_DECODE_ARCHS) |
| 1145 | set(FUSED_GDN_DECODE_SRC |
| 1146 | "csrc/libtorch_stable/gdn/fused_gdn_decode_kernel.cu") |
| 1147 | set_gencode_flags_for_srcs( |
| 1148 | SRCS "${FUSED_GDN_DECODE_SRC}" |
| 1149 | CUDA_ARCHS "${FUSED_GDN_DECODE_ARCHS}") |
| 1150 | set_property(SOURCE ${FUSED_GDN_DECODE_SRC} APPEND PROPERTY |
| 1151 | COMPILE_OPTIONS "$<$<COMPILE_LANGUAGE:CUDA>:--use_fast_math>") |
| 1152 | list(APPEND VLLM_STABLE_EXT_SRC "${FUSED_GDN_DECODE_SRC}") |
| 1153 | message(STATUS |
| 1154 | "Building fused GDN decode for archs: ${FUSED_GDN_DECODE_ARCHS}") |
| 1155 | endif() |
| 1156 | |
| 1157 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 13.0) |
| 1158 | cuda_archs_loose_intersection(KIMI_K3_ATTN_RES_ARCHS |
| 1159 | "10.0f" "${CUDA_ARCHS}") |
| 1160 | endif() |
| 1161 | if(KIMI_K3_ATTN_RES_ARCHS) |
| 1162 | set(KIMI_K3_ATTN_RES_SRC |
| 1163 | "csrc/libtorch_stable/kimi_k3/attn_res_kernel.cu") |
| 1164 | set_gencode_flags_for_srcs( |
| 1165 | SRCS "${KIMI_K3_ATTN_RES_SRC}" |
| 1166 | CUDA_ARCHS "${KIMI_K3_ATTN_RES_ARCHS}") |
| 1167 | set_property(SOURCE ${KIMI_K3_ATTN_RES_SRC} APPEND PROPERTY |
| 1168 | COMPILE_OPTIONS |
| 1169 | "$<$<COMPILE_LANGUAGE:CUDA>:--expt-relaxed-constexpr;--expt-extended-lambda;--use_fast_math>") |
| 1170 | list(APPEND VLLM_STABLE_EXT_SRC "${KIMI_K3_ATTN_RES_SRC}") |
| 1171 | message(STATUS |
| 1172 | "Building Kimi K3 AttnRes for archs: ${KIMI_K3_ATTN_RES_ARCHS}") |
| 1173 | endif() |
| 1174 | |
| 1175 | # Hadacore kernels |
| 1176 | cuda_archs_loose_intersection(HADACORE_ARCHS "8.0+PTX;9.0+PTX" "${CUDA_ARCHS}") |
| 1177 | if(HADACORE_ARCHS) |
| 1178 | set(HADACORE_SRCS "csrc/libtorch_stable/quantization/hadamard/hadacore/hadamard_transform_cuda.cu") |
| 1179 | set_gencode_flags_for_srcs( |
| 1180 | SRCS "${HADACORE_SRCS}" |
| 1181 | CUDA_ARCHS "${HADACORE_ARCHS}") |
| 1182 | list(APPEND VLLM_STABLE_EXT_SRC "${HADACORE_SRCS}") |
| 1183 | message(STATUS "Building hadacore") |
| 1184 | endif() |
| 1185 | |
| 1186 | # if CUDA endif |
| 1187 | endif() |
| 1188 | |
| 1189 | # HIP counterpart of the FUSED_KDA_DECODE block above: same `fused_kda_decode` |
| 1190 | # op, same VLLM_ENABLE_FUSED_KDA_DECODE guard in torch_bindings.cpp |
| 1191 | # Built for the CDNA archs the kernel supports |
| 1192 | # (gfx942 / gfx950), so it is only emitted when one of those is in the arch |
| 1193 | # list. A multi-arch build that includes them still emits this source for |
| 1194 | # every arch in that list. The runtime gate in kda_decode.py keeps the kernel |
| 1195 | # off unsupported devices. Fused prefill KDA kernels is Only measured on |
| 1196 | # gfx950, so it is only built when gfx950 is in the arch list. |
| 1197 | if(VLLM_GPU_LANG STREQUAL "HIP") |
| 1198 | set(FUSED_KDA_DECODE_HIP_ARCHS ${VLLM_GPU_ARCHES}) |
| 1199 | list(FILTER FUSED_KDA_DECODE_HIP_ARCHS INCLUDE REGEX "gfx942|gfx950") |
| 1200 | if(FUSED_KDA_DECODE_HIP_ARCHS) |
| 1201 | set(FUSED_KDA_DECODE_HIP_SRC |
| 1202 | "csrc/libtorch_stable/kimi_k3/fused_kda_decode_kernel_rocm.cu") |
| 1203 | list(APPEND VLLM_STABLE_EXT_SRC "${FUSED_KDA_DECODE_HIP_SRC}") |
| 1204 | message(STATUS |
| 1205 | "Building fused KDA decode for HIP archs: ${FUSED_KDA_DECODE_HIP_ARCHS}") |
| 1206 | endif() |
| 1207 | |
| 1208 | set(FUSED_KDA_CHUNK_HIP_ARCHS ${VLLM_GPU_ARCHES}) |
| 1209 | list(FILTER FUSED_KDA_CHUNK_HIP_ARCHS INCLUDE REGEX "gfx950") |
| 1210 | if(FUSED_KDA_CHUNK_HIP_ARCHS) |
| 1211 | set(FUSED_KDA_CHUNK_HIP_SRC |
| 1212 | "csrc/libtorch_stable/kimi_k3/fused_kda_chunk_kernel_rocm.cu") |
| 1213 | list(APPEND VLLM_STABLE_EXT_SRC "${FUSED_KDA_CHUNK_HIP_SRC}") |
| 1214 | message(STATUS |
| 1215 | "Building fused KDA chunk for HIP archs: ${FUSED_KDA_CHUNK_HIP_ARCHS}") |
| 1216 | endif() |
| 1217 | endif() |
| 1218 | |
| 1219 | message(STATUS "Enabling C_stable extension.") |
| 1220 | list(REMOVE_DUPLICATES VLLM_STABLE_EXT_SRC) |
| 1221 | define_extension_target( |
| 1222 | _C_stable_libtorch |
| 1223 | DESTINATION vllm |
| 1224 | LANGUAGE ${VLLM_GPU_LANG} |
| 1225 | SOURCES ${VLLM_STABLE_EXT_SRC} |
| 1226 | COMPILE_FLAGS ${VLLM_GPU_FLAGS} |
| 1227 | ARCHITECTURES ${VLLM_GPU_ARCHES} |
| 1228 | INCLUDE_DIRECTORIES ${CUTLASS_INCLUDE_DIR} ${CUTLASS_TOOLS_UTIL_INCLUDE_DIR} |
| 1229 | USE_SABI 3 |
| 1230 | WITH_SOABI) |
| 1231 | |
| 1232 | # Set TORCH_TARGET_VERSION for stable ABI compatibility. |
| 1233 | # This ensures we only use C-shim APIs available in PyTorch 2.11. |
| 1234 | # _C_stable_libtorch is abi compatible with PyTorch >= TORCH_TARGET_VERSION |
| 1235 | # which is currently set to 2.11. |
| 1236 | target_compile_definitions(_C_stable_libtorch PRIVATE |
| 1237 | TORCH_TARGET_VERSION=0x020B000000000000ULL) |
| 1238 | |
| 1239 | # Needed to use cuda/hip APIs from C-shim |
| 1240 | if(VLLM_GPU_LANG STREQUAL "CUDA") |
| 1241 | target_compile_definitions(_C_stable_libtorch PRIVATE USE_CUDA) |
| 1242 | if(COOPERATIVE_TOPK_ARCHS) |
| 1243 | target_compile_definitions(_C_stable_libtorch PRIVATE |
| 1244 | VLLM_ENABLE_COOPERATIVE_TOPK=1) |
| 1245 | endif() |
| 1246 | if(FUSED_KDA_DECODE_ARCHS) |
| 1247 | target_compile_definitions(_C_stable_libtorch PRIVATE |
| 1248 | VLLM_ENABLE_FUSED_KDA_DECODE=1) |
| 1249 | endif() |
| 1250 | if(FUSED_GDN_DECODE_ARCHS) |
| 1251 | target_compile_definitions(_C_stable_libtorch PRIVATE |
| 1252 | VLLM_ENABLE_FUSED_GDN_DECODE=1) |
| 1253 | endif() |
| 1254 | if(KIMI_K3_ATTN_RES_ARCHS) |
| 1255 | target_compile_definitions(_C_stable_libtorch PRIVATE |
| 1256 | VLLM_ENABLE_KIMI_K3_ATTN_RES=1) |
| 1257 | endif() |
| 1258 | # Needed by CUTLASS kernels |
| 1259 | target_compile_definitions(_C_stable_libtorch PRIVATE |
| 1260 | CUTLASS_ENABLE_DIRECT_CUDA_DRIVER_CALL=1) |
| 1261 | elseif(VLLM_GPU_LANG STREQUAL "HIP") |
| 1262 | target_compile_definitions(_C_stable_libtorch PRIVATE USE_ROCM) |
| 1263 | if(FUSED_KDA_DECODE_HIP_SRC) |
| 1264 | target_compile_definitions(_C_stable_libtorch PRIVATE |
| 1265 | VLLM_ENABLE_FUSED_KDA_DECODE=1) |
| 1266 | endif() |
| 1267 | if(FUSED_KDA_CHUNK_HIP_SRC) |
| 1268 | target_compile_definitions(_C_stable_libtorch PRIVATE |
| 1269 | VLLM_ENABLE_FUSED_KDA_CHUNK=1) |
| 1270 | endif() |
| 1271 | endif() |
| 1272 | |
| 1273 | # On ROCm, _C_stable_libtorch calls raw HIP APIs (e.g. hipGetDevice in |
| 1274 | # get_device_prop()) which must resolve to the same libamdhip64.so that |
| 1275 | # PyTorch uses. When PyTorch bundles its own copy (pip/conda wheels), |
| 1276 | # the raw HIP calls would otherwise resolve to the system ROCm copy, |
| 1277 | # initializing a second HIP runtime that corrupts device state (wrong |
| 1278 | # device on DeviceGuard, core dumps on multi-GPU tests). |
| 1279 | # |
| 1280 | # If PyTorch doesn't bundle libamdhip64 (built from source against system |
| 1281 | # ROCm), there is only one copy in the process and no action is needed — |
| 1282 | # the HIP compiler already links the system libamdhip64 automatically. |
| 1283 | if(VLLM_GPU_LANG STREQUAL "HIP") |
| 1284 | find_library(_STABLE_TORCH_AMDHIP64 amdhip64 |
| 1285 | PATHS "${TORCH_INSTALL_PREFIX}/lib" NO_DEFAULT_PATH) |
| 1286 | if(_STABLE_TORCH_AMDHIP64) |
| 1287 | message(STATUS "Found PyTorch-bundled libamdhip64 at ${_STABLE_TORCH_AMDHIP64}") |
| 1288 | target_link_libraries(_C_stable_libtorch PRIVATE ${_STABLE_TORCH_AMDHIP64}) |
| 1289 | endif() |
| 1290 | endif() |
| 1291 | endif() |
| 1292 | |
| 1293 | # |
| 1294 | # _moe_C_stable_libtorch extension |
| 1295 | # |
| 1296 | |
| 1297 | set(VLLM_MOE_EXT_SRC |
| 1298 | "csrc/libtorch_stable/moe/torch_bindings.cpp" |
| 1299 | "csrc/libtorch_stable/moe/moe_align_sum_kernels.cu" |
| 1300 | "csrc/libtorch_stable/moe/topk_softmax_kernels.cu" |
| 1301 | "csrc/libtorch_stable/moe/topk_softplus_sqrt_kernels.cu") |
| 1302 | |
| 1303 | if(VLLM_GPU_LANG STREQUAL "CUDA") |
| 1304 | list(APPEND VLLM_MOE_EXT_SRC |
| 1305 | "csrc/libtorch_stable/moe/moe_wna16.cu" |
| 1306 | "csrc/libtorch_stable/moe/grouped_topk_kernels.cu") |
| 1307 | endif() |
| 1308 | |
| 1309 | if(VLLM_GPU_LANG STREQUAL "CUDA") |
| 1310 | set(MOE_PERMUTE_SRC |
| 1311 | "csrc/libtorch_stable/moe/permute_unpermute_kernels/moe_permute_unpermute_kernel.cu" |
| 1312 | "csrc/libtorch_stable/moe/moe_permute_unpermute_op.cu") |
| 1313 | |
| 1314 | list(APPEND VLLM_MOE_EXT_SRC "${MOE_PERMUTE_SRC}") |
| 1315 | endif() |
| 1316 | |
| 1317 | set_gencode_flags_for_srcs( |
| 1318 | SRCS "${VLLM_MOE_EXT_SRC}" |
| 1319 | CUDA_ARCHS "${CUDA_ARCHS}") |
| 1320 | |
| 1321 | if(VLLM_GPU_LANG STREQUAL "CUDA") |
| 1322 | set(VLLM_MOE_WNA16_SRC |
| 1323 | "csrc/libtorch_stable/moe/moe_wna16.cu") |
| 1324 | |
| 1325 | set_gencode_flags_for_srcs( |
| 1326 | SRCS "${VLLM_MOE_WNA16_SRC}" |
| 1327 | CUDA_ARCHS "${CUDA_ARCHS}") |
| 1328 | |
| 1329 | list(APPEND VLLM_MOE_EXT_SRC "${VLLM_MOE_WNA16_SRC}") |
| 1330 | # moe marlin arches |
| 1331 | # note that we always set `use_atomic_add=False` for moe marlin now, |
| 1332 | # so we don't need 9.0 for bf16 atomicAdd PTX |
| 1333 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 13.0) |
| 1334 | cuda_archs_loose_intersection(MARLIN_MOE_ARCHS "8.0+PTX;12.0f" "${CUDA_ARCHS}") |
| 1335 | else() |
| 1336 | cuda_archs_loose_intersection(MARLIN_MOE_ARCHS "8.0+PTX;12.0a;12.1a" "${CUDA_ARCHS}") |
| 1337 | endif() |
| 1338 | # moe marlin has limited support for turing |
| 1339 | cuda_archs_loose_intersection(MARLIN_MOE_SM75_ARCHS "7.5" "${CUDA_ARCHS}") |
| 1340 | # moe marlin arches for fp8 input |
| 1341 | # - sm80 doesn't support fp8 computation |
| 1342 | # - sm90 and sm100 don't support QMMA.16832.F32.E4M3.E4M3 SAAS instruction |
| 1343 | # so we only enable fp8 computation for SM89 (e.g. RTX 40x0) and 12.0 (e.g. RTX 50x0) |
| 1344 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 13.0) |
| 1345 | cuda_archs_loose_intersection(MARLIN_MOE_FP8_ARCHS "8.9;12.0f" "${CUDA_ARCHS}") |
| 1346 | else() |
| 1347 | cuda_archs_loose_intersection(MARLIN_MOE_FP8_ARCHS "8.9;12.0a;12.1a" "${CUDA_ARCHS}") |
| 1348 | endif() |
| 1349 | # moe marlin arches for other files |
| 1350 | cuda_archs_loose_intersection(MARLIN_MOE_OTHER_ARCHS "7.5;8.0+PTX" "${CUDA_ARCHS}") |
| 1351 | if (MARLIN_MOE_OTHER_ARCHS) |
| 1352 | |
| 1353 | # |
| 1354 | # For the Marlin MOE kernels we automatically generate sources for various |
| 1355 | # preselected input type pairs and schedules. |
| 1356 | # Generate sources: |
| 1357 | set(MOE_MARLIN_GEN_SCRIPT |
| 1358 | ${CMAKE_CURRENT_SOURCE_DIR}/csrc/libtorch_stable/moe/marlin_moe_wna16/generate_kernels.py) |
| 1359 | file(MD5 ${MOE_MARLIN_GEN_SCRIPT} MOE_MARLIN_GEN_SCRIPT_HASH) |
| 1360 | list(JOIN CUDA_ARCHS "," CUDA_ARCHS_STR) |
| 1361 | set(MOE_MARLIN_GEN_SCRIPT_HASH_AND_ARCH "${MOE_MARLIN_GEN_SCRIPT_HASH}(ARCH:${CUDA_ARCHS_STR})") |
| 1362 | |
| 1363 | message(STATUS "Marlin MOE generation script hash with arch: ${MOE_MARLIN_GEN_SCRIPT_HASH_AND_ARCH}") |
| 1364 | message(STATUS "Last run Marlin MOE generate script hash with arch: $CACHE{MOE_MARLIN_GEN_SCRIPT_HASH_AND_ARCH}") |
| 1365 | |
| 1366 | if (NOT DEFINED CACHE{MOE_MARLIN_GEN_SCRIPT_HASH_AND_ARCH} |
| 1367 | OR NOT $CACHE{MOE_MARLIN_GEN_SCRIPT_HASH_AND_ARCH} STREQUAL ${MOE_MARLIN_GEN_SCRIPT_HASH_AND_ARCH}) |
| 1368 | execute_process( |
| 1369 | COMMAND ${CMAKE_COMMAND} -E env |
| 1370 | PYTHONPATH=$ENV{PYTHONPATH} |
| 1371 | ${Python_EXECUTABLE} ${MOE_MARLIN_GEN_SCRIPT} ${CUDA_ARCHS_STR} |
| 1372 | RESULT_VARIABLE moe_marlin_generation_result |
| 1373 | OUTPUT_VARIABLE moe_marlin_generation_output |
| 1374 | OUTPUT_FILE ${CMAKE_CURRENT_BINARY_DIR}/moe_marlin_generation.log |
| 1375 | ERROR_FILE ${CMAKE_CURRENT_BINARY_DIR}/moe_marlin_generation.log |
| 1376 | ) |
| 1377 | |
| 1378 | if (NOT moe_marlin_generation_result EQUAL 0) |
| 1379 | message(FATAL_ERROR "Marlin MOE generation failed." |
| 1380 | " Result: \"${moe_marlin_generation_result}\"" |
| 1381 | "\nCheck the log for details: " |
| 1382 | "${CMAKE_CURRENT_BINARY_DIR}/moe_marlin_generation.log") |
| 1383 | else() |
| 1384 | set(MOE_MARLIN_GEN_SCRIPT_HASH_AND_ARCH ${MOE_MARLIN_GEN_SCRIPT_HASH_AND_ARCH} |
| 1385 | CACHE STRING "Last run Marlin MOE generate script hash" FORCE) |
| 1386 | message(STATUS "Marlin MOE generation completed successfully.") |
| 1387 | endif() |
| 1388 | else() |
| 1389 | message(STATUS "Marlin MOE generation script has not changed, skipping generation.") |
| 1390 | endif() |
| 1391 | |
| 1392 | if (MARLIN_MOE_ARCHS) |
| 1393 | file(GLOB MARLIN_MOE_SRC "csrc/libtorch_stable/moe/marlin_moe_wna16/sm80_kernel_*.cu") |
| 1394 | set_gencode_flags_for_srcs( |
| 1395 | SRCS "${MARLIN_MOE_SRC}" |
| 1396 | CUDA_ARCHS "${MARLIN_MOE_ARCHS}") |
| 1397 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 12.8) |
| 1398 | set_source_files_properties(${MARLIN_MOE_SRC} |
| 1399 | PROPERTIES COMPILE_FLAGS "-static-global-template-stub=false") |
| 1400 | endif() |
| 1401 | list(APPEND VLLM_MOE_EXT_SRC ${MARLIN_MOE_SRC}) |
| 1402 | endif() |
| 1403 | |
| 1404 | if (MARLIN_MOE_SM75_ARCHS) |
| 1405 | file(GLOB MARLIN_MOE_SM75_SRC "csrc/libtorch_stable/moe/marlin_moe_wna16/sm75_kernel_*.cu") |
| 1406 | set_gencode_flags_for_srcs( |
| 1407 | SRCS "${MARLIN_MOE_SM75_SRC}" |
| 1408 | CUDA_ARCHS "${MARLIN_MOE_SM75_ARCHS}") |
| 1409 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 12.8) |
| 1410 | set_source_files_properties(${MARLIN_MOE_SM75_SRC} |
| 1411 | PROPERTIES COMPILE_FLAGS "-static-global-template-stub=false") |
| 1412 | endif() |
| 1413 | list(APPEND VLLM_MOE_EXT_SRC ${MARLIN_MOE_SM75_SRC}) |
| 1414 | endif() |
| 1415 | |
| 1416 | if (MARLIN_MOE_FP8_ARCHS) |
| 1417 | file(GLOB MARLIN_MOE_FP8_SRC "csrc/libtorch_stable/moe/marlin_moe_wna16/sm89_kernel_*.cu") |
| 1418 | set_gencode_flags_for_srcs( |
| 1419 | SRCS "${MARLIN_MOE_FP8_SRC}" |
| 1420 | CUDA_ARCHS "${MARLIN_MOE_FP8_ARCHS}") |
| 1421 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 12.8) |
| 1422 | set_source_files_properties(${MARLIN_MOE_FP8_SRC} |
| 1423 | PROPERTIES COMPILE_FLAGS "-static-global-template-stub=false") |
| 1424 | endif() |
| 1425 | list(APPEND VLLM_MOE_EXT_SRC ${MARLIN_MOE_FP8_SRC}) |
| 1426 | endif() |
| 1427 | |
| 1428 | set(MARLIN_MOE_OTHER_SRC "csrc/libtorch_stable/moe/marlin_moe_wna16/ops.cu") |
| 1429 | set_gencode_flags_for_srcs( |
| 1430 | SRCS "${MARLIN_MOE_OTHER_SRC}" |
| 1431 | CUDA_ARCHS "${MARLIN_MOE_OTHER_ARCHS}") |
| 1432 | if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 12.8) |
| 1433 | set_source_files_properties(${MARLIN_MOE_OTHER_SRC} |
| 1434 | PROPERTIES COMPILE_FLAGS "-static-global-template-stub=false") |
| 1435 | endif() |
| 1436 | list(APPEND VLLM_MOE_EXT_SRC "${MARLIN_MOE_OTHER_SRC}") |
| 1437 | |
| 1438 | message(STATUS "Building Marlin MOE kernels for archs: ${MARLIN_MOE_OTHER_ARCHS}") |
| 1439 | else() |
| 1440 | message(STATUS "Not building Marlin MOE kernels as no compatible archs found" |
| 1441 | " in CUDA target architectures") |
| 1442 | endif() |
| 1443 | endif() |
| 1444 | |
| 1445 | message(STATUS "Enabling MoE C_stable extension.") |
| 1446 | define_extension_target( |
| 1447 | _moe_C_stable_libtorch |
| 1448 | DESTINATION vllm |
| 1449 | LANGUAGE ${VLLM_GPU_LANG} |
| 1450 | SOURCES ${VLLM_MOE_EXT_SRC} |
| 1451 | COMPILE_FLAGS ${VLLM_GPU_FLAGS} |
| 1452 | ARCHITECTURES ${VLLM_GPU_ARCHES} |
| 1453 | INCLUDE_DIRECTORIES ${CUTLASS_INCLUDE_DIR} |
| 1454 | INCLUDE_DIRECTORIES ${CUTLASS_TOOLS_UTIL_INCLUDE_DIR} |
| 1455 | USE_SABI 3 |
| 1456 | WITH_SOABI) |
| 1457 | |
| 1458 | # Set TORCH_TARGET_VERSION for stable ABI compatibility. |
| 1459 | # This ensures we only use C-shim APIs available in PyTorch 2.11. |
| 1460 | # _moe_C_stable_libtorch is abi compatible with PyTorch >= TORCH_TARGET_VERSION |
| 1461 | # which is currently set to 2.11. |
| 1462 | target_compile_definitions(_moe_C_stable_libtorch PRIVATE |
| 1463 | TORCH_TARGET_VERSION=0x020B000000000000ULL) |
| 1464 | |
| 1465 | # Needed to use cuda/hip APIs from C-shim |
| 1466 | if(VLLM_GPU_LANG STREQUAL "CUDA") |
| 1467 | target_compile_definitions(_moe_C_stable_libtorch PRIVATE USE_CUDA) |
| 1468 | # Needed by CUTLASS kernels |
| 1469 | target_compile_definitions(_moe_C_stable_libtorch PRIVATE |
| 1470 | CUTLASS_ENABLE_DIRECT_CUDA_DRIVER_CALL=1) |
| 1471 | elseif(VLLM_GPU_LANG STREQUAL "HIP") |
| 1472 | target_compile_definitions(_moe_C_stable_libtorch PRIVATE USE_ROCM) |
| 1473 | endif() |
| 1474 | |
| 1475 | # On ROCm, _moe_C_stable_libtorch calls raw HIP APIs (e.g. hipGetDevice in |
| 1476 | # get_device_prop()) which must resolve to the same libamdhip64.so that |
| 1477 | # PyTorch uses. When PyTorch bundles its own copy (pip/conda wheels), |
| 1478 | # the raw HIP calls would otherwise resolve to the system ROCm copy, |
| 1479 | # initializing a second HIP runtime that corrupts device state (wrong |
| 1480 | # device on DeviceGuard, core dumps on multi-GPU tests). |
| 1481 | # |
| 1482 | # If PyTorch doesn't bundle libamdhip64 (built from source against system |
| 1483 | # ROCm), there is only one copy in the process and no action is needed — |
| 1484 | # the HIP compiler already links the system libamdhip64 automatically. |
| 1485 | if(VLLM_GPU_LANG STREQUAL "HIP") |
| 1486 | find_library(_MOE_STABLE_TORCH_AMDHIP64 amdhip64 |
| 1487 | PATHS "${TORCH_INSTALL_PREFIX}/lib" NO_DEFAULT_PATH) |
| 1488 | if(_MOE_STABLE_TORCH_AMDHIP64) |
| 1489 | message(STATUS "Found PyTorch-bundled libamdhip64 for _moe_C_stable_libtorch at ${_MOE_STABLE_TORCH_AMDHIP64}") |
| 1490 | target_link_libraries(_moe_C_stable_libtorch PRIVATE ${_MOE_STABLE_TORCH_AMDHIP64}) |
| 1491 | endif() |
| 1492 | endif() |
| 1493 | |
| 1494 | if(VLLM_GPU_LANG STREQUAL "HIP") |
| 1495 | # |
| 1496 | # _rocm_C extension |
| 1497 | # |
| 1498 | set(VLLM_ROCM_EXT_SRC |
| 1499 | "csrc/rocm/torch_bindings.cpp" |
| 1500 | "csrc/rocm/skinny_gemms.cu" |
| 1501 | "csrc/rocm/skinny_gemms_int4.cu" |
| 1502 | "csrc/rocm/attention.cu") |
| 1503 | set(VLLM_ROCM_EXT_FLAGS ${VLLM_GPU_FLAGS}) |
| 1504 | |
| 1505 | # skinny_gemms*.cu are built on gfx9/gfx11 ISA (MFMA, dot2/dot4, legacy |
| 1506 | # s_waitcnt asm) that gfx1250 (gfx12) does not provide. Exclude them from the |
| 1507 | # gfx1250 build and disable their op registrations (VLLM_SKIP_SKINNY_GEMMS); |
| 1508 | # vLLM falls back to default/Triton GEMM for those ops on gfx1250. |
| 1509 | list(REMOVE_ITEM VLLM_ROCM_EXT_SRC "csrc/rocm/skinny_gemms.cu") |
| 1510 | list(REMOVE_ITEM VLLM_ROCM_EXT_SRC "csrc/rocm/skinny_gemms_int4.cu") |
| 1511 | set(VLLM_SKINNY_ARCHES ${VLLM_GPU_ARCHES}) |
| 1512 | list(FILTER VLLM_SKINNY_ARCHES EXCLUDE REGEX "gfx1250") |
| 1513 | if(VLLM_SKINNY_ARCHES) |
| 1514 | message(STATUS "Building skinny_gemms for archs: ${VLLM_SKINNY_ARCHES}") |
| 1515 | set(VLLM_SKINNY_SRC |
| 1516 | "csrc/rocm/skinny_gemms.cu" |
| 1517 | "csrc/rocm/skinny_gemms_int4.cu") |
| 1518 | hipify_sources_target(VLLM_SKINNY_HIP_SRCS _rocm_C_skinny "${VLLM_SKINNY_SRC}") |
| 1519 | unset(_VLLM_LAST_HIPIFY_TARGET) |
| 1520 | add_library(_rocm_C_skinny OBJECT ${VLLM_SKINNY_HIP_SRCS}) |
| 1521 | add_dependencies(_rocm_C_skinny hipify_all) |
| 1522 | set_source_files_properties(${VLLM_SKINNY_HIP_SRCS} PROPERTIES LANGUAGE ${VLLM_GPU_LANG}) |
| 1523 | set_target_properties(_rocm_C_skinny PROPERTIES |
| 1524 | ${VLLM_GPU_LANG}_ARCHITECTURES "${VLLM_SKINNY_ARCHES}" |
| 1525 | POSITION_INDEPENDENT_CODE ON) |
| 1526 | target_include_directories(_rocm_C_skinny PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/csrc) |
| 1527 | target_compile_options(_rocm_C_skinny PRIVATE |
| 1528 | $<$<COMPILE_LANGUAGE:${VLLM_GPU_LANG}>:${VLLM_ROCM_EXT_FLAGS}>) |
| 1529 | target_compile_definitions(_rocm_C_skinny PRIVATE "-DTORCH_EXTENSION_NAME=_rocm_C") |
| 1530 | target_link_libraries(_rocm_C_skinny PRIVATE torch) |
| 1531 | else() |
| 1532 | message(STATUS "Only gfx1250, skipping skinny_gemms") |
| 1533 | list(APPEND VLLM_ROCM_EXT_FLAGS "-DVLLM_SKIP_SKINNY_GEMMS") |
| 1534 | endif() |
| 1535 | |
| 1536 | set(VLLM_ROCM_HAS_GFX1100 OFF) |
| 1537 | if(VLLM_GPU_ARCHES MATCHES "gfx1100") |
| 1538 | set(VLLM_ROCM_HAS_GFX1100 ON) |
| 1539 | list(APPEND VLLM_ROCM_EXT_SRC |
| 1540 | "csrc/rocm/q_gemm_rdna3.cu" |
| 1541 | "csrc/rocm/q_gemm_rdna3_wmma.cu" |
| 1542 | "csrc/rocm/moe_q_gemm_rdna3.cu") |
| 1543 | endif() |
| 1544 | |
| 1545 | define_extension_target( |
| 1546 | _rocm_C |
| 1547 | DESTINATION vllm |
| 1548 | LANGUAGE ${VLLM_GPU_LANG} |
| 1549 | SOURCES ${VLLM_ROCM_EXT_SRC} |
| 1550 | COMPILE_FLAGS ${VLLM_ROCM_EXT_FLAGS} |
| 1551 | ARCHITECTURES ${VLLM_GPU_ARCHES} |
| 1552 | USE_SABI 3 |
| 1553 | WITH_SOABI) |
| 1554 | |
| 1555 | if(TARGET _rocm_C_skinny) |
| 1556 | target_link_libraries(_rocm_C PRIVATE _rocm_C_skinny) |
| 1557 | else() |
| 1558 | target_compile_definitions(_rocm_C PRIVATE VLLM_SKIP_SKINNY_GEMMS) |
| 1559 | endif() |
| 1560 | |
| 1561 | if(VLLM_ROCM_HAS_GFX1100) |
| 1562 | target_compile_definitions(_rocm_C PRIVATE VLLM_ROCM_GFX1100) |
| 1563 | endif() |
| 1564 | endif() |
| 1565 | |
| 1566 | # Must run after the last HIP `define_extension_target` so every extension |
| 1567 | # has registered its sources. |
| 1568 | if (VLLM_GPU_LANG STREQUAL "HIP") |
| 1569 | vllm_finalize_hipify_target() |
| 1570 | endif() |
| 1571 | |
| 1572 | # For CUDA and HIP builds also build the triton_kernels external package. |
| 1573 | if(VLLM_GPU_LANG STREQUAL "CUDA" OR VLLM_GPU_LANG STREQUAL "HIP") |
| 1574 | include(cmake/external_projects/triton_kernels.cmake) |
| 1575 | endif() |
| 1576 | |
| 1577 | # For CUDA we also build and ship some external projects. |
| 1578 | if (VLLM_GPU_LANG STREQUAL "CUDA") |
| 1579 | include(cmake/external_projects/deepgemm.cmake) |
| 1580 | include(cmake/external_projects/deepselect.cmake) |
| 1581 | include(cmake/external_projects/fmha_sm100.cmake) |
| 1582 | include(cmake/external_projects/flashmla.cmake) |
| 1583 | include(cmake/external_projects/flashkda.cmake) |
| 1584 | include(cmake/external_projects/qutlass.cmake) |
| 1585 | include(cmake/external_projects/tml_fa4.cmake) |
| 1586 | |
| 1587 | # vllm-flash-attn should be last as it overwrites some CMake functions |
| 1588 | include(cmake/external_projects/vllm_flash_attn.cmake) |
| 1589 | endif () |