Skip to content

Commit

Permalink
[llamacpp] Add .mk files for ndk-build support of llama.cpp
Browse files Browse the repository at this point in the history
This patch adds mk files for ndk-build of llama.cpp.

Signed-off-by: Yelin Jeong <[email protected]>
  • Loading branch information
niley7464 authored and jaeyun-jung committed Dec 20, 2024
1 parent 48ca203 commit eba2cbe
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
48 changes: 48 additions & 0 deletions java/android/nnstreamer/src/main/jni/Android-llamacpp-prebuilt.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#------------------------------------------------------
# llama.cpp : Inference of Meta's LLaMA model (and others) in pure C/C++
# https://github.com/ggerganov/llama.cpp (original upstream repository
#
# This mk file defines a prebuilt shared library for the llamacpp module.
#------------------------------------------------------

LOCAL_PATH := $(call my-dir)

ifndef LLAMACPP_LIB_PATH
$(error LLAMACPP_LIB_PATH is not defined!)
endif

# Check Target ABI. Only supports arm64 and x86_64.
ifeq ($(TARGET_ARCH_ABI),arm64-v8a)
LLAMACPP_LIB_PATH := $(LLAMACPP_LIB_PATH)/arm64
else ifeq ($(TARGET_ARCH_ABI),x86_64)
LLAMACPP_LIB_PATH := $(LLAMACPP_LIB_PATH)/x86_64
else
$(error Target arch ABI not supported: $(TARGET_ARCH_ABI))
endif

LLAMACPP_PREBUILT_LIBS :=

#------------------------------------------------------
# llamacpp prebuilt shared libraries
#------------------------------------------------------
include $(CLEAR_VARS)
LOCAL_MODULE := llama
LOCAL_SRC_FILES := $(LLAMACPP_LIB_PATH)/libllama.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := ggml
LOCAL_SRC_FILES := $(LLAMACPP_LIB_PATH)/libggml.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := ggml-cpu
LOCAL_SRC_FILES := $(LLAMACPP_LIB_PATH)/libggml-cpu.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := ggml-base
LOCAL_SRC_FILES := $(LLAMACPP_LIB_PATH)/libggml-base.so
include $(PREBUILT_SHARED_LIBRARY)
LLAMACPP_PREBUILT_LIBS += llama ggml ggml-cpu ggml-base

23 changes: 23 additions & 0 deletions java/android/nnstreamer/src/main/jni/Android-llamacpp.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
LOCAL_PATH := $(call my-dir)

LLAMACPP_DIR := $(LOCAL_PATH)/llamacpp
LLAMACPP_INCLUDES := $(LLAMACPP_DIR)/include
LLAMACPP_LIB_PATH := $(LLAMACPP_DIR)/lib

#------------------------------------------------------
# Import LLAMACPP_PREBUILT_LIBS
#------------------------------------------------------
include $(LOCAL_PATH)/Android-llamacpp-prebuilt.mk

#------------------------------------------------------
# tensor-filter sub-plugin for llamacpp
#------------------------------------------------------
include $(CLEAR_VARS)
LOCAL_MODULE := llamacpp-subplugin
LOCAL_SRC_FILES := $(NNSTREAMER_FILTER_LLAMACPP_SRCS)
LOCAL_CXXFLAGS := -O3 -fPIC -frtti -fexceptions $(NNS_API_FLAGS)
LOCAL_C_INCLUDES := $(LLAMACPP_INCLUDES) $(NNSTREAMER_INCLUDES) $(GST_HEADERS_COMMON)
LOCAL_SHARED_LIBRARIES := $(LLAMACPP_PREBUILT_LIBS)
LOCAL_STATIC_LIBRARIES := nnstreamer

include $(BUILD_STATIC_LIBRARY)
8 changes: 8 additions & 0 deletions java/android/nnstreamer/src/main/jni/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ endif
endif

ENABLE_LLAMA2C := false
ENABLE_LLAMACPP := false

NNS_API_FLAGS := -DVERSION=\"$(ML_API_VERSION)\" -DVERSION_MAJOR=$(ML_API_VERSION_MAJOR) -DVERSION_MINOR=$(ML_API_VERSION_MINOR) -DVERSION_MICRO=$(ML_API_VERSION_MICRO)
NNS_SUBPLUGINS :=
Expand Down Expand Up @@ -170,6 +171,13 @@ NNS_SUBPLUGINS += llama2c-subplugin
include $(LOCAL_PATH)/Android-llama2c.mk
endif

ifeq ($(ENABLE_LLAMACPP), true)
NNS_API_FLAGS += -DENABLE_LLAMACPP=1
NNS_SUBPLUGINS += llamacpp-subplugin

include $(LOCAL_PATH)/Android-llamacpp.mk
endif

ifneq ($(NNSTREAMER_API_OPTION),single)
ifeq ($(ENABLE_FLATBUF),true)
include $(LOCAL_PATH)/Android-flatbuf.mk
Expand Down
6 changes: 6 additions & 0 deletions java/android/nnstreamer/src/main/jni/nnstreamer-native-api.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ extern void init_filter_mxnet (void);
#if defined (ENABLE_LLAMA2C)
extern void init_filter_llama2c (void);
#endif
#if defined (ENABLE_LLAMACPP)
extern void init_filter_llamacpp (void);
#endif

#if GST_CHECK_VERSION(1, 24, 0)
/**
Expand Down Expand Up @@ -958,6 +961,9 @@ nnstreamer_native_initialize (JNIEnv * env, jobject context)
#if defined (ENABLE_LLAMA2C)
init_filter_llama2c ();
#endif
#if defined (ENABLE_LLAMACPP)
init_filter_llamacpp ();
#endif
#endif /* __ANDROID__ */
nns_is_initilaized = TRUE;
}
Expand Down

0 comments on commit eba2cbe

Please sign in to comment.