-
Notifications
You must be signed in to change notification settings - Fork 640
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CMake and Windows Compilation #788
Closed
Jamezo97
wants to merge
2
commits into
bitsandbytes-foundation:main
from
Jamezo97:feature/cmake-and-windows-compilation
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
# This CMake config hopefully makes it easier to compile. | ||
# Ensure the CUDA Toolkit is available on your path. Then run: | ||
# For GCC: `cmake -B build . && cmake --build build` | ||
# For MSVC: `cmake -B build . && cmake --build build --config Release` | ||
# You can also use the following options | ||
# - BUILD_CUDA: Default ON, will build with CUDA | ||
# - NO_CUBLASLT: Default OFF, will skip building/linking CUBLASLT support | ||
# - CUDA_VERSION: The expected CUDA version, for sanity checking. The actual version | ||
# is whatever CMake finds on your path. | ||
# - COMPUTE_CAPABILITY: Which GPU Arch/Compute codes to provide to NVCC. | ||
# Separate by semicolons, i.e. `-DCOMPUTE_CAPABILITY=89;90` | ||
# Check your compute capability here: https://developer.nvidia.com/cuda-gpus | ||
# - PTXAS_VERBOSE: Pass the `-v` option to the PTX Assembler | ||
cmake_minimum_required(VERSION 3.18) | ||
|
||
project(bitsandbytes LANGUAGES C CXX) | ||
|
||
option(BUILD_CUDA "Build bitsandbytes with CUDA support" ON) | ||
option(NO_CUBLASLT "Disable CUBLAS" OFF) | ||
option(PTXAS_VERBOSE "Pass through -v flag to PTX Assembler" OFF) | ||
|
||
list(APPEND SRC_FILES csrc/common.cpp csrc/cpu_ops.cpp csrc/pythonInterface.cpp) | ||
list(APPEND CUDA_FILES csrc/ops.cu csrc/kernels.cu) | ||
|
||
message(STATUS "BUILD_CUDA := ${BUILD_CUDA}") | ||
message(STATUS "NO_CUBLASLT := ${NO_CUBLASLT}") | ||
|
||
set(BNB_OUTPUT_NAME "libbitsandbytes") | ||
|
||
if(BUILD_CUDA) | ||
enable_language(CUDA) # This will fail if CUDA is not found | ||
|
||
# Convert the CUDA version from X.Y.z to XY. There's probably a shorter way of doing this | ||
string(REGEX MATCH "^[0-9]+.[0-9]+" _CUDA_VERSION_FIRST_TWO "${CMAKE_CUDA_COMPILER_VERSION}") | ||
string(REPLACE "." "" CUDA_VERSION_SHORT "${_CUDA_VERSION_FIRST_TWO}") | ||
|
||
# Expose a cache variable that the user can set to ensure the correct version of CUDA is found | ||
set(CUDA_VERSION "${CUDA_VERSION_SHORT}" CACHE STRING "Expected CUDA Version Shortcode") | ||
|
||
message(STATUS "CUDA Version: ${CUDA_VERSION_SHORT} (${CMAKE_CUDA_COMPILER_VERSION})") | ||
message(STATUS "CUDA Compiler: ${CMAKE_CUDA_COMPILER}") | ||
|
||
# It should match the discovered version | ||
if(NOT CUDA_VERSION STREQUAL "${CUDA_VERSION_SHORT}") | ||
message(FATAL_ERROR "You've specified CUDA version ${CUDA_VERSION} however the CUDA compiler found is ${CUDA_VERSION_SHORT}." | ||
" Ensure the desired CUDA compiler is the first one available on your PATH." | ||
) | ||
endif() | ||
|
||
if(CMAKE_CUDA_COMPILER_VERSION VERSION_LESS "11.0") | ||
message(FATAL_ERROR "CUDA Version < 11 is not supported") | ||
elseif(CMAKE_CUDA_COMPILER_VERSION VERSION_GREATER_EQUAL "13.0") | ||
message(FATAL_ERROR "CUDA Version > 12 is not supported") | ||
endif() | ||
|
||
string(APPEND CMAKE_CUDA_FLAGS " --use_fast_math") | ||
if(PTXAS_VERBOSE) | ||
# Verbose? Outputs register usage information, and other things... | ||
string(APPEND CMAKE_CUDA_FLAGS " -Xptxas=-v") | ||
endif() | ||
|
||
foreach(capability ${CMAKE_CUDA_ARCHITECTURES_ALL}) | ||
# Most of the items here are like: `xx-real`, so we just extract the `xx` portion | ||
string(REGEX MATCH "[0-9]+" capability_id "${capability}") | ||
if(capability_id GREATER 0) | ||
list(APPEND POSSIBLE_CAPABILITIES ${capability_id}) | ||
endif() | ||
endforeach() | ||
|
||
# This can be changed via -D argument to CMake | ||
# By default all possible capabilities are compiled | ||
set(COMPUTE_CAPABILITY "${POSSIBLE_CAPABILITIES}" CACHE STRING "Compute Capabilities Targeted") | ||
|
||
message(STATUS "CUDA Capabilities Available: ${POSSIBLE_CAPABILITIES}") | ||
message(STATUS "CUDA Capabilities Selected: ${COMPUTE_CAPABILITY}") | ||
|
||
foreach(capability ${COMPUTE_CAPABILITY}) | ||
string(APPEND CMAKE_CUDA_FLAGS " -gencode arch=compute_${capability},code=sm_${capability}") | ||
endforeach() | ||
|
||
message(STATUS "CUDA NVCC Flags: ${CMAKE_CUDA_FLAGS}") | ||
|
||
list(APPEND SRC_FILES ${CUDA_FILES}) | ||
|
||
string(APPEND BNB_OUTPUT_NAME "_cuda${CUDA_VERSION_SHORT}") | ||
if(NO_CUBLASLT) | ||
string(APPEND BNB_OUTPUT_NAME "_nocublaslt") | ||
endif() | ||
else() | ||
message(STATUS "Building CPU Only") | ||
string(APPEND BNB_OUTPUT_NAME "_cpu") | ||
if(NO_CUBLASLT) | ||
message(WARNING "We're building in CPU only mode but NO_CUBLASLT is enabled. It will have no effect.") | ||
endif() | ||
endif() | ||
|
||
add_library(libbitsandbytes SHARED ${SRC_FILES}) | ||
target_include_directories(libbitsandbytes PUBLIC csrc include) | ||
target_compile_features(libbitsandbytes PUBLIC cxx_std_14) | ||
|
||
|
||
if(BUILD_CUDA) | ||
target_compile_definitions(libbitsandbytes PUBLIC BUILD_CUDA) | ||
target_link_libraries(libbitsandbytes PUBLIC cudart cublas cusparse) | ||
if(NO_CUBLASLT) | ||
target_compile_definitions(libbitsandbytes PUBLIC NO_CUBLASLT) | ||
else() | ||
target_link_libraries(libbitsandbytes PUBLIC cublasLt) | ||
endif() | ||
|
||
set_target_properties(libbitsandbytes | ||
PROPERTIES | ||
CUDA_SEPARABLE_COMPILATION ON | ||
) | ||
endif() | ||
|
||
set_target_properties(libbitsandbytes | ||
PROPERTIES | ||
OUTPUT_NAME ${BNB_OUTPUT_NAME} | ||
# We have to use a generator expression to prevent MSVC Debug/Release subdirs being made | ||
RUNTIME_OUTPUT_DIRECTORY "$<1:${CMAKE_SOURCE_DIR}/bitsandbytes>" | ||
POSITION_INDEPENDENT_CODE ON # The `-fPIC` commands for non-windows compilers | ||
WINDOWS_EXPORT_ALL_SYMBOLS ON # On Windows, export all c methods as DLL exports | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should be using https://cmake.org/cmake/help/latest/module/FindCUDAToolkit.html#module:FindCUDAToolkit and the targets associacted with it