-
Notifications
You must be signed in to change notification settings - Fork 2
/
agent.py
211 lines (177 loc) · 9.63 KB
/
agent.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
from multiprocessing.spawn import prepare
from dataset import move_to_cuda
from utils.lib import *
from utils.dist import is_main_process, get_world_size, synchronize, reduce_dict, get_local_rank
from utils.metric_logger import log_dict_to_wandb, setup_wandb
from utils.misc import humanbytes
from utils.deepspeed import get_deepspeed_config, fp32_to_fp16
import deepspeed
from torch import nn
import torch.nn.functional as F
class WarmupLinearLR(T.optim.lr_scheduler._LRScheduler):
def __init__(self, optimizer, max_iter,
min_lr=1e-8, warmup_ratio=0.1, last_epoch=-1):
self.max_iter = max_iter
self.min_lr = min_lr
self.warmup_ratio = warmup_ratio
self.warmup_iters = int(warmup_ratio*max_iter)
super(WarmupLinearLR, self).__init__(optimizer, last_epoch)
def get_lr_factor(self):
tot_step = self.max_iter
warmup_step = self.warmup_iters
step = self.last_epoch
if step<warmup_step: return max(0, step/warmup_step)
elif step>tot_step: step = tot_step
return max(0, (tot_step-step)/(tot_step-warmup_step))
def get_lr(self):
warmup_factor = self.get_lr_factor()
return [max(self.min_lr, base_lr*warmup_factor) for base_lr in self.base_lrs]
class NormSoftmaxLoss(nn.Module):
def __init__(self, temperature=0.05):
super().__init__()
self.temperature = temperature
def forward(self, x):
i_logsm = F.log_softmax(x/self.temperature, dim=1)
j_logsm = F.log_softmax(x.t()/self.temperature, dim=1)
ipos = T.diag(i_logsm)
loss_i = ipos.sum()/len(ipos)
jpos = T.diag(j_logsm)
loss_j = jpos.sum()/len(jpos)
return -loss_i-loss_j
class Agent_Base:
def __init__(self, args, model):
super().__init__()
self.args, self.model = args, model
self.loss_func = T.nn.CrossEntropyLoss(ignore_index=-1).cuda()
self.optzr = self.build_optimizer()
self.lr_scheduler = WarmupLinearLR(self.optzr, args.max_iter)
self.scaler = T.cuda.amp.GradScaler()
self.log = None
if hasattr(model, 'tokzr') and self.model.tokzr is not None: self.tokzr = self.model.tokzr
else: self.tokzr = transformers.AutoTokenizer.from_pretrained(args.tokenizer)
[self.cls_token_id, self.sep_token_id,
self.pad_token_id, self.mask_token_id, self.unk_token_id] = self.tokzr.convert_tokens_to_ids([self.tokzr.cls_token,
self.tokzr.sep_token,
self.tokzr.pad_token,
self.tokzr.mask_token,
self.tokzr.unk_token])
self.true_token_id = self.tokzr.convert_tokens_to_ids(["true"])[0]
self.false_token_id = self.tokzr.convert_tokens_to_ids(["false"])[0]
self.global_step = 0
def log_dict_to_wandb(self, log_dict, step=-1):
if WANDB_ENABLE:
if step==-1: step = self.global_step
log_dict_to_wandb(log_dict, step)
def setup_wandb(self):
if WANDB_ENABLE: setup_wandb(self.args, project=f'violet_{self.args.task}')
def build_optimizer(self):
param_optimizer = list(self.model.named_parameters())
no_decay = ['bias', 'LayerNorm.bias', 'LayerNorm.weight']
decay_param_tp = [(n, p) for n, p in param_optimizer if not any(nd in n for nd in no_decay)]
no_decay_param_tp = [(n, p) for n, p in param_optimizer if any(nd in n for nd in no_decay)]
decay_swin_param = [(n, p) for n, p in decay_param_tp if "swin." in n]
decay_other_param = [(n, p) for n, p in decay_param_tp if "swin." not in n]
no_decay_swin_param = [(n, p) for n, p in no_decay_param_tp if "swin." in n]
no_decay_other_param = [(n, p) for n, p in no_decay_param_tp if "swin." not in n]
weight_decay = self.args.decay
coef_lr = self.args.vis_backbone_lr_mul
lr = self.args.lr
optimizer_grouped_parameters = [{'params': [p for n, p in decay_swin_param],
'weight_decay': weight_decay,
'lr': lr*coef_lr},
{'params': [p for n, p in decay_other_param],
'weight_decay': weight_decay},
{'params': [p for n, p in no_decay_swin_param],
'weight_decay': 0.0,
'lr': lr*coef_lr},
{'params': [p for n, p in no_decay_other_param],
'weight_decay': 0.0}]
optzr = T.optim.AdamW(optimizer_grouped_parameters, lr=lr,
betas=(0.9, 0.98), weight_decay=weight_decay)
return optzr
def reduce_dict(self, data):
return reduce_dict(data)
def reduce_mean(self, v):
world_size = get_world_size()
if world_size<2: return v
else:
v = T.tensor(v).cuda()
DIST.all_reduce(v)
v = v.item()/world_size
return v
def save_training_meta(self):
if is_main_process():
os.makedirs(self.args.path_output, exist_ok=True)
print(self.args)
json.dump(self.args, open(f'{self.args.path_output}/args.json', 'w'), indent=2)
self.save_model(0)
def save_model(self, ep):
if is_main_process():
output_dir = self.args.path_output
os.makedirs(output_dir, exist_ok=True)
model_to_save = self.model.module if hasattr(self.model, 'module') else self.model
state_dict = {k: v.cpu() if isinstance(v, T.Tensor) else v for k, v in model_to_save.state_dict().items()}
T.save(state_dict, f"{output_dir}/ckpt_violet_{self.args.task}_{ep}.pt")
if self.log is not None: json.dump(self.log, open(f"{output_dir}/log.json", 'w'), indent=2)
def log_memory(self, ep=-1, step=-1):
if ep==-1 and step==-1:
step = self.global_step
step_str = f"global step: {step},"
else: step_str = f"ep: {ep}, step: {step},"
memory = humanbytes(T.cuda.max_memory_allocated())
lr_swin = f'{self.optzr.param_groups[0]["lr"]:.2e}'
lr_bert = f'{self.optzr.param_groups[1]["lr"]:.2e}'
self.log_dict_to_wandb({'lr_swin': float(lr_swin)}, step)
self.log_dict_to_wandb({'lr_bert': float(lr_bert)}, step)
return f"{step_str} lr_swin: {lr_swin}, "+f"lr_bert: {lr_bert}, max memory: {memory}"
def prepare_batch(self, batch):
batch = move_to_cuda(batch)
if self.args.deepspeed: batch = fp32_to_fp16(batch)
return batch
def forward_step(self, batch):
if self.args.deepspeed:
if isinstance(batch, dict):
model = self.model.module if hasattr(self.model, 'module') else self.model
named_params = inspect.getargspec(model.forward).args
if "batch" in named_params: out = self.model(batch)
else: out = self.model(**batch)
elif isinstance(batch, tuple): out = self.model(*batch)
else: raise TypeError(f"batch is either dict or tuple, {type(batch)}")
else:
with T.cuda.amp.autocast(enabled=True):
if isinstance(batch, dict):
model = self.model.module if hasattr(self.model, 'module') else self.model
named_params = inspect.getargspec(model.forward).args
if "batch" in named_params: out = self.model(batch)
else: out = self.model(**batch)
elif isinstance(batch, tuple): out = self.model(*batch)
else: raise TypeError(f"batch is either dict or tuple, {type(batch)}")
return out
def backward_step(self, loss):
if self.args.deepspeed:
self.model.backward(loss)
self.model.step()
else:
self.scaler.scale(loss).backward()
if self.args.max_grad_norm > 0:
self.scaler.unscale_(self.optzr)
T.nn.utils.clip_grad_norm_(self.model.parameters(), self.args.max_grad_norm)
self.scaler.step(self.optzr)
self.scaler.update()
self.lr_scheduler.step()
self.optzr.zero_grad()
def prepare_dist_model(self):
if self.args.deepspeed:
config = get_deepspeed_config(self.args)
self.model, self.optzr, _, _ = deepspeed.initialize(config_params=config, model=self.model,
optimizer=self.optzr, lr_scheduler=self.lr_scheduler)
else: self.model = T.nn.parallel.DistributedDataParallel(self.model, device_ids=[get_local_rank()],
output_device=get_local_rank(), find_unused_parameters=True)
def best_epoch(self):
if not hasattr(self, "log"): raise NotImplementedError("no log to find the best epoch")
if "ac_vl" not in self.log or "ac_ts" not in self.log: raise ValueError("calling best_epoch in pretraining, maybe?")
val_index = np.argmax(self.log["ac_vl"])
test_index = np.argmax(self.log["ac_ts"])
val_max = self.log["ac_vl"][val_index]
test_max = self.log["ac_ts"][test_index]
return (val_index, val_max), (test_index, test_max)