llama-kv-cache-unified-iswa.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #include "llama-kv-cache-unified-iswa.h"
  2. #include "llama-impl.h"
  3. #include "llama-batch.h"
  4. #include "llama-model.h"
  5. #include <algorithm>
  6. #include <cassert>
  7. //
  8. // llama_kv_cache_unified_iswa
  9. //
  10. llama_kv_cache_unified_iswa::llama_kv_cache_unified_iswa(
  11. const llama_model & model,
  12. ggml_type type_k,
  13. ggml_type type_v,
  14. bool v_trans,
  15. bool offload,
  16. bool swa_full,
  17. uint32_t kv_size,
  18. uint32_t n_seq_max,
  19. uint32_t n_ubatch,
  20. uint32_t n_pad) : hparams(model.hparams) {
  21. llama_kv_cache_unified::layer_filter_cb filter_base = [&](int32_t il) { return !model.hparams.is_swa(il); };
  22. llama_kv_cache_unified::layer_filter_cb filter_swa = [&](int32_t il) { return model.hparams.is_swa(il); };
  23. const uint32_t size_base = kv_size;
  24. uint32_t size_swa = std::min(size_base, GGML_PAD(hparams.n_swa*n_seq_max + n_ubatch, n_pad));
  25. // when using full-size SWA cache, we set the SWA cache size to be equal to the base cache size
  26. if (swa_full) {
  27. LLAMA_LOG_WARN("%s: using full-size SWA cache (ref: %s)\n",
  28. __func__, "https://github.com/ggml-org/llama.cpp/pull/13194#issuecomment-2868343055");
  29. size_swa = size_base;
  30. }
  31. LLAMA_LOG_INFO("%s: creating non-SWA KV cache, size = %u cells\n", __func__, size_base);
  32. kv_base = std::make_unique<llama_kv_cache_unified>(
  33. model, std::move(filter_base), type_k, type_v,
  34. v_trans, offload, size_base, n_seq_max, n_pad,
  35. 0, LLAMA_SWA_TYPE_NONE);
  36. LLAMA_LOG_INFO("%s: creating SWA KV cache, size = %u cells\n", __func__, size_swa);
  37. kv_swa = std::make_unique<llama_kv_cache_unified>(
  38. model, std::move(filter_swa), type_k, type_v,
  39. v_trans, offload, size_swa, n_seq_max, n_pad,
  40. hparams.n_swa, hparams.swa_type);
  41. }
  42. void llama_kv_cache_unified_iswa::clear(bool data) {
  43. kv_base->clear(data);
  44. kv_swa ->clear(data);
  45. }
  46. bool llama_kv_cache_unified_iswa::seq_rm(llama_seq_id seq_id, llama_pos p0, llama_pos p1) {
  47. bool res = true;
  48. res = res & kv_base->seq_rm(seq_id, p0, p1);
  49. res = res & kv_swa ->seq_rm(seq_id, p0, p1);
  50. return res;
  51. }
  52. void llama_kv_cache_unified_iswa::seq_cp(llama_seq_id seq_id_src, llama_seq_id seq_id_dst, llama_pos p0, llama_pos p1) {
  53. kv_base->seq_cp(seq_id_src, seq_id_dst, p0, p1);
  54. kv_swa ->seq_cp(seq_id_src, seq_id_dst, p0, p1);
  55. }
  56. void llama_kv_cache_unified_iswa::seq_keep(llama_seq_id seq_id) {
  57. kv_base->seq_keep(seq_id);
  58. kv_swa ->seq_keep(seq_id);
  59. }
  60. void llama_kv_cache_unified_iswa::seq_add(llama_seq_id seq_id, llama_pos p0, llama_pos p1, llama_pos shift) {
  61. kv_base->seq_add(seq_id, p0, p1, shift);
  62. kv_swa ->seq_add(seq_id, p0, p1, shift);
  63. }
  64. void llama_kv_cache_unified_iswa::seq_div(llama_seq_id seq_id, llama_pos p0, llama_pos p1, int d) {
  65. kv_base->seq_div(seq_id, p0, p1, d);
  66. kv_swa ->seq_div(seq_id, p0, p1, d);
  67. }
  68. llama_pos llama_kv_cache_unified_iswa::seq_pos_min(llama_seq_id seq_id) const {
  69. // the base cache is a superset of the SWA cache, so we can just check the SWA cache
  70. return kv_swa->seq_pos_min(seq_id);
  71. }
  72. llama_pos llama_kv_cache_unified_iswa::seq_pos_max(llama_seq_id seq_id) const {
  73. return kv_swa->seq_pos_max(seq_id);
  74. }
  75. llama_memory_context_ptr llama_kv_cache_unified_iswa::init_batch(llama_batch_allocr & balloc, uint32_t n_ubatch, bool embd_all) {
  76. GGML_UNUSED(embd_all);
  77. // first try simple split
  78. do {
  79. balloc.split_reset();
  80. std::vector<llama_ubatch> ubatches;
  81. while (true) {
  82. auto ubatch = balloc.split_simple(n_ubatch);
  83. if (ubatch.n_tokens == 0) {
  84. break;
  85. }
  86. ubatches.push_back(std::move(ubatch)); // NOLINT
  87. }
  88. if (balloc.get_n_used() < balloc.get_n_tokens()) {
  89. // failed to find a suitable split
  90. break;
  91. }
  92. auto sinfos_base = kv_base->prepare(ubatches);
  93. if (sinfos_base.empty()) {
  94. break;
  95. }
  96. auto sinfos_swa = kv_swa->prepare(ubatches);
  97. if (sinfos_swa.empty()) {
  98. break;
  99. }
  100. assert(sinfos_base.size() == sinfos_swa.size());
  101. return std::make_unique<llama_kv_cache_unified_iswa_context>(
  102. this, std::move(sinfos_base), std::move(sinfos_swa), std::move(ubatches));
  103. } while (false);
  104. // if it fails, try equal split
  105. do {
  106. balloc.split_reset();
  107. std::vector<llama_ubatch> ubatches;
  108. while (true) {
  109. auto ubatch = balloc.split_equal(n_ubatch, false);
  110. if (ubatch.n_tokens == 0) {
  111. break;
  112. }
  113. ubatches.push_back(std::move(ubatch)); // NOLINT
  114. }
  115. if (balloc.get_n_used() < balloc.get_n_tokens()) {
  116. // failed to find a suitable split
  117. break;
  118. }
  119. auto sinfos_base = kv_base->prepare(ubatches);
  120. if (sinfos_base.empty()) {
  121. break;
  122. }
  123. auto sinfos_swa = kv_swa->prepare(ubatches);
  124. if (sinfos_swa.empty()) {
  125. break;
  126. }
  127. assert(sinfos_base.size() == sinfos_swa.size());
  128. return std::make_unique<llama_kv_cache_unified_iswa_context>(
  129. this, std::move(sinfos_base), std::move(sinfos_swa), std::move(ubatches));
  130. } while (false);
  131. // TODO: if we fail again, we should attempt different splitting strategies
  132. // but to do that properly, we first have to refactor the batches to be more flexible
  133. return std::make_unique<llama_kv_cache_unified_iswa_context>(LLAMA_MEMORY_STATUS_FAILED_PREPARE);
  134. }
  135. llama_memory_context_ptr llama_kv_cache_unified_iswa::init_full() {
  136. return std::make_unique<llama_kv_cache_unified_iswa_context>(this);
  137. }
  138. llama_memory_context_ptr llama_kv_cache_unified_iswa::init_update(llama_context * lctx, bool optimize) {
  139. return std::make_unique<llama_kv_cache_unified_iswa_context>(this, lctx, optimize);
  140. }
  141. bool llama_kv_cache_unified_iswa::get_can_shift() const {
  142. return kv_base->get_size() == kv_swa->get_size();
  143. }
  144. void llama_kv_cache_unified_iswa::state_write(llama_io_write_i & io, llama_seq_id seq_id) const {
  145. kv_base->state_write(io, seq_id);
  146. kv_swa ->state_write(io, seq_id);
  147. }
  148. void llama_kv_cache_unified_iswa::state_read(llama_io_read_i & io, llama_seq_id seq_id) {
  149. kv_base->state_read(io, seq_id);
  150. kv_swa ->state_read(io, seq_id);
  151. }
  152. llama_kv_cache_unified * llama_kv_cache_unified_iswa::get_base() const {
  153. return kv_base.get();
  154. }
  155. llama_kv_cache_unified * llama_kv_cache_unified_iswa::get_swa() const {
  156. return kv_swa.get();
  157. }
  158. //
  159. // llama_kv_cache_unified_iswa_context
  160. //
  161. llama_kv_cache_unified_iswa_context::llama_kv_cache_unified_iswa_context(llama_memory_status status) : status(status) {}
  162. llama_kv_cache_unified_iswa_context::llama_kv_cache_unified_iswa_context(
  163. llama_kv_cache_unified_iswa * kv) :
  164. ctx_base(kv->get_base()->init_full()),
  165. ctx_swa (kv->get_swa ()->init_full()),
  166. status(llama_memory_status_combine(ctx_base->get_status(), ctx_swa->get_status())) {
  167. }
  168. llama_kv_cache_unified_iswa_context::llama_kv_cache_unified_iswa_context(
  169. llama_kv_cache_unified_iswa * kv,
  170. llama_context * lctx,
  171. bool optimize) :
  172. ctx_base(kv->get_base()->init_update(lctx, optimize)),
  173. ctx_swa (kv->get_swa ()->init_update(lctx, optimize)),
  174. status(llama_memory_status_combine(ctx_base->get_status(), ctx_swa->get_status())) {
  175. }
  176. llama_kv_cache_unified_iswa_context::llama_kv_cache_unified_iswa_context(
  177. llama_kv_cache_unified_iswa * kv,
  178. slot_info_vec_t sinfos_base,
  179. slot_info_vec_t sinfos_swa,
  180. std::vector<llama_ubatch> ubatches) :
  181. ubatches(std::move(ubatches)),
  182. // note: here we copy the ubatches. not sure if this is ideal
  183. ctx_base(new llama_kv_cache_unified_context(kv->get_base(), std::move(sinfos_base), this->ubatches)),
  184. ctx_swa (new llama_kv_cache_unified_context(kv->get_swa (), std::move(sinfos_swa), this->ubatches)),
  185. status(llama_memory_status_combine(ctx_base->get_status(), ctx_swa->get_status())) {
  186. }
  187. llama_kv_cache_unified_iswa_context:: ~llama_kv_cache_unified_iswa_context() = default;
  188. bool llama_kv_cache_unified_iswa_context::next() {
  189. assert(status == LLAMA_MEMORY_STATUS_SUCCESS);
  190. ctx_base->next();
  191. ctx_swa ->next();
  192. if (++i_next >= ubatches.size()) {
  193. return false;
  194. }
  195. return true;
  196. }
  197. bool llama_kv_cache_unified_iswa_context::apply() {
  198. assert(!llama_memory_status_is_fail(status));
  199. bool res = true;
  200. res = res & ctx_base->apply();
  201. res = res & ctx_swa ->apply();
  202. return res;
  203. }
  204. llama_memory_status llama_kv_cache_unified_iswa_context::get_status() const {
  205. return status;
  206. }
  207. const llama_ubatch & llama_kv_cache_unified_iswa_context::get_ubatch() const {
  208. assert(status == LLAMA_MEMORY_STATUS_SUCCESS);
  209. return ubatches[i_next];
  210. }
  211. const llama_kv_cache_unified_context * llama_kv_cache_unified_iswa_context::get_base() const {
  212. assert(status == LLAMA_MEMORY_STATUS_SUCCESS);
  213. return static_cast<const llama_kv_cache_unified_context *>(ctx_base.get());
  214. }
  215. const llama_kv_cache_unified_context * llama_kv_cache_unified_iswa_context::get_swa() const {
  216. assert(status == LLAMA_MEMORY_STATUS_SUCCESS);
  217. return static_cast<const llama_kv_cache_unified_context *>(ctx_swa.get());
  218. }