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

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