@fugood/llama.node 0.3.17 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CMakeLists.txt +3 -1
- 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 +39 -2
- package/lib/index.js +132 -1
- package/lib/index.ts +203 -3
- package/package.json +2 -1
- package/src/EmbeddingWorker.cpp +1 -1
- package/src/LlamaCompletionWorker.cpp +366 -19
- package/src/LlamaCompletionWorker.h +30 -10
- package/src/LlamaContext.cpp +213 -5
- package/src/LlamaContext.h +12 -0
- package/src/common.hpp +15 -0
- package/src/llama.cpp/.github/workflows/build-linux-cross.yml +133 -24
- package/src/llama.cpp/.github/workflows/build.yml +41 -762
- package/src/llama.cpp/.github/workflows/docker.yml +5 -2
- package/src/llama.cpp/.github/workflows/release.yml +716 -0
- package/src/llama.cpp/.github/workflows/server.yml +12 -12
- package/src/llama.cpp/CMakeLists.txt +5 -17
- package/src/llama.cpp/cmake/build-info.cmake +8 -2
- package/src/llama.cpp/cmake/x64-windows-llvm.cmake +0 -6
- package/src/llama.cpp/common/CMakeLists.txt +31 -3
- package/src/llama.cpp/common/arg.cpp +48 -29
- package/src/llama.cpp/common/chat.cpp +128 -106
- package/src/llama.cpp/common/chat.h +2 -0
- package/src/llama.cpp/common/common.cpp +37 -1
- package/src/llama.cpp/common/common.h +18 -9
- package/src/llama.cpp/common/llguidance.cpp +1 -0
- package/src/llama.cpp/common/minja/chat-template.hpp +9 -5
- package/src/llama.cpp/common/minja/minja.hpp +69 -36
- package/src/llama.cpp/common/regex-partial.cpp +204 -0
- package/src/llama.cpp/common/regex-partial.h +56 -0
- package/src/llama.cpp/common/sampling.cpp +57 -50
- package/src/llama.cpp/examples/CMakeLists.txt +2 -23
- package/src/llama.cpp/examples/embedding/embedding.cpp +2 -11
- package/src/llama.cpp/examples/parallel/parallel.cpp +86 -14
- package/src/llama.cpp/examples/training/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/training/finetune.cpp +96 -0
- package/src/llama.cpp/ggml/CMakeLists.txt +27 -0
- package/src/llama.cpp/ggml/include/ggml-backend.h +4 -4
- package/src/llama.cpp/ggml/include/ggml-cpp.h +1 -1
- package/src/llama.cpp/ggml/include/ggml-opt.h +47 -28
- package/src/llama.cpp/ggml/include/ggml.h +10 -7
- package/src/llama.cpp/ggml/src/CMakeLists.txt +1 -1
- package/src/llama.cpp/ggml/src/ggml-alloc.c +4 -1
- package/src/llama.cpp/ggml/src/ggml-backend.cpp +9 -5
- package/src/llama.cpp/ggml/src/ggml-cpu/CMakeLists.txt +20 -13
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-aarch64.cpp +0 -2
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-quants.c +306 -6
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.c +4 -13
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.cpp +29 -16
- package/src/llama.cpp/ggml/src/ggml-cpu/kleidiai/kernels.cpp +88 -5
- package/src/llama.cpp/ggml/src/ggml-cpu/kleidiai/kernels.h +47 -12
- package/src/llama.cpp/ggml/src/ggml-cpu/kleidiai/kleidiai.cpp +264 -69
- package/src/llama.cpp/ggml/src/ggml-cpu/llamafile/sgemm.cpp +501 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/ops.cpp +0 -13
- package/src/llama.cpp/ggml/src/ggml-cpu/vec.cpp +0 -6
- package/src/llama.cpp/ggml/src/ggml-cuda/CMakeLists.txt +23 -4
- package/src/llama.cpp/ggml/src/ggml-metal/ggml-metal-impl.h +36 -11
- package/src/llama.cpp/ggml/src/ggml-opencl/ggml-opencl.cpp +0 -2
- package/src/llama.cpp/ggml/src/ggml-opt.cpp +368 -190
- package/src/llama.cpp/ggml/src/ggml-quants.c +0 -6
- package/src/llama.cpp/ggml/src/ggml-rpc/ggml-rpc.cpp +41 -27
- package/src/llama.cpp/ggml/src/ggml-sycl/CMakeLists.txt +29 -23
- package/src/llama.cpp/ggml/src/ggml-sycl/backend.hpp +9 -8
- package/src/llama.cpp/ggml/src/ggml-sycl/binbcast.cpp +121 -232
- package/src/llama.cpp/ggml/src/ggml-sycl/common.hpp +7 -15
- package/src/llama.cpp/ggml/src/ggml-sycl/convert.cpp +72 -25
- package/src/llama.cpp/ggml/src/ggml-sycl/convert.hpp +14 -7
- package/src/llama.cpp/ggml/src/ggml-sycl/dequantize.hpp +59 -21
- package/src/llama.cpp/ggml/src/ggml-sycl/dmmv.cpp +7 -1
- package/src/llama.cpp/ggml/src/ggml-sycl/element_wise.cpp +0 -23
- package/src/llama.cpp/ggml/src/ggml-sycl/gemm.hpp +37 -8
- package/src/llama.cpp/ggml/src/ggml-sycl/ggml-sycl.cpp +338 -166
- package/src/llama.cpp/ggml/src/ggml-sycl/mmvq.cpp +185 -89
- package/src/llama.cpp/ggml/src/ggml-sycl/quants.hpp +83 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/vecdotq.hpp +128 -53
- package/src/llama.cpp/ggml/src/ggml-vulkan/CMakeLists.txt +81 -70
- package/src/llama.cpp/ggml/src/ggml-vulkan/ggml-vulkan.cpp +657 -193
- package/src/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/CMakeLists.txt +20 -0
- package/src/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp +123 -29
- package/src/llama.cpp/ggml/src/ggml.c +29 -20
- package/src/llama.cpp/ggml/src/gguf.cpp +33 -33
- package/src/llama.cpp/include/llama.h +52 -11
- package/src/llama.cpp/requirements/requirements-all.txt +3 -3
- package/src/llama.cpp/scripts/xxd.cmake +1 -1
- package/src/llama.cpp/src/CMakeLists.txt +1 -0
- package/src/llama.cpp/src/llama-adapter.cpp +6 -0
- package/src/llama.cpp/src/llama-arch.cpp +3 -0
- package/src/llama.cpp/src/llama-batch.cpp +5 -1
- package/src/llama.cpp/src/llama-batch.h +2 -1
- package/src/llama.cpp/src/llama-chat.cpp +17 -7
- package/src/llama.cpp/src/llama-chat.h +1 -0
- package/src/llama.cpp/src/llama-context.cpp +389 -501
- package/src/llama.cpp/src/llama-context.h +44 -32
- package/src/llama.cpp/src/llama-cparams.h +1 -0
- package/src/llama.cpp/src/llama-graph.cpp +20 -38
- package/src/llama.cpp/src/llama-graph.h +12 -8
- package/src/llama.cpp/src/llama-kv-cache.cpp +1503 -389
- package/src/llama.cpp/src/llama-kv-cache.h +271 -85
- package/src/llama.cpp/src/llama-memory.h +11 -1
- package/src/llama.cpp/src/llama-model-loader.cpp +24 -15
- package/src/llama.cpp/src/llama-model-saver.cpp +281 -0
- package/src/llama.cpp/src/llama-model-saver.h +37 -0
- package/src/llama.cpp/src/llama-model.cpp +316 -69
- package/src/llama.cpp/src/llama-model.h +8 -1
- package/src/llama.cpp/src/llama-quant.cpp +15 -13
- package/src/llama.cpp/src/llama-sampling.cpp +18 -6
- package/src/llama.cpp/src/llama-vocab.cpp +42 -4
- package/src/llama.cpp/src/llama-vocab.h +6 -0
- package/src/llama.cpp/src/llama.cpp +14 -0
- package/src/llama.cpp/tests/CMakeLists.txt +10 -2
- package/src/llama.cpp/tests/test-backend-ops.cpp +107 -47
- package/src/llama.cpp/tests/test-chat-template.cpp +10 -11
- package/src/llama.cpp/tests/test-chat.cpp +3 -1
- package/src/llama.cpp/tests/test-mtmd-c-api.c +63 -0
- package/src/llama.cpp/tests/test-opt.cpp +33 -21
- package/src/llama.cpp/tests/test-regex-partial.cpp +288 -0
- package/src/llama.cpp/tests/test-sampling.cpp +1 -1
- package/src/llama.cpp/tools/CMakeLists.txt +39 -0
- package/src/llama.cpp/{examples → tools}/batched-bench/batched-bench.cpp +2 -2
- package/src/llama.cpp/{examples → tools}/imatrix/imatrix.cpp +11 -9
- package/src/llama.cpp/{examples → tools}/llama-bench/llama-bench.cpp +495 -348
- package/src/llama.cpp/{examples → tools}/main/main.cpp +6 -9
- package/src/llama.cpp/{examples/llava → tools/mtmd}/CMakeLists.txt +1 -35
- package/src/llama.cpp/{examples/llava → tools/mtmd}/clip-impl.h +25 -5
- package/src/llama.cpp/{examples/llava → tools/mtmd}/clip.cpp +1440 -1349
- package/src/llama.cpp/tools/mtmd/clip.h +99 -0
- package/src/llama.cpp/{examples/llava → tools/mtmd}/mtmd-cli.cpp +70 -44
- package/src/llama.cpp/tools/mtmd/mtmd-helper.cpp +310 -0
- package/src/llama.cpp/{examples/llava → tools/mtmd}/mtmd.cpp +251 -281
- package/src/llama.cpp/tools/mtmd/mtmd.h +331 -0
- package/src/llama.cpp/{examples → tools}/perplexity/perplexity.cpp +4 -2
- package/src/llama.cpp/{examples → tools}/quantize/quantize.cpp +13 -76
- package/src/llama.cpp/{examples → tools}/rpc/rpc-server.cpp +70 -74
- package/src/llama.cpp/{examples → tools}/run/run.cpp +18 -4
- package/src/llama.cpp/{examples → tools}/server/CMakeLists.txt +2 -1
- package/src/llama.cpp/{examples → tools}/server/server.cpp +291 -76
- package/src/llama.cpp/{examples → tools}/server/utils.hpp +377 -5
- package/src/llama.cpp/cmake/arm64-windows-msvc.cmake +0 -6
- package/src/llama.cpp/examples/infill/CMakeLists.txt +0 -5
- package/src/llama.cpp/examples/infill/infill.cpp +0 -590
- package/src/llama.cpp/examples/llava/android/build_64.sh +0 -8
- package/src/llama.cpp/examples/llava/clip-quantize-cli.cpp +0 -59
- package/src/llama.cpp/examples/llava/clip.h +0 -135
- package/src/llama.cpp/examples/llava/llava.cpp +0 -586
- package/src/llama.cpp/examples/llava/llava.h +0 -49
- package/src/llama.cpp/examples/llava/mtmd.h +0 -168
- package/src/llama.cpp/examples/llava/qwen2vl-test.cpp +0 -636
- /package/src/llama.cpp/{examples → tools}/batched-bench/CMakeLists.txt +0 -0
- /package/src/llama.cpp/{examples → tools}/cvector-generator/CMakeLists.txt +0 -0
- /package/src/llama.cpp/{examples → tools}/cvector-generator/completions.txt +0 -0
- /package/src/llama.cpp/{examples → tools}/cvector-generator/cvector-generator.cpp +0 -0
- /package/src/llama.cpp/{examples → tools}/cvector-generator/mean.hpp +0 -0
- /package/src/llama.cpp/{examples → tools}/cvector-generator/negative.txt +0 -0
- /package/src/llama.cpp/{examples → tools}/cvector-generator/pca.hpp +0 -0
- /package/src/llama.cpp/{examples → tools}/cvector-generator/positive.txt +0 -0
- /package/src/llama.cpp/{examples → tools}/export-lora/CMakeLists.txt +0 -0
- /package/src/llama.cpp/{examples → tools}/export-lora/export-lora.cpp +0 -0
- /package/src/llama.cpp/{examples → tools}/gguf-split/CMakeLists.txt +0 -0
- /package/src/llama.cpp/{examples → tools}/gguf-split/gguf-split.cpp +0 -0
- /package/src/llama.cpp/{examples → tools}/imatrix/CMakeLists.txt +0 -0
- /package/src/llama.cpp/{examples → tools}/llama-bench/CMakeLists.txt +0 -0
- /package/src/llama.cpp/{examples → tools}/main/CMakeLists.txt +0 -0
- /package/src/llama.cpp/{examples/llava → tools/mtmd}/deprecation-warning.cpp +0 -0
- /package/src/llama.cpp/{examples/llava → tools/mtmd}/requirements.txt +0 -0
- /package/src/llama.cpp/{examples → tools}/perplexity/CMakeLists.txt +0 -0
- /package/src/llama.cpp/{examples → tools}/quantize/CMakeLists.txt +0 -0
- /package/src/llama.cpp/{examples → tools}/rpc/CMakeLists.txt +0 -0
- /package/src/llama.cpp/{examples → tools}/run/CMakeLists.txt +0 -0
- /package/src/llama.cpp/{examples → tools}/run/linenoise.cpp/linenoise.cpp +0 -0
- /package/src/llama.cpp/{examples → tools}/run/linenoise.cpp/linenoise.h +0 -0
- /package/src/llama.cpp/{examples → tools}/server/bench/requirements.txt +0 -0
- /package/src/llama.cpp/{examples → tools}/server/httplib.h +0 -0
- /package/src/llama.cpp/{examples → tools}/server/tests/requirements.txt +0 -0
- /package/src/llama.cpp/{examples → tools}/tokenize/CMakeLists.txt +0 -0
- /package/src/llama.cpp/{examples → tools}/tokenize/tokenize.cpp +0 -0
- /package/src/llama.cpp/{examples → tools}/tts/CMakeLists.txt +0 -0
- /package/src/llama.cpp/{examples → tools}/tts/tts.cpp +0 -0
|
@@ -1,636 +0,0 @@
|
|
|
1
|
-
#include "arg.h"
|
|
2
|
-
#include "base64.hpp"
|
|
3
|
-
#include "log.h"
|
|
4
|
-
#include "common.h"
|
|
5
|
-
#include "sampling.h"
|
|
6
|
-
#include "clip.h"
|
|
7
|
-
#include "llava.h"
|
|
8
|
-
#include "llama.h"
|
|
9
|
-
#include "ggml.h"
|
|
10
|
-
|
|
11
|
-
#ifdef GGML_USE_CUDA
|
|
12
|
-
#include "ggml-cuda.h"
|
|
13
|
-
#endif
|
|
14
|
-
#ifdef NDEBUG
|
|
15
|
-
#include "ggml-alloc.h"
|
|
16
|
-
#include "ggml-backend.h"
|
|
17
|
-
#endif
|
|
18
|
-
|
|
19
|
-
#include <cstdio>
|
|
20
|
-
#include <cstdlib>
|
|
21
|
-
#include <cstring>
|
|
22
|
-
#include <vector>
|
|
23
|
-
#include <algorithm>
|
|
24
|
-
#include <iostream>
|
|
25
|
-
#include <fstream>
|
|
26
|
-
#include <limits>
|
|
27
|
-
#include <cassert>
|
|
28
|
-
#include <cmath>
|
|
29
|
-
|
|
30
|
-
// THIS FILE IS ONLY USED FOR TESTING THE QWEN2VL MODEL
|
|
31
|
-
// IT IS NOT A PRODUCTION CODE
|
|
32
|
-
|
|
33
|
-
static bool qwen2vl_eval_image_embed(llama_context * ctx_llama, const struct llava_image_embed * image_embed,
|
|
34
|
-
int n_batch, int * n_past, int * st_pos_id, struct clip_image_size * image_size) {
|
|
35
|
-
int n_embd = llama_model_n_embd(llama_get_model(ctx_llama));
|
|
36
|
-
const int patch_size = 14 * 2;
|
|
37
|
-
const int ph = image_size->height / patch_size + (image_size->height % patch_size > 0);
|
|
38
|
-
const int pw = image_size->width / patch_size + (image_size->width % patch_size > 0);
|
|
39
|
-
auto img_tokens = image_embed->n_image_pos;
|
|
40
|
-
// llama_pos mrope_pos[img_tokens * 4];
|
|
41
|
-
std::vector<llama_pos> mrope_pos;
|
|
42
|
-
mrope_pos.resize(img_tokens * 4);
|
|
43
|
-
|
|
44
|
-
for (int y = 0; y < ph; y++)
|
|
45
|
-
{
|
|
46
|
-
for (int x = 0; x < pw; x++)
|
|
47
|
-
{
|
|
48
|
-
int i = y * pw + x;
|
|
49
|
-
mrope_pos[i] = *st_pos_id;
|
|
50
|
-
mrope_pos[i + img_tokens] = *st_pos_id + y;
|
|
51
|
-
mrope_pos[i + img_tokens * 2] = *st_pos_id + x;
|
|
52
|
-
mrope_pos[i + img_tokens * 3] = 0;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
*st_pos_id += std::max(pw, ph);
|
|
56
|
-
|
|
57
|
-
int processed = 0;
|
|
58
|
-
std::vector<llama_pos> batch_mrope_pos;
|
|
59
|
-
batch_mrope_pos.resize(img_tokens * 4);
|
|
60
|
-
|
|
61
|
-
for (int i = 0; i < img_tokens; i += n_batch) {
|
|
62
|
-
int n_eval = img_tokens - i;
|
|
63
|
-
if (n_eval > n_batch) {
|
|
64
|
-
n_eval = n_batch;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// llama_pos batch_mrope_pos[n_eval * 4];
|
|
68
|
-
std::fill(batch_mrope_pos.begin(), batch_mrope_pos.end(), 0);
|
|
69
|
-
memcpy(batch_mrope_pos.data(), &mrope_pos[processed], n_eval * sizeof(llama_pos));
|
|
70
|
-
memcpy(&batch_mrope_pos[n_eval * 1], &mrope_pos[img_tokens * 1 + processed], n_eval * sizeof(llama_pos));
|
|
71
|
-
memcpy(&batch_mrope_pos[n_eval * 2], &mrope_pos[img_tokens * 2 + processed], n_eval * sizeof(llama_pos));
|
|
72
|
-
memcpy(&batch_mrope_pos[n_eval * 3], &mrope_pos[img_tokens * 3 + processed], n_eval * sizeof(llama_pos));
|
|
73
|
-
|
|
74
|
-
llama_batch batch = {
|
|
75
|
-
int32_t(n_eval), // n_tokens
|
|
76
|
-
nullptr, // token
|
|
77
|
-
(image_embed->embed+i*n_embd), // embed
|
|
78
|
-
batch_mrope_pos.data(), // pos
|
|
79
|
-
nullptr, // n_seq_id
|
|
80
|
-
nullptr, // seq_id
|
|
81
|
-
nullptr, // logits
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
if (llama_decode(ctx_llama, batch)) {
|
|
85
|
-
LOG_ERR("%s : failed to eval\n", __func__);
|
|
86
|
-
return false;
|
|
87
|
-
}
|
|
88
|
-
*n_past += n_eval;
|
|
89
|
-
processed += n_eval;
|
|
90
|
-
}
|
|
91
|
-
return true;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
static bool eval_tokens(struct llama_context * ctx_llama, std::vector<llama_token> tokens, int n_batch, int * n_past, int * st_pos_id) {
|
|
96
|
-
int N = (int) tokens.size();
|
|
97
|
-
for (int i = 0; i < N; i += n_batch) {
|
|
98
|
-
int n_eval = (int) tokens.size() - i;
|
|
99
|
-
if (n_eval > n_batch) {
|
|
100
|
-
n_eval = n_batch;
|
|
101
|
-
}
|
|
102
|
-
auto batch = llama_batch_get_one(&tokens[i], n_eval);
|
|
103
|
-
|
|
104
|
-
if (llama_decode(ctx_llama, batch)) {
|
|
105
|
-
LOG_ERR("%s : failed to eval. token %d/%d (batch size %d, n_past %d)\n", __func__, i, N, n_batch, *n_past);
|
|
106
|
-
return false;
|
|
107
|
-
}
|
|
108
|
-
*n_past += n_eval;
|
|
109
|
-
*st_pos_id += n_eval;
|
|
110
|
-
}
|
|
111
|
-
return true;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
static bool eval_id(struct llama_context * ctx_llama, int id, int * n_past, int * st_pos_id) {
|
|
115
|
-
std::vector<llama_token> tokens;
|
|
116
|
-
tokens.push_back(id);
|
|
117
|
-
return eval_tokens(ctx_llama, tokens, 1, n_past, st_pos_id);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
static bool eval_string(struct llama_context * ctx_llama, const char* str, int n_batch, int * n_past, int * st_pos_id, bool add_bos){
|
|
121
|
-
std::string str2 = str;
|
|
122
|
-
std::vector<llama_token> embd_inp = common_tokenize(ctx_llama, str2, add_bos, true);
|
|
123
|
-
eval_tokens(ctx_llama, embd_inp, n_batch, n_past, st_pos_id);
|
|
124
|
-
return true;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
static const char * sample(struct common_sampler * smpl,
|
|
128
|
-
struct llama_context * ctx_llama,
|
|
129
|
-
int * n_past, int * st_pos_id) {
|
|
130
|
-
const llama_token id = common_sampler_sample(smpl, ctx_llama, -1);
|
|
131
|
-
common_sampler_accept(smpl, id, true);
|
|
132
|
-
|
|
133
|
-
const llama_model * model = llama_get_model(ctx_llama);
|
|
134
|
-
const llama_vocab * vocab = llama_model_get_vocab(model);
|
|
135
|
-
|
|
136
|
-
static std::string ret;
|
|
137
|
-
if (llama_vocab_is_eog(vocab, id)) {
|
|
138
|
-
ret = "</s>";
|
|
139
|
-
} else {
|
|
140
|
-
ret = common_token_to_piece(ctx_llama, id);
|
|
141
|
-
}
|
|
142
|
-
eval_id(ctx_llama, id, n_past, st_pos_id);
|
|
143
|
-
return ret.c_str();
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
static const char* IMG_BASE64_TAG_BEGIN = "<img src=\"data:image/jpeg;base64,";
|
|
147
|
-
static const char* IMG_BASE64_TAG_END = "\">";
|
|
148
|
-
|
|
149
|
-
static void find_image_tag_in_prompt(const std::string& prompt, size_t& begin_out, size_t& end_out) {
|
|
150
|
-
begin_out = prompt.find(IMG_BASE64_TAG_BEGIN);
|
|
151
|
-
end_out = prompt.find(IMG_BASE64_TAG_END, (begin_out == std::string::npos) ? 0UL : begin_out);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
static bool prompt_contains_image(const std::string& prompt) {
|
|
155
|
-
size_t begin, end;
|
|
156
|
-
find_image_tag_in_prompt(prompt, begin, end);
|
|
157
|
-
return (begin != std::string::npos);
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
// replaces the base64 image tag in the prompt with `replacement`
|
|
161
|
-
static llava_image_embed * llava_image_embed_make_with_prompt_base64(struct clip_ctx * ctx_clip, int n_threads, const std::string& prompt) {
|
|
162
|
-
size_t img_base64_str_start, img_base64_str_end;
|
|
163
|
-
find_image_tag_in_prompt(prompt, img_base64_str_start, img_base64_str_end);
|
|
164
|
-
if (img_base64_str_start == std::string::npos || img_base64_str_end == std::string::npos) {
|
|
165
|
-
LOG_ERR("%s: invalid base64 image tag. must be %s<base64 byte string>%s\n", __func__, IMG_BASE64_TAG_BEGIN, IMG_BASE64_TAG_END);
|
|
166
|
-
return NULL;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
auto base64_bytes_start = img_base64_str_start + strlen(IMG_BASE64_TAG_BEGIN);
|
|
170
|
-
auto base64_bytes_count = img_base64_str_end - base64_bytes_start;
|
|
171
|
-
auto base64_str = prompt.substr(base64_bytes_start, base64_bytes_count );
|
|
172
|
-
|
|
173
|
-
auto required_bytes = base64::required_encode_size(base64_str.size());
|
|
174
|
-
auto img_bytes = std::vector<unsigned char>(required_bytes);
|
|
175
|
-
base64::decode(base64_str.begin(), base64_str.end(), img_bytes.begin());
|
|
176
|
-
|
|
177
|
-
auto embed = llava_image_embed_make_with_bytes(ctx_clip, n_threads, img_bytes.data(), img_bytes.size());
|
|
178
|
-
if (!embed) {
|
|
179
|
-
LOG_ERR("%s: could not load image from base64 string.\n", __func__);
|
|
180
|
-
return NULL;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
return embed;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
static std::string remove_image_from_prompt(const std::string& prompt, const char * replacement = "") {
|
|
187
|
-
size_t begin, end;
|
|
188
|
-
find_image_tag_in_prompt(prompt, begin, end);
|
|
189
|
-
if (begin == std::string::npos || end == std::string::npos) {
|
|
190
|
-
return prompt;
|
|
191
|
-
}
|
|
192
|
-
auto pre = prompt.substr(0, begin);
|
|
193
|
-
auto post = prompt.substr(end + strlen(IMG_BASE64_TAG_END));
|
|
194
|
-
return pre + replacement + post;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
struct llava_context {
|
|
198
|
-
struct clip_ctx * ctx_clip = NULL;
|
|
199
|
-
struct llama_context * ctx_llama = NULL;
|
|
200
|
-
struct llama_model * model = NULL;
|
|
201
|
-
};
|
|
202
|
-
|
|
203
|
-
static void print_usage(int, char ** argv) {
|
|
204
|
-
LOG("\n example usage:\n");
|
|
205
|
-
LOG("\n %s -m <llava-v1.5-7b/ggml-model-q5_k.gguf> --mmproj <llava-v1.5-7b/mmproj-model-f16.gguf> --image <path/to/an/image.jpg> --image <path/to/another/image.jpg> [--temp 0.1] [-p \"describe the image in detail.\"]\n", argv[0]);
|
|
206
|
-
LOG("\n note: a lower temperature value like 0.1 is recommended for better quality.\n");
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
static struct llava_image_embed * load_image(llava_context * ctx_llava, common_params * params, const std::string & fname) {
|
|
210
|
-
|
|
211
|
-
// load and preprocess the image
|
|
212
|
-
llava_image_embed * embed = NULL;
|
|
213
|
-
auto prompt = params->prompt;
|
|
214
|
-
if (prompt_contains_image(prompt)) {
|
|
215
|
-
if (!params->image.empty()) {
|
|
216
|
-
LOG_INF("using base64 encoded image instead of command line image path\n");
|
|
217
|
-
}
|
|
218
|
-
embed = llava_image_embed_make_with_prompt_base64(ctx_llava->ctx_clip, params->cpuparams.n_threads, prompt);
|
|
219
|
-
if (!embed) {
|
|
220
|
-
LOG_ERR("%s: can't load image from prompt\n", __func__);
|
|
221
|
-
return NULL;
|
|
222
|
-
}
|
|
223
|
-
params->prompt = remove_image_from_prompt(prompt);
|
|
224
|
-
} else {
|
|
225
|
-
embed = llava_image_embed_make_with_filename(ctx_llava->ctx_clip, params->cpuparams.n_threads, fname.c_str());
|
|
226
|
-
if (!embed) {
|
|
227
|
-
fprintf(stderr, "%s: is %s really an image file?\n", __func__, fname.c_str());
|
|
228
|
-
return NULL;
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
return embed;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
static void process_prompt(struct llava_context * ctx_llava, struct llava_image_embed * image_embed, common_params * params, const std::string & prompt) {
|
|
236
|
-
int n_past = 0;
|
|
237
|
-
int cur_pos_id = 0;
|
|
238
|
-
|
|
239
|
-
const int max_tgt_len = params->n_predict < 0 ? 256 : params->n_predict;
|
|
240
|
-
|
|
241
|
-
std::string system_prompt, user_prompt;
|
|
242
|
-
size_t image_pos = prompt.find("<|vision_start|>");
|
|
243
|
-
if (image_pos != std::string::npos) {
|
|
244
|
-
// new templating mode: Provide the full prompt including system message and use <image> as a placeholder for the image
|
|
245
|
-
system_prompt = prompt.substr(0, image_pos);
|
|
246
|
-
user_prompt = prompt.substr(image_pos + std::string("<|vision_pad|>").length());
|
|
247
|
-
LOG_INF("system_prompt: %s\n", system_prompt.c_str());
|
|
248
|
-
if (params->verbose_prompt) {
|
|
249
|
-
auto tmp = common_tokenize(ctx_llava->ctx_llama, system_prompt, true, true);
|
|
250
|
-
for (int i = 0; i < (int) tmp.size(); i++) {
|
|
251
|
-
LOG_INF("%6d -> '%s'\n", tmp[i], common_token_to_piece(ctx_llava->ctx_llama, tmp[i]).c_str());
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
LOG_INF("user_prompt: %s\n", user_prompt.c_str());
|
|
255
|
-
if (params->verbose_prompt) {
|
|
256
|
-
auto tmp = common_tokenize(ctx_llava->ctx_llama, user_prompt, true, true);
|
|
257
|
-
for (int i = 0; i < (int) tmp.size(); i++) {
|
|
258
|
-
LOG_INF("%6d -> '%s'\n", tmp[i], common_token_to_piece(ctx_llava->ctx_llama, tmp[i]).c_str());
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
} else {
|
|
262
|
-
// llava-1.5 native mode
|
|
263
|
-
system_prompt = "<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n<|vision_start|>";
|
|
264
|
-
user_prompt = "<|vision_end|>" + prompt + "<|im_end|>\n<|im_start|>assistant\n";
|
|
265
|
-
if (params->verbose_prompt) {
|
|
266
|
-
auto tmp = common_tokenize(ctx_llava->ctx_llama, user_prompt, true, true);
|
|
267
|
-
for (int i = 0; i < (int) tmp.size(); i++) {
|
|
268
|
-
LOG_INF("%6d -> '%s'\n", tmp[i], common_token_to_piece(ctx_llava->ctx_llama, tmp[i]).c_str());
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
eval_string(ctx_llava->ctx_llama, system_prompt.c_str(), params->n_batch, &n_past, &cur_pos_id, true);
|
|
274
|
-
if (image_embed != nullptr) {
|
|
275
|
-
auto image_size = clip_get_load_image_size(ctx_llava->ctx_clip);
|
|
276
|
-
qwen2vl_eval_image_embed(ctx_llava->ctx_llama, image_embed, params->n_batch, &n_past, &cur_pos_id, image_size);
|
|
277
|
-
}
|
|
278
|
-
eval_string(ctx_llava->ctx_llama, user_prompt.c_str(), params->n_batch, &n_past, &cur_pos_id, false);
|
|
279
|
-
|
|
280
|
-
// generate the response
|
|
281
|
-
|
|
282
|
-
LOG("\n");
|
|
283
|
-
|
|
284
|
-
struct common_sampler * smpl = common_sampler_init(ctx_llava->model, params->sampling);
|
|
285
|
-
if (!smpl) {
|
|
286
|
-
LOG_ERR("%s: failed to initialize sampling subsystem\n", __func__);
|
|
287
|
-
exit(1);
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
std::string response = "";
|
|
291
|
-
for (int i = 0; i < max_tgt_len; i++) {
|
|
292
|
-
const char * tmp = sample(smpl, ctx_llava->ctx_llama, &n_past, &cur_pos_id);
|
|
293
|
-
response += tmp;
|
|
294
|
-
if (strcmp(tmp, "</s>") == 0) break;
|
|
295
|
-
if (strstr(tmp, "###")) break; // Yi-VL behavior
|
|
296
|
-
LOG("%s", tmp);
|
|
297
|
-
if (strstr(response.c_str(), "<|im_end|>")) break; // Yi-34B llava-1.6 - for some reason those decode not as the correct token (tokenizer works)
|
|
298
|
-
if (strstr(response.c_str(), "<|im_start|>")) break; // Yi-34B llava-1.6
|
|
299
|
-
if (strstr(response.c_str(), "USER:")) break; // mistral llava-1.6
|
|
300
|
-
|
|
301
|
-
fflush(stdout);
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
common_sampler_free(smpl);
|
|
305
|
-
LOG("\n");
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
static struct llama_model * llava_init(common_params * params) {
|
|
309
|
-
llama_backend_init();
|
|
310
|
-
llama_numa_init(params->numa);
|
|
311
|
-
|
|
312
|
-
llama_model_params model_params = common_model_params_to_llama(*params);
|
|
313
|
-
|
|
314
|
-
llama_model * model = llama_model_load_from_file(params->model.path.c_str(), model_params);
|
|
315
|
-
if (model == NULL) {
|
|
316
|
-
LOG_ERR("%s: unable to load model\n" , __func__);
|
|
317
|
-
return NULL;
|
|
318
|
-
}
|
|
319
|
-
return model;
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
static struct llava_context * llava_init_context(common_params * params, llama_model * model) {
|
|
323
|
-
const char * clip_path = params->mmproj.path.c_str();
|
|
324
|
-
|
|
325
|
-
auto prompt = params->prompt;
|
|
326
|
-
if (prompt.empty()) {
|
|
327
|
-
prompt = "describe the image in detail.";
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
auto ctx_clip = clip_model_load(clip_path, GGML_LOG_LEVEL_INFO);
|
|
331
|
-
|
|
332
|
-
llama_context_params ctx_params = common_context_params_to_llama(*params);
|
|
333
|
-
ctx_params.n_ctx = params->n_ctx < 2048 ? 2048 : params->n_ctx; // we need a longer context size to process image embeddings
|
|
334
|
-
|
|
335
|
-
llama_context * ctx_llama = llama_init_from_model(model, ctx_params);
|
|
336
|
-
|
|
337
|
-
if (ctx_llama == NULL) {
|
|
338
|
-
LOG_ERR("%s: failed to create the llama_context\n" , __func__);
|
|
339
|
-
return NULL;
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
auto * ctx_llava = (struct llava_context *)malloc(sizeof(llava_context));
|
|
343
|
-
|
|
344
|
-
ctx_llava->ctx_llama = ctx_llama;
|
|
345
|
-
ctx_llava->ctx_clip = ctx_clip;
|
|
346
|
-
ctx_llava->model = model;
|
|
347
|
-
return ctx_llava;
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
static void llava_free(struct llava_context * ctx_llava) {
|
|
351
|
-
if (ctx_llava->ctx_clip) {
|
|
352
|
-
clip_free(ctx_llava->ctx_clip);
|
|
353
|
-
ctx_llava->ctx_clip = NULL;
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
llama_free(ctx_llava->ctx_llama);
|
|
357
|
-
llama_model_free(ctx_llava->model);
|
|
358
|
-
llama_backend_free();
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
#ifndef NDEBUG
|
|
362
|
-
|
|
363
|
-
static void debug_test_mrope_2d() {
|
|
364
|
-
// 1. Initialize backend
|
|
365
|
-
ggml_backend_t backend = NULL;
|
|
366
|
-
std::string backend_name = "";
|
|
367
|
-
// #ifdef GGML_USE_CUDA
|
|
368
|
-
// fprintf(stderr, "%s: using CUDA backend\n", __func__);
|
|
369
|
-
// backend = ggml_backend_cuda_init(0); // init device 0
|
|
370
|
-
// backend_name = "cuda";
|
|
371
|
-
// if (!backend) {
|
|
372
|
-
// fprintf(stderr, "%s: ggml_backend_cuda_init() failed\n", __func__);
|
|
373
|
-
// }
|
|
374
|
-
// #endif
|
|
375
|
-
// if there aren't GPU Backends fallback to CPU backend
|
|
376
|
-
if (!backend) {
|
|
377
|
-
backend = ggml_backend_cpu_init();
|
|
378
|
-
backend_name = "cpu";
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
// Calculate the size needed to allocate
|
|
382
|
-
size_t ctx_size = 0;
|
|
383
|
-
ctx_size += 2 * ggml_tensor_overhead(); // tensors
|
|
384
|
-
// no need to allocate anything else!
|
|
385
|
-
|
|
386
|
-
// 2. Allocate `ggml_context` to store tensor data
|
|
387
|
-
struct ggml_init_params params = {
|
|
388
|
-
/*.mem_size =*/ ctx_size,
|
|
389
|
-
/*.mem_buffer =*/ NULL,
|
|
390
|
-
/*.no_alloc =*/ true, // the tensors will be allocated later by ggml_backend_alloc_ctx_tensors()
|
|
391
|
-
};
|
|
392
|
-
struct ggml_context * ctx = ggml_init(params);
|
|
393
|
-
|
|
394
|
-
struct ggml_tensor * inp_raw = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, 128, 12, 30);
|
|
395
|
-
ggml_set_name(inp_raw, "inp_raw");
|
|
396
|
-
ggml_set_input(inp_raw);
|
|
397
|
-
|
|
398
|
-
struct ggml_tensor * pos = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, 30 * 4);
|
|
399
|
-
ggml_set_name(pos, "pos");
|
|
400
|
-
ggml_set_input(pos);
|
|
401
|
-
|
|
402
|
-
std::vector<float> dummy_q;
|
|
403
|
-
dummy_q.resize(128 * 12 * 30);
|
|
404
|
-
std::fill(dummy_q.begin(), dummy_q.end(), 0.1);
|
|
405
|
-
// memcpy(inp_raw->data, dummy_q.data(), 128 * 12 * 30 * ggml_element_size(inp_raw));
|
|
406
|
-
|
|
407
|
-
std::vector<int> pos_id;
|
|
408
|
-
pos_id.resize(30 * 4);
|
|
409
|
-
for (int i = 0; i < 30; i ++) {
|
|
410
|
-
pos_id[i] = i;
|
|
411
|
-
pos_id[i + 30] = i + 10;
|
|
412
|
-
pos_id[i + 60] = i + 20;
|
|
413
|
-
pos_id[i + 90] = i + 30;
|
|
414
|
-
}
|
|
415
|
-
int sections[4] = {32, 32, 0, 0};
|
|
416
|
-
|
|
417
|
-
// 4. Allocate a `ggml_backend_buffer` to store all tensors
|
|
418
|
-
ggml_backend_buffer_t buffer = ggml_backend_alloc_ctx_tensors(ctx, backend);
|
|
419
|
-
|
|
420
|
-
// 5. Copy tensor data from main memory (RAM) to backend buffer
|
|
421
|
-
ggml_backend_tensor_set(inp_raw, dummy_q.data(), 0, ggml_nbytes(inp_raw));
|
|
422
|
-
ggml_backend_tensor_set(pos, pos_id.data(), 0, ggml_nbytes(pos));
|
|
423
|
-
|
|
424
|
-
// 6. Create a `ggml_cgraph` for mul_mat operation
|
|
425
|
-
struct ggml_cgraph * gf = NULL;
|
|
426
|
-
struct ggml_context * ctx_cgraph = NULL;
|
|
427
|
-
|
|
428
|
-
// create a temporally context to build the graph
|
|
429
|
-
struct ggml_init_params params0 = {
|
|
430
|
-
/*.mem_size =*/ ggml_tensor_overhead()*GGML_DEFAULT_GRAPH_SIZE + ggml_graph_overhead(),
|
|
431
|
-
/*.mem_buffer =*/ NULL,
|
|
432
|
-
/*.no_alloc =*/ true, // the tensors will be allocated later by ggml_gallocr_alloc_graph()
|
|
433
|
-
};
|
|
434
|
-
ctx_cgraph = ggml_init(params0);
|
|
435
|
-
gf = ggml_new_graph(ctx_cgraph);
|
|
436
|
-
|
|
437
|
-
struct ggml_tensor * result0 = ggml_rope_multi(
|
|
438
|
-
ctx_cgraph, inp_raw, pos, nullptr,
|
|
439
|
-
128/2, sections, LLAMA_ROPE_TYPE_VISION, 32768, 1000000, 1,
|
|
440
|
-
0, 1, 32, 1);
|
|
441
|
-
|
|
442
|
-
// Add "result" tensor and all of its dependencies to the cgraph
|
|
443
|
-
ggml_build_forward_expand(gf, result0);
|
|
444
|
-
|
|
445
|
-
// 7. Create a `ggml_gallocr` for cgraph computation
|
|
446
|
-
ggml_gallocr_t allocr = ggml_gallocr_new(ggml_backend_get_default_buffer_type(backend));
|
|
447
|
-
ggml_gallocr_alloc_graph(allocr, gf);
|
|
448
|
-
|
|
449
|
-
// 9. Run the computation
|
|
450
|
-
int n_threads = 1; // Optional: number of threads to perform some operations with multi-threading
|
|
451
|
-
if (ggml_backend_is_cpu(backend)) {
|
|
452
|
-
ggml_backend_cpu_set_n_threads(backend, n_threads);
|
|
453
|
-
}
|
|
454
|
-
ggml_backend_graph_compute(backend, gf);
|
|
455
|
-
|
|
456
|
-
// 10. Retrieve results (output tensors)
|
|
457
|
-
// in this example, output tensor is always the last tensor in the graph
|
|
458
|
-
struct ggml_tensor * result = result0;
|
|
459
|
-
// struct ggml_tensor * result = gf->nodes[gf->n_nodes - 1];
|
|
460
|
-
float * result_data = (float *)malloc(ggml_nbytes(result));
|
|
461
|
-
// because the tensor data is stored in device buffer, we need to copy it back to RAM
|
|
462
|
-
ggml_backend_tensor_get(result, result_data, 0, ggml_nbytes(result));
|
|
463
|
-
const std::string bin_file = "mrope_2d_" + backend_name +".bin";
|
|
464
|
-
std::ofstream outFile(bin_file, std::ios::binary);
|
|
465
|
-
|
|
466
|
-
if (outFile.is_open()) {
|
|
467
|
-
outFile.write(reinterpret_cast<const char*>(result_data), ggml_nbytes(result));
|
|
468
|
-
outFile.close();
|
|
469
|
-
std::cout << "Data successfully written to " + bin_file << std::endl;
|
|
470
|
-
} else {
|
|
471
|
-
std::cerr << "Error opening file!" << std::endl;
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
free(result_data);
|
|
475
|
-
// 11. Free memory and exit
|
|
476
|
-
ggml_free(ctx_cgraph);
|
|
477
|
-
ggml_gallocr_free(allocr);
|
|
478
|
-
ggml_free(ctx);
|
|
479
|
-
ggml_backend_buffer_free(buffer);
|
|
480
|
-
ggml_backend_free(backend);
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
enum model_output_type {
|
|
484
|
-
conv3d,
|
|
485
|
-
patch_embed,
|
|
486
|
-
patch_win_attn_scatter,
|
|
487
|
-
first_attn_layer,
|
|
488
|
-
last_attn_layer,
|
|
489
|
-
attn_softmax,
|
|
490
|
-
final_layer,
|
|
491
|
-
};
|
|
492
|
-
|
|
493
|
-
static void debug_dump_img_embed(struct llava_context * ctx_llava, model_output_type output_type) {
|
|
494
|
-
constexpr int ih = 140;
|
|
495
|
-
constexpr int iw = 196;
|
|
496
|
-
// constexpr int ih = 56;
|
|
497
|
-
// constexpr int iw = 56;
|
|
498
|
-
// int n_embd = llama_model_n_embd(llama_get_model(ctx_llava->ctx_llama));
|
|
499
|
-
int n_embd = 1280;
|
|
500
|
-
int merge = 1;
|
|
501
|
-
if (output_type == model_output_type::final_layer) {
|
|
502
|
-
n_embd = 2048;
|
|
503
|
-
merge = 2;
|
|
504
|
-
}
|
|
505
|
-
else if (output_type == model_output_type::attn_softmax) {
|
|
506
|
-
merge = 1;
|
|
507
|
-
n_embd = (ih/14/merge) * (iw/14/merge) * 16;
|
|
508
|
-
}
|
|
509
|
-
|
|
510
|
-
int ne = (ih/14/merge) * (iw/14/merge) * n_embd;
|
|
511
|
-
float vals[iw * ih * 3];
|
|
512
|
-
// float embd[ne];
|
|
513
|
-
std::vector<float> embd;
|
|
514
|
-
embd.resize(ne);
|
|
515
|
-
|
|
516
|
-
for (int i = 0; i < iw*ih; i++)
|
|
517
|
-
{
|
|
518
|
-
for (int c = 0; c < 3; c++)
|
|
519
|
-
vals[i * 3 + c] = (float)i / (iw*ih);
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
clip_encode_float_image(ctx_llava->ctx_clip, 8, vals, ih, iw, embd.data());
|
|
523
|
-
|
|
524
|
-
std::string file_postfix = "";
|
|
525
|
-
switch (output_type)
|
|
526
|
-
{
|
|
527
|
-
case model_output_type::conv3d:
|
|
528
|
-
file_postfix = "conv3d";
|
|
529
|
-
break;
|
|
530
|
-
case model_output_type::patch_embed:
|
|
531
|
-
file_postfix = "patch_embed";
|
|
532
|
-
break;
|
|
533
|
-
case model_output_type::patch_win_attn_scatter:
|
|
534
|
-
file_postfix = "scatter";
|
|
535
|
-
break;
|
|
536
|
-
case model_output_type::first_attn_layer:
|
|
537
|
-
file_postfix = "first_attn";
|
|
538
|
-
break;
|
|
539
|
-
case model_output_type::last_attn_layer:
|
|
540
|
-
file_postfix = "last_attn";
|
|
541
|
-
break;
|
|
542
|
-
case model_output_type::attn_softmax:
|
|
543
|
-
file_postfix = "attn_softmax";
|
|
544
|
-
break;
|
|
545
|
-
case model_output_type::final_layer:
|
|
546
|
-
file_postfix = "final";
|
|
547
|
-
break;
|
|
548
|
-
default:
|
|
549
|
-
break;
|
|
550
|
-
}
|
|
551
|
-
auto output_path = "img_embed_" + file_postfix + ".bin";
|
|
552
|
-
|
|
553
|
-
std::ofstream outFile(output_path, std::ios::binary);
|
|
554
|
-
if (outFile.is_open()) {
|
|
555
|
-
outFile.write(reinterpret_cast<const char*>(embd.data()), ne * sizeof(float));
|
|
556
|
-
|
|
557
|
-
outFile.close();
|
|
558
|
-
std::cout << "Data successfully written to ::[ " << output_path << std::endl;
|
|
559
|
-
} else {
|
|
560
|
-
std::cerr << "Error opening file!" << std::endl;
|
|
561
|
-
}
|
|
562
|
-
}
|
|
563
|
-
|
|
564
|
-
#endif
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
int main(int argc, char ** argv) {
|
|
568
|
-
ggml_time_init();
|
|
569
|
-
|
|
570
|
-
common_params params;
|
|
571
|
-
|
|
572
|
-
if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_LLAVA, print_usage)) {
|
|
573
|
-
return 1;
|
|
574
|
-
}
|
|
575
|
-
|
|
576
|
-
common_init();
|
|
577
|
-
|
|
578
|
-
if (params.mmproj.path.empty() || (params.image.empty() && !prompt_contains_image(params.prompt))) {
|
|
579
|
-
print_usage(argc, argv);
|
|
580
|
-
return 1;
|
|
581
|
-
}
|
|
582
|
-
|
|
583
|
-
auto * model = llava_init(¶ms);
|
|
584
|
-
if (model == NULL) {
|
|
585
|
-
fprintf(stderr, "%s: error: failed to init llava model\n", __func__);
|
|
586
|
-
return 1;
|
|
587
|
-
}
|
|
588
|
-
|
|
589
|
-
if (prompt_contains_image(params.prompt)) {
|
|
590
|
-
auto * ctx_llava = llava_init_context(¶ms, model);
|
|
591
|
-
|
|
592
|
-
auto * image_embed = load_image(ctx_llava, ¶ms, "");
|
|
593
|
-
|
|
594
|
-
// process the prompt
|
|
595
|
-
process_prompt(ctx_llava, image_embed, ¶ms, params.prompt);
|
|
596
|
-
|
|
597
|
-
llama_perf_context_print(ctx_llava->ctx_llama);
|
|
598
|
-
llava_image_embed_free(image_embed);
|
|
599
|
-
ctx_llava->model = NULL;
|
|
600
|
-
llava_free(ctx_llava);
|
|
601
|
-
#ifndef NDEBUG
|
|
602
|
-
} else if (params.image[0].empty()) {
|
|
603
|
-
auto ctx_llava = llava_init_context(¶ms, model);
|
|
604
|
-
|
|
605
|
-
// debug_test_mrope_2d();
|
|
606
|
-
debug_dump_img_embed(ctx_llava, model_output_type::final_layer);
|
|
607
|
-
// debug_dump_img_embed(ctx_llava, model_output_type::last_attn_layer);
|
|
608
|
-
|
|
609
|
-
llama_perf_context_print(ctx_llava->ctx_llama);
|
|
610
|
-
ctx_llava->model = NULL;
|
|
611
|
-
llava_free(ctx_llava);
|
|
612
|
-
#endif
|
|
613
|
-
} else {
|
|
614
|
-
for (auto & image : params.image) {
|
|
615
|
-
auto * ctx_llava = llava_init_context(¶ms, model);
|
|
616
|
-
|
|
617
|
-
auto * image_embed = load_image(ctx_llava, ¶ms, image);
|
|
618
|
-
if (!image_embed) {
|
|
619
|
-
LOG_ERR("%s: failed to load image %s. Terminating\n\n", __func__, image.c_str());
|
|
620
|
-
return 1;
|
|
621
|
-
}
|
|
622
|
-
|
|
623
|
-
// process the prompt
|
|
624
|
-
process_prompt(ctx_llava, image_embed, ¶ms, params.prompt);
|
|
625
|
-
|
|
626
|
-
llama_perf_context_print(ctx_llava->ctx_llama);
|
|
627
|
-
llava_image_embed_free(image_embed);
|
|
628
|
-
ctx_llava->model = NULL;
|
|
629
|
-
llava_free(ctx_llava);
|
|
630
|
-
}
|
|
631
|
-
}
|
|
632
|
-
|
|
633
|
-
llama_model_free(model);
|
|
634
|
-
|
|
635
|
-
return 0;
|
|
636
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|