Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 707adf1

Browse files
fix lazy initialization
1 parent 8ae3556 commit 707adf1

1 file changed

Lines changed: 10 additions & 10 deletions

File tree

‎src/transformers/cache_utils.py‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def __repr__(self):
3737
return f"{self.__class__.__name__}"
3838

3939
@abstractmethod
40-
def lazy_initialization(self, key_states: torch.Tensor): ...
40+
def lazy_initialization(self, key_states: torch.Tensor, value_states: torch.Tensor) -> None: ...
4141

4242
@abstractmethod
4343
def update(
@@ -89,7 +89,7 @@ class DynamicLayer(CacheLayerMixin):
8989

9090
is_sliding = False
9191

92-
def lazy_initialization(self, key_states: torch.Tensor):
92+
def lazy_initialization(self, key_states: torch.Tensor, value_states: torch.Tensor) -> None:
9393
self.dtype, self.device = key_states.dtype, key_states.device
9494
self.keys = torch.tensor([], dtype=self.dtype, device=self.device)
9595
self.values = torch.tensor([], dtype=self.dtype, device=self.device)
@@ -114,7 +114,7 @@ def update(
114114
"""
115115
# Lazy initialization
116116
if not self.is_initialized:
117-
self.lazy_initialization(key_states)
117+
self.lazy_initialization(key_states, value_states)
118118

119119
self.keys = torch.cat([self.keys, key_states], dim=-2)
120120
self.values = torch.cat([self.values, value_states], dim=-2)
@@ -178,8 +178,8 @@ def __init__(self, sliding_window: int):
178178
self.cumulative_length = 0
179179
self._sliding_window_tensor = torch.tensor(self.sliding_window, dtype=torch.long)
180180

181-
def lazy_initialization(self, key_states: torch.Tensor) -> None:
182-
super().lazy_initialization(key_states)
181+
def lazy_initialization(self, key_states: torch.Tensor, value_states: torch.Tensor) -> None:
182+
super().lazy_initialization(key_states, value_states)
183183
self._sliding_window_tensor = self._sliding_window_tensor.to(self.device)
184184

185185
def update(
@@ -201,7 +201,7 @@ def update(
201201
"""
202202
# Lazy initialization
203203
if not self.is_initialized:
204-
self.lazy_initialization(key_states)
204+
self.lazy_initialization(key_states, value_states)
205205

206206
self.cumulative_length += key_states.shape[-2]
207207

@@ -400,7 +400,7 @@ def update(
400400
"""
401401
# Lazy initialization
402402
if not self.is_initialized:
403-
self.lazy_initialization(key_states)
403+
self.lazy_initialization(key_states, value_states)
404404

405405
# Some old models give None for `cache_position` or even omit passing `cache_kwargs` when used as cross-attention,
406406
# in which case we should copy the whole Layer (key_states.shape[-2] == self.max_cache_len)
@@ -535,7 +535,7 @@ def update(
535535

536536
# Lazy initialization
537537
if not self.is_initialized:
538-
self.lazy_initialization(key_states)
538+
self.lazy_initialization(key_states, value_states)
539539
self._quantized_keys = self._quantize(key_states.contiguous(), axis=self.axis_key)
540540
self._quantized_values = self._quantize(value_states.contiguous(), axis=self.axis_value)
541541
return key_states, value_states
@@ -797,10 +797,10 @@ def early_initialization(
797797
# Note that the initialization needs all dimensions (except -2), as well as device and dtype, so we use
798798
# this fake tensor approach. It has size 0 on the -2 dimension, so it does not allocate any data (it only
799799
# creates an empty tensor with correct shape, dtype and device), which is very efficient and practical
800-
fake_keys_tensor = torch.zeros((batch_size, num_heads, 0, head_dim), dtype=dtype, device=device)
800+
fake_kv_tensor = torch.zeros((batch_size, num_heads, 0, head_dim), dtype=dtype, device=device)
801801
# Init all layers
802802
for layer in self.layers:
803-
layer.lazy_initialization(fake_keys_tensor)
803+
layer.lazy_initialization(fake_kv_tensor, fake_kv_tensor)
804804

805805
def get_seq_length(self, layer_idx: int = 0) -> int:
806806
"""Returns the sequence length of the cache for the given layer."""

0 commit comments

Comments
 (0)