From acada1a5e734b5f40d9d9daa2cfd0a812d0300dd Mon Sep 17 00:00:00 2001 From: MaggotHATE Date: Fri, 11 Oct 2024 15:36:25 +0500 Subject: [PATCH] Made algorithm safer and more readable --- src/llama-sampling.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/llama-sampling.cpp b/src/llama-sampling.cpp index 907408295..dd31ba9a5 100644 --- a/src/llama-sampling.cpp +++ b/src/llama-sampling.cpp @@ -1107,15 +1107,18 @@ static void llama_sample_xtc_apply(struct llama_sampler * smpl, llama_token_data } } - size_t to_remove = pos_last - (1 + pos_first); + int to_remove = pos_last - (1 + pos_first); - if (cur_p->size - to_remove < ctx->min_keep || to_remove < 1) return; + if (cur_p->size - to_remove >= ctx->min_keep && to_remove > 0) { - for (size_t i = pos_first + 1; i < cur_p->size - to_remove + 1; ++i) { - cur_p->data[i] = cur_p->data[i + to_remove]; + size_t last_idx = cur_p->size - to_remove; + + for (size_t i = pos_first + 1; i <= last_idx; ++i) { + cur_p->data[i] = cur_p->data[i + to_remove]; + } + + cur_p->size = cur_p->size - to_remove; } - - cur_p->size = cur_p->size - to_remove; } static struct llama_sampler * llama_sampler_xtc_clone(const struct llama_sampler * smpl) {