-
Notifications
You must be signed in to change notification settings - Fork 628
fix: make global DOS inference work on the dpmodel and JAX backends #5722
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
Open
wanghan-iapcm
wants to merge
5
commits into
deepmodeling:master
Choose a base branch
from
wanghan-iapcm:fix-deepdos-redu
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
da03e75
fix(dpmodel): make global DOS inference use the reduced output
a7da1dd
fix(jax): carry numb_dos through the StableHLO export for DOS inference
c6dba95
fix(dpmodel): keep summed atomic DOS when the backend provides it
395e585
fix(tf): reduce DOS per frame so global DOS equals the atomic sum
d1e8cd7
test(tf): fold the multi-frame DOS check into test_model_dos
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
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,85 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
| """Test global-DOS-only inference on the dpmodel backend. | ||
|
|
||
| ``DeepDOS.eval`` used to read the atomic ``dos`` output unconditionally and sum | ||
| it to obtain the global DOS. The dpmodel (and JAX) backends only return the | ||
| atomic ``OUT`` variables when ``atomic=True``; for ``atomic=False`` they return | ||
| the reduced ``dos_redu`` instead, so reading ``results["dos"]`` raised | ||
| ``KeyError``. A global-DOS-only path (e.g. ``dp test`` without atomic DOS | ||
| labels) must use the reduced output. | ||
| """ | ||
|
|
||
| import tempfile | ||
| import unittest | ||
| from pathlib import ( | ||
| Path, | ||
| ) | ||
|
|
||
| import numpy as np | ||
|
|
||
| from deepmd.dpmodel.model.model import get_model as get_model_dp | ||
| from deepmd.dpmodel.utils.serialization import ( | ||
| save_dp_model, | ||
| ) | ||
| from deepmd.infer.deep_dos import ( | ||
| DeepDOS, | ||
| ) | ||
|
|
||
|
|
||
| def _dos_model_config() -> dict: | ||
| return { | ||
| "type_map": ["O", "H"], | ||
| "descriptor": { | ||
| "type": "se_e2_a", | ||
| "sel": [20, 20], | ||
| "rcut_smth": 1.8, | ||
| "rcut": 6.0, | ||
| "neuron": [2, 4, 8], | ||
| "resnet_dt": False, | ||
| "axis_neuron": 8, | ||
| "precision": "float64", | ||
| "type_one_side": True, | ||
| "seed": 1, | ||
| }, | ||
| "fitting_net": { | ||
| "type": "dos", | ||
| "numb_dos": 2, | ||
| "neuron": [4, 4, 4], | ||
| "resnet_dt": True, | ||
| "numb_fparam": 0, | ||
| "precision": "float64", | ||
| "seed": 1, | ||
| }, | ||
| } | ||
|
|
||
|
|
||
| class TestDeepDOSDPModel(unittest.TestCase): | ||
| def setUp(self) -> None: | ||
| model = get_model_dp(_dos_model_config()) | ||
| self.tmpdir = tempfile.TemporaryDirectory() | ||
| model_file = str(Path(self.tmpdir.name) / "dos.dp") | ||
| save_dp_model(model_file, {"model": model.serialize()}) | ||
| self.dp = DeepDOS(model_file) | ||
| rng = np.random.default_rng(0) | ||
| self.coords = rng.random([1, 6, 3]) * 4.0 | ||
| self.cells = (np.eye(3) * 10.0).reshape(1, 9) | ||
| self.atypes = np.array([[0, 1, 1, 0, 1, 1]], dtype=np.int32) | ||
|
|
||
| def tearDown(self) -> None: | ||
| self.tmpdir.cleanup() | ||
|
|
||
| def test_global_dos_only(self) -> None: | ||
| # atomic=False must return the global DOS via the reduced output, | ||
| # without requiring the atomic `dos` key. | ||
| (dos,) = self.dp.eval(self.coords, self.cells, self.atypes, atomic=False) | ||
| self.assertEqual(dos.shape, (1, self.dp.get_numb_dos())) | ||
|
|
||
| def test_global_matches_atomic_sum(self) -> None: | ||
| # The reduced global DOS must equal the sum of the atomic DOS. | ||
| (dos,) = self.dp.eval(self.coords, self.cells, self.atypes, atomic=False) | ||
| _, atomic_dos = self.dp.eval(self.coords, self.cells, self.atypes, atomic=True) | ||
| np.testing.assert_allclose(dos, np.sum(atomic_dos, axis=1)) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
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,81 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
| """Global-DOS-only inference on the JAX backend (StableHLO export). | ||
|
|
||
| Mirrors the dpmodel DOS inference test. The JAX evaluator wraps an ``HLO`` | ||
| object with no live model, so ``numb_dos`` must be carried through the StableHLO | ||
| export for ``DeepDOS.eval`` to reshape the reduced DOS output correctly. | ||
| """ | ||
|
|
||
| import tempfile | ||
| import unittest | ||
| from pathlib import ( | ||
| Path, | ||
| ) | ||
|
|
||
| import numpy as np | ||
|
|
||
| from deepmd.dpmodel.model.model import get_model as get_model_dp | ||
| from deepmd.infer.deep_dos import ( | ||
| DeepDOS, | ||
| ) | ||
| from deepmd.jax.utils.serialization import ( | ||
| deserialize_to_file, | ||
| ) | ||
|
|
||
|
|
||
| def _dos_model_config() -> dict: | ||
| return { | ||
| "type_map": ["O", "H"], | ||
| "descriptor": { | ||
| "type": "se_e2_a", | ||
| "sel": [20, 20], | ||
| "rcut_smth": 1.8, | ||
| "rcut": 6.0, | ||
| "neuron": [2, 4, 8], | ||
| "resnet_dt": False, | ||
| "axis_neuron": 8, | ||
| "precision": "float64", | ||
| "type_one_side": True, | ||
| "seed": 1, | ||
| }, | ||
| "fitting_net": { | ||
| "type": "dos", | ||
| "numb_dos": 2, | ||
| "neuron": [4, 4, 4], | ||
| "resnet_dt": True, | ||
| "numb_fparam": 0, | ||
| "precision": "float64", | ||
| "seed": 1, | ||
| }, | ||
| } | ||
|
|
||
|
|
||
| class TestDeepDOSJAX(unittest.TestCase): | ||
| def setUp(self) -> None: | ||
| config = _dos_model_config() | ||
| model = get_model_dp(config) | ||
| self.tmpdir = tempfile.TemporaryDirectory() | ||
| model_file = str(Path(self.tmpdir.name) / "dos.hlo") | ||
| deserialize_to_file( | ||
| model_file, | ||
| {"model": model.serialize(), "model_def_script": {"model": config}}, | ||
| ) | ||
| self.dp = DeepDOS(model_file) | ||
| rng = np.random.default_rng(0) | ||
| self.coords = rng.random([1, 6, 3]) * 4.0 | ||
| self.cells = (np.eye(3) * 10.0).reshape(1, 9) | ||
| self.atypes = np.array([[0, 1, 1, 0, 1, 1]], dtype=np.int32) | ||
|
|
||
| def tearDown(self) -> None: | ||
| self.tmpdir.cleanup() | ||
|
|
||
| def test_numb_dos_survives_export(self) -> None: | ||
| self.assertEqual(self.dp.get_numb_dos(), 2) | ||
|
|
||
| def test_global_dos_only(self) -> None: | ||
| (dos,) = self.dp.eval(self.coords, self.cells, self.atypes, atomic=False) | ||
| self.assertEqual(dos.shape, (1, 2)) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may need to review the implementation of dpmodel and fix it, instead of adding a workaround here.