-
Notifications
You must be signed in to change notification settings - Fork 1
/
trainer.py
156 lines (141 loc) · 5.28 KB
/
trainer.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
import pytorch_lightning as pl
import torch
from pytorch_lightning.callbacks.early_stopping import EarlyStopping
from pytorch_lightning.loggers import WandbLogger
from transformers import GPT2Config
from transformers import GPT2LMHeadModel
from datamodules import ChineseWebtextDataModule
from datamodules import FileDataModule
from datamodules import OpenWebText2DataModule
from datamodules import OscarDataModule
from gpt import GPTLightning
from utils import get_pst_time
def get_trainer(args):
if args.dataset == "openwebtext2":
print("getting openwebtext2 datamodule")
data_module = OpenWebText2DataModule(
sequence_length=args.n_ctx,
batch_size=args.mini_batch_size,
eval_batch_size=args.eval_batch_size,
data_dir=args.data,
)
elif args.dataset == "webtext2019zh":
print("getting webtext2019zh datamodule")
data_module = ChineseWebtextDataModule(
sequence_length=args.n_ctx,
batch_size=args.mini_batch_size,
eval_batch_size=args.eval_batch_size,
data_dir=args.data,
token_limit=args.token_limit,
diff_tokenization=True if args.diff_tokenization > 0 else False,
)
elif "oscar" in args.dataset:
print("getting Oscar datamoduble")
data_module = OscarDataModule(
sequence_length=args.n_ctx,
batch_size=args.mini_batch_size,
eval_batch_size=args.eval_batch_size,
data_dir=args.data,
token_limit=args.token_limit,
diff_tokenization=True if args.diff_tokenization > 0 else False,
)
else:
print("getting file datamodule")
data_module = FileDataModule(
sequence_length=args.n_ctx,
batch_size=args.mini_batch_size,
eval_batch_size=args.eval_batch_size,
data_dir=args.data,
)
print("preparing dm")
data_module.prepare_data()
print("setting up dm")
data_module.setup("fit")
ntokens = len(data_module.vocab)
args.n_tokens = ntokens
print("creating config")
configuration = GPT2Config(
vocab_size=args.n_tokens,
n_ctx=args.n_ctx,
n_positions=args.n_ctx,
n_layer=args.n_layer,
n_head=args.n_head,
n_inner=args.d_ff,
n_embd=args.d_embd,
bos_token_id=data_module.tokenizer.bos_token_id,
eos_token_id=data_module.tokenizer.eos_token_id,
attn_pdrop=args.dropatt,
embd_pdrop=args.dropout,
resid_pdrop=args.dropout,
)
model = GPT2LMHeadModel(configuration)
if args.finetune > 0:
print("finetuning")
checkpoint_file = "{}/{}.pt".format(args.checkpoints_dir, args.model_size)
checkpoint = torch.load(
checkpoint_file, map_location="cuda:{}".format(args.n_gpus)
)
state_dict = checkpoint["model_state_dict"]
model.load_state_dict(state_dict)
args.n_all_param = sum([p.nelement() for p in model.parameters()])
args.n_nonemb_param = sum(
[p.nelement() for p in model.parameters() if p.requires_grad]
)
gpt_pl = GPTLightning(model=model, args=args, tokenizer=data_module.tokenizer)
dt_string = get_pst_time()
run_name = "{}_{}_{}_{}".format(args.dataset, args.model_size, args.note, dt_string)
wandb_logger = WandbLogger(
name=run_name,
project="openwebtext2",
entity=args.entity,
save_dir=args.save_dir,
)
eval_interval = (
args.eval_interval * args.accumulate_grad_batches
if args.eval_interval > 0
else 1.0
)
limit_train_batches = (
args.limit_train_batches * args.accumulate_grad_batches
if args.limit_train_batches > 0
else 1.0
)
print("eval interval is {}".format(eval_interval))
if args.n_gpus > 1:
trainer = pl.Trainer(
val_check_interval=args.eval_interval,
weights_summary="full",
gpus=args.n_gpus,
logger=wandb_logger,
accelerator="ddp",
gradient_clip_val=args.clip,
limit_val_batches=args.max_eval_steps * args.accumulate_grad_batches,
max_steps=args.max_step,
accumulate_grad_batches=args.accumulate_grad_batches,
enable_pl_optimizer=True,
)
else:
print("no ddp")
trainer = pl.Trainer(
val_check_interval=eval_interval,
weights_summary="full",
gpus=[args.n_gpus],
logger=wandb_logger,
gradient_clip_val=args.clip,
accumulate_grad_batches=args.accumulate_grad_batches,
max_steps=args.max_step * 10000,
max_epochs=1000,
enable_pl_optimizer=True,
log_every_n_steps=args.accumulate_grad_batches,
callbacks=[
EarlyStopping(monitor="validation_avg_loss", patience=3, verbose=True)
],
limit_train_batches=limit_train_batches,
limit_val_batches=args.max_eval_steps,
# check_val_every_n_epoch=1,
)
trainer.fit(gpt_pl, datamodule=data_module)
file_path = "{dir}/last.pt".format(dir=wandb_logger.experiment.dir,)
trainer.save_checkpoint(file_path)
#use latest model and run on test set
trainer.test(ckpt_path=None, datamodule=data_module)