-
Notifications
You must be signed in to change notification settings - Fork 329
/
ls_t5.py
101 lines (77 loc) · 3.02 KB
/
ls_t5.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
import time
import argparse
import torch
import lightseq.inference as lsi
from transformers import T5Tokenizer, T5ForConditionalGeneration
import sentencepiece
def ls_t5(model, inputs):
torch.cuda.synchronize()
start_time = time.perf_counter()
generated_ids = model.infer(inputs)
torch.cuda.synchronize()
end_time = time.perf_counter()
return generated_ids, end_time - start_time
def hf_t5(model, inputs):
torch.cuda.synchronize()
start_time = time.perf_counter()
generated_ids = model.generate(inputs.to("cuda:0"), max_length=50)
torch.cuda.synchronize()
end_time = time.perf_counter()
return generated_ids, end_time - start_time
def ls_generate(model, tokenizer, inputs_id):
print("=========lightseq=========")
print("lightseq generating...")
ls_res_ids, ls_time = ls_t5(model, inputs_id)
# for line in ls_res_ids:
# print(line)
ls_res_ids = [ids[0] for ids in ls_res_ids[0]]
ls_res = tokenizer.batch_decode(ls_res_ids, skip_special_tokens=True)
print(f"lightseq time: {ls_time}s")
print("lightseq results:")
for sent in ls_res:
print(sent)
def hf_generate(model, tokenizer, inputs_id):
print("=========huggingface=========")
print("huggingface generating...")
hf_res_ids, hf_time = hf_t5(model, inputs_id)
hf_res = tokenizer.batch_decode(hf_res_ids, skip_special_tokens=True)
print(f"huggingface time: {hf_time}s")
print("huggingface results:")
for sent in hf_res:
print(sent)
def warmup(tokenizer, ls_model, hf_model, sentences):
inputs = tokenizer(sentences, return_tensors="pt", padding=True)
inputs_id = inputs["input_ids"]
ls_generate(ls_model, tokenizer, inputs_id)
hf_generate(hf_model, tokenizer, inputs_id)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--user_input", action="store_true")
args = parser.parse_args()
print("initializing t5 tokenizer...")
tokenizer = T5Tokenizer.from_pretrained("t5-base")
print("creating lightseq model...")
ls_model = lsi.T5("lightseq_t5_base.hdf5", 128)
print("creating huggingface model...")
hf_model = T5ForConditionalGeneration.from_pretrained("t5-base")
hf_model.to("cuda:0")
hf_model.eval()
sentences = [
"The <extra_id_0> walks in <extra_id_1> park",
"summerize: Tom and Alice go to cinema, and watched the most impactful movie",
]
print("====================START warmup====================")
warmup(tokenizer, ls_model, hf_model, sentences)
print("====================END warmup====================")
while True:
if args.user_input:
sentences = [input("input the masked sentence:\n")]
print("tokenizing the sentences...")
inputs = tokenizer(sentences, return_tensors="pt", padding=True)
inputs_id = inputs["input_ids"]
ls_generate(ls_model, tokenizer, inputs_id)
hf_generate(hf_model, tokenizer, inputs_id)
if not args.user_input:
break
if __name__ == "__main__":
main()