@fugood/llama.node 1.4.15 → 1.5.0-rc.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/lib/binding.ts +1 -5
- package/lib/index.js +2 -2
- package/lib/index.ts +2 -2
- package/package.json +15 -15
- package/scripts/llama.cpp.patch +76 -61
- package/src/LlamaContext.cpp +20 -32
- package/src/llama.cpp/common/CMakeLists.txt +12 -0
- package/src/llama.cpp/common/arg.cpp +20 -0
- package/src/llama.cpp/common/chat.cpp +289 -34
- package/src/llama.cpp/common/chat.h +16 -13
- package/src/llama.cpp/common/common.cpp +0 -1
- package/src/llama.cpp/common/common.h +28 -25
- package/src/llama.cpp/common/jinja/caps.cpp +237 -0
- package/src/llama.cpp/common/jinja/caps.h +24 -0
- package/src/llama.cpp/common/jinja/lexer.cpp +341 -0
- package/src/llama.cpp/common/jinja/lexer.h +157 -0
- package/src/llama.cpp/common/jinja/parser.cpp +591 -0
- package/src/llama.cpp/common/jinja/parser.h +21 -0
- package/src/llama.cpp/common/jinja/runtime.cpp +865 -0
- package/src/llama.cpp/common/jinja/runtime.h +628 -0
- package/src/llama.cpp/common/jinja/string.cpp +207 -0
- package/src/llama.cpp/common/jinja/string.h +58 -0
- package/src/llama.cpp/common/jinja/utils.h +49 -0
- package/src/llama.cpp/common/jinja/value.cpp +1221 -0
- package/src/llama.cpp/common/jinja/value.h +464 -0
- package/src/llama.cpp/common/sampling.cpp +52 -19
- package/src/llama.cpp/ggml/include/ggml.h +39 -7
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.c +4 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/ops.cpp +63 -37
- package/src/llama.cpp/ggml/src/ggml-cpu/simd-mappings.h +31 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/vec.cpp +18 -0
- package/src/llama.cpp/include/llama-cpp.h +3 -1
- package/src/llama.cpp/include/llama.h +29 -2
- package/src/llama.cpp/src/llama-adapter.cpp +7 -13
- package/src/llama.cpp/src/llama-adapter.h +1 -3
- package/src/llama.cpp/src/llama-context.cpp +232 -144
- package/src/llama.cpp/src/llama-context.h +10 -0
- package/src/llama.cpp/src/llama-cparams.h +2 -0
- package/src/llama.cpp/src/llama-hparams.cpp +0 -36
- package/src/llama.cpp/src/llama-hparams.h +38 -1
- package/src/llama.cpp/src/llama-kv-cache.cpp +201 -59
- package/src/llama.cpp/src/llama-kv-cache.h +0 -2
- package/src/llama.cpp/src/llama-mmap.cpp +5 -1
- package/src/llama.cpp/src/llama-model-loader.cpp +21 -7
- package/src/llama.cpp/src/llama-model.cpp +5 -1
- package/src/llama.cpp/src/llama-model.h +3 -2
- package/src/llama.cpp/src/llama-sampling.cpp +170 -13
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
#include "jinja/string.h"
|
|
2
|
+
#include "jinja/value.h"
|
|
3
|
+
|
|
4
|
+
#include <algorithm>
|
|
5
|
+
#include <functional>
|
|
6
|
+
#include <optional>
|
|
7
|
+
#include <sstream>
|
|
8
|
+
#include <string>
|
|
9
|
+
#include <vector>
|
|
10
|
+
|
|
11
|
+
namespace jinja {
|
|
12
|
+
|
|
13
|
+
//
|
|
14
|
+
// string_part
|
|
15
|
+
//
|
|
16
|
+
|
|
17
|
+
bool string_part::is_uppercase() const {
|
|
18
|
+
for (char c : val) {
|
|
19
|
+
if (std::islower(static_cast<unsigned char>(c))) {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
bool string_part::is_lowercase() const {
|
|
27
|
+
for (char c : val) {
|
|
28
|
+
if (std::isupper(static_cast<unsigned char>(c))) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
//
|
|
36
|
+
// string
|
|
37
|
+
//
|
|
38
|
+
|
|
39
|
+
void string::mark_input() {
|
|
40
|
+
for (auto & part : parts) {
|
|
41
|
+
part.is_input = true;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
std::string string::str() const {
|
|
46
|
+
if (parts.size() == 1) {
|
|
47
|
+
return parts[0].val;
|
|
48
|
+
}
|
|
49
|
+
std::ostringstream oss;
|
|
50
|
+
for (const auto & part : parts) {
|
|
51
|
+
oss << part.val;
|
|
52
|
+
}
|
|
53
|
+
return oss.str();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
size_t string::length() const {
|
|
57
|
+
size_t len = 0;
|
|
58
|
+
for (const auto & part : parts) {
|
|
59
|
+
len += part.val.length();
|
|
60
|
+
}
|
|
61
|
+
return len;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
bool string::all_parts_are_input() const {
|
|
65
|
+
for (const auto & part : parts) {
|
|
66
|
+
if (!part.is_input) {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
bool string::is_uppercase() const {
|
|
74
|
+
for (const auto & part : parts) {
|
|
75
|
+
if (!part.is_uppercase()) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
bool string::is_lowercase() const {
|
|
83
|
+
for (const auto & part : parts) {
|
|
84
|
+
if (!part.is_lowercase()) {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// mark this string as input if other has ALL parts as input
|
|
92
|
+
void string::mark_input_based_on(const string & other) {
|
|
93
|
+
if (other.all_parts_are_input()) {
|
|
94
|
+
for (auto & part : parts) {
|
|
95
|
+
part.is_input = true;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
string string::append(const string & other) {
|
|
101
|
+
for (const auto & part : other.parts) {
|
|
102
|
+
parts.push_back(part);
|
|
103
|
+
}
|
|
104
|
+
return *this;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// in-place transformation
|
|
108
|
+
|
|
109
|
+
using transform_fn = std::function<std::string(const std::string&)>;
|
|
110
|
+
static string apply_transform(string & self, const transform_fn & fn) {
|
|
111
|
+
for (auto & part : self.parts) {
|
|
112
|
+
part.val = fn(part.val);
|
|
113
|
+
}
|
|
114
|
+
return self;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
string string::uppercase() {
|
|
118
|
+
return apply_transform(*this, [](const std::string & s) {
|
|
119
|
+
std::string res = s;
|
|
120
|
+
std::transform(res.begin(), res.end(), res.begin(), ::toupper);
|
|
121
|
+
return res;
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
string string::lowercase() {
|
|
125
|
+
return apply_transform(*this, [](const std::string & s) {
|
|
126
|
+
std::string res = s;
|
|
127
|
+
std::transform(res.begin(), res.end(), res.begin(), ::tolower);
|
|
128
|
+
return res;
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
string string::capitalize() {
|
|
132
|
+
return apply_transform(*this, [](const std::string & s) {
|
|
133
|
+
if (s.empty()) return s;
|
|
134
|
+
std::string res = s;
|
|
135
|
+
res[0] = ::toupper(static_cast<unsigned char>(res[0]));
|
|
136
|
+
std::transform(res.begin() + 1, res.end(), res.begin() + 1, ::tolower);
|
|
137
|
+
return res;
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
string string::titlecase() {
|
|
141
|
+
return apply_transform(*this, [](const std::string & s) {
|
|
142
|
+
std::string res = s;
|
|
143
|
+
bool capitalize_next = true;
|
|
144
|
+
for (char &c : res) {
|
|
145
|
+
if (isspace(static_cast<unsigned char>(c))) {
|
|
146
|
+
capitalize_next = true;
|
|
147
|
+
} else if (capitalize_next) {
|
|
148
|
+
c = ::toupper(static_cast<unsigned char>(c));
|
|
149
|
+
capitalize_next = false;
|
|
150
|
+
} else {
|
|
151
|
+
c = ::tolower(static_cast<unsigned char>(c));
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return res;
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
string string::strip(bool left, bool right, std::optional<const std::string_view> chars) {
|
|
158
|
+
static auto strip_part = [](const std::string & s, bool left, bool right, std::optional<const std::string_view> chars) -> std::string {
|
|
159
|
+
size_t start = 0;
|
|
160
|
+
size_t end = s.length();
|
|
161
|
+
auto match_char = [&chars](unsigned char c) -> bool {
|
|
162
|
+
return chars ? (*chars).find(c) != std::string::npos : isspace(c);
|
|
163
|
+
};
|
|
164
|
+
if (left) {
|
|
165
|
+
while (start < end && match_char(static_cast<unsigned char>(s[start]))) {
|
|
166
|
+
++start;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
if (right) {
|
|
170
|
+
while (end > start && match_char(static_cast<unsigned char>(s[end - 1]))) {
|
|
171
|
+
--end;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return s.substr(start, end - start);
|
|
175
|
+
};
|
|
176
|
+
if (parts.empty()) {
|
|
177
|
+
return *this;
|
|
178
|
+
}
|
|
179
|
+
if (left) {
|
|
180
|
+
for (size_t i = 0; i < parts.size(); ++i) {
|
|
181
|
+
parts[i].val = strip_part(parts[i].val, true, false, chars);
|
|
182
|
+
if (parts[i].val.empty()) {
|
|
183
|
+
// remove empty part
|
|
184
|
+
parts.erase(parts.begin() + i);
|
|
185
|
+
--i;
|
|
186
|
+
continue;
|
|
187
|
+
} else {
|
|
188
|
+
break;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
if (right) {
|
|
193
|
+
for (size_t i = parts.size(); i-- > 0;) {
|
|
194
|
+
parts[i].val = strip_part(parts[i].val, false, true, chars);
|
|
195
|
+
if (parts[i].val.empty()) {
|
|
196
|
+
// remove empty part
|
|
197
|
+
parts.erase(parts.begin() + i);
|
|
198
|
+
continue;
|
|
199
|
+
} else {
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return *this;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
} // namespace jinja
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <optional>
|
|
4
|
+
#include <string>
|
|
5
|
+
#include <vector>
|
|
6
|
+
|
|
7
|
+
namespace jinja {
|
|
8
|
+
|
|
9
|
+
// allow differentiate between user input strings and template strings
|
|
10
|
+
// transformations should handle this information as follows:
|
|
11
|
+
// - one-to-one (e.g., uppercase, lowercase): preserve is_input flag
|
|
12
|
+
// - one-to-many (e.g., strip): if input string is marked as is_input, all resulting parts should be marked as is_input
|
|
13
|
+
// - many-to-one (e.g., concat): if ALL input parts are marked as is_input, resulting part should be marked as is_input
|
|
14
|
+
struct string_part {
|
|
15
|
+
bool is_input = false; // may skip parsing special tokens if true
|
|
16
|
+
std::string val;
|
|
17
|
+
|
|
18
|
+
bool is_uppercase() const;
|
|
19
|
+
bool is_lowercase() const;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
struct string {
|
|
23
|
+
std::vector<string_part> parts;
|
|
24
|
+
string() = default;
|
|
25
|
+
string(const std::string & v, bool user_input = false) {
|
|
26
|
+
parts.push_back({user_input, v});
|
|
27
|
+
}
|
|
28
|
+
string(int v) {
|
|
29
|
+
parts.push_back({false, std::to_string(v)});
|
|
30
|
+
}
|
|
31
|
+
string(double v) {
|
|
32
|
+
parts.push_back({false, std::to_string(v)});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// mark all parts as user input
|
|
36
|
+
void mark_input();
|
|
37
|
+
|
|
38
|
+
std::string str() const;
|
|
39
|
+
size_t length() const;
|
|
40
|
+
bool all_parts_are_input() const;
|
|
41
|
+
bool is_uppercase() const;
|
|
42
|
+
bool is_lowercase() const;
|
|
43
|
+
|
|
44
|
+
// mark this string as input if other has ALL parts as input
|
|
45
|
+
void mark_input_based_on(const string & other);
|
|
46
|
+
|
|
47
|
+
string append(const string & other);
|
|
48
|
+
|
|
49
|
+
// in-place transformations
|
|
50
|
+
|
|
51
|
+
string uppercase();
|
|
52
|
+
string lowercase();
|
|
53
|
+
string capitalize();
|
|
54
|
+
string titlecase();
|
|
55
|
+
string strip(bool left, bool right, std::optional<const std::string_view> chars = std::nullopt);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
} // namespace jinja
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <string>
|
|
4
|
+
#include <sstream>
|
|
5
|
+
#include <algorithm>
|
|
6
|
+
|
|
7
|
+
namespace jinja {
|
|
8
|
+
|
|
9
|
+
static void string_replace_all(std::string & s, const std::string & search, const std::string & replace) {
|
|
10
|
+
if (search.empty()) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
std::string builder;
|
|
14
|
+
builder.reserve(s.length());
|
|
15
|
+
size_t pos = 0;
|
|
16
|
+
size_t last_pos = 0;
|
|
17
|
+
while ((pos = s.find(search, last_pos)) != std::string::npos) {
|
|
18
|
+
builder.append(s, last_pos, pos - last_pos);
|
|
19
|
+
builder.append(replace);
|
|
20
|
+
last_pos = pos + search.length();
|
|
21
|
+
}
|
|
22
|
+
builder.append(s, last_pos, std::string::npos);
|
|
23
|
+
s = std::move(builder);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// for displaying source code around error position
|
|
27
|
+
static std::string peak_source(const std::string & source, size_t pos, size_t max_peak_chars = 40) {
|
|
28
|
+
if (source.empty()) {
|
|
29
|
+
return "(no source available)";
|
|
30
|
+
}
|
|
31
|
+
std::string output;
|
|
32
|
+
size_t start = (pos >= max_peak_chars) ? (pos - max_peak_chars) : 0;
|
|
33
|
+
size_t end = std::min(pos + max_peak_chars, source.length());
|
|
34
|
+
std::string substr = source.substr(start, end - start);
|
|
35
|
+
string_replace_all(substr, "\n", "↵");
|
|
36
|
+
output += "..." + substr + "...\n";
|
|
37
|
+
std::string spaces(pos - start + 3, ' ');
|
|
38
|
+
output += spaces + "^";
|
|
39
|
+
return output;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
static std::string fmt_error_with_source(const std::string & tag, const std::string & msg, const std::string & source, size_t pos) {
|
|
43
|
+
std::ostringstream oss;
|
|
44
|
+
oss << tag << ": " << msg << "\n";
|
|
45
|
+
oss << peak_source(source, pos);
|
|
46
|
+
return oss.str();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
} // namespace jinja
|