-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcache.py
More file actions
192 lines (176 loc) · 10.1 KB
/
cache.py
File metadata and controls
192 lines (176 loc) · 10.1 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# Copyright (c) 2026 Xiuying Wei, EPFL CLAIRE lab
# All rights reserved.
# Licensed under the MIT License.
import torch
class RATPlusSingleLayerCache:
# initial + local + dilated
def __init__(self,
layer_id,
max_bs,
max_seq_len,
initial_size,
local_size,
dilation_size,
num_head,
d_head,
d_model,
dtype=torch.bfloat16,
device="cuda"):
self.layer_id = layer_id
self.max_bs = max_bs
self.initial_size = initial_size
self.local_size = local_size
self.bound = self.initial_size + self.local_size
self.dilation_size = dilation_size
assert self.local_size != 0 or self.dilation_size != -1 or self.initial_size != 0 # either local attention or dilated attention
self.max_seq_len = max_seq_len
self.d_model = d_model
self.num_head = num_head
self.d_head = d_head
self.bs_start = 0
self.seq_start = 0 # the position to put the new token
self.seq_end = 0 # the final token ) unclosed pos, also to indicate the position where we temporarily put the current token
sz = self.bound + 1 if self.dilation_size == -1 else self.bound + 1 + (self.max_seq_len // self.dilation_size)
self.kcache = torch.empty(self.max_bs, self.num_head, sz, self.d_head, dtype=dtype, device=device)
self.vcache = torch.empty(self.max_bs, self.num_head, sz, self.d_head, dtype=dtype, device=device)
self.lastkcache = torch.zeros(self.max_bs, self.num_head, 1, self.d_head, dtype=dtype, device=device)
self.lastvcache = torch.zeros(self.max_bs, self.num_head, 1, self.d_head, dtype=dtype, device=device)
self.d_st = self.initial_size + ((self.dilation_size - 1 - self.initial_size) % self.dilation_size)
def reset_cache(self, ):
self.bs_start = 0
self.seq_start = 0
self.seq_end = 0
self.lastkcache.zero_()
self.lastvcache.zero_()
def update_kv_prefill_dilation(self, d_ed, kcache, vcache, gated_k, gated_x):
chunk_gated_k, chunk_gated_x = gated_k[:, :, self.d_st: d_ed: self.dilation_size], gated_x[:, :, self.d_st: d_ed: self.dilation_size]
num_dilated = chunk_gated_k.shape[-2]
kcache[:, :, self.bound: self.bound + num_dilated].copy_(chunk_gated_k)
vcache[:, :, self.bound: self.bound + num_dilated].copy_(chunk_gated_x)
self.seq_end = self.bound + num_dilated
def update_kv_prefill(self, seq_pos, bs, gated_k, gated_x):
kcache, vcache = self.kcache[self.bs_start: self.bs_start + bs], self.vcache[self.bs_start: self.bs_start + bs]
if self.local_size == 0:
ed = seq_pos + 1
if seq_pos < self.bound:
kcache[:, :, :ed].copy_(gated_k[:, :, :ed])
vcache[:, :, :ed].copy_(gated_x[:, :, :ed])
self.seq_start = self.seq_end = ed
else:
kcache[:, :, :self.bound].copy_(gated_k[:, :, :self.bound])
vcache[:, :, :self.bound].copy_(gated_x[:, :, :self.bound])
if self.dilation_size != -1:
self.update_kv_prefill_dilation(ed, kcache, vcache, gated_k, gated_x)
self.seq_start = self.seq_end
else:
self.seq_start = self.seq_end = self.bound
else:
ed = seq_pos + 1
if seq_pos < self.bound - 1:
kcache[:, :, :ed].copy_(gated_k[:, :, :ed])
vcache[:, :, :ed].copy_(gated_x[:, :, :ed])
self.seq_end = self.seq_start = ed
else:
kcache[:, :, :self.initial_size].copy_(gated_k[:, :, :self.initial_size])
vcache[:, :, :self.initial_size].copy_(gated_x[:, :, :self.initial_size])
kcache[:, :, self.initial_size: self.bound].copy_(gated_k[:, :, ed - self.local_size: ed])
vcache[:, :, self.initial_size: self.bound].copy_(gated_x[:, :, ed - self.local_size: ed])
# dilation
self.seq_start = self.initial_size
self.seq_end = self.bound
if self.dilation_size != -1:
self.update_kv_prefill_dilation(ed - self.local_size, kcache, vcache, gated_k, gated_x)
def update_kv_step(self, seq_pos, bs, gated_k, gated_x):
kcache, vcache = self.kcache[self.bs_start: self.bs_start + bs], self.vcache[self.bs_start: self.bs_start + bs]
if self.local_size == 0:
# we store either initial or dilated tokens
if seq_pos < self.bound or (self.dilation_size != -1 and (seq_pos + 1) % self.dilation_size == 0):
kcache[:, :, self.seq_start: self.seq_start + 1].copy_(gated_k)
vcache[:, :, self.seq_start: self.seq_start + 1].copy_(gated_x)
self.seq_start += 1
self.seq_end += 1
else:
if seq_pos >= self.bound:
remove_pos = (seq_pos - self.local_size)
if self.dilation_size != -1 and (remove_pos + 1) % self.dilation_size == 0:
kcache[:, :, self.seq_end].copy_(kcache[:, :, self.seq_start])
vcache[:, :, self.seq_end].copy_(vcache[:, :, self.seq_start])
self.seq_end += 1
else:
self.seq_end += 1
kcache[:, :, self.seq_start: self.seq_start + 1].copy_(gated_k)
vcache[:, :, self.seq_start: self.seq_start + 1].copy_(gated_x)
self.seq_start += 1
if self.seq_start >= self.bound:
self.seq_start -= self.local_size
def get_kv_step(self, seq_pos, bs, gated_k, gated_x):
kcache, vcache = self.kcache[self.bs_start: self.bs_start + bs], self.vcache[self.bs_start: self.bs_start + bs]
kcache[:, :, self.seq_end: self.seq_end + 1].copy_(gated_k)
vcache[:, :, self.seq_end: self.seq_end + 1].copy_(gated_x)
return kcache[:, :, :self.seq_end + 1], vcache[:, :, :self.seq_end + 1]
def __repr__(self):
return f"dilation_size={self.dilation_size}, initial_size={self.initial_size}, local_size={self.local_size}"
class RATPlusFullSingleLayerCache:
# store the full one, take out the corresponding one
def __init__(self,
layer_id,
max_bs,
max_seq_len,
initial_size,
local_size,
dilation_size,
num_head,
d_head,
d_model,
dtype=torch.bfloat16,
device="cuda"):
self.layer_id = layer_id
self.max_bs = max_bs
self.initial_size = initial_size
self.local_size = local_size
self.bound = self.initial_size + self.local_size
self.dilation_size = dilation_size
assert self.local_size != 0 or self.dilation_size != -1 or self.initial_size != 0 # either local attention or dilated attention
self.max_seq_len = max_seq_len
self.d_model = d_model
self.num_head = num_head
self.d_head = d_head
self.bs_start = 0
self.seq_start = 0 # the pos to put the new token
self.seq_end = 0 # the final token ) unclosed pos
self.kcache = torch.empty(self.max_bs, self.num_head, self.max_seq_len, self.d_head, dtype=dtype, device=device)
self.vcache = torch.empty(self.max_bs, self.num_head, self.max_seq_len, self.d_head, dtype=dtype, device=device)
self.lastkcache = torch.zeros(self.max_bs, self.num_head, 1, self.d_head, dtype=dtype, device=device)
self.lastvcache = torch.zeros(self.max_bs, self.num_head, 1, self.d_head, dtype=dtype, device=device)
self.d_st = self.initial_size + ((self.dilation_size - 1 - self.initial_size) % self.dilation_size)
def update_kv_prefill(self, seq_pos, bs, gated_k, gated_x):
self.kcache[self.bs_start: self.bs_start + bs, :, :seq_pos + 1].copy_(gated_k[:, :, :seq_pos + 1])
self.vcache[self.bs_start: self.bs_start + bs, :, :seq_pos + 1].copy_(gated_x[:, :, :seq_pos + 1])
self.seq_start = self.seq_end = seq_pos + 1
def update_kv_step(self, seq_pos, bs, gated_k, gated_x):
self.kcache[self.bs_start: self.bs_start + bs, :, self.seq_start: self.seq_start + 1].copy_(gated_k)
self.vcache[self.bs_start: self.bs_start + bs, :, self.seq_start: self.seq_start + 1].copy_(gated_x)
self.seq_start += 1
self.seq_end += 1
def get_kv_step(self, seq_pos, bs, gated_k, gated_x):
assert seq_pos == self.seq_start and seq_pos == self.seq_end
if seq_pos <= self.bound:
self.kcache[self.bs_start: self.bs_start + bs, :, seq_pos: seq_pos + 1].copy_(gated_k)
self.vcache[self.bs_start: self.bs_start + bs, :, seq_pos: seq_pos + 1].copy_(gated_x)
return self.kcache[self.bs_start: self.bs_start + bs, :, :seq_pos + 1], self.vcache[self.bs_start: self.bs_start + bs, :, :seq_pos + 1]
kcache, vcache = self.kcache[self.bs_start: self.bs_start + bs, :, :self.seq_end], self.vcache[self.bs_start: self.bs_start + bs, :, :self.seq_end]
prefill_kcache, prefill_vcache = kcache[:, :, :self.initial_size], vcache[:, :, :self.initial_size]
local_kcache, local_vcache = kcache[:, :, self.seq_end - self.local_size: ], vcache[:, :, self.seq_end - self.local_size: ]
if self.dilation_size != -1:
d_ed = self.seq_end - self.local_size # (] area
chunk_kcache, chunk_vcache = kcache[:, :, self.d_st: d_ed: self.dilation_size], vcache[:, :, self.d_st: d_ed: self.dilation_size]
return torch.cat([prefill_kcache, local_kcache, chunk_kcache, gated_k], dim=-2), torch.cat([prefill_vcache, local_vcache, chunk_vcache, gated_x], dim=-2)
return torch.cat([prefill_kcache, local_kcache, gated_k], dim=-2), torch.cat([prefill_vcache, local_vcache, gated_x], dim=-2)
def reset_cache(self, ):
self.bs_start = 0
self.seq_start = 0
self.seq_end = 0
self.lastkcache.zero_()
self.lastvcache.zero_()
def __repr__(self):
return f"dilation_size={self.dilation_size}, initial_size={self.initial_size}, local_size={self.local_size}"