@novastera-oss/llamarn 0.1.4-beta.2 → 0.1.5-beta.3

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.
@@ -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 - this is where build_android_external.sh puts the libraries
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 (for headers)
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
- # Check if libraries exist (they should be built by build_android_external.sh)
22
- if(NOT EXISTS "${JNI_LIBS_DIR}/${ANDROID_ABI}/libllama.so")
23
- message(FATAL_ERROR "Prebuilt libraries not found. Please run 'scripts/build_android_external.sh' first to build the native libraries.")
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
- # Import all prebuilt libraries as IMPORTED targets
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
- # Collect additional libraries that exist
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
- # Set compile definitions - always enable dynamic loading and CPU backend
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
- # Add GPU backend definitions based on what libraries exist
118
- if(EXISTS ${JNI_LIBS_DIR}/${ANDROID_ABI}/libggml-opencl.so)
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 "Enabling OpenCL support")
122
- endif()
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 - link against all available 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
- # Copy all libraries to build output directory
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 main libraries"
196
+ COMMENT "Copying dependency libraries to build output directory"
185
197
  )
186
198
 
187
- # Copy any additional GPU libraries that exist
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 backend"
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 ICD loader"
229
+ COMMENT "Copying OpenCL loader library to build output directory"
203
230
  )
204
231
  endif()
205
232
 
206
- if(EXISTS ${JNI_LIBS_DIR}/${ANDROID_ABI}/libggml-vulkan.so)
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
-
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@novastera-oss/llamarn",
3
- "version": "0.1.4-beta.2",
3
+ "version": "0.1.5-beta.3",
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",