@@ -541,14 +541,13 @@ def get_logits(self, token_ids: list[int], include_all_uncached_tokens: bool = F
541541
542542 # clear obsolete parts of kv cache
543543 if past_length > num_cached :
544- past_length = num_cached
545544 if isinstance (past_key_values , tuple ):
546545 self ._past_key_values = tuple (
547- tuple (p [..., :past_length , :] for p in v ) for v in past_key_values
546+ tuple (p [..., :num_cached , :] for p in v ) for v in past_key_values
548547 )
549548 else :
550549 if hasattr (past_key_values , "crop" ):
551- self ._past_key_values .crop (past_length )
550+ self ._past_key_values .crop (num_cached )
552551 else :
553552 warnings .warn (
554553 f"Cropping unsupported for cache type: { type (self ._past_key_values )} . Resetting cache."
@@ -558,8 +557,10 @@ def get_logits(self, token_ids: list[int], include_all_uncached_tokens: bool = F
558557 self ._past_key_values .reset ()
559558 else :
560559 self ._past_key_values = None
561- past_length = 0
562- else :
560+ # Our cache is no longer valid
561+ num_cached = 0
562+ past_length = num_cached
563+ elif past_length < num_cached :
563564 # Should never happen, but just in case -- we're not using all of the cached tokens
564565 # (because they're not really in the kv cache somehow)
565566 num_cached = past_length
0 commit comments