-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargs.py
286 lines (257 loc) · 8.98 KB
/
args.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
import os
from dataclasses import dataclass, field
from typing import Optional
from transformers import (
MODEL_FOR_MASKED_LM_MAPPING,
HfArgumentParser,
TrainingArguments,
)
from transformers.utils.versions import require_version
MODEL_CONFIG_CLASSES = list(MODEL_FOR_MASKED_LM_MAPPING.keys())
MODEL_TYPES = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES)
@dataclass
class ModelArguments:
"""
Arguments pertaining to which model/config/tokenizer we are going to fine-tune, or train from scratch.
"""
model_name_or_path: Optional[str] = field(
default=None,
metadata={
"help": (
"The model checkpoint for weights initialization. Don't set if you want to train a model from scratch."
)
},
)
model_type: Optional[str] = field(
default=None,
metadata={
"help": "If training from scratch, pass a model type from the list: "
+ ", ".join(MODEL_TYPES)
},
)
config_overrides: Optional[str] = field(
default=None,
metadata={
"help": (
"Override some existing default config settings when a model is trained from scratch. Example: "
"n_embd=10,resid_pdrop=0.2,scale_attn_weights=false,summary_type=cls_index"
)
},
)
config_name: Optional[str] = field(
default=None,
metadata={
"help": "Pretrained config name or path if not the same as model_name"
},
)
tokenizer_name: Optional[str] = field(
default=None,
metadata={
"help": "Pretrained tokenizer name or path if not the same as model_name"
},
)
cache_dir: Optional[str] = field(
default=None,
metadata={
"help": "Where do you want to store the pretrained models downloaded from huggingface.co"
},
)
use_fast_tokenizer: bool = field(
default=True,
metadata={
"help": "Whether to use one of the fast tokenizer (backed by the tokenizers library) or not."
},
)
model_revision: str = field(
default="main",
metadata={
"help": "The specific model version to use (can be a branch name, tag name or commit id)."
},
)
token: str = field(
default=None,
metadata={
"help": (
"The token to use as HTTP bearer authorization for remote files. If not specified, will use the token "
"generated when running `huggingface-cli login` (stored in `~/.huggingface`)."
)
},
)
use_auth_token: bool = field(
default=None,
metadata={
"help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token` instead."
},
)
trust_remote_code: bool = field(
default=False,
metadata={
"help": (
"Whether or not to allow for custom models defined on the Hub in their own modeling files. This option "
"should only be set to `True` for repositories you trust and in which you have read the code, as it will "
"execute code present on the Hub on your local machine."
)
},
)
torch_dtype: Optional[str] = field(
default=None,
metadata={
"help": (
"Override the default `torch.dtype` and load the model under this dtype. If `auto` is passed, the "
"dtype will be automatically derived from the model's weights."
),
"choices": ["auto", "bfloat16", "float16", "float32"],
},
)
attn_implementation: Optional[str] = field(
default="sdpa",
metadata={
"help": ("The attention implementation to use in the model."),
"choices": ["eager", "sdpa", "flash_attention_2"],
},
)
low_cpu_mem_usage: bool = field(
default=False,
metadata={
"help": (
"It is an option to create the model as an empty shell, then only materialize its parameters when the pretrained weights are loaded. "
"set True will benefit LLM loading time and RAM consumption."
)
},
)
def __post_init__(self):
if self.config_overrides is not None and (
self.config_name is not None or self.model_name_or_path is not None
):
raise ValueError(
"--config_overrides can't be used in combination with --config_name or --model_name_or_path"
)
@dataclass
class DataTrainingArguments:
"""
Arguments pertaining to what data we are going to input our model for training and eval.
"""
dataset_path: Optional[str] = field(
default=None,
metadata={"help": "Path to folder with train.json and val.json"},
)
overwrite_cache: bool = field(
default=True,
metadata={"help": "Overwrite the cached training and evaluation sets"},
)
validation_split_percentage: Optional[int] = field(
default=5,
metadata={
"help": "The percentage of the train set used as validation set in case there's no validation split"
},
)
max_seq_length: Optional[int] = field(
default=None,
metadata={
"help": (
"The maximum total input sequence length after tokenization. Sequences longer "
"than this will be truncated."
)
},
)
preprocessing_num_workers: Optional[int] = field(
default=None,
metadata={"help": "The number of processes to use for the preprocessing."},
)
mlm_probability: float = field(
default=0.15,
metadata={"help": "Ratio of tokens to mask for masked language modeling loss"},
)
line_by_line: bool = field(
default=False,
metadata={
"help": "Whether distinct lines of text in the dataset are to be handled as distinct sequences."
},
)
pad_to_max_length: bool = field(
default=False,
metadata={
"help": (
"Whether to pad all samples to `max_seq_length`. "
"If False, will pad the samples dynamically when batching to the maximum length in the batch."
)
},
)
max_train_samples: Optional[int] = field(
default=None,
metadata={
"help": (
"For debugging purposes or quicker training, truncate the number of training examples to this "
"value if set."
)
},
)
max_eval_samples: Optional[int] = field(
default=None,
metadata={
"help": (
"For debugging purposes or quicker training, truncate the number of evaluation examples to this "
"value if set."
)
},
)
inference_on_training_precomputed_path: Optional[str] = field(
default=None,
metadata={"help": "Path to precomputed dataset for training"},
)
def __post_init__(self):
if self.dataset_path is None:
raise ValueError(
"Need dataset_path"
)
# add more arguments
@dataclass
class CustomArguments:
"""
Custom arguments for the script
"""
lora_dropout: float = field(
default=0.05, metadata={"help": "The dropout rate for lora"}
)
lora_r: int = field(default=8, metadata={"help": "The r value for lora"})
mask_token_type: str = field(
default="blank",
metadata={"help": "The type of mask token. Options: blank, eos, mask"},
)
stop_after_n_steps: int = field(
default=10000, metadata={"help": "Stop training after n steps"}
)
data_collator_type: str = field(
default="default",
metadata={"help": "The type of data collator. Options: default, all_mask"},
)
contrastive_loss_scale: float = field(
default=20.0, metadata={"help": "The loss scale for the contrastive loss function"}
)
contrastive_mask_prob: float = field(
default=0.5, metadata={"help": "The masking probability for the contrastive loss function"}
)
tail_neg_ratio: float = field(
default=0.5, metadata={"help": ""}
)
num_negatives_per_positive: int = field(
default=2, metadata={"help": ""}
)
negatives_key: str = field(
default="neg_samples",
metadata={"help": "Key by which negative_indices are located"},
)
mntp_loss_weight: float = field(
default=1.0, metadata={"help": "MNTP loss objective weight"}
)
sc_loss_weight: float = field(
default=1.0, metadata={"help": "SC loss objective weight"}
)
def parse_args(config_path):
parser = HfArgumentParser(
(ModelArguments, DataTrainingArguments, TrainingArguments, CustomArguments)
)
model_args, data_args, training_args, custom_args = parser.parse_json_file(
json_file=os.path.abspath(config_path)
)
return model_args, data_args, training_args, custom_args