-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
273 lines (250 loc) · 8.65 KB
/
utils.py
File metadata and controls
273 lines (250 loc) · 8.65 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
import json
from dataclasses import dataclass, field
from typing import Optional
from transformers import MODEL_FOR_CAUSAL_LM_MAPPING
MODEL_CONFIG_CLASSES = list(MODEL_FOR_CAUSAL_LM_MAPPING.keys())
MODEL_TYPES = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES)
# Define a function to read the JSONL file
def read_jsonl(file_path):
try:
with open(file_path, "r", encoding="utf-8") as file:
lines = file.readlines()
data = [json.loads(line) for line in lines]
return data
except FileNotFoundError:
print(f"File not found: {file_path}")
return []
def reduce_array(arr, n):
"""
Reduce the elements of the sorted array to n elements, evenly reducing the elements.
Parameters:
- arr: The original sorted array
- n: The number of elements after reduction
Returns:
- The new reduced array
"""
N = len(arr)
if n >= N:
return arr
step = N / n
reduced_arr = [arr[int(i * step)] for i in range(n)]
return reduced_arr
@dataclass
class ModelArguments:
"""
Arguments pertaining to which model/config/tokenizer we are going to fine-tune, or train from scratch.
"""
model: Optional[str] = field(
default=None,
metadata={"help": "The model architecture to be trained or fine-tuned."},
)
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)."
},
)
use_auth_token: bool = field(
default=False,
metadata={
"help": (
"Will use the token generated when running `huggingface-cli login` (necessary to use this script "
"with private models)."
)
},
)
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_name: Optional[str] = field(
default=None,
metadata={"help": "The name of the dataset to use (via the datasets library)."},
)
dataset_config_name: Optional[str] = field(
default=None,
metadata={
"help": "The configuration name of the dataset to use (via the datasets library)."
},
)
train_file: Optional[str] = field(
default=None, metadata={"help": "The input training data file (a text file)."}
)
validation_file: Optional[str] = field(
default=None,
metadata={
"help": "An optional input evaluation data file to evaluate the perplexity on (a text file)."
},
)
load_data_from_cache: bool = field(
default=False,
metadata={"help": "Whether not to load data from cache"},
)
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."
)
},
)
block_size: Optional[int] = field(
default=512,
metadata={
"help": (
"Optional input sequence length after tokenization. "
"The training dataset will be truncated in block of this size for training. "
"Default to the model max input length for single sentence inputs (take into account special tokens)."
)
},
)
overwrite_cache: bool = field(
default=False,
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"
},
)
preprocessing_num_workers: Optional[int] = field(
default=None,
metadata={"help": "The number of processes to use for the preprocessing."},
)
keep_linebreaks: bool = field(
default=True,
metadata={"help": "Whether to keep line breaks when using TXT files or not."},
)
def __post_init__(self):
if (
self.dataset_name is None
and self.train_file is None
and self.validation_file is None
):
raise ValueError(
"Need either a dataset name or a training/validation file."
)
else:
if self.train_file is not None:
extension = self.train_file.split(".")[-1]
assert extension in [
"csv",
"json",
"jsonl",
"txt",
], "`train_file` should be a csv, a json, a jsonl or a txt file."
if self.validation_file is not None:
extension = self.validation_file.split(".")[-1]
assert extension in [
"csv",
"json",
"jsonl",
"txt",
], "`validation_file` should be a csv, a json, a jsonl or a txt file."
@dataclass
class DataPredictionArguments:
"""
Arguments pertaining to what data we are going to input our model for prediction.
"""
input_file: Optional[str] = field(
default=None, metadata={"help": "The input data file (a text file)."}
)
output_file: Optional[str] = field(
default=None, metadata={"help": "The output file to write the predictions to."}
)
batch_size: Optional[int] = field(
default=16, metadata={"help": "Batch size (default to 16)."}
)
max_predict_samples: Optional[int] = field(
default=None, metadata={"help": "The maximum number of samples to predict."}
)
max_new_tokens: Optional[int] = field(
default=100,
metadata={
"help": (
"The maximum number of new tokens to generate. "
"The actual number of new tokens generated may be less."
)
},
)
num_return_sequences: Optional[int] = field(
default=1, metadata={"help": "The number of different sequences to generate."}
)
@dataclass
class ReplayArguments:
do_replay: Optional[bool] = field(
default=False, metadata={"help": "Whether to replay the training."}
)
replay_file: Optional[str] = field(
default=None, metadata={"help": "The replay file to replay the training."}
)
replay_ratio: Optional[float] = field(
default=0, metadata={"help": "The replay ratio."}
)