forked from microsoft/promptbench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualize.py
147 lines (100 loc) · 4.7 KB
/
visualize.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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
def vis_by_grad(model, tokenizer, input_sentence, label):
model.eval()
def map_subwords_to_words(sentence, tokenizer):
tokens = tokenizer.tokenize(sentence)
mapping = []
i = 0
for token in tokens:
if token[0] == "▁":
mapping.append(i)
i += 1
else:
mapping.append(i - 1)
return mapping, tokens
# input_len = len(input_sentence.split())
mapping, tokens = map_subwords_to_words(input_sentence, tokenizer)
words = "".join(tokens).replace("▁", " ").split()
input_len = len(words)
inputs = tokenizer(input_sentence, return_tensors="pt")
embeddings = model.get_input_embeddings()(inputs['input_ids'])
embeddings.requires_grad_()
embeddings.retain_grad()
labels = tokenizer(label, return_tensors="pt")["input_ids"]
outputs = model(inputs_embeds=embeddings, attention_mask=inputs['attention_mask'], labels=labels)
outputs.loss.backward()
# print(outputs.loss.item())
grads = embeddings.grad
# print(grads.shape)
import torch
word_grads = [torch.zeros_like(grads[0][0]) for _ in range(input_len)] # 初始化每个单词的梯度向量
# ignore the [EOS] token
for idx, grad in enumerate(grads[0][:len(mapping)]):
word_grads[mapping[idx]] += grad
words_importance = [grad.norm().item() for grad in word_grads]
import numpy as np
""" normalize importance by min-max"""
min_importance = np.min(words_importance)
max_importance = np.max(words_importance)
words_importance = (words_importance - min_importance) / (max_importance - min_importance)
# word_importance_dict = {}
# for word, importance in zip(words, word_importance):
# print(f"The gradient for '{word}' is {grad}")
# word_importance_dict[word] = importance
return words, words_importance
def vis_by_delete(model, tokenizer, input_sentence, label):
import copy
words = input_sentence.split()
encoded_label = tokenizer(label, return_tensors="pt")["input_ids"]
inputs = tokenizer(input_sentence, return_tensors="pt")
outputs = model(**inputs, labels=encoded_label)
original_loss = outputs.loss.item()
word_importance = []
for i in range(len(words)):
new_words = copy.deepcopy(words)
del new_words[i]
new_sentence = ' '.join(new_words)
inputs = tokenizer(new_sentence, return_tensors="pt")
outputs = model(**inputs, labels=encoded_label)
new_loss = outputs.loss.item()
importance = abs(new_loss - original_loss)
word_importance.append(importance)
import numpy as np
""" normalize importance by min-max"""
min_importance = np.min(word_importance)
max_importance = np.max(word_importance)
word_importance = (word_importance - min_importance) / (max_importance - min_importance)
word_importance_dict = {}
for word, importance in zip(words, word_importance):
word_importance_dict[word] = importance
return word_importance_dict
def save_importance(words, importance):
from html import escape
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import numpy as np
cmap = plt.colormaps['Reds']
latex_output = ''
for i, word in enumerate(words):
rgba = cmap(importance[i])
rgb = ','.join(str(int(rgba[j]*255)) for j in range(3))
# latex_output += '\\colorbox[RGB]{' + rgb + '}{' + word + '\\vphantom{fg}}\\hspace*{0pt}'
latex_output += word + ' '
return latex_output
if __name__ == "__main__":
from transformers import T5Tokenizer, T5ForConditionalGeneration
tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-large", device_map="auto")
model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-large", device_map="auto")
input_sentence = "As an instrument for entailment evaluation, consider the two sentences and determine if their relationship is 'entailment' or 'not_entailment'. Respond with 'entailment' or 'not_entailment' and true is true :"
label = 'not_entailment'
print("================by grad================")
words, words_importance = vis_by_grad(model, tokenizer, input_sentence, label)
for word, importance in zip(words, words_importance):
print(f"{word:10}: {importance:.4f}")
print()
# print("================by delete================")
# word_importance_dict = vis_by_delete(model, tokenizer, input_sentence, label)
# for word, importance in word_importance_dict.items():
# print(f"{word:10}: {importance:.4f}")
# print()