llama-kv-cells.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. #pragma once
  2. #include "llama.h"
  3. #include "llama-cparams.h"
  4. #include <bitset>
  5. #include <cassert>
  6. #include <vector>
  7. #include <set>
  8. #include <map>
  9. // meta information about KV cells that can be part of multiple sequences at the same time
  10. // TODO: add unit tests
  11. class llama_kv_cells_unified {
  12. public:
  13. void reset() {
  14. for (uint32_t i = 0; i < pos.size(); ++i) {
  15. pos[i] = -1;
  16. shift[i] = 0;
  17. seq[i].reset();
  18. }
  19. has_shift = false;
  20. used.clear();
  21. for (uint32_t s = 0; s < LLAMA_MAX_SEQ; ++s) {
  22. seq_pos[s].clear();
  23. }
  24. }
  25. void reset_shift() {
  26. has_shift = false;
  27. for (uint32_t i = 0; i < shift.size(); ++i) {
  28. shift[i] = 0;
  29. }
  30. }
  31. uint32_t size() const {
  32. return pos.size();
  33. }
  34. void resize(uint32_t n) {
  35. pos.resize(n);
  36. shift.resize(n);
  37. seq.resize(n);
  38. reset();
  39. }
  40. bool is_empty(uint32_t i) const {
  41. assert(i < pos.size());
  42. assert((pos[i] < 0 && pos[i] == -1) || pos[i] >= 0);
  43. return pos[i] == -1;
  44. }
  45. uint32_t get_used() const {
  46. return used.size();
  47. }
  48. // the index of the first cell that is used
  49. // return 0 if no cells are used
  50. uint32_t used_min() const {
  51. return used.empty() ? 0 : *used.begin();
  52. }
  53. // the index of the last cell that is used + 1
  54. // return 0 if no cells are used
  55. uint32_t used_max_p1() const {
  56. return used.empty() ? 0 : *used.rbegin() + 1;
  57. }
  58. bool get_has_shift() const {
  59. return has_shift;
  60. }
  61. // move cell isrc to idst (used during defrag)
  62. void mv(uint32_t isrc, uint32_t idst) {
  63. assert(isrc < pos.size());
  64. assert(idst < pos.size());
  65. assert(pos[idst] == -1);
  66. assert(pos[isrc] != -1);
  67. pos [idst] = pos [isrc];
  68. shift[idst] = shift[isrc];
  69. seq [idst] = seq [isrc];
  70. pos [isrc] = -1;
  71. shift[isrc] = 0;
  72. seq [isrc].reset();
  73. used.erase (isrc);
  74. used.insert(idst);
  75. }
  76. // copy the state of cells [i, i + n) (used for save/restore the state of the cells)
  77. llama_kv_cells_unified cp(uint32_t i, uint32_t n) const {
  78. assert(i + n <= pos.size());
  79. llama_kv_cells_unified res;
  80. res.resize(n);
  81. for (uint32_t j = 0; j < n; ++j) {
  82. res.pos[j] = pos[i + j];
  83. res.seq[j] = seq[i + j];
  84. assert(shift[i + j] == 0);
  85. }
  86. return res;
  87. }
  88. // set the state of cells [i, i + other.pos.size()) (used for save/restore the state of the cells)
  89. void set(uint32_t i, const llama_kv_cells_unified & other) {
  90. assert(i + other.pos.size() <= pos.size());
  91. for (uint32_t j = 0; j < other.pos.size(); ++j) {
  92. if (pos[i + j] == -1 && other.pos[j] != -1) {
  93. used.insert(i + j);
  94. }
  95. if (pos[i + j] != -1 && other.pos[j] == -1) {
  96. used.erase(i + j);
  97. }
  98. if (pos[i + j] != -1) {
  99. seq_pos_rm(i + j);
  100. }
  101. pos[i + j] = other.pos[j];
  102. seq[i + j] = other.seq[j];
  103. if (pos[i + j] != -1) {
  104. seq_pos_add(i + j);
  105. }
  106. assert(shift[i + j] == 0);
  107. }
  108. }
  109. // clear a non-empty cell
  110. void rm(uint32_t i) {
  111. assert(i < pos.size());
  112. assert(pos[i] != -1);
  113. seq_pos_rm(i);
  114. seq[i].reset();
  115. pos[i] = -1;
  116. shift[i] = 0;
  117. used.erase(i);
  118. }
  119. // note: call only if the cell has seq_id
  120. // return true if the cell becomes empty
  121. bool seq_rm(uint32_t i, llama_seq_id seq_id) {
  122. assert(i < pos.size());
  123. assert(seq[i].test(seq_id));
  124. assert(pos[i] != -1);
  125. assert(seq_id >= 0);
  126. seq[i].reset(seq_id);
  127. seq_pos_dec(seq_id, pos[i]);
  128. if (seq[i].none()) {
  129. pos[i] = -1;
  130. shift[i] = 0;
  131. used.erase(i);
  132. return true;
  133. }
  134. return false;
  135. }
  136. // return true if the cell becomes empty (i.e. it did not contain seq_id before the call)
  137. bool seq_keep(uint32_t i, llama_seq_id seq_id) {
  138. assert(i < pos.size());
  139. if (seq[i].test(seq_id)) {
  140. seq_pos_rm(i);
  141. seq[i].reset();
  142. seq[i].set(seq_id);
  143. seq_pos_inc(seq_id, pos[i]);
  144. return false;
  145. }
  146. if (seq[i].any()) {
  147. seq_pos_rm(i);
  148. seq[i].reset();
  149. pos[i] = -1;
  150. shift[i] = 0;
  151. used.erase(i);
  152. return true;
  153. }
  154. assert(pos[i] == -1);
  155. return false;
  156. }
  157. // number of different sequences in the cell
  158. int seq_count(uint32_t i) const {
  159. assert(i < pos.size());
  160. assert(pos[i] != -1);
  161. return seq[i].count();
  162. }
  163. // check if the cell contains seq_id
  164. bool seq_has(uint32_t i, llama_seq_id seq_id) const {
  165. assert(i < pos.size());
  166. assert(seq_id >= 0);
  167. return seq[i].test(seq_id);
  168. }
  169. // note: call only if the cell is not empty and the seq_id is not in the cell
  170. void seq_add(uint32_t i, llama_seq_id seq_id) {
  171. assert(i < pos.size());
  172. assert(pos[i] != -1);
  173. assert(!seq[i].test(seq_id));
  174. seq[i].set(seq_id);
  175. seq_pos_inc(seq_id, pos[i]);
  176. }
  177. // return the sequence id of this cell
  178. // note: call only for cells with exactly one sequence
  179. llama_seq_id seq_get(uint32_t i) const {
  180. assert(seq[i].count() == 1);
  181. for (int s = 0; s < LLAMA_MAX_SEQ; ++s) {
  182. if (seq[i].test(s)) {
  183. return s;
  184. }
  185. }
  186. return -1;
  187. }
  188. // the minimum position of sequence seq_id currently present in any of the cells
  189. // return -1 if the sequence is not present
  190. llama_pos seq_pos_min(llama_seq_id seq_id) const {
  191. assert(seq_id >= 0);
  192. assert(seq_id < LLAMA_MAX_SEQ);
  193. if (seq_pos[seq_id].empty()) {
  194. return -1;
  195. }
  196. assert(seq_pos[seq_id].begin()->second > 0);
  197. return seq_pos[seq_id].begin()->first;
  198. }
  199. // the maximum position of sequence seq_id currently present in any of the cells
  200. // return -1 if the sequence is not present
  201. llama_pos seq_pos_max(llama_seq_id seq_id) const {
  202. assert(seq_id >= 0);
  203. assert(seq_id < LLAMA_MAX_SEQ);
  204. if (seq_pos[seq_id].empty()) {
  205. return -1;
  206. }
  207. assert(seq_pos[seq_id].rbegin()->second > 0);
  208. return seq_pos[seq_id].rbegin()->first;
  209. }
  210. // note: call only if the cell is not empty
  211. llama_pos pos_get(uint32_t i) const {
  212. assert(i < pos.size());
  213. assert(pos[i] != -1);
  214. return pos[i];
  215. }
  216. // note: call only if the cell is not empty
  217. llama_pos get_shift(uint32_t i) const {
  218. assert(i < pos.size());
  219. assert(pos[i] != -1);
  220. return shift[i];
  221. }
  222. // check if a cell is not empty and its position is within [p0, p1)
  223. bool pos_in(uint32_t i, llama_pos p0, llama_pos p1) const {
  224. assert(i < pos.size());
  225. return pos[i] >= p0 && pos[i] < p1;
  226. }
  227. // set the position of an empty cell
  228. // does not modify "has_shift"
  229. // note: call only if the cell is empty
  230. void pos_set(uint32_t i, llama_pos p) {
  231. assert(i < pos.size());
  232. assert(pos[i] == -1);
  233. assert(seq[i].none());
  234. pos[i] = p;
  235. used.insert(i);
  236. }
  237. // pos[i] = pos[i] + d
  238. // sets "has_shift" to true
  239. // note: call only if the cell is not empty
  240. bool pos_add(uint32_t i, llama_pos d) {
  241. assert(i < pos.size());
  242. assert(pos[i] != -1);
  243. seq_pos_rm(i);
  244. pos[i] += d;
  245. shift[i] += d;
  246. has_shift = true;
  247. if (pos[i] < 0) {
  248. seq[i].reset();
  249. pos[i] = -1;
  250. shift[i] = 0;
  251. used.erase(i);
  252. return true;
  253. }
  254. seq_pos_add(i);
  255. return false;
  256. }
  257. // pos[i] = pos[i] / d
  258. // sets "has_shift" to true
  259. // note: call only if the cell is not empty
  260. void pos_div(uint32_t i, int d) {
  261. assert(i < pos.size());
  262. assert(pos[i] != -1);
  263. const llama_pos p_old = pos[i];
  264. seq_pos_rm(i);
  265. pos[i] /= d;
  266. shift[i] += p_old - pos[i];
  267. seq_pos_add(i);
  268. has_shift = true;
  269. }
  270. private:
  271. bool has_shift = false;
  272. // set of indices of used cells (i.e. pos[i] != -1, allowed to not have any seq_id)
  273. std::set<uint32_t> used;
  274. std::vector<llama_pos> pos;
  275. // this array accumulates any applied shifts to the pos array since the last reset_shift() call
  276. // this is used to queue multiple updates to the pos array, which in the end can be applied in one go:
  277. //
  278. // cells.pos_add(x, shift_x);
  279. // cells.pos_div(y, shift_y);
  280. // ...
  281. //
  282. // if (cells.has_shift()) {
  283. // for (int i = 0; i < n; ++i) {
  284. // auto shift_i = cells.get_shift(i);
  285. // ...
  286. // }
  287. // cells.reset_shift();
  288. // }
  289. //
  290. std::vector<llama_pos> shift;
  291. using seq_set_t = std::bitset<LLAMA_MAX_SEQ>;
  292. // the bitset seq[i] tells us which sequences are currently occupying the i-th cell
  293. std::vector<seq_set_t> seq;
  294. // the set seq_pos[s][p] tells us how many times the position p is currently present for sequence s
  295. // if the position p is not present, seq_pos[s][p] is not set
  296. // this way seq_pos[s].begin() and seq_pos[s].rbegin() give us the min/max positions currently in the cache
  297. //
  298. // note that we cannot a use an std::set because in some cases a position can occur more than once for the same seq:
  299. // - during performing a cache reuse via (rm + add)
  300. // - some vision models have input embeddings with repeating positions
  301. //
  302. std::map<llama_pos, int> seq_pos[LLAMA_MAX_SEQ];
  303. // helper functions for updating `seq_pos`, once cell at a time:
  304. void seq_pos_dec(llama_seq_id s, llama_pos p) {
  305. auto it = seq_pos[s].find(p);
  306. assert(it != seq_pos[s].end());
  307. if (--it->second == 0) {
  308. seq_pos[s].erase(it);
  309. }
  310. }
  311. void seq_pos_inc(llama_seq_id s, llama_pos p) {
  312. seq_pos[s][p]++;
  313. }
  314. // remove cell i
  315. void seq_pos_rm(uint32_t i) {
  316. for (int s = 0; s < LLAMA_MAX_SEQ; ++s) {
  317. if (seq[i].test(s)) {
  318. seq_pos_dec(s, pos[i]);
  319. }
  320. }
  321. }
  322. // add cell i
  323. void seq_pos_add(uint32_t i) {
  324. for (int s = 0; s < LLAMA_MAX_SEQ; ++s) {
  325. if (seq[i].test(s)) {
  326. seq_pos_inc(s, pos[i]);
  327. }
  328. }
  329. }
  330. };