diff --git a/examples/llama.android/gradle.properties b/examples/llama.android/gradle.properties index 2cbd6d19d3..8888cc9c51 100644 --- a/examples/llama.android/gradle.properties +++ b/examples/llama.android/gradle.properties @@ -21,3 +21,4 @@ kotlin.code.style=official # resources declared in the library itself and none from the library's dependencies, # thereby reducing the size of the R class for that library android.nonTransitiveRClass=true +android.native.buildOutput=verbose diff --git a/examples/llama.android/llama/build.gradle.kts b/examples/llama.android/llama/build.gradle.kts index e0bab27438..8cdb978eb4 100644 --- a/examples/llama.android/llama/build.gradle.kts +++ b/examples/llama.android/llama/build.gradle.kts @@ -21,19 +21,8 @@ android { externalNativeBuild { cmake { arguments += "-DCMAKE_BUILD_TYPE=Release" - arguments += "-DCMAKE_C_FLAGS=\"-march=armv8.7a+dotprod+i8mm+sve\"" - arguments += "-DCMAKE_CXX_FLAGS=\"-march=armv8.7a+dotprod+i8mm+sve\"" - - arguments += "-DGGML_CPU_KLEIDIAI=ON" - arguments += "-DGGML_LLAMAFILE=OFF" - - arguments += "-DLLAMA_BUILD_COMMON=ON" - arguments += "-DLLAMA_CURL=OFF" - - cppFlags += listOf() - arguments += listOf() - - cppFlags("") + arguments += "-DCMAKE_MESSAGE_LOG_LEVEL=DEBUG" + arguments += "-DCMAKE_VERBOSE_MAKEFILE=ON" } } } diff --git a/examples/llama.android/llama/src/main/cpp/CMakeLists.txt b/examples/llama.android/llama/src/main/cpp/CMakeLists.txt index 6119fe09b0..e524a61df2 100644 --- a/examples/llama.android/llama/src/main/cpp/CMakeLists.txt +++ b/examples/llama.android/llama/src/main/cpp/CMakeLists.txt @@ -1,50 +1,89 @@ -# For more information about using CMake with Android Studio, read the -# documentation: https://d.android.com/studio/projects/add-native-code.html. -# For more examples on how to use CMake, see https://github.com/android/ndk-samples. - # Sets the minimum CMake version required for this project. cmake_minimum_required(VERSION 3.22.1) -# Declares the project name. The project name can be accessed via ${ PROJECT_NAME}, -# Since this is the top level CMakeLists.txt, the project name is also accessible -# with ${CMAKE_PROJECT_NAME} (both CMake variables are in-sync within the top level -# build script scope). -project("llama-android") +project("llama_android" VERSION 1.0.0 LANGUAGES C CXX) +set(CMAKE_C_STANDARD 11) +set(CMAKE_C_STANDARD_REQUIRED true) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED true) + +set(GGML_CPU_KLEIDIAI ON CACHE BOOL "" FORCE) +set(GGML_LLAMAFILE OFF CACHE BOOL "" FORCE) +set(LLAMA_BUILD_COMMON ON CACHE BOOL "" FORCE) +set(LLAMA_CURL OFF CACHE BOOL "" FORCE) + +string(APPEND CMAKE_C_FLAGS " -march=armv8.7-a+dotprod+i8mm+sve") +string(APPEND CMAKE_CXX_FLAGS " -march=armv8.7-a+dotprod+i8mm+sve") + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}" CACHE STRING "" FORCE) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" CACHE STRING "" FORCE) + +## CMake definitions +#add_compile_definitions( +# GGML_CPU_KLEIDIAI=ON +# GGML_LLAMAFILE=OFF +# LLAMA_BUILD_COMMON=ON +# LLAMA_CURL=OFF +#) + +# Force Release only when Gradle didn't specify one +if(NOT DEFINED CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE Release CACHE STRING "" FORCE) +endif() + +## Also provides "common" #include(FetchContent) #FetchContent_Declare( # llama # GIT_REPOSITORY https://github.com/ggml-org/llama.cpp # GIT_TAG master #) - -# Also provides "common" #FetchContent_MakeAvailable(llama) -# Creates and names a library, sets it as either STATIC -# or SHARED, and provides the relative paths to its source code. -# You can define multiple libraries, and CMake builds them for you. -# Gradle automatically packages shared libraries with your APK. -# -# In this top level CMakeLists.txt, ${CMAKE_PROJECT_NAME} is used to define -# the target library name; in the sub-module's CMakeLists.txt, ${PROJECT_NAME} -# is preferred for the same purpose. -# - -#load local llama.cpp +# Add local llama.cpp as subdirectory add_subdirectory(../../../../../../ build-llama) -# In order to load a library into your app from Java/Kotlin, you must call -# System.loadLibrary() and pass the name of the library defined here; -# for GameActivity/NativeActivity derived applications, the same library name must be -# used in the AndroidManifest.xml file. +#function(build_llama_tier tier march_flag) +# set(LIB_NAME "${CMAKE_PROJECT_NAME}_${tier}") +# +## set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=armv8.7-a+i8mm") +## set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=armv8.7-a+i8mm") +# +# add_library(${LIB_NAME} SHARED llama-android.cpp) +# + +# MIGHT NOT WORK???? +# target_include_directories(${LIB_NAME} PRIVATE +# ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../common) +# ????? + +# target_compile_options(${LIB_NAME} PRIVATE "-march=${march_flag}") +# +# target_link_libraries(${LIB_NAME} +# llama +# common +# android +# log +# ) +# +# set_target_properties(${LIB_NAME} PROPERTIES +# OUTPUT_NAME "llama_android_${tier}" +# ) +#endfunction() +# +#build_llama_tier(t0 "armv8-a+simd") +#build_llama_tier(t1 "armv8.2-a+dotprod") +#build_llama_tier(t2 "armv8.6-a+dotprod+i8mm") +#build_llama_tier(t3 "armv9-a+dotprod+i8mm+sve+sve2") +#build_llama_tier(t4 "armv9.2-a+dotprod+i8mm+sve+sve2+sme") + + add_library(${CMAKE_PROJECT_NAME} SHARED # List C/C++ source files with relative paths to this CMakeLists.txt. llama-android.cpp) -# Specifies libraries CMake should link to your target library. You -# can link libraries from various origins, such as libraries defined in this -# build script, prebuilt third-party libraries, or Android system libraries. target_link_libraries(${CMAKE_PROJECT_NAME} # List libraries link to the target library llama diff --git a/examples/llama.android/llama/src/main/java/android/llama/cpp/LLamaAndroid.kt b/examples/llama.android/llama/src/main/java/android/llama/cpp/LLamaAndroid.kt index b0cebcec2f..32f81deedf 100644 --- a/examples/llama.android/llama/src/main/java/android/llama/cpp/LLamaAndroid.kt +++ b/examples/llama.android/llama/src/main/java/android/llama/cpp/LLamaAndroid.kt @@ -76,6 +76,7 @@ class LLamaAndroid private constructor() : InferenceEngine { } _state.value = State.Initializing + Log.i(TAG, "Loading native library $LIB_LLAMA_ANDROID") System.loadLibrary(LIB_LLAMA_ANDROID) init() _state.value = State.Initialized @@ -238,8 +239,8 @@ class LLamaAndroid private constructor() : InferenceEngine { companion object { private val TAG = LLamaAndroid::class.simpleName - private const val LIB_LLAMA_ANDROID = "llama-android" - private const val DEFAULT_PREDICT_LENGTH = 64 + // TODO-han.yin: replace with dynamic loader + private const val LIB_LLAMA_ANDROID = "llama_android" // Enforce only one instance of Llm. private val _instance: LLamaAndroid = LLamaAndroid()