forked from NVIDIA/TensorRT-LLM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enc_dec_benchmark.py
449 lines (416 loc) · 19.8 KB
/
enc_dec_benchmark.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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# SPDX-FileCopyrightText: Copyright (c) 2022-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import os
# isort: off
import torch
#isort: on
from allowed_configs import get_build_config
from base_benchmark import BaseBenchmark, get_engine_name
from build import build_enc_dec
import tensorrt_llm
from tensorrt_llm._utils import (trt_dtype_to_torch, str_dtype_to_trt)
from tensorrt_llm.quantization import QuantMode
from tensorrt_llm.runtime.session import TensorInfo
class EncDecBenchmark(BaseBenchmark):
def __init__(self, args, batch_sizes, in_out_lens, rank, world_size):
self.engine_dir = args.engine_dir
self.model_name = args.model
self.mode = args.mode
self.enable_fp8 = False # hardcode for enc-dec models
self.dtype = args.dtype
self.output_dir = args.output_dir
self.runtime_rank = rank
self.world_size = world_size
self.csv_filename = "" # lazy init
self.batch_sizes = batch_sizes
self.in_out_lens = in_out_lens
self.num_beams = args.num_beams
self.build_time = 0
self.quant_mode = QuantMode(0)
# In current implementation, encoder and decoder have the same name,
# builder config, and plugin config. But they can be different in the future.
# So we use separate variables for encoder and decoder here.
self.encoder_engine_model_name = args.model
self.decoder_engine_model_name = args.model
# only for whisper parameter
self.n_mels = 0
if self.engine_dir is not None:
def read_config(component):
config_path = os.path.join(self.engine_dir, component,
"config.json")
with open(config_path, "r") as f:
config = json.load(f)
# Sanity checks
config_dtype = config["builder_config"]["precision"]
assert (
self.dtype == config_dtype
), f"Engine dtype ({config_dtype}) != Runtime dtype ({self.dtype})"
world_size = config["builder_config"]["tensor_parallel"]
assert (
world_size == self.world_size
), f"Engine world size ({world_size}) != Runtime world size ({self.world_size})"
tp_size = config["builder_config"]["tensor_parallel"]
# TP only for benchmarking
assert (
tp_size == self.world_size
), f"Engine tensor parallel size ({tp_size}) should be equal to world size ({self.world_size})"
assert (
config["plugin_config"]["remove_input_padding"] == False
), "remove_input_padding should be False for enc-dec benchmarks"
num_heads = config["builder_config"]["num_heads"]
assert (num_heads % tp_size) == 0
# Get model config
num_heads = num_heads // tp_size
hidden_size = config["builder_config"]["hidden_size"] // tp_size
num_kv_heads = config["builder_config"].get(
"num_kv_heads", config["builder_config"]["num_heads"])
num_kv_heads = (num_kv_heads + tp_size - 1) // tp_size
model_config = tensorrt_llm.runtime.ModelConfig(
num_heads=num_heads,
num_kv_heads=num_kv_heads,
hidden_size=hidden_size,
head_size=config["builder_config"]["head_size"],
max_batch_size=config["builder_config"]["max_batch_size"],
max_beam_width=config["builder_config"]["max_beam_width"],
vocab_size=config["builder_config"]["vocab_size"],
num_layers=config["builder_config"]["num_layers"],
gpt_attention_plugin=config["plugin_config"]
["gpt_attention_plugin"],
remove_input_padding=config["plugin_config"]
["remove_input_padding"],
cross_attention=config["builder_config"]["cross_attention"],
has_position_embedding=config["builder_config"]
["has_position_embedding"],
has_token_type_embedding=config["builder_config"]
["has_token_type_embedding"],
use_custom_all_reduce=config["plugin_config"].get(
"use_custom_all_reduce", False),
dtype=config_dtype,
)
self.max_batch_size = config["builder_config"]["max_batch_size"]
self.max_input_len = config["builder_config"][
"max_encoder_input_len"]
self.max_output_len = config["builder_config"]["max_output_len"]
self.n_mels = config["builder_config"][
'n_mels'] if 'whisper' in self.model_name else 0
for key, value in config["builder_config"].items():
if key == "name":
engine_model_name = value
break
return engine_model_name, model_config
(
self.encoder_engine_model_name,
self.encoder_model_config,
) = read_config("encoder")
(
self.decoder_engine_model_name,
self.decoder_model_config,
) = read_config("decoder")
self.encoder_engine_name = get_engine_name(
self.encoder_engine_model_name,
self.dtype,
self.world_size,
self.runtime_rank,
)
self.decoder_engine_name = get_engine_name(
self.decoder_engine_model_name,
self.dtype,
self.world_size,
self.runtime_rank,
)
self.encoder_runtime_mapping = tensorrt_llm.Mapping(
world_size=self.world_size,
rank=self.runtime_rank,
tp_size=self.world_size,
)
self.decoder_runtime_mapping = tensorrt_llm.Mapping(
world_size=self.world_size,
rank=self.runtime_rank,
tp_size=self.world_size,
)
if not args.serial_build:
torch.cuda.set_device(self.runtime_rank %
self.encoder_runtime_mapping.gpus_per_node)
self.device = torch.cuda.current_device()
if self.engine_dir is not None:
# Deserialize engine from engine directory
self.encoder_serialize_path = os.path.join(self.engine_dir,
"encoder",
self.encoder_engine_name)
with open(self.encoder_serialize_path, "rb") as f:
encoder_engine_buffer = f.read()
self.decoder_serialize_path = os.path.join(self.engine_dir,
"decoder",
self.decoder_engine_name)
with open(self.decoder_serialize_path, "rb") as f:
decoder_engine_buffer = f.read()
else:
build_config = get_build_config(self.model_name)
self.max_batch_size = build_config['max_batch_size'] \
if args.max_batch_size is None else args.max_batch_size
self.max_input_len = build_config['max_encoder_input_len'] \
if args.max_input_len is None else args.max_input_len
self.max_output_len = build_config['max_output_len'] \
if args.max_output_len is None else args.max_output_len
self.n_mels = build_config[
'n_mels'] if 'whisper' in self.model_name else 0
# Build engine
(
encoder_engine_buffer,
decoder_engine_buffer,
self.encoder_model_config,
self.decoder_model_config,
encoder_build_time,
decoder_build_time,
) = build_enc_dec(args)
self.build_time = encoder_build_time + decoder_build_time
assert encoder_engine_buffer is not None
assert decoder_engine_buffer is not None
# session setup
self.encoder_session = tensorrt_llm.runtime.Session.from_serialized_engine(
encoder_engine_buffer)
self.decoder_session = tensorrt_llm.runtime.GenerationSession(
self.decoder_model_config,
decoder_engine_buffer,
self.decoder_runtime_mapping,
)
def get_config(self):
if 'whisper' in self.model_name:
print(
f"[WARNING] whisper benchmark is input_len=1500, no text prompt, output_len=arbitrary"
)
for inlen, outlen in self.in_out_lens:
if (inlen > self.max_input_len or outlen > self.max_output_len):
print(
f"[WARNING] check inlen({inlen}) <= max_inlen({self.max_input_len}) and "
f"outlen({outlen}) <= max_outlen({self.max_output_len}) failed, skipping."
)
continue
for batch_size in self.batch_sizes:
if batch_size > self.max_batch_size:
print(
f"[WARNING] check batch_size({batch_size}) "
f"<= max_batch_size({self.max_batch_size}) failed, skipping."
)
continue
yield (batch_size, inlen, outlen)
def prepare_inputs(self, config):
batch_size, encoder_input_len = config[0], config[1]
attention_mask = None
whisper_decoder_encoder_input_lengths = None
outputs = {}
if 'whisper' in self.model_name:
# feature_len always fixed 3000 now
feature_len = 3000
encoder_input_ids = (torch.randint(
1, 100, (batch_size, self.n_mels, feature_len)).int().cuda())
encoder_input_lengths = torch.tensor([
encoder_input_ids.shape[2] // 2
for _ in range(encoder_input_ids.shape[0])
],
dtype=torch.int32,
device=self.device)
decoder_input_ids = (torch.randint(1, 100, (1, )).int().cuda())
decoder_input_ids = decoder_input_ids.repeat(
(encoder_input_ids.shape[0], 1))
output_list = [
TensorInfo('x', str_dtype_to_trt(self.dtype),
encoder_input_ids.shape),
TensorInfo('input_lengths', str_dtype_to_trt('int32'),
encoder_input_lengths.shape)
]
output_info = (self.encoder_session).infer_shapes(output_list)
outputs = {
t.name: torch.empty(tuple(t.shape),
dtype=trt_dtype_to_torch(t.dtype),
device='cuda')
for t in output_info
}
whisper_decoder_encoder_input_lengths = torch.tensor(
[
outputs['output'].shape[1]
for x in range(outputs['output'].shape[0])
],
dtype=torch.int32,
device='cuda')
decoder_input_lengths = torch.tensor([
decoder_input_ids.shape[-1]
for _ in range(decoder_input_ids.shape[0])
],
dtype=torch.int32,
device='cuda')
cross_attention_mask = torch.ones(
[outputs['output'].shape[0], 1,
outputs['output'].shape[1]]).int().cuda()
else:
encoder_input_ids = (torch.randint(
100, (batch_size, encoder_input_len)).int().cuda())
# For now, just hardcode the decoder_start_token_id to 0 for t5 models.
decoder_start_token_id = 0
decoder_input_ids = torch.IntTensor([[decoder_start_token_id]
]).to(self.device)
decoder_input_ids = decoder_input_ids.repeat(
(encoder_input_ids.shape[0], 1))
# in padding mode --> keep input, just calculate actual length and max length
# Note: 1st token should always count, even if it is pad_token_id (0). e.g., decoder start id in enc-dec models could be a single pad_token_id, we should count
encoder_input_lengths = ((
1 + (encoder_input_ids[:, 1:] != 0).sum(dim=1).type(
torch.IntTensor).to(self.device)).clone().detach().to(
dtype=torch.int32, device=self.device))
decoder_input_lengths = ((
1 + (decoder_input_ids[:, 1:] != 0).sum(dim=1).type(
torch.IntTensor).to(self.device)).clone().detach().to(
dtype=torch.int32, device=self.device))
# attention mask, always set 1 as if all are valid tokens
attention_mask = torch.ones(
(batch_size, encoder_input_len)).int().cuda()
# cross attention mask, always set 1 as if all are valid tokens
# [batch_size, query_len, encoder_input_len] currently, use query_len=1
cross_attention_mask = torch.ones(
(batch_size, 1, encoder_input_len)).int().cuda()
hidden_size = (self.encoder_model_config.hidden_size *
self.world_size) # tp_size
hidden_states_shape = (
encoder_input_ids.shape[0],
encoder_input_ids.shape[1],
hidden_size,
)
hidden_states_dtype = lambda name: trt_dtype_to_torch(
self.encoder_session.engine.get_tensor_dtype(name))
outputs["encoder_output"] = torch.empty(
hidden_states_shape,
dtype=hidden_states_dtype("encoder_output"),
device=self.device,
).contiguous()
stream = torch.cuda.current_stream().cuda_stream
return (
encoder_input_ids,
encoder_input_lengths,
attention_mask,
decoder_input_ids,
decoder_input_lengths,
cross_attention_mask,
whisper_decoder_encoder_input_lengths,
outputs,
stream,
)
def run(self, inputs, config, benchmark_profiler=None):
output_len = config[2]
(
encoder_input_ids,
encoder_input_lengths,
attention_mask,
decoder_input_ids,
decoder_input_lengths,
cross_attention_mask,
whisper_decoder_encoder_input_lengths,
outputs,
stream,
) = inputs
hidden_states_dtype = lambda name: trt_dtype_to_torch(
self.encoder_session.engine.get_tensor_dtype(name))
# input tensors
inputs = {}
if 'whisper' in self.model_name:
inputs['x'] = encoder_input_ids.contiguous()
inputs["input_lengths"] = encoder_input_lengths
else:
inputs["input_ids"] = encoder_input_ids.contiguous()
inputs["input_lengths"] = encoder_input_lengths
inputs["max_input_length"] = torch.empty(
(self.max_input_len, ),
dtype=hidden_states_dtype("max_input_length"),
device=self.device,
).contiguous()
if not self.encoder_model_config.gpt_attention_plugin:
inputs["attention_mask"] = attention_mask.contiguous()
if self.encoder_model_config.has_position_embedding:
bsz, seq_len = encoder_input_ids.shape[:2]
position_ids = torch.arange(
seq_len, dtype=torch.int32,
device=encoder_input_ids.device).expand(bsz, -1)
inputs['position_ids'] = position_ids.contiguous()
# run encoder
self.encoder_session.set_shapes(inputs)
ok = self.encoder_session.run(inputs, outputs, stream)
assert ok, "Runtime execution failed"
torch.cuda.synchronize()
# run decoder
sampling_config = tensorrt_llm.runtime.SamplingConfig(
end_id=1, pad_id=0, num_beams=self.num_beams, min_length=output_len)
encoder_output = outputs[
'output'] if 'whisper' in self.model_name else outputs[
"encoder_output"]
encoder_max_input_length = encoder_output.shape[
1] if 'whisper' in self.model_name else torch.max(
encoder_input_lengths).item()
self.decoder_session.setup(
decoder_input_lengths.size(0),
torch.max(decoder_input_lengths).item(),
output_len,
beam_width=self.num_beams,
max_attention_window_size=None,
encoder_max_input_length=encoder_max_input_length,
)
cross_attention_mask = None if self.decoder_model_config.gpt_attention_plugin else cross_attention_mask
self.decoder_session.decode(
decoder_input_ids,
decoder_input_lengths,
sampling_config,
encoder_output=encoder_output,
encoder_input_lengths=whisper_decoder_encoder_input_lengths
if 'whisper' in self.model_name else encoder_input_lengths,
cross_attention_mask=cross_attention_mask,
)
def report(self,
config,
latency,
percentile95,
percentile99,
peak_gpu_used,
csv,
benchmark_profiler=None):
# Note: Theoretically, the encoder and decoder can have different configs.
# But for current implementation, we assume they are the same. In the future,
# we can have a special structure of report_dict for enc-dec models.
report_dict = super().get_report_dict()
batch_size, encoder_input_len, output_len = config[0], config[
1], config[2]
tokens_per_sec = round(batch_size * output_len / (latency / 1000), 2)
report_dict["num_heads"] = self.encoder_model_config.num_heads
report_dict["num_kv_heads"] = self.encoder_model_config.num_kv_heads
report_dict["num_layers"] = self.encoder_model_config.num_layers
report_dict["hidden_size"] = self.encoder_model_config.hidden_size
report_dict["vocab_size"] = self.encoder_model_config.vocab_size
report_dict["batch_size"] = batch_size
report_dict["input_length"] = encoder_input_len
report_dict["output_length"] = output_len
report_dict["latency(ms)"] = latency
report_dict["build_time(s)"] = self.build_time
report_dict["tokens_per_sec"] = tokens_per_sec
report_dict["percentile95(ms)"] = percentile95
report_dict["percentile99(ms)"] = percentile99
report_dict["gpu_peak_mem(gb)"] = peak_gpu_used
if self.runtime_rank == 0:
if csv:
line = ",".join([str(v) for v in report_dict.values()])
print(line)
with open(self.get_csv_filename(), "a") as file:
file.write(line + "\n")
else:
kv_pairs = [f"{k} {v}" for k, v in report_dict.items()]
line = "[BENCHMARK] " + " ".join(kv_pairs)
print(line)