@fugood/llama.node 0.0.1-alpha.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 (204) hide show
  1. package/CMakeLists.txt +85 -0
  2. package/README.md +56 -0
  3. package/bin/darwin/arm64/llama-node.node +0 -0
  4. package/bin/darwin/x64/llama-node.node +0 -0
  5. package/bin/linux/arm64/llama-node.node +0 -0
  6. package/bin/linux/x64/llama-node.node +0 -0
  7. package/bin/win32/arm64/llama-node.node +0 -0
  8. package/bin/win32/arm64/node.lib +0 -0
  9. package/bin/win32/x64/llama-node.node +0 -0
  10. package/bin/win32/x64/node.lib +0 -0
  11. package/lib/binding.js +13 -0
  12. package/lib/binding.ts +57 -0
  13. package/lib/index.js +24 -0
  14. package/lib/index.ts +13 -0
  15. package/package.json +65 -0
  16. package/src/addons.cpp +506 -0
  17. package/src/llama.cpp/CMakeLists.txt +1320 -0
  18. package/src/llama.cpp/build.zig +172 -0
  19. package/src/llama.cpp/cmake/FindSIMD.cmake +100 -0
  20. package/src/llama.cpp/common/CMakeLists.txt +87 -0
  21. package/src/llama.cpp/common/base64.hpp +392 -0
  22. package/src/llama.cpp/common/common.cpp +2949 -0
  23. package/src/llama.cpp/common/common.h +324 -0
  24. package/src/llama.cpp/common/console.cpp +501 -0
  25. package/src/llama.cpp/common/console.h +19 -0
  26. package/src/llama.cpp/common/grammar-parser.cpp +440 -0
  27. package/src/llama.cpp/common/grammar-parser.h +29 -0
  28. package/src/llama.cpp/common/json-schema-to-grammar.cpp +764 -0
  29. package/src/llama.cpp/common/json-schema-to-grammar.h +4 -0
  30. package/src/llama.cpp/common/json.hpp +24766 -0
  31. package/src/llama.cpp/common/log.h +724 -0
  32. package/src/llama.cpp/common/ngram-cache.cpp +282 -0
  33. package/src/llama.cpp/common/ngram-cache.h +94 -0
  34. package/src/llama.cpp/common/sampling.cpp +353 -0
  35. package/src/llama.cpp/common/sampling.h +147 -0
  36. package/src/llama.cpp/common/stb_image.h +8396 -0
  37. package/src/llama.cpp/common/train.cpp +1513 -0
  38. package/src/llama.cpp/common/train.h +233 -0
  39. package/src/llama.cpp/examples/CMakeLists.txt +52 -0
  40. package/src/llama.cpp/examples/baby-llama/CMakeLists.txt +5 -0
  41. package/src/llama.cpp/examples/baby-llama/baby-llama.cpp +1640 -0
  42. package/src/llama.cpp/examples/batched/CMakeLists.txt +5 -0
  43. package/src/llama.cpp/examples/batched/batched.cpp +262 -0
  44. package/src/llama.cpp/examples/batched-bench/CMakeLists.txt +5 -0
  45. package/src/llama.cpp/examples/batched-bench/batched-bench.cpp +261 -0
  46. package/src/llama.cpp/examples/beam-search/CMakeLists.txt +5 -0
  47. package/src/llama.cpp/examples/beam-search/beam-search.cpp +188 -0
  48. package/src/llama.cpp/examples/benchmark/CMakeLists.txt +6 -0
  49. package/src/llama.cpp/examples/benchmark/benchmark-matmult.cpp +275 -0
  50. package/src/llama.cpp/examples/convert-llama2c-to-ggml/CMakeLists.txt +5 -0
  51. package/src/llama.cpp/examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp +936 -0
  52. package/src/llama.cpp/examples/embedding/CMakeLists.txt +5 -0
  53. package/src/llama.cpp/examples/embedding/embedding.cpp +211 -0
  54. package/src/llama.cpp/examples/eval-callback/CMakeLists.txt +9 -0
  55. package/src/llama.cpp/examples/eval-callback/eval-callback.cpp +195 -0
  56. package/src/llama.cpp/examples/export-lora/CMakeLists.txt +5 -0
  57. package/src/llama.cpp/examples/export-lora/export-lora.cpp +462 -0
  58. package/src/llama.cpp/examples/finetune/CMakeLists.txt +5 -0
  59. package/src/llama.cpp/examples/finetune/finetune.cpp +1861 -0
  60. package/src/llama.cpp/examples/gbnf-validator/CMakeLists.txt +5 -0
  61. package/src/llama.cpp/examples/gbnf-validator/gbnf-validator.cpp +132 -0
  62. package/src/llama.cpp/examples/gguf/CMakeLists.txt +5 -0
  63. package/src/llama.cpp/examples/gguf/gguf.cpp +256 -0
  64. package/src/llama.cpp/examples/gguf-split/CMakeLists.txt +5 -0
  65. package/src/llama.cpp/examples/gguf-split/gguf-split.cpp +553 -0
  66. package/src/llama.cpp/examples/gritlm/CMakeLists.txt +5 -0
  67. package/src/llama.cpp/examples/gritlm/gritlm.cpp +215 -0
  68. package/src/llama.cpp/examples/imatrix/CMakeLists.txt +5 -0
  69. package/src/llama.cpp/examples/imatrix/imatrix.cpp +655 -0
  70. package/src/llama.cpp/examples/infill/CMakeLists.txt +5 -0
  71. package/src/llama.cpp/examples/infill/infill.cpp +767 -0
  72. package/src/llama.cpp/examples/jeopardy/questions.txt +100 -0
  73. package/src/llama.cpp/examples/llama-bench/CMakeLists.txt +5 -0
  74. package/src/llama.cpp/examples/llama-bench/llama-bench.cpp +1286 -0
  75. package/src/llama.cpp/examples/llama.android/app/src/main/cpp/CMakeLists.txt +50 -0
  76. package/src/llama.cpp/examples/llama.android/app/src/main/cpp/llama-android.cpp +443 -0
  77. package/src/llama.cpp/examples/llava/CMakeLists.txt +37 -0
  78. package/src/llama.cpp/examples/llava/clip.cpp +2027 -0
  79. package/src/llama.cpp/examples/llava/clip.h +85 -0
  80. package/src/llama.cpp/examples/llava/llava-cli.cpp +309 -0
  81. package/src/llama.cpp/examples/llava/llava.cpp +426 -0
  82. package/src/llama.cpp/examples/llava/llava.h +50 -0
  83. package/src/llama.cpp/examples/llava/requirements.txt +3 -0
  84. package/src/llama.cpp/examples/lookahead/CMakeLists.txt +5 -0
  85. package/src/llama.cpp/examples/lookahead/lookahead.cpp +485 -0
  86. package/src/llama.cpp/examples/lookup/CMakeLists.txt +23 -0
  87. package/src/llama.cpp/examples/lookup/lookup-create.cpp +41 -0
  88. package/src/llama.cpp/examples/lookup/lookup-merge.cpp +47 -0
  89. package/src/llama.cpp/examples/lookup/lookup-stats.cpp +160 -0
  90. package/src/llama.cpp/examples/lookup/lookup.cpp +258 -0
  91. package/src/llama.cpp/examples/main/CMakeLists.txt +5 -0
  92. package/src/llama.cpp/examples/main/main.cpp +957 -0
  93. package/src/llama.cpp/examples/main-cmake-pkg/CMakeLists.txt +33 -0
  94. package/src/llama.cpp/examples/parallel/CMakeLists.txt +5 -0
  95. package/src/llama.cpp/examples/parallel/parallel.cpp +427 -0
  96. package/src/llama.cpp/examples/passkey/CMakeLists.txt +5 -0
  97. package/src/llama.cpp/examples/passkey/passkey.cpp +302 -0
  98. package/src/llama.cpp/examples/perplexity/CMakeLists.txt +5 -0
  99. package/src/llama.cpp/examples/perplexity/perplexity.cpp +1943 -0
  100. package/src/llama.cpp/examples/quantize/CMakeLists.txt +6 -0
  101. package/src/llama.cpp/examples/quantize/quantize.cpp +423 -0
  102. package/src/llama.cpp/examples/quantize-stats/CMakeLists.txt +6 -0
  103. package/src/llama.cpp/examples/quantize-stats/quantize-stats.cpp +424 -0
  104. package/src/llama.cpp/examples/retrieval/CMakeLists.txt +5 -0
  105. package/src/llama.cpp/examples/retrieval/retrieval.cpp +350 -0
  106. package/src/llama.cpp/examples/save-load-state/CMakeLists.txt +5 -0
  107. package/src/llama.cpp/examples/save-load-state/save-load-state.cpp +246 -0
  108. package/src/llama.cpp/examples/server/CMakeLists.txt +40 -0
  109. package/src/llama.cpp/examples/server/bench/requirements.txt +2 -0
  110. package/src/llama.cpp/examples/server/httplib.h +9465 -0
  111. package/src/llama.cpp/examples/server/server.cpp +3826 -0
  112. package/src/llama.cpp/examples/server/tests/requirements.txt +6 -0
  113. package/src/llama.cpp/examples/server/utils.hpp +653 -0
  114. package/src/llama.cpp/examples/simple/CMakeLists.txt +5 -0
  115. package/src/llama.cpp/examples/simple/simple.cpp +183 -0
  116. package/src/llama.cpp/examples/speculative/CMakeLists.txt +5 -0
  117. package/src/llama.cpp/examples/speculative/speculative.cpp +614 -0
  118. package/src/llama.cpp/examples/sycl/CMakeLists.txt +9 -0
  119. package/src/llama.cpp/examples/sycl/ls-sycl-device.cpp +13 -0
  120. package/src/llama.cpp/examples/tokenize/CMakeLists.txt +5 -0
  121. package/src/llama.cpp/examples/tokenize/tokenize.cpp +42 -0
  122. package/src/llama.cpp/examples/train-text-from-scratch/CMakeLists.txt +5 -0
  123. package/src/llama.cpp/examples/train-text-from-scratch/train-text-from-scratch.cpp +1252 -0
  124. package/src/llama.cpp/ggml-alloc.c +985 -0
  125. package/src/llama.cpp/ggml-alloc.h +76 -0
  126. package/src/llama.cpp/ggml-backend-impl.h +141 -0
  127. package/src/llama.cpp/ggml-backend.c +2099 -0
  128. package/src/llama.cpp/ggml-backend.h +233 -0
  129. package/src/llama.cpp/ggml-common.h +1853 -0
  130. package/src/llama.cpp/ggml-cuda.h +43 -0
  131. package/src/llama.cpp/ggml-impl.h +265 -0
  132. package/src/llama.cpp/ggml-kompute.cpp +2006 -0
  133. package/src/llama.cpp/ggml-kompute.h +46 -0
  134. package/src/llama.cpp/ggml-metal.h +66 -0
  135. package/src/llama.cpp/ggml-mpi.c +216 -0
  136. package/src/llama.cpp/ggml-mpi.h +39 -0
  137. package/src/llama.cpp/ggml-opencl.cpp +2301 -0
  138. package/src/llama.cpp/ggml-opencl.h +36 -0
  139. package/src/llama.cpp/ggml-quants.c +12678 -0
  140. package/src/llama.cpp/ggml-quants.h +133 -0
  141. package/src/llama.cpp/ggml-sycl.cpp +17882 -0
  142. package/src/llama.cpp/ggml-sycl.h +49 -0
  143. package/src/llama.cpp/ggml-vulkan-shaders.hpp +69849 -0
  144. package/src/llama.cpp/ggml-vulkan.cpp +6442 -0
  145. package/src/llama.cpp/ggml-vulkan.h +29 -0
  146. package/src/llama.cpp/ggml.c +21819 -0
  147. package/src/llama.cpp/ggml.h +2403 -0
  148. package/src/llama.cpp/llama.cpp +17468 -0
  149. package/src/llama.cpp/llama.h +1117 -0
  150. package/src/llama.cpp/pocs/CMakeLists.txt +12 -0
  151. package/src/llama.cpp/pocs/vdot/CMakeLists.txt +9 -0
  152. package/src/llama.cpp/pocs/vdot/q8dot.cpp +172 -0
  153. package/src/llama.cpp/pocs/vdot/vdot.cpp +310 -0
  154. package/src/llama.cpp/prompts/LLM-questions.txt +49 -0
  155. package/src/llama.cpp/prompts/alpaca.txt +1 -0
  156. package/src/llama.cpp/prompts/assistant.txt +31 -0
  157. package/src/llama.cpp/prompts/chat-with-baichuan.txt +4 -0
  158. package/src/llama.cpp/prompts/chat-with-bob.txt +7 -0
  159. package/src/llama.cpp/prompts/chat-with-qwen.txt +1 -0
  160. package/src/llama.cpp/prompts/chat-with-vicuna-v0.txt +7 -0
  161. package/src/llama.cpp/prompts/chat-with-vicuna-v1.txt +7 -0
  162. package/src/llama.cpp/prompts/chat.txt +28 -0
  163. package/src/llama.cpp/prompts/dan-modified.txt +1 -0
  164. package/src/llama.cpp/prompts/dan.txt +1 -0
  165. package/src/llama.cpp/prompts/mnemonics.txt +93 -0
  166. package/src/llama.cpp/prompts/parallel-questions.txt +43 -0
  167. package/src/llama.cpp/prompts/reason-act.txt +18 -0
  168. package/src/llama.cpp/requirements/requirements-convert-hf-to-gguf.txt +3 -0
  169. package/src/llama.cpp/requirements/requirements-convert-llama-ggml-to-gguf.txt +1 -0
  170. package/src/llama.cpp/requirements/requirements-convert-lora-to-ggml.txt +2 -0
  171. package/src/llama.cpp/requirements/requirements-convert-persimmon-to-gguf.txt +2 -0
  172. package/src/llama.cpp/requirements/requirements-convert.txt +5 -0
  173. package/src/llama.cpp/requirements.txt +12 -0
  174. package/src/llama.cpp/scripts/gen-build-info-cpp.cmake +24 -0
  175. package/src/llama.cpp/scripts/xxd.cmake +16 -0
  176. package/src/llama.cpp/sgemm.cpp +999 -0
  177. package/src/llama.cpp/sgemm.h +12 -0
  178. package/src/llama.cpp/tests/CMakeLists.txt +78 -0
  179. package/src/llama.cpp/tests/get-model.cpp +21 -0
  180. package/src/llama.cpp/tests/get-model.h +2 -0
  181. package/src/llama.cpp/tests/test-autorelease.cpp +24 -0
  182. package/src/llama.cpp/tests/test-backend-ops.cpp +2266 -0
  183. package/src/llama.cpp/tests/test-c.c +7 -0
  184. package/src/llama.cpp/tests/test-chat-template.cpp +107 -0
  185. package/src/llama.cpp/tests/test-double-float.cpp +57 -0
  186. package/src/llama.cpp/tests/test-grad0.cpp +1606 -0
  187. package/src/llama.cpp/tests/test-grammar-integration.cpp +243 -0
  188. package/src/llama.cpp/tests/test-grammar-parser.cpp +250 -0
  189. package/src/llama.cpp/tests/test-json-schema-to-grammar.cpp +899 -0
  190. package/src/llama.cpp/tests/test-llama-grammar.cpp +402 -0
  191. package/src/llama.cpp/tests/test-model-load-cancel.cpp +27 -0
  192. package/src/llama.cpp/tests/test-opt.cpp +181 -0
  193. package/src/llama.cpp/tests/test-quantize-fns.cpp +185 -0
  194. package/src/llama.cpp/tests/test-quantize-perf.cpp +363 -0
  195. package/src/llama.cpp/tests/test-rope.cpp +221 -0
  196. package/src/llama.cpp/tests/test-sampling.cpp +301 -0
  197. package/src/llama.cpp/tests/test-tokenizer-0-falcon.cpp +187 -0
  198. package/src/llama.cpp/tests/test-tokenizer-0-llama.cpp +190 -0
  199. package/src/llama.cpp/tests/test-tokenizer-1-bpe.cpp +123 -0
  200. package/src/llama.cpp/tests/test-tokenizer-1-llama.cpp +111 -0
  201. package/src/llama.cpp/unicode-data.cpp +1651 -0
  202. package/src/llama.cpp/unicode-data.h +16 -0
  203. package/src/llama.cpp/unicode.cpp +277 -0
  204. package/src/llama.cpp/unicode.h +28 -0
@@ -0,0 +1,6 @@
1
+ set(TARGET quantize)
2
+ add_executable(${TARGET} quantize.cpp)
3
+ install(TARGETS ${TARGET} RUNTIME)
4
+ target_link_libraries(${TARGET} PRIVATE llama build_info ${CMAKE_THREAD_LIBS_INIT})
5
+ target_include_directories(${TARGET} PRIVATE ../../common)
6
+ target_compile_features(${TARGET} PRIVATE cxx_std_11)
@@ -0,0 +1,423 @@
1
+ #include "common.h"
2
+ #include "llama.h"
3
+
4
+ #include <cstdio>
5
+ #include <cstring>
6
+ #include <vector>
7
+ #include <string>
8
+ #include <unordered_map>
9
+ #include <fstream>
10
+ #include <cmath>
11
+ #include <algorithm>
12
+
13
+ struct quant_option {
14
+ std::string name;
15
+ llama_ftype ftype;
16
+ std::string desc;
17
+ };
18
+
19
+ static const std::vector<struct quant_option> QUANT_OPTIONS = {
20
+ { "Q4_0", LLAMA_FTYPE_MOSTLY_Q4_0, " 3.56G, +0.2166 ppl @ LLaMA-v1-7B", },
21
+ { "Q4_1", LLAMA_FTYPE_MOSTLY_Q4_1, " 3.90G, +0.1585 ppl @ LLaMA-v1-7B", },
22
+ { "Q5_0", LLAMA_FTYPE_MOSTLY_Q5_0, " 4.33G, +0.0683 ppl @ LLaMA-v1-7B", },
23
+ { "Q5_1", LLAMA_FTYPE_MOSTLY_Q5_1, " 4.70G, +0.0349 ppl @ LLaMA-v1-7B", },
24
+ { "IQ2_XXS",LLAMA_FTYPE_MOSTLY_IQ2_XXS," 2.06 bpw quantization", },
25
+ { "IQ2_XS", LLAMA_FTYPE_MOSTLY_IQ2_XS, " 2.31 bpw quantization", },
26
+ { "IQ2_S", LLAMA_FTYPE_MOSTLY_IQ2_S, " 2.5 bpw quantization", },
27
+ { "IQ2_M", LLAMA_FTYPE_MOSTLY_IQ2_M, " 2.7 bpw quantization", },
28
+ { "IQ1_S", LLAMA_FTYPE_MOSTLY_IQ1_S, " 1.56 bpw quantization", },
29
+ { "IQ1_M", LLAMA_FTYPE_MOSTLY_IQ1_M, " 1.75 bpw quantization", },
30
+ { "Q2_K", LLAMA_FTYPE_MOSTLY_Q2_K, " 2.63G, +0.6717 ppl @ LLaMA-v1-7B", },
31
+ { "Q2_K_S", LLAMA_FTYPE_MOSTLY_Q2_K_S, " 2.16G, +9.0634 ppl @ LLaMA-v1-7B", },
32
+ { "IQ3_XXS",LLAMA_FTYPE_MOSTLY_IQ3_XXS," 3.06 bpw quantization", },
33
+ { "IQ3_S", LLAMA_FTYPE_MOSTLY_IQ3_S, " 3.44 bpw quantization", },
34
+ { "IQ3_M", LLAMA_FTYPE_MOSTLY_IQ3_M, " 3.66 bpw quantization mix", },
35
+ { "Q3_K", LLAMA_FTYPE_MOSTLY_Q3_K_M, "alias for Q3_K_M" },
36
+ { "IQ3_XS", LLAMA_FTYPE_MOSTLY_IQ3_XS, " 3.3 bpw quantization" , },
37
+ { "Q3_K_S", LLAMA_FTYPE_MOSTLY_Q3_K_S, " 2.75G, +0.5551 ppl @ LLaMA-v1-7B", },
38
+ { "Q3_K_M", LLAMA_FTYPE_MOSTLY_Q3_K_M, " 3.07G, +0.2496 ppl @ LLaMA-v1-7B", },
39
+ { "Q3_K_L", LLAMA_FTYPE_MOSTLY_Q3_K_L, " 3.35G, +0.1764 ppl @ LLaMA-v1-7B", },
40
+ { "IQ4_NL", LLAMA_FTYPE_MOSTLY_IQ4_NL, " 4.50 bpw non-linear quantization", },
41
+ { "IQ4_XS", LLAMA_FTYPE_MOSTLY_IQ4_XS, " 4.25 bpw non-linear quantization", },
42
+ { "Q4_K", LLAMA_FTYPE_MOSTLY_Q4_K_M, "alias for Q4_K_M", },
43
+ { "Q4_K_S", LLAMA_FTYPE_MOSTLY_Q4_K_S, " 3.59G, +0.0992 ppl @ LLaMA-v1-7B", },
44
+ { "Q4_K_M", LLAMA_FTYPE_MOSTLY_Q4_K_M, " 3.80G, +0.0532 ppl @ LLaMA-v1-7B", },
45
+ { "Q5_K", LLAMA_FTYPE_MOSTLY_Q5_K_M, "alias for Q5_K_M", },
46
+ { "Q5_K_S", LLAMA_FTYPE_MOSTLY_Q5_K_S, " 4.33G, +0.0400 ppl @ LLaMA-v1-7B", },
47
+ { "Q5_K_M", LLAMA_FTYPE_MOSTLY_Q5_K_M, " 4.45G, +0.0122 ppl @ LLaMA-v1-7B", },
48
+ { "Q6_K", LLAMA_FTYPE_MOSTLY_Q6_K, " 5.15G, +0.0008 ppl @ LLaMA-v1-7B", },
49
+ { "Q8_0", LLAMA_FTYPE_MOSTLY_Q8_0, " 6.70G, +0.0004 ppl @ LLaMA-v1-7B", },
50
+ { "F16", LLAMA_FTYPE_MOSTLY_F16, "13.00G @ 7B", },
51
+ { "F32", LLAMA_FTYPE_ALL_F32, "26.00G @ 7B", },
52
+ // Note: Ensure COPY comes after F32 to avoid ftype 0 from matching.
53
+ { "COPY", LLAMA_FTYPE_ALL_F32, "only copy tensors, no quantizing", },
54
+ };
55
+
56
+
57
+ static bool try_parse_ftype(const std::string & ftype_str_in, llama_ftype & ftype, std::string & ftype_str_out) {
58
+ std::string ftype_str;
59
+
60
+ for (auto ch : ftype_str_in) {
61
+ ftype_str.push_back(std::toupper(ch));
62
+ }
63
+ for (auto & it : QUANT_OPTIONS) {
64
+ if (it.name == ftype_str) {
65
+ ftype = it.ftype;
66
+ ftype_str_out = it.name;
67
+ return true;
68
+ }
69
+ }
70
+ try {
71
+ int ftype_int = std::stoi(ftype_str);
72
+ for (auto & it : QUANT_OPTIONS) {
73
+ if (it.ftype == ftype_int) {
74
+ ftype = it.ftype;
75
+ ftype_str_out = it.name;
76
+ return true;
77
+ }
78
+ }
79
+ }
80
+ catch (...) {
81
+ // stoi failed
82
+ }
83
+ return false;
84
+ }
85
+
86
+ // usage:
87
+ // ./quantize [--allow-requantize] [--leave-output-tensor] [--pure] models/llama/ggml-model.gguf [models/llama/ggml-model-quant.gguf] type [nthreads]
88
+ //
89
+ [[noreturn]]
90
+ static void usage(const char * executable) {
91
+ printf("usage: %s [--help] [--allow-requantize] [--leave-output-tensor] [--pure] [--imatrix] [--include-weights] [--exclude-weights] [--output-tensor-type] [--token-embedding-type] [--override-kv] model-f32.gguf [model-quant.gguf] type [nthreads]\n\n", executable);
92
+ printf(" --allow-requantize: Allows requantizing tensors that have already been quantized. Warning: This can severely reduce quality compared to quantizing from 16bit or 32bit\n");
93
+ printf(" --leave-output-tensor: Will leave output.weight un(re)quantized. Increases model size but may also increase quality, especially when requantizing\n");
94
+ printf(" --pure: Disable k-quant mixtures and quantize all tensors to the same type\n");
95
+ printf(" --imatrix file_name: use data in file_name as importance matrix for quant optimizations\n");
96
+ printf(" --include-weights tensor_name: use importance matrix for this/these tensor(s)\n");
97
+ printf(" --exclude-weights tensor_name: use importance matrix for this/these tensor(s)\n");
98
+ printf(" --output-tensor-type ggml_type: use this ggml_type for the output.weight tensor\n");
99
+ printf(" --token-embedding-type ggml_type: use this ggml_type for the token embeddings tensor\n");
100
+ printf(" --override-kv KEY=TYPE:VALUE\n");
101
+ printf(" Advanced option to override model metadata by key in the quantized model. May be specified multiple times.\n");
102
+ printf("Note: --include-weights and --exclude-weights cannot be used together\n");
103
+ printf("\nAllowed quantization types:\n");
104
+ for (auto & it : QUANT_OPTIONS) {
105
+ if (it.name != "COPY") {
106
+ printf(" %2d or ", it.ftype);
107
+ } else {
108
+ printf(" ");
109
+ }
110
+ printf("%-7s : %s\n", it.name.c_str(), it.desc.c_str());
111
+ }
112
+ exit(1);
113
+ }
114
+
115
+ static void load_imatrix(const std::string & imatrix_file, std::unordered_map<std::string, std::vector<float>> & imatrix_data) {
116
+ std::ifstream in(imatrix_file.c_str(), std::ios::binary);
117
+ if (!in) {
118
+ printf("%s: failed to open %s\n",__func__, imatrix_file.c_str());
119
+ exit(1);
120
+ }
121
+ int n_entries;
122
+ in.read((char *)&n_entries, sizeof(n_entries));
123
+ if (in.fail() || n_entries < 1) {
124
+ printf("%s: no data in file %s\n", __func__, imatrix_file.c_str());
125
+ exit(1);
126
+ }
127
+ for (int i = 0; i < n_entries; ++i) {
128
+ int len; in.read((char *)&len, sizeof(len));
129
+ std::vector<char> name_as_vec(len+1);
130
+ in.read((char *)name_as_vec.data(), len);
131
+ if (in.fail()) {
132
+ printf("%s: failed reading name for entry %d from %s\n", __func__, i+1, imatrix_file.c_str());
133
+ exit(1);
134
+ }
135
+ name_as_vec[len] = 0;
136
+ std::string name{name_as_vec.data()};
137
+ auto & e = imatrix_data[name];
138
+ int ncall;
139
+ in.read((char *)&ncall, sizeof(ncall));
140
+ int nval;
141
+ in.read((char *)&nval, sizeof(nval));
142
+ if (in.fail() || nval < 1) {
143
+ printf("%s: failed reading number of values for entry %d\n", __func__, i);
144
+ imatrix_data = {};
145
+ exit(1);
146
+ }
147
+ e.resize(nval);
148
+ in.read((char *)e.data(), nval*sizeof(float));
149
+ if (in.fail()) {
150
+ printf("%s: failed reading data for entry %d\n", __func__, i);
151
+ imatrix_data = {};
152
+ exit(1);
153
+ }
154
+ if (ncall > 0) {
155
+ for (auto& v : e) v /= ncall;
156
+ }
157
+
158
+ if (getenv("LLAMA_TRACE")) {
159
+ printf("%s: loaded data (size = %6d, ncall = %6d) for '%s'\n", __func__, int(e.size()), ncall, name.c_str());
160
+ }
161
+ }
162
+ printf("%s: loaded %d importance matrix entries from %s\n", __func__, int(imatrix_data.size()), imatrix_file.c_str());
163
+ }
164
+
165
+ static void prepare_imatrix(const std::string & imatrix_file,
166
+ const std::vector<std::string> & included_weights,
167
+ const std::vector<std::string> & excluded_weights,
168
+ std::unordered_map<std::string, std::vector<float>> & imatrix_data) {
169
+ if (!imatrix_file.empty()) {
170
+ load_imatrix(imatrix_file, imatrix_data);
171
+ }
172
+ if (imatrix_data.empty()) {
173
+ return;
174
+ }
175
+ if (!excluded_weights.empty()) {
176
+ for (auto& name : excluded_weights) {
177
+ for (auto it = imatrix_data.begin(); it != imatrix_data.end(); ) {
178
+ auto pos = it->first.find(name);
179
+ if (pos != std::string::npos) it = imatrix_data.erase(it);
180
+ else ++it;
181
+ }
182
+ }
183
+ }
184
+ if (!included_weights.empty()) {
185
+ std::unordered_map<std::string, std::vector<float>> tmp;
186
+ for (auto& name : included_weights) {
187
+ for (auto& e : imatrix_data) {
188
+ auto pos = e.first.find(name);
189
+ if (pos != std::string::npos) {
190
+ tmp.emplace(std::move(e));
191
+ }
192
+ }
193
+ }
194
+ imatrix_data = std::move(tmp);
195
+ }
196
+ if (!imatrix_data.empty()) {
197
+ printf("%s: have %d importance matrix entries\n", __func__, int(imatrix_data.size()));
198
+ }
199
+ }
200
+
201
+ static ggml_type parse_ggml_type(const char * arg) {
202
+ ggml_type result = GGML_TYPE_COUNT;
203
+ for (int j = 0; j < GGML_TYPE_COUNT; ++j) {
204
+ auto type = ggml_type(j);
205
+ const auto * name = ggml_type_name(type);
206
+ if (name && strcmp(arg, name) == 0) {
207
+ result = type; break;
208
+ }
209
+ }
210
+ return result;
211
+ }
212
+
213
+ static bool parse_kv_override(const char * data, std::vector<llama_model_kv_override> & overrides) {
214
+ const char* sep = strchr(data, '=');
215
+ if (sep == nullptr || sep - data >= 128) {
216
+ fprintf(stderr, "%s: malformed KV override '%s'\n", __func__, data);
217
+ return false;
218
+ }
219
+ llama_model_kv_override kvo;
220
+ std::strncpy(kvo.key, data, sep - data);
221
+ kvo.key[sep - data] = 0;
222
+ sep++;
223
+ if (strncmp(sep, "int:", 4) == 0) {
224
+ sep += 4;
225
+ kvo.tag = LLAMA_KV_OVERRIDE_TYPE_INT;
226
+ kvo.int_value = std::atol(sep);
227
+ } else if (strncmp(sep, "float:", 6) == 0) {
228
+ sep += 6;
229
+ kvo.tag = LLAMA_KV_OVERRIDE_TYPE_FLOAT;
230
+ kvo.float_value = std::atof(sep);
231
+ } else if (strncmp(sep, "bool:", 5) == 0) {
232
+ sep += 5;
233
+ kvo.tag = LLAMA_KV_OVERRIDE_TYPE_BOOL;
234
+ if (std::strcmp(sep, "true") == 0) {
235
+ kvo.bool_value = true;
236
+ } else if (std::strcmp(sep, "false") == 0) {
237
+ kvo.bool_value = false;
238
+ } else {
239
+ fprintf(stderr, "%s: invalid boolean value for KV override '%s'\n", __func__, data);
240
+ return false;
241
+ }
242
+ } else {
243
+ fprintf(stderr, "%s: invalid type for KV override '%s'\n", __func__, data);
244
+ return false;
245
+ }
246
+ overrides.emplace_back(std::move(kvo));
247
+ return true;
248
+ }
249
+
250
+ int main(int argc, char ** argv) {
251
+ if (argc < 3) {
252
+ usage(argv[0]);
253
+ }
254
+
255
+ llama_model_quantize_params params = llama_model_quantize_default_params();
256
+
257
+ int arg_idx = 1;
258
+ std::string imatrix_file;
259
+ std::vector<std::string> included_weights, excluded_weights;
260
+ std::vector<llama_model_kv_override> kv_overrides;
261
+
262
+ for (; arg_idx < argc && strncmp(argv[arg_idx], "--", 2) == 0; arg_idx++) {
263
+ if (strcmp(argv[arg_idx], "--leave-output-tensor") == 0) {
264
+ params.quantize_output_tensor = false;
265
+ } else if (strcmp(argv[arg_idx], "--output-tensor-type") == 0) {
266
+ if (arg_idx < argc-1) {
267
+ params.output_tensor_type = parse_ggml_type(argv[++arg_idx]);
268
+ } else {
269
+ usage(argv[0]);
270
+ }
271
+ } else if (strcmp(argv[arg_idx], "--token-embedding-type") == 0) {
272
+ if (arg_idx < argc-1) {
273
+ params.token_embedding_type = parse_ggml_type(argv[++arg_idx]);
274
+ } else {
275
+ usage(argv[0]);
276
+ }
277
+ } else if (strcmp(argv[arg_idx], "--override-kv") == 0) {
278
+ if (arg_idx == argc-1 || !parse_kv_override(argv[++arg_idx], kv_overrides)) {
279
+ usage(argv[0]);
280
+ }
281
+ } else if (strcmp(argv[arg_idx], "--allow-requantize") == 0) {
282
+ params.allow_requantize = true;
283
+ } else if (strcmp(argv[arg_idx], "--pure") == 0) {
284
+ params.pure = true;
285
+ } else if (strcmp(argv[arg_idx], "--imatrix") == 0) {
286
+ if (arg_idx < argc-1) {
287
+ imatrix_file = argv[++arg_idx];
288
+ } else {
289
+ usage(argv[0]);
290
+ }
291
+ } else if (strcmp(argv[arg_idx], "--include-weights") == 0) {
292
+ if (arg_idx < argc-1) {
293
+ included_weights.emplace_back(argv[++arg_idx]);
294
+ } else {
295
+ usage(argv[0]);
296
+ }
297
+ } else if (strcmp(argv[arg_idx], "--exclude-weights") == 0) {
298
+ if (arg_idx < argc-1) {
299
+ excluded_weights.emplace_back(argv[++arg_idx]);
300
+ } else {
301
+ usage(argv[0]);
302
+ }
303
+ } else {
304
+ usage(argv[0]);
305
+ }
306
+ }
307
+
308
+ if (argc - arg_idx < 2) {
309
+ printf("%s: bad arguments\n", argv[0]);
310
+ usage(argv[0]);
311
+ }
312
+ if (!included_weights.empty() && !excluded_weights.empty()) {
313
+ usage(argv[0]);
314
+ }
315
+
316
+ std::unordered_map<std::string, std::vector<float>> imatrix_data;
317
+ prepare_imatrix(imatrix_file, included_weights, excluded_weights, imatrix_data);
318
+ if (!imatrix_data.empty()) {
319
+ params.imatrix = &imatrix_data;
320
+ }
321
+ if (!kv_overrides.empty()) {
322
+ kv_overrides.emplace_back();
323
+ kv_overrides.back().key[0] = 0;
324
+ params.kv_overrides = &kv_overrides;
325
+ }
326
+
327
+ llama_backend_init();
328
+
329
+ // parse command line arguments
330
+ const std::string fname_inp = argv[arg_idx];
331
+ arg_idx++;
332
+ std::string fname_out;
333
+
334
+ std::string ftype_str;
335
+ if (try_parse_ftype(argv[arg_idx], params.ftype, ftype_str)) {
336
+ std::string fpath;
337
+ const size_t pos = fname_inp.find_last_of("/\\");
338
+ if (pos != std::string::npos) {
339
+ fpath = fname_inp.substr(0, pos + 1);
340
+ }
341
+ // export as [inp path]/ggml-model-[ftype].gguf
342
+ fname_out = fpath + "ggml-model-" + ftype_str + ".gguf";
343
+ arg_idx++;
344
+ if (ftype_str == "COPY") {
345
+ params.only_copy = true;
346
+ }
347
+ } else {
348
+ fname_out = argv[arg_idx];
349
+ arg_idx++;
350
+
351
+ if (argc <= arg_idx) {
352
+ fprintf(stderr, "%s: missing ftype\n", __func__);
353
+ return 1;
354
+ }
355
+ if (!try_parse_ftype(argv[arg_idx], params.ftype, ftype_str)) {
356
+ fprintf(stderr, "%s: invalid ftype '%s'\n", __func__, argv[3]);
357
+ return 1;
358
+ }
359
+ if (ftype_str == "COPY") {
360
+ params.only_copy = true;
361
+ }
362
+ arg_idx++;
363
+ }
364
+
365
+ // parse nthreads
366
+ if (argc > arg_idx) {
367
+ try {
368
+ params.nthread = std::stoi(argv[arg_idx]);
369
+ }
370
+ catch (const std::exception & e) {
371
+ fprintf(stderr, "%s: invalid nthread '%s' (%s)\n", __func__, argv[arg_idx], e.what());
372
+ return 1;
373
+ }
374
+ }
375
+
376
+ if ((params.ftype == LLAMA_FTYPE_MOSTLY_IQ2_XS || params.ftype == LLAMA_FTYPE_MOSTLY_IQ2_XXS ||
377
+ params.ftype == LLAMA_FTYPE_MOSTLY_IQ2_S ||
378
+ params.ftype == LLAMA_FTYPE_MOSTLY_Q2_K_S ||
379
+ params.ftype == LLAMA_FTYPE_MOSTLY_IQ1_S ||
380
+ params.ftype == LLAMA_FTYPE_MOSTLY_IQ1_M) && imatrix_data.empty()) {
381
+ fprintf(stderr, "\n==========================================================================================================\n");
382
+ fprintf(stderr, "Please do not use IQ1_S, IQ1_M, IQ2_S, IQ2_XXS, IQ2_XS or Q2_K_S quantization without an importance matrix\n");
383
+ fprintf(stderr, "==========================================================================================================\n\n\n");
384
+ return 1;
385
+ }
386
+
387
+ print_build_info();
388
+
389
+ fprintf(stderr, "%s: quantizing '%s' to '%s' as %s", __func__, fname_inp.c_str(), fname_out.c_str(), ftype_str.c_str());
390
+ if (params.nthread > 0) {
391
+ fprintf(stderr, " using %d threads", params.nthread);
392
+ }
393
+ fprintf(stderr, "\n");
394
+
395
+ const int64_t t_main_start_us = llama_time_us();
396
+
397
+ int64_t t_quantize_us = 0;
398
+
399
+ // load the model
400
+ {
401
+ const int64_t t_start_us = llama_time_us();
402
+
403
+ if (llama_model_quantize(fname_inp.c_str(), fname_out.c_str(), &params)) {
404
+ fprintf(stderr, "%s: failed to quantize model from '%s'\n", __func__, fname_inp.c_str());
405
+ return 1;
406
+ }
407
+
408
+ t_quantize_us = llama_time_us() - t_start_us;
409
+ }
410
+
411
+ // report timing
412
+ {
413
+ const int64_t t_main_end_us = llama_time_us();
414
+
415
+ printf("\n");
416
+ printf("%s: quantize time = %8.2f ms\n", __func__, t_quantize_us/1000.0);
417
+ printf("%s: total time = %8.2f ms\n", __func__, (t_main_end_us - t_main_start_us)/1000.0);
418
+ }
419
+
420
+ llama_backend_free();
421
+
422
+ return 0;
423
+ }
@@ -0,0 +1,6 @@
1
+ set(TARGET quantize-stats)
2
+ add_executable(${TARGET} quantize-stats.cpp)
3
+ install(TARGETS ${TARGET} RUNTIME)
4
+ target_link_libraries(${TARGET} PRIVATE llama build_info ${CMAKE_THREAD_LIBS_INIT})
5
+ target_include_directories(${TARGET} PRIVATE ../../common)
6
+ target_compile_features(${TARGET} PRIVATE cxx_std_11)