cui-llama.rn 1.4.4 → 1.5.0

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 (216) hide show
  1. package/android/src/main/CMakeLists.txt +9 -2
  2. package/android/src/main/jni.cpp +54 -34
  3. package/android/src/main/jniLibs/arm64-v8a/librnllama.so +0 -0
  4. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8.so +0 -0
  5. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2.so +0 -0
  6. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_dotprod.so +0 -0
  7. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_dotprod_i8mm.so +0 -0
  8. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_i8mm.so +0 -0
  9. package/android/src/main/jniLibs/x86_64/librnllama.so +0 -0
  10. package/android/src/main/jniLibs/x86_64/librnllama_x86_64.so +0 -0
  11. package/cpp/binary-ops.cpp +158 -0
  12. package/cpp/binary-ops.h +16 -0
  13. package/cpp/chat.cpp +1769 -1085
  14. package/cpp/chat.h +143 -0
  15. package/cpp/common.cpp +1562 -1996
  16. package/cpp/common.h +677 -744
  17. package/cpp/cpu-common.h +72 -0
  18. package/cpp/ggml-alloc.c +1039 -1030
  19. package/cpp/ggml-alloc.h +1 -1
  20. package/cpp/ggml-backend-impl.h +255 -255
  21. package/cpp/ggml-backend-reg.cpp +586 -582
  22. package/cpp/ggml-backend.cpp +2004 -2002
  23. package/cpp/ggml-backend.h +354 -354
  24. package/cpp/ggml-common.h +1857 -1851
  25. package/cpp/ggml-cpp.h +39 -39
  26. package/cpp/ggml-cpu-aarch64.cpp +5725 -4247
  27. package/cpp/ggml-cpu-aarch64.h +8 -8
  28. package/cpp/ggml-cpu-impl.h +512 -380
  29. package/cpp/ggml-cpu-quants.c +13026 -11517
  30. package/cpp/ggml-cpu-traits.cpp +36 -36
  31. package/cpp/ggml-cpu-traits.h +38 -38
  32. package/cpp/ggml-cpu.c +3438 -14485
  33. package/cpp/ggml-cpu.cpp +655 -633
  34. package/cpp/ggml-cpu.h +138 -135
  35. package/cpp/ggml-impl.h +594 -567
  36. package/cpp/ggml-metal-impl.h +312 -3
  37. package/cpp/ggml-metal.h +66 -66
  38. package/cpp/ggml-metal.m +5360 -5002
  39. package/cpp/ggml-opt.cpp +854 -854
  40. package/cpp/ggml-opt.h +216 -216
  41. package/cpp/ggml-quants.c +5238 -5238
  42. package/cpp/ggml-threading.h +14 -14
  43. package/cpp/ggml.c +6618 -6524
  44. package/cpp/ggml.h +2222 -2194
  45. package/cpp/gguf.cpp +1330 -1329
  46. package/cpp/gguf.h +202 -202
  47. package/cpp/json-schema-to-grammar.cpp +1024 -1025
  48. package/cpp/json-schema-to-grammar.h +21 -22
  49. package/cpp/json.hpp +24766 -24766
  50. package/cpp/llama-adapter.cpp +382 -347
  51. package/cpp/llama-adapter.h +76 -74
  52. package/cpp/llama-arch.cpp +1714 -1492
  53. package/cpp/llama-arch.h +428 -402
  54. package/cpp/llama-batch.cpp +368 -368
  55. package/cpp/llama-batch.h +88 -88
  56. package/cpp/llama-chat.cpp +640 -587
  57. package/cpp/llama-chat.h +56 -53
  58. package/cpp/llama-context.cpp +2831 -1775
  59. package/cpp/llama-context.h +265 -128
  60. package/cpp/llama-cparams.cpp +1 -1
  61. package/cpp/llama-cparams.h +38 -37
  62. package/cpp/llama-cpp.h +30 -30
  63. package/cpp/llama-grammar.cpp +1219 -1219
  64. package/cpp/llama-grammar.h +173 -164
  65. package/cpp/llama-graph.cpp +1695 -0
  66. package/cpp/llama-graph.h +592 -0
  67. package/cpp/llama-hparams.cpp +79 -71
  68. package/cpp/llama-hparams.h +156 -139
  69. package/cpp/llama-impl.cpp +167 -167
  70. package/cpp/llama-impl.h +61 -61
  71. package/cpp/llama-io.cpp +15 -0
  72. package/cpp/llama-io.h +35 -0
  73. package/cpp/llama-kv-cache.cpp +1380 -718
  74. package/cpp/llama-kv-cache.h +213 -218
  75. package/cpp/llama-memory.cpp +1 -0
  76. package/cpp/llama-memory.h +21 -0
  77. package/cpp/llama-mmap.cpp +600 -590
  78. package/cpp/llama-mmap.h +68 -68
  79. package/cpp/llama-model-loader.cpp +1129 -1124
  80. package/cpp/llama-model-loader.h +169 -167
  81. package/cpp/llama-model.cpp +13080 -4023
  82. package/cpp/llama-model.h +409 -370
  83. package/cpp/llama-sampling.cpp +2563 -2525
  84. package/cpp/llama-sampling.h +32 -32
  85. package/cpp/llama-vocab.cpp +3295 -3252
  86. package/cpp/llama-vocab.h +125 -125
  87. package/cpp/llama.cpp +351 -10137
  88. package/cpp/llama.h +1434 -1340
  89. package/cpp/log.cpp +427 -423
  90. package/cpp/log.h +132 -132
  91. package/cpp/{chat-template.hpp → minja/chat-template.hpp} +537 -529
  92. package/cpp/{minja.hpp → minja/minja.hpp} +2941 -2883
  93. package/cpp/ops.cpp +8723 -0
  94. package/cpp/ops.h +128 -0
  95. package/cpp/rn-llama.cpp +45 -71
  96. package/cpp/rn-llama.h +3 -3
  97. package/cpp/sampling.cpp +573 -532
  98. package/cpp/sgemm.cpp +3043 -2598
  99. package/cpp/sgemm.h +14 -14
  100. package/cpp/simd-mappings.h +888 -0
  101. package/cpp/speculative.cpp +278 -277
  102. package/cpp/speculative.h +28 -28
  103. package/cpp/unary-ops.cpp +186 -0
  104. package/cpp/unary-ops.h +28 -0
  105. package/cpp/vec.cpp +258 -0
  106. package/cpp/vec.h +802 -0
  107. package/ios/CMakeLists.txt +5 -2
  108. package/ios/RNLlama.mm +2 -2
  109. package/ios/RNLlamaContext.mm +40 -24
  110. package/package.json +1 -1
  111. package/src/NativeRNLlama.ts +6 -4
  112. package/src/index.ts +3 -1
  113. package/android/src/main/build-arm64/CMakeCache.txt +0 -429
  114. package/android/src/main/build-arm64/CMakeFiles/3.31.4/CMakeCCompiler.cmake +0 -81
  115. package/android/src/main/build-arm64/CMakeFiles/3.31.4/CMakeCXXCompiler.cmake +0 -101
  116. package/android/src/main/build-arm64/CMakeFiles/3.31.4/CMakeDetermineCompilerABI_C.bin +0 -0
  117. package/android/src/main/build-arm64/CMakeFiles/3.31.4/CMakeDetermineCompilerABI_CXX.bin +0 -0
  118. package/android/src/main/build-arm64/CMakeFiles/3.31.4/CMakeSystem.cmake +0 -15
  119. package/android/src/main/build-arm64/CMakeFiles/3.31.4/CompilerIdC/CMakeCCompilerId.c +0 -904
  120. package/android/src/main/build-arm64/CMakeFiles/3.31.4/CompilerIdC/CMakeCCompilerId.o +0 -0
  121. package/android/src/main/build-arm64/CMakeFiles/3.31.4/CompilerIdCXX/CMakeCXXCompilerId.cpp +0 -919
  122. package/android/src/main/build-arm64/CMakeFiles/3.31.4/CompilerIdCXX/CMakeCXXCompilerId.o +0 -0
  123. package/android/src/main/build-arm64/CMakeFiles/CMakeConfigureLog.yaml +0 -431
  124. package/android/src/main/build-arm64/CMakeFiles/CMakeDirectoryInformation.cmake +0 -16
  125. package/android/src/main/build-arm64/CMakeFiles/Makefile.cmake +0 -165
  126. package/android/src/main/build-arm64/CMakeFiles/Makefile2 +0 -297
  127. package/android/src/main/build-arm64/CMakeFiles/Progress/1 +0 -1
  128. package/android/src/main/build-arm64/CMakeFiles/Progress/2 +0 -1
  129. package/android/src/main/build-arm64/CMakeFiles/Progress/3 +0 -1
  130. package/android/src/main/build-arm64/CMakeFiles/Progress/4 +0 -1
  131. package/android/src/main/build-arm64/CMakeFiles/Progress/5 +0 -1
  132. package/android/src/main/build-arm64/CMakeFiles/Progress/6 +0 -1
  133. package/android/src/main/build-arm64/CMakeFiles/Progress/count.txt +0 -1
  134. package/android/src/main/build-arm64/CMakeFiles/TargetDirectories.txt +0 -8
  135. package/android/src/main/build-arm64/CMakeFiles/cmake.check_cache +0 -1
  136. package/android/src/main/build-arm64/CMakeFiles/progress.marks +0 -1
  137. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-alloc.c.o +0 -0
  138. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-alloc.c.o.d +0 -58
  139. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-backend-reg.cpp.o +0 -0
  140. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-backend-reg.cpp.o.d +0 -756
  141. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-backend.cpp.o +0 -0
  142. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-backend.cpp.o.d +0 -709
  143. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-cpu-aarch64.cpp.o +0 -0
  144. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-cpu-aarch64.cpp.o.d +0 -714
  145. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-cpu-quants.c.o +0 -0
  146. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-cpu-quants.c.o.d +0 -62
  147. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-cpu-traits.cpp.o +0 -0
  148. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-cpu-traits.cpp.o.d +0 -708
  149. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-cpu.c.o +0 -0
  150. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-cpu.c.o.d +0 -113
  151. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-cpu.cpp.o +0 -0
  152. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-cpu.cpp.o.d +0 -713
  153. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-opt.cpp.o +0 -0
  154. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-opt.cpp.o.d +0 -763
  155. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-quants.c.o +0 -0
  156. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-quants.c.o.d +0 -61
  157. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-threading.cpp.o +0 -0
  158. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-threading.cpp.o.d +0 -707
  159. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml.c.o +0 -0
  160. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml.c.o.d +0 -104
  161. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/gguf.cpp.o +0 -0
  162. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/gguf.cpp.o.d +0 -714
  163. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/log.cpp.o +0 -0
  164. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/log.cpp.o.d +0 -723
  165. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/DependInfo.cmake +0 -62
  166. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/build.make +0 -722
  167. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/cmake_clean.cmake +0 -89
  168. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/compiler_depend.make +0 -2
  169. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/compiler_depend.ts +0 -2
  170. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/depend.make +0 -2
  171. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/flags.make +0 -17
  172. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/progress.make +0 -41
  173. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8.dir/DependInfo.cmake +0 -62
  174. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8.dir/build.make +0 -722
  175. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8.dir/cmake_clean.cmake +0 -89
  176. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8.dir/compiler_depend.make +0 -2
  177. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8.dir/compiler_depend.ts +0 -2
  178. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8.dir/depend.make +0 -2
  179. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8.dir/flags.make +0 -17
  180. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8.dir/progress.make +0 -41
  181. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2.dir/DependInfo.cmake +0 -62
  182. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2.dir/build.make +0 -722
  183. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2.dir/cmake_clean.cmake +0 -89
  184. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2.dir/compiler_depend.make +0 -2
  185. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2.dir/compiler_depend.ts +0 -2
  186. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2.dir/depend.make +0 -2
  187. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2.dir/flags.make +0 -17
  188. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2.dir/progress.make +0 -41
  189. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod.dir/DependInfo.cmake +0 -62
  190. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod.dir/build.make +0 -722
  191. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod.dir/cmake_clean.cmake +0 -89
  192. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod.dir/compiler_depend.make +0 -2
  193. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod.dir/compiler_depend.ts +0 -2
  194. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod.dir/depend.make +0 -2
  195. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod.dir/flags.make +0 -17
  196. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod.dir/progress.make +0 -41
  197. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod_i8mm.dir/DependInfo.cmake +0 -62
  198. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod_i8mm.dir/build.make +0 -722
  199. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod_i8mm.dir/cmake_clean.cmake +0 -89
  200. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod_i8mm.dir/compiler_depend.make +0 -2
  201. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod_i8mm.dir/compiler_depend.ts +0 -2
  202. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod_i8mm.dir/depend.make +0 -2
  203. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod_i8mm.dir/flags.make +0 -17
  204. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod_i8mm.dir/progress.make +0 -41
  205. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_i8mm.dir/DependInfo.cmake +0 -62
  206. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_i8mm.dir/build.make +0 -722
  207. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_i8mm.dir/cmake_clean.cmake +0 -89
  208. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_i8mm.dir/compiler_depend.make +0 -2
  209. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_i8mm.dir/compiler_depend.ts +0 -2
  210. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_i8mm.dir/depend.make +0 -2
  211. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_i8mm.dir/flags.make +0 -17
  212. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_i8mm.dir/progress.make +0 -41
  213. package/android/src/main/build-arm64/Makefile +0 -1862
  214. package/android/src/main/build-arm64/cmake_install.cmake +0 -66
  215. package/cpp/chat.hpp +0 -55
  216. package/cpp/rn-llama.hpp +0 -913
package/cpp/log.cpp CHANGED
@@ -1,423 +1,427 @@
1
- #include "log.h"
2
-
3
- #include <chrono>
4
- #include <condition_variable>
5
- #include <cstdarg>
6
- #include <cstdio>
7
- #include <mutex>
8
- #include <sstream>
9
- #include <thread>
10
- #include <vector>
11
-
12
- #if defined(__ANDROID__) && defined(RNLLAMA_ANDROID_ENABLE_LOGGING)
13
- #include <android/log.h>
14
- #endif
15
-
16
- int common_log_verbosity_thold = LOG_DEFAULT_LLAMA;
17
-
18
- void common_log_set_verbosity_thold(int verbosity) {
19
- common_log_verbosity_thold = verbosity;
20
- }
21
-
22
- static int64_t t_us() {
23
- return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
24
- }
25
-
26
- // colors
27
- enum common_log_col : int {
28
- COMMON_LOG_COL_DEFAULT = 0,
29
- COMMON_LOG_COL_BOLD,
30
- COMMON_LOG_COL_RED,
31
- COMMON_LOG_COL_GREEN,
32
- COMMON_LOG_COL_YELLOW,
33
- COMMON_LOG_COL_BLUE,
34
- COMMON_LOG_COL_MAGENTA,
35
- COMMON_LOG_COL_CYAN,
36
- COMMON_LOG_COL_WHITE,
37
- };
38
-
39
- // disable colors by default
40
- static std::vector<const char *> g_col = {
41
- "",
42
- "",
43
- "",
44
- "",
45
- "",
46
- "",
47
- "",
48
- "",
49
- "",
50
- };
51
-
52
- struct common_log_entry {
53
- enum lm_ggml_log_level level;
54
-
55
- bool prefix;
56
-
57
- int64_t timestamp;
58
-
59
- std::vector<char> msg;
60
-
61
- // signals the worker thread to stop
62
- bool is_end;
63
-
64
- #if defined(__ANDROID__) && defined(RNLLAMA_ANDROID_ENABLE_LOGGING)
65
- void android_print() const {
66
- int android_log_priority;
67
- switch (level) {
68
- case LM_GGML_LOG_LEVEL_INFO:
69
- android_log_priority = ANDROID_LOG_INFO;
70
- break;
71
- case LM_GGML_LOG_LEVEL_WARN:
72
- android_log_priority = ANDROID_LOG_WARN;
73
- break;
74
- case LM_GGML_LOG_LEVEL_ERROR:
75
- android_log_priority = ANDROID_LOG_ERROR;
76
- break;
77
- case LM_GGML_LOG_LEVEL_DEBUG:
78
- android_log_priority = ANDROID_LOG_DEBUG;
79
- break;
80
- default:
81
- android_log_priority = ANDROID_LOG_DEFAULT;
82
- break;
83
- }
84
-
85
- const char * tag = "RNLLAMA_LOG_ANDROID";
86
- __android_log_print(android_log_priority, tag, "%s", msg.data());
87
- }
88
- #endif
89
-
90
- void print(FILE * file = nullptr) const {
91
- FILE * fcur = file;
92
- if (!fcur) {
93
- // stderr displays DBG messages only when their verbosity level is not higher than the threshold
94
- // these messages will still be logged to a file
95
- if (level == LM_GGML_LOG_LEVEL_DEBUG && common_log_verbosity_thold < LOG_DEFAULT_DEBUG) {
96
- return;
97
- }
98
-
99
- fcur = stdout;
100
-
101
- if (level != LM_GGML_LOG_LEVEL_NONE) {
102
- fcur = stderr;
103
- }
104
- }
105
-
106
- if (level != LM_GGML_LOG_LEVEL_NONE && level != LM_GGML_LOG_LEVEL_CONT && prefix) {
107
- if (timestamp) {
108
- // [M.s.ms.us]
109
- fprintf(fcur, "%s%d.%02d.%03d.%03d%s ",
110
- g_col[COMMON_LOG_COL_BLUE],
111
- (int) (timestamp / 1000000 / 60),
112
- (int) (timestamp / 1000000 % 60),
113
- (int) (timestamp / 1000 % 1000),
114
- (int) (timestamp % 1000),
115
- g_col[COMMON_LOG_COL_DEFAULT]);
116
- }
117
-
118
- switch (level) {
119
- case LM_GGML_LOG_LEVEL_INFO: fprintf(fcur, "%sI %s", g_col[COMMON_LOG_COL_GREEN], g_col[COMMON_LOG_COL_DEFAULT]); break;
120
- case LM_GGML_LOG_LEVEL_WARN: fprintf(fcur, "%sW %s", g_col[COMMON_LOG_COL_MAGENTA], "" ); break;
121
- case LM_GGML_LOG_LEVEL_ERROR: fprintf(fcur, "%sE %s", g_col[COMMON_LOG_COL_RED], "" ); break;
122
- case LM_GGML_LOG_LEVEL_DEBUG: fprintf(fcur, "%sD %s", g_col[COMMON_LOG_COL_YELLOW], "" ); break;
123
- default:
124
- break;
125
- }
126
- }
127
-
128
- fprintf(fcur, "%s", msg.data());
129
-
130
- if (level == LM_GGML_LOG_LEVEL_WARN || level == LM_GGML_LOG_LEVEL_ERROR || level == LM_GGML_LOG_LEVEL_DEBUG) {
131
- fprintf(fcur, "%s", g_col[COMMON_LOG_COL_DEFAULT]);
132
- }
133
-
134
- fflush(fcur);
135
- }
136
- };
137
-
138
- struct common_log {
139
- // default capacity - will be expanded if needed
140
- common_log() : common_log(256) {}
141
-
142
- common_log(size_t capacity) {
143
- file = nullptr;
144
- prefix = false;
145
- timestamps = false;
146
- running = false;
147
- t_start = t_us();
148
-
149
- // initial message size - will be expanded if longer messages arrive
150
- entries.resize(capacity);
151
- for (auto & entry : entries) {
152
- entry.msg.resize(256);
153
- }
154
-
155
- head = 0;
156
- tail = 0;
157
-
158
- resume();
159
- }
160
-
161
- ~common_log() {
162
- pause();
163
- if (file) {
164
- fclose(file);
165
- }
166
- }
167
-
168
- private:
169
- std::mutex mtx;
170
- std::thread thrd;
171
- std::condition_variable cv;
172
-
173
- FILE * file;
174
-
175
- bool prefix;
176
- bool timestamps;
177
- bool running;
178
-
179
- int64_t t_start;
180
-
181
- // ring buffer of entries
182
- std::vector<common_log_entry> entries;
183
- size_t head;
184
- size_t tail;
185
-
186
- // worker thread copies into this
187
- common_log_entry cur;
188
-
189
- public:
190
- void add(enum lm_ggml_log_level level, const char * fmt, va_list args) {
191
- std::lock_guard<std::mutex> lock(mtx);
192
-
193
- if (!running) {
194
- // discard messages while the worker thread is paused
195
- return;
196
- }
197
-
198
- auto & entry = entries[tail];
199
-
200
- {
201
- // cannot use args twice, so make a copy in case we need to expand the buffer
202
- va_list args_copy;
203
- va_copy(args_copy, args);
204
-
205
- #if 1
206
- const size_t n = vsnprintf(entry.msg.data(), entry.msg.size(), fmt, args);
207
- if (n >= entry.msg.size()) {
208
- entry.msg.resize(n + 1);
209
- vsnprintf(entry.msg.data(), entry.msg.size(), fmt, args_copy);
210
- }
211
- #else
212
- // hack for bolding arguments
213
-
214
- std::stringstream ss;
215
- for (int i = 0; fmt[i] != 0; i++) {
216
- if (fmt[i] == '%') {
217
- ss << LOG_COL_BOLD;
218
- while (fmt[i] != ' ' && fmt[i] != ')' && fmt[i] != ']' && fmt[i] != 0) ss << fmt[i++];
219
- ss << LOG_COL_DEFAULT;
220
- if (fmt[i] == 0) break;
221
- }
222
- ss << fmt[i];
223
- }
224
- const size_t n = vsnprintf(entry.msg.data(), entry.msg.size(), ss.str().c_str(), args);
225
- if (n >= entry.msg.size()) {
226
- entry.msg.resize(n + 1);
227
- vsnprintf(entry.msg.data(), entry.msg.size(), ss.str().c_str(), args_copy);
228
- }
229
- #endif
230
- va_end(args_copy);
231
- }
232
-
233
- entry.level = level;
234
- entry.prefix = prefix;
235
- entry.timestamp = 0;
236
- if (timestamps) {
237
- entry.timestamp = t_us() - t_start;
238
- }
239
- entry.is_end = false;
240
-
241
- tail = (tail + 1) % entries.size();
242
- if (tail == head) {
243
- // expand the buffer
244
- std::vector<common_log_entry> new_entries(2*entries.size());
245
-
246
- size_t new_tail = 0;
247
-
248
- do {
249
- new_entries[new_tail] = std::move(entries[head]);
250
-
251
- head = (head + 1) % entries.size();
252
- new_tail = (new_tail + 1);
253
- } while (head != tail);
254
-
255
- head = 0;
256
- tail = new_tail;
257
-
258
- for (size_t i = tail; i < new_entries.size(); i++) {
259
- new_entries[i].msg.resize(256);
260
- }
261
-
262
- entries = std::move(new_entries);
263
- }
264
-
265
- cv.notify_one();
266
- }
267
-
268
- void resume() {
269
- std::lock_guard<std::mutex> lock(mtx);
270
-
271
- if (running) {
272
- return;
273
- }
274
-
275
- running = true;
276
-
277
- thrd = std::thread([this]() {
278
- while (true) {
279
- {
280
- std::unique_lock<std::mutex> lock(mtx);
281
- cv.wait(lock, [this]() { return head != tail; });
282
-
283
- cur = entries[head];
284
-
285
- head = (head + 1) % entries.size();
286
- }
287
-
288
- if (cur.is_end) {
289
- break;
290
- }
291
-
292
- cur.print(); // stdout and stderr
293
-
294
- if (file) {
295
- cur.print(file);
296
- }
297
- }
298
- });
299
- }
300
-
301
- void pause() {
302
- {
303
- std::lock_guard<std::mutex> lock(mtx);
304
-
305
- if (!running) {
306
- return;
307
- }
308
-
309
- running = false;
310
-
311
- // push an entry to signal the worker thread to stop
312
- {
313
- auto & entry = entries[tail];
314
- entry.is_end = true;
315
-
316
- tail = (tail + 1) % entries.size();
317
- }
318
-
319
- cv.notify_one();
320
- }
321
-
322
- thrd.join();
323
- }
324
-
325
- void set_file(const char * path) {
326
- pause();
327
-
328
- if (file) {
329
- fclose(file);
330
- }
331
-
332
- if (path) {
333
- file = fopen(path, "w");
334
- } else {
335
- file = nullptr;
336
- }
337
-
338
- resume();
339
- }
340
-
341
- void set_colors(bool colors) {
342
- pause();
343
-
344
- if (colors) {
345
- g_col[COMMON_LOG_COL_DEFAULT] = LOG_COL_DEFAULT;
346
- g_col[COMMON_LOG_COL_BOLD] = LOG_COL_BOLD;
347
- g_col[COMMON_LOG_COL_RED] = LOG_COL_RED;
348
- g_col[COMMON_LOG_COL_GREEN] = LOG_COL_GREEN;
349
- g_col[COMMON_LOG_COL_YELLOW] = LOG_COL_YELLOW;
350
- g_col[COMMON_LOG_COL_BLUE] = LOG_COL_BLUE;
351
- g_col[COMMON_LOG_COL_MAGENTA] = LOG_COL_MAGENTA;
352
- g_col[COMMON_LOG_COL_CYAN] = LOG_COL_CYAN;
353
- g_col[COMMON_LOG_COL_WHITE] = LOG_COL_WHITE;
354
- } else {
355
- for (size_t i = 0; i < g_col.size(); i++) {
356
- g_col[i] = "";
357
- }
358
- }
359
-
360
- resume();
361
- }
362
-
363
- void set_prefix(bool prefix) {
364
- std::lock_guard<std::mutex> lock(mtx);
365
-
366
- this->prefix = prefix;
367
- }
368
-
369
- void set_timestamps(bool timestamps) {
370
- std::lock_guard<std::mutex> lock(mtx);
371
-
372
- this->timestamps = timestamps;
373
- }
374
- };
375
-
376
- //
377
- // public API
378
- //
379
-
380
- struct common_log * common_log_init() {
381
- return new common_log;
382
- }
383
-
384
- struct common_log * common_log_main() {
385
- static struct common_log log;
386
-
387
- return &log;
388
- }
389
-
390
- void common_log_pause(struct common_log * log) {
391
- log->pause();
392
- }
393
-
394
- void common_log_resume(struct common_log * log) {
395
- log->resume();
396
- }
397
-
398
- void common_log_free(struct common_log * log) {
399
- delete log;
400
- }
401
-
402
- void common_log_add(struct common_log * log, enum lm_ggml_log_level level, const char * fmt, ...) {
403
- va_list args;
404
- va_start(args, fmt);
405
- log->add(level, fmt, args);
406
- va_end(args);
407
- }
408
-
409
- void common_log_set_file(struct common_log * log, const char * file) {
410
- log->set_file(file);
411
- }
412
-
413
- void common_log_set_colors(struct common_log * log, bool colors) {
414
- log->set_colors(colors);
415
- }
416
-
417
- void common_log_set_prefix(struct common_log * log, bool prefix) {
418
- log->set_prefix(prefix);
419
- }
420
-
421
- void common_log_set_timestamps(struct common_log * log, bool timestamps) {
422
- log->set_timestamps(timestamps);
423
- }
1
+ #include "log.h"
2
+
3
+ #include <chrono>
4
+ #include <condition_variable>
5
+ #include <cstdarg>
6
+ #include <cstdio>
7
+ #include <mutex>
8
+ #include <sstream>
9
+ #include <thread>
10
+ #include <vector>
11
+
12
+ #if defined(__ANDROID__) && defined(RNLLAMA_ANDROID_ENABLE_LOGGING)
13
+ #include <android/log.h>
14
+ #endif
15
+
16
+ int common_log_verbosity_thold = LOG_DEFAULT_LLAMA;
17
+
18
+ void common_log_set_verbosity_thold(int verbosity) {
19
+ common_log_verbosity_thold = verbosity;
20
+ }
21
+
22
+ static int64_t t_us() {
23
+ return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
24
+ }
25
+
26
+ // colors
27
+ enum common_log_col : int {
28
+ COMMON_LOG_COL_DEFAULT = 0,
29
+ COMMON_LOG_COL_BOLD,
30
+ COMMON_LOG_COL_RED,
31
+ COMMON_LOG_COL_GREEN,
32
+ COMMON_LOG_COL_YELLOW,
33
+ COMMON_LOG_COL_BLUE,
34
+ COMMON_LOG_COL_MAGENTA,
35
+ COMMON_LOG_COL_CYAN,
36
+ COMMON_LOG_COL_WHITE,
37
+ };
38
+
39
+ // disable colors by default
40
+ static std::vector<const char *> g_col = {
41
+ "",
42
+ "",
43
+ "",
44
+ "",
45
+ "",
46
+ "",
47
+ "",
48
+ "",
49
+ "",
50
+ };
51
+
52
+ struct common_log_entry {
53
+ enum lm_ggml_log_level level;
54
+
55
+ bool prefix;
56
+
57
+ int64_t timestamp;
58
+
59
+ std::vector<char> msg;
60
+
61
+ // signals the worker thread to stop
62
+ bool is_end;
63
+
64
+ #if defined(__ANDROID__) && defined(RNLLAMA_ANDROID_ENABLE_LOGGING)
65
+ void android_print() const {
66
+ int android_log_priority;
67
+ switch (level) {
68
+ case LM_GGML_LOG_LEVEL_INFO:
69
+ android_log_priority = ANDROID_LOG_INFO;
70
+ break;
71
+ case LM_GGML_LOG_LEVEL_WARN:
72
+ android_log_priority = ANDROID_LOG_WARN;
73
+ break;
74
+ case LM_GGML_LOG_LEVEL_ERROR:
75
+ android_log_priority = ANDROID_LOG_ERROR;
76
+ break;
77
+ case LM_GGML_LOG_LEVEL_DEBUG:
78
+ android_log_priority = ANDROID_LOG_DEBUG;
79
+ break;
80
+ default:
81
+ android_log_priority = ANDROID_LOG_DEFAULT;
82
+ break;
83
+ }
84
+
85
+ const char * tag = "RNLLAMA_LOG_ANDROID";
86
+ __android_log_print(android_log_priority, tag, "%s", msg.data());
87
+ }
88
+ #endif
89
+
90
+ void print(FILE * file = nullptr) const {
91
+ #if defined(__ANDROID__) && defined(RNLLAMA_ANDROID_ENABLE_LOGGING)
92
+ android_print();
93
+ #else
94
+ FILE * fcur = file;
95
+ if (!fcur) {
96
+ // stderr displays DBG messages only when their verbosity level is not higher than the threshold
97
+ // these messages will still be logged to a file
98
+ if (level == LM_GGML_LOG_LEVEL_DEBUG && common_log_verbosity_thold < LOG_DEFAULT_DEBUG) {
99
+ return;
100
+ }
101
+
102
+ fcur = stdout;
103
+
104
+ if (level != LM_GGML_LOG_LEVEL_NONE) {
105
+ fcur = stderr;
106
+ }
107
+ }
108
+
109
+ if (level != LM_GGML_LOG_LEVEL_NONE && level != LM_GGML_LOG_LEVEL_CONT && prefix) {
110
+ if (timestamp) {
111
+ // [M.s.ms.us]
112
+ fprintf(fcur, "%s%d.%02d.%03d.%03d%s ",
113
+ g_col[COMMON_LOG_COL_BLUE],
114
+ (int) (timestamp / 1000000 / 60),
115
+ (int) (timestamp / 1000000 % 60),
116
+ (int) (timestamp / 1000 % 1000),
117
+ (int) (timestamp % 1000),
118
+ g_col[COMMON_LOG_COL_DEFAULT]);
119
+ }
120
+
121
+ switch (level) {
122
+ case LM_GGML_LOG_LEVEL_INFO: fprintf(fcur, "%sI %s", g_col[COMMON_LOG_COL_GREEN], g_col[COMMON_LOG_COL_DEFAULT]); break;
123
+ case LM_GGML_LOG_LEVEL_WARN: fprintf(fcur, "%sW %s", g_col[COMMON_LOG_COL_MAGENTA], "" ); break;
124
+ case LM_GGML_LOG_LEVEL_ERROR: fprintf(fcur, "%sE %s", g_col[COMMON_LOG_COL_RED], "" ); break;
125
+ case LM_GGML_LOG_LEVEL_DEBUG: fprintf(fcur, "%sD %s", g_col[COMMON_LOG_COL_YELLOW], "" ); break;
126
+ default:
127
+ break;
128
+ }
129
+ }
130
+
131
+ fprintf(fcur, "%s", msg.data());
132
+
133
+ if (level == LM_GGML_LOG_LEVEL_WARN || level == LM_GGML_LOG_LEVEL_ERROR || level == LM_GGML_LOG_LEVEL_DEBUG) {
134
+ fprintf(fcur, "%s", g_col[COMMON_LOG_COL_DEFAULT]);
135
+ }
136
+
137
+ fflush(fcur);
138
+ #endif
139
+ }
140
+ };
141
+
142
+ struct common_log {
143
+ // default capacity - will be expanded if needed
144
+ common_log() : common_log(256) {}
145
+
146
+ common_log(size_t capacity) {
147
+ file = nullptr;
148
+ prefix = false;
149
+ timestamps = false;
150
+ running = false;
151
+ t_start = t_us();
152
+
153
+ // initial message size - will be expanded if longer messages arrive
154
+ entries.resize(capacity);
155
+ for (auto & entry : entries) {
156
+ entry.msg.resize(256);
157
+ }
158
+
159
+ head = 0;
160
+ tail = 0;
161
+
162
+ resume();
163
+ }
164
+
165
+ ~common_log() {
166
+ pause();
167
+ if (file) {
168
+ fclose(file);
169
+ }
170
+ }
171
+
172
+ private:
173
+ std::mutex mtx;
174
+ std::thread thrd;
175
+ std::condition_variable cv;
176
+
177
+ FILE * file;
178
+
179
+ bool prefix;
180
+ bool timestamps;
181
+ bool running;
182
+
183
+ int64_t t_start;
184
+
185
+ // ring buffer of entries
186
+ std::vector<common_log_entry> entries;
187
+ size_t head;
188
+ size_t tail;
189
+
190
+ // worker thread copies into this
191
+ common_log_entry cur;
192
+
193
+ public:
194
+ void add(enum lm_ggml_log_level level, const char * fmt, va_list args) {
195
+ std::lock_guard<std::mutex> lock(mtx);
196
+
197
+ if (!running) {
198
+ // discard messages while the worker thread is paused
199
+ return;
200
+ }
201
+
202
+ auto & entry = entries[tail];
203
+
204
+ {
205
+ // cannot use args twice, so make a copy in case we need to expand the buffer
206
+ va_list args_copy;
207
+ va_copy(args_copy, args);
208
+
209
+ #if 1
210
+ const size_t n = vsnprintf(entry.msg.data(), entry.msg.size(), fmt, args);
211
+ if (n >= entry.msg.size()) {
212
+ entry.msg.resize(n + 1);
213
+ vsnprintf(entry.msg.data(), entry.msg.size(), fmt, args_copy);
214
+ }
215
+ #else
216
+ // hack for bolding arguments
217
+
218
+ std::stringstream ss;
219
+ for (int i = 0; fmt[i] != 0; i++) {
220
+ if (fmt[i] == '%') {
221
+ ss << LOG_COL_BOLD;
222
+ while (fmt[i] != ' ' && fmt[i] != ')' && fmt[i] != ']' && fmt[i] != 0) ss << fmt[i++];
223
+ ss << LOG_COL_DEFAULT;
224
+ if (fmt[i] == 0) break;
225
+ }
226
+ ss << fmt[i];
227
+ }
228
+ const size_t n = vsnprintf(entry.msg.data(), entry.msg.size(), ss.str().c_str(), args);
229
+ if (n >= entry.msg.size()) {
230
+ entry.msg.resize(n + 1);
231
+ vsnprintf(entry.msg.data(), entry.msg.size(), ss.str().c_str(), args_copy);
232
+ }
233
+ #endif
234
+ va_end(args_copy);
235
+ }
236
+
237
+ entry.level = level;
238
+ entry.prefix = prefix;
239
+ entry.timestamp = 0;
240
+ if (timestamps) {
241
+ entry.timestamp = t_us() - t_start;
242
+ }
243
+ entry.is_end = false;
244
+
245
+ tail = (tail + 1) % entries.size();
246
+ if (tail == head) {
247
+ // expand the buffer
248
+ std::vector<common_log_entry> new_entries(2*entries.size());
249
+
250
+ size_t new_tail = 0;
251
+
252
+ do {
253
+ new_entries[new_tail] = std::move(entries[head]);
254
+
255
+ head = (head + 1) % entries.size();
256
+ new_tail = (new_tail + 1);
257
+ } while (head != tail);
258
+
259
+ head = 0;
260
+ tail = new_tail;
261
+
262
+ for (size_t i = tail; i < new_entries.size(); i++) {
263
+ new_entries[i].msg.resize(256);
264
+ }
265
+
266
+ entries = std::move(new_entries);
267
+ }
268
+
269
+ cv.notify_one();
270
+ }
271
+
272
+ void resume() {
273
+ std::lock_guard<std::mutex> lock(mtx);
274
+
275
+ if (running) {
276
+ return;
277
+ }
278
+
279
+ running = true;
280
+
281
+ thrd = std::thread([this]() {
282
+ while (true) {
283
+ {
284
+ std::unique_lock<std::mutex> lock(mtx);
285
+ cv.wait(lock, [this]() { return head != tail; });
286
+
287
+ cur = entries[head];
288
+
289
+ head = (head + 1) % entries.size();
290
+ }
291
+
292
+ if (cur.is_end) {
293
+ break;
294
+ }
295
+
296
+ cur.print(); // stdout and stderr
297
+
298
+ if (file) {
299
+ cur.print(file);
300
+ }
301
+ }
302
+ });
303
+ }
304
+
305
+ void pause() {
306
+ {
307
+ std::lock_guard<std::mutex> lock(mtx);
308
+
309
+ if (!running) {
310
+ return;
311
+ }
312
+
313
+ running = false;
314
+
315
+ // push an entry to signal the worker thread to stop
316
+ {
317
+ auto & entry = entries[tail];
318
+ entry.is_end = true;
319
+
320
+ tail = (tail + 1) % entries.size();
321
+ }
322
+
323
+ cv.notify_one();
324
+ }
325
+
326
+ thrd.join();
327
+ }
328
+
329
+ void set_file(const char * path) {
330
+ pause();
331
+
332
+ if (file) {
333
+ fclose(file);
334
+ }
335
+
336
+ if (path) {
337
+ file = fopen(path, "w");
338
+ } else {
339
+ file = nullptr;
340
+ }
341
+
342
+ resume();
343
+ }
344
+
345
+ void set_colors(bool colors) {
346
+ pause();
347
+
348
+ if (colors) {
349
+ g_col[COMMON_LOG_COL_DEFAULT] = LOG_COL_DEFAULT;
350
+ g_col[COMMON_LOG_COL_BOLD] = LOG_COL_BOLD;
351
+ g_col[COMMON_LOG_COL_RED] = LOG_COL_RED;
352
+ g_col[COMMON_LOG_COL_GREEN] = LOG_COL_GREEN;
353
+ g_col[COMMON_LOG_COL_YELLOW] = LOG_COL_YELLOW;
354
+ g_col[COMMON_LOG_COL_BLUE] = LOG_COL_BLUE;
355
+ g_col[COMMON_LOG_COL_MAGENTA] = LOG_COL_MAGENTA;
356
+ g_col[COMMON_LOG_COL_CYAN] = LOG_COL_CYAN;
357
+ g_col[COMMON_LOG_COL_WHITE] = LOG_COL_WHITE;
358
+ } else {
359
+ for (size_t i = 0; i < g_col.size(); i++) {
360
+ g_col[i] = "";
361
+ }
362
+ }
363
+
364
+ resume();
365
+ }
366
+
367
+ void set_prefix(bool prefix) {
368
+ std::lock_guard<std::mutex> lock(mtx);
369
+
370
+ this->prefix = prefix;
371
+ }
372
+
373
+ void set_timestamps(bool timestamps) {
374
+ std::lock_guard<std::mutex> lock(mtx);
375
+
376
+ this->timestamps = timestamps;
377
+ }
378
+ };
379
+
380
+ //
381
+ // public API
382
+ //
383
+
384
+ struct common_log * common_log_init() {
385
+ return new common_log;
386
+ }
387
+
388
+ struct common_log * common_log_main() {
389
+ static struct common_log log;
390
+
391
+ return &log;
392
+ }
393
+
394
+ void common_log_pause(struct common_log * log) {
395
+ log->pause();
396
+ }
397
+
398
+ void common_log_resume(struct common_log * log) {
399
+ log->resume();
400
+ }
401
+
402
+ void common_log_free(struct common_log * log) {
403
+ delete log;
404
+ }
405
+
406
+ void common_log_add(struct common_log * log, enum lm_ggml_log_level level, const char * fmt, ...) {
407
+ va_list args;
408
+ va_start(args, fmt);
409
+ log->add(level, fmt, args);
410
+ va_end(args);
411
+ }
412
+
413
+ void common_log_set_file(struct common_log * log, const char * file) {
414
+ log->set_file(file);
415
+ }
416
+
417
+ void common_log_set_colors(struct common_log * log, bool colors) {
418
+ log->set_colors(colors);
419
+ }
420
+
421
+ void common_log_set_prefix(struct common_log * log, bool prefix) {
422
+ log->set_prefix(prefix);
423
+ }
424
+
425
+ void common_log_set_timestamps(struct common_log * log, bool timestamps) {
426
+ log->set_timestamps(timestamps);
427
+ }