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

Code before:
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,
    /* .backend_apply     = */ nullptr,
    /* .backend_set_input = */ nullptr,
};

struct llama_sampler * llama_sampler_init_adaptive_p(
    float    target,
    float    decay,
    uint32_t seed
) {
    auto seed_cur = get_rng_seed(seed);
    float clamped_decay = std::clamp(decay, 0.0f, 0.99f);
    return llama_sampler_init(
        /* .iface = */ &llama_sampler_adaptive_p_i,
        /* .ctx   = */ new llama_sampler_adaptive_p {
            /* .target            = */ target,
            /* .decay             = */ clamped_decay,
            /* .seed              = */ seed_cur,
            /* .rng               = */ std::mt19937(seed_cur),
            /* .weighted_sum      = */ target / (1.0f - clamped_decay),
            /* .total_weight      = */ 1.0f / (1.0f - clamped_decay),
            /* .original_probs    = */ {},
            /* .pending_token_id  = */ LLAMA_TOKEN_NULL,
            /* .pending_token_idx = */ -1
        }
    );
}

// logit-bias

struct llama_sampler_logit_bias : public llama_sampler_backend {
    const int32_t n_vocab;

    const std::vector<llama_logit_bias> logit_bias;

    std::vector<llama_logit_bias> to_search;

    struct ggml_tensor * inp_logit_bias;
    struct ggml_tensor * inp_logit_idxs;

Review comment (suggestion):
This `seed_cur` logic breaks when the `seed` is `LLAMA_DEFAULT_SEED`. In that case we generate a random seed based on the time. But with this implementation, if you clone the sampler, it will inherit the "seed_cur" of the source sampler, instead of generating a new seed.

See how the `dist` sampler maintains a `seed_cur` in its context and replicate the logic here.

Code after:
    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,
    /* .backend_apply     = */ nullptr,
    /* .backend_set_input = */ nullptr,
};

struct llama_sampler * llama_sampler_init_adaptive_p(
    float    target,
    float    decay,
    uint32_t seed
) {
    auto seed_cur = get_rng_seed(seed);
    float clamped_decay = std::clamp(decay, 0.0f, 0.99f);
    return llama_sampler_init(
        /* .iface = */ &llama_sampler_adaptive_p_i,
        /* .ctx   = */ new llama_sampler_adaptive_p {
            /* .target            = */ target,
            /* .decay             = */ clamped_decay,
            /* .seed              = */ seed,
            /* .seed_cur          = */ seed_cur,
            /* .rng               = */ std::mt19937(seed_cur),
            /* .weighted_sum      = */ target / (1.0f - clamped_decay),
            /* .total_weight      = */ 1.0f / (1.0f - clamped_decay),
            /* .original_probs    = */ {},
            /* .pending_token_id  = */ LLAMA_TOKEN_NULL,
            /* .pending_token_idx = */ -1
        }
    );
}

// logit-bias

struct llama_sampler_logit_bias : public llama_sampler_backend {
    const int32_t n_vocab;

    const std::vector<llama_logit_bias> logit_bias;