mirror of
https://github.com/ikawrakow/ik_llama.cpp.git
synced 2026-02-28 17:14:17 +00:00
Implement Adaptive-P Sampler (#1100)
* initial implementation of adaptive-p sampler * explicitly mark candidates unsorted + cleanup qualifiers * cosmetic update * reorg prototypes * lockstep with mainline * add _impl for _init + reorg * add LLAMA_API to prototypes * update sharpness to 10 * lockstep: rng seed * delete llama_sampling member in llama_sampler_adaptive_p * fix LLAMA_API return type * lockstep: rng seed cont * actually correct implementation * lockstep: sorting behavior * const -> constexpr for known constants * add missing space * fix softmax usage in adaptive p sampler * cosmetic changes * implement do-not-sort version of softmax * simpify rng seed, add static to constexpr * refactor: remove iface + use shared rng + use actually original probabilities * adaptive-p: add dedicated rng back in * fix initial max_logit + add float vector to adaptive p sampler context + stochastic sampling * adaptive-p: fuse first softmax with transformation * adaptive-p: implement binary search selection * adaptive-p: update comment
This commit is contained in:
@@ -99,7 +99,7 @@ struct llama_sampling_context * llama_sampling_init(const struct llama_vocab* vo
|
||||
result->n_valid = 0;
|
||||
}
|
||||
result->grammar = grmr;
|
||||
// init DRY
|
||||
llama_sampling_set_rng_seed(result, params.seed);
|
||||
for (const auto& cnstr : params.samplers_sequence)
|
||||
{
|
||||
switch (cnstr)
|
||||
@@ -116,11 +116,16 @@ struct llama_sampling_context * llama_sampling_init(const struct llama_vocab* vo
|
||||
|
||||
break;
|
||||
}
|
||||
case llama_sampler_type::ADAPTIVE_P:
|
||||
{
|
||||
result->adapt_p_ctx=llama_sampler_init_adaptive_p(params.adaptive_target, params.adaptive_decay, result->rng());
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
llama_sampling_set_rng_seed(result, params.seed);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -247,11 +252,13 @@ std::string llama_sampling_print(const llama_sampling_params & params) {
|
||||
"\trepeat_last_n = %d, repeat_penalty = %.3f, frequency_penalty = %.3f, presence_penalty = %.3f\n"
|
||||
"\ttop_k = %d, tfs_z = %.3f, top_p = %.3f, min_p = %.3f, typical_p = %.3f, temp = %.3f\n"
|
||||
"\tmirostat = %d, mirostat_lr = %.3f, mirostat_ent = %.3f\n"
|
||||
"\txtc_probability = %.3f, xtc_threshold = %.3f, top_n_sigma = %.3f",
|
||||
"\txtc_probability = %.3f, xtc_threshold = %.3f, top_n_sigma = %.3f\n"
|
||||
"\tadaptive_target = %.2f, adaptive_decay = %.2f",
|
||||
params.penalty_last_n, params.penalty_repeat, params.penalty_freq, params.penalty_present,
|
||||
params.top_k, params.tfs_z, params.top_p, params.min_p, params.typical_p, params.temp,
|
||||
params.mirostat, params.mirostat_eta, params.mirostat_tau,
|
||||
params.xtc_probability, params.xtc_threshold, params.top_n_sigma);
|
||||
params.xtc_probability, params.xtc_threshold, params.top_n_sigma,
|
||||
params.adaptive_target, params.adaptive_decay);
|
||||
|
||||
return std::string(result);
|
||||
}
|
||||
@@ -283,6 +290,7 @@ std::string llama_sampling_type_to_str(llama_sampler_type sampler_type) {
|
||||
case llama_sampler_type::TEMPERATURE: return "temperature";
|
||||
case llama_sampler_type::XTC : return "xtc";
|
||||
case llama_sampler_type::TOP_N_SIGMA: return "top_n_sigma";
|
||||
case llama_sampler_type::ADAPTIVE_P : return "adaptive_p";
|
||||
default : return "";
|
||||
}
|
||||
}
|
||||
@@ -297,7 +305,8 @@ std::vector<llama_sampler_type> llama_sampling_types_from_names(const std::vecto
|
||||
{"tfs_z", llama_sampler_type::TFS_Z},
|
||||
{"xtc", llama_sampler_type::XTC},
|
||||
{"top_n_sigma", llama_sampler_type::TOP_N_SIGMA},
|
||||
{"temperature", llama_sampler_type::TEMPERATURE}
|
||||
{"temperature", llama_sampler_type::TEMPERATURE},
|
||||
{"adaptive_p", llama_sampler_type::ADAPTIVE_P},
|
||||
};
|
||||
|
||||
// since samplers names are written multiple ways
|
||||
@@ -314,7 +323,8 @@ std::vector<llama_sampler_type> llama_sampling_types_from_names(const std::vecto
|
||||
{"tfs", llama_sampler_type::TFS_Z},
|
||||
{"xtc", llama_sampler_type::XTC},
|
||||
{"top-n-sigma", llama_sampler_type::TOP_N_SIGMA},
|
||||
{"temp", llama_sampler_type::TEMPERATURE}
|
||||
{"temp", llama_sampler_type::TEMPERATURE},
|
||||
{"adaptive-p", llama_sampler_type::ADAPTIVE_P},
|
||||
};
|
||||
|
||||
std::vector<llama_sampler_type> sampler_types;
|
||||
@@ -351,7 +361,8 @@ std::vector<llama_sampler_type> llama_sampling_types_from_chars(const std::strin
|
||||
{'f', llama_sampler_type::TFS_Z},
|
||||
{'x', llama_sampler_type::XTC},
|
||||
{'n', llama_sampler_type::TOP_N_SIGMA},
|
||||
{'t', llama_sampler_type::TEMPERATURE}
|
||||
{'t', llama_sampler_type::TEMPERATURE},
|
||||
{'w', llama_sampler_type::ADAPTIVE_P},
|
||||
};
|
||||
|
||||
std::vector<llama_sampler_type> sampler_types;
|
||||
@@ -405,6 +416,7 @@ static void sampler_queue(
|
||||
llama_sample_temp(ctx_main, &cur_p, temp);
|
||||
}
|
||||
break;
|
||||
case llama_sampler_type::ADAPTIVE_P: llama_sample_adaptive_p(ctx_main, ctx_sampling->adapt_p_ctx, &cur_p); break;
|
||||
default : break;
|
||||
}
|
||||
}
|
||||
@@ -422,6 +434,7 @@ static llama_token llama_sampling_sample_impl(
|
||||
const int mirostat = params.mirostat;
|
||||
const float mirostat_tau = params.mirostat_tau;
|
||||
const float mirostat_eta = params.mirostat_eta;
|
||||
const float adaptive_target = params.adaptive_target;
|
||||
|
||||
std::vector<float> original_logits;
|
||||
auto cur_p = llama_sampling_prepare(ctx_sampling, ctx_main, ctx_cfg, idx, /* apply_grammar= */ is_resampling, &original_logits);
|
||||
@@ -451,6 +464,17 @@ static llama_token llama_sampling_sample_impl(
|
||||
} else if (mirostat == 2) {
|
||||
llama_sample_temp(ctx_main, &cur_p, temp);
|
||||
id = llama_sample_token_mirostat_v2(ctx_main, &cur_p, mirostat_tau, mirostat_eta, &ctx_sampling->mirostat_mu);
|
||||
} else if (adaptive_target >= 0.0f) {
|
||||
// adaptive p sampling
|
||||
static thread_local std::vector<float> orig_probs;
|
||||
orig_probs.resize(cur_p.size);
|
||||
|
||||
// store original probabilities
|
||||
for (size_t ii = 0; ii < cur_p.size; ++ii) {
|
||||
orig_probs[ii] = cur_p.data[ii].p;
|
||||
}
|
||||
sampler_queue(ctx_main, params, ctx_sampling, cur_p, std::max(1, params.min_keep));
|
||||
id = llama_sample_token_adaptive_p(ctx_main, &cur_p, ctx_sampling->adapt_p_ctx, orig_probs.data());
|
||||
} else {
|
||||
// temperature sampling
|
||||
size_t min_keep = std::max(1, params.min_keep);
|
||||
|
||||
Reference in New Issue
Block a user