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

fix(dp/pt): support auto sel for dpa2 #4323

Merged
merged 1 commit into from
Nov 8, 2024

Conversation

iProzd
Copy link
Collaborator

@iProzd iProzd commented Nov 7, 2024

Fix #4314 .

Summary by CodeRabbit

  • New Features

    • Enhanced selection update functionality to accommodate three-body interactions.
    • Expanded argument handling to allow flexible input types for neighbor selection parameters.
  • Bug Fixes

    • Improved error handling and robustness in the selection update process.
  • Documentation

    • Updated documentation strings for clarity on new input types and usage constraints.
  • Tests

    • Added a new test method to validate the functionality of the updated selection method for the dpa2 model.

Copy link
Contributor

coderabbitai bot commented Nov 7, 2024

📝 Walkthrough

Walkthrough

This pull request introduces enhancements to the DescrptDPA2 class in two files, deepmd/dpmodel/descriptor/dpa2.py and deepmd/pt/model/descriptor/dpa2.py, focusing on the update_sel method to include three-body interaction selection parameters. Additionally, the deepmd/utils/argcheck.py file has been updated to allow more flexible argument types for selection parameters. A new test method has been added to validate these changes. Overall, the modifications improve the handling of three-body interactions and the clarity of argument documentation.

Changes

File Change Summary
deepmd/dpmodel/descriptor/dpa2.py, deepmd/pt/model/descriptor/dpa2.py Updated update_sel method in DescrptDPA2 class to include logic for three-body selection parameters, ensuring proper handling of three_body_rcut and three_body_sel. No changes to the constructor of RepinitArgs and RepformerArgs.
deepmd/utils/argcheck.py Expanded documentation and updated argument types for nsel and three_body_sel to accept both int and str. Adjustments made to improve clarity on usage and constraints.
source/tests/pt/test_update_sel.py Added new test method test_update_sel_dpa2_auto to validate the update_sel function for the dpa2 model, ensuring the correct update of selection fields from "auto" to numerical values.

Possibly related PRs

  • fix(pt/dp): share params of repinit_three_body #4139: The changes in this PR also modify the dpa2.py file, specifically enhancing the handling of three-body interactions, which is directly related to the updates made in the main PR's update_sel method for three-body selection parameters.

Suggested labels

Python, Docs

Suggested reviewers

  • njzjz
  • wanghan-iapcm
  • Chengqian-Zhang

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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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.

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: 1

🧹 Outside diff range and nitpick comments (4)
source/tests/pt/test_update_sel.py (2)

177-194: Add docstring to explain test configuration.

Consider adding a docstring to explain the dpa2 descriptor configuration, particularly the relationship between repinit and repformer parameters, and the significance of the different rcut values.

 @patch("deepmd.pt.utils.update_sel.UpdateSel.get_nbor_stat")
 def test_update_sel_dpa2_auto(self, sel_mock):
+    """Test update_sel with dpa2 descriptor using auto selection.
+    
+    Tests the conversion of 'auto' selection parameters in both repinit
+    (including three-body interactions) and repformer configurations.
+    The repinit uses a larger rcut (6.0) than repformer (4.0) to capture
+    different interaction ranges.
+    """

213-214: Add specific assertions for individual parameters.

While comparing the entire structure is good, consider adding specific assertions for individual parameters to:

  1. Make test failures more descriptive
  2. Explicitly verify each auto-selection conversion
 jdata = update_sel(jdata)
 self.assertEqual(jdata, expected_out)
+# Add specific assertions
+descriptor = jdata["model"]["descriptor"]
+self.assertEqual(descriptor["repinit"]["nsel"], 28, "repinit nsel not converted correctly")
+self.assertEqual(descriptor["repinit"]["three_body_sel"], 28, "three_body_sel not converted correctly")
+self.assertEqual(descriptor["repformer"]["nsel"], 28, "repformer nsel not converted correctly")
deepmd/pt/model/descriptor/dpa2.py (1)

845-852: LGTM! Consider adding validation for three-body selection parameters.

The implementation correctly adds support for three-body interaction selection parameters, following the same pattern as the existing two-body selection updates. The code is well-structured and maintains consistency with the existing implementation.

Consider adding validation to ensure that the three-body selection parameters are compatible with the two-body parameters. For example:

 min_nbor_dist, repinit_three_body_sel = update_sel.update_one_sel(
     train_data,
     type_map,
     local_jdata_cpy["repinit"]["three_body_rcut"],
     local_jdata_cpy["repinit"]["three_body_sel"],
     True,
 )
+# Validate three-body selection parameters
+if repinit_three_body_sel[0] > repinit_sel[0]:
+    raise ValueError("Three-body selection count cannot be larger than two-body selection count")
 local_jdata_cpy["repinit"]["three_body_sel"] = repinit_three_body_sel[0]
deepmd/dpmodel/descriptor/dpa2.py (1)

1060-1067: Add error handling for missing parameters.

Consider adding validation to ensure three_body_rcut and three_body_sel exist in the input configuration when needed.

+        if "three_body_rcut" not in local_jdata_cpy["repinit"] or "three_body_sel" not in local_jdata_cpy["repinit"]:
+            raise KeyError("Missing required three-body parameters in configuration")
         min_nbor_dist, repinit_three_body_sel = update_sel.update_one_sel(
             train_data,
             type_map,
             local_jdata_cpy["repinit"]["three_body_rcut"],
             local_jdata_cpy["repinit"]["three_body_sel"],
             True,
         )
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 3236db5 and 15bba89.

📒 Files selected for processing (4)
  • deepmd/dpmodel/descriptor/dpa2.py (1 hunks)
  • deepmd/pt/model/descriptor/dpa2.py (1 hunks)
  • deepmd/utils/argcheck.py (5 hunks)
  • source/tests/pt/test_update_sel.py (1 hunks)
🔇 Additional comments (4)
source/tests/pt/test_update_sel.py (1)

173-175: LGTM! Test setup follows established patterns.

The test method setup and mock configuration are consistent with other test methods in the file.

deepmd/dpmodel/descriptor/dpa2.py (1)

1060-1067: LGTM! Verify three-body parameters existence.

The implementation correctly updates the three-body selection parameters using the same pattern as other selections. However, we should verify that the required parameters are always present in the input configuration.

✅ Verification successful

Parameters are well-defined with proper defaults and documentation

The three-body parameters are properly integrated in the codebase:

  • Default values are defined in RepinitArgs class and argcheck.py
  • Parameters are documented with clear descriptions
  • Example configurations in /examples/water/dpa2/ demonstrate correct usage
  • Test files show both "auto" and fixed value configurations
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if three-body parameters are properly documented in example configs
# and if there are any configs missing these parameters

# Test: Look for three-body parameter usage in configuration files
rg -A 5 'three_body_(rcut|sel)' 

Length of output: 13153

deepmd/utils/argcheck.py (2)

943-945: Documentation looks good!

The documentation for selection parameters (nsel and three_body_sel) is clear, consistent, and provides good examples. The auto-selection feature is well explained.

Also applies to: 977-979, 1087-1089


987-987: Implementation is correct and consistent.

The implementation of selection parameters:

  • Uses correct type hints ([int, str])
  • Is properly integrated into the argument groups
  • Maintains consistency with the existing codebase

Let's verify the usage of these parameters in the codebase:

Also applies to: 1073-1077, 1152-1152

✅ Verification successful

Based on the search results, I can now provide a final response:

Implementation is correct and consistent with codebase usage

The nsel and three_body_sel parameters are properly implemented and used consistently throughout the codebase:

  • The parameters accept both int and str types as defined in argcheck.py
  • They are used extensively in descriptor implementations (dpa1.py, dpa2.py) and neighbor list utilities
  • The parameters are properly documented with clear descriptions
  • Test files verify their functionality
  • Example configurations demonstrate proper usage with recommended values

The implementation maintains consistency with:

  • Type checking
  • Documentation standards
  • Integration with the descriptor architecture
  • Usage patterns across the codebase
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check the usage of nsel and three_body_sel parameters
# Test: Search for parameter usage in descriptor implementations
rg -A 5 "nsel|three_body_sel" --type python

Length of output: 76


Script:

#!/bin/bash
# Search for nsel parameter usage without file type restriction
rg -A 5 "nsel"

Length of output: 130805


Script:

#!/bin/bash
# Search for three_body_sel parameter usage without file type restriction
rg -A 5 "three_body_sel"

Length of output: 10019


Script:

#!/bin/bash
# Find Python files that might contain these parameters
fd ".py$" --exec grep -l "nsel\|three_body_sel" {}

Length of output: 2600

source/tests/pt/test_update_sel.py Show resolved Hide resolved
@github-actions github-actions bot added the Python label Nov 7, 2024
@wanghan-iapcm wanghan-iapcm added this pull request to the merge queue Nov 8, 2024
Merged via the queue into deepmodeling:devel with commit 3701566 Nov 8, 2024
51 checks passed
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.

[BUG] DPA-2 nsel and three_body_sel cannot be auto; three_body_sel is not checked
3 participants