-
Notifications
You must be signed in to change notification settings - Fork 1
/
prompt_utils.py
331 lines (272 loc) · 11.5 KB
/
prompt_utils.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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import torch
from diffusers import DiffusionPipeline
from transformers import AutoTokenizer
import spacy
from typing import List, Dict
start_token = "<|startoftext|>"
end_token = "<|endoftext|>"
def to_intlist(curr_list):
rst = []
for idx in curr_list:
if isinstance(idx, list):
idx = to_intlist(idx)
for i in idx:
rst.append(i)
else:
rst.append(idx)
return rst
def split_indices(related_indices: List[int]):
noun = related_indices[-1] # assumes noun is always last in the list
modifier = related_indices[:-1]
if isinstance(modifier, int):
modifier = [modifier]
if isinstance(noun, int):
noun = [noun]
return noun, modifier
def align_wordpieces_indices(
wordpieces2indices, start_idx, target_word
):
"""
Aligns a `target_word` that contains more than one wordpiece (the first wordpiece is `start_idx`)
"""
wp_indices = [start_idx]
wp = wordpieces2indices[start_idx].replace("</w>", "")
# Run over the next wordpieces in the sequence (which is why we use +1)
for wp_idx in range(start_idx + 1, len(wordpieces2indices)):
if wp.lower() == target_word.lower():
break
wp2 = wordpieces2indices[wp_idx].replace("</w>", "")
if target_word.lower().startswith(wp.lower() + wp2.lower()) and wp2.lower() != target_word.lower():
wp += wordpieces2indices[wp_idx].replace("</w>", "")
wp_indices.append(wp_idx)
else:
wp_indices = (
[]
) # if there's no match, you want to clear the list and finish
break
return wp_indices
def extract_attribution_indices(doc):
# doc = parser(prompt)
subtrees = []
modifiers = ["amod", "nmod", "compound", "npadvmod", "advmod", "acomp"]
for w in doc:
if w.pos_ not in ["NOUN", "PROPN"] or w.dep_ in modifiers:
continue
subtree = []
stack = []
for child in w.children:
if child.dep_ in modifiers:
subtree.append(child)
stack.extend(child.children)
while stack:
node = stack.pop()
if node.dep_ in modifiers or node.dep_ == "conj":
subtree.append(node)
stack.extend(node.children)
if subtree:
subtree.append(w)
subtrees.append(subtree)
return subtrees
def extract_attribution_indices_with_verbs(doc):
'''This function specifically addresses cases where a verb is between
a noun and its modifier. For instance: "a dog that is red"
here, the aux is between 'dog' and 'red'. '''
subtrees = []
modifiers = ["amod", "nmod", "compound", "npadvmod", "advmod", "acomp",
'relcl']
for w in doc:
if w.pos_ not in ["NOUN", "PROPN"] or w.dep_ in modifiers:
continue
subtree = []
stack = []
for child in w.children:
if child.dep_ in modifiers:
if child.pos_ not in ['AUX', 'VERB']:
subtree.append(child)
stack.extend(child.children)
while stack:
node = stack.pop()
if node.dep_ in modifiers or node.dep_ == "conj":
# we don't want to add 'is' or other verbs to the loss, we want their children
if node.pos_ not in ['AUX', 'VERB']:
subtree.append(node)
stack.extend(node.children)
if subtree:
subtree.append(w)
subtrees.append(subtree)
return subtrees
def extract_attribution_indices_with_verb_root(doc):
'''This function specifically addresses cases where a verb is between
a noun and its modifier. For instance: "a dog that is red"
here, the aux is between 'dog' and 'red'. '''
subtrees = []
modifiers = ["amod", "nmod", "compound", "npadvmod", "advmod", "acomp"]
for w in doc:
subtree = []
stack = []
# if w is a verb/aux and has a noun child and a modifier child, add them to the stack
if w.pos_ != 'AUX' or w.dep_ in modifiers:
continue
for child in w.children:
if child.dep_ in modifiers or child.pos_ in ['NOUN', 'PROPN']:
if child.pos_ not in ['AUX', 'VERB']:
subtree.append(child)
stack.extend(child.children)
# did not find a pair of noun and modifier
if len(subtree) < 2:
continue
while stack:
node = stack.pop()
if node.dep_ in modifiers or node.dep_ == "conj":
# we don't want to add 'is' or other verbs to the loss, we want their children
if node.pos_ not in ['AUX']:
subtree.append(node)
stack.extend(node.children)
if subtree:
if w.pos_ not in ['AUX']:
subtree.append(w)
subtrees.append(subtree)
return subtrees
def extract_entities_only(doc):
entities = []
for w in doc:
if w.pos_ in ['NOUN', 'PROPN']:
entities.append([w])
return entities
def get_indices(tokenizer, prompt: str) -> Dict[str, int]:
"""Utility function to list the indices of the tokens you wish to alter"""
ids = tokenizer(prompt).input_ids
indices = {
i: tok
for tok, i in zip(
tokenizer.convert_ids_to_tokens(ids), range(len(ids))
)
}
return indices
def get_attention_map_index_to_wordpiece(tokenizer, prompt):
attn_map_idx_to_wp = {}
wordpieces2indices = get_indices(tokenizer, prompt)
# Ignore `start_token` and `end_token`
for i in list(wordpieces2indices.keys())[1:-1]:
wordpiece = wordpieces2indices[i]
wordpiece = wordpiece.replace("</w>", "")
attn_map_idx_to_wp[i] = wordpiece
return attn_map_idx_to_wp
def unify_lists(list_of_lists):
def flatten(lst):
for elem in lst:
if isinstance(elem, list):
yield from flatten(elem)
else:
yield elem
def have_common_element(lst1, lst2):
flat_list1 = set(flatten(lst1))
flat_list2 = set(flatten(lst2))
return not flat_list1.isdisjoint(flat_list2)
lst = []
for l in list_of_lists:
lst += l
changed = True
while changed:
changed = False
merged_list = []
while lst:
first = lst.pop(0)
was_merged = False
for index, other in enumerate(lst):
if have_common_element(first, other):
# If we merge, we should flatten the other list but not first
new_merged = first + [item for item in other if item not in first]
lst[index] = new_merged
changed = True
was_merged = True
break
if not was_merged:
merged_list.append(first)
lst = merged_list
return lst
class PromptParser:
def __init__(self, model_path = None) -> None:
self.tokenizer = AutoTokenizer.from_pretrained(model_path, subfolder = 'tokenizer',torch_dtype=torch.float16,variant="fp16",)
def set_doc(self, doc):
self.doc = doc
def _align_indices(self, prompt, spacy_pairs):
wordpieces2indices = get_indices(self.tokenizer, prompt)
paired_indices = []
collected_spacy_indices = (
set()
) # helps track recurring nouns across different relations (i.e., cases where there is more than one instance of the same word)
for pair in spacy_pairs:
curr_collected_wp_indices = (
[]
) # helps track which nouns and amods were added to the current pair (this is useful in sentences with repeating amod on the same relation (e.g., "a red red red bear"))
for member in pair:
for idx, wp in wordpieces2indices.items():
if wp in [start_token, end_token]:
continue
wp = wp.replace("</w>", "")
if member.text.lower() == wp.lower():
if idx not in curr_collected_wp_indices and idx not in collected_spacy_indices:
curr_collected_wp_indices.append(idx)
break
# take care of wordpieces that are split up
elif member.text.lower().startswith(wp.lower()) and wp.lower() != member.text.lower(): # can maybe be while loop
wp_indices = align_wordpieces_indices(
wordpieces2indices, idx, member.text
)
# check if all wp_indices are not already in collected_spacy_indices
if wp_indices and (wp_indices not in curr_collected_wp_indices) and all(
[wp_idx not in collected_spacy_indices for wp_idx in wp_indices]):
curr_collected_wp_indices.append(wp_indices)
break
for collected_idx in curr_collected_wp_indices:
if isinstance(collected_idx, list):
for idx in collected_idx:
collected_spacy_indices.add(idx)
else:
collected_spacy_indices.add(collected_idx)
if curr_collected_wp_indices:
paired_indices.append(curr_collected_wp_indices)
else:
print(f"No wordpieces were aligned for {pair} in _align_indices")
return paired_indices
def _extract_attribution_indices(self, prompt):
modifier_indices = []
# extract standard attribution indices
modifier_sets_1 = extract_attribution_indices(self.doc)
modifier_indices_1 = self._align_indices(prompt, modifier_sets_1)
if modifier_indices_1:
modifier_indices.append(modifier_indices_1)
# extract attribution indices with verbs in between
modifier_sets_2 = extract_attribution_indices_with_verb_root(self.doc)
modifier_indices_2 = self._align_indices(prompt, modifier_sets_2)
if modifier_indices_2:
modifier_indices.append(modifier_indices_2)
modifier_sets_3 = extract_attribution_indices_with_verbs(self.doc)
modifier_indices_3 = self._align_indices(prompt, modifier_sets_3)
if modifier_indices_3:
modifier_indices.append(modifier_indices_3)
# entities only
self.include_entities = True
if self.include_entities:
modifier_sets_4 = extract_entities_only(self.doc)
modifier_indices_4 = self._align_indices(prompt, modifier_sets_4)
modifier_indices.append(modifier_indices_4)
# make sure there are no duplicates
modifier_indices = unify_lists(modifier_indices)
# print(f"Final modifier indices collected:{modifier_indices}")
return modifier_indices
def _split_prompt(self, doc):
prompt_ls = []
for chunk in doc.noun_chunks:
if chunk.text not in ['top', 'the side', 'the left', 'the right']: # todo remove some phrases
prompt_ls.append(chunk.text)
return prompt_ls
def _get_indices(self, prompt):
subtrees = self._extract_attribution_indices(prompt)
all_indices = []
for subtree in subtrees:
noun, modifier = split_indices(subtree)
noun, modifier = to_intlist(noun), to_intlist(modifier)
all_indices.append([noun, modifier])
return all_indices