cui-llama.rn 0.2.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 (76) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +330 -0
  3. package/android/build.gradle +107 -0
  4. package/android/gradle.properties +5 -0
  5. package/android/src/main/AndroidManifest.xml +4 -0
  6. package/android/src/main/CMakeLists.txt +69 -0
  7. package/android/src/main/java/com/rnllama/LlamaContext.java +353 -0
  8. package/android/src/main/java/com/rnllama/RNLlama.java +446 -0
  9. package/android/src/main/java/com/rnllama/RNLlamaPackage.java +48 -0
  10. package/android/src/main/jni.cpp +635 -0
  11. package/android/src/newarch/java/com/rnllama/RNLlamaModule.java +94 -0
  12. package/android/src/oldarch/java/com/rnllama/RNLlamaModule.java +95 -0
  13. package/cpp/README.md +4 -0
  14. package/cpp/common.cpp +3237 -0
  15. package/cpp/common.h +467 -0
  16. package/cpp/ggml-aarch64.c +2193 -0
  17. package/cpp/ggml-aarch64.h +39 -0
  18. package/cpp/ggml-alloc.c +1041 -0
  19. package/cpp/ggml-alloc.h +76 -0
  20. package/cpp/ggml-backend-impl.h +153 -0
  21. package/cpp/ggml-backend.c +2225 -0
  22. package/cpp/ggml-backend.h +236 -0
  23. package/cpp/ggml-common.h +1829 -0
  24. package/cpp/ggml-impl.h +655 -0
  25. package/cpp/ggml-metal.h +65 -0
  26. package/cpp/ggml-metal.m +3273 -0
  27. package/cpp/ggml-quants.c +15022 -0
  28. package/cpp/ggml-quants.h +132 -0
  29. package/cpp/ggml.c +22034 -0
  30. package/cpp/ggml.h +2444 -0
  31. package/cpp/grammar-parser.cpp +536 -0
  32. package/cpp/grammar-parser.h +29 -0
  33. package/cpp/json-schema-to-grammar.cpp +1045 -0
  34. package/cpp/json-schema-to-grammar.h +8 -0
  35. package/cpp/json.hpp +24766 -0
  36. package/cpp/llama.cpp +21789 -0
  37. package/cpp/llama.h +1201 -0
  38. package/cpp/log.h +737 -0
  39. package/cpp/rn-llama.hpp +630 -0
  40. package/cpp/sampling.cpp +460 -0
  41. package/cpp/sampling.h +160 -0
  42. package/cpp/sgemm.cpp +1027 -0
  43. package/cpp/sgemm.h +14 -0
  44. package/cpp/unicode-data.cpp +7032 -0
  45. package/cpp/unicode-data.h +20 -0
  46. package/cpp/unicode.cpp +812 -0
  47. package/cpp/unicode.h +64 -0
  48. package/ios/RNLlama.h +11 -0
  49. package/ios/RNLlama.mm +302 -0
  50. package/ios/RNLlama.xcodeproj/project.pbxproj +278 -0
  51. package/ios/RNLlamaContext.h +39 -0
  52. package/ios/RNLlamaContext.mm +426 -0
  53. package/jest/mock.js +169 -0
  54. package/lib/commonjs/NativeRNLlama.js +10 -0
  55. package/lib/commonjs/NativeRNLlama.js.map +1 -0
  56. package/lib/commonjs/grammar.js +574 -0
  57. package/lib/commonjs/grammar.js.map +1 -0
  58. package/lib/commonjs/index.js +151 -0
  59. package/lib/commonjs/index.js.map +1 -0
  60. package/lib/module/NativeRNLlama.js +3 -0
  61. package/lib/module/NativeRNLlama.js.map +1 -0
  62. package/lib/module/grammar.js +566 -0
  63. package/lib/module/grammar.js.map +1 -0
  64. package/lib/module/index.js +129 -0
  65. package/lib/module/index.js.map +1 -0
  66. package/lib/typescript/NativeRNLlama.d.ts +107 -0
  67. package/lib/typescript/NativeRNLlama.d.ts.map +1 -0
  68. package/lib/typescript/grammar.d.ts +38 -0
  69. package/lib/typescript/grammar.d.ts.map +1 -0
  70. package/lib/typescript/index.d.ts +46 -0
  71. package/lib/typescript/index.d.ts.map +1 -0
  72. package/llama-rn.podspec +56 -0
  73. package/package.json +230 -0
  74. package/src/NativeRNLlama.ts +132 -0
  75. package/src/grammar.ts +849 -0
  76. package/src/index.ts +182 -0
package/cpp/log.h ADDED
@@ -0,0 +1,737 @@
1
+ #pragma once
2
+
3
+ #include <chrono>
4
+ #include <cstring>
5
+ #include <sstream>
6
+ #include <iostream>
7
+ #include <thread>
8
+ #include <vector>
9
+ #include <algorithm>
10
+ #include <cinttypes>
11
+
12
+ // --------------------------------
13
+ //
14
+ // Basic usage:
15
+ //
16
+ // --------
17
+ //
18
+ // The LOG() and LOG_TEE() macros are ready to go by default
19
+ // they do not require any initialization.
20
+ //
21
+ // LOGLN() and LOG_TEELN() are variants which automatically
22
+ // include \n character at the end of the log string.
23
+ //
24
+ // LOG() behaves exactly like printf, by default writing to a logfile.
25
+ // LOG_TEE() additionally, prints to the screen too ( mimics Unix tee command ).
26
+ //
27
+ // Default logfile is named
28
+ // "llama.<threadID>.log"
29
+ // Default LOG_TEE() secondary output target is
30
+ // stderr
31
+ //
32
+ // Logs can be dynamically disabled or enabled using functions:
33
+ // log_disable()
34
+ // and
35
+ // log_enable()
36
+ //
37
+ // A log target can be changed with:
38
+ // log_set_target( string )
39
+ // creating and opening, or re-opening a file by string filename
40
+ // or
41
+ // log_set_target( FILE* )
42
+ // allowing to point at stderr, stdout, or any valid FILE* file handler.
43
+ //
44
+ // --------
45
+ //
46
+ // End of Basic usage.
47
+ //
48
+ // --------------------------------
49
+
50
+ // Specifies a log target.
51
+ // default uses log_handler() with "llama.log" log file
52
+ // this can be changed, by defining LOG_TARGET
53
+ // like so:
54
+ //
55
+ // #define LOG_TARGET (a valid FILE*)
56
+ // #include "log.h"
57
+ //
58
+ // or it can be simply redirected to stdout or stderr
59
+ // like so:
60
+ //
61
+ // #define LOG_TARGET stderr
62
+ // #include "log.h"
63
+ //
64
+ // The log target can also be redirected to a different function
65
+ // like so:
66
+ //
67
+ // #define LOG_TARGET log_handler_different()
68
+ // #include "log.h"
69
+ //
70
+ // FILE* log_handler_different()
71
+ // {
72
+ // return stderr;
73
+ // }
74
+ //
75
+ // or:
76
+ //
77
+ // #define LOG_TARGET log_handler_another_one("somelog.log")
78
+ // #include "log.h"
79
+ //
80
+ // FILE* log_handler_another_one(char*filename)
81
+ // {
82
+ // static FILE* logfile = nullptr;
83
+ // (...)
84
+ // if( !logfile )
85
+ // {
86
+ // fopen(...)
87
+ // }
88
+ // (...)
89
+ // return logfile
90
+ // }
91
+ //
92
+ #ifndef LOG_TARGET
93
+ #define LOG_TARGET log_handler()
94
+ #endif
95
+
96
+ #ifndef LOG_TEE_TARGET
97
+ #define LOG_TEE_TARGET stderr
98
+ #endif
99
+
100
+ // Utility for synchronizing log configuration state
101
+ // since std::optional was introduced only in c++17
102
+ enum LogTriState
103
+ {
104
+ LogTriStateSame,
105
+ LogTriStateFalse,
106
+ LogTriStateTrue
107
+ };
108
+
109
+ // Utility to obtain "pid" like unique process id and use it when creating log files.
110
+ inline std::string log_get_pid()
111
+ {
112
+ static std::string pid;
113
+ if (pid.empty())
114
+ {
115
+ // std::this_thread::get_id() is the most portable way of obtaining a "process id"
116
+ // it's not the same as "pid" but is unique enough to solve multiple instances
117
+ // trying to write to the same log.
118
+ std::stringstream ss;
119
+ ss << std::this_thread::get_id();
120
+ pid = ss.str();
121
+ }
122
+
123
+ return pid;
124
+ }
125
+
126
+ // Utility function for generating log file names with unique id based on thread id.
127
+ // invocation with log_filename_generator( "llama", "log" ) creates a string "llama.<number>.log"
128
+ // where the number is a runtime id of the current thread.
129
+
130
+ #define log_filename_generator(log_file_basename, log_file_extension) log_filename_generator_impl(LogTriStateSame, log_file_basename, log_file_extension)
131
+
132
+ // INTERNAL, DO NOT USE
133
+ inline std::string log_filename_generator_impl(LogTriState multilog, const std::string & log_file_basename, const std::string & log_file_extension)
134
+ {
135
+ static bool _multilog = false;
136
+
137
+ if (multilog != LogTriStateSame)
138
+ {
139
+ _multilog = multilog == LogTriStateTrue;
140
+ }
141
+
142
+ std::stringstream buf;
143
+
144
+ buf << log_file_basename;
145
+ if (_multilog)
146
+ {
147
+ buf << ".";
148
+ buf << log_get_pid();
149
+ }
150
+ buf << ".";
151
+ buf << log_file_extension;
152
+
153
+ return buf.str();
154
+ }
155
+
156
+ #ifndef LOG_DEFAULT_FILE_NAME
157
+ #define LOG_DEFAULT_FILE_NAME log_filename_generator("llama", "log")
158
+ #endif
159
+
160
+ // Utility for turning #define values into string literals
161
+ // so we can have a define for stderr and
162
+ // we can print "stderr" instead of literal stderr, etc.
163
+ #define LOG_STRINGIZE1(s) #s
164
+ #define LOG_STRINGIZE(s) LOG_STRINGIZE1(s)
165
+
166
+ #define LOG_TEE_TARGET_STRING LOG_STRINGIZE(LOG_TEE_TARGET)
167
+
168
+ // Allows disabling timestamps.
169
+ // in order to disable, define LOG_NO_TIMESTAMPS
170
+ // like so:
171
+ //
172
+ // #define LOG_NO_TIMESTAMPS
173
+ // #include "log.h"
174
+ //
175
+ #ifndef LOG_NO_TIMESTAMPS
176
+ #ifndef _MSC_VER
177
+ #define LOG_TIMESTAMP_FMT "[%" PRIu64 "] "
178
+ #define LOG_TIMESTAMP_VAL , (std::chrono::duration_cast<std::chrono::duration<std::uint64_t>>(std::chrono::system_clock::now().time_since_epoch())).count()
179
+ #else
180
+ #define LOG_TIMESTAMP_FMT "[%" PRIu64 "] "
181
+ #define LOG_TIMESTAMP_VAL , (std::chrono::duration_cast<std::chrono::duration<std::uint64_t>>(std::chrono::system_clock::now().time_since_epoch())).count()
182
+ #endif
183
+ #else
184
+ #define LOG_TIMESTAMP_FMT "%s"
185
+ #define LOG_TIMESTAMP_VAL ,""
186
+ #endif
187
+
188
+ #ifdef LOG_TEE_TIMESTAMPS
189
+ #ifndef _MSC_VER
190
+ #define LOG_TEE_TIMESTAMP_FMT "[%" PRIu64 "] "
191
+ #define LOG_TEE_TIMESTAMP_VAL , (std::chrono::duration_cast<std::chrono::duration<std::uint64_t>>(std::chrono::system_clock::now().time_since_epoch())).count()
192
+ #else
193
+ #define LOG_TEE_TIMESTAMP_FMT "[%" PRIu64 "] "
194
+ #define LOG_TEE_TIMESTAMP_VAL , (std::chrono::duration_cast<std::chrono::duration<std::uint64_t>>(std::chrono::system_clock::now().time_since_epoch())).count()
195
+ #endif
196
+ #else
197
+ #define LOG_TEE_TIMESTAMP_FMT "%s"
198
+ #define LOG_TEE_TIMESTAMP_VAL ,""
199
+ #endif
200
+
201
+ // Allows disabling file/line/function prefix
202
+ // in order to disable, define LOG_NO_FILE_LINE_FUNCTION
203
+ // like so:
204
+ //
205
+ // #define LOG_NO_FILE_LINE_FUNCTION
206
+ // #include "log.h"
207
+ //
208
+ #ifndef LOG_NO_FILE_LINE_FUNCTION
209
+ #ifndef _MSC_VER
210
+ #define LOG_FLF_FMT "[%24s:%5d][%24s] "
211
+ #define LOG_FLF_VAL , __FILE__, __LINE__, __FUNCTION__
212
+ #else
213
+ #define LOG_FLF_FMT "[%24s:%5ld][%24s] "
214
+ #define LOG_FLF_VAL , __FILE__, (long)__LINE__, __FUNCTION__
215
+ #endif
216
+ #else
217
+ #define LOG_FLF_FMT "%s"
218
+ #define LOG_FLF_VAL ,""
219
+ #endif
220
+
221
+ #ifdef LOG_TEE_FILE_LINE_FUNCTION
222
+ #ifndef _MSC_VER
223
+ #define LOG_TEE_FLF_FMT "[%24s:%5d][%24s] "
224
+ #define LOG_TEE_FLF_VAL , __FILE__, __LINE__, __FUNCTION__
225
+ #else
226
+ #define LOG_TEE_FLF_FMT "[%24s:%5ld][%24s] "
227
+ #define LOG_TEE_FLF_VAL , __FILE__, (long)__LINE__, __FUNCTION__
228
+ #endif
229
+ #else
230
+ #define LOG_TEE_FLF_FMT "%s"
231
+ #define LOG_TEE_FLF_VAL ,""
232
+ #endif
233
+
234
+ // INTERNAL, DO NOT USE
235
+ // USE LOG() INSTEAD
236
+ //
237
+ #if !defined(_MSC_VER) || defined(__INTEL_LLVM_COMPILER) || defined(__clang__)
238
+ #define LOG_IMPL(str, ...) \
239
+ do { \
240
+ if (LOG_TARGET != nullptr) \
241
+ { \
242
+ fprintf(LOG_TARGET, LOG_TIMESTAMP_FMT LOG_FLF_FMT str "%s" LOG_TIMESTAMP_VAL LOG_FLF_VAL, __VA_ARGS__); \
243
+ fflush(LOG_TARGET); \
244
+ } \
245
+ } while (0)
246
+ #else
247
+ #define LOG_IMPL(str, ...) \
248
+ do { \
249
+ if (LOG_TARGET != nullptr) \
250
+ { \
251
+ fprintf(LOG_TARGET, LOG_TIMESTAMP_FMT LOG_FLF_FMT str "%s" LOG_TIMESTAMP_VAL LOG_FLF_VAL "", ##__VA_ARGS__); \
252
+ fflush(LOG_TARGET); \
253
+ } \
254
+ } while (0)
255
+ #endif
256
+
257
+ // INTERNAL, DO NOT USE
258
+ // USE LOG_TEE() INSTEAD
259
+ //
260
+ #if !defined(_MSC_VER) || defined(__INTEL_LLVM_COMPILER) || defined(__clang__)
261
+ #define LOG_TEE_IMPL(str, ...) \
262
+ do { \
263
+ if (LOG_TARGET != nullptr) \
264
+ { \
265
+ fprintf(LOG_TARGET, LOG_TIMESTAMP_FMT LOG_FLF_FMT str "%s" LOG_TIMESTAMP_VAL LOG_FLF_VAL, __VA_ARGS__); \
266
+ fflush(LOG_TARGET); \
267
+ } \
268
+ if (LOG_TARGET != nullptr && LOG_TARGET != stdout && LOG_TARGET != stderr && LOG_TEE_TARGET != nullptr) \
269
+ { \
270
+ fprintf(LOG_TEE_TARGET, LOG_TEE_TIMESTAMP_FMT LOG_TEE_FLF_FMT str "%s" LOG_TEE_TIMESTAMP_VAL LOG_TEE_FLF_VAL, __VA_ARGS__); \
271
+ fflush(LOG_TEE_TARGET); \
272
+ } \
273
+ } while (0)
274
+ #else
275
+ #define LOG_TEE_IMPL(str, ...) \
276
+ do { \
277
+ if (LOG_TARGET != nullptr) \
278
+ { \
279
+ fprintf(LOG_TARGET, LOG_TIMESTAMP_FMT LOG_FLF_FMT str "%s" LOG_TIMESTAMP_VAL LOG_FLF_VAL "", ##__VA_ARGS__); \
280
+ fflush(LOG_TARGET); \
281
+ } \
282
+ if (LOG_TARGET != nullptr && LOG_TARGET != stdout && LOG_TARGET != stderr && LOG_TEE_TARGET != nullptr) \
283
+ { \
284
+ fprintf(LOG_TEE_TARGET, LOG_TEE_TIMESTAMP_FMT LOG_TEE_FLF_FMT str "%s" LOG_TEE_TIMESTAMP_VAL LOG_TEE_FLF_VAL "", ##__VA_ARGS__); \
285
+ fflush(LOG_TEE_TARGET); \
286
+ } \
287
+ } while (0)
288
+ #endif
289
+
290
+ // The '\0' as a last argument, is a trick to bypass the silly
291
+ // "warning: ISO C++11 requires at least one argument for the "..." in a variadic macro"
292
+ // so we can have a single macro which can be called just like printf.
293
+
294
+ // Main LOG macro.
295
+ // behaves like printf, and supports arguments the exact same way.
296
+ //
297
+ #if !defined(_MSC_VER) || defined(__clang__)
298
+ #define LOG(...) LOG_IMPL(__VA_ARGS__, "")
299
+ #else
300
+ #define LOG(str, ...) LOG_IMPL("%s" str, "", ##__VA_ARGS__, "")
301
+ #endif
302
+
303
+ // Main TEE macro.
304
+ // does the same as LOG
305
+ // and
306
+ // simultaneously writes stderr.
307
+ //
308
+ // Secondary target can be changed just like LOG_TARGET
309
+ // by defining LOG_TEE_TARGET
310
+ //
311
+ #if !defined(_MSC_VER) || defined(__clang__)
312
+ #define LOG_TEE(...) LOG_TEE_IMPL(__VA_ARGS__, "")
313
+ #else
314
+ #define LOG_TEE(str, ...) LOG_TEE_IMPL("%s" str, "", ##__VA_ARGS__, "")
315
+ #endif
316
+
317
+ // LOG macro variants with auto endline.
318
+ #if !defined(_MSC_VER) || defined(__clang__)
319
+ #define LOGLN(...) LOG_IMPL(__VA_ARGS__, "\n")
320
+ #define LOG_TEELN(...) LOG_TEE_IMPL(__VA_ARGS__, "\n")
321
+ #else
322
+ #define LOGLN(str, ...) LOG_IMPL("%s" str, "", ##__VA_ARGS__, "\n")
323
+ #define LOG_TEELN(str, ...) LOG_TEE_IMPL("%s" str, "", ##__VA_ARGS__, "\n")
324
+ #endif
325
+
326
+ #if defined(__ANDROID__) && defined(RNLLAMA_ANDROID_ENABLE_LOGGING)
327
+ #include <android/log.h>
328
+ #define LLAMA_ANDROID_LOG_TAG "RNLLAMA_LOG_ANDROID"
329
+ #undef LOG
330
+ #undef LOG_TEE
331
+ #undef LOGLN
332
+ #undef LOG_TEELN
333
+ #define LOG(...) __android_log_print(ANDROID_LOG_INFO, LLAMA_ANDROID_LOG_TAG, __VA_ARGS__)
334
+ #define LOG_TEE(...) __android_log_print(ANDROID_LOG_INFO, LLAMA_ANDROID_LOG_TAG, __VA_ARGS__)
335
+ #define LOGLN(...) __android_log_print(ANDROID_LOG_INFO, LLAMA_ANDROID_LOG_TAG, __VA_ARGS__)
336
+ #define LOG_TEELN(...) __android_log_print(ANDROID_LOG_INFO, LLAMA_ANDROID_LOG_TAG, __VA_ARGS__)
337
+ #endif
338
+
339
+ // INTERNAL, DO NOT USE
340
+ inline FILE *log_handler1_impl(bool change = false, LogTriState append = LogTriStateSame, LogTriState disable = LogTriStateSame, const std::string & filename = LOG_DEFAULT_FILE_NAME, FILE *target = nullptr)
341
+ {
342
+ static bool _initialized = false;
343
+ static bool _append = false;
344
+ static bool _disabled = filename.empty() && target == nullptr;
345
+ static std::string log_current_filename{filename};
346
+ static FILE *log_current_target{target};
347
+ static FILE *logfile = nullptr;
348
+
349
+ if (change)
350
+ {
351
+ if (append != LogTriStateSame)
352
+ {
353
+ _append = append == LogTriStateTrue;
354
+ return logfile;
355
+ }
356
+
357
+ if (disable == LogTriStateTrue)
358
+ {
359
+ // Disable primary target
360
+ _disabled = true;
361
+ }
362
+ // If previously disabled, only enable, and keep previous target
363
+ else if (disable == LogTriStateFalse)
364
+ {
365
+ _disabled = false;
366
+ }
367
+ // Otherwise, process the arguments
368
+ else if (log_current_filename != filename || log_current_target != target)
369
+ {
370
+ _initialized = false;
371
+ }
372
+ }
373
+
374
+ if (_disabled)
375
+ {
376
+ // Log is disabled
377
+ return nullptr;
378
+ }
379
+
380
+ if (_initialized)
381
+ {
382
+ // with fallback in case something went wrong
383
+ return logfile ? logfile : stderr;
384
+ }
385
+
386
+ // do the (re)initialization
387
+ if (target != nullptr)
388
+ {
389
+ if (logfile != nullptr && logfile != stdout && logfile != stderr)
390
+ {
391
+ fclose(logfile);
392
+ }
393
+
394
+ log_current_filename = LOG_DEFAULT_FILE_NAME;
395
+ log_current_target = target;
396
+
397
+ logfile = target;
398
+ }
399
+ else
400
+ {
401
+ if (log_current_filename != filename)
402
+ {
403
+ if (logfile != nullptr && logfile != stdout && logfile != stderr)
404
+ {
405
+ fclose(logfile);
406
+ }
407
+ }
408
+
409
+ logfile = fopen(filename.c_str(), _append ? "a" : "w");
410
+ }
411
+
412
+ if (!logfile)
413
+ {
414
+ // Verify whether the file was opened, otherwise fallback to stderr
415
+ logfile = stderr;
416
+
417
+ fprintf(stderr, "Failed to open logfile '%s' with error '%s'\n", filename.c_str(), std::strerror(errno));
418
+ fflush(stderr);
419
+
420
+ // At this point we let the init flag be to true below, and let the target fallback to stderr
421
+ // otherwise we would repeatedly fopen() which was already unsuccessful
422
+ }
423
+
424
+ _initialized = true;
425
+
426
+ return logfile ? logfile : stderr;
427
+ }
428
+
429
+ // INTERNAL, DO NOT USE
430
+ inline FILE *log_handler2_impl(bool change = false, LogTriState append = LogTriStateSame, LogTriState disable = LogTriStateSame, FILE *target = nullptr, const std::string & filename = LOG_DEFAULT_FILE_NAME)
431
+ {
432
+ return log_handler1_impl(change, append, disable, filename, target);
433
+ }
434
+
435
+ // Disables logs entirely at runtime.
436
+ // Makes LOG() and LOG_TEE() produce no output,
437
+ // until enabled back.
438
+ #define log_disable() log_disable_impl()
439
+
440
+ // INTERNAL, DO NOT USE
441
+ inline FILE *log_disable_impl()
442
+ {
443
+ return log_handler1_impl(true, LogTriStateSame, LogTriStateTrue);
444
+ }
445
+
446
+ // Enables logs at runtime.
447
+ #define log_enable() log_enable_impl()
448
+
449
+ // INTERNAL, DO NOT USE
450
+ inline FILE *log_enable_impl()
451
+ {
452
+ return log_handler1_impl(true, LogTriStateSame, LogTriStateFalse);
453
+ }
454
+
455
+ // Sets target fir logs, either by a file name or FILE* pointer (stdout, stderr, or any valid FILE*)
456
+ #define log_set_target(target) log_set_target_impl(target)
457
+
458
+ // INTERNAL, DO NOT USE
459
+ inline FILE *log_set_target_impl(const std::string & filename) { return log_handler1_impl(true, LogTriStateSame, LogTriStateSame, filename); }
460
+ inline FILE *log_set_target_impl(FILE *target) { return log_handler2_impl(true, LogTriStateSame, LogTriStateSame, target); }
461
+
462
+ // INTERNAL, DO NOT USE
463
+ inline FILE *log_handler() { return log_handler1_impl(); }
464
+
465
+ // Enable or disable creating separate log files for each run.
466
+ // can ONLY be invoked BEFORE first log use.
467
+ #define log_multilog(enable) log_filename_generator_impl((enable) ? LogTriStateTrue : LogTriStateFalse, "", "")
468
+ // Enable or disable append mode for log file.
469
+ // can ONLY be invoked BEFORE first log use.
470
+ #define log_append(enable) log_append_impl(enable)
471
+ // INTERNAL, DO NOT USE
472
+ inline FILE *log_append_impl(bool enable)
473
+ {
474
+ return log_handler1_impl(true, enable ? LogTriStateTrue : LogTriStateFalse, LogTriStateSame);
475
+ }
476
+
477
+ inline void log_test()
478
+ {
479
+ log_disable();
480
+ LOG("01 Hello World to nobody, because logs are disabled!\n");
481
+ log_enable();
482
+ LOG("02 Hello World to default output, which is \"%s\" ( Yaaay, arguments! )!\n", LOG_STRINGIZE(LOG_TARGET));
483
+ LOG_TEE("03 Hello World to **both** default output and " LOG_TEE_TARGET_STRING "!\n");
484
+ log_set_target(stderr);
485
+ LOG("04 Hello World to stderr!\n");
486
+ LOG_TEE("05 Hello World TEE with double printing to stderr prevented!\n");
487
+ log_set_target(LOG_DEFAULT_FILE_NAME);
488
+ LOG("06 Hello World to default log file!\n");
489
+ log_set_target(stdout);
490
+ LOG("07 Hello World to stdout!\n");
491
+ log_set_target(LOG_DEFAULT_FILE_NAME);
492
+ LOG("08 Hello World to default log file again!\n");
493
+ log_disable();
494
+ LOG("09 Hello World _1_ into the void!\n");
495
+ log_enable();
496
+ LOG("10 Hello World back from the void ( you should not see _1_ in the log or the output )!\n");
497
+ log_disable();
498
+ log_set_target("llama.anotherlog.log");
499
+ LOG("11 Hello World _2_ to nobody, new target was selected but logs are still disabled!\n");
500
+ log_enable();
501
+ LOG("12 Hello World this time in a new file ( you should not see _2_ in the log or the output )?\n");
502
+ log_set_target("llama.yetanotherlog.log");
503
+ LOG("13 Hello World this time in yet new file?\n");
504
+ log_set_target(log_filename_generator("llama_autonamed", "log"));
505
+ LOG("14 Hello World in log with generated filename!\n");
506
+ #ifdef _MSC_VER
507
+ LOG_TEE("15 Hello msvc TEE without arguments\n");
508
+ LOG_TEE("16 Hello msvc TEE with (%d)(%s) arguments\n", 1, "test");
509
+ LOG_TEELN("17 Hello msvc TEELN without arguments\n");
510
+ LOG_TEELN("18 Hello msvc TEELN with (%d)(%s) arguments\n", 1, "test");
511
+ LOG("19 Hello msvc LOG without arguments\n");
512
+ LOG("20 Hello msvc LOG with (%d)(%s) arguments\n", 1, "test");
513
+ LOGLN("21 Hello msvc LOGLN without arguments\n");
514
+ LOGLN("22 Hello msvc LOGLN with (%d)(%s) arguments\n", 1, "test");
515
+ #endif
516
+ }
517
+
518
+ inline bool log_param_single_parse(const std::string & param)
519
+ {
520
+ if ( param == "--log-test")
521
+ {
522
+ log_test();
523
+ return true;
524
+ }
525
+
526
+ if ( param == "--log-disable")
527
+ {
528
+ log_disable();
529
+ return true;
530
+ }
531
+
532
+ if ( param == "--log-enable")
533
+ {
534
+ log_enable();
535
+ return true;
536
+ }
537
+
538
+ if (param == "--log-new")
539
+ {
540
+ log_multilog(true);
541
+ return true;
542
+ }
543
+
544
+ if (param == "--log-append")
545
+ {
546
+ log_append(true);
547
+ return true;
548
+ }
549
+
550
+ return false;
551
+ }
552
+
553
+ inline bool log_param_pair_parse(bool check_but_dont_parse, const std::string & param, const std::string & next = std::string())
554
+ {
555
+ if ( param == "--log-file")
556
+ {
557
+ if (!check_but_dont_parse)
558
+ {
559
+ log_set_target(log_filename_generator(next.empty() ? "unnamed" : next, "log"));
560
+ }
561
+
562
+ return true;
563
+ }
564
+
565
+ return false;
566
+ }
567
+
568
+ inline void log_print_usage()
569
+ {
570
+ printf("log options:\n");
571
+ /* format
572
+ printf(" -h, --help show this help message and exit\n");*/
573
+ /* spacing
574
+ printf("__-param----------------Description\n");*/
575
+ printf(" --log-test Run simple logging test\n");
576
+ printf(" --log-disable Disable trace logs\n");
577
+ printf(" --log-enable Enable trace logs\n");
578
+ printf(" --log-file Specify a log filename (without extension)\n");
579
+ printf(" --log-new Create a separate new log file on start. "
580
+ "Each log file will have unique name: \"<name>.<ID>.log\"\n");
581
+ printf(" --log-append Don't truncate the old log file.\n");
582
+ printf("\n");
583
+ }
584
+
585
+ #define log_dump_cmdline(argc, argv) log_dump_cmdline_impl(argc, argv)
586
+
587
+ // INTERNAL, DO NOT USE
588
+ inline void log_dump_cmdline_impl(int argc, char **argv)
589
+ {
590
+ std::stringstream buf;
591
+ for (int i = 0; i < argc; ++i)
592
+ {
593
+ if (std::string(argv[i]).find(' ') != std::string::npos)
594
+ {
595
+ buf << " \"" << argv[i] <<"\"";
596
+ }
597
+ else
598
+ {
599
+ buf << " " << argv[i];
600
+ }
601
+ }
602
+ LOGLN("Cmd:%s", buf.str().c_str());
603
+ }
604
+
605
+ #define log_tostr(var) log_var_to_string_impl(var).c_str()
606
+
607
+ inline std::string log_var_to_string_impl(bool var)
608
+ {
609
+ return var ? "true" : "false";
610
+ }
611
+
612
+ inline std::string log_var_to_string_impl(std::string var)
613
+ {
614
+ return var;
615
+ }
616
+
617
+ inline std::string log_var_to_string_impl(const std::vector<int> & var)
618
+ {
619
+ std::stringstream buf;
620
+ buf << "[ ";
621
+ bool first = true;
622
+ for (auto e : var)
623
+ {
624
+ if (first)
625
+ {
626
+ first = false;
627
+ }
628
+ else
629
+ {
630
+ buf << ", ";
631
+ }
632
+ buf << std::to_string(e);
633
+ }
634
+ buf << " ]";
635
+
636
+ return buf.str();
637
+ }
638
+
639
+ template <typename C, typename T>
640
+ inline std::string LOG_TOKENS_TOSTR_PRETTY(const C & ctx, const T & tokens)
641
+ {
642
+ std::stringstream buf;
643
+ buf << "[ ";
644
+
645
+ bool first = true;
646
+ for (const auto & token : tokens)
647
+ {
648
+ if (!first) {
649
+ buf << ", ";
650
+ } else {
651
+ first = false;
652
+ }
653
+
654
+ auto detokenized = llama_token_to_piece(ctx, token);
655
+
656
+ detokenized.erase(
657
+ std::remove_if(
658
+ detokenized.begin(),
659
+ detokenized.end(),
660
+ [](const unsigned char c) { return !std::isprint(c); }),
661
+ detokenized.end());
662
+
663
+ buf
664
+ << "'" << detokenized << "'"
665
+ << ":" << std::to_string(token);
666
+ }
667
+ buf << " ]";
668
+
669
+ return buf.str();
670
+ }
671
+
672
+ template <typename C, typename B>
673
+ inline std::string LOG_BATCH_TOSTR_PRETTY(const C & ctx, const B & batch)
674
+ {
675
+ std::stringstream buf;
676
+ buf << "[ ";
677
+
678
+ bool first = true;
679
+ for (int i = 0; i < batch.n_tokens; ++i)
680
+ {
681
+ if (!first) {
682
+ buf << ", ";
683
+ } else {
684
+ first = false;
685
+ }
686
+
687
+ auto detokenized = llama_token_to_piece(ctx, batch.token[i]);
688
+
689
+ detokenized.erase(
690
+ std::remove_if(
691
+ detokenized.begin(),
692
+ detokenized.end(),
693
+ [](const unsigned char c) { return !std::isprint(c); }),
694
+ detokenized.end());
695
+
696
+ buf
697
+ << "\n" << std::to_string(i)
698
+ << ":token '" << detokenized << "'"
699
+ << ":pos " << std::to_string(batch.pos[i])
700
+ << ":n_seq_id " << std::to_string(batch.n_seq_id[i])
701
+ << ":seq_id " << std::to_string(batch.seq_id[i][0])
702
+ << ":logits " << std::to_string(batch.logits[i]);
703
+ }
704
+ buf << " ]";
705
+
706
+ return buf.str();
707
+ }
708
+
709
+ #ifdef LOG_DISABLE_LOGS
710
+
711
+ #undef LOG
712
+ #define LOG(...) // dummy stub
713
+ #undef LOGLN
714
+ #define LOGLN(...) // dummy stub
715
+
716
+ #undef LOG_TEE
717
+ #define LOG_TEE(...) fprintf(stderr, __VA_ARGS__) // convert to normal fprintf
718
+
719
+ #undef LOG_TEELN
720
+ #define LOG_TEELN(...) fprintf(stderr, __VA_ARGS__) // convert to normal fprintf
721
+
722
+ #undef LOG_DISABLE
723
+ #define LOG_DISABLE() // dummy stub
724
+
725
+ #undef LOG_ENABLE
726
+ #define LOG_ENABLE() // dummy stub
727
+
728
+ #undef LOG_ENABLE
729
+ #define LOG_ENABLE() // dummy stub
730
+
731
+ #undef LOG_SET_TARGET
732
+ #define LOG_SET_TARGET(...) // dummy stub
733
+
734
+ #undef LOG_DUMP_CMDLINE
735
+ #define LOG_DUMP_CMDLINE(...) // dummy stub
736
+
737
+ #endif // LOG_DISABLE_LOGS