-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
41 lines (30 loc) · 1.22 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
import random
import numpy as np
import torch
from transformers import get_linear_schedule_with_warmup, get_constant_schedule_with_warmup
def set_seed(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
def seed_worker(worker_id):
worker_seed = torch.initial_seed() % 2**32
np.random.seed(worker_seed)
random.seed(worker_seed)
def init_scaler(args):
return torch.cuda.amp.GradScaler(enabled=args.use_amp)
def init_optimizer(args, model):
optimizer = torch.optim.AdamW(model.parameters(), lr=args.lr)
return optimizer
def init_scheduler(args, optimizer):
if args.warmup_ratio > 0:
scheduler = get_linear_schedule_with_warmup(optimizer,
num_warmup_steps=args.warmup_steps,
num_training_steps=args.total_steps)
else:
scheduler = get_constant_schedule_with_warmup(optimizer,
num_warmup_steps=args.warmup_steps)
return scheduler