Skip to content

Commit

Permalink
feat(android): enable logging (llama.cpp, log.h) on debug mode (#26)
Browse files Browse the repository at this point in the history
* feat(android): enable logging (llama.cpp, log.h) on debug mode

* fix(android): enable logging on debug mode
  • Loading branch information
jhen0409 authored Oct 12, 2023
1 parent 83dc4a9 commit 2e7c00a
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 4 deletions.
4 changes: 4 additions & 0 deletions android/src/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ include_directories(${RNLLAMA_LIB_DIR})

target_compile_options(${RNLLAMA_LIBRARY_NAME} PRIVATE -DLM_GGML_USE_K_QUANTS -pthread)

if (${CMAKE_BUILD_TYPE} STREQUAL "Debug")
target_compile_options(${RNLLAMA_LIBRARY_NAME} PRIVATE -DRNLLAMA_ANDROID_ENABLE_LOGGING)
endif ()

# NOTE: If you want to debug the native code, you can uncomment if and endif
# if (NOT ${CMAKE_BUILD_TYPE} STREQUAL "Debug")

Expand Down
11 changes: 11 additions & 0 deletions cpp/llama.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,17 @@ static void llama_log_callback_default(lm_ggml_log_level level, const char * tex
#define LLAMA_LOG_WARN(...) llama_log_internal(LM_GGML_LOG_LEVEL_WARN , __VA_ARGS__)
#define LLAMA_LOG_ERROR(...) llama_log_internal(LM_GGML_LOG_LEVEL_ERROR, __VA_ARGS__)

#if defined(__ANDROID__) && defined(RNLLAMA_ANDROID_ENABLE_LOGGING)
#include <android/log.h>
#define LLAMA_ANDROID_TAG "RNLLAMA_LOG_ANDROID"
#undef LLAMA_LOG_INFO
#undef LLAMA_LOG_WARN
#undef LLAMA_LOG_ERROR
#define LLAMA_LOG_INFO(...) __android_log_print(ANDROID_LOG_INFO , LLAMA_ANDROID_TAG, __VA_ARGS__)
#define LLAMA_LOG_WARN(...) __android_log_print(ANDROID_LOG_WARN , LLAMA_ANDROID_TAG, __VA_ARGS__)
#define LLAMA_LOG_ERROR(...) __android_log_print(ANDROID_LOG_ERROR, LLAMA_ANDROID_TAG, __VA_ARGS__)
#endif // __ANDROID__

//
// helpers
//
Expand Down
13 changes: 13 additions & 0 deletions cpp/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,19 @@ enum LogTriState
#define LOG_TEELN(str, ...) LOG_TEE_IMPL("%s" str, "", __VA_ARGS__, "\n")
#endif

#if defined(__ANDROID__) && defined(RNLLAMA_ANDROID_ENABLE_LOGGING)
#include <android/log.h>
#define LLAMA_ANDROID_LOG_TAG "RNLLAMA_LOG_ANDROID"
#undef LOG
#undef LOG_TEE
#undef LOGLN
#undef LOG_TEELN
#define LOG(...) __android_log_print(ANDROID_LOG_INFO, LLAMA_ANDROID_LOG_TAG, __VA_ARGS__)
#define LOG_TEE(...) __android_log_print(ANDROID_LOG_INFO, LLAMA_ANDROID_LOG_TAG, __VA_ARGS__)
#define LOGLN(...) __android_log_print(ANDROID_LOG_INFO, LLAMA_ANDROID_LOG_TAG, __VA_ARGS__)
#define LOG_TEELN(...) __android_log_print(ANDROID_LOG_INFO, LLAMA_ANDROID_LOG_TAG, __VA_ARGS__)
#endif

// INTERNAL, DO NOT USE
inline FILE *log_handler1_impl(bool change = false, LogTriState disable = LogTriStateSame, const std::string & filename = LOG_DEFAULT_FILE_NAME, FILE *target = nullptr)
{
Expand Down
1 change: 1 addition & 0 deletions scripts/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,5 @@ echo "Replacement completed successfully!"
yarn example

# Apply patch
patch -p0 -d ./cpp < ./scripts/log.h.patch
patch -p0 -d ./cpp < ./scripts/llama.cpp.patch
26 changes: 22 additions & 4 deletions scripts/llama.cpp.patch
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
--- llama.cpp.orig 2023-10-11 11:00:04
+++ llama.cpp 2023-10-11 11:00:05
@@ -736,16 +736,16 @@

--- llama.cpp.orig 2023-10-12 09:34:44
+++ llama.cpp 2023-10-12 09:36:38
@@ -102,6 +102,17 @@
#define LLAMA_LOG_WARN(...) llama_log_internal(LM_GGML_LOG_LEVEL_WARN , __VA_ARGS__)
#define LLAMA_LOG_ERROR(...) llama_log_internal(LM_GGML_LOG_LEVEL_ERROR, __VA_ARGS__)

+#if defined(__ANDROID__) && defined(RNLLAMA_ANDROID_ENABLE_LOGGING)
+#include <android/log.h>
+#define LLAMA_ANDROID_TAG "RNLLAMA_LOG_ANDROID"
+#undef LLAMA_LOG_INFO
+#undef LLAMA_LOG_WARN
+#undef LLAMA_LOG_ERROR
+#define LLAMA_LOG_INFO(...) __android_log_print(ANDROID_LOG_INFO , LLAMA_ANDROID_TAG, __VA_ARGS__)
+#define LLAMA_LOG_WARN(...) __android_log_print(ANDROID_LOG_WARN , LLAMA_ANDROID_TAG, __VA_ARGS__)
+#define LLAMA_LOG_ERROR(...) __android_log_print(ANDROID_LOG_ERROR, LLAMA_ANDROID_TAG, __VA_ARGS__)
+#endif // __ANDROID__
+
//
// helpers
//
@@ -736,16 +747,16 @@

if (prefetch > 0) {
// Advise the kernel to preload the mapped memory
- if (posix_madvise(addr, std::min(file->size, prefetch), POSIX_MADV_WILLNEED)) {
Expand Down
22 changes: 22 additions & 0 deletions scripts/log.h.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--- log.h.orig 2023-10-12 09:37:10
+++ log.h 2023-10-12 09:36:47
@@ -313,6 +313,19 @@
#define LOG_TEELN(str, ...) LOG_TEE_IMPL("%s" str, "", __VA_ARGS__, "\n")
#endif

+#if defined(__ANDROID__) && defined(RNLLAMA_ANDROID_ENABLE_LOGGING)
+#include <android/log.h>
+#define LLAMA_ANDROID_LOG_TAG "RNLLAMA_LOG_ANDROID"
+#undef LOG
+#undef LOG_TEE
+#undef LOGLN
+#undef LOG_TEELN
+#define LOG(...) __android_log_print(ANDROID_LOG_INFO, LLAMA_ANDROID_LOG_TAG, __VA_ARGS__)
+#define LOG_TEE(...) __android_log_print(ANDROID_LOG_INFO, LLAMA_ANDROID_LOG_TAG, __VA_ARGS__)
+#define LOGLN(...) __android_log_print(ANDROID_LOG_INFO, LLAMA_ANDROID_LOG_TAG, __VA_ARGS__)
+#define LOG_TEELN(...) __android_log_print(ANDROID_LOG_INFO, LLAMA_ANDROID_LOG_TAG, __VA_ARGS__)
+#endif
+
// INTERNAL, DO NOT USE
inline FILE *log_handler1_impl(bool change = false, LogTriState disable = LogTriStateSame, const std::string & filename = LOG_DEFAULT_FILE_NAME, FILE *target = nullptr)
{

0 comments on commit 2e7c00a

Please sign in to comment.