-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyze.py
229 lines (205 loc) · 7.24 KB
/
analyze.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
import json
import numpy as np
from colorama import Fore as F, Style as S, Back as B
from transformers import AutoTokenizer
class ColorSettor:
_fmap = {
0: F.RED,
1: F.YELLOW,
2: F.GREEN,
3: F.RESET,
}
_bmap = {
0: B.BLUE,
1: B.CYAN,
2: B.RESET
}
_smap = {
0: S.DIM,
1: S.NORMAL,
2: S.BRIGHT
}
def __init__(self, verbose=None):
self.fore = 3
self.back = 2
self.style = 1
if verbose == '1':
self.init_display1()
elif verbose == "2":
self.init_display2()
def init_display1(self):
tmp = ["best", "good", "bad", "worst"]
o = "FORE:"
for i, c in zip((3,2,1,0), tmp):
o += self._fmap[i] + c
o += F.RESET
o += '\nSTYLE:'
for i, c in zip((2,1,0), tmp[:3]):
o += self._smap[i] + c
o += S.RESET_ALL
print(o)
def init_display2(self):
tmp = ["best", "good", "bad", "worst"]
o = "FORE: prob STYLE: rank\n"
for i in range(3):
o += self._smap[2-i]
for i, c in zip((3,2,1,0), tmp):
o += self._fmap[i] + c
o += '\n'
o += F.RESET
o += S.RESET_ALL
print(o)
@property
def state(self):
return (self.fore, self.back)
def _get_fore_and_back(self, status):
"""
p: 0~20(best)
r: 1~50000+(worst)
"""
p, r = status
output = ""
if r > 29:
scolor = 0
elif r > 1:
scolor = 1
else:
scolor = 2
if self.style != scolor:
self.style = scolor
output += self._smap[scolor]
if p > 19.8:
fcolor = 3
elif p > 18:
fcolor = 2
elif p > 12:
fcolor = 1
else:
fcolor = 0
if self.fore != fcolor:
self.fore = fcolor
output += self._fmap[fcolor]
return output
def print(self, c, status):
"""
c: one character
status: logprob and rank
"""
print(self._get_fore_and_back(status)+c, end='')
def seq_print(self, seq, statuss):
output = ""
for c, status in zip(seq, statuss):
if c == "":
continue
output += self._get_fore_and_back(status) + c
print(output)
def index_seq_print(self, seq, probs, ranks, tokenizer):
output = ""
for index, prob, rank in zip(seq, probs, ranks):
c = tokenizer.tokenizer.convert_id_to_token(index)
if c == "":
continue
output += self._get_fore_and_back((prob, rank)) + c
output = output.replace('▁', ' ')
output = output.replace('<0x0A>', '\\n')
print(output + F.RESET + S.RESET_ALL + B.RESET)
self.fore, self.style = 3, 1
class Controller:
def __init__(self, tokenizer, fp, fp_qa, verbose=None):
self.tokenizer = tokenizer
self.cs = ColorSettor(verbose=verbose)
self.status = self._load_status(fp)
self.completion = self._load_completion(fp_qa)
def _load_status(self, fp):
status = []
with open(fp) as f:
for line in f:
status.append(json.loads(line))
return status
def _load_completion(self, fp):
with open(fp) as f:
c = json.load(f)
return c
def _parse_qa_item(self, item):
"""
prompt will be added.
"""
if "preference" in item:
pref = item["preference"]
q = item["instruction"] + '\n' + item["input"]
w = item[f"output_{pref}"]
l = item[f"output_{3-pref}"]
return {
"input": q,
"w": "[Round 1]\n\n问:{}\n\n答:{}".format(q, w),
"l": "[Round 1]\n\n问:{}\n\n答:{}".format(q, l)
}
else:
q = item["input"]
w = item["win"]
l = item["lose"]
return {
"input": q,
"w": "Instruct: {}\nOutput: {}\n<|endoftext|>".format(q, w),
"l": "Instruct: {}\nOutput: {}\n<|endoftext|>".format(q, l)
}
def _display_a(self, pin, pcompletion, p_seq, r_seq, p_normalized=False):
"""
pin: prompted input
pcompletion: prompted completion (with input)
p_seq: probability sequence
r_seq: rank sequence
p_normalized: whether p_seq has been normalized.
"""
q_seq = self.tokenizer([pin], return_tensors="pt")["input_ids"].squeeze(0)
qa_seq = self.tokenizer([pcompletion], return_tensors="pt")["input_ids"].squeeze(0)
a_seq = qa_seq[len(q_seq):].tolist()
if not p_normalized:
p_seq = [20+i for i in p_seq]
self.cs.index_seq_print(a_seq, p_seq, r_seq, self.tokenizer)
def _display_one_pair(self, qa_item, status_item):
"""
status_item: id | r | l | rr | lr
parsed qa_item: (not prompted)input | w | l
"""
qa_item = self._parse_qa_item(qa_item)
print(f"No.{status_item['id'] + 1}\nQ:\n" + qa_item["input"])
qa_item["input"] = "[Round 1]\n\n问:{}\n\n答:".format(qa_item["input"])
print("[[better response]]:")
self._display_a(qa_item["input"], qa_item["w"], status_item["r"], status_item["rr"])
print("[[worse response]]:")
self._display_a(qa_item["input"], qa_item["l"], status_item["l"], status_item["lr"])
def display(self, n):
for idx, (qa_item, status_item) in enumerate(zip(self.completion, self.status)):
if idx >= n:
break
print('-' * 40)
self._display_one_pair(qa_item, status_item)
def analyze(n=10):
fpath = "qa_status/glm2_alpaca_human_pref_.jsonl"
fp_qa = "/root/dataset/alpaca_farm/alpaca_human_preference.json"
mpath = "/root/chatglm2-6b"
tokenizer = AutoTokenizer.from_pretrained(mpath, trust_remote_code=True)
controller = Controller(tokenizer, fpath, fp_qa)
controller.display(n)
def test():
q = "Append the following sentence to the end of the input.\nThe house saw strange events that night."
a = "The house saw strange events that night, and something was lurking in the shadows."
cs = ColorSettor()
mpath = "/root/chatglm2-6b"
tokenizer = AutoTokenizer.from_pretrained(mpath, trust_remote_code=True)
pin = "[Round 1]\n\n问:{}\n\n答:".format(q)
pcompletion = "[Round 1]\n\n问:{}\n\n答:{}".format(q, a)
q_seq = tokenizer([pin], return_tensors="pt")["input_ids"].squeeze(0)
qa_seq = tokenizer([pcompletion], return_tensors="pt")["input_ids"].squeeze(0)
a_seq = qa_seq[len(q_seq):].tolist()
p_seq = [-13.078125, -0.0212249755859375, -0.004405975341796875, 0.0, -0.0014657974243164062, 0.0, 0.0, -0.1346435546875, -0.020721435546875, -16.640625, -2.408203125, -11.46875, 0.0, -1.0380859375, 0.0, -0.09844970703125, -0.0009775161743164062]
p_seq = [20+i for i in p_seq]
r_seq = [59, 1, 1, 1, 1, 1, 1, 1, 1, 10086, 4, 75, 1, 2, 1, 1, 1]
print("Q:")
print(q)
print("A:")
cs.index_seq_print(a_seq, p_seq, r_seq, tokenizer)
if __name__ == "__main__":
analyze(500)
# test()