Skip to content
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

feat: support seed for pt/dp models #3773

Merged
merged 13 commits into from
May 22, 2024
Merged

Conversation

iProzd
Copy link
Collaborator

@iProzd iProzd commented May 12, 2024

Summary by CodeRabbit

  • New Features

    • Added various neural network weight initialization methods: zeros, ones, constants, normal distributions, truncated normal distributions, Kaiming distributions, and Xavier distributions.
  • Improvements

    • Included optional seed parameter in initialization methods and classes.
    • Implemented a get_generator function for random seed management.
  • Bug Fixes

    • Addressed potential unintended behavior by ensuring proper random seed setting during training processes.

Copy link
Contributor

coderabbitai bot commented May 12, 2024

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


Recent Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between ac73c23 and ad594b6.
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:

#!/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:

#!/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:

#!/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
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • 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 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 for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 9

Out of diff range and nitpick comments (6)
deepmd/pt/model/descriptor/se_a.py (2)

Line range hint 85-103: Ensure proper documentation for the new seed parameter in the constructor.

It's important to maintain comprehensive documentation, especially for public APIs. Consider adding a docstring entry for the seed parameter explaining its purpose and usage.


Line range hint 333-404: Ensure proper documentation for the new seed parameter in the constructor.

Similar to the previous class, adding a docstring entry for the seed parameter would help users understand its purpose and how it affects the initialization of the descriptor.

deepmd/dpmodel/utils/network.py (4)

81-87: Ensure proper documentation for the new seed parameter in the constructor.

Adding a docstring entry for the seed parameter would help users understand its purpose and how it affects the initialization of the layer weights and biases.


Line range hint 317-330: Ensure proper documentation for the new seed parameter in the constructor.

As with other classes, adding a docstring entry for the seed parameter in LayerNorm would clarify its role in the initialization process.


Line range hint 575-590: Ensure proper documentation for the new seed parameter in the constructor.

Documentation for the seed parameter in EmbeddingNet is crucial for users to understand its impact on network initialization.


Line range hint 677-697: Ensure proper documentation for the new seed parameter in the constructor.

Adding a docstring entry for the seed parameter in FittingNet would help users understand its purpose and how it affects the initialization of the network.

deepmd/pt/model/descriptor/repformer_layer.py Show resolved Hide resolved
deepmd/pt/model/descriptor/repformer_layer.py Show resolved Hide resolved
deepmd/pt/model/descriptor/repformer_layer.py Show resolved Hide resolved
deepmd/pt/model/descriptor/repformer_layer.py Show resolved Hide resolved
deepmd/pt/model/descriptor/repformer_layer.py Show resolved Hide resolved
deepmd/pt/model/descriptor/repformer_layer.py Show resolved Hide resolved
deepmd/pt/model/descriptor/dpa2.py Show resolved Hide resolved
deepmd/pt/model/descriptor/dpa2.py Show resolved Hide resolved
deepmd/pt/model/descriptor/dpa2.py Show resolved Hide resolved
Copy link

codecov bot commented May 12, 2024

Codecov Report

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

Project coverage is 35.63%. Comparing base (81b5949) to head (ad594b6).
Report is 135 commits behind head on devel.

Files Patch % Lines
deepmd/pt/model/network/init.py 0.00% 113 Missing ⚠️
deepmd/pt/model/network/mlp.py 0.00% 17 Missing ⚠️
deepmd/pt/model/descriptor/repformer_layer.py 0.00% 12 Missing ⚠️
deepmd/pt/model/network/layernorm.py 0.00% 7 Missing ⚠️
deepmd/pt/utils/utils.py 0.00% 6 Missing ⚠️
deepmd/pt/model/descriptor/se_atten.py 0.00% 4 Missing ⚠️
deepmd/pt/model/descriptor/repformers.py 0.00% 2 Missing ⚠️
deepmd/pt/train/training.py 0.00% 2 Missing ⚠️
deepmd/dpmodel/utils/network.py 0.00% 1 Missing ⚠️
deepmd/pt/model/descriptor/se_a.py 0.00% 1 Missing ⚠️
... and 2 more

❗ There is a different number of reports uploaded between BASE (81b5949) and HEAD (ad594b6). Click for more details.

HEAD has 25 uploads less than BASE
Flag BASE (81b5949) HEAD (ad594b6)
27 2
Additional details and impacted files
@@             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     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

deepmd/pt/model/network/layernorm.py Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

Out of diff range and nitpick comments (2)
deepmd/pt/model/descriptor/dpa1.py (1)

175-176: Add documentation for the seed parameter in the class docstring.

It's good practice to document all parameters in the class docstring to improve code maintainability and readability.

deepmd/pt/model/descriptor/dpa2.py (1)

106-106: Add documentation for the seed parameter in the class docstring.

It's good practice to document all parameters in the class docstring to improve code maintainability and readability.

deepmd/pt/model/descriptor/dpa1.py Show resolved Hide resolved
deepmd/pt/model/descriptor/dpa1.py Outdated Show resolved Hide resolved
deepmd/pt/model/descriptor/dpa2.py Outdated Show resolved Hide resolved
@iProzd iProzd requested a review from njzjz May 20, 2024 03:44
deepmd/pt/model/descriptor/dpa1.py Fixed Show fixed Hide fixed
deepmd/pt/model/descriptor/dpa2.py Fixed Show fixed Hide fixed
Copy link
Member

@njzjz njzjz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@wanghan-iapcm
Copy link
Collaborator

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
Copy link
Collaborator Author

iProzd commented May 21, 2024

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

@wanghan-iapcm wanghan-iapcm added this pull request to the merge queue May 22, 2024
Merged via the queue into deepmodeling:devel with commit bc84139 May 22, 2024
60 checks passed
mtaillefumier pushed a commit to mtaillefumier/deepmd-kit that referenced this pull request Sep 18, 2024
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
- Added various neural network weight initialization methods: zeros,
ones, constants, normal distributions, truncated normal distributions,
Kaiming distributions, and Xavier distributions.

- **Improvements**
- Included optional `seed` parameter in initialization methods and
classes.
  - Implemented a `get_generator` function for random seed management.

- **Bug Fixes**
- Addressed potential unintended behavior by ensuring proper random seed
setting during training processes.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Signed-off-by: Duo <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants