-
Notifications
You must be signed in to change notification settings - Fork 641
fix(tf/pt): validate GPU tabulation sizes #5846
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
Draft
njzjz-bot
wants to merge
6
commits into
deepmodeling:master
Choose a base branch
from
njzjz-bot:fix/tf-tabulate-gpu-size-validation-5654
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.
+493
−36
Draft
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6ce049d
fix(tf): validate GPU tabulation sizes
17b44cb
Merge remote-tracking branch 'origin/master' into fix/tf-tabulate-gpu…
03d6501
fix(pt): validate GPU tabulation sizes before launch
8183850
test(pt): cover tabulation gradient size guards
b88dd44
fix(tabulate): validate GPU widths before allocation
njzjz-bot 58dfadb
fix(pt): allow wide SE-T-TEBD tabulation
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,89 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
| """Reject invalid tabulation widths before any PyTorch GPU kernel launch.""" | ||
|
|
||
| import unittest | ||
|
|
||
| import torch | ||
|
|
||
| from deepmd.pt.cxx_op import ( | ||
| ENABLE_CUSTOMIZED_OP, | ||
| ) | ||
|
|
||
| ERROR_MESSAGE = "last_layer_size must be between 1 and 1024" | ||
|
|
||
|
|
||
| @unittest.skipIf(not ENABLE_CUSTOMIZED_OP, "PyTorch customized OPs are not built") | ||
| @unittest.skipUnless( | ||
| torch.cuda.is_available(), "GPU tabulation validation requires a GPU" | ||
| ) | ||
| class TestTabulateGpuSizeValidation(unittest.TestCase): | ||
| """The GPU wrappers must fail on the host, not inside a launch config.""" | ||
|
|
||
| dtype = torch.float64 | ||
|
|
||
| def _zeros(self, *shape: int) -> torch.Tensor: | ||
| return torch.zeros(shape, dtype=self.dtype, device="cuda") | ||
|
|
||
| def _table(self, last_layer_size: int) -> torch.Tensor: | ||
| return self._zeros(1, max(1, 6 * last_layer_size)) | ||
|
|
||
| def _table_info(self) -> torch.Tensor: | ||
| # table_info stays on the CPU for every backend. | ||
| return torch.tensor([0.0, 1.0, 1.0, 1.0, 1.0, 1.0], dtype=self.dtype) | ||
|
|
||
| def _forward_calls(self, last_layer_size: int) -> dict[str, callable]: | ||
| table = self._table(last_layer_size) | ||
| table_info = self._table_info() | ||
| em_x = self._zeros(1, 1) | ||
| em_a = self._zeros(1, 1, 4) | ||
| em_t = self._zeros(1, 1, 1) | ||
| em_r = self._zeros(1, 1) | ||
| two_embed = self._zeros(1, max(1, last_layer_size)) | ||
| return { | ||
| "se_a": lambda: torch.ops.deepmd.tabulate_fusion_se_a( | ||
| table, table_info, em_x, em_a, last_layer_size | ||
| ), | ||
| "se_atten": lambda: torch.ops.deepmd.tabulate_fusion_se_atten( | ||
| table, table_info, em_x, em_a, two_embed, last_layer_size, True | ||
| ), | ||
| "se_t": lambda: torch.ops.deepmd.tabulate_fusion_se_t( | ||
| table, table_info, em_x, em_t, last_layer_size | ||
| ), | ||
| "se_t_tebd": lambda: torch.ops.deepmd.tabulate_fusion_se_t_tebd( | ||
| table, table_info, em_x, em_t, last_layer_size | ||
| ), | ||
| "se_r": lambda: torch.ops.deepmd.tabulate_fusion_se_r( | ||
| table, table_info, em_r, last_layer_size | ||
| ), | ||
| } | ||
|
|
||
| def _assert_rejected(self, last_layer_size: int) -> None: | ||
| for name, call in self._forward_calls(last_layer_size).items(): | ||
| with self.subTest(op=name, last_layer_size=last_layer_size): | ||
| with self.assertRaisesRegex(RuntimeError, ERROR_MESSAGE): | ||
| call() | ||
|
|
||
| def test_rejects_oversized_width(self) -> None: | ||
| """A width past the maximum block dimension must be refused.""" | ||
| self._assert_rejected(1025) | ||
|
|
||
| def test_rejects_zero_width(self) -> None: | ||
| """A zero width would divide by zero while sizing the launch.""" | ||
| self._assert_rejected(0) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| def test_gradient_paths_reject_oversized_width(self) -> None: | ||
| """The autograd wrappers derive the width from the descriptor.""" | ||
| last_layer_size = 1025 | ||
| table = self._table(last_layer_size) | ||
| table_info = self._table_info() | ||
| em_x = self._zeros(1, 1).requires_grad_(True) | ||
| em_a = self._zeros(1, 1, 4).requires_grad_(True) | ||
| with self.assertRaisesRegex(RuntimeError, ERROR_MESSAGE): | ||
| descriptor = torch.ops.deepmd.tabulate_fusion_se_a( | ||
| table, table_info, em_x, em_a, last_layer_size | ||
| )[0] | ||
| descriptor.sum().backward() | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| 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.
Uh oh!
There was an error while loading. Please reload this page.