llama-memory-recurrent.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #pragma once
  2. #include "llama-batch.h"
  3. #include "llama-graph.h"
  4. #include "llama-memory.h"
  5. #include <set>
  6. #include <vector>
  7. //
  8. // llama_memory_recurrent
  9. //
  10. // TODO: extract the cache state used for graph computation into llama_memory_recurrent_context_i
  11. // see the implementation of llama_kv_cache_unified_context_i for an example how to do it
  12. class llama_memory_recurrent : public llama_memory_i {
  13. public:
  14. // this callback is used to filter out layers that should not be included in the cache
  15. using layer_filter_cb = std::function<bool(int32_t il)>;
  16. llama_memory_recurrent(
  17. const llama_model & model,
  18. layer_filter_cb && filter,
  19. ggml_type type_r,
  20. ggml_type type_s,
  21. bool offload,
  22. uint32_t mem_size,
  23. uint32_t n_seq_max);
  24. ~llama_memory_recurrent() = default;
  25. //
  26. // llama_memory_i
  27. //
  28. llama_memory_context_ptr init_batch(
  29. llama_batch_allocr & balloc,
  30. uint32_t n_ubatch,
  31. bool embd_all) override;
  32. llama_memory_context_ptr init_full() override;
  33. llama_memory_context_ptr init_update(llama_context * lctx, bool optimize) override;
  34. void clear(bool data) override;
  35. bool seq_rm (llama_seq_id seq_id, llama_pos p0, llama_pos p1) override;
  36. void seq_cp (llama_seq_id seq_id_src, llama_seq_id seq_id_dst, llama_pos p0, llama_pos p1) override;
  37. void seq_keep(llama_seq_id seq_id) override;
  38. void seq_add (llama_seq_id seq_id, llama_pos p0, llama_pos p1, llama_pos shift) override;
  39. void seq_div (llama_seq_id seq_id, llama_pos p0, llama_pos p1, int d) override;
  40. llama_pos seq_pos_min(llama_seq_id seq_id) const override;
  41. llama_pos seq_pos_max(llama_seq_id seq_id) const override;
  42. bool prepare(const std::vector<llama_ubatch> & ubatches);
  43. // find a contiguous slot of memory cells and emplace the ubatch there
  44. bool find_slot(const llama_ubatch & ubatch);
  45. bool get_can_shift() const override;
  46. // state write/load
  47. void state_write(llama_io_write_i & io, llama_seq_id seq_id = -1) const override;
  48. void state_read (llama_io_read_i & io, llama_seq_id seq_id = -1) override;
  49. uint32_t head = 0; // the location where the batch will be placed in the cache (see find_slot())
  50. uint32_t size = 0; // total number of cells, shared across all sequences
  51. uint32_t used = 0; // used cells (i.e. at least one seq_id)
  52. // computed before each graph build
  53. uint32_t n = 0;
  54. // first zero-ed state
  55. int32_t rs_z = -1;
  56. // TODO: optimize for recurrent state needs
  57. struct mem_cell {
  58. llama_pos pos = -1;
  59. int32_t src = -1; // used to know where states should be copied from
  60. int32_t src0 = -1; // like src, but only used when setting the inputs (allowing to copy once)
  61. int32_t tail = -1;
  62. std::set<llama_seq_id> seq_id;
  63. bool has_seq_id(const llama_seq_id & id) const {
  64. return seq_id.find(id) != seq_id.end();
  65. }
  66. bool is_empty() const {
  67. return seq_id.empty();
  68. }
  69. bool is_same_seq(const mem_cell & other) const {
  70. return seq_id == other.seq_id;
  71. }
  72. };
  73. std::vector<mem_cell> cells;
  74. // per layer
  75. std::vector<ggml_tensor *> r_l;
  76. std::vector<ggml_tensor *> s_l;
  77. private:
  78. //const llama_model & model;
  79. const llama_hparams & hparams;
  80. const uint32_t n_seq_max = 1;
  81. std::vector<ggml_context_ptr> ctxs;
  82. std::vector<ggml_backend_buffer_ptr> bufs;
  83. size_t total_size() const;
  84. size_t size_r_bytes() const;
  85. size_t size_s_bytes() const;
  86. void state_write_meta(llama_io_write_i & io, const std::vector<std::pair<uint32_t, uint32_t>> & cell_ranges, llama_seq_id seq_id = -1) const;
  87. void state_write_data(llama_io_write_i & io, const std::vector<std::pair<uint32_t, uint32_t>> & cell_ranges) const;
  88. bool state_read_meta(llama_io_read_i & io, uint32_t cell_count, llama_seq_id dest_seq_id = -1);
  89. bool state_read_data(llama_io_read_i & io, uint32_t cell_count);
  90. };
  91. class llama_memory_recurrent_context : public llama_memory_context_i {
  92. public:
  93. // used for errors
  94. llama_memory_recurrent_context(llama_memory_status status);
  95. // used to create a full-cache or update context
  96. llama_memory_recurrent_context(
  97. llama_memory_recurrent * mem);
  98. // used to create a batch processing context from a batch
  99. llama_memory_recurrent_context(
  100. llama_memory_recurrent * mem,
  101. std::vector<llama_ubatch> ubatches);
  102. virtual ~llama_memory_recurrent_context();
  103. //
  104. // llama_memory_context_i
  105. //
  106. bool next() override;
  107. bool apply() override;
  108. llama_memory_status get_status() const override;
  109. const llama_ubatch & get_ubatch() const override;
  110. //
  111. // llama_memory_recurrent_context specific API
  112. //
  113. uint32_t get_n_rs() const;
  114. uint32_t get_head() const;
  115. int32_t get_rs_z() const;
  116. uint32_t get_size() const;
  117. ggml_tensor * get_r_l(int32_t il) const;
  118. ggml_tensor * get_s_l(int32_t il) const;
  119. int32_t s_copy(int i) const;
  120. private:
  121. const llama_memory_status status;
  122. llama_memory_recurrent * mem;
  123. size_t i_next = 0;
  124. std::vector<llama_ubatch> ubatches;
  125. //
  126. // data needed for building the compute graph for the current ubatch:
  127. // TODO: extract all the state like `head` and `n` here
  128. //
  129. const bool is_full = false;
  130. };