llama.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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, // except 1d tensors
  62. };
  63. LLAMA_API struct llama_context_params llama_context_default_params();
  64. LLAMA_API bool llama_mmap_supported();
  65. LLAMA_API bool llama_mlock_supported();
  66. // Various functions for loading a ggml llama model.
  67. // Allocate (almost) all memory needed for the model.
  68. // Return NULL on failure
  69. LLAMA_API struct llama_context * llama_init_from_file(
  70. const char * path_model,
  71. struct llama_context_params params);
  72. // Frees all allocated memory
  73. LLAMA_API void llama_free(struct llama_context * ctx);
  74. // TODO: not great API - very likely to change
  75. // Returns 0 on success
  76. // nthread - how many threads to use. If <=0, will use std::thread::hardware_concurrency(), else the number given
  77. LLAMA_API int llama_model_quantize(
  78. const char * fname_inp,
  79. const char * fname_out,
  80. enum llama_ftype ftype,
  81. int nthread);
  82. // Apply a LoRA adapter to a loaded model
  83. // path_base_model is the path to a higher quality model to use as a base for
  84. // the layers modified by the adapter. Can be NULL to use the current loaded model.
  85. // The model needs to be reloaded before applying a new adapter, otherwise the adapter
  86. // will be applied on top of the previous one
  87. // Returns 0 on success
  88. LLAMA_API int llama_apply_lora_from_file(
  89. struct llama_context * ctx,
  90. const char * path_lora,
  91. const char * path_base_model,
  92. int n_threads);
  93. // Returns the KV cache that will contain the context for the
  94. // ongoing prediction with the model.
  95. LLAMA_API const uint8_t * llama_get_kv_cache(struct llama_context * ctx);
  96. // Returns the size of the KV cache
  97. LLAMA_API size_t llama_get_kv_cache_size(struct llama_context * ctx);
  98. // Returns the number of tokens in the KV cache
  99. LLAMA_API int llama_get_kv_cache_token_count(struct llama_context * ctx);
  100. // Sets the KV cache containing the current context for the model
  101. LLAMA_API void llama_set_kv_cache(
  102. struct llama_context * ctx,
  103. const uint8_t * kv_cache,
  104. size_t n_size,
  105. int n_token_count);
  106. // Returns the size in bytes of the state (rng, logits, embedding and kv_cache)
  107. LLAMA_API size_t llama_get_state_size(struct llama_context * ctx);
  108. // Copies the state to the specified destination address.
  109. // Destination needs to have allocated enough memory.
  110. // Returns the number of bytes copied
  111. LLAMA_API size_t llama_copy_state_data(struct llama_context * ctx, uint8_t * dest);
  112. // Set the state reading from the specified address
  113. // Returns the number of bytes read
  114. LLAMA_API size_t llama_set_state_data(struct llama_context * ctx, const uint8_t * src);
  115. // Run the llama inference to obtain the logits and probabilities for the next token.
  116. // tokens + n_tokens is the provided batch of new tokens to process
  117. // n_past is the number of tokens to use from previous eval calls
  118. // Returns 0 on success
  119. LLAMA_API int llama_eval(
  120. struct llama_context * ctx,
  121. const llama_token * tokens,
  122. int n_tokens,
  123. int n_past,
  124. int n_threads);
  125. // Convert the provided text into tokens.
  126. // The tokens pointer must be large enough to hold the resulting tokens.
  127. // Returns the number of tokens on success, no more than n_max_tokens
  128. // Returns a negative number on failure - the number of tokens that would have been returned
  129. // TODO: not sure if correct
  130. LLAMA_API int llama_tokenize(
  131. struct llama_context * ctx,
  132. const char * text,
  133. llama_token * tokens,
  134. int n_max_tokens,
  135. bool add_bos);
  136. LLAMA_API int llama_n_vocab(struct llama_context * ctx);
  137. LLAMA_API int llama_n_ctx (struct llama_context * ctx);
  138. LLAMA_API int llama_n_embd (struct llama_context * ctx);
  139. // Token logits obtained from the last call to llama_eval()
  140. // The logits for the last token are stored in the last row
  141. // Can be mutated in order to change the probabilities of the next token
  142. // Rows: n_tokens
  143. // Cols: n_vocab
  144. LLAMA_API float * llama_get_logits(struct llama_context * ctx);
  145. // Get the embeddings for the input
  146. // shape: [n_embd] (1-dimensional)
  147. LLAMA_API float * llama_get_embeddings(struct llama_context * ctx);
  148. // Token Id -> String. Uses the vocabulary in the provided context
  149. LLAMA_API const char * llama_token_to_str(struct llama_context * ctx, llama_token token);
  150. // Special tokens
  151. LLAMA_API llama_token llama_token_bos();
  152. LLAMA_API llama_token llama_token_eos();
  153. // TODO: improve the last_n_tokens interface ?
  154. LLAMA_API llama_token llama_sample_top_p_top_k(
  155. struct llama_context * ctx,
  156. const llama_token * last_n_tokens_data,
  157. int last_n_tokens_size,
  158. int top_k,
  159. float top_p,
  160. float temp,
  161. float repeat_penalty);
  162. // Performance information
  163. LLAMA_API void llama_print_timings(struct llama_context * ctx);
  164. LLAMA_API void llama_reset_timings(struct llama_context * ctx);
  165. // Print system information
  166. LLAMA_API const char * llama_print_system_info(void);
  167. #ifdef __cplusplus
  168. }
  169. #endif
  170. // Internal API to be implemented by llama.cpp and used by tests/benchmarks only
  171. #ifdef LLAMA_API_INTERNAL
  172. #include <vector>
  173. #include <string>
  174. struct ggml_tensor;
  175. std::vector<std::pair<std::string, struct ggml_tensor *>>& llama_internal_get_tensor_map(struct llama_context * ctx);
  176. #endif
  177. #endif // LLAMA_H