6
6
#include < bitset>
7
7
#include < cassert>
8
8
#include < vector>
9
+ #include < set>
9
10
10
11
// meta information about KV cells that can be part of multiple sequences at the same time
11
12
// TODO: add unit tests
@@ -18,8 +19,13 @@ class llama_kv_cells_unified {
18
19
seq[i].reset ();
19
20
}
20
21
21
- used = 0 ;
22
22
has_shift = false ;
23
+
24
+ used_cells.clear ();
25
+
26
+ for (uint32_t s = 0 ; s < LLAMA_MAX_PARALLEL_SEQUENCES; ++s) {
27
+ seq_pos[s].clear ();
28
+ }
23
29
}
24
30
25
31
void reset_shift () {
@@ -50,7 +56,25 @@ class llama_kv_cells_unified {
50
56
}
51
57
52
58
uint32_t get_used () const {
53
- return used;
59
+ return used_cells.size ();
60
+ }
61
+
62
+ // the index of the first cell that is used
63
+ // return 0 if no cells are used
64
+ uint32_t used_min () const {
65
+ return used_cells.empty () ? 0 : *used_cells.begin ();
66
+ }
67
+
68
+ // the index of the last cell that is used + 1
69
+ // return 0 if no cells are used
70
+ uint32_t used_max_p1 () const {
71
+ #if 0
72
+ if (!seq_pos[0].empty()) printf("kv_cells: min[0] = %5d, max[0] = %5d\n", *seq_pos[0].begin(), *seq_pos[0].rbegin());
73
+ if (!seq_pos[1].empty()) printf("kv_cells: min[1] = %5d, max[1] = %5d\n", *seq_pos[1].begin(), *seq_pos[1].rbegin());
74
+ if (!seq_pos[2].empty()) printf("kv_cells: min[2] = %5d, max[2] = %5d\n", *seq_pos[2].begin(), *seq_pos[2].rbegin());
75
+ #endif
76
+
77
+ return used_cells.empty () ? 0 : *used_cells.rbegin () + 1 ;
54
78
}
55
79
56
80
bool get_has_shift () const {
@@ -69,6 +93,9 @@ class llama_kv_cells_unified {
69
93
pos [isrc] = -1 ;
70
94
shift[isrc] = 0 ;
71
95
seq [isrc].reset ();
96
+
97
+ used_cells.erase (isrc);
98
+ used_cells.insert (idst);
72
99
}
73
100
74
101
// copy the state of cells [i, i + n) (used for save/restore the state of the cells)
@@ -95,16 +122,24 @@ class llama_kv_cells_unified {
95
122
96
123
for (uint32_t j = 0 ; j < other.pos .size (); ++j) {
97
124
if (pos[i + j] == -1 && other.pos [j] != -1 ) {
98
- used++ ;
125
+ used_cells. insert (i + j) ;
99
126
}
100
127
101
128
if (pos[i + j] != -1 && other.pos [j] == -1 ) {
102
- used--;
129
+ used_cells.erase (i + j);
130
+ }
131
+
132
+ if (pos[i + j] != -1 ) {
133
+ seq_pos_rm (i + j);
103
134
}
104
135
105
136
pos[i + j] = other.pos [j];
106
137
seq[i + j] = other.seq [j];
107
138
139
+ if (pos[i + j] != -1 ) {
140
+ seq_pos_add (i + j);
141
+ }
142
+
108
143
assert (shift[i + j] == 0 );
109
144
}
110
145
}
@@ -118,11 +153,12 @@ class llama_kv_cells_unified {
118
153
assert (seq_id >= 0 );
119
154
120
155
seq[i].reset (seq_id);
156
+ seq_pos[seq_id].erase (pos[i]);
121
157
122
158
if (seq[i].none ()) {
123
159
pos[i] = -1 ;
124
160
125
- used-- ;
161
+ used_cells. erase (i) ;
126
162
127
163
return true ;
128
164
}
@@ -135,17 +171,22 @@ class llama_kv_cells_unified {
135
171
assert (i < pos.size ());
136
172
137
173
if (seq[i].test (seq_id)) {
174
+ seq_pos_rm (i);
138
175
seq[i].reset ();
176
+
139
177
seq[i].set (seq_id);
178
+ seq_pos[seq_id].insert (pos[i]);
140
179
141
180
return false ;
142
181
}
143
182
144
183
if (seq[i].any ()) {
184
+ seq_pos_rm (i);
145
185
seq[i].reset ();
186
+
146
187
pos[i] = -1 ;
147
188
148
- used-- ;
189
+ used_cells. erase (i) ;
149
190
150
191
return true ;
151
192
}
@@ -169,6 +210,29 @@ class llama_kv_cells_unified {
169
210
assert (!seq[i].test (seq_id));
170
211
171
212
seq[i].set (seq_id);
213
+ seq_pos[seq_id].insert (pos[i]);
214
+ }
215
+
216
+ llama_pos seq_pos_min (llama_seq_id seq_id) const {
217
+ assert (seq_id >= 0 );
218
+ assert (seq_id < LLAMA_MAX_PARALLEL_SEQUENCES);
219
+
220
+ if (seq_pos[seq_id].empty ()) {
221
+ return -1 ;
222
+ }
223
+
224
+ return *seq_pos[seq_id].begin ();
225
+ }
226
+
227
+ llama_pos seq_pos_max (llama_seq_id seq_id) const {
228
+ assert (seq_id >= 0 );
229
+ assert (seq_id < LLAMA_MAX_PARALLEL_SEQUENCES);
230
+
231
+ if (seq_pos[seq_id].empty ()) {
232
+ return -1 ;
233
+ }
234
+
235
+ return *seq_pos[seq_id].rbegin ();
172
236
}
173
237
174
238
// note: call only if the cell is not empty
@@ -202,7 +266,8 @@ class llama_kv_cells_unified {
202
266
assert (pos[i] == -1 );
203
267
204
268
pos[i] = p;
205
- used++;
269
+
270
+ used_cells.insert (i);
206
271
}
207
272
208
273
// pos[i] = pos[i] + d
@@ -218,10 +283,12 @@ class llama_kv_cells_unified {
218
283
has_shift = true ;
219
284
220
285
if (pos[i] < 0 ) {
221
- pos[i] = -1 ;
286
+ seq_pos_rm (i);
287
+
222
288
seq[i].reset ();
289
+ pos[i] = -1 ;
223
290
224
- used-- ;
291
+ used_cells. erase (i) ;
225
292
226
293
return true ;
227
294
}
@@ -245,10 +312,11 @@ class llama_kv_cells_unified {
245
312
}
246
313
247
314
private:
248
- uint32_t used = 0 ; // used cells (i.e. pos[i] != -1, allowed to not have any seq_id)
249
-
250
315
bool has_shift = false ;
251
316
317
+ // set of indices of used cells (i.e. pos[i] != -1, allowed to not have any seq_id)
318
+ std::set<uint32_t > used_cells;
319
+
252
320
std::vector<llama_pos> pos;
253
321
254
322
// this array accumulates any applied shifts to the pos array since the last reset_shift() call
@@ -268,6 +336,32 @@ class llama_kv_cells_unified {
268
336
//
269
337
std::vector<llama_pos> shift;
270
338
271
- std::vector<std::bitset<LLAMA_MAX_PARALLEL_SEQUENCES>> seq;
272
- };
339
+ using bits_t = std::bitset<LLAMA_MAX_PARALLEL_SEQUENCES>;
273
340
341
+ // the bitset seq[i] tells us which sequences are currently occupying the i-th cell
342
+ std::vector<bits_t > seq;
343
+
344
+ // the set seq_pos[s] tells us which positions are currently occupied by the s-th sequence
345
+ // this way seq_pos[s].begin() and seq_pos[s].rbegin() give us the min/max positions currently in the cache
346
+ std::set<llama_pos> seq_pos[LLAMA_MAX_PARALLEL_SEQUENCES];
347
+
348
+ // helper functions for updating `seq_pos`, once cell at a time:
349
+
350
+ // remove cell i
351
+ void seq_pos_rm (uint32_t i) {
352
+ for (int s = 0 ; s < LLAMA_MAX_PARALLEL_SEQUENCES; ++s) {
353
+ if (seq[i].test (s)) {
354
+ seq_pos[s].erase (pos[i]);
355
+ }
356
+ }
357
+ }
358
+
359
+ // add cell i
360
+ void seq_pos_add (uint32_t i) {
361
+ for (int s = 0 ; s < LLAMA_MAX_PARALLEL_SEQUENCES; ++s) {
362
+ if (seq[i].test (s)) {
363
+ seq_pos[s].insert (pos[i]);
364
+ }
365
+ }
366
+ }
367
+ };
0 commit comments