llama-memory-hybrid.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. bool unified,
  26. /* layer filters */
  27. const layer_filter_cb & filter_attn,
  28. const layer_filter_cb & filter_recr) :
  29. hparams(model.hparams),
  30. mem_attn(new llama_kv_cache(
  31. model,
  32. type_k,
  33. type_v,
  34. v_trans,
  35. offload,
  36. unified,
  37. kv_size,
  38. n_seq_max,
  39. n_pad,
  40. n_swa,
  41. swa_type,
  42. filter_attn == nullptr ?
  43. [&](int32_t il) { return !hparams.is_recurrent(il); }
  44. : filter_attn,
  45. nullptr
  46. )),
  47. mem_recr(new llama_memory_recurrent(
  48. model,
  49. type_r,
  50. type_s,
  51. offload,
  52. rs_size,
  53. n_seq_max,
  54. filter_recr == nullptr ?
  55. [&](int32_t il) { return hparams.is_recurrent(il); }
  56. : filter_recr
  57. )) {}
  58. llama_memory_context_ptr llama_memory_hybrid::init_batch(llama_batch_allocr & balloc, uint32_t n_ubatch, bool embd_all) {
  59. do {
  60. balloc.split_reset();
  61. // follow the recurrent pattern for creating the ubatch splits
  62. std::vector<llama_ubatch> ubatches;
  63. while (true) {
  64. llama_ubatch ubatch;
  65. if (embd_all) {
  66. // if all tokens are output, split by sequence
  67. ubatch = balloc.split_seq(n_ubatch);
  68. } else {
  69. // TODO: non-sequential equal split can be done if using unified KV cache
  70. // for simplicity, we always use sequential equal split for now
  71. ubatch = balloc.split_equal(n_ubatch, true);
  72. }
  73. if (ubatch.n_tokens == 0) {
  74. break;
  75. }
  76. ubatches.push_back(std::move(ubatch)); // NOLINT
  77. }
  78. if (balloc.get_n_used() < balloc.get_n_tokens()) {
  79. // failed to find a suitable split
  80. break;
  81. }
  82. // prepare the recurrent batches first
  83. if (!mem_recr->prepare(ubatches)) {
  84. // TODO: will the recurrent cache be in an undefined context at this point?
  85. LLAMA_LOG_ERROR("%s: failed to prepare recurrent ubatches\n", __func__);
  86. return std::make_unique<llama_memory_hybrid_context>(LLAMA_MEMORY_STATUS_FAILED_PREPARE);
  87. }
  88. // prepare the attention cache
  89. auto heads_attn = mem_attn->prepare(ubatches);
  90. if (heads_attn.empty()) {
  91. LLAMA_LOG_ERROR("%s: failed to prepare attention ubatches\n", __func__);
  92. return std::make_unique<llama_memory_hybrid_context>(LLAMA_MEMORY_STATUS_FAILED_PREPARE);
  93. }
  94. return std::make_unique<llama_memory_hybrid_context>(
  95. this, std::move(heads_attn), std::move(ubatches));
  96. } while(false);
  97. return std::make_unique<llama_memory_hybrid_context>(LLAMA_MEMORY_STATUS_FAILED_PREPARE);
  98. }
  99. llama_memory_context_ptr llama_memory_hybrid::init_full() {
  100. return std::make_unique<llama_memory_hybrid_context>(this);
  101. }
  102. llama_memory_context_ptr llama_memory_hybrid::init_update(llama_context * lctx, bool optimize) {
  103. return std::make_unique<llama_memory_hybrid_context>(this, lctx, optimize);
  104. }
  105. bool llama_memory_hybrid::get_can_shift() const {
  106. // Shifting is trivially supported for recurrent
  107. return mem_attn->get_can_shift();
  108. }
  109. void llama_memory_hybrid::clear(bool data) {
  110. mem_attn->clear(data);
  111. mem_recr->clear(data);
  112. }
  113. bool llama_memory_hybrid::seq_rm(llama_seq_id seq_id, llama_pos p0, llama_pos p1) {
  114. // Try removing from the recurrent cache first since it may fail. If it does
  115. // fail, the cache will not have been mutated.
  116. if (!mem_recr->seq_rm(seq_id, p0, p1)) {
  117. return false;
  118. }
  119. return mem_attn->seq_rm(seq_id, p0, p1);
  120. }
  121. void llama_memory_hybrid::seq_cp(llama_seq_id seq_id_src, llama_seq_id seq_id_dst, llama_pos p0, llama_pos p1) {
  122. mem_attn->seq_cp(seq_id_src, seq_id_dst, p0, p1);
  123. mem_recr->seq_cp(seq_id_src, seq_id_dst, p0, p1);
  124. }
  125. void llama_memory_hybrid::seq_keep(llama_seq_id seq_id) {
  126. mem_attn->seq_keep(seq_id);
  127. mem_recr->seq_keep(seq_id);
  128. }
  129. void llama_memory_hybrid::seq_add(llama_seq_id seq_id, llama_pos p0, llama_pos p1, llama_pos shift) {
  130. mem_attn->seq_add(seq_id, p0, p1, shift);
  131. mem_recr->seq_add(seq_id, p0, p1, shift);
  132. }
  133. void llama_memory_hybrid::seq_div(llama_seq_id seq_id, llama_pos p0, llama_pos p1, int d) {
  134. mem_attn->seq_div(seq_id, p0, p1, d);
  135. mem_recr->seq_div(seq_id, p0, p1, d);
  136. }
  137. llama_pos llama_memory_hybrid::seq_pos_min(llama_seq_id seq_id) const {
  138. // the min of the total cache is the max of the two caches' min values
  139. return std::max(mem_attn->seq_pos_min(seq_id), mem_recr->seq_pos_min(seq_id));
  140. }
  141. llama_pos llama_memory_hybrid::seq_pos_max(llama_seq_id seq_id) const {
  142. // the max of the total cache is the min of the two caches' max values
  143. return std::min(mem_attn->seq_pos_max(seq_id), mem_recr->seq_pos_max(seq_id));
  144. }
  145. std::map<ggml_backend_buffer_type_t, size_t> llama_memory_hybrid::memory_breakdown() const {
  146. std::map<ggml_backend_buffer_type_t, size_t> mb = mem_attn->memory_breakdown();
  147. for (const auto & buft_size : mem_recr->memory_breakdown()) {
  148. mb[buft_size.first] += buft_size.second;
  149. }
  150. return mb;
  151. }
  152. void llama_memory_hybrid::state_write(llama_io_write_i & io, llama_seq_id seq_id, llama_state_seq_flags flags) const {
  153. if ((flags & LLAMA_STATE_SEQ_FLAGS_PARTIAL_ONLY) == 0) {
  154. mem_attn->state_write(io, seq_id, flags);
  155. }
  156. mem_recr->state_write(io, seq_id, flags);
  157. }
  158. void llama_memory_hybrid::state_read(llama_io_read_i & io, llama_seq_id seq_id, llama_state_seq_flags flags) {
  159. if ((flags & LLAMA_STATE_SEQ_FLAGS_PARTIAL_ONLY) == 0) {
  160. mem_attn->state_read(io, seq_id, flags);
  161. }
  162. mem_recr->state_read(io, seq_id, flags);
  163. }
  164. llama_kv_cache * llama_memory_hybrid::get_mem_attn() const {
  165. return mem_attn.get();
  166. }
  167. llama_memory_recurrent * llama_memory_hybrid::get_mem_recr() const {
  168. return mem_recr.get();
  169. }
  170. llama_memory_hybrid_context::llama_memory_hybrid_context(llama_memory_status status) : status(status) {}
  171. llama_memory_hybrid_context::llama_memory_hybrid_context(llama_memory_hybrid * mem) :
  172. ctx_attn(mem->get_mem_attn()->init_full()),
  173. ctx_recr(mem->get_mem_recr()->init_full()),
  174. status(llama_memory_status_combine(ctx_attn->get_status(), ctx_recr->get_status())) {
  175. }
  176. llama_memory_hybrid_context::llama_memory_hybrid_context(
  177. llama_memory_hybrid * mem,
  178. llama_context * lctx,
  179. bool optimize) :
  180. ctx_attn(mem->get_mem_attn()->init_update(lctx, optimize)),
  181. ctx_recr(mem->get_mem_recr()->init_update(lctx, optimize)),
  182. status(llama_memory_status_combine(ctx_attn->get_status(), ctx_recr->get_status())) {
  183. }
  184. llama_memory_hybrid_context::llama_memory_hybrid_context(
  185. llama_memory_hybrid * mem,
  186. slot_info_vec_t sinfos_attn,
  187. std::vector<llama_ubatch> ubatches) :
  188. ubatches(std::move(ubatches)),
  189. // note: here we copy the ubatches. not sure if this is ideal
  190. ctx_attn(new llama_kv_cache_context(mem->get_mem_attn(), std::move(sinfos_attn), this->ubatches)),
  191. ctx_recr(new llama_memory_recurrent_context(mem->get_mem_recr(), this->ubatches)),
  192. status(llama_memory_status_combine(ctx_attn->get_status(), ctx_recr->get_status())) {
  193. }
  194. bool llama_memory_hybrid_context::next() {
  195. assert(status == LLAMA_MEMORY_STATUS_SUCCESS);
  196. ctx_attn->next();
  197. ctx_recr->next();
  198. if (++i_next >= ubatches.size()) {
  199. return false;
  200. }
  201. return true;
  202. }
  203. bool llama_memory_hybrid_context::apply() {
  204. assert(!llama_memory_status_is_fail(status));
  205. bool res = true;
  206. res = res & ctx_attn->apply();
  207. res = res & ctx_recr->apply();
  208. return res;
  209. }
  210. llama_memory_status llama_memory_hybrid_context::get_status() const {
  211. return status;
  212. }
  213. const llama_ubatch & llama_memory_hybrid_context::get_ubatch() const {
  214. assert(status == LLAMA_MEMORY_STATUS_SUCCESS);
  215. return ubatches[i_next];
  216. }
  217. const llama_kv_cache_context * llama_memory_hybrid_context::get_attn() const {
  218. return static_cast<const llama_kv_cache_context *>(ctx_attn.get());
  219. }
  220. const llama_memory_recurrent_context * llama_memory_hybrid_context::get_recr() const {
  221. return static_cast<const llama_memory_recurrent_context *>(ctx_recr.get());
  222. }