llama-kv-cache.cpp 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494
  1. #include "llama-kv-cache.h"
  2. #include "llama-impl.h"
  3. #include "llama-batch.h"
  4. #include "llama-cparams.h"
  5. #include "llama-model.h"
  6. #include "llama-context.h"
  7. #include <algorithm>
  8. #include <cassert>
  9. #include <cmath>
  10. #include <limits>
  11. #include <map>
  12. #include <stdexcept>
  13. //
  14. // llama_kv_cache_unified
  15. //
  16. uint32_t llama_kv_cache_unified::get_padding(const llama_cparams & cparams) {
  17. // the FA kernels require padding to avoid extra runtime boundary checks
  18. return cparams.flash_attn ? 256u : 32u;
  19. }
  20. llama_kv_cache_unified::llama_kv_cache_unified(
  21. const llama_model & model,
  22. ggml_type type_k,
  23. ggml_type type_v,
  24. bool v_trans,
  25. bool offload,
  26. uint32_t kv_size,
  27. uint32_t padding) : model(model), hparams(model.hparams), v_trans(v_trans), padding(padding) {
  28. const int32_t n_layer = hparams.n_layer;
  29. has_shift = false;
  30. can_shift = true;
  31. LLAMA_LOG_INFO("%s: kv_size = %d, type_k = '%s', type_v = '%s', n_layer = %d, can_shift = %d, padding = %d\n",
  32. __func__, kv_size, ggml_type_name(type_k), ggml_type_name(type_v), n_layer, can_shift, padding);
  33. GGML_ASSERT(kv_size % padding == 0 && "kv_size must be a multiple of padding");
  34. head = 0;
  35. size = kv_size;
  36. used = 0;
  37. this->type_k = type_k;
  38. this->type_v = type_v;
  39. cells.clear();
  40. cells.resize(kv_size);
  41. // create a context for each buffer type
  42. std::map<ggml_backend_buffer_type_t, ggml_context *> ctx_map;
  43. auto ctx_for_buft = [&](ggml_backend_buffer_type_t buft) -> ggml_context * {
  44. auto it = ctx_map.find(buft);
  45. if (it == ctx_map.end()) {
  46. ggml_init_params params = {
  47. /*.mem_size =*/ size_t(2u*n_layer*ggml_tensor_overhead()),
  48. /*.mem_buffer =*/ NULL,
  49. /*.no_alloc =*/ true,
  50. };
  51. ggml_context * ctx = ggml_init(params);
  52. if (!ctx) {
  53. return nullptr;
  54. }
  55. ctx_map[buft] = ctx;
  56. ctxs.emplace_back(ctx);
  57. return ctx;
  58. }
  59. return it->second;
  60. };
  61. k_l.reserve(n_layer);
  62. v_l.reserve(n_layer);
  63. for (int i = 0; i < n_layer; i++) {
  64. const uint32_t n_embd_k_gqa = hparams.n_embd_k_gqa(i) + hparams.n_embd_k_s();
  65. const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(i) + hparams.n_embd_v_s();
  66. const char * dev_name = "CPU";
  67. ggml_backend_buffer_type_t buft = ggml_backend_cpu_buffer_type();
  68. if (offload) {
  69. auto * dev = model.dev_layer(i);
  70. buft = ggml_backend_dev_buffer_type(dev);
  71. dev_name = ggml_backend_dev_name(dev);
  72. }
  73. LLAMA_LOG_DEBUG("%s: layer %3d: dev = %s\n", __func__, i, dev_name);
  74. ggml_context * ctx = ctx_for_buft(buft);
  75. if (!ctx) {
  76. throw std::runtime_error("failed to create ggml context for kv cache");
  77. }
  78. ggml_tensor * k = ggml_new_tensor_1d(ctx, type_k, n_embd_k_gqa*kv_size);
  79. ggml_tensor * v = ggml_new_tensor_1d(ctx, type_v, n_embd_v_gqa*kv_size);
  80. ggml_format_name(k, "cache_k_l%d", i);
  81. ggml_format_name(v, "cache_v_l%d", i);
  82. k_l.push_back(k);
  83. v_l.push_back(v);
  84. }
  85. // allocate tensors and initialize the buffers to avoid NaNs in the padding
  86. for (auto it : ctx_map) {
  87. auto * buft = it.first;
  88. auto * ctx = it.second;
  89. ggml_backend_buffer_t buf = ggml_backend_alloc_ctx_tensors_from_buft(ctx, buft);
  90. if (!buf) {
  91. throw std::runtime_error("failed to allocate buffer for kv cache");
  92. }
  93. ggml_backend_buffer_clear(buf, 0);
  94. LLAMA_LOG_INFO("%s: %10s KV buffer size = %8.2f MiB\n", __func__, ggml_backend_buffer_name(buf), ggml_backend_buffer_get_size(buf)/1024.0/1024.0);
  95. bufs.emplace_back(buf);
  96. }
  97. {
  98. const size_t memory_size_k = size_k_bytes();
  99. const size_t memory_size_v = size_v_bytes();
  100. LLAMA_LOG_INFO("%s: KV self size = %7.2f MiB, K (%s): %7.2f MiB, V (%s): %7.2f MiB\n", __func__,
  101. (float)(memory_size_k + memory_size_v) / (1024.0f * 1024.0f),
  102. ggml_type_name(type_k), (float)memory_size_k / (1024.0f * 1024.0f),
  103. ggml_type_name(type_v), (float)memory_size_v / (1024.0f * 1024.0f));
  104. }
  105. }
  106. void llama_kv_cache_unified::clear() {
  107. for (int32_t i = 0; i < (int32_t) size; ++i) {
  108. cells[i].pos = -1;
  109. cells[i].seq_id.clear();
  110. }
  111. head = 0;
  112. used = 0;
  113. for (auto & buf : bufs) {
  114. ggml_backend_buffer_clear(buf.get(), 0);
  115. }
  116. }
  117. bool llama_kv_cache_unified::seq_rm(llama_seq_id seq_id, llama_pos p0, llama_pos p1) {
  118. uint32_t new_head = size;
  119. if (p0 < 0) {
  120. p0 = 0;
  121. }
  122. if (p1 < 0) {
  123. p1 = std::numeric_limits<llama_pos>::max();
  124. }
  125. for (uint32_t i = 0; i < size; ++i) {
  126. if (cells[i].pos >= p0 && cells[i].pos < p1) {
  127. if (seq_id < 0) {
  128. cells[i].seq_id.clear();
  129. } else if (cells[i].has_seq_id(seq_id)) {
  130. cells[i].seq_id.erase(seq_id);
  131. } else {
  132. continue;
  133. }
  134. if (cells[i].is_empty()) {
  135. // keep count of the number of used cells
  136. if (cells[i].pos >= 0) {
  137. used--;
  138. }
  139. cells[i].pos = -1;
  140. if (new_head == size) {
  141. new_head = i;
  142. }
  143. }
  144. }
  145. }
  146. // If we freed up a slot, set head to it so searching can start there.
  147. if (new_head != size && new_head < head) {
  148. head = new_head;
  149. }
  150. return true;
  151. }
  152. void llama_kv_cache_unified::seq_cp(llama_seq_id seq_id_src, llama_seq_id seq_id_dst, llama_pos p0, llama_pos p1) {
  153. if (seq_id_src == seq_id_dst) {
  154. return;
  155. }
  156. if (p0 < 0) {
  157. p0 = 0;
  158. }
  159. if (p1 < 0) {
  160. p1 = std::numeric_limits<llama_pos>::max();
  161. }
  162. // otherwise, this is the KV of a Transformer-like model
  163. head = 0;
  164. for (uint32_t i = 0; i < size; ++i) {
  165. if (cells[i].has_seq_id(seq_id_src) && cells[i].pos >= p0 && cells[i].pos < p1) {
  166. cells[i].seq_id.insert(seq_id_dst);
  167. }
  168. }
  169. }
  170. void llama_kv_cache_unified::seq_keep(llama_seq_id seq_id) {
  171. uint32_t new_head = size;
  172. for (uint32_t i = 0; i < size; ++i) {
  173. if (!cells[i].has_seq_id(seq_id)) {
  174. if (cells[i].pos >= 0) {
  175. used--;
  176. }
  177. cells[i].pos = -1;
  178. cells[i].seq_id.clear();
  179. if (new_head == size){
  180. new_head = i;
  181. }
  182. } else {
  183. cells[i].seq_id.clear();
  184. cells[i].seq_id.insert(seq_id);
  185. }
  186. }
  187. // If we freed up a slot, set head to it so searching can start there.
  188. if (new_head != size && new_head < head) {
  189. head = new_head;
  190. }
  191. }
  192. void llama_kv_cache_unified::seq_add(llama_seq_id seq_id, llama_pos p0, llama_pos p1, llama_pos delta) {
  193. if (delta == 0) {
  194. return;
  195. }
  196. uint32_t new_head = size;
  197. if (p0 < 0) {
  198. p0 = 0;
  199. }
  200. if (p1 < 0) {
  201. p1 = std::numeric_limits<llama_pos>::max();
  202. }
  203. // If there is no range then return early to avoid looping over the
  204. if (p0 == p1) {
  205. return;
  206. }
  207. for (uint32_t i = 0; i < size; ++i) {
  208. if (cells[i].has_seq_id(seq_id) && cells[i].pos >= p0 && cells[i].pos < p1) {
  209. has_shift = true;
  210. cells[i].pos += delta;
  211. cells[i].delta += delta;
  212. if (cells[i].pos < 0) {
  213. if (!cells[i].is_empty()) {
  214. used--;
  215. }
  216. cells[i].pos = -1;
  217. cells[i].seq_id.clear();
  218. if (new_head == size) {
  219. new_head = i;
  220. }
  221. }
  222. }
  223. }
  224. // If we freed up a slot, set head to it so searching can start there.
  225. // Otherwise we just start the next search from the beginning.
  226. head = new_head != size ? new_head : 0;
  227. }
  228. void llama_kv_cache_unified::seq_div(llama_seq_id seq_id, llama_pos p0, llama_pos p1, int d) {
  229. if (d == 1) {
  230. return;
  231. }
  232. if (p0 < 0) {
  233. p0 = 0;
  234. }
  235. if (p1 < 0) {
  236. p1 = std::numeric_limits<llama_pos>::max();
  237. }
  238. // If there is no range then return early to avoid looping over the cache.
  239. if (p0 == p1) {
  240. return;
  241. }
  242. for (uint32_t i = 0; i < size; ++i) {
  243. if (cells[i].has_seq_id(seq_id) && cells[i].pos >= p0 && cells[i].pos < p1) {
  244. has_shift = true;
  245. {
  246. llama_pos p_old = cells[i].pos;
  247. cells[i].pos /= d;
  248. cells[i].delta += cells[i].pos - p_old;
  249. }
  250. }
  251. }
  252. }
  253. llama_pos llama_kv_cache_unified::seq_pos_max(llama_seq_id seq_id) const {
  254. llama_pos result = 0;
  255. for (uint32_t i = 0; i < size; ++i) {
  256. if (cells[i].has_seq_id(seq_id)) {
  257. result = std::max(result, cells[i].pos);
  258. }
  259. }
  260. return result;
  261. }
  262. void llama_kv_cache_unified::restore() {
  263. if (pending.ranges.empty()) {
  264. return;
  265. }
  266. uint32_t new_head = size;
  267. for (auto & range : pending.ranges) {
  268. for (uint32_t i = range.c0; i < range.c1; ++i) {
  269. cells[i].seq_id.clear();
  270. // keep count of the number of used cells
  271. if (cells[i].pos >= 0) {
  272. used--;
  273. }
  274. cells[i].pos = -1;
  275. }
  276. new_head = std::min(new_head, range.c0);
  277. }
  278. if (new_head != size && new_head < head) {
  279. head = new_head;
  280. }
  281. }
  282. void llama_kv_cache_unified::commit() {
  283. if (pending.ranges.empty()) {
  284. LLAMA_LOG_WARN("%s: no pending KV cache updates to commit - might indicate a bug (ref: %s)\n",
  285. __func__, "https://github.com/ggml-org/llama.cpp/pull/12695");
  286. return;
  287. }
  288. pending.ranges.clear();
  289. }
  290. bool llama_kv_cache_unified::update(llama_context & lctx) {
  291. bool need_reserve = false;
  292. auto * sched = lctx.get_sched();
  293. if (has_shift) {
  294. if (!get_can_shift()) {
  295. GGML_ABORT("The current KV cache / model configuration does not support K-shift");
  296. }
  297. LLAMA_LOG_DEBUG("%s: applying K-shift\n", __func__);
  298. // apply K-shift if needed
  299. if (hparams.rope_type != LLAMA_ROPE_TYPE_NONE) {
  300. ggml_backend_sched_reset(sched);
  301. auto * gf = lctx.graph_init();
  302. auto res = build_graph_shift(lctx.get_cparams(), lctx.get_ctx_compute(), gf);
  303. ggml_backend_sched_alloc_graph(sched, gf);
  304. res->set_inputs(nullptr);
  305. lctx.graph_compute(gf, false);
  306. need_reserve = true;
  307. }
  308. {
  309. has_shift = false;
  310. for (uint32_t i = 0; i < size; ++i) {
  311. cells[i].delta = 0;
  312. }
  313. }
  314. }
  315. if (do_defrag) {
  316. LLAMA_LOG_DEBUG("%s: defragmenting KV cache\n", __func__);
  317. if (defrag_prepare(lctx.graph_max_nodes())) {
  318. ggml_backend_sched_reset(sched);
  319. auto * gf = lctx.graph_init();
  320. auto res = build_graph_defrag(lctx.get_cparams(), lctx.get_ctx_compute(), gf);
  321. ggml_backend_sched_alloc_graph(sched, gf);
  322. res->set_inputs(nullptr);
  323. lctx.graph_compute(gf, false);
  324. need_reserve = true;
  325. }
  326. do_defrag = false;
  327. }
  328. return need_reserve;
  329. }
  330. void llama_kv_cache_unified::defrag_sched(float thold) {
  331. // - do not defrag small contexts (i.e. < 2048 tokens)
  332. // - count the padding towards the number of used tokens
  333. const float fragmentation = n >= 2048 ? std::max(0.0f, 1.0f - (float(used + padding)/n)) : 0.0f;
  334. // queue defragmentation for next llama_kv_cache_update
  335. if (fragmentation > thold) {
  336. LLAMA_LOG_DEBUG("%s: fragmentation: %.2f - requesting defrag\n", __func__, fragmentation);
  337. do_defrag = true;
  338. }
  339. }
  340. void llama_kv_cache_unified::set_full() {
  341. n = size;
  342. // when simulating a full KV cache, the specific value of the "head" pointer is not important because it does not
  343. // affect the shapes of the tensors in the compute graph - it only affects the offsets of the K/V views.
  344. // we should only guarantee that the head position won't cause out-of-bounds view of the K, V tensors, so
  345. // setting it to 0 is the simplest way to achieve that
  346. // ref: https://github.com/ggml-org/llama.cpp/issues/13359
  347. head = 0;
  348. }
  349. llama_sbatch llama_kv_cache_unified::sbatch_init(
  350. const llama_batch & batch,
  351. bool logits_all) {
  352. return llama_sbatch(batch, hparams.n_embd, true, logits_all);
  353. }
  354. llama_ubatch llama_kv_cache_unified::ubatch_next(
  355. llama_sbatch & sbatch,
  356. uint32_t n_ubatch,
  357. bool embd_pooled) const {
  358. GGML_UNUSED(embd_pooled);
  359. return sbatch.split_simple(n_ubatch);
  360. }
  361. bool llama_kv_cache_unified::find_slot(
  362. const llama_ubatch & ubatch) {
  363. const uint32_t n_tokens = ubatch.n_tokens;
  364. const uint32_t n_seqs = ubatch.n_seqs;
  365. const uint32_t n_seq_tokens = ubatch.n_seq_tokens;
  366. // if we have enough unused cells before the current head ->
  367. // better to start searching from the beginning of the cache, hoping to fill it
  368. if (head > used + 2*ubatch.n_tokens) {
  369. head = 0;
  370. }
  371. // otherwise, one cell per token.
  372. if (n_tokens > size) {
  373. LLAMA_LOG_ERROR("%s: n_tokens = %d > size = %d\n", __func__, n_tokens, size);
  374. return false;
  375. }
  376. uint32_t n_tested = 0;
  377. while (true) {
  378. if (head + n_tokens > size) {
  379. n_tested += size - head;
  380. head = 0;
  381. continue;
  382. }
  383. bool found = true;
  384. for (uint32_t i = 0; i < n_tokens; i++) {
  385. if (cells[head + i].pos >= 0) {
  386. found = false;
  387. head += i + 1;
  388. n_tested += i + 1;
  389. break;
  390. }
  391. }
  392. if (found) {
  393. break;
  394. }
  395. if (n_tested >= size) {
  396. //LLAMA_LOG_ERROR("%s: failed to find a slot for %d tokens\n", __func__, n_tokens);
  397. return false;
  398. }
  399. }
  400. for (uint32_t s = 0; s < n_seqs; s++) {
  401. for (uint32_t i = 0; i < n_seq_tokens; ++i) {
  402. uint32_t k = s*n_seq_tokens + i;
  403. cells[head + k].pos = ubatch.pos[k];
  404. for (int32_t j = 0; j < ubatch.n_seq_id[s]; j++) {
  405. cells[head + k].seq_id.insert(ubatch.seq_id[s][j]);
  406. }
  407. }
  408. }
  409. used += n_tokens;
  410. pending.ranges.push_back({head, head + n_tokens});
  411. // a heuristic, to avoid attending the full cache if it is not yet utilized
  412. // after enough generations, the benefit from this heuristic disappears
  413. // if we start defragmenting the cache, the benefit from this will be more important
  414. n = std::min(size, std::max(padding, GGML_PAD(cell_max(), padding)));
  415. //printf("n = %5d, used = %5d, head = %5d\n", n, used, head);
  416. return true;
  417. }
  418. int32_t llama_kv_cache_unified::get_n_tokens() const {
  419. int32_t result = 0;
  420. for (uint32_t i = 0; i < size; i++) {
  421. result += cells[i].seq_id.size();
  422. }
  423. return result;
  424. }
  425. int32_t llama_kv_cache_unified::get_used_cells() const {
  426. return used;
  427. }
  428. bool llama_kv_cache_unified::get_can_shift() const {
  429. return can_shift;
  430. }
  431. llama_pos llama_kv_cache_unified::get_pos_max() const {
  432. llama_pos pos_max = -1;
  433. for (const auto & cell : cells) {
  434. pos_max = std::max(pos_max, cell.pos);
  435. }
  436. return pos_max;
  437. }
  438. size_t llama_kv_cache_unified::total_size() const {
  439. size_t size = 0;
  440. for (const auto & buf : bufs) {
  441. size += ggml_backend_buffer_get_size(buf.get());
  442. }
  443. return size;
  444. }
  445. size_t llama_kv_cache_unified::size_k_bytes() const {
  446. size_t size_k_bytes = 0;
  447. for (const auto & k : k_l) {
  448. size_k_bytes += ggml_nbytes(k);
  449. }
  450. return size_k_bytes;
  451. }
  452. size_t llama_kv_cache_unified::size_v_bytes() const {
  453. size_t size_v_bytes = 0;
  454. for (const auto & v : v_l) {
  455. size_v_bytes += ggml_nbytes(v);
  456. }
  457. return size_v_bytes;
  458. }
  459. ggml_tensor * llama_kv_cache_unified::build_rope_shift(
  460. const llama_cparams & cparams,
  461. ggml_context * ctx,
  462. ggml_tensor * cur,
  463. ggml_tensor * shift,
  464. ggml_tensor * factors,
  465. float freq_base,
  466. float freq_scale) const {
  467. const auto & n_ctx_orig = cparams.n_ctx_orig_yarn;
  468. const auto & yarn_ext_factor = cparams.yarn_ext_factor;
  469. const auto & yarn_beta_fast = cparams.yarn_beta_fast;
  470. const auto & yarn_beta_slow = cparams.yarn_beta_slow;
  471. const auto & n_rot = hparams.n_rot;
  472. const auto & rope_type = hparams.rope_type;
  473. // See llm_build_deepseek2() for why attn_factor has to be scaled for YaRN RoPE to work correctly.
  474. // See https://github.com/ggerganov/llama.cpp/discussions/7416 for detailed explanation.
  475. const float yarn_attn_factor = model.arch == LLM_ARCH_DEEPSEEK2 ? 1.0f / (1.0f + 0.1f * logf(1.0f / freq_scale)) : cparams.yarn_attn_factor;
  476. ggml_tensor * tmp;
  477. if (ggml_is_quantized(cur->type)) {
  478. // dequantize to f32 -> RoPE -> quantize back
  479. tmp = ggml_cast(ctx, cur, GGML_TYPE_F32);
  480. tmp = ggml_rope_ext(ctx, tmp,
  481. shift, factors, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  482. yarn_ext_factor, yarn_attn_factor, yarn_beta_fast, yarn_beta_slow);
  483. tmp = ggml_cpy(ctx, tmp, cur);
  484. } else {
  485. // we rotate only the first n_rot dimensions
  486. tmp = ggml_rope_ext_inplace(ctx, cur,
  487. shift, factors, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  488. yarn_ext_factor, yarn_attn_factor, yarn_beta_fast, yarn_beta_slow);
  489. }
  490. return tmp;
  491. }
  492. class llm_graph_input_k_shift : public llm_graph_input_i {
  493. public:
  494. llm_graph_input_k_shift(const llama_kv_cache_unified * kv_self) : kv_self(kv_self) {}
  495. virtual ~llm_graph_input_k_shift() = default;
  496. void set_input(const llama_ubatch * ubatch) override;
  497. ggml_tensor * k_shift; // I32 [kv_size]
  498. const llama_kv_cache_unified * kv_self;
  499. };
  500. void llm_graph_input_k_shift::set_input(const llama_ubatch * ubatch) {
  501. GGML_UNUSED(ubatch);
  502. if (k_shift) {
  503. assert(ggml_backend_buffer_is_host(k_shift->buffer));
  504. int32_t * data = (int32_t *) k_shift->data;
  505. for (uint32_t i = 0; i < kv_self->size; ++i) {
  506. data[i] = kv_self->cells[i].delta;
  507. }
  508. }
  509. }
  510. llm_graph_result_ptr llama_kv_cache_unified::build_graph_shift(
  511. const llama_cparams & cparams,
  512. ggml_context * ctx,
  513. ggml_cgraph * gf) const {
  514. auto res = std::make_unique<llm_graph_result>();
  515. const auto & n_layer = hparams.n_layer;
  516. const auto & n_embd_head_k = hparams.n_embd_head_k;
  517. //const auto & n_embd_head_v = hparams.n_embd_head_v;
  518. const uint32_t n_ctx_per_seq = cparams.n_ctx / cparams.n_seq_max;
  519. //GGML_ASSERT(kv_self->size == n_ctx);
  520. auto inp = std::make_unique<llm_graph_input_k_shift>(this);
  521. inp->k_shift = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, cparams.n_ctx);
  522. ggml_set_input(inp->k_shift);
  523. for (uint32_t il = 0; il < n_layer; ++il) {
  524. const int64_t n_head_kv = hparams.n_head_kv(il);
  525. const int64_t n_embd_k_gqa = hparams.n_embd_k_gqa(il);
  526. const bool is_swa = hparams.is_swa(il);
  527. // note: the swa rope params could become part of the cparams in the future
  528. // if we decide to make them configurable, like the non-sliding ones
  529. const float freq_base_l = is_swa ? hparams.rope_freq_base_train_swa : cparams.rope_freq_base;
  530. const float freq_scale_l = is_swa ? hparams.rope_freq_scale_train_swa : cparams.rope_freq_scale;
  531. ggml_tensor * rope_factors = model.get_rope_factors(n_ctx_per_seq, il);
  532. ggml_tensor * k =
  533. ggml_view_3d(ctx, k_l[il],
  534. n_embd_head_k, n_head_kv, size,
  535. ggml_row_size(k_l[il]->type, n_embd_head_k),
  536. ggml_row_size(k_l[il]->type, n_embd_k_gqa),
  537. 0);
  538. ggml_tensor * cur = build_rope_shift(cparams, ctx, k, inp->k_shift, rope_factors, freq_base_l, freq_scale_l);
  539. ggml_build_forward_expand(gf, cur);
  540. }
  541. res->add_input(std::move(inp));
  542. return res;
  543. }
  544. llm_graph_result_ptr llama_kv_cache_unified::build_graph_defrag(
  545. const llama_cparams & cparams,
  546. ggml_context * ctx,
  547. ggml_cgraph * gf) const {
  548. auto res = std::make_unique<llm_graph_result>();
  549. const auto & ids = defrag_info.ids;
  550. #if 0
  551. // CPU defrag
  552. //
  553. // TODO: optimizations are possible:
  554. // - multiple threads
  555. // - avoid copying to the host memory when already there
  556. //
  557. // likely not worth the effort, as we have ggml_graph based defrag
  558. //
  559. const uint32_t n_embd_k_gqa = hparams.n_embd_k_gqa();
  560. const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa();
  561. const uint32_t kv_size = size;
  562. std::vector<uint8_t> buf_k;
  563. std::vector<uint8_t> buf_v;
  564. for (uint32_t il = 0; il < n_layer; ++il) {
  565. const size_t k_size_row = ggml_row_size(k_l[il]->type, n_embd_k_gqa);
  566. const size_t k_size = ggml_row_size(k_l[il]->type, n_embd_k_gqa*kv_size);
  567. const size_t v_size_el = ggml_type_size(v_l[il]->type);
  568. const size_t v_size = ggml_row_size (v_l[il]->type, n_embd_v_gqa*kv_size);
  569. buf_k.resize(k_size);
  570. buf_v.resize(v_size);
  571. ggml_backend_tensor_get(k_l[il], buf_k.data(), 0, buf_k.size());
  572. ggml_backend_tensor_get(v_l[il], buf_v.data(), 0, buf_v.size());
  573. // batch move [i, i+nm) to [id, id+nm)
  574. // note: cells can move only to a lower index
  575. for (uint32_t i = 0; i < n_kv; ++i) {
  576. const uint32_t id = ids[i];
  577. if (i == id || id == n_kv) {
  578. continue;
  579. }
  580. uint32_t nm = 1;
  581. while (i + nm < n_kv && ids[i + nm] == id + nm) {
  582. nm++;
  583. }
  584. // move keys
  585. {
  586. const int64_t os = i*k_size_row;
  587. const int64_t od = id*k_size_row;
  588. memcpy(buf_k.data() + od, buf_k.data() + os, nm*k_size_row);
  589. }
  590. // move values (note: they are transposed)
  591. {
  592. const int64_t os = i;
  593. const int64_t od = id;
  594. for (uint32_t j = 0; j < n_embd_v_gqa; ++j) {
  595. memcpy(buf_v.data() + (od + j*kv_size)*v_size_el, buf_v.data() + (os + j*kv_size)*v_size_el, nm*v_size_el);
  596. }
  597. }
  598. i += nm - 1;
  599. }
  600. ggml_backend_tensor_set(k_l[il], buf_k.data(), 0, buf_k.size());
  601. ggml_backend_tensor_set(v_l[il], buf_v.data(), 0, buf_v.size());
  602. }
  603. #else
  604. for (uint32_t i = 0; i < ids.size(); ++i) {
  605. const uint32_t id = ids[i];
  606. if (i == id || id == ids.size()) {
  607. continue;
  608. }
  609. uint32_t nm = 1;
  610. while (i + nm < ids.size() && ids[i + nm] == id + nm) {
  611. nm++;
  612. }
  613. for (uint32_t il = 0; il < hparams.n_layer; ++il) { // NOLINT
  614. const int64_t n_embd_k_gqa = hparams.n_embd_k_gqa(il);
  615. const int64_t n_embd_v_gqa = hparams.n_embd_v_gqa(il);
  616. ggml_tensor * view_k_src = ggml_view_2d(ctx, k_l[il],
  617. n_embd_k_gqa, nm,
  618. ggml_row_size(k_l[il]->type, n_embd_k_gqa),
  619. ggml_row_size(k_l[il]->type, n_embd_k_gqa*i));
  620. ggml_tensor * view_k_dst = ggml_view_2d(ctx, k_l[il],
  621. n_embd_k_gqa, nm,
  622. ggml_row_size(k_l[il]->type, n_embd_k_gqa),
  623. ggml_row_size(k_l[il]->type, n_embd_k_gqa*id));
  624. ggml_tensor * view_v_src;
  625. ggml_tensor * view_v_dst;
  626. if (cparams.flash_attn) {
  627. // NOTE: the V cache is not transposed when using flash attention
  628. view_v_src = ggml_view_2d(ctx, v_l[il],
  629. n_embd_v_gqa, nm,
  630. ggml_row_size(v_l[il]->type, n_embd_v_gqa),
  631. ggml_row_size(v_l[il]->type, n_embd_v_gqa*i));
  632. view_v_dst = ggml_view_2d(ctx, v_l[il],
  633. n_embd_v_gqa, nm,
  634. ggml_row_size(v_l[il]->type, n_embd_v_gqa),
  635. ggml_row_size(v_l[il]->type, n_embd_v_gqa*id));
  636. } else {
  637. view_v_src = ggml_view_2d(ctx, v_l[il],
  638. nm, n_embd_v_gqa,
  639. ggml_row_size(v_l[il]->type, size),
  640. ggml_row_size(v_l[il]->type, i));
  641. view_v_dst = ggml_view_2d(ctx, v_l[il],
  642. nm, n_embd_v_gqa,
  643. ggml_row_size(v_l[il]->type, size),
  644. ggml_row_size(v_l[il]->type, id));
  645. }
  646. ggml_build_forward_expand(gf, ggml_cpy(ctx, view_k_src, view_k_dst));
  647. ggml_build_forward_expand(gf, ggml_cpy(ctx, view_v_src, view_v_dst));
  648. }
  649. i += nm - 1;
  650. }
  651. //LLAMA_LOG_INFO("gf->n_nodes = %d\n", gf->n_nodes);
  652. #endif
  653. return res;
  654. }
  655. bool llama_kv_cache_unified::defrag_prepare(int32_t n_max_nodes) {
  656. const uint32_t n_layer = hparams.n_layer;
  657. const uint32_t n_kv = cell_max();
  658. const uint32_t n_used = used;
  659. assert(n_used <= n_kv);
  660. //const int64_t t_start = ggml_time_us();
  661. // number of cells moved
  662. uint32_t n_moves = 0;
  663. // each move requires 6*n_layer tensors (see graph_build_kv_self_defrag)
  664. // - source view, destination view, copy operation
  665. // - x2 for keys and values
  666. //const uint32_t max_moves = max_nodes()/(6*n_layer);
  667. // TODO: tmp fix https://github.com/ggerganov/llama.cpp/issues/6685#issuecomment-2057579516
  668. const uint32_t max_moves = (n_max_nodes - 2*n_layer)/(6*n_layer);
  669. // determine which KV cells to move where
  670. //
  671. // cell i moves to ids[i]
  672. //
  673. // if ids[i] == i || ids[i] == n_kv, then cell i is not moved
  674. //
  675. auto & ids = defrag_info.ids;
  676. ids.clear();
  677. ids.resize(n_kv, n_kv);
  678. for (uint32_t i0 = 0; i0 < n_used; ++i0) {
  679. const auto & cell0 = cells[i0];
  680. if (!cell0.is_empty()) {
  681. ids[i0] = i0;
  682. continue;
  683. }
  684. // found a hole - fill it with data from the end of the cache
  685. uint32_t nh = 1;
  686. // determine the size of the hole
  687. while (i0 + nh < n_used && cells[i0 + nh].is_empty()) {
  688. nh++;
  689. }
  690. uint32_t nf = 0;
  691. uint32_t is = n_kv - 1;
  692. // starting from the end, find nh non-empty cells
  693. for (; is > i0; --is) {
  694. const auto & cell1 = cells[is];
  695. if (cell1.is_empty() || ids[is] != n_kv) {
  696. continue;
  697. }
  698. // non-empty cell which is not yet moved
  699. nf++;
  700. if (nf == nh) {
  701. break;
  702. }
  703. }
  704. // this can only happen if `n_used` is not accurate, which would be a bug
  705. GGML_ASSERT(nf == nh && "KV defrag bug: nf != nh");
  706. nf = 0;
  707. uint32_t i1 = is;
  708. // are we moving a continuous block of memory?
  709. bool cont = false;
  710. // should we stop searching for the next move?
  711. bool stop = false;
  712. // go back and move the nf cells to the hole
  713. for (; i1 < n_kv; ++i1) {
  714. auto & cell1 = cells[i1];
  715. if (cell1.is_empty() || ids[i1] != n_kv) {
  716. if (n_moves == max_moves) {
  717. stop = true;
  718. break;
  719. }
  720. cont = false;
  721. continue;
  722. }
  723. // this cell goes to (i0 + nf)
  724. ids[i1] = i0 + nf;
  725. // move the cell meta data
  726. cells[i0 + nf] = cell1;
  727. // clear the old cell and move the head there
  728. cell1 = kv_cell();
  729. head = n_used;
  730. if (!cont) {
  731. n_moves++;
  732. cont = true;
  733. }
  734. nf++;
  735. if (nf == nh) {
  736. break;
  737. }
  738. }
  739. if (stop || n_moves == max_moves) {
  740. break;
  741. }
  742. //LLAMA_LOG_INFO("(tmp log) KV defrag: move [%u, %u) to [%u, %u)\n", is, i1 + 1, i0, i0 + nh);
  743. i0 += nh - 1;
  744. }
  745. if (n_moves == 0) {
  746. return false;
  747. }
  748. LLAMA_LOG_DEBUG("%s: (tmp log) KV defrag cell moves: %u\n", __func__, n_moves);
  749. LLAMA_LOG_DEBUG("%s: expected gf nodes: %u\n", __func__, 6*n_moves*n_layer);
  750. return true;
  751. }
  752. uint32_t llama_kv_cache_unified::cell_max() const {
  753. for (uint32_t i = size; i > 0; --i) {
  754. const kv_cell & cell = cells[i - 1];
  755. if (cell.pos >= 0 && !cell.is_empty()) {
  756. return i;
  757. }
  758. }
  759. return 0;
  760. }
  761. void llama_kv_cache_unified::state_write(llama_io_write_i & io, llama_seq_id seq_id) const {
  762. std::vector<std::pair<uint32_t, uint32_t>> cell_ranges; // ranges, from inclusive, to exclusive
  763. uint32_t cell_count = 0;
  764. // Count the number of cells with the specified seq_id
  765. // Find all the ranges of cells with this seq id (or all, when -1)
  766. uint32_t cell_range_begin = size;
  767. for (uint32_t i = 0; i < size; ++i) {
  768. const auto & cell = cells[i];
  769. if ((seq_id == -1 && !cell.is_empty()) || cell.has_seq_id(seq_id)) {
  770. ++cell_count;
  771. if (cell_range_begin == size) {
  772. cell_range_begin = i;
  773. }
  774. } else {
  775. if (cell_range_begin != size) {
  776. cell_ranges.emplace_back(cell_range_begin, i);
  777. cell_range_begin = size;
  778. }
  779. }
  780. }
  781. if (cell_range_begin != size) {
  782. cell_ranges.emplace_back(cell_range_begin, size);
  783. }
  784. // DEBUG CHECK: Sum of cell counts in ranges should equal the total cell count
  785. uint32_t cell_count_check = 0;
  786. for (const auto & range : cell_ranges) {
  787. cell_count_check += range.second - range.first;
  788. }
  789. GGML_ASSERT(cell_count == cell_count_check);
  790. io.write(&cell_count, sizeof(cell_count));
  791. state_write_meta(io, cell_ranges, seq_id);
  792. state_write_data(io, cell_ranges);
  793. }
  794. void llama_kv_cache_unified::state_read(llama_io_read_i & io, llama_seq_id seq_id) {
  795. uint32_t cell_count;
  796. io.read_to(&cell_count, sizeof(cell_count));
  797. bool res = true;
  798. res = res && state_read_meta(io, cell_count, seq_id);
  799. res = res && state_read_data(io, cell_count);
  800. if (!res) {
  801. if (seq_id == -1) {
  802. clear();
  803. } else {
  804. seq_rm(seq_id, -1, -1);
  805. }
  806. throw std::runtime_error("failed to restore kv cache");
  807. }
  808. }
  809. void llama_kv_cache_unified::state_write_meta(llama_io_write_i & io, const std::vector<std::pair<uint32_t, uint32_t>> & cell_ranges, llama_seq_id seq_id) const {
  810. for (const auto & range : cell_ranges) {
  811. for (uint32_t i = range.first; i < range.second; ++i) {
  812. const auto & cell = cells[i];
  813. const llama_pos pos = cell.pos;
  814. const uint32_t n_seq_id = seq_id == -1 ? cell.seq_id.size() : 0;
  815. io.write(&pos, sizeof(pos));
  816. io.write(&n_seq_id, sizeof(n_seq_id));
  817. if (n_seq_id) {
  818. for (auto seq_id : cell.seq_id) {
  819. io.write(&seq_id, sizeof(seq_id));
  820. }
  821. }
  822. }
  823. }
  824. }
  825. void llama_kv_cache_unified::state_write_data(llama_io_write_i & io, const std::vector<std::pair<uint32_t, uint32_t>> & cell_ranges) const {
  826. const uint32_t v_trans = this->v_trans ? 1 : 0;
  827. const uint32_t n_layer = hparams.n_layer;
  828. io.write(&v_trans, sizeof(v_trans));
  829. io.write(&n_layer, sizeof(n_layer));
  830. std::vector<uint8_t> tmp_buf;
  831. // Iterate and write all the keys first, each row is a cell
  832. // Get whole range at a time
  833. for (uint32_t il = 0; il < n_layer; ++il) {
  834. const uint32_t n_embd_k_gqa = hparams.n_embd_k_gqa(il) + hparams.n_embd_k_s();
  835. // Write key type
  836. const int32_t k_type_i = (int32_t)k_l[il]->type;
  837. io.write(&k_type_i, sizeof(k_type_i));
  838. // Write row size of key
  839. const uint64_t k_size_row = ggml_row_size(k_l[il]->type, n_embd_k_gqa);
  840. io.write(&k_size_row, sizeof(k_size_row));
  841. // Read each range of cells of k_size length each into tmp_buf and write out
  842. for (const auto & range : cell_ranges) {
  843. const size_t range_size = range.second - range.first;
  844. const size_t buf_size = range_size * k_size_row;
  845. io.write_tensor(k_l[il], range.first * k_size_row, buf_size);
  846. }
  847. }
  848. if (!v_trans) {
  849. for (uint32_t il = 0; il < n_layer; ++il) {
  850. const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(il) + hparams.n_embd_v_s();
  851. // Write value type
  852. const int32_t v_type_i = (int32_t)v_l[il]->type;
  853. io.write(&v_type_i, sizeof(v_type_i));
  854. // Write row size of value
  855. const uint64_t v_size_row = ggml_row_size(v_l[il]->type, n_embd_v_gqa);
  856. io.write(&v_size_row, sizeof(v_size_row));
  857. // Read each range of cells of v_size length each into tmp_buf and write out
  858. for (const auto & range : cell_ranges) {
  859. const size_t range_size = range.second - range.first;
  860. const size_t buf_size = range_size * v_size_row;
  861. io.write_tensor(v_l[il], range.first * v_size_row, buf_size);
  862. }
  863. }
  864. } else {
  865. // When v is transposed, we also need the element size and get the element ranges from each row
  866. const uint32_t kv_size = size;
  867. for (uint32_t il = 0; il < n_layer; ++il) {
  868. const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(il) + hparams.n_embd_v_s();
  869. // Write value type
  870. const int32_t v_type_i = (int32_t)v_l[il]->type;
  871. io.write(&v_type_i, sizeof(v_type_i));
  872. // Write element size
  873. const uint32_t v_size_el = ggml_type_size(v_l[il]->type);
  874. io.write(&v_size_el, sizeof(v_size_el));
  875. // Write GQA embedding size
  876. io.write(&n_embd_v_gqa, sizeof(n_embd_v_gqa));
  877. // For each row, we get the element values of each cell
  878. for (uint32_t j = 0; j < n_embd_v_gqa; ++j) {
  879. // Read each range of cells of v_size_el length each into tmp_buf and write out
  880. for (const auto & range : cell_ranges) {
  881. const size_t range_size = range.second - range.first;
  882. const size_t src_offset = (range.first + j * kv_size) * v_size_el;
  883. const size_t buf_size = range_size * v_size_el;
  884. io.write_tensor(v_l[il], src_offset, buf_size);
  885. }
  886. }
  887. }
  888. }
  889. }
  890. bool llama_kv_cache_unified::state_read_meta(llama_io_read_i & io, uint32_t cell_count, llama_seq_id dest_seq_id) {
  891. if (dest_seq_id != -1) {
  892. // single sequence
  893. seq_rm(dest_seq_id, -1, -1);
  894. llama_sbatch sbatch;
  895. llama_ubatch batch = sbatch.reserve_ubatch(cell_count, /* has_embd */ false);
  896. batch.n_tokens = cell_count;
  897. batch.n_seq_tokens = cell_count;
  898. batch.n_seqs = 1;
  899. for (uint32_t i = 0; i < cell_count; ++i) {
  900. llama_pos pos;
  901. uint32_t n_seq_id;
  902. io.read_to(&pos, sizeof(pos));
  903. io.read_to(&n_seq_id, sizeof(n_seq_id));
  904. if (n_seq_id != 0) {
  905. LLAMA_LOG_ERROR("%s: invalid seq_id-agnostic kv cell\n", __func__);
  906. return false;
  907. }
  908. batch.pos[i] = pos;
  909. }
  910. batch.n_seq_id[0] = 1;
  911. batch.seq_id[0] = &dest_seq_id;
  912. if (!find_slot(batch)) {
  913. LLAMA_LOG_ERROR("%s: failed to find available cells in kv cache\n", __func__);
  914. return false;
  915. }
  916. commit();
  917. // DEBUG CHECK: kv.head should be our first cell, kv.head + cell_count - 1 should be our last cell (verify seq_id and pos values)
  918. // Assume that this is one contiguous block of cells
  919. GGML_ASSERT(head + cell_count <= size);
  920. GGML_ASSERT(cells[head].pos == batch.pos[0]);
  921. GGML_ASSERT(cells[head + cell_count - 1].pos == batch.pos[cell_count - 1]);
  922. GGML_ASSERT(cells[head].has_seq_id(dest_seq_id));
  923. GGML_ASSERT(cells[head + cell_count - 1].has_seq_id(dest_seq_id));
  924. } else {
  925. // whole KV cache restore
  926. if (cell_count > size) {
  927. LLAMA_LOG_ERROR("%s: not enough cells in kv cache\n", __func__);
  928. return false;
  929. }
  930. clear();
  931. for (uint32_t i = 0; i < cell_count; ++i) {
  932. kv_cell & cell = cells[i];
  933. llama_pos pos;
  934. uint32_t n_seq_id;
  935. io.read_to(&pos, sizeof(pos));
  936. io.read_to(&n_seq_id, sizeof(n_seq_id));
  937. cell.pos = pos;
  938. for (uint32_t j = 0; j < n_seq_id; ++j) {
  939. llama_seq_id seq_id;
  940. io.read_to(&seq_id, sizeof(seq_id));
  941. // TODO: llama_kv_cache_unified should have a notion of max sequences
  942. //if (seq_id < 0 || (uint32_t) seq_id >= llama_n_seq_max(ctx)) {
  943. if (seq_id < 0) {
  944. //LLAMA_LOG_ERROR("%s: invalid seq_id, %d is out of range [0, %u)\n", __func__, seq_id, llama_n_seq_max(ctx));
  945. LLAMA_LOG_ERROR("%s: invalid seq_id, %d is out of range [0, inf)\n", __func__, seq_id);
  946. return false;
  947. }
  948. cell.seq_id.insert(seq_id);
  949. }
  950. }
  951. head = 0;
  952. used = cell_count;
  953. }
  954. return true;
  955. }
  956. bool llama_kv_cache_unified::state_read_data(llama_io_read_i & io, uint32_t cell_count) {
  957. uint32_t v_trans;
  958. uint32_t n_layer;
  959. io.read_to(&v_trans, sizeof(v_trans));
  960. io.read_to(&n_layer, sizeof(n_layer));
  961. if (n_layer != hparams.n_layer) {
  962. LLAMA_LOG_ERROR("%s: mismatched layer count (%u instead of %u)\n", __func__, n_layer, hparams.n_layer);
  963. return false;
  964. }
  965. if (cell_count > size) {
  966. LLAMA_LOG_ERROR("%s: not enough cells in kv cache to restore state (%u > %u)\n", __func__, cell_count, size);
  967. return false;
  968. }
  969. if (this->v_trans != (bool) v_trans) {
  970. LLAMA_LOG_ERROR("%s: incompatible V transposition\n", __func__);
  971. return false;
  972. }
  973. // For each layer, read the keys for each cell, one row is one cell, read as one contiguous block
  974. for (uint32_t il = 0; il < n_layer; ++il) {
  975. const uint32_t n_embd_k_gqa = hparams.n_embd_k_gqa(il) + hparams.n_embd_k_s();
  976. // Read type of key
  977. int32_t k_type_i_ref;
  978. io.read_to(&k_type_i_ref, sizeof(k_type_i_ref));
  979. const int32_t k_type_i = (int32_t) k_l[il]->type;
  980. if (k_type_i != k_type_i_ref) {
  981. LLAMA_LOG_ERROR("%s: mismatched key type (%d != %d, layer %d)\n", __func__, k_type_i, k_type_i_ref, il);
  982. return false;
  983. }
  984. // Read row size of key
  985. uint64_t k_size_row_ref;
  986. io.read_to(&k_size_row_ref, sizeof(k_size_row_ref));
  987. const size_t k_size_row = ggml_row_size(k_l[il]->type, n_embd_k_gqa);
  988. if (k_size_row != k_size_row_ref) {
  989. LLAMA_LOG_ERROR("%s: mismatched key row size (%zu != %zu, layer %d)\n", __func__, k_size_row, (size_t) k_size_row_ref, il);
  990. return false;
  991. }
  992. if (cell_count) {
  993. // Read and set the keys for the whole cell range
  994. ggml_backend_tensor_set(k_l[il], io.read(cell_count * k_size_row), head * k_size_row, cell_count * k_size_row);
  995. }
  996. }
  997. if (!this->v_trans) {
  998. for (uint32_t il = 0; il < n_layer; ++il) {
  999. const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(il) + hparams.n_embd_v_s();
  1000. // Read type of value
  1001. int32_t v_type_i_ref;
  1002. io.read_to(&v_type_i_ref, sizeof(v_type_i_ref));
  1003. const int32_t v_type_i = (int32_t)v_l[il]->type;
  1004. if (v_type_i != v_type_i_ref) {
  1005. LLAMA_LOG_ERROR("%s: mismatched value type (%d != %d, layer %d)\n", __func__, v_type_i, v_type_i_ref, il);
  1006. return false;
  1007. }
  1008. // Read row size of value
  1009. uint64_t v_size_row_ref;
  1010. io.read_to(&v_size_row_ref, sizeof(v_size_row_ref));
  1011. const size_t v_size_row = ggml_row_size(v_l[il]->type, n_embd_v_gqa);
  1012. if (v_size_row != v_size_row_ref) {
  1013. LLAMA_LOG_ERROR("%s: mismatched value row size (%zu != %zu, layer %d)\n", __func__, v_size_row, (size_t) v_size_row_ref, il);
  1014. return false;
  1015. }
  1016. if (cell_count) {
  1017. // Read and set the values for the whole cell range
  1018. ggml_backend_tensor_set(v_l[il], io.read(cell_count * v_size_row), head * v_size_row, cell_count * v_size_row);
  1019. }
  1020. }
  1021. } else {
  1022. // For each layer, read the values for each cell (transposed)
  1023. for (uint32_t il = 0; il < n_layer; ++il) {
  1024. const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(il) + hparams.n_embd_v_s();
  1025. // Read type of value
  1026. int32_t v_type_i_ref;
  1027. io.read_to(&v_type_i_ref, sizeof(v_type_i_ref));
  1028. const int32_t v_type_i = (int32_t)v_l[il]->type;
  1029. if (v_type_i != v_type_i_ref) {
  1030. LLAMA_LOG_ERROR("%s: mismatched value type (%d != %d, layer %d)\n", __func__, v_type_i, v_type_i_ref, il);
  1031. return false;
  1032. }
  1033. // Read element size of value
  1034. uint32_t v_size_el_ref;
  1035. io.read_to(&v_size_el_ref, sizeof(v_size_el_ref));
  1036. const size_t v_size_el = ggml_type_size(v_l[il]->type);
  1037. if (v_size_el != v_size_el_ref) {
  1038. LLAMA_LOG_ERROR("%s: mismatched value element size (%zu != %zu, layer %d)\n", __func__, v_size_el, (size_t) v_size_el_ref, il);
  1039. return false;
  1040. }
  1041. // Read GQA embedding size
  1042. uint32_t n_embd_v_gqa_ref;
  1043. io.read_to(&n_embd_v_gqa_ref, sizeof(n_embd_v_gqa_ref));
  1044. if (n_embd_v_gqa != n_embd_v_gqa_ref) {
  1045. LLAMA_LOG_ERROR("%s: mismatched GQA embedding size (%u != %u, layer %d)\n", __func__, n_embd_v_gqa, n_embd_v_gqa_ref, il);
  1046. return false;
  1047. }
  1048. if (cell_count) {
  1049. // For each row in the transposed matrix, read the values for the whole cell range
  1050. for (uint32_t j = 0; j < n_embd_v_gqa; ++j) {
  1051. const size_t dst_offset = (head + j * size) * v_size_el;
  1052. ggml_backend_tensor_set(v_l[il], io.read(cell_count * v_size_el), dst_offset, cell_count * v_size_el);
  1053. }
  1054. }
  1055. }
  1056. }
  1057. return true;
  1058. }
  1059. //
  1060. // llama_kv_cache_recurrent
  1061. //
  1062. llama_kv_cache_recurrent::llama_kv_cache_recurrent(
  1063. const llama_model & model,
  1064. ggml_type type_k,
  1065. ggml_type type_v,
  1066. bool offload,
  1067. uint32_t kv_size) : hparams(model.hparams) {
  1068. const int32_t n_layer = hparams.n_layer;
  1069. LLAMA_LOG_INFO("%s: kv_size = %d, type_k = '%s', type_v = '%s', n_layer = %d\n",
  1070. __func__, kv_size, ggml_type_name(type_k), ggml_type_name(type_v), n_layer);
  1071. head = 0;
  1072. size = kv_size;
  1073. used = 0;
  1074. this->type_k = type_k;
  1075. this->type_v = type_v;
  1076. cells.clear();
  1077. cells.resize(kv_size);
  1078. // create a context for each buffer type
  1079. std::map<ggml_backend_buffer_type_t, ggml_context *> ctx_map;
  1080. auto ctx_for_buft = [&](ggml_backend_buffer_type_t buft) -> ggml_context * {
  1081. auto it = ctx_map.find(buft);
  1082. if (it == ctx_map.end()) {
  1083. ggml_init_params params = {
  1084. /*.mem_size =*/ size_t(2u*n_layer*ggml_tensor_overhead()),
  1085. /*.mem_buffer =*/ NULL,
  1086. /*.no_alloc =*/ true,
  1087. };
  1088. ggml_context * ctx = ggml_init(params);
  1089. if (!ctx) {
  1090. return nullptr;
  1091. }
  1092. ctx_map[buft] = ctx;
  1093. ctxs.emplace_back(ctx);
  1094. return ctx;
  1095. }
  1096. return it->second;
  1097. };
  1098. k_l.reserve(n_layer);
  1099. v_l.reserve(n_layer);
  1100. for (int i = 0; i < n_layer; i++) {
  1101. const uint32_t n_embd_k_gqa = hparams.n_embd_k_gqa(i) + hparams.n_embd_k_s();
  1102. const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(i) + hparams.n_embd_v_s();
  1103. const char * dev_name = "CPU";
  1104. ggml_backend_buffer_type_t buft = ggml_backend_cpu_buffer_type();
  1105. if (offload) {
  1106. auto * dev = model.dev_layer(i);
  1107. buft = ggml_backend_dev_buffer_type(dev);
  1108. dev_name = ggml_backend_dev_name(dev);
  1109. }
  1110. LLAMA_LOG_DEBUG("%s, layer %3d: dev = %s\n", __func__, i, dev_name);
  1111. ggml_context * ctx = ctx_for_buft(buft);
  1112. if (!ctx) {
  1113. throw std::runtime_error("failed to create ggml context for kv cache");
  1114. }
  1115. ggml_tensor * k = ggml_new_tensor_1d(ctx, type_k, n_embd_k_gqa*kv_size);
  1116. ggml_tensor * v = ggml_new_tensor_1d(ctx, type_v, n_embd_v_gqa*kv_size);
  1117. ggml_format_name(k, "cache_k_l%d", i);
  1118. ggml_format_name(v, "cache_v_l%d", i);
  1119. k_l.push_back(k);
  1120. v_l.push_back(v);
  1121. }
  1122. // allocate tensors and initialize the buffers to avoid NaNs in the padding
  1123. for (auto it : ctx_map) {
  1124. auto * buft = it.first;
  1125. auto * ctx = it.second;
  1126. ggml_backend_buffer_t buf = ggml_backend_alloc_ctx_tensors_from_buft(ctx, buft);
  1127. if (!buf) {
  1128. throw std::runtime_error("failed to allocate buffer for kv cache");
  1129. }
  1130. ggml_backend_buffer_clear(buf, 0);
  1131. LLAMA_LOG_INFO("%s: %10s KV buffer size = %8.2f MiB\n", __func__, ggml_backend_buffer_name(buf), ggml_backend_buffer_get_size(buf)/1024.0/1024.0);
  1132. bufs.emplace_back(buf);
  1133. }
  1134. {
  1135. const size_t memory_size_k = size_k_bytes();
  1136. const size_t memory_size_v = size_v_bytes();
  1137. LLAMA_LOG_INFO("%s: KV self size = %7.2f MiB, K (%s): %7.2f MiB, V (%s): %7.2f MiB\n", __func__,
  1138. (float)(memory_size_k + memory_size_v) / (1024.0f * 1024.0f),
  1139. ggml_type_name(type_k), (float)memory_size_k / (1024.0f * 1024.0f),
  1140. ggml_type_name(type_v), (float)memory_size_v / (1024.0f * 1024.0f));
  1141. }
  1142. }
  1143. void llama_kv_cache_recurrent::clear() {
  1144. for (int32_t i = 0; i < (int32_t) size; ++i) {
  1145. cells[i].pos = -1;
  1146. cells[i].seq_id.clear();
  1147. cells[i].src = -1;
  1148. cells[i].tail = -1;
  1149. }
  1150. head = 0;
  1151. used = 0;
  1152. for (auto & buf : bufs) {
  1153. ggml_backend_buffer_clear(buf.get(), 0);
  1154. }
  1155. }
  1156. bool llama_kv_cache_recurrent::seq_rm(llama_seq_id seq_id, llama_pos p0, llama_pos p1) {
  1157. uint32_t new_head = size;
  1158. if (p0 < 0) {
  1159. p0 = 0;
  1160. }
  1161. if (p1 < 0) {
  1162. p1 = std::numeric_limits<llama_pos>::max();
  1163. }
  1164. // models like Mamba or RWKV can't have a state partially erased
  1165. if (seq_id >= (int64_t) size) {
  1166. // could be fatal
  1167. return false;
  1168. }
  1169. if (0 <= seq_id) {
  1170. int32_t & tail_id = cells[seq_id].tail;
  1171. if (tail_id >= 0) {
  1172. const kv_cell & cell = cells[tail_id];
  1173. // partial intersection is invalid
  1174. if ((0 < p0 && p0 <= cell.pos) || (0 < p1 && p1 <= cell.pos)) {
  1175. return false;
  1176. }
  1177. // invalidate tails which will be cleared
  1178. if (p0 <= cell.pos && cell.pos < p1) {
  1179. tail_id = -1;
  1180. }
  1181. }
  1182. } else {
  1183. // seq_id is negative, then the range should include everything or nothing
  1184. if (p0 != p1 && (p0 != 0 || p1 != std::numeric_limits<llama_pos>::max())) {
  1185. return false;
  1186. }
  1187. }
  1188. for (uint32_t i = 0; i < size; ++i) {
  1189. if (cells[i].pos >= p0 && cells[i].pos < p1) {
  1190. if (seq_id < 0) {
  1191. cells[i].seq_id.clear();
  1192. } else if (cells[i].has_seq_id(seq_id)) {
  1193. cells[i].seq_id.erase(seq_id);
  1194. } else {
  1195. continue;
  1196. }
  1197. if (cells[i].is_empty()) {
  1198. // keep count of the number of used cells
  1199. if (cells[i].pos >= 0) {
  1200. used--;
  1201. }
  1202. cells[i].pos = -1;
  1203. cells[i].src = -1;
  1204. if (new_head == size) {
  1205. new_head = i;
  1206. }
  1207. }
  1208. }
  1209. }
  1210. // If we freed up a slot, set head to it so searching can start there.
  1211. if (new_head != size && new_head < head) {
  1212. head = new_head;
  1213. }
  1214. return true;
  1215. }
  1216. void llama_kv_cache_recurrent::seq_cp(llama_seq_id seq_id_src, llama_seq_id seq_id_dst, llama_pos p0, llama_pos p1) {
  1217. if (seq_id_src == seq_id_dst) {
  1218. return;
  1219. }
  1220. if (p0 < 0) {
  1221. p0 = 0;
  1222. }
  1223. if (p1 < 0) {
  1224. p1 = std::numeric_limits<llama_pos>::max();
  1225. }
  1226. if ((uint32_t) seq_id_dst < size && (uint32_t) seq_id_src < size) {
  1227. kv_cell & tail_src = cells[seq_id_src];
  1228. kv_cell & tail_dst = cells[seq_id_dst];
  1229. if (tail_dst.tail >= 0) {
  1230. // clear destination seq_id if it wasn't empty
  1231. kv_cell & cell_dst = cells[tail_dst.tail];
  1232. cell_dst.seq_id.erase(seq_id_dst);
  1233. tail_dst.tail = -1;
  1234. if (cell_dst.seq_id.empty()) {
  1235. cell_dst.pos = -1;
  1236. cell_dst.src = -1;
  1237. used -= 1;
  1238. }
  1239. }
  1240. if (tail_src.tail >= 0) {
  1241. kv_cell & cell_src = cells[tail_src.tail];
  1242. cell_src.seq_id.insert(seq_id_dst);
  1243. tail_dst.tail = tail_src.tail;
  1244. }
  1245. }
  1246. }
  1247. void llama_kv_cache_recurrent::seq_keep(llama_seq_id seq_id) {
  1248. uint32_t new_head = size;
  1249. for (uint32_t i = 0; i < size; ++i) {
  1250. if ((llama_seq_id) i != seq_id) {
  1251. cells[i].tail = -1;
  1252. }
  1253. if (!cells[i].has_seq_id(seq_id)) {
  1254. if (cells[i].pos >= 0) {
  1255. used--;
  1256. }
  1257. cells[i].pos = -1;
  1258. cells[i].src = -1;
  1259. cells[i].seq_id.clear();
  1260. if (new_head == size){
  1261. new_head = i;
  1262. }
  1263. } else {
  1264. cells[i].seq_id.clear();
  1265. cells[i].seq_id.insert(seq_id);
  1266. }
  1267. }
  1268. // If we freed up a slot, set head to it so searching can start there.
  1269. if (new_head != size && new_head < head) {
  1270. head = new_head;
  1271. }
  1272. }
  1273. void llama_kv_cache_recurrent::seq_add(llama_seq_id seq_id, llama_pos p0, llama_pos p1, llama_pos delta) {
  1274. if (delta == 0) {
  1275. return;
  1276. }
  1277. if (p0 < 0) {
  1278. p0 = 0;
  1279. }
  1280. if (p1 < 0) {
  1281. p1 = std::numeric_limits<llama_pos>::max();
  1282. }
  1283. // If there is no range then return early to avoid looping over the
  1284. if (p0 == p1) {
  1285. return;
  1286. }
  1287. // for Mamba-like or RWKV models, only the pos needs to be shifted
  1288. if (0 <= seq_id && seq_id < (int64_t) size) {
  1289. const int32_t tail_id = cells[seq_id].tail;
  1290. if (tail_id >= 0) {
  1291. kv_cell & cell = cells[tail_id];
  1292. if (cell.has_seq_id(seq_id) && p0 <= cell.pos && cell.pos < p1) {
  1293. cell.pos += delta;
  1294. }
  1295. }
  1296. }
  1297. }
  1298. void llama_kv_cache_recurrent::seq_div(llama_seq_id seq_id, llama_pos p0, llama_pos p1, int d) {
  1299. if (d == 1) {
  1300. return;
  1301. }
  1302. if (p0 < 0) {
  1303. p0 = 0;
  1304. }
  1305. if (p1 < 0) {
  1306. p1 = std::numeric_limits<llama_pos>::max();
  1307. }
  1308. // If there is no range then return early to avoid looping over the cache.
  1309. if (p0 == p1) {
  1310. return;
  1311. }
  1312. // for Mamba-like or RWKV models, only the pos needs to be changed
  1313. if (0 <= seq_id && seq_id < (int64_t) size) {
  1314. const int32_t tail_id = cells[seq_id].tail;
  1315. if (tail_id >= 0) {
  1316. kv_cell & cell = cells[tail_id];
  1317. if (cell.has_seq_id(seq_id) && p0 <= cell.pos && cell.pos < p1) {
  1318. cell.pos /= d;
  1319. }
  1320. }
  1321. }
  1322. }
  1323. llama_pos llama_kv_cache_recurrent::seq_pos_max(llama_seq_id seq_id) const {
  1324. llama_pos result = 0;
  1325. for (uint32_t i = 0; i < size; ++i) {
  1326. if (cells[i].has_seq_id(seq_id)) {
  1327. result = std::max(result, cells[i].pos);
  1328. }
  1329. }
  1330. return result;
  1331. }
  1332. void llama_kv_cache_recurrent::restore() {
  1333. if (pending.ranges.empty()) {
  1334. return;
  1335. }
  1336. seq_rm(-1, -1, -1);
  1337. }
  1338. void llama_kv_cache_recurrent::commit() {
  1339. pending.ranges.clear();
  1340. }
  1341. bool llama_kv_cache_recurrent::update(llama_context & lctx) {
  1342. GGML_UNUSED(lctx);
  1343. return false;
  1344. }
  1345. void llama_kv_cache_recurrent::defrag_sched(float thold) {
  1346. GGML_UNUSED(thold);
  1347. // noop
  1348. }
  1349. void llama_kv_cache_recurrent::set_full() {
  1350. n = size;
  1351. head = 0;
  1352. }
  1353. llama_sbatch llama_kv_cache_recurrent::sbatch_init(
  1354. const llama_batch & batch,
  1355. bool logits_all) {
  1356. return llama_sbatch(batch, hparams.n_embd, false, logits_all);
  1357. }
  1358. llama_ubatch llama_kv_cache_recurrent::ubatch_next(llama_sbatch & sbatch, uint32_t n_ubatch, bool embd_pooled) const {
  1359. if (embd_pooled) {
  1360. // Pooled embeddings cannot be split across ubatches (yet)
  1361. return sbatch.split_seq(n_ubatch);
  1362. }
  1363. return sbatch.split_equal(n_ubatch);
  1364. }
  1365. bool llama_kv_cache_recurrent::find_slot(
  1366. const llama_ubatch & ubatch) {
  1367. const uint32_t n_tokens = ubatch.n_tokens;
  1368. const uint32_t n_seqs = ubatch.n_seqs;
  1369. const uint32_t n_seq_tokens = ubatch.n_seq_tokens;
  1370. // if we have enough unused cells before the current head ->
  1371. // better to start searching from the beginning of the cache, hoping to fill it
  1372. if (head > used + 2*n_tokens) {
  1373. head = 0;
  1374. }
  1375. // For recurrent state architectures (like Mamba or RWKV),
  1376. // each cache cell can store the state for a whole sequence.
  1377. // A slot should be always be contiguous.
  1378. // can only process batches with an equal number of new tokens in each sequence
  1379. GGML_ASSERT(ubatch.equal_seqs);
  1380. int32_t min = size - 1;
  1381. int32_t max = 0;
  1382. // everything should fit if all seq_ids are smaller than the max
  1383. for (uint32_t s = 0; s < n_seqs; ++s) {
  1384. const uint32_t n_seq_id = ubatch.n_seq_id[s];
  1385. for (uint32_t j = 0; j < n_seq_id; ++j) {
  1386. const llama_seq_id seq_id = ubatch.seq_id[s][j];
  1387. if (seq_id < 0 || (uint32_t) seq_id >= size) {
  1388. // too big seq_id
  1389. // TODO: would it be possible to resize the cache instead?
  1390. LLAMA_LOG_ERROR("%s: seq_id=%d >= n_seq_max=%d Try using a bigger --parallel value\n", __func__, seq_id, size);
  1391. return false;
  1392. }
  1393. if (j > 0) {
  1394. kv_cell & seq = cells[seq_id];
  1395. if (seq.tail >= 0) {
  1396. kv_cell & cell = cells[seq.tail];
  1397. // clear cells from seq_ids that become shared
  1398. // (should not normally happen, but let's handle it anyway)
  1399. cell.seq_id.erase(seq_id);
  1400. seq.tail = -1;
  1401. if (cell.seq_id.empty()) {
  1402. cell.pos = -1;
  1403. cell.src = -1;
  1404. used -= 1;
  1405. }
  1406. }
  1407. }
  1408. }
  1409. }
  1410. #ifndef NDEBUG
  1411. {
  1412. std::vector<int32_t> tails_verif;
  1413. tails_verif.assign(size, -1);
  1414. for (uint32_t i = 0; i < size; ++i) {
  1415. kv_cell & cell = cells[i];
  1416. for (llama_seq_id seq_id : cell.seq_id) {
  1417. if (tails_verif[seq_id] != -1) {
  1418. LLAMA_LOG_ERROR("%s: duplicate tail for seq_id %d in cell %d and %d\n", __func__, seq_id, i, tails_verif[seq_id]);
  1419. }
  1420. tails_verif[seq_id] = i;
  1421. }
  1422. }
  1423. for (uint32_t i = 0; i < size; ++i) {
  1424. if (tails_verif[i] != cells[i].tail) {
  1425. LLAMA_LOG_ERROR("%s: wrong tail for seq_id %d, (%d instead of %d)\n", __func__, i, cells[i].tail, tails_verif[i]);
  1426. }
  1427. }
  1428. }
  1429. #endif
  1430. // find next empty cell
  1431. uint32_t next_empty_cell = head;
  1432. for (uint32_t i = 0; i < size; ++i) {
  1433. if (next_empty_cell >= size) { next_empty_cell -= size; }
  1434. kv_cell & cell = cells[next_empty_cell];
  1435. if (cell.is_empty()) { break; }
  1436. next_empty_cell += 1;
  1437. }
  1438. // find usable cell range
  1439. for (uint32_t s = 0; s < n_seqs; ++s) {
  1440. const llama_seq_id seq_id = ubatch.seq_id[s][0];
  1441. kv_cell & seq_meta = cells[seq_id];
  1442. bool has_cell = false;
  1443. if (seq_meta.tail >= 0) {
  1444. kv_cell & cell = cells[seq_meta.tail];
  1445. GGML_ASSERT(cell.has_seq_id(seq_id));
  1446. // does this seq_id "own" the cell?
  1447. if (cell.seq_id.size() == 1) { has_cell = true; }
  1448. }
  1449. if (!has_cell) {
  1450. kv_cell & empty_cell = cells[next_empty_cell];
  1451. GGML_ASSERT(empty_cell.is_empty());
  1452. // copy old tail into the empty cell
  1453. if (seq_meta.tail >= 0) {
  1454. kv_cell & orig_cell = cells[seq_meta.tail];
  1455. empty_cell.pos = orig_cell.pos;
  1456. empty_cell.src = orig_cell.src;
  1457. orig_cell.seq_id.erase(seq_id);
  1458. empty_cell.seq_id.insert(seq_id); // will be overwritten
  1459. }
  1460. seq_meta.tail = next_empty_cell;
  1461. // find next empty cell
  1462. if (s + 1 < n_seqs) {
  1463. next_empty_cell += 1;
  1464. for (uint32_t i = 0; i < size; ++i) {
  1465. if (next_empty_cell >= size) { next_empty_cell -= size; }
  1466. kv_cell & cell = cells[next_empty_cell];
  1467. if (cell.is_empty()) { break; }
  1468. next_empty_cell += 1;
  1469. }
  1470. }
  1471. }
  1472. if (min > seq_meta.tail) { min = seq_meta.tail; }
  1473. if (max < seq_meta.tail) { max = seq_meta.tail; }
  1474. }
  1475. // gather and re-order
  1476. for (uint32_t s = 0; s < n_seqs; ++s) {
  1477. int32_t dst_id = s + min;
  1478. int32_t src_id = cells[ubatch.seq_id[s][0]].tail;
  1479. if (dst_id != src_id) {
  1480. kv_cell & dst_cell = cells[dst_id];
  1481. kv_cell & src_cell = cells[src_id];
  1482. std::swap(dst_cell.pos, src_cell.pos);
  1483. std::swap(dst_cell.src, src_cell.src);
  1484. std::swap(dst_cell.seq_id, src_cell.seq_id);
  1485. // swap tails (assuming they NEVER overlap)
  1486. for (const llama_seq_id seq_id : src_cell.seq_id) {
  1487. cells[seq_id].tail = src_id;
  1488. }
  1489. for (const llama_seq_id seq_id : dst_cell.seq_id) {
  1490. cells[seq_id].tail = dst_id;
  1491. }
  1492. }
  1493. }
  1494. // update the pos of the used seqs
  1495. for (uint32_t s = 0; s < n_seqs; ++s) {
  1496. const llama_pos last_pos = ubatch.pos[n_seq_tokens * s + n_seq_tokens - 1];
  1497. int32_t cell_id = s + min;
  1498. kv_cell & cell = cells[cell_id];
  1499. if (cell.pos >= 0 && last_pos != cell.pos + (llama_pos) n_seq_tokens) {
  1500. // What should happen when the pos backtracks or skips a value?
  1501. // Clearing the state mid-batch would require special-casing which isn't done.
  1502. LLAMA_LOG_WARN("%s: non-consecutive token position %d after %d for sequence %d with %u new tokens\n",
  1503. __func__, last_pos, cell.pos, ubatch.seq_id[s][0], n_seq_tokens);
  1504. }
  1505. cell.pos = last_pos;
  1506. cell.seq_id.clear();
  1507. for (int32_t j = 0; j < ubatch.n_seq_id[s]; ++j) {
  1508. const llama_seq_id seq_id = ubatch.seq_id[s][j];
  1509. cell.seq_id.insert(seq_id);
  1510. cells[seq_id].tail = cell_id;
  1511. }
  1512. }
  1513. // allow getting the range of used cells, from head to head + n
  1514. head = min;
  1515. n = max - min + 1;
  1516. used = std::count_if(cells.begin(), cells.end(),
  1517. [](const kv_cell & cell){ return !cell.is_empty(); });
  1518. // sanity check
  1519. return n >= n_seqs;
  1520. }
  1521. int32_t llama_kv_cache_recurrent::get_n_tokens() const {
  1522. int32_t result = 0;
  1523. for (uint32_t i = 0; i < size; i++) {
  1524. result += cells[i].seq_id.size();
  1525. }
  1526. return result;
  1527. }
  1528. int32_t llama_kv_cache_recurrent::get_used_cells() const {
  1529. return used;
  1530. }
  1531. llama_pos llama_kv_cache_recurrent::get_pos_max() const {
  1532. llama_pos pos_max = -1;
  1533. for (const auto & cell : cells) {
  1534. pos_max = std::max(pos_max, cell.pos);
  1535. }
  1536. return pos_max;
  1537. }
  1538. bool llama_kv_cache_recurrent::get_can_shift() const {
  1539. return false;
  1540. }
  1541. int32_t llama_kv_cache_recurrent::s_copy(int i) const {
  1542. const uint32_t cell_id = i + head;
  1543. //////////////////////////////////////////////
  1544. // TODO: this should not mutate the KV cache !
  1545. kv_cell & cell = const_cast<kv_cell &>(cells[cell_id]);
  1546. // prevent out-of-bound sources
  1547. if (cell.src < 0 || (uint32_t) cell.src >= size) {
  1548. cell.src = cell_id;
  1549. }
  1550. int32_t res = cell.src;
  1551. // TODO: do not mutate the KV cache
  1552. // ensure copy only happens once
  1553. if (cell.src != (int32_t) cell_id) {
  1554. cell.src = cell_id;
  1555. }
  1556. return res;
  1557. }
  1558. float llama_kv_cache_recurrent::s_mask(int i) const {
  1559. const uint32_t cell_id = i + head;
  1560. //////////////////////////////////////////////
  1561. // TODO: this should not mutate the KV cache !
  1562. kv_cell & cell = const_cast<kv_cell &>(cells[cell_id]);
  1563. float res = (float) (cell.src >= 0);
  1564. // only clear once
  1565. if (cell.src < 0) {
  1566. cell.src = cell_id;
  1567. }
  1568. return res;
  1569. }
  1570. uint32_t llama_kv_cache_recurrent::cell_max() const {
  1571. for (uint32_t i = size; i > 0; --i) {
  1572. const kv_cell & cell = cells[i - 1];
  1573. if (cell.pos >= 0 && !cell.is_empty()) {
  1574. return i;
  1575. }
  1576. }
  1577. return 0;
  1578. }
  1579. size_t llama_kv_cache_recurrent::total_size() const {
  1580. size_t size = 0;
  1581. for (const auto & buf : bufs) {
  1582. size += ggml_backend_buffer_get_size(buf.get());
  1583. }
  1584. return size;
  1585. }
  1586. size_t llama_kv_cache_recurrent::size_k_bytes() const {
  1587. size_t size_k_bytes = 0;
  1588. for (const auto & k : k_l) {
  1589. size_k_bytes += ggml_nbytes(k);
  1590. }
  1591. return size_k_bytes;
  1592. }
  1593. size_t llama_kv_cache_recurrent::size_v_bytes() const {
  1594. size_t size_v_bytes = 0;
  1595. for (const auto & v : v_l) {
  1596. size_v_bytes += ggml_nbytes(v);
  1597. }
  1598. return size_v_bytes;
  1599. }
  1600. void llama_kv_cache_recurrent::state_write(llama_io_write_i & io, llama_seq_id seq_id) const {
  1601. std::vector<std::pair<uint32_t, uint32_t>> cell_ranges; // ranges, from inclusive, to exclusive
  1602. uint32_t cell_count = 0;
  1603. // Count the number of cells with the specified seq_id
  1604. // Find all the ranges of cells with this seq id (or all, when -1)
  1605. uint32_t cell_range_begin = size;
  1606. for (uint32_t i = 0; i < size; ++i) {
  1607. const auto & cell = cells[i];
  1608. if ((seq_id == -1 && !cell.is_empty()) || cell.has_seq_id(seq_id)) {
  1609. ++cell_count;
  1610. if (cell_range_begin == size) {
  1611. cell_range_begin = i;
  1612. }
  1613. } else {
  1614. if (cell_range_begin != size) {
  1615. cell_ranges.emplace_back(cell_range_begin, i);
  1616. cell_range_begin = size;
  1617. }
  1618. }
  1619. }
  1620. if (cell_range_begin != size) {
  1621. cell_ranges.emplace_back(cell_range_begin, size);
  1622. }
  1623. // DEBUG CHECK: Sum of cell counts in ranges should equal the total cell count
  1624. uint32_t cell_count_check = 0;
  1625. for (const auto & range : cell_ranges) {
  1626. cell_count_check += range.second - range.first;
  1627. }
  1628. GGML_ASSERT(cell_count == cell_count_check);
  1629. io.write(&cell_count, sizeof(cell_count));
  1630. state_write_meta(io, cell_ranges, seq_id);
  1631. state_write_data(io, cell_ranges);
  1632. }
  1633. void llama_kv_cache_recurrent::state_read(llama_io_read_i & io, llama_seq_id seq_id) {
  1634. uint32_t cell_count;
  1635. io.read_to(&cell_count, sizeof(cell_count));
  1636. bool res = true;
  1637. res = res && state_read_meta(io, cell_count, seq_id);
  1638. res = res && state_read_data(io, cell_count);
  1639. if (!res) {
  1640. if (seq_id == -1) {
  1641. clear();
  1642. } else {
  1643. seq_rm(seq_id, -1, -1);
  1644. }
  1645. throw std::runtime_error("failed to restore kv cache");
  1646. }
  1647. }
  1648. void llama_kv_cache_recurrent::state_write_meta(llama_io_write_i & io, const std::vector<std::pair<uint32_t, uint32_t>> & cell_ranges, llama_seq_id seq_id) const {
  1649. for (const auto & range : cell_ranges) {
  1650. for (uint32_t i = range.first; i < range.second; ++i) {
  1651. const auto & cell = cells[i];
  1652. const llama_pos pos = cell.pos;
  1653. const uint32_t n_seq_id = seq_id == -1 ? cell.seq_id.size() : 0;
  1654. io.write(&pos, sizeof(pos));
  1655. io.write(&n_seq_id, sizeof(n_seq_id));
  1656. if (n_seq_id) {
  1657. for (auto seq_id : cell.seq_id) {
  1658. io.write(&seq_id, sizeof(seq_id));
  1659. }
  1660. }
  1661. }
  1662. }
  1663. }
  1664. void llama_kv_cache_recurrent::state_write_data(llama_io_write_i & io, const std::vector<std::pair<uint32_t, uint32_t>> & cell_ranges) const {
  1665. const uint32_t v_trans = 0;
  1666. const uint32_t n_layer = hparams.n_layer;
  1667. io.write(&v_trans, sizeof(v_trans));
  1668. io.write(&n_layer, sizeof(n_layer));
  1669. std::vector<uint8_t> tmp_buf;
  1670. // Iterate and write all the keys first, each row is a cell
  1671. // Get whole range at a time
  1672. for (uint32_t il = 0; il < n_layer; ++il) {
  1673. const uint32_t n_embd_k_gqa = hparams.n_embd_k_gqa(il) + hparams.n_embd_k_s();
  1674. // Write key type
  1675. const int32_t k_type_i = (int32_t)k_l[il]->type;
  1676. io.write(&k_type_i, sizeof(k_type_i));
  1677. // Write row size of key
  1678. const uint64_t k_size_row = ggml_row_size(k_l[il]->type, n_embd_k_gqa);
  1679. io.write(&k_size_row, sizeof(k_size_row));
  1680. // Read each range of cells of k_size length each into tmp_buf and write out
  1681. for (const auto & range : cell_ranges) {
  1682. const size_t range_size = range.second - range.first;
  1683. const size_t buf_size = range_size * k_size_row;
  1684. io.write_tensor(k_l[il], range.first * k_size_row, buf_size);
  1685. }
  1686. }
  1687. if (!v_trans) {
  1688. for (uint32_t il = 0; il < n_layer; ++il) {
  1689. const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(il) + hparams.n_embd_v_s();
  1690. // Write value type
  1691. const int32_t v_type_i = (int32_t)v_l[il]->type;
  1692. io.write(&v_type_i, sizeof(v_type_i));
  1693. // Write row size of value
  1694. const uint64_t v_size_row = ggml_row_size(v_l[il]->type, n_embd_v_gqa);
  1695. io.write(&v_size_row, sizeof(v_size_row));
  1696. // Read each range of cells of v_size length each into tmp_buf and write out
  1697. for (const auto & range : cell_ranges) {
  1698. const size_t range_size = range.second - range.first;
  1699. const size_t buf_size = range_size * v_size_row;
  1700. io.write_tensor(v_l[il], range.first * v_size_row, buf_size);
  1701. }
  1702. }
  1703. } else {
  1704. // When v is transposed, we also need the element size and get the element ranges from each row
  1705. const uint32_t kv_size = size;
  1706. for (uint32_t il = 0; il < n_layer; ++il) {
  1707. const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(il) + hparams.n_embd_v_s();
  1708. // Write value type
  1709. const int32_t v_type_i = (int32_t)v_l[il]->type;
  1710. io.write(&v_type_i, sizeof(v_type_i));
  1711. // Write element size
  1712. const uint32_t v_size_el = ggml_type_size(v_l[il]->type);
  1713. io.write(&v_size_el, sizeof(v_size_el));
  1714. // Write GQA embedding size
  1715. io.write(&n_embd_v_gqa, sizeof(n_embd_v_gqa));
  1716. // For each row, we get the element values of each cell
  1717. for (uint32_t j = 0; j < n_embd_v_gqa; ++j) {
  1718. // Read each range of cells of v_size_el length each into tmp_buf and write out
  1719. for (const auto & range : cell_ranges) {
  1720. const size_t range_size = range.second - range.first;
  1721. const size_t src_offset = (range.first + j * kv_size) * v_size_el;
  1722. const size_t buf_size = range_size * v_size_el;
  1723. io.write_tensor(v_l[il], src_offset, buf_size);
  1724. }
  1725. }
  1726. }
  1727. }
  1728. }
  1729. bool llama_kv_cache_recurrent::state_read_meta(llama_io_read_i & io, uint32_t cell_count, llama_seq_id dest_seq_id) {
  1730. if (dest_seq_id != -1) {
  1731. // single sequence
  1732. seq_rm(dest_seq_id, -1, -1);
  1733. llama_sbatch sbatch;
  1734. llama_ubatch batch = sbatch.reserve_ubatch(cell_count, /* has_embd */ false);
  1735. batch.n_tokens = cell_count;
  1736. batch.n_seq_tokens = cell_count;
  1737. batch.n_seqs = 1;
  1738. for (uint32_t i = 0; i < cell_count; ++i) {
  1739. llama_pos pos;
  1740. uint32_t n_seq_id;
  1741. io.read_to(&pos, sizeof(pos));
  1742. io.read_to(&n_seq_id, sizeof(n_seq_id));
  1743. if (n_seq_id != 0) {
  1744. LLAMA_LOG_ERROR("%s: invalid seq_id-agnostic kv cell\n", __func__);
  1745. return false;
  1746. }
  1747. batch.pos[i] = pos;
  1748. }
  1749. batch.n_seq_id[0] = 1;
  1750. batch.seq_id[0] = &dest_seq_id;
  1751. if (!find_slot(batch)) {
  1752. LLAMA_LOG_ERROR("%s: failed to find available cells in kv cache\n", __func__);
  1753. return false;
  1754. }
  1755. commit();
  1756. // DEBUG CHECK: kv.head should be our first cell, kv.head + cell_count - 1 should be our last cell (verify seq_id and pos values)
  1757. // Assume that this is one contiguous block of cells
  1758. GGML_ASSERT(head + cell_count <= size);
  1759. GGML_ASSERT(cells[head].pos == batch.pos[0]);
  1760. GGML_ASSERT(cells[head + cell_count - 1].pos == batch.pos[cell_count - 1]);
  1761. GGML_ASSERT(cells[head].has_seq_id(dest_seq_id));
  1762. GGML_ASSERT(cells[head + cell_count - 1].has_seq_id(dest_seq_id));
  1763. } else {
  1764. // whole KV cache restore
  1765. if (cell_count > size) {
  1766. LLAMA_LOG_ERROR("%s: not enough cells in kv cache\n", __func__);
  1767. return false;
  1768. }
  1769. clear();
  1770. for (uint32_t i = 0; i < cell_count; ++i) {
  1771. kv_cell & cell = cells[i];
  1772. llama_pos pos;
  1773. uint32_t n_seq_id;
  1774. io.read_to(&pos, sizeof(pos));
  1775. io.read_to(&n_seq_id, sizeof(n_seq_id));
  1776. cell.pos = pos;
  1777. for (uint32_t j = 0; j < n_seq_id; ++j) {
  1778. llama_seq_id seq_id;
  1779. io.read_to(&seq_id, sizeof(seq_id));
  1780. // TODO: llama_kv_cache_recurrent should have a notion of max sequences
  1781. //if (seq_id < 0 || (uint32_t) seq_id >= llama_n_seq_max(ctx)) {
  1782. if (seq_id < 0) {
  1783. //LLAMA_LOG_ERROR("%s: invalid seq_id, %d is out of range [0, %u)\n", __func__, seq_id, llama_n_seq_max(ctx));
  1784. LLAMA_LOG_ERROR("%s: invalid seq_id, %d is out of range [0, inf)\n", __func__, seq_id);
  1785. return false;
  1786. }
  1787. cell.seq_id.insert(seq_id);
  1788. int32_t & tail = cells[seq_id].tail;
  1789. if (tail != -1) {
  1790. LLAMA_LOG_ERROR("%s: duplicate tail for seq_id %d in cell %d and %d\n", __func__, seq_id, i, tail);
  1791. return false;
  1792. }
  1793. tail = i;
  1794. }
  1795. }
  1796. head = 0;
  1797. used = cell_count;
  1798. }
  1799. for (uint32_t i = 0; i < cell_count; ++i) {
  1800. uint32_t cell_id = head + i;
  1801. // make sure the recurrent states will keep their restored state
  1802. cells[cell_id].src = cell_id;
  1803. }
  1804. return true;
  1805. }
  1806. bool llama_kv_cache_recurrent::state_read_data(llama_io_read_i & io, uint32_t cell_count) {
  1807. uint32_t v_trans;
  1808. uint32_t n_layer;
  1809. io.read_to(&v_trans, sizeof(v_trans));
  1810. io.read_to(&n_layer, sizeof(n_layer));
  1811. if (n_layer != hparams.n_layer) {
  1812. LLAMA_LOG_ERROR("%s: mismatched layer count (%u instead of %u)\n", __func__, n_layer, hparams.n_layer);
  1813. return false;
  1814. }
  1815. if (cell_count > size) {
  1816. LLAMA_LOG_ERROR("%s: not enough cells in kv cache to restore state (%u > %u)\n", __func__, cell_count, size);
  1817. return false;
  1818. }
  1819. if (false != (bool) v_trans) {
  1820. LLAMA_LOG_ERROR("%s: incompatible V transposition\n", __func__);
  1821. return false;
  1822. }
  1823. // For each layer, read the keys for each cell, one row is one cell, read as one contiguous block
  1824. for (uint32_t il = 0; il < n_layer; ++il) {
  1825. const uint32_t n_embd_k_gqa = hparams.n_embd_k_gqa(il) + hparams.n_embd_k_s();
  1826. // Read type of key
  1827. int32_t k_type_i_ref;
  1828. io.read_to(&k_type_i_ref, sizeof(k_type_i_ref));
  1829. const int32_t k_type_i = (int32_t) k_l[il]->type;
  1830. if (k_type_i != k_type_i_ref) {
  1831. LLAMA_LOG_ERROR("%s: mismatched key type (%d != %d, layer %d)\n", __func__, k_type_i, k_type_i_ref, il);
  1832. return false;
  1833. }
  1834. // Read row size of key
  1835. uint64_t k_size_row_ref;
  1836. io.read_to(&k_size_row_ref, sizeof(k_size_row_ref));
  1837. const size_t k_size_row = ggml_row_size(k_l[il]->type, n_embd_k_gqa);
  1838. if (k_size_row != k_size_row_ref) {
  1839. LLAMA_LOG_ERROR("%s: mismatched key row size (%zu != %zu, layer %d)\n", __func__, k_size_row, (size_t) k_size_row_ref, il);
  1840. return false;
  1841. }
  1842. if (cell_count) {
  1843. // Read and set the keys for the whole cell range
  1844. ggml_backend_tensor_set(k_l[il], io.read(cell_count * k_size_row), head * k_size_row, cell_count * k_size_row);
  1845. }
  1846. }
  1847. if (!v_trans) {
  1848. for (uint32_t il = 0; il < n_layer; ++il) {
  1849. const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(il) + hparams.n_embd_v_s();
  1850. // Read type of value
  1851. int32_t v_type_i_ref;
  1852. io.read_to(&v_type_i_ref, sizeof(v_type_i_ref));
  1853. const int32_t v_type_i = (int32_t)v_l[il]->type;
  1854. if (v_type_i != v_type_i_ref) {
  1855. LLAMA_LOG_ERROR("%s: mismatched value type (%d != %d, layer %d)\n", __func__, v_type_i, v_type_i_ref, il);
  1856. return false;
  1857. }
  1858. // Read row size of value
  1859. uint64_t v_size_row_ref;
  1860. io.read_to(&v_size_row_ref, sizeof(v_size_row_ref));
  1861. const size_t v_size_row = ggml_row_size(v_l[il]->type, n_embd_v_gqa);
  1862. if (v_size_row != v_size_row_ref) {
  1863. LLAMA_LOG_ERROR("%s: mismatched value row size (%zu != %zu, layer %d)\n", __func__, v_size_row, (size_t) v_size_row_ref, il);
  1864. return false;
  1865. }
  1866. if (cell_count) {
  1867. // Read and set the values for the whole cell range
  1868. ggml_backend_tensor_set(v_l[il], io.read(cell_count * v_size_row), head * v_size_row, cell_count * v_size_row);
  1869. }
  1870. }
  1871. } else {
  1872. // For each layer, read the values for each cell (transposed)
  1873. for (uint32_t il = 0; il < n_layer; ++il) {
  1874. const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(il) + hparams.n_embd_v_s();
  1875. // Read type of value
  1876. int32_t v_type_i_ref;
  1877. io.read_to(&v_type_i_ref, sizeof(v_type_i_ref));
  1878. const int32_t v_type_i = (int32_t)v_l[il]->type;
  1879. if (v_type_i != v_type_i_ref) {
  1880. LLAMA_LOG_ERROR("%s: mismatched value type (%d != %d, layer %d)\n", __func__, v_type_i, v_type_i_ref, il);
  1881. return false;
  1882. }
  1883. // Read element size of value
  1884. uint32_t v_size_el_ref;
  1885. io.read_to(&v_size_el_ref, sizeof(v_size_el_ref));
  1886. const size_t v_size_el = ggml_type_size(v_l[il]->type);
  1887. if (v_size_el != v_size_el_ref) {
  1888. LLAMA_LOG_ERROR("%s: mismatched value element size (%zu != %zu, layer %d)\n", __func__, v_size_el, (size_t) v_size_el_ref, il);
  1889. return false;
  1890. }
  1891. // Read GQA embedding size
  1892. uint32_t n_embd_v_gqa_ref;
  1893. io.read_to(&n_embd_v_gqa_ref, sizeof(n_embd_v_gqa_ref));
  1894. if (n_embd_v_gqa != n_embd_v_gqa_ref) {
  1895. LLAMA_LOG_ERROR("%s: mismatched GQA embedding size (%u != %u, layer %d)\n", __func__, n_embd_v_gqa, n_embd_v_gqa_ref, il);
  1896. return false;
  1897. }
  1898. if (cell_count) {
  1899. // For each row in the transposed matrix, read the values for the whole cell range
  1900. for (uint32_t j = 0; j < n_embd_v_gqa; ++j) {
  1901. const size_t dst_offset = (head + j * size) * v_size_el;
  1902. ggml_backend_tensor_set(v_l[il], io.read(cell_count * v_size_el), dst_offset, cell_count * v_size_el);
  1903. }
  1904. }
  1905. }
  1906. }
  1907. return true;
  1908. }
  1909. //
  1910. // kv cache view
  1911. //
  1912. llama_kv_cache_view llama_kv_cache_view_init(const llama_kv_cache & kv, int32_t n_seq_max) {
  1913. llama_kv_cache_view result = {
  1914. /*.n_cells = */ 0,
  1915. /*.n_seq_max = */ n_seq_max,
  1916. /*.token_count = */ 0,
  1917. /*.used_cells = */ kv.get_used_cells(),
  1918. /*.max_contiguous = */ 0,
  1919. /*.max_contiguous_idx = */ -1,
  1920. /*.cells = */ nullptr,
  1921. /*.cells_sequences = */ nullptr,
  1922. };
  1923. return result;
  1924. }
  1925. void llama_kv_cache_view_free(llama_kv_cache_view * view) {
  1926. if (view->cells != nullptr) {
  1927. free(view->cells);
  1928. view->cells = nullptr;
  1929. }
  1930. if (view->cells_sequences != nullptr) {
  1931. free(view->cells_sequences);
  1932. view->cells_sequences = nullptr;
  1933. }
  1934. }
  1935. void llama_kv_cache_view_update(llama_kv_cache_view * view, const llama_kv_cache * kv) {
  1936. // TODO: rework this in the future, for now quick hack
  1937. const llama_kv_cache_unified * kvu = dynamic_cast<const llama_kv_cache_unified *>(kv);
  1938. if (kvu == nullptr) {
  1939. LLAMA_LOG_ERROR("%s: the kv_cache_view currently works only with llama_kv_cache_unified\n", __func__);
  1940. return;
  1941. }
  1942. if (uint32_t(view->n_cells) < kvu->size || view->cells == nullptr) {
  1943. view->n_cells = int32_t(kvu->size);
  1944. void * p = realloc(view->cells, sizeof(llama_kv_cache_view_cell) * view->n_cells);
  1945. GGML_ASSERT(p != nullptr && "Failed to alloc kv_cache_view cells");
  1946. view->cells = (llama_kv_cache_view_cell *)p;
  1947. p = realloc(view->cells_sequences, sizeof(llama_seq_id) * view->n_seq_max * view->n_cells);
  1948. GGML_ASSERT(p != nullptr && "Failed to alloc kv_cache_view cells sequences");
  1949. view->cells_sequences = (llama_seq_id *)p;
  1950. }
  1951. const std::vector<llama_kv_cache_unified::kv_cell> & kv_cells = kvu->cells;
  1952. llama_kv_cache_view_cell * c_curr = view->cells;
  1953. llama_seq_id * cs_curr = view->cells_sequences;
  1954. int32_t used_cells = 0;
  1955. int32_t token_count = 0;
  1956. int32_t curr_contig_idx = -1;
  1957. uint32_t max_contig = 0;
  1958. int32_t max_contig_idx = -1;
  1959. for (int32_t i = 0; i < int32_t(kvu->size); i++, c_curr++, cs_curr += view->n_seq_max) {
  1960. const size_t curr_size = kv_cells[i].seq_id.size();
  1961. token_count += curr_size;
  1962. c_curr->pos = kv_cells[i].pos + kv_cells[i].delta;
  1963. if (curr_size > 0) {
  1964. if (curr_contig_idx >= 0 && uint32_t(i - curr_contig_idx) > max_contig) {
  1965. max_contig = i - curr_contig_idx;
  1966. max_contig_idx = curr_contig_idx;
  1967. }
  1968. curr_contig_idx = -1;
  1969. } else if (curr_contig_idx < 0) {
  1970. curr_contig_idx = i;
  1971. }
  1972. int seq_idx = 0;
  1973. for (const llama_seq_id it : kv_cells[i].seq_id) {
  1974. if (seq_idx >= view->n_seq_max) {
  1975. break;
  1976. }
  1977. cs_curr[seq_idx] = it;
  1978. seq_idx++;
  1979. }
  1980. if (seq_idx != 0) {
  1981. used_cells++;
  1982. }
  1983. for (; seq_idx < view->n_seq_max; seq_idx++) {
  1984. cs_curr[seq_idx] = -1;
  1985. }
  1986. }
  1987. if (curr_contig_idx >= 0 && kv_cells.size() - curr_contig_idx > max_contig) {
  1988. max_contig_idx = curr_contig_idx;
  1989. max_contig = kv_cells.size() - curr_contig_idx;
  1990. }
  1991. view->max_contiguous = max_contig;
  1992. view->max_contiguous_idx = max_contig_idx;
  1993. view->token_count = token_count;
  1994. view->used_cells = used_cells;
  1995. if (uint32_t(used_cells) != kvu->used) {
  1996. LLAMA_LOG_ERROR("%s: used cells mismatch. kv_cache says %d but we calculated %d\n",
  1997. __func__, kvu->used, used_cells);
  1998. }
  1999. }