forked from deepmodeling/deepmd-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargcheck.py
2952 lines (2752 loc) · 127 KB
/
argcheck.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 json
import logging
import warnings
from typing import (
Callable,
List,
Optional,
Union,
)
from dargs import (
Argument,
ArgumentEncoder,
Variant,
dargs,
)
from dargs.json_schema import (
generate_json_schema,
)
from deepmd import (
__version__,
)
from deepmd.common import (
VALID_ACTIVATION,
VALID_PRECISION,
)
from deepmd.utils.argcheck_nvnmd import (
nvnmd_args,
)
from deepmd.utils.plugin import (
Plugin,
)
log = logging.getLogger(__name__)
ACTIVATION_FN_DICT = dict.fromkeys(VALID_ACTIVATION)
PRECISION_DICT = dict.fromkeys(VALID_PRECISION)
doc_only_tf_supported = "(Supported Backend: TensorFlow) "
doc_only_pt_supported = "(Supported Backend: PyTorch) "
def list_to_doc(xx):
items = []
for ii in xx:
if len(items) == 0:
items.append(f'"{ii}"')
else:
items.append(f', "{ii}"')
items.append(".")
return "".join(items)
def make_link(content, ref_key):
return (
f"`{content} <{ref_key}_>`_"
if not dargs.RAW_ANCHOR
else f"`{content} <#{ref_key}>`_"
)
def deprecate_argument_extra_check(key: str) -> Callable[[dict], bool]:
"""Generate an extra check to deprecate an argument in sub fields.
Parameters
----------
key : str
The name of the deprecated argument.
"""
def deprecate_something(data: Optional[dict]):
if data is not None and key in data:
warnings.warn(f"{key} has been removed and takes no effect.", FutureWarning)
data.pop(key)
return True
return deprecate_something
def type_embedding_args():
doc_neuron = "Number of neurons in each hidden layers of the embedding net. When two layers are of the same size or one layer is twice as large as the previous layer, a skip connection is built."
doc_resnet_dt = 'Whether to use a "Timestep" in the skip connection'
doc_seed = "Random seed for parameter initialization"
doc_activation_function = f'The activation function in the embedding net. Supported activation functions are {list_to_doc(ACTIVATION_FN_DICT.keys())} Note that "gelu" denotes the custom operator version, and "gelu_tf" denotes the TF standard version. If you set "None" or "none" here, no activation function will be used.'
doc_precision = f"The precision of the embedding net parameters, supported options are {list_to_doc(PRECISION_DICT.keys())} Default follows the interface precision."
doc_trainable = "If the parameters in the embedding net are trainable"
doc_use_econf_tebd = "Whether to use electronic configuration type embedding."
doc_use_tebd_bias = "Whether to use bias in the type embedding layer."
return [
Argument("neuron", List[int], optional=True, default=[8], doc=doc_neuron),
Argument(
"activation_function",
str,
optional=True,
default="tanh",
doc=doc_activation_function,
),
Argument("resnet_dt", bool, optional=True, default=False, doc=doc_resnet_dt),
Argument("precision", str, optional=True, default="default", doc=doc_precision),
Argument("trainable", bool, optional=True, default=True, doc=doc_trainable),
Argument("seed", [int, None], optional=True, default=None, doc=doc_seed),
Argument(
"use_econf_tebd", bool, optional=True, default=False, doc=doc_use_econf_tebd
),
Argument(
"use_tebd_bias",
bool,
optional=True,
default=False,
doc=doc_use_tebd_bias,
),
]
def spin_args():
doc_use_spin = (
"Whether to use atomic spin model for each atom type. "
"List of boolean values with the shape of [ntypes] to specify which types use spin, "
f"or a list of integer values {doc_only_pt_supported} "
"to indicate the index of the type that uses spin."
)
doc_spin_norm = "The magnitude of atomic spin for each atom type with spin"
doc_virtual_len = "The distance between virtual atom representing spin and its corresponding real atom for each atom type with spin"
doc_virtual_scale = (
"The scaling factor to determine the virtual distance between a virtual atom "
"representing spin and its corresponding real atom for each atom type with spin. "
"This factor is defined as the virtual distance divided by the magnitude of atomic spin "
"for each atom type with spin. The virtual coordinate is defined as the real coordinate "
"plus spin * virtual_scale. List of float values with shape of [ntypes] or [ntypes_spin] "
"or one single float value for all types, only used when use_spin is True for each atom type."
)
return [
Argument("use_spin", [List[bool], List[int]], doc=doc_use_spin),
Argument(
"spin_norm",
List[float],
optional=True,
doc=doc_only_tf_supported + doc_spin_norm,
),
Argument(
"virtual_len",
List[float],
optional=True,
doc=doc_only_tf_supported + doc_virtual_len,
),
Argument(
"virtual_scale",
[List[float], float],
optional=True,
doc=doc_only_pt_supported + doc_virtual_scale,
),
]
# --- Descriptor configurations: --- #
class ArgsPlugin:
def __init__(self) -> None:
self.__plugin = Plugin()
def register(
self, name: str, alias: Optional[List[str]] = None, doc: str = ""
) -> Callable[
[Union[Callable[[], Argument], Callable[[], List[Argument]]]],
Union[Callable[[], Argument], Callable[[], List[Argument]]],
]:
"""Register a descriptor argument plugin.
Parameters
----------
name : str
the name of a descriptor
alias : List[str], optional
the list of aliases of this descriptor
Returns
-------
Callable[[Union[Callable[[], Argument], Callable[[], List[Argument]]]], Union[Callable[[], Argument], Callable[[], List[Argument]]]]
decorator to return the registered descriptor argument method
Examples
--------
>>> some_plugin = ArgsPlugin()
>>> @some_plugin.register("some_descrpt")
def descrpt_some_descrpt_args():
return []
"""
# convert alias to hashed item
if isinstance(alias, list):
alias = tuple(alias)
return self.__plugin.register((name, alias, doc))
def get_all_argument(self, exclude_hybrid: bool = False) -> List[Argument]:
"""Get all arguments.
Parameters
----------
exclude_hybrid : bool
exclude hybrid descriptor to prevent circular calls
Returns
-------
List[Argument]
all arguments
"""
arguments = []
for (name, alias, doc), metd in self.__plugin.plugins.items():
if exclude_hybrid and name == "hybrid":
continue
args = metd()
if isinstance(args, Argument):
arguments.append(args)
elif isinstance(args, list):
arguments.append(
Argument(
name=name, dtype=dict, sub_fields=metd(), alias=alias, doc=doc
)
)
else:
raise ValueError(f"Invalid return type {type(args)}")
return arguments
descrpt_args_plugin = ArgsPlugin()
@descrpt_args_plugin.register("loc_frame", doc=doc_only_tf_supported)
def descrpt_local_frame_args():
doc_sel_a = "A list of integers. The length of the list should be the same as the number of atom types in the system. `sel_a[i]` gives the selected number of type-i neighbors. The full relative coordinates of the neighbors are used by the descriptor."
doc_sel_r = "A list of integers. The length of the list should be the same as the number of atom types in the system. `sel_r[i]` gives the selected number of type-i neighbors. Only relative distance of the neighbors are used by the descriptor. sel_a[i] + sel_r[i] is recommended to be larger than the maximally possible number of type-i neighbors in the cut-off radius."
doc_rcut = "The cut-off radius. The default value is 6.0"
doc_axis_rule = "A list of integers. The length should be 6 times of the number of types. \n\n\
- axis_rule[i*6+0]: class of the atom defining the first axis of type-i atom. 0 for neighbors with full coordinates and 1 for neighbors only with relative distance.\n\n\
- axis_rule[i*6+1]: type of the atom defining the first axis of type-i atom.\n\n\
- axis_rule[i*6+2]: index of the axis atom defining the first axis. Note that the neighbors with the same class and type are sorted according to their relative distance.\n\n\
- axis_rule[i*6+3]: class of the atom defining the second axis of type-i atom. 0 for neighbors with full coordinates and 1 for neighbors only with relative distance.\n\n\
- axis_rule[i*6+4]: type of the atom defining the second axis of type-i atom.\n\n\
- axis_rule[i*6+5]: index of the axis atom defining the second axis. Note that the neighbors with the same class and type are sorted according to their relative distance."
return [
Argument("sel_a", List[int], optional=False, doc=doc_sel_a),
Argument("sel_r", List[int], optional=False, doc=doc_sel_r),
Argument("rcut", float, optional=True, default=6.0, doc=doc_rcut),
Argument("axis_rule", List[int], optional=False, doc=doc_axis_rule),
]
@descrpt_args_plugin.register("se_e2_a", alias=["se_a"])
def descrpt_se_a_args():
doc_sel = 'This parameter set the number of selected neighbors for each type of atom. It can be:\n\n\
- `List[int]`. The length of the list should be the same as the number of atom types in the system. `sel[i]` gives the selected number of type-i neighbors. `sel[i]` is recommended to be larger than the maximally possible number of type-i neighbors in the cut-off radius. It is noted that the total sel value must be less than 4096 in a GPU environment.\n\n\
- `str`. Can be "auto:factor" or "auto". "factor" is a float number larger than 1. This option will automatically determine the `sel`. In detail it counts the maximal number of neighbors with in the cutoff radius for each type of neighbor, then multiply the maximum by the "factor". Finally the number is wraped up to 4 divisible. The option "auto" is equivalent to "auto:1.1".'
doc_rcut = "The cut-off radius."
doc_rcut_smth = "Where to start smoothing. For example the 1/r term is smoothed from `rcut` to `rcut_smth`"
doc_neuron = "Number of neurons in each hidden layers of the embedding net. When two layers are of the same size or one layer is twice as large as the previous layer, a skip connection is built."
doc_axis_neuron = "Size of the submatrix of G (embedding matrix)."
doc_activation_function = f'The activation function in the embedding net. Supported activation functions are {list_to_doc(ACTIVATION_FN_DICT.keys())} Note that "gelu" denotes the custom operator version, and "gelu_tf" denotes the TF standard version. If you set "None" or "none" here, no activation function will be used.'
doc_resnet_dt = 'Whether to use a "Timestep" in the skip connection'
doc_type_one_side = r"If true, the embedding network parameters vary by types of neighbor atoms only, so there will be $N_\text{types}$ sets of embedding network parameters. Otherwise, the embedding network parameters vary by types of centric atoms and types of neighbor atoms, so there will be $N_\text{types}^2$ sets of embedding network parameters."
doc_precision = f"The precision of the embedding net parameters, supported options are {list_to_doc(PRECISION_DICT.keys())} Default follows the interface precision."
doc_trainable = "If the parameters in the embedding net is trainable"
doc_seed = "Random seed for parameter initialization"
doc_exclude_types = "The excluded pairs of types which have no interaction with each other. For example, `[[0, 1]]` means no interaction between type 0 and type 1."
doc_env_protection = "Protection parameter to prevent division by zero errors during environment matrix calculations. For example, when using paddings, there may be zero distances of neighbors, which may make division by zero error during environment matrix calculations without protection."
doc_set_davg_zero = "Set the normalization average to zero. This option should be set when `atom_ener` in the energy fitting is used"
return [
Argument("sel", [List[int], str], optional=True, default="auto", doc=doc_sel),
Argument("rcut", float, optional=True, default=6.0, doc=doc_rcut),
Argument("rcut_smth", float, optional=True, default=0.5, doc=doc_rcut_smth),
Argument(
"neuron", List[int], optional=True, default=[10, 20, 40], doc=doc_neuron
),
Argument(
"axis_neuron",
int,
optional=True,
default=4,
alias=["n_axis_neuron"],
doc=doc_axis_neuron,
),
Argument(
"activation_function",
str,
optional=True,
default="tanh",
doc=doc_activation_function,
),
Argument("resnet_dt", bool, optional=True, default=False, doc=doc_resnet_dt),
Argument(
"type_one_side", bool, optional=True, default=False, doc=doc_type_one_side
),
Argument("precision", str, optional=True, default="default", doc=doc_precision),
Argument("trainable", bool, optional=True, default=True, doc=doc_trainable),
Argument("seed", [int, None], optional=True, doc=doc_seed),
Argument(
"exclude_types",
List[List[int]],
optional=True,
default=[],
doc=doc_exclude_types,
),
Argument(
"env_protection",
float,
optional=True,
default=0.0,
doc=doc_only_pt_supported + doc_env_protection,
),
Argument(
"set_davg_zero", bool, optional=True, default=False, doc=doc_set_davg_zero
),
]
@descrpt_args_plugin.register("se_e3", alias=["se_at", "se_a_3be", "se_t"])
def descrpt_se_t_args():
doc_sel = 'This parameter set the number of selected neighbors for each type of atom. It can be:\n\n\
- `List[int]`. The length of the list should be the same as the number of atom types in the system. `sel[i]` gives the selected number of type-i neighbors. `sel[i]` is recommended to be larger than the maximally possible number of type-i neighbors in the cut-off radius. It is noted that the total sel value must be less than 4096 in a GPU environment.\n\n\
- `str`. Can be "auto:factor" or "auto". "factor" is a float number larger than 1. This option will automatically determine the `sel`. In detail it counts the maximal number of neighbors with in the cutoff radius for each type of neighbor, then multiply the maximum by the "factor". Finally the number is wraped up to 4 divisible. The option "auto" is equivalent to "auto:1.1".'
doc_rcut = "The cut-off radius."
doc_rcut_smth = "Where to start smoothing. For example the 1/r term is smoothed from `rcut` to `rcut_smth`"
doc_neuron = "Number of neurons in each hidden layers of the embedding net. When two layers are of the same size or one layer is twice as large as the previous layer, a skip connection is built."
doc_activation_function = f'The activation function in the embedding net. Supported activation functions are {list_to_doc(ACTIVATION_FN_DICT.keys())} Note that "gelu" denotes the custom operator version, and "gelu_tf" denotes the TF standard version. If you set "None" or "none" here, no activation function will be used.'
doc_resnet_dt = 'Whether to use a "Timestep" in the skip connection'
doc_precision = f"The precision of the embedding net parameters, supported options are {list_to_doc(PRECISION_DICT.keys())} Default follows the interface precision."
doc_trainable = "If the parameters in the embedding net are trainable"
doc_seed = "Random seed for parameter initialization"
doc_set_davg_zero = "Set the normalization average to zero. This option should be set when `atom_ener` in the energy fitting is used"
doc_exclude_types = "The excluded pairs of types which have no interaction with each other. For example, `[[0, 1]]` means no interaction between type 0 and type 1."
doc_env_protection = "Protection parameter to prevent division by zero errors during environment matrix calculations. For example, when using paddings, there may be zero distances of neighbors, which may make division by zero error during environment matrix calculations without protection."
return [
Argument("sel", [List[int], str], optional=True, default="auto", doc=doc_sel),
Argument("rcut", float, optional=True, default=6.0, doc=doc_rcut),
Argument("rcut_smth", float, optional=True, default=0.5, doc=doc_rcut_smth),
Argument(
"neuron", List[int], optional=True, default=[10, 20, 40], doc=doc_neuron
),
Argument(
"activation_function",
str,
optional=True,
default="tanh",
doc=doc_activation_function,
),
Argument("resnet_dt", bool, optional=True, default=False, doc=doc_resnet_dt),
Argument("precision", str, optional=True, default="default", doc=doc_precision),
Argument("trainable", bool, optional=True, default=True, doc=doc_trainable),
Argument("seed", [int, None], optional=True, doc=doc_seed),
Argument(
"set_davg_zero", bool, optional=True, default=False, doc=doc_set_davg_zero
),
Argument(
"exclude_types",
List[List[int]],
optional=True,
default=[],
doc=doc_exclude_types,
),
Argument(
"env_protection",
float,
optional=True,
default=0.0,
doc=doc_only_pt_supported + doc_env_protection,
),
]
@descrpt_args_plugin.register("se_a_tpe", alias=["se_a_ebd"], doc=doc_only_tf_supported)
def descrpt_se_a_tpe_args():
doc_type_nchanl = "number of channels for type embedding"
doc_type_nlayer = "number of hidden layers of type embedding net"
doc_numb_aparam = "dimension of atomic parameter. if set to a value > 0, the atomic parameters are embedded."
return [
*descrpt_se_a_args(),
Argument("type_nchanl", int, optional=True, default=4, doc=doc_type_nchanl),
Argument("type_nlayer", int, optional=True, default=2, doc=doc_type_nlayer),
Argument("numb_aparam", int, optional=True, default=0, doc=doc_numb_aparam),
]
@descrpt_args_plugin.register("se_e2_r", alias=["se_r"])
def descrpt_se_r_args():
doc_sel = 'This parameter set the number of selected neighbors for each type of atom. It can be:\n\n\
- `List[int]`. The length of the list should be the same as the number of atom types in the system. `sel[i]` gives the selected number of type-i neighbors. `sel[i]` is recommended to be larger than the maximally possible number of type-i neighbors in the cut-off radius. It is noted that the total sel value must be less than 4096 in a GPU environment.\n\n\
- `str`. Can be "auto:factor" or "auto". "factor" is a float number larger than 1. This option will automatically determine the `sel`. In detail it counts the maximal number of neighbors with in the cutoff radius for each type of neighbor, then multiply the maximum by the "factor". Finally the number is wraped up to 4 divisible. The option "auto" is equivalent to "auto:1.1".'
doc_rcut = "The cut-off radius."
doc_rcut_smth = "Where to start smoothing. For example the 1/r term is smoothed from `rcut` to `rcut_smth`"
doc_neuron = "Number of neurons in each hidden layers of the embedding net. When two layers are of the same size or one layer is twice as large as the previous layer, a skip connection is built."
doc_activation_function = f'The activation function in the embedding net. Supported activation functions are {list_to_doc(ACTIVATION_FN_DICT.keys())} Note that "gelu" denotes the custom operator version, and "gelu_tf" denotes the TF standard version. If you set "None" or "none" here, no activation function will be used.'
doc_resnet_dt = 'Whether to use a "Timestep" in the skip connection'
doc_type_one_side = r"If true, the embedding network parameters vary by types of neighbor atoms only, so there will be $N_\text{types}$ sets of embedding network parameters. Otherwise, the embedding network parameters vary by types of centric atoms and types of neighbor atoms, so there will be $N_\text{types}^2$ sets of embedding network parameters."
doc_precision = f"The precision of the embedding net parameters, supported options are {list_to_doc(PRECISION_DICT.keys())} Default follows the interface precision."
doc_trainable = "If the parameters in the embedding net are trainable"
doc_seed = "Random seed for parameter initialization"
doc_exclude_types = "The excluded pairs of types which have no interaction with each other. For example, `[[0, 1]]` means no interaction between type 0 and type 1."
doc_set_davg_zero = "Set the normalization average to zero. This option should be set when `atom_ener` in the energy fitting is used"
doc_env_protection = "Protection parameter to prevent division by zero errors during environment matrix calculations. For example, when using paddings, there may be zero distances of neighbors, which may make division by zero error during environment matrix calculations without protection."
return [
Argument("sel", [List[int], str], optional=True, default="auto", doc=doc_sel),
Argument("rcut", float, optional=True, default=6.0, doc=doc_rcut),
Argument("rcut_smth", float, optional=True, default=0.5, doc=doc_rcut_smth),
Argument(
"neuron", List[int], optional=True, default=[10, 20, 40], doc=doc_neuron
),
Argument(
"activation_function",
str,
optional=True,
default="tanh",
doc=doc_activation_function,
),
Argument("resnet_dt", bool, optional=True, default=False, doc=doc_resnet_dt),
Argument(
"type_one_side", bool, optional=True, default=False, doc=doc_type_one_side
),
Argument("precision", str, optional=True, default="default", doc=doc_precision),
Argument("trainable", bool, optional=True, default=True, doc=doc_trainable),
Argument("seed", [int, None], optional=True, doc=doc_seed),
Argument(
"exclude_types",
List[List[int]],
optional=True,
default=[],
doc=doc_exclude_types,
),
Argument(
"set_davg_zero", bool, optional=True, default=False, doc=doc_set_davg_zero
),
Argument(
"env_protection",
float,
optional=True,
default=0.0,
doc=doc_only_pt_supported + doc_env_protection,
),
]
@descrpt_args_plugin.register("hybrid")
def descrpt_hybrid_args():
doc_list = "A list of descriptor definitions"
return [
Argument(
"list",
list,
optional=False,
doc=doc_list,
repeat=True,
sub_fields=[],
sub_variants=[descrpt_variant_type_args(exclude_hybrid=True)],
fold_subdoc=True,
)
]
def descrpt_se_atten_common_args():
doc_sel = 'This parameter set the number of selected neighbors. Note that this parameter is a little different from that in other descriptors. Instead of separating each type of atoms, only the summation matters. And this number is highly related with the efficiency, thus one should not make it too large. Usually 200 or less is enough, far away from the GPU limitation 4096. It can be:\n\n\
- `int`. The maximum number of neighbor atoms to be considered. We recommend it to be less than 200. \n\n\
- `List[int]`. The length of the list should be the same as the number of atom types in the system. `sel[i]` gives the selected number of type-i neighbors. Only the summation of `sel[i]` matters, and it is recommended to be less than 200.\
- `str`. Can be "auto:factor" or "auto". "factor" is a float number larger than 1. This option will automatically determine the `sel`. In detail it counts the maximal number of neighbors with in the cutoff radius for each type of neighbor, then multiply the maximum by the "factor". Finally the number is wraped up to 4 divisible. The option "auto" is equivalent to "auto:1.1".'
doc_rcut = "The cut-off radius."
doc_rcut_smth = "Where to start smoothing. For example the 1/r term is smoothed from `rcut` to `rcut_smth`"
doc_neuron = "Number of neurons in each hidden layers of the embedding net. When two layers are of the same size or one layer is twice as large as the previous layer, a skip connection is built."
doc_axis_neuron = "Size of the submatrix of G (embedding matrix)."
doc_activation_function = f'The activation function in the embedding net. Supported activation functions are {list_to_doc(ACTIVATION_FN_DICT.keys())} Note that "gelu" denotes the custom operator version, and "gelu_tf" denotes the TF standard version. If you set "None" or "none" here, no activation function will be used.'
doc_resnet_dt = 'Whether to use a "Timestep" in the skip connection'
doc_type_one_side = r"If 'False', type embeddings of both neighbor and central atoms are considered. If 'True', only type embeddings of neighbor atoms are considered. Default is 'False'."
doc_precision = f"The precision of the embedding net parameters, supported options are {list_to_doc(PRECISION_DICT.keys())} Default follows the interface precision."
doc_trainable = "If the parameters in the embedding net is trainable"
doc_seed = "Random seed for parameter initialization"
doc_exclude_types = "The excluded pairs of types which have no interaction with each other. For example, `[[0, 1]]` means no interaction between type 0 and type 1."
doc_env_protection = "Protection parameter to prevent division by zero errors during environment matrix calculations. For example, when using paddings, there may be zero distances of neighbors, which may make division by zero error during environment matrix calculations without protection."
doc_attn = "The length of hidden vectors in attention layers"
doc_attn_layer = "The number of attention layers. Note that model compression of `se_atten` is only enabled when attn_layer==0 and tebd_input_mode=='strip'"
doc_attn_dotr = "Whether to do dot product with the normalized relative coordinates"
doc_attn_mask = "Whether to do mask on the diagonal in the attention matrix"
return [
Argument(
"sel", [int, List[int], str], optional=True, default="auto", doc=doc_sel
),
Argument("rcut", float, optional=True, default=6.0, doc=doc_rcut),
Argument("rcut_smth", float, optional=True, default=0.5, doc=doc_rcut_smth),
Argument(
"neuron", List[int], optional=True, default=[10, 20, 40], doc=doc_neuron
),
Argument(
"axis_neuron",
int,
optional=True,
default=4,
alias=["n_axis_neuron"],
doc=doc_axis_neuron,
),
Argument(
"activation_function",
str,
optional=True,
default="tanh",
doc=doc_activation_function,
),
Argument("resnet_dt", bool, optional=True, default=False, doc=doc_resnet_dt),
Argument(
"type_one_side", bool, optional=True, default=False, doc=doc_type_one_side
),
Argument("precision", str, optional=True, default="default", doc=doc_precision),
Argument("trainable", bool, optional=True, default=True, doc=doc_trainable),
Argument("seed", [int, None], optional=True, doc=doc_seed),
Argument(
"exclude_types",
List[List[int]],
optional=True,
default=[],
doc=doc_exclude_types,
),
Argument(
"env_protection",
float,
optional=True,
default=0.0,
doc=doc_only_pt_supported + doc_env_protection,
),
Argument("attn", int, optional=True, default=128, doc=doc_attn),
Argument("attn_layer", int, optional=True, default=2, doc=doc_attn_layer),
Argument("attn_dotr", bool, optional=True, default=True, doc=doc_attn_dotr),
Argument("attn_mask", bool, optional=True, default=False, doc=doc_attn_mask),
]
@descrpt_args_plugin.register("se_atten", alias=["dpa1"])
def descrpt_se_atten_args():
doc_smooth_type_embedding = f"Whether to use smooth process in attention weights calculation. {doc_only_tf_supported} When using stripped type embedding, whether to dot smooth factor on the network output of type embedding to keep the network smooth, instead of setting `set_davg_zero` to be True."
doc_set_davg_zero = "Set the normalization average to zero. This option should be set when `se_atten` descriptor or `atom_ener` in the energy fitting is used"
doc_trainable_ln = (
"Whether to use trainable shift and scale weights in layer normalization."
)
doc_ln_eps = "The epsilon value for layer normalization. The default value for TensorFlow is set to 1e-3 to keep consistent with keras while set to 1e-5 in PyTorch and DP implementation."
doc_tebd_dim = "The dimension of atom type embedding."
doc_use_econf_tebd = r"Whether to use electronic configuration type embedding. For TensorFlow backend, please set `use_econf_tebd` in `type_embedding` block instead."
doc_use_tebd_bias = "Whether to use bias in the type embedding layer."
doc_temperature = "The scaling factor of normalization in calculations of attention weights, which is used to scale the matmul(Q, K)."
doc_scaling_factor = (
"The scaling factor of normalization in calculations of attention weights, which is used to scale the matmul(Q, K). "
"If `temperature` is None, the scaling of attention weights is (N_hidden_dim * scaling_factor)**0.5. "
"Else, the scaling of attention weights is setting to `temperature`."
)
doc_normalize = (
"Whether to normalize the hidden vectors during attention calculation."
)
doc_concat_output_tebd = (
"Whether to concat type embedding at the output of the descriptor."
)
doc_tebd_input_mode = (
"The input mode of the type embedding. Supported modes are ['concat', 'strip']."
"- 'concat': Concatenate the type embedding with the smoothed radial information as the union input for the embedding network. "
"When `type_one_side` is False, the input is `input_ij = concat([r_ij, tebd_j, tebd_i])`. When `type_one_side` is True, the input is `input_ij = concat([r_ij, tebd_j])`. "
"The output is `out_ij = embeding(input_ij)` for the pair-wise representation of atom i with neighbor j."
"- 'strip': Use a separated embedding network for the type embedding and combine the output with the radial embedding network output. "
f"When `type_one_side` is False, the input is `input_t = concat([tebd_j, tebd_i])`. {doc_only_pt_supported} When `type_one_side` is True, the input is `input_t = tebd_j`. "
"The output is `out_ij = embeding_t(input_t) * embeding_s(r_ij) + embeding_s(r_ij)` for the pair-wise representation of atom i with neighbor j."
)
doc_stripped_type_embedding = (
"(Deprecated, kept only for compatibility.) Whether to strip the type embedding into a separate embedding network. "
"Setting this parameter to `True` is equivalent to setting `tebd_input_mode` to 'strip'. "
"Setting it to `False` is equivalent to setting `tebd_input_mode` to 'concat'."
"The default value is `None`, which means the `tebd_input_mode` setting will be used instead."
)
return [
*descrpt_se_atten_common_args(),
Argument(
"stripped_type_embedding",
bool,
optional=True,
default=None,
doc=doc_stripped_type_embedding,
),
Argument(
"smooth_type_embedding",
bool,
optional=True,
default=False,
alias=["smooth_type_embdding"],
doc=doc_smooth_type_embedding,
),
Argument(
"set_davg_zero", bool, optional=True, default=True, doc=doc_set_davg_zero
),
Argument(
"trainable_ln", bool, optional=True, default=True, doc=doc_trainable_ln
),
Argument("ln_eps", float, optional=True, default=None, doc=doc_ln_eps),
# pt only
Argument(
"tebd_dim",
int,
optional=True,
default=8,
doc=doc_only_pt_supported + doc_tebd_dim,
),
Argument(
"use_econf_tebd",
bool,
optional=True,
default=False,
doc=doc_only_pt_supported + doc_use_econf_tebd,
),
Argument(
"use_tebd_bias",
bool,
optional=True,
default=False,
doc=doc_use_tebd_bias,
),
Argument(
"tebd_input_mode",
str,
optional=True,
default="concat",
doc=doc_tebd_input_mode,
),
Argument(
"scaling_factor",
float,
optional=True,
default=1.0,
doc=doc_only_pt_supported + doc_scaling_factor,
),
Argument(
"normalize",
bool,
optional=True,
default=True,
doc=doc_only_pt_supported + doc_normalize,
),
Argument(
"temperature",
float,
optional=True,
doc=doc_only_pt_supported + doc_temperature,
),
Argument(
"concat_output_tebd",
bool,
optional=True,
default=True,
doc=doc_only_pt_supported + doc_concat_output_tebd,
),
]
@descrpt_args_plugin.register("se_e3_tebd", doc=doc_only_pt_supported)
def descrpt_se_e3_tebd_args():
doc_sel = 'This parameter set the number of selected neighbors. Note that this parameter is a little different from that in other descriptors. Instead of separating each type of atoms, only the summation matters. And this number is highly related with the efficiency, thus one should not make it too large. Usually 200 or less is enough, far away from the GPU limitation 4096. It can be:\n\n\
- `int`. The maximum number of neighbor atoms to be considered. We recommend it to be less than 200. \n\n\
- `List[int]`. The length of the list should be the same as the number of atom types in the system. `sel[i]` gives the selected number of type-i neighbors. Only the summation of `sel[i]` matters, and it is recommended to be less than 200.\
- `str`. Can be "auto:factor" or "auto". "factor" is a float number larger than 1. This option will automatically determine the `sel`. In detail it counts the maximal number of neighbors with in the cutoff radius for each type of neighbor, then multiply the maximum by the "factor". Finally the number is wraped up to 4 divisible. The option "auto" is equivalent to "auto:1.1".'
doc_rcut = "The cut-off radius."
doc_rcut_smth = "Where to start smoothing. For example the 1/r term is smoothed from `rcut` to `rcut_smth`"
doc_neuron = "Number of neurons in each hidden layers of the embedding net. When two layers are of the same size or one layer is twice as large as the previous layer, a skip connection is built."
doc_activation_function = f'The activation function in the embedding net. Supported activation functions are {list_to_doc(ACTIVATION_FN_DICT.keys())} Note that "gelu" denotes the custom operator version, and "gelu_tf" denotes the TF standard version. If you set "None" or "none" here, no activation function will be used.'
doc_resnet_dt = 'Whether to use a "Timestep" in the skip connection'
doc_precision = f"The precision of the embedding net parameters, supported options are {list_to_doc(PRECISION_DICT.keys())} Default follows the interface precision."
doc_trainable = "If the parameters in the embedding net is trainable"
doc_seed = "Random seed for parameter initialization"
doc_exclude_types = "The excluded pairs of types which have no interaction with each other. For example, `[[0, 1]]` means no interaction between type 0 and type 1."
doc_env_protection = "Protection parameter to prevent division by zero errors during environment matrix calculations. For example, when using paddings, there may be zero distances of neighbors, which may make division by zero error during environment matrix calculations without protection."
doc_smooth = "Whether to use smooth process in calculation when using stripped type embedding. Whether to dot smooth factor (both neighbors j and k) on the network output (out_jk) of type embedding to keep the network smooth, instead of setting `set_davg_zero` to be True."
doc_set_davg_zero = "Set the normalization average to zero. This option should be set when `atom_ener` in the energy fitting is used"
doc_tebd_dim = "The dimension of atom type embedding."
doc_use_econf_tebd = r"Whether to use electronic configuration type embedding."
doc_concat_output_tebd = (
"Whether to concat type embedding at the output of the descriptor."
)
doc_tebd_input_mode = (
"The input mode of the type embedding. Supported modes are ['concat', 'strip']."
"- 'concat': Concatenate the type embedding with the smoothed angular information as the union input for the embedding network. "
"The input is `input_jk = concat([angle_jk, tebd_j, tebd_k])`. "
"The output is `out_jk = embeding(input_jk)` for the three-body representation of atom i with neighbors j and k."
"- 'strip': Use a separated embedding network for the type embedding and combine the output with the angular embedding network output. "
"The input is `input_t = concat([tebd_j, tebd_k])`."
"The output is `out_jk = embeding_t(input_t) * embeding_s(angle_jk) + embeding_s(angle_jk)` for the three-body representation of atom i with neighbors j and k."
)
return [
Argument(
"sel", [int, List[int], str], optional=True, default="auto", doc=doc_sel
),
Argument("rcut", float, optional=True, default=6.0, doc=doc_rcut),
Argument("rcut_smth", float, optional=True, default=0.5, doc=doc_rcut_smth),
Argument(
"neuron", List[int], optional=True, default=[10, 20, 40], doc=doc_neuron
),
Argument(
"tebd_dim",
int,
optional=True,
default=8,
doc=doc_only_pt_supported + doc_tebd_dim,
),
Argument(
"tebd_input_mode",
str,
optional=True,
default="concat",
doc=doc_tebd_input_mode,
),
Argument("resnet_dt", bool, optional=True, default=False, doc=doc_resnet_dt),
Argument(
"set_davg_zero", bool, optional=True, default=True, doc=doc_set_davg_zero
),
Argument(
"activation_function",
str,
optional=True,
default="tanh",
doc=doc_activation_function,
),
Argument(
"env_protection",
float,
optional=True,
default=0.0,
doc=doc_only_pt_supported + doc_env_protection,
),
Argument(
"smooth",
bool,
optional=True,
default=True,
doc=doc_smooth,
),
Argument(
"exclude_types",
List[List[int]],
optional=True,
default=[],
doc=doc_exclude_types,
),
Argument("precision", str, optional=True, default="default", doc=doc_precision),
Argument("trainable", bool, optional=True, default=True, doc=doc_trainable),
Argument("seed", [int, None], optional=True, doc=doc_seed),
Argument(
"concat_output_tebd",
bool,
optional=True,
default=True,
doc=doc_only_pt_supported + doc_concat_output_tebd,
),
Argument(
"use_econf_tebd",
bool,
optional=True,
default=False,
doc=doc_only_pt_supported + doc_use_econf_tebd,
),
Argument(
"use_tebd_bias",
bool,
optional=True,
default=True,
),
]
@descrpt_args_plugin.register("se_atten_v2")
def descrpt_se_atten_v2_args():
doc_set_davg_zero = "Set the normalization average to zero. This option should be set when `se_atten` descriptor or `atom_ener` in the energy fitting is used"
doc_trainable_ln = (
"Whether to use trainable shift and scale weights in layer normalization."
)
doc_ln_eps = "The epsilon value for layer normalization. The default value for TensorFlow is set to 1e-3 to keep consistent with keras while set to 1e-5 in PyTorch and DP implementation."
doc_tebd_dim = "The dimension of atom type embedding."
doc_use_econf_tebd = r"Whether to use electronic configuration type embedding. For TensorFlow backend, please set `use_econf_tebd` in `type_embedding` block instead."
doc_use_tebd_bias = "Whether to use bias in the type embedding layer."
doc_temperature = "The scaling factor of normalization in calculations of attention weights, which is used to scale the matmul(Q, K)."
doc_scaling_factor = (
"The scaling factor of normalization in calculations of attention weights, which is used to scale the matmul(Q, K). "
"If `temperature` is None, the scaling of attention weights is (N_hidden_dim * scaling_factor)**0.5. "
"Else, the scaling of attention weights is setting to `temperature`."
)
doc_normalize = (
"Whether to normalize the hidden vectors during attention calculation."
)
doc_concat_output_tebd = (
"Whether to concat type embedding at the output of the descriptor."
)
return [
*descrpt_se_atten_common_args(),
Argument(
"set_davg_zero", bool, optional=True, default=False, doc=doc_set_davg_zero
),
Argument(
"trainable_ln", bool, optional=True, default=True, doc=doc_trainable_ln
),
Argument("ln_eps", float, optional=True, default=None, doc=doc_ln_eps),
# pt only
Argument(
"tebd_dim",
int,
optional=True,
default=8,
doc=doc_only_pt_supported + doc_tebd_dim,
),
Argument(
"use_econf_tebd",
bool,
optional=True,
default=False,
doc=doc_only_pt_supported + doc_use_econf_tebd,
),
Argument(
"use_tebd_bias",
bool,
optional=True,
default=False,
doc=doc_use_tebd_bias,
),
Argument(
"scaling_factor",
float,
optional=True,
default=1.0,
doc=doc_only_pt_supported + doc_scaling_factor,
),
Argument(
"normalize",
bool,
optional=True,
default=True,
doc=doc_only_pt_supported + doc_normalize,
),
Argument(
"temperature",
float,
optional=True,
doc=doc_only_pt_supported + doc_temperature,
),
Argument(
"concat_output_tebd",
bool,
optional=True,
default=True,
doc=doc_only_pt_supported + doc_concat_output_tebd,
),
]
@descrpt_args_plugin.register("dpa2", doc=doc_only_pt_supported)
def descrpt_dpa2_args():
# repinit args
doc_repinit = "The arguments used to initialize the repinit block."
# repformer args
doc_repformer = "The arguments used to initialize the repformer block."
# descriptor args
doc_concat_output_tebd = (
"Whether to concat type embedding at the output of the descriptor."
)
doc_precision = f"The precision of the embedding net parameters, supported options are {list_to_doc(PRECISION_DICT.keys())} Default follows the interface precision."
doc_smooth = (
"Whether to use smoothness in processes such as attention weights calculation."
)
doc_exclude_types = "The excluded pairs of types which have no interaction with each other. For example, `[[0, 1]]` means no interaction between type 0 and type 1."
doc_env_protection = "Protection parameter to prevent division by zero errors during environment matrix calculations. For example, when using paddings, there may be zero distances of neighbors, which may make division by zero error during environment matrix calculations without protection."
doc_trainable = "If the parameters in the embedding net is trainable."
doc_seed = "Random seed for parameter initialization."
doc_add_tebd_to_repinit_out = "Add type embedding to the output representation from repinit before inputting it into repformer."
doc_use_econf_tebd = "Whether to use electronic configuration type embedding."
doc_use_tebd_bias = "Whether to use bias in the type embedding layer."
return [
# repinit args
Argument("repinit", dict, dpa2_repinit_args(), doc=doc_repinit),
# repformer args
Argument("repformer", dict, dpa2_repformer_args(), doc=doc_repformer),
# descriptor args
Argument(
"concat_output_tebd",
bool,
optional=True,
default=True,
doc=doc_concat_output_tebd,
),
Argument("precision", str, optional=True, default="default", doc=doc_precision),
Argument("smooth", bool, optional=True, default=True, doc=doc_smooth),
Argument(
"exclude_types",
List[List[int]],
optional=True,
default=[],
doc=doc_exclude_types,
),
Argument(
"env_protection",
float,
optional=True,
default=0.0,
doc=doc_only_pt_supported + doc_env_protection,
),
Argument("trainable", bool, optional=True, default=True, doc=doc_trainable),
Argument("seed", [int, None], optional=True, doc=doc_seed),
Argument(
"add_tebd_to_repinit_out",
bool,
optional=True,
default=False,
alias=["repformer_add_type_ebd_to_seq"],
doc=doc_add_tebd_to_repinit_out,
),
Argument(
"use_econf_tebd",
bool,
optional=True,
default=False,
doc=doc_only_pt_supported + doc_use_econf_tebd,
),
Argument(
"use_tebd_bias",
bool,
optional=True,
default=False,
doc=doc_use_tebd_bias,
),
]
# repinit for dpa2
def dpa2_repinit_args():
# repinit args
doc_rcut = "The cut-off radius."
doc_rcut_smth = "Where to start smoothing. For example the 1/r term is smoothed from `rcut` to `rcut_smth`."
doc_nsel = "Maximally possible number of selected neighbors."
doc_neuron = (
"Number of neurons in each hidden layers of the embedding net."
"When two layers are of the same size or one layer is twice as large as the previous layer, "
"a skip connection is built."
)
doc_axis_neuron = "Size of the submatrix of G (embedding matrix)."
doc_tebd_dim = "The dimension of atom type embedding."
doc_tebd_input_mode = (
"The input mode of the type embedding. Supported modes are ['concat', 'strip']."
"- 'concat': Concatenate the type embedding with the smoothed radial information as the union input for the embedding network. "
"When `type_one_side` is False, the input is `input_ij = concat([r_ij, tebd_j, tebd_i])`. When `type_one_side` is True, the input is `input_ij = concat([r_ij, tebd_j])`. "
"The output is `out_ij = embeding(input_ij)` for the pair-wise representation of atom i with neighbor j."
"- 'strip': Use a separated embedding network for the type embedding and combine the output with the radial embedding network output. "
f"When `type_one_side` is False, the input is `input_t = concat([tebd_j, tebd_i])`. {doc_only_pt_supported} When `type_one_side` is True, the input is `input_t = tebd_j`. "
"The output is `out_ij = embeding_t(input_t) * embeding_s(r_ij) + embeding_s(r_ij)` for the pair-wise representation of atom i with neighbor j."
)
doc_set_davg_zero = (
"Set the normalization average to zero. "
"This option should be set when `atom_ener` in the energy fitting is used."
)
doc_activation_function = f"The activation function in the embedding net. Supported activation functions are {list_to_doc(ACTIVATION_FN_DICT.keys())}."
doc_type_one_side = r"If true, the embedding network parameters vary by types of neighbor atoms only, so there will be $N_\text{types}$ sets of embedding network parameters. Otherwise, the embedding network parameters vary by types of centric atoms and types of neighbor atoms, so there will be $N_\text{types}^2$ sets of embedding network parameters."
doc_resnet_dt = 'Whether to use a "Timestep" in the skip connection.'
doc_use_three_body = (
"Whether to concatenate three-body representation in the output descriptor."
)
doc_three_body_neuron = (
"Number of neurons in each hidden layers of the three-body embedding net."
"When two layers are of the same size or one layer is twice as large as the previous layer, "
"a skip connection is built."
)
doc_three_body_sel = "Maximally possible number of selected neighbors in the three-body representation."
doc_three_body_rcut = "The cut-off radius in the three-body representation."
doc_three_body_rcut_smth = "Where to start smoothing in the three-body representation. For example the 1/r term is smoothed from `three_body_rcut` to `three_body_rcut_smth`."
return [
# repinit args
Argument("rcut", float, doc=doc_rcut),
Argument("rcut_smth", float, doc=doc_rcut_smth),
Argument("nsel", int, doc=doc_nsel),
Argument(
"neuron",
list,
optional=True,
default=[25, 50, 100],
doc=doc_neuron,
),
Argument(
"axis_neuron",
int,
optional=True,
default=16,
doc=doc_axis_neuron,
),
Argument(
"tebd_dim",