Skip to content
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

Fix: se_r prod_env_mat #3351

Merged
merged 6 commits into from
Feb 28, 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
9 changes: 9 additions & 0 deletions deepmd/pt/utils/env_mat_stat.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@
dtype=env.GLOBAL_PT_FLOAT_PRECISION,
device=env.DEVICE,
)
if self.last_dim == 4:
radial_only = False
elif self.last_dim == 1:
radial_only = True

Check warning on line 107 in deepmd/pt/utils/env_mat_stat.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/utils/env_mat_stat.py#L104-L107

Added lines #L104 - L107 were not covered by tests
else:
raise ValueError(

Check warning on line 109 in deepmd/pt/utils/env_mat_stat.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/utils/env_mat_stat.py#L109

Added line #L109 was not covered by tests
"last_dim should be 1 for raial-only or 4 for full descriptor."
)
anyangml marked this conversation as resolved.
Show resolved Hide resolved
for system in data:
coord, atype, box, natoms = (
system["coord"],
Expand Down Expand Up @@ -130,6 +138,7 @@
self.descriptor.get_rcut(),
# TODO: export rcut_smth from DescriptorBlock
self.descriptor.rcut_smth,
radial_only,
)
# reshape to nframes * nloc at the atom level,
# so nframes/mixed_type do not matter
Expand Down
55 changes: 53 additions & 2 deletions source/tests/pt/model/test_descriptor_se_r.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
from deepmd.pt.utils.env import (
PRECISION_DICT,
)
from deepmd.pt.utils.env_mat_stat import (
EnvMatStatSe,
)

from .test_env_mat import (
TestCaseSingleFrameWithNlist,
Expand Down Expand Up @@ -103,13 +106,61 @@ def test_consistency(
err_msg=err_msg,
)

def test_load_stat(self):
rng = np.random.default_rng()
_, _, nnei = self.nlist.shape
davg = rng.normal(size=(self.nt, nnei, 1))
dstd = rng.normal(size=(self.nt, nnei, 1))
dstd = 0.1 + np.abs(dstd)

for idt, prec in itertools.product(
[False, True],
["float64", "float32"],
):
dtype = PRECISION_DICT[prec]

# sea new impl
dd0 = DescrptSeR(
self.rcut,
self.rcut_smth,
self.sel,
precision=prec,
resnet_dt=idt,
old_impl=False,
)
dd0.mean = torch.tensor(davg, dtype=dtype, device=env.DEVICE)
dd0.dstd = torch.tensor(dstd, dtype=dtype, device=env.DEVICE)
dd1 = DescrptSeR.deserialize(dd0.serialize())
dd1.compute_input_stats(
[
{
"r0": None,
"coord": torch.from_numpy(self.coord_ext)
.reshape(-1, self.nall, 3)
.to(env.DEVICE),
"atype": torch.from_numpy(self.atype_ext).to(env.DEVICE),
"box": None,
"natoms": self.nall,
}
]
)

with self.assertRaises(ValueError) as cm:
ev = EnvMatStatSe(dd1)
ev.last_dim = 3
ev.load_or_compute_stats([])
self.assertEqual(
"last_dim should be 1 for raial-only or 4 for full descriptor.",
str(cm.exception),
)

def test_jit(
self,
):
rng = np.random.default_rng()
_, _, nnei = self.nlist.shape
davg = rng.normal(size=(self.nt, nnei, 4))
dstd = rng.normal(size=(self.nt, nnei, 4))
davg = rng.normal(size=(self.nt, nnei, 1))
dstd = rng.normal(size=(self.nt, nnei, 1))
dstd = 0.1 + np.abs(dstd)

for idt, prec in itertools.product(
Expand Down