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

style: require explicit device and dtype #4001

Merged
merged 3 commits into from
Jul 24, 2024
Merged

Conversation

njzjz
Copy link
Member

@njzjz njzjz commented Jul 22, 2024

Add a linter to require explicit device and dtype for {np,tf,pt}.{tensor,zeros,ones,arange,range,empty,full,rand,eye,linspace}.

  • EDP01: Require explicit device when initializing a PyTorch tensor.
  • EDP02: Require explicit dtype when initializing a NumPy array, a TensorFlow tensor, or a PyTorch tensor.

Missing device or dtype often causes a problem.

Note: I am not going to fix the existing codes in this PR, as too many (200+) lines violate the rules. Fixing them takes a long time which I don't have. I disable rules on those lines.

Summary by CodeRabbit

  • New Features

    • Added enhanced linting capabilities through a new deepmd_checker plugin in the pyproject.toml configuration.
  • Bug Fixes

    • Improved code clarity and compliance by suppressing pylint warnings related to explicit data types across multiple files.
  • Documentation

    • Numerous comments added to clarify the suppression of linting messages, enhancing code maintainability and readability.

njzjz added 3 commits July 22, 2024 02:12
Signed-off-by: Jinzhe Zeng <jinzhe.zeng@rutgers.edu>
Signed-off-by: Jinzhe Zeng <jinzhe.zeng@rutgers.edu>
Signed-off-by: Jinzhe Zeng <jinzhe.zeng@rutgers.edu>
@njzjz njzjz requested review from iProzd and wanghan-iapcm July 22, 2024 07:21
Copy link
Contributor

coderabbitai bot commented Jul 22, 2024

Walkthrough

Walkthrough

This update introduces enhancements to the linting configuration and improves code quality across various files in the deepmd project. Key changes include the addition of Pylint integration with specific configurations to suppress warnings related to explicit data types in numpy and tensor operations. This promotes better code readability while maintaining functional integrity.

Changes

Files Change Summary
.pre-commit-config.yaml Added Pylint hook with custom setup to improve linting process.
deepmd/... (multiple files) Added comments to suppress Pylint warnings about explicit data types during numpy array and tensor initializations, enhancing clarity without changing logic.
pyproject.toml Introduced configuration for Pylint to load the deepmd_checker plugin and control messages reported by the linter.

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.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration 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.

@@ -190,7 +190,7 @@

# YWolfeee: support polar normalization, initialize to a more appropriate point
if self.shift_diag:
mean_polar = np.zeros([len(self.sel_type), 9])
mean_polar = np.zeros([len(self.sel_type), 9]) # pylint: disable=no-explicit-dtype

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable mean_polar is not used.
self.nei_type = tf.constant(nei_type, dtype=tf.int32)

avg_zero = np.zeros([self.ntypes, self.ndescrpt]).astype(
avg_zero = np.zeros([self.ntypes, self.ndescrpt]).astype( # pylint: disable=no-explicit-dtype

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable avg_zero is not used.
GLOBAL_NP_FLOAT_PRECISION
)
std_ones = np.ones([self.ntypes, self.ndescrpt]).astype(
std_ones = np.ones([self.ntypes, self.ndescrpt]).astype( # pylint: disable=no-explicit-dtype

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable std_ones is not used.
@@ -319,9 +322,9 @@

with tf.variable_scope("descrpt_attr" + suffix, reuse=reuse):
if davg is None:
davg = np.zeros([self.ntypes, self.ndescrpt])
davg = np.zeros([self.ntypes, self.ndescrpt]) # pylint: disable=no-explicit-dtype

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable davg is not used.
if dstd is None:
dstd = np.ones([self.ntypes, self.ndescrpt])
dstd = np.ones([self.ntypes, self.ndescrpt]) # pylint: disable=no-explicit-dtype

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable dstd is not used.
@@ -635,7 +635,7 @@
dmatrix = dmatrix.view(
-1, self.ndescrpt
) # shape is [nframes*nall, self.ndescrpt]
xyz_scatter = torch.empty(
xyz_scatter = torch.empty( # pylint: disable=no-explicit-dtype

Check warning

Code scanning / CodeQL

Variable defined multiple times Warning

This assignment to 'xyz_scatter' is unnecessary as it is
redefined
before this value is used.
deepmd/tf/nvnmd/entrypoints/mapt.py 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: 7

deepmd/tf/fit/polar.py Show resolved Hide resolved
deepmd/tf/descriptor/se_a_mask.py Show resolved Hide resolved
deepmd/tf/descriptor/se_a_mask.py Show resolved Hide resolved
deepmd/tf/descriptor/se_a_mask.py Show resolved Hide resolved
deepmd/tf/descriptor/se_a_mask.py Show resolved Hide resolved
deepmd/tf/nvnmd/entrypoints/mapt.py Show resolved Hide resolved
deepmd/pt/model/descriptor/se_a.py Show resolved Hide resolved
Copy link

codecov bot commented Jul 22, 2024

Codecov Report

Attention: Patch coverage is 75.58140% with 42 lines in your changes missing coverage. Please review.

Project coverage is 82.83%. Comparing base (6199b03) to head (2ebb158).
Report is 108 commits behind head on devel.

Files with missing lines Patch % Lines
deepmd/pt/utils/preprocess.py 0.00% 15 Missing ⚠️
deepmd/tf/infer/deep_eval.py 72.22% 5 Missing ⚠️
deepmd/pt/model/descriptor/se_r.py 50.00% 2 Missing ⚠️
deepmd/pt/model/descriptor/se_t.py 0.00% 2 Missing ⚠️
deepmd/tf/descriptor/loc_frame.py 50.00% 2 Missing ⚠️
deepmd/tf/infer/deep_tensor.py 0.00% 2 Missing ⚠️
deepmd/tf/nvnmd/descriptor/se_atten.py 0.00% 2 Missing ⚠️
deepmd/dpmodel/atomic_model/linear_atomic_model.py 0.00% 1 Missing ⚠️
deepmd/dpmodel/infer/deep_eval.py 50.00% 1 Missing ⚠️
deepmd/pt/model/descriptor/gaussian_lcc.py 0.00% 1 Missing ⚠️
... and 9 more
Additional details and impacted files
@@           Coverage Diff           @@
##            devel    #4001   +/-   ##
=======================================
  Coverage   82.83%   82.83%           
=======================================
  Files         522      522           
  Lines       50889    50901   +12     
  Branches     3015     3011    -4     
=======================================
+ Hits        42154    42165   +11     
- Misses       7798     7799    +1     
  Partials      937      937           

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

@njzjz
Copy link
Member Author

njzjz commented Jul 22, 2024

CodeQL alerts are not related to this PR (but it's expected to fix them in a separated PR)

@wanghan-iapcm wanghan-iapcm added this pull request to the merge queue Jul 23, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jul 23, 2024
@wanghan-iapcm wanghan-iapcm added this pull request to the merge queue Jul 23, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jul 23, 2024
@njzjz njzjz added this pull request to the merge queue Jul 24, 2024
Merged via the queue into deepmodeling:devel with commit 73ea96d Jul 24, 2024
59 of 60 checks passed
@njzjz njzjz deleted the checker branch July 24, 2024 04:01
mtaillefumier pushed a commit to mtaillefumier/deepmd-kit that referenced this pull request Sep 18, 2024
Add a linter to require explicit `device` and `dtype` for
`{np,tf,pt}.{tensor,zeros,ones,arange,range,empty,full,rand,eye,linspace}`.

- EDP01: Require explicit device when initializing a PyTorch tensor.
- EDP02: Require explicit dtype when initializing a NumPy array, a
TensorFlow tensor, or a PyTorch tensor.

Missing `device` or `dtype` often causes a problem.

Note: I am not going to fix the existing codes in this PR, as too many
(200+) lines violate the rules. Fixing them takes a long time which I
don't have. I disable rules on those lines.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Added enhanced linting capabilities through a new `deepmd_checker`
plugin in the `pyproject.toml` configuration.
  
- **Bug Fixes**
- Improved code clarity and compliance by suppressing pylint warnings
related to explicit data types across multiple files.

- **Documentation**
- Numerous comments added to clarify the suppression of linting
messages, enhancing code maintainability and readability.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Signed-off-by: Jinzhe Zeng <jinzhe.zeng@rutgers.edu>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants