-
Notifications
You must be signed in to change notification settings - Fork 259
/
kv_cache.py
73 lines (60 loc) · 2.37 KB
/
kv_cache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
from typing import Tuple
import torch
from torch import nn, Tensor
class KVCache(nn.Module):
"""
Standalone ``nn.Module`` containing a kv-cache to cache past key and values during inference.
Args:
batch_size (int): batch size model will be run with
max_seq_len (int): maximum sequence length model will be run with
num_heads (int): number of heads. We take num_heads instead of num_kv_heads because
the cache is created after we've expanded the key and value tensors to have the
same shape as the query tensor. See attention.py for more details
head_dim (int): per-attention head embedding dimension
dtype (torch.dtype): dtype for the caches
"""
def __init__(
self,
batch_size: int,
max_seq_len: int,
num_heads: int,
head_dim: int,
dtype: torch.dtype,
) -> None:
super().__init__()
cache_shape = (batch_size, num_heads, max_seq_len, head_dim)
self.register_buffer(
"k_cache", torch.zeros(cache_shape, dtype=dtype), persistent=False
)
self.register_buffer(
"v_cache", torch.zeros(cache_shape, dtype=dtype), persistent=False
)
self.batch_size = batch_size
def reset(self) -> None:
"""Reset the cache to zero."""
self.k_cache.zero_()
self.v_cache.zero_()
def update(
self, input_pos: Tensor, k_val: Tensor, v_val: Tensor
) -> Tuple[Tensor, Tensor]:
"""Update KV cache and return the updated cache.
Args:
input_pos (Tensor): Current position tensor with shape [S]
k_val (Tensor): Current key tensor with shape [B, H, S, D]
v_val (Tensor): Current value tensor with shape [B, H, S, D]
Raises:
ValueError: if ``input_pos`` is longer than the maximum sequence length
Returns:
Tuple[Tensor, Tensor]: Updated KV cache with key first
"""
assert input_pos.shape[0] == k_val.shape[2]
k_out = self.k_cache
v_out = self.v_cache
k_out[:, :, input_pos] = k_val
v_out[:, :, input_pos] = v_val
return k_out, v_out