@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,183 @@
|
|
|
1
|
+
#include "common.h"
|
|
2
|
+
#include "llama.h"
|
|
3
|
+
|
|
4
|
+
#include <cmath>
|
|
5
|
+
#include <cstdio>
|
|
6
|
+
#include <string>
|
|
7
|
+
#include <vector>
|
|
8
|
+
|
|
9
|
+
int main(int argc, char ** argv) {
|
|
10
|
+
gpt_params params;
|
|
11
|
+
|
|
12
|
+
if (argc == 1 || argv[1][0] == '-') {
|
|
13
|
+
printf("usage: %s MODEL_PATH [PROMPT]\n" , argv[0]);
|
|
14
|
+
return 1 ;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (argc >= 2) {
|
|
18
|
+
params.model = argv[1];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (argc >= 3) {
|
|
22
|
+
params.prompt = argv[2];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (params.prompt.empty()) {
|
|
26
|
+
params.prompt = "Hello my name is";
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// total length of the sequence including the prompt
|
|
30
|
+
const int n_len = 32;
|
|
31
|
+
|
|
32
|
+
// init LLM
|
|
33
|
+
|
|
34
|
+
llama_backend_init();
|
|
35
|
+
llama_numa_init(params.numa);
|
|
36
|
+
|
|
37
|
+
// initialize the model
|
|
38
|
+
|
|
39
|
+
llama_model_params model_params = llama_model_default_params();
|
|
40
|
+
|
|
41
|
+
// model_params.n_gpu_layers = 99; // offload all layers to the GPU
|
|
42
|
+
|
|
43
|
+
llama_model * model = llama_load_model_from_file(params.model.c_str(), model_params);
|
|
44
|
+
|
|
45
|
+
if (model == NULL) {
|
|
46
|
+
fprintf(stderr , "%s: error: unable to load model\n" , __func__);
|
|
47
|
+
return 1;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// initialize the context
|
|
51
|
+
|
|
52
|
+
llama_context_params ctx_params = llama_context_default_params();
|
|
53
|
+
|
|
54
|
+
ctx_params.seed = 1234;
|
|
55
|
+
ctx_params.n_ctx = 2048;
|
|
56
|
+
ctx_params.n_threads = params.n_threads;
|
|
57
|
+
ctx_params.n_threads_batch = params.n_threads_batch == -1 ? params.n_threads : params.n_threads_batch;
|
|
58
|
+
|
|
59
|
+
llama_context * ctx = llama_new_context_with_model(model, ctx_params);
|
|
60
|
+
|
|
61
|
+
if (ctx == NULL) {
|
|
62
|
+
fprintf(stderr , "%s: error: failed to create the llama_context\n" , __func__);
|
|
63
|
+
return 1;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// tokenize the prompt
|
|
67
|
+
|
|
68
|
+
std::vector<llama_token> tokens_list;
|
|
69
|
+
tokens_list = ::llama_tokenize(ctx, params.prompt, true);
|
|
70
|
+
|
|
71
|
+
const int n_ctx = llama_n_ctx(ctx);
|
|
72
|
+
const int n_kv_req = tokens_list.size() + (n_len - tokens_list.size());
|
|
73
|
+
|
|
74
|
+
LOG_TEE("\n%s: n_len = %d, n_ctx = %d, n_kv_req = %d\n", __func__, n_len, n_ctx, n_kv_req);
|
|
75
|
+
|
|
76
|
+
// make sure the KV cache is big enough to hold all the prompt and generated tokens
|
|
77
|
+
if (n_kv_req > n_ctx) {
|
|
78
|
+
LOG_TEE("%s: error: n_kv_req > n_ctx, the required KV cache size is not big enough\n", __func__);
|
|
79
|
+
LOG_TEE("%s: either reduce n_len or increase n_ctx\n", __func__);
|
|
80
|
+
return 1;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// print the prompt token-by-token
|
|
84
|
+
|
|
85
|
+
fprintf(stderr, "\n");
|
|
86
|
+
|
|
87
|
+
for (auto id : tokens_list) {
|
|
88
|
+
fprintf(stderr, "%s", llama_token_to_piece(ctx, id).c_str());
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
fflush(stderr);
|
|
92
|
+
|
|
93
|
+
// create a llama_batch with size 512
|
|
94
|
+
// we use this object to submit token data for decoding
|
|
95
|
+
|
|
96
|
+
llama_batch batch = llama_batch_init(512, 0, 1);
|
|
97
|
+
|
|
98
|
+
// evaluate the initial prompt
|
|
99
|
+
for (size_t i = 0; i < tokens_list.size(); i++) {
|
|
100
|
+
llama_batch_add(batch, tokens_list[i], i, { 0 }, false);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// llama_decode will output logits only for the last token of the prompt
|
|
104
|
+
batch.logits[batch.n_tokens - 1] = true;
|
|
105
|
+
|
|
106
|
+
if (llama_decode(ctx, batch) != 0) {
|
|
107
|
+
LOG_TEE("%s: llama_decode() failed\n", __func__);
|
|
108
|
+
return 1;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// main loop
|
|
112
|
+
|
|
113
|
+
int n_cur = batch.n_tokens;
|
|
114
|
+
int n_decode = 0;
|
|
115
|
+
|
|
116
|
+
const auto t_main_start = ggml_time_us();
|
|
117
|
+
|
|
118
|
+
while (n_cur <= n_len) {
|
|
119
|
+
// sample the next token
|
|
120
|
+
{
|
|
121
|
+
auto n_vocab = llama_n_vocab(model);
|
|
122
|
+
auto * logits = llama_get_logits_ith(ctx, batch.n_tokens - 1);
|
|
123
|
+
|
|
124
|
+
std::vector<llama_token_data> candidates;
|
|
125
|
+
candidates.reserve(n_vocab);
|
|
126
|
+
|
|
127
|
+
for (llama_token token_id = 0; token_id < n_vocab; token_id++) {
|
|
128
|
+
candidates.emplace_back(llama_token_data{ token_id, logits[token_id], 0.0f });
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
llama_token_data_array candidates_p = { candidates.data(), candidates.size(), false };
|
|
132
|
+
|
|
133
|
+
// sample the most likely token
|
|
134
|
+
const llama_token new_token_id = llama_sample_token_greedy(ctx, &candidates_p);
|
|
135
|
+
|
|
136
|
+
// is it an end of generation?
|
|
137
|
+
if (llama_token_is_eog(model, new_token_id) || n_cur == n_len) {
|
|
138
|
+
LOG_TEE("\n");
|
|
139
|
+
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
LOG_TEE("%s", llama_token_to_piece(ctx, new_token_id).c_str());
|
|
144
|
+
fflush(stdout);
|
|
145
|
+
|
|
146
|
+
// prepare the next batch
|
|
147
|
+
llama_batch_clear(batch);
|
|
148
|
+
|
|
149
|
+
// push this new token for next evaluation
|
|
150
|
+
llama_batch_add(batch, new_token_id, n_cur, { 0 }, true);
|
|
151
|
+
|
|
152
|
+
n_decode += 1;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
n_cur += 1;
|
|
156
|
+
|
|
157
|
+
// evaluate the current batch with the transformer model
|
|
158
|
+
if (llama_decode(ctx, batch)) {
|
|
159
|
+
fprintf(stderr, "%s : failed to eval, return code %d\n", __func__, 1);
|
|
160
|
+
return 1;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
LOG_TEE("\n");
|
|
165
|
+
|
|
166
|
+
const auto t_main_end = ggml_time_us();
|
|
167
|
+
|
|
168
|
+
LOG_TEE("%s: decoded %d tokens in %.2f s, speed: %.2f t/s\n",
|
|
169
|
+
__func__, n_decode, (t_main_end - t_main_start) / 1000000.0f, n_decode / ((t_main_end - t_main_start) / 1000000.0f));
|
|
170
|
+
|
|
171
|
+
llama_print_timings(ctx);
|
|
172
|
+
|
|
173
|
+
fprintf(stderr, "\n");
|
|
174
|
+
|
|
175
|
+
llama_batch_free(batch);
|
|
176
|
+
|
|
177
|
+
llama_free(ctx);
|
|
178
|
+
llama_free_model(model);
|
|
179
|
+
|
|
180
|
+
llama_backend_free();
|
|
181
|
+
|
|
182
|
+
return 0;
|
|
183
|
+
}
|