-
Notifications
You must be signed in to change notification settings - Fork 640
fix(pd): handle border scalars and CPU tensors #5832
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
njzjz-bot
wants to merge
3
commits into
deepmodeling:master
Choose a base branch
from
njzjz-bot:fix/paddle-border-op-5627
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.
+123
−30
Open
Changes from 1 commit
Commits
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
| """Regression tests for Paddle border-exchange control and data tensors.""" | ||
|
|
||
| import numpy as np | ||
| import paddle | ||
| import pytest | ||
|
|
||
| deepmd_op_pd = pytest.importorskip( | ||
| "deepmd_op_pd", reason="the Paddle custom operator library is not built" | ||
| ) | ||
|
|
||
|
|
||
| def _control_tensors(nswap: int) -> tuple[paddle.Tensor, ...]: | ||
| """Create the common CPU control tensors for a border exchange.""" | ||
| return ( | ||
| paddle.zeros([nswap], dtype="int32"), # sendproc | ||
| paddle.zeros([nswap], dtype="int32"), # recvproc | ||
| paddle.zeros([nswap], dtype="int32"), # sendnum | ||
| paddle.zeros([nswap], dtype="int32"), # recvnum | ||
| paddle.zeros([1], dtype="int64"), # unused communicator without MPI | ||
| ) | ||
|
|
||
|
|
||
| def test_border_op_accepts_no_swaps() -> None: | ||
| """Scalar atom counts must remain readable when ``nswap == 0``.""" | ||
| sendproc, recvproc, sendnum, recvnum, communicator = _control_tensors(0) | ||
| g1 = paddle.arange(6, dtype="float64").reshape([2, 3]) | ||
|
|
||
| result = deepmd_op_pd.border_op( | ||
| paddle.zeros([0], dtype="int64"), | ||
| sendproc, | ||
| recvproc, | ||
| sendnum, | ||
| recvnum, | ||
| g1, | ||
| communicator, | ||
| paddle.to_tensor([2], dtype="int32"), | ||
| paddle.to_tensor([0], dtype="int32"), | ||
| ) | ||
|
|
||
| np.testing.assert_array_equal(result.numpy(), g1.numpy()) | ||
|
|
||
|
|
||
| def test_border_op_self_copy_uses_cpu_place() -> None: | ||
| """A CUDA-enabled operator must not use a GPU copy for CPU tensors.""" | ||
| sendproc, recvproc, sendnum, recvnum, communicator = _control_tensors(1) | ||
| sendnum = paddle.ones_like(sendnum) | ||
| recvnum = paddle.ones_like(recvnum) | ||
|
|
||
| # The C++ operator receives the LAMMPS send lists as pointer-valued int64 | ||
| # entries. Keep this NumPy owner alive through the call so the pointed-to | ||
| # int32 index remains valid. | ||
| send_indices = np.array([1], dtype=np.int32) | ||
| sendlist = paddle.to_tensor([send_indices.ctypes.data], dtype="int64") | ||
| g1_leaf = paddle.to_tensor( | ||
|
njzjz-bot marked this conversation as resolved.
|
||
| [[1.0, 2.0], [3.0, 4.0], [0.0, 0.0]], stop_gradient=False | ||
| ) | ||
| # Paddle rejects an in-place custom op on an autograd leaf. This identity | ||
| # keeps a leaf for checking gradients while letting border_op update g1. | ||
| g1 = g1_leaf * 1.0 | ||
|
|
||
| result = deepmd_op_pd.border_op( | ||
| sendlist, | ||
| sendproc, | ||
| recvproc, | ||
| sendnum, | ||
| recvnum, | ||
| g1, | ||
| communicator, | ||
| paddle.to_tensor([2], dtype="int32"), | ||
| paddle.to_tensor([1], dtype="int32"), | ||
| ) | ||
|
|
||
| np.testing.assert_array_equal( | ||
| result.numpy(), np.array([[1.0, 2.0], [3.0, 4.0], [3.0, 4.0]]) | ||
| ) | ||
| # Backpropagation runs the reverse self-swap, which needs the same | ||
| # place-based CPU/GPU dispatch as the forward copy. | ||
| result.sum().backward() | ||
| np.testing.assert_array_equal(g1_leaf.grad.numpy(), np.ones([3, 2])) | ||
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.