-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathmasking_utils.py
More file actions
312 lines (269 loc) · 12.4 KB
/
Copy pathmasking_utils.py
File metadata and controls
312 lines (269 loc) · 12.4 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# Copyright (c) Qualcomm Innovation Center, Inc.
# 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 abc import ABC, abstractmethod
from typing import List, Tuple, Union
import torch
PADDING_MASK_VALUE = -255.0
def create_causal_attn_mask(max_batch_size: int, ar_len: int, max_context_len: int):
"""
Creating a causal attention mask (ar_len: 5, max_context_len: 15)
0 ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ● ○ ○ ○ ○
1 ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ● ● ○ ○ ○
2 ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ● ● ● ○ ○
3 ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ● ● ● ● ○
4 ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ● ● ● ● ●
● = activate (can attend), ○ = inactivate (masked)
"""
mask = torch.full((ar_len, ar_len), PADDING_MASK_VALUE)
mask_cond = torch.arange(ar_len)
mask.masked_fill_(mask_cond.view(1, ar_len) <= mask_cond.view(ar_len, 1), 0)
if max_context_len != ar_len:
mask = torch.cat(
[
torch.ones(ar_len, max_context_len - ar_len) * PADDING_MASK_VALUE,
mask,
],
dim=-1,
)
# num_heads=1: the mask broadcasts across all heads.
mask = mask[None, None, :, :].expand(max_batch_size, 1, ar_len, max_context_len)
return mask
def create_sliding_window_attn_mask(
max_batch_size: int, ar_len: int, max_context_len: int, sliding_window: int
):
"""
Creating a sliding_window attention mask (ar_len: 5, max_context_len: 15, sliding_window: 3)
0 ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ● ○ ○ ○ ○
1 ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ● ● ○ ○ ○
2 ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ● ● ● ○ ○
3 ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ● ● ● ○
4 ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ● ● ●
● = activate (can attend), ○ = inactivate (masked)
"""
mask = torch.full((ar_len, ar_len), PADDING_MASK_VALUE)
mask_cond = torch.arange(ar_len)
mask.masked_fill_(
(mask_cond.view(1, ar_len) <= mask_cond.view(ar_len, 1))
& (mask_cond.view(ar_len, 1) - mask_cond.view(1, ar_len) < sliding_window),
0,
)
if max_context_len != ar_len:
mask = torch.cat(
[
torch.ones(ar_len, max_context_len - ar_len) * PADDING_MASK_VALUE,
mask,
],
dim=-1,
)
# num_heads=1: the mask broadcasts across all heads.
mask = mask[None, None, :, :].expand(max_batch_size, 1, ar_len, max_context_len)
return mask
class BaseAttentionMask(ABC):
def __init__(self, max_batch_size: int, ar_len: int, max_context_len: int):
"""
Base class for attention masks used in autoregressive or hybrid attention mechanisms.
Args:
max_batch_size (int): Maximum batch size supported.
ar_len (int): Length of the autoregressive sequence.
max_context_len (int): Maximum sequence length.
"""
self.max_batch_size = max_batch_size
self.ar_len = ar_len
self.max_context_len = max_context_len
@property
@abstractmethod
def mask(self) -> torch.Tensor:
"""
Attention mask tensor that must be initialized by child classes.
"""
pass
@abstractmethod
def smart_mask_init(self, pos):
"""
Initialize the attention mask by smart mask initialization method after model forward.
Args:
pos (int): Current position in the sequence.
"""
pass
@abstractmethod
def smart_mask_update(self, pos, n_updates, lade_pos_offset):
"""
Update the attention mask by smart mask update method after model forward.
Args:
pos (int): Current position in the sequence.
n_updates (int): Number of new tokens to update.
lade_pos_offset (List[int]): Position offset of lookahead attention mask.
"""
pass
def _extra_init_kwargs(self) -> dict:
return {}
def _mask_padding_positions(
self, input_ids: List[List[int]], max_seq_length: int
) -> None:
"""Mask positions beyond each sequence's actual length."""
actual_lens = torch.tensor([len(seq) for seq in input_ids])
pad_rows = torch.arange(max_seq_length) >= actual_lens.unsqueeze(1)
self.mask.masked_fill_(pad_rows[:, None, :, None], PADDING_MASK_VALUE)
class CausalAttentionMask(BaseAttentionMask):
def __init__(self, max_batch_size: int, ar_len: int, max_context_len: int):
super().__init__(max_batch_size, ar_len, max_context_len)
self._max_batch_size = max_batch_size
self._mask = create_causal_attn_mask(max_batch_size, ar_len, max_context_len)
@property
def mask(self):
return self._mask
def smart_mask_init(self, pos):
self._mask = create_causal_attn_mask(
self.max_batch_size, self.ar_len, self.max_context_len
)
self.mask[:, :, :pos] = 0
def smart_mask_update(self, pos, n_updates, _):
"""
Smart Mask mechanism for attention mask updating
Initial mask(5x15) layout (before any updates):
Each row represents a query token in the autoregressive context.
● = activate (can attend), ○ = inactivate (masked)
0 ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ● ○ ○ ○ ○
1 ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ● ● ○ ○ ○
2 ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ● ● ● ○ ○
3 ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ● ● ● ● ○
4 ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ● ● ● ● ●
After 1st update (e.g., pos=0, n_updates=5, sliding_window=3):
Newly added tokens are unmasked (set to 0).
0 ● ● ● ● ● ○ ○ ○ ○ ○ ● ○ ○ ○ ○
1 ● ● ● ● ● ○ ○ ○ ○ ○ ● ● ○ ○ ○
2 ● ● ● ● ● ○ ○ ○ ○ ○ ● ● ● ○ ○
3 ● ● ● ● ● ○ ○ ○ ○ ○ ● ● ● ● ○
4 ● ● ● ● ● ○ ○ ○ ○ ○ ● ● ● ● ●
After 2nd update (e.g., pos=5, n_updates=5):
0 ● ● ● ● ● ● ● ● ● ● ● ○ ○ ○ ○
1 ● ● ● ● ● ● ● ● ● ● ● ● ○ ○ ○
2 ● ● ● ● ● ● ● ● ● ● ● ● ● ○ ○
3 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ○
4 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●
"""
start_pos = pos
end_pos = pos + n_updates
self.mask[:, :, start_pos:end_pos] = 0
@classmethod
def from_input_ids(
cls, input_ids: List[List[int]], max_seq_length: int, **kwargs
) -> "CausalAttentionMask":
"""Build a causal mask and apply padding for variable-length sequences."""
mask = cls(len(input_ids), max_seq_length, max_seq_length)
mask._mask = mask._mask.clone()
mask._mask_padding_positions(input_ids, max_seq_length)
return mask
class SlidingWindowAttentionMask(BaseAttentionMask):
def __init__(
self,
max_batch_size: int,
ar_len: int,
max_context_len: int,
sliding_window: int,
):
super().__init__(max_batch_size, ar_len, max_context_len)
self._mask = create_sliding_window_attn_mask(
max_batch_size, ar_len, max_context_len, sliding_window
)
self.sliding_window = sliding_window
@property
def mask(self):
return self._mask
def smart_mask_init(self, pos):
self._mask = create_sliding_window_attn_mask(
self.max_batch_size, self.ar_len, self.max_context_len, self.sliding_window
)
self.mask[:, :, :pos] = 0
def smart_mask_update(self, pos, n_updates, lade_pos_offset):
"""
Smart Mask mechanism for attention mask updating
Initial mask(5x15) layout (before any updates):
Each row represents a query token in the autoregressive context.
● = activate (can attend), ○ = inactivate (masked)
0 ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ● ○ ○ ○ ○
1 ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ● ● ○ ○ ○
2 ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ● ● ● ○ ○
3 ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ● ● ● ○
4 ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ● ● ●
After 1st update (e.g., pos=0, n_updates=5, sliding_window=3):
Newly added tokens are unmasked (set to 0).
Earlier tokens lose access to older cache due to sliding window limits.
0 ○ ○ ○ ● ● ○ ○ ○ ○ ○ ● ○ ○ ○ ○
1 ○ ○ ○ ○ ● ○ ○ ○ ○ ○ ● ● ○ ○ ○
2 ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ● ● ● ○ ○
3 ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ● ● ● ○
4 ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ● ● ●
After 2nd update (e.g., pos=5, n_updates=5, sliding_window=3):
Sliding window shifts again, masking older positions and activate new position.
0 ○ ○ ○ ○ ○ ○ ○ ○ ● ● ● ○ ○ ○ ○
1 ○ ○ ○ ○ ○ ○ ○ ○ ○ ● ● ● ○ ○ ○
2 ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ● ● ● ○ ○
3 ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ● ● ● ○
4 ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ● ● ●
"""
start_pos = pos
end_pos = pos + n_updates
# Unmask the same range in the sliding window mask
self.mask[:, :, start_pos:end_pos] = 0
for i in range(self.ar_len):
# Calculate how many cached tokens are still available for this row
available_cache_len = self.sliding_window - (
(i + 1) if lade_pos_offset is None else (lade_pos_offset[i] + 1)
)
# If the current position exceeds available cache, mask the overflow
if end_pos > available_cache_len:
# Mask tokens that are no longer within the sliding window
# TODO: [Optional]: it can be optimized by computing the exact start index
self.mask[:, i, : end_pos - available_cache_len] = PADDING_MASK_VALUE
def _extra_init_kwargs(self) -> dict:
return {"sliding_window": self.sliding_window}
@classmethod
def from_input_ids(
cls,
input_ids: List[List[int]],
max_seq_length: int,
sliding_window: int,
**kwargs,
) -> "SlidingWindowAttentionMask":
"""Build a sliding-window mask and apply padding for variable-length sequences."""
mask = cls(len(input_ids), max_seq_length, max_seq_length, sliding_window)
mask._mask = mask._mask.clone()
mask._mask_padding_positions(input_ids, max_seq_length)
return mask
class AttentionMask:
def __init__(self, masks: Union[BaseAttentionMask, List[BaseAttentionMask]]):
self.masks = masks if isinstance(masks, list) else [masks]
def smart_mask_init(self, pos):
for mask in self.masks:
mask.smart_mask_init(pos)
def smart_mask_update(self, pos, n_updates, lade_pos_offset=None):
for mask in self.masks:
mask.smart_mask_update(pos, n_updates, lade_pos_offset)
def __iter__(self):
return iter([mask.mask for mask in self.masks])
def __getitem__(self, idx: int) -> Tuple[torch.Tensor, ...]:
return tuple(m.mask[idx] for m in self.masks)
@classmethod
def from_input_ids(
cls,
template: "AttentionMask",
input_ids: List[List[int]],
max_seq_length: int,
) -> "AttentionMask":
"""
Build a calibration AttentionMask that mirrors template's mask types.
Delegates construction to each mask's own classmethod so that adding a
new mask type only requires implementing from_input_ids on that class —
no edits needed here.
"""
masks = [
type(base_mask).from_input_ids(
input_ids, max_seq_length, **base_mask._extra_init_kwargs()
)
for base_mask in template.masks
]
return cls(masks)