-
Notifications
You must be signed in to change notification settings - Fork 647
fix(dpmodel): keep output reductions on active backend #5789
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
4 commits
Select commit
Hold shift + click to select a range
764d4ca
fix(dpmodel): keep output reductions on active backend
njzjz-bot bb988ae
test(dpmodel): skip transform tests without torch
njzjz-bot d3ed641
test(dpmodel): cover backend output reduction branches
njzjz-bot e900967
chore(dpmodel): merge master into backend reduction fix
njzjz-bot 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
| """Backend-preservation tests for fitting-output reductions.""" | ||
|
|
||
| import pytest | ||
|
|
||
| from deepmd.dpmodel.model.edge_transform_output import ( | ||
| fit_output_to_model_output_graph, | ||
| ) | ||
| from deepmd.dpmodel.model.transform_output import ( | ||
| fit_output_to_model_output, | ||
| ) | ||
| from deepmd.dpmodel.output_def import ( | ||
| FittingOutputDef, | ||
| OutputVariableDef, | ||
| ) | ||
| from deepmd.dpmodel.utils.neighbor_graph import ( | ||
| NeighborGraph, | ||
| ) | ||
|
|
||
| torch = pytest.importorskip("torch") | ||
|
|
||
|
|
||
| def _output_def(*, intensive: bool) -> FittingOutputDef: | ||
| """Build the smallest reducible fitting definition used by these tests.""" | ||
| return FittingOutputDef( | ||
| [ | ||
| OutputVariableDef( | ||
| name="energy", | ||
| shape=[1], | ||
| reducible=True, | ||
| r_differentiable=False, | ||
| c_differentiable=False, | ||
| intensive=intensive, | ||
| ) | ||
| ] | ||
| ) | ||
|
|
||
|
|
||
| def test_dense_torch_extensive_reduction_stays_on_backend() -> None: | ||
| """Dense reduction must not rely on NumPy-style ``Tensor.astype``.""" | ||
| atomic = torch.tensor([[[1.0], [2.0], [3.0]]], dtype=torch.float32) | ||
| coord = torch.zeros((1, 3, 3), dtype=torch.float32) | ||
|
|
||
| result = fit_output_to_model_output( | ||
| {"energy": atomic}, _output_def(intensive=False), coord | ||
| )["energy_redu"] | ||
|
|
||
| assert isinstance(result, torch.Tensor) | ||
| assert result.dtype is torch.float64 | ||
| torch.testing.assert_close(result, torch.tensor([[6.0]], dtype=torch.float64)) | ||
|
|
||
|
|
||
| def test_dense_torch_mask_count_uses_energy_dtype() -> None: | ||
| """The intensive divisor must be reduced by Torch in energy precision.""" | ||
| atomic = torch.tensor([[[1.0], [2.0], [3.0]]], dtype=torch.float32) | ||
| coord = torch.zeros((1, 3, 3), dtype=torch.float32) | ||
| mask = torch.tensor([[True, True, False]]) | ||
|
|
||
| result = fit_output_to_model_output( | ||
| {"energy": atomic}, _output_def(intensive=True), coord, mask=mask | ||
| )["energy_redu"] | ||
|
|
||
| assert isinstance(result, torch.Tensor) | ||
| assert result.dtype is torch.float64 | ||
| torch.testing.assert_close(result, torch.tensor([[3.0]], dtype=torch.float64)) | ||
|
|
||
|
|
||
| def test_dense_torch_no_mask_intensive_reduction_stays_on_backend() -> None: | ||
| """Exercise the intensive reduction branch without a real-atom mask.""" | ||
| atomic = torch.tensor([[[1.0], [2.0], [4.0]]], dtype=torch.float32) | ||
| coord = torch.zeros((1, 3, 3), dtype=torch.float32) | ||
|
|
||
| result = fit_output_to_model_output( | ||
| {"energy": atomic}, _output_def(intensive=True), coord | ||
| )["energy_redu"] | ||
|
|
||
| assert isinstance(result, torch.Tensor) | ||
| assert result.dtype is torch.float64 | ||
| torch.testing.assert_close(result, torch.tensor([[7.0 / 3.0]], dtype=torch.float64)) | ||
|
|
||
|
|
||
| def test_graph_torch_mask_count_uses_backend_dtype() -> None: | ||
| """Graph reductions must translate the NumPy precision to ``torch.dtype``.""" | ||
| graph = NeighborGraph( | ||
| n_node=torch.tensor([2, 1], dtype=torch.int64), | ||
| edge_index=torch.empty((2, 0), dtype=torch.int64), | ||
| edge_vec=torch.empty((0, 3), dtype=torch.float32), | ||
| edge_mask=torch.empty((0,), dtype=torch.bool), | ||
| ) | ||
| atomic = torch.tensor([[1.0], [2.0], [6.0]], dtype=torch.float32) | ||
| mask = torch.tensor([True, True, True]) | ||
|
|
||
| result = fit_output_to_model_output_graph( | ||
| {"energy": atomic}, _output_def(intensive=True), graph, mask=mask | ||
| )["energy_redu"] | ||
|
|
||
| assert isinstance(result, torch.Tensor) | ||
| assert result.dtype is torch.float64 | ||
| torch.testing.assert_close( | ||
| result, torch.tensor([[1.5], [6.0]], dtype=torch.float64) | ||
| ) | ||
|
|
||
|
|
||
| def test_graph_torch_no_mask_intensive_reduction_stays_on_backend() -> None: | ||
| """Exercise the graph intensive fallback using per-frame node counts.""" | ||
| graph = NeighborGraph( | ||
| n_node=torch.tensor([2, 1], dtype=torch.int64), | ||
| edge_index=torch.empty((2, 0), dtype=torch.int64), | ||
| edge_vec=torch.empty((0, 3), dtype=torch.float32), | ||
| edge_mask=torch.empty((0,), dtype=torch.bool), | ||
| ) | ||
| atomic = torch.tensor([[1.0], [3.0], [6.0]], dtype=torch.float32) | ||
|
|
||
| result = fit_output_to_model_output_graph( | ||
| {"energy": atomic}, _output_def(intensive=True), graph | ||
| )["energy_redu"] | ||
|
|
||
| assert isinstance(result, torch.Tensor) | ||
| assert result.dtype is torch.float64 | ||
| torch.testing.assert_close( | ||
| result, torch.tensor([[2.0], [6.0]], dtype=torch.float64) | ||
| ) | ||
|
|
||
|
|
||
| def test_graph_torch_extensive_reduction_stays_on_backend() -> None: | ||
| """Exercise the graph extensive branch independently of intensive counts.""" | ||
| graph = NeighborGraph( | ||
| n_node=torch.tensor([2, 1], dtype=torch.int64), | ||
| edge_index=torch.empty((2, 0), dtype=torch.int64), | ||
| edge_vec=torch.empty((0, 3), dtype=torch.float32), | ||
| edge_mask=torch.empty((0,), dtype=torch.bool), | ||
| ) | ||
| atomic = torch.tensor([[1.0], [3.0], [6.0]], dtype=torch.float32) | ||
|
|
||
| result = fit_output_to_model_output_graph( | ||
| {"energy": atomic}, _output_def(intensive=False), graph | ||
| )["energy_redu"] | ||
|
|
||
| assert isinstance(result, torch.Tensor) | ||
| assert result.dtype is torch.float64 | ||
| torch.testing.assert_close( | ||
| result, torch.tensor([[4.0], [6.0]], dtype=torch.float64) | ||
| ) |
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.