-
Notifications
You must be signed in to change notification settings - Fork 585
feat(dpmodel): support Array API learning rate #5143
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
+122
−13
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
836648c
feat(dpmodel): support Array API learning rate
njzjz c049d44
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ae6fd6b
arary -> array
njzjz e02e057
class name
njzjz adf087f
type hints
njzjz a2507f7
use Array API 2024.12
njzjz 84906e8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8ab8e99
improve how to get xp
njzjz 375a096
Merge branch 'array-api-learning-rate' of https://github.com/njzjz/de…
njzjz 0efdc3d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 188c173
refactor
njzjz 98a42be
Merge branch 'array-api-learning-rate' of https://github.com/njzjz/de…
njzjz 615f692
[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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
| import sys | ||
| import unittest | ||
|
|
||
| import numpy as np | ||
|
|
||
| from deepmd.dpmodel.array_api import ( | ||
| Array, | ||
| ) | ||
| from deepmd.dpmodel.common import ( | ||
| to_numpy_array, | ||
| ) | ||
| from deepmd.dpmodel.utils.learning_rate import ( | ||
| BaseLR, | ||
| ) | ||
|
|
||
| from .common import ( | ||
| INSTALLED_ARRAY_API_STRICT, | ||
| INSTALLED_JAX, | ||
| INSTALLED_PT, | ||
| parameterized, | ||
| ) | ||
|
|
||
| if INSTALLED_PT: | ||
| from deepmd.pt.utils.utils import ( | ||
| to_torch_tensor, | ||
| ) | ||
|
|
||
| if INSTALLED_JAX: | ||
| from deepmd.jax.env import ( | ||
| jnp, | ||
| ) | ||
| if INSTALLED_ARRAY_API_STRICT: | ||
| import array_api_strict as xp | ||
|
|
||
|
|
||
| @parameterized( | ||
| ( | ||
| { | ||
| "type": "exp", | ||
| "start_lr": 1e-3, | ||
| "stop_lr": 1e-8, | ||
| "decay_steps": 1000, | ||
| "stop_steps": 1000000, | ||
| }, | ||
| { | ||
| "type": "cosine", | ||
| "start_lr": 1e-3, | ||
| "stop_lr": 1e-8, | ||
| "decay_steps": 1000, | ||
| "stop_steps": 1000000, | ||
| }, | ||
| ), | ||
| ) | ||
| class TestLearningRateConsistent(unittest.TestCase): | ||
| def setUp(self) -> None: | ||
| (lr_param,) = self.param | ||
| self.lr = BaseLR(**lr_param) | ||
| self.step = 500000 | ||
| self.ref = self.lr.value(self.step) | ||
|
|
||
| def compare_test_with_ref(self, step: Array) -> None: | ||
| test = self.lr.value(step) | ||
| np.testing.assert_allclose(self.ref, to_numpy_array(test), atol=1e-10) | ||
|
|
||
| def compare_numpy_with_ref(self, step: Array) -> None: | ||
| self.compare_test_with_ref(np.asarray(step)) | ||
|
|
||
| @unittest.skipUnless(INSTALLED_PT, "PyTorch is not installed") | ||
| def test_pt_consistent_with_ref(self) -> None: | ||
| self.compare_test_with_ref(to_torch_tensor(self.step)) | ||
|
|
||
| @unittest.skipUnless( | ||
| INSTALLED_ARRAY_API_STRICT, "array_api_strict is not installed" | ||
| ) | ||
njzjz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @unittest.skipUnless( | ||
| sys.version_info >= (3, 9), "array_api_strict doesn't support Python<=3.8" | ||
| ) | ||
| def test_array_api_strict(self) -> None: | ||
| self.compare_test_with_ref(xp.asarray(self.step)) | ||
|
|
||
| @unittest.skipUnless(INSTALLED_JAX, "JAX is not installed") | ||
| def test_jax_consistent_with_ref(self) -> None: | ||
| self.compare_test_with_ref(jnp.array(self.step)) | ||
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.