Skip to content

Commit ccb265c

Browse files
ikawrakowIwan Kawrakow
andauthored
Adding the XTC sampler (#486)
Co-authored-by: Iwan Kawrakow <[email protected]>
1 parent 4f8b05a commit ccb265c

File tree

7 files changed

+76
-2
lines changed

7 files changed

+76
-2
lines changed

common/common.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,16 @@ bool gpt_params_find_arg(int argc, char ** argv, const std::string & arg, gpt_pa
649649
sparams.mirostat_tau = std::stof(argv[i]);
650650
return true;
651651
}
652+
if (arg == "--xtc-probability") {
653+
CHECK_ARG
654+
sparams.xtc_probability = std::stof(argv[i]);
655+
return true;
656+
}
657+
if (arg == "--xtc-threshold") {
658+
CHECK_ARG
659+
sparams.xtc_threshold = std::stof(argv[i]);
660+
return true;
661+
}
652662
if (arg == "--cfg-negative-prompt") {
653663
CHECK_ARG
654664
sparams.cfg_negative_prompt = argv[i];
@@ -1635,6 +1645,8 @@ void gpt_params_print_usage(int /*argc*/, char ** argv, const gpt_params & param
16351645
"(default: %d, 0 = disabled, 1 = Mirostat, 2 = Mirostat 2.0)", sparams.mirostat });
16361646
options.push_back({ "*", " --mirostat-lr N", "Mirostat learning rate, parameter eta (default: %.1f)", (double)sparams.mirostat_eta });
16371647
options.push_back({ "*", " --mirostat-ent N", "Mirostat target entropy, parameter tau (default: %.1f)", (double)sparams.mirostat_tau });
1648+
options.push_back({ "*", " --xtc-probability p", "xtc probability (default: %.1f, 0.0 = disabled)", (double)sparams.xtc_probability });
1649+
options.push_back({ "*", " --xtc-threshold t", "xtc threshold (default: %.1f, 0.0 = disabled)", (double)sparams.xtc_threshold});
16381650
options.push_back({ "*", " -l TOKEN_ID(+/-)BIAS", "modifies the likelihood of token appearing in the completion,\n"
16391651
"i.e. `--logit-bias 15043+1` to increase likelihood of token ' Hello',\n"
16401652
"or `--logit-bias 15043-1` to decrease likelihood of token ' Hello'" });
@@ -3396,6 +3408,8 @@ void yaml_dump_non_result_info(FILE * stream, const gpt_params & params, const l
33963408
fprintf(stream, "mirostat: %d # default: 0 (disabled)\n", sparams.mirostat);
33973409
fprintf(stream, "mirostat_ent: %f # default: 5.0\n", sparams.mirostat_tau);
33983410
fprintf(stream, "mirostat_lr: %f # default: 0.1\n", sparams.mirostat_eta);
3411+
fprintf(stream, "xtc_probability: %f # default: 0.0\n", sparams.xtc_probability);
3412+
fprintf(stream, "xtc_threshold: %f # default: 0.0\n", sparams.xtc_threshold);
33993413
fprintf(stream, "mlock: %s # default: false\n", params.use_mlock ? "true" : "false");
34003414
fprintf(stream, "model: %s # default: %s\n", params.model.c_str(), DEFAULT_MODEL_PATH);
34013415
fprintf(stream, "model_draft: %s # default:\n", params.model_draft.c_str());

common/sampling.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,12 @@ std::string llama_sampling_print(const llama_sampling_params & params) {
121121
snprintf(result, sizeof(result),
122122
"\trepeat_last_n = %d, repeat_penalty = %.3f, frequency_penalty = %.3f, presence_penalty = %.3f\n"
123123
"\ttop_k = %d, tfs_z = %.3f, top_p = %.3f, min_p = %.3f, typical_p = %.3f, temp = %.3f\n"
124-
"\tmirostat = %d, mirostat_lr = %.3f, mirostat_ent = %.3f",
124+
"\tmirostat = %d, mirostat_lr = %.3f, mirostat_ent = %.3f\n"
125+
"\txtc_probability = %.3f, xtc_threshold = %.3f",
125126
params.penalty_last_n, params.penalty_repeat, params.penalty_freq, params.penalty_present,
126127
params.top_k, params.tfs_z, params.top_p, params.min_p, params.typical_p, params.temp,
127-
params.mirostat, params.mirostat_eta, params.mirostat_tau);
128+
params.mirostat, params.mirostat_eta, params.mirostat_tau,
129+
params.xtc_probability, params.xtc_threshold);
128130

129131
return std::string(result);
130132
}
@@ -153,6 +155,7 @@ std::string llama_sampling_type_to_str(llama_sampler_type sampler_type) {
153155
case llama_sampler_type::TOP_P: return "top_p";
154156
case llama_sampler_type::MIN_P: return "min_p";
155157
case llama_sampler_type::TEMPERATURE: return "temperature";
158+
case llama_sampler_type::XTC : return "xtc";
156159
default : return "";
157160
}
158161
}
@@ -164,6 +167,7 @@ std::vector<llama_sampler_type> llama_sampling_types_from_names(const std::vecto
164167
{"typical_p", llama_sampler_type::TYPICAL_P},
165168
{"min_p", llama_sampler_type::MIN_P},
166169
{"tfs_z", llama_sampler_type::TFS_Z},
170+
{"xtc", llama_sampler_type::XTC},
167171
{"temperature", llama_sampler_type::TEMPERATURE}
168172
};
169173

@@ -178,6 +182,7 @@ std::vector<llama_sampler_type> llama_sampling_types_from_names(const std::vecto
178182
{"min-p", llama_sampler_type::MIN_P},
179183
{"tfs-z", llama_sampler_type::TFS_Z},
180184
{"tfs", llama_sampler_type::TFS_Z},
185+
{"xtc", llama_sampler_type::XTC},
181186
{"temp", llama_sampler_type::TEMPERATURE}
182187
};
183188

@@ -212,6 +217,7 @@ std::vector<llama_sampler_type> llama_sampling_types_from_chars(const std::strin
212217
{'y', llama_sampler_type::TYPICAL_P},
213218
{'m', llama_sampler_type::MIN_P},
214219
{'f', llama_sampler_type::TFS_Z},
220+
{'x', llama_sampler_type::XTC},
215221
{'t', llama_sampler_type::TEMPERATURE}
216222
};
217223

@@ -240,6 +246,8 @@ static void sampler_queue(
240246
const float min_p = params.min_p;
241247
const float tfs_z = params.tfs_z;
242248
const float typical_p = params.typical_p;
249+
const float xtc_probability = params.xtc_probability;
250+
const float xtc_threshold = params.xtc_threshold;
243251
const std::vector<llama_sampler_type> & samplers_sequence = params.samplers_sequence;
244252

245253
for (auto sampler_type : samplers_sequence) {
@@ -249,6 +257,7 @@ static void sampler_queue(
249257
case llama_sampler_type::TYPICAL_P: llama_sample_typical (ctx_main, &cur_p, typical_p, min_keep); break;
250258
case llama_sampler_type::TOP_P : llama_sample_top_p (ctx_main, &cur_p, top_p, min_keep); break;
251259
case llama_sampler_type::MIN_P : llama_sample_min_p (ctx_main, &cur_p, min_p, min_keep); break;
260+
case llama_sampler_type::XTC : llama_sample_xtc (ctx_main, &cur_p, xtc_probability, xtc_threshold, min_keep); break;
252261
case llama_sampler_type::TEMPERATURE:
253262
if (dynatemp_range > 0) {
254263
float dynatemp_min = std::max(0.0f, temp - dynatemp_range);

common/sampling.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ enum class llama_sampler_type : char {
1515
TOP_P = 'p',
1616
MIN_P = 'm',
1717
TFS_Z = 'f',
18+
XTC = 'x',
1819
TYPICAL_P = 'y',
1920
TEMPERATURE = 't'
2021
};
@@ -39,6 +40,8 @@ typedef struct llama_sampling_params {
3940
int32_t mirostat = 0; // 0 = disabled, 1 = mirostat, 2 = mirostat 2.0
4041
float mirostat_tau = 5.00f; // target entropy
4142
float mirostat_eta = 0.10f; // learning rate
43+
float xtc_probability = 0.0f; // xtc probability
44+
float xtc_threshold = 1.0f; // xtc threashold, disabled if > 0.5
4245
bool penalize_nl = false; // consider newlines as a repeatable token
4346
uint32_t seed = LLAMA_DEFAULT_SEED; // the seed used to initialize llama_sampling_context
4447

include/llama.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,14 @@ extern "C" {
12081208
llama_token_data_array * candidates,
12091209
float temp);
12101210

1211+
/// @details XTC sampler as described in https://github.com/oobabooga/text-generation-webui/pull/6335
1212+
LLAMA_API void llama_sample_xtc(
1213+
struct llama_context * ctx,
1214+
llama_token_data_array * candidates_p,
1215+
float probability,
1216+
float threshold,
1217+
size_t min_keep);
1218+
12111219
/// @details Mirostat 1.0 algorithm described in the paper https://arxiv.org/abs/2007.14966. Uses tokens instead of words.
12121220
/// @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.
12131221
/// @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.

src/llama-sampling.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,40 @@ void llama_sample_temp_impl(struct llama_sampling * smpl, llama_token_data_array
434434
}
435435
}
436436

437+
void llama_sample_xtc_impl(struct llama_sampling * smpl, llama_token_data_array * candidates, float probability, float threshold, size_t min_keep) {
438+
if (probability < 0 || threshold > 0.5f || candidates->size < 2) {
439+
return;
440+
}
441+
GGML_ASSERT(smpl);
442+
const int64_t t_start_sample_us = ggml_time_us();
443+
if (probability < 1) {
444+
std::uniform_real_distribution<float> distribution(0.0f, 1.0f);
445+
float chance = distribution(smpl->rng);
446+
if (chance > probability) return;
447+
}
448+
449+
llama_sample_softmax_impl(nullptr, candidates);
450+
451+
auto cur_size = candidates->size;
452+
453+
int pos_last = 0;
454+
455+
for (size_t i = 0; i < candidates->size; ++i) {
456+
if (candidates->data[i].p >= threshold) {
457+
pos_last = i;
458+
} else break;
459+
}
460+
461+
if (candidates->size - pos_last >= min_keep && pos_last > 0) {
462+
candidates->data += pos_last;
463+
candidates->size -= pos_last;
464+
}
465+
466+
smpl->t_sample_us += ggml_time_us() - t_start_sample_us;
467+
smpl->n_sample++;
468+
469+
}
470+
437471
void llama_sample_repetition_penalties_impl(
438472
struct llama_sampling * smpl,
439473
llama_token_data_array * candidates,

src/llama-sampling.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ void llama_sample_tail_free_impl(struct llama_sampling * smpl, llama_token_data_
3232
void llama_sample_typical_impl (struct llama_sampling * smpl, llama_token_data_array * candidates, float p, size_t min_keep);
3333
void llama_sample_entropy_impl (struct llama_sampling * smpl, llama_token_data_array * candidates, float min_temp, float max_temp, float exponent_val);
3434
void llama_sample_temp_impl (struct llama_sampling * smpl, llama_token_data_array * candidates, float temp);
35+
void llama_sample_xtc_impl (struct llama_sampling * smpl, llama_token_data_array * candidates, float probability, float threshold, size_t min_keep);
3536

3637
void llama_sample_repetition_penalties_impl(
3738
struct llama_sampling * smpl,

src/llama.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23265,6 +23265,11 @@ void llama_sample_temp(struct llama_context * ctx, llama_token_data_array * cand
2326523265
llama_sample_temp_impl(ctx ? &ctx->sampling : nullptr, candidates_p, temp);
2326623266
}
2326723267

23268+
void llama_sample_xtc(struct llama_context * ctx, llama_token_data_array * candidates_p,
23269+
float probability, float threshold, size_t min_keep) {
23270+
llama_sample_xtc_impl(ctx ? &ctx->sampling : nullptr, candidates_p, probability, threshold, min_keep);
23271+
}
23272+
2326823273
void llama_sample_repetition_penalties(
2326923274
struct llama_context * ctx,
2327023275
llama_token_data_array * candidates,

0 commit comments

Comments
 (0)