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.
Files changed (216) hide show
  1. package/android/src/main/CMakeLists.txt +9 -2
  2. package/android/src/main/jni.cpp +54 -34
  3. package/android/src/main/jniLibs/arm64-v8a/librnllama.so +0 -0
  4. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8.so +0 -0
  5. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2.so +0 -0
  6. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_dotprod.so +0 -0
  7. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_dotprod_i8mm.so +0 -0
  8. package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_i8mm.so +0 -0
  9. package/android/src/main/jniLibs/x86_64/librnllama.so +0 -0
  10. package/android/src/main/jniLibs/x86_64/librnllama_x86_64.so +0 -0
  11. package/cpp/binary-ops.cpp +158 -0
  12. package/cpp/binary-ops.h +16 -0
  13. package/cpp/chat.cpp +1769 -1085
  14. package/cpp/chat.h +143 -0
  15. package/cpp/common.cpp +1562 -1996
  16. package/cpp/common.h +677 -744
  17. package/cpp/cpu-common.h +72 -0
  18. package/cpp/ggml-alloc.c +1039 -1030
  19. package/cpp/ggml-alloc.h +1 -1
  20. package/cpp/ggml-backend-impl.h +255 -255
  21. package/cpp/ggml-backend-reg.cpp +586 -582
  22. package/cpp/ggml-backend.cpp +2004 -2002
  23. package/cpp/ggml-backend.h +354 -354
  24. package/cpp/ggml-common.h +1857 -1851
  25. package/cpp/ggml-cpp.h +39 -39
  26. package/cpp/ggml-cpu-aarch64.cpp +5725 -4247
  27. package/cpp/ggml-cpu-aarch64.h +8 -8
  28. package/cpp/ggml-cpu-impl.h +512 -380
  29. package/cpp/ggml-cpu-quants.c +13026 -11517
  30. package/cpp/ggml-cpu-traits.cpp +36 -36
  31. package/cpp/ggml-cpu-traits.h +38 -38
  32. package/cpp/ggml-cpu.c +3438 -14485
  33. package/cpp/ggml-cpu.cpp +655 -633
  34. package/cpp/ggml-cpu.h +138 -135
  35. package/cpp/ggml-impl.h +594 -567
  36. package/cpp/ggml-metal-impl.h +312 -3
  37. package/cpp/ggml-metal.h +66 -66
  38. package/cpp/ggml-metal.m +5360 -5002
  39. package/cpp/ggml-opt.cpp +854 -854
  40. package/cpp/ggml-opt.h +216 -216
  41. package/cpp/ggml-quants.c +5238 -5238
  42. package/cpp/ggml-threading.h +14 -14
  43. package/cpp/ggml.c +6618 -6524
  44. package/cpp/ggml.h +2222 -2194
  45. package/cpp/gguf.cpp +1330 -1329
  46. package/cpp/gguf.h +202 -202
  47. package/cpp/json-schema-to-grammar.cpp +1024 -1025
  48. package/cpp/json-schema-to-grammar.h +21 -22
  49. package/cpp/json.hpp +24766 -24766
  50. package/cpp/llama-adapter.cpp +382 -347
  51. package/cpp/llama-adapter.h +76 -74
  52. package/cpp/llama-arch.cpp +1714 -1492
  53. package/cpp/llama-arch.h +428 -402
  54. package/cpp/llama-batch.cpp +368 -368
  55. package/cpp/llama-batch.h +88 -88
  56. package/cpp/llama-chat.cpp +640 -587
  57. package/cpp/llama-chat.h +56 -53
  58. package/cpp/llama-context.cpp +2831 -1775
  59. package/cpp/llama-context.h +265 -128
  60. package/cpp/llama-cparams.cpp +1 -1
  61. package/cpp/llama-cparams.h +38 -37
  62. package/cpp/llama-cpp.h +30 -30
  63. package/cpp/llama-grammar.cpp +1219 -1219
  64. package/cpp/llama-grammar.h +173 -164
  65. package/cpp/llama-graph.cpp +1695 -0
  66. package/cpp/llama-graph.h +592 -0
  67. package/cpp/llama-hparams.cpp +79 -71
  68. package/cpp/llama-hparams.h +156 -139
  69. package/cpp/llama-impl.cpp +167 -167
  70. package/cpp/llama-impl.h +61 -61
  71. package/cpp/llama-io.cpp +15 -0
  72. package/cpp/llama-io.h +35 -0
  73. package/cpp/llama-kv-cache.cpp +1380 -718
  74. package/cpp/llama-kv-cache.h +213 -218
  75. package/cpp/llama-memory.cpp +1 -0
  76. package/cpp/llama-memory.h +21 -0
  77. package/cpp/llama-mmap.cpp +600 -590
  78. package/cpp/llama-mmap.h +68 -68
  79. package/cpp/llama-model-loader.cpp +1129 -1124
  80. package/cpp/llama-model-loader.h +169 -167
  81. package/cpp/llama-model.cpp +13080 -4023
  82. package/cpp/llama-model.h +409 -370
  83. package/cpp/llama-sampling.cpp +2563 -2525
  84. package/cpp/llama-sampling.h +32 -32
  85. package/cpp/llama-vocab.cpp +3295 -3252
  86. package/cpp/llama-vocab.h +125 -125
  87. package/cpp/llama.cpp +351 -10137
  88. package/cpp/llama.h +1434 -1340
  89. package/cpp/log.cpp +427 -423
  90. package/cpp/log.h +132 -132
  91. package/cpp/{chat-template.hpp → minja/chat-template.hpp} +537 -529
  92. package/cpp/{minja.hpp → minja/minja.hpp} +2941 -2883
  93. package/cpp/ops.cpp +8723 -0
  94. package/cpp/ops.h +128 -0
  95. package/cpp/rn-llama.cpp +45 -71
  96. package/cpp/rn-llama.h +3 -3
  97. package/cpp/sampling.cpp +573 -532
  98. package/cpp/sgemm.cpp +3043 -2598
  99. package/cpp/sgemm.h +14 -14
  100. package/cpp/simd-mappings.h +888 -0
  101. package/cpp/speculative.cpp +278 -277
  102. package/cpp/speculative.h +28 -28
  103. package/cpp/unary-ops.cpp +186 -0
  104. package/cpp/unary-ops.h +28 -0
  105. package/cpp/vec.cpp +258 -0
  106. package/cpp/vec.h +802 -0
  107. package/ios/CMakeLists.txt +5 -2
  108. package/ios/RNLlama.mm +2 -2
  109. package/ios/RNLlamaContext.mm +40 -24
  110. package/package.json +1 -1
  111. package/src/NativeRNLlama.ts +6 -4
  112. package/src/index.ts +3 -1
  113. package/android/src/main/build-arm64/CMakeCache.txt +0 -429
  114. package/android/src/main/build-arm64/CMakeFiles/3.31.4/CMakeCCompiler.cmake +0 -81
  115. package/android/src/main/build-arm64/CMakeFiles/3.31.4/CMakeCXXCompiler.cmake +0 -101
  116. package/android/src/main/build-arm64/CMakeFiles/3.31.4/CMakeDetermineCompilerABI_C.bin +0 -0
  117. package/android/src/main/build-arm64/CMakeFiles/3.31.4/CMakeDetermineCompilerABI_CXX.bin +0 -0
  118. package/android/src/main/build-arm64/CMakeFiles/3.31.4/CMakeSystem.cmake +0 -15
  119. package/android/src/main/build-arm64/CMakeFiles/3.31.4/CompilerIdC/CMakeCCompilerId.c +0 -904
  120. package/android/src/main/build-arm64/CMakeFiles/3.31.4/CompilerIdC/CMakeCCompilerId.o +0 -0
  121. package/android/src/main/build-arm64/CMakeFiles/3.31.4/CompilerIdCXX/CMakeCXXCompilerId.cpp +0 -919
  122. package/android/src/main/build-arm64/CMakeFiles/3.31.4/CompilerIdCXX/CMakeCXXCompilerId.o +0 -0
  123. package/android/src/main/build-arm64/CMakeFiles/CMakeConfigureLog.yaml +0 -431
  124. package/android/src/main/build-arm64/CMakeFiles/CMakeDirectoryInformation.cmake +0 -16
  125. package/android/src/main/build-arm64/CMakeFiles/Makefile.cmake +0 -165
  126. package/android/src/main/build-arm64/CMakeFiles/Makefile2 +0 -297
  127. package/android/src/main/build-arm64/CMakeFiles/Progress/1 +0 -1
  128. package/android/src/main/build-arm64/CMakeFiles/Progress/2 +0 -1
  129. package/android/src/main/build-arm64/CMakeFiles/Progress/3 +0 -1
  130. package/android/src/main/build-arm64/CMakeFiles/Progress/4 +0 -1
  131. package/android/src/main/build-arm64/CMakeFiles/Progress/5 +0 -1
  132. package/android/src/main/build-arm64/CMakeFiles/Progress/6 +0 -1
  133. package/android/src/main/build-arm64/CMakeFiles/Progress/count.txt +0 -1
  134. package/android/src/main/build-arm64/CMakeFiles/TargetDirectories.txt +0 -8
  135. package/android/src/main/build-arm64/CMakeFiles/cmake.check_cache +0 -1
  136. package/android/src/main/build-arm64/CMakeFiles/progress.marks +0 -1
  137. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-alloc.c.o +0 -0
  138. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-alloc.c.o.d +0 -58
  139. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-backend-reg.cpp.o +0 -0
  140. 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
  141. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-backend.cpp.o +0 -0
  142. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-backend.cpp.o.d +0 -709
  143. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-cpu-aarch64.cpp.o +0 -0
  144. 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
  145. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-cpu-quants.c.o +0 -0
  146. 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
  147. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-cpu-traits.cpp.o +0 -0
  148. 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
  149. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-cpu.c.o +0 -0
  150. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-cpu.c.o.d +0 -113
  151. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-cpu.cpp.o +0 -0
  152. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-cpu.cpp.o.d +0 -713
  153. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-opt.cpp.o +0 -0
  154. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-opt.cpp.o.d +0 -763
  155. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-quants.c.o +0 -0
  156. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-quants.c.o.d +0 -61
  157. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-threading.cpp.o +0 -0
  158. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml-threading.cpp.o.d +0 -707
  159. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml.c.o +0 -0
  160. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/ggml.c.o.d +0 -104
  161. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/gguf.cpp.o +0 -0
  162. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/gguf.cpp.o.d +0 -714
  163. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/log.cpp.o +0 -0
  164. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/D_/dev/react-native/cui-llama.rn/cpp/log.cpp.o.d +0 -723
  165. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/DependInfo.cmake +0 -62
  166. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/build.make +0 -722
  167. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/cmake_clean.cmake +0 -89
  168. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/compiler_depend.make +0 -2
  169. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/compiler_depend.ts +0 -2
  170. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/depend.make +0 -2
  171. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/flags.make +0 -17
  172. package/android/src/main/build-arm64/CMakeFiles/rnllama.dir/progress.make +0 -41
  173. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8.dir/DependInfo.cmake +0 -62
  174. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8.dir/build.make +0 -722
  175. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8.dir/cmake_clean.cmake +0 -89
  176. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8.dir/compiler_depend.make +0 -2
  177. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8.dir/compiler_depend.ts +0 -2
  178. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8.dir/depend.make +0 -2
  179. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8.dir/flags.make +0 -17
  180. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8.dir/progress.make +0 -41
  181. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2.dir/DependInfo.cmake +0 -62
  182. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2.dir/build.make +0 -722
  183. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2.dir/cmake_clean.cmake +0 -89
  184. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2.dir/compiler_depend.make +0 -2
  185. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2.dir/compiler_depend.ts +0 -2
  186. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2.dir/depend.make +0 -2
  187. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2.dir/flags.make +0 -17
  188. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2.dir/progress.make +0 -41
  189. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod.dir/DependInfo.cmake +0 -62
  190. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod.dir/build.make +0 -722
  191. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod.dir/cmake_clean.cmake +0 -89
  192. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod.dir/compiler_depend.make +0 -2
  193. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod.dir/compiler_depend.ts +0 -2
  194. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod.dir/depend.make +0 -2
  195. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod.dir/flags.make +0 -17
  196. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod.dir/progress.make +0 -41
  197. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod_i8mm.dir/DependInfo.cmake +0 -62
  198. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod_i8mm.dir/build.make +0 -722
  199. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod_i8mm.dir/cmake_clean.cmake +0 -89
  200. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod_i8mm.dir/compiler_depend.make +0 -2
  201. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod_i8mm.dir/compiler_depend.ts +0 -2
  202. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod_i8mm.dir/depend.make +0 -2
  203. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod_i8mm.dir/flags.make +0 -17
  204. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_dotprod_i8mm.dir/progress.make +0 -41
  205. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_i8mm.dir/DependInfo.cmake +0 -62
  206. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_i8mm.dir/build.make +0 -722
  207. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_i8mm.dir/cmake_clean.cmake +0 -89
  208. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_i8mm.dir/compiler_depend.make +0 -2
  209. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_i8mm.dir/compiler_depend.ts +0 -2
  210. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_i8mm.dir/depend.make +0 -2
  211. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_i8mm.dir/flags.make +0 -17
  212. package/android/src/main/build-arm64/CMakeFiles/rnllama_v8_2_i8mm.dir/progress.make +0 -41
  213. package/android/src/main/build-arm64/Makefile +0 -1862
  214. package/android/src/main/build-arm64/cmake_install.cmake +0 -66
  215. package/cpp/chat.hpp +0 -55
  216. 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
- // Change JSON_ASSERT from assert() to LM_GGML_ASSERT:
11
- #define JSON_ASSERT LM_GGML_ASSERT
12
- #include "json.hpp"
13
- #include "json-schema-to-grammar.h"
14
- #include "llama.h"
15
- #include "chat.hpp"
16
- #include "chat-template.hpp"
17
-
18
- #include <algorithm>
19
- #include <cinttypes>
20
- #include <climits>
21
- #include <cmath>
22
- #include <codecvt>
23
- #include <cstdarg>
24
- #include <cstring>
25
- #include <ctime>
26
- #include <filesystem>
27
- #include <fstream>
28
- #include <iostream>
29
- #include <iterator>
30
- #include <regex>
31
- #include <sstream>
32
- #include <string>
33
- #include <thread>
34
- #include <unordered_map>
35
- #include <unordered_set>
36
- #include <vector>
37
-
38
- #if defined(__APPLE__) && defined(__MACH__)
39
- #include <sys/types.h>
40
- #include <sys/sysctl.h>
41
- #endif
42
-
43
- #if defined(_WIN32)
44
- #define WIN32_LEAN_AND_MEAN
45
- #ifndef NOMINMAX
46
- # define NOMINMAX
47
- #endif
48
- #include <locale>
49
- #include <windows.h>
50
- #include <fcntl.h>
51
- #include <io.h>
52
- #else
53
- #include <sys/ioctl.h>
54
- #include <sys/stat.h>
55
- #include <unistd.h>
56
- #endif
57
- #if defined(LLAMA_USE_CURL)
58
- #include <curl/curl.h>
59
- #include <curl/easy.h>
60
- #include <future>
61
- #endif
62
-
63
- // build info
64
- int LLAMA_BUILD_NUMBER = 0;
65
- char const *LLAMA_COMMIT = "unknown";
66
- char const *LLAMA_COMPILER = "unknown";
67
- char const *LLAMA_BUILD_TARGET = "unknown";
68
-
69
- #if defined(_MSC_VER)
70
- #pragma warning(disable: 4244 4267) // possible loss of data
71
- #endif
72
-
73
- #if defined(LLAMA_USE_CURL)
74
- #ifdef __linux__
75
- #include <linux/limits.h>
76
- #elif defined(_WIN32)
77
- # if !defined(PATH_MAX)
78
- # define PATH_MAX MAX_PATH
79
- # endif
80
- #else
81
- #include <sys/syslimits.h>
82
- #endif
83
- #define LLAMA_CURL_MAX_URL_LENGTH 2084 // Maximum URL Length in Chrome: 2083
84
-
85
- //
86
- // CURL utils
87
- //
88
-
89
- using curl_ptr = std::unique_ptr<CURL, decltype(&curl_easy_cleanup)>;
90
-
91
- // cannot use unique_ptr for curl_slist, because we cannot update without destroying the old one
92
- struct curl_slist_ptr {
93
- struct curl_slist * ptr = nullptr;
94
- ~curl_slist_ptr() {
95
- if (ptr) {
96
- curl_slist_free_all(ptr);
97
- }
98
- }
99
- };
100
- #endif // LLAMA_USE_CURL
101
-
102
- using json = nlohmann::ordered_json;
103
-
104
- //
105
- // CPU utils
106
- //
107
-
108
- int32_t cpu_get_num_physical_cores() {
109
- #ifdef __linux__
110
- // enumerate the set of thread siblings, num entries is num cores
111
- std::unordered_set<std::string> siblings;
112
- for (uint32_t cpu=0; cpu < UINT32_MAX; ++cpu) {
113
- std::ifstream thread_siblings("/sys/devices/system/cpu/cpu"
114
- + std::to_string(cpu) + "/topology/thread_siblings");
115
- if (!thread_siblings.is_open()) {
116
- break; // no more cpus
117
- }
118
- std::string line;
119
- if (std::getline(thread_siblings, line)) {
120
- siblings.insert(line);
121
- }
122
- }
123
- if (!siblings.empty()) {
124
- return static_cast<int32_t>(siblings.size());
125
- }
126
- #elif defined(__APPLE__) && defined(__MACH__)
127
- int32_t num_physical_cores;
128
- size_t len = sizeof(num_physical_cores);
129
- int result = sysctlbyname("hw.perflevel0.physicalcpu", &num_physical_cores, &len, NULL, 0);
130
- if (result == 0) {
131
- return num_physical_cores;
132
- }
133
- result = sysctlbyname("hw.physicalcpu", &num_physical_cores, &len, NULL, 0);
134
- if (result == 0) {
135
- return num_physical_cores;
136
- }
137
- #elif defined(_WIN32) && (_WIN32_WINNT >= 0x0601) && !defined(__MINGW64__) // windows 7 and later
138
- // TODO: windows + arm64 + mingw64
139
- unsigned int n_threads_win = std::thread::hardware_concurrency();
140
- unsigned int default_threads = n_threads_win > 0 ? (n_threads_win <= 4 ? n_threads_win : n_threads_win / 2) : 4;
141
-
142
- DWORD buffer_size = 0;
143
- if (!GetLogicalProcessorInformationEx(RelationProcessorCore, nullptr, &buffer_size)) {
144
- if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
145
- return default_threads;
146
- }
147
- }
148
-
149
- std::vector<char> buffer(buffer_size);
150
- if (!GetLogicalProcessorInformationEx(RelationProcessorCore, reinterpret_cast<PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX>(buffer.data()), &buffer_size)) {
151
- return default_threads;
152
- }
153
-
154
- int32_t num_physical_cores = 0;
155
- PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX info = reinterpret_cast<PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX>(buffer.data());
156
- while (buffer_size > 0) {
157
- if (info->Relationship == RelationProcessorCore) {
158
- num_physical_cores += info->Processor.GroupCount;
159
- }
160
- buffer_size -= info->Size;
161
- info = reinterpret_cast<PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX>(reinterpret_cast<char*>(info) + info->Size);
162
- }
163
-
164
- return num_physical_cores > 0 ? num_physical_cores : default_threads;
165
- #endif
166
- unsigned int n_threads = std::thread::hardware_concurrency();
167
- return n_threads > 0 ? (n_threads <= 4 ? n_threads : n_threads / 2) : 4;
168
- }
169
-
170
- #if defined(__x86_64__) && defined(__linux__) && !defined(__ANDROID__)
171
- #include <pthread.h>
172
-
173
- static void cpuid(unsigned leaf, unsigned subleaf,
174
- unsigned *eax, unsigned *ebx, unsigned *ecx, unsigned *edx) {
175
- __asm__("movq\t%%rbx,%%rsi\n\t"
176
- "cpuid\n\t"
177
- "xchgq\t%%rbx,%%rsi"
178
- : "=a"(*eax), "=S"(*ebx), "=c"(*ecx), "=d"(*edx)
179
- : "0"(leaf), "2"(subleaf));
180
- }
181
-
182
- static int pin_cpu(int cpu) {
183
- cpu_set_t mask;
184
- CPU_ZERO(&mask);
185
- CPU_SET(cpu, &mask);
186
- return pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask);
187
- }
188
-
189
- static bool is_hybrid_cpu(void) {
190
- unsigned eax, ebx, ecx, edx;
191
- cpuid(7, 0, &eax, &ebx, &ecx, &edx);
192
- return !!(edx & (1u << 15));
193
- }
194
-
195
- static bool is_running_on_efficiency_core(void) {
196
- unsigned eax, ebx, ecx, edx;
197
- cpuid(0x1a, 0, &eax, &ebx, &ecx, &edx);
198
- int intel_atom = 0x20;
199
- int core_type = (eax & 0xff000000u) >> 24;
200
- return core_type == intel_atom;
201
- }
202
-
203
- static int cpu_count_math_cpus(int n_cpu) {
204
- int result = 0;
205
- for (int cpu = 0; cpu < n_cpu; ++cpu) {
206
- if (pin_cpu(cpu)) {
207
- return -1;
208
- }
209
- if (is_running_on_efficiency_core()) {
210
- continue; // efficiency cores harm lockstep threading
211
- }
212
- ++cpu; // hyperthreading isn't useful for linear algebra
213
- ++result;
214
- }
215
- return result;
216
- }
217
-
218
- #endif // __x86_64__ && __linux__
219
-
220
- /**
221
- * Returns number of CPUs on system that are useful for math.
222
- */
223
- int32_t cpu_get_num_math() {
224
- #if defined(__x86_64__) && defined(__linux__) && !defined(__ANDROID__)
225
- int n_cpu = sysconf(_SC_NPROCESSORS_ONLN);
226
- if (n_cpu < 1) {
227
- return cpu_get_num_physical_cores();
228
- }
229
- if (is_hybrid_cpu()) {
230
- cpu_set_t affinity;
231
- if (!pthread_getaffinity_np(pthread_self(), sizeof(affinity), &affinity)) {
232
- int result = cpu_count_math_cpus(n_cpu);
233
- pthread_setaffinity_np(pthread_self(), sizeof(affinity), &affinity);
234
- if (result > 0) {
235
- return result;
236
- }
237
- }
238
- }
239
- #endif
240
- return cpu_get_num_physical_cores();
241
- }
242
-
243
- // Helper for setting process priority
244
-
245
- #if defined(_WIN32)
246
-
247
- bool set_process_priority(enum lm_ggml_sched_priority prio) {
248
- if (prio == LM_GGML_SCHED_PRIO_NORMAL) {
249
- return true;
250
- }
251
-
252
- DWORD p = NORMAL_PRIORITY_CLASS;
253
- switch (prio) {
254
- case LM_GGML_SCHED_PRIO_NORMAL: p = NORMAL_PRIORITY_CLASS; break;
255
- case LM_GGML_SCHED_PRIO_MEDIUM: p = ABOVE_NORMAL_PRIORITY_CLASS; break;
256
- case LM_GGML_SCHED_PRIO_HIGH: p = HIGH_PRIORITY_CLASS; break;
257
- case LM_GGML_SCHED_PRIO_REALTIME: p = REALTIME_PRIORITY_CLASS; break;
258
- }
259
-
260
- if (!SetPriorityClass(GetCurrentProcess(), p)) {
261
- LOG_WRN("failed to set process priority class %d : (%d)\n", prio, (int) GetLastError());
262
- return false;
263
- }
264
-
265
- return true;
266
- }
267
-
268
- #else // MacOS and POSIX
269
- #include <sys/types.h>
270
- #include <sys/resource.h>
271
-
272
- bool set_process_priority(enum lm_ggml_sched_priority prio) {
273
- if (prio == LM_GGML_SCHED_PRIO_NORMAL) {
274
- return true;
275
- }
276
-
277
- int p = 0;
278
- switch (prio) {
279
- case LM_GGML_SCHED_PRIO_NORMAL: p = 0; break;
280
- case LM_GGML_SCHED_PRIO_MEDIUM: p = -5; break;
281
- case LM_GGML_SCHED_PRIO_HIGH: p = -10; break;
282
- case LM_GGML_SCHED_PRIO_REALTIME: p = -20; break;
283
- }
284
-
285
- if (!setpriority(PRIO_PROCESS, 0, p)) {
286
- LOG_WRN("failed to set process priority %d : %s (%d)\n", prio, strerror(errno), errno);
287
- return false;
288
- }
289
- return true;
290
- }
291
-
292
- #endif
293
-
294
- //
295
- // CLI argument parsing
296
- //
297
-
298
-
299
- void postprocess_cpu_params(cpu_params& cpuparams, const cpu_params* role_model) {
300
- int32_t n_set = 0;
301
-
302
- if (cpuparams.n_threads < 0) {
303
- // Assuming everything about cpuparams is invalid
304
- if (role_model != nullptr) {
305
- cpuparams = *role_model;
306
- } else {
307
- cpuparams.n_threads = cpu_get_num_math();
308
- }
309
- }
310
-
311
- for (int32_t i = 0; i < LM_GGML_MAX_N_THREADS; i++) {
312
- if (cpuparams.cpumask[i]) {
313
- n_set++;
314
- }
315
- }
316
-
317
- if (n_set && n_set < cpuparams.n_threads) {
318
- // Not enough set bits, may experience performance issues.
319
- LOG_WRN("Not enough set bits in CPU mask (%d) to satisfy requested thread count: %d\n", n_set, cpuparams.n_threads);
320
- }
321
- }
322
-
323
- bool parse_cpu_range(const std::string & range, bool (&boolmask)[LM_GGML_MAX_N_THREADS]) {
324
- size_t dash_loc = range.find('-');
325
- if (dash_loc == std::string::npos) {
326
- LOG_ERR("Format of CPU range is invalid! Expected [<start>]-[<end>].\n");
327
- return false;
328
- }
329
-
330
- size_t start_i;
331
- size_t end_i;
332
-
333
- if (dash_loc == 0) {
334
- start_i = 0;
335
- } else {
336
- start_i = std::stoull(range.substr(0, dash_loc));
337
- if (start_i >= LM_GGML_MAX_N_THREADS) {
338
- LOG_ERR("Start index out of bounds!\n");
339
- return false;
340
- }
341
- }
342
-
343
- if (dash_loc == range.length() - 1) {
344
- end_i = LM_GGML_MAX_N_THREADS - 1;
345
- } else {
346
- end_i = std::stoull(range.substr(dash_loc + 1));
347
- if (end_i >= LM_GGML_MAX_N_THREADS) {
348
- LOG_ERR("End index out of bounds!\n");
349
- return false;
350
- }
351
- }
352
-
353
- for (size_t i = start_i; i <= end_i; i++) {
354
- boolmask[i] = true;
355
- }
356
-
357
- return true;
358
- }
359
-
360
- bool parse_cpu_mask(const std::string & mask, bool (&boolmask)[LM_GGML_MAX_N_THREADS]) {
361
- // Discard potential 0x prefix
362
- size_t start_i = 0;
363
- if (mask.length() >= 2 && mask.substr(0, 2) == "0x") {
364
- start_i = 2;
365
- }
366
-
367
- size_t num_digits = mask.length() - start_i;
368
- if (num_digits > 128) num_digits = 128;
369
-
370
- size_t end_i = num_digits + start_i;
371
-
372
- for (size_t i = start_i, n = (num_digits*4 - 1); i < end_i; i++, n-=4) {
373
- char c = mask.at(i);
374
- int8_t id = c;
375
-
376
- if ((c >= '0' && c <= '9')) {
377
- id -= '0';
378
- } else if (c >= 'a' && c <= 'f') {
379
- id -= 'a' - 10;
380
- } else if (c >= 'A' && c <= 'F') {
381
- id -= 'A' - 10;
382
- } else {
383
- LOG_ERR("Invalid hex character '%c' at position %d\n", c, int32_t(i));
384
- return false;
385
- }
386
-
387
- boolmask[ n ] = boolmask[ n ] || ((id & 8) != 0);
388
- boolmask[n - 1] = boolmask[n - 1] || ((id & 4) != 0);
389
- boolmask[n - 2] = boolmask[n - 2] || ((id & 2) != 0);
390
- boolmask[n - 3] = boolmask[n - 3] || ((id & 1) != 0);
391
- }
392
-
393
- return true;
394
- }
395
-
396
- void common_init() {
397
- llama_log_set([](lm_ggml_log_level level, const char * text, void * /*user_data*/) {
398
- if (LOG_DEFAULT_LLAMA <= common_log_verbosity_thold) {
399
- common_log_add(common_log_main(), level, "%s", text);
400
- }
401
- }, NULL);
402
-
403
- #ifdef NDEBUG
404
- const char * build_type = "";
405
- #else
406
- const char * build_type = " (debug)";
407
- #endif
408
-
409
- LOG_INF("build: %d (%s) with %s for %s%s\n", LLAMA_BUILD_NUMBER, LLAMA_COMMIT, LLAMA_COMPILER, LLAMA_BUILD_TARGET, build_type);
410
- }
411
-
412
- std::string common_params_get_system_info(const common_params & params) {
413
- std::ostringstream os;
414
-
415
- os << "system_info: n_threads = " << params.cpuparams.n_threads;
416
- if (params.cpuparams_batch.n_threads != -1) {
417
- os << " (n_threads_batch = " << params.cpuparams_batch.n_threads << ")";
418
- }
419
- #if defined(_WIN32) && (_WIN32_WINNT >= 0x0601) && !defined(__MINGW64__) // windows 7 and later
420
- // TODO: windows + arm64 + mingw64
421
- DWORD logicalProcessorCount = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
422
- os << " / " << logicalProcessorCount << " | " << llama_print_system_info();
423
- #else
424
- os << " / " << std::thread::hardware_concurrency() << " | " << llama_print_system_info();
425
- #endif
426
-
427
- return os.str();
428
- }
429
-
430
- //
431
- // String utils
432
- //
433
-
434
- std::string string_format(const char * fmt, ...) {
435
- va_list ap;
436
- va_list ap2;
437
- va_start(ap, fmt);
438
- va_copy(ap2, ap);
439
- int size = vsnprintf(NULL, 0, fmt, ap);
440
- LM_GGML_ASSERT(size >= 0 && size < INT_MAX); // NOLINT
441
- std::vector<char> buf(size + 1);
442
- int size2 = vsnprintf(buf.data(), size + 1, fmt, ap2);
443
- LM_GGML_ASSERT(size2 == size);
444
- va_end(ap2);
445
- va_end(ap);
446
- return std::string(buf.data(), size);
447
- }
448
-
449
- std::string string_strip(const std::string & str) {
450
- size_t start = 0;
451
- size_t end = str.size();
452
- while (start < end && std::isspace(str[start])) {
453
- start++;
454
- }
455
- while (end > start && std::isspace(str[end - 1])) {
456
- end--;
457
- }
458
- return str.substr(start, end - start);
459
- }
460
-
461
- std::string string_get_sortable_timestamp() {
462
- using clock = std::chrono::system_clock;
463
-
464
- const clock::time_point current_time = clock::now();
465
- const time_t as_time_t = clock::to_time_t(current_time);
466
- char timestamp_no_ns[100];
467
- std::strftime(timestamp_no_ns, 100, "%Y_%m_%d-%H_%M_%S", std::localtime(&as_time_t));
468
-
469
- const int64_t ns = std::chrono::duration_cast<std::chrono::nanoseconds>(
470
- current_time.time_since_epoch() % 1000000000).count();
471
- char timestamp_ns[11];
472
- snprintf(timestamp_ns, 11, "%09" PRId64, ns);
473
-
474
- return std::string(timestamp_no_ns) + "." + std::string(timestamp_ns);
475
- }
476
-
477
- void string_replace_all(std::string & s, const std::string & search, const std::string & replace) {
478
- if (search.empty()) {
479
- return;
480
- }
481
- std::string builder;
482
- builder.reserve(s.length());
483
- size_t pos = 0;
484
- size_t last_pos = 0;
485
- while ((pos = s.find(search, last_pos)) != std::string::npos) {
486
- builder.append(s, last_pos, pos - last_pos);
487
- builder.append(replace);
488
- last_pos = pos + search.length();
489
- }
490
- builder.append(s, last_pos, std::string::npos);
491
- s = std::move(builder);
492
- }
493
-
494
- std::string string_join(const std::vector<std::string> & values, const std::string & separator) {
495
- std::ostringstream result;
496
- for (size_t i = 0; i < values.size(); ++i) {
497
- if (i > 0) {
498
- result << separator;
499
- }
500
- result << values[i];
501
- }
502
- return result.str();
503
- }
504
-
505
- std::vector<std::string> string_split(const std::string & str, const std::string & delimiter) {
506
- std::vector<std::string> parts;
507
- size_t start = 0;
508
- size_t end = str.find(delimiter);
509
-
510
- while (end != std::string::npos) {
511
- parts.push_back(str.substr(start, end - start));
512
- start = end + delimiter.length();
513
- end = str.find(delimiter, start);
514
- }
515
-
516
- parts.push_back(str.substr(start));
517
-
518
- return parts;
519
- }
520
-
521
- std::string string_repeat(const std::string & str, size_t n) {
522
- if (n == 0) {
523
- return "";
524
- }
525
-
526
- std::string result;
527
- result.reserve(str.length() * n);
528
-
529
- for (size_t i = 0; i < n; ++i) {
530
- result += str;
531
- }
532
-
533
- return result;
534
- }
535
-
536
- std::string string_from(bool value) {
537
- return value ? "true" : "false";
538
- }
539
-
540
- std::string string_from(const std::vector<int> & values) {
541
- std::stringstream buf;
542
-
543
- buf << "[ ";
544
- bool first = true;
545
- for (auto e : values) {
546
- if (first) {
547
- first = false;
548
- } else {
549
- buf << ", ";
550
- }
551
- buf << std::to_string(e);
552
- }
553
- buf << " ]";
554
-
555
- return buf.str();
556
- }
557
-
558
- std::string string_from(const struct llama_context * ctx, const std::vector<llama_token> & tokens) {
559
- std::stringstream buf;
560
-
561
- buf << "[ ";
562
-
563
- bool first = true;
564
- for (const auto & token : tokens) {
565
- if (!first) {
566
- buf << ", ";
567
- } else {
568
- first = false;
569
- }
570
-
571
- auto detokenized = common_token_to_piece(ctx, token);
572
-
573
- detokenized.erase(
574
- std::remove_if(
575
- detokenized.begin(),
576
- detokenized.end(),
577
- [](const unsigned char c) { return !std::isprint(c); }),
578
- detokenized.end());
579
-
580
- buf << "'" << detokenized << "'"
581
- << ":" << std::to_string(token);
582
- }
583
-
584
- buf << " ]";
585
-
586
- return buf.str();
587
- }
588
-
589
- std::string string_from(const struct llama_context * ctx, const struct llama_batch & batch) {
590
- std::stringstream buf;
591
-
592
- buf << "[ ";
593
-
594
- bool first = true;
595
- for (int i = 0; i < batch.n_tokens; ++i) {
596
- if (!first) {
597
- buf << ", ";
598
- } else {
599
- first = false;
600
- }
601
-
602
- auto detokenized = common_token_to_piece(ctx, batch.token[i]);
603
-
604
- detokenized.erase(
605
- std::remove_if(
606
- detokenized.begin(),
607
- detokenized.end(),
608
- [](const unsigned char c) { return !std::isprint(c); }),
609
- detokenized.end());
610
-
611
- buf << "\n" << std::to_string(i)
612
- << ", token '" << detokenized << "'"
613
- << ", pos " << std::to_string(batch.pos[i])
614
- << ", n_seq_id " << std::to_string(batch.n_seq_id[i])
615
- << ", seq_id " << std::to_string(batch.seq_id[i][0])
616
- << ", logits " << std::to_string(batch.logits[i]);
617
- }
618
-
619
- buf << " ]";
620
-
621
- return buf.str();
622
- }
623
-
624
- void string_process_escapes(std::string & input) {
625
- std::size_t input_len = input.length();
626
- std::size_t output_idx = 0;
627
-
628
- for (std::size_t input_idx = 0; input_idx < input_len; ++input_idx) {
629
- if (input[input_idx] == '\\' && input_idx + 1 < input_len) {
630
- switch (input[++input_idx]) {
631
- case 'n': input[output_idx++] = '\n'; break;
632
- case 'r': input[output_idx++] = '\r'; break;
633
- case 't': input[output_idx++] = '\t'; break;
634
- case '\'': input[output_idx++] = '\''; break;
635
- case '\"': input[output_idx++] = '\"'; break;
636
- case '\\': input[output_idx++] = '\\'; break;
637
- case 'x':
638
- // Handle \x12, etc
639
- if (input_idx + 2 < input_len) {
640
- const char x[3] = { input[input_idx + 1], input[input_idx + 2], 0 };
641
- char *err_p = nullptr;
642
- const long val = std::strtol(x, &err_p, 16);
643
- if (err_p == x + 2) {
644
- input_idx += 2;
645
- input[output_idx++] = char(val);
646
- break;
647
- }
648
- }
649
- // fall through
650
- default: input[output_idx++] = '\\';
651
- input[output_idx++] = input[input_idx]; break;
652
- }
653
- } else {
654
- input[output_idx++] = input[input_idx];
655
- }
656
- }
657
-
658
- input.resize(output_idx);
659
- }
660
-
661
- bool string_parse_kv_override(const char * data, std::vector<llama_model_kv_override> & overrides) {
662
- const char * sep = strchr(data, '=');
663
- if (sep == nullptr || sep - data >= 128) {
664
- LOG_ERR("%s: malformed KV override '%s'\n", __func__, data);
665
- return false;
666
- }
667
- llama_model_kv_override kvo;
668
- std::strncpy(kvo.key, data, sep - data);
669
- kvo.key[sep - data] = 0;
670
- sep++;
671
- if (strncmp(sep, "int:", 4) == 0) {
672
- sep += 4;
673
- kvo.tag = LLAMA_KV_OVERRIDE_TYPE_INT;
674
- kvo.val_i64 = std::atol(sep);
675
- } else if (strncmp(sep, "float:", 6) == 0) {
676
- sep += 6;
677
- kvo.tag = LLAMA_KV_OVERRIDE_TYPE_FLOAT;
678
- kvo.val_f64 = std::atof(sep);
679
- } else if (strncmp(sep, "bool:", 5) == 0) {
680
- sep += 5;
681
- kvo.tag = LLAMA_KV_OVERRIDE_TYPE_BOOL;
682
- if (std::strcmp(sep, "true") == 0) {
683
- kvo.val_bool = true;
684
- } else if (std::strcmp(sep, "false") == 0) {
685
- kvo.val_bool = false;
686
- } else {
687
- LOG_ERR("%s: invalid boolean value for KV override '%s'\n", __func__, data);
688
- return false;
689
- }
690
- } else if (strncmp(sep, "str:", 4) == 0) {
691
- sep += 4;
692
- kvo.tag = LLAMA_KV_OVERRIDE_TYPE_STR;
693
- if (strlen(sep) > 127) {
694
- LOG_ERR("%s: malformed KV override '%s', value cannot exceed 127 chars\n", __func__, data);
695
- return false;
696
- }
697
- strncpy(kvo.val_str, sep, 127);
698
- kvo.val_str[127] = '\0';
699
- } else {
700
- LOG_ERR("%s: invalid type for KV override '%s'\n", __func__, data);
701
- return false;
702
- }
703
- overrides.emplace_back(std::move(kvo));
704
- return true;
705
- }
706
-
707
- //
708
- // Filesystem utils
709
- //
710
-
711
- // Validate if a filename is safe to use
712
- // To validate a full path, split the path by the OS-specific path separator, and validate each part with this function
713
- bool fs_validate_filename(const std::string & filename) {
714
- if (!filename.length()) {
715
- // Empty filename invalid
716
- return false;
717
- }
718
- if (filename.length() > 255) {
719
- // Limit at common largest possible filename on Linux filesystems
720
- // to avoid unnecessary further validation
721
- // (On systems with smaller limits it will be caught by the OS)
722
- return false;
723
- }
724
-
725
- std::u32string filename_utf32;
726
- try {
727
- #if defined(__clang__)
728
- // disable C++17 deprecation warning for std::codecvt_utf8
729
- # pragma clang diagnostic push
730
- # pragma clang diagnostic ignored "-Wdeprecated-declarations"
731
- #endif
732
- std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> converter;
733
-
734
- #if defined(__clang__)
735
- # pragma clang diagnostic pop
736
- #endif
737
-
738
- filename_utf32 = converter.from_bytes(filename);
739
-
740
- // If the reverse conversion mismatches, it means overlong UTF-8 sequences were used,
741
- // or invalid encodings were encountered. Reject such attempts
742
- std::string filename_reencoded = converter.to_bytes(filename_utf32);
743
- if (filename_reencoded != filename) {
744
- return false;
745
- }
746
- } catch (const std::exception &) {
747
- return false;
748
- }
749
-
750
- // Check for forbidden codepoints:
751
- // - Control characters
752
- // - Unicode equivalents of illegal characters
753
- // - UTF-16 surrogate pairs
754
- // - UTF-8 replacement character
755
- // - Byte order mark (BOM)
756
- // - Illegal characters: / \ : * ? " < > |
757
- for (char32_t c : filename_utf32) {
758
- if (c <= 0x1F // Control characters (C0)
759
- || c == 0x7F // Control characters (DEL)
760
- || (c >= 0x80 && c <= 0x9F) // Control characters (C1)
761
- || c == 0xFF0E // Fullwidth Full Stop (period equivalent)
762
- || c == 0x2215 // Division Slash (forward slash equivalent)
763
- || c == 0x2216 // Set Minus (backslash equivalent)
764
- || (c >= 0xD800 && c <= 0xDFFF) // UTF-16 surrogate pairs
765
- || c == 0xFFFD // Replacement Character (UTF-8)
766
- || c == 0xFEFF // Byte Order Mark (BOM)
767
- || c == '/' || c == '\\' || c == ':' || c == '*' // Illegal characters
768
- || c == '?' || c == '"' || c == '<' || c == '>' || c == '|') {
769
- return false;
770
- }
771
- }
772
-
773
- // Reject any leading or trailing ' ', or any trailing '.', these are stripped on Windows and will cause a different filename
774
- // Unicode and other whitespace is not affected, only 0x20 space
775
- if (filename.front() == ' ' || filename.back() == ' ' || filename.back() == '.') {
776
- return false;
777
- }
778
-
779
- // Reject any ".." (currently stricter than necessary, it should be fine to just check for == ".." instead)
780
- if (filename.find("..") != std::string::npos) {
781
- return false;
782
- }
783
-
784
- // Reject "."
785
- if (filename == ".") {
786
- return false;
787
- }
788
-
789
- return true;
790
- }
791
-
792
- // returns true if successful, false otherwise
793
- bool fs_create_directory_with_parents(const std::string & path) {
794
- #ifdef _WIN32
795
- std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
796
- std::wstring wpath = converter.from_bytes(path);
797
-
798
- // if the path already exists, check whether it's a directory
799
- const DWORD attributes = GetFileAttributesW(wpath.c_str());
800
- if ((attributes != INVALID_FILE_ATTRIBUTES) && (attributes & FILE_ATTRIBUTE_DIRECTORY)) {
801
- return true;
802
- }
803
-
804
- size_t pos_slash = 0;
805
-
806
- // process path from front to back, procedurally creating directories
807
- while ((pos_slash = path.find('\\', pos_slash)) != std::string::npos) {
808
- const std::wstring subpath = wpath.substr(0, pos_slash);
809
- const wchar_t * test = subpath.c_str();
810
-
811
- const bool success = CreateDirectoryW(test, NULL);
812
- if (!success) {
813
- const DWORD error = GetLastError();
814
-
815
- // if the path already exists, ensure that it's a directory
816
- if (error == ERROR_ALREADY_EXISTS) {
817
- const DWORD attributes = GetFileAttributesW(subpath.c_str());
818
- if (attributes == INVALID_FILE_ATTRIBUTES || !(attributes & FILE_ATTRIBUTE_DIRECTORY)) {
819
- return false;
820
- }
821
- } else {
822
- return false;
823
- }
824
- }
825
-
826
- pos_slash += 1;
827
- }
828
-
829
- return true;
830
- #else
831
- // if the path already exists, check whether it's a directory
832
- struct stat info;
833
- if (stat(path.c_str(), &info) == 0) {
834
- return S_ISDIR(info.st_mode);
835
- }
836
-
837
- size_t pos_slash = 1; // skip leading slashes for directory creation
838
-
839
- // process path from front to back, procedurally creating directories
840
- while ((pos_slash = path.find('/', pos_slash)) != std::string::npos) {
841
- const std::string subpath = path.substr(0, pos_slash);
842
- struct stat info;
843
-
844
- // if the path already exists, ensure that it's a directory
845
- if (stat(subpath.c_str(), &info) == 0) {
846
- if (!S_ISDIR(info.st_mode)) {
847
- return false;
848
- }
849
- } else {
850
- // create parent directories
851
- const int ret = mkdir(subpath.c_str(), 0755);
852
- if (ret != 0) {
853
- return false;
854
- }
855
- }
856
-
857
- pos_slash += 1;
858
- }
859
-
860
- return true;
861
- #endif // _WIN32
862
- }
863
-
864
- std::string fs_get_cache_directory() {
865
- std::string cache_directory = "";
866
- auto ensure_trailing_slash = [](std::string p) {
867
- // Make sure to add trailing slash
868
- if (p.back() != DIRECTORY_SEPARATOR) {
869
- p += DIRECTORY_SEPARATOR;
870
- }
871
- return p;
872
- };
873
- if (getenv("LLAMA_CACHE")) {
874
- cache_directory = std::getenv("LLAMA_CACHE");
875
- } else {
876
- #ifdef __linux__
877
- if (std::getenv("XDG_CACHE_HOME")) {
878
- cache_directory = std::getenv("XDG_CACHE_HOME");
879
- } else {
880
- cache_directory = std::getenv("HOME") + std::string("/.cache/");
881
- }
882
- #elif defined(__APPLE__)
883
- cache_directory = std::getenv("HOME") + std::string("/Library/Caches/");
884
- #elif defined(_WIN32)
885
- cache_directory = std::getenv("LOCALAPPDATA");
886
- #endif // __linux__
887
- cache_directory = ensure_trailing_slash(cache_directory);
888
- cache_directory += "llama.cpp";
889
- }
890
- return ensure_trailing_slash(cache_directory);
891
- }
892
-
893
- std::string fs_get_cache_file(const std::string & filename) {
894
- LM_GGML_ASSERT(filename.find(DIRECTORY_SEPARATOR) == std::string::npos);
895
- std::string cache_directory = fs_get_cache_directory();
896
- const bool success = fs_create_directory_with_parents(cache_directory);
897
- if (!success) {
898
- throw std::runtime_error("failed to create cache directory: " + cache_directory);
899
- }
900
- return cache_directory + filename;
901
- }
902
-
903
-
904
- //
905
- // Model utils
906
- //
907
- struct common_init_result common_init_from_params(common_params & params) {
908
- common_init_result iparams;
909
- auto mparams = common_model_params_to_llama(params);
910
-
911
- llama_model * model = nullptr;
912
-
913
- if (!params.hf_repo.empty() && !params.hf_file.empty()) {
914
- model = common_load_model_from_hf(params.hf_repo, params.hf_file, params.model, params.hf_token, mparams);
915
- } else if (!params.model_url.empty()) {
916
- model = common_load_model_from_url(params.model_url, params.model, params.hf_token, mparams);
917
- } else {
918
- model = llama_model_load_from_file(params.model.c_str(), mparams);
919
- }
920
-
921
- if (model == NULL) {
922
- LOG_ERR("%s: failed to load model '%s'\n", __func__, params.model.c_str());
923
- return iparams;
924
- }
925
-
926
- const llama_vocab * vocab = llama_model_get_vocab(model);
927
-
928
- if (params.reranking) {
929
- bool ok = true;
930
-
931
- if (llama_vocab_bos(vocab) == LLAMA_TOKEN_NULL) {
932
- LOG_WRN("%s: warning: vocab does not have a BOS token, reranking will not work\n", __func__);
933
- ok = false;
934
- }
935
-
936
- if (llama_vocab_eos(vocab) == LLAMA_TOKEN_NULL) {
937
- LOG_WRN("%s: warning: vocab does not have an EOS token, reranking will not work\n", __func__);
938
- ok = false;
939
- }
940
-
941
- if (llama_vocab_sep(vocab) == LLAMA_TOKEN_NULL) {
942
- LOG_WRN("%s: warning: vocab does not have a SEP token, reranking will not work\n", __func__);
943
- ok = false;
944
- }
945
-
946
- if (!ok) {
947
- llama_model_free(model);
948
-
949
- return iparams;
950
- }
951
- }
952
-
953
- auto cparams = common_context_params_to_llama(params);
954
-
955
- llama_context * lctx = llama_init_from_model(model, cparams);
956
- if (lctx == NULL) {
957
- LOG_ERR("%s: failed to create context with model '%s'\n", __func__, params.model.c_str());
958
- llama_model_free(model);
959
- return iparams;
960
- }
961
-
962
- if (params.ctx_shift && !llama_kv_cache_can_shift(lctx)) {
963
- LOG_WRN("%s: KV cache shifting is not supported for this model, disabling KV cache shifting\n", __func__);
964
- params.ctx_shift = false;
965
- }
966
-
967
- if (!params.control_vectors.empty()) {
968
- if (params.control_vector_layer_start <= 0) params.control_vector_layer_start = 1;
969
- if (params.control_vector_layer_end <= 0) params.control_vector_layer_end = llama_model_n_layer(model);
970
-
971
- const auto cvec = common_control_vector_load(params.control_vectors);
972
- if (cvec.n_embd == -1) {
973
- llama_free(lctx);
974
- llama_model_free(model);
975
-
976
- return iparams;
977
- }
978
-
979
- int err = llama_apply_adapter_cvec(
980
- lctx,
981
- cvec.data.data(),
982
- cvec.data.size(),
983
- cvec.n_embd,
984
- params.control_vector_layer_start,
985
- params.control_vector_layer_end);
986
- if (err) {
987
- llama_free(lctx);
988
- llama_model_free(model);
989
-
990
- return iparams;
991
- }
992
- }
993
-
994
- // load and optionally apply lora adapters
995
- for (auto & la : params.lora_adapters) {
996
- llama_adapter_lora_ptr lora;
997
- lora.reset(llama_adapter_lora_init(model, la.path.c_str()));
998
- if (lora == nullptr) {
999
- LOG_ERR("%s: failed to apply lora adapter '%s'\n", __func__, la.path.c_str());
1000
- llama_free(lctx);
1001
- llama_model_free(model);
1002
- return iparams;
1003
- }
1004
-
1005
- la.ptr = lora.get();
1006
- iparams.lora.emplace_back(std::move(lora)); // copy to list of loaded adapters
1007
- }
1008
-
1009
- if (!params.lora_init_without_apply) {
1010
- common_set_adapter_lora(lctx, params.lora_adapters);
1011
- }
1012
-
1013
- if (params.sampling.ignore_eos && llama_vocab_eos(vocab) == LLAMA_TOKEN_NULL) {
1014
- LOG_WRN("%s: warning: vocab does not have an EOS token, ignoring --ignore-eos\n", __func__);
1015
- params.sampling.ignore_eos = false;
1016
- }
1017
-
1018
- if (params.sampling.ignore_eos) {
1019
- for (llama_token i = 0; i < llama_vocab_n_tokens(vocab); i++) {
1020
- if (llama_vocab_is_eog(vocab, i)) {
1021
- LOG_INF("%s: added %s logit bias = %f\n", __func__, common_token_to_piece(lctx, i).c_str(), -INFINITY);
1022
- params.sampling.logit_bias.push_back({i, -INFINITY});
1023
- }
1024
- }
1025
- }
1026
-
1027
- if (params.sampling.penalty_last_n == -1) {
1028
- LOG_INF("%s: setting penalty_last_n to ctx_size = %d\n", __func__, llama_n_ctx(lctx));
1029
- params.sampling.penalty_last_n = llama_n_ctx(lctx);
1030
- }
1031
-
1032
- if (params.sampling.dry_penalty_last_n == -1) {
1033
- LOG_INF("%s: setting dry_penalty_last_n to ctx_size = %d\n", __func__, llama_n_ctx(lctx));
1034
- params.sampling.dry_penalty_last_n = llama_n_ctx(lctx);
1035
- }
1036
-
1037
- if (params.warmup) {
1038
- LOG_WRN("%s: warming up the model with an empty run - please wait ... (--no-warmup to disable)\n", __func__);
1039
-
1040
- std::vector<llama_token> tmp;
1041
- llama_token bos = llama_vocab_bos(vocab);
1042
- llama_token eos = llama_vocab_eos(vocab);
1043
-
1044
- // some models (e.g. T5) don't have a BOS token
1045
- if (bos != LLAMA_TOKEN_NULL) {
1046
- tmp.push_back(bos);
1047
- }
1048
- if (eos != LLAMA_TOKEN_NULL) {
1049
- tmp.push_back(eos);
1050
- }
1051
- if (tmp.empty()) {
1052
- tmp.push_back(0);
1053
- }
1054
-
1055
- if (llama_model_has_encoder(model)) {
1056
- llama_encode(lctx, llama_batch_get_one(tmp.data(), tmp.size()));
1057
- llama_token decoder_start_token_id = llama_model_decoder_start_token(model);
1058
- if (decoder_start_token_id == LLAMA_TOKEN_NULL) {
1059
- decoder_start_token_id = bos;
1060
- }
1061
- tmp.clear();
1062
- tmp.push_back(decoder_start_token_id);
1063
- }
1064
- if (llama_model_has_decoder(model)) {
1065
- llama_decode(lctx, llama_batch_get_one(tmp.data(), std::min(tmp.size(), (size_t) params.n_batch)));
1066
- }
1067
- llama_kv_cache_clear(lctx);
1068
- llama_synchronize(lctx);
1069
- llama_perf_context_reset(lctx);
1070
- }
1071
-
1072
- iparams.model.reset(model);
1073
- iparams.context.reset(lctx);
1074
-
1075
- return iparams;
1076
- }
1077
-
1078
- void common_set_adapter_lora(struct llama_context * ctx, std::vector<common_adapter_lora_info> & lora) {
1079
- llama_clear_adapter_lora(ctx);
1080
- for (auto & la : lora) {
1081
- if (la.scale != 0.0f) {
1082
- llama_set_adapter_lora(ctx, la.ptr, la.scale);
1083
- }
1084
- }
1085
- }
1086
-
1087
- struct llama_model_params common_model_params_to_llama(common_params & params) {
1088
- auto mparams = llama_model_default_params();
1089
-
1090
- if (!params.devices.empty()) {
1091
- mparams.devices = params.devices.data();
1092
- }
1093
- if (params.n_gpu_layers != -1) {
1094
- mparams.n_gpu_layers = params.n_gpu_layers;
1095
- }
1096
-
1097
- mparams.progress_callback_user_data = params.progress_callback_user_data;
1098
- mparams.progress_callback = params.progress_callback;
1099
- mparams.vocab_only = params.vocab_only;
1100
- mparams.main_gpu = params.main_gpu;
1101
- mparams.split_mode = params.split_mode;
1102
- mparams.tensor_split = params.tensor_split;
1103
- mparams.use_mmap = params.use_mmap;
1104
- mparams.use_mlock = params.use_mlock;
1105
- mparams.check_tensors = params.check_tensors;
1106
- if (params.kv_overrides.empty()) {
1107
- mparams.kv_overrides = NULL;
1108
- } else {
1109
- LM_GGML_ASSERT(params.kv_overrides.back().key[0] == 0 && "KV overrides not terminated with empty key");
1110
- mparams.kv_overrides = params.kv_overrides.data();
1111
- }
1112
-
1113
- return mparams;
1114
- }
1115
-
1116
- struct llama_context_params common_context_params_to_llama(const common_params & params) {
1117
- auto cparams = llama_context_default_params();
1118
-
1119
- cparams.n_ctx = params.n_ctx;
1120
- cparams.n_seq_max = params.n_parallel;
1121
- cparams.n_batch = params.n_batch;
1122
- cparams.n_ubatch = params.n_ubatch;
1123
- cparams.n_threads = params.cpuparams.n_threads;
1124
- cparams.n_threads_batch = params.cpuparams_batch.n_threads == -1 ?
1125
- params.cpuparams.n_threads : params.cpuparams_batch.n_threads;
1126
- cparams.logits_all = params.logits_all;
1127
- cparams.embeddings = params.embedding;
1128
- cparams.rope_scaling_type = params.rope_scaling_type;
1129
- cparams.rope_freq_base = params.rope_freq_base;
1130
- cparams.rope_freq_scale = params.rope_freq_scale;
1131
- cparams.yarn_ext_factor = params.yarn_ext_factor;
1132
- cparams.yarn_attn_factor = params.yarn_attn_factor;
1133
- cparams.yarn_beta_fast = params.yarn_beta_fast;
1134
- cparams.yarn_beta_slow = params.yarn_beta_slow;
1135
- cparams.yarn_orig_ctx = params.yarn_orig_ctx;
1136
- cparams.pooling_type = params.pooling_type;
1137
- cparams.attention_type = params.attention_type;
1138
- cparams.defrag_thold = params.defrag_thold;
1139
- cparams.cb_eval = params.cb_eval;
1140
- cparams.cb_eval_user_data = params.cb_eval_user_data;
1141
- cparams.offload_kqv = !params.no_kv_offload;
1142
- cparams.flash_attn = params.flash_attn;
1143
- cparams.no_perf = params.no_perf;
1144
-
1145
- if (params.reranking) {
1146
- cparams.embeddings = true;
1147
- cparams.pooling_type = LLAMA_POOLING_TYPE_RANK;
1148
- }
1149
-
1150
- cparams.type_k = params.cache_type_k;
1151
- cparams.type_v = params.cache_type_v;
1152
-
1153
- return cparams;
1154
- }
1155
-
1156
- struct lm_ggml_threadpool_params lm_ggml_threadpool_params_from_cpu_params(const cpu_params & params) {
1157
- struct lm_ggml_threadpool_params tpp;
1158
-
1159
- lm_ggml_threadpool_params_init(&tpp, params.n_threads); // setup the defaults
1160
-
1161
- if (params.mask_valid) {
1162
- std::memcpy(&tpp.cpumask, &params.cpumask, LM_GGML_MAX_N_THREADS);
1163
- }
1164
-
1165
- tpp.prio = params.priority;
1166
- tpp.poll = params.poll;
1167
- tpp.strict_cpu = params.strict_cpu;
1168
-
1169
- return tpp;
1170
- }
1171
-
1172
- #ifdef LLAMA_USE_CURL
1173
-
1174
- #define CURL_MAX_RETRY 3
1175
- #define CURL_RETRY_DELAY_SECONDS 2
1176
-
1177
- static bool curl_perform_with_retry(const std::string & url, CURL * curl, int max_attempts, int retry_delay_seconds) {
1178
- int remaining_attempts = max_attempts;
1179
-
1180
- while (remaining_attempts > 0) {
1181
- LOG_INF("%s: Trying to download from %s (attempt %d of %d)...\n", __func__ , url.c_str(), max_attempts - remaining_attempts + 1, max_attempts);
1182
-
1183
- CURLcode res = curl_easy_perform(curl);
1184
- if (res == CURLE_OK) {
1185
- return true;
1186
- }
1187
-
1188
- int exponential_backoff_delay = std::pow(retry_delay_seconds, max_attempts - remaining_attempts) * 1000;
1189
- LOG_WRN("%s: curl_easy_perform() failed: %s, retrying after %d milliseconds...\n", __func__, curl_easy_strerror(res), exponential_backoff_delay);
1190
-
1191
- remaining_attempts--;
1192
- std::this_thread::sleep_for(std::chrono::milliseconds(exponential_backoff_delay));
1193
- }
1194
-
1195
- LOG_ERR("%s: curl_easy_perform() failed after %d attempts\n", __func__, max_attempts);
1196
-
1197
- return false;
1198
- }
1199
-
1200
-
1201
- struct llama_model * common_load_model_from_url(
1202
- const std::string & model_url,
1203
- const std::string & local_path,
1204
- const std::string & hf_token,
1205
- const struct llama_model_params & params) {
1206
- // Basic validation of the model_url
1207
- if (model_url.empty()) {
1208
- LOG_ERR("%s: invalid model_url\n", __func__);
1209
- return NULL;
1210
- }
1211
-
1212
- if (!common_download_file(model_url, local_path, hf_token)) {
1213
- return NULL;
1214
- }
1215
-
1216
- // check for additional GGUFs split to download
1217
- int n_split = 0;
1218
- {
1219
- struct lm_gguf_init_params lm_gguf_params = {
1220
- /*.no_alloc = */ true,
1221
- /*.ctx = */ NULL,
1222
- };
1223
- auto * ctx_gguf = lm_gguf_init_from_file(local_path.c_str(), lm_gguf_params);
1224
- if (!ctx_gguf) {
1225
- LOG_ERR("\n%s: failed to load input GGUF from %s\n", __func__, local_path.c_str());
1226
- return NULL;
1227
- }
1228
-
1229
- auto key_n_split = lm_gguf_find_key(ctx_gguf, LLM_KV_SPLIT_COUNT);
1230
- if (key_n_split >= 0) {
1231
- n_split = lm_gguf_get_val_u16(ctx_gguf, key_n_split);
1232
- }
1233
-
1234
- lm_gguf_free(ctx_gguf);
1235
- }
1236
-
1237
- if (n_split > 1) {
1238
- char split_prefix[PATH_MAX] = {0};
1239
- char split_url_prefix[LLAMA_CURL_MAX_URL_LENGTH] = {0};
1240
-
1241
- // Verify the first split file format
1242
- // and extract split URL and PATH prefixes
1243
- {
1244
- if (!llama_split_prefix(split_prefix, sizeof(split_prefix), local_path.c_str(), 0, n_split)) {
1245
- LOG_ERR("\n%s: unexpected model file name: %s n_split=%d\n", __func__, local_path.c_str(), n_split);
1246
- return NULL;
1247
- }
1248
-
1249
- if (!llama_split_prefix(split_url_prefix, sizeof(split_url_prefix), model_url.c_str(), 0, n_split)) {
1250
- LOG_ERR("\n%s: unexpected model url: %s n_split=%d\n", __func__, model_url.c_str(), n_split);
1251
- return NULL;
1252
- }
1253
- }
1254
-
1255
- // Prepare download in parallel
1256
- std::vector<std::future<bool>> futures_download;
1257
- for (int idx = 1; idx < n_split; idx++) {
1258
- futures_download.push_back(std::async(std::launch::async, [&split_prefix, &split_url_prefix, &n_split, hf_token](int download_idx) -> bool {
1259
- char split_path[PATH_MAX] = {0};
1260
- llama_split_path(split_path, sizeof(split_path), split_prefix, download_idx, n_split);
1261
-
1262
- char split_url[LLAMA_CURL_MAX_URL_LENGTH] = {0};
1263
- llama_split_path(split_url, sizeof(split_url), split_url_prefix, download_idx, n_split);
1264
-
1265
- return common_download_file(split_url, split_path, hf_token);
1266
- }, idx));
1267
- }
1268
-
1269
- // Wait for all downloads to complete
1270
- for (auto & f : futures_download) {
1271
- if (!f.get()) {
1272
- return NULL;
1273
- }
1274
- }
1275
- }
1276
-
1277
- return llama_model_load_from_file(local_path.c_str(), params);
1278
- }
1279
-
1280
- struct llama_model * common_load_model_from_hf(
1281
- const std::string & repo,
1282
- const std::string & remote_path,
1283
- const std::string & local_path,
1284
- const std::string & hf_token,
1285
- const struct llama_model_params & params) {
1286
- // construct hugging face model url:
1287
- //
1288
- // --repo ggml-org/models --file tinyllama-1.1b/ggml-model-f16.gguf
1289
- // https://huggingface.co/ggml-org/models/resolve/main/tinyllama-1.1b/ggml-model-f16.gguf
1290
- //
1291
- // --repo TheBloke/Mixtral-8x7B-v0.1-GGUF --file mixtral-8x7b-v0.1.Q4_K_M.gguf
1292
- // https://huggingface.co/TheBloke/Mixtral-8x7B-v0.1-GGUF/resolve/main/mixtral-8x7b-v0.1.Q4_K_M.gguf
1293
- //
1294
-
1295
- std::string model_url = "https://huggingface.co/";
1296
- model_url += repo;
1297
- model_url += "/resolve/main/";
1298
- model_url += remote_path;
1299
-
1300
- return common_load_model_from_url(model_url, local_path, hf_token, params);
1301
- }
1302
-
1303
- /**
1304
- * Allow getting the HF file from the HF repo with tag (like ollama), for example:
1305
- * - bartowski/Llama-3.2-3B-Instruct-GGUF:q4
1306
- * - bartowski/Llama-3.2-3B-Instruct-GGUF:Q4_K_M
1307
- * - bartowski/Llama-3.2-3B-Instruct-GGUF:q5_k_s
1308
- * Tag is optional, default to "latest" (meaning it checks for Q4_K_M first, then Q4, then if not found, return the first GGUF file in repo)
1309
- *
1310
- * Return pair of <repo, file> (with "repo" already having tag removed)
1311
- *
1312
- * Note: we use the Ollama-compatible HF API, but not using the blobId. Instead, we use the special "ggufFile" field which returns the value for "hf_file". This is done to be backward-compatible with existing cache files.
1313
- */
1314
- std::pair<std::string, std::string> common_get_hf_file(const std::string & hf_repo_with_tag, const std::string & hf_token) {
1315
- auto parts = string_split<std::string>(hf_repo_with_tag, ':');
1316
- std::string tag = parts.size() > 1 ? parts.back() : "latest";
1317
- std::string hf_repo = parts[0];
1318
- if (string_split<std::string>(hf_repo, '/').size() != 2) {
1319
- throw std::invalid_argument("error: invalid HF repo format, expected <user>/<model>[:quant]\n");
1320
- }
1321
-
1322
- // fetch model info from Hugging Face Hub API
1323
- json model_info;
1324
- curl_ptr curl(curl_easy_init(), &curl_easy_cleanup);
1325
- curl_slist_ptr http_headers;
1326
- std::string res_str;
1327
- std::string url = "https://huggingface.co/v2/" + hf_repo + "/manifests/" + tag;
1328
- curl_easy_setopt(curl.get(), CURLOPT_URL, url.c_str());
1329
- curl_easy_setopt(curl.get(), CURLOPT_NOPROGRESS, 1L);
1330
- typedef size_t(*CURLOPT_WRITEFUNCTION_PTR)(void * ptr, size_t size, size_t nmemb, void * data);
1331
- auto write_callback = [](void * ptr, size_t size, size_t nmemb, void * data) -> size_t {
1332
- static_cast<std::string *>(data)->append((char * ) ptr, size * nmemb);
1333
- return size * nmemb;
1334
- };
1335
- curl_easy_setopt(curl.get(), CURLOPT_WRITEFUNCTION, static_cast<CURLOPT_WRITEFUNCTION_PTR>(write_callback));
1336
- curl_easy_setopt(curl.get(), CURLOPT_WRITEDATA, &res_str);
1337
- #if defined(_WIN32)
1338
- curl_easy_setopt(curl.get(), CURLOPT_SSL_OPTIONS, CURLSSLOPT_NATIVE_CA);
1339
- #endif
1340
- if (!hf_token.empty()) {
1341
- std::string auth_header = "Authorization: Bearer " + hf_token;
1342
- http_headers.ptr = curl_slist_append(http_headers.ptr, auth_header.c_str());
1343
- }
1344
- // Important: the User-Agent must be "llama-cpp" to get the "ggufFile" field in the response
1345
- http_headers.ptr = curl_slist_append(http_headers.ptr, "User-Agent: llama-cpp");
1346
- http_headers.ptr = curl_slist_append(http_headers.ptr, "Accept: application/json");
1347
- curl_easy_setopt(curl.get(), CURLOPT_HTTPHEADER, http_headers.ptr);
1348
-
1349
- CURLcode res = curl_easy_perform(curl.get());
1350
-
1351
- if (res != CURLE_OK) {
1352
- throw std::runtime_error("error: cannot make GET request to HF API");
1353
- }
1354
-
1355
- long res_code;
1356
- curl_easy_getinfo(curl.get(), CURLINFO_RESPONSE_CODE, &res_code);
1357
- if (res_code == 200) {
1358
- model_info = json::parse(res_str);
1359
- } else if (res_code == 401) {
1360
- throw std::runtime_error("error: model is private or does not exist; if you are accessing a gated model, please provide a valid HF token");
1361
- } else {
1362
- throw std::runtime_error(string_format("error from HF API, response code: %ld, data: %s", res_code, res_str.c_str()));
1363
- }
1364
-
1365
- // check response
1366
- if (!model_info.contains("ggufFile")) {
1367
- throw std::runtime_error("error: model does not have ggufFile");
1368
- }
1369
- json & lm_gguf_file = model_info.at("ggufFile");
1370
- if (!lm_gguf_file.contains("rfilename")) {
1371
- throw std::runtime_error("error: ggufFile does not have rfilename");
1372
- }
1373
-
1374
- return std::make_pair(hf_repo, lm_gguf_file.at("rfilename"));
1375
- }
1376
-
1377
- #else
1378
-
1379
- struct llama_model * common_load_model_from_url(
1380
- const std::string & /*model_url*/,
1381
- const std::string & /*local_path*/,
1382
- const std::string & /*hf_token*/,
1383
- const struct llama_model_params & /*params*/) {
1384
- LOG_WRN("%s: llama.cpp built without libcurl, downloading from an url not supported.\n", __func__);
1385
- return nullptr;
1386
- }
1387
-
1388
- struct llama_model * common_load_model_from_hf(
1389
- const std::string & /*repo*/,
1390
- const std::string & /*remote_path*/,
1391
- const std::string & /*local_path*/,
1392
- const std::string & /*hf_token*/,
1393
- const struct llama_model_params & /*params*/) {
1394
- LOG_WRN("%s: llama.cpp built without libcurl, downloading from Hugging Face not supported.\n", __func__);
1395
- return nullptr;
1396
- }
1397
-
1398
- std::pair<std::string, std::string> common_get_hf_file(const std::string &, const std::string &) {
1399
- LOG_WRN("%s: llama.cpp built without libcurl, downloading from Hugging Face not supported.\n", __func__);
1400
- return std::make_pair("", "");
1401
- }
1402
-
1403
- #endif // LLAMA_USE_CURL
1404
-
1405
- //
1406
- // Batch utils
1407
- //
1408
-
1409
- void common_batch_clear(struct llama_batch & batch) {
1410
- batch.n_tokens = 0;
1411
- }
1412
-
1413
- void common_batch_add(
1414
- struct llama_batch & batch,
1415
- llama_token id,
1416
- llama_pos pos,
1417
- const std::vector<llama_seq_id> & seq_ids,
1418
- bool logits) {
1419
- LM_GGML_ASSERT(batch.seq_id[batch.n_tokens] && "llama_batch size exceeded");
1420
-
1421
- batch.token [batch.n_tokens] = id;
1422
- batch.pos [batch.n_tokens] = pos;
1423
- batch.n_seq_id[batch.n_tokens] = seq_ids.size();
1424
- for (size_t i = 0; i < seq_ids.size(); ++i) {
1425
- batch.seq_id[batch.n_tokens][i] = seq_ids[i];
1426
- }
1427
- batch.logits [batch.n_tokens] = logits;
1428
-
1429
- batch.n_tokens++;
1430
- }
1431
-
1432
- //
1433
- // Token utils
1434
- //
1435
-
1436
- size_t common_lcp(const llama_tokens & a, const llama_tokens & b) {
1437
- size_t i;
1438
- for (i = 0; i < a.size() && i < b.size() && a[i] == b[i]; i++) {}
1439
-
1440
- return i;
1441
- }
1442
-
1443
- size_t common_lcs(const llama_tokens & a, const llama_tokens & b) {
1444
- // check for empty sequences
1445
- if (a.empty() || b.empty()) {
1446
- return 0;
1447
- }
1448
-
1449
- // get the lengths of the input sequences
1450
- size_t a_len = a.size();
1451
- size_t b_len = b.size();
1452
-
1453
- // initialize the maximum length of the longest common subsequence (LCS)
1454
- size_t max_length = 0;
1455
-
1456
- // use two rows instead of a 2D matrix to optimize space
1457
- std::vector<size_t> prev_row(b_len + 1, 0);
1458
- std::vector<size_t> curr_row(b_len + 1, 0);
1459
-
1460
- // iterate through the elements of a
1461
- for (size_t i = 1; i <= a_len; i++) {
1462
- // iterate through the elements of b
1463
- for (size_t j = 1; j <= b_len; j++) {
1464
- // if elements at the current positions match
1465
- if (a[i - 1] == b[j - 1]) {
1466
- // if it's the first element of either sequences, set LCS length to 1
1467
- if (i == 1 || j == 1) {
1468
- curr_row[j] = 1;
1469
- } else {
1470
- // increment LCS length by 1 compared to the previous element
1471
- curr_row[j] = prev_row[j - 1] + 1;
1472
- }
1473
-
1474
- // update max_length if necessary
1475
- if (curr_row[j] > max_length) {
1476
- max_length = curr_row[j];
1477
- }
1478
- } else {
1479
- // reset LCS length if elements don't match
1480
- curr_row[j] = 0;
1481
- }
1482
- }
1483
-
1484
- // update the previous row for the next iteration
1485
- prev_row = curr_row;
1486
- }
1487
-
1488
- // return the maximum length of the LCS
1489
- return max_length;
1490
- }
1491
-
1492
- //
1493
- // Vocab utils
1494
- //
1495
-
1496
- std::vector<llama_token> common_tokenize(
1497
- const struct llama_context * ctx,
1498
- const std::string & text,
1499
- bool add_special,
1500
- bool parse_special) {
1501
- const llama_model * model = llama_get_model(ctx);
1502
- const llama_vocab * vocab = llama_model_get_vocab(model);
1503
- return common_tokenize(vocab, text, add_special, parse_special);
1504
- }
1505
-
1506
- std::vector<llama_token> common_tokenize(
1507
- const struct llama_vocab * vocab,
1508
- const std::string & text,
1509
- bool add_special,
1510
- bool parse_special) {
1511
- // upper limit for the number of tokens
1512
- int n_tokens = text.length() + 2 * add_special;
1513
- std::vector<llama_token> result(n_tokens);
1514
- n_tokens = llama_tokenize(vocab, text.data(), text.length(), result.data(), result.size(), add_special, parse_special);
1515
- if (n_tokens < 0) {
1516
- result.resize(-n_tokens);
1517
- int check = llama_tokenize(vocab, text.data(), text.length(), result.data(), result.size(), add_special, parse_special);
1518
- LM_GGML_ASSERT(check == -n_tokens);
1519
- } else {
1520
- result.resize(n_tokens);
1521
- }
1522
- return result;
1523
- }
1524
-
1525
- std::string common_token_to_piece(const struct llama_context * ctx, llama_token token, bool special) {
1526
- const llama_model * model = llama_get_model(ctx);
1527
- const llama_vocab * vocab = llama_model_get_vocab(model);
1528
- return common_token_to_piece(vocab, token, special);
1529
- }
1530
-
1531
- std::string common_token_to_piece(const struct llama_vocab * vocab, llama_token token, bool special) {
1532
- std::string piece;
1533
- piece.resize(piece.capacity()); // using string internal cache, 15 bytes + '\n'
1534
- const int n_chars = llama_token_to_piece(vocab, token, &piece[0], piece.size(), 0, special);
1535
- if (n_chars < 0) {
1536
- piece.resize(-n_chars);
1537
- int check = llama_token_to_piece(vocab, token, &piece[0], piece.size(), 0, special);
1538
- LM_GGML_ASSERT(check == -n_chars);
1539
- }
1540
- else {
1541
- piece.resize(n_chars);
1542
- }
1543
-
1544
- return piece;
1545
- }
1546
-
1547
- std::string common_detokenize(const struct llama_context * ctx, const std::vector<llama_token> & tokens, bool special) {
1548
- const llama_model * model = llama_get_model(ctx);
1549
- const llama_vocab * vocab = llama_model_get_vocab(model);
1550
- return common_detokenize(vocab, tokens, special);
1551
- }
1552
-
1553
- std::string common_detokenize(const struct llama_vocab * vocab, const std::vector<llama_token> & tokens, bool special) {
1554
- std::string text;
1555
- text.resize(std::max(text.capacity(), tokens.size()));
1556
- int32_t n_chars = llama_detokenize(vocab, tokens.data(), (int32_t)tokens.size(), &text[0], (int32_t)text.size(), false, special);
1557
- if (n_chars < 0) {
1558
- text.resize(-n_chars);
1559
- n_chars = llama_detokenize(vocab, tokens.data(), (int32_t)tokens.size(), &text[0], (int32_t)text.size(), false, special);
1560
- LM_GGML_ASSERT(n_chars <= (int32_t)text.size()); // whitespace trimming is performed after per-token detokenization
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, &params.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
+ }