From 7b73f45541400db3deaa57c55a46e2c1d6398464 Mon Sep 17 00:00:00 2001 From: Samuel Oliveira Alves <107287165+SamuelOliveirads@users.noreply.github.com> Date: Thu, 21 May 2026 02:11:17 -0300 Subject: [PATCH] Add adaptive sampling clone and free functions to manage memory (#1851) --- common/sampling.cpp | 23 +++++++++++++++++++---- src/llama-sampling.h | 4 ++++ src/llama.cpp | 12 ++++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/common/sampling.cpp b/common/sampling.cpp index ad5b7fae..99fc48ce 100644 --- a/common/sampling.cpp +++ b/common/sampling.cpp @@ -12,6 +12,9 @@ #include using json = nlohmann::ordered_json; +struct llama_sampler_adaptive_p * llama_clone_adaptive_p(const struct llama_sampler_adaptive_p * adapt_p_ctx); +void llama_free_adaptive_p(struct llama_sampler_adaptive_p * adapt_p_ctx); + struct common_sampler * common_sampler_init(const struct llama_model * model, const struct common_params_sampling & params) { const llama_vocab * vocab = llama_model_get_vocab(model); @@ -160,9 +163,11 @@ struct common_sampler * common_sampler_init(const struct llama_model * model, co } case llama_sampler_type::ADAPTIVE_P: { - GGML_ASSERT(vocab); - auto n_vocab = llama_vocab_n_tokens(vocab); - result->adapt_p_ctx = llama_init_adaptive_p(n_vocab, params.adaptive_target, params.adaptive_decay, params.adaptive_updt_w_cur, result->rng()); + if (params.adaptive_target >= 0.0f) { + GGML_ASSERT(vocab); + auto n_vocab = llama_vocab_n_tokens(vocab); + result->adapt_p_ctx = llama_init_adaptive_p(n_vocab, params.adaptive_target, params.adaptive_decay, params.adaptive_updt_w_cur, result->rng()); + } break; } default: @@ -185,6 +190,8 @@ void common_sampler_free(struct common_sampler * ctx) { } if (ctx->smpl) llama_sampler_dry_free(ctx->smpl); + if (ctx->adapt_p_ctx) + llama_free_adaptive_p(ctx->adapt_p_ctx); if (ctx->rbudget) common_reasoning_budget_free(ctx->rbudget); delete ctx; @@ -255,6 +262,14 @@ void common_sampler_clone(common_sampler * src, common_sampler * dst) { dst->smpl = llama_sampler_dry_clone(src->smpl); } + if (dst->adapt_p_ctx) { + llama_free_adaptive_p(dst->adapt_p_ctx); + dst->adapt_p_ctx = nullptr; + } + if (src->adapt_p_ctx) { + dst->adapt_p_ctx = llama_clone_adaptive_p(src->adapt_p_ctx); + } + if (dst->rbudget) { common_reasoning_budget_free(dst->rbudget); dst->rbudget = nullptr; @@ -454,7 +469,7 @@ static void sampler_queue( llama_sample_temp(ctx_main, &cur_p, temp); } break; - case llama_sampler_type::ADAPTIVE_P: use_adaptive_p = true; break; + case llama_sampler_type::ADAPTIVE_P: use_adaptive_p = ctx_sampling->adapt_p_ctx != nullptr; break; default : break; } diff --git a/src/llama-sampling.h b/src/llama-sampling.h index 80d5a4f2..3543df86 100644 --- a/src/llama-sampling.h +++ b/src/llama-sampling.h @@ -90,6 +90,10 @@ struct llama_sampler_adaptive_p * llama_init_adaptive_p_impl(int n_vocab, const bool updt_w_cur, const uint32_t seed); +struct llama_sampler_adaptive_p * llama_clone_adaptive_p(const struct llama_sampler_adaptive_p * adapt_p_ctx); + +void llama_free_adaptive_p(struct llama_sampler_adaptive_p * adapt_p_ctx); + void llama_prep_adaptive_p_impl( struct llama_sampling * smpl, llama_token_data_array * candidates, diff --git a/src/llama.cpp b/src/llama.cpp index 7651ec35..9d04f094 100644 --- a/src/llama.cpp +++ b/src/llama.cpp @@ -10533,6 +10533,18 @@ struct llama_sampler_adaptive_p * llama_init_adaptive_p(int n_vocab, const float return llama_init_adaptive_p_impl(n_vocab, target, decay, updt_w_cur, seed); } +struct llama_sampler_adaptive_p * llama_clone_adaptive_p(const struct llama_sampler_adaptive_p * adapt_p_ctx) { + if (adapt_p_ctx == nullptr) { + return nullptr; + } + + return new llama_sampler_adaptive_p(*adapt_p_ctx); +} + +void llama_free_adaptive_p(struct llama_sampler_adaptive_p * adapt_p_ctx) { + delete adapt_p_ctx; +} + void llama_review_adaptive_p(struct llama_sampler_adaptive_p * adapt_p_ctx, const size_t n_unsent, const bool rewind_status) { llama_review_adaptive_p_impl(adapt_p_ctx, n_unsent, rewind_status); }