-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
73 lines (59 loc) · 2.03 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
import logging
import wandb
import os
import sys
from typing import TYPE_CHECKING
from .manager import Manager
from .utils import wandb_alert, pdb_wrap, fork_detached
if TYPE_CHECKING:
from .config.root import RootConfig
_log = logging.getLogger(__name__)
def set_time_format() -> None:
root_logger = logging.getLogger()
for handler in root_logger.handlers:
formatter = handler.formatter
if formatter is not None:
formatter.default_time_format = '%m-%d %H:%M:%S'
def main(cfg: 'RootConfig') -> None:
set_time_format()
with pdb_wrap('Manager.__init__', post_mortem=cfg.pdb):
manager = Manager(cfg)
with pdb_wrap('manager.dry_run', post_mortem=cfg.pdb):
manager.dry_run()
if cfg.typ == 'dry':
return
with pdb_wrap('manager.init', post_mortem=cfg.pdb):
manager.init()
try:
with pdb_wrap('manager.run', post_mortem=cfg.pdb):
manager.run()
except KeyboardInterrupt:
_log.info('Saving...')
manager.save()
raise
finally:
manager.wandb_upload_metrics()
reduced = manager.pickle_reduced()
manager.clear()
if wandb.run is not None:
match sys.exc_info()[1]:
case None:
title = 'Finished'
level = wandb.AlertLevel.INFO
exit_code = None
case KeyboardInterrupt():
title = 'Killed'
level = wandb.AlertLevel.WARN
exit_code = 255
case _:
title = 'Crashed'
level = wandb.AlertLevel.ERROR
exit_code = 1
wandb_alert(title, wandb.run.id, level)
wandb.run.finish(exit_code)
_log.info('Command: %s', ' '.join(sys.orig_argv).replace('[', '"[').replace(']', ']"'))
_log.info('Logdir: %s', os.getcwd())
if cfg.fork_final:
fork_detached(manager.final, reduced)
else:
manager.final(reduced)