Skip to content

Commit f29e7d8

Browse files
authored
Merge branch 'deepmodeling:master' into master
2 parents c6f9e91 + 2d4c615 commit f29e7d8

32 files changed

Lines changed: 1373 additions & 105 deletions

backend/find_pytorch.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ def find_pytorch() -> tuple[str | None, list[str]]:
4949
str, optional
5050
PyTorch library path if found.
5151
list of str
52-
TensorFlow requirement if not found. Empty if found.
52+
PyTorch requirement if not found. Empty if found.
5353
"""
54-
if os.environ.get("DP_ENABLE_PYTORCH", "0") == "0":
54+
if os.environ.get("DP_ENABLE_PYTORCH", "1") == "0":
5555
return None, []
5656
requires = []
5757
pt_spec = None

backend/read_env.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def get_argument_from_env() -> tuple[str, list, list, dict, str, str]:
104104
cmake_args.append("-DENABLE_TENSORFLOW=OFF")
105105
tf_version = None
106106

107-
if os.environ.get("DP_ENABLE_PYTORCH", "0") == "1":
107+
if os.environ.get("DP_ENABLE_PYTORCH", "1") == "1":
108108
pt_install_dir, _ = find_pytorch()
109109
pt_version = get_pt_version(pt_install_dir)
110110
cmake_args.extend(

deepmd/pt/cxx_op.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ def load_library(module_name: str) -> bool:
8383
f"incompatible and thus an error is raised when loading {module_name}. "
8484
f"You need to install PyTorch {PT_VERSION}, or rebuild deepmd-kit "
8585
f"against PyTorch {pt_py_version}.\nIf you are using a wheel from "
86-
"PyPI, you may consider to install deepmd-kit execuating "
87-
"`DP_ENABLE_PYTORCH=1 pip install deepmd-kit --no-binary deepmd-kit` "
86+
"PyPI, you may consider rebuilding deepmd-kit with "
87+
"`pip install deepmd-kit --no-binary deepmd-kit` "
8888
"instead."
8989
) from e
9090
error_message = (

deepmd/tf/descriptor/se_a.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@
7777
from .se import (
7878
DescrptSe,
7979
)
80+
from .stat import (
81+
load_or_compute_se_input_stats,
82+
)
8083

8184

8285
@Descriptor.register("se_e2_a")
@@ -374,7 +377,8 @@ def compute_input_stats(
374377
**kwargs
375378
Additional keyword arguments.
376379
"""
377-
if True:
380+
381+
def compute_stats() -> dict[str, Any]:
378382
sumr = []
379383
suma = []
380384
sumn = []
@@ -398,7 +402,16 @@ def compute_input_stats(
398402
"sumr2": sumr2,
399403
"suma2": suma2,
400404
}
401-
self.merge_input_stats(stat_dict)
405+
return stat_dict
406+
407+
stat_dict = load_or_compute_se_input_stats(
408+
self,
409+
kwargs.get("stat_file_path"),
410+
last_dim=4,
411+
compute=compute_stats,
412+
mixed_types=False,
413+
)
414+
self.merge_input_stats(stat_dict)
402415

403416
def merge_input_stats(self, stat_dict: dict[str, Any]) -> None:
404417
"""Merge the statistics computed from compute_input_stats to obtain the self.davg and self.dstd.

deepmd/tf/descriptor/se_atten.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@
9090
from .se_a import (
9191
DescrptSeA,
9292
)
93+
from .stat import (
94+
load_or_compute_se_input_stats,
95+
)
9396

9497
log = logging.getLogger(__name__)
9598

@@ -373,7 +376,8 @@ def compute_input_stats(
373376
**kwargs
374377
Additional keyword arguments.
375378
"""
376-
if True:
379+
380+
def compute_stats() -> dict[str, Any]:
377381
sumr = []
378382
suma = []
379383
sumn = []
@@ -418,7 +422,16 @@ def compute_input_stats(
418422
"sumr2": sumr2,
419423
"suma2": suma2,
420424
}
421-
self.merge_input_stats(stat_dict)
425+
return stat_dict
426+
427+
stat_dict = load_or_compute_se_input_stats(
428+
self,
429+
kwargs.get("stat_file_path"),
430+
last_dim=4,
431+
compute=compute_stats,
432+
mixed_types=True,
433+
)
434+
self.merge_input_stats(stat_dict)
422435

423436
def enable_compression(
424437
self,

deepmd/tf/descriptor/se_r.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@
5050
from .se import (
5151
DescrptSe,
5252
)
53+
from .stat import (
54+
load_or_compute_se_input_stats,
55+
)
5356

5457

5558
@Descriptor.register("se_e2_r")
@@ -274,17 +277,27 @@ def compute_input_stats(
274277
**kwargs
275278
Additional keyword arguments.
276279
"""
277-
sumr = []
278-
sumn = []
279-
sumr2 = []
280-
for cc, bb, tt, nn, mm in zip(
281-
data_coord, data_box, data_atype, natoms_vec, mesh, strict=True
282-
):
283-
sysr, sysr2, sysn = self._compute_dstats_sys_se_r(cc, bb, tt, nn, mm)
284-
sumr.append(sysr)
285-
sumn.append(sysn)
286-
sumr2.append(sysr2)
287-
stat_dict = {"sumr": sumr, "sumn": sumn, "sumr2": sumr2}
280+
281+
def compute_stats() -> dict[str, Any]:
282+
sumr = []
283+
sumn = []
284+
sumr2 = []
285+
for cc, bb, tt, nn, mm in zip(
286+
data_coord, data_box, data_atype, natoms_vec, mesh, strict=True
287+
):
288+
sysr, sysr2, sysn = self._compute_dstats_sys_se_r(cc, bb, tt, nn, mm)
289+
sumr.append(sysr)
290+
sumn.append(sysn)
291+
sumr2.append(sysr2)
292+
return {"sumr": sumr, "sumn": sumn, "sumr2": sumr2}
293+
294+
stat_dict = load_or_compute_se_input_stats(
295+
self,
296+
kwargs.get("stat_file_path"),
297+
last_dim=1,
298+
compute=compute_stats,
299+
mixed_types=False,
300+
)
288301
self.merge_input_stats(stat_dict)
289302

290303
def merge_input_stats(self, stat_dict: dict[str, Any]) -> None:

deepmd/tf/descriptor/se_t.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@
5252
from .se import (
5353
DescrptSe,
5454
)
55+
from .stat import (
56+
load_or_compute_se_input_stats,
57+
)
5558

5659

5760
@Descriptor.register("se_e3")
@@ -257,7 +260,8 @@ def compute_input_stats(
257260
**kwargs
258261
Additional keyword arguments.
259262
"""
260-
if True:
263+
264+
def compute_stats() -> dict[str, Any]:
261265
sumr = []
262266
suma = []
263267
sumn = []
@@ -281,7 +285,16 @@ def compute_input_stats(
281285
"sumr2": sumr2,
282286
"suma2": suma2,
283287
}
284-
self.merge_input_stats(stat_dict)
288+
return stat_dict
289+
290+
stat_dict = load_or_compute_se_input_stats(
291+
self,
292+
kwargs.get("stat_file_path"),
293+
last_dim=4,
294+
compute=compute_stats,
295+
mixed_types=False,
296+
)
297+
self.merge_input_stats(stat_dict)
285298

286299
def merge_input_stats(self, stat_dict: dict[str, Any]) -> None:
287300
"""Merge the statistics computed from compute_input_stats to obtain the self.davg and self.dstd.

deepmd/tf/descriptor/stat.py

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# SPDX-License-Identifier: LGPL-3.0-or-later
2+
from collections.abc import (
3+
Callable,
4+
)
5+
from typing import (
6+
Any,
7+
)
8+
9+
import numpy as np
10+
11+
from deepmd.common import (
12+
get_hash,
13+
)
14+
from deepmd.utils.path import (
15+
DPPath,
16+
)
17+
18+
19+
def _descriptor_rcut_smth(descrpt: Any) -> float:
20+
if hasattr(descrpt, "rcut_smth"):
21+
return descrpt.rcut_smth
22+
return descrpt.rcut_r_smth
23+
24+
25+
def _descriptor_sel(descrpt: Any, last_dim: int) -> list[int]:
26+
if hasattr(descrpt, "get_sel"):
27+
sel = descrpt.get_sel()
28+
elif last_dim == 1:
29+
sel = descrpt.sel_r
30+
else:
31+
sel = descrpt.sel_a
32+
if isinstance(sel, np.ndarray):
33+
sel = sel.tolist()
34+
elif isinstance(sel, int):
35+
sel = [sel]
36+
return [int(ii) for ii in sel]
37+
38+
39+
def _descriptor_stat_path(
40+
descrpt: Any,
41+
stat_file_path: DPPath | None,
42+
last_dim: int,
43+
mixed_types: bool,
44+
) -> DPPath | None:
45+
if stat_file_path is None:
46+
return None
47+
sel = _descriptor_sel(descrpt, last_dim)
48+
stat_hash = get_hash(
49+
{
50+
"type": "se_a" if last_dim == 4 else "se_r",
51+
"ntypes": descrpt.get_ntypes(),
52+
"rcut": round(descrpt.get_rcut(), 2),
53+
"rcut_smth": round(_descriptor_rcut_smth(descrpt), 2),
54+
"nsel": sum(sel),
55+
"sel": sel,
56+
"mixed_types": mixed_types,
57+
}
58+
)
59+
return stat_file_path / stat_hash
60+
61+
62+
def _stat_keys(ntypes: int, angular: bool) -> list[str]:
63+
keys = [f"r_{ii}" for ii in range(ntypes)]
64+
if angular:
65+
keys.extend(f"a_{ii}" for ii in range(ntypes))
66+
return keys
67+
68+
69+
def _load_se_input_stats(
70+
path: DPPath | None,
71+
ntypes: int,
72+
angular: bool,
73+
) -> dict[str, list[list[float]]] | None:
74+
if path is None or not path.is_dir():
75+
return None
76+
if any(not (path / kk).is_file() for kk in _stat_keys(ntypes, angular)):
77+
return None
78+
79+
sumr = []
80+
sumn = []
81+
sumr2 = []
82+
suma = []
83+
suma2 = []
84+
for type_i in range(ntypes):
85+
r_stat = (path / f"r_{type_i}").load_numpy()
86+
sumn.append(float(r_stat[0]))
87+
sumr.append(float(r_stat[1]))
88+
sumr2.append(float(r_stat[2]))
89+
if angular:
90+
a_stat = (path / f"a_{type_i}").load_numpy()
91+
suma.append(float(a_stat[1]) / 3.0)
92+
suma2.append(float(a_stat[2]) / 3.0)
93+
94+
ret = {
95+
"sumr": [sumr],
96+
"sumn": [sumn],
97+
"sumr2": [sumr2],
98+
}
99+
if angular:
100+
ret["suma"] = [suma]
101+
ret["suma2"] = [suma2]
102+
return ret
103+
104+
105+
def _save_se_input_stats(
106+
path: DPPath | None,
107+
stat_dict: dict[str, Any],
108+
ntypes: int,
109+
angular: bool,
110+
) -> None:
111+
if path is None:
112+
return
113+
path.mkdir(parents=True, exist_ok=True)
114+
115+
sumr = np.sum(stat_dict["sumr"], axis=0)
116+
sumn = np.sum(stat_dict["sumn"], axis=0)
117+
sumr2 = np.sum(stat_dict["sumr2"], axis=0)
118+
if angular:
119+
suma = np.sum(stat_dict["suma"], axis=0)
120+
suma2 = np.sum(stat_dict["suma2"], axis=0)
121+
122+
for type_i in range(ntypes):
123+
(path / f"r_{type_i}").save_numpy(
124+
np.array([sumn[type_i], sumr[type_i], sumr2[type_i]])
125+
)
126+
if angular:
127+
(path / f"a_{type_i}").save_numpy(
128+
np.array([3.0 * sumn[type_i], 3.0 * suma[type_i], 3.0 * suma2[type_i]])
129+
)
130+
131+
132+
def load_or_compute_se_input_stats(
133+
descrpt: Any,
134+
stat_file_path: DPPath | None,
135+
last_dim: int,
136+
compute: Callable[[], dict[str, Any]],
137+
mixed_types: bool = False,
138+
) -> dict[str, Any]:
139+
"""Load or compute SE descriptor input statistics using EnvMatStatSe format."""
140+
angular = last_dim == 4
141+
stat_path = _descriptor_stat_path(descrpt, stat_file_path, last_dim, mixed_types)
142+
stat_dict = _load_se_input_stats(stat_path, descrpt.get_ntypes(), angular)
143+
if stat_dict is not None:
144+
return stat_dict
145+
146+
stat_dict = compute()
147+
_save_se_input_stats(stat_path, stat_dict, descrpt.get_ntypes(), angular)
148+
return stat_dict

0 commit comments

Comments
 (0)