cui-llama.rn 1.4.3 → 1.4.6

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 (134) hide show
  1. package/README.md +93 -114
  2. package/android/src/main/CMakeLists.txt +5 -0
  3. package/android/src/main/java/com/rnllama/LlamaContext.java +91 -17
  4. package/android/src/main/java/com/rnllama/RNLlama.java +37 -4
  5. package/android/src/main/jni-utils.h +6 -0
  6. package/android/src/main/jni.cpp +289 -31
  7. package/android/src/main/jniLibs/arm64-v8a/librnllama.so +0 -0
  8. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8.so +0 -0
  9. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2.so +0 -0
  10. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_dotprod.so +0 -0
  11. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_dotprod_i8mm.so +0 -0
  12. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_i8mm.so +0 -0
  13. package/android/src/main/jniLibs/x86_64/librnllama.so +0 -0
  14. package/android/src/main/jniLibs/x86_64/librnllama_x86_64.so +0 -0
  15. package/android/src/newarch/java/com/rnllama/RNLlamaModule.java +7 -2
  16. package/android/src/oldarch/java/com/rnllama/RNLlamaModule.java +7 -2
  17. package/cpp/chat-template.hpp +529 -0
  18. package/cpp/chat.cpp +1779 -0
  19. package/cpp/chat.h +135 -0
  20. package/cpp/common.cpp +2064 -1873
  21. package/cpp/common.h +700 -699
  22. package/cpp/ggml-alloc.c +1039 -1042
  23. package/cpp/ggml-alloc.h +1 -1
  24. package/cpp/ggml-backend-impl.h +255 -255
  25. package/cpp/ggml-backend-reg.cpp +586 -582
  26. package/cpp/ggml-backend.cpp +2004 -2002
  27. package/cpp/ggml-backend.h +354 -354
  28. package/cpp/ggml-common.h +1851 -1853
  29. package/cpp/ggml-cpp.h +39 -39
  30. package/cpp/ggml-cpu-aarch64.cpp +4248 -4247
  31. package/cpp/ggml-cpu-aarch64.h +8 -8
  32. package/cpp/ggml-cpu-impl.h +531 -386
  33. package/cpp/ggml-cpu-quants.c +12527 -10920
  34. package/cpp/ggml-cpu-traits.cpp +36 -36
  35. package/cpp/ggml-cpu-traits.h +38 -38
  36. package/cpp/ggml-cpu.c +15766 -14391
  37. package/cpp/ggml-cpu.cpp +655 -635
  38. package/cpp/ggml-cpu.h +138 -135
  39. package/cpp/ggml-impl.h +567 -567
  40. package/cpp/ggml-metal-impl.h +235 -0
  41. package/cpp/ggml-metal.h +1 -1
  42. package/cpp/ggml-metal.m +5146 -4884
  43. package/cpp/ggml-opt.cpp +854 -854
  44. package/cpp/ggml-opt.h +216 -216
  45. package/cpp/ggml-quants.c +5238 -5238
  46. package/cpp/ggml-threading.h +14 -14
  47. package/cpp/ggml.c +6529 -6514
  48. package/cpp/ggml.h +2198 -2194
  49. package/cpp/gguf.cpp +1329 -1329
  50. package/cpp/gguf.h +202 -202
  51. package/cpp/json-schema-to-grammar.cpp +1024 -1045
  52. package/cpp/json-schema-to-grammar.h +21 -8
  53. package/cpp/json.hpp +24766 -24766
  54. package/cpp/llama-adapter.cpp +347 -347
  55. package/cpp/llama-adapter.h +74 -74
  56. package/cpp/llama-arch.cpp +1513 -1487
  57. package/cpp/llama-arch.h +403 -400
  58. package/cpp/llama-batch.cpp +368 -368
  59. package/cpp/llama-batch.h +88 -88
  60. package/cpp/llama-chat.cpp +588 -578
  61. package/cpp/llama-chat.h +53 -52
  62. package/cpp/llama-context.cpp +1775 -1775
  63. package/cpp/llama-context.h +128 -128
  64. package/cpp/llama-cparams.cpp +1 -1
  65. package/cpp/llama-cparams.h +37 -37
  66. package/cpp/llama-cpp.h +30 -30
  67. package/cpp/llama-grammar.cpp +1219 -1139
  68. package/cpp/llama-grammar.h +173 -143
  69. package/cpp/llama-hparams.cpp +71 -71
  70. package/cpp/llama-hparams.h +139 -139
  71. package/cpp/llama-impl.cpp +167 -167
  72. package/cpp/llama-impl.h +61 -61
  73. package/cpp/llama-kv-cache.cpp +718 -718
  74. package/cpp/llama-kv-cache.h +219 -218
  75. package/cpp/llama-mmap.cpp +600 -590
  76. package/cpp/llama-mmap.h +68 -67
  77. package/cpp/llama-model-loader.cpp +1124 -1124
  78. package/cpp/llama-model-loader.h +167 -167
  79. package/cpp/llama-model.cpp +4087 -3997
  80. package/cpp/llama-model.h +370 -370
  81. package/cpp/llama-sampling.cpp +2558 -2408
  82. package/cpp/llama-sampling.h +32 -32
  83. package/cpp/llama-vocab.cpp +3264 -3247
  84. package/cpp/llama-vocab.h +125 -125
  85. package/cpp/llama.cpp +10284 -10077
  86. package/cpp/llama.h +1354 -1323
  87. package/cpp/log.cpp +393 -401
  88. package/cpp/log.h +132 -121
  89. package/cpp/minja/chat-template.hpp +529 -0
  90. package/cpp/minja/minja.hpp +2915 -0
  91. package/cpp/minja.hpp +2915 -0
  92. package/cpp/rn-llama.cpp +66 -6
  93. package/cpp/rn-llama.h +26 -1
  94. package/cpp/sampling.cpp +570 -505
  95. package/cpp/sampling.h +3 -0
  96. package/cpp/sgemm.cpp +2598 -2597
  97. package/cpp/sgemm.h +14 -14
  98. package/cpp/speculative.cpp +278 -277
  99. package/cpp/speculative.h +28 -28
  100. package/cpp/unicode.cpp +9 -2
  101. package/ios/CMakeLists.txt +6 -0
  102. package/ios/RNLlama.h +0 -8
  103. package/ios/RNLlama.mm +27 -3
  104. package/ios/RNLlamaContext.h +10 -1
  105. package/ios/RNLlamaContext.mm +269 -57
  106. package/jest/mock.js +21 -2
  107. package/lib/commonjs/NativeRNLlama.js.map +1 -1
  108. package/lib/commonjs/grammar.js +3 -0
  109. package/lib/commonjs/grammar.js.map +1 -1
  110. package/lib/commonjs/index.js +87 -13
  111. package/lib/commonjs/index.js.map +1 -1
  112. package/lib/module/NativeRNLlama.js.map +1 -1
  113. package/lib/module/grammar.js +3 -0
  114. package/lib/module/grammar.js.map +1 -1
  115. package/lib/module/index.js +86 -13
  116. package/lib/module/index.js.map +1 -1
  117. package/lib/typescript/NativeRNLlama.d.ts +107 -2
  118. package/lib/typescript/NativeRNLlama.d.ts.map +1 -1
  119. package/lib/typescript/grammar.d.ts.map +1 -1
  120. package/lib/typescript/index.d.ts +32 -7
  121. package/lib/typescript/index.d.ts.map +1 -1
  122. package/llama-rn.podspec +1 -1
  123. package/package.json +3 -2
  124. package/src/NativeRNLlama.ts +115 -3
  125. package/src/grammar.ts +3 -0
  126. package/src/index.ts +138 -21
  127. package/android/src/main/build-arm64/CMakeFiles/3.31.4/CMakeCCompiler.cmake +0 -81
  128. package/android/src/main/build-arm64/CMakeFiles/3.31.4/CMakeSystem.cmake +0 -15
  129. package/android/src/main/build-arm64/CMakeFiles/3.31.4/CompilerIdC/CMakeCCompilerId.c +0 -904
  130. package/android/src/main/build-arm64/CMakeFiles/3.31.4/CompilerIdC/CMakeCCompilerId.o +0 -0
  131. package/android/src/main/build-arm64/CMakeFiles/3.31.4/CompilerIdCXX/CMakeCXXCompilerId.cpp +0 -919
  132. package/android/src/main/build-arm64/CMakeFiles/3.31.4/CompilerIdCXX/CMakeCXXCompilerId.o +0 -0
  133. package/android/src/main/build-arm64/CMakeFiles/CMakeConfigureLog.yaml +0 -55
  134. package/cpp/rn-llama.hpp +0 -913
package/cpp/log.h CHANGED
@@ -1,121 +1,132 @@
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
+ #define LOG_CLR_TO_EOL "\033[K\r"
6
+ #define LOG_COL_DEFAULT "\033[0m"
7
+ #define LOG_COL_BOLD "\033[1m"
8
+ #define LOG_COL_RED "\033[31m"
9
+ #define LOG_COL_GREEN "\033[32m"
10
+ #define LOG_COL_YELLOW "\033[33m"
11
+ #define LOG_COL_BLUE "\033[34m"
12
+ #define LOG_COL_MAGENTA "\033[35m"
13
+ #define LOG_COL_CYAN "\033[36m"
14
+ #define LOG_COL_WHITE "\033[37m"
15
+
16
+ #ifndef __GNUC__
17
+ # define LOG_ATTRIBUTE_FORMAT(...)
18
+ #elif defined(__MINGW32__) && !defined(__clang__)
19
+ # define LOG_ATTRIBUTE_FORMAT(...) __attribute__((format(gnu_printf, __VA_ARGS__)))
20
+ #else
21
+ # define LOG_ATTRIBUTE_FORMAT(...) __attribute__((format(printf, __VA_ARGS__)))
22
+ #endif
23
+
24
+ #define LOG_DEFAULT_DEBUG 1
25
+ #define LOG_DEFAULT_LLAMA 0
26
+
27
+ // needed by the LOG_TMPL macro to avoid computing log arguments if the verbosity lower
28
+ // set via common_log_set_verbosity()
29
+ extern int common_log_verbosity_thold;
30
+
31
+ void common_log_set_verbosity_thold(int verbosity); // not thread-safe
32
+
33
+ // the common_log uses an internal worker thread to print/write log messages
34
+ // when the worker thread is paused, incoming log messages are discarded
35
+ struct common_log;
36
+
37
+ struct common_log * common_log_init();
38
+ struct common_log * common_log_main(); // singleton, automatically destroys itself on exit
39
+ void common_log_pause (struct common_log * log); // pause the worker thread, not thread-safe
40
+ void common_log_resume(struct common_log * log); // resume the worker thread, not thread-safe
41
+ void common_log_free (struct common_log * log);
42
+
43
+ LOG_ATTRIBUTE_FORMAT(3, 4)
44
+ void common_log_add(struct common_log * log, enum lm_ggml_log_level level, const char * fmt, ...);
45
+
46
+ // defaults: file = NULL, colors = false, prefix = false, timestamps = false
47
+ //
48
+ // regular log output:
49
+ //
50
+ // lm_ggml_backend_metal_log_allocated_size: allocated buffer, size = 6695.84 MiB, ( 6695.91 / 21845.34)
51
+ // llm_load_tensors: ggml ctx size = 0.27 MiB
52
+ // llm_load_tensors: offloading 32 repeating layers to GPU
53
+ // llm_load_tensors: offloading non-repeating layers to GPU
54
+ //
55
+ // with prefix = true, timestamps = true, the log output will look like this:
56
+ //
57
+ // 0.00.035.060 D lm_ggml_backend_metal_log_allocated_size: allocated buffer, size = 6695.84 MiB, ( 6695.91 / 21845.34)
58
+ // 0.00.035.064 I llm_load_tensors: ggml ctx size = 0.27 MiB
59
+ // 0.00.090.578 I llm_load_tensors: offloading 32 repeating layers to GPU
60
+ // 0.00.090.579 I llm_load_tensors: offloading non-repeating layers to GPU
61
+ //
62
+ // I - info (stdout, V = 0)
63
+ // W - warning (stderr, V = 0)
64
+ // E - error (stderr, V = 0)
65
+ // D - debug (stderr, V = LOG_DEFAULT_DEBUG)
66
+ //
67
+
68
+ void common_log_set_file (struct common_log * log, const char * file); // not thread-safe
69
+ void common_log_set_colors (struct common_log * log, bool colors); // not thread-safe
70
+ void common_log_set_prefix (struct common_log * log, bool prefix); // whether to output prefix to each log
71
+ void common_log_set_timestamps(struct common_log * log, bool timestamps); // whether to output timestamps in the prefix
72
+
73
+ // helper macros for logging
74
+ // use these to avoid computing log arguments if the verbosity of the log is higher than the threshold
75
+ //
76
+ // for example:
77
+ //
78
+ // LOG_DBG("this is a debug message: %d\n", expensive_function());
79
+ //
80
+ // this will avoid calling expensive_function() if LOG_DEFAULT_DEBUG > common_log_verbosity_thold
81
+ //
82
+
83
+
84
+
85
+ #if defined(__ANDROID__)
86
+ #include <android/log.h>
87
+ #define LLAMA_ANDROID_LOG_TAG "RNLLAMA_LOG_ANDROID"
88
+
89
+ #if defined(RNLLAMA_ANDROID_ENABLE_LOGGING)
90
+ #define RNLLAMA_LOG_LEVEL 1
91
+ #else
92
+ #define RNLLAMA_LOG_LEVEL 0
93
+ #endif
94
+
95
+ #define LOG_TMPL(level, verbosity, ...) \
96
+ do { \
97
+ if ((verbosity) <= RNLLAMA_LOG_LEVEL) { \
98
+ int android_log_level = ANDROID_LOG_DEFAULT; \
99
+ switch (level) { \
100
+ case LM_GGML_LOG_LEVEL_INFO: android_log_level = ANDROID_LOG_INFO; break; \
101
+ case LM_GGML_LOG_LEVEL_WARN: android_log_level = ANDROID_LOG_WARN; break; \
102
+ case LM_GGML_LOG_LEVEL_ERROR: android_log_level = ANDROID_LOG_ERROR; break; \
103
+ default: android_log_level = ANDROID_LOG_DEFAULT; \
104
+ } \
105
+ __android_log_print(android_log_level, LLAMA_ANDROID_LOG_TAG, __VA_ARGS__); \
106
+ } \
107
+ } while(0)
108
+ #else
109
+
110
+ #define LOG_TMPL(level, verbosity, ...) \
111
+ do { \
112
+ if ((verbosity) <= common_log_verbosity_thold) { \
113
+ common_log_add(common_log_main(), (level), __VA_ARGS__); \
114
+ } \
115
+ } while (0)
116
+
117
+ #endif
118
+
119
+ #define LOG(...) LOG_TMPL(LM_GGML_LOG_LEVEL_NONE, 0, __VA_ARGS__)
120
+ #define LOGV(verbosity, ...) LOG_TMPL(LM_GGML_LOG_LEVEL_NONE, verbosity, __VA_ARGS__)
121
+
122
+ #define LOG_INF(...) LOG_TMPL(LM_GGML_LOG_LEVEL_INFO, 0, __VA_ARGS__)
123
+ #define LOG_WRN(...) LOG_TMPL(LM_GGML_LOG_LEVEL_WARN, 0, __VA_ARGS__)
124
+ #define LOG_ERR(...) LOG_TMPL(LM_GGML_LOG_LEVEL_ERROR, 0, __VA_ARGS__)
125
+ #define LOG_DBG(...) LOG_TMPL(LM_GGML_LOG_LEVEL_DEBUG, LOG_DEFAULT_DEBUG, __VA_ARGS__)
126
+ #define LOG_CNT(...) LOG_TMPL(LM_GGML_LOG_LEVEL_CONT, 0, __VA_ARGS__)
127
+
128
+ #define LOG_INFV(verbosity, ...) LOG_TMPL(LM_GGML_LOG_LEVEL_INFO, verbosity, __VA_ARGS__)
129
+ #define LOG_WRNV(verbosity, ...) LOG_TMPL(LM_GGML_LOG_LEVEL_WARN, verbosity, __VA_ARGS__)
130
+ #define LOG_ERRV(verbosity, ...) LOG_TMPL(LM_GGML_LOG_LEVEL_ERROR, verbosity, __VA_ARGS__)
131
+ #define LOG_DBGV(verbosity, ...) LOG_TMPL(LM_GGML_LOG_LEVEL_DEBUG, verbosity, __VA_ARGS__)
132
+ #define LOG_CNTV(verbosity, ...) LOG_TMPL(LM_GGML_LOG_LEVEL_CONT, verbosity, __VA_ARGS__)