|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2022-2024 NVIDIA CORPORATION & |
| 2 | +# AFFILIATES. All rights reserved. SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | +# use this file except in compliance with the License. You may obtain a copy of |
| 6 | +# the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | +# License for the specific language governing permissions and limitations under |
| 14 | +# the License. |
| 15 | +cmake_minimum_required(VERSION 3.18) |
| 16 | +project(xqa LANGUAGES CXX CUDA) |
| 17 | + |
| 18 | +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) |
| 19 | +set(CMAKE_CXX_STANDARD 20) |
| 20 | +set(CMAKE_CXX_STANDARD_REQUIRED ON) |
| 21 | +set(CMAKE_CUDA_STANDARD 17) |
| 22 | +set(CMAKE_CUDA_ARCHITECTURES 89-real 90a-real) |
| 23 | +set(CMAKE_POSITION_INDEPENDENT_CODE ON) |
| 24 | + |
| 25 | +option(BUILD_XQA_TESTS "Build XQA tests" OFF) |
| 26 | + |
| 27 | +# todo: remove include_directories link_directories and link libs like |
| 28 | +# CUDA::cuda_driver CUDA::cudart CUDA::nvrtc |
| 29 | +find_package(CUDAToolkit REQUIRED) |
| 30 | + |
| 31 | +include_directories(${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}) |
| 32 | + |
| 33 | +link_directories(${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}/../lib64 |
| 34 | + ${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}/../lib) |
| 35 | + |
| 36 | +set(CMAKE_CXX_FLAGS |
| 37 | + "${CMAKE_CXX_FLAGS} -march=haswell -Wfatal-errors -Wreturn-type -Wall -Wextra -Wno-unknown-pragmas" |
| 38 | +) |
| 39 | +set(CMAKE_CUDA_FLAGS |
| 40 | + "${CMAKE_CUDA_FLAGS} -allow-unsupported-compiler --expt-relaxed-constexpr -t 0 -res-usage" |
| 41 | +) |
| 42 | +set(CUDA_PTXAS_FLAGS "-warn-lmem-usage -warn-double-usage -warn-spills" |
| 43 | +)# -Werror -v |
| 44 | +set(CMAKE_CUDA_FLAGS_RELEASE |
| 45 | + "${CMAKE_CUDA_FLAGS_RELEASE} -lineinfo -keep --use_fast_math -Xptxas='${CUDA_PTXAS_FLAGS}'" |
| 46 | +) |
| 47 | +set(CMAKE_CUDA_FLAGS_DEBUG "${CMAKE_CUDA_FLAGS_DEBUG} -O0 -G -keep") |
| 48 | +# add_definitions(-DSPEC_DEC) set(CMAKE_CUDA_FLAGS_DEBUG |
| 49 | +# "${CMAKE_CUDA_FLAGS_RELEASE}") |
| 50 | + |
| 51 | +set(XQA_SOURCES |
| 52 | + "cuda_hint.cuh" |
| 53 | + "defines.h" |
| 54 | + "ldgsts.cuh" |
| 55 | + "mha.h" |
| 56 | + "mhaUtils.cuh" |
| 57 | + "mma.cuh" |
| 58 | + "platform.h" |
| 59 | + "utils.cuh" |
| 60 | + "utils.h" |
| 61 | + "mha_stdheaders.cuh" |
| 62 | + "gmma.cuh" |
| 63 | + "gmma_impl.cuh" |
| 64 | + "barriers.cuh" |
| 65 | + "tma.h" |
| 66 | + "mha.cu" |
| 67 | + "mha_sm90.cu") |
| 68 | + |
| 69 | +# For ${Python3_EXECUTABLE} |
| 70 | +find_package(Python3 COMPONENTS Interpreter REQUIRED) |
| 71 | + |
| 72 | +set(XQA_SOURCES_H ${CMAKE_CURRENT_BINARY_DIR}/xqa_sources.h) |
| 73 | +add_custom_command( |
| 74 | + OUTPUT ${XQA_SOURCES_H} |
| 75 | + COMMAND ${Python3_EXECUTABLE} gen_cpp_header.py -o ${XQA_SOURCES_H} |
| 76 | + --cuda_root ${CUDAToolkit_LIBRARY_ROOT} |
| 77 | + COMMENT "Generating xqa_sources.h for XQAJIT..." |
| 78 | + DEPENDS gen_cpp_header.py ${XQA_SOURCES} |
| 79 | + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 80 | + VERBATIM) |
| 81 | +add_custom_target(xqa_sources_h DEPENDS ${XQA_SOURCES_H}) |
| 82 | + |
| 83 | +if(BUILD_XQA_TESTS) |
| 84 | + # GoogleTest Preparation - Code block copied from |
| 85 | + # https://google.github.io/googletest/quickstart-cmake.html |
| 86 | + include(FetchContent) |
| 87 | + FetchContent_Declare( |
| 88 | + googletest |
| 89 | + GIT_REPOSITORY https://github.com/google/googletest.git |
| 90 | + GIT_TAG v1.15.2) |
| 91 | + include(GoogleTest) |
| 92 | + |
| 93 | + # Add Eigen via FetchContent |
| 94 | + FetchContent_Declare( |
| 95 | + eigen |
| 96 | + GIT_REPOSITORY https://gitlab.com/libeigen/eigen.git |
| 97 | + GIT_TAG 3.4.0) |
| 98 | + FetchContent_MakeAvailable(googletest eigen) |
| 99 | + |
| 100 | + enable_testing() |
| 101 | + add_executable(unitTests mha.cu mha_sm90.cu test/test.cpp |
| 102 | + test/refAttention.cpp) |
| 103 | + target_include_directories(unitTests PUBLIC ${EIGEN3_INCLUDE_DIR}) |
| 104 | + target_link_libraries(unitTests PUBLIC GTest::gtest GTest::gtest_main cuda |
| 105 | + Eigen3::Eigen) |
| 106 | + |
| 107 | + find_library( |
| 108 | + NVRTC_LIB nvrtc |
| 109 | + HINTS ${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}/../lib |
| 110 | + PATH_SUFFIXES lib64 lib lib/x64) |
| 111 | + if(NOT NVRTC_LIB) |
| 112 | + message("Nvrtc not found") |
| 113 | + add_definitions(-DENABLE_NVRTC=0) |
| 114 | + else() |
| 115 | + add_definitions(-DENABLE_NVRTC=1) |
| 116 | + target_link_libraries(unitTests PUBLIC ${NVRTC_LIB}) |
| 117 | + # Generate xqa_sources.h for nvrtc testing. |
| 118 | + include_directories(${PROJECT_BINARY_DIR}) |
| 119 | + set(GENERATED_XQA_SOURCES |
| 120 | + "${CMAKE_CURRENT_BINARY_DIR}/generated/xqa_sources.h") |
| 121 | + add_custom_command( |
| 122 | + OUTPUT ${GENERATED_XQA_SOURCES} |
| 123 | + COMMAND |
| 124 | + ./gen_cpp_header.py -o ${GENERATED_XQA_SOURCES} --embed-cuda-headers |
| 125 | + --cuda_root ${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}/.. |
| 126 | + DEPENDS gen_cpp_header.py ${XQA_SOURCES} |
| 127 | + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) |
| 128 | + target_sources(unitTests PUBLIC ${GENERATED_XQA_SOURCES}) |
| 129 | + endif() |
| 130 | + |
| 131 | + add_test(NAME unitTests COMMAND unitTests) |
| 132 | +endif() |
0 commit comments