Skip to content

Commit 34459ca

Browse files
Use C++11, clarify llama API documentation, rename Mirostat parameters to --mirostat_lr and --mirostat_ent, add temperature sampling for Mirostat, simplify Mirostat sampling API parameters (removed N and *k)
Use C++11, clarify llama API documentation, rename Mirostat parameters to --mirostat_lr and --mirostat_ent, add temperature sampling for Mirostat, simplify Mirostat sampling API parameters (removed N and *k)
1 parent 4f976f1 commit 34459ca

File tree

6 files changed

+70
-99
lines changed

6 files changed

+70
-99
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ option(LLAMA_BUILD_EXAMPLES "llama: build examples" ${LLAMA_STANDALONE})
7575
# Compile flags
7676
#
7777

78-
set(CMAKE_CXX_STANDARD 20)
78+
set(CMAKE_CXX_STANDARD 11)
7979
set(CMAKE_CXX_STANDARD_REQUIRED true)
8080
set(CMAKE_C_STANDARD 11)
8181
set(CMAKE_C_STANDARD_REQUIRED true)

examples/common.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,13 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
152152
break;
153153
}
154154
params.mirostat = std::stoi(argv[i]);
155-
} else if (arg == "--mirostat_eta") {
155+
} else if (arg == "--mirostat_lr") {
156156
if (++i >= argc) {
157157
invalid_param = true;
158158
break;
159159
}
160160
params.mirostat_eta = std::stof(argv[i]);
161-
} else if (arg == "--mirostat_tau") {
161+
} else if (arg == "--mirostat_ent") {
162162
if (++i >= argc) {
163163
invalid_param = true;
164164
break;
@@ -238,7 +238,7 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
238238
char sign;
239239
std::string value_str;
240240
try {
241-
if (ss >> key && ss >> sign && std::getline(ss, value_str) && (sign == '+' || sign == '-' || sign == '=' || sign == ':')) {
241+
if (ss >> key && ss >> sign && std::getline(ss, value_str) && (sign == '+' || sign == '-')) {
242242
params.logit_bias[key] = std::stof(value_str) * ((sign == '-') ? -1.0f : 1.0f);
243243
} else {
244244
throw std::exception();
@@ -304,18 +304,21 @@ void gpt_print_usage(int /*argc*/, char ** argv, const gpt_params & params) {
304304
fprintf(stderr, " --top_p N top-p sampling (default: %.1f, 1.0 = disabled)\n", (double)params.top_p);
305305
fprintf(stderr, " --tfs N tail free sampling, parameter z (default: %.1f, 1.0 = disabled)\n", (double)params.tfs_z);
306306
fprintf(stderr, " --typical N locally typical sampling, parameter p (default: %.1f, 1.0 = disabled)\n", (double)params.typical_p);
307-
fprintf(stderr, " --repeat_last_n N last n tokens to consider for penalize (default: %d, 0 = disabled)\n", params.repeat_last_n);
307+
fprintf(stderr, " --repeat_last_n N last n tokens to consider for penalize (default: %d, 0 = disabled, -1 = ctx_size)\n", params.repeat_last_n);
308308
fprintf(stderr, " --repeat_penalty N penalize repeat sequence of tokens (default: %.1f, 1.0 = disabled)\n", (double)params.repeat_penalty);
309309
fprintf(stderr, " --presence_penalty N repeat alpha presence penalty (default: %.1f, 0.0 = disabled)\n", (double)params.presence_penalty);
310310
fprintf(stderr, " --frequency_penalty N repeat alpha frequency penalty (default: %.1f, 0.0 = disabled)\n", (double)params.frequency_penalty);
311-
fprintf(stderr, " --mirostat N use mirostat sampling (default: %d, 0 = disabled, 1 = mirostat, 2 = mirostat 2.0)\n", params.mirostat);
312-
fprintf(stderr, " --mirostat_eta N mirostat learning rate (default: %.1f)\n", (double)params.mirostat_eta);
313-
fprintf(stderr, " --mirostat_tau N mirostat target entropy (default: %.1f)\n", (double)params.mirostat_tau);
314-
fprintf(stderr, " -l TOKEN+BIAS, --logit-bias TOKEN+BIAS");
311+
fprintf(stderr, " --mirostat N use Mirostat sampling.\n");
312+
fprintf(stderr, " Top K, Nucleus, Tail Free and Locally Typical samplers are ignored if used.\n");
313+
fprintf(stderr, " (default: %d, 0 = disabled, 1 = Mirostat, 2 = Mirostat 2.0)\n", params.mirostat);
314+
fprintf(stderr, " --mirostat_lr N Mirostat learning rate, parameter eta (default: %.1f)\n", (double)params.mirostat_eta);
315+
fprintf(stderr, " --mirostat_ent N Mirostat target entropy, parameter tau (default: %.1f)\n", (double)params.mirostat_tau);
316+
fprintf(stderr, " -l TOKEN_ID(+/-)BIAS, --logit-bias TOKEN_ID(+/-)BIAS\n");
315317
fprintf(stderr, " modifies the likelihood of token appearing in the completion,\n");
316-
fprintf(stderr, " i.e. `--logit-bias 15043+1` to increase likelihood of token ' Hello'\n");
318+
fprintf(stderr, " i.e. `--logit-bias 15043+1` to increase likelihood of token ' Hello',\n");
319+
fprintf(stderr, " or `--logit-bias 15043-1` to decrease likelihood of token ' Hello'\n");
317320
fprintf(stderr, " -c N, --ctx_size N size of the prompt context (default: %d)\n", params.n_ctx);
318-
fprintf(stderr, " --ignore-eos ignore end of stream token and continue generating (implies --logit-bias 2+-inf)\n");
321+
fprintf(stderr, " --ignore-eos ignore end of stream token and continue generating (implies --logit-bias 2-inf)\n");
319322
fprintf(stderr, " --no-penalize-nl do not penalize newline token\n");
320323
fprintf(stderr, " --memory_f32 use f32 instead of f16 for memory key+value\n");
321324
fprintf(stderr, " --temp N temperature (default: %.1f)\n", (double)params.temp);

examples/main/main.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ int main(int argc, char ** argv) {
230230
fprintf(stderr, "Input prefix: '%s'\n", params.input_prefix.c_str());
231231
}
232232
}
233-
fprintf(stderr, "sampling: repeat_last_n = %d, repeat_penalty = %f, presence_penalty = %f, frequency_penalty = %f, top_k = %d, tfs_z = %f, top_p = %f, typical_p = %f, temp = %f, mirostat = %d, mirostat_eta = %f, mirostat_tau = %f\n",
233+
fprintf(stderr, "sampling: repeat_last_n = %d, repeat_penalty = %f, presence_penalty = %f, frequency_penalty = %f, top_k = %d, tfs_z = %f, top_p = %f, typical_p = %f, temp = %f, mirostat = %d, mirostat_lr = %f, mirostat_ent = %f\n",
234234
params.repeat_last_n, params.repeat_penalty, params.presence_penalty, params.frequency_penalty, params.top_k, params.tfs_z, params.top_p, params.typical_p, params.temp, params.mirostat, params.mirostat_eta, params.mirostat_tau);
235235
fprintf(stderr, "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);
236236
fprintf(stderr, "\n\n");
@@ -331,8 +331,8 @@ int main(int argc, char ** argv) {
331331

332332
std::vector<llama_token_data> candidates;
333333
candidates.reserve(n_vocab);
334-
for (size_t i = 0; i < (size_t) n_vocab; i++) {
335-
candidates.emplace_back(i, logits[i], 0.0f);
334+
for (llama_token token_id = 0; token_id < n_vocab; token_id++) {
335+
candidates.emplace_back(llama_token_data{token_id, logits[token_id], 0.0f});
336336
}
337337

338338
llama_token_data_array candidates_p = { candidates.data(), candidates.size(), false };
@@ -356,11 +356,12 @@ int main(int argc, char ** argv) {
356356
} else {
357357
if (mirostat == 1) {
358358
static float mirostat_mu = 2.0f * mirostat_tau;
359-
static int mirostat_k = 40;
360359
const int mirostat_m = 100;
361-
id = llama_sample_token_mirostat(ctx, &candidates_p, mirostat_tau, mirostat_eta, mirostat_m, float(n_vocab), &mirostat_k, &mirostat_mu);
360+
llama_sample_temperature(ctx, &candidates_p, temp);
361+
id = llama_sample_token_mirostat(ctx, &candidates_p, mirostat_tau, mirostat_eta, mirostat_m, &mirostat_mu);
362362
} else if (mirostat == 2) {
363363
static float mirostat_mu = 2.0f * mirostat_tau;
364+
llama_sample_temperature(ctx, &candidates_p, temp);
364365
id = llama_sample_token_mirostat_v2(ctx, &candidates_p, mirostat_tau, mirostat_eta, &mirostat_mu);
365366
} else {
366367
// Temperature sampling

llama.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,12 +1699,6 @@ void llama_sample_repetition_penalty(struct llama_context * ctx, llama_token_dat
16991699
} else {
17001700
candidates->data[i].logit /= penalty;
17011701
}
1702-
1703-
// But it does not penalize tokens that logits are near zero, which is a problem.
1704-
// Another solution is to convert the logits to probabilities, apply the penalty, and then convert back to logits.
1705-
// float probability = std::exp(candidates[i].logit);
1706-
// probability /= penalty;
1707-
// candidates[i].logit = std::log(probability);
17081702
}
17091703

17101704
candidates->sorted = false;
@@ -1746,9 +1740,9 @@ void llama_sample_frequency_and_presence_penalties(struct llama_context * ctx, l
17461740
}
17471741

17481742

1749-
llama_token llama_sample_token_mirostat(struct llama_context * ctx, llama_token_data_array * candidates, float tau, float eta, int m, float N, int * k, float * mu) {
1743+
llama_token llama_sample_token_mirostat(struct llama_context * ctx, llama_token_data_array * candidates, float tau, float eta, int m, float * mu) {
17501744
assert(ctx);
1751-
1745+
auto N = float(llama_n_vocab(ctx));
17521746
int64_t t_start_sample_us;
17531747
t_start_sample_us = ggml_time_us();
17541748

@@ -1768,12 +1762,10 @@ llama_token llama_sample_token_mirostat(struct llama_context * ctx, llama_token_
17681762

17691763
// Compute k from the estimated s_hat and target surprise value
17701764
float epsilon_hat = s_hat - 1;
1771-
float new_k = powf((epsilon_hat * powf(2, *mu)) / (1 - powf(N, -epsilon_hat)), 1 / s_hat);
1772-
*k = int(std::min(new_k, float(candidates->size)));
1765+
float k = powf((epsilon_hat * powf(2, *mu)) / (1 - powf(N, -epsilon_hat)), 1 / s_hat);
17731766

17741767
// Sample the next word X using top-k sampling
1775-
// printf("llama_sample_mirostat *k = %d\n", *k);
1776-
llama_sample_top_k(nullptr, candidates, *k);
1768+
llama_sample_top_k(nullptr, candidates, int(k));
17771769
if (ctx) {
17781770
ctx->t_sample_us += ggml_time_us() - t_start_sample_us;
17791771
}

llama.h

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -193,37 +193,47 @@ extern "C" {
193193

194194
// Sampling functions
195195

196-
/// @brief Repetition penalty
197-
/// @details Repetition penalty described in CTRL academic paper https://arxiv.org/pdf/1909.05858.pdf with negative logit fix
196+
/// @details Repetition penalty described in CTRL academic paper https://arxiv.org/abs/1909.05858, with negative logit fix.
198197
LLAMA_API void llama_sample_repetition_penalty(struct llama_context * ctx, llama_token_data_array * candidates, llama_token * last_tokens, size_t last_tokens_size, float penalty);
199-
/// @brief Frequency and presence repetition penalties
200-
/// @details Frequency and presence penalties described in OpenAI API https://platform.openai.com/docs/api-reference/parameter-details
198+
199+
/// @details Frequency and presence penalties described in OpenAI API https://platform.openai.com/docs/api-reference/parameter-details.
201200
LLAMA_API void llama_sample_frequency_and_presence_penalties(struct llama_context * ctx, llama_token_data_array * candidates, llama_token * last_tokens, size_t last_tokens_size, float alpha_frequency, float alpha_presence);
202201

202+
/// @details Sorts candidate tokens by their logits in descending order and calculate probabilities based on logits.
203203
LLAMA_API void llama_sample_softmax(struct llama_context * ctx, llama_token_data_array * candidates);
204+
205+
/// @details Top-K sampling described in academic paper "The Curious Case of Neural Text Degeneration" https://arxiv.org/abs/1904.09751
204206
LLAMA_API void llama_sample_top_k(struct llama_context * ctx, llama_token_data_array * candidates, int k, size_t min_keep = 1);
207+
208+
/// @details Nucleus sampling described in academic paper "The Curious Case of Neural Text Degeneration" https://arxiv.org/abs/1904.09751
205209
LLAMA_API void llama_sample_top_p(struct llama_context * ctx, llama_token_data_array * candidates, float p, size_t min_keep = 1);
206210

207-
/// @brief Tail Free Sampling https://www.trentonbricken.com/Tail-Free-Sampling/
211+
/// @details Tail Free Sampling described in https://www.trentonbricken.com/Tail-Free-Sampling/.
208212
LLAMA_API void llama_sample_tail_free(struct llama_context * ctx, llama_token_data_array * candidates, float z, size_t min_keep = 1);
209213

210-
/// @brief Locally Typical Sampling https://arxiv.org/pdf/2202.00666.pdf
214+
/// @details Locally Typical Sampling implementation described in the paper https://arxiv.org/abs/2202.00666.
211215
LLAMA_API void llama_sample_typical(struct llama_context * ctx, llama_token_data_array * candidates, float p, size_t min_keep = 1);
212216
LLAMA_API void llama_sample_temperature(struct llama_context * ctx, llama_token_data_array * candidates, float temp);
213217

214-
/// @brief Mirostat implementation.
215218
/// @details Mirostat 1.0 algorithm described in the paper https://arxiv.org/abs/2007.14966. Uses tokens instead of words.
216-
/// @param ctx The llama context.
217219
/// @param candidates A vector of `llama_token_data` containing the candidate tokens, their probabilities (p), and log-odds (logit) for the current position in the generated text.
218220
/// @param tau The target cross-entropy (or surprise) value you want to achieve for the generated text. A higher value corresponds to more surprising or less predictable text, while a lower value corresponds to less surprising or more predictable text.
219221
/// @param eta The learning rate used to update `mu` based on the error between the target and observed surprisal of the sampled word. A larger learning rate will cause `mu` to be updated more quickly, while a smaller learning rate will result in slower updates.
220222
/// @param m The number of tokens considered in the estimation of `s_hat`. This is an arbitrary value that is used to calculate `s_hat`, which in turn helps to calculate the value of `k`. In the paper, they use `m = 100`, but you can experiment with different values to see how it affects the performance of the algorithm.
221-
/// @param N The size of the vocabulary. This is used in the calculation of the `k` value.
222-
/// @param k A reference to the integer variable used to store the calculated top-k value. The top-k value determines how many of the most probable tokens are considered for sampling.
223-
/// @param mu A reference to the floating-point variable that represents the maximum cross-entropy value. This value is initialized to be twice the target cross-entropy (`2 * tau`) and is updated in the algorithm based on the error between the target and observed surprisal.
224-
LLAMA_API llama_token llama_sample_token_mirostat(struct llama_context * ctx, llama_token_data_array * candidates, float tau, float eta, int m, float N, int * k, float * mu);
223+
/// @param mu Maximum cross-entropy. This value is initialized to be twice the target cross-entropy (`2 * tau`) and is updated in the algorithm based on the error between the target and observed surprisal.
224+
LLAMA_API llama_token llama_sample_token_mirostat(struct llama_context * ctx, llama_token_data_array * candidates, float tau, float eta, int m, float * mu);
225+
226+
/// @details Mirostat 2.0 algorithm described in the paper https://arxiv.org/abs/2007.14966. Uses tokens instead of words.
227+
/// @param candidates A vector of `llama_token_data` containing the candidate tokens, their probabilities (p), and log-odds (logit) for the current position in the generated text.
228+
/// @param tau The target cross-entropy (or surprise) value you want to achieve for the generated text. A higher value corresponds to more surprising or less predictable text, while a lower value corresponds to less surprising or more predictable text.
229+
/// @param eta The learning rate used to update `mu` based on the error between the target and observed surprisal of the sampled word. A larger learning rate will cause `mu` to be updated more quickly, while a smaller learning rate will result in slower updates.
230+
/// @param mu Maximum cross-entropy. This value is initialized to be twice the target cross-entropy (`2 * tau`) and is updated in the algorithm based on the error between the target and observed surprisal.
225231
LLAMA_API llama_token llama_sample_token_mirostat_v2(struct llama_context * ctx, llama_token_data_array * candidates, float tau, float eta, float * mu);
232+
233+
/// @details Selects the token with the highest probability.
226234
LLAMA_API llama_token llama_sample_token_greedy(struct llama_context * ctx, llama_token_data_array * candidates);
235+
236+
/// @details Randomly selects a token from the candidates based on their probabilities.
227237
LLAMA_API llama_token llama_sample_token(struct llama_context * ctx, llama_token_data_array * candidates);
228238

229239
// Performance information

0 commit comments

Comments
 (0)