Skip to content

Commit 69aad36

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent aa386a6 commit 69aad36

8 files changed

Lines changed: 70 additions & 52 deletions

File tree

deepmd/dpmodel/fitting/property_fitting.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66

77
import numpy as np
88

9-
from deepmd.dpmodel.output_def import (
10-
FittingOutputDef,
11-
OutputVariableDef,
12-
)
139
from deepmd.dpmodel.common import (
1410
DEFAULT_PRECISION,
1511
)
1612
from deepmd.dpmodel.fitting.invar_fitting import (
1713
InvarFitting,
1814
)
15+
from deepmd.dpmodel.output_def import (
16+
FittingOutputDef,
17+
OutputVariableDef,
18+
)
1919
from deepmd.utils.version import (
2020
check_version_compatibility,
2121
)

deepmd/dpmodel/model/dp_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@ def update_sel(
4848

4949
def get_fitting_net(self):
5050
"""Get the fitting network."""
51-
return self.atomic_model.fitting
51+
return self.atomic_model.fitting

deepmd/dpmodel/model/property_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ def __init__(
2828

2929
def get_var_name(self) -> str:
3030
"""Get the name of the property."""
31-
return self.get_fitting_net().var_name
31+
return self.get_fitting_net().var_name

deepmd/dpmodel/model/transform_output.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# SPDX-License-Identifier: LGPL-3.0-or-later
22

3+
from typing import (
4+
Optional,
5+
)
6+
37
import array_api_compat
48
import numpy as np
59

@@ -17,15 +21,14 @@
1721
get_hessian_name,
1822
get_reduce_name,
1923
)
20-
from typing import Optional
2124

2225

2326
def fit_output_to_model_output(
2427
fit_ret: dict[str, np.ndarray],
2528
fit_output_def: FittingOutputDef,
2629
coord_ext: np.ndarray,
2730
do_atomic_virial: bool = False,
28-
mask: Optional[np.ndarray] = None
31+
mask: Optional[np.ndarray] = None,
2932
) -> dict[str, np.ndarray]:
3033
"""Transform the output of the fitting network to
3134
the model output.
@@ -43,10 +46,15 @@ def fit_output_to_model_output(
4346
if vdef.intensive:
4447
if (mask is not None) and (mask == 0.0).any():
4548
mask = mask.astype(bool)
46-
model_ret[kk_redu] = xp.stack([
47-
xp.mean(vv[ii].astype(GLOBAL_ENER_FLOAT_PRECISION)[mask[ii]], axis=atom_axis)
48-
for ii in range(mask.shape[0])
49-
])
49+
model_ret[kk_redu] = xp.stack(
50+
[
51+
xp.mean(
52+
vv[ii].astype(GLOBAL_ENER_FLOAT_PRECISION)[mask[ii]],
53+
axis=atom_axis,
54+
)
55+
for ii in range(mask.shape[0])
56+
]
57+
)
5058
else:
5159
model_ret[kk_redu] = xp.mean(
5260
vv.astype(GLOBAL_ENER_FLOAT_PRECISION), axis=atom_axis

deepmd/jax/model/base_model.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,14 @@ def forward_common_atomic(
4848
kk_redu = get_reduce_name(kk)
4949
if vdef.intensive:
5050
mask = atomic_ret["mask"] if "mask" in atomic_ret else None
51-
if (mask is not None) and (mask==0.0).any():
51+
if (mask is not None) and (mask == 0.0).any():
5252
mask = mask.astype(jnp.bool_)
53-
model_predict[kk_redu] = jnp.stack([
54-
jnp.mean(vv[ii][mask[ii]], axis=atom_axis)
55-
for ii in range(mask.shape[0])
56-
])
53+
model_predict[kk_redu] = jnp.stack(
54+
[
55+
jnp.mean(vv[ii][mask[ii]], axis=atom_axis)
56+
for ii in range(mask.shape[0])
57+
]
58+
)
5759
else:
5860
model_predict[kk_redu] = jnp.mean(vv, axis=atom_axis)
5961
else:

source/tests/common/dpmodel/test_padding_atoms.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# SPDX-License-Identifier: LGPL-3.0-or-later
2-
import sys
32
import unittest
3+
from copy import (
4+
deepcopy,
5+
)
46

57
import numpy as np
6-
from copy import deepcopy
78

89
from deepmd.dpmodel.descriptor.se_e2_a import (
910
DescrptSeA,
@@ -32,13 +33,13 @@ def setUp(self) -> None:
3233
[1, 0, 1],
3334
[0, 1, 1],
3435
[1, 1, 0],
35-
]
36+
],
3637
],
3738
dtype=np.float64,
3839
)
39-
self.atype = np.array([[0, 0, 1],[1, 1, 0]], dtype=int).reshape([2, self.nloc])
40+
self.atype = np.array([[0, 0, 1], [1, 1, 0]], dtype=int).reshape([2, self.nloc])
4041
self.cell = 2.0 * np.eye(3).reshape([1, 9])
41-
self.cell = np.array([self.cell,self.cell]).reshape(2, 9)
42+
self.cell = np.array([self.cell, self.cell]).reshape(2, 9)
4243
self.sel = [16, 8]
4344
self.rcut = 2.2
4445
self.rcut_smth = 0.4
@@ -69,7 +70,7 @@ def test_padding_atoms_consistency(self):
6970
# test intensive
7071
np.testing.assert_allclose(
7172
result[f"{var_name}_redu"],
72-
np.mean(result[f"{var_name}"],axis=1),
73+
np.mean(result[f"{var_name}"], axis=1),
7374
atol=self.atol,
7475
)
7576
# test padding atoms
@@ -80,14 +81,14 @@ def test_padding_atoms_consistency(self):
8081
atype_padding = np.pad(
8182
atype,
8283
pad_width=((0, 0), (0, padding_atoms)),
83-
mode='constant',
84-
constant_values=-1
84+
mode="constant",
85+
constant_values=-1,
8586
)
8687
coord_padding = np.pad(
8788
coord,
8889
pad_width=((0, 0), (0, padding_atoms), (0, 0)),
89-
mode='constant',
90-
constant_values=0
90+
mode="constant",
91+
constant_values=0,
9192
)
9293
args = [coord_padding, atype_padding, self.cell]
9394
result_padding = model.call(*args)

source/tests/jax/test_padding_atoms.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# SPDX-License-Identifier: LGPL-3.0-or-later
22
import sys
33
import unittest
4+
from copy import (
5+
deepcopy,
6+
)
47

58
import numpy as np
6-
from copy import deepcopy
79

810
from deepmd.dpmodel.common import (
911
to_numpy_array,
@@ -49,13 +51,13 @@ def setUp(self) -> None:
4951
[1, 0, 1],
5052
[0, 1, 1],
5153
[1, 1, 0],
52-
]
54+
],
5355
],
5456
dtype=np.float64,
5557
)
56-
self.atype = np.array([[0, 0, 1],[1, 1, 0]], dtype=int).reshape([2, self.nloc])
58+
self.atype = np.array([[0, 0, 1], [1, 1, 0]], dtype=int).reshape([2, self.nloc])
5759
self.cell = 2.0 * np.eye(3).reshape([1, 9])
58-
self.cell = np.array([self.cell,self.cell]).reshape(2, 9)
60+
self.cell = np.array([self.cell, self.cell]).reshape(2, 9)
5961
self.sel = [16, 8]
6062
self.rcut = 2.2
6163
self.rcut_smth = 0.4
@@ -90,7 +92,7 @@ def test_padding_atoms_consistency(self):
9092
# test intensive
9193
np.testing.assert_allclose(
9294
to_numpy_array(result[f"{var_name}_redu"]),
93-
np.mean(to_numpy_array(result[f"{var_name}"]),axis=1),
95+
np.mean(to_numpy_array(result[f"{var_name}"]), axis=1),
9496
atol=self.atol,
9597
)
9698
# test padding atoms
@@ -101,16 +103,18 @@ def test_padding_atoms_consistency(self):
101103
atype_padding = np.pad(
102104
atype,
103105
pad_width=((0, 0), (0, padding_atoms)),
104-
mode='constant',
105-
constant_values=-1
106+
mode="constant",
107+
constant_values=-1,
106108
)
107109
coord_padding = np.pad(
108110
coord,
109111
pad_width=((0, 0), (0, padding_atoms), (0, 0)),
110-
mode='constant',
111-
constant_values=0
112+
mode="constant",
113+
constant_values=0,
112114
)
113-
args = [to_jax_array(ii) for ii in [coord_padding, atype_padding, self.cell]]
115+
args = [
116+
to_jax_array(ii) for ii in [coord_padding, atype_padding, self.cell]
117+
]
114118
result_padding = model.call(*args)
115119
np.testing.assert_allclose(
116120
to_numpy_array(result[f"{var_name}_redu"]),

source/tests/pt/test_padding_atoms.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
# SPDX-License-Identifier: LGPL-3.0-or-later
2-
import sys
32
import unittest
3+
from copy import (
4+
deepcopy,
5+
)
46

57
import numpy as np
68
import torch
7-
from copy import deepcopy
89

910
from deepmd.dpmodel.common import (
1011
to_numpy_array,
1112
)
12-
1313
from deepmd.pt.model.descriptor import (
1414
DescrptSeA,
1515
)
16-
from deepmd.pt.model.task import (
17-
PropertyFittingNet,
18-
)
1916
from deepmd.pt.model.model import (
2017
PropertyModel,
2118
)
19+
from deepmd.pt.model.task import (
20+
PropertyFittingNet,
21+
)
2222
from deepmd.pt.utils.utils import (
2323
to_torch_tensor,
2424
)
@@ -40,13 +40,13 @@ def setUp(self) -> None:
4040
[1, 0, 1],
4141
[0, 1, 1],
4242
[1, 1, 0],
43-
]
43+
],
4444
],
4545
dtype=np.float64,
4646
)
47-
self.atype = np.array([[0, 0, 1],[1, 1, 0]], dtype=int).reshape([2, self.nloc])
47+
self.atype = np.array([[0, 0, 1], [1, 1, 0]], dtype=int).reshape([2, self.nloc])
4848
self.cell = 2.0 * np.eye(3).reshape([1, 9])
49-
self.cell = np.array([self.cell,self.cell]).reshape(2, 9)
49+
self.cell = np.array([self.cell, self.cell]).reshape(2, 9)
5050
self.sel = [16, 8]
5151
self.rcut = 2.2
5252
self.rcut_smth = 0.4
@@ -78,7 +78,7 @@ def test_padding_atoms_consistency(self):
7878
# test intensive
7979
np.testing.assert_allclose(
8080
to_numpy_array(result[var_name].cpu().detach()),
81-
np.mean(to_numpy_array(result[f"atom_{var_name}"].cpu().detach()),axis=1),
81+
np.mean(to_numpy_array(result[f"atom_{var_name}"].cpu().detach()), axis=1),
8282
atol=self.atol,
8383
)
8484
# test padding atoms
@@ -89,17 +89,20 @@ def test_padding_atoms_consistency(self):
8989
atype_padding = np.pad(
9090
atype,
9191
pad_width=((0, 0), (0, padding_atoms)),
92-
mode='constant',
93-
constant_values=-1
92+
mode="constant",
93+
constant_values=-1,
9494
)
9595
coord_padding = np.pad(
9696
coord,
9797
pad_width=((0, 0), (0, padding_atoms), (0, 0)),
98-
mode='constant',
99-
constant_values=0
98+
mode="constant",
99+
constant_values=0,
100100
)
101101
with torch.device("cpu"):
102-
args = [to_torch_tensor(ii) for ii in [coord_padding, atype_padding, self.cell]]
102+
args = [
103+
to_torch_tensor(ii)
104+
for ii in [coord_padding, atype_padding, self.cell]
105+
]
103106
result_padding = model(*args)
104107
np.testing.assert_allclose(
105108
to_numpy_array(result[var_name].cpu().detach()),

0 commit comments

Comments
 (0)