-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
243 lines (191 loc) · 6.73 KB
/
run.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
import time
from datetime import datetime
from pathlib import Path
from typing import List, Union
import warnings
import logging
import yaml
from functools import partial
import fire
import wandb
import numpy as np
from datasets import Dataset
from transformers import (
Trainer,
TrainingArguments,
set_seed,
AutoTokenizer,
AutoModelForSequenceClassification,
TrainerCallback,
default_data_collator,
DataCollatorWithPadding,
)
warnings.simplefilter("ignore")
logging.disable(logging.WARNING)
DEFAULT = {
"per_device_train_batch_size": 8,
"tf32": False,
"dataloader_num_workers": 0,
"dataloader_pin_memory": True,
"group_by_length": False,
"gradient_checkpointing": False,
"torch_compile": False,
"optim": "adamw_torch",
"mixed_precision": "fp32",
"fp16": False,
"bf16": False,
"model_name_or_path": "roberta-base",
"max_seq_length": 256,
"varied_lengths": False,
"pad_to_multiple_of": None,
"padding": "longest",
"resize_embeddings": 0,
}
NUM_SAMPLES = 10_000
def create_dataset(num_samples, max_seq_length, varied_lengths=False):
"""Create a dummy dataset for testing purposes."""
input_ids = []
for _ in range(num_samples):
if varied_lengths:
length = np.random.randint(1, max_seq_length)
else:
length = max_seq_length
input_ids.append(np.random.randint(200, 300, length))
attention_mask = [[1] * len(input_id) for input_id in input_ids]
return Dataset.from_dict(
{
"input_ids": input_ids,
"attention_mask": attention_mask,
"labels": [0] * len(input_ids),
}
)
def model_init(resize_embeddings, model_name_or_path):
"""
Initialize the model.
Resize the embeddings if necessary.
"""
model = AutoModelForSequenceClassification.from_pretrained(
model_name_or_path, num_labels=2
)
if resize_embeddings > 0:
num_embeds = model.get_input_embeddings().weight.data.size(0)
if num_embeds % resize_embeddings != 0:
model.resize_token_embeddings(
num_embeds + (resize_embeddings - num_embeds % resize_embeddings)
)
return model
class Need4SpeedCallback(TrainerCallback):
def on_init_end(self, args, state, control, **kwargs):
self.counter = 0
self.start_timer = None
self.batches_completed = 0
def on_epoch_begin(self, args, state, control, **kwargs):
self.epoch_start = time.time()
def on_step_begin(self, args, state, control, **kwargs):
self.counter += 1
if (
self.start_timer is None
and (time.time() - self.epoch_start > 10)
and (self.counter > 10)
):
self.start_timer = time.time()
def on_step_end(self, args, state, control, **kwargs):
if self.start_timer is not None:
self.batches_completed += 1
if (self.start_timer is not None) and (time.time() - self.start_timer) > 10:
samples_completed = (
self.batches_completed
* args.per_device_train_batch_size
* args.n_gpu
* args.gradient_accumulation_steps
)
time_elapsed = time.time() - self.start_timer
wandb.log(
{
"samples_per_second": samples_completed / time_elapsed,
"time_elapsed": time_elapsed,
}
)
control.should_training_stop = True
def wandb_train_fn():
with wandb.init() as run:
config = wandb.config
params = [
"per_device_train_batch_size",
"tf32",
"dataloader_num_workers",
"dataloader_pin_memory",
"group_by_length",
"gradient_checkpointing",
"torch_compile",
"optim",
]
sweep_parameters = {
param: config.get(param, DEFAULT[param]) for param in params
}
ds = create_dataset(
num_samples=NUM_SAMPLES,
max_seq_length=config.get("max_seq_length", DEFAULT["max_seq_length"]),
varied_lengths=config.get("varied_lengths", DEFAULT["varied_lengths"]),
)
sweep_parameters["fp16"] = False
if config.get("mixed_precision", DEFAULT["mixed_precision"]) == "fp16":
sweep_parameters["fp16"] = True
elif config.get("mixed_precision", DEFAULT["mixed_precision"]) == "bf16":
sweep_parameters["bf16"] = True
tokenizer = AutoTokenizer.from_pretrained(
config.get("model_name_or_path", DEFAULT["model_name_or_path"])
)
if config.get("varied_lengths", False):
data_collator = DataCollatorWithPadding(
tokenizer=tokenizer,
pad_to_multiple_of=config.get("pad_to_multiple_of", DEFAULT["pad_to_multiple_of"]),
max_length=config.get("max_seq_length", DEFAULT["max_seq_length"]),
padding=config.get("padding", DEFAULT["padding"]),
)
else:
data_collator = default_data_collator
training_args = TrainingArguments(
f"sweeps/wandb-sweep-{wandb.run.id}",
**sweep_parameters,
save_strategy="no",
evaluation_strategy="no",
logging_strategy="no",
disable_tqdm=False,
log_level="warning",
report_to="wandb",
)
_model_init = partial(
model_init,
resize_embeddings=config.get("resize_embeddings", DEFAULT["resize_embeddings"]),
model_name_or_path=config.get("model_name_or_path", DEFAULT["model_name_or_path"]),
)
trainer = Trainer(
args=training_args,
tokenizer=tokenizer,
train_dataset=ds,
model_init=_model_init,
data_collator=data_collator,
callbacks=[Need4SpeedCallback()],
)
trainer.train()
def main(config_path: Union[str, List[str]], n: int = 1):
"""
Run a sweep with the given config file.
Repeat `n` times.
"""
sweep_start_time = datetime.utcnow().strftime("%Y-%m-%d-%H-%M-%S")
if config_path == "all":
config_path = Path("configs").glob("*.yaml")
elif isinstance(config_path, str):
config_path = [config_path]
for config_path in config_path:
with open(config_path) as f:
sweep_config = yaml.safe_load(f)
set_seed(42)
sweep_config["sweep_start_time"] = sweep_start_time
for _ in range(n):
sweep_id = wandb.sweep(sweep_config, project="need4speed")
wandb.agent(sweep_id, wandb_train_fn)
if __name__ == "__main__":
fire.Fire(main)