forked from Cornell-RelaxML/quip-sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteractive_gen.py
46 lines (39 loc) · 1.67 KB
/
interactive_gen.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
import argparse
import os
import glog
import torch
from torch.profiler import profile, record_function, ProfilerActivity
from transformers import AutoTokenizer
from lib.utils.unsafe_import import model_from_hf_path
import time
torch.set_grad_enabled(False)
parser = argparse.ArgumentParser()
parser.add_argument('--hf_path', default='meta-llama/Llama-2-70b-hf', type=str)
parser.add_argument('--max_length', default=64, type=int)
parser.add_argument('--no_use_flash_attn', action='store_true')
def main(args):
model, model_str = model_from_hf_path(args.hf_path,
use_cuda_graph=False,
use_flash_attn=not args.no_use_flash_attn)
tokenizer = AutoTokenizer.from_pretrained(model_str)
tokenizer.pad_token = tokenizer.eos_token
while True:
print()
prompt = input("Please enter your prompt or 'quit' (without quotes) to quit: ")
if prompt == 'quit':
return
inputs = tokenizer(prompt, return_tensors='pt')
outputs = model.generate(input_ids=inputs['input_ids'].cuda(),
attention_mask=inputs['attention_mask'].cuda(),
max_length=args.max_length,
penalty_alpha=0.6,
top_k=4,
use_cache=True,
return_dict_in_generate=True).sequences[0]
print()
print('Model Output: ', tokenizer.decode(outputs, skip_special_tokens=True))
if __name__ == '__main__':
torch.set_grad_enabled(False)
torch.manual_seed(0)
args = parser.parse_args()
main(args)