-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdecode.py
205 lines (181 loc) · 8.94 KB
/
decode.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
# Load model directly
from transformers import AutoTokenizer, AutoModelForCausalLM
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
import torch
import time
import matplotlib.colors as mcolors
from matplotlib.colors import LinearSegmentedColormap
from tqdm.notebook import tqdm
import re
from typing import Tuple, List
from preference import *
# model_name = "mistralai/Mistral-7B-v0.1"
# model_name = "mlabonne/Monarch-7B"
model_name = "HuggingFaceH4/zephyr-7b-beta"
# model_name = "mistralai/Mixtral-8x7B-v0.1"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
""""
--- Cool Function ---
* Check the log probability of any response given a query | Given a huggingface Model & Tokenizer
"""
def check_response_prob(model, tokenizer, query, target_response, max_required_tokens=80):
inputs = tokenizer([query], return_tensors="pt")
gen_out = model.generate(**inputs, output_scores=True, return_dict_in_generate=True, max_new_tokens=max_required_tokens, pad_token_id=tokenizer.eos_token_id)
target_ids = tokenizer.encode(target_response)
sum_of_logits = 0
sum_of_probs = 0
for i, id in enumerate(target_ids):
prob = torch.nn.functional.softmax(gen_out.scores[i], dim=1)[0, id]
sum_of_probs += prob
avg_prob = sum_of_probs / len(target_ids)
return avg_prob
check_token_length = lambda word: len(tokenizer(word).input_ids)
get_max_tokens_required = lambda words: max([check_token_length(word) for word in words])
def single_choice_response(model, tokenizer, query, possible_answers):
max_tokens_required = get_max_tokens_required(possible_answers)
unorm_probs = []
for target_response in possible_answers:
prob = check_response_prob(model, tokenizer, query, target_response=target_response, max_required_tokens=max_tokens_required)
unorm_probs.append(prob)
norm_const = sum(unorm_probs)
probs = [prob / norm_const for prob in unorm_probs]
return probs
query_template = """Compare customers' response in the two conversations:
Conversation A: {conversation_a}
Conversation B: {conversation_b}
{compare_query}
Your answer: """
def trinary_to_comparative_score(list_of_prob, possible_answers=["Yes", "No", "Unsure"]):
if list_of_prob[2] > 0.3:
return [0.5,0.5]
else:
norm_const = list_of_prob[0]+list_of_prob[1]
return [list_of_prob[0]/norm_const, list_of_prob[1]/norm_const]
# Multi-Attributes Pairwise Comparison
def multi_attributes_pairwise_comparison(model, tokenizer, compare_attributes, conversation_pairs,
query_template,
possible_answers):
pred_preferences = []
for i, compare_query in enumerate(compare_attributes):
query = query_template.format(compare_query=compare_query, conversation_a=conversation_pairs[0], conversation_b=conversation_pairs[1])
compare_result = single_choice_response(model, tokenizer, query, possible_answers)
pred_preferences.append(compare_result)
return pred_preferences
# Decoding-based Multi-Attribute Pairwise Comparison
def pairmatch_decode(conversation_pairs: Tuple[str, str],
requirements: Requirement,
query_template = query_template,
possible_answers = ["Yes", "No", "Unsure"],
model = model,
tokenizer = tokenizer) -> List[dict]:
"""
Open-sourced LLM based Multi-Attribute Comparative Rater
"""
compare_attributes = requirements.get_anno_compare_queries()
compare_names = requirements.get_attribute_names()
pred_preferenes = multi_attributes_pairwise_comparison(model, tokenizer, compare_attributes, conversation_pairs, query_template, possible_answers)
results = {name: trinary_to_comparative_score(pred) for (name, pred) in zip(compare_names, pred_preferenes)}
return results
"""
Now, I wish to do tree-search to locate the confident answer, and NOT the confident continuation
1. Should be doable by checking on the next-token
"""
def get_next_token_logit(model, tokenizer, query):
inputs = tokenizer([query], return_tensors="pt")
gen_out = model.generate(**inputs, output_scores=True, return_dict_in_generate=True, max_new_tokens=1, pad_token_id=tokenizer.eos_token_id)
return gen_out.scores[-1]
# Get Top-k next logits, then greedy-1 search afterwards
def get_k_branch(model, tokenizer, query, k=5):
logit = get_next_token_logit(model, tokenizer, query)
k_token = logit[0].argsort()[-k:]
k_response = []
for token in k_token:
new_query = query + tokenizer.decode(token)
candidate_inputs = tokenizer(new_query, return_tensor="pt")
gen_out = model.generate(**candidate_inputs, output_scores=True, return_dict_in_generate=True)
k_response.append(tokenizer.decode(gen_out.sequences[0], skip_special_tokens=True))
return k_response
# Token Path Probability
def get_token_path_prob(gen_out, num_append:int = 1):
logits = gen_out.scores
num_output = len(logits)
output_ids = gen_out.sequences[0][-num_output-num_append:]
# output = tokenizer.decode(output_ids, skip_special_tokens=True)
path_prob = torch.stack([score[0].max() for score in logits])
path_prob = torch.nn.functional.softmax(path_prob, dim=0)
# path_logprob = torch.log(path_prob)
return output_ids, path_prob
# Word Path Probability -- Ensemble(word[token1,token2,...]) is the average probability of token appearance likelihood
def get_path_prob(gen_out, init_token_prob=None):
if init_token_prob is None:
token_ids, probs = get_token_path_prob(gen_out, num_append=0)
else:
token_ids, probs = get_token_path_prob(gen_out)
probs = torch.concat([init_token_prob, probs])
current_n_words = 0
current_prob = 0
word_probs = []
ids = []
current_n_tokens = 0
word_prob = 0
current_n_words = 0
for token_id, prob in zip(token_ids, probs):
ids.append(token_id)
decode_seq = tokenizer.decode(ids)
# print('Decode Sequence: ', decode_seq)
words = re.split(r' |\n|\.\|:', decode_seq)
# print('Splitted Words: ')
# print(words)
word = words[-1]
if len(words) == current_n_words:
word_prob += prob
current_n_tokens += 1
# more than one tokens correspond to the same word, word gets updated
word_probs[-1] = (word, word_prob / current_n_tokens) # replace the previous word in the word prob list
elif len(words) > current_n_words: # A old word is determined
word_prob = prob
current_n_tokens = 1
word_probs.append((word, word_prob / current_n_tokens))
current_n_words += 1
return word_probs
def get_k_path_prob(model, tokenizer, query, k, max_new_tokens=80):
logit = get_next_token_logit(model, tokenizer, query)
k_token = logit[0].argsort()[-k:]
k_prob = torch.nn.functional.softmax(logit[0][logit[0].argsort()[-k:]], dim=0)
k_response = []
for token in k_token:
new_query = query + tokenizer.decode(token)
candidate_inputs = tokenizer(new_query, return_tensors="pt")
gen_out = model.generate(**candidate_inputs, output_scores=True, return_dict_in_generate=True, max_new_tokens=max_new_tokens)
path_probs = get_path_prob(gen_out, k_prob)
print(path_probs)
print('----'*5)
k_response.append(path_probs)
return k_response
def get_follow_up_output(model, tokenizer, follow_up_template, gen_out, max_new_tokens=40):
construct_input = lambda new_ids: {'input_ids': new_ids, "attention_mask":torch.ones_like(new_ids)}
output_ids = gen_out.sequences
follow_up_ids = tokenizer(follow_up_template, return_tensors="pt")['input_ids']
new_ids = torch.concat([output_ids, follow_up_ids], axis=1)
inputs = construct_input(new_ids)
gen_out = model.generate(**inputs, output_scores=True, return_dict_in_generate=True, max_new_tokens=max_new_tokens, pad_token_id=tokenizer.eos_token_id)
return gen_out
def get_k_path_prob_follow_up(model, tokenizer, query, k, max_new_tokens=80,
follow_up_template=" So the answer is: "):
logit = get_next_token_logit(model, tokenizer, query)
k_token = logit[0].argsort()[-k:]
k_prob = torch.nn.functional.softmax(logit[0][logit[0].argsort()[-k:]], dim=0)
k_response = []
for token in k_token:
new_query = query + tokenizer.decode(token)
candidate_inputs = tokenizer(new_query, return_tensors="pt")
gen_out = model.generate(**candidate_inputs, output_scores=True, return_dict_in_generate=True, max_new_tokens=max_new_tokens, pad_token_id=tokenizer.eos_token_id)
follow_up_out = get_follow_up_output(model, tokenizer, follow_up_template, gen_out)
path_probs = get_path_prob(follow_up_out, k_prob)
print(path_probs)
print('----'*5)
k_response.append(path_probs)
return k_response