mirror of https://github.com/nomic-ai/gpt4all
Dlopen backend 5 (#779)
Major change to the backend that allows for pluggable versions of llama.cpp/ggml. This was squashed merged from dlopen_backend_5 where the history is preserved.pull/913/head
parent
5dea13af51
commit
9c6c09cbd2
@ -1,3 +1,9 @@
|
|||||||
[submodule "llama.cpp"]
|
[submodule "llama.cpp-230519"]
|
||||||
path = gpt4all-backend/llama.cpp
|
path = gpt4all-backend/llama.cpp-230519
|
||||||
|
url = https://github.com/ggerganov/llama.cpp.git
|
||||||
|
[submodule "llama.cpp-230511"]
|
||||||
|
path = gpt4all-backend/llama.cpp-230511
|
||||||
url = https://github.com/manyoso/llama.cpp.git
|
url = https://github.com/manyoso/llama.cpp.git
|
||||||
|
[submodule "llama.cpp-mainline"]
|
||||||
|
path = gpt4all-backend/llama.cpp-mainline
|
||||||
|
url = https://github.com/ggerganov/llama.cpp.git
|
||||||
|
@ -0,0 +1,101 @@
|
|||||||
|
#ifndef DLHANDLE_H
|
||||||
|
#define DLHANDLE_H
|
||||||
|
#ifndef _WIN32
|
||||||
|
#include <string>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <utility>
|
||||||
|
#include <dlfcn.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class Dlhandle {
|
||||||
|
void *chandle;
|
||||||
|
|
||||||
|
public:
|
||||||
|
class Exception : public std::runtime_error {
|
||||||
|
public:
|
||||||
|
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+"\"): "+dlerror());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Dlhandle(const Dlhandle& o) = delete;
|
||||||
|
Dlhandle(Dlhandle&& o) : chandle(o.chandle) {
|
||||||
|
o.chandle = nullptr;
|
||||||
|
}
|
||||||
|
~Dlhandle() {
|
||||||
|
if (chandle) dlclose(chandle);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto operator =(Dlhandle&& o) {
|
||||||
|
chandle = std::exchange(o.chandle, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool is_valid() const {
|
||||||
|
return chandle != nullptr;
|
||||||
|
}
|
||||||
|
operator bool() const {
|
||||||
|
return is_valid();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T* get(const std::string& fname) {
|
||||||
|
auto fres = reinterpret_cast<T*>(dlsym(chandle, fname.c_str()));
|
||||||
|
return (dlerror()==NULL)?fres:nullptr;
|
||||||
|
}
|
||||||
|
auto get_fnc(const std::string& fname) {
|
||||||
|
return get<void*(...)>(fname);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
#else
|
||||||
|
#include <string>
|
||||||
|
#include <exception>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <windows.h>
|
||||||
|
#include <libloaderapi.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class Dlhandle {
|
||||||
|
HMODULE chandle;
|
||||||
|
|
||||||
|
public:
|
||||||
|
class Exception : public std::runtime_error {
|
||||||
|
public:
|
||||||
|
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+"\"): Error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Dlhandle(const Dlhandle& o) = delete;
|
||||||
|
Dlhandle(Dlhandle&& o) : chandle(o.chandle) {
|
||||||
|
o.chandle = nullptr;
|
||||||
|
}
|
||||||
|
~Dlhandle() {
|
||||||
|
if (chandle) FreeLibrary(chandle);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool is_valid() const {
|
||||||
|
return chandle != nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T* get(const std::string& fname) {
|
||||||
|
return reinterpret_cast<T*>(GetProcAddress(chandle, fname.c_str()));
|
||||||
|
}
|
||||||
|
auto get_fnc(const std::string& fname) {
|
||||||
|
return get<void*(...)>(fname);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
#endif // DLHANDLE_H
|
@ -0,0 +1 @@
|
|||||||
|
Subproject commit 5ea43392731040b454c293123839b90e159cbb99
|
@ -0,0 +1 @@
|
|||||||
|
Subproject commit ea600071cb005267e9e8f2629c1e406dd5fde083
|
@ -0,0 +1,364 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.12) # Don't bump this version for no reason
|
||||||
|
|
||||||
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||||
|
|
||||||
|
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
|
||||||
|
set(LLAMA_STANDALONE ON)
|
||||||
|
|
||||||
|
# configure project version
|
||||||
|
# TODO
|
||||||
|
else()
|
||||||
|
set(LLAMA_STANDALONE OFF)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if (EMSCRIPTEN)
|
||||||
|
set(BUILD_SHARED_LIBS_DEFAULT OFF)
|
||||||
|
|
||||||
|
option(LLAMA_WASM_SINGLE_FILE "llama: embed WASM inside the generated llama.js" ON)
|
||||||
|
else()
|
||||||
|
if (MINGW)
|
||||||
|
set(BUILD_SHARED_LIBS_DEFAULT OFF)
|
||||||
|
else()
|
||||||
|
set(BUILD_SHARED_LIBS_DEFAULT ON)
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
|
||||||
|
|
||||||
|
#
|
||||||
|
# Option list
|
||||||
|
#
|
||||||
|
|
||||||
|
# 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)
|
||||||
|
|
||||||
|
# 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_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()
|
||||||
|
|
||||||
|
# 3rd party libs
|
||||||
|
option(LLAMA_ACCELERATE "llama: enable Accelerate framework" ON)
|
||||||
|
option(LLAMA_OPENBLAS "llama: use OpenBLAS" OFF)
|
||||||
|
option(LLAMA_CUBLAS "llama: use cuBLAS" OFF)
|
||||||
|
option(LLAMA_CLBLAST "llama: use CLBlast" OFF)
|
||||||
|
|
||||||
|
#
|
||||||
|
# Compile flags
|
||||||
|
#
|
||||||
|
|
||||||
|
set(CMAKE_C_STANDARD 11)
|
||||||
|
set(CMAKE_C_STANDARD_REQUIRED true)
|
||||||
|
set(THREADS_PREFER_PTHREAD_FLAG ON)
|
||||||
|
find_package(Threads REQUIRED)
|
||||||
|
|
||||||
|
if (NOT MSVC)
|
||||||
|
if (LLAMA_SANITIZE_THREAD)
|
||||||
|
add_compile_options(-fsanitize=thread)
|
||||||
|
link_libraries(-fsanitize=thread)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if (LLAMA_SANITIZE_ADDRESS)
|
||||||
|
add_compile_options(-fsanitize=address -fno-omit-frame-pointer)
|
||||||
|
link_libraries(-fsanitize=address)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if (LLAMA_SANITIZE_UNDEFINED)
|
||||||
|
add_compile_options(-fsanitize=undefined)
|
||||||
|
link_libraries(-fsanitize=undefined)
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if (APPLE AND LLAMA_ACCELERATE)
|
||||||
|
find_library(ACCELERATE_FRAMEWORK Accelerate)
|
||||||
|
if (ACCELERATE_FRAMEWORK)
|
||||||
|
message(STATUS "Accelerate framework found")
|
||||||
|
|
||||||
|
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})
|
||||||
|
set(LLAMA_EXTRA_LIBS ${LLAMA_EXTRA_LIBS} openblas)
|
||||||
|
|
||||||
|
# find header file
|
||||||
|
set(OPENBLAS_INCLUDE_SEARCH_PATHS
|
||||||
|
/usr/include
|
||||||
|
/usr/include/openblas
|
||||||
|
/usr/include/openblas-base
|
||||||
|
/usr/local/include
|
||||||
|
/usr/local/include/openblas
|
||||||
|
/usr/local/include/openblas-base
|
||||||
|
/opt/OpenBLAS/include
|
||||||
|
$ENV{OpenBLAS_HOME}
|
||||||
|
$ENV{OpenBLAS_HOME}/include
|
||||||
|
)
|
||||||
|
find_path(OPENBLAS_INC NAMES cblas.h PATHS ${OPENBLAS_INCLUDE_SEARCH_PATHS})
|
||||||
|
add_compile_options(-I${OPENBLAS_INC})
|
||||||
|
else()
|
||||||
|
message(WARNING "OpenBLAS not found")
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if (LLAMA_ALL_WARNINGS)
|
||||||
|
if (NOT MSVC)
|
||||||
|
set(c_flags
|
||||||
|
-Wall
|
||||||
|
-Wextra
|
||||||
|
-Wpedantic
|
||||||
|
-Wcast-qual
|
||||||
|
-Wdouble-promotion
|
||||||
|
-Wshadow
|
||||||
|
-Wstrict-prototypes
|
||||||
|
-Wpointer-arith
|
||||||
|
)
|
||||||
|
set(cxx_flags
|
||||||
|
-Wall
|
||||||
|
-Wextra
|
||||||
|
-Wpedantic
|
||||||
|
-Wcast-qual
|
||||||
|
-Wno-unused-function
|
||||||
|
-Wno-multichar
|
||||||
|
)
|
||||||
|
else()
|
||||||
|
# todo : msvc
|
||||||
|
endif()
|
||||||
|
|
||||||
|
add_compile_options(
|
||||||
|
"$<$<COMPILE_LANGUAGE:C>:${c_flags}>"
|
||||||
|
"$<$<COMPILE_LANGUAGE:CXX>:${cxx_flags}>"
|
||||||
|
)
|
||||||
|
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if (MSVC)
|
||||||
|
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
|
||||||
|
|
||||||
|
if (BUILD_SHARED_LIBS)
|
||||||
|
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
|
||||||
|
endif()
|
||||||
|
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()
|
||||||
|
|
||||||
|
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()
|
||||||
|
elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^(x86_64|i686|AMD64)$")
|
||||||
|
message(STATUS "x86 detected")
|
||||||
|
if (MSVC)
|
||||||
|
if (LLAMA_AVX512)
|
||||||
|
add_compile_options($<$<COMPILE_LANGUAGE:C>:/arch:AVX512>)
|
||||||
|
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/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($<$<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)
|
||||||
|
add_compile_options(-mavx512vbmi)
|
||||||
|
endif()
|
||||||
|
if (LLAMA_AVX512_VNNI)
|
||||||
|
add_compile_options(-mavx512vnni)
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
else()
|
||||||
|
# TODO: support PowerPC
|
||||||
|
message(STATUS "Unknown architecture")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
#
|
||||||
|
# Build libraries
|
||||||
|
#
|
||||||
|
|
||||||
|
if (LLAMA_CUBLAS AND EXISTS ${DIRECTORY}/ggml-cuda.h)
|
||||||
|
cmake_minimum_required(VERSION 3.17)
|
||||||
|
|
||||||
|
find_package(CUDAToolkit)
|
||||||
|
if (CUDAToolkit_FOUND)
|
||||||
|
message(STATUS "cuBLAS found")
|
||||||
|
|
||||||
|
enable_language(CUDA)
|
||||||
|
|
||||||
|
set(GGML_CUDA_SOURCES ${DIRECTORY}/ggml-cuda.cu ${DIRECTORY}/ggml-cuda.h)
|
||||||
|
|
||||||
|
add_compile_definitions(GGML_USE_CUBLAS)
|
||||||
|
|
||||||
|
if (LLAMA_STATIC)
|
||||||
|
set(LLAMA_EXTRA_LIBS ${LLAMA_EXTRA_LIBS} CUDA::cudart_static CUDA::cublas_static CUDA::cublasLt_static)
|
||||||
|
else()
|
||||||
|
set(LLAMA_EXTRA_LIBS ${LLAMA_EXTRA_LIBS} CUDA::cudart CUDA::cublas CUDA::cublasLt)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
else()
|
||||||
|
message(WARNING "cuBLAS not found")
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if (LLAMA_CLBLAST AND EXISTS ${DIRECTORY}/ggml-opencl.h)
|
||||||
|
find_package(CLBlast)
|
||||||
|
if (CLBlast_FOUND)
|
||||||
|
message(STATUS "CLBlast found")
|
||||||
|
|
||||||
|
set(GGML_OPENCL_SOURCES ${DIRECTORY}/ggml-opencl.c ${DIRECTORY}/ggml-opencl.h)
|
||||||
|
|
||||||
|
add_compile_definitions(GGML_USE_CLBLAST)
|
||||||
|
|
||||||
|
set(LLAMA_EXTRA_LIBS ${LLAMA_EXTRA_LIBS} clblast)
|
||||||
|
else()
|
||||||
|
message(WARNING "CLBlast not found")
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
|
||||||
|
add_library(ggml${SUFFIX} OBJECT
|
||||||
|
${DIRECTORY}/ggml.c
|
||||||
|
${DIRECTORY}/ggml.h
|
||||||
|
${GGML_CUDA_SOURCES}
|
||||||
|
${GGML_OPENCL_SOURCES})
|
||||||
|
|
||||||
|
target_include_directories(ggml${SUFFIX} PUBLIC ${DIRECTORY})
|
||||||
|
target_compile_features(ggml${SUFFIX} PUBLIC c_std_11) # don't bump
|
||||||
|
target_link_libraries(ggml${SUFFIX} PUBLIC Threads::Threads ${LLAMA_EXTRA_LIBS})
|
||||||
|
|
||||||
|
if (BUILD_SHARED_LIBS)
|
||||||
|
set_target_properties(ggml${SUFFIX} PROPERTIES POSITION_INDEPENDENT_CODE ON)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if (WITH_LLAMA)
|
||||||
|
# Backwards compatibility with old llama.cpp versions
|
||||||
|
set(LLAMA_UTIL_SOURCE_FILE llama-util.h)
|
||||||
|
if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${DIRECTORY}/${LLAMA_UTIL_SOURCE_FILE})
|
||||||
|
set(LLAMA_UTIL_SOURCE_FILE llama_util.h)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
add_library(llama${SUFFIX}
|
||||||
|
${DIRECTORY}/llama.cpp
|
||||||
|
${DIRECTORY}/llama.h
|
||||||
|
${DIRECTORY}/${LLAMA_UTIL_SOURCE_FILE})
|
||||||
|
|
||||||
|
target_include_directories(llama${SUFFIX} PUBLIC ${DIRECTORY})
|
||||||
|
target_compile_features(llama${SUFFIX} PUBLIC cxx_std_11) # don't bump
|
||||||
|
target_link_libraries(llama${SUFFIX} PRIVATE ggml${SUFFIX} ${LLAMA_EXTRA_LIBS})
|
||||||
|
|
||||||
|
if (BUILD_SHARED_LIBS)
|
||||||
|
set_target_properties(llama${SUFFIX} PROPERTIES POSITION_INDEPENDENT_CODE ON)
|
||||||
|
target_compile_definitions(llama${SUFFIX} PRIVATE LLAMA_SHARED LLAMA_BUILD)
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if (GGML_CUDA_SOURCES)
|
||||||
|
message(STATUS "GGML CUDA sources found, configuring CUDA architecture")
|
||||||
|
set_property(TARGET ggml${SUFFIX} PROPERTY CUDA_ARCHITECTURES OFF)
|
||||||
|
set_property(TARGET ggml${SUFFIX} PROPERTY CUDA_SELECT_NVCC_ARCH_FLAGS "Auto")
|
||||||
|
if (WITH_LLAMA)
|
||||||
|
set_property(TARGET llama${SUFFIX} PROPERTY CUDA_ARCHITECTURES OFF)
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
endfunction()
|
@ -0,0 +1,90 @@
|
|||||||
|
#include "llmodel.h"
|
||||||
|
#include "dlhandle.h"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <fstream>
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
|
static Dlhandle *get_implementation(std::ifstream& f, const std::string& buildVariant) {
|
||||||
|
// Collect all model implementation libraries
|
||||||
|
// NOTE: allocated on heap so we leak intentionally on exit so we have a chance to clean up the
|
||||||
|
// individual models without the cleanup of the static list interfering
|
||||||
|
static auto* libs = new std::vector<Dlhandle>([] () {
|
||||||
|
std::vector<Dlhandle> fres;
|
||||||
|
|
||||||
|
auto search_in_directory = [&](const std::filesystem::path& path) {
|
||||||
|
// Iterate over all libraries
|
||||||
|
for (const auto& f : std::filesystem::directory_iterator(path)) {
|
||||||
|
// Get path
|
||||||
|
const auto& p = f.path();
|
||||||
|
// Check extension
|
||||||
|
if (p.extension() != LIB_FILE_EXT) continue;
|
||||||
|
// Add to list if model implementation
|
||||||
|
try {
|
||||||
|
Dlhandle dl(p.string());
|
||||||
|
if (dl.get<bool(uint32_t)>("is_g4a_backend_model_implementation")) {
|
||||||
|
fres.emplace_back(std::move(dl));
|
||||||
|
}
|
||||||
|
} catch (...) {}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
search_in_directory(".");
|
||||||
|
#if defined(__APPLE__)
|
||||||
|
search_in_directory("../../../");
|
||||||
|
#endif
|
||||||
|
return fres;
|
||||||
|
}());
|
||||||
|
// Iterate over all libraries
|
||||||
|
for (auto& dl : *libs) {
|
||||||
|
f.seekg(0);
|
||||||
|
// Check that magic matches
|
||||||
|
auto magic_match = dl.get<bool(std::ifstream&)>("magic_match");
|
||||||
|
if (!magic_match || !magic_match(f)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// Check that build variant is correct
|
||||||
|
auto get_build_variant = dl.get<const char *()>("get_build_variant");
|
||||||
|
if (buildVariant != (get_build_variant?get_build_variant():"default")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// Looks like we're good to go, return this dlhandle
|
||||||
|
return &dl;
|
||||||
|
}
|
||||||
|
// Nothing found, so return nothing
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool requires_avxonly() {
|
||||||
|
#ifdef __x86_64__
|
||||||
|
return !__builtin_cpu_supports("avx2") && !__builtin_cpu_supports("fma");
|
||||||
|
#else
|
||||||
|
return false; // Don't know how to handle ARM
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
LLModel *LLModel::construct(const std::string &modelPath, std::string buildVariant) {
|
||||||
|
//TODO: Auto-detect
|
||||||
|
if (buildVariant == "auto") {
|
||||||
|
if (requires_avxonly()) {
|
||||||
|
buildVariant = "avxonly";
|
||||||
|
} else {
|
||||||
|
buildVariant = "default";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Read magic
|
||||||
|
std::ifstream f(modelPath, std::ios::binary);
|
||||||
|
if (!f) return nullptr;
|
||||||
|
// Get correct implementation
|
||||||
|
auto impl = get_implementation(f, buildVariant);
|
||||||
|
if (!impl) return nullptr;
|
||||||
|
f.close();
|
||||||
|
// Get inference constructor
|
||||||
|
auto constructor = impl->get<LLModel *()>("construct");
|
||||||
|
if (!constructor) return nullptr;
|
||||||
|
// Construct llmodel implementation
|
||||||
|
auto fres = constructor();
|
||||||
|
// Return final instance
|
||||||
|
return fres;
|
||||||
|
}
|
Loading…
Reference in New Issue