updated CMakeLists.txt and added JNI implementation to support building this library as a dependency in Android Studio with NDK
This commit is contained in:
parent
d1f563a743
commit
c3b4efc89e
2 changed files with 162 additions and 121 deletions
|
@ -31,7 +31,6 @@ else()
|
|||
endif()
|
||||
endif()
|
||||
|
||||
|
||||
#
|
||||
# Option list
|
||||
#
|
||||
|
@ -58,6 +57,7 @@ option(LLAMA_AVX512 "llama: enable AVX512"
|
|||
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)
|
||||
|
@ -113,7 +113,6 @@ endif()
|
|||
#
|
||||
# Compile flags
|
||||
#
|
||||
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED true)
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
|
@ -140,6 +139,7 @@ endif()
|
|||
|
||||
if(APPLE AND LLAMA_ACCELERATE)
|
||||
find_library(ACCELERATE_FRAMEWORK Accelerate)
|
||||
|
||||
if(ACCELERATE_FRAMEWORK)
|
||||
message(STATUS "Accelerate framework found")
|
||||
|
||||
|
@ -154,11 +154,14 @@ if (LLAMA_BLAS)
|
|||
if(LLAMA_STATIC)
|
||||
set(BLA_STATIC ON)
|
||||
endif()
|
||||
|
||||
if($(CMAKE_VERSION) VERSION_GREATER_EQUAL 3.22)
|
||||
set(BLA_SIZEOF_INTEGER 8)
|
||||
endif()
|
||||
|
||||
set(BLA_VENDOR ${LLAMA_BLAS_VENDOR})
|
||||
find_package(BLAS)
|
||||
|
||||
if(BLAS_FOUND)
|
||||
message(STATUS "BLAS found, Libraries: ${BLAS_LIBRARIES}")
|
||||
|
||||
|
@ -179,6 +182,7 @@ if (LLAMA_CUBLAS)
|
|||
cmake_minimum_required(VERSION 3.17)
|
||||
|
||||
find_package(CUDAToolkit)
|
||||
|
||||
if(CUDAToolkit_FOUND)
|
||||
message(STATUS "cuBLAS found")
|
||||
|
||||
|
@ -228,6 +232,7 @@ endif()
|
|||
|
||||
if(LLAMA_CLBLAST)
|
||||
find_package(CLBlast)
|
||||
|
||||
if(CLBlast_FOUND)
|
||||
message(STATUS "CLBlast found")
|
||||
|
||||
|
@ -269,7 +274,6 @@ if (LLAMA_ALL_WARNINGS)
|
|||
"$<$<COMPILE_LANGUAGE:C>:${c_flags}>"
|
||||
"$<$<COMPILE_LANGUAGE:CXX>:${cxx_flags}>"
|
||||
)
|
||||
|
||||
endif()
|
||||
|
||||
if(MSVC)
|
||||
|
@ -283,6 +287,7 @@ endif()
|
|||
if(LLAMA_LTO)
|
||||
include(CheckIPOSupported)
|
||||
check_ipo_supported(RESULT result OUTPUT output)
|
||||
|
||||
if(result)
|
||||
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
|
||||
else()
|
||||
|
@ -294,16 +299,20 @@ endif()
|
|||
# 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()
|
||||
|
@ -311,33 +320,47 @@ 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")
|
||||
# Apple M1, M2, etc.
|
||||
# Raspberry Pi 3, 4, Zero 2 (64-bit)
|
||||
if(NOT DEFINED ANDROID_NDK)
|
||||
add_compile_options(-mcpu=native)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "armv6")
|
||||
# Raspberry Pi 1, Zero
|
||||
if(NOT DEFINED ANDROID_NDK)
|
||||
add_compile_options(-mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "armv7")
|
||||
# Raspberry Pi 2
|
||||
if(NOT DEFINED ANDROID_NDK)
|
||||
add_compile_options(-mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "armv8")
|
||||
# Raspberry Pi 3, 4, Zero 2 (32-bit)
|
||||
if(NOT DEFINED ANDROID_NDK)
|
||||
add_compile_options(-mfp16-format=ieee -mno-unaligned-access)
|
||||
endif()
|
||||
endif()
|
||||
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.
|
||||
|
@ -346,6 +369,7 @@ elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^(x86_64|i686|AMD64)$")
|
|||
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__>)
|
||||
|
@ -361,22 +385,28 @@ elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^(x86_64|i686|AMD64)$")
|
|||
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()
|
||||
|
@ -384,6 +414,7 @@ elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^(x86_64|i686|AMD64)$")
|
|||
elseif(${CMAKE_SYSTEM_PROCESSOR} MATCHES "ppc64")
|
||||
message(STATUS "PowerPC detected")
|
||||
add_compile_options(-mcpu=native -mtune=native)
|
||||
|
||||
# TODO: Add targets for Power8/Power9 (Altivec/VSX) and Power10(MMA) and query for big endian systems (ppc64/le/be)
|
||||
else()
|
||||
message(STATUS "Unknown architecture")
|
||||
|
@ -392,7 +423,6 @@ endif()
|
|||
#
|
||||
# Build libraries
|
||||
#
|
||||
|
||||
add_library(ggml OBJECT
|
||||
ggml.c
|
||||
ggml.h
|
||||
|
@ -413,6 +443,7 @@ add_library(llama
|
|||
llama.cpp
|
||||
llama.h
|
||||
llama-util.h
|
||||
llama-jni.cpp
|
||||
)
|
||||
|
||||
target_include_directories(llama PUBLIC .)
|
||||
|
@ -434,11 +465,9 @@ if (GGML_SOURCES_CUDA)
|
|||
set_property(TARGET llama PROPERTY CUDA_ARCHITECTURES OFF)
|
||||
endif()
|
||||
|
||||
|
||||
#
|
||||
# programs, examples and tests
|
||||
#
|
||||
|
||||
if(LLAMA_BUILD_TESTS AND NOT CMAKE_JS_VERSION)
|
||||
include(CTest)
|
||||
add_subdirectory(tests)
|
||||
|
|
12
llama-jni.cpp
Normal file
12
llama-jni.cpp
Normal file
|
@ -0,0 +1,12 @@
|
|||
#include <jni.h>
|
||||
#include "llama.h"
|
||||
|
||||
//
|
||||
// Created by gcpth on 05/06/2023.
|
||||
//
|
||||
|
||||
extern "C"
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_layla_LlamaCpp_llama_1init_1backend(JNIEnv *env, jclass clazz) {
|
||||
llama_init_backend();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue