Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/transformers/cache_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,24 @@ def get_max_cache_shape(self) -> int:
"""Return the maximum cache shape of the cache"""
return self.max_cache_len

def crop(self, max_length: int) -> None:
"""Crop the cache to the given length."""
if not self.is_initialized:
return

current_length = self.cumulative_length.item()

if max_length < 0:
raise ValueError(f"`max_length` passed to `StaticLayer.crop()` must be >= 0, got {max_length}.")

if max_length >= current_length:
return

self.keys[:, :, max_length:, :].zero_()
self.values[:, :, max_length:, :].zero_()

self.cumulative_length.fill_(max_length)


class StaticSlidingWindowLayer(StaticLayer):
"""
Expand Down
Loading