|
6 | 6 |
|
7 | 7 | import logging
|
8 | 8 | from enum import Enum
|
| 9 | +from typing import Tuple |
9 | 10 |
|
10 | 11 | import torch
|
11 | 12 | import torch.nn as nn
|
@@ -44,7 +45,6 @@ def __init__(
|
44 | 45 | QuantizedCacheType.AffineSymmetric,
|
45 | 46 | QuantizedCacheType.AffineAsymmetric,
|
46 | 47 | ):
|
47 |
| - |
48 | 48 | raise ValueError(
|
49 | 49 | f"Only affine symmetric and asymmetric cache types are supported: got {cache_type}"
|
50 | 50 | )
|
@@ -81,10 +81,11 @@ def __init__(
|
81 | 81 | )
|
82 | 82 |
|
83 | 83 | def _quantize(self, value):
|
84 |
| - scales, zero_points = ( |
85 |
| - torch.ops.quantized_decomposed.choose_qparams_per_token_asymmetric.default( |
86 |
| - value, self.quantized_cache_dtype |
87 |
| - ) |
| 84 | + ( |
| 85 | + scales, |
| 86 | + zero_points, |
| 87 | + ) = torch.ops.quantized_decomposed.choose_qparams_per_token_asymmetric.default( |
| 88 | + value, self.quantized_cache_dtype |
88 | 89 | )
|
89 | 90 | quantized_value = torch.ops.quantized_decomposed.quantize_per_token(
|
90 | 91 | value,
|
@@ -262,3 +263,71 @@ def replace_kv_cache_with_quantized_kv_cache(module):
|
262 | 263 | else:
|
263 | 264 | replace_kv_cache_with_quantized_kv_cache(child)
|
264 | 265 | return module
|
| 266 | + |
| 267 | + |
| 268 | +class CustomKVCache(nn.Module): |
| 269 | + def __init__( |
| 270 | + self, |
| 271 | + max_batch_size: int, |
| 272 | + max_seq_length: int, |
| 273 | + n_heads: int, |
| 274 | + head_dim: int, |
| 275 | + dtype=torch.float32, |
| 276 | + ): |
| 277 | + super().__init__() |
| 278 | + self.max_seq_length = max_seq_length |
| 279 | + cache_shape = (max_batch_size, max_seq_length, n_heads, head_dim) |
| 280 | + |
| 281 | + self.max_batch_size = max_batch_size |
| 282 | + self.n_heads = n_heads |
| 283 | + self.head_dim = head_dim |
| 284 | + self.register_buffer( |
| 285 | + "k_cache", torch.zeros(cache_shape, dtype=dtype, device="cpu") |
| 286 | + ) |
| 287 | + self.register_buffer( |
| 288 | + "v_cache", torch.zeros(cache_shape, dtype=dtype, device="cpu") |
| 289 | + ) |
| 290 | + |
| 291 | + def update( |
| 292 | + self, input_pos: torch.Tensor, k_val: torch.Tensor, v_val: torch.Tensor |
| 293 | + ) -> Tuple[torch.Tensor, torch.Tensor]: |
| 294 | + # input_pos: [S], k_val: [B, S, H, D] |
| 295 | + start_pos = input_pos[0].item() |
| 296 | + _ = torch.ops.llama.update_cache(k_val, self.k_cache, start_pos) |
| 297 | + _ = torch.ops.llama.update_cache(v_val, self.v_cache, start_pos) |
| 298 | + return self.k_cache, self.v_cache |
| 299 | + |
| 300 | + |
| 301 | +def replace_kv_cache_with_custom_kv_cache(module): |
| 302 | + r""" |
| 303 | + Replace KVCache with CustomKVCache. This modifies the model in place. |
| 304 | + At the moment custom kv cache only supports cache with shape |
| 305 | + [B, S, H, D] as opposed to [B, H, S, D] |
| 306 | + This is because the custom op treats second dim as sequence dim. |
| 307 | + Future work: support [B, H, S, D] |
| 308 | + """ |
| 309 | + logging.warning( |
| 310 | + "Replacing KVCache with CustomKVCache. This modifies the model in place." |
| 311 | + ) |
| 312 | + for name, child in module.named_children(): |
| 313 | + if isinstance(child, KVCache): |
| 314 | + cache_shape = child.k_cache.shape |
| 315 | + cache_dtype = child.k_cache.dtype |
| 316 | + assert ( |
| 317 | + child.is_transposed is False |
| 318 | + ), "CustomKVCache does not support transposed cache" |
| 319 | + max_batch_size, max_seq_length, n_heads, head_dim = cache_shape |
| 320 | + setattr( |
| 321 | + module, |
| 322 | + name, |
| 323 | + CustomKVCache( |
| 324 | + max_batch_size, |
| 325 | + max_seq_length, |
| 326 | + n_heads, |
| 327 | + head_dim, |
| 328 | + dtype=cache_dtype, |
| 329 | + ), |
| 330 | + ) |
| 331 | + else: |
| 332 | + replace_kv_cache_with_custom_kv_cache(child) |
| 333 | + return module |
0 commit comments