-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathtrain.py
More file actions
1339 lines (1201 loc) · 61.1 KB
/
Copy pathtrain.py
File metadata and controls
1339 lines (1201 loc) · 61.1 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
import os
import ast
import sys
import shutil
import glob
import argparse
import functools
import numpy as np
import math
import copy
import torch
from torch.utils.data import DataLoader
from weaver.utils.logger import _logger, _configLogger
from weaver.utils.dataset import SimpleIterDataset
from weaver.utils.import_tools import import_module
from weaver.utils.data.eval_utils import _register_funcs
from weaver.utils.nn.tools import unwrap_model
# fmt: off
parser = argparse.ArgumentParser()
parser.add_argument('--run-mode', type=functools.partial(str.split, sep=','), default='train,val,test',
help='comma-separated list of the steps (train|val|test) to run, e.g., `train,test`')
parser.add_argument('--regression-mode', action=argparse.BooleanOptionalAction, default=False,
help='run in regression mode if this flag is set; otherwise run in classification mode')
parser.add_argument('-c', '--data-config', type=str,
help='data config YAML file')
parser.add_argument('--data-config-val', type=str, default=None,
help='data config YAML file for validation set')
parser.add_argument('--data-config-test', type=str, default=None,
help='data config YAML file for test set')
parser.add_argument('--extra-selection-train', type=str, default=None,
help='Additional selection requirement for training, will modify `selection` to `(selection) & (extra)` on-the-fly')
parser.add_argument('--extra-selection-val', type=str, default=None,
help='Additional selection requirement for validation, will modify `selection` to `(selection) & (extra)` on-the-fly')
parser.add_argument('--extra-selection-test', type=str, default=None,
help='Additional test-time selection requirement, will modify `test_time_selection` to `(test_time_selection) & (extra)` on-the-fly')
parser.add_argument('-i', '--data-train', nargs='*', default=[],
help='training files; supported syntax:'
' (a) plain list, `--data-train /path/to/a/* /path/to/b/*`;'
' (b) (named) groups [Recommended], `--data-train a:/path/to/a/* b:/path/to/b/*`,'
' the file splitting (for each dataloader worker) will be performed per group,'
' and then mixed together, to ensure a uniform mixing from all groups for each worker.'
)
parser.add_argument('-l', '--data-val', nargs='*', default=[],
help='validation files; when not set, will use training files and split by `--train-val-split`')
parser.add_argument('-t', '--data-test', nargs='*', default=[],
help='testing files; supported syntax:'
' (a) plain list, `--data-test /path/to/a/* /path/to/b/*`;'
' (b) keyword-based, `--data-test a:/path/to/a/* b:/path/to/b/*`, will produce output_a, output_b;'
' (c) split output per N input files, `--data-test a%%10:/path/to/a/*`, will split per 10 input files')
parser.add_argument('--data-fraction', type=float, default=1,
help='fraction of events to load from each file; for training, the events are randomly selected for each epoch')
parser.add_argument('--data-fraction-val', type=float, default=None,
help='--data-fraction for the validation data loader; defaults to --data-fraction if not set')
parser.add_argument('--file-fraction', type=float, default=1,
help='fraction of files to load; for training, the files are randomly selected for each epoch')
parser.add_argument('--fetch-by-files', action=argparse.BooleanOptionalAction, default=False,
help='When enabled, will load all events from a small number (set by ``--fetch-step``) of files for each data fetching. '
'Otherwise (default), load a small fraction of events from all files each time, which helps reduce variations in the sample composition.')
parser.add_argument('--fetch-step', type=float, default=0.01,
help='fraction of events to load each time from every file (when ``--fetch-by-files`` is disabled); '
'Or: number of files to load each time (when ``--fetch-by-files`` is enabled). Shuffling & sampling is done within these events, so set a large enough value.')
parser.add_argument('--fetch-step-val', type=float, default=None,
help='--fetch-step for the validation data loader.')
parser.add_argument('--data-split-num', type=int, default=1,
help='for each dataloader worker, split its dataset further into N parts when loading a certain fraction of each file. Setting N > 1 can reduce the workers\' memory usage.')
parser.add_argument('--data-split-val', type=int, default=None,
help='--data-split-num for the validation data loader.')
parser.add_argument('--in-memory', action=argparse.BooleanOptionalAction, default=False,
help='load the whole dataset (and perform the preprocessing) only once and keep it in memory for the entire run')
parser.add_argument('--in-memory-val', action=argparse.BooleanOptionalAction, default=None,
help='--in-memory for the validation data loader')
parser.add_argument('--train-val-split', type=float, default=0.8,
help='training/validation split fraction')
parser.add_argument('--no-remake-weights', action='store_true', default=False,
help='do not remake weights for sampling (reweighting), use existing ones in the previous auto-generated data config YAML file')
parser.add_argument('--demo', action=argparse.BooleanOptionalAction, default=False,
help='quickly test the setup by running over only a small number of events')
parser.add_argument('--lr-finder', type=str, default=None,
help='run learning rate finder instead of the actual training; format: ``start_lr, end_lr, num_iters``')
parser.add_argument('--tensorboard', type=str, default=None,
help='create a tensorboard summary writer with the given comment')
parser.add_argument('--tensorboard-custom-fn', type=str, default=None,
help='the path of the python script containing a user-specified function `get_tensorboard_custom_fn`, '
'to display custom information per mini-batch or per epoch, during the training, validation or test.')
parser.add_argument('--custom-functions', type=str,
help='python file implementing extra functions for data loading and pre-processing')
parser.add_argument('-n', '--network-config', type=str,
help='network architecture configuration file')
parser.add_argument('-o', '--network-option', nargs=2, action='append', default=[],
help='options to pass to the model class constructor, e.g., `--network-option use_counts False`')
parser.add_argument('-m', '--model-prefix', type=str, default='models/{auto}/network',
help='path to save or load the model; for training, this will be used as a prefix, so model snapshots '
'will saved to `{model_prefix}_epoch-%%d_state.pt` after each epoch, and the one with the best '
'validation metric to `{model_prefix}_best_epoch_state.pt`; for testing, this should be the full path '
'including the suffix, otherwise the one with the best validation metric will be used; '
'for training, `{auto}` can be used as part of the path to auto-generate a name, '
'based on the timestamp and network configuration')
parser.add_argument('--load-model-weights', type=str, default=None,
help='initialize model with pre-trained weights')
parser.add_argument('--exclude-model-weights', type=str, default=None,
help='comma-separated regex to exclude matched weights from being loaded, e.g., `a.fc..+,b.fc..+`')
parser.add_argument('--freeze-model-weights', type=str, default=None,
help='comma-separated regex to freeze matched weights from being updated in the training, e.g., `a.fc..+,b.fc..+`')
parser.add_argument('--num-epochs', type=int, default=20,
help='number of epochs')
parser.add_argument('--steps-per-epoch', type=int, default=None,
help='number of steps (iterations) per epochs; '
'if neither of `--steps-per-epoch` or `--samples-per-epoch` is set, each epoch will run over all loaded samples')
parser.add_argument('--steps-per-epoch-val', type=int, default=None,
help='number of steps (iterations) per epochs for validation; '
'if neither of `--steps-per-epoch-val` or `--samples-per-epoch-val` is set, each epoch will run over all loaded samples')
parser.add_argument('--samples-per-epoch', type=int, default=None,
help='number of samples per epochs; '
'if neither of `--steps-per-epoch` or `--samples-per-epoch` is set, each epoch will run over all loaded samples')
parser.add_argument('--samples-per-epoch-val', type=int, default=None,
help='number of samples per epochs for validation; '
'if neither of `--steps-per-epoch-val` or `--samples-per-epoch-val` is set, each epoch will run over all loaded samples')
parser.add_argument('--optimizer', type=str, default='ranger',
help='optimizer for the training; For any PyTorch built-in optimizer in torch.optim, use the case-sensitive class name, e.g., AdamW')
parser.add_argument('-p', '--optimizer-option', nargs=2, action='append', default=[],
help='options to pass to the optimizer class constructor, e.g., `--optimizer-option weight_decay 1e-4`')
parser.add_argument('--lr-scheduler', type=str, default='flat+decay',
choices=['none', 'steps', 'flat+decay', 'flat+linear', 'flat+cos', 'warmup+cos', 'one-cycle'],
help='learning rate scheduler')
parser.add_argument('--warmup-steps', type=float, default=0.25,
help='number of warm-up steps (or fraction of the total steps if <1), only valid for `flat+linear` and `flat+cos` lr schedulers')
parser.add_argument('--load-epoch', type=int, default=None,
help='used to resume interrupted training, load model and optimizer state saved in the `epoch-%%d_state.pt` and `epoch-%%d_optimizer.pt` files')
parser.add_argument('--start-lr', type=float, default=5e-3,
help='start learning rate')
parser.add_argument('--batch-size', type=int, default=128,
help='batch size')
parser.add_argument('--batch-size-val', type=int, default=None,
help='batch size for validation dataset')
parser.add_argument('--batch-size-test', type=int, default=None,
help='batch size for test dataset')
parser.add_argument('--use-amp', action=argparse.BooleanOptionalAction, default=False,
help='use mixed precision training (fp16)')
parser.add_argument('--amp-dtype', type=str, default='bf16', choices=['fp16', 'bf16'],
help='dtype for mixed precision training (fp16 or bf16)')
parser.add_argument('--compile', action=argparse.BooleanOptionalAction, default=False,
help='use torch.compile')
parser.add_argument('--compiler-option', nargs=2, action='append', default=[],
help='options to pass to torch.compile, e.g., `--compiler-option dynamic False`')
parser.add_argument('--compile-optimizer', action=argparse.BooleanOptionalAction, default=False,
help='use torch.compile to compile the optimizer `step` (experimental); reuses `--compiler-option`. '
'The learning rate is wrapped in a Tensor so that LR-scheduler updates do not trigger '
'recompilation. Works best with foreach/fused-capable optimizers (e.g. AdamW)')
parser.add_argument('--gpus', type=str, default='0',
help='device for the training/testing; to use CPU, set to empty string (""); to use multiple gpu, set it as a comma separated list, e.g., `1,2,3,4`')
parser.add_argument('--predict-gpus', type=str, default=None,
help='device for the testing; to use CPU, set to empty string (""); to use multiple gpu, set it as a comma separated list, e.g., `1,2,3,4`; if not set, use the same as `--gpus`')
parser.add_argument('--num-workers', type=int, default=1,
help='number of threads to load the dataset; memory consumption and disk access load increases (~linearly) with this numbers')
parser.add_argument('--prefetch-factor', type=int, default=None,
help='number of batches loaded in advance by each DataLoader worker; increase for I/O-bound workloads')
parser.add_argument('--predict', action=argparse.BooleanOptionalAction, default=False,
help='run prediction instead of training')
parser.add_argument('--predict-output', type=str,
help='path to save the prediction output, support `.root` and `.parquet` format')
parser.add_argument('--export-onnx', type=str, default=None,
help='export the PyTorch model to ONNX model and save it at the given path (path must ends w/ .onnx); '
'needs to set `--data-config`, `--network-config`, and `--model-prefix` (requires the full model path)')
parser.add_argument('--onnx-opset', type=int, default=15,
help='ONNX opset version.')
parser.add_argument('--io-test', action=argparse.BooleanOptionalAction, default=False,
help='test throughput of the dataloader')
parser.add_argument('--copy-inputs', action=argparse.BooleanOptionalAction, default=False,
help='copy input files to the current dir (can help to speed up dataloading when running over remote files, e.g., from EOS)')
parser.add_argument('--log-file', type=str, dest='log', default='',
help='path to the log file; `{auto}` can be used as part of the path to auto-generate a name, based on the timestamp and network configuration')
parser.add_argument('--print', action=argparse.BooleanOptionalAction, default=False,
help='do not run training/prediction but only print model information, e.g., FLOPs and number of parameters of a model')
parser.add_argument('--profile', action=argparse.BooleanOptionalAction, default=False,
help='run the profiler')
parser.add_argument('--backend', type=str, choices=['gloo', 'nccl', 'mpi'], default=None,
help='backend for distributed training')
parser.add_argument('--cross-validation', type=str, default=None,
help='enable k-fold cross validation; input format: `variable_name%%k`')
parser.add_argument('--auto-clean', action=argparse.BooleanOptionalAction, default=False,
help='automatically remove the previous checkpoints, keeping only the last epoch and the best epoch')
# fmt: on
def to_filelist(args, mode="train"):
if mode == "train":
flist = args.data_train
elif mode == "val":
flist = args.data_val
else:
raise NotImplementedError("Invalid mode %s" % mode)
# keyword-based: 'a:/path/to/a b:/path/to/b'
file_dict = {}
for f in flist:
if ":" in f:
name, fp = f.split(":")
else:
name, fp = "_", f
files = glob.glob(fp)
if name in file_dict:
file_dict[name] += files
else:
file_dict[name] = files
# sort files
for name, files in file_dict.items():
file_dict[name] = sorted(files)
if args.local_rank is not None:
if mode == "train" or mode == "val":
local_world_size = int(os.environ["LOCAL_WORLD_SIZE"])
new_file_dict = {}
for name, files in file_dict.items():
new_files = files[args.local_rank :: local_world_size]
assert len(new_files) > 0
np.random.shuffle(new_files)
new_file_dict[name] = new_files
file_dict = new_file_dict
if args.copy_inputs:
import tempfile
tmpdir = tempfile.mkdtemp()
if os.path.exists(tmpdir):
shutil.rmtree(tmpdir)
new_file_dict = {name: [] for name in file_dict}
for name, files in file_dict.items():
for src in files:
dest = os.path.join(tmpdir, src.lstrip("/"))
if not os.path.exists(os.path.dirname(dest)):
os.makedirs(os.path.dirname(dest), exist_ok=True)
shutil.copy2(src, dest)
_logger.info("Copied file %s to %s" % (src, dest))
new_file_dict[name].append(dest)
if len(files) != len(new_file_dict[name]):
_logger.error(
"Only %d/%d files copied for %s file group %s", len(new_file_dict[name]), len(files), mode, name
)
file_dict = new_file_dict
filelist = sum(file_dict.values(), [])
assert len(filelist) == len(set(filelist))
return file_dict, filelist
def train_load(args):
"""
Loads the training data.
:param args:
:return: train_loader, train_data_config, val_loader, val_data_config
"""
train_file_dict, train_files = to_filelist(args, "train")
if args.data_val:
val_file_dict, val_files = to_filelist(args, "val")
train_range = val_range = (0, 1)
else:
val_file_dict, val_files = train_file_dict, train_files
train_range = (0, args.train_val_split)
val_range = (args.train_val_split, 1)
_logger.info("Using %d files for training, range: %s" % (len(train_files), str(train_range)))
_logger.info("Using %d files for validation, range: %s" % (len(val_files), str(val_range)))
if args.demo:
train_files = train_files[:20]
val_files = val_files[:20]
train_file_dict = {"_": train_files}
val_file_dict = {"_": val_files}
_logger.info(train_files)
_logger.info(val_files)
args.data_fraction = 0.1
args.data_split_num = 1
args.fetch_step = 0.002
if args.in_memory:
if args.steps_per_epoch is None:
raise RuntimeError("Must set --steps-per-epoch when using --in-memory!")
if args.fetch_by_files or args.fetch_step < 1:
_logger.warning(
"Running with --in-memory, but --fetch-step is set to a value below 1 (or --fetch-by-files is set). "
"This means only a fraction of data will be loaded throughout the training.",
color="bold",
)
if args.in_memory_val:
if args.steps_per_epoch_val is None:
raise RuntimeError("Must set --steps-per-epoch-val when using --in-memory-val!")
if args.fetch_by_files or args.fetch_step_val < 1:
_logger.warning(
"Running with --in-memory-val, but --fetch-step-val is set to a value below 1 (or --fetch-by-files is set). "
"This means only a fraction of data will be loaded throughout the validation.",
color="bold",
)
train_data = SimpleIterDataset(
train_file_dict,
args.data_config,
batch_size=args.batch_size,
for_training=True,
extra_selection=args.extra_selection_train,
remake_weights=not args.no_remake_weights,
load_range_and_fraction=(train_range, args.data_fraction, args.data_split_num),
file_fraction=args.file_fraction,
fetch_by_files=args.fetch_by_files,
fetch_step=args.fetch_step,
infinity_mode=args.steps_per_epoch is not None,
in_memory=args.in_memory,
name="train" + ("" if args.local_rank is None else "_rank%d" % args.local_rank),
)
val_data = SimpleIterDataset(
val_file_dict,
args.data_config_val,
batch_size=args.batch_size_val,
for_training=True,
extra_selection=args.extra_selection_val,
load_range_and_fraction=(val_range, args.data_fraction_val if args.data_fraction_val is not None else args.data_fraction, args.data_split_val),
file_fraction=args.file_fraction,
fetch_by_files=args.fetch_by_files,
fetch_step=args.fetch_step_val,
infinity_mode=args.steps_per_epoch_val is not None,
in_memory=args.in_memory_val,
name="val" + ("" if args.local_rank is None else "_rank%d" % args.local_rank),
)
num_workers_train = min(args.num_workers, max(1, int(len(train_files) * args.file_fraction)))
num_workers_val = min(args.num_workers, max(1, int(len(val_files) * args.file_fraction)))
train_loader = DataLoader(
train_data,
batch_size=args.batch_size,
drop_last=True,
pin_memory=True,
num_workers=num_workers_train,
persistent_workers=num_workers_train > 0,
prefetch_factor=args.prefetch_factor if num_workers_train > 0 else None,
)
val_loader = DataLoader(
val_data,
batch_size=args.batch_size_val,
drop_last=True,
pin_memory=True,
num_workers=num_workers_val,
persistent_workers=num_workers_val > 0,
prefetch_factor=args.prefetch_factor if num_workers_val > 0 else None,
)
train_data_config = train_data.config
val_data_config = val_data.config
return train_loader, train_data_config, val_loader, val_data_config
def test_load(args):
"""
Loads the test data.
:param args:
:return: test_loaders, test_data_config
"""
# keyword-based --data-test: 'a:/path/to/a b:/path/to/b'
# split --data-test: 'a%10:/path/to/a/*'
file_dict = {}
split_dict = {}
for f in args.data_test:
if ":" in f:
name, fp = f.split(":")
if "%" in name:
name, split = name.split("%")
split_dict[name] = int(split)
else:
name, fp = "", f
files = glob.glob(fp)
if name in file_dict:
file_dict[name] += files
else:
file_dict[name] = files
# sort files
for name, files in file_dict.items():
file_dict[name] = sorted(files)
# apply splitting
for name, split in split_dict.items():
files = file_dict.pop(name)
for i in range((len(files) + split - 1) // split):
file_dict[f"{name}_{i}"] = files[i * split : (i + 1) * split]
def get_test_loader(name):
filelist = file_dict[name]
_logger.info("Running on test file group %s with %d files:\n...%s", name, len(filelist), "\n...".join(filelist))
num_workers = min(args.num_workers, len(filelist))
test_data = SimpleIterDataset(
{name: filelist},
args.data_config_test,
for_training=False,
extra_selection=args.extra_selection_test,
load_range_and_fraction=((0, 1), args.data_fraction, args.data_split_num),
fetch_by_files=True,
fetch_step=1,
name="test_" + name,
)
test_loader = DataLoader(
test_data,
num_workers=num_workers,
batch_size=args.batch_size_test,
drop_last=False,
pin_memory=True,
persistent_workers=num_workers > 0,
prefetch_factor=args.prefetch_factor if num_workers > 0 else None,
)
return test_loader
test_loaders = {name: functools.partial(get_test_loader, name) for name in file_dict}
test_data_config = SimpleIterDataset({}, args.data_config_test, for_training=False).config
return test_loaders, test_data_config
def onnx(args):
"""
Saving model as ONNX.
:param args:
:return:
"""
assert args.export_onnx.endswith(".onnx")
model_path = args.model_prefix
_logger.info("Exporting model %s to ONNX" % model_path)
from weaver.utils.dataset import DataConfig
data_config = DataConfig.load(args.data_config, load_observers=False, load_reweight_info=False)
model, model_info, *_ = model_setup(args, data_config)
model.load_state_dict(torch.load(model_path, map_location="cpu"))
model = model.cpu()
model.eval()
if not os.path.dirname(args.export_onnx):
args.export_onnx = os.path.join(os.path.dirname(model_path), args.export_onnx)
os.makedirs(os.path.dirname(args.export_onnx), exist_ok=True)
# Build the dummy inputs used to trace the model. Tracing through an axis of
# size 1 makes the exporter specialize/bake that dimension into the graph,
# even when it is declared dynamic -- so the exported model would then only
# accept that exact size. To keep declared-dynamic axes truly dynamic, trace
# with size 2 for any dynamic axis whose configured size is 1 (e.g. the batch
# dimension, which is typically 1 in the network config's `input_shapes`).
dynamic_axes = model_info.get("dynamic_axes", None)
def _trace_shape(name, shape):
shape = list(shape)
axes = (dynamic_axes or {}).get(name, None)
if axes is not None:
# `dynamic_axes` entries may be a dict {axis: name} or a list [axis, ...]
axis_indices = axes.keys() if isinstance(axes, dict) else axes
for ax in axis_indices:
if shape[ax] == 1:
shape[ax] = 2
return shape
inputs = tuple(
torch.ones(_trace_shape(k, model_info["input_shapes"][k]), dtype=torch.float32)
for k in model_info["input_names"]
)
export_kwargs = dict(
input_names=model_info["input_names"],
output_names=model_info["output_names"],
dynamic_axes=dynamic_axes,
opset_version=args.onnx_opset,
)
# Use the legacy TorchScript-based exporter: the models' ONNX code paths
# (e.g. `for_onnx` pairwise embedding, manual attention masks) are written
# for tracing, and the newer TorchDynamo exporter (the default since recent
# PyTorch versions) cannot handle their data-dependent shapes and adds an
# `onnxscript` dependency. The `dynamo` kwarg only exists on newer PyTorch,
# so set it conditionally for backward compatibility.
import inspect
if "dynamo" in inspect.signature(torch.onnx.export).parameters:
export_kwargs["dynamo"] = False
torch.onnx.export(model, inputs, args.export_onnx, **export_kwargs)
_logger.info("ONNX model saved to %s", args.export_onnx)
preprocessing_json = os.path.join(os.path.dirname(args.export_onnx), "preprocess.json")
data_config.export_json(preprocessing_json)
_logger.info("Preprocessing parameters saved to %s", preprocessing_json)
def flops(model, model_info, device="cpu"):
"""
Count FLOPs and params.
:param args:
:param model:
:param model_info:
:return:
"""
from weaver.utils.flops_counter import get_model_complexity_info
from torch.utils.flop_counter import FlopCounterMode
import copy
model = copy.deepcopy(model).to(device)
model.eval()
inputs = tuple(
torch.ones(model_info["input_shapes"][k], dtype=torch.float32, device=device) for k in model_info["input_names"]
)
macs, params = get_model_complexity_info(model, inputs, as_strings=True, print_per_layer_stat=True, verbose=True)
_logger.info("{:<30} {:<8}".format("Computational complexity: ", macs))
_logger.info("{:<30} {:<8}".format("Number of parameters: ", params))
flop_counter = FlopCounterMode(display=True)
with flop_counter:
_ = model(*inputs)
total_flops = flop_counter.get_total_flops()
def format_number(n):
for unit in ["", "K", "M", "G", "T", "P"]:
if abs(n) < 1000:
return f"{n:.2f} {unit}"
n /= 1000
return f"{n:.2f}E"
_logger.info("{:<30} {:<8}".format("Total FLOPs: ", format_number(total_flops)))
def profile(args, model, model_info, device):
"""
Profile.
:param model:
:param model_info:
:return:
"""
import copy
from torch.profiler import profile, record_function, ProfilerActivity
model = copy.deepcopy(model)
model = model.to(device)
model.eval()
inputs = tuple(
torch.ones((args.batch_size,) + model_info["input_shapes"][k][1:], dtype=torch.float32).to(device)
for k in model_info["input_names"]
)
for x in inputs:
print(x.shape, x.device)
def trace_handler(p):
output = p.key_averages().table(sort_by="self_cuda_time_total", row_limit=50)
print(output)
p.export_chrome_trace("/tmp/trace_" + str(p.step_num) + ".json")
with profile(
activities=[ProfilerActivity.CPU, ProfilerActivity.CUDA],
schedule=torch.profiler.schedule(wait=2, warmup=2, active=6, repeat=2),
on_trace_ready=trace_handler,
) as p:
for idx in range(100):
model(*inputs)
p.step()
def init_opt(args, model, **optimizer_options):
# When compiling the optimizer step, the learning rate must be a Tensor: torch.compile guards on the
# value of a Python-float lr and would recompile every time the LR scheduler changes it (every step for
# the per-step schedulers). A Tensor lr is updated in-place by the schedulers, so its identity is stable.
compile_optimizer = getattr(args, "compile_optimizer", False)
if compile_optimizer:
lr_device = next((p.device for p in model.parameters()), None)
start_lr = torch.tensor(args.start_lr, device=lr_device)
else:
start_lr = args.start_lr
names_lr_mult = []
if "weight_decay" in optimizer_options or "lr_mult" in optimizer_options:
# https://github.com/rwightman/pytorch-image-models/blob/master/timm/optim/optim_factory.py#L31
import re
decay, no_decay = {}, {}
names_no_decay = []
wd_skip_list = {"pos_embed", "cls_token", "mask_token", "register_token"}
for name, param in model.named_parameters():
if not param.requires_grad:
continue # frozen weights
if (
len(param.shape) == 1
or name.endswith(".bias")
or (name.split(".")[-1] in wd_skip_list)
or (hasattr(model, "no_weight_decay") and name in model.no_weight_decay())
):
no_decay[name] = param
names_no_decay.append(name)
else:
decay[name] = param
decay_1x, no_decay_1x = [], []
decay_mult, no_decay_mult = [], []
mult_factor = 1
if "lr_mult" in optimizer_options:
pattern, mult_factor = optimizer_options.pop("lr_mult")
for name, param in decay.items():
if re.match(pattern, name):
decay_mult.append(param)
names_lr_mult.append(name)
else:
decay_1x.append(param)
for name, param in no_decay.items():
if re.match(pattern, name):
no_decay_mult.append(param)
names_lr_mult.append(name)
else:
no_decay_1x.append(param)
assert len(decay_1x) + len(decay_mult) == len(decay)
assert len(no_decay_1x) + len(no_decay_mult) == len(no_decay)
else:
decay_1x, no_decay_1x = list(decay.values()), list(no_decay.values())
wd = optimizer_options.get("weight_decay", 0.0)
parameters = [
{"params": no_decay_1x, "weight_decay": 0.0},
{"params": decay_1x, "weight_decay": wd},
{"params": no_decay_mult, "weight_decay": 0.0, "lr": start_lr * mult_factor},
{"params": decay_mult, "weight_decay": wd, "lr": start_lr * mult_factor},
]
_logger.info("Parameters excluded from weight decay:\n - %s", "\n - ".join(names_no_decay))
if len(names_lr_mult):
_logger.info("Parameters with lr multiplied by %s:\n - %s", mult_factor, "\n - ".join(names_lr_mult))
else:
parameters = model.parameters()
clip_grad_norm = optimizer_options.pop("clip_grad_norm", float("inf"))
if args.optimizer.lower() == "ranger":
from weaver.utils.nn.optimizer.ranger import Ranger
opt = Ranger(parameters, lr=start_lr, **optimizer_options)
elif args.optimizer.lower() == "adam":
opt = torch.optim.Adam(parameters, lr=start_lr, **optimizer_options)
elif args.optimizer.lower() == "adamw":
if "betas" not in optimizer_options:
optimizer_options["betas"] = (0.95, 0.999)
if "weight_decay" not in optimizer_options:
optimizer_options["weight_decay"] = 0
_logger.info(f"Using AdamW optimizer w/ options: {str(optimizer_options)}")
opt = torch.optim.AdamW(parameters, lr=start_lr, **optimizer_options)
elif args.optimizer.lower() == "radam":
opt = torch.optim.RAdam(parameters, lr=start_lr, **optimizer_options)
else:
opt = getattr(torch.optim, args.optimizer)(parameters, lr=start_lr, **optimizer_options)
if args.load_epoch is not None:
load_checkpoint(args, model, opt)
opt._clip_grad_norm = clip_grad_norm
scheduler = None
if args.lr_finder is None:
if args.lr_scheduler == "steps":
lr_step = round(args.num_epochs / 3)
scheduler = torch.optim.lr_scheduler.MultiStepLR(
opt,
milestones=[lr_step, 2 * lr_step],
gamma=0.1,
last_epoch=-1 if args.load_epoch is None else args.load_epoch,
)
elif args.lr_scheduler == "flat+decay":
num_decay_epochs = max(1, int(args.num_epochs * 0.3))
milestones = list(range(args.num_epochs - num_decay_epochs, args.num_epochs))
gamma = 0.01 ** (1.0 / num_decay_epochs)
if len(names_lr_mult):
def get_lr(epoch):
return gamma ** max(0, epoch - milestones[0] + 1) # noqa
scheduler = torch.optim.lr_scheduler.LambdaLR(
opt,
(lambda _: 1, lambda _: 1, get_lr, get_lr),
last_epoch=-1 if args.load_epoch is None else args.load_epoch,
)
else:
scheduler = torch.optim.lr_scheduler.MultiStepLR(
opt,
milestones=milestones,
gamma=gamma,
last_epoch=-1 if args.load_epoch is None else args.load_epoch,
)
elif args.lr_scheduler == "flat+linear" or args.lr_scheduler == "flat+cos":
total_steps = args.num_epochs * args.steps_per_epoch
warmup_steps = (args.warmup_steps * total_steps) if args.warmup_steps < 1 else args.warmup_steps
flat_steps = total_steps * 0.7 - 1
min_factor = 0.001
def lr_fn(step_num):
if step_num > total_steps:
raise ValueError(
"Tried to step {} times. The specified number of total steps is {}".format(
step_num + 1, total_steps
)
)
if step_num < warmup_steps:
return 1.0 * step_num / warmup_steps
if step_num <= flat_steps:
return 1.0
pct = (step_num - flat_steps) / (total_steps - flat_steps)
if args.lr_scheduler == "flat+linear":
return max(min_factor, 1 - pct)
else:
return max(min_factor, 0.5 * (math.cos(math.pi * pct) + 1))
scheduler = torch.optim.lr_scheduler.LambdaLR(
opt, lr_fn, last_epoch=-1 if args.load_epoch is None else args.load_epoch * args.steps_per_epoch
)
scheduler._update_per_step = True # mark it to update the lr every step, instead of every epoch
elif args.lr_scheduler == "warmup+cos":
num_training_steps = args.num_epochs * args.steps_per_epoch
num_warmup_steps = (args.warmup_steps * num_training_steps) if args.warmup_steps < 1 else args.warmup_steps
num_cycles = 0.5
def lr_lambda(current_step):
if current_step < num_warmup_steps:
return float(current_step) / float(max(1, num_warmup_steps))
progress = float(current_step - num_warmup_steps) / float(max(1, num_training_steps - num_warmup_steps))
return max(0.0, 0.5 * (1.0 + math.cos(math.pi * float(num_cycles) * 2.0 * progress)))
scheduler = torch.optim.lr_scheduler.LambdaLR(
opt, lr_lambda, last_epoch=-1 if args.load_epoch is None else args.load_epoch * args.steps_per_epoch
)
scheduler._update_per_step = True # mark it to update the lr every step, instead of every epoch
elif args.lr_scheduler == "one-cycle":
scheduler = torch.optim.lr_scheduler.OneCycleLR(
opt,
max_lr=args.start_lr,
epochs=args.num_epochs,
steps_per_epoch=args.steps_per_epoch,
pct_start=0.3,
anneal_strategy="cos",
div_factor=25.0,
last_epoch=-1 if args.load_epoch is None else args.load_epoch,
)
scheduler._update_per_step = True # mark it to update the lr every step, instead of every epoch
if compile_optimizer:
compiler_options = {k: ast.literal_eval(v) for k, v in args.compiler_option}
_logger.info("Compiling the optimizer step with torch.compile, options: %s" % str(compiler_options))
# Wrap `step` in place so the training loop keeps calling `opt.step()` unchanged. `torch.compile`
# captures the current `step` callable, so this does not recurse, and `grad_scaler.step(opt)` (fp16
# AMP) still routes through the compiled step. This must run *after* the scheduler is constructed:
# LR schedulers patch `opt.step` (via `__func__`) at init time and require it to still be a bound
# method. The Tensor lr (see `start_lr` above) keeps the scheduler's per-step lr updates from
# triggering recompilation.
opt.step = torch.compile(opt.step, **compiler_options)
return opt, scheduler
def load_checkpoint(args, model, opt):
# load previous training and resume if `--load-epoch` is set
_logger.info("Resume training from epoch %d" % args.load_epoch)
model_state = torch.load(args.model_prefix + "_epoch-%d_state.pt" % args.load_epoch, map_location="cpu")
model.load_state_dict(model_state)
opt_state_file = args.model_prefix + "_epoch-%d_optimizer.pt" % args.load_epoch
if os.path.exists(opt_state_file):
opt_state = torch.load(opt_state_file, map_location="cpu")
opt.load_state_dict(opt_state)
else:
_logger.warning("Optimizer state file %s NOT found!" % opt_state_file)
def model_setup(args, data_config, device="cpu"):
"""
Loads the model
:param args:
:param data_config:
:return: model, model_info, network_module, network_options
"""
network_module = import_module(args.network_config, name="_network_module")
network_options = {k: ast.literal_eval(v) for k, v in args.network_option}
_logger.info("Network options: %s" % str(network_options))
if args.export_onnx:
network_options["for_inference"] = True
if args.use_amp:
network_options["use_amp"] = True
if args.compile:
network_options["compile_model"] = True
model, model_info = network_module.get_model(data_config, **network_options)
if args.load_model_weights:
if ":" in args.load_model_weights:
state_file, prefix = args.load_model_weights.split(":")
model_state = torch.load(state_file, map_location="cpu")
model_state = {
k.replace(prefix + ".", "", 1): v for k, v in model_state.items() if k.startswith(prefix + ".")
}
else:
model_state = torch.load(args.load_model_weights, map_location="cpu")
if args.exclude_model_weights:
import re
exclude_patterns = args.exclude_model_weights.split(",")
_logger.info("The following weights will not be loaded: %s" % str(exclude_patterns))
key_state = {}
for k in model_state.keys():
key_state[k] = True
for pattern in exclude_patterns:
if re.match(pattern, k):
key_state[k] = False
break
model_state = {k: v for k, v in model_state.items() if key_state[k]}
missing_keys, unexpected_keys = model.load_state_dict(model_state, strict=False)
_logger.info(
"Model initialized with weights from %s\n ... Missing: %s\n ... Unexpected: %s"
% (args.load_model_weights, missing_keys, unexpected_keys)
)
if args.freeze_model_weights:
import re
freeze_patterns = args.freeze_model_weights.split(",")
for name, param in model.named_parameters():
freeze = False
for pattern in freeze_patterns:
if re.match(pattern, name):
freeze = True
break
if freeze:
param.requires_grad = False
_logger.info(
"The following weights has been frozen:\n - %s",
"\n - ".join([name for name, p in model.named_parameters() if not p.requires_grad]),
)
# _logger.info(model)
try:
flops(model, model_info, device=device)
except Exception as e:
_logger.error("Error in flops: %s", str(e))
# loss function
try:
loss_func = network_module.get_loss(data_config, **network_options)
_logger.info("Using loss function %s with options %s" % (loss_func, network_options))
except AttributeError:
loss_func = torch.nn.CrossEntropyLoss()
_logger.warning(
"Loss function not defined in %s. Will use `torch.nn.CrossEntropyLoss()` by default.", args.network_config
)
# train / evaluate loop implementation
try:
train = network_module.get_train_fn(data_config, **network_options)
evaluate = network_module.get_evaluate_fn(data_config, **network_options)
_logger.info("Using custom train/evaluate functions with options %s" % network_options)
except AttributeError:
if args.regression_mode:
_logger.info("Running in regression mode")
from weaver.utils.nn.tools import train_regression as train
from weaver.utils.nn.tools import evaluate_regression as evaluate
else:
_logger.info("Running in classification mode")
from weaver.utils.nn.tools import train_classification as train
from weaver.utils.nn.tools import evaluate_classification as evaluate
return model, model_info, loss_func, train, evaluate
def optimizer_setup(args, model):
"""
Optimizer and scheduler.
:param args:
:param model:
:return:
"""
optimizer_options = {k: ast.literal_eval(v) for k, v in args.optimizer_option}
_logger.info("Optimizer options: %s" % str(optimizer_options))
network_module = import_module(args.network_config, name="_network_module")
try:
return network_module.init_opt(args, model, **optimizer_options)
except AttributeError:
return init_opt(args, model, **optimizer_options)
def iotest(args, data_loader):
"""
Io test
:param args:
:param data_loader:
:return:
"""
from tqdm.auto import tqdm
from collections import defaultdict
from weaver.utils.data.tools import _concat
_logger.info("Start running IO test")
monitor_info = defaultdict(list)
for X, y, Z in tqdm(data_loader):
for k, v in Z.items():
monitor_info[k].append(v)
monitor_info = {k: _concat(v) for k, v in monitor_info.items()}
if monitor_info:
monitor_output_path = "weaver_monitor_info.parquet"
try:
import awkward as ak
ak.to_parquet(ak.Array(monitor_info), monitor_output_path, compression="LZ4", compression_level=4)
_logger.info("Monitor info written to %s" % monitor_output_path, color="bold")
except Exception as e:
_logger.error("Error when writing output parquet file: \n" + str(e))
def save_root(args, output_path, data_config, scores, labels, observers):
"""
Saves as .root
:param data_config:
:param scores:
:param labels
:param observers
:return:
"""
import awkward as ak
from weaver.utils.data.fileio import _write_root
output = {}
if data_config.label_type == "simple":
for idx, label_name in enumerate(data_config.label_value):
output[label_name] = labels[data_config.label_names[0]] == idx
output["score_" + label_name] = scores[:, idx]
else:
if scores.ndim <= 2:
output["output"] = scores
elif scores.ndim == 3:
num_classes = len(scores[0, 0, :])
try:
names = data_config.labels["names"]
assert len(names) == num_classes
except KeyError:
names = [f"class_{idx}" for idx in range(num_classes)]
for idx, label_name in enumerate(names):
output[label_name] = labels[data_config.label_names[0]] == idx
output["score_" + label_name] = scores[:, :, idx]
else:
output["output"] = scores
output.update(labels)
output.update(observers)
try:
_write_root(output_path, ak.Array(output))
_logger.info("Written output to %s" % output_path, color="bold")
except Exception as e:
_logger.error("Error when writing output ROOT file: \n" + str(e))
save_as_parquet = any(v.ndim > 2 for v in output.values())
if save_as_parquet:
try:
ak.to_parquet(
ak.Array(output), output_path.replace(".root", ".parquet"), compression="LZ4", compression_level=4
)
_logger.info(
"Written alternative output file to %s" % output_path.replace(".root", ".parquet"), color="bold"
)
except Exception as e:
_logger.error("Error when writing output parquet file: \n" + str(e))
def save_parquet(args, output_path, scores, labels, observers):
"""
Saves as parquet file
:param scores:
:param labels:
:param observers:
:return:
"""
import awkward as ak
output = {"scores": scores}
output.update(labels)
output.update(observers)
try:
ak.to_parquet(ak.Array(output), output_path, compression="LZ4", compression_level=4)
_logger.info("Written output to %s" % output_path, color="bold")
except Exception as e:
_logger.error("Error when writing output parquet file: \n" + str(e))
def _main(args):
_logger.info("args:\n - %s", "\n - ".join(str(it) for it in args.__dict__.items()))
# export to ONNX
if args.export_onnx:
onnx(args)
return
if args.file_fraction < 1:
_logger.warning("Use of `file-fraction` is not recommended in general -- prefer using `data-fraction` instead.")
# training/testing mode
if args.predict:
_logger.warning("The `--predict` flag is set. Overriding the `--run-mode` to `test`.")
args.run_mode = ["test"]
training_mode = any(m in args.run_mode for m in ["train", "val"])
# device
if args.gpus:
# distributed training
if args.backend is not None:
local_rank = args.local_rank
torch.cuda.set_device(local_rank)