Skip to content

Commit 3701566

Browse files
authored
fix(dp/pt): support auto sel for dpa2 (#4323)
Fix #4314 . <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Enhanced selection update functionality to accommodate three-body interactions. - Expanded argument handling to allow flexible input types for neighbor selection parameters. - **Bug Fixes** - Improved error handling and robustness in the selection update process. - **Documentation** - Updated documentation strings for clarity on new input types and usage constraints. - **Tests** - Added a new test method to validate the functionality of the updated selection method for the `dpa2` model. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 6c66be9 commit 3701566

File tree

4 files changed

+75
-6
lines changed

4 files changed

+75
-6
lines changed

deepmd/dpmodel/descriptor/dpa2.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,14 @@ def update_sel(
10571057
True,
10581058
)
10591059
local_jdata_cpy["repinit"]["nsel"] = repinit_sel[0]
1060+
min_nbor_dist, repinit_three_body_sel = update_sel.update_one_sel(
1061+
train_data,
1062+
type_map,
1063+
local_jdata_cpy["repinit"]["three_body_rcut"],
1064+
local_jdata_cpy["repinit"]["three_body_sel"],
1065+
True,
1066+
)
1067+
local_jdata_cpy["repinit"]["three_body_sel"] = repinit_three_body_sel[0]
10601068
min_nbor_dist, repformer_sel = update_sel.update_one_sel(
10611069
train_data,
10621070
type_map,

deepmd/pt/model/descriptor/dpa2.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,14 @@ def update_sel(
842842
True,
843843
)
844844
local_jdata_cpy["repinit"]["nsel"] = repinit_sel[0]
845+
min_nbor_dist, repinit_three_body_sel = update_sel.update_one_sel(
846+
train_data,
847+
type_map,
848+
local_jdata_cpy["repinit"]["three_body_rcut"],
849+
local_jdata_cpy["repinit"]["three_body_sel"],
850+
True,
851+
)
852+
local_jdata_cpy["repinit"]["three_body_sel"] = repinit_three_body_sel[0]
845853
min_nbor_dist, repformer_sel = update_sel.update_one_sel(
846854
train_data,
847855
type_map,

deepmd/utils/argcheck.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,9 @@ def dpa2_repinit_args():
940940
# repinit args
941941
doc_rcut = "The cut-off radius."
942942
doc_rcut_smth = "Where to start smoothing. For example the 1/r term is smoothed from `rcut` to `rcut_smth`."
943-
doc_nsel = "Maximally possible number of selected neighbors."
943+
doc_nsel = 'Maximally possible number of selected neighbors. It can be:\n\n\
944+
- `int`. The maximum number of neighbor atoms to be considered. We recommend it to be less than 200. \n\n\
945+
- `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 wrapped up to 4 divisible. The option "auto" is equivalent to "auto:1.1".'
944946
doc_neuron = (
945947
"Number of neurons in each hidden layers of the embedding net."
946948
"When two layers are of the same size or one layer is twice as large as the previous layer, "
@@ -972,15 +974,17 @@ def dpa2_repinit_args():
972974
"When two layers are of the same size or one layer is twice as large as the previous layer, "
973975
"a skip connection is built."
974976
)
975-
doc_three_body_sel = "Maximally possible number of selected neighbors in the three-body representation."
977+
doc_three_body_sel = 'Maximally possible number of selected neighbors in the three-body representation. It can be:\n\n\
978+
- `int`. The maximum number of neighbor atoms to be considered. We recommend it to be less than 200. \n\n\
979+
- `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 wrapped up to 4 divisible. The option "auto" is equivalent to "auto:1.1".'
976980
doc_three_body_rcut = "The cut-off radius in the three-body representation."
977981
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`."
978982

979983
return [
980984
# repinit args
981985
Argument("rcut", float, doc=doc_rcut),
982986
Argument("rcut_smth", float, doc=doc_rcut_smth),
983-
Argument("nsel", int, doc=doc_nsel),
987+
Argument("nsel", [int, str], doc=doc_nsel),
984988
Argument(
985989
"neuron",
986990
list,
@@ -1066,7 +1070,11 @@ def dpa2_repinit_args():
10661070
doc=doc_three_body_rcut_smth,
10671071
),
10681072
Argument(
1069-
"three_body_sel", int, optional=True, default=40, doc=doc_three_body_sel
1073+
"three_body_sel",
1074+
[int, str],
1075+
optional=True,
1076+
default=40,
1077+
doc=doc_three_body_sel,
10701078
),
10711079
]
10721080

@@ -1076,7 +1084,9 @@ def dpa2_repformer_args():
10761084
# repformer args
10771085
doc_rcut = "The cut-off radius."
10781086
doc_rcut_smth = "Where to start smoothing. For example the 1/r term is smoothed from `rcut` to `rcut_smth`."
1079-
doc_nsel = "Maximally possible number of selected neighbors."
1087+
doc_nsel = 'Maximally possible number of selected neighbors. It can be:\n\n\
1088+
- `int`. The maximum number of neighbor atoms to be considered. We recommend it to be less than 200. \n\n\
1089+
- `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 wrapped up to 4 divisible. The option "auto" is equivalent to "auto:1.1".'
10801090
doc_nlayers = "The number of repformer layers."
10811091
doc_g1_dim = "The dimension of invariant single-atom representation."
10821092
doc_g2_dim = "The dimension of invariant pair-atom representation."
@@ -1139,7 +1149,7 @@ def dpa2_repformer_args():
11391149
# repformer args
11401150
Argument("rcut", float, doc=doc_rcut),
11411151
Argument("rcut_smth", float, doc=doc_rcut_smth),
1142-
Argument("nsel", int, doc=doc_nsel),
1152+
Argument("nsel", [int, str], doc=doc_nsel),
11431153
Argument(
11441154
"nlayers",
11451155
int,

source/tests/pt/test_update_sel.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,49 @@ def test_update_sel_atten_list(self, sel_mock):
170170
jdata = update_sel(jdata)
171171
self.assertEqual(jdata, expected_out)
172172

173+
@patch("deepmd.pt.utils.update_sel.UpdateSel.get_nbor_stat")
174+
def test_update_sel_dpa2_auto(self, sel_mock):
175+
sel_mock.return_value = self.mock_min_nbor_dist, [25]
176+
177+
jdata = {
178+
"model": {
179+
"descriptor": {
180+
"type": "dpa2",
181+
"repinit": {
182+
"rcut": 6.0,
183+
"nsel": "auto",
184+
"three_body_rcut": 4.0,
185+
"three_body_sel": "auto",
186+
},
187+
"repformer": {
188+
"rcut": 4.0,
189+
"nsel": "auto",
190+
},
191+
}
192+
},
193+
"training": {"training_data": {}},
194+
}
195+
expected_out = {
196+
"model": {
197+
"descriptor": {
198+
"type": "dpa2",
199+
"repinit": {
200+
"rcut": 6.0,
201+
"nsel": 28,
202+
"three_body_rcut": 4.0,
203+
"three_body_sel": 28,
204+
},
205+
"repformer": {
206+
"rcut": 4.0,
207+
"nsel": 28,
208+
},
209+
}
210+
},
211+
"training": {"training_data": {}},
212+
}
213+
jdata = update_sel(jdata)
214+
self.assertEqual(jdata, expected_out)
215+
173216
def test_skip_frozen(self):
174217
jdata = {
175218
"model": {

0 commit comments

Comments
 (0)