llama-context.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #pragma once
  2. #include "llama.h"
  3. #include "llama-batch.h"
  4. #include "llama-cparams.h"
  5. #include "llama-graph.h"
  6. #include "llama-adapter.h"
  7. #include "ggml-cpp.h"
  8. #include <map>
  9. #include <vector>
  10. struct llama_model;
  11. struct llama_kv_cache;
  12. class llama_io_read_i;
  13. class llama_io_write_i;
  14. struct llama_context {
  15. // init scheduler and compute buffers, reserve worst-case graphs
  16. llama_context(
  17. const llama_model & model,
  18. llama_context_params params);
  19. ~llama_context();
  20. void synchronize();
  21. const llama_model & get_model() const;
  22. uint32_t n_ctx() const;
  23. uint32_t n_ctx_per_seq() const;
  24. uint32_t n_batch() const;
  25. uint32_t n_ubatch() const;
  26. uint32_t n_seq_max() const;
  27. uint32_t n_threads() const;
  28. uint32_t n_threads_batch() const;
  29. llama_kv_cache * get_kv_self();
  30. const llama_kv_cache * get_kv_self() const;
  31. void kv_self_update();
  32. enum llama_pooling_type pooling_type() const;
  33. float * get_logits();
  34. float * get_logits_ith(int32_t i);
  35. float * get_embeddings();
  36. float * get_embeddings_ith(int32_t i);
  37. float * get_embeddings_seq(llama_seq_id seq_id);
  38. void attach_threadpool(
  39. ggml_threadpool_t threadpool,
  40. ggml_threadpool_t threadpool_batch);
  41. void detach_threadpool();
  42. void set_n_threads(int32_t n_threads, int32_t n_threads_batch);
  43. void set_abort_callback(bool (*abort_callback)(void * data), void * abort_callback_data);
  44. void set_embeddings (bool value);
  45. void set_causal_attn(bool value);
  46. void set_warmup(bool value);
  47. void set_adapter_lora(
  48. llama_adapter_lora * adapter,
  49. float scale);
  50. bool rm_adapter_lora(
  51. llama_adapter_lora * adapter);
  52. void clear_adapter_lora();
  53. bool apply_adapter_cvec(
  54. const float * data,
  55. size_t len,
  56. int32_t n_embd,
  57. int32_t il_start,
  58. int32_t il_end);
  59. int encode(llama_batch & inp_batch);
  60. int decode(llama_batch & inp_batch);
  61. //
  62. // state save/load
  63. //
  64. size_t state_get_size();
  65. size_t state_get_data( uint8_t * dst, size_t size);
  66. size_t state_set_data(const uint8_t * src, size_t size);
  67. size_t state_seq_get_size(llama_seq_id seq_id);
  68. size_t state_seq_get_data(llama_seq_id seq_id, uint8_t * dst, size_t size);
  69. size_t state_seq_set_data(llama_seq_id seq_id, const uint8_t * src, size_t size);
  70. bool state_load_file(
  71. const char * filepath,
  72. llama_token * tokens_out,
  73. size_t n_token_capacity,
  74. size_t * n_token_count_out);
  75. bool state_save_file(
  76. const char * filepath,
  77. const llama_token * tokens,
  78. size_t n_token_count);
  79. size_t state_seq_load_file(
  80. llama_seq_id seq_id,
  81. const char * filepath,
  82. llama_token * tokens_out,
  83. size_t n_token_capacity,
  84. size_t * n_token_count_out);
  85. size_t state_seq_save_file(
  86. llama_seq_id seq_id,
  87. const char * filepath,
  88. const llama_token * tokens,
  89. size_t n_token_count);
  90. //
  91. // perf
  92. //
  93. llama_perf_context_data perf_get_data() const;
  94. void perf_reset();
  95. private:
  96. //
  97. // output
  98. //
  99. // Make sure enough space is available for outputs.
  100. // Returns max number of outputs for which space was reserved.
  101. int32_t output_reserve(int32_t n_outputs);
  102. // make the outputs have the same order they had in the user-provided batch
  103. // TODO: maybe remove this
  104. void output_reorder();
  105. //
  106. // graph
  107. //
  108. int32_t graph_max_nodes() const;
  109. // zero-out inputs and create the ctx_compute for the compute graph
  110. ggml_cgraph * graph_init();
  111. llm_graph_result_ptr graph_build(
  112. ggml_context * ctx,
  113. ggml_cgraph * gf,
  114. const llama_ubatch & ubatch,
  115. llm_graph_type gtype);
  116. // returns the result of ggml_backend_sched_graph_compute_async execution
  117. ggml_status graph_compute(
  118. ggml_cgraph * gf,
  119. bool batched);
  120. llm_graph_cb graph_get_cb() const;
  121. // used by kv_self_update()
  122. ggml_tensor * build_rope_shift(
  123. ggml_context * ctx0,
  124. ggml_tensor * cur,
  125. ggml_tensor * shift,
  126. ggml_tensor * factors,
  127. float freq_base,
  128. float freq_scale) const;
  129. llm_graph_result_ptr build_kv_self_shift(
  130. ggml_context * ctx0,
  131. ggml_cgraph * gf) const;
  132. llm_graph_result_ptr build_kv_self_defrag(
  133. ggml_context * ctx0,
  134. ggml_cgraph * gf) const;
  135. // TODO: read/write lora adapters and cvec
  136. size_t state_write_data(llama_io_write_i & io);
  137. size_t state_read_data (llama_io_read_i & io);
  138. size_t state_seq_write_data(llama_io_write_i & io, llama_seq_id seq_id);
  139. size_t state_seq_read_data (llama_io_read_i & io, llama_seq_id seq_id);
  140. //
  141. // members
  142. //
  143. const llama_model & model;
  144. llama_cparams cparams;
  145. llama_adapter_cvec cvec;
  146. llama_adapter_loras loras;
  147. llama_sbatch sbatch;
  148. llama_cross cross; // TODO: tmp for handling cross-attention - need something better probably
  149. std::unique_ptr<llama_kv_cache_unified> kv_self;
  150. // TODO: remove
  151. bool logits_all = false;
  152. // decode output (2-dimensional array: [n_outputs][n_vocab])
  153. size_t logits_size = 0; // capacity (of floats) for logits
  154. float * logits = nullptr;
  155. // embeddings output (2-dimensional array: [n_outputs][n_embd])
  156. // populated only when pooling_type == LLAMA_POOLING_TYPE_NONE
  157. size_t embd_size = 0; // capacity (of floats) for embeddings
  158. float * embd = nullptr;
  159. // sequence embeddings output (map of [n_embd] vectors)
  160. // populated only when pooling_type != LLAMA_POOLING_TYPE_NONE
  161. std::map<llama_seq_id, std::vector<float>> embd_seq;
  162. int32_t n_outputs = 0; // number of actually-used outputs in the current ubatch or last logical batch
  163. int32_t n_outputs_max = 0; // capacity (of tokens positions) for the output buffers
  164. std::vector<int32_t> output_ids; // map batch token positions to ids of the logits and embd buffers
  165. ggml_backend_sched_ptr sched;
  166. ggml_backend_t backend_cpu = nullptr;
  167. std::vector<ggml_backend_ptr> backends;
  168. ggml_context_ptr ctx_compute;
  169. ggml_threadpool_t threadpool = nullptr;
  170. ggml_threadpool_t threadpool_batch = nullptr;
  171. ggml_abort_callback abort_callback = nullptr;
  172. void * abort_callback_data = nullptr;
  173. std::vector<std::pair<ggml_backend_t, ggml_backend_set_n_threads_t>> set_n_threads_fns;
  174. // buffer types used for the compute buffer of each backend
  175. std::vector<ggml_backend_t> backend_ptrs;
  176. std::vector<ggml_backend_buffer_type_t> backend_buft;
  177. // memory buffers used to evaluate the model
  178. std::vector<uint8_t> buf_compute_meta;
  179. // host buffer for the model output (logits and embeddings)
  180. ggml_backend_buffer_ptr buf_output;
  181. bool has_evaluated_once = false;
  182. // perf
  183. mutable int64_t t_start_us = 0;
  184. mutable int64_t t_load_us = 0;
  185. mutable int64_t t_p_eval_us = 0;
  186. mutable int64_t t_eval_us = 0;
  187. mutable int64_t t_compute_start_us = 0;
  188. mutable int64_t n_queued_tokens = 0;
  189. mutable int32_t n_p_eval = 0; // number of tokens in eval calls for the prompt (with batch size > 1)
  190. mutable int32_t n_eval = 0; // number of eval calls
  191. };