@fugood/llama.node 0.3.15 → 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.
- package/CMakeLists.txt +3 -0
- package/bin/darwin/arm64/llama-node.node +0 -0
- package/bin/darwin/x64/llama-node.node +0 -0
- package/bin/linux/arm64/llama-node.node +0 -0
- package/bin/linux/x64/llama-node.node +0 -0
- package/bin/linux-cuda/arm64/llama-node.node +0 -0
- package/bin/linux-cuda/x64/llama-node.node +0 -0
- package/bin/linux-vulkan/arm64/llama-node.node +0 -0
- package/bin/linux-vulkan/x64/llama-node.node +0 -0
- package/bin/win32/arm64/llama-node.node +0 -0
- package/bin/win32/arm64/node.lib +0 -0
- package/bin/win32/x64/llama-node.node +0 -0
- package/bin/win32/x64/node.lib +0 -0
- package/bin/win32-vulkan/arm64/llama-node.node +0 -0
- package/bin/win32-vulkan/arm64/node.lib +0 -0
- package/bin/win32-vulkan/x64/llama-node.node +0 -0
- package/bin/win32-vulkan/x64/node.lib +0 -0
- package/lib/binding.ts +5 -0
- package/package.json +1 -1
- package/src/LlamaCompletionWorker.cpp +8 -0
- package/src/LlamaCompletionWorker.h +1 -0
- package/src/LlamaContext.cpp +3 -2
- package/src/llama.cpp/.github/workflows/build-linux-cross.yml +124 -0
- package/src/llama.cpp/.github/workflows/build.yml +70 -27
- package/src/llama.cpp/.github/workflows/docker.yml +6 -6
- package/src/llama.cpp/.github/workflows/server.yml +7 -11
- package/src/llama.cpp/CMakeLists.txt +23 -1
- package/src/llama.cpp/common/CMakeLists.txt +6 -3
- package/src/llama.cpp/common/arg.cpp +809 -105
- package/src/llama.cpp/common/arg.h +9 -0
- package/src/llama.cpp/common/chat.cpp +1 -1
- package/src/llama.cpp/common/common.cpp +31 -521
- package/src/llama.cpp/common/common.h +17 -36
- package/src/llama.cpp/common/json-schema-to-grammar.cpp +3 -0
- package/src/llama.cpp/common/llguidance.cpp +30 -47
- package/src/llama.cpp/common/minja/chat-template.hpp +15 -7
- package/src/llama.cpp/common/minja/minja.hpp +119 -93
- package/src/llama.cpp/common/sampling.cpp +3 -0
- package/src/llama.cpp/docs/build.md +122 -7
- package/src/llama.cpp/examples/CMakeLists.txt +0 -9
- package/src/llama.cpp/examples/batched/batched.cpp +1 -1
- package/src/llama.cpp/examples/batched-bench/batched-bench.cpp +1 -1
- package/src/llama.cpp/examples/embedding/embedding.cpp +7 -1
- package/src/llama.cpp/examples/export-lora/export-lora.cpp +1 -1
- package/src/llama.cpp/examples/gguf-split/gguf-split.cpp +15 -16
- package/src/llama.cpp/examples/gritlm/gritlm.cpp +1 -1
- package/src/llama.cpp/examples/llama-bench/llama-bench.cpp +210 -8
- package/src/llama.cpp/examples/llama.android/llama/build.gradle.kts +1 -0
- package/src/llama.cpp/examples/llava/CMakeLists.txt +39 -24
- package/src/llama.cpp/examples/llava/clip-impl.h +345 -0
- package/src/llama.cpp/examples/llava/clip.cpp +2152 -1803
- package/src/llama.cpp/examples/llava/clip.h +39 -22
- package/src/llama.cpp/examples/llava/deprecation-warning.cpp +22 -0
- package/src/llama.cpp/examples/llava/llava.cpp +64 -52
- package/src/llama.cpp/examples/llava/mtmd-cli.cpp +344 -0
- package/src/llama.cpp/examples/llava/mtmd.cpp +708 -0
- package/src/llama.cpp/examples/llava/mtmd.h +168 -0
- package/src/llama.cpp/examples/llava/{qwen2vl-cli.cpp → qwen2vl-test.cpp} +83 -31
- package/src/llama.cpp/examples/main/main.cpp +16 -5
- package/src/llama.cpp/examples/parallel/parallel.cpp +3 -1
- package/src/llama.cpp/examples/passkey/passkey.cpp +1 -1
- package/src/llama.cpp/examples/perplexity/perplexity.cpp +17 -3
- package/src/llama.cpp/examples/quantize/quantize.cpp +115 -2
- package/src/llama.cpp/examples/rpc/CMakeLists.txt +4 -2
- package/src/llama.cpp/examples/rpc/rpc-server.cpp +163 -8
- package/src/llama.cpp/examples/run/CMakeLists.txt +12 -1
- package/src/llama.cpp/examples/run/run.cpp +14 -28
- package/src/llama.cpp/examples/server/httplib.h +313 -247
- package/src/llama.cpp/examples/server/server.cpp +243 -139
- package/src/llama.cpp/examples/server/utils.hpp +51 -2
- package/src/llama.cpp/examples/speculative/speculative.cpp +1 -1
- package/src/llama.cpp/examples/speculative-simple/speculative-simple.cpp +1 -1
- package/src/llama.cpp/examples/sycl/build.sh +2 -2
- package/src/llama.cpp/examples/sycl/win-build-sycl.bat +2 -2
- package/src/llama.cpp/examples/tts/tts.cpp +14 -9
- package/src/llama.cpp/ggml/CMakeLists.txt +8 -2
- package/src/llama.cpp/ggml/cmake/GitVars.cmake +22 -0
- package/src/llama.cpp/ggml/include/ggml-cpu.h +5 -0
- package/src/llama.cpp/ggml/include/ggml-rpc.h +6 -1
- package/src/llama.cpp/ggml/include/ggml.h +66 -99
- package/src/llama.cpp/ggml/src/CMakeLists.txt +15 -8
- package/src/llama.cpp/ggml/src/ggml-cann/CMakeLists.txt +0 -2
- package/src/llama.cpp/ggml/src/ggml-cann/acl_tensor.cpp +8 -4
- package/src/llama.cpp/ggml/src/ggml-cann/acl_tensor.h +5 -5
- package/src/llama.cpp/ggml/src/ggml-cann/aclnn_ops.cpp +692 -1534
- package/src/llama.cpp/ggml/src/ggml-cann/aclnn_ops.h +613 -122
- package/src/llama.cpp/ggml/src/ggml-cann/common.h +135 -1
- package/src/llama.cpp/ggml/src/ggml-cann/ggml-cann.cpp +507 -137
- package/src/llama.cpp/ggml/src/ggml-common.h +12 -6
- package/src/llama.cpp/ggml/src/ggml-cpu/CMakeLists.txt +48 -22
- package/src/llama.cpp/ggml/src/ggml-cpu/binary-ops.cpp +158 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/binary-ops.h +16 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/common.h +72 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/cpu-feats-x86.cpp +1 -1
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-aarch64.cpp +2413 -228
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-impl.h +2 -21
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-quants.c +754 -404
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.c +1004 -13516
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.cpp +2 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/kleidiai/kernels.cpp +2 -7
- package/src/llama.cpp/ggml/src/ggml-cpu/kleidiai/kernels.h +0 -1
- package/src/llama.cpp/ggml/src/ggml-cpu/kleidiai/kleidiai.cpp +3 -4
- package/src/llama.cpp/ggml/src/ggml-cpu/llamafile/sgemm.cpp +533 -88
- package/src/llama.cpp/ggml/src/ggml-cpu/ops.cpp +8809 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/ops.h +110 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/simd-mappings.h +892 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/unary-ops.cpp +186 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/unary-ops.h +28 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/vec.cpp +258 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/vec.h +802 -0
- package/src/llama.cpp/ggml/src/ggml-cuda/vendors/hip.h +7 -0
- package/src/llama.cpp/ggml/src/ggml-cuda/vendors/musa.h +1 -0
- package/src/llama.cpp/ggml/src/ggml-hip/CMakeLists.txt +0 -4
- package/src/llama.cpp/ggml/src/ggml-impl.h +52 -18
- package/src/llama.cpp/ggml/src/ggml-metal/ggml-metal-impl.h +70 -3
- package/src/llama.cpp/ggml/src/ggml-opencl/CMakeLists.txt +67 -119
- package/src/llama.cpp/ggml/src/ggml-opencl/ggml-opencl.cpp +1023 -260
- package/src/llama.cpp/ggml/src/ggml-rpc/ggml-rpc.cpp +293 -40
- package/src/llama.cpp/ggml/src/ggml-sycl/CMakeLists.txt +127 -33
- package/src/llama.cpp/ggml/src/ggml-sycl/backend.hpp +1 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/binbcast.cpp +350 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/binbcast.hpp +39 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/common.cpp +0 -35
- package/src/llama.cpp/ggml/src/ggml-sycl/common.hpp +29 -293
- package/src/llama.cpp/ggml/src/ggml-sycl/dpct/helper.hpp +79 -90
- package/src/llama.cpp/ggml/src/ggml-sycl/element_wise.cpp +967 -438
- package/src/llama.cpp/ggml/src/ggml-sycl/element_wise.hpp +22 -23
- package/src/llama.cpp/ggml/src/ggml-sycl/gemm.hpp +12 -43
- package/src/llama.cpp/ggml/src/ggml-sycl/getrows.cpp +24 -20
- package/src/llama.cpp/ggml/src/ggml-sycl/getrows.hpp +1 -4
- package/src/llama.cpp/ggml/src/ggml-sycl/ggml-sycl.cpp +210 -286
- package/src/llama.cpp/ggml/src/ggml-sycl/im2col.cpp +84 -74
- package/src/llama.cpp/ggml/src/ggml-sycl/im2col.hpp +1 -3
- package/src/llama.cpp/ggml/src/ggml-sycl/norm.cpp +37 -49
- package/src/llama.cpp/ggml/src/ggml-sycl/norm.hpp +7 -22
- package/src/llama.cpp/ggml/src/ggml-sycl/outprod.cpp +4 -14
- package/src/llama.cpp/ggml/src/ggml-sycl/rope.cpp +204 -118
- package/src/llama.cpp/ggml/src/ggml-sycl/rope.hpp +1 -3
- package/src/llama.cpp/ggml/src/ggml-vulkan/CMakeLists.txt +23 -0
- package/src/llama.cpp/ggml/src/ggml-vulkan/ggml-vulkan.cpp +692 -126
- package/src/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/CMakeLists.txt +12 -0
- package/src/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp +21 -10
- package/src/llama.cpp/ggml/src/ggml.c +141 -245
- package/src/llama.cpp/ggml/src/gguf.cpp +1 -0
- package/src/llama.cpp/include/llama.h +30 -11
- package/src/llama.cpp/models/ggml-vocab-llama4.gguf.inp +112 -0
- package/src/llama.cpp/models/ggml-vocab-llama4.gguf.out +46 -0
- package/src/llama.cpp/models/ggml-vocab-pixtral.gguf.inp +112 -0
- package/src/llama.cpp/models/ggml-vocab-pixtral.gguf.out +46 -0
- package/src/llama.cpp/requirements/requirements-all.txt +2 -0
- package/src/llama.cpp/requirements/requirements-gguf_editor_gui.txt +3 -0
- package/src/llama.cpp/src/CMakeLists.txt +3 -2
- package/src/llama.cpp/src/llama-adapter.cpp +37 -1
- package/src/llama.cpp/src/llama-arch.cpp +161 -17
- package/src/llama.cpp/src/llama-arch.h +16 -0
- package/src/llama.cpp/src/llama-chat.cpp +82 -17
- package/src/llama.cpp/src/llama-chat.h +6 -2
- package/src/llama.cpp/src/llama-context.cpp +108 -92
- package/src/llama.cpp/src/llama-context.h +1 -2
- package/src/llama.cpp/src/llama-graph.cpp +189 -119
- package/src/llama.cpp/src/llama-graph.h +26 -6
- package/src/llama.cpp/src/llama-hparams.h +13 -0
- package/src/llama.cpp/src/llama-kv-cache.cpp +70 -123
- package/src/llama.cpp/src/llama-kv-cache.h +41 -115
- package/src/llama.cpp/src/llama-memory.h +1 -1
- package/src/llama.cpp/src/llama-mmap.cpp +1 -1
- package/src/llama.cpp/src/llama-model-loader.cpp +10 -5
- package/src/llama.cpp/src/llama-model-loader.h +5 -3
- package/src/llama.cpp/src/llama-model.cpp +1544 -291
- package/src/llama.cpp/src/llama-model.h +13 -1
- package/src/llama.cpp/src/llama-quant.cpp +29 -8
- package/src/llama.cpp/src/llama-sampling.cpp +7 -1
- package/src/llama.cpp/src/llama-vocab.cpp +44 -6
- package/src/llama.cpp/src/llama.cpp +1 -1
- package/src/llama.cpp/tests/CMakeLists.txt +43 -30
- package/src/llama.cpp/tests/test-arg-parser.cpp +51 -4
- package/src/llama.cpp/tests/test-backend-ops.cpp +139 -57
- package/src/llama.cpp/tests/test-chat-template.cpp +34 -13
- package/src/llama.cpp/tests/test-chat.cpp +12 -2
- package/src/llama.cpp/{examples/gbnf-validator/gbnf-validator.cpp → tests/test-gbnf-validator.cpp} +2 -2
- package/src/llama.cpp/tests/test-grammar-integration.cpp +3 -2
- package/src/llama.cpp/tests/test-grammar-llguidance.cpp +63 -2
- package/src/llama.cpp/tests/test-grammar-parser.cpp +3 -1
- package/src/llama.cpp/tests/test-json-schema-to-grammar.cpp +17 -1
- package/src/llama.cpp/tests/test-llama-grammar.cpp +2 -1
- package/src/llama.cpp/{examples/quantize-stats/quantize-stats.cpp → tests/test-quantize-stats.cpp} +3 -1
- package/src/llama.cpp/tests/test-tokenizer-1-bpe.cpp +2 -1
- package/src/llama.cpp/tests/test-tokenizer-1-spm.cpp +2 -1
- package/src/llama.cpp/examples/gbnf-validator/CMakeLists.txt +0 -5
- package/src/llama.cpp/examples/llava/gemma3-cli.cpp +0 -341
- package/src/llama.cpp/examples/llava/llava-cli.cpp +0 -332
- package/src/llama.cpp/examples/llava/minicpmv-cli.cpp +0 -354
- package/src/llama.cpp/examples/quantize-stats/CMakeLists.txt +0 -6
- package/src/llama.cpp/ggml/src/ggml-cann/kernels/CMakeLists.txt +0 -30
- package/src/llama.cpp/ggml/src/ggml-cann/kernels/ascendc_kernels.h +0 -19
- package/src/llama.cpp/ggml/src/ggml-cann/kernels/dup.cpp +0 -234
- package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_f16.cpp +0 -197
- package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_f32.cpp +0 -190
- package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_q4_0.cpp +0 -204
- package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_q8_0.cpp +0 -191
- package/src/llama.cpp/ggml/src/ggml-cann/kernels/quantize_f16_q8_0.cpp +0 -218
- package/src/llama.cpp/ggml/src/ggml-cann/kernels/quantize_f32_q8_0.cpp +0 -216
- 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
|
+
}
|