cui-llama.rn 1.4.4 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/src/main/CMakeLists.txt +9 -2
- package/android/src/main/jni.cpp +54 -34
- package/android/src/main/jniLibs/arm64-v8a/librnllama.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/librnllama_v8.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_dotprod.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_dotprod_i8mm.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_i8mm.so +0 -0
- package/android/src/main/jniLibs/x86_64/librnllama.so +0 -0
- package/android/src/main/jniLibs/x86_64/librnllama_x86_64.so +0 -0
- package/cpp/binary-ops.cpp +158 -0
- package/cpp/binary-ops.h +16 -0
- package/cpp/chat.cpp +1769 -1085
- package/cpp/chat.h +143 -0
- package/cpp/common.cpp +1562 -1996
- package/cpp/common.h +677 -744
- package/cpp/cpu-common.h +72 -0
- package/cpp/ggml-alloc.c +1039 -1030
- package/cpp/ggml-alloc.h +1 -1
- package/cpp/ggml-backend-impl.h +255 -255
- package/cpp/ggml-backend-reg.cpp +586 -582
- package/cpp/ggml-backend.cpp +2004 -2002
- package/cpp/ggml-backend.h +354 -354
- package/cpp/ggml-common.h +1857 -1851
- package/cpp/ggml-cpp.h +39 -39
- package/cpp/ggml-cpu-aarch64.cpp +5725 -4247
- package/cpp/ggml-cpu-aarch64.h +8 -8
- package/cpp/ggml-cpu-impl.h +512 -380
- package/cpp/ggml-cpu-quants.c +13026 -11517
- package/cpp/ggml-cpu-traits.cpp +36 -36
- package/cpp/ggml-cpu-traits.h +38 -38
- package/cpp/ggml-cpu.c +3438 -14485
- package/cpp/ggml-cpu.cpp +655 -633
- package/cpp/ggml-cpu.h +138 -135
- package/cpp/ggml-impl.h +594 -567
- package/cpp/ggml-metal-impl.h +312 -3
- package/cpp/ggml-metal.h +66 -66
- package/cpp/ggml-metal.m +5360 -5002
- package/cpp/ggml-opt.cpp +854 -854
- package/cpp/ggml-opt.h +216 -216
- package/cpp/ggml-quants.c +5238 -5238
- package/cpp/ggml-threading.h +14 -14
- package/cpp/ggml.c +6618 -6524
- package/cpp/ggml.h +2222 -2194
- package/cpp/gguf.cpp +1330 -1329
- package/cpp/gguf.h +202 -202
- package/cpp/json-schema-to-grammar.cpp +1024 -1025
- package/cpp/json-schema-to-grammar.h +21 -22
- package/cpp/json.hpp +24766 -24766
- package/cpp/llama-adapter.cpp +382 -347
- package/cpp/llama-adapter.h +76 -74
- package/cpp/llama-arch.cpp +1714 -1492
- package/cpp/llama-arch.h +428 -402
- package/cpp/llama-batch.cpp +368 -368
- package/cpp/llama-batch.h +88 -88
- package/cpp/llama-chat.cpp +640 -587
- package/cpp/llama-chat.h +56 -53
- package/cpp/llama-context.cpp +2831 -1775
- package/cpp/llama-context.h +265 -128
- package/cpp/llama-cparams.cpp +1 -1
- package/cpp/llama-cparams.h +38 -37
- package/cpp/llama-cpp.h +30 -30
- package/cpp/llama-grammar.cpp +1219 -1219
- package/cpp/llama-grammar.h +173 -164
- package/cpp/llama-graph.cpp +1695 -0
- package/cpp/llama-graph.h +592 -0
- package/cpp/llama-hparams.cpp +79 -71
- package/cpp/llama-hparams.h +156 -139
- package/cpp/llama-impl.cpp +167 -167
- package/cpp/llama-impl.h +61 -61
- package/cpp/llama-io.cpp +15 -0
- package/cpp/llama-io.h +35 -0
- package/cpp/llama-kv-cache.cpp +1380 -718
- package/cpp/llama-kv-cache.h +213 -218
- package/cpp/llama-memory.cpp +1 -0
- package/cpp/llama-memory.h +21 -0
- package/cpp/llama-mmap.cpp +600 -590
- package/cpp/llama-mmap.h +68 -68
- package/cpp/llama-model-loader.cpp +1129 -1124
- package/cpp/llama-model-loader.h +169 -167
- package/cpp/llama-model.cpp +13080 -4023
- package/cpp/llama-model.h +409 -370
- package/cpp/llama-sampling.cpp +2563 -2525
- package/cpp/llama-sampling.h +32 -32
- package/cpp/llama-vocab.cpp +3295 -3252
- package/cpp/llama-vocab.h +125 -125
- package/cpp/llama.cpp +351 -10137
- package/cpp/llama.h +1434 -1340
- package/cpp/log.cpp +427 -423
- package/cpp/log.h +132 -132
- package/cpp/{chat-template.hpp → minja/chat-template.hpp} +537 -529
- package/cpp/{minja.hpp → minja/minja.hpp} +2941 -2883
- package/cpp/ops.cpp +8723 -0
- package/cpp/ops.h +128 -0
- package/cpp/rn-llama.cpp +45 -71
- package/cpp/rn-llama.h +3 -3
- package/cpp/sampling.cpp +573 -532
- package/cpp/sgemm.cpp +3043 -2598
- package/cpp/sgemm.h +14 -14
- package/cpp/simd-mappings.h +888 -0
- package/cpp/speculative.cpp +278 -277
- package/cpp/speculative.h +28 -28
- package/cpp/unary-ops.cpp +186 -0
- package/cpp/unary-ops.h +28 -0
- package/cpp/vec.cpp +258 -0
- package/cpp/vec.h +802 -0
- package/ios/CMakeLists.txt +5 -2
- package/ios/RNLlama.mm +2 -2
- package/ios/RNLlamaContext.mm +40 -24
- package/package.json +1 -1
- package/src/NativeRNLlama.ts +6 -4
- package/src/index.ts +3 -1
- package/android/src/main/build-arm64/CMakeCache.txt +0 -429
- package/android/src/main/build-arm64/CMakeFiles/3.31.4/CMakeCCompiler.cmake +0 -81
- package/android/src/main/build-arm64/CMakeFiles/3.31.4/CMakeCXXCompiler.cmake +0 -101
- package/android/src/main/build-arm64/CMakeFiles/3.31.4/CMakeDetermineCompilerABI_C.bin +0 -0
- package/android/src/main/build-arm64/CMakeFiles/3.31.4/CMakeDetermineCompilerABI_CXX.bin +0 -0
- package/android/src/main/build-arm64/CMakeFiles/3.31.4/CMakeSystem.cmake +0 -15
- package/android/src/main/build-arm64/CMakeFiles/3.31.4/CompilerIdC/CMakeCCompilerId.c +0 -904
- package/android/src/main/build-arm64/CMakeFiles/3.31.4/CompilerIdC/CMakeCCompilerId.o +0 -0
- package/android/src/main/build-arm64/CMakeFiles/3.31.4/CompilerIdCXX/CMakeCXXCompilerId.cpp +0 -919
- package/android/src/main/build-arm64/CMakeFiles/3.31.4/CompilerIdCXX/CMakeCXXCompilerId.o +0 -0
- package/android/src/main/build-arm64/CMakeFiles/CMakeConfigureLog.yaml +0 -431
- package/android/src/main/build-arm64/CMakeFiles/CMakeDirectoryInformation.cmake +0 -16
- package/android/src/main/build-arm64/CMakeFiles/Makefile.cmake +0 -165
- package/android/src/main/build-arm64/CMakeFiles/Makefile2 +0 -297
- package/android/src/main/build-arm64/CMakeFiles/Progress/1 +0 -1
- package/android/src/main/build-arm64/CMakeFiles/Progress/2 +0 -1
- package/android/src/main/build-arm64/CMakeFiles/Progress/3 +0 -1
- package/android/src/main/build-arm64/CMakeFiles/Progress/4 +0 -1
- package/android/src/main/build-arm64/CMakeFiles/Progress/5 +0 -1
- package/android/src/main/build-arm64/CMakeFiles/Progress/6 +0 -1
- package/android/src/main/build-arm64/CMakeFiles/Progress/count.txt +0 -1
- package/android/src/main/build-arm64/CMakeFiles/TargetDirectories.txt +0 -8
- package/android/src/main/build-arm64/CMakeFiles/cmake.check_cache +0 -1
- package/android/src/main/build-arm64/CMakeFiles/progress.marks +0 -1
- package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-alloc.c.o +0 -0
- package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-alloc.c.o.d +0 -58
- package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-backend-reg.cpp.o +0 -0
- 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
- package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-backend.cpp.o +0 -0
- package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-backend.cpp.o.d +0 -709
- package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-cpu-aarch64.cpp.o +0 -0
- 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
- package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-cpu-quants.c.o +0 -0
- 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
- package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-cpu-traits.cpp.o +0 -0
- 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
- package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-cpu.c.o +0 -0
- package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-cpu.c.o.d +0 -113
- package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-cpu.cpp.o +0 -0
- package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-cpu.cpp.o.d +0 -713
- package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-opt.cpp.o +0 -0
- package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-opt.cpp.o.d +0 -763
- package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-quants.c.o +0 -0
- package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-quants.c.o.d +0 -61
- package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-threading.cpp.o +0 -0
- package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-threading.cpp.o.d +0 -707
- package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml.c.o +0 -0
- package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml.c.o.d +0 -104
- package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/gguf.cpp.o +0 -0
- package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/gguf.cpp.o.d +0 -714
- package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/log.cpp.o +0 -0
- package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/log.cpp.o.d +0 -723
- package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/DependInfo.cmake +0 -62
- package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/build.make +0 -722
- package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/cmake_clean.cmake +0 -89
- package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/compiler_depend.make +0 -2
- package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/compiler_depend.ts +0 -2
- package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/depend.make +0 -2
- package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/flags.make +0 -17
- package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/progress.make +0 -41
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8.dir/DependInfo.cmake +0 -62
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8.dir/build.make +0 -722
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8.dir/cmake_clean.cmake +0 -89
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8.dir/compiler_depend.make +0 -2
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8.dir/compiler_depend.ts +0 -2
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8.dir/depend.make +0 -2
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8.dir/flags.make +0 -17
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8.dir/progress.make +0 -41
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2.dir/DependInfo.cmake +0 -62
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2.dir/build.make +0 -722
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2.dir/cmake_clean.cmake +0 -89
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2.dir/compiler_depend.make +0 -2
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2.dir/compiler_depend.ts +0 -2
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2.dir/depend.make +0 -2
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2.dir/flags.make +0 -17
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2.dir/progress.make +0 -41
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod.dir/DependInfo.cmake +0 -62
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod.dir/build.make +0 -722
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod.dir/cmake_clean.cmake +0 -89
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod.dir/compiler_depend.make +0 -2
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod.dir/compiler_depend.ts +0 -2
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod.dir/depend.make +0 -2
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod.dir/flags.make +0 -17
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod.dir/progress.make +0 -41
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod_i8mm.dir/DependInfo.cmake +0 -62
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod_i8mm.dir/build.make +0 -722
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod_i8mm.dir/cmake_clean.cmake +0 -89
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod_i8mm.dir/compiler_depend.make +0 -2
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod_i8mm.dir/compiler_depend.ts +0 -2
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod_i8mm.dir/depend.make +0 -2
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod_i8mm.dir/flags.make +0 -17
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod_i8mm.dir/progress.make +0 -41
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_i8mm.dir/DependInfo.cmake +0 -62
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_i8mm.dir/build.make +0 -722
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_i8mm.dir/cmake_clean.cmake +0 -89
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_i8mm.dir/compiler_depend.make +0 -2
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_i8mm.dir/compiler_depend.ts +0 -2
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_i8mm.dir/depend.make +0 -2
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_i8mm.dir/flags.make +0 -17
- package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_i8mm.dir/progress.make +0 -41
- package/android/src/main/build-arm64/Makefile +0 -1862
- package/android/src/main/build-arm64/cmake_install.cmake +0 -66
- package/cpp/chat.hpp +0 -55
- package/cpp/rn-llama.hpp +0 -913
package/cpp/common.cpp
CHANGED
@@ -1,1996 +1,1562 @@
|
|
1
|
-
#if defined(_MSC_VER)
|
2
|
-
#define _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING
|
3
|
-
#endif
|
4
|
-
|
5
|
-
#include "ggml.h"
|
6
|
-
#include "gguf.h"
|
7
|
-
|
8
|
-
#include "common.h"
|
9
|
-
#include "log.h"
|
10
|
-
|
11
|
-
|
12
|
-
#include
|
13
|
-
#include
|
14
|
-
#include
|
15
|
-
#include
|
16
|
-
#include
|
17
|
-
|
18
|
-
#include <
|
19
|
-
#include <
|
20
|
-
#include <
|
21
|
-
#include <
|
22
|
-
#include <
|
23
|
-
#include <
|
24
|
-
#include <
|
25
|
-
#include <
|
26
|
-
#include <
|
27
|
-
#include <
|
28
|
-
#include <
|
29
|
-
#include <
|
30
|
-
#include <
|
31
|
-
|
32
|
-
#
|
33
|
-
#include <
|
34
|
-
#include <
|
35
|
-
#
|
36
|
-
|
37
|
-
|
38
|
-
#
|
39
|
-
#
|
40
|
-
#
|
41
|
-
#endif
|
42
|
-
|
43
|
-
#
|
44
|
-
#
|
45
|
-
#
|
46
|
-
#
|
47
|
-
#
|
48
|
-
#include <
|
49
|
-
#include <
|
50
|
-
|
51
|
-
#
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
#
|
60
|
-
#
|
61
|
-
#endif
|
62
|
-
|
63
|
-
//
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
}
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
if (
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
size_t end_i;
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
if (
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
}
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
std::
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
std::
|
590
|
-
std::
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
|
646
|
-
|
647
|
-
|
648
|
-
|
649
|
-
|
650
|
-
|
651
|
-
|
652
|
-
|
653
|
-
}
|
654
|
-
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
|
659
|
-
|
660
|
-
|
661
|
-
|
662
|
-
|
663
|
-
|
664
|
-
LOG_ERR("%s:
|
665
|
-
return false;
|
666
|
-
}
|
667
|
-
|
668
|
-
|
669
|
-
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
|
676
|
-
|
677
|
-
|
678
|
-
|
679
|
-
|
680
|
-
|
681
|
-
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
|
686
|
-
|
687
|
-
|
688
|
-
|
689
|
-
|
690
|
-
|
691
|
-
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
|
696
|
-
|
697
|
-
|
698
|
-
|
699
|
-
|
700
|
-
|
701
|
-
|
702
|
-
|
703
|
-
|
704
|
-
|
705
|
-
|
706
|
-
|
707
|
-
|
708
|
-
|
709
|
-
|
710
|
-
|
711
|
-
|
712
|
-
|
713
|
-
|
714
|
-
|
715
|
-
|
716
|
-
|
717
|
-
|
718
|
-
|
719
|
-
|
720
|
-
|
721
|
-
|
722
|
-
|
723
|
-
|
724
|
-
|
725
|
-
|
726
|
-
|
727
|
-
|
728
|
-
|
729
|
-
|
730
|
-
|
731
|
-
|
732
|
-
|
733
|
-
|
734
|
-
|
735
|
-
|
736
|
-
|
737
|
-
|
738
|
-
|
739
|
-
|
740
|
-
|
741
|
-
|
742
|
-
|
743
|
-
|
744
|
-
|
745
|
-
|
746
|
-
}
|
747
|
-
|
748
|
-
|
749
|
-
|
750
|
-
|
751
|
-
|
752
|
-
|
753
|
-
|
754
|
-
|
755
|
-
|
756
|
-
|
757
|
-
|
758
|
-
|
759
|
-
|
760
|
-
|
761
|
-
|
762
|
-
|
763
|
-
|
764
|
-
|
765
|
-
|
766
|
-
|
767
|
-
|
768
|
-
|
769
|
-
|
770
|
-
|
771
|
-
|
772
|
-
|
773
|
-
|
774
|
-
|
775
|
-
|
776
|
-
|
777
|
-
|
778
|
-
|
779
|
-
|
780
|
-
|
781
|
-
|
782
|
-
|
783
|
-
|
784
|
-
|
785
|
-
|
786
|
-
|
787
|
-
|
788
|
-
|
789
|
-
|
790
|
-
|
791
|
-
|
792
|
-
|
793
|
-
|
794
|
-
#
|
795
|
-
|
796
|
-
|
797
|
-
|
798
|
-
|
799
|
-
|
800
|
-
|
801
|
-
|
802
|
-
|
803
|
-
|
804
|
-
|
805
|
-
|
806
|
-
|
807
|
-
|
808
|
-
|
809
|
-
|
810
|
-
|
811
|
-
|
812
|
-
|
813
|
-
|
814
|
-
|
815
|
-
|
816
|
-
if (
|
817
|
-
|
818
|
-
|
819
|
-
|
820
|
-
|
821
|
-
|
822
|
-
|
823
|
-
|
824
|
-
|
825
|
-
|
826
|
-
|
827
|
-
|
828
|
-
|
829
|
-
|
830
|
-
|
831
|
-
|
832
|
-
|
833
|
-
|
834
|
-
|
835
|
-
|
836
|
-
|
837
|
-
|
838
|
-
|
839
|
-
|
840
|
-
|
841
|
-
|
842
|
-
|
843
|
-
|
844
|
-
|
845
|
-
|
846
|
-
|
847
|
-
|
848
|
-
|
849
|
-
|
850
|
-
|
851
|
-
|
852
|
-
|
853
|
-
|
854
|
-
|
855
|
-
|
856
|
-
|
857
|
-
|
858
|
-
|
859
|
-
|
860
|
-
|
861
|
-
|
862
|
-
|
863
|
-
|
864
|
-
|
865
|
-
|
866
|
-
|
867
|
-
|
868
|
-
|
869
|
-
|
870
|
-
|
871
|
-
|
872
|
-
|
873
|
-
|
874
|
-
|
875
|
-
|
876
|
-
|
877
|
-
|
878
|
-
|
879
|
-
|
880
|
-
|
881
|
-
|
882
|
-
|
883
|
-
|
884
|
-
|
885
|
-
|
886
|
-
|
887
|
-
|
888
|
-
|
889
|
-
|
890
|
-
|
891
|
-
|
892
|
-
|
893
|
-
|
894
|
-
|
895
|
-
|
896
|
-
|
897
|
-
|
898
|
-
|
899
|
-
|
900
|
-
|
901
|
-
|
902
|
-
|
903
|
-
|
904
|
-
|
905
|
-
|
906
|
-
|
907
|
-
|
908
|
-
|
909
|
-
auto
|
910
|
-
|
911
|
-
|
912
|
-
|
913
|
-
|
914
|
-
|
915
|
-
|
916
|
-
|
917
|
-
|
918
|
-
|
919
|
-
|
920
|
-
|
921
|
-
|
922
|
-
|
923
|
-
|
924
|
-
|
925
|
-
|
926
|
-
|
927
|
-
|
928
|
-
|
929
|
-
|
930
|
-
|
931
|
-
|
932
|
-
|
933
|
-
|
934
|
-
|
935
|
-
|
936
|
-
|
937
|
-
|
938
|
-
|
939
|
-
|
940
|
-
|
941
|
-
|
942
|
-
|
943
|
-
|
944
|
-
|
945
|
-
|
946
|
-
|
947
|
-
|
948
|
-
|
949
|
-
|
950
|
-
|
951
|
-
|
952
|
-
|
953
|
-
|
954
|
-
|
955
|
-
|
956
|
-
|
957
|
-
|
958
|
-
|
959
|
-
|
960
|
-
|
961
|
-
|
962
|
-
|
963
|
-
|
964
|
-
|
965
|
-
|
966
|
-
|
967
|
-
|
968
|
-
|
969
|
-
|
970
|
-
|
971
|
-
|
972
|
-
|
973
|
-
|
974
|
-
|
975
|
-
|
976
|
-
|
977
|
-
|
978
|
-
|
979
|
-
|
980
|
-
|
981
|
-
|
982
|
-
|
983
|
-
|
984
|
-
|
985
|
-
|
986
|
-
|
987
|
-
|
988
|
-
|
989
|
-
|
990
|
-
|
991
|
-
|
992
|
-
|
993
|
-
|
994
|
-
|
995
|
-
|
996
|
-
|
997
|
-
|
998
|
-
|
999
|
-
|
1000
|
-
|
1001
|
-
|
1002
|
-
|
1003
|
-
|
1004
|
-
|
1005
|
-
|
1006
|
-
|
1007
|
-
|
1008
|
-
|
1009
|
-
|
1010
|
-
|
1011
|
-
|
1012
|
-
|
1013
|
-
|
1014
|
-
|
1015
|
-
|
1016
|
-
|
1017
|
-
|
1018
|
-
|
1019
|
-
|
1020
|
-
|
1021
|
-
|
1022
|
-
|
1023
|
-
|
1024
|
-
}
|
1025
|
-
|
1026
|
-
|
1027
|
-
|
1028
|
-
|
1029
|
-
|
1030
|
-
|
1031
|
-
|
1032
|
-
|
1033
|
-
|
1034
|
-
|
1035
|
-
|
1036
|
-
|
1037
|
-
|
1038
|
-
|
1039
|
-
|
1040
|
-
|
1041
|
-
|
1042
|
-
|
1043
|
-
|
1044
|
-
|
1045
|
-
|
1046
|
-
|
1047
|
-
|
1048
|
-
|
1049
|
-
|
1050
|
-
|
1051
|
-
|
1052
|
-
|
1053
|
-
|
1054
|
-
|
1055
|
-
|
1056
|
-
|
1057
|
-
|
1058
|
-
|
1059
|
-
|
1060
|
-
|
1061
|
-
|
1062
|
-
|
1063
|
-
|
1064
|
-
|
1065
|
-
|
1066
|
-
|
1067
|
-
|
1068
|
-
|
1069
|
-
|
1070
|
-
|
1071
|
-
|
1072
|
-
|
1073
|
-
|
1074
|
-
|
1075
|
-
|
1076
|
-
}
|
1077
|
-
|
1078
|
-
|
1079
|
-
|
1080
|
-
|
1081
|
-
|
1082
|
-
|
1083
|
-
|
1084
|
-
|
1085
|
-
|
1086
|
-
|
1087
|
-
|
1088
|
-
|
1089
|
-
|
1090
|
-
|
1091
|
-
|
1092
|
-
|
1093
|
-
|
1094
|
-
|
1095
|
-
|
1096
|
-
|
1097
|
-
|
1098
|
-
|
1099
|
-
|
1100
|
-
|
1101
|
-
|
1102
|
-
|
1103
|
-
|
1104
|
-
|
1105
|
-
|
1106
|
-
|
1107
|
-
|
1108
|
-
|
1109
|
-
|
1110
|
-
|
1111
|
-
|
1112
|
-
|
1113
|
-
|
1114
|
-
|
1115
|
-
|
1116
|
-
|
1117
|
-
|
1118
|
-
|
1119
|
-
cparams.
|
1120
|
-
|
1121
|
-
cparams
|
1122
|
-
|
1123
|
-
|
1124
|
-
|
1125
|
-
|
1126
|
-
|
1127
|
-
|
1128
|
-
|
1129
|
-
|
1130
|
-
|
1131
|
-
|
1132
|
-
|
1133
|
-
|
1134
|
-
|
1135
|
-
|
1136
|
-
|
1137
|
-
|
1138
|
-
|
1139
|
-
|
1140
|
-
|
1141
|
-
|
1142
|
-
|
1143
|
-
|
1144
|
-
|
1145
|
-
|
1146
|
-
|
1147
|
-
|
1148
|
-
|
1149
|
-
|
1150
|
-
|
1151
|
-
|
1152
|
-
|
1153
|
-
|
1154
|
-
|
1155
|
-
|
1156
|
-
|
1157
|
-
|
1158
|
-
|
1159
|
-
|
1160
|
-
|
1161
|
-
|
1162
|
-
|
1163
|
-
|
1164
|
-
|
1165
|
-
|
1166
|
-
|
1167
|
-
|
1168
|
-
|
1169
|
-
|
1170
|
-
|
1171
|
-
|
1172
|
-
|
1173
|
-
|
1174
|
-
|
1175
|
-
|
1176
|
-
|
1177
|
-
|
1178
|
-
|
1179
|
-
|
1180
|
-
|
1181
|
-
|
1182
|
-
|
1183
|
-
|
1184
|
-
|
1185
|
-
|
1186
|
-
|
1187
|
-
|
1188
|
-
|
1189
|
-
|
1190
|
-
|
1191
|
-
|
1192
|
-
|
1193
|
-
|
1194
|
-
|
1195
|
-
|
1196
|
-
|
1197
|
-
|
1198
|
-
|
1199
|
-
|
1200
|
-
|
1201
|
-
|
1202
|
-
|
1203
|
-
|
1204
|
-
|
1205
|
-
|
1206
|
-
|
1207
|
-
|
1208
|
-
|
1209
|
-
|
1210
|
-
|
1211
|
-
|
1212
|
-
|
1213
|
-
|
1214
|
-
|
1215
|
-
|
1216
|
-
|
1217
|
-
|
1218
|
-
|
1219
|
-
|
1220
|
-
|
1221
|
-
|
1222
|
-
|
1223
|
-
|
1224
|
-
|
1225
|
-
|
1226
|
-
|
1227
|
-
|
1228
|
-
|
1229
|
-
|
1230
|
-
|
1231
|
-
|
1232
|
-
|
1233
|
-
|
1234
|
-
|
1235
|
-
|
1236
|
-
|
1237
|
-
|
1238
|
-
|
1239
|
-
|
1240
|
-
|
1241
|
-
|
1242
|
-
|
1243
|
-
|
1244
|
-
|
1245
|
-
|
1246
|
-
|
1247
|
-
|
1248
|
-
|
1249
|
-
|
1250
|
-
|
1251
|
-
|
1252
|
-
|
1253
|
-
|
1254
|
-
|
1255
|
-
|
1256
|
-
|
1257
|
-
|
1258
|
-
|
1259
|
-
|
1260
|
-
|
1261
|
-
|
1262
|
-
|
1263
|
-
|
1264
|
-
|
1265
|
-
|
1266
|
-
|
1267
|
-
|
1268
|
-
|
1269
|
-
|
1270
|
-
|
1271
|
-
|
1272
|
-
|
1273
|
-
|
1274
|
-
|
1275
|
-
|
1276
|
-
|
1277
|
-
|
1278
|
-
|
1279
|
-
|
1280
|
-
|
1281
|
-
|
1282
|
-
|
1283
|
-
|
1284
|
-
|
1285
|
-
|
1286
|
-
|
1287
|
-
|
1288
|
-
|
1289
|
-
|
1290
|
-
|
1291
|
-
|
1292
|
-
|
1293
|
-
|
1294
|
-
|
1295
|
-
|
1296
|
-
|
1297
|
-
|
1298
|
-
|
1299
|
-
|
1300
|
-
|
1301
|
-
|
1302
|
-
|
1303
|
-
|
1304
|
-
|
1305
|
-
|
1306
|
-
|
1307
|
-
|
1308
|
-
|
1309
|
-
|
1310
|
-
|
1311
|
-
|
1312
|
-
|
1313
|
-
|
1314
|
-
|
1315
|
-
|
1316
|
-
|
1317
|
-
|
1318
|
-
|
1319
|
-
|
1320
|
-
|
1321
|
-
|
1322
|
-
|
1323
|
-
|
1324
|
-
|
1325
|
-
|
1326
|
-
|
1327
|
-
|
1328
|
-
|
1329
|
-
|
1330
|
-
|
1331
|
-
|
1332
|
-
|
1333
|
-
|
1334
|
-
|
1335
|
-
|
1336
|
-
|
1337
|
-
|
1338
|
-
|
1339
|
-
|
1340
|
-
|
1341
|
-
|
1342
|
-
|
1343
|
-
|
1344
|
-
|
1345
|
-
|
1346
|
-
|
1347
|
-
|
1348
|
-
|
1349
|
-
|
1350
|
-
|
1351
|
-
|
1352
|
-
|
1353
|
-
|
1354
|
-
|
1355
|
-
|
1356
|
-
|
1357
|
-
|
1358
|
-
|
1359
|
-
|
1360
|
-
|
1361
|
-
|
1362
|
-
|
1363
|
-
|
1364
|
-
|
1365
|
-
|
1366
|
-
|
1367
|
-
|
1368
|
-
|
1369
|
-
|
1370
|
-
|
1371
|
-
|
1372
|
-
|
1373
|
-
|
1374
|
-
|
1375
|
-
|
1376
|
-
|
1377
|
-
|
1378
|
-
|
1379
|
-
|
1380
|
-
|
1381
|
-
|
1382
|
-
|
1383
|
-
|
1384
|
-
|
1385
|
-
|
1386
|
-
|
1387
|
-
|
1388
|
-
|
1389
|
-
|
1390
|
-
|
1391
|
-
|
1392
|
-
|
1393
|
-
|
1394
|
-
|
1395
|
-
|
1396
|
-
|
1397
|
-
|
1398
|
-
|
1399
|
-
|
1400
|
-
|
1401
|
-
}
|
1402
|
-
|
1403
|
-
|
1404
|
-
|
1405
|
-
|
1406
|
-
|
1407
|
-
|
1408
|
-
|
1409
|
-
|
1410
|
-
|
1411
|
-
|
1412
|
-
|
1413
|
-
|
1414
|
-
|
1415
|
-
|
1416
|
-
|
1417
|
-
|
1418
|
-
|
1419
|
-
|
1420
|
-
|
1421
|
-
|
1422
|
-
|
1423
|
-
|
1424
|
-
for (
|
1425
|
-
|
1426
|
-
|
1427
|
-
|
1428
|
-
|
1429
|
-
|
1430
|
-
|
1431
|
-
|
1432
|
-
|
1433
|
-
//
|
1434
|
-
|
1435
|
-
|
1436
|
-
|
1437
|
-
|
1438
|
-
|
1439
|
-
|
1440
|
-
|
1441
|
-
|
1442
|
-
|
1443
|
-
|
1444
|
-
|
1445
|
-
|
1446
|
-
|
1447
|
-
|
1448
|
-
|
1449
|
-
|
1450
|
-
|
1451
|
-
|
1452
|
-
|
1453
|
-
|
1454
|
-
|
1455
|
-
|
1456
|
-
|
1457
|
-
|
1458
|
-
|
1459
|
-
|
1460
|
-
|
1461
|
-
|
1462
|
-
|
1463
|
-
|
1464
|
-
|
1465
|
-
|
1466
|
-
|
1467
|
-
|
1468
|
-
|
1469
|
-
|
1470
|
-
|
1471
|
-
|
1472
|
-
|
1473
|
-
|
1474
|
-
|
1475
|
-
|
1476
|
-
|
1477
|
-
|
1478
|
-
|
1479
|
-
|
1480
|
-
|
1481
|
-
|
1482
|
-
}
|
1483
|
-
|
1484
|
-
|
1485
|
-
|
1486
|
-
|
1487
|
-
|
1488
|
-
|
1489
|
-
|
1490
|
-
|
1491
|
-
|
1492
|
-
|
1493
|
-
|
1494
|
-
|
1495
|
-
|
1496
|
-
|
1497
|
-
|
1498
|
-
|
1499
|
-
|
1500
|
-
|
1501
|
-
|
1502
|
-
|
1503
|
-
|
1504
|
-
|
1505
|
-
|
1506
|
-
|
1507
|
-
|
1508
|
-
|
1509
|
-
|
1510
|
-
|
1511
|
-
|
1512
|
-
|
1513
|
-
|
1514
|
-
|
1515
|
-
|
1516
|
-
|
1517
|
-
|
1518
|
-
|
1519
|
-
|
1520
|
-
|
1521
|
-
|
1522
|
-
|
1523
|
-
|
1524
|
-
|
1525
|
-
|
1526
|
-
|
1527
|
-
|
1528
|
-
|
1529
|
-
|
1530
|
-
|
1531
|
-
|
1532
|
-
|
1533
|
-
|
1534
|
-
|
1535
|
-
|
1536
|
-
|
1537
|
-
|
1538
|
-
|
1539
|
-
|
1540
|
-
|
1541
|
-
|
1542
|
-
|
1543
|
-
|
1544
|
-
|
1545
|
-
|
1546
|
-
|
1547
|
-
|
1548
|
-
|
1549
|
-
|
1550
|
-
|
1551
|
-
|
1552
|
-
|
1553
|
-
|
1554
|
-
|
1555
|
-
|
1556
|
-
|
1557
|
-
|
1558
|
-
|
1559
|
-
|
1560
|
-
|
1561
|
-
|
1562
|
-
|
1563
|
-
text.resize(n_chars);
|
1564
|
-
|
1565
|
-
// NOTE: the original tokenizer decodes bytes after collecting the pieces.
|
1566
|
-
return text;
|
1567
|
-
}
|
1568
|
-
|
1569
|
-
//
|
1570
|
-
// Chat template utils
|
1571
|
-
//
|
1572
|
-
|
1573
|
-
bool common_chat_verify_template(const std::string & tmpl, bool use_jinja) {
|
1574
|
-
if (use_jinja) {
|
1575
|
-
try {
|
1576
|
-
auto chat_template = common_chat_template(tmpl, "<s>", "</s>");
|
1577
|
-
common_chat_inputs inputs;
|
1578
|
-
inputs.messages = json::array({{
|
1579
|
-
{"role", "user"},
|
1580
|
-
{"content", "test"},
|
1581
|
-
}});
|
1582
|
-
common_chat_params_init(chat_template, inputs);
|
1583
|
-
return true;
|
1584
|
-
} catch (const std::exception & e) {
|
1585
|
-
LOG_ERR("%s: failed to apply template: %s\n", __func__, e.what());
|
1586
|
-
return false;
|
1587
|
-
}
|
1588
|
-
}
|
1589
|
-
llama_chat_message chat[] = {{"user", "test"}};
|
1590
|
-
const int res = llama_chat_apply_template(tmpl.c_str(), chat, 1, true, nullptr, 0);
|
1591
|
-
return res >= 0;
|
1592
|
-
}
|
1593
|
-
|
1594
|
-
std::string common_chat_apply_template(
|
1595
|
-
const common_chat_template & tmpl,
|
1596
|
-
const std::vector<common_chat_msg> & msgs,
|
1597
|
-
bool add_ass,
|
1598
|
-
bool use_jinja) {
|
1599
|
-
if (use_jinja) {
|
1600
|
-
auto messages = json::array();
|
1601
|
-
for (const auto & msg : msgs) {
|
1602
|
-
messages.push_back({{"role", msg.role}, {"content", msg.content}});
|
1603
|
-
}
|
1604
|
-
common_chat_inputs inputs;
|
1605
|
-
inputs.messages = messages;
|
1606
|
-
inputs.add_generation_prompt = add_ass;
|
1607
|
-
return common_chat_params_init(tmpl, inputs).prompt;
|
1608
|
-
}
|
1609
|
-
|
1610
|
-
int alloc_size = 0;
|
1611
|
-
std::vector<llama_chat_message> chat;
|
1612
|
-
for (const auto & msg : msgs) {
|
1613
|
-
chat.push_back({msg.role.c_str(), msg.content.c_str()});
|
1614
|
-
alloc_size += (msg.role.size() + msg.content.size()) * 1.25;
|
1615
|
-
}
|
1616
|
-
|
1617
|
-
std::vector<char> buf(alloc_size);
|
1618
|
-
|
1619
|
-
// run the first time to get the total output length
|
1620
|
-
int32_t res = llama_chat_apply_template(tmpl.source().c_str(), chat.data(), chat.size(), add_ass, buf.data(), buf.size());
|
1621
|
-
|
1622
|
-
// error: chat template is not supported
|
1623
|
-
if (res < 0) {
|
1624
|
-
// if the custom "tmpl" is not supported, we throw an error
|
1625
|
-
// this is a bit redundant (for good), since we're not sure if user validated the custom template with llama_chat_verify_template()
|
1626
|
-
throw std::runtime_error("this custom template is not supported");
|
1627
|
-
}
|
1628
|
-
|
1629
|
-
// if it turns out that our buffer is too small, we resize it
|
1630
|
-
if ((size_t) res > buf.size()) {
|
1631
|
-
buf.resize(res);
|
1632
|
-
res = llama_chat_apply_template(tmpl.source().c_str(), chat.data(), chat.size(), add_ass, buf.data(), buf.size());
|
1633
|
-
}
|
1634
|
-
|
1635
|
-
std::string formatted_chat(buf.data(), res);
|
1636
|
-
return formatted_chat;
|
1637
|
-
}
|
1638
|
-
|
1639
|
-
std::string common_chat_format_single(
|
1640
|
-
const common_chat_template & tmpl,
|
1641
|
-
const std::vector<common_chat_msg> & past_msg,
|
1642
|
-
const common_chat_msg & new_msg,
|
1643
|
-
bool add_ass,
|
1644
|
-
bool use_jinja) {
|
1645
|
-
std::ostringstream ss;
|
1646
|
-
auto fmt_past_msg = past_msg.empty() ? "" : common_chat_apply_template(tmpl, past_msg, false, use_jinja);
|
1647
|
-
std::vector<common_chat_msg> chat_new(past_msg);
|
1648
|
-
// if the past_msg ends with a newline, we must preserve it in the formatted version
|
1649
|
-
if (add_ass && !fmt_past_msg.empty() && fmt_past_msg.back() == '\n') {
|
1650
|
-
ss << "\n";
|
1651
|
-
};
|
1652
|
-
// format chat with new_msg
|
1653
|
-
chat_new.push_back(new_msg);
|
1654
|
-
auto fmt_new_msg = common_chat_apply_template(tmpl, chat_new, add_ass, use_jinja);
|
1655
|
-
// get the diff part
|
1656
|
-
ss << fmt_new_msg.substr(fmt_past_msg.size(), fmt_new_msg.size() - fmt_past_msg.size());
|
1657
|
-
return ss.str();
|
1658
|
-
}
|
1659
|
-
|
1660
|
-
std::string common_chat_format_example(const common_chat_template & tmpl, bool use_jinja) {
|
1661
|
-
std::vector<common_chat_msg> msgs = {
|
1662
|
-
{"system", "You are a helpful assistant", {}},
|
1663
|
-
{"user", "Hello", {}},
|
1664
|
-
{"assistant", "Hi there", {}},
|
1665
|
-
{"user", "How are you?", {}},
|
1666
|
-
};
|
1667
|
-
return common_chat_apply_template(tmpl, msgs, true, use_jinja);
|
1668
|
-
}
|
1669
|
-
|
1670
|
-
#define CHATML_TEMPLATE_SRC \
|
1671
|
-
"{%- for message in messages -%}\n" \
|
1672
|
-
" {{- '<|im_start|>' + message.role + '\n' + message.content + '<|im_end|>\n' -}}\n" \
|
1673
|
-
"{%- endfor -%}\n" \
|
1674
|
-
"{%- if add_generation_prompt -%}\n" \
|
1675
|
-
" {{- '<|im_start|>assistant\n' -}}\n" \
|
1676
|
-
"{%- endif -%}"
|
1677
|
-
|
1678
|
-
common_chat_templates common_chat_templates_from_model(const struct llama_model * model, const std::string & chat_template_override)
|
1679
|
-
{
|
1680
|
-
std::string default_template_src;
|
1681
|
-
std::string template_tool_use_src;
|
1682
|
-
|
1683
|
-
bool has_explicit_template = !chat_template_override.empty();
|
1684
|
-
if (chat_template_override.empty()) {
|
1685
|
-
auto str = llama_model_chat_template(model, /* name */ nullptr);
|
1686
|
-
if (str) {
|
1687
|
-
default_template_src = str;
|
1688
|
-
has_explicit_template = true;
|
1689
|
-
}
|
1690
|
-
str = llama_model_chat_template(model, /* name */ "tool_use");
|
1691
|
-
if (str) {
|
1692
|
-
template_tool_use_src = str;
|
1693
|
-
has_explicit_template = true;
|
1694
|
-
}
|
1695
|
-
} else {
|
1696
|
-
default_template_src = chat_template_override;
|
1697
|
-
}
|
1698
|
-
if (default_template_src.empty() || default_template_src == "chatml") {
|
1699
|
-
if (!template_tool_use_src.empty()) {
|
1700
|
-
default_template_src = template_tool_use_src;
|
1701
|
-
} else {
|
1702
|
-
default_template_src = CHATML_TEMPLATE_SRC;
|
1703
|
-
}
|
1704
|
-
}
|
1705
|
-
auto vocab = llama_model_get_vocab(model);
|
1706
|
-
const auto get_token = [&](llama_token token, const char * name, const char * jinja_variable_name) {
|
1707
|
-
if (token == LLAMA_TOKEN_NULL) {
|
1708
|
-
if (default_template_src.find(jinja_variable_name) != std::string::npos
|
1709
|
-
|| template_tool_use_src.find(jinja_variable_name) != std::string::npos) {
|
1710
|
-
LOG_WRN("%s: warning: vocab does not have a %s token, jinja template won't work as intended.\n", __func__, name);
|
1711
|
-
}
|
1712
|
-
return std::string();
|
1713
|
-
} else {
|
1714
|
-
return common_token_to_piece(vocab, token, true);
|
1715
|
-
}
|
1716
|
-
};
|
1717
|
-
auto token_bos = get_token(llama_vocab_bos(vocab), "BOS", "bos_token");
|
1718
|
-
auto token_eos = get_token(llama_vocab_eos(vocab), "EOS", "eos_token");
|
1719
|
-
try {
|
1720
|
-
return {
|
1721
|
-
has_explicit_template,
|
1722
|
-
std::make_unique<minja::chat_template>(default_template_src, token_bos, token_eos),
|
1723
|
-
template_tool_use_src.empty()
|
1724
|
-
? nullptr
|
1725
|
-
: std::make_unique<minja::chat_template>(template_tool_use_src, token_bos, token_eos),
|
1726
|
-
};
|
1727
|
-
} catch (const std::exception & e) {
|
1728
|
-
LOG_ERR("%s: failed to parse chat template: %s\n", __func__, e.what());
|
1729
|
-
return {
|
1730
|
-
has_explicit_template,
|
1731
|
-
std::make_unique<minja::chat_template>(CHATML_TEMPLATE_SRC, token_bos, token_eos),
|
1732
|
-
nullptr,
|
1733
|
-
};
|
1734
|
-
}
|
1735
|
-
}
|
1736
|
-
|
1737
|
-
//
|
1738
|
-
// KV cache utils
|
1739
|
-
//
|
1740
|
-
|
1741
|
-
void common_kv_cache_dump_view(const llama_kv_cache_view & view, int row_size) {
|
1742
|
-
static const char slot_chars[] = ".123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+";
|
1743
|
-
|
1744
|
-
printf("=== Dumping KV cache. total cells %d, max sequences per cell %d, populated cells %d, total tokens in cache %d, largest empty slot=%d @ %d",
|
1745
|
-
view.n_cells, view.n_seq_max, view.used_cells, view.token_count, view.max_contiguous, view.max_contiguous_idx);
|
1746
|
-
|
1747
|
-
llama_kv_cache_view_cell * c_curr = view.cells;
|
1748
|
-
llama_seq_id * cs_curr = view.cells_sequences;
|
1749
|
-
|
1750
|
-
for (int i = 0; i < view.n_cells; i++, c_curr++, cs_curr += view.n_seq_max) {
|
1751
|
-
if (i % row_size == 0) {
|
1752
|
-
printf("\n%5d: ", i);
|
1753
|
-
}
|
1754
|
-
int seq_count = 0;
|
1755
|
-
for (int j = 0; j < view.n_seq_max; j++) {
|
1756
|
-
if (cs_curr[j] >= 0) { seq_count++; }
|
1757
|
-
}
|
1758
|
-
putchar(slot_chars[std::min(sizeof(slot_chars) - 2, size_t(seq_count))]);
|
1759
|
-
}
|
1760
|
-
|
1761
|
-
printf("\n=== Done dumping\n");
|
1762
|
-
}
|
1763
|
-
|
1764
|
-
void common_kv_cache_dump_view_seqs(const llama_kv_cache_view & view, int row_size) {
|
1765
|
-
static const char slot_chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
1766
|
-
|
1767
|
-
printf("=== Dumping KV cache. total cells %d, max sequences per cell %d, populated cells %d, total tokens in cache %d, largest empty slot=%d @ %d\n",
|
1768
|
-
view.n_cells, view.n_seq_max, view.used_cells, view.token_count, view.max_contiguous, view.max_contiguous_idx);
|
1769
|
-
|
1770
|
-
std::unordered_map<llama_seq_id, size_t> seqs;
|
1771
|
-
llama_kv_cache_view_cell * c_curr = view.cells;
|
1772
|
-
llama_seq_id * cs_curr = view.cells_sequences;
|
1773
|
-
|
1774
|
-
for (int i = 0; i < view.n_cells; i++, c_curr++, cs_curr += view.n_seq_max) {
|
1775
|
-
for (int j = 0; j < view.n_seq_max; j++) {
|
1776
|
-
if (cs_curr[j] < 0) { continue; }
|
1777
|
-
if (seqs.find(cs_curr[j]) == seqs.end()) {
|
1778
|
-
if (seqs.size() + 1 >= sizeof(slot_chars)) { break; }
|
1779
|
-
const size_t sz = seqs.size();
|
1780
|
-
seqs[cs_curr[j]] = sz;
|
1781
|
-
}
|
1782
|
-
}
|
1783
|
-
if (seqs.size() + 1 >= sizeof(slot_chars)) { break; }
|
1784
|
-
}
|
1785
|
-
|
1786
|
-
printf("=== Sequence legend: ");
|
1787
|
-
for (const auto & it : seqs) {
|
1788
|
-
printf("%zu=%d, ", it.second, it.first);
|
1789
|
-
}
|
1790
|
-
printf("'+'=other sequence ids");
|
1791
|
-
|
1792
|
-
c_curr = view.cells;
|
1793
|
-
cs_curr = view.cells_sequences;
|
1794
|
-
for (int i = 0; i < view.n_cells; i++, c_curr++, cs_curr += view.n_seq_max) {
|
1795
|
-
if (i % row_size == 0) {
|
1796
|
-
printf("\n%5d: ", i);
|
1797
|
-
}
|
1798
|
-
for (int j = 0; j < view.n_seq_max; j++) {
|
1799
|
-
if (cs_curr[j] >= 0) {
|
1800
|
-
const auto & it = seqs.find(cs_curr[j]);
|
1801
|
-
putchar(it != seqs.end() ? int(slot_chars[it->second]) : '+');
|
1802
|
-
} else {
|
1803
|
-
putchar('.');
|
1804
|
-
}
|
1805
|
-
}
|
1806
|
-
putchar(' ');
|
1807
|
-
}
|
1808
|
-
|
1809
|
-
printf("\n=== Done dumping\n");
|
1810
|
-
}
|
1811
|
-
|
1812
|
-
//
|
1813
|
-
// Embedding utils
|
1814
|
-
//
|
1815
|
-
|
1816
|
-
void common_embd_normalize(const float * inp, float * out, int n, int embd_norm) {
|
1817
|
-
double sum = 0.0;
|
1818
|
-
|
1819
|
-
switch (embd_norm) {
|
1820
|
-
case -1: // no normalisation
|
1821
|
-
sum = 1.0;
|
1822
|
-
break;
|
1823
|
-
case 0: // max absolute
|
1824
|
-
for (int i = 0; i < n; i++) {
|
1825
|
-
if (sum < std::abs(inp[i])) {
|
1826
|
-
sum = std::abs(inp[i]);
|
1827
|
-
}
|
1828
|
-
}
|
1829
|
-
sum /= 32760.0; // make an int16 range
|
1830
|
-
break;
|
1831
|
-
case 2: // euclidean
|
1832
|
-
for (int i = 0; i < n; i++) {
|
1833
|
-
sum += inp[i] * inp[i];
|
1834
|
-
}
|
1835
|
-
sum = std::sqrt(sum);
|
1836
|
-
break;
|
1837
|
-
default: // p-norm (euclidean is p-norm p=2)
|
1838
|
-
for (int i = 0; i < n; i++) {
|
1839
|
-
sum += std::pow(std::abs(inp[i]), embd_norm);
|
1840
|
-
}
|
1841
|
-
sum = std::pow(sum, 1.0 / embd_norm);
|
1842
|
-
break;
|
1843
|
-
}
|
1844
|
-
|
1845
|
-
const float norm = sum > 0.0 ? 1.0 / sum : 0.0f;
|
1846
|
-
|
1847
|
-
for (int i = 0; i < n; i++) {
|
1848
|
-
out[i] = inp[i] * norm;
|
1849
|
-
}
|
1850
|
-
}
|
1851
|
-
|
1852
|
-
float common_embd_similarity_cos(const float * embd1, const float * embd2, int n){
|
1853
|
-
double sum = 0.0;
|
1854
|
-
double sum1 = 0.0;
|
1855
|
-
double sum2 = 0.0;
|
1856
|
-
|
1857
|
-
for (int i = 0; i < n; i++) {
|
1858
|
-
sum += embd1[i] * embd2[i];
|
1859
|
-
sum1 += embd1[i] * embd1[i];
|
1860
|
-
sum2 += embd2[i] * embd2[i];
|
1861
|
-
}
|
1862
|
-
|
1863
|
-
// Handle the case where one or both vectors are zero vectors
|
1864
|
-
if (sum1 == 0.0 || sum2 == 0.0) {
|
1865
|
-
if (sum1 == 0.0 && sum2 == 0.0) {
|
1866
|
-
return 1.0f; // two zero vectors are similar
|
1867
|
-
}
|
1868
|
-
return 0.0f;
|
1869
|
-
}
|
1870
|
-
|
1871
|
-
return sum / (sqrt(sum1) * sqrt(sum2));
|
1872
|
-
}
|
1873
|
-
|
1874
|
-
//
|
1875
|
-
// Control vector utils
|
1876
|
-
//
|
1877
|
-
|
1878
|
-
static common_control_vector_data common_control_vector_load_one(const common_control_vector_load_info & load_info) {
|
1879
|
-
common_control_vector_data result = { -1, {} };
|
1880
|
-
|
1881
|
-
lm_ggml_context * ctx = nullptr;
|
1882
|
-
struct lm_gguf_init_params meta_lm_gguf_params = {
|
1883
|
-
/* .no_alloc = */ false,
|
1884
|
-
/* .ctx = */ &ctx,
|
1885
|
-
};
|
1886
|
-
struct lm_gguf_context * ctx_gguf = lm_gguf_init_from_file(load_info.fname.c_str(), meta_lm_gguf_params);
|
1887
|
-
if (!ctx_gguf) {
|
1888
|
-
LOG_ERR("%s: failed to load control vector file from %s\n", __func__, load_info.fname.c_str());
|
1889
|
-
return result;
|
1890
|
-
}
|
1891
|
-
|
1892
|
-
int32_t n_tensors = lm_gguf_get_n_tensors(ctx_gguf);
|
1893
|
-
if (n_tensors == 0) {
|
1894
|
-
LOG_WRN("%s: no direction tensors found in %s\n", __func__, load_info.fname.c_str());
|
1895
|
-
}
|
1896
|
-
|
1897
|
-
for (int i = 0; i < n_tensors; i++) {
|
1898
|
-
std::string name = lm_gguf_get_tensor_name(ctx_gguf, i);
|
1899
|
-
|
1900
|
-
int layer_idx = -1;
|
1901
|
-
|
1902
|
-
// split on '.'
|
1903
|
-
size_t dotpos = name.find('.');
|
1904
|
-
if (dotpos != std::string::npos && name.substr(0, dotpos) == "direction") {
|
1905
|
-
try {
|
1906
|
-
layer_idx = std::stoi(name.substr(dotpos + 1));
|
1907
|
-
} catch (...) {
|
1908
|
-
layer_idx = -1;
|
1909
|
-
}
|
1910
|
-
}
|
1911
|
-
if (layer_idx < 0) {
|
1912
|
-
LOG_ERR("%s: invalid/unparsable direction tensor layer index in %s\n", __func__, load_info.fname.c_str());
|
1913
|
-
result.n_embd = -1;
|
1914
|
-
break;
|
1915
|
-
} else if (layer_idx == 0) {
|
1916
|
-
LOG_ERR("%s: invalid (zero) direction tensor layer index in %s\n", __func__, load_info.fname.c_str());
|
1917
|
-
result.n_embd = -1;
|
1918
|
-
break;
|
1919
|
-
}
|
1920
|
-
|
1921
|
-
struct lm_ggml_tensor * tensor = lm_ggml_get_tensor(ctx, name.c_str());
|
1922
|
-
if (tensor->type != LM_GGML_TYPE_F32) {
|
1923
|
-
LOG_ERR("%s: invalid (non-F32) direction tensor type in %s\n", __func__, load_info.fname.c_str());
|
1924
|
-
result.n_embd = -1;
|
1925
|
-
break;
|
1926
|
-
}
|
1927
|
-
if (lm_ggml_n_dims(tensor) != 1) {
|
1928
|
-
LOG_ERR("%s: invalid (non-1D) direction tensor shape in %s\n", __func__, load_info.fname.c_str());
|
1929
|
-
result.n_embd = -1;
|
1930
|
-
break;
|
1931
|
-
}
|
1932
|
-
|
1933
|
-
if (result.n_embd == -1) {
|
1934
|
-
result.n_embd = lm_ggml_nelements(tensor);
|
1935
|
-
} else if (lm_ggml_nelements(tensor) != result.n_embd) {
|
1936
|
-
LOG_ERR("%s: direction tensor in %s does not match previous dimensions\n", __func__, load_info.fname.c_str());
|
1937
|
-
result.n_embd = -1;
|
1938
|
-
break;
|
1939
|
-
}
|
1940
|
-
|
1941
|
-
// extend if necessary - do not store data for layer 0 (it's not used)
|
1942
|
-
result.data.resize(std::max(result.data.size(), static_cast<size_t>(result.n_embd * layer_idx)), 0.0f);
|
1943
|
-
|
1944
|
-
const float * src = (const float *) tensor->data;
|
1945
|
-
float * dst = result.data.data() + result.n_embd * (layer_idx - 1); // layer 1 at [0]
|
1946
|
-
for (int j = 0; j < result.n_embd; j++) {
|
1947
|
-
dst[j] += src[j] * load_info.strength; // allows multiple directions for same layer in same file
|
1948
|
-
}
|
1949
|
-
|
1950
|
-
}
|
1951
|
-
|
1952
|
-
if (result.n_embd == -1) {
|
1953
|
-
LOG_WRN("%s: skipping %s due to invalid direction tensors\n", __func__, load_info.fname.c_str());
|
1954
|
-
result.data.clear();
|
1955
|
-
}
|
1956
|
-
|
1957
|
-
lm_gguf_free(ctx_gguf);
|
1958
|
-
lm_ggml_free(ctx);
|
1959
|
-
|
1960
|
-
return result;
|
1961
|
-
}
|
1962
|
-
|
1963
|
-
common_control_vector_data common_control_vector_load(const std::vector<common_control_vector_load_info> & load_infos) {
|
1964
|
-
common_control_vector_data result = { -1, {} };
|
1965
|
-
|
1966
|
-
for (const auto & info : load_infos) {
|
1967
|
-
auto cur = common_control_vector_load_one(info);
|
1968
|
-
|
1969
|
-
if (cur.n_embd == -1) {
|
1970
|
-
result.n_embd = -1;
|
1971
|
-
break;
|
1972
|
-
}
|
1973
|
-
if (result.n_embd != -1 && result.n_embd != cur.n_embd) {
|
1974
|
-
LOG_ERR("%s: control vectors in %s does not match previous dimensions\n", __func__, info.fname.c_str());
|
1975
|
-
result.n_embd = -1;
|
1976
|
-
break;
|
1977
|
-
}
|
1978
|
-
|
1979
|
-
if (result.n_embd == -1) {
|
1980
|
-
result = std::move(cur);
|
1981
|
-
} else {
|
1982
|
-
result.data.resize(std::max(result.data.size(), cur.data.size()), 0.0f); // extend if necessary
|
1983
|
-
for (size_t i = 0; i < cur.data.size(); i++) {
|
1984
|
-
result.data[i] += cur.data[i];
|
1985
|
-
}
|
1986
|
-
}
|
1987
|
-
}
|
1988
|
-
|
1989
|
-
if (result.n_embd == -1) {
|
1990
|
-
LOG_ERR("%s: no valid control vector files passed\n", __func__);
|
1991
|
-
result.data.clear();
|
1992
|
-
}
|
1993
|
-
|
1994
|
-
return result;
|
1995
|
-
}
|
1996
|
-
|
1
|
+
#if defined(_MSC_VER)
|
2
|
+
#define _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING
|
3
|
+
#endif
|
4
|
+
|
5
|
+
#include "ggml.h"
|
6
|
+
#include "gguf.h"
|
7
|
+
|
8
|
+
#include "common.h"
|
9
|
+
#include "log.h"
|
10
|
+
#include "llama.h"
|
11
|
+
|
12
|
+
#include <algorithm>
|
13
|
+
#include <cinttypes>
|
14
|
+
#include <climits>
|
15
|
+
#include <cmath>
|
16
|
+
#include <codecvt>
|
17
|
+
#include <cstdarg>
|
18
|
+
#include <cstring>
|
19
|
+
#include <ctime>
|
20
|
+
#include <filesystem>
|
21
|
+
#include <fstream>
|
22
|
+
#include <iostream>
|
23
|
+
#include <iterator>
|
24
|
+
#include <regex>
|
25
|
+
#include <sstream>
|
26
|
+
#include <string>
|
27
|
+
#include <thread>
|
28
|
+
#include <unordered_map>
|
29
|
+
#include <unordered_set>
|
30
|
+
#include <vector>
|
31
|
+
|
32
|
+
#if defined(__APPLE__) && defined(__MACH__)
|
33
|
+
#include <sys/types.h>
|
34
|
+
#include <sys/sysctl.h>
|
35
|
+
#endif
|
36
|
+
|
37
|
+
#if defined(_WIN32)
|
38
|
+
#define WIN32_LEAN_AND_MEAN
|
39
|
+
#ifndef NOMINMAX
|
40
|
+
# define NOMINMAX
|
41
|
+
#endif
|
42
|
+
#include <locale>
|
43
|
+
#include <windows.h>
|
44
|
+
#include <fcntl.h>
|
45
|
+
#include <io.h>
|
46
|
+
#else
|
47
|
+
#include <sys/ioctl.h>
|
48
|
+
#include <sys/stat.h>
|
49
|
+
#include <unistd.h>
|
50
|
+
|
51
|
+
#endif
|
52
|
+
|
53
|
+
// build info
|
54
|
+
int LLAMA_BUILD_NUMBER = 0;
|
55
|
+
char const *LLAMA_COMMIT = "unknown";
|
56
|
+
char const *LLAMA_COMPILER = "unknown";
|
57
|
+
char const *LLAMA_BUILD_TARGET = "unknown";
|
58
|
+
|
59
|
+
#if defined(_MSC_VER)
|
60
|
+
#pragma warning(disable: 4244 4267) // possible loss of data
|
61
|
+
#endif
|
62
|
+
|
63
|
+
//
|
64
|
+
// CPU utils
|
65
|
+
//
|
66
|
+
|
67
|
+
int32_t cpu_get_num_physical_cores() {
|
68
|
+
#ifdef __linux__
|
69
|
+
// enumerate the set of thread siblings, num entries is num cores
|
70
|
+
std::unordered_set<std::string> siblings;
|
71
|
+
for (uint32_t cpu=0; cpu < UINT32_MAX; ++cpu) {
|
72
|
+
std::ifstream thread_siblings("/sys/devices/system/cpu/cpu"
|
73
|
+
+ std::to_string(cpu) + "/topology/thread_siblings");
|
74
|
+
if (!thread_siblings.is_open()) {
|
75
|
+
break; // no more cpus
|
76
|
+
}
|
77
|
+
std::string line;
|
78
|
+
if (std::getline(thread_siblings, line)) {
|
79
|
+
siblings.insert(line);
|
80
|
+
}
|
81
|
+
}
|
82
|
+
if (!siblings.empty()) {
|
83
|
+
return static_cast<int32_t>(siblings.size());
|
84
|
+
}
|
85
|
+
#elif defined(__APPLE__) && defined(__MACH__)
|
86
|
+
int32_t num_physical_cores;
|
87
|
+
size_t len = sizeof(num_physical_cores);
|
88
|
+
int result = sysctlbyname("hw.perflevel0.physicalcpu", &num_physical_cores, &len, NULL, 0);
|
89
|
+
if (result == 0) {
|
90
|
+
return num_physical_cores;
|
91
|
+
}
|
92
|
+
result = sysctlbyname("hw.physicalcpu", &num_physical_cores, &len, NULL, 0);
|
93
|
+
if (result == 0) {
|
94
|
+
return num_physical_cores;
|
95
|
+
}
|
96
|
+
#elif defined(_WIN32) && (_WIN32_WINNT >= 0x0601) && !defined(__MINGW64__) // windows 7 and later
|
97
|
+
// TODO: windows + arm64 + mingw64
|
98
|
+
unsigned int n_threads_win = std::thread::hardware_concurrency();
|
99
|
+
unsigned int default_threads = n_threads_win > 0 ? (n_threads_win <= 4 ? n_threads_win : n_threads_win / 2) : 4;
|
100
|
+
|
101
|
+
DWORD buffer_size = 0;
|
102
|
+
if (!GetLogicalProcessorInformationEx(RelationProcessorCore, nullptr, &buffer_size)) {
|
103
|
+
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
|
104
|
+
return default_threads;
|
105
|
+
}
|
106
|
+
}
|
107
|
+
|
108
|
+
std::vector<char> buffer(buffer_size);
|
109
|
+
if (!GetLogicalProcessorInformationEx(RelationProcessorCore, reinterpret_cast<PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX>(buffer.data()), &buffer_size)) {
|
110
|
+
return default_threads;
|
111
|
+
}
|
112
|
+
|
113
|
+
int32_t num_physical_cores = 0;
|
114
|
+
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX info = reinterpret_cast<PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX>(buffer.data());
|
115
|
+
while (buffer_size > 0) {
|
116
|
+
if (info->Relationship == RelationProcessorCore) {
|
117
|
+
num_physical_cores += info->Processor.GroupCount;
|
118
|
+
}
|
119
|
+
buffer_size -= info->Size;
|
120
|
+
info = reinterpret_cast<PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX>(reinterpret_cast<char*>(info) + info->Size);
|
121
|
+
}
|
122
|
+
|
123
|
+
return num_physical_cores > 0 ? num_physical_cores : default_threads;
|
124
|
+
#endif
|
125
|
+
unsigned int n_threads = std::thread::hardware_concurrency();
|
126
|
+
return n_threads > 0 ? (n_threads <= 4 ? n_threads : n_threads / 2) : 4;
|
127
|
+
}
|
128
|
+
|
129
|
+
#if defined(__x86_64__) && defined(__linux__) && !defined(__ANDROID__)
|
130
|
+
#include <pthread.h>
|
131
|
+
|
132
|
+
static void cpuid(unsigned leaf, unsigned subleaf,
|
133
|
+
unsigned *eax, unsigned *ebx, unsigned *ecx, unsigned *edx) {
|
134
|
+
__asm__("movq\t%%rbx,%%rsi\n\t"
|
135
|
+
"cpuid\n\t"
|
136
|
+
"xchgq\t%%rbx,%%rsi"
|
137
|
+
: "=a"(*eax), "=S"(*ebx), "=c"(*ecx), "=d"(*edx)
|
138
|
+
: "0"(leaf), "2"(subleaf));
|
139
|
+
}
|
140
|
+
|
141
|
+
static int pin_cpu(int cpu) {
|
142
|
+
cpu_set_t mask;
|
143
|
+
CPU_ZERO(&mask);
|
144
|
+
CPU_SET(cpu, &mask);
|
145
|
+
return pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask);
|
146
|
+
}
|
147
|
+
|
148
|
+
static bool is_hybrid_cpu(void) {
|
149
|
+
unsigned eax, ebx, ecx, edx;
|
150
|
+
cpuid(7, 0, &eax, &ebx, &ecx, &edx);
|
151
|
+
return !!(edx & (1u << 15));
|
152
|
+
}
|
153
|
+
|
154
|
+
static bool is_running_on_efficiency_core(void) {
|
155
|
+
unsigned eax, ebx, ecx, edx;
|
156
|
+
cpuid(0x1a, 0, &eax, &ebx, &ecx, &edx);
|
157
|
+
int intel_atom = 0x20;
|
158
|
+
int core_type = (eax & 0xff000000u) >> 24;
|
159
|
+
return core_type == intel_atom;
|
160
|
+
}
|
161
|
+
|
162
|
+
static int cpu_count_math_cpus(int n_cpu) {
|
163
|
+
int result = 0;
|
164
|
+
for (int cpu = 0; cpu < n_cpu; ++cpu) {
|
165
|
+
if (pin_cpu(cpu)) {
|
166
|
+
return -1;
|
167
|
+
}
|
168
|
+
if (is_running_on_efficiency_core()) {
|
169
|
+
continue; // efficiency cores harm lockstep threading
|
170
|
+
}
|
171
|
+
++cpu; // hyperthreading isn't useful for linear algebra
|
172
|
+
++result;
|
173
|
+
}
|
174
|
+
return result;
|
175
|
+
}
|
176
|
+
|
177
|
+
#endif // __x86_64__ && __linux__
|
178
|
+
|
179
|
+
/**
|
180
|
+
* Returns number of CPUs on system that are useful for math.
|
181
|
+
*/
|
182
|
+
int32_t cpu_get_num_math() {
|
183
|
+
#if defined(__x86_64__) && defined(__linux__) && !defined(__ANDROID__)
|
184
|
+
int n_cpu = sysconf(_SC_NPROCESSORS_ONLN);
|
185
|
+
if (n_cpu < 1) {
|
186
|
+
return cpu_get_num_physical_cores();
|
187
|
+
}
|
188
|
+
if (is_hybrid_cpu()) {
|
189
|
+
cpu_set_t affinity;
|
190
|
+
if (!pthread_getaffinity_np(pthread_self(), sizeof(affinity), &affinity)) {
|
191
|
+
int result = cpu_count_math_cpus(n_cpu);
|
192
|
+
pthread_setaffinity_np(pthread_self(), sizeof(affinity), &affinity);
|
193
|
+
if (result > 0) {
|
194
|
+
return result;
|
195
|
+
}
|
196
|
+
}
|
197
|
+
}
|
198
|
+
#endif
|
199
|
+
return cpu_get_num_physical_cores();
|
200
|
+
}
|
201
|
+
|
202
|
+
// Helper for setting process priority
|
203
|
+
|
204
|
+
#if defined(_WIN32)
|
205
|
+
|
206
|
+
bool set_process_priority(enum lm_ggml_sched_priority prio) {
|
207
|
+
if (prio == LM_GGML_SCHED_PRIO_NORMAL) {
|
208
|
+
return true;
|
209
|
+
}
|
210
|
+
|
211
|
+
DWORD p = NORMAL_PRIORITY_CLASS;
|
212
|
+
switch (prio) {
|
213
|
+
case LM_GGML_SCHED_PRIO_NORMAL: p = NORMAL_PRIORITY_CLASS; break;
|
214
|
+
case LM_GGML_SCHED_PRIO_MEDIUM: p = ABOVE_NORMAL_PRIORITY_CLASS; break;
|
215
|
+
case LM_GGML_SCHED_PRIO_HIGH: p = HIGH_PRIORITY_CLASS; break;
|
216
|
+
case LM_GGML_SCHED_PRIO_REALTIME: p = REALTIME_PRIORITY_CLASS; break;
|
217
|
+
}
|
218
|
+
|
219
|
+
if (!SetPriorityClass(GetCurrentProcess(), p)) {
|
220
|
+
LOG_WRN("failed to set process priority class %d : (%d)\n", prio, (int) GetLastError());
|
221
|
+
return false;
|
222
|
+
}
|
223
|
+
|
224
|
+
return true;
|
225
|
+
}
|
226
|
+
|
227
|
+
#else // MacOS and POSIX
|
228
|
+
#include <sys/types.h>
|
229
|
+
#include <sys/resource.h>
|
230
|
+
|
231
|
+
bool set_process_priority(enum lm_ggml_sched_priority prio) {
|
232
|
+
if (prio == LM_GGML_SCHED_PRIO_NORMAL) {
|
233
|
+
return true;
|
234
|
+
}
|
235
|
+
|
236
|
+
int p = 0;
|
237
|
+
switch (prio) {
|
238
|
+
case LM_GGML_SCHED_PRIO_NORMAL: p = 0; break;
|
239
|
+
case LM_GGML_SCHED_PRIO_MEDIUM: p = -5; break;
|
240
|
+
case LM_GGML_SCHED_PRIO_HIGH: p = -10; break;
|
241
|
+
case LM_GGML_SCHED_PRIO_REALTIME: p = -20; break;
|
242
|
+
}
|
243
|
+
|
244
|
+
if (!setpriority(PRIO_PROCESS, 0, p)) {
|
245
|
+
LOG_WRN("failed to set process priority %d : %s (%d)\n", prio, strerror(errno), errno);
|
246
|
+
return false;
|
247
|
+
}
|
248
|
+
return true;
|
249
|
+
}
|
250
|
+
|
251
|
+
#endif
|
252
|
+
|
253
|
+
//
|
254
|
+
// CLI argument parsing
|
255
|
+
//
|
256
|
+
|
257
|
+
|
258
|
+
void postprocess_cpu_params(cpu_params& cpuparams, const cpu_params* role_model) {
|
259
|
+
int32_t n_set = 0;
|
260
|
+
|
261
|
+
if (cpuparams.n_threads < 0) {
|
262
|
+
// Assuming everything about cpuparams is invalid
|
263
|
+
if (role_model != nullptr) {
|
264
|
+
cpuparams = *role_model;
|
265
|
+
} else {
|
266
|
+
cpuparams.n_threads = cpu_get_num_math();
|
267
|
+
}
|
268
|
+
}
|
269
|
+
|
270
|
+
for (int32_t i = 0; i < LM_GGML_MAX_N_THREADS; i++) {
|
271
|
+
if (cpuparams.cpumask[i]) {
|
272
|
+
n_set++;
|
273
|
+
}
|
274
|
+
}
|
275
|
+
|
276
|
+
if (n_set && n_set < cpuparams.n_threads) {
|
277
|
+
// Not enough set bits, may experience performance issues.
|
278
|
+
LOG_WRN("Not enough set bits in CPU mask (%d) to satisfy requested thread count: %d\n", n_set, cpuparams.n_threads);
|
279
|
+
}
|
280
|
+
}
|
281
|
+
|
282
|
+
bool parse_cpu_range(const std::string & range, bool (&boolmask)[LM_GGML_MAX_N_THREADS]) {
|
283
|
+
size_t dash_loc = range.find('-');
|
284
|
+
if (dash_loc == std::string::npos) {
|
285
|
+
LOG_ERR("Format of CPU range is invalid! Expected [<start>]-[<end>].\n");
|
286
|
+
return false;
|
287
|
+
}
|
288
|
+
|
289
|
+
size_t start_i;
|
290
|
+
size_t end_i;
|
291
|
+
|
292
|
+
if (dash_loc == 0) {
|
293
|
+
start_i = 0;
|
294
|
+
} else {
|
295
|
+
start_i = std::stoull(range.substr(0, dash_loc));
|
296
|
+
if (start_i >= LM_GGML_MAX_N_THREADS) {
|
297
|
+
LOG_ERR("Start index out of bounds!\n");
|
298
|
+
return false;
|
299
|
+
}
|
300
|
+
}
|
301
|
+
|
302
|
+
if (dash_loc == range.length() - 1) {
|
303
|
+
end_i = LM_GGML_MAX_N_THREADS - 1;
|
304
|
+
} else {
|
305
|
+
end_i = std::stoull(range.substr(dash_loc + 1));
|
306
|
+
if (end_i >= LM_GGML_MAX_N_THREADS) {
|
307
|
+
LOG_ERR("End index out of bounds!\n");
|
308
|
+
return false;
|
309
|
+
}
|
310
|
+
}
|
311
|
+
|
312
|
+
for (size_t i = start_i; i <= end_i; i++) {
|
313
|
+
boolmask[i] = true;
|
314
|
+
}
|
315
|
+
|
316
|
+
return true;
|
317
|
+
}
|
318
|
+
|
319
|
+
bool parse_cpu_mask(const std::string & mask, bool (&boolmask)[LM_GGML_MAX_N_THREADS]) {
|
320
|
+
// Discard potential 0x prefix
|
321
|
+
size_t start_i = 0;
|
322
|
+
if (mask.length() >= 2 && mask.substr(0, 2) == "0x") {
|
323
|
+
start_i = 2;
|
324
|
+
}
|
325
|
+
|
326
|
+
size_t num_digits = mask.length() - start_i;
|
327
|
+
if (num_digits > 128) num_digits = 128;
|
328
|
+
|
329
|
+
size_t end_i = num_digits + start_i;
|
330
|
+
|
331
|
+
for (size_t i = start_i, n = (num_digits*4 - 1); i < end_i; i++, n-=4) {
|
332
|
+
char c = mask.at(i);
|
333
|
+
int8_t id = c;
|
334
|
+
|
335
|
+
if ((c >= '0' && c <= '9')) {
|
336
|
+
id -= '0';
|
337
|
+
} else if (c >= 'a' && c <= 'f') {
|
338
|
+
id -= 'a' - 10;
|
339
|
+
} else if (c >= 'A' && c <= 'F') {
|
340
|
+
id -= 'A' - 10;
|
341
|
+
} else {
|
342
|
+
LOG_ERR("Invalid hex character '%c' at position %d\n", c, int32_t(i));
|
343
|
+
return false;
|
344
|
+
}
|
345
|
+
|
346
|
+
boolmask[ n ] = boolmask[ n ] || ((id & 8) != 0);
|
347
|
+
boolmask[n - 1] = boolmask[n - 1] || ((id & 4) != 0);
|
348
|
+
boolmask[n - 2] = boolmask[n - 2] || ((id & 2) != 0);
|
349
|
+
boolmask[n - 3] = boolmask[n - 3] || ((id & 1) != 0);
|
350
|
+
}
|
351
|
+
|
352
|
+
return true;
|
353
|
+
}
|
354
|
+
|
355
|
+
void common_init() {
|
356
|
+
llama_log_set([](lm_ggml_log_level level, const char * text, void * /*user_data*/) {
|
357
|
+
if (LOG_DEFAULT_LLAMA <= common_log_verbosity_thold) {
|
358
|
+
common_log_add(common_log_main(), level, "%s", text);
|
359
|
+
}
|
360
|
+
}, NULL);
|
361
|
+
|
362
|
+
#ifdef NDEBUG
|
363
|
+
const char * build_type = "";
|
364
|
+
#else
|
365
|
+
const char * build_type = " (debug)";
|
366
|
+
#endif
|
367
|
+
|
368
|
+
LOG_INF("build: %d (%s) with %s for %s%s\n", LLAMA_BUILD_NUMBER, LLAMA_COMMIT, LLAMA_COMPILER, LLAMA_BUILD_TARGET, build_type);
|
369
|
+
}
|
370
|
+
|
371
|
+
std::string common_params_get_system_info(const common_params & params) {
|
372
|
+
std::ostringstream os;
|
373
|
+
|
374
|
+
os << "system_info: n_threads = " << params.cpuparams.n_threads;
|
375
|
+
if (params.cpuparams_batch.n_threads != -1) {
|
376
|
+
os << " (n_threads_batch = " << params.cpuparams_batch.n_threads << ")";
|
377
|
+
}
|
378
|
+
#if defined(_WIN32) && (_WIN32_WINNT >= 0x0601) && !defined(__MINGW64__) // windows 7 and later
|
379
|
+
// TODO: windows + arm64 + mingw64
|
380
|
+
DWORD logicalProcessorCount = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
|
381
|
+
os << " / " << logicalProcessorCount << " | " << llama_print_system_info();
|
382
|
+
#else
|
383
|
+
os << " / " << std::thread::hardware_concurrency() << " | " << llama_print_system_info();
|
384
|
+
#endif
|
385
|
+
|
386
|
+
return os.str();
|
387
|
+
}
|
388
|
+
|
389
|
+
//
|
390
|
+
// String utils
|
391
|
+
//
|
392
|
+
|
393
|
+
std::string string_format(const char * fmt, ...) {
|
394
|
+
va_list ap;
|
395
|
+
va_list ap2;
|
396
|
+
va_start(ap, fmt);
|
397
|
+
va_copy(ap2, ap);
|
398
|
+
int size = vsnprintf(NULL, 0, fmt, ap);
|
399
|
+
LM_GGML_ASSERT(size >= 0 && size < INT_MAX); // NOLINT
|
400
|
+
std::vector<char> buf(size + 1);
|
401
|
+
int size2 = vsnprintf(buf.data(), size + 1, fmt, ap2);
|
402
|
+
LM_GGML_ASSERT(size2 == size);
|
403
|
+
va_end(ap2);
|
404
|
+
va_end(ap);
|
405
|
+
return std::string(buf.data(), size);
|
406
|
+
}
|
407
|
+
|
408
|
+
std::string string_strip(const std::string & str) {
|
409
|
+
size_t start = 0;
|
410
|
+
size_t end = str.size();
|
411
|
+
while (start < end && std::isspace(str[start])) {
|
412
|
+
start++;
|
413
|
+
}
|
414
|
+
while (end > start && std::isspace(str[end - 1])) {
|
415
|
+
end--;
|
416
|
+
}
|
417
|
+
return str.substr(start, end - start);
|
418
|
+
}
|
419
|
+
|
420
|
+
std::string string_get_sortable_timestamp() {
|
421
|
+
using clock = std::chrono::system_clock;
|
422
|
+
|
423
|
+
const clock::time_point current_time = clock::now();
|
424
|
+
const time_t as_time_t = clock::to_time_t(current_time);
|
425
|
+
char timestamp_no_ns[100];
|
426
|
+
std::strftime(timestamp_no_ns, 100, "%Y_%m_%d-%H_%M_%S", std::localtime(&as_time_t));
|
427
|
+
|
428
|
+
const int64_t ns = std::chrono::duration_cast<std::chrono::nanoseconds>(
|
429
|
+
current_time.time_since_epoch() % 1000000000).count();
|
430
|
+
char timestamp_ns[11];
|
431
|
+
snprintf(timestamp_ns, 11, "%09" PRId64, ns);
|
432
|
+
|
433
|
+
return std::string(timestamp_no_ns) + "." + std::string(timestamp_ns);
|
434
|
+
}
|
435
|
+
|
436
|
+
void string_replace_all(std::string & s, const std::string & search, const std::string & replace) {
|
437
|
+
if (search.empty()) {
|
438
|
+
return;
|
439
|
+
}
|
440
|
+
std::string builder;
|
441
|
+
builder.reserve(s.length());
|
442
|
+
size_t pos = 0;
|
443
|
+
size_t last_pos = 0;
|
444
|
+
while ((pos = s.find(search, last_pos)) != std::string::npos) {
|
445
|
+
builder.append(s, last_pos, pos - last_pos);
|
446
|
+
builder.append(replace);
|
447
|
+
last_pos = pos + search.length();
|
448
|
+
}
|
449
|
+
builder.append(s, last_pos, std::string::npos);
|
450
|
+
s = std::move(builder);
|
451
|
+
}
|
452
|
+
|
453
|
+
std::string regex_escape(const std::string & s) {
|
454
|
+
static const std::regex special_chars("[.^$|()*+?\\[\\]{}\\\\]");
|
455
|
+
return std::regex_replace(s, special_chars, "\\$0");
|
456
|
+
}
|
457
|
+
|
458
|
+
std::string string_join(const std::vector<std::string> & values, const std::string & separator) {
|
459
|
+
std::ostringstream result;
|
460
|
+
for (size_t i = 0; i < values.size(); ++i) {
|
461
|
+
if (i > 0) {
|
462
|
+
result << separator;
|
463
|
+
}
|
464
|
+
result << values[i];
|
465
|
+
}
|
466
|
+
return result.str();
|
467
|
+
}
|
468
|
+
|
469
|
+
std::vector<std::string> string_split(const std::string & str, const std::string & delimiter) {
|
470
|
+
std::vector<std::string> parts;
|
471
|
+
size_t start = 0;
|
472
|
+
size_t end = str.find(delimiter);
|
473
|
+
|
474
|
+
while (end != std::string::npos) {
|
475
|
+
parts.push_back(str.substr(start, end - start));
|
476
|
+
start = end + delimiter.length();
|
477
|
+
end = str.find(delimiter, start);
|
478
|
+
}
|
479
|
+
|
480
|
+
parts.push_back(str.substr(start));
|
481
|
+
|
482
|
+
return parts;
|
483
|
+
}
|
484
|
+
|
485
|
+
std::string string_repeat(const std::string & str, size_t n) {
|
486
|
+
if (n == 0) {
|
487
|
+
return "";
|
488
|
+
}
|
489
|
+
|
490
|
+
std::string result;
|
491
|
+
result.reserve(str.length() * n);
|
492
|
+
|
493
|
+
for (size_t i = 0; i < n; ++i) {
|
494
|
+
result += str;
|
495
|
+
}
|
496
|
+
|
497
|
+
return result;
|
498
|
+
}
|
499
|
+
|
500
|
+
std::string string_from(bool value) {
|
501
|
+
return value ? "true" : "false";
|
502
|
+
}
|
503
|
+
|
504
|
+
std::string string_from(const std::vector<int> & values) {
|
505
|
+
std::stringstream buf;
|
506
|
+
|
507
|
+
buf << "[ ";
|
508
|
+
bool first = true;
|
509
|
+
for (auto e : values) {
|
510
|
+
if (first) {
|
511
|
+
first = false;
|
512
|
+
} else {
|
513
|
+
buf << ", ";
|
514
|
+
}
|
515
|
+
buf << std::to_string(e);
|
516
|
+
}
|
517
|
+
buf << " ]";
|
518
|
+
|
519
|
+
return buf.str();
|
520
|
+
}
|
521
|
+
|
522
|
+
std::string string_from(const struct llama_context * ctx, const std::vector<llama_token> & tokens) {
|
523
|
+
std::stringstream buf;
|
524
|
+
|
525
|
+
buf << "[ ";
|
526
|
+
|
527
|
+
bool first = true;
|
528
|
+
for (const auto & token : tokens) {
|
529
|
+
if (!first) {
|
530
|
+
buf << ", ";
|
531
|
+
} else {
|
532
|
+
first = false;
|
533
|
+
}
|
534
|
+
|
535
|
+
auto detokenized = common_token_to_piece(ctx, token);
|
536
|
+
|
537
|
+
detokenized.erase(
|
538
|
+
std::remove_if(
|
539
|
+
detokenized.begin(),
|
540
|
+
detokenized.end(),
|
541
|
+
[](const unsigned char c) { return !std::isprint(c); }),
|
542
|
+
detokenized.end());
|
543
|
+
|
544
|
+
buf << "'" << detokenized << "'"
|
545
|
+
<< ":" << std::to_string(token);
|
546
|
+
}
|
547
|
+
|
548
|
+
buf << " ]";
|
549
|
+
|
550
|
+
return buf.str();
|
551
|
+
}
|
552
|
+
|
553
|
+
std::string string_from(const struct llama_context * ctx, const struct llama_batch & batch) {
|
554
|
+
std::stringstream buf;
|
555
|
+
|
556
|
+
buf << "[ ";
|
557
|
+
|
558
|
+
bool first = true;
|
559
|
+
for (int i = 0; i < batch.n_tokens; ++i) {
|
560
|
+
if (!first) {
|
561
|
+
buf << ", ";
|
562
|
+
} else {
|
563
|
+
first = false;
|
564
|
+
}
|
565
|
+
|
566
|
+
auto detokenized = common_token_to_piece(ctx, batch.token[i]);
|
567
|
+
|
568
|
+
detokenized.erase(
|
569
|
+
std::remove_if(
|
570
|
+
detokenized.begin(),
|
571
|
+
detokenized.end(),
|
572
|
+
[](const unsigned char c) { return !std::isprint(c); }),
|
573
|
+
detokenized.end());
|
574
|
+
|
575
|
+
buf << "\n" << std::to_string(i)
|
576
|
+
<< ", token '" << detokenized << "'"
|
577
|
+
<< ", pos " << std::to_string(batch.pos[i])
|
578
|
+
<< ", n_seq_id " << std::to_string(batch.n_seq_id[i])
|
579
|
+
<< ", seq_id " << std::to_string(batch.seq_id[i][0])
|
580
|
+
<< ", logits " << std::to_string(batch.logits[i]);
|
581
|
+
}
|
582
|
+
|
583
|
+
buf << " ]";
|
584
|
+
|
585
|
+
return buf.str();
|
586
|
+
}
|
587
|
+
|
588
|
+
void string_process_escapes(std::string & input) {
|
589
|
+
std::size_t input_len = input.length();
|
590
|
+
std::size_t output_idx = 0;
|
591
|
+
|
592
|
+
for (std::size_t input_idx = 0; input_idx < input_len; ++input_idx) {
|
593
|
+
if (input[input_idx] == '\\' && input_idx + 1 < input_len) {
|
594
|
+
switch (input[++input_idx]) {
|
595
|
+
case 'n': input[output_idx++] = '\n'; break;
|
596
|
+
case 'r': input[output_idx++] = '\r'; break;
|
597
|
+
case 't': input[output_idx++] = '\t'; break;
|
598
|
+
case '\'': input[output_idx++] = '\''; break;
|
599
|
+
case '\"': input[output_idx++] = '\"'; break;
|
600
|
+
case '\\': input[output_idx++] = '\\'; break;
|
601
|
+
case 'x':
|
602
|
+
// Handle \x12, etc
|
603
|
+
if (input_idx + 2 < input_len) {
|
604
|
+
const char x[3] = { input[input_idx + 1], input[input_idx + 2], 0 };
|
605
|
+
char *err_p = nullptr;
|
606
|
+
const long val = std::strtol(x, &err_p, 16);
|
607
|
+
if (err_p == x + 2) {
|
608
|
+
input_idx += 2;
|
609
|
+
input[output_idx++] = char(val);
|
610
|
+
break;
|
611
|
+
}
|
612
|
+
}
|
613
|
+
// fall through
|
614
|
+
default: input[output_idx++] = '\\';
|
615
|
+
input[output_idx++] = input[input_idx]; break;
|
616
|
+
}
|
617
|
+
} else {
|
618
|
+
input[output_idx++] = input[input_idx];
|
619
|
+
}
|
620
|
+
}
|
621
|
+
|
622
|
+
input.resize(output_idx);
|
623
|
+
}
|
624
|
+
|
625
|
+
bool string_parse_kv_override(const char * data, std::vector<llama_model_kv_override> & overrides) {
|
626
|
+
const char * sep = strchr(data, '=');
|
627
|
+
if (sep == nullptr || sep - data >= 128) {
|
628
|
+
LOG_ERR("%s: malformed KV override '%s'\n", __func__, data);
|
629
|
+
return false;
|
630
|
+
}
|
631
|
+
llama_model_kv_override kvo;
|
632
|
+
std::strncpy(kvo.key, data, sep - data);
|
633
|
+
kvo.key[sep - data] = 0;
|
634
|
+
sep++;
|
635
|
+
if (strncmp(sep, "int:", 4) == 0) {
|
636
|
+
sep += 4;
|
637
|
+
kvo.tag = LLAMA_KV_OVERRIDE_TYPE_INT;
|
638
|
+
kvo.val_i64 = std::atol(sep);
|
639
|
+
} else if (strncmp(sep, "float:", 6) == 0) {
|
640
|
+
sep += 6;
|
641
|
+
kvo.tag = LLAMA_KV_OVERRIDE_TYPE_FLOAT;
|
642
|
+
kvo.val_f64 = std::atof(sep);
|
643
|
+
} else if (strncmp(sep, "bool:", 5) == 0) {
|
644
|
+
sep += 5;
|
645
|
+
kvo.tag = LLAMA_KV_OVERRIDE_TYPE_BOOL;
|
646
|
+
if (std::strcmp(sep, "true") == 0) {
|
647
|
+
kvo.val_bool = true;
|
648
|
+
} else if (std::strcmp(sep, "false") == 0) {
|
649
|
+
kvo.val_bool = false;
|
650
|
+
} else {
|
651
|
+
LOG_ERR("%s: invalid boolean value for KV override '%s'\n", __func__, data);
|
652
|
+
return false;
|
653
|
+
}
|
654
|
+
} else if (strncmp(sep, "str:", 4) == 0) {
|
655
|
+
sep += 4;
|
656
|
+
kvo.tag = LLAMA_KV_OVERRIDE_TYPE_STR;
|
657
|
+
if (strlen(sep) > 127) {
|
658
|
+
LOG_ERR("%s: malformed KV override '%s', value cannot exceed 127 chars\n", __func__, data);
|
659
|
+
return false;
|
660
|
+
}
|
661
|
+
strncpy(kvo.val_str, sep, 127);
|
662
|
+
kvo.val_str[127] = '\0';
|
663
|
+
} else {
|
664
|
+
LOG_ERR("%s: invalid type for KV override '%s'\n", __func__, data);
|
665
|
+
return false;
|
666
|
+
}
|
667
|
+
overrides.emplace_back(std::move(kvo));
|
668
|
+
return true;
|
669
|
+
}
|
670
|
+
|
671
|
+
//
|
672
|
+
// Filesystem utils
|
673
|
+
//
|
674
|
+
|
675
|
+
// Validate if a filename is safe to use
|
676
|
+
// To validate a full path, split the path by the OS-specific path separator, and validate each part with this function
|
677
|
+
bool fs_validate_filename(const std::string & filename) {
|
678
|
+
if (!filename.length()) {
|
679
|
+
// Empty filename invalid
|
680
|
+
return false;
|
681
|
+
}
|
682
|
+
if (filename.length() > 255) {
|
683
|
+
// Limit at common largest possible filename on Linux filesystems
|
684
|
+
// to avoid unnecessary further validation
|
685
|
+
// (On systems with smaller limits it will be caught by the OS)
|
686
|
+
return false;
|
687
|
+
}
|
688
|
+
|
689
|
+
std::u32string filename_utf32;
|
690
|
+
try {
|
691
|
+
#if defined(__clang__)
|
692
|
+
// disable C++17 deprecation warning for std::codecvt_utf8
|
693
|
+
# pragma clang diagnostic push
|
694
|
+
# pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
695
|
+
#endif
|
696
|
+
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> converter;
|
697
|
+
|
698
|
+
#if defined(__clang__)
|
699
|
+
# pragma clang diagnostic pop
|
700
|
+
#endif
|
701
|
+
|
702
|
+
filename_utf32 = converter.from_bytes(filename);
|
703
|
+
|
704
|
+
// If the reverse conversion mismatches, it means overlong UTF-8 sequences were used,
|
705
|
+
// or invalid encodings were encountered. Reject such attempts
|
706
|
+
std::string filename_reencoded = converter.to_bytes(filename_utf32);
|
707
|
+
if (filename_reencoded != filename) {
|
708
|
+
return false;
|
709
|
+
}
|
710
|
+
} catch (const std::exception &) {
|
711
|
+
return false;
|
712
|
+
}
|
713
|
+
|
714
|
+
// Check for forbidden codepoints:
|
715
|
+
// - Control characters
|
716
|
+
// - Unicode equivalents of illegal characters
|
717
|
+
// - UTF-16 surrogate pairs
|
718
|
+
// - UTF-8 replacement character
|
719
|
+
// - Byte order mark (BOM)
|
720
|
+
// - Illegal characters: / \ : * ? " < > |
|
721
|
+
for (char32_t c : filename_utf32) {
|
722
|
+
if (c <= 0x1F // Control characters (C0)
|
723
|
+
|| c == 0x7F // Control characters (DEL)
|
724
|
+
|| (c >= 0x80 && c <= 0x9F) // Control characters (C1)
|
725
|
+
|| c == 0xFF0E // Fullwidth Full Stop (period equivalent)
|
726
|
+
|| c == 0x2215 // Division Slash (forward slash equivalent)
|
727
|
+
|| c == 0x2216 // Set Minus (backslash equivalent)
|
728
|
+
|| (c >= 0xD800 && c <= 0xDFFF) // UTF-16 surrogate pairs
|
729
|
+
|| c == 0xFFFD // Replacement Character (UTF-8)
|
730
|
+
|| c == 0xFEFF // Byte Order Mark (BOM)
|
731
|
+
|| c == '/' || c == '\\' || c == ':' || c == '*' // Illegal characters
|
732
|
+
|| c == '?' || c == '"' || c == '<' || c == '>' || c == '|') {
|
733
|
+
return false;
|
734
|
+
}
|
735
|
+
}
|
736
|
+
|
737
|
+
// Reject any leading or trailing ' ', or any trailing '.', these are stripped on Windows and will cause a different filename
|
738
|
+
// Unicode and other whitespace is not affected, only 0x20 space
|
739
|
+
if (filename.front() == ' ' || filename.back() == ' ' || filename.back() == '.') {
|
740
|
+
return false;
|
741
|
+
}
|
742
|
+
|
743
|
+
// Reject any ".." (currently stricter than necessary, it should be fine to just check for == ".." instead)
|
744
|
+
if (filename.find("..") != std::string::npos) {
|
745
|
+
return false;
|
746
|
+
}
|
747
|
+
|
748
|
+
// Reject "."
|
749
|
+
if (filename == ".") {
|
750
|
+
return false;
|
751
|
+
}
|
752
|
+
|
753
|
+
return true;
|
754
|
+
}
|
755
|
+
|
756
|
+
// returns true if successful, false otherwise
|
757
|
+
bool fs_create_directory_with_parents(const std::string & path) {
|
758
|
+
#ifdef _WIN32
|
759
|
+
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
|
760
|
+
std::wstring wpath = converter.from_bytes(path);
|
761
|
+
|
762
|
+
// if the path already exists, check whether it's a directory
|
763
|
+
const DWORD attributes = GetFileAttributesW(wpath.c_str());
|
764
|
+
if ((attributes != INVALID_FILE_ATTRIBUTES) && (attributes & FILE_ATTRIBUTE_DIRECTORY)) {
|
765
|
+
return true;
|
766
|
+
}
|
767
|
+
|
768
|
+
size_t pos_slash = 0;
|
769
|
+
|
770
|
+
// process path from front to back, procedurally creating directories
|
771
|
+
while ((pos_slash = path.find('\\', pos_slash)) != std::string::npos) {
|
772
|
+
const std::wstring subpath = wpath.substr(0, pos_slash);
|
773
|
+
const wchar_t * test = subpath.c_str();
|
774
|
+
|
775
|
+
const bool success = CreateDirectoryW(test, NULL);
|
776
|
+
if (!success) {
|
777
|
+
const DWORD error = GetLastError();
|
778
|
+
|
779
|
+
// if the path already exists, ensure that it's a directory
|
780
|
+
if (error == ERROR_ALREADY_EXISTS) {
|
781
|
+
const DWORD attributes = GetFileAttributesW(subpath.c_str());
|
782
|
+
if (attributes == INVALID_FILE_ATTRIBUTES || !(attributes & FILE_ATTRIBUTE_DIRECTORY)) {
|
783
|
+
return false;
|
784
|
+
}
|
785
|
+
} else {
|
786
|
+
return false;
|
787
|
+
}
|
788
|
+
}
|
789
|
+
|
790
|
+
pos_slash += 1;
|
791
|
+
}
|
792
|
+
|
793
|
+
return true;
|
794
|
+
#else
|
795
|
+
// if the path already exists, check whether it's a directory
|
796
|
+
struct stat info;
|
797
|
+
if (stat(path.c_str(), &info) == 0) {
|
798
|
+
return S_ISDIR(info.st_mode);
|
799
|
+
}
|
800
|
+
|
801
|
+
size_t pos_slash = 1; // skip leading slashes for directory creation
|
802
|
+
|
803
|
+
// process path from front to back, procedurally creating directories
|
804
|
+
while ((pos_slash = path.find('/', pos_slash)) != std::string::npos) {
|
805
|
+
const std::string subpath = path.substr(0, pos_slash);
|
806
|
+
struct stat info;
|
807
|
+
|
808
|
+
// if the path already exists, ensure that it's a directory
|
809
|
+
if (stat(subpath.c_str(), &info) == 0) {
|
810
|
+
if (!S_ISDIR(info.st_mode)) {
|
811
|
+
return false;
|
812
|
+
}
|
813
|
+
} else {
|
814
|
+
// create parent directories
|
815
|
+
const int ret = mkdir(subpath.c_str(), 0755);
|
816
|
+
if (ret != 0) {
|
817
|
+
return false;
|
818
|
+
}
|
819
|
+
}
|
820
|
+
|
821
|
+
pos_slash += 1;
|
822
|
+
}
|
823
|
+
|
824
|
+
return true;
|
825
|
+
#endif // _WIN32
|
826
|
+
}
|
827
|
+
|
828
|
+
std::string fs_get_cache_directory() {
|
829
|
+
std::string cache_directory = "";
|
830
|
+
auto ensure_trailing_slash = [](std::string p) {
|
831
|
+
// Make sure to add trailing slash
|
832
|
+
if (p.back() != DIRECTORY_SEPARATOR) {
|
833
|
+
p += DIRECTORY_SEPARATOR;
|
834
|
+
}
|
835
|
+
return p;
|
836
|
+
};
|
837
|
+
if (getenv("LLAMA_CACHE")) {
|
838
|
+
cache_directory = std::getenv("LLAMA_CACHE");
|
839
|
+
} else {
|
840
|
+
#ifdef __linux__
|
841
|
+
if (std::getenv("XDG_CACHE_HOME")) {
|
842
|
+
cache_directory = std::getenv("XDG_CACHE_HOME");
|
843
|
+
} else {
|
844
|
+
cache_directory = std::getenv("HOME") + std::string("/.cache/");
|
845
|
+
}
|
846
|
+
#elif defined(__APPLE__)
|
847
|
+
cache_directory = std::getenv("HOME") + std::string("/Library/Caches/");
|
848
|
+
#elif defined(_WIN32)
|
849
|
+
cache_directory = std::getenv("LOCALAPPDATA");
|
850
|
+
#endif // __linux__
|
851
|
+
cache_directory = ensure_trailing_slash(cache_directory);
|
852
|
+
cache_directory += "llama.cpp";
|
853
|
+
}
|
854
|
+
return ensure_trailing_slash(cache_directory);
|
855
|
+
}
|
856
|
+
|
857
|
+
std::string fs_get_cache_file(const std::string & filename) {
|
858
|
+
LM_GGML_ASSERT(filename.find(DIRECTORY_SEPARATOR) == std::string::npos);
|
859
|
+
std::string cache_directory = fs_get_cache_directory();
|
860
|
+
const bool success = fs_create_directory_with_parents(cache_directory);
|
861
|
+
if (!success) {
|
862
|
+
throw std::runtime_error("failed to create cache directory: " + cache_directory);
|
863
|
+
}
|
864
|
+
return cache_directory + filename;
|
865
|
+
}
|
866
|
+
|
867
|
+
|
868
|
+
//
|
869
|
+
// Model utils
|
870
|
+
//
|
871
|
+
|
872
|
+
struct common_init_result common_init_from_params(common_params & params) {
|
873
|
+
common_init_result iparams;
|
874
|
+
auto mparams = common_model_params_to_llama(params);
|
875
|
+
|
876
|
+
llama_model * model = llama_model_load_from_file(params.model.path.c_str(), mparams);
|
877
|
+
if (model == NULL) {
|
878
|
+
LOG_ERR("%s: failed to load model '%s'\n", __func__, params.model.path.c_str());
|
879
|
+
return iparams;
|
880
|
+
}
|
881
|
+
|
882
|
+
const llama_vocab * vocab = llama_model_get_vocab(model);
|
883
|
+
|
884
|
+
if (params.reranking) {
|
885
|
+
bool ok = true;
|
886
|
+
|
887
|
+
if (llama_vocab_bos(vocab) == LLAMA_TOKEN_NULL) {
|
888
|
+
LOG_WRN("%s: warning: vocab does not have a BOS token, reranking will not work\n", __func__);
|
889
|
+
ok = false;
|
890
|
+
}
|
891
|
+
|
892
|
+
if (llama_vocab_eos(vocab) == LLAMA_TOKEN_NULL) {
|
893
|
+
LOG_WRN("%s: warning: vocab does not have an EOS token, reranking will not work\n", __func__);
|
894
|
+
ok = false;
|
895
|
+
}
|
896
|
+
|
897
|
+
if (llama_vocab_sep(vocab) == LLAMA_TOKEN_NULL) {
|
898
|
+
LOG_WRN("%s: warning: vocab does not have a SEP token, reranking will not work\n", __func__);
|
899
|
+
ok = false;
|
900
|
+
}
|
901
|
+
|
902
|
+
if (!ok) {
|
903
|
+
llama_model_free(model);
|
904
|
+
|
905
|
+
return iparams;
|
906
|
+
}
|
907
|
+
}
|
908
|
+
|
909
|
+
auto cparams = common_context_params_to_llama(params);
|
910
|
+
|
911
|
+
llama_context * lctx = llama_init_from_model(model, cparams);
|
912
|
+
if (lctx == NULL) {
|
913
|
+
LOG_ERR("%s: failed to create context with model '%s'\n", __func__, params.model.path.c_str());
|
914
|
+
llama_model_free(model);
|
915
|
+
return iparams;
|
916
|
+
}
|
917
|
+
|
918
|
+
if (params.ctx_shift && !llama_kv_self_can_shift(lctx)) {
|
919
|
+
LOG_WRN("%s: KV cache shifting is not supported for this context, disabling KV cache shifting\n", __func__);
|
920
|
+
params.ctx_shift = false;
|
921
|
+
}
|
922
|
+
|
923
|
+
if (!params.control_vectors.empty()) {
|
924
|
+
if (params.control_vector_layer_start <= 0) params.control_vector_layer_start = 1;
|
925
|
+
if (params.control_vector_layer_end <= 0) params.control_vector_layer_end = llama_model_n_layer(model);
|
926
|
+
|
927
|
+
const auto cvec = common_control_vector_load(params.control_vectors);
|
928
|
+
if (cvec.n_embd == -1) {
|
929
|
+
llama_free(lctx);
|
930
|
+
llama_model_free(model);
|
931
|
+
|
932
|
+
return iparams;
|
933
|
+
}
|
934
|
+
|
935
|
+
int err = llama_apply_adapter_cvec(
|
936
|
+
lctx,
|
937
|
+
cvec.data.data(),
|
938
|
+
cvec.data.size(),
|
939
|
+
cvec.n_embd,
|
940
|
+
params.control_vector_layer_start,
|
941
|
+
params.control_vector_layer_end);
|
942
|
+
if (err) {
|
943
|
+
llama_free(lctx);
|
944
|
+
llama_model_free(model);
|
945
|
+
|
946
|
+
return iparams;
|
947
|
+
}
|
948
|
+
}
|
949
|
+
|
950
|
+
// load and optionally apply lora adapters
|
951
|
+
for (auto & la : params.lora_adapters) {
|
952
|
+
llama_adapter_lora_ptr lora;
|
953
|
+
lora.reset(llama_adapter_lora_init(model, la.path.c_str()));
|
954
|
+
if (lora == nullptr) {
|
955
|
+
LOG_ERR("%s: failed to apply lora adapter '%s'\n", __func__, la.path.c_str());
|
956
|
+
llama_free(lctx);
|
957
|
+
llama_model_free(model);
|
958
|
+
return iparams;
|
959
|
+
}
|
960
|
+
|
961
|
+
la.ptr = lora.get();
|
962
|
+
iparams.lora.emplace_back(std::move(lora)); // copy to list of loaded adapters
|
963
|
+
}
|
964
|
+
|
965
|
+
if (!params.lora_init_without_apply) {
|
966
|
+
common_set_adapter_lora(lctx, params.lora_adapters);
|
967
|
+
}
|
968
|
+
|
969
|
+
if (params.sampling.ignore_eos && llama_vocab_eos(vocab) == LLAMA_TOKEN_NULL) {
|
970
|
+
LOG_WRN("%s: warning: vocab does not have an EOS token, ignoring --ignore-eos\n", __func__);
|
971
|
+
params.sampling.ignore_eos = false;
|
972
|
+
}
|
973
|
+
|
974
|
+
if (params.sampling.ignore_eos) {
|
975
|
+
for (llama_token i = 0; i < llama_vocab_n_tokens(vocab); i++) {
|
976
|
+
if (llama_vocab_is_eog(vocab, i)) {
|
977
|
+
LOG_INF("%s: added %s logit bias = %f\n", __func__, common_token_to_piece(lctx, i).c_str(), -INFINITY);
|
978
|
+
params.sampling.logit_bias.push_back({i, -INFINITY});
|
979
|
+
}
|
980
|
+
}
|
981
|
+
}
|
982
|
+
|
983
|
+
if (params.sampling.penalty_last_n == -1) {
|
984
|
+
LOG_INF("%s: setting penalty_last_n to ctx_size = %d\n", __func__, llama_n_ctx(lctx));
|
985
|
+
params.sampling.penalty_last_n = llama_n_ctx(lctx);
|
986
|
+
}
|
987
|
+
|
988
|
+
if (params.sampling.dry_penalty_last_n == -1) {
|
989
|
+
LOG_INF("%s: setting dry_penalty_last_n to ctx_size = %d\n", __func__, llama_n_ctx(lctx));
|
990
|
+
params.sampling.dry_penalty_last_n = llama_n_ctx(lctx);
|
991
|
+
}
|
992
|
+
|
993
|
+
if (params.warmup) {
|
994
|
+
LOG_WRN("%s: warming up the model with an empty run - please wait ... (--no-warmup to disable)\n", __func__);
|
995
|
+
|
996
|
+
llama_set_warmup(lctx, true);
|
997
|
+
|
998
|
+
std::vector<llama_token> tmp;
|
999
|
+
llama_token bos = llama_vocab_bos(vocab);
|
1000
|
+
llama_token eos = llama_vocab_eos(vocab);
|
1001
|
+
|
1002
|
+
// some models (e.g. T5) don't have a BOS token
|
1003
|
+
if (bos != LLAMA_TOKEN_NULL) {
|
1004
|
+
tmp.push_back(bos);
|
1005
|
+
}
|
1006
|
+
if (eos != LLAMA_TOKEN_NULL) {
|
1007
|
+
tmp.push_back(eos);
|
1008
|
+
}
|
1009
|
+
if (tmp.empty()) {
|
1010
|
+
tmp.push_back(0);
|
1011
|
+
}
|
1012
|
+
|
1013
|
+
if (llama_model_has_encoder(model)) {
|
1014
|
+
llama_encode(lctx, llama_batch_get_one(tmp.data(), tmp.size()));
|
1015
|
+
llama_token decoder_start_token_id = llama_model_decoder_start_token(model);
|
1016
|
+
if (decoder_start_token_id == LLAMA_TOKEN_NULL) {
|
1017
|
+
decoder_start_token_id = bos;
|
1018
|
+
}
|
1019
|
+
tmp.clear();
|
1020
|
+
tmp.push_back(decoder_start_token_id);
|
1021
|
+
}
|
1022
|
+
if (llama_model_has_decoder(model)) {
|
1023
|
+
llama_decode(lctx, llama_batch_get_one(tmp.data(), std::min(tmp.size(), (size_t) params.n_batch)));
|
1024
|
+
}
|
1025
|
+
llama_kv_self_clear(lctx);
|
1026
|
+
llama_synchronize(lctx);
|
1027
|
+
llama_perf_context_reset(lctx);
|
1028
|
+
llama_set_warmup(lctx, false);
|
1029
|
+
}
|
1030
|
+
|
1031
|
+
iparams.model.reset(model);
|
1032
|
+
iparams.context.reset(lctx);
|
1033
|
+
|
1034
|
+
return iparams;
|
1035
|
+
}
|
1036
|
+
|
1037
|
+
void common_set_adapter_lora(struct llama_context * ctx, std::vector<common_adapter_lora_info> & lora) {
|
1038
|
+
llama_clear_adapter_lora(ctx);
|
1039
|
+
for (auto & la : lora) {
|
1040
|
+
if (la.scale != 0.0f) {
|
1041
|
+
llama_set_adapter_lora(ctx, la.ptr, la.scale);
|
1042
|
+
}
|
1043
|
+
}
|
1044
|
+
}
|
1045
|
+
|
1046
|
+
struct llama_model_params common_model_params_to_llama(common_params & params) {
|
1047
|
+
auto mparams = llama_model_default_params();
|
1048
|
+
|
1049
|
+
if (!params.devices.empty()) {
|
1050
|
+
mparams.devices = params.devices.data();
|
1051
|
+
}
|
1052
|
+
|
1053
|
+
if (params.n_gpu_layers != -1) {
|
1054
|
+
mparams.n_gpu_layers = params.n_gpu_layers;
|
1055
|
+
}
|
1056
|
+
|
1057
|
+
mparams.progress_callback_user_data = params.progress_callback_user_data;
|
1058
|
+
mparams.progress_callback = params.progress_callback;
|
1059
|
+
mparams.vocab_only = params.vocab_only;
|
1060
|
+
mparams.main_gpu = params.main_gpu;
|
1061
|
+
mparams.split_mode = params.split_mode;
|
1062
|
+
mparams.tensor_split = params.tensor_split;
|
1063
|
+
mparams.use_mmap = params.use_mmap;
|
1064
|
+
mparams.use_mlock = params.use_mlock;
|
1065
|
+
mparams.check_tensors = params.check_tensors;
|
1066
|
+
|
1067
|
+
if (params.kv_overrides.empty()) {
|
1068
|
+
mparams.kv_overrides = NULL;
|
1069
|
+
} else {
|
1070
|
+
LM_GGML_ASSERT(params.kv_overrides.back().key[0] == 0 && "KV overrides not terminated with empty key");
|
1071
|
+
mparams.kv_overrides = params.kv_overrides.data();
|
1072
|
+
}
|
1073
|
+
|
1074
|
+
if (params.tensor_buft_overrides.empty()) {
|
1075
|
+
mparams.tensor_buft_overrides = NULL;
|
1076
|
+
} else {
|
1077
|
+
LM_GGML_ASSERT(params.tensor_buft_overrides.back().pattern == nullptr && "Tensor buffer overrides not terminated with empty pattern");
|
1078
|
+
mparams.tensor_buft_overrides = params.tensor_buft_overrides.data();
|
1079
|
+
}
|
1080
|
+
|
1081
|
+
return mparams;
|
1082
|
+
}
|
1083
|
+
|
1084
|
+
struct llama_context_params common_context_params_to_llama(const common_params & params) {
|
1085
|
+
auto cparams = llama_context_default_params();
|
1086
|
+
|
1087
|
+
cparams.n_ctx = params.n_ctx;
|
1088
|
+
cparams.n_seq_max = params.n_parallel;
|
1089
|
+
cparams.n_batch = params.n_batch;
|
1090
|
+
cparams.n_ubatch = params.n_ubatch;
|
1091
|
+
cparams.n_threads = params.cpuparams.n_threads;
|
1092
|
+
cparams.n_threads_batch = params.cpuparams_batch.n_threads == -1 ?
|
1093
|
+
params.cpuparams.n_threads : params.cpuparams_batch.n_threads;
|
1094
|
+
cparams.logits_all = params.logits_all;
|
1095
|
+
cparams.embeddings = params.embedding;
|
1096
|
+
cparams.rope_scaling_type = params.rope_scaling_type;
|
1097
|
+
cparams.rope_freq_base = params.rope_freq_base;
|
1098
|
+
cparams.rope_freq_scale = params.rope_freq_scale;
|
1099
|
+
cparams.yarn_ext_factor = params.yarn_ext_factor;
|
1100
|
+
cparams.yarn_attn_factor = params.yarn_attn_factor;
|
1101
|
+
cparams.yarn_beta_fast = params.yarn_beta_fast;
|
1102
|
+
cparams.yarn_beta_slow = params.yarn_beta_slow;
|
1103
|
+
cparams.yarn_orig_ctx = params.yarn_orig_ctx;
|
1104
|
+
cparams.pooling_type = params.pooling_type;
|
1105
|
+
cparams.attention_type = params.attention_type;
|
1106
|
+
cparams.defrag_thold = params.defrag_thold;
|
1107
|
+
cparams.cb_eval = params.cb_eval;
|
1108
|
+
cparams.cb_eval_user_data = params.cb_eval_user_data;
|
1109
|
+
cparams.offload_kqv = !params.no_kv_offload;
|
1110
|
+
cparams.flash_attn = params.flash_attn;
|
1111
|
+
cparams.no_perf = params.no_perf;
|
1112
|
+
|
1113
|
+
if (params.reranking) {
|
1114
|
+
cparams.embeddings = true;
|
1115
|
+
cparams.pooling_type = LLAMA_POOLING_TYPE_RANK;
|
1116
|
+
}
|
1117
|
+
|
1118
|
+
cparams.type_k = params.cache_type_k;
|
1119
|
+
cparams.type_v = params.cache_type_v;
|
1120
|
+
|
1121
|
+
return cparams;
|
1122
|
+
}
|
1123
|
+
|
1124
|
+
struct lm_ggml_threadpool_params lm_ggml_threadpool_params_from_cpu_params(const cpu_params & params) {
|
1125
|
+
struct lm_ggml_threadpool_params tpp;
|
1126
|
+
|
1127
|
+
lm_ggml_threadpool_params_init(&tpp, params.n_threads); // setup the defaults
|
1128
|
+
|
1129
|
+
if (params.mask_valid) {
|
1130
|
+
std::memcpy(&tpp.cpumask, ¶ms.cpumask, LM_GGML_MAX_N_THREADS);
|
1131
|
+
}
|
1132
|
+
|
1133
|
+
tpp.prio = params.priority;
|
1134
|
+
tpp.poll = params.poll;
|
1135
|
+
tpp.strict_cpu = params.strict_cpu;
|
1136
|
+
|
1137
|
+
return tpp;
|
1138
|
+
}
|
1139
|
+
|
1140
|
+
//
|
1141
|
+
// Batch utils
|
1142
|
+
//
|
1143
|
+
|
1144
|
+
void common_batch_clear(struct llama_batch & batch) {
|
1145
|
+
batch.n_tokens = 0;
|
1146
|
+
}
|
1147
|
+
|
1148
|
+
void common_batch_add(
|
1149
|
+
struct llama_batch & batch,
|
1150
|
+
llama_token id,
|
1151
|
+
llama_pos pos,
|
1152
|
+
const std::vector<llama_seq_id> & seq_ids,
|
1153
|
+
bool logits) {
|
1154
|
+
LM_GGML_ASSERT(batch.seq_id[batch.n_tokens] && "llama_batch size exceeded");
|
1155
|
+
|
1156
|
+
batch.token [batch.n_tokens] = id;
|
1157
|
+
batch.pos [batch.n_tokens] = pos;
|
1158
|
+
batch.n_seq_id[batch.n_tokens] = seq_ids.size();
|
1159
|
+
for (size_t i = 0; i < seq_ids.size(); ++i) {
|
1160
|
+
batch.seq_id[batch.n_tokens][i] = seq_ids[i];
|
1161
|
+
}
|
1162
|
+
batch.logits [batch.n_tokens] = logits;
|
1163
|
+
|
1164
|
+
batch.n_tokens++;
|
1165
|
+
}
|
1166
|
+
|
1167
|
+
//
|
1168
|
+
// Token utils
|
1169
|
+
//
|
1170
|
+
|
1171
|
+
size_t common_lcp(const llama_tokens & a, const llama_tokens & b) {
|
1172
|
+
size_t i;
|
1173
|
+
for (i = 0; i < a.size() && i < b.size() && a[i] == b[i]; i++) {}
|
1174
|
+
|
1175
|
+
return i;
|
1176
|
+
}
|
1177
|
+
|
1178
|
+
size_t common_lcs(const llama_tokens & a, const llama_tokens & b) {
|
1179
|
+
// check for empty sequences
|
1180
|
+
if (a.empty() || b.empty()) {
|
1181
|
+
return 0;
|
1182
|
+
}
|
1183
|
+
|
1184
|
+
// get the lengths of the input sequences
|
1185
|
+
size_t a_len = a.size();
|
1186
|
+
size_t b_len = b.size();
|
1187
|
+
|
1188
|
+
// initialize the maximum length of the longest common subsequence (LCS)
|
1189
|
+
size_t max_length = 0;
|
1190
|
+
|
1191
|
+
// use two rows instead of a 2D matrix to optimize space
|
1192
|
+
std::vector<size_t> prev_row(b_len + 1, 0);
|
1193
|
+
std::vector<size_t> curr_row(b_len + 1, 0);
|
1194
|
+
|
1195
|
+
// iterate through the elements of a
|
1196
|
+
for (size_t i = 1; i <= a_len; i++) {
|
1197
|
+
// iterate through the elements of b
|
1198
|
+
for (size_t j = 1; j <= b_len; j++) {
|
1199
|
+
// if elements at the current positions match
|
1200
|
+
if (a[i - 1] == b[j - 1]) {
|
1201
|
+
// if it's the first element of either sequences, set LCS length to 1
|
1202
|
+
if (i == 1 || j == 1) {
|
1203
|
+
curr_row[j] = 1;
|
1204
|
+
} else {
|
1205
|
+
// increment LCS length by 1 compared to the previous element
|
1206
|
+
curr_row[j] = prev_row[j - 1] + 1;
|
1207
|
+
}
|
1208
|
+
|
1209
|
+
// update max_length if necessary
|
1210
|
+
if (curr_row[j] > max_length) {
|
1211
|
+
max_length = curr_row[j];
|
1212
|
+
}
|
1213
|
+
} else {
|
1214
|
+
// reset LCS length if elements don't match
|
1215
|
+
curr_row[j] = 0;
|
1216
|
+
}
|
1217
|
+
}
|
1218
|
+
|
1219
|
+
// update the previous row for the next iteration
|
1220
|
+
prev_row = curr_row;
|
1221
|
+
}
|
1222
|
+
|
1223
|
+
// return the maximum length of the LCS
|
1224
|
+
return max_length;
|
1225
|
+
}
|
1226
|
+
|
1227
|
+
//
|
1228
|
+
// Vocab utils
|
1229
|
+
//
|
1230
|
+
|
1231
|
+
std::vector<llama_token> common_tokenize(
|
1232
|
+
const struct llama_context * ctx,
|
1233
|
+
const std::string & text,
|
1234
|
+
bool add_special,
|
1235
|
+
bool parse_special) {
|
1236
|
+
const llama_model * model = llama_get_model(ctx);
|
1237
|
+
const llama_vocab * vocab = llama_model_get_vocab(model);
|
1238
|
+
return common_tokenize(vocab, text, add_special, parse_special);
|
1239
|
+
}
|
1240
|
+
|
1241
|
+
std::vector<llama_token> common_tokenize(
|
1242
|
+
const struct llama_vocab * vocab,
|
1243
|
+
const std::string & text,
|
1244
|
+
bool add_special,
|
1245
|
+
bool parse_special) {
|
1246
|
+
// upper limit for the number of tokens
|
1247
|
+
int n_tokens = text.length() + 2 * add_special;
|
1248
|
+
std::vector<llama_token> result(n_tokens);
|
1249
|
+
n_tokens = llama_tokenize(vocab, text.data(), text.length(), result.data(), result.size(), add_special, parse_special);
|
1250
|
+
if (n_tokens < 0) {
|
1251
|
+
result.resize(-n_tokens);
|
1252
|
+
int check = llama_tokenize(vocab, text.data(), text.length(), result.data(), result.size(), add_special, parse_special);
|
1253
|
+
LM_GGML_ASSERT(check == -n_tokens);
|
1254
|
+
} else {
|
1255
|
+
result.resize(n_tokens);
|
1256
|
+
}
|
1257
|
+
return result;
|
1258
|
+
}
|
1259
|
+
|
1260
|
+
std::string common_token_to_piece(const struct llama_context * ctx, llama_token token, bool special) {
|
1261
|
+
const llama_model * model = llama_get_model(ctx);
|
1262
|
+
const llama_vocab * vocab = llama_model_get_vocab(model);
|
1263
|
+
return common_token_to_piece(vocab, token, special);
|
1264
|
+
}
|
1265
|
+
|
1266
|
+
std::string common_token_to_piece(const struct llama_vocab * vocab, llama_token token, bool special) {
|
1267
|
+
std::string piece;
|
1268
|
+
piece.resize(piece.capacity()); // using string internal cache, 15 bytes + '\n'
|
1269
|
+
const int n_chars = llama_token_to_piece(vocab, token, &piece[0], piece.size(), 0, special);
|
1270
|
+
if (n_chars < 0) {
|
1271
|
+
piece.resize(-n_chars);
|
1272
|
+
int check = llama_token_to_piece(vocab, token, &piece[0], piece.size(), 0, special);
|
1273
|
+
LM_GGML_ASSERT(check == -n_chars);
|
1274
|
+
}
|
1275
|
+
else {
|
1276
|
+
piece.resize(n_chars);
|
1277
|
+
}
|
1278
|
+
|
1279
|
+
return piece;
|
1280
|
+
}
|
1281
|
+
|
1282
|
+
std::string common_detokenize(const struct llama_context * ctx, const std::vector<llama_token> & tokens, bool special) {
|
1283
|
+
const llama_model * model = llama_get_model(ctx);
|
1284
|
+
const llama_vocab * vocab = llama_model_get_vocab(model);
|
1285
|
+
return common_detokenize(vocab, tokens, special);
|
1286
|
+
}
|
1287
|
+
|
1288
|
+
std::string common_detokenize(const struct llama_vocab * vocab, const std::vector<llama_token> & tokens, bool special) {
|
1289
|
+
std::string text;
|
1290
|
+
text.resize(std::max(text.capacity(), tokens.size()));
|
1291
|
+
int32_t n_chars = llama_detokenize(vocab, tokens.data(), (int32_t)tokens.size(), &text[0], (int32_t)text.size(), false, special);
|
1292
|
+
if (n_chars < 0) {
|
1293
|
+
text.resize(-n_chars);
|
1294
|
+
n_chars = llama_detokenize(vocab, tokens.data(), (int32_t)tokens.size(), &text[0], (int32_t)text.size(), false, special);
|
1295
|
+
LM_GGML_ASSERT(n_chars <= (int32_t)text.size()); // whitespace trimming is performed after per-token detokenization
|
1296
|
+
}
|
1297
|
+
|
1298
|
+
text.resize(n_chars);
|
1299
|
+
|
1300
|
+
// NOTE: the original tokenizer decodes bytes after collecting the pieces.
|
1301
|
+
return text;
|
1302
|
+
}
|
1303
|
+
|
1304
|
+
//
|
1305
|
+
// KV cache utils
|
1306
|
+
//
|
1307
|
+
|
1308
|
+
void common_kv_cache_dump_view(const llama_kv_cache_view & view, int row_size) {
|
1309
|
+
static const char slot_chars[] = ".123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+";
|
1310
|
+
|
1311
|
+
printf("=== Dumping KV cache. total cells %d, max sequences per cell %d, populated cells %d, total tokens in cache %d, largest empty slot=%d @ %d",
|
1312
|
+
view.n_cells, view.n_seq_max, view.used_cells, view.token_count, view.max_contiguous, view.max_contiguous_idx);
|
1313
|
+
|
1314
|
+
llama_kv_cache_view_cell * c_curr = view.cells;
|
1315
|
+
llama_seq_id * cs_curr = view.cells_sequences;
|
1316
|
+
|
1317
|
+
for (int i = 0; i < view.n_cells; i++, c_curr++, cs_curr += view.n_seq_max) {
|
1318
|
+
if (i % row_size == 0) {
|
1319
|
+
printf("\n%5d: ", i);
|
1320
|
+
}
|
1321
|
+
int seq_count = 0;
|
1322
|
+
for (int j = 0; j < view.n_seq_max; j++) {
|
1323
|
+
if (cs_curr[j] >= 0) { seq_count++; }
|
1324
|
+
}
|
1325
|
+
putchar(slot_chars[std::min(sizeof(slot_chars) - 2, size_t(seq_count))]);
|
1326
|
+
}
|
1327
|
+
|
1328
|
+
printf("\n=== Done dumping\n");
|
1329
|
+
}
|
1330
|
+
|
1331
|
+
void common_kv_cache_dump_view_seqs(const llama_kv_cache_view & view, int row_size) {
|
1332
|
+
static const char slot_chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
1333
|
+
|
1334
|
+
printf("=== Dumping KV cache. total cells %d, max sequences per cell %d, populated cells %d, total tokens in cache %d, largest empty slot=%d @ %d\n",
|
1335
|
+
view.n_cells, view.n_seq_max, view.used_cells, view.token_count, view.max_contiguous, view.max_contiguous_idx);
|
1336
|
+
|
1337
|
+
std::unordered_map<llama_seq_id, size_t> seqs;
|
1338
|
+
llama_kv_cache_view_cell * c_curr = view.cells;
|
1339
|
+
llama_seq_id * cs_curr = view.cells_sequences;
|
1340
|
+
|
1341
|
+
for (int i = 0; i < view.n_cells; i++, c_curr++, cs_curr += view.n_seq_max) {
|
1342
|
+
for (int j = 0; j < view.n_seq_max; j++) {
|
1343
|
+
if (cs_curr[j] < 0) { continue; }
|
1344
|
+
if (seqs.find(cs_curr[j]) == seqs.end()) {
|
1345
|
+
if (seqs.size() + 1 >= sizeof(slot_chars)) { break; }
|
1346
|
+
const size_t sz = seqs.size();
|
1347
|
+
seqs[cs_curr[j]] = sz;
|
1348
|
+
}
|
1349
|
+
}
|
1350
|
+
if (seqs.size() + 1 >= sizeof(slot_chars)) { break; }
|
1351
|
+
}
|
1352
|
+
|
1353
|
+
printf("=== Sequence legend: ");
|
1354
|
+
for (const auto & it : seqs) {
|
1355
|
+
printf("%zu=%d, ", it.second, it.first);
|
1356
|
+
}
|
1357
|
+
printf("'+'=other sequence ids");
|
1358
|
+
|
1359
|
+
c_curr = view.cells;
|
1360
|
+
cs_curr = view.cells_sequences;
|
1361
|
+
for (int i = 0; i < view.n_cells; i++, c_curr++, cs_curr += view.n_seq_max) {
|
1362
|
+
if (i % row_size == 0) {
|
1363
|
+
printf("\n%5d: ", i);
|
1364
|
+
}
|
1365
|
+
for (int j = 0; j < view.n_seq_max; j++) {
|
1366
|
+
if (cs_curr[j] >= 0) {
|
1367
|
+
const auto & it = seqs.find(cs_curr[j]);
|
1368
|
+
putchar(it != seqs.end() ? int(slot_chars[it->second]) : '+');
|
1369
|
+
} else {
|
1370
|
+
putchar('.');
|
1371
|
+
}
|
1372
|
+
}
|
1373
|
+
putchar(' ');
|
1374
|
+
}
|
1375
|
+
|
1376
|
+
printf("\n=== Done dumping\n");
|
1377
|
+
}
|
1378
|
+
|
1379
|
+
//
|
1380
|
+
// Embedding utils
|
1381
|
+
//
|
1382
|
+
|
1383
|
+
void common_embd_normalize(const float * inp, float * out, int n, int embd_norm) {
|
1384
|
+
double sum = 0.0;
|
1385
|
+
|
1386
|
+
switch (embd_norm) {
|
1387
|
+
case -1: // no normalisation
|
1388
|
+
sum = 1.0;
|
1389
|
+
break;
|
1390
|
+
case 0: // max absolute
|
1391
|
+
for (int i = 0; i < n; i++) {
|
1392
|
+
if (sum < std::abs(inp[i])) {
|
1393
|
+
sum = std::abs(inp[i]);
|
1394
|
+
}
|
1395
|
+
}
|
1396
|
+
sum /= 32760.0; // make an int16 range
|
1397
|
+
break;
|
1398
|
+
case 2: // euclidean
|
1399
|
+
for (int i = 0; i < n; i++) {
|
1400
|
+
sum += inp[i] * inp[i];
|
1401
|
+
}
|
1402
|
+
sum = std::sqrt(sum);
|
1403
|
+
break;
|
1404
|
+
default: // p-norm (euclidean is p-norm p=2)
|
1405
|
+
for (int i = 0; i < n; i++) {
|
1406
|
+
sum += std::pow(std::abs(inp[i]), embd_norm);
|
1407
|
+
}
|
1408
|
+
sum = std::pow(sum, 1.0 / embd_norm);
|
1409
|
+
break;
|
1410
|
+
}
|
1411
|
+
|
1412
|
+
const float norm = sum > 0.0 ? 1.0 / sum : 0.0f;
|
1413
|
+
|
1414
|
+
for (int i = 0; i < n; i++) {
|
1415
|
+
out[i] = inp[i] * norm;
|
1416
|
+
}
|
1417
|
+
}
|
1418
|
+
|
1419
|
+
float common_embd_similarity_cos(const float * embd1, const float * embd2, int n){
|
1420
|
+
double sum = 0.0;
|
1421
|
+
double sum1 = 0.0;
|
1422
|
+
double sum2 = 0.0;
|
1423
|
+
|
1424
|
+
for (int i = 0; i < n; i++) {
|
1425
|
+
sum += embd1[i] * embd2[i];
|
1426
|
+
sum1 += embd1[i] * embd1[i];
|
1427
|
+
sum2 += embd2[i] * embd2[i];
|
1428
|
+
}
|
1429
|
+
|
1430
|
+
// Handle the case where one or both vectors are zero vectors
|
1431
|
+
if (sum1 == 0.0 || sum2 == 0.0) {
|
1432
|
+
if (sum1 == 0.0 && sum2 == 0.0) {
|
1433
|
+
return 1.0f; // two zero vectors are similar
|
1434
|
+
}
|
1435
|
+
return 0.0f;
|
1436
|
+
}
|
1437
|
+
|
1438
|
+
return sum / (sqrt(sum1) * sqrt(sum2));
|
1439
|
+
}
|
1440
|
+
|
1441
|
+
//
|
1442
|
+
// Control vector utils
|
1443
|
+
//
|
1444
|
+
|
1445
|
+
static common_control_vector_data common_control_vector_load_one(const common_control_vector_load_info & load_info) {
|
1446
|
+
common_control_vector_data result = { -1, {} };
|
1447
|
+
|
1448
|
+
lm_ggml_context * ctx = nullptr;
|
1449
|
+
struct lm_gguf_init_params meta_lm_gguf_params = {
|
1450
|
+
/* .no_alloc = */ false,
|
1451
|
+
/* .ctx = */ &ctx,
|
1452
|
+
};
|
1453
|
+
struct lm_gguf_context * ctx_gguf = lm_gguf_init_from_file(load_info.fname.c_str(), meta_lm_gguf_params);
|
1454
|
+
if (!ctx_gguf) {
|
1455
|
+
LOG_ERR("%s: failed to load control vector file from %s\n", __func__, load_info.fname.c_str());
|
1456
|
+
return result;
|
1457
|
+
}
|
1458
|
+
|
1459
|
+
int32_t n_tensors = lm_gguf_get_n_tensors(ctx_gguf);
|
1460
|
+
if (n_tensors == 0) {
|
1461
|
+
LOG_WRN("%s: no direction tensors found in %s\n", __func__, load_info.fname.c_str());
|
1462
|
+
}
|
1463
|
+
|
1464
|
+
for (int i = 0; i < n_tensors; i++) {
|
1465
|
+
std::string name = lm_gguf_get_tensor_name(ctx_gguf, i);
|
1466
|
+
|
1467
|
+
int layer_idx = -1;
|
1468
|
+
|
1469
|
+
// split on '.'
|
1470
|
+
size_t dotpos = name.find('.');
|
1471
|
+
if (dotpos != std::string::npos && name.substr(0, dotpos) == "direction") {
|
1472
|
+
try {
|
1473
|
+
layer_idx = std::stoi(name.substr(dotpos + 1));
|
1474
|
+
} catch (...) {
|
1475
|
+
layer_idx = -1;
|
1476
|
+
}
|
1477
|
+
}
|
1478
|
+
if (layer_idx < 0) {
|
1479
|
+
LOG_ERR("%s: invalid/unparsable direction tensor layer index in %s\n", __func__, load_info.fname.c_str());
|
1480
|
+
result.n_embd = -1;
|
1481
|
+
break;
|
1482
|
+
} else if (layer_idx == 0) {
|
1483
|
+
LOG_ERR("%s: invalid (zero) direction tensor layer index in %s\n", __func__, load_info.fname.c_str());
|
1484
|
+
result.n_embd = -1;
|
1485
|
+
break;
|
1486
|
+
}
|
1487
|
+
|
1488
|
+
struct lm_ggml_tensor * tensor = lm_ggml_get_tensor(ctx, name.c_str());
|
1489
|
+
if (tensor->type != LM_GGML_TYPE_F32) {
|
1490
|
+
LOG_ERR("%s: invalid (non-F32) direction tensor type in %s\n", __func__, load_info.fname.c_str());
|
1491
|
+
result.n_embd = -1;
|
1492
|
+
break;
|
1493
|
+
}
|
1494
|
+
if (lm_ggml_n_dims(tensor) != 1) {
|
1495
|
+
LOG_ERR("%s: invalid (non-1D) direction tensor shape in %s\n", __func__, load_info.fname.c_str());
|
1496
|
+
result.n_embd = -1;
|
1497
|
+
break;
|
1498
|
+
}
|
1499
|
+
|
1500
|
+
if (result.n_embd == -1) {
|
1501
|
+
result.n_embd = lm_ggml_nelements(tensor);
|
1502
|
+
} else if (lm_ggml_nelements(tensor) != result.n_embd) {
|
1503
|
+
LOG_ERR("%s: direction tensor in %s does not match previous dimensions\n", __func__, load_info.fname.c_str());
|
1504
|
+
result.n_embd = -1;
|
1505
|
+
break;
|
1506
|
+
}
|
1507
|
+
|
1508
|
+
// extend if necessary - do not store data for layer 0 (it's not used)
|
1509
|
+
result.data.resize(std::max(result.data.size(), static_cast<size_t>(result.n_embd * layer_idx)), 0.0f);
|
1510
|
+
|
1511
|
+
const float * src = (const float *) tensor->data;
|
1512
|
+
float * dst = result.data.data() + result.n_embd * (layer_idx - 1); // layer 1 at [0]
|
1513
|
+
for (int j = 0; j < result.n_embd; j++) {
|
1514
|
+
dst[j] += src[j] * load_info.strength; // allows multiple directions for same layer in same file
|
1515
|
+
}
|
1516
|
+
|
1517
|
+
}
|
1518
|
+
|
1519
|
+
if (result.n_embd == -1) {
|
1520
|
+
LOG_WRN("%s: skipping %s due to invalid direction tensors\n", __func__, load_info.fname.c_str());
|
1521
|
+
result.data.clear();
|
1522
|
+
}
|
1523
|
+
|
1524
|
+
lm_gguf_free(ctx_gguf);
|
1525
|
+
lm_ggml_free(ctx);
|
1526
|
+
|
1527
|
+
return result;
|
1528
|
+
}
|
1529
|
+
|
1530
|
+
common_control_vector_data common_control_vector_load(const std::vector<common_control_vector_load_info> & load_infos) {
|
1531
|
+
common_control_vector_data result = { -1, {} };
|
1532
|
+
|
1533
|
+
for (const auto & info : load_infos) {
|
1534
|
+
auto cur = common_control_vector_load_one(info);
|
1535
|
+
|
1536
|
+
if (cur.n_embd == -1) {
|
1537
|
+
result.n_embd = -1;
|
1538
|
+
break;
|
1539
|
+
}
|
1540
|
+
if (result.n_embd != -1 && result.n_embd != cur.n_embd) {
|
1541
|
+
LOG_ERR("%s: control vectors in %s does not match previous dimensions\n", __func__, info.fname.c_str());
|
1542
|
+
result.n_embd = -1;
|
1543
|
+
break;
|
1544
|
+
}
|
1545
|
+
|
1546
|
+
if (result.n_embd == -1) {
|
1547
|
+
result = std::move(cur);
|
1548
|
+
} else {
|
1549
|
+
result.data.resize(std::max(result.data.size(), cur.data.size()), 0.0f); // extend if necessary
|
1550
|
+
for (size_t i = 0; i < cur.data.size(); i++) {
|
1551
|
+
result.data[i] += cur.data[i];
|
1552
|
+
}
|
1553
|
+
}
|
1554
|
+
}
|
1555
|
+
|
1556
|
+
if (result.n_embd == -1) {
|
1557
|
+
LOG_ERR("%s: no valid control vector files passed\n", __func__);
|
1558
|
+
result.data.clear();
|
1559
|
+
}
|
1560
|
+
|
1561
|
+
return result;
|
1562
|
+
}
|