@novastera-oss/llamarn 0.1.3-beta.5 → 0.1.3-beta.7

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 assumes CMakeLists.txt is in android/
10
+ # Define the path to jniLibs - this is where build_android_external.sh puts the libraries
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 (for headers)
14
14
  set(LLAMA_CPP_DIR "${CPP_DIR}/llama.cpp")
15
15
 
16
16
  # Make sure the llama.cpp submodule exists
@@ -18,40 +18,69 @@ 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
- # 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")
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.")
27
24
  endif()
28
25
 
29
- # Add the prebuilt libraries as IMPORTED with IMPORTED_NO_SONAME to avoid absolute path embedding
26
+ # Import all prebuilt libraries as IMPORTED targets
30
27
  add_library(llama SHARED IMPORTED)
31
28
  set_target_properties(llama PROPERTIES
32
29
  IMPORTED_LOCATION ${JNI_LIBS_DIR}/${ANDROID_ABI}/libllama.so
33
30
  IMPORTED_NO_SONAME TRUE)
34
31
 
35
- add_library(ggml-base SHARED IMPORTED)
36
- set_target_properties(ggml-base PROPERTIES
37
- IMPORTED_LOCATION ${JNI_LIBS_DIR}/${ANDROID_ABI}/libggml-base.so
38
- IMPORTED_NO_SONAME TRUE)
39
-
40
32
  add_library(ggml SHARED IMPORTED)
41
33
  set_target_properties(ggml PROPERTIES
42
34
  IMPORTED_LOCATION ${JNI_LIBS_DIR}/${ANDROID_ABI}/libggml.so
43
35
  IMPORTED_NO_SONAME TRUE)
44
36
 
37
+ add_library(ggml-base SHARED IMPORTED)
38
+ set_target_properties(ggml-base PROPERTIES
39
+ IMPORTED_LOCATION ${JNI_LIBS_DIR}/${ANDROID_ABI}/libggml-base.so
40
+ IMPORTED_NO_SONAME TRUE)
41
+
45
42
  add_library(ggml-cpu SHARED IMPORTED)
46
43
  set_target_properties(ggml-cpu PROPERTIES
47
44
  IMPORTED_LOCATION ${JNI_LIBS_DIR}/${ANDROID_ABI}/libggml-cpu.so
48
45
  IMPORTED_NO_SONAME TRUE)
49
46
 
50
- # Create a minimal common library with only essential files that don't require missing GGML symbols
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
51
81
  add_library(
52
82
  common
53
83
  STATIC
54
- # Add back essential files now that we have prebuilt GGML libraries
55
84
  ${CPP_DIR}/llama.cpp/common/build-info.cpp
56
85
  ${CPP_DIR}/llama.cpp/common/log.cpp
57
86
  ${CPP_DIR}/llama.cpp/common/common.cpp
@@ -60,6 +89,7 @@ add_library(
60
89
  ${CPP_DIR}/llama.cpp/common/json-schema-to-grammar.cpp
61
90
  )
62
91
 
92
+ # Create our main React Native module
63
93
  add_library(
64
94
  RNLlamaCpp
65
95
  SHARED
@@ -70,61 +100,31 @@ add_library(
70
100
  ${CPP_DIR}/rn-completion.cpp
71
101
  )
72
102
 
73
- # Suppress unused function warnings for llama.cpp code
103
+ # Suppress unused function warnings
74
104
  target_compile_options(common PRIVATE -Wno-unused-function)
75
105
  target_compile_options(RNLlamaCpp PRIVATE -Wno-unused-function)
76
106
 
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
107
+ # Set compile definitions - always enable dynamic loading and CPU backend
98
108
  target_compile_definitions(common PRIVATE
99
- -DGGML_BACKEND_DL=1 # Enable dynamic loading for GPU backends
100
- -DGGML_CPU=1 # CPU backend statically built into main libraries
109
+ -DGGML_BACKEND_DL=1
110
+ -DGGML_CPU=1
101
111
  )
102
112
  target_compile_definitions(RNLlamaCpp PRIVATE
103
- -DGGML_BACKEND_DL=1 # Enable dynamic loading for GPU backends
104
- -DGGML_CPU=1 # CPU backend statically built into main libraries
113
+ -DGGML_BACKEND_DL=1
114
+ -DGGML_CPU=1
105
115
  )
106
116
 
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)
117
+ # Add GPU backend definitions based on what libraries exist
118
+ if(EXISTS ${JNI_LIBS_DIR}/${ANDROID_ABI}/libggml-opencl.so)
123
119
  target_compile_definitions(common PRIVATE -DGGML_OPENCL=1)
124
120
  target_compile_definitions(RNLlamaCpp PRIVATE -DGGML_OPENCL=1)
125
- message(STATUS "OpenCL backend support enabled for dynamic loading")
126
- else()
127
- message(STATUS "OpenCL backend support disabled - library not available")
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")
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 # Add this for chat-template.hpp
136
+ ${LLAMA_CPP_DIR}/common/minja
137
137
  ${LLAMA_CPP_DIR}/src
138
138
  )
139
139
 
@@ -142,44 +142,32 @@ 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 # Add this for chat-template.hpp
145
+ ${LLAMA_CPP_DIR}/common/minja
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 with proper dependencies
152
+ # Link libraries - link against all available libraries
153
153
  target_link_libraries(
154
154
  RNLlamaCpp
155
155
  common
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
156
+ react_codegen_RNLlamaCppSpec
157
+ llama
158
+ ggml-base
159
+ ggml
160
+ ggml-cpu
161
+ ${ADDITIONAL_LIBRARIES}
161
162
  jsi
162
163
  reactnative
163
164
  fbjni
164
165
  android
165
166
  log
166
- dl # Required for dynamic loading of backend libraries
167
+ dl
167
168
  )
168
169
 
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
170
+ # Copy all libraries to build output directory
183
171
  add_custom_command(TARGET RNLlamaCpp POST_BUILD
184
172
  COMMAND ${CMAKE_COMMAND} -E copy_if_different
185
173
  ${JNI_LIBS_DIR}/${ANDROID_ABI}/libllama.so
@@ -193,31 +181,16 @@ add_custom_command(TARGET RNLlamaCpp POST_BUILD
193
181
  COMMAND ${CMAKE_COMMAND} -E copy_if_different
194
182
  ${JNI_LIBS_DIR}/${ANDROID_ABI}/libggml-cpu.so
195
183
  $<TARGET_FILE_DIR:RNLlamaCpp>/libggml-cpu.so
196
- COMMENT "Copying dependency libraries to build output directory"
184
+ COMMENT "Copying main libraries"
197
185
  )
198
186
 
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
-
187
+ # Copy any additional GPU libraries that exist
215
188
  if(EXISTS ${JNI_LIBS_DIR}/${ANDROID_ABI}/libggml-opencl.so)
216
189
  add_custom_command(TARGET RNLlamaCpp POST_BUILD
217
190
  COMMAND ${CMAKE_COMMAND} -E copy_if_different
218
191
  ${JNI_LIBS_DIR}/${ANDROID_ABI}/libggml-opencl.so
219
192
  $<TARGET_FILE_DIR:RNLlamaCpp>/libggml-opencl.so
220
- COMMENT "Copying OpenCL library to build output directory"
193
+ COMMENT "Copying OpenCL backend"
221
194
  )
222
195
  endif()
223
196
 
@@ -226,11 +199,20 @@ if(EXISTS ${JNI_LIBS_DIR}/${ANDROID_ABI}/libOpenCL.so)
226
199
  COMMAND ${CMAKE_COMMAND} -E copy_if_different
227
200
  ${JNI_LIBS_DIR}/${ANDROID_ABI}/libOpenCL.so
228
201
  $<TARGET_FILE_DIR:RNLlamaCpp>/libOpenCL.so
229
- COMMENT "Copying OpenCL loader library to build output directory"
202
+ COMMENT "Copying OpenCL ICD loader"
230
203
  )
231
204
  endif()
232
205
 
233
- # Expose our headers to consuming targets (for autolinking)
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
234
216
  target_include_directories(RNLlamaCpp INTERFACE
235
217
  ${CPP_DIR}
236
218
  ${LLAMA_CPP_DIR}/ggml/include
@@ -239,3 +221,4 @@ target_include_directories(RNLlamaCpp INTERFACE
239
221
  ${LLAMA_CPP_DIR}/common/minja
240
222
  ${LLAMA_CPP_DIR}/src
241
223
  )
224
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@novastera-oss/llamarn",
3
- "version": "0.1.3-beta.5",
3
+ "version": "0.1.3-beta.7",
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",