deepmodeling / deepmd-kit

A deep learning package for many-body potential energy representation and molecular dynamics
https://docs.deepmodeling.com/projects/deepmd/
GNU Lesser General Public License v3.0
1.41k stars 487 forks source link

feat: support seed for pt/dp models #3773

Closed iProzd closed 1 month ago

iProzd commented 2 months ago

Summary by CodeRabbit

coderabbitai[bot] commented 2 months ago
Walkthrough ## Walkthrough The recent updates in the `deepmd` module aim to enhance reproducibility by introducing optional `seed` parameters in various classes and functions, allowing users to control random number generation for improved consistency. Additionally, new tensor initialization methods have been added to provide diverse strategies for effectively initializing neural network weights. ## Changes | Files/Paths | Change Summary | |---------------------------------------------------|---------------------------------------------------------------------------------------------------| | `deepmd/pt/model/descriptor/dpa1.py` | Added `seed` parameter to `DescrptDPA1` class for random seed initialization. | | `deepmd/pt/model/descriptor/dpa2.py` | Added `seed` parameter in `DPA2` class affecting parameter initialization and type embedding. | | `deepmd/pt/model/network/init.py` | Introduced various tensor initialization functions based on `torch.nn.init`. | | `source/tests/pt/model/test_forward_lower.py` | Doubled selection for virtual atoms in `setUp` function to avoid large errors in different setups. | ## Possibly related issues - **Issue deepmodeling/deepmd-kit#3467**: The changes in this update align with enforcing explicit seeds for random methods to ensure reproducibility, particularly within the `deepmd/pt` module.

Recent Review Details **Configuration used: CodeRabbit UI** **Review profile: CHILL**
Commits Files that changed from the base of the PR and between ac73c23342d741fb822006b118faaa8628afc865 and ad594b62b34e02996a9348e7dcb6876759899597.
Files selected for processing (4) * deepmd/pt/model/descriptor/dpa1.py (4 hunks) * deepmd/pt/model/descriptor/dpa2.py (6 hunks) * deepmd/pt/model/network/init.py (1 hunks) * source/tests/pt/model/test_forward_lower.py (2 hunks)
Files skipped from review as they are similar to previous changes (2) * deepmd/pt/model/descriptor/dpa1.py * deepmd/pt/model/descriptor/dpa2.py
Additional comments not posted (11)
source/tests/pt/model/test_forward_lower.py (2)
`170-171`: Doubling the `sel` parameter for virtual atoms in tests seems justified to avoid large errors. Ensure this adjustment aligns with expected model behavior. --- `181-183`: Adjusting `nsel` parameters in `repinit` and `repformer` for virtual atoms in tests is noted. Verify that these changes do not adversely affect the model's expected behavior.
deepmd/pt/model/network/init.py (9)
`182-193`: Using a no_grad wrapper in `zeros_` function aligns with best practices for tensor initialization. --- `196-207`: Using a no_grad wrapper in `ones_` function is correctly implemented. --- `210-226`: Proper use of no_grad wrapper and handling of torch function overrides in `constant_` function. --- `229-254`: Correct implementation of no_grad wrapper and torch function overrides in `normal_` function. --- `257-286`: Detailed and correct implementation of the truncated normal distribution in `trunc_normal_` function. --- `347-390`: Correct implementation of `kaiming_normal_` function using the Kaiming normal distribution. --- `393-422`: Correct implementation of `xavier_uniform_` function using the Xavier uniform distribution. --- `425-454`: Correct implementation of `xavier_normal_` function using the Xavier normal distribution. --- `289-344`: Implementation of `kaiming_uniform_` function is correct. Verify the calculations of `fan` and `gain` to ensure they are accurate.
Verification successful --- The implementations of `calculate_gain` and `_calculate_correct_fan` have been successfully extracted. Both functions appear to be correctly implemented according to the expected formulas for Kaiming initialization. - `calculate_gain` correctly returns the gain values for different nonlinearities, including `relu`, `leaky_relu`, and others. - `_calculate_correct_fan` correctly calculates the fan value based on the mode (`fan_in` or `fan_out`). The calculations of `fan` and `gain` in the `kaiming_uniform_` function are accurate.
Scripts Executed The following scripts were executed for the analysis: --- Script: ```shell #!/bin/bash # Description: Verify the calculations of `fan` and `gain` in `kaiming_uniform_`. # Test: Search for usage of `fan` and `gain` calculations. Expect: Accurate calculations. rg --type python $'fan|gain' ``` Length of output: 60 --- --- Script: ```shell #!/bin/bash # Description: Verify the calculations of `fan` and `gain` in `kaiming_uniform_`. # Test: Search for definitions and usages of `fan` and `gain` in the codebase. rg 'fan|gain' ``` Length of output: 24010 --- --- Script: ```shell #!/bin/bash # Description: Extract the implementations of `calculate_gain` and `_calculate_correct_fan` from `deepmd/pt/model/network/init.py`. # Extract the implementation of `calculate_gain`. rg --pcre2 --multiline --multiline-dotall 'def calculate_gain\([^)]*\):.*?def ' deepmd/pt/model/network/init.py # Extract the implementation of `_calculate_correct_fan`. rg --pcre2 --multiline --multiline-dotall 'def _calculate_correct_fan\([^)]*\):.*?def ' deepmd/pt/model/network/init.py ``` Length of output: 3364
--- Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?
Share - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code)
Tips ### Chat There are 3 ways to chat with [CodeRabbit](https://coderabbit.ai): - Review comments: Directly reply to a review comment made by CodeRabbit. Example: - `I pushed a fix in commit .` - `Generate unit testing code for this file.` - `Open a follow-up GitHub issue for this discussion.` - Files and specific lines of code (under the "Files changed" tab): Tag `@coderabbitai` in a new review comment at the desired location with your query. Examples: - `@coderabbitai generate unit testing code for this file.` - `@coderabbitai modularize this function.` - PR comments: Tag `@coderabbitai` in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples: - `@coderabbitai generate interesting stats about this repository and render them as a table.` - `@coderabbitai show all the console.log statements in this repository.` - `@coderabbitai read src/utils.ts and generate unit testing code.` - `@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.` Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. ### CodeRabbit Commands (invoked as PR comments) - `@coderabbitai pause` to pause the reviews on a PR. - `@coderabbitai resume` to resume the paused reviews. - `@coderabbitai review` to trigger a review. This is useful when automatic reviews are disabled for the repository. - `@coderabbitai resolve` resolve all the CodeRabbit review comments. - `@coderabbitai help` to get help. Additionally, you can add `@coderabbitai ignore` anywhere in the PR description to prevent this PR from being reviewed. ### CodeRabbit Configration File (`.coderabbit.yaml`) - You can programmatically configure CodeRabbit by adding a `.coderabbit.yaml` file to the root of your repository. - Please see the [configuration documentation](https://docs.coderabbit.ai/guides/configure-coderabbit) for more information. - If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: `# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json` ### Documentation and Community - Visit our [Documentation](https://coderabbit.ai/docs) for detailed information on how to use CodeRabbit. - Join our [Discord Community](https://discord.com/invite/GsXnASn26c) to get help, request features, and share feedback. - Follow us on [X/Twitter](https://twitter.com/coderabbitai) for updates and announcements.
codecov[bot] commented 2 months ago

Codecov Report

Attention: Patch coverage is 0% with 167 lines in your changes are missing coverage. Please review.

Project coverage is 35.63%. Comparing base (81b5949) to head (ad594b6).

Files Patch % Lines
deepmd/pt/model/network/init.py 0.00% 113 Missing :warning:
deepmd/pt/model/network/mlp.py 0.00% 17 Missing :warning:
deepmd/pt/model/descriptor/repformer_layer.py 0.00% 12 Missing :warning:
deepmd/pt/model/network/layernorm.py 0.00% 7 Missing :warning:
deepmd/pt/utils/utils.py 0.00% 6 Missing :warning:
deepmd/pt/model/descriptor/se_atten.py 0.00% 4 Missing :warning:
deepmd/pt/model/descriptor/repformers.py 0.00% 2 Missing :warning:
deepmd/pt/train/training.py 0.00% 2 Missing :warning:
deepmd/dpmodel/utils/network.py 0.00% 1 Missing :warning:
deepmd/pt/model/descriptor/se_a.py 0.00% 1 Missing :warning:
... and 2 more
Additional details and impacted files ```diff @@ Coverage Diff @@ ## devel #3773 +/- ## =========================================== - Coverage 82.58% 35.63% -46.95% =========================================== Files 515 516 +1 Lines 48806 48923 +117 Branches 2982 2982 =========================================== - Hits 40308 17436 -22872 - Misses 7587 30878 +23291 + Partials 911 609 -302 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

wanghan-iapcm commented 1 month ago

I approve this PR, but it looks to me that seed is not passed from descriptors/fittings to the network in dpmodel, which needs to be fixed in the future.

an issue is necessary before this PR can be merged.

iProzd commented 1 month ago

I approve this PR, but it looks to me that seed is not passed from descriptors/fittings to the network in dpmodel, which needs to be fixed in the future.

an issue is necessary before this PR can be merged.

I've opened an issue about this: #3799