llama-context.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #pragma once
  2. #include "llama.h"
  3. #include "llama-cparams.h"
  4. #include "llama-graph.h"
  5. #include "llama-adapter.h"
  6. #include "ggml-cpp.h"
  7. #include "ggml-opt.h"
  8. #include <map>
  9. #include <vector>
  10. struct llama_model;
  11. class llama_batch_allocr;
  12. class llama_io_read_i;
  13. class llama_io_write_i;
  14. struct llama_memory_i;
  15. struct llama_memory_state_i;
  16. struct llama_context {
  17. // init scheduler and compute buffers, reserve worst-case graphs
  18. llama_context(
  19. const llama_model & model,
  20. llama_context_params params);
  21. ~llama_context();
  22. void synchronize();
  23. const llama_model & get_model() const;
  24. const llama_cparams & get_cparams() const;
  25. ggml_backend_sched_t get_sched() const;
  26. ggml_context * get_ctx_compute() const;
  27. uint32_t n_ctx() const;
  28. uint32_t n_ctx_per_seq() const;
  29. uint32_t n_batch() const;
  30. uint32_t n_ubatch() const;
  31. uint32_t n_seq_max() const;
  32. uint32_t n_threads() const;
  33. uint32_t n_threads_batch() const;
  34. llama_memory_t get_memory() const;
  35. // return true of the KV cache was updated
  36. // TODO: remove
  37. bool kv_self_update(bool optimize);
  38. void kv_self_defrag_sched();
  39. enum llama_pooling_type pooling_type() const;
  40. float * get_logits();
  41. float * get_logits_ith(int32_t i);
  42. float * get_embeddings();
  43. float * get_embeddings_ith(int32_t i);
  44. float * get_embeddings_seq(llama_seq_id seq_id);
  45. void attach_threadpool(
  46. ggml_threadpool_t threadpool,
  47. ggml_threadpool_t threadpool_batch);
  48. void detach_threadpool();
  49. void set_n_threads(int32_t n_threads, int32_t n_threads_batch);
  50. void set_abort_callback(bool (*abort_callback)(void * data), void * abort_callback_data);
  51. void set_embeddings (bool value);
  52. void set_causal_attn(bool value);
  53. void set_warmup(bool value);
  54. void set_adapter_lora(
  55. llama_adapter_lora * adapter,
  56. float scale);
  57. bool rm_adapter_lora(
  58. llama_adapter_lora * adapter);
  59. void clear_adapter_lora();
  60. bool apply_adapter_cvec(
  61. const float * data,
  62. size_t len,
  63. int32_t n_embd,
  64. int32_t il_start,
  65. int32_t il_end);
  66. // process a single ubatch with a specific graph type
  67. // if memory_state is provided, it will be applied first to the context's memory
  68. // ret contains the status of the graph computation
  69. // returns nullptr only if ret != GGML_STATUS_SUCCESS
  70. llm_graph_result_ptr process_ubatch(
  71. const llama_ubatch & ubatch,
  72. llm_graph_type gtype,
  73. llama_memory_state_i * mstate,
  74. ggml_status & ret);
  75. int encode(const llama_batch & batch_inp);
  76. int decode(const llama_batch & batch_inp);
  77. //
  78. // state save/load
  79. //
  80. size_t state_get_size();
  81. size_t state_get_data( uint8_t * dst, size_t size);
  82. size_t state_set_data(const uint8_t * src, size_t size);
  83. size_t state_seq_get_size(llama_seq_id seq_id);
  84. size_t state_seq_get_data(llama_seq_id seq_id, uint8_t * dst, size_t size);
  85. size_t state_seq_set_data(llama_seq_id seq_id, const uint8_t * src, size_t size);
  86. bool state_load_file(
  87. const char * filepath,
  88. llama_token * tokens_out,
  89. size_t n_token_capacity,
  90. size_t * n_token_count_out);
  91. bool state_save_file(
  92. const char * filepath,
  93. const llama_token * tokens,
  94. size_t n_token_count);
  95. size_t state_seq_load_file(
  96. llama_seq_id seq_id,
  97. const char * filepath,
  98. llama_token * tokens_out,
  99. size_t n_token_capacity,
  100. size_t * n_token_count_out);
  101. size_t state_seq_save_file(
  102. llama_seq_id seq_id,
  103. const char * filepath,
  104. const llama_token * tokens,
  105. size_t n_token_count);
  106. //
  107. // perf
  108. //
  109. llama_perf_context_data perf_get_data() const;
  110. void perf_reset();
  111. //
  112. // training
  113. //
  114. void opt_init(struct llama_model * model, struct llama_opt_params lopt_params);
  115. void opt_epoch(
  116. ggml_opt_dataset_t dataset,
  117. ggml_opt_result_t result_train,
  118. ggml_opt_result_t result_eval,
  119. int64_t idata_split,
  120. ggml_opt_epoch_callback callback_train,
  121. ggml_opt_epoch_callback callback_eval);
  122. void opt_epoch_iter(
  123. ggml_opt_dataset_t dataset,
  124. ggml_opt_result_t result,
  125. const std::vector<llama_token> & tokens,
  126. const std::vector<llama_token> & labels_sparse,
  127. llama_batch & batch,
  128. ggml_opt_epoch_callback callback,
  129. bool train,
  130. int64_t idata_in_loop,
  131. int64_t ndata_in_loop,
  132. int64_t t_loop_start);
  133. private:
  134. //
  135. // output
  136. //
  137. // Make sure enough space is available for outputs.
  138. // Returns max number of outputs for which space was reserved.
  139. uint32_t output_reserve(int32_t n_outputs);
  140. //
  141. // graph
  142. //
  143. public:
  144. int32_t graph_max_nodes() const;
  145. // zero-out inputs and create the ctx_compute for the compute graph
  146. ggml_cgraph * graph_init();
  147. // returns the result of ggml_backend_sched_graph_compute_async execution
  148. ggml_status graph_compute(ggml_cgraph * gf, bool batched);
  149. // reserve a graph with a dummy ubatch of the specified size
  150. ggml_cgraph * graph_reserve(uint32_t n_tokens, uint32_t n_seqs, uint32_t n_outputs, const llama_memory_state_i * mstate);
  151. private:
  152. llm_graph_result_ptr graph_build(
  153. ggml_context * ctx,
  154. ggml_cgraph * gf,
  155. const llama_ubatch & ubatch,
  156. llm_graph_type gtype,
  157. const llama_memory_state_i * mstate);
  158. llm_graph_cb graph_get_cb() const;
  159. // TODO: read/write lora adapters and cvec
  160. size_t state_write_data(llama_io_write_i & io);
  161. size_t state_read_data (llama_io_read_i & io);
  162. size_t state_seq_write_data(llama_io_write_i & io, llama_seq_id seq_id);
  163. size_t state_seq_read_data (llama_io_read_i & io, llama_seq_id seq_id);
  164. //
  165. // members
  166. //
  167. const llama_model & model;
  168. llama_cparams cparams;
  169. llama_adapter_cvec cvec;
  170. llama_adapter_loras loras;
  171. llama_cross cross; // TODO: tmp for handling cross-attention - need something better probably
  172. std::unique_ptr<llama_memory_i> memory;
  173. // TODO: temporary, until the llama_kv_self_defrag() API is removed
  174. bool memory_force_optimize = false;
  175. // decode output (2-dimensional array: [n_outputs][n_vocab])
  176. size_t logits_size = 0; // capacity (of floats) for logits
  177. float * logits = nullptr;
  178. // embeddings output (2-dimensional array: [n_outputs][n_embd])
  179. // populated only when pooling_type == LLAMA_POOLING_TYPE_NONE
  180. size_t embd_size = 0; // capacity (of floats) for embeddings
  181. float * embd = nullptr;
  182. // sequence embeddings output (map of [n_embd] vectors)
  183. // populated only when pooling_type != LLAMA_POOLING_TYPE_NONE
  184. std::map<llama_seq_id, std::vector<float>> embd_seq;
  185. // reuse the batch_allocr to avoid unnecessary memory allocations
  186. std::unique_ptr<llama_batch_allocr> batch_allocr;
  187. uint32_t n_outputs = 0; // number of actually-used outputs in the current ubatch or last logical batch
  188. std::vector<int32_t> output_ids; // map batch token positions to ids of the logits and embd buffers
  189. ggml_backend_sched_ptr sched;
  190. ggml_backend_t backend_cpu = nullptr;
  191. std::vector<ggml_backend_ptr> backends;
  192. ggml_context_ptr ctx_compute;
  193. // training
  194. ggml_opt_context_t opt_ctx = nullptr;
  195. ggml_threadpool_t threadpool = nullptr;
  196. ggml_threadpool_t threadpool_batch = nullptr;
  197. ggml_abort_callback abort_callback = nullptr;
  198. void * abort_callback_data = nullptr;
  199. std::vector<std::pair<ggml_backend_t, ggml_backend_set_n_threads_t>> set_n_threads_fns;
  200. // buffer types used for the compute buffer of each backend
  201. std::vector<ggml_backend_t> backend_ptrs;
  202. std::vector<ggml_backend_buffer_type_t> backend_buft;
  203. // memory buffers used to evaluate the model
  204. std::vector<uint8_t> buf_compute_meta;
  205. // host buffer for the model output (logits and embeddings)
  206. ggml_backend_buffer_ptr buf_output;
  207. bool has_evaluated_once = false;
  208. // perf
  209. mutable int64_t t_start_us = 0;
  210. mutable int64_t t_load_us = 0;
  211. mutable int64_t t_p_eval_us = 0;
  212. mutable int64_t t_eval_us = 0;
  213. mutable int64_t t_compute_start_us = 0;
  214. mutable int64_t n_queued_tokens = 0;
  215. mutable int32_t n_p_eval = 0; // number of tokens in eval calls for the prompt (with batch size > 1)
  216. mutable int32_t n_eval = 0; // number of eval calls
  217. };