diff --git a/CMakeLists.txt b/CMakeLists.txt index 652d296..e5cacaf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,14 +20,15 @@ if (LM_COSCHED) endif() -function(target_justlm_setup target) - target_include_directories(${target} PUBLIC include/) +function(target_justlm_setup TARGET_NAME) + message(STATUS "Configuring model implementation target ${TARGET_NAME}") + target_include_directories(${TARGET_NAME} PUBLIC include/) if (LM_COSCHED) - target_compile_definitions(${target} PUBLIC LM_COSCHED) - target_link_libraries(${target} PRIVATE cosched) + target_compile_definitions(${TARGET_NAME} PUBLIC LM_COSCHED) + target_link_libraries(${TARGET_NAME} PRIVATE cosched) endif() if (LM_NOEXCEPT) - target_compile_definitions(${target} PUBLIC LM_NOEXCEPT) + target_compile_definitions(${TARGET_NAME} PUBLIC LM_NOEXCEPT) endif() endfunction() diff --git a/dlhandle.hpp b/dlhandle.hpp index af58034..444385b 100644 --- a/dlhandle.hpp +++ b/dlhandle.hpp @@ -1,30 +1,27 @@ +#ifndef DLHANDLE_H +#define DLHANDLE_H #ifndef __WIN32 #include -#include +#include #include #include + class Dlhandle { void *chandle; public: - class Exception : public std::exception { - std::string errmsg; + class Exception : public std::runtime_error { public: - Exception(std::string errmsg) { - this->errmsg = errmsg; - } - virtual const char* what() const throw() { - return errmsg.c_str(); - } + using std::runtime_error::runtime_error; }; Dlhandle() : chandle(nullptr) {} Dlhandle(const std::string& fpath, int flags = RTLD_LAZY) { chandle = dlopen(fpath.c_str(), flags); if (!chandle) { - throw Exception("dlopen(): "+fpath); + throw Exception("dlopen(\""+fpath+"\"): "+dlerror()); } } Dlhandle(const Dlhandle& o) = delete; @@ -48,7 +45,6 @@ public: template T* get(const std::string& fname) { - dlerror(); // Clear error auto fres = reinterpret_cast(dlsym(chandle, fname.c_str())); return (dlerror()==NULL)?fres:nullptr; } @@ -67,22 +63,16 @@ class Dlhandle { HMODULE chandle; public: - class Exception : public std::exception { - std::string errmsg; + class Exception : public std::runtime_error { public: - Exception(std::string errmsg) { - this->errmsg = errmsg; - } - virtual const char* what() const throw() { - return errmsg.c_str(); - } + using std::runtime_error::runtime_error; }; Dlhandle() : chandle(nullptr) {} Dlhandle(const std::string& fpath) { chandle = LoadLibraryA(fpath.c_str()); if (!chandle) { - throw Exception("dlopen(): "+fpath); + throw Exception("dlopen(\""+fpath+"\"): Error"); } } Dlhandle(const Dlhandle& o) = delete; @@ -106,3 +96,4 @@ public: } }; #endif +#endif // DLHANDLE_H diff --git a/llama.cpp.cmake b/llama.cpp.cmake index 37e7f4d..c30d07c 100644 --- a/llama.cpp.cmake +++ b/llama.cpp.cmake @@ -51,16 +51,16 @@ option(LLAMA_SANITIZE_ADDRESS "llama: enable address sanitizer" 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_AVX512 "llama: enable AVX512" OFF) -option(LLAMA_AVX512_VBMI "llama: enable AVX512-VBMI" OFF) -option(LLAMA_AVX512_VNNI "llama: enable AVX512-VNNI" OFF) -option(LLAMA_FMA "llama: enable FMA" ON) +#option(LLAMA_AVX "llama: enable AVX" ON) +#option(LLAMA_AVX2 "llama: enable AVX2" ON) +#option(LLAMA_AVX512 "llama: enable AVX512" OFF) +#option(LLAMA_AVX512_VBMI "llama: enable AVX512-VBMI" OFF) +#option(LLAMA_AVX512_VNNI "llama: enable AVX512-VNNI" OFF) +#option(LLAMA_FMA "llama: enable FMA" ON) # in MSVC F16C is implied with AVX2/AVX512 -if (NOT MSVC) - option(LLAMA_F16C "llama: enable F16C" ON) -endif() +#if (NOT MSVC) +# option(LLAMA_F16C "llama: enable F16C" ON) +#endif() # 3rd party libs option(LLAMA_ACCELERATE "llama: enable Accelerate framework" ON) @@ -207,75 +207,77 @@ if (NOT MSVC) endif() endif() -if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm" OR ${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64") - message(STATUS "ARM detected") - if (MSVC) - # TODO: arm msvc? - else() - if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64") - add_compile_options(-mcpu=native) +function(include_ggml DIRECTORY SUFFIX WITH_LLAMA) + message(STATUS "Configuring ggml implementation target llama${SUFFIX} in ${CMAKE_CURRENT_SOURCE_DIR}/${DIRECTORY}") + + if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm" OR ${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64") + message(STATUS "ARM detected") + 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() - # TODO: armv6,7,8 version specific flags - endif() -elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^(x86_64|i686|AMD64)$") - message(STATUS "x86 detected") - if (MSVC) - if (LLAMA_AVX512) - add_compile_options($<$:/arch:AVX512>) - add_compile_options($<$:/arch:AVX512>) - # MSVC has no compile-time flags enabling specific - # AVX512 extensions, neither it defines the - # macros corresponding to the extensions. - # Do it manually. + elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^(x86_64|i686|AMD64)$") + message(STATUS "x86 detected") + if (MSVC) + if (LLAMA_AVX512) + add_compile_options($<$:/arch:AVX512>) + add_compile_options($<$:/arch:AVX512>) + # MSVC has no compile-time flags enabling specific + # AVX512 extensions, neither it defines the + # macros corresponding to the extensions. + # Do it manually. + if (LLAMA_AVX512_VBMI) + add_compile_definitions($<$:__AVX512VBMI__>) + add_compile_definitions($<$:__AVX512VBMI__>) + endif() + if (LLAMA_AVX512_VNNI) + add_compile_definitions($<$:__AVX512VNNI__>) + add_compile_definitions($<$:__AVX512VNNI__>) + endif() + elseif (LLAMA_AVX2) + add_compile_options($<$:/arch:AVX2>) + add_compile_options($<$:/arch:AVX2>) + elseif (LLAMA_AVX) + add_compile_options($<$:/arch:AVX>) + add_compile_options($<$:/arch:AVX>) + endif() + else() + if (LLAMA_F16C) + add_compile_options(-mf16c) + endif() + if (LLAMA_FMA) + add_compile_options(-mfma) + endif() + if (LLAMA_AVX) + add_compile_options(-mavx) + endif() + if (LLAMA_AVX2) + add_compile_options(-mavx2) + endif() + if (LLAMA_AVX512) + add_compile_options(-mavx512f) + add_compile_options(-mavx512bw) + endif() if (LLAMA_AVX512_VBMI) - add_compile_definitions($<$:__AVX512VBMI__>) - add_compile_definitions($<$:__AVX512VBMI__>) + add_compile_options(-mavx512vbmi) endif() if (LLAMA_AVX512_VNNI) - add_compile_definitions($<$:__AVX512VNNI__>) - add_compile_definitions($<$:__AVX512VNNI__>) + add_compile_options(-mavx512vnni) endif() - elseif (LLAMA_AVX2) - add_compile_options($<$:/arch:AVX2>) - add_compile_options($<$:/arch:AVX2>) - elseif (LLAMA_AVX) - add_compile_options($<$:/arch:AVX>) - add_compile_options($<$:/arch:AVX>) endif() else() - if (LLAMA_F16C) - add_compile_options(-mf16c) - endif() - if (LLAMA_FMA) - add_compile_options(-mfma) - endif() - if (LLAMA_AVX) - add_compile_options(-mavx) - endif() - if (LLAMA_AVX2) - add_compile_options(-mavx2) - endif() - if (LLAMA_AVX512) - add_compile_options(-mavx512f) - add_compile_options(-mavx512bw) - endif() - if (LLAMA_AVX512_VBMI) - add_compile_options(-mavx512vbmi) - endif() - if (LLAMA_AVX512_VNNI) - add_compile_options(-mavx512vnni) - endif() + # TODO: support PowerPC + message(STATUS "Unknown architecture") endif() -else() - # TODO: support PowerPC - message(STATUS "Unknown architecture") -endif() -# -# Build libraries -# + # + # Build libraries + # -function(include_ggml DIRECTORY SUFFIX WITH_LLAMA) if (LLAMA_CUBLAS) cmake_minimum_required(VERSION 3.17)