cui-llama.rn 1.3.0 → 1.3.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.
Files changed (76) hide show
  1. package/android/src/main/CMakeLists.txt +6 -1
  2. package/android/src/main/jni.cpp +6 -6
  3. package/cpp/amx/amx.cpp +196 -0
  4. package/cpp/amx/amx.h +20 -0
  5. package/cpp/amx/common.h +101 -0
  6. package/cpp/amx/mmq.cpp +2524 -0
  7. package/cpp/amx/mmq.h +16 -0
  8. package/cpp/common.cpp +1981 -1682
  9. package/cpp/common.h +636 -600
  10. package/cpp/ggml-aarch64.c +129 -129
  11. package/cpp/ggml-aarch64.h +19 -19
  12. package/cpp/ggml-alloc.c +1038 -1040
  13. package/cpp/ggml-alloc.h +76 -76
  14. package/cpp/ggml-backend-impl.h +238 -216
  15. package/cpp/ggml-backend-reg.cpp +423 -195
  16. package/cpp/ggml-backend.cpp +1999 -1997
  17. package/cpp/ggml-backend.h +351 -328
  18. package/cpp/ggml-common.h +1859 -1853
  19. package/cpp/ggml-cpp.h +38 -38
  20. package/cpp/ggml-cpu-aarch64.c +3823 -3560
  21. package/cpp/ggml-cpu-aarch64.h +32 -30
  22. package/cpp/ggml-cpu-impl.h +386 -371
  23. package/cpp/ggml-cpu-quants.c +10835 -10822
  24. package/cpp/ggml-cpu-quants.h +63 -63
  25. package/cpp/ggml-cpu.c +99 -103
  26. package/cpp/ggml-cpu.cpp +69 -17
  27. package/cpp/ggml-cpu.h +152 -177
  28. package/cpp/ggml-impl.h +556 -550
  29. package/cpp/ggml-metal.h +66 -66
  30. package/cpp/ggml-metal.m +4426 -4294
  31. package/cpp/ggml-quants.c +5247 -5247
  32. package/cpp/ggml-quants.h +100 -100
  33. package/cpp/ggml-threading.cpp +12 -12
  34. package/cpp/ggml-threading.h +12 -12
  35. package/cpp/ggml.c +7618 -8180
  36. package/cpp/ggml.h +2255 -2411
  37. package/cpp/json-schema-to-grammar.cpp +1045 -0
  38. package/cpp/json-schema-to-grammar.h +8 -0
  39. package/cpp/json.hpp +24766 -0
  40. package/cpp/llama-grammar.cpp +1138 -1138
  41. package/cpp/llama-grammar.h +144 -144
  42. package/cpp/llama-impl.h +181 -181
  43. package/cpp/llama-sampling.cpp +2348 -2348
  44. package/cpp/llama-sampling.h +48 -48
  45. package/cpp/llama-vocab.cpp +1984 -1984
  46. package/cpp/llama-vocab.h +170 -170
  47. package/cpp/llama.cpp +22332 -22132
  48. package/cpp/llama.h +1259 -1253
  49. package/cpp/log.cpp +401 -401
  50. package/cpp/log.h +121 -121
  51. package/cpp/rn-llama.hpp +6 -6
  52. package/cpp/sampling.cpp +505 -466
  53. package/cpp/sampling.h +22 -1
  54. package/cpp/sgemm.cpp +1884 -1884
  55. package/cpp/speculative.cpp +270 -0
  56. package/cpp/speculative.h +28 -0
  57. package/cpp/unicode.cpp +11 -0
  58. package/ios/RNLlamaContext.mm +13 -0
  59. package/lib/commonjs/NativeRNLlama.js.map +1 -1
  60. package/lib/commonjs/grammar.js +4 -2
  61. package/lib/commonjs/grammar.js.map +1 -1
  62. package/lib/commonjs/index.js.map +1 -1
  63. package/lib/module/NativeRNLlama.js.map +1 -1
  64. package/lib/module/grammar.js +2 -1
  65. package/lib/module/grammar.js.map +1 -1
  66. package/lib/module/index.js.map +1 -1
  67. package/lib/typescript/NativeRNLlama.d.ts +94 -4
  68. package/lib/typescript/NativeRNLlama.d.ts.map +1 -1
  69. package/lib/typescript/grammar.d.ts +5 -6
  70. package/lib/typescript/grammar.d.ts.map +1 -1
  71. package/lib/typescript/index.d.ts +4 -2
  72. package/lib/typescript/index.d.ts.map +1 -1
  73. package/package.json +2 -1
  74. package/src/NativeRNLlama.ts +97 -10
  75. package/src/grammar.ts +10 -8
  76. package/src/index.ts +22 -1
package/cpp/log.h CHANGED
@@ -1,121 +1,121 @@
1
- #pragma once
2
-
3
- #include "ggml.h" // for lm_ggml_log_level
4
-
5
- #ifndef __GNUC__
6
- # define LOG_ATTRIBUTE_FORMAT(...)
7
- #elif defined(__MINGW32__)
8
- # define LOG_ATTRIBUTE_FORMAT(...) __attribute__((format(gnu_printf, __VA_ARGS__)))
9
- #else
10
- # define LOG_ATTRIBUTE_FORMAT(...) __attribute__((format(printf, __VA_ARGS__)))
11
- #endif
12
-
13
- #define LOG_DEFAULT_DEBUG 1
14
- #define LOG_DEFAULT_LLAMA 0
15
-
16
- // needed by the LOG_TMPL macro to avoid computing log arguments if the verbosity lower
17
- // set via common_log_set_verbosity()
18
- extern int common_log_verbosity_thold;
19
-
20
- void common_log_set_verbosity_thold(int verbosity); // not thread-safe
21
-
22
- // the common_log uses an internal worker thread to print/write log messages
23
- // when the worker thread is paused, incoming log messages are discarded
24
- struct common_log;
25
-
26
- struct common_log * common_log_init();
27
- struct common_log * common_log_main(); // singleton, automatically destroys itself on exit
28
- void common_log_pause (struct common_log * log); // pause the worker thread, not thread-safe
29
- void common_log_resume(struct common_log * log); // resume the worker thread, not thread-safe
30
- void common_log_free (struct common_log * log);
31
-
32
- LOG_ATTRIBUTE_FORMAT(3, 4)
33
- void common_log_add(struct common_log * log, enum lm_ggml_log_level level, const char * fmt, ...);
34
-
35
- // defaults: file = NULL, colors = false, prefix = false, timestamps = false
36
- //
37
- // regular log output:
38
- //
39
- // lm_ggml_backend_metal_log_allocated_size: allocated buffer, size = 6695.84 MiB, ( 6695.91 / 21845.34)
40
- // llm_load_tensors: ggml ctx size = 0.27 MiB
41
- // llm_load_tensors: offloading 32 repeating layers to GPU
42
- // llm_load_tensors: offloading non-repeating layers to GPU
43
- //
44
- // with prefix = true, timestamps = true, the log output will look like this:
45
- //
46
- // 0.00.035.060 D lm_ggml_backend_metal_log_allocated_size: allocated buffer, size = 6695.84 MiB, ( 6695.91 / 21845.34)
47
- // 0.00.035.064 I llm_load_tensors: ggml ctx size = 0.27 MiB
48
- // 0.00.090.578 I llm_load_tensors: offloading 32 repeating layers to GPU
49
- // 0.00.090.579 I llm_load_tensors: offloading non-repeating layers to GPU
50
- //
51
- // I - info (stdout, V = 0)
52
- // W - warning (stderr, V = 0)
53
- // E - error (stderr, V = 0)
54
- // D - debug (stderr, V = LOG_DEFAULT_DEBUG)
55
- //
56
-
57
- void common_log_set_file (struct common_log * log, const char * file); // not thread-safe
58
- void common_log_set_colors (struct common_log * log, bool colors); // not thread-safe
59
- void common_log_set_prefix (struct common_log * log, bool prefix); // whether to output prefix to each log
60
- void common_log_set_timestamps(struct common_log * log, bool timestamps); // whether to output timestamps in the prefix
61
-
62
- // helper macros for logging
63
- // use these to avoid computing log arguments if the verbosity of the log is higher than the threshold
64
- //
65
- // for example:
66
- //
67
- // LOG_DBG("this is a debug message: %d\n", expensive_function());
68
- //
69
- // this will avoid calling expensive_function() if LOG_DEFAULT_DEBUG > common_log_verbosity_thold
70
- //
71
-
72
-
73
-
74
- #if defined(__ANDROID__)
75
- #include <android/log.h>
76
- #define LLAMA_ANDROID_LOG_TAG "RNLLAMA_LOG_ANDROID"
77
-
78
- #if defined(RNLLAMA_ANDROID_ENABLE_LOGGING)
79
- #define RNLLAMA_LOG_LEVEL 1
80
- #else
81
- #define RNLLAMA_LOG_LEVEL 0
82
- #endif
83
-
84
- #define LOG_TMPL(level, verbosity, ...) \
85
- do { \
86
- if ((verbosity) <= RNLLAMA_LOG_LEVEL) { \
87
- int android_log_level = ANDROID_LOG_DEFAULT; \
88
- switch (level) { \
89
- case LM_GGML_LOG_LEVEL_INFO: android_log_level = ANDROID_LOG_INFO; break; \
90
- case LM_GGML_LOG_LEVEL_WARN: android_log_level = ANDROID_LOG_WARN; break; \
91
- case LM_GGML_LOG_LEVEL_ERROR: android_log_level = ANDROID_LOG_ERROR; break; \
92
- default: android_log_level = ANDROID_LOG_DEFAULT; \
93
- } \
94
- __android_log_print(android_log_level, LLAMA_ANDROID_LOG_TAG, __VA_ARGS__); \
95
- } \
96
- } while(0)
97
- #else
98
-
99
- #define LOG_TMPL(level, verbosity, ...) \
100
- do { \
101
- if ((verbosity) <= common_log_verbosity_thold) { \
102
- common_log_add(common_log_main(), (level), __VA_ARGS__); \
103
- } \
104
- } while (0)
105
-
106
- #endif
107
-
108
- #define LOG(...) LOG_TMPL(LM_GGML_LOG_LEVEL_NONE, 0, __VA_ARGS__)
109
- #define LOGV(verbosity, ...) LOG_TMPL(LM_GGML_LOG_LEVEL_NONE, verbosity, __VA_ARGS__)
110
-
111
- #define LOG_INF(...) LOG_TMPL(LM_GGML_LOG_LEVEL_INFO, 0, __VA_ARGS__)
112
- #define LOG_WRN(...) LOG_TMPL(LM_GGML_LOG_LEVEL_WARN, 0, __VA_ARGS__)
113
- #define LOG_ERR(...) LOG_TMPL(LM_GGML_LOG_LEVEL_ERROR, 0, __VA_ARGS__)
114
- #define LOG_DBG(...) LOG_TMPL(LM_GGML_LOG_LEVEL_DEBUG, LOG_DEFAULT_DEBUG, __VA_ARGS__)
115
- #define LOG_CNT(...) LOG_TMPL(LM_GGML_LOG_LEVEL_CONT, 0, __VA_ARGS__)
116
-
117
- #define LOG_INFV(verbosity, ...) LOG_TMPL(LM_GGML_LOG_LEVEL_INFO, verbosity, __VA_ARGS__)
118
- #define LOG_WRNV(verbosity, ...) LOG_TMPL(LM_GGML_LOG_LEVEL_WARN, verbosity, __VA_ARGS__)
119
- #define LOG_ERRV(verbosity, ...) LOG_TMPL(LM_GGML_LOG_LEVEL_ERROR, verbosity, __VA_ARGS__)
120
- #define LOG_DBGV(verbosity, ...) LOG_TMPL(LM_GGML_LOG_LEVEL_DEBUG, verbosity, __VA_ARGS__)
121
- #define LOG_CNTV(verbosity, ...) LOG_TMPL(LM_GGML_LOG_LEVEL_CONT, verbosity, __VA_ARGS__)
1
+ #pragma once
2
+
3
+ #include "ggml.h" // for lm_ggml_log_level
4
+
5
+ #ifndef __GNUC__
6
+ # define LOG_ATTRIBUTE_FORMAT(...)
7
+ #elif defined(__MINGW32__)
8
+ # define LOG_ATTRIBUTE_FORMAT(...) __attribute__((format(gnu_printf, __VA_ARGS__)))
9
+ #else
10
+ # define LOG_ATTRIBUTE_FORMAT(...) __attribute__((format(printf, __VA_ARGS__)))
11
+ #endif
12
+
13
+ #define LOG_DEFAULT_DEBUG 1
14
+ #define LOG_DEFAULT_LLAMA 0
15
+
16
+ // needed by the LOG_TMPL macro to avoid computing log arguments if the verbosity lower
17
+ // set via common_log_set_verbosity()
18
+ extern int common_log_verbosity_thold;
19
+
20
+ void common_log_set_verbosity_thold(int verbosity); // not thread-safe
21
+
22
+ // the common_log uses an internal worker thread to print/write log messages
23
+ // when the worker thread is paused, incoming log messages are discarded
24
+ struct common_log;
25
+
26
+ struct common_log * common_log_init();
27
+ struct common_log * common_log_main(); // singleton, automatically destroys itself on exit
28
+ void common_log_pause (struct common_log * log); // pause the worker thread, not thread-safe
29
+ void common_log_resume(struct common_log * log); // resume the worker thread, not thread-safe
30
+ void common_log_free (struct common_log * log);
31
+
32
+ LOG_ATTRIBUTE_FORMAT(3, 4)
33
+ void common_log_add(struct common_log * log, enum lm_ggml_log_level level, const char * fmt, ...);
34
+
35
+ // defaults: file = NULL, colors = false, prefix = false, timestamps = false
36
+ //
37
+ // regular log output:
38
+ //
39
+ // lm_ggml_backend_metal_log_allocated_size: allocated buffer, size = 6695.84 MiB, ( 6695.91 / 21845.34)
40
+ // llm_load_tensors: ggml ctx size = 0.27 MiB
41
+ // llm_load_tensors: offloading 32 repeating layers to GPU
42
+ // llm_load_tensors: offloading non-repeating layers to GPU
43
+ //
44
+ // with prefix = true, timestamps = true, the log output will look like this:
45
+ //
46
+ // 0.00.035.060 D lm_ggml_backend_metal_log_allocated_size: allocated buffer, size = 6695.84 MiB, ( 6695.91 / 21845.34)
47
+ // 0.00.035.064 I llm_load_tensors: ggml ctx size = 0.27 MiB
48
+ // 0.00.090.578 I llm_load_tensors: offloading 32 repeating layers to GPU
49
+ // 0.00.090.579 I llm_load_tensors: offloading non-repeating layers to GPU
50
+ //
51
+ // I - info (stdout, V = 0)
52
+ // W - warning (stderr, V = 0)
53
+ // E - error (stderr, V = 0)
54
+ // D - debug (stderr, V = LOG_DEFAULT_DEBUG)
55
+ //
56
+
57
+ void common_log_set_file (struct common_log * log, const char * file); // not thread-safe
58
+ void common_log_set_colors (struct common_log * log, bool colors); // not thread-safe
59
+ void common_log_set_prefix (struct common_log * log, bool prefix); // whether to output prefix to each log
60
+ void common_log_set_timestamps(struct common_log * log, bool timestamps); // whether to output timestamps in the prefix
61
+
62
+ // helper macros for logging
63
+ // use these to avoid computing log arguments if the verbosity of the log is higher than the threshold
64
+ //
65
+ // for example:
66
+ //
67
+ // LOG_DBG("this is a debug message: %d\n", expensive_function());
68
+ //
69
+ // this will avoid calling expensive_function() if LOG_DEFAULT_DEBUG > common_log_verbosity_thold
70
+ //
71
+
72
+
73
+
74
+ #if defined(__ANDROID__)
75
+ #include <android/log.h>
76
+ #define LLAMA_ANDROID_LOG_TAG "RNLLAMA_LOG_ANDROID"
77
+
78
+ #if defined(RNLLAMA_ANDROID_ENABLE_LOGGING)
79
+ #define RNLLAMA_LOG_LEVEL 1
80
+ #else
81
+ #define RNLLAMA_LOG_LEVEL 0
82
+ #endif
83
+
84
+ #define LOG_TMPL(level, verbosity, ...) \
85
+ do { \
86
+ if ((verbosity) <= RNLLAMA_LOG_LEVEL) { \
87
+ int android_log_level = ANDROID_LOG_DEFAULT; \
88
+ switch (level) { \
89
+ case LM_GGML_LOG_LEVEL_INFO: android_log_level = ANDROID_LOG_INFO; break; \
90
+ case LM_GGML_LOG_LEVEL_WARN: android_log_level = ANDROID_LOG_WARN; break; \
91
+ case LM_GGML_LOG_LEVEL_ERROR: android_log_level = ANDROID_LOG_ERROR; break; \
92
+ default: android_log_level = ANDROID_LOG_DEFAULT; \
93
+ } \
94
+ __android_log_print(android_log_level, LLAMA_ANDROID_LOG_TAG, __VA_ARGS__); \
95
+ } \
96
+ } while(0)
97
+ #else
98
+
99
+ #define LOG_TMPL(level, verbosity, ...) \
100
+ do { \
101
+ if ((verbosity) <= common_log_verbosity_thold) { \
102
+ common_log_add(common_log_main(), (level), __VA_ARGS__); \
103
+ } \
104
+ } while (0)
105
+
106
+ #endif
107
+
108
+ #define LOG(...) LOG_TMPL(LM_GGML_LOG_LEVEL_NONE, 0, __VA_ARGS__)
109
+ #define LOGV(verbosity, ...) LOG_TMPL(LM_GGML_LOG_LEVEL_NONE, verbosity, __VA_ARGS__)
110
+
111
+ #define LOG_INF(...) LOG_TMPL(LM_GGML_LOG_LEVEL_INFO, 0, __VA_ARGS__)
112
+ #define LOG_WRN(...) LOG_TMPL(LM_GGML_LOG_LEVEL_WARN, 0, __VA_ARGS__)
113
+ #define LOG_ERR(...) LOG_TMPL(LM_GGML_LOG_LEVEL_ERROR, 0, __VA_ARGS__)
114
+ #define LOG_DBG(...) LOG_TMPL(LM_GGML_LOG_LEVEL_DEBUG, LOG_DEFAULT_DEBUG, __VA_ARGS__)
115
+ #define LOG_CNT(...) LOG_TMPL(LM_GGML_LOG_LEVEL_CONT, 0, __VA_ARGS__)
116
+
117
+ #define LOG_INFV(verbosity, ...) LOG_TMPL(LM_GGML_LOG_LEVEL_INFO, verbosity, __VA_ARGS__)
118
+ #define LOG_WRNV(verbosity, ...) LOG_TMPL(LM_GGML_LOG_LEVEL_WARN, verbosity, __VA_ARGS__)
119
+ #define LOG_ERRV(verbosity, ...) LOG_TMPL(LM_GGML_LOG_LEVEL_ERROR, verbosity, __VA_ARGS__)
120
+ #define LOG_DBGV(verbosity, ...) LOG_TMPL(LM_GGML_LOG_LEVEL_DEBUG, verbosity, __VA_ARGS__)
121
+ #define LOG_CNTV(verbosity, ...) LOG_TMPL(LM_GGML_LOG_LEVEL_CONT, verbosity, __VA_ARGS__)
package/cpp/rn-llama.hpp CHANGED
@@ -197,7 +197,6 @@ static std::string tokens_to_str(llama_context *ctx, Iter begin, Iter end)
197
197
  return ret;
198
198
  }
199
199
 
200
-
201
200
  struct llama_rn_context
202
201
  {
203
202
  bool is_predicting = false;
@@ -253,7 +252,7 @@ struct llama_rn_context
253
252
  {
254
253
  is_interrupted = false;
255
254
  params.antiprompt.clear();
256
- params.sparams.grammar.clear();
255
+ params.sampling.grammar.clear();
257
256
  num_prompt_tokens = 0;
258
257
  num_tokens_predicted = 0;
259
258
  generated_text = "";
@@ -267,14 +266,14 @@ struct llama_rn_context
267
266
  incomplete = false;
268
267
  n_remain = 0;
269
268
  n_past = 0;
270
- params.sparams.n_prev = n_ctx;
269
+ params.sampling.n_prev = n_ctx;
271
270
  }
272
271
 
273
272
  bool initSampling() {
274
273
  if (ctx_sampling != nullptr) {
275
274
  common_sampler_free(ctx_sampling);
276
275
  }
277
- ctx_sampling = common_sampler_init(model, params.sparams);
276
+ ctx_sampling = common_sampler_init(model, params.sampling);
278
277
  return ctx_sampling != nullptr;
279
278
  }
280
279
 
@@ -289,6 +288,7 @@ struct llama_rn_context
289
288
  LOG_ERROR("unable to load model: %s", params_.model.c_str());
290
289
  return false;
291
290
  }
291
+ LOG_VERBOSE("getting n_ctx");
292
292
  n_ctx = llama_n_ctx(ctx);
293
293
  return true;
294
294
  }
@@ -474,7 +474,7 @@ struct llama_rn_context
474
474
 
475
475
  llama_token_data_array cur_p = *common_sampler_get_candidates(ctx_sampling);
476
476
 
477
- const int32_t n_probs = params.sparams.n_probs;
477
+ const int32_t n_probs = params.sampling.n_probs;
478
478
 
479
479
  // deprecated
480
480
  /*if (params.sparams.temp <= 0 && n_probs > 0)
@@ -553,7 +553,7 @@ struct llama_rn_context
553
553
  const std::string token_text = token_with_probs.tok == -1 ? "" : common_token_to_piece(ctx, token_with_probs.tok);
554
554
  generated_text += token_text;
555
555
 
556
- if (params.sparams.n_probs > 0)
556
+ if (params.sampling.n_probs > 0)
557
557
  {
558
558
  generated_token_probs.push_back(token_with_probs);
559
559
  }