causal.go 17.4 KB
Newer Older
Jesse Gross's avatar
Jesse Gross committed
1
2
3
4
5
6
7
8
9
package kvcache

import (
	"errors"
	"fmt"
	"math"
	"slices"

	"github.com/ollama/ollama/ml"
10
	"github.com/ollama/ollama/model/input"
Jesse Gross's avatar
Jesse Gross committed
11
12
13
14
15
16
17
18
19
20
)

type shiftFn func(ctx ml.Context, layer int, key, shift ml.Tensor) (ml.Tensor, error)

// Causal cache stores K and V tensors according to their position in the
// sequence. Returns the history and a mask for attending to past tokens
//
// The tensors are of shape embed dim, kv heads, batch size
// The mask is of shape history size, batch size
type Causal struct {
21
22
23
24
25
26
27
28
29
30
	DType ml.DType

	// swaWindowSize is the number of tokens that will be included in the mask
	// during attention operations. swaMemorySize is the number of tokens that
	// will be retained in memory for partial prefix caching. Set to math.MaxInt32
	// for unlimited or if sliding window attention is not being used.
	swaWindowSize int32
	swaMemorySize int32

	chunkSize int32
Jesse Gross's avatar
Jesse Gross committed
31

32
33
	opts CausalOptions

34
35
36
	// maxBatch is the largest batch that we might receive
	maxBatch int

37
38
39
	// config controls mostly backend-specific optimizations
	config *ml.CacheConfig

Jesse Gross's avatar
Jesse Gross committed
40
41
42
43
44
	// ** current forward pass **

	// size of the current batch
	curBatchSize int

45
46
47
	// locations for data storage for this batch
	curLoc ml.Tensor

Jesse Gross's avatar
Jesse Gross committed
48
49
50
	// mask of the cache as used by this batch
	curMask ml.Tensor

51
52
53
	// the active layer for Get and Put
	curLayer int

Jesse Gross's avatar
Jesse Gross committed
54
55
56
	// locations in the cache that are needed for this batch
	curCellRange cellRange

57
58
59
60
61
62
	// curSequences is the sequences corresponding to this pass's entries in the cache
	curSequences []int

	// curPositions is the positions corresponding to this pass's entries in the cache
	curPositions []int32

Jesse Gross's avatar
Jesse Gross committed
63
64
65
66
67
68
69
70
71
72
73
74
75
	// ** cache metadata **

	// for each possible location in the cache, stores the position and set of sequences
	// that reference the data there
	cells []cacheCell

	// maps from sequence to the range of locations where it is stored in the cache
	cellRanges map[int]cellRange

	// ** cache data storage **

	shiftFn      shiftFn
	backend      ml.Backend
76
77
	ctxs         map[int]ml.Context
	keys, values map[int]ml.Tensor
Jesse Gross's avatar
Jesse Gross committed
78
79
80
81
82
83
84
85
86
87
88
89
90
}

type cacheCell struct {
	pos       int32
	sequences []int
}

type cellRange struct {
	min int
	max int
}

func NewCausalCache(shift shiftFn) *Causal {
91
	return &Causal{
92
93
94
95
		shiftFn: shift,
		ctxs:    make(map[int]ml.Context),
		keys:    make(map[int]ml.Tensor),
		values:  make(map[int]ml.Tensor),
96
	}
Jesse Gross's avatar
Jesse Gross committed
97
98
99
}

func NewSWACache(windowSize int32, shift shiftFn) *Causal {
100
	return &Causal{
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
		swaWindowSize: windowSize,
		shiftFn:       shift,
		ctxs:          make(map[int]ml.Context),
		keys:          make(map[int]ml.Tensor),
		values:        make(map[int]ml.Tensor),
	}
}

func NewSWAMemCache(windowSize int32, memorySize int32, shift shiftFn) *Causal {
	return &Causal{
		swaWindowSize: windowSize,
		swaMemorySize: memorySize,
		shiftFn:       shift,
		ctxs:          make(map[int]ml.Context),
		keys:          make(map[int]ml.Tensor),
		values:        make(map[int]ml.Tensor),
117
	}
Jesse Gross's avatar
Jesse Gross committed
118
119
}

Michael Yang's avatar
Michael Yang committed
120
121
func NewChunkedAttentionCache(chunkSize int32, shift shiftFn) *Causal {
	return &Causal{
122
123
124
125
126
		chunkSize: chunkSize,
		shiftFn:   shift,
		ctxs:      make(map[int]ml.Context),
		keys:      make(map[int]ml.Tensor),
		values:    make(map[int]ml.Tensor),
Michael Yang's avatar
Michael Yang committed
127
128
129
	}
}

130
func (c *Causal) Init(backend ml.Backend, dtype ml.DType, maxSequences, capacity, maxBatch int) {
131
132
133
134
135
136
137
138
139
140
141
142
	if c.config == nil {
		var config ml.CacheConfig
		if cc, ok := backend.(ml.BackendCacheConfig); ok {
			config = cc.CacheConfig()
		}
		c.config = &config
	}

	if c.config.CachePadding == 0 {
		c.config.CachePadding = 1
	}

143
144
145
146
147
148
149
150
	if c.config.MaskBatchPadding == 0 {
		c.config.MaskBatchPadding = 1
	}

	if c.config.MaskDType == ml.DTypeOther {
		c.config.MaskDType = ml.DTypeF32
	}

151
152
153
154
155
156
	if c.swaWindowSize == 0 {
		c.swaWindowSize = math.MaxInt32
	}
	if c.swaMemorySize == 0 {
		c.swaMemorySize = c.swaWindowSize
	}
157
158
159
160
161
162
163
164
165
	// We will allocate space in the cache for the stop token, which won't be part of a follow on
	// sequence, so allocate an extra token of storage to ensure that we can jump back without
	// causing a cache break. As an optimization, only do this when we have parallel sequences
	// because the extra token will live in the batch buffer and won't get overwritten if we
	// only have a single sequence.
	if c.swaMemorySize != math.MaxInt32 && maxSequences > 1 {
		c.swaMemorySize = max(c.swaMemorySize, c.swaWindowSize+1)
	}
	if int(c.swaMemorySize) >= capacity {
166
167
168
169
170
171
172
		c.swaMemorySize = math.MaxInt32
	}

	if c.swaMemorySize < c.swaWindowSize {
		panic(fmt.Errorf("sliding window memory (%v) must be at least as large as the window (%v)", c.swaMemorySize, c.swaWindowSize))
	}

173
	var cacheSize int
174
	if c.swaMemorySize == math.MaxInt32 {
175
176
		cacheSize = maxSequences * capacity
	} else {
177
		cacheSize = (maxSequences * int(c.swaMemorySize)) + maxBatch
178
	}
179
180
181
	cacheSize = roundUp(cacheSize, c.config.CachePadding)
	c.cells = make([]cacheCell, cacheSize)

Jesse Gross's avatar
Jesse Gross committed
182
183
184
	c.DType = dtype
	c.cellRanges = make(map[int]cellRange)
	c.backend = backend
185
	c.maxBatch = maxBatch
Jesse Gross's avatar
Jesse Gross committed
186
187
}

188
189
190
191
192
193
194
195
func (c *Causal) SetConfig(config ml.CacheConfig) {
	if c.config != nil {
		panic("config cannot be changed after being previously set, either by the model or backend")
	}

	c.config = &config
}

Jesse Gross's avatar
Jesse Gross committed
196
func (c *Causal) Close() {
197
198
199
	for _, ctx := range c.ctxs {
		ctx.Close()
	}
Jesse Gross's avatar
Jesse Gross committed
200
201
}

202
func (c *Causal) StartForward(ctx ml.Context, batch input.Batch, reserve bool) error {
Jesse Gross's avatar
Jesse Gross committed
203
204
205
	c.curBatchSize = len(batch.Positions)
	c.curSequences = batch.Sequences
	c.curPositions = batch.Positions
206
	c.opts.Except = nil
Jesse Gross's avatar
Jesse Gross committed
207

208
	var locs []int32
209
	if !reserve {
210
		c.updateSlidingWindow()
211

212
		var err error
213
		locs, err = c.findLocs()
214
215
		if err != nil {
			return err
Jesse Gross's avatar
Jesse Gross committed
216
217
		}

218
219
		for i, pos := range batch.Positions {
			seq := batch.Sequences[i]
220
			loc := int(locs[i])
221

222
			c.cells[loc] = cacheCell{pos: pos, sequences: []int{seq}}
223
224
225
226
227
228

			seqRange, ok := c.cellRanges[seq]
			if !ok {
				seqRange = newRange()
			}

229
230
			seqRange.min = min(seqRange.min, loc)
			c.curCellRange.min = min(c.curCellRange.min, loc)
231

232
233
			seqRange.max = max(seqRange.max, loc)
			c.curCellRange.max = max(c.curCellRange.max, loc)
234
235

			c.cellRanges[seq] = seqRange
Jesse Gross's avatar
Jesse Gross committed
236
		}
237
238
239
	} else {
		// If we are reserving memory, don't update any of the cache metadata but set the size
		// to the worst case.
240
241
242
243
		locs = make([]int32, c.curBatchSize)
		for i := range locs {
			locs[i] = int32(i)
		}
244
245
		c.curCellRange.min = 0
		c.curCellRange.max = len(c.cells) - 1
Jesse Gross's avatar
Jesse Gross committed
246
247
	}

248
	c.curLoc = ctx.Input().FromInts(locs, len(locs))
249
	c.curMask = c.buildMask(ctx)
Jesse Gross's avatar
Jesse Gross committed
250

251
	return nil
Jesse Gross's avatar
Jesse Gross committed
252
253
254
255
256
257
258
259
260
}

func newRange() cellRange {
	return cellRange{
		min: math.MaxInt,
		max: 0,
	}
}

261
262
263
264
// Returns a slice of locations where each token in the batch should be stored
func (c *Causal) findLocs() ([]int32, error) {
	loc := make([]int32, 0, c.curBatchSize)

Jesse Gross's avatar
Jesse Gross committed
265
266
	for i := range c.cells {
		if len(c.cells[i].sequences) == 0 {
267
268
269
			loc = append(loc, int32(i))
			if len(loc) >= c.curBatchSize {
				return loc, nil
Jesse Gross's avatar
Jesse Gross committed
270
271
272
273
			}
		}
	}

274
	return nil, fmt.Errorf("%w (cache: %v batch: %v)", ErrKvCacheFull, len(c.cells), c.curBatchSize)
Jesse Gross's avatar
Jesse Gross committed
275
276
}

277
func (c *Causal) updateSlidingWindow() {
278
279
280
281
282
283
284
285
286
287
	c.curCellRange = newRange()

	if c.swaMemorySize == math.MaxInt32 {
		for _, seq := range c.curSequences {
			if seqRange, ok := c.cellRanges[seq]; ok {
				c.curCellRange.min = min(c.curCellRange.min, seqRange.min)
				c.curCellRange.max = max(c.curCellRange.max, seqRange.max)
			}
		}

288
289
290
		return
	}

291
292
293
294
295
	type lowestPosition struct {
		pos      int32
		curBatch bool
	}

296
	// create a map of unique sequences to the lowest position in that sequence
297
	lowestPos := make(map[int]lowestPosition)
298
299
300
	for i := range c.curPositions {
		seq := c.curSequences[i]

301
		lowest, ok := lowestPos[seq]
302
		if !ok {
303
304
305
			lowest = lowestPosition{pos: c.curPositions[i], curBatch: true}
		} else if c.curPositions[i] < lowest.pos {
			lowest.pos = c.curPositions[i]
306
307
		}

308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
		lowestPos[seq] = lowest
	}

	// for any sequences are not part of this batch, clean up any tokens
	// that are no longer needed after the processing of the previous
	// batch
	for seq, seqRange := range c.cellRanges {
		if _, ok := lowestPos[seq]; !ok {
			var last int32
			for i := seqRange.min; i <= seqRange.max; i++ {
				if slices.Contains(c.cells[i].sequences, seq) {
					last = max(last, c.cells[i].pos)
				}
			}

			lowestPos[seq] = lowestPosition{pos: last + 1, curBatch: false}
		}
325
326
327
	}

	// delete any entries that are beyond the window of the oldest position in the sequence
328
	for seq, lowest := range lowestPos {
329
330
331
332
333
334
335
336
337
		oldRange, ok := c.cellRanges[seq]
		if !ok {
			continue
		}

		newRange := newRange()

		for i := oldRange.min; i <= oldRange.max; i++ {
			if slices.Contains(c.cells[i].sequences, seq) {
338
				if c.cells[i].pos < lowest.pos-c.swaMemorySize {
339
340
341
342
343
					c.cells[i].sequences = slices.DeleteFunc(c.cells[i].sequences, func(s int) bool { return s == seq })
				} else {
					newRange.min = min(newRange.min, i)
					newRange.max = max(newRange.max, i)
				}
344
				if lowest.curBatch && c.cells[i].pos >= lowest.pos-c.swaWindowSize {
345
346
347
					c.curCellRange.min = min(c.curCellRange.min, i)
					c.curCellRange.max = max(c.curCellRange.max, i)
				}
348
349
350
351
352
353
354
			}
		}

		c.cellRanges[seq] = newRange
	}
}

355
356
357
358
359
360
361
362
func roundDown(length, pad int) int {
	return (length / pad) * pad
}

func roundUp(length, pad int) int {
	return ((length + pad - 1) / pad) * pad
}

Jesse Gross's avatar
Jesse Gross committed
363
364
365
// Builds a mask of history x batch indicating whether for each token in the batch the
// token in the history should apply. This is based on both the sequence and causality (the
// position of the history is not ahead of the token in the batch).
366
func (c *Causal) buildMask(ctx ml.Context) ml.Tensor {
367
368
369
	// Align and pad the two dimensions as required by the backend
	batchSize := roundUp(c.curBatchSize, c.config.MaskBatchPadding)

370
371
372
373
	c.curCellRange.min = roundDown(c.curCellRange.min, c.config.CachePadding)
	c.curCellRange.max = roundUp(c.curCellRange.max+1, c.config.CachePadding) - 1

	length := c.curCellRange.max - c.curCellRange.min + 1
374

375
	mask := make([]float32, batchSize*length)
Jesse Gross's avatar
Jesse Gross committed
376
377

	for i := range c.curBatchSize {
378
		enabled := !slices.Contains(c.opts.Except, i)
Jesse Gross's avatar
Jesse Gross committed
379
		for j := c.curCellRange.min; j <= c.curCellRange.max; j++ {
380
			if !slices.Contains(c.cells[j].sequences, c.curSequences[i]) ||
381
				(enabled && c.cells[j].pos > c.curPositions[i]) ||
Michael Yang's avatar
Michael Yang committed
382
				c.chunkSize > 0 && c.cells[j].pos < c.curPositions[i]-c.curPositions[i]%c.chunkSize ||
383
				c.cells[j].pos < c.curPositions[i]-c.swaWindowSize {
384
				mask[i*length+(j-c.curCellRange.min)] = float32(math.Inf(-1))
Jesse Gross's avatar
Jesse Gross committed
385
386
387
388
			}
		}
	}

389
390
391
392
393
394
	// Mask out any padding tokens we added. For padding that we added to the cache history, this
	// has already been masked out because the sequence doesn't match.
	for i := c.curBatchSize * length; i < len(mask); i++ {
		mask[i] = float32(math.Inf(-1))
	}

Michael Yang's avatar
Michael Yang committed
395
	maskTensor := ctx.Input().FromFloats(mask, length, batchSize)
396
397

	if c.config.MaskDType != ml.DTypeF32 {
398
		maskTensor = maskTensor.Cast(ctx, c.config.MaskDType)
399
400
	}

401
	return maskTensor
Jesse Gross's avatar
Jesse Gross committed
402
403
404
405
406
407
}

func (c *Causal) SetLayer(layer int) {
	c.curLayer = layer
}

408
type CausalOptions struct {
409
410
	// Enabled controls whether the causal mask is generated for a particular index in a batch
	Except []int
411
412
}

413
414
// SetCausal disables causal mask generation for a particular range of indicies in
// the current batch for subsequent calls to Get. The state resets for the next forward pass.
415
416
417
func (c *Causal) SetCausal(ctx ml.Context, opts CausalOptions) {
	if !slices.Equal(c.opts.Except, opts.Except) {
		c.opts = opts
418
		if ctx != nil {
419
			c.curMask = c.buildMask(ctx)
420
421
422
423
		}
	}
}

Jesse Gross's avatar
Jesse Gross committed
424
425
426
427
func (c *Causal) Get(ctx ml.Context) (ml.Tensor, ml.Tensor, ml.Tensor) {
	key := c.keys[c.curLayer]
	value := c.values[c.curLayer]

428
429
430
431
	kHeadDim := key.Dim(0)
	numKVHeads := key.Dim(1)
	rowSize := key.Stride(2)
	cachedSize := c.curMask.Dim(0)
Jesse Gross's avatar
Jesse Gross committed
432

433
434
435
436
	key = key.View(ctx, rowSize*c.curCellRange.min,
		kHeadDim, key.Stride(1),
		numKVHeads, key.Stride(2),
		cachedSize,
Jesse Gross's avatar
Jesse Gross committed
437
438
	)

439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
	if c.config.PermutedV {
		vHeadDim := value.Dim(1)
		elemSize := value.Stride(0)

		value = value.View(ctx, elemSize*c.curCellRange.min,
			cachedSize, value.Stride(1),
			vHeadDim, value.Stride(2),
			numKVHeads,
		)
	} else {
		vHeadDim := value.Dim(0)
		rowSize := value.Stride(2)

		value = value.View(ctx, rowSize*c.curCellRange.min,
			vHeadDim, value.Stride(1),
			numKVHeads, value.Stride(2),
			cachedSize,
		)
	}

Jesse Gross's avatar
Jesse Gross committed
459
460
461
462
	return key, value, c.curMask
}

func (c *Causal) Put(ctx ml.Context, key, value ml.Tensor) {
463
464
465
466
467
468
469
	kHeadDim := key.Dim(0)
	vHeadDim := value.Dim(0)
	numKVHeads := key.Dim(1)
	batchSize := key.Dim(2)

	if c.curBatchSize != batchSize {
		panic(fmt.Errorf("inconsistent batch sizes (layer: %v, batch size: %v layer batch size: %v)", c.curLayer, c.curBatchSize, batchSize))
Jesse Gross's avatar
Jesse Gross committed
470
471
	}

472
	if _, ok := c.ctxs[c.curLayer]; !ok {
473
		c.ctxs[c.curLayer] = c.backend.NewContextSize(2).Layer(c.curLayer)
474
475
476
	}

	if _, ok := c.keys[c.curLayer]; !ok {
477
		c.keys[c.curLayer] = c.ctxs[c.curLayer].Zeros(c.DType, kHeadDim, numKVHeads, len(c.cells))
478
	}
479

480
	if _, ok := c.values[c.curLayer]; !ok {
481
		if c.config.PermutedV {
482
			c.values[c.curLayer] = c.ctxs[c.curLayer].Zeros(c.DType, len(c.cells), vHeadDim, numKVHeads)
483
		} else {
484
			c.values[c.curLayer] = c.ctxs[c.curLayer].Zeros(c.DType, vHeadDim, numKVHeads, len(c.cells))
485
		}
Jesse Gross's avatar
Jesse Gross committed
486
487
	}

488
489
490
491
	key = key.Reshape(ctx, kHeadDim*numKVHeads, batchSize)
	keyCache := c.keys[c.curLayer]
	keyCache = keyCache.Reshape(ctx, kHeadDim*numKVHeads, len(c.cells))
	ctx.Forward(keyCache.SetRows(ctx, key, c.curLoc))
492
493

	if c.config.PermutedV {
494
495
496
497
498
		value = value.Reshape(ctx, vHeadDim*numKVHeads, 1, batchSize)
		value = value.Permute(ctx, 2, 0, 1, 3)

		valueCache := c.values[c.curLayer]
		valueCache = valueCache.Reshape(ctx, 1, len(c.cells), vHeadDim*numKVHeads)
499

500
		ctx.Forward(valueCache.SetRows(ctx, value, c.curLoc))
501
	} else {
502
503
504
		value = value.Reshape(ctx, vHeadDim*numKVHeads, batchSize)
		valueCache := c.values[c.curLayer]
		valueCache = valueCache.Reshape(ctx, vHeadDim*numKVHeads, len(c.cells))
505

506
		ctx.Forward(valueCache.SetRows(ctx, value, c.curLoc))
507
	}
Jesse Gross's avatar
Jesse Gross committed
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
}

func (c *Causal) CopyPrefix(srcSeq, dstSeq int, len int32) {
	seqRange := newRange()

	for i := range c.cells {
		// Remove the contents of dstSeq so that we only have the copied prefix, metadata will be reset at the end
		if slices.Contains(c.cells[i].sequences, dstSeq) {
			c.cells[i].sequences = slices.DeleteFunc(c.cells[i].sequences, func(s int) bool { return s == dstSeq })
		}

		if slices.Contains(c.cells[i].sequences, srcSeq) && c.cells[i].pos < len {
			c.cells[i].sequences = append(c.cells[i].sequences, dstSeq)
			if i < seqRange.min {
				seqRange.min = i
			}
			if i > seqRange.max {
				seqRange.max = i
			}
		}
	}

	c.cellRanges[dstSeq] = seqRange
}

533
func (c *Causal) CanResume(seq int, pos int32) bool {
534
	if c.swaMemorySize == math.MaxInt32 {
535
536
537
538
539
540
541
542
543
544
		return true
	}

	seqRange, ok := c.cellRanges[seq]
	if !ok {
		return false
	}

	// for sliding window, check that the window of the new sequence is contained in
	// the window of what we are storing
545
	var first int32 = math.MaxInt32
546
547
548
	var last int32 = -1
	for i := seqRange.min; i <= seqRange.max; i++ {
		if slices.Contains(c.cells[i].sequences, seq) {
549
			first = min(first, c.cells[i].pos)
550
551
552
553
554
555
556
557
			last = max(last, c.cells[i].pos)
		}
	}

	if last == -1 {
		return false
	}

558
	posWindowStart := max(0, pos-c.swaWindowSize)
559
	return posWindowStart >= first && pos <= last+1
560
561
}

Jesse Gross's avatar
Jesse Gross committed
562
563
564
565
566
567
568
func (c *Causal) shift(seq int, beginIndex, offset int32) error {
	if c.shiftFn == nil {
		return ErrNotSupported
	}

	seqRange := c.cellRanges[seq]

569
570
571
	for start := seqRange.min; start <= seqRange.max; start += c.maxBatch {
		size := min(seqRange.max-start+1, c.maxBatch)
		offsets := make([]int32, size)
572
573
574
575

		var batchFirst, batchLast int

		batchFirst = -1
576
577
578
579
580
		for i := range offsets {
			cell := c.cells[start+i]

			if slices.Contains(cell.sequences, seq) && cell.pos >= beginIndex {
				offsets[i] = offset
581
582
583
584
				if batchFirst < 0 {
					batchFirst = i
				}
				batchLast = i
585
			}
Jesse Gross's avatar
Jesse Gross committed
586
587
		}

588
589
590
591
592
593
594
		if batchFirst < 0 {
			continue
		}

		offsets = offsets[batchFirst : batchLast+1]

		ctx := c.backend.NewContext()
Michael Yang's avatar
Michael Yang committed
595
		kShift := ctx.Input().FromInts(offsets, len(offsets))
Jesse Gross's avatar
Jesse Gross committed
596

597
598
599
600
		for i, key := range c.keys {
			if key == nil {
				continue
			}
Jesse Gross's avatar
Jesse Gross committed
601

602
603
604
			kHeadDim := key.Dim(0)
			numKVHeads := key.Dim(1)
			rowSize := key.Stride(2)
605

606
			key = key.View(ctx, rowSize*(start+batchFirst),
607
608
				kHeadDim, key.Stride(1),
				numKVHeads, key.Stride(2),
609
				len(offsets),
610
			)
Jesse Gross's avatar
Jesse Gross committed
611

612
613
614
615
616
617
618
			roped, err := c.shiftFn(ctx, i, key, kShift)
			if err != nil {
				ctx.Close()
				return err
			}

			ctx.Forward(roped.Copy(ctx, key))
Jesse Gross's avatar
Jesse Gross committed
619
620
		}

621
622
		ctx.Compute()
		ctx.Close()
Jesse Gross's avatar
Jesse Gross committed
623
624
625
626
627
628
	}

	return nil
}

func (c *Causal) Remove(seq int, beginIndex, endIndex int32) error {
629
630
631
632
633
634
	// TODO(jessegross): We should check to see if removing the middle of the sequence will
	// cause the sliding window to encompass tokens that we no longer have. If so, then we
	// should return an error, which will trigger the runner to evaluate the full history and
	// rebuild the window. However, if we have multimodal inputs in our history, this reuse
	// results in use after free, so we don't do it for now.

Jesse Gross's avatar
Jesse Gross committed
635
636
637
638
639
640
641
642
643
644
645
646
647
648
	var offset int32
	if endIndex != math.MaxInt32 {
		offset = beginIndex - endIndex
	}

	seqRange := newRange()

	for i := range c.cells {
		if slices.Contains(c.cells[i].sequences, seq) {
			if c.cells[i].pos >= beginIndex && c.cells[i].pos < endIndex {
				c.cells[i].sequences = slices.DeleteFunc(c.cells[i].sequences, func(s int) bool { return s == seq })
			} else {
				if c.cells[i].pos >= endIndex {
					if slices.ContainsFunc(c.cells[i].sequences, func(s int) bool { return s != seq }) {
649
						return errors.New("shifting cells shared by multiple sequences not supported")
Jesse Gross's avatar
Jesse Gross committed
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
					}

					c.cells[i].pos += offset
				}
				if i < seqRange.min {
					seqRange.min = i
				}
				if i > seqRange.max {
					seqRange.max = i
				}
			}
		}
	}

	if seqRange == newRange() {
		delete(c.cellRanges, seq)
		return nil
	}

	c.cellRanges[seq] = seqRange

	if endIndex != math.MaxInt32 {
		err := c.shift(seq, endIndex+offset, offset)
		if err != nil {
			return err
		}
	}

	return nil
}