comdoleger commited on
Commit
c9445d0
·
verified ·
1 Parent(s): 252dcf8

Upload run.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. run.py +120 -0
run.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
3
+ os.environ["NO_ALBUMENTATIONS_UPDATE"] = "1"
4
+ import sys
5
+ from typing import Union, OrderedDict
6
+ from dotenv import load_dotenv
7
+ # Load the .env file if it exists
8
+ load_dotenv()
9
+
10
+ sys.path.insert(0, os.getcwd())
11
+ # must come before ANY torch or fastai imports
12
+ # import toolkit.cuda_malloc
13
+
14
+ # turn off diffusers telemetry until I can figure out how to make it opt-in
15
+ os.environ['DISABLE_TELEMETRY'] = 'YES'
16
+
17
+ # check if we have DEBUG_TOOLKIT in env
18
+ if os.environ.get("DEBUG_TOOLKIT", "0") == "1":
19
+ # set torch to trace mode
20
+ import torch
21
+ torch.autograd.set_detect_anomaly(True)
22
+ import argparse
23
+ from toolkit.job import get_job
24
+ from toolkit.accelerator import get_accelerator
25
+ from toolkit.print import print_acc, setup_log_to_file
26
+
27
+ accelerator = get_accelerator()
28
+
29
+
30
+ def print_end_message(jobs_completed, jobs_failed):
31
+ if not accelerator.is_main_process:
32
+ return
33
+ failure_string = f"{jobs_failed} failure{'' if jobs_failed == 1 else 's'}" if jobs_failed > 0 else ""
34
+ completed_string = f"{jobs_completed} completed job{'' if jobs_completed == 1 else 's'}"
35
+
36
+ print_acc("")
37
+ print_acc("========================================")
38
+ print_acc("Result:")
39
+ if len(completed_string) > 0:
40
+ print_acc(f" - {completed_string}")
41
+ if len(failure_string) > 0:
42
+ print_acc(f" - {failure_string}")
43
+ print_acc("========================================")
44
+
45
+
46
+ def main():
47
+ parser = argparse.ArgumentParser()
48
+
49
+ # require at lease one config file
50
+ parser.add_argument(
51
+ 'config_file_list',
52
+ nargs='+',
53
+ type=str,
54
+ help='Name of config file (eg: person_v1 for config/person_v1.json/yaml), or full path if it is not in config folder, you can pass multiple config files and run them all sequentially'
55
+ )
56
+
57
+ # flag to continue if failed job
58
+ parser.add_argument(
59
+ '-r', '--recover',
60
+ action='store_true',
61
+ help='Continue running additional jobs even if a job fails'
62
+ )
63
+
64
+ # flag to continue if failed job
65
+ parser.add_argument(
66
+ '-n', '--name',
67
+ type=str,
68
+ default=None,
69
+ help='Name to replace [name] tag in config file, useful for shared config file'
70
+ )
71
+
72
+ parser.add_argument(
73
+ '-l', '--log',
74
+ type=str,
75
+ default=None,
76
+ help='Log file to write output to'
77
+ )
78
+ args = parser.parse_args()
79
+
80
+ if args.log is not None:
81
+ setup_log_to_file(args.log)
82
+
83
+ config_file_list = args.config_file_list
84
+ if len(config_file_list) == 0:
85
+ raise Exception("You must provide at least one config file")
86
+
87
+ jobs_completed = 0
88
+ jobs_failed = 0
89
+
90
+ if accelerator.is_main_process:
91
+ print_acc(f"Running {len(config_file_list)} job{'' if len(config_file_list) == 1 else 's'}")
92
+
93
+ for config_file in config_file_list:
94
+ try:
95
+ job = get_job(config_file, args.name)
96
+ job.run()
97
+ job.cleanup()
98
+ jobs_completed += 1
99
+ except Exception as e:
100
+ print_acc(f"Error running job: {e}")
101
+ jobs_failed += 1
102
+ try:
103
+ job.process[0].on_error(e)
104
+ except Exception as e2:
105
+ print_acc(f"Error running on_error: {e2}")
106
+ if not args.recover:
107
+ print_end_message(jobs_completed, jobs_failed)
108
+ raise e
109
+ except KeyboardInterrupt as e:
110
+ try:
111
+ job.process[0].on_error(e)
112
+ except Exception as e2:
113
+ print_acc(f"Error running on_error: {e2}")
114
+ if not args.recover:
115
+ print_end_message(jobs_completed, jobs_failed)
116
+ raise e
117
+
118
+
119
+ if __name__ == '__main__':
120
+ main()