-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils.py
64 lines (52 loc) · 2.53 KB
/
utils.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
from transformers.optimization import Adafactor, AdafactorSchedule
from transformers import AdamW, get_linear_schedule_with_warmup, get_constant_schedule, get_cosine_schedule_with_warmup, Adafactor
class AdamWOpt(object):
def __init__(self, optimizer, scheduler):
self.optimizer = optimizer
self.scheduler = scheduler
def __getattr__(self, name):
return getattr(self.optimizer, name)
def step(self):
self.optimizer.step()
self.scheduler.step()
def build_t5_finetune_optimizer(opt, model):
optimizer = Adafactor(model.parameters(), scale_parameter=False, relative_step=False, warmup_init=False, lr=1e-3)
return optimizer
def build_t5_pretraining_optimizer(opt, model):
optimizer = Adafactor(model.parameters(), beta1=0, scale_parameter=True, relative_step=True, warmup_init=False, lr=None)
lr_scheduler = AdafactorSchedule(optimizer)
return AdamWOpt(optimizer, lr_scheduler)
def build_t5_optimizer(opt, model):
optimizer = Adafactor(model.parameters(), scale_parameter=False, relative_step=False, warmup_init=False, lr=0.3, weight_decay=1e-5)
return optimizer
def build_optimizer(opt, model):
no_decay = ["bias", "LayerNorm.weight"]
optimizer_grouped_parameters = [
{
"params": [p for n, p in model.named_parameters() if not any(nd in n for nd in no_decay)],
"weight_decay": opt.weight_decay,
},
{
"params": [p for n, p in model.named_parameters() if any(nd in n for nd in no_decay)],
"weight_decay": 0.0,
},
]
optimizer = AdamW(optimizer_grouped_parameters, lr=opt.learning_rate, eps=opt.adam_epsilon, betas=(0.9, 0.98))
scheduler = get_constant_schedule(optimizer)
return AdamWOpt(optimizer, scheduler)
def build_warmup_optimizer(opt, model):
no_decay = ["bias", "LayerNorm.weight"]
optimizer_grouped_parameters = [
{
"params": [p for n, p in model.named_parameters() if not any(nd in n for nd in no_decay)],
"weight_decay": opt.weight_decay,
},
{
"params": [p for n, p in model.named_parameters() if any(nd in n for nd in no_decay)],
"weight_decay": 0.0,
},
]
optimizer = AdamW(optimizer_grouped_parameters, lr=opt.learning_rate, eps=opt.adam_epsilon, betas=(0.9, 0.98))
warmup_step = (opt.num_training_steps * opt.warmup_ratio) if opt.warmup_ratio > 0 else opt.warmup_step
scheduler = get_linear_schedule_with_warmup(optimizer, warmup_step, opt.num_training_steps, -1)
return AdamWOpt(optimizer, scheduler)