llama.h 7.2 KB

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