forked from deepmodeling/deepmd-kit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtraining.py
1297 lines (1220 loc) · 52.7 KB
/
training.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
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
# SPDX-License-Identifier: LGPL-3.0-or-later
import functools
import logging
import time
from copy import (
deepcopy,
)
from pathlib import (
Path,
)
from typing import (
Any,
Dict,
)
import numpy as np
import torch
from deepmd.common import (
symlink_prefix_files,
)
from deepmd.loggers.training import (
format_training_message,
format_training_message_per_task,
)
from deepmd.pt.loss import (
DenoiseLoss,
DOSLoss,
EnergySpinLoss,
EnergyStdLoss,
PropertyLoss,
TensorLoss,
)
from deepmd.pt.model.model import (
get_model,
get_zbl_model,
)
from deepmd.pt.optimizer import (
KFOptimizerWrapper,
LKFOptimizer,
)
from deepmd.pt.train.wrapper import (
ModelWrapper,
)
from deepmd.pt.utils import (
dp_random,
)
from deepmd.pt.utils.dataloader import (
BufferedIterator,
get_weighted_sampler,
)
from deepmd.pt.utils.env import (
DEVICE,
JIT,
LOCAL_RANK,
NUM_WORKERS,
SAMPLER_RECORD,
)
from deepmd.pt.utils.learning_rate import (
LearningRateExp,
)
from deepmd.pt.utils.stat import (
make_stat_input,
)
from deepmd.pt.utils.utils import (
to_numpy_array,
)
from deepmd.utils.data import (
DataRequirementItem,
)
if torch.__version__.startswith("2"):
import torch._dynamo
import torch.distributed as dist
from torch.nn.parallel import DistributedDataParallel as DDP
from torch.utils.data import (
DataLoader,
)
from deepmd.utils.path import (
DPH5Path,
)
log = logging.getLogger(__name__)
class Trainer:
def __init__(
self,
config: Dict[str, Any],
training_data,
stat_file_path=None,
validation_data=None,
init_model=None,
restart_model=None,
finetune_model=None,
force_load=False,
shared_links=None,
finetune_links=None,
init_frz_model=None,
):
"""Construct a DeePMD trainer.
Args:
- config: The Dict-like configuration with training options.
"""
if init_model is not None:
resume_model = init_model
elif restart_model is not None:
resume_model = restart_model
elif finetune_model is not None:
resume_model = finetune_model
else:
resume_model = None
resuming = resume_model is not None
self.restart_training = restart_model is not None
model_params = config["model"]
training_params = config["training"]
self.multi_task = "model_dict" in model_params
self.finetune_links = finetune_links
self.finetune_update_stat = False
self.model_keys = (
list(model_params["model_dict"]) if self.multi_task else ["Default"]
)
self.rank = (
dist.get_rank() if dist.is_available() and dist.is_initialized() else 0
)
self.world_size = (
dist.get_world_size()
if dist.is_available() and dist.is_initialized()
else 1
)
self.num_model = len(self.model_keys)
# Iteration config
self.num_steps = training_params["numb_steps"]
self.disp_file = training_params.get("disp_file", "lcurve.out")
self.disp_freq = training_params.get("disp_freq", 1000)
self.save_ckpt = training_params.get("save_ckpt", "model.ckpt")
self.save_freq = training_params.get("save_freq", 1000)
self.max_ckpt_keep = training_params.get("max_ckpt_keep", 5)
self.display_in_training = training_params.get("disp_training", True)
self.timing_in_training = training_params.get("time_training", True)
self.change_bias_after_training = training_params.get(
"change_bias_after_training", False
)
self.lcurve_should_print_header = True
def get_opt_param(params):
opt_type = params.get("opt_type", "Adam")
opt_param = {
"kf_blocksize": params.get("kf_blocksize", 5120),
"kf_start_pref_e": params.get("kf_start_pref_e", 1),
"kf_limit_pref_e": params.get("kf_limit_pref_e", 1),
"kf_start_pref_f": params.get("kf_start_pref_f", 1),
"kf_limit_pref_f": params.get("kf_limit_pref_f", 1),
}
return opt_type, opt_param
def get_data_loader(_training_data, _validation_data, _training_params):
def get_dataloader_and_buffer(_data, _params):
if "auto_prob" in _training_params["training_data"]:
_sampler = get_weighted_sampler(
_data, _params["training_data"]["auto_prob"]
)
elif "sys_probs" in _training_params["training_data"]:
_sampler = get_weighted_sampler(
_data,
_params["training_data"]["sys_probs"],
sys_prob=True,
)
else:
_sampler = get_weighted_sampler(_data, "prob_sys_size")
if _sampler is None:
log.warning(
"Sampler not specified!"
) # None sampler will lead to a premature stop iteration. Replacement should be True in attribute of the sampler to produce expected number of items in one iteration.
_dataloader = DataLoader(
_data,
sampler=_sampler,
batch_size=None,
num_workers=NUM_WORKERS
if dist.is_available()
else 0, # setting to 0 diverges the behavior of its iterator; should be >=1
drop_last=False,
collate_fn=lambda batch: batch, # prevent extra conversion
pin_memory=True,
)
with torch.device("cpu"):
_data_buffered = BufferedIterator(iter(_dataloader))
return _dataloader, _data_buffered
training_dataloader, training_data_buffered = get_dataloader_and_buffer(
_training_data, _training_params
)
if _validation_data is not None:
(
validation_dataloader,
validation_data_buffered,
) = get_dataloader_and_buffer(_validation_data, _training_params)
valid_numb_batch = _training_params["validation_data"].get(
"numb_btch", 1
)
else:
validation_dataloader = None
validation_data_buffered = None
valid_numb_batch = 1
return (
training_dataloader,
training_data_buffered,
validation_dataloader,
validation_data_buffered,
valid_numb_batch,
)
def single_model_stat(
_model,
_data_stat_nbatch,
_training_data,
_validation_data,
_stat_file_path,
_data_requirement,
finetune_has_new_type=False,
):
_data_requirement += get_additional_data_requirement(_model)
_training_data.add_data_requirement(_data_requirement)
if _validation_data is not None:
_validation_data.add_data_requirement(_data_requirement)
@functools.lru_cache
def get_sample():
sampled = make_stat_input(
_training_data.systems,
_training_data.dataloaders,
_data_stat_nbatch,
)
return sampled
if (not resuming or finetune_has_new_type) and self.rank == 0:
_model.compute_or_load_stat(
sampled_func=get_sample,
stat_file_path=_stat_file_path,
)
if isinstance(_stat_file_path, DPH5Path):
_stat_file_path.root.close()
return get_sample
def get_lr(lr_params):
assert (
lr_params.get("type", "exp") == "exp"
), "Only learning rate `exp` is supported!"
lr_params["stop_steps"] = self.num_steps - self.warmup_steps
lr_exp = LearningRateExp(**lr_params)
return lr_exp
# Optimizer
if self.multi_task and training_params.get("optim_dict", None) is not None:
self.optim_dict = training_params.get("optim_dict")
missing_keys = [
key for key in self.model_keys if key not in self.optim_dict
]
assert (
not missing_keys
), f"These keys are not in optim_dict: {missing_keys}!"
self.opt_type = {}
self.opt_param = {}
for model_key in self.model_keys:
self.opt_type[model_key], self.opt_param[model_key] = get_opt_param(
self.optim_dict[model_key]
)
else:
self.opt_type, self.opt_param = get_opt_param(training_params)
# Model
self.model = get_model_for_wrapper(model_params)
# Loss
if not self.multi_task:
self.loss = get_loss(
config["loss"],
config["learning_rate"]["start_lr"],
len(model_params["type_map"]),
self.model,
)
else:
self.loss = {}
for model_key in self.model_keys:
loss_param = config["loss_dict"][model_key]
if config.get("learning_rate_dict", None) is not None:
lr_param = config["learning_rate_dict"][model_key]["start_lr"]
else:
lr_param = config["learning_rate"]["start_lr"]
ntypes = len(model_params["model_dict"][model_key]["type_map"])
self.loss[model_key] = get_loss(
loss_param, lr_param, ntypes, self.model[model_key]
)
# Data
if not self.multi_task:
self.get_sample_func = single_model_stat(
self.model,
model_params.get("data_stat_nbatch", 10),
training_data,
validation_data,
stat_file_path,
self.loss.label_requirement,
finetune_has_new_type=self.finetune_links["Default"].get_has_new_type()
if self.finetune_links is not None
else False,
)
(
self.training_dataloader,
self.training_data,
self.validation_dataloader,
self.validation_data,
self.valid_numb_batch,
) = get_data_loader(training_data, validation_data, training_params)
training_data.print_summary(
"training", to_numpy_array(self.training_dataloader.sampler.weights)
)
if validation_data is not None:
validation_data.print_summary(
"validation",
to_numpy_array(self.validation_dataloader.sampler.weights),
)
else:
(
self.training_dataloader,
self.training_data,
self.validation_dataloader,
self.validation_data,
self.valid_numb_batch,
self.get_sample_func,
) = {}, {}, {}, {}, {}, {}
for model_key in self.model_keys:
self.get_sample_func[model_key] = single_model_stat(
self.model[model_key],
model_params["model_dict"][model_key].get("data_stat_nbatch", 10),
training_data[model_key],
validation_data[model_key],
stat_file_path[model_key],
self.loss[model_key].label_requirement,
finetune_has_new_type=self.finetune_links[
model_key
].get_has_new_type()
if self.finetune_links is not None
else False,
)
(
self.training_dataloader[model_key],
self.training_data[model_key],
self.validation_dataloader[model_key],
self.validation_data[model_key],
self.valid_numb_batch[model_key],
) = get_data_loader(
training_data[model_key],
validation_data[model_key],
training_params["data_dict"][model_key],
)
training_data[model_key].print_summary(
f"training in {model_key}",
to_numpy_array(self.training_dataloader[model_key].sampler.weights),
)
if (
validation_data is not None
and validation_data[model_key] is not None
):
validation_data[model_key].print_summary(
f"validation in {model_key}",
to_numpy_array(
self.validation_dataloader[model_key].sampler.weights
),
)
# Learning rate
self.warmup_steps = training_params.get("warmup_steps", 0)
self.gradient_max_norm = training_params.get("gradient_max_norm", 0.0)
assert (
self.num_steps - self.warmup_steps > 0 or self.warmup_steps == 0
), "Warm up steps must be less than total training steps!"
if self.multi_task and config.get("learning_rate_dict", None) is not None:
self.lr_exp = {}
for model_key in self.model_keys:
self.lr_exp[model_key] = get_lr(config["learning_rate_dict"][model_key])
else:
self.lr_exp = get_lr(config["learning_rate"])
# JIT
if JIT:
self.model = torch.jit.script(self.model)
# Model Wrapper
self.wrapper = ModelWrapper(self.model, self.loss, model_params=model_params)
self.start_step = 0
# resuming and finetune
optimizer_state_dict = None
if resuming:
log.info(f"Resuming from {resume_model}.")
state_dict = torch.load(resume_model, map_location=DEVICE)
if "model" in state_dict:
optimizer_state_dict = (
state_dict["optimizer"] if finetune_model is None else None
)
state_dict = state_dict["model"]
self.start_step = (
state_dict["_extra_state"]["train_infos"]["step"]
if self.restart_training
else 0
)
if self.rank == 0:
if force_load:
input_keys = list(state_dict.keys())
target_keys = list(self.wrapper.state_dict().keys())
missing_keys = [
item for item in target_keys if item not in input_keys
]
if missing_keys:
target_state_dict = self.wrapper.state_dict()
slim_keys = []
for item in missing_keys:
state_dict[item] = target_state_dict[item].clone().detach()
new_key = True
for slim_key in slim_keys:
if slim_key in item:
new_key = False
break
if new_key:
tmp_keys = ".".join(item.split(".")[:3])
slim_keys.append(tmp_keys)
slim_keys = [i + ".*" for i in slim_keys]
log.warning(
f"Force load mode allowed! These keys are not in ckpt and will re-init: {slim_keys}"
)
# update model params in the pretrained model
if finetune_model is not None:
new_state_dict = {}
target_state_dict = self.wrapper.state_dict()
# pretrained_model
pretrained_model = get_model_for_wrapper(
state_dict["_extra_state"]["model_params"]
)
pretrained_model_wrapper = ModelWrapper(pretrained_model)
pretrained_model_wrapper.load_state_dict(state_dict)
# update type related params
for model_key in self.model_keys:
finetune_rule_single = self.finetune_links[model_key]
_model_key_from = finetune_rule_single.get_model_branch()
# skip if updated
if (
finetune_rule_single.get_finetune_tmap()
!= pretrained_model_wrapper.model[
_model_key_from
].get_type_map()
):
model_with_new_type_stat = None
if finetune_rule_single.get_has_new_type():
self.finetune_update_stat = True
model_with_new_type_stat = self.wrapper.model[model_key]
pretrained_model_wrapper.model[
_model_key_from
].change_type_map(
finetune_rule_single.get_finetune_tmap(),
model_with_new_type_stat=model_with_new_type_stat,
)
state_dict = pretrained_model_wrapper.state_dict()
def collect_single_finetune_params(
_model_key,
_finetune_rule_single,
_new_state_dict,
_origin_state_dict,
_random_state_dict,
):
_new_fitting = _finetune_rule_single.get_random_fitting()
_model_key_from = _finetune_rule_single.get_model_branch()
target_keys = [
i
for i in _random_state_dict.keys()
if i != "_extra_state" and f".{_model_key}." in i
]
for item_key in target_keys:
if _new_fitting and (".descriptor." not in item_key):
# print(f'Keep {item_key} in old model!')
_new_state_dict[item_key] = (
_random_state_dict[item_key].clone().detach()
)
else:
new_key = item_key.replace(
f".{_model_key}.", f".{_model_key_from}."
)
# print(f'Replace {item_key} with {new_key} in pretrained_model!')
_new_state_dict[item_key] = (
_origin_state_dict[new_key].clone().detach()
)
# collect model params from the pretrained model
for model_key in self.model_keys:
finetune_rule_single = self.finetune_links[model_key]
collect_single_finetune_params(
model_key,
finetune_rule_single,
new_state_dict,
state_dict,
target_state_dict,
)
state_dict = new_state_dict
state_dict["_extra_state"] = self.wrapper.state_dict()[
"_extra_state"
]
self.wrapper.load_state_dict(state_dict)
# change bias for fine-tuning
if finetune_model is not None:
def single_model_finetune(
_model,
_finetune_rule_single,
_sample_func,
):
_model = model_change_out_bias(
_model,
_sample_func,
_bias_adjust_mode="change-by-statistic"
if not _finetune_rule_single.get_random_fitting()
else "set-by-statistic",
)
return _model
if not self.multi_task:
finetune_rule_single = self.finetune_links["Default"]
self.model = single_model_finetune(
self.model, finetune_rule_single, self.get_sample_func
)
else:
for model_key in self.model_keys:
finetune_rule_single = self.finetune_links[model_key]
if not finetune_rule_single.get_resuming():
log.info(
f"Model branch {model_key} will be fine-tuned. This may take a long time..."
)
self.model[model_key] = single_model_finetune(
self.model[model_key],
finetune_rule_single,
self.get_sample_func[model_key],
)
else:
log.info(
f"Model branch {model_key} will resume training."
)
if init_frz_model is not None:
frz_model = torch.jit.load(init_frz_model, map_location=DEVICE)
self.model.load_state_dict(frz_model.state_dict())
# Multi-task share params
if shared_links is not None:
self.wrapper.share_params(
shared_links,
resume=(resuming and not self.finetune_update_stat) or self.rank != 0,
)
if dist.is_available() and dist.is_initialized():
torch.cuda.set_device(LOCAL_RANK)
# DDP will guarantee the model parameters are identical across all processes
self.wrapper = DDP(
self.wrapper,
device_ids=[LOCAL_RANK],
find_unused_parameters=True,
output_device=LOCAL_RANK,
)
# TODO add lr warmups for multitask
# author: iProzd
def warm_up_linear(step, warmup_steps):
if step < warmup_steps:
return step / warmup_steps
else:
return self.lr_exp.value(step - warmup_steps) / self.lr_exp.start_lr
# TODO add optimizers for multitask
# author: iProzd
if self.opt_type == "Adam":
self.optimizer = torch.optim.Adam(
self.wrapper.parameters(), lr=self.lr_exp.start_lr
)
if optimizer_state_dict is not None and self.restart_training:
self.optimizer.load_state_dict(optimizer_state_dict)
self.scheduler = torch.optim.lr_scheduler.LambdaLR(
self.optimizer,
lambda step: warm_up_linear(step + self.start_step, self.warmup_steps),
)
elif self.opt_type == "LKF":
self.optimizer = LKFOptimizer(
self.wrapper.parameters(), 0.98, 0.99870, self.opt_param["kf_blocksize"]
)
else:
raise ValueError(f"Not supported optimizer type '{self.opt_type}'")
# Get model prob for multi-task
if self.multi_task:
self.model_prob = np.array([0.0 for key in self.model_keys])
if training_params.get("model_prob", None) is not None:
model_prob = training_params["model_prob"]
for ii, model_key in enumerate(self.model_keys):
if model_key in model_prob:
self.model_prob[ii] += float(model_prob[model_key])
else:
for ii, model_key in enumerate(self.model_keys):
self.model_prob[ii] += float(len(self.training_data[model_key]))
sum_prob = np.sum(self.model_prob)
assert sum_prob > 0.0, "Sum of model prob must be larger than 0!"
self.model_prob = self.model_prob / sum_prob
# Tensorboard
self.enable_tensorboard = training_params.get("tensorboard", False)
self.tensorboard_log_dir = training_params.get("tensorboard_log_dir", "log")
self.tensorboard_freq = training_params.get("tensorboard_freq", 1)
self.enable_profiler = training_params.get("enable_profiler", False)
self.profiling = training_params.get("profiling", False)
self.profiling_file = training_params.get("profiling_file", "timeline.json")
def run(self):
fout = (
open(
self.disp_file,
mode="w" if not self.restart_training else "a",
buffering=1,
)
if self.rank == 0
else None
) # line buffered
if SAMPLER_RECORD:
record_file = f"Sample_rank_{self.rank}.txt"
fout1 = open(record_file, mode="w", buffering=1)
log.info("Start to train %d steps.", self.num_steps)
if dist.is_available() and dist.is_initialized():
log.info(f"Rank: {dist.get_rank()}/{dist.get_world_size()}")
if self.enable_tensorboard:
from torch.utils.tensorboard import (
SummaryWriter,
)
writer = SummaryWriter(log_dir=self.tensorboard_log_dir)
if self.enable_profiler or self.profiling:
prof = torch.profiler.profile(
schedule=torch.profiler.schedule(wait=1, warmup=1, active=3, repeat=1),
on_trace_ready=torch.profiler.tensorboard_trace_handler(
self.tensorboard_log_dir
)
if self.enable_profiler
else None,
record_shapes=True,
with_stack=True,
)
prof.start()
def step(_step_id, task_key="Default"):
# PyTorch Profiler
if self.enable_profiler or self.profiling:
prof.step()
self.wrapper.train()
if isinstance(self.lr_exp, dict):
_lr = self.lr_exp[task_key]
else:
_lr = self.lr_exp
cur_lr = _lr.value(_step_id)
pref_lr = cur_lr
self.optimizer.zero_grad(set_to_none=True)
input_dict, label_dict, log_dict = self.get_data(
is_train=True, task_key=task_key
)
if SAMPLER_RECORD:
print_str = f"Step {_step_id}: sample system{log_dict['sid']} frame{log_dict['fid']}\n"
fout1.write(print_str)
fout1.flush()
if self.opt_type == "Adam":
cur_lr = self.scheduler.get_last_lr()[0]
if _step_id < self.warmup_steps:
pref_lr = _lr.start_lr
else:
pref_lr = cur_lr
model_pred, loss, more_loss = self.wrapper(
**input_dict, cur_lr=pref_lr, label=label_dict, task_key=task_key
)
loss.backward()
if self.gradient_max_norm > 0.0:
grad_norm = torch.nn.utils.clip_grad_norm_(
self.wrapper.parameters(), self.gradient_max_norm
)
if not torch.isfinite(grad_norm).all():
# check local gradnorm single GPU case, trigger NanDetector
raise FloatingPointError("gradients are Nan/Inf")
with torch.device("cpu"):
self.optimizer.step()
self.scheduler.step()
elif self.opt_type == "LKF":
if isinstance(self.loss, EnergyStdLoss):
KFOptWrapper = KFOptimizerWrapper(
self.wrapper,
self.optimizer,
24,
6,
dist.is_available() and dist.is_initialized(),
)
pref_e = self.opt_param["kf_start_pref_e"] * (
self.opt_param["kf_limit_pref_e"]
/ self.opt_param["kf_start_pref_e"]
) ** (_step_id / self.num_steps)
_ = KFOptWrapper.update_energy(
input_dict, label_dict["energy"], pref_e
)
pref_f = self.opt_param["kf_start_pref_f"] * (
self.opt_param["kf_limit_pref_f"]
/ self.opt_param["kf_start_pref_f"]
) ** (_step_id / self.num_steps)
p_energy, p_force = KFOptWrapper.update_force(
input_dict, label_dict["force"], pref_f
)
# [coord, atype, natoms, mapping, shift, nlist, box]
model_pred = {"energy": p_energy, "force": p_force}
module = (
self.wrapper.module
if dist.is_available() and dist.is_initialized()
else self.wrapper
)
def fake_model():
return model_pred
_, loss, more_loss = module.loss[task_key](
{},
fake_model,
label_dict,
int(input_dict["atype"].shape[-1]),
learning_rate=pref_lr,
)
elif isinstance(self.loss, DenoiseLoss):
KFOptWrapper = KFOptimizerWrapper(
self.wrapper,
self.optimizer,
24,
6,
dist.is_available() and dist.is_initialized(),
)
module = (
self.wrapper.module
if dist.is_available() and dist.is_initialized()
else self.wrapper
)
model_pred = KFOptWrapper.update_denoise_coord(
input_dict,
label_dict["clean_coord"],
1,
module.loss[task_key].mask_loss_coord,
label_dict["coord_mask"],
)
loss, more_loss = module.loss[task_key](
model_pred,
label_dict,
input_dict["natoms"],
learning_rate=pref_lr,
)
else:
raise ValueError(f"Not supported optimizer type '{self.opt_type}'")
# Log and persist
if self.display_in_training and _step_id % self.disp_freq == 0:
self.wrapper.eval()
def log_loss_train(_loss, _more_loss, _task_key="Default"):
results = {}
rmse_val = {
item: _more_loss[item]
for item in _more_loss
if "l2_" not in item
}
for item in sorted(rmse_val.keys()):
results[item] = rmse_val[item]
return results
def log_loss_valid(_task_key="Default"):
single_results = {}
sum_natoms = 0
if not self.multi_task:
valid_numb_batch = self.valid_numb_batch
else:
valid_numb_batch = self.valid_numb_batch[_task_key]
for ii in range(valid_numb_batch):
self.optimizer.zero_grad()
input_dict, label_dict, _ = self.get_data(
is_train=False, task_key=_task_key
)
if input_dict == {}:
# no validation data
return {}
_, loss, more_loss = self.wrapper(
**input_dict,
cur_lr=pref_lr,
label=label_dict,
task_key=_task_key,
)
# more_loss.update({"rmse": math.sqrt(loss)})
natoms = int(input_dict["atype"].shape[-1])
sum_natoms += natoms
for k, v in more_loss.items():
if "l2_" not in k:
single_results[k] = (
single_results.get(k, 0.0) + v * natoms
)
results = {k: v / sum_natoms for k, v in single_results.items()}
return results
if not self.multi_task:
train_results = log_loss_train(loss, more_loss)
valid_results = log_loss_valid()
if self.rank == 0:
log.info(
format_training_message_per_task(
batch=_step_id,
task_name="trn",
rmse=train_results,
learning_rate=cur_lr,
)
)
if valid_results:
log.info(
format_training_message_per_task(
batch=_step_id,
task_name="val",
rmse=valid_results,
learning_rate=None,
)
)
else:
train_results = {_key: {} for _key in self.model_keys}
valid_results = {_key: {} for _key in self.model_keys}
train_results[task_key] = log_loss_train(
loss, more_loss, _task_key=task_key
)
for _key in self.model_keys:
if _key != task_key:
self.optimizer.zero_grad()
input_dict, label_dict, _ = self.get_data(
is_train=True, task_key=_key
)
_, loss, more_loss = self.wrapper(
**input_dict,
cur_lr=pref_lr,
label=label_dict,
task_key=_key,
)
train_results[_key] = log_loss_train(
loss, more_loss, _task_key=_key
)
valid_results[_key] = log_loss_valid(_task_key=_key)
if self.rank == 0:
log.info(
format_training_message_per_task(
batch=_step_id,
task_name=_key + "_trn",
rmse=train_results[_key],
learning_rate=cur_lr,
)
)
if valid_results[_key]:
log.info(
format_training_message_per_task(
batch=_step_id,
task_name=_key + "_val",
rmse=valid_results[_key],
learning_rate=None,
)
)
current_time = time.time()
train_time = current_time - self.t0
self.t0 = current_time
if self.rank == 0 and self.timing_in_training:
log.info(
format_training_message(
batch=_step_id,
wall_time=train_time,
)
)
# the first training time is not accurate
if (
_step_id + 1
) > self.disp_freq or self.num_steps < 2 * self.disp_freq:
self.total_train_time += train_time
if fout:
if self.lcurve_should_print_header:
self.print_header(fout, train_results, valid_results)
self.lcurve_should_print_header = False
self.print_on_training(
fout, _step_id, cur_lr, train_results, valid_results
)
if (
((_step_id + 1) % self.save_freq == 0 and _step_id != self.start_step)
or (_step_id + 1) == self.num_steps
) and (self.rank == 0 or dist.get_rank() == 0):
# Handle the case if rank 0 aborted and re-assigned
self.latest_model = Path(self.save_ckpt + f"-{_step_id + 1}.pt")
module = (
self.wrapper.module
if dist.is_available() and dist.is_initialized()
else self.wrapper
)
self.save_model(self.latest_model, lr=cur_lr, step=_step_id)
log.info(f"Saved model to {self.latest_model}")
symlink_prefix_files(self.latest_model.stem, self.save_ckpt)
with open("checkpoint", "w") as f:
f.write(str(self.latest_model))
# tensorboard
if self.enable_tensorboard and _step_id % self.tensorboard_freq == 0:
writer.add_scalar(f"{task_key}/lr", cur_lr, _step_id)
writer.add_scalar(f"{task_key}/loss", loss, _step_id)
for item in more_loss:
writer.add_scalar(f"{task_key}/{item}", more_loss[item], _step_id)
self.t0 = time.time()
self.total_train_time = 0.0
for step_id in range(self.num_steps):
if step_id < self.start_step:
continue
if self.multi_task:
chosen_index_list = dp_random.choice(
np.arange(self.num_model), # pylint: disable=no-explicit-dtype
p=np.array(self.model_prob),
size=self.world_size,
replace=True,
)
assert chosen_index_list.size == self.world_size
model_index = chosen_index_list[self.rank]
model_key = self.model_keys[model_index]
else:
model_key = "Default"
step(step_id, model_key)
if JIT:
break
if self.change_bias_after_training and (self.rank == 0 or dist.get_rank() == 0):
if not self.multi_task:
self.model = model_change_out_bias(
self.model,
self.get_sample_func,
_bias_adjust_mode="change-by-statistic",
)
else:
for model_key in self.model_keys:
self.model[model_key] = model_change_out_bias(
self.model[model_key],
self.get_sample_func[model_key],
_bias_adjust_mode="change-by-statistic",
)
self.latest_model = Path(self.save_ckpt + f"-{self.num_steps}.pt")
cur_lr = self.lr_exp.value(self.num_steps - 1)
self.save_model(self.latest_model, lr=cur_lr, step=self.num_steps - 1)
log.info(f"Saved model to {self.latest_model}")
symlink_prefix_files(self.latest_model.stem, self.save_ckpt)
with open("checkpoint", "w") as f:
f.write(str(self.latest_model))
if (
self.rank == 0 or dist.get_rank() == 0
): # Handle the case if rank 0 aborted and re-assigned
if self.num_steps == 0:
# when num_steps is 0, the checkpoint is never not saved
self.latest_model = Path(self.save_ckpt + "-0.pt")
self.save_model(self.latest_model, lr=0, step=0)
log.info(f"Saved model to {self.latest_model}")
symlink_prefix_files(self.latest_model.stem, self.save_ckpt)
with open("checkpoint", "w") as f:
f.write(str(self.latest_model))
if self.timing_in_training and self.num_steps // self.disp_freq > 0:
if self.num_steps >= 2 * self.disp_freq:
log.info(
"average training time: %.4f s/batch (exclude first %d batches)",
self.total_train_time
/ (
self.num_steps // self.disp_freq * self.disp_freq
- self.disp_freq
),
self.disp_freq,
)
else:
log.info(
"average training time: %.4f s/batch",
self.total_train_time
/ (self.num_steps // self.disp_freq * self.disp_freq),