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1 |
+
import copy
|
| 2 |
+
import importlib.metadata
|
| 3 |
+
import json
|
| 4 |
+
import os
|
| 5 |
+
from dataclasses import dataclass
|
| 6 |
+
from typing import Any, Dict, List, Optional, Tuple, Union
|
| 7 |
+
|
| 8 |
+
import torch
|
| 9 |
+
from packaging import version
|
| 10 |
+
|
| 11 |
+
from transformers.configuration_utils import PretrainedConfig
|
| 12 |
+
from transformers.utils import is_torchdynamo_compiling, logging
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
logger = logging.get_logger(__name__)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class Cache(torch.nn.Module):
|
| 19 |
+
"""
|
| 20 |
+
Base, abstract class for all caches. The actual data structure is specific to each subclass.
|
| 21 |
+
"""
|
| 22 |
+
|
| 23 |
+
def __init__(self):
|
| 24 |
+
super().__init__()
|
| 25 |
+
|
| 26 |
+
def update(
|
| 27 |
+
self,
|
| 28 |
+
key_states: torch.Tensor,
|
| 29 |
+
value_states: torch.Tensor,
|
| 30 |
+
layer_idx: int,
|
| 31 |
+
cache_kwargs: Optional[Dict[str, Any]] = None,
|
| 32 |
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
| 33 |
+
"""
|
| 34 |
+
Updates the cache with the new `key_states` and `value_states` for the layer `layer_idx`.
|
| 35 |
+
|
| 36 |
+
Parameters:
|
| 37 |
+
key_states (`torch.Tensor`):
|
| 38 |
+
The new key states to cache.
|
| 39 |
+
value_states (`torch.Tensor`):
|
| 40 |
+
The new value states to cache.
|
| 41 |
+
layer_idx (`int`):
|
| 42 |
+
The index of the layer to cache the states for.
|
| 43 |
+
cache_kwargs (`Dict[str, Any]`, `optional`):
|
| 44 |
+
Additional arguments for the cache subclass. These are specific to each subclass and allow new types of
|
| 45 |
+
cache to be created.
|
| 46 |
+
|
| 47 |
+
Return:
|
| 48 |
+
A tuple containing the updated key and value states.
|
| 49 |
+
"""
|
| 50 |
+
raise NotImplementedError("Make sure to implement `update` in a subclass.")
|
| 51 |
+
|
| 52 |
+
def get_seq_length(self, layer_idx: Optional[int] = 0) -> int:
|
| 53 |
+
"""Returns the sequence length of the cached states. A layer index can be optionally passed."""
|
| 54 |
+
# TODO: deprecate this function in favor of `cache_position`
|
| 55 |
+
raise NotImplementedError("Make sure to implement `get_seq_length` in a subclass.")
|
| 56 |
+
|
| 57 |
+
def get_max_length(self) -> Optional[int]:
|
| 58 |
+
"""Returns the maximum sequence length of the cached states, if there is any."""
|
| 59 |
+
raise NotImplementedError("Make sure to implement `get_max_length` in a subclass.")
|
| 60 |
+
|
| 61 |
+
def get_usable_length(self, new_seq_length: int, layer_idx: Optional[int] = 0) -> int:
|
| 62 |
+
"""Given the sequence length of the new inputs, returns the usable length of the cache."""
|
| 63 |
+
# Cache without size limit -> all cache is usable
|
| 64 |
+
# Cache with size limit -> if the length cache plus the length of the new inputs is larger the maximum cache
|
| 65 |
+
# length, we will need to evict part of the cache (and thus not all cache is usable)
|
| 66 |
+
max_length = self.get_max_length()
|
| 67 |
+
previous_seq_length = self.get_seq_length(layer_idx)
|
| 68 |
+
if max_length is not None and previous_seq_length + new_seq_length > max_length:
|
| 69 |
+
return max_length - new_seq_length
|
| 70 |
+
return previous_seq_length
|
| 71 |
+
|
| 72 |
+
def reorder_cache(self, beam_idx: torch.LongTensor):
|
| 73 |
+
"""Reorders the cache for beam search, given the selected beam indices."""
|
| 74 |
+
for layer_idx in range(len(self.key_cache)):
|
| 75 |
+
device = self.key_cache[layer_idx].device
|
| 76 |
+
self.key_cache[layer_idx] = self.key_cache[layer_idx].index_select(0, beam_idx.to(device))
|
| 77 |
+
device = self.value_cache[layer_idx].device
|
| 78 |
+
self.value_cache[layer_idx] = self.value_cache[layer_idx].index_select(0, beam_idx.to(device))
|
| 79 |
+
|
| 80 |
+
@property
|
| 81 |
+
def seen_tokens(self):
|
| 82 |
+
logger.warning_once(
|
| 83 |
+
"The `seen_tokens` attribute is deprecated and will be removed in v4.41. Use the `cache_position` "
|
| 84 |
+
"model input instead."
|
| 85 |
+
)
|
| 86 |
+
if hasattr(self, "_seen_tokens"):
|
| 87 |
+
return self._seen_tokens
|
| 88 |
+
else:
|
| 89 |
+
return None
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
@dataclass
|
| 93 |
+
class CacheConfig:
|
| 94 |
+
"""
|
| 95 |
+
Base class for cache configs
|
| 96 |
+
"""
|
| 97 |
+
|
| 98 |
+
cache_implementation: None
|
| 99 |
+
|
| 100 |
+
@classmethod
|
| 101 |
+
def from_dict(cls, config_dict, **kwargs):
|
| 102 |
+
"""
|
| 103 |
+
Constructs a CacheConfig instance from a dictionary of parameters.
|
| 104 |
+
Args:
|
| 105 |
+
config_dict (Dict[str, Any]): Dictionary containing configuration parameters.
|
| 106 |
+
**kwargs: Additional keyword arguments to override dictionary values.
|
| 107 |
+
|
| 108 |
+
Returns:
|
| 109 |
+
CacheConfig: Instance of CacheConfig constructed from the dictionary.
|
| 110 |
+
"""
|
| 111 |
+
config = cls(**config_dict)
|
| 112 |
+
to_remove = []
|
| 113 |
+
for key, value in kwargs.items():
|
| 114 |
+
if hasattr(config, key):
|
| 115 |
+
setattr(config, key, value)
|
| 116 |
+
to_remove.append(key)
|
| 117 |
+
for key in to_remove:
|
| 118 |
+
kwargs.pop(key, None)
|
| 119 |
+
return config
|
| 120 |
+
|
| 121 |
+
# Copied from transformers.utils.quantization_config.QuantizationConfigMixin.to_json_file
|
| 122 |
+
def to_json_file(self, json_file_path: Union[str, os.PathLike]):
|
| 123 |
+
"""
|
| 124 |
+
Save this instance to a JSON file.
|
| 125 |
+
|
| 126 |
+
Args:
|
| 127 |
+
json_file_path (`str` or `os.PathLike`):
|
| 128 |
+
Path to the JSON file in which this configuration instance's parameters will be saved.
|
| 129 |
+
use_diff (`bool`, *optional*, defaults to `True`):
|
| 130 |
+
If set to `True`, only the difference between the config instance and the default
|
| 131 |
+
`QuantizationConfig()` is serialized to JSON file.
|
| 132 |
+
"""
|
| 133 |
+
with open(json_file_path, "w", encoding="utf-8") as writer:
|
| 134 |
+
config_dict = self.to_dict()
|
| 135 |
+
json_string = json.dumps(config_dict, indent=2, sort_keys=True) + "\n"
|
| 136 |
+
|
| 137 |
+
writer.write(json_string)
|
| 138 |
+
|
| 139 |
+
# Copied from transformers.utils.quantization_config.QuantizationConfigMixin.to_dict
|
| 140 |
+
def to_dict(self) -> Dict[str, Any]:
|
| 141 |
+
"""
|
| 142 |
+
Serializes this instance to a Python dictionary. Returns:
|
| 143 |
+
`Dict[str, Any]`: Dictionary of all the attributes that make up this configuration instance.
|
| 144 |
+
"""
|
| 145 |
+
return copy.deepcopy(self.__dict__)
|
| 146 |
+
|
| 147 |
+
# Copied from transformers.utils.quantization_config.QuantizationConfigMixin.__iter__
|
| 148 |
+
def __iter__(self):
|
| 149 |
+
"""allows `dict(obj)` for situations where obj may be a dict or QuantizationConfigMixin"""
|
| 150 |
+
for attr, value in copy.deepcopy(self.__dict__).items():
|
| 151 |
+
yield attr, value
|
| 152 |
+
|
| 153 |
+
# Copied from transformers.utils.quantization_config.QuantizationConfigMixin.__repr__
|
| 154 |
+
def __repr__(self):
|
| 155 |
+
return f"{self.__class__.__name__} {self.to_json_string()}"
|
| 156 |
+
|
| 157 |
+
def to_json_string(self):
|
| 158 |
+
"""
|
| 159 |
+
Serializes this instance to a JSON formatted string.
|
| 160 |
+
Returns:
|
| 161 |
+
str: JSON formatted string representing the configuration instance.
|
| 162 |
+
"""
|
| 163 |
+
return json.dumps(self.__dict__, indent=2) + "\n"
|
| 164 |
+
|
| 165 |
+
# Copied from transformers.utils.quantization_config.QuantizationConfigMixin.update
|
| 166 |
+
def update(self, **kwargs):
|
| 167 |
+
"""
|
| 168 |
+
Updates attributes of this class instance with attributes from `kwargs` if they match existing attributes,
|
| 169 |
+
returning all the unused kwargs.
|
| 170 |
+
|
| 171 |
+
Args:
|
| 172 |
+
kwargs (`Dict[str, Any]`):
|
| 173 |
+
Dictionary of attributes to tentatively update this class.
|
| 174 |
+
|
| 175 |
+
Returns:
|
| 176 |
+
`Dict[str, Any]`: Dictionary containing all the key-value pairs that were not used to update the instance.
|
| 177 |
+
"""
|
| 178 |
+
to_remove = []
|
| 179 |
+
for key, value in kwargs.items():
|
| 180 |
+
if hasattr(self, key):
|
| 181 |
+
setattr(self, key, value)
|
| 182 |
+
to_remove.append(key)
|
| 183 |
+
|
| 184 |
+
# Remove all the attributes that were updated, without modifying the input dict
|
| 185 |
+
unused_kwargs = {key: value for key, value in kwargs.items() if key not in to_remove}
|
| 186 |
+
return unused_kwargs
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
class DynamicCache(Cache):
|
| 190 |
+
"""
|
| 191 |
+
A cache that grows dynamically as more tokens are generated. This is the default for generative models.
|
| 192 |
+
|
| 193 |
+
It stores the Key and Value states as a list of tensors, one for each layer. The expected shape for each tensor is
|
| 194 |
+
`[batch_size, num_heads, seq_len, head_dim]`.
|
| 195 |
+
|
| 196 |
+
Example:
|
| 197 |
+
|
| 198 |
+
```python
|
| 199 |
+
>>> from transformers import AutoTokenizer, AutoModelForCausalLM, DynamicCache
|
| 200 |
+
|
| 201 |
+
>>> model = AutoModelForCausalLM.from_pretrained("openai-community/gpt2")
|
| 202 |
+
>>> tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2")
|
| 203 |
+
|
| 204 |
+
>>> inputs = tokenizer(text="My name is GPT2", return_tensors="pt")
|
| 205 |
+
|
| 206 |
+
>>> # Prepare a cache class and pass it to model's forward
|
| 207 |
+
>>> past_key_values = DynamicCache()
|
| 208 |
+
>>> outputs = model(**inputs, past_key_values=past_key_values, use_cache=True)
|
| 209 |
+
>>> past_kv_length = outputs.past_key_values # access cache filled with key/values from generation
|
| 210 |
+
```
|
| 211 |
+
"""
|
| 212 |
+
|
| 213 |
+
def __init__(self) -> None:
|
| 214 |
+
super().__init__()
|
| 215 |
+
self.key_cache: List[torch.Tensor] = []
|
| 216 |
+
self.value_cache: List[torch.Tensor] = []
|
| 217 |
+
self._seen_tokens = 0 # Used in `generate` to keep tally of how many tokens the cache has seen
|
| 218 |
+
|
| 219 |
+
def __getitem__(self, layer_idx: int) -> List[Tuple[torch.Tensor]]:
|
| 220 |
+
"""
|
| 221 |
+
Support for backwards-compatible `past_key_value` indexing, e.g. `past_key_value[0][0].shape[2]` to get the
|
| 222 |
+
sequence length.
|
| 223 |
+
"""
|
| 224 |
+
if layer_idx < len(self):
|
| 225 |
+
return (self.key_cache[layer_idx], self.value_cache[layer_idx])
|
| 226 |
+
else:
|
| 227 |
+
raise KeyError(f"Cache only has {len(self)} layers, attempted to access layer with index {layer_idx}")
|
| 228 |
+
|
| 229 |
+
def __iter__(self):
|
| 230 |
+
"""
|
| 231 |
+
Support for backwards-compatible `past_key_value` iteration, e.g. `for x in past_key_value:` to iterate over
|
| 232 |
+
keys and values
|
| 233 |
+
"""
|
| 234 |
+
for layer_idx in range(len(self)):
|
| 235 |
+
yield (self.key_cache[layer_idx], self.value_cache[layer_idx])
|
| 236 |
+
|
| 237 |
+
def __len__(self):
|
| 238 |
+
"""
|
| 239 |
+
Support for backwards-compatible `past_key_value` length, e.g. `len(past_key_value)`. This value corresponds
|
| 240 |
+
to the number of layers in the model.
|
| 241 |
+
"""
|
| 242 |
+
return len(self.key_cache)
|
| 243 |
+
|
| 244 |
+
def update(
|
| 245 |
+
self,
|
| 246 |
+
key_states: torch.Tensor,
|
| 247 |
+
value_states: torch.Tensor,
|
| 248 |
+
layer_idx: int,
|
| 249 |
+
cache_kwargs: Optional[Dict[str, Any]] = None,
|
| 250 |
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
| 251 |
+
"""
|
| 252 |
+
Updates the cache with the new `key_states` and `value_states` for the layer `layer_idx`.
|
| 253 |
+
|
| 254 |
+
Parameters:
|
| 255 |
+
key_states (`torch.Tensor`):
|
| 256 |
+
The new key states to cache.
|
| 257 |
+
value_states (`torch.Tensor`):
|
| 258 |
+
The new value states to cache.
|
| 259 |
+
layer_idx (`int`):
|
| 260 |
+
The index of the layer to cache the states for.
|
| 261 |
+
cache_kwargs (`Dict[str, Any]`, `optional`):
|
| 262 |
+
Additional arguments for the cache subclass. No additional arguments are used in `DynamicCache`.
|
| 263 |
+
|
| 264 |
+
Return:
|
| 265 |
+
A tuple containing the updated key and value states.
|
| 266 |
+
"""
|
| 267 |
+
# Update the number of seen tokens
|
| 268 |
+
if layer_idx == 0:
|
| 269 |
+
self._seen_tokens += key_states.shape[-2]
|
| 270 |
+
|
| 271 |
+
# Update the cache
|
| 272 |
+
if len(self.key_cache) <= layer_idx:
|
| 273 |
+
self.key_cache.append(key_states)
|
| 274 |
+
self.value_cache.append(value_states)
|
| 275 |
+
else:
|
| 276 |
+
self.key_cache[layer_idx] = torch.cat([self.key_cache[layer_idx], key_states], dim=-2)
|
| 277 |
+
self.value_cache[layer_idx] = torch.cat([self.value_cache[layer_idx], value_states], dim=-2)
|
| 278 |
+
|
| 279 |
+
return self.key_cache[layer_idx], self.value_cache[layer_idx]
|
| 280 |
+
|
| 281 |
+
def get_seq_length(self, layer_idx: Optional[int] = 0) -> int:
|
| 282 |
+
"""Returns the sequence length of the cached states. A layer index can be optionally passed."""
|
| 283 |
+
# TODO: deprecate this function in favor of `cache_position`
|
| 284 |
+
if len(self.key_cache) <= layer_idx:
|
| 285 |
+
return 0
|
| 286 |
+
return self.key_cache[layer_idx].shape[-2]
|
| 287 |
+
|
| 288 |
+
def get_max_length(self) -> Optional[int]:
|
| 289 |
+
"""Returns the maximum sequence length of the cached states. DynamicCache does not have a maximum length."""
|
| 290 |
+
return None
|
| 291 |
+
|
| 292 |
+
def to_legacy_cache(self) -> Tuple[Tuple[torch.Tensor], Tuple[torch.Tensor]]:
|
| 293 |
+
"""Converts the `DynamicCache` instance into the its equivalent in the legacy cache format. Used for
|
| 294 |
+
backward compatibility."""
|
| 295 |
+
legacy_cache = ()
|
| 296 |
+
for layer_idx in range(len(self)):
|
| 297 |
+
legacy_cache += ((self.key_cache[layer_idx], self.value_cache[layer_idx]),)
|
| 298 |
+
return legacy_cache
|
| 299 |
+
|
| 300 |
+
@classmethod
|
| 301 |
+
def from_legacy_cache(cls, past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None) -> "DynamicCache":
|
| 302 |
+
"""Converts a cache in the legacy cache format into an equivalent `DynamicCache`. Used for
|
| 303 |
+
backward compatibility."""
|
| 304 |
+
cache = cls()
|
| 305 |
+
if past_key_values is not None:
|
| 306 |
+
for layer_idx in range(len(past_key_values)):
|
| 307 |
+
key_states, value_states = past_key_values[layer_idx]
|
| 308 |
+
cache.update(key_states, value_states, layer_idx)
|
| 309 |
+
return cache
|
| 310 |
+
|
| 311 |
+
def crop(self, max_length: int):
|
| 312 |
+
"""Crop the past key values up to a new `max_length` in terms of tokens. `max_length` can also be
|
| 313 |
+
negative to remove `max_length` tokens. This is used in assisted decoding and contrastive search."""
|
| 314 |
+
# In case it is negative
|
| 315 |
+
if max_length < 0:
|
| 316 |
+
max_length = self.get_seq_length() - abs(max_length)
|
| 317 |
+
|
| 318 |
+
if self.get_seq_length() <= max_length:
|
| 319 |
+
return
|
| 320 |
+
|
| 321 |
+
self._seen_tokens = max_length
|
| 322 |
+
for idx in range(len(self.key_cache)):
|
| 323 |
+
self.key_cache[idx] = self.key_cache[idx][..., :max_length, :]
|
| 324 |
+
self.value_cache[idx] = self.value_cache[idx][..., :max_length, :]
|
| 325 |
+
|
| 326 |
+
def batch_split(self, full_batch_size: int, split_size: int) -> List["DynamicCache"]:
|
| 327 |
+
"""Split the current instance into a list of `DynamicCache` by the batch size. This will be used by
|
| 328 |
+
`_split_model_inputs()` in `generation.utils`"""
|
| 329 |
+
out = []
|
| 330 |
+
for i in range(0, full_batch_size, split_size):
|
| 331 |
+
current_split = DynamicCache()
|
| 332 |
+
current_split._seen_tokens = self._seen_tokens
|
| 333 |
+
current_split.key_cache = [tensor[i : i + split_size] for tensor in self.key_cache]
|
| 334 |
+
current_split.value_cache = [tensor[i : i + split_size] for tensor in self.value_cache]
|
| 335 |
+
out.append(current_split)
|
| 336 |
+
return out
|
| 337 |
+
|
| 338 |
+
@classmethod
|
| 339 |
+
def from_batch_splits(cls, splits: List["DynamicCache"]) -> "DynamicCache":
|
| 340 |
+
"""This is the opposite of the above `batch_split()` method. This will be used by `stack_model_outputs` in
|
| 341 |
+
`generation.utils`"""
|
| 342 |
+
cache = cls()
|
| 343 |
+
for idx in range(len(splits[0])):
|
| 344 |
+
layer_keys = torch.cat([current.key_cache[idx] for current in splits], dim=0)
|
| 345 |
+
layer_values = torch.cat([current.value_cache[idx] for current in splits], dim=0)
|
| 346 |
+
cache.update(layer_keys, layer_values, idx)
|
| 347 |
+
return cache
|
| 348 |
+
|
| 349 |
+
def batch_repeat_interleave(self, repeats: int):
|
| 350 |
+
"""Repeat the cache `repeats` times in the batch dimension. Used in contrastive search."""
|
| 351 |
+
for layer_idx in range(len(self)):
|
| 352 |
+
self.key_cache[layer_idx] = self.key_cache[layer_idx].repeat_interleave(repeats, dim=0)
|
| 353 |
+
self.value_cache[layer_idx] = self.value_cache[layer_idx].repeat_interleave(repeats, dim=0)
|
| 354 |
+
|
| 355 |
+
def batch_select_indices(self, indices: torch.Tensor):
|
| 356 |
+
"""Only keep the `indices` in the batch dimension of the cache. Used in contrastive search."""
|
| 357 |
+
for layer_idx in range(len(self)):
|
| 358 |
+
self.key_cache[layer_idx] = self.key_cache[layer_idx][indices, ...]
|
| 359 |
+
self.value_cache[layer_idx] = self.value_cache[layer_idx][indices, ...]
|
| 360 |
+
|
| 361 |
+
|
| 362 |
+
class OffloadedCache(DynamicCache):
|
| 363 |
+
"""
|
| 364 |
+
A drop-in replacement for DynamicCache that conserves GPU memory at the expense of more CPU memory.
|
| 365 |
+
Useful for generating from models with very long context.
|
| 366 |
+
|
| 367 |
+
In addition to the default CUDA stream, where all forward() computations happen,
|
| 368 |
+
this class uses another stream, the prefetch stream, which it creates itself.
|
| 369 |
+
Since scheduling of operations on separate streams happens independently, this class uses
|
| 370 |
+
the prefetch stream to asynchronously prefetch the KV cache of layer k+1 when layer k is executing.
|
| 371 |
+
The movement of the layer k-1 cache to the CPU is handled by the default stream as a simple way to
|
| 372 |
+
ensure the eviction is scheduled after all computations on that cache are finished.
|
| 373 |
+
"""
|
| 374 |
+
|
| 375 |
+
def __init__(self) -> None:
|
| 376 |
+
if not torch.cuda.is_available():
|
| 377 |
+
raise RuntimeError("OffloadedCache can only be used with a GPU")
|
| 378 |
+
super().__init__()
|
| 379 |
+
self.original_device = []
|
| 380 |
+
self.prefetch_stream = torch.cuda.Stream()
|
| 381 |
+
self.beam_idx = None # used to delay beam search operations
|
| 382 |
+
|
| 383 |
+
def prefetch_layer(self, layer_idx: int):
|
| 384 |
+
"Starts prefetching the next layer cache"
|
| 385 |
+
if layer_idx < len(self):
|
| 386 |
+
with torch.cuda.stream(self.prefetch_stream):
|
| 387 |
+
# Prefetch next layer tensors to GPU
|
| 388 |
+
device = self.original_device[layer_idx]
|
| 389 |
+
self.key_cache[layer_idx] = self.key_cache[layer_idx].to(device, non_blocking=True)
|
| 390 |
+
self.value_cache[layer_idx] = self.value_cache[layer_idx].to(device, non_blocking=True)
|
| 391 |
+
|
| 392 |
+
def evict_previous_layer(self, layer_idx: int):
|
| 393 |
+
"Moves the previous layer cache to the CPU"
|
| 394 |
+
if len(self) > 2:
|
| 395 |
+
# We do it on the default stream so it occurs after all earlier computations on these tensors are done
|
| 396 |
+
prev_layer_idx = (layer_idx - 1) % len(self)
|
| 397 |
+
self.key_cache[prev_layer_idx] = self.key_cache[prev_layer_idx].to("cpu", non_blocking=True)
|
| 398 |
+
self.value_cache[prev_layer_idx] = self.value_cache[prev_layer_idx].to("cpu", non_blocking=True)
|
| 399 |
+
|
| 400 |
+
def __getitem__(self, layer_idx: int) -> List[Tuple[torch.Tensor]]:
|
| 401 |
+
"Gets the cache for this layer to the device. Prefetches the next and evicts the previous layer."
|
| 402 |
+
if layer_idx < len(self):
|
| 403 |
+
# Evict the previous layer if necessary
|
| 404 |
+
torch.cuda.current_stream().synchronize()
|
| 405 |
+
self.evict_previous_layer(layer_idx)
|
| 406 |
+
# Load current layer cache to its original device if not already there
|
| 407 |
+
original_device = self.original_device[layer_idx]
|
| 408 |
+
self.prefetch_stream.synchronize()
|
| 409 |
+
key_tensor = self.key_cache[layer_idx]
|
| 410 |
+
value_tensor = self.value_cache[layer_idx]
|
| 411 |
+
# Now deal with beam search ops which were delayed
|
| 412 |
+
if self.beam_idx is not None:
|
| 413 |
+
self.beam_idx = self.beam_idx.to(original_device)
|
| 414 |
+
key_tensor = key_tensor.index_select(0, self.beam_idx)
|
| 415 |
+
value_tensor = value_tensor.index_select(0, self.beam_idx)
|
| 416 |
+
# Prefetch the next layer
|
| 417 |
+
self.prefetch_layer((layer_idx + 1) % len(self))
|
| 418 |
+
return (key_tensor, value_tensor)
|
| 419 |
+
else:
|
| 420 |
+
raise KeyError(f"Cache only has {len(self)} layers, attempted to access layer with index {layer_idx}")
|
| 421 |
+
|
| 422 |
+
def reorder_cache(self, beam_idx: torch.LongTensor):
|
| 423 |
+
"""Saves the beam indices and reorders the cache when the tensor is back to its device."""
|
| 424 |
+
# We delay this operation until the tensors are back to their original
|
| 425 |
+
# device because performing torch.index_select on the CPU is very slow
|
| 426 |
+
del self.beam_idx
|
| 427 |
+
self.beam_idx = beam_idx.clone()
|
| 428 |
+
|
| 429 |
+
def update(
|
| 430 |
+
self,
|
| 431 |
+
key_states: torch.Tensor,
|
| 432 |
+
value_states: torch.Tensor,
|
| 433 |
+
layer_idx: int,
|
| 434 |
+
cache_kwargs: Optional[Dict[str, Any]] = None,
|
| 435 |
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
| 436 |
+
"""
|
| 437 |
+
Updates the cache with the new `key_states` and `value_states` for the layer `layer_idx`.
|
| 438 |
+
Parameters:
|
| 439 |
+
key_states (`torch.Tensor`):
|
| 440 |
+
The new key states to cache.
|
| 441 |
+
value_states (`torch.Tensor`):
|
| 442 |
+
The new value states to cache.
|
| 443 |
+
layer_idx (`int`):
|
| 444 |
+
The index of the layer to cache the states for.
|
| 445 |
+
cache_kwargs (`Dict[str, Any]`, `optional`):
|
| 446 |
+
Additional arguments for the cache subclass. No additional arguments are used in `OffloadedCache`.
|
| 447 |
+
Return:
|
| 448 |
+
A tuple containing the updated key and value states.
|
| 449 |
+
"""
|
| 450 |
+
# Update the number of seen tokens
|
| 451 |
+
if layer_idx == 0:
|
| 452 |
+
self._seen_tokens += key_states.shape[-2]
|
| 453 |
+
|
| 454 |
+
# Update the cache
|
| 455 |
+
if len(self.key_cache) <= layer_idx:
|
| 456 |
+
self.key_cache.append(key_states)
|
| 457 |
+
self.value_cache.append(value_states)
|
| 458 |
+
self.original_device.append(key_states.device)
|
| 459 |
+
self.evict_previous_layer(layer_idx)
|
| 460 |
+
else:
|
| 461 |
+
key_tensor, value_tensor = self[layer_idx]
|
| 462 |
+
self.key_cache[layer_idx] = torch.cat([key_tensor, key_states], dim=-2)
|
| 463 |
+
self.value_cache[layer_idx] = torch.cat([value_tensor, value_states], dim=-2)
|
| 464 |
+
|
| 465 |
+
return self.key_cache[layer_idx], self.value_cache[layer_idx]
|
| 466 |
+
|
| 467 |
+
# According to https://docs.python.org/3/library/exceptions.html#NotImplementedError
|
| 468 |
+
# if a method is not supposed to be supported in a subclass we should set it to None
|
| 469 |
+
from_legacy_cache = None
|
| 470 |
+
|
| 471 |
+
to_legacy_cache = None
|
| 472 |
+
|
| 473 |
+
|
| 474 |
+
class SinkCache(Cache):
|
| 475 |
+
"""
|
| 476 |
+
A cache that as described in the [Attention Sinks paper](https://arxiv.org/abs/2309.17453). It allows the model to
|
| 477 |
+
generate beyond the length of its context window, without losing fluency in the conversation. As it discards past
|
| 478 |
+
tokens, the model will lose the ability to generate tokens that depend on the context that was discarded.
|
| 479 |
+
|
| 480 |
+
It stores the Key and Value states as a list of tensors, one for each layer. The expected shape for each tensor is
|
| 481 |
+
`[batch_size, num_heads, seq_len, head_dim]`.
|
| 482 |
+
|
| 483 |
+
Parameters:
|
| 484 |
+
window_length (`int`):
|
| 485 |
+
The length of the context window.
|
| 486 |
+
num_sink_tokens (`int`):
|
| 487 |
+
The number of sink tokens. See the original paper for more information.
|
| 488 |
+
|
| 489 |
+
Example:
|
| 490 |
+
|
| 491 |
+
```python
|
| 492 |
+
>>> from transformers import AutoTokenizer, AutoModelForCausalLM, SinkCache
|
| 493 |
+
|
| 494 |
+
>>> model = AutoModelForCausalLM.from_pretrained("openai-community/gpt2")
|
| 495 |
+
>>> tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2")
|
| 496 |
+
|
| 497 |
+
>>> inputs = tokenizer(text="My name is GPT2", return_tensors="pt")
|
| 498 |
+
|
| 499 |
+
>>> # Prepare a cache class and pass it to model's forward
|
| 500 |
+
>>> past_key_values = SinkCache(window_length=256, num_sink_tokens=4)
|
| 501 |
+
>>> outputs = model(**inputs, past_key_values=past_key_values, use_cache=True)
|
| 502 |
+
>>> past_kv_length = outputs.past_key_values # access cache filled with key/values from generation
|
| 503 |
+
```
|
| 504 |
+
"""
|
| 505 |
+
|
| 506 |
+
def __init__(self, window_length: int, num_sink_tokens: int) -> None:
|
| 507 |
+
super().__init__()
|
| 508 |
+
self.key_cache: List[torch.Tensor] = []
|
| 509 |
+
self.value_cache: List[torch.Tensor] = []
|
| 510 |
+
self.window_length = window_length
|
| 511 |
+
self.num_sink_tokens = num_sink_tokens
|
| 512 |
+
self.cos_sin_rerotation_cache = {}
|
| 513 |
+
self._cos_cache = None
|
| 514 |
+
self._sin_cache = None
|
| 515 |
+
self._seen_tokens = 0 # Used in `generate` to keep tally of how many tokens the cache has seen
|
| 516 |
+
|
| 517 |
+
@staticmethod
|
| 518 |
+
def _rotate_half(x):
|
| 519 |
+
x1 = x[..., : x.shape[-1] // 2]
|
| 520 |
+
x2 = x[..., x.shape[-1] // 2 :]
|
| 521 |
+
return torch.cat((-x2, x1), dim=-1)
|
| 522 |
+
|
| 523 |
+
def _apply_key_rotary_pos_emb(
|
| 524 |
+
self, key_states: torch.Tensor, cos: torch.Tensor, sin: torch.Tensor
|
| 525 |
+
) -> torch.Tensor:
|
| 526 |
+
rotated_key_states = (key_states * cos) + (self._rotate_half(key_states) * sin)
|
| 527 |
+
return rotated_key_states
|
| 528 |
+
|
| 529 |
+
def _get_rerotation_cos_sin(
|
| 530 |
+
self, key_states: torch.Tensor, cos: torch.Tensor, sin: torch.Tensor
|
| 531 |
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
| 532 |
+
if key_states.shape[-2] not in self.cos_sin_rerotation_cache:
|
| 533 |
+
# Upcast to float32 temporarily for better accuracy
|
| 534 |
+
cos = cos.to(torch.float32)
|
| 535 |
+
sin = sin.to(torch.float32)
|
| 536 |
+
|
| 537 |
+
# Compute the cos and sin required for back- and forward-rotating to one position earlier in the sequence
|
| 538 |
+
original_cos = cos[self.num_sink_tokens + key_states.shape[-2] :]
|
| 539 |
+
shifted_cos = cos[self.num_sink_tokens : -key_states.shape[-2]]
|
| 540 |
+
original_sin = sin[self.num_sink_tokens + key_states.shape[-2] :]
|
| 541 |
+
shifted_sin = sin[self.num_sink_tokens : -key_states.shape[-2]]
|
| 542 |
+
rerotation_cos = original_cos * shifted_cos + original_sin * shifted_sin
|
| 543 |
+
rerotation_sin = -original_sin * shifted_cos + original_cos * shifted_sin
|
| 544 |
+
|
| 545 |
+
self.cos_sin_rerotation_cache[key_states.shape[-2]] = (
|
| 546 |
+
rerotation_cos.to(key_states.dtype).unsqueeze(0),
|
| 547 |
+
rerotation_sin.to(key_states.dtype).unsqueeze(0),
|
| 548 |
+
)
|
| 549 |
+
return self.cos_sin_rerotation_cache[key_states.shape[-2]]
|
| 550 |
+
|
| 551 |
+
def get_seq_length(self, layer_idx: Optional[int] = 0) -> int:
|
| 552 |
+
"""Returns the sequence length of the cached states. A layer index can be optionally passed."""
|
| 553 |
+
# TODO: deprecate this function in favor of `cache_position`
|
| 554 |
+
# Workaround to make 'key_states.shape[-2] + past_key_value.get_seq_length(self.layer_idx)' <= window_length
|
| 555 |
+
if len(self.key_cache) <= layer_idx:
|
| 556 |
+
return 0
|
| 557 |
+
return self.key_cache[layer_idx].shape[-2]
|
| 558 |
+
|
| 559 |
+
def get_max_length(self) -> Optional[int]:
|
| 560 |
+
"""Returns the maximum sequence length of the cached states."""
|
| 561 |
+
return self.window_length
|
| 562 |
+
|
| 563 |
+
def update(
|
| 564 |
+
self,
|
| 565 |
+
key_states: torch.Tensor,
|
| 566 |
+
value_states: torch.Tensor,
|
| 567 |
+
layer_idx: int,
|
| 568 |
+
cache_kwargs: Optional[Dict[str, Any]] = None,
|
| 569 |
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
| 570 |
+
"""
|
| 571 |
+
Updates the cache with the new `key_states` and `value_states` for the layer `layer_idx`.
|
| 572 |
+
|
| 573 |
+
Parameters:
|
| 574 |
+
key_states (`torch.Tensor`):
|
| 575 |
+
The new key states to cache.
|
| 576 |
+
value_states (`torch.Tensor`):
|
| 577 |
+
The new value states to cache.
|
| 578 |
+
layer_idx (`int`):
|
| 579 |
+
The index of the layer to cache the states for.
|
| 580 |
+
cache_kwargs (`Dict[str, Any]`, `optional`):
|
| 581 |
+
Additional arguments for the cache subclass. The following arguments can be used in `SinkCache`: `sin`,
|
| 582 |
+
`cos` and `partial_rotation_size`. These arguments are used with models using RoPE, to recompute the
|
| 583 |
+
rotation as the tokens are shifted.
|
| 584 |
+
|
| 585 |
+
Return:
|
| 586 |
+
A tuple containing the updated key and value states.
|
| 587 |
+
"""
|
| 588 |
+
# Optional kwargs for `SinkCache` -- needed on models using RoPE. `partial_rotation_size` is used on models
|
| 589 |
+
# with partially rotated position embeddings, like Phi or Persimmon.
|
| 590 |
+
sin = cache_kwargs.get("sin")
|
| 591 |
+
cos = cache_kwargs.get("cos")
|
| 592 |
+
partial_rotation_size = cache_kwargs.get("partial_rotation_size")
|
| 593 |
+
using_rope = cos is not None and sin is not None
|
| 594 |
+
|
| 595 |
+
# Update the number of seen tokens
|
| 596 |
+
if layer_idx == 0:
|
| 597 |
+
self._seen_tokens += key_states.shape[-2]
|
| 598 |
+
|
| 599 |
+
# Update the sin/cos cache, which holds sin/cos values for all possible positions
|
| 600 |
+
if using_rope and layer_idx == 0:
|
| 601 |
+
# BC: some models still pass `sin`/`cos` with 2 dims. In those models, they are the full sin/cos. Remove
|
| 602 |
+
# after all RoPE models have a llama-like cache utilization.
|
| 603 |
+
if cos.dim() == 2:
|
| 604 |
+
self._cos_cache = cos
|
| 605 |
+
self._sin_cache = sin
|
| 606 |
+
else:
|
| 607 |
+
if self._cos_cache is None:
|
| 608 |
+
self._cos_cache = cos[0, ...]
|
| 609 |
+
self._sin_cache = sin[0, ...]
|
| 610 |
+
elif self._cos_cache.shape[0] < self.window_length:
|
| 611 |
+
self._cos_cache = torch.cat([self._cos_cache, cos[0, ...]], dim=0)
|
| 612 |
+
self._sin_cache = torch.cat([self._sin_cache, sin[0, ...]], dim=0)
|
| 613 |
+
|
| 614 |
+
# [bsz, num_heads, seq_len, head_dim]
|
| 615 |
+
if len(self.key_cache) <= layer_idx:
|
| 616 |
+
# Empty cache
|
| 617 |
+
self.key_cache.append(key_states)
|
| 618 |
+
self.value_cache.append(value_states)
|
| 619 |
+
|
| 620 |
+
elif key_states.shape[-2] + self.get_seq_length(layer_idx) < self.window_length:
|
| 621 |
+
# Growing cache
|
| 622 |
+
self.key_cache[layer_idx] = torch.cat([self.key_cache[layer_idx], key_states], dim=-2)
|
| 623 |
+
self.value_cache[layer_idx] = torch.cat([self.value_cache[layer_idx], value_states], dim=-2)
|
| 624 |
+
|
| 625 |
+
else:
|
| 626 |
+
# Shifting cache
|
| 627 |
+
keys_to_keep = self.key_cache[layer_idx][
|
| 628 |
+
:, :, -self.window_length + self.num_sink_tokens + key_states.shape[-2] :
|
| 629 |
+
]
|
| 630 |
+
|
| 631 |
+
# On RoPE models, we need to recompute the Key rotation as the tokens are shifted
|
| 632 |
+
if using_rope:
|
| 633 |
+
rerotation_cos, rerotation_sin = self._get_rerotation_cos_sin(
|
| 634 |
+
key_states, self._cos_cache[: self.window_length], self._sin_cache[: self.window_length]
|
| 635 |
+
)
|
| 636 |
+
if partial_rotation_size is not None:
|
| 637 |
+
keys_to_keep, keys_pass = (
|
| 638 |
+
keys_to_keep[..., :partial_rotation_size],
|
| 639 |
+
keys_to_keep[..., partial_rotation_size:],
|
| 640 |
+
)
|
| 641 |
+
keys_to_keep = self._apply_key_rotary_pos_emb(keys_to_keep, rerotation_cos, rerotation_sin)
|
| 642 |
+
if partial_rotation_size is not None:
|
| 643 |
+
keys_to_keep = torch.cat((keys_to_keep, keys_pass), dim=-1)
|
| 644 |
+
|
| 645 |
+
# Concatenate sink tokens, shifted & rotated tokens (if needed), and new tokens
|
| 646 |
+
sink_keys = self.key_cache[layer_idx][:, :, : self.num_sink_tokens]
|
| 647 |
+
self.key_cache[layer_idx] = torch.cat([sink_keys, keys_to_keep, key_states], dim=-2)
|
| 648 |
+
|
| 649 |
+
sink_values = self.value_cache[layer_idx][:, :, : self.num_sink_tokens]
|
| 650 |
+
values_to_keep = self.value_cache[layer_idx][
|
| 651 |
+
:, :, -self.window_length + self.num_sink_tokens + value_states.shape[-2] :
|
| 652 |
+
]
|
| 653 |
+
self.value_cache[layer_idx] = torch.cat([sink_values, values_to_keep, value_states], dim=-2)
|
| 654 |
+
|
| 655 |
+
return self.key_cache[layer_idx], self.value_cache[layer_idx]
|
| 656 |
+
|
| 657 |
+
|
| 658 |
+
class StaticCache(Cache):
|
| 659 |
+
"""
|
| 660 |
+
Static Cache class to be used with `torch.compile(model)` and `torch.export()`.
|
| 661 |
+
|
| 662 |
+
Parameters:
|
| 663 |
+
config (`PretrainedConfig`):
|
| 664 |
+
The configuration file defining the shape-related attributes required to initialize the static cache.
|
| 665 |
+
max_batch_size (`int`):
|
| 666 |
+
The maximum batch size with which the model will be used.
|
| 667 |
+
max_cache_len (`int`):
|
| 668 |
+
The maximum sequence length with which the model will be used.
|
| 669 |
+
device (`torch.device`):
|
| 670 |
+
The device on which the cache should be initialized. Should be the same as the layer.
|
| 671 |
+
dtype (*optional*, defaults to `torch.float32`):
|
| 672 |
+
The default `dtype` to use when initializing the layer.
|
| 673 |
+
|
| 674 |
+
Example:
|
| 675 |
+
|
| 676 |
+
```python
|
| 677 |
+
>>> from transformers import AutoTokenizer, AutoModelForCausalLM, StaticCache
|
| 678 |
+
|
| 679 |
+
>>> model = AutoModelForCausalLM.from_pretrained("openai-community/gpt2")
|
| 680 |
+
>>> tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2")
|
| 681 |
+
|
| 682 |
+
>>> inputs = tokenizer(text="My name is GPT2", return_tensors="pt")
|
| 683 |
+
|
| 684 |
+
>>> # Prepare a cache class and pass it to model's forward
|
| 685 |
+
>>> # Leave empty space for 10 new tokens, which can be used when calling forward iteratively 10 times to generate
|
| 686 |
+
>>> max_generated_length = inputs.input_ids.shape[1] + 10
|
| 687 |
+
>>> past_key_values = StaticCache(config=model.config, max_batch_size=1, max_cache_len=max_generated_length, device=model.device, dtype=model.dtype)
|
| 688 |
+
>>> outputs = model(**inputs, past_key_values=past_key_values, use_cache=True)
|
| 689 |
+
>>> past_kv_length = outputs.past_key_values # access cache filled with key/values from generation
|
| 690 |
+
```
|
| 691 |
+
"""
|
| 692 |
+
|
| 693 |
+
def __init__(self, config: PretrainedConfig, max_batch_size: int, max_cache_len: int, device, dtype=None) -> None:
|
| 694 |
+
super().__init__()
|
| 695 |
+
self.max_batch_size = max_batch_size
|
| 696 |
+
self.max_cache_len = config.max_position_embeddings if max_cache_len is None else max_cache_len
|
| 697 |
+
# Some model define a custom `head_dim` != config.hidden_size // config.num_attention_heads
|
| 698 |
+
self.head_dim = (
|
| 699 |
+
config.head_dim if hasattr(config, "head_dim") else config.hidden_size // config.num_attention_heads
|
| 700 |
+
)
|
| 701 |
+
|
| 702 |
+
self.dtype = dtype if dtype is not None else torch.float32
|
| 703 |
+
self.num_key_value_heads = (
|
| 704 |
+
config.num_attention_heads if config.num_key_value_heads is None else config.num_key_value_heads
|
| 705 |
+
)
|
| 706 |
+
|
| 707 |
+
self.key_cache: List[torch.Tensor] = []
|
| 708 |
+
self.value_cache: List[torch.Tensor] = []
|
| 709 |
+
# Note: There will be significant perf decrease if switching to use 5D tensors instead.
|
| 710 |
+
cache_shape = (max_batch_size, self.num_key_value_heads, self.max_cache_len, self.head_dim)
|
| 711 |
+
for idx in range(config.num_hidden_layers):
|
| 712 |
+
new_layer_key_cache = torch.zeros(cache_shape, dtype=self.dtype, device=device)
|
| 713 |
+
new_layer_value_cache = torch.zeros(cache_shape, dtype=self.dtype, device=device)
|
| 714 |
+
# Notes:
|
| 715 |
+
# 1. `mark_static_address` is used to tag the cache as an fixed data pointer, preventing cuda graph
|
| 716 |
+
# breaks when updating the cache. It can't be used if the cache code is being compiled (but in that case
|
| 717 |
+
# it is not needed anyway)
|
| 718 |
+
# 2. `torch.export()` requires mutations to be registered as buffers.
|
| 719 |
+
if not is_torchdynamo_compiling():
|
| 720 |
+
self.register_buffer(f"key_cache_{idx}", torch.zeros(cache_shape, dtype=dtype, device=device))
|
| 721 |
+
self.register_buffer(f"value_cache_{idx}", torch.zeros(cache_shape, dtype=dtype, device=device))
|
| 722 |
+
new_layer_key_cache = getattr(self, f"key_cache_{idx}")
|
| 723 |
+
new_layer_value_cache = getattr(self, f"value_cache_{idx}")
|
| 724 |
+
torch._dynamo.mark_static_address(new_layer_key_cache)
|
| 725 |
+
torch._dynamo.mark_static_address(new_layer_value_cache)
|
| 726 |
+
self.key_cache.append(new_layer_key_cache)
|
| 727 |
+
self.value_cache.append(new_layer_value_cache)
|
| 728 |
+
self._seen_tokens = 0 # Used in `generate` to keep tally of how many tokens the cache has seen
|
| 729 |
+
|
| 730 |
+
def update(
|
| 731 |
+
self,
|
| 732 |
+
key_states: torch.Tensor,
|
| 733 |
+
value_states: torch.Tensor,
|
| 734 |
+
layer_idx: int,
|
| 735 |
+
cache_kwargs: Optional[Dict[str, Any]] = None,
|
| 736 |
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
| 737 |
+
"""
|
| 738 |
+
Updates the cache with the new `key_states` and `value_states` for the layer `layer_idx`.
|
| 739 |
+
It is VERY important to index using a tensor, otherwise you introduce a copy to the device.
|
| 740 |
+
|
| 741 |
+
Parameters:
|
| 742 |
+
key_states (`torch.Tensor`):
|
| 743 |
+
The new key states to cache.
|
| 744 |
+
value_states (`torch.Tensor`):
|
| 745 |
+
The new value states to cache.
|
| 746 |
+
layer_idx (`int`):
|
| 747 |
+
The index of the layer to cache the states for.
|
| 748 |
+
cache_kwargs (`Dict[str, Any]`, `optional`):
|
| 749 |
+
Additional arguments for the cache subclass. The `StaticCache` needs the `cache_position` input
|
| 750 |
+
to know how where to write in the cache.
|
| 751 |
+
|
| 752 |
+
Return:
|
| 753 |
+
A tuple containing the updated key and value states.
|
| 754 |
+
"""
|
| 755 |
+
# Update the number of seen tokens
|
| 756 |
+
if layer_idx == 0:
|
| 757 |
+
self._seen_tokens += key_states.shape[-2]
|
| 758 |
+
|
| 759 |
+
cache_position = cache_kwargs.get("cache_position")
|
| 760 |
+
self.key_cache[layer_idx] = self.key_cache[layer_idx].to(device=key_states.device)
|
| 761 |
+
self.value_cache[layer_idx] = self.value_cache[layer_idx].to(device=value_states.device)
|
| 762 |
+
k_out = self.key_cache[layer_idx]
|
| 763 |
+
v_out = self.value_cache[layer_idx]
|
| 764 |
+
|
| 765 |
+
if cache_position is None:
|
| 766 |
+
k_out.copy_(key_states)
|
| 767 |
+
v_out.copy_(value_states)
|
| 768 |
+
else:
|
| 769 |
+
# Note: here we use `tensor.index_copy_(dim, index, tensor)` that is equivalent to
|
| 770 |
+
# `tensor[:, :, index] = tensor`, but the first one is compile-friendly and it does explicitly an in-place
|
| 771 |
+
# operation, that avoids copies and uses less memory.
|
| 772 |
+
try:
|
| 773 |
+
k_out.index_copy_(2, cache_position, key_states)
|
| 774 |
+
v_out.index_copy_(2, cache_position, value_states)
|
| 775 |
+
except NotImplementedError:
|
| 776 |
+
# The operator 'aten::index_copy.out' is not currently implemented for the MPS device.
|
| 777 |
+
k_out[:, :, cache_position] = key_states
|
| 778 |
+
v_out[:, :, cache_position] = value_states
|
| 779 |
+
|
| 780 |
+
return k_out, v_out
|
| 781 |
+
|
| 782 |
+
def get_seq_length(self, layer_idx: Optional[int] = 0) -> int:
|
| 783 |
+
"""Returns the sequence length of the cached states that were seen by the model."""
|
| 784 |
+
# Occupied cache == any slot in the 3rd dim (sequence length) holds a non-zero value. To save on compute, let's
|
| 785 |
+
# limit the check to the first batch member and head dimension.
|
| 786 |
+
# TODO: deprecate this function in favor of `cache_position`
|
| 787 |
+
# return (self.key_cache[layer_idx][0, 0].any(dim=-1)).sum()
|
| 788 |
+
return self._seen_tokens
|
| 789 |
+
|
| 790 |
+
def get_max_length(self) -> Optional[int]:
|
| 791 |
+
"""Returns the maximum sequence length of the cached states."""
|
| 792 |
+
return self.max_cache_len
|
| 793 |
+
|
| 794 |
+
def reset(self):
|
| 795 |
+
self._seen_tokens = 0
|
| 796 |
+
"""Resets the cache values while preserving the objects"""
|
| 797 |
+
for layer_idx in range(len(self.key_cache)):
|
| 798 |
+
# In-place ops prevent breaking the static address
|
| 799 |
+
self.key_cache[layer_idx].zero_()
|
| 800 |
+
self.value_cache[layer_idx].zero_()
|
| 801 |
+
|
| 802 |
+
|
| 803 |
+
class SlidingWindowCache(StaticCache):
|
| 804 |
+
"""
|
| 805 |
+
Sliding Window Cache class to be used with `torch.compile` for models like Mistral that support sliding window attention.
|
| 806 |
+
Every time when we try to update the cache, we compute the `indices` based on `cache_position >= self.config.sliding_window - 1`,
|
| 807 |
+
if true(which means the cache can not hold all the old key value states and new states together because of the sliding window constraint),
|
| 808 |
+
we need to do a cycle shift based on `indices` to replace the oldest states by the new key value states passed in.
|
| 809 |
+
|
| 810 |
+
The `to_shift` is only true once we are above sliding_window. Thus with `sliding_window==64`:
|
| 811 |
+
|
| 812 |
+
indices = (slicing + to_shift[-1].int()-1) % self.config.sliding_window
|
| 813 |
+
tensor([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
|
| 814 |
+
19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
|
| 815 |
+
37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54,
|
| 816 |
+
55, 56, 57, 58, 59, 60, 61, 62, 63, 0])
|
| 817 |
+
|
| 818 |
+
We overwrite the cache using these, then we always write at cache_position (clamped to `sliding_window`)
|
| 819 |
+
|
| 820 |
+
Parameters:
|
| 821 |
+
config (`PretrainedConfig`):
|
| 822 |
+
The configuration file defining the shape-related attributes required to initialize the static cache.
|
| 823 |
+
max_batch_size (`int`):
|
| 824 |
+
The maximum batch size with which the model will be used.
|
| 825 |
+
max_cache_len (`int`):
|
| 826 |
+
The maximum sequence length with which the model will be used.
|
| 827 |
+
device (`torch.device`):
|
| 828 |
+
The device on which the cache should be initialized. Should be the same as the layer.
|
| 829 |
+
dtype (*optional*, defaults to `torch.float32`):
|
| 830 |
+
The default `dtype` to use when initializing the layer.
|
| 831 |
+
|
| 832 |
+
Example:
|
| 833 |
+
|
| 834 |
+
```python
|
| 835 |
+
>>> from transformers import AutoTokenizer, AutoModelForCausalLM, SlidingWindowCache
|
| 836 |
+
|
| 837 |
+
>>> model = AutoModelForCausalLM.from_pretrained("openai-community/gpt2")
|
| 838 |
+
>>> tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2")
|
| 839 |
+
|
| 840 |
+
>>> inputs = tokenizer(text="My name is GPT2", return_tensors="pt")
|
| 841 |
+
|
| 842 |
+
>>> # Prepare a cache class and pass it to model's forward
|
| 843 |
+
>>> # Leave empty space for 10 new tokens, which can be used when calling forward iteratively 10 times to generate
|
| 844 |
+
>>> max_generated_length = inputs.input_ids.shape[1] + 10
|
| 845 |
+
>>> past_key_values = SlidingWindowCache(config=model.config, max_batch_size=1, max_cache_len=max_generated_length, device=model.device, dtype=model.dtype)
|
| 846 |
+
>>> outputs = model(**inputs, past_key_values=past_key_values, use_cache=True)
|
| 847 |
+
>>> past_kv_length = outputs.past_key_values # access cache filled with key/values from generation
|
| 848 |
+
```
|
| 849 |
+
"""
|
| 850 |
+
|
| 851 |
+
def __init__(self, config: PretrainedConfig, max_batch_size: int, max_cache_len: int, device, dtype=None) -> None:
|
| 852 |
+
super().__init__(config, max_batch_size, max_cache_len, device, dtype)
|
| 853 |
+
if not hasattr(config, "sliding_window") or config.sliding_window is None:
|
| 854 |
+
raise ValueError(
|
| 855 |
+
"Setting `cache_implementation` to 'sliding_window' requires the model config supporting "
|
| 856 |
+
"sliding window attention, please check if there is a `sliding_window` field in the model "
|
| 857 |
+
"config and it's not set to None."
|
| 858 |
+
)
|
| 859 |
+
max_cache_len = min(config.sliding_window, max_cache_len)
|
| 860 |
+
super().__init__(
|
| 861 |
+
config=config, max_batch_size=max_batch_size, max_cache_len=max_cache_len, device=device, dtype=dtype
|
| 862 |
+
)
|
| 863 |
+
|
| 864 |
+
def update(
|
| 865 |
+
self,
|
| 866 |
+
key_states: torch.Tensor,
|
| 867 |
+
value_states: torch.Tensor,
|
| 868 |
+
layer_idx: int,
|
| 869 |
+
cache_kwargs: Optional[Dict[str, Any]] = None,
|
| 870 |
+
) -> Tuple[torch.Tensor]:
|
| 871 |
+
cache_position = cache_kwargs.get("cache_position")
|
| 872 |
+
k_out = self.key_cache[layer_idx]
|
| 873 |
+
v_out = self.value_cache[layer_idx]
|
| 874 |
+
|
| 875 |
+
# assume this only happens in prefill phase when prompt length > sliding_window_size (= max_cache_len)
|
| 876 |
+
if cache_position.shape[0] > self.max_cache_len:
|
| 877 |
+
k_out = key_states[:, :, -self.max_cache_len :, :]
|
| 878 |
+
v_out = value_states[:, :, -self.max_cache_len :, :]
|
| 879 |
+
# Assumption: caches are all zeros at this point, `+=` is equivalent to `=` but compile-friendly
|
| 880 |
+
self.key_cache[layer_idx] += k_out
|
| 881 |
+
self.value_cache[layer_idx] += v_out
|
| 882 |
+
# we should return the whole states instead of k_out, v_out to take the whole prompt
|
| 883 |
+
# into consideration when building kv cache instead of just throwing away tokens outside of the window
|
| 884 |
+
return key_states, value_states
|
| 885 |
+
|
| 886 |
+
slicing = torch.ones(self.max_cache_len, dtype=torch.long, device=value_states.device).cumsum(0)
|
| 887 |
+
cache_position = cache_position.clamp(0, self.max_cache_len - 1)
|
| 888 |
+
to_shift = cache_position >= self.max_cache_len - 1
|
| 889 |
+
indices = (slicing + to_shift[-1].int() - 1) % self.max_cache_len
|
| 890 |
+
|
| 891 |
+
k_out = k_out[:, :, indices]
|
| 892 |
+
v_out = v_out[:, :, indices]
|
| 893 |
+
|
| 894 |
+
try:
|
| 895 |
+
cache_position.to(device=k_out.device)
|
| 896 |
+
k_out.index_copy_(2, cache_position, key_states)
|
| 897 |
+
v_out.index_copy_(2, cache_position, value_states)
|
| 898 |
+
except NotImplementedError:
|
| 899 |
+
# The operator 'aten::index_copy.out' is not currently implemented for the MPS device.
|
| 900 |
+
k_out[:, :, cache_position] = key_states
|
| 901 |
+
v_out[:, :, cache_position] = value_states
|
| 902 |
+
|
| 903 |
+
# `_.zero()` followed by `+=` is equivalent `=`, but compile-friendly (without graph breaks due to assignment)
|
| 904 |
+
self.key_cache[layer_idx].zero_()
|
| 905 |
+
self.value_cache[layer_idx].zero_()
|
| 906 |
+
|
| 907 |
+
self.key_cache[layer_idx] += k_out
|
| 908 |
+
self.value_cache[layer_idx] += v_out
|
| 909 |
+
|
| 910 |
+
return k_out, v_out
|
| 911 |
+
|
| 912 |
+
def get_max_length(self) -> Optional[int]:
|
| 913 |
+
# in theory there is no limit because the sliding window size is fixed no matter how long the sentence is
|
| 914 |
+
return None
|
| 915 |
+
|
| 916 |
+
def reset(self):
|
| 917 |
+
for layer_idx in range(len(self.key_cache)):
|
| 918 |
+
# In-place ops prevent breaking the static address
|
| 919 |
+
self.key_cache[layer_idx].zero_()
|
| 920 |
+
self.value_cache[layer_idx].zero_()
|
| 921 |
+
|
| 922 |
+
|
| 923 |
+
class EncoderDecoderCache(Cache):
|
| 924 |
+
"""
|
| 925 |
+
Base, abstract class for all encoder-decoder caches. Can be used to hold combinations of self-attention and
|
| 926 |
+
cross-attention caches.
|
| 927 |
+
|
| 928 |
+
Example:
|
| 929 |
+
|
| 930 |
+
```python
|
| 931 |
+
>>> from transformers import AutoProcessor, AutoModelForCausalLM, DynamicCache, EncoderDecoderCache
|
| 932 |
+
|
| 933 |
+
>>> model = AutoModelForCausalLM.from_pretrained("openai/whisper-small")
|
| 934 |
+
>>> processor = AutoProcessor.from_pretrained("openai/whisper-small")
|
| 935 |
+
|
| 936 |
+
>>> inputs = processor(audio=YOUR-AUDIO, return_tensors="pt")
|
| 937 |
+
|
| 938 |
+
>>> # Prepare cache classes for encoder and decoder and pass it to model's forward
|
| 939 |
+
>>> self_attention_cache = DynamicCache()
|
| 940 |
+
>>> cross_attention_cache = DynamicCache()
|
| 941 |
+
>>> past_key_values = EncoderDecoderCache(self_attention_cache, cross_attention_cache)
|
| 942 |
+
>>> outputs = model(**inputs, past_key_values=past_key_values, use_cache=True)
|
| 943 |
+
>>> past_kv_length = outputs.past_key_values # access cache filled with key/values from generation
|
| 944 |
+
```
|
| 945 |
+
|
| 946 |
+
"""
|
| 947 |
+
|
| 948 |
+
def __init__(self, self_attention_cache: Cache, cross_attention_cache: Cache):
|
| 949 |
+
super().__init__()
|
| 950 |
+
self.self_attention_cache = self_attention_cache
|
| 951 |
+
self.cross_attention_cache = cross_attention_cache
|
| 952 |
+
|
| 953 |
+
self.is_updated = {}
|
| 954 |
+
for layer_idx in range(len(cross_attention_cache.key_cache)):
|
| 955 |
+
self.is_updated[layer_idx] = bool(cross_attention_cache.get_seq_length(layer_idx) > 0)
|
| 956 |
+
|
| 957 |
+
def __getitem__(self, layer_idx: int) -> List[Tuple[torch.Tensor]]:
|
| 958 |
+
"""
|
| 959 |
+
Support for backwards-compatible `past_key_value` indexing, e.g. `past_key_value[0][0].shape[2]` to get the
|
| 960 |
+
sequence length.
|
| 961 |
+
"""
|
| 962 |
+
if layer_idx < len(self):
|
| 963 |
+
return (
|
| 964 |
+
self.self_attention_cache.key_cache[layer_idx],
|
| 965 |
+
self.self_attention_cache.value_cache[layer_idx],
|
| 966 |
+
self.cross_attention_cache.key_cache[layer_idx],
|
| 967 |
+
self.cross_attention_cache.value_cache[layer_idx],
|
| 968 |
+
)
|
| 969 |
+
else:
|
| 970 |
+
raise KeyError(f"Cache only has {len(self)} layers, attempted to access layer with index {layer_idx}")
|
| 971 |
+
|
| 972 |
+
def __len__(self):
|
| 973 |
+
"""
|
| 974 |
+
Support for backwards-compatible `past_key_value` length, e.g. `len(past_key_value)`. This value corresponds
|
| 975 |
+
to the number of layers in the model.
|
| 976 |
+
"""
|
| 977 |
+
return len(self.self_attention_cache)
|
| 978 |
+
|
| 979 |
+
def to_legacy_cache(self) -> Tuple[Tuple[torch.Tensor], Tuple[torch.Tensor]]:
|
| 980 |
+
"""Converts the `EncoderDecoderCache` instance into its equivalent in the legacy cache format."""
|
| 981 |
+
legacy_cache = ()
|
| 982 |
+
if len(self.cross_attention_cache) > 0:
|
| 983 |
+
for self_attn, cross_attn in zip(
|
| 984 |
+
self.self_attention_cache.to_legacy_cache(), self.cross_attention_cache.to_legacy_cache()
|
| 985 |
+
):
|
| 986 |
+
legacy_cache += (self_attn + cross_attn,)
|
| 987 |
+
else:
|
| 988 |
+
legacy_cache = self.self_attention_cache.to_legacy_cache()
|
| 989 |
+
return legacy_cache
|
| 990 |
+
|
| 991 |
+
@classmethod
|
| 992 |
+
def from_legacy_cache(
|
| 993 |
+
cls, past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None
|
| 994 |
+
) -> "EncoderDecoderCache":
|
| 995 |
+
"""Converts a cache in the legacy cache format into an equivalent `EncoderDecoderCache`."""
|
| 996 |
+
cache = cls(self_attention_cache=DynamicCache(), cross_attention_cache=DynamicCache())
|
| 997 |
+
if past_key_values is not None:
|
| 998 |
+
for layer_idx in range(len(past_key_values)):
|
| 999 |
+
key_states, value_states = past_key_values[layer_idx][:2]
|
| 1000 |
+
cache.self_attention_cache.update(key_states, value_states, layer_idx)
|
| 1001 |
+
if len(past_key_values[layer_idx]) > 2:
|
| 1002 |
+
key_states, value_states = past_key_values[layer_idx][2:]
|
| 1003 |
+
cache.cross_attention_cache.update(key_states, value_states, layer_idx)
|
| 1004 |
+
cache.is_updated[layer_idx] = True
|
| 1005 |
+
return cache
|
| 1006 |
+
|
| 1007 |
+
def get_seq_length(self, layer_idx: Optional[int] = 0) -> int:
|
| 1008 |
+
"""Returns the sequence length of the cached states. A layer index can be optionally passed."""
|
| 1009 |
+
if len(self.self_attention_cache.key_cache) <= layer_idx:
|
| 1010 |
+
return 0
|
| 1011 |
+
return (self.self_attention_cache.key_cache[layer_idx][0, 0].any(dim=-1)).sum()
|
| 1012 |
+
|
| 1013 |
+
def reset(self):
|
| 1014 |
+
if hasattr(self.self_attention_cache, "reset"):
|
| 1015 |
+
self.self_attention_cache.reset()
|
| 1016 |
+
if hasattr(self.cross_attention_cache, "reset"):
|
| 1017 |
+
self.cross_attention_cache.reset()
|
| 1018 |
+
elif not hasattr(self.self_attention_cache, "reset") and not hasattr(self.cross_attention_cache, "reset"):
|
| 1019 |
+
raise ValueError(
|
| 1020 |
+
"Neither self nor cross-attention cache have valid `.reset()` methods. `.reset()` should "
|
| 1021 |
+
"only be called on compatible cache classes, such as `StaticCache` or `SlidingWindowCache`. "
|
| 1022 |
+
f"Got {self.self_attention_cache.__str__()} for the self attention cache and "
|
| 1023 |
+
f"{self.cross_attention_cache.__str__()} for the cross attention cache."
|
| 1024 |
+
)
|
| 1025 |
+
for layer_idx in self.is_updated:
|
| 1026 |
+
self.is_updated[layer_idx] = False
|
| 1027 |
+
|
| 1028 |
+
def reorder_cache(self, beam_idx: torch.LongTensor):
|
| 1029 |
+
"""Reorders the cache for beam search, given the selected beam indices."""
|
| 1030 |
+
self.self_attention_cache.reorder_cache(beam_idx)
|
| 1031 |
+
self.cross_attention_cache.reorder_cache(beam_idx)
|
| 1032 |
+
|
| 1033 |
+
def check_dynamic_cache(self, method: str):
|
| 1034 |
+
if not (
|
| 1035 |
+
isinstance(self.self_attention_cache, DynamicCache)
|
| 1036 |
+
and isinstance(self.cross_attention_cache, DynamicCache)
|
| 1037 |
+
):
|
| 1038 |
+
raise ValueError(
|
| 1039 |
+
f"`{method}` is only defined for dynamic cache, got {self.self_attention_cache.__str__()} for the self "
|
| 1040 |
+
f"attention cache and {self.cross_attention_cache.__str__()} for the cross attention cache."
|
| 1041 |
+
)
|
| 1042 |
+
|
| 1043 |
+
# TODO(gante, sanchit-gandhi): move following functionality into `.generate`
|
| 1044 |
+
def crop(self, maximum_length: int):
|
| 1045 |
+
"""Crop the past key values up to a new `maximum_length` in terms of tokens. `maximum_length` can also be
|
| 1046 |
+
negative to remove `maximum_length` tokens. This is used in assisted decoding and contrastive search."""
|
| 1047 |
+
self.check_dynamic_cache(self.crop.__name__)
|
| 1048 |
+
self.self_attention_cache.crop(maximum_length)
|
| 1049 |
+
|
| 1050 |
+
def batch_split(self, full_batch_size: int, split_size: int) -> "List[EncoderDecoderCache]":
|
| 1051 |
+
"""Split the current instance into a list of `DynamicCache` by the batch size. This will be used by
|
| 1052 |
+
`_split_model_inputs()` in `generation.utils`"""
|
| 1053 |
+
self.check_dynamic_cache(self.batch_split.__name__)
|
| 1054 |
+
self_attention_cache = self.self_attention_cache.batch_split(full_batch_size, split_size)
|
| 1055 |
+
cross_attention_cache = self.cross_attention_cache.batch_split(full_batch_size, split_size)
|
| 1056 |
+
|
| 1057 |
+
out = []
|
| 1058 |
+
for self_attn, cross_attn in zip(self_attention_cache, cross_attention_cache):
|
| 1059 |
+
out.append(EncoderDecoderCache(self_attn, cross_attn))
|
| 1060 |
+
return out
|
| 1061 |
+
|
| 1062 |
+
@classmethod
|
| 1063 |
+
def from_batch_splits(cls, splits: List["EncoderDecoderCache"]) -> "EncoderDecoderCache":
|
| 1064 |
+
"""This is the opposite of the above `batch_split()` method. This will be used by `stack_model_outputs` in
|
| 1065 |
+
`generation.utils`"""
|
| 1066 |
+
self_attention_cache = DynamicCache()
|
| 1067 |
+
cross_attention_cache = DynamicCache()
|
| 1068 |
+
for idx in range(len(splits[0])):
|
| 1069 |
+
layer_keys = torch.cat([current.self_attention_cache.key_cache[idx] for current in splits], dim=0)
|
| 1070 |
+
layer_values = torch.cat([current.self_attention_cache.value_cache[idx] for current in splits], dim=0)
|
| 1071 |
+
self_attention_cache.update(layer_keys, layer_values, idx)
|
| 1072 |
+
|
| 1073 |
+
layer_keys = torch.cat([current.cross_attention_cache.key_cache[idx] for current in splits], dim=0)
|
| 1074 |
+
layer_values = torch.cat([current.cross_attention_cache.value_cache[idx] for current in splits], dim=0)
|
| 1075 |
+
cross_attention_cache.update(layer_keys, layer_values, idx)
|
| 1076 |
+
return cls(self_attention_cache, cross_attention_cache)
|
| 1077 |
+
|
| 1078 |
+
def batch_repeat_interleave(self, repeats: int):
|
| 1079 |
+
"""Repeat the cache `repeats` times in the batch dimension. Used in contrastive search."""
|
| 1080 |
+
self.check_dynamic_cache(self.batch_repeat_interleave.__name__)
|
| 1081 |
+
self.self_attention_cache.batch_repeat_interleave(repeats)
|
| 1082 |
+
self.cross_attention_cache.batch_repeat_interleave(repeats)
|
| 1083 |
+
|
| 1084 |
+
def batch_select_indices(self, indices: torch.Tensor):
|
| 1085 |
+
"""Only keep the `indices` in the batch dimension of the cache. Used in contrastive search."""
|
| 1086 |
+
self.check_dynamic_cache(self.batch_select_indices.__name__)
|
| 1087 |
+
self.self_attention_cache.batch_select_indices(indices)
|
| 1088 |
+
self.cross_attention_cache.batch_select_indices(indices)
|
| 1089 |
+
|
| 1090 |
+
|
| 1091 |
+
class HybridCache(Cache):
|
| 1092 |
+
"""
|
| 1093 |
+
Hybrid Cache class to be used with `torch.compile` for Gemma2 models that alternate between a local sliding window attention
|
| 1094 |
+
and global attention in every other layer. Under the hood, Hybrid Cache leverages ["SlidingWindowCache"] for sliding window attention
|
| 1095 |
+
and ["StaticCache"] for global attention. For more information, see the documentation of each subcomponeent cache class.
|
| 1096 |
+
|
| 1097 |
+
Parameters:
|
| 1098 |
+
config (`PretrainedConfig):
|
| 1099 |
+
The configuration file defining the shape-related attributes required to initialize the static cache.
|
| 1100 |
+
max_batch_size (`int`):
|
| 1101 |
+
The maximum batch size with which the model will be used.
|
| 1102 |
+
max_cache_len (`int`):
|
| 1103 |
+
The maximum sequence length with which the model will be used.
|
| 1104 |
+
device (`torch.device`, *optional*, defaults to `"cpu"`):
|
| 1105 |
+
The device on which the cache should be initialized. Should be the same as the layer.
|
| 1106 |
+
dtype (*optional*, defaults to `torch.float32`):
|
| 1107 |
+
The default `dtype` to use when initializing the layer.
|
| 1108 |
+
|
| 1109 |
+
Example:
|
| 1110 |
+
|
| 1111 |
+
```python
|
| 1112 |
+
>>> from transformers import AutoTokenizer, AutoModelForCausalLM, HybridCache
|
| 1113 |
+
|
| 1114 |
+
>>> model = AutoModelForCausalLM.from_pretrained("google/gemma-2-9b")
|
| 1115 |
+
>>> tokenizer = AutoTokenizer.from_pretrained("google/gemma-2-9b")
|
| 1116 |
+
|
| 1117 |
+
>>> inputs = tokenizer(text="My name is Gemma", return_tensors="pt")
|
| 1118 |
+
|
| 1119 |
+
>>> # Prepare a cache class and pass it to model's forward
|
| 1120 |
+
>>> # Leave empty space for 10 new tokens, which can be used when calling forward iteratively 10 times to generate
|
| 1121 |
+
>>> max_generated_length = inputs.input_ids.shape[1] + 10
|
| 1122 |
+
>>> past_key_values = HybridCache(config=model.config, max_batch_size=1, max_cache_len=max_generated_length, device=model.device, dtype=model.dtype)
|
| 1123 |
+
>>> outputs = model(**inputs, past_key_values=past_key_values, use_cache=True)
|
| 1124 |
+
>>> past_kv_length = outputs.past_key_values # access cache filled with key/values from generation
|
| 1125 |
+
```
|
| 1126 |
+
"""
|
| 1127 |
+
|
| 1128 |
+
def __init__(self, config: PretrainedConfig, max_batch_size, max_cache_len, device="cpu", dtype=None) -> None:
|
| 1129 |
+
super().__init__()
|
| 1130 |
+
if not hasattr(config, "sliding_window") or config.sliding_window is None:
|
| 1131 |
+
raise ValueError(
|
| 1132 |
+
"Setting `cache_implementation` to 'sliding_window' requires the model config supporting "
|
| 1133 |
+
"sliding window attention, please check if there is a `sliding_window` field in the model "
|
| 1134 |
+
"config and it's not set to None."
|
| 1135 |
+
)
|
| 1136 |
+
self.max_cache_len = max_cache_len
|
| 1137 |
+
self.max_batch_size = max_batch_size
|
| 1138 |
+
# Some model define a custom `head_dim` != config.hidden_size // config.num_attention_heads
|
| 1139 |
+
self.head_dim = (
|
| 1140 |
+
config.head_dim if hasattr(config, "head_dim") else config.hidden_size // config.num_attention_heads
|
| 1141 |
+
)
|
| 1142 |
+
|
| 1143 |
+
self.dtype = dtype if dtype is not None else torch.float32
|
| 1144 |
+
self.num_key_value_heads = (
|
| 1145 |
+
config.num_attention_heads if config.num_key_value_heads is None else config.num_key_value_heads
|
| 1146 |
+
)
|
| 1147 |
+
self.is_sliding = torch.tensor(
|
| 1148 |
+
[not bool(i % 2) for i in range(config.num_hidden_layers)], dtype=torch.bool, device=device
|
| 1149 |
+
)
|
| 1150 |
+
self.key_cache: List[torch.Tensor] = []
|
| 1151 |
+
self.value_cache: List[torch.Tensor] = []
|
| 1152 |
+
global_cache_shape = (max_batch_size, self.num_key_value_heads, max_cache_len, self.head_dim)
|
| 1153 |
+
sliding_cache_shape = (
|
| 1154 |
+
max_batch_size,
|
| 1155 |
+
self.num_key_value_heads,
|
| 1156 |
+
min(config.sliding_window, max_cache_len),
|
| 1157 |
+
self.head_dim,
|
| 1158 |
+
)
|
| 1159 |
+
for i in range(config.num_hidden_layers):
|
| 1160 |
+
# Note: `mark_static_address` is used to tag the cache as an fixed data pointer, preventing cuda graph
|
| 1161 |
+
# breaks when updating the cache.
|
| 1162 |
+
cache_shape = global_cache_shape if not self.is_sliding[i] else sliding_cache_shape
|
| 1163 |
+
new_layer_key_cache = torch.zeros(cache_shape, dtype=self.dtype, device=device)
|
| 1164 |
+
new_layer_value_cache = torch.zeros(cache_shape, dtype=self.dtype, device=device)
|
| 1165 |
+
torch._dynamo.mark_static_address(new_layer_key_cache)
|
| 1166 |
+
torch._dynamo.mark_static_address(new_layer_value_cache)
|
| 1167 |
+
self.key_cache.append(new_layer_key_cache)
|
| 1168 |
+
self.value_cache.append(new_layer_value_cache)
|
| 1169 |
+
|
| 1170 |
+
def _sliding_update(self, cache_position, layer_idx, key_states, value_states, k_out, v_out, max_cache_len):
|
| 1171 |
+
if cache_position.shape[0] > max_cache_len:
|
| 1172 |
+
k_out = key_states[:, :, -max_cache_len:, :]
|
| 1173 |
+
v_out = value_states[:, :, -max_cache_len:, :]
|
| 1174 |
+
# Assumption: caches are all zeros at this point, `+=` is equivalent to `=` but compile-friendly
|
| 1175 |
+
self.key_cache[layer_idx] += k_out
|
| 1176 |
+
self.value_cache[layer_idx] += v_out
|
| 1177 |
+
# we should return the whole states instead of k_out, v_out to take the whole prompt
|
| 1178 |
+
# into consideration when building kv cache instead of just throwing away tokens outside of the window
|
| 1179 |
+
return key_states, value_states
|
| 1180 |
+
|
| 1181 |
+
slicing = torch.ones(max_cache_len, dtype=torch.long, device=value_states.device).cumsum(0)
|
| 1182 |
+
cache_position = cache_position.clamp(0, max_cache_len - 1)
|
| 1183 |
+
to_shift = cache_position >= max_cache_len - 1
|
| 1184 |
+
indices = (slicing + to_shift[-1].int() - 1) % max_cache_len
|
| 1185 |
+
k_out = k_out[:, :, indices]
|
| 1186 |
+
v_out = v_out[:, :, indices]
|
| 1187 |
+
|
| 1188 |
+
k_out[:, :, cache_position] = key_states
|
| 1189 |
+
v_out[:, :, cache_position] = value_states
|
| 1190 |
+
# `_.zero()` followed by `+=` is equivalent `=`, but compile-friendly (without graph breaks due to assignment)
|
| 1191 |
+
self.key_cache[layer_idx].zero_()
|
| 1192 |
+
self.value_cache[layer_idx].zero_()
|
| 1193 |
+
|
| 1194 |
+
self.key_cache[layer_idx] += k_out
|
| 1195 |
+
self.value_cache[layer_idx] += v_out
|
| 1196 |
+
return k_out, v_out
|
| 1197 |
+
|
| 1198 |
+
def _static_update(self, cache_position, layer_idx, key_states, value_states, k_out, v_out, max_cache_len):
|
| 1199 |
+
k_out[:, :, cache_position] = key_states
|
| 1200 |
+
v_out[:, :, cache_position] = value_states
|
| 1201 |
+
|
| 1202 |
+
self.key_cache[layer_idx] = k_out
|
| 1203 |
+
self.value_cache[layer_idx] = v_out
|
| 1204 |
+
return k_out, v_out
|
| 1205 |
+
|
| 1206 |
+
def update(
|
| 1207 |
+
self,
|
| 1208 |
+
key_states: torch.Tensor,
|
| 1209 |
+
value_states: torch.Tensor,
|
| 1210 |
+
layer_idx: int,
|
| 1211 |
+
cache_kwargs: Optional[Dict[str, Any]] = None,
|
| 1212 |
+
) -> Tuple[torch.Tensor]:
|
| 1213 |
+
cache_position = cache_kwargs.get("cache_position")
|
| 1214 |
+
sliding_window = cache_kwargs.get("sliding_window")
|
| 1215 |
+
self.key_cache[layer_idx] = self.key_cache[layer_idx].to(device=key_states.device)
|
| 1216 |
+
self.value_cache[layer_idx] = self.value_cache[layer_idx].to(device=value_states.device)
|
| 1217 |
+
k_out = self.key_cache[layer_idx]
|
| 1218 |
+
v_out = self.value_cache[layer_idx]
|
| 1219 |
+
if sliding_window:
|
| 1220 |
+
update_fn = self._sliding_update
|
| 1221 |
+
else:
|
| 1222 |
+
update_fn = self._static_update
|
| 1223 |
+
|
| 1224 |
+
return update_fn(
|
| 1225 |
+
cache_position,
|
| 1226 |
+
layer_idx,
|
| 1227 |
+
key_states,
|
| 1228 |
+
value_states,
|
| 1229 |
+
k_out,
|
| 1230 |
+
v_out,
|
| 1231 |
+
k_out.shape[2],
|
| 1232 |
+
)
|
| 1233 |
+
|
| 1234 |
+
def get_max_length(self) -> Optional[int]:
|
| 1235 |
+
# in theory there is no limit because the sliding window size is fixed
|
| 1236 |
+
# no matter how long the sentence is
|
| 1237 |
+
return self.max_cache_len
|
| 1238 |
+
|
| 1239 |
+
def get_seq_length(self, layer_idx: Optional[int] = 0):
|
| 1240 |
+
return None
|
| 1241 |
+
|
| 1242 |
+
def reset(self):
|
| 1243 |
+
"""Resets the cache values while preserving the objects"""
|
| 1244 |
+
for layer_idx in range(len(self.key_cache)):
|
| 1245 |
+
# In-place ops prevent breaking the static address
|
| 1246 |
+
self.key_cache[layer_idx].zero_()
|
| 1247 |
+
self.value_cache[layer_idx].zero_()
|
| 1248 |
+
|
| 1249 |
+
|
| 1250 |
+
class MambaCache:
|
| 1251 |
+
"""
|
| 1252 |
+
Cache for mamba model which does not have attention mechanism and key value states.
|
| 1253 |
+
|
| 1254 |
+
Arguments:
|
| 1255 |
+
config (`PretrainedConfig):
|
| 1256 |
+
The configuration file defining the shape-related attributes required to initialize the static cache.
|
| 1257 |
+
max_batch_size (`int`):
|
| 1258 |
+
The maximum batch size with which the model will be used.
|
| 1259 |
+
dtype (*optional*, defaults to `torch.float16`):
|
| 1260 |
+
The default `dtype` to use when initializing the layer.
|
| 1261 |
+
device (`torch.device`, *optional*):
|
| 1262 |
+
The device on which the cache should be initialized. Should be the same as the layer.
|
| 1263 |
+
|
| 1264 |
+
Attributes:
|
| 1265 |
+
dtype: (`torch.dtype`):
|
| 1266 |
+
The default `dtype` used to initializing the cache.
|
| 1267 |
+
intermediate_size: (`int`):
|
| 1268 |
+
Model's intermediate_size taken from config.
|
| 1269 |
+
ssm_state_size: (`int`):
|
| 1270 |
+
Model's state_size taken from config.
|
| 1271 |
+
conv_kernel_size: (`int`):
|
| 1272 |
+
Model's convolution kernel size taken from config
|
| 1273 |
+
conv_states: (`torch.Tensor`):
|
| 1274 |
+
A tensor of shape `[layer_idx, batch_size, intermediate_size, conv_kernel_size]` that holds convolutional states.
|
| 1275 |
+
ssm_states: (`torch.Tensor`):
|
| 1276 |
+
A tensor of shape `[layer_idx, batch_size, intermediate_size, ssm_state_size]` that holds ssm states
|
| 1277 |
+
|
| 1278 |
+
Example:
|
| 1279 |
+
|
| 1280 |
+
```python
|
| 1281 |
+
>>> from transformers import AutoTokenizer, MambaForCausalLM, MambaCache
|
| 1282 |
+
|
| 1283 |
+
>>> model = MambaForCausalLM.from_pretrained("state-spaces/mamba-130m-hf")
|
| 1284 |
+
>>> tokenizer = AutoTokenizer.from_pretrained("state-spaces/mamba-130m-hf")
|
| 1285 |
+
|
| 1286 |
+
>>> inputs = tokenizer(text="My name is Mamba", return_tensors="pt")
|
| 1287 |
+
|
| 1288 |
+
>>> # Prepare a cache class and pass it to model's forward
|
| 1289 |
+
>>> past_key_values = MambaCache(config=model.config, max_batch_size=1, device=model.device, dtype=model.dtype)
|
| 1290 |
+
>>> outputs = model(**inputs, past_key_values=past_key_values, use_cache=True)
|
| 1291 |
+
>>> past_kv = outputs.past_key_values
|
| 1292 |
+
```
|
| 1293 |
+
"""
|
| 1294 |
+
|
| 1295 |
+
def __init__(
|
| 1296 |
+
self,
|
| 1297 |
+
config: PretrainedConfig,
|
| 1298 |
+
max_batch_size: int,
|
| 1299 |
+
dtype: torch.dtype = torch.float16,
|
| 1300 |
+
device: Optional[str] = None,
|
| 1301 |
+
**kwargs,
|
| 1302 |
+
):
|
| 1303 |
+
self.dtype = dtype
|
| 1304 |
+
self.max_batch_size = max_batch_size
|
| 1305 |
+
self.intermediate_size = config.intermediate_size
|
| 1306 |
+
self.ssm_state_size = config.state_size
|
| 1307 |
+
self.conv_kernel_size = config.conv_kernel
|
| 1308 |
+
|
| 1309 |
+
self.conv_states: torch.Tensor = torch.zeros(
|
| 1310 |
+
config.num_hidden_layers,
|
| 1311 |
+
self.max_batch_size,
|
| 1312 |
+
self.intermediate_size,
|
| 1313 |
+
self.conv_kernel_size,
|
| 1314 |
+
device=device,
|
| 1315 |
+
dtype=dtype,
|
| 1316 |
+
)
|
| 1317 |
+
self.ssm_states: torch.Tensor = torch.zeros(
|
| 1318 |
+
config.num_hidden_layers,
|
| 1319 |
+
self.max_batch_size,
|
| 1320 |
+
self.intermediate_size,
|
| 1321 |
+
self.ssm_state_size,
|
| 1322 |
+
device=device,
|
| 1323 |
+
dtype=dtype,
|
| 1324 |
+
)
|
| 1325 |
+
|
| 1326 |
+
torch._dynamo.mark_static_address(self.conv_states)
|
| 1327 |
+
torch._dynamo.mark_static_address(self.ssm_states)
|
| 1328 |
+
|
| 1329 |
+
def update_conv_state(
|
| 1330 |
+
self, layer_idx: int, new_conv_state: torch.Tensor, cache_position: torch.LongTensor
|
| 1331 |
+
) -> torch.Tensor:
|
| 1332 |
+
conv_state = self.conv_states[layer_idx]
|
| 1333 |
+
cache_position = cache_position.clamp(0, self.conv_kernel_size - 1)
|
| 1334 |
+
|
| 1335 |
+
conv_state = conv_state.roll(shifts=-1, dims=-1)
|
| 1336 |
+
conv_state[:, :, cache_position] = new_conv_state.to(conv_state.device)
|
| 1337 |
+
self.conv_states[layer_idx].zero_()
|
| 1338 |
+
self.conv_states[layer_idx] += conv_state
|
| 1339 |
+
return self.conv_states[layer_idx]
|
| 1340 |
+
|
| 1341 |
+
def update_ssm_state(self, layer_idx: int, new_ssm_state: torch.Tensor):
|
| 1342 |
+
self.ssm_states[layer_idx] = new_ssm_state.to(self.ssm_states.device)
|
| 1343 |
+
return self.ssm_states[layer_idx]
|
| 1344 |
+
|
| 1345 |
+
def reset(self):
|
| 1346 |
+
self.conv_states.zero_()
|
| 1347 |
+
self.ssm_states.zero_()
|