-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_generation.py
30 lines (22 loc) · 1.12 KB
/
test_generation.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
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
PATH = 'your/path/to/hf/checkpoints'
tokenizer = AutoTokenizer.from_pretrained(PATH,
padding_side='left',
truncation_side='left',
use_fast=True,
trust_remote_code=True)
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
model = AutoModelForCausalLM.from_pretrained(PATH, trust_remote_code=True, torch_dtype=torch.bfloat16).cuda()
model.eval()
print(model)
prompts = ["Austria, is a landlocked country in Central Europe", "小米公司",]
inputs = tokenizer(prompts, padding=True, truncation=True, max_length=512, return_tensors='pt').to('cuda')
print(inputs.input_ids.shape)
outputs = model.generate(inputs.input_ids, do_sample=False, max_new_tokens=256, attention_mask=inputs.attention_mask)
response = tokenizer.batch_decode(outputs, skip_special_tokens=True)
for i in range(len(prompts)):
print("=====Begin======")
print(response[i])
print("======End======")