forked from turboderp/exllamav2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_inference.py
219 lines (142 loc) · 6.23 KB
/
test_inference.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
from exllamav2 import(
ExLlamaV2,
ExLlamaV2Config,
ExLlamaV2Cache,
ExLlamaV2Tokenizer,
model_init,
)
import argparse, os, math, time
import pandas, fastparquet
import torch
import torch.nn.functional as F
from conversion.tokenize import get_tokens
from conversion.quantize import list_live_tensors
import sys
import json
torch.cuda._lazy_init()
torch.set_printoptions(precision = 10)
# torch.backends.cuda.matmul.allow_tf32 = True
# torch.backends.cuda.matmul.allow_fp16_reduced_precision_reduction = True
# torch.set_float32_matmul_precision("medium")
parser = argparse.ArgumentParser(description = "Test inference on ExLlamaV2 model")
parser.add_argument("-ed", "--eval_dataset", type = str, help = "Perplexity evaluation dataset (.parquet file)")
parser.add_argument("-er", "--eval_rows", type = int, default = 128, help = "Number of rows to apply from dataset")
parser.add_argument("-el", "--eval_length", type = int, default = 2048, help = "Max no. tokens per sample")
parser.add_argument("-p", "--prompt", type = str, help = "Generate from prompt")
parser.add_argument("-t", "--tokens", type = int, default = 128, help = "Max no. tokens")
parser.add_argument("-ps", "--prompt_speed", action = "store_true", help = "Test prompt processing (batch) speed over context length")
parser.add_argument("-s", "--speed", action = "store_true", help = "Test raw generation speed over context length")
# Initialize model and tokenizer
model_init.add_args(parser)
args = parser.parse_args()
model_init.check_args(args)
model_init.print_options(args)
model, tokenizer = model_init.init(args)
# Test generation
if args.prompt:
with torch.inference_mode():
cache = ExLlamaV2Cache(model)
ids = tokenizer.encode(args.prompt)
tokens_prompt = ids.shape[-1]
print(f" -- Warmup...")
model.forward(ids[:, -1:])
print(f" -- Generating (greedy sampling)...")
print()
print(args.prompt, end = "")
sys.stdout.flush()
time_begin = time.time()
if ids.shape[-1] > 1: model.forward(ids[:, :-1], cache, preprocess_only = True)
torch.cuda.synchronize()
time_prompt = time.time()
for i in range(args.tokens):
text1 = tokenizer.decode(ids[:, -2:])[0]
logits = model.forward(ids[:, -1:], cache)
sample = torch.argmax(logits[0, -1]).cpu().unsqueeze(0).unsqueeze(0)
ids = torch.cat((ids, sample), dim = -1)
text2 = tokenizer.decode(ids[:, -3:])[0]
text2 = text2[len(text1):]
print (text2, end = "")
# sys.stdout.flush()
time_end = time.time()
print()
print()
total_prompt = time_prompt - time_begin
total_gen = time_end - time_prompt
print(f"Prompt processed in {total_prompt:.2f} seconds, {tokens_prompt} tokens, {tokens_prompt / total_prompt:.2f} tokens/second")
print(f"Response generated in {total_gen:.2f} seconds, {args.tokens} tokens, {args.tokens / total_gen:.2f} tokens/second")
cache = None
# Test perplexity
if args.eval_dataset:
with torch.inference_mode():
eval_dataset = args.eval_dataset
eval_rows = args.eval_rows
eval_length = args.eval_length
print(f" -- Running perplexity test")
print(f" -- Dataset: {eval_dataset}")
print(f" -- Tokenizing eval data, {eval_rows} rows x {eval_length} tokens...")
eval_tokens = get_tokens(eval_rows, eval_length, eval_dataset, tokenizer)
print(f" -- Inference", end = "")
sys.stdout.flush()
logprob_sum = 0.0
logprob_count = 0
for i in range(eval_tokens.shape[0]):
#for i in range(126, 127):
if i % 10 == 0: print(".", end = "")
sys.stdout.flush()
input_ids = eval_tokens[i:i+1, :]
input_ids = input_ids[:, :-1]
logits = model.forward(input_ids)
# print (tokenizer.decode(input_ids))
target_ids = input_ids[:, 1:].to(logits.device)
log_probs = F.log_softmax(logits, dim=-1)
token_log_probs = log_probs.gather(-1, target_ids.unsqueeze(-1)).squeeze(-1)
logprob_sum += token_log_probs.sum().item()
logprob_count += target_ids.numel()
print()
mean_log_prob = logprob_sum / logprob_count
perplexity = math.exp(-mean_log_prob)
print(f" -- Evaluation perplexity: {perplexity:.4f}")
xx = 0
# Test prompt speed
if args.prompt_speed:
with torch.inference_mode():
cache = ExLlamaV2Cache(model)
ids = torch.randint(0, model.config.vocab_size - 1, (1, model.config.max_seq_len))
print(f" -- Warmup...")
model.forward(ids[:, -1:])
print(f" -- Measuring prompt speed...")
current_len = 128
while True:
time_begin = time.time()
cache.current_seq_len = 0
model.forward(ids[:, :current_len], cache, preprocess_only = True)
torch.cuda.synchronize()
time_end = time.time()
tps = current_len / (time_end - time_begin)
print(f" ** Length {current_len:>5} tokens: {tps:>11.4f} t/s")
current_len_ = current_len
current_len = min(current_len + 128, model.config.max_seq_len)
if current_len == current_len_: break
cache = None
# Test token speed
if args.speed:
with torch.inference_mode():
cache = ExLlamaV2Cache(model)
print(f" -- Measuring token speed...")
ids = tokenizer.encode("X")
model.forward(ids[:, :])
current_idx = ids.shape[-1]
next_stop = 128
while True:
time_begin = time.time()
tokens = next_stop - current_idx
for i in range(tokens):
logits = model.forward(ids[:, -1:], cache)
sample = torch.argmax(logits[0, -1]).cpu().unsqueeze(0).unsqueeze(0)
ids = torch.cat((ids, sample), dim=-1)
time_end = time.time()
tps = tokens / (time_end - time_begin)
print(f" ** Position {current_idx:>5} + {tokens:>3} tokens: {tps:>9.4f} t/s")
current_idx = next_stop
next_stop = min(next_stop + 128, model.config.max_seq_len)
if next_stop == current_idx: break