2023-10-11 19:35:46 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "llama.h"
|
|
|
|
|
2023-10-18 13:21:57 +00:00
|
|
|
#include "grammar-parser.h"
|
|
|
|
|
2023-10-11 19:35:46 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <unordered_map>
|
|
|
|
|
2024-02-11 13:43:31 +00:00
|
|
|
// sampler types
|
|
|
|
enum class llama_sampler_type : char {
|
2024-02-16 11:33:25 +00:00
|
|
|
TOP_K = 'k',
|
|
|
|
TOP_P = 'p',
|
|
|
|
MIN_P = 'm',
|
|
|
|
TFS_Z = 'f',
|
|
|
|
TYPICAL_P = 'y',
|
|
|
|
TEMPERATURE = 't'
|
2024-02-11 13:43:31 +00:00
|
|
|
};
|
|
|
|
|
2023-10-11 19:35:46 +00:00
|
|
|
// sampling parameters
|
|
|
|
typedef struct llama_sampling_params {
|
2023-12-05 10:05:51 +00:00
|
|
|
int32_t n_prev = 64; // number of previous tokens to remember
|
|
|
|
int32_t n_probs = 0; // if greater than 0, output the probabilities of top n_probs tokens.
|
2024-02-18 19:11:16 +00:00
|
|
|
int32_t min_keep = 0; // 0 = disabled, otherwise samplers should return at least min_keep tokens
|
2023-12-05 10:05:51 +00:00
|
|
|
int32_t top_k = 40; // <= 0 to use vocab size
|
|
|
|
float top_p = 0.95f; // 1.0 = disabled
|
|
|
|
float min_p = 0.05f; // 0.0 = disabled
|
|
|
|
float tfs_z = 1.00f; // 1.0 = disabled
|
|
|
|
float typical_p = 1.00f; // 1.0 = disabled
|
2024-01-17 07:17:50 +00:00
|
|
|
float temp = 0.80f; // <= 0.0 to sample greedily, 0.0 to not output probabilities
|
2024-01-25 20:06:22 +00:00
|
|
|
float dynatemp_range = 0.00f; // 0.0 = disabled
|
|
|
|
float dynatemp_exponent = 1.00f; // controls how entropy maps to temperature in dynamic temperature sampler
|
2023-12-05 10:05:51 +00:00
|
|
|
int32_t penalty_last_n = 64; // last n tokens to penalize (0 = disable penalty, -1 = context size)
|
2024-03-19 08:21:54 +00:00
|
|
|
float penalty_repeat = 1.00f; // 1.0 = disabled
|
2023-12-05 10:05:51 +00:00
|
|
|
float penalty_freq = 0.00f; // 0.0 = disabled
|
|
|
|
float penalty_present = 0.00f; // 0.0 = disabled
|
|
|
|
int32_t mirostat = 0; // 0 = disabled, 1 = mirostat, 2 = mirostat 2.0
|
|
|
|
float mirostat_tau = 5.00f; // target entropy
|
|
|
|
float mirostat_eta = 0.10f; // learning rate
|
2024-03-19 08:21:54 +00:00
|
|
|
bool penalize_nl = false; // consider newlines as a repeatable token
|
2024-02-11 13:43:31 +00:00
|
|
|
|
|
|
|
std::vector<llama_sampler_type> samplers_sequence = {
|
|
|
|
llama_sampler_type::TOP_K,
|
|
|
|
llama_sampler_type::TFS_Z,
|
|
|
|
llama_sampler_type::TYPICAL_P,
|
|
|
|
llama_sampler_type::TOP_P,
|
|
|
|
llama_sampler_type::MIN_P,
|
2024-02-16 11:33:25 +00:00
|
|
|
llama_sampler_type::TEMPERATURE
|
2024-02-11 13:43:31 +00:00
|
|
|
};
|
2023-10-11 19:35:46 +00:00
|
|
|
|
2023-10-20 18:07:23 +00:00
|
|
|
std::string grammar; // optional BNF-like grammar to constrain sampling
|
2023-10-11 19:35:46 +00:00
|
|
|
|
|
|
|
// Classifier-Free Guidance
|
|
|
|
// https://arxiv.org/abs/2306.17806
|
2023-10-20 18:07:23 +00:00
|
|
|
std::string cfg_negative_prompt; // string to help guidance
|
|
|
|
float cfg_scale = 1.f; // how strong is guidance
|
2023-10-11 19:35:46 +00:00
|
|
|
|
|
|
|
std::unordered_map<llama_token, float> logit_bias; // logit bias for specific tokens
|
2023-12-23 09:31:49 +00:00
|
|
|
|
|
|
|
std::vector<llama_token> penalty_prompt_tokens;
|
|
|
|
bool use_penalty_prompt_tokens = false;
|
2023-10-11 19:35:46 +00:00
|
|
|
} llama_sampling_params;
|
|
|
|
|
|
|
|
// general sampler context
|
2023-10-18 13:21:57 +00:00
|
|
|
// TODO: move to llama.h
|
|
|
|
struct llama_sampling_context {
|
|
|
|
// parameters that will be used for sampling
|
2023-10-11 19:35:46 +00:00
|
|
|
llama_sampling_params params;
|
|
|
|
|
2023-10-18 13:21:57 +00:00
|
|
|
// mirostat sampler state
|
|
|
|
float mirostat_mu;
|
2023-10-11 19:35:46 +00:00
|
|
|
|
|
|
|
llama_grammar * grammar;
|
2023-10-18 13:21:57 +00:00
|
|
|
|
|
|
|
// internal
|
|
|
|
grammar_parser::parse_state parsed_grammar;
|
|
|
|
|
|
|
|
// TODO: replace with ring-buffer
|
|
|
|
std::vector<llama_token> prev;
|
|
|
|
std::vector<llama_token_data> cur;
|
|
|
|
};
|
2023-10-11 19:35:46 +00:00
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
// Create a new sampling context instance.
|
2023-10-20 18:07:23 +00:00
|
|
|
struct llama_sampling_context * llama_sampling_init(const struct llama_sampling_params & params);
|
2023-10-18 13:21:57 +00:00
|
|
|
|
|
|
|
void llama_sampling_free(struct llama_sampling_context * ctx);
|
|
|
|
|
|
|
|
// Reset the sampler context
|
|
|
|
// - clear prev tokens
|
|
|
|
// - reset grammar
|
|
|
|
void llama_sampling_reset(llama_sampling_context * ctx);
|
|
|
|
|
|
|
|
// Copy the sampler context
|
|
|
|
void llama_sampling_cp(llama_sampling_context * src, llama_sampling_context * dst);
|
2023-10-11 19:35:46 +00:00
|
|
|
|
2023-10-20 18:07:23 +00:00
|
|
|
// Get the last sampled token
|
|
|
|
llama_token llama_sampling_last(llama_sampling_context * ctx);
|
|
|
|
|
|
|
|
// Get a string representation of the last sampled tokens
|
|
|
|
std::string llama_sampling_prev_str(llama_sampling_context * ctx_sampling, llama_context * ctx_main, int n);
|
|
|
|
|
|
|
|
// Print sampling parameters into a string
|
|
|
|
std::string llama_sampling_print(const llama_sampling_params & params);
|
2023-12-05 10:05:51 +00:00
|
|
|
|
|
|
|
// Print sampling order into a string
|
|
|
|
std::string llama_sampling_order_print(const llama_sampling_params & params);
|
2023-10-20 18:07:23 +00:00
|
|
|
|
2023-10-11 19:35:46 +00:00
|
|
|
// this is a common sampling function used across the examples for convenience
|
|
|
|
// it can serve as a starting point for implementing your own sampling function
|
|
|
|
// Note: When using multiple sequences, it is the caller's responsibility to call
|
2023-10-18 13:21:57 +00:00
|
|
|
// llama_sampling_reset when a sequence ends
|
2023-10-11 19:35:46 +00:00
|
|
|
//
|
|
|
|
// required:
|
2023-10-18 13:21:57 +00:00
|
|
|
// - ctx_main: context to use for sampling
|
2023-10-11 19:35:46 +00:00
|
|
|
// - ctx_sampling: sampling-specific context
|
|
|
|
//
|
|
|
|
// optional:
|
2023-10-18 13:21:57 +00:00
|
|
|
// - ctx_cfg: context to use for classifier-free guidance
|
|
|
|
// - idx: sample from llama_get_logits_ith(ctx, idx)
|
2023-10-11 19:35:46 +00:00
|
|
|
//
|
|
|
|
// returns:
|
|
|
|
// - token: sampled token
|
|
|
|
// - candidates: vector of candidate tokens
|
|
|
|
//
|
|
|
|
llama_token llama_sampling_sample(
|
2023-10-18 13:21:57 +00:00
|
|
|
struct llama_sampling_context * ctx_sampling,
|
|
|
|
struct llama_context * ctx_main,
|
|
|
|
struct llama_context * ctx_cfg,
|
|
|
|
int idx = 0);
|
|
|
|
|
2024-03-24 08:54:07 +00:00
|
|
|
// Prepares and adjusts the set of token candidates for sampling based on penalties, biases, and sampling parameters.
|
|
|
|
llama_token_data_array llama_sampling_prepare(
|
2024-03-04 18:24:00 +00:00
|
|
|
struct llama_sampling_context * ctx_sampling,
|
|
|
|
struct llama_context * ctx_main,
|
|
|
|
struct llama_context * ctx_cfg,
|
2024-03-24 08:54:07 +00:00
|
|
|
int idx = 0,
|
|
|
|
bool apply_grammar = true,
|
|
|
|
std::vector<float> * original_logits = nullptr);
|
2024-03-04 18:24:00 +00:00
|
|
|
|
2023-10-18 13:21:57 +00:00
|
|
|
void llama_sampling_accept(
|
|
|
|
struct llama_sampling_context * ctx_sampling,
|
|
|
|
struct llama_context * ctx_main,
|
2023-10-20 18:07:23 +00:00
|
|
|
llama_token id,
|
|
|
|
bool apply_grammar);
|