-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsfttrainer.py
More file actions
326 lines (295 loc) · 13.7 KB
/
sfttrainer.py
File metadata and controls
326 lines (295 loc) · 13.7 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
import trl
from trl import SFTTrainer
import dataclasses
import inspect
import os
import warnings
from typing import Callable, Dict, List, Optional, Tuple, Union
import datasets
import torch
import torch.nn as nn
from accelerate.state import PartialState
from datasets import Dataset
from datasets.arrow_writer import SchemaInferenceError
from datasets.builder import DatasetGenerationError
from transformers import (
AutoModelForCausalLM,
AutoTokenizer,
BaseImageProcessor,
DataCollator,
DataCollatorForLanguageModeling,
FeatureExtractionMixin,
PreTrainedModel,
PreTrainedTokenizerBase,
ProcessorMixin,
Trainer,
is_wandb_available,
)
from transformers.trainer_callback import TrainerCallback
from transformers.trainer_utils import EvalPrediction
from transformers.utils import is_liger_kernel_available, is_peft_available
from transformers.utils.deprecation import deprecate_kwarg
from trl.extras.dataset_formatting import get_formatting_func_from_dataset
from trl.sft_config import SFTConfig
from trl.utils import (
ConstantLengthDataset,
DataCollatorForCompletionOnlyLM,
generate_model_card,
peft_module_casting_to_bf16,
)
"""Changes to the original SFTTrainer class are marked with an EDITS comment"""
class PruningSFTTrainer(SFTTrainer):
def __init__(
self,
model: Optional[Union[PreTrainedModel, nn.Module, str]] = None,
args: Optional[SFTConfig] = None,
data_collator: Optional[DataCollator] = None, # type: ignore
train_dataset: Optional[Dataset] = None,
eval_dataset: Optional[Union[Dataset, Dict[str, Dataset]]] = None,
processing_class: Optional[
Union[
PreTrainedTokenizerBase,
BaseImageProcessor,
FeatureExtractionMixin,
ProcessorMixin,
]
] = None,
model_init: Optional[Callable[[], PreTrainedModel]] = None,
compute_metrics: Optional[Callable[[EvalPrediction], Dict]] = None,
callbacks: Optional[List[TrainerCallback]] = None,
optimizers: Tuple[torch.optim.Optimizer, torch.optim.lr_scheduler.LambdaLR] = (
None,
None,
),
preprocess_logits_for_metrics: Optional[
Callable[[torch.Tensor, torch.Tensor], torch.Tensor]
] = None,
peft_config: Optional["PeftConfig"] = None,
formatting_func: Optional[Callable] = None,
):
if args is None:
output_dir = "tmp_trainer"
warnings.warn(f"No `SFTConfig` passed, using `output_dir={output_dir}`.")
args = SFTConfig(output_dir=output_dir)
elif args is not None and args.__class__.__name__ == "TrainingArguments":
args_as_dict = args.to_dict()
# Manually copy token values as TrainingArguments.to_dict() redacts them
args_as_dict.update(
{
k: getattr(args, k)
for k in args_as_dict.keys()
if k.endswith("_token")
}
)
args = SFTConfig(**args_as_dict)
if getattr(args, "model_init_kwargs", None) is None:
model_init_kwargs = {}
elif not isinstance(model, str):
raise ValueError(
"You passed model_init_kwargs to the SFTConfig, but your model is already instantiated."
)
else:
model_init_kwargs = args.model_init_kwargs
torch_dtype = model_init_kwargs.get("torch_dtype")
if torch_dtype is not None:
# Convert to `torch.dtype` if an str is passed
if isinstance(torch_dtype, str) and torch_dtype != "auto":
torch_dtype = getattr(torch, torch_dtype)
if torch_dtype != "auto" and not isinstance(torch_dtype, torch.dtype):
raise ValueError(
f"Invalid `torch_dtype` passed to the SFTConfig. Expected a string with either `torch.dtype` or 'auto', but got {torch_dtype}."
)
model_init_kwargs["torch_dtype"] = torch_dtype
if isinstance(model, str):
warnings.warn(
"You passed a model_id to the SFTTrainer. This will automatically create an "
"`AutoModelForCausalLM` or a `PeftModel` (if you passed a `peft_config`) for you."
)
if args.use_liger:
model = AutoLigerKernelForCausalLM.from_pretrained(
model, **model_init_kwargs
)
else:
model = AutoModelForCausalLM.from_pretrained(model, **model_init_kwargs)
if (
args.packing
and data_collator is not None
and isinstance(data_collator, DataCollatorForCompletionOnlyLM)
):
raise ValueError(
"You passed a `DataCollatorForCompletionOnlyLM` to the SFTTrainer. This is not compatible with the `packing` argument."
)
if is_peft_available() and peft_config is not None:
if not isinstance(peft_config, PeftConfig):
raise ValueError(
"If you want to use the PeftModel, you need to pass a PeftConfig object to the SFTTrainer."
f" and you passed a {type(peft_config)}."
)
if not isinstance(model, PeftModel):
_support_gc_kwargs = hasattr(
args, "gradient_checkpointing_kwargs"
) and "gradient_checkpointing_kwargs" in list(
inspect.signature(prepare_model_for_kbit_training).parameters
)
gradient_checkpointing_kwargs = (
getattr(args, "gradient_checkpointing_kwargs", None) or {}
)
is_sharded_qlora = False
# Below is to support QLoRA + FSDP / DS-Zero3 - one should never call
# peft_module_casting_to_bf16 or prepare_model_for_kbit_training when doing
# QLoRA + FSDP / DS-Zero3
if getattr(model, "is_loaded_in_4bit", False):
for _, param in model.named_parameters():
if param.__class__.__name__ == "Params4bit":
is_sharded_qlora = param.data.device.type in {"cpu", "meta"}
break
if getattr(model, "is_loaded_in_8bit", False) or (
getattr(model, "is_loaded_in_4bit", False) and not is_sharded_qlora
):
prepare_model_kwargs = {
"use_gradient_checkpointing": getattr(
args, "gradient_checkpointing", False
)
}
if _support_gc_kwargs:
prepare_model_kwargs["gradient_checkpointing_kwargs"] = (
gradient_checkpointing_kwargs
)
model = prepare_model_for_kbit_training(
model, **prepare_model_kwargs
)
if args is not None:
args = dataclasses.replace(args, gradient_checkpointing=False)
elif getattr(args, "gradient_checkpointing", False) and (
"use_reentrant" not in gradient_checkpointing_kwargs
or gradient_checkpointing_kwargs["use_reentrant"]
):
# For backward compatibility with older versions of transformers
if hasattr(model, "enable_input_require_grads"):
model.enable_input_require_grads()
else:
def make_inputs_require_grad(module, input, output):
output.requires_grad_(True)
model.get_input_embeddings().register_forward_hook(
make_inputs_require_grad
)
if (
"autocast_adapter_dtype"
in list(inspect.signature(get_peft_model).parameters)
and getattr(model, "is_loaded_in_4bit", False)
and is_sharded_qlora
):
model = get_peft_model(
model, peft_config, autocast_adapter_dtype=False
)
else:
model = get_peft_model(model, peft_config)
if (
args is not None
and args.bf16
and getattr(model, "is_loaded_in_4bit", False)
and not is_sharded_qlora
):
peft_module_casting_to_bf16(model)
if processing_class is None:
processing_class = AutoTokenizer.from_pretrained(model.config._name_or_path)
if getattr(processing_class, "pad_token", None) is None:
processing_class.pad_token = processing_class.eos_token
if args.max_seq_length is None:
# to overcome some issues with broken tokenizers
args.max_seq_length = min(processing_class.model_max_length, 1024)
warnings.warn(
f"You didn't pass a `max_seq_length` argument to the SFTTrainer, this will default to {args.max_seq_length}"
)
self.dataset_num_proc = args.dataset_num_proc
self.dataset_batch_size = args.dataset_batch_size
if args.dataset_kwargs is None:
args.dataset_kwargs = {}
if formatting_func is None:
# check if dataset has ChatML format or instruction format and is supported
# if not stays None
formatting_func = get_formatting_func_from_dataset(
train_dataset, processing_class
)
# if a template is detected, we don't need to add special tokens again
if formatting_func is not None:
args.dataset_kwargs["add_special_tokens"] = False
if not args.packing:
if data_collator is None:
data_collator = DataCollatorForLanguageModeling(
tokenizer=processing_class, mlm=False
)
# Pre-process the datasets only once per node. The remaining processes will use the cache.
with PartialState().local_main_process_first():
if train_dataset is not None:
train_dataset = self._prepare_dataset(
train_dataset,
processing_class,
args.packing,
args.dataset_text_field,
args.max_seq_length,
formatting_func,
args.num_of_sequences,
args.chars_per_token,
remove_unused_columns=(
args.remove_unused_columns if args is not None else True
),
**args.dataset_kwargs,
)
if eval_dataset is not None:
_multiple = isinstance(eval_dataset, dict)
_eval_datasets = (
eval_dataset if _multiple else {"singleton": eval_dataset}
)
eval_packing = (
args.packing if args.eval_packing is None else args.eval_packing
)
for _eval_dataset_name, _eval_dataset in _eval_datasets.items():
_eval_datasets[_eval_dataset_name] = self._prepare_dataset(
_eval_dataset,
processing_class,
eval_packing,
args.dataset_text_field,
args.max_seq_length,
formatting_func,
args.num_of_sequences,
args.chars_per_token,
remove_unused_columns=(
args.remove_unused_columns if args is not None else True
),
**args.dataset_kwargs,
)
if not _multiple:
eval_dataset = _eval_datasets["singleton"]
if (
processing_class.padding_side is not None
and processing_class.padding_side != "right"
):
warnings.warn(
"You passed a processing_class with `padding_side` not equal to `right` to the SFTTrainer. This might lead to some unexpected behaviour due to "
"overflow issues when training a model in half-precision. You might consider adding `processing_class.padding_side = 'right'` to your code."
)
super().__init__(
model=model,
args=args,
data_collator=data_collator,
train_dataset=train_dataset,
eval_dataset=eval_dataset,
processing_class=processing_class,
model_init=model_init,
compute_metrics=compute_metrics,
callbacks=callbacks,
optimizers=optimizers,
preprocess_logits_for_metrics=preprocess_logits_for_metrics,
)
# Add tags for models that have been loaded with the correct transformers version
if hasattr(self.model, "add_model_tags"):
self.model.add_model_tags(self._tag_names)
if self.train_dataset is not None:
if self.args.max_steps > 0 and args.packing:
warnings.warn(
"You passed `packing=True` to the SFTTrainer/SFTConfig, and you are training your model with `max_steps` strategy. The dataset will be iterated until the `max_steps` are reached."
)
self.train_dataset.infinite = True
elif self.args.max_steps == -1 and args.packing:
self.train_dataset.infinite = False