llama-cpp-capacitor 0.0.6 → 0.0.7

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 (149) hide show
  1. package/cpp/LICENSE +21 -0
  2. package/cpp/README.md +4 -0
  3. package/cpp/anyascii.c +22223 -0
  4. package/cpp/anyascii.h +42 -0
  5. package/cpp/chat-parser.cpp +393 -0
  6. package/cpp/chat-parser.h +120 -0
  7. package/cpp/chat.cpp +2315 -0
  8. package/cpp/chat.h +221 -0
  9. package/cpp/common.cpp +1619 -0
  10. package/cpp/common.h +744 -0
  11. package/cpp/ggml-alloc.c +1028 -0
  12. package/cpp/ggml-alloc.h +76 -0
  13. package/cpp/ggml-backend-impl.h +255 -0
  14. package/cpp/ggml-backend-reg.cpp +600 -0
  15. package/cpp/ggml-backend.cpp +2118 -0
  16. package/cpp/ggml-backend.h +354 -0
  17. package/cpp/ggml-common.h +1878 -0
  18. package/cpp/ggml-cpp.h +39 -0
  19. package/cpp/ggml-cpu/amx/amx.cpp +221 -0
  20. package/cpp/ggml-cpu/amx/amx.h +8 -0
  21. package/cpp/ggml-cpu/amx/common.h +91 -0
  22. package/cpp/ggml-cpu/amx/mmq.cpp +2512 -0
  23. package/cpp/ggml-cpu/amx/mmq.h +10 -0
  24. package/cpp/ggml-cpu/arch/arm/cpu-feats.cpp +94 -0
  25. package/cpp/ggml-cpu/arch/arm/quants.c +3650 -0
  26. package/cpp/ggml-cpu/arch/arm/repack.cpp +1891 -0
  27. package/cpp/ggml-cpu/arch/x86/cpu-feats.cpp +327 -0
  28. package/cpp/ggml-cpu/arch/x86/quants.c +3820 -0
  29. package/cpp/ggml-cpu/arch/x86/repack.cpp +6307 -0
  30. package/cpp/ggml-cpu/arch-fallback.h +215 -0
  31. package/cpp/ggml-cpu/binary-ops.cpp +158 -0
  32. package/cpp/ggml-cpu/binary-ops.h +16 -0
  33. package/cpp/ggml-cpu/common.h +73 -0
  34. package/cpp/ggml-cpu/ggml-cpu-impl.h +525 -0
  35. package/cpp/ggml-cpu/ggml-cpu.c +3578 -0
  36. package/cpp/ggml-cpu/ggml-cpu.cpp +672 -0
  37. package/cpp/ggml-cpu/ops.cpp +10587 -0
  38. package/cpp/ggml-cpu/ops.h +114 -0
  39. package/cpp/ggml-cpu/quants.c +1193 -0
  40. package/cpp/ggml-cpu/quants.h +97 -0
  41. package/cpp/ggml-cpu/repack.cpp +1982 -0
  42. package/cpp/ggml-cpu/repack.h +120 -0
  43. package/cpp/ggml-cpu/simd-mappings.h +1184 -0
  44. package/cpp/ggml-cpu/traits.cpp +36 -0
  45. package/cpp/ggml-cpu/traits.h +38 -0
  46. package/cpp/ggml-cpu/unary-ops.cpp +186 -0
  47. package/cpp/ggml-cpu/unary-ops.h +28 -0
  48. package/cpp/ggml-cpu/vec.cpp +348 -0
  49. package/cpp/ggml-cpu/vec.h +1121 -0
  50. package/cpp/ggml-cpu.h +145 -0
  51. package/cpp/ggml-impl.h +622 -0
  52. package/cpp/ggml-metal-impl.h +688 -0
  53. package/cpp/ggml-metal.h +66 -0
  54. package/cpp/ggml-metal.m +6833 -0
  55. package/cpp/ggml-opt.cpp +1093 -0
  56. package/cpp/ggml-opt.h +256 -0
  57. package/cpp/ggml-quants.c +5324 -0
  58. package/cpp/ggml-quants.h +106 -0
  59. package/cpp/ggml-threading.cpp +12 -0
  60. package/cpp/ggml-threading.h +14 -0
  61. package/cpp/ggml.c +7108 -0
  62. package/cpp/ggml.h +2492 -0
  63. package/cpp/gguf.cpp +1358 -0
  64. package/cpp/gguf.h +202 -0
  65. package/cpp/json-partial.cpp +256 -0
  66. package/cpp/json-partial.h +38 -0
  67. package/cpp/json-schema-to-grammar.cpp +985 -0
  68. package/cpp/json-schema-to-grammar.h +21 -0
  69. package/cpp/llama-adapter.cpp +388 -0
  70. package/cpp/llama-adapter.h +76 -0
  71. package/cpp/llama-arch.cpp +2355 -0
  72. package/cpp/llama-arch.h +499 -0
  73. package/cpp/llama-batch.cpp +875 -0
  74. package/cpp/llama-batch.h +160 -0
  75. package/cpp/llama-chat.cpp +783 -0
  76. package/cpp/llama-chat.h +65 -0
  77. package/cpp/llama-context.cpp +2748 -0
  78. package/cpp/llama-context.h +306 -0
  79. package/cpp/llama-cparams.cpp +5 -0
  80. package/cpp/llama-cparams.h +41 -0
  81. package/cpp/llama-cpp.h +30 -0
  82. package/cpp/llama-grammar.cpp +1229 -0
  83. package/cpp/llama-grammar.h +173 -0
  84. package/cpp/llama-graph.cpp +1891 -0
  85. package/cpp/llama-graph.h +810 -0
  86. package/cpp/llama-hparams.cpp +180 -0
  87. package/cpp/llama-hparams.h +233 -0
  88. package/cpp/llama-impl.cpp +167 -0
  89. package/cpp/llama-impl.h +61 -0
  90. package/cpp/llama-io.cpp +15 -0
  91. package/cpp/llama-io.h +35 -0
  92. package/cpp/llama-kv-cache-iswa.cpp +318 -0
  93. package/cpp/llama-kv-cache-iswa.h +135 -0
  94. package/cpp/llama-kv-cache.cpp +2059 -0
  95. package/cpp/llama-kv-cache.h +374 -0
  96. package/cpp/llama-kv-cells.h +491 -0
  97. package/cpp/llama-memory-hybrid.cpp +258 -0
  98. package/cpp/llama-memory-hybrid.h +137 -0
  99. package/cpp/llama-memory-recurrent.cpp +1146 -0
  100. package/cpp/llama-memory-recurrent.h +179 -0
  101. package/cpp/llama-memory.cpp +59 -0
  102. package/cpp/llama-memory.h +119 -0
  103. package/cpp/llama-mmap.cpp +600 -0
  104. package/cpp/llama-mmap.h +68 -0
  105. package/cpp/llama-model-loader.cpp +1164 -0
  106. package/cpp/llama-model-loader.h +170 -0
  107. package/cpp/llama-model-saver.cpp +282 -0
  108. package/cpp/llama-model-saver.h +37 -0
  109. package/cpp/llama-model.cpp +19042 -0
  110. package/cpp/llama-model.h +491 -0
  111. package/cpp/llama-sampling.cpp +2575 -0
  112. package/cpp/llama-sampling.h +32 -0
  113. package/cpp/llama-vocab.cpp +3792 -0
  114. package/cpp/llama-vocab.h +176 -0
  115. package/cpp/llama.cpp +358 -0
  116. package/cpp/llama.h +1373 -0
  117. package/cpp/log.cpp +427 -0
  118. package/cpp/log.h +103 -0
  119. package/cpp/minja/chat-template.hpp +550 -0
  120. package/cpp/minja/minja.hpp +3009 -0
  121. package/cpp/nlohmann/json.hpp +25526 -0
  122. package/cpp/nlohmann/json_fwd.hpp +187 -0
  123. package/cpp/regex-partial.cpp +204 -0
  124. package/cpp/regex-partial.h +56 -0
  125. package/cpp/rn-completion.cpp +681 -0
  126. package/cpp/rn-completion.h +116 -0
  127. package/cpp/rn-llama.cpp +345 -0
  128. package/cpp/rn-llama.h +149 -0
  129. package/cpp/rn-mtmd.hpp +602 -0
  130. package/cpp/rn-tts.cpp +591 -0
  131. package/cpp/rn-tts.h +59 -0
  132. package/cpp/sampling.cpp +579 -0
  133. package/cpp/sampling.h +107 -0
  134. package/cpp/tools/mtmd/clip-impl.h +473 -0
  135. package/cpp/tools/mtmd/clip.cpp +4322 -0
  136. package/cpp/tools/mtmd/clip.h +106 -0
  137. package/cpp/tools/mtmd/miniaudio/miniaudio.h +93468 -0
  138. package/cpp/tools/mtmd/mtmd-audio.cpp +769 -0
  139. package/cpp/tools/mtmd/mtmd-audio.h +47 -0
  140. package/cpp/tools/mtmd/mtmd-helper.cpp +460 -0
  141. package/cpp/tools/mtmd/mtmd-helper.h +91 -0
  142. package/cpp/tools/mtmd/mtmd.cpp +1066 -0
  143. package/cpp/tools/mtmd/mtmd.h +298 -0
  144. package/cpp/tools/mtmd/stb/stb_image.h +7988 -0
  145. package/cpp/unicode-data.cpp +7034 -0
  146. package/cpp/unicode-data.h +20 -0
  147. package/cpp/unicode.cpp +1061 -0
  148. package/cpp/unicode.h +68 -0
  149. package/package.json +2 -1
package/cpp/log.cpp ADDED
@@ -0,0 +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
+ #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
+ }
package/cpp/log.h ADDED
@@ -0,0 +1,103 @@
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
+ #define LOG_TMPL(level, verbosity, ...) \
84
+ do { \
85
+ if ((verbosity) <= common_log_verbosity_thold) { \
86
+ common_log_add(common_log_main(), (level), __VA_ARGS__); \
87
+ } \
88
+ } while (0)
89
+
90
+ #define LOG(...) LOG_TMPL(LM_GGML_LOG_LEVEL_NONE, 0, __VA_ARGS__)
91
+ #define LOGV(verbosity, ...) LOG_TMPL(LM_GGML_LOG_LEVEL_NONE, verbosity, __VA_ARGS__)
92
+
93
+ #define LOG_INF(...) LOG_TMPL(LM_GGML_LOG_LEVEL_INFO, 0, __VA_ARGS__)
94
+ #define LOG_WRN(...) LOG_TMPL(LM_GGML_LOG_LEVEL_WARN, 0, __VA_ARGS__)
95
+ #define LOG_ERR(...) LOG_TMPL(LM_GGML_LOG_LEVEL_ERROR, 0, __VA_ARGS__)
96
+ #define LOG_DBG(...) LOG_TMPL(LM_GGML_LOG_LEVEL_DEBUG, LOG_DEFAULT_DEBUG, __VA_ARGS__)
97
+ #define LOG_CNT(...) LOG_TMPL(LM_GGML_LOG_LEVEL_CONT, 0, __VA_ARGS__)
98
+
99
+ #define LOG_INFV(verbosity, ...) LOG_TMPL(LM_GGML_LOG_LEVEL_INFO, verbosity, __VA_ARGS__)
100
+ #define LOG_WRNV(verbosity, ...) LOG_TMPL(LM_GGML_LOG_LEVEL_WARN, verbosity, __VA_ARGS__)
101
+ #define LOG_ERRV(verbosity, ...) LOG_TMPL(LM_GGML_LOG_LEVEL_ERROR, verbosity, __VA_ARGS__)
102
+ #define LOG_DBGV(verbosity, ...) LOG_TMPL(LM_GGML_LOG_LEVEL_DEBUG, verbosity, __VA_ARGS__)
103
+ #define LOG_CNTV(verbosity, ...) LOG_TMPL(LM_GGML_LOG_LEVEL_CONT, verbosity, __VA_ARGS__)