1
0
Fork 0
mirror of https://gitlab.com/niansa/libjustlm.git synced 2025-03-06 20:49:17 +01:00

Minor improvemens in CMakeFiles and dlhandle

This commit is contained in:
niansa 2023-05-18 22:31:17 +02:00
parent e489f0f53c
commit f279b31d5f
3 changed files with 87 additions and 93 deletions

View file

@ -20,14 +20,15 @@ if (LM_COSCHED)
endif() endif()
function(target_justlm_setup target) function(target_justlm_setup TARGET_NAME)
target_include_directories(${target} PUBLIC include/) message(STATUS "Configuring model implementation target ${TARGET_NAME}")
target_include_directories(${TARGET_NAME} PUBLIC include/)
if (LM_COSCHED) if (LM_COSCHED)
target_compile_definitions(${target} PUBLIC LM_COSCHED) target_compile_definitions(${TARGET_NAME} PUBLIC LM_COSCHED)
target_link_libraries(${target} PRIVATE cosched) target_link_libraries(${TARGET_NAME} PRIVATE cosched)
endif() endif()
if (LM_NOEXCEPT) if (LM_NOEXCEPT)
target_compile_definitions(${target} PUBLIC LM_NOEXCEPT) target_compile_definitions(${TARGET_NAME} PUBLIC LM_NOEXCEPT)
endif() endif()
endfunction() endfunction()

View file

@ -1,30 +1,27 @@
#ifndef DLHANDLE_H
#define DLHANDLE_H
#ifndef __WIN32 #ifndef __WIN32
#include <string> #include <string>
#include <exception> #include <stdexcept>
#include <utility> #include <utility>
#include <dlfcn.h> #include <dlfcn.h>
class Dlhandle { class Dlhandle {
void *chandle; void *chandle;
public: public:
class Exception : public std::exception { class Exception : public std::runtime_error {
std::string errmsg;
public: public:
Exception(std::string errmsg) { using std::runtime_error::runtime_error;
this->errmsg = errmsg;
}
virtual const char* what() const throw() {
return errmsg.c_str();
}
}; };
Dlhandle() : chandle(nullptr) {} Dlhandle() : chandle(nullptr) {}
Dlhandle(const std::string& fpath, int flags = RTLD_LAZY) { Dlhandle(const std::string& fpath, int flags = RTLD_LAZY) {
chandle = dlopen(fpath.c_str(), flags); chandle = dlopen(fpath.c_str(), flags);
if (!chandle) { if (!chandle) {
throw Exception("dlopen(): "+fpath); throw Exception("dlopen(\""+fpath+"\"): "+dlerror());
} }
} }
Dlhandle(const Dlhandle& o) = delete; Dlhandle(const Dlhandle& o) = delete;
@ -48,7 +45,6 @@ public:
template<typename T> template<typename T>
T* get(const std::string& fname) { T* get(const std::string& fname) {
dlerror(); // Clear error
auto fres = reinterpret_cast<T*>(dlsym(chandle, fname.c_str())); auto fres = reinterpret_cast<T*>(dlsym(chandle, fname.c_str()));
return (dlerror()==NULL)?fres:nullptr; return (dlerror()==NULL)?fres:nullptr;
} }
@ -67,22 +63,16 @@ class Dlhandle {
HMODULE chandle; HMODULE chandle;
public: public:
class Exception : public std::exception { class Exception : public std::runtime_error {
std::string errmsg;
public: public:
Exception(std::string errmsg) { using std::runtime_error::runtime_error;
this->errmsg = errmsg;
}
virtual const char* what() const throw() {
return errmsg.c_str();
}
}; };
Dlhandle() : chandle(nullptr) {} Dlhandle() : chandle(nullptr) {}
Dlhandle(const std::string& fpath) { Dlhandle(const std::string& fpath) {
chandle = LoadLibraryA(fpath.c_str()); chandle = LoadLibraryA(fpath.c_str());
if (!chandle) { if (!chandle) {
throw Exception("dlopen(): "+fpath); throw Exception("dlopen(\""+fpath+"\"): Error");
} }
} }
Dlhandle(const Dlhandle& o) = delete; Dlhandle(const Dlhandle& o) = delete;
@ -106,3 +96,4 @@ public:
} }
}; };
#endif #endif
#endif // DLHANDLE_H

View file

@ -51,16 +51,16 @@ option(LLAMA_SANITIZE_ADDRESS "llama: enable address sanitizer"
option(LLAMA_SANITIZE_UNDEFINED "llama: enable undefined sanitizer" OFF) option(LLAMA_SANITIZE_UNDEFINED "llama: enable undefined sanitizer" OFF)
# instruction set specific # instruction set specific
option(LLAMA_AVX "llama: enable AVX" ON) #option(LLAMA_AVX "llama: enable AVX" ON)
option(LLAMA_AVX2 "llama: enable AVX2" ON) #option(LLAMA_AVX2 "llama: enable AVX2" ON)
option(LLAMA_AVX512 "llama: enable AVX512" OFF) #option(LLAMA_AVX512 "llama: enable AVX512" OFF)
option(LLAMA_AVX512_VBMI "llama: enable AVX512-VBMI" OFF) #option(LLAMA_AVX512_VBMI "llama: enable AVX512-VBMI" OFF)
option(LLAMA_AVX512_VNNI "llama: enable AVX512-VNNI" OFF) #option(LLAMA_AVX512_VNNI "llama: enable AVX512-VNNI" OFF)
option(LLAMA_FMA "llama: enable FMA" ON) #option(LLAMA_FMA "llama: enable FMA" ON)
# in MSVC F16C is implied with AVX2/AVX512 # in MSVC F16C is implied with AVX2/AVX512
if (NOT MSVC) #if (NOT MSVC)
option(LLAMA_F16C "llama: enable F16C" ON) # option(LLAMA_F16C "llama: enable F16C" ON)
endif() #endif()
# 3rd party libs # 3rd party libs
option(LLAMA_ACCELERATE "llama: enable Accelerate framework" ON) option(LLAMA_ACCELERATE "llama: enable Accelerate framework" ON)
@ -207,75 +207,77 @@ if (NOT MSVC)
endif() endif()
endif() endif()
if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm" OR ${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64") function(include_ggml DIRECTORY SUFFIX WITH_LLAMA)
message(STATUS "ARM detected") message(STATUS "Configuring ggml implementation target llama${SUFFIX} in ${CMAKE_CURRENT_SOURCE_DIR}/${DIRECTORY}")
if (MSVC)
# TODO: arm msvc? if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm" OR ${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
else() message(STATUS "ARM detected")
if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64") if (MSVC)
add_compile_options(-mcpu=native) # TODO: arm msvc?
else()
if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
add_compile_options(-mcpu=native)
endif()
# TODO: armv6,7,8 version specific flags
endif() endif()
# TODO: armv6,7,8 version specific flags elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^(x86_64|i686|AMD64)$")
endif() message(STATUS "x86 detected")
elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^(x86_64|i686|AMD64)$") if (MSVC)
message(STATUS "x86 detected") if (LLAMA_AVX512)
if (MSVC) add_compile_options($<$<COMPILE_LANGUAGE:C>:/arch:AVX512>)
if (LLAMA_AVX512) add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/arch:AVX512>)
add_compile_options($<$<COMPILE_LANGUAGE:C>:/arch:AVX512>) # MSVC has no compile-time flags enabling specific
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/arch:AVX512>) # AVX512 extensions, neither it defines the
# MSVC has no compile-time flags enabling specific # macros corresponding to the extensions.
# AVX512 extensions, neither it defines the # Do it manually.
# macros corresponding to the extensions. if (LLAMA_AVX512_VBMI)
# Do it manually. add_compile_definitions($<$<COMPILE_LANGUAGE:C>:__AVX512VBMI__>)
add_compile_definitions($<$<COMPILE_LANGUAGE:CXX>:__AVX512VBMI__>)
endif()
if (LLAMA_AVX512_VNNI)
add_compile_definitions($<$<COMPILE_LANGUAGE:C>:__AVX512VNNI__>)
add_compile_definitions($<$<COMPILE_LANGUAGE:CXX>:__AVX512VNNI__>)
endif()
elseif (LLAMA_AVX2)
add_compile_options($<$<COMPILE_LANGUAGE:C>:/arch:AVX2>)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/arch:AVX2>)
elseif (LLAMA_AVX)
add_compile_options($<$<COMPILE_LANGUAGE:C>:/arch:AVX>)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/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) if (LLAMA_AVX512_VBMI)
add_compile_definitions($<$<COMPILE_LANGUAGE:C>:__AVX512VBMI__>) add_compile_options(-mavx512vbmi)
add_compile_definitions($<$<COMPILE_LANGUAGE:CXX>:__AVX512VBMI__>)
endif() endif()
if (LLAMA_AVX512_VNNI) if (LLAMA_AVX512_VNNI)
add_compile_definitions($<$<COMPILE_LANGUAGE:C>:__AVX512VNNI__>) add_compile_options(-mavx512vnni)
add_compile_definitions($<$<COMPILE_LANGUAGE:CXX>:__AVX512VNNI__>)
endif() endif()
elseif (LLAMA_AVX2)
add_compile_options($<$<COMPILE_LANGUAGE:C>:/arch:AVX2>)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/arch:AVX2>)
elseif (LLAMA_AVX)
add_compile_options($<$<COMPILE_LANGUAGE:C>:/arch:AVX>)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/arch:AVX>)
endif() endif()
else() else()
if (LLAMA_F16C) # TODO: support PowerPC
add_compile_options(-mf16c) message(STATUS "Unknown architecture")
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()
endif() endif()
else()
# TODO: support PowerPC
message(STATUS "Unknown architecture")
endif()
# #
# Build libraries # Build libraries
# #
function(include_ggml DIRECTORY SUFFIX WITH_LLAMA)
if (LLAMA_CUBLAS) if (LLAMA_CUBLAS)
cmake_minimum_required(VERSION 3.17) cmake_minimum_required(VERSION 3.17)