@novastera-oss/llamarn 0.1.4-beta.2 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/CMakeLists.txt +104 -87
- package/android/src/main/jniLibs/arm64-v8a/libggml-base.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/libggml-cpu.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/libggml.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/libllama.so +0 -0
- package/android/src/main/jniLibs/x86_64/libggml-base.so +0 -0
- package/android/src/main/jniLibs/x86_64/libggml-cpu.so +0 -0
- package/android/src/main/jniLibs/x86_64/libggml.so +0 -0
- package/android/src/main/jniLibs/x86_64/libllama.so +0 -0
- package/cpp/LlamaCppModel.cpp +148 -5
- package/cpp/LlamaCppModel.h +11 -2
- package/cpp/PureCppImpl.cpp +3 -3
- package/cpp/PureCppImpl.h +3 -0
- package/lib/module/NativeRNLlamaCpp.js.map +1 -1
- package/lib/typescript/src/NativeRNLlamaCpp.d.ts +3 -0
- package/lib/typescript/src/NativeRNLlamaCpp.d.ts.map +1 -1
- package/package.json +2 -1
- package/src/NativeRNLlamaCpp.ts +1 -0
package/android/CMakeLists.txt
CHANGED
|
@@ -7,10 +7,10 @@ set(CMAKE_VERBOSE_MAKEFILE ON)
|
|
|
7
7
|
get_filename_component(MODULE_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/.." ABSOLUTE)
|
|
8
8
|
get_filename_component(CPP_DIR "${MODULE_ROOT}/cpp" ABSOLUTE)
|
|
9
9
|
|
|
10
|
-
# Define the path to jniLibs
|
|
10
|
+
# Define the path to jniLibs. This assumes CMakeLists.txt is in android/
|
|
11
11
|
set(JNI_LIBS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/main/jniLibs)
|
|
12
12
|
|
|
13
|
-
# Define the path to llama.cpp directory
|
|
13
|
+
# Define the path to llama.cpp directory
|
|
14
14
|
set(LLAMA_CPP_DIR "${CPP_DIR}/llama.cpp")
|
|
15
15
|
|
|
16
16
|
# Make sure the llama.cpp submodule exists
|
|
@@ -18,69 +18,40 @@ if(NOT EXISTS "${LLAMA_CPP_DIR}/CMakeLists.txt")
|
|
|
18
18
|
message(FATAL_ERROR "llama.cpp submodule not found at ${LLAMA_CPP_DIR}. Please run 'git submodule update --init --recursive'")
|
|
19
19
|
endif()
|
|
20
20
|
|
|
21
|
-
#
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
# Find Vulkan (available in NDK 23+)
|
|
22
|
+
find_package(Vulkan QUIET)
|
|
23
|
+
if(Vulkan_FOUND)
|
|
24
|
+
message(STATUS "Found Vulkan: ${Vulkan_LIBRARIES}")
|
|
25
|
+
else()
|
|
26
|
+
message(STATUS "Vulkan not found - GPU acceleration will be limited")
|
|
24
27
|
endif()
|
|
25
28
|
|
|
26
|
-
#
|
|
29
|
+
# Add the prebuilt libraries as IMPORTED with IMPORTED_NO_SONAME to avoid absolute path embedding
|
|
27
30
|
add_library(llama SHARED IMPORTED)
|
|
28
31
|
set_target_properties(llama PROPERTIES
|
|
29
32
|
IMPORTED_LOCATION ${JNI_LIBS_DIR}/${ANDROID_ABI}/libllama.so
|
|
30
33
|
IMPORTED_NO_SONAME TRUE)
|
|
31
34
|
|
|
32
|
-
add_library(ggml SHARED IMPORTED)
|
|
33
|
-
set_target_properties(ggml PROPERTIES
|
|
34
|
-
IMPORTED_LOCATION ${JNI_LIBS_DIR}/${ANDROID_ABI}/libggml.so
|
|
35
|
-
IMPORTED_NO_SONAME TRUE)
|
|
36
|
-
|
|
37
35
|
add_library(ggml-base SHARED IMPORTED)
|
|
38
36
|
set_target_properties(ggml-base PROPERTIES
|
|
39
37
|
IMPORTED_LOCATION ${JNI_LIBS_DIR}/${ANDROID_ABI}/libggml-base.so
|
|
40
38
|
IMPORTED_NO_SONAME TRUE)
|
|
41
39
|
|
|
40
|
+
add_library(ggml SHARED IMPORTED)
|
|
41
|
+
set_target_properties(ggml PROPERTIES
|
|
42
|
+
IMPORTED_LOCATION ${JNI_LIBS_DIR}/${ANDROID_ABI}/libggml.so
|
|
43
|
+
IMPORTED_NO_SONAME TRUE)
|
|
44
|
+
|
|
42
45
|
add_library(ggml-cpu SHARED IMPORTED)
|
|
43
46
|
set_target_properties(ggml-cpu PROPERTIES
|
|
44
47
|
IMPORTED_LOCATION ${JNI_LIBS_DIR}/${ANDROID_ABI}/libggml-cpu.so
|
|
45
48
|
IMPORTED_NO_SONAME TRUE)
|
|
46
49
|
|
|
47
|
-
#
|
|
48
|
-
set(ADDITIONAL_LIBRARIES "")
|
|
49
|
-
|
|
50
|
-
# Check for OpenCL backend
|
|
51
|
-
if(EXISTS ${JNI_LIBS_DIR}/${ANDROID_ABI}/libggml-opencl.so)
|
|
52
|
-
add_library(ggml-opencl SHARED IMPORTED)
|
|
53
|
-
set_target_properties(ggml-opencl PROPERTIES
|
|
54
|
-
IMPORTED_LOCATION ${JNI_LIBS_DIR}/${ANDROID_ABI}/libggml-opencl.so
|
|
55
|
-
IMPORTED_NO_SONAME TRUE)
|
|
56
|
-
list(APPEND ADDITIONAL_LIBRARIES ggml-opencl)
|
|
57
|
-
message(STATUS "Found OpenCL backend for ${ANDROID_ABI}")
|
|
58
|
-
endif()
|
|
59
|
-
|
|
60
|
-
# Check for OpenCL ICD loader
|
|
61
|
-
if(EXISTS ${JNI_LIBS_DIR}/${ANDROID_ABI}/libOpenCL.so)
|
|
62
|
-
add_library(OpenCL SHARED IMPORTED)
|
|
63
|
-
set_target_properties(OpenCL PROPERTIES
|
|
64
|
-
IMPORTED_LOCATION ${JNI_LIBS_DIR}/${ANDROID_ABI}/libOpenCL.so
|
|
65
|
-
IMPORTED_NO_SONAME TRUE)
|
|
66
|
-
list(APPEND ADDITIONAL_LIBRARIES OpenCL)
|
|
67
|
-
message(STATUS "Found OpenCL ICD loader for ${ANDROID_ABI}")
|
|
68
|
-
endif()
|
|
69
|
-
|
|
70
|
-
# Check for Vulkan backend
|
|
71
|
-
if(EXISTS ${JNI_LIBS_DIR}/${ANDROID_ABI}/libggml-vulkan.so)
|
|
72
|
-
add_library(ggml-vulkan SHARED IMPORTED)
|
|
73
|
-
set_target_properties(ggml-vulkan PROPERTIES
|
|
74
|
-
IMPORTED_LOCATION ${JNI_LIBS_DIR}/${ANDROID_ABI}/libggml-vulkan.so
|
|
75
|
-
IMPORTED_NO_SONAME TRUE)
|
|
76
|
-
list(APPEND ADDITIONAL_LIBRARIES ggml-vulkan)
|
|
77
|
-
message(STATUS "Found Vulkan backend for ${ANDROID_ABI}")
|
|
78
|
-
endif()
|
|
79
|
-
|
|
80
|
-
# Create common library with essential llama.cpp common files
|
|
50
|
+
# Create a minimal common library with only essential files that don't require missing GGML symbols
|
|
81
51
|
add_library(
|
|
82
52
|
common
|
|
83
53
|
STATIC
|
|
54
|
+
# Add back essential files now that we have prebuilt GGML libraries
|
|
84
55
|
${CPP_DIR}/llama.cpp/common/build-info.cpp
|
|
85
56
|
${CPP_DIR}/llama.cpp/common/log.cpp
|
|
86
57
|
${CPP_DIR}/llama.cpp/common/common.cpp
|
|
@@ -89,7 +60,6 @@ add_library(
|
|
|
89
60
|
${CPP_DIR}/llama.cpp/common/json-schema-to-grammar.cpp
|
|
90
61
|
)
|
|
91
62
|
|
|
92
|
-
# Create our main React Native module
|
|
93
63
|
add_library(
|
|
94
64
|
RNLlamaCpp
|
|
95
65
|
SHARED
|
|
@@ -100,31 +70,61 @@ add_library(
|
|
|
100
70
|
${CPP_DIR}/rn-completion.cpp
|
|
101
71
|
)
|
|
102
72
|
|
|
103
|
-
# Suppress unused function warnings
|
|
73
|
+
# Suppress unused function warnings for llama.cpp code
|
|
104
74
|
target_compile_options(common PRIVATE -Wno-unused-function)
|
|
105
75
|
target_compile_options(RNLlamaCpp PRIVATE -Wno-unused-function)
|
|
106
76
|
|
|
107
|
-
#
|
|
77
|
+
# Check if Vulkan backend library is available
|
|
78
|
+
set(VULKAN_BACKEND_AVAILABLE FALSE)
|
|
79
|
+
if(EXISTS ${JNI_LIBS_DIR}/${ANDROID_ABI}/libggml-vulkan.so)
|
|
80
|
+
set(VULKAN_BACKEND_AVAILABLE TRUE)
|
|
81
|
+
message(STATUS "Vulkan backend library found for ${ANDROID_ABI}")
|
|
82
|
+
else()
|
|
83
|
+
message(STATUS "Vulkan backend library not found for ${ANDROID_ABI}")
|
|
84
|
+
endif()
|
|
85
|
+
|
|
86
|
+
# Check if OpenCL backend library is available
|
|
87
|
+
set(OPENCL_BACKEND_AVAILABLE FALSE)
|
|
88
|
+
if(EXISTS ${JNI_LIBS_DIR}/${ANDROID_ABI}/libggml-opencl.so AND EXISTS ${JNI_LIBS_DIR}/${ANDROID_ABI}/libOpenCL.so)
|
|
89
|
+
set(OPENCL_BACKEND_AVAILABLE TRUE)
|
|
90
|
+
message(STATUS "OpenCL backend libraries found for ${ANDROID_ABI}")
|
|
91
|
+
else()
|
|
92
|
+
message(STATUS "OpenCL backend libraries not found for ${ANDROID_ABI}")
|
|
93
|
+
endif()
|
|
94
|
+
|
|
95
|
+
# Hybrid backend approach: CPU static (built into main libraries), GPU dynamic
|
|
96
|
+
# CPU backend will be statically linked into main libraries (libggml.so, libllama.so)
|
|
97
|
+
# GPU backends (OpenCL, Vulkan) will be dynamically loaded at runtime only if available
|
|
108
98
|
target_compile_definitions(common PRIVATE
|
|
109
|
-
-DGGML_BACKEND_DL=1
|
|
110
|
-
-DGGML_CPU=1
|
|
99
|
+
-DGGML_BACKEND_DL=1 # Enable dynamic loading for GPU backends
|
|
100
|
+
-DGGML_CPU=1 # CPU backend statically built into main libraries
|
|
111
101
|
)
|
|
112
102
|
target_compile_definitions(RNLlamaCpp PRIVATE
|
|
113
|
-
-DGGML_BACKEND_DL=1
|
|
114
|
-
-DGGML_CPU=1
|
|
103
|
+
-DGGML_BACKEND_DL=1 # Enable dynamic loading for GPU backends
|
|
104
|
+
-DGGML_CPU=1 # CPU backend statically built into main libraries
|
|
115
105
|
)
|
|
116
106
|
|
|
117
|
-
#
|
|
118
|
-
|
|
107
|
+
# DISABLE Vulkan on Android - causes crashes during auto-initialization on emulators
|
|
108
|
+
# Even with n_gpu_layers=0, llama.cpp tries to initialize all available backends
|
|
109
|
+
message(STATUS "Vulkan backend support DISABLED on Android to prevent emulator crashes")
|
|
110
|
+
|
|
111
|
+
# TODO: Enable Vulkan backend if available (currently disabled due to emulator crashes)
|
|
112
|
+
# Uncomment the lines below to test Vulkan support on real devices
|
|
113
|
+
# if(VULKAN_BACKEND_AVAILABLE)
|
|
114
|
+
# target_compile_definitions(common PRIVATE -DGGML_VULKAN=1)
|
|
115
|
+
# target_compile_definitions(RNLlamaCpp PRIVATE -DGGML_VULKAN=1)
|
|
116
|
+
# message(STATUS "Vulkan backend support enabled for dynamic loading")
|
|
117
|
+
# else()
|
|
118
|
+
# message(STATUS "Vulkan backend support disabled - library not available")
|
|
119
|
+
# endif()
|
|
120
|
+
|
|
121
|
+
# Enable OpenCL backend if available
|
|
122
|
+
if(OPENCL_BACKEND_AVAILABLE)
|
|
119
123
|
target_compile_definitions(common PRIVATE -DGGML_OPENCL=1)
|
|
120
124
|
target_compile_definitions(RNLlamaCpp PRIVATE -DGGML_OPENCL=1)
|
|
121
|
-
message(STATUS "
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
if(EXISTS ${JNI_LIBS_DIR}/${ANDROID_ABI}/libggml-vulkan.so)
|
|
125
|
-
target_compile_definitions(common PRIVATE -DGGML_VULKAN=1)
|
|
126
|
-
target_compile_definitions(RNLlamaCpp PRIVATE -DGGML_VULKAN=1)
|
|
127
|
-
message(STATUS "Enabling Vulkan support")
|
|
125
|
+
message(STATUS "OpenCL backend support enabled for dynamic loading")
|
|
126
|
+
else()
|
|
127
|
+
message(STATUS "OpenCL backend support disabled - library not available")
|
|
128
128
|
endif()
|
|
129
129
|
|
|
130
130
|
# Include directories
|
|
@@ -133,7 +133,7 @@ target_include_directories(common PRIVATE
|
|
|
133
133
|
${LLAMA_CPP_DIR}/ggml/include
|
|
134
134
|
${LLAMA_CPP_DIR}/include
|
|
135
135
|
${LLAMA_CPP_DIR}/common
|
|
136
|
-
${LLAMA_CPP_DIR}/common/minja
|
|
136
|
+
${LLAMA_CPP_DIR}/common/minja # Add this for chat-template.hpp
|
|
137
137
|
${LLAMA_CPP_DIR}/src
|
|
138
138
|
)
|
|
139
139
|
|
|
@@ -142,32 +142,44 @@ target_include_directories(RNLlamaCpp PRIVATE
|
|
|
142
142
|
${LLAMA_CPP_DIR}/ggml/include
|
|
143
143
|
${LLAMA_CPP_DIR}/include
|
|
144
144
|
${LLAMA_CPP_DIR}/common
|
|
145
|
-
${LLAMA_CPP_DIR}/common/minja
|
|
145
|
+
${LLAMA_CPP_DIR}/common/minja # Add this for chat-template.hpp
|
|
146
146
|
${LLAMA_CPP_DIR}/src
|
|
147
147
|
# Add the generated headers path
|
|
148
148
|
${MODULE_ROOT}/android/generated/jni
|
|
149
149
|
${MODULE_ROOT}/android/generated/jni/react/renderer/components/RNLlamaCppSpec
|
|
150
150
|
)
|
|
151
151
|
|
|
152
|
-
# Link libraries
|
|
152
|
+
# Link libraries with proper dependencies
|
|
153
153
|
target_link_libraries(
|
|
154
154
|
RNLlamaCpp
|
|
155
155
|
common
|
|
156
|
-
react_codegen_RNLlamaCppSpec
|
|
157
|
-
llama
|
|
158
|
-
ggml-base
|
|
159
|
-
ggml
|
|
160
|
-
ggml-cpu
|
|
161
|
-
${ADDITIONAL_LIBRARIES}
|
|
156
|
+
react_codegen_RNLlamaCppSpec # Link against the generated TurboModule code
|
|
157
|
+
llama # Link against the imported prebuilt core llama library
|
|
158
|
+
ggml-base # Link against the imported GGML base library
|
|
159
|
+
ggml # Link against the imported GGML library
|
|
160
|
+
ggml-cpu # Link against the imported GGML CPU library
|
|
162
161
|
jsi
|
|
163
162
|
reactnative
|
|
164
163
|
fbjni
|
|
165
164
|
android
|
|
166
165
|
log
|
|
167
|
-
dl
|
|
166
|
+
dl # Required for dynamic loading of backend libraries
|
|
168
167
|
)
|
|
169
168
|
|
|
170
|
-
#
|
|
169
|
+
# Add Vulkan support if available
|
|
170
|
+
if(Vulkan_FOUND)
|
|
171
|
+
target_link_libraries(RNLlamaCpp ${Vulkan_LIBRARIES})
|
|
172
|
+
target_include_directories(RNLlamaCpp PRIVATE ${Vulkan_INCLUDE_DIRS})
|
|
173
|
+
message(STATUS "Vulkan support enabled for dynamic GPU backend loading")
|
|
174
|
+
else()
|
|
175
|
+
# Even without system Vulkan, we can still support dynamic loading if Vulkan library is present at runtime
|
|
176
|
+
message(STATUS "System Vulkan not found, but dynamic Vulkan loading may still work at runtime")
|
|
177
|
+
endif()
|
|
178
|
+
|
|
179
|
+
# Add OpenCL support - OpenCL will be loaded dynamically at runtime
|
|
180
|
+
# No need to link against OpenCL here since we use dynamic loading
|
|
181
|
+
|
|
182
|
+
# Copy dependency libraries to build output directory so they get packaged into APK
|
|
171
183
|
add_custom_command(TARGET RNLlamaCpp POST_BUILD
|
|
172
184
|
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
|
173
185
|
${JNI_LIBS_DIR}/${ANDROID_ABI}/libllama.so
|
|
@@ -181,16 +193,31 @@ add_custom_command(TARGET RNLlamaCpp POST_BUILD
|
|
|
181
193
|
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
|
182
194
|
${JNI_LIBS_DIR}/${ANDROID_ABI}/libggml-cpu.so
|
|
183
195
|
$<TARGET_FILE_DIR:RNLlamaCpp>/libggml-cpu.so
|
|
184
|
-
COMMENT "Copying
|
|
196
|
+
COMMENT "Copying dependency libraries to build output directory"
|
|
185
197
|
)
|
|
186
198
|
|
|
187
|
-
#
|
|
199
|
+
# Also copy any optional GPU libraries if they exist
|
|
200
|
+
if(EXISTS ${JNI_LIBS_DIR}/${ANDROID_ABI}/libggml-vulkan.so)
|
|
201
|
+
# Don't copy Vulkan backend on Android - it crashes on emulators during auto-initialization
|
|
202
|
+
# Even with n_gpu_layers=0, llama.cpp tries to initialize all available backends
|
|
203
|
+
# and the Android emulator Vulkan driver is broken
|
|
204
|
+
message(STATUS "Skipping Vulkan backend copy to prevent emulator crashes")
|
|
205
|
+
|
|
206
|
+
# TODO: Uncomment the lines below to enable Vulkan library copying for testing on real devices
|
|
207
|
+
# add_custom_command(TARGET RNLlamaCpp POST_BUILD
|
|
208
|
+
# COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
|
209
|
+
# ${JNI_LIBS_DIR}/${ANDROID_ABI}/libggml-vulkan.so
|
|
210
|
+
# $<TARGET_FILE_DIR:RNLlamaCpp>/libggml-vulkan.so
|
|
211
|
+
# COMMENT "Copying Vulkan backend library to build output directory"
|
|
212
|
+
# )
|
|
213
|
+
endif()
|
|
214
|
+
|
|
188
215
|
if(EXISTS ${JNI_LIBS_DIR}/${ANDROID_ABI}/libggml-opencl.so)
|
|
189
216
|
add_custom_command(TARGET RNLlamaCpp POST_BUILD
|
|
190
217
|
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
|
191
218
|
${JNI_LIBS_DIR}/${ANDROID_ABI}/libggml-opencl.so
|
|
192
219
|
$<TARGET_FILE_DIR:RNLlamaCpp>/libggml-opencl.so
|
|
193
|
-
COMMENT "Copying OpenCL
|
|
220
|
+
COMMENT "Copying OpenCL library to build output directory"
|
|
194
221
|
)
|
|
195
222
|
endif()
|
|
196
223
|
|
|
@@ -199,20 +226,11 @@ if(EXISTS ${JNI_LIBS_DIR}/${ANDROID_ABI}/libOpenCL.so)
|
|
|
199
226
|
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
|
200
227
|
${JNI_LIBS_DIR}/${ANDROID_ABI}/libOpenCL.so
|
|
201
228
|
$<TARGET_FILE_DIR:RNLlamaCpp>/libOpenCL.so
|
|
202
|
-
COMMENT "Copying OpenCL
|
|
229
|
+
COMMENT "Copying OpenCL loader library to build output directory"
|
|
203
230
|
)
|
|
204
231
|
endif()
|
|
205
232
|
|
|
206
|
-
|
|
207
|
-
add_custom_command(TARGET RNLlamaCpp POST_BUILD
|
|
208
|
-
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
|
209
|
-
${JNI_LIBS_DIR}/${ANDROID_ABI}/libggml-vulkan.so
|
|
210
|
-
$<TARGET_FILE_DIR:RNLlamaCpp>/libggml-vulkan.so
|
|
211
|
-
COMMENT "Copying Vulkan backend"
|
|
212
|
-
)
|
|
213
|
-
endif()
|
|
214
|
-
|
|
215
|
-
# Expose headers to consuming targets
|
|
233
|
+
# Expose our headers to consuming targets (for autolinking)
|
|
216
234
|
target_include_directories(RNLlamaCpp INTERFACE
|
|
217
235
|
${CPP_DIR}
|
|
218
236
|
${LLAMA_CPP_DIR}/ggml/include
|
|
@@ -221,4 +239,3 @@ target_include_directories(RNLlamaCpp INTERFACE
|
|
|
221
239
|
${LLAMA_CPP_DIR}/common/minja
|
|
222
240
|
${LLAMA_CPP_DIR}/src
|
|
223
241
|
)
|
|
224
|
-
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/cpp/LlamaCppModel.cpp
CHANGED
|
@@ -35,8 +35,8 @@
|
|
|
35
35
|
|
|
36
36
|
namespace facebook::react {
|
|
37
37
|
|
|
38
|
-
LlamaCppModel::LlamaCppModel(rn_llama_context* rn_ctx)
|
|
39
|
-
: rn_ctx_(rn_ctx), should_stop_completion_(false), is_predicting_(false) {
|
|
38
|
+
LlamaCppModel::LlamaCppModel(rn_llama_context* rn_ctx, std::shared_ptr<CallInvoker> jsInvoker)
|
|
39
|
+
: rn_ctx_(rn_ctx), should_stop_completion_(false), is_predicting_(false), jsInvoker_(jsInvoker) {
|
|
40
40
|
initHelpers();
|
|
41
41
|
}
|
|
42
42
|
|
|
@@ -435,11 +435,18 @@ CompletionResult LlamaCppModel::completion(const CompletionOptions& options, std
|
|
|
435
435
|
rn_ctx_->params.n_predict = options.n_predict;
|
|
436
436
|
|
|
437
437
|
// Check for a partial callback
|
|
438
|
-
auto callback_adapter = [&partialCallback, runtime](const std::string& token, bool is_done) -> bool {
|
|
438
|
+
auto callback_adapter = [&partialCallback, runtime, this](const std::string& token, bool is_done) -> bool {
|
|
439
|
+
// Check for stop condition first
|
|
440
|
+
if (should_stop_completion_) {
|
|
441
|
+
return false; // Signal to stop completion
|
|
442
|
+
}
|
|
443
|
+
|
|
439
444
|
if (partialCallback && runtime && !is_done) {
|
|
440
445
|
partialCallback(*runtime, token.c_str());
|
|
441
446
|
}
|
|
442
|
-
|
|
447
|
+
|
|
448
|
+
// Return true to continue, false to stop
|
|
449
|
+
return !should_stop_completion_;
|
|
443
450
|
};
|
|
444
451
|
|
|
445
452
|
// Run the completion based on whether we have messages or prompt
|
|
@@ -546,7 +553,7 @@ jsi::Value LlamaCppModel::jsonToJsi(jsi::Runtime& rt, const json& j) {
|
|
|
546
553
|
return jsi::Value::undefined();
|
|
547
554
|
}
|
|
548
555
|
|
|
549
|
-
// JSI method for completions
|
|
556
|
+
// JSI method for completions (synchronous - kept for compatibility)
|
|
550
557
|
jsi::Value LlamaCppModel::completionJsi(jsi::Runtime& rt, const jsi::Value* args, size_t count) {
|
|
551
558
|
if (count < 1 || !args[0].isObject()) {
|
|
552
559
|
throw jsi::JSError(rt, "completion requires an options object");
|
|
@@ -581,6 +588,116 @@ jsi::Value LlamaCppModel::completionJsi(jsi::Runtime& rt, const jsi::Value* args
|
|
|
581
588
|
}
|
|
582
589
|
}
|
|
583
590
|
|
|
591
|
+
// JSI method for async completions (recommended approach)
|
|
592
|
+
jsi::Value LlamaCppModel::completionAsyncJsi(jsi::Runtime& rt, const jsi::Value* args, size_t count) {
|
|
593
|
+
if (count < 1 || !args[0].isObject()) {
|
|
594
|
+
throw jsi::JSError(rt, "completion requires an options object");
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
if (!jsInvoker_) {
|
|
598
|
+
// Fallback to synchronous if no CallInvoker available
|
|
599
|
+
return completionJsi(rt, args, count);
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
// Parse options and callback on the current thread
|
|
603
|
+
CompletionOptions options;
|
|
604
|
+
std::shared_ptr<jsi::Function> callbackFn = nullptr;
|
|
605
|
+
|
|
606
|
+
try {
|
|
607
|
+
options = parseCompletionOptions(rt, args[0].getObject(rt));
|
|
608
|
+
|
|
609
|
+
if (count > 1 && args[1].isObject() && args[1].getObject(rt).isFunction(rt)) {
|
|
610
|
+
callbackFn = std::make_shared<jsi::Function>(args[1].getObject(rt).getFunction(rt));
|
|
611
|
+
options.stream = true;
|
|
612
|
+
}
|
|
613
|
+
} catch (const std::exception& e) {
|
|
614
|
+
throw jsi::JSError(rt, e.what());
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
// Create Promise constructor
|
|
618
|
+
auto Promise = rt.global().getPropertyAsFunction(rt, "Promise");
|
|
619
|
+
|
|
620
|
+
auto executor = jsi::Function::createFromHostFunction(
|
|
621
|
+
rt,
|
|
622
|
+
jsi::PropNameID::forAscii(rt, "executor"),
|
|
623
|
+
2,
|
|
624
|
+
[this, options, callbackFn](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* args, size_t count) -> jsi::Value {
|
|
625
|
+
|
|
626
|
+
auto resolve = std::make_shared<jsi::Function>(args[0].asObject(runtime).asFunction(runtime));
|
|
627
|
+
auto reject = std::make_shared<jsi::Function>(args[1].asObject(runtime).asFunction(runtime));
|
|
628
|
+
|
|
629
|
+
// Create shared references to runtime and invoker for thread safety
|
|
630
|
+
auto runtimePtr = &runtime;
|
|
631
|
+
auto invoker = jsInvoker_;
|
|
632
|
+
auto selfPtr = shared_from_this(); // This requires LlamaCppModel to inherit from std::enable_shared_from_this
|
|
633
|
+
|
|
634
|
+
// Launch background thread for completion
|
|
635
|
+
std::thread([selfPtr, options, callbackFn, resolve, reject, runtimePtr, invoker]() {
|
|
636
|
+
try {
|
|
637
|
+
// Create callback that schedules token updates on JS thread
|
|
638
|
+
std::function<void(jsi::Runtime&, const char*)> partialCallback = nullptr;
|
|
639
|
+
|
|
640
|
+
if (callbackFn && invoker) {
|
|
641
|
+
partialCallback = [callbackFn, invoker, runtimePtr](jsi::Runtime& rt, const char* token) {
|
|
642
|
+
std::string tokenCopy(token);
|
|
643
|
+
invoker->invokeAsync([callbackFn, tokenCopy, runtimePtr]() {
|
|
644
|
+
try {
|
|
645
|
+
jsi::Object data(*runtimePtr);
|
|
646
|
+
data.setProperty(*runtimePtr, "token", jsi::String::createFromUtf8(*runtimePtr, tokenCopy));
|
|
647
|
+
callbackFn->call(*runtimePtr, data);
|
|
648
|
+
} catch (...) {
|
|
649
|
+
// Ignore callback errors
|
|
650
|
+
}
|
|
651
|
+
});
|
|
652
|
+
};
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
// Run completion
|
|
656
|
+
CompletionResult result = selfPtr->completion(options, partialCallback, runtimePtr);
|
|
657
|
+
|
|
658
|
+
// Schedule success callback on JS thread
|
|
659
|
+
invoker->invokeAsync([selfPtr, resolve, result, runtimePtr]() {
|
|
660
|
+
try {
|
|
661
|
+
jsi::Object jsResult = selfPtr->completionResultToJsi(*runtimePtr, result);
|
|
662
|
+
resolve->call(*runtimePtr, jsResult);
|
|
663
|
+
} catch (const std::exception& e) {
|
|
664
|
+
// If conversion fails, create a simple error response
|
|
665
|
+
jsi::Object errorObj(*runtimePtr);
|
|
666
|
+
errorObj.setProperty(*runtimePtr, "error", jsi::String::createFromUtf8(*runtimePtr, e.what()));
|
|
667
|
+
resolve->call(*runtimePtr, errorObj);
|
|
668
|
+
}
|
|
669
|
+
});
|
|
670
|
+
|
|
671
|
+
} catch (const std::exception& e) {
|
|
672
|
+
// Schedule error callback on JS thread
|
|
673
|
+
std::string errorMsg(e.what());
|
|
674
|
+
invoker->invokeAsync([reject, errorMsg, runtimePtr]() {
|
|
675
|
+
try {
|
|
676
|
+
reject->call(*runtimePtr, jsi::String::createFromUtf8(*runtimePtr, errorMsg));
|
|
677
|
+
} catch (...) {
|
|
678
|
+
// Ignore rejection errors
|
|
679
|
+
}
|
|
680
|
+
});
|
|
681
|
+
}
|
|
682
|
+
}).detach();
|
|
683
|
+
|
|
684
|
+
return jsi::Value::undefined();
|
|
685
|
+
}
|
|
686
|
+
);
|
|
687
|
+
|
|
688
|
+
return Promise.callAsConstructor(rt, std::move(executor));
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
// JSI method for stopping completion
|
|
692
|
+
jsi::Value LlamaCppModel::stopCompletionJsi(jsi::Runtime& rt, const jsi::Value* args, size_t count) {
|
|
693
|
+
try {
|
|
694
|
+
setShouldStopCompletion(true);
|
|
695
|
+
return jsi::Value(true);
|
|
696
|
+
} catch (const std::exception& e) {
|
|
697
|
+
throw jsi::JSError(rt, e.what());
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
|
|
584
701
|
jsi::Value LlamaCppModel::tokenizeJsi(jsi::Runtime& rt, const jsi::Value* args, size_t count) {
|
|
585
702
|
if (count < 1 || !args[0].isObject()) {
|
|
586
703
|
throw jsi::JSError(rt, "tokenize requires an options object with 'content' field");
|
|
@@ -930,12 +1047,36 @@ jsi::Value LlamaCppModel::get(jsi::Runtime& rt, const jsi::PropNameID& name) {
|
|
|
930
1047
|
});
|
|
931
1048
|
}
|
|
932
1049
|
else if (nameStr == "completion") {
|
|
1050
|
+
// Use async completion as the default to provide better UX
|
|
1051
|
+
if (jsInvoker_) {
|
|
1052
|
+
return jsi::Function::createFromHostFunction(
|
|
1053
|
+
rt, name, 2,
|
|
1054
|
+
[this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* args, size_t count) {
|
|
1055
|
+
return this->completionAsyncJsi(runtime, args, count);
|
|
1056
|
+
});
|
|
1057
|
+
} else {
|
|
1058
|
+
// Fallback to sync completion if no CallInvoker
|
|
1059
|
+
return jsi::Function::createFromHostFunction(
|
|
1060
|
+
rt, name, 2,
|
|
1061
|
+
[this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* args, size_t count) {
|
|
1062
|
+
return this->completionJsi(runtime, args, count);
|
|
1063
|
+
});
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
else if (nameStr == "completionSync") {
|
|
933
1067
|
return jsi::Function::createFromHostFunction(
|
|
934
1068
|
rt, name, 2,
|
|
935
1069
|
[this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* args, size_t count) {
|
|
936
1070
|
return this->completionJsi(runtime, args, count);
|
|
937
1071
|
});
|
|
938
1072
|
}
|
|
1073
|
+
else if (nameStr == "stopCompletion") {
|
|
1074
|
+
return jsi::Function::createFromHostFunction(
|
|
1075
|
+
rt, name, 0,
|
|
1076
|
+
[this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* args, size_t count) {
|
|
1077
|
+
return this->stopCompletionJsi(runtime, args, count);
|
|
1078
|
+
});
|
|
1079
|
+
}
|
|
939
1080
|
else if (nameStr == "embedding") {
|
|
940
1081
|
return jsi::Function::createFromHostFunction(
|
|
941
1082
|
rt, name, 1,
|
|
@@ -973,6 +1114,8 @@ std::vector<jsi::PropNameID> LlamaCppModel::getPropertyNames(jsi::Runtime& rt) {
|
|
|
973
1114
|
result.push_back(jsi::PropNameID::forAscii(rt, "tokenize"));
|
|
974
1115
|
result.push_back(jsi::PropNameID::forAscii(rt, "detokenize"));
|
|
975
1116
|
result.push_back(jsi::PropNameID::forAscii(rt, "completion"));
|
|
1117
|
+
result.push_back(jsi::PropNameID::forAscii(rt, "completionSync"));
|
|
1118
|
+
result.push_back(jsi::PropNameID::forAscii(rt, "stopCompletion"));
|
|
976
1119
|
result.push_back(jsi::PropNameID::forAscii(rt, "embedding"));
|
|
977
1120
|
result.push_back(jsi::PropNameID::forAscii(rt, "release"));
|
|
978
1121
|
result.push_back(jsi::PropNameID::forAscii(rt, "n_vocab"));
|
package/cpp/LlamaCppModel.h
CHANGED
|
@@ -8,6 +8,9 @@
|
|
|
8
8
|
#include <unordered_map>
|
|
9
9
|
#include <functional>
|
|
10
10
|
|
|
11
|
+
// Add ReactCommon includes for proper async handling
|
|
12
|
+
#include <ReactCommon/CallInvoker.h>
|
|
13
|
+
|
|
11
14
|
// Include all necessary common headers from llama.cpp
|
|
12
15
|
#include "common.h"
|
|
13
16
|
#include "sampling.h"
|
|
@@ -70,13 +73,14 @@ struct ToolCall {
|
|
|
70
73
|
* - Uses common_token_to_piece for token->text conversion
|
|
71
74
|
* - Leverages the llama.cpp chat template system
|
|
72
75
|
*/
|
|
73
|
-
class LlamaCppModel : public jsi::HostObject {
|
|
76
|
+
class LlamaCppModel : public jsi::HostObject, public std::enable_shared_from_this<LlamaCppModel> {
|
|
74
77
|
public:
|
|
75
78
|
/**
|
|
76
79
|
* Constructor
|
|
77
80
|
* @param rn_ctx A pointer to an initialized rn_llama_context
|
|
81
|
+
* @param jsInvoker CallInvoker for async operations (optional, for async completion)
|
|
78
82
|
*/
|
|
79
|
-
LlamaCppModel(rn_llama_context* rn_ctx);
|
|
83
|
+
LlamaCppModel(rn_llama_context* rn_ctx, std::shared_ptr<CallInvoker> jsInvoker = nullptr);
|
|
80
84
|
virtual ~LlamaCppModel();
|
|
81
85
|
|
|
82
86
|
/**
|
|
@@ -124,6 +128,8 @@ private:
|
|
|
124
128
|
* JSI method implementations
|
|
125
129
|
*/
|
|
126
130
|
jsi::Value completionJsi(jsi::Runtime& rt, const jsi::Value* args, size_t count);
|
|
131
|
+
jsi::Value completionAsyncJsi(jsi::Runtime& rt, const jsi::Value* args, size_t count);
|
|
132
|
+
jsi::Value stopCompletionJsi(jsi::Runtime& rt, const jsi::Value* args, size_t count);
|
|
127
133
|
jsi::Value tokenizeJsi(jsi::Runtime& rt, const jsi::Value* args, size_t count);
|
|
128
134
|
jsi::Value detokenizeJsi(jsi::Runtime& rt, const jsi::Value* args, size_t count);
|
|
129
135
|
jsi::Value embeddingJsi(jsi::Runtime& rt, const jsi::Value* args, size_t count);
|
|
@@ -157,6 +163,9 @@ private:
|
|
|
157
163
|
// Completion state
|
|
158
164
|
bool should_stop_completion_;
|
|
159
165
|
bool is_predicting_;
|
|
166
|
+
|
|
167
|
+
// Add CallInvoker for async operations
|
|
168
|
+
std::shared_ptr<CallInvoker> jsInvoker_;
|
|
160
169
|
};
|
|
161
170
|
|
|
162
171
|
} // namespace facebook::react
|
package/cpp/PureCppImpl.cpp
CHANGED
|
@@ -35,7 +35,7 @@ std::shared_ptr<TurboModule> PureCppImpl::create(std::shared_ptr<CallInvoker> js
|
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
PureCppImpl::PureCppImpl(std::shared_ptr<CallInvoker> jsInvoker)
|
|
38
|
-
: NativeRNLlamaCppCxxSpec(
|
|
38
|
+
: NativeRNLlamaCppCxxSpec(jsInvoker), jsInvoker_(jsInvoker) {
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
double PureCppImpl::multiply(jsi::Runtime& rt, double a, double b) {
|
|
@@ -325,8 +325,8 @@ jsi::Value PureCppImpl::initLlama(jsi::Runtime &runtime, jsi::Object options) {
|
|
|
325
325
|
}
|
|
326
326
|
|
|
327
327
|
jsi::Object PureCppImpl::createModelObject(jsi::Runtime& runtime, rn_llama_context* rn_ctx) {
|
|
328
|
-
// Create a shared_ptr to a new LlamaCppModel instance
|
|
329
|
-
auto llamaModel = std::make_shared<LlamaCppModel>(rn_ctx);
|
|
328
|
+
// Create a shared_ptr to a new LlamaCppModel instance with CallInvoker
|
|
329
|
+
auto llamaModel = std::make_shared<LlamaCppModel>(rn_ctx, jsInvoker_);
|
|
330
330
|
|
|
331
331
|
// Create a host object from the LlamaCppModel instance
|
|
332
332
|
return jsi::Object::createFromHostObject(runtime, std::move(llamaModel));
|
package/cpp/PureCppImpl.h
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["TurboModuleRegistry","LlamaCppRn","getEnforcing","initLlama","params","loadLlamaModelInfo","modelPath"],"sourceRoot":"../../src","sources":["NativeRNLlamaCpp.ts"],"mappings":";;AACA,SAASA,mBAAmB,QAAQ,cAAc;;AAElD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAiKA;;
|
|
1
|
+
{"version":3,"names":["TurboModuleRegistry","LlamaCppRn","getEnforcing","initLlama","params","loadLlamaModelInfo","modelPath"],"sourceRoot":"../../src","sources":["NativeRNLlamaCpp.ts"],"mappings":";;AACA,SAASA,mBAAmB,QAAQ,cAAc;;AAElD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAiKA;;AA+EA,MAAMC,UAAU,GAAGD,mBAAmB,CAACE,YAAY,CAAO,YAAY,CAAC;AACvE;AACA;AACA;;AAGA;AACA;AACA;AACA,OAAO,SAASC,SAASA,CAACC,MAAwB,EAAmD;EACnG,OAAOH,UAAU,CAACE,SAAS,CAACC,MAAM,CAAC;AACrC;;AAEA;AACA;AACA;AACA,OAAO,SAASC,kBAAkBA,CAChCC,SAAiB,EAWhB;EACD,OAAOL,UAAU,CAACI,kBAAkB,CAACC,SAAS,CAAC;AACjD;AAEA,eAAeL,UAAU","ignoreList":[]}
|
|
@@ -152,6 +152,9 @@ export interface LlamaContextMethods {
|
|
|
152
152
|
completion(params: LlamaCompletionParams, partialCallback?: (data: {
|
|
153
153
|
token: string;
|
|
154
154
|
}) => void): Promise<LlamaCompletionResult>;
|
|
155
|
+
completionSync(params: LlamaCompletionParams, partialCallback?: (data: {
|
|
156
|
+
token: string;
|
|
157
|
+
}) => void): LlamaCompletionResult;
|
|
155
158
|
tokenize(options: {
|
|
156
159
|
content: string;
|
|
157
160
|
add_special?: boolean;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NativeRNLlamaCpp.d.ts","sourceRoot":"","sources":["../../../src/NativeRNLlamaCpp.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAGhD;;;;;;;GAOG;AAEH,MAAM,WAAW,gBAAgB;CAGhC;AAED,MAAM,WAAW,gBAAgB;IAE/B,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,QA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W,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,OAAO,CAAC;IACtB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB,CAAC,CAED;AAED,eAAe,UAAU,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@novastera-oss/llamarn",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "An attempt at a pure cpp turbo module library",
|
|
5
5
|
"source": "./src/index.tsx",
|
|
6
6
|
"main": "./lib/module/index.js",
|
|
@@ -66,6 +66,7 @@
|
|
|
66
66
|
"build-android-external": "bash scripts/build_android_external.sh",
|
|
67
67
|
"build-android-clean": "bash scripts/build_android_external.sh --clean-prebuilt",
|
|
68
68
|
"build-android-macos": "npm run clean-android && npm run clean-prebuilt && bash scripts/build_android_gpu_backend.sh --no-vulkan --no-opencl --clean && bash scripts/build_android_external.sh --no-vulkan --no-opencl --clean"
|
|
69
|
+
|
|
69
70
|
},
|
|
70
71
|
"keywords": [
|
|
71
72
|
"react-native",
|
package/src/NativeRNLlamaCpp.ts
CHANGED
|
@@ -195,6 +195,7 @@ export interface EmbeddingResponse {
|
|
|
195
195
|
|
|
196
196
|
export interface LlamaContextMethods {
|
|
197
197
|
completion(params: LlamaCompletionParams, partialCallback?: (data: {token: string}) => void): Promise<LlamaCompletionResult>;
|
|
198
|
+
completionSync(params: LlamaCompletionParams, partialCallback?: (data: {token: string}) => void): LlamaCompletionResult;
|
|
198
199
|
|
|
199
200
|
// Updated tokenize method to match server.cpp interface
|
|
200
201
|
tokenize(options: {
|