remove `target_range` param, make `target == 1` no-op, cleanup code

This commit is contained in:
ddh0 2025-12-11 22:43:10 -06:00
parent dcada035b4
commit 2d62bbea9f
4 changed files with 77 additions and 70 deletions

View File

@ -1503,23 +1503,16 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
).set_sparam()); ).set_sparam());
add_opt(common_arg( add_opt(common_arg(
{"--power-law-target"}, "N", {"--power-law-target"}, "N",
string_format("Power Law sampler target probability (default: %.2f; allowed range 0.0 to 1.0)\n" string_format("target probability for Power Law sampling (valid range 0.0 to 1.0; <0 = disabled) "
"[(more info)](https://github.com/ggml-org/llama.cpp/pull/17927)", "(%.1f = default)\n[(more info)](https://github.com/ggml-org/llama.cpp/pull/17927)",
(double)params.sampling.power_law_target), (double)params.sampling.power_law_target),
[](common_params & params, const std::string & value) { [](common_params & params, const std::string & value) {
params.sampling.power_law_target = std::stof(value); params.sampling.power_law_target = std::stof(value);
} }
).set_sparam()); ).set_sparam());
add_opt(common_arg(
{"--power-law-target-range"}, "N",
string_format("Power Law sampler adaptive target range (target±range) (default: %.2f; 0.0 = fixed target)", (double)params.sampling.power_law_target_range),
[](common_params & params, const std::string & value) {
params.sampling.power_law_target_range = std::stof(value);
}
).set_sparam());
add_opt(common_arg( add_opt(common_arg(
{"--power-law-window-size"}, "N", {"--power-law-window-size"}, "N",
string_format("Power Law sampler rolling window size, in tokens (default: %d; 0 = fixed target)", params.sampling.power_law_window_size), string_format("rolling window size for target adaptation in Power Law sampling (≤0 = fixed target; %d = default)", params.sampling.power_law_window_size),
[](common_params & params, int value) { [](common_params & params, int value) {
params.sampling.power_law_window_size = value; params.sampling.power_law_window_size = value;
} }

View File

@ -184,9 +184,8 @@ struct common_params_sampling {
float dry_base = 1.75f; // 0.0 = disabled; multiplier * base ^ (length of sequence before token - allowed length) float dry_base = 1.75f; // 0.0 = disabled; multiplier * base ^ (length of sequence before token - allowed length)
int32_t dry_allowed_length = 2; // tokens extending repetitions beyond this receive penalty int32_t dry_allowed_length = 2; // tokens extending repetitions beyond this receive penalty
int32_t dry_penalty_last_n = -1; // how many tokens to scan for repetitions (0 = disable penalty, -1 = context size) int32_t dry_penalty_last_n = -1; // how many tokens to scan for repetitions (0 = disable penalty, -1 = context size)
float power_law_target = 0.5; // target probability (0.0 to 1.0) float power_law_target = -1.0f; // target probability for Power Law sampling (valid range 0.0 to 1.0; <0 = disabled)
float power_law_target_range = 0.5; // adapt the target within this range (target +/- range) int32_t power_law_window_size = 10; // rolling window size for target adaptation in Power Law sampling (≤0 = fixed target)
int32_t power_law_window_size = 10; // rolling history window size for target adaptation
int32_t mirostat = 0; // 0 = disabled, 1 = mirostat, 2 = mirostat 2.0 int32_t mirostat = 0; // 0 = disabled, 1 = mirostat, 2 = mirostat 2.0
float top_n_sigma = -1.00f; // -1.0 = disabled float top_n_sigma = -1.00f; // -1.0 = disabled
float mirostat_tau = 5.00f; // target entropy float mirostat_tau = 5.00f; // target entropy
@ -199,7 +198,6 @@ struct common_params_sampling {
std::vector<std::string> dry_sequence_breakers = {"\n", ":", "\"", "*"}; // default sequence breakers for DRY std::vector<std::string> dry_sequence_breakers = {"\n", ":", "\"", "*"}; // default sequence breakers for DRY
std::vector<enum common_sampler_type> samplers = { std::vector<enum common_sampler_type> samplers = {
COMMON_SAMPLER_TYPE_PENALTIES, COMMON_SAMPLER_TYPE_PENALTIES,
COMMON_SAMPLER_TYPE_DRY, COMMON_SAMPLER_TYPE_DRY,

View File

@ -1297,13 +1297,16 @@ extern "C" {
/// ///
/// it is recommended to only perform minimal truncation before this sampler. /// it is recommended to only perform minimal truncation before this sampler.
/// ///
/// @param target target probability (valid range 0.0 to 1.0; <0 = disabled)
/// @param window_size rolling window size for target adaptation (≤0 = fixed target)
/// @param seed RNG seed
///
/// ref: https://github.com/MrJackSpade/llama.cpp/tree/master (original impl, documentation) /// ref: https://github.com/MrJackSpade/llama.cpp/tree/master (original impl, documentation)
/// ref: https://github.com/ggml-org/llama.cpp/pull/17927 (llama.cpp PR) /// ref: https://github.com/ggml-org/llama.cpp/pull/17927 (llama.cpp PR)
LLAMA_API struct llama_sampler * llama_sampler_init_power_law( LLAMA_API struct llama_sampler * llama_sampler_init_power_law(
float target, // target probability (0.0 to 1.0) float target,
float target_range, // adaptive target range (target±range) int32_t window_size,
int32_t window_size, // rolling history window size for target adaptation uint32_t seed);
uint32_t seed); // RNG seed
LLAMA_API struct llama_sampler * llama_sampler_init_logit_bias( LLAMA_API struct llama_sampler * llama_sampler_init_logit_bias(
int32_t n_vocab, int32_t n_vocab,

View File

@ -2326,12 +2326,11 @@ struct llama_sampler * llama_sampler_init_dry_testing(int32_t context_size, floa
struct llama_sampler_power_law { struct llama_sampler_power_law {
const float target; const float target;
const float target_range;
const int32_t window_size; const int32_t window_size;
const uint32_t seed;
const uint32_t seed;
std::mt19937 rng; std::mt19937 rng;
ring_buffer<float> history; ring_buffer<float> window;
}; };
static const char * llama_sampler_power_law_name(const struct llama_sampler * /*smpl*/) { static const char * llama_sampler_power_law_name(const struct llama_sampler * /*smpl*/) {
@ -2341,66 +2340,82 @@ static const char * llama_sampler_power_law_name(const struct llama_sampler * /*
static void llama_sampler_power_law_apply(struct llama_sampler * smpl, llama_token_data_array * cur_p) { static void llama_sampler_power_law_apply(struct llama_sampler * smpl, llama_token_data_array * cur_p) {
auto * ctx = (llama_sampler_power_law *) smpl->ctx; auto * ctx = (llama_sampler_power_law *) smpl->ctx;
// clamp the target range to [0.0, 1.0] if (ctx->target < 0.0f) {
const float min_target = std::max(ctx->target - ctx->target_range, 0.0f); // no-op: just sample from the distribution as-is
const float max_target = std::min(ctx->target + ctx->target_range, 1.0f); llama_sampler_softmax_impl(cur_p, false);
const int idx = llama_sample_dist(cur_p, ctx->rng);
cur_p->selected = idx;
return;
}
// fixed power law transform parameters (from original implementation)
const float distribution_width = 0.2f;
const float peak_logit_value = 3.0f;
const float tail_heaviness = 3.0f;
// compute probabilities to get the "original" values // compute probabilities to get the "original" values
llama_sampler_softmax_impl(cur_p, false); llama_sampler_softmax_impl(cur_p, false);
// store original probabilities (needed for history update) // store original probabilities (used for future target adaptation)
std::vector<float> original_probs; std::vector<float> original_probs;
original_probs.reserve(cur_p->size); original_probs.reserve(cur_p->size);
for (size_t i = 0; i < cur_p->size; ++i) { for (size_t i = 0; i < cur_p->size; ++i) {
original_probs.push_back(cur_p->data[i].p); original_probs.push_back(cur_p->data[i].p);
} }
//
// calculate adaptive target // calculate adaptive target
//
const float min_target = 0.0f;
const float max_target = 1.0f;
float computed_target = ctx->target; float computed_target = ctx->target;
if (ctx->history.size() > 0) { if (ctx->window.size() > 0) {
float sum_excluding_oldest = 0.0f; float sum_excluding_oldest = 0.0f;
size_t sz = ctx->history.size(); size_t sz = ctx->window.size();
// sum all except the oldest element // sum all except the oldest element
for (size_t i = 0; i < sz - 1; ++i) { for (size_t i = 0; i < sz - 1; ++i) {
sum_excluding_oldest += ctx->history.rat(i); sum_excluding_oldest += ctx->window.rat(i);
} }
float next_value = (ctx->target * ctx->window_size) - sum_excluding_oldest; float next_value = (ctx->target * ctx->window_size) - sum_excluding_oldest;
computed_target = std::max(min_target, std::min(next_value, max_target)); computed_target = std::max(min_target, std::min(next_value, max_target));
} }
// apply power law transformation //
// power law transform
//
for (size_t i = 0; i < cur_p->size; ++i) { for (size_t i = 0; i < cur_p->size; ++i) {
float p = cur_p->data[i].p; float p = cur_p->data[i].p;
float normalized_distance = std::abs(p - computed_target) / 0.2f; float normalized_distance = std::abs(p - computed_target) / distribution_width;
cur_p->data[i].logit = 3.0f / (1.0f + std::pow(normalized_distance, 3.0f)); cur_p->data[i].logit = peak_logit_value / (1.0f + std::pow(normalized_distance, tail_heaviness));
} }
llama_sampler_softmax_impl(cur_p, false); llama_sampler_softmax_impl(cur_p, false);
// sample from distribution // sample from the transformed distribution
const int idx = llama_sample_dist(cur_p, ctx->rng); const int idx = llama_sample_dist(cur_p, ctx->rng);
// set sampled token
cur_p->selected = idx; cur_p->selected = idx;
// update history with ORIGINAL probability // add the ORIGINAL probability to the rolling window
ctx->history.push_back(original_probs[idx]); ctx->window.push_back(original_probs[idx]);
} }
static void llama_sampler_power_law_reset(struct llama_sampler * smpl) { static void llama_sampler_power_law_reset(struct llama_sampler * smpl) {
auto * ctx = (llama_sampler_power_law *) smpl->ctx; auto * ctx = (llama_sampler_power_law *) smpl->ctx;
ctx->history = ring_buffer<float>(ctx->window_size); ctx->window = ring_buffer<float>(ctx->window_size);
} }
static struct llama_sampler * llama_sampler_power_law_clone(const struct llama_sampler * smpl) { static struct llama_sampler * llama_sampler_power_law_clone(const struct llama_sampler * smpl) {
const auto * ctx = (const llama_sampler_power_law *) smpl->ctx; const auto * ctx = (const llama_sampler_power_law *) smpl->ctx;
auto * result = llama_sampler_init_power_law(ctx->target, ctx->target_range, ctx->window_size, ctx->seed); auto * result = llama_sampler_init_power_law(ctx->target, ctx->window_size, ctx->seed);
auto * result_ctx = (llama_sampler_power_law *) result->ctx; auto * result_ctx = (llama_sampler_power_law *) result->ctx;
result_ctx->rng = ctx->rng; result_ctx->rng = ctx->rng;
result_ctx->history = ctx->history; result_ctx->window = ctx->window;
return result; return result;
} }
@ -2420,7 +2435,6 @@ static struct llama_sampler_i llama_sampler_power_law_i = {
struct llama_sampler * llama_sampler_init_power_law( struct llama_sampler * llama_sampler_init_power_law(
float target, float target,
float target_range,
int32_t window_size, int32_t window_size,
uint32_t seed uint32_t seed
) { ) {
@ -2429,11 +2443,10 @@ struct llama_sampler * llama_sampler_init_power_law(
/* .iface = */ &llama_sampler_power_law_i, /* .iface = */ &llama_sampler_power_law_i,
/* .ctx = */ new llama_sampler_power_law { /* .ctx = */ new llama_sampler_power_law {
/* .target = */ target, /* .target = */ target,
/* .target_range = */ target_range,
/* .window_size = */ window_size, /* .window_size = */ window_size,
/* .seed = */ seed_cur, /* .seed = */ seed_cur,
/* .rng = */ std::mt19937(seed_cur), /* .rng = */ std::mt19937(seed_cur),
/* .history = */ ring_buffer<float>(window_size), /* .window = */ ring_buffer<float>(window_size),
} }
); );
} }