llama-memory-hybrid.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #include "llama-memory-hybrid.h"
  2. #include "llama-impl.h"
  3. #include "llama-model.h"
  4. #include "llama-context.h"
  5. //
  6. // llama_memory_hybrid
  7. //
  8. llama_memory_hybrid::llama_memory_hybrid(
  9. const llama_model & model,
  10. /* attn */
  11. ggml_type type_k,
  12. ggml_type type_v,
  13. bool v_trans,
  14. uint32_t kv_size,
  15. uint32_t n_pad,
  16. uint32_t n_swa,
  17. llama_swa_type swa_type,
  18. /* recurrent */
  19. ggml_type type_r,
  20. ggml_type type_s,
  21. uint32_t rs_size,
  22. /* common */
  23. uint32_t n_seq_max,
  24. bool offload,
  25. /* layer filters */
  26. layer_filter_cb && filter_attn,
  27. layer_filter_cb && filter_recr) :
  28. hparams(model.hparams),
  29. mem_attn(new llama_kv_cache_unified(
  30. model,
  31. filter_attn == nullptr ?
  32. [&](int32_t il) { return !model.hparams.is_recurrent(il); }
  33. : filter_attn,
  34. type_k,
  35. type_v,
  36. v_trans,
  37. offload,
  38. kv_size,
  39. n_seq_max,
  40. n_pad,
  41. n_swa,
  42. swa_type
  43. )),
  44. mem_recr(new llama_memory_recurrent(
  45. model,
  46. filter_recr == nullptr ?
  47. [&](int32_t il) { return model.hparams.is_recurrent(il); }
  48. : filter_recr,
  49. type_r,
  50. type_s,
  51. offload,
  52. rs_size,
  53. n_seq_max
  54. )) {}
  55. llama_memory_state_ptr llama_memory_hybrid::init_batch(const llama_batch & batch, uint32_t n_ubatch, bool embd_pooled) {
  56. // since this includes a recurrent cache, we cannot use split_simple
  57. auto sbatch = llama_sbatch(batch, hparams.n_embd, false);
  58. // follow the recurrent pattern for creating the ubatch splits
  59. std::vector<llama_ubatch> ubatches;
  60. while (sbatch.n_tokens > 0) {
  61. llama_ubatch ubatch;
  62. if (embd_pooled) {
  63. // Pooled embeddings cannot be split across ubatches (yet)
  64. ubatch = sbatch.split_seq(n_ubatch);
  65. } else {
  66. ubatch = sbatch.split_equal(n_ubatch);
  67. }
  68. ubatches.push_back(ubatch);
  69. }
  70. // prepare the recurrent batches first
  71. if (!mem_recr->prepare(ubatches)) {
  72. // TODO: will the recurrent cache be in an undefined state at this point?
  73. LLAMA_LOG_ERROR("%s: failed to prepare recurrent ubatches\n", __func__);
  74. return std::make_unique<llama_memory_hybrid_state>(LLAMA_MEMORY_STATUS_FAILED_PREPARE);
  75. }
  76. // prepare the attention cache
  77. auto heads_attn = mem_attn->prepare(ubatches);
  78. if (heads_attn.empty()) {
  79. LLAMA_LOG_ERROR("%s: failed to prepare attention ubatches\n", __func__);
  80. return std::make_unique<llama_memory_hybrid_state>(LLAMA_MEMORY_STATUS_FAILED_PREPARE);
  81. }
  82. return std::make_unique<llama_memory_hybrid_state>(
  83. this, std::move(sbatch), std::move(heads_attn), std::move(ubatches));
  84. }
  85. llama_memory_state_ptr llama_memory_hybrid::init_full() {
  86. return std::make_unique<llama_memory_hybrid_state>(this);
  87. }
  88. llama_memory_state_ptr llama_memory_hybrid::init_update(llama_context * lctx, bool optimize) {
  89. return std::make_unique<llama_memory_hybrid_state>(this, lctx, optimize);
  90. }
  91. bool llama_memory_hybrid::get_can_shift() const {
  92. // Shifting is trivially supported for recurrent
  93. return mem_attn->get_can_shift();
  94. }
  95. void llama_memory_hybrid::clear(bool data) {
  96. mem_attn->clear(data);
  97. mem_recr->clear(data);
  98. }
  99. bool llama_memory_hybrid::seq_rm(llama_seq_id seq_id, llama_pos p0, llama_pos p1) {
  100. // Try removing from the recurrent cache first since it may fail. If it does
  101. // fail, the cache will not have been mutated.
  102. if (!mem_recr->seq_rm(seq_id, p0, p1)) {
  103. return false;
  104. }
  105. return mem_attn->seq_rm(seq_id, p0, p1);
  106. }
  107. void llama_memory_hybrid::seq_cp(llama_seq_id seq_id_src, llama_seq_id seq_id_dst, llama_pos p0, llama_pos p1) {
  108. mem_attn->seq_cp(seq_id_src, seq_id_dst, p0, p1);
  109. mem_recr->seq_cp(seq_id_src, seq_id_dst, p0, p1);
  110. }
  111. void llama_memory_hybrid::seq_keep(llama_seq_id seq_id) {
  112. mem_attn->seq_keep(seq_id);
  113. mem_recr->seq_keep(seq_id);
  114. }
  115. void llama_memory_hybrid::seq_add(llama_seq_id seq_id, llama_pos p0, llama_pos p1, llama_pos shift) {
  116. mem_attn->seq_add(seq_id, p0, p1, shift);
  117. mem_recr->seq_add(seq_id, p0, p1, shift);
  118. }
  119. void llama_memory_hybrid::seq_div(llama_seq_id seq_id, llama_pos p0, llama_pos p1, int d) {
  120. mem_attn->seq_div(seq_id, p0, p1, d);
  121. mem_recr->seq_div(seq_id, p0, p1, d);
  122. }
  123. llama_pos llama_memory_hybrid::seq_pos_min(llama_seq_id seq_id) const {
  124. // the min of the total cache is the max of the two caches' min values
  125. return std::max(mem_attn->seq_pos_min(seq_id), mem_recr->seq_pos_min(seq_id));
  126. }
  127. llama_pos llama_memory_hybrid::seq_pos_max(llama_seq_id seq_id) const {
  128. // the max of the total cache is the min of the two caches' max values
  129. return std::min(mem_attn->seq_pos_max(seq_id), mem_recr->seq_pos_max(seq_id));
  130. }
  131. void llama_memory_hybrid::state_write(llama_io_write_i & io, llama_seq_id seq_id) const {
  132. mem_attn->state_write(io, seq_id);
  133. mem_recr->state_write(io, seq_id);
  134. }
  135. void llama_memory_hybrid::state_read(llama_io_read_i & io, llama_seq_id seq_id) {
  136. mem_attn->state_read(io, seq_id);
  137. mem_recr->state_read(io, seq_id);
  138. }
  139. llama_kv_cache_unified * llama_memory_hybrid::get_mem_attn() const {
  140. return mem_attn.get();
  141. }
  142. llama_memory_recurrent * llama_memory_hybrid::get_mem_recr() const {
  143. return mem_recr.get();
  144. }
  145. llama_memory_hybrid_state::llama_memory_hybrid_state(llama_memory_status status) : status(status) {}
  146. llama_memory_hybrid_state::llama_memory_hybrid_state(llama_memory_hybrid * mem) :
  147. state_attn(mem->get_mem_attn()->init_full()),
  148. state_recr(mem->get_mem_recr()->init_full()),
  149. status(llama_memory_status_combine(state_attn->get_status(), state_recr->get_status())) {
  150. }
  151. llama_memory_hybrid_state::llama_memory_hybrid_state(
  152. llama_memory_hybrid * mem,
  153. llama_context * lctx,
  154. bool optimize) :
  155. state_attn(mem->get_mem_attn()->init_update(lctx, optimize)),
  156. state_recr(mem->get_mem_recr()->init_update(lctx, optimize)),
  157. status(llama_memory_status_combine(state_attn->get_status(), state_recr->get_status())) {
  158. }
  159. llama_memory_hybrid_state::llama_memory_hybrid_state(
  160. llama_memory_hybrid * mem,
  161. llama_sbatch sbatch,
  162. std::vector<uint32_t> heads_attn,
  163. std::vector<llama_ubatch> ubatches) :
  164. sbatch(std::move(sbatch)),
  165. ubatches(std::move(ubatches)),
  166. // note: here we copy the ubatches. not sure if this is ideal
  167. state_attn(new llama_kv_cache_unified_state(mem->get_mem_attn(), {}, std::move(heads_attn), this->ubatches)),
  168. state_recr(new llama_memory_recurrent_state(mem->get_mem_recr(), {}, this->ubatches)),
  169. status(LLAMA_MEMORY_STATUS_SUCCESS) {
  170. }
  171. bool llama_memory_hybrid_state::next() {
  172. assert(status == LLAMA_MEMORY_STATUS_SUCCESS);
  173. state_attn->next();
  174. state_recr->next();
  175. if (++i_next >= ubatches.size()) {
  176. return false;
  177. }
  178. return true;
  179. }
  180. bool llama_memory_hybrid_state::apply() {
  181. assert(status == LLAMA_MEMORY_STATUS_SUCCESS);
  182. bool res = true;
  183. res = res & state_attn->apply();
  184. res = res & state_recr->apply();
  185. return res;
  186. }
  187. std::vector<int64_t> & llama_memory_hybrid_state::out_ids() {
  188. assert(status == LLAMA_MEMORY_STATUS_SUCCESS);
  189. return sbatch.out_ids;
  190. }
  191. llama_memory_status llama_memory_hybrid_state::get_status() const {
  192. return status;
  193. }
  194. const llama_ubatch & llama_memory_hybrid_state::get_ubatch() const {
  195. assert(status == LLAMA_MEMORY_STATUS_SUCCESS);
  196. return ubatches[i_next];
  197. }
  198. const llama_kv_cache_unified_state * llama_memory_hybrid_state::get_state_attn() const {
  199. return static_cast<const llama_kv_cache_unified_state *>(state_attn.get());
  200. }
  201. const llama_memory_recurrent_state * llama_memory_hybrid_state::get_state_recr() const {
  202. return static_cast<const llama_memory_recurrent_state *>(state_recr.get());
  203. }