@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.
- package/CMakeLists.txt +85 -0
- package/README.md +56 -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/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/lib/binding.js +13 -0
- package/lib/binding.ts +57 -0
- package/lib/index.js +24 -0
- package/lib/index.ts +13 -0
- package/package.json +65 -0
- package/src/addons.cpp +506 -0
- package/src/llama.cpp/CMakeLists.txt +1320 -0
- package/src/llama.cpp/build.zig +172 -0
- package/src/llama.cpp/cmake/FindSIMD.cmake +100 -0
- package/src/llama.cpp/common/CMakeLists.txt +87 -0
- package/src/llama.cpp/common/base64.hpp +392 -0
- package/src/llama.cpp/common/common.cpp +2949 -0
- package/src/llama.cpp/common/common.h +324 -0
- package/src/llama.cpp/common/console.cpp +501 -0
- package/src/llama.cpp/common/console.h +19 -0
- package/src/llama.cpp/common/grammar-parser.cpp +440 -0
- package/src/llama.cpp/common/grammar-parser.h +29 -0
- package/src/llama.cpp/common/json-schema-to-grammar.cpp +764 -0
- package/src/llama.cpp/common/json-schema-to-grammar.h +4 -0
- package/src/llama.cpp/common/json.hpp +24766 -0
- package/src/llama.cpp/common/log.h +724 -0
- package/src/llama.cpp/common/ngram-cache.cpp +282 -0
- package/src/llama.cpp/common/ngram-cache.h +94 -0
- package/src/llama.cpp/common/sampling.cpp +353 -0
- package/src/llama.cpp/common/sampling.h +147 -0
- package/src/llama.cpp/common/stb_image.h +8396 -0
- package/src/llama.cpp/common/train.cpp +1513 -0
- package/src/llama.cpp/common/train.h +233 -0
- package/src/llama.cpp/examples/CMakeLists.txt +52 -0
- package/src/llama.cpp/examples/baby-llama/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/baby-llama/baby-llama.cpp +1640 -0
- package/src/llama.cpp/examples/batched/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/batched/batched.cpp +262 -0
- package/src/llama.cpp/examples/batched-bench/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/batched-bench/batched-bench.cpp +261 -0
- package/src/llama.cpp/examples/beam-search/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/beam-search/beam-search.cpp +188 -0
- package/src/llama.cpp/examples/benchmark/CMakeLists.txt +6 -0
- package/src/llama.cpp/examples/benchmark/benchmark-matmult.cpp +275 -0
- package/src/llama.cpp/examples/convert-llama2c-to-ggml/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp +936 -0
- package/src/llama.cpp/examples/embedding/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/embedding/embedding.cpp +211 -0
- package/src/llama.cpp/examples/eval-callback/CMakeLists.txt +9 -0
- package/src/llama.cpp/examples/eval-callback/eval-callback.cpp +195 -0
- package/src/llama.cpp/examples/export-lora/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/export-lora/export-lora.cpp +462 -0
- package/src/llama.cpp/examples/finetune/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/finetune/finetune.cpp +1861 -0
- package/src/llama.cpp/examples/gbnf-validator/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/gbnf-validator/gbnf-validator.cpp +132 -0
- package/src/llama.cpp/examples/gguf/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/gguf/gguf.cpp +256 -0
- package/src/llama.cpp/examples/gguf-split/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/gguf-split/gguf-split.cpp +553 -0
- package/src/llama.cpp/examples/gritlm/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/gritlm/gritlm.cpp +215 -0
- package/src/llama.cpp/examples/imatrix/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/imatrix/imatrix.cpp +655 -0
- package/src/llama.cpp/examples/infill/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/infill/infill.cpp +767 -0
- package/src/llama.cpp/examples/jeopardy/questions.txt +100 -0
- package/src/llama.cpp/examples/llama-bench/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/llama-bench/llama-bench.cpp +1286 -0
- package/src/llama.cpp/examples/llama.android/app/src/main/cpp/CMakeLists.txt +50 -0
- package/src/llama.cpp/examples/llama.android/app/src/main/cpp/llama-android.cpp +443 -0
- package/src/llama.cpp/examples/llava/CMakeLists.txt +37 -0
- package/src/llama.cpp/examples/llava/clip.cpp +2027 -0
- package/src/llama.cpp/examples/llava/clip.h +85 -0
- package/src/llama.cpp/examples/llava/llava-cli.cpp +309 -0
- package/src/llama.cpp/examples/llava/llava.cpp +426 -0
- package/src/llama.cpp/examples/llava/llava.h +50 -0
- package/src/llama.cpp/examples/llava/requirements.txt +3 -0
- package/src/llama.cpp/examples/lookahead/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/lookahead/lookahead.cpp +485 -0
- package/src/llama.cpp/examples/lookup/CMakeLists.txt +23 -0
- package/src/llama.cpp/examples/lookup/lookup-create.cpp +41 -0
- package/src/llama.cpp/examples/lookup/lookup-merge.cpp +47 -0
- package/src/llama.cpp/examples/lookup/lookup-stats.cpp +160 -0
- package/src/llama.cpp/examples/lookup/lookup.cpp +258 -0
- package/src/llama.cpp/examples/main/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/main/main.cpp +957 -0
- package/src/llama.cpp/examples/main-cmake-pkg/CMakeLists.txt +33 -0
- package/src/llama.cpp/examples/parallel/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/parallel/parallel.cpp +427 -0
- package/src/llama.cpp/examples/passkey/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/passkey/passkey.cpp +302 -0
- package/src/llama.cpp/examples/perplexity/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/perplexity/perplexity.cpp +1943 -0
- package/src/llama.cpp/examples/quantize/CMakeLists.txt +6 -0
- package/src/llama.cpp/examples/quantize/quantize.cpp +423 -0
- package/src/llama.cpp/examples/quantize-stats/CMakeLists.txt +6 -0
- package/src/llama.cpp/examples/quantize-stats/quantize-stats.cpp +424 -0
- package/src/llama.cpp/examples/retrieval/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/retrieval/retrieval.cpp +350 -0
- package/src/llama.cpp/examples/save-load-state/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/save-load-state/save-load-state.cpp +246 -0
- package/src/llama.cpp/examples/server/CMakeLists.txt +40 -0
- package/src/llama.cpp/examples/server/bench/requirements.txt +2 -0
- package/src/llama.cpp/examples/server/httplib.h +9465 -0
- package/src/llama.cpp/examples/server/server.cpp +3826 -0
- package/src/llama.cpp/examples/server/tests/requirements.txt +6 -0
- package/src/llama.cpp/examples/server/utils.hpp +653 -0
- package/src/llama.cpp/examples/simple/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/simple/simple.cpp +183 -0
- package/src/llama.cpp/examples/speculative/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/speculative/speculative.cpp +614 -0
- package/src/llama.cpp/examples/sycl/CMakeLists.txt +9 -0
- package/src/llama.cpp/examples/sycl/ls-sycl-device.cpp +13 -0
- package/src/llama.cpp/examples/tokenize/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/tokenize/tokenize.cpp +42 -0
- package/src/llama.cpp/examples/train-text-from-scratch/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/train-text-from-scratch/train-text-from-scratch.cpp +1252 -0
- package/src/llama.cpp/ggml-alloc.c +985 -0
- package/src/llama.cpp/ggml-alloc.h +76 -0
- package/src/llama.cpp/ggml-backend-impl.h +141 -0
- package/src/llama.cpp/ggml-backend.c +2099 -0
- package/src/llama.cpp/ggml-backend.h +233 -0
- package/src/llama.cpp/ggml-common.h +1853 -0
- package/src/llama.cpp/ggml-cuda.h +43 -0
- package/src/llama.cpp/ggml-impl.h +265 -0
- package/src/llama.cpp/ggml-kompute.cpp +2006 -0
- package/src/llama.cpp/ggml-kompute.h +46 -0
- package/src/llama.cpp/ggml-metal.h +66 -0
- package/src/llama.cpp/ggml-mpi.c +216 -0
- package/src/llama.cpp/ggml-mpi.h +39 -0
- package/src/llama.cpp/ggml-opencl.cpp +2301 -0
- package/src/llama.cpp/ggml-opencl.h +36 -0
- package/src/llama.cpp/ggml-quants.c +12678 -0
- package/src/llama.cpp/ggml-quants.h +133 -0
- package/src/llama.cpp/ggml-sycl.cpp +17882 -0
- package/src/llama.cpp/ggml-sycl.h +49 -0
- package/src/llama.cpp/ggml-vulkan-shaders.hpp +69849 -0
- package/src/llama.cpp/ggml-vulkan.cpp +6442 -0
- package/src/llama.cpp/ggml-vulkan.h +29 -0
- package/src/llama.cpp/ggml.c +21819 -0
- package/src/llama.cpp/ggml.h +2403 -0
- package/src/llama.cpp/llama.cpp +17468 -0
- package/src/llama.cpp/llama.h +1117 -0
- package/src/llama.cpp/pocs/CMakeLists.txt +12 -0
- package/src/llama.cpp/pocs/vdot/CMakeLists.txt +9 -0
- package/src/llama.cpp/pocs/vdot/q8dot.cpp +172 -0
- package/src/llama.cpp/pocs/vdot/vdot.cpp +310 -0
- package/src/llama.cpp/prompts/LLM-questions.txt +49 -0
- package/src/llama.cpp/prompts/alpaca.txt +1 -0
- package/src/llama.cpp/prompts/assistant.txt +31 -0
- package/src/llama.cpp/prompts/chat-with-baichuan.txt +4 -0
- package/src/llama.cpp/prompts/chat-with-bob.txt +7 -0
- package/src/llama.cpp/prompts/chat-with-qwen.txt +1 -0
- package/src/llama.cpp/prompts/chat-with-vicuna-v0.txt +7 -0
- package/src/llama.cpp/prompts/chat-with-vicuna-v1.txt +7 -0
- package/src/llama.cpp/prompts/chat.txt +28 -0
- package/src/llama.cpp/prompts/dan-modified.txt +1 -0
- package/src/llama.cpp/prompts/dan.txt +1 -0
- package/src/llama.cpp/prompts/mnemonics.txt +93 -0
- package/src/llama.cpp/prompts/parallel-questions.txt +43 -0
- package/src/llama.cpp/prompts/reason-act.txt +18 -0
- package/src/llama.cpp/requirements/requirements-convert-hf-to-gguf.txt +3 -0
- package/src/llama.cpp/requirements/requirements-convert-llama-ggml-to-gguf.txt +1 -0
- package/src/llama.cpp/requirements/requirements-convert-lora-to-ggml.txt +2 -0
- package/src/llama.cpp/requirements/requirements-convert-persimmon-to-gguf.txt +2 -0
- package/src/llama.cpp/requirements/requirements-convert.txt +5 -0
- package/src/llama.cpp/requirements.txt +12 -0
- package/src/llama.cpp/scripts/gen-build-info-cpp.cmake +24 -0
- package/src/llama.cpp/scripts/xxd.cmake +16 -0
- package/src/llama.cpp/sgemm.cpp +999 -0
- package/src/llama.cpp/sgemm.h +12 -0
- package/src/llama.cpp/tests/CMakeLists.txt +78 -0
- package/src/llama.cpp/tests/get-model.cpp +21 -0
- package/src/llama.cpp/tests/get-model.h +2 -0
- package/src/llama.cpp/tests/test-autorelease.cpp +24 -0
- package/src/llama.cpp/tests/test-backend-ops.cpp +2266 -0
- package/src/llama.cpp/tests/test-c.c +7 -0
- package/src/llama.cpp/tests/test-chat-template.cpp +107 -0
- package/src/llama.cpp/tests/test-double-float.cpp +57 -0
- package/src/llama.cpp/tests/test-grad0.cpp +1606 -0
- package/src/llama.cpp/tests/test-grammar-integration.cpp +243 -0
- package/src/llama.cpp/tests/test-grammar-parser.cpp +250 -0
- package/src/llama.cpp/tests/test-json-schema-to-grammar.cpp +899 -0
- package/src/llama.cpp/tests/test-llama-grammar.cpp +402 -0
- package/src/llama.cpp/tests/test-model-load-cancel.cpp +27 -0
- package/src/llama.cpp/tests/test-opt.cpp +181 -0
- package/src/llama.cpp/tests/test-quantize-fns.cpp +185 -0
- package/src/llama.cpp/tests/test-quantize-perf.cpp +363 -0
- package/src/llama.cpp/tests/test-rope.cpp +221 -0
- package/src/llama.cpp/tests/test-sampling.cpp +301 -0
- package/src/llama.cpp/tests/test-tokenizer-0-falcon.cpp +187 -0
- package/src/llama.cpp/tests/test-tokenizer-0-llama.cpp +190 -0
- package/src/llama.cpp/tests/test-tokenizer-1-bpe.cpp +123 -0
- package/src/llama.cpp/tests/test-tokenizer-1-llama.cpp +111 -0
- package/src/llama.cpp/unicode-data.cpp +1651 -0
- package/src/llama.cpp/unicode-data.h +16 -0
- package/src/llama.cpp/unicode.cpp +277 -0
- package/src/llama.cpp/unicode.h +28 -0
|
@@ -0,0 +1,767 @@
|
|
|
1
|
+
#include "common.h"
|
|
2
|
+
|
|
3
|
+
#include "console.h"
|
|
4
|
+
#include "llama.h"
|
|
5
|
+
#include "grammar-parser.h"
|
|
6
|
+
|
|
7
|
+
#include <cassert>
|
|
8
|
+
#include <cinttypes>
|
|
9
|
+
#include <cmath>
|
|
10
|
+
#include <cstdio>
|
|
11
|
+
#include <cstring>
|
|
12
|
+
#include <ctime>
|
|
13
|
+
#include <fstream>
|
|
14
|
+
#include <iostream>
|
|
15
|
+
#include <sstream>
|
|
16
|
+
#include <string>
|
|
17
|
+
#include <vector>
|
|
18
|
+
|
|
19
|
+
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
|
|
20
|
+
#include <signal.h>
|
|
21
|
+
#include <unistd.h>
|
|
22
|
+
#elif defined (_WIN32)
|
|
23
|
+
#define WIN32_LEAN_AND_MEAN
|
|
24
|
+
#ifndef NOMINMAX
|
|
25
|
+
#define NOMINMAX
|
|
26
|
+
#endif
|
|
27
|
+
#include <windows.h>
|
|
28
|
+
#include <signal.h>
|
|
29
|
+
#endif
|
|
30
|
+
|
|
31
|
+
#if defined(_MSC_VER)
|
|
32
|
+
#pragma warning(disable: 4244 4267) // possible loss of data
|
|
33
|
+
#endif
|
|
34
|
+
|
|
35
|
+
static llama_context ** g_ctx;
|
|
36
|
+
static llama_model ** g_model;
|
|
37
|
+
static gpt_params * g_params;
|
|
38
|
+
static std::vector<llama_token> * g_input_tokens;
|
|
39
|
+
static std::ostringstream * g_output_ss;
|
|
40
|
+
static std::vector<llama_token> * g_output_tokens;
|
|
41
|
+
|
|
42
|
+
static bool is_interacting = false;
|
|
43
|
+
|
|
44
|
+
static void write_logfile(
|
|
45
|
+
const llama_context * ctx, const gpt_params & params, const llama_model * model,
|
|
46
|
+
const std::vector<llama_token> & input_tokens, const std::string & output,
|
|
47
|
+
const std::vector<llama_token> & output_tokens
|
|
48
|
+
) {
|
|
49
|
+
if (params.logdir.empty()) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const std::string timestamp = get_sortable_timestamp();
|
|
54
|
+
|
|
55
|
+
const bool success = create_directory_with_parents(params.logdir);
|
|
56
|
+
if (!success) {
|
|
57
|
+
fprintf(stderr, "%s: warning: failed to create logdir %s, cannot write logfile\n",
|
|
58
|
+
__func__, params.logdir.c_str());
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const std::string logfile_path = params.logdir + timestamp + ".yml";
|
|
63
|
+
FILE * logfile = fopen(logfile_path.c_str(), "w");
|
|
64
|
+
|
|
65
|
+
if (logfile == NULL) {
|
|
66
|
+
fprintf(stderr, "%s: failed to open logfile %s\n", __func__, logfile_path.c_str());
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
fprintf(logfile, "binary: infill\n");
|
|
71
|
+
char model_desc[128];
|
|
72
|
+
llama_model_desc(model, model_desc, sizeof(model_desc));
|
|
73
|
+
dump_non_result_info_yaml(logfile, params, ctx, timestamp, input_tokens, model_desc);
|
|
74
|
+
|
|
75
|
+
fprintf(logfile, "\n");
|
|
76
|
+
fprintf(logfile, "######################\n");
|
|
77
|
+
fprintf(logfile, "# Generation Results #\n");
|
|
78
|
+
fprintf(logfile, "######################\n");
|
|
79
|
+
fprintf(logfile, "\n");
|
|
80
|
+
|
|
81
|
+
dump_string_yaml_multiline(logfile, "output", output.c_str());
|
|
82
|
+
dump_vector_int_yaml(logfile, "output_tokens", output_tokens);
|
|
83
|
+
|
|
84
|
+
llama_dump_timing_info_yaml(logfile, ctx);
|
|
85
|
+
fclose(logfile);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)) || defined (_WIN32)
|
|
89
|
+
static void sigint_handler(int signo) {
|
|
90
|
+
if (signo == SIGINT) {
|
|
91
|
+
if (!is_interacting) {
|
|
92
|
+
is_interacting = true;
|
|
93
|
+
} else {
|
|
94
|
+
console::cleanup();
|
|
95
|
+
printf("\n");
|
|
96
|
+
llama_print_timings(*g_ctx);
|
|
97
|
+
write_logfile(*g_ctx, *g_params, *g_model, *g_input_tokens, g_output_ss->str(), *g_output_tokens);
|
|
98
|
+
_exit(130);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
#endif
|
|
103
|
+
|
|
104
|
+
int main(int argc, char ** argv) {
|
|
105
|
+
gpt_params params;
|
|
106
|
+
llama_sampling_params & sparams = params.sparams;
|
|
107
|
+
g_params = ¶ms;
|
|
108
|
+
|
|
109
|
+
if (!gpt_params_parse(argc, argv, params)) {
|
|
110
|
+
return 1;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
#ifndef LOG_DISABLE_LOGS
|
|
114
|
+
log_set_target(log_filename_generator("infill", "log"));
|
|
115
|
+
LOG_TEE("Log start\n");
|
|
116
|
+
log_dump_cmdline(argc, argv);
|
|
117
|
+
#endif // LOG_DISABLE_LOGS
|
|
118
|
+
|
|
119
|
+
console::init(params.simple_io, params.use_color);
|
|
120
|
+
atexit([]() { console::cleanup(); });
|
|
121
|
+
|
|
122
|
+
if (params.logits_all) {
|
|
123
|
+
printf("\n************\n");
|
|
124
|
+
printf("%s: please use the 'perplexity' tool for perplexity calculations\n", __func__);
|
|
125
|
+
printf("************\n\n");
|
|
126
|
+
|
|
127
|
+
return 0;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (params.embedding) {
|
|
131
|
+
printf("\n************\n");
|
|
132
|
+
printf("%s: please use the 'embedding' tool for embedding calculations\n", __func__);
|
|
133
|
+
printf("************\n\n");
|
|
134
|
+
|
|
135
|
+
return 0;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (params.n_ctx != 0 && params.n_ctx < 8) {
|
|
139
|
+
LOG_TEE("%s: warning: minimum context size is 8, using minimum size.\n", __func__);
|
|
140
|
+
params.n_ctx = 8;
|
|
141
|
+
}
|
|
142
|
+
if (params.instruct) {
|
|
143
|
+
printf("\n************\n");
|
|
144
|
+
printf("%s: please use the 'main' tool for instruct mode\n", __func__);
|
|
145
|
+
printf("************\n\n");
|
|
146
|
+
|
|
147
|
+
return 0;
|
|
148
|
+
}
|
|
149
|
+
if (params.chatml) {
|
|
150
|
+
printf("\n************\n");
|
|
151
|
+
printf("%s: please use the 'main' tool for chatml mode\n", __func__);
|
|
152
|
+
printf("************\n\n");
|
|
153
|
+
|
|
154
|
+
return 0;
|
|
155
|
+
}
|
|
156
|
+
if (!params.antiprompt.empty()) {
|
|
157
|
+
printf("\n************\n");
|
|
158
|
+
printf("%s: please use the 'main' tool for antiprompt mode\n", __func__);
|
|
159
|
+
printf("************\n\n");
|
|
160
|
+
|
|
161
|
+
return 0;
|
|
162
|
+
}
|
|
163
|
+
if (!params.interactive_first && (params.input_prefix.empty() && params.input_suffix.empty())) {
|
|
164
|
+
printf("\n************\n");
|
|
165
|
+
printf("%s: please use '--interactive_first' or specify '--in_prefix' and/or '--in_suffix'\n", __func__);
|
|
166
|
+
printf("************\n\n");
|
|
167
|
+
|
|
168
|
+
return 0;
|
|
169
|
+
}
|
|
170
|
+
if (params.random_prompt) {
|
|
171
|
+
printf("\n************\n");
|
|
172
|
+
printf("%s: please use the 'main' tool for random prompt mode\n", __func__);
|
|
173
|
+
printf("************\n\n");
|
|
174
|
+
|
|
175
|
+
return 0;
|
|
176
|
+
}
|
|
177
|
+
if (!params.path_prompt_cache.empty()) {
|
|
178
|
+
printf("\n************\n");
|
|
179
|
+
printf("%s: infill does not support prompt caching\n", __func__);
|
|
180
|
+
printf("************\n\n");
|
|
181
|
+
|
|
182
|
+
return 0;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (params.rope_freq_base != 0.0) {
|
|
186
|
+
LOG_TEE("%s: warning: changing RoPE frequency base to %g.\n", __func__, params.rope_freq_base);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (params.rope_freq_scale != 0.0) {
|
|
190
|
+
LOG_TEE("%s: warning: scaling RoPE frequency by %g.\n", __func__, params.rope_freq_scale);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
LOG_TEE("%s: build = %d (%s)\n", __func__, LLAMA_BUILD_NUMBER, LLAMA_COMMIT);
|
|
194
|
+
LOG_TEE("%s: built with %s for %s\n", __func__, LLAMA_COMPILER, LLAMA_BUILD_TARGET);
|
|
195
|
+
|
|
196
|
+
if (params.seed == LLAMA_DEFAULT_SEED) {
|
|
197
|
+
params.seed = time(NULL);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
LOG_TEE("%s: seed = %u\n", __func__, params.seed);
|
|
201
|
+
|
|
202
|
+
std::mt19937 rng(params.seed);
|
|
203
|
+
|
|
204
|
+
LOG("%s: llama backend init\n", __func__);
|
|
205
|
+
llama_backend_init();
|
|
206
|
+
llama_numa_init(params.numa);
|
|
207
|
+
|
|
208
|
+
llama_model * model;
|
|
209
|
+
llama_context * ctx;
|
|
210
|
+
llama_context * ctx_guidance = NULL;
|
|
211
|
+
g_model = &model;
|
|
212
|
+
g_ctx = &ctx;
|
|
213
|
+
|
|
214
|
+
// load the model and apply lora adapter, if any
|
|
215
|
+
LOG("%s: load the model and apply lora adapter, if any\n", __func__);
|
|
216
|
+
std::tie(model, ctx) = llama_init_from_gpt_params(params);
|
|
217
|
+
if (sparams.cfg_scale > 1.f) {
|
|
218
|
+
struct llama_context_params lparams = llama_context_params_from_gpt_params(params);
|
|
219
|
+
ctx_guidance = llama_new_context_with_model(model, lparams);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
if (model == NULL) {
|
|
223
|
+
LOG_TEE("%s: error: unable to load model\n", __func__);
|
|
224
|
+
return 1;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const int n_ctx_train = llama_n_ctx_train(model);
|
|
228
|
+
const int n_ctx = llama_n_ctx(ctx);
|
|
229
|
+
LOG("n_ctx: %d\n", n_ctx);
|
|
230
|
+
|
|
231
|
+
if (n_ctx > n_ctx_train) {
|
|
232
|
+
LOG_TEE("%s: warning: model was trained on only %d context tokens (%d specified)\n",
|
|
233
|
+
__func__, n_ctx_train, n_ctx);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// print system information
|
|
237
|
+
{
|
|
238
|
+
LOG_TEE("\n");
|
|
239
|
+
LOG_TEE("%s\n", get_system_info(params).c_str());
|
|
240
|
+
}
|
|
241
|
+
const bool add_bos = llama_should_add_bos_token(model);
|
|
242
|
+
GGML_ASSERT(llama_add_eos_token(model) != 1);
|
|
243
|
+
LOG("add_bos: %d\n", add_bos);
|
|
244
|
+
|
|
245
|
+
bool suff_rm_leading_spc = params.escape;
|
|
246
|
+
if (suff_rm_leading_spc && params.input_suffix.find_first_of(' ') == 0 && params.input_suffix.size() > 1) {
|
|
247
|
+
params.input_suffix.erase(0, 1);
|
|
248
|
+
suff_rm_leading_spc = false;
|
|
249
|
+
}
|
|
250
|
+
std::vector<llama_token> embd_inp;
|
|
251
|
+
std::vector<llama_token> inp_pfx = ::llama_tokenize(ctx, params.input_prefix, false);
|
|
252
|
+
std::vector<llama_token> inp_sfx = ::llama_tokenize(ctx, params.input_suffix, false);
|
|
253
|
+
const int space_token = 29871;
|
|
254
|
+
if (suff_rm_leading_spc && inp_sfx[0] == space_token) {
|
|
255
|
+
inp_sfx.erase(inp_sfx.begin());
|
|
256
|
+
}
|
|
257
|
+
inp_pfx.insert(inp_pfx.begin(), llama_token_prefix(model));
|
|
258
|
+
if (add_bos) {
|
|
259
|
+
inp_pfx.insert(inp_pfx.begin(), llama_token_bos(model));
|
|
260
|
+
}
|
|
261
|
+
inp_sfx.insert(inp_sfx.begin(), llama_token_suffix(model));
|
|
262
|
+
embd_inp = inp_pfx;
|
|
263
|
+
embd_inp.insert(embd_inp.end(), inp_sfx.begin(), inp_sfx.end());
|
|
264
|
+
embd_inp.push_back(llama_token_middle(model));
|
|
265
|
+
|
|
266
|
+
LOG("prefix: \"%s\"\n", log_tostr(params.input_prefix));
|
|
267
|
+
LOG("suffix: \"%s\"\n", log_tostr(params.input_suffix));
|
|
268
|
+
LOG("tokens: %s\n", LOG_TOKENS_TOSTR_PRETTY(ctx, embd_inp).c_str());
|
|
269
|
+
|
|
270
|
+
// Should not run without any tokens
|
|
271
|
+
if (embd_inp.empty()) {
|
|
272
|
+
embd_inp.push_back(llama_token_bos(model));
|
|
273
|
+
LOG("embd_inp was considered empty and bos was added: %s\n", LOG_TOKENS_TOSTR_PRETTY(ctx, embd_inp).c_str());
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// Tokenize negative prompt
|
|
277
|
+
std::vector<llama_token> guidance_inp;
|
|
278
|
+
int guidance_offset = 0;
|
|
279
|
+
int original_prompt_len = 0;
|
|
280
|
+
if (ctx_guidance) {
|
|
281
|
+
LOG("cfg_negative_prompt: \"%s\"\n", log_tostr(sparams.cfg_negative_prompt));
|
|
282
|
+
|
|
283
|
+
guidance_inp = ::llama_tokenize(ctx_guidance, sparams.cfg_negative_prompt, true);
|
|
284
|
+
LOG("guidance_inp tokenized: %s\n", LOG_TOKENS_TOSTR_PRETTY(ctx_guidance, guidance_inp).c_str());
|
|
285
|
+
|
|
286
|
+
std::vector<llama_token> original_inp = ::llama_tokenize(ctx, params.prompt, true);
|
|
287
|
+
LOG("original_inp tokenized: %s\n", LOG_TOKENS_TOSTR_PRETTY(ctx, original_inp).c_str());
|
|
288
|
+
|
|
289
|
+
original_prompt_len = original_inp.size();
|
|
290
|
+
guidance_offset = (int)guidance_inp.size() - original_prompt_len;
|
|
291
|
+
LOG("original_prompt_len: %s", log_tostr(original_prompt_len));
|
|
292
|
+
LOG("guidance_offset: %s", log_tostr(guidance_offset));
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
if ((int) embd_inp.size() > n_ctx - 4) {
|
|
296
|
+
LOG_TEE("%s: error: prompt is too long (%d tokens, max %d)\n", __func__, (int) embd_inp.size(), n_ctx - 4);
|
|
297
|
+
return 1;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// number of tokens to keep when resetting context
|
|
301
|
+
if (params.n_keep < 0 || params.n_keep > (int) embd_inp.size()) {
|
|
302
|
+
params.n_keep = (int)embd_inp.size();
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
LOG("inp_pfx: %s\n", LOG_TOKENS_TOSTR_PRETTY(ctx, inp_pfx).c_str());
|
|
306
|
+
LOG("inp_sfx: %s\n", LOG_TOKENS_TOSTR_PRETTY(ctx, inp_sfx).c_str());
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
// enable interactive mode if interactive start is specified
|
|
310
|
+
if (params.interactive_first) {
|
|
311
|
+
params.interactive = true;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
if (params.verbose_prompt) {
|
|
315
|
+
LOG_TEE("\n");
|
|
316
|
+
LOG_TEE("%s: prompt: '%s'\n", __func__, params.prompt.c_str());
|
|
317
|
+
LOG_TEE("%s: number of tokens in prompt = %zu\n", __func__, embd_inp.size());
|
|
318
|
+
for (int i = 0; i < (int) embd_inp.size(); i++) {
|
|
319
|
+
LOG_TEE("%6d -> '%s'\n", embd_inp[i], llama_token_to_piece(ctx, embd_inp[i]).c_str());
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
if (ctx_guidance) {
|
|
323
|
+
LOG_TEE("\n");
|
|
324
|
+
LOG_TEE("%s: negative prompt: '%s'\n", __func__, sparams.cfg_negative_prompt.c_str());
|
|
325
|
+
LOG_TEE("%s: number of tokens in negative prompt = %zu\n", __func__, guidance_inp.size());
|
|
326
|
+
for (int i = 0; i < (int) guidance_inp.size(); i++) {
|
|
327
|
+
LOG_TEE("%6d -> '%s'\n", guidance_inp[i], llama_token_to_piece(ctx, guidance_inp[i]).c_str());
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
if (params.n_keep > 0) {
|
|
332
|
+
LOG_TEE("%s: static prompt based on n_keep: '", __func__);
|
|
333
|
+
for (int i = 0; i < params.n_keep; i++) {
|
|
334
|
+
LOG_TEE("%s", llama_token_to_piece(ctx, embd_inp[i]).c_str());
|
|
335
|
+
}
|
|
336
|
+
LOG_TEE("'\n");
|
|
337
|
+
}
|
|
338
|
+
LOG_TEE("\n");
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
if (params.interactive) {
|
|
342
|
+
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
|
|
343
|
+
struct sigaction sigint_action;
|
|
344
|
+
sigint_action.sa_handler = sigint_handler;
|
|
345
|
+
sigemptyset (&sigint_action.sa_mask);
|
|
346
|
+
sigint_action.sa_flags = 0;
|
|
347
|
+
sigaction(SIGINT, &sigint_action, NULL);
|
|
348
|
+
#elif defined (_WIN32)
|
|
349
|
+
auto console_ctrl_handler = +[](DWORD ctrl_type) -> BOOL {
|
|
350
|
+
return (ctrl_type == CTRL_C_EVENT) ? (sigint_handler(SIGINT), true) : false;
|
|
351
|
+
};
|
|
352
|
+
SetConsoleCtrlHandler(reinterpret_cast<PHANDLER_ROUTINE>(console_ctrl_handler), true);
|
|
353
|
+
#endif
|
|
354
|
+
|
|
355
|
+
LOG_TEE("%s: interactive mode on.\n", __func__);
|
|
356
|
+
|
|
357
|
+
if (params.input_prefix_bos) {
|
|
358
|
+
LOG_TEE("Input prefix with BOS\n");
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
if (!params.input_prefix.empty()) {
|
|
362
|
+
LOG_TEE("Input prefix: '%s'\n", params.input_prefix.c_str());
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
if (!params.input_suffix.empty()) {
|
|
366
|
+
LOG_TEE("Input suffix: '%s'\n", params.input_suffix.c_str());
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
LOG_TEE("sampling: \n%s\n", llama_sampling_print(sparams).c_str());
|
|
370
|
+
LOG_TEE("generate: n_ctx = %d, n_batch = %d, n_predict = %d, n_keep = %d\n", n_ctx, params.n_batch, params.n_predict, params.n_keep);
|
|
371
|
+
LOG_TEE("\n\n");
|
|
372
|
+
|
|
373
|
+
LOG_TEE("\n##### Infill mode #####\n\n");
|
|
374
|
+
if (params.infill) {
|
|
375
|
+
printf("\n************\n");
|
|
376
|
+
printf("no need to specify '--infill', always running infill\n");
|
|
377
|
+
printf("************\n\n");
|
|
378
|
+
}
|
|
379
|
+
if (params.interactive) {
|
|
380
|
+
const char *control_message;
|
|
381
|
+
if (params.multiline_input) {
|
|
382
|
+
control_message = " - To return control to LLaMA, end your input with '\\'.\n"
|
|
383
|
+
" - To return control without starting a new line, end your input with '/'.\n";
|
|
384
|
+
} else {
|
|
385
|
+
control_message = " - Press Return to return control to LLaMA.\n"
|
|
386
|
+
" - To return control without starting a new line, end your input with '/'.\n"
|
|
387
|
+
" - If you want to submit another line, end your input with '\\'.\n";
|
|
388
|
+
}
|
|
389
|
+
LOG_TEE("== Running in interactive mode. ==\n");
|
|
390
|
+
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)) || defined (_WIN32)
|
|
391
|
+
LOG_TEE( " - Press Ctrl+C to interject at any time.\n");
|
|
392
|
+
#endif
|
|
393
|
+
LOG_TEE( "%s\n", control_message);
|
|
394
|
+
|
|
395
|
+
is_interacting = params.interactive_first;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
bool input_echo = true;
|
|
399
|
+
|
|
400
|
+
int n_past = 0;
|
|
401
|
+
int n_remain = params.n_predict;
|
|
402
|
+
int n_consumed = 0;
|
|
403
|
+
int n_past_guidance = 0;
|
|
404
|
+
|
|
405
|
+
std::vector<int> input_tokens; g_input_tokens = &input_tokens;
|
|
406
|
+
std::vector<int> output_tokens; g_output_tokens = &output_tokens;
|
|
407
|
+
std::ostringstream output_ss; g_output_ss = &output_ss;
|
|
408
|
+
|
|
409
|
+
// the first thing we will do is to output the prompt, so set color accordingly
|
|
410
|
+
console::set_display(console::prompt);
|
|
411
|
+
|
|
412
|
+
std::vector<llama_token> embd;
|
|
413
|
+
std::vector<llama_token> embd_guidance;
|
|
414
|
+
|
|
415
|
+
struct llama_sampling_context * ctx_sampling = llama_sampling_init(sparams);
|
|
416
|
+
|
|
417
|
+
while (n_remain != 0 || params.interactive) {
|
|
418
|
+
// predict
|
|
419
|
+
if (!embd.empty()) {
|
|
420
|
+
// Note: n_ctx - 4 here is to match the logic for commandline prompt handling via
|
|
421
|
+
// --prompt or --file which uses the same value.
|
|
422
|
+
int max_embd_size = n_ctx - 4;
|
|
423
|
+
|
|
424
|
+
// Ensure the input doesn't exceed the context size by truncating embd if necessary.
|
|
425
|
+
if ((int) embd.size() > max_embd_size) {
|
|
426
|
+
const int skipped_tokens = (int) embd.size() - max_embd_size;
|
|
427
|
+
embd.resize(max_embd_size);
|
|
428
|
+
|
|
429
|
+
console::set_display(console::error);
|
|
430
|
+
printf("<<input too long: skipped %d token%s>>", skipped_tokens, skipped_tokens != 1 ? "s" : "");
|
|
431
|
+
console::set_display(console::reset);
|
|
432
|
+
fflush(stdout);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
// infinite text generation via context swapping
|
|
436
|
+
// if we run out of context:
|
|
437
|
+
// - take the n_keep first tokens from the original prompt (via n_past)
|
|
438
|
+
// - take half of the last (n_ctx - n_keep) tokens and recompute the logits in batches
|
|
439
|
+
if (n_past + (int) embd.size() + std::max<int>(0, guidance_offset) > n_ctx) {
|
|
440
|
+
if (params.n_predict == -2) {
|
|
441
|
+
LOG_TEE("\n\n%s: context full and n_predict == -%d => stopping\n", __func__, params.n_predict);
|
|
442
|
+
break;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
const int n_left = n_past - params.n_keep - 1;
|
|
446
|
+
const int n_discard = n_left/2;
|
|
447
|
+
|
|
448
|
+
LOG("context full, swapping: n_past = %d, n_left = %d, n_ctx = %d, n_keep = %d, n_discard = %d\n",
|
|
449
|
+
n_past, n_left, n_ctx, params.n_keep, n_discard);
|
|
450
|
+
|
|
451
|
+
llama_kv_cache_seq_rm (ctx, 0, params.n_keep + 1 , params.n_keep + n_discard + 1);
|
|
452
|
+
llama_kv_cache_seq_add(ctx, 0, params.n_keep + 1 + n_discard, n_past, -n_discard);
|
|
453
|
+
|
|
454
|
+
n_past -= n_discard;
|
|
455
|
+
|
|
456
|
+
if (ctx_guidance) {
|
|
457
|
+
n_past_guidance -= n_discard;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
LOG("after swap: n_past = %d, n_past_guidance = %d\n", n_past, n_past_guidance);
|
|
461
|
+
|
|
462
|
+
LOG("embd: %s\n", LOG_TOKENS_TOSTR_PRETTY(ctx, embd).c_str());
|
|
463
|
+
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
// evaluate tokens in batches
|
|
467
|
+
// embd is typically prepared beforehand to fit within a batch, but not always
|
|
468
|
+
|
|
469
|
+
if (ctx_guidance) {
|
|
470
|
+
int input_size = 0;
|
|
471
|
+
llama_token * input_buf = NULL;
|
|
472
|
+
|
|
473
|
+
if (n_past_guidance < (int) guidance_inp.size()) {
|
|
474
|
+
// Guidance context should have the same data with these modifications:
|
|
475
|
+
//
|
|
476
|
+
// * Replace the initial prompt
|
|
477
|
+
// * Shift everything by guidance_offset
|
|
478
|
+
embd_guidance = guidance_inp;
|
|
479
|
+
if (embd.begin() + original_prompt_len < embd.end()) {
|
|
480
|
+
embd_guidance.insert(
|
|
481
|
+
embd_guidance.end(),
|
|
482
|
+
embd.begin() + original_prompt_len,
|
|
483
|
+
embd.end()
|
|
484
|
+
);
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
input_buf = embd_guidance.data();
|
|
488
|
+
input_size = embd_guidance.size();
|
|
489
|
+
|
|
490
|
+
LOG("guidance context: %s\n", LOG_TOKENS_TOSTR_PRETTY(ctx, embd_guidance).c_str());
|
|
491
|
+
} else {
|
|
492
|
+
input_buf = embd.data();
|
|
493
|
+
input_size = embd.size();
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
for (int i = 0; i < input_size; i += params.n_batch) {
|
|
497
|
+
int n_eval = std::min(input_size - i, params.n_batch);
|
|
498
|
+
if (llama_decode(ctx_guidance, llama_batch_get_one(input_buf + i, n_eval, n_past_guidance, 0))) {
|
|
499
|
+
LOG_TEE("%s : failed to eval\n", __func__);
|
|
500
|
+
return 1;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
n_past_guidance += n_eval;
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
for (int i = 0; i < (int) embd.size(); i += params.n_batch) {
|
|
508
|
+
int n_eval = (int) embd.size() - i;
|
|
509
|
+
if (n_eval > params.n_batch) {
|
|
510
|
+
n_eval = params.n_batch;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
LOG("eval: %s\n", LOG_TOKENS_TOSTR_PRETTY(ctx, embd).c_str());
|
|
514
|
+
|
|
515
|
+
if (llama_decode(ctx, llama_batch_get_one(&embd[i], n_eval, n_past, 0))) {
|
|
516
|
+
LOG_TEE("%s : failed to eval\n", __func__);
|
|
517
|
+
return 1;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
n_past += n_eval;
|
|
521
|
+
|
|
522
|
+
LOG("n_past = %d\n", n_past);
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
embd.clear();
|
|
528
|
+
embd_guidance.clear();
|
|
529
|
+
|
|
530
|
+
if ((int) embd_inp.size() <= n_consumed && !is_interacting) {
|
|
531
|
+
|
|
532
|
+
const llama_token id = llama_sampling_sample(ctx_sampling, ctx, ctx_guidance);
|
|
533
|
+
|
|
534
|
+
llama_sampling_accept(ctx_sampling, ctx, id, true);
|
|
535
|
+
|
|
536
|
+
LOG("last: %s\n", LOG_TOKENS_TOSTR_PRETTY(ctx, ctx_sampling->prev).c_str());
|
|
537
|
+
|
|
538
|
+
embd.push_back(id);
|
|
539
|
+
|
|
540
|
+
// echo this to console
|
|
541
|
+
input_echo = true;
|
|
542
|
+
|
|
543
|
+
// decrement remaining sampling budget
|
|
544
|
+
--n_remain;
|
|
545
|
+
|
|
546
|
+
LOG("n_remain: %d\n", n_remain);
|
|
547
|
+
} else {
|
|
548
|
+
// some user input remains from prompt or interaction, forward it to processing
|
|
549
|
+
LOG("embd_inp.size(): %d, n_consumed: %d\n", (int) embd_inp.size(), n_consumed);
|
|
550
|
+
while ((int) embd_inp.size() > n_consumed) {
|
|
551
|
+
embd.push_back(embd_inp[n_consumed]);
|
|
552
|
+
|
|
553
|
+
// push the prompt in the sampling context in order to apply repetition penalties later
|
|
554
|
+
// for the prompt, we don't apply grammar rules
|
|
555
|
+
llama_sampling_accept(ctx_sampling, ctx, embd_inp[n_consumed], false);
|
|
556
|
+
|
|
557
|
+
++n_consumed;
|
|
558
|
+
if ((int) embd.size() >= params.n_batch) {
|
|
559
|
+
break;
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
// display text
|
|
565
|
+
if (input_echo) {
|
|
566
|
+
for (auto id : embd) {
|
|
567
|
+
const std::string token_str = llama_token_to_piece(ctx, id);
|
|
568
|
+
printf("%s", token_str.c_str());
|
|
569
|
+
|
|
570
|
+
if (embd.size() > 1) {
|
|
571
|
+
input_tokens.push_back(id);
|
|
572
|
+
} else {
|
|
573
|
+
output_tokens.push_back(id);
|
|
574
|
+
output_ss << token_str;
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
fflush(stdout);
|
|
578
|
+
}
|
|
579
|
+
// reset color to default if we there is no pending user input
|
|
580
|
+
if (input_echo && (int) embd_inp.size() == n_consumed) {
|
|
581
|
+
console::set_display(console::reset);
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
// if not currently processing queued inputs;
|
|
585
|
+
if ((int) embd_inp.size() <= n_consumed) {
|
|
586
|
+
|
|
587
|
+
// deal with eot token in infill mode
|
|
588
|
+
if ((llama_sampling_last(ctx_sampling) == llama_token_eot(model) || is_interacting) && params.interactive){
|
|
589
|
+
if (is_interacting && !params.interactive_first) {
|
|
590
|
+
// print an eot token
|
|
591
|
+
printf("%s", llama_token_to_piece(ctx, llama_token_eot(model)).c_str());
|
|
592
|
+
}
|
|
593
|
+
fflush(stdout);
|
|
594
|
+
printf("\n");
|
|
595
|
+
console::set_display(console::user_input);
|
|
596
|
+
std::string buffer;
|
|
597
|
+
std::string line;
|
|
598
|
+
bool another_line=true;
|
|
599
|
+
// set a new prefix via stdin
|
|
600
|
+
do {
|
|
601
|
+
another_line = console::readline(line, params.multiline_input);
|
|
602
|
+
buffer += line;
|
|
603
|
+
} while (another_line);
|
|
604
|
+
// check if we got an empty line, if so we use the old input
|
|
605
|
+
if (!buffer.empty() && !(buffer.length() == 1 && buffer[0] == '\n')) {
|
|
606
|
+
params.input_prefix = buffer;
|
|
607
|
+
}
|
|
608
|
+
buffer.clear();
|
|
609
|
+
// set a new suffix via stdin
|
|
610
|
+
do {
|
|
611
|
+
another_line = console::readline(line, params.multiline_input);
|
|
612
|
+
buffer += line;
|
|
613
|
+
} while (another_line);
|
|
614
|
+
// check if we got an empty line
|
|
615
|
+
if (!buffer.empty() && !(buffer.length() == 1 && buffer[0] == '\n')) {
|
|
616
|
+
params.input_suffix = buffer;
|
|
617
|
+
}
|
|
618
|
+
buffer.clear();
|
|
619
|
+
// done taking input, reset color
|
|
620
|
+
console::set_display(console::reset);
|
|
621
|
+
|
|
622
|
+
if (params.escape) {
|
|
623
|
+
//process escape sequences, for the initial prompt this is done in common.cpp when we load the params, but for the interactive mode we need to do it here
|
|
624
|
+
process_escapes(params.input_prefix);
|
|
625
|
+
process_escapes(params.input_suffix);
|
|
626
|
+
}
|
|
627
|
+
suff_rm_leading_spc = params.escape;
|
|
628
|
+
if (suff_rm_leading_spc && params.input_suffix.find_first_of(' ') == 0 && params.input_suffix.size() > 1) {
|
|
629
|
+
params.input_suffix.erase(0, 1);
|
|
630
|
+
suff_rm_leading_spc = false;
|
|
631
|
+
}
|
|
632
|
+
// tokenize new prefix and suffix
|
|
633
|
+
std::vector<llama_token> inp_pfx = ::llama_tokenize(ctx, params.input_prefix, false);
|
|
634
|
+
std::vector<llama_token> inp_sfx = ::llama_tokenize(ctx, params.input_suffix, false);
|
|
635
|
+
if (suff_rm_leading_spc && inp_sfx[0] == space_token) {
|
|
636
|
+
inp_sfx.erase(inp_sfx.begin());
|
|
637
|
+
}
|
|
638
|
+
inp_pfx.insert(inp_pfx.begin(), llama_token_prefix(model));
|
|
639
|
+
if (add_bos) {
|
|
640
|
+
inp_pfx.insert(inp_pfx.begin(), llama_token_bos(model));
|
|
641
|
+
}
|
|
642
|
+
inp_sfx.insert(inp_sfx.begin(), llama_token_suffix(model));
|
|
643
|
+
embd_inp = inp_pfx;
|
|
644
|
+
embd_inp.insert(embd_inp.end(), inp_sfx.begin(), inp_sfx.end());
|
|
645
|
+
embd_inp.push_back(llama_token_middle(model));
|
|
646
|
+
embd.clear();
|
|
647
|
+
embd_guidance.clear();
|
|
648
|
+
n_remain = params.n_predict;
|
|
649
|
+
n_past = 0;
|
|
650
|
+
n_consumed = 0;
|
|
651
|
+
// LOG_TEE("took new input\n");
|
|
652
|
+
is_interacting = false;
|
|
653
|
+
}
|
|
654
|
+
// deal with end of generation tokens in interactive mode
|
|
655
|
+
else if (llama_token_is_eog(model, llama_sampling_last(ctx_sampling))) {
|
|
656
|
+
LOG("found EOS token\n");
|
|
657
|
+
|
|
658
|
+
if (params.interactive) {
|
|
659
|
+
|
|
660
|
+
is_interacting = true;
|
|
661
|
+
printf("\n");
|
|
662
|
+
console::set_display(console::user_input);
|
|
663
|
+
fflush(stdout);
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
if (n_past > 0 && is_interacting && !params.interactive) {
|
|
668
|
+
LOG("waiting for user input\n");
|
|
669
|
+
|
|
670
|
+
if (params.input_prefix_bos) {
|
|
671
|
+
LOG("adding input prefix BOS token\n");
|
|
672
|
+
embd_inp.push_back(llama_token_bos(model));
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
std::string buffer;
|
|
676
|
+
if (!params.input_prefix.empty()) {
|
|
677
|
+
LOG("appending input prefix: '%s'\n", params.input_prefix.c_str());
|
|
678
|
+
buffer += params.input_prefix;
|
|
679
|
+
printf("%s", buffer.c_str());
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
std::string line;
|
|
683
|
+
bool another_line = true;
|
|
684
|
+
do {
|
|
685
|
+
another_line = console::readline(line, params.multiline_input);
|
|
686
|
+
buffer += line;
|
|
687
|
+
} while (another_line);
|
|
688
|
+
|
|
689
|
+
// done taking input, reset color
|
|
690
|
+
console::set_display(console::reset);
|
|
691
|
+
|
|
692
|
+
// Add tokens to embd only if the input buffer is non-empty
|
|
693
|
+
// Entering a empty line lets the user pass control back
|
|
694
|
+
if (buffer.length() > 1) {
|
|
695
|
+
// append input suffix if any
|
|
696
|
+
if (!params.input_suffix.empty()) {
|
|
697
|
+
LOG("appending input suffix: '%s'\n", params.input_suffix.c_str());
|
|
698
|
+
buffer += params.input_suffix;
|
|
699
|
+
printf("%s", params.input_suffix.c_str());
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
LOG("buffer: '%s'\n", buffer.c_str());
|
|
703
|
+
|
|
704
|
+
const size_t original_size = embd_inp.size();
|
|
705
|
+
|
|
706
|
+
const auto line_inp = ::llama_tokenize(ctx, buffer, false);
|
|
707
|
+
LOG("input tokens: %s\n", LOG_TOKENS_TOSTR_PRETTY(ctx, line_inp).c_str());
|
|
708
|
+
|
|
709
|
+
embd_inp.insert(embd_inp.end(), line_inp.begin(), line_inp.end());
|
|
710
|
+
|
|
711
|
+
for (size_t i = original_size; i < embd_inp.size(); ++i) {
|
|
712
|
+
const llama_token token = embd_inp[i];
|
|
713
|
+
output_tokens.push_back(token);
|
|
714
|
+
output_ss << llama_token_to_piece(ctx, token);
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
n_remain -= line_inp.size();
|
|
718
|
+
LOG("n_remain: %d\n", n_remain);
|
|
719
|
+
} else {
|
|
720
|
+
LOG("empty line, passing control back\n");
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
input_echo = false; // do not echo this again
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
if (n_past > 0) {
|
|
727
|
+
if (is_interacting) {
|
|
728
|
+
llama_sampling_reset(ctx_sampling);
|
|
729
|
+
}
|
|
730
|
+
is_interacting = false;
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
// end of generation
|
|
735
|
+
if (!embd.empty() && llama_token_is_eog(model, embd.back()) && !params.interactive) {
|
|
736
|
+
break;
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
// In interactive mode, respect the maximum number of tokens and drop back to user input when reached.
|
|
740
|
+
// We skip this logic when n_predict == -1 (infinite) or -2 (stop at context size).
|
|
741
|
+
if (params.interactive && n_remain <= 0 && params.n_predict >= 0) {
|
|
742
|
+
n_remain = params.n_predict;
|
|
743
|
+
is_interacting = true;
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
if (!params.interactive && n_remain <= 0) {
|
|
747
|
+
printf("%s", llama_token_to_piece(ctx, llama_token_eot(model)).c_str());
|
|
748
|
+
fflush(stdout);
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
llama_print_timings(ctx);
|
|
752
|
+
write_logfile(ctx, params, model, input_tokens, output_ss.str(), output_tokens);
|
|
753
|
+
|
|
754
|
+
if (ctx_guidance) { llama_free(ctx_guidance); }
|
|
755
|
+
llama_free(ctx);
|
|
756
|
+
llama_free_model(model);
|
|
757
|
+
|
|
758
|
+
llama_sampling_free(ctx_sampling);
|
|
759
|
+
llama_backend_free();
|
|
760
|
+
|
|
761
|
+
#ifndef LOG_DISABLE_LOGS
|
|
762
|
+
LOG_TEE("Log end\n");
|
|
763
|
+
#endif // LOG_DISABLE_LOGS
|
|
764
|
+
|
|
765
|
+
return 0;
|
|
766
|
+
}
|
|
767
|
+
|