2023-10-11 19:35:46 +00:00
|
|
|
#include "sampling.h"
|
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
#include "common.h"
|
2023-10-18 13:21:57 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
// the ring buffer works similarly to std::deque, but with a fixed capacity
|
|
|
|
// TODO: deduplicate with llama-impl.h
|
|
|
|
template<typename T>
|
|
|
|
struct ring_buffer {
|
|
|
|
ring_buffer(size_t cap) : capacity(cap), data(cap) {}
|
2023-10-18 13:21:57 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
T & front() {
|
|
|
|
if (sz == 0) {
|
|
|
|
throw std::runtime_error("ring buffer is empty");
|
2023-10-11 19:35:46 +00:00
|
|
|
}
|
2024-09-07 12:16:19 +00:00
|
|
|
return data[first];
|
|
|
|
}
|
2023-10-18 13:21:57 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
const T & front() const {
|
|
|
|
if (sz == 0) {
|
|
|
|
throw std::runtime_error("ring buffer is empty");
|
2024-03-13 18:10:40 +00:00
|
|
|
}
|
2024-09-07 12:16:19 +00:00
|
|
|
return data[first];
|
|
|
|
}
|
2024-03-13 18:10:40 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
T & back() {
|
|
|
|
if (sz == 0) {
|
|
|
|
throw std::runtime_error("ring buffer is empty");
|
2024-06-25 19:07:28 +00:00
|
|
|
}
|
2024-09-07 12:16:19 +00:00
|
|
|
return data[pos];
|
2023-10-11 19:35:46 +00:00
|
|
|
}
|
2023-10-18 13:21:57 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
const T & back() const {
|
|
|
|
if (sz == 0) {
|
|
|
|
throw std::runtime_error("ring buffer is empty");
|
|
|
|
}
|
|
|
|
return data[pos];
|
2023-10-18 13:21:57 +00:00
|
|
|
}
|
2023-10-11 19:35:46 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
void push_back(const T & value) {
|
|
|
|
if (sz == capacity) {
|
|
|
|
// advance the start when buffer is full
|
|
|
|
first = (first + 1) % capacity;
|
|
|
|
} else {
|
|
|
|
sz++;
|
|
|
|
}
|
|
|
|
data[pos] = value;
|
|
|
|
pos = (pos + 1) % capacity;
|
2023-10-11 19:35:46 +00:00
|
|
|
}
|
2023-10-18 13:21:57 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
T pop_front() {
|
|
|
|
if (sz == 0) {
|
|
|
|
throw std::runtime_error("ring buffer is empty");
|
|
|
|
}
|
|
|
|
T value = data[first];
|
|
|
|
first = (first + 1) % capacity;
|
|
|
|
sz--;
|
|
|
|
return value;
|
|
|
|
}
|
2023-10-18 13:21:57 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
const T & rat(size_t i) const {
|
|
|
|
if (i >= sz) {
|
|
|
|
throw std::runtime_error("ring buffer: index out of bounds");
|
2024-06-25 19:07:28 +00:00
|
|
|
}
|
2024-09-07 12:16:19 +00:00
|
|
|
return data[(first + sz - i - 1) % capacity];
|
2023-10-18 13:21:57 +00:00
|
|
|
}
|
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
std::vector<T> to_vector() const {
|
|
|
|
std::vector<T> result;
|
|
|
|
result.reserve(sz);
|
|
|
|
for (size_t i = 0; i < sz; i++) {
|
|
|
|
result.push_back(data[(first + i) % capacity]);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2023-10-11 19:35:46 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
void clear() {
|
|
|
|
// here only reset the status of the buffer
|
|
|
|
sz = 0;
|
|
|
|
first = 0;
|
|
|
|
pos = 0;
|
2024-04-24 09:08:36 +00:00
|
|
|
}
|
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
bool empty() const {
|
|
|
|
return sz == 0;
|
2023-10-11 19:35:46 +00:00
|
|
|
}
|
2023-10-18 13:21:57 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
size_t size() const {
|
|
|
|
return sz;
|
2023-10-18 13:21:57 +00:00
|
|
|
}
|
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
size_t capacity = 0;
|
|
|
|
size_t sz = 0;
|
|
|
|
size_t first = 0;
|
|
|
|
size_t pos = 0;
|
|
|
|
std::vector<T> data;
|
|
|
|
};
|
2023-10-11 19:35:46 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
struct gpt_sampler {
|
|
|
|
gpt_sampler_params params;
|
2023-10-20 18:07:23 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
struct llama_sampler * grmr;
|
|
|
|
struct llama_sampler * chain;
|
2023-10-20 18:07:23 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
ring_buffer<llama_token> prev;
|
2023-10-20 18:07:23 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
std::vector<llama_token_data> cur;
|
2023-10-20 18:07:23 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
llama_token_data_array cur_p;
|
2023-10-20 18:07:23 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
void set_logits(struct llama_context * ctx, int idx) {
|
|
|
|
const auto * logits = llama_get_logits_ith(ctx, idx);
|
|
|
|
|
|
|
|
const int n_vocab = llama_n_vocab(llama_get_model(ctx));
|
|
|
|
|
|
|
|
cur.resize(n_vocab);
|
|
|
|
|
|
|
|
for (llama_token token_id = 0; token_id < n_vocab; token_id++) {
|
|
|
|
cur[token_id] = llama_token_data{token_id, logits[token_id], 0.0f};
|
|
|
|
}
|
|
|
|
|
|
|
|
cur_p = { cur.data(), cur.size(), -1, false };
|
|
|
|
}
|
|
|
|
};
|
2023-10-20 18:07:23 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
std::string gpt_sampler_params::print() const {
|
2023-10-20 18:07:23 +00:00
|
|
|
char result[1024];
|
|
|
|
|
|
|
|
snprintf(result, sizeof(result),
|
|
|
|
"\trepeat_last_n = %d, repeat_penalty = %.3f, frequency_penalty = %.3f, presence_penalty = %.3f\n"
|
2023-10-31 19:44:49 +00:00
|
|
|
"\ttop_k = %d, tfs_z = %.3f, top_p = %.3f, min_p = %.3f, typical_p = %.3f, temp = %.3f\n"
|
2023-10-20 18:07:23 +00:00
|
|
|
"\tmirostat = %d, mirostat_lr = %.3f, mirostat_ent = %.3f",
|
2024-09-07 12:16:19 +00:00
|
|
|
penalty_last_n, penalty_repeat, penalty_freq, penalty_present,
|
|
|
|
top_k, tfs_z, top_p, min_p, typ_p, temp,
|
|
|
|
mirostat, mirostat_eta, mirostat_tau);
|
2023-10-20 18:07:23 +00:00
|
|
|
|
|
|
|
return std::string(result);
|
|
|
|
}
|
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
struct gpt_sampler * gpt_sampler_init(const struct llama_model * model, const struct gpt_sampler_params & params) {
|
|
|
|
llama_sampler_chain_params lparams = llama_sampler_chain_default_params();
|
|
|
|
|
|
|
|
lparams.no_perf = false; // TODO: control via params
|
|
|
|
|
|
|
|
auto * result = new gpt_sampler {
|
|
|
|
/* .params = */ params,
|
|
|
|
/* .grmr = */ llama_sampler_init_grammar(model, params.grammar.c_str(), "root"),
|
|
|
|
/* .chain = */ llama_sampler_chain_init(lparams),
|
|
|
|
/* .prev = */ ring_buffer<llama_token>(params.n_prev),
|
|
|
|
/* .cur = */ {},
|
|
|
|
/* .cur_p = */ {},
|
|
|
|
};
|
|
|
|
|
|
|
|
llama_sampler_chain_add(result->chain,
|
|
|
|
llama_sampler_init_logit_bias(
|
|
|
|
llama_n_vocab(model),
|
|
|
|
params.logit_bias.size(),
|
|
|
|
params.logit_bias.data()));
|
|
|
|
|
|
|
|
llama_sampler_chain_add(result->chain,
|
|
|
|
llama_sampler_init_penalties(
|
|
|
|
llama_n_vocab (model),
|
|
|
|
llama_token_eos(model),
|
|
|
|
llama_token_nl (model),
|
|
|
|
params.penalty_last_n,
|
|
|
|
params.penalty_repeat,
|
|
|
|
params.penalty_freq,
|
|
|
|
params.penalty_present,
|
|
|
|
params.penalize_nl,
|
|
|
|
params.ignore_eos));
|
|
|
|
|
|
|
|
if (params.temp > 0.0f) {
|
|
|
|
if (params.mirostat == 0) {
|
|
|
|
for (const auto & cnstr : params.samplers) {
|
|
|
|
switch (cnstr) {
|
|
|
|
case GPT_SAMPLER_TYPE_TOP_K:
|
|
|
|
llama_sampler_chain_add(result->chain, llama_sampler_init_top_k (params.top_k));
|
|
|
|
break;
|
|
|
|
case GPT_SAMPLER_TYPE_TOP_P:
|
|
|
|
llama_sampler_chain_add(result->chain, llama_sampler_init_top_p (params.top_p, params.min_keep));
|
|
|
|
break;
|
|
|
|
case GPT_SAMPLER_TYPE_MIN_P:
|
|
|
|
llama_sampler_chain_add(result->chain, llama_sampler_init_min_p (params.min_p, params.min_keep));
|
|
|
|
break;
|
|
|
|
case GPT_SAMPLER_TYPE_TFS_Z:
|
|
|
|
llama_sampler_chain_add(result->chain, llama_sampler_init_tail_free(params.tfs_z, params.min_keep));
|
|
|
|
break;
|
|
|
|
case GPT_SAMPLER_TYPE_TYPICAL_P:
|
|
|
|
llama_sampler_chain_add(result->chain, llama_sampler_init_typical (params.typ_p, params.min_keep));
|
|
|
|
break;
|
|
|
|
case GPT_SAMPLER_TYPE_TEMPERATURE:
|
|
|
|
llama_sampler_chain_add(result->chain, llama_sampler_init_temp_ext (params.temp, params.dynatemp_range, params.dynatemp_exponent));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
GGML_ASSERT(false && "unknown sampler type");
|
|
|
|
}
|
2023-12-05 10:05:51 +00:00
|
|
|
}
|
2024-09-07 12:16:19 +00:00
|
|
|
llama_sampler_chain_add(result->chain, llama_sampler_init_softmax());
|
|
|
|
llama_sampler_chain_add(result->chain, llama_sampler_init_dist(params.seed));
|
|
|
|
} else if (params.mirostat == 1) {
|
|
|
|
llama_sampler_chain_add(result->chain, llama_sampler_init_temp(params.temp));
|
|
|
|
llama_sampler_chain_add(result->chain, llama_sampler_init_mirostat(llama_n_vocab(model), params.seed, params.mirostat_tau, params.mirostat_eta, 100));
|
|
|
|
} else if (params.mirostat == 2) {
|
|
|
|
llama_sampler_chain_add(result->chain, llama_sampler_init_temp(params.temp));
|
|
|
|
llama_sampler_chain_add(result->chain, llama_sampler_init_mirostat_v2(params.seed, params.mirostat_tau, params.mirostat_eta));
|
|
|
|
} else {
|
|
|
|
GGML_ASSERT(false && "unknown mirostat version");
|
2023-12-05 10:05:51 +00:00
|
|
|
}
|
2023-12-06 08:41:03 +00:00
|
|
|
} else {
|
2024-09-07 12:16:19 +00:00
|
|
|
llama_sampler_chain_add(result->chain, llama_sampler_init_softmax());
|
|
|
|
llama_sampler_chain_add(result->chain, llama_sampler_init_greedy());
|
2023-12-06 08:41:03 +00:00
|
|
|
}
|
2023-12-05 10:05:51 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
void gpt_sampler_free(struct gpt_sampler * gsmpl) {
|
|
|
|
if (gsmpl) {
|
|
|
|
llama_sampler_free(gsmpl->grmr);
|
|
|
|
|
|
|
|
llama_sampler_free(gsmpl->chain);
|
|
|
|
|
|
|
|
delete gsmpl;
|
2024-05-22 17:04:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
void gpt_sampler_accept(struct gpt_sampler * gsmpl, llama_token token, bool accept_grammar) {
|
|
|
|
if (accept_grammar) {
|
|
|
|
llama_sampler_accept(gsmpl->grmr, token);
|
|
|
|
}
|
2024-05-22 17:04:20 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
llama_sampler_accept(gsmpl->chain, token);
|
2024-05-22 17:04:20 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
gsmpl->prev.push_back(token);
|
2024-05-22 17:04:20 +00:00
|
|
|
}
|
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
void gpt_sampler_reset(struct gpt_sampler * gsmpl) {
|
|
|
|
llama_sampler_reset(gsmpl->grmr);
|
2024-05-22 17:04:20 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
llama_sampler_reset(gsmpl->chain);
|
2024-05-22 17:04:20 +00:00
|
|
|
}
|
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
struct gpt_sampler * gpt_sampler_clone(gpt_sampler * gsmpl) {
|
|
|
|
return new gpt_sampler {
|
|
|
|
/* .params = */ gsmpl->params,
|
|
|
|
/* .grmr = */ llama_sampler_clone(gsmpl->grmr),
|
|
|
|
/* .chain = */ llama_sampler_clone(gsmpl->chain),
|
|
|
|
/* .prev = */ gsmpl->prev,
|
|
|
|
/* .cur = */ gsmpl->cur,
|
|
|
|
/* .cur_p = */ gsmpl->cur_p,
|
|
|
|
};
|
2023-12-05 10:05:51 +00:00
|
|
|
}
|
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
void gpt_perf_print(const struct llama_context * ctx, const struct gpt_sampler * gsmpl) {
|
|
|
|
// TODO: measure grammar performance
|
2023-10-18 13:21:57 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
if (gsmpl) {
|
|
|
|
llama_perf_print(gsmpl->chain, LLAMA_PERF_TYPE_SAMPLER_CHAIN);
|
|
|
|
}
|
|
|
|
if (ctx) {
|
|
|
|
llama_perf_print(ctx, LLAMA_PERF_TYPE_CONTEXT);
|
|
|
|
}
|
|
|
|
}
|
2023-10-18 13:21:57 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
llama_token gpt_sampler_sample(struct gpt_sampler * gsmpl, struct llama_context * ctx, int idx, bool grammar_first) {
|
|
|
|
gsmpl->set_logits(ctx, idx);
|
2023-10-18 13:21:57 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
auto & grmr = gsmpl->grmr;
|
|
|
|
auto & chain = gsmpl->chain;
|
|
|
|
auto & cur_p = gsmpl->cur_p; // initialized by set_logits
|
2023-10-18 13:21:57 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
if (grammar_first) {
|
|
|
|
llama_sampler_apply(grmr, &cur_p);
|
2023-10-11 19:35:46 +00:00
|
|
|
}
|
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
llama_sampler_apply(chain, &cur_p);
|
2024-07-08 06:31:55 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
GGML_ASSERT(cur_p.selected != -1 && "no selected token during sampling - check your sampling configuration");
|
2023-12-23 09:27:07 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
const llama_token id = cur_p.data[cur_p.selected].id;
|
2023-12-23 09:27:07 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
if (grammar_first) {
|
|
|
|
return id;
|
|
|
|
}
|
2023-12-23 09:27:07 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
// check if it the sampled token fits the grammar
|
|
|
|
{
|
|
|
|
llama_token_data single_token_data = { id, 1.0f, 0.0f };
|
|
|
|
llama_token_data_array single_token_data_array = { &single_token_data, 1, -1, false };
|
2023-12-23 09:27:07 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
llama_sampler_apply(grmr, &single_token_data_array);
|
2023-12-23 09:27:07 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
const bool is_valid = single_token_data_array.data[0].logit != -INFINITY;
|
|
|
|
if (is_valid) {
|
|
|
|
return id;
|
2023-12-23 09:27:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
// resampling:
|
|
|
|
// if the token is not valid, sample again, but first apply the grammar sampler and then the sampling chain
|
|
|
|
gsmpl->set_logits(ctx, idx);
|
2024-05-07 21:07:58 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
llama_sampler_apply(grmr, &cur_p);
|
|
|
|
llama_sampler_apply(chain, &cur_p);
|
2023-10-18 13:21:57 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
GGML_ASSERT(cur_p.selected != -1 && "no selected token during re-sampling - check your sampling configuration");
|
2024-03-04 18:24:00 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
return cur_p.data[cur_p.selected].id;
|
|
|
|
}
|
2024-03-04 18:24:00 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
// helpers
|
2024-03-24 08:54:07 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
llama_token_data_array * gpt_sampler_get_candidates(struct gpt_sampler * gsmpl) {
|
|
|
|
return &gsmpl->cur_p;
|
|
|
|
}
|
2024-03-04 18:24:00 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
llama_token gpt_sampler_last(const struct gpt_sampler * gsmpl) {
|
|
|
|
return gsmpl->prev.rat(0);
|
|
|
|
}
|
2024-03-04 18:24:00 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
std::string gpt_sampler_print(const struct gpt_sampler * gsmpl) {
|
|
|
|
std::string result = "\tlogits ";
|
2024-03-04 18:24:00 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
for (int i = 0; i < llama_sampler_chain_n(gsmpl->chain); i++) {
|
|
|
|
const auto * smpl = llama_sampler_chain_get(gsmpl->chain, i);
|
|
|
|
result += std::string("-> ") + llama_sampler_name(smpl) + " ";
|
2024-03-24 08:54:07 +00:00
|
|
|
}
|
2024-03-04 18:24:00 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string gpt_sampler_prev_str(gpt_sampler * gsmpl, llama_context * ctx_main, int n) {
|
|
|
|
n = std::min(n, (int) gsmpl->prev.size());
|
|
|
|
|
|
|
|
if (n <= 0) {
|
|
|
|
return "";
|
2024-03-04 18:24:00 +00:00
|
|
|
}
|
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
std::string result;
|
|
|
|
result.reserve(8*n); // 8 is the average length of a token [citation needed], TODO: compute this from the vocab
|
|
|
|
|
|
|
|
for (int i = n - 1; i >= 0; i--) {
|
|
|
|
const llama_token id = gsmpl->prev.rat(i);
|
|
|
|
|
|
|
|
GGML_ASSERT(id != LLAMA_TOKEN_NULL && "null token in the sampling history - should not happen");
|
|
|
|
|
|
|
|
result += llama_token_to_piece(ctx_main, id);
|
2024-03-04 18:24:00 +00:00
|
|
|
}
|
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
return result;
|
|
|
|
}
|
2024-03-04 18:24:00 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
char gpt_sampler_type_to_chr(enum gpt_sampler_type cnstr) {
|
|
|
|
switch (cnstr) {
|
|
|
|
case GPT_SAMPLER_TYPE_TOP_K: return 'k';
|
|
|
|
case GPT_SAMPLER_TYPE_TFS_Z: return 'f';
|
|
|
|
case GPT_SAMPLER_TYPE_TYPICAL_P: return 'y';
|
|
|
|
case GPT_SAMPLER_TYPE_TOP_P: return 'p';
|
|
|
|
case GPT_SAMPLER_TYPE_MIN_P: return 'm';
|
|
|
|
case GPT_SAMPLER_TYPE_TEMPERATURE: return 't';
|
|
|
|
default : return '?';
|
2024-03-04 18:24:00 +00:00
|
|
|
}
|
2024-09-07 12:16:19 +00:00
|
|
|
}
|
2024-03-04 18:24:00 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
std::string gpt_sampler_type_to_str(enum gpt_sampler_type cnstr) {
|
|
|
|
switch (cnstr) {
|
|
|
|
case GPT_SAMPLER_TYPE_TOP_K: return "top_k";
|
|
|
|
case GPT_SAMPLER_TYPE_TFS_Z: return "tfs_z";
|
|
|
|
case GPT_SAMPLER_TYPE_TYPICAL_P: return "typ_p";
|
|
|
|
case GPT_SAMPLER_TYPE_TOP_P: return "top_p";
|
|
|
|
case GPT_SAMPLER_TYPE_MIN_P: return "min_p";
|
|
|
|
case GPT_SAMPLER_TYPE_TEMPERATURE: return "temperature";
|
|
|
|
default : return "";
|
|
|
|
}
|
|
|
|
}
|
2024-03-04 18:24:00 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
std::vector<gpt_sampler_type> gpt_sampler_types_from_names(const std::vector<std::string> & names, bool allow_alt_names) {
|
|
|
|
std::unordered_map<std::string, gpt_sampler_type> sampler_canonical_name_map {
|
|
|
|
{ "top_k", GPT_SAMPLER_TYPE_TOP_K },
|
|
|
|
{ "top_p", GPT_SAMPLER_TYPE_TOP_P },
|
|
|
|
{ "typ_p", GPT_SAMPLER_TYPE_TYPICAL_P },
|
|
|
|
{ "min_p", GPT_SAMPLER_TYPE_MIN_P },
|
|
|
|
{ "tfs_z", GPT_SAMPLER_TYPE_TFS_Z },
|
|
|
|
{ "temperature", GPT_SAMPLER_TYPE_TEMPERATURE },
|
|
|
|
};
|
2024-03-04 18:24:00 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
// since samplers names are written multiple ways
|
|
|
|
// make it ready for both system names and input names
|
|
|
|
std::unordered_map<std::string, gpt_sampler_type> sampler_alt_name_map {
|
|
|
|
{ "top-k", GPT_SAMPLER_TYPE_TOP_K },
|
|
|
|
{ "top-p", GPT_SAMPLER_TYPE_TOP_P },
|
|
|
|
{ "nucleus", GPT_SAMPLER_TYPE_TOP_P },
|
|
|
|
{ "typical-p", GPT_SAMPLER_TYPE_TYPICAL_P },
|
|
|
|
{ "typical", GPT_SAMPLER_TYPE_TYPICAL_P },
|
|
|
|
{ "typ-p", GPT_SAMPLER_TYPE_TYPICAL_P },
|
|
|
|
{ "typ", GPT_SAMPLER_TYPE_TYPICAL_P },
|
|
|
|
{ "min-p", GPT_SAMPLER_TYPE_MIN_P },
|
|
|
|
{ "tfs-z", GPT_SAMPLER_TYPE_TFS_Z },
|
|
|
|
{ "tfs", GPT_SAMPLER_TYPE_TFS_Z },
|
|
|
|
{ "temp", GPT_SAMPLER_TYPE_TEMPERATURE },
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<gpt_sampler_type> samplers;
|
|
|
|
samplers.reserve(names.size());
|
2024-03-04 18:24:00 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
for (const auto & name : names) {
|
|
|
|
auto sampler = sampler_canonical_name_map.find(name);
|
|
|
|
if (sampler != sampler_canonical_name_map.end()) {
|
|
|
|
samplers.push_back(sampler->second);
|
|
|
|
} else {
|
|
|
|
if (allow_alt_names) {
|
|
|
|
sampler = sampler_alt_name_map.find(name);
|
|
|
|
if (sampler != sampler_alt_name_map.end()) {
|
|
|
|
samplers.push_back(sampler->second);
|
2024-03-04 18:24:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
return samplers;
|
2024-03-04 18:24:00 +00:00
|
|
|
}
|
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
std::vector<gpt_sampler_type> gpt_sampler_types_from_chars(const std::string & chars) {
|
|
|
|
std::unordered_map<char, gpt_sampler_type> sampler_name_map {
|
|
|
|
{ gpt_sampler_type_to_chr(GPT_SAMPLER_TYPE_TOP_K), GPT_SAMPLER_TYPE_TOP_K },
|
|
|
|
{ gpt_sampler_type_to_chr(GPT_SAMPLER_TYPE_TFS_Z), GPT_SAMPLER_TYPE_TFS_Z },
|
|
|
|
{ gpt_sampler_type_to_chr(GPT_SAMPLER_TYPE_TYPICAL_P), GPT_SAMPLER_TYPE_TYPICAL_P },
|
|
|
|
{ gpt_sampler_type_to_chr(GPT_SAMPLER_TYPE_TOP_P), GPT_SAMPLER_TYPE_TOP_P },
|
|
|
|
{ gpt_sampler_type_to_chr(GPT_SAMPLER_TYPE_MIN_P), GPT_SAMPLER_TYPE_MIN_P },
|
|
|
|
{ gpt_sampler_type_to_chr(GPT_SAMPLER_TYPE_TEMPERATURE), GPT_SAMPLER_TYPE_TEMPERATURE }
|
|
|
|
};
|
2024-03-04 18:24:00 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
std::vector<gpt_sampler_type> samplers;
|
|
|
|
samplers.reserve(chars.size());
|
2023-10-18 13:21:57 +00:00
|
|
|
|
2024-09-07 12:16:19 +00:00
|
|
|
for (const auto & c : chars) {
|
|
|
|
const auto sampler = sampler_name_map.find(c);
|
|
|
|
if (sampler != sampler_name_map.end()) {
|
|
|
|
samplers.push_back(sampler->second);
|
|
|
|
}
|
2023-10-18 13:21:57 +00:00
|
|
|
}
|
2024-09-07 12:16:19 +00:00
|
|
|
|
|
|
|
return samplers;
|
2023-10-18 13:21:57 +00:00
|
|
|
}
|