-
Notifications
You must be signed in to change notification settings - Fork 629
fix: fix PT AutoBatchSize OOM bug and merge execute_all into base #4047
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
47d8002
fix: fix PT AutoBatchSize OOM bug and merge execute_all into base
njzjz f93aa1d
bump minimal numpy version
njzjz 74a8980
Use key in dict instead of key in dict.keys()
njzjz 9d38cb8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[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
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,141 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
| import os | ||
| import sys | ||
| import unittest | ||
|
|
||
| from deepmd.utils.batch_size import ( | ||
| AutoBatchSize, | ||
| ) | ||
| from deepmd.utils.errors import ( | ||
| OutOfMemoryError, | ||
| ) | ||
|
|
||
| if sys.version_info >= (3, 9): | ||
| import array_api_strict as xp | ||
| else: | ||
| raise unittest.SkipTest("array_api_strict doesn't support Python<=3.8") | ||
|
|
||
|
|
||
| class CustomizedAutoBatchSizeCPU(AutoBatchSize): | ||
| def is_gpu_available(self): | ||
| return False | ||
|
|
||
| def is_oom_error(self, e): | ||
| return isinstance(e, OutOfMemoryError) | ||
|
|
||
|
|
||
| class CustomizedAutoBatchSizeGPU(AutoBatchSize): | ||
| def is_gpu_available(self): | ||
| return True | ||
|
|
||
| def is_oom_error(self, e): | ||
| return isinstance(e, OutOfMemoryError) | ||
|
|
||
|
|
||
| class TestAutoBatchSize(unittest.TestCase): | ||
| def oom(self, batch_size, start_index): | ||
| if batch_size >= 512: | ||
| raise OutOfMemoryError | ||
| return batch_size, xp.zeros((batch_size, 2)) | ||
|
|
||
| def test_execute_oom_gpu(self): | ||
| # initial batch size 256 = 128 * 2 | ||
| auto_batch_size = CustomizedAutoBatchSizeGPU(256, 2.0) | ||
| # no error - 128 | ||
| nb, result = auto_batch_size.execute(self.oom, 1, 2) | ||
| self.assertEqual(nb, 128) | ||
| self.assertEqual(result.shape, (128, 2)) | ||
| # no error - 256 | ||
| nb, result = auto_batch_size.execute(self.oom, 1, 2) | ||
| self.assertEqual(nb, 256) | ||
| self.assertEqual(result.shape, (256, 2)) | ||
| # error - 512 return 0, None | ||
| nb, result = auto_batch_size.execute(self.oom, 1, 2) | ||
| self.assertEqual(nb, 0) | ||
| self.assertIsNone(result) | ||
| # 256 again | ||
| nb, result = auto_batch_size.execute(self.oom, 1, 2) | ||
| self.assertEqual(nb, 256) | ||
| self.assertEqual(result.shape, (256, 2)) | ||
| # 256 again | ||
| nb, result = auto_batch_size.execute(self.oom, 1, 2) | ||
| self.assertEqual(nb, 256) | ||
| self.assertEqual(result.shape, (256, 2)) | ||
|
|
||
| def test_execute_oom_cpu(self): | ||
| # initial batch size 256 = 128 * 2, nb is always 128 | ||
| auto_batch_size = CustomizedAutoBatchSizeCPU(256, 2.0) | ||
| nb, result = auto_batch_size.execute(self.oom, 1, 2) | ||
| self.assertEqual(nb, 128) | ||
| self.assertEqual(result.shape, (128, 2)) | ||
| nb, result = auto_batch_size.execute(self.oom, 1, 2) | ||
| self.assertEqual(nb, 128) | ||
| self.assertEqual(result.shape, (128, 2)) | ||
| nb, result = auto_batch_size.execute(self.oom, 1, 2) | ||
| self.assertEqual(nb, 128) | ||
| self.assertEqual(result.shape, (128, 2)) | ||
| nb, result = auto_batch_size.execute(self.oom, 1, 2) | ||
| self.assertEqual(nb, 128) | ||
| self.assertEqual(result.shape, (128, 2)) | ||
| nb, result = auto_batch_size.execute(self.oom, 1, 2) | ||
| self.assertEqual(nb, 128) | ||
| self.assertEqual(result.shape, (128, 2)) | ||
|
|
||
| @unittest.mock.patch.dict(os.environ, {"DP_INFER_BATCH_SIZE": "256"}, clear=True) | ||
| def test_execute_oom_environment_variables(self): | ||
| # DP_INFER_BATCH_SIZE = 256 = 128 * 2, nb is always 128 | ||
| auto_batch_size = CustomizedAutoBatchSizeGPU(999, 2.0) | ||
| nb, result = auto_batch_size.execute(self.oom, 1, 2) | ||
| self.assertEqual(nb, 128) | ||
| self.assertEqual(result.shape, (128, 2)) | ||
| nb, result = auto_batch_size.execute(self.oom, 1, 2) | ||
| self.assertEqual(nb, 128) | ||
| self.assertEqual(result.shape, (128, 2)) | ||
| nb, result = auto_batch_size.execute(self.oom, 1, 2) | ||
| self.assertEqual(nb, 128) | ||
| self.assertEqual(result.shape, (128, 2)) | ||
| nb, result = auto_batch_size.execute(self.oom, 1, 2) | ||
| self.assertEqual(nb, 128) | ||
| self.assertEqual(result.shape, (128, 2)) | ||
| nb, result = auto_batch_size.execute(self.oom, 1, 2) | ||
| self.assertEqual(nb, 128) | ||
| self.assertEqual(result.shape, (128, 2)) | ||
|
|
||
| def test_execute_all(self): | ||
| dd1 = xp.zeros((10000, 2, 1)) | ||
| auto_batch_size = CustomizedAutoBatchSizeGPU(256, 2.0) | ||
| dd2 = auto_batch_size.execute_all(xp.asarray, 10000, 2, dd1) | ||
| assert xp.all(dd1 == dd2) | ||
|
|
||
| def test_execute_all_dict(self): | ||
| dd0 = xp.zeros((10000, 2, 1, 3, 4)) | ||
| dd1 = xp.ones((10000, 2, 1, 3, 4)) | ||
| auto_batch_size = CustomizedAutoBatchSizeGPU(256, 2.0) | ||
|
|
||
| def func(dd1): | ||
| return { | ||
| "foo": xp.zeros_like(dd1), | ||
| "bar": xp.ones_like(dd1), | ||
| } | ||
|
|
||
| dd2 = auto_batch_size.execute_all(func, 10000, 2, dd1) | ||
| assert xp.all(dd0 == dd2["foo"]) | ||
| assert xp.all(dd1 == dd2["bar"]) | ||
|
|
||
| def test_execute_all_dict_oom(self): | ||
| # to reproduce #4036 when commenting "if n_batch == 0: continue" | ||
| dd0 = xp.zeros((10, 2, 1, 3, 4)) | ||
| dd1 = xp.ones((10, 2, 1, 3, 4)) | ||
| auto_batch_size = CustomizedAutoBatchSizeGPU(4, 2.0) | ||
|
|
||
| def func(dd1): | ||
| if dd1.shape[0] >= 2: | ||
| raise OutOfMemoryError | ||
| return { | ||
| "foo": xp.zeros_like(dd1), | ||
| "bar": xp.ones_like(dd1), | ||
| } | ||
|
|
||
| dd2 = auto_batch_size.execute_all(func, 10, 2, dd1) | ||
| assert xp.all(dd0 == dd2["foo"]) | ||
| assert xp.all(dd1 == dd2["bar"]) |
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.