llama.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. // Run the llama inference to obtain the logits and probabilities for the next token.
  68. // tokens + n_tokens is the provided batch of new tokens to process
  69. // n_past is the number of tokens to use from previous eval calls
  70. // Returns 0 on success
  71. LLAMA_API int llama_eval(
  72. struct llama_context * ctx,
  73. const llama_token * tokens,
  74. int n_tokens,
  75. int n_past,
  76. int n_threads);
  77. // Convert the provided text into tokens.
  78. // The tokens pointer must be large enough to hold the resulting tokens.
  79. // Returns the number of tokens on success, no more than n_max_tokens
  80. // Returns a negative number on failure - the number of tokens that would have been returned
  81. // TODO: not sure if correct
  82. LLAMA_API int llama_tokenize(
  83. struct llama_context * ctx,
  84. const char * text,
  85. llama_token * tokens,
  86. int n_max_tokens,
  87. bool add_bos);
  88. LLAMA_API int llama_n_vocab(struct llama_context * ctx);
  89. LLAMA_API int llama_n_ctx (struct llama_context * ctx);
  90. LLAMA_API int llama_n_embd (struct llama_context * ctx);
  91. // Token logits obtained from the last call to llama_eval()
  92. // The logits for the last token are stored in the last row
  93. // Can be mutated in order to change the probabilities of the next token
  94. // Rows: n_tokens
  95. // Cols: n_vocab
  96. LLAMA_API float * llama_get_logits(struct llama_context * ctx);
  97. // Get the embeddings for the input
  98. // shape: [n_embd] (1-dimensional)
  99. LLAMA_API float * llama_get_embeddings(struct llama_context * ctx);
  100. // Token Id -> String. Uses the vocabulary in the provided context
  101. LLAMA_API const char * llama_token_to_str(struct llama_context * ctx, llama_token token);
  102. // Special tokens
  103. LLAMA_API llama_token llama_token_bos();
  104. LLAMA_API llama_token llama_token_eos();
  105. // TODO: improve the last_n_tokens interface ?
  106. LLAMA_API llama_token llama_sample_top_p_top_k(
  107. struct llama_context * ctx,
  108. const llama_token * last_n_tokens_data,
  109. int last_n_tokens_size,
  110. int top_k,
  111. float top_p,
  112. float temp,
  113. float repeat_penalty);
  114. // Performance information
  115. LLAMA_API void llama_print_timings(struct llama_context * ctx);
  116. LLAMA_API void llama_reset_timings(struct llama_context * ctx);
  117. // Print system information
  118. LLAMA_API const char * llama_print_system_info(void);
  119. #ifdef __cplusplus
  120. }
  121. #endif
  122. #endif