-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrain_model.py
309 lines (268 loc) · 9.62 KB
/
train_model.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
"""
You can use this script to train a new model from scratch.
By default, this script trains a model under our default settings, but you can
uncomment the corresponding function calls at the bottom of the script to train
a model following one of the ablation settings in the paper.
"""
import copy
import multiprocessing
import os
import shutil
import warnings
import numpy as np
import wandb
from coeditor._utils import cprint, run_long_task
from coeditor.c3problem import (
C3ProblemChangeInlining,
C3ProblemGenerator,
C3ProblemTokenizer,
C3ToCodeCompletion,
)
from coeditor.common import *
from coeditor.dataset import (
C3CombinedEncoder,
C3ProblemDataset,
make_or_load_dataset,
make_or_load_transformed_dataset,
)
from coeditor.model import (
BatchArgs,
C3DataLoader,
DecodingArgs,
RetrievalEditorModel,
TrainingArgs,
)
def train_model(
model_name: str,
dataset_name: str,
description: str,
encoder: C3CombinedEncoder = C3CombinedEncoder(),
batch_args=BatchArgs.train_default(),
eval_batch_args=BatchArgs.eval_default(),
train_args=TrainingArgs(),
fixed_ref_tks_sum: int | None = None,
recreate_data: bool = False,
multi_stage_training: bool = True,
resumed_from: Path | None = None,
model_size: Literal["small", "base", "large"] = "base",
eval_only: bool = False,
quicktest: bool = False,
):
dec_args = DecodingArgs()
if quicktest:
model_name = "quicktest-" + model_name
if not eval_only:
check_save_dir(model_name)
# problems will be transformed and saved for valid and test but not train.
datasets = make_or_load_dataset(
dataset_name,
encoder.change_processor,
remake_problems=recreate_data,
splits=("valid", "test", "train"),
)
with timed_action("Making or loading transformed C3 problems for eval"):
# it's important to cache these due to randomness in the transformations
eval_probs = make_or_load_transformed_dataset(
dataset_name,
datasets,
encoder,
remake_problems=recreate_data,
)
# limit the number of examples for faster testing
datasets["valid"] = random_subset(eval_probs["valid"], 10000, rng=42)
datasets["test"] = random_subset(eval_probs["test"], 10000, rng=42)
config_dict: dict[str, Any] = {
"description": description,
"edit_tokenizer": encoder.edit_tokenizer.get_args(),
"batch_args": batch_args,
"train_args": train_args,
"dec_args": dec_args,
}
project = "Coeditor" if not quicktest else "Coeditor-quicktest"
if eval_only:
project = "eval-" + project
wandb.init(dir="..", project=project, name=model_name, config=config_dict)
if quicktest:
print("Using fewer data for quick test.")
n_quick_exs = 20
datasets = C3ProblemDataset(
train=datasets["train"][:n_quick_exs],
valid=datasets["valid"][:n_quick_exs],
test=datasets["test"][:n_quick_exs],
)
if resumed_from is None:
model = RetrievalEditorModel.from_code_t5(model_size)
else:
model = RetrievalEditorModel.load(resumed_from)
if os.getenv("CUDA_VISIBLE_DEVICES") is None:
warnings.warn(
"CUDA_VISIBLE_DEVICES not set, using 0. Note that "
"the Huggingface Trainer will use all visible GPUs for training."
)
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
train_tkn = encoder.edit_tokenizer
eval_tkn = copy.deepcopy(train_tkn)
eval_tkn.max_query_tks = 1024
eval_tkn.max_output_tks *= 2
eval_tkn.max_ref_tks_sum *= 2
if fixed_ref_tks_sum is not None:
eval_tkn.max_ref_tks_sum = fixed_ref_tks_sum
valid_loader = C3DataLoader(
datasets["valid"], None, eval_tkn, eval_batch_args, shuffle=False, desc="eval"
)
if not eval_only and multi_stage_training:
# gradually increase the ctx size during training
scales = [4, 2]
for scale in scales:
s_tkn = copy.copy(train_tkn)
s_tkn.max_ref_tks_sum //= scale
if fixed_ref_tks_sum is not None:
s_tkn.max_ref_tks_sum = fixed_ref_tks_sum
s_probs = [
x
for x in datasets["train"]
if sum(c.change_size() for c in x.relevant_changes)
< s_tkn.max_ref_tks_sum
]
# n_probs = max(1, scale * len(s_probs) // max(scales))
# s_probs = random_subset(s_probs, n_probs)
desc = f"training (ctx={s_tkn.max_ref_tks_sum})"
s_loader = C3DataLoader(
s_probs,
encoder.problem_tranform,
s_tkn,
batch_args,
shuffle=True,
desc=desc,
)
with timed_action(desc):
model.train_on_data(model_name, s_loader, valid_loader, train_args)
elif not eval_only:
desc = f"training (ctx={train_tkn.max_ref_tks_sum})"
s_probs = [
x
for x in datasets["train"]
if sum(c.change_size() for c in x.relevant_changes)
< C3ProblemTokenizer.max_ref_tks_sum
]
s_loader = C3DataLoader(
s_probs,
encoder.problem_tranform,
train_tkn,
batch_args,
shuffle=True,
desc=desc,
)
with timed_action(desc):
model.train_on_data(model_name, s_loader, valid_loader, train_args)
model.to("cuda")
test_loader = C3DataLoader(
datasets["test"], None, eval_tkn, eval_batch_args, shuffle=False, desc="test"
)
print(f"{len(test_loader)}")
print(f"{len(test_loader.all_probs)}")
with timed_action("Loss Evaluation"):
eval_result = model.eval_loss_on_loader(test_loader)
eval_dict = {f"test/{k}": v.average() for k, v in eval_result.items()}
wandb.log(eval_dict)
with timed_action("Accuracy Evaluation"):
out_dir = get_model_dir() / model_name / "exact_match_samples"
correctness = model.eval_on_data(
datasets["test"],
test_loader,
dec_args,
out_dir,
probs_to_save=300,
)
exact_acc = float(np.mean(correctness))
print("Exact-match accuracy:", exact_acc)
wandb.log({"test/exact-acc": exact_acc})
cprint("blue", "Exact-match samples saved to:", out_dir)
return model
def check_save_dir(model_name: str) -> None:
"Prompt user to remove existing training directory or abort."
training_dir = get_model_dir(False) / model_name
trained_dir = get_model_dir(True) / model_name
if training_dir.exists():
print(f"Training directory already exists:", training_dir)
answer = input("Remove and retrain? (y/n):")
if answer.lower().strip() == "y":
shutil.rmtree(training_dir)
return
else:
print("Training aborted.")
exit(1)
if trained_dir.exists():
print(f"Saved model already exists:", trained_dir)
answer = input("Model will be overriden at the end. Continue? (y/n):")
if answer.lower().strip() != "y":
print("Training aborted.")
exit(1)
def eval_code_completion():
train_model(
model_name="coeditor-xl-c3-completion-v1.6-resumed",
dataset_name="tiny",
description="",
encoder=C3CombinedEncoder(
problem_tranform=C3ToCodeCompletion(),
),
resumed_from=(get_model_dir(True) / "coeditor-xl-c3-dropout-v1.6-resumed"),
eval_only=True,
)
def train_new_model():
train_model(
model_name="coeditor-perm2k-base-v2.0",
dataset_name="perm2k",
description="Coeditor model trained with default settings.",
encoder=C3CombinedEncoder(
problem_tranform=C3ProblemChangeInlining(
max_inline_ratio=0.6, allow_empty_problems=True
),
),
)
def ablation_short_context():
train_model(
model_name="coeditor-perm2k-2048ctx-v2.0",
dataset_name="perm2k",
description="Ablation: Use only 2048 max reference tokens.",
encoder=C3CombinedEncoder(
problem_tranform=C3ProblemChangeInlining(
max_inline_ratio=0.6, allow_empty_problems=True
),
),
fixed_ref_tks_sum=2048,
)
def ablation_no_signatures():
train_model(
model_name="coeditor-perm2k-no_sigs-v2.0",
dataset_name="perm2k",
description="Ablation: No signatures in context.",
encoder=C3CombinedEncoder(
problem_tranform=C3ProblemChangeInlining(
max_inline_ratio=0.6, allow_empty_problems=True
),
edit_tokenizer=C3ProblemTokenizer(disable_unchanged_refs=True),
),
)
def ablation_no_changes():
train_model(
model_name="coeditor-perm2k-no_changes-v2.0",
dataset_name="perm2k",
description="Ablation: No changes in context.",
encoder=C3CombinedEncoder(
problem_tranform=C3ProblemChangeInlining(
max_inline_ratio=0.6, allow_empty_problems=True
),
edit_tokenizer=C3ProblemTokenizer(current_code_only=True),
),
)
if __name__ == "__main__":
os.chdir(proj_root())
with run_long_task("train default model"):
train_new_model()
# with run_long_task("train ablation: short context"):
# ablation_short_context()
# with run_long_task("train ablation: no signatures"):
# ablation_no_signatures()
# with run_long_task("train ablation: no changes"):
# ablation_no_changes()