@fugood/llama.node 0.3.16 → 0.3.17

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 (202) hide show
  1. package/CMakeLists.txt +3 -0
  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-cuda/arm64/llama-node.node +0 -0
  7. package/bin/linux-cuda/x64/llama-node.node +0 -0
  8. package/bin/linux-vulkan/arm64/llama-node.node +0 -0
  9. package/bin/linux-vulkan/x64/llama-node.node +0 -0
  10. package/bin/win32/arm64/llama-node.node +0 -0
  11. package/bin/win32/arm64/node.lib +0 -0
  12. package/bin/win32/x64/llama-node.node +0 -0
  13. package/bin/win32/x64/node.lib +0 -0
  14. package/bin/win32-vulkan/arm64/llama-node.node +0 -0
  15. package/bin/win32-vulkan/arm64/node.lib +0 -0
  16. package/bin/win32-vulkan/x64/llama-node.node +0 -0
  17. package/bin/win32-vulkan/x64/node.lib +0 -0
  18. package/lib/binding.ts +5 -0
  19. package/package.json +1 -1
  20. package/src/LlamaCompletionWorker.cpp +8 -0
  21. package/src/LlamaCompletionWorker.h +1 -0
  22. package/src/LlamaContext.cpp +3 -2
  23. package/src/llama.cpp/.github/workflows/build-linux-cross.yml +124 -0
  24. package/src/llama.cpp/.github/workflows/build.yml +70 -27
  25. package/src/llama.cpp/.github/workflows/docker.yml +6 -6
  26. package/src/llama.cpp/.github/workflows/server.yml +7 -11
  27. package/src/llama.cpp/CMakeLists.txt +23 -1
  28. package/src/llama.cpp/common/CMakeLists.txt +6 -3
  29. package/src/llama.cpp/common/arg.cpp +809 -105
  30. package/src/llama.cpp/common/arg.h +9 -0
  31. package/src/llama.cpp/common/chat.cpp +1 -1
  32. package/src/llama.cpp/common/common.cpp +31 -521
  33. package/src/llama.cpp/common/common.h +17 -36
  34. package/src/llama.cpp/common/json-schema-to-grammar.cpp +3 -0
  35. package/src/llama.cpp/common/llguidance.cpp +30 -47
  36. package/src/llama.cpp/common/minja/chat-template.hpp +15 -7
  37. package/src/llama.cpp/common/minja/minja.hpp +119 -93
  38. package/src/llama.cpp/common/sampling.cpp +3 -0
  39. package/src/llama.cpp/docs/build.md +122 -7
  40. package/src/llama.cpp/examples/CMakeLists.txt +0 -9
  41. package/src/llama.cpp/examples/batched/batched.cpp +1 -1
  42. package/src/llama.cpp/examples/batched-bench/batched-bench.cpp +1 -1
  43. package/src/llama.cpp/examples/embedding/embedding.cpp +7 -1
  44. package/src/llama.cpp/examples/export-lora/export-lora.cpp +1 -1
  45. package/src/llama.cpp/examples/gguf-split/gguf-split.cpp +15 -16
  46. package/src/llama.cpp/examples/gritlm/gritlm.cpp +1 -1
  47. package/src/llama.cpp/examples/llama-bench/llama-bench.cpp +210 -8
  48. package/src/llama.cpp/examples/llama.android/llama/build.gradle.kts +1 -0
  49. package/src/llama.cpp/examples/llava/CMakeLists.txt +39 -24
  50. package/src/llama.cpp/examples/llava/clip-impl.h +345 -0
  51. package/src/llama.cpp/examples/llava/clip.cpp +2152 -1803
  52. package/src/llama.cpp/examples/llava/clip.h +39 -22
  53. package/src/llama.cpp/examples/llava/deprecation-warning.cpp +22 -0
  54. package/src/llama.cpp/examples/llava/llava.cpp +64 -52
  55. package/src/llama.cpp/examples/llava/mtmd-cli.cpp +344 -0
  56. package/src/llama.cpp/examples/llava/mtmd.cpp +708 -0
  57. package/src/llama.cpp/examples/llava/mtmd.h +168 -0
  58. package/src/llama.cpp/examples/llava/{qwen2vl-cli.cpp → qwen2vl-test.cpp} +83 -31
  59. package/src/llama.cpp/examples/main/main.cpp +16 -5
  60. package/src/llama.cpp/examples/parallel/parallel.cpp +3 -1
  61. package/src/llama.cpp/examples/passkey/passkey.cpp +1 -1
  62. package/src/llama.cpp/examples/perplexity/perplexity.cpp +17 -3
  63. package/src/llama.cpp/examples/quantize/quantize.cpp +115 -2
  64. package/src/llama.cpp/examples/rpc/CMakeLists.txt +4 -2
  65. package/src/llama.cpp/examples/rpc/rpc-server.cpp +163 -8
  66. package/src/llama.cpp/examples/run/CMakeLists.txt +12 -1
  67. package/src/llama.cpp/examples/run/run.cpp +14 -28
  68. package/src/llama.cpp/examples/server/httplib.h +313 -247
  69. package/src/llama.cpp/examples/server/server.cpp +238 -139
  70. package/src/llama.cpp/examples/server/utils.hpp +51 -2
  71. package/src/llama.cpp/examples/speculative/speculative.cpp +1 -1
  72. package/src/llama.cpp/examples/speculative-simple/speculative-simple.cpp +1 -1
  73. package/src/llama.cpp/examples/sycl/build.sh +2 -2
  74. package/src/llama.cpp/examples/sycl/win-build-sycl.bat +2 -2
  75. package/src/llama.cpp/examples/tts/tts.cpp +6 -9
  76. package/src/llama.cpp/ggml/CMakeLists.txt +8 -2
  77. package/src/llama.cpp/ggml/cmake/GitVars.cmake +22 -0
  78. package/src/llama.cpp/ggml/include/ggml-cpu.h +5 -0
  79. package/src/llama.cpp/ggml/include/ggml-rpc.h +6 -1
  80. package/src/llama.cpp/ggml/include/ggml.h +66 -99
  81. package/src/llama.cpp/ggml/src/CMakeLists.txt +10 -7
  82. package/src/llama.cpp/ggml/src/ggml-cann/CMakeLists.txt +0 -2
  83. package/src/llama.cpp/ggml/src/ggml-cann/acl_tensor.cpp +8 -4
  84. package/src/llama.cpp/ggml/src/ggml-cann/acl_tensor.h +5 -5
  85. package/src/llama.cpp/ggml/src/ggml-cann/aclnn_ops.cpp +692 -1534
  86. package/src/llama.cpp/ggml/src/ggml-cann/aclnn_ops.h +613 -122
  87. package/src/llama.cpp/ggml/src/ggml-cann/common.h +135 -1
  88. package/src/llama.cpp/ggml/src/ggml-cann/ggml-cann.cpp +507 -137
  89. package/src/llama.cpp/ggml/src/ggml-common.h +12 -6
  90. package/src/llama.cpp/ggml/src/ggml-cpu/CMakeLists.txt +48 -22
  91. package/src/llama.cpp/ggml/src/ggml-cpu/binary-ops.cpp +158 -0
  92. package/src/llama.cpp/ggml/src/ggml-cpu/binary-ops.h +16 -0
  93. package/src/llama.cpp/ggml/src/ggml-cpu/common.h +72 -0
  94. package/src/llama.cpp/ggml/src/ggml-cpu/cpu-feats-x86.cpp +1 -1
  95. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-aarch64.cpp +896 -192
  96. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-impl.h +2 -21
  97. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-quants.c +754 -404
  98. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.c +1003 -13519
  99. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.cpp +2 -0
  100. package/src/llama.cpp/ggml/src/ggml-cpu/kleidiai/kernels.cpp +2 -7
  101. package/src/llama.cpp/ggml/src/ggml-cpu/kleidiai/kernels.h +0 -1
  102. package/src/llama.cpp/ggml/src/ggml-cpu/kleidiai/kleidiai.cpp +3 -4
  103. package/src/llama.cpp/ggml/src/ggml-cpu/llamafile/sgemm.cpp +533 -88
  104. package/src/llama.cpp/ggml/src/ggml-cpu/ops.cpp +8809 -0
  105. package/src/llama.cpp/ggml/src/ggml-cpu/ops.h +110 -0
  106. package/src/llama.cpp/ggml/src/ggml-cpu/simd-mappings.h +892 -0
  107. package/src/llama.cpp/ggml/src/ggml-cpu/unary-ops.cpp +186 -0
  108. package/src/llama.cpp/ggml/src/ggml-cpu/unary-ops.h +28 -0
  109. package/src/llama.cpp/ggml/src/ggml-cpu/vec.cpp +258 -0
  110. package/src/llama.cpp/ggml/src/ggml-cpu/vec.h +802 -0
  111. package/src/llama.cpp/ggml/src/ggml-cuda/vendors/hip.h +7 -0
  112. package/src/llama.cpp/ggml/src/ggml-cuda/vendors/musa.h +1 -0
  113. package/src/llama.cpp/ggml/src/ggml-hip/CMakeLists.txt +0 -4
  114. package/src/llama.cpp/ggml/src/ggml-impl.h +52 -18
  115. package/src/llama.cpp/ggml/src/ggml-metal/ggml-metal-impl.h +70 -3
  116. package/src/llama.cpp/ggml/src/ggml-opencl/CMakeLists.txt +67 -119
  117. package/src/llama.cpp/ggml/src/ggml-opencl/ggml-opencl.cpp +1023 -260
  118. package/src/llama.cpp/ggml/src/ggml-rpc/ggml-rpc.cpp +293 -40
  119. package/src/llama.cpp/ggml/src/ggml-sycl/CMakeLists.txt +96 -22
  120. package/src/llama.cpp/ggml/src/ggml-sycl/backend.hpp +1 -0
  121. package/src/llama.cpp/ggml/src/ggml-sycl/binbcast.cpp +350 -0
  122. package/src/llama.cpp/ggml/src/ggml-sycl/binbcast.hpp +39 -0
  123. package/src/llama.cpp/ggml/src/ggml-sycl/common.cpp +0 -35
  124. package/src/llama.cpp/ggml/src/ggml-sycl/common.hpp +2 -292
  125. package/src/llama.cpp/ggml/src/ggml-sycl/dpct/helper.hpp +79 -90
  126. package/src/llama.cpp/ggml/src/ggml-sycl/element_wise.cpp +967 -438
  127. package/src/llama.cpp/ggml/src/ggml-sycl/element_wise.hpp +22 -23
  128. package/src/llama.cpp/ggml/src/ggml-sycl/getrows.cpp +24 -20
  129. package/src/llama.cpp/ggml/src/ggml-sycl/getrows.hpp +1 -4
  130. package/src/llama.cpp/ggml/src/ggml-sycl/ggml-sycl.cpp +204 -280
  131. package/src/llama.cpp/ggml/src/ggml-sycl/im2col.cpp +84 -74
  132. package/src/llama.cpp/ggml/src/ggml-sycl/im2col.hpp +1 -3
  133. package/src/llama.cpp/ggml/src/ggml-sycl/norm.cpp +37 -49
  134. package/src/llama.cpp/ggml/src/ggml-sycl/norm.hpp +7 -22
  135. package/src/llama.cpp/ggml/src/ggml-sycl/outprod.cpp +4 -14
  136. package/src/llama.cpp/ggml/src/ggml-sycl/rope.cpp +204 -118
  137. package/src/llama.cpp/ggml/src/ggml-sycl/rope.hpp +1 -3
  138. package/src/llama.cpp/ggml/src/ggml-vulkan/CMakeLists.txt +23 -0
  139. package/src/llama.cpp/ggml/src/ggml-vulkan/ggml-vulkan.cpp +646 -114
  140. package/src/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/CMakeLists.txt +12 -0
  141. package/src/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp +17 -8
  142. package/src/llama.cpp/ggml/src/ggml.c +141 -245
  143. package/src/llama.cpp/ggml/src/gguf.cpp +1 -0
  144. package/src/llama.cpp/include/llama.h +30 -11
  145. package/src/llama.cpp/models/ggml-vocab-llama4.gguf.inp +112 -0
  146. package/src/llama.cpp/models/ggml-vocab-llama4.gguf.out +46 -0
  147. package/src/llama.cpp/models/ggml-vocab-pixtral.gguf.inp +112 -0
  148. package/src/llama.cpp/models/ggml-vocab-pixtral.gguf.out +46 -0
  149. package/src/llama.cpp/requirements/requirements-all.txt +2 -0
  150. package/src/llama.cpp/requirements/requirements-gguf_editor_gui.txt +3 -0
  151. package/src/llama.cpp/src/CMakeLists.txt +3 -2
  152. package/src/llama.cpp/src/llama-adapter.cpp +37 -1
  153. package/src/llama.cpp/src/llama-arch.cpp +160 -17
  154. package/src/llama.cpp/src/llama-arch.h +16 -0
  155. package/src/llama.cpp/src/llama-chat.cpp +82 -17
  156. package/src/llama.cpp/src/llama-chat.h +6 -2
  157. package/src/llama.cpp/src/llama-context.cpp +108 -92
  158. package/src/llama.cpp/src/llama-context.h +1 -2
  159. package/src/llama.cpp/src/llama-graph.cpp +189 -119
  160. package/src/llama.cpp/src/llama-graph.h +26 -6
  161. package/src/llama.cpp/src/llama-hparams.h +13 -0
  162. package/src/llama.cpp/src/llama-kv-cache.cpp +70 -123
  163. package/src/llama.cpp/src/llama-kv-cache.h +41 -115
  164. package/src/llama.cpp/src/llama-memory.h +1 -1
  165. package/src/llama.cpp/src/llama-mmap.cpp +1 -1
  166. package/src/llama.cpp/src/llama-model-loader.cpp +10 -5
  167. package/src/llama.cpp/src/llama-model-loader.h +5 -3
  168. package/src/llama.cpp/src/llama-model.cpp +1760 -534
  169. package/src/llama.cpp/src/llama-model.h +13 -1
  170. package/src/llama.cpp/src/llama-quant.cpp +29 -8
  171. package/src/llama.cpp/src/llama-sampling.cpp +7 -1
  172. package/src/llama.cpp/src/llama-vocab.cpp +44 -6
  173. package/src/llama.cpp/src/llama.cpp +1 -1
  174. package/src/llama.cpp/tests/CMakeLists.txt +43 -30
  175. package/src/llama.cpp/tests/test-arg-parser.cpp +51 -4
  176. package/src/llama.cpp/tests/test-backend-ops.cpp +82 -43
  177. package/src/llama.cpp/tests/test-chat-template.cpp +34 -13
  178. package/src/llama.cpp/tests/test-chat.cpp +12 -2
  179. package/src/llama.cpp/{examples/gbnf-validator/gbnf-validator.cpp → tests/test-gbnf-validator.cpp} +2 -2
  180. package/src/llama.cpp/tests/test-grammar-integration.cpp +3 -2
  181. package/src/llama.cpp/tests/test-grammar-llguidance.cpp +63 -2
  182. package/src/llama.cpp/tests/test-grammar-parser.cpp +3 -1
  183. package/src/llama.cpp/tests/test-json-schema-to-grammar.cpp +17 -1
  184. package/src/llama.cpp/tests/test-llama-grammar.cpp +2 -1
  185. package/src/llama.cpp/{examples/quantize-stats/quantize-stats.cpp → tests/test-quantize-stats.cpp} +3 -1
  186. package/src/llama.cpp/tests/test-tokenizer-1-bpe.cpp +2 -1
  187. package/src/llama.cpp/tests/test-tokenizer-1-spm.cpp +2 -1
  188. package/src/llama.cpp/examples/gbnf-validator/CMakeLists.txt +0 -5
  189. package/src/llama.cpp/examples/llava/gemma3-cli.cpp +0 -341
  190. package/src/llama.cpp/examples/llava/llava-cli.cpp +0 -332
  191. package/src/llama.cpp/examples/llava/minicpmv-cli.cpp +0 -354
  192. package/src/llama.cpp/examples/quantize-stats/CMakeLists.txt +0 -6
  193. package/src/llama.cpp/ggml/src/ggml-cann/kernels/CMakeLists.txt +0 -30
  194. package/src/llama.cpp/ggml/src/ggml-cann/kernels/ascendc_kernels.h +0 -19
  195. package/src/llama.cpp/ggml/src/ggml-cann/kernels/dup.cpp +0 -234
  196. package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_f16.cpp +0 -197
  197. package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_f32.cpp +0 -190
  198. package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_q4_0.cpp +0 -204
  199. package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_q8_0.cpp +0 -191
  200. package/src/llama.cpp/ggml/src/ggml-cann/kernels/quantize_f16_q8_0.cpp +0 -218
  201. package/src/llama.cpp/ggml/src/ggml-cann/kernels/quantize_f32_q8_0.cpp +0 -216
  202. package/src/llama.cpp/ggml/src/ggml-cann/kernels/quantize_float_to_q4_0.cpp +0 -295
@@ -0,0 +1,344 @@
1
+ #include "arg.h"
2
+ #include "log.h"
3
+ #include "common.h"
4
+ #include "sampling.h"
5
+ #include "llama.h"
6
+ #include "ggml.h"
7
+ #include "console.h"
8
+ #include "chat.h"
9
+ #include "mtmd.h"
10
+
11
+ #include <vector>
12
+ #include <limits.h>
13
+ #include <cinttypes>
14
+
15
+ #if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
16
+ #include <signal.h>
17
+ #include <unistd.h>
18
+ #elif defined (_WIN32)
19
+ #define WIN32_LEAN_AND_MEAN
20
+ #ifndef NOMINMAX
21
+ #define NOMINMAX
22
+ #endif
23
+ #include <windows.h>
24
+ #include <signal.h>
25
+ #endif
26
+
27
+ // volatile, because of signal being an interrupt
28
+ static volatile bool g_is_generating = false;
29
+ static volatile bool g_is_interrupted = false;
30
+
31
+ /**
32
+ * Please note that this is NOT a production-ready stuff.
33
+ * It is a playground for trying multimodal support in llama.cpp.
34
+ * For contributors: please keep this code simple and easy to understand.
35
+ */
36
+
37
+ static void show_additional_info(int /*argc*/, char ** argv) {
38
+ LOG(
39
+ "Experimental CLI for multimodal\n\n"
40
+ "Usage: %s [options] -m <model> --mmproj <mmproj> --image <image> -p <prompt>\n\n"
41
+ " -m and --mmproj are required\n"
42
+ " -hf user/repo can replace both -m and --mmproj in most cases\n"
43
+ " --image and -p are optional, if NOT provided, the CLI will run in chat mode\n"
44
+ " to disable using GPU for mmproj model, add --no-mmproj-offload\n",
45
+ argv[0]
46
+ );
47
+ }
48
+
49
+ #if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)) || defined (_WIN32)
50
+ static void sigint_handler(int signo) {
51
+ if (signo == SIGINT) {
52
+ if (g_is_generating) {
53
+ g_is_generating = false;
54
+ } else {
55
+ console::cleanup();
56
+ if (g_is_interrupted) {
57
+ _exit(1);
58
+ }
59
+ g_is_interrupted = true;
60
+ }
61
+ }
62
+ }
63
+ #endif
64
+
65
+ struct mtmd_cli_context {
66
+ mtmd_context_ptr ctx_vision;
67
+ common_init_result llama_init;
68
+
69
+ llama_model * model;
70
+ llama_context * lctx;
71
+ const llama_vocab * vocab;
72
+ llama_batch batch;
73
+ int n_batch;
74
+
75
+ // note: we know that gemma3 template is "linear", meaning each turn is completely separated to another
76
+ // so here we don't need to keep track of chat history
77
+ common_chat_templates_ptr tmpls;
78
+
79
+ // support for legacy templates (models not having EOT token)
80
+ llama_tokens antiprompt_tokens;
81
+
82
+ int n_threads = 1;
83
+ llama_pos n_past = 0;
84
+
85
+ mtmd_cli_context(common_params & params) : llama_init(common_init_from_params(params)) {
86
+ model = llama_init.model.get();
87
+ lctx = llama_init.context.get();
88
+ vocab = llama_model_get_vocab(model);
89
+ n_threads = params.cpuparams.n_threads;
90
+ batch = llama_batch_init(params.n_batch, 0, 1);
91
+ n_batch = params.n_batch;
92
+
93
+ if (!llama_model_chat_template(model, nullptr) && params.chat_template.empty()) {
94
+ LOG_ERR("Model does not have chat template.\n");
95
+ LOG_ERR(" For old llava models, you may need to use '--chat-template vicuna'\n");
96
+ LOG_ERR(" For MobileVLM models, use '--chat-template deepseek'\n");
97
+ exit(1);
98
+ }
99
+
100
+ tmpls = common_chat_templates_init(model, params.chat_template);
101
+ LOG_INF("%s: chat template example:\n%s\n", __func__, common_chat_format_example(tmpls.get(), params.use_jinja).c_str());
102
+
103
+ init_vision_context(params);
104
+
105
+ // load antiprompt tokens for legacy templates
106
+ if (params.chat_template == "vicuna") {
107
+ antiprompt_tokens = common_tokenize(lctx, "ASSISTANT:", false, true);
108
+ } else if (params.chat_template == "deepseek") {
109
+ antiprompt_tokens = common_tokenize(lctx, "###", false, true);
110
+ }
111
+ }
112
+
113
+ void init_vision_context(common_params & params) {
114
+ const char * clip_path = params.mmproj.path.c_str();
115
+ ctx_vision.reset(mtmd_init_from_file(clip_path, model, mtmd_context_params{
116
+ /* use_gpu */ params.mmproj_use_gpu,
117
+ /* timings */ true,
118
+ /* n_threads */ params.cpuparams.n_threads,
119
+ /* verbosity */ params.verbosity > 0 ? GGML_LOG_LEVEL_DEBUG : GGML_LOG_LEVEL_INFO,
120
+ }));
121
+ if (!ctx_vision.get()) {
122
+ LOG_ERR("Failed to load vision model from %s\n", clip_path);
123
+ exit(1);
124
+ }
125
+ }
126
+
127
+ bool check_antiprompt(const llama_tokens & generated_tokens) {
128
+ if (antiprompt_tokens.empty() || generated_tokens.size() < antiprompt_tokens.size()) {
129
+ return false;
130
+ }
131
+ return std::equal(
132
+ generated_tokens.end() - antiprompt_tokens.size(),
133
+ generated_tokens.end(),
134
+ antiprompt_tokens.begin()
135
+ );
136
+ }
137
+ };
138
+
139
+ static int generate_response(mtmd_cli_context & ctx, common_sampler * smpl, int n_predict) {
140
+ llama_tokens generated_tokens;
141
+ for (int i = 0; i < n_predict; i++) {
142
+ if (i > n_predict || !g_is_generating || g_is_interrupted) {
143
+ printf("\n");
144
+ break;
145
+ }
146
+
147
+ llama_token token_id = common_sampler_sample(smpl, ctx.lctx, -1);
148
+ generated_tokens.push_back(token_id);
149
+ common_sampler_accept(smpl, token_id, true);
150
+
151
+ if (llama_vocab_is_eog(ctx.vocab, token_id) || ctx.check_antiprompt(generated_tokens)) {
152
+ printf("\n");
153
+ break; // end of generation
154
+ }
155
+
156
+ printf("%s", common_token_to_piece(ctx.lctx, token_id).c_str());
157
+ fflush(stdout);
158
+
159
+ if (g_is_interrupted) {
160
+ printf("\n");
161
+ break;
162
+ }
163
+
164
+ // eval the token
165
+ common_batch_clear(ctx.batch);
166
+ common_batch_add(ctx.batch, token_id, ctx.n_past++, {0}, true);
167
+ if (llama_decode(ctx.lctx, ctx.batch)) {
168
+ LOG_ERR("failed to decode token\n");
169
+ return 1;
170
+ }
171
+ }
172
+ return 0;
173
+ }
174
+
175
+ static int eval_message(mtmd_cli_context & ctx, common_chat_msg & msg, std::vector<std::string> & images_fname, bool add_bos = false) {
176
+ std::vector<mtmd_bitmap> bitmaps;
177
+
178
+ common_chat_templates_inputs tmpl_inputs;
179
+ tmpl_inputs.messages = {msg};
180
+ tmpl_inputs.add_generation_prompt = true;
181
+ tmpl_inputs.use_jinja = false; // jinja is buggy here
182
+ auto formatted_chat = common_chat_templates_apply(ctx.tmpls.get(), tmpl_inputs);
183
+ LOG_DBG("formatted_chat.prompt: %s\n", formatted_chat.prompt.c_str());
184
+
185
+ for (auto & fname : images_fname) {
186
+ mtmd_bitmap bitmap;
187
+ if (mtmd_helper_bitmap_init_from_file(fname.c_str(), bitmap)) {
188
+ LOG_ERR("Unable to load image %s\n", fname.c_str());
189
+ return 2; // image not found
190
+ }
191
+ bitmaps.push_back(std::move(bitmap));
192
+ }
193
+
194
+ mtmd_input_text text;
195
+ text.text = formatted_chat.prompt;
196
+ text.add_special = add_bos;
197
+ text.parse_special = true;
198
+ mtmd_input_chunks chunks;
199
+
200
+ if (g_is_interrupted) return 0;
201
+
202
+ int32_t res = mtmd_tokenize(ctx.ctx_vision.get(), chunks, text, bitmaps);
203
+ if (res != 0) {
204
+ LOG_ERR("Unable to tokenize prompt, res = %d\n", res);
205
+ return 1;
206
+ }
207
+
208
+ if (mtmd_helper_eval(ctx.ctx_vision.get(), ctx.lctx, chunks, ctx.n_past, 0, ctx.n_batch)) {
209
+ LOG_ERR("Unable to eval prompt\n");
210
+ return 1;
211
+ }
212
+
213
+ ctx.n_past += mtmd_helper_get_n_pos(chunks);
214
+
215
+ return 0;
216
+ }
217
+
218
+ int main(int argc, char ** argv) {
219
+ ggml_time_init();
220
+
221
+ common_params params;
222
+ params.sampling.temp = 0.2; // lower temp by default for better quality
223
+
224
+ if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_LLAVA, show_additional_info)) {
225
+ return 1;
226
+ }
227
+
228
+ common_init();
229
+
230
+ if (params.mmproj.path.empty()) {
231
+ show_additional_info(argc, argv);
232
+ LOG_ERR("ERR: Missing --mmproj argument\n");
233
+ return 1;
234
+ }
235
+
236
+ mtmd_cli_context ctx(params);
237
+ printf("%s: %s\n", __func__, params.model.path.c_str());
238
+
239
+ bool is_single_turn = !params.prompt.empty() && !params.image.empty();
240
+
241
+ struct common_sampler * smpl = common_sampler_init(ctx.model, params.sampling);
242
+ int n_predict = params.n_predict < 0 ? INT_MAX : params.n_predict;
243
+
244
+ // ctrl+C handling
245
+ {
246
+ #if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
247
+ struct sigaction sigint_action;
248
+ sigint_action.sa_handler = sigint_handler;
249
+ sigemptyset (&sigint_action.sa_mask);
250
+ sigint_action.sa_flags = 0;
251
+ sigaction(SIGINT, &sigint_action, NULL);
252
+ #elif defined (_WIN32)
253
+ auto console_ctrl_handler = +[](DWORD ctrl_type) -> BOOL {
254
+ return (ctrl_type == CTRL_C_EVENT) ? (sigint_handler(SIGINT), true) : false;
255
+ };
256
+ SetConsoleCtrlHandler(reinterpret_cast<PHANDLER_ROUTINE>(console_ctrl_handler), true);
257
+ #endif
258
+ }
259
+
260
+ if (g_is_interrupted) return 130;
261
+
262
+ if (is_single_turn) {
263
+ g_is_generating = true;
264
+ if (params.prompt.find("<__image__>") == std::string::npos) {
265
+ params.prompt += " <__image__>";
266
+ }
267
+ common_chat_msg msg;
268
+ msg.role = "user";
269
+ msg.content = params.prompt;
270
+ if (eval_message(ctx, msg, params.image, true)) {
271
+ return 1;
272
+ }
273
+ if (!g_is_interrupted && generate_response(ctx, smpl, n_predict)) {
274
+ return 1;
275
+ }
276
+
277
+ } else {
278
+ LOG("\n Running in chat mode, available commands:");
279
+ LOG("\n /image <path> load an image");
280
+ LOG("\n /clear clear the chat history");
281
+ LOG("\n /quit or /exit exit the program");
282
+ LOG("\n");
283
+
284
+ bool is_first_msg = true;
285
+ std::vector<std::string> images_fname;
286
+ std::string content;
287
+
288
+ while (!g_is_interrupted) {
289
+ g_is_generating = false;
290
+ LOG("\n> ");
291
+ console::set_display(console::user_input);
292
+ std::string line;
293
+ console::readline(line, false);
294
+ if (g_is_interrupted) break;
295
+ console::set_display(console::reset);
296
+ line = string_strip(line);
297
+ if (line.empty()) {
298
+ continue;
299
+ }
300
+ if (line == "/quit" || line == "/exit") {
301
+ break;
302
+ }
303
+ if (line == "/clear") {
304
+ ctx.n_past = 0;
305
+ llama_kv_self_seq_rm(ctx.lctx, 0, 1, -1); // keep BOS
306
+ LOG("Chat history cleared\n\n");
307
+ continue;
308
+ }
309
+ g_is_generating = true;
310
+ if (line.find("/image") == 0) {
311
+ std::string image = line.substr(7);
312
+ images_fname.push_back(string_strip(image));
313
+ content += "<__image__>";
314
+ continue;
315
+ } else {
316
+ content += line;
317
+ }
318
+ common_chat_msg msg;
319
+ msg.role = "user";
320
+ msg.content = content;
321
+ int ret = eval_message(ctx, msg, images_fname, is_first_msg);
322
+ if (g_is_interrupted) break;
323
+ if (ret == 2) {
324
+ // non-fatal error
325
+ images_fname.clear();
326
+ content.clear();
327
+ continue;
328
+ }
329
+ if (ret) {
330
+ return 1;
331
+ }
332
+ if (generate_response(ctx, smpl, n_predict)) {
333
+ return 1;
334
+ }
335
+ images_fname.clear();
336
+ content.clear();
337
+ is_first_msg = false;
338
+ }
339
+ }
340
+ if (g_is_interrupted) LOG("\nInterrupted by user\n");
341
+ LOG("\n\n");
342
+ llama_perf_context_print(ctx.lctx);
343
+ return g_is_interrupted ? 130 : 0;
344
+ }