forked from NVIDIA/Model-Optimizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_utils.py
More file actions
executable file
·278 lines (223 loc) · 10.1 KB
/
Copy pathexample_utils.py
File metadata and controls
executable file
·278 lines (223 loc) · 10.1 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
# SPDX-FileCopyrightText: Copyright (c) 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 os
import sys
from typing import Any
import torch
from accelerate import infer_auto_device_map, init_empty_weights
from accelerate.utils import get_max_memory
from transformers import AutoConfig, AutoModelForCausalLM, AutoProcessor, AutoTokenizer
from modelopt.torch.utils.image_processor import MllamaImageProcessor
SPECULATIVE_MODEL_LIST = ["Eagle", "Medusa"]
def is_speculative(hf_config):
"""Check if the model architecture is a speculative model."""
return hf_config.architectures and any(
name in hf_config.architectures[0] for name in SPECULATIVE_MODEL_LIST
)
def get_mode_type_from_engine_dir(engine_dir_str):
# Split the path by '/' and get the last part
last_part = os.path.basename(engine_dir_str)
# Split the last part by '_' and get the first segment
model_type = last_part.split("_")[0]
return model_type
def get_tokenizer(ckpt_path, trust_remote_code=False, **kwargs):
print(f"Initializing tokenizer from {ckpt_path}")
if "vila" in ckpt_path.lower():
ckpt_path += "/llm"
if ckpt_path.endswith(".yaml"):
# Model Optimizer modification
# For Nemo models, tokenizer is instantiated based on its config
from modelopt.deploy.llm.nemo_utils import get_nemo_tokenizer
tokenizer = get_nemo_tokenizer(ckpt_path)
else:
tokenizer = AutoTokenizer.from_pretrained(
ckpt_path, trust_remote_code=trust_remote_code, **kwargs
)
if "qwen" in type(tokenizer).__name__.lower():
# qwen use token id 151643 as pad and eos tokens
tokenizer.pad_token = tokenizer.convert_ids_to_tokens(151643)
tokenizer.eos_token = tokenizer.convert_ids_to_tokens(151643)
# can't set attribute 'pad_token' for "<unk>"
# We skip this step for Nemo models
if tokenizer.pad_token != "<unk>" or tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
assert tokenizer.pad_token is not None, f"Pad token for {ckpt_path} cannot be set!"
return tokenizer
def get_processor(ckpt_path, model_type, device=None, trust_remote_code=False):
"""
Returns a :class:`modelopt.torch.utils.image_processor.MllamaImageProcessor` object.
"""
if model_type == "whisper":
processor = AutoProcessor.from_pretrained(
ckpt_path,
padding_side="left",
trust_remote_code=trust_remote_code,
)
if processor.tokenizer.pad_token is None:
processor.tokenizer.pad_token = processor.tokenizer.eos_token
assert processor.tokenizer.pad_token is not None, (
f"Pad token for {ckpt_path} cannot be set!"
)
return processor
elif model_type == "mllama":
processor = AutoProcessor.from_pretrained(
ckpt_path,
padding_side="left",
trust_remote_code=trust_remote_code,
)
if processor.tokenizer.pad_token is None:
processor.tokenizer.pad_token = processor.tokenizer.eos_token
assert processor.tokenizer.pad_token is not None, (
f"Pad token for {ckpt_path} cannot be set!"
)
return MllamaImageProcessor(processor, device)
def get_dtype(dtype):
if dtype == "bf16":
dtype = torch.bfloat16
elif dtype == "fp16":
dtype = torch.float16
elif dtype == "fp32":
dtype = torch.float32
else:
raise NotImplementedError(f"Unknown dtype {dtype}")
return dtype
def get_model(
ckpt_path,
device="cuda",
gpu_mem_percentage=0.8,
trust_remote_code=False,
use_seq_device_map=False,
):
print(f"Initializing model from {ckpt_path}")
device_map = "auto"
if device == "cpu":
device_map = "cpu"
# Note: Forcibly converting the model precision between bf16 and fp16 may introduce accuracy drop
model_kwargs = {"torch_dtype": "auto"}
if "vila" in ckpt_path.lower():
sys.path.append(os.path.join(ckpt_path, "..", "VILA"))
from llava.model import LlavaLlamaConfig, LlavaLlamaModel # noqa: F401
from transformers import AutoModel
hf_vila = AutoModel.from_pretrained(
ckpt_path,
device_map=device_map,
trust_remote_code=trust_remote_code,
)
model = hf_vila.llm
else:
hf_config = AutoConfig.from_pretrained(ckpt_path, trust_remote_code=trust_remote_code)
if use_seq_device_map:
device_map = "sequential"
# If we use sequential, set max_memory limit to ensure that the model does not occupy the full GPU
max_memory = get_max_memory()
max_memory = {key: value * gpu_mem_percentage for key, value in max_memory.items()}
model_kwargs["max_memory"] = max_memory
if is_speculative(hf_config):
model = AutoModelForCausalLM.from_pretrained(
ckpt_path,
device_map=device_map,
**model_kwargs,
trust_remote_code=trust_remote_code,
)
elif hf_config.model_type == "llava":
from transformers import LlavaForConditionalGeneration
hf_llava = LlavaForConditionalGeneration.from_pretrained(
ckpt_path, device_map=device_map, **model_kwargs
)
model = hf_llava.language_model
elif hf_config.model_type == "t5":
from transformers import AutoModelForSeq2SeqLM
model = AutoModelForSeq2SeqLM.from_pretrained(
ckpt_path, device_map=device_map, **model_kwargs
)
elif hf_config.model_type == "bart":
from transformers import AutoModelForSeq2SeqLM
# device_map "auto" and "cuda" triggers error regarding meta tensor from safetensors
model = AutoModelForSeq2SeqLM.from_pretrained(
ckpt_path, device_map=None, **model_kwargs
).to(device)
elif hf_config.model_type == "whisper":
from transformers import WhisperForConditionalGeneration
model = WhisperForConditionalGeneration.from_pretrained(
ckpt_path, device_map=device_map, **model_kwargs
)
elif hf_config.model_type == "glm":
from transformers import AutoModelForSeq2SeqLM
model = AutoModelForSeq2SeqLM.from_pretrained(
ckpt_path, device_map="cuda", **model_kwargs, trust_remote_code=trust_remote_code
)
elif hf_config.model_type == "mllama":
from transformers import MllamaForConditionalGeneration
model = MllamaForConditionalGeneration.from_pretrained(
ckpt_path,
device_map=device_map,
**model_kwargs,
trust_remote_code=trust_remote_code,
)
elif hf_config.model_type == "llama4":
model = AutoModelForCausalLM.from_pretrained(
ckpt_path,
device_map=device_map,
**model_kwargs,
trust_remote_code=trust_remote_code,
)
else:
with init_empty_weights():
# When computing the device_map, assuming half precision by default,
# unless specified by the hf_config.
torch_dtype = getattr(hf_config, "torch_dtype", torch.float16)
model = AutoModelForCausalLM.from_config(
hf_config, torch_dtype=torch_dtype, trust_remote_code=trust_remote_code
)
max_memory = get_max_memory()
inferred_device_map = infer_auto_device_map(model, max_memory=max_memory)
on_cpu = "cpu" in inferred_device_map.values()
if on_cpu:
for _device in max_memory:
if isinstance(_device, int):
max_memory[_device] *= gpu_mem_percentage
print(
"Model does not fit to the GPU mem. "
f"We apply the following memmory limit for calibration: \n{max_memory}\n"
"If you hit GPU OOM issue, please adjust `gpu_mem_percentage` or "
"reduce the calibration `batch_size` manually."
)
model_kwargs["max_memory"] = max_memory
model = AutoModelForCausalLM.from_pretrained(
ckpt_path,
device_map=device_map,
**model_kwargs,
trust_remote_code=trust_remote_code,
)
model.eval()
if device == "cuda" and not is_model_on_gpu(model):
print("Warning: Some parameters are not on a GPU. Calibration can be slow or hit OOM")
return model
def is_model_on_gpu(model) -> bool:
"""Returns if the model is fully loaded on GPUs."""
return all("cuda" in str(param.device) for param in model.parameters())
def is_enc_dec(model_type) -> bool:
"""Return if the model is a encoder-decoder model."""
return model_type in ["t5", "bart", "whisper"]
def apply_kv_cache_quant(quant_cfg: dict[str, Any], kv_cache_quant_cfg: dict[str, Any]):
"""Apply quantization to the kv cache of the model."""
# Update KV cache related bmm quantizers
# If quant_cfg["quant_cfg"] is None, it corresponds to only kv cache quantization case
quant_cfg["quant_cfg"] = quant_cfg.get("quant_cfg", {"default": {"enable": False}})
quant_cfg["quant_cfg"].update(kv_cache_quant_cfg)
# Set default algorithm for kv cache quantization if not provided.
if not quant_cfg.get("algorithm"):
quant_cfg["algorithm"] = "max"
return quant_cfg