forked from QwenLM/Qwen-VL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gist.py
250 lines (211 loc) · 8.35 KB
/
gist.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
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
"""Utilities for gist mask generation."""
from typing import Optional, Tuple
import torch
def reverse_cumsum(x: torch.Tensor) -> torch.Tensor:
"""Cumulative sum from right to left.
See https://github.com/pytorch/pytorch/issues/33520.
Args:
x: a tensor of shape (batch_size, seq_len)
Returns:
A tensor of shape (batch_size, seq_len) where each element is the sum of
all elements to the right of it.
"""
return x + torch.sum(x, dim=-1, keepdims=True) - torch.cumsum(x, dim=-1)
def make_mask_pre_first_gist(
inputs: torch.Tensor,
gist_token: int,
pad_token: Optional[int] = None,
dtype=torch.int64,
) -> torch.Tensor:
"""Returns a mask where all tokens prior to the first gist token are masked out.
Args:
inputs: an array of input tokens where the last dimension is the
sequence length.
gist_token: the integer id of the gist token.
pad_token: if supplied, mask out where inputs == pad_token.
dtype: the dtype of the mask, default int64.
Returns:
The requested mask.
"""
mask = (inputs == gist_token).cumsum(-1) >= 1
if pad_token is not None:
mask = mask & (inputs != pad_token)
return mask.type(dtype)
def make_mask_post_last_gist(
inputs: torch.Tensor,
gist_token: int,
pad_token: Optional[int] = None,
dtype=torch.int64,
) -> torch.Tensor:
"""Returns a mask where all tokens after the last gist token are masked out.
Computes the same as mask_pre_first_gist_token but reverses the
sequence before and after the cumsum.
Args:
inputs: an array of input tokens where the last dimension is the
sequence length.
gist_token: the integer id of the gist token.
pad_token: if supplied, mask out where inputs == pad_token.
dtype: the dtype of the mask, default int64.
Returns:
The requested mask.
"""
mask = reverse_cumsum(inputs == gist_token) >= 1
if pad_token is not None:
mask = mask & (inputs != pad_token)
return mask.type(dtype)
def make_gist_mask(
inputs: torch.Tensor,
gist_token: int,
pad_token: Optional[int] = None,
dtype=torch.int64,
) -> torch.Tensor:
"""Creates a 4D gist mask.
Here, tokens after the last gist cannot attend to tokens prior to the first
gist.
Additionally, tokens *before* the last gist cannot attend to tokens *after*
the last gist.
Example, where G is the gist token:
a b c G d
a 1 1 1 1 0
b 1 1 1 1 0
c 1 1 1 1 0
G 1 1 1 1 0
d 0 0 0 1 1
Args:
inputs: an array of shape (batch_size, seq_len) input tokens.
gist_token: the integer id of the gist token.
pad_token: if supplied, mask out where inputs == pad_token.
dtype: the dtype of the mask, default int64.
Returns:
The requested mask of shape (batch_size, 1, seq_len, seq_len)
"""
# Attention mask for tokens before the last gist token.
# Don't pass the pad token through for these first two masks, since we mask
# out padding later.
pre_gist_mask = make_mask_post_last_gist(inputs, gist_token, dtype=torch.bool)[
:, None, None
]
# Attention mask for tokens after the last gist token.
post_gist_mask = make_mask_pre_first_gist(inputs, gist_token, dtype=torch.bool)[
:, None, None
]
# Construct time masks by permuting to time dimension.
pre_gist_time_mask = pre_gist_mask.permute((0, 1, 3, 2))
mask = torch.where(pre_gist_time_mask, pre_gist_mask, post_gist_mask)
# If there are no gist tokens in an example, don't modify the mask (return
# all ones)
has_gist = (inputs == gist_token).any(-1)[:, None, None, None]
mask = torch.where(has_gist, mask, True)
if pad_token is not None:
mask = mask & (inputs != pad_token)[:, None, None]
return mask.type(dtype)
def make_neg_control_mask(
inputs: torch.Tensor,
gist_token: int,
pad_token: Optional[int] = None,
dtype=torch.int64,
):
"""Creates a 4D neg control mask.
Here, tokens after the last gist cannot attend to any gist tokens (or prior).
Example, where G is the gist token:
a b c G d
a 1 1 1 1 0
b 1 1 1 1 0
c 1 1 1 1 0
G 1 1 1 1 0
d 0 0 0 0 1
Args:
inputs: an array of shape (batch_size, seq_len) input tokens.
gist_token: the integer id of the gist token.
pad_token: if supplied, mask out where inputs == pad_token.
dtype: the dtype of the mask, default int64.
Returns:
The requested mask of shape (batch_size, 1, seq_len, seq_len)
"""
# Attention mask for tokens before the last gist token.
# Don't pass the pad token through for these first two masks, since we mask
# out padding later.
pre_gist_mask = make_mask_post_last_gist(inputs, gist_token, dtype=torch.bool)[
:, None, None
]
# Attention mask for tokens after the last gist token. This creates a mask
# that is zero for all tokens up to and including the last gist token.
post_gist_mask = torch.logical_not(pre_gist_mask)
# Construct time masks by permuting to time dimension.
pre_gist_time_mask = pre_gist_mask.permute((0, 1, 3, 2))
mask = torch.where(pre_gist_time_mask, pre_gist_mask, post_gist_mask)
# If there are no gist tokens in an example, don't modify the mask (return
# all ones)
has_gist = (inputs == gist_token).any(-1)[:, None, None, None]
mask = torch.where(has_gist, mask, True)
if pad_token is not None:
mask = mask & (inputs != pad_token)[:, None, None]
return mask.type(dtype)
def make_pos_control_mask(
inputs: torch.Tensor,
gist_token: int,
pad_token: Optional[int] = None,
dtype=torch.int64,
):
"""Creates a 4D pos control mask.
Returns all ones (unaffected mask).
Args:
inputs: an array of shape (batch_size, seq_len) input tokens.
gist_token: the integer id of the gist token.
pad_token: if supplied, mask out where inputs == pad_token.
dtype: the dtype of the mask, default int64.
Returns:
The requested mask of shape (batch_size, 1, seq_len, seq_len)
"""
del gist_token
batch_size, seq_len = inputs.shape
mask = torch.ones((batch_size, 1, seq_len, seq_len), dtype=torch.bool)
if pad_token is not None:
mask = mask & (inputs != pad_token)[:, None, None]
return mask.type(dtype)
def get_gist_index(
input_ids: torch.Tensor, gist_token: int, raise_if_no_tokens: bool = False
) -> Tuple[Optional[int], Optional[int]]:
"""Finds the start and end of the gist span in input_ids.
Args:
input_ids: tensor of input ids.
gist_token: value of gist token.
raise_if_no_tokens: raise an error if there are no gist tokens.
Returns:
(start, end) of gist token(s), with exclusive end, if they exist,
otherwise (None, None) if raise_if_no_tokens is False (raises
error if True).
Raises:
RuntimeError: If the gist tokens in the input are not a contiguous span.
ValueError: If no gist tokens are found and raise_if_no_tokens is True.
"""
gist_indices = (input_ids == gist_token).nonzero().squeeze(-1)
if len(gist_indices) == 0:
if raise_if_no_tokens:
raise ValueError(f"Could not find gist token {gist_token} in {input_ids}")
return (None, None)
# Assert that the gist indices are a single continuous sequence.
_assert_continguous_span(gist_indices)
return (gist_indices[0].item(), gist_indices[-1].item() + 1)
def get_first_pad_index(input_ids: torch.Tensor, pad_token: int) -> int:
"""Finds the index of the first pad token in input_ids.
Args:
input_ids: tensor of input ids.
pad_token: value of pad token.
Returns:
index of pad token if exists, otherwise len(input_ids).
"""
pad_indices = (input_ids == pad_token).nonzero()
if len(pad_indices) == 0:
return len(input_ids)
return pad_indices[0].item()
def _assert_continguous_span(gist_indices: torch.Tensor):
"""Assert that the gist indices form a contiguous span."""
gist_start = gist_indices[0]
gist_indices_arange = torch.arange(
start=gist_start,
end=gist_start + len(gist_indices),
device=gist_indices.device,
)
if not (gist_indices == gist_indices_arange).all():
raise RuntimeError(f"gist tokens do not form a contiguous span: {gist_indices}")