-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy pathinfer.py
74 lines (62 loc) · 2.59 KB
/
infer.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
import os
from typing import Optional
import fire
import torch
import transformers
from packaging import version
from utils.modeling_hack import get_model
from utils.streaming import generate_stream
os.environ["TOKENIZERS_PARALLELISM"] = "false"
tok_ins = "\n\n### Instruction:\n"
tok_res = "\n\n### Response:\n"
prompt_input = tok_ins + "{instruction}" + tok_res
def main(
model_path: str,
max_input_length: int = 512,
max_generate_length: int = 1024,
model_type: str = 'chat',
rope_scaling: Optional[str] = None,
rope_factor: float = 8.0,
streaming: bool = True # streaming is always enabled now
):
assert version.parse(transformers.__version__) >= version.parse("4.34")
assert model_type.lower() in ['chat', 'base'], f"model_type must be one of ['chat', 'base'], got {model_type}"
assert rope_scaling in [None, 'yarn',
'dynamic'], f"rope_scaling must be one of [None, 'yarn', 'dynamic'], got {rope_scaling}"
model, tokenizer, generation_config = get_model(model_path=model_path, rope_scaling=rope_scaling,
rope_factor=rope_factor)
generation_config.max_new_tokens = max_generate_length
generation_config.max_length = max_input_length + max_generate_length
device = torch.cuda.current_device()
sess_text = ""
while True:
raw_text = input("prompt(\"exit\" to end, \"clear\" to clear session) >>> ")
if not raw_text:
print('prompt should not be empty!')
continue
if raw_text.strip() == "exit":
print('session ended.')
break
if raw_text.strip() == "clear":
print('session cleared.')
sess_text = ""
continue
query_text = raw_text.strip()
sess_text += tok_ins + query_text
if model_type == 'chat':
input_text = prompt_input.format_map({'instruction': sess_text.split(tok_ins, 1)[1]})
else:
input_text = query_text
inputs = tokenizer(input_text, return_tensors='pt', truncation=True, max_length=max_input_length)
inputs = {k: v.to(device) for k, v in inputs.items()}
print('=' * 100)
answer = ''
for text in generate_stream(model, tokenizer, inputs['input_ids'], inputs['attention_mask'],
generation_config=generation_config):
print(text, end='', flush=True)
answer += text
sess_text += tok_res + answer
print('')
print("=" * 100)
if __name__ == "__main__":
fire.Fire(main)