-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Build OpenBLAS from source code for MSVC #1674
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b81f375
Download OpenBLAS binaries when building with MSVC
zcbenz 1778762
Download dlfcn-win32
zcbenz 419e9c4
Link with dlfcn-win32 correctly
zcbenz d9a302b
Build OpenBLAS from source code
zcbenz 5eaa3b1
Link with openblas statically
zcbenz 2809388
Link with BLAS privately
zcbenz 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 |
---|---|---|
|
@@ -20,6 +20,7 @@ option(MLX_METAL_DEBUG "Enhance metal debug workflow" OFF) | |
option(MLX_ENABLE_X64_MAC "Enable building for x64 macOS" OFF) | ||
option(MLX_BUILD_GGUF "Include support for GGUF format" ON) | ||
option(MLX_BUILD_SAFETENSORS "Include support for safetensors format" ON) | ||
option(MLX_BUILD_BLAS_FROM_SOURCE "Build OpenBLAS from source code" OFF) | ||
option(MLX_METAL_JIT "Use JIT compilation for Metal kernels" OFF) | ||
option(BUILD_SHARED_LIBS "Build mlx as a shared library" OFF) | ||
|
||
|
@@ -112,16 +113,52 @@ elseif(MLX_BUILD_METAL) | |
target_link_libraries(mlx PUBLIC ${METAL_LIB} ${FOUNDATION_LIB} ${QUARTZ_LIB}) | ||
endif() | ||
|
||
if(WIN32) | ||
if(MSVC) | ||
# GGUF does not build with MSVC. | ||
set(MLX_BUILD_GGUF OFF) | ||
# There is no prebuilt OpenBLAS distribution for MSVC. | ||
set(MLX_BUILD_BLAS_FROM_SOURCE ON) | ||
endif() | ||
# Windows implementation of dlfcn.h APIs. | ||
FetchContent_Declare( | ||
dlfcn-win32 | ||
GIT_REPOSITORY https://github.com/dlfcn-win32/dlfcn-win32.git | ||
GIT_TAG v1.4.1 | ||
EXCLUDE_FROM_ALL) | ||
FetchContent_MakeAvailable(dlfcn-win32) | ||
target_include_directories(mlx PRIVATE "${dlfcn-win32_SOURCE_DIR}/src") | ||
target_link_libraries(mlx PRIVATE dl) | ||
endif() | ||
|
||
if(MLX_BUILD_CPU) | ||
find_library(ACCELERATE_LIBRARY Accelerate) | ||
if(ACCELERATE_LIBRARY) | ||
message(STATUS "Accelerate found ${ACCELERATE_LIBRARY}") | ||
set(MLX_BUILD_ACCELERATE ON) | ||
target_link_libraries(mlx PUBLIC ${ACCELERATE_LIBRARY}) | ||
add_compile_definitions(ACCELERATE_NEW_LAPACK) | ||
else() | ||
message(STATUS "Accelerate or arm neon not found, using default backend.") | ||
set(MLX_BUILD_ACCELERATE OFF) | ||
endif() | ||
|
||
if(MLX_BUILD_ACCELERATE) | ||
target_link_libraries(mlx PUBLIC ${ACCELERATE_LIBRARY}) | ||
add_compile_definitions(ACCELERATE_NEW_LAPACK) | ||
elseif(MLX_BUILD_BLAS_FROM_SOURCE) | ||
# Download and build OpenBLAS from source code. | ||
FetchContent_Declare( | ||
openblas | ||
GIT_REPOSITORY https://github.com/OpenMathLib/OpenBLAS.git | ||
GIT_TAG v0.3.28 | ||
EXCLUDE_FROM_ALL) | ||
set(BUILD_STATIC_LIBS ON) # link statically | ||
set(NOFORTRAN ON) # msvc has no fortran compiler | ||
FetchContent_MakeAvailable(openblas) | ||
target_link_libraries(mlx PRIVATE openblas) | ||
zcbenz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
target_include_directories( | ||
mlx PRIVATE "${openblas_SOURCE_DIR}/lapack-netlib/LAPACKE/include" | ||
"${CMAKE_BINARY_DIR}/generated" "${CMAKE_BINARY_DIR}") | ||
else() | ||
if(${CMAKE_HOST_APPLE}) | ||
# The blas shipped in macOS SDK is not supported, search homebrew for | ||
# openblas instead. | ||
|
@@ -139,7 +176,7 @@ if(MLX_BUILD_CPU) | |
message(STATUS "Lapack lib " ${LAPACK_LIBRARIES}) | ||
message(STATUS "Lapack include " ${LAPACK_INCLUDE_DIRS}) | ||
target_include_directories(mlx PRIVATE ${LAPACK_INCLUDE_DIRS}) | ||
target_link_libraries(mlx PUBLIC ${LAPACK_LIBRARIES}) | ||
target_link_libraries(mlx PRIVATE ${LAPACK_LIBRARIES}) | ||
# List blas after lapack otherwise we may accidentally incldue an old | ||
# version of lapack.h from the include dirs of blas. | ||
find_package(BLAS REQUIRED) | ||
|
@@ -152,14 +189,7 @@ if(MLX_BUILD_CPU) | |
message(STATUS "Blas lib " ${BLAS_LIBRARIES}) | ||
message(STATUS "Blas include " ${BLAS_INCLUDE_DIRS}) | ||
target_include_directories(mlx PRIVATE ${BLAS_INCLUDE_DIRS}) | ||
target_link_libraries(mlx PUBLIC ${BLAS_LIBRARIES}) | ||
|
||
if(WIN32) | ||
find_package(dlfcn-win32 REQUIRED) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry to see this gone! A |
||
message(STATUS "dlfcn-win32 lib " ${dlfcn-win32_LIBRARIES}) | ||
message(STATUS "dlfcn-win32 include " ${dlfcn-win32_INCLUDE_DIRS}) | ||
target_link_libraries(mlx PUBLIC ${dlfcn-win32_LIBRARIES}) | ||
endif() | ||
target_link_libraries(mlx PRIVATE ${BLAS_LIBRARIES}) | ||
endif() | ||
else() | ||
set(MLX_BUILD_ACCELERATE OFF) | ||
|
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.
I'm just curious why we check both of these. Is it common to use a compiler other than MSVC on windows?
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.
Some people use MinGW (JuliaPackaging/Yggdrasil#9761), which provides layers for some POSIX APIs.