Skip to content

pt: fix se_e2_a precision cast #3315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions deepmd/pt/model/descriptor/se_a.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ def forward(
else:
assert self.filter_layers is not None
dmatrix = dmatrix.view(-1, self.nnei, 4)
dmatrix = dmatrix.to(dtype=self.prec)
nfnl = dmatrix.shape[0]
# pre-allocate a shape to pass jit
xyz_scatter = torch.zeros(
Expand Down Expand Up @@ -489,8 +490,8 @@ def forward(
result = result.view(-1, nloc, self.filter_neuron[-1] * self.axis_neuron)
rot_mat = rot_mat.view([-1, nloc] + list(rot_mat.shape[1:])) # noqa:RUF005
return (
result,
rot_mat,
result.to(dtype=env.GLOBAL_PT_FLOAT_PRECISION),
rot_mat.to(dtype=env.GLOBAL_PT_FLOAT_PRECISION),
None,
None,
sw,
Expand Down
6 changes: 6 additions & 0 deletions source/tests/consistent/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ def test_tf_consistent_with_ref(self):
np.testing.assert_equal(data1, data2)
for rr1, rr2 in zip(ret1, ret2):
np.testing.assert_allclose(rr1, rr2, rtol=self.rtol, atol=self.atol)
assert rr1.dtype == rr2.dtype, f"{rr1.dtype} != {rr2.dtype}"

def test_tf_self_consistent(self):
"""Test whether TF is self consistent."""
Expand All @@ -269,6 +270,7 @@ def test_tf_self_consistent(self):
np.testing.assert_equal(data1, data2)
for rr1, rr2 in zip(ret1, ret2):
np.testing.assert_allclose(rr1, rr2, rtol=self.rtol, atol=self.atol)
assert rr1.dtype == rr2.dtype, f"{rr1.dtype} != {rr2.dtype}"

def test_dp_consistent_with_ref(self):
"""Test whether DP and reference are consistent."""
Expand All @@ -286,6 +288,7 @@ def test_dp_consistent_with_ref(self):
np.testing.assert_equal(data1, data2)
for rr1, rr2 in zip(ret1, ret2):
np.testing.assert_allclose(rr1, rr2, rtol=self.rtol, atol=self.atol)
assert rr1.dtype == rr2.dtype, f"{rr1.dtype} != {rr2.dtype}"

def test_dp_self_consistent(self):
"""Test whether DP is self consistent."""
Expand All @@ -299,6 +302,7 @@ def test_dp_self_consistent(self):
for rr1, rr2 in zip(ret1, ret2):
if isinstance(rr1, np.ndarray) and isinstance(rr2, np.ndarray):
np.testing.assert_allclose(rr1, rr2, rtol=self.rtol, atol=self.atol)
assert rr1.dtype == rr2.dtype, f"{rr1.dtype} != {rr2.dtype}"
else:
self.assertEqual(rr1, rr2)

Expand All @@ -318,6 +322,7 @@ def test_pt_consistent_with_ref(self):
np.testing.assert_equal(data1, data2)
for rr1, rr2 in zip(ret1, ret2):
np.testing.assert_allclose(rr1, rr2, rtol=self.rtol, atol=self.atol)
assert rr1.dtype == rr2.dtype, f"{rr1.dtype} != {rr2.dtype}"

def test_pt_self_consistent(self):
"""Test whether PT is self consistent."""
Expand All @@ -331,6 +336,7 @@ def test_pt_self_consistent(self):
for rr1, rr2 in zip(ret1, ret2):
if isinstance(rr1, np.ndarray) and isinstance(rr2, np.ndarray):
np.testing.assert_allclose(rr1, rr2, rtol=self.rtol, atol=self.atol)
assert rr1.dtype == rr2.dtype, f"{rr1.dtype} != {rr2.dtype}"
else:
self.assertEqual(rr1, rr2)

Expand Down
37 changes: 37 additions & 0 deletions source/tests/consistent/descriptor/test_se_e2_a.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
(True, False), # resnet_dt
(True, False), # type_one_side
([], [[0, 1]]), # excluded_types
("float32", "float64"), # precision
)
class TestSeA(CommonTest, DescriptorTest, unittest.TestCase):
@property
Expand All @@ -47,6 +48,7 @@ def data(self) -> dict:
resnet_dt,
type_one_side,
excluded_types,
precision,
) = self.param
return {
"sel": [10, 10],
Expand All @@ -57,6 +59,7 @@ def data(self) -> dict:
"resnet_dt": resnet_dt,
"type_one_side": type_one_side,
"exclude_types": excluded_types,
"precision": precision,
"seed": 1145141919810,
}

Expand All @@ -66,6 +69,7 @@ def skip_pt(self) -> bool:
resnet_dt,
type_one_side,
excluded_types,
precision,
) = self.param
return not type_one_side or CommonTest.skip_pt

Expand All @@ -75,6 +79,7 @@ def skip_dp(self) -> bool:
resnet_dt,
type_one_side,
excluded_types,
precision,
) = self.param
return not type_one_side or CommonTest.skip_dp

Expand Down Expand Up @@ -147,3 +152,35 @@ def eval_pt(self, pt_obj: Any) -> Any:

def extract_ret(self, ret: Any, backend) -> Tuple[np.ndarray, ...]:
return (ret[0],)

@property
def rtol(self) -> float:
"""Relative tolerance for comparing the return value."""
(
resnet_dt,
type_one_side,
excluded_types,
precision,
) = self.param
if precision == "float64":
return 1e-10
elif precision == "float32":
return 1e-4
else:
raise ValueError(f"Unknown precision: {precision}")

@property
def atol(self) -> float:
"""Absolute tolerance for comparing the return value."""
(
resnet_dt,
type_one_side,
excluded_types,
precision,
) = self.param
if precision == "float64":
return 1e-10
elif precision == "float32":
return 1e-4
else:
raise ValueError(f"Unknown precision: {precision}")