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

test(hybrid): add ut for descriptor hybrid #3711

Merged
merged 1 commit into from
Apr 29, 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
1 change: 1 addition & 0 deletions deepmd/dpmodel/descriptor/dpa1.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@
# nf x nloc x (ng x ng1 + tebd_dim)
if self.concat_output_tebd:
grrg = np.concatenate([grrg, atype_embd.reshape(nf, nloc, -1)], axis=-1)
gr = gr.reshape(nf, nloc, *gr.shape[1:])

Check warning on line 530 in deepmd/dpmodel/descriptor/dpa1.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/descriptor/dpa1.py#L530

Added line #L530 was not covered by tests
return grrg, gr[..., 1:], None, None, sw

def serialize(self) -> dict:
Expand Down
66 changes: 66 additions & 0 deletions source/tests/common/dpmodel/test_descriptor_hybrid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
import unittest

import numpy as np

from deepmd.dpmodel.descriptor.dpa1 import (
DescrptDPA1,
)
from deepmd.dpmodel.descriptor.hybrid import (
DescrptHybrid,
)
from deepmd.dpmodel.descriptor.se_e2_a import (
DescrptSeA,
)
from deepmd.dpmodel.descriptor.se_r import (
DescrptSeR,
)

from .case_single_frame_with_nlist import (
TestCaseSingleFrameWithNlist,
)


class TestDescrptHybrid(unittest.TestCase, TestCaseSingleFrameWithNlist):
def setUp(self):
unittest.TestCase.setUp(self)
TestCaseSingleFrameWithNlist.setUp(self)

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

ddsub0 = DescrptSeA(
rcut=self.rcut,
rcut_smth=self.rcut_smth,
sel=self.sel,
)
ddsub0.davg = davg
ddsub0.dstd = dstd
ddsub1 = DescrptDPA1(
rcut=self.rcut,
rcut_smth=self.rcut_smth,
sel=np.sum(self.sel).item() - 1,
ntypes=len(self.sel),
)
ddsub1.davg = davg[:, :6]
ddsub1.dstd = dstd[:, :6]
ddsub2 = DescrptSeR(
rcut=self.rcut / 2,
rcut_smth=self.rcut_smth,
sel=[3, 1],
)
ddsub2.davg = davg[:, :4, :1]
ddsub2.dstd = dstd[:, :4, :1]
em0 = DescrptHybrid(list=[ddsub0, ddsub1, ddsub2])

em1 = DescrptHybrid.deserialize(em0.serialize())
mm0 = em0.call(self.coord_ext, self.atype_ext, self.nlist)
mm1 = em1.call(self.coord_ext, self.atype_ext, self.nlist)
for ii in [0, 1]:
np.testing.assert_allclose(mm0[ii], mm1[ii])
Loading