-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
194 lines (160 loc) · 5.94 KB
/
main.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
import argparse
import logging
import os
import pathlib
import warnings
import torch
import torch.distributed as dist
import torch_geometric
from ignite.handlers.param_scheduler import create_lr_scheduler_with_warmup
from torch.cuda.amp import GradScaler
from torch.nn.parallel import DistributedDataParallel
from torch.optim.lr_scheduler import ExponentialLR
from ISubGVQA.datasets.build import build_datasets
from ISubGVQA.models.build import build_model
from ISubGVQA.training.train_loop import train
from ISubGVQA.utils.arg_parser import get_argparser
from ISubGVQA.utils.config import get_config
from ISubGVQA.utils.misc import is_main_process
torch._dynamo.config.cache_size_limit = 64
# https://arxiv.org/abs/2109.08203
torch.manual_seed(3407)
def main(args: argparse.Namespace) -> None:
"""
Main function to run the training and evaluation pipeline.
Args:
args (Namespace): Arguments and hyperparameters for the training and evaluation process.
Initializes:
- Logging configuration if output directory is specified and process is main
- Distributed training setup if specified
- Configuration, datasets, samplers, and dataloaders
- Model and wraps it with DistributedDataParallel if distributed training is specified
- Optimizer, gradient scaler, and learning rate scheduler
Optionally resumes from a checkpoint if specified.
Defines:
- Loss criteria for short answer and node classification tasks
Calls the train function with the initialized components.
Cleans up distributed training setup if specified.
Returns:
None
"""
print(f"torch version: {torch.__version__}")
print(f"torch_geometric version: {torch_geometric.__version__}")
print(
f"os.environ['TOKENIZERS_PARALLELISM']={os.environ['TOKENIZERS_PARALLELISM']}"
)
args.batch_size = args.batch_size * args.scale_factor
args.lr = args.lr * args.scale_factor
print(f"scaled batch size of {args.batch_size}")
print(f"scaled learning rate of {args.lr}")
if args.output_dir and is_main_process():
logging.basicConfig(
filename=os.path.join(args.output_dir, args.log_name),
filemode="w",
format="%(asctime)s: %(levelname)s: [%(filename)s:%(lineno)d]: %(message)s",
level=logging.INFO,
)
warnings.filterwarnings("ignore")
if is_main_process():
logging.info(str(args))
if args.distributed:
local_rank = int(os.environ["LOCAL_RANK"])
torch.cuda.set_device(local_rank)
torch.distributed.init_process_group(backend="nccl")
cfg = get_config(args=args)
# init datasets, samplers, and dataloaders
dataloaders = build_datasets(args=args)
# init model
model = build_model(args=args, cfg=cfg)
if args.distributed:
model.to(local_rank)
model = DistributedDataParallel(
model,
device_ids=[local_rank],
output_device=local_rank,
find_unused_parameters=True,
)
dist.barrier()
torch.cuda.synchronize()
n_parameters = sum(p.numel() for p in model.parameters() if p.requires_grad)
print("number of params:", n_parameters)
# optimizer = torch.optim.AdamW(
# params=model.parameters(),
# lr=args.lr,
# weight_decay=1e-04,
# # samsgrad=True,
# )
optimizer = torch.optim.Adam(
params=model.parameters(),
lr=args.lr,
# weight_decay=1e-04,
# samsgrad=True,
)
gradscaler = GradScaler()
torch_lr_scheduler = ExponentialLR(optimizer=optimizer, gamma=0.98)
lr_scheduler = create_lr_scheduler_with_warmup(
torch_lr_scheduler,
warmup_start_value=1e-6,
warmup_end_value=args.lr,
warmup_duration=10,
)
# optionally resume from a checkpoint
if args.resume:
if os.path.isfile(args.resume):
print("=> loading checkpoint '{}'".format(args.resume))
checkpoint = torch.load(args.resume)
model.load_state_dict(checkpoint["model"])
args = checkpoint["args"]
if not args.evaluate:
if "optimizer" in checkpoint:
optimizer.load_state_dict(checkpoint["optimizer"])
if "lr_scheduler" in checkpoint:
lr_scheduler.load_state_dict(checkpoint["lr_scheduler"])
if "epoch" in checkpoint:
args.start_epoch = checkpoint["epoch"] + 1
else:
print("=> no checkpoint found at '{}'".format(args.resume))
criterion = {
"short_answer": torch.nn.CrossEntropyLoss().to(device=args.device),
"node_class": torch.nn.CrossEntropyLoss().to(device=args.device),
}
train(
args=args,
model=model,
dataloaders=dataloaders,
criterion=criterion,
optimizer=optimizer,
gradscaler=gradscaler,
lr_scheduler=lr_scheduler,
save_model=True,
)
if args.distributed:
cleanup()
return
def get_dist_info():
if dist.is_available() and dist.is_initialized():
rank = dist.get_rank()
world_size = dist.get_world_size()
else:
rank = 0
world_size = 1
return rank, world_size
def setup(rank, world_size):
os.environ["MASTER_ADDR"] = "localhost"
os.environ["MASTER_PORT"] = "12355"
# initialize the process group
dist.init_process_group("gloo", rank=rank, world_size=world_size)
def cleanup():
dist.destroy_process_group()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
"Intrinsic Subgraph Generation for Graph based VQA", parents=[get_argparser()]
)
args = parser.parse_args()
# if "LOCAL_RANK" not in os.environ:
# os.environ["LOCAL_RANK"] = str(args.local_rank)
print(f"Current Working Directory: {os.getcwd()}")
print(args)
if args.output_dir:
pathlib.Path(args.output_dir).mkdir(parents=True, exist_ok=True)
main(args)