Skip to content

Commit 4f976f1

Browse files
Added --logit-bias and --no-penalize-nl, removed std::span
1 parent e62c491 commit 4f976f1

File tree

6 files changed

+185
-165
lines changed

6 files changed

+185
-165
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ endif
3535

3636
# keep standard at C11 and C++11
3737
CFLAGS = -I. -O3 -DNDEBUG -std=c11 -fPIC
38-
CXXFLAGS = -I. -I./examples -O3 -DNDEBUG -std=c++20 -fPIC
38+
CXXFLAGS = -I. -I./examples -O3 -DNDEBUG -std=c++11 -fPIC
3939
LDFLAGS =
4040

4141
# warnings

examples/common.cpp

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <string>
77
#include <iterator>
88
#include <algorithm>
9+
#include <sstream>
10+
#include <iostream>
911

1012
#if defined (_WIN32)
1113
#include <fcntl.h>
@@ -132,18 +134,18 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
132134
break;
133135
}
134136
params.repeat_penalty = std::stof(argv[i]);
135-
} else if (arg == "--alpha_frequency") {
137+
} else if (arg == "--frequency_penalty") {
136138
if (++i >= argc) {
137139
invalid_param = true;
138140
break;
139141
}
140-
params.alpha_frequency = std::stof(argv[i]);
141-
} else if (arg == "--alpha_presence") {
142+
params.frequency_penalty = std::stof(argv[i]);
143+
} else if (arg == "--presence_penalty") {
142144
if (++i >= argc) {
143145
invalid_param = true;
144146
break;
145147
}
146-
params.alpha_presence = std::stof(argv[i]);
148+
params.presence_penalty = std::stof(argv[i]);
147149
} else if (arg == "--mirostat") {
148150
if (++i >= argc) {
149151
invalid_param = true;
@@ -223,7 +225,28 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
223225
} else if (arg == "--perplexity") {
224226
params.perplexity = true;
225227
} else if (arg == "--ignore-eos") {
226-
params.ignore_eos = true;
228+
params.logit_bias[llama_token_eos()] = -INFINITY;
229+
} else if (arg == "--no-penalize-nl") {
230+
params.penalize_nl = false;
231+
} else if (arg == "-l" || arg == "--logit-bias") {
232+
if (++i >= argc) {
233+
invalid_param = true;
234+
break;
235+
}
236+
std::stringstream ss(argv[i]);
237+
llama_token key;
238+
char sign;
239+
std::string value_str;
240+
try {
241+
if (ss >> key && ss >> sign && std::getline(ss, value_str) && (sign == '+' || sign == '-' || sign == '=' || sign == ':')) {
242+
params.logit_bias[key] = std::stof(value_str) * ((sign == '-') ? -1.0f : 1.0f);
243+
} else {
244+
throw std::exception();
245+
}
246+
} catch (const std::exception &e) {
247+
invalid_param = true;
248+
break;
249+
}
227250
} else if (arg == "--n_parts") {
228251
if (++i >= argc) {
229252
invalid_param = true;
@@ -277,19 +300,23 @@ void gpt_print_usage(int /*argc*/, char ** argv, const gpt_params & params) {
277300
fprintf(stderr, " -f FNAME, --file FNAME\n");
278301
fprintf(stderr, " prompt file to start generation.\n");
279302
fprintf(stderr, " -n N, --n_predict N number of tokens to predict (default: %d, -1 = infinity)\n", params.n_predict);
280-
fprintf(stderr, " --top_k N top-k sampling (default: %d, disabled: 0)\n", params.top_k);
281-
fprintf(stderr, " --top_p N top-p sampling (default: %.1f, disabled: 1.0)\n", (double)params.top_p);
282-
fprintf(stderr, " --tfs N tail free sampling, parameter z (default: %.1f, disabled: 1.0)\n", (double)params.tfs_z);
283-
fprintf(stderr, " --typical N locally typical sampling, parameter p (default: %.1f, disabled: 1.0)\n", (double)params.typical_p);
284-
fprintf(stderr, " --repeat_last_n N last n tokens to consider for penalize (default: %d, disabled: 0)\n", params.repeat_last_n);
285-
fprintf(stderr, " --repeat_penalty N penalize repeat sequence of tokens (default: %.1f, disabled: 1.0)\n", (double)params.repeat_penalty);
286-
fprintf(stderr, " --alpha_presence N repeat alpha presence (default: %.1f, disabled: 0.0)\n", (double)params.alpha_presence);
287-
fprintf(stderr, " --alpha_frequency N repeat alpha frequency (default: %.1f, disabled: 0.0)\n", (double)params.alpha_frequency);
288-
fprintf(stderr, " --mirostat N use mirostat sampling (default: %d, disabled: 0, mirostat: 1, mirostat 2.0: 2)\n", params.mirostat);
303+
fprintf(stderr, " --top_k N top-k sampling (default: %d, 0 = disabled)\n", params.top_k);
304+
fprintf(stderr, " --top_p N top-p sampling (default: %.1f, 1.0 = disabled)\n", (double)params.top_p);
305+
fprintf(stderr, " --tfs N tail free sampling, parameter z (default: %.1f, 1.0 = disabled)\n", (double)params.tfs_z);
306+
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);
308+
fprintf(stderr, " --repeat_penalty N penalize repeat sequence of tokens (default: %.1f, 1.0 = disabled)\n", (double)params.repeat_penalty);
309+
fprintf(stderr, " --presence_penalty N repeat alpha presence penalty (default: %.1f, 0.0 = disabled)\n", (double)params.presence_penalty);
310+
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);
289312
fprintf(stderr, " --mirostat_eta N mirostat learning rate (default: %.1f)\n", (double)params.mirostat_eta);
290313
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");
315+
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");
291317
fprintf(stderr, " -c N, --ctx_size N size of the prompt context (default: %d)\n", params.n_ctx);
292-
fprintf(stderr, " --ignore-eos ignore end of stream token and continue generating\n");
318+
fprintf(stderr, " --ignore-eos ignore end of stream token and continue generating (implies --logit-bias 2+-inf)\n");
319+
fprintf(stderr, " --no-penalize-nl do not penalize newline token\n");
293320
fprintf(stderr, " --memory_f32 use f32 instead of f16 for memory key+value\n");
294321
fprintf(stderr, " --temp N temperature (default: %.1f)\n", (double)params.temp);
295322
fprintf(stderr, " --n_parts N number of model parts (default: -1 = determine from dimensions)\n");

examples/common.h

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <vector>
99
#include <random>
1010
#include <thread>
11+
#include <unordered_map>
1112

1213
//
1314
// CLI argument parsing
@@ -23,18 +24,19 @@ struct gpt_params {
2324
int32_t n_keep = 0; // number of tokens to keep from initial prompt
2425

2526
// sampling parameters
26-
int32_t top_k = 0; // <= 0 to use vocab size
27-
float top_p = 1.0f; // 1.0 = disabled
28-
float tfs_z = 1.0f; // 1.0 = disabled
29-
float typical_p = 1.0f; // 1.0 = disabled
30-
float temp = 1.0f; // 1.0 = disabled
27+
std::unordered_map<llama_token, float> logit_bias; // logit bias for specific tokens
28+
int32_t top_k = 0; // <= 0 to use vocab size
29+
float top_p = 1.0f; // 1.0 = disabled
30+
float tfs_z = 1.0f; // 1.0 = disabled
31+
float typical_p = 1.0f; // 1.0 = disabled
32+
float temp = 1.0f; // 1.0 = disabled
3133
float repeat_penalty = 1.0f; // 1.0 = disabled
32-
int32_t repeat_last_n = -1; // last n tokens to penalize (0 = disable penalty, -1 = context size)
33-
float alpha_frequency = 0.0f; // 0.0 = disabled
34-
float alpha_presence = 0.0f; // 0.0 = disabled
35-
int mirostat = 0; // 0 = disabled, 1 = mirostat, 2 = mirostat 2.0
36-
float mirostat_tau = 5.0f; // target entropy
37-
float mirostat_eta = 0.1f; // learning rate
34+
int32_t repeat_last_n = -1; // last n tokens to penalize (0 = disable penalty, -1 = context size)
35+
float frequency_penalty = 0.0f; // 0.0 = disabled
36+
float presence_penalty = 0.0f; // 0.0 = disabled
37+
int mirostat = 0; // 0 = disabled, 1 = mirostat, 2 = mirostat 2.0
38+
float mirostat_tau = 5.0f; // target entropy
39+
float mirostat_eta = 0.1f; // learning rate
3840

3941
std::string model = "models/lamma-7B/ggml-model.bin"; // model path
4042
std::string prompt = "";
@@ -53,7 +55,7 @@ struct gpt_params {
5355
bool interactive_start = false; // wait for user input immediately
5456

5557
bool instruct = false; // instruction mode (used for Alpaca models)
56-
bool ignore_eos = false; // do not stop generating after eos
58+
bool penalize_nl = true; // consider newlines as a repeatable token
5759
bool perplexity = false; // compute perplexity over the prompt
5860
bool use_mmap = true; // use mmap for faster loads
5961
bool use_mlock = false; // use mlock to keep model in memory

examples/main/main.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ 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, alpha_presence = %f, alpha_frequency = %f, top_k = %d, tfs_z = %f, top_p = %f, typical_p = %f, temp = %f, mirostat = %d, mirostat_eta = %f, mirostat_tau = %f\n",
234-
params.repeat_last_n, params.repeat_penalty, params.alpha_presence, params.alpha_frequency, params.top_k, params.tfs_z, params.top_p, params.typical_p, params.temp, params.mirostat, params.mirostat_eta, params.mirostat_tau);
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",
234+
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");
237237

@@ -311,20 +311,22 @@ int main(int argc, char ** argv) {
311311
const float typical_p = params.typical_p;
312312
const int32_t repeat_last_n = params.repeat_last_n < 0 ? n_ctx : params.repeat_last_n;
313313
const float repeat_penalty = params.repeat_penalty;
314-
const float alpha_presence = params.alpha_presence;
315-
const float alpha_frequency = params.alpha_frequency;
316-
const int mirostat = params.mirostat;
314+
const float alpha_presence = params.presence_penalty;
315+
const float alpha_frequency = params.frequency_penalty;
316+
const int mirostat = params.mirostat;
317317
const float mirostat_tau = params.mirostat_tau;
318318
const float mirostat_eta = params.mirostat_eta;
319+
const bool penalize_nl = params.penalize_nl;
319320

320321
llama_token id = 0;
321322

322323
{
323324
auto logits = llama_get_logits(ctx);
324325
auto n_vocab = llama_n_vocab(ctx);
325326

326-
if (params.ignore_eos) {
327-
logits[llama_token_eos()] = -INFINITY;
327+
// Apply params.logit_bias map
328+
for (auto it = params.logit_bias.begin(); it != params.logit_bias.end(); it++) {
329+
logits[it->first] += it->second;
328330
}
329331

330332
std::vector<llama_token_data> candidates;
@@ -336,14 +338,17 @@ int main(int argc, char ** argv) {
336338
llama_token_data_array candidates_p = { candidates.data(), candidates.size(), false };
337339

338340
// Apply penalties
341+
float nl_logit = logits[llama_token_nl()];
339342
auto last_n_repeat = std::min(std::min((int)last_n_tokens.size(), repeat_last_n), n_ctx);
340343
llama_sample_repetition_penalty(ctx, &candidates_p,
341344
last_n_tokens.data() + last_n_tokens.size() - last_n_repeat,
342345
last_n_repeat, repeat_penalty);
343346
llama_sample_frequency_and_presence_penalties(ctx, &candidates_p,
344347
last_n_tokens.data() + last_n_tokens.size() - last_n_repeat,
345348
last_n_repeat, alpha_frequency, alpha_presence);
346-
349+
if (!penalize_nl) {
350+
logits[llama_token_nl()] = nl_logit;
351+
}
347352

348353
if (temp <= 0) {
349354
// Greedy sampling

0 commit comments

Comments
 (0)