-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattention_mask_variable_listops.py
213 lines (184 loc) · 8.22 KB
/
attention_mask_variable_listops.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
import torch
import Config
import os
comparison_mask_12 = torch.tensor([
[1,1,1],
[1,1,1],
[1,1,1],
])
def load_token_ids():
import pickle
with open(os.path.join(Config.TOKENIZER_PATH, 'listops_token_ids.pkl'), 'rb') as reader:
token_ids = pickle.load(reader)
charset = '[]0123456789SUMMEDMAXMIN'
return token_ids
def compute_attn_mask(input_ids, charset_ids, attn_masks, i):
open_bracket = charset_ids[0]
closed_bracket = charset_ids[1]
numbers = charset_ids[2:12]
operators = charset_ids[12:16]
padding = charset_ids[16]
if input_ids[0] in numbers:
attn_masks[i, 0, 0] = 1
return attn_masks
operand_idxs_stack = []
attn_idx = 0
for token_id in input_ids:
if token_id == open_bracket:
pass
elif token_id == closed_bracket:
operand_idxs_stack.pop()
elif token_id in numbers:
attn_masks[i, attn_idx, attn_idx] = 1
attn_masks[i, operand_idxs_stack[-1], attn_idx] = 1
elif token_id in operators: # Operand
operand_idxs_stack.append(attn_idx)
attn_masks[i, attn_idx, attn_idx] = 1
if len(operand_idxs_stack) > 1:
attn_masks[i, operand_idxs_stack[-2], attn_idx] = 1
elif token_id == padding:
break
else:
raise Exception(f"Unknown character {token_id}")
attn_idx += 1
return attn_masks
def compute_attn_mask_red(input_ids, charset_ids, attn_masks, i):
open_bracket = charset_ids[0]
closed_bracket = charset_ids[1]
numbers = charset_ids[2:12]
operators = charset_ids[12:16]
padding = charset_ids[16]
if input_ids[0] in numbers:
attn_masks[i, 0, 0] = 1
return attn_masks
operand_idxs_stack = []
attn_idx = 0
for token_id in input_ids:
if token_id == open_bracket:
pass
elif token_id == closed_bracket:
operand_idxs_stack.pop()
elif token_id in numbers:
attn_masks[i, attn_idx, attn_idx] = 1
attn_masks[i, operand_idxs_stack[-1], attn_idx] = 1
attn_masks[i, attn_idx, operand_idxs_stack[-1]] = 1
elif token_id in operators: # Operand
operand_idxs_stack.append(attn_idx)
attn_masks[i, attn_idx, attn_idx] = 1
if len(operand_idxs_stack) > 1:
attn_masks[i, operand_idxs_stack[-2], attn_idx] = 1
attn_masks[i, attn_idx, operand_idxs_stack[-2]] = 1
elif token_id == padding:
break
else:
raise Exception(f"Unknown character {token_id}")
attn_idx += 1
return attn_masks
def compute_attn_mask_green(input_ids, charset_ids, attn_masks, i):
open_bracket = charset_ids[0]
closed_bracket = charset_ids[1]
numbers = charset_ids[2:12]
operators = charset_ids[12:16]
padding = charset_ids[16]
if input_ids[0] in numbers:
attn_masks[i, 0, 0] = 1
return attn_masks
operand_idxs_stack = []
current_numbers_idxs_stack = []
attn_idx = 0
for token_id in input_ids:
if token_id == open_bracket:
pass
elif token_id == closed_bracket:
operand_idxs_stack.pop()
current_numbers_idxs_stack.pop()
elif token_id in numbers:
attn_masks[i, attn_idx, attn_idx] = 1
attn_masks[i, operand_idxs_stack[-1], attn_idx] = 1
attn_masks[i, attn_idx, operand_idxs_stack[-1]] = 1
for number_idx in current_numbers_idxs_stack[-1]:
attn_masks[i, attn_idx, number_idx] = 1
attn_masks[i, number_idx, attn_idx] = 1
current_numbers_idxs_stack[-1].append(attn_idx)
elif token_id in operators: # Operand
operand_idxs_stack.append(attn_idx)
current_numbers_idxs_stack.append([])
attn_masks[i, attn_idx, attn_idx] = 1
if len(operand_idxs_stack) > 1:
attn_masks[i, operand_idxs_stack[-2], attn_idx] = 1
attn_masks[i, attn_idx, operand_idxs_stack[-2]] = 1
elif token_id == padding:
break
else:
raise Exception(f"Unknown character {token_id}")
attn_idx += 1
return attn_masks
def compute_attn_mask_T(input_ids, charset_ids, attn_masks, i):
open_bracket = charset_ids[0]
closed_bracket = charset_ids[1]
numbers = charset_ids[2:12]
operators = charset_ids[12:16]
padding = charset_ids[16]
if input_ids[0] in numbers:
attn_masks[i, 0, 0] = 1
return attn_masks
operand_idxs_stack = []
attn_idx = 0
for token_id in input_ids:
if token_id == open_bracket:
pass
elif token_id == closed_bracket:
operand_idxs_stack.pop()
elif token_id in numbers:
attn_masks[i, attn_idx, attn_idx] = 1
attn_masks[i, attn_idx, operand_idxs_stack[-1]] = 1
elif token_id in operators: # Operand
operand_idxs_stack.append(attn_idx)
attn_masks[i, attn_idx, attn_idx] = 1
if len(operand_idxs_stack) > 1:
attn_masks[i, attn_idx, operand_idxs_stack[-2]] = 1
elif token_id == padding:
break
else:
raise Exception(f"Unknown character {token_id}")
attn_idx += 1
return attn_masks
def sparse_to_attention_mask(sparse_attn_mask_idxs):
result = []
for sample_idxs in sparse_attn_mask_idxs:
attn_mask = torch.zeros((512,512))
for row, col in sample_idxs:
attn_mask[row, col] = 1
result.append(attn_mask)
return torch.stack(result)
import numpy as np
blueprint_zeros = torch.zeros(100, 512, 512, device=Config.DEVICE)
from cython_attention_mask import cython_attention_mask
def batch_get_attn_mask(batch_token_ids, charset_ids):
attn_masks = np.zeros((len(batch_token_ids), 512, 512), dtype=np.int32) # blueprint_zeros.clone() # torch.zeros(100, 512, 512, device=Config.DEVICE)
for i, token_ids in enumerate(batch_token_ids):
cython_attention_mask.compute_attn_mask(token_ids.to('cpu').numpy().astype(np.int32), np.array(charset_ids).astype(np.int32), attn_masks, i)
res = torch.tensor(attn_masks).to(Config.DEVICE)# torch.tensor(attn_masks, device=Config.DEVICE)
return res # can use sparsity to reduce time taken to go on GPU
def batch_get_attn_mask_green(batch_token_ids, charset_ids):
attn_masks = np.zeros((len(batch_token_ids), 512, 512), dtype=np.int32) # blueprint_zeros.clone() # torch.zeros(100, 512, 512, device=Config.DEVICE)
for i, token_ids in enumerate(batch_token_ids):
compute_attn_mask_green(token_ids.to('cpu').numpy().astype(np.int32), np.array(charset_ids).astype(np.int32), attn_masks, i)
res = torch.tensor(attn_masks).to(Config.DEVICE)# torch.tensor(attn_masks, device=Config.DEVICE)
return res # can use sparsity to reduce time taken to go on GPU
def batch_get_attn_mask_python(batch_token_ids, charset_ids):
attn_masks = np.zeros((len(batch_token_ids), 512, 512), dtype=np.int32) # blueprint_zeros.clone() # torch.zeros(100, 512, 512, device=Config.DEVICE)
for i, token_ids in enumerate(batch_token_ids):
compute_attn_mask(token_ids.to('cpu').numpy().astype(np.int32), np.array(charset_ids).astype(np.int32), attn_masks, i)
res = torch.tensor(attn_masks).to(Config.DEVICE)# torch.tensor(attn_masks, device=Config.DEVICE)
return res # can use sparsity to reduce time taken to go on GPU
def batch_get_attn_mask_red(batch_token_ids, charset_ids):
attn_masks = np.zeros((len(batch_token_ids), 512, 512), dtype=np.int32) # blueprint_zeros.clone() # torch.zeros(100, 512, 512, device=Config.DEVICE)
for i, token_ids in enumerate(batch_token_ids):
compute_attn_mask_red(token_ids.to('cpu').numpy().astype(np.int32), np.array(charset_ids).astype(np.int32), attn_masks, i)
res = torch.tensor(attn_masks).to(Config.DEVICE)# torch.tensor(attn_masks, device=Config.DEVICE)
return res # can use sparsity to reduce time taken to go on GPU
def batch_get_full_attn(batch_token_ids, charset_ids):
attn_masks = torch.zeros((len(batch_token_ids), 512, 512)).to(Config.DEVICE) # blueprint_zeros.clone() # torch.zeros(100, 512, 512, device=Config.DEVICE)
attn_masks[batch_token_ids != 36] = 1
return attn_masks