forked from NVIDIA/Model-Optimizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinetune.py
More file actions
413 lines (355 loc) · 15.1 KB
/
finetune.py
File metadata and controls
413 lines (355 loc) · 15.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
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
# Adapted from https://github.com/tatsu-lab/stanford_alpaca/blob/3783d18/train.py
# Copyright 2023 Rohan Taori, Ishaan Gulrajani, Tianyi Zhang, Yann Dubois, Xuechen Li
#
# 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.
# 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 argparse
import copy
import os
import pickle
from collections.abc import Sequence
from dataclasses import dataclass, field
import torch
import transformers
import utils
from torch.utils.data import Dataset
from tqdm import tqdm
from transformers import Trainer
from transformers.trainer_utils import get_last_checkpoint
import modelopt.torch.opt as mto
import modelopt.torch.utils.distributed as dist
from modelopt.torch.opt.utils import is_dynamic
from modelopt.torch.utils import print_rank_0
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
IGNORE_INDEX = -100
DEFAULT_PAD_TOKEN = "[PAD]"
DEFAULT_EOS_TOKEN = "</s>"
DEFAULT_BOS_TOKEN = "<s>"
DEFAULT_UNK_TOKEN = "<unk>"
PROMPT_DICT = {
"prompt_input": (
"Below is an instruction that describes a task, paired with an input that provides further"
" context. Write a response that appropriately completes the request.\n\n###"
" Instruction:\n{instruction}\n\n### Input:\n{input}\n\n### Response:"
),
"prompt_no_input": (
"Below is an instruction that describes a task. "
"Write a response that appropriately completes the request.\n\n"
"### Instruction:\n{instruction}\n\n### Response:"
),
}
@dataclass
class ModelArguments:
model_name_or_path: str | None = field(default="facebook/opt-125m")
use_flash_attn: bool | None = field(
default=False,
metadata={"help": "Enables Flash attention for training."},
)
trust_remote_code: bool = field(
default=False,
metadata={"help": "Set trust_remote_code for Huggingface models and tokenizers."},
)
@dataclass
class DataArguments:
train_datapath: str = field(default=None, metadata={"help": "Path to the training data."})
val_datapath: str = field(default=None, metadata={"help": "Path to the eval data."})
@dataclass
class TrainingArguments(transformers.TrainingArguments):
cache_dir: str | None = field(default=None)
optim: str = field(default="adamw_torch")
model_max_length: int = field(
default=2048,
metadata={
"help": (
"Maximum sequence length. Sequences will be right padded (and possibly truncated)."
)
},
)
dataloader_drop_last: bool = field(default=True)
bf16: bool = field(default=True)
@dataclass
class ModelOptArguments:
# modelopt quantization arguments
quant_cfg: str | None = field(
default=None,
metadata={
"help": (
"Specify the quantization format for PTQ/QAT. if specified, PTQ/QAT will be enabled"
" with the specified quantization format. choices=['INT8_DEFAULT_CFG',"
" 'INT8_SMOOTHQUANT_CFG', 'FP8_DEFAULT_CFG', 'INT4_AWQ_CFG', 'W4A8_AWQ_BETA_CFG']"
)
},
)
calib_size: int = field(
default=512,
metadata={
"help": (
"Specify the calibration size for quantization. The calibration dataset is used to"
" setup the quantization scale parameters for PTQ/QAT."
)
},
)
# modelopt sparsity arguments
sparse_fmt: str | None = field(
default=None,
metadata={
"help": (
"Specify the sparsity format. if specified, sparsity will be enabled with the"
" specified sparsity format. choices=['dense', 'sparsegpt', 'sparse_magnitude']"
)
},
)
modelopt_restore_path: str | None = field(
default=None,
metadata={"help": "Path to the modelopt state dict to restore from."},
)
def smart_tokenizer_and_embedding_resize(
special_tokens_dict: dict,
tokenizer: transformers.PreTrainedTokenizer,
model: transformers.PreTrainedModel,
):
"""Resize tokenizer and embedding.
Note: This is the unoptimized version that may make your embedding size not be divisible by 64.
"""
num_new_tokens = tokenizer.add_special_tokens(special_tokens_dict)
model.resize_token_embeddings(len(tokenizer))
if num_new_tokens > 0:
input_embeddings = model.get_input_embeddings().weight.data
output_embeddings = model.get_output_embeddings().weight.data
input_embeddings_avg = input_embeddings[:-num_new_tokens].mean(dim=0, keepdim=True)
output_embeddings_avg = output_embeddings[:-num_new_tokens].mean(dim=0, keepdim=True)
input_embeddings[-num_new_tokens:] = input_embeddings_avg
output_embeddings[-num_new_tokens:] = output_embeddings_avg
def _tokenize_fn(strings: Sequence[str], tokenizer: transformers.PreTrainedTokenizer) -> dict:
"""Tokenize a list of strings."""
tokenized_list = [
tokenizer(
text,
return_tensors="pt",
padding="max_length",
max_length=tokenizer.model_max_length,
truncation=True,
)
for text in tqdm(strings, desc="Tokenizing")
]
input_ids = labels = [tokenized.input_ids[0] for tokenized in tokenized_list]
input_ids_lens = labels_lens = [
tokenized.input_ids.ne(tokenizer.pad_token_id).sum().item() for tokenized in tokenized_list
]
for i in range(len(input_ids_lens)):
if input_ids_lens[i] > 2048:
print_rank_0("Input exceeds model length 2048")
print_rank_0(input_ids_lens[i])
print_rank_0(strings[i])
return {
"input_ids": input_ids,
"labels": labels,
"input_ids_lens": input_ids_lens,
"labels_lens": labels_lens,
}
def preprocess(
sources: Sequence[str],
targets: Sequence[str],
tokenizer: transformers.PreTrainedTokenizer,
) -> dict:
"""Preprocess the data by tokenizing."""
examples = [s + t for s, t in zip(sources, targets)]
examples_tokenized, sources_tokenized = [
_tokenize_fn(strings, tokenizer) for strings in (examples, sources)
]
input_ids = examples_tokenized["input_ids"]
labels = copy.deepcopy(input_ids)
for label, source_len in zip(labels, sources_tokenized["input_ids_lens"]):
label[:source_len] = IGNORE_INDEX
return {"input_ids": input_ids, "labels": labels}
def get_model_state_dict(trainer: transformers.Trainer):
"""Collects the state dict."""
state_dict = trainer.model.state_dict()
cpu_state_dict = {key: value.cpu() for key, value in state_dict.items()}
del state_dict
return cpu_state_dict
class SupervisedDataset(Dataset):
"""Dataset for supervised fine-tuning."""
def __init__(
self,
training_args: TrainingArguments,
data_path: str,
tokenizer: transformers.PreTrainedTokenizer,
split: str,
):
super().__init__()
pickle_name = f"dict_{split}_{tokenizer.model_max_length}.pickle"
with training_args.main_process_first():
if os.path.isfile(pickle_name):
with open(pickle_name, "rb") as f:
print_rank_0("Reuse pickled data")
data_dict = pickle.load(f)
else:
print_rank_0("Loading data...")
list_data_dict = utils.jload(data_path)
print_rank_0("Formatting inputs...")
prompt_input = PROMPT_DICT["prompt_input"]
sources = [prompt_input.format_map(example) for example in list_data_dict]
targets = [
f"{example['output']}{tokenizer.eos_token}" for example in list_data_dict
]
print_rank_0("Tokenizing inputs... This may take some time...")
data_dict = preprocess(sources, targets, tokenizer)
with open(pickle_name, "wb") as f:
pickle.dump(data_dict, f, pickle.HIGHEST_PROTOCOL)
self.input_ids = data_dict["input_ids"]
self.labels = data_dict["labels"]
def __len__(self):
return len(self.input_ids)
def __getitem__(self, i) -> dict[str, torch.Tensor]:
return {"input_ids": self.input_ids[i], "labels": self.labels[i]}
@dataclass
class DataCollatorForSupervisedDataset:
"""Collate examples for supervised fine-tuning."""
tokenizer: transformers.PreTrainedTokenizer
def __call__(self, instances: Sequence[dict]) -> dict[str, torch.Tensor]:
input_ids, labels = tuple(
[instance[key] for instance in instances] for key in ("input_ids", "labels")
)
input_ids = torch.nn.utils.rnn.pad_sequence(
input_ids, batch_first=True, padding_value=self.tokenizer.pad_token_id
)
labels = torch.nn.utils.rnn.pad_sequence(
labels, batch_first=True, padding_value=IGNORE_INDEX
)
return {
"input_ids": input_ids,
"labels": labels,
"attention_mask": input_ids.ne(self.tokenizer.pad_token_id),
}
def make_supervised_data_module(
training_args: TrainingArguments,
train_datapath: str,
val_datapath: str,
tokenizer: transformers.PreTrainedTokenizer,
) -> dict:
"""Make dataset and collator for supervised fine-tuning."""
train_dataset = SupervisedDataset(training_args, train_datapath, tokenizer, "train")
val_dataset = SupervisedDataset(training_args, val_datapath, tokenizer, "val")
data_collator = DataCollatorForSupervisedDataset(tokenizer=tokenizer)
return {
"train_dataset": train_dataset,
"eval_dataset": val_dataset,
"data_collator": data_collator,
}
def train():
parser = transformers.HfArgumentParser(
(ModelArguments, DataArguments, TrainingArguments, ModelOptArguments)
)
model_args, data_args, training_args, modelopt_args = parser.parse_args_into_dataclasses()
args = argparse.Namespace(
**vars(model_args), **vars(data_args), **vars(training_args), **vars(modelopt_args)
)
last_checkpoint = None
if os.path.isdir(args.output_dir) and args.do_train and not args.overwrite_output_dir:
last_checkpoint = get_last_checkpoint(args.output_dir)
if last_checkpoint is not None and args.resume_from_checkpoint is None:
print_rank_0(
f"Checkpoint detected, resuming training at {last_checkpoint}. To avoid this"
" behavior, change the `--output_dir` or add `--overwrite_output_dir` to train"
" from scratch."
)
model = transformers.AutoModelForCausalLM.from_pretrained(
model_args.model_name_or_path,
trust_remote_code=args.trust_remote_code,
cache_dir=args.cache_dir,
attn_implementation="flash_attention_2" if model_args.use_flash_attn else "eager",
).to(torch.device("cuda"))
tokenizer = transformers.AutoTokenizer.from_pretrained(
model_args.model_name_or_path,
cache_dir=args.cache_dir,
model_max_length=args.model_max_length,
padding_side="right",
use_fast=True,
trust_remote_code=args.trust_remote_code,
)
if tokenizer.pad_token is None:
smart_tokenizer_and_embedding_resize(
special_tokens_dict={"pad_token": DEFAULT_PAD_TOKEN},
tokenizer=tokenizer,
model=model,
)
data_module = make_supervised_data_module(
training_args, args.train_datapath, args.val_datapath, tokenizer=tokenizer
)
# Detecting last checkpoint.
last_checkpoint = None
if os.path.isdir(args.output_dir) and args.do_train and not args.overwrite_output_dir:
last_checkpoint = get_last_checkpoint(args.output_dir)
if last_checkpoint is None and len(os.listdir(args.output_dir)) > 0:
raise ValueError(
f"Output directory ({args.output_dir}) already exists and is not empty. "
"Use --overwrite_output_dir to overcome."
)
elif last_checkpoint is not None and args.resume_from_checkpoint is None:
print_rank_0(
f"Checkpoint detected, resuming training at {last_checkpoint}. To avoid this"
" behavior, change the `--output_dir` or add `--overwrite_output_dir` to train"
" from scratch."
)
# Training
if args.do_train:
checkpoint = None
if args.resume_from_checkpoint is not None:
checkpoint = args.resume_from_checkpoint
elif last_checkpoint is not None:
checkpoint = last_checkpoint
for name, mod in model.named_modules():
# freeze the embedding layer
if isinstance(mod, torch.nn.Embedding):
mod.weight.requires_grad = False
trainer = Trainer(
model=model, processing_class=tokenizer, args=training_args, **data_module
)
# modelopt sparsity
if args.modelopt_restore_path:
if not os.path.isfile(args.modelopt_restore_path):
raise FileNotFoundError(
f"Sparsity state file {args.modelopt_restore_path} not found."
)
if is_dynamic(model):
raise ValueError("Cannot restore modelopt state dict for a dynamic model.")
print_rank_0(f"Loading sparsity state from {args.modelopt_restore_path}")
mto.restore(trainer.model, args.modelopt_restore_path)
# retrieve the modelopt state after restoring the modelopt state dict
# this is necessary to ensure that `_fsdp_wrapped_module` is not exposed to the modelopt state dict
modelopt_state = mto.modelopt_state(model)
trainer.train(resume_from_checkpoint=checkpoint)
modelopt_state_path = os.path.join(args.output_dir, "finetuned_modelopt_state.pth")
model_state_dict = get_model_state_dict(trainer)
print_rank_0(f"Saving modelopt state to {modelopt_state_path}")
if dist.is_master():
torch.save(
{"modelopt_state": modelopt_state, "model_state_dict": model_state_dict},
modelopt_state_path,
)
if __name__ == "__main__":
train()