llama.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #ifndef LLAMA_H
  2. #define LLAMA_H
  3. #include <stddef.h>
  4. #include <stdint.h>
  5. #include <stdbool.h>
  6. #ifdef LLAMA_SHARED
  7. # if defined(_WIN32) && !defined(__MINGW32__)
  8. # ifdef LLAMA_BUILD
  9. # define LLAMA_API __declspec(dllexport)
  10. # else
  11. # define LLAMA_API __declspec(dllimport)
  12. # endif
  13. # else
  14. # define LLAMA_API __attribute__ ((visibility ("default")))
  15. # endif
  16. #else
  17. # define LLAMA_API
  18. #endif
  19. #define LLAMA_FILE_VERSION 1
  20. #define LLAMA_FILE_MAGIC 0x67676a74 // 'ggjt' in hex
  21. #define LLAMA_FILE_MAGIC_UNVERSIONED 0x67676d6c // pre-versioned files
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. //
  26. // C interface
  27. //
  28. // TODO: show sample usage
  29. //
  30. struct llama_context;
  31. typedef int llama_token;
  32. typedef struct llama_token_data {
  33. llama_token id; // token id
  34. float p; // probability of the token
  35. float plog; // log probability of the token
  36. } llama_token_data;
  37. typedef void (*llama_progress_callback)(float progress, void *ctx);
  38. struct llama_context_params {
  39. int n_ctx; // text context
  40. int n_parts; // -1 for default
  41. int seed; // RNG seed, 0 for random
  42. bool f16_kv; // use fp16 for KV cache
  43. bool logits_all; // the llama_eval() call computes all logits, not just the last one
  44. bool vocab_only; // only load the vocabulary, no weights
  45. bool use_mmap; // use mmap if possible
  46. bool use_mlock; // force system to keep model in RAM
  47. bool embedding; // embedding mode only
  48. // called with a progress value between 0 and 1, pass NULL to disable
  49. llama_progress_callback progress_callback;
  50. // context pointer passed to the progress callback
  51. void * progress_callback_user_data;
  52. };
  53. // model file types
  54. enum llama_ftype {
  55. LLAMA_FTYPE_ALL_F32 = 0,
  56. LLAMA_FTYPE_MOSTLY_F16 = 1, // except 1d tensors
  57. LLAMA_FTYPE_MOSTLY_Q4_0 = 2, // except 1d tensors
  58. LLAMA_FTYPE_MOSTLY_Q4_1 = 3, // except 1d tensors
  59. LLAMA_FTYPE_MOSTLY_Q4_1_SOME_F16 = 4, // tok_embeddings.weight and output.weight are F16
  60. LLAMA_FTYPE_MOSTLY_Q4_2 = 5, // except 1d tensors
  61. // LLAMA_FTYPE_MOSTLY_Q4_3 (6) support has been removed
  62. LLAMA_FTYPE_MOSTLY_Q8_0 = 7, // except 1d tensors
  63. LLAMA_FTYPE_MOSTLY_Q5_0 = 8, // except 1d tensors
  64. LLAMA_FTYPE_MOSTLY_Q5_1 = 9, // except 1d tensors
  65. };
  66. LLAMA_API struct llama_context_params llama_context_default_params();
  67. LLAMA_API bool llama_mmap_supported();
  68. LLAMA_API bool llama_mlock_supported();
  69. // Various functions for loading a ggml llama model.
  70. // Allocate (almost) all memory needed for the model.
  71. // Return NULL on failure
  72. LLAMA_API struct llama_context * llama_init_from_file(
  73. const char * path_model,
  74. struct llama_context_params params);
  75. // Frees all allocated memory
  76. LLAMA_API void llama_free(struct llama_context * ctx);
  77. // TODO: not great API - very likely to change
  78. // Returns 0 on success
  79. // nthread - how many threads to use. If <=0, will use std::thread::hardware_concurrency(), else the number given
  80. LLAMA_API int llama_model_quantize(
  81. const char * fname_inp,
  82. const char * fname_out,
  83. enum llama_ftype ftype,
  84. int nthread);
  85. // Apply a LoRA adapter to a loaded model
  86. // path_base_model is the path to a higher quality model to use as a base for
  87. // the layers modified by the adapter. Can be NULL to use the current loaded model.
  88. // The model needs to be reloaded before applying a new adapter, otherwise the adapter
  89. // will be applied on top of the previous one
  90. // Returns 0 on success
  91. LLAMA_API int llama_apply_lora_from_file(
  92. struct llama_context * ctx,
  93. const char * path_lora,
  94. const char * path_base_model,
  95. int n_threads);
  96. // Returns the number of tokens in the KV cache
  97. LLAMA_API int llama_get_kv_cache_token_count(struct llama_context * ctx);
  98. // Sets the current rng seed.
  99. LLAMA_API void llama_set_rng_seed(struct llama_context * ctx, int seed);
  100. // Returns the size in bytes of the state (rng, logits, embedding and kv_cache)
  101. LLAMA_API size_t llama_get_state_size(struct llama_context * ctx);
  102. // Copies the state to the specified destination address.
  103. // Destination needs to have allocated enough memory.
  104. // Returns the number of bytes copied
  105. LLAMA_API size_t llama_copy_state_data(struct llama_context * ctx, uint8_t * dest);
  106. // Set the state reading from the specified address
  107. // Returns the number of bytes read
  108. LLAMA_API size_t llama_set_state_data(struct llama_context * ctx, const uint8_t * src);
  109. // Save/load session file
  110. LLAMA_API size_t llama_load_session_file(struct llama_context * ctx, const char * path_session, llama_token * tokens_out, size_t n_token_capacity, size_t * n_token_count_out);
  111. LLAMA_API size_t llama_save_session_file(struct llama_context * ctx, const char * path_session, const llama_token * tokens, size_t n_token_count);
  112. // Run the llama inference to obtain the logits and probabilities for the next token.
  113. // tokens + n_tokens is the provided batch of new tokens to process
  114. // n_past is the number of tokens to use from previous eval calls
  115. // Returns 0 on success
  116. LLAMA_API int llama_eval(
  117. struct llama_context * ctx,
  118. const llama_token * tokens,
  119. int n_tokens,
  120. int n_past,
  121. int n_threads);
  122. // Convert the provided text into tokens.
  123. // The tokens pointer must be large enough to hold the resulting tokens.
  124. // Returns the number of tokens on success, no more than n_max_tokens
  125. // Returns a negative number on failure - the number of tokens that would have been returned
  126. // TODO: not sure if correct
  127. LLAMA_API int llama_tokenize(
  128. struct llama_context * ctx,
  129. const char * text,
  130. llama_token * tokens,
  131. int n_max_tokens,
  132. bool add_bos);
  133. LLAMA_API int llama_n_vocab(struct llama_context * ctx);
  134. LLAMA_API int llama_n_ctx (struct llama_context * ctx);
  135. LLAMA_API int llama_n_embd (struct llama_context * ctx);
  136. // Token logits obtained from the last call to llama_eval()
  137. // The logits for the last token are stored in the last row
  138. // Can be mutated in order to change the probabilities of the next token
  139. // Rows: n_tokens
  140. // Cols: n_vocab
  141. LLAMA_API float * llama_get_logits(struct llama_context * ctx);
  142. // Get the embeddings for the input
  143. // shape: [n_embd] (1-dimensional)
  144. LLAMA_API float * llama_get_embeddings(struct llama_context * ctx);
  145. // Token Id -> String. Uses the vocabulary in the provided context
  146. LLAMA_API const char * llama_token_to_str(struct llama_context * ctx, llama_token token);
  147. // Special tokens
  148. LLAMA_API llama_token llama_token_bos();
  149. LLAMA_API llama_token llama_token_eos();
  150. // TODO: improve the last_n_tokens interface ?
  151. LLAMA_API llama_token llama_sample_top_p_top_k(
  152. struct llama_context * ctx,
  153. const llama_token * last_n_tokens_data,
  154. int last_n_tokens_size,
  155. int top_k,
  156. float top_p,
  157. float temp,
  158. float repeat_penalty);
  159. // Performance information
  160. LLAMA_API void llama_print_timings(struct llama_context * ctx);
  161. LLAMA_API void llama_reset_timings(struct llama_context * ctx);
  162. // Print system information
  163. LLAMA_API const char * llama_print_system_info(void);
  164. #ifdef __cplusplus
  165. }
  166. #endif
  167. // Internal API to be implemented by llama.cpp and used by tests/benchmarks only
  168. #ifdef LLAMA_API_INTERNAL
  169. #include <vector>
  170. #include <string>
  171. struct ggml_tensor;
  172. std::vector<std::pair<std::string, struct ggml_tensor *>>& llama_internal_get_tensor_map(struct llama_context * ctx);
  173. #endif
  174. #endif // LLAMA_H