Skip to content

Commit 2e49936

Browse files
pre-commit-ci[bot]Han Wang
authored andcommitted
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 854d654 commit 2e49936

3 files changed

Lines changed: 74 additions & 41 deletions

File tree

deepmd/dpmodel/descriptor/dpa2.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -926,8 +926,10 @@ def call(
926926
# None (multi-rank) is not yet supported on the graph route; the
927927
# graph needs `mapping` to fold ghosts to local owners, so without it
928928
# only nall == nloc is valid.
929-
if self.uses_graph_lower() and comm_dict is None and (
930-
mapping is not None or nall == nloc
929+
if (
930+
self.uses_graph_lower()
931+
and comm_dict is None
932+
and (mapping is not None or nall == nloc)
931933
):
932934
return self._call_graph_adapter(coord_ext, atype_ext, nlist, mapping)
933935
return self._call_dense(
@@ -1147,7 +1149,9 @@ def _block_graph(rc: float, ns: int) -> tuple[Any, int | None]:
11471149
dd = xp.reshape(dist, (n_center, static_nnei))[:, :ns]
11481150
dd = xp.reshape(dd, (n_center * ns,))
11491151
em = em & (dd <= rc)
1150-
sliced = dataclasses.replace(graph, edge_index=ei, edge_vec=ev, edge_mask=em)
1152+
sliced = dataclasses.replace(
1153+
graph, edge_index=ei, edge_vec=ev, edge_mask=em
1154+
)
11511155
return sliced, ns
11521156

11531157
tebd_table = (

source/tests/common/dpmodel/test_neighbor_graph.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ def test_importable_from_utils(self) -> None:
109109

110110

111111
def test_segment_max_trailing_dims():
112-
from deepmd.dpmodel.utils.neighbor_graph import segment_max
112+
from deepmd.dpmodel.utils.neighbor_graph import (
113+
segment_max,
114+
)
113115

114116
data = np.array([[1.0, 5.0], [3.0, 2.0], [2.0, 9.0]])
115117
ids = np.array([0, 0, 1], dtype=np.int64)
@@ -118,7 +120,9 @@ def test_segment_max_trailing_dims():
118120

119121

120122
def test_segment_softmax_trailing_dims_matches_columnwise():
121-
from deepmd.dpmodel.utils.neighbor_graph import segment_softmax
123+
from deepmd.dpmodel.utils.neighbor_graph import (
124+
segment_softmax,
125+
)
122126

123127
rng = np.random.default_rng(0)
124128
data = rng.normal(size=(6, 3))
@@ -133,7 +137,9 @@ def test_segment_softmax_trailing_dims_matches_columnwise():
133137
def test_segment_softmax_trailing_dims_torch():
134138
import torch
135139

136-
from deepmd.dpmodel.utils.neighbor_graph import segment_softmax
140+
from deepmd.dpmodel.utils.neighbor_graph import (
141+
segment_softmax,
142+
)
137143

138144
rng = np.random.default_rng(1)
139145
data = rng.normal(size=(5, 2))

source/tests/common/dpmodel/test_repformer_graph_ops.py

Lines changed: 58 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# SPDX-License-Identifier: LGPL-3.0-or-later
22
"""Per-op parity: repformer graph twins vs the dense reference ops on the
33
identical (shape-static, center-major) edge layout. Same math, fp64 =>
4-
rtol/atol 1e-12."""
4+
rtol/atol 1e-12.
5+
"""
56

67
import itertools
78

@@ -15,12 +16,10 @@
1516
LocalAtten,
1617
RepformerLayer,
1718
_cal_hg,
18-
_cal_grrg,
19-
_make_nei_g1,
20-
_cal_grrg_graph,
2119
_cal_hg_graph,
22-
symmetrization_op_graph,
20+
_make_nei_g1,
2321
symmetrization_op,
22+
symmetrization_op_graph,
2423
)
2524
from deepmd.dpmodel.utils.neighbor_graph import (
2625
center_edge_pairs,
@@ -47,21 +46,32 @@ def test_cal_hg_graph_parity(smooth, use_sqrt_nnei):
4746
g, h, mask, sw, n_total, dst = _mk()
4847
ref = _cal_hg(g, h, mask, sw, smooth=smooth, use_sqrt_nnei=use_sqrt_nnei)
4948
got = _cal_hg_graph(
50-
g.reshape(-1, NG), h.reshape(-1, 3), mask.reshape(-1), sw.reshape(-1),
51-
dst, n_total, NNEI, smooth=smooth, use_sqrt_nnei=use_sqrt_nnei,
52-
)
53-
np.testing.assert_allclose(
54-
got, ref.reshape(n_total, 3, NG), rtol=1e-12, atol=1e-12
49+
g.reshape(-1, NG),
50+
h.reshape(-1, 3),
51+
mask.reshape(-1),
52+
sw.reshape(-1),
53+
dst,
54+
n_total,
55+
NNEI,
56+
smooth=smooth,
57+
use_sqrt_nnei=use_sqrt_nnei,
5558
)
59+
np.testing.assert_allclose(got, ref.reshape(n_total, 3, NG), rtol=1e-12, atol=1e-12)
5660

5761

5862
@pytest.mark.parametrize("axis_neuron", [2, 4])
5963
def test_symmetrization_op_graph_parity(axis_neuron):
6064
g, h, mask, sw, n_total, dst = _mk(1)
6165
ref = symmetrization_op(g, h, mask, sw, axis_neuron)
6266
got = symmetrization_op_graph(
63-
g.reshape(-1, NG), h.reshape(-1, 3), mask.reshape(-1), sw.reshape(-1),
64-
dst, n_total, NNEI, axis_neuron,
67+
g.reshape(-1, NG),
68+
h.reshape(-1, 3),
69+
mask.reshape(-1),
70+
sw.reshape(-1),
71+
dst,
72+
n_total,
73+
NNEI,
74+
axis_neuron,
6575
)
6676
np.testing.assert_allclose(
6777
got, ref.reshape(n_total, axis_neuron * NG), rtol=1e-12, atol=1e-12
@@ -73,13 +83,22 @@ def test_cal_hg_graph_torch():
7383

7484
g, h, mask, sw, n_total, dst = _mk(2)
7585
ref = _cal_hg_graph(
76-
g.reshape(-1, NG), h.reshape(-1, 3), mask.reshape(-1), sw.reshape(-1),
77-
dst, n_total, NNEI,
86+
g.reshape(-1, NG),
87+
h.reshape(-1, 3),
88+
mask.reshape(-1),
89+
sw.reshape(-1),
90+
dst,
91+
n_total,
92+
NNEI,
7893
)
7994
got = _cal_hg_graph(
80-
torch.from_numpy(g.reshape(-1, NG)), torch.from_numpy(h.reshape(-1, 3)),
81-
torch.from_numpy(mask.reshape(-1)), torch.from_numpy(sw.reshape(-1)),
82-
torch.from_numpy(dst), n_total, NNEI,
95+
torch.from_numpy(g.reshape(-1, NG)),
96+
torch.from_numpy(h.reshape(-1, 3)),
97+
torch.from_numpy(mask.reshape(-1)),
98+
torch.from_numpy(sw.reshape(-1)),
99+
torch.from_numpy(dst),
100+
n_total,
101+
NNEI,
83102
)
84103
np.testing.assert_allclose(got.numpy(), ref, rtol=1e-12)
85104

@@ -137,9 +156,7 @@ def test_update_g1_conv_graph_parity(g1_out_conv):
137156
n_total,
138157
NNEI,
139158
)
140-
np.testing.assert_allclose(
141-
got, ref.reshape(n_total, -1), rtol=1e-12, atol=1e-12
142-
)
159+
np.testing.assert_allclose(got, ref.reshape(n_total, -1), rtol=1e-12, atol=1e-12)
143160

144161

145162
def test_update_g2_g1g1_graph_parity():
@@ -148,9 +165,7 @@ def test_update_g2_g1g1_graph_parity():
148165
g1_ext = g1.reshape(NF, NLOC, 8)
149166
gg1 = _make_nei_g1(g1_ext, np.where(mask, nlist, 0))
150167
ref = layer._update_g2_g1g1(g1_ext, gg1, mask, sw)
151-
got = layer._update_g2_g1g1_graph(
152-
g1, src, dst, mask.reshape(-1), sw.reshape(-1)
153-
)
168+
got = layer._update_g2_g1g1_graph(g1, src, dst, mask.reshape(-1), sw.reshape(-1))
154169
np.testing.assert_allclose(
155170
got, ref.reshape(n_total * NNEI, -1), rtol=1e-12, atol=1e-12
156171
)
@@ -196,10 +211,14 @@ def _pairs(mask, dst, n_total):
196211
return q_e, k_e, pm
197212

198213

199-
@pytest.mark.parametrize("has_gate,smooth", [(True, True), (False, True), (True, False)])
214+
@pytest.mark.parametrize(
215+
"has_gate,smooth", [(True, True), (False, True), (True, False)]
216+
)
200217
def test_atten2map_parity(has_gate, smooth):
201218
rng = np.random.default_rng(6)
202-
a2m = Atten2Map(NG, 4, 2, has_gate=has_gate, smooth=smooth, precision="float64", seed=7)
219+
a2m = Atten2Map(
220+
NG, 4, 2, has_gate=has_gate, smooth=smooth, precision="float64", seed=7
221+
)
203222
g2 = rng.normal(size=(NF, NLOC, NNEI, NG))
204223
h2 = rng.normal(size=(NF, NLOC, NNEI, 3))
205224
mask = rng.random((NF, NLOC, NNEI)) > 0.3
@@ -229,11 +248,15 @@ def test_atten2map_parity(has_gate, smooth):
229248
np.testing.assert_allclose(np.asarray(got), ref_pairs, rtol=1e-12, atol=1e-12)
230249

231250

232-
@pytest.mark.parametrize("has_gate,smooth", [(True, True), (False, True), (True, False)])
251+
@pytest.mark.parametrize(
252+
"has_gate,smooth", [(True, True), (False, True), (True, False)]
253+
)
233254
def test_atten2_mh_apply_parity(has_gate, smooth):
234255
rng = np.random.default_rng(9)
235256
nh = 3
236-
a2m = Atten2Map(NG, 4, nh, has_gate=has_gate, smooth=smooth, precision="float64", seed=10)
257+
a2m = Atten2Map(
258+
NG, 4, nh, has_gate=has_gate, smooth=smooth, precision="float64", seed=10
259+
)
237260
mha = Atten2MultiHeadApply(NG, nh, precision="float64", seed=11)
238261
g2 = rng.normal(size=(NF, NLOC, NNEI, NG))
239262
h2 = rng.normal(size=(NF, NLOC, NNEI, 3))
@@ -254,11 +277,15 @@ def test_atten2_mh_apply_parity(has_gate, smooth):
254277
)
255278

256279

257-
@pytest.mark.parametrize("has_gate,smooth", [(True, True), (False, True), (True, False)])
280+
@pytest.mark.parametrize(
281+
"has_gate,smooth", [(True, True), (False, True), (True, False)]
282+
)
258283
def test_atten2_ev_apply_parity(has_gate, smooth):
259284
rng = np.random.default_rng(12)
260285
nh = 3
261-
a2m = Atten2Map(NG, 4, nh, has_gate=has_gate, smooth=smooth, precision="float64", seed=13)
286+
a2m = Atten2Map(
287+
NG, 4, nh, has_gate=has_gate, smooth=smooth, precision="float64", seed=13
288+
)
262289
ev = Atten2EquiVarApply(NG, nh, precision="float64", seed=14)
263290
g2 = rng.normal(size=(NF, NLOC, NNEI, NG))
264291
h2 = rng.normal(size=(NF, NLOC, NNEI, 3))
@@ -403,12 +430,8 @@ def test_repformer_layer_call_graph_parity(case_name):
403430
np.testing.assert_allclose(
404431
got_g1, ref_g1.reshape(n_total, -1), rtol=1e-12, atol=1e-12
405432
)
406-
np.testing.assert_allclose(
407-
got_g2, ref_g2.reshape(-1, NG), rtol=1e-12, atol=1e-12
408-
)
409-
np.testing.assert_allclose(
410-
got_h2, ref_h2.reshape(-1, 3), rtol=1e-12, atol=1e-12
411-
)
433+
np.testing.assert_allclose(got_g2, ref_g2.reshape(-1, NG), rtol=1e-12, atol=1e-12)
434+
np.testing.assert_allclose(got_h2, ref_h2.reshape(-1, 3), rtol=1e-12, atol=1e-12)
412435

413436

414437
def test_repformer_layer_call_graph_torch():

0 commit comments

Comments
 (0)