-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-model.py
66 lines (53 loc) · 2.36 KB
/
test-model.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
# Load model directly
from transformers import AutoTokenizer, AutoModelForCausalLM, TextStreamer
from datetime import datetime
import torch
import argparse
import os
import json
device = "cuda:0" if torch.cuda.is_available() else "cpu"
print(device)
parser = argparse.ArgumentParser()
parser.add_argument("--model", type=str, nargs='?', default="meta-llama/Llama-2-7b-chat-hf")
parser.add_argument("--quantization", type=int, nargs='?', default=16)
parser.add_argument("--token", type=str, nargs='?', default="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")
args = parser.parse_args()
print(args)
quantize_4bit = True if args.quantization == 4 else False
quantize_8bit = True if args.quantization == 8 else False
token = args.token
tokenizer = AutoTokenizer.from_pretrained(args.model, token=token)
model = AutoModelForCausalLM.from_pretrained(args.model, device_map=device, load_in_4bit=quantize_4bit, load_in_8bit=quantize_8bit, token=token)
streamer = TextStreamer(tokenizer)
q_bank = []
bank = []
with open("prompt.json") as f:
data = json.load(f)
q_bank = data['prompts']
bank = data['labels']
timing = []
for i in range(len(bank)):
print("performing: ", bank[i], " test:")
begin = datetime.now()
inputs = tokenizer(q_bank[i], return_tensors='pt').to(device)
outputs = model.generate(**inputs, streamer=streamer, max_new_tokens=100)
generation_time = datetime.now()-begin
print("time: ", generation_time)
print("seconds per token: ", generation_time.total_seconds() / (outputs[0].shape[0] - inputs.input_ids[0].shape[0]))
timing.append((outputs[0].shape[0] - inputs.input_ids[0].shape[0]) / generation_time.total_seconds())
print("tokens per seconds: ", (outputs[0].shape[0] - inputs.input_ids[0].shape[0]) / generation_time.total_seconds())
vram = int(os.popen('nvidia-smi | grep -oP "[0-9]+(?=MiB \/)"').read())
with open('out.txt', 'w') as f:
f.write(f"tokens per second {timing}\n\n")
f.write(f"vram {vram}\n\n")
f.write(f"{os.popen('nvidia-smi').read()}")
# Manual testing at the end
inp=""
while inp!="stop":
inp = input()
begin = datetime.now()
inputs = tokenizer(inp, return_tensors='pt').to(device)
outputs = model.generate(**inputs, streamer=streamer, max_new_tokens=100)
generation_time = datetime.now()-begin
print("time: ", generation_time)
print("seconds per token: ", generation_time / outputs[0].shape[0])