llama.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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_mlock; // force system to keep model in RAM
  46. bool embedding; // embedding mode only
  47. // called with a progress value between 0 and 1, pass NULL to disable
  48. llama_progress_callback progress_callback;
  49. // context pointer passed to the progress callback
  50. void * progress_callback_user_data;
  51. };
  52. LLAMA_API struct llama_context_params llama_context_default_params();
  53. // Various functions for loading a ggml llama model.
  54. // Allocate (almost) all memory needed for the model.
  55. // Return NULL on failure
  56. LLAMA_API struct llama_context * llama_init_from_file(
  57. const char * path_model,
  58. struct llama_context_params params);
  59. // Frees all allocated memory
  60. LLAMA_API void llama_free(struct llama_context * ctx);
  61. // TODO: not great API - very likely to change
  62. // Returns 0 on success
  63. LLAMA_API int llama_model_quantize(
  64. const char * fname_inp,
  65. const char * fname_out,
  66. int itype);
  67. // Returns the KV cache that will contain the context for the
  68. // ongoing prediction with the model.
  69. LLAMA_API const uint8_t * llama_get_kv_cache(struct llama_context * ctx);
  70. // Returns the size of the KV cache
  71. LLAMA_API size_t llama_get_kv_cache_size(struct llama_context * ctx);
  72. // Returns the number of tokens in the KV cache
  73. LLAMA_API int llama_get_kv_cache_token_count(struct llama_context * ctx);
  74. // Sets the KV cache containing the current context for the model
  75. LLAMA_API void llama_set_kv_cache(
  76. struct llama_context * ctx,
  77. const uint8_t * kv_cache,
  78. size_t n_size,
  79. int n_token_count);
  80. // Run the llama inference to obtain the logits and probabilities for the next token.
  81. // tokens + n_tokens is the provided batch of new tokens to process
  82. // n_past is the number of tokens to use from previous eval calls
  83. // Returns 0 on success
  84. LLAMA_API int llama_eval(
  85. struct llama_context * ctx,
  86. const llama_token * tokens,
  87. int n_tokens,
  88. int n_past,
  89. int n_threads);
  90. // Convert the provided text into tokens.
  91. // The tokens pointer must be large enough to hold the resulting tokens.
  92. // Returns the number of tokens on success, no more than n_max_tokens
  93. // Returns a negative number on failure - the number of tokens that would have been returned
  94. // TODO: not sure if correct
  95. LLAMA_API int llama_tokenize(
  96. struct llama_context * ctx,
  97. const char * text,
  98. llama_token * tokens,
  99. int n_max_tokens,
  100. bool add_bos);
  101. LLAMA_API int llama_n_vocab(struct llama_context * ctx);
  102. LLAMA_API int llama_n_ctx (struct llama_context * ctx);
  103. LLAMA_API int llama_n_embd (struct llama_context * ctx);
  104. // Token logits obtained from the last call to llama_eval()
  105. // The logits for the last token are stored in the last row
  106. // Can be mutated in order to change the probabilities of the next token
  107. // Rows: n_tokens
  108. // Cols: n_vocab
  109. LLAMA_API float * llama_get_logits(struct llama_context * ctx);
  110. // Get the embeddings for the input
  111. // shape: [n_embd] (1-dimensional)
  112. LLAMA_API float * llama_get_embeddings(struct llama_context * ctx);
  113. // Token Id -> String. Uses the vocabulary in the provided context
  114. LLAMA_API const char * llama_token_to_str(struct llama_context * ctx, llama_token token);
  115. // Special tokens
  116. LLAMA_API llama_token llama_token_bos();
  117. LLAMA_API llama_token llama_token_eos();
  118. // TODO: improve the last_n_tokens interface ?
  119. LLAMA_API llama_token llama_sample_top_p_top_k(
  120. struct llama_context * ctx,
  121. const llama_token * last_n_tokens_data,
  122. int last_n_tokens_size,
  123. int top_k,
  124. float top_p,
  125. float temp,
  126. float repeat_penalty);
  127. // Performance information
  128. LLAMA_API void llama_print_timings(struct llama_context * ctx);
  129. LLAMA_API void llama_reset_timings(struct llama_context * ctx);
  130. // Print system information
  131. LLAMA_API const char * llama_print_system_info(void);
  132. #ifdef __cplusplus
  133. }
  134. #endif
  135. #endif