Repository: ggerganov/llama.cpp
File: src/llama-sampling.cpp (C++)

Code before:
static void llama_sampler_adaptive_p_accept(struct llama_sampler * smpl, llama_token token) {
    auto * ctx = (llama_sampler_adaptive_p *) smpl->ctx;
    if (ctx->pending_token_id == token) {
        GGML_ASSERT(ctx->pending_token_id != LLAMA_TOKEN_NULL);
        GGML_ASSERT(ctx->pending_token_idx != -1);
        // update EMA with the original probability of the selected token
        ctx->weighted_sum = ctx->original_probs[ctx->pending_token_idx] + ctx->decay * ctx->weighted_sum;
        ctx->total_weight = 1.0f + ctx->decay * ctx->total_weight;
    }
    ctx->pending_token_id = LLAMA_TOKEN_NULL;
    ctx->pending_token_idx = -1;
}

static void llama_sampler_adaptive_p_reset(struct llama_sampler * smpl) {
    auto * ctx = (llama_sampler_adaptive_p *) smpl->ctx;
    // ctx->target and ctx->decay never change after init, so it's safe to keep them as is.
    // original_probs is completely overwritten on every call to _apply.
    // so we only need to reset the EMA state and pending token.
    ctx->weighted_sum      = ctx->target / (1.0f - ctx->decay);
    ctx->total_weight      = 1.0f / (1.0f - ctx->decay);
    ctx->pending_token_id  = LLAMA_TOKEN_NULL;
    ctx->pending_token_idx = -1;
}

static struct llama_sampler * llama_sampler_adaptive_p_clone(const struct llama_sampler * smpl) {
    const auto * ctx  = (const llama_sampler_adaptive_p *) smpl->ctx;
    auto * result     = llama_sampler_init_adaptive_p(ctx->target, ctx->decay, ctx->seed);
    auto * result_ctx = (llama_sampler_adaptive_p *) result->ctx;

    // copy everything (target, decay, seed, and RNG are already set)
    result_ctx->weighted_sum      = ctx->weighted_sum;
    result_ctx->total_weight      = ctx->total_weight;
    result_ctx->pending_token_id  = ctx->pending_token_id;
    result_ctx->pending_token_idx = ctx->pending_token_idx;

    return result;
}

static void llama_sampler_adaptive_p_free(struct llama_sampler * smpl) {
    delete (llama_sampler_adaptive_p *) smpl->ctx;
}

static struct llama_sampler_i llama_sampler_adaptive_p_i = {
    /* .name              = */ llama_sampler_adaptive_p_name,
    /* .accept            = */ llama_sampler_adaptive_p_accept,
    /* .apply             = */ llama_sampler_adaptive_p_apply,
    /* .reset             = */ llama_sampler_adaptive_p_reset,
    /* .clone             = */ llama_sampler_adaptive_p_clone,
    /* .free              = */ llama_sampler_adaptive_p_free,
    /* .backend_init      = */ nullptr,
    /* .backend_accept    = */ nullptr,

Review comment (suggestion):
Should apply the same logic for the RNG seeds here as in the `dist` reset:

https://github.com/ggml-org/llama.cpp/blob/516a4ca9b5f2fa72c2a71f412929a67cf76a6213/src/llama-sampling.cpp#L1111-L1117

Code after:
static void llama_sampler_adaptive_p_accept(struct llama_sampler * smpl, llama_token token) {
    auto * ctx = (llama_sampler_adaptive_p *) smpl->ctx;
    if (ctx->pending_token_id == token) {
        GGML_ASSERT(ctx->pending_token_id != LLAMA_TOKEN_NULL);
        GGML_ASSERT(ctx->pending_token_idx != -1);
        // update EMA with the original probability of the selected token
        ctx->weighted_sum = ctx->original_probs[ctx->pending_token_idx] + ctx->decay * ctx->weighted_sum;
        ctx->total_weight = 1.0f + ctx->decay * ctx->total_weight;
    }
    ctx->pending_token_id = LLAMA_TOKEN_NULL;
    ctx->pending_token_idx = -1;
}

static void llama_sampler_adaptive_p_reset(struct llama_sampler * smpl) {
    auto * ctx = (llama_sampler_adaptive_p *) smpl->ctx;
    // ctx->target and ctx->decay never change after init, so it's safe to keep them as is.
    // original_probs is completely overwritten on every call to _apply.
    // so we only need to reset the EMA state and pending token.
    ctx->weighted_sum      = ctx->target / (1.0f - ctx->decay);
    ctx->total_weight      = 1.0f / (1.0f - ctx->decay);
    ctx->pending_token_id  = LLAMA_TOKEN_NULL;
    ctx->pending_token_idx = -1;
    ctx->seed_cur          = get_rng_seed(ctx->seed);
    ctx->rng.seed(ctx->seed_cur);
}

static struct llama_sampler * llama_sampler_adaptive_p_clone(const struct llama_sampler * smpl) {
    const auto * ctx  = (const llama_sampler_adaptive_p *) smpl->ctx;
    auto * result     = llama_sampler_init_adaptive_p(ctx->target, ctx->decay, ctx->seed);
    auto * result_ctx = (llama_sampler_adaptive_p *) result->ctx;

    // copy everything (target, decay, seed, and RNG are already set)
    result_ctx->weighted_sum      = ctx->weighted_sum;
    result_ctx->total_weight      = ctx->total_weight;
    result_ctx->pending_token_id  = ctx->pending_token_id;
    result_ctx->pending_token_idx = ctx->pending_token_idx;

    return result;
}

static void llama_sampler_adaptive_p_free(struct llama_sampler * smpl) {
    delete (llama_sampler_adaptive_p *) smpl->ctx;
}

static struct llama_sampler_i llama_sampler_adaptive_p_i = {
    /* .name              = */ llama_sampler_adaptive_p_name,
    /* .accept            = */ llama_sampler_adaptive_p_accept,
    /* .apply             = */ llama_sampler_adaptive_p_apply,
    /* .reset             = */ llama_sampler_adaptive_p_reset,
    /* .clone             = */ llama_sampler_adaptive_p_clone,
    /* .free              = */ llama_sampler_adaptive_p_free,