-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathbenchlatency.py
More file actions
303 lines (209 loc) · 9.11 KB
/
benchlatency.py
File metadata and controls
303 lines (209 loc) · 9.11 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import os
import sys
os.environ["WORLD_SIZE"] = "1"
import time
import torch
import argparse
import numpy as np
import pandas as pd
from transformers import AutoTokenizer
from torch.cuda import OutOfMemoryError
torch.manual_seed(0)
from mixquant.Cache import MixLibCache
def warmup(model):
warm_up = torch.randn((4096,4096)).to(next(model.parameters()).device)
torch.mm(warm_up,warm_up)
def prepare_data(_dataset_path = 'wikitext', _split='test', _text_column='text'):
from datasets import load_dataset
"""
Prepares the dataset by loading and formatting.
Returns
-------
str
The formatted dataset as a single string.
"""
if _dataset_path == 'wikitext':
_dataset_name = 'wikitext-2-raw-v1'
data = load_dataset(_dataset_path, _dataset_name, split=_split)
elif _dataset_path == 'c4':
_dataset_name = 'realnewslike'
data = load_dataset(_dataset_path, _dataset_name, split=_split)
else:
_dataset_name = 'wikitext-2-raw-v1'
data = load_dataset(os.path.join(_dataset_path,'wikitext'),
_dataset_name, split=_split, cache_dir="/home/chenyidong/tmp")
# Format the text column of the dataset
text_list = [' \n' if s == '' else s for s in data[_text_column]]
return ''.join(text_list)
def decode_token(model, _tokenizer, _text, n_batch, repeat = 10):
tokens = _tokenizer(_text, truncation=False, return_tensors='pt').input_ids.to('cuda')
start = 0
end = n_batch
for j in range(repeat):
batch_start = start + j * n_batch
batch_size = min(end - batch_start, n_batch)
token_org = tokens[0][batch_start].item()
if j == 0:
# Replace the first token with the BOS token
tokens[0][batch_start] = _tokenizer.bos_token_id
# Compute the logits for the current batch of tokens
_compute_batch_logits(tokens, batch_start, batch_size)
tokens[0][batch_start] = token_org
def _compute_batch_logits(_model,tokens, batch_start, batch_size):
# Compute the logits without keeping track of gradients
outputs = _model(tokens[:, batch_start:batch_start+batch_size])
return outputs
def generate(model, tokens, n_generate, batch_size, cache):
context_time = 0
generate_time = []
with torch.inference_mode():
# prefill context
cache.is_prefill = False
for i in range(10):
batch_start = i * batch_size
inputs = torch.as_tensor(tokens[:, batch_start:batch_start+batch_size], device=next(model.parameters()).device)
inputs = inputs.reshape((batch_size,1,))
out = model(inputs,use_cache=True)
with torch.inference_mode():
# cache.is_prefill = True
# inputs = torch.as_tensor(input_ids, device=next(model.parameters()).device)
# out = model(inputs,use_cache=True)
# token = out[0][:, -1].max(1)[1].unsqueeze(1)
for i in range(n_generate):
batch_start = i * batch_size
torch.cuda.synchronize()
# decode tokens
cache.is_prefill = False
inputs = torch.as_tensor(tokens[:, batch_start:batch_start+batch_size], device=next(model.parameters()).device)
inputs = inputs.reshape((batch_size,1,))
start = time.time()
out = model(inputs,use_cache=True)
torch.cuda.synchronize()
generate_time.append(time.time() - start)
print("--- generate time ---")
#print(generate_time)
return generate_time
def run_round(model_path, quant_file, n_generate, token, batch_size, safetensors, model_type='fp16',mixlibcache=None):
if model_type == 'mix':
from mixquant import AutoForCausalLM
model = AutoForCausalLM.from_quantized(
model_path, quant_file, fuse_layers=True,
max_new_tokens=n_generate, batch_size=batch_size,
safetensors=safetensors,
mix = True,
cache = mixlibcache
)
if model_type == 'awq':
import awq
from awq import AutoAWQForCausalLM
print(f" -- Loading model awq...")
model = AutoAWQForCausalLM.from_quantized(
model_path, quant_file, fuse_layers=True,
max_new_tokens=n_generate, batch_size=batch_size,
safetensors=safetensors
)
if model_type == 'fp16':
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
model = AutoModelForCausalLM.from_pretrained(
model_path, torch_dtype=torch.float16,
device_map='auto', trust_remote_code=True
)
if model_type == 'bitsandbytes':
from transformers import AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained(
model_path,
torch_dtype=torch.float16,
load_in_8bit=True,
trust_remote_code=True,
max_memory=f'{int(torch.cuda.mem_get_info()[0]/1024**3)-2}GB')
if model_type == 'quik':
from mixquant import AutoForCausalLM
model = AutoForCausalLM.from_quantized(
model_path, quant_file, fuse_layers=True,
max_new_tokens=n_generate, batch_size=batch_size,
safetensors=safetensors,
mix = True,
cache = mixlibcache
)
print(model)
print(f" -- Warming up...")
warmup(model)
print(f" -- Generating {n_generate} tokens, in context...")
try:
generate_time = generate(model, token, n_generate, batch_size, mixlibcache)
successful_generate = True
except RuntimeError as ex:
if 'cuda out of memory' in str(ex).lower():
successful_generate = False
else:
raise RuntimeError(ex)
device = next(model.parameters()).device
memory_used = torch.cuda.max_memory_allocated(device) / (1024 ** 3)
memory_pct = memory_used / (torch.cuda.get_device_properties(device).total_memory / (1024 ** 3)) * 100
if successful_generate:
# number of tokens in context / time for processing context * batch size
# 1 second / median time per token in seconds * batch size
decode_tokens_per_second = 1 / np.median(generate_time) * batch_size
print(f" ** Speed (Decode): {decode_tokens_per_second:.2f} tokens/second")
print(f" ** Max Memory (VRAM): {memory_used:.2f} GB ({memory_pct:.2f}%)")
else:
decode_tokens_per_second = 'OOM'
return {
"Batch Size": batch_size,
"Decode Length": n_generate,
"Decode tokens/s": decode_tokens_per_second,
"Memory (VRAM)": f"{memory_used:.2f} GB ({memory_pct:.2f}%)",
"latency" : float(np.median(generate_time))
}, args.model_type
def main(args):
rounds = [
{"context": args.seq_length, "n_generate": args.seq_length},
]
all_stats = []
cache = MixLibCache(bit=args.bit)
print("downloading data......")
text = prepare_data(args.dataset_path)
print("done......")
tokenizer = AutoTokenizer.from_pretrained(args.model_path, use_fast=args.use_fast_tokenizer, trust_remote_code=True)
if not tokenizer.pad_token_id:
tokenizer.pad_token_id = tokenizer.eos_token_id
tokenizer.model_max_length = sys.maxsize
tokens = tokenizer(text, truncation=False, return_tensors='pt').input_ids.to('cuda')
for settings in rounds:
stats, model_version = run_round(
args.model_path,
args.quant_file,
settings["n_generate"],
tokens,
args.batch_size,
args.safetensors,
args.model_type,
cache
)
all_stats.append(stats)
if stats["Decode tokens/s"] == 'OOM':
break
df = pd.DataFrame(all_stats)
print('GPU:', torch.cuda.get_device_name())
print('Model:', args.model_path)
print('Version:', model_version)
print(df.to_markdown(index=False))
try:
os.mkdir('output/throughput/'+args.model_type)
except:
pass
df.to_csv('output/throughput/'+args.model_type + '/' + args.quant_file.split("/")[-1] \
+ str(args.batch_size) + '_' + str(args.bit) + ".csv")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--model_path", type=str, default="", help="path to the model")
parser.add_argument("--quant_file", type=str, default="", help="weights filename")
parser.add_argument("--batch_size", type=int, default=1, help="Batch size for cache and generation")
parser.add_argument("--model_type", type=str, default="fp16")
parser.add_argument("--safetensors", default=False, action="store_true", help="Use for enabling safetensors")
parser.add_argument("--use_fast_tokenizer", action="store_true", help="Wheter to use fast tokenizer")
parser.add_argument("--seq_length", type=int, default=128)
parser.add_argument("--dataset_path", type=str, default='wikitext', help="Path to the dataset.")
parser.add_argument("--bit", type=int, default=8)
args = parser.parse_args()
main(args)