-
Notifications
You must be signed in to change notification settings - Fork 629
test(pt): add common test case for model/atomic model #3767
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d85befd
test(pt): add common test case for model/atomic model
njzjz e51ca13
Merge branch 'devel' into add-common-test
njzjz d015b1c
Merge branch 'devel' into add-common-test
njzjz c98a791
refactor
njzjz 014f16f
Merge branch 'devel' into add-common-test
njzjz 694d500
refactor
njzjz 7cb00b9
Apply suggestions from code review
njzjz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
| """Universal tests for the project.""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
| """Common test case.""" | ||
|
|
||
| from abc import ( | ||
| ABC, | ||
| abstractmethod, | ||
| ) | ||
|
|
||
|
|
||
| class BackendTestCase(ABC): | ||
| """Backend test case.""" | ||
|
|
||
| module: object | ||
| """Module to test.""" | ||
|
|
||
| @property | ||
| @abstractmethod | ||
| def modules_to_test(self) -> list: | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| def forward_wrapper(self, x): | ||
| pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later |
18 changes: 18 additions & 0 deletions
18
source/tests/universal/common/cases/atomic_model/ener_model.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
|
|
||
|
|
||
| from .utils import ( | ||
| AtomicModelTestCase, | ||
| ) | ||
|
|
||
|
|
||
| class EnerAtomicModelTest(AtomicModelTestCase): | ||
| def setUp(self) -> None: | ||
| self.expected_rcut = 5.0 | ||
| self.expected_type_map = ["foo", "bar"] | ||
| self.expected_dim_fparam = 0 | ||
| self.expected_dim_aparam = 0 | ||
| self.expected_sel_type = [0, 1] | ||
| self.expected_aparam_nall = False | ||
| self.expected_model_output_type = ["energy", "mask"] | ||
| self.expected_sel = [8, 12] | ||
116 changes: 116 additions & 0 deletions
116
source/tests/universal/common/cases/atomic_model/utils.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
| from typing import ( | ||
| Any, | ||
| Callable, | ||
| List, | ||
| ) | ||
|
|
||
| import numpy as np | ||
|
|
||
| from deepmd.dpmodel.utils.nlist import ( | ||
| extend_input_and_build_neighbor_list, | ||
| ) | ||
|
|
||
|
|
||
| class AtomicModelTestCase: | ||
| """Common test case for atomic model.""" | ||
|
|
||
| expected_type_map: List[str] | ||
| """Expected type map.""" | ||
| expected_rcut: float | ||
| """Expected cut-off radius.""" | ||
| expected_dim_fparam: int | ||
| """Expected number (dimension) of frame parameters.""" | ||
| expected_dim_aparam: int | ||
| """Expected number (dimension) of atomic parameters.""" | ||
| expected_sel_type: List[int] | ||
| """Expected selected atom types.""" | ||
| expected_aparam_nall: bool | ||
| """Expected shape of atomic parameters.""" | ||
| expected_model_output_type: List[str] | ||
| """Expected output type for the model.""" | ||
| expected_sel: List[int] | ||
| """Expected number of neighbors.""" | ||
| forward_wrapper: Callable[[Any], Any] | ||
| """Calss wrapper for forward method.""" | ||
|
|
||
| def test_get_type_map(self): | ||
| """Test get_type_map.""" | ||
| for module in self.modules_to_test: | ||
| self.assertEqual(module.get_type_map(), self.expected_type_map) | ||
|
|
||
| def test_get_rcut(self): | ||
| """Test get_rcut.""" | ||
| for module in self.modules_to_test: | ||
| self.assertAlmostEqual(module.get_rcut(), self.expected_rcut) | ||
|
|
||
| def test_get_dim_fparam(self): | ||
| """Test get_dim_fparam.""" | ||
| for module in self.modules_to_test: | ||
| self.assertEqual(module.get_dim_fparam(), self.expected_dim_fparam) | ||
|
|
||
| def test_get_dim_aparam(self): | ||
| """Test get_dim_aparam.""" | ||
| for module in self.modules_to_test: | ||
| self.assertEqual(module.get_dim_aparam(), self.expected_dim_aparam) | ||
|
|
||
| def test_get_sel_type(self): | ||
| """Test get_sel_type.""" | ||
| for module in self.modules_to_test: | ||
| self.assertEqual(module.get_sel_type(), self.expected_sel_type) | ||
|
|
||
| def test_is_aparam_nall(self): | ||
| """Test is_aparam_nall.""" | ||
| for module in self.modules_to_test: | ||
| self.assertEqual(module.is_aparam_nall(), self.expected_aparam_nall) | ||
|
|
||
| def test_get_nnei(self): | ||
| """Test get_nnei.""" | ||
| expected_nnei = sum(self.expected_sel) | ||
| for module in self.modules_to_test: | ||
| self.assertEqual(module.get_nnei(), expected_nnei) | ||
|
|
||
| def test_get_ntypes(self): | ||
| """Test get_ntypes.""" | ||
| for module in self.modules_to_test: | ||
| self.assertEqual(module.get_ntypes(), len(self.expected_type_map)) | ||
|
|
||
| def test_forward(self): | ||
| """Test forward.""" | ||
| nf = 1 | ||
| coord = np.array( | ||
| [ | ||
| [0, 0, 0], | ||
| [0, 1, 0], | ||
| [0, 0, 1], | ||
| ], | ||
| dtype=np.float64, | ||
| ).reshape([nf, -1]) | ||
| atype = np.array([0, 0, 1], dtype=int).reshape([nf, -1]) | ||
| cell = 6.0 * np.eye(3).reshape([nf, 9]) | ||
| coord_ext, atype_ext, mapping, nlist = extend_input_and_build_neighbor_list( | ||
| coord, | ||
| atype, | ||
| self.expected_rcut, | ||
| self.expected_sel, | ||
| mixed_types=True, | ||
| box=cell, | ||
| ) | ||
| ret_lower = [] | ||
| for module in self.modules_to_test: | ||
| module = self.forward_wrapper(module) | ||
|
|
||
| ret_lower.append(module(coord_ext, atype_ext, nlist)) | ||
| for kk in ret_lower[0].keys(): | ||
| subret = [] | ||
| for rr in ret_lower: | ||
| if rr is not None: | ||
| subret.append(rr[kk]) | ||
| if len(subret): | ||
| for ii, rr in enumerate(subret[1:]): | ||
| if subret[0] is None: | ||
| assert rr is None | ||
| else: | ||
| np.testing.assert_allclose( | ||
| subret[0], rr, err_msg=f"compare {kk} between 0 and {ii}" | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
|
|
||
|
|
||
| from .utils import ( | ||
| ModelTestCase, | ||
| ) | ||
|
|
||
|
|
||
| class EnerModelTest(ModelTestCase): | ||
| def setUp(self) -> None: | ||
| self.expected_rcut = 5.0 | ||
| self.expected_type_map = ["foo", "bar"] | ||
| self.expected_dim_fparam = 0 | ||
| self.expected_dim_aparam = 0 | ||
| self.expected_sel_type = [0, 1] | ||
| self.expected_aparam_nall = False | ||
| self.expected_model_output_type = ["energy", "mask"] | ||
| self.expected_sel = [8, 12] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.