forked from EleutherAI/w2s
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cost_sweep.py
347 lines (323 loc) · 11.8 KB
/
cost_sweep.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
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
import copy
from functools import partial
from pathlib import Path
import numpy as np
def openai_batch_size_epochs(n):
if n < 1024:
batch_size = 1
elif n < 4096:
batch_size = 2
else:
batch_size = 8
if n < 30:
n_epochs = np.ceil(100 / (n // batch_size))
elif n < 4096:
n_epochs = 3
elif n < 16_384:
n_epochs = 2
else:
n_epochs = 1
return batch_size, n_epochs
def estop_modify_stages_by_n(stages, num_weak, num_oracle, mbs=1, bs=32):
stages = [
stage
for stage in stages
if (stage["type"] == "weak" and num_weak > 0)
or (stage["type"] == "oracle" and num_oracle > 0)
]
# make sure the first stage uses warmup
if stages[0].get("warmup_steps") == 0:
stages[0]["warmup_steps"] = 40
for stage in stages:
is_weak = stage["type"] == "weak"
# NOTE: total number of datapoints, including repetions over epochs
total_points = 30_000
num = num_weak if is_weak else num_oracle
num_epochs = max(total_points / num, 1)
stage["size"] = num
steps_per_epoch = int(np.ceil(stage["size"] / bs))
eval_every = min(
default_eval_every, steps_per_epoch
) # eval at least every epoch
stage["eval_steps"], stage["save_steps"] = (
eval_every,
eval_every,
)
# set num warmup steps to no more than the number of steps per epoch
if "warmup_steps" in stage:
stage["warmup_steps"] = max(min(stage["warmup_steps"], steps_per_epoch), 2)
if stage.get("load_best_model_at_end"):
assert "val_frac" in stage
if "val_frac" in stage:
stage["n_val"] = max(int(num * stage["val_frac"]), 2)
del stage["val_frac"]
stage["num_train_epochs"] = num_epochs
stage["per_device_train_batch_size"] = mbs
stage["gradient_accumulation_steps"] = bs // mbs
stage["per_device_eval_batch_size"] = mbs
return stages
def openai_modify_stages_by_n(stages, num_weak, num_oracle):
stages = [
stage
for stage in stages
if (stage["type"] == "weak" and num_weak > 0)
or (stage["type"] == "oracle" and num_oracle > 0)
]
# make sure the first stage uses warmup
if stages[0].get("warmup_steps") == 0:
stages[0]["warmup_steps"] = 40
for stage in stages:
is_weak = stage["type"] == "weak"
num = num_weak if is_weak else num_oracle
bs, num_epochs = openai_batch_size_epochs(num)
mbs = 1
stage["size"] = num
steps_per_epoch = int(np.ceil(stage["size"] / bs))
# don't do intermediate evals
stage["eval_steps"] = stage["save_steps"] = 1_000_000
# set num warmup steps to no more than the number of steps per epoch
if "warmup_steps" in stage:
stage["warmup_steps"] = max(min(stage["warmup_steps"], steps_per_epoch), 2)
assert "val_frac" not in stage and not stage.get("load_best_model_at_end")
stage["num_train_epochs"] = num_epochs
stage["per_device_train_batch_size"] = mbs
stage["gradient_accumulation_steps"] = bs // mbs
stage["per_device_eval_batch_size"] = mbs
return stages
# CFG 1: LP(weak), FT(GT), FT(weak) with new head, FT(GT)
cfgs = {
"seq_sft_openai_settings": {
"stages": [
{
"modules_with_grad": "all",
"type": "weak",
"sampling": "random",
"warmup_steps": 40,
"load_best_model_at_end": False,
},
{
"modules_with_grad": "all",
"type": "oracle",
"sampling": "random",
"warmup_steps": 40,
"reuse_optimizer_checkpoint": False,
},
],
"modify_stages_by_n": openai_modify_stages_by_n,
},
"seq_sft_both_estop_clean_disjoint_2shot": {
"stages": [
{
"modules_with_grad": "all",
"type": "weak",
"sampling": "random",
"warmup_steps": 40,
"val_frac": 0.2,
"load_best_model_at_end": True,
},
{
"modules_with_grad": "all",
"type": "oracle",
"sampling": "random",
"warmup_steps": 40,
"val_frac": 0.2,
"load_best_model_at_end": True,
"reuse_optimizer_checkpoint": False,
},
],
"modify_stages_by_n": estop_modify_stages_by_n,
"extra_args": ["--n_few_shot 2", "--few_shot_type weak"],
},
"seq_sft_both_estop_clean_disjoint_32shot_weak": {
"stages": [
{
"modules_with_grad": "all",
"type": "weak",
"sampling": "random",
"warmup_steps": 40,
"val_frac": 0.2,
"load_best_model_at_end": True,
},
{
"modules_with_grad": "all",
"type": "oracle",
"sampling": "random",
"warmup_steps": 40,
"val_frac": 0.2,
"load_best_model_at_end": True,
"reuse_optimizer_checkpoint": False,
},
],
"modify_stages_by_n": estop_modify_stages_by_n,
"extra_args": ["--n_few_shot 32", "--few_shot_type weak"],
},
"seq_sft_both_estop_clean_disjoint": {
"stages": [
{
"modules_with_grad": "all",
"type": "weak",
"sampling": "random",
"warmup_steps": 40,
"val_frac": 0.2,
"load_best_model_at_end": True,
},
{
"modules_with_grad": "all",
"type": "oracle",
"sampling": "random",
"warmup_steps": 40,
"val_frac": 0.2,
"load_best_model_at_end": True,
"reuse_optimizer_checkpoint": False,
},
],
"modify_stages_by_n": estop_modify_stages_by_n,
},
"seq_sft_both_estop_disjoint_logconf": {
"stages": [
{
"modules_with_grad": "all",
"type": "weak",
"sampling": "random",
"warmup_steps": 40,
"val_frac": 0.2,
"load_best_model_at_end": True,
"loss": "logconf",
"per_device_train_batch_size": 8,
"gradient_accumulation_steps": 4,
"per_device_eval_batch_size": 8,
},
{
"modules_with_grad": "all",
"type": "oracle",
"sampling": "random",
"warmup_steps": 40,
"val_frac": 0.2,
"load_best_model_at_end": True,
"reuse_optimizer_checkpoint": False,
},
],
"modify_stages_by_n": partial(estop_modify_stages_by_n, mbs=8, bs=32),
"extra_args": ["--max_ctx 403"],
},
"seq_sft_both_estop_active_oracle_disjoint": {
"stages": [
{
"modules_with_grad": "all",
"type": "weak",
"sampling": "random",
"warmup_steps": 40,
"val_frac": 0.2,
"load_best_model_at_end": True,
},
{
"modules_with_grad": "all",
"type": "oracle",
"sampling": "least_confident_pred",
"sample_temp": 0.0,
"warmup_steps": 40,
"load_best_model_at_end": True,
"val_frac": 0.2,
"reuse_optimizer_checkpoint": False,
},
],
"modify_stages_by_n": estop_modify_stages_by_n,
},
}
root = str(Path(__file__).parent)
weak_models = [
"Qwen/Qwen1.5-0.5B",
]
ds_names = [
"boolq",
"hellaswag",
"paws",
"sciq",
"cola",
"cosmos_qa",
"quail",
"social_i_qa",
]
weak_ds_list = [
f"{ds_name}_{model_name.split('/')[-1]}"
for ds_name in ds_names
for model_name in weak_models
]
strong_model_names = [
"Qwen/Qwen1.5-0.5B",
"Qwen/Qwen1.5-4B",
"Qwen/Qwen1.5-7B",
"meta-llama/Meta-Llama-3-8B",
"meta-llama/Meta-Llama-3-70B",
]
default_eval_every = 50
for seed in [0,]:
for i, strong_model_name in list(enumerate(strong_model_names)):
quantize = "70B" in strong_model_name
for weak_ds in weak_ds_list:
for sweep_name, cfg in cfgs.items():
base_command = (
"python train_transformer_reporter.py "
"{weak_ds_path} "
"{oracle_ds_path} "
"{test_ds_path} "
"100_000 100_000 1000 "
"--seed {seed} "
"--strong_model_name {model_name} "
"--reporter_stages {reporter_stages} "
f"--eval_steps {default_eval_every} "
f"--save_steps {default_eval_every} "
"--save_total_limit 1 "
f"--results_folder {root}/{weak_ds} "
'--run_name "{run_name}" '
)
for extra_arg in cfg.get("extra_args", []):
base_command += f"{extra_arg} "
if quantize:
base_command += "--quantize "
weak_ds_path = f"{root}/{weak_ds}/weak_train"
oracle_ds_path = f"{root}/{weak_ds}/weak_train"
test_ds_path = f"{root}/{weak_ds}/weak_test"
def get_command(cfg, num_weak, num_oracle):
stages = copy.deepcopy(cfg["stages"])
stages = cfg["modify_stages_by_n"](stages, num_weak, num_oracle)
model_last = strong_model_name.split("/")[-1]
run_name = f"nw={num_weak}_no={num_oracle}_m={model_last}_{sweep_name}_s{seed}"
command = base_command.format(
weak_ds_path=weak_ds_path,
oracle_ds_path=oracle_ds_path,
test_ds_path=test_ds_path,
seed=seed,
reporter_stages=len(stages),
run_name=run_name,
model_name=strong_model_name,
)
for j, stage in enumerate(stages):
prefix = f"stage{j}_"
for key, value in stage.items():
if isinstance(value, bool):
if value:
command += f"--{prefix}{key} "
else:
command += f"--{prefix}{key} {value} "
return command
weak_marginal_costs = [1 / 10]
oracle_affordables = [16, 64, 256, 1024, 4096]
oracle_spending_fracs = [0.8, 0.6, 0.4, 0.2, 0.05]
pairs = []
for weak_marginal_cost in weak_marginal_costs:
for oracle_affordable in oracle_affordables:
accs = []
actual_osfs = []
for osf in oracle_spending_fracs:
n_oracle = int(osf * oracle_affordable)
n_weak = int(
(oracle_affordable - n_oracle) / weak_marginal_cost
)
n_oracle = min(n_oracle, 23_000)
pairs.append((n_weak, n_oracle))
pairs.append((0, 8192))
for num_weak, num_oracle in pairs:
cmd = get_command(cfg, num_weak, num_oracle)
if cmd:
print(cmd)