-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathattribute_concen_utils.py
156 lines (130 loc) · 5.04 KB
/
attribute_concen_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
import torch.distributions as dist
from typing import List, Dict
import itertools
import pdb
import torch
start_token = "<|startoftext|>"
end_token = "<|endoftext|>"
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 == target_word:
break
wp2 = wordpieces2indices[wp_idx].replace("</w>", "")
if target_word.startswith(wp + wp2) and wp2 != target_word:
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 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