llama-cpp-capacitor 0.0.10 → 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.
@@ -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 = model_path_str;
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;
@@ -199,42 +260,104 @@ Java_ai_annadata_plugin_capacitor_LlamaCpp_initContextNative(
199
260
  cparams.ubatch_size = 512;
200
261
  cparams.ubatch_seq_len_max = 1;
201
262
 
202
- // Load model
203
- LOGI("Attempting to load model from: %s", model_path_str.c_str());
263
+ LOGI("Initialized common parameters, attempting to load model from: %s", full_model_path.c_str());
204
264
  LOGI("Model parameters: n_ctx=%d, n_batch=%d, n_gpu_layers=%d",
205
265
  cparams.n_ctx, cparams.n_batch, cparams.n_gpu_layers);
206
266
 
207
- // Try to load the model
208
- bool load_success = context->loadModel(cparams);
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;
277
+ }
278
+
209
279
  if (!load_success) {
210
- LOGE("Model loading failed for: %s", model_path_str.c_str());
280
+ LOGE("context->loadModel() returned false - model loading failed");
211
281
 
212
282
  // Try with minimal parameters as fallback
213
283
  LOGI("Trying with minimal parameters...");
214
284
  common_params minimal_params;
215
- minimal_params.model.path = model_path_str;
285
+ minimal_params.model.path = full_model_path;
216
286
  minimal_params.n_ctx = 512;
217
287
  minimal_params.n_batch = 256;
218
288
  minimal_params.n_gpu_layers = 0;
219
289
  minimal_params.use_mmap = true;
220
290
  minimal_params.use_mlock = false;
221
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
+ }
222
346
 
223
- load_success = context->loadModel(minimal_params);
224
347
  if (!load_success) {
225
348
  LOGE("Model loading failed even with minimal parameters");
226
- throw_java_exception(env, "java/lang/RuntimeException", "Failed to load model");
349
+ throw_java_exception(env, "java/lang/RuntimeException", "Failed to load model - possible model corruption or incompatibility");
227
350
  return -1;
228
351
  }
229
352
  }
230
353
 
231
- LOGI("Model loaded successfully: %s", model_path_str.c_str());
354
+ LOGI("Model loaded successfully!");
232
355
 
233
356
  // Store context
234
357
  jlong context_id = next_context_id++;
235
358
  contexts[context_id] = std::move(context);
236
359
 
237
- LOGI("Initialized context %ld with model: %s", context_id, model_path_str.c_str());
360
+ LOGI("Initialized context %ld with model: %s", context_id, full_model_path.c_str());
238
361
  return context_id;
239
362
 
240
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.10",
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",