forked from NVIDIA/TensorRT-LLM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_checkpoint.py
190 lines (162 loc) · 6.46 KB
/
convert_checkpoint.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
import argparse
import os
import time
import traceback
from concurrent.futures import ThreadPoolExecutor, as_completed
from typing import Union
from transformers import AutoConfig
import tensorrt_llm
from tensorrt_llm._utils import release_gc
from tensorrt_llm.logger import logger
from tensorrt_llm.mapping import Mapping
from tensorrt_llm.models import (BertForQuestionAnswering,
BertForSequenceClassification, BertModel,
RobertaForQuestionAnswering,
RobertaForSequenceClassification, RobertaModel)
from tensorrt_llm.models.modeling_utils import QuantConfig
from tensorrt_llm.quantization import QuantAlgo
def parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument('--model',
required=True,
choices=[
'BertModel',
'BertForQuestionAnswering',
'BertForSequenceClassification',
'RobertaModel',
'RobertaForQuestionAnswering',
'RobertaForSequenceClassification',
])
parser.add_argument('--model_dir', type=str, default=None)
parser.add_argument('--tp_size',
type=int,
default=1,
help='N-way tensor parallelism size')
parser.add_argument('--pp_size',
type=int,
default=1,
help='N-way pipeline parallelism size')
parser.add_argument('--dtype',
type=str,
default='float16',
choices=['float32', 'float16'])
parser.add_argument('--output_dir',
type=str,
default='tllm_checkpoint',
help='The path to save the TensorRT-LLM checkpoint')
parser.add_argument(
'--workers',
type=int,
default=1,
help='The number of workers for converting checkpoint in parallel')
# Quantization args
parser.add_argument("--use_fp8",
action="store_true",
default=False,
help="Enable FP8 per-tensor quantization")
parser.add_argument(
'--quant_ckpt_path',
type=str,
default=None,
help='Path of a quantized model checkpoint in .safetensors format')
parser.add_argument(
'--calib_dataset',
type=str,
default='ccdv/cnn_dailymail',
help=
"The huggingface dataset name or the local directory of the dataset for calibration."
)
parser.add_argument('--log_level', type=str, default='info')
args = parser.parse_args()
return args
def args_to_quant_config(args: argparse.Namespace) -> QuantConfig:
'''return config dict with quantization info based on the command line args
'''
quant_config = QuantConfig()
if args.use_fp8:
quant_config.quant_algo = QuantAlgo.FP8
return quant_config
def convert_and_save_hf(args):
model_dir = args.model_dir
world_size = args.tp_size * args.pp_size
#TODO: add override_fields if needed
# Need to convert the cli args to the kay-value pairs and override them in the generate config dict.
# Ideally these fields will be moved out of the config and pass them into build API, keep them here for compatibility purpose for now,
# before the refactor is done.
#TODO: add fp8 support later
quant_config = args_to_quant_config(args)
hf_config = AutoConfig.from_pretrained(model_dir, trust_remote_code=True)
assert hf_config is not None, "Failed to load huggingface config, please check!"
def convert_and_save_rank(args, rank, tllm_class: Union[
BertModel,
RobertaModel,
BertForQuestionAnswering,
RobertaForQuestionAnswering,
BertForSequenceClassification,
RobertaForSequenceClassification,
]):
mapping = Mapping(
world_size=world_size,
rank=rank,
tp_size=args.tp_size,
pp_size=args.pp_size,
)
tik = time.time()
tllm_bert = tllm_class.from_hugging_face(
model_dir,
args.dtype,
mapping=mapping,
quant_config=quant_config,
)
print(f'Total time of reading and converting {time.time()-tik} s')
tik = time.time()
tllm_bert.save_checkpoint(args.output_dir, save_config=(rank == 0))
del tllm_bert
print(f'Total time of saving checkpoint {time.time()-tik} s')
tllm_class = globals()[f'{args.model}']
if not args.model == hf_config.architectures[0]:
logger.warning(
"The model doesn't match the architecture in huggingface config.")
execute(args.workers, [convert_and_save_rank] * world_size, args,
tllm_class)
release_gc()
def execute(workers, func, args,
tllm_class: Union[BertModel, RobertaModel, BertForQuestionAnswering,
RobertaForQuestionAnswering,
BertForSequenceClassification,
RobertaForSequenceClassification]):
if workers == 1:
for rank, f in enumerate(func):
f(args, rank, tllm_class)
else:
with ThreadPoolExecutor(max_workers=workers) as p:
futures = [
p.submit(f, args, rank, tllm_class)
for rank, f in enumerate(func)
]
exceptions = []
for future in as_completed(futures):
try:
future.result()
except Exception as e:
traceback.print_exc()
exceptions.append(e)
assert len(
exceptions
) == 0, "Checkpoint conversion failed, please check error log."
def main():
print(tensorrt_llm.__version__)
args = parse_arguments()
logger.set_level(args.log_level)
assert ((args.tp_size <= 2)
and (args.pp_size == 1)), "For now we only support TP = 2!"
tik = time.time()
if not os.path.exists(args.output_dir):
os.makedirs(args.output_dir)
assert args.model_dir is not None
convert_and_save_hf(args)
tok = time.time()
t = time.strftime('%H:%M:%S', time.gmtime(tok - tik))
print(f'Total time of converting checkpoints: {t}')
if __name__ == '__main__':
main()