-
Notifications
You must be signed in to change notification settings - Fork 12.5k
Adding missing features of CMakeLists.txt & Refactoring #131
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
311fc3f
Functionality addition CMakeLists.txt
nusu-github 15c4e11
Merge branch 'master' into patch-1
ggerganov 965d8fd
Fix Accelerate link in CMake
ggerganov 0e83240
Windows build Fix
nusu-github d824baa
C++11 to C++17
nusu-github bc53fc0
Reflects C/C++ standard individually
nusu-github 9d7a993
Change the version to 3.12
nusu-github 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 hidden or 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 |
---|---|---|
@@ -1,131 +1,208 @@ | ||
cmake_minimum_required(VERSION 3.8) | ||
project("llama.cpp") | ||
|
||
set(CMAKE_CXX_STANDARD 20) | ||
set(CMAKE_CXX_STANDARD_REQUIRED true) | ||
set(CMAKE_C_STANDARD 11) | ||
set(THREADS_PREFER_PTHREAD_FLAG ON) | ||
find_package(Threads REQUIRED) | ||
cmake_minimum_required(VERSION 3.9) | ||
project("llama.cpp" C CXX) | ||
|
||
if (NOT XCODE AND NOT MSVC AND NOT CMAKE_BUILD_TYPE) | ||
set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE) | ||
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo") | ||
endif() | ||
|
||
option(LLAMA_ALL_WARNINGS "llama: enable all compiler warnings" ON) | ||
option(LLAMA_ALL_WARNINGS_3RD_PARTY "llama: enable all compiler warnings in 3rd party libs" OFF) | ||
# | ||
# Option list | ||
# | ||
|
||
option(LLAMA_SANITIZE_THREAD "llama: enable thread sanitizer" OFF) | ||
option(LLAMA_SANITIZE_ADDRESS "llama: enable address sanitizer" OFF) | ||
option(LLAMA_SANITIZE_UNDEFINED "llama: enable undefined sanitizer" OFF) | ||
# general | ||
option(LLAMA_STATIC "llama: static link libraries" OFF) | ||
option(LLAMA_NATIVE "llama: enable -march=native flag" OFF) | ||
option(LLAMA_LTO "llama: enable link time optimization" OFF) | ||
|
||
if (APPLE) | ||
option(LLAMA_NO_ACCELERATE "llama: disable Accelerate framework" OFF) | ||
option(LLAMA_NO_AVX "llama: disable AVX" OFF) | ||
option(LLAMA_NO_AVX2 "llama: disable AVX2" OFF) | ||
option(LLAMA_NO_FMA "llama: disable FMA" OFF) | ||
endif() | ||
# debug | ||
option(LLAMA_ALL_WARNINGS "llama: enable all compiler warnings" ON) | ||
option(LLAMA_ALL_WARNINGS_3RD_PARTY "llama: enable all compiler warnings in 3rd party libs" OFF) | ||
option(LLAMA_GPROF "llama: enable gprof" OFF) | ||
|
||
# sanitizers | ||
option(LLAMA_SANITIZE_THREAD "llama: enable thread sanitizer" OFF) | ||
option(LLAMA_SANITIZE_ADDRESS "llama: enable address sanitizer" OFF) | ||
option(LLAMA_SANITIZE_UNDEFINED "llama: enable undefined sanitizer" OFF) | ||
|
||
# instruction set specific | ||
option(LLAMA_AVX "llama: enable AVX" ON) | ||
option(LLAMA_AVX2 "llama: enable AVX2" ON) | ||
option(LLAMA_FMA "llama: enable FMA" ON) | ||
|
||
# 3rd party libs | ||
option(LLAMA_ACCELERATE "llama: enable Accelerate framework" ON) | ||
option(LLAMA_OPENBLAS "llama: use OpenBLAS" OFF) | ||
|
||
# | ||
# Compile flags | ||
# | ||
|
||
set(CMAKE_CXX_STANDARD_REQUIRED true) | ||
set(CMAKE_C_STANDARD_REQUIRED true) | ||
set(THREADS_PREFER_PTHREAD_FLAG ON) | ||
find_package(Threads REQUIRED) | ||
|
||
if (NOT MSVC) | ||
if (LLAMA_SANITIZE_THREAD) | ||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread") | ||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread") | ||
add_compile_options(-fsanitize=thread) | ||
endif() | ||
|
||
if (LLAMA_SANITIZE_ADDRESS) | ||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer") | ||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer") | ||
add_compile_options(-fsanitize=address -fno-omit-frame-pointer) | ||
endif() | ||
|
||
if (LLAMA_SANITIZE_UNDEFINED) | ||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined") | ||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined") | ||
add_compile_options(-fsanitize=undefined) | ||
endif() | ||
endif() | ||
|
||
if (APPLE AND NOT LLAMA_NO_ACCELERATE) | ||
if (APPLE AND LLAMA_ACCELERATE) | ||
find_library(ACCELERATE_FRAMEWORK Accelerate) | ||
if (ACCELERATE_FRAMEWORK) | ||
message(STATUS "Accelerate framework found") | ||
|
||
set(LLAMA_EXTRA_LIBS ${LLAMA_EXTRA_LIBS} ${ACCELERATE_FRAMEWORK}) | ||
set(LLAMA_EXTRA_FLAGS ${LLAMA_EXTRA_FLAGS} -DGGML_USE_ACCELERATE) | ||
add_compile_definitions(GGML_USE_ACCELERATE) | ||
set(LLAMA_EXTRA_LIBS ${LLAMA_EXTRA_LIBS} ${ACCELERATE_FRAMEWORK}) | ||
else() | ||
message(WARNING "Accelerate framework not found") | ||
endif() | ||
endif() | ||
if (LLAMA_OPENBLAS) | ||
if (LLAMA_STATIC) | ||
set(BLA_STATIC ON) | ||
endif() | ||
|
||
set(BLA_VENDOR OpenBLAS) | ||
find_package(BLAS) | ||
if (BLAS_FOUND) | ||
message(STATUS "OpenBLAS found") | ||
|
||
add_compile_definitions(GGML_USE_OPENBLAS) | ||
add_link_options(${BLAS_LIBRARIES}) | ||
else() | ||
message(WARNING "OpenBLAS not found") | ||
endif() | ||
endif() | ||
|
||
if (LLAMA_ALL_WARNINGS) | ||
if (NOT MSVC) | ||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} \ | ||
-Wall \ | ||
-Wextra \ | ||
-Wpedantic \ | ||
-Wshadow \ | ||
-Wcast-qual \ | ||
-Wstrict-prototypes \ | ||
-Wpointer-arith \ | ||
-Wno-unused-function \ | ||
") | ||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} \ | ||
-Wall \ | ||
-Wextra \ | ||
-Wpedantic \ | ||
-Wcast-qual \ | ||
") | ||
set(c_flags | ||
-Wall | ||
-Wextra | ||
-Wpedantic | ||
-Wshadow | ||
-Wcast-qual | ||
-Wstrict-prototypes | ||
-Wpointer-arith | ||
-Wno-unused-function | ||
) | ||
set(cxx_flags | ||
-Wall | ||
-Wextra | ||
-Wpedantic | ||
-Wcast-qual | ||
) | ||
else() | ||
# todo : msvc | ||
endif() | ||
|
||
add_compile_options( | ||
"$<$<COMPILE_LANGUAGE:C>:${c_flags}>" | ||
"$<$<COMPILE_LANGUAGE:CXX>:${cxx_flags}>" | ||
) | ||
|
||
endif() | ||
|
||
if (LLAMA_LTO) | ||
include(CheckIPOSupported) | ||
check_ipo_supported(RESULT result OUTPUT output) | ||
if (result) | ||
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE) | ||
else() | ||
message(WARNING "IPO is not supported: ${output}") | ||
endif() | ||
endif() | ||
|
||
# Architecture specific | ||
# TODO: probably these flags need to be tweaked on some architectures | ||
# feel free to update the Makefile for your architecture and send a pull request or issue | ||
message(STATUS "CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}") | ||
if (NOT MSVC) | ||
if (LLAMA_STATIC) | ||
add_link_options(-static) | ||
if (MINGW) | ||
add_link_options(-static-libgcc -static-libstdc++) | ||
endif() | ||
endif() | ||
if (LLAMA_GPROF) | ||
add_compile_options(-pg) | ||
endif() | ||
if (LLAMA_NATIVE) | ||
add_compile_options(-march=native) | ||
endif() | ||
endif() | ||
|
||
if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm" OR ${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64") | ||
message(STATUS "ARM detected") | ||
else() | ||
if (MSVC) | ||
# TODO: arm msvc? | ||
else() | ||
if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64") | ||
add_compile_options(-mcpu=native) | ||
endif() | ||
# TODO: armv6,7,8 version specific flags | ||
endif() | ||
elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^(x86_64|i686|AMD64)$") | ||
message(STATUS "x86 detected") | ||
if (MSVC) | ||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /arch:AVX2") | ||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /arch:AVX2") | ||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /arch:AVX2") | ||
if (LLAMA_AVX2) | ||
add_compile_options(/arch:AVX2) | ||
elseif (LLAMA_AVX) | ||
add_compile_options(/arch:AVX) | ||
endif() | ||
else() | ||
if(NOT LLAMA_NO_AVX) | ||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mavx") | ||
add_compile_options(-mf16c) | ||
if (LLAMA_FMA) | ||
add_compile_options(-mfma) | ||
endif() | ||
if(NOT LLAMA_NO_AVX2) | ||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mavx2") | ||
if (LLAMA_AVX) | ||
add_compile_options(-mavx) | ||
endif() | ||
if(NOT LLAMA_NO_FMA) | ||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mfma") | ||
if (LLAMA_AVX2) | ||
add_compile_options(-mavx2) | ||
endif() | ||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mf16c") | ||
endif() | ||
else() | ||
# TODO: support PowerPC | ||
message(STATUS "Unknown architecture") | ||
endif() | ||
|
||
# if (LLAMA_PERF) | ||
# set(LLAMA_EXTRA_FLAGS ${LLAMA_EXTRA_FLAGS} -DGGML_PERF) | ||
# endif() | ||
|
||
add_executable(llama | ||
main.cpp | ||
utils.cpp | ||
utils.h) | ||
# | ||
# Build library | ||
# | ||
|
||
add_executable(llama main.cpp) | ||
|
||
add_executable(quantize | ||
quantize.cpp | ||
utils.cpp | ||
utils.h) | ||
add_executable(quantize quantize.cpp) | ||
|
||
add_library(ggml | ||
ggml.c | ||
ggml.h) | ||
add_library(ggml OBJECT | ||
ggml.c | ||
ggml.h) | ||
|
||
target_compile_definitions(ggml PUBLIC ${LLAMA_EXTRA_FLAGS}) | ||
target_compile_definitions(llama PUBLIC ${LLAMA_EXTRA_FLAGS}) | ||
target_compile_definitions(quantize PUBLIC ${LLAMA_EXTRA_FLAGS}) | ||
add_library(utils OBJECT | ||
utils.cpp | ||
utils.h) | ||
5A63 |
|
|
target_link_libraries(ggml PRIVATE ${LLAMA_EXTRA_LIBS}) | ||
target_include_directories(ggml PUBLIC .) | ||
target_link_libraries(quantize PRIVATE ggml) | ||
target_link_libraries(llama PRIVATE ggml) | ||
target_link_libraries(ggml PRIVATE Threads::Threads) | ||
target_compile_features(ggml PUBLIC c_std_11) | ||
target_compile_features(utils PUBLIC cxx_std_17) | ||
|
||
# | ||
# Linking | ||
# | ||
|
||
target_link_libraries(ggml PRIVATE Threads::Threads ${LLAMA_EXTRA_LIBS}) | ||
target_link_libraries(llama PRIVATE ggml utils) | ||
target_link_libraries(quantize PRIVATE ggml utils) |
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.
Uh oh!
There was an error while loading. Please reload this page.