-
Notifications
You must be signed in to change notification settings - Fork 20
/
run_infer_main.py
275 lines (249 loc) · 10.4 KB
/
run_infer_main.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# Copyright 2023 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""lite infer main."""
import sys
import argparse
from threading import Thread
# Avoid bugs when mslite and mindspore are not built from same commit, which may cause running error.
# pylint: disable=W0611
import mindspore_lite as mslite
from mindformers.models.base_tokenizer import Tokenizer
from mindformers.models import BloomTokenizer, LlamaTokenizer
from mindformers.models import ChatGLMTokenizer, ChatGLM2Tokenizer, GPT2Tokenizer
from mindformers.pipeline import pipeline
from mindformers.generation import TextIteratorStreamer
from mindformers.tools.utils import str2bool
from mindformers.inference import InferConfig, InferTask
from research.baichuan2.baichuan2_tokenizer import Baichuan2Tokenizer
from research.qwen.qwen_tokenizer import QwenTokenizer
def pipeline_from_model_paths(args_, tokenizer):
"""build infer pipeline for model paths."""
lite_pipeline = pipeline(
task="text_generation",
model=(args_.prefill_model_path, args_.increment_model_path),
tokenizer=tokenizer,
backend="mslite",
model_name=args_.model_name,
ge_config_path=args_.config_path,
device_id=args_.device_id,
infer_seq_length=args_.seq_length,
)
return lite_pipeline
def pipeline_from_model_name(args_, tokenizer):
"""build infer pipeline for model name."""
lite_pipeline = pipeline(
task="text_generation",
model=args_.model_name,
tokenizer=tokenizer,
backend="mslite",
ge_config_path=args_.config_path,
device_id=args_.device_id,
infer_seq_length=args_.seq_length,
)
return lite_pipeline
def pipeline_from_model_dir(args_, tokenizer):
"""build infer pipeline for model dir."""
lite_pipeline = pipeline(
task="text_generation",
model=args_.model_dir,
tokenizer=tokenizer,
backend="mslite",
model_name=args_.model_name,
ge_config_path=args_.config_path,
device_id=args_.device_id,
infer_seq_length=args_.seq_length,
)
return lite_pipeline
def pipeline_from_infer_config(args_, tokenizer):
"""build infer pipeline for infer config."""
lite_config = InferConfig(
prefill_model_path=args_.prefill_model_path,
increment_model_path=args_.increment_model_path,
model_type="mindir",
model_name=args_.model_name,
ge_config_path=args_.config_path,
device_id=args_.device_id,
infer_seq_length=args_.seq_length,
)
lite_pipeline = InferTask.get_infer_task("text_generation", lite_config, tokenizer=tokenizer)
return lite_pipeline
# the model name list that mslite inference has supported.
LITE_SUPPORT_MODELS = {
'bloom': BloomTokenizer,
'glm': ChatGLMTokenizer,
'glm2': ChatGLM2Tokenizer,
'gpt2': GPT2Tokenizer,
'codegeex2': ChatGLM2Tokenizer,
'qwen': QwenTokenizer,
'llama': LlamaTokenizer,
'llama2': LlamaTokenizer,
'baichuan2': Baichuan2Tokenizer
}
def get_tokenizer(model_name: str, tokenizer_path: str) -> Tokenizer:
"""get tokenizer with model name."""
tokenizer = None
lite_support_model = model_name.split('_')[0]
if lite_support_model in LITE_SUPPORT_MODELS:
if tokenizer_path is not None:
tokenizer = LITE_SUPPORT_MODELS[lite_support_model](vocab_file=tokenizer_path)
else:
tokenizer = LITE_SUPPORT_MODELS[lite_support_model].from_pretrained(model_name)
else:
lite_support_list = tuple(LITE_SUPPORT_MODELS.keys())
raise ValueError(
f"model must be in {lite_support_list} when getting tokenizer, but got input {model_name}.")
return tokenizer
def build_prompt(inputs, model_name, prompt):
"""build prompt for inputs"""
if model_name.startswith('baichuan2'):
if not prompt:
prompt = "<reserved_106>{}<reserved_107>"
else:
prompt = "<reserved_106>" + prompt + "<reserved_107>"
if not prompt:
return inputs
if prompt.find("{}") != -1:
return prompt.format(inputs)
raise ValueError(
"The prompt is invalid! Please make sure your prompt contains placeholder '{}' to replace user input.")
def infer_main(args_):
"""lite infer main."""
tokenizer = get_tokenizer(args_.model_name.lower(), args_.tokenizer_path)
lite_pipeline = pipeline_from_infer_config(
args_, tokenizer
)
while True:
user_input = input("Please enter your predict data: \n")
if user_input == "exit":
print("Task is over.")
sys.exit()
user_input = build_prompt(user_input, args_.model_name.lower(), args_.prompt)
output = lite_pipeline.infer(user_input,
do_sample=args_.do_sample,
top_k=args_.top_k,
top_p=args_.top_p,
repetition_penalty=args_.repetition_penalty,
temperature=args_.temperature,
max_length=args_.max_length,
is_sample_acceleration=args_.is_sample_acceleration,
add_special_tokens=args_.add_special_tokens)
print(output)
def infer_stream_main(args_):
"""main entry for infer stream."""
tokenizer = get_tokenizer(args_.model_name.lower(), args_.tokenizer_path)
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True)
lite_pipeline = pipeline_from_model_paths(
args_, tokenizer
)
while True:
user_input = input("Please enter your predict data(input 'exit' to quit): \n")
if user_input == "exit":
print("Quit now, this may take a while.")
sys.exit()
user_input = build_prompt(user_input, args_.model_name.lower(), args_.prompt)
generation_kwargs = dict(inputs=user_input,
streamer=streamer,
is_sample_acceleration=args_.is_sample_acceleration,
add_special_tokens=args_.add_special_tokens)
thread = Thread(target=lite_pipeline, kwargs=generation_kwargs)
thread.start()
output = ""
for new_text in streamer:
print(new_text)
output += new_text
print(output)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
'--device_id', default=0, type=int,
help='ID of the target device, the value must be in [0, device_num_per_host-1],'
'while device_num_per_host should be no more than 4096. Default: None')
parser.add_argument(
'--rank_id', default=0, type=int,
help='ID of the target device, the value must be in [0, device_num_per_host-1],'
'while device_num_per_host should be no more than 4096. Default: None')
parser.add_argument(
'--model_dir', default=None, type=str,
help="This model dir path."
"Default: None")
parser.add_argument(
'--model_name', default="common", type=str,
help=f"The model name, only supports name in {LITE_SUPPORT_MODELS}."
"Default: None")
parser.add_argument(
'--seq_length', default=2048, type=int,
help="This model dir path."
"Default: None")
parser.add_argument(
'--tokenizer_path', default=None, type=str,
help="Tokenizer model to load."
"Default: None")
parser.add_argument(
'--prefill_model_path', default=None, type=str,
help="This full model path."
"Default: None")
parser.add_argument(
'--increment_model_path', default=None, type=str,
help="When use kv-cache, this is cache mode path."
"Default: None")
parser.add_argument(
'--config_path', default=None, type=str,
help="ge config file path."
"Default: None")
parser.add_argument(
'--do_sample', default=False, type=str2bool,
help="Whether postprocess in graph or not."
"Default: False")
parser.add_argument(
'--top_k', default=1, type=int,
help="top k."
"Default: 1")
parser.add_argument(
'--top_p', default=1.0, type=float,
help="top p."
"Default: 1.0")
parser.add_argument(
'--repetition_penalty', default=1.0, type=float,
help="repetition penalty."
"Default: 1.0")
parser.add_argument(
'--temperature', default=1.0, type=float,
help="The value used to modulate the next token probabilities."
"Default: 1.0")
parser.add_argument(
'--max_length', default=512, type=int,
help="The maximum word length that can be generated."
"Default: 512")
parser.add_argument(
'--is_sample_acceleration', default=False, type=str2bool,
help="Whether postprocess in graph or not."
"Default: False")
parser.add_argument(
'--add_special_tokens', default=False, type=str2bool,
help="Whether preprocess add special tokens or not."
"Default: False")
parser.add_argument(
'--stream', default=False, type=str2bool,
help="Whether decode in stream or not."
"Default: False")
parser.add_argument(
'--prompt', default=None, type=str,
help="The content of prompt."
"Default: None")
args = parser.parse_args()
if args.stream:
infer_stream_main(args)
else:
infer_main(args)