llama-cpp-capacitor 0.0.9 → 0.0.12
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/src/main/jni.cpp +198 -6
- package/package.json +1 -1
package/android/src/main/jni.cpp
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
#include <android/log.h>
|
|
4
4
|
#include <cstring>
|
|
5
5
|
#include <memory>
|
|
6
|
+
#include <fstream> // Added for file existence and size checks
|
|
6
7
|
|
|
7
8
|
// Add missing symbol
|
|
8
9
|
namespace rnllama {
|
|
@@ -139,13 +140,73 @@ Java_ai_annadata_plugin_capacitor_LlamaCpp_initContextNative(
|
|
|
139
140
|
|
|
140
141
|
try {
|
|
141
142
|
std::string model_path_str = jstring_to_string(env, model_path);
|
|
143
|
+
LOGI("Attempting to load model from path: %s", model_path_str.c_str());
|
|
144
|
+
|
|
145
|
+
// List all possible paths we should check
|
|
146
|
+
std::vector<std::string> paths_to_check = {
|
|
147
|
+
model_path_str,
|
|
148
|
+
"/data/data/ai.annadata.app/files/" + model_path_str,
|
|
149
|
+
"/data/data/ai.annadata.app/files/Documents/" + model_path_str,
|
|
150
|
+
"/storage/emulated/0/Android/data/ai.annadata.app/files/" + model_path_str,
|
|
151
|
+
"/storage/emulated/0/Android/data/ai.annadata.app/files/Documents/" + model_path_str,
|
|
152
|
+
"/storage/emulated/0/Documents/" + model_path_str
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
// Check each path and log what we find
|
|
156
|
+
std::string full_model_path;
|
|
157
|
+
bool file_found = false;
|
|
142
158
|
|
|
159
|
+
for (const auto& path : paths_to_check) {
|
|
160
|
+
LOGI("Checking path: %s", path.c_str());
|
|
161
|
+
std::ifstream file_check(path);
|
|
162
|
+
if (file_check.good()) {
|
|
163
|
+
file_check.seekg(0, std::ios::end);
|
|
164
|
+
std::streamsize file_size = file_check.tellg();
|
|
165
|
+
file_check.close();
|
|
166
|
+
LOGI("Found file at: %s, size: %ld bytes", path.c_str(), file_size);
|
|
167
|
+
|
|
168
|
+
// Validate file size
|
|
169
|
+
if (file_size < 1024 * 1024) { // Less than 1MB
|
|
170
|
+
LOGE("Model file is too small, likely corrupted: %s", path.c_str());
|
|
171
|
+
continue; // Try next path
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// Check if it's a valid GGUF file by reading the magic number
|
|
175
|
+
std::ifstream magic_file(path, std::ios::binary);
|
|
176
|
+
if (magic_file.good()) {
|
|
177
|
+
char magic[4];
|
|
178
|
+
if (magic_file.read(magic, 4)) {
|
|
179
|
+
if (magic[0] == 'G' && magic[1] == 'G' && magic[2] == 'U' && magic[3] == 'F') {
|
|
180
|
+
LOGI("Valid GGUF file detected at: %s", path.c_str());
|
|
181
|
+
full_model_path = path;
|
|
182
|
+
file_found = true;
|
|
183
|
+
break;
|
|
184
|
+
} else {
|
|
185
|
+
LOGI("File does not appear to be a GGUF file (magic: %c%c%c%c) at: %s",
|
|
186
|
+
magic[0], magic[1], magic[2], magic[3], path.c_str());
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
magic_file.close();
|
|
190
|
+
}
|
|
191
|
+
} else {
|
|
192
|
+
LOGI("File not found at: %s", path.c_str());
|
|
193
|
+
}
|
|
194
|
+
file_check.close();
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (!file_found) {
|
|
198
|
+
LOGE("Model file not found in any of the checked paths");
|
|
199
|
+
throw_java_exception(env, "java/lang/RuntimeException", "Model file not found in any expected location");
|
|
200
|
+
return -1;
|
|
201
|
+
}
|
|
202
|
+
|
|
143
203
|
// Create new context
|
|
144
204
|
auto context = std::make_unique<rnllama::llama_rn_context>();
|
|
205
|
+
LOGI("Created llama_rn_context");
|
|
145
206
|
|
|
146
207
|
// Initialize common parameters
|
|
147
208
|
common_params cparams;
|
|
148
|
-
cparams.model.path =
|
|
209
|
+
cparams.model.path = full_model_path;
|
|
149
210
|
cparams.n_ctx = 2048;
|
|
150
211
|
cparams.n_batch = 512;
|
|
151
212
|
cparams.n_gpu_layers = 0;
|
|
@@ -154,18 +215,149 @@ Java_ai_annadata_plugin_capacitor_LlamaCpp_initContextNative(
|
|
|
154
215
|
cparams.use_mmap = true;
|
|
155
216
|
cparams.use_mlock = false;
|
|
156
217
|
cparams.numa = LM_GGML_NUMA_STRATEGY_DISABLED;
|
|
218
|
+
cparams.ctx_shift = false;
|
|
219
|
+
cparams.chat_template = "";
|
|
220
|
+
cparams.embedding = false;
|
|
221
|
+
cparams.cont_batching = false;
|
|
222
|
+
cparams.parallel = false;
|
|
223
|
+
cparams.grammar = "";
|
|
224
|
+
cparams.grammar_penalty.clear();
|
|
225
|
+
cparams.antiprompt.clear();
|
|
226
|
+
cparams.lora_adapter.clear();
|
|
227
|
+
cparams.lora_base = "";
|
|
228
|
+
cparams.mul_mat_q = true;
|
|
229
|
+
cparams.f16_kv = true;
|
|
230
|
+
cparams.logits_all = false;
|
|
231
|
+
cparams.vocab_only = false;
|
|
232
|
+
cparams.rope_scaling_type = LLAMA_ROPE_SCALING_TYPE_UNSPECIFIED;
|
|
233
|
+
cparams.rope_scaling_factor = 0.0f;
|
|
234
|
+
cparams.rope_scaling_orig_ctx_len = 0;
|
|
235
|
+
cparams.yarn_ext_factor = -1.0f;
|
|
236
|
+
cparams.yarn_attn_factor = 1.0f;
|
|
237
|
+
cparams.yarn_beta_fast = 32.0f;
|
|
238
|
+
cparams.yarn_beta_slow = 1.0f;
|
|
239
|
+
cparams.yarn_orig_ctx = 0;
|
|
240
|
+
cparams.offload_kqv = true;
|
|
241
|
+
cparams.flash_attn = false;
|
|
242
|
+
cparams.flash_attn_kernel = false;
|
|
243
|
+
cparams.flash_attn_causal = true;
|
|
244
|
+
cparams.mmproj = "";
|
|
245
|
+
cparams.image = "";
|
|
246
|
+
cparams.export = "";
|
|
247
|
+
cparams.export_path = "";
|
|
248
|
+
cparams.seed = -1;
|
|
249
|
+
cparams.n_keep = 0;
|
|
250
|
+
cparams.n_discard = -1;
|
|
251
|
+
cparams.n_draft = 0;
|
|
252
|
+
cparams.n_chunks = -1;
|
|
253
|
+
cparams.n_parallel = 1;
|
|
254
|
+
cparams.n_sequences = 1;
|
|
255
|
+
cparams.p_accept = 0.5f;
|
|
256
|
+
cparams.p_split = 0.1f;
|
|
257
|
+
cparams.n_gqa = 8;
|
|
258
|
+
cparams.rms_norm_eps = 5e-6f;
|
|
259
|
+
cparams.model_alias = "unknown";
|
|
260
|
+
cparams.ubatch_size = 512;
|
|
261
|
+
cparams.ubatch_seq_len_max = 1;
|
|
157
262
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
263
|
+
LOGI("Initialized common parameters, attempting to load model from: %s", full_model_path.c_str());
|
|
264
|
+
LOGI("Model parameters: n_ctx=%d, n_batch=%d, n_gpu_layers=%d",
|
|
265
|
+
cparams.n_ctx, cparams.n_batch, cparams.n_gpu_layers);
|
|
266
|
+
|
|
267
|
+
// Try to load the model with error handling
|
|
268
|
+
bool load_success = false;
|
|
269
|
+
try {
|
|
270
|
+
load_success = context->loadModel(cparams);
|
|
271
|
+
} catch (const std::exception& e) {
|
|
272
|
+
LOGE("Exception during model loading: %s", e.what());
|
|
273
|
+
load_success = false;
|
|
274
|
+
} catch (...) {
|
|
275
|
+
LOGE("Unknown exception during model loading");
|
|
276
|
+
load_success = false;
|
|
162
277
|
}
|
|
163
278
|
|
|
279
|
+
if (!load_success) {
|
|
280
|
+
LOGE("context->loadModel() returned false - model loading failed");
|
|
281
|
+
|
|
282
|
+
// Try with minimal parameters as fallback
|
|
283
|
+
LOGI("Trying with minimal parameters...");
|
|
284
|
+
common_params minimal_params;
|
|
285
|
+
minimal_params.model.path = full_model_path;
|
|
286
|
+
minimal_params.n_ctx = 512;
|
|
287
|
+
minimal_params.n_batch = 256;
|
|
288
|
+
minimal_params.n_gpu_layers = 0;
|
|
289
|
+
minimal_params.use_mmap = true;
|
|
290
|
+
minimal_params.use_mlock = false;
|
|
291
|
+
minimal_params.numa = LM_GGML_NUMA_STRATEGY_DISABLED;
|
|
292
|
+
minimal_params.ctx_shift = false;
|
|
293
|
+
minimal_params.chat_template = "";
|
|
294
|
+
minimal_params.embedding = false;
|
|
295
|
+
minimal_params.cont_batching = false;
|
|
296
|
+
minimal_params.parallel = false;
|
|
297
|
+
minimal_params.grammar = "";
|
|
298
|
+
minimal_params.grammar_penalty.clear();
|
|
299
|
+
minimal_params.antiprompt.clear();
|
|
300
|
+
minimal_params.lora_adapter.clear();
|
|
301
|
+
minimal_params.lora_base = "";
|
|
302
|
+
minimal_params.mul_mat_q = true;
|
|
303
|
+
minimal_params.f16_kv = true;
|
|
304
|
+
minimal_params.logits_all = false;
|
|
305
|
+
minimal_params.vocab_only = false;
|
|
306
|
+
minimal_params.rope_scaling_type = LLAMA_ROPE_SCALING_TYPE_UNSPECIFIED;
|
|
307
|
+
minimal_params.rope_scaling_factor = 0.0f;
|
|
308
|
+
minimal_params.rope_scaling_orig_ctx_len = 0;
|
|
309
|
+
minimal_params.yarn_ext_factor = -1.0f;
|
|
310
|
+
minimal_params.yarn_attn_factor = 1.0f;
|
|
311
|
+
minimal_params.yarn_beta_fast = 32.0f;
|
|
312
|
+
minimal_params.yarn_beta_slow = 1.0f;
|
|
313
|
+
minimal_params.yarn_orig_ctx = 0;
|
|
314
|
+
minimal_params.offload_kqv = true;
|
|
315
|
+
minimal_params.flash_attn = false;
|
|
316
|
+
minimal_params.flash_attn_kernel = false;
|
|
317
|
+
minimal_params.flash_attn_causal = true;
|
|
318
|
+
minimal_params.mmproj = "";
|
|
319
|
+
minimal_params.image = "";
|
|
320
|
+
minimal_params.export = "";
|
|
321
|
+
minimal_params.export_path = "";
|
|
322
|
+
minimal_params.seed = -1;
|
|
323
|
+
minimal_params.n_keep = 0;
|
|
324
|
+
minimal_params.n_discard = -1;
|
|
325
|
+
minimal_params.n_draft = 0;
|
|
326
|
+
minimal_params.n_chunks = -1;
|
|
327
|
+
minimal_params.n_parallel = 1;
|
|
328
|
+
minimal_params.n_sequences = 1;
|
|
329
|
+
minimal_params.p_accept = 0.5f;
|
|
330
|
+
minimal_params.p_split = 0.1f;
|
|
331
|
+
minimal_params.n_gqa = 8;
|
|
332
|
+
minimal_params.rms_norm_eps = 5e-6f;
|
|
333
|
+
minimal_params.model_alias = "unknown";
|
|
334
|
+
minimal_params.ubatch_size = 256;
|
|
335
|
+
minimal_params.ubatch_seq_len_max = 1;
|
|
336
|
+
|
|
337
|
+
try {
|
|
338
|
+
load_success = context->loadModel(minimal_params);
|
|
339
|
+
} catch (const std::exception& e) {
|
|
340
|
+
LOGE("Exception during minimal model loading: %s", e.what());
|
|
341
|
+
load_success = false;
|
|
342
|
+
} catch (...) {
|
|
343
|
+
LOGE("Unknown exception during minimal model loading");
|
|
344
|
+
load_success = false;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
if (!load_success) {
|
|
348
|
+
LOGE("Model loading failed even with minimal parameters");
|
|
349
|
+
throw_java_exception(env, "java/lang/RuntimeException", "Failed to load model - possible model corruption or incompatibility");
|
|
350
|
+
return -1;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
LOGI("Model loaded successfully!");
|
|
355
|
+
|
|
164
356
|
// Store context
|
|
165
357
|
jlong context_id = next_context_id++;
|
|
166
358
|
contexts[context_id] = std::move(context);
|
|
167
359
|
|
|
168
|
-
LOGI("Initialized context %ld with model: %s", context_id,
|
|
360
|
+
LOGI("Initialized context %ld with model: %s", context_id, full_model_path.c_str());
|
|
169
361
|
return context_id;
|
|
170
362
|
|
|
171
363
|
} catch (const std::exception& e) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "llama-cpp-capacitor",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.12",
|
|
4
4
|
"description": "A native Capacitor plugin that embeds llama.cpp directly into mobile apps, enabling offline AI inference with comprehensive support for text generation, multimodal processing, TTS, LoRA adapters, and more.",
|
|
5
5
|
"main": "dist/plugin.cjs.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|