summaryrefslogtreecommitdiff
path: root/llama.h
diff options
context:
space:
mode:
Diffstat (limited to 'llama.h')
-rw-r--r--llama.h265
1 files changed, 141 insertions, 124 deletions
diff --git a/llama.h b/llama.h
index 9d732f91..aa5b7d69 100644
--- a/llama.h
+++ b/llama.h
@@ -34,29 +34,18 @@
# define DEPRECATED(func, hint) func
#endif
-#define LLAMA_FILE_MAGIC_GGJT 0x67676a74u // 'ggjt'
-#define LLAMA_FILE_MAGIC_GGLA 0x67676c61u // 'ggla'
-#define LLAMA_FILE_MAGIC_GGMF 0x67676d66u // 'ggmf'
-#define LLAMA_FILE_MAGIC_GGML 0x67676d6cu // 'ggml'
-#define LLAMA_FILE_MAGIC_GGSN 0x6767736eu // 'ggsn'
+#define LLAMA_DEFAULT_SEED 0xFFFFFFFF
-#define LLAMA_FILE_VERSION 3
-#define LLAMA_FILE_MAGIC LLAMA_FILE_MAGIC_GGJT
-#define LLAMA_FILE_MAGIC_UNVERSIONED LLAMA_FILE_MAGIC_GGML
-#define LLAMA_SESSION_MAGIC LLAMA_FILE_MAGIC_GGSN
-#define LLAMA_SESSION_VERSION 1
+#define LLAMA_FILE_MAGIC_GGSN 0x6767736eu // 'ggsn'
-#define LLAMA_DEFAULT_SEED 0xFFFFFFFF
+#define LLAMA_SESSION_MAGIC LLAMA_FILE_MAGIC_GGSN
+#define LLAMA_SESSION_VERSION 1
#if defined(GGML_USE_CUBLAS) || defined(GGML_USE_CLBLAST) || defined(GGML_USE_METAL)
// Defined when llama.cpp is compiled with support for offloading model layers to GPU.
#define LLAMA_SUPPORTS_GPU_OFFLOAD
#endif
-#ifndef LLAMA_DEFAULT_RMS_EPS
-#define LLAMA_DEFAULT_RMS_EPS 5e-6f
-#endif
-
#ifdef __cplusplus
extern "C" {
#endif
@@ -72,6 +61,50 @@ extern "C" {
typedef int llama_token;
+ enum llama_log_level {
+ LLAMA_LOG_LEVEL_ERROR = 2,
+ LLAMA_LOG_LEVEL_WARN = 3,
+ LLAMA_LOG_LEVEL_INFO = 4
+ };
+
+ enum llama_vocab_type {
+ LLAMA_VOCAB_TYPE_SPM = 0, // SentencePiece
+ LLAMA_VOCAB_TYPE_BPE = 1, // Byte Pair Encoding
+ };
+
+ enum llama_token_type {
+ LLAMA_TOKEN_TYPE_UNDEFINED = 0,
+ LLAMA_TOKEN_TYPE_NORMAL = 1,
+ LLAMA_TOKEN_TYPE_UNKNOWN = 2,
+ LLAMA_TOKEN_TYPE_CONTROL = 3,
+ LLAMA_TOKEN_TYPE_USER_DEFINED = 4,
+ LLAMA_TOKEN_TYPE_UNUSED = 5,
+ LLAMA_TOKEN_TYPE_BYTE = 6,
+ };
+
+ // model file types
+ enum llama_ftype {
+ LLAMA_FTYPE_ALL_F32 = 0,
+ LLAMA_FTYPE_MOSTLY_F16 = 1, // except 1d tensors
+ LLAMA_FTYPE_MOSTLY_Q4_0 = 2, // except 1d tensors
+ LLAMA_FTYPE_MOSTLY_Q4_1 = 3, // except 1d tensors
+ LLAMA_FTYPE_MOSTLY_Q4_1_SOME_F16 = 4, // tok_embeddings.weight and output.weight are F16
+ // LLAMA_FTYPE_MOSTLY_Q4_2 = 5, // support has been removed
+ // LLAMA_FTYPE_MOSTLY_Q4_3 = 6, // support has been removed
+ LLAMA_FTYPE_MOSTLY_Q8_0 = 7, // except 1d tensors
+ LLAMA_FTYPE_MOSTLY_Q5_0 = 8, // except 1d tensors
+ LLAMA_FTYPE_MOSTLY_Q5_1 = 9, // except 1d tensors
+ LLAMA_FTYPE_MOSTLY_Q2_K = 10,// except 1d tensors
+ LLAMA_FTYPE_MOSTLY_Q3_K_S = 11,// except 1d tensors
+ LLAMA_FTYPE_MOSTLY_Q3_K_M = 12,// except 1d tensors
+ LLAMA_FTYPE_MOSTLY_Q3_K_L = 13,// except 1d tensors
+ LLAMA_FTYPE_MOSTLY_Q4_K_S = 14,// except 1d tensors
+ LLAMA_FTYPE_MOSTLY_Q4_K_M = 15,// except 1d tensors
+ LLAMA_FTYPE_MOSTLY_Q5_K_S = 16,// except 1d tensors
+ LLAMA_FTYPE_MOSTLY_Q5_K_M = 17,// except 1d tensors
+ LLAMA_FTYPE_MOSTLY_Q6_K = 18,// except 1d tensors
+ };
+
typedef struct llama_token_data {
llama_token id; // token id
float logit; // log-odds of the token
@@ -86,25 +119,10 @@ extern "C" {
typedef void (*llama_progress_callback)(float progress, void *ctx);
- enum llama_log_level {
- LLAMA_LOG_LEVEL_ERROR = 2,
- LLAMA_LOG_LEVEL_WARN = 3,
- LLAMA_LOG_LEVEL_INFO = 4
- };
-
- // Signature for logging events
- // Note that text includes the new line character at the end for most events.
- // If your logging mechanism cannot handle that, check if the last character is '\n' and strip it
- // if it exists.
- // It might not exist for progress report where '.' is output repeatedly.
- typedef void (*llama_log_callback)(enum llama_log_level level, const char * text, void * user_data);
-
struct llama_context_params {
uint32_t seed; // RNG seed, -1 for random
int32_t n_ctx; // text context
int32_t n_batch; // prompt processing batch size
- int32_t n_gqa; // grouped-query attention (TEMP - will be moved to model hparams)
- float rms_norm_eps; // rms norm epsilon (TEMP - will be moved to model hparams)
int32_t n_gpu_layers; // number of layers to store in VRAM
int32_t main_gpu; // the GPU that is used for scratch and small tensors
@@ -129,33 +147,18 @@ extern "C" {
bool use_mlock; // force system to keep model in RAM
bool embedding; // embedding mode only
};
- // model file types
- enum llama_ftype {
- LLAMA_FTYPE_ALL_F32 = 0,
- LLAMA_FTYPE_MOSTLY_F16 = 1, // except 1d tensors
- LLAMA_FTYPE_MOSTLY_Q4_0 = 2, // except 1d tensors
- LLAMA_FTYPE_MOSTLY_Q4_1 = 3, // except 1d tensors
- LLAMA_FTYPE_MOSTLY_Q4_1_SOME_F16 = 4, // tok_embeddings.weight and output.weight are F16
- // LLAMA_FTYPE_MOSTLY_Q4_2 = 5, // support has been removed
- // LLAMA_FTYPE_MOSTLY_Q4_3 = 6, // support has been removed
- LLAMA_FTYPE_MOSTLY_Q8_0 = 7, // except 1d tensors
- LLAMA_FTYPE_MOSTLY_Q5_0 = 8, // except 1d tensors
- LLAMA_FTYPE_MOSTLY_Q5_1 = 9, // except 1d tensors
- LLAMA_FTYPE_MOSTLY_Q2_K = 10,// except 1d tensors
- LLAMA_FTYPE_MOSTLY_Q3_K_S = 11,// except 1d tensors
- LLAMA_FTYPE_MOSTLY_Q3_K_M = 12,// except 1d tensors
- LLAMA_FTYPE_MOSTLY_Q3_K_L = 13,// except 1d tensors
- LLAMA_FTYPE_MOSTLY_Q4_K_S = 14,// except 1d tensors
- LLAMA_FTYPE_MOSTLY_Q4_K_M = 15,// except 1d tensors
- LLAMA_FTYPE_MOSTLY_Q5_K_S = 16,// except 1d tensors
- LLAMA_FTYPE_MOSTLY_Q5_K_M = 17,// except 1d tensors
- LLAMA_FTYPE_MOSTLY_Q6_K = 18,// except 1d tensors
- };
+
+ // Signature for logging events
+ // Note that text includes the new line character at the end for most events.
+ // If your logging mechanism cannot handle that, check if the last character is '\n' and strip it
+ // if it exists.
+ // It might not exist for progress report where '.' is output repeatedly.
+ typedef void (*llama_log_callback)(enum llama_log_level level, const char * text, void * user_data);
// model quantization parameters
typedef struct llama_model_quantize_params {
int nthread; // number of threads to use for quantizing, if <=0 will use std::thread::hardware_concurrency()
- enum llama_ftype ftype; // quantize to this llama_ftype
+ enum llama_ftype ftype; // quantize to this llama_ftype
bool allow_requantize; // allow quantizing non-f32/f16 tensors
bool quantize_output_tensor; // quantize output.weight
} llama_model_quantize_params;
@@ -208,27 +211,16 @@ extern "C" {
int32_t n_eval;
};
- // Set callback for all future logging events.
- // If this is not called, or NULL is supplied, everything is output on stderr.
- LLAMA_API void llama_log_set(llama_log_callback log_callback, void * user_data);
-
- LLAMA_API int llama_max_devices();
+ LLAMA_API struct llama_context_params llama_context_default_params(void);
+ LLAMA_API struct llama_model_quantize_params llama_model_quantize_default_params(void);
- LLAMA_API struct llama_context_params llama_context_default_params();
- LLAMA_API struct llama_model_quantize_params llama_model_quantize_default_params();
-
- LLAMA_API bool llama_mmap_supported();
- LLAMA_API bool llama_mlock_supported();
-
- // TODO: not great API - very likely to change
// Initialize the llama + ggml backend
// If numa is true, use NUMA optimizations
// Call once at the start of the program
LLAMA_API void llama_backend_init(bool numa);
- // Call once at the end of the program - currently only used for MPI
- LLAMA_API void llama_backend_free();
- LLAMA_API int64_t llama_time_us();
+ // Call once at the end of the program - currently only used for MPI
+ LLAMA_API void llama_backend_free(void);
LLAMA_API struct llama_model * llama_load_model_from_file(
const char * path_model,
@@ -240,17 +232,26 @@ extern "C" {
struct llama_model * model,
struct llama_context_params params);
- // Various functions for loading a ggml llama model.
- // Allocate (almost) all memory needed for the model.
- // Return NULL on failure
- LLAMA_API DEPRECATED(struct llama_context * llama_init_from_file(
- const char * path_model,
- struct llama_context_params params),
- "please use llama_load_model_from_file combined with llama_new_context_with_model instead");
-
// Frees all allocated memory
LLAMA_API void llama_free(struct llama_context * ctx);
+ LLAMA_API int64_t llama_time_us(void);
+
+ LLAMA_API int llama_max_devices (void);
+ LLAMA_API bool llama_mmap_supported (void);
+ LLAMA_API bool llama_mlock_supported(void);
+
+ LLAMA_API int llama_n_vocab(const struct llama_context * ctx);
+ LLAMA_API int llama_n_ctx (const struct llama_context * ctx);
+ LLAMA_API int llama_n_embd (const struct llama_context * ctx);
+
+ LLAMA_API int llama_model_n_vocab(const struct llama_model * model);
+ LLAMA_API int llama_model_n_ctx (const struct llama_model * model);
+ LLAMA_API int llama_model_n_embd (const struct llama_model * model);
+
+ // Get a string describing the model type
+ LLAMA_API int llama_model_type(const struct llama_model * model, char * buf, size_t buf_size);
+
// Returns 0 on success
LLAMA_API int llama_model_quantize(
const char * fname_inp,
@@ -272,9 +273,9 @@ extern "C" {
LLAMA_API int llama_model_apply_lora_from_file(
const struct llama_model * model,
- const char * path_lora,
- const char * path_base_model,
- int n_threads);
+ const char * path_lora,
+ const char * path_base_model,
+ int n_threads);
// Returns the number of tokens in the KV cache
LLAMA_API int llama_get_kv_cache_token_count(const struct llama_context * ctx);
@@ -324,11 +325,40 @@ extern "C" {
// IMPORTANT: do not use for anything else other than debugging and testing!
LLAMA_API int llama_eval_export(struct llama_context * ctx, const char * fname);
+ // Token logits obtained from the last call to llama_eval()
+ // The logits for the last token are stored in the last row
+ // Can be mutated in order to change the probabilities of the next token
+ // Rows: n_tokens
+ // Cols: n_vocab
+ LLAMA_API float * llama_get_logits(struct llama_context * ctx);
+
+ // Get the embeddings for the input
+ // shape: [n_embd] (1-dimensional)
+ LLAMA_API float * llama_get_embeddings(struct llama_context * ctx);
+
+ //
+ // Vocab
+ //
+
+ LLAMA_API const char * llama_token_get_text(const struct llama_context * ctx, llama_token token);
+
+ LLAMA_API float llama_token_get_score(const struct llama_context * ctx, llama_token token);
+
+ LLAMA_API llama_token_type llama_token_get_type(const struct llama_context * ctx, llama_token token);
+
+ // Special tokens
+ LLAMA_API llama_token llama_token_bos(const struct llama_context * ctx); // beginning-of-sentence
+ LLAMA_API llama_token llama_token_eos(const struct llama_context * ctx); // end-of-sentence
+ LLAMA_API llama_token llama_token_nl (const struct llama_context * ctx); // next-line
+
+ //
+ // Tokenization
+ //
+
// Convert the provided text into tokens.
// The tokens pointer must be large enough to hold the resulting tokens.
// Returns the number of tokens on success, no more than n_max_tokens
// Returns a negative number on failure - the number of tokens that would have been returned
- // TODO: not sure if correct
LLAMA_API int llama_tokenize(
struct llama_context * ctx,
const char * text,
@@ -336,6 +366,13 @@ extern "C" {
int n_max_tokens,
bool add_bos);
+ LLAMA_API int llama_tokenize_bpe(
+ struct llama_context * ctx,
+ const char * text,
+ llama_token * tokens,
+ int n_max_tokens,
+ bool add_bos);
+
LLAMA_API int llama_tokenize_with_model(
const struct llama_model * model,
const char * text,
@@ -343,57 +380,30 @@ extern "C" {
int n_max_tokens,
bool add_bos);
- LLAMA_API int llama_n_vocab(const struct llama_context * ctx);
- LLAMA_API int llama_n_ctx (const struct llama_context * ctx);
- LLAMA_API int llama_n_embd (const struct llama_context * ctx);
-
- LLAMA_API int llama_n_vocab_from_model(const struct llama_model * model);
- LLAMA_API int llama_n_ctx_from_model (const struct llama_model * model);
- LLAMA_API int llama_n_embd_from_model (const struct llama_model * model);
-
- LLAMA_API int llama_model_type(const struct llama_model * model, char * buf, size_t buf_size);
-
- // Get the vocabulary as output parameters.
- // Returns number of results.
- LLAMA_API int llama_get_vocab(
+ // Token Id -> String. Uses the vocabulary in the provided context
+ // Does not write null terminator to the buffer
+ LLAMA_API int llama_token_to_str(
const struct llama_context * ctx,
- const char * * strings,
- float * scores,
- int capacity);
-
- LLAMA_API int llama_get_vocab_from_model(
- const struct llama_model * model,
- const char * * strings,
- float * scores,
- int capacity);
-
- // Token logits obtained from the last call to llama_eval()
- // The logits for the last token are stored in the last row
- // Can be mutated in order to change the probabilities of the next token
- // Rows: n_tokens
- // Cols: n_vocab
- LLAMA_API float * llama_get_logits(struct llama_context * ctx);
-
- // Get the embeddings for the input
- // shape: [n_embd] (1-dimensional)
- LLAMA_API float * llama_get_embeddings(struct llama_context * ctx);
+ llama_token token,
+ char * buf,
+ int length);
- // Token Id -> String. Uses the vocabulary in the provided context
- LLAMA_API const char * llama_token_to_str(
+ LLAMA_API int llama_token_to_str_bpe(
const struct llama_context * ctx,
- llama_token token);
+ llama_token token,
+ char * buf,
+ int length);
- LLAMA_API const char * llama_token_to_str_with_model(
+ LLAMA_API int llama_token_to_str_with_model(
const struct llama_model * model,
- llama_token token);
-
- // Special tokens
- LLAMA_API llama_token llama_token_bos(); // beginning-of-sentence
- LLAMA_API llama_token llama_token_eos(); // end-of-sentence
- LLAMA_API llama_token llama_token_nl(); // next-line
+ llama_token token,
+ char * buf,
+ int length);
+ //
// Grammar
//
+
LLAMA_API struct llama_grammar * llama_grammar_init(
const llama_grammar_element ** rules,
size_t n_rules,
@@ -401,7 +411,9 @@ extern "C" {
LLAMA_API void llama_grammar_free(struct llama_grammar * grammar);
+ //
// Sampling functions
+ //
/// @details Repetition penalty described in CTRL academic paper https://arxiv.org/abs/1909.05858, with negative logit fix.
LLAMA_API void llama_sample_repetition_penalty(struct llama_context * ctx, llama_token_data_array * candidates, const llama_token * last_tokens, size_t last_tokens_size, float penalty);
@@ -470,6 +482,10 @@ extern "C" {
// Print system information
LLAMA_API const char * llama_print_system_info(void);
+ // Set callback for all future logging events.
+ // If this is not called, or NULL is supplied, everything is output on stderr.
+ LLAMA_API void llama_log_set(llama_log_callback log_callback, void * user_data);
+
#ifdef __cplusplus
}
#endif
@@ -479,10 +495,11 @@ extern "C" {
#include <vector>
#include <string>
+
struct ggml_tensor;
const std::vector<std::pair<std::string, struct ggml_tensor *>>& llama_internal_get_tensor_map(struct llama_context * ctx);
-#endif
+#endif // LLAMA_API_INTERNAL
#endif // LLAMA_H