Signed-off-by: Lucas Wilkinson <lwilkinson@neuralmagic.com>
This commit is contained in:
Lucas Wilkinson 2025-05-15 04:41:51 +00:00
parent 6b6d496114
commit b478b18f07
2 changed files with 230 additions and 360 deletions

View File

@ -168,7 +168,6 @@ if(NVCC_THREADS AND VLLM_GPU_LANG STREQUAL "CUDA")
list(APPEND VLLM_GPU_FLAGS "--threads=${NVCC_THREADS}") list(APPEND VLLM_GPU_FLAGS "--threads=${NVCC_THREADS}")
endif() endif()
# #
# Use FetchContent for C++ dependencies that are compiled as part of vLLM's build process. # Use FetchContent for C++ dependencies that are compiled as part of vLLM's build process.
# setup.py will override FETCHCONTENT_BASE_DIR to play nicely with sccache. # setup.py will override FETCHCONTENT_BASE_DIR to play nicely with sccache.
@ -308,148 +307,70 @@ if(VLLM_GPU_LANG STREQUAL "CUDA")
# Keep building Marlin for 9.0 as there are some group sizes and shapes that # Keep building Marlin for 9.0 as there are some group sizes and shapes that
# are not supported by Machete yet. # are not supported by Machete yet.
# 9.0 for latest bf16 atomicAdd PTX # 9.0 for latest bf16 atomicAdd PTX
cuda_archs_loose_intersection(MARLIN_ARCHS "8.0;9.0+PTX" "${CUDA_ARCHS}") # Marlin kernels: generate and build for supported architectures
if (MARLIN_ARCHS) cuda_archs_loose_intersection(MARLIN_ARCHS "8.0+PTX" "${CUDA_ARCHS}")
optional_cuda_sources(
NAME Marlin
ARCHS "8.0;9.0+PTX"
GEN_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/csrc/quantization/gptq_marlin/generate_kernels.py"
GEN_GLOB "csrc/quantization/gptq_marlin/kernel_*.cu"
SRCS
"csrc/quantization/marlin/dense/marlin_cuda_kernel.cu"
"csrc/quantization/marlin/sparse/marlin_24_cuda_kernel.cu"
"csrc/quantization/marlin/qqq/marlin_qqq_gemm_kernel.cu"
"csrc/quantization/gptq_marlin/gptq_marlin.cu"
"csrc/quantization/gptq_marlin/gptq_marlin_repack.cu"
"csrc/quantization/gptq_marlin/awq_marlin_repack.cu"
NO_ARCH_MSG
"Not building Marlin kernels as no compatible archs found in CUDA target architectures"
)
# # AllSpark kernels
# For the Marlin kernels we automatically generate sources for various optional_cuda_sources(
# preselected input type pairs and schedules. NAME AllSpark
# Generate sources: ARCHS "8.0;8.6;8.7;8.9"
set(MARLIN_GEN_SCRIPT SRCS
${CMAKE_CURRENT_SOURCE_DIR}/csrc/quantization/gptq_marlin/generate_kernels.py) "csrc/quantization/gptq_allspark/allspark_repack.cu"
file(MD5 ${MARLIN_GEN_SCRIPT} MARLIN_GEN_SCRIPT_HASH) "csrc/quantization/gptq_allspark/allspark_qgemm_w8a16.cu"
NO_ARCH_MSG "Not building AllSpark kernels as no compatible archs found in CUDA target architectures"
message(STATUS "Marlin generation script hash: ${MARLIN_GEN_SCRIPT_HASH}") )
message(STATUS "Last run Marlin generate script hash: $CACHE{MARLIN_GEN_SCRIPT_HASH}")
if (NOT DEFINED CACHE{MARLIN_GEN_SCRIPT_HASH}
OR NOT $CACHE{MARLIN_GEN_SCRIPT_HASH} STREQUAL ${MARLIN_GEN_SCRIPT_HASH})
execute_process(
COMMAND ${CMAKE_COMMAND} -E env
PYTHONPATH=$PYTHONPATH
${Python_EXECUTABLE} ${MARLIN_GEN_SCRIPT}
RESULT_VARIABLE marlin_generation_result
OUTPUT_VARIABLE marlin_generation_result
OUTPUT_FILE ${CMAKE_CURRENT_BINARY_DIR}/marlin_generation.log
ERROR_FILE ${CMAKE_CURRENT_BINARY_DIR}/marlin_generation.log
)
if (NOT marlin_generation_result EQUAL 0)
message(FATAL_ERROR "Marlin generation failed."
" Result: \"${marlin_generation_result}\""
"\nCheck the log for details: "
"${CMAKE_CURRENT_BINARY_DIR}/marlin_generation.log")
else()
set(MARLIN_GEN_SCRIPT_HASH ${MARLIN_GEN_SCRIPT_HASH}
CACHE STRING "Last run Marlin generate script hash" FORCE)
message(STATUS "Marlin generation completed successfully.")
endif()
else()
message(STATUS "Marlin generation script has not changed, skipping generation.")
endif()
file(GLOB MARLIN_TEMPLATE_KERNEL_SRC "csrc/quantization/gptq_marlin/kernel_*.cu")
set_gencode_flags_for_srcs(
SRCS "${MARLIN_TEMPLATE_KERNEL_SRC}"
CUDA_ARCHS "${MARLIN_ARCHS}")
list(APPEND VLLM_EXT_SRC ${MARLIN_TEMPLATE_KERNEL_SRC})
set(MARLIN_SRCS
"csrc/quantization/marlin/dense/marlin_cuda_kernel.cu"
"csrc/quantization/marlin/sparse/marlin_24_cuda_kernel.cu"
"csrc/quantization/marlin/qqq/marlin_qqq_gemm_kernel.cu"
"csrc/quantization/gptq_marlin/gptq_marlin.cu"
"csrc/quantization/gptq_marlin/gptq_marlin_repack.cu"
"csrc/quantization/gptq_marlin/awq_marlin_repack.cu")
set_gencode_flags_for_srcs(
SRCS "${MARLIN_SRCS}"
CUDA_ARCHS "${MARLIN_ARCHS}")
list(APPEND VLLM_EXT_SRC "${MARLIN_SRCS}")
message(STATUS "Building Marlin kernels for archs: ${MARLIN_ARCHS}")
else()
message(STATUS "Not building Marlin kernels as no compatible archs found"
" in CUDA target architectures")
endif()
# Only build AllSpark kernels if we are building for at least some compatible archs.
cuda_archs_loose_intersection(ALLSPARK_ARCHS "8.0;8.6;8.7;8.9" "${CUDA_ARCHS}")
if (ALLSPARK_ARCHS)
set(ALLSPARK_SRCS
"csrc/quantization/gptq_allspark/allspark_repack.cu"
"csrc/quantization/gptq_allspark/allspark_qgemm_w8a16.cu")
set_gencode_flags_for_srcs(
SRCS "${ALLSPARK_SRCS}"
CUDA_ARCHS "${ALLSPARK_ARCHS}")
list(APPEND VLLM_EXT_SRC "${ALLSPARK_SRCS}")
message(STATUS "Building AllSpark kernels for archs: ${ALLSPARK_ARCHS}")
else()
message(STATUS "Not building AllSpark kernels as no compatible archs found"
" in CUDA target architectures")
endif()
set(SCALED_MM_3X_ARCHS) set(SCALED_MM_3X_ARCHS)
# The cutlass_scaled_mm kernels for Hopper (c3x, i.e. CUTLASS 3.x) require # The cutlass_scaled_mm kernels for Hopper (c3x, i.e. CUTLASS 3.x) require CUDA 12.0 or later
# CUDA 12.0 or later optional_cuda_sources(
cuda_archs_loose_intersection(SCALED_MM_ARCHS "9.0a;" "${CUDA_ARCHS}") NAME scaled_mm_c3x_sm90
if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER 12.0 AND SCALED_MM_ARCHS) MIN_VERSION 12.0
set(SRCS ARCHS "9.0a"
"csrc/quantization/cutlass_w8a8/scaled_mm_c3x_sm90.cu" SRCS
"csrc/quantization/cutlass_w8a8/c3x/scaled_mm_sm90_fp8.cu" "csrc/quantization/cutlass_w8a8/scaled_mm_c3x_sm90.cu"
"csrc/quantization/cutlass_w8a8/c3x/scaled_mm_sm90_int8.cu" "csrc/quantization/cutlass_w8a8/c3x/scaled_mm_sm90_fp8.cu"
"csrc/quantization/cutlass_w8a8/c3x/scaled_mm_azp_sm90_int8.cu" "csrc/quantization/cutlass_w8a8/c3x/scaled_mm_sm90_int8.cu"
"csrc/quantization/cutlass_w8a8/c3x/scaled_mm_blockwise_sm90_fp8.cu") "csrc/quantization/cutlass_w8a8/c3x/scaled_mm_azp_sm90_int8.cu"
set_gencode_flags_for_srcs( "csrc/quantization/cutlass_w8a8/c3x/scaled_mm_blockwise_sm90_fp8.cu"
SRCS "${SRCS}" FLAGS "-DENABLE_SCALED_MM_SM90=1"
CUDA_ARCHS "${SCALED_MM_ARCHS}") APPEND_ARCHS SCALED_MM_3X_ARCHS
list(APPEND VLLM_EXT_SRC "${SRCS}") VERSION_MSG
list(APPEND VLLM_GPU_FLAGS "-DENABLE_SCALED_MM_SM90=1") "Not building scaled_mm_c3x_sm90: CUDA Compiler version is not >= 12.0.\n"
# Let scaled_mm_c2x know it doesn't need to build these arches "Please upgrade to CUDA 12.0 or later to run FP8 quantized models on Hopper."
list(APPEND SCALED_MM_3X_ARCHS "${SCALED_MM_ARCHS}") )
message(STATUS "Building scaled_mm_c3x_sm90 for archs: ${SCALED_MM_ARCHS}")
else()
if (NOT ${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER 12.0 AND SCALED_MM_ARCHS)
message(STATUS "Not building scaled_mm_c3x_sm90 as CUDA Compiler version is "
"not >= 12.0, we recommend upgrading to CUDA 12.0 or "
"later if you intend on running FP8 quantized models on "
"Hopper.")
else()
message(STATUS "Not building scaled_mm_c3x_sm90 as no compatible archs found "
"in CUDA target architectures")
endif()
endif()
# The cutlass_scaled_mm kernels for Blackwell (c3x, i.e. CUTLASS 3.x) require # The cutlass_scaled_mm kernels for Blackwell (c3x, i.e. CUTLASS 3.x) require CUDA 12.8 or later
# CUDA 12.8 or later optional_cuda_sources(
cuda_archs_loose_intersection(SCALED_MM_ARCHS "10.0a;10.1a;12.0a" "${CUDA_ARCHS}") NAME scaled_mm_c3x_sm100
if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER 12.8 AND SCALED_MM_ARCHS) MIN_VERSION 12.8
set(SRCS ARCHS "10.0a;10.1a;12.0a"
SRCS
"csrc/quantization/cutlass_w8a8/scaled_mm_c3x_sm100.cu" "csrc/quantization/cutlass_w8a8/scaled_mm_c3x_sm100.cu"
"csrc/quantization/cutlass_w8a8/c3x/scaled_mm_sm100_fp8.cu" "csrc/quantization/cutlass_w8a8/c3x/scaled_mm_sm100_fp8.cu"
"csrc/quantization/cutlass_w8a8/c3x/scaled_mm_blockwise_sm100_fp8.cu" "csrc/quantization/cutlass_w8a8/c3x/scaled_mm_blockwise_sm100_fp8.cu"
) FLAGS "-DENABLE_SCALED_MM_SM100=1"
set_gencode_flags_for_srcs( APPEND_ARCHS SCALED_MM_3X_ARCHS
SRCS "${SRCS}" VERSION_MSG
CUDA_ARCHS "${SCALED_MM_ARCHS}") "Not building scaled_mm_c3x_sm100: CUDA Compiler version is not >= 12.8.\n"
list(APPEND VLLM_EXT_SRC "${SRCS}") "Please upgrade to CUDA 12.8 or later to run FP8 quantized models on Blackwell."
list(APPEND VLLM_GPU_FLAGS "-DENABLE_SCALED_MM_SM100=1") )
# Let scaled_mm_c2x know it doesn't need to build these arches
list(APPEND SCALED_MM_3X_ARCHS "${SCALED_MM_ARCHS}")
message(STATUS "Building scaled_mm_c3x_sm100 for archs: ${SCALED_MM_ARCHS}")
else()
if (NOT ${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER 12.8 AND SCALED_MM_ARCHS)
message(STATUS "Not building scaled_mm_c3x_sm100 as CUDA Compiler version is "
"not >= 12.8, we recommend upgrading to CUDA 12.8 or "
"later if you intend on running FP8 quantized models on "
"Blackwell.")
else()
message(STATUS "Not building scaled_mm_c3x_100 as no compatible archs found "
"in CUDA target architectures")
endif()
endif()
#
# For the cutlass_scaled_mm kernels we want to build the c2x (CUTLASS 2.x) # For the cutlass_scaled_mm kernels we want to build the c2x (CUTLASS 2.x)
# kernels for the remaining archs that are not already built for 3x. # kernels for the remaining archs that are not already built for 3x.
# (Build 8.9 for FP8) # (Build 8.9 for FP8)
@ -457,184 +378,87 @@ if(VLLM_GPU_LANG STREQUAL "CUDA")
"7.5;8.0;8.9+PTX" "${CUDA_ARCHS}") "7.5;8.0;8.9+PTX" "${CUDA_ARCHS}")
# subtract out the archs that are already built for 3x # subtract out the archs that are already built for 3x
list(REMOVE_ITEM SCALED_MM_2X_ARCHS ${SCALED_MM_3X_ARCHS}) list(REMOVE_ITEM SCALED_MM_2X_ARCHS ${SCALED_MM_3X_ARCHS})
if (SCALED_MM_2X_ARCHS) optional_cuda_sources(
set(SRCS "csrc/quantization/cutlass_w8a8/scaled_mm_c2x.cu") NAME scaled_mm_c2x
set_gencode_flags_for_srcs( ARCHS "${SCALED_MM_2X_ARCHS}"
SRCS "${SRCS}" SRCS "csrc/quantization/cutlass_w8a8/scaled_mm_c2x.cu"
CUDA_ARCHS "${SCALED_MM_2X_ARCHS}") FLAGS "-DENABLE_SCALED_MM_C2X=1"
list(APPEND VLLM_EXT_SRC "${SRCS}") NO_ARCH_MSG "Not building scaled_mm_c2x as no compatible archs found in CUDA target architectures, "
list(APPEND VLLM_GPU_FLAGS "-DENABLE_SCALED_MM_C2X=1") "or is already covered by scaled_mm_c3x."
message(STATUS "Building scaled_mm_c2x for archs: ${SCALED_MM_2X_ARCHS}") )
else()
if (SCALED_MM_3X_ARCHS)
message(STATUS "Not building scaled_mm_c2x as all archs are already built"
" for and covered by scaled_mm_c3x")
else()
message(STATUS "Not building scaled_mm_c2x as no compatible archs found "
"in CUDA target architectures")
endif()
endif()
# #
# 2:4 Sparse Kernels # 2:4 Sparse Kernels
optional_cuda_sources(
# The 2:4 sparse kernels cutlass_scaled_sparse_mm and cutlass_compressor NAME sparse_scaled_mm_c3x
# require CUDA 12.2 or later (and only work on Hopper). MIN_VERSION 12.2
cuda_archs_loose_intersection(SCALED_MM_ARCHS "9.0a;" "${CUDA_ARCHS}") ARCHS "9.0a;"
if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER 12.2 AND SCALED_MM_ARCHS) SRCS "csrc/sparse/cutlass/sparse_scaled_mm_c3x.cu"
set(SRCS "csrc/sparse/cutlass/sparse_scaled_mm_c3x.cu") FLAGS "-DENABLE_SPARSE_SCALED_MM_C3X=1"
set_gencode_flags_for_srcs( VERSION_MSG
SRCS "${SRCS}" "Not building sparse_scaled_mm_c3x: CUDA Compiler version is not >= 12.2.\n"
CUDA_ARCHS "${SCALED_MM_ARCHS}") "Please upgrade to CUDA 12.2 or later to run FP8 sparse quantized models on Hopper."
list(APPEND VLLM_EXT_SRC "${SRCS}") )
list(APPEND VLLM_GPU_FLAGS "-DENABLE_SPARSE_SCALED_MM_C3X=1")
message(STATUS "Building sparse_scaled_mm_c3x for archs: ${SCALED_MM_ARCHS}")
else()
if (NOT ${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER 12.2 AND SCALED_MM_ARCHS)
message(STATUS "Not building sparse_scaled_mm_c3x kernels as CUDA Compiler version is "
"not >= 12.2, we recommend upgrading to CUDA 12.2 or later "
"if you intend on running FP8 sparse quantized models on Hopper.")
else()
message(STATUS "Not building sparse_scaled_mm_c3x as no compatible archs found "
"in CUDA target architectures")
endif()
endif()
# FP4 Archs and flags # FP4 Archs and flags
cuda_archs_loose_intersection(FP4_ARCHS "10.0a" "${CUDA_ARCHS}") optional_cuda_sources(
if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER 12.8 AND FP4_ARCHS) NAME NVFP4
set(SRCS MIN_VERSION 12.8
ARCHS "10.0a"
SRCS
"csrc/quantization/fp4/nvfp4_quant_kernels.cu" "csrc/quantization/fp4/nvfp4_quant_kernels.cu"
"csrc/quantization/fp4/nvfp4_experts_quant.cu" "csrc/quantization/fp4/nvfp4_experts_quant.cu"
"csrc/quantization/fp4/nvfp4_scaled_mm_kernels.cu" "csrc/quantization/fp4/nvfp4_scaled_mm_kernels.cu"
"csrc/quantization/fp4/nvfp4_blockwise_moe_kernel.cu") "csrc/quantization/fp4/nvfp4_blockwise_moe_kernel.cu"
set_gencode_flags_for_srcs( FLAGS "-DENABLE_NVFP4=1"
SRCS "${SRCS}" NO_ARCH_MSG "Not building NVFP4 as no compatible archs were found."
CUDA_ARCHS "${FP4_ARCHS}") )
list(APPEND VLLM_EXT_SRC "${SRCS}")
list(APPEND VLLM_GPU_FLAGS "-DENABLE_NVFP4=1")
message(STATUS "Building NVFP4 for archs: ${FP4_ARCHS}")
else()
message(STATUS "Not building NVFP4 as no compatible archs were found.")
# clear FP4_ARCHS
set(FP4_ARCHS)
endif()
# CUTLASS MLA Archs and flags # CUTLASS MLA Archs and flags
cuda_archs_loose_intersection(MLA_ARCHS "10.0a" "${CUDA_ARCHS}") optional_cuda_sources(
if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER 12.8 AND MLA_ARCHS) NAME CUTLASS_MLA
set(SRCS MIN_VERSION 12.8
"csrc/attention/mla/cutlass_mla_kernels.cu") ARCHS "10.0a"
set_gencode_flags_for_srcs( SRCS "csrc/attention/mla/cutlass_mla_kernels.cu"
SRCS "${SRCS}" FLAGS "-DENABLE_CUTLASS_MLA=1"
CUDA_ARCHS "${MLA_ARCHS}") NO_ARCH_MSG "Not building CUTLASS MLA as no compatible archs were found."
list(APPEND VLLM_EXT_SRC "${SRCS}") )
list(APPEND VLLM_GPU_FLAGS "-DENABLE_CUTLASS_MLA=1") # Add MLA-specific include directories only to MLA source files
# Add MLA-specific include directories only to MLA source files set_source_files_properties(
set_source_files_properties(${SRCS} "csrc/attention/mla/cutlass_mla_kernels.cu"
PROPERTIES INCLUDE_DIRECTORIES "${CUTLASS_DIR}/examples/77_blackwell_fmha;${CUTLASS_DIR}/examples/common") PROPERTIES INCLUDE_DIRECTORIES "${CUTLASS_DIR}/examples/77_blackwell_fmha;${CUTLASS_DIR}/examples/common"
message(STATUS "Building CUTLASS MLA for archs: ${MLA_ARCHS}") )
else()
message(STATUS "Not building CUTLASS MLA as no compatible archs were found.")
# clear MLA_ARCHS
set(MLA_ARCHS)
endif()
# CUTLASS MoE kernels # CUTLASS MoE kernels
optional_cuda_sources(
# The MoE kernel cutlass_moe_mm requires CUDA 12.3 or later (and only works NAME grouped_mm_c3x
# on Hopper). get_cutlass_moe_mm_data should only be compiled if it's possible MIN_VERSION 12.3
# to compile MoE kernels that use its output. ARCHS "9.0a;10.0a"
cuda_archs_loose_intersection(SCALED_MM_ARCHS "9.0a;10.0a" "${CUDA_ARCHS}") SRCS
if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 12.3 AND SCALED_MM_ARCHS) "csrc/quantization/cutlass_w8a8/moe/grouped_mm_c3x.cu"
set(SRCS "csrc/quantization/cutlass_w8a8/moe/grouped_mm_c3x.cu" "csrc/quantization/cutlass_w8a8/moe/moe_data.cu"
"csrc/quantization/cutlass_w8a8/moe/moe_data.cu") FLAGS "-DENABLE_CUTLASS_MOE_SM90=1"
set_gencode_flags_for_srcs( VERSION_MSG "Not building grouped_mm_c3x kernels as CUDA Compiler version is not >= 12.3, we recommend upgrading to CUDA 12.3 or later if you intend on running FP8 quantized MoE models on Hopper."
SRCS "${SRCS}" )
CUDA_ARCHS "${SCALED_MM_ARCHS}")
list(APPEND VLLM_EXT_SRC "${SRCS}")
list(APPEND VLLM_GPU_FLAGS "-DENABLE_CUTLASS_MOE_SM90=1")
message(STATUS "Building grouped_mm_c3x for archs: ${SCALED_MM_ARCHS}")
else()
if (NOT ${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER_EQUAL 12.3 AND SCALED_MM_ARCHS)
message(STATUS "Not building grouped_mm_c3x kernels as CUDA Compiler version is "
"not >= 12.3, we recommend upgrading to CUDA 12.3 or later "
"if you intend on running FP8 quantized MoE models on Hopper.")
else()
message(STATUS "Not building grouped_mm_c3x as no compatible archs found "
"in CUDA target architectures")
endif()
endif()
# #
# Machete kernels # Machete kernels
# The machete kernels only work on hopper and require CUDA 12.0 or later. # Machete kernels: generate and build for supported architectures
# Only build Machete kernels if we are building for something compatible with sm90a
cuda_archs_loose_intersection(MACHETE_ARCHS "9.0a" "${CUDA_ARCHS}") cuda_archs_loose_intersection(MACHETE_ARCHS "9.0a" "${CUDA_ARCHS}")
if(${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER 12.0 AND MACHETE_ARCHS) optional_cuda_sources(
# NAME Machete
# For the Machete kernels we automatically generate sources for various MIN_VERSION 12.0
# preselected input type pairs and schedules. ARCHS "${MACHETE_ARCHS}"
# Generate sources: GEN_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/csrc/quantization/machete/generate.py"
set(MACHETE_GEN_SCRIPT GEN_GLOB "csrc/quantization/machete/generated/*.cu"
${CMAKE_CURRENT_SOURCE_DIR}/csrc/quantization/machete/generate.py) SRCS "csrc/quantization/machete/machete_pytorch.cu"
file(MD5 ${MACHETE_GEN_SCRIPT} MACHETE_GEN_SCRIPT_HASH) VERSION_MSG
"Not building Machete kernels as CUDA Compiler version is less than 12.0."
message(STATUS "Machete generation script hash: ${MACHETE_GEN_SCRIPT_HASH}") "We recommend upgrading to CUDA 12.0 or later to run w4a16 quantized models on Hopper."
message(STATUS "Last run machete generate script hash: $CACHE{MACHETE_GEN_SCRIPT_HASH}") NO_ARCH_MSG
"Not building Machete kernels as no compatible archs found in CUDA target architectures"
if (NOT DEFINED CACHE{MACHETE_GEN_SCRIPT_HASH} )
OR NOT $CACHE{MACHETE_GEN_SCRIPT_HASH} STREQUAL ${MACHETE_GEN_SCRIPT_HASH})
execute_process(
COMMAND ${CMAKE_COMMAND} -E env
PYTHONPATH=${CMAKE_CURRENT_SOURCE_DIR}/csrc/cutlass_extensions/:${CUTLASS_DIR}/python/:${VLLM_PYTHON_PATH}:$PYTHONPATH
${Python_EXECUTABLE} ${MACHETE_GEN_SCRIPT}
RESULT_VARIABLE machete_generation_result
OUTPUT_VARIABLE machete_generation_output
OUTPUT_FILE ${CMAKE_CURRENT_BINARY_DIR}/machete_generation.log
ERROR_FILE ${CMAKE_CURRENT_BINARY_DIR}/machete_generation.log
)
if (NOT machete_generation_result EQUAL 0)
message(FATAL_ERROR "Machete generation failed."
" Result: \"${machete_generation_result}\""
"\nCheck the log for details: "
"${CMAKE_CURRENT_BINARY_DIR}/machete_generation.log")
else()
set(MACHETE_GEN_SCRIPT_HASH ${MACHETE_GEN_SCRIPT_HASH}
CACHE STRING "Last run machete generate script hash" FORCE)
message(STATUS "Machete generation completed successfully.")
endif()
else()
message(STATUS "Machete generation script has not changed, skipping generation.")
endif()
# Add machete generated sources
file(GLOB MACHETE_GEN_SOURCES "csrc/quantization/machete/generated/*.cu")
list(APPEND VLLM_EXT_SRC ${MACHETE_GEN_SOURCES})
# forward compatible
set_gencode_flags_for_srcs(
SRCS "${MACHETE_GEN_SOURCES}"
CUDA_ARCHS "${MACHETE_ARCHS}")
list(APPEND VLLM_EXT_SRC
csrc/quantization/machete/machete_pytorch.cu)
message(STATUS "Building Machete kernels for archs: ${MACHETE_ARCHS}")
else()
if (NOT ${CMAKE_CUDA_COMPILER_VERSION} VERSION_GREATER 12.0
AND MACHETE_ARCHS)
message(STATUS "Not building Machete kernels as CUDA Compiler version is "
"not >= 12.0, we recommend upgrading to CUDA 12.0 or "
"later if you intend on running w4a16 quantized models on "
"Hopper.")
else()
message(STATUS "Not building Machete kernels as no compatible archs "
"found in CUDA target architectures")
endif()
endif()
# if CUDA endif # if CUDA endif
endif() endif()
@ -666,76 +490,22 @@ set(VLLM_MOE_EXT_SRC
"csrc/moe/moe_align_sum_kernels.cu" "csrc/moe/moe_align_sum_kernels.cu"
"csrc/moe/topk_softmax_kernels.cu") "csrc/moe/topk_softmax_kernels.cu")
if(VLLM_GPU_LANG STREQUAL "CUDA") # Apply gencode flags to base MOE extension sources
list(APPEND VLLM_MOE_EXT_SRC "csrc/moe/moe_wna16.cu")
endif()
set_gencode_flags_for_srcs( set_gencode_flags_for_srcs(
SRCS "${VLLM_MOE_EXT_SRC}" SRCS "${VLLM_MOE_EXT_SRC}"
CUDA_ARCHS "${CUDA_ARCHS}") CUDA_ARCHS "${CUDA_ARCHS}")
## Marlin MOE kernels: generate and include for supported architectures
if(VLLM_GPU_LANG STREQUAL "CUDA") if(VLLM_GPU_LANG STREQUAL "CUDA")
set(VLLM_MOE_WNA16_SRC optional_cuda_sources(
"csrc/moe/moe_wna16.cu") NAME "Marlin MOE"
ARCHS "8.0;9.0+PTX"
set_gencode_flags_for_srcs( GEN_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/csrc/moe/marlin_moe_wna16/generate_kernels.py"
SRCS "${VLLM_MOE_WNA16_SRC}" GEN_GLOB "csrc/moe/marlin_moe_wna16/*.cu"
CUDA_ARCHS "${CUDA_ARCHS}") SRCS "csrc/moe/moe_wna16.cu"
NO_ARCH_MSG "Not building Marlin MOE kernels as no compatible archs found in CUDA target architectures"
list(APPEND VLLM_MOE_EXT_SRC "${VLLM_MOE_WNA16_SRC}") OUT_SRCS_VAR VLLM_MOE_EXT_SRC
# 9.0 for latest bf16 atomicAdd PTX )
cuda_archs_loose_intersection(MARLIN_MOE_ARCHS "8.0;9.0+PTX" "${CUDA_ARCHS}")
if (MARLIN_MOE_ARCHS)
#
# For the Marlin MOE kernels we automatically generate sources for various
# preselected input type pairs and schedules.
# Generate sources:
set(MOE_MARLIN_GEN_SCRIPT
${CMAKE_CURRENT_SOURCE_DIR}/csrc/moe/marlin_moe_wna16/generate_kernels.py)
file(MD5 ${MOE_MARLIN_GEN_SCRIPT} MOE_MARLIN_GEN_SCRIPT_HASH)
message(STATUS "Marlin MOE generation script hash: ${MOE_MARLIN_GEN_SCRIPT_HASH}")
message(STATUS "Last run Marlin MOE generate script hash: $CACHE{MOE_MARLIN_GEN_SCRIPT_HASH}")
if (NOT DEFINED CACHE{MOE_MARLIN_GEN_SCRIPT_HASH}
OR NOT $CACHE{MOE_MARLIN_GEN_SCRIPT_HASH} STREQUAL ${MOE_MARLIN_GEN_SCRIPT_HASH})
execute_process(
COMMAND ${CMAKE_COMMAND} -E env
PYTHONPATH=$PYTHONPATH
${Python_EXECUTABLE} ${MOE_MARLIN_GEN_SCRIPT}
RESULT_VARIABLE moe_marlin_generation_result
OUTPUT_VARIABLE moe_marlin_generation_output
OUTPUT_FILE ${CMAKE_CURRENT_BINARY_DIR}/moe_marlin_generation.log
ERROR_FILE ${CMAKE_CURRENT_BINARY_DIR}/moe_marlin_generation.log
)
if (NOT moe_marlin_generation_result EQUAL 0)
message(FATAL_ERROR "Marlin MOE generation failed."
" Result: \"${moe_marlin_generation_result}\""
"\nCheck the log for details: "
"${CMAKE_CURRENT_BINARY_DIR}/moe_marlin_generation.log")
else()
set(MOE_MARLIN_GEN_SCRIPT_HASH ${MOE_MARLIN_GEN_SCRIPT_HASH}
CACHE STRING "Last run Marlin MOE generate script hash" FORCE)
message(STATUS "Marlin MOE generation completed successfully.")
endif()
else()
message(STATUS "Marlin MOE generation script has not changed, skipping generation.")
endif()
file(GLOB MOE_WNAA16_MARLIN_SRC "csrc/moe/marlin_moe_wna16/*.cu")
set_gencode_flags_for_srcs(
SRCS "${MOE_WNAA16_MARLIN_SRC}"
CUDA_ARCHS "${MARLIN_MOE_ARCHS}")
list(APPEND VLLM_MOE_EXT_SRC ${MOE_WNAA16_MARLIN_SRC})
message(STATUS "Building Marlin MOE kernels for archs: ${MARLIN_MOE_ARCHS}")
else()
message(STATUS "Not building Marlin MOE kernels as no compatible archs found"
" in CUDA target architectures")
endif()
endif() endif()
if(VLLM_GPU_LANG STREQUAL "CUDA") if(VLLM_GPU_LANG STREQUAL "CUDA")

View File

@ -39,6 +39,34 @@ function (run_python OUT EXPR ERR_MSG)
set(${OUT} ${PYTHON_OUT} PARENT_SCOPE) set(${OUT} ${PYTHON_OUT} PARENT_SCOPE)
endfunction() endfunction()
# Generate CUDA sources via a Python script with caching based on script hash
function(generate_cuda_sources NAME SCRIPT GLOB OUT_SRCS)
string(TOUPPER "${NAME}" _UPPER_NAME)
set(_CACHE_VAR "${_UPPER_NAME}_GEN_SCRIPT_HASH")
file(MD5 "${SCRIPT}" _GEN_HASH)
message(STATUS "${NAME} generation script hash: ${_GEN_HASH}")
message(STATUS "Last run ${NAME} generation script hash: $CACHE{${_CACHE_VAR}}")
if(NOT DEFINED CACHE{${_CACHE_VAR}} OR NOT $CACHE{${_CACHE_VAR}} STREQUAL "${_GEN_HASH}")
execute_process(
COMMAND ${CMAKE_COMMAND} -E env
PYTHONPATH=$ENV{PYTHONPATH}
${Python_EXECUTABLE} "${SCRIPT}"
RESULT_VARIABLE _GEN_RESULT
OUTPUT_FILE "${CMAKE_CURRENT_BINARY_DIR}/${NAME}_generation.log"
ERROR_FILE "${CMAKE_CURRENT_BINARY_DIR}/${NAME}_generation.log"
)
if(NOT _GEN_RESULT EQUAL 0)
message(FATAL_ERROR "${NAME} generation failed. Result: \"${_GEN_RESULT}\"\nCheck the log for details: ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_generation.log")
else()
set(${_CACHE_VAR} "${_GEN_HASH}" CACHE STRING "Last run ${NAME} generation script hash" FORCE)
message(STATUS "${NAME} generation completed successfully.")
endif()
else()
message(STATUS "${NAME} generation script has not changed, skipping generation.")
endif()
file(GLOB ${OUT_SRCS} "${GLOB}")
endfunction()
# Run `EXPR` in python after importing `PKG`. Use the result of this to extend # Run `EXPR` in python after importing `PKG`. Use the result of this to extend
# `CMAKE_PREFIX_PATH` so the torch cmake configuration can be imported. # `CMAKE_PREFIX_PATH` so the torch cmake configuration can be imported.
macro (append_cmake_prefix_path PKG EXPR) macro (append_cmake_prefix_path PKG EXPR)
@ -86,6 +114,78 @@ function (hipify_sources_target OUT_SRCS NAME ORIG_SRCS)
set(${OUT_SRCS} ${HIP_SRCS} PARENT_SCOPE) set(${OUT_SRCS} ${HIP_SRCS} PARENT_SCOPE)
endfunction() endfunction()
## Macro to conditionally include CUDA sources based on architecture and CUDA
## compiler version, optionally generating sources via a Python script.
## Usage:
## optional_cuda_sources(
## NAME <name>
## [MIN_VERSION <version>]
## ARCHS <arch1;arch2;...>
## SRCS <static_source1> [<static_source2> ...]
## [FLAGS <flag1> ...]
## [VERSION_MSG <line1> [<line2> ...]]
## [NO_ARCH_MSG <line1> [<line2> ...]]
## [GEN_SCRIPT <path/to/generate_script.py>]
## [GEN_GLOB <glob_pattern_for_generated_sources>]
## [APPEND_ARCHS <var_to_append_archs>]
## This will run GEN_SCRIPT once when version and arch checks pass, globbing
## sources matching GEN_GLOB and appending them alongside SRCS.
macro(optional_cuda_sources)
set(oneValueArgs NAME MIN_VERSION APPEND_ARCHS GEN_SCRIPT GEN_GLOB OUT_SRCS_VAR)
set(multiValueArgs ARCHS SRCS FLAGS VERSION_MSG NO_ARCH_MSG)
cmake_parse_arguments(OCS "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if(NOT OCS_NAME)
message(FATAL_ERROR "optional_cuda_sources: NAME is required")
endif()
if(NOT OCS_ARCHS)
message(FATAL_ERROR "optional_cuda_sources ${OCS_NAME}: ARCHS is required")
endif()
if(NOT OCS_SRCS)
message(FATAL_ERROR "optional_cuda_sources ${OCS_NAME}: SRCS is required")
endif()
if(NOT OCS_MIN_VERSION)
set(OCS_MIN_VERSION "0.0")
endif()
cuda_archs_loose_intersection(_OCS_ARCHS "${OCS_ARCHS}" "${CUDA_ARCHS}")
if(CMAKE_CUDA_COMPILER_VERSION VERSION_GREATER_EQUAL ${OCS_MIN_VERSION})
if(_OCS_ARCHS)
set(_OCS_SRCS ${OCS_SRCS})
# Generate sources if a script is provided
if(OCS_GEN_SCRIPT AND OCS_GEN_GLOB)
generate_cuda_sources(${OCS_NAME} "${OCS_GEN_SCRIPT}" "${OCS_GEN_GLOB}" _OCS_GEN_SRCS)
list(APPEND _OCS_SRCS ${_OCS_GEN_SRCS})
endif()
set_gencode_flags_for_srcs(SRCS "${_OCS_SRCS}" CUDA_ARCHS "${_OCS_ARCHS}")
if(OCS_OUT_SRCS_VAR)
list(APPEND ${OCS_OUT_SRCS_VAR} ${_OCS_SRCS})
else()
list(APPEND VLLM_EXT_SRC ${_OCS_SRCS})
endif()
if(OCS_FLAGS)
list(APPEND VLLM_GPU_FLAGS ${OCS_FLAGS})
endif()
if(OCS_APPEND_ARCHS)
list(APPEND ${OCS_APPEND_ARCHS} ${_OCS_ARCHS})
endif()
message(STATUS "Building ${OCS_NAME} for archs: ${_OCS_ARCHS}")
else()
if(OCS_NO_ARCH_MSG)
list(JOIN OCS_NO_ARCH_MSG "\n" _OCS_NO_ARCH_JOINED)
message(STATUS "${_OCS_NO_ARCH_JOINED}")
else()
message(STATUS "Not building ${OCS_NAME}: no compatible architectures found in CUDA target architectures")
endif()
endif()
else()
if(OCS_VERSION_MSG)
list(JOIN OCS_VERSION_MSG "\n" _OCS_VERSION_JOINED)
message(STATUS "${_OCS_VERSION_JOINED}")
else()
message(STATUS "Not building ${OCS_NAME}: CUDA Compiler version is less than ${OCS_MIN_VERSION}")
endif()
endif()
endmacro()
# #
# Get additional GPU compiler flags from torch. # Get additional GPU compiler flags from torch.
# #