-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy path__main__.py
More file actions
380 lines (362 loc) · 13.9 KB
/
Copy path__main__.py
File metadata and controls
380 lines (362 loc) · 13.9 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import argparse
from datetime import datetime
from gridfm_graphkit.cli import main_cli, benchmark_cli
import subprocess
import os
def is_lsf():
return (
os.environ.get("LSB_JOBID") is not None
and os.environ.get("LSB_MCPU_HOSTS") is not None
and "LSF_ENVDIR" in os.environ # strong LSF indicator
)
def fix_infiniband():
"""Configure NCCL to skip Ethernet-only IB ports on this host."""
ibv = subprocess.run("ibv_devinfo", stdout=subprocess.PIPE, stderr=subprocess.PIPE)
lines = ibv.stdout.decode("utf-8").split("\n")
exclude = ""
for line in lines:
if "hca_id:" in line:
name = line.split(":")[1].strip()
if "\tport:" in line:
port = line.split(":")[1].strip()
if "link_layer:" in line and "Ethernet" in line:
exclude = exclude + f"{name}:{port},"
if exclude:
exclude = "^" + exclude[:-1]
os.environ["NCCL_IB_HCA"] = exclude
def set_env():
"""Populate distributed-training environment variables from LSF metadata."""
# print("Using " + str(torch.cuda.device_count()) + " GPUs---------------------------------------------------------------------")
LSB_MCPU_HOSTS = os.environ[
"LSB_MCPU_HOSTS"
].split(
" ",
) # Parses Node list set by LSF, in format hostname proceeded by number of cores requested
HOST_LIST = LSB_MCPU_HOSTS[::2] # Strips the cores per node items in the list
LSB_JOBID = os.environ[
"LSB_JOBID"
] # Parses Node list set by LSF, in format hostname proceeded by number of cores requested
os.environ["MASTER_ADDR"] = HOST_LIST[
0
] # Sets the MasterNode to thefirst node on the list of hosts
os.environ["MASTER_PORT"] = "5" + LSB_JOBID[-5:-1]
os.environ["NODE_RANK"] = str(
HOST_LIST.index(os.environ["HOSTNAME"]),
) # Uses the list index for node rank, master node rank must be 0
os.environ["NCCL_SOCKET_IFNAME"] = (
"ib,bond" # avoids using docker of loopback interface
)
os.environ["NCCL_IB_CUDA_SUPPORT"] = "1" # Force use of infiniband
def main():
"""Parse CLI arguments and dispatch to the selected GridFM subcommand."""
if is_lsf():
print("Using LSF")
set_env()
fix_infiniband()
parser = argparse.ArgumentParser(
prog="gridfm_graphkit",
description="gridfm-graphkit CLI",
)
subparsers = parser.add_subparsers(dest="command", required=True)
exp_name = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
_compile_kwargs = dict(
type=str,
default=None,
nargs="?",
const="default",
choices=[
"default",
"reduce-overhead",
"max-autotune",
"max-autotune-no-cudagraphs",
],
help="Enable torch.compile with the given mode (omit value for 'default').",
)
_bfloat16_kwargs = dict(
action="store_true",
default=False,
help="Cast model to bfloat16 (model.to(torch.bfloat16)).",
)
_tf32_kwargs = dict(
action="store_true",
default=False,
help="Enable TF32 on Ampere+ GPUs via torch.set_float32_matmul_precision('high').",
)
_mp_context_kwargs = dict(
dest="mp_context",
type=str,
default="spawn",
choices=["spawn", "fork", "forkserver"],
help=(
"Multiprocessing start method for DataLoader workers. "
"'spawn' (default) is safest and works everywhere. "
"'fork' avoids re-importing modules but is unsafe after CUDA init. "
"'forkserver' uses a clean server process but requires file-descriptor passing."
),
)
# ---- TRAIN SUBCOMMAND ----
train_parser = subparsers.add_parser("train", help="Run training")
train_parser.add_argument("--config", type=str, required=True)
train_parser.add_argument("--exp_name", type=str, default=exp_name)
train_parser.add_argument("--run_name", type=str, default="run")
train_parser.add_argument("--log_dir", type=str, default="mlruns")
train_parser.add_argument("--data_path", type=str, default="data")
train_parser.add_argument("--compile", **_compile_kwargs)
train_parser.add_argument("--bfloat16", **_bfloat16_kwargs)
train_parser.add_argument("--tf32", **_tf32_kwargs)
train_parser.add_argument(
"--dataset_wrapper",
type=str,
default=None,
help="Registered name of a dataset wrapper (see DATASET_WRAPPER_REGISTRY), e.g. SharedMemoryCacheDataset",
)
train_parser.add_argument(
"--plugins",
nargs="*",
default=[],
help="Python packages to import for plugin registration, e.g. gridfm_graphkit_ee",
)
train_parser.add_argument(
"--num_workers",
type=int,
default=None,
help="Override data.workers from the YAML config. Use 0 to debug worker crashes.",
)
train_parser.add_argument(
"--dataset_wrapper_cache_dir",
type=str,
default=None,
help="Directory for the dataset wrapper's disk cache. If set, cache is loaded from here when present and saved here after first population.",
)
train_parser.add_argument(
"--profiler",
type=str,
default=None,
choices=["simple", "advanced", "pytorch"],
help="Enable Lightning profiler: 'simple', 'advanced', or 'pytorch'.",
)
train_parser.add_argument(
"--compute_dc_ac_metrics",
action="store_true",
)
train_parser.add_argument(
"--report-performance",
dest="report_performance",
action="store_true",
help="Print the last training epoch time and a single test metric to stdout.",
)
train_parser.add_argument("--mp_context", **_mp_context_kwargs)
# ---- FINETUNE SUBCOMMAND ----
finetune_parser = subparsers.add_parser("finetune", help="Run fine-tuning")
finetune_parser.add_argument("--config", type=str, required=True)
finetune_parser.add_argument("--model_path", type=str, required=True)
finetune_parser.add_argument("--exp_name", type=str, default=exp_name)
finetune_parser.add_argument("--run_name", type=str, default="run")
finetune_parser.add_argument("--log_dir", type=str, default="mlruns")
finetune_parser.add_argument("--data_path", type=str, default="data")
finetune_parser.add_argument("--compile", **_compile_kwargs)
finetune_parser.add_argument("--bfloat16", **_bfloat16_kwargs)
finetune_parser.add_argument("--tf32", **_tf32_kwargs)
finetune_parser.add_argument(
"--dataset_wrapper",
type=str,
default=None,
help="Registered name of a dataset wrapper (see DATASET_WRAPPER_REGISTRY), e.g. SharedMemoryCacheDataset",
)
finetune_parser.add_argument(
"--plugins",
nargs="*",
default=[],
help="Python packages to import for plugin registration, e.g. gridfm_graphkit_ee",
)
finetune_parser.add_argument(
"--num_workers",
type=int,
default=None,
help="Override data.workers from the YAML config. Use 0 to debug worker crashes.",
)
finetune_parser.add_argument(
"--dataset_wrapper_cache_dir",
type=str,
default=None,
help="Directory for the dataset wrapper's disk cache. If set, cache is loaded from here when present and saved here after first population.",
)
finetune_parser.add_argument(
"--profiler",
type=str,
default=None,
choices=["simple", "advanced", "pytorch"],
help="Enable Lightning profiler: 'simple', 'advanced', or 'pytorch'.",
)
finetune_parser.add_argument(
"--compute_dc_ac_metrics",
action="store_true",
)
finetune_parser.add_argument(
"--report-performance",
dest="report_performance",
action="store_true",
help="Print the last training epoch time and a single test metric to stdout.",
)
finetune_parser.add_argument("--mp_context", **_mp_context_kwargs)
# ---- EVALUATE SUBCOMMAND ----
evaluate_parser = subparsers.add_parser(
"evaluate",
help="Evaluate model performance",
)
evaluate_parser.add_argument("--model_path", type=str, default=None)
evaluate_parser.add_argument(
"--normalizer_stats",
type=str,
default=None,
help="Path to normalizer_stats.pt from a training run.",
)
evaluate_parser.add_argument("--config", type=str, required=True)
evaluate_parser.add_argument("--exp_name", type=str, default=exp_name)
evaluate_parser.add_argument("--run_name", type=str, default="run")
evaluate_parser.add_argument("--log_dir", type=str, default="mlruns")
evaluate_parser.add_argument("--data_path", type=str, default="data")
evaluate_parser.add_argument(
"--batch_size",
type=int,
default=None,
help="Override training.batch_size from the YAML config for evaluation.",
)
evaluate_parser.add_argument("--compile", **_compile_kwargs)
evaluate_parser.add_argument("--bfloat16", **_bfloat16_kwargs)
evaluate_parser.add_argument("--tf32", **_tf32_kwargs)
evaluate_parser.add_argument(
"--dataset_wrapper",
type=str,
default=None,
help="Registered name of a dataset wrapper (see DATASET_WRAPPER_REGISTRY), e.g. SharedMemoryCacheDataset",
)
evaluate_parser.add_argument(
"--plugins",
nargs="*",
default=[],
help="Python packages to import for plugin registration, e.g. gridfm_graphkit_ee",
)
evaluate_parser.add_argument(
"--num_workers",
type=int,
default=None,
help="Override data.workers from the YAML config. Use 0 to debug worker crashes.",
)
evaluate_parser.add_argument(
"--dataset_wrapper_cache_dir",
type=str,
default=None,
help="Directory for the dataset wrapper's disk cache. If set, cache is loaded from here when present and saved here after first population.",
)
evaluate_parser.add_argument(
"--profiler",
type=str,
default=None,
choices=["simple", "advanced", "pytorch"],
help="Enable Lightning profiler: 'simple', 'advanced', or 'pytorch'.",
)
evaluate_parser.add_argument(
"--compute_dc_ac_metrics",
action="store_true",
)
evaluate_parser.add_argument(
"--save_output",
action="store_true",
)
evaluate_parser.add_argument("--mp_context", **_mp_context_kwargs)
# ---- PREDICT SUBCOMMAND ----
predict_parser = subparsers.add_parser("predict", help="Run prediction")
predict_parser.add_argument("--model_path", type=str, required=False)
predict_parser.add_argument("--normalizer_stats", type=str, default=None)
predict_parser.add_argument("--config", type=str, required=True)
predict_parser.add_argument("--exp_name", type=str, default=exp_name)
predict_parser.add_argument("--run_name", type=str, default="run")
predict_parser.add_argument("--log_dir", type=str, default="mlruns")
predict_parser.add_argument("--data_path", type=str, default="data")
predict_parser.add_argument(
"--batch_size",
type=int,
default=None,
help="Override training.batch_size from the YAML config for prediction.",
)
predict_parser.add_argument(
"--dataset_wrapper",
type=str,
default=None,
help="Registered name of a dataset wrapper (see DATASET_WRAPPER_REGISTRY), e.g. SharedMemoryCacheDataset",
)
predict_parser.add_argument(
"--plugins",
nargs="*",
default=[],
help="Python packages to import for plugin registration, e.g. gridfm_graphkit_ee",
)
predict_parser.add_argument(
"--num_workers",
type=int,
default=None,
help="Override data.workers from the YAML config. Use 0 to debug worker crashes.",
)
predict_parser.add_argument(
"--dataset_wrapper_cache_dir",
type=str,
default=None,
help="Directory for the dataset wrapper's disk cache. If set, cache is loaded from here when present and saved here after first population.",
)
predict_parser.add_argument("--output_path", type=str, default="data")
predict_parser.add_argument("--compile", **_compile_kwargs)
predict_parser.add_argument("--bfloat16", **_bfloat16_kwargs)
predict_parser.add_argument("--tf32", **_tf32_kwargs)
predict_parser.add_argument(
"--profiler",
type=str,
default=None,
choices=["simple", "advanced", "pytorch"],
)
predict_parser.add_argument("--mp_context", **_mp_context_kwargs)
# ---- BENCHMARK SUBCOMMAND ----
benchmark_parser = subparsers.add_parser(
"benchmark",
help="Benchmark train-dataloader iteration speed",
)
benchmark_parser.add_argument("--config", type=str, required=True)
benchmark_parser.add_argument("--data_path", type=str, default="data")
benchmark_parser.add_argument(
"--epochs",
type=int,
default=3,
help="Number of epochs to iterate through the train dataloader.",
)
benchmark_parser.add_argument(
"--dataset_wrapper",
type=str,
default=None,
help="Registered name of a dataset wrapper (see DATASET_WRAPPER_REGISTRY), e.g. SharedMemoryCacheDataset",
)
benchmark_parser.add_argument(
"--dataset_wrapper_cache_dir",
type=str,
default=None,
help="Directory for the dataset wrapper's disk cache.",
)
benchmark_parser.add_argument(
"--num_workers",
type=int,
default=None,
help="Override data.workers from the YAML config.",
)
benchmark_parser.add_argument(
"--plugins",
nargs="*",
default=[],
help="Python packages to import for plugin registration.",
)
benchmark_parser.add_argument("--mp_context", **_mp_context_kwargs)
args = parser.parse_args()
if args.command == "benchmark":
benchmark_cli(args)
else:
main_cli(args)
if __name__ == "__main__":
main()