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