-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha3_tests.py
218 lines (198 loc) · 6.91 KB
/
a3_tests.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
from a3_decoding import *
from a3_sampling import *
###########################################################################
# NOTE: Caution - do not modify this file!!
###########################################################################
def greedy_test(
model,
tokenizer,
all_inputs,
max_new_tokens: int
):
# 1) Load the decoder
print("-" * 50)
print("Greedy Tests")
print("-" * 50)
greedy_decoder = GreedySearchDecoderForT5(model=model, tokenizer=tokenizer)
# NOTE: always do early stopping, the way describe in the decoder skeleton
# you don't need to implement the other ways huggingface handles it
for title, inputs in all_inputs:
print("#" * 20)
print("Input: ", title)
print("#" * 20)
print("~ Your Implementation ~")
result_ids = greedy_decoder.search(
inputs=inputs,
max_new_tokens=max_new_tokens
)
if result_ids is None:
print("Input constraint encountered. Exiting...")
exit()
print("Generated sequence: ", tokenizer.batch_decode(result_ids, skip_special_tokens=False)[0])
print("Output shape: ", result_ids.shape)
print("-" * 20)
print("~ Huggingface Implementation ~")
hf_result_ids = model.generate(
**inputs,
max_new_tokens=max_new_tokens,
do_sample=False,
num_beams=1,
length_penalty=0.0,
early_stopping=True,
)
print("Generated sequence: ", tokenizer.batch_decode(hf_result_ids, skip_special_tokens=False)[0])
print("Output shape: ", hf_result_ids.shape)
print("\n")
def beam_test(
model,
tokenizer,
all_inputs,
max_new_tokens: int,
num_beams: int,
length_penalty: int,
num_return_sequences: int,
):
# 1) Load the decoder
print("-" * 50)
print("Beam Tests")
print("-" * 50)
beam_decoder = BeamSearchDecoderForT5(model=model, tokenizer=tokenizer)
# 2) Run it on the 3 examples
for title, inputs in all_inputs:
print("#" * 20)
print("Input: ", title)
print("#" * 20)
print("~ Your Implementation ~")
result_dict = beam_decoder.search(
inputs=inputs,
max_new_tokens=max_new_tokens,
num_beams=num_beams,
length_penalty=length_penalty,
num_return_sequences=num_return_sequences,
)
if result_dict is None:
print("Input constraint encountered. Exiting...")
exit()
seq_scores = enumerate(zip(result_dict["scores"], tokenizer.batch_decode(result_dict["sequences"], skip_special_tokens=False)))
for i, (score, seq) in seq_scores:
print("{}. score: {}".format(i + 1, score))
print("{}. generated sequence: {}".format(i + 1, seq))
print("Best output shape: ", result_dict["sequences"].shape)
print("-" * 20)
print("~ Huggingface Implementation ~")
hf_result_dict = model.generate(
inputs["input_ids"],
max_new_tokens=max_new_tokens,
do_sample=False,
num_beams=num_beams,
early_stopping=True,
num_return_sequences=num_return_sequences,
length_penalty=length_penalty,
return_dict_in_generate=True,
num_beam_groups=1,
constraints=None,
output_scores=True
)
seq_scores = enumerate(zip(hf_result_dict["sequences_scores"], tokenizer.batch_decode(hf_result_dict["sequences"], skip_special_tokens=False)))
for i, (score, seq) in seq_scores:
print("{}. score: {}".format(i + 1, score))
print("{}. generated sequence: {}".format(i + 1, seq))
print("Best output shape: ", hf_result_dict["sequences"].shape)
print("\n")
def top_k_test(
model,
tokenizer,
all_inputs,
max_new_tokens: int,
top_k: int,
temperature: float,
seed: int
):
# 1) Load the decoder
print("-" * 50)
print("Top-k Tests")
print("-" * 50)
top_k_sampler = TopKSamplerForT5(model=model, tokenizer=tokenizer)
# 2) Run it on the 3 examples
for title, inputs in all_inputs:
print("#" * 20)
print("Input: ", title)
print("#" * 20)
print("~ Your Implementation ~")
torch.manual_seed(seed)
result_ids = top_k_sampler.sample(
inputs=inputs,
max_new_tokens=max_new_tokens,
top_k=top_k,
temperature=temperature
)
if result_ids is None:
print("Input constraint encountered. Exiting...")
exit()
print("Generated sequence: ", tokenizer.batch_decode(result_ids, skip_special_tokens=False)[0])
print("Output shape: ", result_ids.shape)
print("-" * 20)
print("~ Huggingface Implementation ~")
torch.manual_seed(seed)
hf_result_ids = model.generate(
**inputs,
do_sample=True,
num_beams=1,
max_new_tokens=max_new_tokens,
length_penalty=0.0,
early_stopping=True,
top_k=top_k,
temperature=temperature
)
print("Generated sequence: ", tokenizer.batch_decode(hf_result_ids, skip_special_tokens=False)[0])
print("Output shape: ", hf_result_ids.shape)
print("\n")
def top_p_test(
model,
tokenizer,
all_inputs,
max_new_tokens: int,
top_p: float,
temperature: float,
seed: int
):
# 1) Load the decoder
print("-" * 50)
print("Top-p Tests")
print("-" * 50)
top_p_sampler = TopPSamplerForT5(model=model, tokenizer=tokenizer)
# 2) Run it on the 3 examples
for title, inputs in all_inputs:
print("#" * 20)
print("Input: ", title)
print("#" * 20)
print("~ Your Implementation ~")
torch.manual_seed(seed)
result_ids = top_p_sampler.sample(
inputs=inputs,
max_new_tokens=max_new_tokens,
temperature=temperature,
top_p=top_p
)
if result_ids is None:
print("Input constraint encountered. Exiting...")
exit()
print("Generated sequence: ", tokenizer.batch_decode(result_ids, skip_special_tokens=False)[0])
print("Output shape: ", result_ids.shape)
print("-" * 20)
print("~ Huggingface Implementation ~")
torch.manual_seed(seed)
hf_result_ids = model.generate(
**inputs,
do_sample=True,
num_beams=1,
max_new_tokens=max_new_tokens,
early_stopping=True,
length_penalty=0.0,
top_p=top_p,
top_k=0, # deactivate top_k sampling
temperature=temperature,
)
print("Generated sequence: ", tokenizer.batch_decode(hf_result_ids, skip_special_tokens=False)[0])
print("Output shape: ", hf_result_ids.shape)
print("\n")