forked from NVIDIA/TensorRT-LLM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
349 lines (320 loc) · 14.1 KB
/
utils.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
# 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
from pathlib import Path
from typing import Optional
from transformers import AutoTokenizer, LlamaTokenizer, T5Tokenizer
from tensorrt_llm.bindings import GptJsonConfig
from tensorrt_llm.builder import get_engine_version
DEFAULT_HF_MODEL_DIRS = {
'BaichuanForCausalLM': 'baichuan-inc/Baichuan-13B-Chat',
'BloomForCausalLM': 'bigscience/bloom-560m',
'ChatGLMForCausalLM': 'THUDM/chatglm3-6b',
'FalconForCausalLM': 'tiiuae/falcon-rw-1b',
'GPTForCausalLM': 'gpt2-medium',
'GPTJForCausalLM': 'EleutherAI/gpt-j-6b',
'GPTNeoXForCausalLM': 'EleutherAI/gpt-neox-20b',
'InternLMForCausalLM': 'internlm/internlm-chat-7b',
'InternLM2ForCausalLM': 'internlm/internlm2-chat-7b',
'LlamaForCausalLM': 'meta-llama/Llama-2-7b-hf',
'MPTForCausalLM': 'mosaicml/mpt-7b',
'PhiForCausalLM': 'microsoft/phi-2',
'OPTForCausalLM': 'facebook/opt-350m',
'QWenForCausalLM': 'Qwen/Qwen-7B',
'RecurrentGemmaForCausalLM': 'google/recurrentgemma-2b',
}
INTERNLM_META_INSTRUCTION = """You are an AI assistant whose name is InternLM (书生·浦语).
- InternLM (书生·浦语) is a conversational language model that is developed by Shanghai AI Laboratory (上海人工智能实验室). It is designed to be helpful, honest, and harmless.
- InternLM (书生·浦语) can understand and communicate fluently in the language chosen by the user such as English and 中文.
"""
DEFAULT_PROMPT_TEMPLATES = {
'InternLMForCausalLM':
"<|User|>:{input_text}<eoh>\n<|Bot|>:",
'InternLM2ForCausalLM':
"<|im_start|>system\n" + INTERNLM_META_INSTRUCTION +
"<|im_end|>\n<|im_start|>user\n{input_text}<|im_end|>\n<|im_start|>assistant\n",
'QWenForCausalLM':
"<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n{input_text}<|im_end|>\n<|im_start|>assistant\n",
}
def supports_inflight_batching(engine_dir):
config_path = Path(engine_dir) / "config.json"
json_config = GptJsonConfig.parse_file(config_path)
model_config = json_config.model_config
return model_config.supports_inflight_batching
def read_decoder_start_token_id(engine_dir):
with open(Path(engine_dir) / "config.json", 'r') as f:
config = json.load(f)
return config['pretrained_config']['decoder_start_token_id']
def read_model_name(engine_dir: str):
engine_version = get_engine_version(engine_dir)
with open(Path(engine_dir) / "config.json", 'r') as f:
config = json.load(f)
if engine_version is None:
return config['builder_config']['name'], None
model_arch = config['pretrained_config']['architecture']
model_version = None
if model_arch == 'ChatGLMForCausalLM':
model_version = config['pretrained_config']['chatglm_version']
if model_arch == 'QWenForCausalLM':
model_version = config['pretrained_config']['qwen_type']
return model_arch, model_version
def throttle_generator(generator, stream_interval):
for i, out in enumerate(generator):
if not i % stream_interval:
yield out
if i % stream_interval:
yield out
def load_tokenizer(tokenizer_dir: Optional[str] = None,
vocab_file: Optional[str] = None,
model_name: str = 'GPTForCausalLM',
model_version: Optional[str] = None,
tokenizer_type: Optional[str] = None):
if vocab_file is None:
use_fast = True
if tokenizer_type is not None and tokenizer_type == "llama":
use_fast = False
# Should set both padding_side and truncation_side to be 'left'
tokenizer = AutoTokenizer.from_pretrained(tokenizer_dir,
legacy=False,
padding_side='left',
truncation_side='left',
trust_remote_code=True,
tokenizer_type=tokenizer_type,
use_fast=use_fast)
elif model_name == 'GemmaForCausalLM' or model_name == 'RecurrentGemmaForCausalLM':
from transformers import GemmaTokenizer
# Initialize tokenizer from vocab file.
tokenizer = GemmaTokenizer(vocab_file=vocab_file,
padding_side='left',
truncation_side='left',
legacy=False)
elif model_name == 'Grok1ModelForCausalLM':
tokenizer = LlamaTokenizer(vocab_file=vocab_file,
padding_side='left',
truncation_side='left',
legacy=False,
use_fast=False)
else:
# For gpt-next, directly load from tokenizer.model
tokenizer = T5Tokenizer(vocab_file=vocab_file,
padding_side='left',
truncation_side='left',
legacy=False)
if model_name == 'QWenForCausalLM' and model_version == 'qwen':
with open(Path(tokenizer_dir) / "generation_config.json") as f:
gen_config = json.load(f)
pad_id = gen_config['pad_token_id']
end_id = gen_config['eos_token_id']
elif model_name == 'ChatGLMForCausalLM' and model_version == 'glm':
pad_id = tokenizer.pad_token_id
end_id = tokenizer.eop_token_id
else:
if tokenizer.pad_token_id is None:
tokenizer.pad_token_id = tokenizer.eos_token_id
pad_id = tokenizer.pad_token_id
end_id = tokenizer.eos_token_id
return tokenizer, pad_id, end_id
def add_common_args(parser):
# sampling arguments
parser.add_argument('--num_beams',
type=int,
help="Use beam search if num_beams > 1",
default=1)
parser.add_argument('--temperature', type=float, default=1.0)
parser.add_argument('--top_k', type=int, default=1)
parser.add_argument('--top_p', type=float, default=0.0)
parser.add_argument('--length_penalty', type=float, default=1.0)
parser.add_argument('--repetition_penalty', type=float, default=1.0)
parser.add_argument('--presence_penalty', type=float, default=0.0)
parser.add_argument('--frequency_penalty', type=float, default=0.0)
parser.add_argument('--beam_search_diversity_rate', type=float, default=0.0)
parser.add_argument('--random_seed', type=int, default=0)
parser.add_argument('--early_stopping',
type=int,
help='Use early stopping if num_beams > 1'
'1 for early-stopping, 0 for non-early-stopping'
'other values for stopping by length',
default=1)
parser.add_argument(
'--end_id',
default=None,
type=int,
help="Override tokenizer end_id to stop on given end_id token.")
parser.add_argument(
'--stop_words',
default=None,
type=str,
nargs="+",
action='append',
help=
'Set stop words for a batch. Successive invocations of --stop_words set stop words for other batches.'
' E.g.: --stop_words " London" " chef" --stop_words "eventually became" "was not"',
)
parser.add_argument(
'--bad_words',
default=None,
type=str,
nargs="+",
action='append',
help=
'Set bad words for a batch. Successive invocations of --bad_words set bad words for other batches.'
' E.g.: --bad_words " London" " chef" --bad_words "eventually became" "was not"',
)
parser.add_argument('--no_repeat_ngram_size', type=int, default=None)
# common runtime arguments
parser.add_argument('--sink_token_length',
type=int,
default=None,
help='The sink token length.')
parser.add_argument(
'--max_attention_window_size',
type=int,
default=None,
help=
'The attention window size that controls the sliding window attention / cyclic kv cache behavior'
)
parser.add_argument('--log_level', type=str, default='info')
parser.add_argument(
'--no_prompt_template',
dest='use_prompt_template',
default=True,
action='store_false',
help=
"Whether or not to use default prompt template to wrap the input text.")
parser.add_argument('--use_py_session',
default=False,
action='store_true',
help="Whether or not to use Python runtime session")
parser.add_argument('--debug_mode',
default=False,
action='store_true',
help="Whether or not to turn on the debug mode")
parser.add_argument('--streaming', default=False, action='store_true')
parser.add_argument('--streaming_interval',
type=int,
help="How often to return tokens when streaming.",
default=5)
parser.add_argument(
'--prompt_table_path',
type=str,
help="Path to .npy file, exported by nemo_prompt_convert.py")
parser.add_argument(
'--prompt_tasks',
help="Comma-separated list of tasks for prompt tuning, e.g., 0,3,1,0")
parser.add_argument('--lora_dir',
type=str,
default=None,
nargs="+",
help="The directory of LoRA weights")
parser.add_argument('--lora_ckpt_source',
type=str,
default="hf",
choices=["hf", "nemo"],
help="The source of lora checkpoint.")
parser.add_argument(
'--lora_task_uids',
type=str,
default=None,
nargs="+",
help="The list of LoRA task uids; use -1 to disable the LoRA module")
parser.add_argument(
'--num_prepend_vtokens',
nargs="+",
type=int,
help="Number of (default) virtual tokens to prepend to each sentence."
" For example, '--num_prepend_vtokens=10' will prepend the tokens"
" [vocab_size, vocab_size + 1, ..., vocab_size + 9] to the sentence.")
parser.add_argument(
'--medusa_choices',
type=str,
default=None,
help="Medusa choice to use, if not none, will use Medusa decoding."
" E.g.: [[0, 0, 0, 0], [0, 1, 0], [1, 0], [1, 1]] for 9 medusa tokens."
)
# model arguments
parser.add_argument('--engine_dir', type=str, default='engine_outputs')
parser.add_argument(
'--tokenizer_type',
help=
'Specify that argument when providing a .model file as the tokenizer_dir. '
'It allows AutoTokenizer to instantiate the correct tokenizer type.')
parser.add_argument('--vocab_file',
help="Used for sentencepiece tokenizers")
parser.add_argument('--no_add_special_tokens',
dest='add_special_tokens',
default=True,
action='store_false',
help="Whether or not to add special tokens")
parser.add_argument('--hf_model_dir', '--model_dir', type=str, default=None)
parser.add_argument(
'--tokenizer_dir',
default=None,
help='tokenizer path; defaults to hf_model_dir if left unspecified')
# memory argument
parser.add_argument(
'--gpu_weights_percent',
default=1,
type=float,
help=
'Specify the percentage of weights that reside on GPU instead of CPU and streaming load during runtime.',
)
parser.add_argument(
'--max_tokens_in_paged_kv_cache',
default=None,
type=int,
help=
'Specify the maximum number of tokens in a kv cache page (only available with cpp session).',
)
parser.add_argument(
'--kv_cache_enable_block_reuse',
action='store_true',
help=
'Enables block reuse in kv cache (only available with cpp session).',
)
parser.add_argument(
'--kv_cache_free_gpu_memory_fraction',
default=0.9,
type=float,
help='Specify the free gpu memory fraction.',
)
parser.add_argument(
'--enable_chunked_context',
action='store_true',
help='Enables chunked context (only available with cpp session).',
)
# hf model argument (if use hf model)
parser.add_argument(
'--hf_data_type',
'--data_type',
type=str,
choices=['fp32', 'fp16', 'bf16', 'float32', 'float16', 'bfloat16'],
default='fp16',
help="The data type for hf model.")
parser.add_argument(
'--hf_device_map_auto',
action='store_true',
help="Use device map 'auto' to load a pretrained HF model. This may "
"help to test a large model that cannot fit into a singlue GPU.")
parser.add_argument(
"--return_all_generated_tokens",
default=False,
action="store_true",
help="This option changes the token output only for streaming. "
"If not specified, return only generated tokens at each step. "
"If specified, return the full beams/outputs at each step. "
"It is automatically enabled for num_beams>1 (only available with cpp session). "
"WARNING: using this option may increase network usage significantly (quadratically w.r.t output length)."
)
return parser