11# SPDX-License-Identifier: LGPL-3.0-or-later
22"""Per-op parity: repformer graph twins vs the dense reference ops on the
33identical (shape-static, center-major) edge layout. Same math, fp64 =>
4- rtol/atol 1e-12."""
4+ rtol/atol 1e-12.
5+ """
56
67import itertools
78
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)
2524from 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 ])
5963def 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
145162def 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+ )
200217def 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+ )
233254def 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+ )
258283def 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
414437def test_repformer_layer_call_graph_torch ():
0 commit comments