@fugood/llama.node 0.2.3 → 0.3.1

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 (319) hide show
  1. package/CMakeLists.txt +6 -3
  2. package/bin/darwin/arm64/llama-node.node +0 -0
  3. package/bin/darwin/x64/llama-node.node +0 -0
  4. package/bin/linux/arm64/llama-node.node +0 -0
  5. package/bin/linux/x64/llama-node.node +0 -0
  6. package/bin/linux-vulkan/arm64/llama-node.node +0 -0
  7. package/bin/linux-vulkan/x64/llama-node.node +0 -0
  8. package/bin/win32/arm64/llama-node.node +0 -0
  9. package/bin/win32/arm64/node.lib +0 -0
  10. package/bin/win32/x64/llama-node.node +0 -0
  11. package/bin/win32/x64/node.lib +0 -0
  12. package/bin/win32-vulkan/arm64/llama-node.node +0 -0
  13. package/bin/win32-vulkan/arm64/node.lib +0 -0
  14. package/bin/win32-vulkan/x64/llama-node.node +0 -0
  15. package/bin/win32-vulkan/x64/node.lib +0 -0
  16. package/lib/binding.ts +8 -1
  17. package/package.json +3 -3
  18. package/patches/llama.patch +12 -12
  19. package/src/DetokenizeWorker.cpp +1 -1
  20. package/src/LlamaContext.cpp +33 -1
  21. package/src/LlamaContext.h +1 -0
  22. package/src/llama.cpp/.github/workflows/bench.yml +310 -0
  23. package/src/llama.cpp/.github/workflows/build.yml +1315 -0
  24. package/src/llama.cpp/.github/workflows/close-issue.yml +23 -0
  25. package/src/llama.cpp/.github/workflows/docker.yml +116 -0
  26. package/src/llama.cpp/.github/workflows/editorconfig.yml +27 -0
  27. package/src/llama.cpp/.github/workflows/gguf-publish.yml +44 -0
  28. package/src/llama.cpp/.github/workflows/labeler.yml +17 -0
  29. package/src/llama.cpp/.github/workflows/nix-ci-aarch64.yml +65 -0
  30. package/src/llama.cpp/.github/workflows/nix-ci.yml +72 -0
  31. package/src/llama.cpp/.github/workflows/nix-flake-update.yml +22 -0
  32. package/src/llama.cpp/.github/workflows/nix-publish-flake.yml +36 -0
  33. package/src/llama.cpp/.github/workflows/python-check-requirements.yml +35 -0
  34. package/src/llama.cpp/.github/workflows/python-lint.yml +23 -0
  35. package/src/llama.cpp/.github/workflows/python-type-check.yml +38 -0
  36. package/src/llama.cpp/.github/workflows/server.yml +183 -0
  37. package/src/llama.cpp/CMakeLists.txt +91 -1245
  38. package/src/llama.cpp/cmake/arm64-windows-llvm.cmake +1 -1
  39. package/src/llama.cpp/cmake/build-info.cmake +58 -0
  40. package/src/llama.cpp/cmake/git-vars.cmake +22 -0
  41. package/src/llama.cpp/common/CMakeLists.txt +4 -3
  42. package/src/llama.cpp/common/build-info.cpp.in +4 -0
  43. package/src/llama.cpp/common/common.cpp +1116 -877
  44. package/src/llama.cpp/common/common.h +191 -77
  45. package/src/llama.cpp/common/grammar-parser.cpp +118 -31
  46. package/src/llama.cpp/common/json-schema-to-grammar.cpp +346 -65
  47. package/src/llama.cpp/common/log.h +1 -1
  48. package/src/llama.cpp/common/ngram-cache.h +10 -3
  49. package/src/llama.cpp/common/sampling.cpp +19 -10
  50. package/src/llama.cpp/docs/build.md +353 -0
  51. package/src/llama.cpp/examples/CMakeLists.txt +22 -22
  52. package/src/llama.cpp/examples/baby-llama/CMakeLists.txt +1 -1
  53. package/src/llama.cpp/examples/baby-llama/baby-llama.cpp +6 -6
  54. package/src/llama.cpp/examples/batched/CMakeLists.txt +1 -1
  55. package/src/llama.cpp/examples/batched/batched.cpp +52 -55
  56. package/src/llama.cpp/examples/batched-bench/CMakeLists.txt +1 -1
  57. package/src/llama.cpp/examples/batched-bench/batched-bench.cpp +20 -72
  58. package/src/llama.cpp/examples/benchmark/CMakeLists.txt +1 -1
  59. package/src/llama.cpp/examples/chat-13B.bat +57 -0
  60. package/src/llama.cpp/examples/convert-llama2c-to-ggml/CMakeLists.txt +1 -1
  61. package/src/llama.cpp/examples/{finetune → cvector-generator}/CMakeLists.txt +2 -2
  62. package/src/llama.cpp/examples/cvector-generator/completions.txt +582 -0
  63. package/src/llama.cpp/examples/cvector-generator/cvector-generator.cpp +503 -0
  64. package/src/llama.cpp/examples/cvector-generator/mean.hpp +48 -0
  65. package/src/llama.cpp/examples/cvector-generator/negative.txt +4 -0
  66. package/src/llama.cpp/examples/cvector-generator/pca.hpp +325 -0
  67. package/src/llama.cpp/examples/cvector-generator/positive.txt +4 -0
  68. package/src/llama.cpp/examples/deprecation-warning/deprecation-warning.cpp +35 -0
  69. package/src/llama.cpp/examples/embedding/CMakeLists.txt +1 -1
  70. package/src/llama.cpp/examples/embedding/embedding.cpp +94 -46
  71. package/src/llama.cpp/examples/eval-callback/CMakeLists.txt +2 -2
  72. package/src/llama.cpp/examples/eval-callback/eval-callback.cpp +4 -6
  73. package/src/llama.cpp/examples/export-lora/CMakeLists.txt +1 -1
  74. package/src/llama.cpp/examples/export-lora/export-lora.cpp +344 -386
  75. package/src/llama.cpp/examples/gbnf-validator/CMakeLists.txt +2 -2
  76. package/src/llama.cpp/examples/gbnf-validator/gbnf-validator.cpp +30 -25
  77. package/src/llama.cpp/examples/gguf/CMakeLists.txt +1 -1
  78. package/src/llama.cpp/examples/gguf/gguf.cpp +5 -0
  79. package/src/llama.cpp/examples/gguf-hash/CMakeLists.txt +15 -0
  80. package/src/llama.cpp/examples/gguf-hash/deps/rotate-bits/rotate-bits.h +46 -0
  81. package/src/llama.cpp/examples/gguf-hash/deps/sha1/sha1.c +295 -0
  82. package/src/llama.cpp/examples/gguf-hash/deps/sha1/sha1.h +52 -0
  83. package/src/llama.cpp/examples/gguf-hash/deps/sha256/sha256.c +221 -0
  84. package/src/llama.cpp/examples/gguf-hash/deps/sha256/sha256.h +24 -0
  85. package/src/llama.cpp/examples/gguf-hash/deps/xxhash/xxhash.c +42 -0
  86. package/src/llama.cpp/examples/gguf-hash/deps/xxhash/xxhash.h +7093 -0
  87. package/src/llama.cpp/examples/gguf-hash/gguf-hash.cpp +693 -0
  88. package/src/llama.cpp/examples/gguf-split/CMakeLists.txt +1 -1
  89. package/src/llama.cpp/examples/gguf-split/gguf-split.cpp +3 -3
  90. package/src/llama.cpp/examples/gritlm/CMakeLists.txt +1 -1
  91. package/src/llama.cpp/examples/gritlm/gritlm.cpp +6 -2
  92. package/src/llama.cpp/examples/imatrix/CMakeLists.txt +1 -1
  93. package/src/llama.cpp/examples/imatrix/imatrix.cpp +137 -176
  94. package/src/llama.cpp/examples/infill/CMakeLists.txt +1 -1
  95. package/src/llama.cpp/examples/infill/infill.cpp +38 -153
  96. package/src/llama.cpp/examples/llama-bench/llama-bench.cpp +175 -94
  97. package/src/llama.cpp/examples/llama.android/app/build.gradle.kts +65 -0
  98. package/src/llama.cpp/examples/llama.android/build.gradle.kts +6 -0
  99. package/src/llama.cpp/examples/llama.android/llama/build.gradle.kts +68 -0
  100. package/src/llama.cpp/examples/llama.android/llama/src/main/cpp/CMakeLists.txt +11 -7
  101. package/src/llama.cpp/examples/llama.android/llama/src/main/cpp/llama-android.cpp +2 -2
  102. package/src/llama.cpp/examples/llama.android/settings.gradle.kts +18 -0
  103. package/src/llama.cpp/examples/llava/CMakeLists.txt +6 -5
  104. package/src/llama.cpp/examples/llava/android/build_64.sh +8 -0
  105. package/src/llama.cpp/examples/llava/clip.cpp +23 -14
  106. package/src/llama.cpp/examples/llava/llava-cli.cpp +8 -6
  107. package/src/llama.cpp/examples/llava/requirements.txt +3 -2
  108. package/src/llama.cpp/examples/lookahead/CMakeLists.txt +1 -1
  109. package/src/llama.cpp/examples/lookahead/lookahead.cpp +2 -1
  110. package/src/llama.cpp/examples/lookup/CMakeLists.txt +4 -4
  111. package/src/llama.cpp/examples/lookup/lookup-create.cpp +2 -0
  112. package/src/llama.cpp/examples/lookup/lookup-merge.cpp +4 -4
  113. package/src/llama.cpp/examples/lookup/lookup-stats.cpp +2 -2
  114. package/src/llama.cpp/examples/lookup/lookup.cpp +1 -1
  115. package/src/llama.cpp/examples/main/CMakeLists.txt +1 -1
  116. package/src/llama.cpp/examples/main/main.cpp +98 -75
  117. package/src/llama.cpp/examples/main-cmake-pkg/CMakeLists.txt +4 -5
  118. package/src/llama.cpp/examples/parallel/CMakeLists.txt +1 -1
  119. package/src/llama.cpp/examples/parallel/parallel.cpp +2 -1
  120. package/src/llama.cpp/examples/passkey/CMakeLists.txt +1 -1
  121. package/src/llama.cpp/examples/passkey/passkey.cpp +23 -43
  122. package/src/llama.cpp/examples/perplexity/CMakeLists.txt +1 -1
  123. package/src/llama.cpp/examples/perplexity/perplexity.cpp +13 -10
  124. package/src/llama.cpp/examples/quantize/CMakeLists.txt +1 -1
  125. package/src/llama.cpp/examples/quantize/quantize.cpp +37 -34
  126. package/src/llama.cpp/examples/quantize-stats/CMakeLists.txt +1 -1
  127. package/src/llama.cpp/examples/quantize-stats/quantize-stats.cpp +1 -1
  128. package/src/llama.cpp/examples/retrieval/CMakeLists.txt +1 -1
  129. package/src/llama.cpp/examples/retrieval/retrieval.cpp +26 -77
  130. package/src/llama.cpp/examples/save-load-state/CMakeLists.txt +1 -1
  131. package/src/llama.cpp/examples/save-load-state/save-load-state.cpp +14 -7
  132. package/src/llama.cpp/examples/server/CMakeLists.txt +26 -2
  133. package/src/llama.cpp/examples/server/server.cpp +274 -671
  134. package/src/llama.cpp/examples/server/tests/requirements.txt +2 -2
  135. package/src/llama.cpp/examples/server/utils.hpp +28 -29
  136. package/src/llama.cpp/examples/simple/CMakeLists.txt +1 -1
  137. package/src/llama.cpp/examples/simple/simple.cpp +21 -29
  138. package/src/llama.cpp/examples/speculative/CMakeLists.txt +1 -1
  139. package/src/llama.cpp/examples/speculative/speculative.cpp +2 -1
  140. package/src/llama.cpp/examples/sycl/CMakeLists.txt +1 -1
  141. package/src/llama.cpp/examples/sycl/build.sh +23 -0
  142. package/src/llama.cpp/examples/sycl/run-llama2.sh +36 -0
  143. package/src/llama.cpp/examples/sycl/win-build-sycl.bat +33 -0
  144. package/src/llama.cpp/examples/sycl/win-run-llama2.bat +9 -0
  145. package/src/llama.cpp/examples/tokenize/CMakeLists.txt +1 -1
  146. package/src/llama.cpp/examples/tokenize/tokenize.cpp +16 -2
  147. package/src/llama.cpp/ggml/CMakeLists.txt +253 -0
  148. package/src/llama.cpp/{cmake → ggml/cmake}/FindSIMD.cmake +6 -6
  149. package/src/llama.cpp/{ggml-backend.h → ggml/include/ggml-backend.h} +22 -17
  150. package/src/llama.cpp/ggml/include/ggml-blas.h +23 -0
  151. package/src/llama.cpp/ggml/include/ggml-cann.h +125 -0
  152. package/src/llama.cpp/{ggml-cuda.h → ggml/include/ggml-cuda.h} +3 -0
  153. package/src/llama.cpp/{ggml-metal.h → ggml/include/ggml-metal.h} +1 -2
  154. package/src/llama.cpp/{ggml-sycl.h → ggml/include/ggml-sycl.h} +3 -10
  155. package/src/llama.cpp/{ggml.h → ggml/include/ggml.h} +80 -85
  156. package/src/llama.cpp/ggml/src/CMakeLists.txt +1329 -0
  157. package/src/llama.cpp/ggml/src/ggml-aarch64.c +2193 -0
  158. package/src/llama.cpp/ggml/src/ggml-aarch64.h +39 -0
  159. package/src/llama.cpp/{ggml-alloc.c → ggml/src/ggml-alloc.c} +100 -49
  160. package/src/llama.cpp/{ggml-backend-impl.h → ggml/src/ggml-backend-impl.h} +20 -8
  161. package/src/llama.cpp/{ggml-backend.c → ggml/src/ggml-backend.c} +307 -167
  162. package/src/llama.cpp/ggml/src/ggml-blas.cpp +367 -0
  163. package/src/llama.cpp/ggml/src/ggml-cann/acl_tensor.cpp +198 -0
  164. package/src/llama.cpp/ggml/src/ggml-cann/acl_tensor.h +230 -0
  165. package/src/llama.cpp/ggml/src/ggml-cann/aclnn_ops.cpp +2944 -0
  166. package/src/llama.cpp/ggml/src/ggml-cann/aclnn_ops.h +592 -0
  167. package/src/llama.cpp/ggml/src/ggml-cann/common.h +282 -0
  168. package/src/llama.cpp/ggml/src/ggml-cann/kernels/CMakeLists.txt +32 -0
  169. package/src/llama.cpp/ggml/src/ggml-cann/kernels/ascendc_kernels.h +17 -0
  170. package/src/llama.cpp/ggml/src/ggml-cann/kernels/dup.cpp +223 -0
  171. package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_f16.cpp +186 -0
  172. package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_f32.cpp +180 -0
  173. package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_q4_0.cpp +193 -0
  174. package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_q8_0.cpp +191 -0
  175. package/src/llama.cpp/ggml/src/ggml-cann/kernels/quantize_f16_q8_0.cpp +208 -0
  176. package/src/llama.cpp/ggml/src/ggml-cann/kernels/quantize_f32_q8_0.cpp +206 -0
  177. package/src/llama.cpp/ggml/src/ggml-cann.cpp +2023 -0
  178. package/src/llama.cpp/{ggml-common.h → ggml/src/ggml-common.h} +41 -7
  179. package/src/llama.cpp/{ggml-impl.h → ggml/src/ggml-impl.h} +113 -9
  180. package/src/llama.cpp/{ggml-kompute.cpp → ggml/src/ggml-kompute.cpp} +33 -18
  181. package/src/llama.cpp/{ggml-quants.c → ggml/src/ggml-quants.c} +1460 -940
  182. package/src/llama.cpp/{ggml-quants.h → ggml/src/ggml-quants.h} +19 -20
  183. package/src/llama.cpp/{ggml-rpc.cpp → ggml/src/ggml-rpc.cpp} +95 -72
  184. package/src/llama.cpp/ggml/src/ggml-sycl/backend.hpp +27 -0
  185. package/src/llama.cpp/ggml/src/ggml-sycl/common.cpp +53 -0
  186. package/src/llama.cpp/ggml/src/ggml-sycl/common.hpp +355 -0
  187. package/src/llama.cpp/ggml/src/ggml-sycl/concat.cpp +195 -0
  188. package/src/llama.cpp/ggml/src/ggml-sycl/concat.hpp +21 -0
  189. package/src/llama.cpp/ggml/src/ggml-sycl/convert.cpp +547 -0
  190. package/src/llama.cpp/ggml/src/ggml-sycl/convert.hpp +27 -0
  191. package/src/llama.cpp/ggml/src/ggml-sycl/dequantize.hpp +698 -0
  192. package/src/llama.cpp/ggml/src/ggml-sycl/dmmv.cpp +1023 -0
  193. package/src/llama.cpp/ggml/src/ggml-sycl/dmmv.hpp +27 -0
  194. package/src/llama.cpp/ggml/src/ggml-sycl/dpct/helper.hpp +3011 -0
  195. package/src/llama.cpp/ggml/src/ggml-sycl/mmq.cpp +3031 -0
  196. package/src/llama.cpp/ggml/src/ggml-sycl/mmq.hpp +33 -0
  197. package/src/llama.cpp/ggml/src/ggml-sycl/mmvq.cpp +1027 -0
  198. package/src/llama.cpp/ggml/src/ggml-sycl/mmvq.hpp +27 -0
  199. package/src/llama.cpp/ggml/src/ggml-sycl/norm.cpp +374 -0
  200. package/src/llama.cpp/ggml/src/ggml-sycl/norm.hpp +35 -0
  201. package/src/llama.cpp/ggml/src/ggml-sycl/presets.hpp +66 -0
  202. package/src/llama.cpp/ggml/src/ggml-sycl/rope.cpp +275 -0
  203. package/src/llama.cpp/ggml/src/ggml-sycl/rope.hpp +22 -0
  204. package/src/llama.cpp/ggml/src/ggml-sycl/softmax.cpp +251 -0
  205. package/src/llama.cpp/ggml/src/ggml-sycl/softmax.hpp +24 -0
  206. package/src/llama.cpp/ggml/src/ggml-sycl/vecdotq.hpp +1140 -0
  207. package/src/llama.cpp/ggml/src/ggml-sycl.cpp +5314 -0
  208. package/src/llama.cpp/{ggml-vulkan.cpp → ggml/src/ggml-vulkan.cpp} +1781 -1868
  209. package/src/llama.cpp/{ggml.c → ggml/src/ggml.c} +1245 -2087
  210. package/src/llama.cpp/{sgemm.cpp → ggml/src/llamafile/sgemm.cpp} +21 -24
  211. package/src/llama.cpp/{sgemm.h → ggml/src/llamafile/sgemm.h} +1 -1
  212. package/src/llama.cpp/ggml/src/vulkan-shaders/CMakeLists.txt +5 -0
  213. package/src/llama.cpp/ggml/src/vulkan-shaders/vulkan-shaders-gen.cpp +552 -0
  214. package/src/llama.cpp/{llama.h → include/llama.h} +175 -100
  215. package/src/llama.cpp/models/.editorconfig +1 -0
  216. package/src/llama.cpp/models/ggml-vocab-aquila.gguf +0 -0
  217. package/src/llama.cpp/models/ggml-vocab-baichuan.gguf +0 -0
  218. package/src/llama.cpp/models/ggml-vocab-bert-bge.gguf +0 -0
  219. package/src/llama.cpp/models/ggml-vocab-bert-bge.gguf.inp +112 -0
  220. package/src/llama.cpp/models/ggml-vocab-bert-bge.gguf.out +46 -0
  221. package/src/llama.cpp/models/ggml-vocab-command-r.gguf +0 -0
  222. package/src/llama.cpp/models/ggml-vocab-command-r.gguf.inp +112 -0
  223. package/src/llama.cpp/models/ggml-vocab-command-r.gguf.out +46 -0
  224. package/src/llama.cpp/models/ggml-vocab-deepseek-coder.gguf +0 -0
  225. package/src/llama.cpp/models/ggml-vocab-deepseek-coder.gguf.inp +112 -0
  226. package/src/llama.cpp/models/ggml-vocab-deepseek-coder.gguf.out +46 -0
  227. package/src/llama.cpp/models/ggml-vocab-deepseek-llm.gguf +0 -0
  228. package/src/llama.cpp/models/ggml-vocab-deepseek-llm.gguf.inp +112 -0
  229. package/src/llama.cpp/models/ggml-vocab-deepseek-llm.gguf.out +46 -0
  230. package/src/llama.cpp/models/ggml-vocab-falcon.gguf +0 -0
  231. package/src/llama.cpp/models/ggml-vocab-falcon.gguf.inp +112 -0
  232. package/src/llama.cpp/models/ggml-vocab-falcon.gguf.out +46 -0
  233. package/src/llama.cpp/models/ggml-vocab-gpt-2.gguf +0 -0
  234. package/src/llama.cpp/models/ggml-vocab-gpt-2.gguf.inp +112 -0
  235. package/src/llama.cpp/models/ggml-vocab-gpt-2.gguf.out +46 -0
  236. package/src/llama.cpp/models/ggml-vocab-gpt-neox.gguf +0 -0
  237. package/src/llama.cpp/models/ggml-vocab-llama-bpe.gguf +0 -0
  238. package/src/llama.cpp/models/ggml-vocab-llama-bpe.gguf.inp +112 -0
  239. package/src/llama.cpp/models/ggml-vocab-llama-bpe.gguf.out +46 -0
  240. package/src/llama.cpp/models/ggml-vocab-llama-spm.gguf +0 -0
  241. package/src/llama.cpp/models/ggml-vocab-llama-spm.gguf.inp +112 -0
  242. package/src/llama.cpp/models/ggml-vocab-llama-spm.gguf.out +46 -0
  243. package/src/llama.cpp/models/ggml-vocab-mpt.gguf +0 -0
  244. package/src/llama.cpp/models/ggml-vocab-mpt.gguf.inp +112 -0
  245. package/src/llama.cpp/models/ggml-vocab-mpt.gguf.out +46 -0
  246. package/src/llama.cpp/models/ggml-vocab-phi-3.gguf +0 -0
  247. package/src/llama.cpp/models/ggml-vocab-phi-3.gguf.inp +112 -0
  248. package/src/llama.cpp/models/ggml-vocab-phi-3.gguf.out +46 -0
  249. package/src/llama.cpp/models/ggml-vocab-qwen2.gguf +0 -0
  250. package/src/llama.cpp/models/ggml-vocab-qwen2.gguf.inp +112 -0
  251. package/src/llama.cpp/models/ggml-vocab-qwen2.gguf.out +46 -0
  252. package/src/llama.cpp/models/ggml-vocab-refact.gguf +0 -0
  253. package/src/llama.cpp/models/ggml-vocab-refact.gguf.inp +112 -0
  254. package/src/llama.cpp/models/ggml-vocab-refact.gguf.out +46 -0
  255. package/src/llama.cpp/models/ggml-vocab-starcoder.gguf +0 -0
  256. package/src/llama.cpp/models/ggml-vocab-starcoder.gguf.inp +112 -0
  257. package/src/llama.cpp/models/ggml-vocab-starcoder.gguf.out +46 -0
  258. package/src/llama.cpp/pocs/vdot/CMakeLists.txt +2 -2
  259. package/src/llama.cpp/requirements/requirements-all.txt +12 -0
  260. package/src/llama.cpp/requirements/requirements-compare-llama-bench.txt +2 -0
  261. package/src/llama.cpp/requirements/requirements-convert_hf_to_gguf.txt +3 -0
  262. package/src/llama.cpp/requirements/requirements-convert_hf_to_gguf_update.txt +3 -0
  263. package/src/llama.cpp/requirements/{requirements-convert.txt → requirements-convert_legacy_llama.txt} +1 -1
  264. package/src/llama.cpp/requirements/requirements-convert_llama_ggml_to_gguf.txt +1 -0
  265. package/src/llama.cpp/requirements/requirements-convert_lora_to_gguf.txt +2 -0
  266. package/src/llama.cpp/requirements/requirements-pydantic.txt +3 -0
  267. package/src/llama.cpp/requirements/requirements-test-tokenizer-random.txt +1 -0
  268. package/src/llama.cpp/requirements.txt +5 -4
  269. package/src/llama.cpp/scripts/build-info.sh +30 -0
  270. package/src/llama.cpp/scripts/install-oneapi.bat +19 -0
  271. package/src/llama.cpp/src/CMakeLists.txt +33 -0
  272. package/src/llama.cpp/src/llama-grammar.cpp +539 -0
  273. package/src/llama.cpp/src/llama-grammar.h +39 -0
  274. package/src/llama.cpp/src/llama-impl.h +26 -0
  275. package/src/llama.cpp/src/llama-sampling.cpp +635 -0
  276. package/src/llama.cpp/src/llama-sampling.h +56 -0
  277. package/src/llama.cpp/src/llama-vocab.cpp +1721 -0
  278. package/src/llama.cpp/src/llama-vocab.h +130 -0
  279. package/src/llama.cpp/{llama.cpp → src/llama.cpp} +5979 -5260
  280. package/src/llama.cpp/{unicode-data.cpp → src/unicode-data.cpp} +851 -802
  281. package/src/llama.cpp/{unicode.cpp → src/unicode.cpp} +52 -30
  282. package/src/llama.cpp/{unicode.h → src/unicode.h} +5 -1
  283. package/src/llama.cpp/tests/CMakeLists.txt +19 -20
  284. package/src/llama.cpp/tests/test-backend-ops.cpp +245 -67
  285. package/src/llama.cpp/tests/test-chat-template.cpp +57 -3
  286. package/src/llama.cpp/tests/test-double-float.cpp +2 -2
  287. package/src/llama.cpp/tests/test-grad0.cpp +2 -2
  288. package/src/llama.cpp/tests/test-grammar-integration.cpp +978 -31
  289. package/src/llama.cpp/tests/test-grammar-parser.cpp +423 -158
  290. package/src/llama.cpp/tests/test-json-schema-to-grammar.cpp +508 -135
  291. package/src/llama.cpp/tests/test-llama-grammar.cpp +15 -9
  292. package/src/llama.cpp/tests/test-quantize-fns.cpp +1 -1
  293. package/src/llama.cpp/tests/test-quantize-perf.cpp +1 -1
  294. package/src/llama.cpp/tests/test-rope.cpp +3 -4
  295. package/src/llama.cpp/tests/test-sampling.cpp +5 -5
  296. package/src/llama.cpp/tests/test-tokenizer-0.cpp +6 -6
  297. package/src/llama.cpp/tests/test-tokenizer-1-bpe.cpp +20 -15
  298. package/src/llama.cpp/tests/test-tokenizer-1-spm.cpp +22 -11
  299. package/bin/darwin/arm64/default.metallib +0 -0
  300. package/bin/darwin/x64/default.metallib +0 -0
  301. package/src/llama.cpp/examples/beam-search/CMakeLists.txt +0 -5
  302. package/src/llama.cpp/examples/beam-search/beam-search.cpp +0 -188
  303. package/src/llama.cpp/examples/finetune/finetune.cpp +0 -1862
  304. package/src/llama.cpp/examples/llama.android/llama/CMakeLists.txt +0 -55
  305. package/src/llama.cpp/examples/train-text-from-scratch/CMakeLists.txt +0 -5
  306. package/src/llama.cpp/examples/train-text-from-scratch/train-text-from-scratch.cpp +0 -1253
  307. package/src/llama.cpp/ggml-opencl.cpp +0 -2305
  308. package/src/llama.cpp/ggml-opencl.h +0 -36
  309. package/src/llama.cpp/ggml-sycl.cpp +0 -17340
  310. package/src/llama.cpp/ggml-vulkan-shaders.hpp +0 -81211
  311. package/src/llama.cpp/requirements/requirements-convert-hf-to-gguf-update.txt +0 -2
  312. package/src/llama.cpp/requirements/requirements-convert-hf-to-gguf.txt +0 -2
  313. package/src/llama.cpp/requirements/requirements-convert-llama-ggml-to-gguf.txt +0 -1
  314. package/src/llama.cpp/scripts/gen-build-info-cpp.cmake +0 -24
  315. /package/src/llama.cpp/{ggml-alloc.h → ggml/include/ggml-alloc.h} +0 -0
  316. /package/src/llama.cpp/{ggml-kompute.h → ggml/include/ggml-kompute.h} +0 -0
  317. /package/src/llama.cpp/{ggml-rpc.h → ggml/include/ggml-rpc.h} +0 -0
  318. /package/src/llama.cpp/{ggml-vulkan.h → ggml/include/ggml-vulkan.h} +0 -0
  319. /package/src/llama.cpp/{unicode-data.h → src/unicode-data.h} +0 -0
@@ -0,0 +1,693 @@
1
+ #include "ggml.h"
2
+
3
+ #include <cstdlib> /* abort() */
4
+ #include <cstddef>
5
+ #include <cstdio>
6
+ #include <string>
7
+ #include <stdexcept>
8
+ #include <algorithm>
9
+ #include <cstring>
10
+
11
+ #include <sstream>
12
+ #include <fstream>
13
+
14
+ #ifdef __cplusplus
15
+ extern "C" {
16
+ #endif
17
+
18
+ #include "xxhash/xxhash.h"
19
+ #include "sha1/sha1.h"
20
+ #include "sha256/sha256.h"
21
+
22
+ #ifdef __cplusplus
23
+ }
24
+ #endif
25
+
26
+
27
+ // uuid.uuid5(uuid.NAMESPACE_URL, 'en.wikipedia.org/wiki/Llama.cpp')
28
+ #define UUID_NAMESPACE_LLAMA_CPP "ef001206-dadc-5f6d-a15f-3359e577d4e5"
29
+ #define UUID_NAMESPACE_LLAMA_CPP_HEX 0xef, 0x00, 0x12, 0x06, 0xda, 0xdc, 0x5f, 0x6d, 0xa1, 0x5f, 0x33, 0x59, 0xe5, 0x77, 0xd4, 0xe5
30
+
31
+
32
+ #define HASH_TYPE_SHA256_STR "sha256"
33
+ #define HASH_TYPE_SHA1_STR "sha1"
34
+ #define HASH_TYPE_XXH64_STR "xxh64"
35
+ #define HASH_TYPE_UUID_STR "uuid"
36
+
37
+
38
+ typedef enum {
39
+ HASH_EXIT_SUCCESS = 0, // All hash has been generated or validated
40
+ HASH_EXIT_FAILURE = 1, // Generic Failure
41
+ HASH_EXIT_MISMATCH = 2, // Hash mismatched during validation
42
+ HASH_EXIT_MANIFEST_MISSING_ENTRY = 3, // Hash attempted validation but missing entry in manifest
43
+ HASH_EXIT_MANIFEST_UNKNOWN_HASH = 4, // Manifest is present, but we do not know any hash format within it
44
+ HASH_EXIT_MANIFEST_FILE_ERROR = 5 // Manifest is either missing or not a known format
45
+ } hash_exit_code_t;
46
+
47
+
48
+ typedef enum {
49
+ HASH_MANIFEST_NOT_FOUND,
50
+ HASH_MANIFEST_MISMATCH,
51
+ HASH_MANIFEST_OK,
52
+ } hash_manifest_result_t;
53
+
54
+
55
+ struct hash_params {
56
+ std::string input;
57
+ bool xxh64 = false;
58
+ bool sha1 = false;
59
+ bool sha256 = false;
60
+ bool uuid = false;
61
+
62
+ bool no_layer = false;
63
+
64
+ bool manifest_is_usable = false;
65
+ std::string manifest_file;
66
+ };
67
+
68
+ struct manifest_check_params {
69
+ bool xxh64 = false;
70
+ bool sha1 = false;
71
+ bool sha256 = false;
72
+ bool uuid = false;
73
+ };
74
+
75
+ static char const * hash_manifest_result_to_str(hash_manifest_result_t value) {
76
+ switch (value) {
77
+ case HASH_MANIFEST_NOT_FOUND: return "Not Found";
78
+ case HASH_MANIFEST_MISMATCH: return "Mismatch";
79
+ case HASH_MANIFEST_OK: return "Ok";
80
+ }
81
+ return "?";
82
+ }
83
+
84
+ static char const * hash_exit_code_to_str(hash_exit_code_t value) {
85
+ switch (value) {
86
+ case HASH_EXIT_SUCCESS: return "Success";
87
+ case HASH_EXIT_FAILURE: return "Failure";
88
+ case HASH_EXIT_MISMATCH: return "Mismatch";
89
+ case HASH_EXIT_MANIFEST_MISSING_ENTRY: return "Manifest Missing Entry";
90
+ case HASH_EXIT_MANIFEST_UNKNOWN_HASH: return "Manifest Unknown Hash";
91
+ case HASH_EXIT_MANIFEST_FILE_ERROR: return "Manifest File Error";
92
+ }
93
+ return "?";
94
+ }
95
+
96
+ static void hash_print_usage(const char * executable) {
97
+ const hash_params default_params;
98
+ printf("\n");
99
+ printf("usage: %s [options] GGUF_IN\n", executable);
100
+ printf("\n");
101
+ printf("Hash a GGUF file");
102
+ printf("\n");
103
+ printf("options:\n");
104
+ printf(" -h, --help show this help message and exit\n");
105
+ printf(" --xxh64 use xxh64 hash\n");
106
+ printf(" --sha1 use sha1 hash\n");
107
+ printf(" --sha256 use sha256 hash\n");
108
+ printf(" --all use all hash\n");
109
+ printf(" --no-layer exclude per layer hash\n");
110
+ printf(" --uuid generate UUIDv5 ID\n");
111
+ printf(" -c, --check <manifest> verify against a manifest\n");
112
+ printf("\n");
113
+ }
114
+
115
+ static void hash_params_parse_ex(int argc, const char ** argv, hash_params & params) {
116
+ std::string arg;
117
+ bool invalid_param = false;
118
+ const std::string arg_prefix = "--";
119
+
120
+ int arg_idx = 1;
121
+ for (; arg_idx < argc && strncmp(argv[arg_idx], "--", 2) == 0; arg_idx++) {
122
+ arg = argv[arg_idx];
123
+ if (arg.compare(0, arg_prefix.size(), arg_prefix) == 0) {
124
+ std::replace(arg.begin(), arg.end(), '_', '-');
125
+ }
126
+
127
+ bool arg_found = false;
128
+ if (arg == "-h" || arg == "--help") {
129
+ hash_print_usage(argv[0]);
130
+ exit(0);
131
+ }
132
+
133
+ if (arg == "--xxh64") {
134
+ arg_found = true;
135
+ params.xxh64 = true;
136
+ }
137
+
138
+ if (arg == "--sha1") {
139
+ arg_found = true;
140
+ params.sha1 = true;
141
+ }
142
+
143
+ if (arg == "--uuid") {
144
+ arg_found = true;
145
+ params.uuid = true;
146
+ }
147
+
148
+ if (arg == "--sha256") {
149
+ arg_found = true;
150
+ params.sha256 = true;
151
+ }
152
+
153
+ if (arg == "--all") {
154
+ arg_found = true;
155
+ params.sha256 = true;
156
+ params.sha1 = true;
157
+ params.xxh64 = true;
158
+ }
159
+
160
+ if (arg == "--no-layer") {
161
+ arg_found = true;
162
+ params.no_layer = true;
163
+ }
164
+
165
+ if (arg == "-c" || arg == "--check") {
166
+ if (++arg_idx >= argc) {
167
+ invalid_param = true;
168
+ break;
169
+ }
170
+ arg_found = true;
171
+ params.manifest_file = argv[arg_idx];
172
+ }
173
+
174
+ if (!arg_found) {
175
+ throw std::invalid_argument("error: unknown argument: " + arg);
176
+ }
177
+ }
178
+
179
+ if (invalid_param) {
180
+ throw std::invalid_argument("error: invalid parameter for argument:" + arg);
181
+ }
182
+
183
+ if (argc - arg_idx < 1) {
184
+ throw std::invalid_argument("error: bad arguments");
185
+ }
186
+
187
+ params.input = argv[arg_idx++];
188
+ }
189
+
190
+ static bool hash_params_parse(int argc, const char ** argv, hash_params & params) {
191
+ bool result = true;
192
+ try {
193
+ hash_params_parse_ex(argc, argv, params);
194
+ }
195
+ catch (const std::invalid_argument & ex) {
196
+ fprintf(stderr, "%s\n", ex.what());
197
+ hash_print_usage(argv[0]);
198
+ exit(EXIT_FAILURE);
199
+ }
200
+ return result;
201
+ }
202
+
203
+ static bool manifest_type(const std::string & manifest_file, manifest_check_params & manifest_check) {
204
+ if (manifest_file.empty()) {
205
+ return false;
206
+ }
207
+
208
+ std::ifstream file(manifest_file);
209
+ if (!file.is_open()) {
210
+ return false;
211
+ }
212
+
213
+ std::string manifest_entry_line;
214
+ while (getline(file, manifest_entry_line)) {
215
+ // hash_type_str hash_str tensor_name
216
+ // e.g. 'xxh64 f66e9cd66a4396a0 test.gguf:tensor_0'
217
+ std::istringstream line_stream(manifest_entry_line);
218
+ std::string file_hash_type;
219
+ if (line_stream >> file_hash_type) {
220
+ if (file_hash_type == HASH_TYPE_SHA256_STR) {
221
+ manifest_check.sha256 = true;
222
+ } else if (file_hash_type == HASH_TYPE_SHA1_STR) {
223
+ manifest_check.sha1 = true;
224
+ } else if (file_hash_type == HASH_TYPE_XXH64_STR) {
225
+ manifest_check.xxh64 = true;
226
+ } else if (file_hash_type == HASH_TYPE_UUID_STR) {
227
+ manifest_check.uuid = true;
228
+ }
229
+ }
230
+ }
231
+
232
+ return true;
233
+ }
234
+
235
+ static hash_manifest_result_t manifest_verify(const std::string& manifest_file, const std::string& hash_type_str, const std::string& hash_str, const std::string& tensor_name) {
236
+ if (manifest_file.empty()) {
237
+ return HASH_MANIFEST_NOT_FOUND;
238
+ }
239
+
240
+ std::ifstream file(manifest_file);
241
+ if (!file.is_open()) {
242
+ return HASH_MANIFEST_NOT_FOUND;
243
+ }
244
+
245
+ std::string manifest_entry_line;
246
+ while (getline(file, manifest_entry_line)) {
247
+ std::istringstream line_stream(manifest_entry_line);
248
+ std::string file_hash_type;
249
+ std::string file_hash;
250
+ std::string file_tensor_name;
251
+ if (line_stream >> file_hash_type >> file_hash >> file_tensor_name) {
252
+ // Line parsed. Check hash validity
253
+
254
+ if (file_hash_type != hash_type_str) {
255
+ continue;
256
+ }
257
+
258
+ if (file_tensor_name != tensor_name) {
259
+ continue;
260
+ }
261
+
262
+ return (file_hash == hash_str) ? HASH_MANIFEST_OK : HASH_MANIFEST_MISMATCH;
263
+ }
264
+ }
265
+
266
+ return HASH_MANIFEST_NOT_FOUND;
267
+ }
268
+
269
+ static void generate_uuidv5(const unsigned char sha1_digest[20], unsigned char uuid[16]) {
270
+ // Ref: https://www.rfc-editor.org/rfc/rfc9562.html#section-5.5
271
+ // Assumes that digest was processed correctly with the expected namespace
272
+ for (int i = 0; i < 16; i++) {
273
+ uuid[i] = sha1_digest[i];
274
+ }
275
+
276
+ // Set bits corresponding to UUID ver 5
277
+ uuid[ 6] &= ~(0xF << 4);
278
+ uuid[ 6] |= (5 << 4);
279
+
280
+ // Set bits corresponding to UUID variant 0b10XX
281
+ uuid[ 8] &= ~(0xc << 4);
282
+ uuid[ 8] |= (0x8 << 4);
283
+ }
284
+
285
+ static hash_exit_code_t gguf_hash(const hash_params & hash_params) {
286
+ const std::string & fname = hash_params.input;
287
+ struct ggml_context * ctx_data = NULL;
288
+
289
+ struct gguf_init_params params = {
290
+ /*.no_alloc = */ false,
291
+ /*.ctx = */ &ctx_data,
292
+ };
293
+
294
+ // xxh64 init
295
+ XXH64_state_t* xxh64_model_hash_state = NULL;
296
+ if (hash_params.xxh64) {
297
+ xxh64_model_hash_state = XXH64_createState();
298
+ if (xxh64_model_hash_state==NULL) {
299
+ abort();
300
+ }
301
+
302
+ XXH64_hash_t const seed = 0;
303
+ if (XXH64_reset(xxh64_model_hash_state, seed) == XXH_ERROR) {
304
+ abort();
305
+ }
306
+ }
307
+
308
+ // sha1 init
309
+ SHA1_CTX sha1_model_hash_ctx;
310
+ if (hash_params.sha1) {
311
+ SHA1Init(&sha1_model_hash_ctx);
312
+ }
313
+
314
+ // sha256 init
315
+ sha256_t sha256_model_hash_ctx;
316
+ if (hash_params.sha256) {
317
+ sha256_init(&sha256_model_hash_ctx);
318
+ }
319
+
320
+ // sha1 for uuid init
321
+ SHA1_CTX sha1_for_uuid_ctx;
322
+ if (hash_params.uuid) {
323
+ unsigned char const uuidv5_namespace[] = {UUID_NAMESPACE_LLAMA_CPP_HEX};
324
+ SHA1Init(&sha1_for_uuid_ctx);
325
+ SHA1Update( &sha1_for_uuid_ctx, (unsigned char const *)uuidv5_namespace, sizeof(uuidv5_namespace));
326
+ }
327
+
328
+ struct gguf_context * ctx = gguf_init_from_file(fname.c_str(), params);
329
+ const int n_tensors = gguf_get_n_tensors(ctx);
330
+ bool tensor_layer_in_manifest = false;
331
+ bool model_in_manifest = false;
332
+ bool tensor_layer_has_mismatch = false;
333
+ bool model_has_mismatch = false;
334
+ for (int i = 0; i < n_tensors; ++i) {
335
+ const char * name = gguf_get_tensor_name(ctx, i);
336
+ struct ggml_tensor * cur = ggml_get_tensor(ctx_data, name);
337
+ auto n_bytes = ggml_nbytes(cur);
338
+ auto *raw_data = cur->data;
339
+ const std::string tensor_layer_name = fname + ":" + name;
340
+
341
+ if (hash_params.xxh64) {
342
+
343
+ if (!hash_params.no_layer) {
344
+ // Per Layer Hash
345
+ XXH64_hash_t hash = XXH64(raw_data, n_bytes, 0);
346
+
347
+ char hex_result[17];
348
+ for (int offset = 0; offset < 8; offset++) {
349
+ unsigned int shift_bits_by = (8 * (8 - offset - 1));
350
+ snprintf( ( hex_result + (2*offset)), sizeof(hex_result) - (2*offset), "%02x", (unsigned char) (hash >> shift_bits_by)&0xff);
351
+ }
352
+
353
+ if (hash_params.manifest_is_usable) {
354
+ hash_manifest_result_t verify_result = manifest_verify(hash_params.manifest_file, HASH_TYPE_XXH64_STR, hex_result, tensor_layer_name);
355
+
356
+ switch (verify_result) {
357
+ case HASH_MANIFEST_NOT_FOUND:
358
+ break;
359
+ case HASH_MANIFEST_MISMATCH:
360
+ tensor_layer_in_manifest = true;
361
+ tensor_layer_has_mismatch = true;
362
+ break;
363
+ case HASH_MANIFEST_OK:
364
+ tensor_layer_in_manifest = true;
365
+ break;
366
+ }
367
+
368
+ printf("%-8s %-s %s - %s\n", HASH_TYPE_XXH64_STR, hex_result, tensor_layer_name.c_str(), hash_manifest_result_to_str(verify_result));
369
+ } else {
370
+ printf("%-8s %-s %s\n", HASH_TYPE_XXH64_STR, hex_result, tensor_layer_name.c_str());
371
+ }
372
+ }
373
+
374
+ // Overall Model Hash
375
+ if (XXH64_update(xxh64_model_hash_state, raw_data, n_bytes) == XXH_ERROR) abort();
376
+ }
377
+
378
+ if (hash_params.sha1) {
379
+
380
+ if (!hash_params.no_layer) {
381
+ // Per Layer Hash
382
+ char result[21]; // sha1 outputs 20 bytes
383
+ SHA1( result, (const char *)raw_data, n_bytes);
384
+
385
+ char hex_result[41] = {0};
386
+ for (int offset = 0; offset < 20; offset++) {
387
+ snprintf( ( hex_result + (2*offset)), sizeof(hex_result) - (2*offset), "%02x", result[offset]&0xff);
388
+ }
389
+
390
+ if (hash_params.manifest_is_usable) {
391
+ hash_manifest_result_t verify_result = manifest_verify(hash_params.manifest_file, HASH_TYPE_SHA1_STR, hex_result, tensor_layer_name);
392
+
393
+ switch (verify_result) {
394
+ case HASH_MANIFEST_NOT_FOUND:
395
+ break;
396
+ case HASH_MANIFEST_MISMATCH:
397
+ tensor_layer_in_manifest = true;
398
+ tensor_layer_has_mismatch = true;
399
+ break;
400
+ case HASH_MANIFEST_OK:
401
+ tensor_layer_in_manifest = true;
402
+ break;
403
+ }
404
+
405
+ printf("%-8s %-s %s - %s\n", HASH_TYPE_SHA1_STR, hex_result, tensor_layer_name.c_str(), hash_manifest_result_to_str(verify_result));
406
+ } else {
407
+ printf("%-8s %-s %s\n", HASH_TYPE_SHA1_STR, hex_result, tensor_layer_name.c_str());
408
+ }
409
+ }
410
+
411
+ // Overall Model Hash
412
+ SHA1Update( &sha1_model_hash_ctx, (unsigned char const *)raw_data, n_bytes);
413
+ }
414
+
415
+ if (hash_params.sha256) {
416
+
417
+ if (!hash_params.no_layer) {
418
+ // Per Layer Hash
419
+ unsigned char result[SHA256_DIGEST_SIZE]; // sha256 outputs 32 bytes
420
+ sha256_hash((unsigned char*) result, (const unsigned char *)raw_data, n_bytes);
421
+
422
+ char hex_result[SHA256_DIGEST_SIZE * 2 + 1] = {0};
423
+ for (int offset = 0; offset < SHA256_DIGEST_SIZE; offset++) {
424
+ snprintf( ( hex_result + (2*offset)), sizeof(hex_result) - (2*offset), "%02x", result[offset]&0xff);
425
+ }
426
+
427
+ if (hash_params.manifest_is_usable) {
428
+ hash_manifest_result_t verify_result = manifest_verify(hash_params.manifest_file, HASH_TYPE_SHA256_STR, hex_result, tensor_layer_name);
429
+
430
+ switch (verify_result) {
431
+ case HASH_MANIFEST_NOT_FOUND:
432
+ break;
433
+ case HASH_MANIFEST_MISMATCH:
434
+ tensor_layer_in_manifest = true;
435
+ tensor_layer_has_mismatch = true;
436
+ break;
437
+ case HASH_MANIFEST_OK:
438
+ tensor_layer_in_manifest = true;
439
+ break;
440
+ }
441
+
442
+ printf("%-8s %-s %s - %s\n", HASH_TYPE_SHA256_STR, hex_result, tensor_layer_name.c_str(), hash_manifest_result_to_str(verify_result));
443
+ } else {
444
+ printf("%-8s %-s %s\n", HASH_TYPE_SHA256_STR, hex_result, tensor_layer_name.c_str());
445
+ }
446
+ }
447
+
448
+ // Overall Model Hash
449
+ sha256_update( &sha256_model_hash_ctx, (unsigned char const *)raw_data, n_bytes);
450
+ }
451
+
452
+ if (hash_params.uuid) {
453
+ SHA1Update( &sha1_for_uuid_ctx, (unsigned char const *)raw_data, n_bytes);
454
+ }
455
+ }
456
+
457
+ if (hash_params.xxh64) {
458
+ XXH64_hash_t const hash = XXH64_digest(xxh64_model_hash_state);
459
+
460
+ char hex_result[17];
461
+ for (int offset = 0; offset < 8; offset++) {
462
+ unsigned int shift_bits_by = (8 * (8 - offset - 1));
463
+ snprintf( ( hex_result + (2*offset)), sizeof(hex_result) - (2*offset), "%02x", (unsigned char) (hash >> shift_bits_by)&0xff);
464
+ }
465
+
466
+ if (hash_params.manifest_is_usable) {
467
+ hash_manifest_result_t verify_result = manifest_verify(hash_params.manifest_file, HASH_TYPE_XXH64_STR, hex_result, fname);
468
+
469
+ switch (verify_result) {
470
+ case HASH_MANIFEST_NOT_FOUND:
471
+ break;
472
+ case HASH_MANIFEST_MISMATCH:
473
+ model_in_manifest = true;
474
+ model_has_mismatch = true;
475
+ break;
476
+ case HASH_MANIFEST_OK:
477
+ model_in_manifest = true;
478
+ break;
479
+ }
480
+
481
+ printf("%-8s %-s %s - %s\n", HASH_TYPE_XXH64_STR, hex_result, fname.c_str(), hash_manifest_result_to_str(verify_result));
482
+ } else {
483
+ printf("%-8s %-s %s\n", HASH_TYPE_XXH64_STR, hex_result, fname.c_str());
484
+ }
485
+ }
486
+
487
+ if (hash_params.sha1) {
488
+ unsigned char result[21];
489
+ SHA1Final(result, &sha1_model_hash_ctx);
490
+
491
+ char hex_result[41];
492
+ for (int offset = 0; offset < 20; offset++) {
493
+ snprintf( ( hex_result + (2*offset)), sizeof(hex_result) - (2*offset), "%02x", result[offset]&0xff);
494
+ }
495
+
496
+ if (hash_params.manifest_is_usable) {
497
+ hash_manifest_result_t verify_result = manifest_verify(hash_params.manifest_file, HASH_TYPE_SHA1_STR, hex_result, fname);
498
+
499
+ switch (verify_result) {
500
+ case HASH_MANIFEST_NOT_FOUND:
501
+ break;
502
+ case HASH_MANIFEST_MISMATCH:
503
+ model_in_manifest = true;
504
+ model_has_mismatch = true;
505
+ break;
506
+ case HASH_MANIFEST_OK:
507
+ model_in_manifest = true;
508
+ break;
509
+ }
510
+
511
+ printf("%-8s %-s %s - %s\n", HASH_TYPE_SHA1_STR, hex_result, fname.c_str(), hash_manifest_result_to_str(verify_result));
512
+ } else {
513
+ printf("%-8s %-s %s\n", HASH_TYPE_SHA1_STR, hex_result, fname.c_str());
514
+ }
515
+ }
516
+
517
+ if (hash_params.sha256) {
518
+ unsigned char result[SHA256_DIGEST_SIZE]; // sha256 outputs 32 bytes
519
+ sha256_final( &sha256_model_hash_ctx, result);
520
+
521
+ char hex_result[SHA256_DIGEST_SIZE * 2 + 1] = {0};
522
+ for (int offset = 0; offset < SHA256_DIGEST_SIZE; offset++) {
523
+ snprintf( ( hex_result + (2*offset)), sizeof(hex_result) - (2*offset), "%02x", result[offset]&0xff);
524
+ }
525
+
526
+ if (hash_params.manifest_is_usable) {
527
+ hash_manifest_result_t verify_result = manifest_verify(hash_params.manifest_file, HASH_TYPE_SHA256_STR, hex_result, fname);
528
+
529
+ switch (verify_result) {
530
+ case HASH_MANIFEST_NOT_FOUND:
531
+ break;
532
+ case HASH_MANIFEST_MISMATCH:
533
+ model_in_manifest = true;
534
+ model_has_mismatch = true;
535
+ break;
536
+ case HASH_MANIFEST_OK:
537
+ model_in_manifest = true;
538
+ break;
539
+ }
540
+
541
+ printf("%-8s %-s %s - %s\n", HASH_TYPE_SHA256_STR, hex_result, fname.c_str(), hash_manifest_result_to_str(verify_result));
542
+ } else {
543
+ printf("%-8s %-s %s\n", HASH_TYPE_SHA256_STR, hex_result, fname.c_str());
544
+ }
545
+ }
546
+
547
+ if (hash_params.uuid) {
548
+ unsigned char result[21];
549
+ SHA1Final(result, &sha1_for_uuid_ctx);
550
+
551
+ unsigned char uuid[16];
552
+ generate_uuidv5(result, uuid);
553
+
554
+ char string_buffer[37] = {0};
555
+ snprintf(string_buffer, sizeof(string_buffer), "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
556
+ uuid[0], uuid[1], uuid[2], uuid[3],
557
+ uuid[4], uuid[5], uuid[6], uuid[7],
558
+ uuid[8], uuid[9], uuid[10], uuid[11],
559
+ uuid[12], uuid[13], uuid[14], uuid[15]);
560
+
561
+ if (hash_params.manifest_is_usable) {
562
+ hash_manifest_result_t verify_result = manifest_verify(hash_params.manifest_file, HASH_TYPE_SHA256_STR, string_buffer, fname);
563
+
564
+ switch (verify_result) {
565
+ case HASH_MANIFEST_NOT_FOUND:
566
+ break;
567
+ case HASH_MANIFEST_MISMATCH:
568
+ model_in_manifest = true;
569
+ model_has_mismatch = true;
570
+ break;
571
+ case HASH_MANIFEST_OK:
572
+ model_in_manifest = true;
573
+ break;
574
+ }
575
+
576
+ printf("%-8s %-s %s - %s\n", HASH_TYPE_UUID_STR, string_buffer, fname.c_str(), hash_manifest_result_to_str(verify_result));
577
+ } else {
578
+ printf("%-8s %-s %s\n", HASH_TYPE_UUID_STR, string_buffer, fname.c_str());
579
+ }
580
+ }
581
+
582
+
583
+ ggml_free(ctx_data);
584
+ gguf_free(ctx);
585
+
586
+
587
+ if (hash_params.manifest_is_usable) {
588
+ // In hash verification mode
589
+
590
+ if (!model_in_manifest) {
591
+ // model missing in manifest?
592
+
593
+ // Check tensor layer...
594
+ if (!tensor_layer_in_manifest) {
595
+ // Still missing? Maybe we are reading the wrong manifest.
596
+ return HASH_EXIT_MANIFEST_MISSING_ENTRY;
597
+ }
598
+
599
+ if (tensor_layer_has_mismatch) {
600
+ // Per tensor check found error
601
+ return HASH_EXIT_FAILURE;
602
+ }
603
+
604
+ // All per tensor layer checks passed? Sounds good enough.
605
+ return HASH_EXIT_SUCCESS;
606
+ }
607
+
608
+ // Overall model check passed, but let's check per layer just in case
609
+ // If missing, we don't care too much as the overall model checked
610
+ if (tensor_layer_in_manifest && tensor_layer_has_mismatch) {
611
+ return HASH_EXIT_FAILURE;
612
+ }
613
+
614
+ if (model_has_mismatch) {
615
+ // model has failed hash somewhere in the model
616
+ return HASH_EXIT_FAILURE;
617
+ }
618
+
619
+ // All checks appears to be fine
620
+ return HASH_EXIT_SUCCESS;
621
+ }
622
+
623
+ // In hash generation mode
624
+ return HASH_EXIT_SUCCESS;
625
+ }
626
+
627
+ int main(int argc, const char ** argv) {
628
+ hash_params params;
629
+ manifest_check_params manifest_check;
630
+ hash_params_parse(argc, argv, params);
631
+
632
+ if (!params.manifest_file.empty()) {
633
+ if (!manifest_type(params.manifest_file, manifest_check)) {
634
+ printf("ERROR cannot open manifest %s", params.manifest_file.c_str());
635
+ return HASH_EXIT_MANIFEST_FILE_ERROR;
636
+ }
637
+
638
+ if (!manifest_check.sha256 && !manifest_check.sha1 && !manifest_check.xxh64 && !manifest_check.uuid) {
639
+ printf("ERROR manifest does not have any known hash format in %s", params.manifest_file.c_str());
640
+ return HASH_EXIT_MANIFEST_UNKNOWN_HASH;
641
+ }
642
+
643
+ printf("manifest %s", params.manifest_file.c_str());
644
+
645
+ if (manifest_check.sha256) {
646
+ printf(" sha256");
647
+ }
648
+
649
+ if (manifest_check.sha1) {
650
+ printf(" sha1");
651
+ }
652
+
653
+ if (manifest_check.xxh64) {
654
+ printf(" xxh64");
655
+ }
656
+
657
+ if (manifest_check.uuid) {
658
+ printf(" uuid");
659
+ }
660
+
661
+ printf("\n");
662
+
663
+ // Autoselect the highest security hash if manifest is provided but
664
+ // the user has not specifically defined the hash they care about
665
+ if (!params.xxh64 && !params.sha1 && !params.uuid && !params.sha256) {
666
+ // User has not selected a specific value, pick most secure hash
667
+ if (manifest_check.sha256) {
668
+ params.sha256 = true;
669
+ } else if (manifest_check.sha1) {
670
+ params.sha1 = true;
671
+ } else if (manifest_check.xxh64) {
672
+ params.xxh64 = true;
673
+ } else if (manifest_check.uuid) {
674
+ params.uuid = true;
675
+ }
676
+ }
677
+
678
+ params.manifest_is_usable = true;
679
+ }
680
+
681
+ // By default if no swich argument provided, assume xxh64
682
+ if (!params.xxh64 && !params.sha1 && !params.uuid && !params.sha256) {
683
+ params.xxh64 = true;
684
+ }
685
+
686
+ hash_exit_code_t exit_code = gguf_hash(params);
687
+
688
+ if (params.manifest_is_usable) {
689
+ printf("\nVerification results for %s - %s\n", params.manifest_file.c_str(), hash_exit_code_to_str(exit_code));
690
+ }
691
+
692
+ return exit_code;
693
+ }
@@ -1,4 +1,4 @@
1
- set(TARGET gguf-split)
1
+ set(TARGET llama-gguf-split)
2
2
  add_executable(${TARGET} gguf-split.cpp)
3
3
  install(TARGETS ${TARGET} RUNTIME)
4
4
  target_link_libraries(${TARGET} PRIVATE common llama ${CMAKE_THREAD_LIBS_INIT})