-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.py
130 lines (107 loc) · 5.11 KB
/
main.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
from typing import Optional
import framework
import tasks
import os
import torch
torch.backends.cudnn.benchmark = True
def register_args(parser: framework.helpers.ArgumentParser):
tasks.register_args(parser)
parser.add_argument("-batch_size", default=128)
parser.add_argument("-lr", default=1e-3)
parser.add_argument("-wd", default=0.0)
parser.add_argument("-lr_warmup", default=0)
parser.add_argument("-test_interval", default=1000)
parser.add_argument("-state_size", default=128)
parser.add_argument("-n_layers", default=2)
parser.add_argument("-stop_after", default="None", parser=parser.int_or_none_parser)
parser.add_argument("-task", default="tuple")
parser.add_argument("-dropout", default=0.0)
parser.add_argument("-grad_clip", default="1.0", parser=parser.float_or_none_parser)
parser.add_argument("-embedding_size", default="16", parser=parser.int_or_none_parser)
parser.add_argument("-encoder_decoder.n_think_steps", default=0)
parser.add_argument("-transformer.n_heads", default=4)
parser.add_argument("-transformer.use_paper_lr_schedule", default=False)
parser.add_argument("-transformer.variant", default="standard")
parser.add_argument("-transformer.ff_multiplier", default=2.0)
parser.add_argument("-transformer.encoder_n_layers", default=3)
parser.add_argument("-transformer.decoder_n_layers", default="3", parser=parser.int_or_none_parser)
parser.add_argument("-transformer.tied_embedding", default=True)
parser.add_argument("-transformer.attention_dropout", default=0.0)
parser.add_argument("-test_batch_size", default="None", parser=parser.int_or_none_parser)
parser.add_argument("-restore_pretrained", type=str)
parser.add_argument("-test_pretrained", default=1)
parser.add_argument("-train_baseline", default=False, help="Train the model on easy task and test on hard,"
"no masking")
parser.add_argument("-lr_sched.steps", default="", parser=parser.int_list_parser)
parser.add_argument("-lr_sched.gamma", default=0.1)
parser.add_argument("-lr_sched.type", default="step", choice=["step", "noam"])
parser.add_argument("-optimizer", default="adam", choice=["adam", "adamw", "sgd"])
parser.add_argument("-adam.betas", default="0.9,0.999", parser=parser.float_list_parser)
parser.add_argument("-adam.eps", default=1e-8)
parser.add_argument("-amp", default=False)
parser.add_argument("-tied_embedding", default=False)
parser.add_argument("-label_smoothing", default=0.0)
parser.add_argument("-max_length_per_batch", default="none", parser=parser.int_or_none_parser)
parser.add_argument("-length_bucketed_sampling", default=False)
parser.add_argument("-eos", default=True)
parser.add_argument("-sos", default=True)
parser.add_argument("-speedtest", default=False)
parser.add_profile([
parser.Profile("scan", {
"task": "scan",
"n_layers": 2,
"state_size": 200,
"lr": 1e-3,
"grad_clip": "5",
"stop_after": 15000,
"step_per_mask": 15000,
"batch_size": 256,
"dropout": 0.5,
"embedding_size": 16
}),
parser.Profile("trafo_scan", {
"task": "trafo_scan",
"state_size": 128,
"transformer.n_heads": 8,
"test_batch_size": 2048
}, include="scan"),
parser.Profile("listops_trafo", {
"task": "listops_trafo",
"state_size": 256,
"transformer.n_heads": 8,
"batch_size": 256,
"lr": 1e-3,
"grad_clip": 1,
}),
])
def initialize(restore: Optional[str] = None):
helper = framework.helpers.TrainingHelper(wandb_project_name="length_generalization",
register_args=register_args, extra_dirs=["export", "model_weights"],
log_async=True, restore=restore)
task = tasks.get_task(helper.args.task)
task = task(helper)
return helper, task
def main():
helper, task = initialize()
if helper.args.restore_pretrained:
assert not helper.args.train_baseline
pretrained = os.path.expanduser(helper.args.restore_pretrained)
if not helper.args.restore_pretrained.endswith(".pth"):
pretrained = os.path.join(pretrained, str(helper.args.sweep_id_for_grid_search), "model.pth")
assert os.path.isfile(pretrained), f"Failed to load pretrained weights. File {pretrained} not found."
task.load_weights(pretrained)
if helper.args.test_pretrained:
helper.log({f"load_validation/{k}": v for k, v in task.validate().items()})
print("Done. Skipping training...")
else:
if helper.args.train_baseline:
task.set_baseline_mode()
task.train()
print("Training finished. Saving model...")
task.save_weights()
if helper.args.analysis.enable and not helper.args.train_baseline:
task.post_train()
task.finish()
helper.finish()
if __name__ == "__main__":
main()