@fugood/llama.node 0.3.1 → 0.3.3
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.
- package/CMakeLists.txt +1 -8
- package/bin/darwin/arm64/llama-node.node +0 -0
- package/bin/darwin/x64/llama-node.node +0 -0
- package/bin/linux/arm64/llama-node.node +0 -0
- package/bin/linux/x64/llama-node.node +0 -0
- package/bin/linux-vulkan/arm64/llama-node.node +0 -0
- package/bin/linux-vulkan/x64/llama-node.node +0 -0
- package/bin/win32/arm64/llama-node.node +0 -0
- package/bin/win32/arm64/node.lib +0 -0
- package/bin/win32/x64/llama-node.node +0 -0
- package/bin/win32/x64/node.lib +0 -0
- package/bin/win32-vulkan/arm64/llama-node.node +0 -0
- package/bin/win32-vulkan/arm64/node.lib +0 -0
- package/bin/win32-vulkan/x64/llama-node.node +0 -0
- package/bin/win32-vulkan/x64/node.lib +0 -0
- package/package.json +4 -2
- package/src/DetokenizeWorker.cpp +1 -1
- package/src/EmbeddingWorker.cpp +2 -2
- package/src/LlamaCompletionWorker.cpp +10 -10
- package/src/LlamaCompletionWorker.h +2 -2
- package/src/LlamaContext.cpp +14 -17
- package/src/TokenizeWorker.cpp +1 -1
- package/src/common.hpp +5 -4
- package/src/llama.cpp/.github/workflows/build.yml +137 -29
- package/src/llama.cpp/.github/workflows/close-issue.yml +5 -0
- package/src/llama.cpp/.github/workflows/docker.yml +46 -34
- package/src/llama.cpp/.github/workflows/nix-ci-aarch64.yml +7 -0
- package/src/llama.cpp/.github/workflows/nix-ci.yml +7 -0
- package/src/llama.cpp/.github/workflows/python-check-requirements.yml +2 -4
- package/src/llama.cpp/.github/workflows/python-type-check.yml +3 -1
- package/src/llama.cpp/.github/workflows/server.yml +7 -0
- package/src/llama.cpp/CMakeLists.txt +26 -11
- package/src/llama.cpp/cmake/arm64-apple-clang.cmake +16 -0
- package/src/llama.cpp/common/CMakeLists.txt +10 -10
- package/src/llama.cpp/common/arg.cpp +2041 -0
- package/src/llama.cpp/common/arg.h +77 -0
- package/src/llama.cpp/common/common.cpp +523 -1861
- package/src/llama.cpp/common/common.h +234 -106
- package/src/llama.cpp/common/console.cpp +3 -0
- package/src/llama.cpp/common/json-schema-to-grammar.cpp +1 -1
- package/src/llama.cpp/common/log.cpp +401 -0
- package/src/llama.cpp/common/log.h +66 -698
- package/src/llama.cpp/common/ngram-cache.cpp +39 -36
- package/src/llama.cpp/common/ngram-cache.h +19 -19
- package/src/llama.cpp/common/sampling.cpp +356 -350
- package/src/llama.cpp/common/sampling.h +62 -139
- package/src/llama.cpp/common/stb_image.h +5990 -6398
- package/src/llama.cpp/docs/build.md +72 -17
- package/src/llama.cpp/examples/CMakeLists.txt +1 -2
- package/src/llama.cpp/examples/batched/batched.cpp +49 -65
- package/src/llama.cpp/examples/batched-bench/batched-bench.cpp +42 -53
- package/src/llama.cpp/examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp +55 -52
- package/src/llama.cpp/examples/cvector-generator/cvector-generator.cpp +22 -22
- package/src/llama.cpp/examples/cvector-generator/pca.hpp +3 -13
- package/src/llama.cpp/examples/embedding/embedding.cpp +147 -91
- package/src/llama.cpp/examples/eval-callback/eval-callback.cpp +37 -37
- package/src/llama.cpp/examples/export-lora/export-lora.cpp +39 -38
- package/src/llama.cpp/examples/gbnf-validator/gbnf-validator.cpp +14 -39
- package/src/llama.cpp/examples/{baby-llama → gen-docs}/CMakeLists.txt +2 -2
- package/src/llama.cpp/examples/gen-docs/gen-docs.cpp +83 -0
- package/src/llama.cpp/examples/gguf-split/gguf-split.cpp +58 -39
- package/src/llama.cpp/examples/gritlm/gritlm.cpp +46 -39
- package/src/llama.cpp/examples/imatrix/imatrix.cpp +75 -69
- package/src/llama.cpp/examples/infill/infill.cpp +131 -192
- package/src/llama.cpp/examples/llama-bench/llama-bench.cpp +276 -178
- package/src/llama.cpp/examples/llama.android/llama/build.gradle.kts +1 -0
- package/src/llama.cpp/examples/llama.android/llama/src/main/cpp/llama-android.cpp +40 -36
- package/src/llama.cpp/examples/llava/CMakeLists.txt +7 -0
- package/src/llama.cpp/examples/llava/clip.cpp +686 -150
- package/src/llama.cpp/examples/llava/clip.h +11 -2
- package/src/llama.cpp/examples/llava/llava-cli.cpp +60 -71
- package/src/llama.cpp/examples/llava/llava.cpp +146 -26
- package/src/llama.cpp/examples/llava/llava.h +2 -3
- package/src/llama.cpp/examples/llava/minicpmv-cli.cpp +323 -0
- package/src/llama.cpp/examples/llava/requirements.txt +1 -0
- package/src/llama.cpp/examples/lookahead/lookahead.cpp +55 -56
- package/src/llama.cpp/examples/lookup/lookup-create.cpp +15 -13
- package/src/llama.cpp/examples/lookup/lookup-merge.cpp +4 -4
- package/src/llama.cpp/examples/lookup/lookup-stats.cpp +34 -33
- package/src/llama.cpp/examples/lookup/lookup.cpp +60 -63
- package/src/llama.cpp/examples/main/main.cpp +216 -313
- package/src/llama.cpp/examples/parallel/parallel.cpp +58 -59
- package/src/llama.cpp/examples/passkey/passkey.cpp +53 -61
- package/src/llama.cpp/examples/perplexity/perplexity.cpp +277 -311
- package/src/llama.cpp/examples/quantize/CMakeLists.txt +1 -1
- package/src/llama.cpp/examples/quantize/quantize.cpp +27 -9
- package/src/llama.cpp/examples/quantize-stats/quantize-stats.cpp +12 -12
- package/src/llama.cpp/examples/retrieval/retrieval.cpp +57 -52
- package/src/llama.cpp/examples/rpc/rpc-server.cpp +27 -2
- package/src/llama.cpp/examples/save-load-state/save-load-state.cpp +60 -46
- package/src/llama.cpp/examples/server/CMakeLists.txt +7 -18
- package/src/llama.cpp/examples/server/server.cpp +1347 -1531
- package/src/llama.cpp/examples/server/tests/requirements.txt +2 -1
- package/src/llama.cpp/examples/server/utils.hpp +396 -107
- package/src/llama.cpp/examples/simple/CMakeLists.txt +1 -1
- package/src/llama.cpp/examples/simple/simple.cpp +132 -106
- package/src/llama.cpp/examples/simple-chat/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/simple-chat/simple-chat.cpp +197 -0
- package/src/llama.cpp/examples/speculative/speculative.cpp +153 -124
- package/src/llama.cpp/examples/sycl/run-llama2.sh +10 -19
- package/src/llama.cpp/examples/sycl/win-run-llama2.bat +1 -1
- package/src/llama.cpp/examples/tokenize/tokenize.cpp +27 -29
- package/src/llama.cpp/ggml/CMakeLists.txt +29 -12
- package/src/llama.cpp/ggml/include/ggml-alloc.h +3 -3
- package/src/llama.cpp/ggml/include/ggml-amx.h +25 -0
- package/src/llama.cpp/ggml/include/ggml-backend.h +166 -68
- package/src/llama.cpp/ggml/include/ggml-blas.h +5 -3
- package/src/llama.cpp/ggml/include/ggml-cann.h +17 -19
- package/src/llama.cpp/ggml/include/ggml-cpp.h +38 -0
- package/src/llama.cpp/ggml/include/ggml-cpu.h +177 -0
- package/src/llama.cpp/ggml/include/ggml-cuda.h +17 -17
- package/src/llama.cpp/ggml/include/ggml-kompute.h +7 -3
- package/src/llama.cpp/ggml/include/ggml-metal.h +13 -12
- package/src/llama.cpp/ggml/include/ggml-opt.h +216 -0
- package/src/llama.cpp/ggml/include/ggml-rpc.h +9 -5
- package/src/llama.cpp/ggml/include/ggml-sycl.h +18 -11
- package/src/llama.cpp/ggml/include/ggml-vulkan.h +10 -8
- package/src/llama.cpp/ggml/include/ggml.h +272 -505
- package/src/llama.cpp/ggml/src/CMakeLists.txt +69 -1110
- package/src/llama.cpp/ggml/src/ggml-aarch64.c +52 -2116
- package/src/llama.cpp/ggml/src/ggml-aarch64.h +0 -20
- package/src/llama.cpp/ggml/src/ggml-alloc.c +29 -27
- package/src/llama.cpp/ggml/src/ggml-amx/CMakeLists.txt +107 -0
- package/src/llama.cpp/ggml/src/ggml-amx/common.h +94 -0
- package/src/llama.cpp/ggml/src/ggml-amx/ggml-amx.cpp +446 -0
- package/src/llama.cpp/ggml/src/ggml-amx/mmq.cpp +2510 -0
- package/src/llama.cpp/ggml/src/ggml-amx/mmq.h +17 -0
- package/src/llama.cpp/ggml/src/ggml-backend-impl.h +144 -81
- package/src/llama.cpp/ggml/src/ggml-backend-reg.cpp +195 -0
- package/src/llama.cpp/ggml/src/{ggml-backend.c → ggml-backend.cpp} +394 -635
- package/src/llama.cpp/ggml/src/ggml-blas/CMakeLists.txt +91 -0
- package/src/llama.cpp/ggml/src/{ggml-blas.cpp → ggml-blas/ggml-blas.cpp} +217 -70
- package/src/llama.cpp/ggml/src/ggml-cann/CMakeLists.txt +46 -0
- package/src/llama.cpp/ggml/src/ggml-cann/acl_tensor.cpp +4 -27
- package/src/llama.cpp/ggml/src/ggml-cann/acl_tensor.h +32 -4
- package/src/llama.cpp/ggml/src/ggml-cann/aclnn_ops.cpp +179 -41
- package/src/llama.cpp/ggml/src/ggml-cann/common.h +1 -0
- package/src/llama.cpp/ggml/src/{ggml-cann.cpp → ggml-cann/ggml-cann.cpp} +458 -353
- package/src/llama.cpp/ggml/src/ggml-cann/kernels/CMakeLists.txt +2 -1
- package/src/llama.cpp/ggml/src/ggml-cann/kernels/ascendc_kernels.h +2 -0
- package/src/llama.cpp/ggml/src/ggml-cann/kernels/quantize_float_to_q4_0.cpp +278 -0
- package/src/llama.cpp/ggml/src/ggml-common.h +20 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/CMakeLists.txt +261 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-aarch64.c +3560 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-aarch64.h +30 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-impl.h +371 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-quants.c +10822 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-quants.h +63 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.c +13970 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.cpp +663 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/llamafile/sgemm.cpp +1885 -0
- package/src/llama.cpp/ggml/src/ggml-cuda/CMakeLists.txt +155 -0
- package/src/llama.cpp/ggml/src/ggml-cuda/vendors/cuda.h +14 -0
- package/src/llama.cpp/ggml/src/ggml-cuda/vendors/hip.h +178 -0
- package/src/llama.cpp/ggml/src/ggml-cuda/vendors/musa.h +134 -0
- package/src/llama.cpp/ggml/src/ggml-hip/CMakeLists.txt +106 -0
- package/src/llama.cpp/ggml/src/ggml-impl.h +380 -584
- package/src/llama.cpp/ggml/src/ggml-kompute/CMakeLists.txt +162 -0
- package/src/llama.cpp/ggml/src/{ggml-kompute.cpp → ggml-kompute/ggml-kompute.cpp} +233 -87
- package/src/llama.cpp/ggml/src/ggml-metal/CMakeLists.txt +108 -0
- package/src/llama.cpp/ggml/src/ggml-metal/ggml-metal-impl.h +249 -0
- package/src/llama.cpp/ggml/src/ggml-musa/CMakeLists.txt +100 -0
- package/src/llama.cpp/ggml/src/ggml-opt.cpp +867 -0
- package/src/llama.cpp/ggml/src/ggml-quants.c +369 -9994
- package/src/llama.cpp/ggml/src/ggml-quants.h +78 -110
- package/src/llama.cpp/ggml/src/ggml-rpc/CMakeLists.txt +11 -0
- package/src/llama.cpp/ggml/src/{ggml-rpc.cpp → ggml-rpc/ggml-rpc.cpp} +560 -335
- package/src/llama.cpp/ggml/src/ggml-sycl/CMakeLists.txt +81 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/backend.hpp +6 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/common.cpp +51 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/common.hpp +310 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/concat.cpp +1 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/conv.cpp +99 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/conv.hpp +21 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/convert.cpp +57 -57
- package/src/llama.cpp/ggml/src/ggml-sycl/convert.hpp +1 -1
- package/src/llama.cpp/ggml/src/ggml-sycl/dequantize.hpp +106 -106
- package/src/llama.cpp/ggml/src/ggml-sycl/dmmv.cpp +4 -4
- package/src/llama.cpp/ggml/src/ggml-sycl/dpct/helper.hpp +18 -25
- package/src/llama.cpp/ggml/src/ggml-sycl/element_wise.cpp +1011 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/element_wise.hpp +76 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/gemm.hpp +101 -0
- package/src/llama.cpp/ggml/src/{ggml-sycl.cpp → ggml-sycl/ggml-sycl.cpp} +3350 -3980
- package/src/llama.cpp/ggml/src/ggml-sycl/im2col.cpp +125 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/im2col.hpp +23 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/mmvq.cpp +70 -68
- package/src/llama.cpp/ggml/src/ggml-sycl/norm.cpp +9 -6
- package/src/llama.cpp/ggml/src/ggml-sycl/outprod.cpp +56 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/outprod.hpp +11 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/presets.hpp +8 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/rope.cpp +1 -1
- package/src/llama.cpp/ggml/src/ggml-sycl/tsembd.cpp +71 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/tsembd.hpp +21 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/vecdotq.hpp +4 -4
- package/src/llama.cpp/ggml/src/ggml-sycl/wkv6.cpp +138 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/wkv6.hpp +10 -0
- package/src/llama.cpp/ggml/src/ggml-threading.cpp +12 -0
- package/src/llama.cpp/ggml/src/ggml-threading.h +12 -0
- package/src/llama.cpp/ggml/src/ggml-vulkan/CMakeLists.txt +78 -0
- package/src/llama.cpp/ggml/src/{ggml-vulkan.cpp → ggml-vulkan/ggml-vulkan.cpp} +2034 -1718
- package/src/llama.cpp/ggml/src/{vulkan-shaders → ggml-vulkan/vulkan-shaders}/CMakeLists.txt +2 -0
- package/src/llama.cpp/ggml/src/{vulkan-shaders → ggml-vulkan/vulkan-shaders}/vulkan-shaders-gen.cpp +152 -185
- package/src/llama.cpp/ggml/src/ggml.c +2075 -16579
- package/src/llama.cpp/include/llama.h +296 -285
- package/src/llama.cpp/models/ggml-vocab-chameleon.gguf.inp +112 -0
- package/src/llama.cpp/models/ggml-vocab-chameleon.gguf.out +46 -0
- package/src/llama.cpp/pocs/vdot/q8dot.cpp +4 -3
- package/src/llama.cpp/pocs/vdot/vdot.cpp +8 -7
- package/src/llama.cpp/requirements/requirements-convert_legacy_llama.txt +1 -1
- package/src/llama.cpp/src/CMakeLists.txt +2 -1
- package/src/llama.cpp/src/llama-grammar.cpp +721 -122
- package/src/llama.cpp/src/llama-grammar.h +120 -15
- package/src/llama.cpp/src/llama-impl.h +156 -1
- package/src/llama.cpp/src/llama-sampling.cpp +2058 -346
- package/src/llama.cpp/src/llama-sampling.h +39 -47
- package/src/llama.cpp/src/llama-vocab.cpp +390 -127
- package/src/llama.cpp/src/llama-vocab.h +60 -20
- package/src/llama.cpp/src/llama.cpp +6215 -3263
- package/src/llama.cpp/src/unicode-data.cpp +6 -4
- package/src/llama.cpp/src/unicode-data.h +4 -4
- package/src/llama.cpp/src/unicode.cpp +15 -7
- package/src/llama.cpp/tests/CMakeLists.txt +4 -2
- package/src/llama.cpp/tests/test-arg-parser.cpp +131 -0
- package/src/llama.cpp/tests/test-backend-ops.cpp +1725 -297
- package/src/llama.cpp/tests/test-barrier.cpp +94 -0
- package/src/llama.cpp/tests/test-chat-template.cpp +9 -5
- package/src/llama.cpp/tests/test-grammar-integration.cpp +23 -38
- package/src/llama.cpp/tests/test-grammar-parser.cpp +6 -4
- package/src/llama.cpp/tests/test-json-schema-to-grammar.cpp +23 -8
- package/src/llama.cpp/tests/test-llama-grammar.cpp +9 -8
- package/src/llama.cpp/tests/test-log.cpp +39 -0
- package/src/llama.cpp/tests/test-opt.cpp +853 -142
- package/src/llama.cpp/tests/test-quantize-fns.cpp +28 -19
- package/src/llama.cpp/tests/test-quantize-perf.cpp +16 -14
- package/src/llama.cpp/tests/test-rope.cpp +2 -1
- package/src/llama.cpp/tests/test-sampling.cpp +226 -142
- package/src/llama.cpp/tests/test-tokenizer-0.cpp +56 -36
- package/src/llama.cpp/tests/test-tokenizer-1-bpe.cpp +5 -5
- package/src/llama.cpp/tests/test-tokenizer-1-spm.cpp +5 -5
- package/patches/llama.patch +0 -22
- package/src/llama.cpp/.github/workflows/bench.yml +0 -310
- package/src/llama.cpp/common/grammar-parser.cpp +0 -536
- package/src/llama.cpp/common/grammar-parser.h +0 -29
- package/src/llama.cpp/common/train.cpp +0 -1513
- package/src/llama.cpp/common/train.h +0 -233
- package/src/llama.cpp/examples/baby-llama/baby-llama.cpp +0 -1640
- package/src/llama.cpp/examples/benchmark/CMakeLists.txt +0 -6
- package/src/llama.cpp/examples/benchmark/benchmark-matmult.cpp +0 -275
- package/src/llama.cpp/ggml/src/llamafile/sgemm.cpp +0 -1027
- package/src/llama.cpp/tests/test-grad0.cpp +0 -1566
- /package/src/llama.cpp/ggml/{cmake → src/ggml-cpu/cmake}/FindSIMD.cmake +0 -0
- /package/src/llama.cpp/ggml/src/{llamafile → ggml-cpu/llamafile}/sgemm.h +0 -0
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
#include "log.h"
|
|
2
|
+
|
|
3
|
+
#include <condition_variable>
|
|
4
|
+
#include <cstdarg>
|
|
5
|
+
#include <cstdio>
|
|
6
|
+
#include <mutex>
|
|
7
|
+
#include <sstream>
|
|
8
|
+
#include <thread>
|
|
9
|
+
#include <vector>
|
|
10
|
+
|
|
11
|
+
int common_log_verbosity_thold = LOG_DEFAULT_LLAMA;
|
|
12
|
+
|
|
13
|
+
void common_log_set_verbosity_thold(int verbosity) {
|
|
14
|
+
common_log_verbosity_thold = verbosity;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
#define LOG_COL_DEFAULT "\033[0m"
|
|
18
|
+
#define LOG_COL_BOLD "\033[1m"
|
|
19
|
+
#define LOG_COL_RED "\033[31m"
|
|
20
|
+
#define LOG_COL_GREEN "\033[32m"
|
|
21
|
+
#define LOG_COL_YELLOW "\033[33m"
|
|
22
|
+
#define LOG_COL_BLUE "\033[34m"
|
|
23
|
+
#define LOG_COL_MAGENTA "\033[35m"
|
|
24
|
+
#define LOG_COL_CYAN "\033[36m"
|
|
25
|
+
#define LOG_COL_WHITE "\033[37m"
|
|
26
|
+
|
|
27
|
+
static int64_t t_us() {
|
|
28
|
+
return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// colors
|
|
32
|
+
enum common_log_col : int {
|
|
33
|
+
COMMON_LOG_COL_DEFAULT = 0,
|
|
34
|
+
COMMON_LOG_COL_BOLD,
|
|
35
|
+
COMMON_LOG_COL_RED,
|
|
36
|
+
COMMON_LOG_COL_GREEN,
|
|
37
|
+
COMMON_LOG_COL_YELLOW,
|
|
38
|
+
COMMON_LOG_COL_BLUE,
|
|
39
|
+
COMMON_LOG_COL_MAGENTA,
|
|
40
|
+
COMMON_LOG_COL_CYAN,
|
|
41
|
+
COMMON_LOG_COL_WHITE,
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// disable colors by default
|
|
45
|
+
static std::vector<const char *> g_col = {
|
|
46
|
+
"",
|
|
47
|
+
"",
|
|
48
|
+
"",
|
|
49
|
+
"",
|
|
50
|
+
"",
|
|
51
|
+
"",
|
|
52
|
+
"",
|
|
53
|
+
"",
|
|
54
|
+
"",
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
struct common_log_entry {
|
|
58
|
+
enum ggml_log_level level;
|
|
59
|
+
|
|
60
|
+
bool prefix;
|
|
61
|
+
|
|
62
|
+
int64_t timestamp;
|
|
63
|
+
|
|
64
|
+
std::vector<char> msg;
|
|
65
|
+
|
|
66
|
+
// signals the worker thread to stop
|
|
67
|
+
bool is_end;
|
|
68
|
+
|
|
69
|
+
void print(FILE * file = nullptr) const {
|
|
70
|
+
FILE * fcur = file;
|
|
71
|
+
if (!fcur) {
|
|
72
|
+
// stderr displays DBG messages only when their verbosity level is not higher than the threshold
|
|
73
|
+
// these messages will still be logged to a file
|
|
74
|
+
if (level == GGML_LOG_LEVEL_DEBUG && common_log_verbosity_thold < LOG_DEFAULT_DEBUG) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
fcur = stdout;
|
|
79
|
+
|
|
80
|
+
if (level != GGML_LOG_LEVEL_NONE) {
|
|
81
|
+
fcur = stderr;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (level != GGML_LOG_LEVEL_NONE && level != GGML_LOG_LEVEL_CONT && prefix) {
|
|
86
|
+
if (timestamp) {
|
|
87
|
+
// [M.s.ms.us]
|
|
88
|
+
fprintf(fcur, "%s%d.%02d.%03d.%03d%s ",
|
|
89
|
+
g_col[COMMON_LOG_COL_BLUE],
|
|
90
|
+
(int) (timestamp / 1000000 / 60),
|
|
91
|
+
(int) (timestamp / 1000000 % 60),
|
|
92
|
+
(int) (timestamp / 1000 % 1000),
|
|
93
|
+
(int) (timestamp % 1000),
|
|
94
|
+
g_col[COMMON_LOG_COL_DEFAULT]);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
switch (level) {
|
|
98
|
+
case GGML_LOG_LEVEL_INFO: fprintf(fcur, "%sI %s", g_col[COMMON_LOG_COL_GREEN], g_col[COMMON_LOG_COL_DEFAULT]); break;
|
|
99
|
+
case GGML_LOG_LEVEL_WARN: fprintf(fcur, "%sW %s", g_col[COMMON_LOG_COL_MAGENTA], "" ); break;
|
|
100
|
+
case GGML_LOG_LEVEL_ERROR: fprintf(fcur, "%sE %s", g_col[COMMON_LOG_COL_RED], "" ); break;
|
|
101
|
+
case GGML_LOG_LEVEL_DEBUG: fprintf(fcur, "%sD %s", g_col[COMMON_LOG_COL_YELLOW], "" ); break;
|
|
102
|
+
default:
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
fprintf(fcur, "%s", msg.data());
|
|
108
|
+
|
|
109
|
+
if (level == GGML_LOG_LEVEL_WARN || level == GGML_LOG_LEVEL_ERROR || level == GGML_LOG_LEVEL_DEBUG) {
|
|
110
|
+
fprintf(fcur, "%s", g_col[COMMON_LOG_COL_DEFAULT]);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
fflush(fcur);
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
struct common_log {
|
|
118
|
+
// default capacity - will be expanded if needed
|
|
119
|
+
common_log() : common_log(256) {}
|
|
120
|
+
|
|
121
|
+
common_log(size_t capacity) {
|
|
122
|
+
file = nullptr;
|
|
123
|
+
prefix = false;
|
|
124
|
+
timestamps = false;
|
|
125
|
+
running = false;
|
|
126
|
+
t_start = t_us();
|
|
127
|
+
|
|
128
|
+
// initial message size - will be expanded if longer messages arrive
|
|
129
|
+
entries.resize(capacity);
|
|
130
|
+
for (auto & entry : entries) {
|
|
131
|
+
entry.msg.resize(256);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
head = 0;
|
|
135
|
+
tail = 0;
|
|
136
|
+
|
|
137
|
+
resume();
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
~common_log() {
|
|
141
|
+
pause();
|
|
142
|
+
if (file) {
|
|
143
|
+
fclose(file);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
private:
|
|
148
|
+
std::mutex mtx;
|
|
149
|
+
std::thread thrd;
|
|
150
|
+
std::condition_variable cv;
|
|
151
|
+
|
|
152
|
+
FILE * file;
|
|
153
|
+
|
|
154
|
+
bool prefix;
|
|
155
|
+
bool timestamps;
|
|
156
|
+
bool running;
|
|
157
|
+
|
|
158
|
+
int64_t t_start;
|
|
159
|
+
|
|
160
|
+
// ring buffer of entries
|
|
161
|
+
std::vector<common_log_entry> entries;
|
|
162
|
+
size_t head;
|
|
163
|
+
size_t tail;
|
|
164
|
+
|
|
165
|
+
// worker thread copies into this
|
|
166
|
+
common_log_entry cur;
|
|
167
|
+
|
|
168
|
+
public:
|
|
169
|
+
void add(enum ggml_log_level level, const char * fmt, va_list args) {
|
|
170
|
+
std::lock_guard<std::mutex> lock(mtx);
|
|
171
|
+
|
|
172
|
+
if (!running) {
|
|
173
|
+
// discard messages while the worker thread is paused
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
auto & entry = entries[tail];
|
|
178
|
+
|
|
179
|
+
{
|
|
180
|
+
// cannot use args twice, so make a copy in case we need to expand the buffer
|
|
181
|
+
va_list args_copy;
|
|
182
|
+
va_copy(args_copy, args);
|
|
183
|
+
|
|
184
|
+
#if 1
|
|
185
|
+
const size_t n = vsnprintf(entry.msg.data(), entry.msg.size(), fmt, args);
|
|
186
|
+
if (n >= entry.msg.size()) {
|
|
187
|
+
entry.msg.resize(n + 1);
|
|
188
|
+
vsnprintf(entry.msg.data(), entry.msg.size(), fmt, args_copy);
|
|
189
|
+
}
|
|
190
|
+
#else
|
|
191
|
+
// hack for bolding arguments
|
|
192
|
+
|
|
193
|
+
std::stringstream ss;
|
|
194
|
+
for (int i = 0; fmt[i] != 0; i++) {
|
|
195
|
+
if (fmt[i] == '%') {
|
|
196
|
+
ss << LOG_COL_BOLD;
|
|
197
|
+
while (fmt[i] != ' ' && fmt[i] != ')' && fmt[i] != ']' && fmt[i] != 0) ss << fmt[i++];
|
|
198
|
+
ss << LOG_COL_DEFAULT;
|
|
199
|
+
if (fmt[i] == 0) break;
|
|
200
|
+
}
|
|
201
|
+
ss << fmt[i];
|
|
202
|
+
}
|
|
203
|
+
const size_t n = vsnprintf(entry.msg.data(), entry.msg.size(), ss.str().c_str(), args);
|
|
204
|
+
if (n >= entry.msg.size()) {
|
|
205
|
+
entry.msg.resize(n + 1);
|
|
206
|
+
vsnprintf(entry.msg.data(), entry.msg.size(), ss.str().c_str(), args_copy);
|
|
207
|
+
}
|
|
208
|
+
#endif
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
entry.level = level;
|
|
212
|
+
entry.prefix = prefix;
|
|
213
|
+
entry.timestamp = 0;
|
|
214
|
+
if (timestamps) {
|
|
215
|
+
entry.timestamp = t_us() - t_start;
|
|
216
|
+
}
|
|
217
|
+
entry.is_end = false;
|
|
218
|
+
|
|
219
|
+
tail = (tail + 1) % entries.size();
|
|
220
|
+
if (tail == head) {
|
|
221
|
+
// expand the buffer
|
|
222
|
+
std::vector<common_log_entry> new_entries(2*entries.size());
|
|
223
|
+
|
|
224
|
+
size_t new_tail = 0;
|
|
225
|
+
|
|
226
|
+
do {
|
|
227
|
+
new_entries[new_tail] = std::move(entries[head]);
|
|
228
|
+
|
|
229
|
+
head = (head + 1) % entries.size();
|
|
230
|
+
new_tail = (new_tail + 1);
|
|
231
|
+
} while (head != tail);
|
|
232
|
+
|
|
233
|
+
head = 0;
|
|
234
|
+
tail = new_tail;
|
|
235
|
+
|
|
236
|
+
for (size_t i = tail; i < new_entries.size(); i++) {
|
|
237
|
+
new_entries[i].msg.resize(256);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
entries = std::move(new_entries);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
cv.notify_one();
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
void resume() {
|
|
247
|
+
std::lock_guard<std::mutex> lock(mtx);
|
|
248
|
+
|
|
249
|
+
if (running) {
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
running = true;
|
|
254
|
+
|
|
255
|
+
thrd = std::thread([this]() {
|
|
256
|
+
while (true) {
|
|
257
|
+
{
|
|
258
|
+
std::unique_lock<std::mutex> lock(mtx);
|
|
259
|
+
cv.wait(lock, [this]() { return head != tail; });
|
|
260
|
+
|
|
261
|
+
cur = entries[head];
|
|
262
|
+
|
|
263
|
+
head = (head + 1) % entries.size();
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
if (cur.is_end) {
|
|
267
|
+
break;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
cur.print(); // stdout and stderr
|
|
271
|
+
|
|
272
|
+
if (file) {
|
|
273
|
+
cur.print(file);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
void pause() {
|
|
280
|
+
{
|
|
281
|
+
std::lock_guard<std::mutex> lock(mtx);
|
|
282
|
+
|
|
283
|
+
if (!running) {
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
running = false;
|
|
288
|
+
|
|
289
|
+
// push an entry to signal the worker thread to stop
|
|
290
|
+
{
|
|
291
|
+
auto & entry = entries[tail];
|
|
292
|
+
entry.is_end = true;
|
|
293
|
+
|
|
294
|
+
tail = (tail + 1) % entries.size();
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
cv.notify_one();
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
thrd.join();
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
void set_file(const char * path) {
|
|
304
|
+
pause();
|
|
305
|
+
|
|
306
|
+
if (file) {
|
|
307
|
+
fclose(file);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
if (path) {
|
|
311
|
+
file = fopen(path, "w");
|
|
312
|
+
} else {
|
|
313
|
+
file = nullptr;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
resume();
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
void set_colors(bool colors) {
|
|
320
|
+
pause();
|
|
321
|
+
|
|
322
|
+
if (colors) {
|
|
323
|
+
g_col[COMMON_LOG_COL_DEFAULT] = LOG_COL_DEFAULT;
|
|
324
|
+
g_col[COMMON_LOG_COL_BOLD] = LOG_COL_BOLD;
|
|
325
|
+
g_col[COMMON_LOG_COL_RED] = LOG_COL_RED;
|
|
326
|
+
g_col[COMMON_LOG_COL_GREEN] = LOG_COL_GREEN;
|
|
327
|
+
g_col[COMMON_LOG_COL_YELLOW] = LOG_COL_YELLOW;
|
|
328
|
+
g_col[COMMON_LOG_COL_BLUE] = LOG_COL_BLUE;
|
|
329
|
+
g_col[COMMON_LOG_COL_MAGENTA] = LOG_COL_MAGENTA;
|
|
330
|
+
g_col[COMMON_LOG_COL_CYAN] = LOG_COL_CYAN;
|
|
331
|
+
g_col[COMMON_LOG_COL_WHITE] = LOG_COL_WHITE;
|
|
332
|
+
} else {
|
|
333
|
+
for (size_t i = 0; i < g_col.size(); i++) {
|
|
334
|
+
g_col[i] = "";
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
resume();
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
void set_prefix(bool prefix) {
|
|
342
|
+
std::lock_guard<std::mutex> lock(mtx);
|
|
343
|
+
|
|
344
|
+
this->prefix = prefix;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
void set_timestamps(bool timestamps) {
|
|
348
|
+
std::lock_guard<std::mutex> lock(mtx);
|
|
349
|
+
|
|
350
|
+
this->timestamps = timestamps;
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
|
|
354
|
+
//
|
|
355
|
+
// public API
|
|
356
|
+
//
|
|
357
|
+
|
|
358
|
+
struct common_log * common_log_init() {
|
|
359
|
+
return new common_log;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
struct common_log * common_log_main() {
|
|
363
|
+
static struct common_log log;
|
|
364
|
+
|
|
365
|
+
return &log;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
void common_log_pause(struct common_log * log) {
|
|
369
|
+
log->pause();
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
void common_log_resume(struct common_log * log) {
|
|
373
|
+
log->resume();
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
void common_log_free(struct common_log * log) {
|
|
377
|
+
delete log;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
void common_log_add(struct common_log * log, enum ggml_log_level level, const char * fmt, ...) {
|
|
381
|
+
va_list args;
|
|
382
|
+
va_start(args, fmt);
|
|
383
|
+
log->add(level, fmt, args);
|
|
384
|
+
va_end(args);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
void common_log_set_file(struct common_log * log, const char * file) {
|
|
388
|
+
log->set_file(file);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
void common_log_set_colors(struct common_log * log, bool colors) {
|
|
392
|
+
log->set_colors(colors);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
void common_log_set_prefix(struct common_log * log, bool prefix) {
|
|
396
|
+
log->set_prefix(prefix);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
void common_log_set_timestamps(struct common_log * log, bool timestamps) {
|
|
400
|
+
log->set_timestamps(timestamps);
|
|
401
|
+
}
|