cui-llama.rn 1.1.2 → 1.1.4
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/android/src/main/CMakeLists.txt +1 -2
- package/android/src/main/jni.cpp +26 -21
- package/cpp/common.cpp +2028 -1520
- package/cpp/common.h +134 -18
- package/cpp/ggml-aarch64.c +612 -0
- package/cpp/ggml-alloc.h +2 -2
- package/cpp/ggml-backend.c +33 -6
- package/cpp/ggml-backend.h +2 -0
- package/cpp/ggml-common.h +20 -0
- package/cpp/ggml-impl.h +4 -7
- package/cpp/ggml-metal.m +63 -2
- package/cpp/ggml-quants.c +690 -2
- package/cpp/ggml-quants.h +15 -0
- package/cpp/ggml.c +1650 -317
- package/cpp/ggml.h +155 -48
- package/cpp/llama-grammar.cpp +721 -122
- package/cpp/llama-grammar.h +120 -15
- package/cpp/llama-impl.h +132 -1
- package/cpp/llama-sampling.cpp +1361 -356
- package/cpp/llama-sampling.h +20 -48
- package/cpp/llama-vocab.cpp +140 -7
- package/cpp/llama-vocab.h +3 -2
- package/cpp/llama.cpp +810 -307
- package/cpp/llama.h +213 -259
- package/cpp/rn-llama.hpp +17 -14
- package/cpp/sampling.cpp +347 -355
- package/cpp/sampling.h +106 -135
- package/cpp/sgemm.cpp +153 -0
- package/package.json +1 -1
- package/cpp/grammar-parser.cpp +0 -539
- package/cpp/grammar-parser.h +0 -29
package/cpp/sampling.cpp
CHANGED
@@ -1,464 +1,456 @@
|
|
1
|
-
#define LLAMA_API_INTERNAL
|
2
1
|
#include "sampling.h"
|
3
|
-
#include <random>
|
4
2
|
|
5
|
-
|
6
|
-
struct llama_sampling_context * result = new llama_sampling_context();
|
3
|
+
#include "common.h"
|
7
4
|
|
8
|
-
|
9
|
-
|
5
|
+
// the ring buffer works similarly to std::deque, but with a fixed capacity
|
6
|
+
// TODO: deduplicate with llama-impl.h
|
7
|
+
template<typename T>
|
8
|
+
struct ring_buffer {
|
9
|
+
ring_buffer(size_t cap) : capacity(cap), data(cap) {}
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
// will be empty (default) if there are parse errors
|
16
|
-
if (result->parsed_grammar.rules.empty()) {
|
17
|
-
fprintf(stderr, "%s: failed to parse grammar\n", __func__);
|
18
|
-
delete result;
|
19
|
-
return nullptr;
|
11
|
+
T & front() {
|
12
|
+
if (sz == 0) {
|
13
|
+
throw std::runtime_error("ring buffer is empty");
|
20
14
|
}
|
15
|
+
return data[first];
|
16
|
+
}
|
21
17
|
|
22
|
-
|
23
|
-
if (
|
24
|
-
|
25
|
-
delete result;
|
26
|
-
return nullptr;
|
18
|
+
const T & front() const {
|
19
|
+
if (sz == 0) {
|
20
|
+
throw std::runtime_error("ring buffer is empty");
|
27
21
|
}
|
22
|
+
return data[first];
|
23
|
+
}
|
28
24
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
grammar_rules.data(),
|
33
|
-
grammar_rules.size(), result->parsed_grammar.symbol_ids.at("root"));
|
34
|
-
if (grammar == nullptr) {
|
35
|
-
throw std::runtime_error("Failed to initialize llama_grammar");
|
25
|
+
T & back() {
|
26
|
+
if (sz == 0) {
|
27
|
+
throw std::runtime_error("ring buffer is empty");
|
36
28
|
}
|
37
|
-
|
29
|
+
return data[pos];
|
38
30
|
}
|
39
31
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
return result;
|
47
|
-
}
|
48
|
-
|
49
|
-
void llama_sampling_free(struct llama_sampling_context * ctx) {
|
50
|
-
if (ctx->grammar != NULL) {
|
51
|
-
llama_grammar_free(ctx->grammar);
|
32
|
+
const T & back() const {
|
33
|
+
if (sz == 0) {
|
34
|
+
throw std::runtime_error("ring buffer is empty");
|
35
|
+
}
|
36
|
+
return data[pos];
|
52
37
|
}
|
53
38
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
39
|
+
void push_back(const T & value) {
|
40
|
+
if (sz == capacity) {
|
41
|
+
// advance the start when buffer is full
|
42
|
+
first = (first + 1) % capacity;
|
43
|
+
} else {
|
44
|
+
sz++;
|
45
|
+
}
|
46
|
+
data[pos] = value;
|
47
|
+
pos = (pos + 1) % capacity;
|
61
48
|
}
|
62
49
|
|
63
|
-
|
64
|
-
|
50
|
+
T pop_front() {
|
51
|
+
if (sz == 0) {
|
52
|
+
throw std::runtime_error("ring buffer is empty");
|
53
|
+
}
|
54
|
+
T value = data[first];
|
55
|
+
first = (first + 1) % capacity;
|
56
|
+
sz--;
|
57
|
+
return value;
|
58
|
+
}
|
65
59
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
if (grammar == nullptr) {
|
70
|
-
throw std::runtime_error("Failed to initialize llama_grammar");
|
60
|
+
const T & rat(size_t i) const {
|
61
|
+
if (i >= sz) {
|
62
|
+
throw std::runtime_error("ring buffer: index out of bounds");
|
71
63
|
}
|
72
|
-
|
64
|
+
return data[(first + sz - i - 1) % capacity];
|
73
65
|
}
|
74
66
|
|
75
|
-
std::
|
76
|
-
|
77
|
-
|
78
|
-
|
67
|
+
std::vector<T> to_vector() const {
|
68
|
+
std::vector<T> result;
|
69
|
+
result.reserve(sz);
|
70
|
+
for (size_t i = 0; i < sz; i++) {
|
71
|
+
result.push_back(data[(first + i) % capacity]);
|
72
|
+
}
|
73
|
+
return result;
|
74
|
+
}
|
79
75
|
|
80
|
-
void
|
81
|
-
|
82
|
-
|
76
|
+
void clear() {
|
77
|
+
// here only reset the status of the buffer
|
78
|
+
sz = 0;
|
79
|
+
first = 0;
|
80
|
+
pos = 0;
|
83
81
|
}
|
84
|
-
ctx->rng.seed(seed);
|
85
|
-
}
|
86
82
|
|
87
|
-
|
88
|
-
|
89
|
-
llama_grammar_free(dst->grammar);
|
90
|
-
dst->grammar = nullptr;
|
83
|
+
bool empty() const {
|
84
|
+
return sz == 0;
|
91
85
|
}
|
92
86
|
|
93
|
-
|
94
|
-
|
87
|
+
size_t size() const {
|
88
|
+
return sz;
|
95
89
|
}
|
96
90
|
|
97
|
-
|
98
|
-
|
91
|
+
size_t capacity = 0;
|
92
|
+
size_t sz = 0;
|
93
|
+
size_t first = 0;
|
94
|
+
size_t pos = 0;
|
95
|
+
std::vector<T> data;
|
96
|
+
};
|
99
97
|
|
100
|
-
|
101
|
-
|
102
|
-
}
|
98
|
+
struct gpt_sampler {
|
99
|
+
gpt_sampler_params params;
|
103
100
|
|
104
|
-
|
105
|
-
|
101
|
+
struct llama_sampler * grmr;
|
102
|
+
struct llama_sampler * chain;
|
106
103
|
|
107
|
-
|
104
|
+
ring_buffer<llama_token> prev;
|
108
105
|
|
109
|
-
std::
|
106
|
+
std::vector<llama_token_data> cur;
|
110
107
|
|
111
|
-
|
112
|
-
result += llama_token_to_piece(ctx_main, ctx_sampling->prev[i]);
|
113
|
-
}
|
108
|
+
llama_token_data_array cur_p;
|
114
109
|
|
115
|
-
|
116
|
-
|
110
|
+
void set_logits(struct llama_context * ctx, int idx) {
|
111
|
+
const auto * logits = llama_get_logits_ith(ctx, idx);
|
112
|
+
|
113
|
+
const int n_vocab = llama_n_vocab(llama_get_model(ctx));
|
114
|
+
|
115
|
+
cur.resize(n_vocab);
|
116
|
+
|
117
|
+
for (llama_token token_id = 0; token_id < n_vocab; token_id++) {
|
118
|
+
cur[token_id] = llama_token_data{token_id, logits[token_id], 0.0f};
|
119
|
+
}
|
120
|
+
|
121
|
+
cur_p = { cur.data(), cur.size(), -1, false };
|
122
|
+
}
|
123
|
+
};
|
117
124
|
|
118
|
-
std::string
|
125
|
+
std::string gpt_sampler_params::print() const {
|
119
126
|
char result[1024];
|
120
127
|
|
121
128
|
snprintf(result, sizeof(result),
|
122
129
|
"\trepeat_last_n = %d, repeat_penalty = %.3f, frequency_penalty = %.3f, presence_penalty = %.3f\n"
|
123
130
|
"\ttop_k = %d, tfs_z = %.3f, top_p = %.3f, min_p = %.3f, typical_p = %.3f, temp = %.3f\n"
|
124
131
|
"\tmirostat = %d, mirostat_lr = %.3f, mirostat_ent = %.3f",
|
125
|
-
|
126
|
-
|
127
|
-
|
132
|
+
penalty_last_n, penalty_repeat, penalty_freq, penalty_present,
|
133
|
+
top_k, tfs_z, top_p, min_p, typ_p, temp,
|
134
|
+
mirostat, mirostat_eta, mirostat_tau);
|
128
135
|
|
129
136
|
return std::string(result);
|
130
137
|
}
|
131
138
|
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
+
struct gpt_sampler * gpt_sampler_init(const struct llama_model * model, const struct gpt_sampler_params & params) {
|
140
|
+
llama_sampler_chain_params lparams = llama_sampler_chain_default_params();
|
141
|
+
|
142
|
+
lparams.no_perf = false; // TODO: control via params
|
143
|
+
|
144
|
+
auto * result = new gpt_sampler {
|
145
|
+
/* .params = */ params,
|
146
|
+
/* .grmr = */ llama_sampler_init_grammar(model, params.grammar.c_str(), "root"),
|
147
|
+
/* .chain = */ llama_sampler_chain_init(lparams),
|
148
|
+
/* .prev = */ ring_buffer<llama_token>(std::max(32, params.n_prev)),
|
149
|
+
/* .cur = */ {},
|
150
|
+
/* .cur_p = */ {},
|
151
|
+
};
|
152
|
+
|
153
|
+
llama_sampler_chain_add(result->chain,
|
154
|
+
llama_sampler_init_logit_bias(
|
155
|
+
llama_n_vocab(model),
|
156
|
+
params.logit_bias.size(),
|
157
|
+
params.logit_bias.data()));
|
158
|
+
|
159
|
+
llama_sampler_chain_add(result->chain,
|
160
|
+
llama_sampler_init_penalties(
|
161
|
+
llama_n_vocab (model),
|
162
|
+
llama_token_eos(model),
|
163
|
+
llama_token_nl (model),
|
164
|
+
params.penalty_last_n,
|
165
|
+
params.penalty_repeat,
|
166
|
+
params.penalty_freq,
|
167
|
+
params.penalty_present,
|
168
|
+
params.penalize_nl,
|
169
|
+
params.ignore_eos));
|
170
|
+
|
171
|
+
if (params.temp > 0.0f) {
|
172
|
+
if (params.mirostat == 0) {
|
173
|
+
for (const auto & cnstr : params.samplers) {
|
174
|
+
switch (cnstr) {
|
175
|
+
case GPT_SAMPLER_TYPE_TOP_K:
|
176
|
+
llama_sampler_chain_add(result->chain, llama_sampler_init_top_k (params.top_k));
|
177
|
+
break;
|
178
|
+
case GPT_SAMPLER_TYPE_TOP_P:
|
179
|
+
llama_sampler_chain_add(result->chain, llama_sampler_init_top_p (params.top_p, params.min_keep));
|
180
|
+
break;
|
181
|
+
case GPT_SAMPLER_TYPE_MIN_P:
|
182
|
+
llama_sampler_chain_add(result->chain, llama_sampler_init_min_p (params.min_p, params.min_keep));
|
183
|
+
break;
|
184
|
+
case GPT_SAMPLER_TYPE_TFS_Z:
|
185
|
+
llama_sampler_chain_add(result->chain, llama_sampler_init_tail_free(params.tfs_z, params.min_keep));
|
186
|
+
break;
|
187
|
+
case GPT_SAMPLER_TYPE_TYPICAL_P:
|
188
|
+
llama_sampler_chain_add(result->chain, llama_sampler_init_typical (params.typ_p, params.min_keep));
|
189
|
+
break;
|
190
|
+
case GPT_SAMPLER_TYPE_XTC:
|
191
|
+
llama_sampler_chain_add(result->chain, llama_sampler_init_xtc (params.xtc_p, params.xtc_t, params.min_keep, params.seed));
|
192
|
+
break;
|
193
|
+
case GPT_SAMPLER_TYPE_TEMPERATURE:
|
194
|
+
llama_sampler_chain_add(result->chain, llama_sampler_init_temp_ext (params.temp, params.dynatemp_range, params.dynatemp_exponent));
|
195
|
+
break;
|
196
|
+
default:
|
197
|
+
LM_GGML_ASSERT(false && "unknown sampler type");
|
198
|
+
}
|
139
199
|
}
|
200
|
+
llama_sampler_chain_add(result->chain, llama_sampler_init_softmax());
|
201
|
+
llama_sampler_chain_add(result->chain, llama_sampler_init_dist(params.seed));
|
202
|
+
} else if (params.mirostat == 1) {
|
203
|
+
llama_sampler_chain_add(result->chain, llama_sampler_init_temp(params.temp));
|
204
|
+
llama_sampler_chain_add(result->chain, llama_sampler_init_mirostat(llama_n_vocab(model), params.seed, params.mirostat_tau, params.mirostat_eta, 100));
|
205
|
+
} else if (params.mirostat == 2) {
|
206
|
+
llama_sampler_chain_add(result->chain, llama_sampler_init_temp(params.temp));
|
207
|
+
llama_sampler_chain_add(result->chain, llama_sampler_init_mirostat_v2(params.seed, params.mirostat_tau, params.mirostat_eta));
|
208
|
+
} else {
|
209
|
+
LM_GGML_ASSERT(false && "unknown mirostat version");
|
140
210
|
}
|
141
211
|
} else {
|
142
|
-
result
|
212
|
+
llama_sampler_chain_add(result->chain, llama_sampler_init_softmax());
|
213
|
+
llama_sampler_chain_add(result->chain, llama_sampler_init_greedy());
|
143
214
|
}
|
144
215
|
|
145
216
|
return result;
|
146
217
|
}
|
147
218
|
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
case llama_sampler_type::TEMPERATURE: return "temperature";
|
156
|
-
default : return "";
|
219
|
+
void gpt_sampler_free(struct gpt_sampler * gsmpl) {
|
220
|
+
if (gsmpl) {
|
221
|
+
llama_sampler_free(gsmpl->grmr);
|
222
|
+
|
223
|
+
llama_sampler_free(gsmpl->chain);
|
224
|
+
|
225
|
+
delete gsmpl;
|
157
226
|
}
|
158
227
|
}
|
159
228
|
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
{"typical_p", llama_sampler_type::TYPICAL_P},
|
165
|
-
{"min_p", llama_sampler_type::MIN_P},
|
166
|
-
{"tfs_z", llama_sampler_type::TFS_Z},
|
167
|
-
{"temperature", llama_sampler_type::TEMPERATURE}
|
168
|
-
};
|
229
|
+
void gpt_sampler_accept(struct gpt_sampler * gsmpl, llama_token token, bool accept_grammar) {
|
230
|
+
if (accept_grammar) {
|
231
|
+
llama_sampler_accept(gsmpl->grmr, token);
|
232
|
+
}
|
169
233
|
|
170
|
-
|
171
|
-
// make it ready for both system names and input names
|
172
|
-
std::unordered_map<std::string, llama_sampler_type> sampler_alt_name_map {
|
173
|
-
{"top-k", llama_sampler_type::TOP_K},
|
174
|
-
{"top-p", llama_sampler_type::TOP_P},
|
175
|
-
{"nucleus", llama_sampler_type::TOP_P},
|
176
|
-
{"typical-p", llama_sampler_type::TYPICAL_P},
|
177
|
-
{"typical", llama_sampler_type::TYPICAL_P},
|
178
|
-
{"min-p", llama_sampler_type::MIN_P},
|
179
|
-
{"tfs-z", llama_sampler_type::TFS_Z},
|
180
|
-
{"tfs", llama_sampler_type::TFS_Z},
|
181
|
-
{"temp", llama_sampler_type::TEMPERATURE}
|
182
|
-
};
|
234
|
+
llama_sampler_accept(gsmpl->chain, token);
|
183
235
|
|
184
|
-
|
185
|
-
sampler_types.reserve(names.size());
|
186
|
-
for (const auto & name : names)
|
187
|
-
{
|
188
|
-
auto sampler_item = sampler_canonical_name_map.find(name);
|
189
|
-
if (sampler_item != sampler_canonical_name_map.end())
|
190
|
-
{
|
191
|
-
sampler_types.push_back(sampler_item->second);
|
192
|
-
}
|
193
|
-
else
|
194
|
-
{
|
195
|
-
if (allow_alt_names)
|
196
|
-
{
|
197
|
-
sampler_item = sampler_alt_name_map.find(name);
|
198
|
-
if (sampler_item != sampler_alt_name_map.end())
|
199
|
-
{
|
200
|
-
sampler_types.push_back(sampler_item->second);
|
201
|
-
}
|
202
|
-
}
|
203
|
-
}
|
204
|
-
}
|
205
|
-
return sampler_types;
|
236
|
+
gsmpl->prev.push_back(token);
|
206
237
|
}
|
207
238
|
|
208
|
-
|
209
|
-
|
210
|
-
{'k', llama_sampler_type::TOP_K},
|
211
|
-
{'p', llama_sampler_type::TOP_P},
|
212
|
-
{'y', llama_sampler_type::TYPICAL_P},
|
213
|
-
{'m', llama_sampler_type::MIN_P},
|
214
|
-
{'f', llama_sampler_type::TFS_Z},
|
215
|
-
{'t', llama_sampler_type::TEMPERATURE}
|
216
|
-
};
|
239
|
+
void gpt_sampler_reset(struct gpt_sampler * gsmpl) {
|
240
|
+
llama_sampler_reset(gsmpl->grmr);
|
217
241
|
|
218
|
-
|
219
|
-
sampler_types.reserve(names_string.size());
|
220
|
-
for (const auto & c : names_string) {
|
221
|
-
const auto sampler_item = sampler_name_map.find(c);
|
222
|
-
if (sampler_item != sampler_name_map.end()) {
|
223
|
-
sampler_types.push_back(sampler_item->second);
|
224
|
-
}
|
225
|
-
}
|
226
|
-
return sampler_types;
|
242
|
+
llama_sampler_reset(gsmpl->chain);
|
227
243
|
}
|
228
244
|
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
const float dynatemp_exponent = params.dynatemp_exponent;
|
239
|
-
const int32_t top_k = params.top_k;
|
240
|
-
const float top_p = params.top_p;
|
241
|
-
const float min_p = params.min_p;
|
242
|
-
const float xtc_t = params.xtc_t;
|
243
|
-
const float xtc_p = params.xtc_p;
|
244
|
-
const float tfs_z = params.tfs_z;
|
245
|
-
const float typical_p = params.typical_p;
|
246
|
-
const std::vector<llama_sampler_type> & samplers_sequence = params.samplers_sequence;
|
247
|
-
|
248
|
-
for (auto sampler_type : samplers_sequence) {
|
249
|
-
switch (sampler_type) {
|
250
|
-
case llama_sampler_type::TOP_K : llama_sample_top_k (ctx_main, &cur_p, top_k, min_keep); break;
|
251
|
-
case llama_sampler_type::TFS_Z : llama_sample_tail_free(ctx_main, &cur_p, tfs_z, min_keep); break;
|
252
|
-
case llama_sampler_type::TYPICAL_P: llama_sample_typical (ctx_main, &cur_p, typical_p, min_keep); break;
|
253
|
-
case llama_sampler_type::TOP_P : llama_sample_top_p (ctx_main, &cur_p, top_p, min_keep); break;
|
254
|
-
case llama_sampler_type::MIN_P : llama_sample_min_p (ctx_main, &cur_p, min_p, min_keep); break;
|
255
|
-
case llama_sampler_type::XTC : llama_sample_xtc (ctx_main, &cur_p, xtc_t, xtc_p, min_keep, ctx_sampling->rng); break;
|
256
|
-
case llama_sampler_type::TEMPERATURE:
|
257
|
-
if (dynatemp_range > 0) {
|
258
|
-
float dynatemp_min = std::max(0.0f, temp - dynatemp_range);
|
259
|
-
float dynatemp_max = std::max(0.0f, temp + dynatemp_range);
|
260
|
-
llama_sample_entropy(ctx_main, &cur_p, dynatemp_min, dynatemp_max, dynatemp_exponent);
|
261
|
-
} else {
|
262
|
-
llama_sample_temp(ctx_main, &cur_p, temp);
|
263
|
-
}
|
264
|
-
break;
|
265
|
-
default : break;
|
266
|
-
}
|
267
|
-
}
|
245
|
+
struct gpt_sampler * gpt_sampler_clone(gpt_sampler * gsmpl) {
|
246
|
+
return new gpt_sampler {
|
247
|
+
/* .params = */ gsmpl->params,
|
248
|
+
/* .grmr = */ llama_sampler_clone(gsmpl->grmr),
|
249
|
+
/* .chain = */ llama_sampler_clone(gsmpl->chain),
|
250
|
+
/* .prev = */ gsmpl->prev,
|
251
|
+
/* .cur = */ gsmpl->cur,
|
252
|
+
/* .cur_p = */ gsmpl->cur_p,
|
253
|
+
};
|
268
254
|
}
|
269
255
|
|
270
|
-
|
271
|
-
|
272
|
-
struct llama_context * ctx_main,
|
273
|
-
struct llama_context * ctx_cfg,
|
274
|
-
const int idx,
|
275
|
-
bool is_resampling) {
|
276
|
-
const llama_sampling_params & params = ctx_sampling->params;
|
277
|
-
|
278
|
-
const float temp = params.temp;
|
279
|
-
const int mirostat = params.mirostat;
|
280
|
-
const float mirostat_tau = params.mirostat_tau;
|
281
|
-
const float mirostat_eta = params.mirostat_eta;
|
282
|
-
|
283
|
-
std::vector<float> original_logits;
|
284
|
-
auto cur_p = llama_sampling_prepare(ctx_sampling, ctx_main, ctx_cfg, idx, /* apply_grammar= */ is_resampling, &original_logits);
|
285
|
-
if (ctx_sampling->grammar != NULL && !is_resampling) {
|
286
|
-
LM_GGML_ASSERT(!original_logits.empty());
|
287
|
-
}
|
288
|
-
llama_token id = 0;
|
289
|
-
|
290
|
-
if (temp < 0.0) {
|
291
|
-
// greedy sampling, with probs
|
292
|
-
llama_sample_softmax(ctx_main, &cur_p);
|
293
|
-
id = cur_p.data[0].id;
|
294
|
-
} else if (temp == 0.0) {
|
295
|
-
// greedy sampling, no probs
|
296
|
-
id = llama_sample_token_greedy(ctx_main, &cur_p);
|
297
|
-
} else {
|
298
|
-
if (mirostat == 1) {
|
299
|
-
const int mirostat_m = 100;
|
300
|
-
llama_sample_temp(ctx_main, &cur_p, temp);
|
301
|
-
id = llama_sample_token_mirostat(ctx_main, &cur_p, mirostat_tau, mirostat_eta, mirostat_m, &ctx_sampling->mirostat_mu);
|
302
|
-
} else if (mirostat == 2) {
|
303
|
-
llama_sample_temp(ctx_main, &cur_p, temp);
|
304
|
-
id = llama_sample_token_mirostat_v2(ctx_main, &cur_p, mirostat_tau, mirostat_eta, &ctx_sampling->mirostat_mu);
|
305
|
-
} else {
|
306
|
-
// temperature sampling
|
307
|
-
size_t min_keep = std::max(1, params.min_keep);
|
308
|
-
|
309
|
-
sampler_queue(ctx_main, ctx_sampling, params, cur_p, min_keep);
|
256
|
+
void gpt_perf_print(const struct llama_context * ctx, const struct gpt_sampler * gsmpl) {
|
257
|
+
// TODO: measure grammar performance
|
310
258
|
|
311
|
-
|
259
|
+
if (gsmpl) {
|
260
|
+
llama_perf_print(gsmpl->chain, LLAMA_PERF_TYPE_SAMPLER_CHAIN);
|
261
|
+
}
|
262
|
+
if (ctx) {
|
263
|
+
llama_perf_print(ctx, LLAMA_PERF_TYPE_CONTEXT);
|
264
|
+
}
|
265
|
+
}
|
312
266
|
|
313
|
-
|
314
|
-
|
315
|
-
// LOG("top %d candidates:\n", n_top);
|
267
|
+
llama_token gpt_sampler_sample(struct gpt_sampler * gsmpl, struct llama_context * ctx, int idx, bool grammar_first) {
|
268
|
+
gsmpl->set_logits(ctx, idx);
|
316
269
|
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
// LOG(" - %5d: '%12s' (%.3f)\n", id, llama_token_to_piece(ctx_main, id).c_str(), cur_p.data[i].p);
|
321
|
-
// }
|
322
|
-
//}
|
270
|
+
auto & grmr = gsmpl->grmr;
|
271
|
+
auto & chain = gsmpl->chain;
|
272
|
+
auto & cur_p = gsmpl->cur_p; // initialized by set_logits
|
323
273
|
|
324
|
-
|
325
|
-
|
274
|
+
if (grammar_first) {
|
275
|
+
llama_sampler_apply(grmr, &cur_p);
|
326
276
|
}
|
327
277
|
|
328
|
-
|
329
|
-
// Get a pointer to the logits
|
330
|
-
float * logits = llama_get_logits_ith(ctx_main, idx);
|
278
|
+
llama_sampler_apply(chain, &cur_p);
|
331
279
|
|
332
|
-
|
333
|
-
llama_token_data single_token_data = {id, logits[id], 0.0f};
|
334
|
-
llama_token_data_array single_token_data_array = { &single_token_data, 1, false };
|
280
|
+
LM_GGML_ASSERT(cur_p.selected != -1 && "no selected token during sampling - check your sampling configuration");
|
335
281
|
|
336
|
-
|
337
|
-
llama_grammar_sample(ctx_sampling->grammar, ctx_main, &single_token_data_array);
|
282
|
+
const llama_token id = cur_p.data[cur_p.selected].id;
|
338
283
|
|
339
|
-
|
340
|
-
|
284
|
+
if (grammar_first) {
|
285
|
+
return id;
|
286
|
+
}
|
341
287
|
|
342
|
-
|
343
|
-
|
344
|
-
|
288
|
+
// check if it the sampled token fits the grammar
|
289
|
+
{
|
290
|
+
llama_token_data single_token_data = { id, 1.0f, 0.0f };
|
291
|
+
llama_token_data_array single_token_data_array = { &single_token_data, 1, -1, false };
|
345
292
|
|
346
|
-
|
347
|
-
std::copy(original_logits.begin(), original_logits.end(), logits);
|
293
|
+
llama_sampler_apply(grmr, &single_token_data_array);
|
348
294
|
|
349
|
-
|
295
|
+
const bool is_valid = single_token_data_array.data[0].logit != -INFINITY;
|
296
|
+
if (is_valid) {
|
297
|
+
return id;
|
350
298
|
}
|
351
299
|
}
|
352
300
|
|
353
|
-
|
301
|
+
// resampling:
|
302
|
+
// if the token is not valid, sample again, but first apply the grammar sampler and then the sampling chain
|
303
|
+
gsmpl->set_logits(ctx, idx);
|
354
304
|
|
355
|
-
|
356
|
-
|
305
|
+
llama_sampler_apply(grmr, &cur_p);
|
306
|
+
llama_sampler_apply(chain, &cur_p);
|
357
307
|
|
358
|
-
|
359
|
-
struct llama_sampling_context * ctx_sampling,
|
360
|
-
struct llama_context * ctx_main,
|
361
|
-
struct llama_context * ctx_cfg,
|
362
|
-
const int idx,
|
363
|
-
bool apply_grammar,
|
364
|
-
std::vector<float> * original_logits) {
|
365
|
-
const llama_sampling_params & params = ctx_sampling->params;
|
308
|
+
LM_GGML_ASSERT(cur_p.selected != -1 && "no selected token during re-sampling - check your sampling configuration");
|
366
309
|
|
367
|
-
|
310
|
+
return cur_p.data[cur_p.selected].id;
|
311
|
+
}
|
368
312
|
|
369
|
-
|
370
|
-
const float penalty_repeat = params.penalty_repeat;
|
371
|
-
const float penalty_freq = params.penalty_freq;
|
372
|
-
const float penalty_present = params.penalty_present;
|
313
|
+
// helpers
|
373
314
|
|
374
|
-
|
315
|
+
llama_token_data_array * gpt_sampler_get_candidates(struct gpt_sampler * gsmpl) {
|
316
|
+
return &gsmpl->cur_p;
|
317
|
+
}
|
375
318
|
|
376
|
-
|
377
|
-
|
319
|
+
llama_token gpt_sampler_last(const struct gpt_sampler * gsmpl) {
|
320
|
+
return gsmpl->prev.rat(0);
|
321
|
+
}
|
378
322
|
|
379
|
-
|
380
|
-
|
323
|
+
std::string gpt_sampler_print(const struct gpt_sampler * gsmpl) {
|
324
|
+
std::string result = "\tlogits ";
|
381
325
|
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
*original_logits = {logits, logits + n_vocab};
|
326
|
+
for (int i = 0; i < llama_sampler_chain_n(gsmpl->chain); i++) {
|
327
|
+
const auto * smpl = llama_sampler_chain_get(gsmpl->chain, i);
|
328
|
+
result += std::string("-> ") + llama_sampler_name(smpl) + " ";
|
386
329
|
}
|
387
330
|
|
388
|
-
|
389
|
-
|
390
|
-
|
331
|
+
return result;
|
332
|
+
}
|
333
|
+
|
334
|
+
std::string gpt_sampler_prev_str(gpt_sampler * gsmpl, llama_context * ctx_main, int n) {
|
335
|
+
n = std::min(n, (int) gsmpl->prev.size());
|
336
|
+
|
337
|
+
if (n <= 0) {
|
338
|
+
return "";
|
391
339
|
}
|
392
340
|
|
393
|
-
|
394
|
-
|
395
|
-
|
341
|
+
std::string result;
|
342
|
+
result.reserve(8*n); // 8 is the average length of a token [citation needed], TODO: compute this from the vocab
|
343
|
+
|
344
|
+
for (int i = n - 1; i >= 0; i--) {
|
345
|
+
const llama_token id = gsmpl->prev.rat(i);
|
346
|
+
|
347
|
+
LM_GGML_ASSERT(id != LLAMA_TOKEN_NULL && "null token in the sampling history - should not happen");
|
348
|
+
|
349
|
+
result += llama_token_to_piece(ctx_main, id);
|
396
350
|
}
|
397
351
|
|
398
|
-
|
352
|
+
return result;
|
353
|
+
}
|
399
354
|
|
400
|
-
|
401
|
-
|
355
|
+
struct llama_sampler_timings gpt_sampler_get_timigs(const struct gpt_sampler * gsmpl){
|
356
|
+
return llama_sampler_chain_timings(gsmpl -> chain);
|
357
|
+
}
|
358
|
+
|
359
|
+
char gpt_sampler_type_to_chr(enum gpt_sampler_type cnstr) {
|
360
|
+
switch (cnstr) {
|
361
|
+
case GPT_SAMPLER_TYPE_TOP_K: return 'k';
|
362
|
+
case GPT_SAMPLER_TYPE_TFS_Z: return 'f';
|
363
|
+
case GPT_SAMPLER_TYPE_TYPICAL_P: return 'y';
|
364
|
+
case GPT_SAMPLER_TYPE_TOP_P: return 'p';
|
365
|
+
case GPT_SAMPLER_TYPE_MIN_P: return 'm';
|
366
|
+
case GPT_SAMPLER_TYPE_TEMPERATURE: return 't';
|
367
|
+
case GPT_SAMPLER_TYPE_XTC: return 'x';
|
368
|
+
default : return '?';
|
369
|
+
}
|
370
|
+
}
|
371
|
+
|
372
|
+
std::string gpt_sampler_type_to_str(enum gpt_sampler_type cnstr) {
|
373
|
+
switch (cnstr) {
|
374
|
+
case GPT_SAMPLER_TYPE_TOP_K: return "top_k";
|
375
|
+
case GPT_SAMPLER_TYPE_TFS_Z: return "tfs_z";
|
376
|
+
case GPT_SAMPLER_TYPE_TYPICAL_P: return "typ_p";
|
377
|
+
case GPT_SAMPLER_TYPE_TOP_P: return "top_p";
|
378
|
+
case GPT_SAMPLER_TYPE_MIN_P: return "min_p";
|
379
|
+
case GPT_SAMPLER_TYPE_XTC: return "xtc";
|
380
|
+
case GPT_SAMPLER_TYPE_TEMPERATURE: return "temperature";
|
381
|
+
default : return "";
|
402
382
|
}
|
383
|
+
}
|
403
384
|
|
404
|
-
|
385
|
+
std::vector<gpt_sampler_type> gpt_sampler_types_from_names(const std::vector<std::string> & names, bool allow_alt_names) {
|
386
|
+
std::unordered_map<std::string, gpt_sampler_type> sampler_canonical_name_map {
|
387
|
+
{ "top_k", GPT_SAMPLER_TYPE_TOP_K },
|
388
|
+
{ "top_p", GPT_SAMPLER_TYPE_TOP_P },
|
389
|
+
{ "typ_p", GPT_SAMPLER_TYPE_TYPICAL_P },
|
390
|
+
{ "min_p", GPT_SAMPLER_TYPE_MIN_P },
|
391
|
+
{ "tfs_z", GPT_SAMPLER_TYPE_TFS_Z },
|
392
|
+
{ "xtc", GPT_SAMPLER_TYPE_XTC},
|
393
|
+
{ "temperature", GPT_SAMPLER_TYPE_TEMPERATURE },
|
394
|
+
};
|
405
395
|
|
406
|
-
//
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
396
|
+
// since samplers names are written multiple ways
|
397
|
+
// make it ready for both system names and input names
|
398
|
+
std::unordered_map<std::string, gpt_sampler_type> sampler_alt_name_map {
|
399
|
+
{ "top-k", GPT_SAMPLER_TYPE_TOP_K },
|
400
|
+
{ "top-p", GPT_SAMPLER_TYPE_TOP_P },
|
401
|
+
{ "nucleus", GPT_SAMPLER_TYPE_TOP_P },
|
402
|
+
{ "typical-p", GPT_SAMPLER_TYPE_TYPICAL_P },
|
403
|
+
{ "typical", GPT_SAMPLER_TYPE_TYPICAL_P },
|
404
|
+
{ "typ-p", GPT_SAMPLER_TYPE_TYPICAL_P },
|
405
|
+
{ "typ", GPT_SAMPLER_TYPE_TYPICAL_P },
|
406
|
+
{ "min-p", GPT_SAMPLER_TYPE_MIN_P },
|
407
|
+
{ "tfs-z", GPT_SAMPLER_TYPE_TFS_Z },
|
408
|
+
{ "tfs", GPT_SAMPLER_TYPE_TFS_Z },
|
409
|
+
{ "xtc_p", GPT_SAMPLER_TYPE_XTC},
|
410
|
+
{ "xtc_t", GPT_SAMPLER_TYPE_XTC},
|
411
|
+
{ "temp", GPT_SAMPLER_TYPE_TEMPERATURE },
|
412
|
+
};
|
411
413
|
|
412
|
-
|
413
|
-
|
414
|
-
penalty_tokens_used_size, penalty_repeat, penalty_freq, penalty_present);
|
414
|
+
std::vector<gpt_sampler_type> samplers;
|
415
|
+
samplers.reserve(names.size());
|
415
416
|
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
417
|
+
for (const auto & name : names) {
|
418
|
+
auto sampler = sampler_canonical_name_map.find(name);
|
419
|
+
if (sampler != sampler_canonical_name_map.end()) {
|
420
|
+
samplers.push_back(sampler->second);
|
421
|
+
} else {
|
422
|
+
if (allow_alt_names) {
|
423
|
+
sampler = sampler_alt_name_map.find(name);
|
424
|
+
if (sampler != sampler_alt_name_map.end()) {
|
425
|
+
samplers.push_back(sampler->second);
|
421
426
|
}
|
422
427
|
}
|
423
428
|
}
|
424
429
|
}
|
425
430
|
|
426
|
-
|
427
|
-
if (apply_grammar && ctx_sampling->grammar != NULL) {
|
428
|
-
llama_grammar_sample(ctx_sampling->grammar, ctx_main, &cur_p);
|
429
|
-
}
|
430
|
-
|
431
|
-
return cur_p;
|
431
|
+
return samplers;
|
432
432
|
}
|
433
433
|
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
}
|
442
|
-
|
443
|
-
|
444
|
-
struct llama_sampling_context * ctx_sampling,
|
445
|
-
struct llama_context * ctx_main,
|
446
|
-
struct llama_context * ctx_cfg,
|
447
|
-
const int idx,
|
448
|
-
bool apply_grammar,
|
449
|
-
std::vector<float> * original_logits) {
|
450
|
-
return llama_sampling_prepare_impl(ctx_sampling,ctx_main, ctx_cfg, idx, apply_grammar, original_logits);
|
451
|
-
}
|
434
|
+
std::vector<gpt_sampler_type> gpt_sampler_types_from_chars(const std::string & chars) {
|
435
|
+
std::unordered_map<char, gpt_sampler_type> sampler_name_map {
|
436
|
+
{ gpt_sampler_type_to_chr(GPT_SAMPLER_TYPE_TOP_K), GPT_SAMPLER_TYPE_TOP_K },
|
437
|
+
{ gpt_sampler_type_to_chr(GPT_SAMPLER_TYPE_TFS_Z), GPT_SAMPLER_TYPE_TFS_Z },
|
438
|
+
{ gpt_sampler_type_to_chr(GPT_SAMPLER_TYPE_TYPICAL_P), GPT_SAMPLER_TYPE_TYPICAL_P },
|
439
|
+
{ gpt_sampler_type_to_chr(GPT_SAMPLER_TYPE_TOP_P), GPT_SAMPLER_TYPE_TOP_P },
|
440
|
+
{ gpt_sampler_type_to_chr(GPT_SAMPLER_TYPE_MIN_P), GPT_SAMPLER_TYPE_MIN_P },
|
441
|
+
{ gpt_sampler_type_to_chr(GPT_SAMPLER_TYPE_XTC), GPT_SAMPLER_TYPE_XTC},
|
442
|
+
{ gpt_sampler_type_to_chr(GPT_SAMPLER_TYPE_TEMPERATURE), GPT_SAMPLER_TYPE_TEMPERATURE }
|
443
|
+
};
|
452
444
|
|
453
|
-
|
454
|
-
|
455
|
-
struct llama_context * ctx_main,
|
456
|
-
llama_token id,
|
457
|
-
bool apply_grammar) {
|
458
|
-
ctx_sampling->prev.erase(ctx_sampling->prev.begin());
|
459
|
-
ctx_sampling->prev.push_back(id);
|
445
|
+
std::vector<gpt_sampler_type> samplers;
|
446
|
+
samplers.reserve(chars.size());
|
460
447
|
|
461
|
-
|
462
|
-
|
448
|
+
for (const auto & c : chars) {
|
449
|
+
const auto sampler = sampler_name_map.find(c);
|
450
|
+
if (sampler != sampler_name_map.end()) {
|
451
|
+
samplers.push_back(sampler->second);
|
452
|
+
}
|
463
453
|
}
|
454
|
+
|
455
|
+
return samplers;
|
464
456
|
}
|