-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlang_runner.py
172 lines (136 loc) · 6.53 KB
/
lang_runner.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
# IMPORTS
import os
import numpy as np
import torch
import torch.optim as optim
from data.dataset import get_dataset
from lang_model import MAC
#from transformers.optimization import get_constant_schedule_with_warmup
import wandb
class Runner(object):
def __init__(self, cfg):
self.cfg = cfg
self.device_id = 'cuda:{}'.format(cfg.local_rank)
self.master_node = (self.cfg.local_rank == 0)
self.distributed = (self.cfg.world_size > 1)
self.train_loader, self.test_loader = get_dataset(cfg, distributed=self.distributed)
self.obs = (self.cfg.seqlen,)
xdim = np.prod(self.obs)
self.epoch = 0
self.net = MAC(obs_dims=self.obs, cfg=self.cfg)
self.net.to(self.device_id)
if self.distributed:
self.net = torch.nn.parallel.DistributedDataParallel(self.net, device_ids=[cfg.local_rank], output_device=cfg.local_rank)
self.net_module = self.net.module
else:
self.net_module = self.net
self.clip_grad = 0.25
self.optimizer = optim.AdamW(self.net.parameters(), lr=self.cfg.lr, betas=(0.9, 0.99), eps=1e-08, weight_decay=0.001)
#self.scheduler = get_constant_schedule_with_warmup(self.optimizer, num_warmup_steps=5000) # some cuda driver issues
if self.cfg.loadpath is not None:
self.load(self.cfg.loadpath)
self.save_every = 200
self.eval_every = 5
def load(self, path):
map_location = {"cuda:0": self.device_id}
checkpoint = torch.load(path, map_location=map_location)
self.net_module.load_state_dict(checkpoint['net'])
self.optimizer.load_state_dict(checkpoint['optimizer'])
self.epoch = checkpoint['epoch']
print("loaded", flush=True)
# scheduler: assumes we dont warmup anymore
# self.scheduler = get_constant_schedule_with_warmup(self.optimizer, num_warmup_steps=0)
def train(self):
print("training rank %u" % self.cfg.local_rank, flush=True)
self.net.train()
dataloader = self.train_loader
while self.epoch < self.cfg.n_epochs:
epoch_metrics = {
'log_ll': 0,
'count': 0,
}
bsz = 0
accum, accumll = 0, 0.0
self.net.train()
for it, (X, y) in enumerate(dataloader):
X = X.cuda(device=self.device_id, non_blocking=True)
X = X.long().squeeze(dim=1) + 1
log_ll = self.net(X)
(-log_ll).backward()
count = X.shape[0]
epoch_metrics['log_ll'] += log_ll * count
epoch_metrics['count'] += count
bsz += X.shape[0]
accum += X.shape[0]
accumll += log_ll * count
if bsz >= 512 // self.cfg.world_size:
total_norm = torch.nn.utils.clip_grad_norm_(self.net.parameters(), self.clip_grad)
self.optimizer.step()
# self.scheduler.step()
self.optimizer.zero_grad()
bsz = 0
if accum >= 5120 // self.cfg.world_size:
if self.master_node:
last_lr = [self.cfg.lr] #self.scheduler.get_last_lr()
print("Iter %u out of %u, log-ll: %.2f, lr: %f" % (it, len(dataloader), log_ll, last_lr[0]))
wandb.log({
"iter": (it + 1 + len(dataloader)*self.epoch) * self.cfg.batch_size,
"batch log_ll": log_ll,
})
accum = 0
accumll = 0.0
if self.master_node:
states = {
'net': self.net_module.state_dict(),
'optimizer': self.optimizer.state_dict(),
'epoch': self.epoch + 1,
}
torch.save(states, os.path.join(self.cfg.model_dir, 'checkpoint.pth'))
if self.epoch % self.save_every == 0:
torch.save(states, os.path.join(self.cfg.model_dir, 'checkpoint_{}.pth'.format(self.epoch)))
if self.epoch % self.eval_every == 0:
with torch.no_grad():
metric_tensor = torch.tensor( [epoch_metrics['log_ll'], epoch_metrics['count'] ] )
if self.distributed:
torch.distributed.reduce(metric_tensor, dst=0)
test_epoch_metric_tensor = self.test_marginal()
if self.master_node:
metric_tensor[0] /= metric_tensor[1]
wandb.log({
"epoch": self.epoch,
"train log_ll": metric_tensor[0],
"test marg_ll": test_epoch_metric_tensor[0],
"test log_ll": test_epoch_metric_tensor[1],
})
print("Epoch %u out of %u, train log_ll: %.2f, test marg_ll: %.2f, test log_ll: %.2f" % (self.epoch, self.cfg.n_epochs, metric_tensor[0], test_epoch_metric_tensor[0], test_epoch_metric_tensor[1]))
self.epoch += 1
def test_marginal(self):
print("testing")
self.net.eval()
dataloader = self.test_loader
mode = 'test'
epoch_metrics = {
'marg_ll': 0,
'log_ll': 0,
'count': 0,
}
for batch, (X, y) in enumerate(dataloader):
X = X.cuda(device=self.device_id, non_blocking=True)
X = X.long().squeeze(dim=1) + 1
with torch.no_grad():
mask, _ = self.net_module._sample_mask(X.shape[0], X.device, strategy='none', shiftup=True)
marg_ll = self.net_module.likelihood(X, mask=mask, full=False)
log_ll = self.net_module.likelihood(X, mask=None, full=False)
count = X.shape[0]
epoch_metrics['marg_ll'] += marg_ll * count
epoch_metrics['log_ll'] += log_ll * count
epoch_metrics['count'] += count
with torch.no_grad():
metric_tensor = torch.tensor( [epoch_metrics['marg_ll'], epoch_metrics['log_ll'], epoch_metrics['count'] ] )
if self.distributed:
torch.distributed.reduce(metric_tensor, dst=0)
if self.master_node:
metric_tensor[0] /= metric_tensor[2]
metric_tensor[1] /= metric_tensor[2]
print("%s count %u marg_ll: %.4f log_ll: %.4f" % (mode, metric_tensor[2], metric_tensor[0], metric_tensor[1]))
return metric_tensor